Skip to contentSkip to conversation

nestedsvm: Ignore attempts to clear EFER.SVME from L2

PATCH In review Ross Lagerwall 3 participants 4 messages Nov 13 – Nov 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nestedsvm: Ignore attempts to clear EFER.SVME from L2

The APM leaves it undefined what happens when the VMM doesn't intercept writes to EFER and EFER.SVME is cleared. The current implementation in Xen resets the nested state without doing a VMEXIT which is unexpected and may potentially hit a BUG or assertion failure.

Therefore, implement the same behaviour as the APM documents for SEV-ES guests; ignore the attempt to clear it.

---

Another option here could be to fail VMRUN if L1 does not intercept writes to EFER. However, this wouldn't match the behaviour of real processors and may cause incomplete toy hypervisors to unexpectedly fail.

 xen/arch/x86/hvm/hvm.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hvm.c +9 -3
index 71da15c0a4ed..f4155f523598 100644
@@ -2156,9 +2156,15 @@ int hvm_set_efer(uint64_t value)
        ((value & EFER_SVME) == 0 ) &&
        ((value ^ v->arch.hvm.guest_efer) & EFER_SVME) )
     {
-        /* Cleared EFER.SVME: Flush all nestedp2m tables */
-        p2m_flush_nestedp2m(v->domain);
-        nestedhvm_vcpu_reset(v);
+        if ( nestedhvm_vcpu_in_guestmode(v) )
+            /* Ignore attempts to clear EFER.SVME from L2 */
+            value |= EFER_SVME;
+        else
+        {
+            /* Cleared EFER.SVME: Flush all nestedp2m tables */
+            p2m_flush_nestedp2m(v->domain);
+            nestedhvm_vcpu_reset(v);
+        }
     }
 
     value |= v->arch.hvm.guest_efer & EFER_LMA;
Signature · 1 line
2.56.0
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Ross Lagerwall

The APM leaves it undefined what happens when the VMM doesn't intercept writes to EFER and EFER.SVME is cleared. The current implementation in Xen resets the nested state without doing a VMEXIT which is unexpected and may potentially hit a BUG or assertion failure.

Therefore, implement the same behaviour as the APM documents for SEV-ES guests; ignore the attempt to clear it.

Quoted 6 lines

---

Another option here could be to fail VMRUN if L1 does not intercept writes to EFER. However, this wouldn't match the behaviour of real processors and may cause incomplete toy hypervisors to unexpectedly fail.

+1

xen/arch/x86/hvm/hvm.c int hvm_set_efer(uint64_t value) · line 2157

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -2156,9 +2156,15 @@ int hvm_set_efer(uint64_t value)
        ((value & EFER_SVME) == 0 ) &&
        ((value ^ v->arch.hvm.guest_efer) & EFER_SVME) )

Unrelated to your change, but: What is this? When we have checked that SVME is clear in value, why would value need XORing with what's currently in effect to figure whether a 1->0 transition is occurring?

Jan

Quoted 16 lines
xen/arch/x86/hvm/hvm.c +9 -3
     {
-        /* Cleared EFER.SVME: Flush all nestedp2m tables */
-        p2m_flush_nestedp2m(v->domain);
-        nestedhvm_vcpu_reset(v);
+        if ( nestedhvm_vcpu_in_guestmode(v) )
+            /* Ignore attempts to clear EFER.SVME from L2 */
+            value |= EFER_SVME;
+        else
+        {
+            /* Cleared EFER.SVME: Flush all nestedp2m tables */
+            p2m_flush_nestedp2m(v->domain);
+            nestedhvm_vcpu_reset(v);
+        }
     }
 
     value |= v->arch.hvm.guest_efer & EFER_LMA;
#2 lore raw
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Ross Lagerwall

The APM leaves it undefined what happens when the VMM doesn't intercept writes to EFER and EFER.SVME is cleared. The current implementation in Xen resets the nested state without doing a VMEXIT which is unexpected and may potentially hit a BUG or assertion failure.

Quoted 7 lines
Quoted 4 lines

Therefore, implement the same behaviour as the APM documents for SEV-ES guests; ignore the attempt to clear it.

Thanks

Quoted 9 lines
Quoted 6 lines

---

Another option here could be to fail VMRUN if L1 does not intercept writes to EFER. However, this wouldn't match the behaviour of real processors and may cause incomplete toy hypervisors to unexpectedly fail.

+1

Quoted 10 lines

xen/arch/x86/hvm/hvm.c int hvm_set_efer(uint64_t value) · line 2157

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -2156,9 +2156,15 @@ int hvm_set_efer(uint64_t value)
         ((value & EFER_SVME) == 0 ) &&
         ((value ^ v->arch.hvm.guest_efer) & EFER_SVME) )

Unrelated to your change, but: What is this? When we have checked that SVME is clear in value, why would value need XORing with what's currently in effect to figure whether a 1->0 transition is occurring?

I agree that it is unnecessary. Looks like it has been that way since the code was added.

Ross

#3 lore raw
In reply to Ross Lagerwall
Quoted 7 lines · Ross Lagerwall

The APM leaves it undefined what happens when the VMM doesn't intercept writes to EFER and EFER.SVME is cleared. The current implementation in Xen resets the nested state without doing a VMEXIT which is unexpected and may potentially hit a BUG or assertion failure.

Therefore, implement the same behaviour as the APM documents for SEV-ES guests; ignore the attempt to clear it.

I agree this is the right course of action.  It's also what KVM does as well IIRC.

Quoted 25 lines

---

Another option here could be to fail VMRUN if L1 does not intercept writes to EFER. However, this wouldn't match the behaviour of real processors and may cause incomplete toy hypervisors to unexpectedly fail.

 xen/arch/x86/hvm/hvm.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -3
index 71da15c0a4ed..f4155f523598 100644
@@ -2156,9 +2156,15 @@ int hvm_set_efer(uint64_t value)
        ((value & EFER_SVME) == 0 ) &&
        ((value ^ v->arch.hvm.guest_efer) & EFER_SVME) )
     {
-        /* Cleared EFER.SVME: Flush all nestedp2m tables */
-        p2m_flush_nestedp2m(v->domain);
-        nestedhvm_vcpu_reset(v);
+        if ( nestedhvm_vcpu_in_guestmode(v) )
+            /* Ignore attempts to clear EFER.SVME from L2 */

xen/arch/x86/hvm/hvm.c int hvm_set_efer(uint64_t value) · line 2170

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +7 -0
+            value |= EFER_SVME;
+        else
+        {
+            /* Cleared EFER.SVME: Flush all nestedp2m tables */
+            p2m_flush_nestedp2m(v->domain);
+            nestedhvm_vcpu_reset(v);
+        }
     }
 
     value |= v->arch.hvm.guest_efer & EFER_LMA;

I think this is ok, given the state of the codebase, so Reviewed-by:
Andrew Cooper <[email protected]>

However, this is bad logic.  nestedhvm_vcpu_reset() ultimately sets GIF=1 and this is almost certainly not what happens in real hardware. It also zeroes a lot of state which are by definition 0 in root mode.

It's also nasty having SVM specifics escape into common code, but it's necessary as we're choosing to ignore in a guest-visible way.  We don't want to have two areas of code trying to decide what the eventual EFER should be.

We'll need to fix all of this due course.

~Andrew