1#ifndef _LINUX_BUG_H
2#define _LINUX_BUG_H
3
4#include <asm/bug.h>
5#include <linux/compiler.h>
6
7enum bug_trap_type {
8 BUG_TRAP_TYPE_NONE = 0,
9 BUG_TRAP_TYPE_WARN = 1,
10 BUG_TRAP_TYPE_BUG = 2,
11};
12
13struct pt_regs;
14
15#ifdef __CHECKER__
16#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
17#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
18#define BUILD_BUG_ON_ZERO(e) (0)
19#define BUILD_BUG_ON_NULL(e) ((void*)0)
20#define BUILD_BUG_ON_INVALID(e) (0)
21#define BUILD_BUG_ON_MSG(cond, msg) (0)
22#define BUILD_BUG_ON(condition) (0)
23#define BUILD_BUG() (0)
24#define MAYBE_BUILD_BUG_ON(cond) (0)
25#else
26
27
28#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
29 BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
30#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
31 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
32
33
34
35
36
37#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
38#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
39
40
41
42
43
44
45#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
46
47
48
49
50
51
52
53
54#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74#ifndef __OPTIMIZE__
75#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
76#else
77#define BUILD_BUG_ON(condition) \
78 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
79#endif
80
81
82
83
84
85
86
87
88#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
89
90#define MAYBE_BUILD_BUG_ON(cond) \
91 do { \
92 if (__builtin_constant_p((cond))) \
93 BUILD_BUG_ON(cond); \
94 else \
95 BUG_ON(cond); \
96 } while (0)
97
98#endif
99
100#ifdef CONFIG_GENERIC_BUG
101#include <asm-generic/bug.h>
102
103static inline int is_warning_bug(const struct bug_entry *bug)
104{
105 return bug->flags & BUGFLAG_WARNING;
106}
107
108struct bug_entry *find_bug(unsigned long bugaddr);
109
110enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
111
112
113int is_valid_bugaddr(unsigned long addr);
114
115#else
116
117static inline enum bug_trap_type report_bug(unsigned long bug_addr,
118 struct pt_regs *regs)
119{
120 return BUG_TRAP_TYPE_BUG;
121}
122
123#endif
124
125
126
127
128
129static inline __must_check bool check_data_corruption(bool v) { return v; }
130#define CHECK_DATA_CORRUPTION(condition, fmt, ...) \
131 check_data_corruption(({ \
132 bool corruption = unlikely(condition); \
133 if (corruption) { \
134 if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
135 pr_err(fmt, ##__VA_ARGS__); \
136 BUG(); \
137 } else \
138 WARN(1, fmt, ##__VA_ARGS__); \
139 } \
140 corruption; \
141 }))
142
143#endif
144