timer-r0drv-nt.cpp revision 0fec00bfa0954891da94ac750d8dd9a867e74749
/* $Id$ */
/** @file
* IPRT - Timers, Ring-0 Driver, NT.
*/
/*
* Copyright (C) 2006-2012 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 "internal-r0drv-nt.h"
/** This seems to provide better accuracy. */
#define RTR0TIMER_NT_MANUAL_RE_ARM 1
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* A sub timer structure.
*
* This is used for keeping the per-cpu tick and DPC object.
*/
typedef struct RTTIMERNTSUBTIMER
{
/** The tick counter. */
/** Pointer to the parent timer. */
/** The NT DPC object. */
/** Pointer to a NT sub-timer structure. */
typedef RTTIMERNTSUBTIMER *PRTTIMERNTSUBTIMER;
/**
* The internal representation of an Linux timer handle.
*/
typedef struct RTTIMER
{
/** Magic.
* This is RTTIMER_MAGIC, but changes to something else before the timer
* is destroyed to indicate clearly that thread should exit. */
/** Suspend count down for single shot omnit timers. */
int32_t volatile cOmniSuspendCountDown;
/** Flag indicating the timer is suspended. */
bool volatile fSuspended;
/** Whether the timer must run on one specific CPU or not. */
bool fSpecificCpu;
/** Whether the timer must run on all CPUs or not. */
bool fOmniTimer;
/** The CPU it must run on if fSpecificCpu is set.
* The master CPU for an omni-timer. */
/** Callback. */
/** User argument. */
void *pvUser;
/** The timer interval. 0 if one-shot. */
#ifdef RTR0TIMER_NT_MANUAL_RE_ARM
/** The NT start time . */
#endif
/** The Nt timer object. */
/** The number of sub-timers. */
/** Sub-timers.
* Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
* an entry for all possible cpus. In that case the index will be the same as
* for the RTCpuSet. */
} RTTIMER;
#ifdef RTR0TIMER_NT_MANUAL_RE_ARM
/**
* Get current NT interrupt time.
* @return NT interrupt time
*/
static uint64_t rtTimerNtQueryInterruptTime(void)
{
# ifdef RT_ARCH_AMD64
return KeQueryInterruptTime(); /* macro */
# else
return g_pfnrtKeQueryInterruptTime();
/* NT4 */
do
{
} while (((KUSER_SHARED_DATA volatile *)SharedUserData)->InterruptTime.High2Time != InterruptTime.HighPart);
return InterruptTime.QuadPart;
# endif
}
#endif /* RTR0TIMER_NT_MANUAL_RE_ARM */
/**
* Manually re-arms an internval timer.
*
* Turns out NT doesn't necessarily do a very good job at re-arming timers
* accurately.
*
* @param pTimer The timer.
* @param iTick The current timer tick.
* @param pMasterDpc The master DPC.
*/
{
#ifdef RTR0TIMER_NT_MANUAL_RE_ARM
else
#endif
}
/**
* Timer callback function for the non-omni timers.
*
* @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a one-shot or interval timer.
* @param pDpc Pointer to the DPC.
* @param pvUser Pointer to our internal timer structure.
* @param SystemArgument1 Some system argument.
* @param SystemArgument2 Some system argument.
*/
static void _stdcall rtTimerNtSimpleCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
{
#ifdef RT_STRICT
if (KeGetCurrentIrql() < DISPATCH_LEVEL)
RTAssertMsg2Weak("rtTimerNtSimpleCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
#endif
/*
* Check that we haven't been suspended before doing the callout.
*/
{
if (!pTimer->u64NanoInterval)
if (pTimer->u64NanoInterval)
}
}
/**
* The slave DPC callback for an omni timer.
*
* @param pDpc The DPC object.
* @param pvUser Pointer to the sub-timer.
* @param SystemArgument1 Some system stuff.
* @param SystemArgument2 Some system stuff.
*/
static void _stdcall rtTimerNtOmniSlaveCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
{
#ifdef RT_STRICT
if (KeGetCurrentIrql() < DISPATCH_LEVEL)
RTAssertMsg2Weak("rtTimerNtOmniSlaveCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
RTAssertMsg2Weak("rtTimerNtOmniSlaveCallback: iCpuSelf=%d pSubTimer=%p / %d\n", iCpuSelf, pSubTimer, pSubTimer - &pTimer->aSubTimers[0]);
#endif
/*
* Check that we haven't been suspended before doing the callout.
*/
{
if (!pTimer->u64NanoInterval)
}
}
/**
* The timer callback for an omni-timer.
*
* This is responsible for queueing the DPCs for the other CPUs and
* perform the callback on the CPU on which it is called.
*
* @param pDpc The DPC object.
* @param pvUser Pointer to the sub-timer.
* @param SystemArgument1 Some system stuff.
* @param SystemArgument2 Some system stuff.
*/
static void _stdcall rtTimerNtOmniMasterCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
{
#ifdef RT_STRICT
if (KeGetCurrentIrql() < DISPATCH_LEVEL)
RTAssertMsg2Weak("rtTimerNtOmniMasterCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
RTAssertMsg2Weak("rtTimerNtOmniMasterCallback: iCpuSelf=%d pSubTimer=%p / %d\n", iCpuSelf, pSubTimer, pSubTimer - &pTimer->aSubTimers[0]);
#endif
/*
* Check that we haven't been suspended before scheduling the other DPCs
* and doing the callout.
*/
{
if (pTimer->u64NanoInterval)
{
/*
* Recurring timer.
*/
rtTimerNtRearmInternval(pTimer, iTick, &pTimer->aSubTimers[RTMpCpuIdToSetIndex(pTimer->idCpu)].NtDpc);
}
else
{
/*
* Single shot timers gets complicated wrt to fSuspended maintance.
*/
cCpus++;
}
}
}
{
/*
* Validate.
*/
return VERR_TIMER_ACTIVE;
if ( pTimer->fSpecificCpu
return VERR_CPU_OFFLINE;
/*
* Start the timer.
*/
#ifndef RTR0TIMER_NT_MANUAL_RE_ARM
if (ulInterval != u64Interval)
ulInterval = 1;
#endif
#ifdef RTR0TIMER_NT_MANUAL_RE_ARM
#else
#endif
return VINF_SUCCESS;
}
/**
* Worker function that stops an active timer.
*
* Shared by RTTimerStop and RTTimerDestroy.
*
* @param pTimer The active timer.
*/
{
/*
* Just cancel the timer, dequeue the DPCs and flush them (if this is supported).
*/
/*
* I'm a bit uncertain whether this should be done during RTTimerStop
* or only in RTTimerDestroy()... Linux and Solaris will wait AFAIK,
* which is why I'm keeping this here for now.
*/
}
{
/*
* Validate.
*/
return VERR_TIMER_SUSPENDED;
/*
* Call the worker we share with RTTimerDestroy.
*/
return VINF_SUCCESS;
}
{
return VERR_NOT_SUPPORTED;
}
{
/* It's ok to pass NULL pointer. */
return VINF_SUCCESS;
/*
* Invalidate the timer, stop it if it's running and finally
* free up the memory.
*/
return VINF_SUCCESS;
}
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
{
/*
* Validate flags.
*/
if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
return VERR_INVALID_PARAMETER;
if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
return VERR_CPU_NOT_FOUND;
/*
* Allocate the timer handler.
*/
{
Assert(cSubTimers <= RTCPUSET_MAX_CPUS); /* On Windows we have a 1:1 relationship between cpuid and set index. */
}
if (!pTimer)
return VERR_NO_MEMORY;
/*
* Initialize it.
*/
pTimer->cOmniSuspendCountDown = 0;
pTimer->fSuspended = true;
pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
pTimer->idCpu = pTimer->fSpecificCpu ? RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK) : NIL_RTCPUID;
if (pTimer->fOmniTimer)
{
/*
* Initialize the per-cpu "sub-timers", select the first online cpu
* to be the master.
* ASSUMES that no cpus will ever go offline.
*/
{
{
KeInitializeDpc(&pTimer->aSubTimers[iCpu].NtDpc, rtTimerNtOmniMasterCallback, &pTimer->aSubTimers[iCpu]);
}
else
KeInitializeDpc(&pTimer->aSubTimers[iCpu].NtDpc, rtTimerNtOmniSlaveCallback, &pTimer->aSubTimers[iCpu]);
}
}
else
{
/*
* Initialize the first "sub-timer", target the DPC on a specific processor
* if requested to do so.
*/
if (pTimer->fSpecificCpu)
}
return VINF_SUCCESS;
}
{
return VERR_NOT_SUPPORTED;
if (pu32Granted)
return VINF_SUCCESS;
}
{
return VERR_NOT_SUPPORTED;
return VINF_SUCCESS;
}
RTDECL(bool) RTTimerCanDoHighResolution(void)
{
return false;
}