mutex.h revision 1.4
11.4Smrg/*	$NetBSD: mutex.h,v 1.4 2007/05/28 22:56:19 mrg Exp $	*/
21.2Sad
31.2Sad/*-
41.2Sad * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
51.2Sad * All rights reserved.
61.2Sad *
71.2Sad * This code is derived from software contributed to The NetBSD Foundation
81.2Sad * by Jason R. Thorpe and Andrew Doran.
91.2Sad *
101.2Sad * Redistribution and use in source and binary forms, with or without
111.2Sad * modification, are permitted provided that the following conditions
121.2Sad * are met:
131.2Sad * 1. Redistributions of source code must retain the above copyright
141.2Sad *    notice, this list of conditions and the following disclaimer.
151.2Sad * 2. Redistributions in binary form must reproduce the above copyright
161.2Sad *    notice, this list of conditions and the following disclaimer in the
171.2Sad *    documentation and/or other materials provided with the distribution.
181.2Sad * 3. All advertising materials mentioning features or use of this software
191.2Sad *    must display the following acknowledgement:
201.2Sad *	This product includes software developed by the NetBSD
211.2Sad *	Foundation, Inc. and its contributors.
221.2Sad * 4. Neither the name of The NetBSD Foundation nor the names of its
231.2Sad *    contributors may be used to endorse or promote products derived
241.2Sad *    from this software without specific prior written permission.
251.2Sad *
261.2Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.2Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.2Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.2Sad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.2Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.2Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.2Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.2Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.2Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.2Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.2Sad * POSSIBILITY OF SUCH DAMAGE.
371.2Sad */
381.2Sad
391.2Sad#ifndef _SPARC_MUTEX_H_
401.2Sad#define	_SPARC_MUTEX_H_
411.2Sad
421.2Sad/*
431.2Sad * There sparc mutex implementation is troublesome, because sparc (v7 and
441.2Sad * v8) lacks a compare-and-set operation, yet there are many SMP sparc
451.2Sad * machines in circulation.  SMP for spin mutexes is easy - we don't need
461.2Sad * to know who owns the lock.  For adaptive mutexes, we need an aditional
471.2Sad * interlock.
481.2Sad *
491.2Sad * The locked byte set by the sparc 'ldstub' instruction is 0xff.  sparc
501.2Sad * kernels are always loaded above 0xe0000000, and the low 5 bits of any
511.2Sad * "struct lwp *" are always zero.  So, to record the lock owner, we only
521.2Sad * need 23 bits of space.  mtxa_owner contains the mutex owner's address
531.2Sad * shifted right by 5: the top three bits of which will always be 0xe,
541.2Sad * overlapping with the interlock at the top byte, which is always 0xff
551.2Sad * when the mutex is held.
561.2Sad *
571.2Sad * For a mutex acquisition, the owner field is set in two steps: first,
581.2Sad * acquire the interlock (top byte), and second OR in the owner's address.
591.2Sad * Once the owner field is non zero, it will appear that the mutex is held,
601.2Sad * by which LWP it does not matter: other LWPs competing for the lock will
611.2Sad * fall through to mutex_vector_enter(), and either spin or sleep.
621.2Sad *
631.2Sad * As a result there is no space for a waiters bit in the owner field.  No
641.2Sad * problem, because it would be hard to synchronise using one without a CAS
651.2Sad * operation.  Note that in order to do unlocked release of adaptive
661.2Sad * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley
671.2Sad * visible on the bus.  So, adaptive mutexes share the spin lock byte with
681.2Sad * spin mutexes (set with ldstub), but it is not treated as a lock in its
691.2Sad * own right, rather as a flag that can be atomically set or cleared.
701.2Sad *
711.2Sad * When releasing an adaptive mutex, we first clear the owners field, and
721.2Sad * then check to see if the waiters byte is set.  This ensures that there
731.2Sad * will always be someone to wake any sleeping waiters up (even it the mutex
741.2Sad * is acquired immediately after we release it, or if we are preempted
751.2Sad * immediatley after clearing the owners field).  The setting or clearing of
761.2Sad * the waiters byte is serialized by the turnstile chain lock associated
771.2Sad * with the mutex.
781.2Sad *
791.2Sad * See comments in kern_mutex.c about releasing adaptive mutexes without
801.2Sad * an interlocking step.
811.2Sad */
821.2Sad
831.2Sad#ifndef __MUTEX_PRIVATE
841.2Sad
851.2Sadstruct kmutex {
861.2Sad	uintptr_t	mtx_pad1;
871.2Sad	uint32_t	mtx_pad2;
881.2Sad};
891.2Sad
901.2Sad#else	/* __MUTEX_PRIVATE */
911.2Sad
921.2Sadstruct kmutex {
931.2Sad	union {
941.2Sad		/* Adaptive mutex */
951.2Sad		volatile uintptr_t	mtxu_owner;		/* 0-3 */
961.2Sad		__cpu_simple_lock_t	mtxu_interlock;		/* 0 */
971.2Sad
981.2Sad		/* Spin mutex. */
991.2Sad		struct {
1001.2Sad			uint8_t			mtxs_dummy;	/* 0 */
1011.2Sad			uint8_t			mtxs_unused1;	/* 1 */
1021.2Sad			ipl_cookie_t		mtxs_ipl;	/* 2 */
1031.2Sad			uint8_t			mtxs_unused2;	/* 3 */
1041.2Sad		} s;
1051.2Sad	} u;
1061.2Sad	__cpu_simple_lock_t	mtx_lock;			/* 4 */
1071.2Sad	uint8_t			mtx_idtype[3];			/* 5-7 */
1081.2Sad};
1091.2Sad
1101.2Sad#define	__HAVE_MUTEX_STUBS	1
1111.4Smrg#if 0 /* does not work for MP yet */
1121.2Sad#define	__HAVE_SPIN_MUTEX_STUBS	1
1131.4Smrg#endif
1141.2Sad
1151.2Sad#define	mtx_owner	u.mtxu_owner
1161.2Sad#define	mtx_interlock	u.mtxu_interlock
1171.2Sad#define	mtx_dummy	u.s.mtxs_dummy
1181.2Sad#define	mtx_ipl		u.s.mtxs_ipl
1191.2Sad
1201.2Sadstatic inline uintptr_t
1211.2SadMUTEX_OWNER(uintptr_t owner)
1221.2Sad{
1231.2Sad	return owner << 5;
1241.2Sad}
1251.2Sad
1261.2Sadstatic inline int
1271.2SadMUTEX_OWNED(uintptr_t owner)
1281.2Sad{
1291.2Sad	return owner != 0;
1301.2Sad}
1311.2Sad
1321.2Sadstatic inline int
1331.2SadMUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
1341.2Sad{
1351.2Sad	(void)__cpu_simple_lock_try(&mtx->mtx_lock);
1361.2Sad 	return mtx->mtx_owner != 0;
1371.2Sad}
1381.2Sad
1391.2Sadstatic inline int
1401.2SadMUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
1411.2Sad{
1421.2Sad	if (mtx->mtx_owner == 0)
1431.2Sad		return 0;
1441.2Sad	return mtx->mtx_lock == __SIMPLELOCK_LOCKED;
1451.2Sad}
1461.2Sad
1471.2Sadstatic inline void
1481.2SadMUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl)
1491.2Sad{
1501.2Sad	mtx->mtx_idtype[0] = (uint8_t)id;
1511.2Sad	mtx->mtx_idtype[1] = (uint8_t)(id >> 8);
1521.2Sad	mtx->mtx_idtype[2] = (uint8_t)((id >> 16) | 0x80);
1531.2Sad	mtx->mtx_ipl = makeiplcookie(ipl);
1541.2Sad	mtx->mtx_interlock = __SIMPLELOCK_LOCKED;
1551.2Sad	__cpu_simple_lock_init(&mtx->mtx_lock);
1561.2Sad}
1571.2Sad
1581.2Sadstatic inline void
1591.2SadMUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id)
1601.2Sad{
1611.2Sad	mtx->mtx_idtype[0] = (uint8_t)id;
1621.2Sad	mtx->mtx_idtype[1] = (uint8_t)(id >> 8);
1631.2Sad	mtx->mtx_idtype[2] = (uint8_t)(id >> 16);
1641.2Sad	__cpu_simple_lock_init(&mtx->mtx_lock);
1651.2Sad}
1661.2Sad
1671.2Sadstatic inline void
1681.2SadMUTEX_DESTROY(kmutex_t *mtx)
1691.2Sad{
1701.2Sad	mtx->mtx_owner = (uintptr_t)-1L;
1711.2Sad	mtx->mtx_idtype[0] = 0xff;
1721.2Sad	mtx->mtx_idtype[1] = 0xff;
1731.2Sad	mtx->mtx_idtype[2] = 0xff;
1741.2Sad}
1751.2Sad
1761.2Sadstatic inline u_int
1771.2SadMUTEX_GETID(kmutex_t *mtx)
1781.2Sad{
1791.2Sad	return (u_int)mtx->mtx_idtype[0] |
1801.2Sad	    ((u_int)mtx->mtx_idtype[1] << 8) |
1811.2Sad	    (((u_int)mtx->mtx_idtype[2] & 0x7f) << 16);
1821.2Sad}
1831.2Sad
1841.2Sadstatic inline int
1851.2SadMUTEX_SPIN_P(volatile kmutex_t *mtx)
1861.2Sad{
1871.2Sad	return mtx->mtx_idtype[2] & 0x80;
1881.2Sad}
1891.2Sad
1901.2Sadstatic inline int
1911.2SadMUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
1921.2Sad{
1931.2Sad	return (mtx->mtx_idtype[2] & 0x80) == 0;
1941.2Sad}
1951.2Sad
1961.2Sadstatic inline int
1971.2SadMUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
1981.2Sad{
1991.2Sad	if (!__cpu_simple_lock_try(&mtx->mtx_interlock))
2001.2Sad		return 0;
2011.2Sad	mtx->mtx_owner = (curthread >> 5) | 0xf8000000;
2021.2Sad	return 1;
2031.2Sad}
2041.2Sad
2051.2Sadstatic inline void
2061.2SadMUTEX_RELEASE(kmutex_t *mtx)
2071.2Sad{
2081.2Sad	mtx->mtx_owner = 0;
2091.2Sad	__cpu_simple_unlock(&mtx->mtx_lock);
2101.2Sad}
2111.2Sad
2121.3Sadstatic inline void
2131.3SadMUTEX_CLEAR_WAITERS(kmutex_t *mtx)
2141.3Sad{
2151.3Sad	__cpu_simple_unlock(&mtx->mtx_lock);
2161.3Sad}
2171.3Sad
2181.2Sad#endif	/* __MUTEX_PRIVATE */
2191.2Sad
2201.2Sad#endif /* _SPARC_MUTEX_H_ */
221