Skip to contentSkip to conversation

x86/svm: Always sync guest CR2 on VMExit

PATCH In review Andrew Cooper 4 participants 7 messages May 1 – May 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/svm: Always sync guest CR2 on VMExit

Under SVM, there are two copies of guest CR2. One is v->arch.hvm.guest_cr[2] and one is in the VMCB.

Xen doesn't intercept CR2 accesses, so this mostly goes unnoticed; hardware loads and saves the guest CR2 across VMRUN/VMExit.

For HAP guests (where #PF is not intercepted, and therefore we don't typically inject #PF either), this causes the guest CR2 value to be lost on migrate. As migration is cooperative and not done from the #PF handler, this also goes unoticed by guests.

It also means that an emulated MOV-from-CR2 reads a stale value.

Fixes d1bd157fbc9b ("Big merge the HVM full-virtualisation abstractions.")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Stefano Stabellini <[email protected]>

It also also works around the QEMU bug that triggered the investigion, where the CR2 intercepts trigger despite Xen requesting CR2 not to be intercepted.

---
 xen/arch/x86/hvm/svm/svm.c | 1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/hvm/svm/svm.c +1 -0
index ced616684732..f49d2ebbfdd5 100644
@@ -2505,6 +2505,7 @@ void asmlinkage svm_vmexit_handler(void)
     hvm_sanitize_regs_fields(
         regs, !(vmcb_get_efer(vmcb) & EFER_LMA) || !(vmcb->cs.l));
 
+    v->arch.hvm.guest_cr[2] = vmcb_get_cr2(vmcb);
     if ( paging_mode_hap(v->domain) )
         v->arch.hvm.guest_cr[3] = v->arch.hvm.hw_cr[3] = vmcb_get_cr3(vmcb);
 

base-commit: 61f957d48c78df6c5254b6f54d6170d3bd3d717e

Signature · 1 line
2.39.5
Tested-by on v1 1/1
#1 lore raw
Quoted 11 lines · Andrew Cooper

Under SVM, there are two copies of guest CR2. One is v->arch.hvm.guest_cr[2] and one is in the VMCB.

Xen doesn't intercept CR2 accesses, so this mostly goes unnoticed; hardware loads and saves the guest CR2 across VMRUN/VMExit.

For HAP guests (where #PF is not intercepted, and therefore we don't typically inject #PF either), this causes the guest CR2 value to be lost on migrate. As migration is cooperative and not done from the #PF handler, this also goes unoticed by guests.

Quoted 5 lines

It also means that an emulated MOV-from-CR2 reads a stale value.

Fixes d1bd157fbc9b ("Big merge the HVM full-virtualisation abstractions.")
Quoted 29 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Stefano Stabellini <[email protected]>

It also also works around the QEMU bug that triggered the investigion, where the CR2 intercepts trigger despite Xen requesting CR2 not to be intercepted.

---
 xen/arch/x86/hvm/svm/svm.c | 1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/hvm/svm/svm.c +1 -0
index ced616684732..f49d2ebbfdd5 100644
@@ -2505,6 +2505,7 @@ void asmlinkage svm_vmexit_handler(void)
     hvm_sanitize_regs_fields(
         regs, !(vmcb_get_efer(vmcb) & EFER_LMA) || !(vmcb->cs.l));
 
+    v->arch.hvm.guest_cr[2] = vmcb_get_cr2(vmcb);
     if ( paging_mode_hap(v->domain) )
         v->arch.hvm.guest_cr[3] = v->arch.hvm.hw_cr[3] = vmcb_get_cr3(vmcb);
 

base-commit: 61f957d48c78df6c5254b6f54d6170d3bd3d717e

Signature · 1 line
2.39.5
#2 lore raw
Quoted 12 lines · Stefano Stabellini
Quoted 11 lines · Andrew Cooper

Under SVM, there are two copies of guest CR2. One is v->arch.hvm.guest_cr[2] and one is in the VMCB.

Xen doesn't intercept CR2 accesses, so this mostly goes unnoticed; hardware loads and saves the guest CR2 across VMRUN/VMExit.

For HAP guests (where #PF is not intercepted, and therefore we don't typically inject #PF either), this causes the guest CR2 value to be lost on migrate. As migration is cooperative and not done from the #PF handler, this also goes unoticed by guests.

Quoted 6 lines
Quoted 5 lines

It also means that an emulated MOV-from-CR2 reads a stale value.

Fixes d1bd157fbc9b ("Big merge the HVM full-virtualisation abstractions.")

Thanks, sadly I made the mistake of extending my XTF PoC for this.

There are also bugs on the emulated MOV-to-CR2 side, and they're far harder to untangle.

This patch probably wants to go in in this form, accepting that there are issues still to be addressed on the other side.

~Andrew

Reviewed-by
#3 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Stefano Stabellini
Quoted 16 lines · Andrew Cooper

Under SVM, there are two copies of guest CR2. One is v->arch.hvm.guest_cr[2] and one is in the VMCB.

Xen doesn't intercept CR2 accesses, so this mostly goes unnoticed; hardware loads and saves the guest CR2 across VMRUN/VMExit.

For HAP guests (where #PF is not intercepted, and therefore we don't typically inject #PF either), this causes the guest CR2 value to be lost on migrate. As migration is cooperative and not done from the #PF handler, this also goes unoticed by guests.

It also means that an emulated MOV-from-CR2 reads a stale value.

Fixes d1bd157fbc9b ("Big merge the HVM full-virtualisation abstractions.")
Quoted 6 lines
Quoted 1 line

Thanks, sadly I made the mistake of extending my XTF PoC for this.

There are also bugs on the emulated MOV-to-CR2 side, and they're far harder to untangle.

Any slightly closer details as to what?

Quoted 2 lines

This patch probably wants to go in in this form, accepting that there are issues still to be addressed on the other side.

I agree:

Jan

#4 lore raw
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Stefano Stabellini
Quoted 16 lines · Andrew Cooper

Under SVM, there are two copies of guest CR2. One is v->arch.hvm.guest_cr[2] and one is in the VMCB.

Xen doesn't intercept CR2 accesses, so this mostly goes unnoticed; hardware loads and saves the guest CR2 across VMRUN/VMExit.

For HAP guests (where #PF is not intercepted, and therefore we don't typically inject #PF either), this causes the guest CR2 value to be lost on migrate. As migration is cooperative and not done from the #PF handler, this also goes unoticed by guests.

It also means that an emulated MOV-from-CR2 reads a stale value.

Fixes d1bd157fbc9b ("Big merge the HVM full-virtualisation abstractions.")
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

Thanks, sadly I made the mistake of extending my XTF PoC for this.

There are also bugs on the emulated MOV-to-CR2 side, and they're far harder to untangle.

Any slightly closer details as to what?

hvmemul_write_cr() updates guest_cr[2] but doesn't sync it into the VMCB.  This doesn't show up on Intel because CR2 is switched explicitly in RAX across VMEntry/exit.

But, it's not the only problem path.

svm_vmexit_do_cr_access() is the fasthpath exit for CR intercepts when decode assists are available.  hvm_mov_to_cr() and hvm_mov_from_cr() are asymmetric in their handling of CR2.  mov_from will read from guest_cr[2] but mov_to will domain crash.

However, case 2 ought to be unreachable in hvm_mov_from_cr() because of how we program the intercepts, yet the QEMU bug which caused this to get noticed will trigger an ASSERT() if I were to put one in.

So, do I fix up both to account for the fact we know QEMU is buggy with intercepts?

Quoted 5 lines
Quoted 3 lines

This patch probably wants to go in in this form, accepting that there are issues still to be addressed on the other side.

I agree:

Thanks.

~Andrew

#5 lore raw
Quoted 38 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Stefano Stabellini
Quoted 16 lines · Andrew Cooper

Under SVM, there are two copies of guest CR2. One is v->arch.hvm.guest_cr[2] and one is in the VMCB.

Xen doesn't intercept CR2 accesses, so this mostly goes unnoticed; hardware loads and saves the guest CR2 across VMRUN/VMExit.

For HAP guests (where #PF is not intercepted, and therefore we don't typically inject #PF either), this causes the guest CR2 value to be lost on migrate. As migration is cooperative and not done from the #PF handler, this also goes unoticed by guests.

It also means that an emulated MOV-from-CR2 reads a stale value.

Fixes d1bd157fbc9b ("Big merge the HVM full-virtualisation abstractions.")
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

Thanks, sadly I made the mistake of extending my XTF PoC for this.

There are also bugs on the emulated MOV-to-CR2 side, and they're far harder to untangle.

Any slightly closer details as to what?

hvmemul_write_cr() updates guest_cr[2] but doesn't sync it into the VMCB.  This doesn't show up on Intel because CR2 is switched explicitly in RAX across VMEntry/exit.

But, it's not the only problem path.

svm_vmexit_do_cr_access() is the fasthpath exit for CR intercepts when decode assists are available.  hvm_mov_to_cr() and hvm_mov_from_cr() are asymmetric in their handling of CR2.  mov_from will read from guest_cr[2] but mov_to will domain crash.

Quoted 6 lines

However, case 2 ought to be unreachable in hvm_mov_from_cr() because of how we program the intercepts, yet the QEMU bug which caused this to get noticed will trigger an ASSERT() if I were to put one in.

So, do I fix up both to account for the fact we know QEMU is buggy with intercepts?

I think that's going to be (about) the best we can do.

Jan

Reviewed-by on v1 1/1
#6 lore raw
In reply to Andrew Cooper

Le 01/05/2026 à 23:43, Andrew Cooper a écrit :

Quoted 41 lines

Under SVM, there are two copies of guest CR2. One is v->arch.hvm.guest_cr[2] and one is in the VMCB.

Xen doesn't intercept CR2 accesses, so this mostly goes unnoticed; hardware loads and saves the guest CR2 across VMRUN/VMExit.

For HAP guests (where #PF is not intercepted, and therefore we don't typically inject #PF either), this causes the guest CR2 value to be lost on migrate. As migration is cooperative and not done from the #PF handler, this also goes unoticed by guests.

It also means that an emulated MOV-from-CR2 reads a stale value.

Fixes d1bd157fbc9b ("Big merge the HVM full-virtualisation abstractions.")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Stefano Stabellini <[email protected]>

It also also works around the QEMU bug that triggered the investigion, where the CR2 intercepts trigger despite Xen requesting CR2 not to be intercepted.

---
  xen/arch/x86/hvm/svm/svm.c | 1 +
  1 file changed, 1 insertion(+)
xen/arch/x86/hvm/svm/svm.c +1 -0
index ced616684732..f49d2ebbfdd5 100644
@@ -2505,6 +2505,7 @@ void asmlinkage svm_vmexit_handler(void)
      hvm_sanitize_regs_fields(
          regs, !(vmcb_get_efer(vmcb) & EFER_LMA) || !(vmcb->cs.l));
 
+    v->arch.hvm.guest_cr[2] = vmcb_get_cr2(vmcb);
      if ( paging_mode_hap(v->domain) )
          v->arch.hvm.guest_cr[3] = v->arch.hvm.hw_cr[3] = vmcb_get_cr3(vmcb);
 
Quoted 1 line

base-commit: 61f957d48c78df6c5254b6f54d6170d3bd3d717e

--
| Vates

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech