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