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.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/percpu.h>
25 #include <linux/gfp.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>
49 #include <asm/processor.h>
51 #include <asm/uaccess.h>
52 #include <asm/pgtable.h>
53 #include <asm-generic/bitops/le.h>
55 #include "coalesced_mmio.h"
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/kvm.h>
60 MODULE_AUTHOR("Qumranet");
61 MODULE_LICENSE("GPL");
66 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
69 DEFINE_SPINLOCK(kvm_lock
);
72 static cpumask_var_t cpus_hardware_enabled
;
73 static int kvm_usage_count
= 0;
74 static atomic_t hardware_enable_failed
;
76 struct kmem_cache
*kvm_vcpu_cache
;
77 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
79 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
81 struct dentry
*kvm_debugfs_dir
;
83 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
85 static int hardware_enable_all(void);
86 static void hardware_disable_all(void);
88 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
);
90 static bool kvm_rebooting
;
92 static bool largepages_enabled
= true;
94 inline int kvm_is_mmio_pfn(pfn_t pfn
)
97 struct page
*page
= compound_head(pfn_to_page(pfn
));
98 return PageReserved(page
);
105 * Switches to specified vcpu, until a matching vcpu_put()
107 void vcpu_load(struct kvm_vcpu
*vcpu
)
111 mutex_lock(&vcpu
->mutex
);
113 preempt_notifier_register(&vcpu
->preempt_notifier
);
114 kvm_arch_vcpu_load(vcpu
, cpu
);
118 void vcpu_put(struct kvm_vcpu
*vcpu
)
121 kvm_arch_vcpu_put(vcpu
);
122 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
124 mutex_unlock(&vcpu
->mutex
);
127 static void ack_flush(void *_completed
)
131 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
136 struct kvm_vcpu
*vcpu
;
138 zalloc_cpumask_var(&cpus
, GFP_ATOMIC
);
140 spin_lock(&kvm
->requests_lock
);
141 me
= smp_processor_id();
142 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
143 if (test_and_set_bit(req
, &vcpu
->requests
))
146 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
147 cpumask_set_cpu(cpu
, cpus
);
149 if (unlikely(cpus
== NULL
))
150 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
151 else if (!cpumask_empty(cpus
))
152 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
155 spin_unlock(&kvm
->requests_lock
);
156 free_cpumask_var(cpus
);
160 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
162 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
163 ++kvm
->stat
.remote_tlb_flush
;
166 void kvm_reload_remote_mmus(struct kvm
*kvm
)
168 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
171 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
176 mutex_init(&vcpu
->mutex
);
180 init_waitqueue_head(&vcpu
->wq
);
182 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
187 vcpu
->run
= page_address(page
);
189 r
= kvm_arch_vcpu_init(vcpu
);
195 free_page((unsigned long)vcpu
->run
);
199 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
201 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
203 kvm_arch_vcpu_uninit(vcpu
);
204 free_page((unsigned long)vcpu
->run
);
206 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
208 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
209 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
211 return container_of(mn
, struct kvm
, mmu_notifier
);
214 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
215 struct mm_struct
*mm
,
216 unsigned long address
)
218 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
219 int need_tlb_flush
, idx
;
222 * When ->invalidate_page runs, the linux pte has been zapped
223 * already but the page is still allocated until
224 * ->invalidate_page returns. So if we increase the sequence
225 * here the kvm page fault will notice if the spte can't be
226 * established because the page is going to be freed. If
227 * instead the kvm page fault establishes the spte before
228 * ->invalidate_page runs, kvm_unmap_hva will release it
231 * The sequence increase only need to be seen at spin_unlock
232 * time, and not at spin_lock time.
234 * Increasing the sequence after the spin_unlock would be
235 * unsafe because the kvm page fault could then establish the
236 * pte after kvm_unmap_hva returned, without noticing the page
237 * is going to be freed.
239 idx
= srcu_read_lock(&kvm
->srcu
);
240 spin_lock(&kvm
->mmu_lock
);
241 kvm
->mmu_notifier_seq
++;
242 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
243 spin_unlock(&kvm
->mmu_lock
);
244 srcu_read_unlock(&kvm
->srcu
, idx
);
246 /* we've to flush the tlb before the pages can be freed */
248 kvm_flush_remote_tlbs(kvm
);
252 static void kvm_mmu_notifier_change_pte(struct mmu_notifier
*mn
,
253 struct mm_struct
*mm
,
254 unsigned long address
,
257 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
260 idx
= srcu_read_lock(&kvm
->srcu
);
261 spin_lock(&kvm
->mmu_lock
);
262 kvm
->mmu_notifier_seq
++;
263 kvm_set_spte_hva(kvm
, address
, pte
);
264 spin_unlock(&kvm
->mmu_lock
);
265 srcu_read_unlock(&kvm
->srcu
, idx
);
268 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
269 struct mm_struct
*mm
,
273 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
274 int need_tlb_flush
= 0, idx
;
276 idx
= srcu_read_lock(&kvm
->srcu
);
277 spin_lock(&kvm
->mmu_lock
);
279 * The count increase must become visible at unlock time as no
280 * spte can be established without taking the mmu_lock and
281 * count is also read inside the mmu_lock critical section.
283 kvm
->mmu_notifier_count
++;
284 for (; start
< end
; start
+= PAGE_SIZE
)
285 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
286 spin_unlock(&kvm
->mmu_lock
);
287 srcu_read_unlock(&kvm
->srcu
, idx
);
289 /* we've to flush the tlb before the pages can be freed */
291 kvm_flush_remote_tlbs(kvm
);
294 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
295 struct mm_struct
*mm
,
299 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
301 spin_lock(&kvm
->mmu_lock
);
303 * This sequence increase will notify the kvm page fault that
304 * the page that is going to be mapped in the spte could have
307 kvm
->mmu_notifier_seq
++;
309 * The above sequence increase must be visible before the
310 * below count decrease but both values are read by the kvm
311 * page fault under mmu_lock spinlock so we don't need to add
312 * a smb_wmb() here in between the two.
314 kvm
->mmu_notifier_count
--;
315 spin_unlock(&kvm
->mmu_lock
);
317 BUG_ON(kvm
->mmu_notifier_count
< 0);
320 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
321 struct mm_struct
*mm
,
322 unsigned long address
)
324 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
327 idx
= srcu_read_lock(&kvm
->srcu
);
328 spin_lock(&kvm
->mmu_lock
);
329 young
= kvm_age_hva(kvm
, address
);
330 spin_unlock(&kvm
->mmu_lock
);
331 srcu_read_unlock(&kvm
->srcu
, idx
);
334 kvm_flush_remote_tlbs(kvm
);
339 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
340 struct mm_struct
*mm
)
342 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
343 kvm_arch_flush_shadow(kvm
);
346 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
347 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
348 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
349 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
350 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
351 .change_pte
= kvm_mmu_notifier_change_pte
,
352 .release
= kvm_mmu_notifier_release
,
355 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
357 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
358 return mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
361 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
363 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
368 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
370 static struct kvm
*kvm_create_vm(void)
373 struct kvm
*kvm
= kvm_arch_create_vm();
374 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
381 r
= hardware_enable_all();
383 goto out_err_nodisable
;
385 #ifdef CONFIG_HAVE_KVM_IRQCHIP
386 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
387 INIT_HLIST_HEAD(&kvm
->irq_ack_notifier_list
);
391 kvm
->memslots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
394 if (init_srcu_struct(&kvm
->srcu
))
396 for (i
= 0; i
< KVM_NR_BUSES
; i
++) {
397 kvm
->buses
[i
] = kzalloc(sizeof(struct kvm_io_bus
),
399 if (!kvm
->buses
[i
]) {
400 cleanup_srcu_struct(&kvm
->srcu
);
405 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
406 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
408 cleanup_srcu_struct(&kvm
->srcu
);
412 kvm
->coalesced_mmio_ring
=
413 (struct kvm_coalesced_mmio_ring
*)page_address(page
);
416 r
= kvm_init_mmu_notifier(kvm
);
418 cleanup_srcu_struct(&kvm
->srcu
);
419 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
425 kvm
->mm
= current
->mm
;
426 atomic_inc(&kvm
->mm
->mm_count
);
427 spin_lock_init(&kvm
->mmu_lock
);
428 spin_lock_init(&kvm
->requests_lock
);
429 kvm_eventfd_init(kvm
);
430 mutex_init(&kvm
->lock
);
431 mutex_init(&kvm
->irq_lock
);
432 mutex_init(&kvm
->slots_lock
);
433 atomic_set(&kvm
->users_count
, 1);
434 spin_lock(&kvm_lock
);
435 list_add(&kvm
->vm_list
, &vm_list
);
436 spin_unlock(&kvm_lock
);
437 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
438 kvm_coalesced_mmio_init(kvm
);
444 hardware_disable_all();
446 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
447 kfree(kvm
->buses
[i
]);
448 kfree(kvm
->memslots
);
454 * Free any memory in @free but not in @dont.
456 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
457 struct kvm_memory_slot
*dont
)
461 if (!dont
|| free
->rmap
!= dont
->rmap
)
464 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
465 vfree(free
->dirty_bitmap
);
468 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
469 if (!dont
|| free
->lpage_info
[i
] != dont
->lpage_info
[i
]) {
470 vfree(free
->lpage_info
[i
]);
471 free
->lpage_info
[i
] = NULL
;
476 free
->dirty_bitmap
= NULL
;
480 void kvm_free_physmem(struct kvm
*kvm
)
483 struct kvm_memslots
*slots
= kvm
->memslots
;
485 for (i
= 0; i
< slots
->nmemslots
; ++i
)
486 kvm_free_physmem_slot(&slots
->memslots
[i
], NULL
);
488 kfree(kvm
->memslots
);
491 static void kvm_destroy_vm(struct kvm
*kvm
)
494 struct mm_struct
*mm
= kvm
->mm
;
496 kvm_arch_sync_events(kvm
);
497 spin_lock(&kvm_lock
);
498 list_del(&kvm
->vm_list
);
499 spin_unlock(&kvm_lock
);
500 kvm_free_irq_routing(kvm
);
501 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
502 kvm_io_bus_destroy(kvm
->buses
[i
]);
503 kvm_coalesced_mmio_free(kvm
);
504 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
505 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
507 kvm_arch_flush_shadow(kvm
);
509 kvm_arch_destroy_vm(kvm
);
510 hardware_disable_all();
514 void kvm_get_kvm(struct kvm
*kvm
)
516 atomic_inc(&kvm
->users_count
);
518 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
520 void kvm_put_kvm(struct kvm
*kvm
)
522 if (atomic_dec_and_test(&kvm
->users_count
))
525 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
528 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
530 struct kvm
*kvm
= filp
->private_data
;
532 kvm_irqfd_release(kvm
);
539 * Allocate some memory and give it an address in the guest physical address
542 * Discontiguous memory is allowed, mostly for framebuffers.
544 * Must be called holding mmap_sem for write.
546 int __kvm_set_memory_region(struct kvm
*kvm
,
547 struct kvm_userspace_memory_region
*mem
,
550 int r
, flush_shadow
= 0;
552 unsigned long npages
;
554 struct kvm_memory_slot
*memslot
;
555 struct kvm_memory_slot old
, new;
556 struct kvm_memslots
*slots
, *old_memslots
;
559 /* General sanity checks */
560 if (mem
->memory_size
& (PAGE_SIZE
- 1))
562 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
564 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
566 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
568 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
571 memslot
= &kvm
->memslots
->memslots
[mem
->slot
];
572 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
573 npages
= mem
->memory_size
>> PAGE_SHIFT
;
576 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
578 new = old
= *memslot
;
580 new.base_gfn
= base_gfn
;
582 new.flags
= mem
->flags
;
584 /* Disallow changing a memory slot's size. */
586 if (npages
&& old
.npages
&& npages
!= old
.npages
)
589 /* Check for overlaps */
591 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
592 struct kvm_memory_slot
*s
= &kvm
->memslots
->memslots
[i
];
594 if (s
== memslot
|| !s
->npages
)
596 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
597 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
601 /* Free page dirty bitmap if unneeded */
602 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
603 new.dirty_bitmap
= NULL
;
607 /* Allocate if a slot is being created */
609 if (npages
&& !new.rmap
) {
610 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
615 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
617 new.user_alloc
= user_alloc
;
618 new.userspace_addr
= mem
->userspace_addr
;
623 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
629 /* Avoid unused variable warning if no large pages */
632 if (new.lpage_info
[i
])
635 lpages
= 1 + (base_gfn
+ npages
- 1) /
636 KVM_PAGES_PER_HPAGE(level
);
637 lpages
-= base_gfn
/ KVM_PAGES_PER_HPAGE(level
);
639 new.lpage_info
[i
] = vmalloc(lpages
* sizeof(*new.lpage_info
[i
]));
641 if (!new.lpage_info
[i
])
644 memset(new.lpage_info
[i
], 0,
645 lpages
* sizeof(*new.lpage_info
[i
]));
647 if (base_gfn
% KVM_PAGES_PER_HPAGE(level
))
648 new.lpage_info
[i
][0].write_count
= 1;
649 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE(level
))
650 new.lpage_info
[i
][lpages
- 1].write_count
= 1;
651 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
653 * If the gfn and userspace address are not aligned wrt each
654 * other, or if explicitly asked to, disable large page
655 * support for this slot
657 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE(level
) - 1) ||
659 for (j
= 0; j
< lpages
; ++j
)
660 new.lpage_info
[i
][j
].write_count
= 1;
665 /* Allocate page dirty bitmap if needed */
666 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
667 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
669 new.dirty_bitmap
= vmalloc(dirty_bytes
);
670 if (!new.dirty_bitmap
)
672 memset(new.dirty_bitmap
, 0, dirty_bytes
);
673 /* destroy any largepage mappings for dirty tracking */
677 #else /* not defined CONFIG_S390 */
678 new.user_alloc
= user_alloc
;
680 new.userspace_addr
= mem
->userspace_addr
;
681 #endif /* not defined CONFIG_S390 */
685 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
688 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
689 if (mem
->slot
>= slots
->nmemslots
)
690 slots
->nmemslots
= mem
->slot
+ 1;
691 slots
->memslots
[mem
->slot
].flags
|= KVM_MEMSLOT_INVALID
;
693 old_memslots
= kvm
->memslots
;
694 rcu_assign_pointer(kvm
->memslots
, slots
);
695 synchronize_srcu_expedited(&kvm
->srcu
);
696 /* From this point no new shadow pages pointing to a deleted
697 * memslot will be created.
699 * validation of sp->gfn happens in:
700 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
701 * - kvm_is_visible_gfn (mmu_check_roots)
703 kvm_arch_flush_shadow(kvm
);
707 r
= kvm_arch_prepare_memory_region(kvm
, &new, old
, mem
, user_alloc
);
712 /* map the pages in iommu page table */
714 r
= kvm_iommu_map_pages(kvm
, &new);
721 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
724 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
725 if (mem
->slot
>= slots
->nmemslots
)
726 slots
->nmemslots
= mem
->slot
+ 1;
728 /* actual memory is freed via old in kvm_free_physmem_slot below */
731 new.dirty_bitmap
= NULL
;
732 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
)
733 new.lpage_info
[i
] = NULL
;
736 slots
->memslots
[mem
->slot
] = new;
737 old_memslots
= kvm
->memslots
;
738 rcu_assign_pointer(kvm
->memslots
, slots
);
739 synchronize_srcu_expedited(&kvm
->srcu
);
741 kvm_arch_commit_memory_region(kvm
, mem
, old
, user_alloc
);
743 kvm_free_physmem_slot(&old
, &new);
747 kvm_arch_flush_shadow(kvm
);
752 kvm_free_physmem_slot(&new, &old
);
757 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
759 int kvm_set_memory_region(struct kvm
*kvm
,
760 struct kvm_userspace_memory_region
*mem
,
765 mutex_lock(&kvm
->slots_lock
);
766 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
767 mutex_unlock(&kvm
->slots_lock
);
770 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
772 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
774 kvm_userspace_memory_region
*mem
,
777 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
779 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
782 int kvm_get_dirty_log(struct kvm
*kvm
,
783 struct kvm_dirty_log
*log
, int *is_dirty
)
785 struct kvm_memory_slot
*memslot
;
788 unsigned long any
= 0;
791 if (log
->slot
>= KVM_MEMORY_SLOTS
)
794 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
796 if (!memslot
->dirty_bitmap
)
799 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
801 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
802 any
= memslot
->dirty_bitmap
[i
];
805 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
816 void kvm_disable_largepages(void)
818 largepages_enabled
= false;
820 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
822 int is_error_page(struct page
*page
)
824 return page
== bad_page
;
826 EXPORT_SYMBOL_GPL(is_error_page
);
828 int is_error_pfn(pfn_t pfn
)
830 return pfn
== bad_pfn
;
832 EXPORT_SYMBOL_GPL(is_error_pfn
);
834 static inline unsigned long bad_hva(void)
839 int kvm_is_error_hva(unsigned long addr
)
841 return addr
== bad_hva();
843 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
845 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
848 struct kvm_memslots
*slots
= rcu_dereference(kvm
->memslots
);
850 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
851 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
853 if (gfn
>= memslot
->base_gfn
854 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
859 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
861 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
863 gfn
= unalias_gfn(kvm
, gfn
);
864 return gfn_to_memslot_unaliased(kvm
, gfn
);
867 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
870 struct kvm_memslots
*slots
= rcu_dereference(kvm
->memslots
);
872 gfn
= unalias_gfn_instantiation(kvm
, gfn
);
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 int memslot_id(struct kvm
*kvm
, gfn_t gfn
)
890 struct kvm_memslots
*slots
= rcu_dereference(kvm
->memslots
);
891 struct kvm_memory_slot
*memslot
= NULL
;
893 gfn
= unalias_gfn(kvm
, gfn
);
894 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
895 memslot
= &slots
->memslots
[i
];
897 if (gfn
>= memslot
->base_gfn
898 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
902 return memslot
- slots
->memslots
;
905 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
907 struct kvm_memory_slot
*slot
;
909 gfn
= unalias_gfn_instantiation(kvm
, gfn
);
910 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
911 if (!slot
|| slot
->flags
& KVM_MEMSLOT_INVALID
)
913 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
915 EXPORT_SYMBOL_GPL(gfn_to_hva
);
917 static pfn_t
hva_to_pfn(struct kvm
*kvm
, unsigned long addr
)
919 struct page
*page
[1];
925 npages
= get_user_pages_fast(addr
, 1, 1, page
);
927 if (unlikely(npages
!= 1)) {
928 struct vm_area_struct
*vma
;
930 down_read(¤t
->mm
->mmap_sem
);
931 vma
= find_vma(current
->mm
, addr
);
933 if (vma
== NULL
|| addr
< vma
->vm_start
||
934 !(vma
->vm_flags
& VM_PFNMAP
)) {
935 up_read(¤t
->mm
->mmap_sem
);
937 return page_to_pfn(bad_page
);
940 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
941 up_read(¤t
->mm
->mmap_sem
);
942 BUG_ON(!kvm_is_mmio_pfn(pfn
));
944 pfn
= page_to_pfn(page
[0]);
949 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
953 addr
= gfn_to_hva(kvm
, gfn
);
954 if (kvm_is_error_hva(addr
)) {
956 return page_to_pfn(bad_page
);
959 return hva_to_pfn(kvm
, addr
);
961 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
963 static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot
*slot
, gfn_t gfn
)
965 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
968 pfn_t
gfn_to_pfn_memslot(struct kvm
*kvm
,
969 struct kvm_memory_slot
*slot
, gfn_t gfn
)
971 unsigned long addr
= gfn_to_hva_memslot(slot
, gfn
);
972 return hva_to_pfn(kvm
, addr
);
975 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
979 pfn
= gfn_to_pfn(kvm
, gfn
);
980 if (!kvm_is_mmio_pfn(pfn
))
981 return pfn_to_page(pfn
);
983 WARN_ON(kvm_is_mmio_pfn(pfn
));
989 EXPORT_SYMBOL_GPL(gfn_to_page
);
991 void kvm_release_page_clean(struct page
*page
)
993 kvm_release_pfn_clean(page_to_pfn(page
));
995 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
997 void kvm_release_pfn_clean(pfn_t pfn
)
999 if (!kvm_is_mmio_pfn(pfn
))
1000 put_page(pfn_to_page(pfn
));
1002 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1004 void kvm_release_page_dirty(struct page
*page
)
1006 kvm_release_pfn_dirty(page_to_pfn(page
));
1008 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1010 void kvm_release_pfn_dirty(pfn_t pfn
)
1012 kvm_set_pfn_dirty(pfn
);
1013 kvm_release_pfn_clean(pfn
);
1015 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1017 void kvm_set_page_dirty(struct page
*page
)
1019 kvm_set_pfn_dirty(page_to_pfn(page
));
1021 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1023 void kvm_set_pfn_dirty(pfn_t pfn
)
1025 if (!kvm_is_mmio_pfn(pfn
)) {
1026 struct page
*page
= pfn_to_page(pfn
);
1027 if (!PageReserved(page
))
1031 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1033 void kvm_set_pfn_accessed(pfn_t pfn
)
1035 if (!kvm_is_mmio_pfn(pfn
))
1036 mark_page_accessed(pfn_to_page(pfn
));
1038 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1040 void kvm_get_pfn(pfn_t pfn
)
1042 if (!kvm_is_mmio_pfn(pfn
))
1043 get_page(pfn_to_page(pfn
));
1045 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1047 static int next_segment(unsigned long len
, int offset
)
1049 if (len
> PAGE_SIZE
- offset
)
1050 return PAGE_SIZE
- offset
;
1055 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1061 addr
= gfn_to_hva(kvm
, gfn
);
1062 if (kvm_is_error_hva(addr
))
1064 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1069 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1071 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1073 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1075 int offset
= offset_in_page(gpa
);
1078 while ((seg
= next_segment(len
, offset
)) != 0) {
1079 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1089 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1091 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1096 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1097 int offset
= offset_in_page(gpa
);
1099 addr
= gfn_to_hva(kvm
, gfn
);
1100 if (kvm_is_error_hva(addr
))
1102 pagefault_disable();
1103 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1109 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1111 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1112 int offset
, int len
)
1117 addr
= gfn_to_hva(kvm
, gfn
);
1118 if (kvm_is_error_hva(addr
))
1120 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1123 mark_page_dirty(kvm
, gfn
);
1126 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1128 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1131 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1133 int offset
= offset_in_page(gpa
);
1136 while ((seg
= next_segment(len
, offset
)) != 0) {
1137 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1148 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1150 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1152 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1154 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1156 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1158 int offset
= offset_in_page(gpa
);
1161 while ((seg
= next_segment(len
, offset
)) != 0) {
1162 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1171 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1173 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1175 struct kvm_memory_slot
*memslot
;
1177 gfn
= unalias_gfn(kvm
, gfn
);
1178 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1179 if (memslot
&& memslot
->dirty_bitmap
) {
1180 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1183 if (!generic_test_le_bit(rel_gfn
, memslot
->dirty_bitmap
))
1184 generic___set_le_bit(rel_gfn
, memslot
->dirty_bitmap
);
1189 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1191 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1196 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1198 if (kvm_arch_vcpu_runnable(vcpu
)) {
1199 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1202 if (kvm_cpu_has_pending_timer(vcpu
))
1204 if (signal_pending(current
))
1210 finish_wait(&vcpu
->wq
, &wait
);
1213 void kvm_resched(struct kvm_vcpu
*vcpu
)
1215 if (!need_resched())
1219 EXPORT_SYMBOL_GPL(kvm_resched
);
1221 void kvm_vcpu_on_spin(struct kvm_vcpu
*vcpu
)
1226 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1228 /* Sleep for 100 us, and hope lock-holder got scheduled */
1229 expires
= ktime_add_ns(ktime_get(), 100000UL);
1230 schedule_hrtimeout(&expires
, HRTIMER_MODE_ABS
);
1232 finish_wait(&vcpu
->wq
, &wait
);
1234 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1236 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1238 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1241 if (vmf
->pgoff
== 0)
1242 page
= virt_to_page(vcpu
->run
);
1244 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1245 page
= virt_to_page(vcpu
->arch
.pio_data
);
1247 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1248 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1249 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1252 return VM_FAULT_SIGBUS
;
1258 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1259 .fault
= kvm_vcpu_fault
,
1262 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1264 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1268 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1270 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1272 kvm_put_kvm(vcpu
->kvm
);
1276 static struct file_operations kvm_vcpu_fops
= {
1277 .release
= kvm_vcpu_release
,
1278 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1279 .compat_ioctl
= kvm_vcpu_ioctl
,
1280 .mmap
= kvm_vcpu_mmap
,
1284 * Allocates an inode for the vcpu.
1286 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1288 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, O_RDWR
);
1292 * Creates some virtual cpus. Good luck creating more than one.
1294 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1297 struct kvm_vcpu
*vcpu
, *v
;
1299 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1301 return PTR_ERR(vcpu
);
1303 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1305 r
= kvm_arch_vcpu_setup(vcpu
);
1309 mutex_lock(&kvm
->lock
);
1310 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1315 kvm_for_each_vcpu(r
, v
, kvm
)
1316 if (v
->vcpu_id
== id
) {
1321 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1323 /* Now it's all set up, let userspace reach it */
1325 r
= create_vcpu_fd(vcpu
);
1331 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1333 atomic_inc(&kvm
->online_vcpus
);
1335 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1336 if (kvm
->bsp_vcpu_id
== id
)
1337 kvm
->bsp_vcpu
= vcpu
;
1339 mutex_unlock(&kvm
->lock
);
1343 mutex_unlock(&kvm
->lock
);
1344 kvm_arch_vcpu_destroy(vcpu
);
1348 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1351 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1352 vcpu
->sigset_active
= 1;
1353 vcpu
->sigset
= *sigset
;
1355 vcpu
->sigset_active
= 0;
1359 static long kvm_vcpu_ioctl(struct file
*filp
,
1360 unsigned int ioctl
, unsigned long arg
)
1362 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1363 void __user
*argp
= (void __user
*)arg
;
1365 struct kvm_fpu
*fpu
= NULL
;
1366 struct kvm_sregs
*kvm_sregs
= NULL
;
1368 if (vcpu
->kvm
->mm
!= current
->mm
)
1375 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1377 case KVM_GET_REGS
: {
1378 struct kvm_regs
*kvm_regs
;
1381 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1384 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1388 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1395 case KVM_SET_REGS
: {
1396 struct kvm_regs
*kvm_regs
;
1399 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1403 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1405 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1413 case KVM_GET_SREGS
: {
1414 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1418 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1422 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1427 case KVM_SET_SREGS
: {
1428 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1433 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1435 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1441 case KVM_GET_MP_STATE
: {
1442 struct kvm_mp_state mp_state
;
1444 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1448 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1453 case KVM_SET_MP_STATE
: {
1454 struct kvm_mp_state mp_state
;
1457 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1459 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1465 case KVM_TRANSLATE
: {
1466 struct kvm_translation tr
;
1469 if (copy_from_user(&tr
, argp
, sizeof tr
))
1471 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1475 if (copy_to_user(argp
, &tr
, sizeof tr
))
1480 case KVM_SET_GUEST_DEBUG
: {
1481 struct kvm_guest_debug dbg
;
1484 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1486 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1492 case KVM_SET_SIGNAL_MASK
: {
1493 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1494 struct kvm_signal_mask kvm_sigmask
;
1495 sigset_t sigset
, *p
;
1500 if (copy_from_user(&kvm_sigmask
, argp
,
1501 sizeof kvm_sigmask
))
1504 if (kvm_sigmask
.len
!= sizeof sigset
)
1507 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1512 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1516 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1520 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1524 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1530 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1535 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1537 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1544 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1552 static long kvm_vm_ioctl(struct file
*filp
,
1553 unsigned int ioctl
, unsigned long arg
)
1555 struct kvm
*kvm
= filp
->private_data
;
1556 void __user
*argp
= (void __user
*)arg
;
1559 if (kvm
->mm
!= current
->mm
)
1562 case KVM_CREATE_VCPU
:
1563 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1567 case KVM_SET_USER_MEMORY_REGION
: {
1568 struct kvm_userspace_memory_region kvm_userspace_mem
;
1571 if (copy_from_user(&kvm_userspace_mem
, argp
,
1572 sizeof kvm_userspace_mem
))
1575 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1580 case KVM_GET_DIRTY_LOG
: {
1581 struct kvm_dirty_log log
;
1584 if (copy_from_user(&log
, argp
, sizeof log
))
1586 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1591 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1592 case KVM_REGISTER_COALESCED_MMIO
: {
1593 struct kvm_coalesced_mmio_zone zone
;
1595 if (copy_from_user(&zone
, argp
, sizeof zone
))
1598 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1604 case KVM_UNREGISTER_COALESCED_MMIO
: {
1605 struct kvm_coalesced_mmio_zone zone
;
1607 if (copy_from_user(&zone
, argp
, sizeof zone
))
1610 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1618 struct kvm_irqfd data
;
1621 if (copy_from_user(&data
, argp
, sizeof data
))
1623 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
1626 case KVM_IOEVENTFD
: {
1627 struct kvm_ioeventfd data
;
1630 if (copy_from_user(&data
, argp
, sizeof data
))
1632 r
= kvm_ioeventfd(kvm
, &data
);
1635 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1636 case KVM_SET_BOOT_CPU_ID
:
1638 mutex_lock(&kvm
->lock
);
1639 if (atomic_read(&kvm
->online_vcpus
) != 0)
1642 kvm
->bsp_vcpu_id
= arg
;
1643 mutex_unlock(&kvm
->lock
);
1647 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1649 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
1655 #ifdef CONFIG_COMPAT
1656 struct compat_kvm_dirty_log
{
1660 compat_uptr_t dirty_bitmap
; /* one bit per page */
1665 static long kvm_vm_compat_ioctl(struct file
*filp
,
1666 unsigned int ioctl
, unsigned long arg
)
1668 struct kvm
*kvm
= filp
->private_data
;
1671 if (kvm
->mm
!= current
->mm
)
1674 case KVM_GET_DIRTY_LOG
: {
1675 struct compat_kvm_dirty_log compat_log
;
1676 struct kvm_dirty_log log
;
1679 if (copy_from_user(&compat_log
, (void __user
*)arg
,
1680 sizeof(compat_log
)))
1682 log
.slot
= compat_log
.slot
;
1683 log
.padding1
= compat_log
.padding1
;
1684 log
.padding2
= compat_log
.padding2
;
1685 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
1687 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1693 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
1701 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1703 struct page
*page
[1];
1706 gfn_t gfn
= vmf
->pgoff
;
1707 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1709 addr
= gfn_to_hva(kvm
, gfn
);
1710 if (kvm_is_error_hva(addr
))
1711 return VM_FAULT_SIGBUS
;
1713 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1715 if (unlikely(npages
!= 1))
1716 return VM_FAULT_SIGBUS
;
1718 vmf
->page
= page
[0];
1722 static const struct vm_operations_struct kvm_vm_vm_ops
= {
1723 .fault
= kvm_vm_fault
,
1726 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1728 vma
->vm_ops
= &kvm_vm_vm_ops
;
1732 static struct file_operations kvm_vm_fops
= {
1733 .release
= kvm_vm_release
,
1734 .unlocked_ioctl
= kvm_vm_ioctl
,
1735 #ifdef CONFIG_COMPAT
1736 .compat_ioctl
= kvm_vm_compat_ioctl
,
1738 .mmap
= kvm_vm_mmap
,
1741 static int kvm_dev_ioctl_create_vm(void)
1746 kvm
= kvm_create_vm();
1748 return PTR_ERR(kvm
);
1749 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, O_RDWR
);
1756 static long kvm_dev_ioctl_check_extension_generic(long arg
)
1759 case KVM_CAP_USER_MEMORY
:
1760 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
1761 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
1762 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1763 case KVM_CAP_SET_BOOT_CPU_ID
:
1765 case KVM_CAP_INTERNAL_ERROR_DATA
:
1767 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1768 case KVM_CAP_IRQ_ROUTING
:
1769 return KVM_MAX_IRQ_ROUTES
;
1774 return kvm_dev_ioctl_check_extension(arg
);
1777 static long kvm_dev_ioctl(struct file
*filp
,
1778 unsigned int ioctl
, unsigned long arg
)
1783 case KVM_GET_API_VERSION
:
1787 r
= KVM_API_VERSION
;
1793 r
= kvm_dev_ioctl_create_vm();
1795 case KVM_CHECK_EXTENSION
:
1796 r
= kvm_dev_ioctl_check_extension_generic(arg
);
1798 case KVM_GET_VCPU_MMAP_SIZE
:
1802 r
= PAGE_SIZE
; /* struct kvm_run */
1804 r
+= PAGE_SIZE
; /* pio data page */
1806 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1807 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1810 case KVM_TRACE_ENABLE
:
1811 case KVM_TRACE_PAUSE
:
1812 case KVM_TRACE_DISABLE
:
1816 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1822 static struct file_operations kvm_chardev_ops
= {
1823 .unlocked_ioctl
= kvm_dev_ioctl
,
1824 .compat_ioctl
= kvm_dev_ioctl
,
1827 static struct miscdevice kvm_dev
= {
1833 static void hardware_enable(void *junk
)
1835 int cpu
= raw_smp_processor_id();
1838 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1841 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
1843 r
= kvm_arch_hardware_enable(NULL
);
1846 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1847 atomic_inc(&hardware_enable_failed
);
1848 printk(KERN_INFO
"kvm: enabling virtualization on "
1849 "CPU%d failed\n", cpu
);
1853 static void hardware_disable(void *junk
)
1855 int cpu
= raw_smp_processor_id();
1857 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1859 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1860 kvm_arch_hardware_disable(NULL
);
1863 static void hardware_disable_all_nolock(void)
1865 BUG_ON(!kvm_usage_count
);
1868 if (!kvm_usage_count
)
1869 on_each_cpu(hardware_disable
, NULL
, 1);
1872 static void hardware_disable_all(void)
1874 spin_lock(&kvm_lock
);
1875 hardware_disable_all_nolock();
1876 spin_unlock(&kvm_lock
);
1879 static int hardware_enable_all(void)
1883 spin_lock(&kvm_lock
);
1886 if (kvm_usage_count
== 1) {
1887 atomic_set(&hardware_enable_failed
, 0);
1888 on_each_cpu(hardware_enable
, NULL
, 1);
1890 if (atomic_read(&hardware_enable_failed
)) {
1891 hardware_disable_all_nolock();
1896 spin_unlock(&kvm_lock
);
1901 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1906 if (!kvm_usage_count
)
1909 val
&= ~CPU_TASKS_FROZEN
;
1912 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1914 hardware_disable(NULL
);
1916 case CPU_UP_CANCELED
:
1917 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1919 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
1922 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1924 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
1931 asmlinkage
void kvm_handle_fault_on_reboot(void)
1934 /* spin while reset goes on */
1937 /* Fault while not rebooting. We want the trace. */
1940 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
1942 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1946 * Some (well, at least mine) BIOSes hang on reboot if
1949 * And Intel TXT required VMX off for all cpu when system shutdown.
1951 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1952 kvm_rebooting
= true;
1953 on_each_cpu(hardware_disable
, NULL
, 1);
1957 static struct notifier_block kvm_reboot_notifier
= {
1958 .notifier_call
= kvm_reboot
,
1962 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
1966 for (i
= 0; i
< bus
->dev_count
; i
++) {
1967 struct kvm_io_device
*pos
= bus
->devs
[i
];
1969 kvm_iodevice_destructor(pos
);
1974 /* kvm_io_bus_write - called under kvm->slots_lock */
1975 int kvm_io_bus_write(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
1976 int len
, const void *val
)
1979 struct kvm_io_bus
*bus
= rcu_dereference(kvm
->buses
[bus_idx
]);
1980 for (i
= 0; i
< bus
->dev_count
; i
++)
1981 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
1986 /* kvm_io_bus_read - called under kvm->slots_lock */
1987 int kvm_io_bus_read(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
1991 struct kvm_io_bus
*bus
= rcu_dereference(kvm
->buses
[bus_idx
]);
1993 for (i
= 0; i
< bus
->dev_count
; i
++)
1994 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
1999 /* Caller must hold slots_lock. */
2000 int kvm_io_bus_register_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2001 struct kvm_io_device
*dev
)
2003 struct kvm_io_bus
*new_bus
, *bus
;
2005 bus
= kvm
->buses
[bus_idx
];
2006 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2009 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2012 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2013 new_bus
->devs
[new_bus
->dev_count
++] = dev
;
2014 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2015 synchronize_srcu_expedited(&kvm
->srcu
);
2021 /* Caller must hold slots_lock. */
2022 int kvm_io_bus_unregister_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2023 struct kvm_io_device
*dev
)
2026 struct kvm_io_bus
*new_bus
, *bus
;
2028 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2032 bus
= kvm
->buses
[bus_idx
];
2033 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2036 for (i
= 0; i
< new_bus
->dev_count
; i
++)
2037 if (new_bus
->devs
[i
] == dev
) {
2039 new_bus
->devs
[i
] = new_bus
->devs
[--new_bus
->dev_count
];
2048 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2049 synchronize_srcu_expedited(&kvm
->srcu
);
2054 static struct notifier_block kvm_cpu_notifier
= {
2055 .notifier_call
= kvm_cpu_hotplug
,
2056 .priority
= 20, /* must be > scheduler priority */
2059 static int vm_stat_get(void *_offset
, u64
*val
)
2061 unsigned offset
= (long)_offset
;
2065 spin_lock(&kvm_lock
);
2066 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2067 *val
+= *(u32
*)((void *)kvm
+ offset
);
2068 spin_unlock(&kvm_lock
);
2072 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2074 static int vcpu_stat_get(void *_offset
, u64
*val
)
2076 unsigned offset
= (long)_offset
;
2078 struct kvm_vcpu
*vcpu
;
2082 spin_lock(&kvm_lock
);
2083 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2084 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2085 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2087 spin_unlock(&kvm_lock
);
2091 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2093 static const struct file_operations
*stat_fops
[] = {
2094 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2095 [KVM_STAT_VM
] = &vm_stat_fops
,
2098 static void kvm_init_debug(void)
2100 struct kvm_stats_debugfs_item
*p
;
2102 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2103 for (p
= debugfs_entries
; p
->name
; ++p
)
2104 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2105 (void *)(long)p
->offset
,
2106 stat_fops
[p
->kind
]);
2109 static void kvm_exit_debug(void)
2111 struct kvm_stats_debugfs_item
*p
;
2113 for (p
= debugfs_entries
; p
->name
; ++p
)
2114 debugfs_remove(p
->dentry
);
2115 debugfs_remove(kvm_debugfs_dir
);
2118 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2120 if (kvm_usage_count
)
2121 hardware_disable(NULL
);
2125 static int kvm_resume(struct sys_device
*dev
)
2127 if (kvm_usage_count
)
2128 hardware_enable(NULL
);
2132 static struct sysdev_class kvm_sysdev_class
= {
2134 .suspend
= kvm_suspend
,
2135 .resume
= kvm_resume
,
2138 static struct sys_device kvm_sysdev
= {
2140 .cls
= &kvm_sysdev_class
,
2143 struct page
*bad_page
;
2147 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2149 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2152 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2154 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2156 kvm_arch_vcpu_load(vcpu
, cpu
);
2159 static void kvm_sched_out(struct preempt_notifier
*pn
,
2160 struct task_struct
*next
)
2162 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2164 kvm_arch_vcpu_put(vcpu
);
2167 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2168 struct module
*module
)
2173 r
= kvm_arch_init(opaque
);
2177 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2179 if (bad_page
== NULL
) {
2184 bad_pfn
= page_to_pfn(bad_page
);
2186 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2191 r
= kvm_arch_hardware_setup();
2195 for_each_online_cpu(cpu
) {
2196 smp_call_function_single(cpu
,
2197 kvm_arch_check_processor_compat
,
2203 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2206 register_reboot_notifier(&kvm_reboot_notifier
);
2208 r
= sysdev_class_register(&kvm_sysdev_class
);
2212 r
= sysdev_register(&kvm_sysdev
);
2216 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2217 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2218 __alignof__(struct kvm_vcpu
),
2220 if (!kvm_vcpu_cache
) {
2225 kvm_chardev_ops
.owner
= module
;
2226 kvm_vm_fops
.owner
= module
;
2227 kvm_vcpu_fops
.owner
= module
;
2229 r
= misc_register(&kvm_dev
);
2231 printk(KERN_ERR
"kvm: misc device register failed\n");
2235 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2236 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2243 kmem_cache_destroy(kvm_vcpu_cache
);
2245 sysdev_unregister(&kvm_sysdev
);
2247 sysdev_class_unregister(&kvm_sysdev_class
);
2249 unregister_reboot_notifier(&kvm_reboot_notifier
);
2250 unregister_cpu_notifier(&kvm_cpu_notifier
);
2253 kvm_arch_hardware_unsetup();
2255 free_cpumask_var(cpus_hardware_enabled
);
2257 __free_page(bad_page
);
2263 EXPORT_SYMBOL_GPL(kvm_init
);
2267 tracepoint_synchronize_unregister();
2269 misc_deregister(&kvm_dev
);
2270 kmem_cache_destroy(kvm_vcpu_cache
);
2271 sysdev_unregister(&kvm_sysdev
);
2272 sysdev_class_unregister(&kvm_sysdev_class
);
2273 unregister_reboot_notifier(&kvm_reboot_notifier
);
2274 unregister_cpu_notifier(&kvm_cpu_notifier
);
2275 on_each_cpu(hardware_disable
, NULL
, 1);
2276 kvm_arch_hardware_unsetup();
2278 free_cpumask_var(cpus_hardware_enabled
);
2279 __free_page(bad_page
);
2281 EXPORT_SYMBOL_GPL(kvm_exit
);