1 | /* $Id: spinlock-r0drv-nt.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "the-nt-kernel.h"
|
---|
42 |
|
---|
43 | #include <iprt/spinlock.h>
|
---|
44 |
|
---|
45 | #include <iprt/asm.h>
|
---|
46 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
47 | # include <iprt/asm-amd64-x86.h>
|
---|
48 | #endif
|
---|
49 | #include <iprt/assert.h>
|
---|
50 | #include <iprt/errcore.h>
|
---|
51 | #include <iprt/mem.h>
|
---|
52 |
|
---|
53 | #include "internal/magics.h"
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Defined Constants And Macros *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | /** Apply the NoIrq hack if defined. */
|
---|
60 | #define RTSPINLOCK_NT_HACK_NOIRQ
|
---|
61 |
|
---|
62 | #ifdef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
63 | /** Indicates that the spinlock is taken. */
|
---|
64 | # define RTSPINLOCK_NT_HACK_NOIRQ_TAKEN UINT32(0x00c0ffee)
|
---|
65 | /** Indicates that the spinlock is taken. */
|
---|
66 | # define RTSPINLOCK_NT_HACK_NOIRQ_FREE UINT32(0xfe0000fe)
|
---|
67 | #endif
|
---|
68 |
|
---|
69 |
|
---|
70 | /*********************************************************************************************************************************
|
---|
71 | * Structures and Typedefs *
|
---|
72 | *********************************************************************************************************************************/
|
---|
73 | /**
|
---|
74 | * Wrapper for the KSPIN_LOCK type.
|
---|
75 | */
|
---|
76 | typedef struct RTSPINLOCKINTERNAL
|
---|
77 | {
|
---|
78 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
79 | uint32_t volatile u32Magic;
|
---|
80 | #ifdef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
81 | /** Spinlock hack. */
|
---|
82 | uint32_t volatile u32Hack;
|
---|
83 | #endif
|
---|
84 | /** The saved IRQL. */
|
---|
85 | KIRQL volatile SavedIrql;
|
---|
86 | /** The saved interrupt flag. */
|
---|
87 | RTCCUINTREG volatile fIntSaved;
|
---|
88 | /** The spinlock creation flags. */
|
---|
89 | uint32_t fFlags;
|
---|
90 | /** The NT spinlock structure. */
|
---|
91 | KSPIN_LOCK Spinlock;
|
---|
92 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
|
---|
96 | {
|
---|
97 | AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
|
---|
98 | RT_NOREF1(pszName);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Allocate.
|
---|
102 | */
|
---|
103 | Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
104 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
105 | if (!pThis)
|
---|
106 | return VERR_NO_MEMORY;
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Initialize & return.
|
---|
110 | */
|
---|
111 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
112 | #ifdef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
113 | pThis->u32Hack = RTSPINLOCK_NT_HACK_NOIRQ_FREE;
|
---|
114 | #endif
|
---|
115 | pThis->SavedIrql = 0;
|
---|
116 | pThis->fIntSaved = 0;
|
---|
117 | pThis->fFlags = fFlags;
|
---|
118 | KeInitializeSpinLock(&pThis->Spinlock);
|
---|
119 |
|
---|
120 | *pSpinlock = pThis;
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
126 | {
|
---|
127 | /*
|
---|
128 | * Validate input.
|
---|
129 | */
|
---|
130 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
131 | if (!pThis)
|
---|
132 | return VERR_INVALID_PARAMETER;
|
---|
133 | if (pThis->u32Magic != RTSPINLOCK_MAGIC)
|
---|
134 | {
|
---|
135 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
|
---|
136 | return VERR_INVALID_PARAMETER;
|
---|
137 | }
|
---|
138 |
|
---|
139 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
140 | RTMemFree(pThis);
|
---|
141 | return VINF_SUCCESS;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
146 | {
|
---|
147 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
148 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pThis->u32Magic));
|
---|
149 |
|
---|
150 | KIRQL SavedIrql;
|
---|
151 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
152 | {
|
---|
153 | #ifndef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
154 | RTCCUINTREG fIntSaved = ASMGetFlags();
|
---|
155 | ASMIntDisable();
|
---|
156 | KeAcquireSpinLock(&pThis->Spinlock, &SavedIrql);
|
---|
157 | #else
|
---|
158 | SavedIrql = KeGetCurrentIrql();
|
---|
159 | if (SavedIrql < DISPATCH_LEVEL)
|
---|
160 | {
|
---|
161 | KeRaiseIrql(DISPATCH_LEVEL, &SavedIrql);
|
---|
162 | Assert(SavedIrql < DISPATCH_LEVEL);
|
---|
163 | }
|
---|
164 | RTCCUINTREG fIntSaved = ASMGetFlags();
|
---|
165 | ASMIntDisable();
|
---|
166 |
|
---|
167 | if (!ASMAtomicCmpXchgU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
|
---|
168 | {
|
---|
169 | while (!ASMAtomicCmpXchgU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
|
---|
170 | ASMNopPause();
|
---|
171 | }
|
---|
172 | #endif
|
---|
173 | pThis->fIntSaved = fIntSaved;
|
---|
174 | }
|
---|
175 | else
|
---|
176 | KeAcquireSpinLock(&pThis->Spinlock, &SavedIrql);
|
---|
177 | pThis->SavedIrql = SavedIrql;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
182 | {
|
---|
183 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
184 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pThis->u32Magic));
|
---|
185 |
|
---|
186 | KIRQL SavedIrql = pThis->SavedIrql;
|
---|
187 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
188 | {
|
---|
189 | RTCCUINTREG fIntSaved = pThis->fIntSaved;
|
---|
190 | pThis->fIntSaved = 0;
|
---|
191 |
|
---|
192 | #ifndef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
193 | KeReleaseSpinLock(&pThis->Spinlock, SavedIrql);
|
---|
194 | ASMSetFlags(fIntSaved);
|
---|
195 | #else
|
---|
196 | Assert(pThis->u32Hack == RTSPINLOCK_NT_HACK_NOIRQ_TAKEN);
|
---|
197 |
|
---|
198 | ASMAtomicWriteU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_FREE);
|
---|
199 | ASMSetFlags(fIntSaved);
|
---|
200 | if (SavedIrql < DISPATCH_LEVEL)
|
---|
201 | KeLowerIrql(SavedIrql);
|
---|
202 | #endif
|
---|
203 | }
|
---|
204 | else
|
---|
205 | KeReleaseSpinLock(&pThis->Spinlock, SavedIrql);
|
---|
206 | }
|
---|
207 |
|
---|