VirtualBox

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

Last change on this file since 90794 was 90417, checked in by vboxsync, 4 years ago

IPRT: Implemented RTThreadQueryTerminationStatus for NT (windows). bugref:6695

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 21.9 KB
Line 
1/* $Id: initterm-r0drv-nt.cpp 90417 2021-07-29 21:29:10Z vboxsync $ */
2/** @file
3 * IPRT - Initialization & Termination, R0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
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#include <iprt/asm-amd64-x86.h>
33#include <iprt/dbg.h>
34#include <iprt/errcore.h>
35#include <iprt/string.h>
36#include "internal/initterm.h"
37#include "internal-r0drv-nt.h"
38#include "symdb.h"
39#include "symdbdata.h"
40
41
42/*********************************************************************************************************************************
43* Global Variables *
44*********************************************************************************************************************************/
45/** ExAllocatePoolWithTag, introduced in W2K. */
46decltype(ExAllocatePoolWithTag) *g_pfnrtExAllocatePoolWithTag;
47/** ExFreePoolWithTag, introduced in W2K. */
48decltype(ExFreePoolWithTag) *g_pfnrtExFreePoolWithTag;
49/** ExSetTimerResolution, introduced in W2K. */
50PFNMYEXSETTIMERRESOLUTION g_pfnrtNtExSetTimerResolution;
51/** KeFlushQueuedDpcs, introduced in XP. */
52PFNMYKEFLUSHQUEUEDDPCS g_pfnrtNtKeFlushQueuedDpcs;
53/** HalRequestIpi, version introduced with windows 7. */
54PFNHALREQUESTIPI_W7PLUS g_pfnrtHalRequestIpiW7Plus;
55/** HalRequestIpi, version valid up to windows vista?? */
56PFNHALREQUESTIPI_PRE_W7 g_pfnrtHalRequestIpiPreW7;
57/** Worker for RTMpPokeCpu. */
58PFNRTSENDIPI g_pfnrtMpPokeCpuWorker;
59/** KeIpiGenericCall - Introduced in Windows Server 2003. */
60PFNRTKEIPIGENERICCALL g_pfnrtKeIpiGenericCall;
61/** KeSetTargetProcessorDpcEx - Introduced in Windows 7. */
62PFNKESETTARGETPROCESSORDPCEX g_pfnrtKeSetTargetProcessorDpcEx;
63/** KeInitializeAffinityEx - Introducted in Windows 7. */
64PFNKEINITIALIZEAFFINITYEX g_pfnrtKeInitializeAffinityEx;
65/** KeAddProcessorAffinityEx - Introducted in Windows 7. */
66PFNKEADDPROCESSORAFFINITYEX g_pfnrtKeAddProcessorAffinityEx;
67/** KeGetProcessorIndexFromNumber - Introducted in Windows 7. */
68PFNKEGETPROCESSORINDEXFROMNUMBER g_pfnrtKeGetProcessorIndexFromNumber;
69/** KeGetProcessorNumberFromIndex - Introducted in Windows 7. */
70PFNKEGETPROCESSORNUMBERFROMINDEX g_pfnrtKeGetProcessorNumberFromIndex;
71/** KeGetCurrentProcessorNumberEx - Introducted in Windows 7. */
72PFNKEGETCURRENTPROCESSORNUMBEREX g_pfnrtKeGetCurrentProcessorNumberEx;
73/** KeQueryActiveProcessors - Introducted in Windows 2000. */
74PFNKEQUERYACTIVEPROCESSORS g_pfnrtKeQueryActiveProcessors;
75/** KeQueryMaximumProcessorCount - Introducted in Vista and obsoleted W7. */
76PFNKEQUERYMAXIMUMPROCESSORCOUNT g_pfnrtKeQueryMaximumProcessorCount;
77/** KeQueryMaximumProcessorCountEx - Introducted in Windows 7. */
78PFNKEQUERYMAXIMUMPROCESSORCOUNTEX g_pfnrtKeQueryMaximumProcessorCountEx;
79/** KeQueryMaximumGroupCount - Introducted in Windows 7. */
80PFNKEQUERYMAXIMUMGROUPCOUNT g_pfnrtKeQueryMaximumGroupCount;
81/** KeQueryActiveProcessorCount - Introducted in Vista and obsoleted W7. */
82PFNKEQUERYACTIVEPROCESSORCOUNT g_pfnrtKeQueryActiveProcessorCount;
83/** KeQueryActiveProcessorCountEx - Introducted in Windows 7. */
84PFNKEQUERYACTIVEPROCESSORCOUNTEX g_pfnrtKeQueryActiveProcessorCountEx;
85/** KeQueryLogicalProcessorRelationship - Introducted in Windows 7. */
86PFNKEQUERYLOGICALPROCESSORRELATIONSHIP g_pfnrtKeQueryLogicalProcessorRelationship;
87/** KeRegisterProcessorChangeCallback - Introducted in Windows 7. */
88PFNKEREGISTERPROCESSORCHANGECALLBACK g_pfnrtKeRegisterProcessorChangeCallback;
89/** KeDeregisterProcessorChangeCallback - Introducted in Windows 7. */
90PFNKEDEREGISTERPROCESSORCHANGECALLBACK g_pfnrtKeDeregisterProcessorChangeCallback;
91/** KeSetImportanceDpc - Introducted in NT 3.51. */
92decltype(KeSetImportanceDpc) *g_pfnrtKeSetImportanceDpc;
93/** KeSetTargetProcessorDpc - Introducted in NT 3.51. */
94decltype(KeSetTargetProcessorDpc) *g_pfnrtKeSetTargetProcessorDpc;
95/** KeInitializeTimerEx - Introduced in NT 4. */
96decltype(KeInitializeTimerEx) *g_pfnrtKeInitializeTimerEx;
97/** KeShouldYieldProcessor - Introduced in Windows 10. */
98PFNKESHOULDYIELDPROCESSOR g_pfnrtKeShouldYieldProcessor;
99/** Pointer to the MmProtectMdlSystemAddress kernel function if it's available.
100 * This API was introduced in XP. */
101decltype(MmProtectMdlSystemAddress) *g_pfnrtMmProtectMdlSystemAddress;
102/** MmAllocatePagesForMdl - Introduced in Windows 2000. */
103decltype(MmAllocatePagesForMdl) *g_pfnrtMmAllocatePagesForMdl;
104/** MmFreePagesFromMdl - Introduced in Windows 2000. */
105decltype(MmFreePagesFromMdl) *g_pfnrtMmFreePagesFromMdl;
106/** MmMapLockedPagesSpecifyCache - Introduced in Windows NT4 SP4. */
107decltype(MmMapLockedPagesSpecifyCache) *g_pfnrtMmMapLockedPagesSpecifyCache;
108/** MmAllocateContiguousMemorySpecifyCache - Introduced in Windows 2000. */
109decltype(MmAllocateContiguousMemorySpecifyCache) *g_pfnrtMmAllocateContiguousMemorySpecifyCache;
110/** MmSecureVirtualMemory - Introduced in NT 3.51. */
111decltype(MmSecureVirtualMemory) *g_pfnrtMmSecureVirtualMemory;
112/** MmUnsecureVirtualMemory - Introduced in NT 3.51. */
113decltype(MmUnsecureVirtualMemory) *g_pfnrtMmUnsecureVirtualMemory;
114/** PsIsThreadTerminating - Introduced in NT 3.50. */
115decltype(PsIsThreadTerminating) *g_pfnrtPsIsThreadTerminating;
116/** RtlGetVersion, introduced in ??. */
117PFNRTRTLGETVERSION g_pfnrtRtlGetVersion;
118#ifdef RT_ARCH_X86
119/** KeQueryInterruptTime - exported/new in Windows 2000. */
120PFNRTKEQUERYINTERRUPTTIME g_pfnrtKeQueryInterruptTime;
121#endif
122/** KeQueryInterruptTimePrecise - new in Windows 8. */
123PFNRTKEQUERYINTERRUPTTIMEPRECISE g_pfnrtKeQueryInterruptTimePrecise;
124/** KeQuerySystemTimePrecise - new in Windows 8. */
125PFNRTKEQUERYSYSTEMTIMEPRECISE g_pfnrtKeQuerySystemTimePrecise;
126
127/** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
128uint32_t g_offrtNtPbQuantumEnd;
129/** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
130uint32_t g_cbrtNtPbQuantumEnd;
131/** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
132uint32_t g_offrtNtPbDpcQueueDepth;
133
134/** The combined NT version, see RTNT_MAKE_VERSION. */
135uint32_t g_uRtNtVersion = RTNT_MAKE_VERSION(4, 0);
136/** The major version number. */
137uint8_t g_uRtNtMajorVer;
138/** The minor version number. */
139uint8_t g_uRtNtMinorVer;
140/** The build number. */
141uint32_t g_uRtNtBuildNo;
142
143/** Pointer to the MmHighestUserAddress kernel variable - can be NULL. */
144uintptr_t const *g_puRtMmHighestUserAddress;
145/** Pointer to the MmSystemRangeStart kernel variable - can be NULL. */
146uintptr_t const *g_puRtMmSystemRangeStart;
147
148
149/**
150 * Determines the NT kernel verison information.
151 *
152 * @param pOsVerInfo Where to return the version information.
153 *
154 * @remarks pOsVerInfo->fSmp is only definitive if @c true.
155 * @remarks pOsVerInfo->uCsdNo is set to MY_NIL_CSD if it cannot be determined.
156 */
157static void rtR0NtGetOsVersionInfo(PRTNTSDBOSVER pOsVerInfo)
158{
159 ULONG ulMajorVersion = 0;
160 ULONG ulMinorVersion = 0;
161 ULONG ulBuildNumber = 0;
162
163 pOsVerInfo->fChecked = PsGetVersion(&ulMajorVersion, &ulMinorVersion, &ulBuildNumber, NULL) == TRUE;
164 pOsVerInfo->uMajorVer = (uint8_t)ulMajorVersion;
165 pOsVerInfo->uMinorVer = (uint8_t)ulMinorVersion;
166 pOsVerInfo->uBuildNo = ulBuildNumber;
167#define MY_NIL_CSD 0x3f
168 pOsVerInfo->uCsdNo = MY_NIL_CSD;
169
170 if (g_pfnrtRtlGetVersion)
171 {
172 RTL_OSVERSIONINFOEXW VerInfo;
173 RT_ZERO(VerInfo);
174 VerInfo.dwOSVersionInfoSize = sizeof(VerInfo);
175
176 NTSTATUS rcNt = g_pfnrtRtlGetVersion(&VerInfo);
177 if (NT_SUCCESS(rcNt))
178 pOsVerInfo->uCsdNo = VerInfo.wServicePackMajor;
179 }
180
181 /* Note! We cannot quite say if something is MP or UNI. So, fSmp is
182 redefined to indicate that it must be MP.
183 Note! RTMpGetCount is not available here. */
184 pOsVerInfo->fSmp = ulMajorVersion >= 6; /* Vista and later has no UNI kernel AFAIK. */
185 if (!pOsVerInfo->fSmp)
186 {
187 if ( g_pfnrtKeQueryMaximumProcessorCountEx
188 && g_pfnrtKeQueryMaximumProcessorCountEx(ALL_PROCESSOR_GROUPS) > 1)
189 pOsVerInfo->fSmp = true;
190 else if ( g_pfnrtKeQueryMaximumProcessorCount
191 && g_pfnrtKeQueryMaximumProcessorCount() > 1)
192 pOsVerInfo->fSmp = true;
193 else if ( g_pfnrtKeQueryActiveProcessors
194 && g_pfnrtKeQueryActiveProcessors() > 1)
195 pOsVerInfo->fSmp = true;
196 else if (KeNumberProcessors > 1)
197 pOsVerInfo->fSmp = true;
198 }
199}
200
201
202/**
203 * Tries a set against the current kernel.
204 *
205 * @retval true if it matched up, global variables are updated.
206 * @retval false otherwise (no globals updated).
207 * @param pSet The data set.
208 * @param pbPrcb Pointer to the processor control block.
209 * @param pszVendor Pointer to the processor vendor string.
210 * @param pOsVerInfo The OS version info.
211 */
212static bool rtR0NtTryMatchSymSet(PCRTNTSDBSET pSet, uint8_t *pbPrcb, const char *pszVendor, PCRTNTSDBOSVER pOsVerInfo)
213{
214 /*
215 * Don't bother trying stuff where the NT kernel version number differs, or
216 * if the build type or SMPness doesn't match up.
217 */
218 if ( pSet->OsVerInfo.uMajorVer != pOsVerInfo->uMajorVer
219 || pSet->OsVerInfo.uMinorVer != pOsVerInfo->uMinorVer
220 || pSet->OsVerInfo.fChecked != pOsVerInfo->fChecked
221 || (!pSet->OsVerInfo.fSmp && pOsVerInfo->fSmp /*must-be-smp*/) )
222 {
223 //DbgPrint("IPRT: #%d Version/type mismatch.\n", pSet - &g_artNtSdbSets[0]);
224 return false;
225 }
226
227 /*
228 * Do the CPU vendor test.
229 *
230 * Note! The MmIsAddressValid call is the real #PF security here as the
231 * __try/__except has limited/no ability to catch everything we need.
232 */
233 char *pszPrcbVendorString = (char *)&pbPrcb[pSet->KPRCB.offVendorString];
234 if (!MmIsAddressValid(&pszPrcbVendorString[4 * 3 - 1]))
235 {
236 //DbgPrint("IPRT: #%d invalid vendor string address.\n", pSet - &g_artNtSdbSets[0]);
237 return false;
238 }
239 __try
240 {
241 if (memcmp(pszPrcbVendorString, pszVendor, RT_MIN(4 * 3, pSet->KPRCB.cbVendorString)) != 0)
242 {
243 //DbgPrint("IPRT: #%d Vendor string mismatch.\n", pSet - &g_artNtSdbSets[0]);
244 return false;
245 }
246 }
247 __except(EXCEPTION_EXECUTE_HANDLER)
248 {
249 DbgPrint("IPRT: %#d Exception\n", pSet - &g_artNtSdbSets[0]);
250 return false;
251 }
252
253 /*
254 * Got a match, update the global variables and report succcess.
255 */
256 g_offrtNtPbQuantumEnd = pSet->KPRCB.offQuantumEnd;
257 g_cbrtNtPbQuantumEnd = pSet->KPRCB.cbQuantumEnd;
258 g_offrtNtPbDpcQueueDepth = pSet->KPRCB.offDpcQueueDepth;
259
260#if 0
261 DbgPrint("IPRT: Using data set #%u for %u.%usp%u build %u %s %s.\n",
262 pSet - &g_artNtSdbSets[0],
263 pSet->OsVerInfo.uMajorVer,
264 pSet->OsVerInfo.uMinorVer,
265 pSet->OsVerInfo.uCsdNo,
266 pSet->OsVerInfo.uBuildNo,
267 pSet->OsVerInfo.fSmp ? "smp" : "uni",
268 pSet->OsVerInfo.fChecked ? "checked" : "free");
269#endif
270 return true;
271}
272
273
274DECLHIDDEN(int) rtR0InitNative(void)
275{
276 /*
277 * Preinitialize g_uRtNtVersion so RTMemAlloc uses the right kind of pool
278 * when RTR0DbgKrnlInfoOpen calls it.
279 */
280 RTNTSDBOSVER OsVerInfo;
281 rtR0NtGetOsVersionInfo(&OsVerInfo);
282 g_uRtNtVersion = RTNT_MAKE_VERSION(OsVerInfo.uMajorVer, OsVerInfo.uMinorVer);
283 g_uRtNtMinorVer = OsVerInfo.uMinorVer;
284 g_uRtNtMajorVer = OsVerInfo.uMajorVer;
285 g_uRtNtBuildNo = OsVerInfo.uBuildNo;
286
287 /*
288 * Initialize the function pointers.
289 */
290 RTDBGKRNLINFO hKrnlInfo;
291 int rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0/*fFlags*/);
292 AssertRCReturn(rc, rc);
293
294#define GET_SYSTEM_ROUTINE_EX(a_Prf, a_Name, a_pfnType) \
295 do { RT_CONCAT3(g_pfnrt, a_Prf, a_Name) = (a_pfnType)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, #a_Name); } while (0)
296#define GET_SYSTEM_ROUTINE(a_Name) GET_SYSTEM_ROUTINE_EX(RT_NOTHING, a_Name, decltype(a_Name) *)
297#define GET_SYSTEM_ROUTINE_PRF(a_Prf,a_Name) GET_SYSTEM_ROUTINE_EX(a_Prf, a_Name, decltype(a_Name) *)
298#define GET_SYSTEM_ROUTINE_TYPE(a_Name, a_pfnType) GET_SYSTEM_ROUTINE_EX(RT_NOTHING, a_Name, a_pfnType)
299
300 GET_SYSTEM_ROUTINE(ExAllocatePoolWithTag);
301 GET_SYSTEM_ROUTINE(ExFreePoolWithTag);
302 GET_SYSTEM_ROUTINE_PRF(Nt,ExSetTimerResolution);
303 GET_SYSTEM_ROUTINE_PRF(Nt,KeFlushQueuedDpcs);
304 GET_SYSTEM_ROUTINE(KeIpiGenericCall);
305 GET_SYSTEM_ROUTINE(KeSetTargetProcessorDpcEx);
306 GET_SYSTEM_ROUTINE(KeInitializeAffinityEx);
307 GET_SYSTEM_ROUTINE(KeAddProcessorAffinityEx);
308 GET_SYSTEM_ROUTINE_TYPE(KeGetProcessorIndexFromNumber, PFNKEGETPROCESSORINDEXFROMNUMBER);
309 GET_SYSTEM_ROUTINE(KeGetProcessorNumberFromIndex);
310 GET_SYSTEM_ROUTINE_TYPE(KeGetCurrentProcessorNumberEx, PFNKEGETCURRENTPROCESSORNUMBEREX);
311 GET_SYSTEM_ROUTINE(KeQueryActiveProcessors);
312 GET_SYSTEM_ROUTINE(KeQueryMaximumProcessorCount);
313 GET_SYSTEM_ROUTINE(KeQueryMaximumProcessorCountEx);
314 GET_SYSTEM_ROUTINE(KeQueryMaximumGroupCount);
315 GET_SYSTEM_ROUTINE(KeQueryActiveProcessorCount);
316 GET_SYSTEM_ROUTINE(KeQueryActiveProcessorCountEx);
317 GET_SYSTEM_ROUTINE(KeQueryLogicalProcessorRelationship);
318 GET_SYSTEM_ROUTINE(KeRegisterProcessorChangeCallback);
319 GET_SYSTEM_ROUTINE(KeDeregisterProcessorChangeCallback);
320 GET_SYSTEM_ROUTINE(KeSetImportanceDpc);
321 GET_SYSTEM_ROUTINE(KeSetTargetProcessorDpc);
322 GET_SYSTEM_ROUTINE(KeInitializeTimerEx);
323 GET_SYSTEM_ROUTINE_TYPE(KeShouldYieldProcessor, PFNKESHOULDYIELDPROCESSOR);
324 GET_SYSTEM_ROUTINE(MmProtectMdlSystemAddress);
325 GET_SYSTEM_ROUTINE(MmAllocatePagesForMdl);
326 GET_SYSTEM_ROUTINE(MmFreePagesFromMdl);
327 GET_SYSTEM_ROUTINE(MmMapLockedPagesSpecifyCache);
328 GET_SYSTEM_ROUTINE(MmAllocateContiguousMemorySpecifyCache);
329 GET_SYSTEM_ROUTINE(MmSecureVirtualMemory);
330 GET_SYSTEM_ROUTINE(MmUnsecureVirtualMemory);
331
332 GET_SYSTEM_ROUTINE_TYPE(RtlGetVersion, PFNRTRTLGETVERSION);
333#ifdef RT_ARCH_X86
334 GET_SYSTEM_ROUTINE(KeQueryInterruptTime);
335#endif
336 GET_SYSTEM_ROUTINE_TYPE(KeQueryInterruptTimePrecise, PFNRTKEQUERYINTERRUPTTIMEPRECISE);
337 GET_SYSTEM_ROUTINE_TYPE(KeQuerySystemTimePrecise, PFNRTKEQUERYSYSTEMTIMEPRECISE);
338
339 g_pfnrtHalRequestIpiW7Plus = (PFNHALREQUESTIPI_W7PLUS)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, "HalRequestIpi");
340 g_pfnrtHalRequestIpiPreW7 = (PFNHALREQUESTIPI_PRE_W7)g_pfnrtHalRequestIpiW7Plus;
341
342 g_puRtMmHighestUserAddress = (uintptr_t const *)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, "MmHighestUserAddress");
343 g_puRtMmSystemRangeStart = (uintptr_t const *)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, "MmSystemRangeStart");
344
345#ifdef RT_ARCH_X86
346 rc = rtR0Nt3InitSymbols(hKrnlInfo);
347 RTR0DbgKrnlInfoRelease(hKrnlInfo);
348 if (RT_FAILURE(rc))
349 return rc;
350#else
351 RTR0DbgKrnlInfoRelease(hKrnlInfo);
352#endif
353
354 /*
355 * Get and publish the definitive NT version.
356 */
357 rtR0NtGetOsVersionInfo(&OsVerInfo);
358 g_uRtNtVersion = RTNT_MAKE_VERSION(OsVerInfo.uMajorVer, OsVerInfo.uMinorVer);
359 g_uRtNtMinorVer = OsVerInfo.uMinorVer;
360 g_uRtNtMajorVer = OsVerInfo.uMajorVer;
361 g_uRtNtBuildNo = OsVerInfo.uBuildNo;
362
363
364 /*
365 * HACK ALERT! (and déjà vu warning - remember win32k.sys on OS/2?)
366 *
367 * Try find _KPRCB::QuantumEnd and _KPRCB::[DpcData.]DpcQueueDepth.
368 * For purpose of verification we use the VendorString member (12+1 chars).
369 *
370 * The offsets was initially derived by poking around with windbg
371 * (dt _KPRCB, !prcb ++, and such like). Systematic harvesting was then
372 * planned using dia2dump, grep and the symbol pack in a manner like this:
373 * dia2dump -type _KDPC_DATA -type _KPRCB EXE\ntkrnlmp.pdb | grep -wE "QuantumEnd|DpcData|DpcQueueDepth|VendorString"
374 *
375 * The final solution ended up using a custom harvester program called
376 * ntBldSymDb that recursively searches thru unpacked symbol packages for
377 * the desired structure offsets. The program assumes that the packages
378 * are unpacked into directories with the same name as the package, with
379 * exception of some of the w2k packages which requires a 'w2k' prefix to
380 * be distinguishable from another.
381 */
382
383 /*
384 * Gather consistent CPU vendor string and PRCB pointers.
385 */
386 KIRQL OldIrql;
387 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); /* make sure we stay on the same cpu */
388
389 union
390 {
391 uint32_t auRegs[4];
392 char szVendor[4*3+1];
393 } u;
394 ASMCpuId(0, &u.auRegs[3], &u.auRegs[0], &u.auRegs[2], &u.auRegs[1]);
395 u.szVendor[4*3] = '\0';
396
397 uint8_t *pbPrcb;
398 __try /* Warning. This try/except statement may provide some false safety. */
399 {
400#if defined(RT_ARCH_X86)
401 PKPCR pPcr = (PKPCR)__readfsdword(RT_UOFFSETOF(KPCR,SelfPcr));
402 pbPrcb = (uint8_t *)pPcr->Prcb;
403#elif defined(RT_ARCH_AMD64)
404 PKPCR pPcr = (PKPCR)__readgsqword(RT_UOFFSETOF(KPCR,Self));
405 pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
406#else
407# error "port me"
408 pbPrcb = NULL;
409#endif
410 }
411 __except(EXCEPTION_EXECUTE_HANDLER)
412 {
413 pbPrcb = NULL;
414 }
415
416 /*
417 * Search the database
418 */
419 if (pbPrcb)
420 {
421 /* Find the best matching kernel version based on build number. */
422 uint32_t iBest = UINT32_MAX;
423 int32_t iBestDelta = INT32_MAX;
424 for (uint32_t i = 0; i < RT_ELEMENTS(g_artNtSdbSets); i++)
425 {
426 if (g_artNtSdbSets[i].OsVerInfo.fChecked != OsVerInfo.fChecked)
427 continue;
428 if (OsVerInfo.fSmp /*must-be-smp*/ && !g_artNtSdbSets[i].OsVerInfo.fSmp)
429 continue;
430
431 int32_t iDelta = RT_ABS((int32_t)OsVerInfo.uBuildNo - (int32_t)g_artNtSdbSets[i].OsVerInfo.uBuildNo);
432 if ( iDelta == 0
433 && (g_artNtSdbSets[i].OsVerInfo.uCsdNo == OsVerInfo.uCsdNo || OsVerInfo.uCsdNo == MY_NIL_CSD))
434 {
435 /* prefect */
436 iBestDelta = iDelta;
437 iBest = i;
438 break;
439 }
440 if ( iDelta < iBestDelta
441 || iBest == UINT32_MAX
442 || ( iDelta == iBestDelta
443 && OsVerInfo.uCsdNo != MY_NIL_CSD
444 && RT_ABS(g_artNtSdbSets[i ].OsVerInfo.uCsdNo - (int32_t)OsVerInfo.uCsdNo)
445 < RT_ABS(g_artNtSdbSets[iBest].OsVerInfo.uCsdNo - (int32_t)OsVerInfo.uCsdNo)
446 )
447 )
448 {
449 iBestDelta = iDelta;
450 iBest = i;
451 }
452 }
453 if (iBest < RT_ELEMENTS(g_artNtSdbSets))
454 {
455 /* Try all sets: iBest -> End; iBest -> Start. */
456 bool fDone = false;
457 int32_t i = iBest;
458 while ( i < RT_ELEMENTS(g_artNtSdbSets)
459 && !(fDone = rtR0NtTryMatchSymSet(&g_artNtSdbSets[i], pbPrcb, u.szVendor, &OsVerInfo)))
460 i++;
461 if (!fDone)
462 {
463 i = (int32_t)iBest - 1;
464 while ( i >= 0
465 && !(fDone = rtR0NtTryMatchSymSet(&g_artNtSdbSets[i], pbPrcb, u.szVendor, &OsVerInfo)))
466 i--;
467 }
468 }
469 else
470 DbgPrint("IPRT: Failed to locate data set.\n");
471 }
472 else
473 DbgPrint("IPRT: Failed to get PCBR pointer.\n");
474
475 KeLowerIrql(OldIrql); /* Lowering the IRQL early in the hope that we may catch exceptions below. */
476
477#ifndef IN_GUEST
478 if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
479 DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found! Kernel %u.%u %u %s\n",
480 OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
481# ifdef DEBUG
482 else
483 DbgPrint("IPRT: _KPRCB:{.QuantumEnd=%x/%d, .DpcQueueDepth=%x/%d} Kernel %u.%u %u %s\n",
484 g_offrtNtPbQuantumEnd, g_cbrtNtPbQuantumEnd, g_offrtNtPbDpcQueueDepth, g_offrtNtPbDpcQueueDepth,
485 OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
486# endif
487#endif
488
489 /*
490 * Initialize multi processor stuff. This registers a callback, so
491 * we call rtR0TermNative to do the deregistration on failure.
492 */
493 rc = rtR0MpNtInit(&OsVerInfo);
494 if (RT_FAILURE(rc))
495 {
496 rtR0TermNative();
497 DbgPrint("IPRT: Fatal: rtR0MpNtInit failed: %d\n", rc);
498 return rc;
499 }
500
501 return VINF_SUCCESS;
502}
503
504
505DECLHIDDEN(void) rtR0TermNative(void)
506{
507 rtR0MpNtTerm();
508}
509
Note: See TracBrowser for help on using the repository browser.

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