1 | /* $Id: spinlock-r0drv-nt.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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/errcore.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 | RT_NOREF1(pszName);
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Allocate.
|
---|
92 | */
|
---|
93 | Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
94 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
95 | if (!pThis)
|
---|
96 | return VERR_NO_MEMORY;
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Initialize & return.
|
---|
100 | */
|
---|
101 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
102 | #ifdef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
103 | pThis->u32Hack = RTSPINLOCK_NT_HACK_NOIRQ_FREE;
|
---|
104 | #endif
|
---|
105 | pThis->SavedIrql = 0;
|
---|
106 | pThis->fIntSaved = 0;
|
---|
107 | pThis->fFlags = fFlags;
|
---|
108 | KeInitializeSpinLock(&pThis->Spinlock);
|
---|
109 |
|
---|
110 | *pSpinlock = pThis;
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
116 | {
|
---|
117 | /*
|
---|
118 | * Validate input.
|
---|
119 | */
|
---|
120 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
121 | if (!pThis)
|
---|
122 | return VERR_INVALID_PARAMETER;
|
---|
123 | if (pThis->u32Magic != RTSPINLOCK_MAGIC)
|
---|
124 | {
|
---|
125 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
|
---|
126 | return VERR_INVALID_PARAMETER;
|
---|
127 | }
|
---|
128 |
|
---|
129 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
130 | RTMemFree(pThis);
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
136 | {
|
---|
137 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
138 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pThis->u32Magic));
|
---|
139 |
|
---|
140 | KIRQL SavedIrql;
|
---|
141 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
142 | {
|
---|
143 | #ifndef RTSPINLOCK_NT_HACK_NOIRQ
|
---|
144 | RTCCUINTREG fIntSaved = ASMGetFlags();
|
---|
145 | ASMIntDisable();
|
---|
146 | KeAcquireSpinLock(&pThis->Spinlock, &SavedIrql);
|
---|
147 | #else
|
---|
148 | SavedIrql = KeGetCurrentIrql();
|
---|
149 | if (SavedIrql < DISPATCH_LEVEL)
|
---|
150 | {
|
---|
151 | KeRaiseIrql(DISPATCH_LEVEL, &SavedIrql);
|
---|
152 | Assert(SavedIrql < DISPATCH_LEVEL);
|
---|
153 | }
|
---|
154 | RTCCUINTREG fIntSaved = ASMGetFlags();
|
---|
155 | ASMIntDisable();
|
---|
156 |
|
---|
157 | if (!ASMAtomicCmpXchgU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
|
---|
158 | {
|
---|
159 | while (!ASMAtomicCmpXchgU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
|
---|
160 | ASMNopPause();
|
---|
161 | }
|
---|
162 | #endif
|
---|
163 | pThis->fIntSaved = fIntSaved;
|
---|
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 |
|
---|