VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/mp-r0drv-freebsd.c@ 53401

Last change on this file since 53401 was 52618, checked in by vboxsync, 10 years ago

HostDrivers, Runtime, Devices, Additions: TSC delta measurement and other changes resulting from bumping supdrv major version. TSC delta measurement currently disabled.

  • 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-freebsd.c 52618 2014-09-05 12:07:29Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Ring-0 Driver, FreeBSD.
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-freebsd-kernel.h"
32
33#include <iprt/mp.h>
34#include <iprt/err.h>
35#include <iprt/asm.h>
36#include <iprt/cpuset.h>
37#include "r0drv/mp-r0drv.h"
38
39
40RTDECL(RTCPUID) RTMpCpuId(void)
41{
42 return curcpu;
43}
44
45
46RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
47{
48 return idCpu < RTCPUSET_MAX_CPUS && idCpu <= mp_maxid ? (int)idCpu : -1;
49}
50
51
52RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
53{
54 return (unsigned)iCpu <= mp_maxid ? (RTCPUID)iCpu : NIL_RTCPUID;
55}
56
57
58RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
59{
60 return mp_maxid;
61}
62
63
64RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
65{
66 return idCpu <= mp_maxid;
67}
68
69
70RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
71{
72 RTCPUID idCpu;
73
74 RTCpuSetEmpty(pSet);
75 idCpu = RTMpGetMaxCpuId();
76 do
77 {
78 if (RTMpIsCpuPossible(idCpu))
79 RTCpuSetAdd(pSet, idCpu);
80 } while (idCpu-- > 0);
81 return pSet;
82}
83
84
85RTDECL(RTCPUID) RTMpGetCount(void)
86{
87 return mp_maxid + 1;
88}
89
90
91RTDECL(RTCPUID) RTMpGetCoreCount(void)
92{
93 return mp_maxid + 1;
94}
95
96RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
97{
98 return idCpu <= mp_maxid
99 && !CPU_ABSENT(idCpu);
100}
101
102
103RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
104{
105 RTCPUID idCpu;
106
107 RTCpuSetEmpty(pSet);
108 idCpu = RTMpGetMaxCpuId();
109 do
110 {
111 if (RTMpIsCpuOnline(idCpu))
112 RTCpuSetAdd(pSet, idCpu);
113 } while (idCpu-- > 0);
114
115 return pSet;
116}
117
118
119RTDECL(RTCPUID) RTMpGetOnlineCount(void)
120{
121 return mp_ncpus;
122}
123
124
125/**
126 * Wrapper between the native FreeBSD per-cpu callback and PFNRTWORKER
127 * for the RTMpOnAll API.
128 *
129 * @param pvArg Pointer to the RTMPARGS package.
130 */
131static void rtmpOnAllFreeBSDWrapper(void *pvArg)
132{
133 PRTMPARGS pArgs = (PRTMPARGS)pvArg;
134 pArgs->pfnWorker(curcpu, pArgs->pvUser1, pArgs->pvUser2);
135}
136
137
138RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
139{
140 RTMPARGS Args;
141 Args.pfnWorker = pfnWorker;
142 Args.pvUser1 = pvUser1;
143 Args.pvUser2 = pvUser2;
144 Args.idCpu = NIL_RTCPUID;
145 Args.cHits = 0;
146 smp_rendezvous(NULL, rtmpOnAllFreeBSDWrapper, smp_no_rendevous_barrier, &Args);
147 return VINF_SUCCESS;
148}
149
150
151/**
152 * Wrapper between the native FreeBSD per-cpu callback and PFNRTWORKER
153 * for the RTMpOnOthers API.
154 *
155 * @param pvArg Pointer to the RTMPARGS package.
156 */
157static void rtmpOnOthersFreeBSDWrapper(void *pvArg)
158{
159 PRTMPARGS pArgs = (PRTMPARGS)pvArg;
160 RTCPUID idCpu = curcpu;
161 if (pArgs->idCpu != idCpu)
162 pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
163}
164
165
166RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
167{
168 /* Will panic if no rendezvousing cpus, so check up front. */
169 if (RTMpGetOnlineCount() > 1)
170 {
171#if __FreeBSD_version >= 900000
172 cpuset_t Mask;
173#elif __FreeBSD_version >= 700000
174 cpumask_t Mask;
175#endif
176 RTMPARGS Args;
177
178 Args.pfnWorker = pfnWorker;
179 Args.pvUser1 = pvUser1;
180 Args.pvUser2 = pvUser2;
181 Args.idCpu = RTMpCpuId();
182 Args.cHits = 0;
183#if __FreeBSD_version >= 700000
184# if __FreeBSD_version >= 900000
185 Mask = all_cpus;
186 CPU_CLR(curcpu, &Mask);
187# else
188 Mask = ~(cpumask_t)curcpu;
189# endif
190 smp_rendezvous_cpus(Mask, NULL, rtmpOnOthersFreeBSDWrapper, smp_no_rendevous_barrier, &Args);
191#else
192 smp_rendezvous(NULL, rtmpOnOthersFreeBSDWrapper, NULL, &Args);
193#endif
194 }
195 return VINF_SUCCESS;
196}
197
198
199/**
200 * Wrapper between the native FreeBSD per-cpu callback and PFNRTWORKER
201 * for the RTMpOnSpecific API.
202 *
203 * @param pvArg Pointer to the RTMPARGS package.
204 */
205static void rtmpOnSpecificFreeBSDWrapper(void *pvArg)
206{
207 PRTMPARGS pArgs = (PRTMPARGS)pvArg;
208 RTCPUID idCpu = curcpu;
209 if (pArgs->idCpu == idCpu)
210 {
211 pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
212 ASMAtomicIncU32(&pArgs->cHits);
213 }
214}
215
216
217RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
218{
219#if __FreeBSD_version >= 900000
220 cpuset_t Mask;
221#elif __FreeBSD_version >= 700000
222 cpumask_t Mask;
223#endif
224 RTMPARGS Args;
225
226 /* Will panic if no rendezvousing cpus, so make sure the cpu is online. */
227 if (!RTMpIsCpuOnline(idCpu))
228 return VERR_CPU_NOT_FOUND;
229
230 Args.pfnWorker = pfnWorker;
231 Args.pvUser1 = pvUser1;
232 Args.pvUser2 = pvUser2;
233 Args.idCpu = idCpu;
234 Args.cHits = 0;
235#if __FreeBSD_version >= 700000
236# if __FreeBSD_version >= 900000
237 CPU_SETOF(idCpu, &Mask);
238# else
239 Mask = (cpumask_t)1 << idCpu;
240# endif
241 smp_rendezvous_cpus(Mask, NULL, rtmpOnSpecificFreeBSDWrapper, smp_no_rendevous_barrier, &Args);
242#else
243 smp_rendezvous(NULL, rtmpOnSpecificFreeBSDWrapper, NULL, &Args);
244#endif
245 return Args.cHits == 1
246 ? VINF_SUCCESS
247 : VERR_CPU_NOT_FOUND;
248}
249
250
251#if __FreeBSD_version >= 700000
252/**
253 * Dummy callback for RTMpPokeCpu.
254 * @param pvArg Ignored
255 */
256static void rtmpFreeBSDPokeCallback(void *pvArg)
257{
258 NOREF(pvArg);
259}
260
261
262RTDECL(int) RTMpPokeCpu(RTCPUID idCpu)
263{
264#if __FreeBSD_version >= 900000
265 cpuset_t Mask;
266#elif __FreeBSD_version >= 700000
267 cpumask_t Mask;
268#endif
269
270 /* Will panic if no rendezvousing cpus, so make sure the cpu is online. */
271 if (!RTMpIsCpuOnline(idCpu))
272 return VERR_CPU_NOT_FOUND;
273
274# if __FreeBSD_version >= 900000
275 CPU_SETOF(idCpu, &Mask);
276# else
277 Mask = (cpumask_t)1 << idCpu;
278# endif
279 smp_rendezvous_cpus(Mask, NULL, rtmpFreeBSDPokeCallback, smp_no_rendevous_barrier, NULL);
280
281 return VINF_SUCCESS;
282}
283
284#else /* < 7.0 */
285RTDECL(int) RTMpPokeCpu(RTCPUID idCpu)
286{
287 return VERR_NOT_SUPPORTED;
288}
289#endif /* < 7.0 */
290
291
292RTDECL(bool) RTMpOnAllIsConcurrentSafe(void)
293{
294 return true;
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