xref: /openbmc/linux/include/asm-generic/bug.h (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_BUG_H
3 #define _ASM_GENERIC_BUG_H
4 
5 #include <linux/compiler.h>
6 
7 #ifdef CONFIG_GENERIC_BUG
8 #define BUGFLAG_WARNING		(1 << 0)
9 #define BUGFLAG_ONCE		(1 << 1)
10 #define BUGFLAG_DONE		(1 << 2)
11 #define BUGFLAG_TAINT(taint)	((taint) << 8)
12 #define BUG_GET_TAINT(bug)	((bug)->flags >> 8)
13 #endif
14 
15 #ifndef __ASSEMBLY__
16 #include <linux/kernel.h>
17 
18 #ifdef CONFIG_BUG
19 
20 #ifdef CONFIG_GENERIC_BUG
21 struct bug_entry {
22 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
23 	unsigned long	bug_addr;
24 #else
25 	signed int	bug_addr_disp;
26 #endif
27 #ifdef CONFIG_DEBUG_BUGVERBOSE
28 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
29 	const char	*file;
30 #else
31 	signed int	file_disp;
32 #endif
33 	unsigned short	line;
34 #endif
35 	unsigned short	flags;
36 };
37 #endif	/* CONFIG_GENERIC_BUG */
38 
39 /*
40  * Don't use BUG() or BUG_ON() unless there's really no way out; one
41  * example might be detecting data structure corruption in the middle
42  * of an operation that can't be backed out of.  If the (sub)system
43  * can somehow continue operating, perhaps with reduced functionality,
44  * it's probably not BUG-worthy.
45  *
46  * If you're tempted to BUG(), think again:  is completely giving up
47  * really the *only* solution?  There are usually better options, where
48  * users don't need to reboot ASAP and can mostly shut down cleanly.
49  */
50 #ifndef HAVE_ARCH_BUG
51 #define BUG() do { \
52 	printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
53 	panic("BUG!"); \
54 } while (0)
55 #endif
56 
57 #ifndef HAVE_ARCH_BUG_ON
58 #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
59 #endif
60 
61 #ifdef __WARN_FLAGS
62 #define __WARN_TAINT(taint)		__WARN_FLAGS(BUGFLAG_TAINT(taint))
63 #define __WARN_ONCE_TAINT(taint)	__WARN_FLAGS(BUGFLAG_ONCE|BUGFLAG_TAINT(taint))
64 
65 #define WARN_ON_ONCE(condition) ({				\
66 	int __ret_warn_on = !!(condition);			\
67 	if (unlikely(__ret_warn_on))				\
68 		__WARN_ONCE_TAINT(TAINT_WARN);			\
69 	unlikely(__ret_warn_on);				\
70 })
71 #endif
72 
73 /*
74  * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
75  * significant issues that need prompt attention if they should ever
76  * appear at runtime.  Use the versions with printk format strings
77  * to provide better diagnostics.
78  */
79 #ifndef __WARN_TAINT
80 extern __printf(3, 4)
81 void warn_slowpath_fmt(const char *file, const int line,
82 		       const char *fmt, ...);
83 extern __printf(4, 5)
84 void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint,
85 			     const char *fmt, ...);
86 extern void warn_slowpath_null(const char *file, const int line);
87 #define WANT_WARN_ON_SLOWPATH
88 #define __WARN()		warn_slowpath_null(__FILE__, __LINE__)
89 #define __WARN_printf(arg...)	warn_slowpath_fmt(__FILE__, __LINE__, arg)
90 #define __WARN_printf_taint(taint, arg...)				\
91 	warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg)
92 #else
93 #define __WARN()		__WARN_TAINT(TAINT_WARN)
94 #define __WARN_printf(arg...)	do { printk(arg); __WARN(); } while (0)
95 #define __WARN_printf_taint(taint, arg...)				\
96 	do { printk(arg); __WARN_TAINT(taint); } while (0)
97 #endif
98 
99 /* used internally by panic.c */
100 struct warn_args;
101 struct pt_regs;
102 
103 void __warn(const char *file, int line, void *caller, unsigned taint,
104 	    struct pt_regs *regs, struct warn_args *args);
105 
106 #ifndef WARN_ON
107 #define WARN_ON(condition) ({						\
108 	int __ret_warn_on = !!(condition);				\
109 	if (unlikely(__ret_warn_on))					\
110 		__WARN();						\
111 	unlikely(__ret_warn_on);					\
112 })
113 #endif
114 
115 #ifndef WARN
116 #define WARN(condition, format...) ({					\
117 	int __ret_warn_on = !!(condition);				\
118 	if (unlikely(__ret_warn_on))					\
119 		__WARN_printf(format);					\
120 	unlikely(__ret_warn_on);					\
121 })
122 #endif
123 
124 #define WARN_TAINT(condition, taint, format...) ({			\
125 	int __ret_warn_on = !!(condition);				\
126 	if (unlikely(__ret_warn_on))					\
127 		__WARN_printf_taint(taint, format);			\
128 	unlikely(__ret_warn_on);					\
129 })
130 
131 #ifndef WARN_ON_ONCE
132 #define WARN_ON_ONCE(condition)	({				\
133 	static bool __section(.data.unlikely) __warned;		\
134 	int __ret_warn_once = !!(condition);			\
135 								\
136 	if (unlikely(__ret_warn_once && !__warned)) {		\
137 		__warned = true;				\
138 		WARN_ON(1);					\
139 	}							\
140 	unlikely(__ret_warn_once);				\
141 })
142 #endif
143 
144 #define WARN_ONCE(condition, format...)	({			\
145 	static bool __section(.data.unlikely) __warned;		\
146 	int __ret_warn_once = !!(condition);			\
147 								\
148 	if (unlikely(__ret_warn_once && !__warned)) {		\
149 		__warned = true;				\
150 		WARN(1, format);				\
151 	}							\
152 	unlikely(__ret_warn_once);				\
153 })
154 
155 #define WARN_TAINT_ONCE(condition, taint, format...)	({	\
156 	static bool __section(.data.unlikely) __warned;		\
157 	int __ret_warn_once = !!(condition);			\
158 								\
159 	if (unlikely(__ret_warn_once && !__warned)) {		\
160 		__warned = true;				\
161 		WARN_TAINT(1, taint, format);			\
162 	}							\
163 	unlikely(__ret_warn_once);				\
164 })
165 
166 #else /* !CONFIG_BUG */
167 #ifndef HAVE_ARCH_BUG
168 #define BUG() do {} while (1)
169 #endif
170 
171 #ifndef HAVE_ARCH_BUG_ON
172 #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
173 #endif
174 
175 #ifndef HAVE_ARCH_WARN_ON
176 #define WARN_ON(condition) ({						\
177 	int __ret_warn_on = !!(condition);				\
178 	unlikely(__ret_warn_on);					\
179 })
180 #endif
181 
182 #ifndef WARN
183 #define WARN(condition, format...) ({					\
184 	int __ret_warn_on = !!(condition);				\
185 	no_printk(format);						\
186 	unlikely(__ret_warn_on);					\
187 })
188 #endif
189 
190 #define WARN_ON_ONCE(condition) WARN_ON(condition)
191 #define WARN_ONCE(condition, format...) WARN(condition, format)
192 #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
193 #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
194 
195 #endif
196 
197 /*
198  * WARN_ON_SMP() is for cases that the warning is either
199  * meaningless for !SMP or may even cause failures.
200  * This is usually used for cases that we have
201  * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
202  * returns 0 for uniprocessor settings.
203  * It can also be used with values that are only defined
204  * on SMP:
205  *
206  * struct foo {
207  *  [...]
208  * #ifdef CONFIG_SMP
209  *	int bar;
210  * #endif
211  * };
212  *
213  * void func(struct foo *zoot)
214  * {
215  *	WARN_ON_SMP(!zoot->bar);
216  *
217  * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
218  * and should be a nop and return false for uniprocessor.
219  *
220  * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
221  * and x is true.
222  */
223 #ifdef CONFIG_SMP
224 # define WARN_ON_SMP(x)			WARN_ON(x)
225 #else
226 /*
227  * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
228  * a stand alone line statement or as a condition in an if ()
229  * statement.
230  * A simple "0" would cause gcc to give a "statement has no effect"
231  * warning.
232  */
233 # define WARN_ON_SMP(x)			({0;})
234 #endif
235 
236 #endif /* __ASSEMBLY__ */
237 
238 #endif
239