]> err.no Git - linux-2.6/blobdiff - drivers/kvm/kvm_main.c
KVM: Don't assign vcpu->cr3 if it's invalid: check first, set last
[linux-2.6] / drivers / kvm / kvm_main.c
index a65a145f30501664f9f7d2ec8234def782a3968a..5dc60170278241235d34009b6094b63fcd10c7b3 100644 (file)
@@ -53,6 +53,8 @@ static LIST_HEAD(vm_list);
 static cpumask_t cpus_hardware_enabled;
 
 struct kvm_arch_ops *kvm_arch_ops;
+struct kmem_cache *kvm_vcpu_cache;
+EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
 
 static __read_mostly struct preempt_ops kvm_preempt_ops;
 
@@ -146,82 +148,14 @@ static inline int valid_vcpu(int n)
        return likely(n >= 0 && n < KVM_MAX_VCPUS);
 }
 
-int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
-                  void *dest)
-{
-       unsigned char *host_buf = dest;
-       unsigned long req_size = size;
-
-       while (size) {
-               hpa_t paddr;
-               unsigned now;
-               unsigned offset;
-               hva_t guest_buf;
-
-               paddr = gva_to_hpa(vcpu, addr);
-
-               if (is_error_hpa(paddr))
-                       break;
-
-               guest_buf = (hva_t)kmap_atomic(
-                                       pfn_to_page(paddr >> PAGE_SHIFT),
-                                       KM_USER0);
-               offset = addr & ~PAGE_MASK;
-               guest_buf |= offset;
-               now = min(size, PAGE_SIZE - offset);
-               memcpy(host_buf, (void*)guest_buf, now);
-               host_buf += now;
-               addr += now;
-               size -= now;
-               kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
-       }
-       return req_size - size;
-}
-EXPORT_SYMBOL_GPL(kvm_read_guest);
-
-int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
-                   void *data)
-{
-       unsigned char *host_buf = data;
-       unsigned long req_size = size;
-
-       while (size) {
-               hpa_t paddr;
-               unsigned now;
-               unsigned offset;
-               hva_t guest_buf;
-               gfn_t gfn;
-
-               paddr = gva_to_hpa(vcpu, addr);
-
-               if (is_error_hpa(paddr))
-                       break;
-
-               gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
-               mark_page_dirty(vcpu->kvm, gfn);
-               guest_buf = (hva_t)kmap_atomic(
-                               pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
-               offset = addr & ~PAGE_MASK;
-               guest_buf |= offset;
-               now = min(size, PAGE_SIZE - offset);
-               memcpy((void*)guest_buf, host_buf, now);
-               host_buf += now;
-               addr += now;
-               size -= now;
-               kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
-       }
-       return req_size - size;
-}
-EXPORT_SYMBOL_GPL(kvm_write_guest);
-
 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 {
        if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
                return;
 
        vcpu->guest_fpu_loaded = 1;
-       fx_save(vcpu->host_fx_image);
-       fx_restore(vcpu->guest_fx_image);
+       fx_save(&vcpu->host_fx_image);
+       fx_restore(&vcpu->guest_fx_image);
 }
 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
 
@@ -231,8 +165,8 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
                return;
 
        vcpu->guest_fpu_loaded = 0;
-       fx_save(vcpu->guest_fx_image);
-       fx_restore(vcpu->host_fx_image);
+       fx_save(&vcpu->guest_fx_image);
+       fx_restore(&vcpu->host_fx_image);
 }
 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
 
@@ -328,10 +262,6 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
        }
        vcpu->pio_data = page_address(page);
 
-       vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
-                                          FX_IMAGE_ALIGN);
-       vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
-
        r = kvm_mmu_create(vcpu);
        if (r < 0)
                goto fail_free_pio_data;
@@ -649,7 +579,6 @@ void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
                }
        }
 
-       vcpu->cr3 = cr3;
        mutex_lock(&vcpu->kvm->lock);
        /*
         * Does the new cr3 value map to physical memory? (Note, we
@@ -662,8 +591,10 @@ void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
         */
        if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
                inject_gp(vcpu);
-       else
+       else {
+               vcpu->cr3 = cr3;
                vcpu->mmu.new_cr3(vcpu);
+       }
        mutex_unlock(&vcpu->kvm->lock);
 }
 EXPORT_SYMBOL_GPL(set_cr3);
@@ -681,30 +612,20 @@ EXPORT_SYMBOL_GPL(set_cr8);
 
 void fx_init(struct kvm_vcpu *vcpu)
 {
-       struct __attribute__ ((__packed__)) fx_image_s {
-               u16 control; //fcw
-               u16 status; //fsw
-               u16 tag; // ftw
-               u16 opcode; //fop
-               u64 ip; // fpu ip
-               u64 operand;// fpu dp
-               u32 mxcsr;
-               u32 mxcsr_mask;
-
-       } *fx_image;
+       unsigned after_mxcsr_mask;
 
        /* Initialize guest FPU by resetting ours and saving into guest's */
        preempt_disable();
-       fx_save(vcpu->host_fx_image);
+       fx_save(&vcpu->host_fx_image);
        fpu_init();
-       fx_save(vcpu->guest_fx_image);
-       fx_restore(vcpu->host_fx_image);
+       fx_save(&vcpu->guest_fx_image);
+       fx_restore(&vcpu->host_fx_image);
        preempt_enable();
 
-       fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
-       fx_image->mxcsr = 0x1f80;
-       memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
-              0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
+       after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
+       vcpu->guest_fx_image.mxcsr = 0x1f80;
+       memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
+              0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
 }
 EXPORT_SYMBOL_GPL(fx_init);
 
@@ -883,11 +804,14 @@ static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
        if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
                goto out;
 
-       mutex_lock(&kvm->lock);
-       kvm_mmu_slot_remove_write_access(kvm, log->slot);
-       kvm_flush_remote_tlbs(kvm);
-       memset(memslot->dirty_bitmap, 0, n);
-       mutex_unlock(&kvm->lock);
+       /* If nothing is dirty, don't bother messing with page tables. */
+       if (any) {
+               mutex_lock(&kvm->lock);
+               kvm_mmu_slot_remove_write_access(kvm, log->slot);
+               kvm_flush_remote_tlbs(kvm);
+               memset(memslot->dirty_bitmap, 0, n);
+               mutex_unlock(&kvm->lock);
+       }
 
        r = 0;
 
@@ -1017,7 +941,7 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
        }
 }
 
-static int emulator_read_std(unsigned long addr,
+int emulator_read_std(unsigned long addr,
                             void *val,
                             unsigned int bytes,
                             struct kvm_vcpu *vcpu)
@@ -1051,6 +975,7 @@ static int emulator_read_std(unsigned long addr,
 
        return X86EMUL_CONTINUE;
 }
+EXPORT_SYMBOL_GPL(emulator_read_std);
 
 static int emulator_write_std(unsigned long addr,
                              const void *val,
@@ -1169,7 +1094,7 @@ static int emulator_write_emulated_onepage(unsigned long addr,
        return X86EMUL_CONTINUE;
 }
 
-static int emulator_write_emulated(unsigned long addr,
+int emulator_write_emulated(unsigned long addr,
                                   const void *val,
                                   unsigned int bytes,
                                   struct kvm_vcpu *vcpu)
@@ -1188,6 +1113,7 @@ static int emulator_write_emulated(unsigned long addr,
        }
        return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
 }
+EXPORT_SYMBOL_GPL(emulator_write_emulated);
 
 static int emulator_cmpxchg_emulated(unsigned long addr,
                                     const void *old,
@@ -2420,6 +2346,9 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
 
        preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
 
+       /* We do fxsave: this must be aligned. */
+       BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
+
        vcpu_load(vcpu);
        r = kvm_mmu_setup(vcpu);
        vcpu_put(vcpu);
@@ -2532,7 +2461,7 @@ struct fxsave {
 
 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
 {
-       struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
+       struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
 
        vcpu_load(vcpu);
 
@@ -2552,7 +2481,7 @@ static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
 
 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
 {
-       struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
+       struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
 
        vcpu_load(vcpu);
 
@@ -3170,9 +3099,11 @@ static void kvm_sched_out(struct preempt_notifier *pn,
        kvm_arch_ops->vcpu_put(vcpu);
 }
 
-int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
+int kvm_init_arch(struct kvm_arch_ops *ops, unsigned int vcpu_size,
+                 struct module *module)
 {
        int r;
+       int cpu;
 
        if (kvm_arch_ops) {
                printk(KERN_ERR "kvm: already loaded the other module\n");
@@ -3194,6 +3125,14 @@ int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
        if (r < 0)
                goto out;
 
+       for_each_online_cpu(cpu) {
+               smp_call_function_single(cpu,
+                               kvm_arch_ops->check_processor_compatibility,
+                               &r, 0, 1);
+               if (r < 0)
+                       goto out_free_0;
+       }
+
        on_each_cpu(hardware_enable, NULL, 0, 1);
        r = register_cpu_notifier(&kvm_cpu_notifier);
        if (r)
@@ -3208,6 +3147,14 @@ int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
        if (r)
                goto out_free_3;
 
+       /* A kmem cache lets us meet the alignment requirements of fx_save. */
+       kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
+                                          __alignof__(struct kvm_vcpu), 0, 0);
+       if (!kvm_vcpu_cache) {
+               r = -ENOMEM;
+               goto out_free_4;
+       }
+
        kvm_chardev_ops.owner = module;
 
        r = misc_register(&kvm_dev);
@@ -3222,6 +3169,8 @@ int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
        return r;
 
 out_free:
+       kmem_cache_destroy(kvm_vcpu_cache);
+out_free_4:
        sysdev_unregister(&kvm_sysdev);
 out_free_3:
        sysdev_class_unregister(&kvm_sysdev_class);
@@ -3230,6 +3179,7 @@ out_free_2:
        unregister_cpu_notifier(&kvm_cpu_notifier);
 out_free_1:
        on_each_cpu(hardware_disable, NULL, 0, 1);
+out_free_0:
        kvm_arch_ops->hardware_unsetup();
 out:
        kvm_arch_ops = NULL;
@@ -3239,6 +3189,7 @@ out:
 void kvm_exit_arch(void)
 {
        misc_deregister(&kvm_dev);
+       kmem_cache_destroy(kvm_vcpu_cache);
        sysdev_unregister(&kvm_sysdev);
        sysdev_class_unregister(&kvm_sysdev_class);
        unregister_reboot_notifier(&kvm_reboot_notifier);