VirtualBox

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

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

The Big Sun Rebranding Header Change

  • 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 8155 2008-04-18 15:16:47Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - 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#ifdef DEBUG_sandervl
168 /* KeFlushQueuedDpcs must be run at IRQL PASSIVE_LEVEL */
169 AssertMsg(KeGetCurrentIrql() == PASSIVE_LEVEL, ("%d != %d (PASSIVE_LEVEL)\n", KeGetCurrentIrql(), PASSIVE_LEVEL));
170#endif
171
172 KAFFINITY Mask = KeQueryActiveProcessors();
173
174 if ( enmCpuid == RT_NT_CPUID_SPECIFIC
175 && ( idCpu >= 64
176 || !(Mask & RT_BIT_64(idCpu))))
177 return VERR_CPU_NOT_FOUND; /* can't distinguish between cpu not present or offline */
178
179 /* KeFlushQueuedDpcs is not present in Windows 2000; import it dynamically so we can just fail this call. */
180 UNICODE_STRING RoutineName;
181 RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
182 VOID (*pfnKeFlushQueuedDpcs)(VOID) = (VOID (*)(VOID))MmGetSystemRoutineAddress(&RoutineName);
183 if (!pfnKeFlushQueuedDpcs)
184 return VERR_NOT_SUPPORTED;
185
186 pArgs = (PRTMPARGS)ExAllocatePoolWithTag(NonPagedPool, MAXIMUM_PROCESSORS*sizeof(KDPC) + sizeof(RTMPARGS), (ULONG)'RTMp');
187 if (!pArgs)
188 return VERR_NO_MEMORY;
189
190 pArgs->pfnWorker = pfnWorker;
191 pArgs->pvUser1 = pvUser1;
192 pArgs->pvUser2 = pvUser2;
193 pArgs->idCpu = NIL_RTCPUID;
194 pArgs->cHits = 0;
195
196 paExecCpuDpcs = (KDPC *)(pArgs + 1);
197
198 if (enmCpuid == RT_NT_CPUID_SPECIFIC)
199 {
200 KeInitializeDpc(&paExecCpuDpcs[0], rtmpNtDPCWrapper, pArgs);
201 KeSetImportanceDpc(&paExecCpuDpcs[0], HighImportance);
202 KeSetTargetProcessorDpc(&paExecCpuDpcs[0], idCpu);
203 }
204 else
205 {
206 for (unsigned i = 0; i < MAXIMUM_PROCESSORS; i++)
207 {
208 KeInitializeDpc(&paExecCpuDpcs[i], rtmpNtDPCWrapper, pArgs);
209 KeSetImportanceDpc(&paExecCpuDpcs[i], HighImportance);
210 KeSetTargetProcessorDpc(&paExecCpuDpcs[i], i);
211 }
212 }
213
214 /* Raise the IRQL to DISPATCH_LEVEL so we can't be rescheduled to another cpu.
215 * KeInsertQueueDpc must also be executed at IRQL >= DISPATCH_LEVEL.
216 */
217 KIRQL oldIrql;
218 KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
219
220 /*
221 * We cannot do other than assume a 1:1 relationship between the
222 * affinity mask and the process despite the warnings in the docs.
223 * If someone knows a better way to get this done, please let bird know.
224 */
225 if (enmCpuid == RT_NT_CPUID_SPECIFIC)
226 {
227 BOOLEAN ret = KeInsertQueueDpc(&paExecCpuDpcs[0], 0, 0);
228 Assert(ret);
229 }
230 else
231 {
232 unsigned iSelf = KeGetCurrentProcessorNumber();
233
234 for (unsigned i = 0; i < MAXIMUM_PROCESSORS; i++)
235 {
236 if ( (i != iSelf)
237 && (Mask & RT_BIT_64(i)))
238 {
239 BOOLEAN ret = KeInsertQueueDpc(&paExecCpuDpcs[i], 0, 0);
240 Assert(ret);
241 }
242 }
243 if (enmCpuid != RT_NT_CPUID_OTHERS)
244 {
245 pfnWorker(iSelf, pvUser1, pvUser2);
246 }
247 }
248
249 KeLowerIrql(oldIrql);
250
251 /* Flush all DPCs and wait for completion. (can take long!) */
252 pfnKeFlushQueuedDpcs();
253
254 ExFreePool(pArgs);
255 return VINF_SUCCESS;
256}
257
258RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
259{
260 return rtMpCall(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_ALL, 0);
261}
262
263RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
264{
265 return rtMpCall(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_OTHERS, 0);
266}
267
268RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
269{
270 return rtMpCall(pfnWorker, pvUser1, pvUser2, RT_NT_CPUID_SPECIFIC, idCpu);
271}
272
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