spinlock-r0drv-freebsd.c revision 3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7cca
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync/* $Id$ */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Spinlocks, Ring-0 Driver, FreeBSD.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync/*
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync *
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Permission is hereby granted, free of charge, to any person
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * obtaining a copy of this software and associated documentation
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * files (the "Software"), to deal in the Software without
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * restriction, including without limitation the rights to use,
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * copy, modify, merge, publish, distribute, sublicense, and/or sell
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * copies of the Software, and to permit persons to whom the
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Software is furnished to do so, subject to the following
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * conditions:
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync *
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * The above copyright notice and this permission notice shall be
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * included in all copies or substantial portions of the Software.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync *
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * OTHER DEALINGS IN THE SOFTWARE.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync/*******************************************************************************
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync* Header Files *
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync*******************************************************************************/
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync#include "the-freebsd-kernel.h"
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync#include "internal/iprt.h"
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync#include <iprt/spinlock.h>
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync#include <iprt/err.h>
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync#include <iprt/alloc.h>
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync#include <iprt/assert.h>
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync#include <iprt/asm.h>
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync#include <iprt/thread.h>
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync#include <iprt/mp.h>
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync#include "internal/magics.h"
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync/*******************************************************************************
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync* Structures and Typedefs *
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync*******************************************************************************/
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync/**
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Wrapper for the struct mtx type.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsynctypedef struct RTSPINLOCKINTERNAL
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync{
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync /** Spinlock magic value (RTSPINLOCK_MAGIC). */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync uint32_t volatile u32Magic;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /** The spinlock. */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync uint32_t volatile fLocked;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /** Reserved to satisfy compile assertion below. */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync uint32_t uReserved;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync#ifdef RT_MORE_STRICT
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /** The idAssertCpu variable before acquring the lock for asserting after
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync * releasing the spinlock. */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RTCPUID volatile idAssertCpu;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /** The CPU that owns the lock. */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RTCPUID volatile idCpuOwner;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync#endif
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync{
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync /*
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Allocate.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPTIBLE();
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
24b814713d57519124d491243137f551be686c66vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pSpinlockInt));
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync if (!pSpinlockInt)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync return VERR_NO_MEMORY;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync /*
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Initialize & return.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync pSpinlockInt->fLocked = 0;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync *pSpinlock = pSpinlockInt;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync return VINF_SUCCESS;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync}
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync{
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync /*
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Validate input.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_INTS_ON();
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync if (!pSpinlockInt)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync return VERR_INVALID_PARAMETER;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync VERR_INVALID_PARAMETER);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync /*
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Make the lock invalid and release the memory.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync ASMAtomicIncU32(&pSpinlockInt->u32Magic);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync RTMemFree(pSpinlockInt);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync return VINF_SUCCESS;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync}
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync{
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync AssertPtr(pSpinlockInt);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_VAR();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync for (;;)
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync {
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync pTmp->uFlags = ASMGetFlags();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync ASMIntDisable();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync critical_enter();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync for (int c = 50; c > 0; c--)
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync {
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pSpinlockInt);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync return;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync }
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync else
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync cpu_spinwait();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /* Enable interrupts while we sleep. */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync ASMSetFlags(pTmp->uFlags);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync critical_exit();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync DELAY(1);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync }
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync}
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync{
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync AssertPtr(pSpinlockInt);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pSpinlockInt);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync NOREF(pTmp);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync ASMSetFlags(pTmp->uFlags);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync critical_exit();
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync}
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync{
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_VAR();
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync AssertPtr(pSpinlockInt);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync NOREF(pTmp);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync for (;;)
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync {
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync critical_enter();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync for (int c = 50; c > 0; c--)
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync {
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pSpinlockInt);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync return;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync }
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync else
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync cpu_spinwait();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync critical_exit();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync DELAY(1);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync }
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync}
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync{
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync AssertPtr(pSpinlockInt);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pSpinlockInt);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync NOREF(pTmp);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync critical_exit();
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync}
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync