1 | /* $Id: threadpreempt-r0drv-darwin.cpp 109126 2025-05-01 00:29:11Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Thread Preemption, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 | #include <iprt/thread.h>
|
---|
44 |
|
---|
45 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
46 | # include <iprt/asm-amd64-x86.h>
|
---|
47 | #elif defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
|
---|
48 | # include <iprt/asm-arm.h>
|
---|
49 | #endif
|
---|
50 | #include <iprt/assert.h>
|
---|
51 | #include <iprt/cpuset.h>
|
---|
52 | #include <iprt/errcore.h>
|
---|
53 | #include <iprt/mp.h>
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Structures and Typedefs *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | typedef struct RTDARWINPREEMPTHACK
|
---|
60 | {
|
---|
61 | /** The spinlock we exploit for disabling preemption. */
|
---|
62 | lck_spin_t *pSpinLock;
|
---|
63 | /** The preemption count for this CPU, to guard against nested calls. */
|
---|
64 | uint32_t cRecursion;
|
---|
65 | } RTDARWINPREEMPTHACK;
|
---|
66 | typedef RTDARWINPREEMPTHACK *PRTDARWINPREEMPTHACK;
|
---|
67 |
|
---|
68 |
|
---|
69 | /*********************************************************************************************************************************
|
---|
70 | * Global Variables *
|
---|
71 | *********************************************************************************************************************************/
|
---|
72 | static RTDARWINPREEMPTHACK g_aPreemptHacks[RTCPUSET_MAX_CPUS];
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Allocates the per-cpu spin locks used to disable preemption.
|
---|
77 | *
|
---|
78 | * Called by rtR0InitNative.
|
---|
79 | */
|
---|
80 | int rtThreadPreemptDarwinInit(void)
|
---|
81 | {
|
---|
82 | Assert(g_pDarwinLockGroup);
|
---|
83 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
84 |
|
---|
85 | for (size_t i = 0; i < RT_ELEMENTS(g_aPreemptHacks); i++)
|
---|
86 | {
|
---|
87 | g_aPreemptHacks[i].pSpinLock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
|
---|
88 | if (!g_aPreemptHacks[i].pSpinLock)
|
---|
89 | return VERR_NO_MEMORY; /* (The caller will invoke rtThreadPreemptDarwinTerm) */
|
---|
90 | }
|
---|
91 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Frees the per-cpu spin locks used to disable preemption.
|
---|
98 | *
|
---|
99 | * Called by rtR0TermNative.
|
---|
100 | */
|
---|
101 | void rtThreadPreemptDarwinTerm(void)
|
---|
102 | {
|
---|
103 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
104 |
|
---|
105 | for (size_t i = 0; i < RT_ELEMENTS(g_aPreemptHacks); i++)
|
---|
106 | if (g_aPreemptHacks[i].pSpinLock)
|
---|
107 | {
|
---|
108 | lck_spin_free(g_aPreemptHacks[i].pSpinLock, g_pDarwinLockGroup);
|
---|
109 | g_aPreemptHacks[i].pSpinLock = NULL;
|
---|
110 | }
|
---|
111 |
|
---|
112 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
117 | {
|
---|
118 | RT_NOREF(hThread);
|
---|
119 | Assert(hThread == NIL_RTTHREAD);
|
---|
120 | return preemption_enabled();
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
125 | {
|
---|
126 | RT_NOREF(hThread);
|
---|
127 | if (!g_pfnR0DarwinAstPending)
|
---|
128 | return false;
|
---|
129 | uint32_t volatile *pfAstPending = g_pfnR0DarwinAstPending(); AssertPtr(pfAstPending);
|
---|
130 | uint32_t const fAstPending = *pfAstPending;
|
---|
131 |
|
---|
132 | AssertMsg(!(fAstPending & UINT32_C(0xfffe0000)), ("%#x\n", fAstPending));
|
---|
133 | return (fAstPending & (AST_PREEMPT | AST_QUANTUM | AST_URGENT)) != 0;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
138 | {
|
---|
139 | /* yes, we think that RTThreadPreemptIsPending is reliable... */
|
---|
140 | return g_pfnR0DarwinAstPending != NULL;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
145 | {
|
---|
146 | /* yes, kernel preemption is possible. */
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
152 | {
|
---|
153 | AssertPtr(pState);
|
---|
154 | Assert(pState->u32Reserved == 0);
|
---|
155 | pState->u32Reserved = 42;
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Disable to prevent preemption while we grab the per-cpu spin lock.
|
---|
159 | * Note! Only take the lock on the first call or we end up spinning for ever.
|
---|
160 | */
|
---|
161 | RTCCUINTREG fSavedFlags = ASMIntDisableFlags();
|
---|
162 | RTCPUID idCpu = RTMpCpuId();
|
---|
163 | if (RT_UNLIKELY(idCpu < RT_ELEMENTS(g_aPreemptHacks)))
|
---|
164 | {
|
---|
165 | Assert(g_aPreemptHacks[idCpu].cRecursion < UINT32_MAX / 2);
|
---|
166 | if (++g_aPreemptHacks[idCpu].cRecursion == 1)
|
---|
167 | {
|
---|
168 | lck_spin_t *pSpinLock = g_aPreemptHacks[idCpu].pSpinLock;
|
---|
169 | if (pSpinLock)
|
---|
170 | lck_spin_lock(pSpinLock);
|
---|
171 | else
|
---|
172 | AssertFailed();
|
---|
173 | }
|
---|
174 | }
|
---|
175 | ASMSetFlags(fSavedFlags);
|
---|
176 | Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
|
---|
177 | RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
182 | {
|
---|
183 | AssertPtr(pState);
|
---|
184 | Assert(pState->u32Reserved == 42);
|
---|
185 | pState->u32Reserved = 0;
|
---|
186 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
187 |
|
---|
188 | RTCPUID idCpu = RTMpCpuId();
|
---|
189 | if (RT_UNLIKELY(idCpu < RT_ELEMENTS(g_aPreemptHacks)))
|
---|
190 | {
|
---|
191 | Assert(g_aPreemptHacks[idCpu].cRecursion > 0);
|
---|
192 | if (--g_aPreemptHacks[idCpu].cRecursion == 0)
|
---|
193 | {
|
---|
194 | lck_spin_t *pSpinLock = g_aPreemptHacks[idCpu].pSpinLock;
|
---|
195 | if (pSpinLock)
|
---|
196 | {
|
---|
197 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
198 | lck_spin_unlock(pSpinLock);
|
---|
199 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
200 | }
|
---|
201 | else
|
---|
202 | AssertFailed();
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
209 | {
|
---|
210 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
211 | #if 1
|
---|
212 | return ml_at_interrupt_context() != FALSE; /* exported since xnu-517.3.7; first intel version was xnu-792.x.y. */
|
---|
213 | #else
|
---|
214 | return !ASMIntAreEnabled();
|
---|
215 | #endif
|
---|
216 | }
|
---|
217 |
|
---|