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