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