xref: /illumos-gate/usr/src/uts/sparc/v7/sys/mutex_impl.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1991-1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ifndef _SYS_MUTEX_IMPL_H
28 #define	_SYS_MUTEX_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifndef	_ASM
33 #include <sys/types.h>
34 #include <sys/machlock.h>
35 #endif
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #ifndef	_ASM
42 
43 /*
44  * mutex_enter() assumes that the mutex is adaptive and tries to grab the
45  * lock by doing an ldstub on byte 0 of the mutex.  If the ldstub fails,
46  * it means that either (1) the lock is a spin lock, or (2) the lock is
47  * adaptive but already held.  mutex_vector_enter() distinguishes these
48  * cases by looking at the mutex type, which is byte 7 for both types.
49  *
50  * Once the lock byte is set for an adaptive lock, the owner is stuffed
51  * into the remaining 24 bits such that the owner word is 0xff:owner24.
52  * This works because a thread pointer only has 24 significant bits: the
53  * upper 3 bits are 111 (since all kernel addresses are above 0xe0000000),
54  * and the lower 5 bits are zero (because threads are 32-byte aligned).
55  */
56 typedef union mutex_impl {
57 	/*
58 	 * Adaptive mutex.
59 	 */
60 	struct adaptive_mutex {
61 		uintptr_t _m_owner;	/* 0-3	owner and lock */
62 		uchar_t	_m_waiters;	/* 4	are there waiters? */
63 		uchar_t	_m_filler[2];	/* 5-6	unused */
64 		uchar_t	_m_type;	/* 7	type */
65 	} m_adaptive;
66 
67 	/*
68 	 * Spin Mutex.
69 	 */
70 	struct spin_mutex {
71 		lock_t	m_dummylock;	/* 0	lock (always set) */
72 		lock_t	m_spinlock;	/* 1	real lock */
73 		ushort_t m_oldspl;	/* 2-3	old %psr value */
74 		ushort_t m_minspl;	/* 4-5	min PSR_PIL val if lock held */
75 		uchar_t m_filler;	/* 6	unused */
76 		uchar_t _m_type;	/* 7	type */
77 	} m_spin;
78 } mutex_impl_t;
79 
80 #define	m_owner		m_adaptive._m_owner
81 #define	m_waiters	m_adaptive._m_waiters
82 #define	m_type		m_adaptive._m_type
83 
84 /*
85  * Macro to retrieve 32-bit pointer field out of mutex.
86  * Relies on 32-byte alignment of thread structures.
87  * Also relies on KERNELBASE (and all thread pointers)
88  * being above 0xe0000000.
89  */
90 #define	MUTEX_OWNER(lp)		((kthread_id_t)((lp)->m_owner << PTR24_LSB))
91 #define	MUTEX_NO_OWNER		((kthread_id_t)PTR24_BASE)
92 
93 #define	MUTEX_SET_WAITERS(lp)	((lp)->m_waiters = 1)
94 #define	MUTEX_HAS_WAITERS(lp)	((lp)->m_waiters != 0)
95 #define	MUTEX_CLEAR_LOCK_AND_WAITERS(lp)	\
96 	(lp)->m_waiters = 0, (lp)->m_owner = 0
97 
98 #define	MUTEX_SET_TYPE(lp, type)	(lp)->m_type = (type)
99 #define	MUTEX_TYPE_ADAPTIVE(lp)		((lp)->m_type == MUTEX_ADAPTIVE)
100 #define	MUTEX_TYPE_SPIN(lp)		((lp)->m_type == MUTEX_SPIN)
101 
102 #define	MUTEX_DESTROY(lp)	\
103 	(lp)->m_type = 0xdd, LOCK_INIT_HELD(&lp->m_spin.m_dummylock)
104 
105 #endif	/* _ASM */
106 
107 #ifdef	__cplusplus
108 }
109 #endif
110 
111 #endif	/* _SYS_MUTEX_IMPL_H */
112