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>
44 #include <linux/bitops.h>
45 #include <linux/spinlock.h>
46 #include <linux/compat.h>
48 #include <asm/processor.h>
50 #include <asm/uaccess.h>
51 #include <asm/pgtable.h>
52 #include <asm-generic/bitops/le.h>
54 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
55 #include "coalesced_mmio.h"
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/kvm.h>
61 MODULE_AUTHOR("Qumranet");
62 MODULE_LICENSE("GPL");
67 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
70 DEFINE_SPINLOCK(kvm_lock
);
73 static cpumask_var_t cpus_hardware_enabled
;
74 static int kvm_usage_count
= 0;
75 static atomic_t hardware_enable_failed
;
77 struct kmem_cache
*kvm_vcpu_cache
;
78 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
80 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
82 struct dentry
*kvm_debugfs_dir
;
84 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
86 static int hardware_enable_all(void);
87 static void hardware_disable_all(void);
89 static bool kvm_rebooting
;
91 static bool largepages_enabled
= true;
93 inline int kvm_is_mmio_pfn(pfn_t pfn
)
96 struct page
*page
= compound_head(pfn_to_page(pfn
));
97 return PageReserved(page
);
104 * Switches to specified vcpu, until a matching vcpu_put()
106 void vcpu_load(struct kvm_vcpu
*vcpu
)
110 mutex_lock(&vcpu
->mutex
);
112 preempt_notifier_register(&vcpu
->preempt_notifier
);
113 kvm_arch_vcpu_load(vcpu
, cpu
);
117 void vcpu_put(struct kvm_vcpu
*vcpu
)
120 kvm_arch_vcpu_put(vcpu
);
121 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
123 mutex_unlock(&vcpu
->mutex
);
126 static void ack_flush(void *_completed
)
130 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
135 struct kvm_vcpu
*vcpu
;
137 zalloc_cpumask_var(&cpus
, GFP_ATOMIC
);
139 spin_lock(&kvm
->requests_lock
);
140 me
= smp_processor_id();
141 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
142 if (test_and_set_bit(req
, &vcpu
->requests
))
145 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
146 cpumask_set_cpu(cpu
, cpus
);
148 if (unlikely(cpus
== NULL
))
149 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
150 else if (!cpumask_empty(cpus
))
151 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
154 spin_unlock(&kvm
->requests_lock
);
155 free_cpumask_var(cpus
);
159 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
161 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
162 ++kvm
->stat
.remote_tlb_flush
;
165 void kvm_reload_remote_mmus(struct kvm
*kvm
)
167 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
170 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
175 mutex_init(&vcpu
->mutex
);
179 init_waitqueue_head(&vcpu
->wq
);
181 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
186 vcpu
->run
= page_address(page
);
188 r
= kvm_arch_vcpu_init(vcpu
);
194 free_page((unsigned long)vcpu
->run
);
198 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
200 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
202 kvm_arch_vcpu_uninit(vcpu
);
203 free_page((unsigned long)vcpu
->run
);
205 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
207 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
208 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
210 return container_of(mn
, struct kvm
, mmu_notifier
);
213 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
214 struct mm_struct
*mm
,
215 unsigned long address
)
217 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
221 * When ->invalidate_page runs, the linux pte has been zapped
222 * already but the page is still allocated until
223 * ->invalidate_page returns. So if we increase the sequence
224 * here the kvm page fault will notice if the spte can't be
225 * established because the page is going to be freed. If
226 * instead the kvm page fault establishes the spte before
227 * ->invalidate_page runs, kvm_unmap_hva will release it
230 * The sequence increase only need to be seen at spin_unlock
231 * time, and not at spin_lock time.
233 * Increasing the sequence after the spin_unlock would be
234 * unsafe because the kvm page fault could then establish the
235 * pte after kvm_unmap_hva returned, without noticing the page
236 * is going to be freed.
238 spin_lock(&kvm
->mmu_lock
);
239 kvm
->mmu_notifier_seq
++;
240 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
241 spin_unlock(&kvm
->mmu_lock
);
243 /* we've to flush the tlb before the pages can be freed */
245 kvm_flush_remote_tlbs(kvm
);
249 static void kvm_mmu_notifier_change_pte(struct mmu_notifier
*mn
,
250 struct mm_struct
*mm
,
251 unsigned long address
,
254 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
256 spin_lock(&kvm
->mmu_lock
);
257 kvm
->mmu_notifier_seq
++;
258 kvm_set_spte_hva(kvm
, address
, pte
);
259 spin_unlock(&kvm
->mmu_lock
);
262 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
263 struct mm_struct
*mm
,
267 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
268 int need_tlb_flush
= 0;
270 spin_lock(&kvm
->mmu_lock
);
272 * The count increase must become visible at unlock time as no
273 * spte can be established without taking the mmu_lock and
274 * count is also read inside the mmu_lock critical section.
276 kvm
->mmu_notifier_count
++;
277 for (; start
< end
; start
+= PAGE_SIZE
)
278 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
279 spin_unlock(&kvm
->mmu_lock
);
281 /* we've to flush the tlb before the pages can be freed */
283 kvm_flush_remote_tlbs(kvm
);
286 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
287 struct mm_struct
*mm
,
291 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
293 spin_lock(&kvm
->mmu_lock
);
295 * This sequence increase will notify the kvm page fault that
296 * the page that is going to be mapped in the spte could have
299 kvm
->mmu_notifier_seq
++;
301 * The above sequence increase must be visible before the
302 * below count decrease but both values are read by the kvm
303 * page fault under mmu_lock spinlock so we don't need to add
304 * a smb_wmb() here in between the two.
306 kvm
->mmu_notifier_count
--;
307 spin_unlock(&kvm
->mmu_lock
);
309 BUG_ON(kvm
->mmu_notifier_count
< 0);
312 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
313 struct mm_struct
*mm
,
314 unsigned long address
)
316 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
319 spin_lock(&kvm
->mmu_lock
);
320 young
= kvm_age_hva(kvm
, address
);
321 spin_unlock(&kvm
->mmu_lock
);
324 kvm_flush_remote_tlbs(kvm
);
329 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
330 struct mm_struct
*mm
)
332 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
333 kvm_arch_flush_shadow(kvm
);
336 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
337 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
338 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
339 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
340 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
341 .change_pte
= kvm_mmu_notifier_change_pte
,
342 .release
= kvm_mmu_notifier_release
,
344 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
346 static struct kvm
*kvm_create_vm(void)
349 struct kvm
*kvm
= kvm_arch_create_vm();
350 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
357 r
= hardware_enable_all();
359 goto out_err_nodisable
;
361 #ifdef CONFIG_HAVE_KVM_IRQCHIP
362 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
363 INIT_HLIST_HEAD(&kvm
->irq_ack_notifier_list
);
366 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
367 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
372 kvm
->coalesced_mmio_ring
=
373 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
376 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
378 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
379 r
= mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
381 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
389 kvm
->mm
= current
->mm
;
390 atomic_inc(&kvm
->mm
->mm_count
);
391 spin_lock_init(&kvm
->mmu_lock
);
392 spin_lock_init(&kvm
->requests_lock
);
393 kvm_io_bus_init(&kvm
->pio_bus
);
394 kvm_eventfd_init(kvm
);
395 mutex_init(&kvm
->lock
);
396 mutex_init(&kvm
->irq_lock
);
397 kvm_io_bus_init(&kvm
->mmio_bus
);
398 init_rwsem(&kvm
->slots_lock
);
399 atomic_set(&kvm
->users_count
, 1);
400 spin_lock(&kvm_lock
);
401 list_add(&kvm
->vm_list
, &vm_list
);
402 spin_unlock(&kvm_lock
);
403 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
404 kvm_coalesced_mmio_init(kvm
);
410 hardware_disable_all();
417 * Free any memory in @free but not in @dont.
419 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
420 struct kvm_memory_slot
*dont
)
424 if (!dont
|| free
->rmap
!= dont
->rmap
)
427 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
428 vfree(free
->dirty_bitmap
);
431 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
432 if (!dont
|| free
->lpage_info
[i
] != dont
->lpage_info
[i
]) {
433 vfree(free
->lpage_info
[i
]);
434 free
->lpage_info
[i
] = NULL
;
439 free
->dirty_bitmap
= NULL
;
443 void kvm_free_physmem(struct kvm
*kvm
)
447 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
448 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
451 static void kvm_destroy_vm(struct kvm
*kvm
)
453 struct mm_struct
*mm
= kvm
->mm
;
455 kvm_arch_sync_events(kvm
);
456 spin_lock(&kvm_lock
);
457 list_del(&kvm
->vm_list
);
458 spin_unlock(&kvm_lock
);
459 kvm_free_irq_routing(kvm
);
460 kvm_io_bus_destroy(&kvm
->pio_bus
);
461 kvm_io_bus_destroy(&kvm
->mmio_bus
);
462 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
463 if (kvm
->coalesced_mmio_ring
!= NULL
)
464 free_page((unsigned long)kvm
->coalesced_mmio_ring
);
466 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
467 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
469 kvm_arch_flush_shadow(kvm
);
471 kvm_arch_destroy_vm(kvm
);
472 hardware_disable_all();
476 void kvm_get_kvm(struct kvm
*kvm
)
478 atomic_inc(&kvm
->users_count
);
480 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
482 void kvm_put_kvm(struct kvm
*kvm
)
484 if (atomic_dec_and_test(&kvm
->users_count
))
487 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
490 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
492 struct kvm
*kvm
= filp
->private_data
;
494 kvm_irqfd_release(kvm
);
501 * Allocate some memory and give it an address in the guest physical address
504 * Discontiguous memory is allowed, mostly for framebuffers.
506 * Must be called holding mmap_sem for write.
508 int __kvm_set_memory_region(struct kvm
*kvm
,
509 struct kvm_userspace_memory_region
*mem
,
514 unsigned long npages
;
516 struct kvm_memory_slot
*memslot
;
517 struct kvm_memory_slot old
, new;
520 /* General sanity checks */
521 if (mem
->memory_size
& (PAGE_SIZE
- 1))
523 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
525 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
527 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
529 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
532 memslot
= &kvm
->memslots
[mem
->slot
];
533 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
534 npages
= mem
->memory_size
>> PAGE_SHIFT
;
537 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
539 new = old
= *memslot
;
541 new.base_gfn
= base_gfn
;
543 new.flags
= mem
->flags
;
545 /* Disallow changing a memory slot's size. */
547 if (npages
&& old
.npages
&& npages
!= old
.npages
)
550 /* Check for overlaps */
552 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
553 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
555 if (s
== memslot
|| !s
->npages
)
557 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
558 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
562 /* Free page dirty bitmap if unneeded */
563 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
564 new.dirty_bitmap
= NULL
;
568 /* Allocate if a slot is being created */
570 if (npages
&& !new.rmap
) {
571 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
576 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
578 new.user_alloc
= user_alloc
;
580 * hva_to_rmmap() serialzies with the mmu_lock and to be
581 * safe it has to ignore memslots with !user_alloc &&
585 new.userspace_addr
= mem
->userspace_addr
;
587 new.userspace_addr
= 0;
592 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
598 /* Avoid unused variable warning if no large pages */
601 if (new.lpage_info
[i
])
604 lpages
= 1 + (base_gfn
+ npages
- 1) /
605 KVM_PAGES_PER_HPAGE(level
);
606 lpages
-= base_gfn
/ KVM_PAGES_PER_HPAGE(level
);
608 new.lpage_info
[i
] = vmalloc(lpages
* sizeof(*new.lpage_info
[i
]));
610 if (!new.lpage_info
[i
])
613 memset(new.lpage_info
[i
], 0,
614 lpages
* sizeof(*new.lpage_info
[i
]));
616 if (base_gfn
% KVM_PAGES_PER_HPAGE(level
))
617 new.lpage_info
[i
][0].write_count
= 1;
618 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE(level
))
619 new.lpage_info
[i
][lpages
- 1].write_count
= 1;
620 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
622 * If the gfn and userspace address are not aligned wrt each
623 * other, or if explicitly asked to, disable large page
624 * support for this slot
626 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE(level
) - 1) ||
628 for (j
= 0; j
< lpages
; ++j
)
629 new.lpage_info
[i
][j
].write_count
= 1;
634 /* Allocate page dirty bitmap if needed */
635 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
636 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
638 new.dirty_bitmap
= vmalloc(dirty_bytes
);
639 if (!new.dirty_bitmap
)
641 memset(new.dirty_bitmap
, 0, dirty_bytes
);
643 kvm_arch_flush_shadow(kvm
);
645 #else /* not defined CONFIG_S390 */
646 new.user_alloc
= user_alloc
;
648 new.userspace_addr
= mem
->userspace_addr
;
649 #endif /* not defined CONFIG_S390 */
652 kvm_arch_flush_shadow(kvm
);
654 spin_lock(&kvm
->mmu_lock
);
655 if (mem
->slot
>= kvm
->nmemslots
)
656 kvm
->nmemslots
= mem
->slot
+ 1;
659 spin_unlock(&kvm
->mmu_lock
);
661 r
= kvm_arch_set_memory_region(kvm
, mem
, old
, user_alloc
);
663 spin_lock(&kvm
->mmu_lock
);
665 spin_unlock(&kvm
->mmu_lock
);
669 kvm_free_physmem_slot(&old
, npages
? &new : NULL
);
670 /* Slot deletion case: we have to update the current slot */
671 spin_lock(&kvm
->mmu_lock
);
674 spin_unlock(&kvm
->mmu_lock
);
676 /* map the pages in iommu page table */
677 r
= kvm_iommu_map_pages(kvm
, base_gfn
, npages
);
684 kvm_free_physmem_slot(&new, &old
);
689 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
691 int kvm_set_memory_region(struct kvm
*kvm
,
692 struct kvm_userspace_memory_region
*mem
,
697 down_write(&kvm
->slots_lock
);
698 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
699 up_write(&kvm
->slots_lock
);
702 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
704 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
706 kvm_userspace_memory_region
*mem
,
709 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
711 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
714 int kvm_get_dirty_log(struct kvm
*kvm
,
715 struct kvm_dirty_log
*log
, int *is_dirty
)
717 struct kvm_memory_slot
*memslot
;
720 unsigned long any
= 0;
723 if (log
->slot
>= KVM_MEMORY_SLOTS
)
726 memslot
= &kvm
->memslots
[log
->slot
];
728 if (!memslot
->dirty_bitmap
)
731 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
733 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
734 any
= memslot
->dirty_bitmap
[i
];
737 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
748 void kvm_disable_largepages(void)
750 largepages_enabled
= false;
752 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
754 int is_error_page(struct page
*page
)
756 return page
== bad_page
;
758 EXPORT_SYMBOL_GPL(is_error_page
);
760 int is_error_pfn(pfn_t pfn
)
762 return pfn
== bad_pfn
;
764 EXPORT_SYMBOL_GPL(is_error_pfn
);
766 static inline unsigned long bad_hva(void)
771 int kvm_is_error_hva(unsigned long addr
)
773 return addr
== bad_hva();
775 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
777 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
781 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
782 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
784 if (gfn
>= memslot
->base_gfn
785 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
790 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
792 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
794 gfn
= unalias_gfn(kvm
, gfn
);
795 return gfn_to_memslot_unaliased(kvm
, gfn
);
798 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
802 gfn
= unalias_gfn(kvm
, gfn
);
803 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
804 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
806 if (gfn
>= memslot
->base_gfn
807 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
812 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
814 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
816 struct kvm_memory_slot
*slot
;
818 gfn
= unalias_gfn(kvm
, gfn
);
819 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
822 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
824 EXPORT_SYMBOL_GPL(gfn_to_hva
);
826 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
828 struct page
*page
[1];
835 addr
= gfn_to_hva(kvm
, gfn
);
836 if (kvm_is_error_hva(addr
)) {
838 return page_to_pfn(bad_page
);
841 npages
= get_user_pages_fast(addr
, 1, 1, page
);
843 if (unlikely(npages
!= 1)) {
844 struct vm_area_struct
*vma
;
846 down_read(¤t
->mm
->mmap_sem
);
847 vma
= find_vma(current
->mm
, addr
);
849 if (vma
== NULL
|| addr
< vma
->vm_start
||
850 !(vma
->vm_flags
& VM_PFNMAP
)) {
851 up_read(¤t
->mm
->mmap_sem
);
853 return page_to_pfn(bad_page
);
856 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
857 up_read(¤t
->mm
->mmap_sem
);
858 BUG_ON(!kvm_is_mmio_pfn(pfn
));
860 pfn
= page_to_pfn(page
[0]);
865 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
867 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
871 pfn
= gfn_to_pfn(kvm
, gfn
);
872 if (!kvm_is_mmio_pfn(pfn
))
873 return pfn_to_page(pfn
);
875 WARN_ON(kvm_is_mmio_pfn(pfn
));
881 EXPORT_SYMBOL_GPL(gfn_to_page
);
883 void kvm_release_page_clean(struct page
*page
)
885 kvm_release_pfn_clean(page_to_pfn(page
));
887 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
889 void kvm_release_pfn_clean(pfn_t pfn
)
891 if (!kvm_is_mmio_pfn(pfn
))
892 put_page(pfn_to_page(pfn
));
894 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
896 void kvm_release_page_dirty(struct page
*page
)
898 kvm_release_pfn_dirty(page_to_pfn(page
));
900 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
902 void kvm_release_pfn_dirty(pfn_t pfn
)
904 kvm_set_pfn_dirty(pfn
);
905 kvm_release_pfn_clean(pfn
);
907 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
909 void kvm_set_page_dirty(struct page
*page
)
911 kvm_set_pfn_dirty(page_to_pfn(page
));
913 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
915 void kvm_set_pfn_dirty(pfn_t pfn
)
917 if (!kvm_is_mmio_pfn(pfn
)) {
918 struct page
*page
= pfn_to_page(pfn
);
919 if (!PageReserved(page
))
923 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
925 void kvm_set_pfn_accessed(pfn_t pfn
)
927 if (!kvm_is_mmio_pfn(pfn
))
928 mark_page_accessed(pfn_to_page(pfn
));
930 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
932 void kvm_get_pfn(pfn_t pfn
)
934 if (!kvm_is_mmio_pfn(pfn
))
935 get_page(pfn_to_page(pfn
));
937 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
939 static int next_segment(unsigned long len
, int offset
)
941 if (len
> PAGE_SIZE
- offset
)
942 return PAGE_SIZE
- offset
;
947 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
953 addr
= gfn_to_hva(kvm
, gfn
);
954 if (kvm_is_error_hva(addr
))
956 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
961 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
963 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
965 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
967 int offset
= offset_in_page(gpa
);
970 while ((seg
= next_segment(len
, offset
)) != 0) {
971 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
981 EXPORT_SYMBOL_GPL(kvm_read_guest
);
983 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
988 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
989 int offset
= offset_in_page(gpa
);
991 addr
= gfn_to_hva(kvm
, gfn
);
992 if (kvm_is_error_hva(addr
))
995 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1001 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1003 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1004 int offset
, int len
)
1009 addr
= gfn_to_hva(kvm
, gfn
);
1010 if (kvm_is_error_hva(addr
))
1012 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1015 mark_page_dirty(kvm
, gfn
);
1018 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1020 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1023 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1025 int offset
= offset_in_page(gpa
);
1028 while ((seg
= next_segment(len
, offset
)) != 0) {
1029 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1040 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1042 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1044 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1046 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1048 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1050 int offset
= offset_in_page(gpa
);
1053 while ((seg
= next_segment(len
, offset
)) != 0) {
1054 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1063 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1065 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1067 struct kvm_memory_slot
*memslot
;
1069 gfn
= unalias_gfn(kvm
, gfn
);
1070 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1071 if (memslot
&& memslot
->dirty_bitmap
) {
1072 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1075 if (!generic_test_le_bit(rel_gfn
, memslot
->dirty_bitmap
))
1076 generic___set_le_bit(rel_gfn
, memslot
->dirty_bitmap
);
1081 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1083 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1088 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1090 if (kvm_arch_vcpu_runnable(vcpu
)) {
1091 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1094 if (kvm_cpu_has_pending_timer(vcpu
))
1096 if (signal_pending(current
))
1102 finish_wait(&vcpu
->wq
, &wait
);
1105 void kvm_resched(struct kvm_vcpu
*vcpu
)
1107 if (!need_resched())
1111 EXPORT_SYMBOL_GPL(kvm_resched
);
1113 void kvm_vcpu_on_spin(struct kvm_vcpu
*vcpu
)
1118 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1120 /* Sleep for 100 us, and hope lock-holder got scheduled */
1121 expires
= ktime_add_ns(ktime_get(), 100000UL);
1122 schedule_hrtimeout(&expires
, HRTIMER_MODE_ABS
);
1124 finish_wait(&vcpu
->wq
, &wait
);
1126 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1128 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1130 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1133 if (vmf
->pgoff
== 0)
1134 page
= virt_to_page(vcpu
->run
);
1136 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1137 page
= virt_to_page(vcpu
->arch
.pio_data
);
1139 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1140 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1141 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1144 return VM_FAULT_SIGBUS
;
1150 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1151 .fault
= kvm_vcpu_fault
,
1154 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1156 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1160 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1162 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1164 kvm_put_kvm(vcpu
->kvm
);
1168 static struct file_operations kvm_vcpu_fops
= {
1169 .release
= kvm_vcpu_release
,
1170 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1171 .compat_ioctl
= kvm_vcpu_ioctl
,
1172 .mmap
= kvm_vcpu_mmap
,
1176 * Allocates an inode for the vcpu.
1178 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1180 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, 0);
1184 * Creates some virtual cpus. Good luck creating more than one.
1186 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1189 struct kvm_vcpu
*vcpu
, *v
;
1191 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1193 return PTR_ERR(vcpu
);
1195 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1197 r
= kvm_arch_vcpu_setup(vcpu
);
1201 mutex_lock(&kvm
->lock
);
1202 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1207 kvm_for_each_vcpu(r
, v
, kvm
)
1208 if (v
->vcpu_id
== id
) {
1213 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1215 /* Now it's all set up, let userspace reach it */
1217 r
= create_vcpu_fd(vcpu
);
1223 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1225 atomic_inc(&kvm
->online_vcpus
);
1227 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1228 if (kvm
->bsp_vcpu_id
== id
)
1229 kvm
->bsp_vcpu
= vcpu
;
1231 mutex_unlock(&kvm
->lock
);
1235 mutex_unlock(&kvm
->lock
);
1236 kvm_arch_vcpu_destroy(vcpu
);
1240 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1243 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1244 vcpu
->sigset_active
= 1;
1245 vcpu
->sigset
= *sigset
;
1247 vcpu
->sigset_active
= 0;
1251 static long kvm_vcpu_ioctl(struct file
*filp
,
1252 unsigned int ioctl
, unsigned long arg
)
1254 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1255 void __user
*argp
= (void __user
*)arg
;
1257 struct kvm_fpu
*fpu
= NULL
;
1258 struct kvm_sregs
*kvm_sregs
= NULL
;
1260 if (vcpu
->kvm
->mm
!= current
->mm
)
1267 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1269 case KVM_GET_REGS
: {
1270 struct kvm_regs
*kvm_regs
;
1273 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1276 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1280 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1287 case KVM_SET_REGS
: {
1288 struct kvm_regs
*kvm_regs
;
1291 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1295 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1297 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1305 case KVM_GET_SREGS
: {
1306 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1310 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1314 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1319 case KVM_SET_SREGS
: {
1320 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1325 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1327 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1333 case KVM_GET_MP_STATE
: {
1334 struct kvm_mp_state mp_state
;
1336 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1340 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1345 case KVM_SET_MP_STATE
: {
1346 struct kvm_mp_state mp_state
;
1349 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1351 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1357 case KVM_TRANSLATE
: {
1358 struct kvm_translation tr
;
1361 if (copy_from_user(&tr
, argp
, sizeof tr
))
1363 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1367 if (copy_to_user(argp
, &tr
, sizeof tr
))
1372 case KVM_SET_GUEST_DEBUG
: {
1373 struct kvm_guest_debug dbg
;
1376 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1378 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1384 case KVM_SET_SIGNAL_MASK
: {
1385 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1386 struct kvm_signal_mask kvm_sigmask
;
1387 sigset_t sigset
, *p
;
1392 if (copy_from_user(&kvm_sigmask
, argp
,
1393 sizeof kvm_sigmask
))
1396 if (kvm_sigmask
.len
!= sizeof sigset
)
1399 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1404 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1408 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1412 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1416 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1422 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1427 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1429 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1436 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1444 static long kvm_vm_ioctl(struct file
*filp
,
1445 unsigned int ioctl
, unsigned long arg
)
1447 struct kvm
*kvm
= filp
->private_data
;
1448 void __user
*argp
= (void __user
*)arg
;
1451 if (kvm
->mm
!= current
->mm
)
1454 case KVM_CREATE_VCPU
:
1455 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1459 case KVM_SET_USER_MEMORY_REGION
: {
1460 struct kvm_userspace_memory_region kvm_userspace_mem
;
1463 if (copy_from_user(&kvm_userspace_mem
, argp
,
1464 sizeof kvm_userspace_mem
))
1467 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1472 case KVM_GET_DIRTY_LOG
: {
1473 struct kvm_dirty_log log
;
1476 if (copy_from_user(&log
, argp
, sizeof log
))
1478 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1483 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1484 case KVM_REGISTER_COALESCED_MMIO
: {
1485 struct kvm_coalesced_mmio_zone zone
;
1487 if (copy_from_user(&zone
, argp
, sizeof zone
))
1490 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1496 case KVM_UNREGISTER_COALESCED_MMIO
: {
1497 struct kvm_coalesced_mmio_zone zone
;
1499 if (copy_from_user(&zone
, argp
, sizeof zone
))
1502 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1510 struct kvm_irqfd data
;
1513 if (copy_from_user(&data
, argp
, sizeof data
))
1515 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
1518 case KVM_IOEVENTFD
: {
1519 struct kvm_ioeventfd data
;
1522 if (copy_from_user(&data
, argp
, sizeof data
))
1524 r
= kvm_ioeventfd(kvm
, &data
);
1527 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1528 case KVM_SET_BOOT_CPU_ID
:
1530 mutex_lock(&kvm
->lock
);
1531 if (atomic_read(&kvm
->online_vcpus
) != 0)
1534 kvm
->bsp_vcpu_id
= arg
;
1535 mutex_unlock(&kvm
->lock
);
1539 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1541 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
1547 #ifdef CONFIG_COMPAT
1548 struct compat_kvm_dirty_log
{
1552 compat_uptr_t dirty_bitmap
; /* one bit per page */
1557 static long kvm_vm_compat_ioctl(struct file
*filp
,
1558 unsigned int ioctl
, unsigned long arg
)
1560 struct kvm
*kvm
= filp
->private_data
;
1563 if (kvm
->mm
!= current
->mm
)
1566 case KVM_GET_DIRTY_LOG
: {
1567 struct compat_kvm_dirty_log compat_log
;
1568 struct kvm_dirty_log log
;
1571 if (copy_from_user(&compat_log
, (void __user
*)arg
,
1572 sizeof(compat_log
)))
1574 log
.slot
= compat_log
.slot
;
1575 log
.padding1
= compat_log
.padding1
;
1576 log
.padding2
= compat_log
.padding2
;
1577 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
1579 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1585 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
1593 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1595 struct page
*page
[1];
1598 gfn_t gfn
= vmf
->pgoff
;
1599 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1601 addr
= gfn_to_hva(kvm
, gfn
);
1602 if (kvm_is_error_hva(addr
))
1603 return VM_FAULT_SIGBUS
;
1605 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1607 if (unlikely(npages
!= 1))
1608 return VM_FAULT_SIGBUS
;
1610 vmf
->page
= page
[0];
1614 static const struct vm_operations_struct kvm_vm_vm_ops
= {
1615 .fault
= kvm_vm_fault
,
1618 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1620 vma
->vm_ops
= &kvm_vm_vm_ops
;
1624 static struct file_operations kvm_vm_fops
= {
1625 .release
= kvm_vm_release
,
1626 .unlocked_ioctl
= kvm_vm_ioctl
,
1627 #ifdef CONFIG_COMPAT
1628 .compat_ioctl
= kvm_vm_compat_ioctl
,
1630 .mmap
= kvm_vm_mmap
,
1633 static int kvm_dev_ioctl_create_vm(void)
1638 kvm
= kvm_create_vm();
1640 return PTR_ERR(kvm
);
1641 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, 0);
1648 static long kvm_dev_ioctl_check_extension_generic(long arg
)
1651 case KVM_CAP_USER_MEMORY
:
1652 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
1653 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
1654 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1655 case KVM_CAP_SET_BOOT_CPU_ID
:
1657 case KVM_CAP_INTERNAL_ERROR_DATA
:
1659 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1660 case KVM_CAP_IRQ_ROUTING
:
1661 return KVM_MAX_IRQ_ROUTES
;
1666 return kvm_dev_ioctl_check_extension(arg
);
1669 static long kvm_dev_ioctl(struct file
*filp
,
1670 unsigned int ioctl
, unsigned long arg
)
1675 case KVM_GET_API_VERSION
:
1679 r
= KVM_API_VERSION
;
1685 r
= kvm_dev_ioctl_create_vm();
1687 case KVM_CHECK_EXTENSION
:
1688 r
= kvm_dev_ioctl_check_extension_generic(arg
);
1690 case KVM_GET_VCPU_MMAP_SIZE
:
1694 r
= PAGE_SIZE
; /* struct kvm_run */
1696 r
+= PAGE_SIZE
; /* pio data page */
1698 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1699 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1702 case KVM_TRACE_ENABLE
:
1703 case KVM_TRACE_PAUSE
:
1704 case KVM_TRACE_DISABLE
:
1708 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1714 static struct file_operations kvm_chardev_ops
= {
1715 .unlocked_ioctl
= kvm_dev_ioctl
,
1716 .compat_ioctl
= kvm_dev_ioctl
,
1719 static struct miscdevice kvm_dev
= {
1725 static void hardware_enable(void *junk
)
1727 int cpu
= raw_smp_processor_id();
1730 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1733 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
1735 r
= kvm_arch_hardware_enable(NULL
);
1738 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1739 atomic_inc(&hardware_enable_failed
);
1740 printk(KERN_INFO
"kvm: enabling virtualization on "
1741 "CPU%d failed\n", cpu
);
1745 static void hardware_disable(void *junk
)
1747 int cpu
= raw_smp_processor_id();
1749 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1751 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1752 kvm_arch_hardware_disable(NULL
);
1755 static void hardware_disable_all_nolock(void)
1757 BUG_ON(!kvm_usage_count
);
1760 if (!kvm_usage_count
)
1761 on_each_cpu(hardware_disable
, NULL
, 1);
1764 static void hardware_disable_all(void)
1766 spin_lock(&kvm_lock
);
1767 hardware_disable_all_nolock();
1768 spin_unlock(&kvm_lock
);
1771 static int hardware_enable_all(void)
1775 spin_lock(&kvm_lock
);
1778 if (kvm_usage_count
== 1) {
1779 atomic_set(&hardware_enable_failed
, 0);
1780 on_each_cpu(hardware_enable
, NULL
, 1);
1782 if (atomic_read(&hardware_enable_failed
)) {
1783 hardware_disable_all_nolock();
1788 spin_unlock(&kvm_lock
);
1793 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1798 if (!kvm_usage_count
)
1801 val
&= ~CPU_TASKS_FROZEN
;
1804 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1806 hardware_disable(NULL
);
1808 case CPU_UP_CANCELED
:
1809 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1811 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
1814 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1816 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
1823 asmlinkage
void kvm_handle_fault_on_reboot(void)
1826 /* spin while reset goes on */
1829 /* Fault while not rebooting. We want the trace. */
1832 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
1834 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1838 * Some (well, at least mine) BIOSes hang on reboot if
1841 * And Intel TXT required VMX off for all cpu when system shutdown.
1843 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1844 kvm_rebooting
= true;
1845 on_each_cpu(hardware_disable
, NULL
, 1);
1849 static struct notifier_block kvm_reboot_notifier
= {
1850 .notifier_call
= kvm_reboot
,
1854 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
1856 memset(bus
, 0, sizeof(*bus
));
1859 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
1863 for (i
= 0; i
< bus
->dev_count
; i
++) {
1864 struct kvm_io_device
*pos
= bus
->devs
[i
];
1866 kvm_iodevice_destructor(pos
);
1870 /* kvm_io_bus_write - called under kvm->slots_lock */
1871 int kvm_io_bus_write(struct kvm_io_bus
*bus
, gpa_t addr
,
1872 int len
, const void *val
)
1875 for (i
= 0; i
< bus
->dev_count
; i
++)
1876 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
1881 /* kvm_io_bus_read - called under kvm->slots_lock */
1882 int kvm_io_bus_read(struct kvm_io_bus
*bus
, gpa_t addr
, int len
, void *val
)
1885 for (i
= 0; i
< bus
->dev_count
; i
++)
1886 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
1891 int kvm_io_bus_register_dev(struct kvm
*kvm
, struct kvm_io_bus
*bus
,
1892 struct kvm_io_device
*dev
)
1896 down_write(&kvm
->slots_lock
);
1897 ret
= __kvm_io_bus_register_dev(bus
, dev
);
1898 up_write(&kvm
->slots_lock
);
1903 /* An unlocked version. Caller must have write lock on slots_lock. */
1904 int __kvm_io_bus_register_dev(struct kvm_io_bus
*bus
,
1905 struct kvm_io_device
*dev
)
1907 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
1910 bus
->devs
[bus
->dev_count
++] = dev
;
1915 void kvm_io_bus_unregister_dev(struct kvm
*kvm
,
1916 struct kvm_io_bus
*bus
,
1917 struct kvm_io_device
*dev
)
1919 down_write(&kvm
->slots_lock
);
1920 __kvm_io_bus_unregister_dev(bus
, dev
);
1921 up_write(&kvm
->slots_lock
);
1924 /* An unlocked version. Caller must have write lock on slots_lock. */
1925 void __kvm_io_bus_unregister_dev(struct kvm_io_bus
*bus
,
1926 struct kvm_io_device
*dev
)
1930 for (i
= 0; i
< bus
->dev_count
; i
++)
1931 if (bus
->devs
[i
] == dev
) {
1932 bus
->devs
[i
] = bus
->devs
[--bus
->dev_count
];
1937 static struct notifier_block kvm_cpu_notifier
= {
1938 .notifier_call
= kvm_cpu_hotplug
,
1939 .priority
= 20, /* must be > scheduler priority */
1942 static int vm_stat_get(void *_offset
, u64
*val
)
1944 unsigned offset
= (long)_offset
;
1948 spin_lock(&kvm_lock
);
1949 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1950 *val
+= *(u32
*)((void *)kvm
+ offset
);
1951 spin_unlock(&kvm_lock
);
1955 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
1957 static int vcpu_stat_get(void *_offset
, u64
*val
)
1959 unsigned offset
= (long)_offset
;
1961 struct kvm_vcpu
*vcpu
;
1965 spin_lock(&kvm_lock
);
1966 list_for_each_entry(kvm
, &vm_list
, vm_list
)
1967 kvm_for_each_vcpu(i
, vcpu
, kvm
)
1968 *val
+= *(u32
*)((void *)vcpu
+ offset
);
1970 spin_unlock(&kvm_lock
);
1974 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
1976 static const struct file_operations
*stat_fops
[] = {
1977 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
1978 [KVM_STAT_VM
] = &vm_stat_fops
,
1981 static void kvm_init_debug(void)
1983 struct kvm_stats_debugfs_item
*p
;
1985 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
1986 for (p
= debugfs_entries
; p
->name
; ++p
)
1987 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
1988 (void *)(long)p
->offset
,
1989 stat_fops
[p
->kind
]);
1992 static void kvm_exit_debug(void)
1994 struct kvm_stats_debugfs_item
*p
;
1996 for (p
= debugfs_entries
; p
->name
; ++p
)
1997 debugfs_remove(p
->dentry
);
1998 debugfs_remove(kvm_debugfs_dir
);
2001 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2003 if (kvm_usage_count
)
2004 hardware_disable(NULL
);
2008 static int kvm_resume(struct sys_device
*dev
)
2010 if (kvm_usage_count
)
2011 hardware_enable(NULL
);
2015 static struct sysdev_class kvm_sysdev_class
= {
2017 .suspend
= kvm_suspend
,
2018 .resume
= kvm_resume
,
2021 static struct sys_device kvm_sysdev
= {
2023 .cls
= &kvm_sysdev_class
,
2026 struct page
*bad_page
;
2030 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2032 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2035 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2037 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2039 kvm_arch_vcpu_load(vcpu
, cpu
);
2042 static void kvm_sched_out(struct preempt_notifier
*pn
,
2043 struct task_struct
*next
)
2045 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2047 kvm_arch_vcpu_put(vcpu
);
2050 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2051 struct module
*module
)
2056 r
= kvm_arch_init(opaque
);
2060 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2062 if (bad_page
== NULL
) {
2067 bad_pfn
= page_to_pfn(bad_page
);
2069 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2074 r
= kvm_arch_hardware_setup();
2078 for_each_online_cpu(cpu
) {
2079 smp_call_function_single(cpu
,
2080 kvm_arch_check_processor_compat
,
2086 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2089 register_reboot_notifier(&kvm_reboot_notifier
);
2091 r
= sysdev_class_register(&kvm_sysdev_class
);
2095 r
= sysdev_register(&kvm_sysdev
);
2099 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2100 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2101 __alignof__(struct kvm_vcpu
),
2103 if (!kvm_vcpu_cache
) {
2108 kvm_chardev_ops
.owner
= module
;
2109 kvm_vm_fops
.owner
= module
;
2110 kvm_vcpu_fops
.owner
= module
;
2112 r
= misc_register(&kvm_dev
);
2114 printk(KERN_ERR
"kvm: misc device register failed\n");
2118 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2119 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2126 kmem_cache_destroy(kvm_vcpu_cache
);
2128 sysdev_unregister(&kvm_sysdev
);
2130 sysdev_class_unregister(&kvm_sysdev_class
);
2132 unregister_reboot_notifier(&kvm_reboot_notifier
);
2133 unregister_cpu_notifier(&kvm_cpu_notifier
);
2136 kvm_arch_hardware_unsetup();
2138 free_cpumask_var(cpus_hardware_enabled
);
2140 __free_page(bad_page
);
2146 EXPORT_SYMBOL_GPL(kvm_init
);
2150 tracepoint_synchronize_unregister();
2152 misc_deregister(&kvm_dev
);
2153 kmem_cache_destroy(kvm_vcpu_cache
);
2154 sysdev_unregister(&kvm_sysdev
);
2155 sysdev_class_unregister(&kvm_sysdev_class
);
2156 unregister_reboot_notifier(&kvm_reboot_notifier
);
2157 unregister_cpu_notifier(&kvm_cpu_notifier
);
2158 on_each_cpu(hardware_disable
, NULL
, 1);
2159 kvm_arch_hardware_unsetup();
2161 free_cpumask_var(cpus_hardware_enabled
);
2162 __free_page(bad_page
);
2164 EXPORT_SYMBOL_GPL(kvm_exit
);