1 | /* $Id: the-linux-kernel.h 24883 2009-11-23 18:09:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Include all necessary headers for the Linux kernel.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef ___the_linux_kernel_h
|
---|
32 | #define ___the_linux_kernel_h
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Include iprt/types.h to install the bool wrappers.
|
---|
36 | * Then use the linux bool type for all the stuff include here.
|
---|
37 | */
|
---|
38 | #include <iprt/types.h>
|
---|
39 | #define bool linux_bool
|
---|
40 |
|
---|
41 | #include <linux/autoconf.h>
|
---|
42 | #include <linux/version.h>
|
---|
43 |
|
---|
44 | /* We only support 2.4 and 2.6 series kernels */
|
---|
45 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
|
---|
46 | # error We only support 2.4 and 2.6 series kernels
|
---|
47 | #endif
|
---|
48 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
|
---|
49 | # error We only support 2.4 and 2.6 series kernels
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
|
---|
53 | # define MODVERSIONS
|
---|
54 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 71)
|
---|
55 | # include <linux/modversions.h>
|
---|
56 | # endif
|
---|
57 | #endif
|
---|
58 | #ifndef KBUILD_STR
|
---|
59 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
|
---|
60 | # define KBUILD_STR(s) s
|
---|
61 | # else
|
---|
62 | # define KBUILD_STR(s) #s
|
---|
63 | # endif
|
---|
64 | #endif
|
---|
65 | #include <linux/string.h>
|
---|
66 | #include <linux/spinlock.h>
|
---|
67 | #include <linux/slab.h>
|
---|
68 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
|
---|
69 | # include <linux/semaphore.h>
|
---|
70 | #else /* older kernels */
|
---|
71 | # include <asm/semaphore.h>
|
---|
72 | #endif /* older kernels */
|
---|
73 | #include <linux/module.h>
|
---|
74 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
75 | # include <linux/moduleparam.h>
|
---|
76 | #endif
|
---|
77 | #include <linux/kernel.h>
|
---|
78 | #include <linux/init.h>
|
---|
79 | #include <linux/fs.h>
|
---|
80 | #include <linux/mm.h>
|
---|
81 | #include <linux/pagemap.h>
|
---|
82 | #include <linux/slab.h>
|
---|
83 | #include <linux/time.h>
|
---|
84 | #include <linux/sched.h>
|
---|
85 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 7)
|
---|
86 | # include <linux/jiffies.h>
|
---|
87 | #endif
|
---|
88 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
|
---|
89 | # include <linux/ktime.h>
|
---|
90 | # include <linux/hrtimer.h>
|
---|
91 | #endif
|
---|
92 | #include <linux/wait.h>
|
---|
93 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 71)
|
---|
94 | # include <linux/cpu.h>
|
---|
95 | # include <linux/notifier.h>
|
---|
96 | #endif
|
---|
97 | /* For the basic additions module */
|
---|
98 | #include <linux/pci.h>
|
---|
99 | #include <linux/delay.h>
|
---|
100 | #include <linux/interrupt.h>
|
---|
101 | #include <linux/completion.h>
|
---|
102 | #include <linux/compiler.h>
|
---|
103 | #ifndef HAVE_UNLOCKED_IOCTL /* linux/fs.h defines this */
|
---|
104 | # include <linux/smp_lock.h>
|
---|
105 | #endif
|
---|
106 | /* For the shared folders module */
|
---|
107 | #include <linux/vmalloc.h>
|
---|
108 | #define wchar_t linux_wchar_t
|
---|
109 | #include <linux/nls.h>
|
---|
110 | #undef wchar_t
|
---|
111 | #include <asm/mman.h>
|
---|
112 | #include <asm/io.h>
|
---|
113 | #include <asm/uaccess.h>
|
---|
114 | #include <asm/div64.h>
|
---|
115 |
|
---|
116 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
|
---|
117 | # ifndef page_to_pfn
|
---|
118 | # define page_to_pfn(page) ((page) - mem_map)
|
---|
119 | # endif
|
---|
120 | #endif
|
---|
121 |
|
---|
122 | #ifndef DEFINE_WAIT
|
---|
123 | # define DEFINE_WAIT(name) DECLARE_WAITQUEUE(name, current)
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * 2.4 compatibility wrappers
|
---|
128 | */
|
---|
129 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 7)
|
---|
130 |
|
---|
131 | # ifndef MAX_JIFFY_OFFSET
|
---|
132 | # define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
|
---|
133 | # endif
|
---|
134 |
|
---|
135 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 29) || LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
136 |
|
---|
137 | DECLINLINE(unsigned int) jiffies_to_msecs(unsigned long cJiffies)
|
---|
138 | {
|
---|
139 | # if HZ <= 1000 && !(1000 % HZ)
|
---|
140 | return (1000 / HZ) * cJiffies;
|
---|
141 | # elif HZ > 1000 && !(HZ % 1000)
|
---|
142 | return (cJiffies + (HZ / 1000) - 1) / (HZ / 1000);
|
---|
143 | # else
|
---|
144 | return (cJiffies * 1000) / HZ;
|
---|
145 | # endif
|
---|
146 | }
|
---|
147 |
|
---|
148 | DECLINLINE(unsigned long) msecs_to_jiffies(unsigned int cMillies)
|
---|
149 | {
|
---|
150 | # if HZ > 1000
|
---|
151 | if (cMillies > jiffies_to_msecs(MAX_JIFFY_OFFSET))
|
---|
152 | return MAX_JIFFY_OFFSET;
|
---|
153 | # endif
|
---|
154 | # if HZ <= 1000 && !(1000 % HZ)
|
---|
155 | return (cMillies + (1000 / HZ) - 1) / (1000 / HZ);
|
---|
156 | # elif HZ > 1000 && !(HZ % 1000)
|
---|
157 | return cMillies * (HZ / 1000);
|
---|
158 | # else
|
---|
159 | return (cMillies * HZ + 999) / 1000;
|
---|
160 | # endif
|
---|
161 | }
|
---|
162 |
|
---|
163 | # endif /* < 2.4.29 || >= 2.6.0 */
|
---|
164 |
|
---|
165 | # define prepare_to_wait(q, wait, state) \
|
---|
166 | do { \
|
---|
167 | set_current_state(state); \
|
---|
168 | add_wait_queue(q, wait); \
|
---|
169 | } while (0)
|
---|
170 |
|
---|
171 | # define finish_wait(q, wait) \
|
---|
172 | do { \
|
---|
173 | unsigned long flags; \
|
---|
174 | set_current_state(TASK_RUNNING); \
|
---|
175 | spin_lock_irqsave(&(q)->lock, flags); \
|
---|
176 | list_del_init((q)->task_list); \
|
---|
177 | spin_unlock_irqrestore(&(q)->lock, flags); \
|
---|
178 | } while (0)
|
---|
179 |
|
---|
180 | #endif /* < 2.6.7 */
|
---|
181 |
|
---|
182 | /** @def TICK_NSEC
|
---|
183 | * The time between ticks in nsec */
|
---|
184 | #ifndef TICK_NSEC
|
---|
185 | # define TICK_NSEC (1000000000UL / HZ)
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * This sucks soooo badly on x86! Why don't they export __PAGE_KERNEL_EXEC so PAGE_KERNEL_EXEC would be usable?
|
---|
190 | */
|
---|
191 | #if defined(RT_ARCH_AMD64)
|
---|
192 | # define MY_PAGE_KERNEL_EXEC PAGE_KERNEL_EXEC
|
---|
193 | #elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
|
---|
194 | # ifdef __PAGE_KERNEL_EXEC
|
---|
195 | /* >= 2.6.27 */
|
---|
196 | # define MY_PAGE_KERNEL_EXEC __pgprot(cpu_has_pge ? __PAGE_KERNEL_EXEC | _PAGE_GLOBAL : __PAGE_KERNEL_EXEC)
|
---|
197 | # else
|
---|
198 | # define MY_PAGE_KERNEL_EXEC __pgprot(cpu_has_pge ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC)
|
---|
199 | # endif
|
---|
200 | #else
|
---|
201 | # define MY_PAGE_KERNEL_EXEC PAGE_KERNEL
|
---|
202 | #endif
|
---|
203 |
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * The redhat hack section.
|
---|
207 | * - The current hacks are for 2.4.21-15.EL only.
|
---|
208 | */
|
---|
209 | #ifndef NO_REDHAT_HACKS
|
---|
210 | /* accounting. */
|
---|
211 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
|
---|
212 | # ifdef VM_ACCOUNT
|
---|
213 | # define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c, 0) /* should it be 1 or 0? */
|
---|
214 | # endif
|
---|
215 | # endif
|
---|
216 |
|
---|
217 | /* backported remap_page_range. */
|
---|
218 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
|
---|
219 | # include <asm/tlb.h>
|
---|
220 | # ifdef tlb_vma /* probably not good enough... */
|
---|
221 | # define HAVE_26_STYLE_REMAP_PAGE_RANGE 1
|
---|
222 | # endif
|
---|
223 | # endif
|
---|
224 |
|
---|
225 | # ifndef RT_ARCH_AMD64
|
---|
226 | /* In 2.6.9-22.ELsmp we have to call change_page_attr() twice when changing
|
---|
227 | * the page attributes from PAGE_KERNEL to something else, because there appears
|
---|
228 | * to be a bug in one of the many patches that redhat applied.
|
---|
229 | * It should be safe to do this on less buggy linux kernels too. ;-)
|
---|
230 | */
|
---|
231 | # define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
|
---|
232 | do { \
|
---|
233 | if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) \
|
---|
234 | change_page_attr(pPages, cPages, prot); \
|
---|
235 | change_page_attr(pPages, cPages, prot); \
|
---|
236 | } while (0)
|
---|
237 | # endif /* !RT_ARCH_AMD64 */
|
---|
238 | #endif /* !NO_REDHAT_HACKS */
|
---|
239 |
|
---|
240 | #ifndef MY_DO_MUNMAP
|
---|
241 | # define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c)
|
---|
242 | #endif
|
---|
243 |
|
---|
244 | #ifndef MY_CHANGE_PAGE_ATTR
|
---|
245 | # ifdef RT_ARCH_AMD64 /** @todo This is a cheap hack, but it'll get around that 'else BUG();' in __change_page_attr(). */
|
---|
246 | # define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
|
---|
247 | do { \
|
---|
248 | change_page_attr(pPages, cPages, PAGE_KERNEL_NOCACHE); \
|
---|
249 | change_page_attr(pPages, cPages, prot); \
|
---|
250 | } while (0)
|
---|
251 | # else
|
---|
252 | # define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) change_page_attr(pPages, cPages, prot)
|
---|
253 | # endif
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
|
---|
257 | # define MY_SET_PAGES_EXEC(pPages, cPages) set_pages_x(pPages, cPages)
|
---|
258 | # define MY_SET_PAGES_NOEXEC(pPages, cPages) set_pages_nx(pPages, cPages)
|
---|
259 | #else
|
---|
260 | # define MY_SET_PAGES_EXEC(pPages, cPages) \
|
---|
261 | do { \
|
---|
262 | if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL)) \
|
---|
263 | MY_CHANGE_PAGE_ATTR(pPages, cPages, MY_PAGE_KERNEL_EXEC); \
|
---|
264 | } while (0)
|
---|
265 | # define MY_SET_PAGES_NOEXEC(pPages, cPages) \
|
---|
266 | do { \
|
---|
267 | if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL)) \
|
---|
268 | MY_CHANGE_PAGE_ATTR(pPages, cPages, PAGE_KERNEL); \
|
---|
269 | } while (0)
|
---|
270 | #endif
|
---|
271 |
|
---|
272 | /** @def ONE_MSEC_IN_JIFFIES
|
---|
273 | * The number of jiffies that make up 1 millisecond. Must be at least 1! */
|
---|
274 | #if HZ <= 1000
|
---|
275 | # define ONE_MSEC_IN_JIFFIES 1
|
---|
276 | #elif !(HZ % 1000)
|
---|
277 | # define ONE_MSEC_IN_JIFFIES (HZ / 1000)
|
---|
278 | #else
|
---|
279 | # define ONE_MSEC_IN_JIFFIES ((HZ + 999) / 1000)
|
---|
280 | # error "HZ is not a multiple of 1000, the GIP stuff won't work right!"
|
---|
281 | #endif
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Stop using the linux bool type.
|
---|
285 | */
|
---|
286 | #undef bool
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * There are post-2.6.24 kernels (confusingly with unchanged version number)
|
---|
290 | * which eliminate macros which were marked as deprecated.
|
---|
291 | */
|
---|
292 | #ifndef __attribute_used__
|
---|
293 | #define __attribute_used__ __used
|
---|
294 | #endif
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Hack for shortening pointers on linux so we can stuff more stuff into the
|
---|
298 | * task_struct::comm field. This is used by the semaphore code but put here
|
---|
299 | * because we don't have any better place atm. Don't use outside IPRT, please.
|
---|
300 | */
|
---|
301 | #ifdef RT_ARCH_AMD64
|
---|
302 | # define IPRT_DEBUG_SEMS_ADDRESS(addr) ( ((long)(addr) & (long)~UINT64_C(0xfffffff000000000)) )
|
---|
303 | #else
|
---|
304 | # define IPRT_DEBUG_SEMS_ADDRESS(addr) ( (long)(addr) )
|
---|
305 | #endif
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * There are some conflicting defines in iprt/param.h, sort them out here.
|
---|
309 | */
|
---|
310 | #ifndef ___iprt_param_h
|
---|
311 | # undef PAGE_SIZE
|
---|
312 | # undef PAGE_OFFSET_MASK
|
---|
313 | # include <iprt/param.h>
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | #endif
|
---|
317 |
|
---|