KVM: Move ack notifier register and IRQ sourcd ID request
[linux-2.6.git] / virt / kvm / kvm_main.c
1 /*
2  * Kernel-based Virtual Machine driver for Linux
3  *
4  * This module enables machines with Intel VT-x extensions to run virtual
5  * machines without emulation or binary translation.
6  *
7  * Copyright (C) 2006 Qumranet, Inc.
8  *
9  * Authors:
10  *   Avi Kivity   <avi@qumranet.com>
11  *   Yaniv Kamay  <yaniv@qumranet.com>
12  *
13  * This work is licensed under the terms of the GNU GPL, version 2.  See
14  * the COPYING file in the top-level directory.
15  *
16  */
17
18 #include "iodev.h"
19
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/percpu.h>
25 #include <linux/gfp.h>
26 #include <linux/mm.h>
27 #include <linux/miscdevice.h>
28 #include <linux/vmalloc.h>
29 #include <linux/reboot.h>
30 #include <linux/debugfs.h>
31 #include <linux/highmem.h>
32 #include <linux/file.h>
33 #include <linux/sysdev.h>
34 #include <linux/cpu.h>
35 #include <linux/sched.h>
36 #include <linux/cpumask.h>
37 #include <linux/smp.h>
38 #include <linux/anon_inodes.h>
39 #include <linux/profile.h>
40 #include <linux/kvm_para.h>
41 #include <linux/pagemap.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44
45 #include <asm/processor.h>
46 #include <asm/io.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
49
50 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51 #include "coalesced_mmio.h"
52 #endif
53
54 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
55 #include <linux/pci.h>
56 #include <linux/interrupt.h>
57 #include "irq.h"
58 #endif
59
60 MODULE_AUTHOR("Qumranet");
61 MODULE_LICENSE("GPL");
62
63 DEFINE_SPINLOCK(kvm_lock);
64 LIST_HEAD(vm_list);
65
66 static cpumask_t cpus_hardware_enabled;
67
68 struct kmem_cache *kvm_vcpu_cache;
69 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
70
71 static __read_mostly struct preempt_ops kvm_preempt_ops;
72
73 struct dentry *kvm_debugfs_dir;
74
75 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
76                            unsigned long arg);
77
78 bool kvm_rebooting;
79
80 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
81 static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
82                                                       int assigned_dev_id)
83 {
84         struct list_head *ptr;
85         struct kvm_assigned_dev_kernel *match;
86
87         list_for_each(ptr, head) {
88                 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
89                 if (match->assigned_dev_id == assigned_dev_id)
90                         return match;
91         }
92         return NULL;
93 }
94
95 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
96 {
97         struct kvm_assigned_dev_kernel *assigned_dev;
98
99         assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
100                                     interrupt_work);
101
102         /* This is taken to safely inject irq inside the guest. When
103          * the interrupt injection (or the ioapic code) uses a
104          * finer-grained lock, update this
105          */
106         mutex_lock(&assigned_dev->kvm->lock);
107         kvm_set_irq(assigned_dev->kvm,
108                     assigned_dev->irq_source_id,
109                     assigned_dev->guest_irq, 1);
110         mutex_unlock(&assigned_dev->kvm->lock);
111         kvm_put_kvm(assigned_dev->kvm);
112 }
113
114 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
115 {
116         struct kvm_assigned_dev_kernel *assigned_dev =
117                 (struct kvm_assigned_dev_kernel *) dev_id;
118
119         kvm_get_kvm(assigned_dev->kvm);
120         schedule_work(&assigned_dev->interrupt_work);
121         disable_irq_nosync(irq);
122         return IRQ_HANDLED;
123 }
124
125 /* Ack the irq line for an assigned device */
126 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
127 {
128         struct kvm_assigned_dev_kernel *dev;
129
130         if (kian->gsi == -1)
131                 return;
132
133         dev = container_of(kian, struct kvm_assigned_dev_kernel,
134                            ack_notifier);
135         kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
136         enable_irq(dev->host_irq);
137 }
138
139 static void kvm_free_assigned_device(struct kvm *kvm,
140                                      struct kvm_assigned_dev_kernel
141                                      *assigned_dev)
142 {
143         if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
144                 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
145
146         kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
147         kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
148
149         if (cancel_work_sync(&assigned_dev->interrupt_work))
150                 /* We had pending work. That means we will have to take
151                  * care of kvm_put_kvm.
152                  */
153                 kvm_put_kvm(kvm);
154
155         pci_reset_function(assigned_dev->dev);
156
157         pci_release_regions(assigned_dev->dev);
158         pci_disable_device(assigned_dev->dev);
159         pci_dev_put(assigned_dev->dev);
160
161         list_del(&assigned_dev->list);
162         kfree(assigned_dev);
163 }
164
165 void kvm_free_all_assigned_devices(struct kvm *kvm)
166 {
167         struct list_head *ptr, *ptr2;
168         struct kvm_assigned_dev_kernel *assigned_dev;
169
170         list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
171                 assigned_dev = list_entry(ptr,
172                                           struct kvm_assigned_dev_kernel,
173                                           list);
174
175                 kvm_free_assigned_device(kvm, assigned_dev);
176         }
177 }
178
179 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
180                                    struct kvm_assigned_irq
181                                    *assigned_irq)
182 {
183         int r = 0;
184         struct kvm_assigned_dev_kernel *match;
185
186         mutex_lock(&kvm->lock);
187
188         match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
189                                       assigned_irq->assigned_dev_id);
190         if (!match) {
191                 mutex_unlock(&kvm->lock);
192                 return -EINVAL;
193         }
194
195         if (!match->irq_requested) {
196                 INIT_WORK(&match->interrupt_work,
197                                 kvm_assigned_dev_interrupt_work_handler);
198                 if (irqchip_in_kernel(kvm)) {
199                         /* Register ack nofitier */
200                         match->ack_notifier.gsi = -1;
201                         match->ack_notifier.irq_acked =
202                                         kvm_assigned_dev_ack_irq;
203                         kvm_register_irq_ack_notifier(kvm,
204                                         &match->ack_notifier);
205
206                         /* Request IRQ source ID */
207                         r = kvm_request_irq_source_id(kvm);
208                         if (r < 0)
209                                 goto out_release;
210                         else
211                                 match->irq_source_id = r;
212                 }
213         } else {
214                 match->guest_irq = assigned_irq->guest_irq;
215                 match->ack_notifier.gsi = assigned_irq->guest_irq;
216                 mutex_unlock(&kvm->lock);
217                 return 0;
218         }
219
220         if (irqchip_in_kernel(kvm)) {
221                 if (!capable(CAP_SYS_RAWIO)) {
222                         r = -EPERM;
223                         goto out_release;
224                 }
225
226                 if (assigned_irq->host_irq)
227                         match->host_irq = assigned_irq->host_irq;
228                 else
229                         match->host_irq = match->dev->irq;
230                 match->guest_irq = assigned_irq->guest_irq;
231                 match->ack_notifier.gsi = assigned_irq->guest_irq;
232
233                 /* Even though this is PCI, we don't want to use shared
234                  * interrupts. Sharing host devices with guest-assigned devices
235                  * on the same interrupt line is not a happy situation: there
236                  * are going to be long delays in accepting, acking, etc.
237                  */
238                 if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
239                                 "kvm_assigned_device", (void *)match)) {
240                         r = -EIO;
241                         goto out_release;
242                 }
243         }
244
245         match->irq_requested = true;
246         mutex_unlock(&kvm->lock);
247         return r;
248 out_release:
249         mutex_unlock(&kvm->lock);
250         kvm_free_assigned_device(kvm, match);
251         return r;
252 }
253
254 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
255                                       struct kvm_assigned_pci_dev *assigned_dev)
256 {
257         int r = 0;
258         struct kvm_assigned_dev_kernel *match;
259         struct pci_dev *dev;
260
261         mutex_lock(&kvm->lock);
262
263         match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
264                                       assigned_dev->assigned_dev_id);
265         if (match) {
266                 /* device already assigned */
267                 r = -EINVAL;
268                 goto out;
269         }
270
271         match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
272         if (match == NULL) {
273                 printk(KERN_INFO "%s: Couldn't allocate memory\n",
274                        __func__);
275                 r = -ENOMEM;
276                 goto out;
277         }
278         dev = pci_get_bus_and_slot(assigned_dev->busnr,
279                                    assigned_dev->devfn);
280         if (!dev) {
281                 printk(KERN_INFO "%s: host device not found\n", __func__);
282                 r = -EINVAL;
283                 goto out_free;
284         }
285         if (pci_enable_device(dev)) {
286                 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
287                 r = -EBUSY;
288                 goto out_put;
289         }
290         r = pci_request_regions(dev, "kvm_assigned_device");
291         if (r) {
292                 printk(KERN_INFO "%s: Could not get access to device regions\n",
293                        __func__);
294                 goto out_disable;
295         }
296
297         pci_reset_function(dev);
298
299         match->assigned_dev_id = assigned_dev->assigned_dev_id;
300         match->host_busnr = assigned_dev->busnr;
301         match->host_devfn = assigned_dev->devfn;
302         match->dev = dev;
303
304         match->kvm = kvm;
305
306         list_add(&match->list, &kvm->arch.assigned_dev_head);
307
308         if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
309                 r = kvm_iommu_map_guest(kvm, match);
310                 if (r)
311                         goto out_list_del;
312         }
313
314 out:
315         mutex_unlock(&kvm->lock);
316         return r;
317 out_list_del:
318         list_del(&match->list);
319         pci_release_regions(dev);
320 out_disable:
321         pci_disable_device(dev);
322 out_put:
323         pci_dev_put(dev);
324 out_free:
325         kfree(match);
326         mutex_unlock(&kvm->lock);
327         return r;
328 }
329 #endif
330
331 static inline int valid_vcpu(int n)
332 {
333         return likely(n >= 0 && n < KVM_MAX_VCPUS);
334 }
335
336 inline int kvm_is_mmio_pfn(pfn_t pfn)
337 {
338         if (pfn_valid(pfn))
339                 return PageReserved(pfn_to_page(pfn));
340
341         return true;
342 }
343
344 /*
345  * Switches to specified vcpu, until a matching vcpu_put()
346  */
347 void vcpu_load(struct kvm_vcpu *vcpu)
348 {
349         int cpu;
350
351         mutex_lock(&vcpu->mutex);
352         cpu = get_cpu();
353         preempt_notifier_register(&vcpu->preempt_notifier);
354         kvm_arch_vcpu_load(vcpu, cpu);
355         put_cpu();
356 }
357
358 void vcpu_put(struct kvm_vcpu *vcpu)
359 {
360         preempt_disable();
361         kvm_arch_vcpu_put(vcpu);
362         preempt_notifier_unregister(&vcpu->preempt_notifier);
363         preempt_enable();
364         mutex_unlock(&vcpu->mutex);
365 }
366
367 static void ack_flush(void *_completed)
368 {
369 }
370
371 void kvm_flush_remote_tlbs(struct kvm *kvm)
372 {
373         int i, cpu, me;
374         cpumask_t cpus;
375         struct kvm_vcpu *vcpu;
376
377         me = get_cpu();
378         cpus_clear(cpus);
379         for (i = 0; i < KVM_MAX_VCPUS; ++i) {
380                 vcpu = kvm->vcpus[i];
381                 if (!vcpu)
382                         continue;
383                 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
384                         continue;
385                 cpu = vcpu->cpu;
386                 if (cpu != -1 && cpu != me)
387                         cpu_set(cpu, cpus);
388         }
389         if (cpus_empty(cpus))
390                 goto out;
391         ++kvm->stat.remote_tlb_flush;
392         smp_call_function_mask(cpus, ack_flush, NULL, 1);
393 out:
394         put_cpu();
395 }
396
397 void kvm_reload_remote_mmus(struct kvm *kvm)
398 {
399         int i, cpu, me;
400         cpumask_t cpus;
401         struct kvm_vcpu *vcpu;
402
403         me = get_cpu();
404         cpus_clear(cpus);
405         for (i = 0; i < KVM_MAX_VCPUS; ++i) {
406                 vcpu = kvm->vcpus[i];
407                 if (!vcpu)
408                         continue;
409                 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
410                         continue;
411                 cpu = vcpu->cpu;
412                 if (cpu != -1 && cpu != me)
413                         cpu_set(cpu, cpus);
414         }
415         if (cpus_empty(cpus))
416                 goto out;
417         smp_call_function_mask(cpus, ack_flush, NULL, 1);
418 out:
419         put_cpu();
420 }
421
422
423 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
424 {
425         struct page *page;
426         int r;
427
428         mutex_init(&vcpu->mutex);
429         vcpu->cpu = -1;
430         vcpu->kvm = kvm;
431         vcpu->vcpu_id = id;
432         init_waitqueue_head(&vcpu->wq);
433
434         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
435         if (!page) {
436                 r = -ENOMEM;
437                 goto fail;
438         }
439         vcpu->run = page_address(page);
440
441         r = kvm_arch_vcpu_init(vcpu);
442         if (r < 0)
443                 goto fail_free_run;
444         return 0;
445
446 fail_free_run:
447         free_page((unsigned long)vcpu->run);
448 fail:
449         return r;
450 }
451 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
452
453 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
454 {
455         kvm_arch_vcpu_uninit(vcpu);
456         free_page((unsigned long)vcpu->run);
457 }
458 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
459
460 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
461 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
462 {
463         return container_of(mn, struct kvm, mmu_notifier);
464 }
465
466 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
467                                              struct mm_struct *mm,
468                                              unsigned long address)
469 {
470         struct kvm *kvm = mmu_notifier_to_kvm(mn);
471         int need_tlb_flush;
472
473         /*
474          * When ->invalidate_page runs, the linux pte has been zapped
475          * already but the page is still allocated until
476          * ->invalidate_page returns. So if we increase the sequence
477          * here the kvm page fault will notice if the spte can't be
478          * established because the page is going to be freed. If
479          * instead the kvm page fault establishes the spte before
480          * ->invalidate_page runs, kvm_unmap_hva will release it
481          * before returning.
482          *
483          * The sequence increase only need to be seen at spin_unlock
484          * time, and not at spin_lock time.
485          *
486          * Increasing the sequence after the spin_unlock would be
487          * unsafe because the kvm page fault could then establish the
488          * pte after kvm_unmap_hva returned, without noticing the page
489          * is going to be freed.
490          */
491         spin_lock(&kvm->mmu_lock);
492         kvm->mmu_notifier_seq++;
493         need_tlb_flush = kvm_unmap_hva(kvm, address);
494         spin_unlock(&kvm->mmu_lock);
495
496         /* we've to flush the tlb before the pages can be freed */
497         if (need_tlb_flush)
498                 kvm_flush_remote_tlbs(kvm);
499
500 }
501
502 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
503                                                     struct mm_struct *mm,
504                                                     unsigned long start,
505                                                     unsigned long end)
506 {
507         struct kvm *kvm = mmu_notifier_to_kvm(mn);
508         int need_tlb_flush = 0;
509
510         spin_lock(&kvm->mmu_lock);
511         /*
512          * The count increase must become visible at unlock time as no
513          * spte can be established without taking the mmu_lock and
514          * count is also read inside the mmu_lock critical section.
515          */
516         kvm->mmu_notifier_count++;
517         for (; start < end; start += PAGE_SIZE)
518                 need_tlb_flush |= kvm_unmap_hva(kvm, start);
519         spin_unlock(&kvm->mmu_lock);
520
521         /* we've to flush the tlb before the pages can be freed */
522         if (need_tlb_flush)
523                 kvm_flush_remote_tlbs(kvm);
524 }
525
526 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
527                                                   struct mm_struct *mm,
528                                                   unsigned long start,
529                                                   unsigned long end)
530 {
531         struct kvm *kvm = mmu_notifier_to_kvm(mn);
532
533         spin_lock(&kvm->mmu_lock);
534         /*
535          * This sequence increase will notify the kvm page fault that
536          * the page that is going to be mapped in the spte could have
537          * been freed.
538          */
539         kvm->mmu_notifier_seq++;
540         /*
541          * The above sequence increase must be visible before the
542          * below count decrease but both values are read by the kvm
543          * page fault under mmu_lock spinlock so we don't need to add
544          * a smb_wmb() here in between the two.
545          */
546         kvm->mmu_notifier_count--;
547         spin_unlock(&kvm->mmu_lock);
548
549         BUG_ON(kvm->mmu_notifier_count < 0);
550 }
551
552 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
553                                               struct mm_struct *mm,
554                                               unsigned long address)
555 {
556         struct kvm *kvm = mmu_notifier_to_kvm(mn);
557         int young;
558
559         spin_lock(&kvm->mmu_lock);
560         young = kvm_age_hva(kvm, address);
561         spin_unlock(&kvm->mmu_lock);
562
563         if (young)
564                 kvm_flush_remote_tlbs(kvm);
565
566         return young;
567 }
568
569 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
570         .invalidate_page        = kvm_mmu_notifier_invalidate_page,
571         .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
572         .invalidate_range_end   = kvm_mmu_notifier_invalidate_range_end,
573         .clear_flush_young      = kvm_mmu_notifier_clear_flush_young,
574 };
575 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
576
577 static struct kvm *kvm_create_vm(void)
578 {
579         struct kvm *kvm = kvm_arch_create_vm();
580 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
581         struct page *page;
582 #endif
583
584         if (IS_ERR(kvm))
585                 goto out;
586
587 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
588         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
589         if (!page) {
590                 kfree(kvm);
591                 return ERR_PTR(-ENOMEM);
592         }
593         kvm->coalesced_mmio_ring =
594                         (struct kvm_coalesced_mmio_ring *)page_address(page);
595 #endif
596
597 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
598         {
599                 int err;
600                 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
601                 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
602                 if (err) {
603 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
604                         put_page(page);
605 #endif
606                         kfree(kvm);
607                         return ERR_PTR(err);
608                 }
609         }
610 #endif
611
612         kvm->mm = current->mm;
613         atomic_inc(&kvm->mm->mm_count);
614         spin_lock_init(&kvm->mmu_lock);
615         kvm_io_bus_init(&kvm->pio_bus);
616         mutex_init(&kvm->lock);
617         kvm_io_bus_init(&kvm->mmio_bus);
618         init_rwsem(&kvm->slots_lock);
619         atomic_set(&kvm->users_count, 1);
620         spin_lock(&kvm_lock);
621         list_add(&kvm->vm_list, &vm_list);
622         spin_unlock(&kvm_lock);
623 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
624         kvm_coalesced_mmio_init(kvm);
625 #endif
626 out:
627         return kvm;
628 }
629
630 /*
631  * Free any memory in @free but not in @dont.
632  */
633 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
634                                   struct kvm_memory_slot *dont)
635 {
636         if (!dont || free->rmap != dont->rmap)
637                 vfree(free->rmap);
638
639         if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
640                 vfree(free->dirty_bitmap);
641
642         if (!dont || free->lpage_info != dont->lpage_info)
643                 vfree(free->lpage_info);
644
645         free->npages = 0;
646         free->dirty_bitmap = NULL;
647         free->rmap = NULL;
648         free->lpage_info = NULL;
649 }
650
651 void kvm_free_physmem(struct kvm *kvm)
652 {
653         int i;
654
655         for (i = 0; i < kvm->nmemslots; ++i)
656                 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
657 }
658
659 static void kvm_destroy_vm(struct kvm *kvm)
660 {
661         struct mm_struct *mm = kvm->mm;
662
663         spin_lock(&kvm_lock);
664         list_del(&kvm->vm_list);
665         spin_unlock(&kvm_lock);
666         kvm_io_bus_destroy(&kvm->pio_bus);
667         kvm_io_bus_destroy(&kvm->mmio_bus);
668 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
669         if (kvm->coalesced_mmio_ring != NULL)
670                 free_page((unsigned long)kvm->coalesced_mmio_ring);
671 #endif
672 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
673         mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
674 #endif
675         kvm_arch_destroy_vm(kvm);
676         mmdrop(mm);
677 }
678
679 void kvm_get_kvm(struct kvm *kvm)
680 {
681         atomic_inc(&kvm->users_count);
682 }
683 EXPORT_SYMBOL_GPL(kvm_get_kvm);
684
685 void kvm_put_kvm(struct kvm *kvm)
686 {
687         if (atomic_dec_and_test(&kvm->users_count))
688                 kvm_destroy_vm(kvm);
689 }
690 EXPORT_SYMBOL_GPL(kvm_put_kvm);
691
692
693 static int kvm_vm_release(struct inode *inode, struct file *filp)
694 {
695         struct kvm *kvm = filp->private_data;
696
697         kvm_put_kvm(kvm);
698         return 0;
699 }
700
701 /*
702  * Allocate some memory and give it an address in the guest physical address
703  * space.
704  *
705  * Discontiguous memory is allowed, mostly for framebuffers.
706  *
707  * Must be called holding mmap_sem for write.
708  */
709 int __kvm_set_memory_region(struct kvm *kvm,
710                             struct kvm_userspace_memory_region *mem,
711                             int user_alloc)
712 {
713         int r;
714         gfn_t base_gfn;
715         unsigned long npages;
716         unsigned long i;
717         struct kvm_memory_slot *memslot;
718         struct kvm_memory_slot old, new;
719
720         r = -EINVAL;
721         /* General sanity checks */
722         if (mem->memory_size & (PAGE_SIZE - 1))
723                 goto out;
724         if (mem->guest_phys_addr & (PAGE_SIZE - 1))
725                 goto out;
726         if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
727                 goto out;
728         if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
729                 goto out;
730         if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
731                 goto out;
732
733         memslot = &kvm->memslots[mem->slot];
734         base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
735         npages = mem->memory_size >> PAGE_SHIFT;
736
737         if (!npages)
738                 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
739
740         new = old = *memslot;
741
742         new.base_gfn = base_gfn;
743         new.npages = npages;
744         new.flags = mem->flags;
745
746         /* Disallow changing a memory slot's size. */
747         r = -EINVAL;
748         if (npages && old.npages && npages != old.npages)
749                 goto out_free;
750
751         /* Check for overlaps */
752         r = -EEXIST;
753         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
754                 struct kvm_memory_slot *s = &kvm->memslots[i];
755
756                 if (s == memslot)
757                         continue;
758                 if (!((base_gfn + npages <= s->base_gfn) ||
759                       (base_gfn >= s->base_gfn + s->npages)))
760                         goto out_free;
761         }
762
763         /* Free page dirty bitmap if unneeded */
764         if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
765                 new.dirty_bitmap = NULL;
766
767         r = -ENOMEM;
768
769         /* Allocate if a slot is being created */
770 #ifndef CONFIG_S390
771         if (npages && !new.rmap) {
772                 new.rmap = vmalloc(npages * sizeof(struct page *));
773
774                 if (!new.rmap)
775                         goto out_free;
776
777                 memset(new.rmap, 0, npages * sizeof(*new.rmap));
778
779                 new.user_alloc = user_alloc;
780                 /*
781                  * hva_to_rmmap() serialzies with the mmu_lock and to be
782                  * safe it has to ignore memslots with !user_alloc &&
783                  * !userspace_addr.
784                  */
785                 if (user_alloc)
786                         new.userspace_addr = mem->userspace_addr;
787                 else
788                         new.userspace_addr = 0;
789         }
790         if (npages && !new.lpage_info) {
791                 int largepages = npages / KVM_PAGES_PER_HPAGE;
792                 if (npages % KVM_PAGES_PER_HPAGE)
793                         largepages++;
794                 if (base_gfn % KVM_PAGES_PER_HPAGE)
795                         largepages++;
796
797                 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
798
799                 if (!new.lpage_info)
800                         goto out_free;
801
802                 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
803
804                 if (base_gfn % KVM_PAGES_PER_HPAGE)
805                         new.lpage_info[0].write_count = 1;
806                 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
807                         new.lpage_info[largepages-1].write_count = 1;
808         }
809
810         /* Allocate page dirty bitmap if needed */
811         if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
812                 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
813
814                 new.dirty_bitmap = vmalloc(dirty_bytes);
815                 if (!new.dirty_bitmap)
816                         goto out_free;
817                 memset(new.dirty_bitmap, 0, dirty_bytes);
818         }
819 #endif /* not defined CONFIG_S390 */
820
821         if (!npages)
822                 kvm_arch_flush_shadow(kvm);
823
824         spin_lock(&kvm->mmu_lock);
825         if (mem->slot >= kvm->nmemslots)
826                 kvm->nmemslots = mem->slot + 1;
827
828         *memslot = new;
829         spin_unlock(&kvm->mmu_lock);
830
831         r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
832         if (r) {
833                 spin_lock(&kvm->mmu_lock);
834                 *memslot = old;
835                 spin_unlock(&kvm->mmu_lock);
836                 goto out_free;
837         }
838
839         kvm_free_physmem_slot(&old, &new);
840 #ifdef CONFIG_DMAR
841         /* map the pages in iommu page table */
842         r = kvm_iommu_map_pages(kvm, base_gfn, npages);
843         if (r)
844                 goto out;
845 #endif
846         return 0;
847
848 out_free:
849         kvm_free_physmem_slot(&new, &old);
850 out:
851         return r;
852
853 }
854 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
855
856 int kvm_set_memory_region(struct kvm *kvm,
857                           struct kvm_userspace_memory_region *mem,
858                           int user_alloc)
859 {
860         int r;
861
862         down_write(&kvm->slots_lock);
863         r = __kvm_set_memory_region(kvm, mem, user_alloc);
864         up_write(&kvm->slots_lock);
865         return r;
866 }
867 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
868
869 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
870                                    struct
871                                    kvm_userspace_memory_region *mem,
872                                    int user_alloc)
873 {
874         if (mem->slot >= KVM_MEMORY_SLOTS)
875                 return -EINVAL;
876         return kvm_set_memory_region(kvm, mem, user_alloc);
877 }
878
879 int kvm_get_dirty_log(struct kvm *kvm,
880                         struct kvm_dirty_log *log, int *is_dirty)
881 {
882         struct kvm_memory_slot *memslot;
883         int r, i;
884         int n;
885         unsigned long any = 0;
886
887         r = -EINVAL;
888         if (log->slot >= KVM_MEMORY_SLOTS)
889                 goto out;
890
891         memslot = &kvm->memslots[log->slot];
892         r = -ENOENT;
893         if (!memslot->dirty_bitmap)
894                 goto out;
895
896         n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
897
898         for (i = 0; !any && i < n/sizeof(long); ++i)
899                 any = memslot->dirty_bitmap[i];
900
901         r = -EFAULT;
902         if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
903                 goto out;
904
905         if (any)
906                 *is_dirty = 1;
907
908         r = 0;
909 out:
910         return r;
911 }
912
913 int is_error_page(struct page *page)
914 {
915         return page == bad_page;
916 }
917 EXPORT_SYMBOL_GPL(is_error_page);
918
919 int is_error_pfn(pfn_t pfn)
920 {
921         return pfn == bad_pfn;
922 }
923 EXPORT_SYMBOL_GPL(is_error_pfn);
924
925 static inline unsigned long bad_hva(void)
926 {
927         return PAGE_OFFSET;
928 }
929
930 int kvm_is_error_hva(unsigned long addr)
931 {
932         return addr == bad_hva();
933 }
934 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
935
936 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
937 {
938         int i;
939
940         for (i = 0; i < kvm->nmemslots; ++i) {
941                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
942
943                 if (gfn >= memslot->base_gfn
944                     && gfn < memslot->base_gfn + memslot->npages)
945                         return memslot;
946         }
947         return NULL;
948 }
949 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
950
951 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
952 {
953         gfn = unalias_gfn(kvm, gfn);
954         return gfn_to_memslot_unaliased(kvm, gfn);
955 }
956
957 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
958 {
959         int i;
960
961         gfn = unalias_gfn(kvm, gfn);
962         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
963                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
964
965                 if (gfn >= memslot->base_gfn
966                     && gfn < memslot->base_gfn + memslot->npages)
967                         return 1;
968         }
969         return 0;
970 }
971 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
972
973 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
974 {
975         struct kvm_memory_slot *slot;
976
977         gfn = unalias_gfn(kvm, gfn);
978         slot = gfn_to_memslot_unaliased(kvm, gfn);
979         if (!slot)
980                 return bad_hva();
981         return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
982 }
983 EXPORT_SYMBOL_GPL(gfn_to_hva);
984
985 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
986 {
987         struct page *page[1];
988         unsigned long addr;
989         int npages;
990         pfn_t pfn;
991
992         might_sleep();
993
994         addr = gfn_to_hva(kvm, gfn);
995         if (kvm_is_error_hva(addr)) {
996                 get_page(bad_page);
997                 return page_to_pfn(bad_page);
998         }
999
1000         npages = get_user_pages_fast(addr, 1, 1, page);
1001
1002         if (unlikely(npages != 1)) {
1003                 struct vm_area_struct *vma;
1004
1005                 down_read(&current->mm->mmap_sem);
1006                 vma = find_vma(current->mm, addr);
1007
1008                 if (vma == NULL || addr < vma->vm_start ||
1009                     !(vma->vm_flags & VM_PFNMAP)) {
1010                         up_read(&current->mm->mmap_sem);
1011                         get_page(bad_page);
1012                         return page_to_pfn(bad_page);
1013                 }
1014
1015                 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1016                 up_read(&current->mm->mmap_sem);
1017                 BUG_ON(!kvm_is_mmio_pfn(pfn));
1018         } else
1019                 pfn = page_to_pfn(page[0]);
1020
1021         return pfn;
1022 }
1023
1024 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1025
1026 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1027 {
1028         pfn_t pfn;
1029
1030         pfn = gfn_to_pfn(kvm, gfn);
1031         if (!kvm_is_mmio_pfn(pfn))
1032                 return pfn_to_page(pfn);
1033
1034         WARN_ON(kvm_is_mmio_pfn(pfn));
1035
1036         get_page(bad_page);
1037         return bad_page;
1038 }
1039
1040 EXPORT_SYMBOL_GPL(gfn_to_page);
1041
1042 void kvm_release_page_clean(struct page *page)
1043 {
1044         kvm_release_pfn_clean(page_to_pfn(page));
1045 }
1046 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1047
1048 void kvm_release_pfn_clean(pfn_t pfn)
1049 {
1050         if (!kvm_is_mmio_pfn(pfn))
1051                 put_page(pfn_to_page(pfn));
1052 }
1053 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1054
1055 void kvm_release_page_dirty(struct page *page)
1056 {
1057         kvm_release_pfn_dirty(page_to_pfn(page));
1058 }
1059 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1060
1061 void kvm_release_pfn_dirty(pfn_t pfn)
1062 {
1063         kvm_set_pfn_dirty(pfn);
1064         kvm_release_pfn_clean(pfn);
1065 }
1066 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1067
1068 void kvm_set_page_dirty(struct page *page)
1069 {
1070         kvm_set_pfn_dirty(page_to_pfn(page));
1071 }
1072 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1073
1074 void kvm_set_pfn_dirty(pfn_t pfn)
1075 {
1076         if (!kvm_is_mmio_pfn(pfn)) {
1077                 struct page *page = pfn_to_page(pfn);
1078                 if (!PageReserved(page))
1079                         SetPageDirty(page);
1080         }
1081 }
1082 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1083
1084 void kvm_set_pfn_accessed(pfn_t pfn)
1085 {
1086         if (!kvm_is_mmio_pfn(pfn))
1087                 mark_page_accessed(pfn_to_page(pfn));
1088 }
1089 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1090
1091 void kvm_get_pfn(pfn_t pfn)
1092 {
1093         if (!kvm_is_mmio_pfn(pfn))
1094                 get_page(pfn_to_page(pfn));
1095 }
1096 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1097
1098 static int next_segment(unsigned long len, int offset)
1099 {
1100         if (len > PAGE_SIZE - offset)
1101                 return PAGE_SIZE - offset;
1102         else
1103                 return len;
1104 }
1105
1106 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1107                         int len)
1108 {
1109         int r;
1110         unsigned long addr;
1111
1112         addr = gfn_to_hva(kvm, gfn);
1113         if (kvm_is_error_hva(addr))
1114                 return -EFAULT;
1115         r = copy_from_user(data, (void __user *)addr + offset, len);
1116         if (r)
1117                 return -EFAULT;
1118         return 0;
1119 }
1120 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1121
1122 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1123 {
1124         gfn_t gfn = gpa >> PAGE_SHIFT;
1125         int seg;
1126         int offset = offset_in_page(gpa);
1127         int ret;
1128
1129         while ((seg = next_segment(len, offset)) != 0) {
1130                 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1131                 if (ret < 0)
1132                         return ret;
1133                 offset = 0;
1134                 len -= seg;
1135                 data += seg;
1136                 ++gfn;
1137         }
1138         return 0;
1139 }
1140 EXPORT_SYMBOL_GPL(kvm_read_guest);
1141
1142 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1143                           unsigned long len)
1144 {
1145         int r;
1146         unsigned long addr;
1147         gfn_t gfn = gpa >> PAGE_SHIFT;
1148         int offset = offset_in_page(gpa);
1149
1150         addr = gfn_to_hva(kvm, gfn);
1151         if (kvm_is_error_hva(addr))
1152                 return -EFAULT;
1153         pagefault_disable();
1154         r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1155         pagefault_enable();
1156         if (r)
1157                 return -EFAULT;
1158         return 0;
1159 }
1160 EXPORT_SYMBOL(kvm_read_guest_atomic);
1161
1162 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1163                          int offset, int len)
1164 {
1165         int r;
1166         unsigned long addr;
1167
1168         addr = gfn_to_hva(kvm, gfn);
1169         if (kvm_is_error_hva(addr))
1170                 return -EFAULT;
1171         r = copy_to_user((void __user *)addr + offset, data, len);
1172         if (r)
1173                 return -EFAULT;
1174         mark_page_dirty(kvm, gfn);
1175         return 0;
1176 }
1177 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1178
1179 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1180                     unsigned long len)
1181 {
1182         gfn_t gfn = gpa >> PAGE_SHIFT;
1183         int seg;
1184         int offset = offset_in_page(gpa);
1185         int ret;
1186
1187         while ((seg = next_segment(len, offset)) != 0) {
1188                 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1189                 if (ret < 0)
1190                         return ret;
1191                 offset = 0;
1192                 len -= seg;
1193                 data += seg;
1194                 ++gfn;
1195         }
1196         return 0;
1197 }
1198
1199 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1200 {
1201         return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1202 }
1203 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1204
1205 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1206 {
1207         gfn_t gfn = gpa >> PAGE_SHIFT;
1208         int seg;
1209         int offset = offset_in_page(gpa);
1210         int ret;
1211
1212         while ((seg = next_segment(len, offset)) != 0) {
1213                 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1214                 if (ret < 0)
1215                         return ret;
1216                 offset = 0;
1217                 len -= seg;
1218                 ++gfn;
1219         }
1220         return 0;
1221 }
1222 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1223
1224 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1225 {
1226         struct kvm_memory_slot *memslot;
1227
1228         gfn = unalias_gfn(kvm, gfn);
1229         memslot = gfn_to_memslot_unaliased(kvm, gfn);
1230         if (memslot && memslot->dirty_bitmap) {
1231                 unsigned long rel_gfn = gfn - memslot->base_gfn;
1232
1233                 /* avoid RMW */
1234                 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1235                         set_bit(rel_gfn, memslot->dirty_bitmap);
1236         }
1237 }
1238
1239 /*
1240  * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1241  */
1242 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1243 {
1244         DEFINE_WAIT(wait);
1245
1246         for (;;) {
1247                 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1248
1249                 if (kvm_cpu_has_interrupt(vcpu) ||
1250                     kvm_cpu_has_pending_timer(vcpu) ||
1251                     kvm_arch_vcpu_runnable(vcpu)) {
1252                         set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1253                         break;
1254                 }
1255                 if (signal_pending(current))
1256                         break;
1257
1258                 vcpu_put(vcpu);
1259                 schedule();
1260                 vcpu_load(vcpu);
1261         }
1262
1263         finish_wait(&vcpu->wq, &wait);
1264 }
1265
1266 void kvm_resched(struct kvm_vcpu *vcpu)
1267 {
1268         if (!need_resched())
1269                 return;
1270         cond_resched();
1271 }
1272 EXPORT_SYMBOL_GPL(kvm_resched);
1273
1274 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1275 {
1276         struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1277         struct page *page;
1278
1279         if (vmf->pgoff == 0)
1280                 page = virt_to_page(vcpu->run);
1281 #ifdef CONFIG_X86
1282         else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1283                 page = virt_to_page(vcpu->arch.pio_data);
1284 #endif
1285 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1286         else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1287                 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1288 #endif
1289         else
1290                 return VM_FAULT_SIGBUS;
1291         get_page(page);
1292         vmf->page = page;
1293         return 0;
1294 }
1295
1296 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1297         .fault = kvm_vcpu_fault,
1298 };
1299
1300 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1301 {
1302         vma->vm_ops = &kvm_vcpu_vm_ops;
1303         return 0;
1304 }
1305
1306 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1307 {
1308         struct kvm_vcpu *vcpu = filp->private_data;
1309
1310         kvm_put_kvm(vcpu->kvm);
1311         return 0;
1312 }
1313
1314 static const struct file_operations kvm_vcpu_fops = {
1315         .release        = kvm_vcpu_release,
1316         .unlocked_ioctl = kvm_vcpu_ioctl,
1317         .compat_ioctl   = kvm_vcpu_ioctl,
1318         .mmap           = kvm_vcpu_mmap,
1319 };
1320
1321 /*
1322  * Allocates an inode for the vcpu.
1323  */
1324 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1325 {
1326         int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1327         if (fd < 0)
1328                 kvm_put_kvm(vcpu->kvm);
1329         return fd;
1330 }
1331
1332 /*
1333  * Creates some virtual cpus.  Good luck creating more than one.
1334  */
1335 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1336 {
1337         int r;
1338         struct kvm_vcpu *vcpu;
1339
1340         if (!valid_vcpu(n))
1341                 return -EINVAL;
1342
1343         vcpu = kvm_arch_vcpu_create(kvm, n);
1344         if (IS_ERR(vcpu))
1345                 return PTR_ERR(vcpu);
1346
1347         preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1348
1349         r = kvm_arch_vcpu_setup(vcpu);
1350         if (r)
1351                 return r;
1352
1353         mutex_lock(&kvm->lock);
1354         if (kvm->vcpus[n]) {
1355                 r = -EEXIST;
1356                 goto vcpu_destroy;
1357         }
1358         kvm->vcpus[n] = vcpu;
1359         mutex_unlock(&kvm->lock);
1360
1361         /* Now it's all set up, let userspace reach it */
1362         kvm_get_kvm(kvm);
1363         r = create_vcpu_fd(vcpu);
1364         if (r < 0)
1365                 goto unlink;
1366         return r;
1367
1368 unlink:
1369         mutex_lock(&kvm->lock);
1370         kvm->vcpus[n] = NULL;
1371 vcpu_destroy:
1372         mutex_unlock(&kvm->lock);
1373         kvm_arch_vcpu_destroy(vcpu);
1374         return r;
1375 }
1376
1377 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1378 {
1379         if (sigset) {
1380                 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1381                 vcpu->sigset_active = 1;
1382                 vcpu->sigset = *sigset;
1383         } else
1384                 vcpu->sigset_active = 0;
1385         return 0;
1386 }
1387
1388 static long kvm_vcpu_ioctl(struct file *filp,
1389                            unsigned int ioctl, unsigned long arg)
1390 {
1391         struct kvm_vcpu *vcpu = filp->private_data;
1392         void __user *argp = (void __user *)arg;
1393         int r;
1394         struct kvm_fpu *fpu = NULL;
1395         struct kvm_sregs *kvm_sregs = NULL;
1396
1397         if (vcpu->kvm->mm != current->mm)
1398                 return -EIO;
1399         switch (ioctl) {
1400         case KVM_RUN:
1401                 r = -EINVAL;
1402                 if (arg)
1403                         goto out;
1404                 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1405                 break;
1406         case KVM_GET_REGS: {
1407                 struct kvm_regs *kvm_regs;
1408
1409                 r = -ENOMEM;
1410                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1411                 if (!kvm_regs)
1412                         goto out;
1413                 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1414                 if (r)
1415                         goto out_free1;
1416                 r = -EFAULT;
1417                 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1418                         goto out_free1;
1419                 r = 0;
1420 out_free1:
1421                 kfree(kvm_regs);
1422                 break;
1423         }
1424         case KVM_SET_REGS: {
1425                 struct kvm_regs *kvm_regs;
1426
1427                 r = -ENOMEM;
1428                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1429                 if (!kvm_regs)
1430                         goto out;
1431                 r = -EFAULT;
1432                 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1433                         goto out_free2;
1434                 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1435                 if (r)
1436                         goto out_free2;
1437                 r = 0;
1438 out_free2:
1439                 kfree(kvm_regs);
1440                 break;
1441         }
1442         case KVM_GET_SREGS: {
1443                 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1444                 r = -ENOMEM;
1445                 if (!kvm_sregs)
1446                         goto out;
1447                 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1448                 if (r)
1449                         goto out;
1450                 r = -EFAULT;
1451                 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1452                         goto out;
1453                 r = 0;
1454                 break;
1455         }
1456         case KVM_SET_SREGS: {
1457                 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1458                 r = -ENOMEM;
1459                 if (!kvm_sregs)
1460                         goto out;
1461                 r = -EFAULT;
1462                 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1463                         goto out;
1464                 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1465                 if (r)
1466                         goto out;
1467                 r = 0;
1468                 break;
1469         }
1470         case KVM_GET_MP_STATE: {
1471                 struct kvm_mp_state mp_state;
1472
1473                 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1474                 if (r)
1475                         goto out;
1476                 r = -EFAULT;
1477                 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1478                         goto out;
1479                 r = 0;
1480                 break;
1481         }
1482         case KVM_SET_MP_STATE: {
1483                 struct kvm_mp_state mp_state;
1484
1485                 r = -EFAULT;
1486                 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1487                         goto out;
1488                 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1489                 if (r)
1490                         goto out;
1491                 r = 0;
1492                 break;
1493         }
1494         case KVM_TRANSLATE: {
1495                 struct kvm_translation tr;
1496
1497                 r = -EFAULT;
1498                 if (copy_from_user(&tr, argp, sizeof tr))
1499                         goto out;
1500                 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1501                 if (r)
1502                         goto out;
1503                 r = -EFAULT;
1504                 if (copy_to_user(argp, &tr, sizeof tr))
1505                         goto out;
1506                 r = 0;
1507                 break;
1508         }
1509         case KVM_DEBUG_GUEST: {
1510                 struct kvm_debug_guest dbg;
1511
1512                 r = -EFAULT;
1513                 if (copy_from_user(&dbg, argp, sizeof dbg))
1514                         goto out;
1515                 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1516                 if (r)
1517                         goto out;
1518                 r = 0;
1519                 break;
1520         }
1521         case KVM_SET_SIGNAL_MASK: {
1522                 struct kvm_signal_mask __user *sigmask_arg = argp;
1523                 struct kvm_signal_mask kvm_sigmask;
1524                 sigset_t sigset, *p;
1525
1526                 p = NULL;
1527                 if (argp) {
1528                         r = -EFAULT;
1529                         if (copy_from_user(&kvm_sigmask, argp,
1530                                            sizeof kvm_sigmask))
1531                                 goto out;
1532                         r = -EINVAL;
1533                         if (kvm_sigmask.len != sizeof sigset)
1534                                 goto out;
1535                         r = -EFAULT;
1536                         if (copy_from_user(&sigset, sigmask_arg->sigset,
1537                                            sizeof sigset))
1538                                 goto out;
1539                         p = &sigset;
1540                 }
1541                 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1542                 break;
1543         }
1544         case KVM_GET_FPU: {
1545                 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1546                 r = -ENOMEM;
1547                 if (!fpu)
1548                         goto out;
1549                 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1550                 if (r)
1551                         goto out;
1552                 r = -EFAULT;
1553                 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1554                         goto out;
1555                 r = 0;
1556                 break;
1557         }
1558         case KVM_SET_FPU: {
1559                 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1560                 r = -ENOMEM;
1561                 if (!fpu)
1562                         goto out;
1563                 r = -EFAULT;
1564                 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1565                         goto out;
1566                 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1567                 if (r)
1568                         goto out;
1569                 r = 0;
1570                 break;
1571         }
1572         default:
1573                 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1574         }
1575 out:
1576         kfree(fpu);
1577         kfree(kvm_sregs);
1578         return r;
1579 }
1580
1581 static long kvm_vm_ioctl(struct file *filp,
1582                            unsigned int ioctl, unsigned long arg)
1583 {
1584         struct kvm *kvm = filp->private_data;
1585         void __user *argp = (void __user *)arg;
1586         int r;
1587
1588         if (kvm->mm != current->mm)
1589                 return -EIO;
1590         switch (ioctl) {
1591         case KVM_CREATE_VCPU:
1592                 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1593                 if (r < 0)
1594                         goto out;
1595                 break;
1596         case KVM_SET_USER_MEMORY_REGION: {
1597                 struct kvm_userspace_memory_region kvm_userspace_mem;
1598
1599                 r = -EFAULT;
1600                 if (copy_from_user(&kvm_userspace_mem, argp,
1601                                                 sizeof kvm_userspace_mem))
1602                         goto out;
1603
1604                 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1605                 if (r)
1606                         goto out;
1607                 break;
1608         }
1609         case KVM_GET_DIRTY_LOG: {
1610                 struct kvm_dirty_log log;
1611
1612                 r = -EFAULT;
1613                 if (copy_from_user(&log, argp, sizeof log))
1614                         goto out;
1615                 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1616                 if (r)
1617                         goto out;
1618                 break;
1619         }
1620 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1621         case KVM_REGISTER_COALESCED_MMIO: {
1622                 struct kvm_coalesced_mmio_zone zone;
1623                 r = -EFAULT;
1624                 if (copy_from_user(&zone, argp, sizeof zone))
1625                         goto out;
1626                 r = -ENXIO;
1627                 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1628                 if (r)
1629                         goto out;
1630                 r = 0;
1631                 break;
1632         }
1633         case KVM_UNREGISTER_COALESCED_MMIO: {
1634                 struct kvm_coalesced_mmio_zone zone;
1635                 r = -EFAULT;
1636                 if (copy_from_user(&zone, argp, sizeof zone))
1637                         goto out;
1638                 r = -ENXIO;
1639                 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1640                 if (r)
1641                         goto out;
1642                 r = 0;
1643                 break;
1644         }
1645 #endif
1646 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1647         case KVM_ASSIGN_PCI_DEVICE: {
1648                 struct kvm_assigned_pci_dev assigned_dev;
1649
1650                 r = -EFAULT;
1651                 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1652                         goto out;
1653                 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1654                 if (r)
1655                         goto out;
1656                 break;
1657         }
1658         case KVM_ASSIGN_IRQ: {
1659                 struct kvm_assigned_irq assigned_irq;
1660
1661                 r = -EFAULT;
1662                 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1663                         goto out;
1664                 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1665                 if (r)
1666                         goto out;
1667                 break;
1668         }
1669 #endif
1670         default:
1671                 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1672         }
1673 out:
1674         return r;
1675 }
1676
1677 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1678 {
1679         struct page *page[1];
1680         unsigned long addr;
1681         int npages;
1682         gfn_t gfn = vmf->pgoff;
1683         struct kvm *kvm = vma->vm_file->private_data;
1684
1685         addr = gfn_to_hva(kvm, gfn);
1686         if (kvm_is_error_hva(addr))
1687                 return VM_FAULT_SIGBUS;
1688
1689         npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1690                                 NULL);
1691         if (unlikely(npages != 1))
1692                 return VM_FAULT_SIGBUS;
1693
1694         vmf->page = page[0];
1695         return 0;
1696 }
1697
1698 static struct vm_operations_struct kvm_vm_vm_ops = {
1699         .fault = kvm_vm_fault,
1700 };
1701
1702 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1703 {
1704         vma->vm_ops = &kvm_vm_vm_ops;
1705         return 0;
1706 }
1707
1708 static const struct file_operations kvm_vm_fops = {
1709         .release        = kvm_vm_release,
1710         .unlocked_ioctl = kvm_vm_ioctl,
1711         .compat_ioctl   = kvm_vm_ioctl,
1712         .mmap           = kvm_vm_mmap,
1713 };
1714
1715 static int kvm_dev_ioctl_create_vm(void)
1716 {
1717         int fd;
1718         struct kvm *kvm;
1719
1720         kvm = kvm_create_vm();
1721         if (IS_ERR(kvm))
1722                 return PTR_ERR(kvm);
1723         fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1724         if (fd < 0)
1725                 kvm_put_kvm(kvm);
1726
1727         return fd;
1728 }
1729
1730 static long kvm_dev_ioctl(struct file *filp,
1731                           unsigned int ioctl, unsigned long arg)
1732 {
1733         long r = -EINVAL;
1734
1735         switch (ioctl) {
1736         case KVM_GET_API_VERSION:
1737                 r = -EINVAL;
1738                 if (arg)
1739                         goto out;
1740                 r = KVM_API_VERSION;
1741                 break;
1742         case KVM_CREATE_VM:
1743                 r = -EINVAL;
1744                 if (arg)
1745                         goto out;
1746                 r = kvm_dev_ioctl_create_vm();
1747                 break;
1748         case KVM_CHECK_EXTENSION:
1749                 r = kvm_dev_ioctl_check_extension(arg);
1750                 break;
1751         case KVM_GET_VCPU_MMAP_SIZE:
1752                 r = -EINVAL;
1753                 if (arg)
1754                         goto out;
1755                 r = PAGE_SIZE;     /* struct kvm_run */
1756 #ifdef CONFIG_X86
1757                 r += PAGE_SIZE;    /* pio data page */
1758 #endif
1759 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1760                 r += PAGE_SIZE;    /* coalesced mmio ring page */
1761 #endif
1762                 break;
1763         case KVM_TRACE_ENABLE:
1764         case KVM_TRACE_PAUSE:
1765         case KVM_TRACE_DISABLE:
1766                 r = kvm_trace_ioctl(ioctl, arg);
1767                 break;
1768         default:
1769                 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1770         }
1771 out:
1772         return r;
1773 }
1774
1775 static struct file_operations kvm_chardev_ops = {
1776         .unlocked_ioctl = kvm_dev_ioctl,
1777         .compat_ioctl   = kvm_dev_ioctl,
1778 };
1779
1780 static struct miscdevice kvm_dev = {
1781         KVM_MINOR,
1782         "kvm",
1783         &kvm_chardev_ops,
1784 };
1785
1786 static void hardware_enable(void *junk)
1787 {
1788         int cpu = raw_smp_processor_id();
1789
1790         if (cpu_isset(cpu, cpus_hardware_enabled))
1791                 return;
1792         cpu_set(cpu, cpus_hardware_enabled);
1793         kvm_arch_hardware_enable(NULL);
1794 }
1795
1796 static void hardware_disable(void *junk)
1797 {
1798         int cpu = raw_smp_processor_id();
1799
1800         if (!cpu_isset(cpu, cpus_hardware_enabled))
1801                 return;
1802         cpu_clear(cpu, cpus_hardware_enabled);
1803         kvm_arch_hardware_disable(NULL);
1804 }
1805
1806 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1807                            void *v)
1808 {
1809         int cpu = (long)v;
1810
1811         val &= ~CPU_TASKS_FROZEN;
1812         switch (val) {
1813         case CPU_DYING:
1814                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1815                        cpu);
1816                 hardware_disable(NULL);
1817                 break;
1818         case CPU_UP_CANCELED:
1819                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1820                        cpu);
1821                 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1822                 break;
1823         case CPU_ONLINE:
1824                 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1825                        cpu);
1826                 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1827                 break;
1828         }
1829         return NOTIFY_OK;
1830 }
1831
1832
1833 asmlinkage void kvm_handle_fault_on_reboot(void)
1834 {
1835         if (kvm_rebooting)
1836                 /* spin while reset goes on */
1837                 while (true)
1838                         ;
1839         /* Fault while not rebooting.  We want the trace. */
1840         BUG();
1841 }
1842 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1843
1844 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1845                       void *v)
1846 {
1847         if (val == SYS_RESTART) {
1848                 /*
1849                  * Some (well, at least mine) BIOSes hang on reboot if
1850                  * in vmx root mode.
1851                  */
1852                 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1853                 kvm_rebooting = true;
1854                 on_each_cpu(hardware_disable, NULL, 1);
1855         }
1856         return NOTIFY_OK;
1857 }
1858
1859 static struct notifier_block kvm_reboot_notifier = {
1860         .notifier_call = kvm_reboot,
1861         .priority = 0,
1862 };
1863
1864 void kvm_io_bus_init(struct kvm_io_bus *bus)
1865 {
1866         memset(bus, 0, sizeof(*bus));
1867 }
1868
1869 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1870 {
1871         int i;
1872
1873         for (i = 0; i < bus->dev_count; i++) {
1874                 struct kvm_io_device *pos = bus->devs[i];
1875
1876                 kvm_iodevice_destructor(pos);
1877         }
1878 }
1879
1880 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1881                                           gpa_t addr, int len, int is_write)
1882 {
1883         int i;
1884
1885         for (i = 0; i < bus->dev_count; i++) {
1886                 struct kvm_io_device *pos = bus->devs[i];
1887
1888                 if (pos->in_range(pos, addr, len, is_write))
1889                         return pos;
1890         }
1891
1892         return NULL;
1893 }
1894
1895 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1896 {
1897         BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1898
1899         bus->devs[bus->dev_count++] = dev;
1900 }
1901
1902 static struct notifier_block kvm_cpu_notifier = {
1903         .notifier_call = kvm_cpu_hotplug,
1904         .priority = 20, /* must be > scheduler priority */
1905 };
1906
1907 static int vm_stat_get(void *_offset, u64 *val)
1908 {
1909         unsigned offset = (long)_offset;
1910         struct kvm *kvm;
1911
1912         *val = 0;
1913         spin_lock(&kvm_lock);
1914         list_for_each_entry(kvm, &vm_list, vm_list)
1915                 *val += *(u32 *)((void *)kvm + offset);
1916         spin_unlock(&kvm_lock);
1917         return 0;
1918 }
1919
1920 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1921
1922 static int vcpu_stat_get(void *_offset, u64 *val)
1923 {
1924         unsigned offset = (long)_offset;
1925         struct kvm *kvm;
1926         struct kvm_vcpu *vcpu;
1927         int i;
1928
1929         *val = 0;
1930         spin_lock(&kvm_lock);
1931         list_for_each_entry(kvm, &vm_list, vm_list)
1932                 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1933                         vcpu = kvm->vcpus[i];
1934                         if (vcpu)
1935                                 *val += *(u32 *)((void *)vcpu + offset);
1936                 }
1937         spin_unlock(&kvm_lock);
1938         return 0;
1939 }
1940
1941 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1942
1943 static struct file_operations *stat_fops[] = {
1944         [KVM_STAT_VCPU] = &vcpu_stat_fops,
1945         [KVM_STAT_VM]   = &vm_stat_fops,
1946 };
1947
1948 static void kvm_init_debug(void)
1949 {
1950         struct kvm_stats_debugfs_item *p;
1951
1952         kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
1953         for (p = debugfs_entries; p->name; ++p)
1954                 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1955                                                 (void *)(long)p->offset,
1956                                                 stat_fops[p->kind]);
1957 }
1958
1959 static void kvm_exit_debug(void)
1960 {
1961         struct kvm_stats_debugfs_item *p;
1962
1963         for (p = debugfs_entries; p->name; ++p)
1964                 debugfs_remove(p->dentry);
1965         debugfs_remove(kvm_debugfs_dir);
1966 }
1967
1968 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1969 {
1970         hardware_disable(NULL);
1971         return 0;
1972 }
1973
1974 static int kvm_resume(struct sys_device *dev)
1975 {
1976         hardware_enable(NULL);
1977         return 0;
1978 }
1979
1980 static struct sysdev_class kvm_sysdev_class = {
1981         .name = "kvm",
1982         .suspend = kvm_suspend,
1983         .resume = kvm_resume,
1984 };
1985
1986 static struct sys_device kvm_sysdev = {
1987         .id = 0,
1988         .cls = &kvm_sysdev_class,
1989 };
1990
1991 struct page *bad_page;
1992 pfn_t bad_pfn;
1993
1994 static inline
1995 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1996 {
1997         return container_of(pn, struct kvm_vcpu, preempt_notifier);
1998 }
1999
2000 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2001 {
2002         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2003
2004         kvm_arch_vcpu_load(vcpu, cpu);
2005 }
2006
2007 static void kvm_sched_out(struct preempt_notifier *pn,
2008                           struct task_struct *next)
2009 {
2010         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2011
2012         kvm_arch_vcpu_put(vcpu);
2013 }
2014
2015 int kvm_init(void *opaque, unsigned int vcpu_size,
2016                   struct module *module)
2017 {
2018         int r;
2019         int cpu;
2020
2021         kvm_init_debug();
2022
2023         r = kvm_arch_init(opaque);
2024         if (r)
2025                 goto out_fail;
2026
2027         bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2028
2029         if (bad_page == NULL) {
2030                 r = -ENOMEM;
2031                 goto out;
2032         }
2033
2034         bad_pfn = page_to_pfn(bad_page);
2035
2036         r = kvm_arch_hardware_setup();
2037         if (r < 0)
2038                 goto out_free_0;
2039
2040         for_each_online_cpu(cpu) {
2041                 smp_call_function_single(cpu,
2042                                 kvm_arch_check_processor_compat,
2043                                 &r, 1);
2044                 if (r < 0)
2045                         goto out_free_1;
2046         }
2047
2048         on_each_cpu(hardware_enable, NULL, 1);
2049         r = register_cpu_notifier(&kvm_cpu_notifier);
2050         if (r)
2051                 goto out_free_2;
2052         register_reboot_notifier(&kvm_reboot_notifier);
2053
2054         r = sysdev_class_register(&kvm_sysdev_class);
2055         if (r)
2056                 goto out_free_3;
2057
2058         r = sysdev_register(&kvm_sysdev);
2059         if (r)
2060                 goto out_free_4;
2061
2062         /* A kmem cache lets us meet the alignment requirements of fx_save. */
2063         kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2064                                            __alignof__(struct kvm_vcpu),
2065                                            0, NULL);
2066         if (!kvm_vcpu_cache) {
2067                 r = -ENOMEM;
2068                 goto out_free_5;
2069         }
2070
2071         kvm_chardev_ops.owner = module;
2072
2073         r = misc_register(&kvm_dev);
2074         if (r) {
2075                 printk(KERN_ERR "kvm: misc device register failed\n");
2076                 goto out_free;
2077         }
2078
2079         kvm_preempt_ops.sched_in = kvm_sched_in;
2080         kvm_preempt_ops.sched_out = kvm_sched_out;
2081
2082         return 0;
2083
2084 out_free:
2085         kmem_cache_destroy(kvm_vcpu_cache);
2086 out_free_5:
2087         sysdev_unregister(&kvm_sysdev);
2088 out_free_4:
2089         sysdev_class_unregister(&kvm_sysdev_class);
2090 out_free_3:
2091         unregister_reboot_notifier(&kvm_reboot_notifier);
2092         unregister_cpu_notifier(&kvm_cpu_notifier);
2093 out_free_2:
2094         on_each_cpu(hardware_disable, NULL, 1);
2095 out_free_1:
2096         kvm_arch_hardware_unsetup();
2097 out_free_0:
2098         __free_page(bad_page);
2099 out:
2100         kvm_arch_exit();
2101         kvm_exit_debug();
2102 out_fail:
2103         return r;
2104 }
2105 EXPORT_SYMBOL_GPL(kvm_init);
2106
2107 void kvm_exit(void)
2108 {
2109         kvm_trace_cleanup();
2110         misc_deregister(&kvm_dev);
2111         kmem_cache_destroy(kvm_vcpu_cache);
2112         sysdev_unregister(&kvm_sysdev);
2113         sysdev_class_unregister(&kvm_sysdev_class);
2114         unregister_reboot_notifier(&kvm_reboot_notifier);
2115         unregister_cpu_notifier(&kvm_cpu_notifier);
2116         on_each_cpu(hardware_disable, NULL, 1);
2117         kvm_arch_hardware_unsetup();
2118         kvm_arch_exit();
2119         kvm_exit_debug();
2120         __free_page(bad_page);
2121 }
2122 EXPORT_SYMBOL_GPL(kvm_exit);