VirtualBox

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

Last change on this file since 74795 was 73097, checked in by vboxsync, 7 years ago

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

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