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>
44 #include <linux/bitops.h>
45 #include <linux/spinlock.h>
47 #include <asm/processor.h>
49 #include <asm/uaccess.h>
50 #include <asm/pgtable.h>
52 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
53 #include "coalesced_mmio.h"
56 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
57 #include <linux/pci.h>
58 #include <linux/interrupt.h>
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/kvm.h>
65 MODULE_AUTHOR("Qumranet");
66 MODULE_LICENSE("GPL");
71 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
74 DEFINE_SPINLOCK(kvm_lock
);
77 static cpumask_var_t cpus_hardware_enabled
;
79 struct kmem_cache
*kvm_vcpu_cache
;
80 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
82 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
84 struct dentry
*kvm_debugfs_dir
;
86 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
89 static bool kvm_rebooting
;
91 static bool largepages_enabled
= true;
93 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
94 static struct kvm_assigned_dev_kernel
*kvm_find_assigned_dev(struct list_head
*head
,
97 struct list_head
*ptr
;
98 struct kvm_assigned_dev_kernel
*match
;
100 list_for_each(ptr
, head
) {
101 match
= list_entry(ptr
, struct kvm_assigned_dev_kernel
, list
);
102 if (match
->assigned_dev_id
== assigned_dev_id
)
108 static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
109 *assigned_dev
, int irq
)
112 struct msix_entry
*host_msix_entries
;
114 host_msix_entries
= assigned_dev
->host_msix_entries
;
117 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
118 if (irq
== host_msix_entries
[i
].vector
) {
123 printk(KERN_WARNING
"Fail to find correlated MSI-X entry!\n");
130 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct
*work
)
132 struct kvm_assigned_dev_kernel
*assigned_dev
;
136 assigned_dev
= container_of(work
, struct kvm_assigned_dev_kernel
,
138 kvm
= assigned_dev
->kvm
;
140 mutex_lock(&kvm
->irq_lock
);
141 spin_lock_irq(&assigned_dev
->assigned_dev_lock
);
142 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSIX
) {
143 struct kvm_guest_msix_entry
*guest_entries
=
144 assigned_dev
->guest_msix_entries
;
145 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++) {
146 if (!(guest_entries
[i
].flags
&
147 KVM_ASSIGNED_MSIX_PENDING
))
149 guest_entries
[i
].flags
&= ~KVM_ASSIGNED_MSIX_PENDING
;
150 kvm_set_irq(assigned_dev
->kvm
,
151 assigned_dev
->irq_source_id
,
152 guest_entries
[i
].vector
, 1);
155 kvm_set_irq(assigned_dev
->kvm
, assigned_dev
->irq_source_id
,
156 assigned_dev
->guest_irq
, 1);
158 spin_unlock_irq(&assigned_dev
->assigned_dev_lock
);
159 mutex_unlock(&assigned_dev
->kvm
->irq_lock
);
162 static irqreturn_t
kvm_assigned_dev_intr(int irq
, void *dev_id
)
165 struct kvm_assigned_dev_kernel
*assigned_dev
=
166 (struct kvm_assigned_dev_kernel
*) dev_id
;
168 spin_lock_irqsave(&assigned_dev
->assigned_dev_lock
, flags
);
169 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_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 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_GUEST_INTX
) {
180 disable_irq_nosync(irq
);
181 assigned_dev
->host_irq_disabled
= true;
185 spin_unlock_irqrestore(&assigned_dev
->assigned_dev_lock
, flags
);
189 /* Ack the irq line for an assigned device */
190 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
192 struct kvm_assigned_dev_kernel
*dev
;
198 dev
= container_of(kian
, struct kvm_assigned_dev_kernel
,
201 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
203 /* The guest irq may be shared so this ack may be
204 * from another device.
206 spin_lock_irqsave(&dev
->assigned_dev_lock
, flags
);
207 if (dev
->host_irq_disabled
) {
208 enable_irq(dev
->host_irq
);
209 dev
->host_irq_disabled
= false;
211 spin_unlock_irqrestore(&dev
->assigned_dev_lock
, flags
);
214 static void deassign_guest_irq(struct kvm
*kvm
,
215 struct kvm_assigned_dev_kernel
*assigned_dev
)
217 kvm_unregister_irq_ack_notifier(kvm
, &assigned_dev
->ack_notifier
);
218 assigned_dev
->ack_notifier
.gsi
= -1;
220 if (assigned_dev
->irq_source_id
!= -1)
221 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
222 assigned_dev
->irq_source_id
= -1;
223 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_GUEST_MASK
);
226 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
227 static void deassign_host_irq(struct kvm
*kvm
,
228 struct kvm_assigned_dev_kernel
*assigned_dev
)
231 * In kvm_free_device_irq, cancel_work_sync return true if:
232 * 1. work is scheduled, and then cancelled.
233 * 2. work callback is executed.
235 * The first one ensured that the irq is disabled and no more events
236 * would happen. But for the second one, the irq may be enabled (e.g.
237 * for MSI). So we disable irq here to prevent further events.
239 * Notice this maybe result in nested disable if the interrupt type is
240 * INTx, but it's OK for we are going to free it.
242 * If this function is a part of VM destroy, please ensure that till
243 * now, the kvm state is still legal for probably we also have to wait
244 * interrupt_work done.
246 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSIX
) {
248 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
249 disable_irq_nosync(assigned_dev
->
250 host_msix_entries
[i
].vector
);
252 cancel_work_sync(&assigned_dev
->interrupt_work
);
254 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
255 free_irq(assigned_dev
->host_msix_entries
[i
].vector
,
256 (void *)assigned_dev
);
258 assigned_dev
->entries_nr
= 0;
259 kfree(assigned_dev
->host_msix_entries
);
260 kfree(assigned_dev
->guest_msix_entries
);
261 pci_disable_msix(assigned_dev
->dev
);
263 /* Deal with MSI and INTx */
264 disable_irq_nosync(assigned_dev
->host_irq
);
265 cancel_work_sync(&assigned_dev
->interrupt_work
);
267 free_irq(assigned_dev
->host_irq
, (void *)assigned_dev
);
269 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSI
)
270 pci_disable_msi(assigned_dev
->dev
);
273 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_HOST_MASK
);
276 static int kvm_deassign_irq(struct kvm
*kvm
,
277 struct kvm_assigned_dev_kernel
*assigned_dev
,
278 unsigned long irq_requested_type
)
280 unsigned long guest_irq_type
, host_irq_type
;
282 if (!irqchip_in_kernel(kvm
))
284 /* no irq assignment to deassign */
285 if (!assigned_dev
->irq_requested_type
)
288 host_irq_type
= irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
;
289 guest_irq_type
= irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
;
292 deassign_host_irq(kvm
, assigned_dev
);
294 deassign_guest_irq(kvm
, assigned_dev
);
299 static void kvm_free_assigned_irq(struct kvm
*kvm
,
300 struct kvm_assigned_dev_kernel
*assigned_dev
)
302 kvm_deassign_irq(kvm
, assigned_dev
, assigned_dev
->irq_requested_type
);
305 static void kvm_free_assigned_device(struct kvm
*kvm
,
306 struct kvm_assigned_dev_kernel
309 kvm_free_assigned_irq(kvm
, assigned_dev
);
311 pci_reset_function(assigned_dev
->dev
);
313 pci_release_regions(assigned_dev
->dev
);
314 pci_disable_device(assigned_dev
->dev
);
315 pci_dev_put(assigned_dev
->dev
);
317 list_del(&assigned_dev
->list
);
321 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
323 struct list_head
*ptr
, *ptr2
;
324 struct kvm_assigned_dev_kernel
*assigned_dev
;
326 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
327 assigned_dev
= list_entry(ptr
,
328 struct kvm_assigned_dev_kernel
,
331 kvm_free_assigned_device(kvm
, assigned_dev
);
335 static int assigned_device_enable_host_intx(struct kvm
*kvm
,
336 struct kvm_assigned_dev_kernel
*dev
)
338 dev
->host_irq
= dev
->dev
->irq
;
339 /* Even though this is PCI, we don't want to use shared
340 * interrupts. Sharing host devices with guest-assigned devices
341 * on the same interrupt line is not a happy situation: there
342 * are going to be long delays in accepting, acking, etc.
344 if (request_irq(dev
->host_irq
, kvm_assigned_dev_intr
,
345 0, "kvm_assigned_intx_device", (void *)dev
))
350 #ifdef __KVM_HAVE_MSI
351 static int assigned_device_enable_host_msi(struct kvm
*kvm
,
352 struct kvm_assigned_dev_kernel
*dev
)
356 if (!dev
->dev
->msi_enabled
) {
357 r
= pci_enable_msi(dev
->dev
);
362 dev
->host_irq
= dev
->dev
->irq
;
363 if (request_irq(dev
->host_irq
, kvm_assigned_dev_intr
, 0,
364 "kvm_assigned_msi_device", (void *)dev
)) {
365 pci_disable_msi(dev
->dev
);
373 #ifdef __KVM_HAVE_MSIX
374 static int assigned_device_enable_host_msix(struct kvm
*kvm
,
375 struct kvm_assigned_dev_kernel
*dev
)
379 /* host_msix_entries and guest_msix_entries should have been
381 if (dev
->entries_nr
== 0)
384 r
= pci_enable_msix(dev
->dev
, dev
->host_msix_entries
, dev
->entries_nr
);
388 for (i
= 0; i
< dev
->entries_nr
; i
++) {
389 r
= request_irq(dev
->host_msix_entries
[i
].vector
,
390 kvm_assigned_dev_intr
, 0,
391 "kvm_assigned_msix_device",
393 /* FIXME: free requested_irq's on failure */
403 static int assigned_device_enable_guest_intx(struct kvm
*kvm
,
404 struct kvm_assigned_dev_kernel
*dev
,
405 struct kvm_assigned_irq
*irq
)
407 dev
->guest_irq
= irq
->guest_irq
;
408 dev
->ack_notifier
.gsi
= irq
->guest_irq
;
412 #ifdef __KVM_HAVE_MSI
413 static int assigned_device_enable_guest_msi(struct kvm
*kvm
,
414 struct kvm_assigned_dev_kernel
*dev
,
415 struct kvm_assigned_irq
*irq
)
417 dev
->guest_irq
= irq
->guest_irq
;
418 dev
->ack_notifier
.gsi
= -1;
419 dev
->host_irq_disabled
= false;
423 #ifdef __KVM_HAVE_MSIX
424 static int assigned_device_enable_guest_msix(struct kvm
*kvm
,
425 struct kvm_assigned_dev_kernel
*dev
,
426 struct kvm_assigned_irq
*irq
)
428 dev
->guest_irq
= irq
->guest_irq
;
429 dev
->ack_notifier
.gsi
= -1;
430 dev
->host_irq_disabled
= false;
435 static int assign_host_irq(struct kvm
*kvm
,
436 struct kvm_assigned_dev_kernel
*dev
,
441 if (dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
)
444 switch (host_irq_type
) {
445 case KVM_DEV_IRQ_HOST_INTX
:
446 r
= assigned_device_enable_host_intx(kvm
, dev
);
448 #ifdef __KVM_HAVE_MSI
449 case KVM_DEV_IRQ_HOST_MSI
:
450 r
= assigned_device_enable_host_msi(kvm
, dev
);
453 #ifdef __KVM_HAVE_MSIX
454 case KVM_DEV_IRQ_HOST_MSIX
:
455 r
= assigned_device_enable_host_msix(kvm
, dev
);
463 dev
->irq_requested_type
|= host_irq_type
;
468 static int assign_guest_irq(struct kvm
*kvm
,
469 struct kvm_assigned_dev_kernel
*dev
,
470 struct kvm_assigned_irq
*irq
,
471 unsigned long guest_irq_type
)
476 if (dev
->irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
)
479 id
= kvm_request_irq_source_id(kvm
);
483 dev
->irq_source_id
= id
;
485 switch (guest_irq_type
) {
486 case KVM_DEV_IRQ_GUEST_INTX
:
487 r
= assigned_device_enable_guest_intx(kvm
, dev
, irq
);
489 #ifdef __KVM_HAVE_MSI
490 case KVM_DEV_IRQ_GUEST_MSI
:
491 r
= assigned_device_enable_guest_msi(kvm
, dev
, irq
);
494 #ifdef __KVM_HAVE_MSIX
495 case KVM_DEV_IRQ_GUEST_MSIX
:
496 r
= assigned_device_enable_guest_msix(kvm
, dev
, irq
);
504 dev
->irq_requested_type
|= guest_irq_type
;
505 kvm_register_irq_ack_notifier(kvm
, &dev
->ack_notifier
);
507 kvm_free_irq_source_id(kvm
, dev
->irq_source_id
);
512 /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
513 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
514 struct kvm_assigned_irq
*assigned_irq
)
517 struct kvm_assigned_dev_kernel
*match
;
518 unsigned long host_irq_type
, guest_irq_type
;
520 if (!capable(CAP_SYS_RAWIO
))
523 if (!irqchip_in_kernel(kvm
))
526 mutex_lock(&kvm
->lock
);
528 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
529 assigned_irq
->assigned_dev_id
);
533 host_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_HOST_MASK
);
534 guest_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_GUEST_MASK
);
537 /* can only assign one type at a time */
538 if (hweight_long(host_irq_type
) > 1)
540 if (hweight_long(guest_irq_type
) > 1)
542 if (host_irq_type
== 0 && guest_irq_type
== 0)
547 r
= assign_host_irq(kvm
, match
, host_irq_type
);
552 r
= assign_guest_irq(kvm
, match
, assigned_irq
, guest_irq_type
);
554 mutex_unlock(&kvm
->lock
);
558 static int kvm_vm_ioctl_deassign_dev_irq(struct kvm
*kvm
,
559 struct kvm_assigned_irq
563 struct kvm_assigned_dev_kernel
*match
;
565 mutex_lock(&kvm
->lock
);
567 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
568 assigned_irq
->assigned_dev_id
);
572 r
= kvm_deassign_irq(kvm
, match
, assigned_irq
->flags
);
574 mutex_unlock(&kvm
->lock
);
578 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
579 struct kvm_assigned_pci_dev
*assigned_dev
)
582 struct kvm_assigned_dev_kernel
*match
;
585 down_read(&kvm
->slots_lock
);
586 mutex_lock(&kvm
->lock
);
588 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
589 assigned_dev
->assigned_dev_id
);
591 /* device already assigned */
596 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
598 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
603 dev
= pci_get_bus_and_slot(assigned_dev
->busnr
,
604 assigned_dev
->devfn
);
606 printk(KERN_INFO
"%s: host device not found\n", __func__
);
610 if (pci_enable_device(dev
)) {
611 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
615 r
= pci_request_regions(dev
, "kvm_assigned_device");
617 printk(KERN_INFO
"%s: Could not get access to device regions\n",
622 pci_reset_function(dev
);
624 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
625 match
->host_busnr
= assigned_dev
->busnr
;
626 match
->host_devfn
= assigned_dev
->devfn
;
627 match
->flags
= assigned_dev
->flags
;
629 spin_lock_init(&match
->assigned_dev_lock
);
630 match
->irq_source_id
= -1;
632 match
->ack_notifier
.irq_acked
= kvm_assigned_dev_ack_irq
;
633 INIT_WORK(&match
->interrupt_work
,
634 kvm_assigned_dev_interrupt_work_handler
);
636 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
638 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
) {
639 if (!kvm
->arch
.iommu_domain
) {
640 r
= kvm_iommu_map_guest(kvm
);
644 r
= kvm_assign_device(kvm
, match
);
650 mutex_unlock(&kvm
->lock
);
651 up_read(&kvm
->slots_lock
);
654 list_del(&match
->list
);
655 pci_release_regions(dev
);
657 pci_disable_device(dev
);
662 mutex_unlock(&kvm
->lock
);
663 up_read(&kvm
->slots_lock
);
668 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
669 static int kvm_vm_ioctl_deassign_device(struct kvm
*kvm
,
670 struct kvm_assigned_pci_dev
*assigned_dev
)
673 struct kvm_assigned_dev_kernel
*match
;
675 mutex_lock(&kvm
->lock
);
677 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
678 assigned_dev
->assigned_dev_id
);
680 printk(KERN_INFO
"%s: device hasn't been assigned before, "
681 "so cannot be deassigned\n", __func__
);
686 if (match
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
)
687 kvm_deassign_device(kvm
, match
);
689 kvm_free_assigned_device(kvm
, match
);
692 mutex_unlock(&kvm
->lock
);
697 inline int kvm_is_mmio_pfn(pfn_t pfn
)
699 if (pfn_valid(pfn
)) {
700 struct page
*page
= compound_head(pfn_to_page(pfn
));
701 return PageReserved(page
);
708 * Switches to specified vcpu, until a matching vcpu_put()
710 void vcpu_load(struct kvm_vcpu
*vcpu
)
714 mutex_lock(&vcpu
->mutex
);
716 preempt_notifier_register(&vcpu
->preempt_notifier
);
717 kvm_arch_vcpu_load(vcpu
, cpu
);
721 void vcpu_put(struct kvm_vcpu
*vcpu
)
724 kvm_arch_vcpu_put(vcpu
);
725 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
727 mutex_unlock(&vcpu
->mutex
);
730 static void ack_flush(void *_completed
)
734 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
739 struct kvm_vcpu
*vcpu
;
741 zalloc_cpumask_var(&cpus
, GFP_ATOMIC
);
743 spin_lock(&kvm
->requests_lock
);
744 me
= smp_processor_id();
745 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
746 if (test_and_set_bit(req
, &vcpu
->requests
))
749 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
750 cpumask_set_cpu(cpu
, cpus
);
752 if (unlikely(cpus
== NULL
))
753 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
754 else if (!cpumask_empty(cpus
))
755 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
758 spin_unlock(&kvm
->requests_lock
);
759 free_cpumask_var(cpus
);
763 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
765 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
766 ++kvm
->stat
.remote_tlb_flush
;
769 void kvm_reload_remote_mmus(struct kvm
*kvm
)
771 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
774 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
779 mutex_init(&vcpu
->mutex
);
783 init_waitqueue_head(&vcpu
->wq
);
785 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
790 vcpu
->run
= page_address(page
);
792 r
= kvm_arch_vcpu_init(vcpu
);
798 free_page((unsigned long)vcpu
->run
);
802 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
804 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
806 kvm_arch_vcpu_uninit(vcpu
);
807 free_page((unsigned long)vcpu
->run
);
809 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
811 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
812 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
814 return container_of(mn
, struct kvm
, mmu_notifier
);
817 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
818 struct mm_struct
*mm
,
819 unsigned long address
)
821 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
825 * When ->invalidate_page runs, the linux pte has been zapped
826 * already but the page is still allocated until
827 * ->invalidate_page returns. So if we increase the sequence
828 * here the kvm page fault will notice if the spte can't be
829 * established because the page is going to be freed. If
830 * instead the kvm page fault establishes the spte before
831 * ->invalidate_page runs, kvm_unmap_hva will release it
834 * The sequence increase only need to be seen at spin_unlock
835 * time, and not at spin_lock time.
837 * Increasing the sequence after the spin_unlock would be
838 * unsafe because the kvm page fault could then establish the
839 * pte after kvm_unmap_hva returned, without noticing the page
840 * is going to be freed.
842 spin_lock(&kvm
->mmu_lock
);
843 kvm
->mmu_notifier_seq
++;
844 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
845 spin_unlock(&kvm
->mmu_lock
);
847 /* we've to flush the tlb before the pages can be freed */
849 kvm_flush_remote_tlbs(kvm
);
853 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
854 struct mm_struct
*mm
,
858 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
859 int need_tlb_flush
= 0;
861 spin_lock(&kvm
->mmu_lock
);
863 * The count increase must become visible at unlock time as no
864 * spte can be established without taking the mmu_lock and
865 * count is also read inside the mmu_lock critical section.
867 kvm
->mmu_notifier_count
++;
868 for (; start
< end
; start
+= PAGE_SIZE
)
869 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
870 spin_unlock(&kvm
->mmu_lock
);
872 /* we've to flush the tlb before the pages can be freed */
874 kvm_flush_remote_tlbs(kvm
);
877 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
878 struct mm_struct
*mm
,
882 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
884 spin_lock(&kvm
->mmu_lock
);
886 * This sequence increase will notify the kvm page fault that
887 * the page that is going to be mapped in the spte could have
890 kvm
->mmu_notifier_seq
++;
892 * The above sequence increase must be visible before the
893 * below count decrease but both values are read by the kvm
894 * page fault under mmu_lock spinlock so we don't need to add
895 * a smb_wmb() here in between the two.
897 kvm
->mmu_notifier_count
--;
898 spin_unlock(&kvm
->mmu_lock
);
900 BUG_ON(kvm
->mmu_notifier_count
< 0);
903 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
904 struct mm_struct
*mm
,
905 unsigned long address
)
907 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
910 spin_lock(&kvm
->mmu_lock
);
911 young
= kvm_age_hva(kvm
, address
);
912 spin_unlock(&kvm
->mmu_lock
);
915 kvm_flush_remote_tlbs(kvm
);
920 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
921 struct mm_struct
*mm
)
923 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
924 kvm_arch_flush_shadow(kvm
);
927 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
928 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
929 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
930 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
931 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
932 .release
= kvm_mmu_notifier_release
,
934 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
936 static struct kvm
*kvm_create_vm(void)
938 struct kvm
*kvm
= kvm_arch_create_vm();
939 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
945 #ifdef CONFIG_HAVE_KVM_IRQCHIP
946 INIT_LIST_HEAD(&kvm
->irq_routing
);
947 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
950 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
951 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
954 return ERR_PTR(-ENOMEM
);
956 kvm
->coalesced_mmio_ring
=
957 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
960 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
963 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
964 err
= mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
966 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
975 kvm
->mm
= current
->mm
;
976 atomic_inc(&kvm
->mm
->mm_count
);
977 spin_lock_init(&kvm
->mmu_lock
);
978 spin_lock_init(&kvm
->requests_lock
);
979 kvm_io_bus_init(&kvm
->pio_bus
);
980 kvm_eventfd_init(kvm
);
981 mutex_init(&kvm
->lock
);
982 mutex_init(&kvm
->irq_lock
);
983 kvm_io_bus_init(&kvm
->mmio_bus
);
984 init_rwsem(&kvm
->slots_lock
);
985 atomic_set(&kvm
->users_count
, 1);
986 spin_lock(&kvm_lock
);
987 list_add(&kvm
->vm_list
, &vm_list
);
988 spin_unlock(&kvm_lock
);
989 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
990 kvm_coalesced_mmio_init(kvm
);
997 * Free any memory in @free but not in @dont.
999 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
1000 struct kvm_memory_slot
*dont
)
1004 if (!dont
|| free
->rmap
!= dont
->rmap
)
1007 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
1008 vfree(free
->dirty_bitmap
);
1011 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
1012 if (!dont
|| free
->lpage_info
[i
] != dont
->lpage_info
[i
]) {
1013 vfree(free
->lpage_info
[i
]);
1014 free
->lpage_info
[i
] = NULL
;
1019 free
->dirty_bitmap
= NULL
;
1023 void kvm_free_physmem(struct kvm
*kvm
)
1027 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
1028 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
1031 static void kvm_destroy_vm(struct kvm
*kvm
)
1033 struct mm_struct
*mm
= kvm
->mm
;
1035 kvm_arch_sync_events(kvm
);
1036 spin_lock(&kvm_lock
);
1037 list_del(&kvm
->vm_list
);
1038 spin_unlock(&kvm_lock
);
1039 kvm_free_irq_routing(kvm
);
1040 kvm_io_bus_destroy(&kvm
->pio_bus
);
1041 kvm_io_bus_destroy(&kvm
->mmio_bus
);
1042 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1043 if (kvm
->coalesced_mmio_ring
!= NULL
)
1044 free_page((unsigned long)kvm
->coalesced_mmio_ring
);
1046 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1047 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
1049 kvm_arch_flush_shadow(kvm
);
1051 kvm_arch_destroy_vm(kvm
);
1055 void kvm_get_kvm(struct kvm
*kvm
)
1057 atomic_inc(&kvm
->users_count
);
1059 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
1061 void kvm_put_kvm(struct kvm
*kvm
)
1063 if (atomic_dec_and_test(&kvm
->users_count
))
1064 kvm_destroy_vm(kvm
);
1066 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
1069 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
1071 struct kvm
*kvm
= filp
->private_data
;
1073 kvm_irqfd_release(kvm
);
1080 * Allocate some memory and give it an address in the guest physical address
1083 * Discontiguous memory is allowed, mostly for framebuffers.
1085 * Must be called holding mmap_sem for write.
1087 int __kvm_set_memory_region(struct kvm
*kvm
,
1088 struct kvm_userspace_memory_region
*mem
,
1093 unsigned long npages
;
1095 struct kvm_memory_slot
*memslot
;
1096 struct kvm_memory_slot old
, new;
1099 /* General sanity checks */
1100 if (mem
->memory_size
& (PAGE_SIZE
- 1))
1102 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
1104 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
1106 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
1108 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
1111 memslot
= &kvm
->memslots
[mem
->slot
];
1112 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
1113 npages
= mem
->memory_size
>> PAGE_SHIFT
;
1116 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
1118 new = old
= *memslot
;
1120 new.base_gfn
= base_gfn
;
1121 new.npages
= npages
;
1122 new.flags
= mem
->flags
;
1124 /* Disallow changing a memory slot's size. */
1126 if (npages
&& old
.npages
&& npages
!= old
.npages
)
1129 /* Check for overlaps */
1131 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1132 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
1134 if (s
== memslot
|| !s
->npages
)
1136 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
1137 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
1141 /* Free page dirty bitmap if unneeded */
1142 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
1143 new.dirty_bitmap
= NULL
;
1147 /* Allocate if a slot is being created */
1149 if (npages
&& !new.rmap
) {
1150 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
1155 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
1157 new.user_alloc
= user_alloc
;
1159 * hva_to_rmmap() serialzies with the mmu_lock and to be
1160 * safe it has to ignore memslots with !user_alloc &&
1164 new.userspace_addr
= mem
->userspace_addr
;
1166 new.userspace_addr
= 0;
1171 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
1177 /* Avoid unused variable warning if no large pages */
1180 if (new.lpage_info
[i
])
1183 lpages
= 1 + (base_gfn
+ npages
- 1) /
1184 KVM_PAGES_PER_HPAGE(level
);
1185 lpages
-= base_gfn
/ KVM_PAGES_PER_HPAGE(level
);
1187 new.lpage_info
[i
] = vmalloc(lpages
* sizeof(*new.lpage_info
[i
]));
1189 if (!new.lpage_info
[i
])
1192 memset(new.lpage_info
[i
], 0,
1193 lpages
* sizeof(*new.lpage_info
[i
]));
1195 if (base_gfn
% KVM_PAGES_PER_HPAGE(level
))
1196 new.lpage_info
[i
][0].write_count
= 1;
1197 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE(level
))
1198 new.lpage_info
[i
][lpages
- 1].write_count
= 1;
1199 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
1201 * If the gfn and userspace address are not aligned wrt each
1202 * other, or if explicitly asked to, disable large page
1203 * support for this slot
1205 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE(level
) - 1) ||
1206 !largepages_enabled
)
1207 for (j
= 0; j
< lpages
; ++j
)
1208 new.lpage_info
[i
][j
].write_count
= 1;
1213 /* Allocate page dirty bitmap if needed */
1214 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
1215 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
1217 new.dirty_bitmap
= vmalloc(dirty_bytes
);
1218 if (!new.dirty_bitmap
)
1220 memset(new.dirty_bitmap
, 0, dirty_bytes
);
1222 kvm_arch_flush_shadow(kvm
);
1224 #else /* not defined CONFIG_S390 */
1225 new.user_alloc
= user_alloc
;
1227 new.userspace_addr
= mem
->userspace_addr
;
1228 #endif /* not defined CONFIG_S390 */
1231 kvm_arch_flush_shadow(kvm
);
1233 spin_lock(&kvm
->mmu_lock
);
1234 if (mem
->slot
>= kvm
->nmemslots
)
1235 kvm
->nmemslots
= mem
->slot
+ 1;
1238 spin_unlock(&kvm
->mmu_lock
);
1240 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
1242 spin_lock(&kvm
->mmu_lock
);
1244 spin_unlock(&kvm
->mmu_lock
);
1248 kvm_free_physmem_slot(&old
, npages
? &new : NULL
);
1249 /* Slot deletion case: we have to update the current slot */
1250 spin_lock(&kvm
->mmu_lock
);
1253 spin_unlock(&kvm
->mmu_lock
);
1255 /* map the pages in iommu page table */
1256 r
= kvm_iommu_map_pages(kvm
, base_gfn
, npages
);
1263 kvm_free_physmem_slot(&new, &old
);
1268 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
1270 int kvm_set_memory_region(struct kvm
*kvm
,
1271 struct kvm_userspace_memory_region
*mem
,
1276 down_write(&kvm
->slots_lock
);
1277 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
1278 up_write(&kvm
->slots_lock
);
1281 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
1283 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
1285 kvm_userspace_memory_region
*mem
,
1288 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
1290 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
1293 int kvm_get_dirty_log(struct kvm
*kvm
,
1294 struct kvm_dirty_log
*log
, int *is_dirty
)
1296 struct kvm_memory_slot
*memslot
;
1299 unsigned long any
= 0;
1302 if (log
->slot
>= KVM_MEMORY_SLOTS
)
1305 memslot
= &kvm
->memslots
[log
->slot
];
1307 if (!memslot
->dirty_bitmap
)
1310 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1312 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
1313 any
= memslot
->dirty_bitmap
[i
];
1316 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
1327 void kvm_disable_largepages(void)
1329 largepages_enabled
= false;
1331 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
1333 int is_error_page(struct page
*page
)
1335 return page
== bad_page
;
1337 EXPORT_SYMBOL_GPL(is_error_page
);
1339 int is_error_pfn(pfn_t pfn
)
1341 return pfn
== bad_pfn
;
1343 EXPORT_SYMBOL_GPL(is_error_pfn
);
1345 static inline unsigned long bad_hva(void)
1350 int kvm_is_error_hva(unsigned long addr
)
1352 return addr
== bad_hva();
1354 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
1356 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
1360 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1361 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1363 if (gfn
>= memslot
->base_gfn
1364 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1369 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
1371 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1373 gfn
= unalias_gfn(kvm
, gfn
);
1374 return gfn_to_memslot_unaliased(kvm
, gfn
);
1377 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
1381 gfn
= unalias_gfn(kvm
, gfn
);
1382 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1383 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1385 if (gfn
>= memslot
->base_gfn
1386 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1391 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
1393 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1395 struct kvm_memory_slot
*slot
;
1397 gfn
= unalias_gfn(kvm
, gfn
);
1398 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1401 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
1403 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1405 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1407 struct page
*page
[1];
1414 addr
= gfn_to_hva(kvm
, gfn
);
1415 if (kvm_is_error_hva(addr
)) {
1417 return page_to_pfn(bad_page
);
1420 npages
= get_user_pages_fast(addr
, 1, 1, page
);
1422 if (unlikely(npages
!= 1)) {
1423 struct vm_area_struct
*vma
;
1425 down_read(¤t
->mm
->mmap_sem
);
1426 vma
= find_vma(current
->mm
, addr
);
1428 if (vma
== NULL
|| addr
< vma
->vm_start
||
1429 !(vma
->vm_flags
& VM_PFNMAP
)) {
1430 up_read(¤t
->mm
->mmap_sem
);
1432 return page_to_pfn(bad_page
);
1435 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1436 up_read(¤t
->mm
->mmap_sem
);
1437 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1439 pfn
= page_to_pfn(page
[0]);
1444 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1446 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1450 pfn
= gfn_to_pfn(kvm
, gfn
);
1451 if (!kvm_is_mmio_pfn(pfn
))
1452 return pfn_to_page(pfn
);
1454 WARN_ON(kvm_is_mmio_pfn(pfn
));
1460 EXPORT_SYMBOL_GPL(gfn_to_page
);
1462 void kvm_release_page_clean(struct page
*page
)
1464 kvm_release_pfn_clean(page_to_pfn(page
));
1466 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1468 void kvm_release_pfn_clean(pfn_t pfn
)
1470 if (!kvm_is_mmio_pfn(pfn
))
1471 put_page(pfn_to_page(pfn
));
1473 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1475 void kvm_release_page_dirty(struct page
*page
)
1477 kvm_release_pfn_dirty(page_to_pfn(page
));
1479 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1481 void kvm_release_pfn_dirty(pfn_t pfn
)
1483 kvm_set_pfn_dirty(pfn
);
1484 kvm_release_pfn_clean(pfn
);
1486 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1488 void kvm_set_page_dirty(struct page
*page
)
1490 kvm_set_pfn_dirty(page_to_pfn(page
));
1492 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1494 void kvm_set_pfn_dirty(pfn_t pfn
)
1496 if (!kvm_is_mmio_pfn(pfn
)) {
1497 struct page
*page
= pfn_to_page(pfn
);
1498 if (!PageReserved(page
))
1502 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1504 void kvm_set_pfn_accessed(pfn_t pfn
)
1506 if (!kvm_is_mmio_pfn(pfn
))
1507 mark_page_accessed(pfn_to_page(pfn
));
1509 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1511 void kvm_get_pfn(pfn_t pfn
)
1513 if (!kvm_is_mmio_pfn(pfn
))
1514 get_page(pfn_to_page(pfn
));
1516 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1518 static int next_segment(unsigned long len
, int offset
)
1520 if (len
> PAGE_SIZE
- offset
)
1521 return PAGE_SIZE
- offset
;
1526 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1532 addr
= gfn_to_hva(kvm
, gfn
);
1533 if (kvm_is_error_hva(addr
))
1535 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1540 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1542 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1544 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1546 int offset
= offset_in_page(gpa
);
1549 while ((seg
= next_segment(len
, offset
)) != 0) {
1550 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1560 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1562 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1567 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1568 int offset
= offset_in_page(gpa
);
1570 addr
= gfn_to_hva(kvm
, gfn
);
1571 if (kvm_is_error_hva(addr
))
1573 pagefault_disable();
1574 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1580 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1582 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1583 int offset
, int len
)
1588 addr
= gfn_to_hva(kvm
, gfn
);
1589 if (kvm_is_error_hva(addr
))
1591 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1594 mark_page_dirty(kvm
, gfn
);
1597 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1599 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1602 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1604 int offset
= offset_in_page(gpa
);
1607 while ((seg
= next_segment(len
, offset
)) != 0) {
1608 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1619 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1621 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1623 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1625 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1627 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1629 int offset
= offset_in_page(gpa
);
1632 while ((seg
= next_segment(len
, offset
)) != 0) {
1633 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1642 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1644 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1646 struct kvm_memory_slot
*memslot
;
1648 gfn
= unalias_gfn(kvm
, gfn
);
1649 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1650 if (memslot
&& memslot
->dirty_bitmap
) {
1651 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1654 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1655 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1660 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1662 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1667 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1669 if (kvm_arch_vcpu_runnable(vcpu
)) {
1670 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1673 if (kvm_cpu_has_pending_timer(vcpu
))
1675 if (signal_pending(current
))
1683 finish_wait(&vcpu
->wq
, &wait
);
1686 void kvm_resched(struct kvm_vcpu
*vcpu
)
1688 if (!need_resched())
1692 EXPORT_SYMBOL_GPL(kvm_resched
);
1694 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1696 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1699 if (vmf
->pgoff
== 0)
1700 page
= virt_to_page(vcpu
->run
);
1702 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1703 page
= virt_to_page(vcpu
->arch
.pio_data
);
1705 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1706 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1707 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1710 return VM_FAULT_SIGBUS
;
1716 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1717 .fault
= kvm_vcpu_fault
,
1720 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1722 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1726 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1728 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1730 kvm_put_kvm(vcpu
->kvm
);
1734 static struct file_operations kvm_vcpu_fops
= {
1735 .release
= kvm_vcpu_release
,
1736 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1737 .compat_ioctl
= kvm_vcpu_ioctl
,
1738 .mmap
= kvm_vcpu_mmap
,
1742 * Allocates an inode for the vcpu.
1744 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1746 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, 0);
1750 * Creates some virtual cpus. Good luck creating more than one.
1752 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1755 struct kvm_vcpu
*vcpu
, *v
;
1757 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1759 return PTR_ERR(vcpu
);
1761 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1763 r
= kvm_arch_vcpu_setup(vcpu
);
1767 mutex_lock(&kvm
->lock
);
1768 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1773 kvm_for_each_vcpu(r
, v
, kvm
)
1774 if (v
->vcpu_id
== id
) {
1779 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1781 /* Now it's all set up, let userspace reach it */
1783 r
= create_vcpu_fd(vcpu
);
1789 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1791 atomic_inc(&kvm
->online_vcpus
);
1793 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1794 if (kvm
->bsp_vcpu_id
== id
)
1795 kvm
->bsp_vcpu
= vcpu
;
1797 mutex_unlock(&kvm
->lock
);
1801 mutex_unlock(&kvm
->lock
);
1802 kvm_arch_vcpu_destroy(vcpu
);
1806 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1809 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1810 vcpu
->sigset_active
= 1;
1811 vcpu
->sigset
= *sigset
;
1813 vcpu
->sigset_active
= 0;
1817 #ifdef __KVM_HAVE_MSIX
1818 static int kvm_vm_ioctl_set_msix_nr(struct kvm
*kvm
,
1819 struct kvm_assigned_msix_nr
*entry_nr
)
1822 struct kvm_assigned_dev_kernel
*adev
;
1824 mutex_lock(&kvm
->lock
);
1826 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
1827 entry_nr
->assigned_dev_id
);
1833 if (adev
->entries_nr
== 0) {
1834 adev
->entries_nr
= entry_nr
->entry_nr
;
1835 if (adev
->entries_nr
== 0 ||
1836 adev
->entries_nr
>= KVM_MAX_MSIX_PER_DEV
) {
1841 adev
->host_msix_entries
= kzalloc(sizeof(struct msix_entry
) *
1844 if (!adev
->host_msix_entries
) {
1848 adev
->guest_msix_entries
= kzalloc(
1849 sizeof(struct kvm_guest_msix_entry
) *
1850 entry_nr
->entry_nr
, GFP_KERNEL
);
1851 if (!adev
->guest_msix_entries
) {
1852 kfree(adev
->host_msix_entries
);
1856 } else /* Not allowed set MSI-X number twice */
1859 mutex_unlock(&kvm
->lock
);
1863 static int kvm_vm_ioctl_set_msix_entry(struct kvm
*kvm
,
1864 struct kvm_assigned_msix_entry
*entry
)
1867 struct kvm_assigned_dev_kernel
*adev
;
1869 mutex_lock(&kvm
->lock
);
1871 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
1872 entry
->assigned_dev_id
);
1876 goto msix_entry_out
;
1879 for (i
= 0; i
< adev
->entries_nr
; i
++)
1880 if (adev
->guest_msix_entries
[i
].vector
== 0 ||
1881 adev
->guest_msix_entries
[i
].entry
== entry
->entry
) {
1882 adev
->guest_msix_entries
[i
].entry
= entry
->entry
;
1883 adev
->guest_msix_entries
[i
].vector
= entry
->gsi
;
1884 adev
->host_msix_entries
[i
].entry
= entry
->entry
;
1887 if (i
== adev
->entries_nr
) {
1889 goto msix_entry_out
;
1893 mutex_unlock(&kvm
->lock
);
1899 static long kvm_vcpu_ioctl(struct file
*filp
,
1900 unsigned int ioctl
, unsigned long arg
)
1902 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1903 void __user
*argp
= (void __user
*)arg
;
1905 struct kvm_fpu
*fpu
= NULL
;
1906 struct kvm_sregs
*kvm_sregs
= NULL
;
1908 if (vcpu
->kvm
->mm
!= current
->mm
)
1915 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1917 case KVM_GET_REGS
: {
1918 struct kvm_regs
*kvm_regs
;
1921 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1924 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1928 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1935 case KVM_SET_REGS
: {
1936 struct kvm_regs
*kvm_regs
;
1939 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1943 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1945 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1953 case KVM_GET_SREGS
: {
1954 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1958 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1962 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1967 case KVM_SET_SREGS
: {
1968 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1973 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1975 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1981 case KVM_GET_MP_STATE
: {
1982 struct kvm_mp_state mp_state
;
1984 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1988 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1993 case KVM_SET_MP_STATE
: {
1994 struct kvm_mp_state mp_state
;
1997 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1999 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
2005 case KVM_TRANSLATE
: {
2006 struct kvm_translation tr
;
2009 if (copy_from_user(&tr
, argp
, sizeof tr
))
2011 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
2015 if (copy_to_user(argp
, &tr
, sizeof tr
))
2020 case KVM_SET_GUEST_DEBUG
: {
2021 struct kvm_guest_debug dbg
;
2024 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
2026 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
2032 case KVM_SET_SIGNAL_MASK
: {
2033 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
2034 struct kvm_signal_mask kvm_sigmask
;
2035 sigset_t sigset
, *p
;
2040 if (copy_from_user(&kvm_sigmask
, argp
,
2041 sizeof kvm_sigmask
))
2044 if (kvm_sigmask
.len
!= sizeof sigset
)
2047 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
2052 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
2056 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
2060 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
2064 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
2070 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
2075 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
2077 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
2084 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
2092 static long kvm_vm_ioctl(struct file
*filp
,
2093 unsigned int ioctl
, unsigned long arg
)
2095 struct kvm
*kvm
= filp
->private_data
;
2096 void __user
*argp
= (void __user
*)arg
;
2099 if (kvm
->mm
!= current
->mm
)
2102 case KVM_CREATE_VCPU
:
2103 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2107 case KVM_SET_USER_MEMORY_REGION
: {
2108 struct kvm_userspace_memory_region kvm_userspace_mem
;
2111 if (copy_from_user(&kvm_userspace_mem
, argp
,
2112 sizeof kvm_userspace_mem
))
2115 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
2120 case KVM_GET_DIRTY_LOG
: {
2121 struct kvm_dirty_log log
;
2124 if (copy_from_user(&log
, argp
, sizeof log
))
2126 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2131 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2132 case KVM_REGISTER_COALESCED_MMIO
: {
2133 struct kvm_coalesced_mmio_zone zone
;
2135 if (copy_from_user(&zone
, argp
, sizeof zone
))
2138 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
2144 case KVM_UNREGISTER_COALESCED_MMIO
: {
2145 struct kvm_coalesced_mmio_zone zone
;
2147 if (copy_from_user(&zone
, argp
, sizeof zone
))
2150 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
2157 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
2158 case KVM_ASSIGN_PCI_DEVICE
: {
2159 struct kvm_assigned_pci_dev assigned_dev
;
2162 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
2164 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
2169 case KVM_ASSIGN_IRQ
: {
2173 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
2174 case KVM_ASSIGN_DEV_IRQ
: {
2175 struct kvm_assigned_irq assigned_irq
;
2178 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
2180 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
2185 case KVM_DEASSIGN_DEV_IRQ
: {
2186 struct kvm_assigned_irq assigned_irq
;
2189 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
2191 r
= kvm_vm_ioctl_deassign_dev_irq(kvm
, &assigned_irq
);
2198 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2199 case KVM_DEASSIGN_PCI_DEVICE
: {
2200 struct kvm_assigned_pci_dev assigned_dev
;
2203 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
2205 r
= kvm_vm_ioctl_deassign_device(kvm
, &assigned_dev
);
2211 #ifdef KVM_CAP_IRQ_ROUTING
2212 case KVM_SET_GSI_ROUTING
: {
2213 struct kvm_irq_routing routing
;
2214 struct kvm_irq_routing __user
*urouting
;
2215 struct kvm_irq_routing_entry
*entries
;
2218 if (copy_from_user(&routing
, argp
, sizeof(routing
)))
2221 if (routing
.nr
>= KVM_MAX_IRQ_ROUTES
)
2226 entries
= vmalloc(routing
.nr
* sizeof(*entries
));
2231 if (copy_from_user(entries
, urouting
->entries
,
2232 routing
.nr
* sizeof(*entries
)))
2233 goto out_free_irq_routing
;
2234 r
= kvm_set_irq_routing(kvm
, entries
, routing
.nr
,
2236 out_free_irq_routing
:
2240 #endif /* KVM_CAP_IRQ_ROUTING */
2241 #ifdef __KVM_HAVE_MSIX
2242 case KVM_ASSIGN_SET_MSIX_NR
: {
2243 struct kvm_assigned_msix_nr entry_nr
;
2245 if (copy_from_user(&entry_nr
, argp
, sizeof entry_nr
))
2247 r
= kvm_vm_ioctl_set_msix_nr(kvm
, &entry_nr
);
2252 case KVM_ASSIGN_SET_MSIX_ENTRY
: {
2253 struct kvm_assigned_msix_entry entry
;
2255 if (copy_from_user(&entry
, argp
, sizeof entry
))
2257 r
= kvm_vm_ioctl_set_msix_entry(kvm
, &entry
);
2264 struct kvm_irqfd data
;
2267 if (copy_from_user(&data
, argp
, sizeof data
))
2269 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
2272 case KVM_IOEVENTFD
: {
2273 struct kvm_ioeventfd data
;
2276 if (copy_from_user(&data
, argp
, sizeof data
))
2278 r
= kvm_ioeventfd(kvm
, &data
);
2281 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2282 case KVM_SET_BOOT_CPU_ID
:
2284 mutex_lock(&kvm
->lock
);
2285 if (atomic_read(&kvm
->online_vcpus
) != 0)
2288 kvm
->bsp_vcpu_id
= arg
;
2289 mutex_unlock(&kvm
->lock
);
2293 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
2299 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2301 struct page
*page
[1];
2304 gfn_t gfn
= vmf
->pgoff
;
2305 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2307 addr
= gfn_to_hva(kvm
, gfn
);
2308 if (kvm_is_error_hva(addr
))
2309 return VM_FAULT_SIGBUS
;
2311 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
2313 if (unlikely(npages
!= 1))
2314 return VM_FAULT_SIGBUS
;
2316 vmf
->page
= page
[0];
2320 static const struct vm_operations_struct kvm_vm_vm_ops
= {
2321 .fault
= kvm_vm_fault
,
2324 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2326 vma
->vm_ops
= &kvm_vm_vm_ops
;
2330 static struct file_operations kvm_vm_fops
= {
2331 .release
= kvm_vm_release
,
2332 .unlocked_ioctl
= kvm_vm_ioctl
,
2333 .compat_ioctl
= kvm_vm_ioctl
,
2334 .mmap
= kvm_vm_mmap
,
2337 static int kvm_dev_ioctl_create_vm(void)
2342 kvm
= kvm_create_vm();
2344 return PTR_ERR(kvm
);
2345 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, 0);
2352 static long kvm_dev_ioctl_check_extension_generic(long arg
)
2355 case KVM_CAP_USER_MEMORY
:
2356 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
2357 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
2358 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2359 case KVM_CAP_SET_BOOT_CPU_ID
:
2362 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2363 case KVM_CAP_IRQ_ROUTING
:
2364 return KVM_MAX_IRQ_ROUTES
;
2369 return kvm_dev_ioctl_check_extension(arg
);
2372 static long kvm_dev_ioctl(struct file
*filp
,
2373 unsigned int ioctl
, unsigned long arg
)
2378 case KVM_GET_API_VERSION
:
2382 r
= KVM_API_VERSION
;
2388 r
= kvm_dev_ioctl_create_vm();
2390 case KVM_CHECK_EXTENSION
:
2391 r
= kvm_dev_ioctl_check_extension_generic(arg
);
2393 case KVM_GET_VCPU_MMAP_SIZE
:
2397 r
= PAGE_SIZE
; /* struct kvm_run */
2399 r
+= PAGE_SIZE
; /* pio data page */
2401 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2402 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
2405 case KVM_TRACE_ENABLE
:
2406 case KVM_TRACE_PAUSE
:
2407 case KVM_TRACE_DISABLE
:
2411 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
2417 static struct file_operations kvm_chardev_ops
= {
2418 .unlocked_ioctl
= kvm_dev_ioctl
,
2419 .compat_ioctl
= kvm_dev_ioctl
,
2422 static struct miscdevice kvm_dev
= {
2428 static void hardware_enable(void *junk
)
2430 int cpu
= raw_smp_processor_id();
2432 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2434 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
2435 kvm_arch_hardware_enable(NULL
);
2438 static void hardware_disable(void *junk
)
2440 int cpu
= raw_smp_processor_id();
2442 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2444 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2445 kvm_arch_hardware_disable(NULL
);
2448 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2453 val
&= ~CPU_TASKS_FROZEN
;
2456 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2458 hardware_disable(NULL
);
2460 case CPU_UP_CANCELED
:
2461 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2463 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
2466 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2468 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
2475 asmlinkage
void kvm_handle_fault_on_reboot(void)
2478 /* spin while reset goes on */
2481 /* Fault while not rebooting. We want the trace. */
2484 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
2486 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2490 * Some (well, at least mine) BIOSes hang on reboot if
2493 * And Intel TXT required VMX off for all cpu when system shutdown.
2495 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2496 kvm_rebooting
= true;
2497 on_each_cpu(hardware_disable
, NULL
, 1);
2501 static struct notifier_block kvm_reboot_notifier
= {
2502 .notifier_call
= kvm_reboot
,
2506 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
2508 memset(bus
, 0, sizeof(*bus
));
2511 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2515 for (i
= 0; i
< bus
->dev_count
; i
++) {
2516 struct kvm_io_device
*pos
= bus
->devs
[i
];
2518 kvm_iodevice_destructor(pos
);
2522 /* kvm_io_bus_write - called under kvm->slots_lock */
2523 int kvm_io_bus_write(struct kvm_io_bus
*bus
, gpa_t addr
,
2524 int len
, const void *val
)
2527 for (i
= 0; i
< bus
->dev_count
; i
++)
2528 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
2533 /* kvm_io_bus_read - called under kvm->slots_lock */
2534 int kvm_io_bus_read(struct kvm_io_bus
*bus
, gpa_t addr
, int len
, void *val
)
2537 for (i
= 0; i
< bus
->dev_count
; i
++)
2538 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
2543 int kvm_io_bus_register_dev(struct kvm
*kvm
, struct kvm_io_bus
*bus
,
2544 struct kvm_io_device
*dev
)
2548 down_write(&kvm
->slots_lock
);
2549 ret
= __kvm_io_bus_register_dev(bus
, dev
);
2550 up_write(&kvm
->slots_lock
);
2555 /* An unlocked version. Caller must have write lock on slots_lock. */
2556 int __kvm_io_bus_register_dev(struct kvm_io_bus
*bus
,
2557 struct kvm_io_device
*dev
)
2559 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2562 bus
->devs
[bus
->dev_count
++] = dev
;
2567 void kvm_io_bus_unregister_dev(struct kvm
*kvm
,
2568 struct kvm_io_bus
*bus
,
2569 struct kvm_io_device
*dev
)
2571 down_write(&kvm
->slots_lock
);
2572 __kvm_io_bus_unregister_dev(bus
, dev
);
2573 up_write(&kvm
->slots_lock
);
2576 /* An unlocked version. Caller must have write lock on slots_lock. */
2577 void __kvm_io_bus_unregister_dev(struct kvm_io_bus
*bus
,
2578 struct kvm_io_device
*dev
)
2582 for (i
= 0; i
< bus
->dev_count
; i
++)
2583 if (bus
->devs
[i
] == dev
) {
2584 bus
->devs
[i
] = bus
->devs
[--bus
->dev_count
];
2589 static struct notifier_block kvm_cpu_notifier
= {
2590 .notifier_call
= kvm_cpu_hotplug
,
2591 .priority
= 20, /* must be > scheduler priority */
2594 static int vm_stat_get(void *_offset
, u64
*val
)
2596 unsigned offset
= (long)_offset
;
2600 spin_lock(&kvm_lock
);
2601 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2602 *val
+= *(u32
*)((void *)kvm
+ offset
);
2603 spin_unlock(&kvm_lock
);
2607 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2609 static int vcpu_stat_get(void *_offset
, u64
*val
)
2611 unsigned offset
= (long)_offset
;
2613 struct kvm_vcpu
*vcpu
;
2617 spin_lock(&kvm_lock
);
2618 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2619 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2620 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2622 spin_unlock(&kvm_lock
);
2626 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2628 static const struct file_operations
*stat_fops
[] = {
2629 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2630 [KVM_STAT_VM
] = &vm_stat_fops
,
2633 static void kvm_init_debug(void)
2635 struct kvm_stats_debugfs_item
*p
;
2637 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2638 for (p
= debugfs_entries
; p
->name
; ++p
)
2639 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2640 (void *)(long)p
->offset
,
2641 stat_fops
[p
->kind
]);
2644 static void kvm_exit_debug(void)
2646 struct kvm_stats_debugfs_item
*p
;
2648 for (p
= debugfs_entries
; p
->name
; ++p
)
2649 debugfs_remove(p
->dentry
);
2650 debugfs_remove(kvm_debugfs_dir
);
2653 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2655 hardware_disable(NULL
);
2659 static int kvm_resume(struct sys_device
*dev
)
2661 hardware_enable(NULL
);
2665 static struct sysdev_class kvm_sysdev_class
= {
2667 .suspend
= kvm_suspend
,
2668 .resume
= kvm_resume
,
2671 static struct sys_device kvm_sysdev
= {
2673 .cls
= &kvm_sysdev_class
,
2676 struct page
*bad_page
;
2680 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2682 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2685 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2687 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2689 kvm_arch_vcpu_load(vcpu
, cpu
);
2692 static void kvm_sched_out(struct preempt_notifier
*pn
,
2693 struct task_struct
*next
)
2695 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2697 kvm_arch_vcpu_put(vcpu
);
2700 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2701 struct module
*module
)
2708 r
= kvm_arch_init(opaque
);
2712 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2714 if (bad_page
== NULL
) {
2719 bad_pfn
= page_to_pfn(bad_page
);
2721 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2726 r
= kvm_arch_hardware_setup();
2730 for_each_online_cpu(cpu
) {
2731 smp_call_function_single(cpu
,
2732 kvm_arch_check_processor_compat
,
2738 on_each_cpu(hardware_enable
, NULL
, 1);
2739 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2742 register_reboot_notifier(&kvm_reboot_notifier
);
2744 r
= sysdev_class_register(&kvm_sysdev_class
);
2748 r
= sysdev_register(&kvm_sysdev
);
2752 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2753 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2754 __alignof__(struct kvm_vcpu
),
2756 if (!kvm_vcpu_cache
) {
2761 kvm_chardev_ops
.owner
= module
;
2762 kvm_vm_fops
.owner
= module
;
2763 kvm_vcpu_fops
.owner
= module
;
2765 r
= misc_register(&kvm_dev
);
2767 printk(KERN_ERR
"kvm: misc device register failed\n");
2771 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2772 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2777 kmem_cache_destroy(kvm_vcpu_cache
);
2779 sysdev_unregister(&kvm_sysdev
);
2781 sysdev_class_unregister(&kvm_sysdev_class
);
2783 unregister_reboot_notifier(&kvm_reboot_notifier
);
2784 unregister_cpu_notifier(&kvm_cpu_notifier
);
2786 on_each_cpu(hardware_disable
, NULL
, 1);
2788 kvm_arch_hardware_unsetup();
2790 free_cpumask_var(cpus_hardware_enabled
);
2792 __free_page(bad_page
);
2799 EXPORT_SYMBOL_GPL(kvm_init
);
2803 tracepoint_synchronize_unregister();
2804 misc_deregister(&kvm_dev
);
2805 kmem_cache_destroy(kvm_vcpu_cache
);
2806 sysdev_unregister(&kvm_sysdev
);
2807 sysdev_class_unregister(&kvm_sysdev_class
);
2808 unregister_reboot_notifier(&kvm_reboot_notifier
);
2809 unregister_cpu_notifier(&kvm_cpu_notifier
);
2810 on_each_cpu(hardware_disable
, NULL
, 1);
2811 kvm_arch_hardware_unsetup();
2814 free_cpumask_var(cpus_hardware_enabled
);
2815 __free_page(bad_page
);
2817 EXPORT_SYMBOL_GPL(kvm_exit
);