VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/spinlock-r0drv-linux.c

Last change on this file was 106061, checked in by vboxsync, 5 days ago

Copyright year updates by scm.

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