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 DEFINE_SPINLOCK(kvm_lock
);
70 static cpumask_t cpus_hardware_enabled
;
72 struct kmem_cache
*kvm_vcpu_cache
;
73 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
75 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
77 struct dentry
*kvm_debugfs_dir
;
79 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
84 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
87 static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel
*dev
)
90 struct kvm_vcpu
*vcpu
;
91 struct kvm_ioapic
*ioapic
= ioapic_irqchip(dev
->kvm
);
92 int dest_id
= (dev
->guest_msi
.address_lo
& MSI_ADDR_DEST_ID_MASK
)
93 >> MSI_ADDR_DEST_ID_SHIFT
;
94 int vector
= (dev
->guest_msi
.data
& MSI_DATA_VECTOR_MASK
)
95 >> MSI_DATA_VECTOR_SHIFT
;
96 int dest_mode
= test_bit(MSI_ADDR_DEST_MODE_SHIFT
,
97 (unsigned long *)&dev
->guest_msi
.address_lo
);
98 int trig_mode
= test_bit(MSI_DATA_TRIGGER_SHIFT
,
99 (unsigned long *)&dev
->guest_msi
.data
);
100 int delivery_mode
= test_bit(MSI_DATA_DELIVERY_MODE_SHIFT
,
101 (unsigned long *)&dev
->guest_msi
.data
);
106 deliver_bitmask
= kvm_ioapic_get_delivery_bitmask(ioapic
,
108 /* IOAPIC delivery mode value is the same as MSI here */
109 switch (delivery_mode
) {
110 case IOAPIC_LOWEST_PRIORITY
:
111 vcpu
= kvm_get_lowest_prio_vcpu(ioapic
->kvm
, vector
,
114 kvm_apic_set_irq(vcpu
, vector
, trig_mode
);
116 printk(KERN_INFO
"kvm: null lowest priority vcpu!\n");
119 for (vcpu_id
= 0; deliver_bitmask
!= 0; vcpu_id
++) {
120 if (!(deliver_bitmask
& (1 << vcpu_id
)))
122 deliver_bitmask
&= ~(1 << vcpu_id
);
123 vcpu
= ioapic
->kvm
->vcpus
[vcpu_id
];
125 kvm_apic_set_irq(vcpu
, vector
, trig_mode
);
129 printk(KERN_INFO
"kvm: unsupported MSI delivery mode\n");
133 static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel
*dev
) {}
136 static struct kvm_assigned_dev_kernel
*kvm_find_assigned_dev(struct list_head
*head
,
139 struct list_head
*ptr
;
140 struct kvm_assigned_dev_kernel
*match
;
142 list_for_each(ptr
, head
) {
143 match
= list_entry(ptr
, struct kvm_assigned_dev_kernel
, list
);
144 if (match
->assigned_dev_id
== assigned_dev_id
)
150 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct
*work
)
152 struct kvm_assigned_dev_kernel
*assigned_dev
;
154 assigned_dev
= container_of(work
, struct kvm_assigned_dev_kernel
,
157 /* This is taken to safely inject irq inside the guest. When
158 * the interrupt injection (or the ioapic code) uses a
159 * finer-grained lock, update this
161 mutex_lock(&assigned_dev
->kvm
->lock
);
162 kvm_set_irq(assigned_dev
->kvm
,
163 assigned_dev
->irq_source_id
,
164 assigned_dev
->guest_irq
, 1);
165 mutex_unlock(&assigned_dev
->kvm
->lock
);
166 kvm_put_kvm(assigned_dev
->kvm
);
169 static irqreturn_t
kvm_assigned_dev_intr(int irq
, void *dev_id
)
171 struct kvm_assigned_dev_kernel
*assigned_dev
=
172 (struct kvm_assigned_dev_kernel
*) dev_id
;
174 kvm_get_kvm(assigned_dev
->kvm
);
175 schedule_work(&assigned_dev
->interrupt_work
);
176 disable_irq_nosync(irq
);
180 /* Ack the irq line for an assigned device */
181 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
183 struct kvm_assigned_dev_kernel
*dev
;
188 dev
= container_of(kian
, struct kvm_assigned_dev_kernel
,
190 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
191 enable_irq(dev
->host_irq
);
194 static void kvm_free_assigned_device(struct kvm
*kvm
,
195 struct kvm_assigned_dev_kernel
198 if (irqchip_in_kernel(kvm
) && assigned_dev
->irq_requested_type
)
199 free_irq(assigned_dev
->host_irq
, (void *)assigned_dev
);
201 kvm_unregister_irq_ack_notifier(&assigned_dev
->ack_notifier
);
202 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
204 if (cancel_work_sync(&assigned_dev
->interrupt_work
))
205 /* We had pending work. That means we will have to take
206 * care of kvm_put_kvm.
210 pci_reset_function(assigned_dev
->dev
);
212 pci_release_regions(assigned_dev
->dev
);
213 pci_disable_device(assigned_dev
->dev
);
214 pci_dev_put(assigned_dev
->dev
);
216 list_del(&assigned_dev
->list
);
220 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
222 struct list_head
*ptr
, *ptr2
;
223 struct kvm_assigned_dev_kernel
*assigned_dev
;
225 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
226 assigned_dev
= list_entry(ptr
,
227 struct kvm_assigned_dev_kernel
,
230 kvm_free_assigned_device(kvm
, assigned_dev
);
234 static int assigned_device_update_intx(struct kvm
*kvm
,
235 struct kvm_assigned_dev_kernel
*adev
,
236 struct kvm_assigned_irq
*airq
)
238 adev
->guest_irq
= airq
->guest_irq
;
239 adev
->ack_notifier
.gsi
= airq
->guest_irq
;
241 if (adev
->irq_requested_type
& KVM_ASSIGNED_DEV_HOST_INTX
)
244 if (irqchip_in_kernel(kvm
)) {
245 if (!capable(CAP_SYS_RAWIO
))
249 adev
->host_irq
= airq
->host_irq
;
251 adev
->host_irq
= adev
->dev
->irq
;
253 /* Even though this is PCI, we don't want to use shared
254 * interrupts. Sharing host devices with guest-assigned devices
255 * on the same interrupt line is not a happy situation: there
256 * are going to be long delays in accepting, acking, etc.
258 if (request_irq(adev
->host_irq
, kvm_assigned_dev_intr
,
259 0, "kvm_assigned_intx_device", (void *)adev
))
263 adev
->irq_requested_type
= KVM_ASSIGNED_DEV_GUEST_INTX
|
264 KVM_ASSIGNED_DEV_HOST_INTX
;
268 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
269 struct kvm_assigned_irq
273 struct kvm_assigned_dev_kernel
*match
;
275 mutex_lock(&kvm
->lock
);
277 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
278 assigned_irq
->assigned_dev_id
);
280 mutex_unlock(&kvm
->lock
);
284 if (!match
->irq_requested_type
) {
285 INIT_WORK(&match
->interrupt_work
,
286 kvm_assigned_dev_interrupt_work_handler
);
287 if (irqchip_in_kernel(kvm
)) {
288 /* Register ack nofitier */
289 match
->ack_notifier
.gsi
= -1;
290 match
->ack_notifier
.irq_acked
=
291 kvm_assigned_dev_ack_irq
;
292 kvm_register_irq_ack_notifier(kvm
,
293 &match
->ack_notifier
);
295 /* Request IRQ source ID */
296 r
= kvm_request_irq_source_id(kvm
);
300 match
->irq_source_id
= r
;
304 r
= assigned_device_update_intx(kvm
, match
, assigned_irq
);
308 mutex_unlock(&kvm
->lock
);
311 mutex_unlock(&kvm
->lock
);
312 kvm_free_assigned_device(kvm
, match
);
316 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
317 struct kvm_assigned_pci_dev
*assigned_dev
)
320 struct kvm_assigned_dev_kernel
*match
;
323 mutex_lock(&kvm
->lock
);
325 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
326 assigned_dev
->assigned_dev_id
);
328 /* device already assigned */
333 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
335 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
340 dev
= pci_get_bus_and_slot(assigned_dev
->busnr
,
341 assigned_dev
->devfn
);
343 printk(KERN_INFO
"%s: host device not found\n", __func__
);
347 if (pci_enable_device(dev
)) {
348 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
352 r
= pci_request_regions(dev
, "kvm_assigned_device");
354 printk(KERN_INFO
"%s: Could not get access to device regions\n",
359 pci_reset_function(dev
);
361 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
362 match
->host_busnr
= assigned_dev
->busnr
;
363 match
->host_devfn
= assigned_dev
->devfn
;
368 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
370 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
) {
371 r
= kvm_iommu_map_guest(kvm
, match
);
377 mutex_unlock(&kvm
->lock
);
380 list_del(&match
->list
);
381 pci_release_regions(dev
);
383 pci_disable_device(dev
);
388 mutex_unlock(&kvm
->lock
);
393 static inline int valid_vcpu(int n
)
395 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
398 inline int kvm_is_mmio_pfn(pfn_t pfn
)
401 return PageReserved(pfn_to_page(pfn
));
407 * Switches to specified vcpu, until a matching vcpu_put()
409 void vcpu_load(struct kvm_vcpu
*vcpu
)
413 mutex_lock(&vcpu
->mutex
);
415 preempt_notifier_register(&vcpu
->preempt_notifier
);
416 kvm_arch_vcpu_load(vcpu
, cpu
);
420 void vcpu_put(struct kvm_vcpu
*vcpu
)
423 kvm_arch_vcpu_put(vcpu
);
424 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
426 mutex_unlock(&vcpu
->mutex
);
429 static void ack_flush(void *_completed
)
433 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
437 struct kvm_vcpu
*vcpu
;
441 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
442 vcpu
= kvm
->vcpus
[i
];
445 if (test_and_set_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
448 if (cpu
!= -1 && cpu
!= me
)
451 if (cpus_empty(cpus
))
453 ++kvm
->stat
.remote_tlb_flush
;
454 smp_call_function_mask(cpus
, ack_flush
, NULL
, 1);
459 void kvm_reload_remote_mmus(struct kvm
*kvm
)
463 struct kvm_vcpu
*vcpu
;
467 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
468 vcpu
= kvm
->vcpus
[i
];
471 if (test_and_set_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
474 if (cpu
!= -1 && cpu
!= me
)
477 if (cpus_empty(cpus
))
479 smp_call_function_mask(cpus
, ack_flush
, NULL
, 1);
485 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
490 mutex_init(&vcpu
->mutex
);
494 init_waitqueue_head(&vcpu
->wq
);
496 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
501 vcpu
->run
= page_address(page
);
503 r
= kvm_arch_vcpu_init(vcpu
);
509 free_page((unsigned long)vcpu
->run
);
513 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
515 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
517 kvm_arch_vcpu_uninit(vcpu
);
518 free_page((unsigned long)vcpu
->run
);
520 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
522 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
523 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
525 return container_of(mn
, struct kvm
, mmu_notifier
);
528 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
529 struct mm_struct
*mm
,
530 unsigned long address
)
532 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
536 * When ->invalidate_page runs, the linux pte has been zapped
537 * already but the page is still allocated until
538 * ->invalidate_page returns. So if we increase the sequence
539 * here the kvm page fault will notice if the spte can't be
540 * established because the page is going to be freed. If
541 * instead the kvm page fault establishes the spte before
542 * ->invalidate_page runs, kvm_unmap_hva will release it
545 * The sequence increase only need to be seen at spin_unlock
546 * time, and not at spin_lock time.
548 * Increasing the sequence after the spin_unlock would be
549 * unsafe because the kvm page fault could then establish the
550 * pte after kvm_unmap_hva returned, without noticing the page
551 * is going to be freed.
553 spin_lock(&kvm
->mmu_lock
);
554 kvm
->mmu_notifier_seq
++;
555 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
556 spin_unlock(&kvm
->mmu_lock
);
558 /* we've to flush the tlb before the pages can be freed */
560 kvm_flush_remote_tlbs(kvm
);
564 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
565 struct mm_struct
*mm
,
569 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
570 int need_tlb_flush
= 0;
572 spin_lock(&kvm
->mmu_lock
);
574 * The count increase must become visible at unlock time as no
575 * spte can be established without taking the mmu_lock and
576 * count is also read inside the mmu_lock critical section.
578 kvm
->mmu_notifier_count
++;
579 for (; start
< end
; start
+= PAGE_SIZE
)
580 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
581 spin_unlock(&kvm
->mmu_lock
);
583 /* we've to flush the tlb before the pages can be freed */
585 kvm_flush_remote_tlbs(kvm
);
588 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
589 struct mm_struct
*mm
,
593 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
595 spin_lock(&kvm
->mmu_lock
);
597 * This sequence increase will notify the kvm page fault that
598 * the page that is going to be mapped in the spte could have
601 kvm
->mmu_notifier_seq
++;
603 * The above sequence increase must be visible before the
604 * below count decrease but both values are read by the kvm
605 * page fault under mmu_lock spinlock so we don't need to add
606 * a smb_wmb() here in between the two.
608 kvm
->mmu_notifier_count
--;
609 spin_unlock(&kvm
->mmu_lock
);
611 BUG_ON(kvm
->mmu_notifier_count
< 0);
614 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
615 struct mm_struct
*mm
,
616 unsigned long address
)
618 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
621 spin_lock(&kvm
->mmu_lock
);
622 young
= kvm_age_hva(kvm
, address
);
623 spin_unlock(&kvm
->mmu_lock
);
626 kvm_flush_remote_tlbs(kvm
);
631 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
632 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
633 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
634 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
635 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
637 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
639 static struct kvm
*kvm_create_vm(void)
641 struct kvm
*kvm
= kvm_arch_create_vm();
642 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
649 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
650 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
653 return ERR_PTR(-ENOMEM
);
655 kvm
->coalesced_mmio_ring
=
656 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
659 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
662 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
663 err
= mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
665 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
674 kvm
->mm
= current
->mm
;
675 atomic_inc(&kvm
->mm
->mm_count
);
676 spin_lock_init(&kvm
->mmu_lock
);
677 kvm_io_bus_init(&kvm
->pio_bus
);
678 mutex_init(&kvm
->lock
);
679 kvm_io_bus_init(&kvm
->mmio_bus
);
680 init_rwsem(&kvm
->slots_lock
);
681 atomic_set(&kvm
->users_count
, 1);
682 spin_lock(&kvm_lock
);
683 list_add(&kvm
->vm_list
, &vm_list
);
684 spin_unlock(&kvm_lock
);
685 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
686 kvm_coalesced_mmio_init(kvm
);
693 * Free any memory in @free but not in @dont.
695 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
696 struct kvm_memory_slot
*dont
)
698 if (!dont
|| free
->rmap
!= dont
->rmap
)
701 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
702 vfree(free
->dirty_bitmap
);
704 if (!dont
|| free
->lpage_info
!= dont
->lpage_info
)
705 vfree(free
->lpage_info
);
708 free
->dirty_bitmap
= NULL
;
710 free
->lpage_info
= NULL
;
713 void kvm_free_physmem(struct kvm
*kvm
)
717 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
718 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
721 static void kvm_destroy_vm(struct kvm
*kvm
)
723 struct mm_struct
*mm
= kvm
->mm
;
725 spin_lock(&kvm_lock
);
726 list_del(&kvm
->vm_list
);
727 spin_unlock(&kvm_lock
);
728 kvm_io_bus_destroy(&kvm
->pio_bus
);
729 kvm_io_bus_destroy(&kvm
->mmio_bus
);
730 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
731 if (kvm
->coalesced_mmio_ring
!= NULL
)
732 free_page((unsigned long)kvm
->coalesced_mmio_ring
);
734 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
735 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
737 kvm_arch_destroy_vm(kvm
);
741 void kvm_get_kvm(struct kvm
*kvm
)
743 atomic_inc(&kvm
->users_count
);
745 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
747 void kvm_put_kvm(struct kvm
*kvm
)
749 if (atomic_dec_and_test(&kvm
->users_count
))
752 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
755 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
757 struct kvm
*kvm
= filp
->private_data
;
764 * Allocate some memory and give it an address in the guest physical address
767 * Discontiguous memory is allowed, mostly for framebuffers.
769 * Must be called holding mmap_sem for write.
771 int __kvm_set_memory_region(struct kvm
*kvm
,
772 struct kvm_userspace_memory_region
*mem
,
777 unsigned long npages
;
779 struct kvm_memory_slot
*memslot
;
780 struct kvm_memory_slot old
, new;
783 /* General sanity checks */
784 if (mem
->memory_size
& (PAGE_SIZE
- 1))
786 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
788 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
790 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
792 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
795 memslot
= &kvm
->memslots
[mem
->slot
];
796 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
797 npages
= mem
->memory_size
>> PAGE_SHIFT
;
800 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
802 new = old
= *memslot
;
804 new.base_gfn
= base_gfn
;
806 new.flags
= mem
->flags
;
808 /* Disallow changing a memory slot's size. */
810 if (npages
&& old
.npages
&& npages
!= old
.npages
)
813 /* Check for overlaps */
815 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
816 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
820 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
821 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
825 /* Free page dirty bitmap if unneeded */
826 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
827 new.dirty_bitmap
= NULL
;
831 /* Allocate if a slot is being created */
833 if (npages
&& !new.rmap
) {
834 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
839 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
841 new.user_alloc
= user_alloc
;
843 * hva_to_rmmap() serialzies with the mmu_lock and to be
844 * safe it has to ignore memslots with !user_alloc &&
848 new.userspace_addr
= mem
->userspace_addr
;
850 new.userspace_addr
= 0;
852 if (npages
&& !new.lpage_info
) {
853 int largepages
= npages
/ KVM_PAGES_PER_HPAGE
;
854 if (npages
% KVM_PAGES_PER_HPAGE
)
856 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
859 new.lpage_info
= vmalloc(largepages
* sizeof(*new.lpage_info
));
864 memset(new.lpage_info
, 0, largepages
* sizeof(*new.lpage_info
));
866 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
867 new.lpage_info
[0].write_count
= 1;
868 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE
)
869 new.lpage_info
[largepages
-1].write_count
= 1;
872 /* Allocate page dirty bitmap if needed */
873 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
874 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
876 new.dirty_bitmap
= vmalloc(dirty_bytes
);
877 if (!new.dirty_bitmap
)
879 memset(new.dirty_bitmap
, 0, dirty_bytes
);
881 #endif /* not defined CONFIG_S390 */
884 kvm_arch_flush_shadow(kvm
);
886 spin_lock(&kvm
->mmu_lock
);
887 if (mem
->slot
>= kvm
->nmemslots
)
888 kvm
->nmemslots
= mem
->slot
+ 1;
891 spin_unlock(&kvm
->mmu_lock
);
893 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
895 spin_lock(&kvm
->mmu_lock
);
897 spin_unlock(&kvm
->mmu_lock
);
901 kvm_free_physmem_slot(&old
, &new);
903 /* map the pages in iommu page table */
904 r
= kvm_iommu_map_pages(kvm
, base_gfn
, npages
);
911 kvm_free_physmem_slot(&new, &old
);
916 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
918 int kvm_set_memory_region(struct kvm
*kvm
,
919 struct kvm_userspace_memory_region
*mem
,
924 down_write(&kvm
->slots_lock
);
925 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
926 up_write(&kvm
->slots_lock
);
929 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
931 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
933 kvm_userspace_memory_region
*mem
,
936 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
938 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
941 int kvm_get_dirty_log(struct kvm
*kvm
,
942 struct kvm_dirty_log
*log
, int *is_dirty
)
944 struct kvm_memory_slot
*memslot
;
947 unsigned long any
= 0;
950 if (log
->slot
>= KVM_MEMORY_SLOTS
)
953 memslot
= &kvm
->memslots
[log
->slot
];
955 if (!memslot
->dirty_bitmap
)
958 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
960 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
961 any
= memslot
->dirty_bitmap
[i
];
964 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
975 int is_error_page(struct page
*page
)
977 return page
== bad_page
;
979 EXPORT_SYMBOL_GPL(is_error_page
);
981 int is_error_pfn(pfn_t pfn
)
983 return pfn
== bad_pfn
;
985 EXPORT_SYMBOL_GPL(is_error_pfn
);
987 static inline unsigned long bad_hva(void)
992 int kvm_is_error_hva(unsigned long addr
)
994 return addr
== bad_hva();
996 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
998 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
1002 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1003 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1005 if (gfn
>= memslot
->base_gfn
1006 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1011 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
1013 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1015 gfn
= unalias_gfn(kvm
, gfn
);
1016 return gfn_to_memslot_unaliased(kvm
, gfn
);
1019 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
1023 gfn
= unalias_gfn(kvm
, gfn
);
1024 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1025 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1027 if (gfn
>= memslot
->base_gfn
1028 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1033 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
1035 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1037 struct kvm_memory_slot
*slot
;
1039 gfn
= unalias_gfn(kvm
, gfn
);
1040 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1043 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
1045 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1047 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1049 struct page
*page
[1];
1056 addr
= gfn_to_hva(kvm
, gfn
);
1057 if (kvm_is_error_hva(addr
)) {
1059 return page_to_pfn(bad_page
);
1062 npages
= get_user_pages_fast(addr
, 1, 1, page
);
1064 if (unlikely(npages
!= 1)) {
1065 struct vm_area_struct
*vma
;
1067 down_read(¤t
->mm
->mmap_sem
);
1068 vma
= find_vma(current
->mm
, addr
);
1070 if (vma
== NULL
|| addr
< vma
->vm_start
||
1071 !(vma
->vm_flags
& VM_PFNMAP
)) {
1072 up_read(¤t
->mm
->mmap_sem
);
1074 return page_to_pfn(bad_page
);
1077 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1078 up_read(¤t
->mm
->mmap_sem
);
1079 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1081 pfn
= page_to_pfn(page
[0]);
1086 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1088 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1092 pfn
= gfn_to_pfn(kvm
, gfn
);
1093 if (!kvm_is_mmio_pfn(pfn
))
1094 return pfn_to_page(pfn
);
1096 WARN_ON(kvm_is_mmio_pfn(pfn
));
1102 EXPORT_SYMBOL_GPL(gfn_to_page
);
1104 void kvm_release_page_clean(struct page
*page
)
1106 kvm_release_pfn_clean(page_to_pfn(page
));
1108 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1110 void kvm_release_pfn_clean(pfn_t pfn
)
1112 if (!kvm_is_mmio_pfn(pfn
))
1113 put_page(pfn_to_page(pfn
));
1115 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1117 void kvm_release_page_dirty(struct page
*page
)
1119 kvm_release_pfn_dirty(page_to_pfn(page
));
1121 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1123 void kvm_release_pfn_dirty(pfn_t pfn
)
1125 kvm_set_pfn_dirty(pfn
);
1126 kvm_release_pfn_clean(pfn
);
1128 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1130 void kvm_set_page_dirty(struct page
*page
)
1132 kvm_set_pfn_dirty(page_to_pfn(page
));
1134 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1136 void kvm_set_pfn_dirty(pfn_t pfn
)
1138 if (!kvm_is_mmio_pfn(pfn
)) {
1139 struct page
*page
= pfn_to_page(pfn
);
1140 if (!PageReserved(page
))
1144 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1146 void kvm_set_pfn_accessed(pfn_t pfn
)
1148 if (!kvm_is_mmio_pfn(pfn
))
1149 mark_page_accessed(pfn_to_page(pfn
));
1151 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1153 void kvm_get_pfn(pfn_t pfn
)
1155 if (!kvm_is_mmio_pfn(pfn
))
1156 get_page(pfn_to_page(pfn
));
1158 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1160 static int next_segment(unsigned long len
, int offset
)
1162 if (len
> PAGE_SIZE
- offset
)
1163 return PAGE_SIZE
- offset
;
1168 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1174 addr
= gfn_to_hva(kvm
, gfn
);
1175 if (kvm_is_error_hva(addr
))
1177 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1182 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1184 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1186 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1188 int offset
= offset_in_page(gpa
);
1191 while ((seg
= next_segment(len
, offset
)) != 0) {
1192 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1202 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1204 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1209 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1210 int offset
= offset_in_page(gpa
);
1212 addr
= gfn_to_hva(kvm
, gfn
);
1213 if (kvm_is_error_hva(addr
))
1215 pagefault_disable();
1216 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1222 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1224 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1225 int offset
, int len
)
1230 addr
= gfn_to_hva(kvm
, gfn
);
1231 if (kvm_is_error_hva(addr
))
1233 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1236 mark_page_dirty(kvm
, gfn
);
1239 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1241 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1244 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1246 int offset
= offset_in_page(gpa
);
1249 while ((seg
= next_segment(len
, offset
)) != 0) {
1250 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1261 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1263 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1265 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1267 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1269 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1271 int offset
= offset_in_page(gpa
);
1274 while ((seg
= next_segment(len
, offset
)) != 0) {
1275 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1284 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1286 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1288 struct kvm_memory_slot
*memslot
;
1290 gfn
= unalias_gfn(kvm
, gfn
);
1291 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1292 if (memslot
&& memslot
->dirty_bitmap
) {
1293 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1296 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1297 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1302 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1304 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1309 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1311 if (kvm_cpu_has_interrupt(vcpu
) ||
1312 kvm_cpu_has_pending_timer(vcpu
) ||
1313 kvm_arch_vcpu_runnable(vcpu
)) {
1314 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1317 if (signal_pending(current
))
1325 finish_wait(&vcpu
->wq
, &wait
);
1328 void kvm_resched(struct kvm_vcpu
*vcpu
)
1330 if (!need_resched())
1334 EXPORT_SYMBOL_GPL(kvm_resched
);
1336 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1338 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1341 if (vmf
->pgoff
== 0)
1342 page
= virt_to_page(vcpu
->run
);
1344 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1345 page
= virt_to_page(vcpu
->arch
.pio_data
);
1347 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1348 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1349 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1352 return VM_FAULT_SIGBUS
;
1358 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
1359 .fault
= kvm_vcpu_fault
,
1362 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1364 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1368 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1370 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1372 kvm_put_kvm(vcpu
->kvm
);
1376 static const struct file_operations kvm_vcpu_fops
= {
1377 .release
= kvm_vcpu_release
,
1378 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1379 .compat_ioctl
= kvm_vcpu_ioctl
,
1380 .mmap
= kvm_vcpu_mmap
,
1384 * Allocates an inode for the vcpu.
1386 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1388 int fd
= anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, 0);
1390 kvm_put_kvm(vcpu
->kvm
);
1395 * Creates some virtual cpus. Good luck creating more than one.
1397 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
1400 struct kvm_vcpu
*vcpu
;
1405 vcpu
= kvm_arch_vcpu_create(kvm
, n
);
1407 return PTR_ERR(vcpu
);
1409 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1411 r
= kvm_arch_vcpu_setup(vcpu
);
1415 mutex_lock(&kvm
->lock
);
1416 if (kvm
->vcpus
[n
]) {
1420 kvm
->vcpus
[n
] = vcpu
;
1421 mutex_unlock(&kvm
->lock
);
1423 /* Now it's all set up, let userspace reach it */
1425 r
= create_vcpu_fd(vcpu
);
1431 mutex_lock(&kvm
->lock
);
1432 kvm
->vcpus
[n
] = NULL
;
1434 mutex_unlock(&kvm
->lock
);
1435 kvm_arch_vcpu_destroy(vcpu
);
1439 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1442 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1443 vcpu
->sigset_active
= 1;
1444 vcpu
->sigset
= *sigset
;
1446 vcpu
->sigset_active
= 0;
1450 static long kvm_vcpu_ioctl(struct file
*filp
,
1451 unsigned int ioctl
, unsigned long arg
)
1453 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1454 void __user
*argp
= (void __user
*)arg
;
1456 struct kvm_fpu
*fpu
= NULL
;
1457 struct kvm_sregs
*kvm_sregs
= NULL
;
1459 if (vcpu
->kvm
->mm
!= current
->mm
)
1466 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1468 case KVM_GET_REGS
: {
1469 struct kvm_regs
*kvm_regs
;
1472 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1475 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1479 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1486 case KVM_SET_REGS
: {
1487 struct kvm_regs
*kvm_regs
;
1490 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1494 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1496 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1504 case KVM_GET_SREGS
: {
1505 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1509 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1513 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1518 case KVM_SET_SREGS
: {
1519 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1524 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1526 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1532 case KVM_GET_MP_STATE
: {
1533 struct kvm_mp_state mp_state
;
1535 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1539 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1544 case KVM_SET_MP_STATE
: {
1545 struct kvm_mp_state mp_state
;
1548 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1550 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1556 case KVM_TRANSLATE
: {
1557 struct kvm_translation tr
;
1560 if (copy_from_user(&tr
, argp
, sizeof tr
))
1562 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1566 if (copy_to_user(argp
, &tr
, sizeof tr
))
1571 case KVM_DEBUG_GUEST
: {
1572 struct kvm_debug_guest dbg
;
1575 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1577 r
= kvm_arch_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
1583 case KVM_SET_SIGNAL_MASK
: {
1584 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1585 struct kvm_signal_mask kvm_sigmask
;
1586 sigset_t sigset
, *p
;
1591 if (copy_from_user(&kvm_sigmask
, argp
,
1592 sizeof kvm_sigmask
))
1595 if (kvm_sigmask
.len
!= sizeof sigset
)
1598 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1603 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1607 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1611 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1615 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1621 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1626 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1628 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1635 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1643 static long kvm_vm_ioctl(struct file
*filp
,
1644 unsigned int ioctl
, unsigned long arg
)
1646 struct kvm
*kvm
= filp
->private_data
;
1647 void __user
*argp
= (void __user
*)arg
;
1650 if (kvm
->mm
!= current
->mm
)
1653 case KVM_CREATE_VCPU
:
1654 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1658 case KVM_SET_USER_MEMORY_REGION
: {
1659 struct kvm_userspace_memory_region kvm_userspace_mem
;
1662 if (copy_from_user(&kvm_userspace_mem
, argp
,
1663 sizeof kvm_userspace_mem
))
1666 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1671 case KVM_GET_DIRTY_LOG
: {
1672 struct kvm_dirty_log log
;
1675 if (copy_from_user(&log
, argp
, sizeof log
))
1677 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1682 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1683 case KVM_REGISTER_COALESCED_MMIO
: {
1684 struct kvm_coalesced_mmio_zone zone
;
1686 if (copy_from_user(&zone
, argp
, sizeof zone
))
1689 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1695 case KVM_UNREGISTER_COALESCED_MMIO
: {
1696 struct kvm_coalesced_mmio_zone zone
;
1698 if (copy_from_user(&zone
, argp
, sizeof zone
))
1701 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1708 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1709 case KVM_ASSIGN_PCI_DEVICE
: {
1710 struct kvm_assigned_pci_dev assigned_dev
;
1713 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1715 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
1720 case KVM_ASSIGN_IRQ
: {
1721 struct kvm_assigned_irq assigned_irq
;
1724 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
1726 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
1733 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1739 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1741 struct page
*page
[1];
1744 gfn_t gfn
= vmf
->pgoff
;
1745 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1747 addr
= gfn_to_hva(kvm
, gfn
);
1748 if (kvm_is_error_hva(addr
))
1749 return VM_FAULT_SIGBUS
;
1751 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1753 if (unlikely(npages
!= 1))
1754 return VM_FAULT_SIGBUS
;
1756 vmf
->page
= page
[0];
1760 static struct vm_operations_struct kvm_vm_vm_ops
= {
1761 .fault
= kvm_vm_fault
,
1764 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1766 vma
->vm_ops
= &kvm_vm_vm_ops
;
1770 static const struct file_operations kvm_vm_fops
= {
1771 .release
= kvm_vm_release
,
1772 .unlocked_ioctl
= kvm_vm_ioctl
,
1773 .compat_ioctl
= kvm_vm_ioctl
,
1774 .mmap
= kvm_vm_mmap
,
1777 static int kvm_dev_ioctl_create_vm(void)
1782 kvm
= kvm_create_vm();
1784 return PTR_ERR(kvm
);
1785 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, 0);
1792 static long kvm_dev_ioctl(struct file
*filp
,
1793 unsigned int ioctl
, unsigned long arg
)
1798 case KVM_GET_API_VERSION
:
1802 r
= KVM_API_VERSION
;
1808 r
= kvm_dev_ioctl_create_vm();
1810 case KVM_CHECK_EXTENSION
:
1811 r
= kvm_dev_ioctl_check_extension(arg
);
1813 case KVM_GET_VCPU_MMAP_SIZE
:
1817 r
= PAGE_SIZE
; /* struct kvm_run */
1819 r
+= PAGE_SIZE
; /* pio data page */
1821 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1822 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1825 case KVM_TRACE_ENABLE
:
1826 case KVM_TRACE_PAUSE
:
1827 case KVM_TRACE_DISABLE
:
1828 r
= kvm_trace_ioctl(ioctl
, arg
);
1831 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1837 static struct file_operations kvm_chardev_ops
= {
1838 .unlocked_ioctl
= kvm_dev_ioctl
,
1839 .compat_ioctl
= kvm_dev_ioctl
,
1842 static struct miscdevice kvm_dev
= {
1848 static void hardware_enable(void *junk
)
1850 int cpu
= raw_smp_processor_id();
1852 if (cpu_isset(cpu
, cpus_hardware_enabled
))
1854 cpu_set(cpu
, cpus_hardware_enabled
);
1855 kvm_arch_hardware_enable(NULL
);
1858 static void hardware_disable(void *junk
)
1860 int cpu
= raw_smp_processor_id();
1862 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
1864 cpu_clear(cpu
, cpus_hardware_enabled
);
1865 kvm_arch_hardware_disable(NULL
);
1868 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1873 val
&= ~CPU_TASKS_FROZEN
;
1876 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1878 hardware_disable(NULL
);
1880 case CPU_UP_CANCELED
:
1881 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1883 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
1886 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1888 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
1895 asmlinkage
void kvm_handle_fault_on_reboot(void)
1898 /* spin while reset goes on */
1901 /* Fault while not rebooting. We want the trace. */
1904 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
1906 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1909 if (val
== SYS_RESTART
) {
1911 * Some (well, at least mine) BIOSes hang on reboot if
1914 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1915 kvm_rebooting
= true;
1916 on_each_cpu(hardware_disable
, NULL
, 1);
1921 static struct notifier_block kvm_reboot_notifier
= {
1922 .notifier_call
= kvm_reboot
,
1926 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
1928 memset(bus
, 0, sizeof(*bus
));
1931 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
1935 for (i
= 0; i
< bus
->dev_count
; i
++) {
1936 struct kvm_io_device
*pos
= bus
->devs
[i
];
1938 kvm_iodevice_destructor(pos
);
1942 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
,
1943 gpa_t addr
, int len
, int is_write
)
1947 for (i
= 0; i
< bus
->dev_count
; i
++) {
1948 struct kvm_io_device
*pos
= bus
->devs
[i
];
1950 if (pos
->in_range(pos
, addr
, len
, is_write
))
1957 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
1959 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
1961 bus
->devs
[bus
->dev_count
++] = dev
;
1964 static struct notifier_block kvm_cpu_notifier
= {
1965 .notifier_call
= kvm_cpu_hotplug
,
1966 .priority
= 20, /* must be > scheduler priority */
1969 static int vm_stat_get(void *_offset
, u64
*val
)
1971 unsigned offset
= (long)_offset
;
1975 spin_lock(&kvm_lock
);
1976 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1977 *val
+= *(u32
*)((void *)kvm
+ offset
);
1978 spin_unlock(&kvm_lock
);
1982 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
1984 static int vcpu_stat_get(void *_offset
, u64
*val
)
1986 unsigned offset
= (long)_offset
;
1988 struct kvm_vcpu
*vcpu
;
1992 spin_lock(&kvm_lock
);
1993 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1994 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
1995 vcpu
= kvm
->vcpus
[i
];
1997 *val
+= *(u32
*)((void *)vcpu
+ offset
);
1999 spin_unlock(&kvm_lock
);
2003 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2005 static struct file_operations
*stat_fops
[] = {
2006 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2007 [KVM_STAT_VM
] = &vm_stat_fops
,
2010 static void kvm_init_debug(void)
2012 struct kvm_stats_debugfs_item
*p
;
2014 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2015 for (p
= debugfs_entries
; p
->name
; ++p
)
2016 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2017 (void *)(long)p
->offset
,
2018 stat_fops
[p
->kind
]);
2021 static void kvm_exit_debug(void)
2023 struct kvm_stats_debugfs_item
*p
;
2025 for (p
= debugfs_entries
; p
->name
; ++p
)
2026 debugfs_remove(p
->dentry
);
2027 debugfs_remove(kvm_debugfs_dir
);
2030 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2032 hardware_disable(NULL
);
2036 static int kvm_resume(struct sys_device
*dev
)
2038 hardware_enable(NULL
);
2042 static struct sysdev_class kvm_sysdev_class
= {
2044 .suspend
= kvm_suspend
,
2045 .resume
= kvm_resume
,
2048 static struct sys_device kvm_sysdev
= {
2050 .cls
= &kvm_sysdev_class
,
2053 struct page
*bad_page
;
2057 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2059 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2062 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2064 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2066 kvm_arch_vcpu_load(vcpu
, cpu
);
2069 static void kvm_sched_out(struct preempt_notifier
*pn
,
2070 struct task_struct
*next
)
2072 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2074 kvm_arch_vcpu_put(vcpu
);
2077 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2078 struct module
*module
)
2085 r
= kvm_arch_init(opaque
);
2089 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2091 if (bad_page
== NULL
) {
2096 bad_pfn
= page_to_pfn(bad_page
);
2098 r
= kvm_arch_hardware_setup();
2102 for_each_online_cpu(cpu
) {
2103 smp_call_function_single(cpu
,
2104 kvm_arch_check_processor_compat
,
2110 on_each_cpu(hardware_enable
, NULL
, 1);
2111 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2114 register_reboot_notifier(&kvm_reboot_notifier
);
2116 r
= sysdev_class_register(&kvm_sysdev_class
);
2120 r
= sysdev_register(&kvm_sysdev
);
2124 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2125 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2126 __alignof__(struct kvm_vcpu
),
2128 if (!kvm_vcpu_cache
) {
2133 kvm_chardev_ops
.owner
= module
;
2135 r
= misc_register(&kvm_dev
);
2137 printk(KERN_ERR
"kvm: misc device register failed\n");
2141 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2142 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2147 kmem_cache_destroy(kvm_vcpu_cache
);
2149 sysdev_unregister(&kvm_sysdev
);
2151 sysdev_class_unregister(&kvm_sysdev_class
);
2153 unregister_reboot_notifier(&kvm_reboot_notifier
);
2154 unregister_cpu_notifier(&kvm_cpu_notifier
);
2156 on_each_cpu(hardware_disable
, NULL
, 1);
2158 kvm_arch_hardware_unsetup();
2160 __free_page(bad_page
);
2167 EXPORT_SYMBOL_GPL(kvm_init
);
2171 kvm_trace_cleanup();
2172 misc_deregister(&kvm_dev
);
2173 kmem_cache_destroy(kvm_vcpu_cache
);
2174 sysdev_unregister(&kvm_sysdev
);
2175 sysdev_class_unregister(&kvm_sysdev_class
);
2176 unregister_reboot_notifier(&kvm_reboot_notifier
);
2177 unregister_cpu_notifier(&kvm_cpu_notifier
);
2178 on_each_cpu(hardware_disable
, NULL
, 1);
2179 kvm_arch_hardware_unsetup();
2182 __free_page(bad_page
);
2184 EXPORT_SYMBOL_GPL(kvm_exit
);