KVM: Fix interrupt unhalting a vcpu when it shouldn't
authorGleb Natapov <gleb@redhat.com>
Mon, 23 Mar 2009 10:12:11 +0000 (12:12 +0200)
committerAvi Kivity <avi@redhat.com>
Wed, 10 Jun 2009 08:48:33 +0000 (11:48 +0300)
kvm_vcpu_block() unhalts vpu on an interrupt/timer without checking
if interrupt window is actually opened.

Signed-off-by: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
arch/ia64/kvm/kvm-ia64.c
arch/powerpc/kvm/powerpc.c
arch/s390/kvm/interrupt.c
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/svm.c
arch/x86/kvm/vmx.c
arch/x86/kvm/x86.c
include/linux/kvm_host.h
virt/kvm/kvm_main.c

index d2a90fd505b00e7430bd20ae6eb574ce388abc36..3bf0a345224afebfc35e98ee53677cb90a1f88d0 100644 (file)
@@ -1963,6 +1963,12 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
        return 0;
 }
 
+int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
+{
+       /* do real check here */
+       return 1;
+}
+
 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
 {
        return vcpu->arch.timer_fired;
index 9057335fdc616ce607f76b2dc7e099965ce49e5e..2cf915e51e7e9e7c7ad6202e0d287187b8a313ed 100644 (file)
@@ -41,6 +41,12 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
        return !!(v->arch.pending_exceptions);
 }
 
+int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
+{
+       /* do real check here */
+       return 1;
+}
+
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
 {
        return !(v->arch.msr & MSR_WE);
index 0189356fe2098cb3f6ddd61434da623b8a7c9cfa..4ed4c3a11485f9ac21bc2a0359441773d8b796cf 100644 (file)
@@ -318,6 +318,12 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
        return rc;
 }
 
+int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
+{
+       /* do real check here */
+       return 1;
+}
+
 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
 {
        return 0;
index 46276273a1a1687843aa59d6763570438e9356d5..8351c4d00ac0faf5661a9b0ebcaf10a9ed1309b0 100644 (file)
@@ -521,7 +521,7 @@ struct kvm_x86_ops {
        void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
        void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
                                       struct kvm_run *run);
-
+       int (*interrupt_allowed)(struct kvm_vcpu *vcpu);
        int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
        int (*get_tdp_level)(void);
        int (*get_mt_mask_shift)(void);
index aa528dbad070f697bae68197579adcdcd82728cf..de741043c5b1540bb628217cc6760d531af7cfb6 100644 (file)
@@ -2270,6 +2270,15 @@ static void update_cr8_intercept(struct kvm_vcpu *vcpu)
                vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
 }
 
+static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
+{
+       struct vcpu_svm *svm = to_svm(vcpu);
+       struct vmcb *vmcb = svm->vmcb;
+       return (vmcb->save.rflags & X86_EFLAGS_IF) &&
+               !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
+               (svm->vcpu.arch.hflags & HF_GIF_MASK);
+}
+
 static void svm_intr_assist(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
@@ -2649,6 +2658,7 @@ static struct kvm_x86_ops svm_x86_ops = {
        .exception_injected = svm_exception_injected,
        .inject_pending_irq = svm_intr_assist,
        .inject_pending_vectors = do_interrupt_requests,
+       .interrupt_allowed = svm_interrupt_allowed,
 
        .set_tss_addr = svm_set_tss_addr,
        .get_tdp_level = get_npt_level,
index da6461d5dc84a53ffa89c4e9f86cb9a1b19f18ae..b9e06b07aca12d1a8c2eb657c68932413819f9a9 100644 (file)
@@ -2490,6 +2490,12 @@ static void vmx_update_window_states(struct kvm_vcpu *vcpu)
                                 GUEST_INTR_STATE_MOV_SS)));
 }
 
+static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
+{
+       vmx_update_window_states(vcpu);
+       return vcpu->arch.interrupt_window_open;
+}
+
 static void do_interrupt_requests(struct kvm_vcpu *vcpu,
                                       struct kvm_run *kvm_run)
 {
@@ -3691,7 +3697,7 @@ static struct kvm_x86_ops vmx_x86_ops = {
        .exception_injected = vmx_exception_injected,
        .inject_pending_irq = vmx_intr_assist,
        .inject_pending_vectors = do_interrupt_requests,
-
+       .interrupt_allowed = vmx_interrupt_allowed,
        .set_tss_addr = vmx_set_tss_addr,
        .get_tdp_level = get_ept_level,
        .get_mt_mask_shift = vmx_get_mt_mask_shift,
index 8fca7a4e95a3e2cb0e88bbf3acd84609ee412e9b..5bbcad345376e4b8ed3f170b81575a8bf59805a7 100644 (file)
@@ -4475,3 +4475,8 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
                smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
        put_cpu();
 }
+
+int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
+{
+       return kvm_x86_ops->interrupt_allowed(vcpu);
+}
index 40e49ede8f91448ef06406ae28e557b8a113e9fb..72d56844f3880fcdea4809695cc44dd983a2cc21 100644 (file)
@@ -298,6 +298,7 @@ int kvm_arch_hardware_setup(void);
 void kvm_arch_hardware_unsetup(void);
 void kvm_arch_check_processor_compat(void *rtn);
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
+int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu);
 
 void kvm_free_physmem(struct kvm *kvm);
 
index a1a4272fa57cb0199ce42e224319b767fbcae23d..63d5fa2bc84ac381f459828158315a17911fe928 100644 (file)
@@ -1610,7 +1610,8 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
        for (;;) {
                prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
 
-               if (kvm_cpu_has_interrupt(vcpu) ||
+               if ((kvm_arch_interrupt_allowed(vcpu) &&
+                                       kvm_cpu_has_interrupt(vcpu)) ||
                                kvm_arch_vcpu_runnable(vcpu)) {
                        set_bit(KVM_REQ_UNHALT, &vcpu->requests);
                        break;