1 | /* $Id: spinlock-r0drv-solaris.c 40149 2012-02-16 14:07:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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/err.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 | /** A Solaris spinlock. */
|
---|
58 | kmutex_t Mtx;
|
---|
59 | #ifdef RT_MORE_STRICT
|
---|
60 | /** The idAssertCpu variable before acquring the lock for asserting after
|
---|
61 | * releasing the spinlock. */
|
---|
62 | RTCPUID volatile idAssertCpu;
|
---|
63 | /** The CPU that owns the lock. */
|
---|
64 | RTCPUID volatile idCpuOwner;
|
---|
65 | #endif
|
---|
66 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Allocate.
|
---|
74 | */
|
---|
75 | RT_ASSERT_PREEMPTIBLE();
|
---|
76 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
77 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
78 | if (!pThis)
|
---|
79 | return VERR_NO_MEMORY;
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Initialize & return.
|
---|
83 | */
|
---|
84 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
85 | mutex_init(&pThis->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(PIL_MAX));
|
---|
86 | *pSpinlock = pThis;
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
92 | {
|
---|
93 | /*
|
---|
94 | * Validate input.
|
---|
95 | */
|
---|
96 | RT_ASSERT_INTS_ON();
|
---|
97 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
98 | if (!pThis)
|
---|
99 | return VERR_INVALID_PARAMETER;
|
---|
100 | AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
101 | ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
|
---|
102 | VERR_INVALID_PARAMETER);
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Make the lock invalid and release the memory.
|
---|
106 | */
|
---|
107 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
108 | mutex_destroy(&pThis->Mtx);
|
---|
109 | RTMemFree(pThis);
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
115 | {
|
---|
116 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
117 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
118 |
|
---|
119 | AssertPtr(pThis);
|
---|
120 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
121 |
|
---|
122 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
123 | pTmp->uFlags = ASMIntDisableFlags();
|
---|
124 | #else
|
---|
125 | pTmp->uFlags = 0;
|
---|
126 | #endif
|
---|
127 | mutex_enter(&pThis->Mtx);
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Solaris 10 doesn't preserve the interrupt flag, but since we're at PIL_MAX we should be
|
---|
131 | * fine and not get interrupts while lock is held. Re-disable interrupts to not upset
|
---|
132 | * assertions & assumptions callers might have.
|
---|
133 | */
|
---|
134 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
135 | ASMIntDisable();
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
139 | Assert(!ASMIntAreEnabled());
|
---|
140 | #endif
|
---|
141 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
146 | {
|
---|
147 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
148 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
149 |
|
---|
150 | AssertPtr(pThis);
|
---|
151 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
152 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
153 | NOREF(pTmp);
|
---|
154 |
|
---|
155 | mutex_exit(&pThis->Mtx);
|
---|
156 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
157 | ASMSetFlags(pTmp->uFlags);
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | RT_ASSERT_PREEMPT_CPUID();
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
165 | {
|
---|
166 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
167 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
168 | AssertPtr(pThis);
|
---|
169 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
170 | NOREF(pTmp);
|
---|
171 | #if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
|
---|
172 | bool fIntsOn = ASMIntAreEnabled();
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | mutex_enter(&pThis->Mtx);
|
---|
176 |
|
---|
177 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
178 | AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
186 | {
|
---|
187 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
188 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
189 |
|
---|
190 | AssertPtr(pThis);
|
---|
191 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
192 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
193 | NOREF(pTmp);
|
---|
194 | #if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
|
---|
195 | bool fIntsOn = ASMIntAreEnabled();
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | mutex_exit(&pThis->Mtx);
|
---|
199 |
|
---|
200 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
201 | AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
|
---|
202 | #endif
|
---|
203 | RT_ASSERT_PREEMPT_CPUID();
|
---|
204 | }
|
---|
205 |
|
---|