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