VirtualBox

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

Last change on this file was 100354, checked in by vboxsync, 11 months ago

Runtime/the-linux-kernel.h: Build fix for debug build kernel modules on linux.arm64, enable the x86 specific SMAP stuff only for x86/amd64 for debug builds, bugref:10457

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

© 2023 Oracle
ContactPrivacy policyTerms of Use