1 | /* $Id: spinlock-r0drv-linux.c 28702 2010-04-25 00:36:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-linux-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 | #include <iprt/spinlock.h>
|
---|
38 |
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/asm.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/mem.h>
|
---|
43 | #include <iprt/mp.h>
|
---|
44 | #include <iprt/thread.h>
|
---|
45 | #include "internal/magics.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *******************************************************************************/
|
---|
51 | /**
|
---|
52 | * Wrapper for the spinlock_t structure.
|
---|
53 | */
|
---|
54 | typedef struct RTSPINLOCKINTERNAL
|
---|
55 | {
|
---|
56 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
57 | uint32_t volatile u32Magic;
|
---|
58 | /** The linux spinlock structure. */
|
---|
59 | spinlock_t Spinlock;
|
---|
60 | #ifdef RT_MORE_STRICT
|
---|
61 | /** The idAssertCpu variable before acquring the lock for asserting after
|
---|
62 | * releasing the spinlock. */
|
---|
63 | RTCPUID volatile idAssertCpu;
|
---|
64 | /** The CPU that owns the lock. */
|
---|
65 | RTCPUID volatile idCpuOwner;
|
---|
66 | #elif !defined(CONFIG_SMP) || defined(RT_ARCH_AMD64)
|
---|
67 | /** A placeholder on Uni systems so we won't piss off RTMemAlloc(). */
|
---|
68 | void *pvUniDummy;
|
---|
69 | #endif
|
---|
70 | #ifdef RT_STRICT
|
---|
71 | /** Set if we've seen any fNoIrqs usage.
|
---|
72 | * This must be consistent or it won't work you know. */
|
---|
73 | bool fNoIrqs;
|
---|
74 | #endif
|
---|
75 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Allocate.
|
---|
83 | */
|
---|
84 | PRTSPINLOCKINTERNAL pThis;
|
---|
85 | Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
86 | pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
87 | if (!pThis)
|
---|
88 | return VERR_NO_MEMORY;
|
---|
89 | /*
|
---|
90 | * Initialize and return.
|
---|
91 | */
|
---|
92 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
93 | #ifdef RT_MORE_STRICT
|
---|
94 | pThis->idCpuOwner = NIL_RTCPUID;
|
---|
95 | pThis->idAssertCpu = NIL_RTCPUID;
|
---|
96 | #endif
|
---|
97 | #ifdef RT_STRICT
|
---|
98 | pThis->fNoIrqs = false;
|
---|
99 | #endif
|
---|
100 | spin_lock_init(&pThis->Spinlock);
|
---|
101 |
|
---|
102 | *pSpinlock = pThis;
|
---|
103 | return VINF_SUCCESS;
|
---|
104 | }
|
---|
105 | RT_EXPORT_SYMBOL(RTSpinlockCreate);
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
109 | {
|
---|
110 | /*
|
---|
111 | * Validate input.
|
---|
112 | */
|
---|
113 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
114 | if (!pThis)
|
---|
115 | return VERR_INVALID_PARAMETER;
|
---|
116 | if (pThis->u32Magic != RTSPINLOCK_MAGIC)
|
---|
117 | {
|
---|
118 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
|
---|
119 | return VERR_INVALID_PARAMETER;
|
---|
120 | }
|
---|
121 |
|
---|
122 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
123 | RTMemFree(pThis);
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 | RT_EXPORT_SYMBOL(RTSpinlockDestroy);
|
---|
127 |
|
---|
128 |
|
---|
129 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
130 | {
|
---|
131 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
132 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
133 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
134 | ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
|
---|
135 |
|
---|
136 | spin_lock_irqsave(&pThis->Spinlock, pTmp->flFlags);
|
---|
137 |
|
---|
138 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
139 | #ifdef RT_STRICT
|
---|
140 | pThis->fNoIrqs = true;
|
---|
141 | #endif
|
---|
142 | }
|
---|
143 | RT_EXPORT_SYMBOL(RTSpinlockAcquireNoInts);
|
---|
144 |
|
---|
145 |
|
---|
146 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
147 | {
|
---|
148 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
149 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
150 |
|
---|
151 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
152 | ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
|
---|
153 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
154 | Assert(pThis->fNoIrqs);
|
---|
155 |
|
---|
156 | spin_unlock_irqrestore(&pThis->Spinlock, pTmp->flFlags);
|
---|
157 |
|
---|
158 | RT_ASSERT_PREEMPT_CPUID();
|
---|
159 | }
|
---|
160 | RT_EXPORT_SYMBOL(RTSpinlockReleaseNoInts);
|
---|
161 |
|
---|
162 |
|
---|
163 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
164 | {
|
---|
165 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
166 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
167 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
168 | ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
|
---|
169 | NOREF(pTmp);
|
---|
170 | Assert(!pThis->fNoIrqs || !ASMIntAreEnabled());
|
---|
171 |
|
---|
172 | spin_lock(&pThis->Spinlock);
|
---|
173 |
|
---|
174 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
175 | }
|
---|
176 | RT_EXPORT_SYMBOL(RTSpinlockAcquire);
|
---|
177 |
|
---|
178 |
|
---|
179 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
180 | {
|
---|
181 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
182 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
183 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
184 | ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
|
---|
185 | NOREF(pTmp);
|
---|
186 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
187 | Assert(!pThis->fNoIrqs || !ASMIntAreEnabled());
|
---|
188 |
|
---|
189 | spin_unlock(&pThis->Spinlock);
|
---|
190 |
|
---|
191 | RT_ASSERT_PREEMPT_CPUID();
|
---|
192 | }
|
---|
193 | RT_EXPORT_SYMBOL(RTSpinlockRelease);
|
---|
194 |
|
---|