VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/spinlock-r0drv-nt.cpp@ 106518

Last change on this file since 106518 was 106433, checked in by vboxsync, 3 months ago

Runtime/r0drv: Work on getting it building on win.arm64 (the easy bits), bugref:10734

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette