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.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/percpu.h>
26 #include <linux/miscdevice.h>
27 #include <linux/vmalloc.h>
28 #include <linux/reboot.h>
29 #include <linux/debugfs.h>
30 #include <linux/highmem.h>
31 #include <linux/file.h>
32 #include <linux/sysdev.h>
33 #include <linux/cpu.h>
34 #include <linux/sched.h>
35 #include <linux/cpumask.h>
36 #include <linux/smp.h>
37 #include <linux/anon_inodes.h>
38 #include <linux/profile.h>
39 #include <linux/kvm_para.h>
40 #include <linux/pagemap.h>
41 #include <linux/mman.h>
42 #include <linux/swap.h>
43 #include <linux/bitops.h>
44 #include <linux/spinlock.h>
45 #include <linux/compat.h>
46 #include <linux/srcu.h>
47 #include <linux/hugetlb.h>
48 #include <linux/slab.h>
50 #include <asm/processor.h>
52 #include <asm/uaccess.h>
53 #include <asm/pgtable.h>
54 #include <asm-generic/bitops/le.h>
56 #include "coalesced_mmio.h"
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/kvm.h>
61 MODULE_AUTHOR("Qumranet");
62 MODULE_LICENSE("GPL");
67 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
70 DEFINE_SPINLOCK(kvm_lock);
73 static cpumask_var_t cpus_hardware_enabled;
74 static int kvm_usage_count = 0;
75 static atomic_t hardware_enable_failed;
77 struct kmem_cache *kvm_vcpu_cache;
78 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
80 static __read_mostly struct preempt_ops kvm_preempt_ops;
82 struct dentry *kvm_debugfs_dir;
84 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
86 static int hardware_enable_all(void);
87 static void hardware_disable_all(void);
89 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
91 static bool kvm_rebooting;
93 static bool largepages_enabled = true;
95 inline int kvm_is_mmio_pfn(pfn_t pfn)
98 struct page *page = compound_head(pfn_to_page(pfn));
99 return PageReserved(page);
106 * Switches to specified vcpu, until a matching vcpu_put()
108 void vcpu_load(struct kvm_vcpu *vcpu)
112 mutex_lock(&vcpu->mutex);
114 preempt_notifier_register(&vcpu->preempt_notifier);
115 kvm_arch_vcpu_load(vcpu, cpu);
119 void vcpu_put(struct kvm_vcpu *vcpu)
122 kvm_arch_vcpu_put(vcpu);
123 preempt_notifier_unregister(&vcpu->preempt_notifier);
125 mutex_unlock(&vcpu->mutex);
128 static void ack_flush(void *_completed)
132 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
137 struct kvm_vcpu *vcpu;
139 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
141 raw_spin_lock(&kvm->requests_lock);
142 me = smp_processor_id();
143 kvm_for_each_vcpu(i, vcpu, kvm) {
144 if (test_and_set_bit(req, &vcpu->requests))
147 if (cpus != NULL && cpu != -1 && cpu != me)
148 cpumask_set_cpu(cpu, cpus);
150 if (unlikely(cpus == NULL))
151 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
152 else if (!cpumask_empty(cpus))
153 smp_call_function_many(cpus, ack_flush, NULL, 1);
156 raw_spin_unlock(&kvm->requests_lock);
157 free_cpumask_var(cpus);
161 void kvm_flush_remote_tlbs(struct kvm *kvm)
163 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
164 ++kvm->stat.remote_tlb_flush;
167 void kvm_reload_remote_mmus(struct kvm *kvm)
169 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
172 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
177 mutex_init(&vcpu->mutex);
181 init_waitqueue_head(&vcpu->wq);
183 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
188 vcpu->run = page_address(page);
190 r = kvm_arch_vcpu_init(vcpu);
196 free_page((unsigned long)vcpu->run);
200 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
202 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
204 kvm_arch_vcpu_uninit(vcpu);
205 free_page((unsigned long)vcpu->run);
207 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
209 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
210 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
212 return container_of(mn, struct kvm, mmu_notifier);
215 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
216 struct mm_struct *mm,
217 unsigned long address)
219 struct kvm *kvm = mmu_notifier_to_kvm(mn);
220 int need_tlb_flush, idx;
223 * When ->invalidate_page runs, the linux pte has been zapped
224 * already but the page is still allocated until
225 * ->invalidate_page returns. So if we increase the sequence
226 * here the kvm page fault will notice if the spte can't be
227 * established because the page is going to be freed. If
228 * instead the kvm page fault establishes the spte before
229 * ->invalidate_page runs, kvm_unmap_hva will release it
232 * The sequence increase only need to be seen at spin_unlock
233 * time, and not at spin_lock time.
235 * Increasing the sequence after the spin_unlock would be
236 * unsafe because the kvm page fault could then establish the
237 * pte after kvm_unmap_hva returned, without noticing the page
238 * is going to be freed.
240 idx = srcu_read_lock(&kvm->srcu);
241 spin_lock(&kvm->mmu_lock);
242 kvm->mmu_notifier_seq++;
243 need_tlb_flush = kvm_unmap_hva(kvm, address);
244 spin_unlock(&kvm->mmu_lock);
245 srcu_read_unlock(&kvm->srcu, idx);
247 /* we've to flush the tlb before the pages can be freed */
249 kvm_flush_remote_tlbs(kvm);
253 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
254 struct mm_struct *mm,
255 unsigned long address,
258 struct kvm *kvm = mmu_notifier_to_kvm(mn);
261 idx = srcu_read_lock(&kvm->srcu);
262 spin_lock(&kvm->mmu_lock);
263 kvm->mmu_notifier_seq++;
264 kvm_set_spte_hva(kvm, address, pte);
265 spin_unlock(&kvm->mmu_lock);
266 srcu_read_unlock(&kvm->srcu, idx);
269 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
270 struct mm_struct *mm,
274 struct kvm *kvm = mmu_notifier_to_kvm(mn);
275 int need_tlb_flush = 0, idx;
277 idx = srcu_read_lock(&kvm->srcu);
278 spin_lock(&kvm->mmu_lock);
280 * The count increase must become visible at unlock time as no
281 * spte can be established without taking the mmu_lock and
282 * count is also read inside the mmu_lock critical section.
284 kvm->mmu_notifier_count++;
285 for (; start < end; start += PAGE_SIZE)
286 need_tlb_flush |= kvm_unmap_hva(kvm, start);
287 spin_unlock(&kvm->mmu_lock);
288 srcu_read_unlock(&kvm->srcu, idx);
290 /* we've to flush the tlb before the pages can be freed */
292 kvm_flush_remote_tlbs(kvm);
295 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
296 struct mm_struct *mm,
300 struct kvm *kvm = mmu_notifier_to_kvm(mn);
302 spin_lock(&kvm->mmu_lock);
304 * This sequence increase will notify the kvm page fault that
305 * the page that is going to be mapped in the spte could have
308 kvm->mmu_notifier_seq++;
310 * The above sequence increase must be visible before the
311 * below count decrease but both values are read by the kvm
312 * page fault under mmu_lock spinlock so we don't need to add
313 * a smb_wmb() here in between the two.
315 kvm->mmu_notifier_count--;
316 spin_unlock(&kvm->mmu_lock);
318 BUG_ON(kvm->mmu_notifier_count < 0);
321 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
322 struct mm_struct *mm,
323 unsigned long address)
325 struct kvm *kvm = mmu_notifier_to_kvm(mn);
328 idx = srcu_read_lock(&kvm->srcu);
329 spin_lock(&kvm->mmu_lock);
330 young = kvm_age_hva(kvm, address);
331 spin_unlock(&kvm->mmu_lock);
332 srcu_read_unlock(&kvm->srcu, idx);
335 kvm_flush_remote_tlbs(kvm);
340 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
341 struct mm_struct *mm)
343 struct kvm *kvm = mmu_notifier_to_kvm(mn);
346 idx = srcu_read_lock(&kvm->srcu);
347 kvm_arch_flush_shadow(kvm);
348 srcu_read_unlock(&kvm->srcu, idx);
351 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
352 .invalidate_page = kvm_mmu_notifier_invalidate_page,
353 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
354 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
355 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
356 .change_pte = kvm_mmu_notifier_change_pte,
357 .release = kvm_mmu_notifier_release,
360 static int kvm_init_mmu_notifier(struct kvm *kvm)
362 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
363 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
366 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
368 static int kvm_init_mmu_notifier(struct kvm *kvm)
373 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
375 static struct kvm *kvm_create_vm(void)
378 struct kvm *kvm = kvm_arch_create_vm();
383 r = hardware_enable_all();
385 goto out_err_nodisable;
387 #ifdef CONFIG_HAVE_KVM_IRQCHIP
388 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
389 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
393 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
396 if (init_srcu_struct(&kvm->srcu))
398 for (i = 0; i < KVM_NR_BUSES; i++) {
399 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
401 if (!kvm->buses[i]) {
402 cleanup_srcu_struct(&kvm->srcu);
407 r = kvm_init_mmu_notifier(kvm);
409 cleanup_srcu_struct(&kvm->srcu);
413 kvm->mm = current->mm;
414 atomic_inc(&kvm->mm->mm_count);
415 spin_lock_init(&kvm->mmu_lock);
416 raw_spin_lock_init(&kvm->requests_lock);
417 kvm_eventfd_init(kvm);
418 mutex_init(&kvm->lock);
419 mutex_init(&kvm->irq_lock);
420 mutex_init(&kvm->slots_lock);
421 atomic_set(&kvm->users_count, 1);
422 spin_lock(&kvm_lock);
423 list_add(&kvm->vm_list, &vm_list);
424 spin_unlock(&kvm_lock);
429 hardware_disable_all();
431 for (i = 0; i < KVM_NR_BUSES; i++)
432 kfree(kvm->buses[i]);
433 kfree(kvm->memslots);
439 * Free any memory in @free but not in @dont.
441 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
442 struct kvm_memory_slot *dont)
446 if (!dont || free->rmap != dont->rmap)
449 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
450 vfree(free->dirty_bitmap);
453 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
454 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
455 vfree(free->lpage_info[i]);
456 free->lpage_info[i] = NULL;
461 free->dirty_bitmap = NULL;
465 void kvm_free_physmem(struct kvm *kvm)
468 struct kvm_memslots *slots = kvm->memslots;
470 for (i = 0; i < slots->nmemslots; ++i)
471 kvm_free_physmem_slot(&slots->memslots[i], NULL);
473 kfree(kvm->memslots);
476 static void kvm_destroy_vm(struct kvm *kvm)
479 struct mm_struct *mm = kvm->mm;
481 kvm_arch_sync_events(kvm);
482 spin_lock(&kvm_lock);
483 list_del(&kvm->vm_list);
484 spin_unlock(&kvm_lock);
485 kvm_free_irq_routing(kvm);
486 for (i = 0; i < KVM_NR_BUSES; i++)
487 kvm_io_bus_destroy(kvm->buses[i]);
488 kvm_coalesced_mmio_free(kvm);
489 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
490 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
492 kvm_arch_flush_shadow(kvm);
494 kvm_arch_destroy_vm(kvm);
495 hardware_disable_all();
499 void kvm_get_kvm(struct kvm *kvm)
501 atomic_inc(&kvm->users_count);
503 EXPORT_SYMBOL_GPL(kvm_get_kvm);
505 void kvm_put_kvm(struct kvm *kvm)
507 if (atomic_dec_and_test(&kvm->users_count))
510 EXPORT_SYMBOL_GPL(kvm_put_kvm);
513 static int kvm_vm_release(struct inode *inode, struct file *filp)
515 struct kvm *kvm = filp->private_data;
517 kvm_irqfd_release(kvm);
524 * Allocate some memory and give it an address in the guest physical address
527 * Discontiguous memory is allowed, mostly for framebuffers.
529 * Must be called holding mmap_sem for write.
531 int __kvm_set_memory_region(struct kvm *kvm,
532 struct kvm_userspace_memory_region *mem,
535 int r, flush_shadow = 0;
537 unsigned long npages;
539 struct kvm_memory_slot *memslot;
540 struct kvm_memory_slot old, new;
541 struct kvm_memslots *slots, *old_memslots;
544 /* General sanity checks */
545 if (mem->memory_size & (PAGE_SIZE - 1))
547 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
549 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
551 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
553 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
556 memslot = &kvm->memslots->memslots[mem->slot];
557 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
558 npages = mem->memory_size >> PAGE_SHIFT;
561 if (npages > KVM_MEM_MAX_NR_PAGES)
565 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
567 new = old = *memslot;
569 new.base_gfn = base_gfn;
571 new.flags = mem->flags;
573 /* Disallow changing a memory slot's size. */
575 if (npages && old.npages && npages != old.npages)
578 /* Check for overlaps */
580 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
581 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
583 if (s == memslot || !s->npages)
585 if (!((base_gfn + npages <= s->base_gfn) ||
586 (base_gfn >= s->base_gfn + s->npages)))
590 /* Free page dirty bitmap if unneeded */
591 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
592 new.dirty_bitmap = NULL;
596 /* Allocate if a slot is being created */
598 if (npages && !new.rmap) {
599 new.rmap = vmalloc(npages * sizeof(struct page *));
604 memset(new.rmap, 0, npages * sizeof(*new.rmap));
606 new.user_alloc = user_alloc;
607 new.userspace_addr = mem->userspace_addr;
612 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
618 /* Avoid unused variable warning if no large pages */
621 if (new.lpage_info[i])
624 lpages = 1 + (base_gfn + npages - 1) /
625 KVM_PAGES_PER_HPAGE(level);
626 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
628 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
630 if (!new.lpage_info[i])
633 memset(new.lpage_info[i], 0,
634 lpages * sizeof(*new.lpage_info[i]));
636 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
637 new.lpage_info[i][0].write_count = 1;
638 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
639 new.lpage_info[i][lpages - 1].write_count = 1;
640 ugfn = new.userspace_addr >> PAGE_SHIFT;
642 * If the gfn and userspace address are not aligned wrt each
643 * other, or if explicitly asked to, disable large page
644 * support for this slot
646 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
648 for (j = 0; j < lpages; ++j)
649 new.lpage_info[i][j].write_count = 1;
654 /* Allocate page dirty bitmap if needed */
655 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
656 unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(&new);
658 new.dirty_bitmap = vmalloc(dirty_bytes);
659 if (!new.dirty_bitmap)
661 memset(new.dirty_bitmap, 0, dirty_bytes);
662 /* destroy any largepage mappings for dirty tracking */
666 #else /* not defined CONFIG_S390 */
667 new.user_alloc = user_alloc;
669 new.userspace_addr = mem->userspace_addr;
670 #endif /* not defined CONFIG_S390 */
674 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
677 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
678 if (mem->slot >= slots->nmemslots)
679 slots->nmemslots = mem->slot + 1;
680 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
682 old_memslots = kvm->memslots;
683 rcu_assign_pointer(kvm->memslots, slots);
684 synchronize_srcu_expedited(&kvm->srcu);
685 /* From this point no new shadow pages pointing to a deleted
686 * memslot will be created.
688 * validation of sp->gfn happens in:
689 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
690 * - kvm_is_visible_gfn (mmu_check_roots)
692 kvm_arch_flush_shadow(kvm);
696 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
701 /* map the pages in iommu page table */
703 r = kvm_iommu_map_pages(kvm, &new);
710 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
713 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
714 if (mem->slot >= slots->nmemslots)
715 slots->nmemslots = mem->slot + 1;
717 /* actual memory is freed via old in kvm_free_physmem_slot below */
720 new.dirty_bitmap = NULL;
721 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
722 new.lpage_info[i] = NULL;
725 slots->memslots[mem->slot] = new;
726 old_memslots = kvm->memslots;
727 rcu_assign_pointer(kvm->memslots, slots);
728 synchronize_srcu_expedited(&kvm->srcu);
730 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
732 kvm_free_physmem_slot(&old, &new);
736 kvm_arch_flush_shadow(kvm);
741 kvm_free_physmem_slot(&new, &old);
746 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
748 int kvm_set_memory_region(struct kvm *kvm,
749 struct kvm_userspace_memory_region *mem,
754 mutex_lock(&kvm->slots_lock);
755 r = __kvm_set_memory_region(kvm, mem, user_alloc);
756 mutex_unlock(&kvm->slots_lock);
759 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
761 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
763 kvm_userspace_memory_region *mem,
766 if (mem->slot >= KVM_MEMORY_SLOTS)
768 return kvm_set_memory_region(kvm, mem, user_alloc);
771 int kvm_get_dirty_log(struct kvm *kvm,
772 struct kvm_dirty_log *log, int *is_dirty)
774 struct kvm_memory_slot *memslot;
777 unsigned long any = 0;
780 if (log->slot >= KVM_MEMORY_SLOTS)
783 memslot = &kvm->memslots->memslots[log->slot];
785 if (!memslot->dirty_bitmap)
788 n = kvm_dirty_bitmap_bytes(memslot);
790 for (i = 0; !any && i < n/sizeof(long); ++i)
791 any = memslot->dirty_bitmap[i];
794 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
805 void kvm_disable_largepages(void)
807 largepages_enabled = false;
809 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
811 int is_error_page(struct page *page)
813 return page == bad_page;
815 EXPORT_SYMBOL_GPL(is_error_page);
817 int is_error_pfn(pfn_t pfn)
819 return pfn == bad_pfn;
821 EXPORT_SYMBOL_GPL(is_error_pfn);
823 static inline unsigned long bad_hva(void)
828 int kvm_is_error_hva(unsigned long addr)
830 return addr == bad_hva();
832 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
834 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
837 struct kvm_memslots *slots = kvm_memslots(kvm);
839 for (i = 0; i < slots->nmemslots; ++i) {
840 struct kvm_memory_slot *memslot = &slots->memslots[i];
842 if (gfn >= memslot->base_gfn
843 && gfn < memslot->base_gfn + memslot->npages)
848 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
850 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
852 gfn = unalias_gfn(kvm, gfn);
853 return gfn_to_memslot_unaliased(kvm, gfn);
856 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
859 struct kvm_memslots *slots = kvm_memslots(kvm);
861 gfn = unalias_gfn_instantiation(kvm, gfn);
862 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
863 struct kvm_memory_slot *memslot = &slots->memslots[i];
865 if (memslot->flags & KVM_MEMSLOT_INVALID)
868 if (gfn >= memslot->base_gfn
869 && gfn < memslot->base_gfn + memslot->npages)
874 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
876 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
878 struct vm_area_struct *vma;
879 unsigned long addr, size;
883 addr = gfn_to_hva(kvm, gfn);
884 if (kvm_is_error_hva(addr))
887 down_read(¤t->mm->mmap_sem);
888 vma = find_vma(current->mm, addr);
892 size = vma_kernel_pagesize(vma);
895 up_read(¤t->mm->mmap_sem);
900 int memslot_id(struct kvm *kvm, gfn_t gfn)
903 struct kvm_memslots *slots = kvm_memslots(kvm);
904 struct kvm_memory_slot *memslot = NULL;
906 gfn = unalias_gfn(kvm, gfn);
907 for (i = 0; i < slots->nmemslots; ++i) {
908 memslot = &slots->memslots[i];
910 if (gfn >= memslot->base_gfn
911 && gfn < memslot->base_gfn + memslot->npages)
915 return memslot - slots->memslots;
918 static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
920 return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
923 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
925 struct kvm_memory_slot *slot;
927 gfn = unalias_gfn_instantiation(kvm, gfn);
928 slot = gfn_to_memslot_unaliased(kvm, gfn);
929 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
931 return gfn_to_hva_memslot(slot, gfn);
933 EXPORT_SYMBOL_GPL(gfn_to_hva);
935 static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr)
937 struct page *page[1];
943 npages = get_user_pages_fast(addr, 1, 1, page);
945 if (unlikely(npages != 1)) {
946 struct vm_area_struct *vma;
948 down_read(¤t->mm->mmap_sem);
949 vma = find_vma(current->mm, addr);
951 if (vma == NULL || addr < vma->vm_start ||
952 !(vma->vm_flags & VM_PFNMAP)) {
953 up_read(¤t->mm->mmap_sem);
955 return page_to_pfn(bad_page);
958 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
959 up_read(¤t->mm->mmap_sem);
960 BUG_ON(!kvm_is_mmio_pfn(pfn));
962 pfn = page_to_pfn(page[0]);
967 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
971 addr = gfn_to_hva(kvm, gfn);
972 if (kvm_is_error_hva(addr)) {
974 return page_to_pfn(bad_page);
977 return hva_to_pfn(kvm, addr);
979 EXPORT_SYMBOL_GPL(gfn_to_pfn);
981 pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
982 struct kvm_memory_slot *slot, gfn_t gfn)
984 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
985 return hva_to_pfn(kvm, addr);
988 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
992 pfn = gfn_to_pfn(kvm, gfn);
993 if (!kvm_is_mmio_pfn(pfn))
994 return pfn_to_page(pfn);
996 WARN_ON(kvm_is_mmio_pfn(pfn));
1002 EXPORT_SYMBOL_GPL(gfn_to_page);
1004 void kvm_release_page_clean(struct page *page)
1006 kvm_release_pfn_clean(page_to_pfn(page));
1008 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1010 void kvm_release_pfn_clean(pfn_t pfn)
1012 if (!kvm_is_mmio_pfn(pfn))
1013 put_page(pfn_to_page(pfn));
1015 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1017 void kvm_release_page_dirty(struct page *page)
1019 kvm_release_pfn_dirty(page_to_pfn(page));
1021 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1023 void kvm_release_pfn_dirty(pfn_t pfn)
1025 kvm_set_pfn_dirty(pfn);
1026 kvm_release_pfn_clean(pfn);
1028 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1030 void kvm_set_page_dirty(struct page *page)
1032 kvm_set_pfn_dirty(page_to_pfn(page));
1034 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1036 void kvm_set_pfn_dirty(pfn_t pfn)
1038 if (!kvm_is_mmio_pfn(pfn)) {
1039 struct page *page = pfn_to_page(pfn);
1040 if (!PageReserved(page))
1044 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1046 void kvm_set_pfn_accessed(pfn_t pfn)
1048 if (!kvm_is_mmio_pfn(pfn))
1049 mark_page_accessed(pfn_to_page(pfn));
1051 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1053 void kvm_get_pfn(pfn_t pfn)
1055 if (!kvm_is_mmio_pfn(pfn))
1056 get_page(pfn_to_page(pfn));
1058 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1060 static int next_segment(unsigned long len, int offset)
1062 if (len > PAGE_SIZE - offset)
1063 return PAGE_SIZE - offset;
1068 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1074 addr = gfn_to_hva(kvm, gfn);
1075 if (kvm_is_error_hva(addr))
1077 r = copy_from_user(data, (void __user *)addr + offset, len);
1082 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1084 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1086 gfn_t gfn = gpa >> PAGE_SHIFT;
1088 int offset = offset_in_page(gpa);
1091 while ((seg = next_segment(len, offset)) != 0) {
1092 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1102 EXPORT_SYMBOL_GPL(kvm_read_guest);
1104 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1109 gfn_t gfn = gpa >> PAGE_SHIFT;
1110 int offset = offset_in_page(gpa);
1112 addr = gfn_to_hva(kvm, gfn);
1113 if (kvm_is_error_hva(addr))
1115 pagefault_disable();
1116 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1122 EXPORT_SYMBOL(kvm_read_guest_atomic);
1124 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1125 int offset, int len)
1130 addr = gfn_to_hva(kvm, gfn);
1131 if (kvm_is_error_hva(addr))
1133 r = copy_to_user((void __user *)addr + offset, data, len);
1136 mark_page_dirty(kvm, gfn);
1139 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1141 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1144 gfn_t gfn = gpa >> PAGE_SHIFT;
1146 int offset = offset_in_page(gpa);
1149 while ((seg = next_segment(len, offset)) != 0) {
1150 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1161 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1163 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1165 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1167 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1169 gfn_t gfn = gpa >> PAGE_SHIFT;
1171 int offset = offset_in_page(gpa);
1174 while ((seg = next_segment(len, offset)) != 0) {
1175 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1184 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1186 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1188 struct kvm_memory_slot *memslot;
1190 gfn = unalias_gfn(kvm, gfn);
1191 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1192 if (memslot && memslot->dirty_bitmap) {
1193 unsigned long rel_gfn = gfn - memslot->base_gfn;
1195 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
1200 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1202 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1207 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1209 if (kvm_arch_vcpu_runnable(vcpu)) {
1210 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1213 if (kvm_cpu_has_pending_timer(vcpu))
1215 if (signal_pending(current))
1221 finish_wait(&vcpu->wq, &wait);
1224 void kvm_resched(struct kvm_vcpu *vcpu)
1226 if (!need_resched())
1230 EXPORT_SYMBOL_GPL(kvm_resched);
1232 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1237 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1239 /* Sleep for 100 us, and hope lock-holder got scheduled */
1240 expires = ktime_add_ns(ktime_get(), 100000UL);
1241 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1243 finish_wait(&vcpu->wq, &wait);
1245 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1247 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1249 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1252 if (vmf->pgoff == 0)
1253 page = virt_to_page(vcpu->run);
1255 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1256 page = virt_to_page(vcpu->arch.pio_data);
1258 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1259 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1260 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1263 return VM_FAULT_SIGBUS;
1269 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1270 .fault = kvm_vcpu_fault,
1273 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1275 vma->vm_ops = &kvm_vcpu_vm_ops;
1279 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1281 struct kvm_vcpu *vcpu = filp->private_data;
1283 kvm_put_kvm(vcpu->kvm);
1287 static struct file_operations kvm_vcpu_fops = {
1288 .release = kvm_vcpu_release,
1289 .unlocked_ioctl = kvm_vcpu_ioctl,
1290 .compat_ioctl = kvm_vcpu_ioctl,
1291 .mmap = kvm_vcpu_mmap,
1295 * Allocates an inode for the vcpu.
1297 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1299 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
1303 * Creates some virtual cpus. Good luck creating more than one.
1305 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1308 struct kvm_vcpu *vcpu, *v;
1310 vcpu = kvm_arch_vcpu_create(kvm, id);
1312 return PTR_ERR(vcpu);
1314 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1316 r = kvm_arch_vcpu_setup(vcpu);
1320 mutex_lock(&kvm->lock);
1321 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1326 kvm_for_each_vcpu(r, v, kvm)
1327 if (v->vcpu_id == id) {
1332 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1334 /* Now it's all set up, let userspace reach it */
1336 r = create_vcpu_fd(vcpu);
1342 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1344 atomic_inc(&kvm->online_vcpus);
1346 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1347 if (kvm->bsp_vcpu_id == id)
1348 kvm->bsp_vcpu = vcpu;
1350 mutex_unlock(&kvm->lock);
1354 mutex_unlock(&kvm->lock);
1355 kvm_arch_vcpu_destroy(vcpu);
1359 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1362 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1363 vcpu->sigset_active = 1;
1364 vcpu->sigset = *sigset;
1366 vcpu->sigset_active = 0;
1370 static long kvm_vcpu_ioctl(struct file *filp,
1371 unsigned int ioctl, unsigned long arg)
1373 struct kvm_vcpu *vcpu = filp->private_data;
1374 void __user *argp = (void __user *)arg;
1376 struct kvm_fpu *fpu = NULL;
1377 struct kvm_sregs *kvm_sregs = NULL;
1379 if (vcpu->kvm->mm != current->mm)
1386 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1388 case KVM_GET_REGS: {
1389 struct kvm_regs *kvm_regs;
1392 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1395 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1399 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1406 case KVM_SET_REGS: {
1407 struct kvm_regs *kvm_regs;
1410 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1414 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1416 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1424 case KVM_GET_SREGS: {
1425 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1429 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1433 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1438 case KVM_SET_SREGS: {
1439 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1444 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1446 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1452 case KVM_GET_MP_STATE: {
1453 struct kvm_mp_state mp_state;
1455 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1459 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1464 case KVM_SET_MP_STATE: {
1465 struct kvm_mp_state mp_state;
1468 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1470 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1476 case KVM_TRANSLATE: {
1477 struct kvm_translation tr;
1480 if (copy_from_user(&tr, argp, sizeof tr))
1482 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1486 if (copy_to_user(argp, &tr, sizeof tr))
1491 case KVM_SET_GUEST_DEBUG: {
1492 struct kvm_guest_debug dbg;
1495 if (copy_from_user(&dbg, argp, sizeof dbg))
1497 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1503 case KVM_SET_SIGNAL_MASK: {
1504 struct kvm_signal_mask __user *sigmask_arg = argp;
1505 struct kvm_signal_mask kvm_sigmask;
1506 sigset_t sigset, *p;
1511 if (copy_from_user(&kvm_sigmask, argp,
1512 sizeof kvm_sigmask))
1515 if (kvm_sigmask.len != sizeof sigset)
1518 if (copy_from_user(&sigset, sigmask_arg->sigset,
1523 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1527 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1531 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1535 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1541 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1546 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1548 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1555 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1563 static long kvm_vm_ioctl(struct file *filp,
1564 unsigned int ioctl, unsigned long arg)
1566 struct kvm *kvm = filp->private_data;
1567 void __user *argp = (void __user *)arg;
1570 if (kvm->mm != current->mm)
1573 case KVM_CREATE_VCPU:
1574 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1578 case KVM_SET_USER_MEMORY_REGION: {
1579 struct kvm_userspace_memory_region kvm_userspace_mem;
1582 if (copy_from_user(&kvm_userspace_mem, argp,
1583 sizeof kvm_userspace_mem))
1586 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1591 case KVM_GET_DIRTY_LOG: {
1592 struct kvm_dirty_log log;
1595 if (copy_from_user(&log, argp, sizeof log))
1597 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1602 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1603 case KVM_REGISTER_COALESCED_MMIO: {
1604 struct kvm_coalesced_mmio_zone zone;
1606 if (copy_from_user(&zone, argp, sizeof zone))
1608 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1614 case KVM_UNREGISTER_COALESCED_MMIO: {
1615 struct kvm_coalesced_mmio_zone zone;
1617 if (copy_from_user(&zone, argp, sizeof zone))
1619 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1627 struct kvm_irqfd data;
1630 if (copy_from_user(&data, argp, sizeof data))
1632 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1635 case KVM_IOEVENTFD: {
1636 struct kvm_ioeventfd data;
1639 if (copy_from_user(&data, argp, sizeof data))
1641 r = kvm_ioeventfd(kvm, &data);
1644 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1645 case KVM_SET_BOOT_CPU_ID:
1647 mutex_lock(&kvm->lock);
1648 if (atomic_read(&kvm->online_vcpus) != 0)
1651 kvm->bsp_vcpu_id = arg;
1652 mutex_unlock(&kvm->lock);
1656 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1658 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
1664 #ifdef CONFIG_COMPAT
1665 struct compat_kvm_dirty_log {
1669 compat_uptr_t dirty_bitmap; /* one bit per page */
1674 static long kvm_vm_compat_ioctl(struct file *filp,
1675 unsigned int ioctl, unsigned long arg)
1677 struct kvm *kvm = filp->private_data;
1680 if (kvm->mm != current->mm)
1683 case KVM_GET_DIRTY_LOG: {
1684 struct compat_kvm_dirty_log compat_log;
1685 struct kvm_dirty_log log;
1688 if (copy_from_user(&compat_log, (void __user *)arg,
1689 sizeof(compat_log)))
1691 log.slot = compat_log.slot;
1692 log.padding1 = compat_log.padding1;
1693 log.padding2 = compat_log.padding2;
1694 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1696 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1702 r = kvm_vm_ioctl(filp, ioctl, arg);
1710 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1712 struct page *page[1];
1715 gfn_t gfn = vmf->pgoff;
1716 struct kvm *kvm = vma->vm_file->private_data;
1718 addr = gfn_to_hva(kvm, gfn);
1719 if (kvm_is_error_hva(addr))
1720 return VM_FAULT_SIGBUS;
1722 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1724 if (unlikely(npages != 1))
1725 return VM_FAULT_SIGBUS;
1727 vmf->page = page[0];
1731 static const struct vm_operations_struct kvm_vm_vm_ops = {
1732 .fault = kvm_vm_fault,
1735 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1737 vma->vm_ops = &kvm_vm_vm_ops;
1741 static struct file_operations kvm_vm_fops = {
1742 .release = kvm_vm_release,
1743 .unlocked_ioctl = kvm_vm_ioctl,
1744 #ifdef CONFIG_COMPAT
1745 .compat_ioctl = kvm_vm_compat_ioctl,
1747 .mmap = kvm_vm_mmap,
1750 static int kvm_dev_ioctl_create_vm(void)
1755 kvm = kvm_create_vm();
1757 return PTR_ERR(kvm);
1758 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1759 r = kvm_coalesced_mmio_init(kvm);
1765 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
1772 static long kvm_dev_ioctl_check_extension_generic(long arg)
1775 case KVM_CAP_USER_MEMORY:
1776 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
1777 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
1778 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1779 case KVM_CAP_SET_BOOT_CPU_ID:
1781 case KVM_CAP_INTERNAL_ERROR_DATA:
1783 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1784 case KVM_CAP_IRQ_ROUTING:
1785 return KVM_MAX_IRQ_ROUTES;
1790 return kvm_dev_ioctl_check_extension(arg);
1793 static long kvm_dev_ioctl(struct file *filp,
1794 unsigned int ioctl, unsigned long arg)
1799 case KVM_GET_API_VERSION:
1803 r = KVM_API_VERSION;
1809 r = kvm_dev_ioctl_create_vm();
1811 case KVM_CHECK_EXTENSION:
1812 r = kvm_dev_ioctl_check_extension_generic(arg);
1814 case KVM_GET_VCPU_MMAP_SIZE:
1818 r = PAGE_SIZE; /* struct kvm_run */
1820 r += PAGE_SIZE; /* pio data page */
1822 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1823 r += PAGE_SIZE; /* coalesced mmio ring page */
1826 case KVM_TRACE_ENABLE:
1827 case KVM_TRACE_PAUSE:
1828 case KVM_TRACE_DISABLE:
1832 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1838 static struct file_operations kvm_chardev_ops = {
1839 .unlocked_ioctl = kvm_dev_ioctl,
1840 .compat_ioctl = kvm_dev_ioctl,
1843 static struct miscdevice kvm_dev = {
1849 static void hardware_enable(void *junk)
1851 int cpu = raw_smp_processor_id();
1854 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
1857 cpumask_set_cpu(cpu, cpus_hardware_enabled);
1859 r = kvm_arch_hardware_enable(NULL);
1862 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1863 atomic_inc(&hardware_enable_failed);
1864 printk(KERN_INFO "kvm: enabling virtualization on "
1865 "CPU%d failed\n", cpu);
1869 static void hardware_disable(void *junk)
1871 int cpu = raw_smp_processor_id();
1873 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
1875 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1876 kvm_arch_hardware_disable(NULL);
1879 static void hardware_disable_all_nolock(void)
1881 BUG_ON(!kvm_usage_count);
1884 if (!kvm_usage_count)
1885 on_each_cpu(hardware_disable, NULL, 1);
1888 static void hardware_disable_all(void)
1890 spin_lock(&kvm_lock);
1891 hardware_disable_all_nolock();
1892 spin_unlock(&kvm_lock);
1895 static int hardware_enable_all(void)
1899 spin_lock(&kvm_lock);
1902 if (kvm_usage_count == 1) {
1903 atomic_set(&hardware_enable_failed, 0);
1904 on_each_cpu(hardware_enable, NULL, 1);
1906 if (atomic_read(&hardware_enable_failed)) {
1907 hardware_disable_all_nolock();
1912 spin_unlock(&kvm_lock);
1917 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1922 if (!kvm_usage_count)
1925 val &= ~CPU_TASKS_FROZEN;
1928 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1930 hardware_disable(NULL);
1933 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1935 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1942 asmlinkage void kvm_handle_fault_on_reboot(void)
1945 /* spin while reset goes on */
1948 /* Fault while not rebooting. We want the trace. */
1951 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1953 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1957 * Some (well, at least mine) BIOSes hang on reboot if
1960 * And Intel TXT required VMX off for all cpu when system shutdown.
1962 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1963 kvm_rebooting = true;
1964 on_each_cpu(hardware_disable, NULL, 1);
1968 static struct notifier_block kvm_reboot_notifier = {
1969 .notifier_call = kvm_reboot,
1973 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1977 for (i = 0; i < bus->dev_count; i++) {
1978 struct kvm_io_device *pos = bus->devs[i];
1980 kvm_iodevice_destructor(pos);
1985 /* kvm_io_bus_write - called under kvm->slots_lock */
1986 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
1987 int len, const void *val)
1990 struct kvm_io_bus *bus;
1992 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
1993 for (i = 0; i < bus->dev_count; i++)
1994 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1999 /* kvm_io_bus_read - called under kvm->slots_lock */
2000 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2004 struct kvm_io_bus *bus;
2006 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2007 for (i = 0; i < bus->dev_count; i++)
2008 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2013 /* Caller must hold slots_lock. */
2014 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2015 struct kvm_io_device *dev)
2017 struct kvm_io_bus *new_bus, *bus;
2019 bus = kvm->buses[bus_idx];
2020 if (bus->dev_count > NR_IOBUS_DEVS-1)
2023 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2026 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2027 new_bus->devs[new_bus->dev_count++] = dev;
2028 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2029 synchronize_srcu_expedited(&kvm->srcu);
2035 /* Caller must hold slots_lock. */
2036 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2037 struct kvm_io_device *dev)
2040 struct kvm_io_bus *new_bus, *bus;
2042 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2046 bus = kvm->buses[bus_idx];
2047 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2050 for (i = 0; i < new_bus->dev_count; i++)
2051 if (new_bus->devs[i] == dev) {
2053 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
2062 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2063 synchronize_srcu_expedited(&kvm->srcu);
2068 static struct notifier_block kvm_cpu_notifier = {
2069 .notifier_call = kvm_cpu_hotplug,
2070 .priority = 20, /* must be > scheduler priority */
2073 static int vm_stat_get(void *_offset, u64 *val)
2075 unsigned offset = (long)_offset;
2079 spin_lock(&kvm_lock);
2080 list_for_each_entry(kvm, &vm_list, vm_list)
2081 *val += *(u32 *)((void *)kvm + offset);
2082 spin_unlock(&kvm_lock);
2086 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2088 static int vcpu_stat_get(void *_offset, u64 *val)
2090 unsigned offset = (long)_offset;
2092 struct kvm_vcpu *vcpu;
2096 spin_lock(&kvm_lock);
2097 list_for_each_entry(kvm, &vm_list, vm_list)
2098 kvm_for_each_vcpu(i, vcpu, kvm)
2099 *val += *(u32 *)((void *)vcpu + offset);
2101 spin_unlock(&kvm_lock);
2105 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2107 static const struct file_operations *stat_fops[] = {
2108 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2109 [KVM_STAT_VM] = &vm_stat_fops,
2112 static void kvm_init_debug(void)
2114 struct kvm_stats_debugfs_item *p;
2116 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2117 for (p = debugfs_entries; p->name; ++p)
2118 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2119 (void *)(long)p->offset,
2120 stat_fops[p->kind]);
2123 static void kvm_exit_debug(void)
2125 struct kvm_stats_debugfs_item *p;
2127 for (p = debugfs_entries; p->name; ++p)
2128 debugfs_remove(p->dentry);
2129 debugfs_remove(kvm_debugfs_dir);
2132 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2134 if (kvm_usage_count)
2135 hardware_disable(NULL);
2139 static int kvm_resume(struct sys_device *dev)
2141 if (kvm_usage_count)
2142 hardware_enable(NULL);
2146 static struct sysdev_class kvm_sysdev_class = {
2148 .suspend = kvm_suspend,
2149 .resume = kvm_resume,
2152 static struct sys_device kvm_sysdev = {
2154 .cls = &kvm_sysdev_class,
2157 struct page *bad_page;
2161 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2163 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2166 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2168 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2170 kvm_arch_vcpu_load(vcpu, cpu);
2173 static void kvm_sched_out(struct preempt_notifier *pn,
2174 struct task_struct *next)
2176 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2178 kvm_arch_vcpu_put(vcpu);
2181 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
2182 struct module *module)
2187 r = kvm_arch_init(opaque);
2191 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2193 if (bad_page == NULL) {
2198 bad_pfn = page_to_pfn(bad_page);
2200 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2205 r = kvm_arch_hardware_setup();
2209 for_each_online_cpu(cpu) {
2210 smp_call_function_single(cpu,
2211 kvm_arch_check_processor_compat,
2217 r = register_cpu_notifier(&kvm_cpu_notifier);
2220 register_reboot_notifier(&kvm_reboot_notifier);
2222 r = sysdev_class_register(&kvm_sysdev_class);
2226 r = sysdev_register(&kvm_sysdev);
2230 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2232 vcpu_align = __alignof__(struct kvm_vcpu);
2233 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
2235 if (!kvm_vcpu_cache) {
2240 kvm_chardev_ops.owner = module;
2241 kvm_vm_fops.owner = module;
2242 kvm_vcpu_fops.owner = module;
2244 r = misc_register(&kvm_dev);
2246 printk(KERN_ERR "kvm: misc device register failed\n");
2250 kvm_preempt_ops.sched_in = kvm_sched_in;
2251 kvm_preempt_ops.sched_out = kvm_sched_out;
2258 kmem_cache_destroy(kvm_vcpu_cache);
2260 sysdev_unregister(&kvm_sysdev);
2262 sysdev_class_unregister(&kvm_sysdev_class);
2264 unregister_reboot_notifier(&kvm_reboot_notifier);
2265 unregister_cpu_notifier(&kvm_cpu_notifier);
2268 kvm_arch_hardware_unsetup();
2270 free_cpumask_var(cpus_hardware_enabled);
2272 __free_page(bad_page);
2278 EXPORT_SYMBOL_GPL(kvm_init);
2283 misc_deregister(&kvm_dev);
2284 kmem_cache_destroy(kvm_vcpu_cache);
2285 sysdev_unregister(&kvm_sysdev);
2286 sysdev_class_unregister(&kvm_sysdev_class);
2287 unregister_reboot_notifier(&kvm_reboot_notifier);
2288 unregister_cpu_notifier(&kvm_cpu_notifier);
2289 on_each_cpu(hardware_disable, NULL, 1);
2290 kvm_arch_hardware_unsetup();
2292 free_cpumask_var(cpus_hardware_enabled);
2293 __free_page(bad_page);
2295 EXPORT_SYMBOL_GPL(kvm_exit);