VirtualBox

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

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

IPRT/r0drv-nt: Cleaning up the preemption hacks; XP SP2 is done.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/* $Id: initterm-r0drv-nt.cpp 19969 2009-05-24 16:21:24Z vboxsync $ */
2/** @file
3 * IPRT - Initialization & Termination, R0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include "the-nt-kernel.h"
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/mp.h>
38#include <iprt/string.h>
39#include "internal/initterm.h"
40#include "internal-r0drv-nt.h"
41
42
43/*******************************************************************************
44* Global Variables *
45*******************************************************************************/
46/** The Nt CPU set.
47 * KeQueryActiveProcssors() cannot be called at all IRQLs and therefore we'll
48 * have to cache it. Fortunately, Nt doesn't really support taking CPUs offline
49 * or online. It's first with W2K8 that support for CPU hotplugging was added.
50 * Once we start caring about this, we'll simply let the native MP event callback
51 * and update this variable as CPUs comes online. (The code is done already.)
52 */
53RTCPUSET g_rtMpNtCpuSet;
54
55/** ExSetTimerResolution, introduced in W2K. */
56PFNMYEXSETTIMERRESOLUTION g_pfnrtNtExSetTimerResolution;
57/** KeFlushQueuedDpcs, introduced in XP. */
58PFNMYKEFLUSHQUEUEDDPCS g_pfnrtNtKeFlushQueuedDpcs;
59
60/** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
61uint32_t g_offrtNtPbQuantumEnd;
62/** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
63uint32_t g_cbrtNtPbQuantumEnd;
64/** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
65uint32_t g_offrtNtPbDpcQueueDepth;
66
67
68
69int rtR0InitNative(void)
70{
71 /*
72 * Init the Nt cpu set.
73 */
74 KAFFINITY ActiveProcessors = KeQueryActiveProcessors();
75 RTCpuSetEmpty(&g_rtMpNtCpuSet);
76 RTCpuSetFromU64(&g_rtMpNtCpuSet, ActiveProcessors);
77
78 /*
79 * Initialize the function pointers.
80 */
81 UNICODE_STRING RoutineName;
82 RtlInitUnicodeString(&RoutineName, L"ExSetTimerResolution");
83 g_pfnrtNtExSetTimerResolution = (PFNMYEXSETTIMERRESOLUTION)MmGetSystemRoutineAddress(&RoutineName);
84
85 RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
86 g_pfnrtNtKeFlushQueuedDpcs = (PFNMYKEFLUSHQUEUEDDPCS)MmGetSystemRoutineAddress(&RoutineName);
87
88 /*
89 * Get some info that might come in handy below.
90 */
91 ULONG MajorVersion = 0;
92 ULONG MinorVersion = 0;
93 ULONG BuildNumber = 0;
94 PsGetVersion(&MajorVersion, &MinorVersion, &BuildNumber, NULL);
95
96 KIRQL OldIrql;
97 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); /* make sure we stay on the same cpu */
98
99 union
100 {
101 uint32_t auRegs[4];
102 char szVendor[4*3+1];
103 } u;
104 ASMCpuId(0, &u.auRegs[3], &u.auRegs[0], &u.auRegs[2], &u.auRegs[1]);
105 u.szVendor[4*3] = '\0';
106
107 /*
108 * Try find _KPRCB::QuantumEnd and possibly also _KPRCB::DpcQueueDepth.
109 */
110 __try
111 {
112 /* HACK ALERT! The offsets are from poking around in windbg. */
113#if defined(RT_ARCH_X86)
114 PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
115 uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
116
117 if ( BuildNumber == 2600 /* XP SP2 */
118 && !memcmp(&pbPrcb[0x900], &u.szVendor[0], 4*3))
119 {
120 g_offrtNtPbQuantumEnd = 0x88c;
121 g_cbrtNtPbQuantumEnd = 4;
122 g_offrtNtPbDpcQueueDepth = 0x870;
123 }
124 /** @todo more */
125 //pbQuantumEnd = (uint8_t volatile *)pPcr->Prcb + 0x1a41;
126
127#elif defined(RT_ARCH_AMD64)
128 PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
129 uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
130 /** @todo proper detection! */
131 if (pbPrcb[0x3375] <= 1)
132 {
133 g_offrtNtPbQuantumEnd = 0x3375;
134 g_cbrtNtPbQuantumEnd = 1;
135 g_offrtNtPbDpcQueueDepth = 0;
136 }
137
138#else
139# error "port me"
140#endif
141 }
142 __except(EXCEPTION_EXECUTE_HANDLER)
143 {
144 g_offrtNtPbQuantumEnd = 0;
145 g_cbrtNtPbQuantumEnd = 0;
146 g_offrtNtPbDpcQueueDepth = 0;
147 }
148
149 KeLowerIrql(OldIrql);
150
151#ifndef IN_GUEST /** @todo fix above for all Nt versions. */
152 if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
153 DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found!\n");
154#endif
155
156 return VINF_SUCCESS;
157}
158
159
160void rtR0TermNative(void)
161{
162}
163
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