VirtualBox

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

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

More comments

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