1 | /* $Id: spinlock-r0drv-solaris.c 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
8 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
22 | *
|
---|
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.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "the-solaris-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/spinlock.h>
|
---|
34 |
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
37 | # include <iprt/asm-amd64-x86.h>
|
---|
38 | #endif
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/errcore.h>
|
---|
41 | #include <iprt/mem.h>
|
---|
42 | #include <iprt/mp.h>
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include "internal/magics.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Structures and Typedefs *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | /**
|
---|
51 | * Wrapper for the struct mutex type.
|
---|
52 | */
|
---|
53 | typedef struct RTSPINLOCKINTERNAL
|
---|
54 | {
|
---|
55 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
56 | uint32_t volatile u32Magic;
|
---|
57 | /** Spinlock creation flags. */
|
---|
58 | uint32_t fFlags;
|
---|
59 | /** Saved interrupt flag. */
|
---|
60 | uint32_t volatile fIntSaved;
|
---|
61 | /** A Solaris spinlock. */
|
---|
62 | kmutex_t Mtx;
|
---|
63 | #ifdef RT_MORE_STRICT
|
---|
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;
|
---|
69 | #endif
|
---|
70 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
|
---|
75 | {
|
---|
76 | RT_ASSERT_PREEMPTIBLE();
|
---|
77 | AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Allocate.
|
---|
81 | */
|
---|
82 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
83 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
84 | if (!pThis)
|
---|
85 | return VERR_NO_MEMORY;
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Initialize & return.
|
---|
89 | */
|
---|
90 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
91 | pThis->fFlags = fFlags;
|
---|
92 | pThis->fIntSaved = 0;
|
---|
93 | /** @todo Consider different PIL when not interrupt safe requirement. */
|
---|
94 | mutex_init(&pThis->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(PIL_MAX));
|
---|
95 | *pSpinlock = pThis;
|
---|
96 | return VINF_SUCCESS;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
101 | {
|
---|
102 | /*
|
---|
103 | * Validate input.
|
---|
104 | */
|
---|
105 | RT_ASSERT_INTS_ON();
|
---|
106 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
107 | if (!pThis)
|
---|
108 | return VERR_INVALID_PARAMETER;
|
---|
109 | AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
110 | ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
|
---|
111 | VERR_INVALID_PARAMETER);
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * Make the lock invalid and release the memory.
|
---|
115 | */
|
---|
116 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
117 | mutex_destroy(&pThis->Mtx);
|
---|
118 | RTMemFree(pThis);
|
---|
119 | return VINF_SUCCESS;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
124 | {
|
---|
125 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
126 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
127 | AssertPtr(pThis);
|
---|
128 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
129 |
|
---|
130 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
131 | {
|
---|
132 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
133 | uint32_t fIntSaved = ASMIntDisableFlags();
|
---|
134 | #endif
|
---|
135 | mutex_enter(&pThis->Mtx);
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Solaris 10 doesn't preserve the interrupt flag, but since we're at PIL_MAX we should be
|
---|
139 | * fine and not get interrupts while lock is held. Re-disable interrupts to not upset
|
---|
140 | * assertions & assumptions callers might have.
|
---|
141 | */
|
---|
142 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
143 | ASMIntDisable();
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
147 | Assert(!ASMIntAreEnabled());
|
---|
148 | #endif
|
---|
149 | pThis->fIntSaved = fIntSaved;
|
---|
150 | }
|
---|
151 | else
|
---|
152 | {
|
---|
153 | #if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
|
---|
154 | bool fIntsOn = ASMIntAreEnabled();
|
---|
155 | #endif
|
---|
156 |
|
---|
157 | mutex_enter(&pThis->Mtx);
|
---|
158 |
|
---|
159 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
160 | AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
|
---|
161 | #endif
|
---|
162 | }
|
---|
163 |
|
---|
164 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
169 | {
|
---|
170 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
171 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
172 |
|
---|
173 | AssertPtr(pThis);
|
---|
174 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
175 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
176 |
|
---|
177 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
178 | {
|
---|
179 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
180 | uint32_t fIntSaved = pThis->fIntSaved;
|
---|
181 | pThis->fIntSaved = 0;
|
---|
182 | #endif
|
---|
183 | mutex_exit(&pThis->Mtx);
|
---|
184 |
|
---|
185 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
186 | ASMSetFlags(fIntSaved);
|
---|
187 | #endif
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | #if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
|
---|
192 | bool fIntsOn = ASMIntAreEnabled();
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | mutex_exit(&pThis->Mtx);
|
---|
196 |
|
---|
197 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
198 | AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
|
---|
199 | #endif
|
---|
200 | }
|
---|
201 |
|
---|
202 | RT_ASSERT_PREEMPT_CPUID();
|
---|
203 | }
|
---|
204 |
|
---|