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