VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/spinlock-r0drv-freebsd.c@ 99743

Last change on this file since 99743 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: spinlock-r0drv-freebsd.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Contributed by knut st. osmundsen.
8 *
9 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
10 *
11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.virtualbox.org.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation, in version 3 of the
17 * License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <https://www.gnu.org/licenses>.
26 *
27 * The contents of this file may alternatively be used under the terms
28 * of the Common Development and Distribution License Version 1.0
29 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
30 * in the VirtualBox distribution, in which case the provisions of the
31 * CDDL are applicable instead of those of the GPL.
32 *
33 * You may elect to license modified versions of this file under the
34 * terms and conditions of either the GPL or the CDDL or both.
35 *
36 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
37 * --------------------------------------------------------------------
38 *
39 * This code is based on:
40 *
41 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
42 *
43 * Permission is hereby granted, free of charge, to any person
44 * obtaining a copy of this software and associated documentation
45 * files (the "Software"), to deal in the Software without
46 * restriction, including without limitation the rights to use,
47 * copy, modify, merge, publish, distribute, sublicense, and/or sell
48 * copies of the Software, and to permit persons to whom the
49 * Software is furnished to do so, subject to the following
50 * conditions:
51 *
52 * The above copyright notice and this permission notice shall be
53 * included in all copies or substantial portions of the Software.
54 *
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
57 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
58 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
59 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
60 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
61 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
62 * OTHER DEALINGS IN THE SOFTWARE.
63 */
64
65
66/*********************************************************************************************************************************
67* Header Files *
68*********************************************************************************************************************************/
69#include "the-freebsd-kernel.h"
70#include "internal/iprt.h"
71
72#include <iprt/spinlock.h>
73#include <iprt/errcore.h>
74#include <iprt/alloc.h>
75#include <iprt/assert.h>
76#include <iprt/asm.h>
77#include <iprt/asm-amd64-x86.h>
78#include <iprt/thread.h>
79#include <iprt/mp.h>
80
81#include "internal/magics.h"
82
83
84/*********************************************************************************************************************************
85* Structures and Typedefs *
86*********************************************************************************************************************************/
87/**
88 * Wrapper for the struct mtx type.
89 */
90typedef struct RTSPINLOCKINTERNAL
91{
92 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
93 uint32_t volatile u32Magic;
94 /** The spinlock. */
95 uint32_t volatile fLocked;
96 /** Saved interrupt flag. */
97 uint32_t volatile fIntSaved;
98 /** The spinlock creation flags. */
99 uint32_t fFlags;
100#ifdef RT_MORE_STRICT
101 /** The idAssertCpu variable before acquring the lock for asserting after
102 * releasing the spinlock. */
103 RTCPUID volatile idAssertCpu;
104 /** The CPU that owns the lock. */
105 RTCPUID volatile idCpuOwner;
106#endif
107} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
108
109
110RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
111{
112 RT_ASSERT_PREEMPTIBLE();
113 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
114
115 /*
116 * Allocate.
117 */
118 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
119 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
120 if (!pThis)
121 return VERR_NO_MEMORY;
122
123 /*
124 * Initialize & return.
125 */
126 pThis->u32Magic = RTSPINLOCK_MAGIC;
127 pThis->fLocked = 0;
128 pThis->fFlags = fFlags;
129 pThis->fIntSaved = 0;
130
131 *pSpinlock = pThis;
132 return VINF_SUCCESS;
133}
134
135
136RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
137{
138 /*
139 * Validate input.
140 */
141 RT_ASSERT_INTS_ON();
142 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
143 if (!pThis)
144 return VERR_INVALID_PARAMETER;
145 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
146 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
147 VERR_INVALID_PARAMETER);
148
149 /*
150 * Make the lock invalid and release the memory.
151 */
152 ASMAtomicIncU32(&pThis->u32Magic);
153 RTMemFree(pThis);
154 return VINF_SUCCESS;
155}
156
157
158RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
159{
160 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
161 RT_ASSERT_PREEMPT_CPUID_VAR();
162 AssertPtr(pThis);
163 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
164
165 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
166 {
167 for (;;)
168 {
169 uint32_t fIntSaved = ASMIntDisableFlags();
170 critical_enter();
171
172 int c = 50;
173 for (;;)
174 {
175 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
176 {
177 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
178 pThis->fIntSaved = fIntSaved;
179 return;
180 }
181 if (--c <= 0)
182 break;
183 cpu_spinwait();
184 }
185
186 /* Enable interrupts while we sleep. */
187 ASMSetFlags(fIntSaved);
188 critical_exit();
189 DELAY(1);
190 }
191 }
192 else
193 {
194 for (;;)
195 {
196 critical_enter();
197
198 int c = 50;
199 for (;;)
200 {
201 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
202 {
203 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
204 return;
205 }
206 if (--c <= 0)
207 break;
208 cpu_spinwait();
209 }
210
211 critical_exit();
212 DELAY(1);
213 }
214 }
215}
216
217
218RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
219{
220 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
221 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
222
223 AssertPtr(pThis);
224 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
225 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
226
227 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
228 {
229 uint32_t fIntSaved = pThis->fIntSaved;
230 pThis->fIntSaved = 0;
231 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
232 ASMSetFlags(fIntSaved);
233 else
234 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
235 }
236 else
237 {
238 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
239 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
240 }
241
242 critical_exit();
243}
244
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette