blob: fe5916550da8c5c4da102dd91385eef8123167f6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08002#ifndef _LINUX_BUG_H
3#define _LINUX_BUG_H
4
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08005#include <asm/bug.h>
Daniel Santosa3ccc492013-02-21 16:41:52 -08006#include <linux/compiler.h>
Ian Abbottbc6245e2017-07-10 15:51:07 -07007#include <linux/build_bug.h>
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08008
9enum bug_trap_type {
10 BUG_TRAP_TYPE_NONE = 0,
11 BUG_TRAP_TYPE_WARN = 1,
12 BUG_TRAP_TYPE_BUG = 2,
13};
14
Heiko Carstens608e2612007-07-15 23:41:39 -070015struct pt_regs;
16
Paul Gortmaker35edd912011-11-16 23:51:05 -050017#ifdef __CHECKER__
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053018#define MAYBE_BUILD_BUG_ON(cond) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050019#else /* __CHECKER__ */
20
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053021#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 Gortmaker35edd912011-11-16 23:51:05 -050029#endif /* __CHECKER__ */
30
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080031#ifdef CONFIG_GENERIC_BUG
32#include <asm-generic/bug.h>
33
34static inline int is_warning_bug(const struct bug_entry *bug)
35{
36 return bug->flags & BUGFLAG_WARNING;
37}
38
Peter Zijlstra19d43622017-02-25 08:56:53 +010039struct bug_entry *find_bug(unsigned long bugaddr);
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080040
Heiko Carstens608e2612007-07-15 23:41:39 -070041enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080042
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080043/* These are defined by the architecture */
44int is_valid_bugaddr(unsigned long addr);
45
Andi Kleenaaf5dcf2017-11-17 15:27:06 -080046void generic_bug_clear_once(void);
47
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080048#else /* !CONFIG_GENERIC_BUG */
49
Heiko Carstens608e2612007-07-15 23:41:39 -070050static inline enum bug_trap_type report_bug(unsigned long bug_addr,
51 struct pt_regs *regs)
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080052{
53 return BUG_TRAP_TYPE_BUG;
54}
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080055
Andi Kleenaaf5dcf2017-11-17 15:27:06 -080056
57static inline void generic_bug_clear_once(void) {}
58
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080059#endif /* CONFIG_GENERIC_BUG */
Kees Cookde54ebbe2016-08-17 14:42:11 -070060
61/*
62 * Since detected data corruption should stop operation on the affected
Kees Cook85caa952017-02-24 15:00:38 -080063 * structures. Return value must be checked and sanely acted on by caller.
Kees Cookde54ebbe2016-08-17 14:42:11 -070064 */
Kees Cook85caa952017-02-24 15:00:38 -080065static inline __must_check bool check_data_corruption(bool v) { return v; }
Kees Cookde54ebbe2016-08-17 14:42:11 -070066#define CHECK_DATA_CORRUPTION(condition, fmt, ...) \
Kees Cook85caa952017-02-24 15:00:38 -080067 check_data_corruption(({ \
68 bool corruption = unlikely(condition); \
69 if (corruption) { \
Kees Cookde54ebbe2016-08-17 14:42:11 -070070 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 Cookde54ebbe2016-08-17 14:42:11 -070075 } \
Kees Cook85caa952017-02-24 15:00:38 -080076 corruption; \
77 }))
Kees Cookde54ebbe2016-08-17 14:42:11 -070078
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080079#endif /* _LINUX_BUG_H */