VirtualBox

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

Last change on this file since 85690 was 85690, checked in by vboxsync, 4 years ago

IPRT/the-linux-kernel.h: Adding macros for shortening the kernel version checks.

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