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