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.
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->guest_irq, 1);
109 mutex_unlock(&assigned_dev->kvm->lock);
110 kvm_put_kvm(assigned_dev->kvm);
113 /* FIXME: Implement the OR logic needed to make shared interrupts on
114 * this line behave properly
116 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
118 struct kvm_assigned_dev_kernel *assigned_dev =
119 (struct kvm_assigned_dev_kernel *) dev_id;
121 kvm_get_kvm(assigned_dev->kvm);
122 schedule_work(&assigned_dev->interrupt_work);
123 disable_irq_nosync(irq);
127 /* Ack the irq line for an assigned device */
128 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
130 struct kvm_assigned_dev_kernel *dev;
135 dev = container_of(kian, struct kvm_assigned_dev_kernel,
137 kvm_set_irq(dev->kvm, dev->guest_irq, 0);
138 enable_irq(dev->host_irq);
141 static void kvm_free_assigned_device(struct kvm *kvm,
142 struct kvm_assigned_dev_kernel
145 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
146 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
148 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
150 if (cancel_work_sync(&assigned_dev->interrupt_work))
151 /* We had pending work. That means we will have to take
152 * care of kvm_put_kvm.
156 pci_release_regions(assigned_dev->dev);
157 pci_disable_device(assigned_dev->dev);
158 pci_dev_put(assigned_dev->dev);
160 list_del(&assigned_dev->list);
164 void kvm_free_all_assigned_devices(struct kvm *kvm)
166 struct list_head *ptr, *ptr2;
167 struct kvm_assigned_dev_kernel *assigned_dev;
169 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
170 assigned_dev = list_entry(ptr,
171 struct kvm_assigned_dev_kernel,
174 kvm_free_assigned_device(kvm, assigned_dev);
178 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
179 struct kvm_assigned_irq
183 struct kvm_assigned_dev_kernel *match;
185 mutex_lock(&kvm->lock);
187 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
188 assigned_irq->assigned_dev_id);
190 mutex_unlock(&kvm->lock);
194 if (match->irq_requested) {
195 match->guest_irq = assigned_irq->guest_irq;
196 match->ack_notifier.gsi = assigned_irq->guest_irq;
197 mutex_unlock(&kvm->lock);
201 INIT_WORK(&match->interrupt_work,
202 kvm_assigned_dev_interrupt_work_handler);
204 if (irqchip_in_kernel(kvm)) {
205 if (!capable(CAP_SYS_RAWIO)) {
210 if (assigned_irq->host_irq)
211 match->host_irq = assigned_irq->host_irq;
213 match->host_irq = match->dev->irq;
214 match->guest_irq = assigned_irq->guest_irq;
215 match->ack_notifier.gsi = assigned_irq->guest_irq;
216 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
217 kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
219 /* Even though this is PCI, we don't want to use shared
220 * interrupts. Sharing host devices with guest-assigned devices
221 * on the same interrupt line is not a happy situation: there
222 * are going to be long delays in accepting, acking, etc.
224 if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
225 "kvm_assigned_device", (void *)match)) {
231 match->irq_requested = true;
232 mutex_unlock(&kvm->lock);
235 mutex_unlock(&kvm->lock);
236 kvm_free_assigned_device(kvm, match);
240 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
241 struct kvm_assigned_pci_dev *assigned_dev)
244 struct kvm_assigned_dev_kernel *match;
247 mutex_lock(&kvm->lock);
249 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
250 assigned_dev->assigned_dev_id);
252 /* device already assigned */
257 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
259 printk(KERN_INFO "%s: Couldn't allocate memory\n",
264 dev = pci_get_bus_and_slot(assigned_dev->busnr,
265 assigned_dev->devfn);
267 printk(KERN_INFO "%s: host device not found\n", __func__);
271 if (pci_enable_device(dev)) {
272 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
276 r = pci_request_regions(dev, "kvm_assigned_device");
278 printk(KERN_INFO "%s: Could not get access to device regions\n",
282 match->assigned_dev_id = assigned_dev->assigned_dev_id;
283 match->host_busnr = assigned_dev->busnr;
284 match->host_devfn = assigned_dev->devfn;
289 list_add(&match->list, &kvm->arch.assigned_dev_head);
291 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
292 r = kvm_iommu_map_guest(kvm, match);
298 mutex_unlock(&kvm->lock);
301 list_del(&match->list);
302 pci_release_regions(dev);
304 pci_disable_device(dev);
309 mutex_unlock(&kvm->lock);
314 static inline int valid_vcpu(int n)
316 return likely(n >= 0 && n < KVM_MAX_VCPUS);
319 inline int kvm_is_mmio_pfn(pfn_t pfn)
322 return PageReserved(pfn_to_page(pfn));
328 * Switches to specified vcpu, until a matching vcpu_put()
330 void vcpu_load(struct kvm_vcpu *vcpu)
334 mutex_lock(&vcpu->mutex);
336 preempt_notifier_register(&vcpu->preempt_notifier);
337 kvm_arch_vcpu_load(vcpu, cpu);
341 void vcpu_put(struct kvm_vcpu *vcpu)
344 kvm_arch_vcpu_put(vcpu);
345 preempt_notifier_unregister(&vcpu->preempt_notifier);
347 mutex_unlock(&vcpu->mutex);
350 static void ack_flush(void *_completed)
354 void kvm_flush_remote_tlbs(struct kvm *kvm)
358 struct kvm_vcpu *vcpu;
362 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
363 vcpu = kvm->vcpus[i];
366 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
369 if (cpu != -1 && cpu != me)
372 if (cpus_empty(cpus))
374 ++kvm->stat.remote_tlb_flush;
375 smp_call_function_mask(cpus, ack_flush, NULL, 1);
380 void kvm_reload_remote_mmus(struct kvm *kvm)
384 struct kvm_vcpu *vcpu;
388 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
389 vcpu = kvm->vcpus[i];
392 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
395 if (cpu != -1 && cpu != me)
398 if (cpus_empty(cpus))
400 smp_call_function_mask(cpus, ack_flush, NULL, 1);
406 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
411 mutex_init(&vcpu->mutex);
415 init_waitqueue_head(&vcpu->wq);
417 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
422 vcpu->run = page_address(page);
424 r = kvm_arch_vcpu_init(vcpu);
430 free_page((unsigned long)vcpu->run);
434 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
436 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
438 kvm_arch_vcpu_uninit(vcpu);
439 free_page((unsigned long)vcpu->run);
441 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
443 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
444 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
446 return container_of(mn, struct kvm, mmu_notifier);
449 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
450 struct mm_struct *mm,
451 unsigned long address)
453 struct kvm *kvm = mmu_notifier_to_kvm(mn);
457 * When ->invalidate_page runs, the linux pte has been zapped
458 * already but the page is still allocated until
459 * ->invalidate_page returns. So if we increase the sequence
460 * here the kvm page fault will notice if the spte can't be
461 * established because the page is going to be freed. If
462 * instead the kvm page fault establishes the spte before
463 * ->invalidate_page runs, kvm_unmap_hva will release it
466 * The sequence increase only need to be seen at spin_unlock
467 * time, and not at spin_lock time.
469 * Increasing the sequence after the spin_unlock would be
470 * unsafe because the kvm page fault could then establish the
471 * pte after kvm_unmap_hva returned, without noticing the page
472 * is going to be freed.
474 spin_lock(&kvm->mmu_lock);
475 kvm->mmu_notifier_seq++;
476 need_tlb_flush = kvm_unmap_hva(kvm, address);
477 spin_unlock(&kvm->mmu_lock);
479 /* we've to flush the tlb before the pages can be freed */
481 kvm_flush_remote_tlbs(kvm);
485 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
486 struct mm_struct *mm,
490 struct kvm *kvm = mmu_notifier_to_kvm(mn);
491 int need_tlb_flush = 0;
493 spin_lock(&kvm->mmu_lock);
495 * The count increase must become visible at unlock time as no
496 * spte can be established without taking the mmu_lock and
497 * count is also read inside the mmu_lock critical section.
499 kvm->mmu_notifier_count++;
500 for (; start < end; start += PAGE_SIZE)
501 need_tlb_flush |= kvm_unmap_hva(kvm, start);
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);
509 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
510 struct mm_struct *mm,
514 struct kvm *kvm = mmu_notifier_to_kvm(mn);
516 spin_lock(&kvm->mmu_lock);
518 * This sequence increase will notify the kvm page fault that
519 * the page that is going to be mapped in the spte could have
522 kvm->mmu_notifier_seq++;
524 * The above sequence increase must be visible before the
525 * below count decrease but both values are read by the kvm
526 * page fault under mmu_lock spinlock so we don't need to add
527 * a smb_wmb() here in between the two.
529 kvm->mmu_notifier_count--;
530 spin_unlock(&kvm->mmu_lock);
532 BUG_ON(kvm->mmu_notifier_count < 0);
535 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
536 struct mm_struct *mm,
537 unsigned long address)
539 struct kvm *kvm = mmu_notifier_to_kvm(mn);
542 spin_lock(&kvm->mmu_lock);
543 young = kvm_age_hva(kvm, address);
544 spin_unlock(&kvm->mmu_lock);
547 kvm_flush_remote_tlbs(kvm);
552 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
553 .invalidate_page = kvm_mmu_notifier_invalidate_page,
554 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
555 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
556 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
558 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
560 static struct kvm *kvm_create_vm(void)
562 struct kvm *kvm = kvm_arch_create_vm();
563 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
570 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
571 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
574 return ERR_PTR(-ENOMEM);
576 kvm->coalesced_mmio_ring =
577 (struct kvm_coalesced_mmio_ring *)page_address(page);
580 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
583 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
584 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
586 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
595 kvm->mm = current->mm;
596 atomic_inc(&kvm->mm->mm_count);
597 spin_lock_init(&kvm->mmu_lock);
598 kvm_io_bus_init(&kvm->pio_bus);
599 mutex_init(&kvm->lock);
600 kvm_io_bus_init(&kvm->mmio_bus);
601 init_rwsem(&kvm->slots_lock);
602 atomic_set(&kvm->users_count, 1);
603 spin_lock(&kvm_lock);
604 list_add(&kvm->vm_list, &vm_list);
605 spin_unlock(&kvm_lock);
606 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
607 kvm_coalesced_mmio_init(kvm);
614 * Free any memory in @free but not in @dont.
616 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
617 struct kvm_memory_slot *dont)
619 if (!dont || free->rmap != dont->rmap)
622 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
623 vfree(free->dirty_bitmap);
625 if (!dont || free->lpage_info != dont->lpage_info)
626 vfree(free->lpage_info);
629 free->dirty_bitmap = NULL;
631 free->lpage_info = NULL;
634 void kvm_free_physmem(struct kvm *kvm)
638 for (i = 0; i < kvm->nmemslots; ++i)
639 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
642 static void kvm_destroy_vm(struct kvm *kvm)
644 struct mm_struct *mm = kvm->mm;
646 spin_lock(&kvm_lock);
647 list_del(&kvm->vm_list);
648 spin_unlock(&kvm_lock);
649 kvm_io_bus_destroy(&kvm->pio_bus);
650 kvm_io_bus_destroy(&kvm->mmio_bus);
651 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
652 if (kvm->coalesced_mmio_ring != NULL)
653 free_page((unsigned long)kvm->coalesced_mmio_ring);
655 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
656 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
658 kvm_arch_destroy_vm(kvm);
662 void kvm_get_kvm(struct kvm *kvm)
664 atomic_inc(&kvm->users_count);
666 EXPORT_SYMBOL_GPL(kvm_get_kvm);
668 void kvm_put_kvm(struct kvm *kvm)
670 if (atomic_dec_and_test(&kvm->users_count))
673 EXPORT_SYMBOL_GPL(kvm_put_kvm);
676 static int kvm_vm_release(struct inode *inode, struct file *filp)
678 struct kvm *kvm = filp->private_data;
685 * Allocate some memory and give it an address in the guest physical address
688 * Discontiguous memory is allowed, mostly for framebuffers.
690 * Must be called holding mmap_sem for write.
692 int __kvm_set_memory_region(struct kvm *kvm,
693 struct kvm_userspace_memory_region *mem,
698 unsigned long npages;
700 struct kvm_memory_slot *memslot;
701 struct kvm_memory_slot old, new;
704 /* General sanity checks */
705 if (mem->memory_size & (PAGE_SIZE - 1))
707 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
709 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
711 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
714 memslot = &kvm->memslots[mem->slot];
715 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
716 npages = mem->memory_size >> PAGE_SHIFT;
719 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
721 new = old = *memslot;
723 new.base_gfn = base_gfn;
725 new.flags = mem->flags;
727 /* Disallow changing a memory slot's size. */
729 if (npages && old.npages && npages != old.npages)
732 /* Check for overlaps */
734 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
735 struct kvm_memory_slot *s = &kvm->memslots[i];
739 if (!((base_gfn + npages <= s->base_gfn) ||
740 (base_gfn >= s->base_gfn + s->npages)))
744 /* Free page dirty bitmap if unneeded */
745 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
746 new.dirty_bitmap = NULL;
750 /* Allocate if a slot is being created */
752 if (npages && !new.rmap) {
753 new.rmap = vmalloc(npages * sizeof(struct page *));
758 memset(new.rmap, 0, npages * sizeof(*new.rmap));
760 new.user_alloc = user_alloc;
762 * hva_to_rmmap() serialzies with the mmu_lock and to be
763 * safe it has to ignore memslots with !user_alloc &&
767 new.userspace_addr = mem->userspace_addr;
769 new.userspace_addr = 0;
771 if (npages && !new.lpage_info) {
772 int largepages = npages / KVM_PAGES_PER_HPAGE;
773 if (npages % KVM_PAGES_PER_HPAGE)
775 if (base_gfn % KVM_PAGES_PER_HPAGE)
778 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
783 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
785 if (base_gfn % KVM_PAGES_PER_HPAGE)
786 new.lpage_info[0].write_count = 1;
787 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
788 new.lpage_info[largepages-1].write_count = 1;
791 /* Allocate page dirty bitmap if needed */
792 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
793 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
795 new.dirty_bitmap = vmalloc(dirty_bytes);
796 if (!new.dirty_bitmap)
798 memset(new.dirty_bitmap, 0, dirty_bytes);
800 #endif /* not defined CONFIG_S390 */
803 kvm_arch_flush_shadow(kvm);
805 spin_lock(&kvm->mmu_lock);
806 if (mem->slot >= kvm->nmemslots)
807 kvm->nmemslots = mem->slot + 1;
810 spin_unlock(&kvm->mmu_lock);
812 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
814 spin_lock(&kvm->mmu_lock);
816 spin_unlock(&kvm->mmu_lock);
820 kvm_free_physmem_slot(&old, &new);
822 /* map the pages in iommu page table */
823 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
830 kvm_free_physmem_slot(&new, &old);
835 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
837 int kvm_set_memory_region(struct kvm *kvm,
838 struct kvm_userspace_memory_region *mem,
843 down_write(&kvm->slots_lock);
844 r = __kvm_set_memory_region(kvm, mem, user_alloc);
845 up_write(&kvm->slots_lock);
848 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
850 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
852 kvm_userspace_memory_region *mem,
855 if (mem->slot >= KVM_MEMORY_SLOTS)
857 return kvm_set_memory_region(kvm, mem, user_alloc);
860 int kvm_get_dirty_log(struct kvm *kvm,
861 struct kvm_dirty_log *log, int *is_dirty)
863 struct kvm_memory_slot *memslot;
866 unsigned long any = 0;
869 if (log->slot >= KVM_MEMORY_SLOTS)
872 memslot = &kvm->memslots[log->slot];
874 if (!memslot->dirty_bitmap)
877 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
879 for (i = 0; !any && i < n/sizeof(long); ++i)
880 any = memslot->dirty_bitmap[i];
883 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
894 int is_error_page(struct page *page)
896 return page == bad_page;
898 EXPORT_SYMBOL_GPL(is_error_page);
900 int is_error_pfn(pfn_t pfn)
902 return pfn == bad_pfn;
904 EXPORT_SYMBOL_GPL(is_error_pfn);
906 static inline unsigned long bad_hva(void)
911 int kvm_is_error_hva(unsigned long addr)
913 return addr == bad_hva();
915 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
917 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
921 for (i = 0; i < kvm->nmemslots; ++i) {
922 struct kvm_memory_slot *memslot = &kvm->memslots[i];
924 if (gfn >= memslot->base_gfn
925 && gfn < memslot->base_gfn + memslot->npages)
931 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
933 gfn = unalias_gfn(kvm, gfn);
934 return __gfn_to_memslot(kvm, gfn);
937 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
941 gfn = unalias_gfn(kvm, gfn);
942 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
943 struct kvm_memory_slot *memslot = &kvm->memslots[i];
945 if (gfn >= memslot->base_gfn
946 && gfn < memslot->base_gfn + memslot->npages)
951 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
953 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
955 struct kvm_memory_slot *slot;
957 gfn = unalias_gfn(kvm, gfn);
958 slot = __gfn_to_memslot(kvm, gfn);
961 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
963 EXPORT_SYMBOL_GPL(gfn_to_hva);
965 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
967 struct page *page[1];
974 addr = gfn_to_hva(kvm, gfn);
975 if (kvm_is_error_hva(addr)) {
977 return page_to_pfn(bad_page);
980 npages = get_user_pages_fast(addr, 1, 1, page);
982 if (unlikely(npages != 1)) {
983 struct vm_area_struct *vma;
985 down_read(¤t->mm->mmap_sem);
986 vma = find_vma(current->mm, addr);
988 if (vma == NULL || addr < vma->vm_start ||
989 !(vma->vm_flags & VM_PFNMAP)) {
990 up_read(¤t->mm->mmap_sem);
992 return page_to_pfn(bad_page);
995 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
996 up_read(¤t->mm->mmap_sem);
997 BUG_ON(!kvm_is_mmio_pfn(pfn));
999 pfn = page_to_pfn(page[0]);
1004 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1006 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1010 pfn = gfn_to_pfn(kvm, gfn);
1011 if (!kvm_is_mmio_pfn(pfn))
1012 return pfn_to_page(pfn);
1014 WARN_ON(kvm_is_mmio_pfn(pfn));
1020 EXPORT_SYMBOL_GPL(gfn_to_page);
1022 void kvm_release_page_clean(struct page *page)
1024 kvm_release_pfn_clean(page_to_pfn(page));
1026 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1028 void kvm_release_pfn_clean(pfn_t pfn)
1030 if (!kvm_is_mmio_pfn(pfn))
1031 put_page(pfn_to_page(pfn));
1033 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1035 void kvm_release_page_dirty(struct page *page)
1037 kvm_release_pfn_dirty(page_to_pfn(page));
1039 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1041 void kvm_release_pfn_dirty(pfn_t pfn)
1043 kvm_set_pfn_dirty(pfn);
1044 kvm_release_pfn_clean(pfn);
1046 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1048 void kvm_set_page_dirty(struct page *page)
1050 kvm_set_pfn_dirty(page_to_pfn(page));
1052 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1054 void kvm_set_pfn_dirty(pfn_t pfn)
1056 if (!kvm_is_mmio_pfn(pfn)) {
1057 struct page *page = pfn_to_page(pfn);
1058 if (!PageReserved(page))
1062 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1064 void kvm_set_pfn_accessed(pfn_t pfn)
1066 if (!kvm_is_mmio_pfn(pfn))
1067 mark_page_accessed(pfn_to_page(pfn));
1069 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1071 void kvm_get_pfn(pfn_t pfn)
1073 if (!kvm_is_mmio_pfn(pfn))
1074 get_page(pfn_to_page(pfn));
1076 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1078 static int next_segment(unsigned long len, int offset)
1080 if (len > PAGE_SIZE - offset)
1081 return PAGE_SIZE - offset;
1086 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1092 addr = gfn_to_hva(kvm, gfn);
1093 if (kvm_is_error_hva(addr))
1095 r = copy_from_user(data, (void __user *)addr + offset, len);
1100 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1102 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1104 gfn_t gfn = gpa >> PAGE_SHIFT;
1106 int offset = offset_in_page(gpa);
1109 while ((seg = next_segment(len, offset)) != 0) {
1110 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1120 EXPORT_SYMBOL_GPL(kvm_read_guest);
1122 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1127 gfn_t gfn = gpa >> PAGE_SHIFT;
1128 int offset = offset_in_page(gpa);
1130 addr = gfn_to_hva(kvm, gfn);
1131 if (kvm_is_error_hva(addr))
1133 pagefault_disable();
1134 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1140 EXPORT_SYMBOL(kvm_read_guest_atomic);
1142 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1143 int offset, int len)
1148 addr = gfn_to_hva(kvm, gfn);
1149 if (kvm_is_error_hva(addr))
1151 r = copy_to_user((void __user *)addr + offset, data, len);
1154 mark_page_dirty(kvm, gfn);
1157 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1159 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1162 gfn_t gfn = gpa >> PAGE_SHIFT;
1164 int offset = offset_in_page(gpa);
1167 while ((seg = next_segment(len, offset)) != 0) {
1168 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1179 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1181 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1183 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1185 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1187 gfn_t gfn = gpa >> PAGE_SHIFT;
1189 int offset = offset_in_page(gpa);
1192 while ((seg = next_segment(len, offset)) != 0) {
1193 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1202 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1204 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1206 struct kvm_memory_slot *memslot;
1208 gfn = unalias_gfn(kvm, gfn);
1209 memslot = __gfn_to_memslot(kvm, gfn);
1210 if (memslot && memslot->dirty_bitmap) {
1211 unsigned long rel_gfn = gfn - memslot->base_gfn;
1214 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1215 set_bit(rel_gfn, memslot->dirty_bitmap);
1220 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1222 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1227 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1229 if (kvm_cpu_has_interrupt(vcpu) ||
1230 kvm_cpu_has_pending_timer(vcpu) ||
1231 kvm_arch_vcpu_runnable(vcpu)) {
1232 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1235 if (signal_pending(current))
1243 finish_wait(&vcpu->wq, &wait);
1246 void kvm_resched(struct kvm_vcpu *vcpu)
1248 if (!need_resched())
1252 EXPORT_SYMBOL_GPL(kvm_resched);
1254 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1256 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1259 if (vmf->pgoff == 0)
1260 page = virt_to_page(vcpu->run);
1262 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1263 page = virt_to_page(vcpu->arch.pio_data);
1265 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1266 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1267 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1270 return VM_FAULT_SIGBUS;
1276 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1277 .fault = kvm_vcpu_fault,
1280 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1282 vma->vm_ops = &kvm_vcpu_vm_ops;
1286 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1288 struct kvm_vcpu *vcpu = filp->private_data;
1290 kvm_put_kvm(vcpu->kvm);
1294 static const struct file_operations kvm_vcpu_fops = {
1295 .release = kvm_vcpu_release,
1296 .unlocked_ioctl = kvm_vcpu_ioctl,
1297 .compat_ioctl = kvm_vcpu_ioctl,
1298 .mmap = kvm_vcpu_mmap,
1302 * Allocates an inode for the vcpu.
1304 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1306 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1308 kvm_put_kvm(vcpu->kvm);
1313 * Creates some virtual cpus. Good luck creating more than one.
1315 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1318 struct kvm_vcpu *vcpu;
1323 vcpu = kvm_arch_vcpu_create(kvm, n);
1325 return PTR_ERR(vcpu);
1327 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1329 r = kvm_arch_vcpu_setup(vcpu);
1333 mutex_lock(&kvm->lock);
1334 if (kvm->vcpus[n]) {
1338 kvm->vcpus[n] = vcpu;
1339 mutex_unlock(&kvm->lock);
1341 /* Now it's all set up, let userspace reach it */
1343 r = create_vcpu_fd(vcpu);
1349 mutex_lock(&kvm->lock);
1350 kvm->vcpus[n] = NULL;
1352 mutex_unlock(&kvm->lock);
1353 kvm_arch_vcpu_destroy(vcpu);
1357 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1360 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1361 vcpu->sigset_active = 1;
1362 vcpu->sigset = *sigset;
1364 vcpu->sigset_active = 0;
1368 static long kvm_vcpu_ioctl(struct file *filp,
1369 unsigned int ioctl, unsigned long arg)
1371 struct kvm_vcpu *vcpu = filp->private_data;
1372 void __user *argp = (void __user *)arg;
1374 struct kvm_fpu *fpu = NULL;
1375 struct kvm_sregs *kvm_sregs = NULL;
1377 if (vcpu->kvm->mm != current->mm)
1384 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1386 case KVM_GET_REGS: {
1387 struct kvm_regs *kvm_regs;
1390 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1393 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1397 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1404 case KVM_SET_REGS: {
1405 struct kvm_regs *kvm_regs;
1408 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1412 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1414 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1422 case KVM_GET_SREGS: {
1423 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1427 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1431 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1436 case KVM_SET_SREGS: {
1437 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1442 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1444 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1450 case KVM_GET_MP_STATE: {
1451 struct kvm_mp_state mp_state;
1453 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1457 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1462 case KVM_SET_MP_STATE: {
1463 struct kvm_mp_state mp_state;
1466 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1468 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1474 case KVM_TRANSLATE: {
1475 struct kvm_translation tr;
1478 if (copy_from_user(&tr, argp, sizeof tr))
1480 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1484 if (copy_to_user(argp, &tr, sizeof tr))
1489 case KVM_DEBUG_GUEST: {
1490 struct kvm_debug_guest dbg;
1493 if (copy_from_user(&dbg, argp, sizeof dbg))
1495 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1501 case KVM_SET_SIGNAL_MASK: {
1502 struct kvm_signal_mask __user *sigmask_arg = argp;
1503 struct kvm_signal_mask kvm_sigmask;
1504 sigset_t sigset, *p;
1509 if (copy_from_user(&kvm_sigmask, argp,
1510 sizeof kvm_sigmask))
1513 if (kvm_sigmask.len != sizeof sigset)
1516 if (copy_from_user(&sigset, sigmask_arg->sigset,
1521 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1525 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1529 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1533 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1539 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1544 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1546 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1553 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1561 static long kvm_vm_ioctl(struct file *filp,
1562 unsigned int ioctl, unsigned long arg)
1564 struct kvm *kvm = filp->private_data;
1565 void __user *argp = (void __user *)arg;
1568 if (kvm->mm != current->mm)
1571 case KVM_CREATE_VCPU:
1572 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1576 case KVM_SET_USER_MEMORY_REGION: {
1577 struct kvm_userspace_memory_region kvm_userspace_mem;
1580 if (copy_from_user(&kvm_userspace_mem, argp,
1581 sizeof kvm_userspace_mem))
1584 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1589 case KVM_GET_DIRTY_LOG: {
1590 struct kvm_dirty_log log;
1593 if (copy_from_user(&log, argp, sizeof log))
1595 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1600 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1601 case KVM_REGISTER_COALESCED_MMIO: {
1602 struct kvm_coalesced_mmio_zone zone;
1604 if (copy_from_user(&zone, argp, sizeof zone))
1607 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1613 case KVM_UNREGISTER_COALESCED_MMIO: {
1614 struct kvm_coalesced_mmio_zone zone;
1616 if (copy_from_user(&zone, argp, sizeof zone))
1619 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1626 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1627 case KVM_ASSIGN_PCI_DEVICE: {
1628 struct kvm_assigned_pci_dev assigned_dev;
1631 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1633 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1638 case KVM_ASSIGN_IRQ: {
1639 struct kvm_assigned_irq assigned_irq;
1642 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1644 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1651 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1657 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1659 struct page *page[1];
1662 gfn_t gfn = vmf->pgoff;
1663 struct kvm *kvm = vma->vm_file->private_data;
1665 addr = gfn_to_hva(kvm, gfn);
1666 if (kvm_is_error_hva(addr))
1667 return VM_FAULT_SIGBUS;
1669 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1671 if (unlikely(npages != 1))
1672 return VM_FAULT_SIGBUS;
1674 vmf->page = page[0];
1678 static struct vm_operations_struct kvm_vm_vm_ops = {
1679 .fault = kvm_vm_fault,
1682 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1684 vma->vm_ops = &kvm_vm_vm_ops;
1688 static const struct file_operations kvm_vm_fops = {
1689 .release = kvm_vm_release,
1690 .unlocked_ioctl = kvm_vm_ioctl,
1691 .compat_ioctl = kvm_vm_ioctl,
1692 .mmap = kvm_vm_mmap,
1695 static int kvm_dev_ioctl_create_vm(void)
1700 kvm = kvm_create_vm();
1702 return PTR_ERR(kvm);
1703 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1710 static long kvm_dev_ioctl(struct file *filp,
1711 unsigned int ioctl, unsigned long arg)
1716 case KVM_GET_API_VERSION:
1720 r = KVM_API_VERSION;
1726 r = kvm_dev_ioctl_create_vm();
1728 case KVM_CHECK_EXTENSION:
1729 r = kvm_dev_ioctl_check_extension(arg);
1731 case KVM_GET_VCPU_MMAP_SIZE:
1735 r = PAGE_SIZE; /* struct kvm_run */
1737 r += PAGE_SIZE; /* pio data page */
1739 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1740 r += PAGE_SIZE; /* coalesced mmio ring page */
1743 case KVM_TRACE_ENABLE:
1744 case KVM_TRACE_PAUSE:
1745 case KVM_TRACE_DISABLE:
1746 r = kvm_trace_ioctl(ioctl, arg);
1749 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1755 static struct file_operations kvm_chardev_ops = {
1756 .unlocked_ioctl = kvm_dev_ioctl,
1757 .compat_ioctl = kvm_dev_ioctl,
1760 static struct miscdevice kvm_dev = {
1766 static void hardware_enable(void *junk)
1768 int cpu = raw_smp_processor_id();
1770 if (cpu_isset(cpu, cpus_hardware_enabled))
1772 cpu_set(cpu, cpus_hardware_enabled);
1773 kvm_arch_hardware_enable(NULL);
1776 static void hardware_disable(void *junk)
1778 int cpu = raw_smp_processor_id();
1780 if (!cpu_isset(cpu, cpus_hardware_enabled))
1782 cpu_clear(cpu, cpus_hardware_enabled);
1783 kvm_arch_hardware_disable(NULL);
1786 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1791 val &= ~CPU_TASKS_FROZEN;
1794 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1796 hardware_disable(NULL);
1798 case CPU_UP_CANCELED:
1799 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1801 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1804 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1806 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1813 asmlinkage void kvm_handle_fault_on_reboot(void)
1816 /* spin while reset goes on */
1819 /* Fault while not rebooting. We want the trace. */
1822 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1824 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1827 if (val == SYS_RESTART) {
1829 * Some (well, at least mine) BIOSes hang on reboot if
1832 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1833 kvm_rebooting = true;
1834 on_each_cpu(hardware_disable, NULL, 1);
1839 static struct notifier_block kvm_reboot_notifier = {
1840 .notifier_call = kvm_reboot,
1844 void kvm_io_bus_init(struct kvm_io_bus *bus)
1846 memset(bus, 0, sizeof(*bus));
1849 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1853 for (i = 0; i < bus->dev_count; i++) {
1854 struct kvm_io_device *pos = bus->devs[i];
1856 kvm_iodevice_destructor(pos);
1860 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1861 gpa_t addr, int len, int is_write)
1865 for (i = 0; i < bus->dev_count; i++) {
1866 struct kvm_io_device *pos = bus->devs[i];
1868 if (pos->in_range(pos, addr, len, is_write))
1875 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1877 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1879 bus->devs[bus->dev_count++] = dev;
1882 static struct notifier_block kvm_cpu_notifier = {
1883 .notifier_call = kvm_cpu_hotplug,
1884 .priority = 20, /* must be > scheduler priority */
1887 static int vm_stat_get(void *_offset, u64 *val)
1889 unsigned offset = (long)_offset;
1893 spin_lock(&kvm_lock);
1894 list_for_each_entry(kvm, &vm_list, vm_list)
1895 *val += *(u32 *)((void *)kvm + offset);
1896 spin_unlock(&kvm_lock);
1900 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1902 static int vcpu_stat_get(void *_offset, u64 *val)
1904 unsigned offset = (long)_offset;
1906 struct kvm_vcpu *vcpu;
1910 spin_lock(&kvm_lock);
1911 list_for_each_entry(kvm, &vm_list, vm_list)
1912 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1913 vcpu = kvm->vcpus[i];
1915 *val += *(u32 *)((void *)vcpu + offset);
1917 spin_unlock(&kvm_lock);
1921 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1923 static struct file_operations *stat_fops[] = {
1924 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1925 [KVM_STAT_VM] = &vm_stat_fops,
1928 static void kvm_init_debug(void)
1930 struct kvm_stats_debugfs_item *p;
1932 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
1933 for (p = debugfs_entries; p->name; ++p)
1934 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1935 (void *)(long)p->offset,
1936 stat_fops[p->kind]);
1939 static void kvm_exit_debug(void)
1941 struct kvm_stats_debugfs_item *p;
1943 for (p = debugfs_entries; p->name; ++p)
1944 debugfs_remove(p->dentry);
1945 debugfs_remove(kvm_debugfs_dir);
1948 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1950 hardware_disable(NULL);
1954 static int kvm_resume(struct sys_device *dev)
1956 hardware_enable(NULL);
1960 static struct sysdev_class kvm_sysdev_class = {
1962 .suspend = kvm_suspend,
1963 .resume = kvm_resume,
1966 static struct sys_device kvm_sysdev = {
1968 .cls = &kvm_sysdev_class,
1971 struct page *bad_page;
1975 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1977 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1980 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1982 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1984 kvm_arch_vcpu_load(vcpu, cpu);
1987 static void kvm_sched_out(struct preempt_notifier *pn,
1988 struct task_struct *next)
1990 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1992 kvm_arch_vcpu_put(vcpu);
1995 int kvm_init(void *opaque, unsigned int vcpu_size,
1996 struct module *module)
2003 r = kvm_arch_init(opaque);
2007 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2009 if (bad_page == NULL) {
2014 bad_pfn = page_to_pfn(bad_page);
2016 r = kvm_arch_hardware_setup();
2020 for_each_online_cpu(cpu) {
2021 smp_call_function_single(cpu,
2022 kvm_arch_check_processor_compat,
2028 on_each_cpu(hardware_enable, NULL, 1);
2029 r = register_cpu_notifier(&kvm_cpu_notifier);
2032 register_reboot_notifier(&kvm_reboot_notifier);
2034 r = sysdev_class_register(&kvm_sysdev_class);
2038 r = sysdev_register(&kvm_sysdev);
2042 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2043 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2044 __alignof__(struct kvm_vcpu),
2046 if (!kvm_vcpu_cache) {
2051 kvm_chardev_ops.owner = module;
2053 r = misc_register(&kvm_dev);
2055 printk(KERN_ERR "kvm: misc device register failed\n");
2059 kvm_preempt_ops.sched_in = kvm_sched_in;
2060 kvm_preempt_ops.sched_out = kvm_sched_out;
2065 kmem_cache_destroy(kvm_vcpu_cache);
2067 sysdev_unregister(&kvm_sysdev);
2069 sysdev_class_unregister(&kvm_sysdev_class);
2071 unregister_reboot_notifier(&kvm_reboot_notifier);
2072 unregister_cpu_notifier(&kvm_cpu_notifier);
2074 on_each_cpu(hardware_disable, NULL, 1);
2076 kvm_arch_hardware_unsetup();
2078 __free_page(bad_page);
2085 EXPORT_SYMBOL_GPL(kvm_init);
2089 kvm_trace_cleanup();
2090 misc_deregister(&kvm_dev);
2091 kmem_cache_destroy(kvm_vcpu_cache);
2092 sysdev_unregister(&kvm_sysdev);
2093 sysdev_class_unregister(&kvm_sysdev_class);
2094 unregister_reboot_notifier(&kvm_reboot_notifier);
2095 unregister_cpu_notifier(&kvm_cpu_notifier);
2096 on_each_cpu(hardware_disable, NULL, 1);
2097 kvm_arch_hardware_unsetup();
2100 __free_page(bad_page);
2102 EXPORT_SYMBOL_GPL(kvm_exit);