VirtualBox

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

Last change on this file since 70460 was 70460, checked in by vboxsync, 7 years ago

Fix Guest Additions vboxsf sendfile() caching problems (see #2548), for older linux kernels (e.g. 2.6.23 <= linux kernel < 2.6.31)

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