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