2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/percpu.h>
25 #include <linux/gfp.h>
27 #include <linux/miscdevice.h>
28 #include <linux/vmalloc.h>
29 #include <linux/reboot.h>
30 #include <linux/debugfs.h>
31 #include <linux/highmem.h>
32 #include <linux/file.h>
33 #include <linux/sysdev.h>
34 #include <linux/cpu.h>
35 #include <linux/sched.h>
36 #include <linux/cpumask.h>
37 #include <linux/smp.h>
38 #include <linux/anon_inodes.h>
39 #include <linux/profile.h>
40 #include <linux/kvm_para.h>
41 #include <linux/pagemap.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
45 #include <asm/processor.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
51 #include <asm/msidef.h>
54 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
55 #include "coalesced_mmio.h"
58 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
59 #include <linux/pci.h>
60 #include <linux/interrupt.h>
64 MODULE_AUTHOR("Qumranet");
65 MODULE_LICENSE("GPL");
67 static int msi2intx
= 1;
68 module_param(msi2intx
, bool, 0);
70 DEFINE_SPINLOCK(kvm_lock
);
73 static cpumask_t cpus_hardware_enabled
;
75 struct kmem_cache
*kvm_vcpu_cache
;
76 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
78 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
80 struct dentry
*kvm_debugfs_dir
;
82 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
85 static bool kvm_rebooting
;
87 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
90 static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel
*dev
)
93 struct kvm_vcpu
*vcpu
;
94 struct kvm_ioapic
*ioapic
= ioapic_irqchip(dev
->kvm
);
95 int dest_id
= (dev
->guest_msi
.address_lo
& MSI_ADDR_DEST_ID_MASK
)
96 >> MSI_ADDR_DEST_ID_SHIFT
;
97 int vector
= (dev
->guest_msi
.data
& MSI_DATA_VECTOR_MASK
)
98 >> MSI_DATA_VECTOR_SHIFT
;
99 int dest_mode
= test_bit(MSI_ADDR_DEST_MODE_SHIFT
,
100 (unsigned long *)&dev
->guest_msi
.address_lo
);
101 int trig_mode
= test_bit(MSI_DATA_TRIGGER_SHIFT
,
102 (unsigned long *)&dev
->guest_msi
.data
);
103 int delivery_mode
= test_bit(MSI_DATA_DELIVERY_MODE_SHIFT
,
104 (unsigned long *)&dev
->guest_msi
.data
);
109 deliver_bitmask
= kvm_ioapic_get_delivery_bitmask(ioapic
,
111 /* IOAPIC delivery mode value is the same as MSI here */
112 switch (delivery_mode
) {
113 case IOAPIC_LOWEST_PRIORITY
:
114 vcpu
= kvm_get_lowest_prio_vcpu(ioapic
->kvm
, vector
,
117 kvm_apic_set_irq(vcpu
, vector
, trig_mode
);
119 printk(KERN_INFO
"kvm: null lowest priority vcpu!\n");
122 for (vcpu_id
= 0; deliver_bitmask
!= 0; vcpu_id
++) {
123 if (!(deliver_bitmask
& (1 << vcpu_id
)))
125 deliver_bitmask
&= ~(1 << vcpu_id
);
126 vcpu
= ioapic
->kvm
->vcpus
[vcpu_id
];
128 kvm_apic_set_irq(vcpu
, vector
, trig_mode
);
132 printk(KERN_INFO
"kvm: unsupported MSI delivery mode\n");
136 static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel
*dev
) {}
139 static struct kvm_assigned_dev_kernel
*kvm_find_assigned_dev(struct list_head
*head
,
142 struct list_head
*ptr
;
143 struct kvm_assigned_dev_kernel
*match
;
145 list_for_each(ptr
, head
) {
146 match
= list_entry(ptr
, struct kvm_assigned_dev_kernel
, list
);
147 if (match
->assigned_dev_id
== assigned_dev_id
)
153 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct
*work
)
155 struct kvm_assigned_dev_kernel
*assigned_dev
;
157 assigned_dev
= container_of(work
, struct kvm_assigned_dev_kernel
,
160 /* This is taken to safely inject irq inside the guest. When
161 * the interrupt injection (or the ioapic code) uses a
162 * finer-grained lock, update this
164 mutex_lock(&assigned_dev
->kvm
->lock
);
165 if (assigned_dev
->irq_requested_type
& KVM_ASSIGNED_DEV_GUEST_INTX
)
166 kvm_set_irq(assigned_dev
->kvm
,
167 assigned_dev
->irq_source_id
,
168 assigned_dev
->guest_irq
, 1);
169 else if (assigned_dev
->irq_requested_type
&
170 KVM_ASSIGNED_DEV_GUEST_MSI
) {
171 assigned_device_msi_dispatch(assigned_dev
);
172 enable_irq(assigned_dev
->host_irq
);
174 mutex_unlock(&assigned_dev
->kvm
->lock
);
175 kvm_put_kvm(assigned_dev
->kvm
);
178 static irqreturn_t
kvm_assigned_dev_intr(int irq
, void *dev_id
)
180 struct kvm_assigned_dev_kernel
*assigned_dev
=
181 (struct kvm_assigned_dev_kernel
*) dev_id
;
183 kvm_get_kvm(assigned_dev
->kvm
);
184 schedule_work(&assigned_dev
->interrupt_work
);
185 disable_irq_nosync(irq
);
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
;
197 dev
= container_of(kian
, struct kvm_assigned_dev_kernel
,
199 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
200 enable_irq(dev
->host_irq
);
203 static void kvm_free_assigned_irq(struct kvm
*kvm
,
204 struct kvm_assigned_dev_kernel
*assigned_dev
)
206 if (!irqchip_in_kernel(kvm
))
209 kvm_unregister_irq_ack_notifier(&assigned_dev
->ack_notifier
);
211 if (assigned_dev
->irq_source_id
!= -1)
212 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
213 assigned_dev
->irq_source_id
= -1;
215 if (!assigned_dev
->irq_requested_type
)
218 if (cancel_work_sync(&assigned_dev
->interrupt_work
))
219 /* We had pending work. That means we will have to take
220 * care of kvm_put_kvm.
224 free_irq(assigned_dev
->host_irq
, (void *)assigned_dev
);
226 if (assigned_dev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
)
227 pci_disable_msi(assigned_dev
->dev
);
229 assigned_dev
->irq_requested_type
= 0;
233 static void kvm_free_assigned_device(struct kvm
*kvm
,
234 struct kvm_assigned_dev_kernel
237 kvm_free_assigned_irq(kvm
, assigned_dev
);
239 pci_reset_function(assigned_dev
->dev
);
241 pci_release_regions(assigned_dev
->dev
);
242 pci_disable_device(assigned_dev
->dev
);
243 pci_dev_put(assigned_dev
->dev
);
245 list_del(&assigned_dev
->list
);
249 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
251 struct list_head
*ptr
, *ptr2
;
252 struct kvm_assigned_dev_kernel
*assigned_dev
;
254 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
255 assigned_dev
= list_entry(ptr
,
256 struct kvm_assigned_dev_kernel
,
259 kvm_free_assigned_device(kvm
, assigned_dev
);
263 static int assigned_device_update_intx(struct kvm
*kvm
,
264 struct kvm_assigned_dev_kernel
*adev
,
265 struct kvm_assigned_irq
*airq
)
267 adev
->guest_irq
= airq
->guest_irq
;
268 adev
->ack_notifier
.gsi
= airq
->guest_irq
;
270 if (adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_INTX
)
273 if (irqchip_in_kernel(kvm
)) {
275 adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
) {
276 free_irq(adev
->host_irq
, (void *)kvm
);
277 pci_disable_msi(adev
->dev
);
280 if (!capable(CAP_SYS_RAWIO
))
284 adev
->host_irq
= airq
->host_irq
;
286 adev
->host_irq
= adev
->dev
->irq
;
288 /* Even though this is PCI, we don't want to use shared
289 * interrupts. Sharing host devices with guest-assigned devices
290 * on the same interrupt line is not a happy situation: there
291 * are going to be long delays in accepting, acking, etc.
293 if (request_irq(adev
->host_irq
, kvm_assigned_dev_intr
,
294 0, "kvm_assigned_intx_device", (void *)adev
))
298 adev
->irq_requested_type
= KVM_ASSIGNED_DEV_GUEST_INTX
|
299 KVM_ASSIGNED_DEV_HOST_INTX
;
304 static int assigned_device_update_msi(struct kvm
*kvm
,
305 struct kvm_assigned_dev_kernel
*adev
,
306 struct kvm_assigned_irq
*airq
)
310 if (airq
->flags
& KVM_DEV_IRQ_ASSIGN_ENABLE_MSI
) {
311 /* x86 don't care upper address of guest msi message addr */
312 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_GUEST_MSI
;
313 adev
->irq_requested_type
&= ~KVM_ASSIGNED_DEV_GUEST_INTX
;
314 adev
->guest_msi
.address_lo
= airq
->guest_msi
.addr_lo
;
315 adev
->guest_msi
.data
= airq
->guest_msi
.data
;
316 adev
->ack_notifier
.gsi
= -1;
317 } else if (msi2intx
) {
318 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_GUEST_INTX
;
319 adev
->irq_requested_type
&= ~KVM_ASSIGNED_DEV_GUEST_MSI
;
320 adev
->guest_irq
= airq
->guest_irq
;
321 adev
->ack_notifier
.gsi
= airq
->guest_irq
;
324 if (adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
)
327 if (irqchip_in_kernel(kvm
)) {
329 if (adev
->irq_requested_type
&
330 KVM_ASSIGNED_DEV_HOST_INTX
)
331 free_irq(adev
->host_irq
, (void *)adev
);
333 r
= pci_enable_msi(adev
->dev
);
338 adev
->host_irq
= adev
->dev
->irq
;
339 if (request_irq(adev
->host_irq
, kvm_assigned_dev_intr
, 0,
340 "kvm_assigned_msi_device", (void *)adev
))
345 adev
->irq_requested_type
= KVM_ASSIGNED_DEV_GUEST_MSI
;
347 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_HOST_MSI
;
352 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
353 struct kvm_assigned_irq
357 struct kvm_assigned_dev_kernel
*match
;
359 mutex_lock(&kvm
->lock
);
361 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
362 assigned_irq
->assigned_dev_id
);
364 mutex_unlock(&kvm
->lock
);
368 if (!match
->irq_requested_type
) {
369 INIT_WORK(&match
->interrupt_work
,
370 kvm_assigned_dev_interrupt_work_handler
);
371 if (irqchip_in_kernel(kvm
)) {
372 /* Register ack nofitier */
373 match
->ack_notifier
.gsi
= -1;
374 match
->ack_notifier
.irq_acked
=
375 kvm_assigned_dev_ack_irq
;
376 kvm_register_irq_ack_notifier(kvm
,
377 &match
->ack_notifier
);
379 /* Request IRQ source ID */
380 r
= kvm_request_irq_source_id(kvm
);
384 match
->irq_source_id
= r
;
387 /* Determine host device irq type, we can know the
388 * result from dev->msi_enabled */
390 pci_enable_msi(match
->dev
);
396 (assigned_irq
->flags
& KVM_DEV_IRQ_ASSIGN_ENABLE_MSI
)) ||
397 (msi2intx
&& match
->dev
->msi_enabled
)) {
399 r
= assigned_device_update_msi(kvm
, match
, assigned_irq
);
401 printk(KERN_WARNING
"kvm: failed to enable "
408 } else if (assigned_irq
->host_irq
== 0 && match
->dev
->irq
== 0) {
409 /* Host device IRQ 0 means don't support INTx */
412 "kvm: wait device to enable MSI!\n");
416 "kvm: failed to enable MSI device!\n");
421 /* Non-sharing INTx mode */
422 r
= assigned_device_update_intx(kvm
, match
, assigned_irq
);
424 printk(KERN_WARNING
"kvm: failed to enable "
430 mutex_unlock(&kvm
->lock
);
433 mutex_unlock(&kvm
->lock
);
434 kvm_free_assigned_device(kvm
, match
);
438 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
439 struct kvm_assigned_pci_dev
*assigned_dev
)
442 struct kvm_assigned_dev_kernel
*match
;
445 mutex_lock(&kvm
->lock
);
447 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
448 assigned_dev
->assigned_dev_id
);
450 /* device already assigned */
455 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
457 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
462 dev
= pci_get_bus_and_slot(assigned_dev
->busnr
,
463 assigned_dev
->devfn
);
465 printk(KERN_INFO
"%s: host device not found\n", __func__
);
469 if (pci_enable_device(dev
)) {
470 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
474 r
= pci_request_regions(dev
, "kvm_assigned_device");
476 printk(KERN_INFO
"%s: Could not get access to device regions\n",
481 pci_reset_function(dev
);
483 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
484 match
->host_busnr
= assigned_dev
->busnr
;
485 match
->host_devfn
= assigned_dev
->devfn
;
487 match
->irq_source_id
= -1;
490 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
492 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
) {
493 r
= kvm_iommu_map_guest(kvm
, match
);
499 mutex_unlock(&kvm
->lock
);
502 list_del(&match
->list
);
503 pci_release_regions(dev
);
505 pci_disable_device(dev
);
510 mutex_unlock(&kvm
->lock
);
515 static inline int valid_vcpu(int n
)
517 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
520 inline int kvm_is_mmio_pfn(pfn_t pfn
)
523 return PageReserved(pfn_to_page(pfn
));
529 * Switches to specified vcpu, until a matching vcpu_put()
531 void vcpu_load(struct kvm_vcpu
*vcpu
)
535 mutex_lock(&vcpu
->mutex
);
537 preempt_notifier_register(&vcpu
->preempt_notifier
);
538 kvm_arch_vcpu_load(vcpu
, cpu
);
542 void vcpu_put(struct kvm_vcpu
*vcpu
)
545 kvm_arch_vcpu_put(vcpu
);
546 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
548 mutex_unlock(&vcpu
->mutex
);
551 static void ack_flush(void *_completed
)
555 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
560 struct kvm_vcpu
*vcpu
;
564 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
565 vcpu
= kvm
->vcpus
[i
];
568 if (test_and_set_bit(req
, &vcpu
->requests
))
571 if (cpu
!= -1 && cpu
!= me
)
574 if (!cpus_empty(cpus
)) {
575 smp_call_function_mask(cpus
, ack_flush
, NULL
, 1);
582 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
584 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
585 ++kvm
->stat
.remote_tlb_flush
;
588 void kvm_reload_remote_mmus(struct kvm
*kvm
)
590 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
593 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
598 mutex_init(&vcpu
->mutex
);
602 init_waitqueue_head(&vcpu
->wq
);
604 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
609 vcpu
->run
= page_address(page
);
611 r
= kvm_arch_vcpu_init(vcpu
);
617 free_page((unsigned long)vcpu
->run
);
621 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
623 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
625 kvm_arch_vcpu_uninit(vcpu
);
626 free_page((unsigned long)vcpu
->run
);
628 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
630 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
631 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
633 return container_of(mn
, struct kvm
, mmu_notifier
);
636 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
637 struct mm_struct
*mm
,
638 unsigned long address
)
640 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
644 * When ->invalidate_page runs, the linux pte has been zapped
645 * already but the page is still allocated until
646 * ->invalidate_page returns. So if we increase the sequence
647 * here the kvm page fault will notice if the spte can't be
648 * established because the page is going to be freed. If
649 * instead the kvm page fault establishes the spte before
650 * ->invalidate_page runs, kvm_unmap_hva will release it
653 * The sequence increase only need to be seen at spin_unlock
654 * time, and not at spin_lock time.
656 * Increasing the sequence after the spin_unlock would be
657 * unsafe because the kvm page fault could then establish the
658 * pte after kvm_unmap_hva returned, without noticing the page
659 * is going to be freed.
661 spin_lock(&kvm
->mmu_lock
);
662 kvm
->mmu_notifier_seq
++;
663 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
664 spin_unlock(&kvm
->mmu_lock
);
666 /* we've to flush the tlb before the pages can be freed */
668 kvm_flush_remote_tlbs(kvm
);
672 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
673 struct mm_struct
*mm
,
677 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
678 int need_tlb_flush
= 0;
680 spin_lock(&kvm
->mmu_lock
);
682 * The count increase must become visible at unlock time as no
683 * spte can be established without taking the mmu_lock and
684 * count is also read inside the mmu_lock critical section.
686 kvm
->mmu_notifier_count
++;
687 for (; start
< end
; start
+= PAGE_SIZE
)
688 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
689 spin_unlock(&kvm
->mmu_lock
);
691 /* we've to flush the tlb before the pages can be freed */
693 kvm_flush_remote_tlbs(kvm
);
696 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
697 struct mm_struct
*mm
,
701 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
703 spin_lock(&kvm
->mmu_lock
);
705 * This sequence increase will notify the kvm page fault that
706 * the page that is going to be mapped in the spte could have
709 kvm
->mmu_notifier_seq
++;
711 * The above sequence increase must be visible before the
712 * below count decrease but both values are read by the kvm
713 * page fault under mmu_lock spinlock so we don't need to add
714 * a smb_wmb() here in between the two.
716 kvm
->mmu_notifier_count
--;
717 spin_unlock(&kvm
->mmu_lock
);
719 BUG_ON(kvm
->mmu_notifier_count
< 0);
722 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
723 struct mm_struct
*mm
,
724 unsigned long address
)
726 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
729 spin_lock(&kvm
->mmu_lock
);
730 young
= kvm_age_hva(kvm
, address
);
731 spin_unlock(&kvm
->mmu_lock
);
734 kvm_flush_remote_tlbs(kvm
);
739 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
740 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
741 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
742 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
743 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
745 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
747 static struct kvm
*kvm_create_vm(void)
749 struct kvm
*kvm
= kvm_arch_create_vm();
750 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
757 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
758 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
761 return ERR_PTR(-ENOMEM
);
763 kvm
->coalesced_mmio_ring
=
764 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
767 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
770 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
771 err
= mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
773 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
782 kvm
->mm
= current
->mm
;
783 atomic_inc(&kvm
->mm
->mm_count
);
784 spin_lock_init(&kvm
->mmu_lock
);
785 kvm_io_bus_init(&kvm
->pio_bus
);
786 mutex_init(&kvm
->lock
);
787 kvm_io_bus_init(&kvm
->mmio_bus
);
788 init_rwsem(&kvm
->slots_lock
);
789 atomic_set(&kvm
->users_count
, 1);
790 spin_lock(&kvm_lock
);
791 list_add(&kvm
->vm_list
, &vm_list
);
792 spin_unlock(&kvm_lock
);
793 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
794 kvm_coalesced_mmio_init(kvm
);
801 * Free any memory in @free but not in @dont.
803 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
804 struct kvm_memory_slot
*dont
)
806 if (!dont
|| free
->rmap
!= dont
->rmap
)
809 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
810 vfree(free
->dirty_bitmap
);
812 if (!dont
|| free
->lpage_info
!= dont
->lpage_info
)
813 vfree(free
->lpage_info
);
816 free
->dirty_bitmap
= NULL
;
818 free
->lpage_info
= NULL
;
821 void kvm_free_physmem(struct kvm
*kvm
)
825 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
826 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
829 static void kvm_destroy_vm(struct kvm
*kvm
)
831 struct mm_struct
*mm
= kvm
->mm
;
833 spin_lock(&kvm_lock
);
834 list_del(&kvm
->vm_list
);
835 spin_unlock(&kvm_lock
);
836 kvm_io_bus_destroy(&kvm
->pio_bus
);
837 kvm_io_bus_destroy(&kvm
->mmio_bus
);
838 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
839 if (kvm
->coalesced_mmio_ring
!= NULL
)
840 free_page((unsigned long)kvm
->coalesced_mmio_ring
);
842 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
843 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
845 kvm_arch_destroy_vm(kvm
);
849 void kvm_get_kvm(struct kvm
*kvm
)
851 atomic_inc(&kvm
->users_count
);
853 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
855 void kvm_put_kvm(struct kvm
*kvm
)
857 if (atomic_dec_and_test(&kvm
->users_count
))
860 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
863 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
865 struct kvm
*kvm
= filp
->private_data
;
872 * Allocate some memory and give it an address in the guest physical address
875 * Discontiguous memory is allowed, mostly for framebuffers.
877 * Must be called holding mmap_sem for write.
879 int __kvm_set_memory_region(struct kvm
*kvm
,
880 struct kvm_userspace_memory_region
*mem
,
885 unsigned long npages
;
887 struct kvm_memory_slot
*memslot
;
888 struct kvm_memory_slot old
, new;
891 /* General sanity checks */
892 if (mem
->memory_size
& (PAGE_SIZE
- 1))
894 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
896 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
898 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
900 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
903 memslot
= &kvm
->memslots
[mem
->slot
];
904 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
905 npages
= mem
->memory_size
>> PAGE_SHIFT
;
908 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
910 new = old
= *memslot
;
912 new.base_gfn
= base_gfn
;
914 new.flags
= mem
->flags
;
916 /* Disallow changing a memory slot's size. */
918 if (npages
&& old
.npages
&& npages
!= old
.npages
)
921 /* Check for overlaps */
923 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
924 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
928 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
929 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
933 /* Free page dirty bitmap if unneeded */
934 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
935 new.dirty_bitmap
= NULL
;
939 /* Allocate if a slot is being created */
941 if (npages
&& !new.rmap
) {
942 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
947 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
949 new.user_alloc
= user_alloc
;
951 * hva_to_rmmap() serialzies with the mmu_lock and to be
952 * safe it has to ignore memslots with !user_alloc &&
956 new.userspace_addr
= mem
->userspace_addr
;
958 new.userspace_addr
= 0;
960 if (npages
&& !new.lpage_info
) {
961 int largepages
= npages
/ KVM_PAGES_PER_HPAGE
;
962 if (npages
% KVM_PAGES_PER_HPAGE
)
964 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
967 new.lpage_info
= vmalloc(largepages
* sizeof(*new.lpage_info
));
972 memset(new.lpage_info
, 0, largepages
* sizeof(*new.lpage_info
));
974 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
975 new.lpage_info
[0].write_count
= 1;
976 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE
)
977 new.lpage_info
[largepages
-1].write_count
= 1;
980 /* Allocate page dirty bitmap if needed */
981 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
982 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
984 new.dirty_bitmap
= vmalloc(dirty_bytes
);
985 if (!new.dirty_bitmap
)
987 memset(new.dirty_bitmap
, 0, dirty_bytes
);
989 #endif /* not defined CONFIG_S390 */
992 kvm_arch_flush_shadow(kvm
);
994 spin_lock(&kvm
->mmu_lock
);
995 if (mem
->slot
>= kvm
->nmemslots
)
996 kvm
->nmemslots
= mem
->slot
+ 1;
999 spin_unlock(&kvm
->mmu_lock
);
1001 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
1003 spin_lock(&kvm
->mmu_lock
);
1005 spin_unlock(&kvm
->mmu_lock
);
1009 kvm_free_physmem_slot(&old
, npages
? &new : NULL
);
1010 /* Slot deletion case: we have to update the current slot */
1014 /* map the pages in iommu page table */
1015 r
= kvm_iommu_map_pages(kvm
, base_gfn
, npages
);
1022 kvm_free_physmem_slot(&new, &old
);
1027 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
1029 int kvm_set_memory_region(struct kvm
*kvm
,
1030 struct kvm_userspace_memory_region
*mem
,
1035 down_write(&kvm
->slots_lock
);
1036 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
1037 up_write(&kvm
->slots_lock
);
1040 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
1042 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
1044 kvm_userspace_memory_region
*mem
,
1047 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
1049 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
1052 int kvm_get_dirty_log(struct kvm
*kvm
,
1053 struct kvm_dirty_log
*log
, int *is_dirty
)
1055 struct kvm_memory_slot
*memslot
;
1058 unsigned long any
= 0;
1061 if (log
->slot
>= KVM_MEMORY_SLOTS
)
1064 memslot
= &kvm
->memslots
[log
->slot
];
1066 if (!memslot
->dirty_bitmap
)
1069 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1071 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
1072 any
= memslot
->dirty_bitmap
[i
];
1075 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
1086 int is_error_page(struct page
*page
)
1088 return page
== bad_page
;
1090 EXPORT_SYMBOL_GPL(is_error_page
);
1092 int is_error_pfn(pfn_t pfn
)
1094 return pfn
== bad_pfn
;
1096 EXPORT_SYMBOL_GPL(is_error_pfn
);
1098 static inline unsigned long bad_hva(void)
1103 int kvm_is_error_hva(unsigned long addr
)
1105 return addr
== bad_hva();
1107 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
1109 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
1113 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1114 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1116 if (gfn
>= memslot
->base_gfn
1117 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1122 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
1124 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1126 gfn
= unalias_gfn(kvm
, gfn
);
1127 return gfn_to_memslot_unaliased(kvm
, gfn
);
1130 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
1134 gfn
= unalias_gfn(kvm
, gfn
);
1135 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1136 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1138 if (gfn
>= memslot
->base_gfn
1139 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1144 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
1146 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1148 struct kvm_memory_slot
*slot
;
1150 gfn
= unalias_gfn(kvm
, gfn
);
1151 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1154 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
1156 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1158 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1160 struct page
*page
[1];
1167 addr
= gfn_to_hva(kvm
, gfn
);
1168 if (kvm_is_error_hva(addr
)) {
1170 return page_to_pfn(bad_page
);
1173 npages
= get_user_pages_fast(addr
, 1, 1, page
);
1175 if (unlikely(npages
!= 1)) {
1176 struct vm_area_struct
*vma
;
1178 down_read(¤t
->mm
->mmap_sem
);
1179 vma
= find_vma(current
->mm
, addr
);
1181 if (vma
== NULL
|| addr
< vma
->vm_start
||
1182 !(vma
->vm_flags
& VM_PFNMAP
)) {
1183 up_read(¤t
->mm
->mmap_sem
);
1185 return page_to_pfn(bad_page
);
1188 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1189 up_read(¤t
->mm
->mmap_sem
);
1190 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1192 pfn
= page_to_pfn(page
[0]);
1197 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1199 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1203 pfn
= gfn_to_pfn(kvm
, gfn
);
1204 if (!kvm_is_mmio_pfn(pfn
))
1205 return pfn_to_page(pfn
);
1207 WARN_ON(kvm_is_mmio_pfn(pfn
));
1213 EXPORT_SYMBOL_GPL(gfn_to_page
);
1215 void kvm_release_page_clean(struct page
*page
)
1217 kvm_release_pfn_clean(page_to_pfn(page
));
1219 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1221 void kvm_release_pfn_clean(pfn_t pfn
)
1223 if (!kvm_is_mmio_pfn(pfn
))
1224 put_page(pfn_to_page(pfn
));
1226 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1228 void kvm_release_page_dirty(struct page
*page
)
1230 kvm_release_pfn_dirty(page_to_pfn(page
));
1232 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1234 void kvm_release_pfn_dirty(pfn_t pfn
)
1236 kvm_set_pfn_dirty(pfn
);
1237 kvm_release_pfn_clean(pfn
);
1239 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1241 void kvm_set_page_dirty(struct page
*page
)
1243 kvm_set_pfn_dirty(page_to_pfn(page
));
1245 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1247 void kvm_set_pfn_dirty(pfn_t pfn
)
1249 if (!kvm_is_mmio_pfn(pfn
)) {
1250 struct page
*page
= pfn_to_page(pfn
);
1251 if (!PageReserved(page
))
1255 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1257 void kvm_set_pfn_accessed(pfn_t pfn
)
1259 if (!kvm_is_mmio_pfn(pfn
))
1260 mark_page_accessed(pfn_to_page(pfn
));
1262 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1264 void kvm_get_pfn(pfn_t pfn
)
1266 if (!kvm_is_mmio_pfn(pfn
))
1267 get_page(pfn_to_page(pfn
));
1269 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1271 static int next_segment(unsigned long len
, int offset
)
1273 if (len
> PAGE_SIZE
- offset
)
1274 return PAGE_SIZE
- offset
;
1279 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1285 addr
= gfn_to_hva(kvm
, gfn
);
1286 if (kvm_is_error_hva(addr
))
1288 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1293 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1295 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1297 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1299 int offset
= offset_in_page(gpa
);
1302 while ((seg
= next_segment(len
, offset
)) != 0) {
1303 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1313 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1315 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1320 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1321 int offset
= offset_in_page(gpa
);
1323 addr
= gfn_to_hva(kvm
, gfn
);
1324 if (kvm_is_error_hva(addr
))
1326 pagefault_disable();
1327 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1333 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1335 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1336 int offset
, int len
)
1341 addr
= gfn_to_hva(kvm
, gfn
);
1342 if (kvm_is_error_hva(addr
))
1344 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1347 mark_page_dirty(kvm
, gfn
);
1350 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1352 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1355 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1357 int offset
= offset_in_page(gpa
);
1360 while ((seg
= next_segment(len
, offset
)) != 0) {
1361 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1372 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1374 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1376 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1378 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1380 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1382 int offset
= offset_in_page(gpa
);
1385 while ((seg
= next_segment(len
, offset
)) != 0) {
1386 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1395 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1397 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1399 struct kvm_memory_slot
*memslot
;
1401 gfn
= unalias_gfn(kvm
, gfn
);
1402 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1403 if (memslot
&& memslot
->dirty_bitmap
) {
1404 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1407 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1408 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1413 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1415 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1420 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1422 if (kvm_cpu_has_interrupt(vcpu
) ||
1423 kvm_cpu_has_pending_timer(vcpu
) ||
1424 kvm_arch_vcpu_runnable(vcpu
)) {
1425 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1428 if (signal_pending(current
))
1436 finish_wait(&vcpu
->wq
, &wait
);
1439 void kvm_resched(struct kvm_vcpu
*vcpu
)
1441 if (!need_resched())
1445 EXPORT_SYMBOL_GPL(kvm_resched
);
1447 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1449 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1452 if (vmf
->pgoff
== 0)
1453 page
= virt_to_page(vcpu
->run
);
1455 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1456 page
= virt_to_page(vcpu
->arch
.pio_data
);
1458 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1459 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1460 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1463 return VM_FAULT_SIGBUS
;
1469 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
1470 .fault
= kvm_vcpu_fault
,
1473 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1475 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1479 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1481 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1483 kvm_put_kvm(vcpu
->kvm
);
1487 static struct file_operations kvm_vcpu_fops
= {
1488 .release
= kvm_vcpu_release
,
1489 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1490 .compat_ioctl
= kvm_vcpu_ioctl
,
1491 .mmap
= kvm_vcpu_mmap
,
1495 * Allocates an inode for the vcpu.
1497 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1499 int fd
= anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, 0);
1501 kvm_put_kvm(vcpu
->kvm
);
1506 * Creates some virtual cpus. Good luck creating more than one.
1508 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
1511 struct kvm_vcpu
*vcpu
;
1516 vcpu
= kvm_arch_vcpu_create(kvm
, n
);
1518 return PTR_ERR(vcpu
);
1520 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1522 r
= kvm_arch_vcpu_setup(vcpu
);
1526 mutex_lock(&kvm
->lock
);
1527 if (kvm
->vcpus
[n
]) {
1531 kvm
->vcpus
[n
] = vcpu
;
1532 mutex_unlock(&kvm
->lock
);
1534 /* Now it's all set up, let userspace reach it */
1536 r
= create_vcpu_fd(vcpu
);
1542 mutex_lock(&kvm
->lock
);
1543 kvm
->vcpus
[n
] = NULL
;
1545 mutex_unlock(&kvm
->lock
);
1546 kvm_arch_vcpu_destroy(vcpu
);
1550 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1553 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1554 vcpu
->sigset_active
= 1;
1555 vcpu
->sigset
= *sigset
;
1557 vcpu
->sigset_active
= 0;
1561 static long kvm_vcpu_ioctl(struct file
*filp
,
1562 unsigned int ioctl
, unsigned long arg
)
1564 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1565 void __user
*argp
= (void __user
*)arg
;
1567 struct kvm_fpu
*fpu
= NULL
;
1568 struct kvm_sregs
*kvm_sregs
= NULL
;
1570 if (vcpu
->kvm
->mm
!= current
->mm
)
1577 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1579 case KVM_GET_REGS
: {
1580 struct kvm_regs
*kvm_regs
;
1583 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1586 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1590 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1597 case KVM_SET_REGS
: {
1598 struct kvm_regs
*kvm_regs
;
1601 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1605 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1607 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1615 case KVM_GET_SREGS
: {
1616 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1620 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1624 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1629 case KVM_SET_SREGS
: {
1630 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1635 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1637 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1643 case KVM_GET_MP_STATE
: {
1644 struct kvm_mp_state mp_state
;
1646 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1650 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1655 case KVM_SET_MP_STATE
: {
1656 struct kvm_mp_state mp_state
;
1659 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1661 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1667 case KVM_TRANSLATE
: {
1668 struct kvm_translation tr
;
1671 if (copy_from_user(&tr
, argp
, sizeof tr
))
1673 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1677 if (copy_to_user(argp
, &tr
, sizeof tr
))
1682 case KVM_DEBUG_GUEST
: {
1683 struct kvm_debug_guest dbg
;
1686 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1688 r
= kvm_arch_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
1694 case KVM_SET_SIGNAL_MASK
: {
1695 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1696 struct kvm_signal_mask kvm_sigmask
;
1697 sigset_t sigset
, *p
;
1702 if (copy_from_user(&kvm_sigmask
, argp
,
1703 sizeof kvm_sigmask
))
1706 if (kvm_sigmask
.len
!= sizeof sigset
)
1709 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1714 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1718 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1722 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1726 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1732 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1737 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1739 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1746 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1754 static long kvm_vm_ioctl(struct file
*filp
,
1755 unsigned int ioctl
, unsigned long arg
)
1757 struct kvm
*kvm
= filp
->private_data
;
1758 void __user
*argp
= (void __user
*)arg
;
1761 if (kvm
->mm
!= current
->mm
)
1764 case KVM_CREATE_VCPU
:
1765 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1769 case KVM_SET_USER_MEMORY_REGION
: {
1770 struct kvm_userspace_memory_region kvm_userspace_mem
;
1773 if (copy_from_user(&kvm_userspace_mem
, argp
,
1774 sizeof kvm_userspace_mem
))
1777 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1782 case KVM_GET_DIRTY_LOG
: {
1783 struct kvm_dirty_log log
;
1786 if (copy_from_user(&log
, argp
, sizeof log
))
1788 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1793 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1794 case KVM_REGISTER_COALESCED_MMIO
: {
1795 struct kvm_coalesced_mmio_zone zone
;
1797 if (copy_from_user(&zone
, argp
, sizeof zone
))
1800 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1806 case KVM_UNREGISTER_COALESCED_MMIO
: {
1807 struct kvm_coalesced_mmio_zone zone
;
1809 if (copy_from_user(&zone
, argp
, sizeof zone
))
1812 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1819 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1820 case KVM_ASSIGN_PCI_DEVICE
: {
1821 struct kvm_assigned_pci_dev assigned_dev
;
1824 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1826 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
1831 case KVM_ASSIGN_IRQ
: {
1832 struct kvm_assigned_irq assigned_irq
;
1835 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
1837 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
1844 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1850 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1852 struct page
*page
[1];
1855 gfn_t gfn
= vmf
->pgoff
;
1856 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1858 addr
= gfn_to_hva(kvm
, gfn
);
1859 if (kvm_is_error_hva(addr
))
1860 return VM_FAULT_SIGBUS
;
1862 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1864 if (unlikely(npages
!= 1))
1865 return VM_FAULT_SIGBUS
;
1867 vmf
->page
= page
[0];
1871 static struct vm_operations_struct kvm_vm_vm_ops
= {
1872 .fault
= kvm_vm_fault
,
1875 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1877 vma
->vm_ops
= &kvm_vm_vm_ops
;
1881 static struct file_operations kvm_vm_fops
= {
1882 .release
= kvm_vm_release
,
1883 .unlocked_ioctl
= kvm_vm_ioctl
,
1884 .compat_ioctl
= kvm_vm_ioctl
,
1885 .mmap
= kvm_vm_mmap
,
1888 static int kvm_dev_ioctl_create_vm(void)
1893 kvm
= kvm_create_vm();
1895 return PTR_ERR(kvm
);
1896 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, 0);
1903 static long kvm_dev_ioctl(struct file
*filp
,
1904 unsigned int ioctl
, unsigned long arg
)
1909 case KVM_GET_API_VERSION
:
1913 r
= KVM_API_VERSION
;
1919 r
= kvm_dev_ioctl_create_vm();
1921 case KVM_CHECK_EXTENSION
:
1922 r
= kvm_dev_ioctl_check_extension(arg
);
1924 case KVM_GET_VCPU_MMAP_SIZE
:
1928 r
= PAGE_SIZE
; /* struct kvm_run */
1930 r
+= PAGE_SIZE
; /* pio data page */
1932 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1933 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1936 case KVM_TRACE_ENABLE
:
1937 case KVM_TRACE_PAUSE
:
1938 case KVM_TRACE_DISABLE
:
1939 r
= kvm_trace_ioctl(ioctl
, arg
);
1942 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1948 static struct file_operations kvm_chardev_ops
= {
1949 .unlocked_ioctl
= kvm_dev_ioctl
,
1950 .compat_ioctl
= kvm_dev_ioctl
,
1953 static struct miscdevice kvm_dev
= {
1959 static void hardware_enable(void *junk
)
1961 int cpu
= raw_smp_processor_id();
1963 if (cpu_isset(cpu
, cpus_hardware_enabled
))
1965 cpu_set(cpu
, cpus_hardware_enabled
);
1966 kvm_arch_hardware_enable(NULL
);
1969 static void hardware_disable(void *junk
)
1971 int cpu
= raw_smp_processor_id();
1973 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
1975 cpu_clear(cpu
, cpus_hardware_enabled
);
1976 kvm_arch_hardware_disable(NULL
);
1979 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1984 val
&= ~CPU_TASKS_FROZEN
;
1987 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1989 hardware_disable(NULL
);
1991 case CPU_UP_CANCELED
:
1992 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1994 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
1997 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1999 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
2006 asmlinkage
void kvm_handle_fault_on_reboot(void)
2009 /* spin while reset goes on */
2012 /* Fault while not rebooting. We want the trace. */
2015 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
2017 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2020 if (val
== SYS_RESTART
) {
2022 * Some (well, at least mine) BIOSes hang on reboot if
2025 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2026 kvm_rebooting
= true;
2027 on_each_cpu(hardware_disable
, NULL
, 1);
2032 static struct notifier_block kvm_reboot_notifier
= {
2033 .notifier_call
= kvm_reboot
,
2037 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
2039 memset(bus
, 0, sizeof(*bus
));
2042 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2046 for (i
= 0; i
< bus
->dev_count
; i
++) {
2047 struct kvm_io_device
*pos
= bus
->devs
[i
];
2049 kvm_iodevice_destructor(pos
);
2053 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
,
2054 gpa_t addr
, int len
, int is_write
)
2058 for (i
= 0; i
< bus
->dev_count
; i
++) {
2059 struct kvm_io_device
*pos
= bus
->devs
[i
];
2061 if (pos
->in_range(pos
, addr
, len
, is_write
))
2068 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
2070 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
2072 bus
->devs
[bus
->dev_count
++] = dev
;
2075 static struct notifier_block kvm_cpu_notifier
= {
2076 .notifier_call
= kvm_cpu_hotplug
,
2077 .priority
= 20, /* must be > scheduler priority */
2080 static int vm_stat_get(void *_offset
, u64
*val
)
2082 unsigned offset
= (long)_offset
;
2086 spin_lock(&kvm_lock
);
2087 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2088 *val
+= *(u32
*)((void *)kvm
+ offset
);
2089 spin_unlock(&kvm_lock
);
2093 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2095 static int vcpu_stat_get(void *_offset
, u64
*val
)
2097 unsigned offset
= (long)_offset
;
2099 struct kvm_vcpu
*vcpu
;
2103 spin_lock(&kvm_lock
);
2104 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2105 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2106 vcpu
= kvm
->vcpus
[i
];
2108 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2110 spin_unlock(&kvm_lock
);
2114 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2116 static struct file_operations
*stat_fops
[] = {
2117 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2118 [KVM_STAT_VM
] = &vm_stat_fops
,
2121 static void kvm_init_debug(void)
2123 struct kvm_stats_debugfs_item
*p
;
2125 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2126 for (p
= debugfs_entries
; p
->name
; ++p
)
2127 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2128 (void *)(long)p
->offset
,
2129 stat_fops
[p
->kind
]);
2132 static void kvm_exit_debug(void)
2134 struct kvm_stats_debugfs_item
*p
;
2136 for (p
= debugfs_entries
; p
->name
; ++p
)
2137 debugfs_remove(p
->dentry
);
2138 debugfs_remove(kvm_debugfs_dir
);
2141 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2143 hardware_disable(NULL
);
2147 static int kvm_resume(struct sys_device
*dev
)
2149 hardware_enable(NULL
);
2153 static struct sysdev_class kvm_sysdev_class
= {
2155 .suspend
= kvm_suspend
,
2156 .resume
= kvm_resume
,
2159 static struct sys_device kvm_sysdev
= {
2161 .cls
= &kvm_sysdev_class
,
2164 struct page
*bad_page
;
2168 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2170 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2173 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2175 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2177 kvm_arch_vcpu_load(vcpu
, cpu
);
2180 static void kvm_sched_out(struct preempt_notifier
*pn
,
2181 struct task_struct
*next
)
2183 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2185 kvm_arch_vcpu_put(vcpu
);
2188 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2189 struct module
*module
)
2196 r
= kvm_arch_init(opaque
);
2200 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2202 if (bad_page
== NULL
) {
2207 bad_pfn
= page_to_pfn(bad_page
);
2209 r
= kvm_arch_hardware_setup();
2213 for_each_online_cpu(cpu
) {
2214 smp_call_function_single(cpu
,
2215 kvm_arch_check_processor_compat
,
2221 on_each_cpu(hardware_enable
, NULL
, 1);
2222 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2225 register_reboot_notifier(&kvm_reboot_notifier
);
2227 r
= sysdev_class_register(&kvm_sysdev_class
);
2231 r
= sysdev_register(&kvm_sysdev
);
2235 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2236 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2237 __alignof__(struct kvm_vcpu
),
2239 if (!kvm_vcpu_cache
) {
2244 kvm_chardev_ops
.owner
= module
;
2245 kvm_vm_fops
.owner
= module
;
2246 kvm_vcpu_fops
.owner
= module
;
2248 r
= misc_register(&kvm_dev
);
2250 printk(KERN_ERR
"kvm: misc device register failed\n");
2254 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2255 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2263 kmem_cache_destroy(kvm_vcpu_cache
);
2265 sysdev_unregister(&kvm_sysdev
);
2267 sysdev_class_unregister(&kvm_sysdev_class
);
2269 unregister_reboot_notifier(&kvm_reboot_notifier
);
2270 unregister_cpu_notifier(&kvm_cpu_notifier
);
2272 on_each_cpu(hardware_disable
, NULL
, 1);
2274 kvm_arch_hardware_unsetup();
2276 __free_page(bad_page
);
2283 EXPORT_SYMBOL_GPL(kvm_init
);
2287 kvm_trace_cleanup();
2288 misc_deregister(&kvm_dev
);
2289 kmem_cache_destroy(kvm_vcpu_cache
);
2290 sysdev_unregister(&kvm_sysdev
);
2291 sysdev_class_unregister(&kvm_sysdev_class
);
2292 unregister_reboot_notifier(&kvm_reboot_notifier
);
2293 unregister_cpu_notifier(&kvm_cpu_notifier
);
2294 on_each_cpu(hardware_disable
, NULL
, 1);
2295 kvm_arch_hardware_unsetup();
2298 __free_page(bad_page
);
2300 EXPORT_SYMBOL_GPL(kvm_exit
);