VirtualBox

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

Last change on this file since 29698 was 29253, checked in by vboxsync, 15 years ago

initterm-r0drv-nt.cpp: build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1/* $Id: initterm-r0drv-nt.cpp 29253 2010-05-09 18:01:10Z 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
189 /** @todo more */
190 //pbQuantumEnd = (uint8_t volatile *)pPcr->Prcb + 0x1a41;
191
192#elif defined(RT_ARCH_AMD64)
193 PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
194 uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
195
196 if ( BuildNumber == 3790 /* XP64 / W2K3-AMD64 SP1 */
197 && !memcmp(&pbPrcb[0x22b4], &u.szVendor[0], 4*3))
198 {
199 g_offrtNtPbQuantumEnd = 0x1f75;
200 g_cbrtNtPbQuantumEnd = 1;
201 g_offrtNtPbDpcQueueDepth = 0x1f00 + 0x18;
202 }
203 else if ( BuildNumber == 6000 /* Vista/AMD64 */
204 && !memcmp(&pbPrcb[0x38bc], &u.szVendor[0], 4*3))
205 {
206 g_offrtNtPbQuantumEnd = 0x3375;
207 g_cbrtNtPbQuantumEnd = 1;
208 g_offrtNtPbDpcQueueDepth = 0x3300 + 0x18;
209 }
210 /* WindowsVista.6002.090410-1830.amd64fre.Symbols
211 WindowsVista.6002.090130-1715.amd64fre.Symbols
212 WindowsVista.6002.090410-1830.amd64chk.Symbols */
213 else if ( BuildNumber == 6002
214 && !memcmp(&pbPrcb[0x399c], &u.szVendor[0], 4*3))
215 {
216 g_offrtNtPbQuantumEnd = 0x3475;
217 g_cbrtNtPbQuantumEnd = 1;
218 g_offrtNtPbDpcQueueDepth = 0x3400 + 0x18;
219 }
220
221#else
222# error "port me"
223#endif
224 }
225 __except(EXCEPTION_EXECUTE_HANDLER) /** @todo this handler doesn't seem to work... Because of Irql? */
226 {
227 g_offrtNtPbQuantumEnd = 0;
228 g_cbrtNtPbQuantumEnd = 0;
229 g_offrtNtPbDpcQueueDepth = 0;
230 }
231
232 KeLowerIrql(OldIrql);
233
234#ifndef IN_GUEST /** @todo fix above for all Nt versions. */
235 if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
236 DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found! Kernel %u.%u %u %s\n",
237 MajorVersion, MinorVersion, BuildNumber, fChecked ? "checked" : "free");
238# ifdef DEBUG
239 else
240 DbgPrint("IPRT: _KPRCB:{.QuantumEnd=%x/%d, .DpcQueueDepth=%x/%d} Kernel %ul.%ul %ul %s\n",
241 g_offrtNtPbQuantumEnd, g_cbrtNtPbQuantumEnd, g_offrtNtPbDpcQueueDepth,
242 MajorVersion, MinorVersion, BuildNumber, fChecked ? "checked" : "free");
243# endif
244#endif
245
246 return VINF_SUCCESS;
247}
248
249
250void rtR0TermNative(void)
251{
252}
253
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