initterm-r0drv-nt.cpp revision ae8302e80cb2f8a5ad2812dd644029761de06c57
0N/A/* $Id$ */
2273N/A/** @file
0N/A * IPRT - Initialization & Termination, R0 Driver, NT.
0N/A */
0N/A
0N/A/*
0N/A * Copyright (C) 2006-2013 Oracle Corporation
0N/A *
0N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License (GPL) as published by the Free Software
0N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
0N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A *
0N/A * The contents of this file may alternatively be used under the terms
0N/A * of the Common Development and Distribution License Version 1.0
1472N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1472N/A * VirtualBox OSE distribution, in which case the provisions of the
1472N/A * CDDL are applicable instead of those of the GPL.
0N/A *
0N/A * You may elect to license modified versions of this file under the
0N/A * terms and conditions of either the GPL or the CDDL or both.
1879N/A */
1879N/A
1879N/A/*******************************************************************************
1879N/A* Header Files *
1879N/A*******************************************************************************/
1879N/A#include "the-nt-kernel.h"
1879N/A#include <iprt/asm-amd64-x86.h>
1879N/A#include <iprt/assert.h>
1879N/A#include <iprt/err.h>
1879N/A#include <iprt/mp.h>
1879N/A#include <iprt/string.h>
1879N/A#include "internal/initterm.h"
1879N/A#include "internal-r0drv-nt.h"
1879N/A#include "symdb.h"
1879N/A#include "symdbdata.h"
1879N/A
1879N/A
2073N/A/*******************************************************************************
2073N/A* Global Variables *
2073N/A*******************************************************************************/
2073N/A/** The NT CPU set.
2073N/A * KeQueryActiveProcssors() cannot be called at all IRQLs and therefore we'll
2073N/A * have to cache it. Fortunately, Nt doesn't really support taking CPUs offline
1879N/A * or online. It's first with W2K8 that support for CPU hotplugging was added.
1879N/A * Once we start caring about this, we'll simply let the native MP event callback
1879N/A * and update this variable as CPUs comes online. (The code is done already.)
1879N/A */
1879N/ARTCPUSET g_rtMpNtCpuSet;
1879N/A
1879N/A/** ExSetTimerResolution, introduced in W2K. */
1879N/APFNMYEXSETTIMERRESOLUTION g_pfnrtNtExSetTimerResolution;
1879N/A/** KeFlushQueuedDpcs, introduced in XP. */
2796N/APFNMYKEFLUSHQUEUEDDPCS g_pfnrtNtKeFlushQueuedDpcs;
2796N/A/** HalRequestIpi, introduced in ??. */
2796N/APFNHALREQUESTIPI g_pfnrtNtHalRequestIpi;
1879N/A/** HalSendSoftwareInterrupt */
0N/APFNHALSENDSOFTWAREINTERRUPT g_pfnrtNtHalSendSoftwareInterrupt;
0N/A/** SendIpi handler based on Windows version */
0N/APFNRTSENDIPI g_pfnrtSendIpi;
0N/A/** KeIpiGenericCall - Windows Server 2003+ only */
0N/APFNRTKEIPIGENERICCALL g_pfnrtKeIpiGenericCall;
0N/A/** RtlGetVersion, introduced in ??. */
0N/APFNRTRTLGETVERSION g_pfnrtRtlGetVersion;
0N/A
0N/A/** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
0N/Auint32_t g_offrtNtPbQuantumEnd;
0N/A/** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
0N/Auint32_t g_cbrtNtPbQuantumEnd;
0N/A/** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
0N/Auint32_t g_offrtNtPbDpcQueueDepth;
0N/A
0N/A
0N/A/**
0N/A * Determines the NT kernel verison information.
0N/A *
0N/A * @param pOsVerInfo Where to return the version information.
0N/A *
0N/A * @remarks pOsVerInfo->fSmp is only definitive if @c true.
0N/A * @remarks pOsVerInfo->uCsdNo is set to MY_NIL_CSD if it cannot be determined.
0N/A */
0N/Astatic void rtR0NtGetOsVersionInfo(PRTNTSDBOSVER pOsVerInfo)
0N/A{
0N/A ULONG ulMajorVersion = 0;
0N/A ULONG ulMinorVersion = 0;
0N/A ULONG ulBuildNumber = 0;
0N/A
0N/A pOsVerInfo->fChecked = PsGetVersion(&ulMajorVersion, &ulMinorVersion, &ulBuildNumber, NULL) == TRUE;
0N/A pOsVerInfo->uMajorVer = (uint8_t)ulMajorVersion;
0N/A pOsVerInfo->uMinorVer = (uint8_t)ulMinorVersion;
0N/A pOsVerInfo->uBuildNo = ulBuildNumber;
0N/A#define MY_NIL_CSD 0x3f
0N/A pOsVerInfo->uCsdNo = MY_NIL_CSD;
0N/A
0N/A if (g_pfnrtRtlGetVersion)
0N/A {
0N/A RTL_OSVERSIONINFOEXW VerInfo;
0N/A RT_ZERO(VerInfo);
0N/A VerInfo.dwOSVersionInfoSize = sizeof(VerInfo);
0N/A
0N/A NTSTATUS rcNt = g_pfnrtRtlGetVersion(&VerInfo);
0N/A if (NT_SUCCESS(rcNt))
0N/A pOsVerInfo->uCsdNo = VerInfo.wServicePackMajor;
0N/A }
0N/A
0N/A /* Note! We cannot quite say if something is MP or UNI. So, fSmp is
0N/A redefined to indicate that it must be MP. */
0N/A pOsVerInfo->fSmp = RTMpGetCount() > 1
0N/A || ulMajorVersion >= 6; /* Vista and later has no UNI kernel AFAIK. */
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Tries a set against the current kernel.
0N/A *
0N/A * @retval @c true if it matched up, global variables are updated.
0N/A * @retval @c false otherwise (no globals updated).
0N/A * @param pSet The data set.
0N/A * @param pbPrcb Pointer to the processor control block.
0N/A * @param pszVendor Pointer to the processor vendor string.
0N/A * @param pOsVerInfo The OS version info.
0N/A */
0N/Astatic bool rtR0NtTryMatchSymSet(PCRTNTSDBSET pSet, uint8_t *pbPrcb, const char *pszVendor, PCRTNTSDBOSVER pOsVerInfo)
0N/A{
0N/A /*
0N/A * Don't bother trying stuff where the NT kernel version number differs, or
0N/A * if the build type or SMPness doesn't match up.
0N/A */
0N/A if ( pSet->OsVerInfo.uMajorVer != pOsVerInfo->uMajorVer
0N/A || pSet->OsVerInfo.uMinorVer != pOsVerInfo->uMinorVer
0N/A || pSet->OsVerInfo.fChecked != pOsVerInfo->fChecked
1426N/A || (!pSet->OsVerInfo.fSmp && pOsVerInfo->fSmp /*must-be-smp*/) )
0N/A {
0N/A //DbgPrint("IPRT: #%d Version/type mismatch.\n", pSet - &g_artNtSdbSets[0]);
1426N/A return false;
0N/A }
0N/A
0N/A /*
0N/A * Do the CPU vendor test.
0N/A *
0N/A * Note! The MmIsAddressValid call is the real #PF security here as the
0N/A * __try/__except has limited/no ability to catch everything we need.
0N/A */
0N/A char *pszPrcbVendorString = (char *)&pbPrcb[pSet->KPRCB.offVendorString];
0N/A if (!MmIsAddressValid(&pszPrcbVendorString[4 * 3 - 1]))
0N/A {
0N/A //DbgPrint("IPRT: #%d invalid vendor string address.\n", pSet - &g_artNtSdbSets[0]);
0N/A return false;
0N/A }
0N/A __try
0N/A {
0N/A if (memcmp(pszPrcbVendorString, pszVendor, RT_MIN(4 * 3, pSet->KPRCB.cbVendorString)) != 0)
0N/A {
0N/A //DbgPrint("IPRT: #%d Vendor string mismatch.\n", pSet - &g_artNtSdbSets[0]);
0N/A return false;
0N/A }
0N/A }
0N/A __except(EXCEPTION_EXECUTE_HANDLER)
0N/A {
0N/A DbgPrint("IPRT: %#d Exception\n", pSet - &g_artNtSdbSets[0]);
0N/A return false;
0N/A }
0N/A
0N/A /*
0N/A * Got a match, update the global variables and report succcess.
0N/A */
0N/A g_offrtNtPbQuantumEnd = pSet->KPRCB.offQuantumEnd;
0N/A g_cbrtNtPbQuantumEnd = pSet->KPRCB.cbQuantumEnd;
0N/A g_offrtNtPbDpcQueueDepth = pSet->KPRCB.offDpcQueueDepth;
0N/A
0N/A#if 0
0N/A DbgPrint("IPRT: Using data set #%u for %u.%usp%u build %u %s %s.\n",
0N/A pSet - &g_artNtSdbSets[0],
0N/A pSet->OsVerInfo.uMajorVer,
0N/A pSet->OsVerInfo.uMinorVer,
0N/A pSet->OsVerInfo.uCsdNo,
0N/A pSet->OsVerInfo.uBuildNo,
0N/A pSet->OsVerInfo.fSmp ? "smp" : "uni",
0N/A pSet->OsVerInfo.fChecked ? "checked" : "free");
0N/A#endif
0N/A return true;
465N/A}
0N/A
0N/A
0N/ADECLHIDDEN(int) rtR0InitNative(void)
0N/A{
0N/A /*
0N/A * Init the Nt cpu set.
0N/A */
0N/A#ifdef IPRT_TARGET_NT4
0N/A KAFFINITY ActiveProcessors = (UINT64_C(1) << KeNumberProcessors) - UINT64_C(1);
0N/A#else
0N/A KAFFINITY ActiveProcessors = KeQueryActiveProcessors();
0N/A#endif
0N/A RTCpuSetEmpty(&g_rtMpNtCpuSet);
0N/A RTCpuSetFromU64(&g_rtMpNtCpuSet, ActiveProcessors);
0N/A/** @todo Port to W2K8 with > 64 cpus/threads. */
2062N/A
0N/A#ifdef IPRT_TARGET_NT4
2062N/A g_pfnrtNtExSetTimerResolution = NULL;
2062N/A g_pfnrtNtKeFlushQueuedDpcs = NULL;
2062N/A g_pfnrtNtHalRequestIpi = NULL;
0N/A g_pfnrtNtHalSendSoftwareInterrupt = NULL;
0N/A g_pfnrtKeIpiGenericCall = NULL;
0N/A g_pfnrtRtlGetVersion = NULL;
0N/A#else
0N/A /*
2062N/A * Initialize the function pointers.
0N/A */
2062N/A UNICODE_STRING RoutineName;
2062N/A RtlInitUnicodeString(&RoutineName, L"ExSetTimerResolution");
2062N/A g_pfnrtNtExSetTimerResolution = (PFNMYEXSETTIMERRESOLUTION)MmGetSystemRoutineAddress(&RoutineName);
0N/A
0N/A RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
0N/A g_pfnrtNtKeFlushQueuedDpcs = (PFNMYKEFLUSHQUEUEDDPCS)MmGetSystemRoutineAddress(&RoutineName);
2062N/A
0N/A RtlInitUnicodeString(&RoutineName, L"HalRequestIpi");
2062N/A g_pfnrtNtHalRequestIpi = (PFNHALREQUESTIPI)MmGetSystemRoutineAddress(&RoutineName);
2062N/A
2062N/A RtlInitUnicodeString(&RoutineName, L"HalSendSoftwareInterrupt");
0N/A g_pfnrtNtHalSendSoftwareInterrupt = (PFNHALSENDSOFTWAREINTERRUPT)MmGetSystemRoutineAddress(&RoutineName);
0N/A
0N/A RtlInitUnicodeString(&RoutineName, L"KeIpiGenericCall");
0N/A g_pfnrtKeIpiGenericCall = (PFNRTKEIPIGENERICCALL)MmGetSystemRoutineAddress(&RoutineName);
1879N/A
1879N/A RtlInitUnicodeString(&RoutineName, L"RtlGetVersion");
g_pfnrtRtlGetVersion = (PFNRTRTLGETVERSION)MmGetSystemRoutineAddress(&RoutineName);
#endif
/*
* HACK ALERT! (and d�j� vu warning - remember win32k.sys?)
*
* Try find _KPRCB::QuantumEnd and _KPRCB::[DpcData.]DpcQueueDepth.
* For purpose of verification we use the VendorString member (12+1 chars).
*
* The offsets was initially derived by poking around with windbg
* (dt _KPRCB, !prcb ++, and such like). Systematic harvesting was then
* planned using dia2dump, grep and the symbol pack in a manner like this:
* dia2dump -type _KDPC_DATA -type _KPRCB EXE\ntkrnlmp.pdb | grep -wE "QuantumEnd|DpcData|DpcQueueDepth|VendorString"
*
* The final solution ended up using a custom harvester program called
* ntBldSymDb that recursively searches thru unpacked symbol packages for
* the desired structure offsets. The program assumes that the packages
* are unpacked into directories with the same name as the package, with
* exception of some of the w2k packages which requires a 'w2k' prefix to
* be distinguishable from another.
*/
RTNTSDBOSVER OsVerInfo;
rtR0NtGetOsVersionInfo(&OsVerInfo);
/*
* Gather consistent CPU vendor string and PRCB pointers.
*/
KIRQL OldIrql;
KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); /* make sure we stay on the same cpu */
union
{
uint32_t auRegs[4];
char szVendor[4*3+1];
} u;
ASMCpuId(0, &u.auRegs[3], &u.auRegs[0], &u.auRegs[2], &u.auRegs[1]);
u.szVendor[4*3] = '\0';
uint8_t *pbPrcb;
__try /* Warning. This try/except statement may provide some false safety. */
{
#if defined(RT_ARCH_X86)
PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
pbPrcb = (uint8_t *)pPcr->Prcb;
#elif defined(RT_ARCH_AMD64)
PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
#else
# error "port me"
pbPrcb = NULL;
#endif
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
pbPrcb = NULL;
}
/*
* Search the database
*/
if (pbPrcb)
{
/* Find the best matching kernel version based on build number. */
uint32_t iBest = UINT32_MAX;
int32_t iBestDelta = INT32_MAX;
for (uint32_t i = 0; i < RT_ELEMENTS(g_artNtSdbSets); i++)
{
if (g_artNtSdbSets[i].OsVerInfo.fChecked != OsVerInfo.fChecked)
continue;
if (OsVerInfo.fSmp /*must-be-smp*/ && !g_artNtSdbSets[i].OsVerInfo.fSmp)
continue;
int32_t iDelta = RT_ABS((int32_t)OsVerInfo.uBuildNo - (int32_t)g_artNtSdbSets[i].OsVerInfo.uBuildNo);
if ( iDelta == 0
&& (g_artNtSdbSets[i].OsVerInfo.uCsdNo == OsVerInfo.uCsdNo || OsVerInfo.uCsdNo == MY_NIL_CSD))
{
/* prefect */
iBestDelta = iDelta;
iBest = i;
break;
}
if ( iDelta < iBestDelta
|| iBest == UINT32_MAX
|| ( iDelta == iBestDelta
&& OsVerInfo.uCsdNo != MY_NIL_CSD
&& RT_ABS(g_artNtSdbSets[i ].OsVerInfo.uCsdNo - (int32_t)OsVerInfo.uCsdNo)
< RT_ABS(g_artNtSdbSets[iBest].OsVerInfo.uCsdNo - (int32_t)OsVerInfo.uCsdNo)
)
)
{
iBestDelta = iDelta;
iBest = i;
}
}
if (iBest < RT_ELEMENTS(g_artNtSdbSets))
{
/* Try all sets: iBest -> End; iBest -> Start. */
bool fDone = false;
int32_t i = iBest;
while ( i < RT_ELEMENTS(g_artNtSdbSets)
&& !(fDone = rtR0NtTryMatchSymSet(&g_artNtSdbSets[i], pbPrcb, u.szVendor, &OsVerInfo)))
i++;
if (!fDone)
{
i = (int32_t)iBest - 1;
while ( i >= 0
&& !(fDone = rtR0NtTryMatchSymSet(&g_artNtSdbSets[i], pbPrcb, u.szVendor, &OsVerInfo)))
i--;
}
}
else
DbgPrint("IPRT: Failed to locate data set.\n");
}
else
DbgPrint("IPRT: Failed to get PCBR pointer.\n");
KeLowerIrql(OldIrql); /* Lowering the IRQL early in the hope that we may catch exceptions below. */
#ifndef IN_GUEST
if (!g_offrtNtPbQuantumEnd && !g_offrtNtPbDpcQueueDepth)
DbgPrint("IPRT: Neither _KPRCB::QuantumEnd nor _KPRCB::DpcQueueDepth was not found! Kernel %u.%u %u %s\n",
OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
# ifdef DEBUG
else
DbgPrint("IPRT: _KPRCB:{.QuantumEnd=%x/%d, .DpcQueueDepth=%x/%d} Kernel %u.%u %u %s\n",
g_offrtNtPbQuantumEnd, g_cbrtNtPbQuantumEnd, g_offrtNtPbDpcQueueDepth,
OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
# endif
#endif
/*
* Special IPI fun.
*/
g_pfnrtSendIpi = rtMpSendIpiDummy;
#ifndef IPRT_TARGET_NT4
if ( g_pfnrtNtHalRequestIpi
&& OsVerInfo.uMajorVer == 6
&& OsVerInfo.uMinorVer == 0)
{
/* Vista or Windows Server 2008 */
g_pfnrtSendIpi = rtMpSendIpiVista;
}
else if ( g_pfnrtNtHalSendSoftwareInterrupt
&& OsVerInfo.uMajorVer == 6
&& OsVerInfo.uMinorVer == 1)
{
/* Windows 7 or Windows Server 2008 R2 */
g_pfnrtSendIpi = rtMpSendIpiWin7;
}
/* Windows XP should send always send an IPI -> VERIFY */
#endif
return VINF_SUCCESS;
}
DECLHIDDEN(void) rtR0TermNative(void)
{
}