KVM: Emulate hlt in the kernel
authorEddie Dong <eddie.dong@intel.com>
Wed, 18 Jul 2007 09:15:21 +0000 (12:15 +0300)
committerAvi Kivity <avi@qumranet.com>
Sat, 13 Oct 2007 08:18:25 +0000 (10:18 +0200)
By sleeping in the kernel when hlt is executed, we simplify the in-kernel
guest interrupt path considerably.

Signed-off-by: Gregory Haskins <ghaskins@novell.com>
Signed-off-by: Yaozu (Eddie) Dong <eddie.dong@intel.com>
Signed-off-by: Avi Kivity <avi@qumranet.com>
drivers/kvm/i8259.c
drivers/kvm/irq.c
drivers/kvm/kvm.h
drivers/kvm/kvm_main.c
drivers/kvm/svm.c
drivers/kvm/vmx.c
include/linux/kvm.h

index 40ad1046223860f3d9d8576dccc82b946723201e..ee6030dc5c04f3dff4034b49a4814319d479d2ce 100644 (file)
@@ -413,8 +413,11 @@ static void picdev_read(struct kvm_io_device *this,
 static void pic_irq_request(void *opaque, int level)
 {
        struct kvm *kvm = opaque;
+       struct kvm_vcpu *vcpu = kvm->vcpus[0];
 
        pic_irqchip(kvm)->output = level;
+       if (vcpu)
+               kvm_vcpu_kick(vcpu);
 }
 
 struct kvm_pic *kvm_create_pic(struct kvm *kvm)
index 5265f8267b3b6e08e131c36c1aca443e929e60bf..e09cd65925d6f53a253bbb9c21ab69483d0033cf 100644 (file)
@@ -70,6 +70,10 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
 {
        int ipi_pcpu = vcpu->cpu;
 
+       if (waitqueue_active(&vcpu->wq)) {
+               wake_up_interruptible(&vcpu->wq);
+               ++vcpu->stat.halt_wakeup;
+       }
        if (vcpu->guest_mode)
                smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
 }
index 8d07a993af94ec02703726a896f0f5ac5c4555ea..bb506b71797d2d95cc5538b7802f6924fc3274aa 100644 (file)
@@ -231,6 +231,7 @@ struct kvm_stat {
        u32 signal_exits;
        u32 irq_window_exits;
        u32 halt_exits;
+       u32 halt_wakeup;
        u32 request_irq_exits;
        u32 irq_exits;
        u32 light_exits;
@@ -353,6 +354,7 @@ struct kvm_vcpu {
        gva_t mmio_fault_cr2;
        struct kvm_pio_request pio;
        void *pio_data;
+       wait_queue_head_t wq;
 
        int sigset_active;
        sigset_t sigset;
index ffbdadd879713de75d7ec182707d0817de47829a..4384364fc0c8a221db1b5a02733b01817b55bd82 100644 (file)
@@ -76,6 +76,7 @@ static struct kvm_stats_debugfs_item {
        { "signal_exits", STAT_OFFSET(signal_exits) },
        { "irq_window", STAT_OFFSET(irq_window_exits) },
        { "halt_exits", STAT_OFFSET(halt_exits) },
+       { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
        { "request_irq", STAT_OFFSET(request_irq_exits) },
        { "irq_exits", STAT_OFFSET(irq_exits) },
        { "light_exits", STAT_OFFSET(light_exits) },
@@ -248,6 +249,7 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
        vcpu->mmu.root_hpa = INVALID_PAGE;
        vcpu->kvm = kvm;
        vcpu->vcpu_id = id;
+       init_waitqueue_head(&vcpu->wq);
 
        page = alloc_page(GFP_KERNEL | __GFP_ZERO);
        if (!page) {
@@ -1307,15 +1309,41 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
 }
 EXPORT_SYMBOL_GPL(emulate_instruction);
 
-int kvm_emulate_halt(struct kvm_vcpu *vcpu)
+/*
+ * The vCPU has executed a HLT instruction with in-kernel mode enabled.
+ */
+static void kvm_vcpu_kernel_halt(struct kvm_vcpu *vcpu)
 {
-       if (vcpu->irq_summary ||
-               (irqchip_in_kernel(vcpu->kvm) && kvm_cpu_has_interrupt(vcpu)))
-               return 1;
+       DECLARE_WAITQUEUE(wait, current);
+
+       add_wait_queue(&vcpu->wq, &wait);
+
+       /*
+        * We will block until either an interrupt or a signal wakes us up
+        */
+       while(!(irqchip_in_kernel(vcpu->kvm) && kvm_cpu_has_interrupt(vcpu))
+             && !vcpu->irq_summary
+             && !signal_pending(current)) {
+               set_current_state(TASK_INTERRUPTIBLE);
+               vcpu_put(vcpu);
+               schedule();
+               vcpu_load(vcpu);
+       }
 
-       vcpu->run->exit_reason = KVM_EXIT_HLT;
+       remove_wait_queue(&vcpu->wq, &wait);
+       set_current_state(TASK_RUNNING);
+}
+
+int kvm_emulate_halt(struct kvm_vcpu *vcpu)
+{
        ++vcpu->stat.halt_exits;
-       return 0;
+       if (irqchip_in_kernel(vcpu->kvm)) {
+               kvm_vcpu_kernel_halt(vcpu);
+               return 1;
+       } else {
+               vcpu->run->exit_reason = KVM_EXIT_HLT;
+               return 0;
+       }
 }
 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
 
@@ -2916,6 +2944,7 @@ static long kvm_dev_ioctl(struct file *filp,
 
                switch (ext) {
                case KVM_CAP_IRQCHIP:
+               case KVM_CAP_HLT:
                        r = 1;
                        break;
                default:
index d576451827e73bbc90305e02ccf612319bfaf9c6..a347b61644cd0f8f7e30f7ff1036964ead938578 100644 (file)
@@ -1398,9 +1398,12 @@ static void do_interrupt_requests(struct vcpu_svm *svm,
 static void post_kvm_run_save(struct vcpu_svm *svm,
                              struct kvm_run *kvm_run)
 {
-       kvm_run->ready_for_interrupt_injection
-               = (svm->vcpu.interrupt_window_open &&
-                  svm->vcpu.irq_summary == 0);
+       if (irqchip_in_kernel(svm->vcpu.kvm))
+               kvm_run->ready_for_interrupt_injection = 1;
+       else
+               kvm_run->ready_for_interrupt_injection =
+                                        (svm->vcpu.interrupt_window_open &&
+                                         svm->vcpu.irq_summary == 0);
        kvm_run->if_flag = (svm->vmcb->save.rflags & X86_EFLAGS_IF) != 0;
        kvm_run->cr8 = get_cr8(&svm->vcpu);
        kvm_run->apic_base = kvm_get_apic_base(&svm->vcpu);
index c4cc17cc00f783e30249ee9868bd2028d7b87c48..7ec8cf84e6eaf1c26e569cb5d3f046dd584d2be3 100644 (file)
@@ -1961,8 +1961,12 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu,
        kvm_run->if_flag = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) != 0;
        kvm_run->cr8 = get_cr8(vcpu);
        kvm_run->apic_base = kvm_get_apic_base(vcpu);
-       kvm_run->ready_for_interrupt_injection = (vcpu->interrupt_window_open &&
-                                                 vcpu->irq_summary == 0);
+       if (irqchip_in_kernel(vcpu->kvm))
+               kvm_run->ready_for_interrupt_injection = 1;
+       else
+               kvm_run->ready_for_interrupt_injection =
+                                       (vcpu->interrupt_window_open &&
+                                        vcpu->irq_summary == 0);
 }
 
 static int handle_interrupt_window(struct kvm_vcpu *vcpu,
index 997bb3e46f1e4d53ca24867462fc269fe2a2475c..b0a13d1b34cc8481b35e06dfa21a6ca4d0ab946f 100644 (file)
@@ -283,6 +283,7 @@ struct kvm_signal_mask {
  * Extension capability list.
  */
 #define KVM_CAP_IRQCHIP          0
+#define KVM_CAP_HLT      1
 
 /*
  * ioctls for VM fds