xref: /linux/arch/x86/include/asm/bug.h (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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_UD1			0xfffe
26 #define BUG_UD2			0xfffd
27 
28 #ifdef CONFIG_GENERIC_BUG
29 
30 #ifdef CONFIG_X86_32
31 # define __BUG_REL(val)	".long " __stringify(val)
32 #else
33 # define __BUG_REL(val)	".long " __stringify(val) " - ."
34 #endif
35 
36 #ifdef CONFIG_DEBUG_BUGVERBOSE
37 
38 #define _BUG_FLAGS(ins, flags, extra)					\
39 do {									\
40 	asm_inline volatile("1:\t" ins "\n"				\
41 		     ".pushsection __bug_table,\"aw\"\n"		\
42 		     "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n"	\
43 		     "\t"  __BUG_REL(%c0) "\t# bug_entry::file\n"	\
44 		     "\t.word %c1"        "\t# bug_entry::line\n"	\
45 		     "\t.word %c2"        "\t# bug_entry::flags\n"	\
46 		     "\t.org 2b+%c3\n"					\
47 		     ".popsection\n"					\
48 		     extra						\
49 		     : : "i" (__FILE__), "i" (__LINE__),		\
50 			 "i" (flags),					\
51 			 "i" (sizeof(struct bug_entry)));		\
52 } while (0)
53 
54 #else /* !CONFIG_DEBUG_BUGVERBOSE */
55 
56 #define _BUG_FLAGS(ins, flags, extra)					\
57 do {									\
58 	asm_inline volatile("1:\t" ins "\n"				\
59 		     ".pushsection __bug_table,\"aw\"\n"		\
60 		     "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n"	\
61 		     "\t.word %c0"        "\t# bug_entry::flags\n"	\
62 		     "\t.org 2b+%c1\n"					\
63 		     ".popsection\n"					\
64 		     extra						\
65 		     : : "i" (flags),					\
66 			 "i" (sizeof(struct bug_entry)));		\
67 } while (0)
68 
69 #endif /* CONFIG_DEBUG_BUGVERBOSE */
70 
71 #else
72 
73 #define _BUG_FLAGS(ins, flags, extra)  asm volatile(ins)
74 
75 #endif /* CONFIG_GENERIC_BUG */
76 
77 #define HAVE_ARCH_BUG
78 #define BUG()							\
79 do {								\
80 	instrumentation_begin();				\
81 	_BUG_FLAGS(ASM_UD2, 0, "");				\
82 	__builtin_unreachable();				\
83 } while (0)
84 
85 /*
86  * This instrumentation_begin() is strictly speaking incorrect; but it
87  * suppresses the complaints from WARN()s in noinstr code. If such a WARN()
88  * were to trigger, we'd rather wreck the machine in an attempt to get the
89  * message out than not know about it.
90  */
91 #define __WARN_FLAGS(flags)					\
92 do {								\
93 	__auto_type __flags = BUGFLAG_WARNING|(flags);		\
94 	instrumentation_begin();				\
95 	_BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE);		\
96 	instrumentation_end();					\
97 } while (0)
98 
99 #include <asm-generic/bug.h>
100 
101 #endif /* _ASM_X86_BUG_H */
102