[RHEL7,COMMIT] ms/KVM: x86: Add emulation_type to not raise #UD on emulation failure

Submitted by Konstantin Khorenko on May 8, 2018, 9:26 a.m.

Details

Message ID 201805080926.w489QNRW001402@finist_ce7.work
State New
Series "ms/KVM: x86: Add emulation_type to not raise #UD on emulation failure"
Headers show

Commit Message

Konstantin Khorenko May 8, 2018, 9:26 a.m.
The commit is pushed to "branch-rh7-3.10.0-693.21.1.vz7.47.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-693.21.1.vz7.47.5
------>
commit 17ea629f64976e85826f4bc5f080216ab4b7e953
Author: Liran Alon <liran.alon@oracle.com>
Date:   Tue May 8 12:26:23 2018 +0300

    ms/KVM: x86: Add emulation_type to not raise #UD on emulation failure
    
    Next commits are going introduce support for accessing VMware backdoor
    ports even though guest's TSS I/O permissions bitmap doesn't allow
    access. This mimic VMware hypervisor behavior.
    
    In order to support this, next commits will change VMX/SVM to
    intercept #GP which was raised by such access and handle it by calling
    the x86 emulator to emulate instruction. Since commit "KVM: x86:
    Always allow access to VMware backdoor I/O ports", the x86 emulator
    handles access to these I/O ports by not checking these ports against
    the TSS I/O permission bitmap.
    
    However, there could be cases that CPU rasies a #GP on instruction
    that fails to be disassembled by the x86 emulator (Because of
    incomplete implementation for example).
    
    In those cases, we would like the #GP intercept to just forward #GP
    as-is to guest as if there was no intercept to begin with.
    However, current emulator code always queues #UD exception in case
    emulator fails (including disassembly failures) which is not what is
    wanted in this flow.
    
    This commit addresses this issue by adding a new emulation_type flag
    that will allow the #GP intercept handler to specify that it wishes
    to be aware when instruction emulation fails and doesn't want #UD
    exception to be queued.
    
    Signed-off-by: Liran Alon <liran.alon@oracle.com>
    Reviewed-by: Nikita Leshenko <nikita.leshchenko@oracle.com>
    Reviewed-by: Radim Krčmář <rkrcmar@redhat.com>
    Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
    
    (cherry picked from commit e23661712005fd01ad9d2bca6eb4a122b79c8b0b)
    Signed-off-by: Jan Dakinevich <jan.dakinevich@virtuozzo.com>
---
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/x86.c              | 11 ++++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 838996db6973..4aadb8916e9a 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1074,6 +1074,7 @@  enum emulation_result {
 #define EMULTYPE_SKIP		    (1 << 2)
 #define EMULTYPE_RETRY		    (1 << 3)
 #define EMULTYPE_NO_REEXECUTE	    (1 << 4)
+#define EMULTYPE_NO_UD_ON_FAIL	    (1 << 5)
 int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,
 			    int emulation_type, void *insn, int insn_len);
 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index d59f8a872a58..bcaf39f0e918 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5246,18 +5246,23 @@  int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
 }
 EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
 
-static int handle_emulation_failure(struct kvm_vcpu *vcpu)
+static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
 {
 	int r = EMULATE_DONE;
 
 	++vcpu->stat.insn_emulation_fail;
 	trace_kvm_emulate_insn_failed(vcpu);
+
+	if (emulation_type & EMULTYPE_NO_UD_ON_FAIL)
+		return EMULATE_FAIL;
+
 	if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
 		vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
 		vcpu->run->internal.ndata = 0;
 		r = EMULATE_FAIL;
 	}
+
 	kvm_queue_exception(vcpu, UD_VECTOR);
 
 	return r;
@@ -5531,7 +5536,7 @@  int x86_emulate_instruction(struct kvm_vcpu *vcpu,
 				return EMULATE_DONE;
 			if (emulation_type & EMULTYPE_SKIP)
 				return EMULATE_FAIL;
-			return handle_emulation_failure(vcpu);
+			return handle_emulation_failure(vcpu, emulation_type);
 		}
 	}
 
@@ -5563,7 +5568,7 @@  int x86_emulate_instruction(struct kvm_vcpu *vcpu,
 					emulation_type))
 			return EMULATE_DONE;
 
-		return handle_emulation_failure(vcpu);
+		return handle_emulation_failure(vcpu, emulation_type);
 	}
 
 	if (ctxt->have_exception) {