Skip to contentSkip to conversation

x86/svm: Fix VMLOAD/VMSAVE state handling when using nested virt

PATCH In review Ross Lagerwall 2 participants 4 messages Sep 11 – Sep 23

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/svm: Fix VMLOAD/VMSAVE state handling when using nested virt

Currently, the SVM VMLOAD/VMSAVE state handling always uses the vCPU's active VMCB to load/store the state. The active VMCB changes depending on whether or not the vCPU is in guest mode yet the state is not properly copied between VMCB(0-1) and VMCB(0-2) nor is the vmcb_sync_state updated when switching active VMCB.

The most common way this fails is when context switching to a vCPU in guest mode and immediately taking a VMEXIT (e.g. due to an interrupt for L1 having arrived in the meantime). The code issues an unconditional VMSAVE into VMCB(0-1) but the state has not yet been loaded since context switching which results in garbage in VMCB(0-1). The garbage state is then (re-)loaded from VMCB(0-1) shortly before VMRUN. This results in frequent, random crashes in L2.

Since the state covered by VMLOAD/VMSAVE is a property of the vCPU and is not affected by switches to and from guest mode, always use VMCB(0-1) to store and retrieve it. This avoids the need to synchronize state between VMCBs and fixes the random crashes. L1 itself is responsible for using VMLOAD/VMSAVE to load/save the state from hardware into its own VMCB(1-2) and this change does not affect that.

Fixes 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")
---
 xen/arch/x86/hvm/svm/nestedsvm.c |   7 --
 xen/arch/x86/hvm/svm/svm.c       | 107 +++++++++++++++++--------------
 xen/arch/x86/hvm/svm/vmcb.c      |  26 ++++----
 xen/arch/x86/hvm/svm/vmcb.h      |   3 +-
 4 files changed, 75 insertions(+), 68 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +0 -7
index 5adb1bd72c4d..c2250b85cf28 100644
@@ -198,11 +198,6 @@ static int nsvm_vcpu_hostrestore(struct vcpu *v, struct cpu_user_regs *regs)
     ASSERT(n1vmcb != NULL);
     ASSERT(n2vmcb != NULL);
 
-    /*
-     * nsvm_vmcb_prepare4vmexit() already saved register values
-     * handled by VMSAVE/VMLOAD into n1vmcb directly.
-     */
-
     /* switch vmcb to l1 guest's vmcb */
     v->arch.hvm.svm.vmcb = n1vmcb;
     v->arch.hvm.svm.vmcb_pa = nv->nv_n1vmcx_pa;
@@ -969,8 +964,6 @@ nsvm_vmcb_prepare4vmexit(struct vcpu *v, struct cpu_user_regs *regs)
     struct vmcb_struct *ns_vmcb = nv->nv_vvmcx;
     struct vmcb_struct *n2vmcb = nv->nv_n2vmcx;
 
-    svm_vmsave_pa(nv->nv_n1vmcx_pa);
-
     /* Cache guest physical address of virtual vmcb
      * for VMCB Cleanbit emulation.
      */
xen/arch/x86/hvm/svm/svm.c +59 -48

280 lines not on this page · raw message

xen/arch/x86/hvm/svm/vmcb.c +14 -12
index 975a1eaef806..6e2a45ff3c4b 100644
@@ -243,16 +243,17 @@ static void svm_dump_sel(const char *name, const struct segment_register *s)
            name, s->sel, s->attr, s->limit, s->base);
 }
 
-void svm_vmcb_dump(const char *from, const struct vmcb_struct *vmcb)
+void svm_vmcb_dump(const char *from, struct vcpu *v,
+                   const struct vmcb_struct *vmcb)
 {
-    struct vcpu *curr = current;
+    struct vmcb_struct *n1_vmcb = vcpu_nestedhvm(v).nv_n1vmcx;
 
     /*
      * If we are dumping the VMCB currently in context, some guest state may
      * still be cached in hardware.  Retrieve it.
      */
-    if ( vmcb == curr->arch.hvm.svm.vmcb )
-        svm_sync_vmcb(curr, vmcb_in_sync);
+    if ( v == current )
+        svm_sync_vmcb(v, vmcb_in_sync);
 
     printk("Dumping guest's current state at %s...\n", from);
     printk("Size of VMCB = %zu, paddr = %"PRIpaddr", vaddr = %p\n",
@@ -286,7 +287,8 @@ void svm_vmcb_dump(const char *from, const struct vmcb_struct *vmcb)
     printk("virtual vmload/vmsave = %d, virt_ext = %#"PRIx64"\n",
            vmcb->virt_ext.fields.vloadsave_enable, vmcb->virt_ext.bytes);
     printk("cpl = %d efer = %#"PRIx64" star = %#"PRIx64" lstar = %#"PRIx64"\n",
-           vmcb_get_cpl(vmcb), vmcb_get_efer(vmcb), vmcb->star, vmcb->lstar);
+           vmcb_get_cpl(vmcb), vmcb_get_efer(vmcb), n1_vmcb->star,
+           n1_vmcb->lstar);
     printk("CR0 = 0x%016"PRIx64" CR2 = 0x%016"PRIx64"\n",
            vmcb_get_cr0(vmcb), vmcb_get_cr2(vmcb));
     printk("CR3 = 0x%016"PRIx64" CR4 = 0x%016"PRIx64"\n",
@@ -298,9 +300,9 @@ void svm_vmcb_dump(const char *from, const struct vmcb_struct *vmcb)
     printk("DR6 = 0x%016"PRIx64", DR7 = 0x%016"PRIx64"\n",
            vmcb_get_dr6(vmcb), vmcb_get_dr7(vmcb));
     printk("CSTAR = 0x%016"PRIx64" SFMask = 0x%016"PRIx64"\n",
-           vmcb->cstar, vmcb->sfmask);
+           n1_vmcb->cstar, n1_vmcb->sfmask);
     printk("KernGSBase = 0x%016"PRIx64" PAT = 0x%016"PRIx64"\n",
-           vmcb->kerngsbase, vmcb_get_g_pat(vmcb));
+           n1_vmcb->kerngsbase, vmcb_get_g_pat(vmcb));
     printk("SSP = 0x%016"PRIx64" S_CET = 0x%016"PRIx64" ISST = 0x%016"PRIx64"\n",
            vmcb->_ssp, vmcb->_msr_s_cet, vmcb->_msr_isst);
     printk("H_CR3 = 0x%016"PRIx64" CleanBits = %#x\n",
@@ -312,12 +314,12 @@ void svm_vmcb_dump(const char *from, const struct vmcb_struct *vmcb)
     svm_dump_sel("  DS", &vmcb->ds);
     svm_dump_sel("  SS", &vmcb->ss);
     svm_dump_sel("  ES", &vmcb->es);
-    svm_dump_sel("  FS", &vmcb->fs);
-    svm_dump_sel("  GS", &vmcb->gs);
+    svm_dump_sel("  FS", &n1_vmcb->fs);
+    svm_dump_sel("  GS", &n1_vmcb->gs);
     svm_dump_sel("GDTR", &vmcb->gdtr);
-    svm_dump_sel("LDTR", &vmcb->ldtr);
+    svm_dump_sel("LDTR", &n1_vmcb->ldtr);
     svm_dump_sel("IDTR", &vmcb->idtr);
-    svm_dump_sel("  TR", &vmcb->tr);
+    svm_dump_sel("  TR", &n1_vmcb->tr);
 }
 
 bool svm_vmcb_isvalid(
@@ -418,7 +420,7 @@ static void cf_check vmcb_dump(unsigned char ch)
                 continue;
             }
             printk("\tVCPU %d\n", v->vcpu_id);
-            svm_vmcb_dump("key_handler", v->arch.hvm.svm.vmcb);
+            svm_vmcb_dump("key_handler", v, v->arch.hvm.svm.vmcb);
 
             process_pending_softirqs();
         }
xen/arch/x86/hvm/svm/vmcb.h +2 -1
index 3760f71a8625..2bae45e41971 100644
@@ -563,7 +563,8 @@ int  svm_create_vmcb(struct vcpu *v);
 void svm_destroy_vmcb(struct vcpu *v);
 
 void setup_vmcb_dump(void);
-void svm_vmcb_dump(const char *from, const struct vmcb_struct *vmcb);
+void svm_vmcb_dump(const char *from, struct vcpu *v,
+                   const struct vmcb_struct *vmcb);
 bool svm_vmcb_isvalid(const char *from, const struct vmcb_struct *vmcb,
                       const struct vcpu *v, bool verbose);
 
Signature · 1 line
2.53.0
#1 lore raw

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c) · line 447

Quoted 8 lines · Ross Lagerwall
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -444,30 +444,30 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c)
 
 static void svm_save_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 {
-    struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
+    struct vmcb_struct *n1_vmcb = vcpu_nestedhvm(v).nv_n1vmcx;

Here and elsewhere you assume that vcpu_nestedhvm() is legitimate to use even in the non-nested case. I think that's heading in the wrong direction; I think that a hypothetical mode with CONFIG_NESTED=n and the entire struct nestedvcpu wrapped in #ifdef CONFIG_NESTED should still be possible to put in place, without meaningful rearrangements or renaming besides the adding of the respective #if{,n}def.

Jan

#2 lore raw
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Ross Lagerwall
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -444,30 +444,30 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c)
 
Quoted 11 lines

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c) · line 447

Quoted 4 lines
xen/arch/x86/hvm/svm/svm.c +1 -1
  static void svm_save_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
  {
-    struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
+    struct vmcb_struct *n1_vmcb = vcpu_nestedhvm(v).nv_n1vmcx;

Here and elsewhere you assume that vcpu_nestedhvm() is legitimate to use even in the non-nested case. I think that's heading in the wrong direction; I think that a hypothetical mode with CONFIG_NESTED=n and the entire struct nestedvcpu wrapped in #ifdef CONFIG_NESTED should still be possible to put in place, without meaningful rearrangements or renaming besides the adding of the respective #if{,n}def.

This assumption already exists, e.g. see svm_create_vmcb(), svm_destroy_vmcb(), svm_dr_access().

If you would prefer not to introduce any new cases like this, I could introduce a new macro, e.g.

#define vcpu_n1vmcx(v) (vcpu_nestedhvm((v)).nv_n1vmcb)

and then for the hypothetical CONFIG_NESTED=n, it would resolve to:

#define vcpu_n1vmcx(v) ((v)->arch.hvm.svm.vmcb)

Does that work for you?

Ross

#3 lore raw
Quoted 18 lines · Ross Lagerwall
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Ross Lagerwall
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -444,30 +444,30 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c)
 
Quoted 11 lines

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c) · line 447

Quoted 4 lines
xen/arch/x86/hvm/svm/svm.c +1 -1
  static void svm_save_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
  {
-    struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
+    struct vmcb_struct *n1_vmcb = vcpu_nestedhvm(v).nv_n1vmcx;

Here and elsewhere you assume that vcpu_nestedhvm() is legitimate to use even in the non-nested case. I think that's heading in the wrong direction; I think that a hypothetical mode with CONFIG_NESTED=n and the entire struct nestedvcpu wrapped in #ifdef CONFIG_NESTED should still be possible to put in place, without meaningful rearrangements or renaming besides the adding of the respective #if{,n}def.

Quoted 2 lines

This assumption already exists, e.g. see svm_create_vmcb(), svm_destroy_vmcb(), svm_dr_access().

Hmm, I see. That's not very nice, but then of course you're okay to follow this model.

Quoted 8 lines

If you would prefer not to introduce any new cases like this, I could introduce a new macro, e.g.

#define vcpu_n1vmcx(v) (vcpu_nestedhvm((v)).nv_n1vmcb)

and then for the hypothetical CONFIG_NESTED=n, it would resolve to:

#define vcpu_n1vmcx(v) ((v)->arch.hvm.svm.vmcb)

May be worthwhile independently, but as per above I wouldn't insist on you doing this right here.

Jan