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);
214 if (assigned_dev->irq_source_id != -1)
215 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
216 assigned_dev->irq_source_id = -1;
218 if (cancel_work_sync(&assigned_dev->interrupt_work))
219 /* We had pending work. That means we will have to take
220 * care of kvm_put_kvm.
224 pci_reset_function(assigned_dev->dev);
226 pci_release_regions(assigned_dev->dev);
227 pci_disable_device(assigned_dev->dev);
228 pci_dev_put(assigned_dev->dev);
230 list_del(&assigned_dev->list);
234 void kvm_free_all_assigned_devices(struct kvm *kvm)
236 struct list_head *ptr, *ptr2;
237 struct kvm_assigned_dev_kernel *assigned_dev;
239 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
240 assigned_dev = list_entry(ptr,
241 struct kvm_assigned_dev_kernel,
244 kvm_free_assigned_device(kvm, assigned_dev);
248 static int assigned_device_update_intx(struct kvm *kvm,
249 struct kvm_assigned_dev_kernel *adev,
250 struct kvm_assigned_irq *airq)
252 adev->guest_irq = airq->guest_irq;
253 adev->ack_notifier.gsi = airq->guest_irq;
255 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
258 if (irqchip_in_kernel(kvm)) {
260 adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) {
261 free_irq(adev->host_irq, (void *)kvm);
262 pci_disable_msi(adev->dev);
265 if (!capable(CAP_SYS_RAWIO))
269 adev->host_irq = airq->host_irq;
271 adev->host_irq = adev->dev->irq;
273 /* Even though this is PCI, we don't want to use shared
274 * interrupts. Sharing host devices with guest-assigned devices
275 * on the same interrupt line is not a happy situation: there
276 * are going to be long delays in accepting, acking, etc.
278 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
279 0, "kvm_assigned_intx_device", (void *)adev))
283 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
284 KVM_ASSIGNED_DEV_HOST_INTX;
289 static int assigned_device_update_msi(struct kvm *kvm,
290 struct kvm_assigned_dev_kernel *adev,
291 struct kvm_assigned_irq *airq)
295 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
296 /* x86 don't care upper address of guest msi message addr */
297 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
298 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
299 adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
300 adev->guest_msi.data = airq->guest_msi.data;
301 adev->ack_notifier.gsi = -1;
302 } else if (msi2intx) {
303 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
304 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
305 adev->guest_irq = airq->guest_irq;
306 adev->ack_notifier.gsi = airq->guest_irq;
309 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
312 if (irqchip_in_kernel(kvm)) {
314 if (adev->irq_requested_type &
315 KVM_ASSIGNED_DEV_HOST_INTX)
316 free_irq(adev->host_irq, (void *)adev);
318 r = pci_enable_msi(adev->dev);
323 adev->host_irq = adev->dev->irq;
324 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
325 "kvm_assigned_msi_device", (void *)adev))
330 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
332 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
337 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
338 struct kvm_assigned_irq
342 struct kvm_assigned_dev_kernel *match;
344 mutex_lock(&kvm->lock);
346 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
347 assigned_irq->assigned_dev_id);
349 mutex_unlock(&kvm->lock);
353 if (!match->irq_requested_type) {
354 INIT_WORK(&match->interrupt_work,
355 kvm_assigned_dev_interrupt_work_handler);
356 if (irqchip_in_kernel(kvm)) {
357 /* Register ack nofitier */
358 match->ack_notifier.gsi = -1;
359 match->ack_notifier.irq_acked =
360 kvm_assigned_dev_ack_irq;
361 kvm_register_irq_ack_notifier(kvm,
362 &match->ack_notifier);
364 /* Request IRQ source ID */
365 r = kvm_request_irq_source_id(kvm);
369 match->irq_source_id = r;
372 /* Determine host device irq type, we can know the
373 * result from dev->msi_enabled */
375 pci_enable_msi(match->dev);
381 (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI)) ||
382 (msi2intx && match->dev->msi_enabled)) {
384 r = assigned_device_update_msi(kvm, match, assigned_irq);
386 printk(KERN_WARNING "kvm: failed to enable "
393 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
394 /* Host device IRQ 0 means don't support INTx */
397 "kvm: wait device to enable MSI!\n");
401 "kvm: failed to enable MSI device!\n");
406 /* Non-sharing INTx mode */
407 r = assigned_device_update_intx(kvm, match, assigned_irq);
409 printk(KERN_WARNING "kvm: failed to enable "
415 mutex_unlock(&kvm->lock);
418 mutex_unlock(&kvm->lock);
419 kvm_free_assigned_device(kvm, match);
423 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
424 struct kvm_assigned_pci_dev *assigned_dev)
427 struct kvm_assigned_dev_kernel *match;
430 mutex_lock(&kvm->lock);
432 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
433 assigned_dev->assigned_dev_id);
435 /* device already assigned */
440 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
442 printk(KERN_INFO "%s: Couldn't allocate memory\n",
447 dev = pci_get_bus_and_slot(assigned_dev->busnr,
448 assigned_dev->devfn);
450 printk(KERN_INFO "%s: host device not found\n", __func__);
454 if (pci_enable_device(dev)) {
455 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
459 r = pci_request_regions(dev, "kvm_assigned_device");
461 printk(KERN_INFO "%s: Could not get access to device regions\n",
466 pci_reset_function(dev);
468 match->assigned_dev_id = assigned_dev->assigned_dev_id;
469 match->host_busnr = assigned_dev->busnr;
470 match->host_devfn = assigned_dev->devfn;
472 match->irq_source_id = -1;
475 list_add(&match->list, &kvm->arch.assigned_dev_head);
477 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
478 r = kvm_iommu_map_guest(kvm, match);
484 mutex_unlock(&kvm->lock);
487 list_del(&match->list);
488 pci_release_regions(dev);
490 pci_disable_device(dev);
495 mutex_unlock(&kvm->lock);
500 static inline int valid_vcpu(int n)
502 return likely(n >= 0 && n < KVM_MAX_VCPUS);
505 inline int kvm_is_mmio_pfn(pfn_t pfn)
508 return PageReserved(pfn_to_page(pfn));
514 * Switches to specified vcpu, until a matching vcpu_put()
516 void vcpu_load(struct kvm_vcpu *vcpu)
520 mutex_lock(&vcpu->mutex);
522 preempt_notifier_register(&vcpu->preempt_notifier);
523 kvm_arch_vcpu_load(vcpu, cpu);
527 void vcpu_put(struct kvm_vcpu *vcpu)
530 kvm_arch_vcpu_put(vcpu);
531 preempt_notifier_unregister(&vcpu->preempt_notifier);
533 mutex_unlock(&vcpu->mutex);
536 static void ack_flush(void *_completed)
540 void kvm_flush_remote_tlbs(struct kvm *kvm)
544 struct kvm_vcpu *vcpu;
548 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
549 vcpu = kvm->vcpus[i];
552 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
555 if (cpu != -1 && cpu != me)
558 if (cpus_empty(cpus))
560 ++kvm->stat.remote_tlb_flush;
561 smp_call_function_mask(cpus, ack_flush, NULL, 1);
566 void kvm_reload_remote_mmus(struct kvm *kvm)
570 struct kvm_vcpu *vcpu;
574 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
575 vcpu = kvm->vcpus[i];
578 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
581 if (cpu != -1 && cpu != me)
584 if (cpus_empty(cpus))
586 smp_call_function_mask(cpus, ack_flush, NULL, 1);
592 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
597 mutex_init(&vcpu->mutex);
601 init_waitqueue_head(&vcpu->wq);
603 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
608 vcpu->run = page_address(page);
610 r = kvm_arch_vcpu_init(vcpu);
616 free_page((unsigned long)vcpu->run);
620 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
622 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
624 kvm_arch_vcpu_uninit(vcpu);
625 free_page((unsigned long)vcpu->run);
627 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
629 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
630 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
632 return container_of(mn, struct kvm, mmu_notifier);
635 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
636 struct mm_struct *mm,
637 unsigned long address)
639 struct kvm *kvm = mmu_notifier_to_kvm(mn);
643 * When ->invalidate_page runs, the linux pte has been zapped
644 * already but the page is still allocated until
645 * ->invalidate_page returns. So if we increase the sequence
646 * here the kvm page fault will notice if the spte can't be
647 * established because the page is going to be freed. If
648 * instead the kvm page fault establishes the spte before
649 * ->invalidate_page runs, kvm_unmap_hva will release it
652 * The sequence increase only need to be seen at spin_unlock
653 * time, and not at spin_lock time.
655 * Increasing the sequence after the spin_unlock would be
656 * unsafe because the kvm page fault could then establish the
657 * pte after kvm_unmap_hva returned, without noticing the page
658 * is going to be freed.
660 spin_lock(&kvm->mmu_lock);
661 kvm->mmu_notifier_seq++;
662 need_tlb_flush = kvm_unmap_hva(kvm, address);
663 spin_unlock(&kvm->mmu_lock);
665 /* we've to flush the tlb before the pages can be freed */
667 kvm_flush_remote_tlbs(kvm);
671 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
672 struct mm_struct *mm,
676 struct kvm *kvm = mmu_notifier_to_kvm(mn);
677 int need_tlb_flush = 0;
679 spin_lock(&kvm->mmu_lock);
681 * The count increase must become visible at unlock time as no
682 * spte can be established without taking the mmu_lock and
683 * count is also read inside the mmu_lock critical section.
685 kvm->mmu_notifier_count++;
686 for (; start < end; start += PAGE_SIZE)
687 need_tlb_flush |= kvm_unmap_hva(kvm, start);
688 spin_unlock(&kvm->mmu_lock);
690 /* we've to flush the tlb before the pages can be freed */
692 kvm_flush_remote_tlbs(kvm);
695 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
696 struct mm_struct *mm,
700 struct kvm *kvm = mmu_notifier_to_kvm(mn);
702 spin_lock(&kvm->mmu_lock);
704 * This sequence increase will notify the kvm page fault that
705 * the page that is going to be mapped in the spte could have
708 kvm->mmu_notifier_seq++;
710 * The above sequence increase must be visible before the
711 * below count decrease but both values are read by the kvm
712 * page fault under mmu_lock spinlock so we don't need to add
713 * a smb_wmb() here in between the two.
715 kvm->mmu_notifier_count--;
716 spin_unlock(&kvm->mmu_lock);
718 BUG_ON(kvm->mmu_notifier_count < 0);
721 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
722 struct mm_struct *mm,
723 unsigned long address)
725 struct kvm *kvm = mmu_notifier_to_kvm(mn);
728 spin_lock(&kvm->mmu_lock);
729 young = kvm_age_hva(kvm, address);
730 spin_unlock(&kvm->mmu_lock);
733 kvm_flush_remote_tlbs(kvm);
738 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
739 .invalidate_page = kvm_mmu_notifier_invalidate_page,
740 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
741 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
742 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
744 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
746 static struct kvm *kvm_create_vm(void)
748 struct kvm *kvm = kvm_arch_create_vm();
749 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
756 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
757 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
760 return ERR_PTR(-ENOMEM);
762 kvm->coalesced_mmio_ring =
763 (struct kvm_coalesced_mmio_ring *)page_address(page);
766 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
769 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
770 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
772 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
781 kvm->mm = current->mm;
782 atomic_inc(&kvm->mm->mm_count);
783 spin_lock_init(&kvm->mmu_lock);
784 kvm_io_bus_init(&kvm->pio_bus);
785 mutex_init(&kvm->lock);
786 kvm_io_bus_init(&kvm->mmio_bus);
787 init_rwsem(&kvm->slots_lock);
788 atomic_set(&kvm->users_count, 1);
789 spin_lock(&kvm_lock);
790 list_add(&kvm->vm_list, &vm_list);
791 spin_unlock(&kvm_lock);
792 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
793 kvm_coalesced_mmio_init(kvm);
800 * Free any memory in @free but not in @dont.
802 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
803 struct kvm_memory_slot *dont)
805 if (!dont || free->rmap != dont->rmap)
808 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
809 vfree(free->dirty_bitmap);
811 if (!dont || free->lpage_info != dont->lpage_info)
812 vfree(free->lpage_info);
815 free->dirty_bitmap = NULL;
817 free->lpage_info = NULL;
820 void kvm_free_physmem(struct kvm *kvm)
824 for (i = 0; i < kvm->nmemslots; ++i)
825 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
828 static void kvm_destroy_vm(struct kvm *kvm)
830 struct mm_struct *mm = kvm->mm;
832 spin_lock(&kvm_lock);
833 list_del(&kvm->vm_list);
834 spin_unlock(&kvm_lock);
835 kvm_io_bus_destroy(&kvm->pio_bus);
836 kvm_io_bus_destroy(&kvm->mmio_bus);
837 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
838 if (kvm->coalesced_mmio_ring != NULL)
839 free_page((unsigned long)kvm->coalesced_mmio_ring);
841 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
842 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
844 kvm_arch_destroy_vm(kvm);
848 void kvm_get_kvm(struct kvm *kvm)
850 atomic_inc(&kvm->users_count);
852 EXPORT_SYMBOL_GPL(kvm_get_kvm);
854 void kvm_put_kvm(struct kvm *kvm)
856 if (atomic_dec_and_test(&kvm->users_count))
859 EXPORT_SYMBOL_GPL(kvm_put_kvm);
862 static int kvm_vm_release(struct inode *inode, struct file *filp)
864 struct kvm *kvm = filp->private_data;
871 * Allocate some memory and give it an address in the guest physical address
874 * Discontiguous memory is allowed, mostly for framebuffers.
876 * Must be called holding mmap_sem for write.
878 int __kvm_set_memory_region(struct kvm *kvm,
879 struct kvm_userspace_memory_region *mem,
884 unsigned long npages;
886 struct kvm_memory_slot *memslot;
887 struct kvm_memory_slot old, new;
890 /* General sanity checks */
891 if (mem->memory_size & (PAGE_SIZE - 1))
893 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
895 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
897 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
899 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
902 memslot = &kvm->memslots[mem->slot];
903 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
904 npages = mem->memory_size >> PAGE_SHIFT;
907 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
909 new = old = *memslot;
911 new.base_gfn = base_gfn;
913 new.flags = mem->flags;
915 /* Disallow changing a memory slot's size. */
917 if (npages && old.npages && npages != old.npages)
920 /* Check for overlaps */
922 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
923 struct kvm_memory_slot *s = &kvm->memslots[i];
927 if (!((base_gfn + npages <= s->base_gfn) ||
928 (base_gfn >= s->base_gfn + s->npages)))
932 /* Free page dirty bitmap if unneeded */
933 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
934 new.dirty_bitmap = NULL;
938 /* Allocate if a slot is being created */
940 if (npages && !new.rmap) {
941 new.rmap = vmalloc(npages * sizeof(struct page *));
946 memset(new.rmap, 0, npages * sizeof(*new.rmap));
948 new.user_alloc = user_alloc;
950 * hva_to_rmmap() serialzies with the mmu_lock and to be
951 * safe it has to ignore memslots with !user_alloc &&
955 new.userspace_addr = mem->userspace_addr;
957 new.userspace_addr = 0;
959 if (npages && !new.lpage_info) {
960 int largepages = npages / KVM_PAGES_PER_HPAGE;
961 if (npages % KVM_PAGES_PER_HPAGE)
963 if (base_gfn % KVM_PAGES_PER_HPAGE)
966 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
971 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
973 if (base_gfn % KVM_PAGES_PER_HPAGE)
974 new.lpage_info[0].write_count = 1;
975 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
976 new.lpage_info[largepages-1].write_count = 1;
979 /* Allocate page dirty bitmap if needed */
980 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
981 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
983 new.dirty_bitmap = vmalloc(dirty_bytes);
984 if (!new.dirty_bitmap)
986 memset(new.dirty_bitmap, 0, dirty_bytes);
988 #endif /* not defined CONFIG_S390 */
991 kvm_arch_flush_shadow(kvm);
993 spin_lock(&kvm->mmu_lock);
994 if (mem->slot >= kvm->nmemslots)
995 kvm->nmemslots = mem->slot + 1;
998 spin_unlock(&kvm->mmu_lock);
1000 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1002 spin_lock(&kvm->mmu_lock);
1004 spin_unlock(&kvm->mmu_lock);
1008 kvm_free_physmem_slot(&old, &new);
1010 /* map the pages in iommu page table */
1011 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1018 kvm_free_physmem_slot(&new, &old);
1023 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1025 int kvm_set_memory_region(struct kvm *kvm,
1026 struct kvm_userspace_memory_region *mem,
1031 down_write(&kvm->slots_lock);
1032 r = __kvm_set_memory_region(kvm, mem, user_alloc);
1033 up_write(&kvm->slots_lock);
1036 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1038 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1040 kvm_userspace_memory_region *mem,
1043 if (mem->slot >= KVM_MEMORY_SLOTS)
1045 return kvm_set_memory_region(kvm, mem, user_alloc);
1048 int kvm_get_dirty_log(struct kvm *kvm,
1049 struct kvm_dirty_log *log, int *is_dirty)
1051 struct kvm_memory_slot *memslot;
1054 unsigned long any = 0;
1057 if (log->slot >= KVM_MEMORY_SLOTS)
1060 memslot = &kvm->memslots[log->slot];
1062 if (!memslot->dirty_bitmap)
1065 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1067 for (i = 0; !any && i < n/sizeof(long); ++i)
1068 any = memslot->dirty_bitmap[i];
1071 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1082 int is_error_page(struct page *page)
1084 return page == bad_page;
1086 EXPORT_SYMBOL_GPL(is_error_page);
1088 int is_error_pfn(pfn_t pfn)
1090 return pfn == bad_pfn;
1092 EXPORT_SYMBOL_GPL(is_error_pfn);
1094 static inline unsigned long bad_hva(void)
1099 int kvm_is_error_hva(unsigned long addr)
1101 return addr == bad_hva();
1103 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1105 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1109 for (i = 0; i < kvm->nmemslots; ++i) {
1110 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1112 if (gfn >= memslot->base_gfn
1113 && gfn < memslot->base_gfn + memslot->npages)
1118 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1120 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1122 gfn = unalias_gfn(kvm, gfn);
1123 return gfn_to_memslot_unaliased(kvm, gfn);
1126 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1130 gfn = unalias_gfn(kvm, gfn);
1131 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1132 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1134 if (gfn >= memslot->base_gfn
1135 && gfn < memslot->base_gfn + memslot->npages)
1140 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1142 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1144 struct kvm_memory_slot *slot;
1146 gfn = unalias_gfn(kvm, gfn);
1147 slot = gfn_to_memslot_unaliased(kvm, gfn);
1150 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1152 EXPORT_SYMBOL_GPL(gfn_to_hva);
1154 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1156 struct page *page[1];
1163 addr = gfn_to_hva(kvm, gfn);
1164 if (kvm_is_error_hva(addr)) {
1166 return page_to_pfn(bad_page);
1169 npages = get_user_pages_fast(addr, 1, 1, page);
1171 if (unlikely(npages != 1)) {
1172 struct vm_area_struct *vma;
1174 down_read(¤t->mm->mmap_sem);
1175 vma = find_vma(current->mm, addr);
1177 if (vma == NULL || addr < vma->vm_start ||
1178 !(vma->vm_flags & VM_PFNMAP)) {
1179 up_read(¤t->mm->mmap_sem);
1181 return page_to_pfn(bad_page);
1184 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1185 up_read(¤t->mm->mmap_sem);
1186 BUG_ON(!kvm_is_mmio_pfn(pfn));
1188 pfn = page_to_pfn(page[0]);
1193 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1195 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1199 pfn = gfn_to_pfn(kvm, gfn);
1200 if (!kvm_is_mmio_pfn(pfn))
1201 return pfn_to_page(pfn);
1203 WARN_ON(kvm_is_mmio_pfn(pfn));
1209 EXPORT_SYMBOL_GPL(gfn_to_page);
1211 void kvm_release_page_clean(struct page *page)
1213 kvm_release_pfn_clean(page_to_pfn(page));
1215 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1217 void kvm_release_pfn_clean(pfn_t pfn)
1219 if (!kvm_is_mmio_pfn(pfn))
1220 put_page(pfn_to_page(pfn));
1222 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1224 void kvm_release_page_dirty(struct page *page)
1226 kvm_release_pfn_dirty(page_to_pfn(page));
1228 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1230 void kvm_release_pfn_dirty(pfn_t pfn)
1232 kvm_set_pfn_dirty(pfn);
1233 kvm_release_pfn_clean(pfn);
1235 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1237 void kvm_set_page_dirty(struct page *page)
1239 kvm_set_pfn_dirty(page_to_pfn(page));
1241 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1243 void kvm_set_pfn_dirty(pfn_t pfn)
1245 if (!kvm_is_mmio_pfn(pfn)) {
1246 struct page *page = pfn_to_page(pfn);
1247 if (!PageReserved(page))
1251 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1253 void kvm_set_pfn_accessed(pfn_t pfn)
1255 if (!kvm_is_mmio_pfn(pfn))
1256 mark_page_accessed(pfn_to_page(pfn));
1258 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1260 void kvm_get_pfn(pfn_t pfn)
1262 if (!kvm_is_mmio_pfn(pfn))
1263 get_page(pfn_to_page(pfn));
1265 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1267 static int next_segment(unsigned long len, int offset)
1269 if (len > PAGE_SIZE - offset)
1270 return PAGE_SIZE - offset;
1275 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1281 addr = gfn_to_hva(kvm, gfn);
1282 if (kvm_is_error_hva(addr))
1284 r = copy_from_user(data, (void __user *)addr + offset, len);
1289 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1291 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1293 gfn_t gfn = gpa >> PAGE_SHIFT;
1295 int offset = offset_in_page(gpa);
1298 while ((seg = next_segment(len, offset)) != 0) {
1299 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1309 EXPORT_SYMBOL_GPL(kvm_read_guest);
1311 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1316 gfn_t gfn = gpa >> PAGE_SHIFT;
1317 int offset = offset_in_page(gpa);
1319 addr = gfn_to_hva(kvm, gfn);
1320 if (kvm_is_error_hva(addr))
1322 pagefault_disable();
1323 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1329 EXPORT_SYMBOL(kvm_read_guest_atomic);
1331 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1332 int offset, int len)
1337 addr = gfn_to_hva(kvm, gfn);
1338 if (kvm_is_error_hva(addr))
1340 r = copy_to_user((void __user *)addr + offset, data, len);
1343 mark_page_dirty(kvm, gfn);
1346 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1348 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1351 gfn_t gfn = gpa >> PAGE_SHIFT;
1353 int offset = offset_in_page(gpa);
1356 while ((seg = next_segment(len, offset)) != 0) {
1357 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1368 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1370 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1372 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1374 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1376 gfn_t gfn = gpa >> PAGE_SHIFT;
1378 int offset = offset_in_page(gpa);
1381 while ((seg = next_segment(len, offset)) != 0) {
1382 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1391 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1393 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1395 struct kvm_memory_slot *memslot;
1397 gfn = unalias_gfn(kvm, gfn);
1398 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1399 if (memslot && memslot->dirty_bitmap) {
1400 unsigned long rel_gfn = gfn - memslot->base_gfn;
1403 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1404 set_bit(rel_gfn, memslot->dirty_bitmap);
1409 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1411 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1416 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1418 if (kvm_cpu_has_interrupt(vcpu) ||
1419 kvm_cpu_has_pending_timer(vcpu) ||
1420 kvm_arch_vcpu_runnable(vcpu)) {
1421 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1424 if (signal_pending(current))
1432 finish_wait(&vcpu->wq, &wait);
1435 void kvm_resched(struct kvm_vcpu *vcpu)
1437 if (!need_resched())
1441 EXPORT_SYMBOL_GPL(kvm_resched);
1443 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1445 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1448 if (vmf->pgoff == 0)
1449 page = virt_to_page(vcpu->run);
1451 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1452 page = virt_to_page(vcpu->arch.pio_data);
1454 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1455 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1456 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1459 return VM_FAULT_SIGBUS;
1465 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1466 .fault = kvm_vcpu_fault,
1469 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1471 vma->vm_ops = &kvm_vcpu_vm_ops;
1475 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1477 struct kvm_vcpu *vcpu = filp->private_data;
1479 kvm_put_kvm(vcpu->kvm);
1483 static const struct file_operations kvm_vcpu_fops = {
1484 .release = kvm_vcpu_release,
1485 .unlocked_ioctl = kvm_vcpu_ioctl,
1486 .compat_ioctl = kvm_vcpu_ioctl,
1487 .mmap = kvm_vcpu_mmap,
1491 * Allocates an inode for the vcpu.
1493 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1495 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1497 kvm_put_kvm(vcpu->kvm);
1502 * Creates some virtual cpus. Good luck creating more than one.
1504 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1507 struct kvm_vcpu *vcpu;
1512 vcpu = kvm_arch_vcpu_create(kvm, n);
1514 return PTR_ERR(vcpu);
1516 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1518 r = kvm_arch_vcpu_setup(vcpu);
1522 mutex_lock(&kvm->lock);
1523 if (kvm->vcpus[n]) {
1527 kvm->vcpus[n] = vcpu;
1528 mutex_unlock(&kvm->lock);
1530 /* Now it's all set up, let userspace reach it */
1532 r = create_vcpu_fd(vcpu);
1538 mutex_lock(&kvm->lock);
1539 kvm->vcpus[n] = NULL;
1541 mutex_unlock(&kvm->lock);
1542 kvm_arch_vcpu_destroy(vcpu);
1546 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1549 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1550 vcpu->sigset_active = 1;
1551 vcpu->sigset = *sigset;
1553 vcpu->sigset_active = 0;
1557 static long kvm_vcpu_ioctl(struct file *filp,
1558 unsigned int ioctl, unsigned long arg)
1560 struct kvm_vcpu *vcpu = filp->private_data;
1561 void __user *argp = (void __user *)arg;
1563 struct kvm_fpu *fpu = NULL;
1564 struct kvm_sregs *kvm_sregs = NULL;
1566 if (vcpu->kvm->mm != current->mm)
1573 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1575 case KVM_GET_REGS: {
1576 struct kvm_regs *kvm_regs;
1579 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1582 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1586 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1593 case KVM_SET_REGS: {
1594 struct kvm_regs *kvm_regs;
1597 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1601 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1603 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1611 case KVM_GET_SREGS: {
1612 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1616 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1620 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1625 case KVM_SET_SREGS: {
1626 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1631 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1633 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1639 case KVM_GET_MP_STATE: {
1640 struct kvm_mp_state mp_state;
1642 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1646 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1651 case KVM_SET_MP_STATE: {
1652 struct kvm_mp_state mp_state;
1655 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1657 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1663 case KVM_TRANSLATE: {
1664 struct kvm_translation tr;
1667 if (copy_from_user(&tr, argp, sizeof tr))
1669 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1673 if (copy_to_user(argp, &tr, sizeof tr))
1678 case KVM_DEBUG_GUEST: {
1679 struct kvm_debug_guest dbg;
1682 if (copy_from_user(&dbg, argp, sizeof dbg))
1684 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1690 case KVM_SET_SIGNAL_MASK: {
1691 struct kvm_signal_mask __user *sigmask_arg = argp;
1692 struct kvm_signal_mask kvm_sigmask;
1693 sigset_t sigset, *p;
1698 if (copy_from_user(&kvm_sigmask, argp,
1699 sizeof kvm_sigmask))
1702 if (kvm_sigmask.len != sizeof sigset)
1705 if (copy_from_user(&sigset, sigmask_arg->sigset,
1710 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1714 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1718 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1722 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1728 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1733 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1735 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1742 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1750 static long kvm_vm_ioctl(struct file *filp,
1751 unsigned int ioctl, unsigned long arg)
1753 struct kvm *kvm = filp->private_data;
1754 void __user *argp = (void __user *)arg;
1757 if (kvm->mm != current->mm)
1760 case KVM_CREATE_VCPU:
1761 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1765 case KVM_SET_USER_MEMORY_REGION: {
1766 struct kvm_userspace_memory_region kvm_userspace_mem;
1769 if (copy_from_user(&kvm_userspace_mem, argp,
1770 sizeof kvm_userspace_mem))
1773 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1778 case KVM_GET_DIRTY_LOG: {
1779 struct kvm_dirty_log log;
1782 if (copy_from_user(&log, argp, sizeof log))
1784 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1789 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1790 case KVM_REGISTER_COALESCED_MMIO: {
1791 struct kvm_coalesced_mmio_zone zone;
1793 if (copy_from_user(&zone, argp, sizeof zone))
1796 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1802 case KVM_UNREGISTER_COALESCED_MMIO: {
1803 struct kvm_coalesced_mmio_zone zone;
1805 if (copy_from_user(&zone, argp, sizeof zone))
1808 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1815 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1816 case KVM_ASSIGN_PCI_DEVICE: {
1817 struct kvm_assigned_pci_dev assigned_dev;
1820 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1822 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1827 case KVM_ASSIGN_IRQ: {
1828 struct kvm_assigned_irq assigned_irq;
1831 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1833 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1840 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1846 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1848 struct page *page[1];
1851 gfn_t gfn = vmf->pgoff;
1852 struct kvm *kvm = vma->vm_file->private_data;
1854 addr = gfn_to_hva(kvm, gfn);
1855 if (kvm_is_error_hva(addr))
1856 return VM_FAULT_SIGBUS;
1858 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1860 if (unlikely(npages != 1))
1861 return VM_FAULT_SIGBUS;
1863 vmf->page = page[0];
1867 static struct vm_operations_struct kvm_vm_vm_ops = {
1868 .fault = kvm_vm_fault,
1871 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1873 vma->vm_ops = &kvm_vm_vm_ops;
1877 static const struct file_operations kvm_vm_fops = {
1878 .release = kvm_vm_release,
1879 .unlocked_ioctl = kvm_vm_ioctl,
1880 .compat_ioctl = kvm_vm_ioctl,
1881 .mmap = kvm_vm_mmap,
1884 static int kvm_dev_ioctl_create_vm(void)
1889 kvm = kvm_create_vm();
1891 return PTR_ERR(kvm);
1892 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1899 static long kvm_dev_ioctl(struct file *filp,
1900 unsigned int ioctl, unsigned long arg)
1905 case KVM_GET_API_VERSION:
1909 r = KVM_API_VERSION;
1915 r = kvm_dev_ioctl_create_vm();
1917 case KVM_CHECK_EXTENSION:
1918 r = kvm_dev_ioctl_check_extension(arg);
1920 case KVM_GET_VCPU_MMAP_SIZE:
1924 r = PAGE_SIZE; /* struct kvm_run */
1926 r += PAGE_SIZE; /* pio data page */
1928 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1929 r += PAGE_SIZE; /* coalesced mmio ring page */
1932 case KVM_TRACE_ENABLE:
1933 case KVM_TRACE_PAUSE:
1934 case KVM_TRACE_DISABLE:
1935 r = kvm_trace_ioctl(ioctl, arg);
1938 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1944 static struct file_operations kvm_chardev_ops = {
1945 .unlocked_ioctl = kvm_dev_ioctl,
1946 .compat_ioctl = kvm_dev_ioctl,
1949 static struct miscdevice kvm_dev = {
1955 static void hardware_enable(void *junk)
1957 int cpu = raw_smp_processor_id();
1959 if (cpu_isset(cpu, cpus_hardware_enabled))
1961 cpu_set(cpu, cpus_hardware_enabled);
1962 kvm_arch_hardware_enable(NULL);
1965 static void hardware_disable(void *junk)
1967 int cpu = raw_smp_processor_id();
1969 if (!cpu_isset(cpu, cpus_hardware_enabled))
1971 cpu_clear(cpu, cpus_hardware_enabled);
1972 kvm_arch_hardware_disable(NULL);
1975 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1980 val &= ~CPU_TASKS_FROZEN;
1983 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1985 hardware_disable(NULL);
1987 case CPU_UP_CANCELED:
1988 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1990 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1993 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1995 smp_call_function_single(cpu, hardware_enable, NULL, 1);
2002 asmlinkage void kvm_handle_fault_on_reboot(void)
2005 /* spin while reset goes on */
2008 /* Fault while not rebooting. We want the trace. */
2011 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2013 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2016 if (val == SYS_RESTART) {
2018 * Some (well, at least mine) BIOSes hang on reboot if
2021 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2022 kvm_rebooting = true;
2023 on_each_cpu(hardware_disable, NULL, 1);
2028 static struct notifier_block kvm_reboot_notifier = {
2029 .notifier_call = kvm_reboot,
2033 void kvm_io_bus_init(struct kvm_io_bus *bus)
2035 memset(bus, 0, sizeof(*bus));
2038 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2042 for (i = 0; i < bus->dev_count; i++) {
2043 struct kvm_io_device *pos = bus->devs[i];
2045 kvm_iodevice_destructor(pos);
2049 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2050 gpa_t addr, int len, int is_write)
2054 for (i = 0; i < bus->dev_count; i++) {
2055 struct kvm_io_device *pos = bus->devs[i];
2057 if (pos->in_range(pos, addr, len, is_write))
2064 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2066 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2068 bus->devs[bus->dev_count++] = dev;
2071 static struct notifier_block kvm_cpu_notifier = {
2072 .notifier_call = kvm_cpu_hotplug,
2073 .priority = 20, /* must be > scheduler priority */
2076 static int vm_stat_get(void *_offset, u64 *val)
2078 unsigned offset = (long)_offset;
2082 spin_lock(&kvm_lock);
2083 list_for_each_entry(kvm, &vm_list, vm_list)
2084 *val += *(u32 *)((void *)kvm + offset);
2085 spin_unlock(&kvm_lock);
2089 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2091 static int vcpu_stat_get(void *_offset, u64 *val)
2093 unsigned offset = (long)_offset;
2095 struct kvm_vcpu *vcpu;
2099 spin_lock(&kvm_lock);
2100 list_for_each_entry(kvm, &vm_list, vm_list)
2101 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2102 vcpu = kvm->vcpus[i];
2104 *val += *(u32 *)((void *)vcpu + offset);
2106 spin_unlock(&kvm_lock);
2110 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2112 static struct file_operations *stat_fops[] = {
2113 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2114 [KVM_STAT_VM] = &vm_stat_fops,
2117 static void kvm_init_debug(void)
2119 struct kvm_stats_debugfs_item *p;
2121 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2122 for (p = debugfs_entries; p->name; ++p)
2123 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2124 (void *)(long)p->offset,
2125 stat_fops[p->kind]);
2128 static void kvm_exit_debug(void)
2130 struct kvm_stats_debugfs_item *p;
2132 for (p = debugfs_entries; p->name; ++p)
2133 debugfs_remove(p->dentry);
2134 debugfs_remove(kvm_debugfs_dir);
2137 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2139 hardware_disable(NULL);
2143 static int kvm_resume(struct sys_device *dev)
2145 hardware_enable(NULL);
2149 static struct sysdev_class kvm_sysdev_class = {
2151 .suspend = kvm_suspend,
2152 .resume = kvm_resume,
2155 static struct sys_device kvm_sysdev = {
2157 .cls = &kvm_sysdev_class,
2160 struct page *bad_page;
2164 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2166 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2169 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2171 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2173 kvm_arch_vcpu_load(vcpu, cpu);
2176 static void kvm_sched_out(struct preempt_notifier *pn,
2177 struct task_struct *next)
2179 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2181 kvm_arch_vcpu_put(vcpu);
2184 int kvm_init(void *opaque, unsigned int vcpu_size,
2185 struct module *module)
2192 r = kvm_arch_init(opaque);
2196 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2198 if (bad_page == NULL) {
2203 bad_pfn = page_to_pfn(bad_page);
2205 r = kvm_arch_hardware_setup();
2209 for_each_online_cpu(cpu) {
2210 smp_call_function_single(cpu,
2211 kvm_arch_check_processor_compat,
2217 on_each_cpu(hardware_enable, NULL, 1);
2218 r = register_cpu_notifier(&kvm_cpu_notifier);
2221 register_reboot_notifier(&kvm_reboot_notifier);
2223 r = sysdev_class_register(&kvm_sysdev_class);
2227 r = sysdev_register(&kvm_sysdev);
2231 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2232 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2233 __alignof__(struct kvm_vcpu),
2235 if (!kvm_vcpu_cache) {
2240 kvm_chardev_ops.owner = module;
2242 r = misc_register(&kvm_dev);
2244 printk(KERN_ERR "kvm: misc device register failed\n");
2248 kvm_preempt_ops.sched_in = kvm_sched_in;
2249 kvm_preempt_ops.sched_out = kvm_sched_out;
2257 kmem_cache_destroy(kvm_vcpu_cache);
2259 sysdev_unregister(&kvm_sysdev);
2261 sysdev_class_unregister(&kvm_sysdev_class);
2263 unregister_reboot_notifier(&kvm_reboot_notifier);
2264 unregister_cpu_notifier(&kvm_cpu_notifier);
2266 on_each_cpu(hardware_disable, NULL, 1);
2268 kvm_arch_hardware_unsetup();
2270 __free_page(bad_page);
2277 EXPORT_SYMBOL_GPL(kvm_init);
2281 kvm_trace_cleanup();
2282 misc_deregister(&kvm_dev);
2283 kmem_cache_destroy(kvm_vcpu_cache);
2284 sysdev_unregister(&kvm_sysdev);
2285 sysdev_class_unregister(&kvm_sysdev_class);
2286 unregister_reboot_notifier(&kvm_reboot_notifier);
2287 unregister_cpu_notifier(&kvm_cpu_notifier);
2288 on_each_cpu(hardware_disable, NULL, 1);
2289 kvm_arch_hardware_unsetup();
2292 __free_page(bad_page);
2294 EXPORT_SYMBOL_GPL(kvm_exit);