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 affilates.
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
);
709 /* map the pages in iommu page table */
711 r
= kvm_iommu_map_pages(kvm
, &new);
718 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
721 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
722 if (mem
->slot
>= slots
->nmemslots
)
723 slots
->nmemslots
= mem
->slot
+ 1;
725 /* actual memory is freed via old in kvm_free_physmem_slot below */
728 new.dirty_bitmap
= NULL
;
729 for (i
= 0; i
< KVM_NR_PAGE_SIZES
- 1; ++i
)
730 new.lpage_info
[i
] = NULL
;
733 slots
->memslots
[mem
->slot
] = new;
734 old_memslots
= kvm
->memslots
;
735 rcu_assign_pointer(kvm
->memslots
, slots
);
736 synchronize_srcu_expedited(&kvm
->srcu
);
738 kvm_arch_commit_memory_region(kvm
, mem
, old
, user_alloc
);
740 kvm_free_physmem_slot(&old
, &new);
744 kvm_arch_flush_shadow(kvm
);
749 kvm_free_physmem_slot(&new, &old
);
754 EXPORT_SYMBOL_GPL(__kvm_set_memory_region
);
756 int kvm_set_memory_region(struct kvm
*kvm
,
757 struct kvm_userspace_memory_region
*mem
,
762 mutex_lock(&kvm
->slots_lock
);
763 r
= __kvm_set_memory_region(kvm
, mem
, user_alloc
);
764 mutex_unlock(&kvm
->slots_lock
);
767 EXPORT_SYMBOL_GPL(kvm_set_memory_region
);
769 int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
771 kvm_userspace_memory_region
*mem
,
774 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
776 return kvm_set_memory_region(kvm
, mem
, user_alloc
);
779 int kvm_get_dirty_log(struct kvm
*kvm
,
780 struct kvm_dirty_log
*log
, int *is_dirty
)
782 struct kvm_memory_slot
*memslot
;
785 unsigned long any
= 0;
788 if (log
->slot
>= KVM_MEMORY_SLOTS
)
791 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
793 if (!memslot
->dirty_bitmap
)
796 n
= kvm_dirty_bitmap_bytes(memslot
);
798 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
799 any
= memslot
->dirty_bitmap
[i
];
802 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
813 void kvm_disable_largepages(void)
815 largepages_enabled
= false;
817 EXPORT_SYMBOL_GPL(kvm_disable_largepages
);
819 int is_error_page(struct page
*page
)
821 return page
== bad_page
|| page
== hwpoison_page
|| page
== fault_page
;
823 EXPORT_SYMBOL_GPL(is_error_page
);
825 int is_error_pfn(pfn_t pfn
)
827 return pfn
== bad_pfn
|| pfn
== hwpoison_pfn
|| pfn
== fault_pfn
;
829 EXPORT_SYMBOL_GPL(is_error_pfn
);
831 int is_hwpoison_pfn(pfn_t pfn
)
833 return pfn
== hwpoison_pfn
;
835 EXPORT_SYMBOL_GPL(is_hwpoison_pfn
);
837 int is_fault_pfn(pfn_t pfn
)
839 return pfn
== fault_pfn
;
841 EXPORT_SYMBOL_GPL(is_fault_pfn
);
843 static inline unsigned long bad_hva(void)
848 int kvm_is_error_hva(unsigned long addr
)
850 return addr
== bad_hva();
852 EXPORT_SYMBOL_GPL(kvm_is_error_hva
);
854 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
857 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
859 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
860 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
862 if (gfn
>= memslot
->base_gfn
863 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
868 EXPORT_SYMBOL_GPL(gfn_to_memslot
);
870 int kvm_is_visible_gfn(struct kvm
*kvm
, gfn_t gfn
)
873 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
875 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
876 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
878 if (memslot
->flags
& KVM_MEMSLOT_INVALID
)
881 if (gfn
>= memslot
->base_gfn
882 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
887 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn
);
889 unsigned long kvm_host_page_size(struct kvm
*kvm
, gfn_t gfn
)
891 struct vm_area_struct
*vma
;
892 unsigned long addr
, size
;
896 addr
= gfn_to_hva(kvm
, gfn
);
897 if (kvm_is_error_hva(addr
))
900 down_read(¤t
->mm
->mmap_sem
);
901 vma
= find_vma(current
->mm
, addr
);
905 size
= vma_kernel_pagesize(vma
);
908 up_read(¤t
->mm
->mmap_sem
);
913 int memslot_id(struct kvm
*kvm
, gfn_t gfn
)
916 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
917 struct kvm_memory_slot
*memslot
= NULL
;
919 for (i
= 0; i
< slots
->nmemslots
; ++i
) {
920 memslot
= &slots
->memslots
[i
];
922 if (gfn
>= memslot
->base_gfn
923 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
927 return memslot
- slots
->memslots
;
930 static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot
*slot
, gfn_t gfn
)
932 return slot
->userspace_addr
+ (gfn
- slot
->base_gfn
) * PAGE_SIZE
;
935 unsigned long gfn_to_hva(struct kvm
*kvm
, gfn_t gfn
)
937 struct kvm_memory_slot
*slot
;
939 slot
= gfn_to_memslot(kvm
, gfn
);
940 if (!slot
|| slot
->flags
& KVM_MEMSLOT_INVALID
)
942 return gfn_to_hva_memslot(slot
, gfn
);
944 EXPORT_SYMBOL_GPL(gfn_to_hva
);
946 static pfn_t
hva_to_pfn(struct kvm
*kvm
, unsigned long addr
)
948 struct page
*page
[1];
954 npages
= get_user_pages_fast(addr
, 1, 1, page
);
956 if (unlikely(npages
!= 1)) {
957 struct vm_area_struct
*vma
;
959 down_read(¤t
->mm
->mmap_sem
);
960 if (is_hwpoison_address(addr
)) {
961 up_read(¤t
->mm
->mmap_sem
);
962 get_page(hwpoison_page
);
963 return page_to_pfn(hwpoison_page
);
966 vma
= find_vma(current
->mm
, addr
);
968 if (vma
== NULL
|| addr
< vma
->vm_start
||
969 !(vma
->vm_flags
& VM_PFNMAP
)) {
970 up_read(¤t
->mm
->mmap_sem
);
971 get_page(fault_page
);
972 return page_to_pfn(fault_page
);
975 pfn
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
976 up_read(¤t
->mm
->mmap_sem
);
977 BUG_ON(!kvm_is_mmio_pfn(pfn
));
979 pfn
= page_to_pfn(page
[0]);
984 pfn_t
gfn_to_pfn(struct kvm
*kvm
, gfn_t gfn
)
988 addr
= gfn_to_hva(kvm
, gfn
);
989 if (kvm_is_error_hva(addr
)) {
991 return page_to_pfn(bad_page
);
994 return hva_to_pfn(kvm
, addr
);
996 EXPORT_SYMBOL_GPL(gfn_to_pfn
);
998 pfn_t
gfn_to_pfn_memslot(struct kvm
*kvm
,
999 struct kvm_memory_slot
*slot
, gfn_t gfn
)
1001 unsigned long addr
= gfn_to_hva_memslot(slot
, gfn
);
1002 return hva_to_pfn(kvm
, addr
);
1005 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1009 pfn
= gfn_to_pfn(kvm
, gfn
);
1010 if (!kvm_is_mmio_pfn(pfn
))
1011 return pfn_to_page(pfn
);
1013 WARN_ON(kvm_is_mmio_pfn(pfn
));
1019 EXPORT_SYMBOL_GPL(gfn_to_page
);
1021 void kvm_release_page_clean(struct page
*page
)
1023 kvm_release_pfn_clean(page_to_pfn(page
));
1025 EXPORT_SYMBOL_GPL(kvm_release_page_clean
);
1027 void kvm_release_pfn_clean(pfn_t pfn
)
1029 if (!kvm_is_mmio_pfn(pfn
))
1030 put_page(pfn_to_page(pfn
));
1032 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean
);
1034 void kvm_release_page_dirty(struct page
*page
)
1036 kvm_release_pfn_dirty(page_to_pfn(page
));
1038 EXPORT_SYMBOL_GPL(kvm_release_page_dirty
);
1040 void kvm_release_pfn_dirty(pfn_t pfn
)
1042 kvm_set_pfn_dirty(pfn
);
1043 kvm_release_pfn_clean(pfn
);
1045 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty
);
1047 void kvm_set_page_dirty(struct page
*page
)
1049 kvm_set_pfn_dirty(page_to_pfn(page
));
1051 EXPORT_SYMBOL_GPL(kvm_set_page_dirty
);
1053 void kvm_set_pfn_dirty(pfn_t pfn
)
1055 if (!kvm_is_mmio_pfn(pfn
)) {
1056 struct page
*page
= pfn_to_page(pfn
);
1057 if (!PageReserved(page
))
1061 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty
);
1063 void kvm_set_pfn_accessed(pfn_t pfn
)
1065 if (!kvm_is_mmio_pfn(pfn
))
1066 mark_page_accessed(pfn_to_page(pfn
));
1068 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed
);
1070 void kvm_get_pfn(pfn_t pfn
)
1072 if (!kvm_is_mmio_pfn(pfn
))
1073 get_page(pfn_to_page(pfn
));
1075 EXPORT_SYMBOL_GPL(kvm_get_pfn
);
1077 static int next_segment(unsigned long len
, int offset
)
1079 if (len
> PAGE_SIZE
- offset
)
1080 return PAGE_SIZE
- offset
;
1085 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1091 addr
= gfn_to_hva(kvm
, gfn
);
1092 if (kvm_is_error_hva(addr
))
1094 r
= copy_from_user(data
, (void __user
*)addr
+ offset
, len
);
1099 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1101 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1103 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1105 int offset
= offset_in_page(gpa
);
1108 while ((seg
= next_segment(len
, offset
)) != 0) {
1109 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1119 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1121 int kvm_read_guest_atomic(struct kvm
*kvm
, gpa_t gpa
, void *data
,
1126 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1127 int offset
= offset_in_page(gpa
);
1129 addr
= gfn_to_hva(kvm
, gfn
);
1130 if (kvm_is_error_hva(addr
))
1132 pagefault_disable();
1133 r
= __copy_from_user_inatomic(data
, (void __user
*)addr
+ offset
, len
);
1139 EXPORT_SYMBOL(kvm_read_guest_atomic
);
1141 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1142 int offset
, int len
)
1147 addr
= gfn_to_hva(kvm
, gfn
);
1148 if (kvm_is_error_hva(addr
))
1150 r
= copy_to_user((void __user
*)addr
+ offset
, data
, len
);
1153 mark_page_dirty(kvm
, gfn
);
1156 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1158 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1161 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1163 int offset
= offset_in_page(gpa
);
1166 while ((seg
= next_segment(len
, offset
)) != 0) {
1167 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1178 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1180 return kvm_write_guest_page(kvm
, gfn
, empty_zero_page
, offset
, len
);
1182 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1184 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1186 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1188 int offset
= offset_in_page(gpa
);
1191 while ((seg
= next_segment(len
, offset
)) != 0) {
1192 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1201 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1203 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1205 struct kvm_memory_slot
*memslot
;
1207 memslot
= gfn_to_memslot(kvm
, gfn
);
1208 if (memslot
&& memslot
->dirty_bitmap
) {
1209 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1211 generic___set_le_bit(rel_gfn
, memslot
->dirty_bitmap
);
1216 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1218 void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1223 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1225 if (kvm_arch_vcpu_runnable(vcpu
)) {
1226 kvm_make_request(KVM_REQ_UNHALT
, vcpu
);
1229 if (kvm_cpu_has_pending_timer(vcpu
))
1231 if (signal_pending(current
))
1237 finish_wait(&vcpu
->wq
, &wait
);
1240 void kvm_resched(struct kvm_vcpu
*vcpu
)
1242 if (!need_resched())
1246 EXPORT_SYMBOL_GPL(kvm_resched
);
1248 void kvm_vcpu_on_spin(struct kvm_vcpu
*vcpu
)
1253 prepare_to_wait(&vcpu
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1255 /* Sleep for 100 us, and hope lock-holder got scheduled */
1256 expires
= ktime_add_ns(ktime_get(), 100000UL);
1257 schedule_hrtimeout(&expires
, HRTIMER_MODE_ABS
);
1259 finish_wait(&vcpu
->wq
, &wait
);
1261 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin
);
1263 static int kvm_vcpu_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1265 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
1268 if (vmf
->pgoff
== 0)
1269 page
= virt_to_page(vcpu
->run
);
1271 else if (vmf
->pgoff
== KVM_PIO_PAGE_OFFSET
)
1272 page
= virt_to_page(vcpu
->arch
.pio_data
);
1274 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1275 else if (vmf
->pgoff
== KVM_COALESCED_MMIO_PAGE_OFFSET
)
1276 page
= virt_to_page(vcpu
->kvm
->coalesced_mmio_ring
);
1279 return VM_FAULT_SIGBUS
;
1285 static const struct vm_operations_struct kvm_vcpu_vm_ops
= {
1286 .fault
= kvm_vcpu_fault
,
1289 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1291 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
1295 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1297 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1299 kvm_put_kvm(vcpu
->kvm
);
1303 static struct file_operations kvm_vcpu_fops
= {
1304 .release
= kvm_vcpu_release
,
1305 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1306 .compat_ioctl
= kvm_vcpu_ioctl
,
1307 .mmap
= kvm_vcpu_mmap
,
1311 * Allocates an inode for the vcpu.
1313 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1315 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops
, vcpu
, O_RDWR
);
1319 * Creates some virtual cpus. Good luck creating more than one.
1321 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, u32 id
)
1324 struct kvm_vcpu
*vcpu
, *v
;
1326 vcpu
= kvm_arch_vcpu_create(kvm
, id
);
1328 return PTR_ERR(vcpu
);
1330 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
1332 r
= kvm_arch_vcpu_setup(vcpu
);
1336 mutex_lock(&kvm
->lock
);
1337 if (atomic_read(&kvm
->online_vcpus
) == KVM_MAX_VCPUS
) {
1342 kvm_for_each_vcpu(r
, v
, kvm
)
1343 if (v
->vcpu_id
== id
) {
1348 BUG_ON(kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)]);
1350 /* Now it's all set up, let userspace reach it */
1352 r
= create_vcpu_fd(vcpu
);
1358 kvm
->vcpus
[atomic_read(&kvm
->online_vcpus
)] = vcpu
;
1360 atomic_inc(&kvm
->online_vcpus
);
1362 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1363 if (kvm
->bsp_vcpu_id
== id
)
1364 kvm
->bsp_vcpu
= vcpu
;
1366 mutex_unlock(&kvm
->lock
);
1370 mutex_unlock(&kvm
->lock
);
1371 kvm_arch_vcpu_destroy(vcpu
);
1375 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
1378 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1379 vcpu
->sigset_active
= 1;
1380 vcpu
->sigset
= *sigset
;
1382 vcpu
->sigset_active
= 0;
1386 static long kvm_vcpu_ioctl(struct file
*filp
,
1387 unsigned int ioctl
, unsigned long arg
)
1389 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1390 void __user
*argp
= (void __user
*)arg
;
1392 struct kvm_fpu
*fpu
= NULL
;
1393 struct kvm_sregs
*kvm_sregs
= NULL
;
1395 if (vcpu
->kvm
->mm
!= current
->mm
)
1398 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1400 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1401 * so vcpu_load() would break it.
1403 if (ioctl
== KVM_S390_INTERRUPT
|| ioctl
== KVM_INTERRUPT
)
1404 return kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1414 r
= kvm_arch_vcpu_ioctl_run(vcpu
, vcpu
->run
);
1416 case KVM_GET_REGS
: {
1417 struct kvm_regs
*kvm_regs
;
1420 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1423 r
= kvm_arch_vcpu_ioctl_get_regs(vcpu
, kvm_regs
);
1427 if (copy_to_user(argp
, kvm_regs
, sizeof(struct kvm_regs
)))
1434 case KVM_SET_REGS
: {
1435 struct kvm_regs
*kvm_regs
;
1438 kvm_regs
= kzalloc(sizeof(struct kvm_regs
), GFP_KERNEL
);
1442 if (copy_from_user(kvm_regs
, argp
, sizeof(struct kvm_regs
)))
1444 r
= kvm_arch_vcpu_ioctl_set_regs(vcpu
, kvm_regs
);
1452 case KVM_GET_SREGS
: {
1453 kvm_sregs
= kzalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1457 r
= kvm_arch_vcpu_ioctl_get_sregs(vcpu
, kvm_sregs
);
1461 if (copy_to_user(argp
, kvm_sregs
, sizeof(struct kvm_sregs
)))
1466 case KVM_SET_SREGS
: {
1467 kvm_sregs
= kmalloc(sizeof(struct kvm_sregs
), GFP_KERNEL
);
1472 if (copy_from_user(kvm_sregs
, argp
, sizeof(struct kvm_sregs
)))
1474 r
= kvm_arch_vcpu_ioctl_set_sregs(vcpu
, kvm_sregs
);
1480 case KVM_GET_MP_STATE
: {
1481 struct kvm_mp_state mp_state
;
1483 r
= kvm_arch_vcpu_ioctl_get_mpstate(vcpu
, &mp_state
);
1487 if (copy_to_user(argp
, &mp_state
, sizeof mp_state
))
1492 case KVM_SET_MP_STATE
: {
1493 struct kvm_mp_state mp_state
;
1496 if (copy_from_user(&mp_state
, argp
, sizeof mp_state
))
1498 r
= kvm_arch_vcpu_ioctl_set_mpstate(vcpu
, &mp_state
);
1504 case KVM_TRANSLATE
: {
1505 struct kvm_translation tr
;
1508 if (copy_from_user(&tr
, argp
, sizeof tr
))
1510 r
= kvm_arch_vcpu_ioctl_translate(vcpu
, &tr
);
1514 if (copy_to_user(argp
, &tr
, sizeof tr
))
1519 case KVM_SET_GUEST_DEBUG
: {
1520 struct kvm_guest_debug dbg
;
1523 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
1525 r
= kvm_arch_vcpu_ioctl_set_guest_debug(vcpu
, &dbg
);
1531 case KVM_SET_SIGNAL_MASK
: {
1532 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
1533 struct kvm_signal_mask kvm_sigmask
;
1534 sigset_t sigset
, *p
;
1539 if (copy_from_user(&kvm_sigmask
, argp
,
1540 sizeof kvm_sigmask
))
1543 if (kvm_sigmask
.len
!= sizeof sigset
)
1546 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
1551 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, p
);
1555 fpu
= kzalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1559 r
= kvm_arch_vcpu_ioctl_get_fpu(vcpu
, fpu
);
1563 if (copy_to_user(argp
, fpu
, sizeof(struct kvm_fpu
)))
1569 fpu
= kmalloc(sizeof(struct kvm_fpu
), GFP_KERNEL
);
1574 if (copy_from_user(fpu
, argp
, sizeof(struct kvm_fpu
)))
1576 r
= kvm_arch_vcpu_ioctl_set_fpu(vcpu
, fpu
);
1583 r
= kvm_arch_vcpu_ioctl(filp
, ioctl
, arg
);
1592 static long kvm_vm_ioctl(struct file
*filp
,
1593 unsigned int ioctl
, unsigned long arg
)
1595 struct kvm
*kvm
= filp
->private_data
;
1596 void __user
*argp
= (void __user
*)arg
;
1599 if (kvm
->mm
!= current
->mm
)
1602 case KVM_CREATE_VCPU
:
1603 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
1607 case KVM_SET_USER_MEMORY_REGION
: {
1608 struct kvm_userspace_memory_region kvm_userspace_mem
;
1611 if (copy_from_user(&kvm_userspace_mem
, argp
,
1612 sizeof kvm_userspace_mem
))
1615 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 1);
1620 case KVM_GET_DIRTY_LOG
: {
1621 struct kvm_dirty_log log
;
1624 if (copy_from_user(&log
, argp
, sizeof log
))
1626 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1631 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1632 case KVM_REGISTER_COALESCED_MMIO
: {
1633 struct kvm_coalesced_mmio_zone zone
;
1635 if (copy_from_user(&zone
, argp
, sizeof zone
))
1637 r
= kvm_vm_ioctl_register_coalesced_mmio(kvm
, &zone
);
1643 case KVM_UNREGISTER_COALESCED_MMIO
: {
1644 struct kvm_coalesced_mmio_zone zone
;
1646 if (copy_from_user(&zone
, argp
, sizeof zone
))
1648 r
= kvm_vm_ioctl_unregister_coalesced_mmio(kvm
, &zone
);
1656 struct kvm_irqfd data
;
1659 if (copy_from_user(&data
, argp
, sizeof data
))
1661 r
= kvm_irqfd(kvm
, data
.fd
, data
.gsi
, data
.flags
);
1664 case KVM_IOEVENTFD
: {
1665 struct kvm_ioeventfd data
;
1668 if (copy_from_user(&data
, argp
, sizeof data
))
1670 r
= kvm_ioeventfd(kvm
, &data
);
1673 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1674 case KVM_SET_BOOT_CPU_ID
:
1676 mutex_lock(&kvm
->lock
);
1677 if (atomic_read(&kvm
->online_vcpus
) != 0)
1680 kvm
->bsp_vcpu_id
= arg
;
1681 mutex_unlock(&kvm
->lock
);
1685 r
= kvm_arch_vm_ioctl(filp
, ioctl
, arg
);
1687 r
= kvm_vm_ioctl_assigned_device(kvm
, ioctl
, arg
);
1693 #ifdef CONFIG_COMPAT
1694 struct compat_kvm_dirty_log
{
1698 compat_uptr_t dirty_bitmap
; /* one bit per page */
1703 static long kvm_vm_compat_ioctl(struct file
*filp
,
1704 unsigned int ioctl
, unsigned long arg
)
1706 struct kvm
*kvm
= filp
->private_data
;
1709 if (kvm
->mm
!= current
->mm
)
1712 case KVM_GET_DIRTY_LOG
: {
1713 struct compat_kvm_dirty_log compat_log
;
1714 struct kvm_dirty_log log
;
1717 if (copy_from_user(&compat_log
, (void __user
*)arg
,
1718 sizeof(compat_log
)))
1720 log
.slot
= compat_log
.slot
;
1721 log
.padding1
= compat_log
.padding1
;
1722 log
.padding2
= compat_log
.padding2
;
1723 log
.dirty_bitmap
= compat_ptr(compat_log
.dirty_bitmap
);
1725 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
1731 r
= kvm_vm_ioctl(filp
, ioctl
, arg
);
1739 static int kvm_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1741 struct page
*page
[1];
1744 gfn_t gfn
= vmf
->pgoff
;
1745 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1747 addr
= gfn_to_hva(kvm
, gfn
);
1748 if (kvm_is_error_hva(addr
))
1749 return VM_FAULT_SIGBUS
;
1751 npages
= get_user_pages(current
, current
->mm
, addr
, 1, 1, 0, page
,
1753 if (unlikely(npages
!= 1))
1754 return VM_FAULT_SIGBUS
;
1756 vmf
->page
= page
[0];
1760 static const struct vm_operations_struct kvm_vm_vm_ops
= {
1761 .fault
= kvm_vm_fault
,
1764 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1766 vma
->vm_ops
= &kvm_vm_vm_ops
;
1770 static struct file_operations kvm_vm_fops
= {
1771 .release
= kvm_vm_release
,
1772 .unlocked_ioctl
= kvm_vm_ioctl
,
1773 #ifdef CONFIG_COMPAT
1774 .compat_ioctl
= kvm_vm_compat_ioctl
,
1776 .mmap
= kvm_vm_mmap
,
1779 static int kvm_dev_ioctl_create_vm(void)
1784 kvm
= kvm_create_vm();
1786 return PTR_ERR(kvm
);
1787 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1788 r
= kvm_coalesced_mmio_init(kvm
);
1794 fd
= anon_inode_getfd("kvm-vm", &kvm_vm_fops
, kvm
, O_RDWR
);
1801 static long kvm_dev_ioctl_check_extension_generic(long arg
)
1804 case KVM_CAP_USER_MEMORY
:
1805 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS
:
1806 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
:
1807 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1808 case KVM_CAP_SET_BOOT_CPU_ID
:
1810 case KVM_CAP_INTERNAL_ERROR_DATA
:
1812 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1813 case KVM_CAP_IRQ_ROUTING
:
1814 return KVM_MAX_IRQ_ROUTES
;
1819 return kvm_dev_ioctl_check_extension(arg
);
1822 static long kvm_dev_ioctl(struct file
*filp
,
1823 unsigned int ioctl
, unsigned long arg
)
1828 case KVM_GET_API_VERSION
:
1832 r
= KVM_API_VERSION
;
1838 r
= kvm_dev_ioctl_create_vm();
1840 case KVM_CHECK_EXTENSION
:
1841 r
= kvm_dev_ioctl_check_extension_generic(arg
);
1843 case KVM_GET_VCPU_MMAP_SIZE
:
1847 r
= PAGE_SIZE
; /* struct kvm_run */
1849 r
+= PAGE_SIZE
; /* pio data page */
1851 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1852 r
+= PAGE_SIZE
; /* coalesced mmio ring page */
1855 case KVM_TRACE_ENABLE
:
1856 case KVM_TRACE_PAUSE
:
1857 case KVM_TRACE_DISABLE
:
1861 return kvm_arch_dev_ioctl(filp
, ioctl
, arg
);
1867 static struct file_operations kvm_chardev_ops
= {
1868 .unlocked_ioctl
= kvm_dev_ioctl
,
1869 .compat_ioctl
= kvm_dev_ioctl
,
1872 static struct miscdevice kvm_dev
= {
1878 static void hardware_enable(void *junk
)
1880 int cpu
= raw_smp_processor_id();
1883 if (cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1886 cpumask_set_cpu(cpu
, cpus_hardware_enabled
);
1888 r
= kvm_arch_hardware_enable(NULL
);
1891 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1892 atomic_inc(&hardware_enable_failed
);
1893 printk(KERN_INFO
"kvm: enabling virtualization on "
1894 "CPU%d failed\n", cpu
);
1898 static void hardware_disable(void *junk
)
1900 int cpu
= raw_smp_processor_id();
1902 if (!cpumask_test_cpu(cpu
, cpus_hardware_enabled
))
1904 cpumask_clear_cpu(cpu
, cpus_hardware_enabled
);
1905 kvm_arch_hardware_disable(NULL
);
1908 static void hardware_disable_all_nolock(void)
1910 BUG_ON(!kvm_usage_count
);
1913 if (!kvm_usage_count
)
1914 on_each_cpu(hardware_disable
, NULL
, 1);
1917 static void hardware_disable_all(void)
1919 spin_lock(&kvm_lock
);
1920 hardware_disable_all_nolock();
1921 spin_unlock(&kvm_lock
);
1924 static int hardware_enable_all(void)
1928 spin_lock(&kvm_lock
);
1931 if (kvm_usage_count
== 1) {
1932 atomic_set(&hardware_enable_failed
, 0);
1933 on_each_cpu(hardware_enable
, NULL
, 1);
1935 if (atomic_read(&hardware_enable_failed
)) {
1936 hardware_disable_all_nolock();
1941 spin_unlock(&kvm_lock
);
1946 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
1951 if (!kvm_usage_count
)
1954 val
&= ~CPU_TASKS_FROZEN
;
1957 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
1959 hardware_disable(NULL
);
1962 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
1964 smp_call_function_single(cpu
, hardware_enable
, NULL
, 1);
1971 asmlinkage
void kvm_handle_fault_on_reboot(void)
1974 /* spin while reset goes on */
1977 /* Fault while not rebooting. We want the trace. */
1980 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot
);
1982 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1986 * Some (well, at least mine) BIOSes hang on reboot if
1989 * And Intel TXT required VMX off for all cpu when system shutdown.
1991 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1992 kvm_rebooting
= true;
1993 on_each_cpu(hardware_disable
, NULL
, 1);
1997 static struct notifier_block kvm_reboot_notifier
= {
1998 .notifier_call
= kvm_reboot
,
2002 static void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
2006 for (i
= 0; i
< bus
->dev_count
; i
++) {
2007 struct kvm_io_device
*pos
= bus
->devs
[i
];
2009 kvm_iodevice_destructor(pos
);
2014 /* kvm_io_bus_write - called under kvm->slots_lock */
2015 int kvm_io_bus_write(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2016 int len
, const void *val
)
2019 struct kvm_io_bus
*bus
;
2021 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2022 for (i
= 0; i
< bus
->dev_count
; i
++)
2023 if (!kvm_iodevice_write(bus
->devs
[i
], addr
, len
, val
))
2028 /* kvm_io_bus_read - called under kvm->slots_lock */
2029 int kvm_io_bus_read(struct kvm
*kvm
, enum kvm_bus bus_idx
, gpa_t addr
,
2033 struct kvm_io_bus
*bus
;
2035 bus
= srcu_dereference(kvm
->buses
[bus_idx
], &kvm
->srcu
);
2036 for (i
= 0; i
< bus
->dev_count
; i
++)
2037 if (!kvm_iodevice_read(bus
->devs
[i
], addr
, len
, val
))
2042 /* Caller must hold slots_lock. */
2043 int kvm_io_bus_register_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2044 struct kvm_io_device
*dev
)
2046 struct kvm_io_bus
*new_bus
, *bus
;
2048 bus
= kvm
->buses
[bus_idx
];
2049 if (bus
->dev_count
> NR_IOBUS_DEVS
-1)
2052 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2055 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2056 new_bus
->devs
[new_bus
->dev_count
++] = dev
;
2057 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2058 synchronize_srcu_expedited(&kvm
->srcu
);
2064 /* Caller must hold slots_lock. */
2065 int kvm_io_bus_unregister_dev(struct kvm
*kvm
, enum kvm_bus bus_idx
,
2066 struct kvm_io_device
*dev
)
2069 struct kvm_io_bus
*new_bus
, *bus
;
2071 new_bus
= kzalloc(sizeof(struct kvm_io_bus
), GFP_KERNEL
);
2075 bus
= kvm
->buses
[bus_idx
];
2076 memcpy(new_bus
, bus
, sizeof(struct kvm_io_bus
));
2079 for (i
= 0; i
< new_bus
->dev_count
; i
++)
2080 if (new_bus
->devs
[i
] == dev
) {
2082 new_bus
->devs
[i
] = new_bus
->devs
[--new_bus
->dev_count
];
2091 rcu_assign_pointer(kvm
->buses
[bus_idx
], new_bus
);
2092 synchronize_srcu_expedited(&kvm
->srcu
);
2097 static struct notifier_block kvm_cpu_notifier
= {
2098 .notifier_call
= kvm_cpu_hotplug
,
2099 .priority
= 20, /* must be > scheduler priority */
2102 static int vm_stat_get(void *_offset
, u64
*val
)
2104 unsigned offset
= (long)_offset
;
2108 spin_lock(&kvm_lock
);
2109 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2110 *val
+= *(u32
*)((void *)kvm
+ offset
);
2111 spin_unlock(&kvm_lock
);
2115 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops
, vm_stat_get
, NULL
, "%llu\n");
2117 static int vcpu_stat_get(void *_offset
, u64
*val
)
2119 unsigned offset
= (long)_offset
;
2121 struct kvm_vcpu
*vcpu
;
2125 spin_lock(&kvm_lock
);
2126 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2127 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2128 *val
+= *(u32
*)((void *)vcpu
+ offset
);
2130 spin_unlock(&kvm_lock
);
2134 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops
, vcpu_stat_get
, NULL
, "%llu\n");
2136 static const struct file_operations
*stat_fops
[] = {
2137 [KVM_STAT_VCPU
] = &vcpu_stat_fops
,
2138 [KVM_STAT_VM
] = &vm_stat_fops
,
2141 static void kvm_init_debug(void)
2143 struct kvm_stats_debugfs_item
*p
;
2145 kvm_debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2146 for (p
= debugfs_entries
; p
->name
; ++p
)
2147 p
->dentry
= debugfs_create_file(p
->name
, 0444, kvm_debugfs_dir
,
2148 (void *)(long)p
->offset
,
2149 stat_fops
[p
->kind
]);
2152 static void kvm_exit_debug(void)
2154 struct kvm_stats_debugfs_item
*p
;
2156 for (p
= debugfs_entries
; p
->name
; ++p
)
2157 debugfs_remove(p
->dentry
);
2158 debugfs_remove(kvm_debugfs_dir
);
2161 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2163 if (kvm_usage_count
)
2164 hardware_disable(NULL
);
2168 static int kvm_resume(struct sys_device
*dev
)
2170 if (kvm_usage_count
)
2171 hardware_enable(NULL
);
2175 static struct sysdev_class kvm_sysdev_class
= {
2177 .suspend
= kvm_suspend
,
2178 .resume
= kvm_resume
,
2181 static struct sys_device kvm_sysdev
= {
2183 .cls
= &kvm_sysdev_class
,
2186 struct page
*bad_page
;
2190 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
2192 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
2195 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
2197 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2199 kvm_arch_vcpu_load(vcpu
, cpu
);
2202 static void kvm_sched_out(struct preempt_notifier
*pn
,
2203 struct task_struct
*next
)
2205 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
2207 kvm_arch_vcpu_put(vcpu
);
2210 int kvm_init(void *opaque
, unsigned vcpu_size
, unsigned vcpu_align
,
2211 struct module
*module
)
2216 r
= kvm_arch_init(opaque
);
2220 bad_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2222 if (bad_page
== NULL
) {
2227 bad_pfn
= page_to_pfn(bad_page
);
2229 hwpoison_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2231 if (hwpoison_page
== NULL
) {
2236 hwpoison_pfn
= page_to_pfn(hwpoison_page
);
2238 fault_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2240 if (fault_page
== NULL
) {
2245 fault_pfn
= page_to_pfn(fault_page
);
2247 if (!zalloc_cpumask_var(&cpus_hardware_enabled
, GFP_KERNEL
)) {
2252 r
= kvm_arch_hardware_setup();
2256 for_each_online_cpu(cpu
) {
2257 smp_call_function_single(cpu
,
2258 kvm_arch_check_processor_compat
,
2264 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2267 register_reboot_notifier(&kvm_reboot_notifier
);
2269 r
= sysdev_class_register(&kvm_sysdev_class
);
2273 r
= sysdev_register(&kvm_sysdev
);
2277 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2279 vcpu_align
= __alignof__(struct kvm_vcpu
);
2280 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
, vcpu_align
,
2282 if (!kvm_vcpu_cache
) {
2287 kvm_chardev_ops
.owner
= module
;
2288 kvm_vm_fops
.owner
= module
;
2289 kvm_vcpu_fops
.owner
= module
;
2291 r
= misc_register(&kvm_dev
);
2293 printk(KERN_ERR
"kvm: misc device register failed\n");
2297 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
2298 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
2305 kmem_cache_destroy(kvm_vcpu_cache
);
2307 sysdev_unregister(&kvm_sysdev
);
2309 sysdev_class_unregister(&kvm_sysdev_class
);
2311 unregister_reboot_notifier(&kvm_reboot_notifier
);
2312 unregister_cpu_notifier(&kvm_cpu_notifier
);
2315 kvm_arch_hardware_unsetup();
2317 free_cpumask_var(cpus_hardware_enabled
);
2320 __free_page(fault_page
);
2322 __free_page(hwpoison_page
);
2323 __free_page(bad_page
);
2329 EXPORT_SYMBOL_GPL(kvm_init
);
2334 misc_deregister(&kvm_dev
);
2335 kmem_cache_destroy(kvm_vcpu_cache
);
2336 sysdev_unregister(&kvm_sysdev
);
2337 sysdev_class_unregister(&kvm_sysdev_class
);
2338 unregister_reboot_notifier(&kvm_reboot_notifier
);
2339 unregister_cpu_notifier(&kvm_cpu_notifier
);
2340 on_each_cpu(hardware_disable
, NULL
, 1);
2341 kvm_arch_hardware_unsetup();
2343 free_cpumask_var(cpus_hardware_enabled
);
2344 __free_page(hwpoison_page
);
2345 __free_page(bad_page
);
2347 EXPORT_SYMBOL_GPL(kvm_exit
);