1 | /* $Id: mp-r0drv-darwin.cpp 74638 2018-10-06 18:31:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2017 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/mp.h>
|
---|
34 |
|
---|
35 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
36 | # include <iprt/asm-amd64-x86.h>
|
---|
37 | #endif
|
---|
38 | #include <iprt/cpuset.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include "r0drv/mp-r0drv.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Global Variables *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | static int32_t volatile g_cMaxCpus = -1;
|
---|
47 |
|
---|
48 |
|
---|
49 | static int rtMpDarwinInitMaxCpus(void)
|
---|
50 | {
|
---|
51 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
52 |
|
---|
53 | int32_t cCpus = -1;
|
---|
54 | size_t oldLen = sizeof(cCpus);
|
---|
55 | int rc = sysctlbyname("hw.ncpu", &cCpus, &oldLen, NULL, NULL);
|
---|
56 | if (rc)
|
---|
57 | {
|
---|
58 | printf("IPRT: sysctlbyname(hw.ncpu) failed with rc=%d!\n", rc);
|
---|
59 | cCpus = 64; /* whatever */
|
---|
60 | }
|
---|
61 |
|
---|
62 | ASMAtomicWriteS32(&g_cMaxCpus, cCpus);
|
---|
63 |
|
---|
64 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
65 | return cCpus;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | DECLINLINE(int) rtMpDarwinMaxCpus(void)
|
---|
70 | {
|
---|
71 | int cCpus = g_cMaxCpus;
|
---|
72 | if (RT_UNLIKELY(cCpus <= 0))
|
---|
73 | return rtMpDarwinInitMaxCpus();
|
---|
74 | return cCpus;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(RTCPUID) RTMpCpuId(void)
|
---|
79 | {
|
---|
80 | return cpu_number();
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(int) RTMpCurSetIndex(void)
|
---|
85 | {
|
---|
86 | return cpu_number();
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(int) RTMpCurSetIndexAndId(PRTCPUID pidCpu)
|
---|
91 | {
|
---|
92 | return *pidCpu = cpu_number();
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
|
---|
97 | {
|
---|
98 | return idCpu < RTCPUSET_MAX_CPUS ? (int)idCpu : -1;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
|
---|
103 | {
|
---|
104 | return (unsigned)iCpu < RTCPUSET_MAX_CPUS ? (RTCPUID)iCpu : NIL_RTCPUID;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
|
---|
109 | {
|
---|
110 | return rtMpDarwinMaxCpus() - 1;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
|
---|
115 | {
|
---|
116 | return idCpu < RTCPUSET_MAX_CPUS
|
---|
117 | && idCpu < (RTCPUID)rtMpDarwinMaxCpus();
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
|
---|
122 | {
|
---|
123 | RTCPUID idCpu;
|
---|
124 |
|
---|
125 | RTCpuSetEmpty(pSet);
|
---|
126 | idCpu = RTMpGetMaxCpuId();
|
---|
127 | do
|
---|
128 | {
|
---|
129 | if (RTMpIsCpuPossible(idCpu))
|
---|
130 | RTCpuSetAdd(pSet, idCpu);
|
---|
131 | } while (idCpu-- > 0);
|
---|
132 | return pSet;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RTDECL(RTCPUID) RTMpGetCount(void)
|
---|
137 | {
|
---|
138 | return rtMpDarwinMaxCpus();
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
|
---|
143 | {
|
---|
144 | /** @todo darwin R0 MP */
|
---|
145 | return RTMpGetSet(pSet);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | RTDECL(RTCPUID) RTMpGetOnlineCount(void)
|
---|
150 | {
|
---|
151 | /** @todo darwin R0 MP */
|
---|
152 | return RTMpGetCount();
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
|
---|
157 | {
|
---|
158 | /** @todo darwin R0 MP */
|
---|
159 | return RTMpIsCpuPossible(idCpu);
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
|
---|
164 | {
|
---|
165 | /** @todo darwin R0 MP (rainy day) */
|
---|
166 | RT_NOREF(idCpu);
|
---|
167 | return 0;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
|
---|
172 | {
|
---|
173 | /** @todo darwin R0 MP (rainy day) */
|
---|
174 | RT_NOREF(idCpu);
|
---|
175 | return 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | RTDECL(bool) RTMpIsCpuWorkPending(void)
|
---|
180 | {
|
---|
181 | /** @todo (not used on non-Windows platforms yet). */
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
188 | * for the RTMpOnAll API.
|
---|
189 | *
|
---|
190 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
191 | */
|
---|
192 | static void rtmpOnAllDarwinWrapper(void *pvArg)
|
---|
193 | {
|
---|
194 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
195 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
196 | pArgs->pfnWorker(cpu_number(), pArgs->pvUser1, pArgs->pvUser2);
|
---|
197 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
202 | {
|
---|
203 | RT_ASSERT_INTS_ON();
|
---|
204 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
205 |
|
---|
206 | RTMPARGS Args;
|
---|
207 | Args.pfnWorker = pfnWorker;
|
---|
208 | Args.pvUser1 = pvUser1;
|
---|
209 | Args.pvUser2 = pvUser2;
|
---|
210 | Args.idCpu = NIL_RTCPUID;
|
---|
211 | Args.cHits = 0;
|
---|
212 | mp_rendezvous_no_intrs(rtmpOnAllDarwinWrapper, &Args);
|
---|
213 |
|
---|
214 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
215 | return VINF_SUCCESS;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
221 | * for the RTMpOnOthers API.
|
---|
222 | *
|
---|
223 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
224 | */
|
---|
225 | static void rtmpOnOthersDarwinWrapper(void *pvArg)
|
---|
226 | {
|
---|
227 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
228 | RTCPUID idCpu = cpu_number();
|
---|
229 | if (pArgs->idCpu != idCpu)
|
---|
230 | {
|
---|
231 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
232 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
233 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
239 | {
|
---|
240 | RT_ASSERT_INTS_ON();
|
---|
241 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
242 |
|
---|
243 | RTMPARGS Args;
|
---|
244 | Args.pfnWorker = pfnWorker;
|
---|
245 | Args.pvUser1 = pvUser1;
|
---|
246 | Args.pvUser2 = pvUser2;
|
---|
247 | Args.idCpu = RTMpCpuId();
|
---|
248 | Args.cHits = 0;
|
---|
249 | mp_rendezvous_no_intrs(rtmpOnOthersDarwinWrapper, &Args);
|
---|
250 |
|
---|
251 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
252 | return VINF_SUCCESS;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
258 | * for the RTMpOnSpecific API.
|
---|
259 | *
|
---|
260 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
261 | */
|
---|
262 | static void rtmpOnSpecificDarwinWrapper(void *pvArg)
|
---|
263 | {
|
---|
264 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
265 | RTCPUID idCpu = cpu_number();
|
---|
266 | if (pArgs->idCpu == idCpu)
|
---|
267 | {
|
---|
268 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
269 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
270 | ASMAtomicIncU32(&pArgs->cHits);
|
---|
271 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
277 | {
|
---|
278 | RT_ASSERT_INTS_ON();
|
---|
279 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
280 |
|
---|
281 | RTMPARGS Args;
|
---|
282 | Args.pfnWorker = pfnWorker;
|
---|
283 | Args.pvUser1 = pvUser1;
|
---|
284 | Args.pvUser2 = pvUser2;
|
---|
285 | Args.idCpu = idCpu;
|
---|
286 | Args.cHits = 0;
|
---|
287 | mp_rendezvous_no_intrs(rtmpOnSpecificDarwinWrapper, &Args);
|
---|
288 |
|
---|
289 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
290 | return Args.cHits == 1
|
---|
291 | ? VINF_SUCCESS
|
---|
292 | : VERR_CPU_NOT_FOUND;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | RTDECL(int) RTMpPokeCpu(RTCPUID idCpu)
|
---|
297 | {
|
---|
298 | RT_ASSERT_INTS_ON();
|
---|
299 |
|
---|
300 | if (g_pfnR0DarwinCpuInterrupt == NULL)
|
---|
301 | return VERR_NOT_SUPPORTED;
|
---|
302 | IPRT_DARWIN_SAVE_EFL_AC(); /* paranoia */
|
---|
303 | /// @todo use mp_cpus_kick() when available (since 10.10)? It's probably slower (locks, mask iteration, checks), though...
|
---|
304 | g_pfnR0DarwinCpuInterrupt(idCpu);
|
---|
305 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
306 | return VINF_SUCCESS;
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | RTDECL(bool) RTMpOnAllIsConcurrentSafe(void)
|
---|
311 | {
|
---|
312 | return true;
|
---|
313 | }
|
---|
314 |
|
---|