1 | /* $Id: spinlock-r0drv-netbsd.c 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, NetBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Contributed by knut st. osmundsen.
|
---|
8 | *
|
---|
9 | * Copyright (C) 2007-2022 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | * --------------------------------------------------------------------
|
---|
28 | *
|
---|
29 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
30 | *
|
---|
31 | * Permission is hereby granted, free of charge, to any person
|
---|
32 | * obtaining a copy of this software and associated documentation
|
---|
33 | * files (the "Software"), to deal in the Software without
|
---|
34 | * restriction, including without limitation the rights to use,
|
---|
35 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
36 | * copies of the Software, and to permit persons to whom the
|
---|
37 | * Software is furnished to do so, subject to the following
|
---|
38 | * conditions:
|
---|
39 | *
|
---|
40 | * The above copyright notice and this permission notice shall be
|
---|
41 | * included in all copies or substantial portions of the Software.
|
---|
42 | *
|
---|
43 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
44 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
45 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
46 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
47 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
48 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
49 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
50 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
51 | */
|
---|
52 |
|
---|
53 |
|
---|
54 | /*********************************************************************************************************************************
|
---|
55 | * Header Files *
|
---|
56 | *********************************************************************************************************************************/
|
---|
57 | #include "the-netbsd-kernel.h"
|
---|
58 | #include "internal/iprt.h"
|
---|
59 |
|
---|
60 | #include <iprt/spinlock.h>
|
---|
61 | #include <iprt/errcore.h>
|
---|
62 | #include <iprt/alloc.h>
|
---|
63 | #include <iprt/assert.h>
|
---|
64 | #include <iprt/asm.h>
|
---|
65 | #include <iprt/asm-amd64-x86.h>
|
---|
66 | #include <iprt/thread.h>
|
---|
67 | #include <iprt/mp.h>
|
---|
68 |
|
---|
69 | #include "internal/magics.h"
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Structures and Typedefs *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 | /**
|
---|
76 | * Wrapper for the struct mtx type.
|
---|
77 | */
|
---|
78 | typedef struct RTSPINLOCKINTERNAL
|
---|
79 | {
|
---|
80 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
81 | uint32_t volatile u32Magic;
|
---|
82 | /** The spinlock. */
|
---|
83 | kmutex_t pSpinLock;
|
---|
84 | /** Saved interrupt flag. */
|
---|
85 | uint32_t volatile fIntSaved;
|
---|
86 | /** The spinlock creation flags. */
|
---|
87 | uint32_t fFlags;
|
---|
88 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
89 |
|
---|
90 |
|
---|
91 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
|
---|
92 | {
|
---|
93 | RT_ASSERT_PREEMPTIBLE();
|
---|
94 | AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Allocate.
|
---|
98 | */
|
---|
99 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
100 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
|
---|
101 | if (!pThis)
|
---|
102 | return VERR_NO_MEMORY;
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Initialize & return.
|
---|
106 | */
|
---|
107 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
108 | pThis->fFlags = fFlags;
|
---|
109 | pThis->fIntSaved = 0;
|
---|
110 | if (fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
|
---|
111 | mutex_init(&pThis->pSpinLock, MUTEX_DEFAULT, IPL_BIO);
|
---|
112 | } else {
|
---|
113 | mutex_init(&pThis->pSpinLock, MUTEX_DEFAULT, IPL_NONE);
|
---|
114 | }
|
---|
115 |
|
---|
116 | *pSpinlock = pThis;
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
122 | {
|
---|
123 | /*
|
---|
124 | * Validate input.
|
---|
125 | */
|
---|
126 | RT_ASSERT_INTS_ON();
|
---|
127 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
128 | if (!pThis)
|
---|
129 | return VERR_INVALID_PARAMETER;
|
---|
130 | AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
131 | ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
|
---|
132 | VERR_INVALID_PARAMETER);
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Make the lock invalid and release the memory.
|
---|
136 | */
|
---|
137 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
138 | mutex_destroy(&pThis->pSpinLock);
|
---|
139 | RTMemFree(pThis);
|
---|
140 | return VINF_SUCCESS;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
145 | {
|
---|
146 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
147 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
148 | AssertPtr(pThis);
|
---|
149 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
150 |
|
---|
151 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
|
---|
152 | mutex_spin_enter(&pThis->pSpinLock);
|
---|
153 | } else {
|
---|
154 | mutex_enter(&pThis->pSpinLock);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
160 | {
|
---|
161 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
162 | AssertPtr(pThis);
|
---|
163 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
164 |
|
---|
165 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
|
---|
166 | mutex_spin_exit(&pThis->pSpinLock);
|
---|
167 | } else {
|
---|
168 | mutex_exit(&pThis->pSpinLock);
|
---|
169 | }
|
---|
170 | }
|
---|