VirtualBox

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

Last change on this file since 32043 was 29648, checked in by vboxsync, 15 years ago

Runtime: s/TASK_COMM_LEN/sizeof(current->comm)/ (fix debug builds on older Linux kernels)

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