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>
51 #include <asm/msidef.h>
54 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
55 #include "coalesced_mmio.h"
58 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
59 #include <linux/pci.h>
60 #include <linux/interrupt.h>
64 MODULE_AUTHOR("Qumranet");
65 MODULE_LICENSE("GPL");
67 static int msi2intx = 1;
68 module_param(msi2intx, bool, 0);
70 DEFINE_SPINLOCK(kvm_lock);
73 static cpumask_t cpus_hardware_enabled;
75 struct kmem_cache *kvm_vcpu_cache;
76 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
78 static __read_mostly struct preempt_ops kvm_preempt_ops;
80 struct dentry *kvm_debugfs_dir;
82 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
85 static bool kvm_rebooting;
87 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
90 static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev)
93 struct kvm_vcpu *vcpu;
94 struct kvm_ioapic *ioapic = ioapic_irqchip(dev->kvm);
95 int dest_id = (dev->guest_msi.address_lo & MSI_ADDR_DEST_ID_MASK)
96 >> MSI_ADDR_DEST_ID_SHIFT;
97 int vector = (dev->guest_msi.data & MSI_DATA_VECTOR_MASK)
98 >> MSI_DATA_VECTOR_SHIFT;
99 int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
100 (unsigned long *)&dev->guest_msi.address_lo);
101 int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
102 (unsigned long *)&dev->guest_msi.data);
103 int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
104 (unsigned long *)&dev->guest_msi.data);
109 deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
111 /* IOAPIC delivery mode value is the same as MSI here */
112 switch (delivery_mode) {
113 case IOAPIC_LOWEST_PRIORITY:
114 vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
117 kvm_apic_set_irq(vcpu, vector, trig_mode);
119 printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
122 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
123 if (!(deliver_bitmask & (1 << vcpu_id)))
125 deliver_bitmask &= ~(1 << vcpu_id);
126 vcpu = ioapic->kvm->vcpus[vcpu_id];
128 kvm_apic_set_irq(vcpu, vector, trig_mode);
132 printk(KERN_INFO "kvm: unsupported MSI delivery mode\n");
136 static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev) {}
139 static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
142 struct list_head *ptr;
143 struct kvm_assigned_dev_kernel *match;
145 list_for_each(ptr, head) {
146 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
147 if (match->assigned_dev_id == assigned_dev_id)
153 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
155 struct kvm_assigned_dev_kernel *assigned_dev;
157 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
160 /* This is taken to safely inject irq inside the guest. When
161 * the interrupt injection (or the ioapic code) uses a
162 * finer-grained lock, update this
164 mutex_lock(&assigned_dev->kvm->lock);
165 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_INTX)
166 kvm_set_irq(assigned_dev->kvm,
167 assigned_dev->irq_source_id,
168 assigned_dev->guest_irq, 1);
169 else if (assigned_dev->irq_requested_type &
170 KVM_ASSIGNED_DEV_GUEST_MSI) {
171 assigned_device_msi_dispatch(assigned_dev);
172 enable_irq(assigned_dev->host_irq);
174 mutex_unlock(&assigned_dev->kvm->lock);
175 kvm_put_kvm(assigned_dev->kvm);
178 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
180 struct kvm_assigned_dev_kernel *assigned_dev =
181 (struct kvm_assigned_dev_kernel *) dev_id;
183 kvm_get_kvm(assigned_dev->kvm);
184 schedule_work(&assigned_dev->interrupt_work);
185 disable_irq_nosync(irq);
189 /* Ack the irq line for an assigned device */
190 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
192 struct kvm_assigned_dev_kernel *dev;
197 dev = container_of(kian, struct kvm_assigned_dev_kernel,
199 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
200 enable_irq(dev->host_irq);
203 static void kvm_free_assigned_device(struct kvm *kvm,
204 struct kvm_assigned_dev_kernel
207 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested_type)
208 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
209 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
210 pci_disable_msi(assigned_dev->dev);
212 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
213 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
215 if (cancel_work_sync(&assigned_dev->interrupt_work))
216 /* We had pending work. That means we will have to take
217 * care of kvm_put_kvm.
221 pci_reset_function(assigned_dev->dev);
223 pci_release_regions(assigned_dev->dev);
224 pci_disable_device(assigned_dev->dev);
225 pci_dev_put(assigned_dev->dev);
227 list_del(&assigned_dev->list);
231 void kvm_free_all_assigned_devices(struct kvm *kvm)
233 struct list_head *ptr, *ptr2;
234 struct kvm_assigned_dev_kernel *assigned_dev;
236 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
237 assigned_dev = list_entry(ptr,
238 struct kvm_assigned_dev_kernel,
241 kvm_free_assigned_device(kvm, assigned_dev);
245 static int assigned_device_update_intx(struct kvm *kvm,
246 struct kvm_assigned_dev_kernel *adev,
247 struct kvm_assigned_irq *airq)
249 adev->guest_irq = airq->guest_irq;
250 adev->ack_notifier.gsi = airq->guest_irq;
252 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
255 if (irqchip_in_kernel(kvm)) {
257 adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) {
258 free_irq(adev->host_irq, (void *)kvm);
259 pci_disable_msi(adev->dev);
262 if (!capable(CAP_SYS_RAWIO))
266 adev->host_irq = airq->host_irq;
268 adev->host_irq = adev->dev->irq;
270 /* Even though this is PCI, we don't want to use shared
271 * interrupts. Sharing host devices with guest-assigned devices
272 * on the same interrupt line is not a happy situation: there
273 * are going to be long delays in accepting, acking, etc.
275 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
276 0, "kvm_assigned_intx_device", (void *)adev))
280 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
281 KVM_ASSIGNED_DEV_HOST_INTX;
286 static int assigned_device_update_msi(struct kvm *kvm,
287 struct kvm_assigned_dev_kernel *adev,
288 struct kvm_assigned_irq *airq)
292 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
293 /* x86 don't care upper address of guest msi message addr */
294 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
295 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
296 adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
297 adev->guest_msi.data = airq->guest_msi.data;
298 adev->ack_notifier.gsi = -1;
299 } else if (msi2intx) {
300 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
301 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
302 adev->guest_irq = airq->guest_irq;
303 adev->ack_notifier.gsi = airq->guest_irq;
306 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
309 if (irqchip_in_kernel(kvm)) {
311 if (adev->irq_requested_type &
312 KVM_ASSIGNED_DEV_HOST_INTX)
313 free_irq(adev->host_irq, (void *)adev);
315 r = pci_enable_msi(adev->dev);
320 adev->host_irq = adev->dev->irq;
321 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
322 "kvm_assigned_msi_device", (void *)adev))
327 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
329 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
334 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
335 struct kvm_assigned_irq
339 struct kvm_assigned_dev_kernel *match;
341 mutex_lock(&kvm->lock);
343 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
344 assigned_irq->assigned_dev_id);
346 mutex_unlock(&kvm->lock);
350 if (!match->irq_requested_type) {
351 INIT_WORK(&match->interrupt_work,
352 kvm_assigned_dev_interrupt_work_handler);
353 if (irqchip_in_kernel(kvm)) {
354 /* Register ack nofitier */
355 match->ack_notifier.gsi = -1;
356 match->ack_notifier.irq_acked =
357 kvm_assigned_dev_ack_irq;
358 kvm_register_irq_ack_notifier(kvm,
359 &match->ack_notifier);
361 /* Request IRQ source ID */
362 r = kvm_request_irq_source_id(kvm);
366 match->irq_source_id = r;
369 /* Determine host device irq type, we can know the
370 * result from dev->msi_enabled */
372 pci_enable_msi(match->dev);
378 (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI)) ||
379 (msi2intx && match->dev->msi_enabled)) {
381 r = assigned_device_update_msi(kvm, match, assigned_irq);
383 printk(KERN_WARNING "kvm: failed to enable "
390 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
391 /* Host device IRQ 0 means don't support INTx */
394 "kvm: wait device to enable MSI!\n");
398 "kvm: failed to enable MSI device!\n");
403 /* Non-sharing INTx mode */
404 r = assigned_device_update_intx(kvm, match, assigned_irq);
406 printk(KERN_WARNING "kvm: failed to enable "
412 mutex_unlock(&kvm->lock);
415 mutex_unlock(&kvm->lock);
416 kvm_free_assigned_device(kvm, match);
420 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
421 struct kvm_assigned_pci_dev *assigned_dev)
424 struct kvm_assigned_dev_kernel *match;
427 mutex_lock(&kvm->lock);
429 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
430 assigned_dev->assigned_dev_id);
432 /* device already assigned */
437 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
439 printk(KERN_INFO "%s: Couldn't allocate memory\n",
444 dev = pci_get_bus_and_slot(assigned_dev->busnr,
445 assigned_dev->devfn);
447 printk(KERN_INFO "%s: host device not found\n", __func__);
451 if (pci_enable_device(dev)) {
452 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
456 r = pci_request_regions(dev, "kvm_assigned_device");
458 printk(KERN_INFO "%s: Could not get access to device regions\n",
463 pci_reset_function(dev);
465 match->assigned_dev_id = assigned_dev->assigned_dev_id;
466 match->host_busnr = assigned_dev->busnr;
467 match->host_devfn = assigned_dev->devfn;
472 list_add(&match->list, &kvm->arch.assigned_dev_head);
474 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
475 r = kvm_iommu_map_guest(kvm, match);
481 mutex_unlock(&kvm->lock);
484 list_del(&match->list);
485 pci_release_regions(dev);
487 pci_disable_device(dev);
492 mutex_unlock(&kvm->lock);
497 static inline int valid_vcpu(int n)
499 return likely(n >= 0 && n < KVM_MAX_VCPUS);
502 inline int kvm_is_mmio_pfn(pfn_t pfn)
505 return PageReserved(pfn_to_page(pfn));
511 * Switches to specified vcpu, until a matching vcpu_put()
513 void vcpu_load(struct kvm_vcpu *vcpu)
517 mutex_lock(&vcpu->mutex);
519 preempt_notifier_register(&vcpu->preempt_notifier);
520 kvm_arch_vcpu_load(vcpu, cpu);
524 void vcpu_put(struct kvm_vcpu *vcpu)
527 kvm_arch_vcpu_put(vcpu);
528 preempt_notifier_unregister(&vcpu->preempt_notifier);
530 mutex_unlock(&vcpu->mutex);
533 static void ack_flush(void *_completed)
537 void kvm_flush_remote_tlbs(struct kvm *kvm)
541 struct kvm_vcpu *vcpu;
545 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
546 vcpu = kvm->vcpus[i];
549 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
552 if (cpu != -1 && cpu != me)
555 if (cpus_empty(cpus))
557 ++kvm->stat.remote_tlb_flush;
558 smp_call_function_mask(cpus, ack_flush, NULL, 1);
563 void kvm_reload_remote_mmus(struct kvm *kvm)
567 struct kvm_vcpu *vcpu;
571 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
572 vcpu = kvm->vcpus[i];
575 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
578 if (cpu != -1 && cpu != me)
581 if (cpus_empty(cpus))
583 smp_call_function_mask(cpus, ack_flush, NULL, 1);
589 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
594 mutex_init(&vcpu->mutex);
598 init_waitqueue_head(&vcpu->wq);
600 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
605 vcpu->run = page_address(page);
607 r = kvm_arch_vcpu_init(vcpu);
613 free_page((unsigned long)vcpu->run);
617 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
619 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
621 kvm_arch_vcpu_uninit(vcpu);
622 free_page((unsigned long)vcpu->run);
624 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
626 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
627 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
629 return container_of(mn, struct kvm, mmu_notifier);
632 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
633 struct mm_struct *mm,
634 unsigned long address)
636 struct kvm *kvm = mmu_notifier_to_kvm(mn);
640 * When ->invalidate_page runs, the linux pte has been zapped
641 * already but the page is still allocated until
642 * ->invalidate_page returns. So if we increase the sequence
643 * here the kvm page fault will notice if the spte can't be
644 * established because the page is going to be freed. If
645 * instead the kvm page fault establishes the spte before
646 * ->invalidate_page runs, kvm_unmap_hva will release it
649 * The sequence increase only need to be seen at spin_unlock
650 * time, and not at spin_lock time.
652 * Increasing the sequence after the spin_unlock would be
653 * unsafe because the kvm page fault could then establish the
654 * pte after kvm_unmap_hva returned, without noticing the page
655 * is going to be freed.
657 spin_lock(&kvm->mmu_lock);
658 kvm->mmu_notifier_seq++;
659 need_tlb_flush = kvm_unmap_hva(kvm, address);
660 spin_unlock(&kvm->mmu_lock);
662 /* we've to flush the tlb before the pages can be freed */
664 kvm_flush_remote_tlbs(kvm);
668 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
669 struct mm_struct *mm,
673 struct kvm *kvm = mmu_notifier_to_kvm(mn);
674 int need_tlb_flush = 0;
676 spin_lock(&kvm->mmu_lock);
678 * The count increase must become visible at unlock time as no
679 * spte can be established without taking the mmu_lock and
680 * count is also read inside the mmu_lock critical section.
682 kvm->mmu_notifier_count++;
683 for (; start < end; start += PAGE_SIZE)
684 need_tlb_flush |= kvm_unmap_hva(kvm, start);
685 spin_unlock(&kvm->mmu_lock);
687 /* we've to flush the tlb before the pages can be freed */
689 kvm_flush_remote_tlbs(kvm);
692 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
693 struct mm_struct *mm,
697 struct kvm *kvm = mmu_notifier_to_kvm(mn);
699 spin_lock(&kvm->mmu_lock);
701 * This sequence increase will notify the kvm page fault that
702 * the page that is going to be mapped in the spte could have
705 kvm->mmu_notifier_seq++;
707 * The above sequence increase must be visible before the
708 * below count decrease but both values are read by the kvm
709 * page fault under mmu_lock spinlock so we don't need to add
710 * a smb_wmb() here in between the two.
712 kvm->mmu_notifier_count--;
713 spin_unlock(&kvm->mmu_lock);
715 BUG_ON(kvm->mmu_notifier_count < 0);
718 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
719 struct mm_struct *mm,
720 unsigned long address)
722 struct kvm *kvm = mmu_notifier_to_kvm(mn);
725 spin_lock(&kvm->mmu_lock);
726 young = kvm_age_hva(kvm, address);
727 spin_unlock(&kvm->mmu_lock);
730 kvm_flush_remote_tlbs(kvm);
735 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
736 .invalidate_page = kvm_mmu_notifier_invalidate_page,
737 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
738 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
739 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
741 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
743 static struct kvm *kvm_create_vm(void)
745 struct kvm *kvm = kvm_arch_create_vm();
746 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
753 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
754 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
757 return ERR_PTR(-ENOMEM);
759 kvm->coalesced_mmio_ring =
760 (struct kvm_coalesced_mmio_ring *)page_address(page);
763 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
766 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
767 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
769 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
778 kvm->mm = current->mm;
779 atomic_inc(&kvm->mm->mm_count);
780 spin_lock_init(&kvm->mmu_lock);
781 kvm_io_bus_init(&kvm->pio_bus);
782 mutex_init(&kvm->lock);
783 kvm_io_bus_init(&kvm->mmio_bus);
784 init_rwsem(&kvm->slots_lock);
785 atomic_set(&kvm->users_count, 1);
786 spin_lock(&kvm_lock);
787 list_add(&kvm->vm_list, &vm_list);
788 spin_unlock(&kvm_lock);
789 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
790 kvm_coalesced_mmio_init(kvm);
797 * Free any memory in @free but not in @dont.
799 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
800 struct kvm_memory_slot *dont)
802 if (!dont || free->rmap != dont->rmap)
805 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
806 vfree(free->dirty_bitmap);
808 if (!dont || free->lpage_info != dont->lpage_info)
809 vfree(free->lpage_info);
812 free->dirty_bitmap = NULL;
814 free->lpage_info = NULL;
817 void kvm_free_physmem(struct kvm *kvm)
821 for (i = 0; i < kvm->nmemslots; ++i)
822 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
825 static void kvm_destroy_vm(struct kvm *kvm)
827 struct mm_struct *mm = kvm->mm;
829 spin_lock(&kvm_lock);
830 list_del(&kvm->vm_list);
831 spin_unlock(&kvm_lock);
832 kvm_io_bus_destroy(&kvm->pio_bus);
833 kvm_io_bus_destroy(&kvm->mmio_bus);
834 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
835 if (kvm->coalesced_mmio_ring != NULL)
836 free_page((unsigned long)kvm->coalesced_mmio_ring);
838 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
839 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
841 kvm_arch_destroy_vm(kvm);
845 void kvm_get_kvm(struct kvm *kvm)
847 atomic_inc(&kvm->users_count);
849 EXPORT_SYMBOL_GPL(kvm_get_kvm);
851 void kvm_put_kvm(struct kvm *kvm)
853 if (atomic_dec_and_test(&kvm->users_count))
856 EXPORT_SYMBOL_GPL(kvm_put_kvm);
859 static int kvm_vm_release(struct inode *inode, struct file *filp)
861 struct kvm *kvm = filp->private_data;
868 * Allocate some memory and give it an address in the guest physical address
871 * Discontiguous memory is allowed, mostly for framebuffers.
873 * Must be called holding mmap_sem for write.
875 int __kvm_set_memory_region(struct kvm *kvm,
876 struct kvm_userspace_memory_region *mem,
881 unsigned long npages;
883 struct kvm_memory_slot *memslot;
884 struct kvm_memory_slot old, new;
887 /* General sanity checks */
888 if (mem->memory_size & (PAGE_SIZE - 1))
890 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
892 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
894 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
896 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
899 memslot = &kvm->memslots[mem->slot];
900 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
901 npages = mem->memory_size >> PAGE_SHIFT;
904 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
906 new = old = *memslot;
908 new.base_gfn = base_gfn;
910 new.flags = mem->flags;
912 /* Disallow changing a memory slot's size. */
914 if (npages && old.npages && npages != old.npages)
917 /* Check for overlaps */
919 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
920 struct kvm_memory_slot *s = &kvm->memslots[i];
924 if (!((base_gfn + npages <= s->base_gfn) ||
925 (base_gfn >= s->base_gfn + s->npages)))
929 /* Free page dirty bitmap if unneeded */
930 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
931 new.dirty_bitmap = NULL;
935 /* Allocate if a slot is being created */
937 if (npages && !new.rmap) {
938 new.rmap = vmalloc(npages * sizeof(struct page *));
943 memset(new.rmap, 0, npages * sizeof(*new.rmap));
945 new.user_alloc = user_alloc;
947 * hva_to_rmmap() serialzies with the mmu_lock and to be
948 * safe it has to ignore memslots with !user_alloc &&
952 new.userspace_addr = mem->userspace_addr;
954 new.userspace_addr = 0;
956 if (npages && !new.lpage_info) {
957 int largepages = npages / KVM_PAGES_PER_HPAGE;
958 if (npages % KVM_PAGES_PER_HPAGE)
960 if (base_gfn % KVM_PAGES_PER_HPAGE)
963 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
968 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
970 if (base_gfn % KVM_PAGES_PER_HPAGE)
971 new.lpage_info[0].write_count = 1;
972 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
973 new.lpage_info[largepages-1].write_count = 1;
976 /* Allocate page dirty bitmap if needed */
977 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
978 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
980 new.dirty_bitmap = vmalloc(dirty_bytes);
981 if (!new.dirty_bitmap)
983 memset(new.dirty_bitmap, 0, dirty_bytes);
985 #endif /* not defined CONFIG_S390 */
988 kvm_arch_flush_shadow(kvm);
990 spin_lock(&kvm->mmu_lock);
991 if (mem->slot >= kvm->nmemslots)
992 kvm->nmemslots = mem->slot + 1;
995 spin_unlock(&kvm->mmu_lock);
997 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
999 spin_lock(&kvm->mmu_lock);
1001 spin_unlock(&kvm->mmu_lock);
1005 kvm_free_physmem_slot(&old, &new);
1007 /* map the pages in iommu page table */
1008 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1015 kvm_free_physmem_slot(&new, &old);
1020 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1022 int kvm_set_memory_region(struct kvm *kvm,
1023 struct kvm_userspace_memory_region *mem,
1028 down_write(&kvm->slots_lock);
1029 r = __kvm_set_memory_region(kvm, mem, user_alloc);
1030 up_write(&kvm->slots_lock);
1033 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1035 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1037 kvm_userspace_memory_region *mem,
1040 if (mem->slot >= KVM_MEMORY_SLOTS)
1042 return kvm_set_memory_region(kvm, mem, user_alloc);
1045 int kvm_get_dirty_log(struct kvm *kvm,
1046 struct kvm_dirty_log *log, int *is_dirty)
1048 struct kvm_memory_slot *memslot;
1051 unsigned long any = 0;
1054 if (log->slot >= KVM_MEMORY_SLOTS)
1057 memslot = &kvm->memslots[log->slot];
1059 if (!memslot->dirty_bitmap)
1062 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1064 for (i = 0; !any && i < n/sizeof(long); ++i)
1065 any = memslot->dirty_bitmap[i];
1068 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1079 int is_error_page(struct page *page)
1081 return page == bad_page;
1083 EXPORT_SYMBOL_GPL(is_error_page);
1085 int is_error_pfn(pfn_t pfn)
1087 return pfn == bad_pfn;
1089 EXPORT_SYMBOL_GPL(is_error_pfn);
1091 static inline unsigned long bad_hva(void)
1096 int kvm_is_error_hva(unsigned long addr)
1098 return addr == bad_hva();
1100 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1102 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1106 for (i = 0; i < kvm->nmemslots; ++i) {
1107 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1109 if (gfn >= memslot->base_gfn
1110 && gfn < memslot->base_gfn + memslot->npages)
1115 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1117 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1119 gfn = unalias_gfn(kvm, gfn);
1120 return gfn_to_memslot_unaliased(kvm, gfn);
1123 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1127 gfn = unalias_gfn(kvm, gfn);
1128 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1129 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1131 if (gfn >= memslot->base_gfn
1132 && gfn < memslot->base_gfn + memslot->npages)
1137 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1139 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1141 struct kvm_memory_slot *slot;
1143 gfn = unalias_gfn(kvm, gfn);
1144 slot = gfn_to_memslot_unaliased(kvm, gfn);
1147 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1149 EXPORT_SYMBOL_GPL(gfn_to_hva);
1151 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1153 struct page *page[1];
1160 addr = gfn_to_hva(kvm, gfn);
1161 if (kvm_is_error_hva(addr)) {
1163 return page_to_pfn(bad_page);
1166 npages = get_user_pages_fast(addr, 1, 1, page);
1168 if (unlikely(npages != 1)) {
1169 struct vm_area_struct *vma;
1171 down_read(¤t->mm->mmap_sem);
1172 vma = find_vma(current->mm, addr);
1174 if (vma == NULL || addr < vma->vm_start ||
1175 !(vma->vm_flags & VM_PFNMAP)) {
1176 up_read(¤t->mm->mmap_sem);
1178 return page_to_pfn(bad_page);
1181 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1182 up_read(¤t->mm->mmap_sem);
1183 BUG_ON(!kvm_is_mmio_pfn(pfn));
1185 pfn = page_to_pfn(page[0]);
1190 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1192 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1196 pfn = gfn_to_pfn(kvm, gfn);
1197 if (!kvm_is_mmio_pfn(pfn))
1198 return pfn_to_page(pfn);
1200 WARN_ON(kvm_is_mmio_pfn(pfn));
1206 EXPORT_SYMBOL_GPL(gfn_to_page);
1208 void kvm_release_page_clean(struct page *page)
1210 kvm_release_pfn_clean(page_to_pfn(page));
1212 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1214 void kvm_release_pfn_clean(pfn_t pfn)
1216 if (!kvm_is_mmio_pfn(pfn))
1217 put_page(pfn_to_page(pfn));
1219 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1221 void kvm_release_page_dirty(struct page *page)
1223 kvm_release_pfn_dirty(page_to_pfn(page));
1225 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1227 void kvm_release_pfn_dirty(pfn_t pfn)
1229 kvm_set_pfn_dirty(pfn);
1230 kvm_release_pfn_clean(pfn);
1232 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1234 void kvm_set_page_dirty(struct page *page)
1236 kvm_set_pfn_dirty(page_to_pfn(page));
1238 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1240 void kvm_set_pfn_dirty(pfn_t pfn)
1242 if (!kvm_is_mmio_pfn(pfn)) {
1243 struct page *page = pfn_to_page(pfn);
1244 if (!PageReserved(page))
1248 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1250 void kvm_set_pfn_accessed(pfn_t pfn)
1252 if (!kvm_is_mmio_pfn(pfn))
1253 mark_page_accessed(pfn_to_page(pfn));
1255 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1257 void kvm_get_pfn(pfn_t pfn)
1259 if (!kvm_is_mmio_pfn(pfn))
1260 get_page(pfn_to_page(pfn));
1262 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1264 static int next_segment(unsigned long len, int offset)
1266 if (len > PAGE_SIZE - offset)
1267 return PAGE_SIZE - offset;
1272 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1278 addr = gfn_to_hva(kvm, gfn);
1279 if (kvm_is_error_hva(addr))
1281 r = copy_from_user(data, (void __user *)addr + offset, len);
1286 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1288 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1290 gfn_t gfn = gpa >> PAGE_SHIFT;
1292 int offset = offset_in_page(gpa);
1295 while ((seg = next_segment(len, offset)) != 0) {
1296 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1306 EXPORT_SYMBOL_GPL(kvm_read_guest);
1308 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1313 gfn_t gfn = gpa >> PAGE_SHIFT;
1314 int offset = offset_in_page(gpa);
1316 addr = gfn_to_hva(kvm, gfn);
1317 if (kvm_is_error_hva(addr))
1319 pagefault_disable();
1320 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1326 EXPORT_SYMBOL(kvm_read_guest_atomic);
1328 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1329 int offset, int len)
1334 addr = gfn_to_hva(kvm, gfn);
1335 if (kvm_is_error_hva(addr))
1337 r = copy_to_user((void __user *)addr + offset, data, len);
1340 mark_page_dirty(kvm, gfn);
1343 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1345 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1348 gfn_t gfn = gpa >> PAGE_SHIFT;
1350 int offset = offset_in_page(gpa);
1353 while ((seg = next_segment(len, offset)) != 0) {
1354 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1365 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1367 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1369 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1371 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1373 gfn_t gfn = gpa >> PAGE_SHIFT;
1375 int offset = offset_in_page(gpa);
1378 while ((seg = next_segment(len, offset)) != 0) {
1379 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1388 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1390 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1392 struct kvm_memory_slot *memslot;
1394 gfn = unalias_gfn(kvm, gfn);
1395 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1396 if (memslot && memslot->dirty_bitmap) {
1397 unsigned long rel_gfn = gfn - memslot->base_gfn;
1400 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1401 set_bit(rel_gfn, memslot->dirty_bitmap);
1406 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1408 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1413 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1415 if (kvm_cpu_has_interrupt(vcpu) ||
1416 kvm_cpu_has_pending_timer(vcpu) ||
1417 kvm_arch_vcpu_runnable(vcpu)) {
1418 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1421 if (signal_pending(current))
1429 finish_wait(&vcpu->wq, &wait);
1432 void kvm_resched(struct kvm_vcpu *vcpu)
1434 if (!need_resched())
1438 EXPORT_SYMBOL_GPL(kvm_resched);
1440 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1442 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1445 if (vmf->pgoff == 0)
1446 page = virt_to_page(vcpu->run);
1448 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1449 page = virt_to_page(vcpu->arch.pio_data);
1451 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1452 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1453 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1456 return VM_FAULT_SIGBUS;
1462 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1463 .fault = kvm_vcpu_fault,
1466 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1468 vma->vm_ops = &kvm_vcpu_vm_ops;
1472 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1474 struct kvm_vcpu *vcpu = filp->private_data;
1476 kvm_put_kvm(vcpu->kvm);
1480 static const struct file_operations kvm_vcpu_fops = {
1481 .release = kvm_vcpu_release,
1482 .unlocked_ioctl = kvm_vcpu_ioctl,
1483 .compat_ioctl = kvm_vcpu_ioctl,
1484 .mmap = kvm_vcpu_mmap,
1488 * Allocates an inode for the vcpu.
1490 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1492 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1494 kvm_put_kvm(vcpu->kvm);
1499 * Creates some virtual cpus. Good luck creating more than one.
1501 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1504 struct kvm_vcpu *vcpu;
1509 vcpu = kvm_arch_vcpu_create(kvm, n);
1511 return PTR_ERR(vcpu);
1513 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1515 r = kvm_arch_vcpu_setup(vcpu);
1519 mutex_lock(&kvm->lock);
1520 if (kvm->vcpus[n]) {
1524 kvm->vcpus[n] = vcpu;
1525 mutex_unlock(&kvm->lock);
1527 /* Now it's all set up, let userspace reach it */
1529 r = create_vcpu_fd(vcpu);
1535 mutex_lock(&kvm->lock);
1536 kvm->vcpus[n] = NULL;
1538 mutex_unlock(&kvm->lock);
1539 kvm_arch_vcpu_destroy(vcpu);
1543 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1546 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1547 vcpu->sigset_active = 1;
1548 vcpu->sigset = *sigset;
1550 vcpu->sigset_active = 0;
1554 static long kvm_vcpu_ioctl(struct file *filp,
1555 unsigned int ioctl, unsigned long arg)
1557 struct kvm_vcpu *vcpu = filp->private_data;
1558 void __user *argp = (void __user *)arg;
1560 struct kvm_fpu *fpu = NULL;
1561 struct kvm_sregs *kvm_sregs = NULL;
1563 if (vcpu->kvm->mm != current->mm)
1570 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1572 case KVM_GET_REGS: {
1573 struct kvm_regs *kvm_regs;
1576 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1579 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1583 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1590 case KVM_SET_REGS: {
1591 struct kvm_regs *kvm_regs;
1594 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1598 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1600 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1608 case KVM_GET_SREGS: {
1609 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1613 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1617 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1622 case KVM_SET_SREGS: {
1623 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1628 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1630 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1636 case KVM_GET_MP_STATE: {
1637 struct kvm_mp_state mp_state;
1639 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1643 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1648 case KVM_SET_MP_STATE: {
1649 struct kvm_mp_state mp_state;
1652 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1654 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1660 case KVM_TRANSLATE: {
1661 struct kvm_translation tr;
1664 if (copy_from_user(&tr, argp, sizeof tr))
1666 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1670 if (copy_to_user(argp, &tr, sizeof tr))
1675 case KVM_DEBUG_GUEST: {
1676 struct kvm_debug_guest dbg;
1679 if (copy_from_user(&dbg, argp, sizeof dbg))
1681 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1687 case KVM_SET_SIGNAL_MASK: {
1688 struct kvm_signal_mask __user *sigmask_arg = argp;
1689 struct kvm_signal_mask kvm_sigmask;
1690 sigset_t sigset, *p;
1695 if (copy_from_user(&kvm_sigmask, argp,
1696 sizeof kvm_sigmask))
1699 if (kvm_sigmask.len != sizeof sigset)
1702 if (copy_from_user(&sigset, sigmask_arg->sigset,
1707 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1711 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1715 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1719 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1725 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1730 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1732 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1739 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1747 static long kvm_vm_ioctl(struct file *filp,
1748 unsigned int ioctl, unsigned long arg)
1750 struct kvm *kvm = filp->private_data;
1751 void __user *argp = (void __user *)arg;
1754 if (kvm->mm != current->mm)
1757 case KVM_CREATE_VCPU:
1758 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1762 case KVM_SET_USER_MEMORY_REGION: {
1763 struct kvm_userspace_memory_region kvm_userspace_mem;
1766 if (copy_from_user(&kvm_userspace_mem, argp,
1767 sizeof kvm_userspace_mem))
1770 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1775 case KVM_GET_DIRTY_LOG: {
1776 struct kvm_dirty_log log;
1779 if (copy_from_user(&log, argp, sizeof log))
1781 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1786 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1787 case KVM_REGISTER_COALESCED_MMIO: {
1788 struct kvm_coalesced_mmio_zone zone;
1790 if (copy_from_user(&zone, argp, sizeof zone))
1793 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1799 case KVM_UNREGISTER_COALESCED_MMIO: {
1800 struct kvm_coalesced_mmio_zone zone;
1802 if (copy_from_user(&zone, argp, sizeof zone))
1805 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1812 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1813 case KVM_ASSIGN_PCI_DEVICE: {
1814 struct kvm_assigned_pci_dev assigned_dev;
1817 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1819 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1824 case KVM_ASSIGN_IRQ: {
1825 struct kvm_assigned_irq assigned_irq;
1828 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1830 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1837 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1843 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1845 struct page *page[1];
1848 gfn_t gfn = vmf->pgoff;
1849 struct kvm *kvm = vma->vm_file->private_data;
1851 addr = gfn_to_hva(kvm, gfn);
1852 if (kvm_is_error_hva(addr))
1853 return VM_FAULT_SIGBUS;
1855 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1857 if (unlikely(npages != 1))
1858 return VM_FAULT_SIGBUS;
1860 vmf->page = page[0];
1864 static struct vm_operations_struct kvm_vm_vm_ops = {
1865 .fault = kvm_vm_fault,
1868 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1870 vma->vm_ops = &kvm_vm_vm_ops;
1874 static const struct file_operations kvm_vm_fops = {
1875 .release = kvm_vm_release,
1876 .unlocked_ioctl = kvm_vm_ioctl,
1877 .compat_ioctl = kvm_vm_ioctl,
1878 .mmap = kvm_vm_mmap,
1881 static int kvm_dev_ioctl_create_vm(void)
1886 kvm = kvm_create_vm();
1888 return PTR_ERR(kvm);
1889 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1896 static long kvm_dev_ioctl(struct file *filp,
1897 unsigned int ioctl, unsigned long arg)
1902 case KVM_GET_API_VERSION:
1906 r = KVM_API_VERSION;
1912 r = kvm_dev_ioctl_create_vm();
1914 case KVM_CHECK_EXTENSION:
1915 r = kvm_dev_ioctl_check_extension(arg);
1917 case KVM_GET_VCPU_MMAP_SIZE:
1921 r = PAGE_SIZE; /* struct kvm_run */
1923 r += PAGE_SIZE; /* pio data page */
1925 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1926 r += PAGE_SIZE; /* coalesced mmio ring page */
1929 case KVM_TRACE_ENABLE:
1930 case KVM_TRACE_PAUSE:
1931 case KVM_TRACE_DISABLE:
1932 r = kvm_trace_ioctl(ioctl, arg);
1935 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1941 static struct file_operations kvm_chardev_ops = {
1942 .unlocked_ioctl = kvm_dev_ioctl,
1943 .compat_ioctl = kvm_dev_ioctl,
1946 static struct miscdevice kvm_dev = {
1952 static void hardware_enable(void *junk)
1954 int cpu = raw_smp_processor_id();
1956 if (cpu_isset(cpu, cpus_hardware_enabled))
1958 cpu_set(cpu, cpus_hardware_enabled);
1959 kvm_arch_hardware_enable(NULL);
1962 static void hardware_disable(void *junk)
1964 int cpu = raw_smp_processor_id();
1966 if (!cpu_isset(cpu, cpus_hardware_enabled))
1968 cpu_clear(cpu, cpus_hardware_enabled);
1969 kvm_arch_hardware_disable(NULL);
1972 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1977 val &= ~CPU_TASKS_FROZEN;
1980 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1982 hardware_disable(NULL);
1984 case CPU_UP_CANCELED:
1985 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1987 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1990 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1992 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1999 asmlinkage void kvm_handle_fault_on_reboot(void)
2002 /* spin while reset goes on */
2005 /* Fault while not rebooting. We want the trace. */
2008 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2010 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2013 if (val == SYS_RESTART) {
2015 * Some (well, at least mine) BIOSes hang on reboot if
2018 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2019 kvm_rebooting = true;
2020 on_each_cpu(hardware_disable, NULL, 1);
2025 static struct notifier_block kvm_reboot_notifier = {
2026 .notifier_call = kvm_reboot,
2030 void kvm_io_bus_init(struct kvm_io_bus *bus)
2032 memset(bus, 0, sizeof(*bus));
2035 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2039 for (i = 0; i < bus->dev_count; i++) {
2040 struct kvm_io_device *pos = bus->devs[i];
2042 kvm_iodevice_destructor(pos);
2046 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2047 gpa_t addr, int len, int is_write)
2051 for (i = 0; i < bus->dev_count; i++) {
2052 struct kvm_io_device *pos = bus->devs[i];
2054 if (pos->in_range(pos, addr, len, is_write))
2061 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2063 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2065 bus->devs[bus->dev_count++] = dev;
2068 static struct notifier_block kvm_cpu_notifier = {
2069 .notifier_call = kvm_cpu_hotplug,
2070 .priority = 20, /* must be > scheduler priority */
2073 static int vm_stat_get(void *_offset, u64 *val)
2075 unsigned offset = (long)_offset;
2079 spin_lock(&kvm_lock);
2080 list_for_each_entry(kvm, &vm_list, vm_list)
2081 *val += *(u32 *)((void *)kvm + offset);
2082 spin_unlock(&kvm_lock);
2086 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2088 static int vcpu_stat_get(void *_offset, u64 *val)
2090 unsigned offset = (long)_offset;
2092 struct kvm_vcpu *vcpu;
2096 spin_lock(&kvm_lock);
2097 list_for_each_entry(kvm, &vm_list, vm_list)
2098 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2099 vcpu = kvm->vcpus[i];
2101 *val += *(u32 *)((void *)vcpu + offset);
2103 spin_unlock(&kvm_lock);
2107 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2109 static struct file_operations *stat_fops[] = {
2110 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2111 [KVM_STAT_VM] = &vm_stat_fops,
2114 static void kvm_init_debug(void)
2116 struct kvm_stats_debugfs_item *p;
2118 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2119 for (p = debugfs_entries; p->name; ++p)
2120 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2121 (void *)(long)p->offset,
2122 stat_fops[p->kind]);
2125 static void kvm_exit_debug(void)
2127 struct kvm_stats_debugfs_item *p;
2129 for (p = debugfs_entries; p->name; ++p)
2130 debugfs_remove(p->dentry);
2131 debugfs_remove(kvm_debugfs_dir);
2134 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2136 hardware_disable(NULL);
2140 static int kvm_resume(struct sys_device *dev)
2142 hardware_enable(NULL);
2146 static struct sysdev_class kvm_sysdev_class = {
2148 .suspend = kvm_suspend,
2149 .resume = kvm_resume,
2152 static struct sys_device kvm_sysdev = {
2154 .cls = &kvm_sysdev_class,
2157 struct page *bad_page;
2161 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2163 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2166 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2168 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2170 kvm_arch_vcpu_load(vcpu, cpu);
2173 static void kvm_sched_out(struct preempt_notifier *pn,
2174 struct task_struct *next)
2176 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2178 kvm_arch_vcpu_put(vcpu);
2181 int kvm_init(void *opaque, unsigned int vcpu_size,
2182 struct module *module)
2189 r = kvm_arch_init(opaque);
2193 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2195 if (bad_page == NULL) {
2200 bad_pfn = page_to_pfn(bad_page);
2202 r = kvm_arch_hardware_setup();
2206 for_each_online_cpu(cpu) {
2207 smp_call_function_single(cpu,
2208 kvm_arch_check_processor_compat,
2214 on_each_cpu(hardware_enable, NULL, 1);
2215 r = register_cpu_notifier(&kvm_cpu_notifier);
2218 register_reboot_notifier(&kvm_reboot_notifier);
2220 r = sysdev_class_register(&kvm_sysdev_class);
2224 r = sysdev_register(&kvm_sysdev);
2228 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2229 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2230 __alignof__(struct kvm_vcpu),
2232 if (!kvm_vcpu_cache) {
2237 kvm_chardev_ops.owner = module;
2239 r = misc_register(&kvm_dev);
2241 printk(KERN_ERR "kvm: misc device register failed\n");
2245 kvm_preempt_ops.sched_in = kvm_sched_in;
2246 kvm_preempt_ops.sched_out = kvm_sched_out;
2254 kmem_cache_destroy(kvm_vcpu_cache);
2256 sysdev_unregister(&kvm_sysdev);
2258 sysdev_class_unregister(&kvm_sysdev_class);
2260 unregister_reboot_notifier(&kvm_reboot_notifier);
2261 unregister_cpu_notifier(&kvm_cpu_notifier);
2263 on_each_cpu(hardware_disable, NULL, 1);
2265 kvm_arch_hardware_unsetup();
2267 __free_page(bad_page);
2274 EXPORT_SYMBOL_GPL(kvm_init);
2278 kvm_trace_cleanup();
2279 misc_deregister(&kvm_dev);
2280 kmem_cache_destroy(kvm_vcpu_cache);
2281 sysdev_unregister(&kvm_sysdev);
2282 sysdev_class_unregister(&kvm_sysdev_class);
2283 unregister_reboot_notifier(&kvm_reboot_notifier);
2284 unregister_cpu_notifier(&kvm_cpu_notifier);
2285 on_each_cpu(hardware_disable, NULL, 1);
2286 kvm_arch_hardware_unsetup();
2289 __free_page(bad_page);
2291 EXPORT_SYMBOL_GPL(kvm_exit);