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_type)
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 adev->guest_irq = airq->guest_irq;
184 adev->ack_notifier.gsi = airq->guest_irq;
186 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
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;
198 /* Even though this is PCI, we don't want to use shared
199 * interrupts. Sharing host devices with guest-assigned devices
200 * on the same interrupt line is not a happy situation: there
201 * are going to be long delays in accepting, acking, etc.
203 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
204 0, "kvm_assigned_intx_device", (void *)adev))
208 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
209 KVM_ASSIGNED_DEV_HOST_INTX;
213 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
214 struct kvm_assigned_irq
218 struct kvm_assigned_dev_kernel *match;
220 mutex_lock(&kvm->lock);
222 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
223 assigned_irq->assigned_dev_id);
225 mutex_unlock(&kvm->lock);
229 if (!match->irq_requested_type) {
230 INIT_WORK(&match->interrupt_work,
231 kvm_assigned_dev_interrupt_work_handler);
232 if (irqchip_in_kernel(kvm)) {
233 /* Register ack nofitier */
234 match->ack_notifier.gsi = -1;
235 match->ack_notifier.irq_acked =
236 kvm_assigned_dev_ack_irq;
237 kvm_register_irq_ack_notifier(kvm,
238 &match->ack_notifier);
240 /* Request IRQ source ID */
241 r = kvm_request_irq_source_id(kvm);
245 match->irq_source_id = r;
249 r = assigned_device_update_intx(kvm, match, assigned_irq);
253 mutex_unlock(&kvm->lock);
256 mutex_unlock(&kvm->lock);
257 kvm_free_assigned_device(kvm, match);
261 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
262 struct kvm_assigned_pci_dev *assigned_dev)
265 struct kvm_assigned_dev_kernel *match;
268 mutex_lock(&kvm->lock);
270 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
271 assigned_dev->assigned_dev_id);
273 /* device already assigned */
278 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
280 printk(KERN_INFO "%s: Couldn't allocate memory\n",
285 dev = pci_get_bus_and_slot(assigned_dev->busnr,
286 assigned_dev->devfn);
288 printk(KERN_INFO "%s: host device not found\n", __func__);
292 if (pci_enable_device(dev)) {
293 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
297 r = pci_request_regions(dev, "kvm_assigned_device");
299 printk(KERN_INFO "%s: Could not get access to device regions\n",
304 pci_reset_function(dev);
306 match->assigned_dev_id = assigned_dev->assigned_dev_id;
307 match->host_busnr = assigned_dev->busnr;
308 match->host_devfn = assigned_dev->devfn;
313 list_add(&match->list, &kvm->arch.assigned_dev_head);
315 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
316 r = kvm_iommu_map_guest(kvm, match);
322 mutex_unlock(&kvm->lock);
325 list_del(&match->list);
326 pci_release_regions(dev);
328 pci_disable_device(dev);
333 mutex_unlock(&kvm->lock);
338 static inline int valid_vcpu(int n)
340 return likely(n >= 0 && n < KVM_MAX_VCPUS);
343 inline int kvm_is_mmio_pfn(pfn_t pfn)
346 return PageReserved(pfn_to_page(pfn));
352 * Switches to specified vcpu, until a matching vcpu_put()
354 void vcpu_load(struct kvm_vcpu *vcpu)
358 mutex_lock(&vcpu->mutex);
360 preempt_notifier_register(&vcpu->preempt_notifier);
361 kvm_arch_vcpu_load(vcpu, cpu);
365 void vcpu_put(struct kvm_vcpu *vcpu)
368 kvm_arch_vcpu_put(vcpu);
369 preempt_notifier_unregister(&vcpu->preempt_notifier);
371 mutex_unlock(&vcpu->mutex);
374 static void ack_flush(void *_completed)
378 void kvm_flush_remote_tlbs(struct kvm *kvm)
382 struct kvm_vcpu *vcpu;
386 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
387 vcpu = kvm->vcpus[i];
390 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
393 if (cpu != -1 && cpu != me)
396 if (cpus_empty(cpus))
398 ++kvm->stat.remote_tlb_flush;
399 smp_call_function_mask(cpus, ack_flush, NULL, 1);
404 void kvm_reload_remote_mmus(struct kvm *kvm)
408 struct kvm_vcpu *vcpu;
412 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
413 vcpu = kvm->vcpus[i];
416 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
419 if (cpu != -1 && cpu != me)
422 if (cpus_empty(cpus))
424 smp_call_function_mask(cpus, ack_flush, NULL, 1);
430 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
435 mutex_init(&vcpu->mutex);
439 init_waitqueue_head(&vcpu->wq);
441 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
446 vcpu->run = page_address(page);
448 r = kvm_arch_vcpu_init(vcpu);
454 free_page((unsigned long)vcpu->run);
458 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
460 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
462 kvm_arch_vcpu_uninit(vcpu);
463 free_page((unsigned long)vcpu->run);
465 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
467 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
468 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
470 return container_of(mn, struct kvm, mmu_notifier);
473 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
474 struct mm_struct *mm,
475 unsigned long address)
477 struct kvm *kvm = mmu_notifier_to_kvm(mn);
481 * When ->invalidate_page runs, the linux pte has been zapped
482 * already but the page is still allocated until
483 * ->invalidate_page returns. So if we increase the sequence
484 * here the kvm page fault will notice if the spte can't be
485 * established because the page is going to be freed. If
486 * instead the kvm page fault establishes the spte before
487 * ->invalidate_page runs, kvm_unmap_hva will release it
490 * The sequence increase only need to be seen at spin_unlock
491 * time, and not at spin_lock time.
493 * Increasing the sequence after the spin_unlock would be
494 * unsafe because the kvm page fault could then establish the
495 * pte after kvm_unmap_hva returned, without noticing the page
496 * is going to be freed.
498 spin_lock(&kvm->mmu_lock);
499 kvm->mmu_notifier_seq++;
500 need_tlb_flush = kvm_unmap_hva(kvm, address);
501 spin_unlock(&kvm->mmu_lock);
503 /* we've to flush the tlb before the pages can be freed */
505 kvm_flush_remote_tlbs(kvm);
509 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
510 struct mm_struct *mm,
514 struct kvm *kvm = mmu_notifier_to_kvm(mn);
515 int need_tlb_flush = 0;
517 spin_lock(&kvm->mmu_lock);
519 * The count increase must become visible at unlock time as no
520 * spte can be established without taking the mmu_lock and
521 * count is also read inside the mmu_lock critical section.
523 kvm->mmu_notifier_count++;
524 for (; start < end; start += PAGE_SIZE)
525 need_tlb_flush |= kvm_unmap_hva(kvm, start);
526 spin_unlock(&kvm->mmu_lock);
528 /* we've to flush the tlb before the pages can be freed */
530 kvm_flush_remote_tlbs(kvm);
533 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
534 struct mm_struct *mm,
538 struct kvm *kvm = mmu_notifier_to_kvm(mn);
540 spin_lock(&kvm->mmu_lock);
542 * This sequence increase will notify the kvm page fault that
543 * the page that is going to be mapped in the spte could have
546 kvm->mmu_notifier_seq++;
548 * The above sequence increase must be visible before the
549 * below count decrease but both values are read by the kvm
550 * page fault under mmu_lock spinlock so we don't need to add
551 * a smb_wmb() here in between the two.
553 kvm->mmu_notifier_count--;
554 spin_unlock(&kvm->mmu_lock);
556 BUG_ON(kvm->mmu_notifier_count < 0);
559 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
560 struct mm_struct *mm,
561 unsigned long address)
563 struct kvm *kvm = mmu_notifier_to_kvm(mn);
566 spin_lock(&kvm->mmu_lock);
567 young = kvm_age_hva(kvm, address);
568 spin_unlock(&kvm->mmu_lock);
571 kvm_flush_remote_tlbs(kvm);
576 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
577 .invalidate_page = kvm_mmu_notifier_invalidate_page,
578 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
579 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
580 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
582 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
584 static struct kvm *kvm_create_vm(void)
586 struct kvm *kvm = kvm_arch_create_vm();
587 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
594 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
595 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
598 return ERR_PTR(-ENOMEM);
600 kvm->coalesced_mmio_ring =
601 (struct kvm_coalesced_mmio_ring *)page_address(page);
604 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
607 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
608 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
610 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
619 kvm->mm = current->mm;
620 atomic_inc(&kvm->mm->mm_count);
621 spin_lock_init(&kvm->mmu_lock);
622 kvm_io_bus_init(&kvm->pio_bus);
623 mutex_init(&kvm->lock);
624 kvm_io_bus_init(&kvm->mmio_bus);
625 init_rwsem(&kvm->slots_lock);
626 atomic_set(&kvm->users_count, 1);
627 spin_lock(&kvm_lock);
628 list_add(&kvm->vm_list, &vm_list);
629 spin_unlock(&kvm_lock);
630 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
631 kvm_coalesced_mmio_init(kvm);
638 * Free any memory in @free but not in @dont.
640 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
641 struct kvm_memory_slot *dont)
643 if (!dont || free->rmap != dont->rmap)
646 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
647 vfree(free->dirty_bitmap);
649 if (!dont || free->lpage_info != dont->lpage_info)
650 vfree(free->lpage_info);
653 free->dirty_bitmap = NULL;
655 free->lpage_info = NULL;
658 void kvm_free_physmem(struct kvm *kvm)
662 for (i = 0; i < kvm->nmemslots; ++i)
663 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
666 static void kvm_destroy_vm(struct kvm *kvm)
668 struct mm_struct *mm = kvm->mm;
670 spin_lock(&kvm_lock);
671 list_del(&kvm->vm_list);
672 spin_unlock(&kvm_lock);
673 kvm_io_bus_destroy(&kvm->pio_bus);
674 kvm_io_bus_destroy(&kvm->mmio_bus);
675 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
676 if (kvm->coalesced_mmio_ring != NULL)
677 free_page((unsigned long)kvm->coalesced_mmio_ring);
679 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
680 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
682 kvm_arch_destroy_vm(kvm);
686 void kvm_get_kvm(struct kvm *kvm)
688 atomic_inc(&kvm->users_count);
690 EXPORT_SYMBOL_GPL(kvm_get_kvm);
692 void kvm_put_kvm(struct kvm *kvm)
694 if (atomic_dec_and_test(&kvm->users_count))
697 EXPORT_SYMBOL_GPL(kvm_put_kvm);
700 static int kvm_vm_release(struct inode *inode, struct file *filp)
702 struct kvm *kvm = filp->private_data;
709 * Allocate some memory and give it an address in the guest physical address
712 * Discontiguous memory is allowed, mostly for framebuffers.
714 * Must be called holding mmap_sem for write.
716 int __kvm_set_memory_region(struct kvm *kvm,
717 struct kvm_userspace_memory_region *mem,
722 unsigned long npages;
724 struct kvm_memory_slot *memslot;
725 struct kvm_memory_slot old, new;
728 /* General sanity checks */
729 if (mem->memory_size & (PAGE_SIZE - 1))
731 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
733 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
735 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
737 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
740 memslot = &kvm->memslots[mem->slot];
741 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
742 npages = mem->memory_size >> PAGE_SHIFT;
745 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
747 new = old = *memslot;
749 new.base_gfn = base_gfn;
751 new.flags = mem->flags;
753 /* Disallow changing a memory slot's size. */
755 if (npages && old.npages && npages != old.npages)
758 /* Check for overlaps */
760 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
761 struct kvm_memory_slot *s = &kvm->memslots[i];
765 if (!((base_gfn + npages <= s->base_gfn) ||
766 (base_gfn >= s->base_gfn + s->npages)))
770 /* Free page dirty bitmap if unneeded */
771 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
772 new.dirty_bitmap = NULL;
776 /* Allocate if a slot is being created */
778 if (npages && !new.rmap) {
779 new.rmap = vmalloc(npages * sizeof(struct page *));
784 memset(new.rmap, 0, npages * sizeof(*new.rmap));
786 new.user_alloc = user_alloc;
788 * hva_to_rmmap() serialzies with the mmu_lock and to be
789 * safe it has to ignore memslots with !user_alloc &&
793 new.userspace_addr = mem->userspace_addr;
795 new.userspace_addr = 0;
797 if (npages && !new.lpage_info) {
798 int largepages = npages / KVM_PAGES_PER_HPAGE;
799 if (npages % KVM_PAGES_PER_HPAGE)
801 if (base_gfn % KVM_PAGES_PER_HPAGE)
804 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
809 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
811 if (base_gfn % KVM_PAGES_PER_HPAGE)
812 new.lpage_info[0].write_count = 1;
813 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
814 new.lpage_info[largepages-1].write_count = 1;
817 /* Allocate page dirty bitmap if needed */
818 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
819 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
821 new.dirty_bitmap = vmalloc(dirty_bytes);
822 if (!new.dirty_bitmap)
824 memset(new.dirty_bitmap, 0, dirty_bytes);
826 #endif /* not defined CONFIG_S390 */
829 kvm_arch_flush_shadow(kvm);
831 spin_lock(&kvm->mmu_lock);
832 if (mem->slot >= kvm->nmemslots)
833 kvm->nmemslots = mem->slot + 1;
836 spin_unlock(&kvm->mmu_lock);
838 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
840 spin_lock(&kvm->mmu_lock);
842 spin_unlock(&kvm->mmu_lock);
846 kvm_free_physmem_slot(&old, &new);
848 /* map the pages in iommu page table */
849 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
856 kvm_free_physmem_slot(&new, &old);
861 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
863 int kvm_set_memory_region(struct kvm *kvm,
864 struct kvm_userspace_memory_region *mem,
869 down_write(&kvm->slots_lock);
870 r = __kvm_set_memory_region(kvm, mem, user_alloc);
871 up_write(&kvm->slots_lock);
874 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
876 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
878 kvm_userspace_memory_region *mem,
881 if (mem->slot >= KVM_MEMORY_SLOTS)
883 return kvm_set_memory_region(kvm, mem, user_alloc);
886 int kvm_get_dirty_log(struct kvm *kvm,
887 struct kvm_dirty_log *log, int *is_dirty)
889 struct kvm_memory_slot *memslot;
892 unsigned long any = 0;
895 if (log->slot >= KVM_MEMORY_SLOTS)
898 memslot = &kvm->memslots[log->slot];
900 if (!memslot->dirty_bitmap)
903 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
905 for (i = 0; !any && i < n/sizeof(long); ++i)
906 any = memslot->dirty_bitmap[i];
909 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
920 int is_error_page(struct page *page)
922 return page == bad_page;
924 EXPORT_SYMBOL_GPL(is_error_page);
926 int is_error_pfn(pfn_t pfn)
928 return pfn == bad_pfn;
930 EXPORT_SYMBOL_GPL(is_error_pfn);
932 static inline unsigned long bad_hva(void)
937 int kvm_is_error_hva(unsigned long addr)
939 return addr == bad_hva();
941 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
943 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
947 for (i = 0; i < kvm->nmemslots; ++i) {
948 struct kvm_memory_slot *memslot = &kvm->memslots[i];
950 if (gfn >= memslot->base_gfn
951 && gfn < memslot->base_gfn + memslot->npages)
956 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
958 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
960 gfn = unalias_gfn(kvm, gfn);
961 return gfn_to_memslot_unaliased(kvm, gfn);
964 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
968 gfn = unalias_gfn(kvm, gfn);
969 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
970 struct kvm_memory_slot *memslot = &kvm->memslots[i];
972 if (gfn >= memslot->base_gfn
973 && gfn < memslot->base_gfn + memslot->npages)
978 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
980 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
982 struct kvm_memory_slot *slot;
984 gfn = unalias_gfn(kvm, gfn);
985 slot = gfn_to_memslot_unaliased(kvm, gfn);
988 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
990 EXPORT_SYMBOL_GPL(gfn_to_hva);
992 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
994 struct page *page[1];
1001 addr = gfn_to_hva(kvm, gfn);
1002 if (kvm_is_error_hva(addr)) {
1004 return page_to_pfn(bad_page);
1007 npages = get_user_pages_fast(addr, 1, 1, page);
1009 if (unlikely(npages != 1)) {
1010 struct vm_area_struct *vma;
1012 down_read(¤t->mm->mmap_sem);
1013 vma = find_vma(current->mm, addr);
1015 if (vma == NULL || addr < vma->vm_start ||
1016 !(vma->vm_flags & VM_PFNMAP)) {
1017 up_read(¤t->mm->mmap_sem);
1019 return page_to_pfn(bad_page);
1022 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1023 up_read(¤t->mm->mmap_sem);
1024 BUG_ON(!kvm_is_mmio_pfn(pfn));
1026 pfn = page_to_pfn(page[0]);
1031 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1033 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1037 pfn = gfn_to_pfn(kvm, gfn);
1038 if (!kvm_is_mmio_pfn(pfn))
1039 return pfn_to_page(pfn);
1041 WARN_ON(kvm_is_mmio_pfn(pfn));
1047 EXPORT_SYMBOL_GPL(gfn_to_page);
1049 void kvm_release_page_clean(struct page *page)
1051 kvm_release_pfn_clean(page_to_pfn(page));
1053 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1055 void kvm_release_pfn_clean(pfn_t pfn)
1057 if (!kvm_is_mmio_pfn(pfn))
1058 put_page(pfn_to_page(pfn));
1060 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1062 void kvm_release_page_dirty(struct page *page)
1064 kvm_release_pfn_dirty(page_to_pfn(page));
1066 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1068 void kvm_release_pfn_dirty(pfn_t pfn)
1070 kvm_set_pfn_dirty(pfn);
1071 kvm_release_pfn_clean(pfn);
1073 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1075 void kvm_set_page_dirty(struct page *page)
1077 kvm_set_pfn_dirty(page_to_pfn(page));
1079 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1081 void kvm_set_pfn_dirty(pfn_t pfn)
1083 if (!kvm_is_mmio_pfn(pfn)) {
1084 struct page *page = pfn_to_page(pfn);
1085 if (!PageReserved(page))
1089 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1091 void kvm_set_pfn_accessed(pfn_t pfn)
1093 if (!kvm_is_mmio_pfn(pfn))
1094 mark_page_accessed(pfn_to_page(pfn));
1096 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1098 void kvm_get_pfn(pfn_t pfn)
1100 if (!kvm_is_mmio_pfn(pfn))
1101 get_page(pfn_to_page(pfn));
1103 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1105 static int next_segment(unsigned long len, int offset)
1107 if (len > PAGE_SIZE - offset)
1108 return PAGE_SIZE - offset;
1113 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1119 addr = gfn_to_hva(kvm, gfn);
1120 if (kvm_is_error_hva(addr))
1122 r = copy_from_user(data, (void __user *)addr + offset, len);
1127 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1129 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1131 gfn_t gfn = gpa >> PAGE_SHIFT;
1133 int offset = offset_in_page(gpa);
1136 while ((seg = next_segment(len, offset)) != 0) {
1137 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1147 EXPORT_SYMBOL_GPL(kvm_read_guest);
1149 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1154 gfn_t gfn = gpa >> PAGE_SHIFT;
1155 int offset = offset_in_page(gpa);
1157 addr = gfn_to_hva(kvm, gfn);
1158 if (kvm_is_error_hva(addr))
1160 pagefault_disable();
1161 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1167 EXPORT_SYMBOL(kvm_read_guest_atomic);
1169 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1170 int offset, int len)
1175 addr = gfn_to_hva(kvm, gfn);
1176 if (kvm_is_error_hva(addr))
1178 r = copy_to_user((void __user *)addr + offset, data, len);
1181 mark_page_dirty(kvm, gfn);
1184 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1186 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1189 gfn_t gfn = gpa >> PAGE_SHIFT;
1191 int offset = offset_in_page(gpa);
1194 while ((seg = next_segment(len, offset)) != 0) {
1195 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1206 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1208 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1210 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1212 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1214 gfn_t gfn = gpa >> PAGE_SHIFT;
1216 int offset = offset_in_page(gpa);
1219 while ((seg = next_segment(len, offset)) != 0) {
1220 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1229 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1231 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1233 struct kvm_memory_slot *memslot;
1235 gfn = unalias_gfn(kvm, gfn);
1236 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1237 if (memslot && memslot->dirty_bitmap) {
1238 unsigned long rel_gfn = gfn - memslot->base_gfn;
1241 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1242 set_bit(rel_gfn, memslot->dirty_bitmap);
1247 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1249 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1254 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1256 if (kvm_cpu_has_interrupt(vcpu) ||
1257 kvm_cpu_has_pending_timer(vcpu) ||
1258 kvm_arch_vcpu_runnable(vcpu)) {
1259 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1262 if (signal_pending(current))
1270 finish_wait(&vcpu->wq, &wait);
1273 void kvm_resched(struct kvm_vcpu *vcpu)
1275 if (!need_resched())
1279 EXPORT_SYMBOL_GPL(kvm_resched);
1281 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1283 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1286 if (vmf->pgoff == 0)
1287 page = virt_to_page(vcpu->run);
1289 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1290 page = virt_to_page(vcpu->arch.pio_data);
1292 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1293 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1294 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1297 return VM_FAULT_SIGBUS;
1303 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1304 .fault = kvm_vcpu_fault,
1307 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1309 vma->vm_ops = &kvm_vcpu_vm_ops;
1313 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1315 struct kvm_vcpu *vcpu = filp->private_data;
1317 kvm_put_kvm(vcpu->kvm);
1321 static const struct file_operations kvm_vcpu_fops = {
1322 .release = kvm_vcpu_release,
1323 .unlocked_ioctl = kvm_vcpu_ioctl,
1324 .compat_ioctl = kvm_vcpu_ioctl,
1325 .mmap = kvm_vcpu_mmap,
1329 * Allocates an inode for the vcpu.
1331 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1333 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1335 kvm_put_kvm(vcpu->kvm);
1340 * Creates some virtual cpus. Good luck creating more than one.
1342 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1345 struct kvm_vcpu *vcpu;
1350 vcpu = kvm_arch_vcpu_create(kvm, n);
1352 return PTR_ERR(vcpu);
1354 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1356 r = kvm_arch_vcpu_setup(vcpu);
1360 mutex_lock(&kvm->lock);
1361 if (kvm->vcpus[n]) {
1365 kvm->vcpus[n] = vcpu;
1366 mutex_unlock(&kvm->lock);
1368 /* Now it's all set up, let userspace reach it */
1370 r = create_vcpu_fd(vcpu);
1376 mutex_lock(&kvm->lock);
1377 kvm->vcpus[n] = NULL;
1379 mutex_unlock(&kvm->lock);
1380 kvm_arch_vcpu_destroy(vcpu);
1384 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1387 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1388 vcpu->sigset_active = 1;
1389 vcpu->sigset = *sigset;
1391 vcpu->sigset_active = 0;
1395 static long kvm_vcpu_ioctl(struct file *filp,
1396 unsigned int ioctl, unsigned long arg)
1398 struct kvm_vcpu *vcpu = filp->private_data;
1399 void __user *argp = (void __user *)arg;
1401 struct kvm_fpu *fpu = NULL;
1402 struct kvm_sregs *kvm_sregs = NULL;
1404 if (vcpu->kvm->mm != current->mm)
1411 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1413 case KVM_GET_REGS: {
1414 struct kvm_regs *kvm_regs;
1417 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1420 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1424 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1431 case KVM_SET_REGS: {
1432 struct kvm_regs *kvm_regs;
1435 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1439 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1441 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1449 case KVM_GET_SREGS: {
1450 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1454 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1458 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1463 case KVM_SET_SREGS: {
1464 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1469 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1471 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1477 case KVM_GET_MP_STATE: {
1478 struct kvm_mp_state mp_state;
1480 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1484 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1489 case KVM_SET_MP_STATE: {
1490 struct kvm_mp_state mp_state;
1493 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1495 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1501 case KVM_TRANSLATE: {
1502 struct kvm_translation tr;
1505 if (copy_from_user(&tr, argp, sizeof tr))
1507 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1511 if (copy_to_user(argp, &tr, sizeof tr))
1516 case KVM_DEBUG_GUEST: {
1517 struct kvm_debug_guest dbg;
1520 if (copy_from_user(&dbg, argp, sizeof dbg))
1522 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1528 case KVM_SET_SIGNAL_MASK: {
1529 struct kvm_signal_mask __user *sigmask_arg = argp;
1530 struct kvm_signal_mask kvm_sigmask;
1531 sigset_t sigset, *p;
1536 if (copy_from_user(&kvm_sigmask, argp,
1537 sizeof kvm_sigmask))
1540 if (kvm_sigmask.len != sizeof sigset)
1543 if (copy_from_user(&sigset, sigmask_arg->sigset,
1548 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1552 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1556 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1560 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1566 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1571 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1573 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1580 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1588 static long kvm_vm_ioctl(struct file *filp,
1589 unsigned int ioctl, unsigned long arg)
1591 struct kvm *kvm = filp->private_data;
1592 void __user *argp = (void __user *)arg;
1595 if (kvm->mm != current->mm)
1598 case KVM_CREATE_VCPU:
1599 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1603 case KVM_SET_USER_MEMORY_REGION: {
1604 struct kvm_userspace_memory_region kvm_userspace_mem;
1607 if (copy_from_user(&kvm_userspace_mem, argp,
1608 sizeof kvm_userspace_mem))
1611 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1616 case KVM_GET_DIRTY_LOG: {
1617 struct kvm_dirty_log log;
1620 if (copy_from_user(&log, argp, sizeof log))
1622 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1627 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1628 case KVM_REGISTER_COALESCED_MMIO: {
1629 struct kvm_coalesced_mmio_zone zone;
1631 if (copy_from_user(&zone, argp, sizeof zone))
1634 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1640 case KVM_UNREGISTER_COALESCED_MMIO: {
1641 struct kvm_coalesced_mmio_zone zone;
1643 if (copy_from_user(&zone, argp, sizeof zone))
1646 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1653 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1654 case KVM_ASSIGN_PCI_DEVICE: {
1655 struct kvm_assigned_pci_dev assigned_dev;
1658 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1660 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1665 case KVM_ASSIGN_IRQ: {
1666 struct kvm_assigned_irq assigned_irq;
1669 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1671 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1678 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1684 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1686 struct page *page[1];
1689 gfn_t gfn = vmf->pgoff;
1690 struct kvm *kvm = vma->vm_file->private_data;
1692 addr = gfn_to_hva(kvm, gfn);
1693 if (kvm_is_error_hva(addr))
1694 return VM_FAULT_SIGBUS;
1696 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1698 if (unlikely(npages != 1))
1699 return VM_FAULT_SIGBUS;
1701 vmf->page = page[0];
1705 static struct vm_operations_struct kvm_vm_vm_ops = {
1706 .fault = kvm_vm_fault,
1709 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1711 vma->vm_ops = &kvm_vm_vm_ops;
1715 static const struct file_operations kvm_vm_fops = {
1716 .release = kvm_vm_release,
1717 .unlocked_ioctl = kvm_vm_ioctl,
1718 .compat_ioctl = kvm_vm_ioctl,
1719 .mmap = kvm_vm_mmap,
1722 static int kvm_dev_ioctl_create_vm(void)
1727 kvm = kvm_create_vm();
1729 return PTR_ERR(kvm);
1730 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1737 static long kvm_dev_ioctl(struct file *filp,
1738 unsigned int ioctl, unsigned long arg)
1743 case KVM_GET_API_VERSION:
1747 r = KVM_API_VERSION;
1753 r = kvm_dev_ioctl_create_vm();
1755 case KVM_CHECK_EXTENSION:
1756 r = kvm_dev_ioctl_check_extension(arg);
1758 case KVM_GET_VCPU_MMAP_SIZE:
1762 r = PAGE_SIZE; /* struct kvm_run */
1764 r += PAGE_SIZE; /* pio data page */
1766 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1767 r += PAGE_SIZE; /* coalesced mmio ring page */
1770 case KVM_TRACE_ENABLE:
1771 case KVM_TRACE_PAUSE:
1772 case KVM_TRACE_DISABLE:
1773 r = kvm_trace_ioctl(ioctl, arg);
1776 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1782 static struct file_operations kvm_chardev_ops = {
1783 .unlocked_ioctl = kvm_dev_ioctl,
1784 .compat_ioctl = kvm_dev_ioctl,
1787 static struct miscdevice kvm_dev = {
1793 static void hardware_enable(void *junk)
1795 int cpu = raw_smp_processor_id();
1797 if (cpu_isset(cpu, cpus_hardware_enabled))
1799 cpu_set(cpu, cpus_hardware_enabled);
1800 kvm_arch_hardware_enable(NULL);
1803 static void hardware_disable(void *junk)
1805 int cpu = raw_smp_processor_id();
1807 if (!cpu_isset(cpu, cpus_hardware_enabled))
1809 cpu_clear(cpu, cpus_hardware_enabled);
1810 kvm_arch_hardware_disable(NULL);
1813 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1818 val &= ~CPU_TASKS_FROZEN;
1821 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1823 hardware_disable(NULL);
1825 case CPU_UP_CANCELED:
1826 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1828 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1831 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1833 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1840 asmlinkage void kvm_handle_fault_on_reboot(void)
1843 /* spin while reset goes on */
1846 /* Fault while not rebooting. We want the trace. */
1849 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1851 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1854 if (val == SYS_RESTART) {
1856 * Some (well, at least mine) BIOSes hang on reboot if
1859 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1860 kvm_rebooting = true;
1861 on_each_cpu(hardware_disable, NULL, 1);
1866 static struct notifier_block kvm_reboot_notifier = {
1867 .notifier_call = kvm_reboot,
1871 void kvm_io_bus_init(struct kvm_io_bus *bus)
1873 memset(bus, 0, sizeof(*bus));
1876 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1880 for (i = 0; i < bus->dev_count; i++) {
1881 struct kvm_io_device *pos = bus->devs[i];
1883 kvm_iodevice_destructor(pos);
1887 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1888 gpa_t addr, int len, int is_write)
1892 for (i = 0; i < bus->dev_count; i++) {
1893 struct kvm_io_device *pos = bus->devs[i];
1895 if (pos->in_range(pos, addr, len, is_write))
1902 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1904 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1906 bus->devs[bus->dev_count++] = dev;
1909 static struct notifier_block kvm_cpu_notifier = {
1910 .notifier_call = kvm_cpu_hotplug,
1911 .priority = 20, /* must be > scheduler priority */
1914 static int vm_stat_get(void *_offset, u64 *val)
1916 unsigned offset = (long)_offset;
1920 spin_lock(&kvm_lock);
1921 list_for_each_entry(kvm, &vm_list, vm_list)
1922 *val += *(u32 *)((void *)kvm + offset);
1923 spin_unlock(&kvm_lock);
1927 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1929 static int vcpu_stat_get(void *_offset, u64 *val)
1931 unsigned offset = (long)_offset;
1933 struct kvm_vcpu *vcpu;
1937 spin_lock(&kvm_lock);
1938 list_for_each_entry(kvm, &vm_list, vm_list)
1939 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1940 vcpu = kvm->vcpus[i];
1942 *val += *(u32 *)((void *)vcpu + offset);
1944 spin_unlock(&kvm_lock);
1948 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1950 static struct file_operations *stat_fops[] = {
1951 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1952 [KVM_STAT_VM] = &vm_stat_fops,
1955 static void kvm_init_debug(void)
1957 struct kvm_stats_debugfs_item *p;
1959 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
1960 for (p = debugfs_entries; p->name; ++p)
1961 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1962 (void *)(long)p->offset,
1963 stat_fops[p->kind]);
1966 static void kvm_exit_debug(void)
1968 struct kvm_stats_debugfs_item *p;
1970 for (p = debugfs_entries; p->name; ++p)
1971 debugfs_remove(p->dentry);
1972 debugfs_remove(kvm_debugfs_dir);
1975 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1977 hardware_disable(NULL);
1981 static int kvm_resume(struct sys_device *dev)
1983 hardware_enable(NULL);
1987 static struct sysdev_class kvm_sysdev_class = {
1989 .suspend = kvm_suspend,
1990 .resume = kvm_resume,
1993 static struct sys_device kvm_sysdev = {
1995 .cls = &kvm_sysdev_class,
1998 struct page *bad_page;
2002 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2004 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2007 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2009 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2011 kvm_arch_vcpu_load(vcpu, cpu);
2014 static void kvm_sched_out(struct preempt_notifier *pn,
2015 struct task_struct *next)
2017 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2019 kvm_arch_vcpu_put(vcpu);
2022 int kvm_init(void *opaque, unsigned int vcpu_size,
2023 struct module *module)
2030 r = kvm_arch_init(opaque);
2034 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2036 if (bad_page == NULL) {
2041 bad_pfn = page_to_pfn(bad_page);
2043 r = kvm_arch_hardware_setup();
2047 for_each_online_cpu(cpu) {
2048 smp_call_function_single(cpu,
2049 kvm_arch_check_processor_compat,
2055 on_each_cpu(hardware_enable, NULL, 1);
2056 r = register_cpu_notifier(&kvm_cpu_notifier);
2059 register_reboot_notifier(&kvm_reboot_notifier);
2061 r = sysdev_class_register(&kvm_sysdev_class);
2065 r = sysdev_register(&kvm_sysdev);
2069 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2070 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2071 __alignof__(struct kvm_vcpu),
2073 if (!kvm_vcpu_cache) {
2078 kvm_chardev_ops.owner = module;
2080 r = misc_register(&kvm_dev);
2082 printk(KERN_ERR "kvm: misc device register failed\n");
2086 kvm_preempt_ops.sched_in = kvm_sched_in;
2087 kvm_preempt_ops.sched_out = kvm_sched_out;
2092 kmem_cache_destroy(kvm_vcpu_cache);
2094 sysdev_unregister(&kvm_sysdev);
2096 sysdev_class_unregister(&kvm_sysdev_class);
2098 unregister_reboot_notifier(&kvm_reboot_notifier);
2099 unregister_cpu_notifier(&kvm_cpu_notifier);
2101 on_each_cpu(hardware_disable, NULL, 1);
2103 kvm_arch_hardware_unsetup();
2105 __free_page(bad_page);
2112 EXPORT_SYMBOL_GPL(kvm_init);
2116 kvm_trace_cleanup();
2117 misc_deregister(&kvm_dev);
2118 kmem_cache_destroy(kvm_vcpu_cache);
2119 sysdev_unregister(&kvm_sysdev);
2120 sysdev_class_unregister(&kvm_sysdev_class);
2121 unregister_reboot_notifier(&kvm_reboot_notifier);
2122 unregister_cpu_notifier(&kvm_cpu_notifier);
2123 on_each_cpu(hardware_disable, NULL, 1);
2124 kvm_arch_hardware_unsetup();
2127 __free_page(bad_page);
2129 EXPORT_SYMBOL_GPL(kvm_exit);