VirtualBox

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

Last change on this file since 76729 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

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