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