VirtualBox

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

Last change on this file since 65990 was 65990, checked in by vboxsync, 8 years ago

Runtime/r0drv/linux: Linux 4.11 compile fixes

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

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