162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_GENERIC_BUG_H
362306a36Sopenharmony_ci#define _ASM_GENERIC_BUG_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <linux/compiler.h>
662306a36Sopenharmony_ci#include <linux/instrumentation.h>
762306a36Sopenharmony_ci#include <linux/once_lite.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#define CUT_HERE		"------------[ cut here ]------------\n"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifdef CONFIG_GENERIC_BUG
1262306a36Sopenharmony_ci#define BUGFLAG_WARNING		(1 << 0)
1362306a36Sopenharmony_ci#define BUGFLAG_ONCE		(1 << 1)
1462306a36Sopenharmony_ci#define BUGFLAG_DONE		(1 << 2)
1562306a36Sopenharmony_ci#define BUGFLAG_NO_CUT_HERE	(1 << 3)	/* CUT_HERE already sent */
1662306a36Sopenharmony_ci#define BUGFLAG_TAINT(taint)	((taint) << 8)
1762306a36Sopenharmony_ci#define BUG_GET_TAINT(bug)	((bug)->flags >> 8)
1862306a36Sopenharmony_ci#endif
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#ifndef __ASSEMBLY__
2162306a36Sopenharmony_ci#include <linux/panic.h>
2262306a36Sopenharmony_ci#include <linux/printk.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistruct warn_args;
2562306a36Sopenharmony_cistruct pt_regs;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_civoid __warn(const char *file, int line, void *caller, unsigned taint,
2862306a36Sopenharmony_ci	    struct pt_regs *regs, struct warn_args *args);
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#ifdef CONFIG_BUG
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#ifdef CONFIG_GENERIC_BUG
3362306a36Sopenharmony_cistruct bug_entry {
3462306a36Sopenharmony_ci#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
3562306a36Sopenharmony_ci	unsigned long	bug_addr;
3662306a36Sopenharmony_ci#else
3762306a36Sopenharmony_ci	signed int	bug_addr_disp;
3862306a36Sopenharmony_ci#endif
3962306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_BUGVERBOSE
4062306a36Sopenharmony_ci#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
4162306a36Sopenharmony_ci	const char	*file;
4262306a36Sopenharmony_ci#else
4362306a36Sopenharmony_ci	signed int	file_disp;
4462306a36Sopenharmony_ci#endif
4562306a36Sopenharmony_ci	unsigned short	line;
4662306a36Sopenharmony_ci#endif
4762306a36Sopenharmony_ci	unsigned short	flags;
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci#endif	/* CONFIG_GENERIC_BUG */
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/*
5262306a36Sopenharmony_ci * Don't use BUG() or BUG_ON() unless there's really no way out; one
5362306a36Sopenharmony_ci * example might be detecting data structure corruption in the middle
5462306a36Sopenharmony_ci * of an operation that can't be backed out of.  If the (sub)system
5562306a36Sopenharmony_ci * can somehow continue operating, perhaps with reduced functionality,
5662306a36Sopenharmony_ci * it's probably not BUG-worthy.
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * If you're tempted to BUG(), think again:  is completely giving up
5962306a36Sopenharmony_ci * really the *only* solution?  There are usually better options, where
6062306a36Sopenharmony_ci * users don't need to reboot ASAP and can mostly shut down cleanly.
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_ci#ifndef HAVE_ARCH_BUG
6362306a36Sopenharmony_ci#define BUG() do { \
6462306a36Sopenharmony_ci	printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
6562306a36Sopenharmony_ci	barrier_before_unreachable(); \
6662306a36Sopenharmony_ci	panic("BUG!"); \
6762306a36Sopenharmony_ci} while (0)
6862306a36Sopenharmony_ci#endif
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#ifndef HAVE_ARCH_BUG_ON
7162306a36Sopenharmony_ci#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
7262306a36Sopenharmony_ci#endif
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/*
7562306a36Sopenharmony_ci * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
7662306a36Sopenharmony_ci * significant kernel issues that need prompt attention if they should ever
7762306a36Sopenharmony_ci * appear at runtime.
7862306a36Sopenharmony_ci *
7962306a36Sopenharmony_ci * Do not use these macros when checking for invalid external inputs
8062306a36Sopenharmony_ci * (e.g. invalid system call arguments, or invalid data coming from
8162306a36Sopenharmony_ci * network/devices), and on transient conditions like ENOMEM or EAGAIN.
8262306a36Sopenharmony_ci * These macros should be used for recoverable kernel issues only.
8362306a36Sopenharmony_ci * For invalid external inputs, transient conditions, etc use
8462306a36Sopenharmony_ci * pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary.
8562306a36Sopenharmony_ci * Do not include "BUG"/"WARNING" in format strings manually to make these
8662306a36Sopenharmony_ci * conditions distinguishable from kernel issues.
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * Use the versions with printk format strings to provide better diagnostics.
8962306a36Sopenharmony_ci */
9062306a36Sopenharmony_ciextern __printf(4, 5)
9162306a36Sopenharmony_civoid warn_slowpath_fmt(const char *file, const int line, unsigned taint,
9262306a36Sopenharmony_ci		       const char *fmt, ...);
9362306a36Sopenharmony_ciextern __printf(1, 2) void __warn_printk(const char *fmt, ...);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci#ifndef __WARN_FLAGS
9662306a36Sopenharmony_ci#define __WARN()		__WARN_printf(TAINT_WARN, NULL)
9762306a36Sopenharmony_ci#define __WARN_printf(taint, arg...) do {				\
9862306a36Sopenharmony_ci		instrumentation_begin();				\
9962306a36Sopenharmony_ci		warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);	\
10062306a36Sopenharmony_ci		instrumentation_end();					\
10162306a36Sopenharmony_ci	} while (0)
10262306a36Sopenharmony_ci#else
10362306a36Sopenharmony_ci#define __WARN()		__WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN))
10462306a36Sopenharmony_ci#define __WARN_printf(taint, arg...) do {				\
10562306a36Sopenharmony_ci		instrumentation_begin();				\
10662306a36Sopenharmony_ci		__warn_printk(arg);					\
10762306a36Sopenharmony_ci		__WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
10862306a36Sopenharmony_ci		instrumentation_end();					\
10962306a36Sopenharmony_ci	} while (0)
11062306a36Sopenharmony_ci#define WARN_ON_ONCE(condition) ({				\
11162306a36Sopenharmony_ci	int __ret_warn_on = !!(condition);			\
11262306a36Sopenharmony_ci	if (unlikely(__ret_warn_on))				\
11362306a36Sopenharmony_ci		__WARN_FLAGS(BUGFLAG_ONCE |			\
11462306a36Sopenharmony_ci			     BUGFLAG_TAINT(TAINT_WARN));	\
11562306a36Sopenharmony_ci	unlikely(__ret_warn_on);				\
11662306a36Sopenharmony_ci})
11762306a36Sopenharmony_ci#endif
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci/* used internally by panic.c */
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#ifndef WARN_ON
12262306a36Sopenharmony_ci#define WARN_ON(condition) ({						\
12362306a36Sopenharmony_ci	int __ret_warn_on = !!(condition);				\
12462306a36Sopenharmony_ci	if (unlikely(__ret_warn_on))					\
12562306a36Sopenharmony_ci		__WARN();						\
12662306a36Sopenharmony_ci	unlikely(__ret_warn_on);					\
12762306a36Sopenharmony_ci})
12862306a36Sopenharmony_ci#endif
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#ifndef WARN
13162306a36Sopenharmony_ci#define WARN(condition, format...) ({					\
13262306a36Sopenharmony_ci	int __ret_warn_on = !!(condition);				\
13362306a36Sopenharmony_ci	if (unlikely(__ret_warn_on))					\
13462306a36Sopenharmony_ci		__WARN_printf(TAINT_WARN, format);			\
13562306a36Sopenharmony_ci	unlikely(__ret_warn_on);					\
13662306a36Sopenharmony_ci})
13762306a36Sopenharmony_ci#endif
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#define WARN_TAINT(condition, taint, format...) ({			\
14062306a36Sopenharmony_ci	int __ret_warn_on = !!(condition);				\
14162306a36Sopenharmony_ci	if (unlikely(__ret_warn_on))					\
14262306a36Sopenharmony_ci		__WARN_printf(taint, format);				\
14362306a36Sopenharmony_ci	unlikely(__ret_warn_on);					\
14462306a36Sopenharmony_ci})
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#ifndef WARN_ON_ONCE
14762306a36Sopenharmony_ci#define WARN_ON_ONCE(condition)					\
14862306a36Sopenharmony_ci	DO_ONCE_LITE_IF(condition, WARN_ON, 1)
14962306a36Sopenharmony_ci#endif
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci#define WARN_ONCE(condition, format...)				\
15262306a36Sopenharmony_ci	DO_ONCE_LITE_IF(condition, WARN, 1, format)
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci#define WARN_TAINT_ONCE(condition, taint, format...)		\
15562306a36Sopenharmony_ci	DO_ONCE_LITE_IF(condition, WARN_TAINT, 1, taint, format)
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#else /* !CONFIG_BUG */
15862306a36Sopenharmony_ci#ifndef HAVE_ARCH_BUG
15962306a36Sopenharmony_ci#define BUG() do {} while (1)
16062306a36Sopenharmony_ci#endif
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci#ifndef HAVE_ARCH_BUG_ON
16362306a36Sopenharmony_ci#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
16462306a36Sopenharmony_ci#endif
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci#ifndef HAVE_ARCH_WARN_ON
16762306a36Sopenharmony_ci#define WARN_ON(condition) ({						\
16862306a36Sopenharmony_ci	int __ret_warn_on = !!(condition);				\
16962306a36Sopenharmony_ci	unlikely(__ret_warn_on);					\
17062306a36Sopenharmony_ci})
17162306a36Sopenharmony_ci#endif
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci#ifndef WARN
17462306a36Sopenharmony_ci#define WARN(condition, format...) ({					\
17562306a36Sopenharmony_ci	int __ret_warn_on = !!(condition);				\
17662306a36Sopenharmony_ci	no_printk(format);						\
17762306a36Sopenharmony_ci	unlikely(__ret_warn_on);					\
17862306a36Sopenharmony_ci})
17962306a36Sopenharmony_ci#endif
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci#define WARN_ON_ONCE(condition) WARN_ON(condition)
18262306a36Sopenharmony_ci#define WARN_ONCE(condition, format...) WARN(condition, format)
18362306a36Sopenharmony_ci#define WARN_TAINT(condition, taint, format...) WARN(condition, format)
18462306a36Sopenharmony_ci#define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci#endif
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci/*
18962306a36Sopenharmony_ci * WARN_ON_SMP() is for cases that the warning is either
19062306a36Sopenharmony_ci * meaningless for !SMP or may even cause failures.
19162306a36Sopenharmony_ci * It can also be used with values that are only defined
19262306a36Sopenharmony_ci * on SMP:
19362306a36Sopenharmony_ci *
19462306a36Sopenharmony_ci * struct foo {
19562306a36Sopenharmony_ci *  [...]
19662306a36Sopenharmony_ci * #ifdef CONFIG_SMP
19762306a36Sopenharmony_ci *	int bar;
19862306a36Sopenharmony_ci * #endif
19962306a36Sopenharmony_ci * };
20062306a36Sopenharmony_ci *
20162306a36Sopenharmony_ci * void func(struct foo *zoot)
20262306a36Sopenharmony_ci * {
20362306a36Sopenharmony_ci *	WARN_ON_SMP(!zoot->bar);
20462306a36Sopenharmony_ci *
20562306a36Sopenharmony_ci * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
20662306a36Sopenharmony_ci * and should be a nop and return false for uniprocessor.
20762306a36Sopenharmony_ci *
20862306a36Sopenharmony_ci * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
20962306a36Sopenharmony_ci * and x is true.
21062306a36Sopenharmony_ci */
21162306a36Sopenharmony_ci#ifdef CONFIG_SMP
21262306a36Sopenharmony_ci# define WARN_ON_SMP(x)			WARN_ON(x)
21362306a36Sopenharmony_ci#else
21462306a36Sopenharmony_ci/*
21562306a36Sopenharmony_ci * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
21662306a36Sopenharmony_ci * a stand alone line statement or as a condition in an if ()
21762306a36Sopenharmony_ci * statement.
21862306a36Sopenharmony_ci * A simple "0" would cause gcc to give a "statement has no effect"
21962306a36Sopenharmony_ci * warning.
22062306a36Sopenharmony_ci */
22162306a36Sopenharmony_ci# define WARN_ON_SMP(x)			({0;})
22262306a36Sopenharmony_ci#endif
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci#endif
227