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.
22 #include <linux/kvm.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
28 #include <linux/miscdevice.h>
29 #include <linux/vmalloc.h>
30 #include <linux/reboot.h>
31 #include <linux/debugfs.h>
32 #include <linux/highmem.h>
33 #include <linux/file.h>
34 #include <linux/sysdev.h>
35 #include <linux/cpu.h>
36 #include <linux/sched.h>
37 #include <linux/cpumask.h>
38 #include <linux/smp.h>
39 #include <linux/anon_inodes.h>
40 #include <linux/profile.h>
41 #include <linux/kvm_para.h>
42 #include <linux/pagemap.h>
43 #include <linux/mman.h>
45 #include <asm/processor.h>
47 #include <asm/uaccess.h>
49 #include <asm/pgtable.h>
51 MODULE_AUTHOR("Qumranet");
52 MODULE_LICENSE("GPL");
54 DEFINE_SPINLOCK(kvm_lock
);
57 static cpumask_t cpus_hardware_enabled
;
59 struct kmem_cache
*kvm_vcpu_cache
;
60 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
62 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
64 static struct dentry
*debugfs_dir
;
66 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
69 static inline int valid_vcpu(int n
)
71 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
75 * Switches to specified vcpu, until a matching vcpu_put()
77 void vcpu_load(struct kvm_vcpu
*vcpu
)
81 mutex_lock(&vcpu
->mutex
);
83 preempt_notifier_register(&vcpu
->preempt_notifier
);
84 kvm_arch_vcpu_load(vcpu
, cpu
);
88 void vcpu_put(struct kvm_vcpu
*vcpu
)
91 kvm_arch_vcpu_put(vcpu
);
92 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
94 mutex_unlock(&vcpu
->mutex
);
97 static void ack_flush(void *_completed
)
101 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
105 struct kvm_vcpu
*vcpu
;
108 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
109 vcpu
= kvm
->vcpus
[i
];
112 if (test_and_set_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
115 if (cpu
!= -1 && cpu
!= raw_smp_processor_id())
118 smp_call_function_mask(cpus
, ack_flush
, NULL
, 1);
121 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
126 mutex_init(&vcpu
->mutex
);
130 init_waitqueue_head(&vcpu
->wq
);
132 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
137 vcpu
->run
= page_address(page
);
139 r
= kvm_arch_vcpu_init(vcpu
);
145 free_page((unsigned long)vcpu
->run
);
149 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
151 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
153 kvm_arch_vcpu_uninit(vcpu
);
154 free_page((unsigned long)vcpu
->run
);
156 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
158 static struct kvm
*kvm_create_vm(void)
160 struct kvm
*kvm
= kvm_arch_create_vm();
165 kvm_io_bus_init(&kvm
->pio_bus
);
166 mutex_init(&kvm
->lock
);
167 kvm_io_bus_init(&kvm
->mmio_bus
);
168 spin_lock(&kvm_lock
);
169 list_add(&kvm
->vm_list
, &vm_list
);
170 spin_unlock(&kvm_lock
);
176 * Free any memory in @free but not in @dont.
178 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
179 struct kvm_memory_slot
*dont
)
181 if (!dont
|| free
->rmap
!= dont
->rmap
)
184 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
185 vfree(free
->dirty_bitmap
);
188 free
->dirty_bitmap
= NULL
;
192 void kvm_free_physmem(struct kvm
*kvm
)
196 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
197 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
200 static void kvm_destroy_vm(struct kvm
*kvm
)
202 spin_lock(&kvm_lock
);
203 list_del(&kvm
->vm_list
);
204 spin_unlock(&kvm_lock
);
205 kvm_io_bus_destroy(&kvm
->pio_bus
);
206 kvm_io_bus_destroy(&kvm
->mmio_bus
);
207 kvm_arch_destroy_vm(kvm
);
210 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
212 struct kvm
*kvm
= filp
->private_data
;
219 * Allocate some memory and give it an address in the guest physical address
222 * Discontiguous memory is allowed, mostly for framebuffers.
224 * Must be called holding kvm->lock.
226 int __kvm_set_memory_region(struct kvm
*kvm
,
227 struct kvm_userspace_memory_region
*mem
,
232 unsigned long npages
;
234 struct kvm_memory_slot
*memslot
;
235 struct kvm_memory_slot old
, new;
238 /* General sanity checks */
239 if (mem
->memory_size
& (PAGE_SIZE
- 1))
241 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
243 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
245 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
248 memslot
= &kvm
->memslots
[mem
->slot
];
249 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
250 npages
= mem
->memory_size
>> PAGE_SHIFT
;
253 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
255 new = old
= *memslot
;
257 new.base_gfn
= base_gfn
;
259 new.flags
= mem
->flags
;
261 /* Disallow changing a memory slot's size. */
263 if (npages
&& old
.npages
&& npages
!= old
.npages
)
266 /* Check for overlaps */
268 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
269 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
273 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
274 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
278 /* Free page dirty bitmap if unneeded */
279 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
280 new.dirty_bitmap
= NULL
;
284 /* Allocate if a slot is being created */
285 if (npages
&& !new.rmap
) {
286 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
291 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
293 new.user_alloc
= user_alloc
;
295 new.userspace_addr
= mem
->userspace_addr
;
297 down_write(¤t
->mm
->mmap_sem
);
298 new.userspace_addr
= do_mmap(NULL
, 0,
300 PROT_READ
| PROT_WRITE
,
301 MAP_SHARED
| MAP_ANONYMOUS
,
303 up_write(¤t
->mm
->mmap_sem
);
305 if (IS_ERR((void *)new.userspace_addr
))
309 if (!old
.user_alloc
&& old
.rmap
) {
312 down_write(¤t
->mm
->mmap_sem
);
313 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
314 old
.npages
* PAGE_SIZE
);
315 up_write(¤t
->mm
->mmap_sem
);
318 "kvm_vm_ioctl_set_memory_region: "
319 "failed to munmap memory\n");
323 /* Allocate page dirty bitmap if needed */
324 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
325 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
327 new.dirty_bitmap
= vmalloc(dirty_bytes
);
328 if (!new.dirty_bitmap
)
330 memset(new.dirty_bitmap
, 0, dirty_bytes
);
333 if (mem
->slot
>= kvm
->nmemslots
)
334 kvm
->nmemslots
= mem
->slot
+ 1;
336 if (!kvm
->n_requested_mmu_pages
) {
337 unsigned int n_pages
;
340 n_pages
= npages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
341 kvm_mmu_change_mmu_pages(kvm
, kvm
->n_alloc_mmu_pages
+
344 unsigned int nr_mmu_pages
;
346 n_pages
= old
.npages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
347 nr_mmu_pages
= kvm
->n_alloc_mmu_pages
- n_pages
;
348 nr_mmu_pages
= max(nr_mmu_pages
,
349 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES
);
350 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
356 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
357 kvm_flush_remote_tlbs(kvm
);
359 kvm_free_physmem_slot(&old
, &new);
363 kvm_free_physmem_slot(&new, &old
);
368 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
370 int kvm_set_memory_region(struct kvm
*kvm
,
371 struct kvm_userspace_memory_region
*mem
,
376 mutex_lock(&kvm
->lock
);
377 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
378 mutex_unlock(&kvm
->lock
);
381 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
383 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
385 kvm_userspace_memory_region
*mem
,
388 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
390 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
393 int kvm_get_dirty_log(struct kvm
*kvm
,
394 struct kvm_dirty_log
*log
, int *is_dirty
)
396 struct kvm_memory_slot
*memslot
;
399 unsigned long any
= 0;
402 if (log
->slot
>= KVM_MEMORY_SLOTS
)
405 memslot
= &kvm
->memslots
[log
->slot
];
407 if (!memslot
->dirty_bitmap
)
410 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
412 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
413 any
= memslot
->dirty_bitmap
[i
];
416 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
427 int is_error_page(struct page
*page
)
429 return page
== bad_page
;
431 EXPORT_SYMBOL_GPL(is_error_page
);
433 static inline unsigned long bad_hva(void)
438 int kvm_is_error_hva(unsigned long addr
)
440 return addr
== bad_hva();
442 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
444 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
447 struct kvm_mem_alias
*alias
;
449 for (i
= 0; i
< kvm
->naliases
; ++i
) {
450 alias
= &kvm
->aliases
[i
];
451 if (gfn
>= alias
->base_gfn
452 && gfn
< alias
->base_gfn
+ alias
->npages
)
453 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
458 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
462 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
463 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
465 if (gfn
>= memslot
->base_gfn
466 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
472 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
474 gfn
= unalias_gfn(kvm
, gfn
);
475 return __gfn_to_memslot(kvm
, gfn
);
478 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
482 gfn
= unalias_gfn(kvm
, gfn
);
483 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
484 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
486 if (gfn
>= memslot
->base_gfn
487 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
492 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
494 static unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
496 struct kvm_memory_slot
*slot
;
498 gfn
= unalias_gfn(kvm
, gfn
);
499 slot
= __gfn_to_memslot(kvm
, gfn
);
502 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
506 * Requires current->mm->mmap_sem to be held
508 static struct page
*__gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
510 struct page
*page
[1];
516 addr
= gfn_to_hva(kvm
, gfn
);
517 if (kvm_is_error_hva(addr
)) {
522 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 1, page
,
533 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
537 down_read(¤t
->mm
->mmap_sem
);
538 page
= __gfn_to_page(kvm
, gfn
);
539 up_read(¤t
->mm
->mmap_sem
);
544 EXPORT_SYMBOL_GPL(gfn_to_page
);
546 void kvm_release_page(struct page
*page
)
548 if (!PageReserved(page
))
552 EXPORT_SYMBOL_GPL(kvm_release_page
);
554 static int next_segment(unsigned long len
, int offset
)
556 if (len
> PAGE_SIZE
- offset
)
557 return PAGE_SIZE
- offset
;
562 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
568 addr
= gfn_to_hva(kvm
, gfn
);
569 if (kvm_is_error_hva(addr
))
571 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
576 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
578 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
580 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
582 int offset
= offset_in_page(gpa
);
585 while ((seg
= next_segment(len
, offset
)) != 0) {
586 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
596 EXPORT_SYMBOL_GPL(kvm_read_guest
);
598 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
604 addr
= gfn_to_hva(kvm
, gfn
);
605 if (kvm_is_error_hva(addr
))
607 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
610 mark_page_dirty(kvm
, gfn
);
613 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
615 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
618 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
620 int offset
= offset_in_page(gpa
);
623 while ((seg
= next_segment(len
, offset
)) != 0) {
624 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
635 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
637 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
639 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
641 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
643 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
645 int offset
= offset_in_page(gpa
);
648 while ((seg
= next_segment(len
, offset
)) != 0) {
649 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
658 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
660 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
662 struct kvm_memory_slot
*memslot
;
664 gfn
= unalias_gfn(kvm
, gfn
);
665 memslot
= __gfn_to_memslot(kvm
, gfn
);
666 if (memslot
&& memslot
->dirty_bitmap
) {
667 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
670 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
671 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
676 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
678 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
680 DECLARE_WAITQUEUE(wait
, current
);
682 add_wait_queue(&vcpu
->wq
, &wait
);
685 * We will block until either an interrupt or a signal wakes us up
687 while (!kvm_cpu_has_interrupt(vcpu
)
688 && !signal_pending(current
)
689 && vcpu
->mp_state
!= VCPU_MP_STATE_RUNNABLE
690 && vcpu
->mp_state
!= VCPU_MP_STATE_SIPI_RECEIVED
) {
691 set_current_state(TASK_INTERRUPTIBLE
);
697 __set_current_state(TASK_RUNNING
);
698 remove_wait_queue(&vcpu
->wq
, &wait
);
701 void kvm_resched(struct kvm_vcpu
*vcpu
)
707 EXPORT_SYMBOL_GPL(kvm_resched
);
709 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
710 struct kvm_interrupt
*irq
)
712 if (irq
->irq
< 0 || irq
->irq
>= 256)
714 if (irqchip_in_kernel(vcpu
->kvm
))
718 set_bit(irq
->irq
, vcpu
->irq_pending
);
719 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
726 static struct page
*kvm_vcpu_nopage(struct vm_area_struct
*vma
,
727 unsigned long address
,
730 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
734 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
736 page
= virt_to_page(vcpu
->run
);
737 else if (pgoff
== KVM_PIO_PAGE_OFFSET
)
738 page
= virt_to_page(vcpu
->pio_data
);
740 return NOPAGE_SIGBUS
;
743 *type
= VM_FAULT_MINOR
;
748 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
749 .nopage
= kvm_vcpu_nopage
,
752 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
754 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
758 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
760 struct kvm_vcpu
*vcpu
= filp
->private_data
;
762 fput(vcpu
->kvm
->filp
);
766 static struct file_operations kvm_vcpu_fops
= {
767 .release
= kvm_vcpu_release
,
768 .unlocked_ioctl
= kvm_vcpu_ioctl
,
769 .compat_ioctl
= kvm_vcpu_ioctl
,
770 .mmap
= kvm_vcpu_mmap
,
774 * Allocates an inode for the vcpu.
776 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
782 r
= anon_inode_getfd(&fd
, &inode
, &file
,
783 "kvm-vcpu", &kvm_vcpu_fops
, vcpu
);
786 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
791 * Creates some virtual cpus. Good luck creating more than one.
793 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
796 struct kvm_vcpu
*vcpu
;
801 vcpu
= kvm_arch_vcpu_create(kvm
, n
);
803 return PTR_ERR(vcpu
);
805 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
807 mutex_lock(&kvm
->lock
);
810 mutex_unlock(&kvm
->lock
);
813 kvm
->vcpus
[n
] = vcpu
;
814 mutex_unlock(&kvm
->lock
);
816 /* Now it's all set up, let userspace reach it */
817 r
= create_vcpu_fd(vcpu
);
823 mutex_lock(&kvm
->lock
);
824 kvm
->vcpus
[n
] = NULL
;
825 mutex_unlock(&kvm
->lock
);
827 kvm_arch_vcpu_destory(vcpu
);
831 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
834 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
835 vcpu
->sigset_active
= 1;
836 vcpu
->sigset
= *sigset
;
838 vcpu
->sigset_active
= 0;
842 static long kvm_vcpu_ioctl(struct file
*filp
,
843 unsigned int ioctl
, unsigned long arg
)
845 struct kvm_vcpu
*vcpu
= filp
->private_data
;
846 void __user
*argp
= (void __user
*)arg
;
854 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
857 struct kvm_regs kvm_regs
;
859 memset(&kvm_regs
, 0, sizeof kvm_regs
);
860 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
864 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
870 struct kvm_regs kvm_regs
;
873 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
875 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
881 case KVM_GET_SREGS
: {
882 struct kvm_sregs kvm_sregs
;
884 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
885 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
889 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
894 case KVM_SET_SREGS
: {
895 struct kvm_sregs kvm_sregs
;
898 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
900 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
906 case KVM_TRANSLATE
: {
907 struct kvm_translation tr
;
910 if (copy_from_user(&tr
, argp
, sizeof tr
))
912 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
916 if (copy_to_user(argp
, &tr
, sizeof tr
))
921 case KVM_INTERRUPT
: {
922 struct kvm_interrupt irq
;
925 if (copy_from_user(&irq
, argp
, sizeof irq
))
927 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
933 case KVM_DEBUG_GUEST
: {
934 struct kvm_debug_guest dbg
;
937 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
939 r
= kvm_arch_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
945 case KVM_SET_SIGNAL_MASK
: {
946 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
947 struct kvm_signal_mask kvm_sigmask
;
953 if (copy_from_user(&kvm_sigmask
, argp
,
957 if (kvm_sigmask
.len
!= sizeof sigset
)
960 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
965 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
971 memset(&fpu
, 0, sizeof fpu
);
972 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, &fpu
);
976 if (copy_to_user(argp
, &fpu
, sizeof fpu
))
985 if (copy_from_user(&fpu
, argp
, sizeof fpu
))
987 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, &fpu
);
994 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1000 static long kvm_vm_ioctl(struct file
*filp
,
1001 unsigned int ioctl
, unsigned long arg
)
1003 struct kvm
*kvm
= filp
->private_data
;
1004 void __user
*argp
= (void __user
*)arg
;
1008 case KVM_CREATE_VCPU
:
1009 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1013 case KVM_SET_USER_MEMORY_REGION
: {
1014 struct kvm_userspace_memory_region kvm_userspace_mem
;
1017 if (copy_from_user(&kvm_userspace_mem
, argp
,
1018 sizeof kvm_userspace_mem
))
1021 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1026 case KVM_GET_DIRTY_LOG
: {
1027 struct kvm_dirty_log log
;
1030 if (copy_from_user(&log
, argp
, sizeof log
))
1032 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1038 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1044 static struct page
*kvm_vm_nopage(struct vm_area_struct
*vma
,
1045 unsigned long address
,
1048 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1049 unsigned long pgoff
;
1052 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1053 if (!kvm_is_visible_gfn(kvm
, pgoff
))
1054 return NOPAGE_SIGBUS
;
1055 /* current->mm->mmap_sem is already held so call lockless version */
1056 page
= __gfn_to_page(kvm
, pgoff
);
1057 if (is_error_page(page
)) {
1058 kvm_release_page(page
);
1059 return NOPAGE_SIGBUS
;
1062 *type
= VM_FAULT_MINOR
;
1067 static struct vm_operations_struct kvm_vm_vm_ops
= {
1068 .nopage
= kvm_vm_nopage
,
1071 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1073 vma
->vm_ops
= &kvm_vm_vm_ops
;
1077 static struct file_operations kvm_vm_fops
= {
1078 .release
= kvm_vm_release
,
1079 .unlocked_ioctl
= kvm_vm_ioctl
,
1080 .compat_ioctl
= kvm_vm_ioctl
,
1081 .mmap
= kvm_vm_mmap
,
1084 static int kvm_dev_ioctl_create_vm(void)
1087 struct inode
*inode
;
1091 kvm
= kvm_create_vm();
1093 return PTR_ERR(kvm
);
1094 r
= anon_inode_getfd(&fd
, &inode
, &file
, "kvm-vm", &kvm_vm_fops
, kvm
);
1096 kvm_destroy_vm(kvm
);
1105 static long kvm_dev_ioctl(struct file
*filp
,
1106 unsigned int ioctl
, unsigned long arg
)
1108 void __user
*argp
= (void __user
*)arg
;
1112 case KVM_GET_API_VERSION
:
1116 r
= KVM_API_VERSION
;
1122 r
= kvm_dev_ioctl_create_vm();
1124 case KVM_CHECK_EXTENSION
:
1125 r
= kvm_dev_ioctl_check_extension((long)argp
);
1127 case KVM_GET_VCPU_MMAP_SIZE
:
1134 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1140 static struct file_operations kvm_chardev_ops
= {
1141 .unlocked_ioctl
= kvm_dev_ioctl
,
1142 .compat_ioctl
= kvm_dev_ioctl
,
1145 static struct miscdevice kvm_dev
= {
1151 static void hardware_enable(void *junk
)
1153 int cpu
= raw_smp_processor_id();
1155 if (cpu_isset(cpu
, cpus_hardware_enabled
))
1157 cpu_set(cpu
, cpus_hardware_enabled
);
1158 kvm_arch_hardware_enable(NULL
);
1161 static void hardware_disable(void *junk
)
1163 int cpu
= raw_smp_processor_id();
1165 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
1167 cpu_clear(cpu
, cpus_hardware_enabled
);
1168 decache_vcpus_on_cpu(cpu
);
1169 kvm_arch_hardware_disable(NULL
);
1172 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1177 val
&= ~CPU_TASKS_FROZEN
;
1180 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1182 hardware_disable(NULL
);
1184 case CPU_UP_CANCELED
:
1185 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1187 smp_call_function_single(cpu
, hardware_disable
, NULL
, 0, 1);
1190 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1192 smp_call_function_single(cpu
, hardware_enable
, NULL
, 0, 1);
1198 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1201 if (val
== SYS_RESTART
) {
1203 * Some (well, at least mine) BIOSes hang on reboot if
1206 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1207 on_each_cpu(hardware_disable
, NULL
, 0, 1);
1212 static struct notifier_block kvm_reboot_notifier
= {
1213 .notifier_call
= kvm_reboot
,
1217 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
1219 memset(bus
, 0, sizeof(*bus
));
1222 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
1226 for (i
= 0; i
< bus
->dev_count
; i
++) {
1227 struct kvm_io_device
*pos
= bus
->devs
[i
];
1229 kvm_iodevice_destructor(pos
);
1233 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
, gpa_t addr
)
1237 for (i
= 0; i
< bus
->dev_count
; i
++) {
1238 struct kvm_io_device
*pos
= bus
->devs
[i
];
1240 if (pos
->in_range(pos
, addr
))
1247 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
1249 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
1251 bus
->devs
[bus
->dev_count
++] = dev
;
1254 static struct notifier_block kvm_cpu_notifier
= {
1255 .notifier_call
= kvm_cpu_hotplug
,
1256 .priority
= 20, /* must be > scheduler priority */
1259 static u64
vm_stat_get(void *_offset
)
1261 unsigned offset
= (long)_offset
;
1265 spin_lock(&kvm_lock
);
1266 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1267 total
+= *(u32
*)((void *)kvm
+ offset
);
1268 spin_unlock(&kvm_lock
);
1272 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
1274 static u64
vcpu_stat_get(void *_offset
)
1276 unsigned offset
= (long)_offset
;
1279 struct kvm_vcpu
*vcpu
;
1282 spin_lock(&kvm_lock
);
1283 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1284 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
1285 vcpu
= kvm
->vcpus
[i
];
1287 total
+= *(u32
*)((void *)vcpu
+ offset
);
1289 spin_unlock(&kvm_lock
);
1293 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
1295 static struct file_operations
*stat_fops
[] = {
1296 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
1297 [KVM_STAT_VM
] = &vm_stat_fops
,
1300 static void kvm_init_debug(void)
1302 struct kvm_stats_debugfs_item
*p
;
1304 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
1305 for (p
= debugfs_entries
; p
->name
; ++p
)
1306 p
->dentry
= debugfs_create_file(p
->name
, 0444, debugfs_dir
,
1307 (void *)(long)p
->offset
,
1308 stat_fops
[p
->kind
]);
1311 static void kvm_exit_debug(void)
1313 struct kvm_stats_debugfs_item
*p
;
1315 for (p
= debugfs_entries
; p
->name
; ++p
)
1316 debugfs_remove(p
->dentry
);
1317 debugfs_remove(debugfs_dir
);
1320 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
1322 hardware_disable(NULL
);
1326 static int kvm_resume(struct sys_device
*dev
)
1328 hardware_enable(NULL
);
1332 static struct sysdev_class kvm_sysdev_class
= {
1334 .suspend
= kvm_suspend
,
1335 .resume
= kvm_resume
,
1338 static struct sys_device kvm_sysdev
= {
1340 .cls
= &kvm_sysdev_class
,
1343 struct page
*bad_page
;
1346 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
1348 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
1351 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
1353 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
1355 kvm_arch_vcpu_load(vcpu
, cpu
);
1358 static void kvm_sched_out(struct preempt_notifier
*pn
,
1359 struct task_struct
*next
)
1361 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
1363 kvm_arch_vcpu_put(vcpu
);
1366 int kvm_init(void *opaque
, unsigned int vcpu_size
,
1367 struct module
*module
)
1374 r
= kvm_arch_init(opaque
);
1378 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
1380 if (bad_page
== NULL
) {
1385 r
= kvm_arch_hardware_setup();
1389 for_each_online_cpu(cpu
) {
1390 smp_call_function_single(cpu
,
1391 kvm_arch_check_processor_compat
,
1397 on_each_cpu(hardware_enable
, NULL
, 0, 1);
1398 r
= register_cpu_notifier(&kvm_cpu_notifier
);
1401 register_reboot_notifier(&kvm_reboot_notifier
);
1403 r
= sysdev_class_register(&kvm_sysdev_class
);
1407 r
= sysdev_register(&kvm_sysdev
);
1411 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1412 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
1413 __alignof__(struct kvm_vcpu
),
1415 if (!kvm_vcpu_cache
) {
1420 kvm_chardev_ops
.owner
= module
;
1422 r
= misc_register(&kvm_dev
);
1424 printk(KERN_ERR
"kvm: misc device register failed\n");
1428 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
1429 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
1434 kmem_cache_destroy(kvm_vcpu_cache
);
1436 sysdev_unregister(&kvm_sysdev
);
1438 sysdev_class_unregister(&kvm_sysdev_class
);
1440 unregister_reboot_notifier(&kvm_reboot_notifier
);
1441 unregister_cpu_notifier(&kvm_cpu_notifier
);
1443 on_each_cpu(hardware_disable
, NULL
, 0, 1);
1445 kvm_arch_hardware_unsetup();
1452 EXPORT_SYMBOL_GPL(kvm_init
);
1456 misc_deregister(&kvm_dev
);
1457 kmem_cache_destroy(kvm_vcpu_cache
);
1458 sysdev_unregister(&kvm_sysdev
);
1459 sysdev_class_unregister(&kvm_sysdev_class
);
1460 unregister_reboot_notifier(&kvm_reboot_notifier
);
1461 unregister_cpu_notifier(&kvm_cpu_notifier
);
1462 on_each_cpu(hardware_disable
, NULL
, 0, 1);
1463 kvm_arch_hardware_unsetup();
1466 __free_page(bad_page
);
1468 EXPORT_SYMBOL_GPL(kvm_exit
);