timer-r0drv-solaris.c revision f43a4242779824fc22218b47d037d7d296441d0b
/* $Id$ */
/** @file
* IPRT - Timer, Ring-0 Driver, Solaris.
*/
/*
* Copyright (C) 2006-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-solaris-kernel.h"
#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
# include <iprt/asm-amd64-x86.h>
#endif
#include <iprt/spinlock.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The internal representation of a Solaris 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. */
/** Reference counter. */
/** Flag indicating that the timer is suspended (hCyclicId should be
* CYCLIC_NONE). */
bool volatile fSuspended;
/** Flag indicating that the timer was suspended from the timer callback and
* therefore the hCyclicId may still be valid. */
bool volatile fSuspendedFromTimer;
/** Flag indicating that the timer interval was changed and that it requires
* manual expiration time programming for each callout. */
bool volatile fIntervalChanged;
/** Whether the timer must run on all CPUs or not. */
/** Whether the timer must run on a specific CPU or not. */
/** The CPU it must run on if fSpecificCpu is set. */
/** The nano second interval for repeating timers. */
uint64_t volatile cNsInterval;
/** Cyclic timer Id. This is CYCLIC_NONE if no active timer.
* @remarks Please keep in mind that cyclic may call us back before the
* cyclic_add/cyclic_add_omni functions returns, so don't use this
* unguarded with cyclic_reprogram. */
/** The user callback. */
/** The argument for the user callback. */
void *pvUser;
/** Union with timer type specific data. */
union
{
/** Single timer (fAllCpus == false). */
struct
{
/** Cyclic handler. */
/** Cyclic time and interval representation. */
/** Timer ticks. */
/** The next tick when fIntervalChanged is true, otherwise 0. */
/** The (interrupt) thread currently active in the callback. */
kthread_t * volatile pActiveThread;
} Single;
/** Omni timer (fAllCpus == true). */
struct
{
/** Absolute timestamp of when the timer should fire first when starting up. */
/** Array of per CPU data (variable size). */
struct
{
/** Timer ticks (reinitialized when online'd). */
/** The (interrupt) thread currently active in the callback. */
kthread_t * volatile pActiveThread;
/** The next tick when fIntervalChanged is true, otherwise 0. */
} aPerCpu[1];
} Omni;
} u;
} RTTIMER;
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Validates that the timer is valid. */
#define RTTIMER_ASSERT_VALID_RET(pTimer) \
do \
{ \
AssertMsgReturn((pTimer)->u32Magic == RTTIMER_MAGIC, ("pTimer=%p u32Magic=%x expected %x\n", (pTimer), (pTimer)->u32Magic, RTTIMER_MAGIC), \
} while (0)
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static void rtTimerSolSingleCallbackWrapper(void *pvArg);
/**
* Retains a reference to the timer.
*
* @returns New reference counter value.
* @param pTimer The timer.
*/
{
}
/**
* Destroys the timer when the reference counter has reached zero.
*
* @returns 0 (new references counter value).
* @param pTimer The timer.
*/
{
}
/**
* Releases a reference to the timer.
*
* @returns New reference counter value.
* @param pTimer The timer.
*/
{
if (!cRefs)
return rtTimeSolReleaseCleanup(pTimer);
return cRefs;
}
/**
* RTMpOnSpecific callback used by rtTimerSolCallbackWrapper() to deal with
* callouts on the wrong CPU (race with cyclic_bind).
*
* @param idCpu The CPU this is fired on.
* @param pvUser1 Opaque pointer to the timer.
* @param pvUser2 Not used, NULL.
*/
{
/* This avoids some code duplication. */
}
/**
* Callback wrapper for single-CPU timers.
*
* @param pvArg Opaque pointer to the timer.
*
* @remarks This will be executed in interrupt context but only at the specified
* level i.e. CY_LOCK_LEVEL in our case. We -CANNOT- call into the
* cyclic subsystem here, neither should pfnTimer().
*/
static void rtTimerSolSingleCallbackWrapper(void *pvArg)
{
/* Make sure one-shots do not fire another time. */
|| pTimer->cNsInterval != 0);
if (!pTimer->fSuspendedFromTimer)
{
/* For specific timers, we might fire on the wrong CPU between cyclic_add() and cyclic_bind().
Redirect these shots to the right CPU as we are temporarily rebinding to the right CPU. */
{
return;
}
/* For one-shot, we may allow the callback to restart them. */
if (pTimer->cNsInterval == 0)
pTimer->fSuspendedFromTimer = true;
/*
* Perform the callout.
*/
{
if ( !pTimer->fIntervalChanged
return;
/*
* The interval was changed, we need to set the expiration time
* our selves before returning. This comes at a slight cost,
* which is why we don't do it all the time.
*/
else
return;
}
/*
* The timer has been suspended, set expiration time to infinitiy.
*/
}
}
/**
* Callback wrapper for Omni-CPU timers.
*
* @param pvArg Opaque pointer to the timer.
*
* @remarks This will be executed in interrupt context but only at the specified
* level i.e. CY_LOCK_LEVEL in our case. We -CANNOT- call into the
* cyclic subsystem here, neither should pfnTimer().
*/
static void rtTimerSolOmniCallbackWrapper(void *pvArg)
{
if (!pTimer->fSuspendedFromTimer)
{
/*
* Perform the callout.
*/
{
if ( !pTimer->fIntervalChanged
return;
/*
* The interval was changed, we need to set the expiration time
* our selves before returning. This comes at a slight cost,
* which is why we don't do it all the time.
*
* Note! The cyclic_reprogram call only affects the omni cyclic
* component for this CPU.
*/
else
pTimer->u.Omni.aPerCpu[iCpu].nsNextTick = RTTimeSystemNanoTS() + ASMAtomicUoReadU64(&pTimer->cNsInterval);
return;
}
/*
* The timer has been suspended, set expiration time to infinitiy.
*/
}
}
/**
* Omni-CPU cyclic online event. This is called before the omni cycle begins to
* fire on the specified CPU.
*
* @param pvArg Opaque pointer to the timer.
* @param pCpu Pointer to the CPU on which it will fire.
* @param pCyclicHandler Pointer to a cyclic handler to add to the CPU
* specified in @a pCpu.
* @param pCyclicTime Pointer to the cyclic time and interval object.
*
* @remarks We -CANNOT- call back into the cyclic subsystem here, we can however
* block (sleep).
*/
static void rtTimerSolOmniCpuOnline(void *pvArg, cpu_t *pCpu, cyc_handler_t *pCyclicHandler, cyc_time_t *pCyclicTime)
{
else
}
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;
/* One-shot omni timers are not supported by the cyclic system. */
&& u64NanoInterval == 0)
return VERR_NOT_SUPPORTED;
/*
* Allocate and initialize the timer handle. The omni variant has a
* variable sized array of ticks counts, thus the size calculation.
*/
: sizeof(RTTIMER));
if (!pTimer)
return VERR_NO_MEMORY;
pTimer->fSuspended = true;
pTimer->fSuspendedFromTimer = false;
pTimer->fIntervalChanged = false;
{
pTimer->fSpecificCpu = false;
}
else if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
{
pTimer->fSpecificCpu = true;
}
else
{
pTimer->fSpecificCpu = false;
}
return VINF_SUCCESS;
}
/**
* Checks if the calling thread is currently executing the timer proceduce for
* the given timer.
*
* @returns true if it is, false if it isn't.
* @param pTimer The timer in question.
*/
{
}
{
return VINF_SUCCESS;
/*
* It is not possible to destroy a timer from it's callback function.
* Cyclic makes that impossible (or at least extremely risky).
*/
/*
* Invalidate the handle, make sure it's stopped nad free the associated resources.
*/
if ( !pTimer->fSuspended
return VINF_SUCCESS;
}
{
/*
* It's not possible to restart a one-shot time from it's callback function,
* at least not at the moment.
*/
/*
* Make sure it's not active already. If it was suspended from a timer
* callback function, we need to do some cleanup work here before we can
* restart the timer.
*/
if (!pTimer->fSuspended)
{
if (!pTimer->fSuspendedFromTimer)
{
return VERR_TIMER_ACTIVE;
}
}
pTimer->fSuspended = false;
pTimer->fSuspendedFromTimer = false;
pTimer->fIntervalChanged = false;
{
/*
* Setup omni (all CPU) timer. The Omni-CPU online event will fire
* and from there we setup periodic timers per CPU.
*/
}
else
{
/*
* Setup a single CPU timer. If a specific CPU was requested, it
* must be online or the timer cannot start.
*/
if ( pTimer->fSpecificCpu
{
pTimer->fSuspended = true;
return VERR_CPU_OFFLINE;
}
/** @todo we probably don't need to have cyc_handler_t and cyc_time_t in the
* timer structure... */
: CY_INFINITY /* Special value, see cyclic_fire. */;
if (pTimer->fSpecificCpu)
}
return VINF_SUCCESS;
}
/**
* Worker common for RTTimerStop and RTTimerDestroy.
*
* @param pTimer The timer to stop.
*/
{
pTimer->fSuspended = true;
{
}
pTimer->fSuspendedFromTimer = false;
}
{
if (pTimer->fSuspended)
return VERR_TIMER_SUSPENDED;
/* Trying the cpu_lock stuff and calling cyclic_remove may deadlock
the system, so just mark the timer as suspened and deal with it in
the callback wrapper function above. */
pTimer->fSuspendedFromTimer = true;
else
return VINF_SUCCESS;
}
{
/*
* Validate.
*/
else
{
}
return VINF_SUCCESS;
}
{
return nsec_per_tick;
}
{
return VERR_NOT_SUPPORTED;
}
{
return VERR_NOT_SUPPORTED;
}
RTDECL(bool) RTTimerCanDoHighResolution(void)
{
/** @todo return true; - when missing bits have been implemented and tested*/
return false;
}