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