xref: /linux/include/linux/bug.h (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
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 
is_warning_bug(const struct bug_entry * bug)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 
3926dbc7e2SAndrew Scull void bug_get_file_line(struct bug_entry *bug, const char **file,
4026dbc7e2SAndrew Scull 		       unsigned int *line);
4126dbc7e2SAndrew 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 
find_bug(unsigned long bugaddr)53e336b402SMasami Hiramatsu static inline void *find_bug(unsigned long bugaddr)
54e336b402SMasami Hiramatsu {
55e336b402SMasami Hiramatsu 	return NULL;
56e336b402SMasami Hiramatsu }
57e336b402SMasami Hiramatsu 
report_bug(unsigned long bug_addr,struct pt_regs * regs)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 
64*d3debfccSMarc Zyngier struct bug_entry;
bug_get_file_line(struct bug_entry * bug,const char ** file,unsigned int * line)65*d3debfccSMarc Zyngier static inline void bug_get_file_line(struct bug_entry *bug, const char **file,
66*d3debfccSMarc Zyngier 				     unsigned int *line)
67*d3debfccSMarc Zyngier {
68*d3debfccSMarc Zyngier 	*file = NULL;
69*d3debfccSMarc Zyngier 	*line = 0;
70*d3debfccSMarc Zyngier }
71aaf5dcfbSAndi Kleen 
generic_bug_clear_once(void)72aaf5dcfbSAndi Kleen static inline void generic_bug_clear_once(void) {}
73aaf5dcfbSAndi Kleen 
747664c5a1SJeremy Fitzhardinge #endif	/* CONFIG_GENERIC_BUG */
75de54ebbeSKees Cook 
76de54ebbeSKees Cook /*
77de54ebbeSKees Cook  * Since detected data corruption should stop operation on the affected
7885caa95bSKees Cook  * structures. Return value must be checked and sanely acted on by caller.
79de54ebbeSKees Cook  */
check_data_corruption(bool v)8085caa95bSKees Cook static inline __must_check bool check_data_corruption(bool v) { return v; }
81de54ebbeSKees Cook #define CHECK_DATA_CORRUPTION(condition, fmt, ...)			 \
8285caa95bSKees Cook 	check_data_corruption(({					 \
8385caa95bSKees Cook 		bool corruption = unlikely(condition);			 \
8485caa95bSKees Cook 		if (corruption) {					 \
85de54ebbeSKees Cook 			if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
86de54ebbeSKees Cook 				pr_err(fmt, ##__VA_ARGS__);		 \
87de54ebbeSKees Cook 				BUG();					 \
88de54ebbeSKees Cook 			} else						 \
89de54ebbeSKees Cook 				WARN(1, fmt, ##__VA_ARGS__);		 \
90de54ebbeSKees Cook 		}							 \
9185caa95bSKees Cook 		corruption;						 \
9285caa95bSKees Cook 	}))
93de54ebbeSKees Cook 
947664c5a1SJeremy Fitzhardinge #endif	/* _LINUX_BUG_H */
95