xref: /linux/include/linux/bug.h (revision bc6245e5efd70c41eaf9334b1b5e646745cb0fb3)
17664c5a1SJeremy Fitzhardinge #ifndef _LINUX_BUG_H
27664c5a1SJeremy Fitzhardinge #define _LINUX_BUG_H
37664c5a1SJeremy Fitzhardinge 
47664c5a1SJeremy Fitzhardinge #include <asm/bug.h>
5a3ccc497SDaniel Santos #include <linux/compiler.h>
6*bc6245e5SIan Abbott #include <linux/build_bug.h>
77664c5a1SJeremy Fitzhardinge 
87664c5a1SJeremy Fitzhardinge enum bug_trap_type {
97664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_NONE = 0,
107664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_WARN = 1,
117664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_BUG = 2,
127664c5a1SJeremy Fitzhardinge };
137664c5a1SJeremy Fitzhardinge 
14608e2619SHeiko Carstens struct pt_regs;
15608e2619SHeiko Carstens 
1635edd910SPaul Gortmaker #ifdef __CHECKER__
17ff20c2e0SKirill A. Shutemov #define MAYBE_BUILD_BUG_ON(cond) (0)
1835edd910SPaul Gortmaker #else /* __CHECKER__ */
1935edd910SPaul Gortmaker 
20ff20c2e0SKirill A. Shutemov #define MAYBE_BUILD_BUG_ON(cond)			\
21ff20c2e0SKirill A. Shutemov 	do {						\
22ff20c2e0SKirill A. Shutemov 		if (__builtin_constant_p((cond)))       \
23ff20c2e0SKirill A. Shutemov 			BUILD_BUG_ON(cond);             \
24ff20c2e0SKirill A. Shutemov 		else                                    \
25ff20c2e0SKirill A. Shutemov 			BUG_ON(cond);                   \
26ff20c2e0SKirill A. Shutemov 	} while (0)
27ff20c2e0SKirill A. Shutemov 
2835edd910SPaul Gortmaker #endif	/* __CHECKER__ */
2935edd910SPaul Gortmaker 
307664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG
317664c5a1SJeremy Fitzhardinge #include <asm-generic/bug.h>
327664c5a1SJeremy Fitzhardinge 
337664c5a1SJeremy Fitzhardinge static inline int is_warning_bug(const struct bug_entry *bug)
347664c5a1SJeremy Fitzhardinge {
357664c5a1SJeremy Fitzhardinge 	return bug->flags & BUGFLAG_WARNING;
367664c5a1SJeremy Fitzhardinge }
377664c5a1SJeremy Fitzhardinge 
3819d43626SPeter Zijlstra struct bug_entry *find_bug(unsigned long bugaddr);
397664c5a1SJeremy Fitzhardinge 
40608e2619SHeiko Carstens enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
417664c5a1SJeremy Fitzhardinge 
427664c5a1SJeremy Fitzhardinge /* These are defined by the architecture */
437664c5a1SJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr);
447664c5a1SJeremy Fitzhardinge 
457664c5a1SJeremy Fitzhardinge #else	/* !CONFIG_GENERIC_BUG */
467664c5a1SJeremy Fitzhardinge 
47608e2619SHeiko Carstens static inline enum bug_trap_type report_bug(unsigned long bug_addr,
48608e2619SHeiko Carstens 					    struct pt_regs *regs)
497664c5a1SJeremy Fitzhardinge {
507664c5a1SJeremy Fitzhardinge 	return BUG_TRAP_TYPE_BUG;
517664c5a1SJeremy Fitzhardinge }
527664c5a1SJeremy Fitzhardinge 
537664c5a1SJeremy Fitzhardinge #endif	/* CONFIG_GENERIC_BUG */
54de54ebbeSKees Cook 
55de54ebbeSKees Cook /*
56de54ebbeSKees Cook  * Since detected data corruption should stop operation on the affected
5785caa95bSKees Cook  * structures. Return value must be checked and sanely acted on by caller.
58de54ebbeSKees Cook  */
5985caa95bSKees Cook static inline __must_check bool check_data_corruption(bool v) { return v; }
60de54ebbeSKees Cook #define CHECK_DATA_CORRUPTION(condition, fmt, ...)			 \
6185caa95bSKees Cook 	check_data_corruption(({					 \
6285caa95bSKees Cook 		bool corruption = unlikely(condition);			 \
6385caa95bSKees Cook 		if (corruption) {					 \
64de54ebbeSKees Cook 			if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
65de54ebbeSKees Cook 				pr_err(fmt, ##__VA_ARGS__);		 \
66de54ebbeSKees Cook 				BUG();					 \
67de54ebbeSKees Cook 			} else						 \
68de54ebbeSKees Cook 				WARN(1, fmt, ##__VA_ARGS__);		 \
69de54ebbeSKees Cook 		}							 \
7085caa95bSKees Cook 		corruption;						 \
7185caa95bSKees Cook 	}))
72de54ebbeSKees Cook 
737664c5a1SJeremy Fitzhardinge #endif	/* _LINUX_BUG_H */
74