Skip to contentSkip to conversation

Various SVM Patches

PATCH In review Brian Woods 3 participants 8 messages Dec 21, 2017 – Jan 5, 2018

lore
Share & tools
lore
#0 lore raw

cover Various SVM Patches

This is small patch series which does some basic maintenance work in the SVM section.

1. Add support for checking the SVME bit before doing nested SVM operations
2. Add support for checking if nestedhvm is enabled before enabling nested SVM features in the VMCB.

Brian Woods (2):
x86/svm: Add SVME checking for SVM intercepts
x86/svm: Add checks for nested HW features

 xen/arch/x86/hvm/svm/svm.c  | 28 ++++++++++++++++++++++++++++
 xen/arch/x86/hvm/svm/vmcb.c |  5 +++--
 2 files changed, 31 insertions(+), 2 deletions(-)
Signature · 31 lines
2.11.0


Brian Woods (2):
  x86/svm: Add SVME checking for SVM intercepts
  x86/svm: Add checks for nested HW features

 xen/arch/x86/hvm/svm/svm.c  | 29 +++++++++++++++++++++++++++++
 xen/arch/x86/hvm/svm/vmcb.c |  5 +++--
 2 files changed, 32 insertions(+), 2 deletions(-)

-- 
2.11.0


Brian Woods (2):
  x86/svm: Add SVME checking for SVM intercepts
  x86/svm: Add checks for nested HW features

 xen/arch/x86/hvm/svm/svm.c  | 29 +++++++++++++++++++++++++++++
 xen/arch/x86/hvm/svm/vmcb.c |  5 +++--
 2 files changed, 32 insertions(+), 2 deletions(-)

-- 
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/2 x86/svm: Add SVME checking for SVM intercepts

Checks the hvm EFER.SVME bit to make sure the EFER.SVME bit it high before allowing nested SVM intercepts to be handled successfully. On SVME being low, it generates a #UD as per the AMD APM vol2 15.4.

---
 xen/arch/x86/hvm/svm/svm.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
xen/arch/x86/hvm/svm/svm.c +29 -0
index 2e62b9bb6d..2d0a82ae77 100644
@@ -2587,6 +2587,35 @@ void svm_vmexit_handler(struct cpu_user_regs *regs)
                                      eventinj.fields.vector) )
         vmcb->eventinj = eventinj;
 
+    /*
+     * Making sure SVME is enabled see AMD APM vol2 section 15.4
+     * Nested Xen needs VMMCALL to boot.  It hasn't set SVME by the time
+     * it uses it, therefore it isn't checked
+     */
+    switch ( exit_reason )
+    {
+    case VMEXIT_VMRUN:
+    case VMEXIT_VMLOAD:
+    case VMEXIT_VMSAVE:
+    case VMEXIT_CLGI:
+    case VMEXIT_INVLPGA:
+        if ( !(nestedhvm_enabled(v->domain) &&
+             nsvm_efer_svm_enabled(v)) )
+        {
+            gdprintk(XENLOG_ERR, "nestedhvm nested/SVME disabled, injecting #UD\n");
+            hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
+	    goto out;
+        }
+    case VMEXIT_STGI:
+    case VMEXIT_SKINIT:
+        if ( !nestedhvm_enabled(v->domain) )
+        {
+            gdprintk(XENLOG_ERR, "nestedhvm nested disabled, injecting #UD\n");
+            hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
+	    goto out;
+        }
+    }
+
     switch ( exit_reason )
     {
     case VMEXIT_INTR:
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH 1/2] x86/svm: Add SVME checking for SVM intercepts

Quoted 1 line · Brian Woods

Checks the hvm EFER.SVME bit to make sure the EFER.SVME bit it high

s/it/is

Quoted 21 lines

before allowing nested SVM intercepts to be handled successfully. On SVME being low, it generates a #UD as per the AMD APM vol2 15.4.

---
 xen/arch/x86/hvm/svm/svm.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
xen/arch/x86/hvm/svm/svm.c +4 -0
index 2e62b9bb6d..2d0a82ae77 100644
@@ -2587,6 +2587,35 @@ void svm_vmexit_handler(struct cpu_user_regs *regs)
                                      eventinj.fields.vector) )
         vmcb->eventinj = eventinj;
 
+    /*
+     * Making sure SVME is enabled see AMD APM vol2 section 15.4
+     * Nested Xen needs VMMCALL to boot.  It hasn't set SVME by the time
+     * it uses it, therefore it isn't checked

xen/arch/x86/hvm/svm/svm.c void svm_vmexit_handler(struct cpu_user_regs *regs) · line 2603

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +10 -0
+     */
+    switch ( exit_reason )
+    {
+    case VMEXIT_VMRUN:
+    case VMEXIT_VMLOAD:
+    case VMEXIT_VMSAVE:
+    case VMEXIT_CLGI:
+    case VMEXIT_INVLPGA:
+        if ( !(nestedhvm_enabled(v->domain) &&
+             nsvm_efer_svm_enabled(v)) )

I haven't checked all of them but at least for the first two (svm_vmexit_do_vmrun() and smv_vmexit_do_vmload()) we check EFER and print a similar error message. So it seems they can be handled in the switch statement below.

-boris

Quoted 8 lines
xen/arch/x86/hvm/svm/svm.c +8 -0
+        {
+            gdprintk(XENLOG_ERR, "nestedhvm nested/SVME disabled, injecting #UD\n");
+            hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
+	    goto out;
+        }
+    case VMEXIT_STGI:
+    case VMEXIT_SKINIT:
+        if ( !nestedhvm_enabled(v->domain) )

xen/arch/x86/hvm/svm/svm.c void svm_vmexit_handler(struct cpu_user_regs *regs) · line 2621

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +7 -0
+        {
+            gdprintk(XENLOG_ERR, "nestedhvm nested disabled, injecting #UD\n");
+            hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
+	    goto out;
+        }
+    }
+
     switch ( exit_reason )
     {
     case VMEXIT_INTR:

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [PATCH 1/2] x86/svm: Add SVME checking for SVM intercepts

Quoted 1 line · Boris Ostrovsky

s/it/is

Oopsy, thank you.

Quoted 6 lines

I haven't checked all of them but at least for the first two (svm_vmexit_do_vmrun() and smv_vmexit_do_vmload()) we check EFER and print a similar error message. So it seems they can be handled in the switch statement below.

-boris

They are indeed checked in those functions. I also need to check to make sure that the switching from L2 to L1 is done correctly in cause of SVME being low. I tested it with Xen and it worked correctly but it might not be failing correctly. The joys of being under the weather and forgetting all your mental notes. I'll try to send out a v2 of this patch today with the corrections.

Thank you for your input.

Signature · 6 lines
Brian Woods

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

PATCH 2/2 x86/svm: Add checks for nested HW features

In reply to Brian Woods

Add a nestedhvm_enable() check to the existing checks for setting the virtual GIF and virtual VMLOAD/VMSAVE features. If it isn't a nestedhvm guest, do not enable the features in the VMCB.

---
 xen/arch/x86/hvm/svm/vmcb.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/svm/vmcb.c +3 -2
index 0e6cba5b7b..62f7d7f7dd 100644
@@ -27,6 +27,7 @@
 #include <asm/msr-index.h>
 #include <asm/p2m.h>
 #include <asm/hvm/support.h>
+#include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/svmdebug.h>
 
@@ -202,7 +203,7 @@ static int construct_vmcb(struct vcpu *v)
         svm_disable_intercept_for_msr(v, MSR_IA32_CR_PAT);
 
         /* Use virtual VMLOAD/VMSAVE if available. */
-        if ( cpu_has_svm_vloadsave )
+        if ( cpu_has_svm_vloadsave && nestedhvm_enabled(v->domain) )
         {
             vmcb->virt_ext.fields.vloadsave_enable = 1;
             vmcb->_general2_intercepts &= ~GENERAL2_INTERCEPT_VMLOAD;
@@ -215,7 +216,7 @@ static int construct_vmcb(struct vcpu *v)
     }
 
     /* if available, enable and configure virtual gif */
-    if ( cpu_has_svm_vgif )
+    if ( cpu_has_svm_vgif && nestedhvm_enabled(v->domain) )
     {
         vmcb->_vintr.fields.vgif = 1;
         vmcb->_vintr.fields.vgif_enable = 1;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] x86/svm: Add checks for nested HW features

Quoted 5 lines · Brian Woods

Add a nestedhvm_enable() check to the existing checks for setting the virtual GIF and virtual VMLOAD/VMSAVE features. If it isn't a nestedhvm guest, do not enable the features in the VMCB.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

Re: [PATCH 2/2] x86/svm: Add checks for nested HW features

In reply to Brian Woods
Quoted 5 lines · Brian Woods

Add a nestedhvm_enable() check to the existing checks for setting the virtual GIF and virtual VMLOAD/VMSAVE features. If it isn't a nestedhvm guest, do not enable the features in the VMCB.

Unfortunately, nestedhvm_enabled() is guaranteed to be false at the point that construct_vmcb() is called (due the order in which information appears while constructing the VM), which means we will never enable these optimisations.

Combined with the observation of EFER in the pipeline, the logic to enable/disable these optimisations needs to be in svm_update_guest_efer(), and need to trigger when EFER.SVME changes.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

Re: [PATCH 2/2] x86/svm: Add checks for nested HW features

Quoted 10 lines · Andrew Cooper

Unfortunately, nestedhvm_enabled() is guaranteed to be false at the point that construct_vmcb() is called (due the order in which information appears while constructing the VM), which means we will never enable these optimisations.

Combined with the observation of EFER in the pipeline, the logic to enable/disable these optimisations needs to be in svm_update_guest_efer(), and need to trigger when EFER.SVME changes.

~Andrew

Sorry for the late reply. I tired working this before vacation but it turned out to be a little bit longer than that... I have a set of patches that _should_ work, but there are other issues. Turns out there are interrupt issues with nestted SVM HVM and I'm trying to hunt those down and fix them so I can properly test the patches I've done. Oddly enough you can at least get a system booted on 17h family systems even if it's fragile but 15h just fails to even boot. Not sure how it even worked when I tested previous patches on the 15h system.

Signature · 6 lines
Brian Woods

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel