x86/nestedsvm: TPR bugfixes
cover x86/nestedsvm: TPR bugfixes
Hi,
This series fixes a couple of bugs with TPR handling of nested guests.
Thanks,
Ross
Ross Lagerwall (2):
x86/nestedsvm: Handle CR8 accesses with V_INTR_MASKING=0 correctly x86/nestedsvm: Handle L2 writing to the L1 APIC TPR
xen/arch/x86/hvm/emulate.c | 1 + xen/arch/x86/hvm/hvm.c | 1 + xen/arch/x86/hvm/svm/nestedsvm.c | 3 +++ xen/arch/x86/hvm/svm/svm.c | 36 +++++++++++++++++++------------- xen/arch/x86/hvm/vmx/vmx.c | 4 ++++ 5 files changed, 31 insertions(+), 14 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/2 x86/nestedsvm: Handle CR8 accesses with V_INTR_MASKING=0 correctly
When V_INTR_MASKING=0, reads return the value in the APIC TPR and writes update the value in the APIC TPR and the V_TPR.
To implement this for Nested SVM, intercept reads and writes of CR8 when V_INTR_MASKING=0. For reads, simply return the value in Xen's emulated APIC. For writes, update the value in the emulated APIC as well as VMCB(0-2)'s V_TPR.
--- xen/arch/x86/hvm/emulate.c | 1 + xen/arch/x86/hvm/hvm.c | 1 + xen/arch/x86/hvm/svm/nestedsvm.c | 3 +++ xen/arch/x86/hvm/svm/svm.c | 30 ++++++++++++++++++------------ xen/arch/x86/hvm/vmx/vmx.c | 4 ++++ 5 files changed, 27 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -0
@@ -2376,6 +2376,7 @@ static int cf_check hvmemul_write_cr( }vlapic_set_reg (vcpu_vlapic (curr),APIC_TASKPRI , val <<4 ); +hvm_update_guest_cr (curr,8 ); rc =X86EMUL_OKAY ;break ;
xen/arch/x86/hvm/hvm.c +1 -0
@@ -2205,6 +2205,7 @@ int hvm_mov_to_cr(unsigned int cr, unsigned int gpr)case 8 :vlapic_set_reg (vcpu_vlapic (curr),APIC_TASKPRI , ((val &0x0f ) <<4 )); +hvm_update_guest_cr (curr, cr); rc =X86EMUL_OKAY ;break ;
xen/arch/x86/hvm/svm/nestedsvm.c +3 -0
@@ -394,6 +394,9 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs) n2vmcb->_cr_intercepts = n1vmcb->_cr_intercepts | ns_vmcb->_cr_intercepts; +if ( !svm->ns_hostflags.vintrmask ) + n2vmcb->_cr_intercepts |= +CR_INTERCEPT_CR8_READ |CR_INTERCEPT_CR8_WRITE ; n2vmcb->_dr_intercepts = n1vmcb->_dr_intercepts | ns_vmcb->_dr_intercepts; n2vmcb->_exception_intercepts =
xen/arch/x86/hvm/svm/svm.c +18 -12
@@ -104,6 +104,15 @@ static void cf_check svm_cpu_down(void)write_efer (read_efer () & ~EFER_SVME ); } +static void svm_update_tpr (struct vmcb *vmcb,struct vlapic *vlapic) +{ +/* Reflect the vlapic's TPR in the hardware vtpr */ +vintr_t intr =vmcb_get_vintr (vmcb); + + intr.tpr = (vlapic_get_reg (vlapic,APIC_TASKPRI ) &0xFF ) >>4 ; +vmcb_set_vintr (vmcb, intr); +} + static void cf_check svm_update_guest_cr(struct vcpu *v,unsigned int cr,unsigned int flags) {
@@ -170,6 +179,13 @@ static void cf_check svm_update_guest_cr(vmcb_set_cr4 (vmcb, value);break ; + +case 8 : +if (nestedhvm_vcpu_in_guestmode (v) && + !vcpu_nestedsvm (v).ns_hostflags.vintrmask ) +svm_update_tpr (vmcb,vcpu_vlapic (v)); +break ; +default :BUG (); }
@@ -959,14 +975,7 @@ static void noreturn cf_check svm_do_resume(void) }if ( !vcpu_guestmode && !vlapic_hw_disabled (vlapic) ) - { -vintr_t intr; - -/* Reflect the vlapic's TPR in the hardware vtpr */ - intr =vmcb_get_vintr (vmcb); - intr.tpr = (vlapic_get_reg (vlapic,APIC_TASKPRI ) &0xFF ) >>4 ; -vmcb_set_vintr (vmcb, intr); - } +svm_update_tpr (vmcb, vlapic);hvm_do_resume (v);
@@ -3069,10 +3078,7 @@ void asmlinkage svm_vmexit_handler(void)if ( vcpu_guestmode ||vlapic_hw_disabled (vlapic) )return ; -/* The exit may have updated the TPR: reflect this in the hardware vtpr */ - intr =vmcb_get_vintr (vmcb); - intr.tpr = (vlapic_get_reg (vlapic,APIC_TASKPRI ) &0xFF ) >>4 ; -vmcb_set_vintr (vmcb, intr); +svm_update_tpr (vmcb, vlapic); }/*
xen/arch/x86/hvm/vmx/vmx.c +4 -0
@@ -1874,6 +1874,10 @@ static void cf_check vmx_update_guest_cr(hvm_asid_flush_vcpu (v);break ; +case 8 : +/* Nothing to do */ +break ; +default :BUG (); }
Signature · 1 line
2.56.0
PATCH 2/2 x86/nestedsvm: Handle L2 writing to the L1 APIC TPR
In reply to Ross LagerwallL1 may allow L2 to write to its APIC TPR. Handle these changes by always syncing the APIC TPR to VMCB(0-1) rather than skipping the sync if in guest mode. This ensures that subsequent L1 accesses to CR8 see the correct value.
--- xen/arch/x86/hvm/svm/svm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/svm/svm.c +6 -4
@@ -944,6 +944,7 @@ static void noreturn cf_check svm_do_resume(void) {struct vcpu *v = current;struct vmcb *vmcb = v->arch.hvm.svm.vmcb; +struct vmcb *n1_vmcb =vcpu_nestedhvm (v).nv_n1vmcx;bool debug_state = (v->domain->debugger_attached || v->domain->arch.monitor.software_breakpoint_enabled || v->domain->arch.monitor.debug_exception_enabled);
@@ -974,8 +975,8 @@ static void noreturn cf_check svm_do_resume(void) vmcb->cleanbits.raw =0 ; } -if ( !vcpu_guestmode && !vlapic_hw_disabled (vlapic) ) -svm_update_tpr (vmcb, vlapic); +if ( !vlapic_hw_disabled (vlapic) ) +svm_update_tpr (n1_vmcb, vlapic);hvm_do_resume (v);
@@ -2521,6 +2522,7 @@ void asmlinkage svm_vmexit_handler(void)uint64_t exit_reason;struct vcpu *v = current;struct vmcb *vmcb = v->arch.hvm.svm.vmcb; +struct vmcb *n1_vmcb =vcpu_nestedhvm (v).nv_n1vmcx;int insn_len;vintr_t intr;bool vcpu_guestmode = false;
@@ -3075,10 +3077,10 @@ void asmlinkage svm_vmexit_handler(void) } out: -if ( vcpu_guestmode ||vlapic_hw_disabled (vlapic) ) +if (vlapic_hw_disabled (vlapic) )return ; -svm_update_tpr (vmcb, vlapic); +svm_update_tpr (n1_vmcb, vlapic); }/*
Signature · 1 line
2.56.0
Re: [PATCH v1 2/2] x86/nestedsvm: Handle L2 writing to the L1 APIC TPR
Quoted 44 lines · Ross Lagerwall
L1 may allow L2 to write to its APIC TPR. Handle these changes by always syncing the APIC TPR to VMCB(0-1) rather than skipping the sync if in guest mode. This ensures that subsequent L1 accesses to CR8 see the correct value.
Signed-off-by Ross Lagerwall <[email protected]>--- xen/arch/x86/hvm/svm/svm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)xen/arch/x86/hvm/svm/svm.c +4
-2@@ -944,6 +944,7 @@ static void noreturn cf_check svm_do_resume(void) {struct vcpu *v = current;struct vmcb *vmcb = v->arch.hvm.svm.vmcb; +struct vmcb *n1_vmcb =vcpu_nestedhvm (v).nv_n1vmcx;bool debug_state = (v->domain->debugger_attached || v->domain->arch.monitor.software_breakpoint_enabled || v->domain->arch.monitor.debug_exception_enabled);@@ -974,8 +975,8 @@ static void noreturn cf_check svm_do_resume(void) vmcb->cleanbits.raw =0 ; } -if ( !vcpu_guestmode && !vlapic_hw_disabled (vlapic) ) -svm_update_tpr (vmcb, vlapic); +if ( !vlapic_hw_disabled (vlapic) ) +svm_update_tpr (n1_vmcb, vlapic);hvm_do_resume (v);@@ -2521,6 +2522,7 @@ void asmlinkage svm_vmexit_handler(void)uint64_t exit_reason;struct vcpu *v = current;struct vmcb *vmcb = v->arch.hvm.svm.vmcb; +struct vmcb *n1_vmcb =vcpu_nestedhvm (v).nv_n1vmcx;int insn_len;vintr_t intr;bool vcpu_guestmode = false;@@ -3075,10 +3077,10 @@ void asmlinkage svm_vmexit_handler(void) }
xen/arch/x86/hvm/svm/svm.c void asmlinkage svm_vmexit_handler(void) · line 3086
Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +2
-2out: -if ( vcpu_guestmode ||vlapic_hw_disabled (vlapic) ) +if (vlapic_hw_disabled (vlapic) )return ; -svm_update_tpr (vmcb, vlapic); +svm_update_tpr (n1_vmcb, vlapic); }/*
Both of these paths only exist because we're missing correct state updates for writing to cr8 or APIC_TPR in the first place.
Please can we fix that problem, rather than making changes we're just going to need to delete anyway.
~Andrew