mutex.h revision 1.6
11.6Sthorpej/*	$NetBSD: mutex.h,v 1.6 2007/03/09 21:35:33 thorpej Exp $	*/
21.1Smatt
31.1Smatt/*-
41.1Smatt * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
51.1Smatt * All rights reserved.
61.1Smatt *
71.1Smatt * This code is derived from software contributed to The NetBSD Foundation
81.1Smatt * by Jason R. Thorpe and Andrew Doran.
91.1Smatt *
101.1Smatt * Redistribution and use in source and binary forms, with or without
111.1Smatt * modification, are permitted provided that the following conditions
121.1Smatt * are met:
131.1Smatt * 1. Redistributions of source code must retain the above copyright
141.1Smatt *    notice, this list of conditions and the following disclaimer.
151.1Smatt * 2. Redistributions in binary form must reproduce the above copyright
161.1Smatt *    notice, this list of conditions and the following disclaimer in the
171.1Smatt *    documentation and/or other materials provided with the distribution.
181.1Smatt * 3. All advertising materials mentioning features or use of this software
191.1Smatt *    must display the following acknowledgement:
201.1Smatt *	This product includes software developed by the NetBSD
211.1Smatt *	Foundation, Inc. and its contributors.
221.1Smatt * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Smatt *    contributors may be used to endorse or promote products derived
241.1Smatt *    from this software without specific prior written permission.
251.1Smatt *
261.1Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Smatt * POSSIBILITY OF SUCH DAMAGE.
371.1Smatt */
381.1Smatt
391.1Smatt#ifndef _VAX_MUTEX_H_
401.1Smatt#define	_VAX_MUTEX_H_
411.1Smatt
421.1Smatt/*
431.1Smatt * The VAX mutex implementation is troublesome, because the VAX architecture
441.1Smatt * lacks a compare-and-set operation, yet there are many SMP VAX
451.1Smatt * machines in circulation.  SMP for spin mutexes is easy - we don't need
461.1Smatt * to know who owns the lock.  For adaptive mutexes, we need an aditional
471.1Smatt * interlock.  However, since we know that owners will be kernel addresses
481.1Smatt * and all kernel addresses have the high bit set, we can use the high bit
491.1Smatt * as an interlock.
501.1Smatt *
511.1Smatt * So we test the high bit with BBSSI and if clear
521.1Smatt * kernels are always loaded above 0xe0000000, and the low 5 bits of any
531.1Smatt * "struct lwp *" are always zero.  So, to record the lock owner, we only
541.1Smatt * need 23 bits of space.  mtxa_owner contains the mutex owner's address
551.1Smatt * shifted right by 5: the top three bits of which will always be 0xe,
561.1Smatt * overlapping with the interlock at the top byte, which is always 0xff
571.1Smatt * when the mutex is held.
581.1Smatt *
591.1Smatt * For a mutex acquisition, the owner field is set in two steps: first,
601.1Smatt * acquire the interlock (top bit), and second OR in the owner's address.
611.1Smatt * Once the owner field is non zero, it will appear that the mutex is held,
621.1Smatt * by which LWP it does not matter: other LWPs competing for the lock will
631.1Smatt * fall through to mutex_vector_enter(), and either spin or sleep.
641.1Smatt *
651.1Smatt * As a result there is no space for a waiters bit in the owner field.  No
661.1Smatt * problem, because it would be hard to synchronise using one without a CAS
671.1Smatt * operation.  Note that in order to do unlocked release of adaptive
681.1Smatt * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley
691.1Smatt * visible on the bus.  So, adaptive mutexes share the spin lock byte with
701.1Smatt * spin mutexes (set with bb{cc,ss}i), but it is not treated as a lock in its
711.1Smatt * own right, rather as a flag that can be atomically set or cleared.
721.1Smatt *
731.1Smatt * When releasing an adaptive mutex, we first clear the owners field, and
741.1Smatt * then check to see if the waiters byte is set.  This ensures that there
751.1Smatt * will always be someone to wake any sleeping waiters up (even it the mutex
761.1Smatt * is acquired immediately after we release it, or if we are preempted
771.1Smatt * immediatley after clearing the owners field).  The setting or clearing of
781.1Smatt * the waiters byte is serialized by the turnstile chain lock associated
791.1Smatt * with the mutex.
801.1Smatt *
811.1Smatt * See comments in kern_mutex.c about releasing adaptive mutexes without
821.1Smatt * an interlocking step.
831.1Smatt */
841.1Smatt
851.1Smatt#ifndef __MUTEX_PRIVATE
861.1Smatt
871.1Smattstruct kmutex {
881.1Smatt	uintptr_t	mtx_pad1;
891.1Smatt	uint32_t	mtx_pad2[2];
901.1Smatt};
911.1Smatt
921.1Smatt#else	/* __MUTEX_PRIVATE */
931.1Smatt
941.1Smattstruct kmutex {
951.1Smatt	/* Adaptive mutex */
961.1Smatt	union {
971.1Smatt		volatile uintptr_t	mtxu_owner;	/* 0-3 */
981.1Smatt		__cpu_simple_lock_t	mtxu_lock;	/* 0 */
991.1Smatt	} mtx_u;
1001.1Smatt	ipl_cookie_t			mtx_ipl;	/* 4-7 */
1011.1Smatt	uint32_t			mtx_id;		/* 8-11 */
1021.1Smatt};
1031.1Smatt#define	mtx_owner	mtx_u.mtxu_owner
1041.1Smatt#define	mtx_lock	mtx_u.mtxu_lock
1051.1Smatt
1061.3Smatt#define	__HAVE_MUTEX_STUBS		1
1071.3Smatt#define	__HAVE_SPIN_MUTEX_STUBS		1
1081.4Sad
1091.4Sad#define	MUTEX_NO_SPIN_ACTIVE_P(ci)	((ci)->ci_mtx_count == 1)
1101.1Smatt
1111.1Smattstatic inline uintptr_t
1121.1SmattMUTEX_OWNER(uintptr_t owner)
1131.1Smatt{
1141.1Smatt	return owner & ~1;
1151.1Smatt}
1161.1Smatt
1171.1Smattstatic inline bool
1181.1SmattMUTEX_OWNED(uintptr_t owner)
1191.1Smatt{
1201.1Smatt	return owner != 0;
1211.1Smatt}
1221.1Smatt
1231.1Smattstatic inline bool
1241.1SmattMUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
1251.1Smatt{
1261.1Smatt	mtx->mtx_owner |= 1;
1271.2Sad 	return (mtx->mtx_owner & ~1) != 0;
1281.1Smatt}
1291.1Smatt
1301.1Smattstatic inline bool
1311.1SmattMUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
1321.1Smatt{
1331.1Smatt	return (mtx->mtx_owner & 1) != 0;
1341.1Smatt}
1351.1Smatt
1361.1Smattstatic inline void
1371.1SmattMUTEX_CLEAR_WAITERS(volatile kmutex_t *mtx)
1381.1Smatt{
1391.1Smatt	mtx->mtx_owner &= ~1;
1401.1Smatt}
1411.1Smatt
1421.1Smattstatic inline void
1431.1SmattMUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl)
1441.1Smatt{
1451.1Smatt	mtx->mtx_id = (id << 1) | 1;
1461.1Smatt	mtx->mtx_ipl = makeiplcookie(ipl);
1471.1Smatt	mtx->mtx_lock = 0;
1481.1Smatt}
1491.1Smatt
1501.1Smattstatic inline void
1511.1SmattMUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id)
1521.1Smatt{
1531.1Smatt	mtx->mtx_id = id << 1;
1541.1Smatt	mtx->mtx_ipl = makeiplcookie(-1);
1551.1Smatt	mtx->mtx_owner = 0;
1561.1Smatt}
1571.1Smatt
1581.1Smattstatic inline void
1591.1SmattMUTEX_DESTROY(kmutex_t *mtx)
1601.1Smatt{
1611.1Smatt	mtx->mtx_owner = (uintptr_t)-1L;
1621.1Smatt	mtx->mtx_id = 0xdeadface << 1;
1631.1Smatt}
1641.1Smatt
1651.1Smattstatic inline u_int
1661.1SmattMUTEX_GETID(volatile kmutex_t *mtx)
1671.1Smatt{
1681.1Smatt	return (mtx)->mtx_id >> 1;
1691.1Smatt}
1701.1Smatt
1711.1Smattstatic inline bool
1721.1SmattMUTEX_SPIN_P(volatile kmutex_t *mtx)
1731.1Smatt{
1741.1Smatt	return (mtx->mtx_id & 1) != 0;
1751.1Smatt}
1761.1Smatt
1771.1Smattstatic inline bool
1781.1SmattMUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
1791.1Smatt{
1801.1Smatt	return (mtx->mtx_id & 1) == 0;
1811.1Smatt}
1821.1Smatt
1831.1Smattstatic inline bool
1841.1SmattMUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
1851.1Smatt{
1861.1Smatt	int rv;
1871.1Smatt	__asm __volatile(
1881.1Smatt		"clrl %1;"
1891.1Smatt		"bbssi $31,%0,1f;"
1901.1Smatt		"incl %1;"
1911.6Sthorpej		"insv %2,$0,$31,%0;"
1921.1Smatt		"1:"
1931.1Smatt	    : "=m"(mtx->mtx_owner), "=r"(rv)
1941.1Smatt	    : "g"(curthread));
1951.5Smatt	return rv;
1961.1Smatt}
1971.1Smatt
1981.1Smattstatic inline void
1991.1SmattMUTEX_RELEASE(kmutex_t *mtx)
2001.1Smatt{
2011.1Smatt	__asm __volatile(
2021.1Smatt		"insv $0,$0,$31,%0;"
2031.1Smatt		"bbcci $31,%0,1f;"
2041.1Smatt		"1:"
2051.1Smatt	   : "=m" (mtx->mtx_owner));
2061.1Smatt}
2071.1Smatt
2081.1Smatt#endif	/* __MUTEX_PRIVATE */
2091.1Smatt
2101.1Smatt#endif /* _VAX_MUTEX_H_ */
211