VirtualBox

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

Last change on this file since 32736 was 32463, checked in by vboxsync, 14 years ago

IPRT/spinlock-r0drv-nt: Enabled RTSPINLOCK_NT_HACK_NOIRQ by default.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1/* $Id: spinlock-r0drv-nt.cpp 32463 2010-09-14 07:30:11Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 */
66typedef 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 NT spinlock structure. */
75 KSPIN_LOCK Spinlock;
76} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
77
78
79
80RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
81{
82 /*
83 * Allocate.
84 */
85 Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
86 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
87 if (!pSpinlockInt)
88 return VERR_NO_MEMORY;
89
90 /*
91 * Initialize & return.
92 */
93 pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
94#ifdef RTSPINLOCK_NT_HACK_NOIRQ
95 pSpinlockInt->u32Hack = RTSPINLOCK_NT_HACK_NOIRQ_FREE;
96#endif
97 KeInitializeSpinLock(&pSpinlockInt->Spinlock);
98 Assert(sizeof(KIRQL) == sizeof(unsigned char));
99 AssertCompile(sizeof(KIRQL) == sizeof(unsigned char));
100
101 *pSpinlock = pSpinlockInt;
102 return VINF_SUCCESS;
103}
104
105
106RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
107{
108 /*
109 * Validate input.
110 */
111 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
112 if (!pSpinlockInt)
113 return VERR_INVALID_PARAMETER;
114 if (pSpinlockInt->u32Magic != RTSPINLOCK_MAGIC)
115 {
116 AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic));
117 return VERR_INVALID_PARAMETER;
118 }
119
120 ASMAtomicIncU32(&pSpinlockInt->u32Magic);
121 RTMemFree(pSpinlockInt);
122 return VINF_SUCCESS;
123}
124
125
126RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
127{
128 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
129 AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pSpinlockInt->u32Magic));
130
131#ifndef RTSPINLOCK_NT_HACK_NOIRQ
132 KeAcquireSpinLock(&pSpinlockInt->Spinlock, &pTmp->uchIrqL);
133 pTmp->uFlags = ASMGetFlags();
134 ASMIntDisable();
135#else
136 pTmp->uchIrqL = KeGetCurrentIrql();
137 if (pTmp->uchIrqL < DISPATCH_LEVEL)
138 {
139 KeRaiseIrql(DISPATCH_LEVEL, &pTmp->uchIrqL);
140 Assert(pTmp->uchIrqL < DISPATCH_LEVEL);
141 }
142 pTmp->uFlags = ASMGetFlags();
143 ASMIntDisable();
144 if (!ASMAtomicCmpXchgU32(&pSpinlockInt->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
145 {
146 while (!ASMAtomicCmpXchgU32(&pSpinlockInt->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
147 ASMNopPause();
148 }
149#endif
150}
151
152
153RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
154{
155 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
156 AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pSpinlockInt->u32Magic));
157
158#ifndef RTSPINLOCK_NT_HACK_NOIRQ
159 ASMSetFlags(pTmp->uFlags);
160 KeReleaseSpinLock(&pSpinlockInt->Spinlock, pTmp->uchIrqL);
161#else
162 Assert(pSpinlockInt->u32Hack == RTSPINLOCK_NT_HACK_NOIRQ_TAKEN);
163 ASMAtomicWriteU32(&pSpinlockInt->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_FREE);
164 ASMSetFlags(pTmp->uFlags);
165 if (pTmp->uchIrqL < DISPATCH_LEVEL)
166 KeLowerIrql(pTmp->uchIrqL);
167#endif
168}
169
170
171RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
172{
173 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
174 AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pSpinlockInt->u32Magic));
175
176 KeAcquireSpinLock(&pSpinlockInt->Spinlock, &pTmp->uchIrqL);
177}
178
179
180RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
181{
182 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
183 AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pSpinlockInt->u32Magic));
184
185 KeReleaseSpinLock(&pSpinlockInt->Spinlock, pTmp->uchIrqL);
186}
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