1 | /* $Id: mp-r0drv-darwin.cpp 16354 2009-01-28 23:52:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-darwin-kernel.h"
|
---|
36 |
|
---|
37 | #include <iprt/mp.h>
|
---|
38 | #include <iprt/cpuset.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/asm.h>
|
---|
41 | #include "r0drv/mp-r0drv.h"
|
---|
42 |
|
---|
43 | #define MY_DARWIN_MAX_CPUS (0xf + 1) /* see MAX_CPUS */
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *******************************************************************************/
|
---|
49 | static int32_t volatile g_cMaxCpus = -1;
|
---|
50 |
|
---|
51 |
|
---|
52 | static int rtMpDarwinInitMaxCpus(void)
|
---|
53 | {
|
---|
54 | int32_t cCpus = -1;
|
---|
55 | #ifdef RT_ARCH_AMD64
|
---|
56 | size_t oldLen = sizeof(cCpus);
|
---|
57 | int rc = sysctlbyname("hw.ncpu", &cCpus, &oldLen, NULL, NULL);
|
---|
58 | if (rc)
|
---|
59 | {
|
---|
60 | printf("IPRT: sysctlbyname(hw.ncpu) failed with rc=%d!\n", rc);
|
---|
61 | cCpus = MY_DARWIN_MAX_CPUS;
|
---|
62 | }
|
---|
63 | #else
|
---|
64 | cCpus = ml_get_max_cpus();
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | ASMAtomicWriteS32(&g_cMaxCpus, cCpus);
|
---|
68 | return cCpus;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | DECLINLINE(int) rtMpDarwinMaxCpus(void)
|
---|
73 | {
|
---|
74 | int cCpus = g_cMaxCpus;
|
---|
75 | if (RT_UNLIKELY(cCpus <= 0))
|
---|
76 | return rtMpDarwinInitMaxCpus();
|
---|
77 | return cCpus;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | RTDECL(RTCPUID) RTMpCpuId(void)
|
---|
82 | {
|
---|
83 | return cpu_number();
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
|
---|
88 | {
|
---|
89 | return idCpu < MY_DARWIN_MAX_CPUS ? (int)idCpu : -1;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
|
---|
94 | {
|
---|
95 | return (unsigned)iCpu < MY_DARWIN_MAX_CPUS ? (RTCPUID)iCpu : NIL_RTCPUID;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
|
---|
100 | {
|
---|
101 | return rtMpDarwinMaxCpus() - 1;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
|
---|
106 | {
|
---|
107 | return idCpu < MY_DARWIN_MAX_CPUS
|
---|
108 | && idCpu < (RTCPUID)rtMpDarwinMaxCpus();
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
|
---|
113 | {
|
---|
114 | RTCPUID idCpu;
|
---|
115 |
|
---|
116 | RTCpuSetEmpty(pSet);
|
---|
117 | idCpu = RTMpGetMaxCpuId();
|
---|
118 | do
|
---|
119 | {
|
---|
120 | if (RTMpIsCpuPossible(idCpu))
|
---|
121 | RTCpuSetAdd(pSet, idCpu);
|
---|
122 | } while (idCpu-- > 0);
|
---|
123 | return pSet;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | RTDECL(RTCPUID) RTMpGetCount(void)
|
---|
128 | {
|
---|
129 | return rtMpDarwinMaxCpus();
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
|
---|
134 | {
|
---|
135 | /** @todo darwin R0 MP */
|
---|
136 | return RTMpGetSet(pSet);
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | RTDECL(RTCPUID) RTMpGetOnlineCount(void)
|
---|
141 | {
|
---|
142 | /** @todo darwin R0 MP */
|
---|
143 | return RTMpGetCount();
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
|
---|
148 | {
|
---|
149 | /** @todo darwin R0 MP */
|
---|
150 | return RTMpIsCpuPossible(idCpu);
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | RTDECL(PRTCPUSET) RTMpGetPresentSet(PRTCPUSET pSet)
|
---|
155 | {
|
---|
156 | return RTMpGetSet(pSet);
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | RTDECL(RTCPUID) RTMpGetPresentCount(void)
|
---|
161 | {
|
---|
162 | return RTMpGetCount();
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | RTDECL(bool) RTMpIsCpuPresent(RTCPUID idCpu)
|
---|
167 | {
|
---|
168 | return RTMpIsCpuPossible(idCpu);
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
|
---|
173 | {
|
---|
174 | /** @todo darwin R0 MP (rainy day) */
|
---|
175 | return 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
|
---|
180 | {
|
---|
181 | /** @todo darwin R0 MP (rainy day) */
|
---|
182 | return 0;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | RTDECL(bool) RTMpIsCpuWorkPending(void)
|
---|
187 | {
|
---|
188 | /** @todo (not used on non-Windows platforms yet). */
|
---|
189 | return false;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
195 | * for the RTMpOnAll API.
|
---|
196 | *
|
---|
197 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
198 | */
|
---|
199 | static void rtmpOnAllDarwinWrapper(void *pvArg)
|
---|
200 | {
|
---|
201 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
202 | pArgs->pfnWorker(cpu_number(), pArgs->pvUser1, pArgs->pvUser2);
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
207 | {
|
---|
208 | RTMPARGS Args;
|
---|
209 | Args.pfnWorker = pfnWorker;
|
---|
210 | Args.pvUser1 = pvUser1;
|
---|
211 | Args.pvUser2 = pvUser2;
|
---|
212 | Args.idCpu = NIL_RTCPUID;
|
---|
213 | Args.cHits = 0;
|
---|
214 | mp_rendezvous_no_intrs(rtmpOnAllDarwinWrapper, &Args);
|
---|
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 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
235 | {
|
---|
236 | int rc;
|
---|
237 | RTMPARGS Args;
|
---|
238 | Args.pfnWorker = pfnWorker;
|
---|
239 | Args.pvUser1 = pvUser1;
|
---|
240 | Args.pvUser2 = pvUser2;
|
---|
241 | Args.idCpu = NIL_RTCPUID;
|
---|
242 | Args.cHits = 0;
|
---|
243 | mp_rendezvous_no_intrs(rtmpOnOthersDarwinWrapper, &Args);
|
---|
244 | return VINF_SUCCESS;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
250 | * for the RTMpOnSpecific API.
|
---|
251 | *
|
---|
252 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
253 | */
|
---|
254 | static void rtmpOnSpecificDarwinWrapper(void *pvArg)
|
---|
255 | {
|
---|
256 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
257 | RTCPUID idCpu = cpu_number();
|
---|
258 | if (pArgs->idCpu == idCpu)
|
---|
259 | {
|
---|
260 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
261 | ASMAtomicIncU32(&pArgs->cHits);
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
267 | {
|
---|
268 | int rc;
|
---|
269 | RTMPARGS Args;
|
---|
270 | Args.pfnWorker = pfnWorker;
|
---|
271 | Args.pvUser1 = pvUser1;
|
---|
272 | Args.pvUser2 = pvUser2;
|
---|
273 | Args.idCpu = idCpu;
|
---|
274 | Args.cHits = 0;
|
---|
275 | mp_rendezvous_no_intrs(rtmpOnSpecificDarwinWrapper, &Args);
|
---|
276 | return Args.cHits == 1
|
---|
277 | ? VINF_SUCCESS
|
---|
278 | : VERR_CPU_NOT_FOUND;
|
---|
279 | }
|
---|
280 |
|
---|