1 | /* $Id: mp-r0drv-darwin.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2015 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 | return 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
|
---|
171 | {
|
---|
172 | /** @todo darwin R0 MP (rainy day) */
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | RTDECL(bool) RTMpIsCpuWorkPending(void)
|
---|
178 | {
|
---|
179 | /** @todo (not used on non-Windows platforms yet). */
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
186 | * for the RTMpOnAll API.
|
---|
187 | *
|
---|
188 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
189 | */
|
---|
190 | static void rtmpOnAllDarwinWrapper(void *pvArg)
|
---|
191 | {
|
---|
192 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
193 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
194 | pArgs->pfnWorker(cpu_number(), pArgs->pvUser1, pArgs->pvUser2);
|
---|
195 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
200 | {
|
---|
201 | RT_ASSERT_INTS_ON();
|
---|
202 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
203 |
|
---|
204 | RTMPARGS Args;
|
---|
205 | Args.pfnWorker = pfnWorker;
|
---|
206 | Args.pvUser1 = pvUser1;
|
---|
207 | Args.pvUser2 = pvUser2;
|
---|
208 | Args.idCpu = NIL_RTCPUID;
|
---|
209 | Args.cHits = 0;
|
---|
210 | mp_rendezvous_no_intrs(rtmpOnAllDarwinWrapper, &Args);
|
---|
211 |
|
---|
212 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
219 | * for the RTMpOnOthers API.
|
---|
220 | *
|
---|
221 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
222 | */
|
---|
223 | static void rtmpOnOthersDarwinWrapper(void *pvArg)
|
---|
224 | {
|
---|
225 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
226 | RTCPUID idCpu = cpu_number();
|
---|
227 | if (pArgs->idCpu != idCpu)
|
---|
228 | {
|
---|
229 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
230 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
231 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
237 | {
|
---|
238 | RT_ASSERT_INTS_ON();
|
---|
239 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
240 |
|
---|
241 | RTMPARGS Args;
|
---|
242 | Args.pfnWorker = pfnWorker;
|
---|
243 | Args.pvUser1 = pvUser1;
|
---|
244 | Args.pvUser2 = pvUser2;
|
---|
245 | Args.idCpu = RTMpCpuId();
|
---|
246 | Args.cHits = 0;
|
---|
247 | mp_rendezvous_no_intrs(rtmpOnOthersDarwinWrapper, &Args);
|
---|
248 |
|
---|
249 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
250 | return VINF_SUCCESS;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
256 | * for the RTMpOnSpecific API.
|
---|
257 | *
|
---|
258 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
259 | */
|
---|
260 | static void rtmpOnSpecificDarwinWrapper(void *pvArg)
|
---|
261 | {
|
---|
262 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
263 | RTCPUID idCpu = cpu_number();
|
---|
264 | if (pArgs->idCpu == idCpu)
|
---|
265 | {
|
---|
266 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
267 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
268 | ASMAtomicIncU32(&pArgs->cHits);
|
---|
269 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
275 | {
|
---|
276 | RT_ASSERT_INTS_ON();
|
---|
277 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
278 |
|
---|
279 | RTMPARGS Args;
|
---|
280 | Args.pfnWorker = pfnWorker;
|
---|
281 | Args.pvUser1 = pvUser1;
|
---|
282 | Args.pvUser2 = pvUser2;
|
---|
283 | Args.idCpu = idCpu;
|
---|
284 | Args.cHits = 0;
|
---|
285 | mp_rendezvous_no_intrs(rtmpOnSpecificDarwinWrapper, &Args);
|
---|
286 |
|
---|
287 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
288 | return Args.cHits == 1
|
---|
289 | ? VINF_SUCCESS
|
---|
290 | : VERR_CPU_NOT_FOUND;
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | RTDECL(int) RTMpPokeCpu(RTCPUID idCpu)
|
---|
295 | {
|
---|
296 | RT_ASSERT_INTS_ON();
|
---|
297 |
|
---|
298 | if (g_pfnR0DarwinCpuInterrupt == NULL)
|
---|
299 | return VERR_NOT_SUPPORTED;
|
---|
300 | IPRT_DARWIN_SAVE_EFL_AC(); /* paranoia */
|
---|
301 | g_pfnR0DarwinCpuInterrupt(idCpu);
|
---|
302 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
303 | return VINF_SUCCESS;
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | RTDECL(bool) RTMpOnAllIsConcurrentSafe(void)
|
---|
308 | {
|
---|
309 | return true;
|
---|
310 | }
|
---|
311 |
|
---|