1 | /* $Id: spinlock-r0drv-nt.cpp 48935 2013-10-07 21:19:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 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-nt-kernel.h"
|
---|
32 |
|
---|
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 |
|
---|
43 | #include "internal/magics.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *******************************************************************************/
|
---|
49 | /** Apply the NoIrq hack if defined. */
|
---|
50 | #define RTSPINLOCK_NT_HACK_NOIRQ
|
---|
51 |
|
---|
52 | #ifdef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
53 | /** Indicates that the spinlock is taken. */
|
---|
54 | # define RTSPINLOCK_NT_HACK_NOIRQ_TAKEN UINT32(0x00c0ffee)
|
---|
55 | /** Indicates that the spinlock is taken. */
|
---|
56 | # define RTSPINLOCK_NT_HACK_NOIRQ_FREE UINT32(0xfe0000fe)
|
---|
57 | #endif
|
---|
58 |
|
---|
59 |
|
---|
60 | /*******************************************************************************
|
---|
61 | * Structures and Typedefs *
|
---|
62 | *******************************************************************************/
|
---|
63 | /**
|
---|
64 | * Wrapper for the KSPIN_LOCK type.
|
---|
65 | */
|
---|
66 | typedef struct RTSPINLOCKINTERNAL
|
---|
67 | {
|
---|
68 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
69 | uint32_t volatile u32Magic;
|
---|
70 | #ifdef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
71 | /** Spinlock hack. */
|
---|
72 | uint32_t volatile u32Hack;
|
---|
73 | #endif
|
---|
74 | /** The saved IRQL. */
|
---|
75 | KIRQL volatile SavedIrql;
|
---|
76 | /** The saved interrupt flag. */
|
---|
77 | RTCCUINTREG volatile fIntSaved;
|
---|
78 | /** The spinlock creation flags. */
|
---|
79 | uint32_t fFlags;
|
---|
80 | /** The NT spinlock structure. */
|
---|
81 | KSPIN_LOCK Spinlock;
|
---|
82 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
83 |
|
---|
84 |
|
---|
85 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
|
---|
86 | {
|
---|
87 | AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Allocate.
|
---|
91 | */
|
---|
92 | Assert(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 | #ifdef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
102 | pThis->u32Hack = RTSPINLOCK_NT_HACK_NOIRQ_FREE;
|
---|
103 | #endif
|
---|
104 | pThis->SavedIrql = 0;
|
---|
105 | pThis->fIntSaved = 0;
|
---|
106 | pThis->fFlags = fFlags;
|
---|
107 | KeInitializeSpinLock(&pThis->Spinlock);
|
---|
108 |
|
---|
109 | *pSpinlock = pThis;
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
115 | {
|
---|
116 | /*
|
---|
117 | * Validate input.
|
---|
118 | */
|
---|
119 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
120 | if (!pThis)
|
---|
121 | return VERR_INVALID_PARAMETER;
|
---|
122 | if (pThis->u32Magic != RTSPINLOCK_MAGIC)
|
---|
123 | {
|
---|
124 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
|
---|
125 | return VERR_INVALID_PARAMETER;
|
---|
126 | }
|
---|
127 |
|
---|
128 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
129 | RTMemFree(pThis);
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
135 | {
|
---|
136 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
137 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pThis->u32Magic));
|
---|
138 |
|
---|
139 | KIRQL SavedIrql;
|
---|
140 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
141 | {
|
---|
142 | #ifndef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
143 | RTCCUINTREG fIntSaved = ASMGetFlags();
|
---|
144 | ASMIntDisable();
|
---|
145 | KeAcquireSpinLock(&pThis->Spinlock, &SavedIrql);
|
---|
146 | #else
|
---|
147 | SavedIrql = KeGetCurrentIrql();
|
---|
148 | if (SavedIrql < DISPATCH_LEVEL)
|
---|
149 | {
|
---|
150 | KeRaiseIrql(DISPATCH_LEVEL, &SavedIrql);
|
---|
151 | Assert(SavedIrql < DISPATCH_LEVEL);
|
---|
152 | }
|
---|
153 | RTCCUINTREG fIntSaved = ASMGetFlags();
|
---|
154 | ASMIntDisable();
|
---|
155 |
|
---|
156 | if (!ASMAtomicCmpXchgU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
|
---|
157 | {
|
---|
158 | while (!ASMAtomicCmpXchgU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
|
---|
159 | ASMNopPause();
|
---|
160 | }
|
---|
161 |
|
---|
162 | pThis->fIntSaved = fIntSaved;
|
---|
163 | #endif
|
---|
164 | }
|
---|
165 | else
|
---|
166 | KeAcquireSpinLock(&pThis->Spinlock, &SavedIrql);
|
---|
167 | pThis->SavedIrql = SavedIrql;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
172 | {
|
---|
173 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
174 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pThis->u32Magic));
|
---|
175 |
|
---|
176 | KIRQL SavedIrql = pThis->SavedIrql;
|
---|
177 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
178 | {
|
---|
179 | RTCCUINTREG fIntSaved = pThis->fIntSaved;
|
---|
180 | pThis->fIntSaved = 0;
|
---|
181 |
|
---|
182 | #ifndef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
183 | KeReleaseSpinLock(&pThis->Spinlock, SavedIrql);
|
---|
184 | ASMSetFlags(fIntSaved);
|
---|
185 | #else
|
---|
186 | Assert(pThis->u32Hack == RTSPINLOCK_NT_HACK_NOIRQ_TAKEN);
|
---|
187 |
|
---|
188 | ASMAtomicWriteU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_FREE);
|
---|
189 | ASMSetFlags(fIntSaved);
|
---|
190 | if (SavedIrql < DISPATCH_LEVEL)
|
---|
191 | KeLowerIrql(SavedIrql);
|
---|
192 | #endif
|
---|
193 | }
|
---|
194 | else
|
---|
195 | KeReleaseSpinLock(&pThis->Spinlock, SavedIrql);
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock)
|
---|
200 | {
|
---|
201 | #if 1
|
---|
202 | if (RT_UNLIKELY(!(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)))
|
---|
203 | RTAssertMsg2("RTSpinlockReleaseNoInts: %p (magic=%#x)\n", Spinlock, Spinlock->u32Magic);
|
---|
204 | #else
|
---|
205 | AssertRelease(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE);
|
---|
206 | #endif
|
---|
207 | RTSpinlockRelease(Spinlock);
|
---|
208 | }
|
---|
209 |
|
---|