VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/the-linux-kernel.h@ 95131

Last change on this file since 95131 was 95131, checked in by vboxsync, 3 years ago

the-linux-kernel.h: bugref:10209 Include fpu/api.h for kernel_fpu_begin/end().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 KB
Line 
1/* $Id: the-linux-kernel.h 95131 2022-05-27 12:19:10Z vboxsync $ */
2/** @file
3 * IPRT - Include all necessary headers for the Linux kernel.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef IPRT_INCLUDED_SRC_r0drv_linux_the_linux_kernel_h
28#define IPRT_INCLUDED_SRC_r0drv_linux_the_linux_kernel_h
29#ifndef RT_WITHOUT_PRAGMA_ONCE
30# pragma once
31#endif
32
33/*
34 * Include iprt/types.h to install the bool wrappers.
35 * Then use the linux bool type for all the stuff include here.
36 */
37#include <iprt/types.h>
38#define bool linux_bool
39
40#if RT_GNUC_PREREQ(4, 6)
41# pragma GCC diagnostic push
42#endif
43#if RT_GNUC_PREREQ(4, 2)
44# pragma GCC diagnostic ignored "-Wunused-parameter"
45# if !defined(__cplusplus) && RT_GNUC_PREREQ(4, 3)
46# pragma GCC diagnostic ignored "-Wold-style-declaration" /* 2.6.18-411.0.0.0.1.el5/build/include/asm/apic.h:110: warning: 'inline' is not at beginning of declaration [-Wold-style-declaration] */
47# endif
48#endif
49
50
51#include <iprt/linux/version.h>
52#if RTLNX_VER_MIN(2,6,33)
53# include <generated/autoconf.h>
54#else
55# ifndef AUTOCONF_INCLUDED
56# include <linux/autoconf.h>
57# endif
58#endif
59
60/* We only support 2.4 and 2.6 series kernels */
61#if RTLNX_VER_MAX(2,4,0)
62# error Sorry, we do not support 2.3 and earlier kernels.
63#endif
64#if RTLNX_VER_MIN(2,5,0) && RTLNX_VER_MAX(2,6,0)
65# error Sorry, we do not support 2.5 series kernels (might work though).
66#endif
67
68#if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
69# define MODVERSIONS
70# if RTLNX_VER_MAX(2,5,71)
71# include <linux/modversions.h>
72# endif
73#endif
74#ifndef KBUILD_STR
75# if RTLNX_VER_MAX(2,6,16)
76# define KBUILD_STR(s) s
77# else
78# define KBUILD_STR(s) #s
79# endif
80#endif
81# if RTLNX_VER_MIN(3,3,0)
82# include <linux/kconfig.h> /* for macro IS_ENABLED */
83# endif
84#include <linux/string.h>
85#include <linux/spinlock.h>
86#include <linux/slab.h>
87#if RTLNX_VER_MIN(2,6,27)
88# include <linux/semaphore.h>
89#else /* older kernels */
90# include <asm/semaphore.h>
91#endif /* older kernels */
92#include <linux/module.h>
93#if RTLNX_VER_MIN(2,6,0)
94# include <linux/moduleparam.h>
95#endif
96#include <linux/kernel.h>
97#include <linux/init.h>
98#include <linux/fs.h>
99#if RTLNX_VER_MIN(2,6,0)
100# include <linux/namei.h>
101#endif
102#include <linux/mm.h>
103#include <linux/pagemap.h>
104#include <linux/slab.h>
105#include <linux/time.h>
106#include <linux/sched.h>
107
108#if RTLNX_VER_RANGE(3,9,23, 3,9,31)
109# include <linux/splice.h>
110#endif
111
112#if RTLNX_VER_MIN(3,9,0)
113# include <linux/sched/rt.h>
114#endif
115#if RTLNX_VER_MIN(4,11,0)
116# include <linux/sched/signal.h>
117# include <linux/sched/types.h>
118#endif
119#if RTLNX_VER_MIN(2,6,7)
120# include <linux/jiffies.h>
121#endif
122#if RTLNX_VER_MIN(2,6,16)
123# include <linux/ktime.h>
124# include <linux/hrtimer.h>
125#endif
126#include <linux/wait.h>
127#if RTLNX_VER_MIN(2,5,71)
128# include <linux/cpu.h>
129# include <linux/notifier.h>
130#endif
131#if RTLNX_VER_MIN(5,1,0)
132# include <uapi/linux/mman.h>
133#endif
134/* For the basic additions module */
135#include <linux/pci.h>
136#include <linux/delay.h>
137#include <linux/interrupt.h>
138#include <linux/completion.h>
139#include <linux/compiler.h>
140#if RTLNX_VER_MIN(5,9,0) || RTLNX_SUSE_MAJ_PREREQ(15,3) /* linux/fs.h defined HAVE_UNLOCKED_IOCTL from 2.6.11 up to 5.9 (also 5.3.18-56 in SLES15-SP3), when it became an implicit assumption. */
141# define HAVE_UNLOCKED_IOCTL 1 /* We use this in a couple of places, so for now just define it for 5.9+ too. */
142#endif
143#if !defined(HAVE_UNLOCKED_IOCTL) && RTLNX_VER_MAX(2,6,38)
144# include <linux/smp_lock.h>
145#endif
146/* For the shared folders module */
147#include <linux/vmalloc.h>
148#define wchar_t linux_wchar_t
149#include <linux/nls.h>
150#undef wchar_t
151#include <asm/mman.h>
152#include <asm/io.h>
153#include <asm/uaccess.h>
154#include <asm/div64.h>
155
156/* For thread-context hooks. */
157#if RTLNX_VER_MIN(2,6,18) && defined(CONFIG_PREEMPT_NOTIFIERS)
158# include <linux/preempt.h>
159#endif
160
161/* for workqueue / task queues. */
162#if RTLNX_VER_MIN(2,5,41)
163# include <linux/workqueue.h>
164#else
165# include <linux/tqueue.h>
166#endif
167
168#if RTLNX_VER_MIN(2,6,4)
169# include <linux/kthread.h>
170#endif
171
172/* for cr4_init_shadow() / cpu_tlbstate. */
173#if RTLNX_VER_MIN(3,20,0)
174# include <asm/tlbflush.h>
175#endif
176
177/* for set_pages_x() */
178#if RTLNX_VER_MIN(4,12,0)
179# include <asm/set_memory.h>
180#endif
181
182/* for __flush_tlb_all() */
183#if RTLNX_VER_MIN(2,6,28) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
184# include <asm/tlbflush.h>
185#endif
186
187/* for kernel_fpu_begin / kernel_fpu_end() */
188#if RTLNX_VER_MIN(4,20,0)
189# include <asm/fpu/api.h>
190#endif
191
192#if RTLNX_VER_MIN(3,7,0)
193# include <asm/smap.h>
194#else
195static inline void clac(void) { }
196static inline void stac(void) { }
197#endif
198
199#if RTLNX_VER_MAX(2,6,0)
200# ifndef page_to_pfn
201# define page_to_pfn(page) ((page) - mem_map)
202# endif
203#endif
204
205#ifndef DEFINE_WAIT
206# define DEFINE_WAIT(name) DECLARE_WAITQUEUE(name, current)
207#endif
208
209#ifndef __GFP_NOWARN
210# define __GFP_NOWARN 0
211#endif
212
213/*
214 * 2.4 / early 2.6 compatibility wrappers
215 */
216#if RTLNX_VER_MAX(2,6,7)
217
218# ifndef MAX_JIFFY_OFFSET
219# define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
220# endif
221
222# if RTLNX_VER_MAX(2,4,29) || RTLNX_VER_MIN(2,6,0)
223
224DECLINLINE(unsigned int) jiffies_to_msecs(unsigned long cJiffies)
225{
226# if HZ <= 1000 && !(1000 % HZ)
227 return (1000 / HZ) * cJiffies;
228# elif HZ > 1000 && !(HZ % 1000)
229 return (cJiffies + (HZ / 1000) - 1) / (HZ / 1000);
230# else
231 return (cJiffies * 1000) / HZ;
232# endif
233}
234
235DECLINLINE(unsigned long) msecs_to_jiffies(unsigned int cMillies)
236{
237# if HZ > 1000
238 if (cMillies > jiffies_to_msecs(MAX_JIFFY_OFFSET))
239 return MAX_JIFFY_OFFSET;
240# endif
241# if HZ <= 1000 && !(1000 % HZ)
242 return (cMillies + (1000 / HZ) - 1) / (1000 / HZ);
243# elif HZ > 1000 && !(HZ % 1000)
244 return cMillies * (HZ / 1000);
245# else
246 return (cMillies * HZ + 999) / 1000;
247# endif
248}
249
250# endif /* < 2.4.29 || >= 2.6.0 */
251
252#endif /* < 2.6.7 */
253
254/*
255 * 2.4 compatibility wrappers
256 */
257#if RTLNX_VER_MAX(2,6,0)
258
259# define prepare_to_wait(q, wait, state) \
260 do { \
261 add_wait_queue(q, wait); \
262 set_current_state(state); \
263 } while (0)
264
265# define after_wait(wait) \
266 do { \
267 list_del_init(&(wait)->task_list); \
268 } while (0)
269
270# define finish_wait(q, wait) \
271 do { \
272 set_current_state(TASK_RUNNING); \
273 remove_wait_queue(q, wait); \
274 } while (0)
275
276#else /* >= 2.6.0 */
277
278# define after_wait(wait) do {} while (0)
279
280#endif /* >= 2.6.0 */
281
282/** @def TICK_NSEC
283 * The time between ticks in nsec */
284#ifndef TICK_NSEC
285# define TICK_NSEC (1000000000UL / HZ)
286#endif
287
288/*
289 * This sucks soooo badly on x86! Why don't they export __PAGE_KERNEL_EXEC so PAGE_KERNEL_EXEC would be usable?
290 */
291#if RTLNX_VER_MIN(2,6,8) && defined(RT_ARCH_AMD64)
292# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL_EXEC
293#elif RTLNX_VER_MIN(2,6,8) && defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
294# ifdef __PAGE_KERNEL_EXEC
295 /* >= 2.6.27 */
296# define MY_PAGE_KERNEL_EXEC __pgprot(boot_cpu_has(X86_FEATURE_PGE) ? __PAGE_KERNEL_EXEC | _PAGE_GLOBAL : __PAGE_KERNEL_EXEC)
297# else
298# define MY_PAGE_KERNEL_EXEC __pgprot(boot_cpu_has(X86_FEATURE_PGE) ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC)
299# endif
300#else
301# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL
302#endif
303
304
305/*
306 * The redhat hack section.
307 * - The current hacks are for 2.4.21-15.EL only.
308 */
309#ifndef NO_REDHAT_HACKS
310/* accounting. */
311# if RTLNX_VER_MAX(2,6,0)
312# ifdef VM_ACCOUNT
313# define USE_RHEL4_MUNMAP
314# endif
315# endif
316
317/* backported remap_page_range. */
318# if RTLNX_VER_MAX(2,6,0)
319# include <asm/tlb.h>
320# ifdef tlb_vma /* probably not good enough... */
321# define HAVE_26_STYLE_REMAP_PAGE_RANGE 1
322# endif
323# endif
324
325# ifndef RT_ARCH_AMD64
326/* In 2.6.9-22.ELsmp we have to call change_page_attr() twice when changing
327 * the page attributes from PAGE_KERNEL to something else, because there appears
328 * to be a bug in one of the many patches that redhat applied.
329 * It should be safe to do this on less buggy linux kernels too. ;-)
330 */
331# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
332 do { \
333 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) \
334 change_page_attr(pPages, cPages, prot); \
335 change_page_attr(pPages, cPages, prot); \
336 } while (0)
337# endif /* !RT_ARCH_AMD64 */
338#endif /* !NO_REDHAT_HACKS */
339
340#ifndef MY_CHANGE_PAGE_ATTR
341# ifdef RT_ARCH_AMD64 /** @todo This is a cheap hack, but it'll get around that 'else BUG();' in __change_page_attr(). */
342# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
343 do { \
344 change_page_attr(pPages, cPages, PAGE_KERNEL_NOCACHE); \
345 change_page_attr(pPages, cPages, prot); \
346 } while (0)
347# else
348# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) change_page_attr(pPages, cPages, prot)
349# endif
350#endif
351
352#if RTLNX_VER_MIN(2,6,25)
353# if RTLNX_VER_MAX(5,4,0) /* The interface was removed, but we only need it for < 2.4.22, so who cares. */
354# define MY_SET_PAGES_EXEC(pPages, cPages) set_pages_x(pPages, cPages)
355# define MY_SET_PAGES_NOEXEC(pPages, cPages) set_pages_nx(pPages, cPages)
356# endif
357#else
358# define MY_SET_PAGES_EXEC(pPages, cPages) \
359 do { \
360 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL)) \
361 MY_CHANGE_PAGE_ATTR(pPages, cPages, MY_PAGE_KERNEL_EXEC); \
362 } while (0)
363# define MY_SET_PAGES_NOEXEC(pPages, cPages) \
364 do { \
365 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL)) \
366 MY_CHANGE_PAGE_ATTR(pPages, cPages, PAGE_KERNEL); \
367 } while (0)
368#endif
369
370/** @def ONE_MSEC_IN_JIFFIES
371 * The number of jiffies that make up 1 millisecond. Must be at least 1! */
372#if HZ <= 1000
373# define ONE_MSEC_IN_JIFFIES 1
374#elif !(HZ % 1000)
375# define ONE_MSEC_IN_JIFFIES (HZ / 1000)
376#else
377# define ONE_MSEC_IN_JIFFIES ((HZ + 999) / 1000)
378# error "HZ is not a multiple of 1000, the GIP stuff won't work right!"
379#endif
380
381/*
382 * Stop using the linux bool type.
383 */
384#undef bool
385
386#if RT_GNUC_PREREQ(4, 6)
387# pragma GCC diagnostic pop
388#endif
389
390/*
391 * There are post-2.6.24 kernels (confusingly with unchanged version number)
392 * which eliminate macros which were marked as deprecated.
393 */
394#ifndef __attribute_used__
395#define __attribute_used__ __used
396#endif
397
398/**
399 * Hack for shortening pointers on linux so we can stuff more stuff into the
400 * task_struct::comm field. This is used by the semaphore code but put here
401 * because we don't have any better place atm. Don't use outside IPRT, please.
402 */
403#ifdef RT_ARCH_AMD64
404# define IPRT_DEBUG_SEMS_ADDRESS(addr) ( ((long)(addr) & (long)~UINT64_C(0xfffffff000000000)) )
405#else
406# define IPRT_DEBUG_SEMS_ADDRESS(addr) ( (long)(addr) )
407#endif
408
409/**
410 * Puts semaphore info into the task_struct::comm field if IPRT_DEBUG_SEMS is
411 * defined.
412 */
413#ifdef IPRT_DEBUG_SEMS
414# define IPRT_DEBUG_SEMS_STATE(pThis, chState) \
415 snprintf(current->comm, sizeof(current->comm), "%c%lx", (chState), IPRT_DEBUG_SEMS_ADDRESS(pThis));
416#else
417# define IPRT_DEBUG_SEMS_STATE(pThis, chState) do { } while (0)
418#endif
419
420/**
421 * Puts semaphore info into the task_struct::comm field if IPRT_DEBUG_SEMS is
422 * defined.
423 */
424#ifdef IPRT_DEBUG_SEMS
425# define IPRT_DEBUG_SEMS_STATE_RC(pThis, chState, rc) \
426 snprintf(current->comm, sizeof(current->comm), "%c%lx:%d", (chState), IPRT_DEBUG_SEMS_ADDRESS(pThis), rc);
427#else
428# define IPRT_DEBUG_SEMS_STATE_RC(pThis, chState, rc) do { } while (0)
429#endif
430
431/** @name Macros for preserving EFLAGS.AC on 3.19+/amd64 paranoid.
432 * The AMD 64 switch_to in macro in arch/x86/include/asm/switch_to.h stopped
433 * restoring flags.
434 * @{ */
435#if (defined(CONFIG_X86_SMAP) || defined(RT_STRICT) || defined(IPRT_WITH_EFLAGS_AC_PRESERVING)) \
436 && !defined(IPRT_WITHOUT_EFLAGS_AC_PRESERVING)
437# include <iprt/asm-amd64-x86.h>
438# define IPRT_X86_EFL_AC RT_BIT(18)
439# define IPRT_LINUX_SAVE_EFL_AC() RTCCUINTREG fSavedEfl = ASMGetFlags()
440# define IPRT_LINUX_RESTORE_EFL_AC() ASMSetFlags(fSavedEfl)
441# define IPRT_LINUX_RESTORE_EFL_ONLY_AC() ASMChangeFlags(~IPRT_X86_EFL_AC, fSavedEfl & IPRT_X86_EFL_AC)
442#else
443# define IPRT_LINUX_SAVE_EFL_AC() do { } while (0)
444# define IPRT_LINUX_RESTORE_EFL_AC() do { } while (0)
445# define IPRT_LINUX_RESTORE_EFL_ONLY_AC() do { } while (0)
446#endif
447/** @} */
448
449/*
450 * There are some conflicting defines in iprt/param.h, sort them out here.
451 */
452#ifndef IPRT_INCLUDED_param_h
453# undef PAGE_SIZE
454# undef PAGE_OFFSET_MASK
455# include <iprt/param.h>
456#endif
457
458/*
459 * Some global indicator macros.
460 */
461/** @def IPRT_LINUX_HAS_HRTIMER
462 * Whether the kernel support high resolution timers (Linux kernel versions
463 * 2.6.28 and later (hrtimer_add_expires_ns() & schedule_hrtimeout). */
464#if RTLNX_VER_MIN(2,6,28)
465# define IPRT_LINUX_HAS_HRTIMER
466#endif
467
468/*
469 * Workqueue stuff, see initterm-r0drv-linux.c.
470 */
471#if RTLNX_VER_MIN(2,5,41)
472typedef struct work_struct RTR0LNXWORKQUEUEITEM;
473#else
474typedef struct tq_struct RTR0LNXWORKQUEUEITEM;
475#endif
476DECLHIDDEN(void) rtR0LnxWorkqueuePush(RTR0LNXWORKQUEUEITEM *pWork, void (*pfnWorker)(RTR0LNXWORKQUEUEITEM *));
477DECLHIDDEN(void) rtR0LnxWorkqueueFlush(void);
478
479/*
480 * Memory hacks from memobj-r0drv-linux.c that shared folders need.
481 */
482RTDECL(struct page *) rtR0MemObjLinuxVirtToPage(void *pv);
483
484#endif /* !IPRT_INCLUDED_SRC_r0drv_linux_the_linux_kernel_h */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette