Skip to content

Commit ceb9e77

Browse files
author
Ingo Molnar
committed
Merge branch 'x86/core' into perf/core, to resolve conflicts and to pick up completed topic tree
Conflicts: tools/perf/check-headers.sh Signed-off-by: Ingo Molnar <[email protected]>
2 parents c494cd6 + 004e8dc commit ceb9e77

File tree

12 files changed

+128
-13
lines changed

12 files changed

+128
-13
lines changed

arch/x86/include/asm/asm.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
# define __ASM_FORM_RAW(x) x
88
# define __ASM_FORM_COMMA(x) x,
99
#else
10-
# define __ASM_FORM(x) " " #x " "
11-
# define __ASM_FORM_RAW(x) #x
12-
# define __ASM_FORM_COMMA(x) " " #x ","
10+
#include <linux/stringify.h>
11+
12+
# define __ASM_FORM(x) " " __stringify(x) " "
13+
# define __ASM_FORM_RAW(x) __stringify(x)
14+
# define __ASM_FORM_COMMA(x) " " __stringify(x) ","
1315
#endif
1416

1517
#ifndef __x86_64__

arch/x86/include/asm/emulate_prefix.h

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

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/include/asm/xen/interface.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,9 @@ struct xen_pmu_arch {
379379
* Prefix forces emulation of some non-trapping instructions.
380380
* Currently only CPUID.
381381
*/
382-
#ifdef __ASSEMBLY__
383-
#define XEN_EMULATE_PREFIX .byte 0x0f,0x0b,0x78,0x65,0x6e ;
384-
#define XEN_CPUID XEN_EMULATE_PREFIX cpuid
385-
#else
386-
#define XEN_EMULATE_PREFIX ".byte 0x0f,0x0b,0x78,0x65,0x6e ; "
387-
#define XEN_CPUID XEN_EMULATE_PREFIX "cpuid"
388-
#endif
382+
#include <asm/emulate_prefix.h>
383+
384+
#define XEN_EMULATE_PREFIX __ASM_FORM(.byte __XEN_EMULATE_PREFIX ;)
385+
#define XEN_CPUID XEN_EMULATE_PREFIX __ASM_FORM(cpuid)
389386

390387
#endif /* _ASM_X86_XEN_INTERFACE_H */

arch/x86/kernel/kprobes/core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
351351
kernel_insn_init(insn, dest, MAX_INSN_SIZE);
352352
insn_get_length(insn);
353353

354+
/* We can not probe force emulate prefixed instruction */
355+
if (insn_has_emulate_prefix(insn))
356+
return 0;
357+
354358
/* Another subsystem puts a breakpoint, failed to recover */
355359
if (insn->opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
356360
return 0;

arch/x86/kvm/x86.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include <asm/mshyperv.h>
6969
#include <asm/hypervisor.h>
7070
#include <asm/intel_pt.h>
71+
#include <asm/emulate_prefix.h>
7172
#include <clocksource/hyperv_timer.h>
7273

7374
#define CREATE_TRACE_POINTS
@@ -5471,14 +5472,15 @@ EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
54715472

54725473
int handle_ud(struct kvm_vcpu *vcpu)
54735474
{
5475+
static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
54745476
int emul_type = EMULTYPE_TRAP_UD;
54755477
char sig[5]; /* ud2; .ascii "kvm" */
54765478
struct x86_exception e;
54775479

54785480
if (force_emulation_prefix &&
54795481
kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
54805482
sig, sizeof(sig), &e) == 0 &&
5481-
memcmp(sig, "\xf\xbkvm", sizeof(sig)) == 0) {
5483+
memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
54825484
kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
54835485
emul_type = EMULTYPE_TRAP_UD_FORCED;
54845486
}

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);

0 commit comments

Comments
 (0)