VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/the-darwin-kernel.h@ 109128

Last change on this file since 109128 was 109128, checked in by vboxsync, 8 days ago

IPRT/r0drv: Build adjustments for darwin.arm64. jiraref:VBP-1653

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.2 KB
Line 
1/* $Id: the-darwin-kernel.h 109128 2025-05-01 01:31:56Z vboxsync $ */
2/** @file
3 * IPRT - Include all necessary headers for the Darwing kernel.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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_darwin_the_darwin_kernel_h
38#define IPRT_INCLUDED_SRC_r0drv_darwin_the_darwin_kernel_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43/* Problematic header(s) containing conflicts with IPRT first. (FreeBSD has fixed these ages ago.) */
44#define __STDC_CONSTANT_MACROS
45#define __STDC_LIMIT_MACROS
46#include <sys/param.h>
47#include <mach/vm_param.h>
48#undef ALIGN
49#undef MIN
50#undef MAX
51#undef PAGE_SIZE
52#undef PAGE_SHIFT
53#undef PVM
54
55
56/* Include the IPRT definitions of the conflicting #defines & typedefs. */
57#include <iprt/cdefs.h>
58#include <iprt/types.h>
59#include <iprt/param.h>
60
61
62/* After including cdefs, we can check that this really is Darwin. */
63#ifndef RT_OS_DARWIN
64# error "RT_OS_DARWIN must be defined!"
65#endif
66
67#if defined(__clang__) || RT_GNUC_PREREQ(4, 4)
68# pragma GCC diagnostic push
69#endif
70#if defined(__clang__) || RT_GNUC_PREREQ(4, 2)
71# pragma GCC diagnostic ignored "-Wc++11-extensions"
72# pragma GCC diagnostic ignored "-Wc99-extensions"
73# pragma GCC diagnostic ignored "-Wextra-semi"
74# pragma GCC diagnostic ignored "-Wzero-length-array"
75# pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
76#endif
77
78/* now we're ready for including the rest of the Darwin headers. */
79#include <kern/thread.h>
80#include <kern/clock.h>
81#include <kern/sched_prim.h>
82#include <kern/locks.h>
83#if defined(RT_ARCH_X86) && MAC_OS_X_VERSION_MIN_REQUIRED < 1060
84# include <i386/mp_events.h>
85#endif
86#include <libkern/libkern.h>
87#include <libkern/sysctl.h>
88#include <libkern/version.h>
89#include <mach/thread_act.h>
90#include <mach/vm_map.h>
91#include <mach/vm_region.h>
92#include <pexpert/pexpert.h>
93#include <sys/conf.h>
94#include <sys/errno.h>
95#include <sys/ioccom.h>
96#include <sys/malloc.h>
97#include <sys/proc.h>
98#include <sys/vnode.h>
99#include <sys/fcntl.h>
100#include <IOKit/IOTypes.h>
101#include <IOKit/IOLib.h> /* Note! Has Assert down as a function. */
102#define _OS_OSUNSERIALIZE_H /* HACK ALERT! Block importing OSUnserialized.h as it causes compilation trouble with
103 newer clang versions and the 10.15 SDK, and we really don't need it. Sample error:
104 libkern/c++/OSUnserialize.h:72:2: error: use of OSPtr outside of a return type [-Werror,-Wossharedptr-misuse] */
105#include <IOKit/IOMemoryDescriptor.h>
106#include <IOKit/IOBufferMemoryDescriptor.h>
107#include <IOKit/IOMapper.h>
108
109#if defined(__clang__) || RT_GNUC_PREREQ(4, 4)
110# pragma GCC diagnostic pop
111#endif
112
113
114/* See osfmk/kern/ast.h. */
115#ifndef AST_PREEMPT
116# define AST_PREEMPT UINT32_C(1)
117# define AST_QUANTUM UINT32_C(2)
118# define AST_URGENT UINT32_C(4)
119#endif
120
121/* This flag was added in 10.6, it seems. Should be harmless in earlier
122 releases... */
123#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1060
124# define kIOMemoryMapperNone UINT32_C(0x800)
125#endif
126
127/* This flag was added in 10.8.2, it seems. */
128#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1082
129# define kIOMemoryHostPhysicallyContiguous UINT32_C(0x00000080)
130#endif
131
132/** @name Macros for preserving EFLAGS.AC (despair / paranoid)
133 * @remarks Unlike linux, we have to restore it unconditionally on darwin.
134 * @{ */
135#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
136# include <iprt/asm-amd64-x86.h>
137# include <iprt/x86.h>
138# define IPRT_DARWIN_SAVE_EFL_AC() RTCCUINTREG const fSavedEfl = ASMGetFlags();
139# define IPRT_DARWIN_RESTORE_EFL_AC() ASMSetFlags(fSavedEfl)
140# define IPRT_DARWIN_RESTORE_EFL_ONLY_AC() ASMChangeFlags(~X86_EFL_AC, fSavedEfl & X86_EFL_AC)
141# define IPRT_DARWIN_RESTORE_EFL_ONLY_AC_EX(a_fSavedEfl) ASMChangeFlags(~X86_EFL_AC, (a_fSavedEfl) & X86_EFL_AC)
142#else
143# define IPRT_DARWIN_SAVE_EFL_AC() ((void)0)
144# define IPRT_DARWIN_RESTORE_EFL_AC() ((void)0)
145# define IPRT_DARWIN_RESTORE_EFL_ONLY_AC() ((void)0)
146# define IPRT_DARWIN_RESTORE_EFL_ONLY_AC_EX(a_fSavedEfl) ((void)0)
147#endif
148/** @} */
149
150
151RT_C_DECLS_BEGIN
152
153/* mach/vm_types.h */
154typedef struct pmap *pmap_t;
155
156/* vm/vm_kern.h */
157extern vm_map_t kernel_map;
158
159/* vm/pmap.h */
160extern pmap_t kernel_pmap;
161
162/* kern/task.h */
163extern vm_map_t get_task_map(task_t);
164
165/* osfmk/i386/pmap.h */
166extern ppnum_t pmap_find_phys(pmap_t, addr64_t);
167
168/* vm/vm_map.h */
169extern kern_return_t vm_map_wire(vm_map_t, vm_map_offset_t, vm_map_offset_t, vm_prot_t, boolean_t);
170extern kern_return_t vm_map_unwire(vm_map_t, vm_map_offset_t, vm_map_offset_t, boolean_t);
171
172/* mach/thread_act.defs / mach/i386/thread_act.h */
173extern kern_return_t thread_terminate(thread_t);
174
175#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
176/* osfmk/i386/mp.h */
177extern void mp_rendezvous(void (*)(void *), void (*)(void *), void (*)(void *), void *);
178extern void mp_rendezvous_no_intrs(void (*)(void *), void *);
179
180#elif defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
181/* mach/arm/machine_routines.h */
182extern kern_return_t cpu_xcall(int, void (*)(void *), void *);
183extern kern_return_t cpu_immediate_xcall(int, void (*)(void *), void *);
184extern unsigned int cpu_broadcast_xcall(uint32_t *, boolean_t, void (*)(void *), void *);
185extern unsigned int cpu_broadcast_xcall_simple(boolean_t, void (*)(void *), void *);
186typedef kern_return_t (*PFN_DARWIN_CPU_XCALL_T)(int, void (*)(void *), void *);
187typedef kern_return_t (*PFN_DARWIN_CPU_IMMEDIATE_XCALL_T)(int, void (*)(void *), void *);
188typedef unsigned int (*PFN_DARWIN_CPU_BROADCAST_XCALL_T)(uint32_t *, boolean_t, void (*)(void *), void *);
189typedef unsigned int (*PFN_DARWIN_CPU_BROADCAST_XCALL_SIMPLE_T)(boolean_t, void (*)(void *), void *);
190#endif
191
192#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
193/* osfmk/i386/cpu_data.h */
194struct my_cpu_data_x86
195{
196 struct my_cpu_data_x86 *cpu_this;
197 thread_t cpu_active_thread;
198 void *cpu_int_state;
199 vm_offset_t cpu_active_stack;
200 vm_offset_t cpu_kernel_stack;
201 vm_offset_t cpu_int_stack_top;
202 int cpu_preemption_level;
203 int cpu_simple_lock_count;
204 int cpu_interrupt_level;
205 int cpu_number;
206 int cpu_phys_number;
207 cpu_id_t cpu_id;
208 int cpu_signals;
209 int cpu_mcount_off;
210 /*ast_t*/uint32_t cpu_pending_ast;
211 int cpu_type;
212 int cpu_subtype;
213 int cpu_threadtype;
214 int cpu_running;
215};
216#endif /* RT_ARCH_AMD64 || RT_ARCH_X86 */
217
218/* osfmk/i386/cpu_number.h */
219extern int cpu_number(void);
220
221/* osfmk/vm/vm_user.c */
222extern kern_return_t vm_protect(vm_map_t, vm_offset_t, vm_size_t, boolean_t, vm_prot_t);
223/*extern kern_return_t vm_region(vm_map_t, vm_address_t *, vm_size_t *, vm_region_flavor_t, vm_region_info_t,
224 mach_msg_type_number_t *, mach_port_t *);*/
225
226/* i386/machine_routines.h */
227extern int ml_get_max_cpus(void);
228
229RT_C_DECLS_END
230
231
232/*
233 * Internals of the Darwin Ring-0 IPRT.
234 */
235RT_C_DECLS_BEGIN
236
237/* initterm-r0drv-darwin.cpp. */
238typedef uint32_t * (*PFNR0DARWINASTPENDING)(void);
239typedef void (*PFNR0DARWINCPUINTERRUPT)(int);
240extern DECL_HIDDEN_DATA(lck_grp_t *) g_pDarwinLockGroup;
241extern DECL_HIDDEN_DATA(PFNR0DARWINASTPENDING) g_pfnR0DarwinAstPending;
242extern DECL_HIDDEN_DATA(PFNR0DARWINCPUINTERRUPT) g_pfnR0DarwinCpuInterrupt;
243#ifdef DEBUG /* Used once for debugging memory issues (see #9466). */
244typedef kern_return_t (*PFNR0DARWINVMFAULTEXTERNAL)(vm_map_t, vm_map_offset_t, vm_prot_t, boolean_t, int, pmap_t, vm_map_offset_t);
245extern DECL_HIDDEN_DATA(PFNR0DARWINVMFAULTEXTERNAL) g_pfnR0DarwinVmFaultExternal;
246#endif
247
248#if defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
249extern DECL_HIDDEN_DATA(PFN_DARWIN_CPU_XCALL_T) g_pfnR0DarwinCpuXCall;
250extern DECL_HIDDEN_DATA(PFN_DARWIN_CPU_BROADCAST_XCALL_T) g_pfnR0DarwinCpuBroadcastXCall;
251typedef int (*PFN_DARWIN_CPU_NUMBER_T)(void); /* private API for arm */
252extern DECL_HIDDEN_DATA(PFN_DARWIN_CPU_NUMBER_T) g_pfnR0DarwinCpuNumber;
253# define cpu_number() (g_pfnR0DarwinCpuNumber())
254#endif
255
256/* threadpreempt-r0drv-darwin.cpp */
257int rtThreadPreemptDarwinInit(void);
258void rtThreadPreemptDarwinTerm(void);
259
260RT_C_DECLS_END
261
262
263/**
264 * Converts from nanoseconds to Darwin absolute time units.
265 * @returns Darwin absolute time.
266 * @param u64Nano Time interval in nanoseconds
267 */
268DECLINLINE(uint64_t) rtDarwinAbsTimeFromNano(const uint64_t u64Nano)
269{
270 uint64_t u64AbsTime;
271 nanoseconds_to_absolutetime(u64Nano, &u64AbsTime);
272 return u64AbsTime;
273}
274
275
276#include <iprt/err.h>
277
278/**
279 * Convert from mach kernel return code to IPRT status code.
280 * @todo put this where it belongs! (i.e. in a separate file and prototype in iprt/err.h)
281 */
282DECLINLINE(int) RTErrConvertFromMachKernReturn(kern_return_t rc)
283{
284 switch (rc)
285 {
286 case KERN_SUCCESS: return VINF_SUCCESS;
287 default: return VERR_GENERAL_FAILURE;
288 }
289}
290
291#endif /* !IPRT_INCLUDED_SRC_r0drv_darwin_the_darwin_kernel_h */
292
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