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.
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.
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>
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>
50 #include <linux/sort.h>
51 #include <linux/bsearch.h>
53 #include <asm/processor.h>
55 #include <asm/ioctl.h>
56 #include <asm/uaccess.h>
57 #include <asm/pgtable.h>
59 #include "coalesced_mmio.h"
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/kvm.h>
65 MODULE_AUTHOR("Qumranet");
66 MODULE_LICENSE("GPL");
71 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
74 DEFINE_RAW_SPINLOCK(kvm_lock);
77 static cpumask_var_t cpus_hardware_enabled;
78 static int kvm_usage_count = 0;
79 static atomic_t hardware_enable_failed;
81 struct kmem_cache *kvm_vcpu_cache;
82 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
84 static __read_mostly struct preempt_ops kvm_preempt_ops;
86 struct dentry *kvm_debugfs_dir;
88 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
91 static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
94 static int hardware_enable_all(void);
95 static void hardware_disable_all(void);
97 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
100 EXPORT_SYMBOL_GPL(kvm_rebooting);
102 static bool largepages_enabled = true;
104 static struct page *hwpoison_page;
105 static pfn_t hwpoison_pfn;
107 struct page *fault_page;
110 inline int kvm_is_mmio_pfn(pfn_t pfn)
112 if (pfn_valid(pfn)) {
114 struct page *tail = pfn_to_page(pfn);
115 struct page *head = compound_trans_head(tail);
116 reserved = PageReserved(head);
119 * "head" is not a dangling pointer
120 * (compound_trans_head takes care of that)
121 * but the hugepage may have been splitted
122 * from under us (and we may not hold a
123 * reference count on the head page so it can
124 * be reused before we run PageReferenced), so
125 * we've to check PageTail before returning
132 return PageReserved(tail);
139 * Switches to specified vcpu, until a matching vcpu_put()
141 void vcpu_load(struct kvm_vcpu *vcpu)
145 mutex_lock(&vcpu->mutex);
146 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
147 /* The thread running this VCPU changed. */
148 struct pid *oldpid = vcpu->pid;
149 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
150 rcu_assign_pointer(vcpu->pid, newpid);
155 preempt_notifier_register(&vcpu->preempt_notifier);
156 kvm_arch_vcpu_load(vcpu, cpu);
159 EXPORT_SYMBOL_GPL(vcpu_load);
161 void vcpu_put(struct kvm_vcpu *vcpu)
164 kvm_arch_vcpu_put(vcpu);
165 preempt_notifier_unregister(&vcpu->preempt_notifier);
167 mutex_unlock(&vcpu->mutex);
169 EXPORT_SYMBOL_GPL(vcpu_put);
171 static void ack_flush(void *_completed)
175 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
180 struct kvm_vcpu *vcpu;
182 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
185 kvm_for_each_vcpu(i, vcpu, kvm) {
186 kvm_make_request(req, vcpu);
189 /* Set ->requests bit before we read ->mode */
192 if (cpus != NULL && cpu != -1 && cpu != me &&
193 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
194 cpumask_set_cpu(cpu, cpus);
196 if (unlikely(cpus == NULL))
197 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
198 else if (!cpumask_empty(cpus))
199 smp_call_function_many(cpus, ack_flush, NULL, 1);
203 free_cpumask_var(cpus);
207 void kvm_flush_remote_tlbs(struct kvm *kvm)
209 int dirty_count = kvm->tlbs_dirty;
212 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
213 ++kvm->stat.remote_tlb_flush;
214 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
217 void kvm_reload_remote_mmus(struct kvm *kvm)
219 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
222 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
227 mutex_init(&vcpu->mutex);
232 init_waitqueue_head(&vcpu->wq);
233 kvm_async_pf_vcpu_init(vcpu);
235 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
240 vcpu->run = page_address(page);
242 r = kvm_arch_vcpu_init(vcpu);
248 free_page((unsigned long)vcpu->run);
252 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
254 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
257 kvm_arch_vcpu_uninit(vcpu);
258 free_page((unsigned long)vcpu->run);
260 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
262 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
263 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
265 return container_of(mn, struct kvm, mmu_notifier);
268 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
269 struct mm_struct *mm,
270 unsigned long address)
272 struct kvm *kvm = mmu_notifier_to_kvm(mn);
273 int need_tlb_flush, idx;
276 * When ->invalidate_page runs, the linux pte has been zapped
277 * already but the page is still allocated until
278 * ->invalidate_page returns. So if we increase the sequence
279 * here the kvm page fault will notice if the spte can't be
280 * established because the page is going to be freed. If
281 * instead the kvm page fault establishes the spte before
282 * ->invalidate_page runs, kvm_unmap_hva will release it
285 * The sequence increase only need to be seen at spin_unlock
286 * time, and not at spin_lock time.
288 * Increasing the sequence after the spin_unlock would be
289 * unsafe because the kvm page fault could then establish the
290 * pte after kvm_unmap_hva returned, without noticing the page
291 * is going to be freed.
293 idx = srcu_read_lock(&kvm->srcu);
294 spin_lock(&kvm->mmu_lock);
296 kvm->mmu_notifier_seq++;
297 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
298 /* we've to flush the tlb before the pages can be freed */
300 kvm_flush_remote_tlbs(kvm);
302 spin_unlock(&kvm->mmu_lock);
303 srcu_read_unlock(&kvm->srcu, idx);
306 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
307 struct mm_struct *mm,
308 unsigned long address,
311 struct kvm *kvm = mmu_notifier_to_kvm(mn);
314 idx = srcu_read_lock(&kvm->srcu);
315 spin_lock(&kvm->mmu_lock);
316 kvm->mmu_notifier_seq++;
317 kvm_set_spte_hva(kvm, address, pte);
318 spin_unlock(&kvm->mmu_lock);
319 srcu_read_unlock(&kvm->srcu, idx);
322 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
323 struct mm_struct *mm,
327 struct kvm *kvm = mmu_notifier_to_kvm(mn);
328 int need_tlb_flush = 0, idx;
330 idx = srcu_read_lock(&kvm->srcu);
331 spin_lock(&kvm->mmu_lock);
333 * The count increase must become visible at unlock time as no
334 * spte can be established without taking the mmu_lock and
335 * count is also read inside the mmu_lock critical section.
337 kvm->mmu_notifier_count++;
338 for (; start < end; start += PAGE_SIZE)
339 need_tlb_flush |= kvm_unmap_hva(kvm, start);
340 need_tlb_flush |= kvm->tlbs_dirty;
341 /* we've to flush the tlb before the pages can be freed */
343 kvm_flush_remote_tlbs(kvm);
345 spin_unlock(&kvm->mmu_lock);
346 srcu_read_unlock(&kvm->srcu, idx);
349 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
350 struct mm_struct *mm,
354 struct kvm *kvm = mmu_notifier_to_kvm(mn);
356 spin_lock(&kvm->mmu_lock);
358 * This sequence increase will notify the kvm page fault that
359 * the page that is going to be mapped in the spte could have
362 kvm->mmu_notifier_seq++;
364 * The above sequence increase must be visible before the
365 * below count decrease but both values are read by the kvm
366 * page fault under mmu_lock spinlock so we don't need to add
367 * a smb_wmb() here in between the two.
369 kvm->mmu_notifier_count--;
370 spin_unlock(&kvm->mmu_lock);
372 BUG_ON(kvm->mmu_notifier_count < 0);
375 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
376 struct mm_struct *mm,
377 unsigned long address)
379 struct kvm *kvm = mmu_notifier_to_kvm(mn);
382 idx = srcu_read_lock(&kvm->srcu);
383 spin_lock(&kvm->mmu_lock);
385 young = kvm_age_hva(kvm, address);
387 kvm_flush_remote_tlbs(kvm);
389 spin_unlock(&kvm->mmu_lock);
390 srcu_read_unlock(&kvm->srcu, idx);
395 static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
396 struct mm_struct *mm,
397 unsigned long address)
399 struct kvm *kvm = mmu_notifier_to_kvm(mn);
402 idx = srcu_read_lock(&kvm->srcu);
403 spin_lock(&kvm->mmu_lock);
404 young = kvm_test_age_hva(kvm, address);
405 spin_unlock(&kvm->mmu_lock);
406 srcu_read_unlock(&kvm->srcu, idx);
411 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
412 struct mm_struct *mm)
414 struct kvm *kvm = mmu_notifier_to_kvm(mn);
417 idx = srcu_read_lock(&kvm->srcu);
418 kvm_arch_flush_shadow(kvm);
419 srcu_read_unlock(&kvm->srcu, idx);
422 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
423 .invalidate_page = kvm_mmu_notifier_invalidate_page,
424 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
425 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
426 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
427 .test_young = kvm_mmu_notifier_test_young,
428 .change_pte = kvm_mmu_notifier_change_pte,
429 .release = kvm_mmu_notifier_release,
432 static int kvm_init_mmu_notifier(struct kvm *kvm)
434 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
435 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
438 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
440 static int kvm_init_mmu_notifier(struct kvm *kvm)
445 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
447 static struct kvm *kvm_create_vm(void)
450 struct kvm *kvm = kvm_arch_alloc_vm();
453 return ERR_PTR(-ENOMEM);
455 r = kvm_arch_init_vm(kvm);
457 goto out_err_nodisable;
459 r = hardware_enable_all();
461 goto out_err_nodisable;
463 #ifdef CONFIG_HAVE_KVM_IRQCHIP
464 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
465 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
469 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
472 if (init_srcu_struct(&kvm->srcu))
474 for (i = 0; i < KVM_NR_BUSES; i++) {
475 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
481 spin_lock_init(&kvm->mmu_lock);
482 kvm->mm = current->mm;
483 atomic_inc(&kvm->mm->mm_count);
484 kvm_eventfd_init(kvm);
485 mutex_init(&kvm->lock);
486 mutex_init(&kvm->irq_lock);
487 mutex_init(&kvm->slots_lock);
488 atomic_set(&kvm->users_count, 1);
490 r = kvm_init_mmu_notifier(kvm);
494 raw_spin_lock(&kvm_lock);
495 list_add(&kvm->vm_list, &vm_list);
496 raw_spin_unlock(&kvm_lock);
501 cleanup_srcu_struct(&kvm->srcu);
503 hardware_disable_all();
505 for (i = 0; i < KVM_NR_BUSES; i++)
506 kfree(kvm->buses[i]);
507 kfree(kvm->memslots);
508 kvm_arch_free_vm(kvm);
512 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
514 if (!memslot->dirty_bitmap)
517 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
518 vfree(memslot->dirty_bitmap_head);
520 kfree(memslot->dirty_bitmap_head);
522 memslot->dirty_bitmap = NULL;
523 memslot->dirty_bitmap_head = NULL;
527 * Free any memory in @free but not in @dont.
529 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
530 struct kvm_memory_slot *dont)
534 if (!dont || free->rmap != dont->rmap)
537 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
538 kvm_destroy_dirty_bitmap(free);
541 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
542 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
543 vfree(free->lpage_info[i]);
544 free->lpage_info[i] = NULL;
552 void kvm_free_physmem(struct kvm *kvm)
555 struct kvm_memslots *slots = kvm->memslots;
557 for (i = 0; i < slots->nmemslots; ++i)
558 kvm_free_physmem_slot(&slots->memslots[i], NULL);
560 kfree(kvm->memslots);
563 static void kvm_destroy_vm(struct kvm *kvm)
566 struct mm_struct *mm = kvm->mm;
568 kvm_arch_sync_events(kvm);
569 raw_spin_lock(&kvm_lock);
570 list_del(&kvm->vm_list);
571 raw_spin_unlock(&kvm_lock);
572 kvm_free_irq_routing(kvm);
573 for (i = 0; i < KVM_NR_BUSES; i++)
574 kvm_io_bus_destroy(kvm->buses[i]);
575 kvm_coalesced_mmio_free(kvm);
576 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
577 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
579 kvm_arch_flush_shadow(kvm);
581 kvm_arch_destroy_vm(kvm);
582 kvm_free_physmem(kvm);
583 cleanup_srcu_struct(&kvm->srcu);
584 kvm_arch_free_vm(kvm);
585 hardware_disable_all();
589 void kvm_get_kvm(struct kvm *kvm)
591 atomic_inc(&kvm->users_count);
593 EXPORT_SYMBOL_GPL(kvm_get_kvm);
595 void kvm_put_kvm(struct kvm *kvm)
597 if (atomic_dec_and_test(&kvm->users_count))
600 EXPORT_SYMBOL_GPL(kvm_put_kvm);
603 static int kvm_vm_release(struct inode *inode, struct file *filp)
605 struct kvm *kvm = filp->private_data;
607 kvm_irqfd_release(kvm);
615 * Allocation size is twice as large as the actual dirty bitmap size.
616 * This makes it possible to do double buffering: see x86's
617 * kvm_vm_ioctl_get_dirty_log().
619 static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
621 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
623 if (dirty_bytes > PAGE_SIZE)
624 memslot->dirty_bitmap = vzalloc(dirty_bytes);
626 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
628 if (!memslot->dirty_bitmap)
631 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
634 #endif /* !CONFIG_S390 */
637 * Allocate some memory and give it an address in the guest physical address
640 * Discontiguous memory is allowed, mostly for framebuffers.
642 * Must be called holding mmap_sem for write.
644 int __kvm_set_memory_region(struct kvm *kvm,
645 struct kvm_userspace_memory_region *mem,
650 unsigned long npages;
652 struct kvm_memory_slot *memslot;
653 struct kvm_memory_slot old, new;
654 struct kvm_memslots *slots, *old_memslots;
657 /* General sanity checks */
658 if (mem->memory_size & (PAGE_SIZE - 1))
660 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
662 /* We can read the guest memory with __xxx_user() later on. */
664 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
665 !access_ok(VERIFY_WRITE,
666 (void __user *)(unsigned long)mem->userspace_addr,
669 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
671 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
674 memslot = &kvm->memslots->memslots[mem->slot];
675 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
676 npages = mem->memory_size >> PAGE_SHIFT;
679 if (npages > KVM_MEM_MAX_NR_PAGES)
683 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
685 new = old = *memslot;
688 new.base_gfn = base_gfn;
690 new.flags = mem->flags;
692 /* Disallow changing a memory slot's size. */
694 if (npages && old.npages && npages != old.npages)
697 /* Check for overlaps */
699 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
700 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
702 if (s == memslot || !s->npages)
704 if (!((base_gfn + npages <= s->base_gfn) ||
705 (base_gfn >= s->base_gfn + s->npages)))
709 /* Free page dirty bitmap if unneeded */
710 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
711 new.dirty_bitmap = NULL;
715 /* Allocate if a slot is being created */
717 if (npages && !new.rmap) {
718 new.rmap = vzalloc(npages * sizeof(*new.rmap));
723 new.user_alloc = user_alloc;
724 new.userspace_addr = mem->userspace_addr;
729 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
735 /* Avoid unused variable warning if no large pages */
738 if (new.lpage_info[i])
741 lpages = 1 + ((base_gfn + npages - 1)
742 >> KVM_HPAGE_GFN_SHIFT(level));
743 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
745 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
747 if (!new.lpage_info[i])
750 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
751 new.lpage_info[i][0].write_count = 1;
752 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
753 new.lpage_info[i][lpages - 1].write_count = 1;
754 ugfn = new.userspace_addr >> PAGE_SHIFT;
756 * If the gfn and userspace address are not aligned wrt each
757 * other, or if explicitly asked to, disable large page
758 * support for this slot
760 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
762 for (j = 0; j < lpages; ++j)
763 new.lpage_info[i][j].write_count = 1;
768 /* Allocate page dirty bitmap if needed */
769 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
770 if (kvm_create_dirty_bitmap(&new) < 0)
772 /* destroy any largepage mappings for dirty tracking */
774 #else /* not defined CONFIG_S390 */
775 new.user_alloc = user_alloc;
777 new.userspace_addr = mem->userspace_addr;
778 #endif /* not defined CONFIG_S390 */
780 if (!npages || base_gfn != old.base_gfn) {
782 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
785 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
786 if (mem->slot >= slots->nmemslots)
787 slots->nmemslots = mem->slot + 1;
789 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
791 old_memslots = kvm->memslots;
792 rcu_assign_pointer(kvm->memslots, slots);
793 synchronize_srcu_expedited(&kvm->srcu);
794 /* slot was deleted or moved, clear iommu mapping */
795 kvm_iommu_unmap_pages(kvm, &old);
796 /* From this point no new shadow pages pointing to a deleted,
797 * or moved, memslot will be created.
799 * validation of sp->gfn happens in:
800 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
801 * - kvm_is_visible_gfn (mmu_check_roots)
803 kvm_arch_flush_shadow(kvm);
807 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
812 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
815 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
816 if (mem->slot >= slots->nmemslots)
817 slots->nmemslots = mem->slot + 1;
820 /* map new memory slot into the iommu */
822 r = kvm_iommu_map_pages(kvm, &new);
827 /* actual memory is freed via old in kvm_free_physmem_slot below */
830 new.dirty_bitmap = NULL;
831 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
832 new.lpage_info[i] = NULL;
835 slots->memslots[mem->slot] = new;
836 old_memslots = kvm->memslots;
837 rcu_assign_pointer(kvm->memslots, slots);
838 synchronize_srcu_expedited(&kvm->srcu);
840 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
843 * If the new memory slot is created, we need to clear all
846 if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
847 kvm_arch_flush_shadow(kvm);
849 kvm_free_physmem_slot(&old, &new);
857 kvm_free_physmem_slot(&new, &old);
862 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
864 int kvm_set_memory_region(struct kvm *kvm,
865 struct kvm_userspace_memory_region *mem,
870 mutex_lock(&kvm->slots_lock);
871 r = __kvm_set_memory_region(kvm, mem, user_alloc);
872 mutex_unlock(&kvm->slots_lock);
875 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
877 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
879 kvm_userspace_memory_region *mem,
882 if (mem->slot >= KVM_MEMORY_SLOTS)
884 return kvm_set_memory_region(kvm, mem, user_alloc);
887 int kvm_get_dirty_log(struct kvm *kvm,
888 struct kvm_dirty_log *log, int *is_dirty)
890 struct kvm_memory_slot *memslot;
893 unsigned long any = 0;
896 if (log->slot >= KVM_MEMORY_SLOTS)
899 memslot = &kvm->memslots->memslots[log->slot];
901 if (!memslot->dirty_bitmap)
904 n = kvm_dirty_bitmap_bytes(memslot);
906 for (i = 0; !any && i < n/sizeof(long); ++i)
907 any = memslot->dirty_bitmap[i];
910 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
921 void kvm_disable_largepages(void)
923 largepages_enabled = false;
925 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
927 int is_error_page(struct page *page)
929 return page == bad_page || page == hwpoison_page || page == fault_page;
931 EXPORT_SYMBOL_GPL(is_error_page);
933 int is_error_pfn(pfn_t pfn)
935 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
937 EXPORT_SYMBOL_GPL(is_error_pfn);
939 int is_hwpoison_pfn(pfn_t pfn)
941 return pfn == hwpoison_pfn;
943 EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
945 int is_fault_pfn(pfn_t pfn)
947 return pfn == fault_pfn;
949 EXPORT_SYMBOL_GPL(is_fault_pfn);
951 int is_noslot_pfn(pfn_t pfn)
953 return pfn == bad_pfn;
955 EXPORT_SYMBOL_GPL(is_noslot_pfn);
957 int is_invalid_pfn(pfn_t pfn)
959 return pfn == hwpoison_pfn || pfn == fault_pfn;
961 EXPORT_SYMBOL_GPL(is_invalid_pfn);
963 static inline unsigned long bad_hva(void)
968 int kvm_is_error_hva(unsigned long addr)
970 return addr == bad_hva();
972 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
974 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
979 for (i = 0; i < slots->nmemslots; ++i) {
980 struct kvm_memory_slot *memslot = &slots->memslots[i];
982 if (gfn >= memslot->base_gfn
983 && gfn < memslot->base_gfn + memslot->npages)
989 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
991 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
993 EXPORT_SYMBOL_GPL(gfn_to_memslot);
995 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
998 struct kvm_memslots *slots = kvm_memslots(kvm);
1000 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1001 struct kvm_memory_slot *memslot = &slots->memslots[i];
1003 if (memslot->flags & KVM_MEMSLOT_INVALID)
1006 if (gfn >= memslot->base_gfn
1007 && gfn < memslot->base_gfn + memslot->npages)
1012 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1014 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
1016 struct vm_area_struct *vma;
1017 unsigned long addr, size;
1021 addr = gfn_to_hva(kvm, gfn);
1022 if (kvm_is_error_hva(addr))
1025 down_read(¤t->mm->mmap_sem);
1026 vma = find_vma(current->mm, addr);
1030 size = vma_kernel_pagesize(vma);
1033 up_read(¤t->mm->mmap_sem);
1038 static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1041 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
1045 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1047 return gfn_to_hva_memslot(slot, gfn);
1050 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1052 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
1054 EXPORT_SYMBOL_GPL(gfn_to_hva);
1056 static pfn_t get_fault_pfn(void)
1058 get_page(fault_page);
1062 int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1063 unsigned long start, int write, struct page **page)
1065 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1068 flags |= FOLL_WRITE;
1070 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1073 static inline int check_user_page_hwpoison(unsigned long addr)
1075 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1077 rc = __get_user_pages(current, current->mm, addr, 1,
1078 flags, NULL, NULL, NULL);
1079 return rc == -EHWPOISON;
1082 static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
1083 bool *async, bool write_fault, bool *writable)
1085 struct page *page[1];
1089 /* we can do it either atomically or asynchronously, not both */
1090 BUG_ON(atomic && async);
1092 BUG_ON(!write_fault && !writable);
1097 if (atomic || async)
1098 npages = __get_user_pages_fast(addr, 1, 1, page);
1100 if (unlikely(npages != 1) && !atomic) {
1104 *writable = write_fault;
1107 down_read(¤t->mm->mmap_sem);
1108 npages = get_user_page_nowait(current, current->mm,
1109 addr, write_fault, page);
1110 up_read(¤t->mm->mmap_sem);
1112 npages = get_user_pages_fast(addr, 1, write_fault,
1115 /* map read fault as writable if possible */
1116 if (unlikely(!write_fault) && npages == 1) {
1117 struct page *wpage[1];
1119 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1129 if (unlikely(npages != 1)) {
1130 struct vm_area_struct *vma;
1133 return get_fault_pfn();
1135 down_read(¤t->mm->mmap_sem);
1136 if (npages == -EHWPOISON ||
1137 (!async && check_user_page_hwpoison(addr))) {
1138 up_read(¤t->mm->mmap_sem);
1139 get_page(hwpoison_page);
1140 return page_to_pfn(hwpoison_page);
1143 vma = find_vma_intersection(current->mm, addr, addr+1);
1146 pfn = get_fault_pfn();
1147 else if ((vma->vm_flags & VM_PFNMAP)) {
1148 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1150 BUG_ON(!kvm_is_mmio_pfn(pfn));
1152 if (async && (vma->vm_flags & VM_WRITE))
1154 pfn = get_fault_pfn();
1156 up_read(¤t->mm->mmap_sem);
1158 pfn = page_to_pfn(page[0]);
1163 pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1165 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
1167 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1169 static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1170 bool write_fault, bool *writable)
1177 addr = gfn_to_hva(kvm, gfn);
1178 if (kvm_is_error_hva(addr)) {
1180 return page_to_pfn(bad_page);
1183 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
1186 pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1188 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
1190 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1192 pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1193 bool write_fault, bool *writable)
1195 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
1197 EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1199 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1201 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
1203 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1205 pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1208 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1210 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1212 pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1213 struct kvm_memory_slot *slot, gfn_t gfn)
1215 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
1216 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
1219 int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1225 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
1226 if (kvm_is_error_hva(addr))
1229 if (entry < nr_pages)
1232 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1234 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1236 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1240 pfn = gfn_to_pfn(kvm, gfn);
1241 if (!kvm_is_mmio_pfn(pfn))
1242 return pfn_to_page(pfn);
1244 WARN_ON(kvm_is_mmio_pfn(pfn));
1250 EXPORT_SYMBOL_GPL(gfn_to_page);
1252 void kvm_release_page_clean(struct page *page)
1254 kvm_release_pfn_clean(page_to_pfn(page));
1256 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1258 void kvm_release_pfn_clean(pfn_t pfn)
1260 if (!kvm_is_mmio_pfn(pfn))
1261 put_page(pfn_to_page(pfn));
1263 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1265 void kvm_release_page_dirty(struct page *page)
1267 kvm_release_pfn_dirty(page_to_pfn(page));
1269 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1271 void kvm_release_pfn_dirty(pfn_t pfn)
1273 kvm_set_pfn_dirty(pfn);
1274 kvm_release_pfn_clean(pfn);
1276 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1278 void kvm_set_page_dirty(struct page *page)
1280 kvm_set_pfn_dirty(page_to_pfn(page));
1282 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1284 void kvm_set_pfn_dirty(pfn_t pfn)
1286 if (!kvm_is_mmio_pfn(pfn)) {
1287 struct page *page = pfn_to_page(pfn);
1288 if (!PageReserved(page))
1292 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1294 void kvm_set_pfn_accessed(pfn_t pfn)
1296 if (!kvm_is_mmio_pfn(pfn))
1297 mark_page_accessed(pfn_to_page(pfn));
1299 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1301 void kvm_get_pfn(pfn_t pfn)
1303 if (!kvm_is_mmio_pfn(pfn))
1304 get_page(pfn_to_page(pfn));
1306 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1308 static int next_segment(unsigned long len, int offset)
1310 if (len > PAGE_SIZE - offset)
1311 return PAGE_SIZE - offset;
1316 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1322 addr = gfn_to_hva(kvm, gfn);
1323 if (kvm_is_error_hva(addr))
1325 r = __copy_from_user(data, (void __user *)addr + offset, len);
1330 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1332 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1334 gfn_t gfn = gpa >> PAGE_SHIFT;
1336 int offset = offset_in_page(gpa);
1339 while ((seg = next_segment(len, offset)) != 0) {
1340 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1350 EXPORT_SYMBOL_GPL(kvm_read_guest);
1352 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1357 gfn_t gfn = gpa >> PAGE_SHIFT;
1358 int offset = offset_in_page(gpa);
1360 addr = gfn_to_hva(kvm, gfn);
1361 if (kvm_is_error_hva(addr))
1363 pagefault_disable();
1364 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1370 EXPORT_SYMBOL(kvm_read_guest_atomic);
1372 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1373 int offset, int len)
1378 addr = gfn_to_hva(kvm, gfn);
1379 if (kvm_is_error_hva(addr))
1381 r = __copy_to_user((void __user *)addr + offset, data, len);
1384 mark_page_dirty(kvm, gfn);
1387 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1389 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1392 gfn_t gfn = gpa >> PAGE_SHIFT;
1394 int offset = offset_in_page(gpa);
1397 while ((seg = next_segment(len, offset)) != 0) {
1398 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1409 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1410 gpa_t gpa, unsigned long len)
1412 struct kvm_memslots *slots = kvm_memslots(kvm);
1413 int offset = offset_in_page(gpa);
1414 gfn_t start_gfn = gpa >> PAGE_SHIFT;
1415 gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
1416 gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
1417 gfn_t nr_pages_avail;
1420 ghc->generation = slots->generation;
1422 ghc->memslot = __gfn_to_memslot(slots, start_gfn);
1423 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, &nr_pages_avail);
1424 if (!kvm_is_error_hva(ghc->hva) && nr_pages_avail >= nr_pages_needed) {
1428 * If the requested region crosses two memslots, we still
1429 * verify that the entire region is valid here.
1431 while (start_gfn <= end_gfn) {
1432 ghc->memslot = __gfn_to_memslot(slots, start_gfn);
1433 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
1435 if (kvm_is_error_hva(ghc->hva))
1437 start_gfn += nr_pages_avail;
1439 /* Use the slow path for cross page reads and writes. */
1440 ghc->memslot = NULL;
1444 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1446 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1447 void *data, unsigned long len)
1449 struct kvm_memslots *slots = kvm_memslots(kvm);
1452 BUG_ON(len > ghc->len);
1454 if (slots->generation != ghc->generation)
1455 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
1457 if (unlikely(!ghc->memslot))
1458 return kvm_write_guest(kvm, ghc->gpa, data, len);
1460 if (kvm_is_error_hva(ghc->hva))
1463 r = __copy_to_user((void __user *)ghc->hva, data, len);
1466 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1470 EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1472 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1473 void *data, unsigned long len)
1475 struct kvm_memslots *slots = kvm_memslots(kvm);
1478 BUG_ON(len > ghc->len);
1480 if (slots->generation != ghc->generation)
1481 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
1483 if (unlikely(!ghc->memslot))
1484 return kvm_read_guest(kvm, ghc->gpa, data, len);
1486 if (kvm_is_error_hva(ghc->hva))
1489 r = __copy_from_user(data, (void __user *)ghc->hva, len);
1495 EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
1497 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1499 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1502 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1504 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1506 gfn_t gfn = gpa >> PAGE_SHIFT;
1508 int offset = offset_in_page(gpa);
1511 while ((seg = next_segment(len, offset)) != 0) {
1512 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1521 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1523 void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1526 if (memslot && memslot->dirty_bitmap) {
1527 unsigned long rel_gfn = gfn - memslot->base_gfn;
1529 __set_bit_le(rel_gfn, memslot->dirty_bitmap);
1533 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1535 struct kvm_memory_slot *memslot;
1537 memslot = gfn_to_memslot(kvm, gfn);
1538 mark_page_dirty_in_slot(kvm, memslot, gfn);
1542 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1544 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1549 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1551 if (kvm_arch_vcpu_runnable(vcpu)) {
1552 kvm_make_request(KVM_REQ_UNHALT, vcpu);
1555 if (kvm_cpu_has_pending_timer(vcpu))
1557 if (signal_pending(current))
1563 finish_wait(&vcpu->wq, &wait);
1566 void kvm_resched(struct kvm_vcpu *vcpu)
1568 if (!need_resched())
1572 EXPORT_SYMBOL_GPL(kvm_resched);
1574 void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1576 struct kvm *kvm = me->kvm;
1577 struct kvm_vcpu *vcpu;
1578 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1584 * We boost the priority of a VCPU that is runnable but not
1585 * currently running, because it got preempted by something
1586 * else and called schedule in __vcpu_run. Hopefully that
1587 * VCPU is holding the lock that we need and will release it.
1588 * We approximate round-robin by starting at the last boosted VCPU.
1590 for (pass = 0; pass < 2 && !yielded; pass++) {
1591 kvm_for_each_vcpu(i, vcpu, kvm) {
1592 struct task_struct *task = NULL;
1594 if (!pass && i < last_boosted_vcpu) {
1595 i = last_boosted_vcpu;
1597 } else if (pass && i > last_boosted_vcpu)
1601 if (waitqueue_active(&vcpu->wq))
1604 pid = rcu_dereference(vcpu->pid);
1606 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1610 if (task->flags & PF_VCPU) {
1611 put_task_struct(task);
1614 if (yield_to(task, 1)) {
1615 put_task_struct(task);
1616 kvm->last_boosted_vcpu = i;
1620 put_task_struct(task);
1624 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1626 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1628 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1631 if (vmf->pgoff == 0)
1632 page = virt_to_page(vcpu->run);
1634 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1635 page = virt_to_page(vcpu->arch.pio_data);
1637 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1638 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1639 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1642 return VM_FAULT_SIGBUS;
1648 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1649 .fault = kvm_vcpu_fault,
1652 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1654 vma->vm_ops = &kvm_vcpu_vm_ops;
1658 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1660 struct kvm_vcpu *vcpu = filp->private_data;
1662 kvm_put_kvm(vcpu->kvm);
1666 static struct file_operations kvm_vcpu_fops = {
1667 .release = kvm_vcpu_release,
1668 .unlocked_ioctl = kvm_vcpu_ioctl,
1669 #ifdef CONFIG_COMPAT
1670 .compat_ioctl = kvm_vcpu_compat_ioctl,
1672 .mmap = kvm_vcpu_mmap,
1673 .llseek = noop_llseek,
1677 * Allocates an inode for the vcpu.
1679 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1681 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
1685 * Creates some virtual cpus. Good luck creating more than one.
1687 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1690 struct kvm_vcpu *vcpu, *v;
1692 if (id >= KVM_MAX_VCPUS)
1695 vcpu = kvm_arch_vcpu_create(kvm, id);
1697 return PTR_ERR(vcpu);
1699 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1701 r = kvm_arch_vcpu_setup(vcpu);
1705 mutex_lock(&kvm->lock);
1706 if (!kvm_vcpu_compatible(vcpu)) {
1708 goto unlock_vcpu_destroy;
1710 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1712 goto unlock_vcpu_destroy;
1715 kvm_for_each_vcpu(r, v, kvm)
1716 if (v->vcpu_id == id) {
1718 goto unlock_vcpu_destroy;
1721 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1723 /* Now it's all set up, let userspace reach it */
1725 r = create_vcpu_fd(vcpu);
1728 goto unlock_vcpu_destroy;
1731 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1733 atomic_inc(&kvm->online_vcpus);
1735 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1736 if (kvm->bsp_vcpu_id == id)
1737 kvm->bsp_vcpu = vcpu;
1739 mutex_unlock(&kvm->lock);
1742 unlock_vcpu_destroy:
1743 mutex_unlock(&kvm->lock);
1745 kvm_arch_vcpu_destroy(vcpu);
1749 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1752 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1753 vcpu->sigset_active = 1;
1754 vcpu->sigset = *sigset;
1756 vcpu->sigset_active = 0;
1760 static long kvm_vcpu_ioctl(struct file *filp,
1761 unsigned int ioctl, unsigned long arg)
1763 struct kvm_vcpu *vcpu = filp->private_data;
1764 void __user *argp = (void __user *)arg;
1766 struct kvm_fpu *fpu = NULL;
1767 struct kvm_sregs *kvm_sregs = NULL;
1769 if (vcpu->kvm->mm != current->mm)
1772 if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
1775 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1777 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1778 * so vcpu_load() would break it.
1780 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1781 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1791 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1792 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
1794 case KVM_GET_REGS: {
1795 struct kvm_regs *kvm_regs;
1798 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1801 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1805 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1812 case KVM_SET_REGS: {
1813 struct kvm_regs *kvm_regs;
1816 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1820 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1822 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1830 case KVM_GET_SREGS: {
1831 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1835 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1839 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1844 case KVM_SET_SREGS: {
1845 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1850 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1852 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1858 case KVM_GET_MP_STATE: {
1859 struct kvm_mp_state mp_state;
1861 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1865 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1870 case KVM_SET_MP_STATE: {
1871 struct kvm_mp_state mp_state;
1874 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1876 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1882 case KVM_TRANSLATE: {
1883 struct kvm_translation tr;
1886 if (copy_from_user(&tr, argp, sizeof tr))
1888 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1892 if (copy_to_user(argp, &tr, sizeof tr))
1897 case KVM_SET_GUEST_DEBUG: {
1898 struct kvm_guest_debug dbg;
1901 if (copy_from_user(&dbg, argp, sizeof dbg))
1903 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1909 case KVM_SET_SIGNAL_MASK: {
1910 struct kvm_signal_mask __user *sigmask_arg = argp;
1911 struct kvm_signal_mask kvm_sigmask;
1912 sigset_t sigset, *p;
1917 if (copy_from_user(&kvm_sigmask, argp,
1918 sizeof kvm_sigmask))
1921 if (kvm_sigmask.len != sizeof sigset)
1924 if (copy_from_user(&sigset, sigmask_arg->sigset,
1929 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
1933 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1937 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1941 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1947 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1952 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1954 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1961 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1970 #ifdef CONFIG_COMPAT
1971 static long kvm_vcpu_compat_ioctl(struct file *filp,
1972 unsigned int ioctl, unsigned long arg)
1974 struct kvm_vcpu *vcpu = filp->private_data;
1975 void __user *argp = compat_ptr(arg);
1978 if (vcpu->kvm->mm != current->mm)
1982 case KVM_SET_SIGNAL_MASK: {
1983 struct kvm_signal_mask __user *sigmask_arg = argp;
1984 struct kvm_signal_mask kvm_sigmask;
1985 compat_sigset_t csigset;
1990 if (copy_from_user(&kvm_sigmask, argp,
1991 sizeof kvm_sigmask))
1994 if (kvm_sigmask.len != sizeof csigset)
1997 if (copy_from_user(&csigset, sigmask_arg->sigset,
2001 sigset_from_compat(&sigset, &csigset);
2002 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2006 r = kvm_vcpu_ioctl(filp, ioctl, arg);
2014 static long kvm_vm_ioctl(struct file *filp,
2015 unsigned int ioctl, unsigned long arg)
2017 struct kvm *kvm = filp->private_data;
2018 void __user *argp = (void __user *)arg;
2021 if (kvm->mm != current->mm)
2024 case KVM_CREATE_VCPU:
2025 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2029 case KVM_SET_USER_MEMORY_REGION: {
2030 struct kvm_userspace_memory_region kvm_userspace_mem;
2033 if (copy_from_user(&kvm_userspace_mem, argp,
2034 sizeof kvm_userspace_mem))
2037 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
2042 case KVM_GET_DIRTY_LOG: {
2043 struct kvm_dirty_log log;
2046 if (copy_from_user(&log, argp, sizeof log))
2048 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2053 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2054 case KVM_REGISTER_COALESCED_MMIO: {
2055 struct kvm_coalesced_mmio_zone zone;
2057 if (copy_from_user(&zone, argp, sizeof zone))
2059 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2065 case KVM_UNREGISTER_COALESCED_MMIO: {
2066 struct kvm_coalesced_mmio_zone zone;
2068 if (copy_from_user(&zone, argp, sizeof zone))
2070 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2078 struct kvm_irqfd data;
2081 if (copy_from_user(&data, argp, sizeof data))
2083 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2086 case KVM_IOEVENTFD: {
2087 struct kvm_ioeventfd data;
2090 if (copy_from_user(&data, argp, sizeof data))
2092 r = kvm_ioeventfd(kvm, &data);
2095 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2096 case KVM_SET_BOOT_CPU_ID:
2098 mutex_lock(&kvm->lock);
2099 if (atomic_read(&kvm->online_vcpus) != 0)
2102 kvm->bsp_vcpu_id = arg;
2103 mutex_unlock(&kvm->lock);
2107 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2109 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
2115 #ifdef CONFIG_COMPAT
2116 struct compat_kvm_dirty_log {
2120 compat_uptr_t dirty_bitmap; /* one bit per page */
2125 static long kvm_vm_compat_ioctl(struct file *filp,
2126 unsigned int ioctl, unsigned long arg)
2128 struct kvm *kvm = filp->private_data;
2131 if (kvm->mm != current->mm)
2134 case KVM_GET_DIRTY_LOG: {
2135 struct compat_kvm_dirty_log compat_log;
2136 struct kvm_dirty_log log;
2139 if (copy_from_user(&compat_log, (void __user *)arg,
2140 sizeof(compat_log)))
2142 log.slot = compat_log.slot;
2143 log.padding1 = compat_log.padding1;
2144 log.padding2 = compat_log.padding2;
2145 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2147 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2153 r = kvm_vm_ioctl(filp, ioctl, arg);
2161 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2163 struct page *page[1];
2166 gfn_t gfn = vmf->pgoff;
2167 struct kvm *kvm = vma->vm_file->private_data;
2169 addr = gfn_to_hva(kvm, gfn);
2170 if (kvm_is_error_hva(addr))
2171 return VM_FAULT_SIGBUS;
2173 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2175 if (unlikely(npages != 1))
2176 return VM_FAULT_SIGBUS;
2178 vmf->page = page[0];
2182 static const struct vm_operations_struct kvm_vm_vm_ops = {
2183 .fault = kvm_vm_fault,
2186 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2188 vma->vm_ops = &kvm_vm_vm_ops;
2192 static struct file_operations kvm_vm_fops = {
2193 .release = kvm_vm_release,
2194 .unlocked_ioctl = kvm_vm_ioctl,
2195 #ifdef CONFIG_COMPAT
2196 .compat_ioctl = kvm_vm_compat_ioctl,
2198 .mmap = kvm_vm_mmap,
2199 .llseek = noop_llseek,
2202 static int kvm_dev_ioctl_create_vm(void)
2207 kvm = kvm_create_vm();
2209 return PTR_ERR(kvm);
2210 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2211 r = kvm_coalesced_mmio_init(kvm);
2217 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2224 static long kvm_dev_ioctl_check_extension_generic(long arg)
2227 case KVM_CAP_USER_MEMORY:
2228 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2229 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2230 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2231 case KVM_CAP_SET_BOOT_CPU_ID:
2233 case KVM_CAP_INTERNAL_ERROR_DATA:
2235 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2236 case KVM_CAP_IRQ_ROUTING:
2237 return KVM_MAX_IRQ_ROUTES;
2242 return kvm_dev_ioctl_check_extension(arg);
2245 static long kvm_dev_ioctl(struct file *filp,
2246 unsigned int ioctl, unsigned long arg)
2251 case KVM_GET_API_VERSION:
2255 r = KVM_API_VERSION;
2261 r = kvm_dev_ioctl_create_vm();
2263 case KVM_CHECK_EXTENSION:
2264 r = kvm_dev_ioctl_check_extension_generic(arg);
2266 case KVM_GET_VCPU_MMAP_SIZE:
2270 r = PAGE_SIZE; /* struct kvm_run */
2272 r += PAGE_SIZE; /* pio data page */
2274 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2275 r += PAGE_SIZE; /* coalesced mmio ring page */
2278 case KVM_TRACE_ENABLE:
2279 case KVM_TRACE_PAUSE:
2280 case KVM_TRACE_DISABLE:
2284 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2290 static struct file_operations kvm_chardev_ops = {
2291 .unlocked_ioctl = kvm_dev_ioctl,
2292 .compat_ioctl = kvm_dev_ioctl,
2293 .llseek = noop_llseek,
2296 static struct miscdevice kvm_dev = {
2302 static void hardware_enable_nolock(void *junk)
2304 int cpu = raw_smp_processor_id();
2307 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2310 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2312 r = kvm_arch_hardware_enable(NULL);
2315 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2316 atomic_inc(&hardware_enable_failed);
2317 printk(KERN_INFO "kvm: enabling virtualization on "
2318 "CPU%d failed\n", cpu);
2322 static void hardware_enable(void *junk)
2324 raw_spin_lock(&kvm_lock);
2325 hardware_enable_nolock(junk);
2326 raw_spin_unlock(&kvm_lock);
2329 static void hardware_disable_nolock(void *junk)
2331 int cpu = raw_smp_processor_id();
2333 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2335 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2336 kvm_arch_hardware_disable(NULL);
2339 static void hardware_disable(void *junk)
2341 raw_spin_lock(&kvm_lock);
2342 hardware_disable_nolock(junk);
2343 raw_spin_unlock(&kvm_lock);
2346 static void hardware_disable_all_nolock(void)
2348 BUG_ON(!kvm_usage_count);
2351 if (!kvm_usage_count)
2352 on_each_cpu(hardware_disable_nolock, NULL, 1);
2355 static void hardware_disable_all(void)
2357 raw_spin_lock(&kvm_lock);
2358 hardware_disable_all_nolock();
2359 raw_spin_unlock(&kvm_lock);
2362 static int hardware_enable_all(void)
2366 raw_spin_lock(&kvm_lock);
2369 if (kvm_usage_count == 1) {
2370 atomic_set(&hardware_enable_failed, 0);
2371 on_each_cpu(hardware_enable_nolock, NULL, 1);
2373 if (atomic_read(&hardware_enable_failed)) {
2374 hardware_disable_all_nolock();
2379 raw_spin_unlock(&kvm_lock);
2384 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2389 if (!kvm_usage_count)
2392 val &= ~CPU_TASKS_FROZEN;
2395 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2397 hardware_disable(NULL);
2400 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2402 hardware_enable(NULL);
2409 asmlinkage void kvm_spurious_fault(void)
2411 /* Fault while not rebooting. We want the trace. */
2414 EXPORT_SYMBOL_GPL(kvm_spurious_fault);
2416 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2420 * Some (well, at least mine) BIOSes hang on reboot if
2423 * And Intel TXT required VMX off for all cpu when system shutdown.
2425 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2426 kvm_rebooting = true;
2427 on_each_cpu(hardware_disable_nolock, NULL, 1);
2431 static struct notifier_block kvm_reboot_notifier = {
2432 .notifier_call = kvm_reboot,
2436 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2440 for (i = 0; i < bus->dev_count; i++) {
2441 struct kvm_io_device *pos = bus->range[i].dev;
2443 kvm_iodevice_destructor(pos);
2448 int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
2450 const struct kvm_io_range *r1 = p1;
2451 const struct kvm_io_range *r2 = p2;
2453 if (r1->addr < r2->addr)
2455 if (r1->addr + r1->len > r2->addr + r2->len)
2460 int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
2461 gpa_t addr, int len)
2463 if (bus->dev_count == NR_IOBUS_DEVS)
2466 bus->range[bus->dev_count++] = (struct kvm_io_range) {
2472 sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
2473 kvm_io_bus_sort_cmp, NULL);
2478 int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
2479 gpa_t addr, int len)
2481 struct kvm_io_range *range, key;
2484 key = (struct kvm_io_range) {
2489 range = bsearch(&key, bus->range, bus->dev_count,
2490 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
2494 off = range - bus->range;
2496 while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
2502 /* kvm_io_bus_write - called under kvm->slots_lock */
2503 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2504 int len, const void *val)
2507 struct kvm_io_bus *bus;
2508 struct kvm_io_range range;
2510 range = (struct kvm_io_range) {
2515 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2516 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2520 while (idx < bus->dev_count &&
2521 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2522 if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
2530 /* kvm_io_bus_read - called under kvm->slots_lock */
2531 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2535 struct kvm_io_bus *bus;
2536 struct kvm_io_range range;
2538 range = (struct kvm_io_range) {
2543 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2544 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2548 while (idx < bus->dev_count &&
2549 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2550 if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
2558 /* Caller must hold slots_lock. */
2559 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2560 int len, struct kvm_io_device *dev)
2562 struct kvm_io_bus *new_bus, *bus;
2564 bus = kvm->buses[bus_idx];
2565 if (bus->dev_count > NR_IOBUS_DEVS-1)
2568 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2571 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2572 kvm_io_bus_insert_dev(new_bus, dev, addr, len);
2573 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2574 synchronize_srcu_expedited(&kvm->srcu);
2580 /* Caller must hold slots_lock. */
2581 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2582 struct kvm_io_device *dev)
2585 struct kvm_io_bus *new_bus, *bus;
2587 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2591 bus = kvm->buses[bus_idx];
2592 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2595 for (i = 0; i < new_bus->dev_count; i++)
2596 if (new_bus->range[i].dev == dev) {
2598 new_bus->dev_count--;
2599 new_bus->range[i] = new_bus->range[new_bus->dev_count];
2600 sort(new_bus->range, new_bus->dev_count,
2601 sizeof(struct kvm_io_range),
2602 kvm_io_bus_sort_cmp, NULL);
2611 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2612 synchronize_srcu_expedited(&kvm->srcu);
2617 static struct notifier_block kvm_cpu_notifier = {
2618 .notifier_call = kvm_cpu_hotplug,
2621 static int vm_stat_get(void *_offset, u64 *val)
2623 unsigned offset = (long)_offset;
2627 raw_spin_lock(&kvm_lock);
2628 list_for_each_entry(kvm, &vm_list, vm_list)
2629 *val += *(u32 *)((void *)kvm + offset);
2630 raw_spin_unlock(&kvm_lock);
2634 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2636 static int vcpu_stat_get(void *_offset, u64 *val)
2638 unsigned offset = (long)_offset;
2640 struct kvm_vcpu *vcpu;
2644 raw_spin_lock(&kvm_lock);
2645 list_for_each_entry(kvm, &vm_list, vm_list)
2646 kvm_for_each_vcpu(i, vcpu, kvm)
2647 *val += *(u32 *)((void *)vcpu + offset);
2649 raw_spin_unlock(&kvm_lock);
2653 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2655 static const struct file_operations *stat_fops[] = {
2656 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2657 [KVM_STAT_VM] = &vm_stat_fops,
2660 static void kvm_init_debug(void)
2662 struct kvm_stats_debugfs_item *p;
2664 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2665 for (p = debugfs_entries; p->name; ++p)
2666 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2667 (void *)(long)p->offset,
2668 stat_fops[p->kind]);
2671 static void kvm_exit_debug(void)
2673 struct kvm_stats_debugfs_item *p;
2675 for (p = debugfs_entries; p->name; ++p)
2676 debugfs_remove(p->dentry);
2677 debugfs_remove(kvm_debugfs_dir);
2680 static int kvm_suspend(void)
2682 if (kvm_usage_count)
2683 hardware_disable_nolock(NULL);
2687 static void kvm_resume(void)
2689 if (kvm_usage_count) {
2690 WARN_ON(raw_spin_is_locked(&kvm_lock));
2691 hardware_enable_nolock(NULL);
2695 static struct syscore_ops kvm_syscore_ops = {
2696 .suspend = kvm_suspend,
2697 .resume = kvm_resume,
2700 struct page *bad_page;
2704 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2706 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2709 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2711 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2713 kvm_arch_vcpu_load(vcpu, cpu);
2716 static void kvm_sched_out(struct preempt_notifier *pn,
2717 struct task_struct *next)
2719 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2721 kvm_arch_vcpu_put(vcpu);
2724 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
2725 struct module *module)
2730 r = kvm_arch_init(opaque);
2734 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2736 if (bad_page == NULL) {
2741 bad_pfn = page_to_pfn(bad_page);
2743 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2745 if (hwpoison_page == NULL) {
2750 hwpoison_pfn = page_to_pfn(hwpoison_page);
2752 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2754 if (fault_page == NULL) {
2759 fault_pfn = page_to_pfn(fault_page);
2761 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2766 r = kvm_arch_hardware_setup();
2770 for_each_online_cpu(cpu) {
2771 smp_call_function_single(cpu,
2772 kvm_arch_check_processor_compat,
2778 r = register_cpu_notifier(&kvm_cpu_notifier);
2781 register_reboot_notifier(&kvm_reboot_notifier);
2783 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2785 vcpu_align = __alignof__(struct kvm_vcpu);
2786 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
2788 if (!kvm_vcpu_cache) {
2793 r = kvm_async_pf_init();
2797 kvm_chardev_ops.owner = module;
2798 kvm_vm_fops.owner = module;
2799 kvm_vcpu_fops.owner = module;
2801 r = misc_register(&kvm_dev);
2803 printk(KERN_ERR "kvm: misc device register failed\n");
2807 register_syscore_ops(&kvm_syscore_ops);
2809 kvm_preempt_ops.sched_in = kvm_sched_in;
2810 kvm_preempt_ops.sched_out = kvm_sched_out;
2817 kvm_async_pf_deinit();
2819 kmem_cache_destroy(kvm_vcpu_cache);
2821 unregister_reboot_notifier(&kvm_reboot_notifier);
2822 unregister_cpu_notifier(&kvm_cpu_notifier);
2825 kvm_arch_hardware_unsetup();
2827 free_cpumask_var(cpus_hardware_enabled);
2830 __free_page(fault_page);
2832 __free_page(hwpoison_page);
2833 __free_page(bad_page);
2839 EXPORT_SYMBOL_GPL(kvm_init);
2844 misc_deregister(&kvm_dev);
2845 kmem_cache_destroy(kvm_vcpu_cache);
2846 kvm_async_pf_deinit();
2847 unregister_syscore_ops(&kvm_syscore_ops);
2848 unregister_reboot_notifier(&kvm_reboot_notifier);
2849 unregister_cpu_notifier(&kvm_cpu_notifier);
2850 on_each_cpu(hardware_disable_nolock, NULL, 1);
2851 kvm_arch_hardware_unsetup();
2853 free_cpumask_var(cpus_hardware_enabled);
2854 __free_page(hwpoison_page);
2855 __free_page(bad_page);
2857 EXPORT_SYMBOL_GPL(kvm_exit);