1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 27664c5a1SJeremy Fitzhardinge #ifndef _LINUX_BUG_H 37664c5a1SJeremy Fitzhardinge #define _LINUX_BUG_H 47664c5a1SJeremy Fitzhardinge 57664c5a1SJeremy Fitzhardinge #include <asm/bug.h> 6a3ccc497SDaniel Santos #include <linux/compiler.h> 7bc6245e5SIan Abbott #include <linux/build_bug.h> 87664c5a1SJeremy Fitzhardinge 97664c5a1SJeremy Fitzhardinge enum bug_trap_type { 107664c5a1SJeremy Fitzhardinge BUG_TRAP_TYPE_NONE = 0, 117664c5a1SJeremy Fitzhardinge BUG_TRAP_TYPE_WARN = 1, 127664c5a1SJeremy Fitzhardinge BUG_TRAP_TYPE_BUG = 2, 137664c5a1SJeremy Fitzhardinge }; 147664c5a1SJeremy Fitzhardinge 15608e2619SHeiko Carstens struct pt_regs; 16608e2619SHeiko Carstens 1735edd910SPaul Gortmaker #ifdef __CHECKER__ 18ff20c2e0SKirill A. Shutemov #define MAYBE_BUILD_BUG_ON(cond) (0) 1935edd910SPaul Gortmaker #else /* __CHECKER__ */ 2035edd910SPaul Gortmaker 21ff20c2e0SKirill A. Shutemov #define MAYBE_BUILD_BUG_ON(cond) \ 22ff20c2e0SKirill A. Shutemov do { \ 23ff20c2e0SKirill A. Shutemov if (__builtin_constant_p((cond))) \ 24ff20c2e0SKirill A. Shutemov BUILD_BUG_ON(cond); \ 25ff20c2e0SKirill A. Shutemov else \ 26ff20c2e0SKirill A. Shutemov BUG_ON(cond); \ 27ff20c2e0SKirill A. Shutemov } while (0) 28ff20c2e0SKirill A. Shutemov 2935edd910SPaul Gortmaker #endif /* __CHECKER__ */ 3035edd910SPaul Gortmaker 317664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG 327664c5a1SJeremy Fitzhardinge #include <asm-generic/bug.h> 337664c5a1SJeremy Fitzhardinge 347664c5a1SJeremy Fitzhardinge static inline int is_warning_bug(const struct bug_entry *bug) 357664c5a1SJeremy Fitzhardinge { 367664c5a1SJeremy Fitzhardinge return bug->flags & BUGFLAG_WARNING; 377664c5a1SJeremy Fitzhardinge } 387664c5a1SJeremy Fitzhardinge 3919d43626SPeter Zijlstra struct bug_entry *find_bug(unsigned long bugaddr); 407664c5a1SJeremy Fitzhardinge 41608e2619SHeiko Carstens enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs); 427664c5a1SJeremy Fitzhardinge 437664c5a1SJeremy Fitzhardinge /* These are defined by the architecture */ 447664c5a1SJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr); 457664c5a1SJeremy Fitzhardinge 467664c5a1SJeremy Fitzhardinge #else /* !CONFIG_GENERIC_BUG */ 477664c5a1SJeremy Fitzhardinge 48608e2619SHeiko Carstens static inline enum bug_trap_type report_bug(unsigned long bug_addr, 49608e2619SHeiko Carstens struct pt_regs *regs) 507664c5a1SJeremy Fitzhardinge { 517664c5a1SJeremy Fitzhardinge return BUG_TRAP_TYPE_BUG; 527664c5a1SJeremy Fitzhardinge } 537664c5a1SJeremy Fitzhardinge 547664c5a1SJeremy Fitzhardinge #endif /* CONFIG_GENERIC_BUG */ 55de54ebbeSKees Cook 56de54ebbeSKees Cook /* 57de54ebbeSKees Cook * Since detected data corruption should stop operation on the affected 5885caa95bSKees Cook * structures. Return value must be checked and sanely acted on by caller. 59de54ebbeSKees Cook */ 6085caa95bSKees Cook static inline __must_check bool check_data_corruption(bool v) { return v; } 61de54ebbeSKees Cook #define CHECK_DATA_CORRUPTION(condition, fmt, ...) \ 6285caa95bSKees Cook check_data_corruption(({ \ 6385caa95bSKees Cook bool corruption = unlikely(condition); \ 6485caa95bSKees Cook if (corruption) { \ 65de54ebbeSKees Cook if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \ 66de54ebbeSKees Cook pr_err(fmt, ##__VA_ARGS__); \ 67de54ebbeSKees Cook BUG(); \ 68de54ebbeSKees Cook } else \ 69de54ebbeSKees Cook WARN(1, fmt, ##__VA_ARGS__); \ 70de54ebbeSKees Cook } \ 7185caa95bSKees Cook corruption; \ 7285caa95bSKees Cook })) 73de54ebbeSKees Cook 747664c5a1SJeremy Fitzhardinge #endif /* _LINUX_BUG_H */ 75