1 | /* $Id: spinlock-r0drv-netbsd.c 63354 2016-08-12 00:47:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, NetBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
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 | * Header Files *
|
---|
33 | *********************************************************************************************************************************/
|
---|
34 | #include "the-netbsd-kernel.h"
|
---|
35 | #include "internal/iprt.h"
|
---|
36 |
|
---|
37 | #include <iprt/spinlock.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/alloc.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/asm-amd64-x86.h>
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include <iprt/mp.h>
|
---|
45 |
|
---|
46 | #include "internal/magics.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Structures and Typedefs *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | /**
|
---|
53 | * Wrapper for the struct mtx type.
|
---|
54 | */
|
---|
55 | typedef struct RTSPINLOCKINTERNAL
|
---|
56 | {
|
---|
57 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
58 | uint32_t volatile u32Magic;
|
---|
59 | /** The spinlock. */
|
---|
60 | kmutex_t pSpinLock;
|
---|
61 | /** Saved interrupt flag. */
|
---|
62 | uint32_t volatile fIntSaved;
|
---|
63 | /** The spinlock creation flags. */
|
---|
64 | uint32_t fFlags;
|
---|
65 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
66 |
|
---|
67 |
|
---|
68 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
|
---|
69 | {
|
---|
70 | RT_ASSERT_PREEMPTIBLE();
|
---|
71 | AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Allocate.
|
---|
75 | */
|
---|
76 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
77 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
|
---|
78 | if (!pThis)
|
---|
79 | return VERR_NO_MEMORY;
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Initialize & return.
|
---|
83 | */
|
---|
84 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
85 | pThis->fFlags = fFlags;
|
---|
86 | pThis->fIntSaved = 0;
|
---|
87 | if (fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
|
---|
88 | mutex_init(&pThis->pSpinLock, MUTEX_DEFAULT, IPL_BIO);
|
---|
89 | } else {
|
---|
90 | mutex_init(&pThis->pSpinLock, MUTEX_DEFAULT, IPL_NONE);
|
---|
91 | }
|
---|
92 |
|
---|
93 | *pSpinlock = pThis;
|
---|
94 | return VINF_SUCCESS;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
99 | {
|
---|
100 | /*
|
---|
101 | * Validate input.
|
---|
102 | */
|
---|
103 | RT_ASSERT_INTS_ON();
|
---|
104 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
105 | if (!pThis)
|
---|
106 | return VERR_INVALID_PARAMETER;
|
---|
107 | AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
108 | ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
|
---|
109 | VERR_INVALID_PARAMETER);
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Make the lock invalid and release the memory.
|
---|
113 | */
|
---|
114 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
115 | mutex_destroy(&pThis->pSpinLock);
|
---|
116 | RTMemFree(pThis);
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
122 | {
|
---|
123 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
124 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
125 | AssertPtr(pThis);
|
---|
126 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
127 |
|
---|
128 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
|
---|
129 | mutex_spin_enter(&pThis->pSpinLock);
|
---|
130 | } else {
|
---|
131 | mutex_enter(&pThis->pSpinLock);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
137 | {
|
---|
138 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
139 | AssertPtr(pThis);
|
---|
140 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
141 |
|
---|
142 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
|
---|
143 | mutex_spin_exit(&pThis->pSpinLock);
|
---|
144 | } else {
|
---|
145 | mutex_exit(&pThis->pSpinLock);
|
---|
146 | }
|
---|
147 | }
|
---|