initterm-r0drv-nt.cpp revision 3a11c660d37a68f1ba83d8f08ee6516ebc5aaa05
/* $Id$ */
/** @file
* IPRT - Initialization & Termination, R0 Driver, NT.
*/
/*
* Copyright (C) 2006-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-nt-kernel.h"
#include <iprt/asm-amd64-x86.h>
#include "internal/initterm.h"
#include "internal-r0drv-nt.h"
#include "symdb.h"
#include "symdbdata.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The NT CPU set.
* KeQueryActiveProcssors() cannot be called at all IRQLs and therefore we'll
* have to cache it. Fortunately, Nt doesn't really support taking CPUs offline
* or online. It's first with W2K8 that support for CPU hotplugging was added.
* Once we start caring about this, we'll simply let the native MP event callback
* and update this variable as CPUs comes online. (The code is done already.)
*/
/** ExSetTimerResolution, introduced in W2K. */
/** KeFlushQueuedDpcs, introduced in XP. */
/** HalRequestIpi, version introduced with windows 7. */
/** HalRequestIpi, version valid up to windows vista?? */
/** HalSendSoftwareInterrupt, introduced in AMD64 version of W2K3. */
/** Worker for RTMpPokeCpu. */
/** KeIpiGenericCall - Introduced in Windows Server 2003. */
/** KeInitializeAffinityEx - Introducted in Windows 7. */
/** KeAddProcessorAffinityEx - Introducted in Windows 7. */
/** KeGetProcessorIndexFromNumber - Introducted in Windows 7. */
/** RtlGetVersion, introduced in ??. */
#ifndef RT_ARCH_AMD64
#endif
/** KeQueryInterruptTimePrecise - new in Windows 8. */
/** KeQuerySystemTimePrecise - new in Windows 8. */
/** Offset of the _KPRCB::QuantumEnd field. 0 if not found. */
/** Size of the _KPRCB::QuantumEnd field. 0 if not found. */
/** Offset of the _KPRCB::DpcQueueDepth field. 0 if not found. */
/**
* Determines the NT kernel verison information.
*
* @param pOsVerInfo Where to return the version information.
*
* @remarks pOsVerInfo->fSmp is only definitive if @c true.
* @remarks pOsVerInfo->uCsdNo is set to MY_NIL_CSD if it cannot be determined.
*/
{
ULONG ulMajorVersion = 0;
ULONG ulMinorVersion = 0;
ULONG ulBuildNumber = 0;
pOsVerInfo->fChecked = PsGetVersion(&ulMajorVersion, &ulMinorVersion, &ulBuildNumber, NULL) == TRUE;
#define MY_NIL_CSD 0x3f
if (g_pfnrtRtlGetVersion)
{
if (NT_SUCCESS(rcNt))
}
/* Note! We cannot quite say if something is MP or UNI. So, fSmp is
redefined to indicate that it must be MP. */
}
/**
* Tries a set against the current kernel.
*
* @retval @c true if it matched up, global variables are updated.
* @retval @c false otherwise (no globals updated).
* @param pSet The data set.
* @param pbPrcb Pointer to the processor control block.
* @param pszVendor Pointer to the processor vendor string.
* @param pOsVerInfo The OS version info.
*/
static bool rtR0NtTryMatchSymSet(PCRTNTSDBSET pSet, uint8_t *pbPrcb, const char *pszVendor, PCRTNTSDBOSVER pOsVerInfo)
{
/*
* Don't bother trying stuff where the NT kernel version number differs, or
* if the build type or SMPness doesn't match up.
*/
{
return false;
}
/*
* Do the CPU vendor test.
*
* Note! The MmIsAddressValid call is the real #PF security here as the
*/
{
//DbgPrint("IPRT: #%d invalid vendor string address.\n", pSet - &g_artNtSdbSets[0]);
return false;
}
{
{
//DbgPrint("IPRT: #%d Vendor string mismatch.\n", pSet - &g_artNtSdbSets[0]);
return false;
}
}
{
return false;
}
/*
* Got a match, update the global variables and report succcess.
*/
#if 0
DbgPrint("IPRT: Using data set #%u for %u.%usp%u build %u %s %s.\n",
pSet - &g_artNtSdbSets[0],
#endif
return true;
}
DECLHIDDEN(int) rtR0InitNative(void)
{
/*
* Init the Nt cpu set.
*/
#ifdef IPRT_TARGET_NT4
#else
#endif
/*
* Initialize the function pointers.
*/
#ifdef IPRT_TARGET_NT4
#else
g_pfnrtNtHalSendSoftwareInterrupt = (PFNHALSENDSOFTWAREINTERRUPT)MmGetSystemRoutineAddress(&RoutineName);
g_pfnrtKeAddProcessorAffinityEx = (PFNKEADDPROCESSORAFFINITYEX)MmGetSystemRoutineAddress(&RoutineName);
g_pfnrtKeGetProcessorIndexFromNumber = (PFNKEGETPROCESSORINDEXFROMNUMBER)MmGetSystemRoutineAddress(&RoutineName);
# ifndef RT_ARCH_AMD64
# endif
g_pfnrtKeQueryInterruptTimePrecise = (PFNRTKEQUERYINTERRUPTTIMEPRECISE)MmGetSystemRoutineAddress(&RoutineName);
g_pfnrtKeQuerySystemTimePrecise = (PFNRTKEQUERYSYSTEMTIMEPRECISE)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.
*/
/*
* Gather consistent CPU vendor string and PRCB pointers.
*/
union
{
} u;
{
#if defined(RT_ARCH_X86)
#elif defined(RT_ARCH_AMD64)
#else
# error "port me"
#endif
}
{
}
/*
* Search the database
*/
if (pbPrcb)
{
/* Find the best matching kernel version based on build number. */
{
continue;
continue;
int32_t iDelta = RT_ABS((int32_t)OsVerInfo.uBuildNo - (int32_t)g_artNtSdbSets[i].OsVerInfo.uBuildNo);
if ( iDelta == 0
{
/* prefect */
iBestDelta = iDelta;
iBest = i;
break;
}
if ( iDelta < iBestDelta
|| iBest == UINT32_MAX
|| ( iDelta == iBestDelta
)
)
{
iBestDelta = iDelta;
iBest = i;
}
}
{
/* Try all sets: iBest -> End; iBest -> Start. */
bool fDone = false;
while ( i < RT_ELEMENTS(g_artNtSdbSets)
i++;
if (!fDone)
{
while ( i >= 0
i--;
}
}
else
DbgPrint("IPRT: Failed to locate data set.\n");
}
else
DbgPrint("IPRT: Failed to get PCBR pointer.\n");
#ifndef IN_GUEST
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",
OsVerInfo.uMajorVer, OsVerInfo.uMinorVer, OsVerInfo.uBuildNo, OsVerInfo.fChecked ? "checked" : "free");
# endif
#endif
/*
* Special IPI fun for RTMpPokeCpu.
*
* On Vista and later the DPC method doesn't seem to reliably send IPIs,
* so we have to use alternative methods. The NtHalSendSoftwareInterrupt
* is preferrable, but it's AMD64 only. The NalRequestIpip method changed
* in Windows 7 with the lots-of-processors-support, but it's the only
* targeted IPI game in town if we cannot use KeInsertQueueDpc. Worst case
* we use broadcast IPIs.
*/
else
#ifndef IPRT_TARGET_NT4
else if ( g_pfnrtHalRequestIpiW7Plus
/* else: Windows XP should send always send an IPI -> VERIFY */
#endif
return VINF_SUCCESS;
}
DECLHIDDEN(void) rtR0TermNative(void)
{
}