VirtualBox

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

Last change on this file since 34296 was 29255, checked in by vboxsync, 15 years ago

darwin+asm.h build fixes.

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