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/syscore_ops.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>
56 #include "coalesced_mmio.h"
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/kvm.h>
62 MODULE_AUTHOR("Qumranet");
63 MODULE_LICENSE("GPL");
68 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
71 DEFINE_RAW_SPINLOCK(kvm_lock
);
74 static cpumask_var_t cpus_hardware_enabled
;
75 static int kvm_usage_count
= 0;
76 static atomic_t hardware_enable_failed
;
78 struct kmem_cache
*kvm_vcpu_cache
;
79 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
81 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
83 struct dentry
*kvm_debugfs_dir
;
85 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
88 static long kvm_vcpu_compat_ioctl(struct file
*file
, unsigned int ioctl
,
91 static int hardware_enable_all(void);
92 static void hardware_disable_all(void);
94 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
);
97 EXPORT_SYMBOL_GPL(kvm_rebooting
);
99 static bool largepages_enabled
= true;
101 static struct page
*hwpoison_page
;
102 static pfn_t hwpoison_pfn
;
104 static struct page
*fault_page
;
105 static pfn_t fault_pfn
;
107 inline int kvm_is_mmio_pfn(pfn_t pfn
)
109 if (pfn_valid(pfn
)) {
111 struct page
*tail
= pfn_to_page(pfn
);
112 struct page
*head
= compound_trans_head(tail
);
113 reserved
= PageReserved(head
);
116 * "head" is not a dangling pointer
117 * (compound_trans_head takes care of that)
118 * but the hugepage may have been splitted
119 * from under us (and we may not hold a
120 * reference count on the head page so it can
121 * be reused before we run PageReferenced), so
122 * we've to check PageTail before returning
129 return PageReserved(tail
);
136 * Switches to specified vcpu, until a matching vcpu_put()
138 void vcpu_load(struct kvm_vcpu
*vcpu
)
142 mutex_lock(&vcpu
->mutex
);
143 if (unlikely(vcpu
->pid
!= current
->pids
[PIDTYPE_PID
].pid
)) {
144 /* The thread running this VCPU changed. */
145 struct pid
*oldpid
= vcpu
->pid
;
146 struct pid
*newpid
= get_task_pid(current
, PIDTYPE_PID
);
147 rcu_assign_pointer(vcpu
->pid
, newpid
);
152 preempt_notifier_register(&vcpu
->preempt_notifier
);
153 kvm_arch_vcpu_load(vcpu
, cpu
);
157 void vcpu_put(struct kvm_vcpu
*vcpu
)
160 kvm_arch_vcpu_put(vcpu
);
161 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
163 mutex_unlock(&vcpu
->mutex
);
166 static void ack_flush(void *_completed
)
170 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
175 struct kvm_vcpu
*vcpu
;
177 zalloc_cpumask_var(&cpus
, GFP_ATOMIC
);
180 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
181 kvm_make_request(req
, vcpu
);
184 /* Set ->requests bit before we read ->mode */
187 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
&&
188 kvm_vcpu_exiting_guest_mode(vcpu
) != OUTSIDE_GUEST_MODE
)
189 cpumask_set_cpu(cpu
, cpus
);
191 if (unlikely(cpus
== NULL
))
192 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
193 else if (!cpumask_empty(cpus
))
194 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
198 free_cpumask_var(cpus
);
202 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
204 int dirty_count
= kvm
->tlbs_dirty
;
207 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
208 ++kvm
->stat
.remote_tlb_flush
;
209 cmpxchg(&kvm
->tlbs_dirty
, dirty_count
, 0);
212 void kvm_reload_remote_mmus(struct kvm
*kvm
)
214 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
217 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
222 mutex_init(&vcpu
->mutex
);
227 init_waitqueue_head(&vcpu
->wq
);
228 kvm_async_pf_vcpu_init(vcpu
);
230 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
235 vcpu
->run
= page_address(page
);
237 r
= kvm_arch_vcpu_init(vcpu
);
243 free_page((unsigned long)vcpu
->run
);
247 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
249 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
252 kvm_arch_vcpu_uninit(vcpu
);
253 free_page((unsigned long)vcpu
->run
);
255 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
257 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
258 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
260 return container_of(mn
, struct kvm
, mmu_notifier
);
263 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
264 struct mm_struct
*mm
,
265 unsigned long address
)
267 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
268 int need_tlb_flush
, idx
;
271 * When ->invalidate_page runs, the linux pte has been zapped
272 * already but the page is still allocated until
273 * ->invalidate_page returns. So if we increase the sequence
274 * here the kvm page fault will notice if the spte can't be
275 * established because the page is going to be freed. If
276 * instead the kvm page fault establishes the spte before
277 * ->invalidate_page runs, kvm_unmap_hva will release it
280 * The sequence increase only need to be seen at spin_unlock
281 * time, and not at spin_lock time.
283 * Increasing the sequence after the spin_unlock would be
284 * unsafe because the kvm page fault could then establish the
285 * pte after kvm_unmap_hva returned, without noticing the page
286 * is going to be freed.
288 idx
= srcu_read_lock(&kvm
->srcu
);
289 spin_lock(&kvm
->mmu_lock
);
290 kvm
->mmu_notifier_seq
++;
291 need_tlb_flush
= kvm_unmap_hva(kvm
, address
) | kvm
->tlbs_dirty
;
292 spin_unlock(&kvm
->mmu_lock
);
293 srcu_read_unlock(&kvm
->srcu
, idx
);
295 /* we've to flush the tlb before the pages can be freed */
297 kvm_flush_remote_tlbs(kvm
);
301 static void kvm_mmu_notifier_change_pte(struct mmu_notifier
*mn
,
302 struct mm_struct
*mm
,
303 unsigned long address
,
306 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
309 idx
= srcu_read_lock(&kvm
->srcu
);
310 spin_lock(&kvm
->mmu_lock
);
311 kvm
->mmu_notifier_seq
++;
312 kvm_set_spte_hva(kvm
, address
, pte
);
313 spin_unlock(&kvm
->mmu_lock
);
314 srcu_read_unlock(&kvm
->srcu
, idx
);
317 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
318 struct mm_struct
*mm
,
322 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
323 int need_tlb_flush
= 0, idx
;
325 idx
= srcu_read_lock(&kvm
->srcu
);
326 spin_lock(&kvm
->mmu_lock
);
328 * The count increase must become visible at unlock time as no
329 * spte can be established without taking the mmu_lock and
330 * count is also read inside the mmu_lock critical section.
332 kvm
->mmu_notifier_count
++;
333 for (; start
< end
; start
+= PAGE_SIZE
)
334 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
335 need_tlb_flush
|= kvm
->tlbs_dirty
;
336 spin_unlock(&kvm
->mmu_lock
);
337 srcu_read_unlock(&kvm
->srcu
, idx
);
339 /* we've to flush the tlb before the pages can be freed */
341 kvm_flush_remote_tlbs(kvm
);
344 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
345 struct mm_struct
*mm
,
349 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
351 spin_lock(&kvm
->mmu_lock
);
353 * This sequence increase will notify the kvm page fault that
354 * the page that is going to be mapped in the spte could have
357 kvm
->mmu_notifier_seq
++;
359 * The above sequence increase must be visible before the
360 * below count decrease but both values are read by the kvm
361 * page fault under mmu_lock spinlock so we don't need to add
362 * a smb_wmb() here in between the two.
364 kvm
->mmu_notifier_count
--;
365 spin_unlock(&kvm
->mmu_lock
);
367 BUG_ON(kvm
->mmu_notifier_count
< 0);
370 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
371 struct mm_struct
*mm
,
372 unsigned long address
)
374 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
377 idx
= srcu_read_lock(&kvm
->srcu
);
378 spin_lock(&kvm
->mmu_lock
);
379 young
= kvm_age_hva(kvm
, address
);
380 spin_unlock(&kvm
->mmu_lock
);
381 srcu_read_unlock(&kvm
->srcu
, idx
);
384 kvm_flush_remote_tlbs(kvm
);
389 static int kvm_mmu_notifier_test_young(struct mmu_notifier
*mn
,
390 struct mm_struct
*mm
,
391 unsigned long address
)
393 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
396 idx
= srcu_read_lock(&kvm
->srcu
);
397 spin_lock(&kvm
->mmu_lock
);
398 young
= kvm_test_age_hva(kvm
, address
);
399 spin_unlock(&kvm
->mmu_lock
);
400 srcu_read_unlock(&kvm
->srcu
, idx
);
405 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
406 struct mm_struct
*mm
)
408 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
411 idx
= srcu_read_lock(&kvm
->srcu
);
412 kvm_arch_flush_shadow(kvm
);
413 srcu_read_unlock(&kvm
->srcu
, idx
);
416 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
417 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
418 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
419 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
420 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
421 .test_young
= kvm_mmu_notifier_test_young
,
422 .change_pte
= kvm_mmu_notifier_change_pte
,
423 .release
= kvm_mmu_notifier_release
,
426 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
428 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
429 return mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
432 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
434 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
439 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
441 static struct kvm
*kvm_create_vm(void)
444 struct kvm
*kvm
= kvm_arch_alloc_vm();
447 return ERR_PTR(-ENOMEM
);
449 r
= kvm_arch_init_vm(kvm
);
451 goto out_err_nodisable
;
453 r
= hardware_enable_all();
455 goto out_err_nodisable
;
457 #ifdef CONFIG_HAVE_KVM_IRQCHIP
458 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
459 INIT_HLIST_HEAD(&kvm
->irq_ack_notifier_list
);
463 kvm
->memslots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
466 if (init_srcu_struct(&kvm
->srcu
))
468 for (i
= 0; i
< KVM_NR_BUSES
; i
++) {
469 kvm
->buses
[i
] = kzalloc(sizeof(struct kvm_io_bus
),
475 spin_lock_init(&kvm
->mmu_lock
);
476 kvm
->mm
= current
->mm
;
477 atomic_inc(&kvm
->mm
->mm_count
);
478 kvm_eventfd_init(kvm
);
479 mutex_init(&kvm
->lock
);
480 mutex_init(&kvm
->irq_lock
);
481 mutex_init(&kvm
->slots_lock
);
482 atomic_set(&kvm
->users_count
, 1);
484 r
= kvm_init_mmu_notifier(kvm
);
488 raw_spin_lock(&kvm_lock
);
489 list_add(&kvm
->vm_list
, &vm_list
);
490 raw_spin_unlock(&kvm_lock
);
495 cleanup_srcu_struct(&kvm
->srcu
);
497 hardware_disable_all();
499 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
500 kfree(kvm
->buses
[i
]);
501 kfree(kvm
->memslots
);
502 kvm_arch_free_vm(kvm
);
506 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot
*memslot
)
508 if (!memslot
->dirty_bitmap
)
511 if (2 * kvm_dirty_bitmap_bytes(memslot
) > PAGE_SIZE
)
512 vfree(memslot
->dirty_bitmap_head
);
514 kfree(memslot
->dirty_bitmap_head
);
516 memslot
->dirty_bitmap
= NULL
;
517 memslot
->dirty_bitmap_head
= NULL
;
521 * Free any memory in @free but not in @dont.
523 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
524 struct kvm_memory_slot
*dont
)
528 if (!dont
|| free
->rmap
!= dont
->rmap
)
531 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
532 kvm_destroy_dirty_bitmap(free
);
535 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
536 if (!dont
|| free
->lpage_info
[i
] != dont
->lpage_info
[i
]) {
537 vfree(free
->lpage_info
[i
]);
538 free
->lpage_info
[i
] = NULL
;
546 void kvm_free_physmem(struct kvm
*kvm
)
549 struct kvm_memslots
*slots
= kvm
->memslots
;
551 for (i
= 0; i
< slots
->nmemslots
; ++i
)
552 kvm_free_physmem_slot(&slots
->memslots
[i
], NULL
);
554 kfree(kvm
->memslots
);
557 static void kvm_destroy_vm(struct kvm
*kvm
)
560 struct mm_struct
*mm
= kvm
->mm
;
562 kvm_arch_sync_events(kvm
);
563 raw_spin_lock(&kvm_lock
);
564 list_del(&kvm
->vm_list
);
565 raw_spin_unlock(&kvm_lock
);
566 kvm_free_irq_routing(kvm
);
567 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
568 kvm_io_bus_destroy(kvm
->buses
[i
]);
569 kvm_coalesced_mmio_free(kvm
);
570 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
571 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
573 kvm_arch_flush_shadow(kvm
);
575 kvm_arch_destroy_vm(kvm
);
576 kvm_free_physmem(kvm
);
577 cleanup_srcu_struct(&kvm
->srcu
);
578 kvm_arch_free_vm(kvm
);
579 hardware_disable_all();
583 void kvm_get_kvm(struct kvm
*kvm
)
585 atomic_inc(&kvm
->users_count
);
587 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
589 void kvm_put_kvm(struct kvm
*kvm
)
591 if (atomic_dec_and_test(&kvm
->users_count
))
594 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
597 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
599 struct kvm
*kvm
= filp
->private_data
;
601 kvm_irqfd_release(kvm
);
609 * Allocation size is twice as large as the actual dirty bitmap size.
610 * This makes it possible to do double buffering: see x86's
611 * kvm_vm_ioctl_get_dirty_log().
613 static int kvm_create_dirty_bitmap(struct kvm_memory_slot
*memslot
)
615 unsigned long dirty_bytes
= 2 * kvm_dirty_bitmap_bytes(memslot
);
617 if (dirty_bytes
> PAGE_SIZE
)
618 memslot
->dirty_bitmap
= vzalloc(dirty_bytes
);
620 memslot
->dirty_bitmap
= kzalloc(dirty_bytes
, GFP_KERNEL
);
622 if (!memslot
->dirty_bitmap
)
625 memslot
->dirty_bitmap_head
= memslot
->dirty_bitmap
;
628 #endif /* !CONFIG_S390 */
631 * Allocate some memory and give it an address in the guest physical address
634 * Discontiguous memory is allowed, mostly for framebuffers.
636 * Must be called holding mmap_sem for write.
638 int __kvm_set_memory_region(struct kvm
*kvm
,
639 struct kvm_userspace_memory_region
*mem
,
644 unsigned long npages
;
646 struct kvm_memory_slot
*memslot
;
647 struct kvm_memory_slot old
, new;
648 struct kvm_memslots
*slots
, *old_memslots
;
651 /* General sanity checks */
652 if (mem
->memory_size
& (PAGE_SIZE
- 1))
654 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
656 /* We can read the guest memory with __xxx_user() later on. */
658 ((mem
->userspace_addr
& (PAGE_SIZE
- 1)) ||
659 !access_ok(VERIFY_WRITE
,
660 (void __user
*)(unsigned long)mem
->userspace_addr
,
663 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
665 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
668 memslot
= &kvm
->memslots
->memslots
[mem
->slot
];
669 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
670 npages
= mem
->memory_size
>> PAGE_SHIFT
;
673 if (npages
> KVM_MEM_MAX_NR_PAGES
)
677 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
679 new = old
= *memslot
;
682 new.base_gfn
= base_gfn
;
684 new.flags
= mem
->flags
;
686 /* Disallow changing a memory slot's size. */
688 if (npages
&& old
.npages
&& npages
!= old
.npages
)
691 /* Check for overlaps */
693 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
694 struct kvm_memory_slot
*s
= &kvm
->memslots
->memslots
[i
];
696 if (s
== memslot
|| !s
->npages
)
698 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
699 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
703 /* Free page dirty bitmap if unneeded */
704 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
705 new.dirty_bitmap
= NULL
;
709 /* Allocate if a slot is being created */
711 if (npages
&& !new.rmap
) {
712 new.rmap
= vzalloc(npages
* sizeof(*new.rmap
));
717 new.user_alloc
= user_alloc
;
718 new.userspace_addr
= mem
->userspace_addr
;
723 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
729 /* Avoid unused variable warning if no large pages */
732 if (new.lpage_info
[i
])
735 lpages
= 1 + ((base_gfn
+ npages
- 1)
736 >> KVM_HPAGE_GFN_SHIFT(level
));
737 lpages
-= base_gfn
>> KVM_HPAGE_GFN_SHIFT(level
);
739 new.lpage_info
[i
] = vzalloc(lpages
* sizeof(*new.lpage_info
[i
]));
741 if (!new.lpage_info
[i
])
744 if (base_gfn
& (KVM_PAGES_PER_HPAGE(level
) - 1))
745 new.lpage_info
[i
][0].write_count
= 1;
746 if ((base_gfn
+npages
) & (KVM_PAGES_PER_HPAGE(level
) - 1))
747 new.lpage_info
[i
][lpages
- 1].write_count
= 1;
748 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
750 * If the gfn and userspace address are not aligned wrt each
751 * other, or if explicitly asked to, disable large page
752 * support for this slot
754 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE(level
) - 1) ||
756 for (j
= 0; j
< lpages
; ++j
)
757 new.lpage_info
[i
][j
].write_count
= 1;
762 /* Allocate page dirty bitmap if needed */
763 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
764 if (kvm_create_dirty_bitmap(&new) < 0)
766 /* destroy any largepage mappings for dirty tracking */
768 #else /* not defined CONFIG_S390 */
769 new.user_alloc
= user_alloc
;
771 new.userspace_addr
= mem
->userspace_addr
;
772 #endif /* not defined CONFIG_S390 */
776 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
779 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
780 if (mem
->slot
>= slots
->nmemslots
)
781 slots
->nmemslots
= mem
->slot
+ 1;
783 slots
->memslots
[mem
->slot
].flags
|= KVM_MEMSLOT_INVALID
;
785 old_memslots
= kvm
->memslots
;
786 rcu_assign_pointer(kvm
->memslots
, slots
);
787 synchronize_srcu_expedited(&kvm
->srcu
);
788 /* From this point no new shadow pages pointing to a deleted
789 * memslot will be created.
791 * validation of sp->gfn happens in:
792 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
793 * - kvm_is_visible_gfn (mmu_check_roots)
795 kvm_arch_flush_shadow(kvm
);
799 r
= kvm_arch_prepare_memory_region(kvm
, &new, old
, mem
, user_alloc
);
803 /* map the pages in iommu page table */
805 r
= kvm_iommu_map_pages(kvm
, &new);
811 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
814 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
815 if (mem
->slot
>= slots
->nmemslots
)
816 slots
->nmemslots
= mem
->slot
+ 1;
819 /* actual memory is freed via old in kvm_free_physmem_slot below */
822 new.dirty_bitmap
= NULL
;
823 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
)
824 new.lpage_info
[i
] = NULL
;
827 slots
->memslots
[mem
->slot
] = new;
828 old_memslots
= kvm
->memslots
;
829 rcu_assign_pointer(kvm
->memslots
, slots
);
830 synchronize_srcu_expedited(&kvm
->srcu
);
832 kvm_arch_commit_memory_region(kvm
, mem
, old
, user_alloc
);
834 kvm_free_physmem_slot(&old
, &new);
840 kvm_free_physmem_slot(&new, &old
);
845 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
847 int kvm_set_memory_region(struct kvm
*kvm
,
848 struct kvm_userspace_memory_region
*mem
,
853 mutex_lock(&kvm
->slots_lock
);
854 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
855 mutex_unlock(&kvm
->slots_lock
);
858 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
860 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
862 kvm_userspace_memory_region
*mem
,
865 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
867 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
870 int kvm_get_dirty_log(struct kvm
*kvm
,
871 struct kvm_dirty_log
*log
, int *is_dirty
)
873 struct kvm_memory_slot
*memslot
;
876 unsigned long any
= 0;
879 if (log
->slot
>= KVM_MEMORY_SLOTS
)
882 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
884 if (!memslot
->dirty_bitmap
)
887 n
= kvm_dirty_bitmap_bytes(memslot
);
889 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
890 any
= memslot
->dirty_bitmap
[i
];
893 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
904 void kvm_disable_largepages(void)
906 largepages_enabled
= false;
908 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
910 int is_error_page(struct page
*page
)
912 return page
== bad_page
|| page
== hwpoison_page
|| page
== fault_page
;
914 EXPORT_SYMBOL_GPL(is_error_page
);
916 int is_error_pfn(pfn_t pfn
)
918 return pfn
== bad_pfn
|| pfn
== hwpoison_pfn
|| pfn
== fault_pfn
;
920 EXPORT_SYMBOL_GPL(is_error_pfn
);
922 int is_hwpoison_pfn(pfn_t pfn
)
924 return pfn
== hwpoison_pfn
;
926 EXPORT_SYMBOL_GPL(is_hwpoison_pfn
);
928 int is_fault_pfn(pfn_t pfn
)
930 return pfn
== fault_pfn
;
932 EXPORT_SYMBOL_GPL(is_fault_pfn
);
934 static inline unsigned long bad_hva(void)
939 int kvm_is_error_hva(unsigned long addr
)
941 return addr
== bad_hva();
943 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
945 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm_memslots
*slots
,
950 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
951 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
953 if (gfn
>= memslot
->base_gfn
954 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
960 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
962 return __gfn_to_memslot(kvm_memslots(kvm
), gfn
);
964 EXPORT_SYMBOL_GPL(gfn_to_memslot
);
966 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
969 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
971 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
972 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
974 if (memslot
->flags
& KVM_MEMSLOT_INVALID
)
977 if (gfn
>= memslot
->base_gfn
978 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
983 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
985 unsigned long kvm_host_page_size(struct kvm
*kvm
, gfn_t gfn
)
987 struct vm_area_struct
*vma
;
988 unsigned long addr
, size
;
992 addr
= gfn_to_hva(kvm
, gfn
);
993 if (kvm_is_error_hva(addr
))
996 down_read(¤t
->mm
->mmap_sem
);
997 vma
= find_vma(current
->mm
, addr
);
1001 size
= vma_kernel_pagesize(vma
);
1004 up_read(¤t
->mm
->mmap_sem
);
1009 static unsigned long gfn_to_hva_many(struct kvm_memory_slot
*slot
, gfn_t gfn
,
1012 if (!slot
|| slot
->flags
& KVM_MEMSLOT_INVALID
)
1016 *nr_pages
= slot
->npages
- (gfn
- slot
->base_gfn
);
1018 return gfn_to_hva_memslot(slot
, gfn
);
1021 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1023 return gfn_to_hva_many(gfn_to_memslot(kvm
, gfn
), gfn
, NULL
);
1025 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1027 static pfn_t
get_fault_pfn(void)
1029 get_page(fault_page
);
1033 int get_user_page_nowait(struct task_struct
*tsk
, struct mm_struct
*mm
,
1034 unsigned long start
, int write
, struct page
**page
)
1036 int flags
= FOLL_TOUCH
| FOLL_NOWAIT
| FOLL_HWPOISON
| FOLL_GET
;
1039 flags
|= FOLL_WRITE
;
1041 return __get_user_pages(tsk
, mm
, start
, 1, flags
, page
, NULL
, NULL
);
1044 static inline int check_user_page_hwpoison(unsigned long addr
)
1046 int rc
, flags
= FOLL_TOUCH
| FOLL_HWPOISON
| FOLL_WRITE
;
1048 rc
= __get_user_pages(current
, current
->mm
, addr
, 1,
1049 flags
, NULL
, NULL
, NULL
);
1050 return rc
== -EHWPOISON
;
1053 static pfn_t
hva_to_pfn(struct kvm
*kvm
, unsigned long addr
, bool atomic
,
1054 bool *async
, bool write_fault
, bool *writable
)
1056 struct page
*page
[1];
1060 /* we can do it either atomically or asynchronously, not both */
1061 BUG_ON(atomic
&& async
);
1063 BUG_ON(!write_fault
&& !writable
);
1068 if (atomic
|| async
)
1069 npages
= __get_user_pages_fast(addr
, 1, 1, page
);
1071 if (unlikely(npages
!= 1) && !atomic
) {
1075 *writable
= write_fault
;
1078 down_read(¤t
->mm
->mmap_sem
);
1079 npages
= get_user_page_nowait(current
, current
->mm
,
1080 addr
, write_fault
, page
);
1081 up_read(¤t
->mm
->mmap_sem
);
1083 npages
= get_user_pages_fast(addr
, 1, write_fault
,
1086 /* map read fault as writable if possible */
1087 if (unlikely(!write_fault
) && npages
== 1) {
1088 struct page
*wpage
[1];
1090 npages
= __get_user_pages_fast(addr
, 1, 1, wpage
);
1100 if (unlikely(npages
!= 1)) {
1101 struct vm_area_struct
*vma
;
1104 return get_fault_pfn();
1106 down_read(¤t
->mm
->mmap_sem
);
1107 if (npages
== -EHWPOISON
||
1108 (!async
&& check_user_page_hwpoison(addr
))) {
1109 up_read(¤t
->mm
->mmap_sem
);
1110 get_page(hwpoison_page
);
1111 return page_to_pfn(hwpoison_page
);
1114 vma
= find_vma_intersection(current
->mm
, addr
, addr
+1);
1117 pfn
= get_fault_pfn();
1118 else if ((vma
->vm_flags
& VM_PFNMAP
)) {
1119 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) +
1121 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1123 if (async
&& (vma
->vm_flags
& VM_WRITE
))
1125 pfn
= get_fault_pfn();
1127 up_read(¤t
->mm
->mmap_sem
);
1129 pfn
= page_to_pfn(page
[0]);
1134 pfn_t
hva_to_pfn_atomic(struct kvm
*kvm
, unsigned long addr
)
1136 return hva_to_pfn(kvm
, addr
, true, NULL
, true, NULL
);
1138 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic
);
1140 static pfn_t
__gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
, bool atomic
, bool *async
,
1141 bool write_fault
, bool *writable
)
1148 addr
= gfn_to_hva(kvm
, gfn
);
1149 if (kvm_is_error_hva(addr
)) {
1151 return page_to_pfn(bad_page
);
1154 return hva_to_pfn(kvm
, addr
, atomic
, async
, write_fault
, writable
);
1157 pfn_t
gfn_to_pfn_atomic(struct kvm
*kvm
, gfn_t gfn
)
1159 return __gfn_to_pfn(kvm
, gfn
, true, NULL
, true, NULL
);
1161 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic
);
1163 pfn_t
gfn_to_pfn_async(struct kvm
*kvm
, gfn_t gfn
, bool *async
,
1164 bool write_fault
, bool *writable
)
1166 return __gfn_to_pfn(kvm
, gfn
, false, async
, write_fault
, writable
);
1168 EXPORT_SYMBOL_GPL(gfn_to_pfn_async
);
1170 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1172 return __gfn_to_pfn(kvm
, gfn
, false, NULL
, true, NULL
);
1174 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1176 pfn_t
gfn_to_pfn_prot(struct kvm
*kvm
, gfn_t gfn
, bool write_fault
,
1179 return __gfn_to_pfn(kvm
, gfn
, false, NULL
, write_fault
, writable
);
1181 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot
);
1183 pfn_t
gfn_to_pfn_memslot(struct kvm
*kvm
,
1184 struct kvm_memory_slot
*slot
, gfn_t gfn
)
1186 unsigned long addr
= gfn_to_hva_memslot(slot
, gfn
);
1187 return hva_to_pfn(kvm
, addr
, false, NULL
, true, NULL
);
1190 int gfn_to_page_many_atomic(struct kvm
*kvm
, gfn_t gfn
, struct page
**pages
,
1196 addr
= gfn_to_hva_many(gfn_to_memslot(kvm
, gfn
), gfn
, &entry
);
1197 if (kvm_is_error_hva(addr
))
1200 if (entry
< nr_pages
)
1203 return __get_user_pages_fast(addr
, nr_pages
, 1, pages
);
1205 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic
);
1207 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1211 pfn
= gfn_to_pfn(kvm
, gfn
);
1212 if (!kvm_is_mmio_pfn(pfn
))
1213 return pfn_to_page(pfn
);
1215 WARN_ON(kvm_is_mmio_pfn(pfn
));
1221 EXPORT_SYMBOL_GPL(gfn_to_page
);
1223 void kvm_release_page_clean(struct page
*page
)
1225 kvm_release_pfn_clean(page_to_pfn(page
));
1227 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1229 void kvm_release_pfn_clean(pfn_t pfn
)
1231 if (!kvm_is_mmio_pfn(pfn
))
1232 put_page(pfn_to_page(pfn
));
1234 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1236 void kvm_release_page_dirty(struct page
*page
)
1238 kvm_release_pfn_dirty(page_to_pfn(page
));
1240 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1242 void kvm_release_pfn_dirty(pfn_t pfn
)
1244 kvm_set_pfn_dirty(pfn
);
1245 kvm_release_pfn_clean(pfn
);
1247 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1249 void kvm_set_page_dirty(struct page
*page
)
1251 kvm_set_pfn_dirty(page_to_pfn(page
));
1253 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1255 void kvm_set_pfn_dirty(pfn_t pfn
)
1257 if (!kvm_is_mmio_pfn(pfn
)) {
1258 struct page
*page
= pfn_to_page(pfn
);
1259 if (!PageReserved(page
))
1263 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1265 void kvm_set_pfn_accessed(pfn_t pfn
)
1267 if (!kvm_is_mmio_pfn(pfn
))
1268 mark_page_accessed(pfn_to_page(pfn
));
1270 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1272 void kvm_get_pfn(pfn_t pfn
)
1274 if (!kvm_is_mmio_pfn(pfn
))
1275 get_page(pfn_to_page(pfn
));
1277 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1279 static int next_segment(unsigned long len
, int offset
)
1281 if (len
> PAGE_SIZE
- offset
)
1282 return PAGE_SIZE
- offset
;
1287 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1293 addr
= gfn_to_hva(kvm
, gfn
);
1294 if (kvm_is_error_hva(addr
))
1296 r
= __copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1301 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1303 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1305 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1307 int offset
= offset_in_page(gpa
);
1310 while ((seg
= next_segment(len
, offset
)) != 0) {
1311 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1321 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1323 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1328 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1329 int offset
= offset_in_page(gpa
);
1331 addr
= gfn_to_hva(kvm
, gfn
);
1332 if (kvm_is_error_hva(addr
))
1334 pagefault_disable();
1335 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1341 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1343 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1344 int offset
, int len
)
1349 addr
= gfn_to_hva(kvm
, gfn
);
1350 if (kvm_is_error_hva(addr
))
1352 r
= __copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1355 mark_page_dirty(kvm
, gfn
);
1358 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1360 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1363 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1365 int offset
= offset_in_page(gpa
);
1368 while ((seg
= next_segment(len
, offset
)) != 0) {
1369 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1380 int kvm_gfn_to_hva_cache_init(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1383 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1384 int offset
= offset_in_page(gpa
);
1385 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1388 ghc
->generation
= slots
->generation
;
1389 ghc
->memslot
= __gfn_to_memslot(slots
, gfn
);
1390 ghc
->hva
= gfn_to_hva_many(ghc
->memslot
, gfn
, NULL
);
1391 if (!kvm_is_error_hva(ghc
->hva
))
1398 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init
);
1400 int kvm_write_guest_cached(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1401 void *data
, unsigned long len
)
1403 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1406 if (slots
->generation
!= ghc
->generation
)
1407 kvm_gfn_to_hva_cache_init(kvm
, ghc
, ghc
->gpa
);
1409 if (kvm_is_error_hva(ghc
->hva
))
1412 r
= __copy_to_user((void __user
*)ghc
->hva
, data
, len
);
1415 mark_page_dirty_in_slot(kvm
, ghc
->memslot
, ghc
->gpa
>> PAGE_SHIFT
);
1419 EXPORT_SYMBOL_GPL(kvm_write_guest_cached
);
1421 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1423 return kvm_write_guest_page(kvm
, gfn
, (const void *) empty_zero_page
,
1426 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1428 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1430 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1432 int offset
= offset_in_page(gpa
);
1435 while ((seg
= next_segment(len
, offset
)) != 0) {
1436 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1445 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1447 void mark_page_dirty_in_slot(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
1450 if (memslot
&& memslot
->dirty_bitmap
) {
1451 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1453 __set_bit_le(rel_gfn
, memslot
->dirty_bitmap
);
1457 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1459 struct kvm_memory_slot
*memslot
;
1461 memslot
= gfn_to_memslot(kvm
, gfn
);
1462 mark_page_dirty_in_slot(kvm
, memslot
, gfn
);
1466 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1468 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1473 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1475 if (kvm_arch_vcpu_runnable(vcpu
)) {
1476 kvm_make_request(KVM_REQ_UNHALT
, vcpu
);
1479 if (kvm_cpu_has_pending_timer(vcpu
))
1481 if (signal_pending(current
))
1487 finish_wait(&vcpu
->wq
, &wait
);
1490 void kvm_resched(struct kvm_vcpu
*vcpu
)
1492 if (!need_resched())
1496 EXPORT_SYMBOL_GPL(kvm_resched
);
1498 void kvm_vcpu_on_spin(struct kvm_vcpu
*me
)
1500 struct kvm
*kvm
= me
->kvm
;
1501 struct kvm_vcpu
*vcpu
;
1502 int last_boosted_vcpu
= me
->kvm
->last_boosted_vcpu
;
1508 * We boost the priority of a VCPU that is runnable but not
1509 * currently running, because it got preempted by something
1510 * else and called schedule in __vcpu_run. Hopefully that
1511 * VCPU is holding the lock that we need and will release it.
1512 * We approximate round-robin by starting at the last boosted VCPU.
1514 for (pass
= 0; pass
< 2 && !yielded
; pass
++) {
1515 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
1516 struct task_struct
*task
= NULL
;
1518 if (!pass
&& i
< last_boosted_vcpu
) {
1519 i
= last_boosted_vcpu
;
1521 } else if (pass
&& i
> last_boosted_vcpu
)
1525 if (waitqueue_active(&vcpu
->wq
))
1528 pid
= rcu_dereference(vcpu
->pid
);
1530 task
= get_pid_task(vcpu
->pid
, PIDTYPE_PID
);
1534 if (task
->flags
& PF_VCPU
) {
1535 put_task_struct(task
);
1538 if (yield_to(task
, 1)) {
1539 put_task_struct(task
);
1540 kvm
->last_boosted_vcpu
= i
;
1544 put_task_struct(task
);
1548 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1550 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1552 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1555 if (vmf
->pgoff
== 0)
1556 page
= virt_to_page(vcpu
->run
);
1558 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1559 page
= virt_to_page(vcpu
->arch
.pio_data
);
1561 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1562 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1563 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1566 return VM_FAULT_SIGBUS
;
1572 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1573 .fault
= kvm_vcpu_fault
,
1576 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1578 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1582 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1584 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1586 kvm_put_kvm(vcpu
->kvm
);
1590 static struct file_operations kvm_vcpu_fops
= {
1591 .release
= kvm_vcpu_release
,
1592 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1593 #ifdef CONFIG_COMPAT
1594 .compat_ioctl
= kvm_vcpu_compat_ioctl
,
1596 .mmap
= kvm_vcpu_mmap
,
1597 .llseek
= noop_llseek
,
1601 * Allocates an inode for the vcpu.
1603 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1605 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, O_RDWR
);
1609 * Creates some virtual cpus. Good luck creating more than one.
1611 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1614 struct kvm_vcpu
*vcpu
, *v
;
1616 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1618 return PTR_ERR(vcpu
);
1620 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1622 r
= kvm_arch_vcpu_setup(vcpu
);
1626 mutex_lock(&kvm
->lock
);
1627 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1629 goto unlock_vcpu_destroy
;
1632 kvm_for_each_vcpu(r
, v
, kvm
)
1633 if (v
->vcpu_id
== id
) {
1635 goto unlock_vcpu_destroy
;
1638 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1640 /* Now it's all set up, let userspace reach it */
1642 r
= create_vcpu_fd(vcpu
);
1645 goto unlock_vcpu_destroy
;
1648 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1650 atomic_inc(&kvm
->online_vcpus
);
1652 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1653 if (kvm
->bsp_vcpu_id
== id
)
1654 kvm
->bsp_vcpu
= vcpu
;
1656 mutex_unlock(&kvm
->lock
);
1659 unlock_vcpu_destroy
:
1660 mutex_unlock(&kvm
->lock
);
1662 kvm_arch_vcpu_destroy(vcpu
);
1666 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1669 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1670 vcpu
->sigset_active
= 1;
1671 vcpu
->sigset
= *sigset
;
1673 vcpu
->sigset_active
= 0;
1677 static long kvm_vcpu_ioctl(struct file
*filp
,
1678 unsigned int ioctl
, unsigned long arg
)
1680 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1681 void __user
*argp
= (void __user
*)arg
;
1683 struct kvm_fpu
*fpu
= NULL
;
1684 struct kvm_sregs
*kvm_sregs
= NULL
;
1686 if (vcpu
->kvm
->mm
!= current
->mm
)
1689 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1691 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1692 * so vcpu_load() would break it.
1694 if (ioctl
== KVM_S390_INTERRUPT
|| ioctl
== KVM_INTERRUPT
)
1695 return kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1705 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1706 trace_kvm_userspace_exit(vcpu
->run
->exit_reason
, r
);
1708 case KVM_GET_REGS
: {
1709 struct kvm_regs
*kvm_regs
;
1712 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1715 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1719 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1726 case KVM_SET_REGS
: {
1727 struct kvm_regs
*kvm_regs
;
1730 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1734 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1736 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1744 case KVM_GET_SREGS
: {
1745 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1749 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1753 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1758 case KVM_SET_SREGS
: {
1759 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1764 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1766 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1772 case KVM_GET_MP_STATE
: {
1773 struct kvm_mp_state mp_state
;
1775 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1779 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1784 case KVM_SET_MP_STATE
: {
1785 struct kvm_mp_state mp_state
;
1788 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1790 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1796 case KVM_TRANSLATE
: {
1797 struct kvm_translation tr
;
1800 if (copy_from_user(&tr
, argp
, sizeof tr
))
1802 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1806 if (copy_to_user(argp
, &tr
, sizeof tr
))
1811 case KVM_SET_GUEST_DEBUG
: {
1812 struct kvm_guest_debug dbg
;
1815 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1817 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1823 case KVM_SET_SIGNAL_MASK
: {
1824 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1825 struct kvm_signal_mask kvm_sigmask
;
1826 sigset_t sigset
, *p
;
1831 if (copy_from_user(&kvm_sigmask
, argp
,
1832 sizeof kvm_sigmask
))
1835 if (kvm_sigmask
.len
!= sizeof sigset
)
1838 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1843 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, p
);
1847 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1851 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1855 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1861 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1866 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1868 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1875 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1884 #ifdef CONFIG_COMPAT
1885 static long kvm_vcpu_compat_ioctl(struct file
*filp
,
1886 unsigned int ioctl
, unsigned long arg
)
1888 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1889 void __user
*argp
= compat_ptr(arg
);
1892 if (vcpu
->kvm
->mm
!= current
->mm
)
1896 case KVM_SET_SIGNAL_MASK
: {
1897 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1898 struct kvm_signal_mask kvm_sigmask
;
1899 compat_sigset_t csigset
;
1904 if (copy_from_user(&kvm_sigmask
, argp
,
1905 sizeof kvm_sigmask
))
1908 if (kvm_sigmask
.len
!= sizeof csigset
)
1911 if (copy_from_user(&csigset
, sigmask_arg
->sigset
,
1915 sigset_from_compat(&sigset
, &csigset
);
1916 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1920 r
= kvm_vcpu_ioctl(filp
, ioctl
, arg
);
1928 static long kvm_vm_ioctl(struct file
*filp
,
1929 unsigned int ioctl
, unsigned long arg
)
1931 struct kvm
*kvm
= filp
->private_data
;
1932 void __user
*argp
= (void __user
*)arg
;
1935 if (kvm
->mm
!= current
->mm
)
1938 case KVM_CREATE_VCPU
:
1939 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1943 case KVM_SET_USER_MEMORY_REGION
: {
1944 struct kvm_userspace_memory_region kvm_userspace_mem
;
1947 if (copy_from_user(&kvm_userspace_mem
, argp
,
1948 sizeof kvm_userspace_mem
))
1951 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1956 case KVM_GET_DIRTY_LOG
: {
1957 struct kvm_dirty_log log
;
1960 if (copy_from_user(&log
, argp
, sizeof log
))
1962 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1967 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1968 case KVM_REGISTER_COALESCED_MMIO
: {
1969 struct kvm_coalesced_mmio_zone zone
;
1971 if (copy_from_user(&zone
, argp
, sizeof zone
))
1973 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1979 case KVM_UNREGISTER_COALESCED_MMIO
: {
1980 struct kvm_coalesced_mmio_zone zone
;
1982 if (copy_from_user(&zone
, argp
, sizeof zone
))
1984 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1992 struct kvm_irqfd data
;
1995 if (copy_from_user(&data
, argp
, sizeof data
))
1997 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
2000 case KVM_IOEVENTFD
: {
2001 struct kvm_ioeventfd data
;
2004 if (copy_from_user(&data
, argp
, sizeof data
))
2006 r
= kvm_ioeventfd(kvm
, &data
);
2009 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2010 case KVM_SET_BOOT_CPU_ID
:
2012 mutex_lock(&kvm
->lock
);
2013 if (atomic_read(&kvm
->online_vcpus
) != 0)
2016 kvm
->bsp_vcpu_id
= arg
;
2017 mutex_unlock(&kvm
->lock
);
2021 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
2023 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
2029 #ifdef CONFIG_COMPAT
2030 struct compat_kvm_dirty_log
{
2034 compat_uptr_t dirty_bitmap
; /* one bit per page */
2039 static long kvm_vm_compat_ioctl(struct file
*filp
,
2040 unsigned int ioctl
, unsigned long arg
)
2042 struct kvm
*kvm
= filp
->private_data
;
2045 if (kvm
->mm
!= current
->mm
)
2048 case KVM_GET_DIRTY_LOG
: {
2049 struct compat_kvm_dirty_log compat_log
;
2050 struct kvm_dirty_log log
;
2053 if (copy_from_user(&compat_log
, (void __user
*)arg
,
2054 sizeof(compat_log
)))
2056 log
.slot
= compat_log
.slot
;
2057 log
.padding1
= compat_log
.padding1
;
2058 log
.padding2
= compat_log
.padding2
;
2059 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
2061 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2067 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
2075 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2077 struct page
*page
[1];
2080 gfn_t gfn
= vmf
->pgoff
;
2081 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2083 addr
= gfn_to_hva(kvm
, gfn
);
2084 if (kvm_is_error_hva(addr
))
2085 return VM_FAULT_SIGBUS
;
2087 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
2089 if (unlikely(npages
!= 1))
2090 return VM_FAULT_SIGBUS
;
2092 vmf
->page
= page
[0];
2096 static const struct vm_operations_struct kvm_vm_vm_ops
= {
2097 .fault
= kvm_vm_fault
,
2100 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2102 vma
->vm_ops
= &kvm_vm_vm_ops
;
2106 static struct file_operations kvm_vm_fops
= {
2107 .release
= kvm_vm_release
,
2108 .unlocked_ioctl
= kvm_vm_ioctl
,
2109 #ifdef CONFIG_COMPAT
2110 .compat_ioctl
= kvm_vm_compat_ioctl
,
2112 .mmap
= kvm_vm_mmap
,
2113 .llseek
= noop_llseek
,
2116 static int kvm_dev_ioctl_create_vm(void)
2121 kvm
= kvm_create_vm();
2123 return PTR_ERR(kvm
);
2124 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2125 r
= kvm_coalesced_mmio_init(kvm
);
2131 r
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, O_RDWR
);
2138 static long kvm_dev_ioctl_check_extension_generic(long arg
)
2141 case KVM_CAP_USER_MEMORY
:
2142 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
2143 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
2144 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2145 case KVM_CAP_SET_BOOT_CPU_ID
:
2147 case KVM_CAP_INTERNAL_ERROR_DATA
:
2149 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2150 case KVM_CAP_IRQ_ROUTING
:
2151 return KVM_MAX_IRQ_ROUTES
;
2156 return kvm_dev_ioctl_check_extension(arg
);
2159 static long kvm_dev_ioctl(struct file
*filp
,
2160 unsigned int ioctl
, unsigned long arg
)
2165 case KVM_GET_API_VERSION
:
2169 r
= KVM_API_VERSION
;
2175 r
= kvm_dev_ioctl_create_vm();
2177 case KVM_CHECK_EXTENSION
:
2178 r
= kvm_dev_ioctl_check_extension_generic(arg
);
2180 case KVM_GET_VCPU_MMAP_SIZE
:
2184 r
= PAGE_SIZE
; /* struct kvm_run */
2186 r
+= PAGE_SIZE
; /* pio data page */
2188 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2189 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
2192 case KVM_TRACE_ENABLE
:
2193 case KVM_TRACE_PAUSE
:
2194 case KVM_TRACE_DISABLE
:
2198 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
2204 static struct file_operations kvm_chardev_ops
= {
2205 .unlocked_ioctl
= kvm_dev_ioctl
,
2206 .compat_ioctl
= kvm_dev_ioctl
,
2207 .llseek
= noop_llseek
,
2210 static struct miscdevice kvm_dev
= {
2216 static void hardware_enable_nolock(void *junk
)
2218 int cpu
= raw_smp_processor_id();
2221 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2224 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
2226 r
= kvm_arch_hardware_enable(NULL
);
2229 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2230 atomic_inc(&hardware_enable_failed
);
2231 printk(KERN_INFO
"kvm: enabling virtualization on "
2232 "CPU%d failed\n", cpu
);
2236 static void hardware_enable(void *junk
)
2238 raw_spin_lock(&kvm_lock
);
2239 hardware_enable_nolock(junk
);
2240 raw_spin_unlock(&kvm_lock
);
2243 static void hardware_disable_nolock(void *junk
)
2245 int cpu
= raw_smp_processor_id();
2247 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2249 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2250 kvm_arch_hardware_disable(NULL
);
2253 static void hardware_disable(void *junk
)
2255 raw_spin_lock(&kvm_lock
);
2256 hardware_disable_nolock(junk
);
2257 raw_spin_unlock(&kvm_lock
);
2260 static void hardware_disable_all_nolock(void)
2262 BUG_ON(!kvm_usage_count
);
2265 if (!kvm_usage_count
)
2266 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2269 static void hardware_disable_all(void)
2271 raw_spin_lock(&kvm_lock
);
2272 hardware_disable_all_nolock();
2273 raw_spin_unlock(&kvm_lock
);
2276 static int hardware_enable_all(void)
2280 raw_spin_lock(&kvm_lock
);
2283 if (kvm_usage_count
== 1) {
2284 atomic_set(&hardware_enable_failed
, 0);
2285 on_each_cpu(hardware_enable_nolock
, NULL
, 1);
2287 if (atomic_read(&hardware_enable_failed
)) {
2288 hardware_disable_all_nolock();
2293 raw_spin_unlock(&kvm_lock
);
2298 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2303 if (!kvm_usage_count
)
2306 val
&= ~CPU_TASKS_FROZEN
;
2309 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2311 hardware_disable(NULL
);
2314 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2316 hardware_enable(NULL
);
2323 asmlinkage
void kvm_spurious_fault(void)
2325 /* Fault while not rebooting. We want the trace. */
2328 EXPORT_SYMBOL_GPL(kvm_spurious_fault
);
2330 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2334 * Some (well, at least mine) BIOSes hang on reboot if
2337 * And Intel TXT required VMX off for all cpu when system shutdown.
2339 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2340 kvm_rebooting
= true;
2341 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2345 static struct notifier_block kvm_reboot_notifier
= {
2346 .notifier_call
= kvm_reboot
,
2350 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2354 for (i
= 0; i
< bus
->dev_count
; i
++) {
2355 struct kvm_io_device
*pos
= bus
->devs
[i
];
2357 kvm_iodevice_destructor(pos
);
2362 /* kvm_io_bus_write - called under kvm->slots_lock */
2363 int kvm_io_bus_write(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2364 int len
, const void *val
)
2367 struct kvm_io_bus
*bus
;
2369 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2370 for (i
= 0; i
< bus
->dev_count
; i
++)
2371 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
2376 /* kvm_io_bus_read - called under kvm->slots_lock */
2377 int kvm_io_bus_read(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2381 struct kvm_io_bus
*bus
;
2383 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2384 for (i
= 0; i
< bus
->dev_count
; i
++)
2385 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
2390 /* Caller must hold slots_lock. */
2391 int kvm_io_bus_register_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2392 struct kvm_io_device
*dev
)
2394 struct kvm_io_bus
*new_bus
, *bus
;
2396 bus
= kvm
->buses
[bus_idx
];
2397 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2400 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2403 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2404 new_bus
->devs
[new_bus
->dev_count
++] = dev
;
2405 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2406 synchronize_srcu_expedited(&kvm
->srcu
);
2412 /* Caller must hold slots_lock. */
2413 int kvm_io_bus_unregister_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2414 struct kvm_io_device
*dev
)
2417 struct kvm_io_bus
*new_bus
, *bus
;
2419 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2423 bus
= kvm
->buses
[bus_idx
];
2424 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2427 for (i
= 0; i
< new_bus
->dev_count
; i
++)
2428 if (new_bus
->devs
[i
] == dev
) {
2430 new_bus
->devs
[i
] = new_bus
->devs
[--new_bus
->dev_count
];
2439 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2440 synchronize_srcu_expedited(&kvm
->srcu
);
2445 static struct notifier_block kvm_cpu_notifier
= {
2446 .notifier_call
= kvm_cpu_hotplug
,
2449 static int vm_stat_get(void *_offset
, u64
*val
)
2451 unsigned offset
= (long)_offset
;
2455 raw_spin_lock(&kvm_lock
);
2456 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2457 *val
+= *(u32
*)((void *)kvm
+ offset
);
2458 raw_spin_unlock(&kvm_lock
);
2462 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2464 static int vcpu_stat_get(void *_offset
, u64
*val
)
2466 unsigned offset
= (long)_offset
;
2468 struct kvm_vcpu
*vcpu
;
2472 raw_spin_lock(&kvm_lock
);
2473 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2474 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2475 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2477 raw_spin_unlock(&kvm_lock
);
2481 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2483 static const struct file_operations
*stat_fops
[] = {
2484 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2485 [KVM_STAT_VM
] = &vm_stat_fops
,
2488 static void kvm_init_debug(void)
2490 struct kvm_stats_debugfs_item
*p
;
2492 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2493 for (p
= debugfs_entries
; p
->name
; ++p
)
2494 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2495 (void *)(long)p
->offset
,
2496 stat_fops
[p
->kind
]);
2499 static void kvm_exit_debug(void)
2501 struct kvm_stats_debugfs_item
*p
;
2503 for (p
= debugfs_entries
; p
->name
; ++p
)
2504 debugfs_remove(p
->dentry
);
2505 debugfs_remove(kvm_debugfs_dir
);
2508 static int kvm_suspend(void)
2510 if (kvm_usage_count
)
2511 hardware_disable_nolock(NULL
);
2515 static void kvm_resume(void)
2517 if (kvm_usage_count
) {
2518 WARN_ON(raw_spin_is_locked(&kvm_lock
));
2519 hardware_enable_nolock(NULL
);
2523 static struct syscore_ops kvm_syscore_ops
= {
2524 .suspend
= kvm_suspend
,
2525 .resume
= kvm_resume
,
2528 struct page
*bad_page
;
2532 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2534 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2537 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2539 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2541 kvm_arch_vcpu_load(vcpu
, cpu
);
2544 static void kvm_sched_out(struct preempt_notifier
*pn
,
2545 struct task_struct
*next
)
2547 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2549 kvm_arch_vcpu_put(vcpu
);
2552 int kvm_init(void *opaque
, unsigned vcpu_size
, unsigned vcpu_align
,
2553 struct module
*module
)
2558 r
= kvm_arch_init(opaque
);
2562 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2564 if (bad_page
== NULL
) {
2569 bad_pfn
= page_to_pfn(bad_page
);
2571 hwpoison_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2573 if (hwpoison_page
== NULL
) {
2578 hwpoison_pfn
= page_to_pfn(hwpoison_page
);
2580 fault_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2582 if (fault_page
== NULL
) {
2587 fault_pfn
= page_to_pfn(fault_page
);
2589 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2594 r
= kvm_arch_hardware_setup();
2598 for_each_online_cpu(cpu
) {
2599 smp_call_function_single(cpu
,
2600 kvm_arch_check_processor_compat
,
2606 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2609 register_reboot_notifier(&kvm_reboot_notifier
);
2611 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2613 vcpu_align
= __alignof__(struct kvm_vcpu
);
2614 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
, vcpu_align
,
2616 if (!kvm_vcpu_cache
) {
2621 r
= kvm_async_pf_init();
2625 kvm_chardev_ops
.owner
= module
;
2626 kvm_vm_fops
.owner
= module
;
2627 kvm_vcpu_fops
.owner
= module
;
2629 r
= misc_register(&kvm_dev
);
2631 printk(KERN_ERR
"kvm: misc device register failed\n");
2635 register_syscore_ops(&kvm_syscore_ops
);
2637 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2638 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2645 kvm_async_pf_deinit();
2647 kmem_cache_destroy(kvm_vcpu_cache
);
2649 unregister_reboot_notifier(&kvm_reboot_notifier
);
2650 unregister_cpu_notifier(&kvm_cpu_notifier
);
2653 kvm_arch_hardware_unsetup();
2655 free_cpumask_var(cpus_hardware_enabled
);
2658 __free_page(fault_page
);
2660 __free_page(hwpoison_page
);
2661 __free_page(bad_page
);
2667 EXPORT_SYMBOL_GPL(kvm_init
);
2672 misc_deregister(&kvm_dev
);
2673 kmem_cache_destroy(kvm_vcpu_cache
);
2674 kvm_async_pf_deinit();
2675 unregister_syscore_ops(&kvm_syscore_ops
);
2676 unregister_reboot_notifier(&kvm_reboot_notifier
);
2677 unregister_cpu_notifier(&kvm_cpu_notifier
);
2678 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2679 kvm_arch_hardware_unsetup();
2681 free_cpumask_var(cpus_hardware_enabled
);
2682 __free_page(hwpoison_page
);
2683 __free_page(bad_page
);
2685 EXPORT_SYMBOL_GPL(kvm_exit
);