2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
21 #include <linux/kvm_host.h>
22 #include <linux/kvm.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.h>
27 #include <linux/miscdevice.h>
28 #include <linux/vmalloc.h>
29 #include <linux/reboot.h>
30 #include <linux/debugfs.h>
31 #include <linux/highmem.h>
32 #include <linux/file.h>
33 #include <linux/sysdev.h>
34 #include <linux/cpu.h>
35 #include <linux/sched.h>
36 #include <linux/cpumask.h>
37 #include <linux/smp.h>
38 #include <linux/anon_inodes.h>
39 #include <linux/profile.h>
40 #include <linux/kvm_para.h>
41 #include <linux/pagemap.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44 #include <linux/bitops.h>
45 #include <linux/spinlock.h>
46 #include <linux/compat.h>
47 #include <linux/srcu.h>
48 #include <linux/hugetlb.h>
49 #include <linux/slab.h>
51 #include <asm/processor.h>
53 #include <asm/uaccess.h>
54 #include <asm/pgtable.h>
55 #include <asm-generic/bitops/le.h>
57 #include "coalesced_mmio.h"
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_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
,
87 static int hardware_enable_all(void);
88 static void hardware_disable_all(void);
90 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
);
92 static bool kvm_rebooting
;
94 static bool largepages_enabled
= true;
96 static struct page
*hwpoison_page
;
97 static pfn_t hwpoison_pfn
;
99 static struct page
*fault_page
;
100 static pfn_t fault_pfn
;
102 inline int kvm_is_mmio_pfn(pfn_t pfn
)
104 if (pfn_valid(pfn
)) {
105 struct page
*page
= compound_head(pfn_to_page(pfn
));
106 return PageReserved(page
);
113 * Switches to specified vcpu, until a matching vcpu_put()
115 void vcpu_load(struct kvm_vcpu
*vcpu
)
119 mutex_lock(&vcpu
->mutex
);
121 preempt_notifier_register(&vcpu
->preempt_notifier
);
122 kvm_arch_vcpu_load(vcpu
, cpu
);
126 void vcpu_put(struct kvm_vcpu
*vcpu
)
129 kvm_arch_vcpu_put(vcpu
);
130 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
132 mutex_unlock(&vcpu
->mutex
);
135 static void ack_flush(void *_completed
)
139 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
144 struct kvm_vcpu
*vcpu
;
146 zalloc_cpumask_var(&cpus
, GFP_ATOMIC
);
148 raw_spin_lock(&kvm
->requests_lock
);
149 me
= smp_processor_id();
150 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
151 if (kvm_make_check_request(req
, vcpu
))
154 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
155 cpumask_set_cpu(cpu
, cpus
);
157 if (unlikely(cpus
== NULL
))
158 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
159 else if (!cpumask_empty(cpus
))
160 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
163 raw_spin_unlock(&kvm
->requests_lock
);
164 free_cpumask_var(cpus
);
168 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
170 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
171 ++kvm
->stat
.remote_tlb_flush
;
174 void kvm_reload_remote_mmus(struct kvm
*kvm
)
176 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
179 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
184 mutex_init(&vcpu
->mutex
);
188 init_waitqueue_head(&vcpu
->wq
);
190 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
195 vcpu
->run
= page_address(page
);
197 r
= kvm_arch_vcpu_init(vcpu
);
203 free_page((unsigned long)vcpu
->run
);
207 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
209 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
211 kvm_arch_vcpu_uninit(vcpu
);
212 free_page((unsigned long)vcpu
->run
);
214 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
216 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
217 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
219 return container_of(mn
, struct kvm
, mmu_notifier
);
222 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
223 struct mm_struct
*mm
,
224 unsigned long address
)
226 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
227 int need_tlb_flush
, idx
;
230 * When ->invalidate_page runs, the linux pte has been zapped
231 * already but the page is still allocated until
232 * ->invalidate_page returns. So if we increase the sequence
233 * here the kvm page fault will notice if the spte can't be
234 * established because the page is going to be freed. If
235 * instead the kvm page fault establishes the spte before
236 * ->invalidate_page runs, kvm_unmap_hva will release it
239 * The sequence increase only need to be seen at spin_unlock
240 * time, and not at spin_lock time.
242 * Increasing the sequence after the spin_unlock would be
243 * unsafe because the kvm page fault could then establish the
244 * pte after kvm_unmap_hva returned, without noticing the page
245 * is going to be freed.
247 idx
= srcu_read_lock(&kvm
->srcu
);
248 spin_lock(&kvm
->mmu_lock
);
249 kvm
->mmu_notifier_seq
++;
250 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
251 spin_unlock(&kvm
->mmu_lock
);
252 srcu_read_unlock(&kvm
->srcu
, idx
);
254 /* we've to flush the tlb before the pages can be freed */
256 kvm_flush_remote_tlbs(kvm
);
260 static void kvm_mmu_notifier_change_pte(struct mmu_notifier
*mn
,
261 struct mm_struct
*mm
,
262 unsigned long address
,
265 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
268 idx
= srcu_read_lock(&kvm
->srcu
);
269 spin_lock(&kvm
->mmu_lock
);
270 kvm
->mmu_notifier_seq
++;
271 kvm_set_spte_hva(kvm
, address
, pte
);
272 spin_unlock(&kvm
->mmu_lock
);
273 srcu_read_unlock(&kvm
->srcu
, idx
);
276 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
277 struct mm_struct
*mm
,
281 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
282 int need_tlb_flush
= 0, idx
;
284 idx
= srcu_read_lock(&kvm
->srcu
);
285 spin_lock(&kvm
->mmu_lock
);
287 * The count increase must become visible at unlock time as no
288 * spte can be established without taking the mmu_lock and
289 * count is also read inside the mmu_lock critical section.
291 kvm
->mmu_notifier_count
++;
292 for (; start
< end
; start
+= PAGE_SIZE
)
293 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
294 spin_unlock(&kvm
->mmu_lock
);
295 srcu_read_unlock(&kvm
->srcu
, idx
);
297 /* we've to flush the tlb before the pages can be freed */
299 kvm_flush_remote_tlbs(kvm
);
302 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
303 struct mm_struct
*mm
,
307 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
309 spin_lock(&kvm
->mmu_lock
);
311 * This sequence increase will notify the kvm page fault that
312 * the page that is going to be mapped in the spte could have
315 kvm
->mmu_notifier_seq
++;
317 * The above sequence increase must be visible before the
318 * below count decrease but both values are read by the kvm
319 * page fault under mmu_lock spinlock so we don't need to add
320 * a smb_wmb() here in between the two.
322 kvm
->mmu_notifier_count
--;
323 spin_unlock(&kvm
->mmu_lock
);
325 BUG_ON(kvm
->mmu_notifier_count
< 0);
328 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
329 struct mm_struct
*mm
,
330 unsigned long address
)
332 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
335 idx
= srcu_read_lock(&kvm
->srcu
);
336 spin_lock(&kvm
->mmu_lock
);
337 young
= kvm_age_hva(kvm
, address
);
338 spin_unlock(&kvm
->mmu_lock
);
339 srcu_read_unlock(&kvm
->srcu
, idx
);
342 kvm_flush_remote_tlbs(kvm
);
347 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
348 struct mm_struct
*mm
)
350 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
353 idx
= srcu_read_lock(&kvm
->srcu
);
354 kvm_arch_flush_shadow(kvm
);
355 srcu_read_unlock(&kvm
->srcu
, idx
);
358 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
359 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
360 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
361 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
362 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
363 .change_pte
= kvm_mmu_notifier_change_pte
,
364 .release
= kvm_mmu_notifier_release
,
367 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
369 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
370 return mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
373 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
375 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
380 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
382 static struct kvm
*kvm_create_vm(void)
385 struct kvm
*kvm
= kvm_arch_create_vm();
390 r
= hardware_enable_all();
392 goto out_err_nodisable
;
394 #ifdef CONFIG_HAVE_KVM_IRQCHIP
395 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
396 INIT_HLIST_HEAD(&kvm
->irq_ack_notifier_list
);
400 kvm
->memslots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
403 if (init_srcu_struct(&kvm
->srcu
))
405 for (i
= 0; i
< KVM_NR_BUSES
; i
++) {
406 kvm
->buses
[i
] = kzalloc(sizeof(struct kvm_io_bus
),
408 if (!kvm
->buses
[i
]) {
409 cleanup_srcu_struct(&kvm
->srcu
);
414 r
= kvm_init_mmu_notifier(kvm
);
416 cleanup_srcu_struct(&kvm
->srcu
);
420 kvm
->mm
= current
->mm
;
421 atomic_inc(&kvm
->mm
->mm_count
);
422 spin_lock_init(&kvm
->mmu_lock
);
423 raw_spin_lock_init(&kvm
->requests_lock
);
424 kvm_eventfd_init(kvm
);
425 mutex_init(&kvm
->lock
);
426 mutex_init(&kvm
->irq_lock
);
427 mutex_init(&kvm
->slots_lock
);
428 atomic_set(&kvm
->users_count
, 1);
429 spin_lock(&kvm_lock
);
430 list_add(&kvm
->vm_list
, &vm_list
);
431 spin_unlock(&kvm_lock
);
436 hardware_disable_all();
438 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
439 kfree(kvm
->buses
[i
]);
440 kfree(kvm
->memslots
);
446 * Free any memory in @free but not in @dont.
448 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
449 struct kvm_memory_slot
*dont
)
453 if (!dont
|| free
->rmap
!= dont
->rmap
)
456 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
457 vfree(free
->dirty_bitmap
);
460 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
461 if (!dont
|| free
->lpage_info
[i
] != dont
->lpage_info
[i
]) {
462 vfree(free
->lpage_info
[i
]);
463 free
->lpage_info
[i
] = NULL
;
468 free
->dirty_bitmap
= NULL
;
472 void kvm_free_physmem(struct kvm
*kvm
)
475 struct kvm_memslots
*slots
= kvm
->memslots
;
477 for (i
= 0; i
< slots
->nmemslots
; ++i
)
478 kvm_free_physmem_slot(&slots
->memslots
[i
], NULL
);
480 kfree(kvm
->memslots
);
483 static void kvm_destroy_vm(struct kvm
*kvm
)
486 struct mm_struct
*mm
= kvm
->mm
;
488 kvm_arch_sync_events(kvm
);
489 spin_lock(&kvm_lock
);
490 list_del(&kvm
->vm_list
);
491 spin_unlock(&kvm_lock
);
492 kvm_free_irq_routing(kvm
);
493 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
494 kvm_io_bus_destroy(kvm
->buses
[i
]);
495 kvm_coalesced_mmio_free(kvm
);
496 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
497 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
499 kvm_arch_flush_shadow(kvm
);
501 kvm_arch_destroy_vm(kvm
);
502 hardware_disable_all();
506 void kvm_get_kvm(struct kvm
*kvm
)
508 atomic_inc(&kvm
->users_count
);
510 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
512 void kvm_put_kvm(struct kvm
*kvm
)
514 if (atomic_dec_and_test(&kvm
->users_count
))
517 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
520 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
522 struct kvm
*kvm
= filp
->private_data
;
524 kvm_irqfd_release(kvm
);
531 * Allocate some memory and give it an address in the guest physical address
534 * Discontiguous memory is allowed, mostly for framebuffers.
536 * Must be called holding mmap_sem for write.
538 int __kvm_set_memory_region(struct kvm
*kvm
,
539 struct kvm_userspace_memory_region
*mem
,
542 int r
, flush_shadow
= 0;
544 unsigned long npages
;
546 struct kvm_memory_slot
*memslot
;
547 struct kvm_memory_slot old
, new;
548 struct kvm_memslots
*slots
, *old_memslots
;
551 /* General sanity checks */
552 if (mem
->memory_size
& (PAGE_SIZE
- 1))
554 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
556 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
558 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
560 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
563 memslot
= &kvm
->memslots
->memslots
[mem
->slot
];
564 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
565 npages
= mem
->memory_size
>> PAGE_SHIFT
;
568 if (npages
> KVM_MEM_MAX_NR_PAGES
)
572 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
574 new = old
= *memslot
;
577 new.base_gfn
= base_gfn
;
579 new.flags
= mem
->flags
;
581 /* Disallow changing a memory slot's size. */
583 if (npages
&& old
.npages
&& npages
!= old
.npages
)
586 /* Check for overlaps */
588 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
589 struct kvm_memory_slot
*s
= &kvm
->memslots
->memslots
[i
];
591 if (s
== memslot
|| !s
->npages
)
593 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
594 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
598 /* Free page dirty bitmap if unneeded */
599 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
600 new.dirty_bitmap
= NULL
;
604 /* Allocate if a slot is being created */
606 if (npages
&& !new.rmap
) {
607 new.rmap
= vmalloc(npages
* sizeof(*new.rmap
));
612 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
614 new.user_alloc
= user_alloc
;
615 new.userspace_addr
= mem
->userspace_addr
;
620 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
626 /* Avoid unused variable warning if no large pages */
629 if (new.lpage_info
[i
])
632 lpages
= 1 + ((base_gfn
+ npages
- 1)
633 >> KVM_HPAGE_GFN_SHIFT(level
));
634 lpages
-= base_gfn
>> KVM_HPAGE_GFN_SHIFT(level
);
636 new.lpage_info
[i
] = vmalloc(lpages
* sizeof(*new.lpage_info
[i
]));
638 if (!new.lpage_info
[i
])
641 memset(new.lpage_info
[i
], 0,
642 lpages
* sizeof(*new.lpage_info
[i
]));
644 if (base_gfn
& (KVM_PAGES_PER_HPAGE(level
) - 1))
645 new.lpage_info
[i
][0].write_count
= 1;
646 if ((base_gfn
+npages
) & (KVM_PAGES_PER_HPAGE(level
) - 1))
647 new.lpage_info
[i
][lpages
- 1].write_count
= 1;
648 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
650 * If the gfn and userspace address are not aligned wrt each
651 * other, or if explicitly asked to, disable large page
652 * support for this slot
654 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE(level
) - 1) ||
656 for (j
= 0; j
< lpages
; ++j
)
657 new.lpage_info
[i
][j
].write_count
= 1;
662 /* Allocate page dirty bitmap if needed */
663 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
664 unsigned long dirty_bytes
= kvm_dirty_bitmap_bytes(&new);
666 new.dirty_bitmap
= vmalloc(dirty_bytes
);
667 if (!new.dirty_bitmap
)
669 memset(new.dirty_bitmap
, 0, dirty_bytes
);
670 /* destroy any largepage mappings for dirty tracking */
674 #else /* not defined CONFIG_S390 */
675 new.user_alloc
= user_alloc
;
677 new.userspace_addr
= mem
->userspace_addr
;
678 #endif /* not defined CONFIG_S390 */
682 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
685 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
686 if (mem
->slot
>= slots
->nmemslots
)
687 slots
->nmemslots
= mem
->slot
+ 1;
688 slots
->memslots
[mem
->slot
].flags
|= KVM_MEMSLOT_INVALID
;
690 old_memslots
= kvm
->memslots
;
691 rcu_assign_pointer(kvm
->memslots
, slots
);
692 synchronize_srcu_expedited(&kvm
->srcu
);
693 /* From this point no new shadow pages pointing to a deleted
694 * memslot will be created.
696 * validation of sp->gfn happens in:
697 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
698 * - kvm_is_visible_gfn (mmu_check_roots)
700 kvm_arch_flush_shadow(kvm
);
704 r
= kvm_arch_prepare_memory_region(kvm
, &new, old
, mem
, user_alloc
);
708 /* map the pages in iommu page table */
710 r
= kvm_iommu_map_pages(kvm
, &new);
716 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
719 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
720 if (mem
->slot
>= slots
->nmemslots
)
721 slots
->nmemslots
= mem
->slot
+ 1;
723 /* actual memory is freed via old in kvm_free_physmem_slot below */
726 new.dirty_bitmap
= NULL
;
727 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
)
728 new.lpage_info
[i
] = NULL
;
731 slots
->memslots
[mem
->slot
] = new;
732 old_memslots
= kvm
->memslots
;
733 rcu_assign_pointer(kvm
->memslots
, slots
);
734 synchronize_srcu_expedited(&kvm
->srcu
);
736 kvm_arch_commit_memory_region(kvm
, mem
, old
, user_alloc
);
738 kvm_free_physmem_slot(&old
, &new);
742 kvm_arch_flush_shadow(kvm
);
747 kvm_free_physmem_slot(&new, &old
);
752 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
754 int kvm_set_memory_region(struct kvm
*kvm
,
755 struct kvm_userspace_memory_region
*mem
,
760 mutex_lock(&kvm
->slots_lock
);
761 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
762 mutex_unlock(&kvm
->slots_lock
);
765 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
767 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
769 kvm_userspace_memory_region
*mem
,
772 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
774 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
777 int kvm_get_dirty_log(struct kvm
*kvm
,
778 struct kvm_dirty_log
*log
, int *is_dirty
)
780 struct kvm_memory_slot
*memslot
;
783 unsigned long any
= 0;
786 if (log
->slot
>= KVM_MEMORY_SLOTS
)
789 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
791 if (!memslot
->dirty_bitmap
)
794 n
= kvm_dirty_bitmap_bytes(memslot
);
796 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
797 any
= memslot
->dirty_bitmap
[i
];
800 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
811 void kvm_disable_largepages(void)
813 largepages_enabled
= false;
815 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
817 int is_error_page(struct page
*page
)
819 return page
== bad_page
|| page
== hwpoison_page
|| page
== fault_page
;
821 EXPORT_SYMBOL_GPL(is_error_page
);
823 int is_error_pfn(pfn_t pfn
)
825 return pfn
== bad_pfn
|| pfn
== hwpoison_pfn
|| pfn
== fault_pfn
;
827 EXPORT_SYMBOL_GPL(is_error_pfn
);
829 int is_hwpoison_pfn(pfn_t pfn
)
831 return pfn
== hwpoison_pfn
;
833 EXPORT_SYMBOL_GPL(is_hwpoison_pfn
);
835 int is_fault_pfn(pfn_t pfn
)
837 return pfn
== fault_pfn
;
839 EXPORT_SYMBOL_GPL(is_fault_pfn
);
841 static inline unsigned long bad_hva(void)
846 int kvm_is_error_hva(unsigned long addr
)
848 return addr
== bad_hva();
850 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
852 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
855 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
857 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
858 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
860 if (gfn
>= memslot
->base_gfn
861 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
866 EXPORT_SYMBOL_GPL(gfn_to_memslot
);
868 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
871 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
873 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
874 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
876 if (memslot
->flags
& KVM_MEMSLOT_INVALID
)
879 if (gfn
>= memslot
->base_gfn
880 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
885 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
887 unsigned long kvm_host_page_size(struct kvm
*kvm
, gfn_t gfn
)
889 struct vm_area_struct
*vma
;
890 unsigned long addr
, size
;
894 addr
= gfn_to_hva(kvm
, gfn
);
895 if (kvm_is_error_hva(addr
))
898 down_read(¤t
->mm
->mmap_sem
);
899 vma
= find_vma(current
->mm
, addr
);
903 size
= vma_kernel_pagesize(vma
);
906 up_read(¤t
->mm
->mmap_sem
);
911 int memslot_id(struct kvm
*kvm
, gfn_t gfn
)
914 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
915 struct kvm_memory_slot
*memslot
= NULL
;
917 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
918 memslot
= &slots
->memslots
[i
];
920 if (gfn
>= memslot
->base_gfn
921 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
925 return memslot
- slots
->memslots
;
928 static unsigned long gfn_to_hva_many(struct kvm
*kvm
, gfn_t gfn
,
931 struct kvm_memory_slot
*slot
;
933 slot
= gfn_to_memslot(kvm
, gfn
);
934 if (!slot
|| slot
->flags
& KVM_MEMSLOT_INVALID
)
938 *nr_pages
= slot
->npages
- (gfn
- slot
->base_gfn
);
940 return gfn_to_hva_memslot(slot
, gfn
);
943 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
945 return gfn_to_hva_many(kvm
, gfn
, NULL
);
947 EXPORT_SYMBOL_GPL(gfn_to_hva
);
949 static pfn_t
hva_to_pfn(struct kvm
*kvm
, unsigned long addr
, bool atomic
)
951 struct page
*page
[1];
956 npages
= __get_user_pages_fast(addr
, 1, 1, page
);
959 npages
= get_user_pages_fast(addr
, 1, 1, page
);
962 if (unlikely(npages
!= 1)) {
963 struct vm_area_struct
*vma
;
966 goto return_fault_page
;
968 down_read(¤t
->mm
->mmap_sem
);
969 if (is_hwpoison_address(addr
)) {
970 up_read(¤t
->mm
->mmap_sem
);
971 get_page(hwpoison_page
);
972 return page_to_pfn(hwpoison_page
);
975 vma
= find_vma(current
->mm
, addr
);
977 if (vma
== NULL
|| addr
< vma
->vm_start
||
978 !(vma
->vm_flags
& VM_PFNMAP
)) {
979 up_read(¤t
->mm
->mmap_sem
);
981 get_page(fault_page
);
982 return page_to_pfn(fault_page
);
985 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
986 up_read(¤t
->mm
->mmap_sem
);
987 BUG_ON(!kvm_is_mmio_pfn(pfn
));
989 pfn
= page_to_pfn(page
[0]);
994 pfn_t
hva_to_pfn_atomic(struct kvm
*kvm
, unsigned long addr
)
996 return hva_to_pfn(kvm
, addr
, true);
998 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic
);
1000 static pfn_t
__gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
, bool atomic
)
1004 addr
= gfn_to_hva(kvm
, gfn
);
1005 if (kvm_is_error_hva(addr
)) {
1007 return page_to_pfn(bad_page
);
1010 return hva_to_pfn(kvm
, addr
, atomic
);
1013 pfn_t
gfn_to_pfn_atomic(struct kvm
*kvm
, gfn_t gfn
)
1015 return __gfn_to_pfn(kvm
, gfn
, true);
1017 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic
);
1019 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
1021 return __gfn_to_pfn(kvm
, gfn
, false);
1023 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
1025 pfn_t
gfn_to_pfn_memslot(struct kvm
*kvm
,
1026 struct kvm_memory_slot
*slot
, gfn_t gfn
)
1028 unsigned long addr
= gfn_to_hva_memslot(slot
, gfn
);
1029 return hva_to_pfn(kvm
, addr
, false);
1032 int gfn_to_page_many_atomic(struct kvm
*kvm
, gfn_t gfn
, struct page
**pages
,
1038 addr
= gfn_to_hva_many(kvm
, gfn
, &entry
);
1039 if (kvm_is_error_hva(addr
))
1042 if (entry
< nr_pages
)
1045 return __get_user_pages_fast(addr
, nr_pages
, 1, pages
);
1047 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic
);
1049 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1053 pfn
= gfn_to_pfn(kvm
, gfn
);
1054 if (!kvm_is_mmio_pfn(pfn
))
1055 return pfn_to_page(pfn
);
1057 WARN_ON(kvm_is_mmio_pfn(pfn
));
1063 EXPORT_SYMBOL_GPL(gfn_to_page
);
1065 void kvm_release_page_clean(struct page
*page
)
1067 kvm_release_pfn_clean(page_to_pfn(page
));
1069 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1071 void kvm_release_pfn_clean(pfn_t pfn
)
1073 if (!kvm_is_mmio_pfn(pfn
))
1074 put_page(pfn_to_page(pfn
));
1076 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1078 void kvm_release_page_dirty(struct page
*page
)
1080 kvm_release_pfn_dirty(page_to_pfn(page
));
1082 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1084 void kvm_release_pfn_dirty(pfn_t pfn
)
1086 kvm_set_pfn_dirty(pfn
);
1087 kvm_release_pfn_clean(pfn
);
1089 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1091 void kvm_set_page_dirty(struct page
*page
)
1093 kvm_set_pfn_dirty(page_to_pfn(page
));
1095 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1097 void kvm_set_pfn_dirty(pfn_t pfn
)
1099 if (!kvm_is_mmio_pfn(pfn
)) {
1100 struct page
*page
= pfn_to_page(pfn
);
1101 if (!PageReserved(page
))
1105 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1107 void kvm_set_pfn_accessed(pfn_t pfn
)
1109 if (!kvm_is_mmio_pfn(pfn
))
1110 mark_page_accessed(pfn_to_page(pfn
));
1112 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1114 void kvm_get_pfn(pfn_t pfn
)
1116 if (!kvm_is_mmio_pfn(pfn
))
1117 get_page(pfn_to_page(pfn
));
1119 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1121 static int next_segment(unsigned long len
, int offset
)
1123 if (len
> PAGE_SIZE
- offset
)
1124 return PAGE_SIZE
- offset
;
1129 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1135 addr
= gfn_to_hva(kvm
, gfn
);
1136 if (kvm_is_error_hva(addr
))
1138 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1143 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1145 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1147 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1149 int offset
= offset_in_page(gpa
);
1152 while ((seg
= next_segment(len
, offset
)) != 0) {
1153 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1163 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1165 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1170 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1171 int offset
= offset_in_page(gpa
);
1173 addr
= gfn_to_hva(kvm
, gfn
);
1174 if (kvm_is_error_hva(addr
))
1176 pagefault_disable();
1177 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1183 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1185 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1186 int offset
, int len
)
1191 addr
= gfn_to_hva(kvm
, gfn
);
1192 if (kvm_is_error_hva(addr
))
1194 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1197 mark_page_dirty(kvm
, gfn
);
1200 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1202 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1205 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1207 int offset
= offset_in_page(gpa
);
1210 while ((seg
= next_segment(len
, offset
)) != 0) {
1211 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1222 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1224 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1226 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1228 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1230 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1232 int offset
= offset_in_page(gpa
);
1235 while ((seg
= next_segment(len
, offset
)) != 0) {
1236 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1245 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1247 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1249 struct kvm_memory_slot
*memslot
;
1251 memslot
= gfn_to_memslot(kvm
, gfn
);
1252 if (memslot
&& memslot
->dirty_bitmap
) {
1253 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1255 generic___set_le_bit(rel_gfn
, memslot
->dirty_bitmap
);
1260 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1262 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1267 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1269 if (kvm_arch_vcpu_runnable(vcpu
)) {
1270 kvm_make_request(KVM_REQ_UNHALT
, vcpu
);
1273 if (kvm_cpu_has_pending_timer(vcpu
))
1275 if (signal_pending(current
))
1281 finish_wait(&vcpu
->wq
, &wait
);
1284 void kvm_resched(struct kvm_vcpu
*vcpu
)
1286 if (!need_resched())
1290 EXPORT_SYMBOL_GPL(kvm_resched
);
1292 void kvm_vcpu_on_spin(struct kvm_vcpu
*vcpu
)
1297 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1299 /* Sleep for 100 us, and hope lock-holder got scheduled */
1300 expires
= ktime_add_ns(ktime_get(), 100000UL);
1301 schedule_hrtimeout(&expires
, HRTIMER_MODE_ABS
);
1303 finish_wait(&vcpu
->wq
, &wait
);
1305 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1307 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1309 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1312 if (vmf
->pgoff
== 0)
1313 page
= virt_to_page(vcpu
->run
);
1315 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1316 page
= virt_to_page(vcpu
->arch
.pio_data
);
1318 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1319 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1320 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1323 return VM_FAULT_SIGBUS
;
1329 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1330 .fault
= kvm_vcpu_fault
,
1333 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1335 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1339 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1341 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1343 kvm_put_kvm(vcpu
->kvm
);
1347 static struct file_operations kvm_vcpu_fops
= {
1348 .release
= kvm_vcpu_release
,
1349 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1350 .compat_ioctl
= kvm_vcpu_ioctl
,
1351 .mmap
= kvm_vcpu_mmap
,
1352 .llseek
= noop_llseek
,
1356 * Allocates an inode for the vcpu.
1358 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1360 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, O_RDWR
);
1364 * Creates some virtual cpus. Good luck creating more than one.
1366 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1369 struct kvm_vcpu
*vcpu
, *v
;
1371 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1373 return PTR_ERR(vcpu
);
1375 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1377 r
= kvm_arch_vcpu_setup(vcpu
);
1381 mutex_lock(&kvm
->lock
);
1382 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1387 kvm_for_each_vcpu(r
, v
, kvm
)
1388 if (v
->vcpu_id
== id
) {
1393 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1395 /* Now it's all set up, let userspace reach it */
1397 r
= create_vcpu_fd(vcpu
);
1403 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1405 atomic_inc(&kvm
->online_vcpus
);
1407 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1408 if (kvm
->bsp_vcpu_id
== id
)
1409 kvm
->bsp_vcpu
= vcpu
;
1411 mutex_unlock(&kvm
->lock
);
1415 mutex_unlock(&kvm
->lock
);
1416 kvm_arch_vcpu_destroy(vcpu
);
1420 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1423 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1424 vcpu
->sigset_active
= 1;
1425 vcpu
->sigset
= *sigset
;
1427 vcpu
->sigset_active
= 0;
1431 static long kvm_vcpu_ioctl(struct file
*filp
,
1432 unsigned int ioctl
, unsigned long arg
)
1434 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1435 void __user
*argp
= (void __user
*)arg
;
1437 struct kvm_fpu
*fpu
= NULL
;
1438 struct kvm_sregs
*kvm_sregs
= NULL
;
1440 if (vcpu
->kvm
->mm
!= current
->mm
)
1443 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1445 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1446 * so vcpu_load() would break it.
1448 if (ioctl
== KVM_S390_INTERRUPT
|| ioctl
== KVM_INTERRUPT
)
1449 return kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1459 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1461 case KVM_GET_REGS
: {
1462 struct kvm_regs
*kvm_regs
;
1465 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1468 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1472 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1479 case KVM_SET_REGS
: {
1480 struct kvm_regs
*kvm_regs
;
1483 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1487 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1489 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1497 case KVM_GET_SREGS
: {
1498 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1502 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1506 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1511 case KVM_SET_SREGS
: {
1512 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1517 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1519 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1525 case KVM_GET_MP_STATE
: {
1526 struct kvm_mp_state mp_state
;
1528 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1532 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1537 case KVM_SET_MP_STATE
: {
1538 struct kvm_mp_state mp_state
;
1541 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1543 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1549 case KVM_TRANSLATE
: {
1550 struct kvm_translation tr
;
1553 if (copy_from_user(&tr
, argp
, sizeof tr
))
1555 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1559 if (copy_to_user(argp
, &tr
, sizeof tr
))
1564 case KVM_SET_GUEST_DEBUG
: {
1565 struct kvm_guest_debug dbg
;
1568 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1570 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1576 case KVM_SET_SIGNAL_MASK
: {
1577 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1578 struct kvm_signal_mask kvm_sigmask
;
1579 sigset_t sigset
, *p
;
1584 if (copy_from_user(&kvm_sigmask
, argp
,
1585 sizeof kvm_sigmask
))
1588 if (kvm_sigmask
.len
!= sizeof sigset
)
1591 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1596 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, p
);
1600 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1604 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1608 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1614 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1619 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1621 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1628 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1637 static long kvm_vm_ioctl(struct file
*filp
,
1638 unsigned int ioctl
, unsigned long arg
)
1640 struct kvm
*kvm
= filp
->private_data
;
1641 void __user
*argp
= (void __user
*)arg
;
1644 if (kvm
->mm
!= current
->mm
)
1647 case KVM_CREATE_VCPU
:
1648 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1652 case KVM_SET_USER_MEMORY_REGION
: {
1653 struct kvm_userspace_memory_region kvm_userspace_mem
;
1656 if (copy_from_user(&kvm_userspace_mem
, argp
,
1657 sizeof kvm_userspace_mem
))
1660 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1665 case KVM_GET_DIRTY_LOG
: {
1666 struct kvm_dirty_log log
;
1669 if (copy_from_user(&log
, argp
, sizeof log
))
1671 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1676 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1677 case KVM_REGISTER_COALESCED_MMIO
: {
1678 struct kvm_coalesced_mmio_zone zone
;
1680 if (copy_from_user(&zone
, argp
, sizeof zone
))
1682 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1688 case KVM_UNREGISTER_COALESCED_MMIO
: {
1689 struct kvm_coalesced_mmio_zone zone
;
1691 if (copy_from_user(&zone
, argp
, sizeof zone
))
1693 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1701 struct kvm_irqfd data
;
1704 if (copy_from_user(&data
, argp
, sizeof data
))
1706 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
1709 case KVM_IOEVENTFD
: {
1710 struct kvm_ioeventfd data
;
1713 if (copy_from_user(&data
, argp
, sizeof data
))
1715 r
= kvm_ioeventfd(kvm
, &data
);
1718 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1719 case KVM_SET_BOOT_CPU_ID
:
1721 mutex_lock(&kvm
->lock
);
1722 if (atomic_read(&kvm
->online_vcpus
) != 0)
1725 kvm
->bsp_vcpu_id
= arg
;
1726 mutex_unlock(&kvm
->lock
);
1730 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1732 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
1738 #ifdef CONFIG_COMPAT
1739 struct compat_kvm_dirty_log
{
1743 compat_uptr_t dirty_bitmap
; /* one bit per page */
1748 static long kvm_vm_compat_ioctl(struct file
*filp
,
1749 unsigned int ioctl
, unsigned long arg
)
1751 struct kvm
*kvm
= filp
->private_data
;
1754 if (kvm
->mm
!= current
->mm
)
1757 case KVM_GET_DIRTY_LOG
: {
1758 struct compat_kvm_dirty_log compat_log
;
1759 struct kvm_dirty_log log
;
1762 if (copy_from_user(&compat_log
, (void __user
*)arg
,
1763 sizeof(compat_log
)))
1765 log
.slot
= compat_log
.slot
;
1766 log
.padding1
= compat_log
.padding1
;
1767 log
.padding2
= compat_log
.padding2
;
1768 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
1770 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1776 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
1784 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1786 struct page
*page
[1];
1789 gfn_t gfn
= vmf
->pgoff
;
1790 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1792 addr
= gfn_to_hva(kvm
, gfn
);
1793 if (kvm_is_error_hva(addr
))
1794 return VM_FAULT_SIGBUS
;
1796 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1798 if (unlikely(npages
!= 1))
1799 return VM_FAULT_SIGBUS
;
1801 vmf
->page
= page
[0];
1805 static const struct vm_operations_struct kvm_vm_vm_ops
= {
1806 .fault
= kvm_vm_fault
,
1809 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1811 vma
->vm_ops
= &kvm_vm_vm_ops
;
1815 static struct file_operations kvm_vm_fops
= {
1816 .release
= kvm_vm_release
,
1817 .unlocked_ioctl
= kvm_vm_ioctl
,
1818 #ifdef CONFIG_COMPAT
1819 .compat_ioctl
= kvm_vm_compat_ioctl
,
1821 .mmap
= kvm_vm_mmap
,
1822 .llseek
= noop_llseek
,
1825 static int kvm_dev_ioctl_create_vm(void)
1830 kvm
= kvm_create_vm();
1832 return PTR_ERR(kvm
);
1833 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1834 r
= kvm_coalesced_mmio_init(kvm
);
1840 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, O_RDWR
);
1847 static long kvm_dev_ioctl_check_extension_generic(long arg
)
1850 case KVM_CAP_USER_MEMORY
:
1851 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
1852 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
1853 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1854 case KVM_CAP_SET_BOOT_CPU_ID
:
1856 case KVM_CAP_INTERNAL_ERROR_DATA
:
1858 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1859 case KVM_CAP_IRQ_ROUTING
:
1860 return KVM_MAX_IRQ_ROUTES
;
1865 return kvm_dev_ioctl_check_extension(arg
);
1868 static long kvm_dev_ioctl(struct file
*filp
,
1869 unsigned int ioctl
, unsigned long arg
)
1874 case KVM_GET_API_VERSION
:
1878 r
= KVM_API_VERSION
;
1884 r
= kvm_dev_ioctl_create_vm();
1886 case KVM_CHECK_EXTENSION
:
1887 r
= kvm_dev_ioctl_check_extension_generic(arg
);
1889 case KVM_GET_VCPU_MMAP_SIZE
:
1893 r
= PAGE_SIZE
; /* struct kvm_run */
1895 r
+= PAGE_SIZE
; /* pio data page */
1897 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1898 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1901 case KVM_TRACE_ENABLE
:
1902 case KVM_TRACE_PAUSE
:
1903 case KVM_TRACE_DISABLE
:
1907 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1913 static struct file_operations kvm_chardev_ops
= {
1914 .unlocked_ioctl
= kvm_dev_ioctl
,
1915 .compat_ioctl
= kvm_dev_ioctl
,
1916 .llseek
= noop_llseek
,
1919 static struct miscdevice kvm_dev
= {
1925 static void hardware_enable(void *junk
)
1927 int cpu
= raw_smp_processor_id();
1930 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1933 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
1935 r
= kvm_arch_hardware_enable(NULL
);
1938 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1939 atomic_inc(&hardware_enable_failed
);
1940 printk(KERN_INFO
"kvm: enabling virtualization on "
1941 "CPU%d failed\n", cpu
);
1945 static void hardware_disable(void *junk
)
1947 int cpu
= raw_smp_processor_id();
1949 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1951 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1952 kvm_arch_hardware_disable(NULL
);
1955 static void hardware_disable_all_nolock(void)
1957 BUG_ON(!kvm_usage_count
);
1960 if (!kvm_usage_count
)
1961 on_each_cpu(hardware_disable
, NULL
, 1);
1964 static void hardware_disable_all(void)
1966 spin_lock(&kvm_lock
);
1967 hardware_disable_all_nolock();
1968 spin_unlock(&kvm_lock
);
1971 static int hardware_enable_all(void)
1975 spin_lock(&kvm_lock
);
1978 if (kvm_usage_count
== 1) {
1979 atomic_set(&hardware_enable_failed
, 0);
1980 on_each_cpu(hardware_enable
, NULL
, 1);
1982 if (atomic_read(&hardware_enable_failed
)) {
1983 hardware_disable_all_nolock();
1988 spin_unlock(&kvm_lock
);
1993 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1998 if (!kvm_usage_count
)
2001 val
&= ~CPU_TASKS_FROZEN
;
2004 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2006 hardware_disable(NULL
);
2009 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2011 spin_lock(&kvm_lock
);
2012 hardware_enable(NULL
);
2013 spin_unlock(&kvm_lock
);
2020 asmlinkage
void kvm_handle_fault_on_reboot(void)
2022 if (kvm_rebooting
) {
2023 /* spin while reset goes on */
2028 /* Fault while not rebooting. We want the trace. */
2031 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
2033 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2037 * Some (well, at least mine) BIOSes hang on reboot if
2040 * And Intel TXT required VMX off for all cpu when system shutdown.
2042 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2043 kvm_rebooting
= true;
2044 on_each_cpu(hardware_disable
, NULL
, 1);
2048 static struct notifier_block kvm_reboot_notifier
= {
2049 .notifier_call
= kvm_reboot
,
2053 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2057 for (i
= 0; i
< bus
->dev_count
; i
++) {
2058 struct kvm_io_device
*pos
= bus
->devs
[i
];
2060 kvm_iodevice_destructor(pos
);
2065 /* kvm_io_bus_write - called under kvm->slots_lock */
2066 int kvm_io_bus_write(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2067 int len
, const void *val
)
2070 struct kvm_io_bus
*bus
;
2072 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2073 for (i
= 0; i
< bus
->dev_count
; i
++)
2074 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
2079 /* kvm_io_bus_read - called under kvm->slots_lock */
2080 int kvm_io_bus_read(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2084 struct kvm_io_bus
*bus
;
2086 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2087 for (i
= 0; i
< bus
->dev_count
; i
++)
2088 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
2093 /* Caller must hold slots_lock. */
2094 int kvm_io_bus_register_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2095 struct kvm_io_device
*dev
)
2097 struct kvm_io_bus
*new_bus
, *bus
;
2099 bus
= kvm
->buses
[bus_idx
];
2100 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2103 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2106 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2107 new_bus
->devs
[new_bus
->dev_count
++] = dev
;
2108 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2109 synchronize_srcu_expedited(&kvm
->srcu
);
2115 /* Caller must hold slots_lock. */
2116 int kvm_io_bus_unregister_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2117 struct kvm_io_device
*dev
)
2120 struct kvm_io_bus
*new_bus
, *bus
;
2122 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2126 bus
= kvm
->buses
[bus_idx
];
2127 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2130 for (i
= 0; i
< new_bus
->dev_count
; i
++)
2131 if (new_bus
->devs
[i
] == dev
) {
2133 new_bus
->devs
[i
] = new_bus
->devs
[--new_bus
->dev_count
];
2142 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2143 synchronize_srcu_expedited(&kvm
->srcu
);
2148 static struct notifier_block kvm_cpu_notifier
= {
2149 .notifier_call
= kvm_cpu_hotplug
,
2152 static int vm_stat_get(void *_offset
, u64
*val
)
2154 unsigned offset
= (long)_offset
;
2158 spin_lock(&kvm_lock
);
2159 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2160 *val
+= *(u32
*)((void *)kvm
+ offset
);
2161 spin_unlock(&kvm_lock
);
2165 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2167 static int vcpu_stat_get(void *_offset
, u64
*val
)
2169 unsigned offset
= (long)_offset
;
2171 struct kvm_vcpu
*vcpu
;
2175 spin_lock(&kvm_lock
);
2176 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2177 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2178 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2180 spin_unlock(&kvm_lock
);
2184 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2186 static const struct file_operations
*stat_fops
[] = {
2187 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2188 [KVM_STAT_VM
] = &vm_stat_fops
,
2191 static void kvm_init_debug(void)
2193 struct kvm_stats_debugfs_item
*p
;
2195 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2196 for (p
= debugfs_entries
; p
->name
; ++p
)
2197 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2198 (void *)(long)p
->offset
,
2199 stat_fops
[p
->kind
]);
2202 static void kvm_exit_debug(void)
2204 struct kvm_stats_debugfs_item
*p
;
2206 for (p
= debugfs_entries
; p
->name
; ++p
)
2207 debugfs_remove(p
->dentry
);
2208 debugfs_remove(kvm_debugfs_dir
);
2211 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2213 if (kvm_usage_count
)
2214 hardware_disable(NULL
);
2218 static int kvm_resume(struct sys_device
*dev
)
2220 if (kvm_usage_count
) {
2221 WARN_ON(spin_is_locked(&kvm_lock
));
2222 hardware_enable(NULL
);
2227 static struct sysdev_class kvm_sysdev_class
= {
2229 .suspend
= kvm_suspend
,
2230 .resume
= kvm_resume
,
2233 static struct sys_device kvm_sysdev
= {
2235 .cls
= &kvm_sysdev_class
,
2238 struct page
*bad_page
;
2242 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2244 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2247 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2249 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2251 kvm_arch_vcpu_load(vcpu
, cpu
);
2254 static void kvm_sched_out(struct preempt_notifier
*pn
,
2255 struct task_struct
*next
)
2257 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2259 kvm_arch_vcpu_put(vcpu
);
2262 int kvm_init(void *opaque
, unsigned vcpu_size
, unsigned vcpu_align
,
2263 struct module
*module
)
2268 r
= kvm_arch_init(opaque
);
2272 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2274 if (bad_page
== NULL
) {
2279 bad_pfn
= page_to_pfn(bad_page
);
2281 hwpoison_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2283 if (hwpoison_page
== NULL
) {
2288 hwpoison_pfn
= page_to_pfn(hwpoison_page
);
2290 fault_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2292 if (fault_page
== NULL
) {
2297 fault_pfn
= page_to_pfn(fault_page
);
2299 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2304 r
= kvm_arch_hardware_setup();
2308 for_each_online_cpu(cpu
) {
2309 smp_call_function_single(cpu
,
2310 kvm_arch_check_processor_compat
,
2316 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2319 register_reboot_notifier(&kvm_reboot_notifier
);
2321 r
= sysdev_class_register(&kvm_sysdev_class
);
2325 r
= sysdev_register(&kvm_sysdev
);
2329 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2331 vcpu_align
= __alignof__(struct kvm_vcpu
);
2332 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
, vcpu_align
,
2334 if (!kvm_vcpu_cache
) {
2339 kvm_chardev_ops
.owner
= module
;
2340 kvm_vm_fops
.owner
= module
;
2341 kvm_vcpu_fops
.owner
= module
;
2343 r
= misc_register(&kvm_dev
);
2345 printk(KERN_ERR
"kvm: misc device register failed\n");
2349 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2350 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2357 kmem_cache_destroy(kvm_vcpu_cache
);
2359 sysdev_unregister(&kvm_sysdev
);
2361 sysdev_class_unregister(&kvm_sysdev_class
);
2363 unregister_reboot_notifier(&kvm_reboot_notifier
);
2364 unregister_cpu_notifier(&kvm_cpu_notifier
);
2367 kvm_arch_hardware_unsetup();
2369 free_cpumask_var(cpus_hardware_enabled
);
2372 __free_page(fault_page
);
2374 __free_page(hwpoison_page
);
2375 __free_page(bad_page
);
2381 EXPORT_SYMBOL_GPL(kvm_init
);
2386 misc_deregister(&kvm_dev
);
2387 kmem_cache_destroy(kvm_vcpu_cache
);
2388 sysdev_unregister(&kvm_sysdev
);
2389 sysdev_class_unregister(&kvm_sysdev_class
);
2390 unregister_reboot_notifier(&kvm_reboot_notifier
);
2391 unregister_cpu_notifier(&kvm_cpu_notifier
);
2392 on_each_cpu(hardware_disable
, NULL
, 1);
2393 kvm_arch_hardware_unsetup();
2395 free_cpumask_var(cpus_hardware_enabled
);
2396 __free_page(hwpoison_page
);
2397 __free_page(bad_page
);
2399 EXPORT_SYMBOL_GPL(kvm_exit
);