1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_BUG_H
3 #define _LINUX_BUG_H
4
5 #include <asm/bug.h>
6 #include <linux/compiler.h>
7 #include <linux/build_bug.h>
8
9 enum bug_trap_type {
10 BUG_TRAP_TYPE_NONE = 0,
11 BUG_TRAP_TYPE_WARN = 1,
12 BUG_TRAP_TYPE_BUG = 2,
13 };
14
15 struct pt_regs;
16
17 #ifdef __CHECKER__
18 #define MAYBE_BUILD_BUG_ON(cond) (0)
19 #else /* __CHECKER__ */
20
21 #define MAYBE_BUILD_BUG_ON(cond) \
22 do { \
23 if (__builtin_constant_p((cond))) \
24 BUILD_BUG_ON(cond); \
25 else \
26 BUG_ON(cond); \
27 } while (0)
28
29 #endif /* __CHECKER__ */
30
31 #ifdef CONFIG_GENERIC_BUG
32 #include <asm-generic/bug.h>
33
is_warning_bug(const struct bug_entry * bug)34 static inline int is_warning_bug(const struct bug_entry *bug)
35 {
36 return bug->flags & BUGFLAG_WARNING;
37 }
38
39 void bug_get_file_line(struct bug_entry *bug, const char **file,
40 unsigned int *line);
41
42 struct bug_entry *find_bug(unsigned long bugaddr);
43
44 enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
45 enum bug_trap_type report_bug_entry(struct bug_entry *bug, struct pt_regs *regs);
46
47 /* These are defined by the architecture */
48 int is_valid_bugaddr(unsigned long addr);
49
50 void generic_bug_clear_once(void);
51
52 #else /* !CONFIG_GENERIC_BUG */
53
find_bug(unsigned long bugaddr)54 static inline void *find_bug(unsigned long bugaddr)
55 {
56 return NULL;
57 }
58
report_bug(unsigned long bug_addr,struct pt_regs * regs)59 static inline enum bug_trap_type report_bug(unsigned long bug_addr,
60 struct pt_regs *regs)
61 {
62 return BUG_TRAP_TYPE_BUG;
63 }
64
65 struct bug_entry;
66
67 static inline enum bug_trap_type
report_bug_entry(struct bug_entry * bug,struct pt_regs * regs)68 report_bug_entry(struct bug_entry *bug, struct pt_regs *regs)
69 {
70 return BUG_TRAP_TYPE_BUG;
71 }
72
bug_get_file_line(struct bug_entry * bug,const char ** file,unsigned int * line)73 static inline void bug_get_file_line(struct bug_entry *bug, const char **file,
74 unsigned int *line)
75 {
76 *file = NULL;
77 *line = 0;
78 }
79
generic_bug_clear_once(void)80 static inline void generic_bug_clear_once(void) {}
81
82 #endif /* CONFIG_GENERIC_BUG */
83
84 #ifdef CONFIG_PRINTK
85 void mem_dump_obj(void *object);
86 #else
mem_dump_obj(void * object)87 static inline void mem_dump_obj(void *object) {}
88 #endif
89
90 /*
91 * Since detected data corruption should stop operation on the affected
92 * structures. Return value must be checked and sanely acted on by caller.
93 */
check_data_corruption(bool v)94 static inline __must_check bool check_data_corruption(bool v) { return v; }
95 #define CHECK_DATA_CORRUPTION(condition, addr, fmt, ...) \
96 check_data_corruption(({ \
97 bool corruption = unlikely(condition); \
98 if (corruption) { \
99 if (addr) \
100 mem_dump_obj(addr); \
101 if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
102 pr_err(fmt, ##__VA_ARGS__); \
103 BUG(); \
104 } else \
105 WARN(1, fmt, ##__VA_ARGS__); \
106 } \
107 corruption; \
108 }))
109
110 #endif /* _LINUX_BUG_H */
111