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>
25 #include <linux/gfp.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>
45 #include <asm/processor.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
50 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51 #include "coalesced_mmio.h"
54 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
55 #include <linux/pci.h>
56 #include <linux/interrupt.h>
60 MODULE_AUTHOR("Qumranet");
61 MODULE_LICENSE("GPL");
63 DEFINE_SPINLOCK(kvm_lock);
66 static cpumask_t cpus_hardware_enabled;
68 struct kmem_cache *kvm_vcpu_cache;
69 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
71 static __read_mostly struct preempt_ops kvm_preempt_ops;
73 struct dentry *kvm_debugfs_dir;
75 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
80 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
81 static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
84 struct list_head *ptr;
85 struct kvm_assigned_dev_kernel *match;
87 list_for_each(ptr, head) {
88 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
89 if (match->assigned_dev_id == assigned_dev_id)
95 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
97 struct kvm_assigned_dev_kernel *assigned_dev;
99 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
102 /* This is taken to safely inject irq inside the guest. When
103 * the interrupt injection (or the ioapic code) uses a
104 * finer-grained lock, update this
106 mutex_lock(&assigned_dev->kvm->lock);
107 kvm_set_irq(assigned_dev->kvm,
108 assigned_dev->irq_source_id,
109 assigned_dev->guest_irq, 1);
110 mutex_unlock(&assigned_dev->kvm->lock);
111 kvm_put_kvm(assigned_dev->kvm);
114 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
116 struct kvm_assigned_dev_kernel *assigned_dev =
117 (struct kvm_assigned_dev_kernel *) dev_id;
119 kvm_get_kvm(assigned_dev->kvm);
120 schedule_work(&assigned_dev->interrupt_work);
121 disable_irq_nosync(irq);
125 /* Ack the irq line for an assigned device */
126 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
128 struct kvm_assigned_dev_kernel *dev;
133 dev = container_of(kian, struct kvm_assigned_dev_kernel,
135 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
136 enable_irq(dev->host_irq);
139 static void kvm_free_assigned_device(struct kvm *kvm,
140 struct kvm_assigned_dev_kernel
143 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
144 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
146 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
147 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
149 if (cancel_work_sync(&assigned_dev->interrupt_work))
150 /* We had pending work. That means we will have to take
151 * care of kvm_put_kvm.
155 pci_reset_function(assigned_dev->dev);
157 pci_release_regions(assigned_dev->dev);
158 pci_disable_device(assigned_dev->dev);
159 pci_dev_put(assigned_dev->dev);
161 list_del(&assigned_dev->list);
165 void kvm_free_all_assigned_devices(struct kvm *kvm)
167 struct list_head *ptr, *ptr2;
168 struct kvm_assigned_dev_kernel *assigned_dev;
170 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
171 assigned_dev = list_entry(ptr,
172 struct kvm_assigned_dev_kernel,
175 kvm_free_assigned_device(kvm, assigned_dev);
179 static int assigned_device_update_intx(struct kvm *kvm,
180 struct kvm_assigned_dev_kernel *adev,
181 struct kvm_assigned_irq *airq)
183 if (adev->irq_requested) {
184 adev->guest_irq = airq->guest_irq;
185 adev->ack_notifier.gsi = airq->guest_irq;
189 if (irqchip_in_kernel(kvm)) {
190 if (!capable(CAP_SYS_RAWIO))
194 adev->host_irq = airq->host_irq;
196 adev->host_irq = adev->dev->irq;
197 adev->guest_irq = airq->guest_irq;
198 adev->ack_notifier.gsi = airq->guest_irq;
200 /* Even though this is PCI, we don't want to use shared
201 * interrupts. Sharing host devices with guest-assigned devices
202 * on the same interrupt line is not a happy situation: there
203 * are going to be long delays in accepting, acking, etc.
205 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
206 0, "kvm_assigned_intx_device", (void *)adev))
210 adev->irq_requested = true;
214 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
215 struct kvm_assigned_irq
219 struct kvm_assigned_dev_kernel *match;
221 mutex_lock(&kvm->lock);
223 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
224 assigned_irq->assigned_dev_id);
226 mutex_unlock(&kvm->lock);
230 if (!match->irq_requested) {
231 INIT_WORK(&match->interrupt_work,
232 kvm_assigned_dev_interrupt_work_handler);
233 if (irqchip_in_kernel(kvm)) {
234 /* Register ack nofitier */
235 match->ack_notifier.gsi = -1;
236 match->ack_notifier.irq_acked =
237 kvm_assigned_dev_ack_irq;
238 kvm_register_irq_ack_notifier(kvm,
239 &match->ack_notifier);
241 /* Request IRQ source ID */
242 r = kvm_request_irq_source_id(kvm);
246 match->irq_source_id = r;
250 r = assigned_device_update_intx(kvm, match, assigned_irq);
254 mutex_unlock(&kvm->lock);
257 mutex_unlock(&kvm->lock);
258 kvm_free_assigned_device(kvm, match);
262 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
263 struct kvm_assigned_pci_dev *assigned_dev)
266 struct kvm_assigned_dev_kernel *match;
269 mutex_lock(&kvm->lock);
271 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
272 assigned_dev->assigned_dev_id);
274 /* device already assigned */
279 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
281 printk(KERN_INFO "%s: Couldn't allocate memory\n",
286 dev = pci_get_bus_and_slot(assigned_dev->busnr,
287 assigned_dev->devfn);
289 printk(KERN_INFO "%s: host device not found\n", __func__);
293 if (pci_enable_device(dev)) {
294 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
298 r = pci_request_regions(dev, "kvm_assigned_device");
300 printk(KERN_INFO "%s: Could not get access to device regions\n",
305 pci_reset_function(dev);
307 match->assigned_dev_id = assigned_dev->assigned_dev_id;
308 match->host_busnr = assigned_dev->busnr;
309 match->host_devfn = assigned_dev->devfn;
314 list_add(&match->list, &kvm->arch.assigned_dev_head);
316 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
317 r = kvm_iommu_map_guest(kvm, match);
323 mutex_unlock(&kvm->lock);
326 list_del(&match->list);
327 pci_release_regions(dev);
329 pci_disable_device(dev);
334 mutex_unlock(&kvm->lock);
339 static inline int valid_vcpu(int n)
341 return likely(n >= 0 && n < KVM_MAX_VCPUS);
344 inline int kvm_is_mmio_pfn(pfn_t pfn)
347 return PageReserved(pfn_to_page(pfn));
353 * Switches to specified vcpu, until a matching vcpu_put()
355 void vcpu_load(struct kvm_vcpu *vcpu)
359 mutex_lock(&vcpu->mutex);
361 preempt_notifier_register(&vcpu->preempt_notifier);
362 kvm_arch_vcpu_load(vcpu, cpu);
366 void vcpu_put(struct kvm_vcpu *vcpu)
369 kvm_arch_vcpu_put(vcpu);
370 preempt_notifier_unregister(&vcpu->preempt_notifier);
372 mutex_unlock(&vcpu->mutex);
375 static void ack_flush(void *_completed)
379 void kvm_flush_remote_tlbs(struct kvm *kvm)
383 struct kvm_vcpu *vcpu;
387 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
388 vcpu = kvm->vcpus[i];
391 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
394 if (cpu != -1 && cpu != me)
397 if (cpus_empty(cpus))
399 ++kvm->stat.remote_tlb_flush;
400 smp_call_function_mask(cpus, ack_flush, NULL, 1);
405 void kvm_reload_remote_mmus(struct kvm *kvm)
409 struct kvm_vcpu *vcpu;
413 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
414 vcpu = kvm->vcpus[i];
417 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
420 if (cpu != -1 && cpu != me)
423 if (cpus_empty(cpus))
425 smp_call_function_mask(cpus, ack_flush, NULL, 1);
431 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
436 mutex_init(&vcpu->mutex);
440 init_waitqueue_head(&vcpu->wq);
442 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
447 vcpu->run = page_address(page);
449 r = kvm_arch_vcpu_init(vcpu);
455 free_page((unsigned long)vcpu->run);
459 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
461 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
463 kvm_arch_vcpu_uninit(vcpu);
464 free_page((unsigned long)vcpu->run);
466 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
468 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
469 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
471 return container_of(mn, struct kvm, mmu_notifier);
474 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
475 struct mm_struct *mm,
476 unsigned long address)
478 struct kvm *kvm = mmu_notifier_to_kvm(mn);
482 * When ->invalidate_page runs, the linux pte has been zapped
483 * already but the page is still allocated until
484 * ->invalidate_page returns. So if we increase the sequence
485 * here the kvm page fault will notice if the spte can't be
486 * established because the page is going to be freed. If
487 * instead the kvm page fault establishes the spte before
488 * ->invalidate_page runs, kvm_unmap_hva will release it
491 * The sequence increase only need to be seen at spin_unlock
492 * time, and not at spin_lock time.
494 * Increasing the sequence after the spin_unlock would be
495 * unsafe because the kvm page fault could then establish the
496 * pte after kvm_unmap_hva returned, without noticing the page
497 * is going to be freed.
499 spin_lock(&kvm->mmu_lock);
500 kvm->mmu_notifier_seq++;
501 need_tlb_flush = kvm_unmap_hva(kvm, address);
502 spin_unlock(&kvm->mmu_lock);
504 /* we've to flush the tlb before the pages can be freed */
506 kvm_flush_remote_tlbs(kvm);
510 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
511 struct mm_struct *mm,
515 struct kvm *kvm = mmu_notifier_to_kvm(mn);
516 int need_tlb_flush = 0;
518 spin_lock(&kvm->mmu_lock);
520 * The count increase must become visible at unlock time as no
521 * spte can be established without taking the mmu_lock and
522 * count is also read inside the mmu_lock critical section.
524 kvm->mmu_notifier_count++;
525 for (; start < end; start += PAGE_SIZE)
526 need_tlb_flush |= kvm_unmap_hva(kvm, start);
527 spin_unlock(&kvm->mmu_lock);
529 /* we've to flush the tlb before the pages can be freed */
531 kvm_flush_remote_tlbs(kvm);
534 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
535 struct mm_struct *mm,
539 struct kvm *kvm = mmu_notifier_to_kvm(mn);
541 spin_lock(&kvm->mmu_lock);
543 * This sequence increase will notify the kvm page fault that
544 * the page that is going to be mapped in the spte could have
547 kvm->mmu_notifier_seq++;
549 * The above sequence increase must be visible before the
550 * below count decrease but both values are read by the kvm
551 * page fault under mmu_lock spinlock so we don't need to add
552 * a smb_wmb() here in between the two.
554 kvm->mmu_notifier_count--;
555 spin_unlock(&kvm->mmu_lock);
557 BUG_ON(kvm->mmu_notifier_count < 0);
560 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
561 struct mm_struct *mm,
562 unsigned long address)
564 struct kvm *kvm = mmu_notifier_to_kvm(mn);
567 spin_lock(&kvm->mmu_lock);
568 young = kvm_age_hva(kvm, address);
569 spin_unlock(&kvm->mmu_lock);
572 kvm_flush_remote_tlbs(kvm);
577 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
578 .invalidate_page = kvm_mmu_notifier_invalidate_page,
579 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
580 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
581 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
583 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
585 static struct kvm *kvm_create_vm(void)
587 struct kvm *kvm = kvm_arch_create_vm();
588 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
595 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
596 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
599 return ERR_PTR(-ENOMEM);
601 kvm->coalesced_mmio_ring =
602 (struct kvm_coalesced_mmio_ring *)page_address(page);
605 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
608 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
609 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
611 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
620 kvm->mm = current->mm;
621 atomic_inc(&kvm->mm->mm_count);
622 spin_lock_init(&kvm->mmu_lock);
623 kvm_io_bus_init(&kvm->pio_bus);
624 mutex_init(&kvm->lock);
625 kvm_io_bus_init(&kvm->mmio_bus);
626 init_rwsem(&kvm->slots_lock);
627 atomic_set(&kvm->users_count, 1);
628 spin_lock(&kvm_lock);
629 list_add(&kvm->vm_list, &vm_list);
630 spin_unlock(&kvm_lock);
631 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
632 kvm_coalesced_mmio_init(kvm);
639 * Free any memory in @free but not in @dont.
641 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
642 struct kvm_memory_slot *dont)
644 if (!dont || free->rmap != dont->rmap)
647 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
648 vfree(free->dirty_bitmap);
650 if (!dont || free->lpage_info != dont->lpage_info)
651 vfree(free->lpage_info);
654 free->dirty_bitmap = NULL;
656 free->lpage_info = NULL;
659 void kvm_free_physmem(struct kvm *kvm)
663 for (i = 0; i < kvm->nmemslots; ++i)
664 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
667 static void kvm_destroy_vm(struct kvm *kvm)
669 struct mm_struct *mm = kvm->mm;
671 spin_lock(&kvm_lock);
672 list_del(&kvm->vm_list);
673 spin_unlock(&kvm_lock);
674 kvm_io_bus_destroy(&kvm->pio_bus);
675 kvm_io_bus_destroy(&kvm->mmio_bus);
676 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
677 if (kvm->coalesced_mmio_ring != NULL)
678 free_page((unsigned long)kvm->coalesced_mmio_ring);
680 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
681 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
683 kvm_arch_destroy_vm(kvm);
687 void kvm_get_kvm(struct kvm *kvm)
689 atomic_inc(&kvm->users_count);
691 EXPORT_SYMBOL_GPL(kvm_get_kvm);
693 void kvm_put_kvm(struct kvm *kvm)
695 if (atomic_dec_and_test(&kvm->users_count))
698 EXPORT_SYMBOL_GPL(kvm_put_kvm);
701 static int kvm_vm_release(struct inode *inode, struct file *filp)
703 struct kvm *kvm = filp->private_data;
710 * Allocate some memory and give it an address in the guest physical address
713 * Discontiguous memory is allowed, mostly for framebuffers.
715 * Must be called holding mmap_sem for write.
717 int __kvm_set_memory_region(struct kvm *kvm,
718 struct kvm_userspace_memory_region *mem,
723 unsigned long npages;
725 struct kvm_memory_slot *memslot;
726 struct kvm_memory_slot old, new;
729 /* General sanity checks */
730 if (mem->memory_size & (PAGE_SIZE - 1))
732 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
734 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
736 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
738 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
741 memslot = &kvm->memslots[mem->slot];
742 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
743 npages = mem->memory_size >> PAGE_SHIFT;
746 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
748 new = old = *memslot;
750 new.base_gfn = base_gfn;
752 new.flags = mem->flags;
754 /* Disallow changing a memory slot's size. */
756 if (npages && old.npages && npages != old.npages)
759 /* Check for overlaps */
761 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
762 struct kvm_memory_slot *s = &kvm->memslots[i];
766 if (!((base_gfn + npages <= s->base_gfn) ||
767 (base_gfn >= s->base_gfn + s->npages)))
771 /* Free page dirty bitmap if unneeded */
772 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
773 new.dirty_bitmap = NULL;
777 /* Allocate if a slot is being created */
779 if (npages && !new.rmap) {
780 new.rmap = vmalloc(npages * sizeof(struct page *));
785 memset(new.rmap, 0, npages * sizeof(*new.rmap));
787 new.user_alloc = user_alloc;
789 * hva_to_rmmap() serialzies with the mmu_lock and to be
790 * safe it has to ignore memslots with !user_alloc &&
794 new.userspace_addr = mem->userspace_addr;
796 new.userspace_addr = 0;
798 if (npages && !new.lpage_info) {
799 int largepages = npages / KVM_PAGES_PER_HPAGE;
800 if (npages % KVM_PAGES_PER_HPAGE)
802 if (base_gfn % KVM_PAGES_PER_HPAGE)
805 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
810 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
812 if (base_gfn % KVM_PAGES_PER_HPAGE)
813 new.lpage_info[0].write_count = 1;
814 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
815 new.lpage_info[largepages-1].write_count = 1;
818 /* Allocate page dirty bitmap if needed */
819 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
820 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
822 new.dirty_bitmap = vmalloc(dirty_bytes);
823 if (!new.dirty_bitmap)
825 memset(new.dirty_bitmap, 0, dirty_bytes);
827 #endif /* not defined CONFIG_S390 */
830 kvm_arch_flush_shadow(kvm);
832 spin_lock(&kvm->mmu_lock);
833 if (mem->slot >= kvm->nmemslots)
834 kvm->nmemslots = mem->slot + 1;
837 spin_unlock(&kvm->mmu_lock);
839 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
841 spin_lock(&kvm->mmu_lock);
843 spin_unlock(&kvm->mmu_lock);
847 kvm_free_physmem_slot(&old, &new);
849 /* map the pages in iommu page table */
850 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
857 kvm_free_physmem_slot(&new, &old);
862 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
864 int kvm_set_memory_region(struct kvm *kvm,
865 struct kvm_userspace_memory_region *mem,
870 down_write(&kvm->slots_lock);
871 r = __kvm_set_memory_region(kvm, mem, user_alloc);
872 up_write(&kvm->slots_lock);
875 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
877 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
879 kvm_userspace_memory_region *mem,
882 if (mem->slot >= KVM_MEMORY_SLOTS)
884 return kvm_set_memory_region(kvm, mem, user_alloc);
887 int kvm_get_dirty_log(struct kvm *kvm,
888 struct kvm_dirty_log *log, int *is_dirty)
890 struct kvm_memory_slot *memslot;
893 unsigned long any = 0;
896 if (log->slot >= KVM_MEMORY_SLOTS)
899 memslot = &kvm->memslots[log->slot];
901 if (!memslot->dirty_bitmap)
904 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
906 for (i = 0; !any && i < n/sizeof(long); ++i)
907 any = memslot->dirty_bitmap[i];
910 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
921 int is_error_page(struct page *page)
923 return page == bad_page;
925 EXPORT_SYMBOL_GPL(is_error_page);
927 int is_error_pfn(pfn_t pfn)
929 return pfn == bad_pfn;
931 EXPORT_SYMBOL_GPL(is_error_pfn);
933 static inline unsigned long bad_hva(void)
938 int kvm_is_error_hva(unsigned long addr)
940 return addr == bad_hva();
942 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
944 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
948 for (i = 0; i < kvm->nmemslots; ++i) {
949 struct kvm_memory_slot *memslot = &kvm->memslots[i];
951 if (gfn >= memslot->base_gfn
952 && gfn < memslot->base_gfn + memslot->npages)
957 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
959 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
961 gfn = unalias_gfn(kvm, gfn);
962 return gfn_to_memslot_unaliased(kvm, gfn);
965 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
969 gfn = unalias_gfn(kvm, gfn);
970 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
971 struct kvm_memory_slot *memslot = &kvm->memslots[i];
973 if (gfn >= memslot->base_gfn
974 && gfn < memslot->base_gfn + memslot->npages)
979 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
981 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
983 struct kvm_memory_slot *slot;
985 gfn = unalias_gfn(kvm, gfn);
986 slot = gfn_to_memslot_unaliased(kvm, gfn);
989 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
991 EXPORT_SYMBOL_GPL(gfn_to_hva);
993 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
995 struct page *page[1];
1002 addr = gfn_to_hva(kvm, gfn);
1003 if (kvm_is_error_hva(addr)) {
1005 return page_to_pfn(bad_page);
1008 npages = get_user_pages_fast(addr, 1, 1, page);
1010 if (unlikely(npages != 1)) {
1011 struct vm_area_struct *vma;
1013 down_read(¤t->mm->mmap_sem);
1014 vma = find_vma(current->mm, addr);
1016 if (vma == NULL || addr < vma->vm_start ||
1017 !(vma->vm_flags & VM_PFNMAP)) {
1018 up_read(¤t->mm->mmap_sem);
1020 return page_to_pfn(bad_page);
1023 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1024 up_read(¤t->mm->mmap_sem);
1025 BUG_ON(!kvm_is_mmio_pfn(pfn));
1027 pfn = page_to_pfn(page[0]);
1032 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1034 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1038 pfn = gfn_to_pfn(kvm, gfn);
1039 if (!kvm_is_mmio_pfn(pfn))
1040 return pfn_to_page(pfn);
1042 WARN_ON(kvm_is_mmio_pfn(pfn));
1048 EXPORT_SYMBOL_GPL(gfn_to_page);
1050 void kvm_release_page_clean(struct page *page)
1052 kvm_release_pfn_clean(page_to_pfn(page));
1054 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1056 void kvm_release_pfn_clean(pfn_t pfn)
1058 if (!kvm_is_mmio_pfn(pfn))
1059 put_page(pfn_to_page(pfn));
1061 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1063 void kvm_release_page_dirty(struct page *page)
1065 kvm_release_pfn_dirty(page_to_pfn(page));
1067 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1069 void kvm_release_pfn_dirty(pfn_t pfn)
1071 kvm_set_pfn_dirty(pfn);
1072 kvm_release_pfn_clean(pfn);
1074 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1076 void kvm_set_page_dirty(struct page *page)
1078 kvm_set_pfn_dirty(page_to_pfn(page));
1080 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1082 void kvm_set_pfn_dirty(pfn_t pfn)
1084 if (!kvm_is_mmio_pfn(pfn)) {
1085 struct page *page = pfn_to_page(pfn);
1086 if (!PageReserved(page))
1090 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1092 void kvm_set_pfn_accessed(pfn_t pfn)
1094 if (!kvm_is_mmio_pfn(pfn))
1095 mark_page_accessed(pfn_to_page(pfn));
1097 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1099 void kvm_get_pfn(pfn_t pfn)
1101 if (!kvm_is_mmio_pfn(pfn))
1102 get_page(pfn_to_page(pfn));
1104 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1106 static int next_segment(unsigned long len, int offset)
1108 if (len > PAGE_SIZE - offset)
1109 return PAGE_SIZE - offset;
1114 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1120 addr = gfn_to_hva(kvm, gfn);
1121 if (kvm_is_error_hva(addr))
1123 r = copy_from_user(data, (void __user *)addr + offset, len);
1128 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1130 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1132 gfn_t gfn = gpa >> PAGE_SHIFT;
1134 int offset = offset_in_page(gpa);
1137 while ((seg = next_segment(len, offset)) != 0) {
1138 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1148 EXPORT_SYMBOL_GPL(kvm_read_guest);
1150 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1155 gfn_t gfn = gpa >> PAGE_SHIFT;
1156 int offset = offset_in_page(gpa);
1158 addr = gfn_to_hva(kvm, gfn);
1159 if (kvm_is_error_hva(addr))
1161 pagefault_disable();
1162 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1168 EXPORT_SYMBOL(kvm_read_guest_atomic);
1170 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1171 int offset, int len)
1176 addr = gfn_to_hva(kvm, gfn);
1177 if (kvm_is_error_hva(addr))
1179 r = copy_to_user((void __user *)addr + offset, data, len);
1182 mark_page_dirty(kvm, gfn);
1185 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1187 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1190 gfn_t gfn = gpa >> PAGE_SHIFT;
1192 int offset = offset_in_page(gpa);
1195 while ((seg = next_segment(len, offset)) != 0) {
1196 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1207 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1209 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1211 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1213 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1215 gfn_t gfn = gpa >> PAGE_SHIFT;
1217 int offset = offset_in_page(gpa);
1220 while ((seg = next_segment(len, offset)) != 0) {
1221 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1230 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1232 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1234 struct kvm_memory_slot *memslot;
1236 gfn = unalias_gfn(kvm, gfn);
1237 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1238 if (memslot && memslot->dirty_bitmap) {
1239 unsigned long rel_gfn = gfn - memslot->base_gfn;
1242 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1243 set_bit(rel_gfn, memslot->dirty_bitmap);
1248 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1250 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1255 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1257 if (kvm_cpu_has_interrupt(vcpu) ||
1258 kvm_cpu_has_pending_timer(vcpu) ||
1259 kvm_arch_vcpu_runnable(vcpu)) {
1260 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1263 if (signal_pending(current))
1271 finish_wait(&vcpu->wq, &wait);
1274 void kvm_resched(struct kvm_vcpu *vcpu)
1276 if (!need_resched())
1280 EXPORT_SYMBOL_GPL(kvm_resched);
1282 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1284 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1287 if (vmf->pgoff == 0)
1288 page = virt_to_page(vcpu->run);
1290 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1291 page = virt_to_page(vcpu->arch.pio_data);
1293 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1294 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1295 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1298 return VM_FAULT_SIGBUS;
1304 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1305 .fault = kvm_vcpu_fault,
1308 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1310 vma->vm_ops = &kvm_vcpu_vm_ops;
1314 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1316 struct kvm_vcpu *vcpu = filp->private_data;
1318 kvm_put_kvm(vcpu->kvm);
1322 static const struct file_operations kvm_vcpu_fops = {
1323 .release = kvm_vcpu_release,
1324 .unlocked_ioctl = kvm_vcpu_ioctl,
1325 .compat_ioctl = kvm_vcpu_ioctl,
1326 .mmap = kvm_vcpu_mmap,
1330 * Allocates an inode for the vcpu.
1332 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1334 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1336 kvm_put_kvm(vcpu->kvm);
1341 * Creates some virtual cpus. Good luck creating more than one.
1343 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1346 struct kvm_vcpu *vcpu;
1351 vcpu = kvm_arch_vcpu_create(kvm, n);
1353 return PTR_ERR(vcpu);
1355 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1357 r = kvm_arch_vcpu_setup(vcpu);
1361 mutex_lock(&kvm->lock);
1362 if (kvm->vcpus[n]) {
1366 kvm->vcpus[n] = vcpu;
1367 mutex_unlock(&kvm->lock);
1369 /* Now it's all set up, let userspace reach it */
1371 r = create_vcpu_fd(vcpu);
1377 mutex_lock(&kvm->lock);
1378 kvm->vcpus[n] = NULL;
1380 mutex_unlock(&kvm->lock);
1381 kvm_arch_vcpu_destroy(vcpu);
1385 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1388 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1389 vcpu->sigset_active = 1;
1390 vcpu->sigset = *sigset;
1392 vcpu->sigset_active = 0;
1396 static long kvm_vcpu_ioctl(struct file *filp,
1397 unsigned int ioctl, unsigned long arg)
1399 struct kvm_vcpu *vcpu = filp->private_data;
1400 void __user *argp = (void __user *)arg;
1402 struct kvm_fpu *fpu = NULL;
1403 struct kvm_sregs *kvm_sregs = NULL;
1405 if (vcpu->kvm->mm != current->mm)
1412 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1414 case KVM_GET_REGS: {
1415 struct kvm_regs *kvm_regs;
1418 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1421 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1425 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1432 case KVM_SET_REGS: {
1433 struct kvm_regs *kvm_regs;
1436 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1440 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1442 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1450 case KVM_GET_SREGS: {
1451 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1455 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1459 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1464 case KVM_SET_SREGS: {
1465 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1470 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1472 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1478 case KVM_GET_MP_STATE: {
1479 struct kvm_mp_state mp_state;
1481 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1485 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1490 case KVM_SET_MP_STATE: {
1491 struct kvm_mp_state mp_state;
1494 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1496 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1502 case KVM_TRANSLATE: {
1503 struct kvm_translation tr;
1506 if (copy_from_user(&tr, argp, sizeof tr))
1508 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1512 if (copy_to_user(argp, &tr, sizeof tr))
1517 case KVM_DEBUG_GUEST: {
1518 struct kvm_debug_guest dbg;
1521 if (copy_from_user(&dbg, argp, sizeof dbg))
1523 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1529 case KVM_SET_SIGNAL_MASK: {
1530 struct kvm_signal_mask __user *sigmask_arg = argp;
1531 struct kvm_signal_mask kvm_sigmask;
1532 sigset_t sigset, *p;
1537 if (copy_from_user(&kvm_sigmask, argp,
1538 sizeof kvm_sigmask))
1541 if (kvm_sigmask.len != sizeof sigset)
1544 if (copy_from_user(&sigset, sigmask_arg->sigset,
1549 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1553 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1557 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1561 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1567 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1572 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1574 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1581 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1589 static long kvm_vm_ioctl(struct file *filp,
1590 unsigned int ioctl, unsigned long arg)
1592 struct kvm *kvm = filp->private_data;
1593 void __user *argp = (void __user *)arg;
1596 if (kvm->mm != current->mm)
1599 case KVM_CREATE_VCPU:
1600 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1604 case KVM_SET_USER_MEMORY_REGION: {
1605 struct kvm_userspace_memory_region kvm_userspace_mem;
1608 if (copy_from_user(&kvm_userspace_mem, argp,
1609 sizeof kvm_userspace_mem))
1612 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1617 case KVM_GET_DIRTY_LOG: {
1618 struct kvm_dirty_log log;
1621 if (copy_from_user(&log, argp, sizeof log))
1623 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1628 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1629 case KVM_REGISTER_COALESCED_MMIO: {
1630 struct kvm_coalesced_mmio_zone zone;
1632 if (copy_from_user(&zone, argp, sizeof zone))
1635 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1641 case KVM_UNREGISTER_COALESCED_MMIO: {
1642 struct kvm_coalesced_mmio_zone zone;
1644 if (copy_from_user(&zone, argp, sizeof zone))
1647 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1654 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1655 case KVM_ASSIGN_PCI_DEVICE: {
1656 struct kvm_assigned_pci_dev assigned_dev;
1659 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1661 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1666 case KVM_ASSIGN_IRQ: {
1667 struct kvm_assigned_irq assigned_irq;
1670 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1672 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1679 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1685 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1687 struct page *page[1];
1690 gfn_t gfn = vmf->pgoff;
1691 struct kvm *kvm = vma->vm_file->private_data;
1693 addr = gfn_to_hva(kvm, gfn);
1694 if (kvm_is_error_hva(addr))
1695 return VM_FAULT_SIGBUS;
1697 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1699 if (unlikely(npages != 1))
1700 return VM_FAULT_SIGBUS;
1702 vmf->page = page[0];
1706 static struct vm_operations_struct kvm_vm_vm_ops = {
1707 .fault = kvm_vm_fault,
1710 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1712 vma->vm_ops = &kvm_vm_vm_ops;
1716 static const struct file_operations kvm_vm_fops = {
1717 .release = kvm_vm_release,
1718 .unlocked_ioctl = kvm_vm_ioctl,
1719 .compat_ioctl = kvm_vm_ioctl,
1720 .mmap = kvm_vm_mmap,
1723 static int kvm_dev_ioctl_create_vm(void)
1728 kvm = kvm_create_vm();
1730 return PTR_ERR(kvm);
1731 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1738 static long kvm_dev_ioctl(struct file *filp,
1739 unsigned int ioctl, unsigned long arg)
1744 case KVM_GET_API_VERSION:
1748 r = KVM_API_VERSION;
1754 r = kvm_dev_ioctl_create_vm();
1756 case KVM_CHECK_EXTENSION:
1757 r = kvm_dev_ioctl_check_extension(arg);
1759 case KVM_GET_VCPU_MMAP_SIZE:
1763 r = PAGE_SIZE; /* struct kvm_run */
1765 r += PAGE_SIZE; /* pio data page */
1767 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1768 r += PAGE_SIZE; /* coalesced mmio ring page */
1771 case KVM_TRACE_ENABLE:
1772 case KVM_TRACE_PAUSE:
1773 case KVM_TRACE_DISABLE:
1774 r = kvm_trace_ioctl(ioctl, arg);
1777 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1783 static struct file_operations kvm_chardev_ops = {
1784 .unlocked_ioctl = kvm_dev_ioctl,
1785 .compat_ioctl = kvm_dev_ioctl,
1788 static struct miscdevice kvm_dev = {
1794 static void hardware_enable(void *junk)
1796 int cpu = raw_smp_processor_id();
1798 if (cpu_isset(cpu, cpus_hardware_enabled))
1800 cpu_set(cpu, cpus_hardware_enabled);
1801 kvm_arch_hardware_enable(NULL);
1804 static void hardware_disable(void *junk)
1806 int cpu = raw_smp_processor_id();
1808 if (!cpu_isset(cpu, cpus_hardware_enabled))
1810 cpu_clear(cpu, cpus_hardware_enabled);
1811 kvm_arch_hardware_disable(NULL);
1814 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1819 val &= ~CPU_TASKS_FROZEN;
1822 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1824 hardware_disable(NULL);
1826 case CPU_UP_CANCELED:
1827 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1829 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1832 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1834 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1841 asmlinkage void kvm_handle_fault_on_reboot(void)
1844 /* spin while reset goes on */
1847 /* Fault while not rebooting. We want the trace. */
1850 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1852 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1855 if (val == SYS_RESTART) {
1857 * Some (well, at least mine) BIOSes hang on reboot if
1860 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1861 kvm_rebooting = true;
1862 on_each_cpu(hardware_disable, NULL, 1);
1867 static struct notifier_block kvm_reboot_notifier = {
1868 .notifier_call = kvm_reboot,
1872 void kvm_io_bus_init(struct kvm_io_bus *bus)
1874 memset(bus, 0, sizeof(*bus));
1877 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1881 for (i = 0; i < bus->dev_count; i++) {
1882 struct kvm_io_device *pos = bus->devs[i];
1884 kvm_iodevice_destructor(pos);
1888 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1889 gpa_t addr, int len, int is_write)
1893 for (i = 0; i < bus->dev_count; i++) {
1894 struct kvm_io_device *pos = bus->devs[i];
1896 if (pos->in_range(pos, addr, len, is_write))
1903 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1905 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1907 bus->devs[bus->dev_count++] = dev;
1910 static struct notifier_block kvm_cpu_notifier = {
1911 .notifier_call = kvm_cpu_hotplug,
1912 .priority = 20, /* must be > scheduler priority */
1915 static int vm_stat_get(void *_offset, u64 *val)
1917 unsigned offset = (long)_offset;
1921 spin_lock(&kvm_lock);
1922 list_for_each_entry(kvm, &vm_list, vm_list)
1923 *val += *(u32 *)((void *)kvm + offset);
1924 spin_unlock(&kvm_lock);
1928 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1930 static int vcpu_stat_get(void *_offset, u64 *val)
1932 unsigned offset = (long)_offset;
1934 struct kvm_vcpu *vcpu;
1938 spin_lock(&kvm_lock);
1939 list_for_each_entry(kvm, &vm_list, vm_list)
1940 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1941 vcpu = kvm->vcpus[i];
1943 *val += *(u32 *)((void *)vcpu + offset);
1945 spin_unlock(&kvm_lock);
1949 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1951 static struct file_operations *stat_fops[] = {
1952 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1953 [KVM_STAT_VM] = &vm_stat_fops,
1956 static void kvm_init_debug(void)
1958 struct kvm_stats_debugfs_item *p;
1960 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
1961 for (p = debugfs_entries; p->name; ++p)
1962 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1963 (void *)(long)p->offset,
1964 stat_fops[p->kind]);
1967 static void kvm_exit_debug(void)
1969 struct kvm_stats_debugfs_item *p;
1971 for (p = debugfs_entries; p->name; ++p)
1972 debugfs_remove(p->dentry);
1973 debugfs_remove(kvm_debugfs_dir);
1976 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1978 hardware_disable(NULL);
1982 static int kvm_resume(struct sys_device *dev)
1984 hardware_enable(NULL);
1988 static struct sysdev_class kvm_sysdev_class = {
1990 .suspend = kvm_suspend,
1991 .resume = kvm_resume,
1994 static struct sys_device kvm_sysdev = {
1996 .cls = &kvm_sysdev_class,
1999 struct page *bad_page;
2003 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2005 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2008 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2010 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2012 kvm_arch_vcpu_load(vcpu, cpu);
2015 static void kvm_sched_out(struct preempt_notifier *pn,
2016 struct task_struct *next)
2018 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2020 kvm_arch_vcpu_put(vcpu);
2023 int kvm_init(void *opaque, unsigned int vcpu_size,
2024 struct module *module)
2031 r = kvm_arch_init(opaque);
2035 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2037 if (bad_page == NULL) {
2042 bad_pfn = page_to_pfn(bad_page);
2044 r = kvm_arch_hardware_setup();
2048 for_each_online_cpu(cpu) {
2049 smp_call_function_single(cpu,
2050 kvm_arch_check_processor_compat,
2056 on_each_cpu(hardware_enable, NULL, 1);
2057 r = register_cpu_notifier(&kvm_cpu_notifier);
2060 register_reboot_notifier(&kvm_reboot_notifier);
2062 r = sysdev_class_register(&kvm_sysdev_class);
2066 r = sysdev_register(&kvm_sysdev);
2070 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2071 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2072 __alignof__(struct kvm_vcpu),
2074 if (!kvm_vcpu_cache) {
2079 kvm_chardev_ops.owner = module;
2081 r = misc_register(&kvm_dev);
2083 printk(KERN_ERR "kvm: misc device register failed\n");
2087 kvm_preempt_ops.sched_in = kvm_sched_in;
2088 kvm_preempt_ops.sched_out = kvm_sched_out;
2093 kmem_cache_destroy(kvm_vcpu_cache);
2095 sysdev_unregister(&kvm_sysdev);
2097 sysdev_class_unregister(&kvm_sysdev_class);
2099 unregister_reboot_notifier(&kvm_reboot_notifier);
2100 unregister_cpu_notifier(&kvm_cpu_notifier);
2102 on_each_cpu(hardware_disable, NULL, 1);
2104 kvm_arch_hardware_unsetup();
2106 __free_page(bad_page);
2113 EXPORT_SYMBOL_GPL(kvm_init);
2117 kvm_trace_cleanup();
2118 misc_deregister(&kvm_dev);
2119 kmem_cache_destroy(kvm_vcpu_cache);
2120 sysdev_unregister(&kvm_sysdev);
2121 sysdev_class_unregister(&kvm_sysdev_class);
2122 unregister_reboot_notifier(&kvm_reboot_notifier);
2123 unregister_cpu_notifier(&kvm_cpu_notifier);
2124 on_each_cpu(hardware_disable, NULL, 1);
2125 kvm_arch_hardware_unsetup();
2128 __free_page(bad_page);
2130 EXPORT_SYMBOL_GPL(kvm_exit);