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 #include <asm/asm.h> 9 10 #ifndef __ASSEMBLY__ 11 struct bug_entry; 12 extern void __WARN_trap(struct bug_entry *bug, ...); 13 #endif 14 15 /* 16 * Despite that some emulators terminate on UD2, we use it for WARN(). 17 */ 18 #define ASM_UD2 _ASM_BYTES(0x0f, 0x0b) 19 #define INSN_UD2 0x0b0f 20 #define LEN_UD2 2 21 22 #define ASM_UDB _ASM_BYTES(0xd6) 23 #define INSN_UDB 0xd6 24 #define LEN_UDB 1 25 26 /* 27 * In clang we have UD1s reporting UBSAN failures on X86, 64 and 32bit. 28 */ 29 #define INSN_ASOP 0x67 30 #define INSN_LOCK 0xf0 31 #define OPCODE_ESCAPE 0x0f 32 #define SECOND_BYTE_OPCODE_UD1 0xb9 33 #define SECOND_BYTE_OPCODE_UD2 0x0b 34 35 #define BUG_NONE 0xffff 36 #define BUG_UD2 0xfffe 37 #define BUG_UD1 0xfffd 38 #define BUG_UD1_UBSAN 0xfffc 39 #define BUG_UD1_WARN 0xfffb 40 #define BUG_UDB 0xffd6 41 #define BUG_LOCK 0xfff0 42 43 #ifdef CONFIG_GENERIC_BUG 44 45 #ifdef CONFIG_DEBUG_BUGVERBOSE 46 #define __BUG_ENTRY_VERBOSE(file, line) \ 47 "\t.long " file " - .\t# bug_entry::file\n" \ 48 "\t.word " line "\t# bug_entry::line\n" 49 #else 50 #define __BUG_ENTRY_VERBOSE(file, line) 51 #endif 52 53 #if defined(CONFIG_X86_64) || defined(CONFIG_DEBUG_BUGVERBOSE_DETAILED) 54 #define HAVE_ARCH_BUG_FORMAT 55 #define __BUG_ENTRY_FORMAT(format) \ 56 "\t.long " format " - .\t# bug_entry::format\n" 57 #else 58 #define __BUG_ENTRY_FORMAT(format) 59 #endif 60 61 #ifdef CONFIG_X86_64 62 #define HAVE_ARCH_BUG_FORMAT_ARGS 63 #endif 64 65 #define __BUG_ENTRY(format, file, line, flags) \ 66 "\t.long 1b - ." "\t# bug_entry::bug_addr\n" \ 67 __BUG_ENTRY_FORMAT(format) \ 68 __BUG_ENTRY_VERBOSE(file, line) \ 69 "\t.word " flags "\t# bug_entry::flags\n" 70 71 #define _BUG_FLAGS_ASM(format, file, line, flags, size, extra) \ 72 ".pushsection __bug_table,\"aw\"\n\t" \ 73 ANNOTATE_DATA_SPECIAL \ 74 "2:\n\t" \ 75 __BUG_ENTRY(format, file, line, flags) \ 76 "\t.org 2b + " size "\n" \ 77 ".popsection\n" \ 78 extra 79 80 #ifdef CONFIG_DEBUG_BUGVERBOSE_DETAILED 81 #define WARN_CONDITION_STR(cond_str) cond_str 82 #else 83 #define WARN_CONDITION_STR(cond_str) "" 84 #endif 85 86 #define _BUG_FLAGS(cond_str, ins, flags, extra) \ 87 do { \ 88 asm_inline volatile("1:\t" ins "\n" \ 89 _BUG_FLAGS_ASM("%c[fmt]", "%c[file]", \ 90 "%c[line]", "%c[fl]", \ 91 "%c[size]", extra) \ 92 : : [fmt] "i" (WARN_CONDITION_STR(cond_str)), \ 93 [file] "i" (__FILE__), \ 94 [line] "i" (__LINE__), \ 95 [fl] "i" (flags), \ 96 [size] "i" (sizeof(struct bug_entry))); \ 97 } while (0) 98 99 #define ARCH_WARN_ASM(file, line, flags, size) \ 100 ".pushsection .rodata.str1.1, \"aMS\", @progbits, 1\n" \ 101 "99:\n" \ 102 "\t.string \"\"\n" \ 103 ".popsection\n" \ 104 "1:\t " ASM_UD2 "\n" \ 105 _BUG_FLAGS_ASM("99b", file, line, flags, size, "") 106 107 #else 108 109 #define _BUG_FLAGS(cond_str, ins, flags, extra) asm volatile(ins) 110 111 #endif /* CONFIG_GENERIC_BUG */ 112 113 #define HAVE_ARCH_BUG 114 #define BUG() \ 115 do { \ 116 instrumentation_begin(); \ 117 _BUG_FLAGS("", ASM_UD2, 0, ""); \ 118 __builtin_unreachable(); \ 119 } while (0) 120 121 /* 122 * This instrumentation_begin() is strictly speaking incorrect; but it 123 * suppresses the complaints from WARN()s in noinstr code. If such a WARN() 124 * were to trigger, we'd rather wreck the machine in an attempt to get the 125 * message out than not know about it. 126 */ 127 128 #define ARCH_WARN_REACHABLE ANNOTATE_REACHABLE(1b) 129 130 #define __WARN_FLAGS(cond_str, flags) \ 131 do { \ 132 __auto_type __flags = BUGFLAG_WARNING|(flags); \ 133 instrumentation_begin(); \ 134 _BUG_FLAGS(cond_str, ASM_UD2, __flags, ARCH_WARN_REACHABLE); \ 135 instrumentation_end(); \ 136 } while (0) 137 138 #ifdef HAVE_ARCH_BUG_FORMAT_ARGS 139 140 #ifndef __ASSEMBLY__ 141 #include <linux/static_call_types.h> 142 DECLARE_STATIC_CALL(WARN_trap, __WARN_trap); 143 144 struct pt_regs; 145 struct sysv_va_list { /* from AMD64 System V ABI */ 146 unsigned int gp_offset; 147 unsigned int fp_offset; 148 void *overflow_arg_area; 149 void *reg_save_area; 150 }; 151 struct arch_va_list { 152 unsigned long regs[6]; 153 struct sysv_va_list args; 154 }; 155 extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs); 156 #endif /* __ASSEMBLY__ */ 157 158 #define __WARN_bug_entry(flags, format) ({ \ 159 struct bug_entry *bug; \ 160 asm_inline volatile("lea (2f)(%%rip), %[addr]\n1:\n" \ 161 _BUG_FLAGS_ASM("%c[fmt]", "%c[file]", \ 162 "%c[line]", "%c[fl]", \ 163 "%c[size]", "") \ 164 : [addr] "=r" (bug) \ 165 : [fmt] "i" (format), \ 166 [file] "i" (__FILE__), \ 167 [line] "i" (__LINE__), \ 168 [fl] "i" (flags), \ 169 [size] "i" (sizeof(struct bug_entry))); \ 170 bug; }) 171 172 #define __WARN_print_arg(flags, format, arg...) \ 173 do { \ 174 int __flags = (flags) | BUGFLAG_WARNING | BUGFLAG_ARGS ; \ 175 static_call_mod(WARN_trap)(__WARN_bug_entry(__flags, format), ## arg); \ 176 asm (""); /* inhibit tail-call optimization */ \ 177 } while (0) 178 179 #define __WARN_printf(taint, fmt, arg...) \ 180 __WARN_print_arg(BUGFLAG_TAINT(taint), fmt, ## arg) 181 182 #define WARN_ONCE(cond, format, arg...) ({ \ 183 int __ret_warn_on = !!(cond); \ 184 if (unlikely(__ret_warn_on)) { \ 185 __WARN_print_arg(BUGFLAG_ONCE|BUGFLAG_TAINT(TAINT_WARN),\ 186 format, ## arg); \ 187 } \ 188 __ret_warn_on; \ 189 }) 190 191 #endif /* HAVE_ARCH_BUG_FORMAT_ARGS */ 192 193 #include <asm-generic/bug.h> 194 195 #endif /* _ASM_X86_BUG_H */ 196