xref: /openbmc/linux/arch/sh/include/asm/bug.h (revision b2441318)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2f15cbe6fSPaul Mundt #ifndef __ASM_SH_BUG_H
3f15cbe6fSPaul Mundt #define __ASM_SH_BUG_H
4f15cbe6fSPaul Mundt 
5e839ca52SDavid Howells #include <linux/linkage.h>
6e839ca52SDavid Howells 
7f15cbe6fSPaul Mundt #define TRAPA_BUG_OPCODE	0xc33e	/* trapa #0x3e */
8e115f2c1SPaul Mundt #define BUGFLAG_UNWINDER	(1 << 1)
9f15cbe6fSPaul Mundt 
10f15cbe6fSPaul Mundt #ifdef CONFIG_GENERIC_BUG
11f15cbe6fSPaul Mundt #define HAVE_ARCH_BUG
12f15cbe6fSPaul Mundt #define HAVE_ARCH_WARN_ON
13f15cbe6fSPaul Mundt 
14f15cbe6fSPaul Mundt /**
15f15cbe6fSPaul Mundt  * _EMIT_BUG_ENTRY
16f15cbe6fSPaul Mundt  * %1 - __FILE__
17f15cbe6fSPaul Mundt  * %2 - __LINE__
18f15cbe6fSPaul Mundt  * %3 - trap type
19f15cbe6fSPaul Mundt  * %4 - sizeof(struct bug_entry)
20f15cbe6fSPaul Mundt  *
21f15cbe6fSPaul Mundt  * The trapa opcode itself sits in %0.
22f15cbe6fSPaul Mundt  * The %O notation is used to avoid # generation.
23f15cbe6fSPaul Mundt  *
24f15cbe6fSPaul Mundt  * The offending file and line are encoded in the __bug_table section.
25f15cbe6fSPaul Mundt  */
26f15cbe6fSPaul Mundt #ifdef CONFIG_DEBUG_BUGVERBOSE
27f15cbe6fSPaul Mundt #define _EMIT_BUG_ENTRY				\
28325cdacdSJosh Poimboeuf 	"\t.pushsection __bug_table,\"aw\"\n"	\
29f15cbe6fSPaul Mundt 	"2:\t.long 1b, %O1\n"			\
30f15cbe6fSPaul Mundt 	"\t.short %O2, %O3\n"			\
31f15cbe6fSPaul Mundt 	"\t.org 2b+%O4\n"			\
32f15cbe6fSPaul Mundt 	"\t.popsection\n"
33f15cbe6fSPaul Mundt #else
34f15cbe6fSPaul Mundt #define _EMIT_BUG_ENTRY				\
35325cdacdSJosh Poimboeuf 	"\t.pushsection __bug_table,\"aw\"\n"	\
36f15cbe6fSPaul Mundt 	"2:\t.long 1b\n"			\
37f15cbe6fSPaul Mundt 	"\t.short %O3\n"			\
38f15cbe6fSPaul Mundt 	"\t.org 2b+%O4\n"			\
39f15cbe6fSPaul Mundt 	"\t.popsection\n"
40f15cbe6fSPaul Mundt #endif
41f15cbe6fSPaul Mundt 
42f15cbe6fSPaul Mundt #define BUG()						\
43f15cbe6fSPaul Mundt do {							\
44f15cbe6fSPaul Mundt 	__asm__ __volatile__ (				\
45f15cbe6fSPaul Mundt 		"1:\t.short %O0\n"			\
46f15cbe6fSPaul Mundt 		_EMIT_BUG_ENTRY				\
47f15cbe6fSPaul Mundt 		 :					\
48f15cbe6fSPaul Mundt 		 : "n" (TRAPA_BUG_OPCODE),		\
49f15cbe6fSPaul Mundt 		   "i" (__FILE__),			\
50f15cbe6fSPaul Mundt 		   "i" (__LINE__), "i" (0),		\
51f15cbe6fSPaul Mundt 		   "i" (sizeof(struct bug_entry)));	\
52579e1452SKees Cook 	unreachable();					\
53f15cbe6fSPaul Mundt } while (0)
54f15cbe6fSPaul Mundt 
5519d43626SPeter Zijlstra #define __WARN_FLAGS(flags)				\
56f15cbe6fSPaul Mundt do {							\
57f15cbe6fSPaul Mundt 	__asm__ __volatile__ (				\
58f15cbe6fSPaul Mundt 		"1:\t.short %O0\n"			\
59f15cbe6fSPaul Mundt 		 _EMIT_BUG_ENTRY			\
60f15cbe6fSPaul Mundt 		 :					\
61f15cbe6fSPaul Mundt 		 : "n" (TRAPA_BUG_OPCODE),		\
62f15cbe6fSPaul Mundt 		   "i" (__FILE__),			\
63f15cbe6fSPaul Mundt 		   "i" (__LINE__),			\
6419d43626SPeter Zijlstra 		   "i" (BUGFLAG_WARNING|(flags)),	\
65f15cbe6fSPaul Mundt 		   "i" (sizeof(struct bug_entry)));	\
66f15cbe6fSPaul Mundt } while (0)
67f15cbe6fSPaul Mundt 
68f15cbe6fSPaul Mundt #define WARN_ON(x) ({						\
69f15cbe6fSPaul Mundt 	int __ret_warn_on = !!(x);				\
70f15cbe6fSPaul Mundt 	if (__builtin_constant_p(__ret_warn_on)) {		\
71f15cbe6fSPaul Mundt 		if (__ret_warn_on)				\
72f15cbe6fSPaul Mundt 			__WARN();				\
73f15cbe6fSPaul Mundt 	} else {						\
74f15cbe6fSPaul Mundt 		if (unlikely(__ret_warn_on))			\
75f15cbe6fSPaul Mundt 			__WARN();				\
76f15cbe6fSPaul Mundt 	}							\
77f15cbe6fSPaul Mundt 	unlikely(__ret_warn_on);				\
78f15cbe6fSPaul Mundt })
79f15cbe6fSPaul Mundt 
80b344e24aSMatt Fleming #define UNWINDER_BUG()					\
81b344e24aSMatt Fleming do {							\
82b344e24aSMatt Fleming 	__asm__ __volatile__ (				\
83b344e24aSMatt Fleming 		"1:\t.short %O0\n"			\
84b344e24aSMatt Fleming 		_EMIT_BUG_ENTRY				\
85b344e24aSMatt Fleming 		 :					\
86e115f2c1SPaul Mundt 		 : "n" (TRAPA_BUG_OPCODE),		\
87b344e24aSMatt Fleming 		   "i" (__FILE__),			\
88e115f2c1SPaul Mundt 		   "i" (__LINE__),			\
89e115f2c1SPaul Mundt 		   "i" (BUGFLAG_UNWINDER),		\
90b344e24aSMatt Fleming 		   "i" (sizeof(struct bug_entry)));	\
91b344e24aSMatt Fleming } while (0)
92b344e24aSMatt Fleming 
93b344e24aSMatt Fleming #define UNWINDER_BUG_ON(x) ({					\
94b344e24aSMatt Fleming 	int __ret_unwinder_on = !!(x);				\
95b344e24aSMatt Fleming 	if (__builtin_constant_p(__ret_unwinder_on)) {		\
96b344e24aSMatt Fleming 		if (__ret_unwinder_on)				\
97b344e24aSMatt Fleming 			UNWINDER_BUG();				\
98b344e24aSMatt Fleming 	} else {						\
99b344e24aSMatt Fleming 		if (unlikely(__ret_unwinder_on))		\
100b344e24aSMatt Fleming 			UNWINDER_BUG();				\
101b344e24aSMatt Fleming 	}							\
102b344e24aSMatt Fleming 	unlikely(__ret_unwinder_on);				\
103b344e24aSMatt Fleming })
104b344e24aSMatt Fleming 
10574db2479SPaul Mundt #else
10674db2479SPaul Mundt 
10774db2479SPaul Mundt #define UNWINDER_BUG	BUG
10874db2479SPaul Mundt #define UNWINDER_BUG_ON	BUG_ON
10974db2479SPaul Mundt 
110f15cbe6fSPaul Mundt #endif /* CONFIG_GENERIC_BUG */
111f15cbe6fSPaul Mundt 
112f15cbe6fSPaul Mundt #include <asm-generic/bug.h>
113f15cbe6fSPaul Mundt 
114e839ca52SDavid Howells struct pt_regs;
1155f857bceSPaul Mundt 
1165f857bceSPaul Mundt /* arch/sh/kernel/traps.c */
117e839ca52SDavid Howells extern void die(const char *str, struct pt_regs *regs, long err) __attribute__ ((noreturn));
1185f857bceSPaul Mundt extern void die_if_kernel(const char *str, struct pt_regs *regs, long err);
1195f857bceSPaul Mundt extern void die_if_no_fixup(const char *str, struct pt_regs *regs, long err);
120e839ca52SDavid Howells 
121f15cbe6fSPaul Mundt #endif /* __ASM_SH_BUG_H */
122