1 From foo@baz Tue Apr 24 15:29:20 CEST 2018
2 From: Vitaly Kuznetsov <vkuznets@redhat.com>
3 Date: Thu, 25 Jan 2018 16:37:07 +0100
4 Subject: x86/kvm/vmx: do not use vm-exit instruction length for fast MMIO when running nested
6 From: Vitaly Kuznetsov <vkuznets@redhat.com>
9 [ Upstream commit d391f1207067268261add0485f0f34503539c5b0 ]
11 I was investigating an issue with seabios >= 1.10 which stopped working
12 for nested KVM on Hyper-V. The problem appears to be in
13 handle_ept_violation() function: when we do fast mmio we need to skip
14 the instruction so we do kvm_skip_emulated_instruction(). This, however,
15 depends on VM_EXIT_INSTRUCTION_LEN field being set correctly in VMCS.
16 However, this is not the case.
18 Intel's manual doesn't mandate VM_EXIT_INSTRUCTION_LEN to be set when
19 EPT MISCONFIG occurs. While on real hardware it was observed to be set,
20 some hypervisors follow the spec and don't set it; we end up advancing
21 IP with some random value.
23 I checked with Microsoft and they confirmed they don't fill
24 VM_EXIT_INSTRUCTION_LEN on EPT MISCONFIG.
26 Fix the issue by doing instruction skip through emulator when running
29 Fixes: 68c3b4d1676d870f0453c31d5a52e7e65c7448ae
30 Suggested-by: Radim Krčmář <rkrcmar@redhat.com>
31 Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
32 Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
33 Acked-by: Michael S. Tsirkin <mst@redhat.com>
34 Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
35 Signed-off-by: Sasha Levin <alexander.levin@microsoft.com>
36 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
38 arch/x86/kvm/vmx.c | 16 +++++++++++++++-
39 arch/x86/kvm/x86.c | 3 ++-
40 2 files changed, 17 insertions(+), 2 deletions(-)
42 --- a/arch/x86/kvm/vmx.c
43 +++ b/arch/x86/kvm/vmx.c
44 @@ -6765,7 +6765,21 @@ static int handle_ept_misconfig(struct k
45 if (!is_guest_mode(vcpu) &&
46 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
47 trace_kvm_fast_mmio(gpa);
48 - return kvm_skip_emulated_instruction(vcpu);
50 + * Doing kvm_skip_emulated_instruction() depends on undefined
51 + * behavior: Intel's manual doesn't mandate
52 + * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
53 + * occurs and while on real hardware it was observed to be set,
54 + * other hypervisors (namely Hyper-V) don't set it, we end up
55 + * advancing IP with some random value. Disable fast mmio when
56 + * running nested and keep it for real hardware in hope that
57 + * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
59 + if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
60 + return kvm_skip_emulated_instruction(vcpu);
62 + return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
63 + NULL, 0) == EMULATE_DONE;
66 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
67 --- a/arch/x86/kvm/x86.c
68 +++ b/arch/x86/kvm/x86.c
69 @@ -5699,7 +5699,8 @@ int x86_emulate_instruction(struct kvm_v
70 * handle watchpoints yet, those would be handled in
73 - if (kvm_vcpu_check_breakpoint(vcpu, &r))
74 + if (!(emulation_type & EMULTYPE_SKIP) &&
75 + kvm_vcpu_check_breakpoint(vcpu, &r))
78 ctxt->interruptibility = 0;