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 void kvm_assigned_dev_interrupt_work_handler(struct work_struct
*work
)
100 struct kvm_assigned_dev_kernel
*assigned_dev
;
102 assigned_dev
= container_of(work
, struct kvm_assigned_dev_kernel
,
105 /* This is taken to safely inject irq inside the guest. When
106 * the interrupt injection (or the ioapic code) uses a
107 * finer-grained lock, update this
109 mutex_lock(&assigned_dev
->kvm
->lock
);
110 kvm_set_irq(assigned_dev
->kvm
, assigned_dev
->irq_source_id
,
111 assigned_dev
->guest_irq
, 1);
113 if (assigned_dev
->irq_requested_type
& KVM_ASSIGNED_DEV_GUEST_MSI
) {
114 enable_irq(assigned_dev
->host_irq
);
115 assigned_dev
->host_irq_disabled
= false;
117 mutex_unlock(&assigned_dev
->kvm
->lock
);
120 static irqreturn_t
kvm_assigned_dev_intr(int irq
, void *dev_id
)
122 struct kvm_assigned_dev_kernel
*assigned_dev
=
123 (struct kvm_assigned_dev_kernel
*) dev_id
;
125 schedule_work(&assigned_dev
->interrupt_work
);
127 disable_irq_nosync(irq
);
128 assigned_dev
->host_irq_disabled
= true;
133 /* Ack the irq line for an assigned device */
134 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
136 struct kvm_assigned_dev_kernel
*dev
;
141 dev
= container_of(kian
, struct kvm_assigned_dev_kernel
,
144 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
146 /* The guest irq may be shared so this ack may be
147 * from another device.
149 if (dev
->host_irq_disabled
) {
150 enable_irq(dev
->host_irq
);
151 dev
->host_irq_disabled
= false;
155 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
156 static void kvm_free_assigned_irq(struct kvm
*kvm
,
157 struct kvm_assigned_dev_kernel
*assigned_dev
)
159 if (!irqchip_in_kernel(kvm
))
162 kvm_unregister_irq_ack_notifier(&assigned_dev
->ack_notifier
);
164 if (assigned_dev
->irq_source_id
!= -1)
165 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
166 assigned_dev
->irq_source_id
= -1;
168 if (!assigned_dev
->irq_requested_type
)
172 * In kvm_free_device_irq, cancel_work_sync return true if:
173 * 1. work is scheduled, and then cancelled.
174 * 2. work callback is executed.
176 * The first one ensured that the irq is disabled and no more events
177 * would happen. But for the second one, the irq may be enabled (e.g.
178 * for MSI). So we disable irq here to prevent further events.
180 * Notice this maybe result in nested disable if the interrupt type is
181 * INTx, but it's OK for we are going to free it.
183 * If this function is a part of VM destroy, please ensure that till
184 * now, the kvm state is still legal for probably we also have to wait
185 * interrupt_work done.
187 disable_irq_nosync(assigned_dev
->host_irq
);
188 cancel_work_sync(&assigned_dev
->interrupt_work
);
190 free_irq(assigned_dev
->host_irq
, (void *)assigned_dev
);
192 if (assigned_dev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
)
193 pci_disable_msi(assigned_dev
->dev
);
195 assigned_dev
->irq_requested_type
= 0;
199 static void kvm_free_assigned_device(struct kvm
*kvm
,
200 struct kvm_assigned_dev_kernel
203 kvm_free_assigned_irq(kvm
, assigned_dev
);
205 pci_reset_function(assigned_dev
->dev
);
207 pci_release_regions(assigned_dev
->dev
);
208 pci_disable_device(assigned_dev
->dev
);
209 pci_dev_put(assigned_dev
->dev
);
211 list_del(&assigned_dev
->list
);
215 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
217 struct list_head
*ptr
, *ptr2
;
218 struct kvm_assigned_dev_kernel
*assigned_dev
;
220 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
221 assigned_dev
= list_entry(ptr
,
222 struct kvm_assigned_dev_kernel
,
225 kvm_free_assigned_device(kvm
, assigned_dev
);
229 static int assigned_device_update_intx(struct kvm
*kvm
,
230 struct kvm_assigned_dev_kernel
*adev
,
231 struct kvm_assigned_irq
*airq
)
233 adev
->guest_irq
= airq
->guest_irq
;
234 adev
->ack_notifier
.gsi
= airq
->guest_irq
;
236 if (adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_INTX
)
239 if (irqchip_in_kernel(kvm
)) {
241 (adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
)) {
242 free_irq(adev
->host_irq
, (void *)adev
);
243 pci_disable_msi(adev
->dev
);
246 if (!capable(CAP_SYS_RAWIO
))
250 adev
->host_irq
= airq
->host_irq
;
252 adev
->host_irq
= adev
->dev
->irq
;
254 /* Even though this is PCI, we don't want to use shared
255 * interrupts. Sharing host devices with guest-assigned devices
256 * on the same interrupt line is not a happy situation: there
257 * are going to be long delays in accepting, acking, etc.
259 if (request_irq(adev
->host_irq
, kvm_assigned_dev_intr
,
260 0, "kvm_assigned_intx_device", (void *)adev
))
264 adev
->irq_requested_type
= KVM_ASSIGNED_DEV_GUEST_INTX
|
265 KVM_ASSIGNED_DEV_HOST_INTX
;
270 static int assigned_device_update_msi(struct kvm
*kvm
,
271 struct kvm_assigned_dev_kernel
*adev
,
272 struct kvm_assigned_irq
*airq
)
276 adev
->guest_irq
= airq
->guest_irq
;
277 if (airq
->flags
& KVM_DEV_IRQ_ASSIGN_ENABLE_MSI
) {
278 /* x86 don't care upper address of guest msi message addr */
279 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_GUEST_MSI
;
280 adev
->irq_requested_type
&= ~KVM_ASSIGNED_DEV_GUEST_INTX
;
281 adev
->ack_notifier
.gsi
= -1;
282 } else if (msi2intx
) {
283 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_GUEST_INTX
;
284 adev
->irq_requested_type
&= ~KVM_ASSIGNED_DEV_GUEST_MSI
;
285 adev
->ack_notifier
.gsi
= airq
->guest_irq
;
288 * Guest require to disable device MSI, we disable MSI and
289 * re-enable INTx by default again. Notice it's only for
292 assigned_device_update_intx(kvm
, adev
, airq
);
296 if (adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
)
299 if (irqchip_in_kernel(kvm
)) {
301 if (adev
->irq_requested_type
&
302 KVM_ASSIGNED_DEV_HOST_INTX
)
303 free_irq(adev
->host_irq
, (void *)adev
);
305 r
= pci_enable_msi(adev
->dev
);
310 adev
->host_irq
= adev
->dev
->irq
;
311 if (request_irq(adev
->host_irq
, kvm_assigned_dev_intr
, 0,
312 "kvm_assigned_msi_device", (void *)adev
))
317 adev
->irq_requested_type
= KVM_ASSIGNED_DEV_GUEST_MSI
;
319 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_HOST_MSI
;
324 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
325 struct kvm_assigned_irq
329 struct kvm_assigned_dev_kernel
*match
;
330 u32 current_flags
= 0, changed_flags
;
332 mutex_lock(&kvm
->lock
);
334 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
335 assigned_irq
->assigned_dev_id
);
337 mutex_unlock(&kvm
->lock
);
341 if (!match
->irq_requested_type
) {
342 INIT_WORK(&match
->interrupt_work
,
343 kvm_assigned_dev_interrupt_work_handler
);
344 if (irqchip_in_kernel(kvm
)) {
345 /* Register ack nofitier */
346 match
->ack_notifier
.gsi
= -1;
347 match
->ack_notifier
.irq_acked
=
348 kvm_assigned_dev_ack_irq
;
349 kvm_register_irq_ack_notifier(kvm
,
350 &match
->ack_notifier
);
352 /* Request IRQ source ID */
353 r
= kvm_request_irq_source_id(kvm
);
357 match
->irq_source_id
= r
;
360 /* Determine host device irq type, we can know the
361 * result from dev->msi_enabled */
363 pci_enable_msi(match
->dev
);
368 if ((match
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
) &&
369 (match
->irq_requested_type
& KVM_ASSIGNED_DEV_GUEST_MSI
))
370 current_flags
|= KVM_DEV_IRQ_ASSIGN_ENABLE_MSI
;
372 changed_flags
= assigned_irq
->flags
^ current_flags
;
374 if ((changed_flags
& KVM_DEV_IRQ_ASSIGN_MSI_ACTION
) ||
375 (msi2intx
&& match
->dev
->msi_enabled
)) {
377 r
= assigned_device_update_msi(kvm
, match
, assigned_irq
);
379 printk(KERN_WARNING
"kvm: failed to enable "
386 } else if (assigned_irq
->host_irq
== 0 && match
->dev
->irq
== 0) {
387 /* Host device IRQ 0 means don't support INTx */
390 "kvm: wait device to enable MSI!\n");
394 "kvm: failed to enable MSI device!\n");
399 /* Non-sharing INTx mode */
400 r
= assigned_device_update_intx(kvm
, match
, assigned_irq
);
402 printk(KERN_WARNING
"kvm: failed to enable "
408 mutex_unlock(&kvm
->lock
);
411 mutex_unlock(&kvm
->lock
);
412 kvm_free_assigned_device(kvm
, match
);
416 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
417 struct kvm_assigned_pci_dev
*assigned_dev
)
420 struct kvm_assigned_dev_kernel
*match
;
423 down_read(&kvm
->slots_lock
);
424 mutex_lock(&kvm
->lock
);
426 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
427 assigned_dev
->assigned_dev_id
);
429 /* device already assigned */
434 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
436 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
441 dev
= pci_get_bus_and_slot(assigned_dev
->busnr
,
442 assigned_dev
->devfn
);
444 printk(KERN_INFO
"%s: host device not found\n", __func__
);
448 if (pci_enable_device(dev
)) {
449 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
453 r
= pci_request_regions(dev
, "kvm_assigned_device");
455 printk(KERN_INFO
"%s: Could not get access to device regions\n",
460 pci_reset_function(dev
);
462 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
463 match
->host_busnr
= assigned_dev
->busnr
;
464 match
->host_devfn
= assigned_dev
->devfn
;
465 match
->flags
= assigned_dev
->flags
;
467 match
->irq_source_id
= -1;
470 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
472 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
) {
473 if (!kvm
->arch
.iommu_domain
) {
474 r
= kvm_iommu_map_guest(kvm
);
478 r
= kvm_assign_device(kvm
, match
);
484 mutex_unlock(&kvm
->lock
);
485 up_read(&kvm
->slots_lock
);
488 list_del(&match
->list
);
489 pci_release_regions(dev
);
491 pci_disable_device(dev
);
496 mutex_unlock(&kvm
->lock
);
497 up_read(&kvm
->slots_lock
);
502 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
503 static int kvm_vm_ioctl_deassign_device(struct kvm
*kvm
,
504 struct kvm_assigned_pci_dev
*assigned_dev
)
507 struct kvm_assigned_dev_kernel
*match
;
509 mutex_lock(&kvm
->lock
);
511 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
512 assigned_dev
->assigned_dev_id
);
514 printk(KERN_INFO
"%s: device hasn't been assigned before, "
515 "so cannot be deassigned\n", __func__
);
520 if (match
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
)
521 kvm_deassign_device(kvm
, match
);
523 kvm_free_assigned_device(kvm
, match
);
526 mutex_unlock(&kvm
->lock
);
531 static inline int valid_vcpu(int n
)
533 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
536 inline int kvm_is_mmio_pfn(pfn_t pfn
)
538 if (pfn_valid(pfn
)) {
539 struct page
*page
= compound_head(pfn_to_page(pfn
));
540 return PageReserved(page
);
547 * Switches to specified vcpu, until a matching vcpu_put()
549 void vcpu_load(struct kvm_vcpu
*vcpu
)
553 mutex_lock(&vcpu
->mutex
);
555 preempt_notifier_register(&vcpu
->preempt_notifier
);
556 kvm_arch_vcpu_load(vcpu
, cpu
);
560 void vcpu_put(struct kvm_vcpu
*vcpu
)
563 kvm_arch_vcpu_put(vcpu
);
564 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
566 mutex_unlock(&vcpu
->mutex
);
569 static void ack_flush(void *_completed
)
573 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
578 struct kvm_vcpu
*vcpu
;
580 if (alloc_cpumask_var(&cpus
, GFP_ATOMIC
))
584 spin_lock(&kvm
->requests_lock
);
585 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
586 vcpu
= kvm
->vcpus
[i
];
589 if (test_and_set_bit(req
, &vcpu
->requests
))
592 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
593 cpumask_set_cpu(cpu
, cpus
);
595 if (unlikely(cpus
== NULL
))
596 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
597 else if (!cpumask_empty(cpus
))
598 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
601 spin_unlock(&kvm
->requests_lock
);
603 free_cpumask_var(cpus
);
607 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
609 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
610 ++kvm
->stat
.remote_tlb_flush
;
613 void kvm_reload_remote_mmus(struct kvm
*kvm
)
615 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
618 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
623 mutex_init(&vcpu
->mutex
);
627 init_waitqueue_head(&vcpu
->wq
);
629 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
634 vcpu
->run
= page_address(page
);
636 r
= kvm_arch_vcpu_init(vcpu
);
642 free_page((unsigned long)vcpu
->run
);
646 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
648 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
650 kvm_arch_vcpu_uninit(vcpu
);
651 free_page((unsigned long)vcpu
->run
);
653 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
655 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
656 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
658 return container_of(mn
, struct kvm
, mmu_notifier
);
661 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
662 struct mm_struct
*mm
,
663 unsigned long address
)
665 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
669 * When ->invalidate_page runs, the linux pte has been zapped
670 * already but the page is still allocated until
671 * ->invalidate_page returns. So if we increase the sequence
672 * here the kvm page fault will notice if the spte can't be
673 * established because the page is going to be freed. If
674 * instead the kvm page fault establishes the spte before
675 * ->invalidate_page runs, kvm_unmap_hva will release it
678 * The sequence increase only need to be seen at spin_unlock
679 * time, and not at spin_lock time.
681 * Increasing the sequence after the spin_unlock would be
682 * unsafe because the kvm page fault could then establish the
683 * pte after kvm_unmap_hva returned, without noticing the page
684 * is going to be freed.
686 spin_lock(&kvm
->mmu_lock
);
687 kvm
->mmu_notifier_seq
++;
688 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
689 spin_unlock(&kvm
->mmu_lock
);
691 /* we've to flush the tlb before the pages can be freed */
693 kvm_flush_remote_tlbs(kvm
);
697 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
698 struct mm_struct
*mm
,
702 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
703 int need_tlb_flush
= 0;
705 spin_lock(&kvm
->mmu_lock
);
707 * The count increase must become visible at unlock time as no
708 * spte can be established without taking the mmu_lock and
709 * count is also read inside the mmu_lock critical section.
711 kvm
->mmu_notifier_count
++;
712 for (; start
< end
; start
+= PAGE_SIZE
)
713 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
714 spin_unlock(&kvm
->mmu_lock
);
716 /* we've to flush the tlb before the pages can be freed */
718 kvm_flush_remote_tlbs(kvm
);
721 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
722 struct mm_struct
*mm
,
726 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
728 spin_lock(&kvm
->mmu_lock
);
730 * This sequence increase will notify the kvm page fault that
731 * the page that is going to be mapped in the spte could have
734 kvm
->mmu_notifier_seq
++;
736 * The above sequence increase must be visible before the
737 * below count decrease but both values are read by the kvm
738 * page fault under mmu_lock spinlock so we don't need to add
739 * a smb_wmb() here in between the two.
741 kvm
->mmu_notifier_count
--;
742 spin_unlock(&kvm
->mmu_lock
);
744 BUG_ON(kvm
->mmu_notifier_count
< 0);
747 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
748 struct mm_struct
*mm
,
749 unsigned long address
)
751 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
754 spin_lock(&kvm
->mmu_lock
);
755 young
= kvm_age_hva(kvm
, address
);
756 spin_unlock(&kvm
->mmu_lock
);
759 kvm_flush_remote_tlbs(kvm
);
764 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
765 struct mm_struct
*mm
)
767 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
768 kvm_arch_flush_shadow(kvm
);
771 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
772 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
773 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
774 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
775 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
776 .release
= kvm_mmu_notifier_release
,
778 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
780 static struct kvm
*kvm_create_vm(void)
782 struct kvm
*kvm
= kvm_arch_create_vm();
783 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
789 #ifdef CONFIG_HAVE_KVM_IRQCHIP
790 INIT_LIST_HEAD(&kvm
->irq_routing
);
791 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
794 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
795 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
798 return ERR_PTR(-ENOMEM
);
800 kvm
->coalesced_mmio_ring
=
801 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
804 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
807 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
808 err
= mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
810 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
819 kvm
->mm
= current
->mm
;
820 atomic_inc(&kvm
->mm
->mm_count
);
821 spin_lock_init(&kvm
->mmu_lock
);
822 spin_lock_init(&kvm
->requests_lock
);
823 kvm_io_bus_init(&kvm
->pio_bus
);
824 mutex_init(&kvm
->lock
);
825 kvm_io_bus_init(&kvm
->mmio_bus
);
826 init_rwsem(&kvm
->slots_lock
);
827 atomic_set(&kvm
->users_count
, 1);
828 spin_lock(&kvm_lock
);
829 list_add(&kvm
->vm_list
, &vm_list
);
830 spin_unlock(&kvm_lock
);
831 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
832 kvm_coalesced_mmio_init(kvm
);
839 * Free any memory in @free but not in @dont.
841 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
842 struct kvm_memory_slot
*dont
)
844 if (!dont
|| free
->rmap
!= dont
->rmap
)
847 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
848 vfree(free
->dirty_bitmap
);
850 if (!dont
|| free
->lpage_info
!= dont
->lpage_info
)
851 vfree(free
->lpage_info
);
854 free
->dirty_bitmap
= NULL
;
856 free
->lpage_info
= NULL
;
859 void kvm_free_physmem(struct kvm
*kvm
)
863 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
864 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
867 static void kvm_destroy_vm(struct kvm
*kvm
)
869 struct mm_struct
*mm
= kvm
->mm
;
871 kvm_arch_sync_events(kvm
);
872 spin_lock(&kvm_lock
);
873 list_del(&kvm
->vm_list
);
874 spin_unlock(&kvm_lock
);
875 kvm_free_irq_routing(kvm
);
876 kvm_io_bus_destroy(&kvm
->pio_bus
);
877 kvm_io_bus_destroy(&kvm
->mmio_bus
);
878 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
879 if (kvm
->coalesced_mmio_ring
!= NULL
)
880 free_page((unsigned long)kvm
->coalesced_mmio_ring
);
882 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
883 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
885 kvm_arch_flush_shadow(kvm
);
887 kvm_arch_destroy_vm(kvm
);
891 void kvm_get_kvm(struct kvm
*kvm
)
893 atomic_inc(&kvm
->users_count
);
895 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
897 void kvm_put_kvm(struct kvm
*kvm
)
899 if (atomic_dec_and_test(&kvm
->users_count
))
902 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
905 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
907 struct kvm
*kvm
= filp
->private_data
;
914 * Allocate some memory and give it an address in the guest physical address
917 * Discontiguous memory is allowed, mostly for framebuffers.
919 * Must be called holding mmap_sem for write.
921 int __kvm_set_memory_region(struct kvm
*kvm
,
922 struct kvm_userspace_memory_region
*mem
,
927 unsigned long npages
, ugfn
;
928 unsigned long largepages
, i
;
929 struct kvm_memory_slot
*memslot
;
930 struct kvm_memory_slot old
, new;
933 /* General sanity checks */
934 if (mem
->memory_size
& (PAGE_SIZE
- 1))
936 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
938 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
940 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
942 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
945 memslot
= &kvm
->memslots
[mem
->slot
];
946 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
947 npages
= mem
->memory_size
>> PAGE_SHIFT
;
950 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
952 new = old
= *memslot
;
954 new.base_gfn
= base_gfn
;
956 new.flags
= mem
->flags
;
958 /* Disallow changing a memory slot's size. */
960 if (npages
&& old
.npages
&& npages
!= old
.npages
)
963 /* Check for overlaps */
965 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
966 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
968 if (s
== memslot
|| !s
->npages
)
970 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
971 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
975 /* Free page dirty bitmap if unneeded */
976 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
977 new.dirty_bitmap
= NULL
;
981 /* Allocate if a slot is being created */
983 if (npages
&& !new.rmap
) {
984 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
989 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
991 new.user_alloc
= user_alloc
;
993 * hva_to_rmmap() serialzies with the mmu_lock and to be
994 * safe it has to ignore memslots with !user_alloc &&
998 new.userspace_addr
= mem
->userspace_addr
;
1000 new.userspace_addr
= 0;
1002 if (npages
&& !new.lpage_info
) {
1003 largepages
= 1 + (base_gfn
+ npages
- 1) / KVM_PAGES_PER_HPAGE
;
1004 largepages
-= base_gfn
/ KVM_PAGES_PER_HPAGE
;
1006 new.lpage_info
= vmalloc(largepages
* sizeof(*new.lpage_info
));
1008 if (!new.lpage_info
)
1011 memset(new.lpage_info
, 0, largepages
* sizeof(*new.lpage_info
));
1013 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
1014 new.lpage_info
[0].write_count
= 1;
1015 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE
)
1016 new.lpage_info
[largepages
-1].write_count
= 1;
1017 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
1019 * If the gfn and userspace address are not aligned wrt each
1020 * other, disable large page support for this slot
1022 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE
- 1))
1023 for (i
= 0; i
< largepages
; ++i
)
1024 new.lpage_info
[i
].write_count
= 1;
1027 /* Allocate page dirty bitmap if needed */
1028 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
1029 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
1031 new.dirty_bitmap
= vmalloc(dirty_bytes
);
1032 if (!new.dirty_bitmap
)
1034 memset(new.dirty_bitmap
, 0, dirty_bytes
);
1036 kvm_arch_flush_shadow(kvm
);
1038 #endif /* not defined CONFIG_S390 */
1041 kvm_arch_flush_shadow(kvm
);
1043 spin_lock(&kvm
->mmu_lock
);
1044 if (mem
->slot
>= kvm
->nmemslots
)
1045 kvm
->nmemslots
= mem
->slot
+ 1;
1048 spin_unlock(&kvm
->mmu_lock
);
1050 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
1052 spin_lock(&kvm
->mmu_lock
);
1054 spin_unlock(&kvm
->mmu_lock
);
1058 kvm_free_physmem_slot(&old
, npages
? &new : NULL
);
1059 /* Slot deletion case: we have to update the current slot */
1060 spin_lock(&kvm
->mmu_lock
);
1063 spin_unlock(&kvm
->mmu_lock
);
1065 /* map the pages in iommu page table */
1066 r
= kvm_iommu_map_pages(kvm
, base_gfn
, npages
);
1073 kvm_free_physmem_slot(&new, &old
);
1078 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
1080 int kvm_set_memory_region(struct kvm
*kvm
,
1081 struct kvm_userspace_memory_region
*mem
,
1086 down_write(&kvm
->slots_lock
);
1087 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
1088 up_write(&kvm
->slots_lock
);
1091 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
1093 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
1095 kvm_userspace_memory_region
*mem
,
1098 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
1100 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
1103 int kvm_get_dirty_log(struct kvm
*kvm
,
1104 struct kvm_dirty_log
*log
, int *is_dirty
)
1106 struct kvm_memory_slot
*memslot
;
1109 unsigned long any
= 0;
1112 if (log
->slot
>= KVM_MEMORY_SLOTS
)
1115 memslot
= &kvm
->memslots
[log
->slot
];
1117 if (!memslot
->dirty_bitmap
)
1120 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1122 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
1123 any
= memslot
->dirty_bitmap
[i
];
1126 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
1137 int is_error_page(struct page
*page
)
1139 return page
== bad_page
;
1141 EXPORT_SYMBOL_GPL(is_error_page
);
1143 int is_error_pfn(pfn_t pfn
)
1145 return pfn
== bad_pfn
;
1147 EXPORT_SYMBOL_GPL(is_error_pfn
);
1149 static inline unsigned long bad_hva(void)
1154 int kvm_is_error_hva(unsigned long addr
)
1156 return addr
== bad_hva();
1158 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
1160 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
1164 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1165 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1167 if (gfn
>= memslot
->base_gfn
1168 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1173 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
1175 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1177 gfn
= unalias_gfn(kvm
, gfn
);
1178 return gfn_to_memslot_unaliased(kvm
, gfn
);
1181 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
1185 gfn
= unalias_gfn(kvm
, gfn
);
1186 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1187 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1189 if (gfn
>= memslot
->base_gfn
1190 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1195 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
1197 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1199 struct kvm_memory_slot
*slot
;
1201 gfn
= unalias_gfn(kvm
, gfn
);
1202 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1205 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
1207 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1209 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1211 struct page
*page
[1];
1218 addr
= gfn_to_hva(kvm
, gfn
);
1219 if (kvm_is_error_hva(addr
)) {
1221 return page_to_pfn(bad_page
);
1224 npages
= get_user_pages_fast(addr
, 1, 1, page
);
1226 if (unlikely(npages
!= 1)) {
1227 struct vm_area_struct
*vma
;
1229 down_read(¤t
->mm
->mmap_sem
);
1230 vma
= find_vma(current
->mm
, addr
);
1232 if (vma
== NULL
|| addr
< vma
->vm_start
||
1233 !(vma
->vm_flags
& VM_PFNMAP
)) {
1234 up_read(¤t
->mm
->mmap_sem
);
1236 return page_to_pfn(bad_page
);
1239 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1240 up_read(¤t
->mm
->mmap_sem
);
1241 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1243 pfn
= page_to_pfn(page
[0]);
1248 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1250 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1254 pfn
= gfn_to_pfn(kvm
, gfn
);
1255 if (!kvm_is_mmio_pfn(pfn
))
1256 return pfn_to_page(pfn
);
1258 WARN_ON(kvm_is_mmio_pfn(pfn
));
1264 EXPORT_SYMBOL_GPL(gfn_to_page
);
1266 void kvm_release_page_clean(struct page
*page
)
1268 kvm_release_pfn_clean(page_to_pfn(page
));
1270 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1272 void kvm_release_pfn_clean(pfn_t pfn
)
1274 if (!kvm_is_mmio_pfn(pfn
))
1275 put_page(pfn_to_page(pfn
));
1277 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1279 void kvm_release_page_dirty(struct page
*page
)
1281 kvm_release_pfn_dirty(page_to_pfn(page
));
1283 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1285 void kvm_release_pfn_dirty(pfn_t pfn
)
1287 kvm_set_pfn_dirty(pfn
);
1288 kvm_release_pfn_clean(pfn
);
1290 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1292 void kvm_set_page_dirty(struct page
*page
)
1294 kvm_set_pfn_dirty(page_to_pfn(page
));
1296 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1298 void kvm_set_pfn_dirty(pfn_t pfn
)
1300 if (!kvm_is_mmio_pfn(pfn
)) {
1301 struct page
*page
= pfn_to_page(pfn
);
1302 if (!PageReserved(page
))
1306 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1308 void kvm_set_pfn_accessed(pfn_t pfn
)
1310 if (!kvm_is_mmio_pfn(pfn
))
1311 mark_page_accessed(pfn_to_page(pfn
));
1313 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1315 void kvm_get_pfn(pfn_t pfn
)
1317 if (!kvm_is_mmio_pfn(pfn
))
1318 get_page(pfn_to_page(pfn
));
1320 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1322 static int next_segment(unsigned long len
, int offset
)
1324 if (len
> PAGE_SIZE
- offset
)
1325 return PAGE_SIZE
- offset
;
1330 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1336 addr
= gfn_to_hva(kvm
, gfn
);
1337 if (kvm_is_error_hva(addr
))
1339 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1344 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1346 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1348 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1350 int offset
= offset_in_page(gpa
);
1353 while ((seg
= next_segment(len
, offset
)) != 0) {
1354 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1364 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1366 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1371 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1372 int offset
= offset_in_page(gpa
);
1374 addr
= gfn_to_hva(kvm
, gfn
);
1375 if (kvm_is_error_hva(addr
))
1377 pagefault_disable();
1378 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1384 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1386 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1387 int offset
, int len
)
1392 addr
= gfn_to_hva(kvm
, gfn
);
1393 if (kvm_is_error_hva(addr
))
1395 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1398 mark_page_dirty(kvm
, gfn
);
1401 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1403 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1406 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1408 int offset
= offset_in_page(gpa
);
1411 while ((seg
= next_segment(len
, offset
)) != 0) {
1412 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1423 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1425 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1427 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1429 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1431 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1433 int offset
= offset_in_page(gpa
);
1436 while ((seg
= next_segment(len
, offset
)) != 0) {
1437 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1446 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1448 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1450 struct kvm_memory_slot
*memslot
;
1452 gfn
= unalias_gfn(kvm
, gfn
);
1453 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1454 if (memslot
&& memslot
->dirty_bitmap
) {
1455 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1458 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1459 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1464 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1466 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1471 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1473 if (kvm_cpu_has_interrupt(vcpu
) ||
1474 kvm_cpu_has_pending_timer(vcpu
) ||
1475 kvm_arch_vcpu_runnable(vcpu
)) {
1476 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1479 if (signal_pending(current
))
1487 finish_wait(&vcpu
->wq
, &wait
);
1490 void kvm_resched(struct kvm_vcpu
*vcpu
)
1492 if (!need_resched())
1496 EXPORT_SYMBOL_GPL(kvm_resched
);
1498 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1500 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1503 if (vmf
->pgoff
== 0)
1504 page
= virt_to_page(vcpu
->run
);
1506 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1507 page
= virt_to_page(vcpu
->arch
.pio_data
);
1509 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1510 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1511 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1514 return VM_FAULT_SIGBUS
;
1520 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
1521 .fault
= kvm_vcpu_fault
,
1524 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1526 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1530 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1532 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1534 kvm_put_kvm(vcpu
->kvm
);
1538 static struct file_operations kvm_vcpu_fops
= {
1539 .release
= kvm_vcpu_release
,
1540 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1541 .compat_ioctl
= kvm_vcpu_ioctl
,
1542 .mmap
= kvm_vcpu_mmap
,
1546 * Allocates an inode for the vcpu.
1548 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1550 int fd
= anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, 0);
1552 kvm_put_kvm(vcpu
->kvm
);
1557 * Creates some virtual cpus. Good luck creating more than one.
1559 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
1562 struct kvm_vcpu
*vcpu
;
1567 vcpu
= kvm_arch_vcpu_create(kvm
, n
);
1569 return PTR_ERR(vcpu
);
1571 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1573 r
= kvm_arch_vcpu_setup(vcpu
);
1577 mutex_lock(&kvm
->lock
);
1578 if (kvm
->vcpus
[n
]) {
1582 kvm
->vcpus
[n
] = vcpu
;
1583 mutex_unlock(&kvm
->lock
);
1585 /* Now it's all set up, let userspace reach it */
1587 r
= create_vcpu_fd(vcpu
);
1593 mutex_lock(&kvm
->lock
);
1594 kvm
->vcpus
[n
] = NULL
;
1596 mutex_unlock(&kvm
->lock
);
1597 kvm_arch_vcpu_destroy(vcpu
);
1601 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1604 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1605 vcpu
->sigset_active
= 1;
1606 vcpu
->sigset
= *sigset
;
1608 vcpu
->sigset_active
= 0;
1612 static long kvm_vcpu_ioctl(struct file
*filp
,
1613 unsigned int ioctl
, unsigned long arg
)
1615 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1616 void __user
*argp
= (void __user
*)arg
;
1618 struct kvm_fpu
*fpu
= NULL
;
1619 struct kvm_sregs
*kvm_sregs
= NULL
;
1621 if (vcpu
->kvm
->mm
!= current
->mm
)
1628 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1630 case KVM_GET_REGS
: {
1631 struct kvm_regs
*kvm_regs
;
1634 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1637 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1641 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1648 case KVM_SET_REGS
: {
1649 struct kvm_regs
*kvm_regs
;
1652 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1656 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1658 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1666 case KVM_GET_SREGS
: {
1667 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1671 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1675 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1680 case KVM_SET_SREGS
: {
1681 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1686 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1688 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1694 case KVM_GET_MP_STATE
: {
1695 struct kvm_mp_state mp_state
;
1697 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1701 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1706 case KVM_SET_MP_STATE
: {
1707 struct kvm_mp_state mp_state
;
1710 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1712 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1718 case KVM_TRANSLATE
: {
1719 struct kvm_translation tr
;
1722 if (copy_from_user(&tr
, argp
, sizeof tr
))
1724 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1728 if (copy_to_user(argp
, &tr
, sizeof tr
))
1733 case KVM_SET_GUEST_DEBUG
: {
1734 struct kvm_guest_debug dbg
;
1737 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1739 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1745 case KVM_SET_SIGNAL_MASK
: {
1746 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1747 struct kvm_signal_mask kvm_sigmask
;
1748 sigset_t sigset
, *p
;
1753 if (copy_from_user(&kvm_sigmask
, argp
,
1754 sizeof kvm_sigmask
))
1757 if (kvm_sigmask
.len
!= sizeof sigset
)
1760 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1765 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1769 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1773 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1777 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1783 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1788 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1790 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1797 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1805 static long kvm_vm_ioctl(struct file
*filp
,
1806 unsigned int ioctl
, unsigned long arg
)
1808 struct kvm
*kvm
= filp
->private_data
;
1809 void __user
*argp
= (void __user
*)arg
;
1812 if (kvm
->mm
!= current
->mm
)
1815 case KVM_CREATE_VCPU
:
1816 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1820 case KVM_SET_USER_MEMORY_REGION
: {
1821 struct kvm_userspace_memory_region kvm_userspace_mem
;
1824 if (copy_from_user(&kvm_userspace_mem
, argp
,
1825 sizeof kvm_userspace_mem
))
1828 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1833 case KVM_GET_DIRTY_LOG
: {
1834 struct kvm_dirty_log log
;
1837 if (copy_from_user(&log
, argp
, sizeof log
))
1839 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1844 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1845 case KVM_REGISTER_COALESCED_MMIO
: {
1846 struct kvm_coalesced_mmio_zone zone
;
1848 if (copy_from_user(&zone
, argp
, sizeof zone
))
1851 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1857 case KVM_UNREGISTER_COALESCED_MMIO
: {
1858 struct kvm_coalesced_mmio_zone zone
;
1860 if (copy_from_user(&zone
, argp
, sizeof zone
))
1863 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1870 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1871 case KVM_ASSIGN_PCI_DEVICE
: {
1872 struct kvm_assigned_pci_dev assigned_dev
;
1875 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1877 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
1882 case KVM_ASSIGN_IRQ
: {
1883 struct kvm_assigned_irq assigned_irq
;
1886 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
1888 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
1894 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1895 case KVM_DEASSIGN_PCI_DEVICE
: {
1896 struct kvm_assigned_pci_dev assigned_dev
;
1899 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1901 r
= kvm_vm_ioctl_deassign_device(kvm
, &assigned_dev
);
1907 #ifdef KVM_CAP_IRQ_ROUTING
1908 case KVM_SET_GSI_ROUTING
: {
1909 struct kvm_irq_routing routing
;
1910 struct kvm_irq_routing __user
*urouting
;
1911 struct kvm_irq_routing_entry
*entries
;
1914 if (copy_from_user(&routing
, argp
, sizeof(routing
)))
1917 if (routing
.nr
>= KVM_MAX_IRQ_ROUTES
)
1922 entries
= vmalloc(routing
.nr
* sizeof(*entries
));
1927 if (copy_from_user(entries
, urouting
->entries
,
1928 routing
.nr
* sizeof(*entries
)))
1929 goto out_free_irq_routing
;
1930 r
= kvm_set_irq_routing(kvm
, entries
, routing
.nr
,
1932 out_free_irq_routing
:
1938 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1944 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1946 struct page
*page
[1];
1949 gfn_t gfn
= vmf
->pgoff
;
1950 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1952 addr
= gfn_to_hva(kvm
, gfn
);
1953 if (kvm_is_error_hva(addr
))
1954 return VM_FAULT_SIGBUS
;
1956 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1958 if (unlikely(npages
!= 1))
1959 return VM_FAULT_SIGBUS
;
1961 vmf
->page
= page
[0];
1965 static struct vm_operations_struct kvm_vm_vm_ops
= {
1966 .fault
= kvm_vm_fault
,
1969 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1971 vma
->vm_ops
= &kvm_vm_vm_ops
;
1975 static struct file_operations kvm_vm_fops
= {
1976 .release
= kvm_vm_release
,
1977 .unlocked_ioctl
= kvm_vm_ioctl
,
1978 .compat_ioctl
= kvm_vm_ioctl
,
1979 .mmap
= kvm_vm_mmap
,
1982 static int kvm_dev_ioctl_create_vm(void)
1987 kvm
= kvm_create_vm();
1989 return PTR_ERR(kvm
);
1990 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, 0);
1997 static long kvm_dev_ioctl_check_extension_generic(long arg
)
2000 case KVM_CAP_USER_MEMORY
:
2001 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
2002 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
2004 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2005 case KVM_CAP_IRQ_ROUTING
:
2006 return KVM_MAX_IRQ_ROUTES
;
2011 return kvm_dev_ioctl_check_extension(arg
);
2014 static long kvm_dev_ioctl(struct file
*filp
,
2015 unsigned int ioctl
, unsigned long arg
)
2020 case KVM_GET_API_VERSION
:
2024 r
= KVM_API_VERSION
;
2030 r
= kvm_dev_ioctl_create_vm();
2032 case KVM_CHECK_EXTENSION
:
2033 r
= kvm_dev_ioctl_check_extension_generic(arg
);
2035 case KVM_GET_VCPU_MMAP_SIZE
:
2039 r
= PAGE_SIZE
; /* struct kvm_run */
2041 r
+= PAGE_SIZE
; /* pio data page */
2043 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2044 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
2047 case KVM_TRACE_ENABLE
:
2048 case KVM_TRACE_PAUSE
:
2049 case KVM_TRACE_DISABLE
:
2050 r
= kvm_trace_ioctl(ioctl
, arg
);
2053 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
2059 static struct file_operations kvm_chardev_ops
= {
2060 .unlocked_ioctl
= kvm_dev_ioctl
,
2061 .compat_ioctl
= kvm_dev_ioctl
,
2064 static struct miscdevice kvm_dev
= {
2070 static void hardware_enable(void *junk
)
2072 int cpu
= raw_smp_processor_id();
2074 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2076 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
2077 kvm_arch_hardware_enable(NULL
);
2080 static void hardware_disable(void *junk
)
2082 int cpu
= raw_smp_processor_id();
2084 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2086 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2087 kvm_arch_hardware_disable(NULL
);
2090 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2095 val
&= ~CPU_TASKS_FROZEN
;
2098 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2100 hardware_disable(NULL
);
2102 case CPU_UP_CANCELED
:
2103 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2105 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
2108 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2110 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
2117 asmlinkage
void kvm_handle_fault_on_reboot(void)
2120 /* spin while reset goes on */
2123 /* Fault while not rebooting. We want the trace. */
2126 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
2128 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2131 if (val
== SYS_RESTART
) {
2133 * Some (well, at least mine) BIOSes hang on reboot if
2136 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2137 kvm_rebooting
= true;
2138 on_each_cpu(hardware_disable
, NULL
, 1);
2143 static struct notifier_block kvm_reboot_notifier
= {
2144 .notifier_call
= kvm_reboot
,
2148 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
2150 memset(bus
, 0, sizeof(*bus
));
2153 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2157 for (i
= 0; i
< bus
->dev_count
; i
++) {
2158 struct kvm_io_device
*pos
= bus
->devs
[i
];
2160 kvm_iodevice_destructor(pos
);
2164 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
,
2165 gpa_t addr
, int len
, int is_write
)
2169 for (i
= 0; i
< bus
->dev_count
; i
++) {
2170 struct kvm_io_device
*pos
= bus
->devs
[i
];
2172 if (pos
->in_range(pos
, addr
, len
, is_write
))
2179 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
2181 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
2183 bus
->devs
[bus
->dev_count
++] = dev
;
2186 static struct notifier_block kvm_cpu_notifier
= {
2187 .notifier_call
= kvm_cpu_hotplug
,
2188 .priority
= 20, /* must be > scheduler priority */
2191 static int vm_stat_get(void *_offset
, u64
*val
)
2193 unsigned offset
= (long)_offset
;
2197 spin_lock(&kvm_lock
);
2198 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2199 *val
+= *(u32
*)((void *)kvm
+ offset
);
2200 spin_unlock(&kvm_lock
);
2204 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2206 static int vcpu_stat_get(void *_offset
, u64
*val
)
2208 unsigned offset
= (long)_offset
;
2210 struct kvm_vcpu
*vcpu
;
2214 spin_lock(&kvm_lock
);
2215 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2216 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2217 vcpu
= kvm
->vcpus
[i
];
2219 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2221 spin_unlock(&kvm_lock
);
2225 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2227 static struct file_operations
*stat_fops
[] = {
2228 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2229 [KVM_STAT_VM
] = &vm_stat_fops
,
2232 static void kvm_init_debug(void)
2234 struct kvm_stats_debugfs_item
*p
;
2236 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2237 for (p
= debugfs_entries
; p
->name
; ++p
)
2238 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2239 (void *)(long)p
->offset
,
2240 stat_fops
[p
->kind
]);
2243 static void kvm_exit_debug(void)
2245 struct kvm_stats_debugfs_item
*p
;
2247 for (p
= debugfs_entries
; p
->name
; ++p
)
2248 debugfs_remove(p
->dentry
);
2249 debugfs_remove(kvm_debugfs_dir
);
2252 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2254 hardware_disable(NULL
);
2258 static int kvm_resume(struct sys_device
*dev
)
2260 hardware_enable(NULL
);
2264 static struct sysdev_class kvm_sysdev_class
= {
2266 .suspend
= kvm_suspend
,
2267 .resume
= kvm_resume
,
2270 static struct sys_device kvm_sysdev
= {
2272 .cls
= &kvm_sysdev_class
,
2275 struct page
*bad_page
;
2279 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2281 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2284 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2286 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2288 kvm_arch_vcpu_load(vcpu
, cpu
);
2291 static void kvm_sched_out(struct preempt_notifier
*pn
,
2292 struct task_struct
*next
)
2294 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2296 kvm_arch_vcpu_put(vcpu
);
2299 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2300 struct module
*module
)
2307 r
= kvm_arch_init(opaque
);
2311 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2313 if (bad_page
== NULL
) {
2318 bad_pfn
= page_to_pfn(bad_page
);
2320 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2324 cpumask_clear(cpus_hardware_enabled
);
2326 r
= kvm_arch_hardware_setup();
2330 for_each_online_cpu(cpu
) {
2331 smp_call_function_single(cpu
,
2332 kvm_arch_check_processor_compat
,
2338 on_each_cpu(hardware_enable
, NULL
, 1);
2339 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2342 register_reboot_notifier(&kvm_reboot_notifier
);
2344 r
= sysdev_class_register(&kvm_sysdev_class
);
2348 r
= sysdev_register(&kvm_sysdev
);
2352 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2353 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2354 __alignof__(struct kvm_vcpu
),
2356 if (!kvm_vcpu_cache
) {
2361 kvm_chardev_ops
.owner
= module
;
2362 kvm_vm_fops
.owner
= module
;
2363 kvm_vcpu_fops
.owner
= module
;
2365 r
= misc_register(&kvm_dev
);
2367 printk(KERN_ERR
"kvm: misc device register failed\n");
2371 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2372 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2380 kmem_cache_destroy(kvm_vcpu_cache
);
2382 sysdev_unregister(&kvm_sysdev
);
2384 sysdev_class_unregister(&kvm_sysdev_class
);
2386 unregister_reboot_notifier(&kvm_reboot_notifier
);
2387 unregister_cpu_notifier(&kvm_cpu_notifier
);
2389 on_each_cpu(hardware_disable
, NULL
, 1);
2391 kvm_arch_hardware_unsetup();
2393 free_cpumask_var(cpus_hardware_enabled
);
2395 __free_page(bad_page
);
2402 EXPORT_SYMBOL_GPL(kvm_init
);
2406 kvm_trace_cleanup();
2407 misc_deregister(&kvm_dev
);
2408 kmem_cache_destroy(kvm_vcpu_cache
);
2409 sysdev_unregister(&kvm_sysdev
);
2410 sysdev_class_unregister(&kvm_sysdev_class
);
2411 unregister_reboot_notifier(&kvm_reboot_notifier
);
2412 unregister_cpu_notifier(&kvm_cpu_notifier
);
2413 on_each_cpu(hardware_disable
, NULL
, 1);
2414 kvm_arch_hardware_unsetup();
2417 free_cpumask_var(cpus_hardware_enabled
);
2418 __free_page(bad_page
);
2420 EXPORT_SYMBOL_GPL(kvm_exit
);