VirtualBox

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

Last change on this file since 42586 was 41660, checked in by vboxsync, 13 years ago

IPRT/r0drv/Linux: make it work with Linux 3.5 kernels

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: the-linux-kernel.h 41660 2012-06-12 08:08:17Z vboxsync $ */
2/** @file
3 * IPRT - Include all necessary headers for the Linux kernel.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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 ___the_linux_kernel_h
28#define ___the_linux_kernel_h
29
30/*
31 * Include iprt/types.h to install the bool wrappers.
32 * Then use the linux bool type for all the stuff include here.
33 */
34#include <iprt/types.h>
35#define bool linux_bool
36
37#include <linux/version.h>
38#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
39# include <generated/autoconf.h>
40#else
41# ifndef AUTOCONF_INCLUDED
42# include <linux/autoconf.h>
43# endif
44#endif
45
46/* We only support 2.4 and 2.6 series kernels */
47#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
48# error We only support 2.4 and 2.6 series kernels
49#endif
50#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
51# error We only support 2.4 and 2.6 series kernels
52#endif
53
54#if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
55# define MODVERSIONS
56# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 71)
57# include <linux/modversions.h>
58# endif
59#endif
60#ifndef KBUILD_STR
61# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
62# define KBUILD_STR(s) s
63# else
64# define KBUILD_STR(s) #s
65# endif
66#endif
67# if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
68# include <linux/kconfig.h> /* for macro IS_ENABLED */
69# endif
70#include <linux/string.h>
71#include <linux/spinlock.h>
72#include <linux/slab.h>
73#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
74# include <linux/semaphore.h>
75#else /* older kernels */
76# include <asm/semaphore.h>
77#endif /* older kernels */
78#include <linux/module.h>
79#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
80# include <linux/moduleparam.h>
81#endif
82#include <linux/kernel.h>
83#include <linux/init.h>
84#include <linux/fs.h>
85#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
86# include <linux/namei.h>
87#endif
88#include <linux/mm.h>
89#include <linux/pagemap.h>
90#include <linux/slab.h>
91#include <linux/time.h>
92#include <linux/sched.h>
93#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 7)
94# include <linux/jiffies.h>
95#endif
96#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
97# include <linux/ktime.h>
98# include <linux/hrtimer.h>
99#endif
100#include <linux/wait.h>
101#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 71)
102# include <linux/cpu.h>
103# include <linux/notifier.h>
104#endif
105/* For the basic additions module */
106#include <linux/pci.h>
107#include <linux/delay.h>
108#include <linux/interrupt.h>
109#include <linux/completion.h>
110#include <linux/compiler.h>
111#ifndef HAVE_UNLOCKED_IOCTL /* linux/fs.h defines this */
112# include <linux/smp_lock.h>
113#endif
114/* For the shared folders module */
115#include <linux/vmalloc.h>
116#define wchar_t linux_wchar_t
117#include <linux/nls.h>
118#undef wchar_t
119#include <asm/mman.h>
120#include <asm/io.h>
121#include <asm/uaccess.h>
122#include <asm/div64.h>
123
124/* for workqueue / task queues. */
125#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 41)
126# include <linux/workqueue.h>
127#else
128# include <linux/tqueue.h>
129#endif
130
131#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
132# include <linux/kthread.h>
133#endif
134
135#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
136# ifndef page_to_pfn
137# define page_to_pfn(page) ((page) - mem_map)
138# endif
139#endif
140
141#ifndef DEFINE_WAIT
142# define DEFINE_WAIT(name) DECLARE_WAITQUEUE(name, current)
143#endif
144
145/*
146 * 2.4 / early 2.6 compatibility wrappers
147 */
148#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 7)
149
150# ifndef MAX_JIFFY_OFFSET
151# define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
152# endif
153
154# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 29) || LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
155
156DECLINLINE(unsigned int) jiffies_to_msecs(unsigned long cJiffies)
157{
158# if HZ <= 1000 && !(1000 % HZ)
159 return (1000 / HZ) * cJiffies;
160# elif HZ > 1000 && !(HZ % 1000)
161 return (cJiffies + (HZ / 1000) - 1) / (HZ / 1000);
162# else
163 return (cJiffies * 1000) / HZ;
164# endif
165}
166
167DECLINLINE(unsigned long) msecs_to_jiffies(unsigned int cMillies)
168{
169# if HZ > 1000
170 if (cMillies > jiffies_to_msecs(MAX_JIFFY_OFFSET))
171 return MAX_JIFFY_OFFSET;
172# endif
173# if HZ <= 1000 && !(1000 % HZ)
174 return (cMillies + (1000 / HZ) - 1) / (1000 / HZ);
175# elif HZ > 1000 && !(HZ % 1000)
176 return cMillies * (HZ / 1000);
177# else
178 return (cMillies * HZ + 999) / 1000;
179# endif
180}
181
182# endif /* < 2.4.29 || >= 2.6.0 */
183
184#endif /* < 2.6.7 */
185
186/*
187 * 2.4 compatibility wrappers
188 */
189#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
190
191# define prepare_to_wait(q, wait, state) \
192 do { \
193 add_wait_queue(q, wait); \
194 set_current_state(state); \
195 } while (0)
196
197# define after_wait(wait) \
198 do { \
199 list_del_init(&(wait)->task_list); \
200 } while (0)
201
202# define finish_wait(q, wait) \
203 do { \
204 set_current_state(TASK_RUNNING); \
205 remove_wait_queue(q, wait); \
206 } while (0)
207
208#else /* >= 2.6.0 */
209
210# define after_wait(wait) do {} while (0)
211
212#endif /* >= 2.6.0 */
213
214/** @def TICK_NSEC
215 * The time between ticks in nsec */
216#ifndef TICK_NSEC
217# define TICK_NSEC (1000000000UL / HZ)
218#endif
219
220/*
221 * This sucks soooo badly on x86! Why don't they export __PAGE_KERNEL_EXEC so PAGE_KERNEL_EXEC would be usable?
222 */
223#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) && defined(RT_ARCH_AMD64)
224# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL_EXEC
225#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) && defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
226# ifdef __PAGE_KERNEL_EXEC
227 /* >= 2.6.27 */
228# define MY_PAGE_KERNEL_EXEC __pgprot(cpu_has_pge ? __PAGE_KERNEL_EXEC | _PAGE_GLOBAL : __PAGE_KERNEL_EXEC)
229# else
230# define MY_PAGE_KERNEL_EXEC __pgprot(cpu_has_pge ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC)
231# endif
232#else
233# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL
234#endif
235
236
237/*
238 * The redhat hack section.
239 * - The current hacks are for 2.4.21-15.EL only.
240 */
241#ifndef NO_REDHAT_HACKS
242/* accounting. */
243# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
244# ifdef VM_ACCOUNT
245# define USE_RHEL4_MUNMAP
246# endif
247# endif
248
249/* backported remap_page_range. */
250# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
251# include <asm/tlb.h>
252# ifdef tlb_vma /* probably not good enough... */
253# define HAVE_26_STYLE_REMAP_PAGE_RANGE 1
254# endif
255# endif
256
257# ifndef RT_ARCH_AMD64
258/* In 2.6.9-22.ELsmp we have to call change_page_attr() twice when changing
259 * the page attributes from PAGE_KERNEL to something else, because there appears
260 * to be a bug in one of the many patches that redhat applied.
261 * It should be safe to do this on less buggy linux kernels too. ;-)
262 */
263# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
264 do { \
265 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) \
266 change_page_attr(pPages, cPages, prot); \
267 change_page_attr(pPages, cPages, prot); \
268 } while (0)
269# endif /* !RT_ARCH_AMD64 */
270#endif /* !NO_REDHAT_HACKS */
271
272#ifndef MY_CHANGE_PAGE_ATTR
273# ifdef RT_ARCH_AMD64 /** @todo This is a cheap hack, but it'll get around that 'else BUG();' in __change_page_attr(). */
274# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
275 do { \
276 change_page_attr(pPages, cPages, PAGE_KERNEL_NOCACHE); \
277 change_page_attr(pPages, cPages, prot); \
278 } while (0)
279# else
280# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) change_page_attr(pPages, cPages, prot)
281# endif
282#endif
283
284#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
285# define MY_SET_PAGES_EXEC(pPages, cPages) set_pages_x(pPages, cPages)
286# define MY_SET_PAGES_NOEXEC(pPages, cPages) set_pages_nx(pPages, cPages)
287#else
288# define MY_SET_PAGES_EXEC(pPages, cPages) \
289 do { \
290 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL)) \
291 MY_CHANGE_PAGE_ATTR(pPages, cPages, MY_PAGE_KERNEL_EXEC); \
292 } while (0)
293# define MY_SET_PAGES_NOEXEC(pPages, cPages) \
294 do { \
295 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL)) \
296 MY_CHANGE_PAGE_ATTR(pPages, cPages, PAGE_KERNEL); \
297 } while (0)
298#endif
299
300/** @def ONE_MSEC_IN_JIFFIES
301 * The number of jiffies that make up 1 millisecond. Must be at least 1! */
302#if HZ <= 1000
303# define ONE_MSEC_IN_JIFFIES 1
304#elif !(HZ % 1000)
305# define ONE_MSEC_IN_JIFFIES (HZ / 1000)
306#else
307# define ONE_MSEC_IN_JIFFIES ((HZ + 999) / 1000)
308# error "HZ is not a multiple of 1000, the GIP stuff won't work right!"
309#endif
310
311/*
312 * Stop using the linux bool type.
313 */
314#undef bool
315
316/*
317 * There are post-2.6.24 kernels (confusingly with unchanged version number)
318 * which eliminate macros which were marked as deprecated.
319 */
320#ifndef __attribute_used__
321#define __attribute_used__ __used
322#endif
323
324/**
325 * Hack for shortening pointers on linux so we can stuff more stuff into the
326 * task_struct::comm field. This is used by the semaphore code but put here
327 * because we don't have any better place atm. Don't use outside IPRT, please.
328 */
329#ifdef RT_ARCH_AMD64
330# define IPRT_DEBUG_SEMS_ADDRESS(addr) ( ((long)(addr) & (long)~UINT64_C(0xfffffff000000000)) )
331#else
332# define IPRT_DEBUG_SEMS_ADDRESS(addr) ( (long)(addr) )
333#endif
334
335/**
336 * Puts semaphore info into the task_struct::comm field if IPRT_DEBUG_SEMS is
337 * defined.
338 */
339#ifdef IPRT_DEBUG_SEMS
340# define IPRT_DEBUG_SEMS_STATE(pThis, chState) \
341 snprintf(current->comm, sizeof(current->comm), "%c%lx", (chState), IPRT_DEBUG_SEMS_ADDRESS(pThis));
342#else
343# define IPRT_DEBUG_SEMS_STATE(pThis, chState) do { } while (0)
344#endif
345
346/**
347 * Puts semaphore info into the task_struct::comm field if IPRT_DEBUG_SEMS is
348 * defined.
349 */
350#ifdef IPRT_DEBUG_SEMS
351# define IPRT_DEBUG_SEMS_STATE_RC(pThis, chState, rc) \
352 snprintf(current->comm, sizeof(current->comm), "%c%lx:%d", (chState), IPRT_DEBUG_SEMS_ADDRESS(pThis), rc);
353#else
354# define IPRT_DEBUG_SEMS_STATE_RC(pThis, chState, rc) do { } while (0)
355#endif
356
357/*
358 * There are some conflicting defines in iprt/param.h, sort them out here.
359 */
360#ifndef ___iprt_param_h
361# undef PAGE_SIZE
362# undef PAGE_OFFSET_MASK
363# include <iprt/param.h>
364#endif
365
366/*
367 * Some global indicator macros.
368 */
369/** @def IPRT_LINUX_HAS_HRTIMER
370 * Whether the kernel support high resolution timers (Linux kernel versions
371 * 2.6.28 and later (hrtimer_add_expires_ns() & schedule_hrtimeout). */
372#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
373# define IPRT_LINUX_HAS_HRTIMER
374#endif
375
376/*
377 * Workqueue stuff, see initterm-r0drv-linux.c.
378 */
379#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 41)
380typedef struct work_struct RTR0LNXWORKQUEUEITEM;
381#else
382typedef struct tq_struct RTR0LNXWORKQUEUEITEM;
383#endif
384DECLHIDDEN(void) rtR0LnxWorkqueuePush(RTR0LNXWORKQUEUEITEM *pWork, void (*pfnWorker)(RTR0LNXWORKQUEUEITEM *));
385DECLHIDDEN(void) rtR0LnxWorkqueueFlush(void);
386
387
388#endif
Note: See TracBrowser for help on using the repository browser.

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