From: David Woodhouse V_TPR holds only TPR[7:4]. While KVM does not intercept CR8 writes, sync_cr8_to_lapic() pushes V_TPR back into the local APIC after every VMRUN, whether or not the guest touched CR8. A guest that sets the TPR through the APIC page or the x2APIC MSR therefore loses TPR[3:0] at the next exit: it writes 0xff, reads back 0xf0, and interrupts in the priority sub-class it meant to mask are delivered. VMX keeps all eight TPR bits in the virtual-APIC page and does not have this problem. Hardware changes V_TPR only on a guest CR8 write. Record the value sync_lapic_to_cr8() loads on entry and push V_TPR back into the APIC only when it differs, which only a guest CR8 write can make it do. A CR8 write whose value matches the loaded TPR[7:4] is indistinguishable from no write and now preserves TPR[3:0], where architecturally it would clear it; the previous behaviour cleared TPR[3:0] on every exit regardless. Found while implementing Hyper-V VSM on KVM planes. Fixes: d7bf8221a303 ("KVM: SVM: sync V_TPR with LAPIC.TPR if CR8 write intercept is disabled") Signed-off-by: David Woodhouse --- arch/x86/kvm/svm/svm.c | 11 ++++++++++- arch/x86/kvm/svm/svm.h | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index cf049367523c..9bfcc7684504 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4622,7 +4622,15 @@ static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) { int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; - kvm_set_cr8(vcpu, cr8); + + /* + * Hardware changes V_TPR only on a guest CR8 write. Pushing + * an unchanged value into the APIC would zero TPR[3:0] (CR8 + * maps TPR[7:4] alone), wiping the low nibble of a TASKPRI + * value the guest set through the APIC page or MSR. + */ + if (cr8 != svm->v_tpr_entry) + kvm_set_cr8(vcpu, cr8); } } @@ -4637,6 +4645,7 @@ static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) cr8 = kvm_get_cr8(vcpu); svm->vmcb->control.int_ctl &= ~V_TPR_MASK; svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; + svm->v_tpr_entry = cr8 & V_TPR_MASK; } static void svm_complete_soft_interrupt(struct kvm_vcpu *vcpu, u8 vector, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e7fced399e05..3dfa8b20152b 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -323,6 +323,19 @@ struct vcpu_svm { void *msrpm; + /* + * V_TPR value written by sync_lapic_to_cr8() on the last entry. + * sync_cr8_to_lapic() pushes V_TPR back into the APIC only when it + * differs: V_TPR holds TPR[7:4] only, so an unconditional back-sync + * would zero TPR[3:0] after every exit, losing the low nibble of an + * APIC TASKPRI write. A differing V_TPR implies a guest CR8 write, + * but not the converse: a CR8 write of the same value is + * indistinguishable from no write, so TPR[3:0] survives it where + * the architectural write (and the intercepted path through + * kvm_lapic_set_tpr()) would clear it. + */ + u8 v_tpr_entry; + ulong nmi_iret_rip; struct svm_nested_state nested; -- 2.50.1