VirtualBox

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

Last change on this file since 32736 was 30362, checked in by vboxsync, 15 years ago

Exact version

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.1 KB
Line 
1/* $Id: initterm-r0drv-nt.cpp 30362 2010-06-22 11:14:16Z 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 */
50RTCPUSET g_rtMpNtCpuSet;
51
52/** ExSetTimerResolution, introduced in W2K. */
53PFNMYEXSETTIMERRESOLUTION g_pfnrtNtExSetTimerResolution;
54/** KeFlushQueuedDpcs, introduced in XP. */
55PFNMYKEFLUSHQUEUEDDPCS g_pfnrtNtKeFlushQueuedDpcs;
56/** HalRequestIpi, introduced in ??. */
57PFNHALREQUESTIPI g_pfnrtNtHalRequestIpi;
58/** HalSendSoftwareInterrupt */
59PFNHALSENDSOFTWAREINTERRUPT g_pfnrtNtHalSendSoftwareInterrupt;
60/** SendIpi handler based on Windows version */
61PFNRTSENDIPI g_pfnrtSendIpi;
62/** KeIpiGenericCall - Windows Server 2003+ only */
63PFNRTKEIPIGENERICCALL g_pfnrtKeIpiGenericCall;
64
65/** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
66uint32_t g_offrtNtPbQuantumEnd;
67/** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
68uint32_t g_cbrtNtPbQuantumEnd;
69/** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
70uint32_t g_offrtNtPbDpcQueueDepth;
71
72
73
74int 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
87#ifdef IPRT_TARGET_NT4
88 g_pfnrtNtExSetTimerResolution = NULL;
89 g_pfnrtNtKeFlushQueuedDpcs = NULL;
90 g_pfnrtNtHalRequestIpi = NULL;
91 g_pfnrtNtHalSendSoftwareInterrupt = NULL;
92 g_pfnrtKeIpiGenericCall = NULL;
93#else
94 /*
95 * Initialize the function pointers.
96 */
97 UNICODE_STRING RoutineName;
98 RtlInitUnicodeString(&RoutineName, L"ExSetTimerResolution");
99 g_pfnrtNtExSetTimerResolution = (PFNMYEXSETTIMERRESOLUTION)MmGetSystemRoutineAddress(&RoutineName);
100
101 RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
102 g_pfnrtNtKeFlushQueuedDpcs = (PFNMYKEFLUSHQUEUEDDPCS)MmGetSystemRoutineAddress(&RoutineName);
103
104 RtlInitUnicodeString(&RoutineName, L"HalRequestIpi");
105 g_pfnrtNtHalRequestIpi = (PFNHALREQUESTIPI)MmGetSystemRoutineAddress(&RoutineName);
106
107 RtlInitUnicodeString(&RoutineName, L"HalSendSoftwareInterrupt");
108 g_pfnrtNtHalSendSoftwareInterrupt = (PFNHALSENDSOFTWAREINTERRUPT)MmGetSystemRoutineAddress(&RoutineName);
109
110 RtlInitUnicodeString(&RoutineName, L"KeIpiGenericCall");
111 g_pfnrtKeIpiGenericCall = (PFNRTKEIPIGENERICCALL)MmGetSystemRoutineAddress(&RoutineName);
112#endif
113
114 /*
115 * Get some info that might come in handy below.
116 */
117 ULONG MajorVersion = 0;
118 ULONG MinorVersion = 0;
119 ULONG BuildNumber = 0;
120 BOOLEAN fChecked = PsGetVersion(&MajorVersion, &MinorVersion, &BuildNumber, NULL);
121
122 g_pfnrtSendIpi = rtMpSendIpiDummy;
123#ifndef IPRT_TARGET_NT4
124 if ( g_pfnrtNtHalRequestIpi
125 && MajorVersion == 6
126 && MinorVersion == 0)
127 {
128 /* Vista or Windows Server 2008 */
129 g_pfnrtSendIpi = rtMpSendIpiVista;
130 }
131 else
132 if ( g_pfnrtNtHalSendSoftwareInterrupt
133 && MajorVersion == 6
134 && MinorVersion == 1)
135 {
136 /* Windows 7 or Windows Server 2008 R2 */
137 g_pfnrtSendIpi = rtMpSendIpiWin7;
138 }
139 /* Windows XP should send always send an IPI -> VERIFY */
140#endif
141 KIRQL OldIrql;
142 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); /* make sure we stay on the same cpu */
143
144 union
145 {
146 uint32_t auRegs[4];
147 char szVendor[4*3+1];
148 } u;
149 ASMCpuId(0, &u.auRegs[3], &u.auRegs[0], &u.auRegs[2], &u.auRegs[1]);
150 u.szVendor[4*3] = '\0';
151
152 /*
153 * HACK ALERT (and déjà vu warning)!
154 *
155 * Try find _KPRCB::QuantumEnd and _KPRCB::[DpcData.]DpcQueueDepth.
156 * For purpose of verification we use the VendorString member (12+1 chars).
157 *
158 * The offsets was initially derived by poking around with windbg
159 * (dt _KPRCB, !prcb ++, and such like). Systematic harvesting is now done
160 * by means of dia2dump, grep and the symbol packs. Typically:
161 * dia2dump -type _KDPC_DATA -type _KPRCB EXE\ntkrnlmp.pdb | grep -wE "QuantumEnd|DpcData|DpcQueueDepth|VendorString"
162 */
163 /** @todo array w/ data + script for extracting a row. (save space + readability; table will be short.) */
164 __try
165 {
166#if defined(RT_ARCH_X86)
167 PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
168 uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
169
170 if ( BuildNumber == 2600 /* XP SP2 */
171 && !memcmp(&pbPrcb[0x900], &u.szVendor[0], 4*3))
172 {
173 g_offrtNtPbQuantumEnd = 0x88c;
174 g_cbrtNtPbQuantumEnd = 4;
175 g_offrtNtPbDpcQueueDepth = 0x870;
176 }
177 /* WindowsVista.6002.090410-1830.x86fre.Symbols.exe
178 WindowsVista.6002.090410-1830.x86chk.Symbols.exe
179 WindowsVista.6002.090130-1715.x86fre.Symbols.exe
180 WindowsVista.6002.090130-1715.x86chk.Symbols.exe */
181 else if ( BuildNumber == 6002
182 && !memcmp(&pbPrcb[0x1c2c], &u.szVendor[0], 4*3))
183 {
184 g_offrtNtPbQuantumEnd = 0x1a41;
185 g_cbrtNtPbQuantumEnd = 1;
186 g_offrtNtPbDpcQueueDepth = 0x19e0 + 0xc;
187 }
188 else if ( BuildNumber == 3790 /* Server 2003 SP2 */
189 && !memcmp(&pbPrcb[0xb60], &u.szVendor[0], 4*3))
190 {
191 g_offrtNtPbQuantumEnd = 0x981;
192 g_cbrtNtPbQuantumEnd = 1;
193 g_offrtNtPbDpcQueueDepth = 0x920 + 0xc;
194 }
195
196 /** @todo more */
197 //pbQuantumEnd = (uint8_t volatile *)pPcr->Prcb + 0x1a41;
198
199#elif defined(RT_ARCH_AMD64)
200 PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
201 uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
202
203 if ( BuildNumber == 3790 /* XP64 / W2K3-AMD64 SP1 */
204 && !memcmp(&pbPrcb[0x22b4], &u.szVendor[0], 4*3))
205 {
206 g_offrtNtPbQuantumEnd = 0x1f75;
207 g_cbrtNtPbQuantumEnd = 1;
208 g_offrtNtPbDpcQueueDepth = 0x1f00 + 0x18;
209 }
210 else if ( BuildNumber == 6000 /* Vista/AMD64 */
211 && !memcmp(&pbPrcb[0x38bc], &u.szVendor[0], 4*3))
212 {
213 g_offrtNtPbQuantumEnd = 0x3375;
214 g_cbrtNtPbQuantumEnd = 1;
215 g_offrtNtPbDpcQueueDepth = 0x3300 + 0x18;
216 }
217 /* WindowsVista.6002.090410-1830.amd64fre.Symbols
218 WindowsVista.6002.090130-1715.amd64fre.Symbols
219 WindowsVista.6002.090410-1830.amd64chk.Symbols */
220 else if ( BuildNumber == 6002
221 && !memcmp(&pbPrcb[0x399c], &u.szVendor[0], 4*3))
222 {
223 g_offrtNtPbQuantumEnd = 0x3475;
224 g_cbrtNtPbQuantumEnd = 1;
225 g_offrtNtPbDpcQueueDepth = 0x3400 + 0x18;
226 }
227 /* Windows7.7600.16539.amd64fre.win7_gdr.100226-1909 */
228 else if ( BuildNumber == 7600
229 && !memcmp(&pbPrcb[0x4bb8], &u.szVendor[0], 4*3))
230 {
231 g_offrtNtPbQuantumEnd = 0x21d9;
232 g_cbrtNtPbQuantumEnd = 1;
233 g_offrtNtPbDpcQueueDepth = 0x2180 + 0x18;
234 }
235
236#else
237# error "port me"
238#endif
239 }
240 __except(EXCEPTION_EXECUTE_HANDLER) /** @todo this handler doesn't seem to work... Because of Irql? */
241 {
242 g_offrtNtPbQuantumEnd = 0;
243 g_cbrtNtPbQuantumEnd = 0;
244 g_offrtNtPbDpcQueueDepth = 0;
245 }
246
247 KeLowerIrql(OldIrql);
248
249#ifndef IN_GUEST /** @todo fix above for all Nt versions. */
250 if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
251 DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found! Kernel %u.%u %u %s\n",
252 MajorVersion, MinorVersion, BuildNumber, fChecked ? "checked" : "free");
253# ifdef DEBUG
254 else
255 DbgPrint("IPRT: _KPRCB:{.QuantumEnd=%x/%d, .DpcQueueDepth=%x/%d} Kernel %ul.%ul %ul %s\n",
256 g_offrtNtPbQuantumEnd, g_cbrtNtPbQuantumEnd, g_offrtNtPbDpcQueueDepth,
257 MajorVersion, MinorVersion, BuildNumber, fChecked ? "checked" : "free");
258# endif
259#endif
260
261 return VINF_SUCCESS;
262}
263
264
265void rtR0TermNative(void)
266{
267}
268
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