1 /* $Id: spinlock-r0drv-linux.c $ */
3 * IPRT - Spinlocks, Ring-0 Driver, Linux.
7 * Copyright (C) 2006-2016 Oracle Corporation
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
28 /*********************************************************************************************************************************
30 *********************************************************************************************************************************/
31 #include "the-linux-kernel.h"
32 #include "internal/iprt.h"
33 #include <iprt/spinlock.h>
36 #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
37 # include <iprt/asm-amd64-x86.h>
39 #include <iprt/assert.h>
43 #include <iprt/thread.h>
44 #include "internal/magics.h"
47 /*********************************************************************************************************************************
48 * Structures and Typedefs *
49 *********************************************************************************************************************************/
51 * Wrapper for the spinlock_t structure.
53 typedef struct RTSPINLOCKINTERNAL
55 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
56 uint32_t volatile u32Magic
;
57 /** The spinlock creation flags. */
59 /** The saved interrupt flag. */
60 unsigned long volatile fIntSaved
;
61 /** The linux spinlock structure. */
64 /** The idAssertCpu variable before acquring the lock for asserting after
65 * releasing the spinlock. */
66 RTCPUID
volatile idAssertCpu
;
67 /** The CPU that owns the lock. */
68 RTCPUID
volatile idCpuOwner
;
70 } RTSPINLOCKINTERNAL
, *PRTSPINLOCKINTERNAL
;
74 RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock
, uint32_t fFlags
, const char *pszName
)
76 IPRT_LINUX_SAVE_EFL_AC();
77 PRTSPINLOCKINTERNAL pThis
;
78 AssertReturn(fFlags
== RTSPINLOCK_FLAGS_INTERRUPT_SAFE
|| fFlags
== RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE
, VERR_INVALID_PARAMETER
);
84 Assert(sizeof(RTSPINLOCKINTERNAL
) > sizeof(void *));
85 pThis
= (PRTSPINLOCKINTERNAL
)RTMemAlloc(sizeof(*pThis
));
87 return VERR_NO_MEMORY
;
89 * Initialize and return.
91 pThis
->u32Magic
= RTSPINLOCK_MAGIC
;
92 pThis
->fFlags
= fFlags
;
95 pThis
->idCpuOwner
= NIL_RTCPUID
;
96 pThis
->idAssertCpu
= NIL_RTCPUID
;
99 spin_lock_init(&pThis
->Spinlock
);
102 IPRT_LINUX_RESTORE_EFL_AC();
105 RT_EXPORT_SYMBOL(RTSpinlockCreate
);
108 RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock
)
113 PRTSPINLOCKINTERNAL pThis
= (PRTSPINLOCKINTERNAL
)Spinlock
;
115 return VERR_INVALID_PARAMETER
;
116 if (pThis
->u32Magic
!= RTSPINLOCK_MAGIC
)
118 AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis
, pThis
->u32Magic
));
119 return VERR_INVALID_PARAMETER
;
122 ASMAtomicIncU32(&pThis
->u32Magic
);
126 RT_EXPORT_SYMBOL(RTSpinlockDestroy
);
129 RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock
)
131 PRTSPINLOCKINTERNAL pThis
= (PRTSPINLOCKINTERNAL
)Spinlock
;
132 IPRT_LINUX_SAVE_EFL_AC();
133 RT_ASSERT_PREEMPT_CPUID_VAR();
134 AssertMsg(pThis
&& pThis
->u32Magic
== RTSPINLOCK_MAGIC
,
135 ("pThis=%p u32Magic=%08x\n", pThis
, pThis
? (int)pThis
->u32Magic
: 0));
137 #ifdef CONFIG_PROVE_LOCKING
140 if (pThis
->fFlags
& RTSPINLOCK_FLAGS_INTERRUPT_SAFE
)
142 unsigned long fIntSaved
;
143 spin_lock_irqsave(&pThis
->Spinlock
, fIntSaved
);
144 pThis
->fIntSaved
= fIntSaved
;
147 spin_lock(&pThis
->Spinlock
);
148 #ifdef CONFIG_PROVE_LOCKING
152 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
153 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis
);
155 RT_EXPORT_SYMBOL(RTSpinlockAcquire
);
158 RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock
)
160 PRTSPINLOCKINTERNAL pThis
= (PRTSPINLOCKINTERNAL
)Spinlock
;
161 IPRT_LINUX_SAVE_EFL_AC(); /* spin_unlock* may preempt and trash eflags.ac. */
162 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
163 AssertMsg(pThis
&& pThis
->u32Magic
== RTSPINLOCK_MAGIC
,
164 ("pThis=%p u32Magic=%08x\n", pThis
, pThis
? (int)pThis
->u32Magic
: 0));
165 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis
);
167 #ifdef CONFIG_PROVE_LOCKING
170 if (pThis
->fFlags
& RTSPINLOCK_FLAGS_INTERRUPT_SAFE
)
172 unsigned long fIntSaved
= pThis
->fIntSaved
;
173 pThis
->fIntSaved
= 0;
174 spin_unlock_irqrestore(&pThis
->Spinlock
, fIntSaved
);
177 spin_unlock(&pThis
->Spinlock
);
178 #ifdef CONFIG_PROVE_LOCKING
182 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
183 RT_ASSERT_PREEMPT_CPUID();
185 RT_EXPORT_SYMBOL(RTSpinlockRelease
);