blob: 297fa12e7e2799438e843c5cc6ece0699c1ac7db [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_BUG_H
3#define _ASM_X86_BUG_H
4
5#include <linux/stringify.h>
Olivier Deprez157378f2022-04-04 15:47:50 +02006#include <linux/instrumentation.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007
8/*
9 * Despite that some emulators terminate on UD2, we use it for WARN().
10 *
11 * Since various instruction decoders/specs disagree on the encoding of
12 * UD0/UD1.
13 */
14
15#define ASM_UD0 ".byte 0x0f, 0xff" /* + ModRM (for Intel) */
16#define ASM_UD1 ".byte 0x0f, 0xb9" /* + ModRM */
17#define ASM_UD2 ".byte 0x0f, 0x0b"
18
19#define INSN_UD0 0xff0f
20#define INSN_UD2 0x0b0f
21
22#define LEN_UD2 2
23
24#ifdef CONFIG_GENERIC_BUG
25
26#ifdef CONFIG_X86_32
27# define __BUG_REL(val) ".long " __stringify(val)
28#else
29# define __BUG_REL(val) ".long " __stringify(val) " - 2b"
30#endif
31
32#ifdef CONFIG_DEBUG_BUGVERBOSE
33
34#define _BUG_FLAGS(ins, flags) \
35do { \
David Brazdil0f672f62019-12-10 10:32:29 +000036 asm_inline volatile("1:\t" ins "\n" \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 ".pushsection __bug_table,\"aw\"\n" \
38 "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
39 "\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \
40 "\t.word %c1" "\t# bug_entry::line\n" \
41 "\t.word %c2" "\t# bug_entry::flags\n" \
42 "\t.org 2b+%c3\n" \
43 ".popsection" \
44 : : "i" (__FILE__), "i" (__LINE__), \
45 "i" (flags), \
46 "i" (sizeof(struct bug_entry))); \
47} while (0)
48
49#else /* !CONFIG_DEBUG_BUGVERBOSE */
50
51#define _BUG_FLAGS(ins, flags) \
52do { \
David Brazdil0f672f62019-12-10 10:32:29 +000053 asm_inline volatile("1:\t" ins "\n" \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 ".pushsection __bug_table,\"aw\"\n" \
55 "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
56 "\t.word %c0" "\t# bug_entry::flags\n" \
57 "\t.org 2b+%c1\n" \
58 ".popsection" \
59 : : "i" (flags), \
60 "i" (sizeof(struct bug_entry))); \
61} while (0)
62
63#endif /* CONFIG_DEBUG_BUGVERBOSE */
64
65#else
66
67#define _BUG_FLAGS(ins, flags) asm volatile(ins)
68
69#endif /* CONFIG_GENERIC_BUG */
70
71#define HAVE_ARCH_BUG
72#define BUG() \
73do { \
Olivier Deprez157378f2022-04-04 15:47:50 +020074 instrumentation_begin(); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 _BUG_FLAGS(ASM_UD2, 0); \
76 unreachable(); \
77} while (0)
78
Olivier Deprez157378f2022-04-04 15:47:50 +020079/*
80 * This instrumentation_begin() is strictly speaking incorrect; but it
81 * suppresses the complaints from WARN()s in noinstr code. If such a WARN()
82 * were to trigger, we'd rather wreck the machine in an attempt to get the
83 * message out than not know about it.
84 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085#define __WARN_FLAGS(flags) \
86do { \
Olivier Deprez157378f2022-04-04 15:47:50 +020087 instrumentation_begin(); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088 _BUG_FLAGS(ASM_UD2, BUGFLAG_WARNING|(flags)); \
89 annotate_reachable(); \
Olivier Deprez157378f2022-04-04 15:47:50 +020090 instrumentation_end(); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091} while (0)
92
93#include <asm-generic/bug.h>
94
95#endif /* _ASM_X86_BUG_H */