]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - ubuntu/vbox/r0drv/linux/spinlock-r0drv-linux.c
UBUNTU: ubuntu: vbox -- update to 5.1.28-dfsg-1
[mirror_ubuntu-bionic-kernel.git] / ubuntu / vbox / r0drv / linux / spinlock-r0drv-linux.c
1 /* $Id: spinlock-r0drv-linux.c $ */
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 RT_NOREF_PV(pszName);
80
81 /*
82 * Allocate.
83 */
84 Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
85 pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
86 if (!pThis)
87 return VERR_NO_MEMORY;
88 /*
89 * Initialize and return.
90 */
91 pThis->u32Magic = RTSPINLOCK_MAGIC;
92 pThis->fFlags = fFlags;
93 pThis->fIntSaved = 0;
94 #ifdef RT_MORE_STRICT
95 pThis->idCpuOwner = NIL_RTCPUID;
96 pThis->idAssertCpu = NIL_RTCPUID;
97 #endif
98
99 spin_lock_init(&pThis->Spinlock);
100
101 *pSpinlock = pThis;
102 IPRT_LINUX_RESTORE_EFL_AC();
103 return VINF_SUCCESS;
104 }
105 RT_EXPORT_SYMBOL(RTSpinlockCreate);
106
107
108 RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
109 {
110 /*
111 * Validate input.
112 */
113 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
114 if (!pThis)
115 return VERR_INVALID_PARAMETER;
116 if (pThis->u32Magic != RTSPINLOCK_MAGIC)
117 {
118 AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
119 return VERR_INVALID_PARAMETER;
120 }
121
122 ASMAtomicIncU32(&pThis->u32Magic);
123 RTMemFree(pThis);
124 return VINF_SUCCESS;
125 }
126 RT_EXPORT_SYMBOL(RTSpinlockDestroy);
127
128
129 RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
130 {
131 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
132 IPRT_LINUX_SAVE_EFL_AC();
133 RT_ASSERT_PREEMPT_CPUID_VAR();
134 AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
135 ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
136
137 #ifdef CONFIG_PROVE_LOCKING
138 lockdep_off();
139 #endif
140 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
141 {
142 unsigned long fIntSaved;
143 spin_lock_irqsave(&pThis->Spinlock, fIntSaved);
144 pThis->fIntSaved = fIntSaved;
145 }
146 else
147 spin_lock(&pThis->Spinlock);
148 #ifdef CONFIG_PROVE_LOCKING
149 lockdep_on();
150 #endif
151
152 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
153 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
154 }
155 RT_EXPORT_SYMBOL(RTSpinlockAcquire);
156
157
158 RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
159 {
160 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
161 IPRT_LINUX_SAVE_EFL_AC(); /* spin_unlock* may preempt and trash eflags.ac. */
162 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
163 AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
164 ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
165 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
166
167 #ifdef CONFIG_PROVE_LOCKING
168 lockdep_off();
169 #endif
170 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
171 {
172 unsigned long fIntSaved = pThis->fIntSaved;
173 pThis->fIntSaved = 0;
174 spin_unlock_irqrestore(&pThis->Spinlock, fIntSaved);
175 }
176 else
177 spin_unlock(&pThis->Spinlock);
178 #ifdef CONFIG_PROVE_LOCKING
179 lockdep_on();
180 #endif
181
182 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
183 RT_ASSERT_PREEMPT_CPUID();
184 }
185 RT_EXPORT_SYMBOL(RTSpinlockRelease);
186