x86/svm: Intercept CR0 writes selectively
PATCH 1/1 x86/svm: Intercept CR0 writes selectively
Xen does not need to track when the TS or MP bits change so opt to intercept CR0 writes selectively. Aside from potentially reducing a few VMEXITs, this fixes a nested virt bug where L1 intercepts CR0_SEL_WRITE and L0 intercepts CR0_WRITE. The hardware prioritizes CR0_WRITE and so L1 never sees any CR0 writes.
Since CR0 may now change behind Xen's back, sync it on VMEXIT so that the emulator sees the correct value.
--- xen/arch/x86/hvm/svm/svm.c | 7 +++++-- xen/arch/x86/hvm/svm/vmcb.c | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/svm/svm.c +5 -2
@@ -1640,7 +1640,8 @@ static void svm_vmexit_do_cr_access( {int gp, cr, dir, rc; - cr = vmcb->exitcode -VMEXIT_CR0_READ ; + cr = (vmcb->exitcode ==VMEXIT_CR0_SEL_WRITE ) + ?16 : (vmcb->exitcode -VMEXIT_CR0_READ ); dir = (cr >15 ); cr &=0xf ; gp = vmcb->ei.mov_cr.gpr;
@@ -2517,6 +2518,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[0 ] =vmcb_get_cr0 (vmcb); 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);
@@ -2882,7 +2884,8 @@ void asmlinkage svm_vmexit_handler(void)break ; case VMEXIT_CR0_READ ... VMEXIT_CR15_READ: - case VMEXIT_CR0_WRITE ... VMEXIT_CR15_WRITE: + case VMEXIT_CR1_WRITE ... VMEXIT_CR15_WRITE: +case VMEXIT_CR0_SEL_WRITE :if ( cpu_has_svm_decode && vmcb->ei.mov_cr.mov_insn )svm_vmexit_do_cr_access (vmcb, regs);else if ( !hvm_emulate_one_insn (x86_insn_is_cr_access,"CR access" ) )
xen/arch/x86/hvm/svm/vmcb.c +5 -3
@@ -56,7 +56,7 @@ static int construct_vmcb(struct vcpu *v)GENERAL1_INTERCEPT_HLT |GENERAL1_INTERCEPT_INVLPG |GENERAL1_INTERCEPT_INVLPGA |GENERAL1_INTERCEPT_IOIO_PROT |GENERAL1_INTERCEPT_MSR_PROT |GENERAL1_INTERCEPT_SHUTDOWN_EVT | -GENERAL1_INTERCEPT_TASK_SWITCH ; +GENERAL1_INTERCEPT_TASK_SWITCH |GENERAL1_INTERCEPT_CR0_SEL_WRITE ; vmcb->_general2_intercepts =GENERAL2_INTERCEPT_VMRUN |GENERAL2_INTERCEPT_VMMCALL | GENERAL2_INTERCEPT_VMLOAD | GENERAL2_INTERCEPT_VMSAVE |
@@ -76,11 +76,13 @@ static int construct_vmcb(struct vcpu *v)/* Intercept all debug-register writes. */ vmcb->_dr_intercepts = ~0u ; -/* Intercept all control-register accesses except for CR2 and CR8. */ +/* Intercept all control-register accesses except for CR2, CR8 and +* CR0 (covered by selective write). */ vmcb->_cr_intercepts = ~(CR_INTERCEPT_CR2_READ |CR_INTERCEPT_CR2_WRITE |CR_INTERCEPT_CR8_READ | -CR_INTERCEPT_CR8_WRITE ); +CR_INTERCEPT_CR8_WRITE | +CR_INTERCEPT_CR0_WRITE ); svm->vmcb_sync_state = vmcb_needs_vmload;
Signature · 1 line
2.53.0
Quoted 2 lines · Ross Lagerwall
Xen does not need to track when the TS or MP bits change so opt to intercept CR0 writes selectively.
Not anymore, which may want expressing here (or else it looks as if this would have been possible from the start).
Quoted 4 lines
Aside from potentially reducing a few
VMEXITs, this fixes a nested virt bug where L1 interceptsCR0_SEL_WRITEand L0 interceptsCR0_WRITE. The hardware prioritizesCR0_WRITEand so L1 never sees any CR0 writes.
Yet if L1 sets CR0_WRITE, since intercept masks are ORed together (if I'm not mistaken), ...
xen/arch/x86/hvm/svm/svm.c void asmlinkage svm_vmexit_handler(void) · line 2891
Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +2
-1@@ -2882,7 +2884,8 @@ void asmlinkage svm_vmexit_handler(void)break ; case VMEXIT_CR0_READ ... VMEXIT_CR15_READ: - case VMEXIT_CR0_WRITE ... VMEXIT_CR15_WRITE: + case VMEXIT_CR1_WRITE ... VMEXIT_CR15_WRITE: +case VMEXIT_CR0_SEL_WRITE :if ( cpu_has_svm_decode && vmcb->ei.mov_cr.mov_insn )svm_vmexit_do_cr_access (vmcb, regs);else if ( !hvm_emulate_one_insn (x86_insn_is_cr_access,"CR access" ) )
... we may still see VMEXIT_CR0_WRITE here (i.e. its handling cannot be removed).
xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 59
Quoted 8 lines
xen/arch/x86/hvm/svm/vmcb.c +1
-1@@ -56,7 +56,7 @@ static int construct_vmcb(struct vcpu *v)GENERAL1_INTERCEPT_HLT |GENERAL1_INTERCEPT_INVLPG |GENERAL1_INTERCEPT_INVLPGA |GENERAL1_INTERCEPT_IOIO_PROT |GENERAL1_INTERCEPT_MSR_PROT |GENERAL1_INTERCEPT_SHUTDOWN_EVT | -GENERAL1_INTERCEPT_TASK_SWITCH ; +GENERAL1_INTERCEPT_TASK_SWITCH |GENERAL1_INTERCEPT_CR0_SEL_WRITE ;
I understand GENERAL1_INTERCEPT_SHUTDOWN_EVT is an existing outlier here, but can we please not add more? This expression is sorted by bit position, with said exception. (Yes, that'll be more churn, but I think that's acceptable here.)
xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 85
Quoted 13 lines
xen/arch/x86/hvm/svm/vmcb.c +4
-2@@ -76,11 +76,13 @@ static int construct_vmcb(struct vcpu *v)/* Intercept all debug-register writes. */ vmcb->_dr_intercepts = ~0u ; -/* Intercept all control-register accesses except for CR2 and CR8. */ +/* Intercept all control-register accesses except for CR2, CR8 and +* CR0 (covered by selective write). */ vmcb->_cr_intercepts = ~(CR_INTERCEPT_CR2_READ |CR_INTERCEPT_CR2_WRITE |CR_INTERCEPT_CR8_READ | -CR_INTERCEPT_CR8_WRITE ); +CR_INTERCEPT_CR8_WRITE | +CR_INTERCEPT_CR0_WRITE );
In both comment and code I think it would be nice if numeric sorting was retained. In the comment you also want to mirror what the code does (it only excludes writes, not reads). Finally - nit: Comment style.
Jan
Quoted 18 lines · Jan Beulich
Quoted 2 lines · Ross Lagerwall
Xen does not need to track when the TS or MP bits change so opt to intercept CR0 writes selectively.
Not anymore, which may want expressing here (or else it looks as if this would have been possible from the start).
Quoted 4 lines
Aside from potentially reducing a few
VMEXITs, this fixes a nested virt bug where L1 interceptsCR0_SEL_WRITEand L0 interceptsCR0_WRITE. The hardware prioritizesCR0_WRITEand so L1 never sees any CR0 writes.Yet if L1 sets
CR0_WRITE, since intercept masks are ORed together (if I'm not mistaken), ...Quoted 3 lines
(quoted diff) +0
-0@@ -2882,7 +2884,8 @@ void asmlinkage svm_vmexit_handler(void) break;
Quoted 11 lines
xen/arch/x86/hvm/svm/svm.c void asmlinkage svm_vmexit_handler(void) · line 2891
Quoted 7 lines
xen/arch/x86/hvm/svm/svm.c +2
-1case VMEXIT_CR0_READ ... VMEXIT_CR15_READ: - case VMEXIT_CR0_WRITE ... VMEXIT_CR15_WRITE: + case VMEXIT_CR1_WRITE ... VMEXIT_CR15_WRITE: +case VMEXIT_CR0_SEL_WRITE :if ( cpu_has_svm_decode && vmcb->ei.mov_cr.mov_insn )svm_vmexit_do_cr_access (vmcb, regs);else if ( !hvm_emulate_one_insn (x86_insn_is_cr_access,"CR access" ) )... we may still see
VMEXIT_CR0_WRITEhere (i.e. its handling cannot be removed).
No, it shouldn't get here in that case. If L1 sets CR0_WRITE, it will enter nestedsvm_check_intercepts() and then hit the NESTEDHVM_VMEXIT_INJECT case.
Ross
Quoted 30 lines · Ross Lagerwall
Quoted 18 lines · Jan Beulich
Quoted 2 lines · Ross Lagerwall
Xen does not need to track when the TS or MP bits change so opt to intercept CR0 writes selectively.
Not anymore, which may want expressing here (or else it looks as if this would have been possible from the start).
Quoted 4 lines
Aside from potentially reducing a few
VMEXITs, this fixes a nested virt bug where L1 interceptsCR0_SEL_WRITEand L0 interceptsCR0_WRITE. The hardware prioritizesCR0_WRITEand so L1 never sees any CR0 writes.Yet if L1 sets
CR0_WRITE, since intercept masks are ORed together (if I'm not mistaken), ...Quoted 3 lines
(quoted diff) +0
-0@@ -2882,7 +2884,8 @@ void asmlinkage svm_vmexit_handler(void) break;Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c void asmlinkage svm_vmexit_handler(void) · line 2891
Quoted 7 lines
xen/arch/x86/hvm/svm/svm.c +2
-1case VMEXIT_CR0_READ ... VMEXIT_CR15_READ: - case VMEXIT_CR0_WRITE ... VMEXIT_CR15_WRITE: + case VMEXIT_CR1_WRITE ... VMEXIT_CR15_WRITE: +case VMEXIT_CR0_SEL_WRITE :if ( cpu_has_svm_decode && vmcb->ei.mov_cr.mov_insn )svm_vmexit_do_cr_access (vmcb, regs);else if ( !hvm_emulate_one_insn (x86_insn_is_cr_access,"CR access" ) )... we may still see
VMEXIT_CR0_WRITEhere (i.e. its handling cannot be removed).
Quoted 2 lines
No, it shouldn't get here in that case. If L1 sets
CR0_WRITE, it will enternestedsvm_check_intercepts()and then hit theNESTEDHVM_VMEXIT_INJECTcase.
Hmm, okay. Yet then I still think the original case label shouldn't be removed, as we don't exclude the CR2 or CR8 cases either.
Jan
Quoted 6 lines · Jan Beulich
Quoted 4 lines
Aside from potentially reducing a few
VMEXITs, this fixes a nested virt bug where L1 interceptsCR0_SEL_WRITEand L0 interceptsCR0_WRITE. The hardware prioritizesCR0_WRITEand so L1 never sees any CR0 writes.Yet if L1 sets
CR0_WRITE, since intercept masks are ORed together (if I'm not mistaken),
L0's choice of intercepts needs combining with L1's choice of intercepts.
Mostly this is OR-ing/unioning, but not exclusively, just like levelling for migrate isn't a straight interception.
~Andrew
Quoted 11 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
Quoted 4 lines
Aside from potentially reducing a few
VMEXITs, this fixes a nested virt bug where L1 interceptsCR0_SEL_WRITEand L0 interceptsCR0_WRITE. The hardware prioritizesCR0_WRITEand so L1 never sees any CR0 writes.Yet if L1 sets
CR0_WRITE, since intercept masks are ORed together (if I'm not mistaken),L0's choice of intercepts needs combining with L1's choice of intercepts.
Mostly this is OR-ing/unioning, but not exclusively, just like levelling for migrate isn't a straight interception.
Sorry, intersection.
~Andrew