Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86: xen: insn: Decode Xen and KVM emulate-prefix signature

Decode Xen and KVM's emulate-prefix signature by x86 insn decoder.
It is called "prefix" but actually not x86 instruction prefix, so
this adds insn.emulate_prefix_size field instead of reusing
insn.prefixes.

If x86 decoder finds a special sequence of instructions of
XEN_EMULATE_PREFIX and 'ud2a; .ascii "kvm"', it just counts the
length, set insn.emulate_prefix_size and fold it with the next
instruction. In other words, the signature and the next instruction
is treated as a single instruction.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: x86@kernel.org
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: xen-devel@lists.xenproject.org
Cc: Randy Dunlap <rdunlap@infradead.org>
Link: https://lkml.kernel.org/r/156777564986.25081.4964537658500952557.stgit@devnote2

authored by

Masami Hiramatsu and committed by
Peter Zijlstra
4d65adfc b3dc0695

+98 -2
+6
arch/x86/include/asm/insn.h
··· 45 45 struct insn_field immediate2; /* for 64bit imm or seg16 */ 46 46 }; 47 47 48 + int emulate_prefix_size; 48 49 insn_attr_t attr; 49 50 unsigned char opnd_bytes; 50 51 unsigned char addr_bytes; ··· 127 126 if (!insn->prefixes.got) 128 127 insn_get_prefixes(insn); 129 128 return (insn->vex_prefix.nbytes == 4); 129 + } 130 + 131 + static inline int insn_has_emulate_prefix(struct insn *insn) 132 + { 133 + return !!insn->emulate_prefix_size; 130 134 } 131 135 132 136 /* Ensure this instruction is decoded completely */
+34
arch/x86/lib/insn.c
··· 13 13 #include <asm/inat.h> 14 14 #include <asm/insn.h> 15 15 16 + #include <asm/emulate_prefix.h> 17 + 16 18 /* Verify next sizeof(t) bytes can be on the same instruction */ 17 19 #define validate_next(t, insn, n) \ 18 20 ((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr) ··· 60 58 insn->addr_bytes = 4; 61 59 } 62 60 61 + static const insn_byte_t xen_prefix[] = { __XEN_EMULATE_PREFIX }; 62 + static const insn_byte_t kvm_prefix[] = { __KVM_EMULATE_PREFIX }; 63 + 64 + static int __insn_get_emulate_prefix(struct insn *insn, 65 + const insn_byte_t *prefix, size_t len) 66 + { 67 + size_t i; 68 + 69 + for (i = 0; i < len; i++) { 70 + if (peek_nbyte_next(insn_byte_t, insn, i) != prefix[i]) 71 + goto err_out; 72 + } 73 + 74 + insn->emulate_prefix_size = len; 75 + insn->next_byte += len; 76 + 77 + return 1; 78 + 79 + err_out: 80 + return 0; 81 + } 82 + 83 + static void insn_get_emulate_prefix(struct insn *insn) 84 + { 85 + if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix))) 86 + return; 87 + 88 + __insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix)); 89 + } 90 + 63 91 /** 64 92 * insn_get_prefixes - scan x86 instruction prefix bytes 65 93 * @insn: &struct insn containing instruction ··· 107 75 108 76 if (prefixes->got) 109 77 return; 78 + 79 + insn_get_emulate_prefix(insn); 110 80 111 81 nb = 0; 112 82 lb = 0;
+14
tools/arch/x86/include/asm/emulate_prefix.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _ASM_X86_EMULATE_PREFIX_H 3 + #define _ASM_X86_EMULATE_PREFIX_H 4 + 5 + /* 6 + * Virt escape sequences to trigger instruction emulation; 7 + * ideally these would decode to 'whole' instruction and not destroy 8 + * the instruction stream; sadly this is not true for the 'kvm' one :/ 9 + */ 10 + 11 + #define __XEN_EMULATE_PREFIX 0x0f,0x0b,0x78,0x65,0x6e /* ud2 ; .ascii "xen" */ 12 + #define __KVM_EMULATE_PREFIX 0x0f,0x0b,0x6b,0x76,0x6d /* ud2 ; .ascii "kvm" */ 13 + 14 + #endif
+6
tools/arch/x86/include/asm/insn.h
··· 45 45 struct insn_field immediate2; /* for 64bit imm or seg16 */ 46 46 }; 47 47 48 + int emulate_prefix_size; 48 49 insn_attr_t attr; 49 50 unsigned char opnd_bytes; 50 51 unsigned char addr_bytes; ··· 127 126 if (!insn->prefixes.got) 128 127 insn_get_prefixes(insn); 129 128 return (insn->vex_prefix.nbytes == 4); 129 + } 130 + 131 + static inline int insn_has_emulate_prefix(struct insn *insn) 132 + { 133 + return !!insn->emulate_prefix_size; 130 134 } 131 135 132 136 /* Ensure this instruction is decoded completely */
+34
tools/arch/x86/lib/insn.c
··· 13 13 #include "../include/asm/inat.h" 14 14 #include "../include/asm/insn.h" 15 15 16 + #include "../include/asm/emulate_prefix.h" 17 + 16 18 /* Verify next sizeof(t) bytes can be on the same instruction */ 17 19 #define validate_next(t, insn, n) \ 18 20 ((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr) ··· 60 58 insn->addr_bytes = 4; 61 59 } 62 60 61 + static const insn_byte_t xen_prefix[] = { __XEN_EMULATE_PREFIX }; 62 + static const insn_byte_t kvm_prefix[] = { __KVM_EMULATE_PREFIX }; 63 + 64 + static int __insn_get_emulate_prefix(struct insn *insn, 65 + const insn_byte_t *prefix, size_t len) 66 + { 67 + size_t i; 68 + 69 + for (i = 0; i < len; i++) { 70 + if (peek_nbyte_next(insn_byte_t, insn, i) != prefix[i]) 71 + goto err_out; 72 + } 73 + 74 + insn->emulate_prefix_size = len; 75 + insn->next_byte += len; 76 + 77 + return 1; 78 + 79 + err_out: 80 + return 0; 81 + } 82 + 83 + static void insn_get_emulate_prefix(struct insn *insn) 84 + { 85 + if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix))) 86 + return; 87 + 88 + __insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix)); 89 + } 90 + 63 91 /** 64 92 * insn_get_prefixes - scan x86 instruction prefix bytes 65 93 * @insn: &struct insn containing instruction ··· 107 75 108 76 if (prefixes->got) 109 77 return; 78 + 79 + insn_get_emulate_prefix(insn); 110 80 111 81 nb = 0; 112 82 lb = 0;
+2 -1
tools/objtool/sync-check.sh
··· 4 4 FILES=' 5 5 arch/x86/include/asm/inat_types.h 6 6 arch/x86/include/asm/orc_types.h 7 + arch/x86/include/asm/emulate_prefix.h 7 8 arch/x86/lib/x86-opcode-map.txt 8 9 arch/x86/tools/gen-insn-attr-x86.awk 9 10 ' ··· 47 46 check arch/x86/include/asm/inat.h '-I "^#include [\"<]\(asm/\)*inat_types.h[\">]"' 48 47 check arch/x86/include/asm/insn.h '-I "^#include [\"<]\(asm/\)*inat.h[\">]"' 49 48 check arch/x86/lib/inat.c '-I "^#include [\"<]\(../include/\)*asm/insn.h[\">]"' 50 - check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]"' 49 + check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]"' 51 50 52 51 cd -
+2 -1
tools/perf/check-headers.sh
··· 28 28 arch/x86/include/asm/required-features.h 29 29 arch/x86/include/asm/cpufeatures.h 30 30 arch/x86/include/asm/inat_types.h 31 + arch/x86/include/asm/emulate_prefix.h 31 32 arch/x86/include/uapi/asm/prctl.h 32 33 arch/x86/lib/x86-opcode-map.txt 33 34 arch/x86/tools/gen-insn-attr-x86.awk ··· 117 116 check arch/x86/include/asm/inat.h '-I "^#include [\"<]\(asm/\)*inat_types.h[\">]"' 118 117 check arch/x86/include/asm/insn.h '-I "^#include [\"<]\(asm/\)*inat.h[\">]"' 119 118 check arch/x86/lib/inat.c '-I "^#include [\"<]\(../include/\)*asm/insn.h[\">]"' 120 - check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]"' 119 + check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]"' 121 120 122 121 # diff non-symmetric files 123 122 check_2 tools/perf/arch/x86/entry/syscalls/syscall_64.tbl arch/x86/entry/syscalls/syscall_64.tbl