timer-r0drv-solaris.c revision e64031e20c39650a7bc902a3e1aba613b9415dee
1008N/A/* $Id$ */
1008N/A/** @file
1008N/A * IPRT - Timer, Ring-0 Driver, Solaris.
1008N/A */
1008N/A
1008N/A/*
1008N/A * Copyright (C) 2006-2007 Oracle Corporation
1008N/A *
1008N/A * This file is part of VirtualBox Open Source Edition (OSE), as
1008N/A * available from http://www.virtualbox.org. This file is free software;
1008N/A * you can redistribute it and/or modify it under the terms of the GNU
1008N/A * General Public License (GPL) as published by the Free Software
1008N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
1008N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1008N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1008N/A *
1008N/A * The contents of this file may alternatively be used under the terms
1008N/A * of the Common Development and Distribution License Version 1.0
1008N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1008N/A * VirtualBox OSE distribution, in which case the provisions of the
1008N/A * CDDL are applicable instead of those of the GPL.
1008N/A *
1008N/A * You may elect to license modified versions of this file under the
1008N/A * terms and conditions of either the GPL or the CDDL or both.
1008N/A */
1008N/A
1008N/A
2333N/A/*******************************************************************************
1008N/A* Header Files *
1008N/A*******************************************************************************/
2521N/A#include "../the-solaris-kernel.h"
2521N/A#include "internal/iprt.h"
2521N/A#include <iprt/timer.h>
2521N/A
2521N/A#include <iprt/asm.h>
2521N/A#include <iprt/assert.h>
2521N/A#include <iprt/err.h>
2521N/A#include <iprt/mem.h>
2521N/A#include <iprt/mp.h>
2521N/A#include <iprt/spinlock.h>
2521N/A#include <iprt/time.h>
1008N/A#include <iprt/thread.h>
2521N/A#include "internal/magics.h"
2521N/A
2521N/A
2521N/A/*******************************************************************************
2521N/A* Structures and Typedefs *
2521N/A*******************************************************************************/
2521N/A/**
2521N/A * The internal representation of a Solaris timer handle.
2521N/A */
2333N/Atypedef struct RTTIMER
1008N/A{
2521N/A /** Magic.
2521N/A * This is RTTIMER_MAGIC, but changes to something else before the timer
2521N/A * is destroyed to indicate clearly that thread should exit. */
1008N/A uint32_t volatile u32Magic;
1008N/A /** Flag indicating that the timer is suspended. */
1008N/A uint8_t volatile fSuspended;
1008N/A /** Run on all CPUs if set */
2342N/A uint8_t fAllCpu;
1008N/A /** Whether the timer must run on a specific CPU or not. */
1008N/A uint8_t fSpecificCpu;
1008N/A /** The CPU it must run on if fSpecificCpu is set. */
1008N/A uint8_t iCpu;
1008N/A /** The nano second interval for repeating timers */
1008N/A uint64_t interval;
1008N/A /** simple Solaris timer handle. */
1008N/A vbi_stimer_t *stimer;
1008N/A /** global Solaris timer handle. */
1008N/A vbi_gtimer_t *gtimer;
1008N/A /** The user callback. */
1008N/A PFNRTTIMER pfnTimer;
1008N/A /** The argument for the user callback. */
1008N/A void *pvUser;
1008N/A} RTTIMER;
1008N/A
1008N/A
1008N/A/*******************************************************************************
1008N/A* Defined Constants And Macros *
1008N/A*******************************************************************************/
1008N/A/** Validates that the timer is valid. */
1008N/A#define RTTIMER_ASSERT_VALID_RET(pTimer) \
1008N/A do \
1008N/A { \
1008N/A AssertPtrReturn(pTimer, VERR_INVALID_HANDLE); \
1008N/A AssertReturn((pTimer)->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE); \
1008N/A } while (0)
1008N/A
1008N/A
2521N/A/*
1008N/A * Need a wrapper to get the PRTTIMER passed through
1008N/A */
1008N/Astatic void rtTimerSolarisCallbackWrapper(PRTTIMER pTimer, uint64_t tick)
1008N/A{
1008N/A pTimer->pfnTimer(pTimer, pTimer->pvUser, tick);
1008N/A}
2521N/A
1008N/A
1008N/A
1008N/A
1008N/ARTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
1008N/A{
1008N/A RT_ASSERT_PREEMPTIBLE();
2521N/A *ppTimer = NULL;
1008N/A
1008N/A /*
1008N/A * Validate flags.
1008N/A */
1008N/A if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
1008N/A return VERR_INVALID_PARAMETER;
1008N/A if (vbi_revision_level < 2)
1008N/A return VERR_NOT_SUPPORTED;
1008N/A
1008N/A if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
1008N/A && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
1008N/A && !RTMpIsCpuPossible((fFlags & RTTIMER_FLAGS_CPU_MASK)))
1008N/A return VERR_CPU_NOT_FOUND;
1008N/A
1008N/A if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL && u64NanoInterval == 0)
1008N/A return VERR_NOT_SUPPORTED;
1008N/A
1008N/A /*
1008N/A * Allocate and initialize the timer handle.
1008N/A */
1008N/A PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
1008N/A if (!pTimer)
1008N/A return VERR_NO_MEMORY;
1008N/A
1008N/A pTimer->u32Magic = RTTIMER_MAGIC;
1008N/A pTimer->fSuspended = true;
1008N/A if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
1008N/A {
1008N/A pTimer->fAllCpu = true;
1008N/A pTimer->fSpecificCpu = false;
1008N/A pTimer->iCpu = 255;
1008N/A }
1008N/A else if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
1008N/A {
1008N/A pTimer->fAllCpu = false;
1008N/A pTimer->fSpecificCpu = true;
1008N/A pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
1008N/A }
1008N/A else
1008N/A {
1008N/A pTimer->fAllCpu = false;
1008N/A pTimer->fSpecificCpu = false;
1008N/A pTimer->iCpu = 255;
1008N/A }
1008N/A pTimer->interval = u64NanoInterval;
1008N/A pTimer->pfnTimer = pfnTimer;
1008N/A pTimer->pvUser = pvUser;
1008N/A pTimer->stimer = NULL;
1008N/A pTimer->gtimer = NULL;
1008N/A
1008N/A *ppTimer = pTimer;
1008N/A return VINF_SUCCESS;
1008N/A}
1008N/A
1008N/A
1008N/ARTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
1008N/A{
1008N/A if (pTimer == NULL)
1008N/A return VINF_SUCCESS;
1008N/A RTTIMER_ASSERT_VALID_RET(pTimer);
1008N/A RT_ASSERT_INTS_ON();
1008N/A
1008N/A /*
1008N/A * Free the associated resources.
1008N/A */
1008N/A RTTimerStop(pTimer);
1008N/A ASMAtomicWriteU32(pTimer, ~RTTIMER_MAGIC);
1008N/A RTMemFree(pTimer);
1008N/A return VINF_SUCCESS;
1008N/A}
1008N/A
1008N/A
1008N/ARTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
1008N/A{
1008N/A RTTIMER_ASSERT_VALID_RET(pTimer);
1008N/A RT_ASSERT_INTS_ON();
1008N/A
1008N/A if (!pTimer->fSuspended)
1008N/A return VERR_TIMER_ACTIVE;
1008N/A
1008N/A pTimer->fSuspended = false;
1008N/A if (pTimer->fAllCpu)
1008N/A {
1008N/A pTimer->gtimer = vbi_gtimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval);
1008N/A if (pTimer->gtimer == NULL)
1008N/A return VERR_INVALID_PARAMETER;
1008N/A }
1008N/A else
1008N/A {
1008N/A int iCpu = VBI_ANY_CPU;
1008N/A if (pTimer->fSpecificCpu)
1008N/A iCpu = pTimer->iCpu;
1008N/A pTimer->stimer = vbi_stimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval, iCpu);
1008N/A if (pTimer->stimer == NULL)
1008N/A {
1008N/A if (iCpu != VBI_ANY_CPU)
1008N/A return VERR_CPU_OFFLINE;
1008N/A return VERR_INVALID_PARAMETER;
1008N/A }
1008N/A }
1008N/A
1008N/A return VINF_SUCCESS;
1008N/A}
1008N/A
1008N/A
1008N/ARTDECL(int) RTTimerStop(PRTTIMER pTimer)
1008N/A{
1008N/A RTTIMER_ASSERT_VALID_RET(pTimer);
1008N/A RT_ASSERT_INTS_ON();
1008N/A
1008N/A if (pTimer->fSuspended)
1008N/A return VERR_TIMER_SUSPENDED;
1008N/A
1008N/A pTimer->fSuspended = true;
1008N/A if (pTimer->stimer)
1008N/A {
1008N/A vbi_stimer_end(pTimer->stimer);
1008N/A pTimer->stimer = NULL;
1008N/A }
1008N/A else if (pTimer->gtimer)
1008N/A {
1008N/A vbi_gtimer_end(pTimer->gtimer);
1008N/A pTimer->gtimer = NULL;
1008N/A }
1008N/A
1008N/A return VINF_SUCCESS;
1008N/A}
1008N/A
1008N/A
1008N/A
1008N/ARTDECL(uint32_t) RTTimerGetSystemGranularity(void)
1008N/A{
1008N/A return vbi_timer_granularity();
1008N/A}
1008N/A
1418N/A
1008N/ARTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
1418N/A{
1008N/A return VERR_NOT_SUPPORTED;
1008N/A}
1008N/A
1008N/A
1008N/ARTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
1008N/A{
1008N/A return VERR_NOT_SUPPORTED;
1008N/A}
1008N/A
1008N/A