[RESEND,08/13] KVM: x86: Move #UD injection for failed emulation into emulation code
diff mbox series

Message ID 20190823010709.24879-9-sean.j.christopherson@intel.com
State New
Headers show
Series
  • KVM: x86: Remove emulation_result enums
Related show

Commit Message

Sean Christopherson Aug. 23, 2019, 1:07 a.m. UTC
Immediately inject a #UD and return EMULATE done if emulation fails when
handling an intercepted #UD.  This helps pave the way for removing
EMULATE_FAIL altogether.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 arch/x86/kvm/x86.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

Comments

Liran Alon Aug. 23, 2019, 1:48 p.m. UTC | #1
> On 23 Aug 2019, at 4:07, Sean Christopherson <sean.j.christopherson@intel.com> wrote:
> 
> Immediately inject a #UD and return EMULATE done if emulation fails when
> handling an intercepted #UD.  This helps pave the way for removing
> EMULATE_FAIL altogether.
> 
> Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>

I suggest squashing this commit which previous one.

-Liran

> ---
> arch/x86/kvm/x86.c | 14 +++++---------
> 1 file changed, 5 insertions(+), 9 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index a1f9e36b2d58..bff3320aa78e 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -5328,7 +5328,6 @@ EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
> int handle_ud(struct kvm_vcpu *vcpu)
> {
> 	int emul_type = EMULTYPE_TRAP_UD;
> -	enum emulation_result er;
> 	char sig[5]; /* ud2; .ascii "kvm" */
> 	struct x86_exception e;
> 
> @@ -5340,12 +5339,7 @@ int handle_ud(struct kvm_vcpu *vcpu)
> 		emul_type = EMULTYPE_TRAP_UD_FORCED;
> 	}
> 
> -	er = kvm_emulate_instruction(vcpu, emul_type);
> -	if (er == EMULATE_USER_EXIT)
> -		return 0;
> -	if (er != EMULATE_DONE)
> -		kvm_queue_exception(vcpu, UD_VECTOR);
> -	return 1;
> +	return kvm_emulate_instruction(vcpu, emul_type) != EMULATE_USER_EXIT;
> }
> EXPORT_SYMBOL_GPL(handle_ud);
> 
> @@ -6533,8 +6527,10 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
> 		++vcpu->stat.insn_emulation;
> 		if (r != EMULATION_OK)  {
> 			if ((emulation_type & EMULTYPE_TRAP_UD) ||
> -			    (emulation_type & EMULTYPE_TRAP_UD_FORCED))
> -				return EMULATE_FAIL;
> +			    (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
> +				kvm_queue_exception(vcpu, UD_VECTOR);
> +				return EMULATE_DONE;
> +			}
> 			if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
> 						emulation_type))
> 				return EMULATE_DONE;
> -- 
> 2.22.0
>
Sean Christopherson Aug. 27, 2019, 8:22 p.m. UTC | #2
On Fri, Aug 23, 2019 at 04:48:16PM +0300, Liran Alon wrote:
> 
> 
> > On 23 Aug 2019, at 4:07, Sean Christopherson <sean.j.christopherson@intel.com> wrote:
> > 
> > Immediately inject a #UD and return EMULATE done if emulation fails when
> > handling an intercepted #UD.  This helps pave the way for removing
> > EMULATE_FAIL altogether.
> > 
> > Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
> 
> I suggest squashing this commit which previous one.

Missed this comment first time around...

I'd like to keep the two patches separate in this case.  Adding the
EMULTYPE_TRAP_UD_FORCED flag is a functional change, whereas this patch
is purely a refactor.

Patch
diff mbox series

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a1f9e36b2d58..bff3320aa78e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5328,7 +5328,6 @@  EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
 int handle_ud(struct kvm_vcpu *vcpu)
 {
 	int emul_type = EMULTYPE_TRAP_UD;
-	enum emulation_result er;
 	char sig[5]; /* ud2; .ascii "kvm" */
 	struct x86_exception e;
 
@@ -5340,12 +5339,7 @@  int handle_ud(struct kvm_vcpu *vcpu)
 		emul_type = EMULTYPE_TRAP_UD_FORCED;
 	}
 
-	er = kvm_emulate_instruction(vcpu, emul_type);
-	if (er == EMULATE_USER_EXIT)
-		return 0;
-	if (er != EMULATE_DONE)
-		kvm_queue_exception(vcpu, UD_VECTOR);
-	return 1;
+	return kvm_emulate_instruction(vcpu, emul_type) != EMULATE_USER_EXIT;
 }
 EXPORT_SYMBOL_GPL(handle_ud);
 
@@ -6533,8 +6527,10 @@  int x86_emulate_instruction(struct kvm_vcpu *vcpu,
 		++vcpu->stat.insn_emulation;
 		if (r != EMULATION_OK)  {
 			if ((emulation_type & EMULTYPE_TRAP_UD) ||
-			    (emulation_type & EMULTYPE_TRAP_UD_FORCED))
-				return EMULATE_FAIL;
+			    (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
+				kvm_queue_exception(vcpu, UD_VECTOR);
+				return EMULATE_DONE;
+			}
 			if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
 						emulation_type))
 				return EMULATE_DONE;