1 | /* $Id: spinlock-r0drv-freebsd.c 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*********************************************************************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *********************************************************************************************************************************/
|
---|
35 | #include "the-freebsd-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/spinlock.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/alloc.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/asm.h>
|
---|
43 | #include <iprt/asm-amd64-x86.h>
|
---|
44 | #include <iprt/thread.h>
|
---|
45 | #include <iprt/mp.h>
|
---|
46 |
|
---|
47 | #include "internal/magics.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /**
|
---|
54 | * Wrapper for the struct mtx type.
|
---|
55 | */
|
---|
56 | typedef struct RTSPINLOCKINTERNAL
|
---|
57 | {
|
---|
58 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
59 | uint32_t volatile u32Magic;
|
---|
60 | /** The spinlock. */
|
---|
61 | uint32_t volatile fLocked;
|
---|
62 | /** Saved interrupt flag. */
|
---|
63 | uint32_t volatile fIntSaved;
|
---|
64 | /** The spinlock creation flags. */
|
---|
65 | uint32_t fFlags;
|
---|
66 | #ifdef RT_MORE_STRICT
|
---|
67 | /** The idAssertCpu variable before acquring the lock for asserting after
|
---|
68 | * releasing the spinlock. */
|
---|
69 | RTCPUID volatile idAssertCpu;
|
---|
70 | /** The CPU that owns the lock. */
|
---|
71 | RTCPUID volatile idCpuOwner;
|
---|
72 | #endif
|
---|
73 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
74 |
|
---|
75 |
|
---|
76 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
|
---|
77 | {
|
---|
78 | RT_ASSERT_PREEMPTIBLE();
|
---|
79 | AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Allocate.
|
---|
83 | */
|
---|
84 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
85 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
|
---|
86 | if (!pThis)
|
---|
87 | return VERR_NO_MEMORY;
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Initialize & return.
|
---|
91 | */
|
---|
92 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
93 | pThis->fLocked = 0;
|
---|
94 | pThis->fFlags = fFlags;
|
---|
95 | pThis->fIntSaved = 0;
|
---|
96 |
|
---|
97 | *pSpinlock = pThis;
|
---|
98 | return VINF_SUCCESS;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
103 | {
|
---|
104 | /*
|
---|
105 | * Validate input.
|
---|
106 | */
|
---|
107 | RT_ASSERT_INTS_ON();
|
---|
108 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
109 | if (!pThis)
|
---|
110 | return VERR_INVALID_PARAMETER;
|
---|
111 | AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
112 | ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
|
---|
113 | VERR_INVALID_PARAMETER);
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Make the lock invalid and release the memory.
|
---|
117 | */
|
---|
118 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
119 | RTMemFree(pThis);
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
125 | {
|
---|
126 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
127 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
128 | AssertPtr(pThis);
|
---|
129 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
130 |
|
---|
131 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
132 | {
|
---|
133 | for (;;)
|
---|
134 | {
|
---|
135 | uint32_t fIntSaved = ASMIntDisableFlags();
|
---|
136 | critical_enter();
|
---|
137 |
|
---|
138 | int c = 50;
|
---|
139 | for (;;)
|
---|
140 | {
|
---|
141 | if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
|
---|
142 | {
|
---|
143 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
144 | pThis->fIntSaved = fIntSaved;
|
---|
145 | return;
|
---|
146 | }
|
---|
147 | if (--c <= 0)
|
---|
148 | break;
|
---|
149 | cpu_spinwait();
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* Enable interrupts while we sleep. */
|
---|
153 | ASMSetFlags(fIntSaved);
|
---|
154 | critical_exit();
|
---|
155 | DELAY(1);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | for (;;)
|
---|
161 | {
|
---|
162 | critical_enter();
|
---|
163 |
|
---|
164 | int c = 50;
|
---|
165 | for (;;)
|
---|
166 | {
|
---|
167 | if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
|
---|
168 | {
|
---|
169 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
170 | return;
|
---|
171 | }
|
---|
172 | if (--c <= 0)
|
---|
173 | break;
|
---|
174 | cpu_spinwait();
|
---|
175 | }
|
---|
176 |
|
---|
177 | critical_exit();
|
---|
178 | DELAY(1);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
185 | {
|
---|
186 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
187 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
188 |
|
---|
189 | AssertPtr(pThis);
|
---|
190 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
191 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
192 |
|
---|
193 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
194 | {
|
---|
195 | uint32_t fIntSaved = pThis->fIntSaved;
|
---|
196 | pThis->fIntSaved = 0;
|
---|
197 | if (ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
|
---|
198 | ASMSetFlags(fIntSaved);
|
---|
199 | else
|
---|
200 | AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
|
---|
201 | }
|
---|
202 | else
|
---|
203 | {
|
---|
204 | if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
|
---|
205 | AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
|
---|
206 | }
|
---|
207 |
|
---|
208 | critical_exit();
|
---|
209 | }
|
---|
210 |
|
---|