VirtualBox

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

Last change on this file since 7506 was 7506, checked in by vboxsync, 17 years ago

Made assertion private

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/* $Id: mp-r0drv-nt.cpp 7506 2008-03-20 09:58:19Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Multiprocessor, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2008 innotek GmbH
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 "r0drv/mp-r0drv.h"
38
39
40/*******************************************************************************
41* Structures and Typedefs *
42*******************************************************************************/
43typedef enum
44{
45 RT_NT_CPUID_SPECIFIC,
46 RT_NT_CPUID_OTHERS,
47 RT_NT_CPUID_ALL
48} RT_NT_CPUID;
49
50
51/* test a couple of assumption. */
52AssertCompile(MAXIMUM_PROCESSORS <= RTCPUSET_MAX_CPUS);
53AssertCompile(NIL_RTCPUID >= MAXIMUM_PROCESSORS);
54
55/** @todo
56 * We cannot do other than assume a 1:1 relationship between the
57 * affinity mask and the process despite the vagueness/warnings in
58 * the docs. If someone knows a better way to get this done, please
59 * let bird know.
60 */
61
62
63RTDECL(RTCPUID) RTMpCpuId(void)
64{
65 /* WDK upgrade warning: PCR->Number changed from BYTE to WORD. */
66 return KeGetCurrentProcessorNumber();
67}
68
69
70RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
71{
72 return idCpu < MAXIMUM_PROCESSORS ? idCpu : -1;
73}
74
75
76RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
77{
78 return (unsigned)iCpu < MAXIMUM_PROCESSORS ? iCpu : NIL_RTCPUID;
79}
80
81
82RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
83{
84 return MAXIMUM_PROCESSORS - 1;
85}
86
87
88RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
89{
90 if (idCpu >= MAXIMUM_PROCESSORS)
91 return false;
92
93 KAFFINITY Mask = KeQueryActiveProcessors();
94 return !!(Mask & RT_BIT_64(idCpu));
95}
96
97
98RTDECL(bool) RTMpDoesCpuExist(RTCPUID idCpu)
99{
100 /* Cannot easily distinguish between online and offline cpus. */
101 return RTMpIsCpuOnline(idCpu);
102}
103
104
105RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
106{
107 return RTMpGetOnlineSet(pSet);
108}
109
110
111RTDECL(RTCPUID) RTMpGetCount(void)
112{
113 return RTMpGetOnlineCount();
114}
115
116
117RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
118{
119 KAFFINITY Mask = KeQueryActiveProcessors();
120 return RTCpuSetFromU64(pSet, Mask);
121}
122
123
124RTDECL(RTCPUID) RTMpGetOnlineCount(void)
125{
126 RTCPUSET Set;
127 RTMpGetOnlineSet(&Set);
128 return RTCpuSetCount(&Set);
129}
130
131
132/**
133 * Wrapper between the native nt per-cpu callbacks and PFNRTWORKER
134 *
135 * @param Dpc DPC object
136 * @param DeferredContext Context argument specified by KeInitializeDpc
137 * @param SystemArgument1 Argument specified by KeInsertQueueDpc
138 * @param SystemArgument2 Argument specified by KeInsertQueueDpc
139 */
140static VOID rtmpNtDPCWrapper(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
141{
142 PRTMPARGS pArgs = (PRTMPARGS)DeferredContext;
143 ASMAtomicIncU32(&pArgs->cHits);
144 pArgs->pfnWorker(KeGetCurrentProcessorNumber(), pArgs->pvUser1, pArgs->pvUser2);
145}
146
147
148/**
149 * Internal worker for the RTMpOn* APIs.
150 *
151 * @returns IPRT status code.
152 * @param pfnWorker The callback.
153 * @param pvUser1 User argument 1.
154 * @param pvUser2 User argument 2.
155 * @param enmCpuid What to do / is idCpu valid.
156 * @param idCpu Used if enmCpuid RT_NT_CPUID_SPECIFIC, otherwise ignored.
157 */
158static int rtMpCall(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2, RT_NT_CPUID enmCpuid, RTCPUID idCpu)
159{
160 PRTMPARGS pArgs;
161 KDPC *paExecCpuDpcs;
162
163#ifdef DEBUG_sandervl
164 /* KeFlushQueuedDpcs must be run at IRQL PASSIVE_LEVEL */
165 AssertMsg(KeGetCurrentIrql() == PASSIVE_LEVEL, ("%d != %d (PASSIVE_LEVEL)\n", KeGetCurrentIrql(), PASSIVE_LEVEL));
166#endif
167
168 KAFFINITY Mask = KeQueryActiveProcessors();
169
170 if ( enmCpuid == RT_NT_CPUID_SPECIFIC
171 && !(Mask & RT_BIT_64(idCpu)))
172 return VERR_CPU_NOT_FOUND; /* can't distinguish between cpu not present or offline */
173
174 /* KeFlushQueuedDpcs is not present in Windows 2000; import it dynamically so we can just fail this call. */
175 UNICODE_STRING RoutineName;
176 RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
177 VOID (*pfnKeFlushQueuedDpcs)(VOID) = (VOID (*)(VOID))MmGetSystemRoutineAddress(&RoutineName);
178 if (!pfnKeFlushQueuedDpcs)
179 return VERR_NOT_SUPPORTED;
180
181 pArgs = (PRTMPARGS)ExAllocatePoolWithTag(NonPagedPool, MAXIMUM_PROCESSORS*sizeof(KDPC) + sizeof(RTMPARGS), (ULONG)'RTMp');
182 if (!pArgs)
183 return VERR_NO_MEMORY;
184
185 pArgs->pfnWorker = pfnWorker;
186 pArgs->pvUser1 = pvUser1;
187 pArgs->pvUser2 = pvUser2;
188 pArgs->idCpu = NIL_RTCPUID;
189 pArgs->cHits = 0;
190
191 paExecCpuDpcs = (KDPC *)(pArgs + 1);
192
193 if (enmCpuid == RT_NT_CPUID_SPECIFIC)
194 {
195 KeInitializeDpc(&paExecCpuDpcs[0], rtmpNtDPCWrapper, pArgs);
196 KeSetImportanceDpc(&paExecCpuDpcs[0], HighImportance);
197 KeSetTargetProcessorDpc(&paExecCpuDpcs[0], idCpu);
198 }
199 else
200 {
201 for (unsigned i = 0; i < MAXIMUM_PROCESSORS; i++)
202 {
203 KeInitializeDpc(&paExecCpuDpcs[i], rtmpNtDPCWrapper, pArgs);
204 KeSetImportanceDpc(&paExecCpuDpcs[i], HighImportance);
205 KeSetTargetProcessorDpc(&paExecCpuDpcs[i], i);
206 }
207 }
208
209 /* Raise the IRQL to DISPATCH_LEVEL so we can't be rescheduled to another cpu.
210 * KeInsertQueueDpc must also be executed at IRQL >= DISPATCH_LEVEL.
211 */
212 KIRQL oldIrql;
213 KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
214
215 /*
216 * We cannot do other than assume a 1:1 relationship between the
217 * affinity mask and the process despite the warnings in the docs.
218 * If someone knows a better way to get this done, please let bird know.
219 */
220 if (enmCpuid == RT_NT_CPUID_SPECIFIC)
221 {
222 BOOLEAN ret = KeInsertQueueDpc(&paExecCpuDpcs[0], 0, 0);
223 Assert(ret);
224 }
225 else
226 {
227 unsigned iSelf = KeGetCurrentProcessorNumber();
228
229 for (unsigned i = 0; i < MAXIMUM_PROCESSORS; i++)
230 {
231 if ( (i != iSelf)
232 && (Mask & RT_BIT_64(i)))
233 {
234 BOOLEAN ret = KeInsertQueueDpc(&paExecCpuDpcs[i], 0, 0);
235 Assert(ret);
236 }
237 }
238 if (enmCpuid != RT_NT_CPUID_OTHERS)
239 {
240 pfnWorker(iSelf, pvUser1, pvUser2);
241 }
242 }
243
244 KeLowerIrql(oldIrql);
245
246 /* Flush all DPCs and wait for completion. (can take long!) */
247 pfnKeFlushQueuedDpcs();
248
249 ExFreePool(pArgs);
250 return VINF_SUCCESS;
251}
252
253RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
254{
255 return rtMpCall(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_ALL, 0);
256}
257
258RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
259{
260 return rtMpCall(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_OTHERS, 0);
261}
262
263RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
264{
265 return rtMpCall(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_SPECIFIC, idCpu);
266}
267
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