KVM: Add mini-API for vcpu->requests
authorAvi Kivity <avi@redhat.com>
Mon, 10 May 2010 09:34:53 +0000 (12:34 +0300)
committerAvi Kivity <avi@redhat.com>
Sun, 1 Aug 2010 07:47:05 +0000 (10:47 +0300)
Makes it a little more readable and hackable.

Signed-off-by: Avi Kivity <avi@redhat.com>
arch/x86/kvm/lapic.c
arch/x86/kvm/mmu.c
arch/x86/kvm/svm.c
arch/x86/kvm/timer.c
arch/x86/kvm/vmx.c
arch/x86/kvm/x86.c
include/linux/kvm_host.h
virt/kvm/kvm_main.c

index 49573c78c24b0691d5e696165dc210d3653174b8..77d8c0f4817d5f10f88e725ad49b22642c1172c5 100644 (file)
@@ -534,7 +534,7 @@ static void __report_tpr_access(struct kvm_lapic *apic, bool write)
        struct kvm_vcpu *vcpu = apic->vcpu;
        struct kvm_run *run = vcpu->run;
 
-       set_bit(KVM_REQ_REPORT_TPR_ACCESS, &vcpu->requests);
+       kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
        run->tpr_access.rip = kvm_rip_read(vcpu);
        run->tpr_access.is_write = write;
 }
index c5501bc101068401cc7066d2933c9a14135b0010..690a7fc58c1711d645374c5097b30587be83db1b 100644 (file)
@@ -1378,7 +1378,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
 
                mmu_page_add_parent_pte(vcpu, sp, parent_pte);
                if (sp->unsync_children) {
-                       set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
+                       kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
                        kvm_mmu_mark_parents_unsync(sp);
                } else if (sp->unsync)
                        kvm_mmu_mark_parents_unsync(sp);
@@ -2131,7 +2131,7 @@ static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
        int ret = 0;
 
        if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
-               set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
+               kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
                ret = 1;
        }
 
@@ -2329,7 +2329,7 @@ static int nonpaging_init_context(struct kvm_vcpu *vcpu)
 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
 {
        ++vcpu->stat.tlb_flush;
-       set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
+       kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
 }
 
 static void paging_new_cr3(struct kvm_vcpu *vcpu)
index f7a6fdcf8ef35eb58bccc37d2a9837157779a79c..587b99d37d44e9523e35640efce2016443759f17 100644 (file)
@@ -1494,7 +1494,7 @@ static void svm_handle_mce(struct vcpu_svm *svm)
                 */
                pr_err("KVM: Guest triggered AMD Erratum 383\n");
 
-               set_bit(KVM_REQ_TRIPLE_FAULT, &svm->vcpu.requests);
+               kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
 
                return;
        }
index 564548fbb3d6f38d291ebb447436563ffa72e239..e16a0dbe74d8268bc0af65bbe005f76e9616f71e 100644 (file)
@@ -32,7 +32,7 @@ static int __kvm_timer_fn(struct kvm_vcpu *vcpu, struct kvm_timer *ktimer)
        if (ktimer->reinject || !atomic_read(&ktimer->pending)) {
                atomic_inc(&ktimer->pending);
                /* FIXME: this code should not know anything about vcpus */
-               set_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
+               kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
        }
 
        if (waitqueue_active(q))
index 345a35470511abbc8b94280956c2c388bbfc64a2..661c6e199b4a2ed89493fe4f27374d86ff01de16 100644 (file)
@@ -899,7 +899,7 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
                unsigned long sysenter_esp;
 
                kvm_migrate_timers(vcpu);
-               set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
+               kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
                local_irq_disable();
                list_add(&vmx->local_vcpus_link,
                         &per_cpu(vcpus_on_cpu, cpu));
index 9be6e4e5e8eefc9a138f3ef77fb084da7f809457..7ef44107a14a13147c1862127360fe2451bfee57 100644 (file)
@@ -296,7 +296,7 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
        prev_nr = vcpu->arch.exception.nr;
        if (prev_nr == DF_VECTOR) {
                /* triple fault -> shutdown */
-               set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
+               kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
                return;
        }
        class1 = exception_class(prev_nr);
@@ -948,7 +948,7 @@ static int kvm_request_guest_time_update(struct kvm_vcpu *v)
 
        if (!vcpu->time_page)
                return 0;
-       set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
+       kvm_make_request(KVM_REQ_KVMCLOCK_UPDATE, v);
        return 1;
 }
 
@@ -2253,7 +2253,7 @@ static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
                        printk(KERN_DEBUG "kvm: set_mce: "
                               "injects mce exception while "
                               "previous one is in progress!\n");
-                       set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
+                       kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
                        return 0;
                }
                if (banks[1] & MCI_STATUS_VAL)
@@ -4617,7 +4617,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
                vcpu->run->request_interrupt_window;
 
        if (vcpu->requests)
-               if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
+               if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
                        kvm_mmu_unload(vcpu);
 
        r = kvm_mmu_reload(vcpu);
@@ -4625,26 +4625,25 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
                goto out;
 
        if (vcpu->requests) {
-               if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
+               if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
                        __kvm_migrate_timers(vcpu);
-               if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
+               if (kvm_check_request(KVM_REQ_KVMCLOCK_UPDATE, vcpu))
                        kvm_write_guest_time(vcpu);
-               if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
+               if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
                        kvm_mmu_sync_roots(vcpu);
-               if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
+               if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
                        kvm_x86_ops->tlb_flush(vcpu);
-               if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
-                                      &vcpu->requests)) {
+               if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
                        vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
                        r = 0;
                        goto out;
                }
-               if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
+               if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
                        vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
                        r = 0;
                        goto out;
                }
-               if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
+               if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
                        vcpu->fpu_active = 0;
                        kvm_x86_ops->fpu_deactivate(vcpu);
                }
@@ -4773,7 +4772,7 @@ static int __vcpu_run(struct kvm_vcpu *vcpu)
                        srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
                        kvm_vcpu_block(vcpu);
                        vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
-                       if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
+                       if (kvm_check_request(KVM_REQ_UNHALT, vcpu))
                        {
                                switch(vcpu->arch.mp_state) {
                                case KVM_MP_STATE_HALTED:
@@ -5255,7 +5254,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
        vcpu->guest_fpu_loaded = 0;
        fpu_save_init(&vcpu->arch.guest_fpu);
        ++vcpu->stat.fpu_reload;
-       set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
+       kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
        trace_kvm_fpu(0);
 }
 
index 240e460777bcaa6320f1a3704b4029947fb4bb93..c8a9d628898e43323a7140e29156bd40ca546940 100644 (file)
@@ -624,5 +624,20 @@ static inline long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
 
 #endif
 
+static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
+{
+       set_bit(req, &vcpu->requests);
+}
+
+static inline bool kvm_make_check_request(int req, struct kvm_vcpu *vcpu)
+{
+       return test_and_set_bit(req, &vcpu->requests);
+}
+
+static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
+{
+       return test_and_clear_bit(req, &vcpu->requests);
+}
+
 #endif
 
index 65417e3d8462a87e87fc860b4fe441e94f639062..5bd2f34ba5764df2ece9aa8948a3e43039fc0ec1 100644 (file)
@@ -145,7 +145,7 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
        raw_spin_lock(&kvm->requests_lock);
        me = smp_processor_id();
        kvm_for_each_vcpu(i, vcpu, kvm) {
-               if (test_and_set_bit(req, &vcpu->requests))
+               if (kvm_make_check_request(req, vcpu))
                        continue;
                cpu = vcpu->cpu;
                if (cpus != NULL && cpu != -1 && cpu != me)
@@ -1212,7 +1212,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
                prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
 
                if (kvm_arch_vcpu_runnable(vcpu)) {
-                       set_bit(KVM_REQ_UNHALT, &vcpu->requests);
+                       kvm_make_request(KVM_REQ_UNHALT, vcpu);
                        break;
                }
                if (kvm_cpu_has_pending_timer(vcpu))