]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/kassert.h
MFV: xz-embedded 3f438e15109229bb14ab45f285f4bff5412a9542
[FreeBSD/FreeBSD.git] / sys / sys / kassert.h
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1999 Eivind Eklund <eivind@FreeBSD.org>
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. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``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 THE REGENTS OR CONTRIBUTORS 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
31 #ifndef _SYS_KASSERT_H_
32 #define _SYS_KASSERT_H_
33
34 #include <sys/cdefs.h>
35
36 #ifdef _KERNEL
37 extern const char *panicstr;    /* panic message */
38 extern bool panicked;
39 #define KERNEL_PANICKED()       __predict_false(panicked)
40
41 #ifdef  INVARIANTS              /* The option is always available */
42 #define VNASSERT(exp, vp, msg) do {                                     \
43         if (__predict_false(!(exp))) {                                  \
44                 vn_printf(vp, "VNASSERT failed: %s not true at %s:%d (%s)\n",\
45                    #exp, __FILE__, __LINE__, __func__);                 \
46                 kassert_panic msg;                                      \
47         }                                                               \
48 } while (0)
49 #define MPASSERT(exp, mp, msg) do {                                     \
50         if (__predict_false(!(exp))) {                                  \
51                 printf("MPASSERT mp %p failed: %s not true at %s:%d (%s)\n",\
52                     (mp), #exp, __FILE__, __LINE__, __func__);          \
53                 kassert_panic msg;                                      \
54         }                                                               \
55 } while (0)
56 #define VNPASS(exp, vp) do {                                            \
57         const char *_exp = #exp;                                        \
58         VNASSERT(exp, vp, ("condition %s not met at %s:%d (%s)",        \
59             _exp, __FILE__, __LINE__, __func__));                       \
60 } while (0)
61 #define MPPASS(exp, mp) do {                                            \
62         const char *_exp = #exp;                                        \
63         MPASSERT(exp, mp, ("condition %s not met at %s:%d (%s)",        \
64             _exp, __FILE__, __LINE__, __func__));                       \
65 } while (0)
66 #define __assert_unreachable() \
67         panic("executing segment marked as unreachable at %s:%d (%s)\n", \
68             __FILE__, __LINE__, __func__)
69 #else   /* INVARIANTS */
70 #define VNASSERT(exp, vp, msg) do { \
71 } while (0)
72 #define MPASSERT(exp, mp, msg) do { \
73 } while (0)
74 #define VNPASS(exp, vp) do { \
75 } while (0)
76 #define MPPASS(exp, mp) do { \
77 } while (0)
78 #define __assert_unreachable()  __unreachable()
79 #endif  /* INVARIANTS */
80
81 #ifndef CTASSERT        /* Allow lint to override */
82 #define CTASSERT(x)     _Static_assert(x, "compile-time assertion failed")
83 #endif
84
85 /*
86  * These functions need to be declared before the KASSERT macro is invoked in
87  * !KASSERT_PANIC_OPTIONAL builds, so their declarations are sort of out of
88  * place compared to other function definitions in this header.  On the other
89  * hand, this header is a bit disorganized anyway.
90  */
91 void    panic(const char *, ...) __dead2 __printflike(1, 2);
92 void    vpanic(const char *, __va_list) __dead2 __printflike(1, 0);
93 #endif  /* _KERNEL */
94
95 #if defined(_STANDALONE)
96 /*
97  * Until we have more experience with KASSERTS that are called
98  * from the boot loader, they are off. The bootloader does this
99  * a little differently than the kernel (we just call printf atm).
100  * we avoid most of the common functions in the boot loader, so
101  * declare printf() here too.
102  */
103 int     printf(const char *, ...) __printflike(1, 2);
104 #  define kassert_panic printf
105 #else /* !_STANDALONE */
106 #  if defined(WITNESS) || defined(INVARIANT_SUPPORT)
107 #    ifdef KASSERT_PANIC_OPTIONAL
108 void    kassert_panic(const char *fmt, ...)  __printflike(1, 2);
109 #    else
110 #      define kassert_panic     panic
111 #    endif /* KASSERT_PANIC_OPTIONAL */
112 #  endif /* defined(WITNESS) || defined(INVARIANT_SUPPORT) */
113 #endif /* _STANDALONE */
114
115 #if (defined(_KERNEL) && defined(INVARIANTS)) || defined(_STANDALONE)
116 #define KASSERT(exp,msg) do {                                           \
117         if (__predict_false(!(exp)))                                    \
118                 kassert_panic msg;                                      \
119 } while (0)
120 #else /* !(KERNEL && INVARIANTS) && !_STANDALONE */
121 #define KASSERT(exp,msg) do { \
122 } while (0)
123 #endif /* (_KERNEL && INVARIANTS) || _STANDALONE */
124
125 #ifdef _KERNEL
126 /*
127  * Helpful macros for quickly coming up with assertions with informative
128  * panic messages.
129  */
130 #define MPASS(ex)               MPASS4(ex, #ex, __FILE__, __LINE__)
131 #define MPASS2(ex, what)        MPASS4(ex, what, __FILE__, __LINE__)
132 #define MPASS3(ex, file, line)  MPASS4(ex, #ex, file, line)
133 #define MPASS4(ex, what, file, line)                                    \
134         KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
135
136 /*
137  * Assert that a pointer can be loaded from memory atomically.
138  *
139  * This assertion enforces stronger alignment than necessary.  For example,
140  * on some architectures, atomicity for unaligned loads will depend on
141  * whether or not the load spans multiple cache lines.
142  */
143 #define ASSERT_ATOMIC_LOAD_PTR(var, msg)                                \
144         KASSERT(sizeof(var) == sizeof(void *) &&                        \
145             ((uintptr_t)&(var) & (sizeof(void *) - 1)) == 0, msg)
146 /*
147  * Assert that a thread is in critical(9) section.
148  */
149 #define CRITICAL_ASSERT(td)                                             \
150         KASSERT((td)->td_critnest >= 1, ("Not in critical section"))
151
152 /*
153  * If we have already panic'd and this is the thread that called
154  * panic(), then don't block on any mutexes but silently succeed.
155  * Otherwise, the kernel will deadlock since the scheduler isn't
156  * going to run the thread that holds any lock we need.
157  */
158 #define SCHEDULER_STOPPED_TD(td)  ({                                    \
159         MPASS((td) == curthread);                                       \
160         __predict_false((td)->td_stopsched);                            \
161 })
162 #define SCHEDULER_STOPPED() SCHEDULER_STOPPED_TD(curthread)
163 #endif /* _KERNEL */
164
165 #endif  /* _SYS_KASSERT_H_ */