Skip to contentSkip to conversation

x86/svm: Fix a livelock when trying to run shadowed unpaged guests

PATCH In review Andrew Cooper 2 participants 3 messages Sep 29, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/svm: Fix a livelock when trying to run shadowed unpaged guests

On AMD processors which support SMEP (Some Fam16h processors) and SMAP (Zen, Fam17h), a guest which is running with shadow paging and clears CR0.PG while keeping CR4.{SMEP,SMAP} set will livelock, as hardware raises #PF which the shadow pagetable concludes shouldn't happen.

This occurs because hardware is running with host paging settings, which causes the guests choice of SMEP/SMAP to actually take effect, even though they shouldn't from the guests point of view.

---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

Discovered when trying to fix my comprehensive pagetable XTF test to run on Zen processors.

---
 xen/arch/x86/hvm/svm/svm.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
xen/arch/x86/hvm/svm/svm.c +18 -0
index 12ddc8a..b9cf423 100644
@@ -576,6 +576,24 @@ void svm_update_guest_cr(struct vcpu *v, unsigned int cr)
         if ( paging_mode_hap(v->domain) )
             value &= ~X86_CR4_PAE;
         value |= v->arch.hvm_vcpu.guest_cr[4];
+
+        if ( !hvm_paging_enabled(v) )
+        {
+            /*
+             * When the guest thinks paging is disabled, Xen may need to hide
+             * the effects of shadow paging, as hardware runs with the host
+             * paging settings, rather than the guests settings.
+             *
+             * Without CR0.PG, all memory accesses are user mode, so
+             * _PAGE_USER must be set in the shadow pagetables for guest
+             * userspace to function.  This in turn trips up guest supervisor
+             * mode if SMEP/SMAP are left active in context.  They wouldn't
+             * have any effect if paging was actually disabled, so hide them
+             * behind the back of the guest.
+             */
+            value &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
+        }
+
         vmcb_set_cr4(vmcb, value);
         break;
     default:
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrew Cooper

On AMD processors which support SMEP (Some Fam16h processors) and SMAP (Zen, Fam17h), a guest which is running with shadow paging and clears CR0.PG while keeping CR4.{SMEP,SMAP} set will livelock, as hardware raises #PF which the shadow pagetable concludes shouldn't happen.

This occurs because hardware is running with host paging settings, which causes the guests choice of SMEP/SMAP to actually take effect, even though they shouldn't from the guests point of view.

although the commit message makes it sound as if this problem is specific to AMD. This is not an issue on Intel because VMX already has such code, no?

-boris

Quoted 32 lines

---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

Discovered when trying to fix my comprehensive pagetable XTF test to run on Zen processors.

---
 xen/arch/x86/hvm/svm/svm.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
xen/arch/x86/hvm/svm/svm.c +11 -0
index 12ddc8a..b9cf423 100644
@@ -576,6 +576,24 @@ void svm_update_guest_cr(struct vcpu *v, unsigned int cr)
         if ( paging_mode_hap(v->domain) )
             value &= ~X86_CR4_PAE;
         value |= v->arch.hvm_vcpu.guest_cr[4];
+
+        if ( !hvm_paging_enabled(v) )
+        {
+            /*
+             * When the guest thinks paging is disabled, Xen may need to hide
+             * the effects of shadow paging, as hardware runs with the host
+             * paging settings, rather than the guests settings.
+             *
+             * Without CR0.PG, all memory accesses are user mode, so
+             * _PAGE_USER must be set in the shadow pagetables for guest
+             * userspace to function.  This in turn trips up guest supervisor

xen/arch/x86/hvm/svm/svm.c void svm_update_guest_cr(struct vcpu *v, unsigned int cr) · line 599

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +7 -0
+             * mode if SMEP/SMAP are left active in context.  They wouldn't
+             * have any effect if paging was actually disabled, so hide them
+             * behind the back of the guest.
+             */
+            value &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
+        }
+
         vmcb_set_cr4(vmcb, value);
         break;
     default:

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

#2 lore raw
Quoted 6 lines · Boris Ostrovsky
Quoted 5 lines · Andrew Cooper

On AMD processors which support SMEP (Some Fam16h processors) and SMAP (Zen, Fam17h), a guest which is running with shadow paging and clears CR0.PG while keeping CR4.{SMEP,SMAP} set will livelock, as hardware raises #PF which the shadow pagetable concludes shouldn't happen.

Quoted 8 lines
Quoted 3 lines

This occurs because hardware is running with host paging settings, which causes the guests choice of SMEP/SMAP to actually take effect, even though they shouldn't from the guests point of view.

although the commit message makes it sound as if this problem is specific to AMD. This is not an issue on Intel because VMX already has such code, no?

"Its Complicated" (he says, having spent far too long pouring over each set of manuals).

The SMEP/SMAP behaviour with paging enabled while the guest is in unpaged mode are architectural and the same on Intel and AMD processors.

As for control register settings, AMD and Intel behave differently. Intel has separate host and guest control register fields in the VMCS, while AMD only has guest control register fields in the VMCB and they behave differently depending on whether NPT is enabled or not (at which point, some settings are inherited from the host state on VMRUN).

There have been a very large number of patches to fix this on Intel, which as of c/s 224acdd0 appears to function correctly in all combinations.  I am clearly the first person to try and excise this corner case in anger on AMD hardware.

This brings the AMD code in line Intel, other than the unrestricted_guest corner case, which is specific to Intel.

~Andrew

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