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 MODULE_AUTHOR("Qumranet");
63 MODULE_LICENSE("GPL");
65 DEFINE_SPINLOCK(kvm_lock
);
68 static cpumask_var_t cpus_hardware_enabled
;
70 struct kmem_cache
*kvm_vcpu_cache
;
71 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
73 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
75 struct dentry
*kvm_debugfs_dir
;
77 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
80 static bool kvm_rebooting
;
82 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
83 static struct kvm_assigned_dev_kernel
*kvm_find_assigned_dev(struct list_head
*head
,
86 struct list_head
*ptr
;
87 struct kvm_assigned_dev_kernel
*match
;
89 list_for_each(ptr
, head
) {
90 match
= list_entry(ptr
, struct kvm_assigned_dev_kernel
, list
);
91 if (match
->assigned_dev_id
== assigned_dev_id
)
97 static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
98 *assigned_dev
, int irq
)
101 struct msix_entry
*host_msix_entries
;
103 host_msix_entries
= assigned_dev
->host_msix_entries
;
106 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
107 if (irq
== host_msix_entries
[i
].vector
) {
112 printk(KERN_WARNING
"Fail to find correlated MSI-X entry!\n");
119 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct
*work
)
121 struct kvm_assigned_dev_kernel
*assigned_dev
;
125 assigned_dev
= container_of(work
, struct kvm_assigned_dev_kernel
,
127 kvm
= assigned_dev
->kvm
;
129 /* This is taken to safely inject irq inside the guest. When
130 * the interrupt injection (or the ioapic code) uses a
131 * finer-grained lock, update this
133 mutex_lock(&kvm
->lock
);
134 spin_lock_irq(&assigned_dev
->assigned_dev_lock
);
135 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSIX
) {
136 struct kvm_guest_msix_entry
*guest_entries
=
137 assigned_dev
->guest_msix_entries
;
138 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++) {
139 if (!(guest_entries
[i
].flags
&
140 KVM_ASSIGNED_MSIX_PENDING
))
142 guest_entries
[i
].flags
&= ~KVM_ASSIGNED_MSIX_PENDING
;
143 kvm_set_irq(assigned_dev
->kvm
,
144 assigned_dev
->irq_source_id
,
145 guest_entries
[i
].vector
, 1);
146 irq
= assigned_dev
->host_msix_entries
[i
].vector
;
149 assigned_dev
->host_irq_disabled
= false;
152 kvm_set_irq(assigned_dev
->kvm
, assigned_dev
->irq_source_id
,
153 assigned_dev
->guest_irq
, 1);
154 if (assigned_dev
->irq_requested_type
&
155 KVM_DEV_IRQ_GUEST_MSI
) {
156 enable_irq(assigned_dev
->host_irq
);
157 assigned_dev
->host_irq_disabled
= false;
161 spin_unlock_irq(&assigned_dev
->assigned_dev_lock
);
162 mutex_unlock(&assigned_dev
->kvm
->lock
);
165 static irqreturn_t
kvm_assigned_dev_intr(int irq
, void *dev_id
)
168 struct kvm_assigned_dev_kernel
*assigned_dev
=
169 (struct kvm_assigned_dev_kernel
*) dev_id
;
171 spin_lock_irqsave(&assigned_dev
->assigned_dev_lock
, flags
);
172 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSIX
) {
173 int index
= find_index_from_host_irq(assigned_dev
, irq
);
176 assigned_dev
->guest_msix_entries
[index
].flags
|=
177 KVM_ASSIGNED_MSIX_PENDING
;
180 schedule_work(&assigned_dev
->interrupt_work
);
182 disable_irq_nosync(irq
);
183 assigned_dev
->host_irq_disabled
= true;
186 spin_unlock_irqrestore(&assigned_dev
->assigned_dev_lock
, flags
);
190 /* Ack the irq line for an assigned device */
191 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
193 struct kvm_assigned_dev_kernel
*dev
;
199 dev
= container_of(kian
, struct kvm_assigned_dev_kernel
,
202 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
204 /* The guest irq may be shared so this ack may be
205 * from another device.
207 spin_lock_irqsave(&dev
->assigned_dev_lock
, flags
);
208 if (dev
->host_irq_disabled
) {
209 enable_irq(dev
->host_irq
);
210 dev
->host_irq_disabled
= false;
212 spin_unlock_irqrestore(&dev
->assigned_dev_lock
, flags
);
215 static void deassign_guest_irq(struct kvm
*kvm
,
216 struct kvm_assigned_dev_kernel
*assigned_dev
)
218 kvm_unregister_irq_ack_notifier(&assigned_dev
->ack_notifier
);
219 assigned_dev
->ack_notifier
.gsi
= -1;
221 if (assigned_dev
->irq_source_id
!= -1)
222 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
223 assigned_dev
->irq_source_id
= -1;
224 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_GUEST_MASK
);
227 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
228 static void deassign_host_irq(struct kvm
*kvm
,
229 struct kvm_assigned_dev_kernel
*assigned_dev
)
232 * In kvm_free_device_irq, cancel_work_sync return true if:
233 * 1. work is scheduled, and then cancelled.
234 * 2. work callback is executed.
236 * The first one ensured that the irq is disabled and no more events
237 * would happen. But for the second one, the irq may be enabled (e.g.
238 * for MSI). So we disable irq here to prevent further events.
240 * Notice this maybe result in nested disable if the interrupt type is
241 * INTx, but it's OK for we are going to free it.
243 * If this function is a part of VM destroy, please ensure that till
244 * now, the kvm state is still legal for probably we also have to wait
245 * interrupt_work done.
247 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSIX
) {
249 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
250 disable_irq_nosync(assigned_dev
->
251 host_msix_entries
[i
].vector
);
253 cancel_work_sync(&assigned_dev
->interrupt_work
);
255 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
256 free_irq(assigned_dev
->host_msix_entries
[i
].vector
,
257 (void *)assigned_dev
);
259 assigned_dev
->entries_nr
= 0;
260 kfree(assigned_dev
->host_msix_entries
);
261 kfree(assigned_dev
->guest_msix_entries
);
262 pci_disable_msix(assigned_dev
->dev
);
264 /* Deal with MSI and INTx */
265 disable_irq_nosync(assigned_dev
->host_irq
);
266 cancel_work_sync(&assigned_dev
->interrupt_work
);
268 free_irq(assigned_dev
->host_irq
, (void *)assigned_dev
);
270 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSI
)
271 pci_disable_msi(assigned_dev
->dev
);
274 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_HOST_MASK
);
277 static int kvm_deassign_irq(struct kvm
*kvm
,
278 struct kvm_assigned_dev_kernel
*assigned_dev
,
279 unsigned long irq_requested_type
)
281 unsigned long guest_irq_type
, host_irq_type
;
283 if (!irqchip_in_kernel(kvm
))
285 /* no irq assignment to deassign */
286 if (!assigned_dev
->irq_requested_type
)
289 host_irq_type
= irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
;
290 guest_irq_type
= irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
;
293 deassign_host_irq(kvm
, assigned_dev
);
295 deassign_guest_irq(kvm
, assigned_dev
);
300 static void kvm_free_assigned_irq(struct kvm
*kvm
,
301 struct kvm_assigned_dev_kernel
*assigned_dev
)
303 kvm_deassign_irq(kvm
, assigned_dev
, assigned_dev
->irq_requested_type
);
306 static void kvm_free_assigned_device(struct kvm
*kvm
,
307 struct kvm_assigned_dev_kernel
310 kvm_free_assigned_irq(kvm
, assigned_dev
);
312 pci_reset_function(assigned_dev
->dev
);
314 pci_release_regions(assigned_dev
->dev
);
315 pci_disable_device(assigned_dev
->dev
);
316 pci_dev_put(assigned_dev
->dev
);
318 list_del(&assigned_dev
->list
);
322 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
324 struct list_head
*ptr
, *ptr2
;
325 struct kvm_assigned_dev_kernel
*assigned_dev
;
327 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
328 assigned_dev
= list_entry(ptr
,
329 struct kvm_assigned_dev_kernel
,
332 kvm_free_assigned_device(kvm
, assigned_dev
);
336 static int assigned_device_enable_host_intx(struct kvm
*kvm
,
337 struct kvm_assigned_dev_kernel
*dev
)
339 dev
->host_irq
= dev
->dev
->irq
;
340 /* Even though this is PCI, we don't want to use shared
341 * interrupts. Sharing host devices with guest-assigned devices
342 * on the same interrupt line is not a happy situation: there
343 * are going to be long delays in accepting, acking, etc.
345 if (request_irq(dev
->host_irq
, kvm_assigned_dev_intr
,
346 0, "kvm_assigned_intx_device", (void *)dev
))
351 #ifdef __KVM_HAVE_MSI
352 static int assigned_device_enable_host_msi(struct kvm
*kvm
,
353 struct kvm_assigned_dev_kernel
*dev
)
357 if (!dev
->dev
->msi_enabled
) {
358 r
= pci_enable_msi(dev
->dev
);
363 dev
->host_irq
= dev
->dev
->irq
;
364 if (request_irq(dev
->host_irq
, kvm_assigned_dev_intr
, 0,
365 "kvm_assigned_msi_device", (void *)dev
)) {
366 pci_disable_msi(dev
->dev
);
374 #ifdef __KVM_HAVE_MSIX
375 static int assigned_device_enable_host_msix(struct kvm
*kvm
,
376 struct kvm_assigned_dev_kernel
*dev
)
380 /* host_msix_entries and guest_msix_entries should have been
382 if (dev
->entries_nr
== 0)
385 r
= pci_enable_msix(dev
->dev
, dev
->host_msix_entries
, dev
->entries_nr
);
389 for (i
= 0; i
< dev
->entries_nr
; i
++) {
390 r
= request_irq(dev
->host_msix_entries
[i
].vector
,
391 kvm_assigned_dev_intr
, 0,
392 "kvm_assigned_msix_device",
394 /* FIXME: free requested_irq's on failure */
404 static int assigned_device_enable_guest_intx(struct kvm
*kvm
,
405 struct kvm_assigned_dev_kernel
*dev
,
406 struct kvm_assigned_irq
*irq
)
408 dev
->guest_irq
= irq
->guest_irq
;
409 dev
->ack_notifier
.gsi
= irq
->guest_irq
;
413 #ifdef __KVM_HAVE_MSI
414 static int assigned_device_enable_guest_msi(struct kvm
*kvm
,
415 struct kvm_assigned_dev_kernel
*dev
,
416 struct kvm_assigned_irq
*irq
)
418 dev
->guest_irq
= irq
->guest_irq
;
419 dev
->ack_notifier
.gsi
= -1;
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;
434 static int assign_host_irq(struct kvm
*kvm
,
435 struct kvm_assigned_dev_kernel
*dev
,
440 if (dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
)
443 switch (host_irq_type
) {
444 case KVM_DEV_IRQ_HOST_INTX
:
445 r
= assigned_device_enable_host_intx(kvm
, dev
);
447 #ifdef __KVM_HAVE_MSI
448 case KVM_DEV_IRQ_HOST_MSI
:
449 r
= assigned_device_enable_host_msi(kvm
, dev
);
452 #ifdef __KVM_HAVE_MSIX
453 case KVM_DEV_IRQ_HOST_MSIX
:
454 r
= assigned_device_enable_host_msix(kvm
, dev
);
462 dev
->irq_requested_type
|= host_irq_type
;
467 static int assign_guest_irq(struct kvm
*kvm
,
468 struct kvm_assigned_dev_kernel
*dev
,
469 struct kvm_assigned_irq
*irq
,
470 unsigned long guest_irq_type
)
475 if (dev
->irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
)
478 id
= kvm_request_irq_source_id(kvm
);
482 dev
->irq_source_id
= id
;
484 switch (guest_irq_type
) {
485 case KVM_DEV_IRQ_GUEST_INTX
:
486 r
= assigned_device_enable_guest_intx(kvm
, dev
, irq
);
488 #ifdef __KVM_HAVE_MSI
489 case KVM_DEV_IRQ_GUEST_MSI
:
490 r
= assigned_device_enable_guest_msi(kvm
, dev
, irq
);
493 #ifdef __KVM_HAVE_MSIX
494 case KVM_DEV_IRQ_GUEST_MSIX
:
495 r
= assigned_device_enable_guest_msix(kvm
, dev
, irq
);
503 dev
->irq_requested_type
|= guest_irq_type
;
504 kvm_register_irq_ack_notifier(kvm
, &dev
->ack_notifier
);
506 kvm_free_irq_source_id(kvm
, dev
->irq_source_id
);
511 /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
512 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
513 struct kvm_assigned_irq
*assigned_irq
)
516 struct kvm_assigned_dev_kernel
*match
;
517 unsigned long host_irq_type
, guest_irq_type
;
519 if (!capable(CAP_SYS_RAWIO
))
522 if (!irqchip_in_kernel(kvm
))
525 mutex_lock(&kvm
->lock
);
527 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
528 assigned_irq
->assigned_dev_id
);
532 host_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_HOST_MASK
);
533 guest_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_GUEST_MASK
);
536 /* can only assign one type at a time */
537 if (hweight_long(host_irq_type
) > 1)
539 if (hweight_long(guest_irq_type
) > 1)
541 if (host_irq_type
== 0 && guest_irq_type
== 0)
546 r
= assign_host_irq(kvm
, match
, host_irq_type
);
551 r
= assign_guest_irq(kvm
, match
, assigned_irq
, guest_irq_type
);
553 mutex_unlock(&kvm
->lock
);
557 static int kvm_vm_ioctl_deassign_dev_irq(struct kvm
*kvm
,
558 struct kvm_assigned_irq
562 struct kvm_assigned_dev_kernel
*match
;
564 mutex_lock(&kvm
->lock
);
566 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
567 assigned_irq
->assigned_dev_id
);
571 r
= kvm_deassign_irq(kvm
, match
, assigned_irq
->flags
);
573 mutex_unlock(&kvm
->lock
);
577 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
578 struct kvm_assigned_pci_dev
*assigned_dev
)
581 struct kvm_assigned_dev_kernel
*match
;
584 down_read(&kvm
->slots_lock
);
585 mutex_lock(&kvm
->lock
);
587 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
588 assigned_dev
->assigned_dev_id
);
590 /* device already assigned */
595 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
597 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
602 dev
= pci_get_bus_and_slot(assigned_dev
->busnr
,
603 assigned_dev
->devfn
);
605 printk(KERN_INFO
"%s: host device not found\n", __func__
);
609 if (pci_enable_device(dev
)) {
610 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
614 r
= pci_request_regions(dev
, "kvm_assigned_device");
616 printk(KERN_INFO
"%s: Could not get access to device regions\n",
621 pci_reset_function(dev
);
623 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
624 match
->host_busnr
= assigned_dev
->busnr
;
625 match
->host_devfn
= assigned_dev
->devfn
;
626 match
->flags
= assigned_dev
->flags
;
628 spin_lock_init(&match
->assigned_dev_lock
);
629 match
->irq_source_id
= -1;
631 match
->ack_notifier
.irq_acked
= kvm_assigned_dev_ack_irq
;
632 INIT_WORK(&match
->interrupt_work
,
633 kvm_assigned_dev_interrupt_work_handler
);
635 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
637 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
) {
638 if (!kvm
->arch
.iommu_domain
) {
639 r
= kvm_iommu_map_guest(kvm
);
643 r
= kvm_assign_device(kvm
, match
);
649 mutex_unlock(&kvm
->lock
);
650 up_read(&kvm
->slots_lock
);
653 list_del(&match
->list
);
654 pci_release_regions(dev
);
656 pci_disable_device(dev
);
661 mutex_unlock(&kvm
->lock
);
662 up_read(&kvm
->slots_lock
);
667 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
668 static int kvm_vm_ioctl_deassign_device(struct kvm
*kvm
,
669 struct kvm_assigned_pci_dev
*assigned_dev
)
672 struct kvm_assigned_dev_kernel
*match
;
674 mutex_lock(&kvm
->lock
);
676 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
677 assigned_dev
->assigned_dev_id
);
679 printk(KERN_INFO
"%s: device hasn't been assigned before, "
680 "so cannot be deassigned\n", __func__
);
685 if (match
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
)
686 kvm_deassign_device(kvm
, match
);
688 kvm_free_assigned_device(kvm
, match
);
691 mutex_unlock(&kvm
->lock
);
696 static inline int valid_vcpu(int n
)
698 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
701 inline int kvm_is_mmio_pfn(pfn_t pfn
)
703 if (pfn_valid(pfn
)) {
704 struct page
*page
= compound_head(pfn_to_page(pfn
));
705 return PageReserved(page
);
712 * Switches to specified vcpu, until a matching vcpu_put()
714 void vcpu_load(struct kvm_vcpu
*vcpu
)
718 mutex_lock(&vcpu
->mutex
);
720 preempt_notifier_register(&vcpu
->preempt_notifier
);
721 kvm_arch_vcpu_load(vcpu
, cpu
);
725 void vcpu_put(struct kvm_vcpu
*vcpu
)
728 kvm_arch_vcpu_put(vcpu
);
729 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
731 mutex_unlock(&vcpu
->mutex
);
734 static void ack_flush(void *_completed
)
738 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
743 struct kvm_vcpu
*vcpu
;
745 if (alloc_cpumask_var(&cpus
, GFP_ATOMIC
))
749 spin_lock(&kvm
->requests_lock
);
750 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
751 vcpu
= kvm
->vcpus
[i
];
754 if (test_and_set_bit(req
, &vcpu
->requests
))
757 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
758 cpumask_set_cpu(cpu
, cpus
);
760 if (unlikely(cpus
== NULL
))
761 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
762 else if (!cpumask_empty(cpus
))
763 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
766 spin_unlock(&kvm
->requests_lock
);
768 free_cpumask_var(cpus
);
772 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
774 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
775 ++kvm
->stat
.remote_tlb_flush
;
778 void kvm_reload_remote_mmus(struct kvm
*kvm
)
780 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
783 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
788 mutex_init(&vcpu
->mutex
);
792 init_waitqueue_head(&vcpu
->wq
);
794 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
799 vcpu
->run
= page_address(page
);
801 r
= kvm_arch_vcpu_init(vcpu
);
807 free_page((unsigned long)vcpu
->run
);
811 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
813 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
815 kvm_arch_vcpu_uninit(vcpu
);
816 free_page((unsigned long)vcpu
->run
);
818 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
820 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
821 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
823 return container_of(mn
, struct kvm
, mmu_notifier
);
826 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
827 struct mm_struct
*mm
,
828 unsigned long address
)
830 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
834 * When ->invalidate_page runs, the linux pte has been zapped
835 * already but the page is still allocated until
836 * ->invalidate_page returns. So if we increase the sequence
837 * here the kvm page fault will notice if the spte can't be
838 * established because the page is going to be freed. If
839 * instead the kvm page fault establishes the spte before
840 * ->invalidate_page runs, kvm_unmap_hva will release it
843 * The sequence increase only need to be seen at spin_unlock
844 * time, and not at spin_lock time.
846 * Increasing the sequence after the spin_unlock would be
847 * unsafe because the kvm page fault could then establish the
848 * pte after kvm_unmap_hva returned, without noticing the page
849 * is going to be freed.
851 spin_lock(&kvm
->mmu_lock
);
852 kvm
->mmu_notifier_seq
++;
853 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
854 spin_unlock(&kvm
->mmu_lock
);
856 /* we've to flush the tlb before the pages can be freed */
858 kvm_flush_remote_tlbs(kvm
);
862 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
863 struct mm_struct
*mm
,
867 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
868 int need_tlb_flush
= 0;
870 spin_lock(&kvm
->mmu_lock
);
872 * The count increase must become visible at unlock time as no
873 * spte can be established without taking the mmu_lock and
874 * count is also read inside the mmu_lock critical section.
876 kvm
->mmu_notifier_count
++;
877 for (; start
< end
; start
+= PAGE_SIZE
)
878 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
879 spin_unlock(&kvm
->mmu_lock
);
881 /* we've to flush the tlb before the pages can be freed */
883 kvm_flush_remote_tlbs(kvm
);
886 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
887 struct mm_struct
*mm
,
891 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
893 spin_lock(&kvm
->mmu_lock
);
895 * This sequence increase will notify the kvm page fault that
896 * the page that is going to be mapped in the spte could have
899 kvm
->mmu_notifier_seq
++;
901 * The above sequence increase must be visible before the
902 * below count decrease but both values are read by the kvm
903 * page fault under mmu_lock spinlock so we don't need to add
904 * a smb_wmb() here in between the two.
906 kvm
->mmu_notifier_count
--;
907 spin_unlock(&kvm
->mmu_lock
);
909 BUG_ON(kvm
->mmu_notifier_count
< 0);
912 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
913 struct mm_struct
*mm
,
914 unsigned long address
)
916 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
919 spin_lock(&kvm
->mmu_lock
);
920 young
= kvm_age_hva(kvm
, address
);
921 spin_unlock(&kvm
->mmu_lock
);
924 kvm_flush_remote_tlbs(kvm
);
929 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
930 struct mm_struct
*mm
)
932 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
933 kvm_arch_flush_shadow(kvm
);
936 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
937 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
938 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
939 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
940 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
941 .release
= kvm_mmu_notifier_release
,
943 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
945 static struct kvm
*kvm_create_vm(void)
947 struct kvm
*kvm
= kvm_arch_create_vm();
948 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
954 #ifdef CONFIG_HAVE_KVM_IRQCHIP
955 INIT_LIST_HEAD(&kvm
->irq_routing
);
956 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
959 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
960 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
963 return ERR_PTR(-ENOMEM
);
965 kvm
->coalesced_mmio_ring
=
966 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
969 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
972 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
973 err
= mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
975 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
984 kvm
->mm
= current
->mm
;
985 atomic_inc(&kvm
->mm
->mm_count
);
986 spin_lock_init(&kvm
->mmu_lock
);
987 spin_lock_init(&kvm
->requests_lock
);
988 kvm_io_bus_init(&kvm
->pio_bus
);
989 mutex_init(&kvm
->lock
);
990 kvm_io_bus_init(&kvm
->mmio_bus
);
991 init_rwsem(&kvm
->slots_lock
);
992 atomic_set(&kvm
->users_count
, 1);
993 spin_lock(&kvm_lock
);
994 list_add(&kvm
->vm_list
, &vm_list
);
995 spin_unlock(&kvm_lock
);
996 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
997 kvm_coalesced_mmio_init(kvm
);
1004 * Free any memory in @free but not in @dont.
1006 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
1007 struct kvm_memory_slot
*dont
)
1009 if (!dont
|| free
->rmap
!= dont
->rmap
)
1012 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
1013 vfree(free
->dirty_bitmap
);
1015 if (!dont
|| free
->lpage_info
!= dont
->lpage_info
)
1016 vfree(free
->lpage_info
);
1019 free
->dirty_bitmap
= NULL
;
1021 free
->lpage_info
= NULL
;
1024 void kvm_free_physmem(struct kvm
*kvm
)
1028 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
1029 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
1032 static void kvm_destroy_vm(struct kvm
*kvm
)
1034 struct mm_struct
*mm
= kvm
->mm
;
1036 kvm_arch_sync_events(kvm
);
1037 spin_lock(&kvm_lock
);
1038 list_del(&kvm
->vm_list
);
1039 spin_unlock(&kvm_lock
);
1040 kvm_free_irq_routing(kvm
);
1041 kvm_io_bus_destroy(&kvm
->pio_bus
);
1042 kvm_io_bus_destroy(&kvm
->mmio_bus
);
1043 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1044 if (kvm
->coalesced_mmio_ring
!= NULL
)
1045 free_page((unsigned long)kvm
->coalesced_mmio_ring
);
1047 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1048 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
1050 kvm_arch_flush_shadow(kvm
);
1052 kvm_arch_destroy_vm(kvm
);
1056 void kvm_get_kvm(struct kvm
*kvm
)
1058 atomic_inc(&kvm
->users_count
);
1060 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
1062 void kvm_put_kvm(struct kvm
*kvm
)
1064 if (atomic_dec_and_test(&kvm
->users_count
))
1065 kvm_destroy_vm(kvm
);
1067 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
1070 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
1072 struct kvm
*kvm
= filp
->private_data
;
1079 * Allocate some memory and give it an address in the guest physical address
1082 * Discontiguous memory is allowed, mostly for framebuffers.
1084 * Must be called holding mmap_sem for write.
1086 int __kvm_set_memory_region(struct kvm
*kvm
,
1087 struct kvm_userspace_memory_region
*mem
,
1092 unsigned long npages
, ugfn
;
1093 unsigned long largepages
, i
;
1094 struct kvm_memory_slot
*memslot
;
1095 struct kvm_memory_slot old
, new;
1098 /* General sanity checks */
1099 if (mem
->memory_size
& (PAGE_SIZE
- 1))
1101 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
1103 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
1105 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
1107 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
1110 memslot
= &kvm
->memslots
[mem
->slot
];
1111 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
1112 npages
= mem
->memory_size
>> PAGE_SHIFT
;
1115 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
1117 new = old
= *memslot
;
1119 new.base_gfn
= base_gfn
;
1120 new.npages
= npages
;
1121 new.flags
= mem
->flags
;
1123 /* Disallow changing a memory slot's size. */
1125 if (npages
&& old
.npages
&& npages
!= old
.npages
)
1128 /* Check for overlaps */
1130 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1131 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
1133 if (s
== memslot
|| !s
->npages
)
1135 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
1136 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
1140 /* Free page dirty bitmap if unneeded */
1141 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
1142 new.dirty_bitmap
= NULL
;
1146 /* Allocate if a slot is being created */
1148 if (npages
&& !new.rmap
) {
1149 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
1154 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
1156 new.user_alloc
= user_alloc
;
1158 * hva_to_rmmap() serialzies with the mmu_lock and to be
1159 * safe it has to ignore memslots with !user_alloc &&
1163 new.userspace_addr
= mem
->userspace_addr
;
1165 new.userspace_addr
= 0;
1167 if (npages
&& !new.lpage_info
) {
1168 largepages
= 1 + (base_gfn
+ npages
- 1) / KVM_PAGES_PER_HPAGE
;
1169 largepages
-= base_gfn
/ KVM_PAGES_PER_HPAGE
;
1171 new.lpage_info
= vmalloc(largepages
* sizeof(*new.lpage_info
));
1173 if (!new.lpage_info
)
1176 memset(new.lpage_info
, 0, largepages
* sizeof(*new.lpage_info
));
1178 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
1179 new.lpage_info
[0].write_count
= 1;
1180 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE
)
1181 new.lpage_info
[largepages
-1].write_count
= 1;
1182 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
1184 * If the gfn and userspace address are not aligned wrt each
1185 * other, disable large page support for this slot
1187 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE
- 1))
1188 for (i
= 0; i
< largepages
; ++i
)
1189 new.lpage_info
[i
].write_count
= 1;
1192 /* Allocate page dirty bitmap if needed */
1193 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
1194 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
1196 new.dirty_bitmap
= vmalloc(dirty_bytes
);
1197 if (!new.dirty_bitmap
)
1199 memset(new.dirty_bitmap
, 0, dirty_bytes
);
1201 kvm_arch_flush_shadow(kvm
);
1203 #endif /* not defined CONFIG_S390 */
1206 kvm_arch_flush_shadow(kvm
);
1208 spin_lock(&kvm
->mmu_lock
);
1209 if (mem
->slot
>= kvm
->nmemslots
)
1210 kvm
->nmemslots
= mem
->slot
+ 1;
1213 spin_unlock(&kvm
->mmu_lock
);
1215 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
1217 spin_lock(&kvm
->mmu_lock
);
1219 spin_unlock(&kvm
->mmu_lock
);
1223 kvm_free_physmem_slot(&old
, npages
? &new : NULL
);
1224 /* Slot deletion case: we have to update the current slot */
1225 spin_lock(&kvm
->mmu_lock
);
1228 spin_unlock(&kvm
->mmu_lock
);
1230 /* map the pages in iommu page table */
1231 r
= kvm_iommu_map_pages(kvm
, base_gfn
, npages
);
1238 kvm_free_physmem_slot(&new, &old
);
1243 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
1245 int kvm_set_memory_region(struct kvm
*kvm
,
1246 struct kvm_userspace_memory_region
*mem
,
1251 down_write(&kvm
->slots_lock
);
1252 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
1253 up_write(&kvm
->slots_lock
);
1256 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
1258 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
1260 kvm_userspace_memory_region
*mem
,
1263 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
1265 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
1268 int kvm_get_dirty_log(struct kvm
*kvm
,
1269 struct kvm_dirty_log
*log
, int *is_dirty
)
1271 struct kvm_memory_slot
*memslot
;
1274 unsigned long any
= 0;
1277 if (log
->slot
>= KVM_MEMORY_SLOTS
)
1280 memslot
= &kvm
->memslots
[log
->slot
];
1282 if (!memslot
->dirty_bitmap
)
1285 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1287 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
1288 any
= memslot
->dirty_bitmap
[i
];
1291 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
1302 int is_error_page(struct page
*page
)
1304 return page
== bad_page
;
1306 EXPORT_SYMBOL_GPL(is_error_page
);
1308 int is_error_pfn(pfn_t pfn
)
1310 return pfn
== bad_pfn
;
1312 EXPORT_SYMBOL_GPL(is_error_pfn
);
1314 static inline unsigned long bad_hva(void)
1319 int kvm_is_error_hva(unsigned long addr
)
1321 return addr
== bad_hva();
1323 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
1325 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
1329 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1330 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1332 if (gfn
>= memslot
->base_gfn
1333 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1338 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
1340 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1342 gfn
= unalias_gfn(kvm
, gfn
);
1343 return gfn_to_memslot_unaliased(kvm
, gfn
);
1346 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
1350 gfn
= unalias_gfn(kvm
, gfn
);
1351 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1352 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1354 if (gfn
>= memslot
->base_gfn
1355 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1360 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
1362 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1364 struct kvm_memory_slot
*slot
;
1366 gfn
= unalias_gfn(kvm
, gfn
);
1367 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1370 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
1372 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1374 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1376 struct page
*page
[1];
1383 addr
= gfn_to_hva(kvm
, gfn
);
1384 if (kvm_is_error_hva(addr
)) {
1386 return page_to_pfn(bad_page
);
1389 npages
= get_user_pages_fast(addr
, 1, 1, page
);
1391 if (unlikely(npages
!= 1)) {
1392 struct vm_area_struct
*vma
;
1394 down_read(¤t
->mm
->mmap_sem
);
1395 vma
= find_vma(current
->mm
, addr
);
1397 if (vma
== NULL
|| addr
< vma
->vm_start
||
1398 !(vma
->vm_flags
& VM_PFNMAP
)) {
1399 up_read(¤t
->mm
->mmap_sem
);
1401 return page_to_pfn(bad_page
);
1404 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1405 up_read(¤t
->mm
->mmap_sem
);
1406 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1408 pfn
= page_to_pfn(page
[0]);
1413 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1415 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1419 pfn
= gfn_to_pfn(kvm
, gfn
);
1420 if (!kvm_is_mmio_pfn(pfn
))
1421 return pfn_to_page(pfn
);
1423 WARN_ON(kvm_is_mmio_pfn(pfn
));
1429 EXPORT_SYMBOL_GPL(gfn_to_page
);
1431 void kvm_release_page_clean(struct page
*page
)
1433 kvm_release_pfn_clean(page_to_pfn(page
));
1435 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1437 void kvm_release_pfn_clean(pfn_t pfn
)
1439 if (!kvm_is_mmio_pfn(pfn
))
1440 put_page(pfn_to_page(pfn
));
1442 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1444 void kvm_release_page_dirty(struct page
*page
)
1446 kvm_release_pfn_dirty(page_to_pfn(page
));
1448 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1450 void kvm_release_pfn_dirty(pfn_t pfn
)
1452 kvm_set_pfn_dirty(pfn
);
1453 kvm_release_pfn_clean(pfn
);
1455 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1457 void kvm_set_page_dirty(struct page
*page
)
1459 kvm_set_pfn_dirty(page_to_pfn(page
));
1461 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1463 void kvm_set_pfn_dirty(pfn_t pfn
)
1465 if (!kvm_is_mmio_pfn(pfn
)) {
1466 struct page
*page
= pfn_to_page(pfn
);
1467 if (!PageReserved(page
))
1471 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1473 void kvm_set_pfn_accessed(pfn_t pfn
)
1475 if (!kvm_is_mmio_pfn(pfn
))
1476 mark_page_accessed(pfn_to_page(pfn
));
1478 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1480 void kvm_get_pfn(pfn_t pfn
)
1482 if (!kvm_is_mmio_pfn(pfn
))
1483 get_page(pfn_to_page(pfn
));
1485 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1487 static int next_segment(unsigned long len
, int offset
)
1489 if (len
> PAGE_SIZE
- offset
)
1490 return PAGE_SIZE
- offset
;
1495 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1501 addr
= gfn_to_hva(kvm
, gfn
);
1502 if (kvm_is_error_hva(addr
))
1504 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1509 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1511 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1513 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1515 int offset
= offset_in_page(gpa
);
1518 while ((seg
= next_segment(len
, offset
)) != 0) {
1519 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1529 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1531 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1536 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1537 int offset
= offset_in_page(gpa
);
1539 addr
= gfn_to_hva(kvm
, gfn
);
1540 if (kvm_is_error_hva(addr
))
1542 pagefault_disable();
1543 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1549 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1551 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1552 int offset
, int len
)
1557 addr
= gfn_to_hva(kvm
, gfn
);
1558 if (kvm_is_error_hva(addr
))
1560 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1563 mark_page_dirty(kvm
, gfn
);
1566 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1568 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1571 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1573 int offset
= offset_in_page(gpa
);
1576 while ((seg
= next_segment(len
, offset
)) != 0) {
1577 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1588 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1590 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1592 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1594 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1596 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1598 int offset
= offset_in_page(gpa
);
1601 while ((seg
= next_segment(len
, offset
)) != 0) {
1602 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1611 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1613 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1615 struct kvm_memory_slot
*memslot
;
1617 gfn
= unalias_gfn(kvm
, gfn
);
1618 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1619 if (memslot
&& memslot
->dirty_bitmap
) {
1620 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1623 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1624 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1629 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1631 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1636 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1638 if ((kvm_arch_interrupt_allowed(vcpu
) &&
1639 kvm_cpu_has_interrupt(vcpu
)) ||
1640 kvm_arch_vcpu_runnable(vcpu
)) {
1641 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1644 if (kvm_cpu_has_pending_timer(vcpu
))
1646 if (signal_pending(current
))
1654 finish_wait(&vcpu
->wq
, &wait
);
1657 void kvm_resched(struct kvm_vcpu
*vcpu
)
1659 if (!need_resched())
1663 EXPORT_SYMBOL_GPL(kvm_resched
);
1665 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1667 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1670 if (vmf
->pgoff
== 0)
1671 page
= virt_to_page(vcpu
->run
);
1673 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1674 page
= virt_to_page(vcpu
->arch
.pio_data
);
1676 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1677 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1678 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1681 return VM_FAULT_SIGBUS
;
1687 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
1688 .fault
= kvm_vcpu_fault
,
1691 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1693 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1697 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1699 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1701 kvm_put_kvm(vcpu
->kvm
);
1705 static struct file_operations kvm_vcpu_fops
= {
1706 .release
= kvm_vcpu_release
,
1707 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1708 .compat_ioctl
= kvm_vcpu_ioctl
,
1709 .mmap
= kvm_vcpu_mmap
,
1713 * Allocates an inode for the vcpu.
1715 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1717 int fd
= anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, 0);
1719 kvm_put_kvm(vcpu
->kvm
);
1724 * Creates some virtual cpus. Good luck creating more than one.
1726 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
1729 struct kvm_vcpu
*vcpu
;
1734 vcpu
= kvm_arch_vcpu_create(kvm
, n
);
1736 return PTR_ERR(vcpu
);
1738 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1740 r
= kvm_arch_vcpu_setup(vcpu
);
1744 mutex_lock(&kvm
->lock
);
1745 if (kvm
->vcpus
[n
]) {
1749 kvm
->vcpus
[n
] = vcpu
;
1750 mutex_unlock(&kvm
->lock
);
1752 /* Now it's all set up, let userspace reach it */
1754 r
= create_vcpu_fd(vcpu
);
1760 mutex_lock(&kvm
->lock
);
1761 kvm
->vcpus
[n
] = NULL
;
1763 mutex_unlock(&kvm
->lock
);
1764 kvm_arch_vcpu_destroy(vcpu
);
1768 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1771 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1772 vcpu
->sigset_active
= 1;
1773 vcpu
->sigset
= *sigset
;
1775 vcpu
->sigset_active
= 0;
1779 #ifdef __KVM_HAVE_MSIX
1780 static int kvm_vm_ioctl_set_msix_nr(struct kvm
*kvm
,
1781 struct kvm_assigned_msix_nr
*entry_nr
)
1784 struct kvm_assigned_dev_kernel
*adev
;
1786 mutex_lock(&kvm
->lock
);
1788 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
1789 entry_nr
->assigned_dev_id
);
1795 if (adev
->entries_nr
== 0) {
1796 adev
->entries_nr
= entry_nr
->entry_nr
;
1797 if (adev
->entries_nr
== 0 ||
1798 adev
->entries_nr
>= KVM_MAX_MSIX_PER_DEV
) {
1803 adev
->host_msix_entries
= kzalloc(sizeof(struct msix_entry
) *
1806 if (!adev
->host_msix_entries
) {
1810 adev
->guest_msix_entries
= kzalloc(
1811 sizeof(struct kvm_guest_msix_entry
) *
1812 entry_nr
->entry_nr
, GFP_KERNEL
);
1813 if (!adev
->guest_msix_entries
) {
1814 kfree(adev
->host_msix_entries
);
1818 } else /* Not allowed set MSI-X number twice */
1821 mutex_unlock(&kvm
->lock
);
1825 static int kvm_vm_ioctl_set_msix_entry(struct kvm
*kvm
,
1826 struct kvm_assigned_msix_entry
*entry
)
1829 struct kvm_assigned_dev_kernel
*adev
;
1831 mutex_lock(&kvm
->lock
);
1833 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
1834 entry
->assigned_dev_id
);
1838 goto msix_entry_out
;
1841 for (i
= 0; i
< adev
->entries_nr
; i
++)
1842 if (adev
->guest_msix_entries
[i
].vector
== 0 ||
1843 adev
->guest_msix_entries
[i
].entry
== entry
->entry
) {
1844 adev
->guest_msix_entries
[i
].entry
= entry
->entry
;
1845 adev
->guest_msix_entries
[i
].vector
= entry
->gsi
;
1846 adev
->host_msix_entries
[i
].entry
= entry
->entry
;
1849 if (i
== adev
->entries_nr
) {
1851 goto msix_entry_out
;
1855 mutex_unlock(&kvm
->lock
);
1861 static long kvm_vcpu_ioctl(struct file
*filp
,
1862 unsigned int ioctl
, unsigned long arg
)
1864 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1865 void __user
*argp
= (void __user
*)arg
;
1867 struct kvm_fpu
*fpu
= NULL
;
1868 struct kvm_sregs
*kvm_sregs
= NULL
;
1870 if (vcpu
->kvm
->mm
!= current
->mm
)
1877 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1879 case KVM_GET_REGS
: {
1880 struct kvm_regs
*kvm_regs
;
1883 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1886 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1890 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1897 case KVM_SET_REGS
: {
1898 struct kvm_regs
*kvm_regs
;
1901 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1905 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1907 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1915 case KVM_GET_SREGS
: {
1916 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1920 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1924 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1929 case KVM_SET_SREGS
: {
1930 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1935 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1937 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1943 case KVM_GET_MP_STATE
: {
1944 struct kvm_mp_state mp_state
;
1946 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1950 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1955 case KVM_SET_MP_STATE
: {
1956 struct kvm_mp_state mp_state
;
1959 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1961 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1967 case KVM_TRANSLATE
: {
1968 struct kvm_translation tr
;
1971 if (copy_from_user(&tr
, argp
, sizeof tr
))
1973 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1977 if (copy_to_user(argp
, &tr
, sizeof tr
))
1982 case KVM_SET_GUEST_DEBUG
: {
1983 struct kvm_guest_debug dbg
;
1986 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1988 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1994 case KVM_SET_SIGNAL_MASK
: {
1995 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1996 struct kvm_signal_mask kvm_sigmask
;
1997 sigset_t sigset
, *p
;
2002 if (copy_from_user(&kvm_sigmask
, argp
,
2003 sizeof kvm_sigmask
))
2006 if (kvm_sigmask
.len
!= sizeof sigset
)
2009 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
2014 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
2018 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
2022 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
2026 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
2032 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
2037 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
2039 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
2046 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
2054 static long kvm_vm_ioctl(struct file
*filp
,
2055 unsigned int ioctl
, unsigned long arg
)
2057 struct kvm
*kvm
= filp
->private_data
;
2058 void __user
*argp
= (void __user
*)arg
;
2061 if (kvm
->mm
!= current
->mm
)
2064 case KVM_CREATE_VCPU
:
2065 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2069 case KVM_SET_USER_MEMORY_REGION
: {
2070 struct kvm_userspace_memory_region kvm_userspace_mem
;
2073 if (copy_from_user(&kvm_userspace_mem
, argp
,
2074 sizeof kvm_userspace_mem
))
2077 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
2082 case KVM_GET_DIRTY_LOG
: {
2083 struct kvm_dirty_log log
;
2086 if (copy_from_user(&log
, argp
, sizeof log
))
2088 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2093 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2094 case KVM_REGISTER_COALESCED_MMIO
: {
2095 struct kvm_coalesced_mmio_zone zone
;
2097 if (copy_from_user(&zone
, argp
, sizeof zone
))
2100 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
2106 case KVM_UNREGISTER_COALESCED_MMIO
: {
2107 struct kvm_coalesced_mmio_zone zone
;
2109 if (copy_from_user(&zone
, argp
, sizeof zone
))
2112 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
2119 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
2120 case KVM_ASSIGN_PCI_DEVICE
: {
2121 struct kvm_assigned_pci_dev assigned_dev
;
2124 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
2126 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
2131 case KVM_ASSIGN_IRQ
: {
2135 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
2136 case KVM_ASSIGN_DEV_IRQ
: {
2137 struct kvm_assigned_irq assigned_irq
;
2140 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
2142 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
2147 case KVM_DEASSIGN_DEV_IRQ
: {
2148 struct kvm_assigned_irq assigned_irq
;
2151 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
2153 r
= kvm_vm_ioctl_deassign_dev_irq(kvm
, &assigned_irq
);
2160 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2161 case KVM_DEASSIGN_PCI_DEVICE
: {
2162 struct kvm_assigned_pci_dev assigned_dev
;
2165 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
2167 r
= kvm_vm_ioctl_deassign_device(kvm
, &assigned_dev
);
2173 #ifdef KVM_CAP_IRQ_ROUTING
2174 case KVM_SET_GSI_ROUTING
: {
2175 struct kvm_irq_routing routing
;
2176 struct kvm_irq_routing __user
*urouting
;
2177 struct kvm_irq_routing_entry
*entries
;
2180 if (copy_from_user(&routing
, argp
, sizeof(routing
)))
2183 if (routing
.nr
>= KVM_MAX_IRQ_ROUTES
)
2188 entries
= vmalloc(routing
.nr
* sizeof(*entries
));
2193 if (copy_from_user(entries
, urouting
->entries
,
2194 routing
.nr
* sizeof(*entries
)))
2195 goto out_free_irq_routing
;
2196 r
= kvm_set_irq_routing(kvm
, entries
, routing
.nr
,
2198 out_free_irq_routing
:
2202 #ifdef __KVM_HAVE_MSIX
2203 case KVM_ASSIGN_SET_MSIX_NR
: {
2204 struct kvm_assigned_msix_nr entry_nr
;
2206 if (copy_from_user(&entry_nr
, argp
, sizeof entry_nr
))
2208 r
= kvm_vm_ioctl_set_msix_nr(kvm
, &entry_nr
);
2213 case KVM_ASSIGN_SET_MSIX_ENTRY
: {
2214 struct kvm_assigned_msix_entry entry
;
2216 if (copy_from_user(&entry
, argp
, sizeof entry
))
2218 r
= kvm_vm_ioctl_set_msix_entry(kvm
, &entry
);
2224 #endif /* KVM_CAP_IRQ_ROUTING */
2226 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
2232 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2234 struct page
*page
[1];
2237 gfn_t gfn
= vmf
->pgoff
;
2238 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2240 addr
= gfn_to_hva(kvm
, gfn
);
2241 if (kvm_is_error_hva(addr
))
2242 return VM_FAULT_SIGBUS
;
2244 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
2246 if (unlikely(npages
!= 1))
2247 return VM_FAULT_SIGBUS
;
2249 vmf
->page
= page
[0];
2253 static struct vm_operations_struct kvm_vm_vm_ops
= {
2254 .fault
= kvm_vm_fault
,
2257 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2259 vma
->vm_ops
= &kvm_vm_vm_ops
;
2263 static struct file_operations kvm_vm_fops
= {
2264 .release
= kvm_vm_release
,
2265 .unlocked_ioctl
= kvm_vm_ioctl
,
2266 .compat_ioctl
= kvm_vm_ioctl
,
2267 .mmap
= kvm_vm_mmap
,
2270 static int kvm_dev_ioctl_create_vm(void)
2275 kvm
= kvm_create_vm();
2277 return PTR_ERR(kvm
);
2278 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, 0);
2285 static long kvm_dev_ioctl_check_extension_generic(long arg
)
2288 case KVM_CAP_USER_MEMORY
:
2289 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
2290 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
2292 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2293 case KVM_CAP_IRQ_ROUTING
:
2294 return KVM_MAX_IRQ_ROUTES
;
2299 return kvm_dev_ioctl_check_extension(arg
);
2302 static long kvm_dev_ioctl(struct file
*filp
,
2303 unsigned int ioctl
, unsigned long arg
)
2308 case KVM_GET_API_VERSION
:
2312 r
= KVM_API_VERSION
;
2318 r
= kvm_dev_ioctl_create_vm();
2320 case KVM_CHECK_EXTENSION
:
2321 r
= kvm_dev_ioctl_check_extension_generic(arg
);
2323 case KVM_GET_VCPU_MMAP_SIZE
:
2327 r
= PAGE_SIZE
; /* struct kvm_run */
2329 r
+= PAGE_SIZE
; /* pio data page */
2331 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2332 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
2335 case KVM_TRACE_ENABLE
:
2336 case KVM_TRACE_PAUSE
:
2337 case KVM_TRACE_DISABLE
:
2338 r
= kvm_trace_ioctl(ioctl
, arg
);
2341 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
2347 static struct file_operations kvm_chardev_ops
= {
2348 .unlocked_ioctl
= kvm_dev_ioctl
,
2349 .compat_ioctl
= kvm_dev_ioctl
,
2352 static struct miscdevice kvm_dev
= {
2358 static void hardware_enable(void *junk
)
2360 int cpu
= raw_smp_processor_id();
2362 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2364 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
2365 kvm_arch_hardware_enable(NULL
);
2368 static void hardware_disable(void *junk
)
2370 int cpu
= raw_smp_processor_id();
2372 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2374 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2375 kvm_arch_hardware_disable(NULL
);
2378 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2383 val
&= ~CPU_TASKS_FROZEN
;
2386 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2388 hardware_disable(NULL
);
2390 case CPU_UP_CANCELED
:
2391 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2393 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
2396 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2398 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
2405 asmlinkage
void kvm_handle_fault_on_reboot(void)
2408 /* spin while reset goes on */
2411 /* Fault while not rebooting. We want the trace. */
2414 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
2416 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2420 * Some (well, at least mine) BIOSes hang on reboot if
2423 * And Intel TXT required VMX off for all cpu when system shutdown.
2425 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2426 kvm_rebooting
= true;
2427 on_each_cpu(hardware_disable
, NULL
, 1);
2431 static struct notifier_block kvm_reboot_notifier
= {
2432 .notifier_call
= kvm_reboot
,
2436 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
2438 memset(bus
, 0, sizeof(*bus
));
2441 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2445 for (i
= 0; i
< bus
->dev_count
; i
++) {
2446 struct kvm_io_device
*pos
= bus
->devs
[i
];
2448 kvm_iodevice_destructor(pos
);
2452 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
,
2453 gpa_t addr
, int len
, int is_write
)
2457 for (i
= 0; i
< bus
->dev_count
; i
++) {
2458 struct kvm_io_device
*pos
= bus
->devs
[i
];
2460 if (pos
->in_range(pos
, addr
, len
, is_write
))
2467 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
2469 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
2471 bus
->devs
[bus
->dev_count
++] = dev
;
2474 static struct notifier_block kvm_cpu_notifier
= {
2475 .notifier_call
= kvm_cpu_hotplug
,
2476 .priority
= 20, /* must be > scheduler priority */
2479 static int vm_stat_get(void *_offset
, u64
*val
)
2481 unsigned offset
= (long)_offset
;
2485 spin_lock(&kvm_lock
);
2486 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2487 *val
+= *(u32
*)((void *)kvm
+ offset
);
2488 spin_unlock(&kvm_lock
);
2492 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2494 static int vcpu_stat_get(void *_offset
, u64
*val
)
2496 unsigned offset
= (long)_offset
;
2498 struct kvm_vcpu
*vcpu
;
2502 spin_lock(&kvm_lock
);
2503 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2504 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2505 vcpu
= kvm
->vcpus
[i
];
2507 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2509 spin_unlock(&kvm_lock
);
2513 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2515 static struct file_operations
*stat_fops
[] = {
2516 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2517 [KVM_STAT_VM
] = &vm_stat_fops
,
2520 static void kvm_init_debug(void)
2522 struct kvm_stats_debugfs_item
*p
;
2524 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2525 for (p
= debugfs_entries
; p
->name
; ++p
)
2526 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2527 (void *)(long)p
->offset
,
2528 stat_fops
[p
->kind
]);
2531 static void kvm_exit_debug(void)
2533 struct kvm_stats_debugfs_item
*p
;
2535 for (p
= debugfs_entries
; p
->name
; ++p
)
2536 debugfs_remove(p
->dentry
);
2537 debugfs_remove(kvm_debugfs_dir
);
2540 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2542 hardware_disable(NULL
);
2546 static int kvm_resume(struct sys_device
*dev
)
2548 hardware_enable(NULL
);
2552 static struct sysdev_class kvm_sysdev_class
= {
2554 .suspend
= kvm_suspend
,
2555 .resume
= kvm_resume
,
2558 static struct sys_device kvm_sysdev
= {
2560 .cls
= &kvm_sysdev_class
,
2563 struct page
*bad_page
;
2567 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2569 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2572 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2574 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2576 kvm_arch_vcpu_load(vcpu
, cpu
);
2579 static void kvm_sched_out(struct preempt_notifier
*pn
,
2580 struct task_struct
*next
)
2582 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2584 kvm_arch_vcpu_put(vcpu
);
2587 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2588 struct module
*module
)
2593 r
= kvm_arch_init(opaque
);
2597 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2599 if (bad_page
== NULL
) {
2604 bad_pfn
= page_to_pfn(bad_page
);
2606 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2611 r
= kvm_arch_hardware_setup();
2615 for_each_online_cpu(cpu
) {
2616 smp_call_function_single(cpu
,
2617 kvm_arch_check_processor_compat
,
2623 on_each_cpu(hardware_enable
, NULL
, 1);
2624 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2627 register_reboot_notifier(&kvm_reboot_notifier
);
2629 r
= sysdev_class_register(&kvm_sysdev_class
);
2633 r
= sysdev_register(&kvm_sysdev
);
2637 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2638 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2639 __alignof__(struct kvm_vcpu
),
2641 if (!kvm_vcpu_cache
) {
2646 kvm_chardev_ops
.owner
= module
;
2647 kvm_vm_fops
.owner
= module
;
2648 kvm_vcpu_fops
.owner
= module
;
2650 r
= misc_register(&kvm_dev
);
2652 printk(KERN_ERR
"kvm: misc device register failed\n");
2656 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2657 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2664 kmem_cache_destroy(kvm_vcpu_cache
);
2666 sysdev_unregister(&kvm_sysdev
);
2668 sysdev_class_unregister(&kvm_sysdev_class
);
2670 unregister_reboot_notifier(&kvm_reboot_notifier
);
2671 unregister_cpu_notifier(&kvm_cpu_notifier
);
2673 on_each_cpu(hardware_disable
, NULL
, 1);
2675 kvm_arch_hardware_unsetup();
2677 free_cpumask_var(cpus_hardware_enabled
);
2679 __free_page(bad_page
);
2685 EXPORT_SYMBOL_GPL(kvm_init
);
2689 kvm_trace_cleanup();
2691 misc_deregister(&kvm_dev
);
2692 kmem_cache_destroy(kvm_vcpu_cache
);
2693 sysdev_unregister(&kvm_sysdev
);
2694 sysdev_class_unregister(&kvm_sysdev_class
);
2695 unregister_reboot_notifier(&kvm_reboot_notifier
);
2696 unregister_cpu_notifier(&kvm_cpu_notifier
);
2697 on_each_cpu(hardware_disable
, NULL
, 1);
2698 kvm_arch_hardware_unsetup();
2700 free_cpumask_var(cpus_hardware_enabled
);
2701 __free_page(bad_page
);
2703 EXPORT_SYMBOL_GPL(kvm_exit
);