18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _LINUX_BUG_H
38c2ecf20Sopenharmony_ci#define _LINUX_BUG_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <asm/bug.h>
68c2ecf20Sopenharmony_ci#include <linux/compiler.h>
78c2ecf20Sopenharmony_ci#include <linux/build_bug.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cienum bug_trap_type {
108c2ecf20Sopenharmony_ci	BUG_TRAP_TYPE_NONE = 0,
118c2ecf20Sopenharmony_ci	BUG_TRAP_TYPE_WARN = 1,
128c2ecf20Sopenharmony_ci	BUG_TRAP_TYPE_BUG = 2,
138c2ecf20Sopenharmony_ci};
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct pt_regs;
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#ifdef __CHECKER__
188c2ecf20Sopenharmony_ci#define MAYBE_BUILD_BUG_ON(cond) (0)
198c2ecf20Sopenharmony_ci#else /* __CHECKER__ */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define MAYBE_BUILD_BUG_ON(cond)			\
228c2ecf20Sopenharmony_ci	do {						\
238c2ecf20Sopenharmony_ci		if (__builtin_constant_p((cond)))       \
248c2ecf20Sopenharmony_ci			BUILD_BUG_ON(cond);             \
258c2ecf20Sopenharmony_ci		else                                    \
268c2ecf20Sopenharmony_ci			BUG_ON(cond);                   \
278c2ecf20Sopenharmony_ci	} while (0)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#endif	/* __CHECKER__ */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#ifdef CONFIG_GENERIC_BUG
328c2ecf20Sopenharmony_ci#include <asm-generic/bug.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic inline int is_warning_bug(const struct bug_entry *bug)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	return bug->flags & BUGFLAG_WARNING;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct bug_entry *find_bug(unsigned long bugaddr);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cienum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* These are defined by the architecture */
448c2ecf20Sopenharmony_ciint is_valid_bugaddr(unsigned long addr);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_civoid generic_bug_clear_once(void);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#else	/* !CONFIG_GENERIC_BUG */
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic inline void *find_bug(unsigned long bugaddr)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	return NULL;
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic inline enum bug_trap_type report_bug(unsigned long bug_addr,
568c2ecf20Sopenharmony_ci					    struct pt_regs *regs)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	return BUG_TRAP_TYPE_BUG;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic inline void generic_bug_clear_once(void) {}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#endif	/* CONFIG_GENERIC_BUG */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/*
678c2ecf20Sopenharmony_ci * Since detected data corruption should stop operation on the affected
688c2ecf20Sopenharmony_ci * structures. Return value must be checked and sanely acted on by caller.
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_cistatic inline __must_check bool check_data_corruption(bool v) { return v; }
718c2ecf20Sopenharmony_ci#define CHECK_DATA_CORRUPTION(condition, fmt, ...)			 \
728c2ecf20Sopenharmony_ci	check_data_corruption(({					 \
738c2ecf20Sopenharmony_ci		bool corruption = unlikely(condition);			 \
748c2ecf20Sopenharmony_ci		if (corruption) {					 \
758c2ecf20Sopenharmony_ci			if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
768c2ecf20Sopenharmony_ci				pr_err(fmt, ##__VA_ARGS__);		 \
778c2ecf20Sopenharmony_ci				BUG();					 \
788c2ecf20Sopenharmony_ci			} else						 \
798c2ecf20Sopenharmony_ci				WARN(1, fmt, ##__VA_ARGS__);		 \
808c2ecf20Sopenharmony_ci		}							 \
818c2ecf20Sopenharmony_ci		corruption;						 \
828c2ecf20Sopenharmony_ci	}))
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#endif	/* _LINUX_BUG_H */
85