xref: /openbmc/linux/arch/x86/include/asm/bug.h (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_BUG_H
3 #define _ASM_X86_BUG_H
4 
5 #include <linux/stringify.h>
6 #include <linux/instrumentation.h>
7 #include <linux/objtool.h>
8 
9 /*
10  * Despite that some emulators terminate on UD2, we use it for WARN().
11  */
12 #define ASM_UD2		".byte 0x0f, 0x0b"
13 #define INSN_UD2	0x0b0f
14 #define LEN_UD2		2
15 
16 /*
17  * In clang we have UD1s reporting UBSAN failures on X86, 64 and 32bit.
18  */
19 #define INSN_ASOP		0x67
20 #define OPCODE_ESCAPE		0x0f
21 #define SECOND_BYTE_OPCODE_UD1	0xb9
22 #define SECOND_BYTE_OPCODE_UD2	0x0b
23 
24 #define BUG_NONE		0xffff
25 #define BUG_UD2			0xfffe
26 #define BUG_UD1			0xfffd
27 #define BUG_UD1_UBSAN		0xfffc
28 
29 #ifdef CONFIG_GENERIC_BUG
30 
31 #ifdef CONFIG_X86_32
32 # define __BUG_REL(val)	".long " __stringify(val)
33 #else
34 # define __BUG_REL(val)	".long " __stringify(val) " - ."
35 #endif
36 
37 #ifdef CONFIG_DEBUG_BUGVERBOSE
38 
39 #define _BUG_FLAGS(ins, flags, extra)					\
40 do {									\
41 	asm_inline volatile("1:\t" ins "\n"				\
42 		     ".pushsection __bug_table,\"aw\"\n"		\
43 		     "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n"	\
44 		     "\t"  __BUG_REL(%c0) "\t# bug_entry::file\n"	\
45 		     "\t.word %c1"        "\t# bug_entry::line\n"	\
46 		     "\t.word %c2"        "\t# bug_entry::flags\n"	\
47 		     "\t.org 2b+%c3\n"					\
48 		     ".popsection\n"					\
49 		     extra						\
50 		     : : "i" (__FILE__), "i" (__LINE__),		\
51 			 "i" (flags),					\
52 			 "i" (sizeof(struct bug_entry)));		\
53 } while (0)
54 
55 #else /* !CONFIG_DEBUG_BUGVERBOSE */
56 
57 #define _BUG_FLAGS(ins, flags, extra)					\
58 do {									\
59 	asm_inline volatile("1:\t" ins "\n"				\
60 		     ".pushsection __bug_table,\"aw\"\n"		\
61 		     "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n"	\
62 		     "\t.word %c0"        "\t# bug_entry::flags\n"	\
63 		     "\t.org 2b+%c1\n"					\
64 		     ".popsection\n"					\
65 		     extra						\
66 		     : : "i" (flags),					\
67 			 "i" (sizeof(struct bug_entry)));		\
68 } while (0)
69 
70 #endif /* CONFIG_DEBUG_BUGVERBOSE */
71 
72 #else
73 
74 #define _BUG_FLAGS(ins, flags, extra)  asm volatile(ins)
75 
76 #endif /* CONFIG_GENERIC_BUG */
77 
78 #define HAVE_ARCH_BUG
79 #define BUG()							\
80 do {								\
81 	instrumentation_begin();				\
82 	_BUG_FLAGS(ASM_UD2, 0, "");				\
83 	__builtin_unreachable();				\
84 } while (0)
85 
86 /*
87  * This instrumentation_begin() is strictly speaking incorrect; but it
88  * suppresses the complaints from WARN()s in noinstr code. If such a WARN()
89  * were to trigger, we'd rather wreck the machine in an attempt to get the
90  * message out than not know about it.
91  */
92 #define __WARN_FLAGS(flags)					\
93 do {								\
94 	__auto_type __flags = BUGFLAG_WARNING|(flags);		\
95 	instrumentation_begin();				\
96 	_BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE);		\
97 	instrumentation_end();					\
98 } while (0)
99 
100 #include <asm-generic/bug.h>
101 
102 #endif /* _ASM_X86_BUG_H */
103