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