VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/mp-r0drv-nt.cpp@ 63558

Last change on this file since 63558 was 63063, checked in by vboxsync, 9 years ago

IPRT: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.5 KB
Line 
1/* $Id: mp-r0drv-nt.cpp 63063 2016-08-05 21:14:33Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2008-2016 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-nt-kernel.h"
32
33#include <iprt/mp.h>
34#include <iprt/cpuset.h>
35#include <iprt/err.h>
36#include <iprt/asm.h>
37#include <iprt/log.h>
38#include <iprt/time.h>
39#include "r0drv/mp-r0drv.h"
40#include "internal-r0drv-nt.h"
41
42
43/*********************************************************************************************************************************
44* Structures and Typedefs *
45*********************************************************************************************************************************/
46typedef enum
47{
48 RT_NT_CPUID_SPECIFIC,
49 RT_NT_CPUID_PAIR,
50 RT_NT_CPUID_OTHERS,
51 RT_NT_CPUID_ALL
52} RT_NT_CPUID;
53
54
55/**
56 * Used by the RTMpOnSpecific.
57 */
58typedef struct RTMPNTONSPECIFICARGS
59{
60 /** Set if we're executing. */
61 bool volatile fExecuting;
62 /** Set when done executing. */
63 bool volatile fDone;
64 /** Number of references to this heap block. */
65 uint32_t volatile cRefs;
66 /** Event that the calling thread is waiting on. */
67 KEVENT DoneEvt;
68 /** The deferred procedure call object. */
69 KDPC Dpc;
70 /** The callback argument package. */
71 RTMPARGS CallbackArgs;
72} RTMPNTONSPECIFICARGS;
73/** Pointer to an argument/state structure for RTMpOnSpecific on NT. */
74typedef RTMPNTONSPECIFICARGS *PRTMPNTONSPECIFICARGS;
75
76
77
78/* test a couple of assumption. */
79AssertCompile(MAXIMUM_PROCESSORS <= RTCPUSET_MAX_CPUS);
80AssertCompile(NIL_RTCPUID >= MAXIMUM_PROCESSORS);
81
82/** @todo
83 * We cannot do other than assume a 1:1 relationship between the
84 * affinity mask and the process despite the vagueness/warnings in
85 * the docs. If someone knows a better way to get this done, please
86 * let bird know.
87 */
88
89
90RTDECL(RTCPUID) RTMpCpuId(void)
91{
92 /* WDK upgrade warning: PCR->Number changed from BYTE to WORD. */
93 return KeGetCurrentProcessorNumber();
94}
95
96
97RTDECL(int) RTMpCurSetIndex(void)
98{
99 /* WDK upgrade warning: PCR->Number changed from BYTE to WORD. */
100 return KeGetCurrentProcessorNumber();
101}
102
103
104RTDECL(int) RTMpCurSetIndexAndId(PRTCPUID pidCpu)
105{
106 return *pidCpu = KeGetCurrentProcessorNumber();
107}
108
109
110RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
111{
112 return idCpu < MAXIMUM_PROCESSORS ? (int)idCpu : -1;
113}
114
115
116RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
117{
118 return (unsigned)iCpu < MAXIMUM_PROCESSORS ? iCpu : NIL_RTCPUID;
119}
120
121
122RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
123{
124 /** @todo use KeQueryMaximumProcessorCount on vista+ */
125 return MAXIMUM_PROCESSORS - 1;
126}
127
128
129RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
130{
131 if (idCpu >= MAXIMUM_PROCESSORS)
132 return false;
133
134#if 0 /* this isn't safe at all IRQLs (great work guys) */
135 KAFFINITY Mask = KeQueryActiveProcessors();
136 return !!(Mask & RT_BIT_64(idCpu));
137#else
138 return RTCpuSetIsMember(&g_rtMpNtCpuSet, idCpu);
139#endif
140}
141
142
143RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
144{
145 /* Cannot easily distinguish between online and offline cpus. */
146 /** @todo online/present cpu stuff must be corrected for proper W2K8 support
147 * (KeQueryMaximumProcessorCount). */
148 return RTMpIsCpuOnline(idCpu);
149}
150
151
152
153RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
154{
155 /** @todo online/present cpu stuff must be corrected for proper W2K8 support
156 * (KeQueryMaximumProcessorCount). */
157 return RTMpGetOnlineSet(pSet);
158}
159
160
161RTDECL(RTCPUID) RTMpGetCount(void)
162{
163 /** @todo online/present cpu stuff must be corrected for proper W2K8 support
164 * (KeQueryMaximumProcessorCount). */
165 return RTMpGetOnlineCount();
166}
167
168
169RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
170{
171#if 0 /* this isn't safe at all IRQLs (great work guys) */
172 KAFFINITY Mask = KeQueryActiveProcessors();
173 return RTCpuSetFromU64(pSet, Mask);
174#else
175 *pSet = g_rtMpNtCpuSet;
176 return pSet;
177#endif
178}
179
180
181RTDECL(RTCPUID) RTMpGetOnlineCount(void)
182{
183 RTCPUSET Set;
184 RTMpGetOnlineSet(&Set);
185 return RTCpuSetCount(&Set);
186}
187
188
189#if 0
190/* Experiment with checking the undocumented KPRCB structure
191 * 'dt nt!_kprcb 0xaddress' shows the layout
192 */
193typedef struct
194{
195 LIST_ENTRY DpcListHead;
196 ULONG_PTR DpcLock;
197 volatile ULONG DpcQueueDepth;
198 ULONG DpcQueueCount;
199} KDPC_DATA, *PKDPC_DATA;
200
201RTDECL(bool) RTMpIsCpuWorkPending(void)
202{
203 uint8_t *pkprcb;
204 PKDPC_DATA pDpcData;
205
206 _asm {
207 mov eax, fs:0x20
208 mov pkprcb, eax
209 }
210 pDpcData = (PKDPC_DATA)(pkprcb + 0x19e0);
211 if (pDpcData->DpcQueueDepth)
212 return true;
213
214 pDpcData++;
215 if (pDpcData->DpcQueueDepth)
216 return true;
217 return false;
218}
219#else
220RTDECL(bool) RTMpIsCpuWorkPending(void)
221{
222 /** @todo not implemented */
223 return false;
224}
225#endif
226
227
228/**
229 * Wrapper between the native KIPI_BROADCAST_WORKER and IPRT's PFNRTMPWORKER for
230 * the RTMpOnAll case.
231 *
232 * @param uUserCtx The user context argument (PRTMPARGS).
233 */
234static ULONG_PTR rtmpNtOnAllBroadcastIpiWrapper(ULONG_PTR uUserCtx)
235{
236 PRTMPARGS pArgs = (PRTMPARGS)uUserCtx;
237 /*ASMAtomicIncU32(&pArgs->cHits); - not needed */
238 pArgs->pfnWorker(KeGetCurrentProcessorNumber(), pArgs->pvUser1, pArgs->pvUser2);
239 return 0;
240}
241
242
243/**
244 * Wrapper between the native KIPI_BROADCAST_WORKER and IPRT's PFNRTMPWORKER for
245 * the RTMpOnOthers case.
246 *
247 * @param uUserCtx The user context argument (PRTMPARGS).
248 */
249static ULONG_PTR rtmpNtOnOthersBroadcastIpiWrapper(ULONG_PTR uUserCtx)
250{
251 PRTMPARGS pArgs = (PRTMPARGS)uUserCtx;
252 RTCPUID idCpu = KeGetCurrentProcessorNumber();
253 if (pArgs->idCpu != idCpu)
254 {
255 /*ASMAtomicIncU32(&pArgs->cHits); - not needed */
256 pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
257 }
258 return 0;
259}
260
261
262/**
263 * Wrapper between the native KIPI_BROADCAST_WORKER and IPRT's PFNRTMPWORKER for
264 * the RTMpOnPair case.
265 *
266 * @param uUserCtx The user context argument (PRTMPARGS).
267 */
268static ULONG_PTR rtmpNtOnPairBroadcastIpiWrapper(ULONG_PTR uUserCtx)
269{
270 PRTMPARGS pArgs = (PRTMPARGS)uUserCtx;
271 RTCPUID idCpu = KeGetCurrentProcessorNumber();
272 if ( pArgs->idCpu == idCpu
273 || pArgs->idCpu2 == idCpu)
274 {
275 ASMAtomicIncU32(&pArgs->cHits);
276 pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
277 }
278 return 0;
279}
280
281
282/**
283 * Wrapper between the native KIPI_BROADCAST_WORKER and IPRT's PFNRTMPWORKER for
284 * the RTMpOnSpecific case.
285 *
286 * @param uUserCtx The user context argument (PRTMPARGS).
287 */
288static ULONG_PTR rtmpNtOnSpecificBroadcastIpiWrapper(ULONG_PTR uUserCtx)
289{
290 PRTMPARGS pArgs = (PRTMPARGS)uUserCtx;
291 RTCPUID idCpu = KeGetCurrentProcessorNumber();
292 if (pArgs->idCpu == idCpu)
293 {
294 ASMAtomicIncU32(&pArgs->cHits);
295 pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
296 }
297 return 0;
298}
299
300
301/**
302 * Internal worker for the RTMpOn* APIs using KeIpiGenericCall.
303 *
304 * @returns VINF_SUCCESS.
305 * @param pfnWorker The callback.
306 * @param pvUser1 User argument 1.
307 * @param pvUser2 User argument 2.
308 * @param pfnNativeWrapper The wrapper between the NT and IPRT callbacks.
309 * @param idCpu First CPU to match, ultimately specific to the
310 * pfnNativeWrapper used.
311 * @param idCpu2 Second CPU to match, ultimately specific to the
312 * pfnNativeWrapper used.
313 * @param pcHits Where to return the number of this. Optional.
314 */
315static int rtMpCallUsingBroadcastIpi(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2,
316 PKIPI_BROADCAST_WORKER pfnNativeWrapper, RTCPUID idCpu, RTCPUID idCpu2,
317 uint32_t *pcHits)
318{
319 RTMPARGS Args;
320 Args.pfnWorker = pfnWorker;
321 Args.pvUser1 = pvUser1;
322 Args.pvUser2 = pvUser2;
323 Args.idCpu = idCpu;
324 Args.idCpu2 = idCpu2;
325 Args.cRefs = 0;
326 Args.cHits = 0;
327
328 AssertPtr(g_pfnrtKeIpiGenericCall);
329 g_pfnrtKeIpiGenericCall(pfnNativeWrapper, (uintptr_t)&Args);
330 if (pcHits)
331 *pcHits = Args.cHits;
332 return VINF_SUCCESS;
333}
334
335
336/**
337 * Wrapper between the native nt per-cpu callbacks and PFNRTWORKER
338 *
339 * @param Dpc DPC object
340 * @param DeferredContext Context argument specified by KeInitializeDpc
341 * @param SystemArgument1 Argument specified by KeInsertQueueDpc
342 * @param SystemArgument2 Argument specified by KeInsertQueueDpc
343 */
344static VOID rtmpNtDPCWrapper(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
345{
346 PRTMPARGS pArgs = (PRTMPARGS)DeferredContext;
347 RT_NOREF3(Dpc, SystemArgument1, SystemArgument2);
348
349 ASMAtomicIncU32(&pArgs->cHits);
350 pArgs->pfnWorker(KeGetCurrentProcessorNumber(), pArgs->pvUser1, pArgs->pvUser2);
351
352 /* Dereference the argument structure. */
353 int32_t cRefs = ASMAtomicDecS32(&pArgs->cRefs);
354 Assert(cRefs >= 0);
355 if (cRefs == 0)
356 ExFreePool(pArgs);
357}
358
359
360/**
361 * Internal worker for the RTMpOn* APIs.
362 *
363 * @returns IPRT status code.
364 * @param pfnWorker The callback.
365 * @param pvUser1 User argument 1.
366 * @param pvUser2 User argument 2.
367 * @param enmCpuid What to do / is idCpu valid.
368 * @param idCpu Used if enmCpuid is RT_NT_CPUID_SPECIFIC or
369 * RT_NT_CPUID_PAIR, otherwise ignored.
370 * @param idCpu2 Used if enmCpuid is RT_NT_CPUID_PAIR, otherwise ignored.
371 * @param pcHits Where to return the number of this. Optional.
372 */
373static int rtMpCallUsingDpcs(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2,
374 RT_NT_CPUID enmCpuid, RTCPUID idCpu, RTCPUID idCpu2, uint32_t *pcHits)
375{
376#ifdef IPRT_TARGET_NT4
377 RT_NOREF(pfnWorker, pvUser1, pvUser2, enmCpuid, idCpu, idCpu2, pcHits);
378 /* g_pfnrtNt* are not present on NT anyway. */
379 return VERR_NOT_SUPPORTED;
380
381#else /* !IPRT_TARGET_NT4 */
382 PRTMPARGS pArgs;
383 KDPC *paExecCpuDpcs;
384
385# if 0
386 /* KeFlushQueuedDpcs must be run at IRQL PASSIVE_LEVEL according to MSDN, but the
387 * driver verifier doesn't complain...
388 */
389 AssertMsg(KeGetCurrentIrql() == PASSIVE_LEVEL, ("%d != %d (PASSIVE_LEVEL)\n", KeGetCurrentIrql(), PASSIVE_LEVEL));
390# endif
391
392 KAFFINITY Mask = KeQueryActiveProcessors();
393
394 /* KeFlushQueuedDpcs is not present in Windows 2000; import it dynamically so we can just fail this call. */
395 if (!g_pfnrtNtKeFlushQueuedDpcs)
396 return VERR_NOT_SUPPORTED;
397
398 pArgs = (PRTMPARGS)ExAllocatePoolWithTag(NonPagedPool, MAXIMUM_PROCESSORS*sizeof(KDPC) + sizeof(RTMPARGS), (ULONG)'RTMp');
399 if (!pArgs)
400 return VERR_NO_MEMORY;
401
402 pArgs->pfnWorker = pfnWorker;
403 pArgs->pvUser1 = pvUser1;
404 pArgs->pvUser2 = pvUser2;
405 pArgs->idCpu = NIL_RTCPUID;
406 pArgs->idCpu2 = NIL_RTCPUID;
407 pArgs->cHits = 0;
408 pArgs->cRefs = 1;
409
410 paExecCpuDpcs = (KDPC *)(pArgs + 1);
411
412 if (enmCpuid == RT_NT_CPUID_SPECIFIC)
413 {
414 KeInitializeDpc(&paExecCpuDpcs[0], rtmpNtDPCWrapper, pArgs);
415 KeSetImportanceDpc(&paExecCpuDpcs[0], HighImportance);
416 KeSetTargetProcessorDpc(&paExecCpuDpcs[0], (int)idCpu);
417 pArgs->idCpu = idCpu;
418 }
419 else if (enmCpuid == RT_NT_CPUID_PAIR)
420 {
421 KeInitializeDpc(&paExecCpuDpcs[0], rtmpNtDPCWrapper, pArgs);
422 KeSetImportanceDpc(&paExecCpuDpcs[0], HighImportance);
423 KeSetTargetProcessorDpc(&paExecCpuDpcs[0], (int)idCpu);
424 pArgs->idCpu = idCpu;
425
426 KeInitializeDpc(&paExecCpuDpcs[1], rtmpNtDPCWrapper, pArgs);
427 KeSetImportanceDpc(&paExecCpuDpcs[1], HighImportance);
428 KeSetTargetProcessorDpc(&paExecCpuDpcs[1], (int)idCpu2);
429 pArgs->idCpu2 = idCpu2;
430 }
431 else
432 {
433 for (unsigned i = 0; i < MAXIMUM_PROCESSORS; i++)
434 {
435 KeInitializeDpc(&paExecCpuDpcs[i], rtmpNtDPCWrapper, pArgs);
436 KeSetImportanceDpc(&paExecCpuDpcs[i], HighImportance);
437 KeSetTargetProcessorDpc(&paExecCpuDpcs[i], i);
438 }
439 }
440
441 /* Raise the IRQL to DISPATCH_LEVEL so we can't be rescheduled to another cpu.
442 * KeInsertQueueDpc must also be executed at IRQL >= DISPATCH_LEVEL.
443 */
444 KIRQL oldIrql;
445 KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
446
447 /*
448 * We cannot do other than assume a 1:1 relationship between the
449 * affinity mask and the process despite the warnings in the docs.
450 * If someone knows a better way to get this done, please let bird know.
451 */
452 ASMCompilerBarrier(); /* paranoia */
453 if (enmCpuid == RT_NT_CPUID_SPECIFIC)
454 {
455 ASMAtomicIncS32(&pArgs->cRefs);
456 BOOLEAN fRc = KeInsertQueueDpc(&paExecCpuDpcs[0], 0, 0);
457 Assert(fRc); NOREF(fRc);
458 }
459 else if (enmCpuid == RT_NT_CPUID_PAIR)
460 {
461 ASMAtomicIncS32(&pArgs->cRefs);
462 BOOLEAN fRc = KeInsertQueueDpc(&paExecCpuDpcs[0], 0, 0);
463 Assert(fRc); NOREF(fRc);
464
465 ASMAtomicIncS32(&pArgs->cRefs);
466 fRc = KeInsertQueueDpc(&paExecCpuDpcs[1], 0, 0);
467 Assert(fRc); NOREF(fRc);
468 }
469 else
470 {
471 unsigned iSelf = KeGetCurrentProcessorNumber();
472
473 for (unsigned i = 0; i < MAXIMUM_PROCESSORS; i++)
474 {
475 if ( (i != iSelf)
476 && (Mask & RT_BIT_64(i)))
477 {
478 ASMAtomicIncS32(&pArgs->cRefs);
479 BOOLEAN fRc = KeInsertQueueDpc(&paExecCpuDpcs[i], 0, 0);
480 Assert(fRc); NOREF(fRc);
481 }
482 }
483 if (enmCpuid != RT_NT_CPUID_OTHERS)
484 pfnWorker(iSelf, pvUser1, pvUser2);
485 }
486
487 KeLowerIrql(oldIrql);
488
489 /* Flush all DPCs and wait for completion. (can take long!) */
490 /** @todo Consider changing this to an active wait using some atomic inc/dec
491 * stuff (and check for the current cpu above in the specific case). */
492 /** @todo Seems KeFlushQueuedDpcs doesn't wait for the DPCs to be completely
493 * executed. Seen pArgs being freed while some CPU was using it before
494 * cRefs was added. */
495 g_pfnrtNtKeFlushQueuedDpcs();
496
497 if (pcHits)
498 *pcHits = pArgs->cHits;
499
500 /* Dereference the argument structure. */
501 int32_t cRefs = ASMAtomicDecS32(&pArgs->cRefs);
502 Assert(cRefs >= 0);
503 if (cRefs == 0)
504 ExFreePool(pArgs);
505
506 return VINF_SUCCESS;
507#endif /* */
508}
509
510
511RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
512{
513 if (g_pfnrtKeIpiGenericCall)
514 return rtMpCallUsingBroadcastIpi(pfnWorker, pvUser1, pvUser2, rtmpNtOnAllBroadcastIpiWrapper,
515 NIL_RTCPUID, NIL_RTCPUID, NULL);
516 return rtMpCallUsingDpcs(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_ALL, NIL_RTCPUID, NIL_RTCPUID, NULL);
517}
518
519
520RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
521{
522 if (g_pfnrtKeIpiGenericCall)
523 return rtMpCallUsingBroadcastIpi(pfnWorker, pvUser1, pvUser2, rtmpNtOnOthersBroadcastIpiWrapper,
524 NIL_RTCPUID, NIL_RTCPUID, NULL);
525 return rtMpCallUsingDpcs(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_OTHERS, NIL_RTCPUID, NIL_RTCPUID, NULL);
526}
527
528
529RTDECL(int) RTMpOnPair(RTCPUID idCpu1, RTCPUID idCpu2, uint32_t fFlags, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
530{
531 int rc;
532 AssertReturn(idCpu1 != idCpu2, VERR_INVALID_PARAMETER);
533 AssertReturn(!(fFlags & RTMPON_F_VALID_MASK), VERR_INVALID_FLAGS);
534 if ((fFlags & RTMPON_F_CONCURRENT_EXEC) && !g_pfnrtKeIpiGenericCall)
535 return VERR_NOT_SUPPORTED;
536
537 /*
538 * Check that both CPUs are online before doing the broadcast call.
539 */
540 if ( RTMpIsCpuOnline(idCpu1)
541 && RTMpIsCpuOnline(idCpu2))
542 {
543 /*
544 * The broadcast IPI isn't quite as bad as it could have been, because
545 * it looks like windows doesn't synchronize CPUs on the way out, they
546 * seems to get back to normal work while the pair is still busy.
547 */
548 uint32_t cHits = 0;
549 if (g_pfnrtKeIpiGenericCall)
550 rc = rtMpCallUsingBroadcastIpi(pfnWorker, pvUser1, pvUser2, rtmpNtOnPairBroadcastIpiWrapper, idCpu1, idCpu2, &cHits);
551 else
552 rc = rtMpCallUsingDpcs(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_PAIR, idCpu1, idCpu2, &cHits);
553 if (RT_SUCCESS(rc))
554 {
555 Assert(cHits <= 2);
556 if (cHits == 2)
557 rc = VINF_SUCCESS;
558 else if (cHits == 1)
559 rc = VERR_NOT_ALL_CPUS_SHOWED;
560 else if (cHits == 0)
561 rc = VERR_CPU_OFFLINE;
562 else
563 rc = VERR_CPU_IPE_1;
564 }
565 }
566 /*
567 * A CPU must be present to be considered just offline.
568 */
569 else if ( RTMpIsCpuPresent(idCpu1)
570 && RTMpIsCpuPresent(idCpu2))
571 rc = VERR_CPU_OFFLINE;
572 else
573 rc = VERR_CPU_NOT_FOUND;
574 return rc;
575}
576
577
578RTDECL(bool) RTMpOnPairIsConcurrentExecSupported(void)
579{
580 return g_pfnrtKeIpiGenericCall != NULL;
581}
582
583
584/**
585 * Releases a reference to a RTMPNTONSPECIFICARGS heap allocation, freeing it
586 * when the last reference is released.
587 */
588DECLINLINE(void) rtMpNtOnSpecificRelease(PRTMPNTONSPECIFICARGS pArgs)
589{
590 uint32_t cRefs = ASMAtomicDecU32(&pArgs->cRefs);
591 AssertMsg(cRefs <= 1, ("cRefs=%#x\n", cRefs));
592 if (cRefs == 0)
593 ExFreePool(pArgs);
594}
595
596
597/**
598 * Wrapper between the native nt per-cpu callbacks and PFNRTWORKER
599 *
600 * @param Dpc DPC object
601 * @param DeferredContext Context argument specified by KeInitializeDpc
602 * @param SystemArgument1 Argument specified by KeInsertQueueDpc
603 * @param SystemArgument2 Argument specified by KeInsertQueueDpc
604 */
605static VOID rtMpNtOnSpecificDpcWrapper(IN PKDPC Dpc, IN PVOID DeferredContext,
606 IN PVOID SystemArgument1, IN PVOID SystemArgument2)
607{
608 PRTMPNTONSPECIFICARGS pArgs = (PRTMPNTONSPECIFICARGS)DeferredContext;
609 RT_NOREF3(Dpc, SystemArgument1, SystemArgument2);
610
611 ASMAtomicWriteBool(&pArgs->fExecuting, true);
612
613 pArgs->CallbackArgs.pfnWorker(KeGetCurrentProcessorNumber(), pArgs->CallbackArgs.pvUser1, pArgs->CallbackArgs.pvUser2);
614
615 ASMAtomicWriteBool(&pArgs->fDone, true);
616 KeSetEvent(&pArgs->DoneEvt, 1 /*PriorityIncrement*/, FALSE /*Wait*/);
617
618 rtMpNtOnSpecificRelease(pArgs);
619}
620
621
622RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
623{
624 /*
625 * Don't try mess with an offline CPU.
626 */
627 if (!RTMpIsCpuOnline(idCpu))
628 return !RTMpIsCpuPossible(idCpu)
629 ? VERR_CPU_NOT_FOUND
630 : VERR_CPU_OFFLINE;
631
632 /*
633 * Use the broadcast IPI routine if there are no more than two CPUs online,
634 * or if the current IRQL is unsuitable for KeWaitForSingleObject.
635 */
636 int rc;
637 uint32_t cHits = 0;
638 if ( g_pfnrtKeIpiGenericCall
639 && ( RTMpGetOnlineCount() <= 2
640 || KeGetCurrentIrql() > APC_LEVEL)
641 )
642 {
643 rc = rtMpCallUsingBroadcastIpi(pfnWorker, pvUser1, pvUser2, rtmpNtOnSpecificBroadcastIpiWrapper,
644 idCpu, NIL_RTCPUID, &cHits);
645 if (RT_SUCCESS(rc))
646 {
647 if (cHits == 1)
648 return VINF_SUCCESS;
649 rc = cHits == 0 ? VERR_CPU_OFFLINE : VERR_CPU_IPE_1;
650 }
651 return rc;
652 }
653
654#if 0
655 rc = rtMpCallUsingDpcs(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_SPECIFIC, idCpu, NIL_RTCPUID, &cHits);
656 if (RT_SUCCESS(rc))
657 {
658 if (cHits == 1)
659 return VINF_SUCCESS;
660 rc = cHits == 0 ? VERR_CPU_OFFLINE : VERR_CPU_IPE_1;
661 }
662 return rc;
663
664#else
665 /*
666 * Initialize the argument package and the objects within it.
667 * The package is referenced counted to avoid unnecessary spinning to
668 * synchronize cleanup and prevent stack corruption.
669 */
670 PRTMPNTONSPECIFICARGS pArgs = (PRTMPNTONSPECIFICARGS)ExAllocatePoolWithTag(NonPagedPool, sizeof(*pArgs), (ULONG)'RTMp');
671 if (!pArgs)
672 return VERR_NO_MEMORY;
673 pArgs->cRefs = 2;
674 pArgs->fExecuting = false;
675 pArgs->fDone = false;
676 pArgs->CallbackArgs.pfnWorker = pfnWorker;
677 pArgs->CallbackArgs.pvUser1 = pvUser1;
678 pArgs->CallbackArgs.pvUser2 = pvUser2;
679 pArgs->CallbackArgs.idCpu = idCpu;
680 pArgs->CallbackArgs.cHits = 0;
681 pArgs->CallbackArgs.cRefs = 2;
682 KeInitializeEvent(&pArgs->DoneEvt, SynchronizationEvent, FALSE /* not signalled */);
683 KeInitializeDpc(&pArgs->Dpc, rtMpNtOnSpecificDpcWrapper, pArgs);
684 KeSetImportanceDpc(&pArgs->Dpc, HighImportance);
685 KeSetTargetProcessorDpc(&pArgs->Dpc, (int)idCpu);
686
687 /*
688 * Disable preemption while we check the current processor and inserts the DPC.
689 */
690 KIRQL bOldIrql;
691 KeRaiseIrql(DISPATCH_LEVEL, &bOldIrql);
692 ASMCompilerBarrier(); /* paranoia */
693
694 if (RTMpCpuId() == idCpu)
695 {
696 /* Just execute the callback on the current CPU. */
697 pfnWorker(idCpu, pvUser1, pvUser2);
698 KeLowerIrql(bOldIrql);
699
700 ExFreePool(pArgs);
701 return VINF_SUCCESS;
702 }
703
704 /* Different CPU, so queue it if the CPU is still online. */
705 if (RTMpIsCpuOnline(idCpu))
706 {
707 BOOLEAN fRc = KeInsertQueueDpc(&pArgs->Dpc, 0, 0);
708 Assert(fRc); NOREF(fRc);
709 KeLowerIrql(bOldIrql);
710
711 uint64_t const nsRealWaitTS = RTTimeNanoTS();
712
713 /*
714 * Wait actively for a while in case the CPU/thread responds quickly.
715 */
716 uint32_t cLoopsLeft = 0x20000;
717 while (cLoopsLeft-- > 0)
718 {
719 if (pArgs->fDone)
720 {
721 rtMpNtOnSpecificRelease(pArgs);
722 return VINF_SUCCESS;
723 }
724 ASMNopPause();
725 }
726
727 /*
728 * It didn't respond, so wait on the event object, poking the CPU if it's slow.
729 */
730 LARGE_INTEGER Timeout;
731 Timeout.QuadPart = -10000; /* 1ms */
732 NTSTATUS rcNt = KeWaitForSingleObject(&pArgs->DoneEvt, Executive, KernelMode, FALSE /* Alertable */, &Timeout);
733 if (rcNt == STATUS_SUCCESS)
734 {
735 rtMpNtOnSpecificRelease(pArgs);
736 return VINF_SUCCESS;
737 }
738
739 /* If it hasn't respondend yet, maybe poke it and wait some more. */
740 if (rcNt == STATUS_TIMEOUT)
741 {
742#ifndef IPRT_TARGET_NT4
743 if ( !pArgs->fExecuting
744 && ( g_pfnrtMpPokeCpuWorker == rtMpPokeCpuUsingHalReqestIpiW7Plus
745 || g_pfnrtMpPokeCpuWorker == rtMpPokeCpuUsingHalReqestIpiPreW7))
746 RTMpPokeCpu(idCpu);
747#endif
748
749 Timeout.QuadPart = -1280000; /* 128ms */
750 rcNt = KeWaitForSingleObject(&pArgs->DoneEvt, Executive, KernelMode, FALSE /* Alertable */, &Timeout);
751 if (rcNt == STATUS_SUCCESS)
752 {
753 rtMpNtOnSpecificRelease(pArgs);
754 return VINF_SUCCESS;
755 }
756 }
757
758 /*
759 * Something weird is happening, try bail out.
760 */
761 if (KeRemoveQueueDpc(&pArgs->Dpc))
762 {
763 ExFreePool(pArgs); /* DPC was still queued, so we can return without further ado. */
764 LogRel(("RTMpOnSpecific(%#x): Not processed after %llu ns: rcNt=%#x\n", idCpu, RTTimeNanoTS() - nsRealWaitTS, rcNt));
765 }
766 else
767 {
768 /* DPC is running, wait a good while for it to complete. */
769 LogRel(("RTMpOnSpecific(%#x): Still running after %llu ns: rcNt=%#x\n", idCpu, RTTimeNanoTS() - nsRealWaitTS, rcNt));
770
771 Timeout.QuadPart = -30*1000*1000*10; /* 30 seconds */
772 rcNt = KeWaitForSingleObject(&pArgs->DoneEvt, Executive, KernelMode, FALSE /* Alertable */, &Timeout);
773 if (rcNt != STATUS_SUCCESS)
774 LogRel(("RTMpOnSpecific(%#x): Giving up on running worker after %llu ns: rcNt=%#x\n", idCpu, RTTimeNanoTS() - nsRealWaitTS, rcNt));
775 }
776 rc = RTErrConvertFromNtStatus(rcNt);
777 }
778 else
779 {
780 /* CPU is offline.*/
781 KeLowerIrql(bOldIrql);
782 rc = !RTMpIsCpuPossible(idCpu) ? VERR_CPU_NOT_FOUND : VERR_CPU_OFFLINE;
783 }
784
785 rtMpNtOnSpecificRelease(pArgs);
786 return rc;
787#endif
788}
789
790
791
792
793static VOID rtMpNtPokeCpuDummy(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
794{
795 NOREF(Dpc);
796 NOREF(DeferredContext);
797 NOREF(SystemArgument1);
798 NOREF(SystemArgument2);
799}
800
801#ifndef IPRT_TARGET_NT4
802
803/** Callback used by rtMpPokeCpuUsingBroadcastIpi. */
804static ULONG_PTR rtMpIpiGenericCall(ULONG_PTR Argument)
805{
806 NOREF(Argument);
807 return 0;
808}
809
810
811/**
812 * RTMpPokeCpu worker that uses broadcast IPIs for doing the work.
813 *
814 * @returns VINF_SUCCESS
815 * @param idCpu The CPU identifier.
816 */
817int rtMpPokeCpuUsingBroadcastIpi(RTCPUID idCpu)
818{
819 NOREF(idCpu);
820 g_pfnrtKeIpiGenericCall(rtMpIpiGenericCall, 0);
821 return VINF_SUCCESS;
822}
823
824
825/**
826 * RTMpPokeCpu worker that uses the Windows 7 and later version of
827 * HalRequestIpip to get the job done.
828 *
829 * @returns VINF_SUCCESS
830 * @param idCpu The CPU identifier.
831 */
832int rtMpPokeCpuUsingHalReqestIpiW7Plus(RTCPUID idCpu)
833{
834 /*
835 * I think we'll let idCpu be an NT processor number and not a HAL processor
836 * index. KeAddProcessorAffinityEx is for HAL and uses HAL processor
837 * indexes as input from what I can tell.
838 */
839 PROCESSOR_NUMBER ProcNumber = { /*Group=*/ idCpu / 64, /*Number=*/ idCpu % 64, /* Reserved=*/ 0};
840 KAFFINITY_EX Target;
841 g_pfnrtKeInitializeAffinityEx(&Target);
842 g_pfnrtKeAddProcessorAffinityEx(&Target, g_pfnrtKeGetProcessorIndexFromNumber(&ProcNumber));
843
844 g_pfnrtHalRequestIpiW7Plus(0, &Target);
845 return VINF_SUCCESS;
846}
847
848
849/**
850 * RTMpPokeCpu worker that uses the Vista and earlier version of HalRequestIpip
851 * to get the job done.
852 *
853 * @returns VINF_SUCCESS
854 * @param idCpu The CPU identifier.
855 */
856int rtMpPokeCpuUsingHalReqestIpiPreW7(RTCPUID idCpu)
857{
858 __debugbreak(); /** @todo this code needs testing!! */
859 KAFFINITY Target = 1;
860 Target <<= idCpu;
861 g_pfnrtHalRequestIpiPreW7(Target);
862 return VINF_SUCCESS;
863}
864
865#endif /* !IPRT_TARGET_NT4 */
866
867
868int rtMpPokeCpuUsingDpc(RTCPUID idCpu)
869{
870 /*
871 * APC fallback.
872 */
873 static KDPC s_aPokeDpcs[MAXIMUM_PROCESSORS] = {0};
874 static bool s_fPokeDPCsInitialized = false;
875
876 if (!s_fPokeDPCsInitialized)
877 {
878 for (unsigned i = 0; i < RT_ELEMENTS(s_aPokeDpcs); i++)
879 {
880 KeInitializeDpc(&s_aPokeDpcs[i], rtMpNtPokeCpuDummy, NULL);
881 KeSetImportanceDpc(&s_aPokeDpcs[i], HighImportance);
882 KeSetTargetProcessorDpc(&s_aPokeDpcs[i], (int)i);
883 }
884 s_fPokeDPCsInitialized = true;
885 }
886
887 /* Raise the IRQL to DISPATCH_LEVEL so we can't be rescheduled to another cpu.
888 * KeInsertQueueDpc must also be executed at IRQL >= DISPATCH_LEVEL.
889 */
890 KIRQL oldIrql;
891 KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
892
893 KeSetImportanceDpc(&s_aPokeDpcs[idCpu], HighImportance);
894 KeSetTargetProcessorDpc(&s_aPokeDpcs[idCpu], (int)idCpu);
895
896 /* Assuming here that high importance DPCs will be delivered immediately; or at least an IPI will be sent immediately.
897 * @note: not true on at least Vista & Windows 7
898 */
899 BOOLEAN bRet = KeInsertQueueDpc(&s_aPokeDpcs[idCpu], 0, 0);
900
901 KeLowerIrql(oldIrql);
902 return (bRet == TRUE) ? VINF_SUCCESS : VERR_ACCESS_DENIED /* already queued */;
903}
904
905
906RTDECL(int) RTMpPokeCpu(RTCPUID idCpu)
907{
908 if (!RTMpIsCpuOnline(idCpu))
909 return !RTMpIsCpuPossible(idCpu)
910 ? VERR_CPU_NOT_FOUND
911 : VERR_CPU_OFFLINE;
912 /* Calls rtMpSendIpiFallback, rtMpSendIpiWin7AndLater or rtMpSendIpiVista. */
913 return g_pfnrtMpPokeCpuWorker(idCpu);
914}
915
916
917RTDECL(bool) RTMpOnAllIsConcurrentSafe(void)
918{
919 return false;
920}
921
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette