Skip to content

Commit 4d65adf

Browse files
mhiramatPeter Zijlstra
authored andcommitted
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 <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Cc: Juergen Gross <[email protected]> Cc: [email protected] Cc: Boris Ostrovsky <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Stefano Stabellini <[email protected]> Cc: Andrew Cooper <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: [email protected] Cc: Randy Dunlap <[email protected]> Link: https://lkml.kernel.org/r/156777564986.25081.4964537658500952557.stgit@devnote2
1 parent b3dc069 commit 4d65adf

File tree

7 files changed

+98
-2
lines changed

7 files changed

+98
-2
lines changed

arch/x86/include/asm/insn.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct insn {
4545
struct insn_field immediate2; /* for 64bit imm or seg16 */
4646
};
4747

48+
int emulate_prefix_size;
4849
insn_attr_t attr;
4950
unsigned char opnd_bytes;
5051
unsigned char addr_bytes;
@@ -128,6 +129,11 @@ static inline int insn_is_evex(struct insn *insn)
128129
return (insn->vex_prefix.nbytes == 4);
129130
}
130131

132+
static inline int insn_has_emulate_prefix(struct insn *insn)
133+
{
134+
return !!insn->emulate_prefix_size;
135+
}
136+
131137
/* Ensure this instruction is decoded completely */
132138
static inline int insn_complete(struct insn *insn)
133139
{

arch/x86/lib/insn.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <asm/inat.h>
1414
#include <asm/insn.h>
1515

16+
#include <asm/emulate_prefix.h>
17+
1618
/* Verify next sizeof(t) bytes can be on the same instruction */
1719
#define validate_next(t, insn, n) \
1820
((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
@@ -58,6 +60,36 @@ void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
5860
insn->addr_bytes = 4;
5961
}
6062

63+
static const insn_byte_t xen_prefix[] = { __XEN_EMULATE_PREFIX };
64+
static const insn_byte_t kvm_prefix[] = { __KVM_EMULATE_PREFIX };
65+
66+
static int __insn_get_emulate_prefix(struct insn *insn,
67+
const insn_byte_t *prefix, size_t len)
68+
{
69+
size_t i;
70+
71+
for (i = 0; i < len; i++) {
72+
if (peek_nbyte_next(insn_byte_t, insn, i) != prefix[i])
73+
goto err_out;
74+
}
75+
76+
insn->emulate_prefix_size = len;
77+
insn->next_byte += len;
78+
79+
return 1;
80+
81+
err_out:
82+
return 0;
83+
}
84+
85+
static void insn_get_emulate_prefix(struct insn *insn)
86+
{
87+
if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix)))
88+
return;
89+
90+
__insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix));
91+
}
92+
6193
/**
6294
* insn_get_prefixes - scan x86 instruction prefix bytes
6395
* @insn: &struct insn containing instruction
@@ -76,6 +108,8 @@ void insn_get_prefixes(struct insn *insn)
76108
if (prefixes->got)
77109
return;
78110

111+
insn_get_emulate_prefix(insn);
112+
79113
nb = 0;
80114
lb = 0;
81115
b = peek_next(insn_byte_t, insn);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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

tools/arch/x86/include/asm/insn.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct insn {
4545
struct insn_field immediate2; /* for 64bit imm or seg16 */
4646
};
4747

48+
int emulate_prefix_size;
4849
insn_attr_t attr;
4950
unsigned char opnd_bytes;
5051
unsigned char addr_bytes;
@@ -128,6 +129,11 @@ static inline int insn_is_evex(struct insn *insn)
128129
return (insn->vex_prefix.nbytes == 4);
129130
}
130131

132+
static inline int insn_has_emulate_prefix(struct insn *insn)
133+
{
134+
return !!insn->emulate_prefix_size;
135+
}
136+
131137
/* Ensure this instruction is decoded completely */
132138
static inline int insn_complete(struct insn *insn)
133139
{

tools/arch/x86/lib/insn.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "../include/asm/inat.h"
1414
#include "../include/asm/insn.h"
1515

16+
#include "../include/asm/emulate_prefix.h"
17+
1618
/* Verify next sizeof(t) bytes can be on the same instruction */
1719
#define validate_next(t, insn, n) \
1820
((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
@@ -58,6 +60,36 @@ void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
5860
insn->addr_bytes = 4;
5961
}
6062

63+
static const insn_byte_t xen_prefix[] = { __XEN_EMULATE_PREFIX };
64+
static const insn_byte_t kvm_prefix[] = { __KVM_EMULATE_PREFIX };
65+
66+
static int __insn_get_emulate_prefix(struct insn *insn,
67+
const insn_byte_t *prefix, size_t len)
68+
{
69+
size_t i;
70+
71+
for (i = 0; i < len; i++) {
72+
if (peek_nbyte_next(insn_byte_t, insn, i) != prefix[i])
73+
goto err_out;
74+
}
75+
76+
insn->emulate_prefix_size = len;
77+
insn->next_byte += len;
78+
79+
return 1;
80+
81+
err_out:
82+
return 0;
83+
}
84+
85+
static void insn_get_emulate_prefix(struct insn *insn)
86+
{
87+
if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix)))
88+
return;
89+
90+
__insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix));
91+
}
92+
6193
/**
6294
* insn_get_prefixes - scan x86 instruction prefix bytes
6395
* @insn: &struct insn containing instruction
@@ -76,6 +108,8 @@ void insn_get_prefixes(struct insn *insn)
76108
if (prefixes->got)
77109
return;
78110

111+
insn_get_emulate_prefix(insn);
112+
79113
nb = 0;
80114
lb = 0;
81115
b = peek_next(insn_byte_t, insn);

tools/objtool/sync-check.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
FILES='
55
arch/x86/include/asm/inat_types.h
66
arch/x86/include/asm/orc_types.h
7+
arch/x86/include/asm/emulate_prefix.h
78
arch/x86/lib/x86-opcode-map.txt
89
arch/x86/tools/gen-insn-attr-x86.awk
910
'
@@ -46,6 +47,6 @@ done
4647
check arch/x86/include/asm/inat.h '-I "^#include [\"<]\(asm/\)*inat_types.h[\">]"'
4748
check arch/x86/include/asm/insn.h '-I "^#include [\"<]\(asm/\)*inat.h[\">]"'
4849
check arch/x86/lib/inat.c '-I "^#include [\"<]\(../include/\)*asm/insn.h[\">]"'
49-
check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]"'
50+
check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]"'
5051

5152
cd -

tools/perf/check-headers.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ arch/x86/include/asm/disabled-features.h
2828
arch/x86/include/asm/required-features.h
2929
arch/x86/include/asm/cpufeatures.h
3030
arch/x86/include/asm/inat_types.h
31+
arch/x86/include/asm/emulate_prefix.h
3132
arch/x86/include/uapi/asm/prctl.h
3233
arch/x86/lib/x86-opcode-map.txt
3334
arch/x86/tools/gen-insn-attr-x86.awk
@@ -116,7 +117,7 @@ check lib/ctype.c '-I "^EXPORT_SYMBOL" -I "^#include <linux/export.h>" -B
116117
check arch/x86/include/asm/inat.h '-I "^#include [\"<]\(asm/\)*inat_types.h[\">]"'
117118
check arch/x86/include/asm/insn.h '-I "^#include [\"<]\(asm/\)*inat.h[\">]"'
118119
check arch/x86/lib/inat.c '-I "^#include [\"<]\(../include/\)*asm/insn.h[\">]"'
119-
check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]"'
120+
check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]"'
120121

121122
# diff non-symmetric files
122123
check_2 tools/perf/arch/x86/entry/syscalls/syscall_64.tbl arch/x86/entry/syscalls/syscall_64.tbl

0 commit comments

Comments
 (0)