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