mutex.h revision 1.3
1/*	$NetBSD: mutex.h,v 1.3 2007/02/17 05:34:07 matt Exp $	*/
2
3/*-
4 * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the NetBSD
21 *	Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifndef _VAX_MUTEX_H_
40#define	_VAX_MUTEX_H_
41
42/*
43 * The VAX mutex implementation is troublesome, because the VAX architecture
44 * lacks a compare-and-set operation, yet there are many SMP VAX
45 * machines in circulation.  SMP for spin mutexes is easy - we don't need
46 * to know who owns the lock.  For adaptive mutexes, we need an aditional
47 * interlock.  However, since we know that owners will be kernel addresses
48 * and all kernel addresses have the high bit set, we can use the high bit
49 * as an interlock.
50 *
51 * So we test the high bit with BBSSI and if clear
52 * kernels are always loaded above 0xe0000000, and the low 5 bits of any
53 * "struct lwp *" are always zero.  So, to record the lock owner, we only
54 * need 23 bits of space.  mtxa_owner contains the mutex owner's address
55 * shifted right by 5: the top three bits of which will always be 0xe,
56 * overlapping with the interlock at the top byte, which is always 0xff
57 * when the mutex is held.
58 *
59 * For a mutex acquisition, the owner field is set in two steps: first,
60 * acquire the interlock (top bit), and second OR in the owner's address.
61 * Once the owner field is non zero, it will appear that the mutex is held,
62 * by which LWP it does not matter: other LWPs competing for the lock will
63 * fall through to mutex_vector_enter(), and either spin or sleep.
64 *
65 * As a result there is no space for a waiters bit in the owner field.  No
66 * problem, because it would be hard to synchronise using one without a CAS
67 * operation.  Note that in order to do unlocked release of adaptive
68 * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley
69 * visible on the bus.  So, adaptive mutexes share the spin lock byte with
70 * spin mutexes (set with bb{cc,ss}i), but it is not treated as a lock in its
71 * own right, rather as a flag that can be atomically set or cleared.
72 *
73 * When releasing an adaptive mutex, we first clear the owners field, and
74 * then check to see if the waiters byte is set.  This ensures that there
75 * will always be someone to wake any sleeping waiters up (even it the mutex
76 * is acquired immediately after we release it, or if we are preempted
77 * immediatley after clearing the owners field).  The setting or clearing of
78 * the waiters byte is serialized by the turnstile chain lock associated
79 * with the mutex.
80 *
81 * See comments in kern_mutex.c about releasing adaptive mutexes without
82 * an interlocking step.
83 */
84
85#ifndef __MUTEX_PRIVATE
86
87struct kmutex {
88	uintptr_t	mtx_pad1;
89	uint32_t	mtx_pad2[2];
90};
91
92#else	/* __MUTEX_PRIVATE */
93
94struct kmutex {
95	/* Adaptive mutex */
96	union {
97		volatile uintptr_t	mtxu_owner;	/* 0-3 */
98		__cpu_simple_lock_t	mtxu_lock;	/* 0 */
99	} mtx_u;
100	ipl_cookie_t			mtx_ipl;	/* 4-7 */
101	uint32_t			mtx_id;		/* 8-11 */
102};
103#define	mtx_owner	mtx_u.mtxu_owner
104#define	mtx_lock	mtx_u.mtxu_lock
105
106#define	__HAVE_MUTEX_STUBS		1
107#define	__HAVE_SPIN_MUTEX_STUBS		1
108#define	__HAVE_MUTEX_NO_SPIN_ACTIVE_P	1
109
110static inline uintptr_t
111MUTEX_OWNER(uintptr_t owner)
112{
113	return owner & ~1;
114}
115
116static inline bool
117MUTEX_OWNED(uintptr_t owner)
118{
119	return owner != 0;
120}
121
122static inline bool
123MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
124{
125	mtx->mtx_owner |= 1;
126 	return (mtx->mtx_owner & ~1) != 0;
127}
128
129static inline bool
130MUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
131{
132	return (mtx->mtx_owner & 1) != 0;
133}
134
135static inline void
136MUTEX_CLEAR_WAITERS(volatile kmutex_t *mtx)
137{
138	mtx->mtx_owner &= ~1;
139}
140
141static inline void
142MUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl)
143{
144	mtx->mtx_id = (id << 1) | 1;
145	mtx->mtx_ipl = makeiplcookie(ipl);
146	mtx->mtx_lock = 0;
147}
148
149static inline void
150MUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id)
151{
152	mtx->mtx_id = id << 1;
153	mtx->mtx_ipl = makeiplcookie(-1);
154	mtx->mtx_owner = 0;
155}
156
157static inline void
158MUTEX_DESTROY(kmutex_t *mtx)
159{
160	mtx->mtx_owner = (uintptr_t)-1L;
161	mtx->mtx_id = 0xdeadface << 1;
162}
163
164static inline u_int
165MUTEX_GETID(volatile kmutex_t *mtx)
166{
167	return (mtx)->mtx_id >> 1;
168}
169
170static inline bool
171MUTEX_SPIN_P(volatile kmutex_t *mtx)
172{
173	return (mtx->mtx_id & 1) != 0;
174}
175
176static inline bool
177MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
178{
179	return (mtx->mtx_id & 1) == 0;
180}
181
182static inline bool
183MUTEX_NO_SPIN_ACTIVE_P(struct cpu_info *ci)
184{
185	return ci->ci_mtx_count == 1;
186}
187
188static inline bool
189MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
190{
191	int rv;
192	__asm __volatile(
193		"clrl %1;"
194		"bbssi $31,%0,1f;"
195		"incl %1;"
196		"insv %2,%0,$31,%0;"
197		"1:"
198	    : "=m"(mtx->mtx_owner), "=r"(rv)
199	    : "g"(curthread));
200	return 1;
201}
202
203static inline void
204MUTEX_RELEASE(kmutex_t *mtx)
205{
206	__asm __volatile(
207		"insv $0,$0,$31,%0;"
208		"bbcci $31,%0,1f;"
209		"1:"
210	   : "=m" (mtx->mtx_owner));
211}
212
213#endif	/* __MUTEX_PRIVATE */
214
215#endif /* _VAX_MUTEX_H_ */
216