KVM: pending irq save/restore
authorEddie Dong <eddie.dong@intel.com>
Mon, 6 Aug 2007 13:29:07 +0000 (16:29 +0300)
committerAvi Kivity <avi@qumranet.com>
Sat, 13 Oct 2007 08:18:26 +0000 (10:18 +0200)
Add in kernel irqchip save/restore support for pending vectors.

[avi: fix compile warning on i386]
[avi: remove printk]

Signed-off-by: Yaozu (Eddie) Dong <eddie.dong@intel.com>
Signed-off-by: Qing He <qing.he@intel.com>
Signed-off-by: Avi Kivity <avi@qumranet.com>
drivers/kvm/kvm.h
drivers/kvm/kvm_main.c
drivers/kvm/svm.c
drivers/kvm/vmx.c

index bb506b71797d2d95cc5538b7802f6924fc3274aa..f8fe87d3ddbddae9b68e6bb2a2d95c5b0ae44910 100644 (file)
@@ -490,6 +490,8 @@ struct kvm_arch_ops {
        void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
        void (*patch_hypercall)(struct kvm_vcpu *vcpu,
                                unsigned char *hypercall_addr);
+       int (*get_irq)(struct kvm_vcpu *vcpu);
+       void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
 };
 
 extern struct kvm_arch_ops *kvm_arch_ops;
index a012d70d9effc53ac9034703537fc6c10dbf804b..d56964a6eb801ced082377816a4b2d62e65abed6 100644 (file)
@@ -2126,6 +2126,7 @@ static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
                                    struct kvm_sregs *sregs)
 {
        struct descriptor_table dt;
+       int pending_vec;
 
        vcpu_load(vcpu);
 
@@ -2155,10 +2156,13 @@ static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
        sregs->efer = vcpu->shadow_efer;
        sregs->apic_base = kvm_get_apic_base(vcpu);
 
-       if (irqchip_in_kernel(vcpu->kvm))
+       if (irqchip_in_kernel(vcpu->kvm)) {
                memset(sregs->interrupt_bitmap, 0,
                       sizeof sregs->interrupt_bitmap);
-       else
+               pending_vec = kvm_arch_ops->get_irq(vcpu);
+               if (pending_vec >= 0)
+                       set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
+       } else
                memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
                       sizeof sregs->interrupt_bitmap);
 
@@ -2177,7 +2181,7 @@ static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
                                    struct kvm_sregs *sregs)
 {
        int mmu_reset_needed = 0;
-       int i;
+       int i, pending_vec, max_bits;
        struct descriptor_table dt;
 
        vcpu_load(vcpu);
@@ -2221,6 +2225,16 @@ static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
                for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
                        if (vcpu->irq_pending[i])
                                __set_bit(i, &vcpu->irq_summary);
+       } else {
+               max_bits = (sizeof sregs->interrupt_bitmap) << 3;
+               pending_vec = find_first_bit(
+                       (const unsigned long *)sregs->interrupt_bitmap,
+                       max_bits);
+               /* Only pending external irq is handled here */
+               if (pending_vec < max_bits) {
+                       kvm_arch_ops->set_irq(vcpu, pending_vec);
+                       printk("Set back pending irq %d\n", pending_vec);
+               }
        }
 
        set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
index a347b61644cd0f8f7e30f7ff1036964ead938578..c8cd242f36ff6dca2ea0c29743615a0a9a53b8b7 100644 (file)
@@ -843,6 +843,16 @@ static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
        return -EOPNOTSUPP;
 }
 
+static int svm_get_irq(struct kvm_vcpu *vcpu)
+{
+       struct vcpu_svm *svm = to_svm(vcpu);
+       u32 exit_int_info = svm->vmcb->control.exit_int_info;
+
+       if (is_external_interrupt(exit_int_info))
+               return exit_int_info & SVM_EVTINJ_VEC_MASK;
+       return -1;
+}
+
 static void load_host_msrs(struct kvm_vcpu *vcpu)
 {
 #ifdef CONFIG_X86_64
@@ -1310,6 +1320,13 @@ static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
                ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
 }
 
+static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
+{
+       struct vcpu_svm *svm = to_svm(vcpu);
+
+       svm_inject_irq(svm, irq);
+}
+
 static void svm_intr_assist(struct vcpu_svm *svm)
 {
        struct vmcb *vmcb = svm->vmcb;
@@ -1783,6 +1800,8 @@ static struct kvm_arch_ops svm_arch_ops = {
        .run = svm_vcpu_run,
        .skip_emulated_instruction = skip_emulated_instruction,
        .patch_hypercall = svm_patch_hypercall,
+       .get_irq = svm_get_irq,
+       .set_irq = svm_set_irq,
 };
 
 static int __init svm_init(void)
index 7ec8cf84e6eaf1c26e569cb5d3f046dd584d2be3..6c371ea210469b9e3f505973eb1b5b65256a3f25 100644 (file)
@@ -790,6 +790,20 @@ static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
        return 0;
 }
 
+static int vmx_get_irq(struct kvm_vcpu *vcpu)
+{
+       u32 idtv_info_field;
+
+       idtv_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+       if (idtv_info_field & INTR_INFO_VALID_MASK) {
+               if (is_external_interrupt(idtv_info_field))
+                       return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
+               else
+                       printk("pending exception: not handled yet\n");
+       }
+       return -1;
+}
+
 static __init int cpu_has_kvm_support(void)
 {
        unsigned long ecx = cpuid_ecx(1);
@@ -2500,6 +2514,8 @@ static struct kvm_arch_ops vmx_arch_ops = {
        .run = vmx_vcpu_run,
        .skip_emulated_instruction = skip_emulated_instruction,
        .patch_hypercall = vmx_patch_hypercall,
+       .get_irq = vmx_get_irq,
+       .set_irq = vmx_inject_irq,
 };
 
 static int __init vmx_init(void)