xref: /linux/arch/x86/include/asm/bug.h (revision 64b14a184e83eb62ea0615e31a409956049d40e7)
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 
8 /*
9  * Despite that some emulators terminate on UD2, we use it for WARN().
10  */
11 #define ASM_UD2		".byte 0x0f, 0x0b"
12 #define INSN_UD2	0x0b0f
13 #define LEN_UD2		2
14 
15 #ifdef CONFIG_GENERIC_BUG
16 
17 #ifdef CONFIG_X86_32
18 # define __BUG_REL(val)	".long " __stringify(val)
19 #else
20 # define __BUG_REL(val)	".long " __stringify(val) " - 2b"
21 #endif
22 
23 #ifdef CONFIG_DEBUG_BUGVERBOSE
24 
25 #define _BUG_FLAGS(ins, flags, extra)					\
26 do {									\
27 	asm_inline volatile("1:\t" ins "\n"				\
28 		     ".pushsection __bug_table,\"aw\"\n"		\
29 		     "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n"	\
30 		     "\t"  __BUG_REL(%c0) "\t# bug_entry::file\n"	\
31 		     "\t.word %c1"        "\t# bug_entry::line\n"	\
32 		     "\t.word %c2"        "\t# bug_entry::flags\n"	\
33 		     "\t.org 2b+%c3\n"					\
34 		     ".popsection\n"					\
35 		     extra						\
36 		     : : "i" (__FILE__), "i" (__LINE__),		\
37 			 "i" (flags),					\
38 			 "i" (sizeof(struct bug_entry)));		\
39 } while (0)
40 
41 #else /* !CONFIG_DEBUG_BUGVERBOSE */
42 
43 #define _BUG_FLAGS(ins, flags, extra)					\
44 do {									\
45 	asm_inline volatile("1:\t" ins "\n"				\
46 		     ".pushsection __bug_table,\"aw\"\n"		\
47 		     "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n"	\
48 		     "\t.word %c0"        "\t# bug_entry::flags\n"	\
49 		     "\t.org 2b+%c1\n"					\
50 		     ".popsection\n"					\
51 		     extra						\
52 		     : : "i" (flags),					\
53 			 "i" (sizeof(struct bug_entry)));		\
54 } while (0)
55 
56 #endif /* CONFIG_DEBUG_BUGVERBOSE */
57 
58 #else
59 
60 #define _BUG_FLAGS(ins, flags, extra)  asm volatile(ins)
61 
62 #endif /* CONFIG_GENERIC_BUG */
63 
64 #define HAVE_ARCH_BUG
65 #define BUG()							\
66 do {								\
67 	instrumentation_begin();				\
68 	_BUG_FLAGS(ASM_UD2, 0, "");				\
69 	__builtin_unreachable();				\
70 } while (0)
71 
72 /*
73  * This instrumentation_begin() is strictly speaking incorrect; but it
74  * suppresses the complaints from WARN()s in noinstr code. If such a WARN()
75  * were to trigger, we'd rather wreck the machine in an attempt to get the
76  * message out than not know about it.
77  */
78 #define __WARN_FLAGS(flags)					\
79 do {								\
80 	__auto_type f = BUGFLAG_WARNING|(flags);		\
81 	instrumentation_begin();				\
82 	_BUG_FLAGS(ASM_UD2, f, ASM_REACHABLE);			\
83 	instrumentation_end();					\
84 } while (0)
85 
86 #include <asm-generic/bug.h>
87 
88 #endif /* _ASM_X86_BUG_H */
89