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__ |
Daniel Santos | ca623c9 | 2013-02-21 16:41:44 -0800 | [diff] [blame] | 16 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 17 | #define BUILD_BUG_ON_ZERO(e) (0) |
| 18 | #define BUILD_BUG_ON_NULL(e) ((void*)0) |
Tushar Behera | c5782e9 | 2012-11-26 16:29:38 -0800 | [diff] [blame] | 19 | #define BUILD_BUG_ON_INVALID(e) (0) |
Daniel Santos | ca623c9 | 2013-02-21 16:41:44 -0800 | [diff] [blame] | 20 | #define BUILD_BUG_ON(condition) (0) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 21 | #define BUILD_BUG() (0) |
| 22 | #else /* __CHECKER__ */ |
| 23 | |
| 24 | /* Force a compilation error if a constant expression is not a power of 2 */ |
| 25 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
| 26 | BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) |
| 27 | |
| 28 | /* Force a compilation error if condition is true, but also produce a |
| 29 | result (of value 0 and type size_t), so the expression can be used |
| 30 | e.g. in a structure initializer (or where-ever else comma expressions |
| 31 | aren't permitted). */ |
| 32 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) |
| 33 | #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) |
| 34 | |
Konstantin Khlebnikov | baf05aa | 2012-05-29 15:06:27 -0700 | [diff] [blame] | 35 | /* |
| 36 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the |
| 37 | * expression but avoids the generation of any code, even if that expression |
| 38 | * has side-effects. |
| 39 | */ |
| 40 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) |
| 41 | |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 42 | /** |
| 43 | * BUILD_BUG_ON - break compile if a condition is true. |
| 44 | * @condition: the condition which the compiler should know is false. |
| 45 | * |
| 46 | * 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^] | 47 | * 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] | 48 | * detect if someone changes it. |
| 49 | * |
Daniel Santos | a3ccc49 | 2013-02-21 16:41:52 -0800 | [diff] [blame^] | 50 | * The implementation uses gcc's reluctance to create a negative array, but gcc |
| 51 | * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to |
| 52 | * inline functions). Luckily, in 4.3 they added the "error" function |
| 53 | * attribute just for this type of case. Thus, we use a negative sized array |
| 54 | * (should always create an error on gcc versions older than 4.4) and then call |
| 55 | * an undefined function with the error attribute (should always create an |
| 56 | * error on gcc 4.3 and later). If for some reason, neither creates a |
| 57 | * compile-time error, we'll still have a link-time error, which is harder to |
| 58 | * track down. |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 59 | */ |
| 60 | #ifndef __OPTIMIZE__ |
| 61 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
| 62 | #else |
Daniel Santos | a3ccc49 | 2013-02-21 16:41:52 -0800 | [diff] [blame^] | 63 | #define BUILD_BUG_ON(condition) \ |
| 64 | do { \ |
| 65 | bool __cond = !!(condition); \ |
| 66 | extern void __build_bug_on_failed(void) \ |
| 67 | __compiletime_error("BUILD_BUG_ON failed"); \ |
| 68 | if (__cond) \ |
| 69 | __build_bug_on_failed(); \ |
| 70 | ((void)sizeof(char[1 - 2 * __cond])); \ |
Daniel Santos | 1d6a0d1 | 2013-02-21 16:41:45 -0800 | [diff] [blame] | 71 | } while (0) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 72 | #endif |
| 73 | |
| 74 | /** |
| 75 | * BUILD_BUG - break compile if used. |
| 76 | * |
| 77 | * If you have some code that you expect the compiler to eliminate at |
| 78 | * build time, you should use BUILD_BUG to detect if it is |
| 79 | * unexpectedly used. |
| 80 | */ |
| 81 | #define BUILD_BUG() \ |
| 82 | do { \ |
| 83 | extern void __build_bug_failed(void) \ |
Daniel Santos | 6ae8d04 | 2013-02-21 16:41:42 -0800 | [diff] [blame] | 84 | __compiletime_error("BUILD_BUG failed");\ |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 85 | __build_bug_failed(); \ |
| 86 | } while (0) |
| 87 | |
| 88 | #endif /* __CHECKER__ */ |
| 89 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 90 | #ifdef CONFIG_GENERIC_BUG |
| 91 | #include <asm-generic/bug.h> |
| 92 | |
| 93 | static inline int is_warning_bug(const struct bug_entry *bug) |
| 94 | { |
| 95 | return bug->flags & BUGFLAG_WARNING; |
| 96 | } |
| 97 | |
| 98 | const struct bug_entry *find_bug(unsigned long bugaddr); |
| 99 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 100 | 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] | 101 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 102 | /* These are defined by the architecture */ |
| 103 | int is_valid_bugaddr(unsigned long addr); |
| 104 | |
| 105 | #else /* !CONFIG_GENERIC_BUG */ |
| 106 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 107 | static inline enum bug_trap_type report_bug(unsigned long bug_addr, |
| 108 | struct pt_regs *regs) |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 109 | { |
| 110 | return BUG_TRAP_TYPE_BUG; |
| 111 | } |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 112 | |
| 113 | #endif /* CONFIG_GENERIC_BUG */ |
| 114 | #endif /* _LINUX_BUG_H */ |