VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/spinlock-r0drv-solaris.c

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: spinlock-r0drv-solaris.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "the-solaris-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/spinlock.h>
44
45#include <iprt/asm.h>
46#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
47# include <iprt/asm-amd64-x86.h>
48#endif
49#include <iprt/assert.h>
50#include <iprt/errcore.h>
51#include <iprt/mem.h>
52#include <iprt/mp.h>
53#include <iprt/thread.h>
54#include "internal/magics.h"
55
56
57/*********************************************************************************************************************************
58* Structures and Typedefs *
59*********************************************************************************************************************************/
60/**
61 * Wrapper for the struct mutex type.
62 */
63typedef struct RTSPINLOCKINTERNAL
64{
65 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
66 uint32_t volatile u32Magic;
67 /** Spinlock creation flags. */
68 uint32_t fFlags;
69 /** Saved interrupt flag. */
70 uint32_t volatile fIntSaved;
71 /** A Solaris spinlock. */
72 kmutex_t Mtx;
73#ifdef RT_MORE_STRICT
74 /** The idAssertCpu variable before acquring the lock for asserting after
75 * releasing the spinlock. */
76 RTCPUID volatile idAssertCpu;
77 /** The CPU that owns the lock. */
78 RTCPUID volatile idCpuOwner;
79#endif
80} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
81
82
83
84RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
85{
86 RT_ASSERT_PREEMPTIBLE();
87 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
88
89 /*
90 * Allocate.
91 */
92 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
93 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
94 if (!pThis)
95 return VERR_NO_MEMORY;
96
97 /*
98 * Initialize & return.
99 */
100 pThis->u32Magic = RTSPINLOCK_MAGIC;
101 pThis->fFlags = fFlags;
102 pThis->fIntSaved = 0;
103 /** @todo Consider different PIL when not interrupt safe requirement. */
104 mutex_init(&pThis->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(PIL_MAX));
105 *pSpinlock = pThis;
106 return VINF_SUCCESS;
107}
108
109
110RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
111{
112 /*
113 * Validate input.
114 */
115 RT_ASSERT_INTS_ON();
116 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
117 if (!pThis)
118 return VERR_INVALID_PARAMETER;
119 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
120 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
121 VERR_INVALID_PARAMETER);
122
123 /*
124 * Make the lock invalid and release the memory.
125 */
126 ASMAtomicIncU32(&pThis->u32Magic);
127 mutex_destroy(&pThis->Mtx);
128 RTMemFree(pThis);
129 return VINF_SUCCESS;
130}
131
132
133RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
134{
135 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
136 RT_ASSERT_PREEMPT_CPUID_VAR();
137 AssertPtr(pThis);
138 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
139
140 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
141 {
142#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
143 uint32_t fIntSaved = ASMIntDisableFlags();
144#endif
145 mutex_enter(&pThis->Mtx);
146
147 /*
148 * Solaris 10 doesn't preserve the interrupt flag, but since we're at PIL_MAX we should be
149 * fine and not get interrupts while lock is held. Re-disable interrupts to not upset
150 * assertions & assumptions callers might have.
151 */
152#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
153 ASMIntDisable();
154#endif
155
156#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
157 Assert(!ASMIntAreEnabled());
158#endif
159 pThis->fIntSaved = fIntSaved;
160 }
161 else
162 {
163#if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
164 bool fIntsOn = ASMIntAreEnabled();
165#endif
166
167 mutex_enter(&pThis->Mtx);
168
169#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
170 AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
171#endif
172 }
173
174 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
175}
176
177
178RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
179{
180 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
181 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
182
183 AssertPtr(pThis);
184 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
185 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
186
187 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
188 {
189#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
190 uint32_t fIntSaved = pThis->fIntSaved;
191 pThis->fIntSaved = 0;
192#endif
193 mutex_exit(&pThis->Mtx);
194
195#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
196 ASMSetFlags(fIntSaved);
197#endif
198 }
199 else
200 {
201#if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
202 bool fIntsOn = ASMIntAreEnabled();
203#endif
204
205 mutex_exit(&pThis->Mtx);
206
207#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
208 AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
209#endif
210 }
211
212 RT_ASSERT_PREEMPT_CPUID();
213}
214
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use