xref: /linux/include/linux/bug.h (revision 26dbc7e299c7ebbb6a95e2c620b21b5280b37c57)
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 
39*26dbc7e2SAndrew Scull void bug_get_file_line(struct bug_entry *bug, const char **file,
40*26dbc7e2SAndrew Scull 		       unsigned int *line);
41*26dbc7e2SAndrew Scull 
4219d43626SPeter Zijlstra struct bug_entry *find_bug(unsigned long bugaddr);
437664c5a1SJeremy Fitzhardinge 
44608e2619SHeiko Carstens enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
457664c5a1SJeremy Fitzhardinge 
467664c5a1SJeremy Fitzhardinge /* These are defined by the architecture */
477664c5a1SJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr);
487664c5a1SJeremy Fitzhardinge 
49aaf5dcfbSAndi Kleen void generic_bug_clear_once(void);
50aaf5dcfbSAndi Kleen 
517664c5a1SJeremy Fitzhardinge #else	/* !CONFIG_GENERIC_BUG */
527664c5a1SJeremy Fitzhardinge 
53e336b402SMasami Hiramatsu static inline void *find_bug(unsigned long bugaddr)
54e336b402SMasami Hiramatsu {
55e336b402SMasami Hiramatsu 	return NULL;
56e336b402SMasami Hiramatsu }
57e336b402SMasami Hiramatsu 
58608e2619SHeiko Carstens static inline enum bug_trap_type report_bug(unsigned long bug_addr,
59608e2619SHeiko Carstens 					    struct pt_regs *regs)
607664c5a1SJeremy Fitzhardinge {
617664c5a1SJeremy Fitzhardinge 	return BUG_TRAP_TYPE_BUG;
627664c5a1SJeremy Fitzhardinge }
637664c5a1SJeremy Fitzhardinge 
64aaf5dcfbSAndi Kleen 
65aaf5dcfbSAndi Kleen static inline void generic_bug_clear_once(void) {}
66aaf5dcfbSAndi Kleen 
677664c5a1SJeremy Fitzhardinge #endif	/* CONFIG_GENERIC_BUG */
68de54ebbeSKees Cook 
69de54ebbeSKees Cook /*
70de54ebbeSKees Cook  * Since detected data corruption should stop operation on the affected
7185caa95bSKees Cook  * structures. Return value must be checked and sanely acted on by caller.
72de54ebbeSKees Cook  */
7385caa95bSKees Cook static inline __must_check bool check_data_corruption(bool v) { return v; }
74de54ebbeSKees Cook #define CHECK_DATA_CORRUPTION(condition, fmt, ...)			 \
7585caa95bSKees Cook 	check_data_corruption(({					 \
7685caa95bSKees Cook 		bool corruption = unlikely(condition);			 \
7785caa95bSKees Cook 		if (corruption) {					 \
78de54ebbeSKees Cook 			if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
79de54ebbeSKees Cook 				pr_err(fmt, ##__VA_ARGS__);		 \
80de54ebbeSKees Cook 				BUG();					 \
81de54ebbeSKees Cook 			} else						 \
82de54ebbeSKees Cook 				WARN(1, fmt, ##__VA_ARGS__);		 \
83de54ebbeSKees Cook 		}							 \
8485caa95bSKees Cook 		corruption;						 \
8585caa95bSKees Cook 	}))
86de54ebbeSKees Cook 
877664c5a1SJeremy Fitzhardinge #endif	/* _LINUX_BUG_H */
88