1 | /* $Id: initterm-r0drv-nt.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Initialization & Termination, R0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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. */
|
---|
46 | decltype(ExAllocatePoolWithTag) *g_pfnrtExAllocatePoolWithTag;
|
---|
47 | /** ExFreePoolWithTag, introduced in W2K. */
|
---|
48 | decltype(ExFreePoolWithTag) *g_pfnrtExFreePoolWithTag;
|
---|
49 | /** ExSetTimerResolution, introduced in W2K. */
|
---|
50 | PFNMYEXSETTIMERRESOLUTION g_pfnrtNtExSetTimerResolution;
|
---|
51 | /** KeFlushQueuedDpcs, introduced in XP. */
|
---|
52 | PFNMYKEFLUSHQUEUEDDPCS g_pfnrtNtKeFlushQueuedDpcs;
|
---|
53 | /** HalRequestIpi, version introduced with windows 7. */
|
---|
54 | PFNHALREQUESTIPI_W7PLUS g_pfnrtHalRequestIpiW7Plus;
|
---|
55 | /** HalRequestIpi, version valid up to windows vista?? */
|
---|
56 | PFNHALREQUESTIPI_PRE_W7 g_pfnrtHalRequestIpiPreW7;
|
---|
57 | /** Worker for RTMpPokeCpu. */
|
---|
58 | PFNRTSENDIPI g_pfnrtMpPokeCpuWorker;
|
---|
59 | /** KeIpiGenericCall - Introduced in Windows Server 2003. */
|
---|
60 | PFNRTKEIPIGENERICCALL g_pfnrtKeIpiGenericCall;
|
---|
61 | /** KeSetTargetProcessorDpcEx - Introduced in Windows 7. */
|
---|
62 | PFNKESETTARGETPROCESSORDPCEX g_pfnrtKeSetTargetProcessorDpcEx;
|
---|
63 | /** KeInitializeAffinityEx - Introducted in Windows 7. */
|
---|
64 | PFNKEINITIALIZEAFFINITYEX g_pfnrtKeInitializeAffinityEx;
|
---|
65 | /** KeAddProcessorAffinityEx - Introducted in Windows 7. */
|
---|
66 | PFNKEADDPROCESSORAFFINITYEX g_pfnrtKeAddProcessorAffinityEx;
|
---|
67 | /** KeGetProcessorIndexFromNumber - Introducted in Windows 7. */
|
---|
68 | PFNKEGETPROCESSORINDEXFROMNUMBER g_pfnrtKeGetProcessorIndexFromNumber;
|
---|
69 | /** KeGetProcessorNumberFromIndex - Introducted in Windows 7. */
|
---|
70 | PFNKEGETPROCESSORNUMBERFROMINDEX g_pfnrtKeGetProcessorNumberFromIndex;
|
---|
71 | /** KeGetCurrentProcessorNumberEx - Introducted in Windows 7. */
|
---|
72 | PFNKEGETCURRENTPROCESSORNUMBEREX g_pfnrtKeGetCurrentProcessorNumberEx;
|
---|
73 | /** KeQueryActiveProcessors - Introducted in Windows 2000. */
|
---|
74 | PFNKEQUERYACTIVEPROCESSORS g_pfnrtKeQueryActiveProcessors;
|
---|
75 | /** KeQueryMaximumProcessorCount - Introducted in Vista and obsoleted W7. */
|
---|
76 | PFNKEQUERYMAXIMUMPROCESSORCOUNT g_pfnrtKeQueryMaximumProcessorCount;
|
---|
77 | /** KeQueryMaximumProcessorCountEx - Introducted in Windows 7. */
|
---|
78 | PFNKEQUERYMAXIMUMPROCESSORCOUNTEX g_pfnrtKeQueryMaximumProcessorCountEx;
|
---|
79 | /** KeQueryMaximumGroupCount - Introducted in Windows 7. */
|
---|
80 | PFNKEQUERYMAXIMUMGROUPCOUNT g_pfnrtKeQueryMaximumGroupCount;
|
---|
81 | /** KeQueryActiveProcessorCount - Introducted in Vista and obsoleted W7. */
|
---|
82 | PFNKEQUERYACTIVEPROCESSORCOUNT g_pfnrtKeQueryActiveProcessorCount;
|
---|
83 | /** KeQueryActiveProcessorCountEx - Introducted in Windows 7. */
|
---|
84 | PFNKEQUERYACTIVEPROCESSORCOUNTEX g_pfnrtKeQueryActiveProcessorCountEx;
|
---|
85 | /** KeQueryLogicalProcessorRelationship - Introducted in Windows 7. */
|
---|
86 | PFNKEQUERYLOGICALPROCESSORRELATIONSHIP g_pfnrtKeQueryLogicalProcessorRelationship;
|
---|
87 | /** KeRegisterProcessorChangeCallback - Introducted in Windows 7. */
|
---|
88 | PFNKEREGISTERPROCESSORCHANGECALLBACK g_pfnrtKeRegisterProcessorChangeCallback;
|
---|
89 | /** KeDeregisterProcessorChangeCallback - Introducted in Windows 7. */
|
---|
90 | PFNKEDEREGISTERPROCESSORCHANGECALLBACK g_pfnrtKeDeregisterProcessorChangeCallback;
|
---|
91 | /** KeSetImportanceDpc - Introducted in NT 3.51. */
|
---|
92 | decltype(KeSetImportanceDpc) *g_pfnrtKeSetImportanceDpc;
|
---|
93 | /** KeSetTargetProcessorDpc - Introducted in NT 3.51. */
|
---|
94 | decltype(KeSetTargetProcessorDpc) *g_pfnrtKeSetTargetProcessorDpc;
|
---|
95 | /** KeInitializeTimerEx - Introduced in NT 4. */
|
---|
96 | decltype(KeInitializeTimerEx) *g_pfnrtKeInitializeTimerEx;
|
---|
97 | /** KeShouldYieldProcessor - Introduced in Windows 10. */
|
---|
98 | PFNKESHOULDYIELDPROCESSOR g_pfnrtKeShouldYieldProcessor;
|
---|
99 | /** Pointer to the MmProtectMdlSystemAddress kernel function if it's available.
|
---|
100 | * This API was introduced in XP. */
|
---|
101 | decltype(MmProtectMdlSystemAddress) *g_pfnrtMmProtectMdlSystemAddress;
|
---|
102 | /** MmAllocatePagesForMdl - Introduced in Windows 2000. */
|
---|
103 | decltype(MmAllocatePagesForMdl) *g_pfnrtMmAllocatePagesForMdl;
|
---|
104 | /** MmFreePagesFromMdl - Introduced in Windows 2000. */
|
---|
105 | decltype(MmFreePagesFromMdl) *g_pfnrtMmFreePagesFromMdl;
|
---|
106 | /** MmMapLockedPagesSpecifyCache - Introduced in Windows NT4 SP4. */
|
---|
107 | decltype(MmMapLockedPagesSpecifyCache) *g_pfnrtMmMapLockedPagesSpecifyCache;
|
---|
108 | /** MmAllocateContiguousMemorySpecifyCache - Introduced in Windows 2000. */
|
---|
109 | decltype(MmAllocateContiguousMemorySpecifyCache) *g_pfnrtMmAllocateContiguousMemorySpecifyCache;
|
---|
110 | /** MmSecureVirtualMemory - Introduced in NT 3.51. */
|
---|
111 | decltype(MmSecureVirtualMemory) *g_pfnrtMmSecureVirtualMemory;
|
---|
112 | /** MmUnsecureVirtualMemory - Introduced in NT 3.51. */
|
---|
113 | decltype(MmUnsecureVirtualMemory) *g_pfnrtMmUnsecureVirtualMemory;
|
---|
114 | /** RtlGetVersion, introduced in ??. */
|
---|
115 | PFNRTRTLGETVERSION g_pfnrtRtlGetVersion;
|
---|
116 | #ifdef RT_ARCH_X86
|
---|
117 | /** KeQueryInterruptTime - exported/new in Windows 2000. */
|
---|
118 | PFNRTKEQUERYINTERRUPTTIME g_pfnrtKeQueryInterruptTime;
|
---|
119 | #endif
|
---|
120 | /** KeQueryInterruptTimePrecise - new in Windows 8. */
|
---|
121 | PFNRTKEQUERYINTERRUPTTIMEPRECISE g_pfnrtKeQueryInterruptTimePrecise;
|
---|
122 | /** KeQuerySystemTimePrecise - new in Windows 8. */
|
---|
123 | PFNRTKEQUERYSYSTEMTIMEPRECISE g_pfnrtKeQuerySystemTimePrecise;
|
---|
124 |
|
---|
125 | /** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
|
---|
126 | uint32_t g_offrtNtPbQuantumEnd;
|
---|
127 | /** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
|
---|
128 | uint32_t g_cbrtNtPbQuantumEnd;
|
---|
129 | /** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
|
---|
130 | uint32_t g_offrtNtPbDpcQueueDepth;
|
---|
131 |
|
---|
132 | /** The combined NT version, see RTNT_MAKE_VERSION. */
|
---|
133 | uint32_t g_uRtNtVersion = RTNT_MAKE_VERSION(4, 0);
|
---|
134 | /** The major version number. */
|
---|
135 | uint8_t g_uRtNtMajorVer;
|
---|
136 | /** The minor version number. */
|
---|
137 | uint8_t g_uRtNtMinorVer;
|
---|
138 | /** The build number. */
|
---|
139 | uint32_t g_uRtNtBuildNo;
|
---|
140 |
|
---|
141 | /** Pointer to the MmHighestUserAddress kernel variable - can be NULL. */
|
---|
142 | uintptr_t const *g_puRtMmHighestUserAddress;
|
---|
143 | /** Pointer to the MmSystemRangeStart kernel variable - can be NULL. */
|
---|
144 | uintptr_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 | */
|
---|
155 | static 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 | */
|
---|
210 | static 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 |
|
---|
272 | DECLHIDDEN(int) rtR0InitNative(void)
|
---|
273 | {
|
---|
274 | /*
|
---|
275 | * Preinitialize g_uRtNtVersion so RTMemAlloc uses the right kind of pool
|
---|
276 | * when RTR0DbgKrnlInfoOpen calls it.
|
---|
277 | */
|
---|
278 | RTNTSDBOSVER OsVerInfo;
|
---|
279 | rtR0NtGetOsVersionInfo(&OsVerInfo);
|
---|
280 | g_uRtNtVersion = RTNT_MAKE_VERSION(OsVerInfo.uMajorVer, OsVerInfo.uMinorVer);
|
---|
281 | g_uRtNtMinorVer = OsVerInfo.uMinorVer;
|
---|
282 | g_uRtNtMajorVer = OsVerInfo.uMajorVer;
|
---|
283 | g_uRtNtBuildNo = OsVerInfo.uBuildNo;
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Initialize the function pointers.
|
---|
287 | */
|
---|
288 | RTDBGKRNLINFO hKrnlInfo;
|
---|
289 | int rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0/*fFlags*/);
|
---|
290 | AssertRCReturn(rc, rc);
|
---|
291 |
|
---|
292 | #define GET_SYSTEM_ROUTINE_EX(a_Prf, a_Name, a_pfnType) \
|
---|
293 | do { RT_CONCAT3(g_pfnrt, a_Prf, a_Name) = (a_pfnType)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, #a_Name); } while (0)
|
---|
294 | #define GET_SYSTEM_ROUTINE(a_Name) GET_SYSTEM_ROUTINE_EX(RT_NOTHING, a_Name, decltype(a_Name) *)
|
---|
295 | #define GET_SYSTEM_ROUTINE_PRF(a_Prf,a_Name) GET_SYSTEM_ROUTINE_EX(a_Prf, a_Name, decltype(a_Name) *)
|
---|
296 | #define GET_SYSTEM_ROUTINE_TYPE(a_Name, a_pfnType) GET_SYSTEM_ROUTINE_EX(RT_NOTHING, a_Name, a_pfnType)
|
---|
297 |
|
---|
298 | GET_SYSTEM_ROUTINE(ExAllocatePoolWithTag);
|
---|
299 | GET_SYSTEM_ROUTINE(ExFreePoolWithTag);
|
---|
300 | GET_SYSTEM_ROUTINE_PRF(Nt,ExSetTimerResolution);
|
---|
301 | GET_SYSTEM_ROUTINE_PRF(Nt,KeFlushQueuedDpcs);
|
---|
302 | GET_SYSTEM_ROUTINE(KeIpiGenericCall);
|
---|
303 | GET_SYSTEM_ROUTINE(KeSetTargetProcessorDpcEx);
|
---|
304 | GET_SYSTEM_ROUTINE(KeInitializeAffinityEx);
|
---|
305 | GET_SYSTEM_ROUTINE(KeAddProcessorAffinityEx);
|
---|
306 | GET_SYSTEM_ROUTINE_TYPE(KeGetProcessorIndexFromNumber, PFNKEGETPROCESSORINDEXFROMNUMBER);
|
---|
307 | GET_SYSTEM_ROUTINE(KeGetProcessorNumberFromIndex);
|
---|
308 | GET_SYSTEM_ROUTINE_TYPE(KeGetCurrentProcessorNumberEx, PFNKEGETCURRENTPROCESSORNUMBEREX);
|
---|
309 | GET_SYSTEM_ROUTINE(KeQueryActiveProcessors);
|
---|
310 | GET_SYSTEM_ROUTINE(KeQueryMaximumProcessorCount);
|
---|
311 | GET_SYSTEM_ROUTINE(KeQueryMaximumProcessorCountEx);
|
---|
312 | GET_SYSTEM_ROUTINE(KeQueryMaximumGroupCount);
|
---|
313 | GET_SYSTEM_ROUTINE(KeQueryActiveProcessorCount);
|
---|
314 | GET_SYSTEM_ROUTINE(KeQueryActiveProcessorCountEx);
|
---|
315 | GET_SYSTEM_ROUTINE(KeQueryLogicalProcessorRelationship);
|
---|
316 | GET_SYSTEM_ROUTINE(KeRegisterProcessorChangeCallback);
|
---|
317 | GET_SYSTEM_ROUTINE(KeDeregisterProcessorChangeCallback);
|
---|
318 | GET_SYSTEM_ROUTINE(KeSetImportanceDpc);
|
---|
319 | GET_SYSTEM_ROUTINE(KeSetTargetProcessorDpc);
|
---|
320 | GET_SYSTEM_ROUTINE(KeInitializeTimerEx);
|
---|
321 | GET_SYSTEM_ROUTINE_TYPE(KeShouldYieldProcessor, PFNKESHOULDYIELDPROCESSOR);
|
---|
322 | GET_SYSTEM_ROUTINE(MmProtectMdlSystemAddress);
|
---|
323 | GET_SYSTEM_ROUTINE(MmAllocatePagesForMdl);
|
---|
324 | GET_SYSTEM_ROUTINE(MmFreePagesFromMdl);
|
---|
325 | GET_SYSTEM_ROUTINE(MmMapLockedPagesSpecifyCache);
|
---|
326 | GET_SYSTEM_ROUTINE(MmAllocateContiguousMemorySpecifyCache);
|
---|
327 | GET_SYSTEM_ROUTINE(MmSecureVirtualMemory);
|
---|
328 | GET_SYSTEM_ROUTINE(MmUnsecureVirtualMemory);
|
---|
329 |
|
---|
330 | GET_SYSTEM_ROUTINE_TYPE(RtlGetVersion, PFNRTRTLGETVERSION);
|
---|
331 | #ifdef RT_ARCH_X86
|
---|
332 | GET_SYSTEM_ROUTINE(KeQueryInterruptTime);
|
---|
333 | #endif
|
---|
334 | GET_SYSTEM_ROUTINE_TYPE(KeQueryInterruptTimePrecise, PFNRTKEQUERYINTERRUPTTIMEPRECISE);
|
---|
335 | GET_SYSTEM_ROUTINE_TYPE(KeQuerySystemTimePrecise, PFNRTKEQUERYSYSTEMTIMEPRECISE);
|
---|
336 |
|
---|
337 | g_pfnrtHalRequestIpiW7Plus = (PFNHALREQUESTIPI_W7PLUS)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, "HalRequestIpi");
|
---|
338 | g_pfnrtHalRequestIpiPreW7 = (PFNHALREQUESTIPI_PRE_W7)g_pfnrtHalRequestIpiW7Plus;
|
---|
339 |
|
---|
340 | g_puRtMmHighestUserAddress = (uintptr_t const *)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, "MmHighestUserAddress");
|
---|
341 | g_puRtMmSystemRangeStart = (uintptr_t const *)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, "MmSystemRangeStart");
|
---|
342 |
|
---|
343 | #ifdef RT_ARCH_X86
|
---|
344 | rc = rtR0Nt3InitSymbols(hKrnlInfo);
|
---|
345 | RTR0DbgKrnlInfoRelease(hKrnlInfo);
|
---|
346 | if (RT_FAILURE(rc))
|
---|
347 | return rc;
|
---|
348 | #else
|
---|
349 | RTR0DbgKrnlInfoRelease(hKrnlInfo);
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Get and publish the definitive NT version.
|
---|
354 | */
|
---|
355 | rtR0NtGetOsVersionInfo(&OsVerInfo);
|
---|
356 | g_uRtNtVersion = RTNT_MAKE_VERSION(OsVerInfo.uMajorVer, OsVerInfo.uMinorVer);
|
---|
357 | g_uRtNtMinorVer = OsVerInfo.uMinorVer;
|
---|
358 | g_uRtNtMajorVer = OsVerInfo.uMajorVer;
|
---|
359 | g_uRtNtBuildNo = OsVerInfo.uBuildNo;
|
---|
360 |
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * HACK ALERT! (and déjà vu warning - remember win32k.sys on OS/2?)
|
---|
364 | *
|
---|
365 | * Try find _KPRCB::QuantumEnd and _KPRCB::[DpcData.]DpcQueueDepth.
|
---|
366 | * For purpose of verification we use the VendorString member (12+1 chars).
|
---|
367 | *
|
---|
368 | * The offsets was initially derived by poking around with windbg
|
---|
369 | * (dt _KPRCB, !prcb ++, and such like). Systematic harvesting was then
|
---|
370 | * planned using dia2dump, grep and the symbol pack in a manner like this:
|
---|
371 | * dia2dump -type _KDPC_DATA -type _KPRCB EXE\ntkrnlmp.pdb | grep -wE "QuantumEnd|DpcData|DpcQueueDepth|VendorString"
|
---|
372 | *
|
---|
373 | * The final solution ended up using a custom harvester program called
|
---|
374 | * ntBldSymDb that recursively searches thru unpacked symbol packages for
|
---|
375 | * the desired structure offsets. The program assumes that the packages
|
---|
376 | * are unpacked into directories with the same name as the package, with
|
---|
377 | * exception of some of the w2k packages which requires a 'w2k' prefix to
|
---|
378 | * be distinguishable from another.
|
---|
379 | */
|
---|
380 |
|
---|
381 | /*
|
---|
382 | * Gather consistent CPU vendor string and PRCB pointers.
|
---|
383 | */
|
---|
384 | KIRQL OldIrql;
|
---|
385 | KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); /* make sure we stay on the same cpu */
|
---|
386 |
|
---|
387 | union
|
---|
388 | {
|
---|
389 | uint32_t auRegs[4];
|
---|
390 | char szVendor[4*3+1];
|
---|
391 | } u;
|
---|
392 | ASMCpuId(0, &u.auRegs[3], &u.auRegs[0], &u.auRegs[2], &u.auRegs[1]);
|
---|
393 | u.szVendor[4*3] = '\0';
|
---|
394 |
|
---|
395 | uint8_t *pbPrcb;
|
---|
396 | __try /* Warning. This try/except statement may provide some false safety. */
|
---|
397 | {
|
---|
398 | #if defined(RT_ARCH_X86)
|
---|
399 | PKPCR pPcr = (PKPCR)__readfsdword(RT_UOFFSETOF(KPCR,SelfPcr));
|
---|
400 | pbPrcb = (uint8_t *)pPcr->Prcb;
|
---|
401 | #elif defined(RT_ARCH_AMD64)
|
---|
402 | PKPCR pPcr = (PKPCR)__readgsqword(RT_UOFFSETOF(KPCR,Self));
|
---|
403 | pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
|
---|
404 | #else
|
---|
405 | # error "port me"
|
---|
406 | pbPrcb = NULL;
|
---|
407 | #endif
|
---|
408 | }
|
---|
409 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
410 | {
|
---|
411 | pbPrcb = NULL;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /*
|
---|
415 | * Search the database
|
---|
416 | */
|
---|
417 | if (pbPrcb)
|
---|
418 | {
|
---|
419 | /* Find the best matching kernel version based on build number. */
|
---|
420 | uint32_t iBest = UINT32_MAX;
|
---|
421 | int32_t iBestDelta = INT32_MAX;
|
---|
422 | for (uint32_t i = 0; i < RT_ELEMENTS(g_artNtSdbSets); i++)
|
---|
423 | {
|
---|
424 | if (g_artNtSdbSets[i].OsVerInfo.fChecked != OsVerInfo.fChecked)
|
---|
425 | continue;
|
---|
426 | if (OsVerInfo.fSmp /*must-be-smp*/ && !g_artNtSdbSets[i].OsVerInfo.fSmp)
|
---|
427 | continue;
|
---|
428 |
|
---|
429 | int32_t iDelta = RT_ABS((int32_t)OsVerInfo.uBuildNo - (int32_t)g_artNtSdbSets[i].OsVerInfo.uBuildNo);
|
---|
430 | if ( iDelta == 0
|
---|
431 | && (g_artNtSdbSets[i].OsVerInfo.uCsdNo == OsVerInfo.uCsdNo || OsVerInfo.uCsdNo == MY_NIL_CSD))
|
---|
432 | {
|
---|
433 | /* prefect */
|
---|
434 | iBestDelta = iDelta;
|
---|
435 | iBest = i;
|
---|
436 | break;
|
---|
437 | }
|
---|
438 | if ( iDelta < iBestDelta
|
---|
439 | || iBest == UINT32_MAX
|
---|
440 | || ( iDelta == iBestDelta
|
---|
441 | && OsVerInfo.uCsdNo != MY_NIL_CSD
|
---|
442 | && RT_ABS(g_artNtSdbSets[i ].OsVerInfo.uCsdNo - (int32_t)OsVerInfo.uCsdNo)
|
---|
443 | < RT_ABS(g_artNtSdbSets[iBest].OsVerInfo.uCsdNo - (int32_t)OsVerInfo.uCsdNo)
|
---|
444 | )
|
---|
445 | )
|
---|
446 | {
|
---|
447 | iBestDelta = iDelta;
|
---|
448 | iBest = i;
|
---|
449 | }
|
---|
450 | }
|
---|
451 | if (iBest < RT_ELEMENTS(g_artNtSdbSets))
|
---|
452 | {
|
---|
453 | /* Try all sets: iBest -> End; iBest -> Start. */
|
---|
454 | bool fDone = false;
|
---|
455 | int32_t i = iBest;
|
---|
456 | while ( i < RT_ELEMENTS(g_artNtSdbSets)
|
---|
457 | && !(fDone = rtR0NtTryMatchSymSet(&g_artNtSdbSets[i], pbPrcb, u.szVendor, &OsVerInfo)))
|
---|
458 | i++;
|
---|
459 | if (!fDone)
|
---|
460 | {
|
---|
461 | i = (int32_t)iBest - 1;
|
---|
462 | while ( i >= 0
|
---|
463 | && !(fDone = rtR0NtTryMatchSymSet(&g_artNtSdbSets[i], pbPrcb, u.szVendor, &OsVerInfo)))
|
---|
464 | i--;
|
---|
465 | }
|
---|
466 | }
|
---|
467 | else
|
---|
468 | DbgPrint("IPRT: Failed to locate data set.\n");
|
---|
469 | }
|
---|
470 | else
|
---|
471 | DbgPrint("IPRT: Failed to get PCBR pointer.\n");
|
---|
472 |
|
---|
473 | KeLowerIrql(OldIrql); /* Lowering the IRQL early in the hope that we may catch exceptions below. */
|
---|
474 |
|
---|
475 | #ifndef IN_GUEST
|
---|
476 | if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
|
---|
477 | DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found! Kernel %u.%u %u %s\n",
|
---|
478 | OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
|
---|
479 | # ifdef DEBUG
|
---|
480 | else
|
---|
481 | DbgPrint("IPRT: _KPRCB:{.QuantumEnd=%x/%d, .DpcQueueDepth=%x/%d} Kernel %u.%u %u %s\n",
|
---|
482 | g_offrtNtPbQuantumEnd, g_cbrtNtPbQuantumEnd, g_offrtNtPbDpcQueueDepth, g_offrtNtPbDpcQueueDepth,
|
---|
483 | OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
|
---|
484 | # endif
|
---|
485 | #endif
|
---|
486 |
|
---|
487 | /*
|
---|
488 | * Initialize multi processor stuff. This registers a callback, so
|
---|
489 | * we call rtR0TermNative to do the deregistration on failure.
|
---|
490 | */
|
---|
491 | rc = rtR0MpNtInit(&OsVerInfo);
|
---|
492 | if (RT_FAILURE(rc))
|
---|
493 | {
|
---|
494 | rtR0TermNative();
|
---|
495 | DbgPrint("IPRT: Fatal: rtR0MpNtInit failed: %d\n", rc);
|
---|
496 | return rc;
|
---|
497 | }
|
---|
498 |
|
---|
499 | return VINF_SUCCESS;
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | DECLHIDDEN(void) rtR0TermNative(void)
|
---|
504 | {
|
---|
505 | rtR0MpNtTerm();
|
---|
506 | }
|
---|
507 |
|
---|