VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/mp-r0drv-darwin.cpp@ 24181

Last change on this file since 24181 was 22052, checked in by vboxsync, 15 years ago

IPRT: RT_MORE_STRICT for r0rdv and r0drv/darwin.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette