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>
44 #include <asm/processor.h>
46 #include <asm/uaccess.h>
47 #include <asm/pgtable.h>
49 MODULE_AUTHOR("Qumranet");
50 MODULE_LICENSE("GPL");
52 DEFINE_SPINLOCK(kvm_lock
);
55 static cpumask_t cpus_hardware_enabled
;
57 struct kmem_cache
*kvm_vcpu_cache
;
58 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
60 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
62 static struct dentry
*debugfs_dir
;
64 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
67 static inline int valid_vcpu(int n
)
69 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
73 * Switches to specified vcpu, until a matching vcpu_put()
75 void vcpu_load(struct kvm_vcpu
*vcpu
)
79 mutex_lock(&vcpu
->mutex
);
81 preempt_notifier_register(&vcpu
->preempt_notifier
);
82 kvm_arch_vcpu_load(vcpu
, cpu
);
86 void vcpu_put(struct kvm_vcpu
*vcpu
)
89 kvm_arch_vcpu_put(vcpu
);
90 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
92 mutex_unlock(&vcpu
->mutex
);
95 static void ack_flush(void *_completed
)
99 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
103 struct kvm_vcpu
*vcpu
;
106 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
107 vcpu
= kvm
->vcpus
[i
];
110 if (test_and_set_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
113 if (cpu
!= -1 && cpu
!= raw_smp_processor_id())
116 if (cpus_empty(cpus
))
118 ++kvm
->stat
.remote_tlb_flush
;
119 smp_call_function_mask(cpus
, ack_flush
, NULL
, 1);
122 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
127 mutex_init(&vcpu
->mutex
);
131 init_waitqueue_head(&vcpu
->wq
);
133 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
138 vcpu
->run
= page_address(page
);
140 r
= kvm_arch_vcpu_init(vcpu
);
146 free_page((unsigned long)vcpu
->run
);
150 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
152 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
154 kvm_arch_vcpu_uninit(vcpu
);
155 free_page((unsigned long)vcpu
->run
);
157 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
159 static struct kvm
*kvm_create_vm(void)
161 struct kvm
*kvm
= kvm_arch_create_vm();
166 kvm
->mm
= current
->mm
;
167 atomic_inc(&kvm
->mm
->mm_count
);
168 kvm_io_bus_init(&kvm
->pio_bus
);
169 mutex_init(&kvm
->lock
);
170 kvm_io_bus_init(&kvm
->mmio_bus
);
171 spin_lock(&kvm_lock
);
172 list_add(&kvm
->vm_list
, &vm_list
);
173 spin_unlock(&kvm_lock
);
179 * Free any memory in @free but not in @dont.
181 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
182 struct kvm_memory_slot
*dont
)
184 if (!dont
|| free
->rmap
!= dont
->rmap
)
187 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
188 vfree(free
->dirty_bitmap
);
191 free
->dirty_bitmap
= NULL
;
195 void kvm_free_physmem(struct kvm
*kvm
)
199 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
200 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
203 static void kvm_destroy_vm(struct kvm
*kvm
)
205 struct mm_struct
*mm
= kvm
->mm
;
207 spin_lock(&kvm_lock
);
208 list_del(&kvm
->vm_list
);
209 spin_unlock(&kvm_lock
);
210 kvm_io_bus_destroy(&kvm
->pio_bus
);
211 kvm_io_bus_destroy(&kvm
->mmio_bus
);
212 kvm_arch_destroy_vm(kvm
);
216 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
218 struct kvm
*kvm
= filp
->private_data
;
225 * Allocate some memory and give it an address in the guest physical address
228 * Discontiguous memory is allowed, mostly for framebuffers.
230 * Must be called holding kvm->lock.
232 int __kvm_set_memory_region(struct kvm
*kvm
,
233 struct kvm_userspace_memory_region
*mem
,
238 unsigned long npages
;
240 struct kvm_memory_slot
*memslot
;
241 struct kvm_memory_slot old
, new;
244 /* General sanity checks */
245 if (mem
->memory_size
& (PAGE_SIZE
- 1))
247 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
249 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
251 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
254 memslot
= &kvm
->memslots
[mem
->slot
];
255 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
256 npages
= mem
->memory_size
>> PAGE_SHIFT
;
259 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
261 new = old
= *memslot
;
263 new.base_gfn
= base_gfn
;
265 new.flags
= mem
->flags
;
267 /* Disallow changing a memory slot's size. */
269 if (npages
&& old
.npages
&& npages
!= old
.npages
)
272 /* Check for overlaps */
274 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
275 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
279 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
280 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
284 /* Free page dirty bitmap if unneeded */
285 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
286 new.dirty_bitmap
= NULL
;
290 /* Allocate if a slot is being created */
291 if (npages
&& !new.rmap
) {
292 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
297 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
299 new.user_alloc
= user_alloc
;
300 new.userspace_addr
= mem
->userspace_addr
;
303 /* Allocate page dirty bitmap if needed */
304 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
305 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
307 new.dirty_bitmap
= vmalloc(dirty_bytes
);
308 if (!new.dirty_bitmap
)
310 memset(new.dirty_bitmap
, 0, dirty_bytes
);
313 if (mem
->slot
>= kvm
->nmemslots
)
314 kvm
->nmemslots
= mem
->slot
+ 1;
318 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
324 kvm_free_physmem_slot(&old
, &new);
328 kvm_free_physmem_slot(&new, &old
);
333 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
335 int kvm_set_memory_region(struct kvm
*kvm
,
336 struct kvm_userspace_memory_region
*mem
,
341 mutex_lock(&kvm
->lock
);
342 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
343 mutex_unlock(&kvm
->lock
);
346 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
348 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
350 kvm_userspace_memory_region
*mem
,
353 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
355 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
358 int kvm_get_dirty_log(struct kvm
*kvm
,
359 struct kvm_dirty_log
*log
, int *is_dirty
)
361 struct kvm_memory_slot
*memslot
;
364 unsigned long any
= 0;
367 if (log
->slot
>= KVM_MEMORY_SLOTS
)
370 memslot
= &kvm
->memslots
[log
->slot
];
372 if (!memslot
->dirty_bitmap
)
375 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
377 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
378 any
= memslot
->dirty_bitmap
[i
];
381 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
392 int is_error_page(struct page
*page
)
394 return page
== bad_page
;
396 EXPORT_SYMBOL_GPL(is_error_page
);
398 static inline unsigned long bad_hva(void)
403 int kvm_is_error_hva(unsigned long addr
)
405 return addr
== bad_hva();
407 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
409 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
413 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
414 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
416 if (gfn
>= memslot
->base_gfn
417 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
423 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
425 gfn
= unalias_gfn(kvm
, gfn
);
426 return __gfn_to_memslot(kvm
, gfn
);
429 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
433 gfn
= unalias_gfn(kvm
, gfn
);
434 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
435 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
437 if (gfn
>= memslot
->base_gfn
438 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
443 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
445 static unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
447 struct kvm_memory_slot
*slot
;
449 gfn
= unalias_gfn(kvm
, gfn
);
450 slot
= __gfn_to_memslot(kvm
, gfn
);
453 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
457 * Requires current->mm->mmap_sem to be held
459 static struct page
*__gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
461 struct page
*page
[1];
467 addr
= gfn_to_hva(kvm
, gfn
);
468 if (kvm_is_error_hva(addr
)) {
473 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 1, page
,
484 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
488 down_read(¤t
->mm
->mmap_sem
);
489 page
= __gfn_to_page(kvm
, gfn
);
490 up_read(¤t
->mm
->mmap_sem
);
495 EXPORT_SYMBOL_GPL(gfn_to_page
);
497 void kvm_release_page_clean(struct page
*page
)
501 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
503 void kvm_release_page_dirty(struct page
*page
)
505 if (!PageReserved(page
))
509 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
511 static int next_segment(unsigned long len
, int offset
)
513 if (len
> PAGE_SIZE
- offset
)
514 return PAGE_SIZE
- offset
;
519 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
525 addr
= gfn_to_hva(kvm
, gfn
);
526 if (kvm_is_error_hva(addr
))
528 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
533 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
535 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
537 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
539 int offset
= offset_in_page(gpa
);
542 while ((seg
= next_segment(len
, offset
)) != 0) {
543 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
553 EXPORT_SYMBOL_GPL(kvm_read_guest
);
555 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
561 addr
= gfn_to_hva(kvm
, gfn
);
562 if (kvm_is_error_hva(addr
))
564 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
567 mark_page_dirty(kvm
, gfn
);
570 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
572 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
575 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
577 int offset
= offset_in_page(gpa
);
580 while ((seg
= next_segment(len
, offset
)) != 0) {
581 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
592 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
594 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
596 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
598 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
600 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
602 int offset
= offset_in_page(gpa
);
605 while ((seg
= next_segment(len
, offset
)) != 0) {
606 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
615 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
617 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
619 struct kvm_memory_slot
*memslot
;
621 gfn
= unalias_gfn(kvm
, gfn
);
622 memslot
= __gfn_to_memslot(kvm
, gfn
);
623 if (memslot
&& memslot
->dirty_bitmap
) {
624 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
627 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
628 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
633 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
635 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
637 DECLARE_WAITQUEUE(wait
, current
);
639 add_wait_queue(&vcpu
->wq
, &wait
);
642 * We will block until either an interrupt or a signal wakes us up
644 while (!kvm_cpu_has_interrupt(vcpu
)
645 && !signal_pending(current
)
646 && !kvm_arch_vcpu_runnable(vcpu
)) {
647 set_current_state(TASK_INTERRUPTIBLE
);
653 __set_current_state(TASK_RUNNING
);
654 remove_wait_queue(&vcpu
->wq
, &wait
);
657 void kvm_resched(struct kvm_vcpu
*vcpu
)
663 EXPORT_SYMBOL_GPL(kvm_resched
);
665 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
667 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
671 page
= virt_to_page(vcpu
->run
);
672 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
673 page
= virt_to_page(vcpu
->arch
.pio_data
);
675 return VM_FAULT_SIGBUS
;
681 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
682 .fault
= kvm_vcpu_fault
,
685 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
687 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
691 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
693 struct kvm_vcpu
*vcpu
= filp
->private_data
;
695 fput(vcpu
->kvm
->filp
);
699 static struct file_operations kvm_vcpu_fops
= {
700 .release
= kvm_vcpu_release
,
701 .unlocked_ioctl
= kvm_vcpu_ioctl
,
702 .compat_ioctl
= kvm_vcpu_ioctl
,
703 .mmap
= kvm_vcpu_mmap
,
707 * Allocates an inode for the vcpu.
709 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
715 r
= anon_inode_getfd(&fd
, &inode
, &file
,
716 "kvm-vcpu", &kvm_vcpu_fops
, vcpu
);
719 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
724 * Creates some virtual cpus. Good luck creating more than one.
726 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
729 struct kvm_vcpu
*vcpu
;
734 vcpu
= kvm_arch_vcpu_create(kvm
, n
);
736 return PTR_ERR(vcpu
);
738 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
740 r
= kvm_arch_vcpu_setup(vcpu
);
744 mutex_lock(&kvm
->lock
);
747 mutex_unlock(&kvm
->lock
);
750 kvm
->vcpus
[n
] = vcpu
;
751 mutex_unlock(&kvm
->lock
);
753 /* Now it's all set up, let userspace reach it */
754 r
= create_vcpu_fd(vcpu
);
760 mutex_lock(&kvm
->lock
);
761 kvm
->vcpus
[n
] = NULL
;
762 mutex_unlock(&kvm
->lock
);
764 kvm_arch_vcpu_destroy(vcpu
);
768 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
771 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
772 vcpu
->sigset_active
= 1;
773 vcpu
->sigset
= *sigset
;
775 vcpu
->sigset_active
= 0;
779 static long kvm_vcpu_ioctl(struct file
*filp
,
780 unsigned int ioctl
, unsigned long arg
)
782 struct kvm_vcpu
*vcpu
= filp
->private_data
;
783 void __user
*argp
= (void __user
*)arg
;
786 if (vcpu
->kvm
->mm
!= current
->mm
)
793 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
796 struct kvm_regs kvm_regs
;
798 memset(&kvm_regs
, 0, sizeof kvm_regs
);
799 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
803 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
809 struct kvm_regs kvm_regs
;
812 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
814 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
820 case KVM_GET_SREGS
: {
821 struct kvm_sregs kvm_sregs
;
823 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
824 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
828 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
833 case KVM_SET_SREGS
: {
834 struct kvm_sregs kvm_sregs
;
837 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
839 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
845 case KVM_TRANSLATE
: {
846 struct kvm_translation tr
;
849 if (copy_from_user(&tr
, argp
, sizeof tr
))
851 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
855 if (copy_to_user(argp
, &tr
, sizeof tr
))
860 case KVM_DEBUG_GUEST
: {
861 struct kvm_debug_guest dbg
;
864 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
866 r
= kvm_arch_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
872 case KVM_SET_SIGNAL_MASK
: {
873 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
874 struct kvm_signal_mask kvm_sigmask
;
880 if (copy_from_user(&kvm_sigmask
, argp
,
884 if (kvm_sigmask
.len
!= sizeof sigset
)
887 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
892 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
898 memset(&fpu
, 0, sizeof fpu
);
899 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, &fpu
);
903 if (copy_to_user(argp
, &fpu
, sizeof fpu
))
912 if (copy_from_user(&fpu
, argp
, sizeof fpu
))
914 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, &fpu
);
921 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
927 static long kvm_vm_ioctl(struct file
*filp
,
928 unsigned int ioctl
, unsigned long arg
)
930 struct kvm
*kvm
= filp
->private_data
;
931 void __user
*argp
= (void __user
*)arg
;
934 if (kvm
->mm
!= current
->mm
)
937 case KVM_CREATE_VCPU
:
938 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
942 case KVM_SET_USER_MEMORY_REGION
: {
943 struct kvm_userspace_memory_region kvm_userspace_mem
;
946 if (copy_from_user(&kvm_userspace_mem
, argp
,
947 sizeof kvm_userspace_mem
))
950 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
955 case KVM_GET_DIRTY_LOG
: {
956 struct kvm_dirty_log log
;
959 if (copy_from_user(&log
, argp
, sizeof log
))
961 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
967 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
973 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
975 struct kvm
*kvm
= vma
->vm_file
->private_data
;
978 if (!kvm_is_visible_gfn(kvm
, vmf
->pgoff
))
979 return VM_FAULT_SIGBUS
;
980 /* current->mm->mmap_sem is already held so call lockless version */
981 page
= __gfn_to_page(kvm
, vmf
->pgoff
);
982 if (is_error_page(page
)) {
983 kvm_release_page_clean(page
);
984 return VM_FAULT_SIGBUS
;
990 static struct vm_operations_struct kvm_vm_vm_ops
= {
991 .fault
= kvm_vm_fault
,
994 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
996 vma
->vm_ops
= &kvm_vm_vm_ops
;
1000 static struct file_operations kvm_vm_fops
= {
1001 .release
= kvm_vm_release
,
1002 .unlocked_ioctl
= kvm_vm_ioctl
,
1003 .compat_ioctl
= kvm_vm_ioctl
,
1004 .mmap
= kvm_vm_mmap
,
1007 static int kvm_dev_ioctl_create_vm(void)
1010 struct inode
*inode
;
1014 kvm
= kvm_create_vm();
1016 return PTR_ERR(kvm
);
1017 r
= anon_inode_getfd(&fd
, &inode
, &file
, "kvm-vm", &kvm_vm_fops
, kvm
);
1019 kvm_destroy_vm(kvm
);
1028 static long kvm_dev_ioctl(struct file
*filp
,
1029 unsigned int ioctl
, unsigned long arg
)
1031 void __user
*argp
= (void __user
*)arg
;
1035 case KVM_GET_API_VERSION
:
1039 r
= KVM_API_VERSION
;
1045 r
= kvm_dev_ioctl_create_vm();
1047 case KVM_CHECK_EXTENSION
:
1048 r
= kvm_dev_ioctl_check_extension((long)argp
);
1050 case KVM_GET_VCPU_MMAP_SIZE
:
1057 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1063 static struct file_operations kvm_chardev_ops
= {
1064 .unlocked_ioctl
= kvm_dev_ioctl
,
1065 .compat_ioctl
= kvm_dev_ioctl
,
1068 static struct miscdevice kvm_dev
= {
1074 static void hardware_enable(void *junk
)
1076 int cpu
= raw_smp_processor_id();
1078 if (cpu_isset(cpu
, cpus_hardware_enabled
))
1080 cpu_set(cpu
, cpus_hardware_enabled
);
1081 kvm_arch_hardware_enable(NULL
);
1084 static void hardware_disable(void *junk
)
1086 int cpu
= raw_smp_processor_id();
1088 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
1090 cpu_clear(cpu
, cpus_hardware_enabled
);
1091 decache_vcpus_on_cpu(cpu
);
1092 kvm_arch_hardware_disable(NULL
);
1095 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1100 val
&= ~CPU_TASKS_FROZEN
;
1103 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1105 hardware_disable(NULL
);
1107 case CPU_UP_CANCELED
:
1108 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1110 smp_call_function_single(cpu
, hardware_disable
, NULL
, 0, 1);
1113 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1115 smp_call_function_single(cpu
, hardware_enable
, NULL
, 0, 1);
1121 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1124 if (val
== SYS_RESTART
) {
1126 * Some (well, at least mine) BIOSes hang on reboot if
1129 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1130 on_each_cpu(hardware_disable
, NULL
, 0, 1);
1135 static struct notifier_block kvm_reboot_notifier
= {
1136 .notifier_call
= kvm_reboot
,
1140 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
1142 memset(bus
, 0, sizeof(*bus
));
1145 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
1149 for (i
= 0; i
< bus
->dev_count
; i
++) {
1150 struct kvm_io_device
*pos
= bus
->devs
[i
];
1152 kvm_iodevice_destructor(pos
);
1156 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
, gpa_t addr
)
1160 for (i
= 0; i
< bus
->dev_count
; i
++) {
1161 struct kvm_io_device
*pos
= bus
->devs
[i
];
1163 if (pos
->in_range(pos
, addr
))
1170 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
1172 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
1174 bus
->devs
[bus
->dev_count
++] = dev
;
1177 static struct notifier_block kvm_cpu_notifier
= {
1178 .notifier_call
= kvm_cpu_hotplug
,
1179 .priority
= 20, /* must be > scheduler priority */
1182 static u64
vm_stat_get(void *_offset
)
1184 unsigned offset
= (long)_offset
;
1188 spin_lock(&kvm_lock
);
1189 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1190 total
+= *(u32
*)((void *)kvm
+ offset
);
1191 spin_unlock(&kvm_lock
);
1195 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
1197 static u64
vcpu_stat_get(void *_offset
)
1199 unsigned offset
= (long)_offset
;
1202 struct kvm_vcpu
*vcpu
;
1205 spin_lock(&kvm_lock
);
1206 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1207 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
1208 vcpu
= kvm
->vcpus
[i
];
1210 total
+= *(u32
*)((void *)vcpu
+ offset
);
1212 spin_unlock(&kvm_lock
);
1216 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
1218 static struct file_operations
*stat_fops
[] = {
1219 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
1220 [KVM_STAT_VM
] = &vm_stat_fops
,
1223 static void kvm_init_debug(void)
1225 struct kvm_stats_debugfs_item
*p
;
1227 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
1228 for (p
= debugfs_entries
; p
->name
; ++p
)
1229 p
->dentry
= debugfs_create_file(p
->name
, 0444, debugfs_dir
,
1230 (void *)(long)p
->offset
,
1231 stat_fops
[p
->kind
]);
1234 static void kvm_exit_debug(void)
1236 struct kvm_stats_debugfs_item
*p
;
1238 for (p
= debugfs_entries
; p
->name
; ++p
)
1239 debugfs_remove(p
->dentry
);
1240 debugfs_remove(debugfs_dir
);
1243 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
1245 hardware_disable(NULL
);
1249 static int kvm_resume(struct sys_device
*dev
)
1251 hardware_enable(NULL
);
1255 static struct sysdev_class kvm_sysdev_class
= {
1257 .suspend
= kvm_suspend
,
1258 .resume
= kvm_resume
,
1261 static struct sys_device kvm_sysdev
= {
1263 .cls
= &kvm_sysdev_class
,
1266 struct page
*bad_page
;
1269 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
1271 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
1274 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
1276 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
1278 kvm_arch_vcpu_load(vcpu
, cpu
);
1281 static void kvm_sched_out(struct preempt_notifier
*pn
,
1282 struct task_struct
*next
)
1284 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
1286 kvm_arch_vcpu_put(vcpu
);
1289 int kvm_init(void *opaque
, unsigned int vcpu_size
,
1290 struct module
*module
)
1297 r
= kvm_arch_init(opaque
);
1301 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
1303 if (bad_page
== NULL
) {
1308 r
= kvm_arch_hardware_setup();
1312 for_each_online_cpu(cpu
) {
1313 smp_call_function_single(cpu
,
1314 kvm_arch_check_processor_compat
,
1320 on_each_cpu(hardware_enable
, NULL
, 0, 1);
1321 r
= register_cpu_notifier(&kvm_cpu_notifier
);
1324 register_reboot_notifier(&kvm_reboot_notifier
);
1326 r
= sysdev_class_register(&kvm_sysdev_class
);
1330 r
= sysdev_register(&kvm_sysdev
);
1334 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1335 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
1336 __alignof__(struct kvm_vcpu
),
1338 if (!kvm_vcpu_cache
) {
1343 kvm_chardev_ops
.owner
= module
;
1345 r
= misc_register(&kvm_dev
);
1347 printk(KERN_ERR
"kvm: misc device register failed\n");
1351 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
1352 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
1357 kmem_cache_destroy(kvm_vcpu_cache
);
1359 sysdev_unregister(&kvm_sysdev
);
1361 sysdev_class_unregister(&kvm_sysdev_class
);
1363 unregister_reboot_notifier(&kvm_reboot_notifier
);
1364 unregister_cpu_notifier(&kvm_cpu_notifier
);
1366 on_each_cpu(hardware_disable
, NULL
, 0, 1);
1368 kvm_arch_hardware_unsetup();
1370 __free_page(bad_page
);
1377 EXPORT_SYMBOL_GPL(kvm_init
);
1381 misc_deregister(&kvm_dev
);
1382 kmem_cache_destroy(kvm_vcpu_cache
);
1383 sysdev_unregister(&kvm_sysdev
);
1384 sysdev_class_unregister(&kvm_sysdev_class
);
1385 unregister_reboot_notifier(&kvm_reboot_notifier
);
1386 unregister_cpu_notifier(&kvm_cpu_notifier
);
1387 on_each_cpu(hardware_disable
, NULL
, 0, 1);
1388 kvm_arch_hardware_unsetup();
1391 __free_page(bad_page
);
1393 EXPORT_SYMBOL_GPL(kvm_exit
);