18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_BUG_H 38c2ecf20Sopenharmony_ci#define _ASM_X86_BUG_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <linux/stringify.h> 68c2ecf20Sopenharmony_ci#include <linux/instrumentation.h> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/* 98c2ecf20Sopenharmony_ci * Despite that some emulators terminate on UD2, we use it for WARN(). 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Since various instruction decoders/specs disagree on the encoding of 128c2ecf20Sopenharmony_ci * UD0/UD1. 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define ASM_UD0 ".byte 0x0f, 0xff" /* + ModRM (for Intel) */ 168c2ecf20Sopenharmony_ci#define ASM_UD1 ".byte 0x0f, 0xb9" /* + ModRM */ 178c2ecf20Sopenharmony_ci#define ASM_UD2 ".byte 0x0f, 0x0b" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define INSN_UD0 0xff0f 208c2ecf20Sopenharmony_ci#define INSN_UD2 0x0b0f 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define LEN_UD2 2 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#ifdef CONFIG_GENERIC_BUG 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32 278c2ecf20Sopenharmony_ci# define __BUG_REL(val) ".long " __stringify(val) 288c2ecf20Sopenharmony_ci#else 298c2ecf20Sopenharmony_ci# define __BUG_REL(val) ".long " __stringify(val) " - 2b" 308c2ecf20Sopenharmony_ci#endif 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_BUGVERBOSE 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#define _BUG_FLAGS(ins, flags) \ 358c2ecf20Sopenharmony_cido { \ 368c2ecf20Sopenharmony_ci asm_inline volatile("1:\t" ins "\n" \ 378c2ecf20Sopenharmony_ci ".pushsection __bug_table,\"aw\"\n" \ 388c2ecf20Sopenharmony_ci "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \ 398c2ecf20Sopenharmony_ci "\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \ 408c2ecf20Sopenharmony_ci "\t.word %c1" "\t# bug_entry::line\n" \ 418c2ecf20Sopenharmony_ci "\t.word %c2" "\t# bug_entry::flags\n" \ 428c2ecf20Sopenharmony_ci "\t.org 2b+%c3\n" \ 438c2ecf20Sopenharmony_ci ".popsection" \ 448c2ecf20Sopenharmony_ci : : "i" (__FILE__), "i" (__LINE__), \ 458c2ecf20Sopenharmony_ci "i" (flags), \ 468c2ecf20Sopenharmony_ci "i" (sizeof(struct bug_entry))); \ 478c2ecf20Sopenharmony_ci} while (0) 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci#else /* !CONFIG_DEBUG_BUGVERBOSE */ 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci#define _BUG_FLAGS(ins, flags) \ 528c2ecf20Sopenharmony_cido { \ 538c2ecf20Sopenharmony_ci asm_inline volatile("1:\t" ins "\n" \ 548c2ecf20Sopenharmony_ci ".pushsection __bug_table,\"aw\"\n" \ 558c2ecf20Sopenharmony_ci "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \ 568c2ecf20Sopenharmony_ci "\t.word %c0" "\t# bug_entry::flags\n" \ 578c2ecf20Sopenharmony_ci "\t.org 2b+%c1\n" \ 588c2ecf20Sopenharmony_ci ".popsection" \ 598c2ecf20Sopenharmony_ci : : "i" (flags), \ 608c2ecf20Sopenharmony_ci "i" (sizeof(struct bug_entry))); \ 618c2ecf20Sopenharmony_ci} while (0) 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_BUGVERBOSE */ 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci#else 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci#define _BUG_FLAGS(ins, flags) asm volatile(ins) 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci#endif /* CONFIG_GENERIC_BUG */ 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci#define HAVE_ARCH_BUG 728c2ecf20Sopenharmony_ci#define BUG() \ 738c2ecf20Sopenharmony_cido { \ 748c2ecf20Sopenharmony_ci instrumentation_begin(); \ 758c2ecf20Sopenharmony_ci _BUG_FLAGS(ASM_UD2, 0); \ 768c2ecf20Sopenharmony_ci unreachable(); \ 778c2ecf20Sopenharmony_ci} while (0) 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/* 808c2ecf20Sopenharmony_ci * This instrumentation_begin() is strictly speaking incorrect; but it 818c2ecf20Sopenharmony_ci * suppresses the complaints from WARN()s in noinstr code. If such a WARN() 828c2ecf20Sopenharmony_ci * were to trigger, we'd rather wreck the machine in an attempt to get the 838c2ecf20Sopenharmony_ci * message out than not know about it. 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_ci#define __WARN_FLAGS(flags) \ 868c2ecf20Sopenharmony_cido { \ 878c2ecf20Sopenharmony_ci instrumentation_begin(); \ 888c2ecf20Sopenharmony_ci _BUG_FLAGS(ASM_UD2, BUGFLAG_WARNING|(flags)); \ 898c2ecf20Sopenharmony_ci annotate_reachable(); \ 908c2ecf20Sopenharmony_ci instrumentation_end(); \ 918c2ecf20Sopenharmony_ci} while (0) 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci#include <asm-generic/bug.h> 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci#endif /* _ASM_X86_BUG_H */ 96