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_var_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
);
173 assigned_dev
->host_irq_disabled
= false;
175 mutex_unlock(&assigned_dev
->kvm
->lock
);
176 kvm_put_kvm(assigned_dev
->kvm
);
179 static irqreturn_t
kvm_assigned_dev_intr(int irq
, void *dev_id
)
181 struct kvm_assigned_dev_kernel
*assigned_dev
=
182 (struct kvm_assigned_dev_kernel
*) dev_id
;
184 kvm_get_kvm(assigned_dev
->kvm
);
186 schedule_work(&assigned_dev
->interrupt_work
);
188 disable_irq_nosync(irq
);
189 assigned_dev
->host_irq_disabled
= true;
194 /* Ack the irq line for an assigned device */
195 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
197 struct kvm_assigned_dev_kernel
*dev
;
202 dev
= container_of(kian
, struct kvm_assigned_dev_kernel
,
205 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
207 /* The guest irq may be shared so this ack may be
208 * from another device.
210 if (dev
->host_irq_disabled
) {
211 enable_irq(dev
->host_irq
);
212 dev
->host_irq_disabled
= false;
216 static void kvm_free_assigned_irq(struct kvm
*kvm
,
217 struct kvm_assigned_dev_kernel
*assigned_dev
)
219 if (!irqchip_in_kernel(kvm
))
222 kvm_unregister_irq_ack_notifier(&assigned_dev
->ack_notifier
);
224 if (assigned_dev
->irq_source_id
!= -1)
225 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
226 assigned_dev
->irq_source_id
= -1;
228 if (!assigned_dev
->irq_requested_type
)
231 if (cancel_work_sync(&assigned_dev
->interrupt_work
))
232 /* We had pending work. That means we will have to take
233 * care of kvm_put_kvm.
237 free_irq(assigned_dev
->host_irq
, (void *)assigned_dev
);
239 if (assigned_dev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
)
240 pci_disable_msi(assigned_dev
->dev
);
242 assigned_dev
->irq_requested_type
= 0;
246 static void kvm_free_assigned_device(struct kvm
*kvm
,
247 struct kvm_assigned_dev_kernel
250 kvm_free_assigned_irq(kvm
, assigned_dev
);
252 pci_reset_function(assigned_dev
->dev
);
254 pci_release_regions(assigned_dev
->dev
);
255 pci_disable_device(assigned_dev
->dev
);
256 pci_dev_put(assigned_dev
->dev
);
258 list_del(&assigned_dev
->list
);
262 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
264 struct list_head
*ptr
, *ptr2
;
265 struct kvm_assigned_dev_kernel
*assigned_dev
;
267 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
268 assigned_dev
= list_entry(ptr
,
269 struct kvm_assigned_dev_kernel
,
272 kvm_free_assigned_device(kvm
, assigned_dev
);
276 static int assigned_device_update_intx(struct kvm
*kvm
,
277 struct kvm_assigned_dev_kernel
*adev
,
278 struct kvm_assigned_irq
*airq
)
280 adev
->guest_irq
= airq
->guest_irq
;
281 adev
->ack_notifier
.gsi
= airq
->guest_irq
;
283 if (adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_INTX
)
286 if (irqchip_in_kernel(kvm
)) {
288 adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
) {
289 free_irq(adev
->host_irq
, (void *)kvm
);
290 pci_disable_msi(adev
->dev
);
293 if (!capable(CAP_SYS_RAWIO
))
297 adev
->host_irq
= airq
->host_irq
;
299 adev
->host_irq
= adev
->dev
->irq
;
301 /* Even though this is PCI, we don't want to use shared
302 * interrupts. Sharing host devices with guest-assigned devices
303 * on the same interrupt line is not a happy situation: there
304 * are going to be long delays in accepting, acking, etc.
306 if (request_irq(adev
->host_irq
, kvm_assigned_dev_intr
,
307 0, "kvm_assigned_intx_device", (void *)adev
))
311 adev
->irq_requested_type
= KVM_ASSIGNED_DEV_GUEST_INTX
|
312 KVM_ASSIGNED_DEV_HOST_INTX
;
317 static int assigned_device_update_msi(struct kvm
*kvm
,
318 struct kvm_assigned_dev_kernel
*adev
,
319 struct kvm_assigned_irq
*airq
)
323 if (airq
->flags
& KVM_DEV_IRQ_ASSIGN_ENABLE_MSI
) {
324 /* x86 don't care upper address of guest msi message addr */
325 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_GUEST_MSI
;
326 adev
->irq_requested_type
&= ~KVM_ASSIGNED_DEV_GUEST_INTX
;
327 adev
->guest_msi
.address_lo
= airq
->guest_msi
.addr_lo
;
328 adev
->guest_msi
.data
= airq
->guest_msi
.data
;
329 adev
->ack_notifier
.gsi
= -1;
330 } else if (msi2intx
) {
331 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_GUEST_INTX
;
332 adev
->irq_requested_type
&= ~KVM_ASSIGNED_DEV_GUEST_MSI
;
333 adev
->guest_irq
= airq
->guest_irq
;
334 adev
->ack_notifier
.gsi
= airq
->guest_irq
;
337 if (adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_MSI
)
340 if (irqchip_in_kernel(kvm
)) {
342 if (adev
->irq_requested_type
&
343 KVM_ASSIGNED_DEV_HOST_INTX
)
344 free_irq(adev
->host_irq
, (void *)adev
);
346 r
= pci_enable_msi(adev
->dev
);
351 adev
->host_irq
= adev
->dev
->irq
;
352 if (request_irq(adev
->host_irq
, kvm_assigned_dev_intr
, 0,
353 "kvm_assigned_msi_device", (void *)adev
))
358 adev
->irq_requested_type
= KVM_ASSIGNED_DEV_GUEST_MSI
;
360 adev
->irq_requested_type
|= KVM_ASSIGNED_DEV_HOST_MSI
;
365 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
366 struct kvm_assigned_irq
370 struct kvm_assigned_dev_kernel
*match
;
372 mutex_lock(&kvm
->lock
);
374 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
375 assigned_irq
->assigned_dev_id
);
377 mutex_unlock(&kvm
->lock
);
381 if (!match
->irq_requested_type
) {
382 INIT_WORK(&match
->interrupt_work
,
383 kvm_assigned_dev_interrupt_work_handler
);
384 if (irqchip_in_kernel(kvm
)) {
385 /* Register ack nofitier */
386 match
->ack_notifier
.gsi
= -1;
387 match
->ack_notifier
.irq_acked
=
388 kvm_assigned_dev_ack_irq
;
389 kvm_register_irq_ack_notifier(kvm
,
390 &match
->ack_notifier
);
392 /* Request IRQ source ID */
393 r
= kvm_request_irq_source_id(kvm
);
397 match
->irq_source_id
= r
;
400 /* Determine host device irq type, we can know the
401 * result from dev->msi_enabled */
403 pci_enable_msi(match
->dev
);
409 (assigned_irq
->flags
& KVM_DEV_IRQ_ASSIGN_ENABLE_MSI
)) ||
410 (msi2intx
&& match
->dev
->msi_enabled
)) {
412 r
= assigned_device_update_msi(kvm
, match
, assigned_irq
);
414 printk(KERN_WARNING
"kvm: failed to enable "
421 } else if (assigned_irq
->host_irq
== 0 && match
->dev
->irq
== 0) {
422 /* Host device IRQ 0 means don't support INTx */
425 "kvm: wait device to enable MSI!\n");
429 "kvm: failed to enable MSI device!\n");
434 /* Non-sharing INTx mode */
435 r
= assigned_device_update_intx(kvm
, match
, assigned_irq
);
437 printk(KERN_WARNING
"kvm: failed to enable "
443 mutex_unlock(&kvm
->lock
);
446 mutex_unlock(&kvm
->lock
);
447 kvm_free_assigned_device(kvm
, match
);
451 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
452 struct kvm_assigned_pci_dev
*assigned_dev
)
455 struct kvm_assigned_dev_kernel
*match
;
458 mutex_lock(&kvm
->lock
);
460 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
461 assigned_dev
->assigned_dev_id
);
463 /* device already assigned */
468 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
470 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
475 dev
= pci_get_bus_and_slot(assigned_dev
->busnr
,
476 assigned_dev
->devfn
);
478 printk(KERN_INFO
"%s: host device not found\n", __func__
);
482 if (pci_enable_device(dev
)) {
483 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
487 r
= pci_request_regions(dev
, "kvm_assigned_device");
489 printk(KERN_INFO
"%s: Could not get access to device regions\n",
494 pci_reset_function(dev
);
496 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
497 match
->host_busnr
= assigned_dev
->busnr
;
498 match
->host_devfn
= assigned_dev
->devfn
;
500 match
->irq_source_id
= -1;
503 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
505 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
) {
506 r
= kvm_iommu_map_guest(kvm
, match
);
512 mutex_unlock(&kvm
->lock
);
515 list_del(&match
->list
);
516 pci_release_regions(dev
);
518 pci_disable_device(dev
);
523 mutex_unlock(&kvm
->lock
);
528 static inline int valid_vcpu(int n
)
530 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
533 inline int kvm_is_mmio_pfn(pfn_t pfn
)
536 return PageReserved(pfn_to_page(pfn
));
542 * Switches to specified vcpu, until a matching vcpu_put()
544 void vcpu_load(struct kvm_vcpu
*vcpu
)
548 mutex_lock(&vcpu
->mutex
);
550 preempt_notifier_register(&vcpu
->preempt_notifier
);
551 kvm_arch_vcpu_load(vcpu
, cpu
);
555 void vcpu_put(struct kvm_vcpu
*vcpu
)
558 kvm_arch_vcpu_put(vcpu
);
559 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
561 mutex_unlock(&vcpu
->mutex
);
564 static void ack_flush(void *_completed
)
568 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
573 struct kvm_vcpu
*vcpu
;
575 if (alloc_cpumask_var(&cpus
, GFP_ATOMIC
))
579 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
580 vcpu
= kvm
->vcpus
[i
];
583 if (test_and_set_bit(req
, &vcpu
->requests
))
586 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
587 cpumask_set_cpu(cpu
, cpus
);
589 if (unlikely(cpus
== NULL
))
590 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
591 else if (!cpumask_empty(cpus
))
592 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
596 free_cpumask_var(cpus
);
600 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
602 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
603 ++kvm
->stat
.remote_tlb_flush
;
606 void kvm_reload_remote_mmus(struct kvm
*kvm
)
608 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
611 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
616 mutex_init(&vcpu
->mutex
);
620 init_waitqueue_head(&vcpu
->wq
);
622 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
627 vcpu
->run
= page_address(page
);
629 r
= kvm_arch_vcpu_init(vcpu
);
635 free_page((unsigned long)vcpu
->run
);
639 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
641 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
643 kvm_arch_vcpu_uninit(vcpu
);
644 free_page((unsigned long)vcpu
->run
);
646 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
648 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
649 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
651 return container_of(mn
, struct kvm
, mmu_notifier
);
654 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
655 struct mm_struct
*mm
,
656 unsigned long address
)
658 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
662 * When ->invalidate_page runs, the linux pte has been zapped
663 * already but the page is still allocated until
664 * ->invalidate_page returns. So if we increase the sequence
665 * here the kvm page fault will notice if the spte can't be
666 * established because the page is going to be freed. If
667 * instead the kvm page fault establishes the spte before
668 * ->invalidate_page runs, kvm_unmap_hva will release it
671 * The sequence increase only need to be seen at spin_unlock
672 * time, and not at spin_lock time.
674 * Increasing the sequence after the spin_unlock would be
675 * unsafe because the kvm page fault could then establish the
676 * pte after kvm_unmap_hva returned, without noticing the page
677 * is going to be freed.
679 spin_lock(&kvm
->mmu_lock
);
680 kvm
->mmu_notifier_seq
++;
681 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
682 spin_unlock(&kvm
->mmu_lock
);
684 /* we've to flush the tlb before the pages can be freed */
686 kvm_flush_remote_tlbs(kvm
);
690 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
691 struct mm_struct
*mm
,
695 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
696 int need_tlb_flush
= 0;
698 spin_lock(&kvm
->mmu_lock
);
700 * The count increase must become visible at unlock time as no
701 * spte can be established without taking the mmu_lock and
702 * count is also read inside the mmu_lock critical section.
704 kvm
->mmu_notifier_count
++;
705 for (; start
< end
; start
+= PAGE_SIZE
)
706 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
707 spin_unlock(&kvm
->mmu_lock
);
709 /* we've to flush the tlb before the pages can be freed */
711 kvm_flush_remote_tlbs(kvm
);
714 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
715 struct mm_struct
*mm
,
719 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
721 spin_lock(&kvm
->mmu_lock
);
723 * This sequence increase will notify the kvm page fault that
724 * the page that is going to be mapped in the spte could have
727 kvm
->mmu_notifier_seq
++;
729 * The above sequence increase must be visible before the
730 * below count decrease but both values are read by the kvm
731 * page fault under mmu_lock spinlock so we don't need to add
732 * a smb_wmb() here in between the two.
734 kvm
->mmu_notifier_count
--;
735 spin_unlock(&kvm
->mmu_lock
);
737 BUG_ON(kvm
->mmu_notifier_count
< 0);
740 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
741 struct mm_struct
*mm
,
742 unsigned long address
)
744 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
747 spin_lock(&kvm
->mmu_lock
);
748 young
= kvm_age_hva(kvm
, address
);
749 spin_unlock(&kvm
->mmu_lock
);
752 kvm_flush_remote_tlbs(kvm
);
757 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
758 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
759 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
760 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
761 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
763 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
765 static struct kvm
*kvm_create_vm(void)
767 struct kvm
*kvm
= kvm_arch_create_vm();
768 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
775 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
776 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
779 return ERR_PTR(-ENOMEM
);
781 kvm
->coalesced_mmio_ring
=
782 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
785 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
788 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
789 err
= mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
791 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
800 kvm
->mm
= current
->mm
;
801 atomic_inc(&kvm
->mm
->mm_count
);
802 spin_lock_init(&kvm
->mmu_lock
);
803 kvm_io_bus_init(&kvm
->pio_bus
);
804 mutex_init(&kvm
->lock
);
805 kvm_io_bus_init(&kvm
->mmio_bus
);
806 init_rwsem(&kvm
->slots_lock
);
807 atomic_set(&kvm
->users_count
, 1);
808 spin_lock(&kvm_lock
);
809 list_add(&kvm
->vm_list
, &vm_list
);
810 spin_unlock(&kvm_lock
);
811 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
812 kvm_coalesced_mmio_init(kvm
);
819 * Free any memory in @free but not in @dont.
821 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
822 struct kvm_memory_slot
*dont
)
824 if (!dont
|| free
->rmap
!= dont
->rmap
)
827 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
828 vfree(free
->dirty_bitmap
);
830 if (!dont
|| free
->lpage_info
!= dont
->lpage_info
)
831 vfree(free
->lpage_info
);
834 free
->dirty_bitmap
= NULL
;
836 free
->lpage_info
= NULL
;
839 void kvm_free_physmem(struct kvm
*kvm
)
843 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
844 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
847 static void kvm_destroy_vm(struct kvm
*kvm
)
849 struct mm_struct
*mm
= kvm
->mm
;
851 spin_lock(&kvm_lock
);
852 list_del(&kvm
->vm_list
);
853 spin_unlock(&kvm_lock
);
854 kvm_io_bus_destroy(&kvm
->pio_bus
);
855 kvm_io_bus_destroy(&kvm
->mmio_bus
);
856 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
857 if (kvm
->coalesced_mmio_ring
!= NULL
)
858 free_page((unsigned long)kvm
->coalesced_mmio_ring
);
860 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
861 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
863 kvm_arch_destroy_vm(kvm
);
867 void kvm_get_kvm(struct kvm
*kvm
)
869 atomic_inc(&kvm
->users_count
);
871 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
873 void kvm_put_kvm(struct kvm
*kvm
)
875 if (atomic_dec_and_test(&kvm
->users_count
))
878 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
881 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
883 struct kvm
*kvm
= filp
->private_data
;
890 * Allocate some memory and give it an address in the guest physical address
893 * Discontiguous memory is allowed, mostly for framebuffers.
895 * Must be called holding mmap_sem for write.
897 int __kvm_set_memory_region(struct kvm
*kvm
,
898 struct kvm_userspace_memory_region
*mem
,
903 unsigned long npages
;
905 struct kvm_memory_slot
*memslot
;
906 struct kvm_memory_slot old
, new;
909 /* General sanity checks */
910 if (mem
->memory_size
& (PAGE_SIZE
- 1))
912 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
914 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
916 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
918 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
921 memslot
= &kvm
->memslots
[mem
->slot
];
922 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
923 npages
= mem
->memory_size
>> PAGE_SHIFT
;
926 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
928 new = old
= *memslot
;
930 new.base_gfn
= base_gfn
;
932 new.flags
= mem
->flags
;
934 /* Disallow changing a memory slot's size. */
936 if (npages
&& old
.npages
&& npages
!= old
.npages
)
939 /* Check for overlaps */
941 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
942 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
946 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
947 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
951 /* Free page dirty bitmap if unneeded */
952 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
953 new.dirty_bitmap
= NULL
;
957 /* Allocate if a slot is being created */
959 if (npages
&& !new.rmap
) {
960 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
965 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
967 new.user_alloc
= user_alloc
;
969 * hva_to_rmmap() serialzies with the mmu_lock and to be
970 * safe it has to ignore memslots with !user_alloc &&
974 new.userspace_addr
= mem
->userspace_addr
;
976 new.userspace_addr
= 0;
978 if (npages
&& !new.lpage_info
) {
979 int largepages
= npages
/ KVM_PAGES_PER_HPAGE
;
980 if (npages
% KVM_PAGES_PER_HPAGE
)
982 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
985 new.lpage_info
= vmalloc(largepages
* sizeof(*new.lpage_info
));
990 memset(new.lpage_info
, 0, largepages
* sizeof(*new.lpage_info
));
992 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
993 new.lpage_info
[0].write_count
= 1;
994 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE
)
995 new.lpage_info
[largepages
-1].write_count
= 1;
998 /* Allocate page dirty bitmap if needed */
999 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
1000 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
1002 new.dirty_bitmap
= vmalloc(dirty_bytes
);
1003 if (!new.dirty_bitmap
)
1005 memset(new.dirty_bitmap
, 0, dirty_bytes
);
1007 #endif /* not defined CONFIG_S390 */
1010 kvm_arch_flush_shadow(kvm
);
1012 spin_lock(&kvm
->mmu_lock
);
1013 if (mem
->slot
>= kvm
->nmemslots
)
1014 kvm
->nmemslots
= mem
->slot
+ 1;
1017 spin_unlock(&kvm
->mmu_lock
);
1019 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
1021 spin_lock(&kvm
->mmu_lock
);
1023 spin_unlock(&kvm
->mmu_lock
);
1027 kvm_free_physmem_slot(&old
, npages
? &new : NULL
);
1028 /* Slot deletion case: we have to update the current slot */
1032 /* map the pages in iommu page table */
1033 r
= kvm_iommu_map_pages(kvm
, base_gfn
, npages
);
1040 kvm_free_physmem_slot(&new, &old
);
1045 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
1047 int kvm_set_memory_region(struct kvm
*kvm
,
1048 struct kvm_userspace_memory_region
*mem
,
1053 down_write(&kvm
->slots_lock
);
1054 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
1055 up_write(&kvm
->slots_lock
);
1058 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
1060 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
1062 kvm_userspace_memory_region
*mem
,
1065 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
1067 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
1070 int kvm_get_dirty_log(struct kvm
*kvm
,
1071 struct kvm_dirty_log
*log
, int *is_dirty
)
1073 struct kvm_memory_slot
*memslot
;
1076 unsigned long any
= 0;
1079 if (log
->slot
>= KVM_MEMORY_SLOTS
)
1082 memslot
= &kvm
->memslots
[log
->slot
];
1084 if (!memslot
->dirty_bitmap
)
1087 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1089 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
1090 any
= memslot
->dirty_bitmap
[i
];
1093 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
1104 int is_error_page(struct page
*page
)
1106 return page
== bad_page
;
1108 EXPORT_SYMBOL_GPL(is_error_page
);
1110 int is_error_pfn(pfn_t pfn
)
1112 return pfn
== bad_pfn
;
1114 EXPORT_SYMBOL_GPL(is_error_pfn
);
1116 static inline unsigned long bad_hva(void)
1121 int kvm_is_error_hva(unsigned long addr
)
1123 return addr
== bad_hva();
1125 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
1127 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
1131 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1132 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1134 if (gfn
>= memslot
->base_gfn
1135 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1140 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
1142 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1144 gfn
= unalias_gfn(kvm
, gfn
);
1145 return gfn_to_memslot_unaliased(kvm
, gfn
);
1148 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
1152 gfn
= unalias_gfn(kvm
, gfn
);
1153 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1154 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1156 if (gfn
>= memslot
->base_gfn
1157 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1162 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
1164 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1166 struct kvm_memory_slot
*slot
;
1168 gfn
= unalias_gfn(kvm
, gfn
);
1169 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1172 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
1174 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1176 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1178 struct page
*page
[1];
1185 addr
= gfn_to_hva(kvm
, gfn
);
1186 if (kvm_is_error_hva(addr
)) {
1188 return page_to_pfn(bad_page
);
1191 npages
= get_user_pages_fast(addr
, 1, 1, page
);
1193 if (unlikely(npages
!= 1)) {
1194 struct vm_area_struct
*vma
;
1196 down_read(¤t
->mm
->mmap_sem
);
1197 vma
= find_vma(current
->mm
, addr
);
1199 if (vma
== NULL
|| addr
< vma
->vm_start
||
1200 !(vma
->vm_flags
& VM_PFNMAP
)) {
1201 up_read(¤t
->mm
->mmap_sem
);
1203 return page_to_pfn(bad_page
);
1206 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1207 up_read(¤t
->mm
->mmap_sem
);
1208 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1210 pfn
= page_to_pfn(page
[0]);
1215 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1217 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1221 pfn
= gfn_to_pfn(kvm
, gfn
);
1222 if (!kvm_is_mmio_pfn(pfn
))
1223 return pfn_to_page(pfn
);
1225 WARN_ON(kvm_is_mmio_pfn(pfn
));
1231 EXPORT_SYMBOL_GPL(gfn_to_page
);
1233 void kvm_release_page_clean(struct page
*page
)
1235 kvm_release_pfn_clean(page_to_pfn(page
));
1237 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1239 void kvm_release_pfn_clean(pfn_t pfn
)
1241 if (!kvm_is_mmio_pfn(pfn
))
1242 put_page(pfn_to_page(pfn
));
1244 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1246 void kvm_release_page_dirty(struct page
*page
)
1248 kvm_release_pfn_dirty(page_to_pfn(page
));
1250 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1252 void kvm_release_pfn_dirty(pfn_t pfn
)
1254 kvm_set_pfn_dirty(pfn
);
1255 kvm_release_pfn_clean(pfn
);
1257 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1259 void kvm_set_page_dirty(struct page
*page
)
1261 kvm_set_pfn_dirty(page_to_pfn(page
));
1263 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1265 void kvm_set_pfn_dirty(pfn_t pfn
)
1267 if (!kvm_is_mmio_pfn(pfn
)) {
1268 struct page
*page
= pfn_to_page(pfn
);
1269 if (!PageReserved(page
))
1273 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1275 void kvm_set_pfn_accessed(pfn_t pfn
)
1277 if (!kvm_is_mmio_pfn(pfn
))
1278 mark_page_accessed(pfn_to_page(pfn
));
1280 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1282 void kvm_get_pfn(pfn_t pfn
)
1284 if (!kvm_is_mmio_pfn(pfn
))
1285 get_page(pfn_to_page(pfn
));
1287 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1289 static int next_segment(unsigned long len
, int offset
)
1291 if (len
> PAGE_SIZE
- offset
)
1292 return PAGE_SIZE
- offset
;
1297 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1303 addr
= gfn_to_hva(kvm
, gfn
);
1304 if (kvm_is_error_hva(addr
))
1306 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1311 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1313 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1315 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1317 int offset
= offset_in_page(gpa
);
1320 while ((seg
= next_segment(len
, offset
)) != 0) {
1321 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1331 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1333 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1338 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1339 int offset
= offset_in_page(gpa
);
1341 addr
= gfn_to_hva(kvm
, gfn
);
1342 if (kvm_is_error_hva(addr
))
1344 pagefault_disable();
1345 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1351 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1353 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1354 int offset
, int len
)
1359 addr
= gfn_to_hva(kvm
, gfn
);
1360 if (kvm_is_error_hva(addr
))
1362 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1365 mark_page_dirty(kvm
, gfn
);
1368 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1370 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1373 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1375 int offset
= offset_in_page(gpa
);
1378 while ((seg
= next_segment(len
, offset
)) != 0) {
1379 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1390 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1392 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1394 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1396 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1398 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1400 int offset
= offset_in_page(gpa
);
1403 while ((seg
= next_segment(len
, offset
)) != 0) {
1404 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1413 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1415 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1417 struct kvm_memory_slot
*memslot
;
1419 gfn
= unalias_gfn(kvm
, gfn
);
1420 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1421 if (memslot
&& memslot
->dirty_bitmap
) {
1422 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1425 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1426 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1431 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1433 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1438 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1440 if (kvm_cpu_has_interrupt(vcpu
) ||
1441 kvm_cpu_has_pending_timer(vcpu
) ||
1442 kvm_arch_vcpu_runnable(vcpu
)) {
1443 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1446 if (signal_pending(current
))
1454 finish_wait(&vcpu
->wq
, &wait
);
1457 void kvm_resched(struct kvm_vcpu
*vcpu
)
1459 if (!need_resched())
1463 EXPORT_SYMBOL_GPL(kvm_resched
);
1465 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1467 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1470 if (vmf
->pgoff
== 0)
1471 page
= virt_to_page(vcpu
->run
);
1473 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1474 page
= virt_to_page(vcpu
->arch
.pio_data
);
1476 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1477 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1478 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1481 return VM_FAULT_SIGBUS
;
1487 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
1488 .fault
= kvm_vcpu_fault
,
1491 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1493 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1497 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1499 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1501 kvm_put_kvm(vcpu
->kvm
);
1505 static struct file_operations kvm_vcpu_fops
= {
1506 .release
= kvm_vcpu_release
,
1507 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1508 .compat_ioctl
= kvm_vcpu_ioctl
,
1509 .mmap
= kvm_vcpu_mmap
,
1513 * Allocates an inode for the vcpu.
1515 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1517 int fd
= anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, 0);
1519 kvm_put_kvm(vcpu
->kvm
);
1524 * Creates some virtual cpus. Good luck creating more than one.
1526 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
1529 struct kvm_vcpu
*vcpu
;
1534 vcpu
= kvm_arch_vcpu_create(kvm
, n
);
1536 return PTR_ERR(vcpu
);
1538 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1540 r
= kvm_arch_vcpu_setup(vcpu
);
1544 mutex_lock(&kvm
->lock
);
1545 if (kvm
->vcpus
[n
]) {
1549 kvm
->vcpus
[n
] = vcpu
;
1550 mutex_unlock(&kvm
->lock
);
1552 /* Now it's all set up, let userspace reach it */
1554 r
= create_vcpu_fd(vcpu
);
1560 mutex_lock(&kvm
->lock
);
1561 kvm
->vcpus
[n
] = NULL
;
1563 mutex_unlock(&kvm
->lock
);
1564 kvm_arch_vcpu_destroy(vcpu
);
1568 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1571 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1572 vcpu
->sigset_active
= 1;
1573 vcpu
->sigset
= *sigset
;
1575 vcpu
->sigset_active
= 0;
1579 static long kvm_vcpu_ioctl(struct file
*filp
,
1580 unsigned int ioctl
, unsigned long arg
)
1582 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1583 void __user
*argp
= (void __user
*)arg
;
1585 struct kvm_fpu
*fpu
= NULL
;
1586 struct kvm_sregs
*kvm_sregs
= NULL
;
1588 if (vcpu
->kvm
->mm
!= current
->mm
)
1595 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1597 case KVM_GET_REGS
: {
1598 struct kvm_regs
*kvm_regs
;
1601 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1604 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1608 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1615 case KVM_SET_REGS
: {
1616 struct kvm_regs
*kvm_regs
;
1619 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1623 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1625 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1633 case KVM_GET_SREGS
: {
1634 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1638 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1642 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1647 case KVM_SET_SREGS
: {
1648 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1653 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1655 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1661 case KVM_GET_MP_STATE
: {
1662 struct kvm_mp_state mp_state
;
1664 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1668 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1673 case KVM_SET_MP_STATE
: {
1674 struct kvm_mp_state mp_state
;
1677 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1679 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1685 case KVM_TRANSLATE
: {
1686 struct kvm_translation tr
;
1689 if (copy_from_user(&tr
, argp
, sizeof tr
))
1691 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1695 if (copy_to_user(argp
, &tr
, sizeof tr
))
1700 case KVM_DEBUG_GUEST
: {
1701 struct kvm_debug_guest dbg
;
1704 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1706 r
= kvm_arch_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
1712 case KVM_SET_SIGNAL_MASK
: {
1713 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1714 struct kvm_signal_mask kvm_sigmask
;
1715 sigset_t sigset
, *p
;
1720 if (copy_from_user(&kvm_sigmask
, argp
,
1721 sizeof kvm_sigmask
))
1724 if (kvm_sigmask
.len
!= sizeof sigset
)
1727 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1732 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1736 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1740 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1744 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1750 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1755 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1757 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1764 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1772 static long kvm_vm_ioctl(struct file
*filp
,
1773 unsigned int ioctl
, unsigned long arg
)
1775 struct kvm
*kvm
= filp
->private_data
;
1776 void __user
*argp
= (void __user
*)arg
;
1779 if (kvm
->mm
!= current
->mm
)
1782 case KVM_CREATE_VCPU
:
1783 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1787 case KVM_SET_USER_MEMORY_REGION
: {
1788 struct kvm_userspace_memory_region kvm_userspace_mem
;
1791 if (copy_from_user(&kvm_userspace_mem
, argp
,
1792 sizeof kvm_userspace_mem
))
1795 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1800 case KVM_GET_DIRTY_LOG
: {
1801 struct kvm_dirty_log log
;
1804 if (copy_from_user(&log
, argp
, sizeof log
))
1806 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1811 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1812 case KVM_REGISTER_COALESCED_MMIO
: {
1813 struct kvm_coalesced_mmio_zone zone
;
1815 if (copy_from_user(&zone
, argp
, sizeof zone
))
1818 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1824 case KVM_UNREGISTER_COALESCED_MMIO
: {
1825 struct kvm_coalesced_mmio_zone zone
;
1827 if (copy_from_user(&zone
, argp
, sizeof zone
))
1830 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1837 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1838 case KVM_ASSIGN_PCI_DEVICE
: {
1839 struct kvm_assigned_pci_dev assigned_dev
;
1842 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1844 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
1849 case KVM_ASSIGN_IRQ
: {
1850 struct kvm_assigned_irq assigned_irq
;
1853 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
1855 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
1862 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1868 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1870 struct page
*page
[1];
1873 gfn_t gfn
= vmf
->pgoff
;
1874 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1876 addr
= gfn_to_hva(kvm
, gfn
);
1877 if (kvm_is_error_hva(addr
))
1878 return VM_FAULT_SIGBUS
;
1880 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1882 if (unlikely(npages
!= 1))
1883 return VM_FAULT_SIGBUS
;
1885 vmf
->page
= page
[0];
1889 static struct vm_operations_struct kvm_vm_vm_ops
= {
1890 .fault
= kvm_vm_fault
,
1893 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1895 vma
->vm_ops
= &kvm_vm_vm_ops
;
1899 static struct file_operations kvm_vm_fops
= {
1900 .release
= kvm_vm_release
,
1901 .unlocked_ioctl
= kvm_vm_ioctl
,
1902 .compat_ioctl
= kvm_vm_ioctl
,
1903 .mmap
= kvm_vm_mmap
,
1906 static int kvm_dev_ioctl_create_vm(void)
1911 kvm
= kvm_create_vm();
1913 return PTR_ERR(kvm
);
1914 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, 0);
1921 static long kvm_dev_ioctl_check_extension_generic(long arg
)
1924 case KVM_CAP_USER_MEMORY
:
1925 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
1930 return kvm_dev_ioctl_check_extension(arg
);
1933 static long kvm_dev_ioctl(struct file
*filp
,
1934 unsigned int ioctl
, unsigned long arg
)
1939 case KVM_GET_API_VERSION
:
1943 r
= KVM_API_VERSION
;
1949 r
= kvm_dev_ioctl_create_vm();
1951 case KVM_CHECK_EXTENSION
:
1952 r
= kvm_dev_ioctl_check_extension_generic(arg
);
1954 case KVM_GET_VCPU_MMAP_SIZE
:
1958 r
= PAGE_SIZE
; /* struct kvm_run */
1960 r
+= PAGE_SIZE
; /* pio data page */
1962 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1963 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1966 case KVM_TRACE_ENABLE
:
1967 case KVM_TRACE_PAUSE
:
1968 case KVM_TRACE_DISABLE
:
1969 r
= kvm_trace_ioctl(ioctl
, arg
);
1972 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1978 static struct file_operations kvm_chardev_ops
= {
1979 .unlocked_ioctl
= kvm_dev_ioctl
,
1980 .compat_ioctl
= kvm_dev_ioctl
,
1983 static struct miscdevice kvm_dev
= {
1989 static void hardware_enable(void *junk
)
1991 int cpu
= raw_smp_processor_id();
1993 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1995 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
1996 kvm_arch_hardware_enable(NULL
);
1999 static void hardware_disable(void *junk
)
2001 int cpu
= raw_smp_processor_id();
2003 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2005 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2006 kvm_arch_hardware_disable(NULL
);
2009 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2014 val
&= ~CPU_TASKS_FROZEN
;
2017 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2019 hardware_disable(NULL
);
2021 case CPU_UP_CANCELED
:
2022 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2024 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
2027 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2029 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
2036 asmlinkage
void kvm_handle_fault_on_reboot(void)
2039 /* spin while reset goes on */
2042 /* Fault while not rebooting. We want the trace. */
2045 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
2047 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2050 if (val
== SYS_RESTART
) {
2052 * Some (well, at least mine) BIOSes hang on reboot if
2055 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2056 kvm_rebooting
= true;
2057 on_each_cpu(hardware_disable
, NULL
, 1);
2062 static struct notifier_block kvm_reboot_notifier
= {
2063 .notifier_call
= kvm_reboot
,
2067 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
2069 memset(bus
, 0, sizeof(*bus
));
2072 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2076 for (i
= 0; i
< bus
->dev_count
; i
++) {
2077 struct kvm_io_device
*pos
= bus
->devs
[i
];
2079 kvm_iodevice_destructor(pos
);
2083 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
,
2084 gpa_t addr
, int len
, int is_write
)
2088 for (i
= 0; i
< bus
->dev_count
; i
++) {
2089 struct kvm_io_device
*pos
= bus
->devs
[i
];
2091 if (pos
->in_range(pos
, addr
, len
, is_write
))
2098 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
2100 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
2102 bus
->devs
[bus
->dev_count
++] = dev
;
2105 static struct notifier_block kvm_cpu_notifier
= {
2106 .notifier_call
= kvm_cpu_hotplug
,
2107 .priority
= 20, /* must be > scheduler priority */
2110 static int vm_stat_get(void *_offset
, u64
*val
)
2112 unsigned offset
= (long)_offset
;
2116 spin_lock(&kvm_lock
);
2117 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2118 *val
+= *(u32
*)((void *)kvm
+ offset
);
2119 spin_unlock(&kvm_lock
);
2123 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2125 static int vcpu_stat_get(void *_offset
, u64
*val
)
2127 unsigned offset
= (long)_offset
;
2129 struct kvm_vcpu
*vcpu
;
2133 spin_lock(&kvm_lock
);
2134 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2135 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2136 vcpu
= kvm
->vcpus
[i
];
2138 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2140 spin_unlock(&kvm_lock
);
2144 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2146 static struct file_operations
*stat_fops
[] = {
2147 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2148 [KVM_STAT_VM
] = &vm_stat_fops
,
2151 static void kvm_init_debug(void)
2153 struct kvm_stats_debugfs_item
*p
;
2155 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2156 for (p
= debugfs_entries
; p
->name
; ++p
)
2157 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2158 (void *)(long)p
->offset
,
2159 stat_fops
[p
->kind
]);
2162 static void kvm_exit_debug(void)
2164 struct kvm_stats_debugfs_item
*p
;
2166 for (p
= debugfs_entries
; p
->name
; ++p
)
2167 debugfs_remove(p
->dentry
);
2168 debugfs_remove(kvm_debugfs_dir
);
2171 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2173 hardware_disable(NULL
);
2177 static int kvm_resume(struct sys_device
*dev
)
2179 hardware_enable(NULL
);
2183 static struct sysdev_class kvm_sysdev_class
= {
2185 .suspend
= kvm_suspend
,
2186 .resume
= kvm_resume
,
2189 static struct sys_device kvm_sysdev
= {
2191 .cls
= &kvm_sysdev_class
,
2194 struct page
*bad_page
;
2198 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2200 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2203 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2205 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2207 kvm_arch_vcpu_load(vcpu
, cpu
);
2210 static void kvm_sched_out(struct preempt_notifier
*pn
,
2211 struct task_struct
*next
)
2213 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2215 kvm_arch_vcpu_put(vcpu
);
2218 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2219 struct module
*module
)
2226 r
= kvm_arch_init(opaque
);
2230 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2232 if (bad_page
== NULL
) {
2237 bad_pfn
= page_to_pfn(bad_page
);
2239 if (!alloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2244 r
= kvm_arch_hardware_setup();
2248 for_each_online_cpu(cpu
) {
2249 smp_call_function_single(cpu
,
2250 kvm_arch_check_processor_compat
,
2256 on_each_cpu(hardware_enable
, NULL
, 1);
2257 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2260 register_reboot_notifier(&kvm_reboot_notifier
);
2262 r
= sysdev_class_register(&kvm_sysdev_class
);
2266 r
= sysdev_register(&kvm_sysdev
);
2270 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2271 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2272 __alignof__(struct kvm_vcpu
),
2274 if (!kvm_vcpu_cache
) {
2279 kvm_chardev_ops
.owner
= module
;
2280 kvm_vm_fops
.owner
= module
;
2281 kvm_vcpu_fops
.owner
= module
;
2283 r
= misc_register(&kvm_dev
);
2285 printk(KERN_ERR
"kvm: misc device register failed\n");
2289 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2290 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2298 kmem_cache_destroy(kvm_vcpu_cache
);
2300 sysdev_unregister(&kvm_sysdev
);
2302 sysdev_class_unregister(&kvm_sysdev_class
);
2304 unregister_reboot_notifier(&kvm_reboot_notifier
);
2305 unregister_cpu_notifier(&kvm_cpu_notifier
);
2307 on_each_cpu(hardware_disable
, NULL
, 1);
2309 kvm_arch_hardware_unsetup();
2311 free_cpumask_var(cpus_hardware_enabled
);
2313 __free_page(bad_page
);
2320 EXPORT_SYMBOL_GPL(kvm_init
);
2324 kvm_trace_cleanup();
2325 misc_deregister(&kvm_dev
);
2326 kmem_cache_destroy(kvm_vcpu_cache
);
2327 sysdev_unregister(&kvm_sysdev
);
2328 sysdev_class_unregister(&kvm_sysdev_class
);
2329 unregister_reboot_notifier(&kvm_reboot_notifier
);
2330 unregister_cpu_notifier(&kvm_cpu_notifier
);
2331 on_each_cpu(hardware_disable
, NULL
, 1);
2332 kvm_arch_hardware_unsetup();
2335 free_cpumask_var(cpus_hardware_enabled
);
2336 __free_page(bad_page
);
2338 EXPORT_SYMBOL_GPL(kvm_exit
);