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>
48 #include <linux/hugetlb.h>
50 #include <asm/processor.h>
52 #include <asm/uaccess.h>
53 #include <asm/pgtable.h>
54 #include <asm-generic/bitops/le.h>
56 #include "coalesced_mmio.h"
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/kvm.h>
61 MODULE_AUTHOR("Qumranet");
62 MODULE_LICENSE("GPL");
67 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
70 DEFINE_SPINLOCK(kvm_lock
);
73 static cpumask_var_t cpus_hardware_enabled
;
74 static int kvm_usage_count
= 0;
75 static atomic_t hardware_enable_failed
;
77 struct kmem_cache
*kvm_vcpu_cache
;
78 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
80 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
82 struct dentry
*kvm_debugfs_dir
;
84 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
86 static int hardware_enable_all(void);
87 static void hardware_disable_all(void);
89 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
);
91 static bool kvm_rebooting
;
93 static bool largepages_enabled
= true;
95 inline int kvm_is_mmio_pfn(pfn_t pfn
)
98 struct page
*page
= compound_head(pfn_to_page(pfn
));
99 return PageReserved(page
);
106 * Switches to specified vcpu, until a matching vcpu_put()
108 void vcpu_load(struct kvm_vcpu
*vcpu
)
112 mutex_lock(&vcpu
->mutex
);
114 preempt_notifier_register(&vcpu
->preempt_notifier
);
115 kvm_arch_vcpu_load(vcpu
, cpu
);
119 void vcpu_put(struct kvm_vcpu
*vcpu
)
122 kvm_arch_vcpu_put(vcpu
);
123 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
125 mutex_unlock(&vcpu
->mutex
);
128 static void ack_flush(void *_completed
)
132 static bool make_all_cpus_request(struct kvm
*kvm
, unsigned int req
)
137 struct kvm_vcpu
*vcpu
;
139 zalloc_cpumask_var(&cpus
, GFP_ATOMIC
);
141 raw_spin_lock(&kvm
->requests_lock
);
142 me
= smp_processor_id();
143 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
144 if (test_and_set_bit(req
, &vcpu
->requests
))
147 if (cpus
!= NULL
&& cpu
!= -1 && cpu
!= me
)
148 cpumask_set_cpu(cpu
, cpus
);
150 if (unlikely(cpus
== NULL
))
151 smp_call_function_many(cpu_online_mask
, ack_flush
, NULL
, 1);
152 else if (!cpumask_empty(cpus
))
153 smp_call_function_many(cpus
, ack_flush
, NULL
, 1);
156 raw_spin_unlock(&kvm
->requests_lock
);
157 free_cpumask_var(cpus
);
161 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
163 if (make_all_cpus_request(kvm
, KVM_REQ_TLB_FLUSH
))
164 ++kvm
->stat
.remote_tlb_flush
;
167 void kvm_reload_remote_mmus(struct kvm
*kvm
)
169 make_all_cpus_request(kvm
, KVM_REQ_MMU_RELOAD
);
172 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
177 mutex_init(&vcpu
->mutex
);
181 init_waitqueue_head(&vcpu
->wq
);
183 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
188 vcpu
->run
= page_address(page
);
190 r
= kvm_arch_vcpu_init(vcpu
);
196 free_page((unsigned long)vcpu
->run
);
200 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
202 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
204 kvm_arch_vcpu_uninit(vcpu
);
205 free_page((unsigned long)vcpu
->run
);
207 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
209 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
210 static inline struct kvm
*mmu_notifier_to_kvm(struct mmu_notifier
*mn
)
212 return container_of(mn
, struct kvm
, mmu_notifier
);
215 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
216 struct mm_struct
*mm
,
217 unsigned long address
)
219 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
220 int need_tlb_flush
, idx
;
223 * When ->invalidate_page runs, the linux pte has been zapped
224 * already but the page is still allocated until
225 * ->invalidate_page returns. So if we increase the sequence
226 * here the kvm page fault will notice if the spte can't be
227 * established because the page is going to be freed. If
228 * instead the kvm page fault establishes the spte before
229 * ->invalidate_page runs, kvm_unmap_hva will release it
232 * The sequence increase only need to be seen at spin_unlock
233 * time, and not at spin_lock time.
235 * Increasing the sequence after the spin_unlock would be
236 * unsafe because the kvm page fault could then establish the
237 * pte after kvm_unmap_hva returned, without noticing the page
238 * is going to be freed.
240 idx
= srcu_read_lock(&kvm
->srcu
);
241 spin_lock(&kvm
->mmu_lock
);
242 kvm
->mmu_notifier_seq
++;
243 need_tlb_flush
= kvm_unmap_hva(kvm
, address
);
244 spin_unlock(&kvm
->mmu_lock
);
245 srcu_read_unlock(&kvm
->srcu
, idx
);
247 /* we've to flush the tlb before the pages can be freed */
249 kvm_flush_remote_tlbs(kvm
);
253 static void kvm_mmu_notifier_change_pte(struct mmu_notifier
*mn
,
254 struct mm_struct
*mm
,
255 unsigned long address
,
258 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
261 idx
= srcu_read_lock(&kvm
->srcu
);
262 spin_lock(&kvm
->mmu_lock
);
263 kvm
->mmu_notifier_seq
++;
264 kvm_set_spte_hva(kvm
, address
, pte
);
265 spin_unlock(&kvm
->mmu_lock
);
266 srcu_read_unlock(&kvm
->srcu
, idx
);
269 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
270 struct mm_struct
*mm
,
274 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
275 int need_tlb_flush
= 0, idx
;
277 idx
= srcu_read_lock(&kvm
->srcu
);
278 spin_lock(&kvm
->mmu_lock
);
280 * The count increase must become visible at unlock time as no
281 * spte can be established without taking the mmu_lock and
282 * count is also read inside the mmu_lock critical section.
284 kvm
->mmu_notifier_count
++;
285 for (; start
< end
; start
+= PAGE_SIZE
)
286 need_tlb_flush
|= kvm_unmap_hva(kvm
, start
);
287 spin_unlock(&kvm
->mmu_lock
);
288 srcu_read_unlock(&kvm
->srcu
, idx
);
290 /* we've to flush the tlb before the pages can be freed */
292 kvm_flush_remote_tlbs(kvm
);
295 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
296 struct mm_struct
*mm
,
300 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
302 spin_lock(&kvm
->mmu_lock
);
304 * This sequence increase will notify the kvm page fault that
305 * the page that is going to be mapped in the spte could have
308 kvm
->mmu_notifier_seq
++;
310 * The above sequence increase must be visible before the
311 * below count decrease but both values are read by the kvm
312 * page fault under mmu_lock spinlock so we don't need to add
313 * a smb_wmb() here in between the two.
315 kvm
->mmu_notifier_count
--;
316 spin_unlock(&kvm
->mmu_lock
);
318 BUG_ON(kvm
->mmu_notifier_count
< 0);
321 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier
*mn
,
322 struct mm_struct
*mm
,
323 unsigned long address
)
325 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
328 idx
= srcu_read_lock(&kvm
->srcu
);
329 spin_lock(&kvm
->mmu_lock
);
330 young
= kvm_age_hva(kvm
, address
);
331 spin_unlock(&kvm
->mmu_lock
);
332 srcu_read_unlock(&kvm
->srcu
, idx
);
335 kvm_flush_remote_tlbs(kvm
);
340 static void kvm_mmu_notifier_release(struct mmu_notifier
*mn
,
341 struct mm_struct
*mm
)
343 struct kvm
*kvm
= mmu_notifier_to_kvm(mn
);
344 kvm_arch_flush_shadow(kvm
);
347 static const struct mmu_notifier_ops kvm_mmu_notifier_ops
= {
348 .invalidate_page
= kvm_mmu_notifier_invalidate_page
,
349 .invalidate_range_start
= kvm_mmu_notifier_invalidate_range_start
,
350 .invalidate_range_end
= kvm_mmu_notifier_invalidate_range_end
,
351 .clear_flush_young
= kvm_mmu_notifier_clear_flush_young
,
352 .change_pte
= kvm_mmu_notifier_change_pte
,
353 .release
= kvm_mmu_notifier_release
,
356 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
358 kvm
->mmu_notifier
.ops
= &kvm_mmu_notifier_ops
;
359 return mmu_notifier_register(&kvm
->mmu_notifier
, current
->mm
);
362 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
364 static int kvm_init_mmu_notifier(struct kvm
*kvm
)
369 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
371 static struct kvm
*kvm_create_vm(void)
374 struct kvm
*kvm
= kvm_arch_create_vm();
379 r
= hardware_enable_all();
381 goto out_err_nodisable
;
383 #ifdef CONFIG_HAVE_KVM_IRQCHIP
384 INIT_HLIST_HEAD(&kvm
->mask_notifier_list
);
385 INIT_HLIST_HEAD(&kvm
->irq_ack_notifier_list
);
389 kvm
->memslots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
392 if (init_srcu_struct(&kvm
->srcu
))
394 for (i
= 0; i
< KVM_NR_BUSES
; i
++) {
395 kvm
->buses
[i
] = kzalloc(sizeof(struct kvm_io_bus
),
397 if (!kvm
->buses
[i
]) {
398 cleanup_srcu_struct(&kvm
->srcu
);
403 r
= kvm_init_mmu_notifier(kvm
);
405 cleanup_srcu_struct(&kvm
->srcu
);
409 kvm
->mm
= current
->mm
;
410 atomic_inc(&kvm
->mm
->mm_count
);
411 spin_lock_init(&kvm
->mmu_lock
);
412 raw_spin_lock_init(&kvm
->requests_lock
);
413 kvm_eventfd_init(kvm
);
414 mutex_init(&kvm
->lock
);
415 mutex_init(&kvm
->irq_lock
);
416 mutex_init(&kvm
->slots_lock
);
417 atomic_set(&kvm
->users_count
, 1);
418 spin_lock(&kvm_lock
);
419 list_add(&kvm
->vm_list
, &vm_list
);
420 spin_unlock(&kvm_lock
);
421 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
422 kvm_coalesced_mmio_init(kvm
);
428 hardware_disable_all();
430 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
431 kfree(kvm
->buses
[i
]);
432 kfree(kvm
->memslots
);
438 * Free any memory in @free but not in @dont.
440 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
441 struct kvm_memory_slot
*dont
)
445 if (!dont
|| free
->rmap
!= dont
->rmap
)
448 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
449 vfree(free
->dirty_bitmap
);
452 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
453 if (!dont
|| free
->lpage_info
[i
] != dont
->lpage_info
[i
]) {
454 vfree(free
->lpage_info
[i
]);
455 free
->lpage_info
[i
] = NULL
;
460 free
->dirty_bitmap
= NULL
;
464 void kvm_free_physmem(struct kvm
*kvm
)
467 struct kvm_memslots
*slots
= kvm
->memslots
;
469 for (i
= 0; i
< slots
->nmemslots
; ++i
)
470 kvm_free_physmem_slot(&slots
->memslots
[i
], NULL
);
472 kfree(kvm
->memslots
);
475 static void kvm_destroy_vm(struct kvm
*kvm
)
478 struct mm_struct
*mm
= kvm
->mm
;
480 kvm_arch_sync_events(kvm
);
481 spin_lock(&kvm_lock
);
482 list_del(&kvm
->vm_list
);
483 spin_unlock(&kvm_lock
);
484 kvm_free_irq_routing(kvm
);
485 for (i
= 0; i
< KVM_NR_BUSES
; i
++)
486 kvm_io_bus_destroy(kvm
->buses
[i
]);
487 kvm_coalesced_mmio_free(kvm
);
488 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
489 mmu_notifier_unregister(&kvm
->mmu_notifier
, kvm
->mm
);
491 kvm_arch_flush_shadow(kvm
);
493 kvm_arch_destroy_vm(kvm
);
494 hardware_disable_all();
498 void kvm_get_kvm(struct kvm
*kvm
)
500 atomic_inc(&kvm
->users_count
);
502 EXPORT_SYMBOL_GPL(kvm_get_kvm
);
504 void kvm_put_kvm(struct kvm
*kvm
)
506 if (atomic_dec_and_test(&kvm
->users_count
))
509 EXPORT_SYMBOL_GPL(kvm_put_kvm
);
512 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
514 struct kvm
*kvm
= filp
->private_data
;
516 kvm_irqfd_release(kvm
);
523 * Allocate some memory and give it an address in the guest physical address
526 * Discontiguous memory is allowed, mostly for framebuffers.
528 * Must be called holding mmap_sem for write.
530 int __kvm_set_memory_region(struct kvm
*kvm
,
531 struct kvm_userspace_memory_region
*mem
,
534 int r
, flush_shadow
= 0;
536 unsigned long npages
;
538 struct kvm_memory_slot
*memslot
;
539 struct kvm_memory_slot old
, new;
540 struct kvm_memslots
*slots
, *old_memslots
;
543 /* General sanity checks */
544 if (mem
->memory_size
& (PAGE_SIZE
- 1))
546 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
548 if (user_alloc
&& (mem
->userspace_addr
& (PAGE_SIZE
- 1)))
550 if (mem
->slot
>= KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
)
552 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
555 memslot
= &kvm
->memslots
->memslots
[mem
->slot
];
556 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
557 npages
= mem
->memory_size
>> PAGE_SHIFT
;
560 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
562 new = old
= *memslot
;
564 new.base_gfn
= base_gfn
;
566 new.flags
= mem
->flags
;
568 /* Disallow changing a memory slot's size. */
570 if (npages
&& old
.npages
&& npages
!= old
.npages
)
573 /* Check for overlaps */
575 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
576 struct kvm_memory_slot
*s
= &kvm
->memslots
->memslots
[i
];
578 if (s
== memslot
|| !s
->npages
)
580 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
581 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
585 /* Free page dirty bitmap if unneeded */
586 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
587 new.dirty_bitmap
= NULL
;
591 /* Allocate if a slot is being created */
593 if (npages
&& !new.rmap
) {
594 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
599 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
601 new.user_alloc
= user_alloc
;
602 new.userspace_addr
= mem
->userspace_addr
;
607 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
) {
613 /* Avoid unused variable warning if no large pages */
616 if (new.lpage_info
[i
])
619 lpages
= 1 + (base_gfn
+ npages
- 1) /
620 KVM_PAGES_PER_HPAGE(level
);
621 lpages
-= base_gfn
/ KVM_PAGES_PER_HPAGE(level
);
623 new.lpage_info
[i
] = vmalloc(lpages
* sizeof(*new.lpage_info
[i
]));
625 if (!new.lpage_info
[i
])
628 memset(new.lpage_info
[i
], 0,
629 lpages
* sizeof(*new.lpage_info
[i
]));
631 if (base_gfn
% KVM_PAGES_PER_HPAGE(level
))
632 new.lpage_info
[i
][0].write_count
= 1;
633 if ((base_gfn
+npages
) % KVM_PAGES_PER_HPAGE(level
))
634 new.lpage_info
[i
][lpages
- 1].write_count
= 1;
635 ugfn
= new.userspace_addr
>> PAGE_SHIFT
;
637 * If the gfn and userspace address are not aligned wrt each
638 * other, or if explicitly asked to, disable large page
639 * support for this slot
641 if ((base_gfn
^ ugfn
) & (KVM_PAGES_PER_HPAGE(level
) - 1) ||
643 for (j
= 0; j
< lpages
; ++j
)
644 new.lpage_info
[i
][j
].write_count
= 1;
649 /* Allocate page dirty bitmap if needed */
650 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
651 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
653 new.dirty_bitmap
= vmalloc(dirty_bytes
);
654 if (!new.dirty_bitmap
)
656 memset(new.dirty_bitmap
, 0, dirty_bytes
);
657 /* destroy any largepage mappings for dirty tracking */
661 #else /* not defined CONFIG_S390 */
662 new.user_alloc
= user_alloc
;
664 new.userspace_addr
= mem
->userspace_addr
;
665 #endif /* not defined CONFIG_S390 */
669 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
672 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
673 if (mem
->slot
>= slots
->nmemslots
)
674 slots
->nmemslots
= mem
->slot
+ 1;
675 slots
->memslots
[mem
->slot
].flags
|= KVM_MEMSLOT_INVALID
;
677 old_memslots
= kvm
->memslots
;
678 rcu_assign_pointer(kvm
->memslots
, slots
);
679 synchronize_srcu_expedited(&kvm
->srcu
);
680 /* From this point no new shadow pages pointing to a deleted
681 * memslot will be created.
683 * validation of sp->gfn happens in:
684 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
685 * - kvm_is_visible_gfn (mmu_check_roots)
687 kvm_arch_flush_shadow(kvm
);
691 r
= kvm_arch_prepare_memory_region(kvm
, &new, old
, mem
, user_alloc
);
696 /* map the pages in iommu page table */
698 r
= kvm_iommu_map_pages(kvm
, &new);
705 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
708 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
709 if (mem
->slot
>= slots
->nmemslots
)
710 slots
->nmemslots
= mem
->slot
+ 1;
712 /* actual memory is freed via old in kvm_free_physmem_slot below */
715 new.dirty_bitmap
= NULL
;
716 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
)
717 new.lpage_info
[i
] = NULL
;
720 slots
->memslots
[mem
->slot
] = new;
721 old_memslots
= kvm
->memslots
;
722 rcu_assign_pointer(kvm
->memslots
, slots
);
723 synchronize_srcu_expedited(&kvm
->srcu
);
725 kvm_arch_commit_memory_region(kvm
, mem
, old
, user_alloc
);
727 kvm_free_physmem_slot(&old
, &new);
731 kvm_arch_flush_shadow(kvm
);
736 kvm_free_physmem_slot(&new, &old
);
741 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
743 int kvm_set_memory_region(struct kvm
*kvm
,
744 struct kvm_userspace_memory_region
*mem
,
749 mutex_lock(&kvm
->slots_lock
);
750 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
751 mutex_unlock(&kvm
->slots_lock
);
754 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
756 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
758 kvm_userspace_memory_region
*mem
,
761 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
763 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
766 int kvm_get_dirty_log(struct kvm
*kvm
,
767 struct kvm_dirty_log
*log
, int *is_dirty
)
769 struct kvm_memory_slot
*memslot
;
772 unsigned long any
= 0;
775 if (log
->slot
>= KVM_MEMORY_SLOTS
)
778 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
780 if (!memslot
->dirty_bitmap
)
783 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
785 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
786 any
= memslot
->dirty_bitmap
[i
];
789 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
800 void kvm_disable_largepages(void)
802 largepages_enabled
= false;
804 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
806 int is_error_page(struct page
*page
)
808 return page
== bad_page
;
810 EXPORT_SYMBOL_GPL(is_error_page
);
812 int is_error_pfn(pfn_t pfn
)
814 return pfn
== bad_pfn
;
816 EXPORT_SYMBOL_GPL(is_error_pfn
);
818 static inline unsigned long bad_hva(void)
823 int kvm_is_error_hva(unsigned long addr
)
825 return addr
== bad_hva();
827 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
829 struct kvm_memory_slot
*gfn_to_memslot_unaliased(struct kvm
*kvm
, gfn_t gfn
)
832 struct kvm_memslots
*slots
= rcu_dereference(kvm
->memslots
);
834 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
835 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
837 if (gfn
>= memslot
->base_gfn
838 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
843 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased
);
845 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
847 gfn
= unalias_gfn(kvm
, gfn
);
848 return gfn_to_memslot_unaliased(kvm
, gfn
);
851 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
854 struct kvm_memslots
*slots
= rcu_dereference(kvm
->memslots
);
856 gfn
= unalias_gfn_instantiation(kvm
, gfn
);
857 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
858 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
860 if (memslot
->flags
& KVM_MEMSLOT_INVALID
)
863 if (gfn
>= memslot
->base_gfn
864 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
869 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
871 unsigned long kvm_host_page_size(struct kvm
*kvm
, gfn_t gfn
)
873 struct vm_area_struct
*vma
;
874 unsigned long addr
, size
;
878 addr
= gfn_to_hva(kvm
, gfn
);
879 if (kvm_is_error_hva(addr
))
882 down_read(¤t
->mm
->mmap_sem
);
883 vma
= find_vma(current
->mm
, addr
);
887 size
= vma_kernel_pagesize(vma
);
890 up_read(¤t
->mm
->mmap_sem
);
895 int memslot_id(struct kvm
*kvm
, gfn_t gfn
)
898 struct kvm_memslots
*slots
= rcu_dereference(kvm
->memslots
);
899 struct kvm_memory_slot
*memslot
= NULL
;
901 gfn
= unalias_gfn(kvm
, gfn
);
902 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
903 memslot
= &slots
->memslots
[i
];
905 if (gfn
>= memslot
->base_gfn
906 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
910 return memslot
- slots
->memslots
;
913 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
915 struct kvm_memory_slot
*slot
;
917 gfn
= unalias_gfn_instantiation(kvm
, gfn
);
918 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
919 if (!slot
|| slot
->flags
& KVM_MEMSLOT_INVALID
)
921 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
923 EXPORT_SYMBOL_GPL(gfn_to_hva
);
925 static pfn_t
hva_to_pfn(struct kvm
*kvm
, unsigned long addr
)
927 struct page
*page
[1];
933 npages
= get_user_pages_fast(addr
, 1, 1, page
);
935 if (unlikely(npages
!= 1)) {
936 struct vm_area_struct
*vma
;
938 down_read(¤t
->mm
->mmap_sem
);
939 vma
= find_vma(current
->mm
, addr
);
941 if (vma
== NULL
|| addr
< vma
->vm_start
||
942 !(vma
->vm_flags
& VM_PFNMAP
)) {
943 up_read(¤t
->mm
->mmap_sem
);
945 return page_to_pfn(bad_page
);
948 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
949 up_read(¤t
->mm
->mmap_sem
);
950 BUG_ON(!kvm_is_mmio_pfn(pfn
));
952 pfn
= page_to_pfn(page
[0]);
957 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
961 addr
= gfn_to_hva(kvm
, gfn
);
962 if (kvm_is_error_hva(addr
)) {
964 return page_to_pfn(bad_page
);
967 return hva_to_pfn(kvm
, addr
);
969 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
971 static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot
*slot
, gfn_t gfn
)
973 return (slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
);
976 pfn_t
gfn_to_pfn_memslot(struct kvm
*kvm
,
977 struct kvm_memory_slot
*slot
, gfn_t gfn
)
979 unsigned long addr
= gfn_to_hva_memslot(slot
, gfn
);
980 return hva_to_pfn(kvm
, addr
);
983 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
987 pfn
= gfn_to_pfn(kvm
, gfn
);
988 if (!kvm_is_mmio_pfn(pfn
))
989 return pfn_to_page(pfn
);
991 WARN_ON(kvm_is_mmio_pfn(pfn
));
997 EXPORT_SYMBOL_GPL(gfn_to_page
);
999 void kvm_release_page_clean(struct page
*page
)
1001 kvm_release_pfn_clean(page_to_pfn(page
));
1003 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1005 void kvm_release_pfn_clean(pfn_t pfn
)
1007 if (!kvm_is_mmio_pfn(pfn
))
1008 put_page(pfn_to_page(pfn
));
1010 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1012 void kvm_release_page_dirty(struct page
*page
)
1014 kvm_release_pfn_dirty(page_to_pfn(page
));
1016 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1018 void kvm_release_pfn_dirty(pfn_t pfn
)
1020 kvm_set_pfn_dirty(pfn
);
1021 kvm_release_pfn_clean(pfn
);
1023 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1025 void kvm_set_page_dirty(struct page
*page
)
1027 kvm_set_pfn_dirty(page_to_pfn(page
));
1029 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1031 void kvm_set_pfn_dirty(pfn_t pfn
)
1033 if (!kvm_is_mmio_pfn(pfn
)) {
1034 struct page
*page
= pfn_to_page(pfn
);
1035 if (!PageReserved(page
))
1039 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1041 void kvm_set_pfn_accessed(pfn_t pfn
)
1043 if (!kvm_is_mmio_pfn(pfn
))
1044 mark_page_accessed(pfn_to_page(pfn
));
1046 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1048 void kvm_get_pfn(pfn_t pfn
)
1050 if (!kvm_is_mmio_pfn(pfn
))
1051 get_page(pfn_to_page(pfn
));
1053 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1055 static int next_segment(unsigned long len
, int offset
)
1057 if (len
> PAGE_SIZE
- offset
)
1058 return PAGE_SIZE
- offset
;
1063 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1069 addr
= gfn_to_hva(kvm
, gfn
);
1070 if (kvm_is_error_hva(addr
))
1072 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1077 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1079 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1081 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1083 int offset
= offset_in_page(gpa
);
1086 while ((seg
= next_segment(len
, offset
)) != 0) {
1087 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1097 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1099 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1104 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1105 int offset
= offset_in_page(gpa
);
1107 addr
= gfn_to_hva(kvm
, gfn
);
1108 if (kvm_is_error_hva(addr
))
1110 pagefault_disable();
1111 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1117 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1119 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1120 int offset
, int len
)
1125 addr
= gfn_to_hva(kvm
, gfn
);
1126 if (kvm_is_error_hva(addr
))
1128 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1131 mark_page_dirty(kvm
, gfn
);
1134 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1136 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1139 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1141 int offset
= offset_in_page(gpa
);
1144 while ((seg
= next_segment(len
, offset
)) != 0) {
1145 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1156 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1158 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1160 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1162 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1164 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1166 int offset
= offset_in_page(gpa
);
1169 while ((seg
= next_segment(len
, offset
)) != 0) {
1170 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1179 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1181 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1183 struct kvm_memory_slot
*memslot
;
1185 gfn
= unalias_gfn(kvm
, gfn
);
1186 memslot
= gfn_to_memslot_unaliased(kvm
, gfn
);
1187 if (memslot
&& memslot
->dirty_bitmap
) {
1188 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1191 if (!generic_test_le_bit(rel_gfn
, memslot
->dirty_bitmap
))
1192 generic___set_le_bit(rel_gfn
, memslot
->dirty_bitmap
);
1197 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1199 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1204 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1206 if (kvm_arch_vcpu_runnable(vcpu
)) {
1207 set_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
1210 if (kvm_cpu_has_pending_timer(vcpu
))
1212 if (signal_pending(current
))
1218 finish_wait(&vcpu
->wq
, &wait
);
1221 void kvm_resched(struct kvm_vcpu
*vcpu
)
1223 if (!need_resched())
1227 EXPORT_SYMBOL_GPL(kvm_resched
);
1229 void kvm_vcpu_on_spin(struct kvm_vcpu
*vcpu
)
1234 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1236 /* Sleep for 100 us, and hope lock-holder got scheduled */
1237 expires
= ktime_add_ns(ktime_get(), 100000UL);
1238 schedule_hrtimeout(&expires
, HRTIMER_MODE_ABS
);
1240 finish_wait(&vcpu
->wq
, &wait
);
1242 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1244 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1246 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1249 if (vmf
->pgoff
== 0)
1250 page
= virt_to_page(vcpu
->run
);
1252 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1253 page
= virt_to_page(vcpu
->arch
.pio_data
);
1255 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1256 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1257 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1260 return VM_FAULT_SIGBUS
;
1266 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1267 .fault
= kvm_vcpu_fault
,
1270 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1272 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1276 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1278 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1280 kvm_put_kvm(vcpu
->kvm
);
1284 static struct file_operations kvm_vcpu_fops
= {
1285 .release
= kvm_vcpu_release
,
1286 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1287 .compat_ioctl
= kvm_vcpu_ioctl
,
1288 .mmap
= kvm_vcpu_mmap
,
1292 * Allocates an inode for the vcpu.
1294 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1296 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, O_RDWR
);
1300 * Creates some virtual cpus. Good luck creating more than one.
1302 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1305 struct kvm_vcpu
*vcpu
, *v
;
1307 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1309 return PTR_ERR(vcpu
);
1311 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1313 r
= kvm_arch_vcpu_setup(vcpu
);
1317 mutex_lock(&kvm
->lock
);
1318 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1323 kvm_for_each_vcpu(r
, v
, kvm
)
1324 if (v
->vcpu_id
== id
) {
1329 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1331 /* Now it's all set up, let userspace reach it */
1333 r
= create_vcpu_fd(vcpu
);
1339 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1341 atomic_inc(&kvm
->online_vcpus
);
1343 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1344 if (kvm
->bsp_vcpu_id
== id
)
1345 kvm
->bsp_vcpu
= vcpu
;
1347 mutex_unlock(&kvm
->lock
);
1351 mutex_unlock(&kvm
->lock
);
1352 kvm_arch_vcpu_destroy(vcpu
);
1356 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1359 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1360 vcpu
->sigset_active
= 1;
1361 vcpu
->sigset
= *sigset
;
1363 vcpu
->sigset_active
= 0;
1367 static long kvm_vcpu_ioctl(struct file
*filp
,
1368 unsigned int ioctl
, unsigned long arg
)
1370 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1371 void __user
*argp
= (void __user
*)arg
;
1373 struct kvm_fpu
*fpu
= NULL
;
1374 struct kvm_sregs
*kvm_sregs
= NULL
;
1376 if (vcpu
->kvm
->mm
!= current
->mm
)
1383 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1385 case KVM_GET_REGS
: {
1386 struct kvm_regs
*kvm_regs
;
1389 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1392 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1396 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1403 case KVM_SET_REGS
: {
1404 struct kvm_regs
*kvm_regs
;
1407 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1411 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1413 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1421 case KVM_GET_SREGS
: {
1422 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1426 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1430 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1435 case KVM_SET_SREGS
: {
1436 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1441 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1443 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1449 case KVM_GET_MP_STATE
: {
1450 struct kvm_mp_state mp_state
;
1452 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1456 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1461 case KVM_SET_MP_STATE
: {
1462 struct kvm_mp_state mp_state
;
1465 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1467 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1473 case KVM_TRANSLATE
: {
1474 struct kvm_translation tr
;
1477 if (copy_from_user(&tr
, argp
, sizeof tr
))
1479 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1483 if (copy_to_user(argp
, &tr
, sizeof tr
))
1488 case KVM_SET_GUEST_DEBUG
: {
1489 struct kvm_guest_debug dbg
;
1492 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1494 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1500 case KVM_SET_SIGNAL_MASK
: {
1501 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1502 struct kvm_signal_mask kvm_sigmask
;
1503 sigset_t sigset
, *p
;
1508 if (copy_from_user(&kvm_sigmask
, argp
,
1509 sizeof kvm_sigmask
))
1512 if (kvm_sigmask
.len
!= sizeof sigset
)
1515 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1520 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
1524 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1528 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1532 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1538 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1543 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1545 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1552 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1560 static long kvm_vm_ioctl(struct file
*filp
,
1561 unsigned int ioctl
, unsigned long arg
)
1563 struct kvm
*kvm
= filp
->private_data
;
1564 void __user
*argp
= (void __user
*)arg
;
1567 if (kvm
->mm
!= current
->mm
)
1570 case KVM_CREATE_VCPU
:
1571 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1575 case KVM_SET_USER_MEMORY_REGION
: {
1576 struct kvm_userspace_memory_region kvm_userspace_mem
;
1579 if (copy_from_user(&kvm_userspace_mem
, argp
,
1580 sizeof kvm_userspace_mem
))
1583 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1588 case KVM_GET_DIRTY_LOG
: {
1589 struct kvm_dirty_log log
;
1592 if (copy_from_user(&log
, argp
, sizeof log
))
1594 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1599 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1600 case KVM_REGISTER_COALESCED_MMIO
: {
1601 struct kvm_coalesced_mmio_zone zone
;
1603 if (copy_from_user(&zone
, argp
, sizeof zone
))
1606 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1612 case KVM_UNREGISTER_COALESCED_MMIO
: {
1613 struct kvm_coalesced_mmio_zone zone
;
1615 if (copy_from_user(&zone
, argp
, sizeof zone
))
1618 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1626 struct kvm_irqfd data
;
1629 if (copy_from_user(&data
, argp
, sizeof data
))
1631 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
1634 case KVM_IOEVENTFD
: {
1635 struct kvm_ioeventfd data
;
1638 if (copy_from_user(&data
, argp
, sizeof data
))
1640 r
= kvm_ioeventfd(kvm
, &data
);
1643 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1644 case KVM_SET_BOOT_CPU_ID
:
1646 mutex_lock(&kvm
->lock
);
1647 if (atomic_read(&kvm
->online_vcpus
) != 0)
1650 kvm
->bsp_vcpu_id
= arg
;
1651 mutex_unlock(&kvm
->lock
);
1655 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1657 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
1663 #ifdef CONFIG_COMPAT
1664 struct compat_kvm_dirty_log
{
1668 compat_uptr_t dirty_bitmap
; /* one bit per page */
1673 static long kvm_vm_compat_ioctl(struct file
*filp
,
1674 unsigned int ioctl
, unsigned long arg
)
1676 struct kvm
*kvm
= filp
->private_data
;
1679 if (kvm
->mm
!= current
->mm
)
1682 case KVM_GET_DIRTY_LOG
: {
1683 struct compat_kvm_dirty_log compat_log
;
1684 struct kvm_dirty_log log
;
1687 if (copy_from_user(&compat_log
, (void __user
*)arg
,
1688 sizeof(compat_log
)))
1690 log
.slot
= compat_log
.slot
;
1691 log
.padding1
= compat_log
.padding1
;
1692 log
.padding2
= compat_log
.padding2
;
1693 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
1695 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1701 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
1709 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1711 struct page
*page
[1];
1714 gfn_t gfn
= vmf
->pgoff
;
1715 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1717 addr
= gfn_to_hva(kvm
, gfn
);
1718 if (kvm_is_error_hva(addr
))
1719 return VM_FAULT_SIGBUS
;
1721 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1723 if (unlikely(npages
!= 1))
1724 return VM_FAULT_SIGBUS
;
1726 vmf
->page
= page
[0];
1730 static const struct vm_operations_struct kvm_vm_vm_ops
= {
1731 .fault
= kvm_vm_fault
,
1734 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1736 vma
->vm_ops
= &kvm_vm_vm_ops
;
1740 static struct file_operations kvm_vm_fops
= {
1741 .release
= kvm_vm_release
,
1742 .unlocked_ioctl
= kvm_vm_ioctl
,
1743 #ifdef CONFIG_COMPAT
1744 .compat_ioctl
= kvm_vm_compat_ioctl
,
1746 .mmap
= kvm_vm_mmap
,
1749 static int kvm_dev_ioctl_create_vm(void)
1754 kvm
= kvm_create_vm();
1756 return PTR_ERR(kvm
);
1757 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, O_RDWR
);
1764 static long kvm_dev_ioctl_check_extension_generic(long arg
)
1767 case KVM_CAP_USER_MEMORY
:
1768 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
1769 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
1770 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1771 case KVM_CAP_SET_BOOT_CPU_ID
:
1773 case KVM_CAP_INTERNAL_ERROR_DATA
:
1775 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1776 case KVM_CAP_IRQ_ROUTING
:
1777 return KVM_MAX_IRQ_ROUTES
;
1782 return kvm_dev_ioctl_check_extension(arg
);
1785 static long kvm_dev_ioctl(struct file
*filp
,
1786 unsigned int ioctl
, unsigned long arg
)
1791 case KVM_GET_API_VERSION
:
1795 r
= KVM_API_VERSION
;
1801 r
= kvm_dev_ioctl_create_vm();
1803 case KVM_CHECK_EXTENSION
:
1804 r
= kvm_dev_ioctl_check_extension_generic(arg
);
1806 case KVM_GET_VCPU_MMAP_SIZE
:
1810 r
= PAGE_SIZE
; /* struct kvm_run */
1812 r
+= PAGE_SIZE
; /* pio data page */
1814 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1815 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1818 case KVM_TRACE_ENABLE
:
1819 case KVM_TRACE_PAUSE
:
1820 case KVM_TRACE_DISABLE
:
1824 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1830 static struct file_operations kvm_chardev_ops
= {
1831 .unlocked_ioctl
= kvm_dev_ioctl
,
1832 .compat_ioctl
= kvm_dev_ioctl
,
1835 static struct miscdevice kvm_dev
= {
1841 static void hardware_enable(void *junk
)
1843 int cpu
= raw_smp_processor_id();
1846 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1849 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
1851 r
= kvm_arch_hardware_enable(NULL
);
1854 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1855 atomic_inc(&hardware_enable_failed
);
1856 printk(KERN_INFO
"kvm: enabling virtualization on "
1857 "CPU%d failed\n", cpu
);
1861 static void hardware_disable(void *junk
)
1863 int cpu
= raw_smp_processor_id();
1865 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1867 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1868 kvm_arch_hardware_disable(NULL
);
1871 static void hardware_disable_all_nolock(void)
1873 BUG_ON(!kvm_usage_count
);
1876 if (!kvm_usage_count
)
1877 on_each_cpu(hardware_disable
, NULL
, 1);
1880 static void hardware_disable_all(void)
1882 spin_lock(&kvm_lock
);
1883 hardware_disable_all_nolock();
1884 spin_unlock(&kvm_lock
);
1887 static int hardware_enable_all(void)
1891 spin_lock(&kvm_lock
);
1894 if (kvm_usage_count
== 1) {
1895 atomic_set(&hardware_enable_failed
, 0);
1896 on_each_cpu(hardware_enable
, NULL
, 1);
1898 if (atomic_read(&hardware_enable_failed
)) {
1899 hardware_disable_all_nolock();
1904 spin_unlock(&kvm_lock
);
1909 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1914 if (!kvm_usage_count
)
1917 val
&= ~CPU_TASKS_FROZEN
;
1920 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1922 hardware_disable(NULL
);
1924 case CPU_UP_CANCELED
:
1925 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1927 smp_call_function_single(cpu
, hardware_disable
, NULL
, 1);
1930 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1932 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
1939 asmlinkage
void kvm_handle_fault_on_reboot(void)
1942 /* spin while reset goes on */
1945 /* Fault while not rebooting. We want the trace. */
1948 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
1950 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1954 * Some (well, at least mine) BIOSes hang on reboot if
1957 * And Intel TXT required VMX off for all cpu when system shutdown.
1959 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1960 kvm_rebooting
= true;
1961 on_each_cpu(hardware_disable
, NULL
, 1);
1965 static struct notifier_block kvm_reboot_notifier
= {
1966 .notifier_call
= kvm_reboot
,
1970 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
1974 for (i
= 0; i
< bus
->dev_count
; i
++) {
1975 struct kvm_io_device
*pos
= bus
->devs
[i
];
1977 kvm_iodevice_destructor(pos
);
1982 /* kvm_io_bus_write - called under kvm->slots_lock */
1983 int kvm_io_bus_write(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
1984 int len
, const void *val
)
1987 struct kvm_io_bus
*bus
= rcu_dereference(kvm
->buses
[bus_idx
]);
1988 for (i
= 0; i
< bus
->dev_count
; i
++)
1989 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
1994 /* kvm_io_bus_read - called under kvm->slots_lock */
1995 int kvm_io_bus_read(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
1999 struct kvm_io_bus
*bus
= rcu_dereference(kvm
->buses
[bus_idx
]);
2001 for (i
= 0; i
< bus
->dev_count
; i
++)
2002 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
2007 /* Caller must hold slots_lock. */
2008 int kvm_io_bus_register_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2009 struct kvm_io_device
*dev
)
2011 struct kvm_io_bus
*new_bus
, *bus
;
2013 bus
= kvm
->buses
[bus_idx
];
2014 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2017 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2020 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2021 new_bus
->devs
[new_bus
->dev_count
++] = dev
;
2022 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2023 synchronize_srcu_expedited(&kvm
->srcu
);
2029 /* Caller must hold slots_lock. */
2030 int kvm_io_bus_unregister_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2031 struct kvm_io_device
*dev
)
2034 struct kvm_io_bus
*new_bus
, *bus
;
2036 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2040 bus
= kvm
->buses
[bus_idx
];
2041 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2044 for (i
= 0; i
< new_bus
->dev_count
; i
++)
2045 if (new_bus
->devs
[i
] == dev
) {
2047 new_bus
->devs
[i
] = new_bus
->devs
[--new_bus
->dev_count
];
2056 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2057 synchronize_srcu_expedited(&kvm
->srcu
);
2062 static struct notifier_block kvm_cpu_notifier
= {
2063 .notifier_call
= kvm_cpu_hotplug
,
2064 .priority
= 20, /* must be > scheduler priority */
2067 static int vm_stat_get(void *_offset
, u64
*val
)
2069 unsigned offset
= (long)_offset
;
2073 spin_lock(&kvm_lock
);
2074 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2075 *val
+= *(u32
*)((void *)kvm
+ offset
);
2076 spin_unlock(&kvm_lock
);
2080 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2082 static int vcpu_stat_get(void *_offset
, u64
*val
)
2084 unsigned offset
= (long)_offset
;
2086 struct kvm_vcpu
*vcpu
;
2090 spin_lock(&kvm_lock
);
2091 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2092 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2093 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2095 spin_unlock(&kvm_lock
);
2099 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2101 static const struct file_operations
*stat_fops
[] = {
2102 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2103 [KVM_STAT_VM
] = &vm_stat_fops
,
2106 static void kvm_init_debug(void)
2108 struct kvm_stats_debugfs_item
*p
;
2110 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2111 for (p
= debugfs_entries
; p
->name
; ++p
)
2112 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2113 (void *)(long)p
->offset
,
2114 stat_fops
[p
->kind
]);
2117 static void kvm_exit_debug(void)
2119 struct kvm_stats_debugfs_item
*p
;
2121 for (p
= debugfs_entries
; p
->name
; ++p
)
2122 debugfs_remove(p
->dentry
);
2123 debugfs_remove(kvm_debugfs_dir
);
2126 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2128 if (kvm_usage_count
)
2129 hardware_disable(NULL
);
2133 static int kvm_resume(struct sys_device
*dev
)
2135 if (kvm_usage_count
)
2136 hardware_enable(NULL
);
2140 static struct sysdev_class kvm_sysdev_class
= {
2142 .suspend
= kvm_suspend
,
2143 .resume
= kvm_resume
,
2146 static struct sys_device kvm_sysdev
= {
2148 .cls
= &kvm_sysdev_class
,
2151 struct page
*bad_page
;
2155 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2157 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2160 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2162 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2164 kvm_arch_vcpu_load(vcpu
, cpu
);
2167 static void kvm_sched_out(struct preempt_notifier
*pn
,
2168 struct task_struct
*next
)
2170 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2172 kvm_arch_vcpu_put(vcpu
);
2175 int kvm_init(void *opaque
, unsigned int vcpu_size
,
2176 struct module
*module
)
2181 r
= kvm_arch_init(opaque
);
2185 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2187 if (bad_page
== NULL
) {
2192 bad_pfn
= page_to_pfn(bad_page
);
2194 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2199 r
= kvm_arch_hardware_setup();
2203 for_each_online_cpu(cpu
) {
2204 smp_call_function_single(cpu
,
2205 kvm_arch_check_processor_compat
,
2211 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2214 register_reboot_notifier(&kvm_reboot_notifier
);
2216 r
= sysdev_class_register(&kvm_sysdev_class
);
2220 r
= sysdev_register(&kvm_sysdev
);
2224 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2225 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
2226 __alignof__(struct kvm_vcpu
),
2228 if (!kvm_vcpu_cache
) {
2233 kvm_chardev_ops
.owner
= module
;
2234 kvm_vm_fops
.owner
= module
;
2235 kvm_vcpu_fops
.owner
= module
;
2237 r
= misc_register(&kvm_dev
);
2239 printk(KERN_ERR
"kvm: misc device register failed\n");
2243 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2244 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2251 kmem_cache_destroy(kvm_vcpu_cache
);
2253 sysdev_unregister(&kvm_sysdev
);
2255 sysdev_class_unregister(&kvm_sysdev_class
);
2257 unregister_reboot_notifier(&kvm_reboot_notifier
);
2258 unregister_cpu_notifier(&kvm_cpu_notifier
);
2261 kvm_arch_hardware_unsetup();
2263 free_cpumask_var(cpus_hardware_enabled
);
2265 __free_page(bad_page
);
2271 EXPORT_SYMBOL_GPL(kvm_init
);
2275 tracepoint_synchronize_unregister();
2277 misc_deregister(&kvm_dev
);
2278 kmem_cache_destroy(kvm_vcpu_cache
);
2279 sysdev_unregister(&kvm_sysdev
);
2280 sysdev_class_unregister(&kvm_sysdev_class
);
2281 unregister_reboot_notifier(&kvm_reboot_notifier
);
2282 unregister_cpu_notifier(&kvm_cpu_notifier
);
2283 on_each_cpu(hardware_disable
, NULL
, 1);
2284 kvm_arch_hardware_unsetup();
2286 free_cpumask_var(cpus_hardware_enabled
);
2287 __free_page(bad_page
);
2289 EXPORT_SYMBOL_GPL(kvm_exit
);