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/sysdev.h>
34 #include <linux/cpu.h>
35 #include <linux/sched.h>
36 #include <linux/cpumask.h>
37 #include <linux/smp.h>
38 #include <linux/anon_inodes.h>
39 #include <linux/profile.h>
40 #include <linux/kvm_para.h>
41 #include <linux/pagemap.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44 #include <linux/bitops.h>
45 #include <linux/spinlock.h>
46 #include <linux/compat.h>
47 #include <linux/srcu.h>
48 #include <linux/hugetlb.h>
49 #include <linux/slab.h>
51 #include <asm/processor.h>
53 #include <asm/uaccess.h>
54 #include <asm/pgtable.h>
55 #include <asm-generic/bitops/le.h>
57 #include "coalesced_mmio.h"
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/kvm.h>
63 MODULE_AUTHOR("Qumranet");
64 MODULE_LICENSE("GPL");
69 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
72 DEFINE_RAW_SPINLOCK(kvm_lock);
75 static cpumask_var_t cpus_hardware_enabled;
76 static int kvm_usage_count = 0;
77 static atomic_t hardware_enable_failed;
79 struct kmem_cache *kvm_vcpu_cache;
80 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
82 static __read_mostly struct preempt_ops kvm_preempt_ops;
84 struct dentry *kvm_debugfs_dir;
86 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
88 static int hardware_enable_all(void);
89 static void hardware_disable_all(void);
91 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
94 EXPORT_SYMBOL_GPL(kvm_rebooting);
96 static bool largepages_enabled = true;
98 static struct page *hwpoison_page;
99 static pfn_t hwpoison_pfn;
101 static struct page *fault_page;
102 static pfn_t fault_pfn;
104 inline int kvm_is_mmio_pfn(pfn_t pfn)
106 if (pfn_valid(pfn)) {
108 struct page *tail = pfn_to_page(pfn);
109 struct page *head = compound_trans_head(tail);
110 reserved = PageReserved(head);
113 * "head" is not a dangling pointer
114 * (compound_trans_head takes care of that)
115 * but the hugepage may have been splitted
116 * from under us (and we may not hold a
117 * reference count on the head page so it can
118 * be reused before we run PageReferenced), so
119 * we've to check PageTail before returning
126 return PageReserved(tail);
133 * Switches to specified vcpu, until a matching vcpu_put()
135 void vcpu_load(struct kvm_vcpu *vcpu)
139 mutex_lock(&vcpu->mutex);
140 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
141 /* The thread running this VCPU changed. */
142 struct pid *oldpid = vcpu->pid;
143 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
144 rcu_assign_pointer(vcpu->pid, newpid);
149 preempt_notifier_register(&vcpu->preempt_notifier);
150 kvm_arch_vcpu_load(vcpu, cpu);
154 void vcpu_put(struct kvm_vcpu *vcpu)
157 kvm_arch_vcpu_put(vcpu);
158 preempt_notifier_unregister(&vcpu->preempt_notifier);
160 mutex_unlock(&vcpu->mutex);
163 static void ack_flush(void *_completed)
167 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
172 struct kvm_vcpu *vcpu;
174 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
177 kvm_for_each_vcpu(i, vcpu, kvm) {
178 kvm_make_request(req, vcpu);
181 /* Set ->requests bit before we read ->mode */
184 if (cpus != NULL && cpu != -1 && cpu != me &&
185 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
186 cpumask_set_cpu(cpu, cpus);
188 if (unlikely(cpus == NULL))
189 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
190 else if (!cpumask_empty(cpus))
191 smp_call_function_many(cpus, ack_flush, NULL, 1);
195 free_cpumask_var(cpus);
199 void kvm_flush_remote_tlbs(struct kvm *kvm)
201 int dirty_count = kvm->tlbs_dirty;
204 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
205 ++kvm->stat.remote_tlb_flush;
206 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
209 void kvm_reload_remote_mmus(struct kvm *kvm)
211 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
214 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
219 mutex_init(&vcpu->mutex);
224 init_waitqueue_head(&vcpu->wq);
225 kvm_async_pf_vcpu_init(vcpu);
227 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
232 vcpu->run = page_address(page);
234 r = kvm_arch_vcpu_init(vcpu);
240 free_page((unsigned long)vcpu->run);
244 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
246 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
249 kvm_arch_vcpu_uninit(vcpu);
250 free_page((unsigned long)vcpu->run);
252 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
254 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
255 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
257 return container_of(mn, struct kvm, mmu_notifier);
260 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
261 struct mm_struct *mm,
262 unsigned long address)
264 struct kvm *kvm = mmu_notifier_to_kvm(mn);
265 int need_tlb_flush, idx;
268 * When ->invalidate_page runs, the linux pte has been zapped
269 * already but the page is still allocated until
270 * ->invalidate_page returns. So if we increase the sequence
271 * here the kvm page fault will notice if the spte can't be
272 * established because the page is going to be freed. If
273 * instead the kvm page fault establishes the spte before
274 * ->invalidate_page runs, kvm_unmap_hva will release it
277 * The sequence increase only need to be seen at spin_unlock
278 * time, and not at spin_lock time.
280 * Increasing the sequence after the spin_unlock would be
281 * unsafe because the kvm page fault could then establish the
282 * pte after kvm_unmap_hva returned, without noticing the page
283 * is going to be freed.
285 idx = srcu_read_lock(&kvm->srcu);
286 spin_lock(&kvm->mmu_lock);
287 kvm->mmu_notifier_seq++;
288 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
289 spin_unlock(&kvm->mmu_lock);
290 srcu_read_unlock(&kvm->srcu, idx);
292 /* we've to flush the tlb before the pages can be freed */
294 kvm_flush_remote_tlbs(kvm);
298 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
299 struct mm_struct *mm,
300 unsigned long address,
303 struct kvm *kvm = mmu_notifier_to_kvm(mn);
306 idx = srcu_read_lock(&kvm->srcu);
307 spin_lock(&kvm->mmu_lock);
308 kvm->mmu_notifier_seq++;
309 kvm_set_spte_hva(kvm, address, pte);
310 spin_unlock(&kvm->mmu_lock);
311 srcu_read_unlock(&kvm->srcu, idx);
314 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
315 struct mm_struct *mm,
319 struct kvm *kvm = mmu_notifier_to_kvm(mn);
320 int need_tlb_flush = 0, idx;
322 idx = srcu_read_lock(&kvm->srcu);
323 spin_lock(&kvm->mmu_lock);
325 * The count increase must become visible at unlock time as no
326 * spte can be established without taking the mmu_lock and
327 * count is also read inside the mmu_lock critical section.
329 kvm->mmu_notifier_count++;
330 for (; start < end; start += PAGE_SIZE)
331 need_tlb_flush |= kvm_unmap_hva(kvm, start);
332 need_tlb_flush |= kvm->tlbs_dirty;
333 spin_unlock(&kvm->mmu_lock);
334 srcu_read_unlock(&kvm->srcu, idx);
336 /* we've to flush the tlb before the pages can be freed */
338 kvm_flush_remote_tlbs(kvm);
341 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
342 struct mm_struct *mm,
346 struct kvm *kvm = mmu_notifier_to_kvm(mn);
348 spin_lock(&kvm->mmu_lock);
350 * This sequence increase will notify the kvm page fault that
351 * the page that is going to be mapped in the spte could have
354 kvm->mmu_notifier_seq++;
356 * The above sequence increase must be visible before the
357 * below count decrease but both values are read by the kvm
358 * page fault under mmu_lock spinlock so we don't need to add
359 * a smb_wmb() here in between the two.
361 kvm->mmu_notifier_count--;
362 spin_unlock(&kvm->mmu_lock);
364 BUG_ON(kvm->mmu_notifier_count < 0);
367 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
368 struct mm_struct *mm,
369 unsigned long address)
371 struct kvm *kvm = mmu_notifier_to_kvm(mn);
374 idx = srcu_read_lock(&kvm->srcu);
375 spin_lock(&kvm->mmu_lock);
376 young = kvm_age_hva(kvm, address);
377 spin_unlock(&kvm->mmu_lock);
378 srcu_read_unlock(&kvm->srcu, idx);
381 kvm_flush_remote_tlbs(kvm);
386 static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
387 struct mm_struct *mm,
388 unsigned long address)
390 struct kvm *kvm = mmu_notifier_to_kvm(mn);
393 idx = srcu_read_lock(&kvm->srcu);
394 spin_lock(&kvm->mmu_lock);
395 young = kvm_test_age_hva(kvm, address);
396 spin_unlock(&kvm->mmu_lock);
397 srcu_read_unlock(&kvm->srcu, idx);
402 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
403 struct mm_struct *mm)
405 struct kvm *kvm = mmu_notifier_to_kvm(mn);
408 idx = srcu_read_lock(&kvm->srcu);
409 kvm_arch_flush_shadow(kvm);
410 srcu_read_unlock(&kvm->srcu, idx);
413 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
414 .invalidate_page = kvm_mmu_notifier_invalidate_page,
415 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
416 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
417 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
418 .test_young = kvm_mmu_notifier_test_young,
419 .change_pte = kvm_mmu_notifier_change_pte,
420 .release = kvm_mmu_notifier_release,
423 static int kvm_init_mmu_notifier(struct kvm *kvm)
425 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
426 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
429 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
431 static int kvm_init_mmu_notifier(struct kvm *kvm)
436 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
438 static struct kvm *kvm_create_vm(void)
441 struct kvm *kvm = kvm_arch_alloc_vm();
444 return ERR_PTR(-ENOMEM);
446 r = kvm_arch_init_vm(kvm);
448 goto out_err_nodisable;
450 r = hardware_enable_all();
452 goto out_err_nodisable;
454 #ifdef CONFIG_HAVE_KVM_IRQCHIP
455 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
456 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
460 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
463 if (init_srcu_struct(&kvm->srcu))
465 for (i = 0; i < KVM_NR_BUSES; i++) {
466 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
472 r = kvm_init_mmu_notifier(kvm);
476 kvm->mm = current->mm;
477 atomic_inc(&kvm->mm->mm_count);
478 spin_lock_init(&kvm->mmu_lock);
479 kvm_eventfd_init(kvm);
480 mutex_init(&kvm->lock);
481 mutex_init(&kvm->irq_lock);
482 mutex_init(&kvm->slots_lock);
483 atomic_set(&kvm->users_count, 1);
484 raw_spin_lock(&kvm_lock);
485 list_add(&kvm->vm_list, &vm_list);
486 raw_spin_unlock(&kvm_lock);
491 cleanup_srcu_struct(&kvm->srcu);
493 hardware_disable_all();
495 for (i = 0; i < KVM_NR_BUSES; i++)
496 kfree(kvm->buses[i]);
497 kfree(kvm->memslots);
498 kvm_arch_free_vm(kvm);
502 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
504 if (!memslot->dirty_bitmap)
507 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
508 vfree(memslot->dirty_bitmap_head);
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)
524 if (!dont || free->rmap != dont->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;
542 void kvm_free_physmem(struct kvm *kvm)
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)
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);
569 kvm_arch_flush_shadow(kvm);
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();
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))
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);
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);
616 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
618 if (!memslot->dirty_bitmap)
621 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
624 #endif /* !CONFIG_S390 */
627 * Allocate some memory and give it an address in the guest physical address
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,
640 unsigned long npages;
642 struct kvm_memory_slot *memslot;
643 struct kvm_memory_slot old, new;
644 struct kvm_memslots *slots, *old_memslots;
647 /* General sanity checks */
648 if (mem->memory_size & (PAGE_SIZE - 1))
650 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
652 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
654 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
656 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
659 memslot = &kvm->memslots->memslots[mem->slot];
660 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
661 npages = mem->memory_size >> PAGE_SHIFT;
664 if (npages > KVM_MEM_MAX_NR_PAGES)
668 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
670 new = old = *memslot;
673 new.base_gfn = base_gfn;
675 new.flags = mem->flags;
677 /* Disallow changing a memory slot's size. */
679 if (npages && old.npages && npages != old.npages)
682 /* Check for overlaps */
684 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
685 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
687 if (s == memslot || !s->npages)
689 if (!((base_gfn + npages <= s->base_gfn) ||
690 (base_gfn >= s->base_gfn + s->npages)))
694 /* Free page dirty bitmap if unneeded */
695 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
696 new.dirty_bitmap = NULL;
700 /* Allocate if a slot is being created */
702 if (npages && !new.rmap) {
703 new.rmap = vzalloc(npages * sizeof(*new.rmap));
708 new.user_alloc = user_alloc;
709 new.userspace_addr = mem->userspace_addr;
714 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
720 /* Avoid unused variable warning if no large pages */
723 if (new.lpage_info[i])
726 lpages = 1 + ((base_gfn + npages - 1)
727 >> KVM_HPAGE_GFN_SHIFT(level));
728 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
730 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
732 if (!new.lpage_info[i])
735 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
736 new.lpage_info[i][0].write_count = 1;
737 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
738 new.lpage_info[i][lpages - 1].write_count = 1;
739 ugfn = new.userspace_addr >> PAGE_SHIFT;
741 * If the gfn and userspace address are not aligned wrt each
742 * other, or if explicitly asked to, disable large page
743 * support for this slot
745 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
747 for (j = 0; j < lpages; ++j)
748 new.lpage_info[i][j].write_count = 1;
753 /* Allocate page dirty bitmap if needed */
754 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
755 if (kvm_create_dirty_bitmap(&new) < 0)
757 /* destroy any largepage mappings for dirty tracking */
759 #else /* not defined CONFIG_S390 */
760 new.user_alloc = user_alloc;
762 new.userspace_addr = mem->userspace_addr;
763 #endif /* not defined CONFIG_S390 */
767 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
770 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
771 if (mem->slot >= slots->nmemslots)
772 slots->nmemslots = mem->slot + 1;
774 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
776 old_memslots = kvm->memslots;
777 rcu_assign_pointer(kvm->memslots, slots);
778 synchronize_srcu_expedited(&kvm->srcu);
779 /* From this point no new shadow pages pointing to a deleted
780 * memslot will be created.
782 * validation of sp->gfn happens in:
783 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
784 * - kvm_is_visible_gfn (mmu_check_roots)
786 kvm_arch_flush_shadow(kvm);
790 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
794 /* map the pages in iommu page table */
796 r = kvm_iommu_map_pages(kvm, &new);
802 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
805 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
806 if (mem->slot >= slots->nmemslots)
807 slots->nmemslots = mem->slot + 1;
810 /* actual memory is freed via old in kvm_free_physmem_slot below */
813 new.dirty_bitmap = NULL;
814 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
815 new.lpage_info[i] = NULL;
818 slots->memslots[mem->slot] = new;
819 old_memslots = kvm->memslots;
820 rcu_assign_pointer(kvm->memslots, slots);
821 synchronize_srcu_expedited(&kvm->srcu);
823 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
825 kvm_free_physmem_slot(&old, &new);
831 kvm_free_physmem_slot(&new, &old);
836 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
838 int kvm_set_memory_region(struct kvm *kvm,
839 struct kvm_userspace_memory_region *mem,
844 mutex_lock(&kvm->slots_lock);
845 r = __kvm_set_memory_region(kvm, mem, user_alloc);
846 mutex_unlock(&kvm->slots_lock);
849 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
851 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
853 kvm_userspace_memory_region *mem,
856 if (mem->slot >= KVM_MEMORY_SLOTS)
858 return kvm_set_memory_region(kvm, mem, user_alloc);
861 int kvm_get_dirty_log(struct kvm *kvm,
862 struct kvm_dirty_log *log, int *is_dirty)
864 struct kvm_memory_slot *memslot;
867 unsigned long any = 0;
870 if (log->slot >= KVM_MEMORY_SLOTS)
873 memslot = &kvm->memslots->memslots[log->slot];
875 if (!memslot->dirty_bitmap)
878 n = kvm_dirty_bitmap_bytes(memslot);
880 for (i = 0; !any && i < n/sizeof(long); ++i)
881 any = memslot->dirty_bitmap[i];
884 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
895 void kvm_disable_largepages(void)
897 largepages_enabled = false;
899 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
901 int is_error_page(struct page *page)
903 return page == bad_page || page == hwpoison_page || page == fault_page;
905 EXPORT_SYMBOL_GPL(is_error_page);
907 int is_error_pfn(pfn_t pfn)
909 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
911 EXPORT_SYMBOL_GPL(is_error_pfn);
913 int is_hwpoison_pfn(pfn_t pfn)
915 return pfn == hwpoison_pfn;
917 EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
919 int is_fault_pfn(pfn_t pfn)
921 return pfn == fault_pfn;
923 EXPORT_SYMBOL_GPL(is_fault_pfn);
925 static inline unsigned long bad_hva(void)
930 int kvm_is_error_hva(unsigned long addr)
932 return addr == bad_hva();
934 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
936 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
941 for (i = 0; i < slots->nmemslots; ++i) {
942 struct kvm_memory_slot *memslot = &slots->memslots[i];
944 if (gfn >= memslot->base_gfn
945 && gfn < memslot->base_gfn + memslot->npages)
951 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
953 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
955 EXPORT_SYMBOL_GPL(gfn_to_memslot);
957 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
960 struct kvm_memslots *slots = kvm_memslots(kvm);
962 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
963 struct kvm_memory_slot *memslot = &slots->memslots[i];
965 if (memslot->flags & KVM_MEMSLOT_INVALID)
968 if (gfn >= memslot->base_gfn
969 && gfn < memslot->base_gfn + memslot->npages)
974 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
976 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
978 struct vm_area_struct *vma;
979 unsigned long addr, size;
983 addr = gfn_to_hva(kvm, gfn);
984 if (kvm_is_error_hva(addr))
987 down_read(¤t->mm->mmap_sem);
988 vma = find_vma(current->mm, addr);
992 size = vma_kernel_pagesize(vma);
995 up_read(¤t->mm->mmap_sem);
1000 int memslot_id(struct kvm *kvm, gfn_t gfn)
1003 struct kvm_memslots *slots = kvm_memslots(kvm);
1004 struct kvm_memory_slot *memslot = NULL;
1006 for (i = 0; i < slots->nmemslots; ++i) {
1007 memslot = &slots->memslots[i];
1009 if (gfn >= memslot->base_gfn
1010 && gfn < memslot->base_gfn + memslot->npages)
1014 return memslot - slots->memslots;
1017 static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1020 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
1024 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1026 return gfn_to_hva_memslot(slot, gfn);
1029 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1031 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
1033 EXPORT_SYMBOL_GPL(gfn_to_hva);
1035 static pfn_t get_fault_pfn(void)
1037 get_page(fault_page);
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];
1057 /* we can do it either atomically or asynchronously, not both */
1058 BUG_ON(atomic && async);
1060 BUG_ON(!write_fault && !writable);
1065 if (atomic || async)
1066 npages = __get_user_pages_fast(addr, 1, 1, page);
1068 if (unlikely(npages != 1) && !atomic) {
1072 *writable = write_fault;
1074 npages = get_user_pages_fast(addr, 1, write_fault, page);
1076 /* map read fault as writable if possible */
1077 if (unlikely(!write_fault) && npages == 1) {
1078 struct page *wpage[1];
1080 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1090 if (unlikely(npages != 1)) {
1091 struct vm_area_struct *vma;
1094 return get_fault_pfn();
1096 down_read(¤t->mm->mmap_sem);
1097 if (check_user_page_hwpoison(addr)) {
1098 up_read(¤t->mm->mmap_sem);
1099 get_page(hwpoison_page);
1100 return page_to_pfn(hwpoison_page);
1103 vma = find_vma_intersection(current->mm, addr, addr+1);
1106 pfn = get_fault_pfn();
1107 else if ((vma->vm_flags & VM_PFNMAP)) {
1108 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1110 BUG_ON(!kvm_is_mmio_pfn(pfn));
1112 if (async && (vma->vm_flags & VM_WRITE))
1114 pfn = get_fault_pfn();
1116 up_read(¤t->mm->mmap_sem);
1118 pfn = page_to_pfn(page[0]);
1123 pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1125 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
1127 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1129 static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1130 bool write_fault, bool *writable)
1137 addr = gfn_to_hva(kvm, gfn);
1138 if (kvm_is_error_hva(addr)) {
1140 return page_to_pfn(bad_page);
1143 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
1146 pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1148 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
1150 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1152 pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1153 bool write_fault, bool *writable)
1155 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
1157 EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1159 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1161 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
1163 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1165 pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1168 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1170 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1172 pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1173 struct kvm_memory_slot *slot, gfn_t gfn)
1175 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
1176 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
1179 int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1185 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
1186 if (kvm_is_error_hva(addr))
1189 if (entry < nr_pages)
1192 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1194 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1196 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1200 pfn = gfn_to_pfn(kvm, gfn);
1201 if (!kvm_is_mmio_pfn(pfn))
1202 return pfn_to_page(pfn);
1204 WARN_ON(kvm_is_mmio_pfn(pfn));
1210 EXPORT_SYMBOL_GPL(gfn_to_page);
1212 void kvm_release_page_clean(struct page *page)
1214 kvm_release_pfn_clean(page_to_pfn(page));
1216 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1218 void kvm_release_pfn_clean(pfn_t pfn)
1220 if (!kvm_is_mmio_pfn(pfn))
1221 put_page(pfn_to_page(pfn));
1223 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1225 void kvm_release_page_dirty(struct page *page)
1227 kvm_release_pfn_dirty(page_to_pfn(page));
1229 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1231 void kvm_release_pfn_dirty(pfn_t pfn)
1233 kvm_set_pfn_dirty(pfn);
1234 kvm_release_pfn_clean(pfn);
1236 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1238 void kvm_set_page_dirty(struct page *page)
1240 kvm_set_pfn_dirty(page_to_pfn(page));
1242 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1244 void kvm_set_pfn_dirty(pfn_t pfn)
1246 if (!kvm_is_mmio_pfn(pfn)) {
1247 struct page *page = pfn_to_page(pfn);
1248 if (!PageReserved(page))
1252 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1254 void kvm_set_pfn_accessed(pfn_t pfn)
1256 if (!kvm_is_mmio_pfn(pfn))
1257 mark_page_accessed(pfn_to_page(pfn));
1259 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1261 void kvm_get_pfn(pfn_t pfn)
1263 if (!kvm_is_mmio_pfn(pfn))
1264 get_page(pfn_to_page(pfn));
1266 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1268 static int next_segment(unsigned long len, int offset)
1270 if (len > PAGE_SIZE - offset)
1271 return PAGE_SIZE - offset;
1276 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1282 addr = gfn_to_hva(kvm, gfn);
1283 if (kvm_is_error_hva(addr))
1285 r = copy_from_user(data, (void __user *)addr + offset, len);
1290 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1292 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1294 gfn_t gfn = gpa >> PAGE_SHIFT;
1296 int offset = offset_in_page(gpa);
1299 while ((seg = next_segment(len, offset)) != 0) {
1300 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1310 EXPORT_SYMBOL_GPL(kvm_read_guest);
1312 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1317 gfn_t gfn = gpa >> PAGE_SHIFT;
1318 int offset = offset_in_page(gpa);
1320 addr = gfn_to_hva(kvm, gfn);
1321 if (kvm_is_error_hva(addr))
1323 pagefault_disable();
1324 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1330 EXPORT_SYMBOL(kvm_read_guest_atomic);
1332 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1333 int offset, int len)
1338 addr = gfn_to_hva(kvm, gfn);
1339 if (kvm_is_error_hva(addr))
1341 r = copy_to_user((void __user *)addr + offset, data, len);
1344 mark_page_dirty(kvm, gfn);
1347 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1349 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1352 gfn_t gfn = gpa >> PAGE_SHIFT;
1354 int offset = offset_in_page(gpa);
1357 while ((seg = next_segment(len, offset)) != 0) {
1358 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1369 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1372 struct kvm_memslots *slots = kvm_memslots(kvm);
1373 int offset = offset_in_page(gpa);
1374 gfn_t gfn = gpa >> PAGE_SHIFT;
1377 ghc->generation = slots->generation;
1378 ghc->memslot = __gfn_to_memslot(slots, gfn);
1379 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1380 if (!kvm_is_error_hva(ghc->hva))
1387 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1389 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1390 void *data, unsigned long len)
1392 struct kvm_memslots *slots = kvm_memslots(kvm);
1395 if (slots->generation != ghc->generation)
1396 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1398 if (kvm_is_error_hva(ghc->hva))
1401 r = copy_to_user((void __user *)ghc->hva, data, len);
1404 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1408 EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1410 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1412 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1415 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1417 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1419 gfn_t gfn = gpa >> PAGE_SHIFT;
1421 int offset = offset_in_page(gpa);
1424 while ((seg = next_segment(len, offset)) != 0) {
1425 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1434 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1436 void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1439 if (memslot && memslot->dirty_bitmap) {
1440 unsigned long rel_gfn = gfn - memslot->base_gfn;
1442 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
1446 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1448 struct kvm_memory_slot *memslot;
1450 memslot = gfn_to_memslot(kvm, gfn);
1451 mark_page_dirty_in_slot(kvm, memslot, gfn);
1455 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1457 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1462 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1464 if (kvm_arch_vcpu_runnable(vcpu)) {
1465 kvm_make_request(KVM_REQ_UNHALT, vcpu);
1468 if (kvm_cpu_has_pending_timer(vcpu))
1470 if (signal_pending(current))
1476 finish_wait(&vcpu->wq, &wait);
1479 void kvm_resched(struct kvm_vcpu *vcpu)
1481 if (!need_resched())
1485 EXPORT_SYMBOL_GPL(kvm_resched);
1487 void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1489 struct kvm *kvm = me->kvm;
1490 struct kvm_vcpu *vcpu;
1491 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1497 * We boost the priority of a VCPU that is runnable but not
1498 * currently running, because it got preempted by something
1499 * else and called schedule in __vcpu_run. Hopefully that
1500 * VCPU is holding the lock that we need and will release it.
1501 * We approximate round-robin by starting at the last boosted VCPU.
1503 for (pass = 0; pass < 2 && !yielded; pass++) {
1504 kvm_for_each_vcpu(i, vcpu, kvm) {
1505 struct task_struct *task = NULL;
1507 if (!pass && i < last_boosted_vcpu) {
1508 i = last_boosted_vcpu;
1510 } else if (pass && i > last_boosted_vcpu)
1514 if (waitqueue_active(&vcpu->wq))
1517 pid = rcu_dereference(vcpu->pid);
1519 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1523 if (task->flags & PF_VCPU) {
1524 put_task_struct(task);
1527 if (yield_to(task, 1)) {
1528 put_task_struct(task);
1529 kvm->last_boosted_vcpu = i;
1533 put_task_struct(task);
1537 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1539 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1541 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1544 if (vmf->pgoff == 0)
1545 page = virt_to_page(vcpu->run);
1547 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1548 page = virt_to_page(vcpu->arch.pio_data);
1550 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1551 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1552 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1555 return VM_FAULT_SIGBUS;
1561 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1562 .fault = kvm_vcpu_fault,
1565 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1567 vma->vm_ops = &kvm_vcpu_vm_ops;
1571 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1573 struct kvm_vcpu *vcpu = filp->private_data;
1575 kvm_put_kvm(vcpu->kvm);
1579 static struct file_operations kvm_vcpu_fops = {
1580 .release = kvm_vcpu_release,
1581 .unlocked_ioctl = kvm_vcpu_ioctl,
1582 .compat_ioctl = kvm_vcpu_ioctl,
1583 .mmap = kvm_vcpu_mmap,
1584 .llseek = noop_llseek,
1588 * Allocates an inode for the vcpu.
1590 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1592 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
1596 * Creates some virtual cpus. Good luck creating more than one.
1598 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1601 struct kvm_vcpu *vcpu, *v;
1603 vcpu = kvm_arch_vcpu_create(kvm, id);
1605 return PTR_ERR(vcpu);
1607 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1609 r = kvm_arch_vcpu_setup(vcpu);
1613 mutex_lock(&kvm->lock);
1614 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1619 kvm_for_each_vcpu(r, v, kvm)
1620 if (v->vcpu_id == id) {
1625 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1627 /* Now it's all set up, let userspace reach it */
1629 r = create_vcpu_fd(vcpu);
1635 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1637 atomic_inc(&kvm->online_vcpus);
1639 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1640 if (kvm->bsp_vcpu_id == id)
1641 kvm->bsp_vcpu = vcpu;
1643 mutex_unlock(&kvm->lock);
1647 mutex_unlock(&kvm->lock);
1648 kvm_arch_vcpu_destroy(vcpu);
1652 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1655 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1656 vcpu->sigset_active = 1;
1657 vcpu->sigset = *sigset;
1659 vcpu->sigset_active = 0;
1663 static long kvm_vcpu_ioctl(struct file *filp,
1664 unsigned int ioctl, unsigned long arg)
1666 struct kvm_vcpu *vcpu = filp->private_data;
1667 void __user *argp = (void __user *)arg;
1669 struct kvm_fpu *fpu = NULL;
1670 struct kvm_sregs *kvm_sregs = NULL;
1672 if (vcpu->kvm->mm != current->mm)
1675 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1677 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1678 * so vcpu_load() would break it.
1680 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1681 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1691 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1692 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
1694 case KVM_GET_REGS: {
1695 struct kvm_regs *kvm_regs;
1698 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1701 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1705 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1712 case KVM_SET_REGS: {
1713 struct kvm_regs *kvm_regs;
1716 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1720 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1722 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1730 case KVM_GET_SREGS: {
1731 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1735 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1739 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1744 case KVM_SET_SREGS: {
1745 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1750 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1752 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1758 case KVM_GET_MP_STATE: {
1759 struct kvm_mp_state mp_state;
1761 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1765 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1770 case KVM_SET_MP_STATE: {
1771 struct kvm_mp_state mp_state;
1774 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1776 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1782 case KVM_TRANSLATE: {
1783 struct kvm_translation tr;
1786 if (copy_from_user(&tr, argp, sizeof tr))
1788 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1792 if (copy_to_user(argp, &tr, sizeof tr))
1797 case KVM_SET_GUEST_DEBUG: {
1798 struct kvm_guest_debug dbg;
1801 if (copy_from_user(&dbg, argp, sizeof dbg))
1803 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1809 case KVM_SET_SIGNAL_MASK: {
1810 struct kvm_signal_mask __user *sigmask_arg = argp;
1811 struct kvm_signal_mask kvm_sigmask;
1812 sigset_t sigset, *p;
1817 if (copy_from_user(&kvm_sigmask, argp,
1818 sizeof kvm_sigmask))
1821 if (kvm_sigmask.len != sizeof sigset)
1824 if (copy_from_user(&sigset, sigmask_arg->sigset,
1829 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
1833 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1837 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1841 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1847 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1852 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1854 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1861 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1870 static long kvm_vm_ioctl(struct file *filp,
1871 unsigned int ioctl, unsigned long arg)
1873 struct kvm *kvm = filp->private_data;
1874 void __user *argp = (void __user *)arg;
1877 if (kvm->mm != current->mm)
1880 case KVM_CREATE_VCPU:
1881 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1885 case KVM_SET_USER_MEMORY_REGION: {
1886 struct kvm_userspace_memory_region kvm_userspace_mem;
1889 if (copy_from_user(&kvm_userspace_mem, argp,
1890 sizeof kvm_userspace_mem))
1893 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1898 case KVM_GET_DIRTY_LOG: {
1899 struct kvm_dirty_log log;
1902 if (copy_from_user(&log, argp, sizeof log))
1904 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1909 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1910 case KVM_REGISTER_COALESCED_MMIO: {
1911 struct kvm_coalesced_mmio_zone zone;
1913 if (copy_from_user(&zone, argp, sizeof zone))
1915 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1921 case KVM_UNREGISTER_COALESCED_MMIO: {
1922 struct kvm_coalesced_mmio_zone zone;
1924 if (copy_from_user(&zone, argp, sizeof zone))
1926 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1934 struct kvm_irqfd data;
1937 if (copy_from_user(&data, argp, sizeof data))
1939 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1942 case KVM_IOEVENTFD: {
1943 struct kvm_ioeventfd data;
1946 if (copy_from_user(&data, argp, sizeof data))
1948 r = kvm_ioeventfd(kvm, &data);
1951 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1952 case KVM_SET_BOOT_CPU_ID:
1954 mutex_lock(&kvm->lock);
1955 if (atomic_read(&kvm->online_vcpus) != 0)
1958 kvm->bsp_vcpu_id = arg;
1959 mutex_unlock(&kvm->lock);
1963 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1965 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
1971 #ifdef CONFIG_COMPAT
1972 struct compat_kvm_dirty_log {
1976 compat_uptr_t dirty_bitmap; /* one bit per page */
1981 static long kvm_vm_compat_ioctl(struct file *filp,
1982 unsigned int ioctl, unsigned long arg)
1984 struct kvm *kvm = filp->private_data;
1987 if (kvm->mm != current->mm)
1990 case KVM_GET_DIRTY_LOG: {
1991 struct compat_kvm_dirty_log compat_log;
1992 struct kvm_dirty_log log;
1995 if (copy_from_user(&compat_log, (void __user *)arg,
1996 sizeof(compat_log)))
1998 log.slot = compat_log.slot;
1999 log.padding1 = compat_log.padding1;
2000 log.padding2 = compat_log.padding2;
2001 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2003 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2009 r = kvm_vm_ioctl(filp, ioctl, arg);
2017 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2019 struct page *page[1];
2022 gfn_t gfn = vmf->pgoff;
2023 struct kvm *kvm = vma->vm_file->private_data;
2025 addr = gfn_to_hva(kvm, gfn);
2026 if (kvm_is_error_hva(addr))
2027 return VM_FAULT_SIGBUS;
2029 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2031 if (unlikely(npages != 1))
2032 return VM_FAULT_SIGBUS;
2034 vmf->page = page[0];
2038 static const struct vm_operations_struct kvm_vm_vm_ops = {
2039 .fault = kvm_vm_fault,
2042 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2044 vma->vm_ops = &kvm_vm_vm_ops;
2048 static struct file_operations kvm_vm_fops = {
2049 .release = kvm_vm_release,
2050 .unlocked_ioctl = kvm_vm_ioctl,
2051 #ifdef CONFIG_COMPAT
2052 .compat_ioctl = kvm_vm_compat_ioctl,
2054 .mmap = kvm_vm_mmap,
2055 .llseek = noop_llseek,
2058 static int kvm_dev_ioctl_create_vm(void)
2063 kvm = kvm_create_vm();
2065 return PTR_ERR(kvm);
2066 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2067 r = kvm_coalesced_mmio_init(kvm);
2073 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2080 static long kvm_dev_ioctl_check_extension_generic(long arg)
2083 case KVM_CAP_USER_MEMORY:
2084 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2085 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2086 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2087 case KVM_CAP_SET_BOOT_CPU_ID:
2089 case KVM_CAP_INTERNAL_ERROR_DATA:
2091 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2092 case KVM_CAP_IRQ_ROUTING:
2093 return KVM_MAX_IRQ_ROUTES;
2098 return kvm_dev_ioctl_check_extension(arg);
2101 static long kvm_dev_ioctl(struct file *filp,
2102 unsigned int ioctl, unsigned long arg)
2107 case KVM_GET_API_VERSION:
2111 r = KVM_API_VERSION;
2117 r = kvm_dev_ioctl_create_vm();
2119 case KVM_CHECK_EXTENSION:
2120 r = kvm_dev_ioctl_check_extension_generic(arg);
2122 case KVM_GET_VCPU_MMAP_SIZE:
2126 r = PAGE_SIZE; /* struct kvm_run */
2128 r += PAGE_SIZE; /* pio data page */
2130 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2131 r += PAGE_SIZE; /* coalesced mmio ring page */
2134 case KVM_TRACE_ENABLE:
2135 case KVM_TRACE_PAUSE:
2136 case KVM_TRACE_DISABLE:
2140 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2146 static struct file_operations kvm_chardev_ops = {
2147 .unlocked_ioctl = kvm_dev_ioctl,
2148 .compat_ioctl = kvm_dev_ioctl,
2149 .llseek = noop_llseek,
2152 static struct miscdevice kvm_dev = {
2158 static void hardware_enable_nolock(void *junk)
2160 int cpu = raw_smp_processor_id();
2163 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2166 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2168 r = kvm_arch_hardware_enable(NULL);
2171 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2172 atomic_inc(&hardware_enable_failed);
2173 printk(KERN_INFO "kvm: enabling virtualization on "
2174 "CPU%d failed\n", cpu);
2178 static void hardware_enable(void *junk)
2180 raw_spin_lock(&kvm_lock);
2181 hardware_enable_nolock(junk);
2182 raw_spin_unlock(&kvm_lock);
2185 static void hardware_disable_nolock(void *junk)
2187 int cpu = raw_smp_processor_id();
2189 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2191 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2192 kvm_arch_hardware_disable(NULL);
2195 static void hardware_disable(void *junk)
2197 raw_spin_lock(&kvm_lock);
2198 hardware_disable_nolock(junk);
2199 raw_spin_unlock(&kvm_lock);
2202 static void hardware_disable_all_nolock(void)
2204 BUG_ON(!kvm_usage_count);
2207 if (!kvm_usage_count)
2208 on_each_cpu(hardware_disable_nolock, NULL, 1);
2211 static void hardware_disable_all(void)
2213 raw_spin_lock(&kvm_lock);
2214 hardware_disable_all_nolock();
2215 raw_spin_unlock(&kvm_lock);
2218 static int hardware_enable_all(void)
2222 raw_spin_lock(&kvm_lock);
2225 if (kvm_usage_count == 1) {
2226 atomic_set(&hardware_enable_failed, 0);
2227 on_each_cpu(hardware_enable_nolock, NULL, 1);
2229 if (atomic_read(&hardware_enable_failed)) {
2230 hardware_disable_all_nolock();
2235 raw_spin_unlock(&kvm_lock);
2240 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2245 if (!kvm_usage_count)
2248 val &= ~CPU_TASKS_FROZEN;
2251 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2253 hardware_disable(NULL);
2256 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2258 hardware_enable(NULL);
2265 asmlinkage void kvm_spurious_fault(void)
2267 /* Fault while not rebooting. We want the trace. */
2270 EXPORT_SYMBOL_GPL(kvm_spurious_fault);
2272 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2276 * Some (well, at least mine) BIOSes hang on reboot if
2279 * And Intel TXT required VMX off for all cpu when system shutdown.
2281 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2282 kvm_rebooting = true;
2283 on_each_cpu(hardware_disable_nolock, NULL, 1);
2287 static struct notifier_block kvm_reboot_notifier = {
2288 .notifier_call = kvm_reboot,
2292 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2296 for (i = 0; i < bus->dev_count; i++) {
2297 struct kvm_io_device *pos = bus->devs[i];
2299 kvm_iodevice_destructor(pos);
2304 /* kvm_io_bus_write - called under kvm->slots_lock */
2305 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2306 int len, const void *val)
2309 struct kvm_io_bus *bus;
2311 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2312 for (i = 0; i < bus->dev_count; i++)
2313 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2318 /* kvm_io_bus_read - called under kvm->slots_lock */
2319 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2323 struct kvm_io_bus *bus;
2325 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2326 for (i = 0; i < bus->dev_count; i++)
2327 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2332 /* Caller must hold slots_lock. */
2333 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2334 struct kvm_io_device *dev)
2336 struct kvm_io_bus *new_bus, *bus;
2338 bus = kvm->buses[bus_idx];
2339 if (bus->dev_count > NR_IOBUS_DEVS-1)
2342 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2345 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2346 new_bus->devs[new_bus->dev_count++] = dev;
2347 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2348 synchronize_srcu_expedited(&kvm->srcu);
2354 /* Caller must hold slots_lock. */
2355 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2356 struct kvm_io_device *dev)
2359 struct kvm_io_bus *new_bus, *bus;
2361 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2365 bus = kvm->buses[bus_idx];
2366 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2369 for (i = 0; i < new_bus->dev_count; i++)
2370 if (new_bus->devs[i] == dev) {
2372 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
2381 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2382 synchronize_srcu_expedited(&kvm->srcu);
2387 static struct notifier_block kvm_cpu_notifier = {
2388 .notifier_call = kvm_cpu_hotplug,
2391 static int vm_stat_get(void *_offset, u64 *val)
2393 unsigned offset = (long)_offset;
2397 raw_spin_lock(&kvm_lock);
2398 list_for_each_entry(kvm, &vm_list, vm_list)
2399 *val += *(u32 *)((void *)kvm + offset);
2400 raw_spin_unlock(&kvm_lock);
2404 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2406 static int vcpu_stat_get(void *_offset, u64 *val)
2408 unsigned offset = (long)_offset;
2410 struct kvm_vcpu *vcpu;
2414 raw_spin_lock(&kvm_lock);
2415 list_for_each_entry(kvm, &vm_list, vm_list)
2416 kvm_for_each_vcpu(i, vcpu, kvm)
2417 *val += *(u32 *)((void *)vcpu + offset);
2419 raw_spin_unlock(&kvm_lock);
2423 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2425 static const struct file_operations *stat_fops[] = {
2426 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2427 [KVM_STAT_VM] = &vm_stat_fops,
2430 static void kvm_init_debug(void)
2432 struct kvm_stats_debugfs_item *p;
2434 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2435 for (p = debugfs_entries; p->name; ++p)
2436 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2437 (void *)(long)p->offset,
2438 stat_fops[p->kind]);
2441 static void kvm_exit_debug(void)
2443 struct kvm_stats_debugfs_item *p;
2445 for (p = debugfs_entries; p->name; ++p)
2446 debugfs_remove(p->dentry);
2447 debugfs_remove(kvm_debugfs_dir);
2450 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2452 if (kvm_usage_count)
2453 hardware_disable_nolock(NULL);
2457 static int kvm_resume(struct sys_device *dev)
2459 if (kvm_usage_count) {
2460 WARN_ON(raw_spin_is_locked(&kvm_lock));
2461 hardware_enable_nolock(NULL);
2466 static struct sysdev_class kvm_sysdev_class = {
2468 .suspend = kvm_suspend,
2469 .resume = kvm_resume,
2472 static struct sys_device kvm_sysdev = {
2474 .cls = &kvm_sysdev_class,
2477 struct page *bad_page;
2481 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2483 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2486 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2488 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2490 kvm_arch_vcpu_load(vcpu, cpu);
2493 static void kvm_sched_out(struct preempt_notifier *pn,
2494 struct task_struct *next)
2496 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2498 kvm_arch_vcpu_put(vcpu);
2501 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
2502 struct module *module)
2507 r = kvm_arch_init(opaque);
2511 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2513 if (bad_page == NULL) {
2518 bad_pfn = page_to_pfn(bad_page);
2520 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2522 if (hwpoison_page == NULL) {
2527 hwpoison_pfn = page_to_pfn(hwpoison_page);
2529 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2531 if (fault_page == NULL) {
2536 fault_pfn = page_to_pfn(fault_page);
2538 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2543 r = kvm_arch_hardware_setup();
2547 for_each_online_cpu(cpu) {
2548 smp_call_function_single(cpu,
2549 kvm_arch_check_processor_compat,
2555 r = register_cpu_notifier(&kvm_cpu_notifier);
2558 register_reboot_notifier(&kvm_reboot_notifier);
2560 r = sysdev_class_register(&kvm_sysdev_class);
2564 r = sysdev_register(&kvm_sysdev);
2568 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2570 vcpu_align = __alignof__(struct kvm_vcpu);
2571 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
2573 if (!kvm_vcpu_cache) {
2578 r = kvm_async_pf_init();
2582 kvm_chardev_ops.owner = module;
2583 kvm_vm_fops.owner = module;
2584 kvm_vcpu_fops.owner = module;
2586 r = misc_register(&kvm_dev);
2588 printk(KERN_ERR "kvm: misc device register failed\n");
2592 kvm_preempt_ops.sched_in = kvm_sched_in;
2593 kvm_preempt_ops.sched_out = kvm_sched_out;
2600 kvm_async_pf_deinit();
2602 kmem_cache_destroy(kvm_vcpu_cache);
2604 sysdev_unregister(&kvm_sysdev);
2606 sysdev_class_unregister(&kvm_sysdev_class);
2608 unregister_reboot_notifier(&kvm_reboot_notifier);
2609 unregister_cpu_notifier(&kvm_cpu_notifier);
2612 kvm_arch_hardware_unsetup();
2614 free_cpumask_var(cpus_hardware_enabled);
2617 __free_page(fault_page);
2619 __free_page(hwpoison_page);
2620 __free_page(bad_page);
2626 EXPORT_SYMBOL_GPL(kvm_init);
2631 misc_deregister(&kvm_dev);
2632 kmem_cache_destroy(kvm_vcpu_cache);
2633 kvm_async_pf_deinit();
2634 sysdev_unregister(&kvm_sysdev);
2635 sysdev_class_unregister(&kvm_sysdev_class);
2636 unregister_reboot_notifier(&kvm_reboot_notifier);
2637 unregister_cpu_notifier(&kvm_cpu_notifier);
2638 on_each_cpu(hardware_disable_nolock, NULL, 1);
2639 kvm_arch_hardware_unsetup();
2641 free_cpumask_var(cpus_hardware_enabled);
2642 __free_page(hwpoison_page);
2643 __free_page(bad_page);
2645 EXPORT_SYMBOL_GPL(kvm_exit);