VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/threadpreempt-r0drv-darwin.cpp@ 98187

Last change on this file since 98187 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette