KVM: nVMX: fix instruction skipping during emulated vm-entry
PATCH 1/1 KVM: nVMX: fix instruction skipping during emulated vm-entry
kvm_skip_emulated_instruction() should not be called after emulating a VM-entry failure during or after loading guest state (nested_vmx_entry_failure()). Otherwise the L1 hypervisor is resumed some number of bytes past vmcs->host_rip.
--- arch/x86/kvm/vmx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx.c +4 -4
@@ -10474,12 +10474,12 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; }if (vmcs12->vmcs_link_pointer !=-1ull ) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_VMCS_LINK_PTR ); - goto out; +return 1 ; }/*
@@ -10499,7 +10499,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; } }
@@ -10517,7 +10517,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; } }
Signature · 1 line
2.8.0.rc3.226.g39d4020
Quoted 4 lines · David Matlack
kvm_skip_emulated_instruction()should not be called after emulating a VM-entry failure during or after loading guest state (nested_vmx_entry_failure()). Otherwise the L1 hypervisor is resumed some number of bytes pastvmcs->host_rip.
Ah, I see. Sorry for that regression.
These paths are supposed to trigger TF-induced singlestep exceptions though. Quoting from the Intel SDM (Vol 3, Chapter 26)
"EFLAGS.TF = 1 causes a VM-entry instruction to generate a single-step debug exception only if failure of one of the checks in Section 26.1 and Section 26.2 causes control to pass to the following instruction. A VM-entry does not generate a single-step debug exception in any of the following cases: (1) the instruction generates a fault; (2) failure of one of the checks in Section 26.3 or in loading MSRs causes processor state to be loaded from the hoststate area of the VMCS; or
(3) the instruction passes all checks in Section 26.1, Section 26.2, and Section 26.3 and there is no failure in loading MSRs"
- Kyle
Quoted 46 lines
Fixes eb2775621701e6ee3ea2a474437d04e93ccdcb2fSigned-off-by David Matlack <[email protected]>--- arch/x86/kvm/vmx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)arch/x86/kvm/vmx.c +4
-5@@ -10474,12 +10474,12 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; }if (vmcs12->vmcs_link_pointer !=-1ull ) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_VMCS_LINK_PTR ); - goto out; +return 1 ; }/* @@ -10499,7 +10499,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; } }@@ -10517,7 +10517,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; } } --2.8.0.rc3.226.g39d4020
Radim Krčmář
Reviewed-by2016-12-19 19:15-0800, Kyle Huey:
Quoted 11 lines · David Matlack
Quoted 4 lines
kvm_skip_emulated_instruction()should not be called after emulating a VM-entry failure during or after loading guest state (nested_vmx_entry_failure()). Otherwise the L1 hypervisor is resumed some number of bytes pastvmcs->host_rip.Ah, I see. Sorry for that regression.
These paths are supposed to trigger TF-induced singlestep exceptions though. Quoting from the Intel SDM (Vol 3, Chapter 26)
Quoted 9 lines
"EFLAGS.TF = 1 causes a VM-entry instruction to generate a single-step debug exception only if failure of one of the checks in Section 26.1 and Section 26.2 causes control to pass to the following instruction. A VM-entry does not generate a single-step debug exception in any of the following cases: (1) the instruction generates a fault; (2) failure of one of the checks in Section 26.3 or in loading MSRs causes processor state to be loaded from the hoststate area of the VMCS; or
(3) the instruction passes all checks in Section 26.1, Section 26.2, and Section 26.3 and there is no failure in loading MSRs"
Changed cases are in section 26.3 => not generating #DB is correct,
Sorry for missing that while applying, I wonder if there is a reason why we didn't check them after enter_guest_mode() ...
Quoted 46 lines
Quoted 46 lines
Fixes eb2775621701e6ee3ea2a474437d04e93ccdcb2fSigned-off-by David Matlack <[email protected]>--- arch/x86/kvm/vmx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)arch/x86/kvm/vmx.c +4
-5@@ -10474,12 +10474,12 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; }if (vmcs12->vmcs_link_pointer !=-1ull ) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_VMCS_LINK_PTR ); - goto out; +return 1 ; }/* @@ -10499,7 +10499,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; } }@@ -10517,7 +10517,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ; } } --2.8.0.rc3.226.g39d4020
2016-12-20 16:24+0100, Radim Krčmář:
Quoted 29 lines
2016-12-19 19:15-0800, Kyle Huey:
Quoted 11 lines · David Matlack
Quoted 4 lines
kvm_skip_emulated_instruction()should not be called after emulating a VM-entry failure during or after loading guest state (nested_vmx_entry_failure()). Otherwise the L1 hypervisor is resumed some number of bytes pastvmcs->host_rip.Ah, I see. Sorry for that regression.
These paths are supposed to trigger TF-induced singlestep exceptions though. Quoting from the Intel SDM (Vol 3, Chapter 26)
Quoted 9 lines
"EFLAGS.TF = 1 causes a VM-entry instruction to generate a single-step debug exception only if failure of one of the checks in Section 26.1 and Section 26.2 causes control to pass to the following instruction. A VM-entry does not generate a single-step debug exception in any of the following cases: (1) the instruction generates a fault; (2) failure of one of the checks in Section 26.3 or in loading MSRs causes processor state to be loaded from the hoststate area of the VMCS; or
(3) the instruction passes all checks in Section 26.1, Section 26.2, and Section 26.3 and there is no failure in loading MSRs"Changed cases are in section 26.3 => not generating #DB is correct,
Reviewed-by Radim Krčmář <[email protected]>Sorry for missing that while applying, I wonder if there is a reason why we didn't check them after
enter_guest_mode()...
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx.c static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) · line 10520
Quoted 6 lines
arch/x86/kvm/vmx.c +1
-1@@ -10517,7 +10517,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ;
My attention is in shambles these days ... this one looks like it is in section 26.2.2 and should therefore be
nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); goto out;
Not a problem of this patch though, I'll go for a minor refactoring after a beer.
Quoted 29 lines · Radim Krčmář
2016-12-20 16:24+0100, Radim Krčmář:
Quoted 28 lines
2016-12-19 19:15-0800, Kyle Huey:
Quoted 11 lines · David Matlack
Quoted 4 lines
kvm_skip_emulated_instruction()should not be called after emulating a VM-entry failure during or after loading guest state (nested_vmx_entry_failure()). Otherwise the L1 hypervisor is resumed some number of bytes pastvmcs->host_rip.Ah, I see. Sorry for that regression.
These paths are supposed to trigger TF-induced singlestep exceptions though. Quoting from the Intel SDM (Vol 3, Chapter 26)
Quoted 9 lines
"EFLAGS.TF = 1 causes a VM-entry instruction to generate a single-step debug exception only if failure of one of the checks in Section 26.1 and Section 26.2 causes control to pass to the following instruction. A VM-entry does not generate a single-step debug exception in any of the following cases: (1) the instruction generates a fault; (2) failure of one of the checks in Section 26.3 or in loading MSRs causes processor state to be loaded from the hoststate area of the VMCS; or
(3) the instruction passes all checks in Section 26.1, Section 26.2, and Section 26.3 and there is no failure in loading MSRs"Changed cases are in section 26.3 => not generating #DB is correct,
Reviewed-by Radim Krčmář <[email protected]>Sorry for missing that while applying, I wonder if there is a reason why we didn't check them after
enter_guest_mode()...
I think this would be a nice cleanup.
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx.c static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) · line 10520
Quoted 7 lines
arch/x86/kvm/vmx.c +1
-1@@ -10517,7 +10517,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT ); - goto out; +return 1 ;My attention is in shambles these days ... this one looks like it is in section 26.2.2 and should therefore be
Quoted 2 lines
nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); goto out;
I agree. Good catch.
Quoted 3 lines
Not a problem of this patch though, I'll go for a minor refactoring after a beer.