1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_POWERPC_BUG_H
3#define _ASM_POWERPC_BUG_H
4#ifdef __KERNEL__
5
6#include <asm/asm-compat.h>
7#include <asm/extable.h>
8
9#ifdef CONFIG_BUG
10
11#ifdef __ASSEMBLY__
12#include <asm/asm-offsets.h>
13#ifdef CONFIG_DEBUG_BUGVERBOSE
14.macro __EMIT_BUG_ENTRY addr,file,line,flags
15	 .section __bug_table,"aw"
165001:	 .4byte \addr - .
17	 .4byte 5002f - .
18	 .short \line, \flags
19	 .org 5001b+BUG_ENTRY_SIZE
20	 .previous
21	 .section .rodata,"a"
225002:	 .asciz "\file"
23	 .previous
24.endm
25#else
26.macro __EMIT_BUG_ENTRY addr,file,line,flags
27	 .section __bug_table,"aw"
285001:	 .4byte \addr - .
29	 .short \flags
30	 .org 5001b+BUG_ENTRY_SIZE
31	 .previous
32.endm
33#endif /* verbose */
34
35.macro EMIT_WARN_ENTRY addr,file,line,flags
36	EX_TABLE(\addr,\addr+4)
37	__EMIT_BUG_ENTRY \addr,\file,\line,\flags
38.endm
39
40.macro EMIT_BUG_ENTRY addr,file,line,flags
41	.if \flags & 1 /* BUGFLAG_WARNING */
42	.err /* Use EMIT_WARN_ENTRY for warnings */
43	.endif
44	__EMIT_BUG_ENTRY \addr,\file,\line,\flags
45.endm
46
47#else /* !__ASSEMBLY__ */
48/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3 to be FILE, LINE, flags and
49   sizeof(struct bug_entry), respectively */
50#ifdef CONFIG_DEBUG_BUGVERBOSE
51#define _EMIT_BUG_ENTRY				\
52	".section __bug_table,\"aw\"\n"		\
53	"2:	.4byte 1b - .\n"		\
54	"	.4byte %0 - .\n"		\
55	"	.short %1, %2\n"		\
56	".org 2b+%3\n"				\
57	".previous\n"
58#else
59#define _EMIT_BUG_ENTRY				\
60	".section __bug_table,\"aw\"\n"		\
61	"2:	.4byte 1b - .\n"		\
62	"	.short %2\n"			\
63	".org 2b+%3\n"				\
64	".previous\n"
65#endif
66
67#define BUG_ENTRY(insn, flags, ...)			\
68	__asm__ __volatile__(				\
69		"1:	" insn "\n"			\
70		_EMIT_BUG_ENTRY				\
71		: : "i" (__FILE__), "i" (__LINE__),	\
72		  "i" (flags),				\
73		  "i" (sizeof(struct bug_entry)),	\
74		  ##__VA_ARGS__)
75
76#define WARN_ENTRY(insn, flags, label, ...)		\
77	asm_volatile_goto(				\
78		"1:	" insn "\n"			\
79		EX_TABLE(1b, %l[label])			\
80		_EMIT_BUG_ENTRY				\
81		: : "i" (__FILE__), "i" (__LINE__),	\
82		  "i" (flags),				\
83		  "i" (sizeof(struct bug_entry)),	\
84		  ##__VA_ARGS__ : : label)
85
86/*
87 * BUG_ON() and WARN_ON() do their best to cooperate with compile-time
88 * optimisations. However depending on the complexity of the condition
89 * some compiler versions may not produce optimal results.
90 */
91
92#define BUG() do {						\
93	BUG_ENTRY("twi 31, 0, 0", 0);				\
94	unreachable();						\
95} while (0)
96#define HAVE_ARCH_BUG
97
98#define __WARN_FLAGS(flags) do {				\
99	__label__ __label_warn_on;				\
100								\
101	WARN_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags), __label_warn_on); \
102	unreachable();						\
103								\
104__label_warn_on:						\
105	break;							\
106} while (0)
107
108#ifdef CONFIG_PPC64
109#define BUG_ON(x) do {						\
110	if (__builtin_constant_p(x)) {				\
111		if (x)						\
112			BUG();					\
113	} else {						\
114		BUG_ENTRY(PPC_TLNEI " %4, 0", 0, "r" ((__force long)(x)));	\
115	}							\
116} while (0)
117
118#define WARN_ON(x) ({						\
119	bool __ret_warn_on = false;				\
120	do {							\
121		if (__builtin_constant_p((x))) {		\
122			if (!(x)) 				\
123				break; 				\
124			__WARN();				\
125			__ret_warn_on = true;			\
126		} else {					\
127			__label__ __label_warn_on;		\
128								\
129			WARN_ENTRY(PPC_TLNEI " %4, 0",		\
130				   BUGFLAG_WARNING | BUGFLAG_TAINT(TAINT_WARN),	\
131				   __label_warn_on,		\
132				   "r" ((__force long)(x)));	\
133			break;					\
134__label_warn_on:						\
135			__ret_warn_on = true;			\
136		}						\
137	} while (0);						\
138	unlikely(__ret_warn_on);				\
139})
140
141#define HAVE_ARCH_BUG_ON
142#define HAVE_ARCH_WARN_ON
143#endif
144
145#endif /* __ASSEMBLY __ */
146#else
147#ifdef __ASSEMBLY__
148.macro EMIT_BUG_ENTRY addr,file,line,flags
149.endm
150.macro EMIT_WARN_ENTRY addr,file,line,flags
151.endm
152#else /* !__ASSEMBLY__ */
153#define _EMIT_BUG_ENTRY
154#define _EMIT_WARN_ENTRY
155#endif
156#endif /* CONFIG_BUG */
157
158#include <asm-generic/bug.h>
159
160#ifndef __ASSEMBLY__
161
162struct pt_regs;
163void hash__do_page_fault(struct pt_regs *);
164void bad_page_fault(struct pt_regs *, int);
165extern void _exception(int, struct pt_regs *, int, unsigned long);
166extern void _exception_pkey(struct pt_regs *, unsigned long, int);
167extern void die(const char *, struct pt_regs *, long);
168void die_mce(const char *str, struct pt_regs *regs, long err);
169extern bool die_will_crash(void);
170extern void panic_flush_kmsg_start(void);
171extern void panic_flush_kmsg_end(void);
172#endif /* !__ASSEMBLY__ */
173
174#endif /* __KERNEL__ */
175#endif /* _ASM_POWERPC_BUG_H */
176