1/*	$OpenBSD: witness.h,v 1.5 2019/04/23 13:35:12 visa 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
72void	witness_initialize(void);
73void	witness_init(struct lock_object *, const struct lock_type *);
74int	witness_defineorder(struct lock_object *, struct lock_object *);
75void	witness_checkorder(struct lock_object *, int, struct lock_object *);
76void	witness_lock(struct lock_object *, int);
77void	witness_upgrade(struct lock_object *, int);
78void	witness_downgrade(struct lock_object *, int);
79void	witness_unlock(struct lock_object *, int);
80int	witness_warn(int, struct lock_object *, const char *, ...);
81void	witness_assert(const struct lock_object *, int);
82void	witness_display_spinlock(struct lock_object *, struct proc *,
83	    int (*)(const char *, ...));
84int	witness_line(struct lock_object *);
85void	witness_norelease(struct lock_object *);
86void	witness_releaseok(struct lock_object *);
87const char *witness_file(struct lock_object *);
88void	witness_thread_exit(struct proc *);
89int	witness_sysctl(int *, u_int, void *, size_t *, void *, size_t);
90int	witness_sysctl_watch(void *, size_t *, void *, size_t);
91
92#ifdef	WITNESS
93
94/* Flags for witness_warn(). */
95#define	WARN_KERNELOK	0x01	/* Kernel lock is exempt from this check. */
96#define	WARN_PANIC	0x02	/* Panic if check fails. */
97#define	WARN_SLEEPOK	0x04	/* Sleepable locks are exempt from check. */
98
99#define	WITNESS_INITIALIZE()						\
100	witness_initialize()
101
102#define	WITNESS_INIT(lock, type)					\
103	witness_init((lock), (type))
104
105#define	WITNESS_CHECKORDER(lock, flags, interlock)			\
106	witness_checkorder((lock), (flags), (interlock))
107
108#define	WITNESS_DEFINEORDER(lock1, lock2)				\
109	witness_defineorder((struct lock_object *)(lock1),		\
110	    (struct lock_object *)(lock2))
111
112#define	WITNESS_LOCK(lock, flags)					\
113	witness_lock((lock), (flags))
114
115#define	WITNESS_UPGRADE(lock, flags)					\
116	witness_upgrade((lock), (flags))
117
118#define	WITNESS_DOWNGRADE(lock, flags)					\
119	witness_downgrade((lock), (flags))
120
121#define	WITNESS_UNLOCK(lock, flags)					\
122	witness_unlock((lock), (flags))
123
124#define	WITNESS_CHECK(flags, lock, fmt, ...)				\
125	witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
126
127#define	WITNESS_WARN(flags, lock, fmt, ...)				\
128	witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
129
130#define	WITNESS_NORELEASE(lock)						\
131	witness_norelease(&(lock)->lock_object)
132
133#define	WITNESS_RELEASEOK(lock)						\
134	witness_releaseok(&(lock)->lock_object)
135
136#define	WITNESS_THREAD_EXIT(p)						\
137	witness_thread_exit((p))
138
139#else	/* WITNESS */
140#define	WITNESS_INITIALIZE()					(void)0
141#define	WITNESS_INIT(lock, type)				(void)0
142#define	WITNESS_DEFINEORDER(lock1, lock2)	0
143#define	WITNESS_CHECKORDER(lock, flagsi, interlock)		(void)0
144#define	WITNESS_LOCK(lock, flags)				(void)0
145#define	WITNESS_UPGRADE(lock, flags)				(void)0
146#define	WITNESS_DOWNGRADE(lock, flags)				(void)0
147#define	WITNESS_UNLOCK(lock, flags)				(void)0
148#define	WITNESS_CHECK(flags, lock, fmt, ...)	0
149#define	WITNESS_WARN(flags, lock, fmt, ...)			(void)0
150#define	WITNESS_NORELEASE(lock)					(void)0
151#define	WITNESS_RELEASEOK(lock)					(void)0
152#define	WITNESS_THREAD_EXIT(p)					(void)0
153#endif	/* WITNESS */
154
155#endif	/* _KERNEL */
156#endif	/* _SYS_WITNESS_H_ */
157