1 | /* $Id: spinlock-r0drv-linux.c 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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-linux-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
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 | #include <iprt/mp.h>
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include "internal/magics.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Structures and Typedefs *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | /**
|
---|
51 | * Wrapper for the spinlock_t structure.
|
---|
52 | */
|
---|
53 | typedef struct RTSPINLOCKINTERNAL
|
---|
54 | {
|
---|
55 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
56 | uint32_t volatile u32Magic;
|
---|
57 | /** The spinlock creation flags. */
|
---|
58 | uint32_t fFlags;
|
---|
59 | /** The saved interrupt flag. */
|
---|
60 | unsigned long volatile fIntSaved;
|
---|
61 | /** The linux spinlock structure. */
|
---|
62 | spinlock_t Spinlock;
|
---|
63 | #ifdef RT_MORE_STRICT
|
---|
64 | /** The idAssertCpu variable before acquring the lock for asserting after
|
---|
65 | * releasing the spinlock. */
|
---|
66 | RTCPUID volatile idAssertCpu;
|
---|
67 | /** The CPU that owns the lock. */
|
---|
68 | RTCPUID volatile idCpuOwner;
|
---|
69 | #endif
|
---|
70 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
|
---|
75 | {
|
---|
76 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
77 | PRTSPINLOCKINTERNAL pThis;
|
---|
78 | AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Allocate.
|
---|
82 | */
|
---|
83 | Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
84 | pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
85 | if (!pThis)
|
---|
86 | return VERR_NO_MEMORY;
|
---|
87 | /*
|
---|
88 | * Initialize and return.
|
---|
89 | */
|
---|
90 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
91 | pThis->fFlags = fFlags;
|
---|
92 | pThis->fIntSaved = 0;
|
---|
93 | #ifdef RT_MORE_STRICT
|
---|
94 | pThis->idCpuOwner = NIL_RTCPUID;
|
---|
95 | pThis->idAssertCpu = NIL_RTCPUID;
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | spin_lock_init(&pThis->Spinlock);
|
---|
99 |
|
---|
100 | *pSpinlock = pThis;
|
---|
101 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 | RT_EXPORT_SYMBOL(RTSpinlockCreate);
|
---|
105 |
|
---|
106 |
|
---|
107 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
108 | {
|
---|
109 | /*
|
---|
110 | * Validate input.
|
---|
111 | */
|
---|
112 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
113 | if (!pThis)
|
---|
114 | return VERR_INVALID_PARAMETER;
|
---|
115 | if (pThis->u32Magic != RTSPINLOCK_MAGIC)
|
---|
116 | {
|
---|
117 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
|
---|
118 | return VERR_INVALID_PARAMETER;
|
---|
119 | }
|
---|
120 |
|
---|
121 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
122 | RTMemFree(pThis);
|
---|
123 | return VINF_SUCCESS;
|
---|
124 | }
|
---|
125 | RT_EXPORT_SYMBOL(RTSpinlockDestroy);
|
---|
126 |
|
---|
127 |
|
---|
128 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
129 | {
|
---|
130 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
131 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
132 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
133 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
134 | ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
|
---|
135 |
|
---|
136 | #ifdef CONFIG_PROVE_LOCKING
|
---|
137 | lockdep_off();
|
---|
138 | #endif
|
---|
139 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
140 | {
|
---|
141 | unsigned long fIntSaved;
|
---|
142 | spin_lock_irqsave(&pThis->Spinlock, fIntSaved);
|
---|
143 | pThis->fIntSaved = fIntSaved;
|
---|
144 | }
|
---|
145 | else
|
---|
146 | spin_lock(&pThis->Spinlock);
|
---|
147 | #ifdef CONFIG_PROVE_LOCKING
|
---|
148 | lockdep_on();
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | IPRT_LINUX_RESTORE_EFL_ONLY_AC();
|
---|
152 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
153 | }
|
---|
154 | RT_EXPORT_SYMBOL(RTSpinlockAcquire);
|
---|
155 |
|
---|
156 |
|
---|
157 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
158 | {
|
---|
159 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
160 | IPRT_LINUX_SAVE_EFL_AC(); /* spin_unlock* may preempt and trash eflags.ac. */
|
---|
161 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
162 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
163 | ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
|
---|
164 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
165 |
|
---|
166 | #ifdef CONFIG_PROVE_LOCKING
|
---|
167 | lockdep_off();
|
---|
168 | #endif
|
---|
169 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
170 | {
|
---|
171 | unsigned long fIntSaved = pThis->fIntSaved;
|
---|
172 | pThis->fIntSaved = 0;
|
---|
173 | spin_unlock_irqrestore(&pThis->Spinlock, fIntSaved);
|
---|
174 | }
|
---|
175 | else
|
---|
176 | spin_unlock(&pThis->Spinlock);
|
---|
177 | #ifdef CONFIG_PROVE_LOCKING
|
---|
178 | lockdep_on();
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | IPRT_LINUX_RESTORE_EFL_ONLY_AC();
|
---|
182 | RT_ASSERT_PREEMPT_CPUID();
|
---|
183 | }
|
---|
184 | RT_EXPORT_SYMBOL(RTSpinlockRelease);
|
---|
185 |
|
---|