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 struct page
*fault_page
;
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
);
835 * If the new memory slot is created, we need to clear all
838 if (npages
&& old
.base_gfn
!= mem
->guest_phys_addr
>> PAGE_SHIFT
)
839 kvm_arch_flush_shadow(kvm
);
841 kvm_free_physmem_slot(&old
, &new);
847 kvm_free_physmem_slot(&new, &old
);
852 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
854 int kvm_set_memory_region(struct kvm
*kvm
,
855 struct kvm_userspace_memory_region
*mem
,
860 mutex_lock(&kvm
->slots_lock
);
861 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
862 mutex_unlock(&kvm
->slots_lock
);
865 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
867 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
869 kvm_userspace_memory_region
*mem
,
872 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
874 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
877 int kvm_get_dirty_log(struct kvm
*kvm
,
878 struct kvm_dirty_log
*log
, int *is_dirty
)
880 struct kvm_memory_slot
*memslot
;
883 unsigned long any
= 0;
886 if (log
->slot
>= KVM_MEMORY_SLOTS
)
889 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
891 if (!memslot
->dirty_bitmap
)
894 n
= kvm_dirty_bitmap_bytes(memslot
);
896 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
897 any
= memslot
->dirty_bitmap
[i
];
900 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
911 void kvm_disable_largepages(void)
913 largepages_enabled
= false;
915 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
917 int is_error_page(struct page
*page
)
919 return page
== bad_page
|| page
== hwpoison_page
|| page
== fault_page
;
921 EXPORT_SYMBOL_GPL(is_error_page
);
923 int is_error_pfn(pfn_t pfn
)
925 return pfn
== bad_pfn
|| pfn
== hwpoison_pfn
|| pfn
== fault_pfn
;
927 EXPORT_SYMBOL_GPL(is_error_pfn
);
929 int is_hwpoison_pfn(pfn_t pfn
)
931 return pfn
== hwpoison_pfn
;
933 EXPORT_SYMBOL_GPL(is_hwpoison_pfn
);
935 int is_fault_pfn(pfn_t pfn
)
937 return pfn
== fault_pfn
;
939 EXPORT_SYMBOL_GPL(is_fault_pfn
);
941 int is_noslot_pfn(pfn_t pfn
)
943 return pfn
== bad_pfn
;
945 EXPORT_SYMBOL_GPL(is_noslot_pfn
);
947 int is_invalid_pfn(pfn_t pfn
)
949 return pfn
== hwpoison_pfn
|| pfn
== fault_pfn
;
951 EXPORT_SYMBOL_GPL(is_invalid_pfn
);
953 static inline unsigned long bad_hva(void)
958 int kvm_is_error_hva(unsigned long addr
)
960 return addr
== bad_hva();
962 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
964 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm_memslots
*slots
,
969 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
970 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
972 if (gfn
>= memslot
->base_gfn
973 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
979 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
981 return __gfn_to_memslot(kvm_memslots(kvm
), gfn
);
983 EXPORT_SYMBOL_GPL(gfn_to_memslot
);
985 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
988 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
990 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
991 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
993 if (memslot
->flags
& KVM_MEMSLOT_INVALID
)
996 if (gfn
>= memslot
->base_gfn
997 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1002 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
1004 unsigned long kvm_host_page_size(struct kvm
*kvm
, gfn_t gfn
)
1006 struct vm_area_struct
*vma
;
1007 unsigned long addr
, size
;
1011 addr
= gfn_to_hva(kvm
, gfn
);
1012 if (kvm_is_error_hva(addr
))
1015 down_read(¤t
->mm
->mmap_sem
);
1016 vma
= find_vma(current
->mm
, addr
);
1020 size
= vma_kernel_pagesize(vma
);
1023 up_read(¤t
->mm
->mmap_sem
);
1028 static unsigned long gfn_to_hva_many(struct kvm_memory_slot
*slot
, gfn_t gfn
,
1031 if (!slot
|| slot
->flags
& KVM_MEMSLOT_INVALID
)
1035 *nr_pages
= slot
->npages
- (gfn
- slot
->base_gfn
);
1037 return gfn_to_hva_memslot(slot
, gfn
);
1040 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1042 return gfn_to_hva_many(gfn_to_memslot(kvm
, gfn
), gfn
, NULL
);
1044 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1046 static pfn_t
get_fault_pfn(void)
1048 get_page(fault_page
);
1052 int get_user_page_nowait(struct task_struct
*tsk
, struct mm_struct
*mm
,
1053 unsigned long start
, int write
, struct page
**page
)
1055 int flags
= FOLL_TOUCH
| FOLL_NOWAIT
| FOLL_HWPOISON
| FOLL_GET
;
1058 flags
|= FOLL_WRITE
;
1060 return __get_user_pages(tsk
, mm
, start
, 1, flags
, page
, NULL
, NULL
);
1063 static inline int check_user_page_hwpoison(unsigned long addr
)
1065 int rc
, flags
= FOLL_TOUCH
| FOLL_HWPOISON
| FOLL_WRITE
;
1067 rc
= __get_user_pages(current
, current
->mm
, addr
, 1,
1068 flags
, NULL
, NULL
, NULL
);
1069 return rc
== -EHWPOISON
;
1072 static pfn_t
hva_to_pfn(struct kvm
*kvm
, unsigned long addr
, bool atomic
,
1073 bool *async
, bool write_fault
, bool *writable
)
1075 struct page
*page
[1];
1079 /* we can do it either atomically or asynchronously, not both */
1080 BUG_ON(atomic
&& async
);
1082 BUG_ON(!write_fault
&& !writable
);
1087 if (atomic
|| async
)
1088 npages
= __get_user_pages_fast(addr
, 1, 1, page
);
1090 if (unlikely(npages
!= 1) && !atomic
) {
1094 *writable
= write_fault
;
1097 down_read(¤t
->mm
->mmap_sem
);
1098 npages
= get_user_page_nowait(current
, current
->mm
,
1099 addr
, write_fault
, page
);
1100 up_read(¤t
->mm
->mmap_sem
);
1102 npages
= get_user_pages_fast(addr
, 1, write_fault
,
1105 /* map read fault as writable if possible */
1106 if (unlikely(!write_fault
) && npages
== 1) {
1107 struct page
*wpage
[1];
1109 npages
= __get_user_pages_fast(addr
, 1, 1, wpage
);
1119 if (unlikely(npages
!= 1)) {
1120 struct vm_area_struct
*vma
;
1123 return get_fault_pfn();
1125 down_read(¤t
->mm
->mmap_sem
);
1126 if (npages
== -EHWPOISON
||
1127 (!async
&& check_user_page_hwpoison(addr
))) {
1128 up_read(¤t
->mm
->mmap_sem
);
1129 get_page(hwpoison_page
);
1130 return page_to_pfn(hwpoison_page
);
1133 vma
= find_vma_intersection(current
->mm
, addr
, addr
+1);
1136 pfn
= get_fault_pfn();
1137 else if ((vma
->vm_flags
& VM_PFNMAP
)) {
1138 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) +
1140 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1142 if (async
&& (vma
->vm_flags
& VM_WRITE
))
1144 pfn
= get_fault_pfn();
1146 up_read(¤t
->mm
->mmap_sem
);
1148 pfn
= page_to_pfn(page
[0]);
1153 pfn_t
hva_to_pfn_atomic(struct kvm
*kvm
, unsigned long addr
)
1155 return hva_to_pfn(kvm
, addr
, true, NULL
, true, NULL
);
1157 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic
);
1159 static pfn_t
__gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
, bool atomic
, bool *async
,
1160 bool write_fault
, bool *writable
)
1167 addr
= gfn_to_hva(kvm
, gfn
);
1168 if (kvm_is_error_hva(addr
)) {
1170 return page_to_pfn(bad_page
);
1173 return hva_to_pfn(kvm
, addr
, atomic
, async
, write_fault
, writable
);
1176 pfn_t
gfn_to_pfn_atomic(struct kvm
*kvm
, gfn_t gfn
)
1178 return __gfn_to_pfn(kvm
, gfn
, true, NULL
, true, NULL
);
1180 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic
);
1182 pfn_t
gfn_to_pfn_async(struct kvm
*kvm
, gfn_t gfn
, bool *async
,
1183 bool write_fault
, bool *writable
)
1185 return __gfn_to_pfn(kvm
, gfn
, false, async
, write_fault
, writable
);
1187 EXPORT_SYMBOL_GPL(gfn_to_pfn_async
);
1189 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1191 return __gfn_to_pfn(kvm
, gfn
, false, NULL
, true, NULL
);
1193 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1195 pfn_t
gfn_to_pfn_prot(struct kvm
*kvm
, gfn_t gfn
, bool write_fault
,
1198 return __gfn_to_pfn(kvm
, gfn
, false, NULL
, write_fault
, writable
);
1200 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot
);
1202 pfn_t
gfn_to_pfn_memslot(struct kvm
*kvm
,
1203 struct kvm_memory_slot
*slot
, gfn_t gfn
)
1205 unsigned long addr
= gfn_to_hva_memslot(slot
, gfn
);
1206 return hva_to_pfn(kvm
, addr
, false, NULL
, true, NULL
);
1209 int gfn_to_page_many_atomic(struct kvm
*kvm
, gfn_t gfn
, struct page
**pages
,
1215 addr
= gfn_to_hva_many(gfn_to_memslot(kvm
, gfn
), gfn
, &entry
);
1216 if (kvm_is_error_hva(addr
))
1219 if (entry
< nr_pages
)
1222 return __get_user_pages_fast(addr
, nr_pages
, 1, pages
);
1224 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic
);
1226 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1230 pfn
= gfn_to_pfn(kvm
, gfn
);
1231 if (!kvm_is_mmio_pfn(pfn
))
1232 return pfn_to_page(pfn
);
1234 WARN_ON(kvm_is_mmio_pfn(pfn
));
1240 EXPORT_SYMBOL_GPL(gfn_to_page
);
1242 void kvm_release_page_clean(struct page
*page
)
1244 kvm_release_pfn_clean(page_to_pfn(page
));
1246 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1248 void kvm_release_pfn_clean(pfn_t pfn
)
1250 if (!kvm_is_mmio_pfn(pfn
))
1251 put_page(pfn_to_page(pfn
));
1253 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1255 void kvm_release_page_dirty(struct page
*page
)
1257 kvm_release_pfn_dirty(page_to_pfn(page
));
1259 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1261 void kvm_release_pfn_dirty(pfn_t pfn
)
1263 kvm_set_pfn_dirty(pfn
);
1264 kvm_release_pfn_clean(pfn
);
1266 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1268 void kvm_set_page_dirty(struct page
*page
)
1270 kvm_set_pfn_dirty(page_to_pfn(page
));
1272 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1274 void kvm_set_pfn_dirty(pfn_t pfn
)
1276 if (!kvm_is_mmio_pfn(pfn
)) {
1277 struct page
*page
= pfn_to_page(pfn
);
1278 if (!PageReserved(page
))
1282 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1284 void kvm_set_pfn_accessed(pfn_t pfn
)
1286 if (!kvm_is_mmio_pfn(pfn
))
1287 mark_page_accessed(pfn_to_page(pfn
));
1289 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1291 void kvm_get_pfn(pfn_t pfn
)
1293 if (!kvm_is_mmio_pfn(pfn
))
1294 get_page(pfn_to_page(pfn
));
1296 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1298 static int next_segment(unsigned long len
, int offset
)
1300 if (len
> PAGE_SIZE
- offset
)
1301 return PAGE_SIZE
- offset
;
1306 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1312 addr
= gfn_to_hva(kvm
, gfn
);
1313 if (kvm_is_error_hva(addr
))
1315 r
= __copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1320 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1322 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1324 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1326 int offset
= offset_in_page(gpa
);
1329 while ((seg
= next_segment(len
, offset
)) != 0) {
1330 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1340 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1342 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1347 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1348 int offset
= offset_in_page(gpa
);
1350 addr
= gfn_to_hva(kvm
, gfn
);
1351 if (kvm_is_error_hva(addr
))
1353 pagefault_disable();
1354 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1360 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1362 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1363 int offset
, int len
)
1368 addr
= gfn_to_hva(kvm
, gfn
);
1369 if (kvm_is_error_hva(addr
))
1371 r
= __copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1374 mark_page_dirty(kvm
, gfn
);
1377 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1379 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1382 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1384 int offset
= offset_in_page(gpa
);
1387 while ((seg
= next_segment(len
, offset
)) != 0) {
1388 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1399 int kvm_gfn_to_hva_cache_init(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1402 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1403 int offset
= offset_in_page(gpa
);
1404 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1407 ghc
->generation
= slots
->generation
;
1408 ghc
->memslot
= __gfn_to_memslot(slots
, gfn
);
1409 ghc
->hva
= gfn_to_hva_many(ghc
->memslot
, gfn
, NULL
);
1410 if (!kvm_is_error_hva(ghc
->hva
))
1417 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init
);
1419 int kvm_write_guest_cached(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1420 void *data
, unsigned long len
)
1422 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1425 if (slots
->generation
!= ghc
->generation
)
1426 kvm_gfn_to_hva_cache_init(kvm
, ghc
, ghc
->gpa
);
1428 if (kvm_is_error_hva(ghc
->hva
))
1431 r
= __copy_to_user((void __user
*)ghc
->hva
, data
, len
);
1434 mark_page_dirty_in_slot(kvm
, ghc
->memslot
, ghc
->gpa
>> PAGE_SHIFT
);
1438 EXPORT_SYMBOL_GPL(kvm_write_guest_cached
);
1440 int kvm_read_guest_cached(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1441 void *data
, unsigned long len
)
1443 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1446 if (slots
->generation
!= ghc
->generation
)
1447 kvm_gfn_to_hva_cache_init(kvm
, ghc
, ghc
->gpa
);
1449 if (kvm_is_error_hva(ghc
->hva
))
1452 r
= __copy_from_user(data
, (void __user
*)ghc
->hva
, len
);
1458 EXPORT_SYMBOL_GPL(kvm_read_guest_cached
);
1460 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1462 return kvm_write_guest_page(kvm
, gfn
, (const void *) empty_zero_page
,
1465 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1467 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1469 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1471 int offset
= offset_in_page(gpa
);
1474 while ((seg
= next_segment(len
, offset
)) != 0) {
1475 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1484 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1486 void mark_page_dirty_in_slot(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
1489 if (memslot
&& memslot
->dirty_bitmap
) {
1490 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1492 __set_bit_le(rel_gfn
, memslot
->dirty_bitmap
);
1496 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1498 struct kvm_memory_slot
*memslot
;
1500 memslot
= gfn_to_memslot(kvm
, gfn
);
1501 mark_page_dirty_in_slot(kvm
, memslot
, gfn
);
1505 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1507 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1512 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1514 if (kvm_arch_vcpu_runnable(vcpu
)) {
1515 kvm_make_request(KVM_REQ_UNHALT
, vcpu
);
1518 if (kvm_cpu_has_pending_timer(vcpu
))
1520 if (signal_pending(current
))
1526 finish_wait(&vcpu
->wq
, &wait
);
1529 void kvm_resched(struct kvm_vcpu
*vcpu
)
1531 if (!need_resched())
1535 EXPORT_SYMBOL_GPL(kvm_resched
);
1537 void kvm_vcpu_on_spin(struct kvm_vcpu
*me
)
1539 struct kvm
*kvm
= me
->kvm
;
1540 struct kvm_vcpu
*vcpu
;
1541 int last_boosted_vcpu
= me
->kvm
->last_boosted_vcpu
;
1547 * We boost the priority of a VCPU that is runnable but not
1548 * currently running, because it got preempted by something
1549 * else and called schedule in __vcpu_run. Hopefully that
1550 * VCPU is holding the lock that we need and will release it.
1551 * We approximate round-robin by starting at the last boosted VCPU.
1553 for (pass
= 0; pass
< 2 && !yielded
; pass
++) {
1554 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
1555 struct task_struct
*task
= NULL
;
1557 if (!pass
&& i
< last_boosted_vcpu
) {
1558 i
= last_boosted_vcpu
;
1560 } else if (pass
&& i
> last_boosted_vcpu
)
1564 if (waitqueue_active(&vcpu
->wq
))
1567 pid
= rcu_dereference(vcpu
->pid
);
1569 task
= get_pid_task(vcpu
->pid
, PIDTYPE_PID
);
1573 if (task
->flags
& PF_VCPU
) {
1574 put_task_struct(task
);
1577 if (yield_to(task
, 1)) {
1578 put_task_struct(task
);
1579 kvm
->last_boosted_vcpu
= i
;
1583 put_task_struct(task
);
1587 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1589 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1591 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1594 if (vmf
->pgoff
== 0)
1595 page
= virt_to_page(vcpu
->run
);
1597 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1598 page
= virt_to_page(vcpu
->arch
.pio_data
);
1600 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1601 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1602 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1605 return VM_FAULT_SIGBUS
;
1611 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1612 .fault
= kvm_vcpu_fault
,
1615 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1617 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1621 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1623 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1625 kvm_put_kvm(vcpu
->kvm
);
1629 static struct file_operations kvm_vcpu_fops
= {
1630 .release
= kvm_vcpu_release
,
1631 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1632 #ifdef CONFIG_COMPAT
1633 .compat_ioctl
= kvm_vcpu_compat_ioctl
,
1635 .mmap
= kvm_vcpu_mmap
,
1636 .llseek
= noop_llseek
,
1640 * Allocates an inode for the vcpu.
1642 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1644 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, O_RDWR
);
1648 * Creates some virtual cpus. Good luck creating more than one.
1650 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1653 struct kvm_vcpu
*vcpu
, *v
;
1655 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1657 return PTR_ERR(vcpu
);
1659 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1661 r
= kvm_arch_vcpu_setup(vcpu
);
1665 mutex_lock(&kvm
->lock
);
1666 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1668 goto unlock_vcpu_destroy
;
1671 kvm_for_each_vcpu(r
, v
, kvm
)
1672 if (v
->vcpu_id
== id
) {
1674 goto unlock_vcpu_destroy
;
1677 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1679 /* Now it's all set up, let userspace reach it */
1681 r
= create_vcpu_fd(vcpu
);
1684 goto unlock_vcpu_destroy
;
1687 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1689 atomic_inc(&kvm
->online_vcpus
);
1691 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1692 if (kvm
->bsp_vcpu_id
== id
)
1693 kvm
->bsp_vcpu
= vcpu
;
1695 mutex_unlock(&kvm
->lock
);
1698 unlock_vcpu_destroy
:
1699 mutex_unlock(&kvm
->lock
);
1701 kvm_arch_vcpu_destroy(vcpu
);
1705 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1708 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1709 vcpu
->sigset_active
= 1;
1710 vcpu
->sigset
= *sigset
;
1712 vcpu
->sigset_active
= 0;
1716 static long kvm_vcpu_ioctl(struct file
*filp
,
1717 unsigned int ioctl
, unsigned long arg
)
1719 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1720 void __user
*argp
= (void __user
*)arg
;
1722 struct kvm_fpu
*fpu
= NULL
;
1723 struct kvm_sregs
*kvm_sregs
= NULL
;
1725 if (vcpu
->kvm
->mm
!= current
->mm
)
1728 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1730 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1731 * so vcpu_load() would break it.
1733 if (ioctl
== KVM_S390_INTERRUPT
|| ioctl
== KVM_INTERRUPT
)
1734 return kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1744 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1745 trace_kvm_userspace_exit(vcpu
->run
->exit_reason
, r
);
1747 case KVM_GET_REGS
: {
1748 struct kvm_regs
*kvm_regs
;
1751 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1754 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1758 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1765 case KVM_SET_REGS
: {
1766 struct kvm_regs
*kvm_regs
;
1769 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1773 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1775 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1783 case KVM_GET_SREGS
: {
1784 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1788 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1792 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1797 case KVM_SET_SREGS
: {
1798 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1803 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1805 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1811 case KVM_GET_MP_STATE
: {
1812 struct kvm_mp_state mp_state
;
1814 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1818 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1823 case KVM_SET_MP_STATE
: {
1824 struct kvm_mp_state mp_state
;
1827 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1829 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1835 case KVM_TRANSLATE
: {
1836 struct kvm_translation tr
;
1839 if (copy_from_user(&tr
, argp
, sizeof tr
))
1841 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1845 if (copy_to_user(argp
, &tr
, sizeof tr
))
1850 case KVM_SET_GUEST_DEBUG
: {
1851 struct kvm_guest_debug dbg
;
1854 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1856 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1862 case KVM_SET_SIGNAL_MASK
: {
1863 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1864 struct kvm_signal_mask kvm_sigmask
;
1865 sigset_t sigset
, *p
;
1870 if (copy_from_user(&kvm_sigmask
, argp
,
1871 sizeof kvm_sigmask
))
1874 if (kvm_sigmask
.len
!= sizeof sigset
)
1877 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1882 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, p
);
1886 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1890 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1894 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1900 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1905 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1907 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1914 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1923 #ifdef CONFIG_COMPAT
1924 static long kvm_vcpu_compat_ioctl(struct file
*filp
,
1925 unsigned int ioctl
, unsigned long arg
)
1927 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1928 void __user
*argp
= compat_ptr(arg
);
1931 if (vcpu
->kvm
->mm
!= current
->mm
)
1935 case KVM_SET_SIGNAL_MASK
: {
1936 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1937 struct kvm_signal_mask kvm_sigmask
;
1938 compat_sigset_t csigset
;
1943 if (copy_from_user(&kvm_sigmask
, argp
,
1944 sizeof kvm_sigmask
))
1947 if (kvm_sigmask
.len
!= sizeof csigset
)
1950 if (copy_from_user(&csigset
, sigmask_arg
->sigset
,
1954 sigset_from_compat(&sigset
, &csigset
);
1955 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1959 r
= kvm_vcpu_ioctl(filp
, ioctl
, arg
);
1967 static long kvm_vm_ioctl(struct file
*filp
,
1968 unsigned int ioctl
, unsigned long arg
)
1970 struct kvm
*kvm
= filp
->private_data
;
1971 void __user
*argp
= (void __user
*)arg
;
1974 if (kvm
->mm
!= current
->mm
)
1977 case KVM_CREATE_VCPU
:
1978 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1982 case KVM_SET_USER_MEMORY_REGION
: {
1983 struct kvm_userspace_memory_region kvm_userspace_mem
;
1986 if (copy_from_user(&kvm_userspace_mem
, argp
,
1987 sizeof kvm_userspace_mem
))
1990 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1995 case KVM_GET_DIRTY_LOG
: {
1996 struct kvm_dirty_log log
;
1999 if (copy_from_user(&log
, argp
, sizeof log
))
2001 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2006 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2007 case KVM_REGISTER_COALESCED_MMIO
: {
2008 struct kvm_coalesced_mmio_zone zone
;
2010 if (copy_from_user(&zone
, argp
, sizeof zone
))
2012 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
2018 case KVM_UNREGISTER_COALESCED_MMIO
: {
2019 struct kvm_coalesced_mmio_zone zone
;
2021 if (copy_from_user(&zone
, argp
, sizeof zone
))
2023 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
2031 struct kvm_irqfd data
;
2034 if (copy_from_user(&data
, argp
, sizeof data
))
2036 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
2039 case KVM_IOEVENTFD
: {
2040 struct kvm_ioeventfd data
;
2043 if (copy_from_user(&data
, argp
, sizeof data
))
2045 r
= kvm_ioeventfd(kvm
, &data
);
2048 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2049 case KVM_SET_BOOT_CPU_ID
:
2051 mutex_lock(&kvm
->lock
);
2052 if (atomic_read(&kvm
->online_vcpus
) != 0)
2055 kvm
->bsp_vcpu_id
= arg
;
2056 mutex_unlock(&kvm
->lock
);
2060 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
2062 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
2068 #ifdef CONFIG_COMPAT
2069 struct compat_kvm_dirty_log
{
2073 compat_uptr_t dirty_bitmap
; /* one bit per page */
2078 static long kvm_vm_compat_ioctl(struct file
*filp
,
2079 unsigned int ioctl
, unsigned long arg
)
2081 struct kvm
*kvm
= filp
->private_data
;
2084 if (kvm
->mm
!= current
->mm
)
2087 case KVM_GET_DIRTY_LOG
: {
2088 struct compat_kvm_dirty_log compat_log
;
2089 struct kvm_dirty_log log
;
2092 if (copy_from_user(&compat_log
, (void __user
*)arg
,
2093 sizeof(compat_log
)))
2095 log
.slot
= compat_log
.slot
;
2096 log
.padding1
= compat_log
.padding1
;
2097 log
.padding2
= compat_log
.padding2
;
2098 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
2100 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2106 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
2114 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2116 struct page
*page
[1];
2119 gfn_t gfn
= vmf
->pgoff
;
2120 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2122 addr
= gfn_to_hva(kvm
, gfn
);
2123 if (kvm_is_error_hva(addr
))
2124 return VM_FAULT_SIGBUS
;
2126 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
2128 if (unlikely(npages
!= 1))
2129 return VM_FAULT_SIGBUS
;
2131 vmf
->page
= page
[0];
2135 static const struct vm_operations_struct kvm_vm_vm_ops
= {
2136 .fault
= kvm_vm_fault
,
2139 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2141 vma
->vm_ops
= &kvm_vm_vm_ops
;
2145 static struct file_operations kvm_vm_fops
= {
2146 .release
= kvm_vm_release
,
2147 .unlocked_ioctl
= kvm_vm_ioctl
,
2148 #ifdef CONFIG_COMPAT
2149 .compat_ioctl
= kvm_vm_compat_ioctl
,
2151 .mmap
= kvm_vm_mmap
,
2152 .llseek
= noop_llseek
,
2155 static int kvm_dev_ioctl_create_vm(void)
2160 kvm
= kvm_create_vm();
2162 return PTR_ERR(kvm
);
2163 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2164 r
= kvm_coalesced_mmio_init(kvm
);
2170 r
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, O_RDWR
);
2177 static long kvm_dev_ioctl_check_extension_generic(long arg
)
2180 case KVM_CAP_USER_MEMORY
:
2181 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
2182 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
2183 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2184 case KVM_CAP_SET_BOOT_CPU_ID
:
2186 case KVM_CAP_INTERNAL_ERROR_DATA
:
2188 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2189 case KVM_CAP_IRQ_ROUTING
:
2190 return KVM_MAX_IRQ_ROUTES
;
2195 return kvm_dev_ioctl_check_extension(arg
);
2198 static long kvm_dev_ioctl(struct file
*filp
,
2199 unsigned int ioctl
, unsigned long arg
)
2204 case KVM_GET_API_VERSION
:
2208 r
= KVM_API_VERSION
;
2214 r
= kvm_dev_ioctl_create_vm();
2216 case KVM_CHECK_EXTENSION
:
2217 r
= kvm_dev_ioctl_check_extension_generic(arg
);
2219 case KVM_GET_VCPU_MMAP_SIZE
:
2223 r
= PAGE_SIZE
; /* struct kvm_run */
2225 r
+= PAGE_SIZE
; /* pio data page */
2227 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2228 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
2231 case KVM_TRACE_ENABLE
:
2232 case KVM_TRACE_PAUSE
:
2233 case KVM_TRACE_DISABLE
:
2237 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
2243 static struct file_operations kvm_chardev_ops
= {
2244 .unlocked_ioctl
= kvm_dev_ioctl
,
2245 .compat_ioctl
= kvm_dev_ioctl
,
2246 .llseek
= noop_llseek
,
2249 static struct miscdevice kvm_dev
= {
2255 static void hardware_enable_nolock(void *junk
)
2257 int cpu
= raw_smp_processor_id();
2260 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2263 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
2265 r
= kvm_arch_hardware_enable(NULL
);
2268 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2269 atomic_inc(&hardware_enable_failed
);
2270 printk(KERN_INFO
"kvm: enabling virtualization on "
2271 "CPU%d failed\n", cpu
);
2275 static void hardware_enable(void *junk
)
2277 raw_spin_lock(&kvm_lock
);
2278 hardware_enable_nolock(junk
);
2279 raw_spin_unlock(&kvm_lock
);
2282 static void hardware_disable_nolock(void *junk
)
2284 int cpu
= raw_smp_processor_id();
2286 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2288 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2289 kvm_arch_hardware_disable(NULL
);
2292 static void hardware_disable(void *junk
)
2294 raw_spin_lock(&kvm_lock
);
2295 hardware_disable_nolock(junk
);
2296 raw_spin_unlock(&kvm_lock
);
2299 static void hardware_disable_all_nolock(void)
2301 BUG_ON(!kvm_usage_count
);
2304 if (!kvm_usage_count
)
2305 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2308 static void hardware_disable_all(void)
2310 raw_spin_lock(&kvm_lock
);
2311 hardware_disable_all_nolock();
2312 raw_spin_unlock(&kvm_lock
);
2315 static int hardware_enable_all(void)
2319 raw_spin_lock(&kvm_lock
);
2322 if (kvm_usage_count
== 1) {
2323 atomic_set(&hardware_enable_failed
, 0);
2324 on_each_cpu(hardware_enable_nolock
, NULL
, 1);
2326 if (atomic_read(&hardware_enable_failed
)) {
2327 hardware_disable_all_nolock();
2332 raw_spin_unlock(&kvm_lock
);
2337 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2342 if (!kvm_usage_count
)
2345 val
&= ~CPU_TASKS_FROZEN
;
2348 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2350 hardware_disable(NULL
);
2353 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2355 hardware_enable(NULL
);
2362 asmlinkage
void kvm_spurious_fault(void)
2364 /* Fault while not rebooting. We want the trace. */
2367 EXPORT_SYMBOL_GPL(kvm_spurious_fault
);
2369 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2373 * Some (well, at least mine) BIOSes hang on reboot if
2376 * And Intel TXT required VMX off for all cpu when system shutdown.
2378 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2379 kvm_rebooting
= true;
2380 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2384 static struct notifier_block kvm_reboot_notifier
= {
2385 .notifier_call
= kvm_reboot
,
2389 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2393 for (i
= 0; i
< bus
->dev_count
; i
++) {
2394 struct kvm_io_device
*pos
= bus
->devs
[i
];
2396 kvm_iodevice_destructor(pos
);
2401 /* kvm_io_bus_write - called under kvm->slots_lock */
2402 int kvm_io_bus_write(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2403 int len
, const void *val
)
2406 struct kvm_io_bus
*bus
;
2408 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2409 for (i
= 0; i
< bus
->dev_count
; i
++)
2410 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
2415 /* kvm_io_bus_read - called under kvm->slots_lock */
2416 int kvm_io_bus_read(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2420 struct kvm_io_bus
*bus
;
2422 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2423 for (i
= 0; i
< bus
->dev_count
; i
++)
2424 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
2429 /* Caller must hold slots_lock. */
2430 int kvm_io_bus_register_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2431 struct kvm_io_device
*dev
)
2433 struct kvm_io_bus
*new_bus
, *bus
;
2435 bus
= kvm
->buses
[bus_idx
];
2436 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2439 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2442 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2443 new_bus
->devs
[new_bus
->dev_count
++] = dev
;
2444 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2445 synchronize_srcu_expedited(&kvm
->srcu
);
2451 /* Caller must hold slots_lock. */
2452 int kvm_io_bus_unregister_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2453 struct kvm_io_device
*dev
)
2456 struct kvm_io_bus
*new_bus
, *bus
;
2458 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2462 bus
= kvm
->buses
[bus_idx
];
2463 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2466 for (i
= 0; i
< new_bus
->dev_count
; i
++)
2467 if (new_bus
->devs
[i
] == dev
) {
2469 new_bus
->devs
[i
] = new_bus
->devs
[--new_bus
->dev_count
];
2478 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2479 synchronize_srcu_expedited(&kvm
->srcu
);
2484 static struct notifier_block kvm_cpu_notifier
= {
2485 .notifier_call
= kvm_cpu_hotplug
,
2488 static int vm_stat_get(void *_offset
, u64
*val
)
2490 unsigned offset
= (long)_offset
;
2494 raw_spin_lock(&kvm_lock
);
2495 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2496 *val
+= *(u32
*)((void *)kvm
+ offset
);
2497 raw_spin_unlock(&kvm_lock
);
2501 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2503 static int vcpu_stat_get(void *_offset
, u64
*val
)
2505 unsigned offset
= (long)_offset
;
2507 struct kvm_vcpu
*vcpu
;
2511 raw_spin_lock(&kvm_lock
);
2512 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2513 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2514 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2516 raw_spin_unlock(&kvm_lock
);
2520 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2522 static const struct file_operations
*stat_fops
[] = {
2523 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2524 [KVM_STAT_VM
] = &vm_stat_fops
,
2527 static void kvm_init_debug(void)
2529 struct kvm_stats_debugfs_item
*p
;
2531 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2532 for (p
= debugfs_entries
; p
->name
; ++p
)
2533 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2534 (void *)(long)p
->offset
,
2535 stat_fops
[p
->kind
]);
2538 static void kvm_exit_debug(void)
2540 struct kvm_stats_debugfs_item
*p
;
2542 for (p
= debugfs_entries
; p
->name
; ++p
)
2543 debugfs_remove(p
->dentry
);
2544 debugfs_remove(kvm_debugfs_dir
);
2547 static int kvm_suspend(void)
2549 if (kvm_usage_count
)
2550 hardware_disable_nolock(NULL
);
2554 static void kvm_resume(void)
2556 if (kvm_usage_count
) {
2557 WARN_ON(raw_spin_is_locked(&kvm_lock
));
2558 hardware_enable_nolock(NULL
);
2562 static struct syscore_ops kvm_syscore_ops
= {
2563 .suspend
= kvm_suspend
,
2564 .resume
= kvm_resume
,
2567 struct page
*bad_page
;
2571 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2573 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2576 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2578 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2580 kvm_arch_vcpu_load(vcpu
, cpu
);
2583 static void kvm_sched_out(struct preempt_notifier
*pn
,
2584 struct task_struct
*next
)
2586 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2588 kvm_arch_vcpu_put(vcpu
);
2591 int kvm_init(void *opaque
, unsigned vcpu_size
, unsigned vcpu_align
,
2592 struct module
*module
)
2597 r
= kvm_arch_init(opaque
);
2601 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2603 if (bad_page
== NULL
) {
2608 bad_pfn
= page_to_pfn(bad_page
);
2610 hwpoison_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2612 if (hwpoison_page
== NULL
) {
2617 hwpoison_pfn
= page_to_pfn(hwpoison_page
);
2619 fault_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2621 if (fault_page
== NULL
) {
2626 fault_pfn
= page_to_pfn(fault_page
);
2628 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2633 r
= kvm_arch_hardware_setup();
2637 for_each_online_cpu(cpu
) {
2638 smp_call_function_single(cpu
,
2639 kvm_arch_check_processor_compat
,
2645 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2648 register_reboot_notifier(&kvm_reboot_notifier
);
2650 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2652 vcpu_align
= __alignof__(struct kvm_vcpu
);
2653 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
, vcpu_align
,
2655 if (!kvm_vcpu_cache
) {
2660 r
= kvm_async_pf_init();
2664 kvm_chardev_ops
.owner
= module
;
2665 kvm_vm_fops
.owner
= module
;
2666 kvm_vcpu_fops
.owner
= module
;
2668 r
= misc_register(&kvm_dev
);
2670 printk(KERN_ERR
"kvm: misc device register failed\n");
2674 register_syscore_ops(&kvm_syscore_ops
);
2676 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2677 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2684 kvm_async_pf_deinit();
2686 kmem_cache_destroy(kvm_vcpu_cache
);
2688 unregister_reboot_notifier(&kvm_reboot_notifier
);
2689 unregister_cpu_notifier(&kvm_cpu_notifier
);
2692 kvm_arch_hardware_unsetup();
2694 free_cpumask_var(cpus_hardware_enabled
);
2697 __free_page(fault_page
);
2699 __free_page(hwpoison_page
);
2700 __free_page(bad_page
);
2706 EXPORT_SYMBOL_GPL(kvm_init
);
2711 misc_deregister(&kvm_dev
);
2712 kmem_cache_destroy(kvm_vcpu_cache
);
2713 kvm_async_pf_deinit();
2714 unregister_syscore_ops(&kvm_syscore_ops
);
2715 unregister_reboot_notifier(&kvm_reboot_notifier
);
2716 unregister_cpu_notifier(&kvm_cpu_notifier
);
2717 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2718 kvm_arch_hardware_unsetup();
2720 free_cpumask_var(cpus_hardware_enabled
);
2721 __free_page(hwpoison_page
);
2722 __free_page(bad_page
);
2724 EXPORT_SYMBOL_GPL(kvm_exit
);