xref: /linux/include/linux/bug.h (revision aaf5dcfb223617ac2d16113e4b500199c65689de)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
27664c5a1SJeremy Fitzhardinge #ifndef _LINUX_BUG_H
37664c5a1SJeremy Fitzhardinge #define _LINUX_BUG_H
47664c5a1SJeremy Fitzhardinge 
57664c5a1SJeremy Fitzhardinge #include <asm/bug.h>
6a3ccc497SDaniel Santos #include <linux/compiler.h>
7bc6245e5SIan Abbott #include <linux/build_bug.h>
87664c5a1SJeremy Fitzhardinge 
97664c5a1SJeremy Fitzhardinge enum bug_trap_type {
107664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_NONE = 0,
117664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_WARN = 1,
127664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_BUG = 2,
137664c5a1SJeremy Fitzhardinge };
147664c5a1SJeremy Fitzhardinge 
15608e2619SHeiko Carstens struct pt_regs;
16608e2619SHeiko Carstens 
1735edd910SPaul Gortmaker #ifdef __CHECKER__
18ff20c2e0SKirill A. Shutemov #define MAYBE_BUILD_BUG_ON(cond) (0)
1935edd910SPaul Gortmaker #else /* __CHECKER__ */
2035edd910SPaul Gortmaker 
21ff20c2e0SKirill A. Shutemov #define MAYBE_BUILD_BUG_ON(cond)			\
22ff20c2e0SKirill A. Shutemov 	do {						\
23ff20c2e0SKirill A. Shutemov 		if (__builtin_constant_p((cond)))       \
24ff20c2e0SKirill A. Shutemov 			BUILD_BUG_ON(cond);             \
25ff20c2e0SKirill A. Shutemov 		else                                    \
26ff20c2e0SKirill A. Shutemov 			BUG_ON(cond);                   \
27ff20c2e0SKirill A. Shutemov 	} while (0)
28ff20c2e0SKirill A. Shutemov 
2935edd910SPaul Gortmaker #endif	/* __CHECKER__ */
3035edd910SPaul Gortmaker 
317664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG
327664c5a1SJeremy Fitzhardinge #include <asm-generic/bug.h>
337664c5a1SJeremy Fitzhardinge 
347664c5a1SJeremy Fitzhardinge static inline int is_warning_bug(const struct bug_entry *bug)
357664c5a1SJeremy Fitzhardinge {
367664c5a1SJeremy Fitzhardinge 	return bug->flags & BUGFLAG_WARNING;
377664c5a1SJeremy Fitzhardinge }
387664c5a1SJeremy Fitzhardinge 
3919d43626SPeter Zijlstra struct bug_entry *find_bug(unsigned long bugaddr);
407664c5a1SJeremy Fitzhardinge 
41608e2619SHeiko Carstens enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
427664c5a1SJeremy Fitzhardinge 
437664c5a1SJeremy Fitzhardinge /* These are defined by the architecture */
447664c5a1SJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr);
457664c5a1SJeremy Fitzhardinge 
46*aaf5dcfbSAndi Kleen void generic_bug_clear_once(void);
47*aaf5dcfbSAndi Kleen 
487664c5a1SJeremy Fitzhardinge #else	/* !CONFIG_GENERIC_BUG */
497664c5a1SJeremy Fitzhardinge 
50608e2619SHeiko Carstens static inline enum bug_trap_type report_bug(unsigned long bug_addr,
51608e2619SHeiko Carstens 					    struct pt_regs *regs)
527664c5a1SJeremy Fitzhardinge {
537664c5a1SJeremy Fitzhardinge 	return BUG_TRAP_TYPE_BUG;
547664c5a1SJeremy Fitzhardinge }
557664c5a1SJeremy Fitzhardinge 
56*aaf5dcfbSAndi Kleen 
57*aaf5dcfbSAndi Kleen static inline void generic_bug_clear_once(void) {}
58*aaf5dcfbSAndi Kleen 
597664c5a1SJeremy Fitzhardinge #endif	/* CONFIG_GENERIC_BUG */
60de54ebbeSKees Cook 
61de54ebbeSKees Cook /*
62de54ebbeSKees Cook  * Since detected data corruption should stop operation on the affected
6385caa95bSKees Cook  * structures. Return value must be checked and sanely acted on by caller.
64de54ebbeSKees Cook  */
6585caa95bSKees Cook static inline __must_check bool check_data_corruption(bool v) { return v; }
66de54ebbeSKees Cook #define CHECK_DATA_CORRUPTION(condition, fmt, ...)			 \
6785caa95bSKees Cook 	check_data_corruption(({					 \
6885caa95bSKees Cook 		bool corruption = unlikely(condition);			 \
6985caa95bSKees Cook 		if (corruption) {					 \
70de54ebbeSKees Cook 			if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
71de54ebbeSKees Cook 				pr_err(fmt, ##__VA_ARGS__);		 \
72de54ebbeSKees Cook 				BUG();					 \
73de54ebbeSKees Cook 			} else						 \
74de54ebbeSKees Cook 				WARN(1, fmt, ##__VA_ARGS__);		 \
75de54ebbeSKees Cook 		}							 \
7685caa95bSKees Cook 		corruption;						 \
7785caa95bSKees Cook 	}))
78de54ebbeSKees Cook 
797664c5a1SJeremy Fitzhardinge #endif	/* _LINUX_BUG_H */
80