mp-r0drv-nt.cpp revision 14565f279fb52d17994857f5b68f261cca3a0ba9
/* $Id$ */
/** @file
* IPRT - Multiprocessor, Ring-0 Driver, NT.
*/
/*
* Copyright (C) 2008-2014 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 "r0drv/mp-r0drv.h"
#include "internal-r0drv-nt.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef enum
{
} RT_NT_CPUID;
/* test a couple of assumption. */
/** @todo
* We cannot do other than assume a 1:1 relationship between the
* the docs. If someone knows a better way to get this done, please
* let bird know.
*/
{
/* WDK upgrade warning: PCR->Number changed from BYTE to WORD. */
return KeGetCurrentProcessorNumber();
}
{
}
{
}
{
/** @todo use KeQueryMaximumProcessorCount on vista+ */
return MAXIMUM_PROCESSORS - 1;
}
{
if (idCpu >= MAXIMUM_PROCESSORS)
return false;
#if 0 /* this isn't safe at all IRQLs (great work guys) */
#else
#endif
}
{
/* Cannot easily distinguish between online and offline cpus. */
* (KeQueryMaximumProcessorCount). */
return RTMpIsCpuOnline(idCpu);
}
{
* (KeQueryMaximumProcessorCount). */
return RTMpGetOnlineSet(pSet);
}
{
* (KeQueryMaximumProcessorCount). */
return RTMpGetOnlineCount();
}
{
#if 0 /* this isn't safe at all IRQLs (great work guys) */
#else
*pSet = g_rtMpNtCpuSet;
return pSet;
#endif
}
{
return RTCpuSetCount(&Set);
}
#if 0
/* Experiment with checking the undocumented KPRCB structure
* 'dt nt!_kprcb 0xaddress' shows the layout
*/
typedef struct
{
volatile ULONG DpcQueueDepth;
} KDPC_DATA, *PKDPC_DATA;
RTDECL(bool) RTMpIsCpuWorkPending(void)
{
_asm {
}
if (pDpcData->DpcQueueDepth)
return true;
pDpcData++;
if (pDpcData->DpcQueueDepth)
return true;
return false;
}
#else
RTDECL(bool) RTMpIsCpuWorkPending(void)
{
/** @todo not implemented */
return false;
}
#endif
/**
* Wrapper between the native KIPI_BROADCAST_WORKER and IPRT's PFNRTMPWORKER for
* the RTMpOnAll case.
*
* @param uUserCtx The user context argument (PRTMPARGS).
*/
{
/*ASMAtomicIncU32(&pArgs->cHits); - not needed */
return 0;
}
/**
* Wrapper between the native KIPI_BROADCAST_WORKER and IPRT's PFNRTMPWORKER for
* the RTMpOnOthers case.
*
* @param uUserCtx The user context argument (PRTMPARGS).
*/
{
{
/*ASMAtomicIncU32(&pArgs->cHits); - not needed */
}
return 0;
}
/**
* Wrapper between the native KIPI_BROADCAST_WORKER and IPRT's PFNRTMPWORKER for
* the RTMpOnSpecific case.
*
* @param uUserCtx The user context argument (PRTMPARGS).
*/
{
{
}
return 0;
}
/**
* Internal worker for the RTMpOn* APIs using KeIpiGenericCall.
*
* @returns IPRT status code.
* @param pfnWorker The callback.
* @param pvUser1 User argument 1.
* @param pvUser2 User argument 2.
* @param enmCpuid What to do / is idCpu valid.
* @param idCpu Used if enmCpuid RT_NT_CPUID_SPECIFIC, otherwise ignored.
*/
{
return VINF_SUCCESS;
return VERR_CPU_OFFLINE;
}
/**
* Wrapper between the native nt per-cpu callbacks and PFNRTWORKER
*
* @param Dpc DPC object
* @param DeferredContext Context argument specified by KeInitializeDpc
* @param SystemArgument1 Argument specified by KeInsertQueueDpc
* @param SystemArgument2 Argument specified by KeInsertQueueDpc
*/
static VOID __stdcall rtmpNtDPCWrapper(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
{
/* Dereference the argument structure. */
if (cRefs == 0)
}
/**
* Internal worker for the RTMpOn* APIs.
*
* @returns IPRT status code.
* @param pfnWorker The callback.
* @param pvUser1 User argument 1.
* @param pvUser2 User argument 2.
* @param enmCpuid What to do / is idCpu valid.
* @param idCpu Used if enmCpuid RT_NT_CPUID_SPECIFIC, otherwise ignored.
*/
static int rtMpCallUsingDpcs(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2, RT_NT_CPUID enmCpuid, RTCPUID idCpu)
{
#if 0
/* KeFlushQueuedDpcs must be run at IRQL PASSIVE_LEVEL according to MSDN, but the
* driver verifier doesn't complain...
*/
AssertMsg(KeGetCurrentIrql() == PASSIVE_LEVEL, ("%d != %d (PASSIVE_LEVEL)\n", KeGetCurrentIrql(), PASSIVE_LEVEL));
#endif
#ifdef IPRT_TARGET_NT4
/* g_pfnrtNt* are not present on NT anyway. */
return VERR_NOT_SUPPORTED;
#else
#endif
/* KeFlushQueuedDpcs is not present in Windows 2000; import it dynamically so we can just fail this call. */
return VERR_NOT_SUPPORTED;
pArgs = (PRTMPARGS)ExAllocatePoolWithTag(NonPagedPool, MAXIMUM_PROCESSORS*sizeof(KDPC) + sizeof(RTMPARGS), (ULONG)'RTMp');
if (!pArgs)
return VERR_NO_MEMORY;
if (enmCpuid == RT_NT_CPUID_SPECIFIC)
{
}
else
{
for (unsigned i = 0; i < MAXIMUM_PROCESSORS; i++)
{
KeSetTargetProcessorDpc(&paExecCpuDpcs[i], i);
}
}
/* Raise the IRQL to DISPATCH_LEVEL so we can't be rescheduled to another cpu.
* KeInsertQueueDpc must also be executed at IRQL >= DISPATCH_LEVEL.
*/
/*
* We cannot do other than assume a 1:1 relationship between the
* affinity mask and the process despite the warnings in the docs.
* If someone knows a better way to get this done, please let bird know.
*/
ASMCompilerBarrier(); /* paranoia */
if (enmCpuid == RT_NT_CPUID_SPECIFIC)
{
}
else
{
unsigned iSelf = KeGetCurrentProcessorNumber();
for (unsigned i = 0; i < MAXIMUM_PROCESSORS; i++)
{
if ( (i != iSelf)
{
}
}
if (enmCpuid != RT_NT_CPUID_OTHERS)
}
/* Flush all DPCs and wait for completion. (can take long!) */
* stuff (and check for the current cpu above in the specific case). */
/** @todo Seems KeFlushQueuedDpcs doesn't wait for the DPCs to be completely
* executed. Seen pArgs being freed while some CPU was using it before
* cRefs was added. */
/* Dereference the argument structure. */
if (cRefs == 0)
return VINF_SUCCESS;
}
{
}
{
return rtMpCallUsingBroadcastIpi(pfnWorker, pvUser1, pvUser2, rtmpNtOnOthersBroadcastIpiWrapper, 0);
}
{
if (!RTMpIsCpuOnline(idCpu))
return !RTMpIsCpuPossible(idCpu)
return rtMpCallUsingBroadcastIpi(pfnWorker, pvUser1, pvUser2, rtmpNtOnSpecificBroadcastIpiWrapper, 0);
}
static VOID rtMpNtPokeCpuDummy(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
{
}
#ifndef IPRT_TARGET_NT4
{
return 0;
}
{
return VINF_SUCCESS;
}
# if 0
{
/* If we start care about Vista, we should investigate a non-broadcast API. */
//// g_pfnrtNtHalRequestIpi(1 << idCpu);
return VINF_SUCCESS;
}
# endif
{
return VINF_SUCCESS;
}
#endif /* IPRT_TARGET_NT4 */
{
/*
* APC fallback.
*/
static bool s_fPokeDPCsInitialized = false;
if (!s_fPokeDPCsInitialized)
{
for (unsigned i = 0; i < RT_ELEMENTS(s_aPokeDpcs); i++)
{
KeSetTargetProcessorDpc(&s_aPokeDpcs[i], (int)i);
}
s_fPokeDPCsInitialized = true;
}
/* Raise the IRQL to DISPATCH_LEVEL so we can't be rescheduled to another cpu.
* KeInsertQueueDpc must also be executed at IRQL >= DISPATCH_LEVEL.
*/
/* Assuming here that high importance DPCs will be delivered immediately; or at least an IPI will be sent immediately.
* @note: not true on at least Vista & Windows 7
*/
}
{
if (!RTMpIsCpuOnline(idCpu))
return !RTMpIsCpuPossible(idCpu)
/* Calls rtMpSendIpiFallback, rtMpSendIpiWin7AndLater or rtMpSendIpiVista. */
return g_pfnrtMpPokeCpuWorker(idCpu);
}
RTDECL(bool) RTMpOnAllIsConcurrentSafe(void)
{
return false;
}