Skip to contentSkip to conversation

x86/svm: Fix svm_update_guest_efer() for domains using shadow paging

PATCH In review Andrew Cooper 4 participants 7 messages Oct 5, 2018 – Oct 17, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/svm: Fix svm_update_guest_efer() for domains using shadow paging

When using shadow paging, EFER.NX is a Xen controlled bit, and is required by the shadow pagefault handler to distinguish instruction fetches from data accesses.

This can be observed by a guest which has NX and SMEP clear but SMAP active by attempting to execute code on a user mapping. The first attempt to build the target shadow will #PF so is handled by the shadow code, but when walking the the guest pagetables, the lack of PFEC_insn_fetch being signalled causes the shadow code to mistake the instruction fetch for a data fetch, and believe that it is a real guest fault. As a result, the guest receives #PF[-d-srP] for an action which should complete successfully.

The suspicious-looking gymnastics with LME is actually a subtle corner case with shadow paging. When dropping out of Long Mode, a guests choice of LME and Xen's choice of CR0.PG cause hardware to operate in Long Mode, but the shadow code to operate in 2-on-3 mode.

In addition to describing this corner case in the SVM side, extend the comment for the same fix on the VT-x side. (I have a suspicion that I've just worked out why VT-x doesn't tolerate LMA != LME when Unrestricted Guest is clear.)

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

Unlike the VT-x side, there is no point playing with the conditional intercept of EFER reads. The SVME requirement means that only L1 hypervisors would benefit from accelerated reads.

---
 xen/arch/x86/hvm/svm/svm.c | 31 +++++++++++++++++++++++++------
 xen/arch/x86/hvm/vmx/vmx.c |  6 ++++++
 2 files changed, 31 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/svm/svm.c +25 -6
index c98cfc2..2ab2c54 100644
@@ -649,13 +649,32 @@ void svm_update_guest_cr(struct vcpu *v, unsigned int cr, unsigned int flags)
 static void svm_update_guest_efer(struct vcpu *v)
 {
     struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
-    bool lma = v->arch.hvm.guest_efer & EFER_LMA;
-    uint64_t new_efer;
+    unsigned long guest_efer = v->arch.hvm.guest_efer,
+        xen_efer = read_efer();
 
-    new_efer = (v->arch.hvm.guest_efer | EFER_SVME) & ~EFER_LME;
-    if ( lma )
-        new_efer |= EFER_LME;
-    vmcb_set_efer(vmcb, new_efer);
+    if ( paging_mode_shadow(v->domain) )
+    {
+        /* EFER.NX is a Xen-owned bit and is not under guest control. */
+        guest_efer &= ~EFER_NX;
+        guest_efer |= xen_efer & EFER_NX;
+
+        /*
+         * CR0.PG is a Xen-owned bit, and remains set even when the guest has
+         * logically disabled paging.
+         *
+         * LMA was calculated using the guest CR0.PG setting, but LME needs
+         * clearing to avoid interacting with Xen's CR0.PG setting.  As writes
+         * to CR0 are intercepted, it is safe to leave LME clear at this
+         * point, and fix up both LME and LMA when CR0.PG is set.
+         */
+        if ( !(guest_efer & EFER_LMA) )
+            guest_efer &= ~EFER_LME;
+    }
+
+    /* SVME must remain set in non-root mode. */
+    guest_efer |= EFER_SVME;
+
+    vmcb_set_efer(vmcb, guest_efer);
 
     ASSERT(nestedhvm_enabled(v->domain) ||
            !(v->arch.hvm.guest_efer & EFER_SVME));
xen/arch/x86/hvm/vmx/vmx.c +6 -0
index bf90e22..eea6330 100644
@@ -1666,6 +1666,12 @@ static void vmx_update_guest_efer(struct vcpu *v)
          * not tolerate the LME and LMA settings being different.  As writes
          * to CR0 are intercepted, it is safe to leave LME clear at this
          * point, and fix up both LME and LMA when CR0.PG is set.
+         *
+         * Furthermore, when using shadow pagetables (subsumed by the
+         * Unrestricted Guest check), CR0.PG is a Xen-owned bit, and remains
+         * set even when the guest has logically disabled paging.  LMA was
+         * calculated using the guest CR0.PG setting, but LME needs clearing
+         * to avoid interacting with Xen's CR0.PG setting.
          */
         if ( !(guest_efer & EFER_LMA) )
             guest_efer &= ~EFER_LME;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 05.10.18 at 19:02, <[email protected]> wrote:
xen/arch/x86/hvm/svm/svm.c +10 -6
@@ -649,13 +649,32 @@ void svm_update_guest_cr(struct vcpu *v, unsigned int cr, unsigned int flags)
 static void svm_update_guest_efer(struct vcpu *v)
 {
     struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
-    bool lma = v->arch.hvm.guest_efer & EFER_LMA;
-    uint64_t new_efer;
+    unsigned long guest_efer = v->arch.hvm.guest_efer,
+        xen_efer = read_efer();
 
-    new_efer = (v->arch.hvm.guest_efer | EFER_SVME) & ~EFER_LME;
-    if ( lma )
-        new_efer |= EFER_LME;
-    vmcb_set_efer(vmcb, new_efer);
+    if ( paging_mode_shadow(v->domain) )
+    {
+        /* EFER.NX is a Xen-owned bit and is not under guest control. */
+        guest_efer &= ~EFER_NX;
+        guest_efer |= xen_efer & EFER_NX;
+
+        /*
+         * CR0.PG is a Xen-owned bit, and remains set even when the guest has

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

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +10 -0
+         * logically disabled paging.
+         *
+         * LMA was calculated using the guest CR0.PG setting, but LME needs
+         * clearing to avoid interacting with Xen's CR0.PG setting.  As writes
+         * to CR0 are intercepted, it is safe to leave LME clear at this
+         * point, and fix up both LME and LMA when CR0.PG is set.
+         */
+        if ( !(guest_efer & EFER_LMA) )
+            guest_efer &= ~EFER_LME;
+    }

I think this wants an "else", either ASSERT()ing that what the removed code did is actually the case (arranged for by the callers), or retaining the original logic in some form. This looks particularly relevant when hvm_efer_valid() was called with -1 as its cr0_pg argument, as in that case there was not necessarily any correlation enforced between EFER.LMA and CR0.PG.

Jan

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

#2 lore raw
Quoted 29 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 05.10.18 at 19:02, <[email protected]> wrote:
xen/arch/x86/hvm/svm/svm.c +15 -6
@@ -649,13 +649,32 @@ void svm_update_guest_cr(struct vcpu *v, unsigned int cr, unsigned int flags)
 static void svm_update_guest_efer(struct vcpu *v)
 {
     struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
-    bool lma = v->arch.hvm.guest_efer & EFER_LMA;
-    uint64_t new_efer;
+    unsigned long guest_efer = v->arch.hvm.guest_efer,
+        xen_efer = read_efer();
 
-    new_efer = (v->arch.hvm.guest_efer | EFER_SVME) & ~EFER_LME;
-    if ( lma )
-        new_efer |= EFER_LME;
-    vmcb_set_efer(vmcb, new_efer);
+    if ( paging_mode_shadow(v->domain) )
+    {
+        /* EFER.NX is a Xen-owned bit and is not under guest control. */
+        guest_efer &= ~EFER_NX;
+        guest_efer |= xen_efer & EFER_NX;
+
+        /*
+         * CR0.PG is a Xen-owned bit, and remains set even when the guest has
+         * logically disabled paging.
+         *
+         * LMA was calculated using the guest CR0.PG setting, but LME needs
+         * clearing to avoid interacting with Xen's CR0.PG setting.  As writes
+         * to CR0 are intercepted, it is safe to leave LME clear at this
Quoted 8 lines

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

Quoted 5 lines
xen/arch/x86/hvm/svm/svm.c +5 -0
+         * point, and fix up both LME and LMA when CR0.PG is set.
+         */
+        if ( !(guest_efer & EFER_LMA) )
+            guest_efer &= ~EFER_LME;
+    }

I think this wants an "else", either ASSERT()ing that what the removed code did is actually the case (arranged for by the callers), or retaining the original logic in some form.

No - the original logic does not want keeping.  It is a latent bug in the HAP case, because if the guest could write to CR0, setting CR0.PG would fail to activate long mode.

Nothing goes wrong because SVM doesn't have a guest/host mask which allows selective updating of

Quoted 4 lines

This looks particularly relevant
when hvm_efer_valid() was called with -1 as its cr0_pg argument, as in that case there was not necessarily any correlation enforced between EFER.LMA and CR0.PG.

On the subject of passing -1,  all of that logic is horrible, but it is only ever used when LME/LMA is being recalculated around other state changes.

This patch brings the SVM logic in line with VT-x logic (c/s fd32dcfe4)
- SVM and VT-x are a whole lot less different than our code suggests. If there were problems with the common code, we've seen them already.

Either both functions want changing, or neither, but I don't think it is a useful check to make.

~Andrew

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

#3 lore raw
Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 08.10.18 at 13:03, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

xen/arch/x86/hvm/svm/svm.c void svm_update_guest_cr(struct vcpu *v, unsigned int

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.10.18 at 19:02, <[email protected]> wrote:
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -649,13 +649,32 @@ void svm_update_guest_cr(struct vcpu *v, unsigned int 

cr, unsigned int flags)

Quoted 30 lines
Quoted 30 lines
xen/arch/x86/hvm/svm/svm.c +20 -6
 static void svm_update_guest_efer(struct vcpu *v)
 {
     struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
-    bool lma = v->arch.hvm.guest_efer & EFER_LMA;
-    uint64_t new_efer;
+    unsigned long guest_efer = v->arch.hvm.guest_efer,
+        xen_efer = read_efer();
 
-    new_efer = (v->arch.hvm.guest_efer | EFER_SVME) & ~EFER_LME;
-    if ( lma )
-        new_efer |= EFER_LME;
-    vmcb_set_efer(vmcb, new_efer);
+    if ( paging_mode_shadow(v->domain) )
+    {
+        /* EFER.NX is a Xen-owned bit and is not under guest control. */
+        guest_efer &= ~EFER_NX;
+        guest_efer |= xen_efer & EFER_NX;
+
+        /*
+         * CR0.PG is a Xen-owned bit, and remains set even when the guest has
+         * logically disabled paging.
+         *
+         * LMA was calculated using the guest CR0.PG setting, but LME needs
+         * clearing to avoid interacting with Xen's CR0.PG setting.  As writes
+         * to CR0 are intercepted, it is safe to leave LME clear at this
+         * point, and fix up both LME and LMA when CR0.PG is set.
+         */
+        if ( !(guest_efer & EFER_LMA) )
+            guest_efer &= ~EFER_LME;
+    }
Quoted 7 lines
Quoted 3 lines

I think this wants an "else", either ASSERT()ing that what the removed code did is actually the case (arranged for by the callers), or retaining the original logic in some form.

No - the original logic does not want keeping. It is a latent bug in the HAP case, because if the guest could write to CR0, setting CR0.PG would fail to activate long mode.

Hmm, perhaps we're talking of different parts of the original code:
I think you talk about the clearing of LME, whereas I am mostly concerned about the dropped implication of LME from LMA. Also note that I suggested keeping some form of the old code only as one option; the other was to add an ASSERT() verifying that the callers have taken care of that implication. In particular for these
...

Quoted 8 lines
Quoted 4 lines

This looks particularly relevant
when hvm_efer_valid() was called with -1 as its cr0_pg argument, as in that case there was not necessarily any correlation enforced between EFER.LMA and CR0.PG.

On the subject of passing -1, all of that logic is horrible, but it is only ever used when LME/LMA is being recalculated around other state changes.

... cases I couldn't really convince myself that all callers really guarantee this.

Anyway - don't read this as an objection to the change. I agree SVM and VMX should be in line with one another wherever possible.

Jan

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

Reviewed-by on v1 1/1
#4 lore raw
In reply to Andrew Cooper
Quoted 26 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Saturday, October 6, 2018 1:02 AM

When using shadow paging, EFER.NX is a Xen controlled bit, and is required by
the shadow pagefault handler to distinguish instruction fetches from data accesses.

This can be observed by a guest which has NX and SMEP clear but SMAP active by
attempting to execute code on a user mapping. The first attempt to build the
target shadow will #PF so is handled by the shadow code, but when walking the
the guest pagetables, the lack of PFEC_insn_fetch being signalled causes the shadow code to mistake the instruction fetch for a data fetch, and believe that it is a real guest fault. As a result, the guest receives #PF[-d-srP] for an action which should complete successfully.

The suspicious-looking gymnastics with LME is actually a subtle corner case with shadow paging. When dropping out of Long Mode, a guests choice of LME
and Xen's choice of CR0.PG cause hardware to operate in Long Mode, but the
shadow code to operate in 2-on-3 mode.

Quoted 6 lines

In addition to describing this corner case in the SVM side, extend the comment
for the same fix on the VT-x side. (I have a suspicion that I've just worked out why VT-x doesn't tolerate LMA != LME when Unrestricted Guest is clear.)

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

#5 lore raw

Ping AMD maintainers? [PATCH] x86/svm: Fix svm_update_guest_efer() for domains using shadow paging

In reply to Andrew Cooper
Quoted 91 lines · Andrew Cooper

When using shadow paging, EFER.NX is a Xen controlled bit, and is required by the shadow pagefault handler to distinguish instruction fetches from data accesses.

This can be observed by a guest which has NX and SMEP clear but SMAP active by attempting to execute code on a user mapping. The first attempt to build the target shadow will #PF so is handled by the shadow code, but when walking the the guest pagetables, the lack of PFEC_insn_fetch being signalled causes the shadow code to mistake the instruction fetch for a data fetch, and believe that it is a real guest fault. As a result, the guest receives #PF[-d-srP] for an action which should complete successfully.

The suspicious-looking gymnastics with LME is actually a subtle corner case with shadow paging. When dropping out of Long Mode, a guests choice of LME and Xen's choice of CR0.PG cause hardware to operate in Long Mode, but the shadow code to operate in 2-on-3 mode.

In addition to describing this corner case in the SVM side, extend the comment for the same fix on the VT-x side. (I have a suspicion that I've just worked out why VT-x doesn't tolerate LMA != LME when Unrestricted Guest is clear.)

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

Unlike the VT-x side, there is no point playing with the conditional intercept of EFER reads. The SVME requirement means that only L1 hypervisors would benefit from accelerated reads.

---
 xen/arch/x86/hvm/svm/svm.c | 31 +++++++++++++++++++++++++------
 xen/arch/x86/hvm/vmx/vmx.c |  6 ++++++
 2 files changed, 31 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/svm/svm.c +25 -6
index c98cfc2..2ab2c54 100644
@@ -649,13 +649,32 @@ void svm_update_guest_cr(struct vcpu *v, unsigned int cr, unsigned int flags)
 static void svm_update_guest_efer(struct vcpu *v)
 {
     struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
-    bool lma = v->arch.hvm.guest_efer & EFER_LMA;
-    uint64_t new_efer;
+    unsigned long guest_efer = v->arch.hvm.guest_efer,
+        xen_efer = read_efer();
 
-    new_efer = (v->arch.hvm.guest_efer | EFER_SVME) & ~EFER_LME;
-    if ( lma )
-        new_efer |= EFER_LME;
-    vmcb_set_efer(vmcb, new_efer);
+    if ( paging_mode_shadow(v->domain) )
+    {
+        /* EFER.NX is a Xen-owned bit and is not under guest control. */
+        guest_efer &= ~EFER_NX;
+        guest_efer |= xen_efer & EFER_NX;
+
+        /*
+         * CR0.PG is a Xen-owned bit, and remains set even when the guest has
+         * logically disabled paging.
+         *
+         * LMA was calculated using the guest CR0.PG setting, but LME needs
+         * clearing to avoid interacting with Xen's CR0.PG setting.  As writes
+         * to CR0 are intercepted, it is safe to leave LME clear at this
+         * point, and fix up both LME and LMA when CR0.PG is set.
+         */
+        if ( !(guest_efer & EFER_LMA) )
+            guest_efer &= ~EFER_LME;
+    }
+
+    /* SVME must remain set in non-root mode. */
+    guest_efer |= EFER_SVME;
+
+    vmcb_set_efer(vmcb, guest_efer);
 
     ASSERT(nestedhvm_enabled(v->domain) ||
            !(v->arch.hvm.guest_efer & EFER_SVME));
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index bf90e22..eea6330 100644
@@ -1666,6 +1666,12 @@ static void vmx_update_guest_efer(struct vcpu *v)
          * not tolerate the LME and LMA settings being different.  As writes
          * to CR0 are intercepted, it is safe to leave LME clear at this

xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_guest_efer(struct vcpu *v) · line 1677

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
          * point, and fix up both LME and LMA when CR0.PG is set.
+         *
+         * Furthermore, when using shadow pagetables (subsumed by the
+         * Unrestricted Guest check), CR0.PG is a Xen-owned bit, and remains
+         * set even when the guest has logically disabled paging.  LMA was
+         * calculated using the guest CR0.PG setting, but LME needs clearing
+         * to avoid interacting with Xen's CR0.PG setting.
          */
         if ( !(guest_efer & EFER_LMA) )
             guest_efer &= ~EFER_LME;

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

Reviewed-by
#6 lore raw

Re: Ping AMD maintainers? [PATCH] x86/svm: Fix svm_update_guest_efer() for domains using shadow paging

Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Andrew Cooper

When using shadow paging, EFER.NX is a Xen controlled bit, and is required by the shadow pagefault handler to distinguish instruction fetches from data accesses.

This can be observed by a guest which has NX and SMEP clear but SMAP active by attempting to execute code on a user mapping. The first attempt to build the target shadow will #PF so is handled by the shadow code, but when walking the the guest pagetables, the lack of PFEC_insn_fetch being signalled causes the shadow code to mistake the instruction fetch for a data fetch, and believe that it is a real guest fault. As a result, the guest receives #PF[-d-srP] for an action which should complete successfully.

The suspicious-looking gymnastics with LME is actually a subtle corner case with shadow paging. When dropping out of Long Mode, a guests choice of LME and Xen's choice of CR0.PG cause hardware to operate in Long Mode, but the shadow code to operate in 2-on-3 mode.

Quoted 5 lines
Quoted 5 lines

In addition to describing this corner case in the SVM side, extend the comment for the same fix on the VT-x side. (I have a suspicion that I've just worked out why VT-x doesn't tolerate LMA != LME when Unrestricted Guest is clear.)

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