Skip to contentSkip to conversation

x86/vvmx: set CR4 before CR0

PATCH In review Andrew Cooper 3 participants 4 messages Jun 26, 2019 – Jun 28, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vvmx: set CR4 before CR0

From: Sergey Dyasli <[email protected]>

Otherwise hvm_set_cr0() will check the wrong CR4 bits (L1 instead of L2 and vice-versa).

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

I found this patch languishing in the XenServer patchqueue, and Sergey is OoO so I'm submitting it on his behalf.

Without this change, nested virt is broken when L1 and L2 differ in their use of PCID.

This is only a stopgap solution - it resolves the PCID issue without introducing other issues, but the proper fix needs to consider all control bits at once, rather than considering a vmentry/exit as a sequence of changes of discrete registers.

---
 xen/arch/x86/hvm/vmx/vvmx.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +4 -4
index 7bca572d88..332623d006 100644
@@ -1024,11 +1024,11 @@ static void load_shadow_guest_state(struct vcpu *v)
     nvcpu->guest_cr[0] = get_vvmcs(v, CR0_READ_SHADOW);
     nvcpu->guest_cr[4] = get_vvmcs(v, CR4_READ_SHADOW);
 
-    rc = hvm_set_cr0(get_vvmcs(v, GUEST_CR0), true);
+    rc = hvm_set_cr4(get_vvmcs(v, GUEST_CR4), true);
     if ( rc == X86EMUL_EXCEPTION )
         hvm_inject_hw_exception(TRAP_gp_fault, 0);
 
-    rc = hvm_set_cr4(get_vvmcs(v, GUEST_CR4), true);
+    rc = hvm_set_cr0(get_vvmcs(v, GUEST_CR0), true);
     if ( rc == X86EMUL_EXCEPTION )
         hvm_inject_hw_exception(TRAP_gp_fault, 0);
 
@@ -1238,11 +1238,11 @@ static void load_vvmcs_host_state(struct vcpu *v)
         __vmwrite(vmcs_h2g_field[i].guest_field, r);
     }
 
-    rc = hvm_set_cr0(get_vvmcs(v, HOST_CR0), true);
+    rc = hvm_set_cr4(get_vvmcs(v, HOST_CR4), true);
     if ( rc == X86EMUL_EXCEPTION )
         hvm_inject_hw_exception(TRAP_gp_fault, 0);
 
-    rc = hvm_set_cr4(get_vvmcs(v, HOST_CR4), true);
+    rc = hvm_set_cr0(get_vvmcs(v, HOST_CR0), true);
     if ( rc == X86EMUL_EXCEPTION )
         hvm_inject_hw_exception(TRAP_gp_fault, 0);
 
Signature · 7 lines
2.11.0


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

From: Sergey Dyasli <[email protected]>

Otherwise hvm_set_cr0() will check the wrong CR4 bits (L1 instead of L2 and vice-versa).

Quoted 10 lines

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

I found this patch languishing in the XenServer patchqueue, and Sergey is OoO so I'm submitting it on his behalf.

Quoted 7 lines

Without this change, nested virt is broken when L1 and L2 differ in their use of PCID.

This is only a stopgap solution - it resolves the PCID issue without introducing other issues, but the proper fix needs to consider all control bits at once, rather than considering a vmentry/exit as a sequence of changes of discrete registers.

The current approach seems prone to such ordering issues, and I don't see a way to make it more robust while keeping the current approach, so I guess setting all the registers state and then evaluating them would make more sense and prevent this kind of mistakes.

Thanks.

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

#2 lore raw
Quoted 23 lines · Roger Pau Monné
Quoted 7 lines · Andrew Cooper

From: Sergey Dyasli <[email protected]>

Otherwise hvm_set_cr0() will check the wrong CR4 bits (L1 instead of L2 and vice-versa).

Quoted 13 lines

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

I found this patch languishing in the XenServer patchqueue, and Sergey is OoO so I'm submitting it on his behalf.

Without this change, nested virt is broken when L1 and L2 differ in their use of PCID.

Quoted 8 lines
Quoted 4 lines

This is only a stopgap solution - it resolves the PCID issue without introducing other issues, but the proper fix needs to consider all control bits at once, rather than considering a vmentry/exit as a sequence of changes of discrete registers.

The current approach seems prone to such ordering issues, and I don't see a way to make it more robust while keeping the current approach, so I guess setting all the registers state and then evaluating them would make more sense and prevent this kind of mistakes.

I'm pretty sure that when we start doing all the checks that we should be doing, there will be combinations which can't be expressed as a non-faulting sequence of writes to cr0, cr4 and efer.

Unfortunately, there is a load of nested virt prep work to do before implementing an approach like this becomes viable.

Hence the stopgap solution in the meantime.

~Andrew

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

Acked-by on v1 1/1
#3 lore raw
In reply to Andrew Cooper
Quoted 10 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Thursday, June 27, 2019 3:02 AM

From: Sergey Dyasli <[email protected]>

Otherwise hvm_set_cr0() will check the wrong CR4 bits (L1 instead of L2 and vice-versa).

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