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 affilates.
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"
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/kvm.h>
62 MODULE_AUTHOR("Qumranet");
63 MODULE_LICENSE("GPL");
68 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
71 DEFINE_SPINLOCK(kvm_lock);
74 static cpumask_var_t cpus_hardware_enabled;
75 static int kvm_usage_count = 0;
76 static atomic_t hardware_enable_failed;
78 struct kmem_cache *kvm_vcpu_cache;
79 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
81 static __read_mostly struct preempt_ops kvm_preempt_ops;
83 struct dentry *kvm_debugfs_dir;
85 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
87 static int hardware_enable_all(void);
88 static void hardware_disable_all(void);
90 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
92 static bool kvm_rebooting;
94 static bool largepages_enabled = true;
96 struct page *hwpoison_page;
99 inline int kvm_is_mmio_pfn(pfn_t pfn)
101 if (pfn_valid(pfn)) {
102 struct page *page = compound_head(pfn_to_page(pfn));
103 return PageReserved(page);
110 * Switches to specified vcpu, until a matching vcpu_put()
112 void vcpu_load(struct kvm_vcpu *vcpu)
116 mutex_lock(&vcpu->mutex);
118 preempt_notifier_register(&vcpu->preempt_notifier);
119 kvm_arch_vcpu_load(vcpu, cpu);
123 void vcpu_put(struct kvm_vcpu *vcpu)
126 kvm_arch_vcpu_put(vcpu);
127 preempt_notifier_unregister(&vcpu->preempt_notifier);
129 mutex_unlock(&vcpu->mutex);
132 static void ack_flush(void *_completed)
136 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
141 struct kvm_vcpu *vcpu;
143 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
145 raw_spin_lock(&kvm->requests_lock);
146 me = smp_processor_id();
147 kvm_for_each_vcpu(i, vcpu, kvm) {
148 if (test_and_set_bit(req, &vcpu->requests))
151 if (cpus != NULL && cpu != -1 && cpu != me)
152 cpumask_set_cpu(cpu, cpus);
154 if (unlikely(cpus == NULL))
155 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
156 else if (!cpumask_empty(cpus))
157 smp_call_function_many(cpus, ack_flush, NULL, 1);
160 raw_spin_unlock(&kvm->requests_lock);
161 free_cpumask_var(cpus);
165 void kvm_flush_remote_tlbs(struct kvm *kvm)
167 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
168 ++kvm->stat.remote_tlb_flush;
171 void kvm_reload_remote_mmus(struct kvm *kvm)
173 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
176 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
181 mutex_init(&vcpu->mutex);
185 init_waitqueue_head(&vcpu->wq);
187 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
192 vcpu->run = page_address(page);
194 r = kvm_arch_vcpu_init(vcpu);
200 free_page((unsigned long)vcpu->run);
204 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
206 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
208 kvm_arch_vcpu_uninit(vcpu);
209 free_page((unsigned long)vcpu->run);
211 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
213 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
214 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
216 return container_of(mn, struct kvm, mmu_notifier);
219 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
220 struct mm_struct *mm,
221 unsigned long address)
223 struct kvm *kvm = mmu_notifier_to_kvm(mn);
224 int need_tlb_flush, idx;
227 * When ->invalidate_page runs, the linux pte has been zapped
228 * already but the page is still allocated until
229 * ->invalidate_page returns. So if we increase the sequence
230 * here the kvm page fault will notice if the spte can't be
231 * established because the page is going to be freed. If
232 * instead the kvm page fault establishes the spte before
233 * ->invalidate_page runs, kvm_unmap_hva will release it
236 * The sequence increase only need to be seen at spin_unlock
237 * time, and not at spin_lock time.
239 * Increasing the sequence after the spin_unlock would be
240 * unsafe because the kvm page fault could then establish the
241 * pte after kvm_unmap_hva returned, without noticing the page
242 * is going to be freed.
244 idx = srcu_read_lock(&kvm->srcu);
245 spin_lock(&kvm->mmu_lock);
246 kvm->mmu_notifier_seq++;
247 need_tlb_flush = kvm_unmap_hva(kvm, address);
248 spin_unlock(&kvm->mmu_lock);
249 srcu_read_unlock(&kvm->srcu, idx);
251 /* we've to flush the tlb before the pages can be freed */
253 kvm_flush_remote_tlbs(kvm);
257 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
258 struct mm_struct *mm,
259 unsigned long address,
262 struct kvm *kvm = mmu_notifier_to_kvm(mn);
265 idx = srcu_read_lock(&kvm->srcu);
266 spin_lock(&kvm->mmu_lock);
267 kvm->mmu_notifier_seq++;
268 kvm_set_spte_hva(kvm, address, pte);
269 spin_unlock(&kvm->mmu_lock);
270 srcu_read_unlock(&kvm->srcu, idx);
273 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
274 struct mm_struct *mm,
278 struct kvm *kvm = mmu_notifier_to_kvm(mn);
279 int need_tlb_flush = 0, idx;
281 idx = srcu_read_lock(&kvm->srcu);
282 spin_lock(&kvm->mmu_lock);
284 * The count increase must become visible at unlock time as no
285 * spte can be established without taking the mmu_lock and
286 * count is also read inside the mmu_lock critical section.
288 kvm->mmu_notifier_count++;
289 for (; start < end; start += PAGE_SIZE)
290 need_tlb_flush |= kvm_unmap_hva(kvm, start);
291 spin_unlock(&kvm->mmu_lock);
292 srcu_read_unlock(&kvm->srcu, idx);
294 /* we've to flush the tlb before the pages can be freed */
296 kvm_flush_remote_tlbs(kvm);
299 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
300 struct mm_struct *mm,
304 struct kvm *kvm = mmu_notifier_to_kvm(mn);
306 spin_lock(&kvm->mmu_lock);
308 * This sequence increase will notify the kvm page fault that
309 * the page that is going to be mapped in the spte could have
312 kvm->mmu_notifier_seq++;
314 * The above sequence increase must be visible before the
315 * below count decrease but both values are read by the kvm
316 * page fault under mmu_lock spinlock so we don't need to add
317 * a smb_wmb() here in between the two.
319 kvm->mmu_notifier_count--;
320 spin_unlock(&kvm->mmu_lock);
322 BUG_ON(kvm->mmu_notifier_count < 0);
325 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
326 struct mm_struct *mm,
327 unsigned long address)
329 struct kvm *kvm = mmu_notifier_to_kvm(mn);
332 idx = srcu_read_lock(&kvm->srcu);
333 spin_lock(&kvm->mmu_lock);
334 young = kvm_age_hva(kvm, address);
335 spin_unlock(&kvm->mmu_lock);
336 srcu_read_unlock(&kvm->srcu, idx);
339 kvm_flush_remote_tlbs(kvm);
344 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
345 struct mm_struct *mm)
347 struct kvm *kvm = mmu_notifier_to_kvm(mn);
350 idx = srcu_read_lock(&kvm->srcu);
351 kvm_arch_flush_shadow(kvm);
352 srcu_read_unlock(&kvm->srcu, idx);
355 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
356 .invalidate_page = kvm_mmu_notifier_invalidate_page,
357 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
358 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
359 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
360 .change_pte = kvm_mmu_notifier_change_pte,
361 .release = kvm_mmu_notifier_release,
364 static int kvm_init_mmu_notifier(struct kvm *kvm)
366 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
367 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
370 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
372 static int kvm_init_mmu_notifier(struct kvm *kvm)
377 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
379 static struct kvm *kvm_create_vm(void)
382 struct kvm *kvm = kvm_arch_create_vm();
387 r = hardware_enable_all();
389 goto out_err_nodisable;
391 #ifdef CONFIG_HAVE_KVM_IRQCHIP
392 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
393 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
397 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
400 if (init_srcu_struct(&kvm->srcu))
402 for (i = 0; i < KVM_NR_BUSES; i++) {
403 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
405 if (!kvm->buses[i]) {
406 cleanup_srcu_struct(&kvm->srcu);
411 r = kvm_init_mmu_notifier(kvm);
413 cleanup_srcu_struct(&kvm->srcu);
417 kvm->mm = current->mm;
418 atomic_inc(&kvm->mm->mm_count);
419 spin_lock_init(&kvm->mmu_lock);
420 raw_spin_lock_init(&kvm->requests_lock);
421 kvm_eventfd_init(kvm);
422 mutex_init(&kvm->lock);
423 mutex_init(&kvm->irq_lock);
424 mutex_init(&kvm->slots_lock);
425 atomic_set(&kvm->users_count, 1);
426 spin_lock(&kvm_lock);
427 list_add(&kvm->vm_list, &vm_list);
428 spin_unlock(&kvm_lock);
433 hardware_disable_all();
435 for (i = 0; i < KVM_NR_BUSES; i++)
436 kfree(kvm->buses[i]);
437 kfree(kvm->memslots);
443 * Free any memory in @free but not in @dont.
445 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
446 struct kvm_memory_slot *dont)
450 if (!dont || free->rmap != dont->rmap)
453 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
454 vfree(free->dirty_bitmap);
457 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
458 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
459 vfree(free->lpage_info[i]);
460 free->lpage_info[i] = NULL;
465 free->dirty_bitmap = NULL;
469 void kvm_free_physmem(struct kvm *kvm)
472 struct kvm_memslots *slots = kvm->memslots;
474 for (i = 0; i < slots->nmemslots; ++i)
475 kvm_free_physmem_slot(&slots->memslots[i], NULL);
477 kfree(kvm->memslots);
480 static void kvm_destroy_vm(struct kvm *kvm)
483 struct mm_struct *mm = kvm->mm;
485 kvm_arch_sync_events(kvm);
486 spin_lock(&kvm_lock);
487 list_del(&kvm->vm_list);
488 spin_unlock(&kvm_lock);
489 kvm_free_irq_routing(kvm);
490 for (i = 0; i < KVM_NR_BUSES; i++)
491 kvm_io_bus_destroy(kvm->buses[i]);
492 kvm_coalesced_mmio_free(kvm);
493 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
494 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
496 kvm_arch_flush_shadow(kvm);
498 kvm_arch_destroy_vm(kvm);
499 hardware_disable_all();
503 void kvm_get_kvm(struct kvm *kvm)
505 atomic_inc(&kvm->users_count);
507 EXPORT_SYMBOL_GPL(kvm_get_kvm);
509 void kvm_put_kvm(struct kvm *kvm)
511 if (atomic_dec_and_test(&kvm->users_count))
514 EXPORT_SYMBOL_GPL(kvm_put_kvm);
517 static int kvm_vm_release(struct inode *inode, struct file *filp)
519 struct kvm *kvm = filp->private_data;
521 kvm_irqfd_release(kvm);
528 * Allocate some memory and give it an address in the guest physical address
531 * Discontiguous memory is allowed, mostly for framebuffers.
533 * Must be called holding mmap_sem for write.
535 int __kvm_set_memory_region(struct kvm *kvm,
536 struct kvm_userspace_memory_region *mem,
539 int r, flush_shadow = 0;
541 unsigned long npages;
543 struct kvm_memory_slot *memslot;
544 struct kvm_memory_slot old, new;
545 struct kvm_memslots *slots, *old_memslots;
548 /* General sanity checks */
549 if (mem->memory_size & (PAGE_SIZE - 1))
551 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
553 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
555 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
557 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
560 memslot = &kvm->memslots->memslots[mem->slot];
561 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
562 npages = mem->memory_size >> PAGE_SHIFT;
565 if (npages > KVM_MEM_MAX_NR_PAGES)
569 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
571 new = old = *memslot;
573 new.base_gfn = base_gfn;
575 new.flags = mem->flags;
577 /* Disallow changing a memory slot's size. */
579 if (npages && old.npages && npages != old.npages)
582 /* Check for overlaps */
584 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
585 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
587 if (s == memslot || !s->npages)
589 if (!((base_gfn + npages <= s->base_gfn) ||
590 (base_gfn >= s->base_gfn + s->npages)))
594 /* Free page dirty bitmap if unneeded */
595 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
596 new.dirty_bitmap = NULL;
600 /* Allocate if a slot is being created */
602 if (npages && !new.rmap) {
603 new.rmap = vmalloc(npages * sizeof(*new.rmap));
608 memset(new.rmap, 0, npages * sizeof(*new.rmap));
610 new.user_alloc = user_alloc;
611 new.userspace_addr = mem->userspace_addr;
616 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
622 /* Avoid unused variable warning if no large pages */
625 if (new.lpage_info[i])
628 lpages = 1 + (base_gfn + npages - 1) /
629 KVM_PAGES_PER_HPAGE(level);
630 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
632 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
634 if (!new.lpage_info[i])
637 memset(new.lpage_info[i], 0,
638 lpages * sizeof(*new.lpage_info[i]));
640 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
641 new.lpage_info[i][0].write_count = 1;
642 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
643 new.lpage_info[i][lpages - 1].write_count = 1;
644 ugfn = new.userspace_addr >> PAGE_SHIFT;
646 * If the gfn and userspace address are not aligned wrt each
647 * other, or if explicitly asked to, disable large page
648 * support for this slot
650 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
652 for (j = 0; j < lpages; ++j)
653 new.lpage_info[i][j].write_count = 1;
658 /* Allocate page dirty bitmap if needed */
659 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
660 unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(&new);
662 new.dirty_bitmap = vmalloc(dirty_bytes);
663 if (!new.dirty_bitmap)
665 memset(new.dirty_bitmap, 0, dirty_bytes);
666 /* destroy any largepage mappings for dirty tracking */
670 #else /* not defined CONFIG_S390 */
671 new.user_alloc = user_alloc;
673 new.userspace_addr = mem->userspace_addr;
674 #endif /* not defined CONFIG_S390 */
678 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
681 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
682 if (mem->slot >= slots->nmemslots)
683 slots->nmemslots = mem->slot + 1;
684 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
686 old_memslots = kvm->memslots;
687 rcu_assign_pointer(kvm->memslots, slots);
688 synchronize_srcu_expedited(&kvm->srcu);
689 /* From this point no new shadow pages pointing to a deleted
690 * memslot will be created.
692 * validation of sp->gfn happens in:
693 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
694 * - kvm_is_visible_gfn (mmu_check_roots)
696 kvm_arch_flush_shadow(kvm);
700 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
705 /* map the pages in iommu page table */
707 r = kvm_iommu_map_pages(kvm, &new);
714 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
717 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
718 if (mem->slot >= slots->nmemslots)
719 slots->nmemslots = mem->slot + 1;
721 /* actual memory is freed via old in kvm_free_physmem_slot below */
724 new.dirty_bitmap = NULL;
725 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
726 new.lpage_info[i] = NULL;
729 slots->memslots[mem->slot] = new;
730 old_memslots = kvm->memslots;
731 rcu_assign_pointer(kvm->memslots, slots);
732 synchronize_srcu_expedited(&kvm->srcu);
734 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
736 kvm_free_physmem_slot(&old, &new);
740 kvm_arch_flush_shadow(kvm);
745 kvm_free_physmem_slot(&new, &old);
750 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
752 int kvm_set_memory_region(struct kvm *kvm,
753 struct kvm_userspace_memory_region *mem,
758 mutex_lock(&kvm->slots_lock);
759 r = __kvm_set_memory_region(kvm, mem, user_alloc);
760 mutex_unlock(&kvm->slots_lock);
763 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
765 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
767 kvm_userspace_memory_region *mem,
770 if (mem->slot >= KVM_MEMORY_SLOTS)
772 return kvm_set_memory_region(kvm, mem, user_alloc);
775 int kvm_get_dirty_log(struct kvm *kvm,
776 struct kvm_dirty_log *log, int *is_dirty)
778 struct kvm_memory_slot *memslot;
781 unsigned long any = 0;
784 if (log->slot >= KVM_MEMORY_SLOTS)
787 memslot = &kvm->memslots->memslots[log->slot];
789 if (!memslot->dirty_bitmap)
792 n = kvm_dirty_bitmap_bytes(memslot);
794 for (i = 0; !any && i < n/sizeof(long); ++i)
795 any = memslot->dirty_bitmap[i];
798 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
809 void kvm_disable_largepages(void)
811 largepages_enabled = false;
813 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
815 int is_error_page(struct page *page)
817 return page == bad_page || page == hwpoison_page;
819 EXPORT_SYMBOL_GPL(is_error_page);
821 int is_error_pfn(pfn_t pfn)
823 return pfn == bad_pfn || pfn == hwpoison_pfn;
825 EXPORT_SYMBOL_GPL(is_error_pfn);
827 int is_hwpoison_pfn(pfn_t pfn)
829 return pfn == hwpoison_pfn;
831 EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
833 static inline unsigned long bad_hva(void)
838 int kvm_is_error_hva(unsigned long addr)
840 return addr == bad_hva();
842 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
844 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
847 struct kvm_memslots *slots = kvm_memslots(kvm);
849 for (i = 0; i < slots->nmemslots; ++i) {
850 struct kvm_memory_slot *memslot = &slots->memslots[i];
852 if (gfn >= memslot->base_gfn
853 && gfn < memslot->base_gfn + memslot->npages)
858 EXPORT_SYMBOL_GPL(gfn_to_memslot);
860 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
863 struct kvm_memslots *slots = kvm_memslots(kvm);
865 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
866 struct kvm_memory_slot *memslot = &slots->memslots[i];
868 if (memslot->flags & KVM_MEMSLOT_INVALID)
871 if (gfn >= memslot->base_gfn
872 && gfn < memslot->base_gfn + memslot->npages)
877 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
879 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
881 struct vm_area_struct *vma;
882 unsigned long addr, size;
886 addr = gfn_to_hva(kvm, gfn);
887 if (kvm_is_error_hva(addr))
890 down_read(¤t->mm->mmap_sem);
891 vma = find_vma(current->mm, addr);
895 size = vma_kernel_pagesize(vma);
898 up_read(¤t->mm->mmap_sem);
903 int memslot_id(struct kvm *kvm, gfn_t gfn)
906 struct kvm_memslots *slots = kvm_memslots(kvm);
907 struct kvm_memory_slot *memslot = NULL;
909 for (i = 0; i < slots->nmemslots; ++i) {
910 memslot = &slots->memslots[i];
912 if (gfn >= memslot->base_gfn
913 && gfn < memslot->base_gfn + memslot->npages)
917 return memslot - slots->memslots;
920 static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
922 return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
925 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
927 struct kvm_memory_slot *slot;
929 slot = gfn_to_memslot(kvm, gfn);
930 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
932 return gfn_to_hva_memslot(slot, gfn);
934 EXPORT_SYMBOL_GPL(gfn_to_hva);
936 static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr)
938 struct page *page[1];
944 npages = get_user_pages_fast(addr, 1, 1, page);
946 if (unlikely(npages != 1)) {
947 struct vm_area_struct *vma;
949 if (is_hwpoison_address(addr)) {
950 get_page(hwpoison_page);
951 return page_to_pfn(hwpoison_page);
954 down_read(¤t->mm->mmap_sem);
955 vma = find_vma(current->mm, addr);
957 if (vma == NULL || addr < vma->vm_start ||
958 !(vma->vm_flags & VM_PFNMAP)) {
959 up_read(¤t->mm->mmap_sem);
961 return page_to_pfn(bad_page);
964 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
965 up_read(¤t->mm->mmap_sem);
966 BUG_ON(!kvm_is_mmio_pfn(pfn));
968 pfn = page_to_pfn(page[0]);
973 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
977 addr = gfn_to_hva(kvm, gfn);
978 if (kvm_is_error_hva(addr)) {
980 return page_to_pfn(bad_page);
983 return hva_to_pfn(kvm, addr);
985 EXPORT_SYMBOL_GPL(gfn_to_pfn);
987 pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
988 struct kvm_memory_slot *slot, gfn_t gfn)
990 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
991 return hva_to_pfn(kvm, addr);
994 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
998 pfn = gfn_to_pfn(kvm, gfn);
999 if (!kvm_is_mmio_pfn(pfn))
1000 return pfn_to_page(pfn);
1002 WARN_ON(kvm_is_mmio_pfn(pfn));
1008 EXPORT_SYMBOL_GPL(gfn_to_page);
1010 void kvm_release_page_clean(struct page *page)
1012 kvm_release_pfn_clean(page_to_pfn(page));
1014 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1016 void kvm_release_pfn_clean(pfn_t pfn)
1018 if (!kvm_is_mmio_pfn(pfn))
1019 put_page(pfn_to_page(pfn));
1021 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1023 void kvm_release_page_dirty(struct page *page)
1025 kvm_release_pfn_dirty(page_to_pfn(page));
1027 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1029 void kvm_release_pfn_dirty(pfn_t pfn)
1031 kvm_set_pfn_dirty(pfn);
1032 kvm_release_pfn_clean(pfn);
1034 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1036 void kvm_set_page_dirty(struct page *page)
1038 kvm_set_pfn_dirty(page_to_pfn(page));
1040 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1042 void kvm_set_pfn_dirty(pfn_t pfn)
1044 if (!kvm_is_mmio_pfn(pfn)) {
1045 struct page *page = pfn_to_page(pfn);
1046 if (!PageReserved(page))
1050 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1052 void kvm_set_pfn_accessed(pfn_t pfn)
1054 if (!kvm_is_mmio_pfn(pfn))
1055 mark_page_accessed(pfn_to_page(pfn));
1057 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1059 void kvm_get_pfn(pfn_t pfn)
1061 if (!kvm_is_mmio_pfn(pfn))
1062 get_page(pfn_to_page(pfn));
1064 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1066 static int next_segment(unsigned long len, int offset)
1068 if (len > PAGE_SIZE - offset)
1069 return PAGE_SIZE - offset;
1074 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1080 addr = gfn_to_hva(kvm, gfn);
1081 if (kvm_is_error_hva(addr))
1083 r = copy_from_user(data, (void __user *)addr + offset, len);
1088 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1090 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1092 gfn_t gfn = gpa >> PAGE_SHIFT;
1094 int offset = offset_in_page(gpa);
1097 while ((seg = next_segment(len, offset)) != 0) {
1098 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1108 EXPORT_SYMBOL_GPL(kvm_read_guest);
1110 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1115 gfn_t gfn = gpa >> PAGE_SHIFT;
1116 int offset = offset_in_page(gpa);
1118 addr = gfn_to_hva(kvm, gfn);
1119 if (kvm_is_error_hva(addr))
1121 pagefault_disable();
1122 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1128 EXPORT_SYMBOL(kvm_read_guest_atomic);
1130 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1131 int offset, int len)
1136 addr = gfn_to_hva(kvm, gfn);
1137 if (kvm_is_error_hva(addr))
1139 r = copy_to_user((void __user *)addr + offset, data, len);
1142 mark_page_dirty(kvm, gfn);
1145 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1147 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1150 gfn_t gfn = gpa >> PAGE_SHIFT;
1152 int offset = offset_in_page(gpa);
1155 while ((seg = next_segment(len, offset)) != 0) {
1156 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1167 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1169 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1171 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1173 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1175 gfn_t gfn = gpa >> PAGE_SHIFT;
1177 int offset = offset_in_page(gpa);
1180 while ((seg = next_segment(len, offset)) != 0) {
1181 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1190 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1192 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1194 struct kvm_memory_slot *memslot;
1196 memslot = gfn_to_memslot(kvm, gfn);
1197 if (memslot && memslot->dirty_bitmap) {
1198 unsigned long rel_gfn = gfn - memslot->base_gfn;
1200 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
1205 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1207 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1212 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1214 if (kvm_arch_vcpu_runnable(vcpu)) {
1215 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1218 if (kvm_cpu_has_pending_timer(vcpu))
1220 if (signal_pending(current))
1226 finish_wait(&vcpu->wq, &wait);
1229 void kvm_resched(struct kvm_vcpu *vcpu)
1231 if (!need_resched())
1235 EXPORT_SYMBOL_GPL(kvm_resched);
1237 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1242 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1244 /* Sleep for 100 us, and hope lock-holder got scheduled */
1245 expires = ktime_add_ns(ktime_get(), 100000UL);
1246 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1248 finish_wait(&vcpu->wq, &wait);
1250 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1252 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1254 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1257 if (vmf->pgoff == 0)
1258 page = virt_to_page(vcpu->run);
1260 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1261 page = virt_to_page(vcpu->arch.pio_data);
1263 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1264 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1265 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1268 return VM_FAULT_SIGBUS;
1274 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1275 .fault = kvm_vcpu_fault,
1278 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1280 vma->vm_ops = &kvm_vcpu_vm_ops;
1284 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1286 struct kvm_vcpu *vcpu = filp->private_data;
1288 kvm_put_kvm(vcpu->kvm);
1292 static struct file_operations kvm_vcpu_fops = {
1293 .release = kvm_vcpu_release,
1294 .unlocked_ioctl = kvm_vcpu_ioctl,
1295 .compat_ioctl = kvm_vcpu_ioctl,
1296 .mmap = kvm_vcpu_mmap,
1300 * Allocates an inode for the vcpu.
1302 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1304 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
1308 * Creates some virtual cpus. Good luck creating more than one.
1310 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1313 struct kvm_vcpu *vcpu, *v;
1315 vcpu = kvm_arch_vcpu_create(kvm, id);
1317 return PTR_ERR(vcpu);
1319 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1321 r = kvm_arch_vcpu_setup(vcpu);
1325 mutex_lock(&kvm->lock);
1326 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1331 kvm_for_each_vcpu(r, v, kvm)
1332 if (v->vcpu_id == id) {
1337 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1339 /* Now it's all set up, let userspace reach it */
1341 r = create_vcpu_fd(vcpu);
1347 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1349 atomic_inc(&kvm->online_vcpus);
1351 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1352 if (kvm->bsp_vcpu_id == id)
1353 kvm->bsp_vcpu = vcpu;
1355 mutex_unlock(&kvm->lock);
1359 mutex_unlock(&kvm->lock);
1360 kvm_arch_vcpu_destroy(vcpu);
1364 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1367 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1368 vcpu->sigset_active = 1;
1369 vcpu->sigset = *sigset;
1371 vcpu->sigset_active = 0;
1375 static long kvm_vcpu_ioctl(struct file *filp,
1376 unsigned int ioctl, unsigned long arg)
1378 struct kvm_vcpu *vcpu = filp->private_data;
1379 void __user *argp = (void __user *)arg;
1381 struct kvm_fpu *fpu = NULL;
1382 struct kvm_sregs *kvm_sregs = NULL;
1384 if (vcpu->kvm->mm != current->mm)
1387 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1389 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1390 * so vcpu_load() would break it.
1392 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1393 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1403 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1405 case KVM_GET_REGS: {
1406 struct kvm_regs *kvm_regs;
1409 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1412 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1416 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1423 case KVM_SET_REGS: {
1424 struct kvm_regs *kvm_regs;
1427 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1431 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1433 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1441 case KVM_GET_SREGS: {
1442 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1446 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1450 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1455 case KVM_SET_SREGS: {
1456 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1461 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1463 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1469 case KVM_GET_MP_STATE: {
1470 struct kvm_mp_state mp_state;
1472 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1476 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1481 case KVM_SET_MP_STATE: {
1482 struct kvm_mp_state mp_state;
1485 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1487 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1493 case KVM_TRANSLATE: {
1494 struct kvm_translation tr;
1497 if (copy_from_user(&tr, argp, sizeof tr))
1499 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1503 if (copy_to_user(argp, &tr, sizeof tr))
1508 case KVM_SET_GUEST_DEBUG: {
1509 struct kvm_guest_debug dbg;
1512 if (copy_from_user(&dbg, argp, sizeof dbg))
1514 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1520 case KVM_SET_SIGNAL_MASK: {
1521 struct kvm_signal_mask __user *sigmask_arg = argp;
1522 struct kvm_signal_mask kvm_sigmask;
1523 sigset_t sigset, *p;
1528 if (copy_from_user(&kvm_sigmask, argp,
1529 sizeof kvm_sigmask))
1532 if (kvm_sigmask.len != sizeof sigset)
1535 if (copy_from_user(&sigset, sigmask_arg->sigset,
1540 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
1544 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1548 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1552 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1558 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1563 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1565 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1572 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1581 static long kvm_vm_ioctl(struct file *filp,
1582 unsigned int ioctl, unsigned long arg)
1584 struct kvm *kvm = filp->private_data;
1585 void __user *argp = (void __user *)arg;
1588 if (kvm->mm != current->mm)
1591 case KVM_CREATE_VCPU:
1592 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1596 case KVM_SET_USER_MEMORY_REGION: {
1597 struct kvm_userspace_memory_region kvm_userspace_mem;
1600 if (copy_from_user(&kvm_userspace_mem, argp,
1601 sizeof kvm_userspace_mem))
1604 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1609 case KVM_GET_DIRTY_LOG: {
1610 struct kvm_dirty_log log;
1613 if (copy_from_user(&log, argp, sizeof log))
1615 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1620 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1621 case KVM_REGISTER_COALESCED_MMIO: {
1622 struct kvm_coalesced_mmio_zone zone;
1624 if (copy_from_user(&zone, argp, sizeof zone))
1626 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1632 case KVM_UNREGISTER_COALESCED_MMIO: {
1633 struct kvm_coalesced_mmio_zone zone;
1635 if (copy_from_user(&zone, argp, sizeof zone))
1637 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1645 struct kvm_irqfd data;
1648 if (copy_from_user(&data, argp, sizeof data))
1650 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1653 case KVM_IOEVENTFD: {
1654 struct kvm_ioeventfd data;
1657 if (copy_from_user(&data, argp, sizeof data))
1659 r = kvm_ioeventfd(kvm, &data);
1662 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1663 case KVM_SET_BOOT_CPU_ID:
1665 mutex_lock(&kvm->lock);
1666 if (atomic_read(&kvm->online_vcpus) != 0)
1669 kvm->bsp_vcpu_id = arg;
1670 mutex_unlock(&kvm->lock);
1674 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1676 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
1682 #ifdef CONFIG_COMPAT
1683 struct compat_kvm_dirty_log {
1687 compat_uptr_t dirty_bitmap; /* one bit per page */
1692 static long kvm_vm_compat_ioctl(struct file *filp,
1693 unsigned int ioctl, unsigned long arg)
1695 struct kvm *kvm = filp->private_data;
1698 if (kvm->mm != current->mm)
1701 case KVM_GET_DIRTY_LOG: {
1702 struct compat_kvm_dirty_log compat_log;
1703 struct kvm_dirty_log log;
1706 if (copy_from_user(&compat_log, (void __user *)arg,
1707 sizeof(compat_log)))
1709 log.slot = compat_log.slot;
1710 log.padding1 = compat_log.padding1;
1711 log.padding2 = compat_log.padding2;
1712 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1714 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1720 r = kvm_vm_ioctl(filp, ioctl, arg);
1728 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1730 struct page *page[1];
1733 gfn_t gfn = vmf->pgoff;
1734 struct kvm *kvm = vma->vm_file->private_data;
1736 addr = gfn_to_hva(kvm, gfn);
1737 if (kvm_is_error_hva(addr))
1738 return VM_FAULT_SIGBUS;
1740 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1742 if (unlikely(npages != 1))
1743 return VM_FAULT_SIGBUS;
1745 vmf->page = page[0];
1749 static const struct vm_operations_struct kvm_vm_vm_ops = {
1750 .fault = kvm_vm_fault,
1753 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1755 vma->vm_ops = &kvm_vm_vm_ops;
1759 static struct file_operations kvm_vm_fops = {
1760 .release = kvm_vm_release,
1761 .unlocked_ioctl = kvm_vm_ioctl,
1762 #ifdef CONFIG_COMPAT
1763 .compat_ioctl = kvm_vm_compat_ioctl,
1765 .mmap = kvm_vm_mmap,
1768 static int kvm_dev_ioctl_create_vm(void)
1773 kvm = kvm_create_vm();
1775 return PTR_ERR(kvm);
1776 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1777 r = kvm_coalesced_mmio_init(kvm);
1783 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
1790 static long kvm_dev_ioctl_check_extension_generic(long arg)
1793 case KVM_CAP_USER_MEMORY:
1794 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
1795 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
1796 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1797 case KVM_CAP_SET_BOOT_CPU_ID:
1799 case KVM_CAP_INTERNAL_ERROR_DATA:
1801 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1802 case KVM_CAP_IRQ_ROUTING:
1803 return KVM_MAX_IRQ_ROUTES;
1808 return kvm_dev_ioctl_check_extension(arg);
1811 static long kvm_dev_ioctl(struct file *filp,
1812 unsigned int ioctl, unsigned long arg)
1817 case KVM_GET_API_VERSION:
1821 r = KVM_API_VERSION;
1827 r = kvm_dev_ioctl_create_vm();
1829 case KVM_CHECK_EXTENSION:
1830 r = kvm_dev_ioctl_check_extension_generic(arg);
1832 case KVM_GET_VCPU_MMAP_SIZE:
1836 r = PAGE_SIZE; /* struct kvm_run */
1838 r += PAGE_SIZE; /* pio data page */
1840 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1841 r += PAGE_SIZE; /* coalesced mmio ring page */
1844 case KVM_TRACE_ENABLE:
1845 case KVM_TRACE_PAUSE:
1846 case KVM_TRACE_DISABLE:
1850 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1856 static struct file_operations kvm_chardev_ops = {
1857 .unlocked_ioctl = kvm_dev_ioctl,
1858 .compat_ioctl = kvm_dev_ioctl,
1861 static struct miscdevice kvm_dev = {
1867 static void hardware_enable(void *junk)
1869 int cpu = raw_smp_processor_id();
1872 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
1875 cpumask_set_cpu(cpu, cpus_hardware_enabled);
1877 r = kvm_arch_hardware_enable(NULL);
1880 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1881 atomic_inc(&hardware_enable_failed);
1882 printk(KERN_INFO "kvm: enabling virtualization on "
1883 "CPU%d failed\n", cpu);
1887 static void hardware_disable(void *junk)
1889 int cpu = raw_smp_processor_id();
1891 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
1893 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1894 kvm_arch_hardware_disable(NULL);
1897 static void hardware_disable_all_nolock(void)
1899 BUG_ON(!kvm_usage_count);
1902 if (!kvm_usage_count)
1903 on_each_cpu(hardware_disable, NULL, 1);
1906 static void hardware_disable_all(void)
1908 spin_lock(&kvm_lock);
1909 hardware_disable_all_nolock();
1910 spin_unlock(&kvm_lock);
1913 static int hardware_enable_all(void)
1917 spin_lock(&kvm_lock);
1920 if (kvm_usage_count == 1) {
1921 atomic_set(&hardware_enable_failed, 0);
1922 on_each_cpu(hardware_enable, NULL, 1);
1924 if (atomic_read(&hardware_enable_failed)) {
1925 hardware_disable_all_nolock();
1930 spin_unlock(&kvm_lock);
1935 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1940 if (!kvm_usage_count)
1943 val &= ~CPU_TASKS_FROZEN;
1946 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1948 hardware_disable(NULL);
1951 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1953 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1960 asmlinkage void kvm_handle_fault_on_reboot(void)
1963 /* spin while reset goes on */
1966 /* Fault while not rebooting. We want the trace. */
1969 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1971 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1975 * Some (well, at least mine) BIOSes hang on reboot if
1978 * And Intel TXT required VMX off for all cpu when system shutdown.
1980 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1981 kvm_rebooting = true;
1982 on_each_cpu(hardware_disable, NULL, 1);
1986 static struct notifier_block kvm_reboot_notifier = {
1987 .notifier_call = kvm_reboot,
1991 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1995 for (i = 0; i < bus->dev_count; i++) {
1996 struct kvm_io_device *pos = bus->devs[i];
1998 kvm_iodevice_destructor(pos);
2003 /* kvm_io_bus_write - called under kvm->slots_lock */
2004 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2005 int len, const void *val)
2008 struct kvm_io_bus *bus;
2010 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2011 for (i = 0; i < bus->dev_count; i++)
2012 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2017 /* kvm_io_bus_read - called under kvm->slots_lock */
2018 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2022 struct kvm_io_bus *bus;
2024 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2025 for (i = 0; i < bus->dev_count; i++)
2026 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2031 /* Caller must hold slots_lock. */
2032 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2033 struct kvm_io_device *dev)
2035 struct kvm_io_bus *new_bus, *bus;
2037 bus = kvm->buses[bus_idx];
2038 if (bus->dev_count > NR_IOBUS_DEVS-1)
2041 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2044 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2045 new_bus->devs[new_bus->dev_count++] = dev;
2046 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2047 synchronize_srcu_expedited(&kvm->srcu);
2053 /* Caller must hold slots_lock. */
2054 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2055 struct kvm_io_device *dev)
2058 struct kvm_io_bus *new_bus, *bus;
2060 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2064 bus = kvm->buses[bus_idx];
2065 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2068 for (i = 0; i < new_bus->dev_count; i++)
2069 if (new_bus->devs[i] == dev) {
2071 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
2080 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2081 synchronize_srcu_expedited(&kvm->srcu);
2086 static struct notifier_block kvm_cpu_notifier = {
2087 .notifier_call = kvm_cpu_hotplug,
2088 .priority = 20, /* must be > scheduler priority */
2091 static int vm_stat_get(void *_offset, u64 *val)
2093 unsigned offset = (long)_offset;
2097 spin_lock(&kvm_lock);
2098 list_for_each_entry(kvm, &vm_list, vm_list)
2099 *val += *(u32 *)((void *)kvm + offset);
2100 spin_unlock(&kvm_lock);
2104 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2106 static int vcpu_stat_get(void *_offset, u64 *val)
2108 unsigned offset = (long)_offset;
2110 struct kvm_vcpu *vcpu;
2114 spin_lock(&kvm_lock);
2115 list_for_each_entry(kvm, &vm_list, vm_list)
2116 kvm_for_each_vcpu(i, vcpu, kvm)
2117 *val += *(u32 *)((void *)vcpu + offset);
2119 spin_unlock(&kvm_lock);
2123 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2125 static const struct file_operations *stat_fops[] = {
2126 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2127 [KVM_STAT_VM] = &vm_stat_fops,
2130 static void kvm_init_debug(void)
2132 struct kvm_stats_debugfs_item *p;
2134 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2135 for (p = debugfs_entries; p->name; ++p)
2136 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2137 (void *)(long)p->offset,
2138 stat_fops[p->kind]);
2141 static void kvm_exit_debug(void)
2143 struct kvm_stats_debugfs_item *p;
2145 for (p = debugfs_entries; p->name; ++p)
2146 debugfs_remove(p->dentry);
2147 debugfs_remove(kvm_debugfs_dir);
2150 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2152 if (kvm_usage_count)
2153 hardware_disable(NULL);
2157 static int kvm_resume(struct sys_device *dev)
2159 if (kvm_usage_count)
2160 hardware_enable(NULL);
2164 static struct sysdev_class kvm_sysdev_class = {
2166 .suspend = kvm_suspend,
2167 .resume = kvm_resume,
2170 static struct sys_device kvm_sysdev = {
2172 .cls = &kvm_sysdev_class,
2175 struct page *bad_page;
2179 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2181 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2184 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2186 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2188 kvm_arch_vcpu_load(vcpu, cpu);
2191 static void kvm_sched_out(struct preempt_notifier *pn,
2192 struct task_struct *next)
2194 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2196 kvm_arch_vcpu_put(vcpu);
2199 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
2200 struct module *module)
2205 r = kvm_arch_init(opaque);
2209 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2211 if (bad_page == NULL) {
2216 bad_pfn = page_to_pfn(bad_page);
2218 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2220 if (hwpoison_page == NULL) {
2225 hwpoison_pfn = page_to_pfn(hwpoison_page);
2227 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2232 r = kvm_arch_hardware_setup();
2236 for_each_online_cpu(cpu) {
2237 smp_call_function_single(cpu,
2238 kvm_arch_check_processor_compat,
2244 r = register_cpu_notifier(&kvm_cpu_notifier);
2247 register_reboot_notifier(&kvm_reboot_notifier);
2249 r = sysdev_class_register(&kvm_sysdev_class);
2253 r = sysdev_register(&kvm_sysdev);
2257 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2259 vcpu_align = __alignof__(struct kvm_vcpu);
2260 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
2262 if (!kvm_vcpu_cache) {
2267 kvm_chardev_ops.owner = module;
2268 kvm_vm_fops.owner = module;
2269 kvm_vcpu_fops.owner = module;
2271 r = misc_register(&kvm_dev);
2273 printk(KERN_ERR "kvm: misc device register failed\n");
2277 kvm_preempt_ops.sched_in = kvm_sched_in;
2278 kvm_preempt_ops.sched_out = kvm_sched_out;
2285 kmem_cache_destroy(kvm_vcpu_cache);
2287 sysdev_unregister(&kvm_sysdev);
2289 sysdev_class_unregister(&kvm_sysdev_class);
2291 unregister_reboot_notifier(&kvm_reboot_notifier);
2292 unregister_cpu_notifier(&kvm_cpu_notifier);
2295 kvm_arch_hardware_unsetup();
2297 free_cpumask_var(cpus_hardware_enabled);
2300 __free_page(hwpoison_page);
2301 __free_page(bad_page);
2307 EXPORT_SYMBOL_GPL(kvm_init);
2312 misc_deregister(&kvm_dev);
2313 kmem_cache_destroy(kvm_vcpu_cache);
2314 sysdev_unregister(&kvm_sysdev);
2315 sysdev_class_unregister(&kvm_sysdev_class);
2316 unregister_reboot_notifier(&kvm_reboot_notifier);
2317 unregister_cpu_notifier(&kvm_cpu_notifier);
2318 on_each_cpu(hardware_disable, NULL, 1);
2319 kvm_arch_hardware_unsetup();
2321 free_cpumask_var(cpus_hardware_enabled);
2322 __free_page(hwpoison_page);
2323 __free_page(bad_page);
2325 EXPORT_SYMBOL_GPL(kvm_exit);