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.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
21 #include <linux/kvm_host.h>
22 #include <linux/kvm.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.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>
47 #include <linux/srcu.h>
48 #include <linux/hugetlb.h>
49 #include <linux/slab.h>
51 #include <asm/processor.h>
53 #include <asm/uaccess.h>
54 #include <asm/pgtable.h>
55 #include <asm-generic/bitops/le.h>
57 #include "coalesced_mmio.h"
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/kvm.h>
63 MODULE_AUTHOR("Qumranet");
64 MODULE_LICENSE("GPL");
69 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
72 DEFINE_SPINLOCK(kvm_lock
);
75 static cpumask_var_t cpus_hardware_enabled
;
76 static int kvm_usage_count
= 0;
77 static atomic_t hardware_enable_failed
;
79 struct kmem_cache
*kvm_vcpu_cache
;
80 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
82 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
84 struct dentry
*kvm_debugfs_dir
;
86 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
88 static int hardware_enable_all(void);
89 static void hardware_disable_all(void);
91 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
);
94 EXPORT_SYMBOL_GPL(kvm_rebooting
);
96 static bool largepages_enabled
= true;
98 static struct page
*hwpoison_page
;
99 static pfn_t hwpoison_pfn
;
101 static struct page
*fault_page
;
102 static pfn_t fault_pfn
;
104 inline int kvm_is_mmio_pfn(pfn_t pfn
)
106 if (pfn_valid(pfn
)) {
108 struct page
*tail
= pfn_to_page(pfn
);
109 struct page
*head
= compound_trans_head(tail
);
110 reserved
= PageReserved(head
);
113 * "head" is not a dangling pointer
114 * (compound_trans_head takes care of that)
115 * but the hugepage may have been splitted
116 * from under us (and we may not hold a
117 * reference count on the head page so it can
118 * be reused before we run PageReferenced), so
119 * we've to check PageTail before returning
126 return PageReserved(tail
);
133 * Switches to specified vcpu, until a matching vcpu_put()
135 void vcpu_load(struct kvm_vcpu
*vcpu
)
139 mutex_lock(&vcpu
->mutex
);
141 preempt_notifier_register(&vcpu
->preempt_notifier
);
142 kvm_arch_vcpu_load(vcpu
, cpu
);
146 void vcpu_put(struct kvm_vcpu
*vcpu
)
149 kvm_arch_vcpu_put(vcpu
);
150 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
152 mutex_unlock(&vcpu
->mutex
);
155 static void ack_flush(void *_completed
)
159 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
164 struct kvm_vcpu
*vcpu
;
166 zalloc_cpumask_var(&cpus
, GFP_ATOMIC
);
168 raw_spin_lock(&kvm
->requests_lock
);
169 me
= smp_processor_id();
170 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
171 if (kvm_make_check_request(req
, vcpu
))
174 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
175 cpumask_set_cpu(cpu
, cpus
);
177 if (unlikely(cpus
== NULL
))
178 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
179 else if (!cpumask_empty(cpus
))
180 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
183 raw_spin_unlock(&kvm
->requests_lock
);
184 free_cpumask_var(cpus
);
188 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
190 int dirty_count
= kvm
->tlbs_dirty
;
193 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
194 ++kvm
->stat
.remote_tlb_flush
;
195 cmpxchg(&kvm
->tlbs_dirty
, dirty_count
, 0);
198 void kvm_reload_remote_mmus(struct kvm
*kvm
)
200 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
203 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
208 mutex_init(&vcpu
->mutex
);
212 init_waitqueue_head(&vcpu
->wq
);
213 kvm_async_pf_vcpu_init(vcpu
);
215 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
220 vcpu
->run
= page_address(page
);
222 r
= kvm_arch_vcpu_init(vcpu
);
228 free_page((unsigned long)vcpu
->run
);
232 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
234 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
236 kvm_arch_vcpu_uninit(vcpu
);
237 free_page((unsigned long)vcpu
->run
);
239 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
241 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
242 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
244 return container_of(mn
, struct kvm
, mmu_notifier
);
247 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
248 struct mm_struct
*mm
,
249 unsigned long address
)
251 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
252 int need_tlb_flush
, idx
;
255 * When ->invalidate_page runs, the linux pte has been zapped
256 * already but the page is still allocated until
257 * ->invalidate_page returns. So if we increase the sequence
258 * here the kvm page fault will notice if the spte can't be
259 * established because the page is going to be freed. If
260 * instead the kvm page fault establishes the spte before
261 * ->invalidate_page runs, kvm_unmap_hva will release it
264 * The sequence increase only need to be seen at spin_unlock
265 * time, and not at spin_lock time.
267 * Increasing the sequence after the spin_unlock would be
268 * unsafe because the kvm page fault could then establish the
269 * pte after kvm_unmap_hva returned, without noticing the page
270 * is going to be freed.
272 idx
= srcu_read_lock(&kvm
->srcu
);
273 spin_lock(&kvm
->mmu_lock
);
274 kvm
->mmu_notifier_seq
++;
275 need_tlb_flush
= kvm_unmap_hva(kvm
, address
) | kvm
->tlbs_dirty
;
276 spin_unlock(&kvm
->mmu_lock
);
277 srcu_read_unlock(&kvm
->srcu
, idx
);
279 /* we've to flush the tlb before the pages can be freed */
281 kvm_flush_remote_tlbs(kvm
);
285 static void kvm_mmu_notifier_change_pte(struct mmu_notifier
*mn
,
286 struct mm_struct
*mm
,
287 unsigned long address
,
290 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
293 idx
= srcu_read_lock(&kvm
->srcu
);
294 spin_lock(&kvm
->mmu_lock
);
295 kvm
->mmu_notifier_seq
++;
296 kvm_set_spte_hva(kvm
, address
, pte
);
297 spin_unlock(&kvm
->mmu_lock
);
298 srcu_read_unlock(&kvm
->srcu
, idx
);
301 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
302 struct mm_struct
*mm
,
306 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
307 int need_tlb_flush
= 0, idx
;
309 idx
= srcu_read_lock(&kvm
->srcu
);
310 spin_lock(&kvm
->mmu_lock
);
312 * The count increase must become visible at unlock time as no
313 * spte can be established without taking the mmu_lock and
314 * count is also read inside the mmu_lock critical section.
316 kvm
->mmu_notifier_count
++;
317 for (; start
< end
; start
+= PAGE_SIZE
)
318 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
319 need_tlb_flush
|= kvm
->tlbs_dirty
;
320 spin_unlock(&kvm
->mmu_lock
);
321 srcu_read_unlock(&kvm
->srcu
, idx
);
323 /* we've to flush the tlb before the pages can be freed */
325 kvm_flush_remote_tlbs(kvm
);
328 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
329 struct mm_struct
*mm
,
333 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
335 spin_lock(&kvm
->mmu_lock
);
337 * This sequence increase will notify the kvm page fault that
338 * the page that is going to be mapped in the spte could have
341 kvm
->mmu_notifier_seq
++;
343 * The above sequence increase must be visible before the
344 * below count decrease but both values are read by the kvm
345 * page fault under mmu_lock spinlock so we don't need to add
346 * a smb_wmb() here in between the two.
348 kvm
->mmu_notifier_count
--;
349 spin_unlock(&kvm
->mmu_lock
);
351 BUG_ON(kvm
->mmu_notifier_count
< 0);
354 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
355 struct mm_struct
*mm
,
356 unsigned long address
)
358 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
361 idx
= srcu_read_lock(&kvm
->srcu
);
362 spin_lock(&kvm
->mmu_lock
);
363 young
= kvm_age_hva(kvm
, address
);
364 spin_unlock(&kvm
->mmu_lock
);
365 srcu_read_unlock(&kvm
->srcu
, idx
);
368 kvm_flush_remote_tlbs(kvm
);
373 static int kvm_mmu_notifier_test_young(struct mmu_notifier
*mn
,
374 struct mm_struct
*mm
,
375 unsigned long address
)
377 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
380 idx
= srcu_read_lock(&kvm
->srcu
);
381 spin_lock(&kvm
->mmu_lock
);
382 young
= kvm_test_age_hva(kvm
, address
);
383 spin_unlock(&kvm
->mmu_lock
);
384 srcu_read_unlock(&kvm
->srcu
, idx
);
389 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
390 struct mm_struct
*mm
)
392 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
395 idx
= srcu_read_lock(&kvm
->srcu
);
396 kvm_arch_flush_shadow(kvm
);
397 srcu_read_unlock(&kvm
->srcu
, idx
);
400 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
401 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
402 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
403 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
404 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
405 .test_young
= kvm_mmu_notifier_test_young
,
406 .change_pte
= kvm_mmu_notifier_change_pte
,
407 .release
= kvm_mmu_notifier_release
,
410 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
412 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
413 return mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
416 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
418 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
423 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
425 static struct kvm
*kvm_create_vm(void)
428 struct kvm
*kvm
= kvm_arch_alloc_vm();
431 return ERR_PTR(-ENOMEM
);
433 r
= kvm_arch_init_vm(kvm
);
435 goto out_err_nodisable
;
437 r
= hardware_enable_all();
439 goto out_err_nodisable
;
441 #ifdef CONFIG_HAVE_KVM_IRQCHIP
442 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
443 INIT_HLIST_HEAD(&kvm
->irq_ack_notifier_list
);
447 kvm
->memslots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
450 if (init_srcu_struct(&kvm
->srcu
))
452 for (i
= 0; i
< KVM_NR_BUSES
; i
++) {
453 kvm
->buses
[i
] = kzalloc(sizeof(struct kvm_io_bus
),
459 r
= kvm_init_mmu_notifier(kvm
);
463 kvm
->mm
= current
->mm
;
464 atomic_inc(&kvm
->mm
->mm_count
);
465 spin_lock_init(&kvm
->mmu_lock
);
466 raw_spin_lock_init(&kvm
->requests_lock
);
467 kvm_eventfd_init(kvm
);
468 mutex_init(&kvm
->lock
);
469 mutex_init(&kvm
->irq_lock
);
470 mutex_init(&kvm
->slots_lock
);
471 atomic_set(&kvm
->users_count
, 1);
472 spin_lock(&kvm_lock
);
473 list_add(&kvm
->vm_list
, &vm_list
);
474 spin_unlock(&kvm_lock
);
479 cleanup_srcu_struct(&kvm
->srcu
);
481 hardware_disable_all();
483 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
484 kfree(kvm
->buses
[i
]);
485 kfree(kvm
->memslots
);
486 kvm_arch_free_vm(kvm
);
490 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot
*memslot
)
492 if (!memslot
->dirty_bitmap
)
495 if (2 * kvm_dirty_bitmap_bytes(memslot
) > PAGE_SIZE
)
496 vfree(memslot
->dirty_bitmap_head
);
498 kfree(memslot
->dirty_bitmap_head
);
500 memslot
->dirty_bitmap
= NULL
;
501 memslot
->dirty_bitmap_head
= NULL
;
505 * Free any memory in @free but not in @dont.
507 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
508 struct kvm_memory_slot
*dont
)
512 if (!dont
|| free
->rmap
!= dont
->rmap
)
515 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
516 kvm_destroy_dirty_bitmap(free
);
519 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
520 if (!dont
|| free
->lpage_info
[i
] != dont
->lpage_info
[i
]) {
521 vfree(free
->lpage_info
[i
]);
522 free
->lpage_info
[i
] = NULL
;
530 void kvm_free_physmem(struct kvm
*kvm
)
533 struct kvm_memslots
*slots
= kvm
->memslots
;
535 for (i
= 0; i
< slots
->nmemslots
; ++i
)
536 kvm_free_physmem_slot(&slots
->memslots
[i
], NULL
);
538 kfree(kvm
->memslots
);
541 static void kvm_destroy_vm(struct kvm
*kvm
)
544 struct mm_struct
*mm
= kvm
->mm
;
546 kvm_arch_sync_events(kvm
);
547 spin_lock(&kvm_lock
);
548 list_del(&kvm
->vm_list
);
549 spin_unlock(&kvm_lock
);
550 kvm_free_irq_routing(kvm
);
551 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
552 kvm_io_bus_destroy(kvm
->buses
[i
]);
553 kvm_coalesced_mmio_free(kvm
);
554 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
555 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
557 kvm_arch_flush_shadow(kvm
);
559 kvm_arch_destroy_vm(kvm
);
560 kvm_free_physmem(kvm
);
561 cleanup_srcu_struct(&kvm
->srcu
);
562 kvm_arch_free_vm(kvm
);
563 hardware_disable_all();
567 void kvm_get_kvm(struct kvm
*kvm
)
569 atomic_inc(&kvm
->users_count
);
571 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
573 void kvm_put_kvm(struct kvm
*kvm
)
575 if (atomic_dec_and_test(&kvm
->users_count
))
578 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
581 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
583 struct kvm
*kvm
= filp
->private_data
;
585 kvm_irqfd_release(kvm
);
592 * Allocation size is twice as large as the actual dirty bitmap size.
593 * This makes it possible to do double buffering: see x86's
594 * kvm_vm_ioctl_get_dirty_log().
596 static int kvm_create_dirty_bitmap(struct kvm_memory_slot
*memslot
)
598 unsigned long dirty_bytes
= 2 * kvm_dirty_bitmap_bytes(memslot
);
600 if (dirty_bytes
> PAGE_SIZE
)
601 memslot
->dirty_bitmap
= vzalloc(dirty_bytes
);
603 memslot
->dirty_bitmap
= kzalloc(dirty_bytes
, GFP_KERNEL
);
605 if (!memslot
->dirty_bitmap
)
608 memslot
->dirty_bitmap_head
= memslot
->dirty_bitmap
;
613 * Allocate some memory and give it an address in the guest physical address
616 * Discontiguous memory is allowed, mostly for framebuffers.
618 * Must be called holding mmap_sem for write.
620 int __kvm_set_memory_region(struct kvm
*kvm
,
621 struct kvm_userspace_memory_region
*mem
,
624 int r
, flush_shadow
= 0;
626 unsigned long npages
;
628 struct kvm_memory_slot
*memslot
;
629 struct kvm_memory_slot old
, new;
630 struct kvm_memslots
*slots
, *old_memslots
;
633 /* General sanity checks */
634 if (mem
->memory_size
& (PAGE_SIZE
- 1))
636 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
638 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
640 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
642 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
645 memslot
= &kvm
->memslots
->memslots
[mem
->slot
];
646 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
647 npages
= mem
->memory_size
>> PAGE_SHIFT
;
650 if (npages
> KVM_MEM_MAX_NR_PAGES
)
654 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
656 new = old
= *memslot
;
659 new.base_gfn
= base_gfn
;
661 new.flags
= mem
->flags
;
663 /* Disallow changing a memory slot's size. */
665 if (npages
&& old
.npages
&& npages
!= old
.npages
)
668 /* Check for overlaps */
670 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
671 struct kvm_memory_slot
*s
= &kvm
->memslots
->memslots
[i
];
673 if (s
== memslot
|| !s
->npages
)
675 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
676 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
680 /* Free page dirty bitmap if unneeded */
681 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
682 new.dirty_bitmap
= NULL
;
686 /* Allocate if a slot is being created */
688 if (npages
&& !new.rmap
) {
689 new.rmap
= vzalloc(npages
* sizeof(*new.rmap
));
694 new.user_alloc
= user_alloc
;
695 new.userspace_addr
= mem
->userspace_addr
;
700 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
706 /* Avoid unused variable warning if no large pages */
709 if (new.lpage_info
[i
])
712 lpages
= 1 + ((base_gfn
+ npages
- 1)
713 >> KVM_HPAGE_GFN_SHIFT(level
));
714 lpages
-= base_gfn
>> KVM_HPAGE_GFN_SHIFT(level
);
716 new.lpage_info
[i
] = vzalloc(lpages
* sizeof(*new.lpage_info
[i
]));
718 if (!new.lpage_info
[i
])
721 if (base_gfn
& (KVM_PAGES_PER_HPAGE(level
) - 1))
722 new.lpage_info
[i
][0].write_count
= 1;
723 if ((base_gfn
+npages
) & (KVM_PAGES_PER_HPAGE(level
) - 1))
724 new.lpage_info
[i
][lpages
- 1].write_count
= 1;
725 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
727 * If the gfn and userspace address are not aligned wrt each
728 * other, or if explicitly asked to, disable large page
729 * support for this slot
731 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE(level
) - 1) ||
733 for (j
= 0; j
< lpages
; ++j
)
734 new.lpage_info
[i
][j
].write_count
= 1;
739 /* Allocate page dirty bitmap if needed */
740 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
741 if (kvm_create_dirty_bitmap(&new) < 0)
743 /* destroy any largepage mappings for dirty tracking */
747 #else /* not defined CONFIG_S390 */
748 new.user_alloc
= user_alloc
;
750 new.userspace_addr
= mem
->userspace_addr
;
751 #endif /* not defined CONFIG_S390 */
755 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
758 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
759 if (mem
->slot
>= slots
->nmemslots
)
760 slots
->nmemslots
= mem
->slot
+ 1;
762 slots
->memslots
[mem
->slot
].flags
|= KVM_MEMSLOT_INVALID
;
764 old_memslots
= kvm
->memslots
;
765 rcu_assign_pointer(kvm
->memslots
, slots
);
766 synchronize_srcu_expedited(&kvm
->srcu
);
767 /* From this point no new shadow pages pointing to a deleted
768 * memslot will be created.
770 * validation of sp->gfn happens in:
771 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
772 * - kvm_is_visible_gfn (mmu_check_roots)
774 kvm_arch_flush_shadow(kvm
);
778 r
= kvm_arch_prepare_memory_region(kvm
, &new, old
, mem
, user_alloc
);
782 /* map the pages in iommu page table */
784 r
= kvm_iommu_map_pages(kvm
, &new);
790 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
793 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
794 if (mem
->slot
>= slots
->nmemslots
)
795 slots
->nmemslots
= mem
->slot
+ 1;
798 /* actual memory is freed via old in kvm_free_physmem_slot below */
801 new.dirty_bitmap
= NULL
;
802 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
)
803 new.lpage_info
[i
] = NULL
;
806 slots
->memslots
[mem
->slot
] = new;
807 old_memslots
= kvm
->memslots
;
808 rcu_assign_pointer(kvm
->memslots
, slots
);
809 synchronize_srcu_expedited(&kvm
->srcu
);
811 kvm_arch_commit_memory_region(kvm
, mem
, old
, user_alloc
);
813 kvm_free_physmem_slot(&old
, &new);
817 kvm_arch_flush_shadow(kvm
);
822 kvm_free_physmem_slot(&new, &old
);
827 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
829 int kvm_set_memory_region(struct kvm
*kvm
,
830 struct kvm_userspace_memory_region
*mem
,
835 mutex_lock(&kvm
->slots_lock
);
836 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
837 mutex_unlock(&kvm
->slots_lock
);
840 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
842 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
844 kvm_userspace_memory_region
*mem
,
847 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
849 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
852 int kvm_get_dirty_log(struct kvm
*kvm
,
853 struct kvm_dirty_log
*log
, int *is_dirty
)
855 struct kvm_memory_slot
*memslot
;
858 unsigned long any
= 0;
861 if (log
->slot
>= KVM_MEMORY_SLOTS
)
864 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
866 if (!memslot
->dirty_bitmap
)
869 n
= kvm_dirty_bitmap_bytes(memslot
);
871 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
872 any
= memslot
->dirty_bitmap
[i
];
875 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
886 void kvm_disable_largepages(void)
888 largepages_enabled
= false;
890 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
892 int is_error_page(struct page
*page
)
894 return page
== bad_page
|| page
== hwpoison_page
|| page
== fault_page
;
896 EXPORT_SYMBOL_GPL(is_error_page
);
898 int is_error_pfn(pfn_t pfn
)
900 return pfn
== bad_pfn
|| pfn
== hwpoison_pfn
|| pfn
== fault_pfn
;
902 EXPORT_SYMBOL_GPL(is_error_pfn
);
904 int is_hwpoison_pfn(pfn_t pfn
)
906 return pfn
== hwpoison_pfn
;
908 EXPORT_SYMBOL_GPL(is_hwpoison_pfn
);
910 int is_fault_pfn(pfn_t pfn
)
912 return pfn
== fault_pfn
;
914 EXPORT_SYMBOL_GPL(is_fault_pfn
);
916 static inline unsigned long bad_hva(void)
921 int kvm_is_error_hva(unsigned long addr
)
923 return addr
== bad_hva();
925 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
927 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm_memslots
*slots
,
932 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
933 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
935 if (gfn
>= memslot
->base_gfn
936 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
942 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
944 return __gfn_to_memslot(kvm_memslots(kvm
), gfn
);
946 EXPORT_SYMBOL_GPL(gfn_to_memslot
);
948 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
951 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
953 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
954 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
956 if (memslot
->flags
& KVM_MEMSLOT_INVALID
)
959 if (gfn
>= memslot
->base_gfn
960 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
965 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
967 unsigned long kvm_host_page_size(struct kvm
*kvm
, gfn_t gfn
)
969 struct vm_area_struct
*vma
;
970 unsigned long addr
, size
;
974 addr
= gfn_to_hva(kvm
, gfn
);
975 if (kvm_is_error_hva(addr
))
978 down_read(¤t
->mm
->mmap_sem
);
979 vma
= find_vma(current
->mm
, addr
);
983 size
= vma_kernel_pagesize(vma
);
986 up_read(¤t
->mm
->mmap_sem
);
991 int memslot_id(struct kvm
*kvm
, gfn_t gfn
)
994 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
995 struct kvm_memory_slot
*memslot
= NULL
;
997 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
998 memslot
= &slots
->memslots
[i
];
1000 if (gfn
>= memslot
->base_gfn
1001 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1005 return memslot
- slots
->memslots
;
1008 static unsigned long gfn_to_hva_many(struct kvm_memory_slot
*slot
, gfn_t gfn
,
1011 if (!slot
|| slot
->flags
& KVM_MEMSLOT_INVALID
)
1015 *nr_pages
= slot
->npages
- (gfn
- slot
->base_gfn
);
1017 return gfn_to_hva_memslot(slot
, gfn
);
1020 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1022 return gfn_to_hva_many(gfn_to_memslot(kvm
, gfn
), gfn
, NULL
);
1024 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1026 static pfn_t
get_fault_pfn(void)
1028 get_page(fault_page
);
1032 static pfn_t
hva_to_pfn(struct kvm
*kvm
, unsigned long addr
, bool atomic
,
1033 bool *async
, bool write_fault
, bool *writable
)
1035 struct page
*page
[1];
1039 /* we can do it either atomically or asynchronously, not both */
1040 BUG_ON(atomic
&& async
);
1042 BUG_ON(!write_fault
&& !writable
);
1047 if (atomic
|| async
)
1048 npages
= __get_user_pages_fast(addr
, 1, 1, page
);
1050 if (unlikely(npages
!= 1) && !atomic
) {
1054 *writable
= write_fault
;
1056 npages
= get_user_pages_fast(addr
, 1, write_fault
, page
);
1058 /* map read fault as writable if possible */
1059 if (unlikely(!write_fault
) && npages
== 1) {
1060 struct page
*wpage
[1];
1062 npages
= __get_user_pages_fast(addr
, 1, 1, wpage
);
1072 if (unlikely(npages
!= 1)) {
1073 struct vm_area_struct
*vma
;
1076 return get_fault_pfn();
1078 down_read(¤t
->mm
->mmap_sem
);
1079 if (is_hwpoison_address(addr
)) {
1080 up_read(¤t
->mm
->mmap_sem
);
1081 get_page(hwpoison_page
);
1082 return page_to_pfn(hwpoison_page
);
1085 vma
= find_vma_intersection(current
->mm
, addr
, addr
+1);
1088 pfn
= get_fault_pfn();
1089 else if ((vma
->vm_flags
& VM_PFNMAP
)) {
1090 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) +
1092 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1094 if (async
&& (vma
->vm_flags
& VM_WRITE
))
1096 pfn
= get_fault_pfn();
1098 up_read(¤t
->mm
->mmap_sem
);
1100 pfn
= page_to_pfn(page
[0]);
1105 pfn_t
hva_to_pfn_atomic(struct kvm
*kvm
, unsigned long addr
)
1107 return hva_to_pfn(kvm
, addr
, true, NULL
, true, NULL
);
1109 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic
);
1111 static pfn_t
__gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
, bool atomic
, bool *async
,
1112 bool write_fault
, bool *writable
)
1119 addr
= gfn_to_hva(kvm
, gfn
);
1120 if (kvm_is_error_hva(addr
)) {
1122 return page_to_pfn(bad_page
);
1125 return hva_to_pfn(kvm
, addr
, atomic
, async
, write_fault
, writable
);
1128 pfn_t
gfn_to_pfn_atomic(struct kvm
*kvm
, gfn_t gfn
)
1130 return __gfn_to_pfn(kvm
, gfn
, true, NULL
, true, NULL
);
1132 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic
);
1134 pfn_t
gfn_to_pfn_async(struct kvm
*kvm
, gfn_t gfn
, bool *async
,
1135 bool write_fault
, bool *writable
)
1137 return __gfn_to_pfn(kvm
, gfn
, false, async
, write_fault
, writable
);
1139 EXPORT_SYMBOL_GPL(gfn_to_pfn_async
);
1141 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1143 return __gfn_to_pfn(kvm
, gfn
, false, NULL
, true, NULL
);
1145 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1147 pfn_t
gfn_to_pfn_prot(struct kvm
*kvm
, gfn_t gfn
, bool write_fault
,
1150 return __gfn_to_pfn(kvm
, gfn
, false, NULL
, write_fault
, writable
);
1152 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot
);
1154 pfn_t
gfn_to_pfn_memslot(struct kvm
*kvm
,
1155 struct kvm_memory_slot
*slot
, gfn_t gfn
)
1157 unsigned long addr
= gfn_to_hva_memslot(slot
, gfn
);
1158 return hva_to_pfn(kvm
, addr
, false, NULL
, true, NULL
);
1161 int gfn_to_page_many_atomic(struct kvm
*kvm
, gfn_t gfn
, struct page
**pages
,
1167 addr
= gfn_to_hva_many(gfn_to_memslot(kvm
, gfn
), gfn
, &entry
);
1168 if (kvm_is_error_hva(addr
))
1171 if (entry
< nr_pages
)
1174 return __get_user_pages_fast(addr
, nr_pages
, 1, pages
);
1176 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic
);
1178 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1182 pfn
= gfn_to_pfn(kvm
, gfn
);
1183 if (!kvm_is_mmio_pfn(pfn
))
1184 return pfn_to_page(pfn
);
1186 WARN_ON(kvm_is_mmio_pfn(pfn
));
1192 EXPORT_SYMBOL_GPL(gfn_to_page
);
1194 void kvm_release_page_clean(struct page
*page
)
1196 kvm_release_pfn_clean(page_to_pfn(page
));
1198 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1200 void kvm_release_pfn_clean(pfn_t pfn
)
1202 if (!kvm_is_mmio_pfn(pfn
))
1203 put_page(pfn_to_page(pfn
));
1205 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1207 void kvm_release_page_dirty(struct page
*page
)
1209 kvm_release_pfn_dirty(page_to_pfn(page
));
1211 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1213 void kvm_release_pfn_dirty(pfn_t pfn
)
1215 kvm_set_pfn_dirty(pfn
);
1216 kvm_release_pfn_clean(pfn
);
1218 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1220 void kvm_set_page_dirty(struct page
*page
)
1222 kvm_set_pfn_dirty(page_to_pfn(page
));
1224 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1226 void kvm_set_pfn_dirty(pfn_t pfn
)
1228 if (!kvm_is_mmio_pfn(pfn
)) {
1229 struct page
*page
= pfn_to_page(pfn
);
1230 if (!PageReserved(page
))
1234 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1236 void kvm_set_pfn_accessed(pfn_t pfn
)
1238 if (!kvm_is_mmio_pfn(pfn
))
1239 mark_page_accessed(pfn_to_page(pfn
));
1241 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1243 void kvm_get_pfn(pfn_t pfn
)
1245 if (!kvm_is_mmio_pfn(pfn
))
1246 get_page(pfn_to_page(pfn
));
1248 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1250 static int next_segment(unsigned long len
, int offset
)
1252 if (len
> PAGE_SIZE
- offset
)
1253 return PAGE_SIZE
- offset
;
1258 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1264 addr
= gfn_to_hva(kvm
, gfn
);
1265 if (kvm_is_error_hva(addr
))
1267 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1272 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1274 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1276 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1278 int offset
= offset_in_page(gpa
);
1281 while ((seg
= next_segment(len
, offset
)) != 0) {
1282 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1292 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1294 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1299 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1300 int offset
= offset_in_page(gpa
);
1302 addr
= gfn_to_hva(kvm
, gfn
);
1303 if (kvm_is_error_hva(addr
))
1305 pagefault_disable();
1306 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1312 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1314 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1315 int offset
, int len
)
1320 addr
= gfn_to_hva(kvm
, gfn
);
1321 if (kvm_is_error_hva(addr
))
1323 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1326 mark_page_dirty(kvm
, gfn
);
1329 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1331 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1334 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1336 int offset
= offset_in_page(gpa
);
1339 while ((seg
= next_segment(len
, offset
)) != 0) {
1340 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1351 int kvm_gfn_to_hva_cache_init(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1354 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1355 int offset
= offset_in_page(gpa
);
1356 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1359 ghc
->generation
= slots
->generation
;
1360 ghc
->memslot
= __gfn_to_memslot(slots
, gfn
);
1361 ghc
->hva
= gfn_to_hva_many(ghc
->memslot
, gfn
, NULL
);
1362 if (!kvm_is_error_hva(ghc
->hva
))
1369 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init
);
1371 int kvm_write_guest_cached(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1372 void *data
, unsigned long len
)
1374 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1377 if (slots
->generation
!= ghc
->generation
)
1378 kvm_gfn_to_hva_cache_init(kvm
, ghc
, ghc
->gpa
);
1380 if (kvm_is_error_hva(ghc
->hva
))
1383 r
= copy_to_user((void __user
*)ghc
->hva
, data
, len
);
1386 mark_page_dirty_in_slot(kvm
, ghc
->memslot
, ghc
->gpa
>> PAGE_SHIFT
);
1390 EXPORT_SYMBOL_GPL(kvm_write_guest_cached
);
1392 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1394 return kvm_write_guest_page(kvm
, gfn
, (const void *) empty_zero_page
,
1397 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1399 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1401 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1403 int offset
= offset_in_page(gpa
);
1406 while ((seg
= next_segment(len
, offset
)) != 0) {
1407 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1416 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1418 void mark_page_dirty_in_slot(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
1421 if (memslot
&& memslot
->dirty_bitmap
) {
1422 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1424 generic___set_le_bit(rel_gfn
, memslot
->dirty_bitmap
);
1428 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1430 struct kvm_memory_slot
*memslot
;
1432 memslot
= gfn_to_memslot(kvm
, gfn
);
1433 mark_page_dirty_in_slot(kvm
, memslot
, gfn
);
1437 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1439 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1444 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1446 if (kvm_arch_vcpu_runnable(vcpu
)) {
1447 kvm_make_request(KVM_REQ_UNHALT
, vcpu
);
1450 if (kvm_cpu_has_pending_timer(vcpu
))
1452 if (signal_pending(current
))
1458 finish_wait(&vcpu
->wq
, &wait
);
1461 void kvm_resched(struct kvm_vcpu
*vcpu
)
1463 if (!need_resched())
1467 EXPORT_SYMBOL_GPL(kvm_resched
);
1469 void kvm_vcpu_on_spin(struct kvm_vcpu
*vcpu
)
1474 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1476 /* Sleep for 100 us, and hope lock-holder got scheduled */
1477 expires
= ktime_add_ns(ktime_get(), 100000UL);
1478 schedule_hrtimeout(&expires
, HRTIMER_MODE_ABS
);
1480 finish_wait(&vcpu
->wq
, &wait
);
1482 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1484 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1486 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1489 if (vmf
->pgoff
== 0)
1490 page
= virt_to_page(vcpu
->run
);
1492 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1493 page
= virt_to_page(vcpu
->arch
.pio_data
);
1495 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1496 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1497 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1500 return VM_FAULT_SIGBUS
;
1506 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1507 .fault
= kvm_vcpu_fault
,
1510 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1512 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1516 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1518 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1520 kvm_put_kvm(vcpu
->kvm
);
1524 static struct file_operations kvm_vcpu_fops
= {
1525 .release
= kvm_vcpu_release
,
1526 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1527 .compat_ioctl
= kvm_vcpu_ioctl
,
1528 .mmap
= kvm_vcpu_mmap
,
1529 .llseek
= noop_llseek
,
1533 * Allocates an inode for the vcpu.
1535 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1537 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, O_RDWR
);
1541 * Creates some virtual cpus. Good luck creating more than one.
1543 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1546 struct kvm_vcpu
*vcpu
, *v
;
1548 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1550 return PTR_ERR(vcpu
);
1552 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1554 r
= kvm_arch_vcpu_setup(vcpu
);
1558 mutex_lock(&kvm
->lock
);
1559 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1564 kvm_for_each_vcpu(r
, v
, kvm
)
1565 if (v
->vcpu_id
== id
) {
1570 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1572 /* Now it's all set up, let userspace reach it */
1574 r
= create_vcpu_fd(vcpu
);
1580 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1582 atomic_inc(&kvm
->online_vcpus
);
1584 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1585 if (kvm
->bsp_vcpu_id
== id
)
1586 kvm
->bsp_vcpu
= vcpu
;
1588 mutex_unlock(&kvm
->lock
);
1592 mutex_unlock(&kvm
->lock
);
1593 kvm_arch_vcpu_destroy(vcpu
);
1597 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1600 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1601 vcpu
->sigset_active
= 1;
1602 vcpu
->sigset
= *sigset
;
1604 vcpu
->sigset_active
= 0;
1608 static long kvm_vcpu_ioctl(struct file
*filp
,
1609 unsigned int ioctl
, unsigned long arg
)
1611 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1612 void __user
*argp
= (void __user
*)arg
;
1614 struct kvm_fpu
*fpu
= NULL
;
1615 struct kvm_sregs
*kvm_sregs
= NULL
;
1617 if (vcpu
->kvm
->mm
!= current
->mm
)
1620 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1622 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1623 * so vcpu_load() would break it.
1625 if (ioctl
== KVM_S390_INTERRUPT
|| ioctl
== KVM_INTERRUPT
)
1626 return kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1636 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1637 trace_kvm_userspace_exit(vcpu
->run
->exit_reason
, r
);
1639 case KVM_GET_REGS
: {
1640 struct kvm_regs
*kvm_regs
;
1643 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1646 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1650 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1657 case KVM_SET_REGS
: {
1658 struct kvm_regs
*kvm_regs
;
1661 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1665 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1667 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1675 case KVM_GET_SREGS
: {
1676 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1680 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1684 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1689 case KVM_SET_SREGS
: {
1690 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1695 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1697 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1703 case KVM_GET_MP_STATE
: {
1704 struct kvm_mp_state mp_state
;
1706 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1710 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1715 case KVM_SET_MP_STATE
: {
1716 struct kvm_mp_state mp_state
;
1719 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1721 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1727 case KVM_TRANSLATE
: {
1728 struct kvm_translation tr
;
1731 if (copy_from_user(&tr
, argp
, sizeof tr
))
1733 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1737 if (copy_to_user(argp
, &tr
, sizeof tr
))
1742 case KVM_SET_GUEST_DEBUG
: {
1743 struct kvm_guest_debug dbg
;
1746 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1748 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1754 case KVM_SET_SIGNAL_MASK
: {
1755 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1756 struct kvm_signal_mask kvm_sigmask
;
1757 sigset_t sigset
, *p
;
1762 if (copy_from_user(&kvm_sigmask
, argp
,
1763 sizeof kvm_sigmask
))
1766 if (kvm_sigmask
.len
!= sizeof sigset
)
1769 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1774 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, p
);
1778 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1782 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1786 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1792 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1797 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1799 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1806 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1815 static long kvm_vm_ioctl(struct file
*filp
,
1816 unsigned int ioctl
, unsigned long arg
)
1818 struct kvm
*kvm
= filp
->private_data
;
1819 void __user
*argp
= (void __user
*)arg
;
1822 if (kvm
->mm
!= current
->mm
)
1825 case KVM_CREATE_VCPU
:
1826 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1830 case KVM_SET_USER_MEMORY_REGION
: {
1831 struct kvm_userspace_memory_region kvm_userspace_mem
;
1834 if (copy_from_user(&kvm_userspace_mem
, argp
,
1835 sizeof kvm_userspace_mem
))
1838 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1843 case KVM_GET_DIRTY_LOG
: {
1844 struct kvm_dirty_log log
;
1847 if (copy_from_user(&log
, argp
, sizeof log
))
1849 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1854 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1855 case KVM_REGISTER_COALESCED_MMIO
: {
1856 struct kvm_coalesced_mmio_zone zone
;
1858 if (copy_from_user(&zone
, argp
, sizeof zone
))
1860 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1866 case KVM_UNREGISTER_COALESCED_MMIO
: {
1867 struct kvm_coalesced_mmio_zone zone
;
1869 if (copy_from_user(&zone
, argp
, sizeof zone
))
1871 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1879 struct kvm_irqfd data
;
1882 if (copy_from_user(&data
, argp
, sizeof data
))
1884 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
1887 case KVM_IOEVENTFD
: {
1888 struct kvm_ioeventfd data
;
1891 if (copy_from_user(&data
, argp
, sizeof data
))
1893 r
= kvm_ioeventfd(kvm
, &data
);
1896 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1897 case KVM_SET_BOOT_CPU_ID
:
1899 mutex_lock(&kvm
->lock
);
1900 if (atomic_read(&kvm
->online_vcpus
) != 0)
1903 kvm
->bsp_vcpu_id
= arg
;
1904 mutex_unlock(&kvm
->lock
);
1908 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1910 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
1916 #ifdef CONFIG_COMPAT
1917 struct compat_kvm_dirty_log
{
1921 compat_uptr_t dirty_bitmap
; /* one bit per page */
1926 static long kvm_vm_compat_ioctl(struct file
*filp
,
1927 unsigned int ioctl
, unsigned long arg
)
1929 struct kvm
*kvm
= filp
->private_data
;
1932 if (kvm
->mm
!= current
->mm
)
1935 case KVM_GET_DIRTY_LOG
: {
1936 struct compat_kvm_dirty_log compat_log
;
1937 struct kvm_dirty_log log
;
1940 if (copy_from_user(&compat_log
, (void __user
*)arg
,
1941 sizeof(compat_log
)))
1943 log
.slot
= compat_log
.slot
;
1944 log
.padding1
= compat_log
.padding1
;
1945 log
.padding2
= compat_log
.padding2
;
1946 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
1948 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1954 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
1962 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1964 struct page
*page
[1];
1967 gfn_t gfn
= vmf
->pgoff
;
1968 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1970 addr
= gfn_to_hva(kvm
, gfn
);
1971 if (kvm_is_error_hva(addr
))
1972 return VM_FAULT_SIGBUS
;
1974 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1976 if (unlikely(npages
!= 1))
1977 return VM_FAULT_SIGBUS
;
1979 vmf
->page
= page
[0];
1983 static const struct vm_operations_struct kvm_vm_vm_ops
= {
1984 .fault
= kvm_vm_fault
,
1987 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1989 vma
->vm_ops
= &kvm_vm_vm_ops
;
1993 static struct file_operations kvm_vm_fops
= {
1994 .release
= kvm_vm_release
,
1995 .unlocked_ioctl
= kvm_vm_ioctl
,
1996 #ifdef CONFIG_COMPAT
1997 .compat_ioctl
= kvm_vm_compat_ioctl
,
1999 .mmap
= kvm_vm_mmap
,
2000 .llseek
= noop_llseek
,
2003 static int kvm_dev_ioctl_create_vm(void)
2008 kvm
= kvm_create_vm();
2010 return PTR_ERR(kvm
);
2011 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2012 r
= kvm_coalesced_mmio_init(kvm
);
2018 r
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, O_RDWR
);
2025 static long kvm_dev_ioctl_check_extension_generic(long arg
)
2028 case KVM_CAP_USER_MEMORY
:
2029 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
2030 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
2031 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2032 case KVM_CAP_SET_BOOT_CPU_ID
:
2034 case KVM_CAP_INTERNAL_ERROR_DATA
:
2036 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2037 case KVM_CAP_IRQ_ROUTING
:
2038 return KVM_MAX_IRQ_ROUTES
;
2043 return kvm_dev_ioctl_check_extension(arg
);
2046 static long kvm_dev_ioctl(struct file
*filp
,
2047 unsigned int ioctl
, unsigned long arg
)
2052 case KVM_GET_API_VERSION
:
2056 r
= KVM_API_VERSION
;
2062 r
= kvm_dev_ioctl_create_vm();
2064 case KVM_CHECK_EXTENSION
:
2065 r
= kvm_dev_ioctl_check_extension_generic(arg
);
2067 case KVM_GET_VCPU_MMAP_SIZE
:
2071 r
= PAGE_SIZE
; /* struct kvm_run */
2073 r
+= PAGE_SIZE
; /* pio data page */
2075 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2076 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
2079 case KVM_TRACE_ENABLE
:
2080 case KVM_TRACE_PAUSE
:
2081 case KVM_TRACE_DISABLE
:
2085 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
2091 static struct file_operations kvm_chardev_ops
= {
2092 .unlocked_ioctl
= kvm_dev_ioctl
,
2093 .compat_ioctl
= kvm_dev_ioctl
,
2094 .llseek
= noop_llseek
,
2097 static struct miscdevice kvm_dev
= {
2103 static void hardware_enable_nolock(void *junk
)
2105 int cpu
= raw_smp_processor_id();
2108 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2111 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
2113 r
= kvm_arch_hardware_enable(NULL
);
2116 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2117 atomic_inc(&hardware_enable_failed
);
2118 printk(KERN_INFO
"kvm: enabling virtualization on "
2119 "CPU%d failed\n", cpu
);
2123 static void hardware_enable(void *junk
)
2125 spin_lock(&kvm_lock
);
2126 hardware_enable_nolock(junk
);
2127 spin_unlock(&kvm_lock
);
2130 static void hardware_disable_nolock(void *junk
)
2132 int cpu
= raw_smp_processor_id();
2134 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2136 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2137 kvm_arch_hardware_disable(NULL
);
2140 static void hardware_disable(void *junk
)
2142 spin_lock(&kvm_lock
);
2143 hardware_disable_nolock(junk
);
2144 spin_unlock(&kvm_lock
);
2147 static void hardware_disable_all_nolock(void)
2149 BUG_ON(!kvm_usage_count
);
2152 if (!kvm_usage_count
)
2153 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2156 static void hardware_disable_all(void)
2158 spin_lock(&kvm_lock
);
2159 hardware_disable_all_nolock();
2160 spin_unlock(&kvm_lock
);
2163 static int hardware_enable_all(void)
2167 spin_lock(&kvm_lock
);
2170 if (kvm_usage_count
== 1) {
2171 atomic_set(&hardware_enable_failed
, 0);
2172 on_each_cpu(hardware_enable_nolock
, NULL
, 1);
2174 if (atomic_read(&hardware_enable_failed
)) {
2175 hardware_disable_all_nolock();
2180 spin_unlock(&kvm_lock
);
2185 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2190 if (!kvm_usage_count
)
2193 val
&= ~CPU_TASKS_FROZEN
;
2196 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2198 hardware_disable(NULL
);
2201 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2203 hardware_enable(NULL
);
2210 asmlinkage
void kvm_spurious_fault(void)
2212 /* Fault while not rebooting. We want the trace. */
2215 EXPORT_SYMBOL_GPL(kvm_spurious_fault
);
2217 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2221 * Some (well, at least mine) BIOSes hang on reboot if
2224 * And Intel TXT required VMX off for all cpu when system shutdown.
2226 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2227 kvm_rebooting
= true;
2228 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2232 static struct notifier_block kvm_reboot_notifier
= {
2233 .notifier_call
= kvm_reboot
,
2237 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2241 for (i
= 0; i
< bus
->dev_count
; i
++) {
2242 struct kvm_io_device
*pos
= bus
->devs
[i
];
2244 kvm_iodevice_destructor(pos
);
2249 /* kvm_io_bus_write - called under kvm->slots_lock */
2250 int kvm_io_bus_write(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2251 int len
, const void *val
)
2254 struct kvm_io_bus
*bus
;
2256 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2257 for (i
= 0; i
< bus
->dev_count
; i
++)
2258 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
2263 /* kvm_io_bus_read - called under kvm->slots_lock */
2264 int kvm_io_bus_read(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2268 struct kvm_io_bus
*bus
;
2270 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2271 for (i
= 0; i
< bus
->dev_count
; i
++)
2272 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
2277 /* Caller must hold slots_lock. */
2278 int kvm_io_bus_register_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2279 struct kvm_io_device
*dev
)
2281 struct kvm_io_bus
*new_bus
, *bus
;
2283 bus
= kvm
->buses
[bus_idx
];
2284 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2287 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2290 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2291 new_bus
->devs
[new_bus
->dev_count
++] = dev
;
2292 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2293 synchronize_srcu_expedited(&kvm
->srcu
);
2299 /* Caller must hold slots_lock. */
2300 int kvm_io_bus_unregister_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2301 struct kvm_io_device
*dev
)
2304 struct kvm_io_bus
*new_bus
, *bus
;
2306 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2310 bus
= kvm
->buses
[bus_idx
];
2311 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2314 for (i
= 0; i
< new_bus
->dev_count
; i
++)
2315 if (new_bus
->devs
[i
] == dev
) {
2317 new_bus
->devs
[i
] = new_bus
->devs
[--new_bus
->dev_count
];
2326 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2327 synchronize_srcu_expedited(&kvm
->srcu
);
2332 static struct notifier_block kvm_cpu_notifier
= {
2333 .notifier_call
= kvm_cpu_hotplug
,
2336 static int vm_stat_get(void *_offset
, u64
*val
)
2338 unsigned offset
= (long)_offset
;
2342 spin_lock(&kvm_lock
);
2343 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2344 *val
+= *(u32
*)((void *)kvm
+ offset
);
2345 spin_unlock(&kvm_lock
);
2349 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2351 static int vcpu_stat_get(void *_offset
, u64
*val
)
2353 unsigned offset
= (long)_offset
;
2355 struct kvm_vcpu
*vcpu
;
2359 spin_lock(&kvm_lock
);
2360 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2361 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2362 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2364 spin_unlock(&kvm_lock
);
2368 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2370 static const struct file_operations
*stat_fops
[] = {
2371 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2372 [KVM_STAT_VM
] = &vm_stat_fops
,
2375 static void kvm_init_debug(void)
2377 struct kvm_stats_debugfs_item
*p
;
2379 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2380 for (p
= debugfs_entries
; p
->name
; ++p
)
2381 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2382 (void *)(long)p
->offset
,
2383 stat_fops
[p
->kind
]);
2386 static void kvm_exit_debug(void)
2388 struct kvm_stats_debugfs_item
*p
;
2390 for (p
= debugfs_entries
; p
->name
; ++p
)
2391 debugfs_remove(p
->dentry
);
2392 debugfs_remove(kvm_debugfs_dir
);
2395 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2397 if (kvm_usage_count
)
2398 hardware_disable_nolock(NULL
);
2402 static int kvm_resume(struct sys_device
*dev
)
2404 if (kvm_usage_count
) {
2405 WARN_ON(spin_is_locked(&kvm_lock
));
2406 hardware_enable_nolock(NULL
);
2411 static struct sysdev_class kvm_sysdev_class
= {
2413 .suspend
= kvm_suspend
,
2414 .resume
= kvm_resume
,
2417 static struct sys_device kvm_sysdev
= {
2419 .cls
= &kvm_sysdev_class
,
2422 struct page
*bad_page
;
2426 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2428 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2431 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2433 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2435 kvm_arch_vcpu_load(vcpu
, cpu
);
2438 static void kvm_sched_out(struct preempt_notifier
*pn
,
2439 struct task_struct
*next
)
2441 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2443 kvm_arch_vcpu_put(vcpu
);
2446 int kvm_init(void *opaque
, unsigned vcpu_size
, unsigned vcpu_align
,
2447 struct module
*module
)
2452 r
= kvm_arch_init(opaque
);
2456 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2458 if (bad_page
== NULL
) {
2463 bad_pfn
= page_to_pfn(bad_page
);
2465 hwpoison_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2467 if (hwpoison_page
== NULL
) {
2472 hwpoison_pfn
= page_to_pfn(hwpoison_page
);
2474 fault_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2476 if (fault_page
== NULL
) {
2481 fault_pfn
= page_to_pfn(fault_page
);
2483 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2488 r
= kvm_arch_hardware_setup();
2492 for_each_online_cpu(cpu
) {
2493 smp_call_function_single(cpu
,
2494 kvm_arch_check_processor_compat
,
2500 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2503 register_reboot_notifier(&kvm_reboot_notifier
);
2505 r
= sysdev_class_register(&kvm_sysdev_class
);
2509 r
= sysdev_register(&kvm_sysdev
);
2513 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2515 vcpu_align
= __alignof__(struct kvm_vcpu
);
2516 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
, vcpu_align
,
2518 if (!kvm_vcpu_cache
) {
2523 r
= kvm_async_pf_init();
2527 kvm_chardev_ops
.owner
= module
;
2528 kvm_vm_fops
.owner
= module
;
2529 kvm_vcpu_fops
.owner
= module
;
2531 r
= misc_register(&kvm_dev
);
2533 printk(KERN_ERR
"kvm: misc device register failed\n");
2537 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2538 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2545 kvm_async_pf_deinit();
2547 kmem_cache_destroy(kvm_vcpu_cache
);
2549 sysdev_unregister(&kvm_sysdev
);
2551 sysdev_class_unregister(&kvm_sysdev_class
);
2553 unregister_reboot_notifier(&kvm_reboot_notifier
);
2554 unregister_cpu_notifier(&kvm_cpu_notifier
);
2557 kvm_arch_hardware_unsetup();
2559 free_cpumask_var(cpus_hardware_enabled
);
2562 __free_page(fault_page
);
2564 __free_page(hwpoison_page
);
2565 __free_page(bad_page
);
2571 EXPORT_SYMBOL_GPL(kvm_init
);
2576 misc_deregister(&kvm_dev
);
2577 kmem_cache_destroy(kvm_vcpu_cache
);
2578 kvm_async_pf_deinit();
2579 sysdev_unregister(&kvm_sysdev
);
2580 sysdev_class_unregister(&kvm_sysdev_class
);
2581 unregister_reboot_notifier(&kvm_reboot_notifier
);
2582 unregister_cpu_notifier(&kvm_cpu_notifier
);
2583 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2584 kvm_arch_hardware_unsetup();
2586 free_cpumask_var(cpus_hardware_enabled
);
2587 __free_page(hwpoison_page
);
2588 __free_page(bad_page
);
2590 EXPORT_SYMBOL_GPL(kvm_exit
);