ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / virt / kvm / kvm_main.c
blob6b39ba9540e8cd417eba5c1004bd57b782cbf7c7
1 /*
2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
19 #include "iodev.h"
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/syscore_ops.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>
51 #include <asm/processor.h>
52 #include <asm/io.h>
53 #include <asm/uaccess.h>
54 #include <asm/pgtable.h>
56 #include "coalesced_mmio.h"
57 #include "async_pf.h"
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/kvm.h>
62 MODULE_AUTHOR("Qumranet");
63 MODULE_LICENSE("GPL");
66 * Ordering of locks:
68 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
71 DEFINE_RAW_SPINLOCK(kvm_lock);
72 LIST_HEAD(vm_list);
74 static cpumask_var_t cpus_hardware_enabled;
75 static int kvm_usage_count = 0;
76 static atomic_t hardware_enable_failed;
78 struct kmem_cache *kvm_vcpu_cache;
79 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
81 static __read_mostly struct preempt_ops kvm_preempt_ops;
83 struct dentry *kvm_debugfs_dir;
85 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
86 unsigned long arg);
87 static int hardware_enable_all(void);
88 static void hardware_disable_all(void);
90 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
92 bool kvm_rebooting;
93 EXPORT_SYMBOL_GPL(kvm_rebooting);
95 static bool largepages_enabled = true;
97 static struct page *hwpoison_page;
98 static pfn_t hwpoison_pfn;
100 static struct page *fault_page;
101 static pfn_t fault_pfn;
103 inline int kvm_is_mmio_pfn(pfn_t pfn)
105 if (pfn_valid(pfn)) {
106 int reserved;
107 struct page *tail = pfn_to_page(pfn);
108 struct page *head = compound_trans_head(tail);
109 reserved = PageReserved(head);
110 if (head != tail) {
112 * "head" is not a dangling pointer
113 * (compound_trans_head takes care of that)
114 * but the hugepage may have been splitted
115 * from under us (and we may not hold a
116 * reference count on the head page so it can
117 * be reused before we run PageReferenced), so
118 * we've to check PageTail before returning
119 * what we just read.
121 smp_rmb();
122 if (PageTail(tail))
123 return reserved;
125 return PageReserved(tail);
128 return true;
132 * Switches to specified vcpu, until a matching vcpu_put()
134 void vcpu_load(struct kvm_vcpu *vcpu)
136 int cpu;
138 mutex_lock(&vcpu->mutex);
139 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
140 /* The thread running this VCPU changed. */
141 struct pid *oldpid = vcpu->pid;
142 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
143 rcu_assign_pointer(vcpu->pid, newpid);
144 synchronize_rcu();
145 put_pid(oldpid);
147 cpu = get_cpu();
148 preempt_notifier_register(&vcpu->preempt_notifier);
149 kvm_arch_vcpu_load(vcpu, cpu);
150 put_cpu();
153 void vcpu_put(struct kvm_vcpu *vcpu)
155 preempt_disable();
156 kvm_arch_vcpu_put(vcpu);
157 preempt_notifier_unregister(&vcpu->preempt_notifier);
158 preempt_enable();
159 mutex_unlock(&vcpu->mutex);
162 static void ack_flush(void *_completed)
166 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
168 int i, cpu, me;
169 cpumask_var_t cpus;
170 bool called = true;
171 struct kvm_vcpu *vcpu;
173 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
175 me = get_cpu();
176 kvm_for_each_vcpu(i, vcpu, kvm) {
177 kvm_make_request(req, vcpu);
178 cpu = vcpu->cpu;
180 /* Set ->requests bit before we read ->mode */
181 smp_mb();
183 if (cpus != NULL && cpu != -1 && cpu != me &&
184 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
185 cpumask_set_cpu(cpu, cpus);
187 if (unlikely(cpus == NULL))
188 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
189 else if (!cpumask_empty(cpus))
190 smp_call_function_many(cpus, ack_flush, NULL, 1);
191 else
192 called = false;
193 put_cpu();
194 free_cpumask_var(cpus);
195 return called;
198 void kvm_flush_remote_tlbs(struct kvm *kvm)
200 int dirty_count = kvm->tlbs_dirty;
202 smp_mb();
203 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
204 ++kvm->stat.remote_tlb_flush;
205 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
208 void kvm_reload_remote_mmus(struct kvm *kvm)
210 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
213 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
215 struct page *page;
216 int r;
218 mutex_init(&vcpu->mutex);
219 vcpu->cpu = -1;
220 vcpu->kvm = kvm;
221 vcpu->vcpu_id = id;
222 vcpu->pid = NULL;
223 init_waitqueue_head(&vcpu->wq);
224 kvm_async_pf_vcpu_init(vcpu);
226 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
227 if (!page) {
228 r = -ENOMEM;
229 goto fail;
231 vcpu->run = page_address(page);
233 r = kvm_arch_vcpu_init(vcpu);
234 if (r < 0)
235 goto fail_free_run;
236 return 0;
238 fail_free_run:
239 free_page((unsigned long)vcpu->run);
240 fail:
241 return r;
243 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
245 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
247 put_pid(vcpu->pid);
248 kvm_arch_vcpu_uninit(vcpu);
249 free_page((unsigned long)vcpu->run);
251 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
253 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
254 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
256 return container_of(mn, struct kvm, mmu_notifier);
259 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
260 struct mm_struct *mm,
261 unsigned long address)
263 struct kvm *kvm = mmu_notifier_to_kvm(mn);
264 int need_tlb_flush, idx;
267 * When ->invalidate_page runs, the linux pte has been zapped
268 * already but the page is still allocated until
269 * ->invalidate_page returns. So if we increase the sequence
270 * here the kvm page fault will notice if the spte can't be
271 * established because the page is going to be freed. If
272 * instead the kvm page fault establishes the spte before
273 * ->invalidate_page runs, kvm_unmap_hva will release it
274 * before returning.
276 * The sequence increase only need to be seen at spin_unlock
277 * time, and not at spin_lock time.
279 * Increasing the sequence after the spin_unlock would be
280 * unsafe because the kvm page fault could then establish the
281 * pte after kvm_unmap_hva returned, without noticing the page
282 * is going to be freed.
284 idx = srcu_read_lock(&kvm->srcu);
285 spin_lock(&kvm->mmu_lock);
286 kvm->mmu_notifier_seq++;
287 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
288 spin_unlock(&kvm->mmu_lock);
289 srcu_read_unlock(&kvm->srcu, idx);
291 /* we've to flush the tlb before the pages can be freed */
292 if (need_tlb_flush)
293 kvm_flush_remote_tlbs(kvm);
297 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
298 struct mm_struct *mm,
299 unsigned long address,
300 pte_t pte)
302 struct kvm *kvm = mmu_notifier_to_kvm(mn);
303 int idx;
305 idx = srcu_read_lock(&kvm->srcu);
306 spin_lock(&kvm->mmu_lock);
307 kvm->mmu_notifier_seq++;
308 kvm_set_spte_hva(kvm, address, pte);
309 spin_unlock(&kvm->mmu_lock);
310 srcu_read_unlock(&kvm->srcu, idx);
313 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
314 struct mm_struct *mm,
315 unsigned long start,
316 unsigned long end)
318 struct kvm *kvm = mmu_notifier_to_kvm(mn);
319 int need_tlb_flush = 0, idx;
321 idx = srcu_read_lock(&kvm->srcu);
322 spin_lock(&kvm->mmu_lock);
324 * The count increase must become visible at unlock time as no
325 * spte can be established without taking the mmu_lock and
326 * count is also read inside the mmu_lock critical section.
328 kvm->mmu_notifier_count++;
329 for (; start < end; start += PAGE_SIZE)
330 need_tlb_flush |= kvm_unmap_hva(kvm, start);
331 need_tlb_flush |= kvm->tlbs_dirty;
332 spin_unlock(&kvm->mmu_lock);
333 srcu_read_unlock(&kvm->srcu, idx);
335 /* we've to flush the tlb before the pages can be freed */
336 if (need_tlb_flush)
337 kvm_flush_remote_tlbs(kvm);
340 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
341 struct mm_struct *mm,
342 unsigned long start,
343 unsigned long end)
345 struct kvm *kvm = mmu_notifier_to_kvm(mn);
347 spin_lock(&kvm->mmu_lock);
349 * This sequence increase will notify the kvm page fault that
350 * the page that is going to be mapped in the spte could have
351 * been freed.
353 kvm->mmu_notifier_seq++;
355 * The above sequence increase must be visible before the
356 * below count decrease but both values are read by the kvm
357 * page fault under mmu_lock spinlock so we don't need to add
358 * a smb_wmb() here in between the two.
360 kvm->mmu_notifier_count--;
361 spin_unlock(&kvm->mmu_lock);
363 BUG_ON(kvm->mmu_notifier_count < 0);
366 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
367 struct mm_struct *mm,
368 unsigned long address)
370 struct kvm *kvm = mmu_notifier_to_kvm(mn);
371 int young, idx;
373 idx = srcu_read_lock(&kvm->srcu);
374 spin_lock(&kvm->mmu_lock);
375 young = kvm_age_hva(kvm, address);
376 spin_unlock(&kvm->mmu_lock);
377 srcu_read_unlock(&kvm->srcu, idx);
379 if (young)
380 kvm_flush_remote_tlbs(kvm);
382 return young;
385 static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
386 struct mm_struct *mm,
387 unsigned long address)
389 struct kvm *kvm = mmu_notifier_to_kvm(mn);
390 int young, idx;
392 idx = srcu_read_lock(&kvm->srcu);
393 spin_lock(&kvm->mmu_lock);
394 young = kvm_test_age_hva(kvm, address);
395 spin_unlock(&kvm->mmu_lock);
396 srcu_read_unlock(&kvm->srcu, idx);
398 return young;
401 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
402 struct mm_struct *mm)
404 struct kvm *kvm = mmu_notifier_to_kvm(mn);
405 int idx;
407 idx = srcu_read_lock(&kvm->srcu);
408 kvm_arch_flush_shadow(kvm);
409 srcu_read_unlock(&kvm->srcu, idx);
412 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
413 .invalidate_page = kvm_mmu_notifier_invalidate_page,
414 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
415 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
416 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
417 .test_young = kvm_mmu_notifier_test_young,
418 .change_pte = kvm_mmu_notifier_change_pte,
419 .release = kvm_mmu_notifier_release,
422 static int kvm_init_mmu_notifier(struct kvm *kvm)
424 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
425 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
428 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
430 static int kvm_init_mmu_notifier(struct kvm *kvm)
432 return 0;
435 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
437 static struct kvm *kvm_create_vm(void)
439 int r, i;
440 struct kvm *kvm = kvm_arch_alloc_vm();
442 if (!kvm)
443 return ERR_PTR(-ENOMEM);
445 r = kvm_arch_init_vm(kvm);
446 if (r)
447 goto out_err_nodisable;
449 r = hardware_enable_all();
450 if (r)
451 goto out_err_nodisable;
453 #ifdef CONFIG_HAVE_KVM_IRQCHIP
454 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
455 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
456 #endif
458 r = -ENOMEM;
459 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
460 if (!kvm->memslots)
461 goto out_err_nosrcu;
462 if (init_srcu_struct(&kvm->srcu))
463 goto out_err_nosrcu;
464 for (i = 0; i < KVM_NR_BUSES; i++) {
465 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
466 GFP_KERNEL);
467 if (!kvm->buses[i])
468 goto out_err;
471 spin_lock_init(&kvm->mmu_lock);
472 kvm->mm = current->mm;
473 atomic_inc(&kvm->mm->mm_count);
474 kvm_eventfd_init(kvm);
475 mutex_init(&kvm->lock);
476 mutex_init(&kvm->irq_lock);
477 mutex_init(&kvm->slots_lock);
478 atomic_set(&kvm->users_count, 1);
480 r = kvm_init_mmu_notifier(kvm);
481 if (r)
482 goto out_err;
484 raw_spin_lock(&kvm_lock);
485 list_add(&kvm->vm_list, &vm_list);
486 raw_spin_unlock(&kvm_lock);
488 return kvm;
490 out_err:
491 cleanup_srcu_struct(&kvm->srcu);
492 out_err_nosrcu:
493 hardware_disable_all();
494 out_err_nodisable:
495 for (i = 0; i < KVM_NR_BUSES; i++)
496 kfree(kvm->buses[i]);
497 kfree(kvm->memslots);
498 kvm_arch_free_vm(kvm);
499 return ERR_PTR(r);
502 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
504 if (!memslot->dirty_bitmap)
505 return;
507 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
508 vfree(memslot->dirty_bitmap_head);
509 else
510 kfree(memslot->dirty_bitmap_head);
512 memslot->dirty_bitmap = NULL;
513 memslot->dirty_bitmap_head = NULL;
517 * Free any memory in @free but not in @dont.
519 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
520 struct kvm_memory_slot *dont)
522 int i;
524 if (!dont || free->rmap != dont->rmap)
525 vfree(free->rmap);
527 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
528 kvm_destroy_dirty_bitmap(free);
531 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
532 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
533 vfree(free->lpage_info[i]);
534 free->lpage_info[i] = NULL;
538 free->npages = 0;
539 free->rmap = NULL;
542 void kvm_free_physmem(struct kvm *kvm)
544 int i;
545 struct kvm_memslots *slots = kvm->memslots;
547 for (i = 0; i < slots->nmemslots; ++i)
548 kvm_free_physmem_slot(&slots->memslots[i], NULL);
550 kfree(kvm->memslots);
553 static void kvm_destroy_vm(struct kvm *kvm)
555 int i;
556 struct mm_struct *mm = kvm->mm;
558 kvm_arch_sync_events(kvm);
559 raw_spin_lock(&kvm_lock);
560 list_del(&kvm->vm_list);
561 raw_spin_unlock(&kvm_lock);
562 kvm_free_irq_routing(kvm);
563 for (i = 0; i < KVM_NR_BUSES; i++)
564 kvm_io_bus_destroy(kvm->buses[i]);
565 kvm_coalesced_mmio_free(kvm);
566 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
567 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
568 #else
569 kvm_arch_flush_shadow(kvm);
570 #endif
571 kvm_arch_destroy_vm(kvm);
572 kvm_free_physmem(kvm);
573 cleanup_srcu_struct(&kvm->srcu);
574 kvm_arch_free_vm(kvm);
575 hardware_disable_all();
576 mmdrop(mm);
579 void kvm_get_kvm(struct kvm *kvm)
581 atomic_inc(&kvm->users_count);
583 EXPORT_SYMBOL_GPL(kvm_get_kvm);
585 void kvm_put_kvm(struct kvm *kvm)
587 if (atomic_dec_and_test(&kvm->users_count))
588 kvm_destroy_vm(kvm);
590 EXPORT_SYMBOL_GPL(kvm_put_kvm);
593 static int kvm_vm_release(struct inode *inode, struct file *filp)
595 struct kvm *kvm = filp->private_data;
597 kvm_irqfd_release(kvm);
599 kvm_put_kvm(kvm);
600 return 0;
603 #ifndef CONFIG_S390
605 * Allocation size is twice as large as the actual dirty bitmap size.
606 * This makes it possible to do double buffering: see x86's
607 * kvm_vm_ioctl_get_dirty_log().
609 static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
611 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
613 if (dirty_bytes > PAGE_SIZE)
614 memslot->dirty_bitmap = vzalloc(dirty_bytes);
615 else
616 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
618 if (!memslot->dirty_bitmap)
619 return -ENOMEM;
621 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
622 return 0;
624 #endif /* !CONFIG_S390 */
627 * Allocate some memory and give it an address in the guest physical address
628 * space.
630 * Discontiguous memory is allowed, mostly for framebuffers.
632 * Must be called holding mmap_sem for write.
634 int __kvm_set_memory_region(struct kvm *kvm,
635 struct kvm_userspace_memory_region *mem,
636 int user_alloc)
638 int r;
639 gfn_t base_gfn;
640 unsigned long npages;
641 unsigned long i;
642 struct kvm_memory_slot *memslot;
643 struct kvm_memory_slot old, new;
644 struct kvm_memslots *slots, *old_memslots;
646 r = -EINVAL;
647 /* General sanity checks */
648 if (mem->memory_size & (PAGE_SIZE - 1))
649 goto out;
650 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
651 goto out;
652 /* We can read the guest memory with __xxx_user() later on. */
653 if (user_alloc &&
654 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
655 !access_ok(VERIFY_WRITE,
656 (void __user *)(unsigned long)mem->userspace_addr,
657 mem->memory_size)))
658 goto out;
659 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
660 goto out;
661 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
662 goto out;
664 memslot = &kvm->memslots->memslots[mem->slot];
665 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
666 npages = mem->memory_size >> PAGE_SHIFT;
668 r = -EINVAL;
669 if (npages > KVM_MEM_MAX_NR_PAGES)
670 goto out;
672 if (!npages)
673 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
675 new = old = *memslot;
677 new.id = mem->slot;
678 new.base_gfn = base_gfn;
679 new.npages = npages;
680 new.flags = mem->flags;
682 /* Disallow changing a memory slot's size. */
683 r = -EINVAL;
684 if (npages && old.npages && npages != old.npages)
685 goto out_free;
687 /* Check for overlaps */
688 r = -EEXIST;
689 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
690 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
692 if (s == memslot || !s->npages)
693 continue;
694 if (!((base_gfn + npages <= s->base_gfn) ||
695 (base_gfn >= s->base_gfn + s->npages)))
696 goto out_free;
699 /* Free page dirty bitmap if unneeded */
700 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
701 new.dirty_bitmap = NULL;
703 r = -ENOMEM;
705 /* Allocate if a slot is being created */
706 #ifndef CONFIG_S390
707 if (npages && !new.rmap) {
708 new.rmap = vzalloc(npages * sizeof(*new.rmap));
710 if (!new.rmap)
711 goto out_free;
713 new.user_alloc = user_alloc;
714 new.userspace_addr = mem->userspace_addr;
716 if (!npages)
717 goto skip_lpage;
719 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
720 unsigned long ugfn;
721 unsigned long j;
722 int lpages;
723 int level = i + 2;
725 /* Avoid unused variable warning if no large pages */
726 (void)level;
728 if (new.lpage_info[i])
729 continue;
731 lpages = 1 + ((base_gfn + npages - 1)
732 >> KVM_HPAGE_GFN_SHIFT(level));
733 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
735 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
737 if (!new.lpage_info[i])
738 goto out_free;
740 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
741 new.lpage_info[i][0].write_count = 1;
742 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
743 new.lpage_info[i][lpages - 1].write_count = 1;
744 ugfn = new.userspace_addr >> PAGE_SHIFT;
746 * If the gfn and userspace address are not aligned wrt each
747 * other, or if explicitly asked to, disable large page
748 * support for this slot
750 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
751 !largepages_enabled)
752 for (j = 0; j < lpages; ++j)
753 new.lpage_info[i][j].write_count = 1;
756 skip_lpage:
758 /* Allocate page dirty bitmap if needed */
759 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
760 if (kvm_create_dirty_bitmap(&new) < 0)
761 goto out_free;
762 /* destroy any largepage mappings for dirty tracking */
764 #else /* not defined CONFIG_S390 */
765 new.user_alloc = user_alloc;
766 if (user_alloc)
767 new.userspace_addr = mem->userspace_addr;
768 #endif /* not defined CONFIG_S390 */
770 if (!npages) {
771 r = -ENOMEM;
772 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
773 if (!slots)
774 goto out_free;
775 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
776 if (mem->slot >= slots->nmemslots)
777 slots->nmemslots = mem->slot + 1;
778 slots->generation++;
779 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
781 old_memslots = kvm->memslots;
782 rcu_assign_pointer(kvm->memslots, slots);
783 synchronize_srcu_expedited(&kvm->srcu);
784 /* From this point no new shadow pages pointing to a deleted
785 * memslot will be created.
787 * validation of sp->gfn happens in:
788 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
789 * - kvm_is_visible_gfn (mmu_check_roots)
791 kvm_arch_flush_shadow(kvm);
792 kfree(old_memslots);
795 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
796 if (r)
797 goto out_free;
799 /* map/unmap the pages in iommu page table */
800 if (npages) {
801 r = kvm_iommu_map_pages(kvm, &new);
802 if (r)
803 goto out_free;
804 } else
805 kvm_iommu_unmap_pages(kvm, &old);
807 r = -ENOMEM;
808 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
809 if (!slots)
810 goto out_free;
811 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
812 if (mem->slot >= slots->nmemslots)
813 slots->nmemslots = mem->slot + 1;
814 slots->generation++;
816 /* actual memory is freed via old in kvm_free_physmem_slot below */
817 if (!npages) {
818 new.rmap = NULL;
819 new.dirty_bitmap = NULL;
820 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
821 new.lpage_info[i] = NULL;
824 slots->memslots[mem->slot] = new;
825 old_memslots = kvm->memslots;
826 rcu_assign_pointer(kvm->memslots, slots);
827 synchronize_srcu_expedited(&kvm->srcu);
829 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
831 kvm_free_physmem_slot(&old, &new);
832 kfree(old_memslots);
834 return 0;
836 out_free:
837 kvm_free_physmem_slot(&new, &old);
838 out:
839 return r;
842 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
844 int kvm_set_memory_region(struct kvm *kvm,
845 struct kvm_userspace_memory_region *mem,
846 int user_alloc)
848 int r;
850 mutex_lock(&kvm->slots_lock);
851 r = __kvm_set_memory_region(kvm, mem, user_alloc);
852 mutex_unlock(&kvm->slots_lock);
853 return r;
855 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
857 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
858 struct
859 kvm_userspace_memory_region *mem,
860 int user_alloc)
862 if (mem->slot >= KVM_MEMORY_SLOTS)
863 return -EINVAL;
864 return kvm_set_memory_region(kvm, mem, user_alloc);
867 int kvm_get_dirty_log(struct kvm *kvm,
868 struct kvm_dirty_log *log, int *is_dirty)
870 struct kvm_memory_slot *memslot;
871 int r, i;
872 unsigned long n;
873 unsigned long any = 0;
875 r = -EINVAL;
876 if (log->slot >= KVM_MEMORY_SLOTS)
877 goto out;
879 memslot = &kvm->memslots->memslots[log->slot];
880 r = -ENOENT;
881 if (!memslot->dirty_bitmap)
882 goto out;
884 n = kvm_dirty_bitmap_bytes(memslot);
886 for (i = 0; !any && i < n/sizeof(long); ++i)
887 any = memslot->dirty_bitmap[i];
889 r = -EFAULT;
890 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
891 goto out;
893 if (any)
894 *is_dirty = 1;
896 r = 0;
897 out:
898 return r;
901 void kvm_disable_largepages(void)
903 largepages_enabled = false;
905 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
907 int is_error_page(struct page *page)
909 return page == bad_page || page == hwpoison_page || page == fault_page;
911 EXPORT_SYMBOL_GPL(is_error_page);
913 int is_error_pfn(pfn_t pfn)
915 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
917 EXPORT_SYMBOL_GPL(is_error_pfn);
919 int is_hwpoison_pfn(pfn_t pfn)
921 return pfn == hwpoison_pfn;
923 EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
925 int is_fault_pfn(pfn_t pfn)
927 return pfn == fault_pfn;
929 EXPORT_SYMBOL_GPL(is_fault_pfn);
931 static inline unsigned long bad_hva(void)
933 return PAGE_OFFSET;
936 int kvm_is_error_hva(unsigned long addr)
938 return addr == bad_hva();
940 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
942 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
943 gfn_t gfn)
945 int i;
947 for (i = 0; i < slots->nmemslots; ++i) {
948 struct kvm_memory_slot *memslot = &slots->memslots[i];
950 if (gfn >= memslot->base_gfn
951 && gfn < memslot->base_gfn + memslot->npages)
952 return memslot;
954 return NULL;
957 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
959 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
961 EXPORT_SYMBOL_GPL(gfn_to_memslot);
963 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
965 int i;
966 struct kvm_memslots *slots = kvm_memslots(kvm);
968 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
969 struct kvm_memory_slot *memslot = &slots->memslots[i];
971 if (memslot->flags & KVM_MEMSLOT_INVALID)
972 continue;
974 if (gfn >= memslot->base_gfn
975 && gfn < memslot->base_gfn + memslot->npages)
976 return 1;
978 return 0;
980 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
982 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
984 struct vm_area_struct *vma;
985 unsigned long addr, size;
987 size = PAGE_SIZE;
989 addr = gfn_to_hva(kvm, gfn);
990 if (kvm_is_error_hva(addr))
991 return PAGE_SIZE;
993 down_read(&current->mm->mmap_sem);
994 vma = find_vma(current->mm, addr);
995 if (!vma)
996 goto out;
998 size = vma_kernel_pagesize(vma);
1000 out:
1001 up_read(&current->mm->mmap_sem);
1003 return size;
1006 static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1007 gfn_t *nr_pages)
1009 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
1010 return bad_hva();
1012 if (nr_pages)
1013 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1015 return gfn_to_hva_memslot(slot, gfn);
1018 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1020 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
1022 EXPORT_SYMBOL_GPL(gfn_to_hva);
1024 static pfn_t get_fault_pfn(void)
1026 get_page(fault_page);
1027 return fault_pfn;
1030 int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1031 unsigned long start, int write, struct page **page)
1033 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1035 if (write)
1036 flags |= FOLL_WRITE;
1038 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1041 static inline int check_user_page_hwpoison(unsigned long addr)
1043 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1045 rc = __get_user_pages(current, current->mm, addr, 1,
1046 flags, NULL, NULL, NULL);
1047 return rc == -EHWPOISON;
1050 static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
1051 bool *async, bool write_fault, bool *writable)
1053 struct page *page[1];
1054 int npages = 0;
1055 pfn_t pfn;
1057 /* we can do it either atomically or asynchronously, not both */
1058 BUG_ON(atomic && async);
1060 BUG_ON(!write_fault && !writable);
1062 if (writable)
1063 *writable = true;
1065 if (atomic || async)
1066 npages = __get_user_pages_fast(addr, 1, 1, page);
1068 if (unlikely(npages != 1) && !atomic) {
1069 might_sleep();
1071 if (writable)
1072 *writable = write_fault;
1074 if (async) {
1075 down_read(&current->mm->mmap_sem);
1076 npages = get_user_page_nowait(current, current->mm,
1077 addr, write_fault, page);
1078 up_read(&current->mm->mmap_sem);
1079 } else
1080 npages = get_user_pages_fast(addr, 1, write_fault,
1081 page);
1083 /* map read fault as writable if possible */
1084 if (unlikely(!write_fault) && npages == 1) {
1085 struct page *wpage[1];
1087 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1088 if (npages == 1) {
1089 *writable = true;
1090 put_page(page[0]);
1091 page[0] = wpage[0];
1093 npages = 1;
1097 if (unlikely(npages != 1)) {
1098 struct vm_area_struct *vma;
1100 if (atomic)
1101 return get_fault_pfn();
1103 down_read(&current->mm->mmap_sem);
1104 if (npages == -EHWPOISON ||
1105 (!async && check_user_page_hwpoison(addr))) {
1106 up_read(&current->mm->mmap_sem);
1107 get_page(hwpoison_page);
1108 return page_to_pfn(hwpoison_page);
1111 vma = find_vma_intersection(current->mm, addr, addr+1);
1113 if (vma == NULL)
1114 pfn = get_fault_pfn();
1115 else if ((vma->vm_flags & VM_PFNMAP)) {
1116 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1117 vma->vm_pgoff;
1118 BUG_ON(!kvm_is_mmio_pfn(pfn));
1119 } else {
1120 if (async && (vma->vm_flags & VM_WRITE))
1121 *async = true;
1122 pfn = get_fault_pfn();
1124 up_read(&current->mm->mmap_sem);
1125 } else
1126 pfn = page_to_pfn(page[0]);
1128 return pfn;
1131 pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1133 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
1135 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1137 static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1138 bool write_fault, bool *writable)
1140 unsigned long addr;
1142 if (async)
1143 *async = false;
1145 addr = gfn_to_hva(kvm, gfn);
1146 if (kvm_is_error_hva(addr)) {
1147 get_page(bad_page);
1148 return page_to_pfn(bad_page);
1151 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
1154 pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1156 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
1158 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1160 pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1161 bool write_fault, bool *writable)
1163 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
1165 EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1167 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1169 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
1171 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1173 pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1174 bool *writable)
1176 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1178 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1180 pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1181 struct kvm_memory_slot *slot, gfn_t gfn)
1183 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
1184 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
1187 int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1188 int nr_pages)
1190 unsigned long addr;
1191 gfn_t entry;
1193 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
1194 if (kvm_is_error_hva(addr))
1195 return -1;
1197 if (entry < nr_pages)
1198 return 0;
1200 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1202 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1204 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1206 pfn_t pfn;
1208 pfn = gfn_to_pfn(kvm, gfn);
1209 if (!kvm_is_mmio_pfn(pfn))
1210 return pfn_to_page(pfn);
1212 WARN_ON(kvm_is_mmio_pfn(pfn));
1214 get_page(bad_page);
1215 return bad_page;
1218 EXPORT_SYMBOL_GPL(gfn_to_page);
1220 void kvm_release_page_clean(struct page *page)
1222 kvm_release_pfn_clean(page_to_pfn(page));
1224 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1226 void kvm_release_pfn_clean(pfn_t pfn)
1228 if (!kvm_is_mmio_pfn(pfn))
1229 put_page(pfn_to_page(pfn));
1231 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1233 void kvm_release_page_dirty(struct page *page)
1235 kvm_release_pfn_dirty(page_to_pfn(page));
1237 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1239 void kvm_release_pfn_dirty(pfn_t pfn)
1241 kvm_set_pfn_dirty(pfn);
1242 kvm_release_pfn_clean(pfn);
1244 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1246 void kvm_set_page_dirty(struct page *page)
1248 kvm_set_pfn_dirty(page_to_pfn(page));
1250 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1252 void kvm_set_pfn_dirty(pfn_t pfn)
1254 if (!kvm_is_mmio_pfn(pfn)) {
1255 struct page *page = pfn_to_page(pfn);
1256 if (!PageReserved(page))
1257 SetPageDirty(page);
1260 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1262 void kvm_set_pfn_accessed(pfn_t pfn)
1264 if (!kvm_is_mmio_pfn(pfn))
1265 mark_page_accessed(pfn_to_page(pfn));
1267 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1269 void kvm_get_pfn(pfn_t pfn)
1271 if (!kvm_is_mmio_pfn(pfn))
1272 get_page(pfn_to_page(pfn));
1274 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1276 static int next_segment(unsigned long len, int offset)
1278 if (len > PAGE_SIZE - offset)
1279 return PAGE_SIZE - offset;
1280 else
1281 return len;
1284 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1285 int len)
1287 int r;
1288 unsigned long addr;
1290 addr = gfn_to_hva(kvm, gfn);
1291 if (kvm_is_error_hva(addr))
1292 return -EFAULT;
1293 r = __copy_from_user(data, (void __user *)addr + offset, len);
1294 if (r)
1295 return -EFAULT;
1296 return 0;
1298 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1300 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1302 gfn_t gfn = gpa >> PAGE_SHIFT;
1303 int seg;
1304 int offset = offset_in_page(gpa);
1305 int ret;
1307 while ((seg = next_segment(len, offset)) != 0) {
1308 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1309 if (ret < 0)
1310 return ret;
1311 offset = 0;
1312 len -= seg;
1313 data += seg;
1314 ++gfn;
1316 return 0;
1318 EXPORT_SYMBOL_GPL(kvm_read_guest);
1320 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1321 unsigned long len)
1323 int r;
1324 unsigned long addr;
1325 gfn_t gfn = gpa >> PAGE_SHIFT;
1326 int offset = offset_in_page(gpa);
1328 addr = gfn_to_hva(kvm, gfn);
1329 if (kvm_is_error_hva(addr))
1330 return -EFAULT;
1331 pagefault_disable();
1332 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1333 pagefault_enable();
1334 if (r)
1335 return -EFAULT;
1336 return 0;
1338 EXPORT_SYMBOL(kvm_read_guest_atomic);
1340 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1341 int offset, int len)
1343 int r;
1344 unsigned long addr;
1346 addr = gfn_to_hva(kvm, gfn);
1347 if (kvm_is_error_hva(addr))
1348 return -EFAULT;
1349 r = copy_to_user((void __user *)addr + offset, data, len);
1350 if (r)
1351 return -EFAULT;
1352 mark_page_dirty(kvm, gfn);
1353 return 0;
1355 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1357 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1358 unsigned long len)
1360 gfn_t gfn = gpa >> PAGE_SHIFT;
1361 int seg;
1362 int offset = offset_in_page(gpa);
1363 int ret;
1365 while ((seg = next_segment(len, offset)) != 0) {
1366 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1367 if (ret < 0)
1368 return ret;
1369 offset = 0;
1370 len -= seg;
1371 data += seg;
1372 ++gfn;
1374 return 0;
1377 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1378 gpa_t gpa)
1380 struct kvm_memslots *slots = kvm_memslots(kvm);
1381 int offset = offset_in_page(gpa);
1382 gfn_t gfn = gpa >> PAGE_SHIFT;
1384 ghc->gpa = gpa;
1385 ghc->generation = slots->generation;
1386 ghc->memslot = __gfn_to_memslot(slots, gfn);
1387 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1388 if (!kvm_is_error_hva(ghc->hva))
1389 ghc->hva += offset;
1390 else
1391 return -EFAULT;
1393 return 0;
1395 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1397 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1398 void *data, unsigned long len)
1400 struct kvm_memslots *slots = kvm_memslots(kvm);
1401 int r;
1403 if (slots->generation != ghc->generation)
1404 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1406 if (kvm_is_error_hva(ghc->hva))
1407 return -EFAULT;
1409 r = copy_to_user((void __user *)ghc->hva, data, len);
1410 if (r)
1411 return -EFAULT;
1412 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1414 return 0;
1416 EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1418 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1420 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1421 offset, len);
1423 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1425 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1427 gfn_t gfn = gpa >> PAGE_SHIFT;
1428 int seg;
1429 int offset = offset_in_page(gpa);
1430 int ret;
1432 while ((seg = next_segment(len, offset)) != 0) {
1433 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1434 if (ret < 0)
1435 return ret;
1436 offset = 0;
1437 len -= seg;
1438 ++gfn;
1440 return 0;
1442 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1444 void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1445 gfn_t gfn)
1447 if (memslot && memslot->dirty_bitmap) {
1448 unsigned long rel_gfn = gfn - memslot->base_gfn;
1450 __set_bit_le(rel_gfn, memslot->dirty_bitmap);
1454 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1456 struct kvm_memory_slot *memslot;
1458 memslot = gfn_to_memslot(kvm, gfn);
1459 mark_page_dirty_in_slot(kvm, memslot, gfn);
1463 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1465 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1467 DEFINE_WAIT(wait);
1469 for (;;) {
1470 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1472 if (kvm_arch_vcpu_runnable(vcpu)) {
1473 kvm_make_request(KVM_REQ_UNHALT, vcpu);
1474 break;
1476 if (kvm_cpu_has_pending_timer(vcpu))
1477 break;
1478 if (signal_pending(current))
1479 break;
1481 schedule();
1484 finish_wait(&vcpu->wq, &wait);
1487 void kvm_resched(struct kvm_vcpu *vcpu)
1489 if (!need_resched())
1490 return;
1491 cond_resched();
1493 EXPORT_SYMBOL_GPL(kvm_resched);
1495 void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1497 struct kvm *kvm = me->kvm;
1498 struct kvm_vcpu *vcpu;
1499 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1500 int yielded = 0;
1501 int pass;
1502 int i;
1505 * We boost the priority of a VCPU that is runnable but not
1506 * currently running, because it got preempted by something
1507 * else and called schedule in __vcpu_run. Hopefully that
1508 * VCPU is holding the lock that we need and will release it.
1509 * We approximate round-robin by starting at the last boosted VCPU.
1511 for (pass = 0; pass < 2 && !yielded; pass++) {
1512 kvm_for_each_vcpu(i, vcpu, kvm) {
1513 struct task_struct *task = NULL;
1514 struct pid *pid;
1515 if (!pass && i < last_boosted_vcpu) {
1516 i = last_boosted_vcpu;
1517 continue;
1518 } else if (pass && i > last_boosted_vcpu)
1519 break;
1520 if (vcpu == me)
1521 continue;
1522 if (waitqueue_active(&vcpu->wq))
1523 continue;
1524 rcu_read_lock();
1525 pid = rcu_dereference(vcpu->pid);
1526 if (pid)
1527 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1528 rcu_read_unlock();
1529 if (!task)
1530 continue;
1531 if (task->flags & PF_VCPU) {
1532 put_task_struct(task);
1533 continue;
1535 if (yield_to(task, 1)) {
1536 put_task_struct(task);
1537 kvm->last_boosted_vcpu = i;
1538 yielded = 1;
1539 break;
1541 put_task_struct(task);
1545 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1547 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1549 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1550 struct page *page;
1552 if (vmf->pgoff == 0)
1553 page = virt_to_page(vcpu->run);
1554 #ifdef CONFIG_X86
1555 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1556 page = virt_to_page(vcpu->arch.pio_data);
1557 #endif
1558 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1559 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1560 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1561 #endif
1562 else
1563 return VM_FAULT_SIGBUS;
1564 get_page(page);
1565 vmf->page = page;
1566 return 0;
1569 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1570 .fault = kvm_vcpu_fault,
1573 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1575 vma->vm_ops = &kvm_vcpu_vm_ops;
1576 return 0;
1579 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1581 struct kvm_vcpu *vcpu = filp->private_data;
1583 kvm_put_kvm(vcpu->kvm);
1584 return 0;
1587 static struct file_operations kvm_vcpu_fops = {
1588 .release = kvm_vcpu_release,
1589 .unlocked_ioctl = kvm_vcpu_ioctl,
1590 .compat_ioctl = kvm_vcpu_ioctl,
1591 .mmap = kvm_vcpu_mmap,
1592 .llseek = noop_llseek,
1596 * Allocates an inode for the vcpu.
1598 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1600 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
1604 * Creates some virtual cpus. Good luck creating more than one.
1606 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1608 int r;
1609 struct kvm_vcpu *vcpu, *v;
1611 vcpu = kvm_arch_vcpu_create(kvm, id);
1612 if (IS_ERR(vcpu))
1613 return PTR_ERR(vcpu);
1615 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1617 r = kvm_arch_vcpu_setup(vcpu);
1618 if (r)
1619 return r;
1621 mutex_lock(&kvm->lock);
1622 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1623 r = -EINVAL;
1624 goto vcpu_destroy;
1627 kvm_for_each_vcpu(r, v, kvm)
1628 if (v->vcpu_id == id) {
1629 r = -EEXIST;
1630 goto vcpu_destroy;
1633 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1635 /* Now it's all set up, let userspace reach it */
1636 kvm_get_kvm(kvm);
1637 r = create_vcpu_fd(vcpu);
1638 if (r < 0) {
1639 kvm_put_kvm(kvm);
1640 goto vcpu_destroy;
1643 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1644 smp_wmb();
1645 atomic_inc(&kvm->online_vcpus);
1647 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1648 if (kvm->bsp_vcpu_id == id)
1649 kvm->bsp_vcpu = vcpu;
1650 #endif
1651 mutex_unlock(&kvm->lock);
1652 return r;
1654 vcpu_destroy:
1655 mutex_unlock(&kvm->lock);
1656 kvm_arch_vcpu_destroy(vcpu);
1657 return r;
1660 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1662 if (sigset) {
1663 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1664 vcpu->sigset_active = 1;
1665 vcpu->sigset = *sigset;
1666 } else
1667 vcpu->sigset_active = 0;
1668 return 0;
1671 static long kvm_vcpu_ioctl(struct file *filp,
1672 unsigned int ioctl, unsigned long arg)
1674 struct kvm_vcpu *vcpu = filp->private_data;
1675 void __user *argp = (void __user *)arg;
1676 int r;
1677 struct kvm_fpu *fpu = NULL;
1678 struct kvm_sregs *kvm_sregs = NULL;
1680 if (vcpu->kvm->mm != current->mm)
1681 return -EIO;
1683 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1685 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1686 * so vcpu_load() would break it.
1688 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1689 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1690 #endif
1693 vcpu_load(vcpu);
1694 switch (ioctl) {
1695 case KVM_RUN:
1696 r = -EINVAL;
1697 if (arg)
1698 goto out;
1699 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1700 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
1701 break;
1702 case KVM_GET_REGS: {
1703 struct kvm_regs *kvm_regs;
1705 r = -ENOMEM;
1706 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1707 if (!kvm_regs)
1708 goto out;
1709 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1710 if (r)
1711 goto out_free1;
1712 r = -EFAULT;
1713 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1714 goto out_free1;
1715 r = 0;
1716 out_free1:
1717 kfree(kvm_regs);
1718 break;
1720 case KVM_SET_REGS: {
1721 struct kvm_regs *kvm_regs;
1723 r = -ENOMEM;
1724 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1725 if (!kvm_regs)
1726 goto out;
1727 r = -EFAULT;
1728 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1729 goto out_free2;
1730 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1731 if (r)
1732 goto out_free2;
1733 r = 0;
1734 out_free2:
1735 kfree(kvm_regs);
1736 break;
1738 case KVM_GET_SREGS: {
1739 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1740 r = -ENOMEM;
1741 if (!kvm_sregs)
1742 goto out;
1743 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1744 if (r)
1745 goto out;
1746 r = -EFAULT;
1747 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1748 goto out;
1749 r = 0;
1750 break;
1752 case KVM_SET_SREGS: {
1753 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1754 r = -ENOMEM;
1755 if (!kvm_sregs)
1756 goto out;
1757 r = -EFAULT;
1758 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1759 goto out;
1760 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1761 if (r)
1762 goto out;
1763 r = 0;
1764 break;
1766 case KVM_GET_MP_STATE: {
1767 struct kvm_mp_state mp_state;
1769 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1770 if (r)
1771 goto out;
1772 r = -EFAULT;
1773 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1774 goto out;
1775 r = 0;
1776 break;
1778 case KVM_SET_MP_STATE: {
1779 struct kvm_mp_state mp_state;
1781 r = -EFAULT;
1782 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1783 goto out;
1784 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1785 if (r)
1786 goto out;
1787 r = 0;
1788 break;
1790 case KVM_TRANSLATE: {
1791 struct kvm_translation tr;
1793 r = -EFAULT;
1794 if (copy_from_user(&tr, argp, sizeof tr))
1795 goto out;
1796 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1797 if (r)
1798 goto out;
1799 r = -EFAULT;
1800 if (copy_to_user(argp, &tr, sizeof tr))
1801 goto out;
1802 r = 0;
1803 break;
1805 case KVM_SET_GUEST_DEBUG: {
1806 struct kvm_guest_debug dbg;
1808 r = -EFAULT;
1809 if (copy_from_user(&dbg, argp, sizeof dbg))
1810 goto out;
1811 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1812 if (r)
1813 goto out;
1814 r = 0;
1815 break;
1817 case KVM_SET_SIGNAL_MASK: {
1818 struct kvm_signal_mask __user *sigmask_arg = argp;
1819 struct kvm_signal_mask kvm_sigmask;
1820 sigset_t sigset, *p;
1822 p = NULL;
1823 if (argp) {
1824 r = -EFAULT;
1825 if (copy_from_user(&kvm_sigmask, argp,
1826 sizeof kvm_sigmask))
1827 goto out;
1828 r = -EINVAL;
1829 if (kvm_sigmask.len != sizeof sigset)
1830 goto out;
1831 r = -EFAULT;
1832 if (copy_from_user(&sigset, sigmask_arg->sigset,
1833 sizeof sigset))
1834 goto out;
1835 p = &sigset;
1837 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
1838 break;
1840 case KVM_GET_FPU: {
1841 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1842 r = -ENOMEM;
1843 if (!fpu)
1844 goto out;
1845 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1846 if (r)
1847 goto out;
1848 r = -EFAULT;
1849 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1850 goto out;
1851 r = 0;
1852 break;
1854 case KVM_SET_FPU: {
1855 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1856 r = -ENOMEM;
1857 if (!fpu)
1858 goto out;
1859 r = -EFAULT;
1860 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1861 goto out;
1862 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1863 if (r)
1864 goto out;
1865 r = 0;
1866 break;
1868 default:
1869 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1871 out:
1872 vcpu_put(vcpu);
1873 kfree(fpu);
1874 kfree(kvm_sregs);
1875 return r;
1878 static long kvm_vm_ioctl(struct file *filp,
1879 unsigned int ioctl, unsigned long arg)
1881 struct kvm *kvm = filp->private_data;
1882 void __user *argp = (void __user *)arg;
1883 int r;
1885 if (kvm->mm != current->mm)
1886 return -EIO;
1887 switch (ioctl) {
1888 case KVM_CREATE_VCPU:
1889 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1890 if (r < 0)
1891 goto out;
1892 break;
1893 case KVM_SET_USER_MEMORY_REGION: {
1894 struct kvm_userspace_memory_region kvm_userspace_mem;
1896 r = -EFAULT;
1897 if (copy_from_user(&kvm_userspace_mem, argp,
1898 sizeof kvm_userspace_mem))
1899 goto out;
1901 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1902 if (r)
1903 goto out;
1904 break;
1906 case KVM_GET_DIRTY_LOG: {
1907 struct kvm_dirty_log log;
1909 r = -EFAULT;
1910 if (copy_from_user(&log, argp, sizeof log))
1911 goto out;
1912 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1913 if (r)
1914 goto out;
1915 break;
1917 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1918 case KVM_REGISTER_COALESCED_MMIO: {
1919 struct kvm_coalesced_mmio_zone zone;
1920 r = -EFAULT;
1921 if (copy_from_user(&zone, argp, sizeof zone))
1922 goto out;
1923 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1924 if (r)
1925 goto out;
1926 r = 0;
1927 break;
1929 case KVM_UNREGISTER_COALESCED_MMIO: {
1930 struct kvm_coalesced_mmio_zone zone;
1931 r = -EFAULT;
1932 if (copy_from_user(&zone, argp, sizeof zone))
1933 goto out;
1934 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1935 if (r)
1936 goto out;
1937 r = 0;
1938 break;
1940 #endif
1941 case KVM_IRQFD: {
1942 struct kvm_irqfd data;
1944 r = -EFAULT;
1945 if (copy_from_user(&data, argp, sizeof data))
1946 goto out;
1947 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1948 break;
1950 case KVM_IOEVENTFD: {
1951 struct kvm_ioeventfd data;
1953 r = -EFAULT;
1954 if (copy_from_user(&data, argp, sizeof data))
1955 goto out;
1956 r = kvm_ioeventfd(kvm, &data);
1957 break;
1959 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1960 case KVM_SET_BOOT_CPU_ID:
1961 r = 0;
1962 mutex_lock(&kvm->lock);
1963 if (atomic_read(&kvm->online_vcpus) != 0)
1964 r = -EBUSY;
1965 else
1966 kvm->bsp_vcpu_id = arg;
1967 mutex_unlock(&kvm->lock);
1968 break;
1969 #endif
1970 default:
1971 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1972 if (r == -ENOTTY)
1973 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
1975 out:
1976 return r;
1979 #ifdef CONFIG_COMPAT
1980 struct compat_kvm_dirty_log {
1981 __u32 slot;
1982 __u32 padding1;
1983 union {
1984 compat_uptr_t dirty_bitmap; /* one bit per page */
1985 __u64 padding2;
1989 static long kvm_vm_compat_ioctl(struct file *filp,
1990 unsigned int ioctl, unsigned long arg)
1992 struct kvm *kvm = filp->private_data;
1993 int r;
1995 if (kvm->mm != current->mm)
1996 return -EIO;
1997 switch (ioctl) {
1998 case KVM_GET_DIRTY_LOG: {
1999 struct compat_kvm_dirty_log compat_log;
2000 struct kvm_dirty_log log;
2002 r = -EFAULT;
2003 if (copy_from_user(&compat_log, (void __user *)arg,
2004 sizeof(compat_log)))
2005 goto out;
2006 log.slot = compat_log.slot;
2007 log.padding1 = compat_log.padding1;
2008 log.padding2 = compat_log.padding2;
2009 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2011 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2012 if (r)
2013 goto out;
2014 break;
2016 default:
2017 r = kvm_vm_ioctl(filp, ioctl, arg);
2020 out:
2021 return r;
2023 #endif
2025 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2027 struct page *page[1];
2028 unsigned long addr;
2029 int npages;
2030 gfn_t gfn = vmf->pgoff;
2031 struct kvm *kvm = vma->vm_file->private_data;
2033 addr = gfn_to_hva(kvm, gfn);
2034 if (kvm_is_error_hva(addr))
2035 return VM_FAULT_SIGBUS;
2037 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2038 NULL);
2039 if (unlikely(npages != 1))
2040 return VM_FAULT_SIGBUS;
2042 vmf->page = page[0];
2043 return 0;
2046 static const struct vm_operations_struct kvm_vm_vm_ops = {
2047 .fault = kvm_vm_fault,
2050 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2052 vma->vm_ops = &kvm_vm_vm_ops;
2053 return 0;
2056 static struct file_operations kvm_vm_fops = {
2057 .release = kvm_vm_release,
2058 .unlocked_ioctl = kvm_vm_ioctl,
2059 #ifdef CONFIG_COMPAT
2060 .compat_ioctl = kvm_vm_compat_ioctl,
2061 #endif
2062 .mmap = kvm_vm_mmap,
2063 .llseek = noop_llseek,
2066 static int kvm_dev_ioctl_create_vm(void)
2068 int r;
2069 struct kvm *kvm;
2071 kvm = kvm_create_vm();
2072 if (IS_ERR(kvm))
2073 return PTR_ERR(kvm);
2074 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2075 r = kvm_coalesced_mmio_init(kvm);
2076 if (r < 0) {
2077 kvm_put_kvm(kvm);
2078 return r;
2080 #endif
2081 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2082 if (r < 0)
2083 kvm_put_kvm(kvm);
2085 return r;
2088 static long kvm_dev_ioctl_check_extension_generic(long arg)
2090 switch (arg) {
2091 case KVM_CAP_USER_MEMORY:
2092 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2093 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2094 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2095 case KVM_CAP_SET_BOOT_CPU_ID:
2096 #endif
2097 case KVM_CAP_INTERNAL_ERROR_DATA:
2098 return 1;
2099 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2100 case KVM_CAP_IRQ_ROUTING:
2101 return KVM_MAX_IRQ_ROUTES;
2102 #endif
2103 default:
2104 break;
2106 return kvm_dev_ioctl_check_extension(arg);
2109 static long kvm_dev_ioctl(struct file *filp,
2110 unsigned int ioctl, unsigned long arg)
2112 long r = -EINVAL;
2114 switch (ioctl) {
2115 case KVM_GET_API_VERSION:
2116 r = -EINVAL;
2117 if (arg)
2118 goto out;
2119 r = KVM_API_VERSION;
2120 break;
2121 case KVM_CREATE_VM:
2122 r = -EINVAL;
2123 if (arg)
2124 goto out;
2125 r = kvm_dev_ioctl_create_vm();
2126 break;
2127 case KVM_CHECK_EXTENSION:
2128 r = kvm_dev_ioctl_check_extension_generic(arg);
2129 break;
2130 case KVM_GET_VCPU_MMAP_SIZE:
2131 r = -EINVAL;
2132 if (arg)
2133 goto out;
2134 r = PAGE_SIZE; /* struct kvm_run */
2135 #ifdef CONFIG_X86
2136 r += PAGE_SIZE; /* pio data page */
2137 #endif
2138 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2139 r += PAGE_SIZE; /* coalesced mmio ring page */
2140 #endif
2141 break;
2142 case KVM_TRACE_ENABLE:
2143 case KVM_TRACE_PAUSE:
2144 case KVM_TRACE_DISABLE:
2145 r = -EOPNOTSUPP;
2146 break;
2147 default:
2148 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2150 out:
2151 return r;
2154 static struct file_operations kvm_chardev_ops = {
2155 .unlocked_ioctl = kvm_dev_ioctl,
2156 .compat_ioctl = kvm_dev_ioctl,
2157 .llseek = noop_llseek,
2160 static struct miscdevice kvm_dev = {
2161 KVM_MINOR,
2162 "kvm",
2163 &kvm_chardev_ops,
2166 static void hardware_enable_nolock(void *junk)
2168 int cpu = raw_smp_processor_id();
2169 int r;
2171 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2172 return;
2174 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2176 r = kvm_arch_hardware_enable(NULL);
2178 if (r) {
2179 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2180 atomic_inc(&hardware_enable_failed);
2181 printk(KERN_INFO "kvm: enabling virtualization on "
2182 "CPU%d failed\n", cpu);
2186 static void hardware_enable(void *junk)
2188 raw_spin_lock(&kvm_lock);
2189 hardware_enable_nolock(junk);
2190 raw_spin_unlock(&kvm_lock);
2193 static void hardware_disable_nolock(void *junk)
2195 int cpu = raw_smp_processor_id();
2197 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2198 return;
2199 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2200 kvm_arch_hardware_disable(NULL);
2203 static void hardware_disable(void *junk)
2205 raw_spin_lock(&kvm_lock);
2206 hardware_disable_nolock(junk);
2207 raw_spin_unlock(&kvm_lock);
2210 static void hardware_disable_all_nolock(void)
2212 BUG_ON(!kvm_usage_count);
2214 kvm_usage_count--;
2215 if (!kvm_usage_count)
2216 on_each_cpu(hardware_disable_nolock, NULL, 1);
2219 static void hardware_disable_all(void)
2221 raw_spin_lock(&kvm_lock);
2222 hardware_disable_all_nolock();
2223 raw_spin_unlock(&kvm_lock);
2226 static int hardware_enable_all(void)
2228 int r = 0;
2230 raw_spin_lock(&kvm_lock);
2232 kvm_usage_count++;
2233 if (kvm_usage_count == 1) {
2234 atomic_set(&hardware_enable_failed, 0);
2235 on_each_cpu(hardware_enable_nolock, NULL, 1);
2237 if (atomic_read(&hardware_enable_failed)) {
2238 hardware_disable_all_nolock();
2239 r = -EBUSY;
2243 raw_spin_unlock(&kvm_lock);
2245 return r;
2248 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2249 void *v)
2251 int cpu = (long)v;
2253 if (!kvm_usage_count)
2254 return NOTIFY_OK;
2256 val &= ~CPU_TASKS_FROZEN;
2257 switch (val) {
2258 case CPU_DYING:
2259 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2260 cpu);
2261 hardware_disable(NULL);
2262 break;
2263 case CPU_STARTING:
2264 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2265 cpu);
2266 hardware_enable(NULL);
2267 break;
2269 return NOTIFY_OK;
2273 asmlinkage void kvm_spurious_fault(void)
2275 /* Fault while not rebooting. We want the trace. */
2276 BUG();
2278 EXPORT_SYMBOL_GPL(kvm_spurious_fault);
2280 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2281 void *v)
2284 * Some (well, at least mine) BIOSes hang on reboot if
2285 * in vmx root mode.
2287 * And Intel TXT required VMX off for all cpu when system shutdown.
2289 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2290 kvm_rebooting = true;
2291 on_each_cpu(hardware_disable_nolock, NULL, 1);
2292 return NOTIFY_OK;
2295 static struct notifier_block kvm_reboot_notifier = {
2296 .notifier_call = kvm_reboot,
2297 .priority = 0,
2300 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2302 int i;
2304 for (i = 0; i < bus->dev_count; i++) {
2305 struct kvm_io_device *pos = bus->devs[i];
2307 kvm_iodevice_destructor(pos);
2309 kfree(bus);
2312 /* kvm_io_bus_write - called under kvm->slots_lock */
2313 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2314 int len, const void *val)
2316 int i;
2317 struct kvm_io_bus *bus;
2319 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2320 for (i = 0; i < bus->dev_count; i++)
2321 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2322 return 0;
2323 return -EOPNOTSUPP;
2326 /* kvm_io_bus_read - called under kvm->slots_lock */
2327 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2328 int len, void *val)
2330 int i;
2331 struct kvm_io_bus *bus;
2333 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2334 for (i = 0; i < bus->dev_count; i++)
2335 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2336 return 0;
2337 return -EOPNOTSUPP;
2340 /* Caller must hold slots_lock. */
2341 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2342 struct kvm_io_device *dev)
2344 struct kvm_io_bus *new_bus, *bus;
2346 bus = kvm->buses[bus_idx];
2347 if (bus->dev_count > NR_IOBUS_DEVS-1)
2348 return -ENOSPC;
2350 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2351 if (!new_bus)
2352 return -ENOMEM;
2353 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2354 new_bus->devs[new_bus->dev_count++] = dev;
2355 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2356 synchronize_srcu_expedited(&kvm->srcu);
2357 kfree(bus);
2359 return 0;
2362 /* Caller must hold slots_lock. */
2363 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2364 struct kvm_io_device *dev)
2366 int i, r;
2367 struct kvm_io_bus *new_bus, *bus;
2369 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2370 if (!new_bus)
2371 return -ENOMEM;
2373 bus = kvm->buses[bus_idx];
2374 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2376 r = -ENOENT;
2377 for (i = 0; i < new_bus->dev_count; i++)
2378 if (new_bus->devs[i] == dev) {
2379 r = 0;
2380 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
2381 break;
2384 if (r) {
2385 kfree(new_bus);
2386 return r;
2389 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2390 synchronize_srcu_expedited(&kvm->srcu);
2391 kfree(bus);
2392 return r;
2395 static struct notifier_block kvm_cpu_notifier = {
2396 .notifier_call = kvm_cpu_hotplug,
2399 static int vm_stat_get(void *_offset, u64 *val)
2401 unsigned offset = (long)_offset;
2402 struct kvm *kvm;
2404 *val = 0;
2405 raw_spin_lock(&kvm_lock);
2406 list_for_each_entry(kvm, &vm_list, vm_list)
2407 *val += *(u32 *)((void *)kvm + offset);
2408 raw_spin_unlock(&kvm_lock);
2409 return 0;
2412 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2414 static int vcpu_stat_get(void *_offset, u64 *val)
2416 unsigned offset = (long)_offset;
2417 struct kvm *kvm;
2418 struct kvm_vcpu *vcpu;
2419 int i;
2421 *val = 0;
2422 raw_spin_lock(&kvm_lock);
2423 list_for_each_entry(kvm, &vm_list, vm_list)
2424 kvm_for_each_vcpu(i, vcpu, kvm)
2425 *val += *(u32 *)((void *)vcpu + offset);
2427 raw_spin_unlock(&kvm_lock);
2428 return 0;
2431 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2433 static const struct file_operations *stat_fops[] = {
2434 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2435 [KVM_STAT_VM] = &vm_stat_fops,
2438 static void kvm_init_debug(void)
2440 struct kvm_stats_debugfs_item *p;
2442 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2443 for (p = debugfs_entries; p->name; ++p)
2444 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2445 (void *)(long)p->offset,
2446 stat_fops[p->kind]);
2449 static void kvm_exit_debug(void)
2451 struct kvm_stats_debugfs_item *p;
2453 for (p = debugfs_entries; p->name; ++p)
2454 debugfs_remove(p->dentry);
2455 debugfs_remove(kvm_debugfs_dir);
2458 static int kvm_suspend(void)
2460 if (kvm_usage_count)
2461 hardware_disable_nolock(NULL);
2462 return 0;
2465 static void kvm_resume(void)
2467 if (kvm_usage_count) {
2468 WARN_ON(raw_spin_is_locked(&kvm_lock));
2469 hardware_enable_nolock(NULL);
2473 static struct syscore_ops kvm_syscore_ops = {
2474 .suspend = kvm_suspend,
2475 .resume = kvm_resume,
2478 struct page *bad_page;
2479 pfn_t bad_pfn;
2481 static inline
2482 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2484 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2487 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2489 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2491 kvm_arch_vcpu_load(vcpu, cpu);
2494 static void kvm_sched_out(struct preempt_notifier *pn,
2495 struct task_struct *next)
2497 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2499 kvm_arch_vcpu_put(vcpu);
2502 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
2503 struct module *module)
2505 int r;
2506 int cpu;
2508 r = kvm_arch_init(opaque);
2509 if (r)
2510 goto out_fail;
2512 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2514 if (bad_page == NULL) {
2515 r = -ENOMEM;
2516 goto out;
2519 bad_pfn = page_to_pfn(bad_page);
2521 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2523 if (hwpoison_page == NULL) {
2524 r = -ENOMEM;
2525 goto out_free_0;
2528 hwpoison_pfn = page_to_pfn(hwpoison_page);
2530 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2532 if (fault_page == NULL) {
2533 r = -ENOMEM;
2534 goto out_free_0;
2537 fault_pfn = page_to_pfn(fault_page);
2539 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2540 r = -ENOMEM;
2541 goto out_free_0;
2544 r = kvm_arch_hardware_setup();
2545 if (r < 0)
2546 goto out_free_0a;
2548 for_each_online_cpu(cpu) {
2549 smp_call_function_single(cpu,
2550 kvm_arch_check_processor_compat,
2551 &r, 1);
2552 if (r < 0)
2553 goto out_free_1;
2556 r = register_cpu_notifier(&kvm_cpu_notifier);
2557 if (r)
2558 goto out_free_2;
2559 register_reboot_notifier(&kvm_reboot_notifier);
2561 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2562 if (!vcpu_align)
2563 vcpu_align = __alignof__(struct kvm_vcpu);
2564 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
2565 0, NULL);
2566 if (!kvm_vcpu_cache) {
2567 r = -ENOMEM;
2568 goto out_free_3;
2571 r = kvm_async_pf_init();
2572 if (r)
2573 goto out_free;
2575 kvm_chardev_ops.owner = module;
2576 kvm_vm_fops.owner = module;
2577 kvm_vcpu_fops.owner = module;
2579 r = misc_register(&kvm_dev);
2580 if (r) {
2581 printk(KERN_ERR "kvm: misc device register failed\n");
2582 goto out_unreg;
2585 register_syscore_ops(&kvm_syscore_ops);
2587 kvm_preempt_ops.sched_in = kvm_sched_in;
2588 kvm_preempt_ops.sched_out = kvm_sched_out;
2590 kvm_init_debug();
2592 return 0;
2594 out_unreg:
2595 kvm_async_pf_deinit();
2596 out_free:
2597 kmem_cache_destroy(kvm_vcpu_cache);
2598 out_free_3:
2599 unregister_reboot_notifier(&kvm_reboot_notifier);
2600 unregister_cpu_notifier(&kvm_cpu_notifier);
2601 out_free_2:
2602 out_free_1:
2603 kvm_arch_hardware_unsetup();
2604 out_free_0a:
2605 free_cpumask_var(cpus_hardware_enabled);
2606 out_free_0:
2607 if (fault_page)
2608 __free_page(fault_page);
2609 if (hwpoison_page)
2610 __free_page(hwpoison_page);
2611 __free_page(bad_page);
2612 out:
2613 kvm_arch_exit();
2614 out_fail:
2615 return r;
2617 EXPORT_SYMBOL_GPL(kvm_init);
2619 void kvm_exit(void)
2621 kvm_exit_debug();
2622 misc_deregister(&kvm_dev);
2623 kmem_cache_destroy(kvm_vcpu_cache);
2624 kvm_async_pf_deinit();
2625 unregister_syscore_ops(&kvm_syscore_ops);
2626 unregister_reboot_notifier(&kvm_reboot_notifier);
2627 unregister_cpu_notifier(&kvm_cpu_notifier);
2628 on_each_cpu(hardware_disable_nolock, NULL, 1);
2629 kvm_arch_hardware_unsetup();
2630 kvm_arch_exit();
2631 free_cpumask_var(cpus_hardware_enabled);
2632 __free_page(hwpoison_page);
2633 __free_page(bad_page);
2635 EXPORT_SYMBOL_GPL(kvm_exit);