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>
50 #include <linux/sort.h>
51 #include <linux/bsearch.h>
53 #include <asm/processor.h>
55 #include <asm/uaccess.h>
56 #include <asm/pgtable.h>
58 #include "coalesced_mmio.h"
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/kvm.h>
64 MODULE_AUTHOR("Qumranet");
65 MODULE_LICENSE("GPL");
70 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
73 DEFINE_RAW_SPINLOCK(kvm_lock
);
76 static cpumask_var_t cpus_hardware_enabled
;
77 static int kvm_usage_count
= 0;
78 static atomic_t hardware_enable_failed
;
80 struct kmem_cache
*kvm_vcpu_cache
;
81 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
83 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
85 struct dentry
*kvm_debugfs_dir
;
87 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
90 static long kvm_vcpu_compat_ioctl(struct file
*file
, unsigned int ioctl
,
93 static int hardware_enable_all(void);
94 static void hardware_disable_all(void);
96 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
);
99 EXPORT_SYMBOL_GPL(kvm_rebooting
);
101 static bool largepages_enabled
= true;
103 static struct page
*hwpoison_page
;
104 static pfn_t hwpoison_pfn
;
106 struct page
*fault_page
;
109 inline int kvm_is_mmio_pfn(pfn_t pfn
)
111 if (pfn_valid(pfn
)) {
113 struct page
*tail
= pfn_to_page(pfn
);
114 struct page
*head
= compound_trans_head(tail
);
115 reserved
= PageReserved(head
);
118 * "head" is not a dangling pointer
119 * (compound_trans_head takes care of that)
120 * but the hugepage may have been splitted
121 * from under us (and we may not hold a
122 * reference count on the head page so it can
123 * be reused before we run PageReferenced), so
124 * we've to check PageTail before returning
131 return PageReserved(tail
);
138 * Switches to specified vcpu, until a matching vcpu_put()
140 void vcpu_load(struct kvm_vcpu
*vcpu
)
144 mutex_lock(&vcpu
->mutex
);
145 if (unlikely(vcpu
->pid
!= current
->pids
[PIDTYPE_PID
].pid
)) {
146 /* The thread running this VCPU changed. */
147 struct pid
*oldpid
= vcpu
->pid
;
148 struct pid
*newpid
= get_task_pid(current
, PIDTYPE_PID
);
149 rcu_assign_pointer(vcpu
->pid
, newpid
);
154 preempt_notifier_register(&vcpu
->preempt_notifier
);
155 kvm_arch_vcpu_load(vcpu
, cpu
);
159 void vcpu_put(struct kvm_vcpu
*vcpu
)
162 kvm_arch_vcpu_put(vcpu
);
163 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
165 mutex_unlock(&vcpu
->mutex
);
168 static void ack_flush(void *_completed
)
172 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
177 struct kvm_vcpu
*vcpu
;
179 zalloc_cpumask_var(&cpus
, GFP_ATOMIC
);
182 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
183 kvm_make_request(req
, vcpu
);
186 /* Set ->requests bit before we read ->mode */
189 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
&&
190 kvm_vcpu_exiting_guest_mode(vcpu
) != OUTSIDE_GUEST_MODE
)
191 cpumask_set_cpu(cpu
, cpus
);
193 if (unlikely(cpus
== NULL
))
194 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
195 else if (!cpumask_empty(cpus
))
196 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
200 free_cpumask_var(cpus
);
204 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
206 int dirty_count
= kvm
->tlbs_dirty
;
209 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
210 ++kvm
->stat
.remote_tlb_flush
;
211 cmpxchg(&kvm
->tlbs_dirty
, dirty_count
, 0);
214 void kvm_reload_remote_mmus(struct kvm
*kvm
)
216 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
219 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
224 mutex_init(&vcpu
->mutex
);
229 init_waitqueue_head(&vcpu
->wq
);
230 kvm_async_pf_vcpu_init(vcpu
);
232 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
237 vcpu
->run
= page_address(page
);
239 r
= kvm_arch_vcpu_init(vcpu
);
245 free_page((unsigned long)vcpu
->run
);
249 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
251 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
254 kvm_arch_vcpu_uninit(vcpu
);
255 free_page((unsigned long)vcpu
->run
);
257 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
259 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
260 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
262 return container_of(mn
, struct kvm
, mmu_notifier
);
265 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
266 struct mm_struct
*mm
,
267 unsigned long address
)
269 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
270 int need_tlb_flush
, idx
;
273 * When ->invalidate_page runs, the linux pte has been zapped
274 * already but the page is still allocated until
275 * ->invalidate_page returns. So if we increase the sequence
276 * here the kvm page fault will notice if the spte can't be
277 * established because the page is going to be freed. If
278 * instead the kvm page fault establishes the spte before
279 * ->invalidate_page runs, kvm_unmap_hva will release it
282 * The sequence increase only need to be seen at spin_unlock
283 * time, and not at spin_lock time.
285 * Increasing the sequence after the spin_unlock would be
286 * unsafe because the kvm page fault could then establish the
287 * pte after kvm_unmap_hva returned, without noticing the page
288 * is going to be freed.
290 idx
= srcu_read_lock(&kvm
->srcu
);
291 spin_lock(&kvm
->mmu_lock
);
293 kvm
->mmu_notifier_seq
++;
294 need_tlb_flush
= kvm_unmap_hva(kvm
, address
) | kvm
->tlbs_dirty
;
295 /* we've to flush the tlb before the pages can be freed */
297 kvm_flush_remote_tlbs(kvm
);
299 spin_unlock(&kvm
->mmu_lock
);
300 srcu_read_unlock(&kvm
->srcu
, idx
);
303 static void kvm_mmu_notifier_change_pte(struct mmu_notifier
*mn
,
304 struct mm_struct
*mm
,
305 unsigned long address
,
308 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
311 idx
= srcu_read_lock(&kvm
->srcu
);
312 spin_lock(&kvm
->mmu_lock
);
313 kvm
->mmu_notifier_seq
++;
314 kvm_set_spte_hva(kvm
, address
, pte
);
315 spin_unlock(&kvm
->mmu_lock
);
316 srcu_read_unlock(&kvm
->srcu
, idx
);
319 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
320 struct mm_struct
*mm
,
324 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
325 int need_tlb_flush
= 0, idx
;
327 idx
= srcu_read_lock(&kvm
->srcu
);
328 spin_lock(&kvm
->mmu_lock
);
330 * The count increase must become visible at unlock time as no
331 * spte can be established without taking the mmu_lock and
332 * count is also read inside the mmu_lock critical section.
334 kvm
->mmu_notifier_count
++;
335 for (; start
< end
; start
+= PAGE_SIZE
)
336 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
337 need_tlb_flush
|= kvm
->tlbs_dirty
;
338 /* we've to flush the tlb before the pages can be freed */
340 kvm_flush_remote_tlbs(kvm
);
342 spin_unlock(&kvm
->mmu_lock
);
343 srcu_read_unlock(&kvm
->srcu
, idx
);
346 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
347 struct mm_struct
*mm
,
351 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
353 spin_lock(&kvm
->mmu_lock
);
355 * This sequence increase will notify the kvm page fault that
356 * the page that is going to be mapped in the spte could have
359 kvm
->mmu_notifier_seq
++;
361 * The above sequence increase must be visible before the
362 * below count decrease but both values are read by the kvm
363 * page fault under mmu_lock spinlock so we don't need to add
364 * a smb_wmb() here in between the two.
366 kvm
->mmu_notifier_count
--;
367 spin_unlock(&kvm
->mmu_lock
);
369 BUG_ON(kvm
->mmu_notifier_count
< 0);
372 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
373 struct mm_struct
*mm
,
374 unsigned long address
)
376 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
379 idx
= srcu_read_lock(&kvm
->srcu
);
380 spin_lock(&kvm
->mmu_lock
);
382 young
= kvm_age_hva(kvm
, address
);
384 kvm_flush_remote_tlbs(kvm
);
386 spin_unlock(&kvm
->mmu_lock
);
387 srcu_read_unlock(&kvm
->srcu
, idx
);
392 static int kvm_mmu_notifier_test_young(struct mmu_notifier
*mn
,
393 struct mm_struct
*mm
,
394 unsigned long address
)
396 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
399 idx
= srcu_read_lock(&kvm
->srcu
);
400 spin_lock(&kvm
->mmu_lock
);
401 young
= kvm_test_age_hva(kvm
, address
);
402 spin_unlock(&kvm
->mmu_lock
);
403 srcu_read_unlock(&kvm
->srcu
, idx
);
408 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
409 struct mm_struct
*mm
)
411 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
414 idx
= srcu_read_lock(&kvm
->srcu
);
415 kvm_arch_flush_shadow(kvm
);
416 srcu_read_unlock(&kvm
->srcu
, idx
);
419 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
420 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
421 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
422 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
423 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
424 .test_young
= kvm_mmu_notifier_test_young
,
425 .change_pte
= kvm_mmu_notifier_change_pte
,
426 .release
= kvm_mmu_notifier_release
,
429 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
431 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
432 return mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
435 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
437 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
442 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
444 static void kvm_init_memslots_id(struct kvm
*kvm
)
447 struct kvm_memslots
*slots
= kvm
->memslots
;
449 for (i
= 0; i
< KVM_MEM_SLOTS_NUM
; i
++)
450 slots
->id_to_index
[i
] = slots
->memslots
[i
].id
= i
;
453 static struct kvm
*kvm_create_vm(void)
456 struct kvm
*kvm
= kvm_arch_alloc_vm();
459 return ERR_PTR(-ENOMEM
);
461 r
= kvm_arch_init_vm(kvm
);
463 goto out_err_nodisable
;
465 r
= hardware_enable_all();
467 goto out_err_nodisable
;
469 #ifdef CONFIG_HAVE_KVM_IRQCHIP
470 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
471 INIT_HLIST_HEAD(&kvm
->irq_ack_notifier_list
);
475 kvm
->memslots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
478 kvm_init_memslots_id(kvm
);
479 if (init_srcu_struct(&kvm
->srcu
))
481 for (i
= 0; i
< KVM_NR_BUSES
; i
++) {
482 kvm
->buses
[i
] = kzalloc(sizeof(struct kvm_io_bus
),
488 spin_lock_init(&kvm
->mmu_lock
);
489 kvm
->mm
= current
->mm
;
490 atomic_inc(&kvm
->mm
->mm_count
);
491 kvm_eventfd_init(kvm
);
492 mutex_init(&kvm
->lock
);
493 mutex_init(&kvm
->irq_lock
);
494 mutex_init(&kvm
->slots_lock
);
495 atomic_set(&kvm
->users_count
, 1);
497 r
= kvm_init_mmu_notifier(kvm
);
501 raw_spin_lock(&kvm_lock
);
502 list_add(&kvm
->vm_list
, &vm_list
);
503 raw_spin_unlock(&kvm_lock
);
508 cleanup_srcu_struct(&kvm
->srcu
);
510 hardware_disable_all();
512 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
513 kfree(kvm
->buses
[i
]);
514 kfree(kvm
->memslots
);
515 kvm_arch_free_vm(kvm
);
519 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot
*memslot
)
521 if (!memslot
->dirty_bitmap
)
524 if (2 * kvm_dirty_bitmap_bytes(memslot
) > PAGE_SIZE
)
525 vfree(memslot
->dirty_bitmap_head
);
527 kfree(memslot
->dirty_bitmap_head
);
529 memslot
->dirty_bitmap
= NULL
;
530 memslot
->dirty_bitmap_head
= NULL
;
534 * Free any memory in @free but not in @dont.
536 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
537 struct kvm_memory_slot
*dont
)
541 if (!dont
|| free
->rmap
!= dont
->rmap
)
544 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
545 kvm_destroy_dirty_bitmap(free
);
548 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
549 if (!dont
|| free
->lpage_info
[i
] != dont
->lpage_info
[i
]) {
550 vfree(free
->lpage_info
[i
]);
551 free
->lpage_info
[i
] = NULL
;
559 void kvm_free_physmem(struct kvm
*kvm
)
561 struct kvm_memslots
*slots
= kvm
->memslots
;
562 struct kvm_memory_slot
*memslot
;
564 kvm_for_each_memslot(memslot
, slots
)
565 kvm_free_physmem_slot(memslot
, NULL
);
567 kfree(kvm
->memslots
);
570 static void kvm_destroy_vm(struct kvm
*kvm
)
573 struct mm_struct
*mm
= kvm
->mm
;
575 kvm_arch_sync_events(kvm
);
576 raw_spin_lock(&kvm_lock
);
577 list_del(&kvm
->vm_list
);
578 raw_spin_unlock(&kvm_lock
);
579 kvm_free_irq_routing(kvm
);
580 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
581 kvm_io_bus_destroy(kvm
->buses
[i
]);
582 kvm_coalesced_mmio_free(kvm
);
583 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
584 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
586 kvm_arch_flush_shadow(kvm
);
588 kvm_arch_destroy_vm(kvm
);
589 kvm_free_physmem(kvm
);
590 cleanup_srcu_struct(&kvm
->srcu
);
591 kvm_arch_free_vm(kvm
);
592 hardware_disable_all();
596 void kvm_get_kvm(struct kvm
*kvm
)
598 atomic_inc(&kvm
->users_count
);
600 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
602 void kvm_put_kvm(struct kvm
*kvm
)
604 if (atomic_dec_and_test(&kvm
->users_count
))
607 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
610 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
612 struct kvm
*kvm
= filp
->private_data
;
614 kvm_irqfd_release(kvm
);
622 * Allocation size is twice as large as the actual dirty bitmap size.
623 * This makes it possible to do double buffering: see x86's
624 * kvm_vm_ioctl_get_dirty_log().
626 static int kvm_create_dirty_bitmap(struct kvm_memory_slot
*memslot
)
628 unsigned long dirty_bytes
= 2 * kvm_dirty_bitmap_bytes(memslot
);
630 if (dirty_bytes
> PAGE_SIZE
)
631 memslot
->dirty_bitmap
= vzalloc(dirty_bytes
);
633 memslot
->dirty_bitmap
= kzalloc(dirty_bytes
, GFP_KERNEL
);
635 if (!memslot
->dirty_bitmap
)
638 memslot
->dirty_bitmap_head
= memslot
->dirty_bitmap
;
639 memslot
->nr_dirty_pages
= 0;
642 #endif /* !CONFIG_S390 */
644 static struct kvm_memory_slot
*
645 search_memslots(struct kvm_memslots
*slots
, gfn_t gfn
)
647 struct kvm_memory_slot
*memslot
;
649 kvm_for_each_memslot(memslot
, slots
)
650 if (gfn
>= memslot
->base_gfn
&&
651 gfn
< memslot
->base_gfn
+ memslot
->npages
)
657 static int cmp_memslot(const void *slot1
, const void *slot2
)
659 struct kvm_memory_slot
*s1
, *s2
;
661 s1
= (struct kvm_memory_slot
*)slot1
;
662 s2
= (struct kvm_memory_slot
*)slot2
;
664 if (s1
->npages
< s2
->npages
)
666 if (s1
->npages
> s2
->npages
)
673 * Sort the memslots base on its size, so the larger slots
674 * will get better fit.
676 static void sort_memslots(struct kvm_memslots
*slots
)
680 sort(slots
->memslots
, KVM_MEM_SLOTS_NUM
,
681 sizeof(struct kvm_memory_slot
), cmp_memslot
, NULL
);
683 for (i
= 0; i
< KVM_MEM_SLOTS_NUM
; i
++)
684 slots
->id_to_index
[slots
->memslots
[i
].id
] = i
;
687 void update_memslots(struct kvm_memslots
*slots
, struct kvm_memory_slot
*new)
691 struct kvm_memory_slot
*old
= id_to_memslot(slots
, id
);
692 unsigned long npages
= old
->npages
;
695 if (new->npages
!= npages
)
696 sort_memslots(slots
);
703 * Allocate some memory and give it an address in the guest physical address
706 * Discontiguous memory is allowed, mostly for framebuffers.
708 * Must be called holding mmap_sem for write.
710 int __kvm_set_memory_region(struct kvm
*kvm
,
711 struct kvm_userspace_memory_region
*mem
,
716 unsigned long npages
;
718 struct kvm_memory_slot
*memslot
;
719 struct kvm_memory_slot old
, new;
720 struct kvm_memslots
*slots
, *old_memslots
;
723 /* General sanity checks */
724 if (mem
->memory_size
& (PAGE_SIZE
- 1))
726 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
728 /* We can read the guest memory with __xxx_user() later on. */
730 ((mem
->userspace_addr
& (PAGE_SIZE
- 1)) ||
731 !access_ok(VERIFY_WRITE
,
732 (void __user
*)(unsigned long)mem
->userspace_addr
,
735 if (mem
->slot
>= KVM_MEM_SLOTS_NUM
)
737 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
740 memslot
= id_to_memslot(kvm
->memslots
, mem
->slot
);
741 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
742 npages
= mem
->memory_size
>> PAGE_SHIFT
;
745 if (npages
> KVM_MEM_MAX_NR_PAGES
)
749 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
751 new = old
= *memslot
;
754 new.base_gfn
= base_gfn
;
756 new.flags
= mem
->flags
;
758 /* Disallow changing a memory slot's size. */
760 if (npages
&& old
.npages
&& npages
!= old
.npages
)
763 /* Check for overlaps */
765 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
766 struct kvm_memory_slot
*s
= &kvm
->memslots
->memslots
[i
];
768 if (s
== memslot
|| !s
->npages
)
770 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
771 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
775 /* Free page dirty bitmap if unneeded */
776 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
777 new.dirty_bitmap
= NULL
;
781 /* Allocate if a slot is being created */
783 if (npages
&& !new.rmap
) {
784 new.rmap
= vzalloc(npages
* sizeof(*new.rmap
));
789 new.user_alloc
= user_alloc
;
790 new.userspace_addr
= mem
->userspace_addr
;
795 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
801 /* Avoid unused variable warning if no large pages */
804 if (new.lpage_info
[i
])
807 lpages
= 1 + ((base_gfn
+ npages
- 1)
808 >> KVM_HPAGE_GFN_SHIFT(level
));
809 lpages
-= base_gfn
>> KVM_HPAGE_GFN_SHIFT(level
);
811 new.lpage_info
[i
] = vzalloc(lpages
* sizeof(*new.lpage_info
[i
]));
813 if (!new.lpage_info
[i
])
816 if (base_gfn
& (KVM_PAGES_PER_HPAGE(level
) - 1))
817 new.lpage_info
[i
][0].write_count
= 1;
818 if ((base_gfn
+npages
) & (KVM_PAGES_PER_HPAGE(level
) - 1))
819 new.lpage_info
[i
][lpages
- 1].write_count
= 1;
820 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
822 * If the gfn and userspace address are not aligned wrt each
823 * other, or if explicitly asked to, disable large page
824 * support for this slot
826 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE(level
) - 1) ||
828 for (j
= 0; j
< lpages
; ++j
)
829 new.lpage_info
[i
][j
].write_count
= 1;
834 /* Allocate page dirty bitmap if needed */
835 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
836 if (kvm_create_dirty_bitmap(&new) < 0)
838 /* destroy any largepage mappings for dirty tracking */
840 #else /* not defined CONFIG_S390 */
841 new.user_alloc
= user_alloc
;
843 new.userspace_addr
= mem
->userspace_addr
;
844 #endif /* not defined CONFIG_S390 */
847 struct kvm_memory_slot
*slot
;
850 slots
= kmemdup(kvm
->memslots
, sizeof(struct kvm_memslots
),
854 slot
= id_to_memslot(slots
, mem
->slot
);
855 slot
->flags
|= KVM_MEMSLOT_INVALID
;
857 update_memslots(slots
, NULL
);
859 old_memslots
= kvm
->memslots
;
860 rcu_assign_pointer(kvm
->memslots
, slots
);
861 synchronize_srcu_expedited(&kvm
->srcu
);
862 /* From this point no new shadow pages pointing to a deleted
863 * memslot will be created.
865 * validation of sp->gfn happens in:
866 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
867 * - kvm_is_visible_gfn (mmu_check_roots)
869 kvm_arch_flush_shadow(kvm
);
873 r
= kvm_arch_prepare_memory_region(kvm
, &new, old
, mem
, user_alloc
);
877 /* map/unmap the pages in iommu page table */
879 r
= kvm_iommu_map_pages(kvm
, &new);
883 kvm_iommu_unmap_pages(kvm
, &old
);
886 slots
= kmemdup(kvm
->memslots
, sizeof(struct kvm_memslots
),
891 /* actual memory is freed via old in kvm_free_physmem_slot below */
894 new.dirty_bitmap
= NULL
;
895 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
)
896 new.lpage_info
[i
] = NULL
;
899 update_memslots(slots
, &new);
900 old_memslots
= kvm
->memslots
;
901 rcu_assign_pointer(kvm
->memslots
, slots
);
902 synchronize_srcu_expedited(&kvm
->srcu
);
904 kvm_arch_commit_memory_region(kvm
, mem
, old
, user_alloc
);
907 * If the new memory slot is created, we need to clear all
910 if (npages
&& old
.base_gfn
!= mem
->guest_phys_addr
>> PAGE_SHIFT
)
911 kvm_arch_flush_shadow(kvm
);
913 kvm_free_physmem_slot(&old
, &new);
919 kvm_free_physmem_slot(&new, &old
);
924 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
926 int kvm_set_memory_region(struct kvm
*kvm
,
927 struct kvm_userspace_memory_region
*mem
,
932 mutex_lock(&kvm
->slots_lock
);
933 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
934 mutex_unlock(&kvm
->slots_lock
);
937 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
939 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
941 kvm_userspace_memory_region
*mem
,
944 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
946 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
949 int kvm_get_dirty_log(struct kvm
*kvm
,
950 struct kvm_dirty_log
*log
, int *is_dirty
)
952 struct kvm_memory_slot
*memslot
;
955 unsigned long any
= 0;
958 if (log
->slot
>= KVM_MEMORY_SLOTS
)
961 memslot
= id_to_memslot(kvm
->memslots
, log
->slot
);
963 if (!memslot
->dirty_bitmap
)
966 n
= kvm_dirty_bitmap_bytes(memslot
);
968 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
969 any
= memslot
->dirty_bitmap
[i
];
972 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
983 void kvm_disable_largepages(void)
985 largepages_enabled
= false;
987 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
989 int is_error_page(struct page
*page
)
991 return page
== bad_page
|| page
== hwpoison_page
|| page
== fault_page
;
993 EXPORT_SYMBOL_GPL(is_error_page
);
995 int is_error_pfn(pfn_t pfn
)
997 return pfn
== bad_pfn
|| pfn
== hwpoison_pfn
|| pfn
== fault_pfn
;
999 EXPORT_SYMBOL_GPL(is_error_pfn
);
1001 int is_hwpoison_pfn(pfn_t pfn
)
1003 return pfn
== hwpoison_pfn
;
1005 EXPORT_SYMBOL_GPL(is_hwpoison_pfn
);
1007 int is_fault_pfn(pfn_t pfn
)
1009 return pfn
== fault_pfn
;
1011 EXPORT_SYMBOL_GPL(is_fault_pfn
);
1013 int is_noslot_pfn(pfn_t pfn
)
1015 return pfn
== bad_pfn
;
1017 EXPORT_SYMBOL_GPL(is_noslot_pfn
);
1019 int is_invalid_pfn(pfn_t pfn
)
1021 return pfn
== hwpoison_pfn
|| pfn
== fault_pfn
;
1023 EXPORT_SYMBOL_GPL(is_invalid_pfn
);
1025 static inline unsigned long bad_hva(void)
1030 int kvm_is_error_hva(unsigned long addr
)
1032 return addr
== bad_hva();
1034 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
1036 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm_memslots
*slots
,
1039 return search_memslots(slots
, gfn
);
1042 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1044 return __gfn_to_memslot(kvm_memslots(kvm
), gfn
);
1046 EXPORT_SYMBOL_GPL(gfn_to_memslot
);
1048 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
1050 struct kvm_memory_slot
*memslot
= gfn_to_memslot(kvm
, gfn
);
1052 if (!memslot
|| memslot
->id
>= KVM_MEMORY_SLOTS
||
1053 memslot
->flags
& KVM_MEMSLOT_INVALID
)
1058 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
1060 unsigned long kvm_host_page_size(struct kvm
*kvm
, gfn_t gfn
)
1062 struct vm_area_struct
*vma
;
1063 unsigned long addr
, size
;
1067 addr
= gfn_to_hva(kvm
, gfn
);
1068 if (kvm_is_error_hva(addr
))
1071 down_read(¤t
->mm
->mmap_sem
);
1072 vma
= find_vma(current
->mm
, addr
);
1076 size
= vma_kernel_pagesize(vma
);
1079 up_read(¤t
->mm
->mmap_sem
);
1084 static unsigned long gfn_to_hva_many(struct kvm_memory_slot
*slot
, gfn_t gfn
,
1087 if (!slot
|| slot
->flags
& KVM_MEMSLOT_INVALID
)
1091 *nr_pages
= slot
->npages
- (gfn
- slot
->base_gfn
);
1093 return gfn_to_hva_memslot(slot
, gfn
);
1096 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
1098 return gfn_to_hva_many(gfn_to_memslot(kvm
, gfn
), gfn
, NULL
);
1100 EXPORT_SYMBOL_GPL(gfn_to_hva
);
1102 static pfn_t
get_fault_pfn(void)
1104 get_page(fault_page
);
1108 int get_user_page_nowait(struct task_struct
*tsk
, struct mm_struct
*mm
,
1109 unsigned long start
, int write
, struct page
**page
)
1111 int flags
= FOLL_TOUCH
| FOLL_NOWAIT
| FOLL_HWPOISON
| FOLL_GET
;
1114 flags
|= FOLL_WRITE
;
1116 return __get_user_pages(tsk
, mm
, start
, 1, flags
, page
, NULL
, NULL
);
1119 static inline int check_user_page_hwpoison(unsigned long addr
)
1121 int rc
, flags
= FOLL_TOUCH
| FOLL_HWPOISON
| FOLL_WRITE
;
1123 rc
= __get_user_pages(current
, current
->mm
, addr
, 1,
1124 flags
, NULL
, NULL
, NULL
);
1125 return rc
== -EHWPOISON
;
1128 static pfn_t
hva_to_pfn(struct kvm
*kvm
, unsigned long addr
, bool atomic
,
1129 bool *async
, bool write_fault
, bool *writable
)
1131 struct page
*page
[1];
1135 /* we can do it either atomically or asynchronously, not both */
1136 BUG_ON(atomic
&& async
);
1138 BUG_ON(!write_fault
&& !writable
);
1143 if (atomic
|| async
)
1144 npages
= __get_user_pages_fast(addr
, 1, 1, page
);
1146 if (unlikely(npages
!= 1) && !atomic
) {
1150 *writable
= write_fault
;
1153 down_read(¤t
->mm
->mmap_sem
);
1154 npages
= get_user_page_nowait(current
, current
->mm
,
1155 addr
, write_fault
, page
);
1156 up_read(¤t
->mm
->mmap_sem
);
1158 npages
= get_user_pages_fast(addr
, 1, write_fault
,
1161 /* map read fault as writable if possible */
1162 if (unlikely(!write_fault
) && npages
== 1) {
1163 struct page
*wpage
[1];
1165 npages
= __get_user_pages_fast(addr
, 1, 1, wpage
);
1175 if (unlikely(npages
!= 1)) {
1176 struct vm_area_struct
*vma
;
1179 return get_fault_pfn();
1181 down_read(¤t
->mm
->mmap_sem
);
1182 if (npages
== -EHWPOISON
||
1183 (!async
&& check_user_page_hwpoison(addr
))) {
1184 up_read(¤t
->mm
->mmap_sem
);
1185 get_page(hwpoison_page
);
1186 return page_to_pfn(hwpoison_page
);
1189 vma
= find_vma_intersection(current
->mm
, addr
, addr
+1);
1192 pfn
= get_fault_pfn();
1193 else if ((vma
->vm_flags
& VM_PFNMAP
)) {
1194 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) +
1196 BUG_ON(!kvm_is_mmio_pfn(pfn
));
1198 if (async
&& (vma
->vm_flags
& VM_WRITE
))
1200 pfn
= get_fault_pfn();
1202 up_read(¤t
->mm
->mmap_sem
);
1204 pfn
= page_to_pfn(page
[0]);
1209 pfn_t
hva_to_pfn_atomic(struct kvm
*kvm
, unsigned long addr
)
1211 return hva_to_pfn(kvm
, addr
, true, NULL
, true, NULL
);
1213 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic
);
1215 static pfn_t
__gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
, bool atomic
, bool *async
,
1216 bool write_fault
, bool *writable
)
1223 addr
= gfn_to_hva(kvm
, gfn
);
1224 if (kvm_is_error_hva(addr
)) {
1226 return page_to_pfn(bad_page
);
1229 return hva_to_pfn(kvm
, addr
, atomic
, async
, write_fault
, writable
);
1232 pfn_t
gfn_to_pfn_atomic(struct kvm
*kvm
, gfn_t gfn
)
1234 return __gfn_to_pfn(kvm
, gfn
, true, NULL
, true, NULL
);
1236 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic
);
1238 pfn_t
gfn_to_pfn_async(struct kvm
*kvm
, gfn_t gfn
, bool *async
,
1239 bool write_fault
, bool *writable
)
1241 return __gfn_to_pfn(kvm
, gfn
, false, async
, write_fault
, writable
);
1243 EXPORT_SYMBOL_GPL(gfn_to_pfn_async
);
1245 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1247 return __gfn_to_pfn(kvm
, gfn
, false, NULL
, true, NULL
);
1249 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1251 pfn_t
gfn_to_pfn_prot(struct kvm
*kvm
, gfn_t gfn
, bool write_fault
,
1254 return __gfn_to_pfn(kvm
, gfn
, false, NULL
, write_fault
, writable
);
1256 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot
);
1258 pfn_t
gfn_to_pfn_memslot(struct kvm
*kvm
,
1259 struct kvm_memory_slot
*slot
, gfn_t gfn
)
1261 unsigned long addr
= gfn_to_hva_memslot(slot
, gfn
);
1262 return hva_to_pfn(kvm
, addr
, false, NULL
, true, NULL
);
1265 int gfn_to_page_many_atomic(struct kvm
*kvm
, gfn_t gfn
, struct page
**pages
,
1271 addr
= gfn_to_hva_many(gfn_to_memslot(kvm
, gfn
), gfn
, &entry
);
1272 if (kvm_is_error_hva(addr
))
1275 if (entry
< nr_pages
)
1278 return __get_user_pages_fast(addr
, nr_pages
, 1, pages
);
1280 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic
);
1282 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1286 pfn
= gfn_to_pfn(kvm
, gfn
);
1287 if (!kvm_is_mmio_pfn(pfn
))
1288 return pfn_to_page(pfn
);
1290 WARN_ON(kvm_is_mmio_pfn(pfn
));
1296 EXPORT_SYMBOL_GPL(gfn_to_page
);
1298 void kvm_release_page_clean(struct page
*page
)
1300 kvm_release_pfn_clean(page_to_pfn(page
));
1302 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1304 void kvm_release_pfn_clean(pfn_t pfn
)
1306 if (!kvm_is_mmio_pfn(pfn
))
1307 put_page(pfn_to_page(pfn
));
1309 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1311 void kvm_release_page_dirty(struct page
*page
)
1313 kvm_release_pfn_dirty(page_to_pfn(page
));
1315 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1317 void kvm_release_pfn_dirty(pfn_t pfn
)
1319 kvm_set_pfn_dirty(pfn
);
1320 kvm_release_pfn_clean(pfn
);
1322 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1324 void kvm_set_page_dirty(struct page
*page
)
1326 kvm_set_pfn_dirty(page_to_pfn(page
));
1328 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1330 void kvm_set_pfn_dirty(pfn_t pfn
)
1332 if (!kvm_is_mmio_pfn(pfn
)) {
1333 struct page
*page
= pfn_to_page(pfn
);
1334 if (!PageReserved(page
))
1338 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1340 void kvm_set_pfn_accessed(pfn_t pfn
)
1342 if (!kvm_is_mmio_pfn(pfn
))
1343 mark_page_accessed(pfn_to_page(pfn
));
1345 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1347 void kvm_get_pfn(pfn_t pfn
)
1349 if (!kvm_is_mmio_pfn(pfn
))
1350 get_page(pfn_to_page(pfn
));
1352 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1354 static int next_segment(unsigned long len
, int offset
)
1356 if (len
> PAGE_SIZE
- offset
)
1357 return PAGE_SIZE
- offset
;
1362 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1368 addr
= gfn_to_hva(kvm
, gfn
);
1369 if (kvm_is_error_hva(addr
))
1371 r
= __copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1376 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1378 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1380 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1382 int offset
= offset_in_page(gpa
);
1385 while ((seg
= next_segment(len
, offset
)) != 0) {
1386 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1396 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1398 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1403 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1404 int offset
= offset_in_page(gpa
);
1406 addr
= gfn_to_hva(kvm
, gfn
);
1407 if (kvm_is_error_hva(addr
))
1409 pagefault_disable();
1410 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1416 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1418 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1419 int offset
, int len
)
1424 addr
= gfn_to_hva(kvm
, gfn
);
1425 if (kvm_is_error_hva(addr
))
1427 r
= __copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1430 mark_page_dirty(kvm
, gfn
);
1433 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1435 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1438 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1440 int offset
= offset_in_page(gpa
);
1443 while ((seg
= next_segment(len
, offset
)) != 0) {
1444 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1455 int kvm_gfn_to_hva_cache_init(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1458 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1459 int offset
= offset_in_page(gpa
);
1460 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1463 ghc
->generation
= slots
->generation
;
1464 ghc
->memslot
= __gfn_to_memslot(slots
, gfn
);
1465 ghc
->hva
= gfn_to_hva_many(ghc
->memslot
, gfn
, NULL
);
1466 if (!kvm_is_error_hva(ghc
->hva
))
1473 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init
);
1475 int kvm_write_guest_cached(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1476 void *data
, unsigned long len
)
1478 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1481 if (slots
->generation
!= ghc
->generation
)
1482 kvm_gfn_to_hva_cache_init(kvm
, ghc
, ghc
->gpa
);
1484 if (kvm_is_error_hva(ghc
->hva
))
1487 r
= __copy_to_user((void __user
*)ghc
->hva
, data
, len
);
1490 mark_page_dirty_in_slot(kvm
, ghc
->memslot
, ghc
->gpa
>> PAGE_SHIFT
);
1494 EXPORT_SYMBOL_GPL(kvm_write_guest_cached
);
1496 int kvm_read_guest_cached(struct kvm
*kvm
, struct gfn_to_hva_cache
*ghc
,
1497 void *data
, unsigned long len
)
1499 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1502 if (slots
->generation
!= ghc
->generation
)
1503 kvm_gfn_to_hva_cache_init(kvm
, ghc
, ghc
->gpa
);
1505 if (kvm_is_error_hva(ghc
->hva
))
1508 r
= __copy_from_user(data
, (void __user
*)ghc
->hva
, len
);
1514 EXPORT_SYMBOL_GPL(kvm_read_guest_cached
);
1516 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1518 return kvm_write_guest_page(kvm
, gfn
, (const void *) empty_zero_page
,
1521 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1523 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1525 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1527 int offset
= offset_in_page(gpa
);
1530 while ((seg
= next_segment(len
, offset
)) != 0) {
1531 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1540 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1542 void mark_page_dirty_in_slot(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
1545 if (memslot
&& memslot
->dirty_bitmap
) {
1546 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1548 if (!test_and_set_bit_le(rel_gfn
, memslot
->dirty_bitmap
))
1549 memslot
->nr_dirty_pages
++;
1553 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1555 struct kvm_memory_slot
*memslot
;
1557 memslot
= gfn_to_memslot(kvm
, gfn
);
1558 mark_page_dirty_in_slot(kvm
, memslot
, gfn
);
1562 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1564 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1569 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1571 if (kvm_arch_vcpu_runnable(vcpu
)) {
1572 kvm_make_request(KVM_REQ_UNHALT
, vcpu
);
1575 if (kvm_cpu_has_pending_timer(vcpu
))
1577 if (signal_pending(current
))
1583 finish_wait(&vcpu
->wq
, &wait
);
1586 void kvm_resched(struct kvm_vcpu
*vcpu
)
1588 if (!need_resched())
1592 EXPORT_SYMBOL_GPL(kvm_resched
);
1594 void kvm_vcpu_on_spin(struct kvm_vcpu
*me
)
1596 struct kvm
*kvm
= me
->kvm
;
1597 struct kvm_vcpu
*vcpu
;
1598 int last_boosted_vcpu
= me
->kvm
->last_boosted_vcpu
;
1604 * We boost the priority of a VCPU that is runnable but not
1605 * currently running, because it got preempted by something
1606 * else and called schedule in __vcpu_run. Hopefully that
1607 * VCPU is holding the lock that we need and will release it.
1608 * We approximate round-robin by starting at the last boosted VCPU.
1610 for (pass
= 0; pass
< 2 && !yielded
; pass
++) {
1611 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
1612 struct task_struct
*task
= NULL
;
1614 if (!pass
&& i
< last_boosted_vcpu
) {
1615 i
= last_boosted_vcpu
;
1617 } else if (pass
&& i
> last_boosted_vcpu
)
1621 if (waitqueue_active(&vcpu
->wq
))
1624 pid
= rcu_dereference(vcpu
->pid
);
1626 task
= get_pid_task(vcpu
->pid
, PIDTYPE_PID
);
1630 if (task
->flags
& PF_VCPU
) {
1631 put_task_struct(task
);
1634 if (yield_to(task
, 1)) {
1635 put_task_struct(task
);
1636 kvm
->last_boosted_vcpu
= i
;
1640 put_task_struct(task
);
1644 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1646 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1648 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1651 if (vmf
->pgoff
== 0)
1652 page
= virt_to_page(vcpu
->run
);
1654 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1655 page
= virt_to_page(vcpu
->arch
.pio_data
);
1657 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1658 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1659 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1662 return VM_FAULT_SIGBUS
;
1668 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1669 .fault
= kvm_vcpu_fault
,
1672 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1674 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1678 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1680 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1682 kvm_put_kvm(vcpu
->kvm
);
1686 static struct file_operations kvm_vcpu_fops
= {
1687 .release
= kvm_vcpu_release
,
1688 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1689 #ifdef CONFIG_COMPAT
1690 .compat_ioctl
= kvm_vcpu_compat_ioctl
,
1692 .mmap
= kvm_vcpu_mmap
,
1693 .llseek
= noop_llseek
,
1697 * Allocates an inode for the vcpu.
1699 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1701 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, O_RDWR
);
1705 * Creates some virtual cpus. Good luck creating more than one.
1707 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1710 struct kvm_vcpu
*vcpu
, *v
;
1712 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1714 return PTR_ERR(vcpu
);
1716 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1718 r
= kvm_arch_vcpu_setup(vcpu
);
1722 mutex_lock(&kvm
->lock
);
1723 if (!kvm_vcpu_compatible(vcpu
)) {
1725 goto unlock_vcpu_destroy
;
1727 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1729 goto unlock_vcpu_destroy
;
1732 kvm_for_each_vcpu(r
, v
, kvm
)
1733 if (v
->vcpu_id
== id
) {
1735 goto unlock_vcpu_destroy
;
1738 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1740 /* Now it's all set up, let userspace reach it */
1742 r
= create_vcpu_fd(vcpu
);
1745 goto unlock_vcpu_destroy
;
1748 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1750 atomic_inc(&kvm
->online_vcpus
);
1752 mutex_unlock(&kvm
->lock
);
1755 unlock_vcpu_destroy
:
1756 mutex_unlock(&kvm
->lock
);
1758 kvm_arch_vcpu_destroy(vcpu
);
1762 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1765 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1766 vcpu
->sigset_active
= 1;
1767 vcpu
->sigset
= *sigset
;
1769 vcpu
->sigset_active
= 0;
1773 static long kvm_vcpu_ioctl(struct file
*filp
,
1774 unsigned int ioctl
, unsigned long arg
)
1776 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1777 void __user
*argp
= (void __user
*)arg
;
1779 struct kvm_fpu
*fpu
= NULL
;
1780 struct kvm_sregs
*kvm_sregs
= NULL
;
1782 if (vcpu
->kvm
->mm
!= current
->mm
)
1785 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1787 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1788 * so vcpu_load() would break it.
1790 if (ioctl
== KVM_S390_INTERRUPT
|| ioctl
== KVM_INTERRUPT
)
1791 return kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1801 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1802 trace_kvm_userspace_exit(vcpu
->run
->exit_reason
, r
);
1804 case KVM_GET_REGS
: {
1805 struct kvm_regs
*kvm_regs
;
1808 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1811 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1815 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1822 case KVM_SET_REGS
: {
1823 struct kvm_regs
*kvm_regs
;
1826 kvm_regs
= memdup_user(argp
, sizeof(*kvm_regs
));
1827 if (IS_ERR(kvm_regs
)) {
1828 r
= PTR_ERR(kvm_regs
);
1831 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1839 case KVM_GET_SREGS
: {
1840 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1844 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1848 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1853 case KVM_SET_SREGS
: {
1854 kvm_sregs
= memdup_user(argp
, sizeof(*kvm_sregs
));
1855 if (IS_ERR(kvm_sregs
)) {
1856 r
= PTR_ERR(kvm_sregs
);
1859 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1865 case KVM_GET_MP_STATE
: {
1866 struct kvm_mp_state mp_state
;
1868 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1872 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1877 case KVM_SET_MP_STATE
: {
1878 struct kvm_mp_state mp_state
;
1881 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1883 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1889 case KVM_TRANSLATE
: {
1890 struct kvm_translation tr
;
1893 if (copy_from_user(&tr
, argp
, sizeof tr
))
1895 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1899 if (copy_to_user(argp
, &tr
, sizeof tr
))
1904 case KVM_SET_GUEST_DEBUG
: {
1905 struct kvm_guest_debug dbg
;
1908 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1910 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1916 case KVM_SET_SIGNAL_MASK
: {
1917 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1918 struct kvm_signal_mask kvm_sigmask
;
1919 sigset_t sigset
, *p
;
1924 if (copy_from_user(&kvm_sigmask
, argp
,
1925 sizeof kvm_sigmask
))
1928 if (kvm_sigmask
.len
!= sizeof sigset
)
1931 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1936 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, p
);
1940 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1944 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1948 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1954 fpu
= memdup_user(argp
, sizeof(*fpu
));
1959 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1966 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1975 #ifdef CONFIG_COMPAT
1976 static long kvm_vcpu_compat_ioctl(struct file
*filp
,
1977 unsigned int ioctl
, unsigned long arg
)
1979 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1980 void __user
*argp
= compat_ptr(arg
);
1983 if (vcpu
->kvm
->mm
!= current
->mm
)
1987 case KVM_SET_SIGNAL_MASK
: {
1988 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1989 struct kvm_signal_mask kvm_sigmask
;
1990 compat_sigset_t csigset
;
1995 if (copy_from_user(&kvm_sigmask
, argp
,
1996 sizeof kvm_sigmask
))
1999 if (kvm_sigmask
.len
!= sizeof csigset
)
2002 if (copy_from_user(&csigset
, sigmask_arg
->sigset
,
2006 sigset_from_compat(&sigset
, &csigset
);
2007 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
2011 r
= kvm_vcpu_ioctl(filp
, ioctl
, arg
);
2019 static long kvm_vm_ioctl(struct file
*filp
,
2020 unsigned int ioctl
, unsigned long arg
)
2022 struct kvm
*kvm
= filp
->private_data
;
2023 void __user
*argp
= (void __user
*)arg
;
2026 if (kvm
->mm
!= current
->mm
)
2029 case KVM_CREATE_VCPU
:
2030 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2034 case KVM_SET_USER_MEMORY_REGION
: {
2035 struct kvm_userspace_memory_region kvm_userspace_mem
;
2038 if (copy_from_user(&kvm_userspace_mem
, argp
,
2039 sizeof kvm_userspace_mem
))
2042 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
2047 case KVM_GET_DIRTY_LOG
: {
2048 struct kvm_dirty_log log
;
2051 if (copy_from_user(&log
, argp
, sizeof log
))
2053 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2058 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2059 case KVM_REGISTER_COALESCED_MMIO
: {
2060 struct kvm_coalesced_mmio_zone zone
;
2062 if (copy_from_user(&zone
, argp
, sizeof zone
))
2064 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
2070 case KVM_UNREGISTER_COALESCED_MMIO
: {
2071 struct kvm_coalesced_mmio_zone zone
;
2073 if (copy_from_user(&zone
, argp
, sizeof zone
))
2075 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
2083 struct kvm_irqfd data
;
2086 if (copy_from_user(&data
, argp
, sizeof data
))
2088 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
2091 case KVM_IOEVENTFD
: {
2092 struct kvm_ioeventfd data
;
2095 if (copy_from_user(&data
, argp
, sizeof data
))
2097 r
= kvm_ioeventfd(kvm
, &data
);
2100 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2101 case KVM_SET_BOOT_CPU_ID
:
2103 mutex_lock(&kvm
->lock
);
2104 if (atomic_read(&kvm
->online_vcpus
) != 0)
2107 kvm
->bsp_vcpu_id
= arg
;
2108 mutex_unlock(&kvm
->lock
);
2112 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
2114 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
2120 #ifdef CONFIG_COMPAT
2121 struct compat_kvm_dirty_log
{
2125 compat_uptr_t dirty_bitmap
; /* one bit per page */
2130 static long kvm_vm_compat_ioctl(struct file
*filp
,
2131 unsigned int ioctl
, unsigned long arg
)
2133 struct kvm
*kvm
= filp
->private_data
;
2136 if (kvm
->mm
!= current
->mm
)
2139 case KVM_GET_DIRTY_LOG
: {
2140 struct compat_kvm_dirty_log compat_log
;
2141 struct kvm_dirty_log log
;
2144 if (copy_from_user(&compat_log
, (void __user
*)arg
,
2145 sizeof(compat_log
)))
2147 log
.slot
= compat_log
.slot
;
2148 log
.padding1
= compat_log
.padding1
;
2149 log
.padding2
= compat_log
.padding2
;
2150 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
2152 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2158 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
2166 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2168 struct page
*page
[1];
2171 gfn_t gfn
= vmf
->pgoff
;
2172 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2174 addr
= gfn_to_hva(kvm
, gfn
);
2175 if (kvm_is_error_hva(addr
))
2176 return VM_FAULT_SIGBUS
;
2178 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
2180 if (unlikely(npages
!= 1))
2181 return VM_FAULT_SIGBUS
;
2183 vmf
->page
= page
[0];
2187 static const struct vm_operations_struct kvm_vm_vm_ops
= {
2188 .fault
= kvm_vm_fault
,
2191 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2193 vma
->vm_ops
= &kvm_vm_vm_ops
;
2197 static struct file_operations kvm_vm_fops
= {
2198 .release
= kvm_vm_release
,
2199 .unlocked_ioctl
= kvm_vm_ioctl
,
2200 #ifdef CONFIG_COMPAT
2201 .compat_ioctl
= kvm_vm_compat_ioctl
,
2203 .mmap
= kvm_vm_mmap
,
2204 .llseek
= noop_llseek
,
2207 static int kvm_dev_ioctl_create_vm(void)
2212 kvm
= kvm_create_vm();
2214 return PTR_ERR(kvm
);
2215 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2216 r
= kvm_coalesced_mmio_init(kvm
);
2222 r
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, O_RDWR
);
2229 static long kvm_dev_ioctl_check_extension_generic(long arg
)
2232 case KVM_CAP_USER_MEMORY
:
2233 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
2234 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
2235 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2236 case KVM_CAP_SET_BOOT_CPU_ID
:
2238 case KVM_CAP_INTERNAL_ERROR_DATA
:
2240 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2241 case KVM_CAP_IRQ_ROUTING
:
2242 return KVM_MAX_IRQ_ROUTES
;
2247 return kvm_dev_ioctl_check_extension(arg
);
2250 static long kvm_dev_ioctl(struct file
*filp
,
2251 unsigned int ioctl
, unsigned long arg
)
2256 case KVM_GET_API_VERSION
:
2260 r
= KVM_API_VERSION
;
2266 r
= kvm_dev_ioctl_create_vm();
2268 case KVM_CHECK_EXTENSION
:
2269 r
= kvm_dev_ioctl_check_extension_generic(arg
);
2271 case KVM_GET_VCPU_MMAP_SIZE
:
2275 r
= PAGE_SIZE
; /* struct kvm_run */
2277 r
+= PAGE_SIZE
; /* pio data page */
2279 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2280 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
2283 case KVM_TRACE_ENABLE
:
2284 case KVM_TRACE_PAUSE
:
2285 case KVM_TRACE_DISABLE
:
2289 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
2295 static struct file_operations kvm_chardev_ops
= {
2296 .unlocked_ioctl
= kvm_dev_ioctl
,
2297 .compat_ioctl
= kvm_dev_ioctl
,
2298 .llseek
= noop_llseek
,
2301 static struct miscdevice kvm_dev
= {
2307 static void hardware_enable_nolock(void *junk
)
2309 int cpu
= raw_smp_processor_id();
2312 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2315 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
2317 r
= kvm_arch_hardware_enable(NULL
);
2320 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2321 atomic_inc(&hardware_enable_failed
);
2322 printk(KERN_INFO
"kvm: enabling virtualization on "
2323 "CPU%d failed\n", cpu
);
2327 static void hardware_enable(void *junk
)
2329 raw_spin_lock(&kvm_lock
);
2330 hardware_enable_nolock(junk
);
2331 raw_spin_unlock(&kvm_lock
);
2334 static void hardware_disable_nolock(void *junk
)
2336 int cpu
= raw_smp_processor_id();
2338 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
2340 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
2341 kvm_arch_hardware_disable(NULL
);
2344 static void hardware_disable(void *junk
)
2346 raw_spin_lock(&kvm_lock
);
2347 hardware_disable_nolock(junk
);
2348 raw_spin_unlock(&kvm_lock
);
2351 static void hardware_disable_all_nolock(void)
2353 BUG_ON(!kvm_usage_count
);
2356 if (!kvm_usage_count
)
2357 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2360 static void hardware_disable_all(void)
2362 raw_spin_lock(&kvm_lock
);
2363 hardware_disable_all_nolock();
2364 raw_spin_unlock(&kvm_lock
);
2367 static int hardware_enable_all(void)
2371 raw_spin_lock(&kvm_lock
);
2374 if (kvm_usage_count
== 1) {
2375 atomic_set(&hardware_enable_failed
, 0);
2376 on_each_cpu(hardware_enable_nolock
, NULL
, 1);
2378 if (atomic_read(&hardware_enable_failed
)) {
2379 hardware_disable_all_nolock();
2384 raw_spin_unlock(&kvm_lock
);
2389 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2394 if (!kvm_usage_count
)
2397 val
&= ~CPU_TASKS_FROZEN
;
2400 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2402 hardware_disable(NULL
);
2405 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2407 hardware_enable(NULL
);
2414 asmlinkage
void kvm_spurious_fault(void)
2416 /* Fault while not rebooting. We want the trace. */
2419 EXPORT_SYMBOL_GPL(kvm_spurious_fault
);
2421 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2425 * Some (well, at least mine) BIOSes hang on reboot if
2428 * And Intel TXT required VMX off for all cpu when system shutdown.
2430 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2431 kvm_rebooting
= true;
2432 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2436 static struct notifier_block kvm_reboot_notifier
= {
2437 .notifier_call
= kvm_reboot
,
2441 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2445 for (i
= 0; i
< bus
->dev_count
; i
++) {
2446 struct kvm_io_device
*pos
= bus
->range
[i
].dev
;
2448 kvm_iodevice_destructor(pos
);
2453 int kvm_io_bus_sort_cmp(const void *p1
, const void *p2
)
2455 const struct kvm_io_range
*r1
= p1
;
2456 const struct kvm_io_range
*r2
= p2
;
2458 if (r1
->addr
< r2
->addr
)
2460 if (r1
->addr
+ r1
->len
> r2
->addr
+ r2
->len
)
2465 int kvm_io_bus_insert_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
,
2466 gpa_t addr
, int len
)
2468 if (bus
->dev_count
== NR_IOBUS_DEVS
)
2471 bus
->range
[bus
->dev_count
++] = (struct kvm_io_range
) {
2477 sort(bus
->range
, bus
->dev_count
, sizeof(struct kvm_io_range
),
2478 kvm_io_bus_sort_cmp
, NULL
);
2483 int kvm_io_bus_get_first_dev(struct kvm_io_bus
*bus
,
2484 gpa_t addr
, int len
)
2486 struct kvm_io_range
*range
, key
;
2489 key
= (struct kvm_io_range
) {
2494 range
= bsearch(&key
, bus
->range
, bus
->dev_count
,
2495 sizeof(struct kvm_io_range
), kvm_io_bus_sort_cmp
);
2499 off
= range
- bus
->range
;
2501 while (off
> 0 && kvm_io_bus_sort_cmp(&key
, &bus
->range
[off
-1]) == 0)
2507 /* kvm_io_bus_write - called under kvm->slots_lock */
2508 int kvm_io_bus_write(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2509 int len
, const void *val
)
2512 struct kvm_io_bus
*bus
;
2513 struct kvm_io_range range
;
2515 range
= (struct kvm_io_range
) {
2520 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2521 idx
= kvm_io_bus_get_first_dev(bus
, addr
, len
);
2525 while (idx
< bus
->dev_count
&&
2526 kvm_io_bus_sort_cmp(&range
, &bus
->range
[idx
]) == 0) {
2527 if (!kvm_iodevice_write(bus
->range
[idx
].dev
, addr
, len
, val
))
2535 /* kvm_io_bus_read - called under kvm->slots_lock */
2536 int kvm_io_bus_read(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2540 struct kvm_io_bus
*bus
;
2541 struct kvm_io_range range
;
2543 range
= (struct kvm_io_range
) {
2548 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2549 idx
= kvm_io_bus_get_first_dev(bus
, addr
, len
);
2553 while (idx
< bus
->dev_count
&&
2554 kvm_io_bus_sort_cmp(&range
, &bus
->range
[idx
]) == 0) {
2555 if (!kvm_iodevice_read(bus
->range
[idx
].dev
, addr
, len
, val
))
2563 /* Caller must hold slots_lock. */
2564 int kvm_io_bus_register_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2565 int len
, struct kvm_io_device
*dev
)
2567 struct kvm_io_bus
*new_bus
, *bus
;
2569 bus
= kvm
->buses
[bus_idx
];
2570 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2573 new_bus
= kmemdup(bus
, sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2576 kvm_io_bus_insert_dev(new_bus
, dev
, addr
, len
);
2577 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2578 synchronize_srcu_expedited(&kvm
->srcu
);
2584 /* Caller must hold slots_lock. */
2585 int kvm_io_bus_unregister_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2586 struct kvm_io_device
*dev
)
2589 struct kvm_io_bus
*new_bus
, *bus
;
2591 bus
= kvm
->buses
[bus_idx
];
2593 new_bus
= kmemdup(bus
, sizeof(*bus
), GFP_KERNEL
);
2598 for (i
= 0; i
< new_bus
->dev_count
; i
++)
2599 if (new_bus
->range
[i
].dev
== dev
) {
2601 new_bus
->dev_count
--;
2602 new_bus
->range
[i
] = new_bus
->range
[new_bus
->dev_count
];
2603 sort(new_bus
->range
, new_bus
->dev_count
,
2604 sizeof(struct kvm_io_range
),
2605 kvm_io_bus_sort_cmp
, NULL
);
2614 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2615 synchronize_srcu_expedited(&kvm
->srcu
);
2620 static struct notifier_block kvm_cpu_notifier
= {
2621 .notifier_call
= kvm_cpu_hotplug
,
2624 static int vm_stat_get(void *_offset
, u64
*val
)
2626 unsigned offset
= (long)_offset
;
2630 raw_spin_lock(&kvm_lock
);
2631 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2632 *val
+= *(u32
*)((void *)kvm
+ offset
);
2633 raw_spin_unlock(&kvm_lock
);
2637 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2639 static int vcpu_stat_get(void *_offset
, u64
*val
)
2641 unsigned offset
= (long)_offset
;
2643 struct kvm_vcpu
*vcpu
;
2647 raw_spin_lock(&kvm_lock
);
2648 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2649 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2650 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2652 raw_spin_unlock(&kvm_lock
);
2656 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2658 static const struct file_operations
*stat_fops
[] = {
2659 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2660 [KVM_STAT_VM
] = &vm_stat_fops
,
2663 static int kvm_init_debug(void)
2666 struct kvm_stats_debugfs_item
*p
;
2668 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2669 if (kvm_debugfs_dir
== NULL
)
2672 for (p
= debugfs_entries
; p
->name
; ++p
) {
2673 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2674 (void *)(long)p
->offset
,
2675 stat_fops
[p
->kind
]);
2676 if (p
->dentry
== NULL
)
2683 debugfs_remove_recursive(kvm_debugfs_dir
);
2688 static void kvm_exit_debug(void)
2690 struct kvm_stats_debugfs_item
*p
;
2692 for (p
= debugfs_entries
; p
->name
; ++p
)
2693 debugfs_remove(p
->dentry
);
2694 debugfs_remove(kvm_debugfs_dir
);
2697 static int kvm_suspend(void)
2699 if (kvm_usage_count
)
2700 hardware_disable_nolock(NULL
);
2704 static void kvm_resume(void)
2706 if (kvm_usage_count
) {
2707 WARN_ON(raw_spin_is_locked(&kvm_lock
));
2708 hardware_enable_nolock(NULL
);
2712 static struct syscore_ops kvm_syscore_ops
= {
2713 .suspend
= kvm_suspend
,
2714 .resume
= kvm_resume
,
2717 struct page
*bad_page
;
2721 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2723 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2726 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2728 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2730 kvm_arch_vcpu_load(vcpu
, cpu
);
2733 static void kvm_sched_out(struct preempt_notifier
*pn
,
2734 struct task_struct
*next
)
2736 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2738 kvm_arch_vcpu_put(vcpu
);
2741 int kvm_init(void *opaque
, unsigned vcpu_size
, unsigned vcpu_align
,
2742 struct module
*module
)
2747 r
= kvm_arch_init(opaque
);
2751 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2753 if (bad_page
== NULL
) {
2758 bad_pfn
= page_to_pfn(bad_page
);
2760 hwpoison_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2762 if (hwpoison_page
== NULL
) {
2767 hwpoison_pfn
= page_to_pfn(hwpoison_page
);
2769 fault_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2771 if (fault_page
== NULL
) {
2776 fault_pfn
= page_to_pfn(fault_page
);
2778 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2783 r
= kvm_arch_hardware_setup();
2787 for_each_online_cpu(cpu
) {
2788 smp_call_function_single(cpu
,
2789 kvm_arch_check_processor_compat
,
2795 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2798 register_reboot_notifier(&kvm_reboot_notifier
);
2800 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2802 vcpu_align
= __alignof__(struct kvm_vcpu
);
2803 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
, vcpu_align
,
2805 if (!kvm_vcpu_cache
) {
2810 r
= kvm_async_pf_init();
2814 kvm_chardev_ops
.owner
= module
;
2815 kvm_vm_fops
.owner
= module
;
2816 kvm_vcpu_fops
.owner
= module
;
2818 r
= misc_register(&kvm_dev
);
2820 printk(KERN_ERR
"kvm: misc device register failed\n");
2824 register_syscore_ops(&kvm_syscore_ops
);
2826 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2827 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2829 r
= kvm_init_debug();
2831 printk(KERN_ERR
"kvm: create debugfs files failed\n");
2838 unregister_syscore_ops(&kvm_syscore_ops
);
2840 kvm_async_pf_deinit();
2842 kmem_cache_destroy(kvm_vcpu_cache
);
2844 unregister_reboot_notifier(&kvm_reboot_notifier
);
2845 unregister_cpu_notifier(&kvm_cpu_notifier
);
2848 kvm_arch_hardware_unsetup();
2850 free_cpumask_var(cpus_hardware_enabled
);
2853 __free_page(fault_page
);
2855 __free_page(hwpoison_page
);
2856 __free_page(bad_page
);
2862 EXPORT_SYMBOL_GPL(kvm_init
);
2867 misc_deregister(&kvm_dev
);
2868 kmem_cache_destroy(kvm_vcpu_cache
);
2869 kvm_async_pf_deinit();
2870 unregister_syscore_ops(&kvm_syscore_ops
);
2871 unregister_reboot_notifier(&kvm_reboot_notifier
);
2872 unregister_cpu_notifier(&kvm_cpu_notifier
);
2873 on_each_cpu(hardware_disable_nolock
, NULL
, 1);
2874 kvm_arch_hardware_unsetup();
2876 free_cpumask_var(cpus_hardware_enabled
);
2877 __free_page(hwpoison_page
);
2878 __free_page(bad_page
);
2880 EXPORT_SYMBOL_GPL(kvm_exit
);