1 /* $Id: the-linux-kernel.h $ */
3 * IPRT - Include all necessary headers for the Linux kernel.
7 * Copyright (C) 2006-2016 Oracle Corporation
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.
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.
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.
27 #ifndef ___the_linux_kernel_h
28 #define ___the_linux_kernel_h
31 * Include iprt/types.h to install the bool wrappers.
32 * Then use the linux bool type for all the stuff include here.
34 #include <iprt/types.h>
35 #define bool linux_bool
37 #if RT_GNUC_PREREQ(4, 6)
38 # pragma GCC diagnostic push
40 #if RT_GNUC_PREREQ(4, 2)
41 # pragma GCC diagnostic ignored "-Wunused-parameter"
42 # if !defined(__cplusplus) && RT_GNUC_PREREQ(4, 3)
43 # 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 #include <linux/version.h>
48 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
49 # include <generated/autoconf.h>
51 # ifndef AUTOCONF_INCLUDED
52 # include <linux/autoconf.h>
56 /* We only support 2.4 and 2.6 series kernels */
57 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
58 # error We only support 2.4 and 2.6 series kernels
60 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
61 # error We only support 2.4 and 2.6 series kernels
64 #if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
66 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 71)
67 # include <linux/modversions.h>
71 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
72 # define KBUILD_STR(s) s
74 # define KBUILD_STR(s) #s
77 # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
78 # include <linux/kconfig.h> /* for macro IS_ENABLED */
80 #include <linux/string.h>
81 #include <linux/spinlock.h>
82 #include <linux/slab.h>
83 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
84 # include <linux/semaphore.h>
85 #else /* older kernels */
86 # include <asm/semaphore.h>
87 #endif /* older kernels */
88 #include <linux/module.h>
89 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
90 # include <linux/moduleparam.h>
92 #include <linux/kernel.h>
93 #include <linux/init.h>
95 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
96 # include <linux/namei.h>
99 #include <linux/pagemap.h>
100 #include <linux/slab.h>
101 #include <linux/time.h>
102 #include <linux/sched.h>
103 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
104 # include <linux/sched/rt.h>
106 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 7)
107 # include <linux/jiffies.h>
109 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
110 # include <linux/ktime.h>
111 # include <linux/hrtimer.h>
113 #include <linux/wait.h>
114 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 71)
115 # include <linux/cpu.h>
116 # include <linux/notifier.h>
118 /* For the basic additions module */
119 #include <linux/pci.h>
120 #include <linux/delay.h>
121 #include <linux/interrupt.h>
122 #include <linux/completion.h>
123 #include <linux/compiler.h>
124 #ifndef HAVE_UNLOCKED_IOCTL /* linux/fs.h defines this */
125 # include <linux/smp_lock.h>
127 /* For the shared folders module */
128 #include <linux/vmalloc.h>
129 #define wchar_t linux_wchar_t
130 #include <linux/nls.h>
132 #include <asm/mman.h>
134 #include <asm/uaccess.h>
135 #include <asm/div64.h>
137 /* For thread-context hooks. */
138 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18) && defined(CONFIG_PREEMPT_NOTIFIERS)
139 # include <linux/preempt.h>
142 /* for workqueue / task queues. */
143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 41)
144 # include <linux/workqueue.h>
146 # include <linux/tqueue.h>
149 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
150 # include <linux/kthread.h>
153 /* for cr4_init_shadow() / cpu_tlbstate. */
154 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 20, 0)
155 # include <asm/tlbflush.h>
158 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
159 # include <asm/smap.h>
161 static inline void clac(void) { }
162 static inline void stac(void) { }
165 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
167 # define page_to_pfn(page) ((page) - mem_map)
172 # define DEFINE_WAIT(name) DECLARE_WAITQUEUE(name, current)
176 # define __GFP_NOWARN 0
180 * 2.4 / early 2.6 compatibility wrappers
182 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 7)
184 # ifndef MAX_JIFFY_OFFSET
185 # define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
188 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 29) || LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
190 DECLINLINE(unsigned int) jiffies_to_msecs(unsigned long cJiffies
)
192 # if HZ <= 1000 && !(1000 % HZ)
193 return (1000 / HZ
) * cJiffies
;
194 # elif HZ > 1000 && !(HZ % 1000)
195 return (cJiffies
+ (HZ
/ 1000) - 1) / (HZ
/ 1000);
197 return (cJiffies
* 1000) / HZ
;
201 DECLINLINE(unsigned long) msecs_to_jiffies(unsigned int cMillies
)
204 if (cMillies
> jiffies_to_msecs(MAX_JIFFY_OFFSET
))
205 return MAX_JIFFY_OFFSET
;
207 # if HZ <= 1000 && !(1000 % HZ)
208 return (cMillies
+ (1000 / HZ
) - 1) / (1000 / HZ
);
209 # elif HZ > 1000 && !(HZ % 1000)
210 return cMillies
* (HZ
/ 1000);
212 return (cMillies
* HZ
+ 999) / 1000;
216 # endif /* < 2.4.29 || >= 2.6.0 */
221 * 2.4 compatibility wrappers
223 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
225 # define prepare_to_wait(q, wait, state) \
227 add_wait_queue(q, wait); \
228 set_current_state(state); \
231 # define after_wait(wait) \
233 list_del_init(&(wait)->task_list); \
236 # define finish_wait(q, wait) \
238 set_current_state(TASK_RUNNING); \
239 remove_wait_queue(q, wait); \
244 # define after_wait(wait) do {} while (0)
246 #endif /* >= 2.6.0 */
249 * The time between ticks in nsec */
251 # define TICK_NSEC (1000000000UL / HZ)
255 * This sucks soooo badly on x86! Why don't they export __PAGE_KERNEL_EXEC so PAGE_KERNEL_EXEC would be usable?
257 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) && defined(RT_ARCH_AMD64)
258 # define MY_PAGE_KERNEL_EXEC PAGE_KERNEL_EXEC
259 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) && defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
260 # ifdef __PAGE_KERNEL_EXEC
262 # define MY_PAGE_KERNEL_EXEC __pgprot(boot_cpu_has(X86_FEATURE_PGE) ? __PAGE_KERNEL_EXEC | _PAGE_GLOBAL : __PAGE_KERNEL_EXEC)
264 # define MY_PAGE_KERNEL_EXEC __pgprot(boot_cpu_has(X86_FEATURE_PGE) ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC)
267 # define MY_PAGE_KERNEL_EXEC PAGE_KERNEL
272 * The redhat hack section.
273 * - The current hacks are for 2.4.21-15.EL only.
275 #ifndef NO_REDHAT_HACKS
277 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
279 # define USE_RHEL4_MUNMAP
283 /* backported remap_page_range. */
284 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
285 # include <asm/tlb.h>
286 # ifdef tlb_vma /* probably not good enough... */
287 # define HAVE_26_STYLE_REMAP_PAGE_RANGE 1
291 # ifndef RT_ARCH_AMD64
292 /* In 2.6.9-22.ELsmp we have to call change_page_attr() twice when changing
293 * the page attributes from PAGE_KERNEL to something else, because there appears
294 * to be a bug in one of the many patches that redhat applied.
295 * It should be safe to do this on less buggy linux kernels too. ;-)
297 # define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
299 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) \
300 change_page_attr(pPages, cPages, prot); \
301 change_page_attr(pPages, cPages, prot); \
303 # endif /* !RT_ARCH_AMD64 */
304 #endif /* !NO_REDHAT_HACKS */
306 #ifndef MY_CHANGE_PAGE_ATTR
307 # ifdef RT_ARCH_AMD64 /** @todo This is a cheap hack, but it'll get around that 'else BUG();' in __change_page_attr(). */
308 # define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
310 change_page_attr(pPages, cPages, PAGE_KERNEL_NOCACHE); \
311 change_page_attr(pPages, cPages, prot); \
314 # define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) change_page_attr(pPages, cPages, prot)
318 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
319 # define MY_SET_PAGES_EXEC(pPages, cPages) set_pages_x(pPages, cPages)
320 # define MY_SET_PAGES_NOEXEC(pPages, cPages) set_pages_nx(pPages, cPages)
322 # define MY_SET_PAGES_EXEC(pPages, cPages) \
324 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL)) \
325 MY_CHANGE_PAGE_ATTR(pPages, cPages, MY_PAGE_KERNEL_EXEC); \
327 # define MY_SET_PAGES_NOEXEC(pPages, cPages) \
329 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL)) \
330 MY_CHANGE_PAGE_ATTR(pPages, cPages, PAGE_KERNEL); \
334 /** @def ONE_MSEC_IN_JIFFIES
335 * The number of jiffies that make up 1 millisecond. Must be at least 1! */
337 # define ONE_MSEC_IN_JIFFIES 1
339 # define ONE_MSEC_IN_JIFFIES (HZ / 1000)
341 # define ONE_MSEC_IN_JIFFIES ((HZ + 999) / 1000)
342 # error "HZ is not a multiple of 1000, the GIP stuff won't work right!"
346 * Stop using the linux bool type.
350 #if RT_GNUC_PREREQ(4, 6)
351 # pragma GCC diagnostic pop
355 * There are post-2.6.24 kernels (confusingly with unchanged version number)
356 * which eliminate macros which were marked as deprecated.
358 #ifndef __attribute_used__
359 #define __attribute_used__ __used
363 * Hack for shortening pointers on linux so we can stuff more stuff into the
364 * task_struct::comm field. This is used by the semaphore code but put here
365 * because we don't have any better place atm. Don't use outside IPRT, please.
368 # define IPRT_DEBUG_SEMS_ADDRESS(addr) ( ((long)(addr) & (long)~UINT64_C(0xfffffff000000000)) )
370 # define IPRT_DEBUG_SEMS_ADDRESS(addr) ( (long)(addr) )
374 * Puts semaphore info into the task_struct::comm field if IPRT_DEBUG_SEMS is
377 #ifdef IPRT_DEBUG_SEMS
378 # define IPRT_DEBUG_SEMS_STATE(pThis, chState) \
379 snprintf(current->comm, sizeof(current->comm), "%c%lx", (chState), IPRT_DEBUG_SEMS_ADDRESS(pThis));
381 # define IPRT_DEBUG_SEMS_STATE(pThis, chState) do { } while (0)
385 * Puts semaphore info into the task_struct::comm field if IPRT_DEBUG_SEMS is
388 #ifdef IPRT_DEBUG_SEMS
389 # define IPRT_DEBUG_SEMS_STATE_RC(pThis, chState, rc) \
390 snprintf(current->comm, sizeof(current->comm), "%c%lx:%d", (chState), IPRT_DEBUG_SEMS_ADDRESS(pThis), rc);
392 # define IPRT_DEBUG_SEMS_STATE_RC(pThis, chState, rc) do { } while (0)
395 /** @name Macros for preserving EFLAGS.AC on 3.19+/amd64 paranoid.
396 * The AMD 64 switch_to in macro in arch/x86/include/asm/switch_to.h stopped
399 #if defined(CONFIG_X86_SMAP) || defined(RT_STRICT) || defined(IPRT_WITH_EFLAGS_AC_PRESERVING)
400 # include <iprt/asm-amd64-x86.h>
401 # define IPRT_X86_EFL_AC RT_BIT(18)
402 # define IPRT_LINUX_SAVE_EFL_AC() RTCCUINTREG fSavedEfl = ASMGetFlags()
403 # define IPRT_LINUX_RESTORE_EFL_AC() ASMSetFlags(fSavedEfl)
404 # define IPRT_LINUX_RESTORE_EFL_ONLY_AC() ASMChangeFlags(~IPRT_X86_EFL_AC, fSavedEfl & IPRT_X86_EFL_AC)
406 # define IPRT_LINUX_SAVE_EFL_AC() do { } while (0)
407 # define IPRT_LINUX_RESTORE_EFL_AC() do { } while (0)
408 # define IPRT_LINUX_RESTORE_EFL_ONLY_AC() do { } while (0)
413 * There are some conflicting defines in iprt/param.h, sort them out here.
415 #ifndef ___iprt_param_h
417 # undef PAGE_OFFSET_MASK
418 # include <iprt/param.h>
422 * Some global indicator macros.
424 /** @def IPRT_LINUX_HAS_HRTIMER
425 * Whether the kernel support high resolution timers (Linux kernel versions
426 * 2.6.28 and later (hrtimer_add_expires_ns() & schedule_hrtimeout). */
427 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
428 # define IPRT_LINUX_HAS_HRTIMER
432 * Workqueue stuff, see initterm-r0drv-linux.c.
434 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 41)
435 typedef struct work_struct RTR0LNXWORKQUEUEITEM
;
437 typedef struct tq_struct RTR0LNXWORKQUEUEITEM
;
439 DECLHIDDEN(void) rtR0LnxWorkqueuePush(RTR0LNXWORKQUEUEITEM
*pWork
, void (*pfnWorker
)(RTR0LNXWORKQUEUEITEM
*));
440 DECLHIDDEN(void) rtR0LnxWorkqueueFlush(void);