1 | /* $Id: initterm-r0drv-nt.cpp 36555 2011-04-05 12:34:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Initialization & Termination, R0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include "the-nt-kernel.h"
|
---|
31 | #include <iprt/asm-amd64-x86.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/mp.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include "internal/initterm.h"
|
---|
37 | #include "internal-r0drv-nt.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *******************************************************************************/
|
---|
43 | /** The Nt CPU set.
|
---|
44 | * KeQueryActiveProcssors() cannot be called at all IRQLs and therefore we'll
|
---|
45 | * have to cache it. Fortunately, Nt doesn't really support taking CPUs offline
|
---|
46 | * or online. It's first with W2K8 that support for CPU hotplugging was added.
|
---|
47 | * Once we start caring about this, we'll simply let the native MP event callback
|
---|
48 | * and update this variable as CPUs comes online. (The code is done already.)
|
---|
49 | */
|
---|
50 | RTCPUSET g_rtMpNtCpuSet;
|
---|
51 |
|
---|
52 | /** ExSetTimerResolution, introduced in W2K. */
|
---|
53 | PFNMYEXSETTIMERRESOLUTION g_pfnrtNtExSetTimerResolution;
|
---|
54 | /** KeFlushQueuedDpcs, introduced in XP. */
|
---|
55 | PFNMYKEFLUSHQUEUEDDPCS g_pfnrtNtKeFlushQueuedDpcs;
|
---|
56 | /** HalRequestIpi, introduced in ??. */
|
---|
57 | PFNHALREQUESTIPI g_pfnrtNtHalRequestIpi;
|
---|
58 | /** HalSendSoftwareInterrupt */
|
---|
59 | PFNHALSENDSOFTWAREINTERRUPT g_pfnrtNtHalSendSoftwareInterrupt;
|
---|
60 | /** SendIpi handler based on Windows version */
|
---|
61 | PFNRTSENDIPI g_pfnrtSendIpi;
|
---|
62 | /** KeIpiGenericCall - Windows Server 2003+ only */
|
---|
63 | PFNRTKEIPIGENERICCALL g_pfnrtKeIpiGenericCall;
|
---|
64 |
|
---|
65 | /** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
|
---|
66 | uint32_t g_offrtNtPbQuantumEnd;
|
---|
67 | /** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
|
---|
68 | uint32_t g_cbrtNtPbQuantumEnd;
|
---|
69 | /** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
|
---|
70 | uint32_t g_offrtNtPbDpcQueueDepth;
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | DECLHIDDEN(int) rtR0InitNative(void)
|
---|
75 | {
|
---|
76 | /*
|
---|
77 | * Init the Nt cpu set.
|
---|
78 | */
|
---|
79 | #ifdef IPRT_TARGET_NT4
|
---|
80 | KAFFINITY ActiveProcessors = (UINT64_C(1) << KeNumberProcessors) - UINT64_C(1);
|
---|
81 | #else
|
---|
82 | KAFFINITY ActiveProcessors = KeQueryActiveProcessors();
|
---|
83 | #endif
|
---|
84 | RTCpuSetEmpty(&g_rtMpNtCpuSet);
|
---|
85 | RTCpuSetFromU64(&g_rtMpNtCpuSet, ActiveProcessors);
|
---|
86 | /** @todo Port to W2K8 with > 64 cpus/threads. */
|
---|
87 |
|
---|
88 | #ifdef IPRT_TARGET_NT4
|
---|
89 | g_pfnrtNtExSetTimerResolution = NULL;
|
---|
90 | g_pfnrtNtKeFlushQueuedDpcs = NULL;
|
---|
91 | g_pfnrtNtHalRequestIpi = NULL;
|
---|
92 | g_pfnrtNtHalSendSoftwareInterrupt = NULL;
|
---|
93 | g_pfnrtKeIpiGenericCall = NULL;
|
---|
94 | #else
|
---|
95 | /*
|
---|
96 | * Initialize the function pointers.
|
---|
97 | */
|
---|
98 | UNICODE_STRING RoutineName;
|
---|
99 | RtlInitUnicodeString(&RoutineName, L"ExSetTimerResolution");
|
---|
100 | g_pfnrtNtExSetTimerResolution = (PFNMYEXSETTIMERRESOLUTION)MmGetSystemRoutineAddress(&RoutineName);
|
---|
101 |
|
---|
102 | RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
|
---|
103 | g_pfnrtNtKeFlushQueuedDpcs = (PFNMYKEFLUSHQUEUEDDPCS)MmGetSystemRoutineAddress(&RoutineName);
|
---|
104 |
|
---|
105 | RtlInitUnicodeString(&RoutineName, L"HalRequestIpi");
|
---|
106 | g_pfnrtNtHalRequestIpi = (PFNHALREQUESTIPI)MmGetSystemRoutineAddress(&RoutineName);
|
---|
107 |
|
---|
108 | RtlInitUnicodeString(&RoutineName, L"HalSendSoftwareInterrupt");
|
---|
109 | g_pfnrtNtHalSendSoftwareInterrupt = (PFNHALSENDSOFTWAREINTERRUPT)MmGetSystemRoutineAddress(&RoutineName);
|
---|
110 |
|
---|
111 | RtlInitUnicodeString(&RoutineName, L"KeIpiGenericCall");
|
---|
112 | g_pfnrtKeIpiGenericCall = (PFNRTKEIPIGENERICCALL)MmGetSystemRoutineAddress(&RoutineName);
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Get some info that might come in handy below.
|
---|
117 | */
|
---|
118 | ULONG MajorVersion = 0;
|
---|
119 | ULONG MinorVersion = 0;
|
---|
120 | ULONG BuildNumber = 0;
|
---|
121 | BOOLEAN fChecked = PsGetVersion(&MajorVersion, &MinorVersion, &BuildNumber, NULL);
|
---|
122 |
|
---|
123 | g_pfnrtSendIpi = rtMpSendIpiDummy;
|
---|
124 | #ifndef IPRT_TARGET_NT4
|
---|
125 | if ( g_pfnrtNtHalRequestIpi
|
---|
126 | && MajorVersion == 6
|
---|
127 | && MinorVersion == 0)
|
---|
128 | {
|
---|
129 | /* Vista or Windows Server 2008 */
|
---|
130 | g_pfnrtSendIpi = rtMpSendIpiVista;
|
---|
131 | }
|
---|
132 | else
|
---|
133 | if ( g_pfnrtNtHalSendSoftwareInterrupt
|
---|
134 | && MajorVersion == 6
|
---|
135 | && MinorVersion == 1)
|
---|
136 | {
|
---|
137 | /* Windows 7 or Windows Server 2008 R2 */
|
---|
138 | g_pfnrtSendIpi = rtMpSendIpiWin7;
|
---|
139 | }
|
---|
140 | /* Windows XP should send always send an IPI -> VERIFY */
|
---|
141 | #endif
|
---|
142 | KIRQL OldIrql;
|
---|
143 | KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); /* make sure we stay on the same cpu */
|
---|
144 |
|
---|
145 | union
|
---|
146 | {
|
---|
147 | uint32_t auRegs[4];
|
---|
148 | char szVendor[4*3+1];
|
---|
149 | } u;
|
---|
150 | ASMCpuId(0, &u.auRegs[3], &u.auRegs[0], &u.auRegs[2], &u.auRegs[1]);
|
---|
151 | u.szVendor[4*3] = '\0';
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * HACK ALERT (and déjà vu warning)!
|
---|
155 | *
|
---|
156 | * Try find _KPRCB::QuantumEnd and _KPRCB::[DpcData.]DpcQueueDepth.
|
---|
157 | * For purpose of verification we use the VendorString member (12+1 chars).
|
---|
158 | *
|
---|
159 | * The offsets was initially derived by poking around with windbg
|
---|
160 | * (dt _KPRCB, !prcb ++, and such like). Systematic harvesting is now done
|
---|
161 | * by means of dia2dump, grep and the symbol packs. Typically:
|
---|
162 | * dia2dump -type _KDPC_DATA -type _KPRCB EXE\ntkrnlmp.pdb | grep -wE "QuantumEnd|DpcData|DpcQueueDepth|VendorString"
|
---|
163 | */
|
---|
164 | /** @todo array w/ data + script for extracting a row. (save space + readability; table will be short.) */
|
---|
165 | __try
|
---|
166 | {
|
---|
167 | #if defined(RT_ARCH_X86)
|
---|
168 | PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
|
---|
169 | uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
|
---|
170 |
|
---|
171 | if ( BuildNumber == 2600 /* XP SP2 */
|
---|
172 | && !memcmp(&pbPrcb[0x900], &u.szVendor[0], 4*3))
|
---|
173 | {
|
---|
174 | g_offrtNtPbQuantumEnd = 0x88c;
|
---|
175 | g_cbrtNtPbQuantumEnd = 4;
|
---|
176 | g_offrtNtPbDpcQueueDepth = 0x870;
|
---|
177 | }
|
---|
178 | /* WindowsVista.6002.090410-1830.x86fre.Symbols.exe
|
---|
179 | WindowsVista.6002.090410-1830.x86chk.Symbols.exe
|
---|
180 | WindowsVista.6002.090130-1715.x86fre.Symbols.exe
|
---|
181 | WindowsVista.6002.090130-1715.x86chk.Symbols.exe */
|
---|
182 | else if ( BuildNumber == 6002
|
---|
183 | && !memcmp(&pbPrcb[0x1c2c], &u.szVendor[0], 4*3))
|
---|
184 | {
|
---|
185 | g_offrtNtPbQuantumEnd = 0x1a41;
|
---|
186 | g_cbrtNtPbQuantumEnd = 1;
|
---|
187 | g_offrtNtPbDpcQueueDepth = 0x19e0 + 0xc;
|
---|
188 | }
|
---|
189 | else if ( BuildNumber == 3790 /* Server 2003 SP2 */
|
---|
190 | && !memcmp(&pbPrcb[0xb60], &u.szVendor[0], 4*3))
|
---|
191 | {
|
---|
192 | g_offrtNtPbQuantumEnd = 0x981;
|
---|
193 | g_cbrtNtPbQuantumEnd = 1;
|
---|
194 | g_offrtNtPbDpcQueueDepth = 0x920 + 0xc;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** @todo more */
|
---|
198 | //pbQuantumEnd = (uint8_t volatile *)pPcr->Prcb + 0x1a41;
|
---|
199 |
|
---|
200 | #elif defined(RT_ARCH_AMD64)
|
---|
201 | PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
|
---|
202 | uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
|
---|
203 |
|
---|
204 | if ( BuildNumber == 3790 /* XP64 / W2K3-AMD64 SP1 */
|
---|
205 | && !memcmp(&pbPrcb[0x22b4], &u.szVendor[0], 4*3))
|
---|
206 | {
|
---|
207 | g_offrtNtPbQuantumEnd = 0x1f75;
|
---|
208 | g_cbrtNtPbQuantumEnd = 1;
|
---|
209 | g_offrtNtPbDpcQueueDepth = 0x1f00 + 0x18;
|
---|
210 | }
|
---|
211 | else if ( BuildNumber == 6000 /* Vista/AMD64 */
|
---|
212 | && !memcmp(&pbPrcb[0x38bc], &u.szVendor[0], 4*3))
|
---|
213 | {
|
---|
214 | g_offrtNtPbQuantumEnd = 0x3375;
|
---|
215 | g_cbrtNtPbQuantumEnd = 1;
|
---|
216 | g_offrtNtPbDpcQueueDepth = 0x3300 + 0x18;
|
---|
217 | }
|
---|
218 | /* WindowsVista.6002.090410-1830.amd64fre.Symbols
|
---|
219 | WindowsVista.6002.090130-1715.amd64fre.Symbols
|
---|
220 | WindowsVista.6002.090410-1830.amd64chk.Symbols */
|
---|
221 | else if ( BuildNumber == 6002
|
---|
222 | && !memcmp(&pbPrcb[0x399c], &u.szVendor[0], 4*3))
|
---|
223 | {
|
---|
224 | g_offrtNtPbQuantumEnd = 0x3475;
|
---|
225 | g_cbrtNtPbQuantumEnd = 1;
|
---|
226 | g_offrtNtPbDpcQueueDepth = 0x3400 + 0x18;
|
---|
227 | }
|
---|
228 | /* Windows7.7600.16539.amd64fre.win7_gdr.100226-1909 */
|
---|
229 | else if ( BuildNumber == 7600
|
---|
230 | && !memcmp(&pbPrcb[0x4bb8], &u.szVendor[0], 4*3))
|
---|
231 | {
|
---|
232 | g_offrtNtPbQuantumEnd = 0x21d9;
|
---|
233 | g_cbrtNtPbQuantumEnd = 1;
|
---|
234 | g_offrtNtPbDpcQueueDepth = 0x2180 + 0x18;
|
---|
235 | }
|
---|
236 |
|
---|
237 | #else
|
---|
238 | # error "port me"
|
---|
239 | #endif
|
---|
240 | }
|
---|
241 | __except(EXCEPTION_EXECUTE_HANDLER) /** @todo this handler doesn't seem to work... Because of Irql? */
|
---|
242 | {
|
---|
243 | g_offrtNtPbQuantumEnd = 0;
|
---|
244 | g_cbrtNtPbQuantumEnd = 0;
|
---|
245 | g_offrtNtPbDpcQueueDepth = 0;
|
---|
246 | }
|
---|
247 |
|
---|
248 | KeLowerIrql(OldIrql);
|
---|
249 |
|
---|
250 | #ifndef IN_GUEST /** @todo fix above for all Nt versions. */
|
---|
251 | if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
|
---|
252 | DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found! Kernel %u.%u %u %s\n",
|
---|
253 | MajorVersion, MinorVersion, BuildNumber, fChecked ? "checked" : "free");
|
---|
254 | # ifdef DEBUG
|
---|
255 | else
|
---|
256 | DbgPrint("IPRT: _KPRCB:{.QuantumEnd=%x/%d, .DpcQueueDepth=%x/%d} Kernel %ul.%ul %ul %s\n",
|
---|
257 | g_offrtNtPbQuantumEnd, g_cbrtNtPbQuantumEnd, g_offrtNtPbDpcQueueDepth,
|
---|
258 | MajorVersion, MinorVersion, BuildNumber, fChecked ? "checked" : "free");
|
---|
259 | # endif
|
---|
260 | #endif
|
---|
261 |
|
---|
262 | return VINF_SUCCESS;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | DECLHIDDEN(void) rtR0TermNative(void)
|
---|
267 | {
|
---|
268 | }
|
---|
269 |
|
---|