VirtualBox

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

Last change on this file since 54934 was 54415, checked in by vboxsync, 10 years ago

IPRT/r0drv/mp: Added RTMpCurSetIndex and RTMpCurSetIndexAndId.

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