VirtualBox

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

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

Disabled assertion

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