1 | /* $Id: mp-r0drv-darwin.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011 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) RTMpCpuIdToSetIndex(RTCPUID idCpu)
|
---|
81 | {
|
---|
82 | return idCpu < RTCPUSET_MAX_CPUS ? (int)idCpu : -1;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
|
---|
87 | {
|
---|
88 | return (unsigned)iCpu < RTCPUSET_MAX_CPUS ? (RTCPUID)iCpu : NIL_RTCPUID;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
|
---|
93 | {
|
---|
94 | return rtMpDarwinMaxCpus() - 1;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
|
---|
99 | {
|
---|
100 | return idCpu < RTCPUSET_MAX_CPUS
|
---|
101 | && idCpu < (RTCPUID)rtMpDarwinMaxCpus();
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
|
---|
106 | {
|
---|
107 | RTCPUID idCpu;
|
---|
108 |
|
---|
109 | RTCpuSetEmpty(pSet);
|
---|
110 | idCpu = RTMpGetMaxCpuId();
|
---|
111 | do
|
---|
112 | {
|
---|
113 | if (RTMpIsCpuPossible(idCpu))
|
---|
114 | RTCpuSetAdd(pSet, idCpu);
|
---|
115 | } while (idCpu-- > 0);
|
---|
116 | return pSet;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | RTDECL(RTCPUID) RTMpGetCount(void)
|
---|
121 | {
|
---|
122 | return rtMpDarwinMaxCpus();
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
|
---|
127 | {
|
---|
128 | /** @todo darwin R0 MP */
|
---|
129 | return RTMpGetSet(pSet);
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | RTDECL(RTCPUID) RTMpGetOnlineCount(void)
|
---|
134 | {
|
---|
135 | /** @todo darwin R0 MP */
|
---|
136 | return RTMpGetCount();
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
|
---|
141 | {
|
---|
142 | /** @todo darwin R0 MP */
|
---|
143 | return RTMpIsCpuPossible(idCpu);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
|
---|
148 | {
|
---|
149 | /** @todo darwin R0 MP (rainy day) */
|
---|
150 | return 0;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
|
---|
155 | {
|
---|
156 | /** @todo darwin R0 MP (rainy day) */
|
---|
157 | return 0;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | RTDECL(bool) RTMpIsCpuWorkPending(void)
|
---|
162 | {
|
---|
163 | /** @todo (not used on non-Windows platforms yet). */
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
170 | * for the RTMpOnAll API.
|
---|
171 | *
|
---|
172 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
173 | */
|
---|
174 | static void rtmpOnAllDarwinWrapper(void *pvArg)
|
---|
175 | {
|
---|
176 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
177 | pArgs->pfnWorker(cpu_number(), pArgs->pvUser1, pArgs->pvUser2);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
182 | {
|
---|
183 | RT_ASSERT_INTS_ON();
|
---|
184 |
|
---|
185 | RTMPARGS Args;
|
---|
186 | Args.pfnWorker = pfnWorker;
|
---|
187 | Args.pvUser1 = pvUser1;
|
---|
188 | Args.pvUser2 = pvUser2;
|
---|
189 | Args.idCpu = NIL_RTCPUID;
|
---|
190 | Args.cHits = 0;
|
---|
191 | mp_rendezvous_no_intrs(rtmpOnAllDarwinWrapper, &Args);
|
---|
192 | return VINF_SUCCESS;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
198 | * for the RTMpOnOthers API.
|
---|
199 | *
|
---|
200 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
201 | */
|
---|
202 | static void rtmpOnOthersDarwinWrapper(void *pvArg)
|
---|
203 | {
|
---|
204 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
205 | RTCPUID idCpu = cpu_number();
|
---|
206 | if (pArgs->idCpu != idCpu)
|
---|
207 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
212 | {
|
---|
213 | RT_ASSERT_INTS_ON();
|
---|
214 |
|
---|
215 | int rc;
|
---|
216 | RTMPARGS Args;
|
---|
217 | Args.pfnWorker = pfnWorker;
|
---|
218 | Args.pvUser1 = pvUser1;
|
---|
219 | Args.pvUser2 = pvUser2;
|
---|
220 | Args.idCpu = RTMpCpuId();
|
---|
221 | Args.cHits = 0;
|
---|
222 | mp_rendezvous_no_intrs(rtmpOnOthersDarwinWrapper, &Args);
|
---|
223 | return VINF_SUCCESS;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Wrapper between the native darwin per-cpu callback and PFNRTWORKER
|
---|
229 | * for the RTMpOnSpecific API.
|
---|
230 | *
|
---|
231 | * @param pvArg Pointer to the RTMPARGS package.
|
---|
232 | */
|
---|
233 | static void rtmpOnSpecificDarwinWrapper(void *pvArg)
|
---|
234 | {
|
---|
235 | PRTMPARGS pArgs = (PRTMPARGS)pvArg;
|
---|
236 | RTCPUID idCpu = cpu_number();
|
---|
237 | if (pArgs->idCpu == idCpu)
|
---|
238 | {
|
---|
239 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
240 | ASMAtomicIncU32(&pArgs->cHits);
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
246 | {
|
---|
247 | RT_ASSERT_INTS_ON();
|
---|
248 |
|
---|
249 | int rc;
|
---|
250 | RTMPARGS Args;
|
---|
251 | Args.pfnWorker = pfnWorker;
|
---|
252 | Args.pvUser1 = pvUser1;
|
---|
253 | Args.pvUser2 = pvUser2;
|
---|
254 | Args.idCpu = idCpu;
|
---|
255 | Args.cHits = 0;
|
---|
256 | mp_rendezvous_no_intrs(rtmpOnSpecificDarwinWrapper, &Args);
|
---|
257 | return Args.cHits == 1
|
---|
258 | ? VINF_SUCCESS
|
---|
259 | : VERR_CPU_NOT_FOUND;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | RTDECL(int) RTMpPokeCpu(RTCPUID idCpu)
|
---|
264 | {
|
---|
265 | RT_ASSERT_INTS_ON();
|
---|
266 |
|
---|
267 | if (g_pfnR0DarwinCpuInterrupt == NULL)
|
---|
268 | return VERR_NOT_SUPPORTED;
|
---|
269 | g_pfnR0DarwinCpuInterrupt(idCpu);
|
---|
270 | return VINF_SUCCESS;
|
---|
271 | }
|
---|
272 |
|
---|