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