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 kvm_vm_ioctl_assign_irq(struct kvm *kvm,
180 struct kvm_assigned_irq
184 struct kvm_assigned_dev_kernel *match;
186 mutex_lock(&kvm->lock);
188 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
189 assigned_irq->assigned_dev_id);
191 mutex_unlock(&kvm->lock);
195 if (match->irq_requested) {
196 match->guest_irq = assigned_irq->guest_irq;
197 match->ack_notifier.gsi = assigned_irq->guest_irq;
198 mutex_unlock(&kvm->lock);
202 INIT_WORK(&match->interrupt_work,
203 kvm_assigned_dev_interrupt_work_handler);
205 if (irqchip_in_kernel(kvm)) {
206 if (!capable(CAP_SYS_RAWIO)) {
211 if (assigned_irq->host_irq)
212 match->host_irq = assigned_irq->host_irq;
214 match->host_irq = match->dev->irq;
215 match->guest_irq = assigned_irq->guest_irq;
216 match->ack_notifier.gsi = assigned_irq->guest_irq;
217 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
218 kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
219 r = kvm_request_irq_source_id(kvm);
223 match->irq_source_id = r;
225 /* Even though this is PCI, we don't want to use shared
226 * interrupts. Sharing host devices with guest-assigned devices
227 * on the same interrupt line is not a happy situation: there
228 * are going to be long delays in accepting, acking, etc.
230 if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
231 "kvm_assigned_device", (void *)match)) {
237 match->irq_requested = true;
238 mutex_unlock(&kvm->lock);
241 mutex_unlock(&kvm->lock);
242 kvm_free_assigned_device(kvm, match);
246 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
247 struct kvm_assigned_pci_dev *assigned_dev)
250 struct kvm_assigned_dev_kernel *match;
253 mutex_lock(&kvm->lock);
255 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
256 assigned_dev->assigned_dev_id);
258 /* device already assigned */
263 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
265 printk(KERN_INFO "%s: Couldn't allocate memory\n",
270 dev = pci_get_bus_and_slot(assigned_dev->busnr,
271 assigned_dev->devfn);
273 printk(KERN_INFO "%s: host device not found\n", __func__);
277 if (pci_enable_device(dev)) {
278 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
282 r = pci_request_regions(dev, "kvm_assigned_device");
284 printk(KERN_INFO "%s: Could not get access to device regions\n",
289 pci_reset_function(dev);
291 match->assigned_dev_id = assigned_dev->assigned_dev_id;
292 match->host_busnr = assigned_dev->busnr;
293 match->host_devfn = assigned_dev->devfn;
298 list_add(&match->list, &kvm->arch.assigned_dev_head);
300 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
301 r = kvm_iommu_map_guest(kvm, match);
307 mutex_unlock(&kvm->lock);
310 list_del(&match->list);
311 pci_release_regions(dev);
313 pci_disable_device(dev);
318 mutex_unlock(&kvm->lock);
323 static inline int valid_vcpu(int n)
325 return likely(n >= 0 && n < KVM_MAX_VCPUS);
328 inline int kvm_is_mmio_pfn(pfn_t pfn)
331 return PageReserved(pfn_to_page(pfn));
337 * Switches to specified vcpu, until a matching vcpu_put()
339 void vcpu_load(struct kvm_vcpu *vcpu)
343 mutex_lock(&vcpu->mutex);
345 preempt_notifier_register(&vcpu->preempt_notifier);
346 kvm_arch_vcpu_load(vcpu, cpu);
350 void vcpu_put(struct kvm_vcpu *vcpu)
353 kvm_arch_vcpu_put(vcpu);
354 preempt_notifier_unregister(&vcpu->preempt_notifier);
356 mutex_unlock(&vcpu->mutex);
359 static void ack_flush(void *_completed)
363 void kvm_flush_remote_tlbs(struct kvm *kvm)
367 struct kvm_vcpu *vcpu;
371 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
372 vcpu = kvm->vcpus[i];
375 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
378 if (cpu != -1 && cpu != me)
381 if (cpus_empty(cpus))
383 ++kvm->stat.remote_tlb_flush;
384 smp_call_function_mask(cpus, ack_flush, NULL, 1);
389 void kvm_reload_remote_mmus(struct kvm *kvm)
393 struct kvm_vcpu *vcpu;
397 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
398 vcpu = kvm->vcpus[i];
401 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
404 if (cpu != -1 && cpu != me)
407 if (cpus_empty(cpus))
409 smp_call_function_mask(cpus, ack_flush, NULL, 1);
415 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
420 mutex_init(&vcpu->mutex);
424 init_waitqueue_head(&vcpu->wq);
426 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
431 vcpu->run = page_address(page);
433 r = kvm_arch_vcpu_init(vcpu);
439 free_page((unsigned long)vcpu->run);
443 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
445 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
447 kvm_arch_vcpu_uninit(vcpu);
448 free_page((unsigned long)vcpu->run);
450 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
452 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
453 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
455 return container_of(mn, struct kvm, mmu_notifier);
458 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
459 struct mm_struct *mm,
460 unsigned long address)
462 struct kvm *kvm = mmu_notifier_to_kvm(mn);
466 * When ->invalidate_page runs, the linux pte has been zapped
467 * already but the page is still allocated until
468 * ->invalidate_page returns. So if we increase the sequence
469 * here the kvm page fault will notice if the spte can't be
470 * established because the page is going to be freed. If
471 * instead the kvm page fault establishes the spte before
472 * ->invalidate_page runs, kvm_unmap_hva will release it
475 * The sequence increase only need to be seen at spin_unlock
476 * time, and not at spin_lock time.
478 * Increasing the sequence after the spin_unlock would be
479 * unsafe because the kvm page fault could then establish the
480 * pte after kvm_unmap_hva returned, without noticing the page
481 * is going to be freed.
483 spin_lock(&kvm->mmu_lock);
484 kvm->mmu_notifier_seq++;
485 need_tlb_flush = kvm_unmap_hva(kvm, address);
486 spin_unlock(&kvm->mmu_lock);
488 /* we've to flush the tlb before the pages can be freed */
490 kvm_flush_remote_tlbs(kvm);
494 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
495 struct mm_struct *mm,
499 struct kvm *kvm = mmu_notifier_to_kvm(mn);
500 int need_tlb_flush = 0;
502 spin_lock(&kvm->mmu_lock);
504 * The count increase must become visible at unlock time as no
505 * spte can be established without taking the mmu_lock and
506 * count is also read inside the mmu_lock critical section.
508 kvm->mmu_notifier_count++;
509 for (; start < end; start += PAGE_SIZE)
510 need_tlb_flush |= kvm_unmap_hva(kvm, start);
511 spin_unlock(&kvm->mmu_lock);
513 /* we've to flush the tlb before the pages can be freed */
515 kvm_flush_remote_tlbs(kvm);
518 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
519 struct mm_struct *mm,
523 struct kvm *kvm = mmu_notifier_to_kvm(mn);
525 spin_lock(&kvm->mmu_lock);
527 * This sequence increase will notify the kvm page fault that
528 * the page that is going to be mapped in the spte could have
531 kvm->mmu_notifier_seq++;
533 * The above sequence increase must be visible before the
534 * below count decrease but both values are read by the kvm
535 * page fault under mmu_lock spinlock so we don't need to add
536 * a smb_wmb() here in between the two.
538 kvm->mmu_notifier_count--;
539 spin_unlock(&kvm->mmu_lock);
541 BUG_ON(kvm->mmu_notifier_count < 0);
544 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
545 struct mm_struct *mm,
546 unsigned long address)
548 struct kvm *kvm = mmu_notifier_to_kvm(mn);
551 spin_lock(&kvm->mmu_lock);
552 young = kvm_age_hva(kvm, address);
553 spin_unlock(&kvm->mmu_lock);
556 kvm_flush_remote_tlbs(kvm);
561 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
562 .invalidate_page = kvm_mmu_notifier_invalidate_page,
563 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
564 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
565 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
567 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
569 static struct kvm *kvm_create_vm(void)
571 struct kvm *kvm = kvm_arch_create_vm();
572 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
579 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
580 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
583 return ERR_PTR(-ENOMEM);
585 kvm->coalesced_mmio_ring =
586 (struct kvm_coalesced_mmio_ring *)page_address(page);
589 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
592 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
593 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
595 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
604 kvm->mm = current->mm;
605 atomic_inc(&kvm->mm->mm_count);
606 spin_lock_init(&kvm->mmu_lock);
607 kvm_io_bus_init(&kvm->pio_bus);
608 mutex_init(&kvm->lock);
609 kvm_io_bus_init(&kvm->mmio_bus);
610 init_rwsem(&kvm->slots_lock);
611 atomic_set(&kvm->users_count, 1);
612 spin_lock(&kvm_lock);
613 list_add(&kvm->vm_list, &vm_list);
614 spin_unlock(&kvm_lock);
615 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
616 kvm_coalesced_mmio_init(kvm);
623 * Free any memory in @free but not in @dont.
625 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
626 struct kvm_memory_slot *dont)
628 if (!dont || free->rmap != dont->rmap)
631 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
632 vfree(free->dirty_bitmap);
634 if (!dont || free->lpage_info != dont->lpage_info)
635 vfree(free->lpage_info);
638 free->dirty_bitmap = NULL;
640 free->lpage_info = NULL;
643 void kvm_free_physmem(struct kvm *kvm)
647 for (i = 0; i < kvm->nmemslots; ++i)
648 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
651 static void kvm_destroy_vm(struct kvm *kvm)
653 struct mm_struct *mm = kvm->mm;
655 spin_lock(&kvm_lock);
656 list_del(&kvm->vm_list);
657 spin_unlock(&kvm_lock);
658 kvm_io_bus_destroy(&kvm->pio_bus);
659 kvm_io_bus_destroy(&kvm->mmio_bus);
660 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
661 if (kvm->coalesced_mmio_ring != NULL)
662 free_page((unsigned long)kvm->coalesced_mmio_ring);
664 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
665 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
667 kvm_arch_destroy_vm(kvm);
671 void kvm_get_kvm(struct kvm *kvm)
673 atomic_inc(&kvm->users_count);
675 EXPORT_SYMBOL_GPL(kvm_get_kvm);
677 void kvm_put_kvm(struct kvm *kvm)
679 if (atomic_dec_and_test(&kvm->users_count))
682 EXPORT_SYMBOL_GPL(kvm_put_kvm);
685 static int kvm_vm_release(struct inode *inode, struct file *filp)
687 struct kvm *kvm = filp->private_data;
694 * Allocate some memory and give it an address in the guest physical address
697 * Discontiguous memory is allowed, mostly for framebuffers.
699 * Must be called holding mmap_sem for write.
701 int __kvm_set_memory_region(struct kvm *kvm,
702 struct kvm_userspace_memory_region *mem,
707 unsigned long npages;
709 struct kvm_memory_slot *memslot;
710 struct kvm_memory_slot old, new;
713 /* General sanity checks */
714 if (mem->memory_size & (PAGE_SIZE - 1))
716 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
718 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
720 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
723 memslot = &kvm->memslots[mem->slot];
724 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
725 npages = mem->memory_size >> PAGE_SHIFT;
728 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
730 new = old = *memslot;
732 new.base_gfn = base_gfn;
734 new.flags = mem->flags;
736 /* Disallow changing a memory slot's size. */
738 if (npages && old.npages && npages != old.npages)
741 /* Check for overlaps */
743 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
744 struct kvm_memory_slot *s = &kvm->memslots[i];
748 if (!((base_gfn + npages <= s->base_gfn) ||
749 (base_gfn >= s->base_gfn + s->npages)))
753 /* Free page dirty bitmap if unneeded */
754 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
755 new.dirty_bitmap = NULL;
759 /* Allocate if a slot is being created */
761 if (npages && !new.rmap) {
762 new.rmap = vmalloc(npages * sizeof(struct page *));
767 memset(new.rmap, 0, npages * sizeof(*new.rmap));
769 new.user_alloc = user_alloc;
771 * hva_to_rmmap() serialzies with the mmu_lock and to be
772 * safe it has to ignore memslots with !user_alloc &&
776 new.userspace_addr = mem->userspace_addr;
778 new.userspace_addr = 0;
780 if (npages && !new.lpage_info) {
781 int largepages = npages / KVM_PAGES_PER_HPAGE;
782 if (npages % KVM_PAGES_PER_HPAGE)
784 if (base_gfn % KVM_PAGES_PER_HPAGE)
787 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
792 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
794 if (base_gfn % KVM_PAGES_PER_HPAGE)
795 new.lpage_info[0].write_count = 1;
796 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
797 new.lpage_info[largepages-1].write_count = 1;
800 /* Allocate page dirty bitmap if needed */
801 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
802 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
804 new.dirty_bitmap = vmalloc(dirty_bytes);
805 if (!new.dirty_bitmap)
807 memset(new.dirty_bitmap, 0, dirty_bytes);
809 #endif /* not defined CONFIG_S390 */
812 kvm_arch_flush_shadow(kvm);
814 spin_lock(&kvm->mmu_lock);
815 if (mem->slot >= kvm->nmemslots)
816 kvm->nmemslots = mem->slot + 1;
819 spin_unlock(&kvm->mmu_lock);
821 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
823 spin_lock(&kvm->mmu_lock);
825 spin_unlock(&kvm->mmu_lock);
829 kvm_free_physmem_slot(&old, &new);
831 /* map the pages in iommu page table */
832 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
839 kvm_free_physmem_slot(&new, &old);
844 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
846 int kvm_set_memory_region(struct kvm *kvm,
847 struct kvm_userspace_memory_region *mem,
852 down_write(&kvm->slots_lock);
853 r = __kvm_set_memory_region(kvm, mem, user_alloc);
854 up_write(&kvm->slots_lock);
857 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
859 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
861 kvm_userspace_memory_region *mem,
864 if (mem->slot >= KVM_MEMORY_SLOTS)
866 return kvm_set_memory_region(kvm, mem, user_alloc);
869 int kvm_get_dirty_log(struct kvm *kvm,
870 struct kvm_dirty_log *log, int *is_dirty)
872 struct kvm_memory_slot *memslot;
875 unsigned long any = 0;
878 if (log->slot >= KVM_MEMORY_SLOTS)
881 memslot = &kvm->memslots[log->slot];
883 if (!memslot->dirty_bitmap)
886 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
888 for (i = 0; !any && i < n/sizeof(long); ++i)
889 any = memslot->dirty_bitmap[i];
892 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
903 int is_error_page(struct page *page)
905 return page == bad_page;
907 EXPORT_SYMBOL_GPL(is_error_page);
909 int is_error_pfn(pfn_t pfn)
911 return pfn == bad_pfn;
913 EXPORT_SYMBOL_GPL(is_error_pfn);
915 static inline unsigned long bad_hva(void)
920 int kvm_is_error_hva(unsigned long addr)
922 return addr == bad_hva();
924 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
926 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
930 for (i = 0; i < kvm->nmemslots; ++i) {
931 struct kvm_memory_slot *memslot = &kvm->memslots[i];
933 if (gfn >= memslot->base_gfn
934 && gfn < memslot->base_gfn + memslot->npages)
939 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
941 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
943 gfn = unalias_gfn(kvm, gfn);
944 return gfn_to_memslot_unaliased(kvm, gfn);
947 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
951 gfn = unalias_gfn(kvm, gfn);
952 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
953 struct kvm_memory_slot *memslot = &kvm->memslots[i];
955 if (gfn >= memslot->base_gfn
956 && gfn < memslot->base_gfn + memslot->npages)
961 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
963 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
965 struct kvm_memory_slot *slot;
967 gfn = unalias_gfn(kvm, gfn);
968 slot = gfn_to_memslot_unaliased(kvm, gfn);
971 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
973 EXPORT_SYMBOL_GPL(gfn_to_hva);
975 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
977 struct page *page[1];
984 addr = gfn_to_hva(kvm, gfn);
985 if (kvm_is_error_hva(addr)) {
987 return page_to_pfn(bad_page);
990 npages = get_user_pages_fast(addr, 1, 1, page);
992 if (unlikely(npages != 1)) {
993 struct vm_area_struct *vma;
995 down_read(¤t->mm->mmap_sem);
996 vma = find_vma(current->mm, addr);
998 if (vma == NULL || addr < vma->vm_start ||
999 !(vma->vm_flags & VM_PFNMAP)) {
1000 up_read(¤t->mm->mmap_sem);
1002 return page_to_pfn(bad_page);
1005 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1006 up_read(¤t->mm->mmap_sem);
1007 BUG_ON(!kvm_is_mmio_pfn(pfn));
1009 pfn = page_to_pfn(page[0]);
1014 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1016 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1020 pfn = gfn_to_pfn(kvm, gfn);
1021 if (!kvm_is_mmio_pfn(pfn))
1022 return pfn_to_page(pfn);
1024 WARN_ON(kvm_is_mmio_pfn(pfn));
1030 EXPORT_SYMBOL_GPL(gfn_to_page);
1032 void kvm_release_page_clean(struct page *page)
1034 kvm_release_pfn_clean(page_to_pfn(page));
1036 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1038 void kvm_release_pfn_clean(pfn_t pfn)
1040 if (!kvm_is_mmio_pfn(pfn))
1041 put_page(pfn_to_page(pfn));
1043 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1045 void kvm_release_page_dirty(struct page *page)
1047 kvm_release_pfn_dirty(page_to_pfn(page));
1049 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1051 void kvm_release_pfn_dirty(pfn_t pfn)
1053 kvm_set_pfn_dirty(pfn);
1054 kvm_release_pfn_clean(pfn);
1056 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1058 void kvm_set_page_dirty(struct page *page)
1060 kvm_set_pfn_dirty(page_to_pfn(page));
1062 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1064 void kvm_set_pfn_dirty(pfn_t pfn)
1066 if (!kvm_is_mmio_pfn(pfn)) {
1067 struct page *page = pfn_to_page(pfn);
1068 if (!PageReserved(page))
1072 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1074 void kvm_set_pfn_accessed(pfn_t pfn)
1076 if (!kvm_is_mmio_pfn(pfn))
1077 mark_page_accessed(pfn_to_page(pfn));
1079 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1081 void kvm_get_pfn(pfn_t pfn)
1083 if (!kvm_is_mmio_pfn(pfn))
1084 get_page(pfn_to_page(pfn));
1086 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1088 static int next_segment(unsigned long len, int offset)
1090 if (len > PAGE_SIZE - offset)
1091 return PAGE_SIZE - offset;
1096 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1102 addr = gfn_to_hva(kvm, gfn);
1103 if (kvm_is_error_hva(addr))
1105 r = copy_from_user(data, (void __user *)addr + offset, len);
1110 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1112 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1114 gfn_t gfn = gpa >> PAGE_SHIFT;
1116 int offset = offset_in_page(gpa);
1119 while ((seg = next_segment(len, offset)) != 0) {
1120 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1130 EXPORT_SYMBOL_GPL(kvm_read_guest);
1132 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1137 gfn_t gfn = gpa >> PAGE_SHIFT;
1138 int offset = offset_in_page(gpa);
1140 addr = gfn_to_hva(kvm, gfn);
1141 if (kvm_is_error_hva(addr))
1143 pagefault_disable();
1144 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1150 EXPORT_SYMBOL(kvm_read_guest_atomic);
1152 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1153 int offset, int len)
1158 addr = gfn_to_hva(kvm, gfn);
1159 if (kvm_is_error_hva(addr))
1161 r = copy_to_user((void __user *)addr + offset, data, len);
1164 mark_page_dirty(kvm, gfn);
1167 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1169 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1172 gfn_t gfn = gpa >> PAGE_SHIFT;
1174 int offset = offset_in_page(gpa);
1177 while ((seg = next_segment(len, offset)) != 0) {
1178 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1189 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1191 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1193 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1195 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1197 gfn_t gfn = gpa >> PAGE_SHIFT;
1199 int offset = offset_in_page(gpa);
1202 while ((seg = next_segment(len, offset)) != 0) {
1203 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1212 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1214 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1216 struct kvm_memory_slot *memslot;
1218 gfn = unalias_gfn(kvm, gfn);
1219 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1220 if (memslot && memslot->dirty_bitmap) {
1221 unsigned long rel_gfn = gfn - memslot->base_gfn;
1224 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1225 set_bit(rel_gfn, memslot->dirty_bitmap);
1230 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1232 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1237 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1239 if (kvm_cpu_has_interrupt(vcpu) ||
1240 kvm_cpu_has_pending_timer(vcpu) ||
1241 kvm_arch_vcpu_runnable(vcpu)) {
1242 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1245 if (signal_pending(current))
1253 finish_wait(&vcpu->wq, &wait);
1256 void kvm_resched(struct kvm_vcpu *vcpu)
1258 if (!need_resched())
1262 EXPORT_SYMBOL_GPL(kvm_resched);
1264 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1266 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1269 if (vmf->pgoff == 0)
1270 page = virt_to_page(vcpu->run);
1272 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1273 page = virt_to_page(vcpu->arch.pio_data);
1275 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1276 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1277 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1280 return VM_FAULT_SIGBUS;
1286 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1287 .fault = kvm_vcpu_fault,
1290 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1292 vma->vm_ops = &kvm_vcpu_vm_ops;
1296 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1298 struct kvm_vcpu *vcpu = filp->private_data;
1300 kvm_put_kvm(vcpu->kvm);
1304 static const struct file_operations kvm_vcpu_fops = {
1305 .release = kvm_vcpu_release,
1306 .unlocked_ioctl = kvm_vcpu_ioctl,
1307 .compat_ioctl = kvm_vcpu_ioctl,
1308 .mmap = kvm_vcpu_mmap,
1312 * Allocates an inode for the vcpu.
1314 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1316 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1318 kvm_put_kvm(vcpu->kvm);
1323 * Creates some virtual cpus. Good luck creating more than one.
1325 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1328 struct kvm_vcpu *vcpu;
1333 vcpu = kvm_arch_vcpu_create(kvm, n);
1335 return PTR_ERR(vcpu);
1337 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1339 r = kvm_arch_vcpu_setup(vcpu);
1343 mutex_lock(&kvm->lock);
1344 if (kvm->vcpus[n]) {
1348 kvm->vcpus[n] = vcpu;
1349 mutex_unlock(&kvm->lock);
1351 /* Now it's all set up, let userspace reach it */
1353 r = create_vcpu_fd(vcpu);
1359 mutex_lock(&kvm->lock);
1360 kvm->vcpus[n] = NULL;
1362 mutex_unlock(&kvm->lock);
1363 kvm_arch_vcpu_destroy(vcpu);
1367 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1370 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1371 vcpu->sigset_active = 1;
1372 vcpu->sigset = *sigset;
1374 vcpu->sigset_active = 0;
1378 static long kvm_vcpu_ioctl(struct file *filp,
1379 unsigned int ioctl, unsigned long arg)
1381 struct kvm_vcpu *vcpu = filp->private_data;
1382 void __user *argp = (void __user *)arg;
1384 struct kvm_fpu *fpu = NULL;
1385 struct kvm_sregs *kvm_sregs = NULL;
1387 if (vcpu->kvm->mm != current->mm)
1394 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1396 case KVM_GET_REGS: {
1397 struct kvm_regs *kvm_regs;
1400 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1403 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1407 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1414 case KVM_SET_REGS: {
1415 struct kvm_regs *kvm_regs;
1418 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1422 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1424 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1432 case KVM_GET_SREGS: {
1433 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1437 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1441 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1446 case KVM_SET_SREGS: {
1447 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1452 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1454 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1460 case KVM_GET_MP_STATE: {
1461 struct kvm_mp_state mp_state;
1463 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1467 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1472 case KVM_SET_MP_STATE: {
1473 struct kvm_mp_state mp_state;
1476 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1478 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1484 case KVM_TRANSLATE: {
1485 struct kvm_translation tr;
1488 if (copy_from_user(&tr, argp, sizeof tr))
1490 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1494 if (copy_to_user(argp, &tr, sizeof tr))
1499 case KVM_DEBUG_GUEST: {
1500 struct kvm_debug_guest dbg;
1503 if (copy_from_user(&dbg, argp, sizeof dbg))
1505 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1511 case KVM_SET_SIGNAL_MASK: {
1512 struct kvm_signal_mask __user *sigmask_arg = argp;
1513 struct kvm_signal_mask kvm_sigmask;
1514 sigset_t sigset, *p;
1519 if (copy_from_user(&kvm_sigmask, argp,
1520 sizeof kvm_sigmask))
1523 if (kvm_sigmask.len != sizeof sigset)
1526 if (copy_from_user(&sigset, sigmask_arg->sigset,
1531 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1535 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1539 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1543 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1549 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1554 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1556 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1563 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1571 static long kvm_vm_ioctl(struct file *filp,
1572 unsigned int ioctl, unsigned long arg)
1574 struct kvm *kvm = filp->private_data;
1575 void __user *argp = (void __user *)arg;
1578 if (kvm->mm != current->mm)
1581 case KVM_CREATE_VCPU:
1582 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1586 case KVM_SET_USER_MEMORY_REGION: {
1587 struct kvm_userspace_memory_region kvm_userspace_mem;
1590 if (copy_from_user(&kvm_userspace_mem, argp,
1591 sizeof kvm_userspace_mem))
1594 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1599 case KVM_GET_DIRTY_LOG: {
1600 struct kvm_dirty_log log;
1603 if (copy_from_user(&log, argp, sizeof log))
1605 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1610 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1611 case KVM_REGISTER_COALESCED_MMIO: {
1612 struct kvm_coalesced_mmio_zone zone;
1614 if (copy_from_user(&zone, argp, sizeof zone))
1617 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1623 case KVM_UNREGISTER_COALESCED_MMIO: {
1624 struct kvm_coalesced_mmio_zone zone;
1626 if (copy_from_user(&zone, argp, sizeof zone))
1629 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1636 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1637 case KVM_ASSIGN_PCI_DEVICE: {
1638 struct kvm_assigned_pci_dev assigned_dev;
1641 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1643 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1648 case KVM_ASSIGN_IRQ: {
1649 struct kvm_assigned_irq assigned_irq;
1652 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1654 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1661 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1667 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1669 struct page *page[1];
1672 gfn_t gfn = vmf->pgoff;
1673 struct kvm *kvm = vma->vm_file->private_data;
1675 addr = gfn_to_hva(kvm, gfn);
1676 if (kvm_is_error_hva(addr))
1677 return VM_FAULT_SIGBUS;
1679 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1681 if (unlikely(npages != 1))
1682 return VM_FAULT_SIGBUS;
1684 vmf->page = page[0];
1688 static struct vm_operations_struct kvm_vm_vm_ops = {
1689 .fault = kvm_vm_fault,
1692 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1694 vma->vm_ops = &kvm_vm_vm_ops;
1698 static const struct file_operations kvm_vm_fops = {
1699 .release = kvm_vm_release,
1700 .unlocked_ioctl = kvm_vm_ioctl,
1701 .compat_ioctl = kvm_vm_ioctl,
1702 .mmap = kvm_vm_mmap,
1705 static int kvm_dev_ioctl_create_vm(void)
1710 kvm = kvm_create_vm();
1712 return PTR_ERR(kvm);
1713 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1720 static long kvm_dev_ioctl(struct file *filp,
1721 unsigned int ioctl, unsigned long arg)
1726 case KVM_GET_API_VERSION:
1730 r = KVM_API_VERSION;
1736 r = kvm_dev_ioctl_create_vm();
1738 case KVM_CHECK_EXTENSION:
1739 r = kvm_dev_ioctl_check_extension(arg);
1741 case KVM_GET_VCPU_MMAP_SIZE:
1745 r = PAGE_SIZE; /* struct kvm_run */
1747 r += PAGE_SIZE; /* pio data page */
1749 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1750 r += PAGE_SIZE; /* coalesced mmio ring page */
1753 case KVM_TRACE_ENABLE:
1754 case KVM_TRACE_PAUSE:
1755 case KVM_TRACE_DISABLE:
1756 r = kvm_trace_ioctl(ioctl, arg);
1759 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1765 static struct file_operations kvm_chardev_ops = {
1766 .unlocked_ioctl = kvm_dev_ioctl,
1767 .compat_ioctl = kvm_dev_ioctl,
1770 static struct miscdevice kvm_dev = {
1776 static void hardware_enable(void *junk)
1778 int cpu = raw_smp_processor_id();
1780 if (cpu_isset(cpu, cpus_hardware_enabled))
1782 cpu_set(cpu, cpus_hardware_enabled);
1783 kvm_arch_hardware_enable(NULL);
1786 static void hardware_disable(void *junk)
1788 int cpu = raw_smp_processor_id();
1790 if (!cpu_isset(cpu, cpus_hardware_enabled))
1792 cpu_clear(cpu, cpus_hardware_enabled);
1793 kvm_arch_hardware_disable(NULL);
1796 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1801 val &= ~CPU_TASKS_FROZEN;
1804 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1806 hardware_disable(NULL);
1808 case CPU_UP_CANCELED:
1809 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1811 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1814 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1816 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1823 asmlinkage void kvm_handle_fault_on_reboot(void)
1826 /* spin while reset goes on */
1829 /* Fault while not rebooting. We want the trace. */
1832 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1834 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1837 if (val == SYS_RESTART) {
1839 * Some (well, at least mine) BIOSes hang on reboot if
1842 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1843 kvm_rebooting = true;
1844 on_each_cpu(hardware_disable, NULL, 1);
1849 static struct notifier_block kvm_reboot_notifier = {
1850 .notifier_call = kvm_reboot,
1854 void kvm_io_bus_init(struct kvm_io_bus *bus)
1856 memset(bus, 0, sizeof(*bus));
1859 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1863 for (i = 0; i < bus->dev_count; i++) {
1864 struct kvm_io_device *pos = bus->devs[i];
1866 kvm_iodevice_destructor(pos);
1870 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1871 gpa_t addr, int len, int is_write)
1875 for (i = 0; i < bus->dev_count; i++) {
1876 struct kvm_io_device *pos = bus->devs[i];
1878 if (pos->in_range(pos, addr, len, is_write))
1885 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1887 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1889 bus->devs[bus->dev_count++] = dev;
1892 static struct notifier_block kvm_cpu_notifier = {
1893 .notifier_call = kvm_cpu_hotplug,
1894 .priority = 20, /* must be > scheduler priority */
1897 static int vm_stat_get(void *_offset, u64 *val)
1899 unsigned offset = (long)_offset;
1903 spin_lock(&kvm_lock);
1904 list_for_each_entry(kvm, &vm_list, vm_list)
1905 *val += *(u32 *)((void *)kvm + offset);
1906 spin_unlock(&kvm_lock);
1910 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1912 static int vcpu_stat_get(void *_offset, u64 *val)
1914 unsigned offset = (long)_offset;
1916 struct kvm_vcpu *vcpu;
1920 spin_lock(&kvm_lock);
1921 list_for_each_entry(kvm, &vm_list, vm_list)
1922 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1923 vcpu = kvm->vcpus[i];
1925 *val += *(u32 *)((void *)vcpu + offset);
1927 spin_unlock(&kvm_lock);
1931 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1933 static struct file_operations *stat_fops[] = {
1934 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1935 [KVM_STAT_VM] = &vm_stat_fops,
1938 static void kvm_init_debug(void)
1940 struct kvm_stats_debugfs_item *p;
1942 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
1943 for (p = debugfs_entries; p->name; ++p)
1944 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1945 (void *)(long)p->offset,
1946 stat_fops[p->kind]);
1949 static void kvm_exit_debug(void)
1951 struct kvm_stats_debugfs_item *p;
1953 for (p = debugfs_entries; p->name; ++p)
1954 debugfs_remove(p->dentry);
1955 debugfs_remove(kvm_debugfs_dir);
1958 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1960 hardware_disable(NULL);
1964 static int kvm_resume(struct sys_device *dev)
1966 hardware_enable(NULL);
1970 static struct sysdev_class kvm_sysdev_class = {
1972 .suspend = kvm_suspend,
1973 .resume = kvm_resume,
1976 static struct sys_device kvm_sysdev = {
1978 .cls = &kvm_sysdev_class,
1981 struct page *bad_page;
1985 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1987 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1990 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1992 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1994 kvm_arch_vcpu_load(vcpu, cpu);
1997 static void kvm_sched_out(struct preempt_notifier *pn,
1998 struct task_struct *next)
2000 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2002 kvm_arch_vcpu_put(vcpu);
2005 int kvm_init(void *opaque, unsigned int vcpu_size,
2006 struct module *module)
2013 r = kvm_arch_init(opaque);
2017 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2019 if (bad_page == NULL) {
2024 bad_pfn = page_to_pfn(bad_page);
2026 r = kvm_arch_hardware_setup();
2030 for_each_online_cpu(cpu) {
2031 smp_call_function_single(cpu,
2032 kvm_arch_check_processor_compat,
2038 on_each_cpu(hardware_enable, NULL, 1);
2039 r = register_cpu_notifier(&kvm_cpu_notifier);
2042 register_reboot_notifier(&kvm_reboot_notifier);
2044 r = sysdev_class_register(&kvm_sysdev_class);
2048 r = sysdev_register(&kvm_sysdev);
2052 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2053 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2054 __alignof__(struct kvm_vcpu),
2056 if (!kvm_vcpu_cache) {
2061 kvm_chardev_ops.owner = module;
2063 r = misc_register(&kvm_dev);
2065 printk(KERN_ERR "kvm: misc device register failed\n");
2069 kvm_preempt_ops.sched_in = kvm_sched_in;
2070 kvm_preempt_ops.sched_out = kvm_sched_out;
2075 kmem_cache_destroy(kvm_vcpu_cache);
2077 sysdev_unregister(&kvm_sysdev);
2079 sysdev_class_unregister(&kvm_sysdev_class);
2081 unregister_reboot_notifier(&kvm_reboot_notifier);
2082 unregister_cpu_notifier(&kvm_cpu_notifier);
2084 on_each_cpu(hardware_disable, NULL, 1);
2086 kvm_arch_hardware_unsetup();
2088 __free_page(bad_page);
2095 EXPORT_SYMBOL_GPL(kvm_init);
2099 kvm_trace_cleanup();
2100 misc_deregister(&kvm_dev);
2101 kmem_cache_destroy(kvm_vcpu_cache);
2102 sysdev_unregister(&kvm_sysdev);
2103 sysdev_class_unregister(&kvm_sysdev_class);
2104 unregister_reboot_notifier(&kvm_reboot_notifier);
2105 unregister_cpu_notifier(&kvm_cpu_notifier);
2106 on_each_cpu(hardware_disable, NULL, 1);
2107 kvm_arch_hardware_unsetup();
2110 __free_page(bad_page);
2112 EXPORT_SYMBOL_GPL(kvm_exit);