1 | /* $Id: the-darwin-kernel.h 85124 2020-07-08 21:13:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Include all necessary headers for the Darwing 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_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 | #include <IOKit/IOMemoryDescriptor.h>
|
---|
93 | #include <IOKit/IOBufferMemoryDescriptor.h>
|
---|
94 | #include <IOKit/IOMapper.h>
|
---|
95 |
|
---|
96 | #if defined(__clang__) || RT_GNUC_PREREQ(4, 4)
|
---|
97 | # pragma GCC diagnostic pop
|
---|
98 | #endif
|
---|
99 |
|
---|
100 |
|
---|
101 | /* See osfmk/kern/ast.h. */
|
---|
102 | #ifndef AST_PREEMPT
|
---|
103 | # define AST_PREEMPT UINT32_C(1)
|
---|
104 | # define AST_QUANTUM UINT32_C(2)
|
---|
105 | # define AST_URGENT UINT32_C(4)
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | /* This flag was added in 10.6, it seems. Should be harmless in earlier
|
---|
109 | releases... */
|
---|
110 | #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1060
|
---|
111 | # define kIOMemoryMapperNone UINT32_C(0x800)
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | /* This flag was added in 10.8.2, it seems. */
|
---|
115 | #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1082
|
---|
116 | # define kIOMemoryHostPhysicallyContiguous UINT32_C(0x00000080)
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | /** @name Macros for preserving EFLAGS.AC (despair / paranoid)
|
---|
120 | * @remarks Unlike linux, we have to restore it unconditionally on darwin.
|
---|
121 | * @{ */
|
---|
122 | #include <iprt/asm-amd64-x86.h>
|
---|
123 | #include <iprt/x86.h>
|
---|
124 | #define IPRT_DARWIN_SAVE_EFL_AC() RTCCUINTREG const fSavedEfl = ASMGetFlags();
|
---|
125 | #define IPRT_DARWIN_RESTORE_EFL_AC() ASMSetFlags(fSavedEfl)
|
---|
126 | #define IPRT_DARWIN_RESTORE_EFL_ONLY_AC() ASMChangeFlags(~X86_EFL_AC, fSavedEfl & X86_EFL_AC)
|
---|
127 | #define IPRT_DARWIN_RESTORE_EFL_ONLY_AC_EX(a_fSavedEfl) ASMChangeFlags(~X86_EFL_AC, (a_fSavedEfl) & X86_EFL_AC)
|
---|
128 | /** @} */
|
---|
129 |
|
---|
130 |
|
---|
131 | RT_C_DECLS_BEGIN
|
---|
132 |
|
---|
133 | /* mach/vm_types.h */
|
---|
134 | typedef struct pmap *pmap_t;
|
---|
135 |
|
---|
136 | /* vm/vm_kern.h */
|
---|
137 | extern vm_map_t kernel_map;
|
---|
138 |
|
---|
139 | /* vm/pmap.h */
|
---|
140 | extern pmap_t kernel_pmap;
|
---|
141 |
|
---|
142 | /* kern/task.h */
|
---|
143 | extern vm_map_t get_task_map(task_t);
|
---|
144 |
|
---|
145 | /* osfmk/i386/pmap.h */
|
---|
146 | extern ppnum_t pmap_find_phys(pmap_t, addr64_t);
|
---|
147 |
|
---|
148 | /* vm/vm_map.h */
|
---|
149 | extern kern_return_t vm_map_wire(vm_map_t, vm_map_offset_t, vm_map_offset_t, vm_prot_t, boolean_t);
|
---|
150 | extern kern_return_t vm_map_unwire(vm_map_t, vm_map_offset_t, vm_map_offset_t, boolean_t);
|
---|
151 |
|
---|
152 | /* mach/i386/thread_act.h */
|
---|
153 | extern kern_return_t thread_terminate(thread_t);
|
---|
154 |
|
---|
155 | /* osfmk/i386/mp.h */
|
---|
156 | extern void mp_rendezvous(void (*)(void *), void (*)(void *), void (*)(void *), void *);
|
---|
157 | extern void mp_rendezvous_no_intrs(void (*)(void *), void *);
|
---|
158 |
|
---|
159 | /* osfmk/i386/cpu_data.h */
|
---|
160 | struct my_cpu_data_x86
|
---|
161 | {
|
---|
162 | struct my_cpu_data_x86 *cpu_this;
|
---|
163 | thread_t cpu_active_thread;
|
---|
164 | void *cpu_int_state;
|
---|
165 | vm_offset_t cpu_active_stack;
|
---|
166 | vm_offset_t cpu_kernel_stack;
|
---|
167 | vm_offset_t cpu_int_stack_top;
|
---|
168 | int cpu_preemption_level;
|
---|
169 | int cpu_simple_lock_count;
|
---|
170 | int cpu_interrupt_level;
|
---|
171 | int cpu_number;
|
---|
172 | int cpu_phys_number;
|
---|
173 | cpu_id_t cpu_id;
|
---|
174 | int cpu_signals;
|
---|
175 | int cpu_mcount_off;
|
---|
176 | /*ast_t*/uint32_t cpu_pending_ast;
|
---|
177 | int cpu_type;
|
---|
178 | int cpu_subtype;
|
---|
179 | int cpu_threadtype;
|
---|
180 | int cpu_running;
|
---|
181 | };
|
---|
182 |
|
---|
183 | /* osfmk/i386/cpu_number.h */
|
---|
184 | extern int cpu_number(void);
|
---|
185 |
|
---|
186 | /* osfmk/vm/vm_user.c */
|
---|
187 | extern kern_return_t vm_protect(vm_map_t, vm_offset_t, vm_size_t, boolean_t, vm_prot_t);
|
---|
188 | /*extern kern_return_t vm_region(vm_map_t, vm_address_t *, vm_size_t *, vm_region_flavor_t, vm_region_info_t,
|
---|
189 | mach_msg_type_number_t *, mach_port_t *);*/
|
---|
190 |
|
---|
191 | /* i386/machine_routines.h */
|
---|
192 | extern int ml_get_max_cpus(void);
|
---|
193 |
|
---|
194 | RT_C_DECLS_END
|
---|
195 |
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Internals of the Darwin Ring-0 IPRT.
|
---|
199 | */
|
---|
200 | RT_C_DECLS_BEGIN
|
---|
201 |
|
---|
202 | /* initterm-r0drv-darwin.cpp. */
|
---|
203 | typedef uint32_t * (*PFNR0DARWINASTPENDING)(void);
|
---|
204 | typedef void (*PFNR0DARWINCPUINTERRUPT)(int);
|
---|
205 | extern DECL_HIDDEN_DATA(lck_grp_t *) g_pDarwinLockGroup;
|
---|
206 | extern DECL_HIDDEN_DATA(PFNR0DARWINASTPENDING) g_pfnR0DarwinAstPending;
|
---|
207 | extern DECL_HIDDEN_DATA(PFNR0DARWINCPUINTERRUPT) g_pfnR0DarwinCpuInterrupt;
|
---|
208 | #ifdef DEBUG /* Used once for debugging memory issues (see #9466). */
|
---|
209 | typedef kern_return_t (*PFNR0DARWINVMFAULTEXTERNAL)(vm_map_t, vm_map_offset_t, vm_prot_t, boolean_t, int, pmap_t, vm_map_offset_t);
|
---|
210 | extern DECL_HIDDEN_DATA(PFNR0DARWINVMFAULTEXTERNAL) g_pfnR0DarwinVmFaultExternal;
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | /* threadpreempt-r0drv-darwin.cpp */
|
---|
214 | int rtThreadPreemptDarwinInit(void);
|
---|
215 | void rtThreadPreemptDarwinTerm(void);
|
---|
216 |
|
---|
217 | RT_C_DECLS_END
|
---|
218 |
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Converts from nanoseconds to Darwin absolute time units.
|
---|
222 | * @returns Darwin absolute time.
|
---|
223 | * @param u64Nano Time interval in nanoseconds
|
---|
224 | */
|
---|
225 | DECLINLINE(uint64_t) rtDarwinAbsTimeFromNano(const uint64_t u64Nano)
|
---|
226 | {
|
---|
227 | uint64_t u64AbsTime;
|
---|
228 | nanoseconds_to_absolutetime(u64Nano, &u64AbsTime);
|
---|
229 | return u64AbsTime;
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | #include <iprt/err.h>
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Convert from mach kernel return code to IPRT status code.
|
---|
237 | * @todo put this where it belongs! (i.e. in a separate file and prototype in iprt/err.h)
|
---|
238 | */
|
---|
239 | DECLINLINE(int) RTErrConvertFromMachKernReturn(kern_return_t rc)
|
---|
240 | {
|
---|
241 | switch (rc)
|
---|
242 | {
|
---|
243 | case KERN_SUCCESS: return VINF_SUCCESS;
|
---|
244 | default: return VERR_GENERAL_FAILURE;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | #endif /* !IPRT_INCLUDED_SRC_r0drv_darwin_the_darwin_kernel_h */
|
---|
249 |
|
---|