VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/initterm-r0drv-nt.cpp@ 59055

最後變更 在這個檔案從59055是 57978,由 vboxsync 提交於 9 年 前

IPRT: Doxygen warning fixes (last ones, hopefully).

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 18.2 KB
 
1/* $Id: initterm-r0drv-nt.cpp 57978 2015-09-30 19:39:30Z vboxsync $ */
2/** @file
3 * IPRT - Initialization & Termination, R0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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#include <iprt/asm-amd64-x86.h>
33#include <iprt/assert.h>
34#include <iprt/err.h>
35#include <iprt/mp.h>
36#include <iprt/string.h>
37#include "internal/initterm.h"
38#include "internal-r0drv-nt.h"
39#include "symdb.h"
40#include "symdbdata.h"
41
42
43/*********************************************************************************************************************************
44* Global Variables *
45*********************************************************************************************************************************/
46/** The NT CPU set.
47 * KeQueryActiveProcssors() cannot be called at all IRQLs and therefore we'll
48 * have to cache it. Fortunately, Nt doesn't really support taking CPUs offline
49 * or online. It's first with W2K8 that support for CPU hotplugging was added.
50 * Once we start caring about this, we'll simply let the native MP event callback
51 * and update this variable as CPUs comes online. (The code is done already.)
52 */
53RTCPUSET g_rtMpNtCpuSet;
54
55/** ExSetTimerResolution, introduced in W2K. */
56PFNMYEXSETTIMERRESOLUTION g_pfnrtNtExSetTimerResolution;
57/** KeFlushQueuedDpcs, introduced in XP. */
58PFNMYKEFLUSHQUEUEDDPCS g_pfnrtNtKeFlushQueuedDpcs;
59/** HalRequestIpi, version introduced with windows 7. */
60PFNHALREQUESTIPI_W7PLUS g_pfnrtHalRequestIpiW7Plus;
61/** HalRequestIpi, version valid up to windows vista?? */
62PFNHALREQUESTIPI_PRE_W7 g_pfnrtHalRequestIpiPreW7;
63/** HalSendSoftwareInterrupt, introduced in AMD64 version of W2K3. */
64PFNHALSENDSOFTWAREINTERRUPT g_pfnrtNtHalSendSoftwareInterrupt;
65/** Worker for RTMpPokeCpu. */
66PFNRTSENDIPI g_pfnrtMpPokeCpuWorker;
67/** KeIpiGenericCall - Introduced in Windows Server 2003. */
68PFNRTKEIPIGENERICCALL g_pfnrtKeIpiGenericCall;
69/** KeInitializeAffinityEx - Introducted in Windows 7. */
70PFNKEINITIALIZEAFFINITYEX g_pfnrtKeInitializeAffinityEx;
71/** KeAddProcessorAffinityEx - Introducted in Windows 7. */
72PFNKEADDPROCESSORAFFINITYEX g_pfnrtKeAddProcessorAffinityEx;
73/** KeGetProcessorIndexFromNumber - Introducted in Windows 7. */
74PFNKEGETPROCESSORINDEXFROMNUMBER g_pfnrtKeGetProcessorIndexFromNumber;
75/** RtlGetVersion, introduced in ??. */
76PFNRTRTLGETVERSION g_pfnrtRtlGetVersion;
77#ifndef RT_ARCH_AMD64
78/** KeQueryInterruptTime - exported/new in Windows 2000. */
79PFNRTKEQUERYINTERRUPTTIME g_pfnrtKeQueryInterruptTime;
80/** KeQuerySystemTime - exported/new in Windows 2000. */
81PFNRTKEQUERYSYSTEMTIME g_pfnrtKeQuerySystemTime;
82#endif
83/** KeQueryInterruptTimePrecise - new in Windows 8. */
84PFNRTKEQUERYINTERRUPTTIMEPRECISE g_pfnrtKeQueryInterruptTimePrecise;
85/** KeQuerySystemTimePrecise - new in Windows 8. */
86PFNRTKEQUERYSYSTEMTIMEPRECISE g_pfnrtKeQuerySystemTimePrecise;
87
88/** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
89uint32_t g_offrtNtPbQuantumEnd;
90/** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
91uint32_t g_cbrtNtPbQuantumEnd;
92/** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
93uint32_t g_offrtNtPbDpcQueueDepth;
94
95
96/**
97 * Determines the NT kernel verison information.
98 *
99 * @param pOsVerInfo Where to return the version information.
100 *
101 * @remarks pOsVerInfo->fSmp is only definitive if @c true.
102 * @remarks pOsVerInfo->uCsdNo is set to MY_NIL_CSD if it cannot be determined.
103 */
104static void rtR0NtGetOsVersionInfo(PRTNTSDBOSVER pOsVerInfo)
105{
106 ULONG ulMajorVersion = 0;
107 ULONG ulMinorVersion = 0;
108 ULONG ulBuildNumber = 0;
109
110 pOsVerInfo->fChecked = PsGetVersion(&ulMajorVersion, &ulMinorVersion, &ulBuildNumber, NULL) == TRUE;
111 pOsVerInfo->uMajorVer = (uint8_t)ulMajorVersion;
112 pOsVerInfo->uMinorVer = (uint8_t)ulMinorVersion;
113 pOsVerInfo->uBuildNo = ulBuildNumber;
114#define MY_NIL_CSD 0x3f
115 pOsVerInfo->uCsdNo = MY_NIL_CSD;
116
117 if (g_pfnrtRtlGetVersion)
118 {
119 RTL_OSVERSIONINFOEXW VerInfo;
120 RT_ZERO(VerInfo);
121 VerInfo.dwOSVersionInfoSize = sizeof(VerInfo);
122
123 NTSTATUS rcNt = g_pfnrtRtlGetVersion(&VerInfo);
124 if (NT_SUCCESS(rcNt))
125 pOsVerInfo->uCsdNo = VerInfo.wServicePackMajor;
126 }
127
128 /* Note! We cannot quite say if something is MP or UNI. So, fSmp is
129 redefined to indicate that it must be MP. */
130 pOsVerInfo->fSmp = RTMpGetCount() > 1
131 || ulMajorVersion >= 6; /* Vista and later has no UNI kernel AFAIK. */
132}
133
134
135/**
136 * Tries a set against the current kernel.
137 *
138 * @retval true if it matched up, global variables are updated.
139 * @retval false otherwise (no globals updated).
140 * @param pSet The data set.
141 * @param pbPrcb Pointer to the processor control block.
142 * @param pszVendor Pointer to the processor vendor string.
143 * @param pOsVerInfo The OS version info.
144 */
145static bool rtR0NtTryMatchSymSet(PCRTNTSDBSET pSet, uint8_t *pbPrcb, const char *pszVendor, PCRTNTSDBOSVER pOsVerInfo)
146{
147 /*
148 * Don't bother trying stuff where the NT kernel version number differs, or
149 * if the build type or SMPness doesn't match up.
150 */
151 if ( pSet->OsVerInfo.uMajorVer != pOsVerInfo->uMajorVer
152 || pSet->OsVerInfo.uMinorVer != pOsVerInfo->uMinorVer
153 || pSet->OsVerInfo.fChecked != pOsVerInfo->fChecked
154 || (!pSet->OsVerInfo.fSmp && pOsVerInfo->fSmp /*must-be-smp*/) )
155 {
156 //DbgPrint("IPRT: #%d Version/type mismatch.\n", pSet - &g_artNtSdbSets[0]);
157 return false;
158 }
159
160 /*
161 * Do the CPU vendor test.
162 *
163 * Note! The MmIsAddressValid call is the real #PF security here as the
164 * __try/__except has limited/no ability to catch everything we need.
165 */
166 char *pszPrcbVendorString = (char *)&pbPrcb[pSet->KPRCB.offVendorString];
167 if (!MmIsAddressValid(&pszPrcbVendorString[4 * 3 - 1]))
168 {
169 //DbgPrint("IPRT: #%d invalid vendor string address.\n", pSet - &g_artNtSdbSets[0]);
170 return false;
171 }
172 __try
173 {
174 if (memcmp(pszPrcbVendorString, pszVendor, RT_MIN(4 * 3, pSet->KPRCB.cbVendorString)) != 0)
175 {
176 //DbgPrint("IPRT: #%d Vendor string mismatch.\n", pSet - &g_artNtSdbSets[0]);
177 return false;
178 }
179 }
180 __except(EXCEPTION_EXECUTE_HANDLER)
181 {
182 DbgPrint("IPRT: %#d Exception\n", pSet - &g_artNtSdbSets[0]);
183 return false;
184 }
185
186 /*
187 * Got a match, update the global variables and report succcess.
188 */
189 g_offrtNtPbQuantumEnd = pSet->KPRCB.offQuantumEnd;
190 g_cbrtNtPbQuantumEnd = pSet->KPRCB.cbQuantumEnd;
191 g_offrtNtPbDpcQueueDepth = pSet->KPRCB.offDpcQueueDepth;
192
193#if 0
194 DbgPrint("IPRT: Using data set #%u for %u.%usp%u build %u %s %s.\n",
195 pSet - &g_artNtSdbSets[0],
196 pSet->OsVerInfo.uMajorVer,
197 pSet->OsVerInfo.uMinorVer,
198 pSet->OsVerInfo.uCsdNo,
199 pSet->OsVerInfo.uBuildNo,
200 pSet->OsVerInfo.fSmp ? "smp" : "uni",
201 pSet->OsVerInfo.fChecked ? "checked" : "free");
202#endif
203 return true;
204}
205
206
207DECLHIDDEN(int) rtR0InitNative(void)
208{
209 /*
210 * Init the Nt cpu set.
211 */
212#ifdef IPRT_TARGET_NT4
213 KAFFINITY ActiveProcessors = (UINT64_C(1) << KeNumberProcessors) - UINT64_C(1);
214#else
215 KAFFINITY ActiveProcessors = KeQueryActiveProcessors();
216#endif
217 RTCpuSetEmpty(&g_rtMpNtCpuSet);
218 RTCpuSetFromU64(&g_rtMpNtCpuSet, ActiveProcessors);
219/** @todo Port to W2K8 with > 64 cpus/threads. */
220
221 /*
222 * Initialize the function pointers.
223 */
224#ifdef IPRT_TARGET_NT4
225 g_pfnrtNtExSetTimerResolution = NULL;
226 g_pfnrtNtKeFlushQueuedDpcs = NULL;
227 g_pfnrtHalRequestIpiW7Plus = NULL;
228 g_pfnrtHalRequestIpiPreW7 = NULL;
229 g_pfnrtNtHalSendSoftwareInterrupt = NULL;
230 g_pfnrtKeIpiGenericCall = NULL;
231 g_pfnrtKeInitializeAffinityEx = NULL;
232 g_pfnrtKeAddProcessorAffinityEx = NULL;
233 g_pfnrtKeGetProcessorIndexFromNumber = NULL;
234 g_pfnrtRtlGetVersion = NULL;
235 g_pfnrtKeQueryInterruptTime = NULL;
236 g_pfnrtKeQueryInterruptTimePrecise = NULL;
237 g_pfnrtKeQuerySystemTime = NULL;
238 g_pfnrtKeQuerySystemTimePrecise = NULL;
239#else
240 UNICODE_STRING RoutineName;
241 RtlInitUnicodeString(&RoutineName, L"ExSetTimerResolution");
242 g_pfnrtNtExSetTimerResolution = (PFNMYEXSETTIMERRESOLUTION)MmGetSystemRoutineAddress(&RoutineName);
243
244 RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
245 g_pfnrtNtKeFlushQueuedDpcs = (PFNMYKEFLUSHQUEUEDDPCS)MmGetSystemRoutineAddress(&RoutineName);
246
247 RtlInitUnicodeString(&RoutineName, L"HalRequestIpi");
248 g_pfnrtHalRequestIpiW7Plus = (PFNHALREQUESTIPI_W7PLUS)MmGetSystemRoutineAddress(&RoutineName);
249 g_pfnrtHalRequestIpiPreW7 = (PFNHALREQUESTIPI_PRE_W7)g_pfnrtHalRequestIpiW7Plus;
250
251 RtlInitUnicodeString(&RoutineName, L"HalSendSoftwareInterrupt");
252 g_pfnrtNtHalSendSoftwareInterrupt = (PFNHALSENDSOFTWAREINTERRUPT)MmGetSystemRoutineAddress(&RoutineName);
253
254 RtlInitUnicodeString(&RoutineName, L"KeIpiGenericCall");
255 g_pfnrtKeIpiGenericCall = (PFNRTKEIPIGENERICCALL)MmGetSystemRoutineAddress(&RoutineName);
256
257 RtlInitUnicodeString(&RoutineName, L"KeInitializeAffinityEx");
258 g_pfnrtKeInitializeAffinityEx = (PFNKEINITIALIZEAFFINITYEX)MmGetSystemRoutineAddress(&RoutineName);
259
260 RtlInitUnicodeString(&RoutineName, L"KeAddProcessorAffinityEx");
261 g_pfnrtKeAddProcessorAffinityEx = (PFNKEADDPROCESSORAFFINITYEX)MmGetSystemRoutineAddress(&RoutineName);
262
263 RtlInitUnicodeString(&RoutineName, L"KeGetProcessorIndexFromNumber");
264 g_pfnrtKeGetProcessorIndexFromNumber = (PFNKEGETPROCESSORINDEXFROMNUMBER)MmGetSystemRoutineAddress(&RoutineName);
265
266 RtlInitUnicodeString(&RoutineName, L"RtlGetVersion");
267 g_pfnrtRtlGetVersion = (PFNRTRTLGETVERSION)MmGetSystemRoutineAddress(&RoutineName);
268# ifndef RT_ARCH_AMD64
269 RtlInitUnicodeString(&RoutineName, L"KeQueryInterruptTime");
270 g_pfnrtKeQueryInterruptTime = (PFNRTKEQUERYINTERRUPTTIME)MmGetSystemRoutineAddress(&RoutineName);
271
272 RtlInitUnicodeString(&RoutineName, L"KeQuerySystemTime");
273 g_pfnrtKeQuerySystemTime = (PFNRTKEQUERYSYSTEMTIME)MmGetSystemRoutineAddress(&RoutineName);
274# endif
275 RtlInitUnicodeString(&RoutineName, L"KeQueryInterruptTimePrecise");
276 g_pfnrtKeQueryInterruptTimePrecise = (PFNRTKEQUERYINTERRUPTTIMEPRECISE)MmGetSystemRoutineAddress(&RoutineName);
277
278 RtlInitUnicodeString(&RoutineName, L"KeQuerySystemTimePrecise");
279 g_pfnrtKeQuerySystemTimePrecise = (PFNRTKEQUERYSYSTEMTIMEPRECISE)MmGetSystemRoutineAddress(&RoutineName);
280#endif
281
282 /*
283 * HACK ALERT! (and déjà vu warning - remember win32k.sys?)
284 *
285 * Try find _KPRCB::QuantumEnd and _KPRCB::[DpcData.]DpcQueueDepth.
286 * For purpose of verification we use the VendorString member (12+1 chars).
287 *
288 * The offsets was initially derived by poking around with windbg
289 * (dt _KPRCB, !prcb ++, and such like). Systematic harvesting was then
290 * planned using dia2dump, grep and the symbol pack in a manner like this:
291 * dia2dump -type _KDPC_DATA -type _KPRCB EXE\ntkrnlmp.pdb | grep -wE "QuantumEnd|DpcData|DpcQueueDepth|VendorString"
292 *
293 * The final solution ended up using a custom harvester program called
294 * ntBldSymDb that recursively searches thru unpacked symbol packages for
295 * the desired structure offsets. The program assumes that the packages
296 * are unpacked into directories with the same name as the package, with
297 * exception of some of the w2k packages which requires a 'w2k' prefix to
298 * be distinguishable from another.
299 */
300
301 RTNTSDBOSVER OsVerInfo;
302 rtR0NtGetOsVersionInfo(&OsVerInfo);
303
304 /*
305 * Gather consistent CPU vendor string and PRCB pointers.
306 */
307 KIRQL OldIrql;
308 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); /* make sure we stay on the same cpu */
309
310 union
311 {
312 uint32_t auRegs[4];
313 char szVendor[4*3+1];
314 } u;
315 ASMCpuId(0, &u.auRegs[3], &u.auRegs[0], &u.auRegs[2], &u.auRegs[1]);
316 u.szVendor[4*3] = '\0';
317
318 uint8_t *pbPrcb;
319 __try /* Warning. This try/except statement may provide some false safety. */
320 {
321#if defined(RT_ARCH_X86)
322 PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
323 pbPrcb = (uint8_t *)pPcr->Prcb;
324#elif defined(RT_ARCH_AMD64)
325 PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
326 pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
327#else
328# error "port me"
329 pbPrcb = NULL;
330#endif
331 }
332 __except(EXCEPTION_EXECUTE_HANDLER)
333 {
334 pbPrcb = NULL;
335 }
336
337 /*
338 * Search the database
339 */
340 if (pbPrcb)
341 {
342 /* Find the best matching kernel version based on build number. */
343 uint32_t iBest = UINT32_MAX;
344 int32_t iBestDelta = INT32_MAX;
345 for (uint32_t i = 0; i < RT_ELEMENTS(g_artNtSdbSets); i++)
346 {
347 if (g_artNtSdbSets[i].OsVerInfo.fChecked != OsVerInfo.fChecked)
348 continue;
349 if (OsVerInfo.fSmp /*must-be-smp*/ && !g_artNtSdbSets[i].OsVerInfo.fSmp)
350 continue;
351
352 int32_t iDelta = RT_ABS((int32_t)OsVerInfo.uBuildNo - (int32_t)g_artNtSdbSets[i].OsVerInfo.uBuildNo);
353 if ( iDelta == 0
354 && (g_artNtSdbSets[i].OsVerInfo.uCsdNo == OsVerInfo.uCsdNo || OsVerInfo.uCsdNo == MY_NIL_CSD))
355 {
356 /* prefect */
357 iBestDelta = iDelta;
358 iBest = i;
359 break;
360 }
361 if ( iDelta < iBestDelta
362 || iBest == UINT32_MAX
363 || ( iDelta == iBestDelta
364 && OsVerInfo.uCsdNo != MY_NIL_CSD
365 && RT_ABS(g_artNtSdbSets[i ].OsVerInfo.uCsdNo - (int32_t)OsVerInfo.uCsdNo)
366 < RT_ABS(g_artNtSdbSets[iBest].OsVerInfo.uCsdNo - (int32_t)OsVerInfo.uCsdNo)
367 )
368 )
369 {
370 iBestDelta = iDelta;
371 iBest = i;
372 }
373 }
374 if (iBest < RT_ELEMENTS(g_artNtSdbSets))
375 {
376 /* Try all sets: iBest -> End; iBest -> Start. */
377 bool fDone = false;
378 int32_t i = iBest;
379 while ( i < RT_ELEMENTS(g_artNtSdbSets)
380 && !(fDone = rtR0NtTryMatchSymSet(&g_artNtSdbSets[i], pbPrcb, u.szVendor, &OsVerInfo)))
381 i++;
382 if (!fDone)
383 {
384 i = (int32_t)iBest - 1;
385 while ( i >= 0
386 && !(fDone = rtR0NtTryMatchSymSet(&g_artNtSdbSets[i], pbPrcb, u.szVendor, &OsVerInfo)))
387 i--;
388 }
389 }
390 else
391 DbgPrint("IPRT: Failed to locate data set.\n");
392 }
393 else
394 DbgPrint("IPRT: Failed to get PCBR pointer.\n");
395
396 KeLowerIrql(OldIrql); /* Lowering the IRQL early in the hope that we may catch exceptions below. */
397
398#ifndef IN_GUEST
399 if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
400 DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found! Kernel %u.%u %u %s\n",
401 OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
402# ifdef DEBUG
403 else
404 DbgPrint("IPRT: _KPRCB:{.QuantumEnd=%x/%d, .DpcQueueDepth=%x/%d} Kernel %u.%u %u %s\n",
405 g_offrtNtPbQuantumEnd, g_cbrtNtPbQuantumEnd, g_offrtNtPbDpcQueueDepth,
406 OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
407# endif
408#endif
409
410 /*
411 * Special IPI fun for RTMpPokeCpu.
412 *
413 * On Vista and later the DPC method doesn't seem to reliably send IPIs,
414 * so we have to use alternative methods. The NtHalSendSoftwareInterrupt
415 * is preferrable, but it's AMD64 only. The NalRequestIpip method changed
416 * in Windows 7 with the lots-of-processors-support, but it's the only
417 * targeted IPI game in town if we cannot use KeInsertQueueDpc. Worst case
418 * we use broadcast IPIs.
419 */
420 if ( OsVerInfo.uMajorVer > 6
421 || (OsVerInfo.uMajorVer == 6 && OsVerInfo.uMinorVer > 0))
422 g_pfnrtHalRequestIpiPreW7 = NULL;
423 else
424 g_pfnrtHalRequestIpiW7Plus = NULL;
425
426 g_pfnrtMpPokeCpuWorker = rtMpPokeCpuUsingDpc;
427#ifndef IPRT_TARGET_NT4
428 if (g_pfnrtNtHalSendSoftwareInterrupt)
429 g_pfnrtMpPokeCpuWorker = rtMpPokeCpuUsingHalSendSoftwareInterrupt;
430 else if ( g_pfnrtHalRequestIpiW7Plus
431 && g_pfnrtKeInitializeAffinityEx
432 && g_pfnrtKeAddProcessorAffinityEx
433 && g_pfnrtKeGetProcessorIndexFromNumber)
434 g_pfnrtMpPokeCpuWorker = rtMpPokeCpuUsingHalReqestIpiW7Plus;
435 else if (OsVerInfo.uMajorVer >= 6 && g_pfnrtKeIpiGenericCall)
436 g_pfnrtMpPokeCpuWorker = rtMpPokeCpuUsingBroadcastIpi;
437 /* else: Windows XP should send always send an IPI -> VERIFY */
438#endif
439
440 return VINF_SUCCESS;
441}
442
443
444DECLHIDDEN(void) rtR0TermNative(void)
445{
446}
447
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette