mutex.h revision 1.10
1/* $NetBSD: mutex.h,v 1.10 2007/11/21 11:15:50 yamt 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 LOCKDEBUG 86#define MUTEX_COUNT_BIAS 1 87#endif 88 89#ifndef __MUTEX_PRIVATE 90 91struct kmutex { 92 uintptr_t mtx_pad1; 93 uint32_t mtx_pad2; 94}; 95 96#else /* __MUTEX_PRIVATE */ 97 98struct kmutex { 99 /* Adaptive mutex */ 100 union { 101 volatile uintptr_t u_owner; /* 0-3 */ 102 struct { 103 uint8_t s_dummylo; /* 0 */ 104 __cpu_simple_lock_t s_lock; /* 1 */ 105 ipl_cookie_t s_ipl; /* 2 */ 106 uint8_t s_dummyhi; /* 3 */ 107 } u_s; 108 } mtx_u; 109 uint32_t mtx_flags; /* 4-7 */ 110}; 111#define mtx_owner mtx_u.u_owner 112#define mtx_lock mtx_u.u_s.s_lock 113#define mtx_ipl mtx_u.u_s.s_ipl 114 115#define __HAVE_MUTEX_STUBS 1 116#define __HAVE_SPIN_MUTEX_STUBS 1 117 118static inline uintptr_t 119MUTEX_OWNER(uintptr_t owner) 120{ 121 return owner & ~1; 122} 123 124static inline bool 125MUTEX_OWNED(uintptr_t owner) 126{ 127 return owner != 0; 128} 129 130static inline bool 131MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner) 132{ 133 mtx->mtx_owner |= 1; 134 return (mtx->mtx_owner & ~1) != 0; 135} 136 137static inline bool 138MUTEX_HAS_WAITERS(volatile kmutex_t *mtx) 139{ 140 return (mtx->mtx_owner & 1) != 0; 141} 142 143static inline void 144MUTEX_CLEAR_WAITERS(volatile kmutex_t *mtx) 145{ 146 mtx->mtx_owner &= ~1; 147} 148 149static inline void 150MUTEX_INITIALIZE_SPIN(kmutex_t *mtx, bool dodebug, int ipl) 151{ 152 /* lock_stubs.S checks the lowest bit of mtx_flags using blbs/blbc */ 153 mtx->mtx_flags = (dodebug << 1) | 1; 154 mtx->mtx_owner = 0x80000000; 155 mtx->mtx_ipl = makeiplcookie(ipl); 156 mtx->mtx_lock = 0; 157} 158 159static inline void 160MUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, bool dodebug) 161{ 162 /* lock_stubs.S checks the lowest bit of mtx_flags using blbs/blbc */ 163 mtx->mtx_flags = (dodebug << 1); 164 mtx->mtx_ipl = makeiplcookie(-1); 165 mtx->mtx_owner = 0; 166} 167 168static inline void 169MUTEX_DESTROY(kmutex_t *mtx) 170{ 171 mtx->mtx_owner = (uintptr_t)-1L; 172 mtx->mtx_flags = 0xdeadface << 1; 173} 174 175static inline bool 176MUTEX_DEBUG_P(volatile kmutex_t *mtx) 177{ 178 return mtx->mtx_flags >> 1; 179} 180 181static inline bool 182MUTEX_SPIN_P(volatile kmutex_t *mtx) 183{ 184 return (mtx->mtx_flags & 1) != 0; 185} 186 187static inline bool 188MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx) 189{ 190 return (mtx->mtx_flags & 1) == 0; 191} 192 193static inline bool 194MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread) 195{ 196 int rv; 197 __asm __volatile( 198 "clrl %1;" 199 "bbssi $31,%0,1f;" 200 "incl %1;" 201 "insv %2,$0,$31,%0;" 202 "1:" 203 : "=m"(mtx->mtx_owner), "=r"(rv) 204 : "g"(curthread)); 205 return rv; 206} 207 208static inline void 209MUTEX_RELEASE(kmutex_t *mtx) 210{ 211 __asm __volatile( 212 "insv $0,$0,$31,%0;" 213 "bbcci $31,%0,1f;" 214 "1:" 215 : "=m" (mtx->mtx_owner)); 216} 217 218#endif /* __MUTEX_PRIVATE */ 219 220#endif /* _VAX_MUTEX_H_ */ 221