VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/mp-r0drv-solaris.c@ 97896

Last change on this file since 97896 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1/* $Id: mp-r0drv-solaris.c 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2008-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "the-solaris-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/mp.h>
44#include <iprt/cpuset.h>
45#include <iprt/thread.h>
46
47#include <iprt/asm.h>
48#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
49# include <iprt/asm-amd64-x86.h>
50#endif
51#include <iprt/err.h>
52#include "r0drv/mp-r0drv.h"
53
54typedef int FNRTMPSOLWORKER(void *pvUser1, void *pvUser2, void *pvUser3);
55typedef FNRTMPSOLWORKER *PFNRTMPSOLWORKER;
56
57
58RTDECL(bool) RTMpIsCpuWorkPending(void)
59{
60 return false;
61}
62
63
64RTDECL(RTCPUID) RTMpCpuId(void)
65{
66 return CPU->cpu_id;
67}
68
69
70RTDECL(int) RTMpCurSetIndex(void)
71{
72 return CPU->cpu_id;
73}
74
75
76RTDECL(int) RTMpCurSetIndexAndId(PRTCPUID pidCpu)
77{
78 return *pidCpu = CPU->cpu_id;
79}
80
81
82RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
83{
84 return idCpu < RTCPUSET_MAX_CPUS && idCpu <= max_cpuid ? idCpu : -1;
85}
86
87
88RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
89{
90 return (unsigned)iCpu <= max_cpuid ? iCpu : NIL_RTCPUID;
91}
92
93
94RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
95{
96 return max_cpuid;
97}
98
99
100RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
101{
102 /*
103 * We cannot query CPU status recursively, check cpu member from cached set.
104 */
105 if (idCpu >= ncpus)
106 return false;
107
108 return RTCpuSetIsMember(&g_rtMpSolCpuSet, idCpu);
109}
110
111
112RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
113{
114 return idCpu < ncpus;
115}
116
117
118RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
119{
120 RTCPUID idCpu;
121
122 RTCpuSetEmpty(pSet);
123 idCpu = RTMpGetMaxCpuId(); /* it's inclusive */
124 do
125 {
126 if (RTMpIsCpuPossible(idCpu))
127 RTCpuSetAdd(pSet, idCpu);
128 } while (idCpu-- > 0);
129
130 return pSet;
131}
132
133
134RTDECL(RTCPUID) RTMpGetCount(void)
135{
136 return ncpus;
137}
138
139
140RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
141{
142 /*
143 * We cannot query CPU status recursively, return the cached set.
144 */
145 *pSet = g_rtMpSolCpuSet;
146 return pSet;
147}
148
149
150RTDECL(RTCPUID) RTMpGetOnlineCount(void)
151{
152 RTCPUSET Set;
153 RTMpGetOnlineSet(&Set);
154 return RTCpuSetCount(&Set);
155}
156
157
158/**
159 * Wrapper to Solaris IPI infrastructure.
160 *
161 * @returns Solaris error code.
162 * @param pCpuSet Pointer to Solaris CPU set.
163 * @param pfnSolWorker Function to execute on target CPU(s).
164 * @param pArgs Pointer to RTMPARGS to pass to @a pfnSolWorker.
165 */
166static void rtMpSolCrossCall(PRTSOLCPUSET pCpuSet, PFNRTMPSOLWORKER pfnSolWorker, PRTMPARGS pArgs)
167{
168 AssertPtrReturnVoid(pCpuSet);
169 AssertPtrReturnVoid(pfnSolWorker);
170 AssertPtrReturnVoid(pCpuSet);
171
172 if (g_frtSolOldIPI)
173 {
174 if (g_frtSolOldIPIUlong)
175 {
176 g_rtSolXcCall.u.pfnSol_xc_call_old_ulong((xc_arg_t)pArgs, /* Arg to IPI function */
177 0, /* Arg2, ignored */
178 0, /* Arg3, ignored */
179 IPRT_SOL_X_CALL_HIPRI, /* IPI priority */
180 pCpuSet->auCpus[0], /* Target CPU(s) */
181 (xc_func_t)pfnSolWorker); /* Function to execute on target(s) */
182 }
183 else
184 {
185 g_rtSolXcCall.u.pfnSol_xc_call_old((xc_arg_t)pArgs, /* Arg to IPI function */
186 0, /* Arg2, ignored */
187 0, /* Arg3, ignored */
188 IPRT_SOL_X_CALL_HIPRI, /* IPI priority */
189 *pCpuSet, /* Target CPU set */
190 (xc_func_t)pfnSolWorker); /* Function to execute on target(s) */
191 }
192 }
193 else
194 {
195 g_rtSolXcCall.u.pfnSol_xc_call((xc_arg_t)pArgs, /* Arg to IPI function */
196 0, /* Arg2 */
197 0, /* Arg3 */
198 &pCpuSet->auCpus[0], /* Target CPU set */
199 (xc_func_t)pfnSolWorker); /* Function to execute on target(s) */
200 }
201}
202
203
204/**
205 * Wrapper between the native solaris per-cpu callback and PFNRTWORKER
206 * for the RTMpOnAll API.
207 *
208 * @returns Solaris error code.
209 * @param uArgs Pointer to the RTMPARGS package.
210 * @param pvIgnored1 Ignored.
211 * @param pvIgnored2 Ignored.
212 */
213static int rtMpSolOnAllCpuWrapper(void *uArg, void *pvIgnored1, void *pvIgnored2)
214{
215 PRTMPARGS pArgs = (PRTMPARGS)(uArg);
216
217 /*
218 * Solaris CPU cross calls execute on offline CPUs too. Check our CPU cache
219 * set and ignore if it's offline.
220 */
221 if (!RTMpIsCpuOnline(RTMpCpuId()))
222 return 0;
223
224 pArgs->pfnWorker(RTMpCpuId(), pArgs->pvUser1, pArgs->pvUser2);
225
226 NOREF(pvIgnored1);
227 NOREF(pvIgnored2);
228 return 0;
229}
230
231
232RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
233{
234 RTMPARGS Args;
235 RTSOLCPUSET CpuSet;
236 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
237 RT_ASSERT_INTS_ON();
238
239 Args.pfnWorker = pfnWorker;
240 Args.pvUser1 = pvUser1;
241 Args.pvUser2 = pvUser2;
242 Args.idCpu = NIL_RTCPUID;
243 Args.cHits = 0;
244
245 for (int i = 0; i < IPRT_SOL_SET_WORDS; i++)
246 CpuSet.auCpus[i] = (ulong_t)-1L;
247
248 RTThreadPreemptDisable(&PreemptState);
249
250 rtMpSolCrossCall(&CpuSet, rtMpSolOnAllCpuWrapper, &Args);
251
252 RTThreadPreemptRestore(&PreemptState);
253
254 return VINF_SUCCESS;
255}
256
257
258/**
259 * Wrapper between the native solaris per-cpu callback and PFNRTWORKER
260 * for the RTMpOnOthers API.
261 *
262 * @returns Solaris error code.
263 * @param uArgs Pointer to the RTMPARGS package.
264 * @param pvIgnored1 Ignored.
265 * @param pvIgnored2 Ignored.
266 */
267static int rtMpSolOnOtherCpusWrapper(void *uArg, void *pvIgnored1, void *pvIgnored2)
268{
269 PRTMPARGS pArgs = (PRTMPARGS)(uArg);
270 RTCPUID idCpu = RTMpCpuId();
271
272 Assert(idCpu != pArgs->idCpu);
273 pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
274
275 NOREF(pvIgnored1);
276 NOREF(pvIgnored2);
277 return 0;
278}
279
280
281RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
282{
283 RTMPARGS Args;
284 RTSOLCPUSET CpuSet;
285 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
286 RT_ASSERT_INTS_ON();
287
288 Args.pfnWorker = pfnWorker;
289 Args.pvUser1 = pvUser1;
290 Args.pvUser2 = pvUser2;
291 Args.idCpu = RTMpCpuId();
292 Args.cHits = 0;
293
294 /* The caller is supposed to have disabled preemption, but take no chances. */
295 RTThreadPreemptDisable(&PreemptState);
296
297 for (int i = 0; i < IPRT_SOL_SET_WORDS; i++)
298 CpuSet.auCpus[0] = (ulong_t)-1L;
299 BT_CLEAR(CpuSet.auCpus, RTMpCpuId());
300
301 rtMpSolCrossCall(&CpuSet, rtMpSolOnOtherCpusWrapper, &Args);
302
303 RTThreadPreemptRestore(&PreemptState);
304
305 return VINF_SUCCESS;
306}
307
308
309
310/**
311 * Wrapper between the native solaris per-cpu callback and PFNRTWORKER
312 * for the RTMpOnPair API.
313 *
314 * @returns Solaris error code.
315 * @param uArgs Pointer to the RTMPARGS package.
316 * @param pvIgnored1 Ignored.
317 * @param pvIgnored2 Ignored.
318 */
319static int rtMpSolOnPairCpuWrapper(void *uArg, void *pvIgnored1, void *pvIgnored2)
320{
321 PRTMPARGS pArgs = (PRTMPARGS)(uArg);
322 RTCPUID idCpu = RTMpCpuId();
323
324 Assert(idCpu == pArgs->idCpu || idCpu == pArgs->idCpu2);
325 pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
326 ASMAtomicIncU32(&pArgs->cHits);
327
328 NOREF(pvIgnored1);
329 NOREF(pvIgnored2);
330 return 0;
331}
332
333
334RTDECL(int) RTMpOnPair(RTCPUID idCpu1, RTCPUID idCpu2, uint32_t fFlags, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
335{
336 int rc;
337 RTMPARGS Args;
338 RTSOLCPUSET CpuSet;
339 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
340
341 AssertReturn(idCpu1 != idCpu2, VERR_INVALID_PARAMETER);
342 AssertReturn(!(fFlags & RTMPON_F_VALID_MASK), VERR_INVALID_FLAGS);
343
344 Args.pfnWorker = pfnWorker;
345 Args.pvUser1 = pvUser1;
346 Args.pvUser2 = pvUser2;
347 Args.idCpu = idCpu1;
348 Args.idCpu2 = idCpu2;
349 Args.cHits = 0;
350
351 for (int i = 0; i < IPRT_SOL_SET_WORDS; i++)
352 CpuSet.auCpus[i] = 0;
353 BT_SET(CpuSet.auCpus, idCpu1);
354 BT_SET(CpuSet.auCpus, idCpu2);
355
356 /*
357 * Check that both CPUs are online before doing the broadcast call.
358 */
359 RTThreadPreemptDisable(&PreemptState);
360 if ( RTMpIsCpuOnline(idCpu1)
361 && RTMpIsCpuOnline(idCpu2))
362 {
363 rtMpSolCrossCall(&CpuSet, rtMpSolOnPairCpuWrapper, &Args);
364
365 Assert(Args.cHits <= 2);
366 if (Args.cHits == 2)
367 rc = VINF_SUCCESS;
368 else if (Args.cHits == 1)
369 rc = VERR_NOT_ALL_CPUS_SHOWED;
370 else if (Args.cHits == 0)
371 rc = VERR_CPU_OFFLINE;
372 else
373 rc = VERR_CPU_IPE_1;
374 }
375 /*
376 * A CPU must be present to be considered just offline.
377 */
378 else if ( RTMpIsCpuPresent(idCpu1)
379 && RTMpIsCpuPresent(idCpu2))
380 rc = VERR_CPU_OFFLINE;
381 else
382 rc = VERR_CPU_NOT_FOUND;
383
384 RTThreadPreemptRestore(&PreemptState);
385 return rc;
386}
387
388
389RTDECL(bool) RTMpOnPairIsConcurrentExecSupported(void)
390{
391 return true;
392}
393
394
395/**
396 * Wrapper between the native solaris per-cpu callback and PFNRTWORKER
397 * for the RTMpOnSpecific API.
398 *
399 * @returns Solaris error code.
400 * @param uArgs Pointer to the RTMPARGS package.
401 * @param pvIgnored1 Ignored.
402 * @param pvIgnored2 Ignored.
403 */
404static int rtMpSolOnSpecificCpuWrapper(void *uArg, void *pvIgnored1, void *pvIgnored2)
405{
406 PRTMPARGS pArgs = (PRTMPARGS)(uArg);
407 RTCPUID idCpu = RTMpCpuId();
408
409 Assert(idCpu == pArgs->idCpu);
410 pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
411 ASMAtomicIncU32(&pArgs->cHits);
412
413 NOREF(pvIgnored1);
414 NOREF(pvIgnored2);
415 return 0;
416}
417
418
419RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
420{
421 RTMPARGS Args;
422 RTSOLCPUSET CpuSet;
423 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
424 RT_ASSERT_INTS_ON();
425
426 if (idCpu >= ncpus)
427 return VERR_CPU_NOT_FOUND;
428
429 if (RT_UNLIKELY(!RTMpIsCpuOnline(idCpu)))
430 return RTMpIsCpuPresent(idCpu) ? VERR_CPU_OFFLINE : VERR_CPU_NOT_FOUND;
431
432 Args.pfnWorker = pfnWorker;
433 Args.pvUser1 = pvUser1;
434 Args.pvUser2 = pvUser2;
435 Args.idCpu = idCpu;
436 Args.cHits = 0;
437
438 for (int i = 0; i < IPRT_SOL_SET_WORDS; i++)
439 CpuSet.auCpus[i] = 0;
440 BT_SET(CpuSet.auCpus, idCpu);
441
442 RTThreadPreemptDisable(&PreemptState);
443
444 rtMpSolCrossCall(&CpuSet, rtMpSolOnSpecificCpuWrapper, &Args);
445
446 RTThreadPreemptRestore(&PreemptState);
447
448 Assert(ASMAtomicUoReadU32(&Args.cHits) <= 1);
449
450 return ASMAtomicUoReadU32(&Args.cHits) == 1
451 ? VINF_SUCCESS
452 : VERR_CPU_NOT_FOUND;
453}
454
455
456RTDECL(bool) RTMpOnAllIsConcurrentSafe(void)
457{
458 return true;
459}
460
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