spinlock-r0drv-solaris.c revision 1d398d1aa90bdac2b68d4f434283746c5838a96d
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;
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 *));
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync if (!pSpinlockInt)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync return VERR_NO_MEMORY;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /*
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Initialize & return.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
f12e977924d891da49dfcaefc8bb282d11699bd0vboxsync mutex_init(&pSpinlockInt->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync *pSpinlock = pSpinlockInt;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync return VINF_SUCCESS;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync}
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsyncRTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync{
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /*
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Validate input.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync */
9c11b89c71ca727d975c39f2719063501ddcd03dvboxsync RT_ASSERT_INTS_ON();
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync if (!pSpinlockInt)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync return VERR_INVALID_PARAMETER;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync VERR_INVALID_PARAMETER);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /*
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Make the lock invalid and release the memory.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync ASMAtomicIncU32(&pSpinlockInt->u32Magic);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync mutex_destroy(&pSpinlockInt->Mtx);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync RTMemFree(pSpinlockInt);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync return VINF_SUCCESS;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync}
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsyncRTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync{
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync AssertPtr(pSpinlockInt);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
ad81f667a6be22bee5e7628bf8cfb849723734c6vboxsync pTmp->uFlags = ASMIntDisableFlags();
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync {
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync RT_ASSERT_PREEMPT_CPUID_VAR();
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync mutex_enter(&pSpinlockInt->Mtx);
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync RT_ASSERT_PREEMPT_CPUID();
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync }
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync}
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsyncRTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync{
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync AssertPtr(pSpinlockInt);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync mutex_exit(&pSpinlockInt->Mtx);
ad81f667a6be22bee5e7628bf8cfb849723734c6vboxsync ASMSetFlags(pTmp->uFlags);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync}
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsyncRTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync{
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync RT_ASSERT_PREEMPT_CPUID_VAR();
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync AssertPtr(pSpinlockInt);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync NOREF(pTmp);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync mutex_enter(&pSpinlockInt->Mtx);
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync#ifdef RT_MORE_STRICT
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync {
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync RTCPUID const idAssertCpuNow = RTMpCpuId(); /* Spinlocks are not preemptible, so we cannot be rescheduled. */
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync AssertMsg(idAssertCpu == idAssertCpuNow || idAssertCpu == NIL_RTCPUID, ("%#x, %#x\n", idAssertCpu, idAssertCpuNow));
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync pTmp->uFlags = idAssertCpuNow;
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync }
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync#endif
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync}
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsyncRTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync{
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync#ifdef RT_MORE_STRICT
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync RTCPUID const idAssertCpu = pTmp->uFlags;
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync pTmp->uFlags = 0;
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync RT_ASSERT_PREEMPT_CPUID();
f85630691753e2acd5099c3079b3e6d40c049a62vboxsync#endif
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync AssertPtr(pSpinlockInt);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync NOREF(pTmp);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync mutex_exit(&pSpinlockInt->Mtx);
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync#ifdef RT_MORE_STRICT
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync RT_ASSERT_PREEMPT_CPUID();
1d398d1aa90bdac2b68d4f434283746c5838a96dvboxsync#endif
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync}
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync