Skip to content

Commit b3dc069

Browse files
mhiramatPeter Zijlstra
authored andcommitted
x86: xen: kvm: Gather the definition of emulate prefixes
Gather the emulate prefixes, which forcibly make the following instruction emulated on virtualization, in one place. Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Masami Hiramatsu <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Juergen Gross <[email protected]> Cc: [email protected] Cc: Ingo Molnar <[email protected]> Cc: Boris Ostrovsky <[email protected]> Cc: Andrew Cooper <[email protected]> Cc: Stefano Stabellini <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: [email protected] Cc: Randy Dunlap <[email protected]> Cc: Josh Poimboeuf <[email protected]> Link: https://lkml.kernel.org/r/156777563917.25081.7286628561790289995.stgit@devnote2
1 parent f7919fd commit b3dc069

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

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/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/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
@@ -5446,14 +5447,15 @@ EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
54465447

54475448
int handle_ud(struct kvm_vcpu *vcpu)
54485449
{
5450+
static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
54495451
int emul_type = EMULTYPE_TRAP_UD;
54505452
char sig[5]; /* ud2; .ascii "kvm" */
54515453
struct x86_exception e;
54525454

54535455
if (force_emulation_prefix &&
54545456
kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
54555457
sig, sizeof(sig), &e) == 0 &&
5456-
memcmp(sig, "\xf\xbkvm", sizeof(sig)) == 0) {
5458+
memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
54575459
kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
54585460
emul_type = EMULTYPE_TRAP_UD_FORCED;
54595461
}

0 commit comments

Comments
 (0)