Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 1 | #ifndef _LINUX_BUG_H |
| 2 | #define _LINUX_BUG_H |
| 3 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 4 | #include <asm/bug.h> |
Daniel Santos | a3ccc49 | 2013-02-21 16:41:52 -0800 | [diff] [blame] | 5 | #include <linux/compiler.h> |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 6 | |
| 7 | enum bug_trap_type { |
| 8 | BUG_TRAP_TYPE_NONE = 0, |
| 9 | BUG_TRAP_TYPE_WARN = 1, |
| 10 | BUG_TRAP_TYPE_BUG = 2, |
| 11 | }; |
| 12 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 13 | struct pt_regs; |
| 14 | |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 15 | #ifdef __CHECKER__ |
Jakub Kicinski | 3e9b311 | 2016-08-31 12:46:44 +0100 | [diff] [blame] | 16 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) |
Daniel Santos | ca623c9 | 2013-02-21 16:41:44 -0800 | [diff] [blame] | 17 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 18 | #define BUILD_BUG_ON_ZERO(e) (0) |
| 19 | #define BUILD_BUG_ON_NULL(e) ((void*)0) |
Tushar Behera | c5782e9 | 2012-11-26 16:29:38 -0800 | [diff] [blame] | 20 | #define BUILD_BUG_ON_INVALID(e) (0) |
Daniel Santos | 9a8ab1c | 2013-02-21 16:41:55 -0800 | [diff] [blame] | 21 | #define BUILD_BUG_ON_MSG(cond, msg) (0) |
Daniel Santos | ca623c9 | 2013-02-21 16:41:44 -0800 | [diff] [blame] | 22 | #define BUILD_BUG_ON(condition) (0) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 23 | #define BUILD_BUG() (0) |
Kirill A. Shutemov | ff20c2e | 2016-03-01 09:45:14 +0530 | [diff] [blame] | 24 | #define MAYBE_BUILD_BUG_ON(cond) (0) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 25 | #else /* __CHECKER__ */ |
| 26 | |
| 27 | /* Force a compilation error if a constant expression is not a power of 2 */ |
Jakub Kicinski | 3e9b311 | 2016-08-31 12:46:44 +0100 | [diff] [blame] | 28 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
| 29 | BUILD_BUG_ON(((n) & ((n) - 1)) != 0) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 30 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
| 31 | BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) |
| 32 | |
| 33 | /* Force a compilation error if condition is true, but also produce a |
| 34 | result (of value 0 and type size_t), so the expression can be used |
| 35 | e.g. in a structure initializer (or where-ever else comma expressions |
| 36 | aren't permitted). */ |
| 37 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) |
| 38 | #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) |
| 39 | |
Konstantin Khlebnikov | baf05aa | 2012-05-29 15:06:27 -0700 | [diff] [blame] | 40 | /* |
| 41 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the |
| 42 | * expression but avoids the generation of any code, even if that expression |
| 43 | * has side-effects. |
| 44 | */ |
| 45 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) |
| 46 | |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 47 | /** |
Daniel Santos | 9a8ab1c | 2013-02-21 16:41:55 -0800 | [diff] [blame] | 48 | * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied |
| 49 | * error message. |
| 50 | * @condition: the condition which the compiler should know is false. |
| 51 | * |
| 52 | * See BUILD_BUG_ON for description. |
| 53 | */ |
| 54 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) |
| 55 | |
| 56 | /** |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 57 | * BUILD_BUG_ON - break compile if a condition is true. |
| 58 | * @condition: the condition which the compiler should know is false. |
| 59 | * |
| 60 | * If you have some code which relies on certain constants being equal, or |
Daniel Santos | a3ccc49 | 2013-02-21 16:41:52 -0800 | [diff] [blame] | 61 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 62 | * detect if someone changes it. |
| 63 | * |
Daniel Santos | a3ccc49 | 2013-02-21 16:41:52 -0800 | [diff] [blame] | 64 | * The implementation uses gcc's reluctance to create a negative array, but gcc |
| 65 | * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to |
| 66 | * inline functions). Luckily, in 4.3 they added the "error" function |
| 67 | * attribute just for this type of case. Thus, we use a negative sized array |
| 68 | * (should always create an error on gcc versions older than 4.4) and then call |
| 69 | * an undefined function with the error attribute (should always create an |
| 70 | * error on gcc 4.3 and later). If for some reason, neither creates a |
| 71 | * compile-time error, we'll still have a link-time error, which is harder to |
| 72 | * track down. |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 73 | */ |
| 74 | #ifndef __OPTIMIZE__ |
| 75 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
| 76 | #else |
Daniel Santos | 9a8ab1c | 2013-02-21 16:41:55 -0800 | [diff] [blame] | 77 | #define BUILD_BUG_ON(condition) \ |
| 78 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | /** |
| 82 | * BUILD_BUG - break compile if used. |
| 83 | * |
| 84 | * If you have some code that you expect the compiler to eliminate at |
| 85 | * build time, you should use BUILD_BUG to detect if it is |
| 86 | * unexpectedly used. |
| 87 | */ |
Daniel Santos | 9a8ab1c | 2013-02-21 16:41:55 -0800 | [diff] [blame] | 88 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 89 | |
Kirill A. Shutemov | ff20c2e | 2016-03-01 09:45:14 +0530 | [diff] [blame] | 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 | |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 98 | #endif /* __CHECKER__ */ |
| 99 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 100 | #ifdef CONFIG_GENERIC_BUG |
| 101 | #include <asm-generic/bug.h> |
| 102 | |
| 103 | static inline int is_warning_bug(const struct bug_entry *bug) |
| 104 | { |
| 105 | return bug->flags & BUGFLAG_WARNING; |
| 106 | } |
| 107 | |
| 108 | const struct bug_entry *find_bug(unsigned long bugaddr); |
| 109 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 110 | enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs); |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 111 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 112 | /* These are defined by the architecture */ |
| 113 | int is_valid_bugaddr(unsigned long addr); |
| 114 | |
| 115 | #else /* !CONFIG_GENERIC_BUG */ |
| 116 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 117 | static inline enum bug_trap_type report_bug(unsigned long bug_addr, |
| 118 | struct pt_regs *regs) |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 119 | { |
| 120 | return BUG_TRAP_TYPE_BUG; |
| 121 | } |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 122 | |
| 123 | #endif /* CONFIG_GENERIC_BUG */ |
Syed Rameez Mustafa | cf9c1e4 | 2013-07-15 11:52:09 -0700 | [diff] [blame] | 124 | |
| 125 | #ifdef CONFIG_PANIC_ON_DATA_CORRUPTION |
| 126 | #define PANIC_CORRUPTION 1 |
| 127 | #else |
| 128 | #define PANIC_CORRUPTION 0 |
| 129 | #endif /* CONFIG_PANIC_ON_DATA_CORRUPTION */ |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 130 | #endif /* _LINUX_BUG_H */ |