VirtualBox

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

Last change on this file since 85546 was 85518, checked in by vboxsync, 5 years ago

IPRT/the-linux-kernel.h: Need header for flush_tlb_all() now. bugref:9801

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