VirtualBox

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

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

Drivers/Linux: fixes for upcoming Linux 5.1 kernel.
bugref:4567: Linux kernel driver maintenance.
Linux 5.1 moves MAP_SHARED to uapi/linux/mman.h.
Thank you Valdis Kletnieks.

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