spinlock-r0drv-solaris.c revision fc0e1701814ea969173fadb20fef6833b04fb2bc
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/* $Id$ */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/** @file
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * IPRT - Spinlocks, Ring-0 Driver, Solaris.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/*
c0b6af690ad705bddfa87c643b89770a7a0aaf5avboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * available from http://www.virtualbox.org. This file is free software;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * you can redistribute it and/or modify it under the terms of the GNU
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * General Public License (GPL) as published by the Free Software
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * The contents of this file may alternatively be used under the terms
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * of the Common Development and Distribution License Version 1.0
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * VirtualBox OSE distribution, in which case the provisions of the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * CDDL are applicable instead of those of the GPL.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * You may elect to license modified versions of this file under the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * terms and conditions of either the GPL or the CDDL or both.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * additional information or have any questions.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/*******************************************************************************
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync* Header Files *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync*******************************************************************************/
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include "the-solaris-kernel.h"
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include "internal/iprt.h"
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/spinlock.h>
2b3dc93fedb4e72ac5b3cbaa89a9fc2f559be550vboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/asm.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/assert.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/err.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/mem.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/mp.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/thread.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include "internal/magics.h"
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/*******************************************************************************
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync* Structures and Typedefs *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync*******************************************************************************/
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/**
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Wrapper for the struct mutex type.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsynctypedef struct RTSPINLOCKINTERNAL
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync{
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /** Spinlock magic value (RTSPINLOCK_MAGIC). */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync uint32_t volatile u32Magic;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /** A Solaris spinlock. */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync kmutex_t Mtx;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#ifdef RT_MORE_STRICT
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /** The idAssertCpu variable before acquring the lock for asserting after
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * releasing the spinlock. */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RTCPUID volatile idAssertCpu;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /** The CPU that owns the lock. */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RTCPUID volatile idCpuOwner;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#endif
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsyncRTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync{
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /*
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Allocate.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RT_ASSERT_PREEMPTIBLE();
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync if (!pThis)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync return VERR_NO_MEMORY;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /*
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Initialize & return.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync pThis->u32Magic = RTSPINLOCK_MAGIC;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync mutex_init(&pThis->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *pSpinlock = pThis;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync return VINF_SUCCESS;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync}
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsyncRTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync{
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /*
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Validate input.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RT_ASSERT_INTS_ON();
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync if (!pThis)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync return VERR_INVALID_PARAMETER;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync VERR_INVALID_PARAMETER);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /*
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Make the lock invalid and release the memory.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync ASMAtomicIncU32(&pThis->u32Magic);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync mutex_destroy(&pThis->Mtx);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RTMemFree(pThis);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync return VINF_SUCCESS;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync}
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsyncRTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync{
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RT_ASSERT_PREEMPT_CPUID_VAR();
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync AssertPtr(pThis);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync pTmp->uFlags = ASMIntDisableFlags();
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync mutex_enter(&pThis->Mtx);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync Assert(!ASMIntAreEnabled());
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync}
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsyncRTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync{
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync AssertPtr(pThis);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
NOREF(pTmp);
mutex_exit(&pThis->Mtx);
ASMSetFlags(pTmp->uFlags);
RT_ASSERT_PREEMPT_CPUID();
}
RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
{
PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
RT_ASSERT_PREEMPT_CPUID_VAR();
AssertPtr(pThis);
Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
NOREF(pTmp);
#ifdef RT_STRICT
bool fIntsOn = ASMIntAreEnabled();
#endif
mutex_enter(&pThis->Mtx);
AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
}
RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
{
PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
AssertPtr(pThis);
Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
NOREF(pTmp);
#ifdef RT_STRICT
bool fIntsOn = ASMIntAreEnabled();
#endif
mutex_exit(&pThis->Mtx);
AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
RT_ASSERT_PREEMPT_CPUID();
}