initterm-r0drv-nt.cpp revision 95222546f70bb121c5cb934da27875ea965831c9
2788N/A/* $Id$ */
2788N/A/** @file
2788N/A * IPRT - Initialization & Termination, R0 Driver, NT.
2788N/A */
2788N/A
2788N/A/*
2788N/A * Copyright (C) 2006-2007 Sun Microsystems, Inc.
2788N/A *
2788N/A * This file is part of VirtualBox Open Source Edition (OSE), as
2788N/A * available from http://www.virtualbox.org. This file is free software;
2788N/A * you can redistribute it and/or modify it under the terms of the GNU
2788N/A * General Public License (GPL) as published by the Free Software
2788N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
2788N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2788N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2788N/A *
2788N/A * The contents of this file may alternatively be used under the terms
2788N/A * of the Common Development and Distribution License Version 1.0
2788N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
2788N/A * VirtualBox OSE distribution, in which case the provisions of the
2788N/A * CDDL are applicable instead of those of the GPL.
2788N/A *
2788N/A * You may elect to license modified versions of this file under the
2788N/A * terms and conditions of either the GPL or the CDDL or both.
2788N/A *
4458N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
2788N/A * Clara, CA 95054 USA or visit http://www.sun.com if you need
2788N/A * additional information or have any questions.
2788N/A */
2788N/A
2788N/A/*******************************************************************************
2788N/A* Header Files *
2788N/A*******************************************************************************/
2788N/A#include "the-nt-kernel.h"
2788N/A#include <iprt/assert.h>
2788N/A#include <iprt/err.h>
2788N/A#include <iprt/mp.h>
2788N/A#include <iprt/string.h>
2788N/A#include "internal/initterm.h"
2788N/A#include "internal-r0drv-nt.h"
2788N/A
2788N/A
2788N/A/*******************************************************************************
2788N/A* Global Variables *
2788N/A*******************************************************************************/
2788N/A/** The Nt CPU set.
2788N/A * KeQueryActiveProcssors() cannot be called at all IRQLs and therefore we'll
2788N/A * have to cache it. Fortunately, Nt doesn't really support taking CPUs offline
2788N/A * or online. It's first with W2K8 that support for CPU hotplugging was added.
2788N/A * Once we start caring about this, we'll simply let the native MP event callback
2788N/A * and update this variable as CPUs comes online. (The code is done already.)
2788N/A */
2788N/ARTCPUSET g_rtMpNtCpuSet;
2788N/A
2788N/A/** ExSetTimerResolution, introduced in W2K. */
3194N/APFNMYEXSETTIMERRESOLUTION g_pfnrtNtExSetTimerResolution;
2788N/A/** KeFlushQueuedDpcs, introduced in XP. */
2788N/APFNMYKEFLUSHQUEUEDDPCS g_pfnrtNtKeFlushQueuedDpcs;
2788N/A
2788N/A/** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
2788N/Auint32_t g_offrtNtPbQuantumEnd;
2788N/A/** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
2788N/Auint32_t g_cbrtNtPbQuantumEnd;
2788N/A/** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
2788N/Auint32_t g_offrtNtPbDpcQueueDepth;
2788N/A
2788N/A
2788N/A
2788N/Aint rtR0InitNative(void)
2788N/A{
2788N/A /*
2788N/A * Init the Nt cpu set.
2788N/A */
2788N/A KAFFINITY ActiveProcessors = KeQueryActiveProcessors();
2788N/A RTCpuSetEmpty(&g_rtMpNtCpuSet);
2788N/A RTCpuSetFromU64(&g_rtMpNtCpuSet, ActiveProcessors);
2788N/A
2788N/A /*
2788N/A * Initialize the function pointers.
2788N/A */
2788N/A UNICODE_STRING RoutineName;
2788N/A RtlInitUnicodeString(&RoutineName, L"ExSetTimerResolution");
2788N/A g_pfnrtNtExSetTimerResolution = (PFNMYEXSETTIMERRESOLUTION)MmGetSystemRoutineAddress(&RoutineName);
2788N/A
2788N/A RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
2788N/A g_pfnrtNtKeFlushQueuedDpcs = (PFNMYKEFLUSHQUEUEDDPCS)MmGetSystemRoutineAddress(&RoutineName);
2788N/A
2788N/A /*
2788N/A * Get some info that might come in handy below.
2788N/A */
2788N/A ULONG MajorVersion = 0;
2788N/A ULONG MinorVersion = 0;
2788N/A ULONG BuildNumber = 0;
2788N/A BOOLEAN fChecked = PsGetVersion(&MajorVersion, &MinorVersion, &BuildNumber, NULL);
2788N/A
2788N/A KIRQL OldIrql;
2788N/A KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); /* make sure we stay on the same cpu */
2788N/A
2788N/A union
2788N/A {
2788N/A uint32_t auRegs[4];
2788N/A char szVendor[4*3+1];
2788N/A } u;
2788N/A ASMCpuId(0, &u.auRegs[3], &u.auRegs[0], &u.auRegs[2], &u.auRegs[1]);
2788N/A u.szVendor[4*3] = '\0';
3951N/A
3951N/A /*
3951N/A * HACK ALERT (and d�j� vu warning)!
3951N/A *
3951N/A * Try find _KPRCB::QuantumEnd and _KPRCB::[DpcData.]DpcQueueDepth.
3951N/A * For purpose of verification we use the VendorString member (12+1 chars).
2788N/A *
3951N/A * The offsets was initially derived by poking around with windbg
2788N/A * (dt _KPRCB, !prcb ++, and such like). Systematic harvesting is now done
3194N/A * by means of dia2dump, grep and the symbol packs. Typically:
2788N/A * dia2dump -type _KDPC_DATA -type _KPRCB EXE\ntkrnlmp.pdb | grep -wE "QuantumEnd|DpcData|DpcQueueDepth|VendorString"
2788N/A */
2788N/A /** @todo array w/ data + script for extracting a row. (save space + readability; table will be short.) */
2788N/A __try
2788N/A {
2788N/A#if defined(RT_ARCH_X86)
3951N/A PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
2788N/A uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
3600N/A
4495N/A if ( BuildNumber == 2600 /* XP SP2 */
3600N/A && !memcmp(&pbPrcb[0x900], &u.szVendor[0], 4*3))
3600N/A {
3600N/A g_offrtNtPbQuantumEnd = 0x88c;
3600N/A g_cbrtNtPbQuantumEnd = 4;
3600N/A g_offrtNtPbDpcQueueDepth = 0x870;
3600N/A }
3600N/A /* WindowsVista.6002.090410-1830.x86fre.Symbols.exe
3600N/A WindowsVista.6002.090410-1830.x86chk.Symbols.exe
3600N/A WindowsVista.6002.090130-1715.x86fre.Symbols.exe
2788N/A WindowsVista.6002.090130-1715.x86chk.Symbols.exe */
2788N/A else if ( BuildNumber == 6002
2788N/A && !memcmp(&pbPrcb[0x1c2c], &u.szVendor[0], 4*3))
2788N/A {
2788N/A g_offrtNtPbQuantumEnd = 0x1a41;
2788N/A g_cbrtNtPbQuantumEnd = 1;
2788N/A g_offrtNtPbDpcQueueDepth = 0x19e0 + 0xc;
2788N/A }
3951N/A
3951N/A /** @todo more */
3951N/A //pbQuantumEnd = (uint8_t volatile *)pPcr->Prcb + 0x1a41;
3951N/A
3951N/A#elif defined(RT_ARCH_AMD64)
3951N/A PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
3951N/A uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
3951N/A
3951N/A if ( BuildNumber == 3790 /* XP64 / W2K3-AMD64 SP1 */
3951N/A && !memcmp(&pbPrcb[0x22b4], &u.szVendor[0], 4*3))
3951N/A {
2788N/A g_offrtNtPbQuantumEnd = 0x1f75;
2788N/A g_cbrtNtPbQuantumEnd = 1;
2788N/A g_offrtNtPbDpcQueueDepth = 0x1f00 + 0x18;
2788N/A }
2788N/A else if ( BuildNumber == 6000 /* Vista/AMD64 */
2788N/A && !memcmp(&pbPrcb[0x38bc], &u.szVendor[0], 4*3))
2788N/A {
2788N/A g_offrtNtPbQuantumEnd = 0x3375;
2788N/A g_cbrtNtPbQuantumEnd = 1;
2788N/A g_offrtNtPbDpcQueueDepth = 0x3300 + 0x18;
2788N/A }
3951N/A /* WindowsVista.6002.090410-1830.amd64fre.Symbols
3951N/A WindowsVista.6002.090130-1715.amd64fre.Symbols
3951N/A WindowsVista.6002.090410-1830.amd64chk.Symbols */
3951N/A else if ( BuildNumber == 6002
3951N/A && !memcmp(&pbPrcb[0x399c], &u.szVendor[0], 4*3))
3951N/A {
3951N/A g_offrtNtPbQuantumEnd = 0x3475;
3951N/A g_cbrtNtPbQuantumEnd = 1;
3951N/A g_offrtNtPbDpcQueueDepth = 0x3400 + 0x18;
3951N/A }
3951N/A
3951N/A#else
3951N/A# error "port me"
3951N/A#endif
3951N/A }
3951N/A __except(EXCEPTION_EXECUTE_HANDLER) /** @todo this handler doesn't seem to work... Because of Irql? */
3951N/A {
3962N/A g_offrtNtPbQuantumEnd = 0;
3962N/A g_cbrtNtPbQuantumEnd = 0;
3951N/A g_offrtNtPbDpcQueueDepth = 0;
3951N/A }
3951N/A
3951N/A KeLowerIrql(OldIrql);
3951N/A
3951N/A#ifndef IN_GUEST /** @todo fix above for all Nt versions. */
3951N/A if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
3951N/A DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found! Kernel %u.%u %u %s\n",
3951N/A MajorVersion, MinorVersion, BuildNumber, fChecked ? "checked" : "free");
3951N/A# ifdef DEBUG
3951N/A else
3951N/A DbgPrint("IPRT: _KPRCB:{.QuantumEnd=%x/%d, .DpcQueueDepth=%x/%d} Kernel %ul.%ul %ul %s\n",
2788N/A g_offrtNtPbQuantumEnd, g_cbrtNtPbQuantumEnd, g_offrtNtPbDpcQueueDepth,
2788N/A MajorVersion, MinorVersion, BuildNumber, fChecked ? "checked" : "free");
2788N/A# endif
2788N/A#endif
2788N/A
2788N/A return VINF_SUCCESS;
2788N/A}
2788N/A
2788N/A
2788N/Avoid rtR0TermNative(void)
2788N/A{
2788N/A}
2788N/A
2788N/A