KVM: Prevent overflow in largepages calculation
[safe/jmp/linux-2.6] / virt / kvm / irq_comm.c
index 1c6ff6d..a8bd466 100644 (file)
@@ -22,6 +22,9 @@
 #include <linux/kvm_host.h>
 
 #include <asm/msidef.h>
+#ifdef CONFIG_IA64
+#include <asm/iosapic.h>
+#endif
 
 #include "irq.h"
 
@@ -43,117 +46,73 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
        return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
 }
 
-void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
-                                  union kvm_ioapic_redirect_entry *entry,
-                                  unsigned long *deliver_bitmask)
+inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
-       int i;
-       struct kvm *kvm = ioapic->kvm;
-       struct kvm_vcpu *vcpu;
-
-       bitmap_zero(deliver_bitmask, KVM_MAX_VCPUS);
-
-       if (entry->fields.dest_mode == 0) {     /* Physical mode. */
-               if (entry->fields.dest_id == 0xFF) {    /* Broadcast. */
-                       for (i = 0; i < KVM_MAX_VCPUS; ++i)
-                               if (kvm->vcpus[i] && kvm->vcpus[i]->arch.apic)
-                                       __set_bit(i, deliver_bitmask);
-                       /* Lowest priority shouldn't combine with broadcast */
-                       if (entry->fields.delivery_mode ==
-                           IOAPIC_LOWEST_PRIORITY && printk_ratelimit())
-                               printk(KERN_INFO "kvm: apic: phys broadcast "
-                                                 "and lowest prio\n");
-                       return;
-               }
-               for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-                       vcpu = kvm->vcpus[i];
-                       if (!vcpu)
-                               continue;
-                       if (kvm_apic_match_physical_addr(vcpu->arch.apic,
-                                       entry->fields.dest_id)) {
-                               if (vcpu->arch.apic)
-                                       __set_bit(i, deliver_bitmask);
-                               break;
-                       }
-               }
-       } else if (entry->fields.dest_id != 0) /* Logical mode, MDA non-zero. */
-               for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-                       vcpu = kvm->vcpus[i];
-                       if (!vcpu)
-                               continue;
-                       if (vcpu->arch.apic &&
-                           kvm_apic_match_logical_addr(vcpu->arch.apic,
-                                       entry->fields.dest_id))
-                               __set_bit(i, deliver_bitmask);
-               }
-
-       switch (entry->fields.delivery_mode) {
-       case IOAPIC_LOWEST_PRIORITY:
-               /* Select one in deliver_bitmask */
-               vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm,
-                               entry->fields.vector, deliver_bitmask);
-               bitmap_zero(deliver_bitmask, KVM_MAX_VCPUS);
-               if (!vcpu)
-                       return;
-               __set_bit(vcpu->vcpu_id, deliver_bitmask);
-               break;
-       case IOAPIC_FIXED:
-       case IOAPIC_NMI:
-               break;
-       default:
-               if (printk_ratelimit())
-                       printk(KERN_INFO "kvm: unsupported delivery mode %d\n",
-                               entry->fields.delivery_mode);
-               bitmap_zero(deliver_bitmask, KVM_MAX_VCPUS);
-       }
+#ifdef CONFIG_IA64
+       return irq->delivery_mode ==
+               (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
+#else
+       return irq->delivery_mode == APIC_DM_LOWEST;
+#endif
 }
 
-static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
-                      struct kvm *kvm, int level)
+int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
+               struct kvm_lapic_irq *irq)
 {
-       int vcpu_id, r = -1;
-       struct kvm_vcpu *vcpu;
-       struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
-       union kvm_ioapic_redirect_entry entry;
-       DECLARE_BITMAP(deliver_bitmask, KVM_MAX_VCPUS);
+       int i, r = -1;
+       struct kvm_vcpu *vcpu, *lowest = NULL;
 
-       BUG_ON(!ioapic);
+       if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
+                       kvm_is_dm_lowest_prio(irq))
+               printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
 
-       entry.bits = 0;
-       entry.fields.dest_id = (e->msi.address_lo &
-                       MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
-       entry.fields.vector = (e->msi.data &
-                       MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
-       entry.fields.dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
-                       (unsigned long *)&e->msi.address_lo);
-       entry.fields.trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
-                       (unsigned long *)&e->msi.data);
-       entry.fields.delivery_mode = test_bit(
-                       MSI_DATA_DELIVERY_MODE_SHIFT,
-                       (unsigned long *)&e->msi.data);
+       for (i = 0; i < KVM_MAX_VCPUS; i++) {
+               vcpu = kvm->vcpus[i];
 
-       /* TODO Deal with RH bit of MSI message address */
+               if (!vcpu || !kvm_apic_present(vcpu))
+                       continue;
 
-       kvm_get_intr_delivery_bitmask(ioapic, &entry, deliver_bitmask);
+               if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
+                                       irq->dest_id, irq->dest_mode))
+                       continue;
 
-       if (find_first_bit(deliver_bitmask, KVM_MAX_VCPUS) >= KVM_MAX_VCPUS) {
-               printk(KERN_WARNING "kvm: no destination for MSI delivery!");
-               return -1;
-       }
-       while ((vcpu_id = find_first_bit(deliver_bitmask,
-                                       KVM_MAX_VCPUS)) < KVM_MAX_VCPUS) {
-               __clear_bit(vcpu_id, deliver_bitmask);
-               vcpu = ioapic->kvm->vcpus[vcpu_id];
-               if (vcpu) {
+               if (!kvm_is_dm_lowest_prio(irq)) {
                        if (r < 0)
                                r = 0;
-                       r += kvm_apic_set_irq(vcpu, entry.fields.vector,
-                                             entry.fields.trig_mode);
+                       r += kvm_apic_set_irq(vcpu, irq);
+               } else {
+                       if (!lowest)
+                               lowest = vcpu;
+                       else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
+                               lowest = vcpu;
                }
        }
+
+       if (lowest)
+               r = kvm_apic_set_irq(lowest, irq);
+
        return r;
 }
 
+static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
+                      struct kvm *kvm, int level)
+{
+       struct kvm_lapic_irq irq;
+
+       irq.dest_id = (e->msi.address_lo &
+                       MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
+       irq.vector = (e->msi.data &
+                       MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
+       irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
+       irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
+       irq.delivery_mode = e->msi.data & 0x700;
+       irq.level = 1;
+       irq.shorthand = 0;
+
+       /* TODO Deal with RH bit of MSI message address */
+       return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
+}
+
 /* This should be called with the kvm->lock mutex held
  * Return value:
  *  < 0   Interrupt was ignored (masked or not delivered for other reasons)
@@ -312,7 +271,7 @@ static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
                        delta = 8;
                        break;
                case KVM_IRQCHIP_IOAPIC:
-                               e->set = kvm_set_ioapic_irq;
+                       e->set = kvm_set_ioapic_irq;
                        break;
                default:
                        goto out;