Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 2 | #ifndef _LINUX_BUG_H |
| 3 | #define _LINUX_BUG_H |
| 4 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 5 | #include <asm/bug.h> |
Daniel Santos | a3ccc49 | 2013-02-21 16:41:52 -0800 | [diff] [blame] | 6 | #include <linux/compiler.h> |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 7 | #include <linux/build_bug.h> |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 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 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 15 | struct pt_regs; |
| 16 | |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 17 | #ifdef __CHECKER__ |
Kirill A. Shutemov | ff20c2e | 2016-03-01 09:45:14 +0530 | [diff] [blame] | 18 | #define MAYBE_BUILD_BUG_ON(cond) (0) |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 19 | #else /* __CHECKER__ */ |
| 20 | |
Kirill A. Shutemov | ff20c2e | 2016-03-01 09:45:14 +0530 | [diff] [blame] | 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 | |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 29 | #endif /* __CHECKER__ */ |
| 30 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 31 | #ifdef CONFIG_GENERIC_BUG |
| 32 | #include <asm-generic/bug.h> |
| 33 | |
| 34 | static inline int is_warning_bug(const struct bug_entry *bug) |
| 35 | { |
| 36 | return bug->flags & BUGFLAG_WARNING; |
| 37 | } |
| 38 | |
Peter Zijlstra | 19d4362 | 2017-02-25 08:56:53 +0100 | [diff] [blame] | 39 | struct bug_entry *find_bug(unsigned long bugaddr); |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 40 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 41 | 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] | 42 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 43 | /* These are defined by the architecture */ |
| 44 | int is_valid_bugaddr(unsigned long addr); |
| 45 | |
Andi Kleen | aaf5dcf | 2017-11-17 15:27:06 -0800 | [diff] [blame] | 46 | void generic_bug_clear_once(void); |
| 47 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 48 | #else /* !CONFIG_GENERIC_BUG */ |
| 49 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 50 | static inline enum bug_trap_type report_bug(unsigned long bug_addr, |
| 51 | struct pt_regs *regs) |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 52 | { |
| 53 | return BUG_TRAP_TYPE_BUG; |
| 54 | } |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 55 | |
Andi Kleen | aaf5dcf | 2017-11-17 15:27:06 -0800 | [diff] [blame] | 56 | |
| 57 | static inline void generic_bug_clear_once(void) {} |
| 58 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 59 | #endif /* CONFIG_GENERIC_BUG */ |
Kees Cook | de54ebb | 2016-08-17 14:42:11 -0700 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * Since detected data corruption should stop operation on the affected |
Kees Cook | 85caa95 | 2017-02-24 15:00:38 -0800 | [diff] [blame] | 63 | * structures. Return value must be checked and sanely acted on by caller. |
Kees Cook | de54ebb | 2016-08-17 14:42:11 -0700 | [diff] [blame] | 64 | */ |
Kees Cook | 85caa95 | 2017-02-24 15:00:38 -0800 | [diff] [blame] | 65 | static inline __must_check bool check_data_corruption(bool v) { return v; } |
Kees Cook | de54ebb | 2016-08-17 14:42:11 -0700 | [diff] [blame] | 66 | #define CHECK_DATA_CORRUPTION(condition, fmt, ...) \ |
Kees Cook | 85caa95 | 2017-02-24 15:00:38 -0800 | [diff] [blame] | 67 | check_data_corruption(({ \ |
| 68 | bool corruption = unlikely(condition); \ |
| 69 | if (corruption) { \ |
Kees Cook | de54ebb | 2016-08-17 14:42:11 -0700 | [diff] [blame] | 70 | if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \ |
| 71 | pr_err(fmt, ##__VA_ARGS__); \ |
| 72 | BUG(); \ |
| 73 | } else \ |
| 74 | WARN(1, fmt, ##__VA_ARGS__); \ |
Kees Cook | de54ebb | 2016-08-17 14:42:11 -0700 | [diff] [blame] | 75 | } \ |
Kees Cook | 85caa95 | 2017-02-24 15:00:38 -0800 | [diff] [blame] | 76 | corruption; \ |
| 77 | })) |
Kees Cook | de54ebb | 2016-08-17 14:42:11 -0700 | [diff] [blame] | 78 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 79 | #endif /* _LINUX_BUG_H */ |