VirtualBox

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

Last change on this file since 37826 was 36233, checked in by vboxsync, 14 years ago

IPRT: linux R0 threads

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