KVM: Enable 32bit dirty log pointers on 64bit host
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / virt / kvm / kvm_main.c
blobbd44fb48ac43bd1f860ba8797d982f39cacbb93b
1 /*
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.
9 * Authors:
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.
18 #include "iodev.h"
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>
26 #include <linux/mm.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>
48 #include <asm/processor.h>
49 #include <asm/io.h>
50 #include <asm/uaccess.h>
51 #include <asm/pgtable.h>
53 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
54 #include "coalesced_mmio.h"
55 #endif
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/kvm.h>
60 MODULE_AUTHOR("Qumranet");
61 MODULE_LICENSE("GPL");
64 * Ordering of locks:
66 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
69 DEFINE_SPINLOCK(kvm_lock);
70 LIST_HEAD(vm_list);
72 static cpumask_var_t cpus_hardware_enabled;
73 static int kvm_usage_count = 0;
74 static atomic_t hardware_enable_failed;
76 struct kmem_cache *kvm_vcpu_cache;
77 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
79 static __read_mostly struct preempt_ops kvm_preempt_ops;
81 struct dentry *kvm_debugfs_dir;
83 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
84 unsigned long arg);
85 static int hardware_enable_all(void);
86 static void hardware_disable_all(void);
88 static bool kvm_rebooting;
90 static bool largepages_enabled = true;
92 inline int kvm_is_mmio_pfn(pfn_t pfn)
94 if (pfn_valid(pfn)) {
95 struct page *page = compound_head(pfn_to_page(pfn));
96 return PageReserved(page);
99 return true;
103 * Switches to specified vcpu, until a matching vcpu_put()
105 void vcpu_load(struct kvm_vcpu *vcpu)
107 int cpu;
109 mutex_lock(&vcpu->mutex);
110 cpu = get_cpu();
111 preempt_notifier_register(&vcpu->preempt_notifier);
112 kvm_arch_vcpu_load(vcpu, cpu);
113 put_cpu();
116 void vcpu_put(struct kvm_vcpu *vcpu)
118 preempt_disable();
119 kvm_arch_vcpu_put(vcpu);
120 preempt_notifier_unregister(&vcpu->preempt_notifier);
121 preempt_enable();
122 mutex_unlock(&vcpu->mutex);
125 static void ack_flush(void *_completed)
129 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
131 int i, cpu, me;
132 cpumask_var_t cpus;
133 bool called = true;
134 struct kvm_vcpu *vcpu;
136 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
138 spin_lock(&kvm->requests_lock);
139 me = smp_processor_id();
140 kvm_for_each_vcpu(i, vcpu, kvm) {
141 if (test_and_set_bit(req, &vcpu->requests))
142 continue;
143 cpu = vcpu->cpu;
144 if (cpus != NULL && cpu != -1 && cpu != me)
145 cpumask_set_cpu(cpu, cpus);
147 if (unlikely(cpus == NULL))
148 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
149 else if (!cpumask_empty(cpus))
150 smp_call_function_many(cpus, ack_flush, NULL, 1);
151 else
152 called = false;
153 spin_unlock(&kvm->requests_lock);
154 free_cpumask_var(cpus);
155 return called;
158 void kvm_flush_remote_tlbs(struct kvm *kvm)
160 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
161 ++kvm->stat.remote_tlb_flush;
164 void kvm_reload_remote_mmus(struct kvm *kvm)
166 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
169 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
171 struct page *page;
172 int r;
174 mutex_init(&vcpu->mutex);
175 vcpu->cpu = -1;
176 vcpu->kvm = kvm;
177 vcpu->vcpu_id = id;
178 init_waitqueue_head(&vcpu->wq);
180 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
181 if (!page) {
182 r = -ENOMEM;
183 goto fail;
185 vcpu->run = page_address(page);
187 r = kvm_arch_vcpu_init(vcpu);
188 if (r < 0)
189 goto fail_free_run;
190 return 0;
192 fail_free_run:
193 free_page((unsigned long)vcpu->run);
194 fail:
195 return r;
197 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
199 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
201 kvm_arch_vcpu_uninit(vcpu);
202 free_page((unsigned long)vcpu->run);
204 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
206 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
207 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
209 return container_of(mn, struct kvm, mmu_notifier);
212 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
213 struct mm_struct *mm,
214 unsigned long address)
216 struct kvm *kvm = mmu_notifier_to_kvm(mn);
217 int need_tlb_flush;
220 * When ->invalidate_page runs, the linux pte has been zapped
221 * already but the page is still allocated until
222 * ->invalidate_page returns. So if we increase the sequence
223 * here the kvm page fault will notice if the spte can't be
224 * established because the page is going to be freed. If
225 * instead the kvm page fault establishes the spte before
226 * ->invalidate_page runs, kvm_unmap_hva will release it
227 * before returning.
229 * The sequence increase only need to be seen at spin_unlock
230 * time, and not at spin_lock time.
232 * Increasing the sequence after the spin_unlock would be
233 * unsafe because the kvm page fault could then establish the
234 * pte after kvm_unmap_hva returned, without noticing the page
235 * is going to be freed.
237 spin_lock(&kvm->mmu_lock);
238 kvm->mmu_notifier_seq++;
239 need_tlb_flush = kvm_unmap_hva(kvm, address);
240 spin_unlock(&kvm->mmu_lock);
242 /* we've to flush the tlb before the pages can be freed */
243 if (need_tlb_flush)
244 kvm_flush_remote_tlbs(kvm);
248 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
249 struct mm_struct *mm,
250 unsigned long address,
251 pte_t pte)
253 struct kvm *kvm = mmu_notifier_to_kvm(mn);
255 spin_lock(&kvm->mmu_lock);
256 kvm->mmu_notifier_seq++;
257 kvm_set_spte_hva(kvm, address, pte);
258 spin_unlock(&kvm->mmu_lock);
261 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
262 struct mm_struct *mm,
263 unsigned long start,
264 unsigned long end)
266 struct kvm *kvm = mmu_notifier_to_kvm(mn);
267 int need_tlb_flush = 0;
269 spin_lock(&kvm->mmu_lock);
271 * The count increase must become visible at unlock time as no
272 * spte can be established without taking the mmu_lock and
273 * count is also read inside the mmu_lock critical section.
275 kvm->mmu_notifier_count++;
276 for (; start < end; start += PAGE_SIZE)
277 need_tlb_flush |= kvm_unmap_hva(kvm, start);
278 spin_unlock(&kvm->mmu_lock);
280 /* we've to flush the tlb before the pages can be freed */
281 if (need_tlb_flush)
282 kvm_flush_remote_tlbs(kvm);
285 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
286 struct mm_struct *mm,
287 unsigned long start,
288 unsigned long end)
290 struct kvm *kvm = mmu_notifier_to_kvm(mn);
292 spin_lock(&kvm->mmu_lock);
294 * This sequence increase will notify the kvm page fault that
295 * the page that is going to be mapped in the spte could have
296 * been freed.
298 kvm->mmu_notifier_seq++;
300 * The above sequence increase must be visible before the
301 * below count decrease but both values are read by the kvm
302 * page fault under mmu_lock spinlock so we don't need to add
303 * a smb_wmb() here in between the two.
305 kvm->mmu_notifier_count--;
306 spin_unlock(&kvm->mmu_lock);
308 BUG_ON(kvm->mmu_notifier_count < 0);
311 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
312 struct mm_struct *mm,
313 unsigned long address)
315 struct kvm *kvm = mmu_notifier_to_kvm(mn);
316 int young;
318 spin_lock(&kvm->mmu_lock);
319 young = kvm_age_hva(kvm, address);
320 spin_unlock(&kvm->mmu_lock);
322 if (young)
323 kvm_flush_remote_tlbs(kvm);
325 return young;
328 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
329 struct mm_struct *mm)
331 struct kvm *kvm = mmu_notifier_to_kvm(mn);
332 kvm_arch_flush_shadow(kvm);
335 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
336 .invalidate_page = kvm_mmu_notifier_invalidate_page,
337 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
338 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
339 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
340 .change_pte = kvm_mmu_notifier_change_pte,
341 .release = kvm_mmu_notifier_release,
343 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
345 static struct kvm *kvm_create_vm(void)
347 int r = 0;
348 struct kvm *kvm = kvm_arch_create_vm();
349 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
350 struct page *page;
351 #endif
353 if (IS_ERR(kvm))
354 goto out;
356 r = hardware_enable_all();
357 if (r)
358 goto out_err_nodisable;
360 #ifdef CONFIG_HAVE_KVM_IRQCHIP
361 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
362 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
363 #endif
365 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
366 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
367 if (!page) {
368 r = -ENOMEM;
369 goto out_err;
371 kvm->coalesced_mmio_ring =
372 (struct kvm_coalesced_mmio_ring *)page_address(page);
373 #endif
375 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
377 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
378 r = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
379 if (r) {
380 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
381 put_page(page);
382 #endif
383 goto out_err;
386 #endif
388 kvm->mm = current->mm;
389 atomic_inc(&kvm->mm->mm_count);
390 spin_lock_init(&kvm->mmu_lock);
391 spin_lock_init(&kvm->requests_lock);
392 kvm_io_bus_init(&kvm->pio_bus);
393 kvm_eventfd_init(kvm);
394 mutex_init(&kvm->lock);
395 mutex_init(&kvm->irq_lock);
396 kvm_io_bus_init(&kvm->mmio_bus);
397 init_rwsem(&kvm->slots_lock);
398 atomic_set(&kvm->users_count, 1);
399 spin_lock(&kvm_lock);
400 list_add(&kvm->vm_list, &vm_list);
401 spin_unlock(&kvm_lock);
402 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
403 kvm_coalesced_mmio_init(kvm);
404 #endif
405 out:
406 return kvm;
408 out_err:
409 hardware_disable_all();
410 out_err_nodisable:
411 kfree(kvm);
412 return ERR_PTR(r);
416 * Free any memory in @free but not in @dont.
418 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
419 struct kvm_memory_slot *dont)
421 int i;
423 if (!dont || free->rmap != dont->rmap)
424 vfree(free->rmap);
426 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
427 vfree(free->dirty_bitmap);
430 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
431 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
432 vfree(free->lpage_info[i]);
433 free->lpage_info[i] = NULL;
437 free->npages = 0;
438 free->dirty_bitmap = NULL;
439 free->rmap = NULL;
442 void kvm_free_physmem(struct kvm *kvm)
444 int i;
446 for (i = 0; i < kvm->nmemslots; ++i)
447 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
450 static void kvm_destroy_vm(struct kvm *kvm)
452 struct mm_struct *mm = kvm->mm;
454 kvm_arch_sync_events(kvm);
455 spin_lock(&kvm_lock);
456 list_del(&kvm->vm_list);
457 spin_unlock(&kvm_lock);
458 kvm_free_irq_routing(kvm);
459 kvm_io_bus_destroy(&kvm->pio_bus);
460 kvm_io_bus_destroy(&kvm->mmio_bus);
461 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
462 if (kvm->coalesced_mmio_ring != NULL)
463 free_page((unsigned long)kvm->coalesced_mmio_ring);
464 #endif
465 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
466 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
467 #else
468 kvm_arch_flush_shadow(kvm);
469 #endif
470 kvm_arch_destroy_vm(kvm);
471 hardware_disable_all();
472 mmdrop(mm);
475 void kvm_get_kvm(struct kvm *kvm)
477 atomic_inc(&kvm->users_count);
479 EXPORT_SYMBOL_GPL(kvm_get_kvm);
481 void kvm_put_kvm(struct kvm *kvm)
483 if (atomic_dec_and_test(&kvm->users_count))
484 kvm_destroy_vm(kvm);
486 EXPORT_SYMBOL_GPL(kvm_put_kvm);
489 static int kvm_vm_release(struct inode *inode, struct file *filp)
491 struct kvm *kvm = filp->private_data;
493 kvm_irqfd_release(kvm);
495 kvm_put_kvm(kvm);
496 return 0;
500 * Allocate some memory and give it an address in the guest physical address
501 * space.
503 * Discontiguous memory is allowed, mostly for framebuffers.
505 * Must be called holding mmap_sem for write.
507 int __kvm_set_memory_region(struct kvm *kvm,
508 struct kvm_userspace_memory_region *mem,
509 int user_alloc)
511 int r;
512 gfn_t base_gfn;
513 unsigned long npages;
514 unsigned long i;
515 struct kvm_memory_slot *memslot;
516 struct kvm_memory_slot old, new;
518 r = -EINVAL;
519 /* General sanity checks */
520 if (mem->memory_size & (PAGE_SIZE - 1))
521 goto out;
522 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
523 goto out;
524 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
525 goto out;
526 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
527 goto out;
528 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
529 goto out;
531 memslot = &kvm->memslots[mem->slot];
532 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
533 npages = mem->memory_size >> PAGE_SHIFT;
535 if (!npages)
536 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
538 new = old = *memslot;
540 new.base_gfn = base_gfn;
541 new.npages = npages;
542 new.flags = mem->flags;
544 /* Disallow changing a memory slot's size. */
545 r = -EINVAL;
546 if (npages && old.npages && npages != old.npages)
547 goto out_free;
549 /* Check for overlaps */
550 r = -EEXIST;
551 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
552 struct kvm_memory_slot *s = &kvm->memslots[i];
554 if (s == memslot || !s->npages)
555 continue;
556 if (!((base_gfn + npages <= s->base_gfn) ||
557 (base_gfn >= s->base_gfn + s->npages)))
558 goto out_free;
561 /* Free page dirty bitmap if unneeded */
562 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
563 new.dirty_bitmap = NULL;
565 r = -ENOMEM;
567 /* Allocate if a slot is being created */
568 #ifndef CONFIG_S390
569 if (npages && !new.rmap) {
570 new.rmap = vmalloc(npages * sizeof(struct page *));
572 if (!new.rmap)
573 goto out_free;
575 memset(new.rmap, 0, npages * sizeof(*new.rmap));
577 new.user_alloc = user_alloc;
579 * hva_to_rmmap() serialzies with the mmu_lock and to be
580 * safe it has to ignore memslots with !user_alloc &&
581 * !userspace_addr.
583 if (user_alloc)
584 new.userspace_addr = mem->userspace_addr;
585 else
586 new.userspace_addr = 0;
588 if (!npages)
589 goto skip_lpage;
591 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
592 unsigned long ugfn;
593 unsigned long j;
594 int lpages;
595 int level = i + 2;
597 /* Avoid unused variable warning if no large pages */
598 (void)level;
600 if (new.lpage_info[i])
601 continue;
603 lpages = 1 + (base_gfn + npages - 1) /
604 KVM_PAGES_PER_HPAGE(level);
605 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
607 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
609 if (!new.lpage_info[i])
610 goto out_free;
612 memset(new.lpage_info[i], 0,
613 lpages * sizeof(*new.lpage_info[i]));
615 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
616 new.lpage_info[i][0].write_count = 1;
617 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
618 new.lpage_info[i][lpages - 1].write_count = 1;
619 ugfn = new.userspace_addr >> PAGE_SHIFT;
621 * If the gfn and userspace address are not aligned wrt each
622 * other, or if explicitly asked to, disable large page
623 * support for this slot
625 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
626 !largepages_enabled)
627 for (j = 0; j < lpages; ++j)
628 new.lpage_info[i][j].write_count = 1;
631 skip_lpage:
633 /* Allocate page dirty bitmap if needed */
634 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
635 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
637 new.dirty_bitmap = vmalloc(dirty_bytes);
638 if (!new.dirty_bitmap)
639 goto out_free;
640 memset(new.dirty_bitmap, 0, dirty_bytes);
641 if (old.npages)
642 kvm_arch_flush_shadow(kvm);
644 #else /* not defined CONFIG_S390 */
645 new.user_alloc = user_alloc;
646 if (user_alloc)
647 new.userspace_addr = mem->userspace_addr;
648 #endif /* not defined CONFIG_S390 */
650 if (!npages)
651 kvm_arch_flush_shadow(kvm);
653 spin_lock(&kvm->mmu_lock);
654 if (mem->slot >= kvm->nmemslots)
655 kvm->nmemslots = mem->slot + 1;
657 *memslot = new;
658 spin_unlock(&kvm->mmu_lock);
660 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
661 if (r) {
662 spin_lock(&kvm->mmu_lock);
663 *memslot = old;
664 spin_unlock(&kvm->mmu_lock);
665 goto out_free;
668 kvm_free_physmem_slot(&old, npages ? &new : NULL);
669 /* Slot deletion case: we have to update the current slot */
670 spin_lock(&kvm->mmu_lock);
671 if (!npages)
672 *memslot = old;
673 spin_unlock(&kvm->mmu_lock);
674 #ifdef CONFIG_DMAR
675 /* map the pages in iommu page table */
676 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
677 if (r)
678 goto out;
679 #endif
680 return 0;
682 out_free:
683 kvm_free_physmem_slot(&new, &old);
684 out:
685 return r;
688 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
690 int kvm_set_memory_region(struct kvm *kvm,
691 struct kvm_userspace_memory_region *mem,
692 int user_alloc)
694 int r;
696 down_write(&kvm->slots_lock);
697 r = __kvm_set_memory_region(kvm, mem, user_alloc);
698 up_write(&kvm->slots_lock);
699 return r;
701 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
703 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
704 struct
705 kvm_userspace_memory_region *mem,
706 int user_alloc)
708 if (mem->slot >= KVM_MEMORY_SLOTS)
709 return -EINVAL;
710 return kvm_set_memory_region(kvm, mem, user_alloc);
713 int kvm_get_dirty_log(struct kvm *kvm,
714 struct kvm_dirty_log *log, int *is_dirty)
716 struct kvm_memory_slot *memslot;
717 int r, i;
718 int n;
719 unsigned long any = 0;
721 r = -EINVAL;
722 if (log->slot >= KVM_MEMORY_SLOTS)
723 goto out;
725 memslot = &kvm->memslots[log->slot];
726 r = -ENOENT;
727 if (!memslot->dirty_bitmap)
728 goto out;
730 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
732 for (i = 0; !any && i < n/sizeof(long); ++i)
733 any = memslot->dirty_bitmap[i];
735 r = -EFAULT;
736 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
737 goto out;
739 if (any)
740 *is_dirty = 1;
742 r = 0;
743 out:
744 return r;
747 void kvm_disable_largepages(void)
749 largepages_enabled = false;
751 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
753 int is_error_page(struct page *page)
755 return page == bad_page;
757 EXPORT_SYMBOL_GPL(is_error_page);
759 int is_error_pfn(pfn_t pfn)
761 return pfn == bad_pfn;
763 EXPORT_SYMBOL_GPL(is_error_pfn);
765 static inline unsigned long bad_hva(void)
767 return PAGE_OFFSET;
770 int kvm_is_error_hva(unsigned long addr)
772 return addr == bad_hva();
774 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
776 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
778 int i;
780 for (i = 0; i < kvm->nmemslots; ++i) {
781 struct kvm_memory_slot *memslot = &kvm->memslots[i];
783 if (gfn >= memslot->base_gfn
784 && gfn < memslot->base_gfn + memslot->npages)
785 return memslot;
787 return NULL;
789 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
791 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
793 gfn = unalias_gfn(kvm, gfn);
794 return gfn_to_memslot_unaliased(kvm, gfn);
797 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
799 int i;
801 gfn = unalias_gfn(kvm, gfn);
802 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
803 struct kvm_memory_slot *memslot = &kvm->memslots[i];
805 if (gfn >= memslot->base_gfn
806 && gfn < memslot->base_gfn + memslot->npages)
807 return 1;
809 return 0;
811 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
813 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
815 struct kvm_memory_slot *slot;
817 gfn = unalias_gfn(kvm, gfn);
818 slot = gfn_to_memslot_unaliased(kvm, gfn);
819 if (!slot)
820 return bad_hva();
821 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
823 EXPORT_SYMBOL_GPL(gfn_to_hva);
825 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
827 struct page *page[1];
828 unsigned long addr;
829 int npages;
830 pfn_t pfn;
832 might_sleep();
834 addr = gfn_to_hva(kvm, gfn);
835 if (kvm_is_error_hva(addr)) {
836 get_page(bad_page);
837 return page_to_pfn(bad_page);
840 npages = get_user_pages_fast(addr, 1, 1, page);
842 if (unlikely(npages != 1)) {
843 struct vm_area_struct *vma;
845 down_read(&current->mm->mmap_sem);
846 vma = find_vma(current->mm, addr);
848 if (vma == NULL || addr < vma->vm_start ||
849 !(vma->vm_flags & VM_PFNMAP)) {
850 up_read(&current->mm->mmap_sem);
851 get_page(bad_page);
852 return page_to_pfn(bad_page);
855 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
856 up_read(&current->mm->mmap_sem);
857 BUG_ON(!kvm_is_mmio_pfn(pfn));
858 } else
859 pfn = page_to_pfn(page[0]);
861 return pfn;
864 EXPORT_SYMBOL_GPL(gfn_to_pfn);
866 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
868 pfn_t pfn;
870 pfn = gfn_to_pfn(kvm, gfn);
871 if (!kvm_is_mmio_pfn(pfn))
872 return pfn_to_page(pfn);
874 WARN_ON(kvm_is_mmio_pfn(pfn));
876 get_page(bad_page);
877 return bad_page;
880 EXPORT_SYMBOL_GPL(gfn_to_page);
882 void kvm_release_page_clean(struct page *page)
884 kvm_release_pfn_clean(page_to_pfn(page));
886 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
888 void kvm_release_pfn_clean(pfn_t pfn)
890 if (!kvm_is_mmio_pfn(pfn))
891 put_page(pfn_to_page(pfn));
893 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
895 void kvm_release_page_dirty(struct page *page)
897 kvm_release_pfn_dirty(page_to_pfn(page));
899 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
901 void kvm_release_pfn_dirty(pfn_t pfn)
903 kvm_set_pfn_dirty(pfn);
904 kvm_release_pfn_clean(pfn);
906 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
908 void kvm_set_page_dirty(struct page *page)
910 kvm_set_pfn_dirty(page_to_pfn(page));
912 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
914 void kvm_set_pfn_dirty(pfn_t pfn)
916 if (!kvm_is_mmio_pfn(pfn)) {
917 struct page *page = pfn_to_page(pfn);
918 if (!PageReserved(page))
919 SetPageDirty(page);
922 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
924 void kvm_set_pfn_accessed(pfn_t pfn)
926 if (!kvm_is_mmio_pfn(pfn))
927 mark_page_accessed(pfn_to_page(pfn));
929 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
931 void kvm_get_pfn(pfn_t pfn)
933 if (!kvm_is_mmio_pfn(pfn))
934 get_page(pfn_to_page(pfn));
936 EXPORT_SYMBOL_GPL(kvm_get_pfn);
938 static int next_segment(unsigned long len, int offset)
940 if (len > PAGE_SIZE - offset)
941 return PAGE_SIZE - offset;
942 else
943 return len;
946 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
947 int len)
949 int r;
950 unsigned long addr;
952 addr = gfn_to_hva(kvm, gfn);
953 if (kvm_is_error_hva(addr))
954 return -EFAULT;
955 r = copy_from_user(data, (void __user *)addr + offset, len);
956 if (r)
957 return -EFAULT;
958 return 0;
960 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
962 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
964 gfn_t gfn = gpa >> PAGE_SHIFT;
965 int seg;
966 int offset = offset_in_page(gpa);
967 int ret;
969 while ((seg = next_segment(len, offset)) != 0) {
970 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
971 if (ret < 0)
972 return ret;
973 offset = 0;
974 len -= seg;
975 data += seg;
976 ++gfn;
978 return 0;
980 EXPORT_SYMBOL_GPL(kvm_read_guest);
982 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
983 unsigned long len)
985 int r;
986 unsigned long addr;
987 gfn_t gfn = gpa >> PAGE_SHIFT;
988 int offset = offset_in_page(gpa);
990 addr = gfn_to_hva(kvm, gfn);
991 if (kvm_is_error_hva(addr))
992 return -EFAULT;
993 pagefault_disable();
994 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
995 pagefault_enable();
996 if (r)
997 return -EFAULT;
998 return 0;
1000 EXPORT_SYMBOL(kvm_read_guest_atomic);
1002 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1003 int offset, int len)
1005 int r;
1006 unsigned long addr;
1008 addr = gfn_to_hva(kvm, gfn);
1009 if (kvm_is_error_hva(addr))
1010 return -EFAULT;
1011 r = copy_to_user((void __user *)addr + offset, data, len);
1012 if (r)
1013 return -EFAULT;
1014 mark_page_dirty(kvm, gfn);
1015 return 0;
1017 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1019 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1020 unsigned long len)
1022 gfn_t gfn = gpa >> PAGE_SHIFT;
1023 int seg;
1024 int offset = offset_in_page(gpa);
1025 int ret;
1027 while ((seg = next_segment(len, offset)) != 0) {
1028 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1029 if (ret < 0)
1030 return ret;
1031 offset = 0;
1032 len -= seg;
1033 data += seg;
1034 ++gfn;
1036 return 0;
1039 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1041 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1043 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1045 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1047 gfn_t gfn = gpa >> PAGE_SHIFT;
1048 int seg;
1049 int offset = offset_in_page(gpa);
1050 int ret;
1052 while ((seg = next_segment(len, offset)) != 0) {
1053 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1054 if (ret < 0)
1055 return ret;
1056 offset = 0;
1057 len -= seg;
1058 ++gfn;
1060 return 0;
1062 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1064 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1066 struct kvm_memory_slot *memslot;
1068 gfn = unalias_gfn(kvm, gfn);
1069 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1070 if (memslot && memslot->dirty_bitmap) {
1071 unsigned long rel_gfn = gfn - memslot->base_gfn;
1073 /* avoid RMW */
1074 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1075 set_bit(rel_gfn, memslot->dirty_bitmap);
1080 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1082 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1084 DEFINE_WAIT(wait);
1086 for (;;) {
1087 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1089 if (kvm_arch_vcpu_runnable(vcpu)) {
1090 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1091 break;
1093 if (kvm_cpu_has_pending_timer(vcpu))
1094 break;
1095 if (signal_pending(current))
1096 break;
1098 schedule();
1101 finish_wait(&vcpu->wq, &wait);
1104 void kvm_resched(struct kvm_vcpu *vcpu)
1106 if (!need_resched())
1107 return;
1108 cond_resched();
1110 EXPORT_SYMBOL_GPL(kvm_resched);
1112 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1114 ktime_t expires;
1115 DEFINE_WAIT(wait);
1117 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1119 /* Sleep for 100 us, and hope lock-holder got scheduled */
1120 expires = ktime_add_ns(ktime_get(), 100000UL);
1121 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1123 finish_wait(&vcpu->wq, &wait);
1125 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1127 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1129 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1130 struct page *page;
1132 if (vmf->pgoff == 0)
1133 page = virt_to_page(vcpu->run);
1134 #ifdef CONFIG_X86
1135 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1136 page = virt_to_page(vcpu->arch.pio_data);
1137 #endif
1138 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1139 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1140 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1141 #endif
1142 else
1143 return VM_FAULT_SIGBUS;
1144 get_page(page);
1145 vmf->page = page;
1146 return 0;
1149 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1150 .fault = kvm_vcpu_fault,
1153 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1155 vma->vm_ops = &kvm_vcpu_vm_ops;
1156 return 0;
1159 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1161 struct kvm_vcpu *vcpu = filp->private_data;
1163 kvm_put_kvm(vcpu->kvm);
1164 return 0;
1167 static struct file_operations kvm_vcpu_fops = {
1168 .release = kvm_vcpu_release,
1169 .unlocked_ioctl = kvm_vcpu_ioctl,
1170 .compat_ioctl = kvm_vcpu_ioctl,
1171 .mmap = kvm_vcpu_mmap,
1175 * Allocates an inode for the vcpu.
1177 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1179 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1183 * Creates some virtual cpus. Good luck creating more than one.
1185 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1187 int r;
1188 struct kvm_vcpu *vcpu, *v;
1190 vcpu = kvm_arch_vcpu_create(kvm, id);
1191 if (IS_ERR(vcpu))
1192 return PTR_ERR(vcpu);
1194 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1196 r = kvm_arch_vcpu_setup(vcpu);
1197 if (r)
1198 return r;
1200 mutex_lock(&kvm->lock);
1201 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1202 r = -EINVAL;
1203 goto vcpu_destroy;
1206 kvm_for_each_vcpu(r, v, kvm)
1207 if (v->vcpu_id == id) {
1208 r = -EEXIST;
1209 goto vcpu_destroy;
1212 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1214 /* Now it's all set up, let userspace reach it */
1215 kvm_get_kvm(kvm);
1216 r = create_vcpu_fd(vcpu);
1217 if (r < 0) {
1218 kvm_put_kvm(kvm);
1219 goto vcpu_destroy;
1222 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1223 smp_wmb();
1224 atomic_inc(&kvm->online_vcpus);
1226 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1227 if (kvm->bsp_vcpu_id == id)
1228 kvm->bsp_vcpu = vcpu;
1229 #endif
1230 mutex_unlock(&kvm->lock);
1231 return r;
1233 vcpu_destroy:
1234 mutex_unlock(&kvm->lock);
1235 kvm_arch_vcpu_destroy(vcpu);
1236 return r;
1239 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1241 if (sigset) {
1242 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1243 vcpu->sigset_active = 1;
1244 vcpu->sigset = *sigset;
1245 } else
1246 vcpu->sigset_active = 0;
1247 return 0;
1250 static long kvm_vcpu_ioctl(struct file *filp,
1251 unsigned int ioctl, unsigned long arg)
1253 struct kvm_vcpu *vcpu = filp->private_data;
1254 void __user *argp = (void __user *)arg;
1255 int r;
1256 struct kvm_fpu *fpu = NULL;
1257 struct kvm_sregs *kvm_sregs = NULL;
1259 if (vcpu->kvm->mm != current->mm)
1260 return -EIO;
1261 switch (ioctl) {
1262 case KVM_RUN:
1263 r = -EINVAL;
1264 if (arg)
1265 goto out;
1266 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1267 break;
1268 case KVM_GET_REGS: {
1269 struct kvm_regs *kvm_regs;
1271 r = -ENOMEM;
1272 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1273 if (!kvm_regs)
1274 goto out;
1275 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1276 if (r)
1277 goto out_free1;
1278 r = -EFAULT;
1279 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1280 goto out_free1;
1281 r = 0;
1282 out_free1:
1283 kfree(kvm_regs);
1284 break;
1286 case KVM_SET_REGS: {
1287 struct kvm_regs *kvm_regs;
1289 r = -ENOMEM;
1290 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1291 if (!kvm_regs)
1292 goto out;
1293 r = -EFAULT;
1294 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1295 goto out_free2;
1296 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1297 if (r)
1298 goto out_free2;
1299 r = 0;
1300 out_free2:
1301 kfree(kvm_regs);
1302 break;
1304 case KVM_GET_SREGS: {
1305 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1306 r = -ENOMEM;
1307 if (!kvm_sregs)
1308 goto out;
1309 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1310 if (r)
1311 goto out;
1312 r = -EFAULT;
1313 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1314 goto out;
1315 r = 0;
1316 break;
1318 case KVM_SET_SREGS: {
1319 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1320 r = -ENOMEM;
1321 if (!kvm_sregs)
1322 goto out;
1323 r = -EFAULT;
1324 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1325 goto out;
1326 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1327 if (r)
1328 goto out;
1329 r = 0;
1330 break;
1332 case KVM_GET_MP_STATE: {
1333 struct kvm_mp_state mp_state;
1335 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1336 if (r)
1337 goto out;
1338 r = -EFAULT;
1339 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1340 goto out;
1341 r = 0;
1342 break;
1344 case KVM_SET_MP_STATE: {
1345 struct kvm_mp_state mp_state;
1347 r = -EFAULT;
1348 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1349 goto out;
1350 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1351 if (r)
1352 goto out;
1353 r = 0;
1354 break;
1356 case KVM_TRANSLATE: {
1357 struct kvm_translation tr;
1359 r = -EFAULT;
1360 if (copy_from_user(&tr, argp, sizeof tr))
1361 goto out;
1362 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1363 if (r)
1364 goto out;
1365 r = -EFAULT;
1366 if (copy_to_user(argp, &tr, sizeof tr))
1367 goto out;
1368 r = 0;
1369 break;
1371 case KVM_SET_GUEST_DEBUG: {
1372 struct kvm_guest_debug dbg;
1374 r = -EFAULT;
1375 if (copy_from_user(&dbg, argp, sizeof dbg))
1376 goto out;
1377 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1378 if (r)
1379 goto out;
1380 r = 0;
1381 break;
1383 case KVM_SET_SIGNAL_MASK: {
1384 struct kvm_signal_mask __user *sigmask_arg = argp;
1385 struct kvm_signal_mask kvm_sigmask;
1386 sigset_t sigset, *p;
1388 p = NULL;
1389 if (argp) {
1390 r = -EFAULT;
1391 if (copy_from_user(&kvm_sigmask, argp,
1392 sizeof kvm_sigmask))
1393 goto out;
1394 r = -EINVAL;
1395 if (kvm_sigmask.len != sizeof sigset)
1396 goto out;
1397 r = -EFAULT;
1398 if (copy_from_user(&sigset, sigmask_arg->sigset,
1399 sizeof sigset))
1400 goto out;
1401 p = &sigset;
1403 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1404 break;
1406 case KVM_GET_FPU: {
1407 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1408 r = -ENOMEM;
1409 if (!fpu)
1410 goto out;
1411 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1412 if (r)
1413 goto out;
1414 r = -EFAULT;
1415 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1416 goto out;
1417 r = 0;
1418 break;
1420 case KVM_SET_FPU: {
1421 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1422 r = -ENOMEM;
1423 if (!fpu)
1424 goto out;
1425 r = -EFAULT;
1426 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1427 goto out;
1428 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1429 if (r)
1430 goto out;
1431 r = 0;
1432 break;
1434 default:
1435 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1437 out:
1438 kfree(fpu);
1439 kfree(kvm_sregs);
1440 return r;
1443 static long kvm_vm_ioctl(struct file *filp,
1444 unsigned int ioctl, unsigned long arg)
1446 struct kvm *kvm = filp->private_data;
1447 void __user *argp = (void __user *)arg;
1448 int r;
1450 if (kvm->mm != current->mm)
1451 return -EIO;
1452 switch (ioctl) {
1453 case KVM_CREATE_VCPU:
1454 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1455 if (r < 0)
1456 goto out;
1457 break;
1458 case KVM_SET_USER_MEMORY_REGION: {
1459 struct kvm_userspace_memory_region kvm_userspace_mem;
1461 r = -EFAULT;
1462 if (copy_from_user(&kvm_userspace_mem, argp,
1463 sizeof kvm_userspace_mem))
1464 goto out;
1466 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1467 if (r)
1468 goto out;
1469 break;
1471 case KVM_GET_DIRTY_LOG: {
1472 struct kvm_dirty_log log;
1474 r = -EFAULT;
1475 if (copy_from_user(&log, argp, sizeof log))
1476 goto out;
1477 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1478 if (r)
1479 goto out;
1480 break;
1482 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1483 case KVM_REGISTER_COALESCED_MMIO: {
1484 struct kvm_coalesced_mmio_zone zone;
1485 r = -EFAULT;
1486 if (copy_from_user(&zone, argp, sizeof zone))
1487 goto out;
1488 r = -ENXIO;
1489 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1490 if (r)
1491 goto out;
1492 r = 0;
1493 break;
1495 case KVM_UNREGISTER_COALESCED_MMIO: {
1496 struct kvm_coalesced_mmio_zone zone;
1497 r = -EFAULT;
1498 if (copy_from_user(&zone, argp, sizeof zone))
1499 goto out;
1500 r = -ENXIO;
1501 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1502 if (r)
1503 goto out;
1504 r = 0;
1505 break;
1507 #endif
1508 case KVM_IRQFD: {
1509 struct kvm_irqfd data;
1511 r = -EFAULT;
1512 if (copy_from_user(&data, argp, sizeof data))
1513 goto out;
1514 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1515 break;
1517 case KVM_IOEVENTFD: {
1518 struct kvm_ioeventfd data;
1520 r = -EFAULT;
1521 if (copy_from_user(&data, argp, sizeof data))
1522 goto out;
1523 r = kvm_ioeventfd(kvm, &data);
1524 break;
1526 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1527 case KVM_SET_BOOT_CPU_ID:
1528 r = 0;
1529 mutex_lock(&kvm->lock);
1530 if (atomic_read(&kvm->online_vcpus) != 0)
1531 r = -EBUSY;
1532 else
1533 kvm->bsp_vcpu_id = arg;
1534 mutex_unlock(&kvm->lock);
1535 break;
1536 #endif
1537 default:
1538 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1539 if (r == -ENOTTY)
1540 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
1542 out:
1543 return r;
1546 #ifdef CONFIG_COMPAT
1547 struct compat_kvm_dirty_log {
1548 __u32 slot;
1549 __u32 padding1;
1550 union {
1551 compat_uptr_t dirty_bitmap; /* one bit per page */
1552 __u64 padding2;
1556 static long kvm_vm_compat_ioctl(struct file *filp,
1557 unsigned int ioctl, unsigned long arg)
1559 struct kvm *kvm = filp->private_data;
1560 int r;
1562 if (kvm->mm != current->mm)
1563 return -EIO;
1564 switch (ioctl) {
1565 case KVM_GET_DIRTY_LOG: {
1566 struct compat_kvm_dirty_log compat_log;
1567 struct kvm_dirty_log log;
1569 r = -EFAULT;
1570 if (copy_from_user(&compat_log, (void __user *)arg,
1571 sizeof(compat_log)))
1572 goto out;
1573 log.slot = compat_log.slot;
1574 log.padding1 = compat_log.padding1;
1575 log.padding2 = compat_log.padding2;
1576 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1578 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1579 if (r)
1580 goto out;
1581 break;
1583 default:
1584 r = kvm_vm_ioctl(filp, ioctl, arg);
1587 out:
1588 return r;
1590 #endif
1592 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1594 struct page *page[1];
1595 unsigned long addr;
1596 int npages;
1597 gfn_t gfn = vmf->pgoff;
1598 struct kvm *kvm = vma->vm_file->private_data;
1600 addr = gfn_to_hva(kvm, gfn);
1601 if (kvm_is_error_hva(addr))
1602 return VM_FAULT_SIGBUS;
1604 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1605 NULL);
1606 if (unlikely(npages != 1))
1607 return VM_FAULT_SIGBUS;
1609 vmf->page = page[0];
1610 return 0;
1613 static const struct vm_operations_struct kvm_vm_vm_ops = {
1614 .fault = kvm_vm_fault,
1617 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1619 vma->vm_ops = &kvm_vm_vm_ops;
1620 return 0;
1623 static struct file_operations kvm_vm_fops = {
1624 .release = kvm_vm_release,
1625 .unlocked_ioctl = kvm_vm_ioctl,
1626 #ifdef CONFIG_COMPAT
1627 .compat_ioctl = kvm_vm_compat_ioctl,
1628 #endif
1629 .mmap = kvm_vm_mmap,
1632 static int kvm_dev_ioctl_create_vm(void)
1634 int fd;
1635 struct kvm *kvm;
1637 kvm = kvm_create_vm();
1638 if (IS_ERR(kvm))
1639 return PTR_ERR(kvm);
1640 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1641 if (fd < 0)
1642 kvm_put_kvm(kvm);
1644 return fd;
1647 static long kvm_dev_ioctl_check_extension_generic(long arg)
1649 switch (arg) {
1650 case KVM_CAP_USER_MEMORY:
1651 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
1652 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
1653 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1654 case KVM_CAP_SET_BOOT_CPU_ID:
1655 #endif
1656 return 1;
1657 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1658 case KVM_CAP_IRQ_ROUTING:
1659 return KVM_MAX_IRQ_ROUTES;
1660 #endif
1661 default:
1662 break;
1664 return kvm_dev_ioctl_check_extension(arg);
1667 static long kvm_dev_ioctl(struct file *filp,
1668 unsigned int ioctl, unsigned long arg)
1670 long r = -EINVAL;
1672 switch (ioctl) {
1673 case KVM_GET_API_VERSION:
1674 r = -EINVAL;
1675 if (arg)
1676 goto out;
1677 r = KVM_API_VERSION;
1678 break;
1679 case KVM_CREATE_VM:
1680 r = -EINVAL;
1681 if (arg)
1682 goto out;
1683 r = kvm_dev_ioctl_create_vm();
1684 break;
1685 case KVM_CHECK_EXTENSION:
1686 r = kvm_dev_ioctl_check_extension_generic(arg);
1687 break;
1688 case KVM_GET_VCPU_MMAP_SIZE:
1689 r = -EINVAL;
1690 if (arg)
1691 goto out;
1692 r = PAGE_SIZE; /* struct kvm_run */
1693 #ifdef CONFIG_X86
1694 r += PAGE_SIZE; /* pio data page */
1695 #endif
1696 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1697 r += PAGE_SIZE; /* coalesced mmio ring page */
1698 #endif
1699 break;
1700 case KVM_TRACE_ENABLE:
1701 case KVM_TRACE_PAUSE:
1702 case KVM_TRACE_DISABLE:
1703 r = -EOPNOTSUPP;
1704 break;
1705 default:
1706 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1708 out:
1709 return r;
1712 static struct file_operations kvm_chardev_ops = {
1713 .unlocked_ioctl = kvm_dev_ioctl,
1714 .compat_ioctl = kvm_dev_ioctl,
1717 static struct miscdevice kvm_dev = {
1718 KVM_MINOR,
1719 "kvm",
1720 &kvm_chardev_ops,
1723 static void hardware_enable(void *junk)
1725 int cpu = raw_smp_processor_id();
1726 int r;
1728 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
1729 return;
1731 cpumask_set_cpu(cpu, cpus_hardware_enabled);
1733 r = kvm_arch_hardware_enable(NULL);
1735 if (r) {
1736 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1737 atomic_inc(&hardware_enable_failed);
1738 printk(KERN_INFO "kvm: enabling virtualization on "
1739 "CPU%d failed\n", cpu);
1743 static void hardware_disable(void *junk)
1745 int cpu = raw_smp_processor_id();
1747 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
1748 return;
1749 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1750 kvm_arch_hardware_disable(NULL);
1753 static void hardware_disable_all_nolock(void)
1755 BUG_ON(!kvm_usage_count);
1757 kvm_usage_count--;
1758 if (!kvm_usage_count)
1759 on_each_cpu(hardware_disable, NULL, 1);
1762 static void hardware_disable_all(void)
1764 spin_lock(&kvm_lock);
1765 hardware_disable_all_nolock();
1766 spin_unlock(&kvm_lock);
1769 static int hardware_enable_all(void)
1771 int r = 0;
1773 spin_lock(&kvm_lock);
1775 kvm_usage_count++;
1776 if (kvm_usage_count == 1) {
1777 atomic_set(&hardware_enable_failed, 0);
1778 on_each_cpu(hardware_enable, NULL, 1);
1780 if (atomic_read(&hardware_enable_failed)) {
1781 hardware_disable_all_nolock();
1782 r = -EBUSY;
1786 spin_unlock(&kvm_lock);
1788 return r;
1791 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1792 void *v)
1794 int cpu = (long)v;
1796 if (!kvm_usage_count)
1797 return NOTIFY_OK;
1799 val &= ~CPU_TASKS_FROZEN;
1800 switch (val) {
1801 case CPU_DYING:
1802 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1803 cpu);
1804 hardware_disable(NULL);
1805 break;
1806 case CPU_UP_CANCELED:
1807 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1808 cpu);
1809 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1810 break;
1811 case CPU_ONLINE:
1812 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1813 cpu);
1814 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1815 break;
1817 return NOTIFY_OK;
1821 asmlinkage void kvm_handle_fault_on_reboot(void)
1823 if (kvm_rebooting)
1824 /* spin while reset goes on */
1825 while (true)
1827 /* Fault while not rebooting. We want the trace. */
1828 BUG();
1830 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1832 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1833 void *v)
1836 * Some (well, at least mine) BIOSes hang on reboot if
1837 * in vmx root mode.
1839 * And Intel TXT required VMX off for all cpu when system shutdown.
1841 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1842 kvm_rebooting = true;
1843 on_each_cpu(hardware_disable, NULL, 1);
1844 return NOTIFY_OK;
1847 static struct notifier_block kvm_reboot_notifier = {
1848 .notifier_call = kvm_reboot,
1849 .priority = 0,
1852 void kvm_io_bus_init(struct kvm_io_bus *bus)
1854 memset(bus, 0, sizeof(*bus));
1857 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1859 int i;
1861 for (i = 0; i < bus->dev_count; i++) {
1862 struct kvm_io_device *pos = bus->devs[i];
1864 kvm_iodevice_destructor(pos);
1868 /* kvm_io_bus_write - called under kvm->slots_lock */
1869 int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
1870 int len, const void *val)
1872 int i;
1873 for (i = 0; i < bus->dev_count; i++)
1874 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1875 return 0;
1876 return -EOPNOTSUPP;
1879 /* kvm_io_bus_read - called under kvm->slots_lock */
1880 int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
1882 int i;
1883 for (i = 0; i < bus->dev_count; i++)
1884 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
1885 return 0;
1886 return -EOPNOTSUPP;
1889 int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
1890 struct kvm_io_device *dev)
1892 int ret;
1894 down_write(&kvm->slots_lock);
1895 ret = __kvm_io_bus_register_dev(bus, dev);
1896 up_write(&kvm->slots_lock);
1898 return ret;
1901 /* An unlocked version. Caller must have write lock on slots_lock. */
1902 int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
1903 struct kvm_io_device *dev)
1905 if (bus->dev_count > NR_IOBUS_DEVS-1)
1906 return -ENOSPC;
1908 bus->devs[bus->dev_count++] = dev;
1910 return 0;
1913 void kvm_io_bus_unregister_dev(struct kvm *kvm,
1914 struct kvm_io_bus *bus,
1915 struct kvm_io_device *dev)
1917 down_write(&kvm->slots_lock);
1918 __kvm_io_bus_unregister_dev(bus, dev);
1919 up_write(&kvm->slots_lock);
1922 /* An unlocked version. Caller must have write lock on slots_lock. */
1923 void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
1924 struct kvm_io_device *dev)
1926 int i;
1928 for (i = 0; i < bus->dev_count; i++)
1929 if (bus->devs[i] == dev) {
1930 bus->devs[i] = bus->devs[--bus->dev_count];
1931 break;
1935 static struct notifier_block kvm_cpu_notifier = {
1936 .notifier_call = kvm_cpu_hotplug,
1937 .priority = 20, /* must be > scheduler priority */
1940 static int vm_stat_get(void *_offset, u64 *val)
1942 unsigned offset = (long)_offset;
1943 struct kvm *kvm;
1945 *val = 0;
1946 spin_lock(&kvm_lock);
1947 list_for_each_entry(kvm, &vm_list, vm_list)
1948 *val += *(u32 *)((void *)kvm + offset);
1949 spin_unlock(&kvm_lock);
1950 return 0;
1953 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1955 static int vcpu_stat_get(void *_offset, u64 *val)
1957 unsigned offset = (long)_offset;
1958 struct kvm *kvm;
1959 struct kvm_vcpu *vcpu;
1960 int i;
1962 *val = 0;
1963 spin_lock(&kvm_lock);
1964 list_for_each_entry(kvm, &vm_list, vm_list)
1965 kvm_for_each_vcpu(i, vcpu, kvm)
1966 *val += *(u32 *)((void *)vcpu + offset);
1968 spin_unlock(&kvm_lock);
1969 return 0;
1972 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1974 static const struct file_operations *stat_fops[] = {
1975 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1976 [KVM_STAT_VM] = &vm_stat_fops,
1979 static void kvm_init_debug(void)
1981 struct kvm_stats_debugfs_item *p;
1983 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
1984 for (p = debugfs_entries; p->name; ++p)
1985 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1986 (void *)(long)p->offset,
1987 stat_fops[p->kind]);
1990 static void kvm_exit_debug(void)
1992 struct kvm_stats_debugfs_item *p;
1994 for (p = debugfs_entries; p->name; ++p)
1995 debugfs_remove(p->dentry);
1996 debugfs_remove(kvm_debugfs_dir);
1999 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2001 if (kvm_usage_count)
2002 hardware_disable(NULL);
2003 return 0;
2006 static int kvm_resume(struct sys_device *dev)
2008 if (kvm_usage_count)
2009 hardware_enable(NULL);
2010 return 0;
2013 static struct sysdev_class kvm_sysdev_class = {
2014 .name = "kvm",
2015 .suspend = kvm_suspend,
2016 .resume = kvm_resume,
2019 static struct sys_device kvm_sysdev = {
2020 .id = 0,
2021 .cls = &kvm_sysdev_class,
2024 struct page *bad_page;
2025 pfn_t bad_pfn;
2027 static inline
2028 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2030 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2033 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2035 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2037 kvm_arch_vcpu_load(vcpu, cpu);
2040 static void kvm_sched_out(struct preempt_notifier *pn,
2041 struct task_struct *next)
2043 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2045 kvm_arch_vcpu_put(vcpu);
2048 int kvm_init(void *opaque, unsigned int vcpu_size,
2049 struct module *module)
2051 int r;
2052 int cpu;
2054 r = kvm_arch_init(opaque);
2055 if (r)
2056 goto out_fail;
2058 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2060 if (bad_page == NULL) {
2061 r = -ENOMEM;
2062 goto out;
2065 bad_pfn = page_to_pfn(bad_page);
2067 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2068 r = -ENOMEM;
2069 goto out_free_0;
2072 r = kvm_arch_hardware_setup();
2073 if (r < 0)
2074 goto out_free_0a;
2076 for_each_online_cpu(cpu) {
2077 smp_call_function_single(cpu,
2078 kvm_arch_check_processor_compat,
2079 &r, 1);
2080 if (r < 0)
2081 goto out_free_1;
2084 r = register_cpu_notifier(&kvm_cpu_notifier);
2085 if (r)
2086 goto out_free_2;
2087 register_reboot_notifier(&kvm_reboot_notifier);
2089 r = sysdev_class_register(&kvm_sysdev_class);
2090 if (r)
2091 goto out_free_3;
2093 r = sysdev_register(&kvm_sysdev);
2094 if (r)
2095 goto out_free_4;
2097 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2098 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2099 __alignof__(struct kvm_vcpu),
2100 0, NULL);
2101 if (!kvm_vcpu_cache) {
2102 r = -ENOMEM;
2103 goto out_free_5;
2106 kvm_chardev_ops.owner = module;
2107 kvm_vm_fops.owner = module;
2108 kvm_vcpu_fops.owner = module;
2110 r = misc_register(&kvm_dev);
2111 if (r) {
2112 printk(KERN_ERR "kvm: misc device register failed\n");
2113 goto out_free;
2116 kvm_preempt_ops.sched_in = kvm_sched_in;
2117 kvm_preempt_ops.sched_out = kvm_sched_out;
2119 kvm_init_debug();
2121 return 0;
2123 out_free:
2124 kmem_cache_destroy(kvm_vcpu_cache);
2125 out_free_5:
2126 sysdev_unregister(&kvm_sysdev);
2127 out_free_4:
2128 sysdev_class_unregister(&kvm_sysdev_class);
2129 out_free_3:
2130 unregister_reboot_notifier(&kvm_reboot_notifier);
2131 unregister_cpu_notifier(&kvm_cpu_notifier);
2132 out_free_2:
2133 out_free_1:
2134 kvm_arch_hardware_unsetup();
2135 out_free_0a:
2136 free_cpumask_var(cpus_hardware_enabled);
2137 out_free_0:
2138 __free_page(bad_page);
2139 out:
2140 kvm_arch_exit();
2141 out_fail:
2142 return r;
2144 EXPORT_SYMBOL_GPL(kvm_init);
2146 void kvm_exit(void)
2148 tracepoint_synchronize_unregister();
2149 kvm_exit_debug();
2150 misc_deregister(&kvm_dev);
2151 kmem_cache_destroy(kvm_vcpu_cache);
2152 sysdev_unregister(&kvm_sysdev);
2153 sysdev_class_unregister(&kvm_sysdev_class);
2154 unregister_reboot_notifier(&kvm_reboot_notifier);
2155 unregister_cpu_notifier(&kvm_cpu_notifier);
2156 on_each_cpu(hardware_disable, NULL, 1);
2157 kvm_arch_hardware_unsetup();
2158 kvm_arch_exit();
2159 free_cpumask_var(cpus_hardware_enabled);
2160 __free_page(bad_page);
2162 EXPORT_SYMBOL_GPL(kvm_exit);