1 | /* $Id: initterm-r0drv-darwin.cpp 109168 2025-05-06 09:15:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Initialization & Termination, R0 Driver, Darwin.
|
---|
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.215389.xyz.
|
---|
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 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "the-darwin-kernel.h"
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/dbg.h>
|
---|
47 | #include "internal/initterm.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Internal Functions *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | #if defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
|
---|
54 | static int rtR0DarwinFallbackCpuNumber(void);
|
---|
55 | #endif
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | /*********************************************************************************************************************************
|
---|
60 | * Global Variables *
|
---|
61 | *********************************************************************************************************************************/
|
---|
62 | /** Pointer to the lock group used by IPRT. */
|
---|
63 | DECL_HIDDEN_DATA(lck_grp_t *) g_pDarwinLockGroup = NULL;
|
---|
64 | /** Pointer to the ast_pending function, if found. */
|
---|
65 | DECL_HIDDEN_DATA(PFNR0DARWINASTPENDING) g_pfnR0DarwinAstPending = NULL;
|
---|
66 | /** Pointer to the cpu_interrupt function, if found. */
|
---|
67 | DECL_HIDDEN_DATA(PFNR0DARWINCPUINTERRUPT) g_pfnR0DarwinCpuInterrupt = NULL;
|
---|
68 | #ifdef DEBUG
|
---|
69 | /** Pointer to the vm_fault_external function - used once for debugging @bugref{9466}. */
|
---|
70 | DECL_HIDDEN_DATA(PFNR0DARWINVMFAULTEXTERNAL) g_pfnR0DarwinVmFaultExternal = NULL;
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | #if defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
|
---|
74 | /** Pointer to cpu_xcall (private arm API). */
|
---|
75 | DECL_HIDDEN_DATA(PFN_DARWIN_CPU_XCALL_T) g_pfnR0DarwinCpuXCall = NULL;
|
---|
76 | /** Pointer to cpu_broadcast_xcall (private arm API). */
|
---|
77 | DECL_HIDDEN_DATA(PFN_DARWIN_CPU_BROADCAST_XCALL_T) g_pfnR0DarwinCpuBroadcastXCall = NULL;
|
---|
78 | /** Pointer to cpu_number (private API on arm) - can be NULL. */
|
---|
79 | DECL_HIDDEN_DATA(PFN_DARWIN_CPU_NUMBER_T) g_pfnR0DarwinCpuNumberMayBeNull = NULL;
|
---|
80 | /** Pointer to cpu_number (private API on arm) - never NULL. */
|
---|
81 | DECL_HIDDEN_DATA(PFN_DARWIN_CPU_NUMBER_T) g_pfnR0DarwinCpuNumber = rtR0DarwinFallbackCpuNumber;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Fallback for g_pfnR0DarwinCpuNumber, just so we won't crash if it isn't found.
|
---|
85 | */
|
---|
86 | static int rtR0DarwinFallbackCpuNumber(void)
|
---|
87 | {
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 | #endif /* RT_ARCH_ARM64 || RT_ARCH_ARM32 */
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | DECLHIDDEN(int) rtR0InitNative(void)
|
---|
95 | {
|
---|
96 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Create the lock group.
|
---|
100 | */
|
---|
101 | g_pDarwinLockGroup = lck_grp_alloc_init("IPRT", LCK_GRP_ATTR_NULL);
|
---|
102 | AssertReturn(g_pDarwinLockGroup, VERR_NO_MEMORY);
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Initialize the preemption hacks.
|
---|
106 | */
|
---|
107 | int rc = rtThreadPreemptDarwinInit();
|
---|
108 | if (RT_SUCCESS(rc))
|
---|
109 | {
|
---|
110 | /*
|
---|
111 | * Try resolve kernel symbols we need but apple don't wish to give us.
|
---|
112 | */
|
---|
113 | RTDBGKRNLINFO hKrnlInfo;
|
---|
114 | rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0 /*fFlags*/);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | {
|
---|
117 | #define GET_FUNCTION(a_VarType, a_VarName, a_szSymbol) do { \
|
---|
118 | PFNRT pfn = RTR0DbgKrnlInfoGetFunction(hKrnlInfo, NULL, a_szSymbol); \
|
---|
119 | printf("rtR0InitNative: " a_szSymbol "=%llx\n", (unsigned long long)(uintptr_t)pfn); \
|
---|
120 | if (pfn != NULL) \
|
---|
121 | a_VarName = (a_VarType)pfn; \
|
---|
122 | } while (0)
|
---|
123 |
|
---|
124 | GET_FUNCTION(PFNR0DARWINASTPENDING, g_pfnR0DarwinAstPending, "ast_pending");
|
---|
125 | GET_FUNCTION(PFNR0DARWINCPUINTERRUPT, g_pfnR0DarwinCpuInterrupt, "cpu_interrupt");
|
---|
126 | #ifdef DEBUG
|
---|
127 | GET_FUNCTION(PFNR0DARWINVMFAULTEXTERNAL, g_pfnR0DarwinVmFaultExternal, "vm_fault_external");
|
---|
128 | #endif
|
---|
129 | #if defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
|
---|
130 | GET_FUNCTION(PFN_DARWIN_CPU_XCALL_T, g_pfnR0DarwinCpuXCall, "cpu_xcall");
|
---|
131 | GET_FUNCTION(PFN_DARWIN_CPU_BROADCAST_XCALL_T, g_pfnR0DarwinCpuBroadcastXCall, "cpu_broadcast_xcall");
|
---|
132 | GET_FUNCTION(PFN_DARWIN_CPU_NUMBER_T, g_pfnR0DarwinCpuNumberMayBeNull,"cpu_number");
|
---|
133 | g_pfnR0DarwinCpuNumber = g_pfnR0DarwinCpuNumberMayBeNull
|
---|
134 | ? g_pfnR0DarwinCpuNumberMayBeNull : rtR0DarwinFallbackCpuNumber;
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | #undef GET_FUNCTION
|
---|
138 | RTR0DbgKrnlInfoRelease(hKrnlInfo);
|
---|
139 | }
|
---|
140 | if (RT_FAILURE(rc))
|
---|
141 | {
|
---|
142 | printf("rtR0InitNative: warning! failed to resolve special kernel symbols\n");
|
---|
143 | rc = VINF_SUCCESS;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | if (RT_FAILURE(rc))
|
---|
147 | rtR0TermNative();
|
---|
148 |
|
---|
149 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | DECLHIDDEN(void) rtR0TermNative(void)
|
---|
155 | {
|
---|
156 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Preemption hacks before the lock group.
|
---|
160 | */
|
---|
161 | rtThreadPreemptDarwinTerm();
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Free the lock group.
|
---|
165 | */
|
---|
166 | if (g_pDarwinLockGroup)
|
---|
167 | {
|
---|
168 | lck_grp_free(g_pDarwinLockGroup);
|
---|
169 | g_pDarwinLockGroup = NULL;
|
---|
170 | }
|
---|
171 |
|
---|
172 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
173 | }
|
---|
174 |
|
---|