| 1 | /*	$OpenBSD: witness.h,v 1.7 2025/07/05 09:24:37 jsg Exp $	*/ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. Berkeley Software Design Inc's name may not be used to endorse or |
| 15 | * promote products derived from this software without specific prior |
| 16 | * written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | * |
| 30 | *	from BSDI Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp |
| 31 | * $FreeBSD: head/sys/sys/lock.h 313908 2017-02-18 01:52:10Z mjg $ |
| 32 | */ |
| 33 | |
| 34 | #ifndef _SYS_WITNESS_H_ |
| 35 | #define _SYS_WITNESS_H_ |
| 36 | |
| 37 | #include <sys/_lock.h> |
| 38 | |
| 39 | /* |
| 40 | * Lock classes are statically assigned an index into the global lock_classes |
| 41 | * array. Debugging code looks up the lock class for a given lock object |
| 42 | * by indexing the array. |
| 43 | */ |
| 44 | #define	LO_CLASSINDEX(lock) \ |
| 45 | 	((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT) |
| 46 | #define	LOCK_CLASS(lock) \ |
| 47 | 	(lock_classes[LO_CLASSINDEX((lock))]) |
| 48 | #define	LOCK_CLASS_MAX \ |
| 49 | 	(LO_CLASSMASK >> LO_CLASSSHIFT) |
| 50 | |
| 51 | /* |
| 52 | * Option flags passed to lock operations that witness also needs to know |
| 53 | * about or that are generic across all locks. |
| 54 | */ |
| 55 | #define	LOP_NEWORDER	0x00000001	/* Define a new lock order. */ |
| 56 | #define	LOP_QUIET	0x00000002	/* Don't log locking operations. */ |
| 57 | #define	LOP_TRYLOCK	0x00000004	/* Don't check lock order. */ |
| 58 | #define	LOP_EXCLUSIVE	0x00000008	/* Exclusive lock. */ |
| 59 | #define	LOP_DUPOK	0x00000010	/* Don't check for duplicate acquires */ |
| 60 | |
| 61 | /* Flags passed to witness_assert. */ |
| 62 | #define	LA_MASKASSERT	0x000000ff	/* Mask for witness defined asserts. */ |
| 63 | #define	LA_UNLOCKED	0x00000000	/* Lock is unlocked. */ |
| 64 | #define	LA_LOCKED	0x00000001	/* Lock is at least share locked. */ |
| 65 | #define	LA_SLOCKED	0x00000002	/* Lock is exactly share locked. */ |
| 66 | #define	LA_XLOCKED	0x00000004	/* Lock is exclusively locked. */ |
| 67 | #define	LA_RECURSED	0x00000008	/* Lock is recursed. */ |
| 68 | #define	LA_NOTRECURSED	0x00000010	/* Lock is not recursed. */ |
| 69 | |
| 70 | #ifdef _KERNEL |
| 71 | |
| 72 | void	witness_initialize(void); |
| 73 | void	witness_init(struct lock_object *, const struct lock_type *); |
| 74 | int	witness_defineorder(struct lock_object *, struct lock_object *); |
| 75 | void	witness_checkorder(struct lock_object *, int, struct lock_object *); |
| 76 | void	witness_lock(struct lock_object *, int); |
| 77 | void	witness_upgrade(struct lock_object *, int); |
| 78 | void	witness_downgrade(struct lock_object *, int); |
| 79 | void	witness_unlock(struct lock_object *, int); |
| 80 | void	witness_setrelative(struct lock_object *, struct lock_object *, int); |
| 81 | int	witness_warn(int, struct lock_object *, const char *, ...); |
| 82 | void	witness_assert(const struct lock_object *, int); |
| 83 | void	witness_display_spinlock(struct lock_object *, struct proc *, |
| 84 | 	 int (*)(const char *, ...)); |
| 85 | void	witness_norelease(struct lock_object *); |
| 86 | void	witness_releaseok(struct lock_object *); |
| 87 | void	witness_thread_exit(struct proc *); |
| 88 | int	witness_sysctl(int *, u_int, void *, size_t *, void *, size_t); |
| 89 | int	witness_sysctl_watch(void *, size_t *, void *, size_t); |
| 90 | |
| 91 | #ifdef	WITNESS |
| 92 | |
| 93 | /* Flags for witness_warn(). */ |
| 94 | #define	WARN_KERNELOK	0x01	/* Kernel lock is exempt from this check. */ |
| 95 | #define	WARN_PANIC	0x02	/* Panic if check fails. */ |
| 96 | #define	WARN_SLEEPOK	0x04	/* Sleepable locks are exempt from check. */ |
| 97 | |
| 98 | #define	WITNESS_INITIALIZE()						\ |
| 99 | 	witness_initialize() |
| 100 | |
| 101 | #define	WITNESS_INIT(lock, type)					\ |
| 102 | 	witness_init((lock), (type)) |
| 103 | |
| 104 | #define	WITNESS_CHECKORDER(lock, flags, interlock)			\ |
| 105 | 	witness_checkorder((lock), (flags), (interlock)) |
| 106 | |
| 107 | #define	WITNESS_DEFINEORDER(lock1, lock2)				\ |
| 108 | 	witness_defineorder((struct lock_object *)(lock1),		\ |
| 109 | 	 (struct lock_object *)(lock2)) |
| 110 | |
| 111 | #define	WITNESS_LOCK(lock, flags)					\ |
| 112 | 	witness_lock((lock), (flags)) |
| 113 | |
| 114 | #define	WITNESS_UPGRADE(lock, flags)					\ |
| 115 | 	witness_upgrade((lock), (flags)) |
| 116 | |
| 117 | #define	WITNESS_DOWNGRADE(lock, flags)					\ |
| 118 | 	witness_downgrade((lock), (flags)) |
| 119 | |
| 120 | #define	WITNESS_UNLOCK(lock, flags)					\ |
| 121 | 	witness_unlock((lock), (flags)) |
| 122 | |
| 123 | /* Set permitted child lock for lock. */ |
| 124 | #define	WITNESS_SETCHILD(lock, child)					\ |
| 125 | 	witness_setrelative((lock), (child), 0) |
| 126 | |
| 127 | /* Set permitted parent lock for lock. */ |
| 128 | #define	WITNESS_SETPARENT(lock, parent)					\ |
| 129 | 	witness_setrelative((lock), (parent), 1) |
| 130 | |
| 131 | #define	WITNESS_CHECK(flags, lock, fmt, ...)				\ |
| 132 | 	witness_warn((flags), (lock), (fmt), ## __VA_ARGS__) |
| 133 | |
| 134 | #define	WITNESS_WARN(flags, lock, fmt, ...)				\ |
| 135 | 	witness_warn((flags), (lock), (fmt), ## __VA_ARGS__) |
| 136 | |
| 137 | #define	WITNESS_NORELEASE(lock)						\ |
| 138 | 	witness_norelease(&(lock)->lock_object) |
| 139 | |
| 140 | #define	WITNESS_RELEASEOK(lock)						\ |
| 141 | 	witness_releaseok(&(lock)->lock_object) |
| 142 | |
| 143 | #define	WITNESS_THREAD_EXIT(p)						\ |
| 144 | 	witness_thread_exit((p)) |
| 145 | |
| 146 | #else	/* WITNESS */ |
| 147 | #define	WITNESS_INITIALIZE()					(void)0 |
| 148 | #define	WITNESS_INIT(lock, type)				(void)0 |
| 149 | #define	WITNESS_DEFINEORDER(lock1, lock2)	0 |
| 150 | #define	WITNESS_CHECKORDER(lock, flagsi, interlock)		(void)0 |
| 151 | #define	WITNESS_LOCK(lock, flags)				(void)0 |
| 152 | #define	WITNESS_UPGRADE(lock, flags)				(void)0 |
| 153 | #define	WITNESS_DOWNGRADE(lock, flags)				(void)0 |
| 154 | #define	WITNESS_UNLOCK(lock, flags)				(void)0 |
| 155 | #define	WITNESS_SETCHILD(lock, child)				(void)0 |
| 156 | #define	WITNESS_SETPARENT(lock, parent)				(void)0 |
| 157 | #define	WITNESS_CHECK(flags, lock, fmt, ...)	0 |
| 158 | #define	WITNESS_WARN(flags, lock, fmt, ...)			(void)0 |
| 159 | #define	WITNESS_NORELEASE(lock)					(void)0 |
| 160 | #define	WITNESS_RELEASEOK(lock)					(void)0 |
| 161 | #define	WITNESS_THREAD_EXIT(p)					(void)0 |
| 162 | #endif	/* WITNESS */ |
| 163 | |
| 164 | #endif	/* _KERNEL */ |
| 165 | #endif	/* _SYS_WITNESS_H_ */ |