Skip to contentSkip to conversation

KVM: nSVM: check for EFER.SVME=1 before entering guest

PATCH In review Paolo Bonzini 3 participants 5 messages Mar 18, 2020 – Mar 19, 2020

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 KVM: nSVM: check for EFER.SVME=1 before entering guest

EFER is set for L2 using svm_set_efer, which hardcodes EFER_SVME to 1 and hides an incorrect value for EFER.SVME in the L1 VMCB. Perform the check manually to detect invalid guest state.

---
 arch/x86/kvm/svm.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/svm.c +3 -0
index 08568ae9f7a1..2125c6ae5951 100644
@@ -3558,6 +3558,9 @@ static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
 
 static bool nested_vmcb_checks(struct vmcb *vmcb)
 {
+	if ((vmcb->save.efer & EFER_SVME) == 0)
+		return false;
+
 	if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
 		return false;
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Paolo Bonzini

EFER is set for L2 using svm_set_efer, which hardcodes EFER_SVME to 1 and hides an incorrect value for EFER.SVME in the L1 VMCB. Perform the check manually to detect invalid guest state.

---
 arch/x86/kvm/svm.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/svm.c +0 -0
index 08568ae9f7a1..2125c6ae5951 100644

arch/x86/kvm/svm.c static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) · line 3565

Quoted 8 lines
arch/x86/kvm/svm.c +3 -0
@@ -3558,6 +3558,9 @@ static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
 
 static bool nested_vmcb_checks(struct vmcb *vmcb)
 {
+	if ((vmcb->save.efer & EFER_SVME) == 0)
+		return false;
+
 	if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
 		return false;
Signature · 1 line
Vitaly
#2 lore raw
In reply to Paolo Bonzini
Quoted 15 lines · Paolo Bonzini

EFER is set for L2 using svm_set_efer, which hardcodes EFER_SVME to 1 and hides an incorrect value for EFER.SVME in the L1 VMCB. Perform the check manually to detect invalid guest state.

---
  arch/x86/kvm/svm.c | 3 +++
  1 file changed, 3 insertions(+)
arch/x86/kvm/svm.c +0 -0
index 08568ae9f7a1..2125c6ae5951 100644

arch/x86/kvm/svm.c static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) · line 3566

Quoted 9 lines
arch/x86/kvm/svm.c +3 -0
@@ -3558,6 +3558,9 @@ static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
 
  static bool nested_vmcb_checks(struct vmcb *vmcb)
  {
+	if ((vmcb->save.efer & EFER_SVME) == 0)
+		return false;
+
  	if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
  		return false;
 

Ah! This now tells me that I forgot the KVM fix that was supposed to accompany my patchset.

Do we need this check in software ? I wasn't checking the bit in KVM and instead I was just making sure that L0 sets that bit based on the setting in nested vmcb:

+static void nested_svm_set_efer(struct kvm_vcpu *vcpu, u64 nested_vmcb_efer) +{
+       svm_set_efer(vcpu, nested_vmcb_efer);
+
+       if (!(nested_vmcb_efer & EFER_SVME))
+               to_svm(vcpu)->vmcb->save.efer &= ~EFER_SVME;
+}
+
 static int is_external_interrupt(u32 info)
 {
        info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
@@ -3554,7 +3562,7 @@ static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; svm->vmcb->save.idtr = nested_vmcb->save.idtr; kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
-       svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
+       nested_svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
        if (npt_enabled) {

author maintainer
#3 lore raw
Quoted 23 lines · Krish Sadhukhan
Quoted 21 lines · Paolo Bonzini

EFER is set for L2 using svm_set_efer, which hardcodes EFER_SVME to 1 and hides
an incorrect value for EFER.SVME in the L1 VMCB.  Perform the check manually
to detect invalid guest state.

---
  arch/x86/kvm/svm.c | 3 +++

  1 file changed, 3 insertions(+)
arch/x86/kvm/svm.c +1 -0
index 08568ae9f7a1..2125c6ae5951 100644
@@ -3558,6 +3558,9 @@ static bool nested_svm_vmrun_msrpm(struct
 vcpu_svm *svm)
     static bool nested_vmcb_checks(struct vmcb *vmcb)
   {
+    if ((vmcb->save.efer & EFER_SVME) == 0)
Quoted 8 lines
Quoted 5 lines
arch/x86/kvm/svm.c +2 -0
+        return false;
+
       if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
           return false;

Ah! This now tells me that I forgot the KVM fix that was supposed to accompany my patchset.

Heh, indeed. I was puzzled for a second after applying it, then decided I would just fix it myself. :)

Quoted 3 lines

Do we need this check in software ? I wasn't checking the bit in KVM and instead I was just making sure that L0 sets that bit based on the setting in nested vmcb:

The only effect of the function below over svm_set_efer is to guarantee a vmrun error to happen. Doing the test in nested_vmcb_checks is more consistent with other must-be-one bits such as the VMRUN intercept, and it's also a smaller patch.

Paolo

Quoted 24 lines

+static void nested_svm_set_efer(struct kvm_vcpu *vcpu, u64 nested_vmcb_efer)

(quoted diff) +7 -0
+{
+       svm_set_efer(vcpu, nested_vmcb_efer);
+
+       if (!(nested_vmcb_efer & EFER_SVME))
+               to_svm(vcpu)->vmcb->save.efer &= ~EFER_SVME;
+}
+

 static int is_external_interrupt(u32 info)
 {
        info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;

(quoted diff) +1 -1
@@ -3554,7 +3562,7 @@ static void enter_svm_guest_mode(struct vcpu_svm
 *svm, u64
         svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
         svm->vmcb->save.idtr = nested_vmcb->save.idtr;
         kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
-       svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
+       nested_svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
         svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
         svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);

        if (npt_enabled) {

Reviewed-by
#4 lore raw
Quoted 30 lines · Paolo Bonzini
Quoted 13 lines · Krish Sadhukhan
Quoted 12 lines · Paolo Bonzini

EFER is set for L2 using svm_set_efer, which hardcodes EFER_SVME to 1 and hides
an incorrect value for EFER.SVME in the L1 VMCB.  Perform the check manually
to detect invalid guest state.

---
   arch/x86/kvm/svm.c | 3 +++
   1 file changed, 3 insertions(+)
Quoted 16 lines
Quoted 5 lines
arch/x86/kvm/svm.c +0 -0
index 08568ae9f7a1..2125c6ae5951 100644

arch/x86/kvm/svm.c static bool nested_svm_vmrun_msrpm(struct · line 3566

Quoted 9 lines
arch/x86/kvm/svm.c +3 -0
@@ -3558,6 +3558,9 @@ static bool nested_svm_vmrun_msrpm(struct
 vcpu_svm *svm)
     static bool nested_vmcb_checks(struct vmcb *vmcb)
   {
+    if ((vmcb->save.efer & EFER_SVME) == 0)
+        return false;
+
       if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
           return false;
 

Ah! This now tells me that I forgot the KVM fix that was supposed to accompany my patchset.

Quoted 6 lines

Heh, indeed. I was puzzled for a second after applying it, then decided I would just fix it myself. :)

Quoted 3 lines

Do we need this check in software ? I wasn't checking the bit in KVM and instead I was just making sure that L0 sets that bit based on the setting in nested vmcb:

Thanks !

Quoted 4 lines

The only effect of the function below over svm_set_efer is to guarantee a vmrun error to happen. Doing the test in nested_vmcb_checks is more consistent with other must-be-one bits such as the VMRUN intercept, and it's also a smaller patch.

Quoted 26 lines

Paolo

Quoted 23 lines

+static void nested_svm_set_efer(struct kvm_vcpu *vcpu, u64 nested_vmcb_efer)

(quoted diff) +8 -1
+{
+       svm_set_efer(vcpu, nested_vmcb_efer);
+
+       if (!(nested_vmcb_efer & EFER_SVME))
+               to_svm(vcpu)->vmcb->save.efer &= ~EFER_SVME;
+}
+
  static int is_external_interrupt(u32 info)
  {
         info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
@@ -3554,7 +3562,7 @@ static void enter_svm_guest_mode(struct vcpu_svm
 *svm, u64
         svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
         svm->vmcb->save.idtr = nested_vmcb->save.idtr;
         kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
-       svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
+       nested_svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
         svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
         svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);

        if (npt_enabled) {