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