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>
50 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51 #include "coalesced_mmio.h"
54 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
55 #include <linux/pci.h>
56 #include <linux/interrupt.h>
60 MODULE_AUTHOR("Qumranet");
61 MODULE_LICENSE("GPL");
63 DEFINE_SPINLOCK(kvm_lock
);
66 static cpumask_t cpus_hardware_enabled
;
68 struct kmem_cache
*kvm_vcpu_cache
;
69 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
71 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
73 struct dentry
*kvm_debugfs_dir
;
75 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
80 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
81 static struct kvm_assigned_dev_kernel
*kvm_find_assigned_dev(struct list_head
*head
,
84 struct list_head
*ptr
;
85 struct kvm_assigned_dev_kernel
*match
;
87 list_for_each(ptr
, head
) {
88 match
= list_entry(ptr
, struct kvm_assigned_dev_kernel
, list
);
89 if (match
->assigned_dev_id
== assigned_dev_id
)
95 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct
*work
)
97 struct kvm_assigned_dev_kernel
*assigned_dev
;
99 assigned_dev
= container_of(work
, struct kvm_assigned_dev_kernel
,
102 /* This is taken to safely inject irq inside the guest. When
103 * the interrupt injection (or the ioapic code) uses a
104 * finer-grained lock, update this
106 mutex_lock(&assigned_dev
->kvm
->lock
);
107 kvm_set_irq(assigned_dev
->kvm
,
108 assigned_dev
->irq_source_id
,
109 assigned_dev
->guest_irq
, 1);
110 mutex_unlock(&assigned_dev
->kvm
->lock
);
111 kvm_put_kvm(assigned_dev
->kvm
);
114 static irqreturn_t
kvm_assigned_dev_intr(int irq
, void *dev_id
)
116 struct kvm_assigned_dev_kernel
*assigned_dev
=
117 (struct kvm_assigned_dev_kernel
*) dev_id
;
119 kvm_get_kvm(assigned_dev
->kvm
);
120 schedule_work(&assigned_dev
->interrupt_work
);
121 disable_irq_nosync(irq
);
125 /* Ack the irq line for an assigned device */
126 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
128 struct kvm_assigned_dev_kernel
*dev
;
133 dev
= container_of(kian
, struct kvm_assigned_dev_kernel
,
135 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
136 enable_irq(dev
->host_irq
);
139 static void kvm_free_assigned_device(struct kvm
*kvm
,
140 struct kvm_assigned_dev_kernel
143 if (irqchip_in_kernel(kvm
) && assigned_dev
->irq_requested
)
144 free_irq(assigned_dev
->host_irq
, (void *)assigned_dev
);
146 kvm_unregister_irq_ack_notifier(kvm
, &assigned_dev
->ack_notifier
);
147 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
149 if (cancel_work_sync(&assigned_dev
->interrupt_work
))
150 /* We had pending work. That means we will have to take
151 * care of kvm_put_kvm.
155 pci_release_regions(assigned_dev
->dev
);
156 pci_disable_device(assigned_dev
->dev
);
157 pci_dev_put(assigned_dev
->dev
);
159 list_del(&assigned_dev
->list
);
163 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
165 struct list_head
*ptr
, *ptr2
;
166 struct kvm_assigned_dev_kernel
*assigned_dev
;
168 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
169 assigned_dev
= list_entry(ptr
,
170 struct kvm_assigned_dev_kernel
,
173 kvm_free_assigned_device(kvm
, assigned_dev
);
177 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
178 struct kvm_assigned_irq
182 struct kvm_assigned_dev_kernel
*match
;
184 mutex_lock(&kvm
->lock
);
186 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
187 assigned_irq
->assigned_dev_id
);
189 mutex_unlock(&kvm
->lock
);
193 if (match
->irq_requested
) {
194 match
->guest_irq
= assigned_irq
->guest_irq
;
195 match
->ack_notifier
.gsi
= assigned_irq
->guest_irq
;
196 mutex_unlock(&kvm
->lock
);
200 INIT_WORK(&match
->interrupt_work
,
201 kvm_assigned_dev_interrupt_work_handler
);
203 if (irqchip_in_kernel(kvm
)) {
204 if (!capable(CAP_SYS_RAWIO
)) {
209 if (assigned_irq
->host_irq
)
210 match
->host_irq
= assigned_irq
->host_irq
;
212 match
->host_irq
= match
->dev
->irq
;
213 match
->guest_irq
= assigned_irq
->guest_irq
;
214 match
->ack_notifier
.gsi
= assigned_irq
->guest_irq
;
215 match
->ack_notifier
.irq_acked
= kvm_assigned_dev_ack_irq
;
216 kvm_register_irq_ack_notifier(kvm
, &match
->ack_notifier
);
217 r
= kvm_request_irq_source_id(kvm
);
221 match
->irq_source_id
= r
;
223 /* Even though this is PCI, we don't want to use shared
224 * interrupts. Sharing host devices with guest-assigned devices
225 * on the same interrupt line is not a happy situation: there
226 * are going to be long delays in accepting, acking, etc.
228 if (request_irq(match
->host_irq
, kvm_assigned_dev_intr
, 0,
229 "kvm_assigned_device", (void *)match
)) {
235 match
->irq_requested
= true;
236 mutex_unlock(&kvm
->lock
);
239 mutex_unlock(&kvm
->lock
);
240 kvm_free_assigned_device(kvm
, match
);
244 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
245 struct kvm_assigned_pci_dev
*assigned_dev
)
248 struct kvm_assigned_dev_kernel
*match
;
251 mutex_lock(&kvm
->lock
);
253 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
254 assigned_dev
->assigned_dev_id
);
256 /* device already assigned */
261 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
263 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
268 dev
= pci_get_bus_and_slot(assigned_dev
->busnr
,
269 assigned_dev
->devfn
);
271 printk(KERN_INFO
"%s: host device not found\n", __func__
);
275 if (pci_enable_device(dev
)) {
276 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
280 r
= pci_request_regions(dev
, "kvm_assigned_device");
282 printk(KERN_INFO
"%s: Could not get access to device regions\n",
286 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
287 match
->host_busnr
= assigned_dev
->busnr
;
288 match
->host_devfn
= assigned_dev
->devfn
;
293 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
295 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
) {
296 r
= kvm_iommu_map_guest(kvm
, match
);
302 mutex_unlock(&kvm
->lock
);
305 list_del(&match
->list
);
306 pci_release_regions(dev
);
308 pci_disable_device(dev
);
313 mutex_unlock(&kvm
->lock
);
318 static inline int valid_vcpu(int n
)
320 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
323 inline int kvm_is_mmio_pfn(pfn_t pfn
)
326 return PageReserved(pfn_to_page(pfn
));
332 * Switches to specified vcpu, until a matching vcpu_put()
334 void vcpu_load(struct kvm_vcpu
*vcpu
)
338 mutex_lock(&vcpu
->mutex
);
340 preempt_notifier_register(&vcpu
->preempt_notifier
);
341 kvm_arch_vcpu_load(vcpu
, cpu
);
345 void vcpu_put(struct kvm_vcpu
*vcpu
)
348 kvm_arch_vcpu_put(vcpu
);
349 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
351 mutex_unlock(&vcpu
->mutex
);
354 static void ack_flush(void *_completed
)
358 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
362 struct kvm_vcpu
*vcpu
;
366 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
367 vcpu
= kvm
->vcpus
[i
];
370 if (test_and_set_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
373 if (cpu
!= -1 && cpu
!= me
)
376 if (cpus_empty(cpus
))
378 ++kvm
->stat
.remote_tlb_flush
;
379 smp_call_function_mask(cpus
, ack_flush
, NULL
, 1);
384 void kvm_reload_remote_mmus(struct kvm
*kvm
)
388 struct kvm_vcpu
*vcpu
;
392 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
393 vcpu
= kvm
->vcpus
[i
];
396 if (test_and_set_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
399 if (cpu
!= -1 && cpu
!= me
)
402 if (cpus_empty(cpus
))
404 smp_call_function_mask(cpus
, ack_flush
, NULL
, 1);
410 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
415 mutex_init(&vcpu
->mutex
);
419 init_waitqueue_head(&vcpu
->wq
);
421 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
426 vcpu
->run
= page_address(page
);
428 r
= kvm_arch_vcpu_init(vcpu
);
434 free_page((unsigned long)vcpu
->run
);
438 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
440 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
442 kvm_arch_vcpu_uninit(vcpu
);
443 free_page((unsigned long)vcpu
->run
);
445 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
447 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
448 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
450 return container_of(mn
, struct kvm
, mmu_notifier
);
453 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
454 struct mm_struct
*mm
,
455 unsigned long address
)
457 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
461 * When ->invalidate_page runs, the linux pte has been zapped
462 * already but the page is still allocated until
463 * ->invalidate_page returns. So if we increase the sequence
464 * here the kvm page fault will notice if the spte can't be
465 * established because the page is going to be freed. If
466 * instead the kvm page fault establishes the spte before
467 * ->invalidate_page runs, kvm_unmap_hva will release it
470 * The sequence increase only need to be seen at spin_unlock
471 * time, and not at spin_lock time.
473 * Increasing the sequence after the spin_unlock would be
474 * unsafe because the kvm page fault could then establish the
475 * pte after kvm_unmap_hva returned, without noticing the page
476 * is going to be freed.
478 spin_lock(&kvm
->mmu_lock
);
479 kvm
->mmu_notifier_seq
++;
480 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
481 spin_unlock(&kvm
->mmu_lock
);
483 /* we've to flush the tlb before the pages can be freed */
485 kvm_flush_remote_tlbs(kvm
);
489 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
490 struct mm_struct
*mm
,
494 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
495 int need_tlb_flush
= 0;
497 spin_lock(&kvm
->mmu_lock
);
499 * The count increase must become visible at unlock time as no
500 * spte can be established without taking the mmu_lock and
501 * count is also read inside the mmu_lock critical section.
503 kvm
->mmu_notifier_count
++;
504 for (; start
< end
; start
+= PAGE_SIZE
)
505 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
506 spin_unlock(&kvm
->mmu_lock
);
508 /* we've to flush the tlb before the pages can be freed */
510 kvm_flush_remote_tlbs(kvm
);
513 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
514 struct mm_struct
*mm
,
518 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
520 spin_lock(&kvm
->mmu_lock
);
522 * This sequence increase will notify the kvm page fault that
523 * the page that is going to be mapped in the spte could have
526 kvm
->mmu_notifier_seq
++;
528 * The above sequence increase must be visible before the
529 * below count decrease but both values are read by the kvm
530 * page fault under mmu_lock spinlock so we don't need to add
531 * a smb_wmb() here in between the two.
533 kvm
->mmu_notifier_count
--;
534 spin_unlock(&kvm
->mmu_lock
);
536 BUG_ON(kvm
->mmu_notifier_count
< 0);
539 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
540 struct mm_struct
*mm
,
541 unsigned long address
)
543 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
546 spin_lock(&kvm
->mmu_lock
);
547 young
= kvm_age_hva(kvm
, address
);
548 spin_unlock(&kvm
->mmu_lock
);
551 kvm_flush_remote_tlbs(kvm
);
556 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
557 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
558 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
559 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
560 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
562 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
564 static struct kvm
*kvm_create_vm(void)
566 struct kvm
*kvm
= kvm_arch_create_vm();
567 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
574 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
575 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
578 return ERR_PTR(-ENOMEM
);
580 kvm
->coalesced_mmio_ring
=
581 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
584 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
587 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
588 err
= mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
590 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
599 kvm
->mm
= current
->mm
;
600 atomic_inc(&kvm
->mm
->mm_count
);
601 spin_lock_init(&kvm
->mmu_lock
);
602 kvm_io_bus_init(&kvm
->pio_bus
);
603 mutex_init(&kvm
->lock
);
604 kvm_io_bus_init(&kvm
->mmio_bus
);
605 init_rwsem(&kvm
->slots_lock
);
606 atomic_set(&kvm
->users_count
, 1);
607 spin_lock(&kvm_lock
);
608 list_add(&kvm
->vm_list
, &vm_list
);
609 spin_unlock(&kvm_lock
);
610 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
611 kvm_coalesced_mmio_init(kvm
);
618 * Free any memory in @free but not in @dont.
620 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
621 struct kvm_memory_slot
*dont
)
623 if (!dont
|| free
->rmap
!= dont
->rmap
)
626 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
627 vfree(free
->dirty_bitmap
);
629 if (!dont
|| free
->lpage_info
!= dont
->lpage_info
)
630 vfree(free
->lpage_info
);
633 free
->dirty_bitmap
= NULL
;
635 free
->lpage_info
= NULL
;
638 void kvm_free_physmem(struct kvm
*kvm
)
642 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
643 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
646 static void kvm_destroy_vm(struct kvm
*kvm
)
648 struct mm_struct
*mm
= kvm
->mm
;
650 spin_lock(&kvm_lock
);
651 list_del(&kvm
->vm_list
);
652 spin_unlock(&kvm_lock
);
653 kvm_io_bus_destroy(&kvm
->pio_bus
);
654 kvm_io_bus_destroy(&kvm
->mmio_bus
);
655 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
656 if (kvm
->coalesced_mmio_ring
!= NULL
)
657 free_page((unsigned long)kvm
->coalesced_mmio_ring
);
659 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
660 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
662 kvm_arch_destroy_vm(kvm
);
666 void kvm_get_kvm(struct kvm
*kvm
)
668 atomic_inc(&kvm
->users_count
);
670 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
672 void kvm_put_kvm(struct kvm
*kvm
)
674 if (atomic_dec_and_test(&kvm
->users_count
))
677 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
680 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
682 struct kvm
*kvm
= filp
->private_data
;
689 * Allocate some memory and give it an address in the guest physical address
692 * Discontiguous memory is allowed, mostly for framebuffers.
694 * Must be called holding mmap_sem for write.
696 int __kvm_set_memory_region(struct kvm
*kvm
,
697 struct kvm_userspace_memory_region
*mem
,
702 unsigned long npages
;
704 struct kvm_memory_slot
*memslot
;
705 struct kvm_memory_slot old
, new;
708 /* General sanity checks */
709 if (mem
->memory_size
& (PAGE_SIZE
- 1))
711 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
713 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
715 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
718 memslot
= &kvm
->memslots
[mem
->slot
];
719 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
720 npages
= mem
->memory_size
>> PAGE_SHIFT
;
723 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
725 new = old
= *memslot
;
727 new.base_gfn
= base_gfn
;
729 new.flags
= mem
->flags
;
731 /* Disallow changing a memory slot's size. */
733 if (npages
&& old
.npages
&& npages
!= old
.npages
)
736 /* Check for overlaps */
738 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
739 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
743 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
744 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
748 /* Free page dirty bitmap if unneeded */
749 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
750 new.dirty_bitmap
= NULL
;
754 /* Allocate if a slot is being created */
756 if (npages
&& !new.rmap
) {
757 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
762 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
764 new.user_alloc
= user_alloc
;
766 * hva_to_rmmap() serialzies with the mmu_lock and to be
767 * safe it has to ignore memslots with !user_alloc &&
771 new.userspace_addr
= mem
->userspace_addr
;
773 new.userspace_addr
= 0;
775 if (npages
&& !new.lpage_info
) {
776 int largepages
= npages
/ KVM_PAGES_PER_HPAGE
;
777 if (npages
% KVM_PAGES_PER_HPAGE
)
779 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
782 new.lpage_info
= vmalloc(largepages
* sizeof(*new.lpage_info
));
787 memset(new.lpage_info
, 0, largepages
* sizeof(*new.lpage_info
));
789 if (base_gfn
% KVM_PAGES_PER_HPAGE
)
790 new.lpage_info
[0].write_count
= 1;
791 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE
)
792 new.lpage_info
[largepages
-1].write_count
= 1;
795 /* Allocate page dirty bitmap if needed */
796 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
797 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
799 new.dirty_bitmap
= vmalloc(dirty_bytes
);
800 if (!new.dirty_bitmap
)
802 memset(new.dirty_bitmap
, 0, dirty_bytes
);
804 #endif /* not defined CONFIG_S390 */
807 kvm_arch_flush_shadow(kvm
);
809 spin_lock(&kvm
->mmu_lock
);
810 if (mem
->slot
>= kvm
->nmemslots
)
811 kvm
->nmemslots
= mem
->slot
+ 1;
814 spin_unlock(&kvm
->mmu_lock
);
816 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
818 spin_lock(&kvm
->mmu_lock
);
820 spin_unlock(&kvm
->mmu_lock
);
824 kvm_free_physmem_slot(&old
, &new);
826 /* map the pages in iommu page table */
827 r
= kvm_iommu_map_pages(kvm
, base_gfn
, npages
);
834 kvm_free_physmem_slot(&new, &old
);
839 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
841 int kvm_set_memory_region(struct kvm
*kvm
,
842 struct kvm_userspace_memory_region
*mem
,
847 down_write(&kvm
->slots_lock
);
848 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
849 up_write(&kvm
->slots_lock
);
852 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
854 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
856 kvm_userspace_memory_region
*mem
,
859 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
861 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
864 int kvm_get_dirty_log(struct kvm
*kvm
,
865 struct kvm_dirty_log
*log
, int *is_dirty
)
867 struct kvm_memory_slot
*memslot
;
870 unsigned long any
= 0;
873 if (log
->slot
>= KVM_MEMORY_SLOTS
)
876 memslot
= &kvm
->memslots
[log
->slot
];
878 if (!memslot
->dirty_bitmap
)
881 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
883 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
884 any
= memslot
->dirty_bitmap
[i
];
887 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
898 int is_error_page(struct page
*page
)
900 return page
== bad_page
;
902 EXPORT_SYMBOL_GPL(is_error_page
);
904 int is_error_pfn(pfn_t pfn
)
906 return pfn
== bad_pfn
;
908 EXPORT_SYMBOL_GPL(is_error_pfn
);
910 static inline unsigned long bad_hva(void)
915 int kvm_is_error_hva(unsigned long addr
)
917 return addr
== bad_hva();
919 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
921 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
925 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
926 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
928 if (gfn
>= memslot
->base_gfn
929 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
935 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
937 gfn
= unalias_gfn(kvm
, gfn
);
938 return __gfn_to_memslot(kvm
, gfn
);
941 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
945 gfn
= unalias_gfn(kvm
, gfn
);
946 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
947 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
949 if (gfn
>= memslot
->base_gfn
950 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
955 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
957 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
959 struct kvm_memory_slot
*slot
;
961 gfn
= unalias_gfn(kvm
, gfn
);
962 slot
= __gfn_to_memslot(kvm
, gfn
);
965 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
967 EXPORT_SYMBOL_GPL(gfn_to_hva
);
969 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
971 struct page
*page
[1];
978 addr
= gfn_to_hva(kvm
, gfn
);
979 if (kvm_is_error_hva(addr
)) {
981 return page_to_pfn(bad_page
);
984 npages
= get_user_pages_fast(addr
, 1, 1, page
);
986 if (unlikely(npages
!= 1)) {
987 struct vm_area_struct
*vma
;
989 down_read(¤t
->mm
->mmap_sem
);
990 vma
= find_vma(current
->mm
, addr
);
992 if (vma
== NULL
|| addr
< vma
->vm_start
||
993 !(vma
->vm_flags
& VM_PFNMAP
)) {
994 up_read(¤t
->mm
->mmap_sem
);
996 return page_to_pfn(bad_page
);
999 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1000 up_read(¤t
->mm
->mmap_sem
);
1001 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1003 pfn
= page_to_pfn(page
[0]);
1008 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1010 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1014 pfn
= gfn_to_pfn(kvm
, gfn
);
1015 if (!kvm_is_mmio_pfn(pfn
))
1016 return pfn_to_page(pfn
);
1018 WARN_ON(kvm_is_mmio_pfn(pfn
));
1024 EXPORT_SYMBOL_GPL(gfn_to_page
);
1026 void kvm_release_page_clean(struct page
*page
)
1028 kvm_release_pfn_clean(page_to_pfn(page
));
1030 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1032 void kvm_release_pfn_clean(pfn_t pfn
)
1034 if (!kvm_is_mmio_pfn(pfn
))
1035 put_page(pfn_to_page(pfn
));
1037 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1039 void kvm_release_page_dirty(struct page
*page
)
1041 kvm_release_pfn_dirty(page_to_pfn(page
));
1043 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1045 void kvm_release_pfn_dirty(pfn_t pfn
)
1047 kvm_set_pfn_dirty(pfn
);
1048 kvm_release_pfn_clean(pfn
);
1050 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1052 void kvm_set_page_dirty(struct page
*page
)
1054 kvm_set_pfn_dirty(page_to_pfn(page
));
1056 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1058 void kvm_set_pfn_dirty(pfn_t pfn
)
1060 if (!kvm_is_mmio_pfn(pfn
)) {
1061 struct page
*page
= pfn_to_page(pfn
);
1062 if (!PageReserved(page
))
1066 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1068 void kvm_set_pfn_accessed(pfn_t pfn
)
1070 if (!kvm_is_mmio_pfn(pfn
))
1071 mark_page_accessed(pfn_to_page(pfn
));
1073 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1075 void kvm_get_pfn(pfn_t pfn
)
1077 if (!kvm_is_mmio_pfn(pfn
))
1078 get_page(pfn_to_page(pfn
));
1080 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1082 static int next_segment(unsigned long len
, int offset
)
1084 if (len
> PAGE_SIZE
- offset
)
1085 return PAGE_SIZE
- offset
;
1090 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1096 addr
= gfn_to_hva(kvm
, gfn
);
1097 if (kvm_is_error_hva(addr
))
1099 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1104 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1106 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1108 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1110 int offset
= offset_in_page(gpa
);
1113 while ((seg
= next_segment(len
, offset
)) != 0) {
1114 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1124 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1126 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1131 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1132 int offset
= offset_in_page(gpa
);
1134 addr
= gfn_to_hva(kvm
, gfn
);
1135 if (kvm_is_error_hva(addr
))
1137 pagefault_disable();
1138 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1144 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1146 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1147 int offset
, int len
)
1152 addr
= gfn_to_hva(kvm
, gfn
);
1153 if (kvm_is_error_hva(addr
))
1155 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1158 mark_page_dirty(kvm
, gfn
);
1161 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1163 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1166 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1168 int offset
= offset_in_page(gpa
);
1171 while ((seg
= next_segment(len
, offset
)) != 0) {
1172 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1183 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1185 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1187 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1189 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1191 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1193 int offset
= offset_in_page(gpa
);
1196 while ((seg
= next_segment(len
, offset
)) != 0) {
1197 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1206 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1208 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1210 struct kvm_memory_slot
*memslot
;
1212 gfn
= unalias_gfn(kvm
, gfn
);
1213 memslot
= __gfn_to_memslot(kvm
, gfn
);
1214 if (memslot
&& memslot
->dirty_bitmap
) {
1215 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1218 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1219 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1224 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1226 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1231 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1233 if (kvm_cpu_has_interrupt(vcpu
) ||
1234 kvm_cpu_has_pending_timer(vcpu
) ||
1235 kvm_arch_vcpu_runnable(vcpu
)) {
1236 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1239 if (signal_pending(current
))
1247 finish_wait(&vcpu
->wq
, &wait
);
1250 void kvm_resched(struct kvm_vcpu
*vcpu
)
1252 if (!need_resched())
1256 EXPORT_SYMBOL_GPL(kvm_resched
);
1258 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1260 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1263 if (vmf
->pgoff
== 0)
1264 page
= virt_to_page(vcpu
->run
);
1266 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1267 page
= virt_to_page(vcpu
->arch
.pio_data
);
1269 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1270 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1271 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1274 return VM_FAULT_SIGBUS
;
1280 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
1281 .fault
= kvm_vcpu_fault
,
1284 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1286 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1290 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1292 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1294 kvm_put_kvm(vcpu
->kvm
);
1298 static const struct file_operations kvm_vcpu_fops
= {
1299 .release
= kvm_vcpu_release
,
1300 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1301 .compat_ioctl
= kvm_vcpu_ioctl
,
1302 .mmap
= kvm_vcpu_mmap
,
1306 * Allocates an inode for the vcpu.
1308 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1310 int fd
= anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, 0);
1312 kvm_put_kvm(vcpu
->kvm
);
1317 * Creates some virtual cpus. Good luck creating more than one.
1319 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
1322 struct kvm_vcpu
*vcpu
;
1327 vcpu
= kvm_arch_vcpu_create(kvm
, n
);
1329 return PTR_ERR(vcpu
);
1331 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1333 r
= kvm_arch_vcpu_setup(vcpu
);
1337 mutex_lock(&kvm
->lock
);
1338 if (kvm
->vcpus
[n
]) {
1342 kvm
->vcpus
[n
] = vcpu
;
1343 mutex_unlock(&kvm
->lock
);
1345 /* Now it's all set up, let userspace reach it */
1347 r
= create_vcpu_fd(vcpu
);
1353 mutex_lock(&kvm
->lock
);
1354 kvm
->vcpus
[n
] = NULL
;
1356 mutex_unlock(&kvm
->lock
);
1357 kvm_arch_vcpu_destroy(vcpu
);
1361 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1364 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1365 vcpu
->sigset_active
= 1;
1366 vcpu
->sigset
= *sigset
;
1368 vcpu
->sigset_active
= 0;
1372 static long kvm_vcpu_ioctl(struct file
*filp
,
1373 unsigned int ioctl
, unsigned long arg
)
1375 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1376 void __user
*argp
= (void __user
*)arg
;
1378 struct kvm_fpu
*fpu
= NULL
;
1379 struct kvm_sregs
*kvm_sregs
= NULL
;
1381 if (vcpu
->kvm
->mm
!= current
->mm
)
1388 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1390 case KVM_GET_REGS
: {
1391 struct kvm_regs
*kvm_regs
;
1394 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1397 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1401 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1408 case KVM_SET_REGS
: {
1409 struct kvm_regs
*kvm_regs
;
1412 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1416 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1418 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1426 case KVM_GET_SREGS
: {
1427 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1431 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1435 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1440 case KVM_SET_SREGS
: {
1441 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1446 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1448 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1454 case KVM_GET_MP_STATE
: {
1455 struct kvm_mp_state mp_state
;
1457 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1461 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1466 case KVM_SET_MP_STATE
: {
1467 struct kvm_mp_state mp_state
;
1470 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1472 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1478 case KVM_TRANSLATE
: {
1479 struct kvm_translation tr
;
1482 if (copy_from_user(&tr
, argp
, sizeof tr
))
1484 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1488 if (copy_to_user(argp
, &tr
, sizeof tr
))
1493 case KVM_DEBUG_GUEST
: {
1494 struct kvm_debug_guest dbg
;
1497 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1499 r
= kvm_arch_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
1505 case KVM_SET_SIGNAL_MASK
: {
1506 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1507 struct kvm_signal_mask kvm_sigmask
;
1508 sigset_t sigset
, *p
;
1513 if (copy_from_user(&kvm_sigmask
, argp
,
1514 sizeof kvm_sigmask
))
1517 if (kvm_sigmask
.len
!= sizeof sigset
)
1520 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1525 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1529 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1533 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1537 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1543 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1548 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1550 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1557 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1565 static long kvm_vm_ioctl(struct file
*filp
,
1566 unsigned int ioctl
, unsigned long arg
)
1568 struct kvm
*kvm
= filp
->private_data
;
1569 void __user
*argp
= (void __user
*)arg
;
1572 if (kvm
->mm
!= current
->mm
)
1575 case KVM_CREATE_VCPU
:
1576 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1580 case KVM_SET_USER_MEMORY_REGION
: {
1581 struct kvm_userspace_memory_region kvm_userspace_mem
;
1584 if (copy_from_user(&kvm_userspace_mem
, argp
,
1585 sizeof kvm_userspace_mem
))
1588 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1593 case KVM_GET_DIRTY_LOG
: {
1594 struct kvm_dirty_log log
;
1597 if (copy_from_user(&log
, argp
, sizeof log
))
1599 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1604 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1605 case KVM_REGISTER_COALESCED_MMIO
: {
1606 struct kvm_coalesced_mmio_zone zone
;
1608 if (copy_from_user(&zone
, argp
, sizeof zone
))
1611 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1617 case KVM_UNREGISTER_COALESCED_MMIO
: {
1618 struct kvm_coalesced_mmio_zone zone
;
1620 if (copy_from_user(&zone
, argp
, sizeof zone
))
1623 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1630 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1631 case KVM_ASSIGN_PCI_DEVICE
: {
1632 struct kvm_assigned_pci_dev assigned_dev
;
1635 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1637 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
1642 case KVM_ASSIGN_IRQ
: {
1643 struct kvm_assigned_irq assigned_irq
;
1646 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
1648 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
1655 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1661 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1663 struct page
*page
[1];
1666 gfn_t gfn
= vmf
->pgoff
;
1667 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1669 addr
= gfn_to_hva(kvm
, gfn
);
1670 if (kvm_is_error_hva(addr
))
1671 return VM_FAULT_SIGBUS
;
1673 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1675 if (unlikely(npages
!= 1))
1676 return VM_FAULT_SIGBUS
;
1678 vmf
->page
= page
[0];
1682 static struct vm_operations_struct kvm_vm_vm_ops
= {
1683 .fault
= kvm_vm_fault
,
1686 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1688 vma
->vm_ops
= &kvm_vm_vm_ops
;
1692 static const struct file_operations kvm_vm_fops
= {
1693 .release
= kvm_vm_release
,
1694 .unlocked_ioctl
= kvm_vm_ioctl
,
1695 .compat_ioctl
= kvm_vm_ioctl
,
1696 .mmap
= kvm_vm_mmap
,
1699 static int kvm_dev_ioctl_create_vm(void)
1704 kvm
= kvm_create_vm();
1706 return PTR_ERR(kvm
);
1707 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, 0);
1714 static long kvm_dev_ioctl(struct file
*filp
,
1715 unsigned int ioctl
, unsigned long arg
)
1720 case KVM_GET_API_VERSION
:
1724 r
= KVM_API_VERSION
;
1730 r
= kvm_dev_ioctl_create_vm();
1732 case KVM_CHECK_EXTENSION
:
1733 r
= kvm_dev_ioctl_check_extension(arg
);
1735 case KVM_GET_VCPU_MMAP_SIZE
:
1739 r
= PAGE_SIZE
; /* struct kvm_run */
1741 r
+= PAGE_SIZE
; /* pio data page */
1743 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1744 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1747 case KVM_TRACE_ENABLE
:
1748 case KVM_TRACE_PAUSE
:
1749 case KVM_TRACE_DISABLE
:
1750 r
= kvm_trace_ioctl(ioctl
, arg
);
1753 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1759 static struct file_operations kvm_chardev_ops
= {
1760 .unlocked_ioctl
= kvm_dev_ioctl
,
1761 .compat_ioctl
= kvm_dev_ioctl
,
1764 static struct miscdevice kvm_dev
= {
1770 static void hardware_enable(void *junk
)
1772 int cpu
= raw_smp_processor_id();
1774 if (cpu_isset(cpu
, cpus_hardware_enabled
))
1776 cpu_set(cpu
, cpus_hardware_enabled
);
1777 kvm_arch_hardware_enable(NULL
);
1780 static void hardware_disable(void *junk
)
1782 int cpu
= raw_smp_processor_id();
1784 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
1786 cpu_clear(cpu
, cpus_hardware_enabled
);
1787 kvm_arch_hardware_disable(NULL
);
1790 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1795 val
&= ~CPU_TASKS_FROZEN
;
1798 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1800 hardware_disable(NULL
);
1802 case CPU_UP_CANCELED
:
1803 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1805 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
1808 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1810 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
1817 asmlinkage
void kvm_handle_fault_on_reboot(void)
1820 /* spin while reset goes on */
1823 /* Fault while not rebooting. We want the trace. */
1826 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
1828 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1831 if (val
== SYS_RESTART
) {
1833 * Some (well, at least mine) BIOSes hang on reboot if
1836 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1837 kvm_rebooting
= true;
1838 on_each_cpu(hardware_disable
, NULL
, 1);
1843 static struct notifier_block kvm_reboot_notifier
= {
1844 .notifier_call
= kvm_reboot
,
1848 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
1850 memset(bus
, 0, sizeof(*bus
));
1853 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
1857 for (i
= 0; i
< bus
->dev_count
; i
++) {
1858 struct kvm_io_device
*pos
= bus
->devs
[i
];
1860 kvm_iodevice_destructor(pos
);
1864 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
,
1865 gpa_t addr
, int len
, int is_write
)
1869 for (i
= 0; i
< bus
->dev_count
; i
++) {
1870 struct kvm_io_device
*pos
= bus
->devs
[i
];
1872 if (pos
->in_range(pos
, addr
, len
, is_write
))
1879 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
1881 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
1883 bus
->devs
[bus
->dev_count
++] = dev
;
1886 static struct notifier_block kvm_cpu_notifier
= {
1887 .notifier_call
= kvm_cpu_hotplug
,
1888 .priority
= 20, /* must be > scheduler priority */
1891 static int vm_stat_get(void *_offset
, u64
*val
)
1893 unsigned offset
= (long)_offset
;
1897 spin_lock(&kvm_lock
);
1898 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1899 *val
+= *(u32
*)((void *)kvm
+ offset
);
1900 spin_unlock(&kvm_lock
);
1904 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
1906 static int vcpu_stat_get(void *_offset
, u64
*val
)
1908 unsigned offset
= (long)_offset
;
1910 struct kvm_vcpu
*vcpu
;
1914 spin_lock(&kvm_lock
);
1915 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1916 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
1917 vcpu
= kvm
->vcpus
[i
];
1919 *val
+= *(u32
*)((void *)vcpu
+ offset
);
1921 spin_unlock(&kvm_lock
);
1925 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
1927 static struct file_operations
*stat_fops
[] = {
1928 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
1929 [KVM_STAT_VM
] = &vm_stat_fops
,
1932 static void kvm_init_debug(void)
1934 struct kvm_stats_debugfs_item
*p
;
1936 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
1937 for (p
= debugfs_entries
; p
->name
; ++p
)
1938 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
1939 (void *)(long)p
->offset
,
1940 stat_fops
[p
->kind
]);
1943 static void kvm_exit_debug(void)
1945 struct kvm_stats_debugfs_item
*p
;
1947 for (p
= debugfs_entries
; p
->name
; ++p
)
1948 debugfs_remove(p
->dentry
);
1949 debugfs_remove(kvm_debugfs_dir
);
1952 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
1954 hardware_disable(NULL
);
1958 static int kvm_resume(struct sys_device
*dev
)
1960 hardware_enable(NULL
);
1964 static struct sysdev_class kvm_sysdev_class
= {
1966 .suspend
= kvm_suspend
,
1967 .resume
= kvm_resume
,
1970 static struct sys_device kvm_sysdev
= {
1972 .cls
= &kvm_sysdev_class
,
1975 struct page
*bad_page
;
1979 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
1981 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
1984 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
1986 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
1988 kvm_arch_vcpu_load(vcpu
, cpu
);
1991 static void kvm_sched_out(struct preempt_notifier
*pn
,
1992 struct task_struct
*next
)
1994 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
1996 kvm_arch_vcpu_put(vcpu
);
1999 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2000 struct module
*module
)
2007 r
= kvm_arch_init(opaque
);
2011 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2013 if (bad_page
== NULL
) {
2018 bad_pfn
= page_to_pfn(bad_page
);
2020 r
= kvm_arch_hardware_setup();
2024 for_each_online_cpu(cpu
) {
2025 smp_call_function_single(cpu
,
2026 kvm_arch_check_processor_compat
,
2032 on_each_cpu(hardware_enable
, NULL
, 1);
2033 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2036 register_reboot_notifier(&kvm_reboot_notifier
);
2038 r
= sysdev_class_register(&kvm_sysdev_class
);
2042 r
= sysdev_register(&kvm_sysdev
);
2046 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2047 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2048 __alignof__(struct kvm_vcpu
),
2050 if (!kvm_vcpu_cache
) {
2055 kvm_chardev_ops
.owner
= module
;
2057 r
= misc_register(&kvm_dev
);
2059 printk(KERN_ERR
"kvm: misc device register failed\n");
2063 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2064 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2069 kmem_cache_destroy(kvm_vcpu_cache
);
2071 sysdev_unregister(&kvm_sysdev
);
2073 sysdev_class_unregister(&kvm_sysdev_class
);
2075 unregister_reboot_notifier(&kvm_reboot_notifier
);
2076 unregister_cpu_notifier(&kvm_cpu_notifier
);
2078 on_each_cpu(hardware_disable
, NULL
, 1);
2080 kvm_arch_hardware_unsetup();
2082 __free_page(bad_page
);
2089 EXPORT_SYMBOL_GPL(kvm_init
);
2093 kvm_trace_cleanup();
2094 misc_deregister(&kvm_dev
);
2095 kmem_cache_destroy(kvm_vcpu_cache
);
2096 sysdev_unregister(&kvm_sysdev
);
2097 sysdev_class_unregister(&kvm_sysdev_class
);
2098 unregister_reboot_notifier(&kvm_reboot_notifier
);
2099 unregister_cpu_notifier(&kvm_cpu_notifier
);
2100 on_each_cpu(hardware_disable
, NULL
, 1);
2101 kvm_arch_hardware_unsetup();
2104 __free_page(bad_page
);
2106 EXPORT_SYMBOL_GPL(kvm_exit
);