2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/percpu.h>
25 #include <linux/gfp.h>
27 #include <linux/miscdevice.h>
28 #include <linux/vmalloc.h>
29 #include <linux/reboot.h>
30 #include <linux/debugfs.h>
31 #include <linux/highmem.h>
32 #include <linux/file.h>
33 #include <linux/sysdev.h>
34 #include <linux/cpu.h>
35 #include <linux/sched.h>
36 #include <linux/cpumask.h>
37 #include <linux/smp.h>
38 #include <linux/anon_inodes.h>
39 #include <linux/profile.h>
40 #include <linux/kvm_para.h>
41 #include <linux/pagemap.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
45 #include <asm/processor.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
50 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51 #include "coalesced_mmio.h"
54 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
55 #include <linux/pci.h>
56 #include <linux/interrupt.h>
60 MODULE_AUTHOR("Qumranet");
61 MODULE_LICENSE("GPL");
63 static int msi2intx = 1;
64 module_param(msi2intx, bool, 0);
66 DEFINE_SPINLOCK(kvm_lock);
69 static cpumask_var_t cpus_hardware_enabled;
71 struct kmem_cache *kvm_vcpu_cache;
72 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
74 static __read_mostly struct preempt_ops kvm_preempt_ops;
76 struct dentry *kvm_debugfs_dir;
78 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
81 static bool kvm_rebooting;
83 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
84 static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
87 struct list_head *ptr;
88 struct kvm_assigned_dev_kernel *match;
90 list_for_each(ptr, head) {
91 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
92 if (match->assigned_dev_id == assigned_dev_id)
98 static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
99 *assigned_dev, int irq)
102 struct msix_entry *host_msix_entries;
104 host_msix_entries = assigned_dev->host_msix_entries;
107 for (i = 0; i < assigned_dev->entries_nr; i++)
108 if (irq == host_msix_entries[i].vector) {
113 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
120 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
122 struct kvm_assigned_dev_kernel *assigned_dev;
126 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
128 kvm = assigned_dev->kvm;
130 /* This is taken to safely inject irq inside the guest. When
131 * the interrupt injection (or the ioapic code) uses a
132 * finer-grained lock, update this
134 mutex_lock(&kvm->lock);
135 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_MSIX) {
136 struct kvm_guest_msix_entry *guest_entries =
137 assigned_dev->guest_msix_entries;
138 for (i = 0; i < assigned_dev->entries_nr; i++) {
139 if (!(guest_entries[i].flags &
140 KVM_ASSIGNED_MSIX_PENDING))
142 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
143 kvm_set_irq(assigned_dev->kvm,
144 assigned_dev->irq_source_id,
145 guest_entries[i].vector, 1);
146 irq = assigned_dev->host_msix_entries[i].vector;
149 assigned_dev->host_irq_disabled = false;
152 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
153 assigned_dev->guest_irq, 1);
154 if (assigned_dev->irq_requested_type &
155 KVM_ASSIGNED_DEV_GUEST_MSI) {
156 enable_irq(assigned_dev->host_irq);
157 assigned_dev->host_irq_disabled = false;
161 mutex_unlock(&assigned_dev->kvm->lock);
164 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
166 struct kvm_assigned_dev_kernel *assigned_dev =
167 (struct kvm_assigned_dev_kernel *) dev_id;
169 if (assigned_dev->irq_requested_type == KVM_ASSIGNED_DEV_MSIX) {
170 int index = find_index_from_host_irq(assigned_dev, irq);
173 assigned_dev->guest_msix_entries[index].flags |=
174 KVM_ASSIGNED_MSIX_PENDING;
177 schedule_work(&assigned_dev->interrupt_work);
179 disable_irq_nosync(irq);
180 assigned_dev->host_irq_disabled = true;
185 /* Ack the irq line for an assigned device */
186 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
188 struct kvm_assigned_dev_kernel *dev;
193 dev = container_of(kian, struct kvm_assigned_dev_kernel,
196 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
198 /* The guest irq may be shared so this ack may be
199 * from another device.
201 if (dev->host_irq_disabled) {
202 enable_irq(dev->host_irq);
203 dev->host_irq_disabled = false;
207 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
208 static void kvm_free_assigned_irq(struct kvm *kvm,
209 struct kvm_assigned_dev_kernel *assigned_dev)
211 if (!irqchip_in_kernel(kvm))
214 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
216 if (assigned_dev->irq_source_id != -1)
217 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
218 assigned_dev->irq_source_id = -1;
220 if (!assigned_dev->irq_requested_type)
224 * In kvm_free_device_irq, cancel_work_sync return true if:
225 * 1. work is scheduled, and then cancelled.
226 * 2. work callback is executed.
228 * The first one ensured that the irq is disabled and no more events
229 * would happen. But for the second one, the irq may be enabled (e.g.
230 * for MSI). So we disable irq here to prevent further events.
232 * Notice this maybe result in nested disable if the interrupt type is
233 * INTx, but it's OK for we are going to free it.
235 * If this function is a part of VM destroy, please ensure that till
236 * now, the kvm state is still legal for probably we also have to wait
237 * interrupt_work done.
239 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_MSIX) {
241 for (i = 0; i < assigned_dev->entries_nr; i++)
242 disable_irq_nosync(assigned_dev->
243 host_msix_entries[i].vector);
245 cancel_work_sync(&assigned_dev->interrupt_work);
247 for (i = 0; i < assigned_dev->entries_nr; i++)
248 free_irq(assigned_dev->host_msix_entries[i].vector,
249 (void *)assigned_dev);
251 assigned_dev->entries_nr = 0;
252 kfree(assigned_dev->host_msix_entries);
253 kfree(assigned_dev->guest_msix_entries);
254 pci_disable_msix(assigned_dev->dev);
256 /* Deal with MSI and INTx */
257 disable_irq_nosync(assigned_dev->host_irq);
258 cancel_work_sync(&assigned_dev->interrupt_work);
260 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
262 if (assigned_dev->irq_requested_type &
263 KVM_ASSIGNED_DEV_HOST_MSI)
264 pci_disable_msi(assigned_dev->dev);
267 assigned_dev->irq_requested_type = 0;
271 static void kvm_free_assigned_device(struct kvm *kvm,
272 struct kvm_assigned_dev_kernel
275 kvm_free_assigned_irq(kvm, assigned_dev);
277 pci_reset_function(assigned_dev->dev);
279 pci_release_regions(assigned_dev->dev);
280 pci_disable_device(assigned_dev->dev);
281 pci_dev_put(assigned_dev->dev);
283 list_del(&assigned_dev->list);
287 void kvm_free_all_assigned_devices(struct kvm *kvm)
289 struct list_head *ptr, *ptr2;
290 struct kvm_assigned_dev_kernel *assigned_dev;
292 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
293 assigned_dev = list_entry(ptr,
294 struct kvm_assigned_dev_kernel,
297 kvm_free_assigned_device(kvm, assigned_dev);
301 static int assigned_device_update_intx(struct kvm *kvm,
302 struct kvm_assigned_dev_kernel *adev,
303 struct kvm_assigned_irq *airq)
305 adev->guest_irq = airq->guest_irq;
306 adev->ack_notifier.gsi = airq->guest_irq;
308 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
311 if (irqchip_in_kernel(kvm)) {
313 (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
314 free_irq(adev->host_irq, (void *)adev);
315 pci_disable_msi(adev->dev);
318 if (!capable(CAP_SYS_RAWIO))
322 adev->host_irq = airq->host_irq;
324 adev->host_irq = adev->dev->irq;
326 /* Even though this is PCI, we don't want to use shared
327 * interrupts. Sharing host devices with guest-assigned devices
328 * on the same interrupt line is not a happy situation: there
329 * are going to be long delays in accepting, acking, etc.
331 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
332 0, "kvm_assigned_intx_device", (void *)adev))
336 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
337 KVM_ASSIGNED_DEV_HOST_INTX;
342 static int assigned_device_update_msi(struct kvm *kvm,
343 struct kvm_assigned_dev_kernel *adev,
344 struct kvm_assigned_irq *airq)
348 adev->guest_irq = airq->guest_irq;
349 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
350 /* x86 don't care upper address of guest msi message addr */
351 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
352 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
353 adev->ack_notifier.gsi = -1;
354 } else if (msi2intx) {
355 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
356 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
357 adev->ack_notifier.gsi = airq->guest_irq;
360 * Guest require to disable device MSI, we disable MSI and
361 * re-enable INTx by default again. Notice it's only for
364 assigned_device_update_intx(kvm, adev, airq);
368 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
371 if (irqchip_in_kernel(kvm)) {
373 if (adev->irq_requested_type &
374 KVM_ASSIGNED_DEV_HOST_INTX)
375 free_irq(adev->host_irq, (void *)adev);
377 r = pci_enable_msi(adev->dev);
382 adev->host_irq = adev->dev->irq;
383 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
384 "kvm_assigned_msi_device", (void *)adev))
389 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
391 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
396 #ifdef __KVM_HAVE_MSIX
397 static int assigned_device_update_msix(struct kvm *kvm,
398 struct kvm_assigned_dev_kernel *adev,
399 struct kvm_assigned_irq *airq)
401 /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
404 adev->ack_notifier.gsi = -1;
406 if (irqchip_in_kernel(kvm)) {
407 if (airq->flags & KVM_DEV_IRQ_ASSIGN_MASK_MSIX)
410 if (!(airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSIX)) {
411 /* Guest disable MSI-X */
412 kvm_free_assigned_irq(kvm, adev);
414 pci_enable_msi(adev->dev);
415 if (adev->dev->msi_enabled)
416 return assigned_device_update_msi(kvm,
419 return assigned_device_update_intx(kvm, adev, airq);
422 /* host_msix_entries and guest_msix_entries should have been
424 if (adev->entries_nr == 0)
427 kvm_free_assigned_irq(kvm, adev);
429 r = pci_enable_msix(adev->dev, adev->host_msix_entries,
434 for (i = 0; i < adev->entries_nr; i++) {
435 r = request_irq((adev->host_msix_entries + i)->vector,
436 kvm_assigned_dev_intr, 0,
437 "kvm_assigned_msix_device",
444 adev->irq_requested_type |= KVM_ASSIGNED_DEV_MSIX;
450 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
451 struct kvm_assigned_irq
455 struct kvm_assigned_dev_kernel *match;
456 u32 current_flags = 0, changed_flags;
458 mutex_lock(&kvm->lock);
460 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
461 assigned_irq->assigned_dev_id);
463 mutex_unlock(&kvm->lock);
467 if (!match->irq_requested_type) {
468 INIT_WORK(&match->interrupt_work,
469 kvm_assigned_dev_interrupt_work_handler);
470 if (irqchip_in_kernel(kvm)) {
471 /* Register ack nofitier */
472 match->ack_notifier.gsi = -1;
473 match->ack_notifier.irq_acked =
474 kvm_assigned_dev_ack_irq;
475 kvm_register_irq_ack_notifier(kvm,
476 &match->ack_notifier);
478 /* Request IRQ source ID */
479 r = kvm_request_irq_source_id(kvm);
483 match->irq_source_id = r;
486 /* Determine host device irq type, we can know the
487 * result from dev->msi_enabled */
489 pci_enable_msi(match->dev);
494 if (match->irq_requested_type & KVM_ASSIGNED_DEV_MSIX)
495 current_flags |= KVM_DEV_IRQ_ASSIGN_ENABLE_MSIX;
496 else if ((match->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) &&
497 (match->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_MSI))
498 current_flags |= KVM_DEV_IRQ_ASSIGN_ENABLE_MSI;
500 changed_flags = assigned_irq->flags ^ current_flags;
502 #ifdef __KVM_HAVE_MSIX
503 if (changed_flags & KVM_DEV_IRQ_ASSIGN_MSIX_ACTION) {
504 r = assigned_device_update_msix(kvm, match, assigned_irq);
506 printk(KERN_WARNING "kvm: failed to execute "
512 if ((changed_flags & KVM_DEV_IRQ_ASSIGN_MSI_ACTION) ||
513 (msi2intx && match->dev->msi_enabled)) {
515 r = assigned_device_update_msi(kvm, match, assigned_irq);
517 printk(KERN_WARNING "kvm: failed to enable "
524 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
525 /* Host device IRQ 0 means don't support INTx */
528 "kvm: wait device to enable MSI!\n");
532 "kvm: failed to enable MSI device!\n");
537 /* Non-sharing INTx mode */
538 r = assigned_device_update_intx(kvm, match, assigned_irq);
540 printk(KERN_WARNING "kvm: failed to enable "
546 mutex_unlock(&kvm->lock);
549 mutex_unlock(&kvm->lock);
550 kvm_free_assigned_device(kvm, match);
554 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
555 struct kvm_assigned_pci_dev *assigned_dev)
558 struct kvm_assigned_dev_kernel *match;
561 down_read(&kvm->slots_lock);
562 mutex_lock(&kvm->lock);
564 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
565 assigned_dev->assigned_dev_id);
567 /* device already assigned */
572 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
574 printk(KERN_INFO "%s: Couldn't allocate memory\n",
579 dev = pci_get_bus_and_slot(assigned_dev->busnr,
580 assigned_dev->devfn);
582 printk(KERN_INFO "%s: host device not found\n", __func__);
586 if (pci_enable_device(dev)) {
587 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
591 r = pci_request_regions(dev, "kvm_assigned_device");
593 printk(KERN_INFO "%s: Could not get access to device regions\n",
598 pci_reset_function(dev);
600 match->assigned_dev_id = assigned_dev->assigned_dev_id;
601 match->host_busnr = assigned_dev->busnr;
602 match->host_devfn = assigned_dev->devfn;
603 match->flags = assigned_dev->flags;
605 match->irq_source_id = -1;
608 list_add(&match->list, &kvm->arch.assigned_dev_head);
610 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
611 if (!kvm->arch.iommu_domain) {
612 r = kvm_iommu_map_guest(kvm);
616 r = kvm_assign_device(kvm, match);
622 mutex_unlock(&kvm->lock);
623 up_read(&kvm->slots_lock);
626 list_del(&match->list);
627 pci_release_regions(dev);
629 pci_disable_device(dev);
634 mutex_unlock(&kvm->lock);
635 up_read(&kvm->slots_lock);
640 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
641 static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
642 struct kvm_assigned_pci_dev *assigned_dev)
645 struct kvm_assigned_dev_kernel *match;
647 mutex_lock(&kvm->lock);
649 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
650 assigned_dev->assigned_dev_id);
652 printk(KERN_INFO "%s: device hasn't been assigned before, "
653 "so cannot be deassigned\n", __func__);
658 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
659 kvm_deassign_device(kvm, match);
661 kvm_free_assigned_device(kvm, match);
664 mutex_unlock(&kvm->lock);
669 static inline int valid_vcpu(int n)
671 return likely(n >= 0 && n < KVM_MAX_VCPUS);
674 inline int kvm_is_mmio_pfn(pfn_t pfn)
676 if (pfn_valid(pfn)) {
677 struct page *page = compound_head(pfn_to_page(pfn));
678 return PageReserved(page);
685 * Switches to specified vcpu, until a matching vcpu_put()
687 void vcpu_load(struct kvm_vcpu *vcpu)
691 mutex_lock(&vcpu->mutex);
693 preempt_notifier_register(&vcpu->preempt_notifier);
694 kvm_arch_vcpu_load(vcpu, cpu);
698 void vcpu_put(struct kvm_vcpu *vcpu)
701 kvm_arch_vcpu_put(vcpu);
702 preempt_notifier_unregister(&vcpu->preempt_notifier);
704 mutex_unlock(&vcpu->mutex);
707 static void ack_flush(void *_completed)
711 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
716 struct kvm_vcpu *vcpu;
718 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
722 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
723 vcpu = kvm->vcpus[i];
726 if (test_and_set_bit(req, &vcpu->requests))
729 if (cpus != NULL && cpu != -1 && cpu != me)
730 cpumask_set_cpu(cpu, cpus);
732 if (unlikely(cpus == NULL))
733 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
734 else if (!cpumask_empty(cpus))
735 smp_call_function_many(cpus, ack_flush, NULL, 1);
739 free_cpumask_var(cpus);
743 void kvm_flush_remote_tlbs(struct kvm *kvm)
745 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
746 ++kvm->stat.remote_tlb_flush;
749 void kvm_reload_remote_mmus(struct kvm *kvm)
751 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
754 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
759 mutex_init(&vcpu->mutex);
763 init_waitqueue_head(&vcpu->wq);
765 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
770 vcpu->run = page_address(page);
772 r = kvm_arch_vcpu_init(vcpu);
778 free_page((unsigned long)vcpu->run);
782 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
784 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
786 kvm_arch_vcpu_uninit(vcpu);
787 free_page((unsigned long)vcpu->run);
789 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
791 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
792 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
794 return container_of(mn, struct kvm, mmu_notifier);
797 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
798 struct mm_struct *mm,
799 unsigned long address)
801 struct kvm *kvm = mmu_notifier_to_kvm(mn);
805 * When ->invalidate_page runs, the linux pte has been zapped
806 * already but the page is still allocated until
807 * ->invalidate_page returns. So if we increase the sequence
808 * here the kvm page fault will notice if the spte can't be
809 * established because the page is going to be freed. If
810 * instead the kvm page fault establishes the spte before
811 * ->invalidate_page runs, kvm_unmap_hva will release it
814 * The sequence increase only need to be seen at spin_unlock
815 * time, and not at spin_lock time.
817 * Increasing the sequence after the spin_unlock would be
818 * unsafe because the kvm page fault could then establish the
819 * pte after kvm_unmap_hva returned, without noticing the page
820 * is going to be freed.
822 spin_lock(&kvm->mmu_lock);
823 kvm->mmu_notifier_seq++;
824 need_tlb_flush = kvm_unmap_hva(kvm, address);
825 spin_unlock(&kvm->mmu_lock);
827 /* we've to flush the tlb before the pages can be freed */
829 kvm_flush_remote_tlbs(kvm);
833 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
834 struct mm_struct *mm,
838 struct kvm *kvm = mmu_notifier_to_kvm(mn);
839 int need_tlb_flush = 0;
841 spin_lock(&kvm->mmu_lock);
843 * The count increase must become visible at unlock time as no
844 * spte can be established without taking the mmu_lock and
845 * count is also read inside the mmu_lock critical section.
847 kvm->mmu_notifier_count++;
848 for (; start < end; start += PAGE_SIZE)
849 need_tlb_flush |= kvm_unmap_hva(kvm, start);
850 spin_unlock(&kvm->mmu_lock);
852 /* we've to flush the tlb before the pages can be freed */
854 kvm_flush_remote_tlbs(kvm);
857 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
858 struct mm_struct *mm,
862 struct kvm *kvm = mmu_notifier_to_kvm(mn);
864 spin_lock(&kvm->mmu_lock);
866 * This sequence increase will notify the kvm page fault that
867 * the page that is going to be mapped in the spte could have
870 kvm->mmu_notifier_seq++;
872 * The above sequence increase must be visible before the
873 * below count decrease but both values are read by the kvm
874 * page fault under mmu_lock spinlock so we don't need to add
875 * a smb_wmb() here in between the two.
877 kvm->mmu_notifier_count--;
878 spin_unlock(&kvm->mmu_lock);
880 BUG_ON(kvm->mmu_notifier_count < 0);
883 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
884 struct mm_struct *mm,
885 unsigned long address)
887 struct kvm *kvm = mmu_notifier_to_kvm(mn);
890 spin_lock(&kvm->mmu_lock);
891 young = kvm_age_hva(kvm, address);
892 spin_unlock(&kvm->mmu_lock);
895 kvm_flush_remote_tlbs(kvm);
900 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
901 struct mm_struct *mm)
903 struct kvm *kvm = mmu_notifier_to_kvm(mn);
904 kvm_arch_flush_shadow(kvm);
907 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
908 .invalidate_page = kvm_mmu_notifier_invalidate_page,
909 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
910 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
911 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
912 .release = kvm_mmu_notifier_release,
914 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
916 static struct kvm *kvm_create_vm(void)
918 struct kvm *kvm = kvm_arch_create_vm();
919 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
925 #ifdef CONFIG_HAVE_KVM_IRQCHIP
926 INIT_LIST_HEAD(&kvm->irq_routing);
927 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
930 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
931 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
934 return ERR_PTR(-ENOMEM);
936 kvm->coalesced_mmio_ring =
937 (struct kvm_coalesced_mmio_ring *)page_address(page);
940 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
943 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
944 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
946 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
955 kvm->mm = current->mm;
956 atomic_inc(&kvm->mm->mm_count);
957 spin_lock_init(&kvm->mmu_lock);
958 kvm_io_bus_init(&kvm->pio_bus);
959 mutex_init(&kvm->lock);
960 kvm_io_bus_init(&kvm->mmio_bus);
961 init_rwsem(&kvm->slots_lock);
962 atomic_set(&kvm->users_count, 1);
963 spin_lock(&kvm_lock);
964 list_add(&kvm->vm_list, &vm_list);
965 spin_unlock(&kvm_lock);
966 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
967 kvm_coalesced_mmio_init(kvm);
974 * Free any memory in @free but not in @dont.
976 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
977 struct kvm_memory_slot *dont)
979 if (!dont || free->rmap != dont->rmap)
982 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
983 vfree(free->dirty_bitmap);
985 if (!dont || free->lpage_info != dont->lpage_info)
986 vfree(free->lpage_info);
989 free->dirty_bitmap = NULL;
991 free->lpage_info = NULL;
994 void kvm_free_physmem(struct kvm *kvm)
998 for (i = 0; i < kvm->nmemslots; ++i)
999 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
1002 static void kvm_destroy_vm(struct kvm *kvm)
1004 struct mm_struct *mm = kvm->mm;
1006 kvm_arch_sync_events(kvm);
1007 spin_lock(&kvm_lock);
1008 list_del(&kvm->vm_list);
1009 spin_unlock(&kvm_lock);
1010 kvm_free_irq_routing(kvm);
1011 kvm_io_bus_destroy(&kvm->pio_bus);
1012 kvm_io_bus_destroy(&kvm->mmio_bus);
1013 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1014 if (kvm->coalesced_mmio_ring != NULL)
1015 free_page((unsigned long)kvm->coalesced_mmio_ring);
1017 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1018 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
1020 kvm_arch_destroy_vm(kvm);
1024 void kvm_get_kvm(struct kvm *kvm)
1026 atomic_inc(&kvm->users_count);
1028 EXPORT_SYMBOL_GPL(kvm_get_kvm);
1030 void kvm_put_kvm(struct kvm *kvm)
1032 if (atomic_dec_and_test(&kvm->users_count))
1033 kvm_destroy_vm(kvm);
1035 EXPORT_SYMBOL_GPL(kvm_put_kvm);
1038 static int kvm_vm_release(struct inode *inode, struct file *filp)
1040 struct kvm *kvm = filp->private_data;
1047 * Allocate some memory and give it an address in the guest physical address
1050 * Discontiguous memory is allowed, mostly for framebuffers.
1052 * Must be called holding mmap_sem for write.
1054 int __kvm_set_memory_region(struct kvm *kvm,
1055 struct kvm_userspace_memory_region *mem,
1060 unsigned long npages;
1063 struct kvm_memory_slot *memslot;
1064 struct kvm_memory_slot old, new;
1067 /* General sanity checks */
1068 if (mem->memory_size & (PAGE_SIZE - 1))
1070 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1072 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
1074 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
1076 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1079 memslot = &kvm->memslots[mem->slot];
1080 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1081 npages = mem->memory_size >> PAGE_SHIFT;
1084 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1086 new = old = *memslot;
1088 new.base_gfn = base_gfn;
1089 new.npages = npages;
1090 new.flags = mem->flags;
1092 /* Disallow changing a memory slot's size. */
1094 if (npages && old.npages && npages != old.npages)
1097 /* Check for overlaps */
1099 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1100 struct kvm_memory_slot *s = &kvm->memslots[i];
1102 if (s == memslot || !s->npages)
1104 if (!((base_gfn + npages <= s->base_gfn) ||
1105 (base_gfn >= s->base_gfn + s->npages)))
1109 /* Free page dirty bitmap if unneeded */
1110 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
1111 new.dirty_bitmap = NULL;
1115 /* Allocate if a slot is being created */
1117 if (npages && !new.rmap) {
1118 new.rmap = vmalloc(npages * sizeof(struct page *));
1123 memset(new.rmap, 0, npages * sizeof(*new.rmap));
1125 new.user_alloc = user_alloc;
1127 * hva_to_rmmap() serialzies with the mmu_lock and to be
1128 * safe it has to ignore memslots with !user_alloc &&
1132 new.userspace_addr = mem->userspace_addr;
1134 new.userspace_addr = 0;
1136 if (npages && !new.lpage_info) {
1137 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1138 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
1140 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1142 if (!new.lpage_info)
1145 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1147 if (base_gfn % KVM_PAGES_PER_HPAGE)
1148 new.lpage_info[0].write_count = 1;
1149 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1150 new.lpage_info[largepages-1].write_count = 1;
1153 /* Allocate page dirty bitmap if needed */
1154 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1155 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1157 new.dirty_bitmap = vmalloc(dirty_bytes);
1158 if (!new.dirty_bitmap)
1160 memset(new.dirty_bitmap, 0, dirty_bytes);
1162 #endif /* not defined CONFIG_S390 */
1165 kvm_arch_flush_shadow(kvm);
1167 spin_lock(&kvm->mmu_lock);
1168 if (mem->slot >= kvm->nmemslots)
1169 kvm->nmemslots = mem->slot + 1;
1172 spin_unlock(&kvm->mmu_lock);
1174 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1176 spin_lock(&kvm->mmu_lock);
1178 spin_unlock(&kvm->mmu_lock);
1182 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1183 /* Slot deletion case: we have to update the current slot */
1187 /* map the pages in iommu page table */
1188 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1195 kvm_free_physmem_slot(&new, &old);
1200 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1202 int kvm_set_memory_region(struct kvm *kvm,
1203 struct kvm_userspace_memory_region *mem,
1208 down_write(&kvm->slots_lock);
1209 r = __kvm_set_memory_region(kvm, mem, user_alloc);
1210 up_write(&kvm->slots_lock);
1213 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1215 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1217 kvm_userspace_memory_region *mem,
1220 if (mem->slot >= KVM_MEMORY_SLOTS)
1222 return kvm_set_memory_region(kvm, mem, user_alloc);
1225 int kvm_get_dirty_log(struct kvm *kvm,
1226 struct kvm_dirty_log *log, int *is_dirty)
1228 struct kvm_memory_slot *memslot;
1231 unsigned long any = 0;
1234 if (log->slot >= KVM_MEMORY_SLOTS)
1237 memslot = &kvm->memslots[log->slot];
1239 if (!memslot->dirty_bitmap)
1242 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1244 for (i = 0; !any && i < n/sizeof(long); ++i)
1245 any = memslot->dirty_bitmap[i];
1248 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1259 int is_error_page(struct page *page)
1261 return page == bad_page;
1263 EXPORT_SYMBOL_GPL(is_error_page);
1265 int is_error_pfn(pfn_t pfn)
1267 return pfn == bad_pfn;
1269 EXPORT_SYMBOL_GPL(is_error_pfn);
1271 static inline unsigned long bad_hva(void)
1276 int kvm_is_error_hva(unsigned long addr)
1278 return addr == bad_hva();
1280 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1282 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1286 for (i = 0; i < kvm->nmemslots; ++i) {
1287 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1289 if (gfn >= memslot->base_gfn
1290 && gfn < memslot->base_gfn + memslot->npages)
1295 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1297 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1299 gfn = unalias_gfn(kvm, gfn);
1300 return gfn_to_memslot_unaliased(kvm, gfn);
1303 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1307 gfn = unalias_gfn(kvm, gfn);
1308 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1309 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1311 if (gfn >= memslot->base_gfn
1312 && gfn < memslot->base_gfn + memslot->npages)
1317 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1319 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1321 struct kvm_memory_slot *slot;
1323 gfn = unalias_gfn(kvm, gfn);
1324 slot = gfn_to_memslot_unaliased(kvm, gfn);
1327 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1329 EXPORT_SYMBOL_GPL(gfn_to_hva);
1331 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1333 struct page *page[1];
1340 addr = gfn_to_hva(kvm, gfn);
1341 if (kvm_is_error_hva(addr)) {
1343 return page_to_pfn(bad_page);
1346 npages = get_user_pages_fast(addr, 1, 1, page);
1348 if (unlikely(npages != 1)) {
1349 struct vm_area_struct *vma;
1351 down_read(¤t->mm->mmap_sem);
1352 vma = find_vma(current->mm, addr);
1354 if (vma == NULL || addr < vma->vm_start ||
1355 !(vma->vm_flags & VM_PFNMAP)) {
1356 up_read(¤t->mm->mmap_sem);
1358 return page_to_pfn(bad_page);
1361 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1362 up_read(¤t->mm->mmap_sem);
1363 BUG_ON(!kvm_is_mmio_pfn(pfn));
1365 pfn = page_to_pfn(page[0]);
1370 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1372 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1376 pfn = gfn_to_pfn(kvm, gfn);
1377 if (!kvm_is_mmio_pfn(pfn))
1378 return pfn_to_page(pfn);
1380 WARN_ON(kvm_is_mmio_pfn(pfn));
1386 EXPORT_SYMBOL_GPL(gfn_to_page);
1388 void kvm_release_page_clean(struct page *page)
1390 kvm_release_pfn_clean(page_to_pfn(page));
1392 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1394 void kvm_release_pfn_clean(pfn_t pfn)
1396 if (!kvm_is_mmio_pfn(pfn))
1397 put_page(pfn_to_page(pfn));
1399 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1401 void kvm_release_page_dirty(struct page *page)
1403 kvm_release_pfn_dirty(page_to_pfn(page));
1405 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1407 void kvm_release_pfn_dirty(pfn_t pfn)
1409 kvm_set_pfn_dirty(pfn);
1410 kvm_release_pfn_clean(pfn);
1412 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1414 void kvm_set_page_dirty(struct page *page)
1416 kvm_set_pfn_dirty(page_to_pfn(page));
1418 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1420 void kvm_set_pfn_dirty(pfn_t pfn)
1422 if (!kvm_is_mmio_pfn(pfn)) {
1423 struct page *page = pfn_to_page(pfn);
1424 if (!PageReserved(page))
1428 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1430 void kvm_set_pfn_accessed(pfn_t pfn)
1432 if (!kvm_is_mmio_pfn(pfn))
1433 mark_page_accessed(pfn_to_page(pfn));
1435 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1437 void kvm_get_pfn(pfn_t pfn)
1439 if (!kvm_is_mmio_pfn(pfn))
1440 get_page(pfn_to_page(pfn));
1442 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1444 static int next_segment(unsigned long len, int offset)
1446 if (len > PAGE_SIZE - offset)
1447 return PAGE_SIZE - offset;
1452 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1458 addr = gfn_to_hva(kvm, gfn);
1459 if (kvm_is_error_hva(addr))
1461 r = copy_from_user(data, (void __user *)addr + offset, len);
1466 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1468 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1470 gfn_t gfn = gpa >> PAGE_SHIFT;
1472 int offset = offset_in_page(gpa);
1475 while ((seg = next_segment(len, offset)) != 0) {
1476 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1486 EXPORT_SYMBOL_GPL(kvm_read_guest);
1488 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1493 gfn_t gfn = gpa >> PAGE_SHIFT;
1494 int offset = offset_in_page(gpa);
1496 addr = gfn_to_hva(kvm, gfn);
1497 if (kvm_is_error_hva(addr))
1499 pagefault_disable();
1500 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1506 EXPORT_SYMBOL(kvm_read_guest_atomic);
1508 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1509 int offset, int len)
1514 addr = gfn_to_hva(kvm, gfn);
1515 if (kvm_is_error_hva(addr))
1517 r = copy_to_user((void __user *)addr + offset, data, len);
1520 mark_page_dirty(kvm, gfn);
1523 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1525 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1528 gfn_t gfn = gpa >> PAGE_SHIFT;
1530 int offset = offset_in_page(gpa);
1533 while ((seg = next_segment(len, offset)) != 0) {
1534 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1545 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1547 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1549 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1551 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1553 gfn_t gfn = gpa >> PAGE_SHIFT;
1555 int offset = offset_in_page(gpa);
1558 while ((seg = next_segment(len, offset)) != 0) {
1559 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1568 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1570 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1572 struct kvm_memory_slot *memslot;
1574 gfn = unalias_gfn(kvm, gfn);
1575 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1576 if (memslot && memslot->dirty_bitmap) {
1577 unsigned long rel_gfn = gfn - memslot->base_gfn;
1580 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1581 set_bit(rel_gfn, memslot->dirty_bitmap);
1586 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1588 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1593 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1595 if (kvm_cpu_has_interrupt(vcpu) ||
1596 kvm_cpu_has_pending_timer(vcpu) ||
1597 kvm_arch_vcpu_runnable(vcpu)) {
1598 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1601 if (signal_pending(current))
1609 finish_wait(&vcpu->wq, &wait);
1612 void kvm_resched(struct kvm_vcpu *vcpu)
1614 if (!need_resched())
1618 EXPORT_SYMBOL_GPL(kvm_resched);
1620 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1622 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1625 if (vmf->pgoff == 0)
1626 page = virt_to_page(vcpu->run);
1628 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1629 page = virt_to_page(vcpu->arch.pio_data);
1631 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1632 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1633 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1636 return VM_FAULT_SIGBUS;
1642 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1643 .fault = kvm_vcpu_fault,
1646 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1648 vma->vm_ops = &kvm_vcpu_vm_ops;
1652 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1654 struct kvm_vcpu *vcpu = filp->private_data;
1656 kvm_put_kvm(vcpu->kvm);
1660 static struct file_operations kvm_vcpu_fops = {
1661 .release = kvm_vcpu_release,
1662 .unlocked_ioctl = kvm_vcpu_ioctl,
1663 .compat_ioctl = kvm_vcpu_ioctl,
1664 .mmap = kvm_vcpu_mmap,
1668 * Allocates an inode for the vcpu.
1670 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1672 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1674 kvm_put_kvm(vcpu->kvm);
1679 * Creates some virtual cpus. Good luck creating more than one.
1681 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1684 struct kvm_vcpu *vcpu;
1689 vcpu = kvm_arch_vcpu_create(kvm, n);
1691 return PTR_ERR(vcpu);
1693 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1695 r = kvm_arch_vcpu_setup(vcpu);
1699 mutex_lock(&kvm->lock);
1700 if (kvm->vcpus[n]) {
1704 kvm->vcpus[n] = vcpu;
1705 mutex_unlock(&kvm->lock);
1707 /* Now it's all set up, let userspace reach it */
1709 r = create_vcpu_fd(vcpu);
1715 mutex_lock(&kvm->lock);
1716 kvm->vcpus[n] = NULL;
1718 mutex_unlock(&kvm->lock);
1719 kvm_arch_vcpu_destroy(vcpu);
1723 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1726 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1727 vcpu->sigset_active = 1;
1728 vcpu->sigset = *sigset;
1730 vcpu->sigset_active = 0;
1734 #ifdef __KVM_HAVE_MSIX
1735 static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1736 struct kvm_assigned_msix_nr *entry_nr)
1739 struct kvm_assigned_dev_kernel *adev;
1741 mutex_lock(&kvm->lock);
1743 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1744 entry_nr->assigned_dev_id);
1750 if (adev->entries_nr == 0) {
1751 adev->entries_nr = entry_nr->entry_nr;
1752 if (adev->entries_nr == 0 ||
1753 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1758 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1761 if (!adev->host_msix_entries) {
1765 adev->guest_msix_entries = kzalloc(
1766 sizeof(struct kvm_guest_msix_entry) *
1767 entry_nr->entry_nr, GFP_KERNEL);
1768 if (!adev->guest_msix_entries) {
1769 kfree(adev->host_msix_entries);
1773 } else /* Not allowed set MSI-X number twice */
1776 mutex_unlock(&kvm->lock);
1780 static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1781 struct kvm_assigned_msix_entry *entry)
1784 struct kvm_assigned_dev_kernel *adev;
1786 mutex_lock(&kvm->lock);
1788 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1789 entry->assigned_dev_id);
1793 goto msix_entry_out;
1796 for (i = 0; i < adev->entries_nr; i++)
1797 if (adev->guest_msix_entries[i].vector == 0 ||
1798 adev->guest_msix_entries[i].entry == entry->entry) {
1799 adev->guest_msix_entries[i].entry = entry->entry;
1800 adev->guest_msix_entries[i].vector = entry->gsi;
1801 adev->host_msix_entries[i].entry = entry->entry;
1804 if (i == adev->entries_nr) {
1806 goto msix_entry_out;
1810 mutex_unlock(&kvm->lock);
1816 static long kvm_vcpu_ioctl(struct file *filp,
1817 unsigned int ioctl, unsigned long arg)
1819 struct kvm_vcpu *vcpu = filp->private_data;
1820 void __user *argp = (void __user *)arg;
1822 struct kvm_fpu *fpu = NULL;
1823 struct kvm_sregs *kvm_sregs = NULL;
1825 if (vcpu->kvm->mm != current->mm)
1832 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1834 case KVM_GET_REGS: {
1835 struct kvm_regs *kvm_regs;
1838 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1841 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1845 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1852 case KVM_SET_REGS: {
1853 struct kvm_regs *kvm_regs;
1856 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1860 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1862 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1870 case KVM_GET_SREGS: {
1871 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1875 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1879 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1884 case KVM_SET_SREGS: {
1885 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1890 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1892 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1898 case KVM_GET_MP_STATE: {
1899 struct kvm_mp_state mp_state;
1901 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1905 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1910 case KVM_SET_MP_STATE: {
1911 struct kvm_mp_state mp_state;
1914 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1916 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1922 case KVM_TRANSLATE: {
1923 struct kvm_translation tr;
1926 if (copy_from_user(&tr, argp, sizeof tr))
1928 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1932 if (copy_to_user(argp, &tr, sizeof tr))
1937 case KVM_SET_GUEST_DEBUG: {
1938 struct kvm_guest_debug dbg;
1941 if (copy_from_user(&dbg, argp, sizeof dbg))
1943 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1949 case KVM_SET_SIGNAL_MASK: {
1950 struct kvm_signal_mask __user *sigmask_arg = argp;
1951 struct kvm_signal_mask kvm_sigmask;
1952 sigset_t sigset, *p;
1957 if (copy_from_user(&kvm_sigmask, argp,
1958 sizeof kvm_sigmask))
1961 if (kvm_sigmask.len != sizeof sigset)
1964 if (copy_from_user(&sigset, sigmask_arg->sigset,
1969 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1973 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1977 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1981 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1987 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1992 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1994 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
2001 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
2009 static long kvm_vm_ioctl(struct file *filp,
2010 unsigned int ioctl, unsigned long arg)
2012 struct kvm *kvm = filp->private_data;
2013 void __user *argp = (void __user *)arg;
2016 if (kvm->mm != current->mm)
2019 case KVM_CREATE_VCPU:
2020 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2024 case KVM_SET_USER_MEMORY_REGION: {
2025 struct kvm_userspace_memory_region kvm_userspace_mem;
2028 if (copy_from_user(&kvm_userspace_mem, argp,
2029 sizeof kvm_userspace_mem))
2032 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
2037 case KVM_GET_DIRTY_LOG: {
2038 struct kvm_dirty_log log;
2041 if (copy_from_user(&log, argp, sizeof log))
2043 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2048 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2049 case KVM_REGISTER_COALESCED_MMIO: {
2050 struct kvm_coalesced_mmio_zone zone;
2052 if (copy_from_user(&zone, argp, sizeof zone))
2055 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2061 case KVM_UNREGISTER_COALESCED_MMIO: {
2062 struct kvm_coalesced_mmio_zone zone;
2064 if (copy_from_user(&zone, argp, sizeof zone))
2067 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2074 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
2075 case KVM_ASSIGN_PCI_DEVICE: {
2076 struct kvm_assigned_pci_dev assigned_dev;
2079 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2081 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2086 case KVM_ASSIGN_IRQ: {
2087 struct kvm_assigned_irq assigned_irq;
2090 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2092 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2098 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2099 case KVM_DEASSIGN_PCI_DEVICE: {
2100 struct kvm_assigned_pci_dev assigned_dev;
2103 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2105 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2111 #ifdef KVM_CAP_IRQ_ROUTING
2112 case KVM_SET_GSI_ROUTING: {
2113 struct kvm_irq_routing routing;
2114 struct kvm_irq_routing __user *urouting;
2115 struct kvm_irq_routing_entry *entries;
2118 if (copy_from_user(&routing, argp, sizeof(routing)))
2121 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2126 entries = vmalloc(routing.nr * sizeof(*entries));
2131 if (copy_from_user(entries, urouting->entries,
2132 routing.nr * sizeof(*entries)))
2133 goto out_free_irq_routing;
2134 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2136 out_free_irq_routing:
2140 #ifdef __KVM_HAVE_MSIX
2141 case KVM_ASSIGN_SET_MSIX_NR: {
2142 struct kvm_assigned_msix_nr entry_nr;
2144 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2146 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2151 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2152 struct kvm_assigned_msix_entry entry;
2154 if (copy_from_user(&entry, argp, sizeof entry))
2156 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2162 #endif /* KVM_CAP_IRQ_ROUTING */
2164 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2170 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2172 struct page *page[1];
2175 gfn_t gfn = vmf->pgoff;
2176 struct kvm *kvm = vma->vm_file->private_data;
2178 addr = gfn_to_hva(kvm, gfn);
2179 if (kvm_is_error_hva(addr))
2180 return VM_FAULT_SIGBUS;
2182 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2184 if (unlikely(npages != 1))
2185 return VM_FAULT_SIGBUS;
2187 vmf->page = page[0];
2191 static struct vm_operations_struct kvm_vm_vm_ops = {
2192 .fault = kvm_vm_fault,
2195 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2197 vma->vm_ops = &kvm_vm_vm_ops;
2201 static struct file_operations kvm_vm_fops = {
2202 .release = kvm_vm_release,
2203 .unlocked_ioctl = kvm_vm_ioctl,
2204 .compat_ioctl = kvm_vm_ioctl,
2205 .mmap = kvm_vm_mmap,
2208 static int kvm_dev_ioctl_create_vm(void)
2213 kvm = kvm_create_vm();
2215 return PTR_ERR(kvm);
2216 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
2223 static long kvm_dev_ioctl_check_extension_generic(long arg)
2226 case KVM_CAP_USER_MEMORY:
2227 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2228 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2230 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2231 case KVM_CAP_IRQ_ROUTING:
2232 return KVM_MAX_IRQ_ROUTES;
2237 return kvm_dev_ioctl_check_extension(arg);
2240 static long kvm_dev_ioctl(struct file *filp,
2241 unsigned int ioctl, unsigned long arg)
2246 case KVM_GET_API_VERSION:
2250 r = KVM_API_VERSION;
2256 r = kvm_dev_ioctl_create_vm();
2258 case KVM_CHECK_EXTENSION:
2259 r = kvm_dev_ioctl_check_extension_generic(arg);
2261 case KVM_GET_VCPU_MMAP_SIZE:
2265 r = PAGE_SIZE; /* struct kvm_run */
2267 r += PAGE_SIZE; /* pio data page */
2269 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2270 r += PAGE_SIZE; /* coalesced mmio ring page */
2273 case KVM_TRACE_ENABLE:
2274 case KVM_TRACE_PAUSE:
2275 case KVM_TRACE_DISABLE:
2276 r = kvm_trace_ioctl(ioctl, arg);
2279 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2285 static struct file_operations kvm_chardev_ops = {
2286 .unlocked_ioctl = kvm_dev_ioctl,
2287 .compat_ioctl = kvm_dev_ioctl,
2290 static struct miscdevice kvm_dev = {
2296 static void hardware_enable(void *junk)
2298 int cpu = raw_smp_processor_id();
2300 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2302 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2303 kvm_arch_hardware_enable(NULL);
2306 static void hardware_disable(void *junk)
2308 int cpu = raw_smp_processor_id();
2310 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2312 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2313 kvm_arch_hardware_disable(NULL);
2316 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2321 val &= ~CPU_TASKS_FROZEN;
2324 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2326 hardware_disable(NULL);
2328 case CPU_UP_CANCELED:
2329 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2331 smp_call_function_single(cpu, hardware_disable, NULL, 1);
2334 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2336 smp_call_function_single(cpu, hardware_enable, NULL, 1);
2343 asmlinkage void kvm_handle_fault_on_reboot(void)
2346 /* spin while reset goes on */
2349 /* Fault while not rebooting. We want the trace. */
2352 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2354 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2357 if (val == SYS_RESTART) {
2359 * Some (well, at least mine) BIOSes hang on reboot if
2362 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2363 kvm_rebooting = true;
2364 on_each_cpu(hardware_disable, NULL, 1);
2369 static struct notifier_block kvm_reboot_notifier = {
2370 .notifier_call = kvm_reboot,
2374 void kvm_io_bus_init(struct kvm_io_bus *bus)
2376 memset(bus, 0, sizeof(*bus));
2379 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2383 for (i = 0; i < bus->dev_count; i++) {
2384 struct kvm_io_device *pos = bus->devs[i];
2386 kvm_iodevice_destructor(pos);
2390 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2391 gpa_t addr, int len, int is_write)
2395 for (i = 0; i < bus->dev_count; i++) {
2396 struct kvm_io_device *pos = bus->devs[i];
2398 if (pos->in_range(pos, addr, len, is_write))
2405 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2407 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2409 bus->devs[bus->dev_count++] = dev;
2412 static struct notifier_block kvm_cpu_notifier = {
2413 .notifier_call = kvm_cpu_hotplug,
2414 .priority = 20, /* must be > scheduler priority */
2417 static int vm_stat_get(void *_offset, u64 *val)
2419 unsigned offset = (long)_offset;
2423 spin_lock(&kvm_lock);
2424 list_for_each_entry(kvm, &vm_list, vm_list)
2425 *val += *(u32 *)((void *)kvm + offset);
2426 spin_unlock(&kvm_lock);
2430 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2432 static int vcpu_stat_get(void *_offset, u64 *val)
2434 unsigned offset = (long)_offset;
2436 struct kvm_vcpu *vcpu;
2440 spin_lock(&kvm_lock);
2441 list_for_each_entry(kvm, &vm_list, vm_list)
2442 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2443 vcpu = kvm->vcpus[i];
2445 *val += *(u32 *)((void *)vcpu + offset);
2447 spin_unlock(&kvm_lock);
2451 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2453 static struct file_operations *stat_fops[] = {
2454 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2455 [KVM_STAT_VM] = &vm_stat_fops,
2458 static void kvm_init_debug(void)
2460 struct kvm_stats_debugfs_item *p;
2462 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2463 for (p = debugfs_entries; p->name; ++p)
2464 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2465 (void *)(long)p->offset,
2466 stat_fops[p->kind]);
2469 static void kvm_exit_debug(void)
2471 struct kvm_stats_debugfs_item *p;
2473 for (p = debugfs_entries; p->name; ++p)
2474 debugfs_remove(p->dentry);
2475 debugfs_remove(kvm_debugfs_dir);
2478 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2480 hardware_disable(NULL);
2484 static int kvm_resume(struct sys_device *dev)
2486 hardware_enable(NULL);
2490 static struct sysdev_class kvm_sysdev_class = {
2492 .suspend = kvm_suspend,
2493 .resume = kvm_resume,
2496 static struct sys_device kvm_sysdev = {
2498 .cls = &kvm_sysdev_class,
2501 struct page *bad_page;
2505 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2507 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2510 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2512 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2514 kvm_arch_vcpu_load(vcpu, cpu);
2517 static void kvm_sched_out(struct preempt_notifier *pn,
2518 struct task_struct *next)
2520 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2522 kvm_arch_vcpu_put(vcpu);
2525 int kvm_init(void *opaque, unsigned int vcpu_size,
2526 struct module *module)
2533 r = kvm_arch_init(opaque);
2537 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2539 if (bad_page == NULL) {
2544 bad_pfn = page_to_pfn(bad_page);
2546 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2550 cpumask_clear(cpus_hardware_enabled);
2552 r = kvm_arch_hardware_setup();
2556 for_each_online_cpu(cpu) {
2557 smp_call_function_single(cpu,
2558 kvm_arch_check_processor_compat,
2564 on_each_cpu(hardware_enable, NULL, 1);
2565 r = register_cpu_notifier(&kvm_cpu_notifier);
2568 register_reboot_notifier(&kvm_reboot_notifier);
2570 r = sysdev_class_register(&kvm_sysdev_class);
2574 r = sysdev_register(&kvm_sysdev);
2578 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2579 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2580 __alignof__(struct kvm_vcpu),
2582 if (!kvm_vcpu_cache) {
2587 kvm_chardev_ops.owner = module;
2588 kvm_vm_fops.owner = module;
2589 kvm_vcpu_fops.owner = module;
2591 r = misc_register(&kvm_dev);
2593 printk(KERN_ERR "kvm: misc device register failed\n");
2597 kvm_preempt_ops.sched_in = kvm_sched_in;
2598 kvm_preempt_ops.sched_out = kvm_sched_out;
2606 kmem_cache_destroy(kvm_vcpu_cache);
2608 sysdev_unregister(&kvm_sysdev);
2610 sysdev_class_unregister(&kvm_sysdev_class);
2612 unregister_reboot_notifier(&kvm_reboot_notifier);
2613 unregister_cpu_notifier(&kvm_cpu_notifier);
2615 on_each_cpu(hardware_disable, NULL, 1);
2617 kvm_arch_hardware_unsetup();
2619 free_cpumask_var(cpus_hardware_enabled);
2621 __free_page(bad_page);
2628 EXPORT_SYMBOL_GPL(kvm_init);
2632 kvm_trace_cleanup();
2633 misc_deregister(&kvm_dev);
2634 kmem_cache_destroy(kvm_vcpu_cache);
2635 sysdev_unregister(&kvm_sysdev);
2636 sysdev_class_unregister(&kvm_sysdev_class);
2637 unregister_reboot_notifier(&kvm_reboot_notifier);
2638 unregister_cpu_notifier(&kvm_cpu_notifier);
2639 on_each_cpu(hardware_disable, NULL, 1);
2640 kvm_arch_hardware_unsetup();
2643 free_cpumask_var(cpus_hardware_enabled);
2644 __free_page(bad_page);
2646 EXPORT_SYMBOL_GPL(kvm_exit);