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