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