Skip to content

Commit 219b65d

Browse files
agrafavikivity
authored andcommitted
KVM: SVM: Improve nested interrupt injection
While trying to get Hyper-V running, I realized that the interrupt injection mechanisms that are in place right now are not 100% correct. This patch makes nested SVM's interrupt injection behave more like on a real machine. Signed-off-by: Alexander Graf <[email protected]> Signed-off-by: Avi Kivity <[email protected]>
1 parent ff09238 commit 219b65d

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

arch/x86/kvm/svm.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,8 @@ static int nested_svm_vmexit_real(struct vcpu_svm *svm, void *arg1,
16121612
/* Kill any pending exceptions */
16131613
if (svm->vcpu.arch.exception.pending == true)
16141614
nsvm_printk("WARNING: Pending Exception\n");
1615-
svm->vcpu.arch.exception.pending = false;
1615+
kvm_clear_exception_queue(&svm->vcpu);
1616+
kvm_clear_interrupt_queue(&svm->vcpu);
16161617

16171618
/* Restore selected save entries */
16181619
svm->vmcb->save.es = hsave->save.es;
@@ -1680,7 +1681,8 @@ static int nested_svm_vmrun(struct vcpu_svm *svm, void *arg1,
16801681
svm->nested_vmcb = svm->vmcb->save.rax;
16811682

16821683
/* Clear internal status */
1683-
svm->vcpu.arch.exception.pending = false;
1684+
kvm_clear_exception_queue(&svm->vcpu);
1685+
kvm_clear_interrupt_queue(&svm->vcpu);
16841686

16851687
/* Save the old vmcb, so we don't need to pick what we save, but
16861688
can restore everything when a VMEXIT occurs */
@@ -2362,21 +2364,14 @@ static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
23622364
((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
23632365
}
23642366

2365-
static void svm_queue_irq(struct kvm_vcpu *vcpu, unsigned nr)
2366-
{
2367-
struct vcpu_svm *svm = to_svm(vcpu);
2368-
2369-
svm->vmcb->control.event_inj = nr |
2370-
SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
2371-
}
2372-
23732367
static void svm_set_irq(struct kvm_vcpu *vcpu)
23742368
{
23752369
struct vcpu_svm *svm = to_svm(vcpu);
23762370

2377-
nested_svm_intr(svm);
2371+
BUG_ON(!(svm->vcpu.arch.hflags & HF_GIF_MASK));
23782372

2379-
svm_queue_irq(vcpu, vcpu->arch.interrupt.nr);
2373+
svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2374+
SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
23802375
}
23812376

23822377
static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
@@ -2404,13 +2399,25 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
24042399
struct vmcb *vmcb = svm->vmcb;
24052400
return (vmcb->save.rflags & X86_EFLAGS_IF) &&
24062401
!(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2407-
(svm->vcpu.arch.hflags & HF_GIF_MASK);
2402+
(svm->vcpu.arch.hflags & HF_GIF_MASK) &&
2403+
!is_nested(svm);
24082404
}
24092405

24102406
static void enable_irq_window(struct kvm_vcpu *vcpu)
24112407
{
2412-
svm_set_vintr(to_svm(vcpu));
2413-
svm_inject_irq(to_svm(vcpu), 0x0);
2408+
struct vcpu_svm *svm = to_svm(vcpu);
2409+
nsvm_printk("Trying to open IRQ window\n");
2410+
2411+
nested_svm_intr(svm);
2412+
2413+
/* In case GIF=0 we can't rely on the CPU to tell us when
2414+
* GIF becomes 1, because that's a separate STGI/VMRUN intercept.
2415+
* The next time we get that intercept, this function will be
2416+
* called again though and we'll get the vintr intercept. */
2417+
if (svm->vcpu.arch.hflags & HF_GIF_MASK) {
2418+
svm_set_vintr(svm);
2419+
svm_inject_irq(svm, 0x0);
2420+
}
24142421
}
24152422

24162423
static void enable_nmi_window(struct kvm_vcpu *vcpu)
@@ -2489,6 +2496,8 @@ static void svm_complete_interrupts(struct vcpu_svm *svm)
24892496
case SVM_EXITINTINFO_TYPE_EXEPT:
24902497
/* In case of software exception do not reinject an exception
24912498
vector, but re-execute and instruction instead */
2499+
if (is_nested(svm))
2500+
break;
24922501
if (kvm_exception_is_soft(vector))
24932502
break;
24942503
if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {

0 commit comments

Comments
 (0)