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_var_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);
173 assigned_dev->host_irq_disabled = false;
175 mutex_unlock(&assigned_dev->kvm->lock);
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 schedule_work(&assigned_dev->interrupt_work);
185 disable_irq_nosync(irq);
186 assigned_dev->host_irq_disabled = true;
191 /* Ack the irq line for an assigned device */
192 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
194 struct kvm_assigned_dev_kernel *dev;
199 dev = container_of(kian, struct kvm_assigned_dev_kernel,
202 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
204 /* The guest irq may be shared so this ack may be
205 * from another device.
207 if (dev->host_irq_disabled) {
208 enable_irq(dev->host_irq);
209 dev->host_irq_disabled = false;
213 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
214 static void kvm_free_assigned_irq(struct kvm *kvm,
215 struct kvm_assigned_dev_kernel *assigned_dev)
217 if (!irqchip_in_kernel(kvm))
220 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
222 if (assigned_dev->irq_source_id != -1)
223 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
224 assigned_dev->irq_source_id = -1;
226 if (!assigned_dev->irq_requested_type)
230 * In kvm_free_device_irq, cancel_work_sync return true if:
231 * 1. work is scheduled, and then cancelled.
232 * 2. work callback is executed.
234 * The first one ensured that the irq is disabled and no more events
235 * would happen. But for the second one, the irq may be enabled (e.g.
236 * for MSI). So we disable irq here to prevent further events.
238 * Notice this maybe result in nested disable if the interrupt type is
239 * INTx, but it's OK for we are going to free it.
241 * If this function is a part of VM destroy, please ensure that till
242 * now, the kvm state is still legal for probably we also have to wait
243 * interrupt_work done.
245 disable_irq_nosync(assigned_dev->host_irq);
246 cancel_work_sync(&assigned_dev->interrupt_work);
248 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
250 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
251 pci_disable_msi(assigned_dev->dev);
253 assigned_dev->irq_requested_type = 0;
257 static void kvm_free_assigned_device(struct kvm *kvm,
258 struct kvm_assigned_dev_kernel
261 kvm_free_assigned_irq(kvm, assigned_dev);
263 pci_reset_function(assigned_dev->dev);
265 pci_release_regions(assigned_dev->dev);
266 pci_disable_device(assigned_dev->dev);
267 pci_dev_put(assigned_dev->dev);
269 list_del(&assigned_dev->list);
273 void kvm_free_all_assigned_devices(struct kvm *kvm)
275 struct list_head *ptr, *ptr2;
276 struct kvm_assigned_dev_kernel *assigned_dev;
278 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
279 assigned_dev = list_entry(ptr,
280 struct kvm_assigned_dev_kernel,
283 kvm_free_assigned_device(kvm, assigned_dev);
287 static int assigned_device_update_intx(struct kvm *kvm,
288 struct kvm_assigned_dev_kernel *adev,
289 struct kvm_assigned_irq *airq)
291 adev->guest_irq = airq->guest_irq;
292 adev->ack_notifier.gsi = airq->guest_irq;
294 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
297 if (irqchip_in_kernel(kvm)) {
299 (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
300 free_irq(adev->host_irq, (void *)adev);
301 pci_disable_msi(adev->dev);
304 if (!capable(CAP_SYS_RAWIO))
308 adev->host_irq = airq->host_irq;
310 adev->host_irq = adev->dev->irq;
312 /* Even though this is PCI, we don't want to use shared
313 * interrupts. Sharing host devices with guest-assigned devices
314 * on the same interrupt line is not a happy situation: there
315 * are going to be long delays in accepting, acking, etc.
317 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
318 0, "kvm_assigned_intx_device", (void *)adev))
322 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
323 KVM_ASSIGNED_DEV_HOST_INTX;
328 static int assigned_device_update_msi(struct kvm *kvm,
329 struct kvm_assigned_dev_kernel *adev,
330 struct kvm_assigned_irq *airq)
334 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
335 /* x86 don't care upper address of guest msi message addr */
336 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
337 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
338 adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
339 adev->guest_msi.data = airq->guest_msi.data;
340 adev->ack_notifier.gsi = -1;
341 } else if (msi2intx) {
342 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
343 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
344 adev->guest_irq = airq->guest_irq;
345 adev->ack_notifier.gsi = airq->guest_irq;
348 * Guest require to disable device MSI, we disable MSI and
349 * re-enable INTx by default again. Notice it's only for
352 assigned_device_update_intx(kvm, adev, airq);
356 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
359 if (irqchip_in_kernel(kvm)) {
361 if (adev->irq_requested_type &
362 KVM_ASSIGNED_DEV_HOST_INTX)
363 free_irq(adev->host_irq, (void *)adev);
365 r = pci_enable_msi(adev->dev);
370 adev->host_irq = adev->dev->irq;
371 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
372 "kvm_assigned_msi_device", (void *)adev))
377 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
379 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
384 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
385 struct kvm_assigned_irq
389 struct kvm_assigned_dev_kernel *match;
390 u32 current_flags = 0, changed_flags;
392 mutex_lock(&kvm->lock);
394 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
395 assigned_irq->assigned_dev_id);
397 mutex_unlock(&kvm->lock);
401 if (!match->irq_requested_type) {
402 INIT_WORK(&match->interrupt_work,
403 kvm_assigned_dev_interrupt_work_handler);
404 if (irqchip_in_kernel(kvm)) {
405 /* Register ack nofitier */
406 match->ack_notifier.gsi = -1;
407 match->ack_notifier.irq_acked =
408 kvm_assigned_dev_ack_irq;
409 kvm_register_irq_ack_notifier(kvm,
410 &match->ack_notifier);
412 /* Request IRQ source ID */
413 r = kvm_request_irq_source_id(kvm);
417 match->irq_source_id = r;
420 /* Determine host device irq type, we can know the
421 * result from dev->msi_enabled */
423 pci_enable_msi(match->dev);
428 if ((match->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) &&
429 (match->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_MSI))
430 current_flags |= KVM_DEV_IRQ_ASSIGN_ENABLE_MSI;
432 changed_flags = assigned_irq->flags ^ current_flags;
434 if ((changed_flags & KVM_DEV_IRQ_ASSIGN_MSI_ACTION) ||
435 (msi2intx && match->dev->msi_enabled)) {
437 r = assigned_device_update_msi(kvm, match, assigned_irq);
439 printk(KERN_WARNING "kvm: failed to enable "
446 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
447 /* Host device IRQ 0 means don't support INTx */
450 "kvm: wait device to enable MSI!\n");
454 "kvm: failed to enable MSI device!\n");
459 /* Non-sharing INTx mode */
460 r = assigned_device_update_intx(kvm, match, assigned_irq);
462 printk(KERN_WARNING "kvm: failed to enable "
468 mutex_unlock(&kvm->lock);
471 mutex_unlock(&kvm->lock);
472 kvm_free_assigned_device(kvm, match);
476 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
477 struct kvm_assigned_pci_dev *assigned_dev)
480 struct kvm_assigned_dev_kernel *match;
483 down_read(&kvm->slots_lock);
484 mutex_lock(&kvm->lock);
486 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
487 assigned_dev->assigned_dev_id);
489 /* device already assigned */
494 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
496 printk(KERN_INFO "%s: Couldn't allocate memory\n",
501 dev = pci_get_bus_and_slot(assigned_dev->busnr,
502 assigned_dev->devfn);
504 printk(KERN_INFO "%s: host device not found\n", __func__);
508 if (pci_enable_device(dev)) {
509 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
513 r = pci_request_regions(dev, "kvm_assigned_device");
515 printk(KERN_INFO "%s: Could not get access to device regions\n",
520 pci_reset_function(dev);
522 match->assigned_dev_id = assigned_dev->assigned_dev_id;
523 match->host_busnr = assigned_dev->busnr;
524 match->host_devfn = assigned_dev->devfn;
525 match->flags = assigned_dev->flags;
527 match->irq_source_id = -1;
530 list_add(&match->list, &kvm->arch.assigned_dev_head);
532 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
533 if (!kvm->arch.iommu_domain) {
534 r = kvm_iommu_map_guest(kvm);
538 r = kvm_assign_device(kvm, match);
544 mutex_unlock(&kvm->lock);
545 up_read(&kvm->slots_lock);
548 list_del(&match->list);
549 pci_release_regions(dev);
551 pci_disable_device(dev);
556 mutex_unlock(&kvm->lock);
557 up_read(&kvm->slots_lock);
562 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
563 static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
564 struct kvm_assigned_pci_dev *assigned_dev)
567 struct kvm_assigned_dev_kernel *match;
569 mutex_lock(&kvm->lock);
571 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
572 assigned_dev->assigned_dev_id);
574 printk(KERN_INFO "%s: device hasn't been assigned before, "
575 "so cannot be deassigned\n", __func__);
580 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
581 kvm_deassign_device(kvm, match);
583 kvm_free_assigned_device(kvm, match);
586 mutex_unlock(&kvm->lock);
591 static inline int valid_vcpu(int n)
593 return likely(n >= 0 && n < KVM_MAX_VCPUS);
596 inline int kvm_is_mmio_pfn(pfn_t pfn)
599 return PageReserved(pfn_to_page(pfn));
605 * Switches to specified vcpu, until a matching vcpu_put()
607 void vcpu_load(struct kvm_vcpu *vcpu)
611 mutex_lock(&vcpu->mutex);
613 preempt_notifier_register(&vcpu->preempt_notifier);
614 kvm_arch_vcpu_load(vcpu, cpu);
618 void vcpu_put(struct kvm_vcpu *vcpu)
621 kvm_arch_vcpu_put(vcpu);
622 preempt_notifier_unregister(&vcpu->preempt_notifier);
624 mutex_unlock(&vcpu->mutex);
627 static void ack_flush(void *_completed)
631 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
636 struct kvm_vcpu *vcpu;
638 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
642 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
643 vcpu = kvm->vcpus[i];
646 if (test_and_set_bit(req, &vcpu->requests))
649 if (cpus != NULL && cpu != -1 && cpu != me)
650 cpumask_set_cpu(cpu, cpus);
652 if (unlikely(cpus == NULL))
653 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
654 else if (!cpumask_empty(cpus))
655 smp_call_function_many(cpus, ack_flush, NULL, 1);
659 free_cpumask_var(cpus);
663 void kvm_flush_remote_tlbs(struct kvm *kvm)
665 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
666 ++kvm->stat.remote_tlb_flush;
669 void kvm_reload_remote_mmus(struct kvm *kvm)
671 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
674 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
679 mutex_init(&vcpu->mutex);
683 init_waitqueue_head(&vcpu->wq);
685 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
690 vcpu->run = page_address(page);
692 r = kvm_arch_vcpu_init(vcpu);
698 free_page((unsigned long)vcpu->run);
702 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
704 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
706 kvm_arch_vcpu_uninit(vcpu);
707 free_page((unsigned long)vcpu->run);
709 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
711 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
712 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
714 return container_of(mn, struct kvm, mmu_notifier);
717 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
718 struct mm_struct *mm,
719 unsigned long address)
721 struct kvm *kvm = mmu_notifier_to_kvm(mn);
725 * When ->invalidate_page runs, the linux pte has been zapped
726 * already but the page is still allocated until
727 * ->invalidate_page returns. So if we increase the sequence
728 * here the kvm page fault will notice if the spte can't be
729 * established because the page is going to be freed. If
730 * instead the kvm page fault establishes the spte before
731 * ->invalidate_page runs, kvm_unmap_hva will release it
734 * The sequence increase only need to be seen at spin_unlock
735 * time, and not at spin_lock time.
737 * Increasing the sequence after the spin_unlock would be
738 * unsafe because the kvm page fault could then establish the
739 * pte after kvm_unmap_hva returned, without noticing the page
740 * is going to be freed.
742 spin_lock(&kvm->mmu_lock);
743 kvm->mmu_notifier_seq++;
744 need_tlb_flush = kvm_unmap_hva(kvm, address);
745 spin_unlock(&kvm->mmu_lock);
747 /* we've to flush the tlb before the pages can be freed */
749 kvm_flush_remote_tlbs(kvm);
753 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
754 struct mm_struct *mm,
758 struct kvm *kvm = mmu_notifier_to_kvm(mn);
759 int need_tlb_flush = 0;
761 spin_lock(&kvm->mmu_lock);
763 * The count increase must become visible at unlock time as no
764 * spte can be established without taking the mmu_lock and
765 * count is also read inside the mmu_lock critical section.
767 kvm->mmu_notifier_count++;
768 for (; start < end; start += PAGE_SIZE)
769 need_tlb_flush |= kvm_unmap_hva(kvm, start);
770 spin_unlock(&kvm->mmu_lock);
772 /* we've to flush the tlb before the pages can be freed */
774 kvm_flush_remote_tlbs(kvm);
777 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
778 struct mm_struct *mm,
782 struct kvm *kvm = mmu_notifier_to_kvm(mn);
784 spin_lock(&kvm->mmu_lock);
786 * This sequence increase will notify the kvm page fault that
787 * the page that is going to be mapped in the spte could have
790 kvm->mmu_notifier_seq++;
792 * The above sequence increase must be visible before the
793 * below count decrease but both values are read by the kvm
794 * page fault under mmu_lock spinlock so we don't need to add
795 * a smb_wmb() here in between the two.
797 kvm->mmu_notifier_count--;
798 spin_unlock(&kvm->mmu_lock);
800 BUG_ON(kvm->mmu_notifier_count < 0);
803 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
804 struct mm_struct *mm,
805 unsigned long address)
807 struct kvm *kvm = mmu_notifier_to_kvm(mn);
810 spin_lock(&kvm->mmu_lock);
811 young = kvm_age_hva(kvm, address);
812 spin_unlock(&kvm->mmu_lock);
815 kvm_flush_remote_tlbs(kvm);
820 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
821 struct mm_struct *mm)
823 struct kvm *kvm = mmu_notifier_to_kvm(mn);
824 kvm_arch_flush_shadow(kvm);
827 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
828 .invalidate_page = kvm_mmu_notifier_invalidate_page,
829 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
830 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
831 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
832 .release = kvm_mmu_notifier_release,
834 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
836 static struct kvm *kvm_create_vm(void)
838 struct kvm *kvm = kvm_arch_create_vm();
839 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
845 #ifdef CONFIG_HAVE_KVM_IRQCHIP
846 INIT_LIST_HEAD(&kvm->irq_routing);
847 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
850 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
851 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
854 return ERR_PTR(-ENOMEM);
856 kvm->coalesced_mmio_ring =
857 (struct kvm_coalesced_mmio_ring *)page_address(page);
860 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
863 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
864 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
866 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
875 kvm->mm = current->mm;
876 atomic_inc(&kvm->mm->mm_count);
877 spin_lock_init(&kvm->mmu_lock);
878 kvm_io_bus_init(&kvm->pio_bus);
879 mutex_init(&kvm->lock);
880 kvm_io_bus_init(&kvm->mmio_bus);
881 init_rwsem(&kvm->slots_lock);
882 atomic_set(&kvm->users_count, 1);
883 spin_lock(&kvm_lock);
884 list_add(&kvm->vm_list, &vm_list);
885 spin_unlock(&kvm_lock);
886 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
887 kvm_coalesced_mmio_init(kvm);
894 * Free any memory in @free but not in @dont.
896 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
897 struct kvm_memory_slot *dont)
899 if (!dont || free->rmap != dont->rmap)
902 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
903 vfree(free->dirty_bitmap);
905 if (!dont || free->lpage_info != dont->lpage_info)
906 vfree(free->lpage_info);
909 free->dirty_bitmap = NULL;
911 free->lpage_info = NULL;
914 void kvm_free_physmem(struct kvm *kvm)
918 for (i = 0; i < kvm->nmemslots; ++i)
919 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
922 static void kvm_destroy_vm(struct kvm *kvm)
924 struct mm_struct *mm = kvm->mm;
926 kvm_arch_sync_events(kvm);
927 spin_lock(&kvm_lock);
928 list_del(&kvm->vm_list);
929 spin_unlock(&kvm_lock);
930 kvm_free_irq_routing(kvm);
931 kvm_io_bus_destroy(&kvm->pio_bus);
932 kvm_io_bus_destroy(&kvm->mmio_bus);
933 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
934 if (kvm->coalesced_mmio_ring != NULL)
935 free_page((unsigned long)kvm->coalesced_mmio_ring);
937 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
938 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
940 kvm_arch_destroy_vm(kvm);
944 void kvm_get_kvm(struct kvm *kvm)
946 atomic_inc(&kvm->users_count);
948 EXPORT_SYMBOL_GPL(kvm_get_kvm);
950 void kvm_put_kvm(struct kvm *kvm)
952 if (atomic_dec_and_test(&kvm->users_count))
955 EXPORT_SYMBOL_GPL(kvm_put_kvm);
958 static int kvm_vm_release(struct inode *inode, struct file *filp)
960 struct kvm *kvm = filp->private_data;
967 * Allocate some memory and give it an address in the guest physical address
970 * Discontiguous memory is allowed, mostly for framebuffers.
972 * Must be called holding mmap_sem for write.
974 int __kvm_set_memory_region(struct kvm *kvm,
975 struct kvm_userspace_memory_region *mem,
980 unsigned long npages;
982 struct kvm_memory_slot *memslot;
983 struct kvm_memory_slot old, new;
986 /* General sanity checks */
987 if (mem->memory_size & (PAGE_SIZE - 1))
989 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
991 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
993 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
995 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
998 memslot = &kvm->memslots[mem->slot];
999 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1000 npages = mem->memory_size >> PAGE_SHIFT;
1003 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1005 new = old = *memslot;
1007 new.base_gfn = base_gfn;
1008 new.npages = npages;
1009 new.flags = mem->flags;
1011 /* Disallow changing a memory slot's size. */
1013 if (npages && old.npages && npages != old.npages)
1016 /* Check for overlaps */
1018 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1019 struct kvm_memory_slot *s = &kvm->memslots[i];
1023 if (!((base_gfn + npages <= s->base_gfn) ||
1024 (base_gfn >= s->base_gfn + s->npages)))
1028 /* Free page dirty bitmap if unneeded */
1029 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
1030 new.dirty_bitmap = NULL;
1034 /* Allocate if a slot is being created */
1036 if (npages && !new.rmap) {
1037 new.rmap = vmalloc(npages * sizeof(struct page *));
1042 memset(new.rmap, 0, npages * sizeof(*new.rmap));
1044 new.user_alloc = user_alloc;
1046 * hva_to_rmmap() serialzies with the mmu_lock and to be
1047 * safe it has to ignore memslots with !user_alloc &&
1051 new.userspace_addr = mem->userspace_addr;
1053 new.userspace_addr = 0;
1055 if (npages && !new.lpage_info) {
1056 int largepages = npages / KVM_PAGES_PER_HPAGE;
1057 if (npages % KVM_PAGES_PER_HPAGE)
1059 if (base_gfn % KVM_PAGES_PER_HPAGE)
1062 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1064 if (!new.lpage_info)
1067 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1069 if (base_gfn % KVM_PAGES_PER_HPAGE)
1070 new.lpage_info[0].write_count = 1;
1071 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1072 new.lpage_info[largepages-1].write_count = 1;
1075 /* Allocate page dirty bitmap if needed */
1076 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1077 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1079 new.dirty_bitmap = vmalloc(dirty_bytes);
1080 if (!new.dirty_bitmap)
1082 memset(new.dirty_bitmap, 0, dirty_bytes);
1084 #endif /* not defined CONFIG_S390 */
1087 kvm_arch_flush_shadow(kvm);
1089 spin_lock(&kvm->mmu_lock);
1090 if (mem->slot >= kvm->nmemslots)
1091 kvm->nmemslots = mem->slot + 1;
1094 spin_unlock(&kvm->mmu_lock);
1096 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1098 spin_lock(&kvm->mmu_lock);
1100 spin_unlock(&kvm->mmu_lock);
1104 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1105 /* Slot deletion case: we have to update the current slot */
1109 /* map the pages in iommu page table */
1110 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1117 kvm_free_physmem_slot(&new, &old);
1122 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1124 int kvm_set_memory_region(struct kvm *kvm,
1125 struct kvm_userspace_memory_region *mem,
1130 down_write(&kvm->slots_lock);
1131 r = __kvm_set_memory_region(kvm, mem, user_alloc);
1132 up_write(&kvm->slots_lock);
1135 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1137 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1139 kvm_userspace_memory_region *mem,
1142 if (mem->slot >= KVM_MEMORY_SLOTS)
1144 return kvm_set_memory_region(kvm, mem, user_alloc);
1147 int kvm_get_dirty_log(struct kvm *kvm,
1148 struct kvm_dirty_log *log, int *is_dirty)
1150 struct kvm_memory_slot *memslot;
1153 unsigned long any = 0;
1156 if (log->slot >= KVM_MEMORY_SLOTS)
1159 memslot = &kvm->memslots[log->slot];
1161 if (!memslot->dirty_bitmap)
1164 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1166 for (i = 0; !any && i < n/sizeof(long); ++i)
1167 any = memslot->dirty_bitmap[i];
1170 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1181 int is_error_page(struct page *page)
1183 return page == bad_page;
1185 EXPORT_SYMBOL_GPL(is_error_page);
1187 int is_error_pfn(pfn_t pfn)
1189 return pfn == bad_pfn;
1191 EXPORT_SYMBOL_GPL(is_error_pfn);
1193 static inline unsigned long bad_hva(void)
1198 int kvm_is_error_hva(unsigned long addr)
1200 return addr == bad_hva();
1202 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1204 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1208 for (i = 0; i < kvm->nmemslots; ++i) {
1209 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1211 if (gfn >= memslot->base_gfn
1212 && gfn < memslot->base_gfn + memslot->npages)
1217 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1219 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1221 gfn = unalias_gfn(kvm, gfn);
1222 return gfn_to_memslot_unaliased(kvm, gfn);
1225 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1229 gfn = unalias_gfn(kvm, gfn);
1230 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1231 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1233 if (gfn >= memslot->base_gfn
1234 && gfn < memslot->base_gfn + memslot->npages)
1239 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1241 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1243 struct kvm_memory_slot *slot;
1245 gfn = unalias_gfn(kvm, gfn);
1246 slot = gfn_to_memslot_unaliased(kvm, gfn);
1249 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1251 EXPORT_SYMBOL_GPL(gfn_to_hva);
1253 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1255 struct page *page[1];
1262 addr = gfn_to_hva(kvm, gfn);
1263 if (kvm_is_error_hva(addr)) {
1265 return page_to_pfn(bad_page);
1268 npages = get_user_pages_fast(addr, 1, 1, page);
1270 if (unlikely(npages != 1)) {
1271 struct vm_area_struct *vma;
1273 down_read(¤t->mm->mmap_sem);
1274 vma = find_vma(current->mm, addr);
1276 if (vma == NULL || addr < vma->vm_start ||
1277 !(vma->vm_flags & VM_PFNMAP)) {
1278 up_read(¤t->mm->mmap_sem);
1280 return page_to_pfn(bad_page);
1283 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1284 up_read(¤t->mm->mmap_sem);
1285 BUG_ON(!kvm_is_mmio_pfn(pfn));
1287 pfn = page_to_pfn(page[0]);
1292 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1294 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1298 pfn = gfn_to_pfn(kvm, gfn);
1299 if (!kvm_is_mmio_pfn(pfn))
1300 return pfn_to_page(pfn);
1302 WARN_ON(kvm_is_mmio_pfn(pfn));
1308 EXPORT_SYMBOL_GPL(gfn_to_page);
1310 void kvm_release_page_clean(struct page *page)
1312 kvm_release_pfn_clean(page_to_pfn(page));
1314 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1316 void kvm_release_pfn_clean(pfn_t pfn)
1318 if (!kvm_is_mmio_pfn(pfn))
1319 put_page(pfn_to_page(pfn));
1321 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1323 void kvm_release_page_dirty(struct page *page)
1325 kvm_release_pfn_dirty(page_to_pfn(page));
1327 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1329 void kvm_release_pfn_dirty(pfn_t pfn)
1331 kvm_set_pfn_dirty(pfn);
1332 kvm_release_pfn_clean(pfn);
1334 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1336 void kvm_set_page_dirty(struct page *page)
1338 kvm_set_pfn_dirty(page_to_pfn(page));
1340 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1342 void kvm_set_pfn_dirty(pfn_t pfn)
1344 if (!kvm_is_mmio_pfn(pfn)) {
1345 struct page *page = pfn_to_page(pfn);
1346 if (!PageReserved(page))
1350 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1352 void kvm_set_pfn_accessed(pfn_t pfn)
1354 if (!kvm_is_mmio_pfn(pfn))
1355 mark_page_accessed(pfn_to_page(pfn));
1357 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1359 void kvm_get_pfn(pfn_t pfn)
1361 if (!kvm_is_mmio_pfn(pfn))
1362 get_page(pfn_to_page(pfn));
1364 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1366 static int next_segment(unsigned long len, int offset)
1368 if (len > PAGE_SIZE - offset)
1369 return PAGE_SIZE - offset;
1374 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1380 addr = gfn_to_hva(kvm, gfn);
1381 if (kvm_is_error_hva(addr))
1383 r = copy_from_user(data, (void __user *)addr + offset, len);
1388 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1390 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1392 gfn_t gfn = gpa >> PAGE_SHIFT;
1394 int offset = offset_in_page(gpa);
1397 while ((seg = next_segment(len, offset)) != 0) {
1398 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1408 EXPORT_SYMBOL_GPL(kvm_read_guest);
1410 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1415 gfn_t gfn = gpa >> PAGE_SHIFT;
1416 int offset = offset_in_page(gpa);
1418 addr = gfn_to_hva(kvm, gfn);
1419 if (kvm_is_error_hva(addr))
1421 pagefault_disable();
1422 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1428 EXPORT_SYMBOL(kvm_read_guest_atomic);
1430 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1431 int offset, int len)
1436 addr = gfn_to_hva(kvm, gfn);
1437 if (kvm_is_error_hva(addr))
1439 r = copy_to_user((void __user *)addr + offset, data, len);
1442 mark_page_dirty(kvm, gfn);
1445 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1447 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1450 gfn_t gfn = gpa >> PAGE_SHIFT;
1452 int offset = offset_in_page(gpa);
1455 while ((seg = next_segment(len, offset)) != 0) {
1456 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1467 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1469 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1471 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1473 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1475 gfn_t gfn = gpa >> PAGE_SHIFT;
1477 int offset = offset_in_page(gpa);
1480 while ((seg = next_segment(len, offset)) != 0) {
1481 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1490 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1492 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1494 struct kvm_memory_slot *memslot;
1496 gfn = unalias_gfn(kvm, gfn);
1497 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1498 if (memslot && memslot->dirty_bitmap) {
1499 unsigned long rel_gfn = gfn - memslot->base_gfn;
1502 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1503 set_bit(rel_gfn, memslot->dirty_bitmap);
1508 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1510 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1515 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1517 if (kvm_cpu_has_interrupt(vcpu) ||
1518 kvm_cpu_has_pending_timer(vcpu) ||
1519 kvm_arch_vcpu_runnable(vcpu)) {
1520 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1523 if (signal_pending(current))
1531 finish_wait(&vcpu->wq, &wait);
1534 void kvm_resched(struct kvm_vcpu *vcpu)
1536 if (!need_resched())
1540 EXPORT_SYMBOL_GPL(kvm_resched);
1542 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1544 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1547 if (vmf->pgoff == 0)
1548 page = virt_to_page(vcpu->run);
1550 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1551 page = virt_to_page(vcpu->arch.pio_data);
1553 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1554 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1555 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1558 return VM_FAULT_SIGBUS;
1564 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1565 .fault = kvm_vcpu_fault,
1568 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1570 vma->vm_ops = &kvm_vcpu_vm_ops;
1574 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1576 struct kvm_vcpu *vcpu = filp->private_data;
1578 kvm_put_kvm(vcpu->kvm);
1582 static struct file_operations kvm_vcpu_fops = {
1583 .release = kvm_vcpu_release,
1584 .unlocked_ioctl = kvm_vcpu_ioctl,
1585 .compat_ioctl = kvm_vcpu_ioctl,
1586 .mmap = kvm_vcpu_mmap,
1590 * Allocates an inode for the vcpu.
1592 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1594 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1596 kvm_put_kvm(vcpu->kvm);
1601 * Creates some virtual cpus. Good luck creating more than one.
1603 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1606 struct kvm_vcpu *vcpu;
1611 vcpu = kvm_arch_vcpu_create(kvm, n);
1613 return PTR_ERR(vcpu);
1615 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1617 r = kvm_arch_vcpu_setup(vcpu);
1621 mutex_lock(&kvm->lock);
1622 if (kvm->vcpus[n]) {
1626 kvm->vcpus[n] = vcpu;
1627 mutex_unlock(&kvm->lock);
1629 /* Now it's all set up, let userspace reach it */
1631 r = create_vcpu_fd(vcpu);
1637 mutex_lock(&kvm->lock);
1638 kvm->vcpus[n] = NULL;
1640 mutex_unlock(&kvm->lock);
1641 kvm_arch_vcpu_destroy(vcpu);
1645 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1648 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1649 vcpu->sigset_active = 1;
1650 vcpu->sigset = *sigset;
1652 vcpu->sigset_active = 0;
1656 static long kvm_vcpu_ioctl(struct file *filp,
1657 unsigned int ioctl, unsigned long arg)
1659 struct kvm_vcpu *vcpu = filp->private_data;
1660 void __user *argp = (void __user *)arg;
1662 struct kvm_fpu *fpu = NULL;
1663 struct kvm_sregs *kvm_sregs = NULL;
1665 if (vcpu->kvm->mm != current->mm)
1672 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1674 case KVM_GET_REGS: {
1675 struct kvm_regs *kvm_regs;
1678 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1681 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1685 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1692 case KVM_SET_REGS: {
1693 struct kvm_regs *kvm_regs;
1696 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1700 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1702 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1710 case KVM_GET_SREGS: {
1711 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1715 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1719 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1724 case KVM_SET_SREGS: {
1725 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1730 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1732 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1738 case KVM_GET_MP_STATE: {
1739 struct kvm_mp_state mp_state;
1741 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1745 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1750 case KVM_SET_MP_STATE: {
1751 struct kvm_mp_state mp_state;
1754 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1756 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1762 case KVM_TRANSLATE: {
1763 struct kvm_translation tr;
1766 if (copy_from_user(&tr, argp, sizeof tr))
1768 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1772 if (copy_to_user(argp, &tr, sizeof tr))
1777 case KVM_SET_GUEST_DEBUG: {
1778 struct kvm_guest_debug dbg;
1781 if (copy_from_user(&dbg, argp, sizeof dbg))
1783 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1789 case KVM_SET_SIGNAL_MASK: {
1790 struct kvm_signal_mask __user *sigmask_arg = argp;
1791 struct kvm_signal_mask kvm_sigmask;
1792 sigset_t sigset, *p;
1797 if (copy_from_user(&kvm_sigmask, argp,
1798 sizeof kvm_sigmask))
1801 if (kvm_sigmask.len != sizeof sigset)
1804 if (copy_from_user(&sigset, sigmask_arg->sigset,
1809 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1813 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1817 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1821 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1827 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1832 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1834 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1841 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1849 static long kvm_vm_ioctl(struct file *filp,
1850 unsigned int ioctl, unsigned long arg)
1852 struct kvm *kvm = filp->private_data;
1853 void __user *argp = (void __user *)arg;
1856 if (kvm->mm != current->mm)
1859 case KVM_CREATE_VCPU:
1860 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1864 case KVM_SET_USER_MEMORY_REGION: {
1865 struct kvm_userspace_memory_region kvm_userspace_mem;
1868 if (copy_from_user(&kvm_userspace_mem, argp,
1869 sizeof kvm_userspace_mem))
1872 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1877 case KVM_GET_DIRTY_LOG: {
1878 struct kvm_dirty_log log;
1881 if (copy_from_user(&log, argp, sizeof log))
1883 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1888 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1889 case KVM_REGISTER_COALESCED_MMIO: {
1890 struct kvm_coalesced_mmio_zone zone;
1892 if (copy_from_user(&zone, argp, sizeof zone))
1895 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1901 case KVM_UNREGISTER_COALESCED_MMIO: {
1902 struct kvm_coalesced_mmio_zone zone;
1904 if (copy_from_user(&zone, argp, sizeof zone))
1907 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1914 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1915 case KVM_ASSIGN_PCI_DEVICE: {
1916 struct kvm_assigned_pci_dev assigned_dev;
1919 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1921 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1926 case KVM_ASSIGN_IRQ: {
1927 struct kvm_assigned_irq assigned_irq;
1930 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1932 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1938 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1939 case KVM_DEASSIGN_PCI_DEVICE: {
1940 struct kvm_assigned_pci_dev assigned_dev;
1943 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1945 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
1951 #ifdef KVM_CAP_IRQ_ROUTING
1952 case KVM_SET_GSI_ROUTING: {
1953 struct kvm_irq_routing routing;
1954 struct kvm_irq_routing __user *urouting;
1955 struct kvm_irq_routing_entry *entries;
1958 if (copy_from_user(&routing, argp, sizeof(routing)))
1961 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
1966 entries = vmalloc(routing.nr * sizeof(*entries));
1971 if (copy_from_user(entries, urouting->entries,
1972 routing.nr * sizeof(*entries)))
1973 goto out_free_irq_routing;
1974 r = kvm_set_irq_routing(kvm, entries, routing.nr,
1976 out_free_irq_routing:
1982 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1988 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1990 struct page *page[1];
1993 gfn_t gfn = vmf->pgoff;
1994 struct kvm *kvm = vma->vm_file->private_data;
1996 addr = gfn_to_hva(kvm, gfn);
1997 if (kvm_is_error_hva(addr))
1998 return VM_FAULT_SIGBUS;
2000 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2002 if (unlikely(npages != 1))
2003 return VM_FAULT_SIGBUS;
2005 vmf->page = page[0];
2009 static struct vm_operations_struct kvm_vm_vm_ops = {
2010 .fault = kvm_vm_fault,
2013 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2015 vma->vm_ops = &kvm_vm_vm_ops;
2019 static struct file_operations kvm_vm_fops = {
2020 .release = kvm_vm_release,
2021 .unlocked_ioctl = kvm_vm_ioctl,
2022 .compat_ioctl = kvm_vm_ioctl,
2023 .mmap = kvm_vm_mmap,
2026 static int kvm_dev_ioctl_create_vm(void)
2031 kvm = kvm_create_vm();
2033 return PTR_ERR(kvm);
2034 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
2041 static long kvm_dev_ioctl_check_extension_generic(long arg)
2044 case KVM_CAP_USER_MEMORY:
2045 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2047 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2048 case KVM_CAP_IRQ_ROUTING:
2054 return kvm_dev_ioctl_check_extension(arg);
2057 static long kvm_dev_ioctl(struct file *filp,
2058 unsigned int ioctl, unsigned long arg)
2063 case KVM_GET_API_VERSION:
2067 r = KVM_API_VERSION;
2073 r = kvm_dev_ioctl_create_vm();
2075 case KVM_CHECK_EXTENSION:
2076 r = kvm_dev_ioctl_check_extension_generic(arg);
2078 case KVM_GET_VCPU_MMAP_SIZE:
2082 r = PAGE_SIZE; /* struct kvm_run */
2084 r += PAGE_SIZE; /* pio data page */
2086 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2087 r += PAGE_SIZE; /* coalesced mmio ring page */
2090 case KVM_TRACE_ENABLE:
2091 case KVM_TRACE_PAUSE:
2092 case KVM_TRACE_DISABLE:
2093 r = kvm_trace_ioctl(ioctl, arg);
2096 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2102 static struct file_operations kvm_chardev_ops = {
2103 .unlocked_ioctl = kvm_dev_ioctl,
2104 .compat_ioctl = kvm_dev_ioctl,
2107 static struct miscdevice kvm_dev = {
2113 static void hardware_enable(void *junk)
2115 int cpu = raw_smp_processor_id();
2117 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2119 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2120 kvm_arch_hardware_enable(NULL);
2123 static void hardware_disable(void *junk)
2125 int cpu = raw_smp_processor_id();
2127 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2129 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2130 kvm_arch_hardware_disable(NULL);
2133 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2138 val &= ~CPU_TASKS_FROZEN;
2141 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2143 hardware_disable(NULL);
2145 case CPU_UP_CANCELED:
2146 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2148 smp_call_function_single(cpu, hardware_disable, NULL, 1);
2151 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2153 smp_call_function_single(cpu, hardware_enable, NULL, 1);
2160 asmlinkage void kvm_handle_fault_on_reboot(void)
2163 /* spin while reset goes on */
2166 /* Fault while not rebooting. We want the trace. */
2169 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2171 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2174 if (val == SYS_RESTART) {
2176 * Some (well, at least mine) BIOSes hang on reboot if
2179 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2180 kvm_rebooting = true;
2181 on_each_cpu(hardware_disable, NULL, 1);
2186 static struct notifier_block kvm_reboot_notifier = {
2187 .notifier_call = kvm_reboot,
2191 void kvm_io_bus_init(struct kvm_io_bus *bus)
2193 memset(bus, 0, sizeof(*bus));
2196 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2200 for (i = 0; i < bus->dev_count; i++) {
2201 struct kvm_io_device *pos = bus->devs[i];
2203 kvm_iodevice_destructor(pos);
2207 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2208 gpa_t addr, int len, int is_write)
2212 for (i = 0; i < bus->dev_count; i++) {
2213 struct kvm_io_device *pos = bus->devs[i];
2215 if (pos->in_range(pos, addr, len, is_write))
2222 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2224 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2226 bus->devs[bus->dev_count++] = dev;
2229 static struct notifier_block kvm_cpu_notifier = {
2230 .notifier_call = kvm_cpu_hotplug,
2231 .priority = 20, /* must be > scheduler priority */
2234 static int vm_stat_get(void *_offset, u64 *val)
2236 unsigned offset = (long)_offset;
2240 spin_lock(&kvm_lock);
2241 list_for_each_entry(kvm, &vm_list, vm_list)
2242 *val += *(u32 *)((void *)kvm + offset);
2243 spin_unlock(&kvm_lock);
2247 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2249 static int vcpu_stat_get(void *_offset, u64 *val)
2251 unsigned offset = (long)_offset;
2253 struct kvm_vcpu *vcpu;
2257 spin_lock(&kvm_lock);
2258 list_for_each_entry(kvm, &vm_list, vm_list)
2259 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2260 vcpu = kvm->vcpus[i];
2262 *val += *(u32 *)((void *)vcpu + offset);
2264 spin_unlock(&kvm_lock);
2268 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2270 static struct file_operations *stat_fops[] = {
2271 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2272 [KVM_STAT_VM] = &vm_stat_fops,
2275 static void kvm_init_debug(void)
2277 struct kvm_stats_debugfs_item *p;
2279 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2280 for (p = debugfs_entries; p->name; ++p)
2281 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2282 (void *)(long)p->offset,
2283 stat_fops[p->kind]);
2286 static void kvm_exit_debug(void)
2288 struct kvm_stats_debugfs_item *p;
2290 for (p = debugfs_entries; p->name; ++p)
2291 debugfs_remove(p->dentry);
2292 debugfs_remove(kvm_debugfs_dir);
2295 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2297 hardware_disable(NULL);
2301 static int kvm_resume(struct sys_device *dev)
2303 hardware_enable(NULL);
2307 static struct sysdev_class kvm_sysdev_class = {
2309 .suspend = kvm_suspend,
2310 .resume = kvm_resume,
2313 static struct sys_device kvm_sysdev = {
2315 .cls = &kvm_sysdev_class,
2318 struct page *bad_page;
2322 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2324 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2327 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2329 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2331 kvm_arch_vcpu_load(vcpu, cpu);
2334 static void kvm_sched_out(struct preempt_notifier *pn,
2335 struct task_struct *next)
2337 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2339 kvm_arch_vcpu_put(vcpu);
2342 int kvm_init(void *opaque, unsigned int vcpu_size,
2343 struct module *module)
2350 r = kvm_arch_init(opaque);
2354 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2356 if (bad_page == NULL) {
2361 bad_pfn = page_to_pfn(bad_page);
2363 if (!alloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2368 r = kvm_arch_hardware_setup();
2372 for_each_online_cpu(cpu) {
2373 smp_call_function_single(cpu,
2374 kvm_arch_check_processor_compat,
2380 on_each_cpu(hardware_enable, NULL, 1);
2381 r = register_cpu_notifier(&kvm_cpu_notifier);
2384 register_reboot_notifier(&kvm_reboot_notifier);
2386 r = sysdev_class_register(&kvm_sysdev_class);
2390 r = sysdev_register(&kvm_sysdev);
2394 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2395 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2396 __alignof__(struct kvm_vcpu),
2398 if (!kvm_vcpu_cache) {
2403 kvm_chardev_ops.owner = module;
2404 kvm_vm_fops.owner = module;
2405 kvm_vcpu_fops.owner = module;
2407 r = misc_register(&kvm_dev);
2409 printk(KERN_ERR "kvm: misc device register failed\n");
2413 kvm_preempt_ops.sched_in = kvm_sched_in;
2414 kvm_preempt_ops.sched_out = kvm_sched_out;
2422 kmem_cache_destroy(kvm_vcpu_cache);
2424 sysdev_unregister(&kvm_sysdev);
2426 sysdev_class_unregister(&kvm_sysdev_class);
2428 unregister_reboot_notifier(&kvm_reboot_notifier);
2429 unregister_cpu_notifier(&kvm_cpu_notifier);
2431 on_each_cpu(hardware_disable, NULL, 1);
2433 kvm_arch_hardware_unsetup();
2435 free_cpumask_var(cpus_hardware_enabled);
2437 __free_page(bad_page);
2444 EXPORT_SYMBOL_GPL(kvm_init);
2448 kvm_trace_cleanup();
2449 misc_deregister(&kvm_dev);
2450 kmem_cache_destroy(kvm_vcpu_cache);
2451 sysdev_unregister(&kvm_sysdev);
2452 sysdev_class_unregister(&kvm_sysdev_class);
2453 unregister_reboot_notifier(&kvm_reboot_notifier);
2454 unregister_cpu_notifier(&kvm_cpu_notifier);
2455 on_each_cpu(hardware_disable, NULL, 1);
2456 kvm_arch_hardware_unsetup();
2459 free_cpumask_var(cpus_hardware_enabled);
2460 __free_page(bad_page);
2462 EXPORT_SYMBOL_GPL(kvm_exit);