| 1 | /*	$OpenBSD: mutex.h,v 1.26 2025/12/11 23:34:44 dlg Exp $	*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2004 Artur Grabowski <art@openbsd.org> |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifndef _SYS_MUTEX_H_ |
| 20 | #define _SYS_MUTEX_H_ |
| 21 | |
| 22 | /* |
| 23 | * A mutex is: |
| 24 | * - owned by a cpu. |
| 25 | * - non-recursive. |
| 26 | * - spinning. |
| 27 | * - not providing mutual exclusion between processes, only cpus. |
| 28 | * - providing interrupt blocking when necessary. |
| 29 | * |
| 30 | * Different mutexes can be nested, but not interleaved. This is ok: |
| 31 | * "mtx_enter(foo); mtx_enter(bar); mtx_leave(bar); mtx_leave(foo);" |
| 32 | * This is _not_ ok: |
| 33 | * "mtx_enter(foo); mtx_enter(bar); mtx_leave(foo); mtx_leave(bar);" |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * To prevent lock ordering problems with the kernel lock, we need to |
| 38 | * make sure we block all interrupts that can grab the kernel lock. |
| 39 | * The simplest way to achieve this is to make sure mutexes always |
| 40 | * raise the interrupt priority level to the highest level that has |
| 41 | * interrupts that grab the kernel lock. |
| 42 | */ |
| 43 | #ifdef MULTIPROCESSOR |
| 44 | #define __MUTEX_IPL(ipl) \ |
| 45 | 	(((ipl) < IPL_MPFLOOR) ? IPL_MPFLOOR : (ipl)) |
| 46 | #else |
| 47 | #define __MUTEX_IPL(ipl) (ipl) |
| 48 | #endif |
| 49 | |
| 50 | #include <machine/mutex.h> |
| 51 | |
| 52 | #ifdef __USE_MI_MUTEX |
| 53 | |
| 54 | #include <sys/_lock.h> |
| 55 | |
| 56 | struct mutex { |
| 57 | 	volatile unsigned long mtx_owner; |
| 58 | 	int mtx_wantipl; |
| 59 | 	int mtx_oldipl; |
| 60 | #ifdef WITNESS |
| 61 | 	struct lock_object mtx_lock_obj; |
| 62 | #endif |
| 63 | }; |
| 64 | |
| 65 | #ifdef WITNESS |
| 66 | #define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ |
| 67 | 	{ 0, __MUTEX_IPL((ipl)), IPL_NONE, MTX_LO_INITIALIZER(name, flags) } |
| 68 | #else |
| 69 | #define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ |
| 70 | 	{ 0, __MUTEX_IPL((ipl)), IPL_NONE } |
| 71 | #endif |
| 72 | |
| 73 | void __mtx_init(struct mutex *, int); |
| 74 | #define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) |
| 75 | |
| 76 | #define mtx_curcpu() (unsigned long)curcpu() |
| 77 | #define mtx_owner(mtx) ((mtx)->mtx_owner & ~1UL) |
| 78 | |
| 79 | #ifdef DIAGNOSTIC |
| 80 | #define MUTEX_ASSERT_LOCKED(mtx) do {					\ |
| 81 | 	if (mtx_owner(mtx) != mtx_curcpu() && !(panicstr || db_active))	\ |
| 82 | 		panic("mutex %p not held in %s", (mtx), __func__);	\ |
| 83 | } while (0) |
| 84 | |
| 85 | #define MUTEX_ASSERT_UNLOCKED(mtx) do {					\ |
| 86 | 	if (mtx_owner(mtx) == mtx_curcpu() && !(panicstr || db_active))	\ |
| 87 | 		panic("mutex %p held in %s", (mtx), __func__);		\ |
| 88 | } while (0) |
| 89 | #else |
| 90 | #define MUTEX_ASSERT_LOCKED(mtx) do { (void)(mtx); } while (0) |
| 91 | #define MUTEX_ASSERT_UNLOCKED(mtx) do { (void)(mtx); } while (0) |
| 92 | #endif |
| 93 | |
| 94 | #define MUTEX_LOCK_OBJECT(mtx)	(&(mtx)->mtx_lock_obj) |
| 95 | #define MUTEX_OLDIPL(mtx)	(mtx)->mtx_oldipl |
| 96 | |
| 97 | #endif	/* __USE_MI_MUTEX */ |
| 98 | |
| 99 | |
| 100 | #define MTX_LO_FLAGS(flags) \ |
| 101 | 	((!((flags) & MTX_NOWITNESS) ? LO_WITNESS : 0) | \ |
| 102 | 	 ((flags) & MTX_DUPOK ? LO_DUPOK : 0) | \ |
| 103 | 	 LO_INITIALIZED | (LO_CLASS_MUTEX << LO_CLASSSHIFT)) |
| 104 | |
| 105 | #define __MTX_STRING(x) #x |
| 106 | #define __MTX_S(x) __MTX_STRING(x) |
| 107 | #define __MTX_NAME __FILE__ ":" __MTX_S(__LINE__) |
| 108 | |
| 109 | #define MTX_LO_INITIALIZER(name, flags) \ |
| 110 | 	{ .lo_type = &(const struct lock_type){ .lt_name = __MTX_NAME }, \ |
| 111 | 	 .lo_name = (name), \ |
| 112 | 	 .lo_flags = MTX_LO_FLAGS(flags) } |
| 113 | |
| 114 | #define MTX_NOWITNESS	0x01 |
| 115 | #define MTX_DUPOK	0x02 |
| 116 | |
| 117 | #define MUTEX_INITIALIZER(ipl) \ |
| 118 | 	MUTEX_INITIALIZER_FLAGS(ipl, __MTX_NAME, 0) |
| 119 | |
| 120 | /* |
| 121 | * Some architectures need to do magic for the ipl, so they need a macro. |
| 122 | */ |
| 123 | #ifndef _mtx_init |
| 124 | void _mtx_init(struct mutex *, int); |
| 125 | #endif |
| 126 | |
| 127 | void	mtx_enter(struct mutex *); |
| 128 | int	mtx_enter_try(struct mutex *); |
| 129 | void	mtx_leave(struct mutex *); |
| 130 | |
| 131 | #define mtx_init(m, ipl)	mtx_init_flags(m, ipl, NULL, 0) |
| 132 | |
| 133 | #define mtx_owned(mtx) \ |
| 134 | 	((mtx_owner(mtx) == mtx_curcpu()) || panicstr || db_active) |
| 135 | |
| 136 | #ifdef WITNESS |
| 137 | |
| 138 | void	_mtx_init_flags(struct mutex *, int, const char *, int, |
| 139 | 	 const struct lock_type *); |
| 140 | |
| 141 | #define mtx_init_flags(m, ipl, name, flags) do {			\ |
| 142 | 	static const struct lock_type __lock_type = { .lt_name = #m };	\ |
| 143 | 	_mtx_init_flags(m, ipl, name, flags, &__lock_type);		\ |
| 144 | } while (0) |
| 145 | |
| 146 | #else /* WITNESS */ |
| 147 | |
| 148 | #define mtx_init_flags(m, ipl, name, flags) do {			\ |
| 149 | 	(void)(name); (void)(flags);					\ |
| 150 | 	_mtx_init(m, ipl);						\ |
| 151 | } while (0) |
| 152 | |
| 153 | #define _mtx_init_flags(m,i,n,f,t)	_mtx_init(m,i) |
| 154 | |
| 155 | #endif /* WITNESS */ |
| 156 | |
| 157 | #if defined(_KERNEL) && defined(DDB) |
| 158 | |
| 159 | struct db_mutex { |
| 160 | 	struct cpu_info	*volatile mtx_owner; |
| 161 | 	unsigned long	 mtx_intr_state; |
| 162 | }; |
| 163 | |
| 164 | #define DB_MUTEX_INITIALIZER	{ NULL, 0 } |
| 165 | |
| 166 | void	db_mtx_enter(struct db_mutex *); |
| 167 | void	db_mtx_leave(struct db_mutex *); |
| 168 | |
| 169 | #endif /* _KERNEL && DDB */ |
| 170 | |
| 171 | #endif |