Avoid beyond bounds copy while caching ACL
[zen-stable.git] / virt / kvm / kvm_main.c
blob785822896ced66d694bd0d941e26c4833b87ae34
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.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10 * Authors:
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.
19 #include "iodev.h"
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>
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/syscore_ops.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>
50 #include <linux/sort.h>
51 #include <linux/bsearch.h>
53 #include <asm/processor.h>
54 #include <asm/io.h>
55 #include <asm/uaccess.h>
56 #include <asm/pgtable.h>
58 #include "coalesced_mmio.h"
59 #include "async_pf.h"
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/kvm.h>
64 MODULE_AUTHOR("Qumranet");
65 MODULE_LICENSE("GPL");
68 * Ordering of locks:
70 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
73 DEFINE_RAW_SPINLOCK(kvm_lock);
74 LIST_HEAD(vm_list);
76 static cpumask_var_t cpus_hardware_enabled;
77 static int kvm_usage_count = 0;
78 static atomic_t hardware_enable_failed;
80 struct kmem_cache *kvm_vcpu_cache;
81 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
83 static __read_mostly struct preempt_ops kvm_preempt_ops;
85 struct dentry *kvm_debugfs_dir;
87 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
88 unsigned long arg);
89 #ifdef CONFIG_COMPAT
90 static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
91 unsigned long arg);
92 #endif
93 static int hardware_enable_all(void);
94 static void hardware_disable_all(void);
96 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
98 bool kvm_rebooting;
99 EXPORT_SYMBOL_GPL(kvm_rebooting);
101 static bool largepages_enabled = true;
103 static struct page *hwpoison_page;
104 static pfn_t hwpoison_pfn;
106 struct page *fault_page;
107 pfn_t fault_pfn;
109 inline int kvm_is_mmio_pfn(pfn_t pfn)
111 if (pfn_valid(pfn)) {
112 int reserved;
113 struct page *tail = pfn_to_page(pfn);
114 struct page *head = compound_trans_head(tail);
115 reserved = PageReserved(head);
116 if (head != tail) {
118 * "head" is not a dangling pointer
119 * (compound_trans_head takes care of that)
120 * but the hugepage may have been splitted
121 * from under us (and we may not hold a
122 * reference count on the head page so it can
123 * be reused before we run PageReferenced), so
124 * we've to check PageTail before returning
125 * what we just read.
127 smp_rmb();
128 if (PageTail(tail))
129 return reserved;
131 return PageReserved(tail);
134 return true;
138 * Switches to specified vcpu, until a matching vcpu_put()
140 void vcpu_load(struct kvm_vcpu *vcpu)
142 int cpu;
144 mutex_lock(&vcpu->mutex);
145 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
146 /* The thread running this VCPU changed. */
147 struct pid *oldpid = vcpu->pid;
148 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
149 rcu_assign_pointer(vcpu->pid, newpid);
150 synchronize_rcu();
151 put_pid(oldpid);
153 cpu = get_cpu();
154 preempt_notifier_register(&vcpu->preempt_notifier);
155 kvm_arch_vcpu_load(vcpu, cpu);
156 put_cpu();
159 void vcpu_put(struct kvm_vcpu *vcpu)
161 preempt_disable();
162 kvm_arch_vcpu_put(vcpu);
163 preempt_notifier_unregister(&vcpu->preempt_notifier);
164 preempt_enable();
165 mutex_unlock(&vcpu->mutex);
168 static void ack_flush(void *_completed)
172 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
174 int i, cpu, me;
175 cpumask_var_t cpus;
176 bool called = true;
177 struct kvm_vcpu *vcpu;
179 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
181 me = get_cpu();
182 kvm_for_each_vcpu(i, vcpu, kvm) {
183 kvm_make_request(req, vcpu);
184 cpu = vcpu->cpu;
186 /* Set ->requests bit before we read ->mode */
187 smp_mb();
189 if (cpus != NULL && cpu != -1 && cpu != me &&
190 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
191 cpumask_set_cpu(cpu, cpus);
193 if (unlikely(cpus == NULL))
194 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
195 else if (!cpumask_empty(cpus))
196 smp_call_function_many(cpus, ack_flush, NULL, 1);
197 else
198 called = false;
199 put_cpu();
200 free_cpumask_var(cpus);
201 return called;
204 void kvm_flush_remote_tlbs(struct kvm *kvm)
206 int dirty_count = kvm->tlbs_dirty;
208 smp_mb();
209 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
210 ++kvm->stat.remote_tlb_flush;
211 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
214 void kvm_reload_remote_mmus(struct kvm *kvm)
216 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
219 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
221 struct page *page;
222 int r;
224 mutex_init(&vcpu->mutex);
225 vcpu->cpu = -1;
226 vcpu->kvm = kvm;
227 vcpu->vcpu_id = id;
228 vcpu->pid = NULL;
229 init_waitqueue_head(&vcpu->wq);
230 kvm_async_pf_vcpu_init(vcpu);
232 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
233 if (!page) {
234 r = -ENOMEM;
235 goto fail;
237 vcpu->run = page_address(page);
239 r = kvm_arch_vcpu_init(vcpu);
240 if (r < 0)
241 goto fail_free_run;
242 return 0;
244 fail_free_run:
245 free_page((unsigned long)vcpu->run);
246 fail:
247 return r;
249 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
251 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
253 put_pid(vcpu->pid);
254 kvm_arch_vcpu_uninit(vcpu);
255 free_page((unsigned long)vcpu->run);
257 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
259 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
260 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
262 return container_of(mn, struct kvm, mmu_notifier);
265 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
266 struct mm_struct *mm,
267 unsigned long address)
269 struct kvm *kvm = mmu_notifier_to_kvm(mn);
270 int need_tlb_flush, idx;
273 * When ->invalidate_page runs, the linux pte has been zapped
274 * already but the page is still allocated until
275 * ->invalidate_page returns. So if we increase the sequence
276 * here the kvm page fault will notice if the spte can't be
277 * established because the page is going to be freed. If
278 * instead the kvm page fault establishes the spte before
279 * ->invalidate_page runs, kvm_unmap_hva will release it
280 * before returning.
282 * The sequence increase only need to be seen at spin_unlock
283 * time, and not at spin_lock time.
285 * Increasing the sequence after the spin_unlock would be
286 * unsafe because the kvm page fault could then establish the
287 * pte after kvm_unmap_hva returned, without noticing the page
288 * is going to be freed.
290 idx = srcu_read_lock(&kvm->srcu);
291 spin_lock(&kvm->mmu_lock);
293 kvm->mmu_notifier_seq++;
294 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
295 /* we've to flush the tlb before the pages can be freed */
296 if (need_tlb_flush)
297 kvm_flush_remote_tlbs(kvm);
299 spin_unlock(&kvm->mmu_lock);
300 srcu_read_unlock(&kvm->srcu, idx);
303 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
304 struct mm_struct *mm,
305 unsigned long address,
306 pte_t pte)
308 struct kvm *kvm = mmu_notifier_to_kvm(mn);
309 int idx;
311 idx = srcu_read_lock(&kvm->srcu);
312 spin_lock(&kvm->mmu_lock);
313 kvm->mmu_notifier_seq++;
314 kvm_set_spte_hva(kvm, address, pte);
315 spin_unlock(&kvm->mmu_lock);
316 srcu_read_unlock(&kvm->srcu, idx);
319 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
320 struct mm_struct *mm,
321 unsigned long start,
322 unsigned long end)
324 struct kvm *kvm = mmu_notifier_to_kvm(mn);
325 int need_tlb_flush = 0, idx;
327 idx = srcu_read_lock(&kvm->srcu);
328 spin_lock(&kvm->mmu_lock);
330 * The count increase must become visible at unlock time as no
331 * spte can be established without taking the mmu_lock and
332 * count is also read inside the mmu_lock critical section.
334 kvm->mmu_notifier_count++;
335 for (; start < end; start += PAGE_SIZE)
336 need_tlb_flush |= kvm_unmap_hva(kvm, start);
337 need_tlb_flush |= kvm->tlbs_dirty;
338 /* we've to flush the tlb before the pages can be freed */
339 if (need_tlb_flush)
340 kvm_flush_remote_tlbs(kvm);
342 spin_unlock(&kvm->mmu_lock);
343 srcu_read_unlock(&kvm->srcu, idx);
346 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
347 struct mm_struct *mm,
348 unsigned long start,
349 unsigned long end)
351 struct kvm *kvm = mmu_notifier_to_kvm(mn);
353 spin_lock(&kvm->mmu_lock);
355 * This sequence increase will notify the kvm page fault that
356 * the page that is going to be mapped in the spte could have
357 * been freed.
359 kvm->mmu_notifier_seq++;
361 * The above sequence increase must be visible before the
362 * below count decrease but both values are read by the kvm
363 * page fault under mmu_lock spinlock so we don't need to add
364 * a smb_wmb() here in between the two.
366 kvm->mmu_notifier_count--;
367 spin_unlock(&kvm->mmu_lock);
369 BUG_ON(kvm->mmu_notifier_count < 0);
372 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
373 struct mm_struct *mm,
374 unsigned long address)
376 struct kvm *kvm = mmu_notifier_to_kvm(mn);
377 int young, idx;
379 idx = srcu_read_lock(&kvm->srcu);
380 spin_lock(&kvm->mmu_lock);
382 young = kvm_age_hva(kvm, address);
383 if (young)
384 kvm_flush_remote_tlbs(kvm);
386 spin_unlock(&kvm->mmu_lock);
387 srcu_read_unlock(&kvm->srcu, idx);
389 return young;
392 static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
393 struct mm_struct *mm,
394 unsigned long address)
396 struct kvm *kvm = mmu_notifier_to_kvm(mn);
397 int young, idx;
399 idx = srcu_read_lock(&kvm->srcu);
400 spin_lock(&kvm->mmu_lock);
401 young = kvm_test_age_hva(kvm, address);
402 spin_unlock(&kvm->mmu_lock);
403 srcu_read_unlock(&kvm->srcu, idx);
405 return young;
408 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
409 struct mm_struct *mm)
411 struct kvm *kvm = mmu_notifier_to_kvm(mn);
412 int idx;
414 idx = srcu_read_lock(&kvm->srcu);
415 kvm_arch_flush_shadow(kvm);
416 srcu_read_unlock(&kvm->srcu, idx);
419 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
420 .invalidate_page = kvm_mmu_notifier_invalidate_page,
421 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
422 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
423 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
424 .test_young = kvm_mmu_notifier_test_young,
425 .change_pte = kvm_mmu_notifier_change_pte,
426 .release = kvm_mmu_notifier_release,
429 static int kvm_init_mmu_notifier(struct kvm *kvm)
431 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
432 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
435 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
437 static int kvm_init_mmu_notifier(struct kvm *kvm)
439 return 0;
442 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
444 static void kvm_init_memslots_id(struct kvm *kvm)
446 int i;
447 struct kvm_memslots *slots = kvm->memslots;
449 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
450 slots->id_to_index[i] = slots->memslots[i].id = i;
453 static struct kvm *kvm_create_vm(void)
455 int r, i;
456 struct kvm *kvm = kvm_arch_alloc_vm();
458 if (!kvm)
459 return ERR_PTR(-ENOMEM);
461 r = kvm_arch_init_vm(kvm);
462 if (r)
463 goto out_err_nodisable;
465 r = hardware_enable_all();
466 if (r)
467 goto out_err_nodisable;
469 #ifdef CONFIG_HAVE_KVM_IRQCHIP
470 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
471 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
472 #endif
474 r = -ENOMEM;
475 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
476 if (!kvm->memslots)
477 goto out_err_nosrcu;
478 kvm_init_memslots_id(kvm);
479 if (init_srcu_struct(&kvm->srcu))
480 goto out_err_nosrcu;
481 for (i = 0; i < KVM_NR_BUSES; i++) {
482 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
483 GFP_KERNEL);
484 if (!kvm->buses[i])
485 goto out_err;
488 spin_lock_init(&kvm->mmu_lock);
489 kvm->mm = current->mm;
490 atomic_inc(&kvm->mm->mm_count);
491 kvm_eventfd_init(kvm);
492 mutex_init(&kvm->lock);
493 mutex_init(&kvm->irq_lock);
494 mutex_init(&kvm->slots_lock);
495 atomic_set(&kvm->users_count, 1);
497 r = kvm_init_mmu_notifier(kvm);
498 if (r)
499 goto out_err;
501 raw_spin_lock(&kvm_lock);
502 list_add(&kvm->vm_list, &vm_list);
503 raw_spin_unlock(&kvm_lock);
505 return kvm;
507 out_err:
508 cleanup_srcu_struct(&kvm->srcu);
509 out_err_nosrcu:
510 hardware_disable_all();
511 out_err_nodisable:
512 for (i = 0; i < KVM_NR_BUSES; i++)
513 kfree(kvm->buses[i]);
514 kfree(kvm->memslots);
515 kvm_arch_free_vm(kvm);
516 return ERR_PTR(r);
519 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
521 if (!memslot->dirty_bitmap)
522 return;
524 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
525 vfree(memslot->dirty_bitmap_head);
526 else
527 kfree(memslot->dirty_bitmap_head);
529 memslot->dirty_bitmap = NULL;
530 memslot->dirty_bitmap_head = NULL;
534 * Free any memory in @free but not in @dont.
536 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
537 struct kvm_memory_slot *dont)
539 int i;
541 if (!dont || free->rmap != dont->rmap)
542 vfree(free->rmap);
544 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
545 kvm_destroy_dirty_bitmap(free);
548 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
549 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
550 vfree(free->lpage_info[i]);
551 free->lpage_info[i] = NULL;
555 free->npages = 0;
556 free->rmap = NULL;
559 void kvm_free_physmem(struct kvm *kvm)
561 struct kvm_memslots *slots = kvm->memslots;
562 struct kvm_memory_slot *memslot;
564 kvm_for_each_memslot(memslot, slots)
565 kvm_free_physmem_slot(memslot, NULL);
567 kfree(kvm->memslots);
570 static void kvm_destroy_vm(struct kvm *kvm)
572 int i;
573 struct mm_struct *mm = kvm->mm;
575 kvm_arch_sync_events(kvm);
576 raw_spin_lock(&kvm_lock);
577 list_del(&kvm->vm_list);
578 raw_spin_unlock(&kvm_lock);
579 kvm_free_irq_routing(kvm);
580 for (i = 0; i < KVM_NR_BUSES; i++)
581 kvm_io_bus_destroy(kvm->buses[i]);
582 kvm_coalesced_mmio_free(kvm);
583 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
584 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
585 #else
586 kvm_arch_flush_shadow(kvm);
587 #endif
588 kvm_arch_destroy_vm(kvm);
589 kvm_free_physmem(kvm);
590 cleanup_srcu_struct(&kvm->srcu);
591 kvm_arch_free_vm(kvm);
592 hardware_disable_all();
593 mmdrop(mm);
596 void kvm_get_kvm(struct kvm *kvm)
598 atomic_inc(&kvm->users_count);
600 EXPORT_SYMBOL_GPL(kvm_get_kvm);
602 void kvm_put_kvm(struct kvm *kvm)
604 if (atomic_dec_and_test(&kvm->users_count))
605 kvm_destroy_vm(kvm);
607 EXPORT_SYMBOL_GPL(kvm_put_kvm);
610 static int kvm_vm_release(struct inode *inode, struct file *filp)
612 struct kvm *kvm = filp->private_data;
614 kvm_irqfd_release(kvm);
616 kvm_put_kvm(kvm);
617 return 0;
620 #ifndef CONFIG_S390
622 * Allocation size is twice as large as the actual dirty bitmap size.
623 * This makes it possible to do double buffering: see x86's
624 * kvm_vm_ioctl_get_dirty_log().
626 static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
628 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
630 if (dirty_bytes > PAGE_SIZE)
631 memslot->dirty_bitmap = vzalloc(dirty_bytes);
632 else
633 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
635 if (!memslot->dirty_bitmap)
636 return -ENOMEM;
638 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
639 memslot->nr_dirty_pages = 0;
640 return 0;
642 #endif /* !CONFIG_S390 */
644 static struct kvm_memory_slot *
645 search_memslots(struct kvm_memslots *slots, gfn_t gfn)
647 struct kvm_memory_slot *memslot;
649 kvm_for_each_memslot(memslot, slots)
650 if (gfn >= memslot->base_gfn &&
651 gfn < memslot->base_gfn + memslot->npages)
652 return memslot;
654 return NULL;
657 static int cmp_memslot(const void *slot1, const void *slot2)
659 struct kvm_memory_slot *s1, *s2;
661 s1 = (struct kvm_memory_slot *)slot1;
662 s2 = (struct kvm_memory_slot *)slot2;
664 if (s1->npages < s2->npages)
665 return 1;
666 if (s1->npages > s2->npages)
667 return -1;
669 return 0;
673 * Sort the memslots base on its size, so the larger slots
674 * will get better fit.
676 static void sort_memslots(struct kvm_memslots *slots)
678 int i;
680 sort(slots->memslots, KVM_MEM_SLOTS_NUM,
681 sizeof(struct kvm_memory_slot), cmp_memslot, NULL);
683 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
684 slots->id_to_index[slots->memslots[i].id] = i;
687 void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new)
689 if (new) {
690 int id = new->id;
691 struct kvm_memory_slot *old = id_to_memslot(slots, id);
692 unsigned long npages = old->npages;
694 *old = *new;
695 if (new->npages != npages)
696 sort_memslots(slots);
699 slots->generation++;
703 * Allocate some memory and give it an address in the guest physical address
704 * space.
706 * Discontiguous memory is allowed, mostly for framebuffers.
708 * Must be called holding mmap_sem for write.
710 int __kvm_set_memory_region(struct kvm *kvm,
711 struct kvm_userspace_memory_region *mem,
712 int user_alloc)
714 int r;
715 gfn_t base_gfn;
716 unsigned long npages;
717 unsigned long i;
718 struct kvm_memory_slot *memslot;
719 struct kvm_memory_slot old, new;
720 struct kvm_memslots *slots, *old_memslots;
722 r = -EINVAL;
723 /* General sanity checks */
724 if (mem->memory_size & (PAGE_SIZE - 1))
725 goto out;
726 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
727 goto out;
728 /* We can read the guest memory with __xxx_user() later on. */
729 if (user_alloc &&
730 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
731 !access_ok(VERIFY_WRITE,
732 (void __user *)(unsigned long)mem->userspace_addr,
733 mem->memory_size)))
734 goto out;
735 if (mem->slot >= KVM_MEM_SLOTS_NUM)
736 goto out;
737 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
738 goto out;
740 memslot = id_to_memslot(kvm->memslots, mem->slot);
741 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
742 npages = mem->memory_size >> PAGE_SHIFT;
744 r = -EINVAL;
745 if (npages > KVM_MEM_MAX_NR_PAGES)
746 goto out;
748 if (!npages)
749 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
751 new = old = *memslot;
753 new.id = mem->slot;
754 new.base_gfn = base_gfn;
755 new.npages = npages;
756 new.flags = mem->flags;
758 /* Disallow changing a memory slot's size. */
759 r = -EINVAL;
760 if (npages && old.npages && npages != old.npages)
761 goto out_free;
763 /* Check for overlaps */
764 r = -EEXIST;
765 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
766 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
768 if (s == memslot || !s->npages)
769 continue;
770 if (!((base_gfn + npages <= s->base_gfn) ||
771 (base_gfn >= s->base_gfn + s->npages)))
772 goto out_free;
775 /* Free page dirty bitmap if unneeded */
776 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
777 new.dirty_bitmap = NULL;
779 r = -ENOMEM;
781 /* Allocate if a slot is being created */
782 #ifndef CONFIG_S390
783 if (npages && !new.rmap) {
784 new.rmap = vzalloc(npages * sizeof(*new.rmap));
786 if (!new.rmap)
787 goto out_free;
789 new.user_alloc = user_alloc;
790 new.userspace_addr = mem->userspace_addr;
792 if (!npages)
793 goto skip_lpage;
795 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
796 unsigned long ugfn;
797 unsigned long j;
798 int lpages;
799 int level = i + 2;
801 /* Avoid unused variable warning if no large pages */
802 (void)level;
804 if (new.lpage_info[i])
805 continue;
807 lpages = 1 + ((base_gfn + npages - 1)
808 >> KVM_HPAGE_GFN_SHIFT(level));
809 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
811 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
813 if (!new.lpage_info[i])
814 goto out_free;
816 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
817 new.lpage_info[i][0].write_count = 1;
818 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
819 new.lpage_info[i][lpages - 1].write_count = 1;
820 ugfn = new.userspace_addr >> PAGE_SHIFT;
822 * If the gfn and userspace address are not aligned wrt each
823 * other, or if explicitly asked to, disable large page
824 * support for this slot
826 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
827 !largepages_enabled)
828 for (j = 0; j < lpages; ++j)
829 new.lpage_info[i][j].write_count = 1;
832 skip_lpage:
834 /* Allocate page dirty bitmap if needed */
835 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
836 if (kvm_create_dirty_bitmap(&new) < 0)
837 goto out_free;
838 /* destroy any largepage mappings for dirty tracking */
840 #else /* not defined CONFIG_S390 */
841 new.user_alloc = user_alloc;
842 if (user_alloc)
843 new.userspace_addr = mem->userspace_addr;
844 #endif /* not defined CONFIG_S390 */
846 if (!npages) {
847 struct kvm_memory_slot *slot;
849 r = -ENOMEM;
850 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
851 GFP_KERNEL);
852 if (!slots)
853 goto out_free;
854 slot = id_to_memslot(slots, mem->slot);
855 slot->flags |= KVM_MEMSLOT_INVALID;
857 update_memslots(slots, NULL);
859 old_memslots = kvm->memslots;
860 rcu_assign_pointer(kvm->memslots, slots);
861 synchronize_srcu_expedited(&kvm->srcu);
862 /* From this point no new shadow pages pointing to a deleted
863 * memslot will be created.
865 * validation of sp->gfn happens in:
866 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
867 * - kvm_is_visible_gfn (mmu_check_roots)
869 kvm_arch_flush_shadow(kvm);
870 kfree(old_memslots);
873 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
874 if (r)
875 goto out_free;
877 /* map/unmap the pages in iommu page table */
878 if (npages) {
879 r = kvm_iommu_map_pages(kvm, &new);
880 if (r)
881 goto out_free;
882 } else
883 kvm_iommu_unmap_pages(kvm, &old);
885 r = -ENOMEM;
886 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
887 GFP_KERNEL);
888 if (!slots)
889 goto out_free;
891 /* actual memory is freed via old in kvm_free_physmem_slot below */
892 if (!npages) {
893 new.rmap = NULL;
894 new.dirty_bitmap = NULL;
895 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
896 new.lpage_info[i] = NULL;
899 update_memslots(slots, &new);
900 old_memslots = kvm->memslots;
901 rcu_assign_pointer(kvm->memslots, slots);
902 synchronize_srcu_expedited(&kvm->srcu);
904 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
907 * If the new memory slot is created, we need to clear all
908 * mmio sptes.
910 if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
911 kvm_arch_flush_shadow(kvm);
913 kvm_free_physmem_slot(&old, &new);
914 kfree(old_memslots);
916 return 0;
918 out_free:
919 kvm_free_physmem_slot(&new, &old);
920 out:
921 return r;
924 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
926 int kvm_set_memory_region(struct kvm *kvm,
927 struct kvm_userspace_memory_region *mem,
928 int user_alloc)
930 int r;
932 mutex_lock(&kvm->slots_lock);
933 r = __kvm_set_memory_region(kvm, mem, user_alloc);
934 mutex_unlock(&kvm->slots_lock);
935 return r;
937 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
939 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
940 struct
941 kvm_userspace_memory_region *mem,
942 int user_alloc)
944 if (mem->slot >= KVM_MEMORY_SLOTS)
945 return -EINVAL;
946 return kvm_set_memory_region(kvm, mem, user_alloc);
949 int kvm_get_dirty_log(struct kvm *kvm,
950 struct kvm_dirty_log *log, int *is_dirty)
952 struct kvm_memory_slot *memslot;
953 int r, i;
954 unsigned long n;
955 unsigned long any = 0;
957 r = -EINVAL;
958 if (log->slot >= KVM_MEMORY_SLOTS)
959 goto out;
961 memslot = id_to_memslot(kvm->memslots, log->slot);
962 r = -ENOENT;
963 if (!memslot->dirty_bitmap)
964 goto out;
966 n = kvm_dirty_bitmap_bytes(memslot);
968 for (i = 0; !any && i < n/sizeof(long); ++i)
969 any = memslot->dirty_bitmap[i];
971 r = -EFAULT;
972 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
973 goto out;
975 if (any)
976 *is_dirty = 1;
978 r = 0;
979 out:
980 return r;
983 void kvm_disable_largepages(void)
985 largepages_enabled = false;
987 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
989 int is_error_page(struct page *page)
991 return page == bad_page || page == hwpoison_page || page == fault_page;
993 EXPORT_SYMBOL_GPL(is_error_page);
995 int is_error_pfn(pfn_t pfn)
997 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
999 EXPORT_SYMBOL_GPL(is_error_pfn);
1001 int is_hwpoison_pfn(pfn_t pfn)
1003 return pfn == hwpoison_pfn;
1005 EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
1007 int is_fault_pfn(pfn_t pfn)
1009 return pfn == fault_pfn;
1011 EXPORT_SYMBOL_GPL(is_fault_pfn);
1013 int is_noslot_pfn(pfn_t pfn)
1015 return pfn == bad_pfn;
1017 EXPORT_SYMBOL_GPL(is_noslot_pfn);
1019 int is_invalid_pfn(pfn_t pfn)
1021 return pfn == hwpoison_pfn || pfn == fault_pfn;
1023 EXPORT_SYMBOL_GPL(is_invalid_pfn);
1025 static inline unsigned long bad_hva(void)
1027 return PAGE_OFFSET;
1030 int kvm_is_error_hva(unsigned long addr)
1032 return addr == bad_hva();
1034 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1036 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
1037 gfn_t gfn)
1039 return search_memslots(slots, gfn);
1042 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1044 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
1046 EXPORT_SYMBOL_GPL(gfn_to_memslot);
1048 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1050 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
1052 if (!memslot || memslot->id >= KVM_MEMORY_SLOTS ||
1053 memslot->flags & KVM_MEMSLOT_INVALID)
1054 return 0;
1056 return 1;
1058 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1060 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
1062 struct vm_area_struct *vma;
1063 unsigned long addr, size;
1065 size = PAGE_SIZE;
1067 addr = gfn_to_hva(kvm, gfn);
1068 if (kvm_is_error_hva(addr))
1069 return PAGE_SIZE;
1071 down_read(&current->mm->mmap_sem);
1072 vma = find_vma(current->mm, addr);
1073 if (!vma)
1074 goto out;
1076 size = vma_kernel_pagesize(vma);
1078 out:
1079 up_read(&current->mm->mmap_sem);
1081 return size;
1084 static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1085 gfn_t *nr_pages)
1087 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
1088 return bad_hva();
1090 if (nr_pages)
1091 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1093 return gfn_to_hva_memslot(slot, gfn);
1096 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1098 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
1100 EXPORT_SYMBOL_GPL(gfn_to_hva);
1102 static pfn_t get_fault_pfn(void)
1104 get_page(fault_page);
1105 return fault_pfn;
1108 int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1109 unsigned long start, int write, struct page **page)
1111 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1113 if (write)
1114 flags |= FOLL_WRITE;
1116 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1119 static inline int check_user_page_hwpoison(unsigned long addr)
1121 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1123 rc = __get_user_pages(current, current->mm, addr, 1,
1124 flags, NULL, NULL, NULL);
1125 return rc == -EHWPOISON;
1128 static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
1129 bool *async, bool write_fault, bool *writable)
1131 struct page *page[1];
1132 int npages = 0;
1133 pfn_t pfn;
1135 /* we can do it either atomically or asynchronously, not both */
1136 BUG_ON(atomic && async);
1138 BUG_ON(!write_fault && !writable);
1140 if (writable)
1141 *writable = true;
1143 if (atomic || async)
1144 npages = __get_user_pages_fast(addr, 1, 1, page);
1146 if (unlikely(npages != 1) && !atomic) {
1147 might_sleep();
1149 if (writable)
1150 *writable = write_fault;
1152 if (async) {
1153 down_read(&current->mm->mmap_sem);
1154 npages = get_user_page_nowait(current, current->mm,
1155 addr, write_fault, page);
1156 up_read(&current->mm->mmap_sem);
1157 } else
1158 npages = get_user_pages_fast(addr, 1, write_fault,
1159 page);
1161 /* map read fault as writable if possible */
1162 if (unlikely(!write_fault) && npages == 1) {
1163 struct page *wpage[1];
1165 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1166 if (npages == 1) {
1167 *writable = true;
1168 put_page(page[0]);
1169 page[0] = wpage[0];
1171 npages = 1;
1175 if (unlikely(npages != 1)) {
1176 struct vm_area_struct *vma;
1178 if (atomic)
1179 return get_fault_pfn();
1181 down_read(&current->mm->mmap_sem);
1182 if (npages == -EHWPOISON ||
1183 (!async && check_user_page_hwpoison(addr))) {
1184 up_read(&current->mm->mmap_sem);
1185 get_page(hwpoison_page);
1186 return page_to_pfn(hwpoison_page);
1189 vma = find_vma_intersection(current->mm, addr, addr+1);
1191 if (vma == NULL)
1192 pfn = get_fault_pfn();
1193 else if ((vma->vm_flags & VM_PFNMAP)) {
1194 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1195 vma->vm_pgoff;
1196 BUG_ON(!kvm_is_mmio_pfn(pfn));
1197 } else {
1198 if (async && (vma->vm_flags & VM_WRITE))
1199 *async = true;
1200 pfn = get_fault_pfn();
1202 up_read(&current->mm->mmap_sem);
1203 } else
1204 pfn = page_to_pfn(page[0]);
1206 return pfn;
1209 pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1211 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
1213 EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1215 static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1216 bool write_fault, bool *writable)
1218 unsigned long addr;
1220 if (async)
1221 *async = false;
1223 addr = gfn_to_hva(kvm, gfn);
1224 if (kvm_is_error_hva(addr)) {
1225 get_page(bad_page);
1226 return page_to_pfn(bad_page);
1229 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
1232 pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1234 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
1236 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1238 pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1239 bool write_fault, bool *writable)
1241 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
1243 EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1245 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1247 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
1249 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1251 pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1252 bool *writable)
1254 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1256 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1258 pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1259 struct kvm_memory_slot *slot, gfn_t gfn)
1261 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
1262 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
1265 int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1266 int nr_pages)
1268 unsigned long addr;
1269 gfn_t entry;
1271 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
1272 if (kvm_is_error_hva(addr))
1273 return -1;
1275 if (entry < nr_pages)
1276 return 0;
1278 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1280 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1282 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1284 pfn_t pfn;
1286 pfn = gfn_to_pfn(kvm, gfn);
1287 if (!kvm_is_mmio_pfn(pfn))
1288 return pfn_to_page(pfn);
1290 WARN_ON(kvm_is_mmio_pfn(pfn));
1292 get_page(bad_page);
1293 return bad_page;
1296 EXPORT_SYMBOL_GPL(gfn_to_page);
1298 void kvm_release_page_clean(struct page *page)
1300 kvm_release_pfn_clean(page_to_pfn(page));
1302 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1304 void kvm_release_pfn_clean(pfn_t pfn)
1306 if (!kvm_is_mmio_pfn(pfn))
1307 put_page(pfn_to_page(pfn));
1309 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1311 void kvm_release_page_dirty(struct page *page)
1313 kvm_release_pfn_dirty(page_to_pfn(page));
1315 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1317 void kvm_release_pfn_dirty(pfn_t pfn)
1319 kvm_set_pfn_dirty(pfn);
1320 kvm_release_pfn_clean(pfn);
1322 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1324 void kvm_set_page_dirty(struct page *page)
1326 kvm_set_pfn_dirty(page_to_pfn(page));
1328 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1330 void kvm_set_pfn_dirty(pfn_t pfn)
1332 if (!kvm_is_mmio_pfn(pfn)) {
1333 struct page *page = pfn_to_page(pfn);
1334 if (!PageReserved(page))
1335 SetPageDirty(page);
1338 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1340 void kvm_set_pfn_accessed(pfn_t pfn)
1342 if (!kvm_is_mmio_pfn(pfn))
1343 mark_page_accessed(pfn_to_page(pfn));
1345 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1347 void kvm_get_pfn(pfn_t pfn)
1349 if (!kvm_is_mmio_pfn(pfn))
1350 get_page(pfn_to_page(pfn));
1352 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1354 static int next_segment(unsigned long len, int offset)
1356 if (len > PAGE_SIZE - offset)
1357 return PAGE_SIZE - offset;
1358 else
1359 return len;
1362 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1363 int len)
1365 int r;
1366 unsigned long addr;
1368 addr = gfn_to_hva(kvm, gfn);
1369 if (kvm_is_error_hva(addr))
1370 return -EFAULT;
1371 r = __copy_from_user(data, (void __user *)addr + offset, len);
1372 if (r)
1373 return -EFAULT;
1374 return 0;
1376 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1378 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1380 gfn_t gfn = gpa >> PAGE_SHIFT;
1381 int seg;
1382 int offset = offset_in_page(gpa);
1383 int ret;
1385 while ((seg = next_segment(len, offset)) != 0) {
1386 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1387 if (ret < 0)
1388 return ret;
1389 offset = 0;
1390 len -= seg;
1391 data += seg;
1392 ++gfn;
1394 return 0;
1396 EXPORT_SYMBOL_GPL(kvm_read_guest);
1398 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1399 unsigned long len)
1401 int r;
1402 unsigned long addr;
1403 gfn_t gfn = gpa >> PAGE_SHIFT;
1404 int offset = offset_in_page(gpa);
1406 addr = gfn_to_hva(kvm, gfn);
1407 if (kvm_is_error_hva(addr))
1408 return -EFAULT;
1409 pagefault_disable();
1410 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1411 pagefault_enable();
1412 if (r)
1413 return -EFAULT;
1414 return 0;
1416 EXPORT_SYMBOL(kvm_read_guest_atomic);
1418 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1419 int offset, int len)
1421 int r;
1422 unsigned long addr;
1424 addr = gfn_to_hva(kvm, gfn);
1425 if (kvm_is_error_hva(addr))
1426 return -EFAULT;
1427 r = __copy_to_user((void __user *)addr + offset, data, len);
1428 if (r)
1429 return -EFAULT;
1430 mark_page_dirty(kvm, gfn);
1431 return 0;
1433 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1435 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1436 unsigned long len)
1438 gfn_t gfn = gpa >> PAGE_SHIFT;
1439 int seg;
1440 int offset = offset_in_page(gpa);
1441 int ret;
1443 while ((seg = next_segment(len, offset)) != 0) {
1444 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1445 if (ret < 0)
1446 return ret;
1447 offset = 0;
1448 len -= seg;
1449 data += seg;
1450 ++gfn;
1452 return 0;
1455 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1456 gpa_t gpa)
1458 struct kvm_memslots *slots = kvm_memslots(kvm);
1459 int offset = offset_in_page(gpa);
1460 gfn_t gfn = gpa >> PAGE_SHIFT;
1462 ghc->gpa = gpa;
1463 ghc->generation = slots->generation;
1464 ghc->memslot = __gfn_to_memslot(slots, gfn);
1465 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1466 if (!kvm_is_error_hva(ghc->hva))
1467 ghc->hva += offset;
1468 else
1469 return -EFAULT;
1471 return 0;
1473 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1475 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1476 void *data, unsigned long len)
1478 struct kvm_memslots *slots = kvm_memslots(kvm);
1479 int r;
1481 if (slots->generation != ghc->generation)
1482 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1484 if (kvm_is_error_hva(ghc->hva))
1485 return -EFAULT;
1487 r = __copy_to_user((void __user *)ghc->hva, data, len);
1488 if (r)
1489 return -EFAULT;
1490 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1492 return 0;
1494 EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1496 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1497 void *data, unsigned long len)
1499 struct kvm_memslots *slots = kvm_memslots(kvm);
1500 int r;
1502 if (slots->generation != ghc->generation)
1503 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1505 if (kvm_is_error_hva(ghc->hva))
1506 return -EFAULT;
1508 r = __copy_from_user(data, (void __user *)ghc->hva, len);
1509 if (r)
1510 return -EFAULT;
1512 return 0;
1514 EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
1516 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1518 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1519 offset, len);
1521 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1523 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1525 gfn_t gfn = gpa >> PAGE_SHIFT;
1526 int seg;
1527 int offset = offset_in_page(gpa);
1528 int ret;
1530 while ((seg = next_segment(len, offset)) != 0) {
1531 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1532 if (ret < 0)
1533 return ret;
1534 offset = 0;
1535 len -= seg;
1536 ++gfn;
1538 return 0;
1540 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1542 void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1543 gfn_t gfn)
1545 if (memslot && memslot->dirty_bitmap) {
1546 unsigned long rel_gfn = gfn - memslot->base_gfn;
1548 if (!test_and_set_bit_le(rel_gfn, memslot->dirty_bitmap))
1549 memslot->nr_dirty_pages++;
1553 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1555 struct kvm_memory_slot *memslot;
1557 memslot = gfn_to_memslot(kvm, gfn);
1558 mark_page_dirty_in_slot(kvm, memslot, gfn);
1562 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1564 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1566 DEFINE_WAIT(wait);
1568 for (;;) {
1569 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1571 if (kvm_arch_vcpu_runnable(vcpu)) {
1572 kvm_make_request(KVM_REQ_UNHALT, vcpu);
1573 break;
1575 if (kvm_cpu_has_pending_timer(vcpu))
1576 break;
1577 if (signal_pending(current))
1578 break;
1580 schedule();
1583 finish_wait(&vcpu->wq, &wait);
1586 void kvm_resched(struct kvm_vcpu *vcpu)
1588 if (!need_resched())
1589 return;
1590 cond_resched();
1592 EXPORT_SYMBOL_GPL(kvm_resched);
1594 void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1596 struct kvm *kvm = me->kvm;
1597 struct kvm_vcpu *vcpu;
1598 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1599 int yielded = 0;
1600 int pass;
1601 int i;
1604 * We boost the priority of a VCPU that is runnable but not
1605 * currently running, because it got preempted by something
1606 * else and called schedule in __vcpu_run. Hopefully that
1607 * VCPU is holding the lock that we need and will release it.
1608 * We approximate round-robin by starting at the last boosted VCPU.
1610 for (pass = 0; pass < 2 && !yielded; pass++) {
1611 kvm_for_each_vcpu(i, vcpu, kvm) {
1612 struct task_struct *task = NULL;
1613 struct pid *pid;
1614 if (!pass && i < last_boosted_vcpu) {
1615 i = last_boosted_vcpu;
1616 continue;
1617 } else if (pass && i > last_boosted_vcpu)
1618 break;
1619 if (vcpu == me)
1620 continue;
1621 if (waitqueue_active(&vcpu->wq))
1622 continue;
1623 rcu_read_lock();
1624 pid = rcu_dereference(vcpu->pid);
1625 if (pid)
1626 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1627 rcu_read_unlock();
1628 if (!task)
1629 continue;
1630 if (task->flags & PF_VCPU) {
1631 put_task_struct(task);
1632 continue;
1634 if (yield_to(task, 1)) {
1635 put_task_struct(task);
1636 kvm->last_boosted_vcpu = i;
1637 yielded = 1;
1638 break;
1640 put_task_struct(task);
1644 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1646 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1648 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1649 struct page *page;
1651 if (vmf->pgoff == 0)
1652 page = virt_to_page(vcpu->run);
1653 #ifdef CONFIG_X86
1654 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1655 page = virt_to_page(vcpu->arch.pio_data);
1656 #endif
1657 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1658 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1659 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1660 #endif
1661 else
1662 return VM_FAULT_SIGBUS;
1663 get_page(page);
1664 vmf->page = page;
1665 return 0;
1668 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1669 .fault = kvm_vcpu_fault,
1672 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1674 vma->vm_ops = &kvm_vcpu_vm_ops;
1675 return 0;
1678 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1680 struct kvm_vcpu *vcpu = filp->private_data;
1682 kvm_put_kvm(vcpu->kvm);
1683 return 0;
1686 static struct file_operations kvm_vcpu_fops = {
1687 .release = kvm_vcpu_release,
1688 .unlocked_ioctl = kvm_vcpu_ioctl,
1689 #ifdef CONFIG_COMPAT
1690 .compat_ioctl = kvm_vcpu_compat_ioctl,
1691 #endif
1692 .mmap = kvm_vcpu_mmap,
1693 .llseek = noop_llseek,
1697 * Allocates an inode for the vcpu.
1699 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1701 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
1705 * Creates some virtual cpus. Good luck creating more than one.
1707 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1709 int r;
1710 struct kvm_vcpu *vcpu, *v;
1712 vcpu = kvm_arch_vcpu_create(kvm, id);
1713 if (IS_ERR(vcpu))
1714 return PTR_ERR(vcpu);
1716 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1718 r = kvm_arch_vcpu_setup(vcpu);
1719 if (r)
1720 goto vcpu_destroy;
1722 mutex_lock(&kvm->lock);
1723 if (!kvm_vcpu_compatible(vcpu)) {
1724 r = -EINVAL;
1725 goto unlock_vcpu_destroy;
1727 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1728 r = -EINVAL;
1729 goto unlock_vcpu_destroy;
1732 kvm_for_each_vcpu(r, v, kvm)
1733 if (v->vcpu_id == id) {
1734 r = -EEXIST;
1735 goto unlock_vcpu_destroy;
1738 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1740 /* Now it's all set up, let userspace reach it */
1741 kvm_get_kvm(kvm);
1742 r = create_vcpu_fd(vcpu);
1743 if (r < 0) {
1744 kvm_put_kvm(kvm);
1745 goto unlock_vcpu_destroy;
1748 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1749 smp_wmb();
1750 atomic_inc(&kvm->online_vcpus);
1752 mutex_unlock(&kvm->lock);
1753 return r;
1755 unlock_vcpu_destroy:
1756 mutex_unlock(&kvm->lock);
1757 vcpu_destroy:
1758 kvm_arch_vcpu_destroy(vcpu);
1759 return r;
1762 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1764 if (sigset) {
1765 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1766 vcpu->sigset_active = 1;
1767 vcpu->sigset = *sigset;
1768 } else
1769 vcpu->sigset_active = 0;
1770 return 0;
1773 static long kvm_vcpu_ioctl(struct file *filp,
1774 unsigned int ioctl, unsigned long arg)
1776 struct kvm_vcpu *vcpu = filp->private_data;
1777 void __user *argp = (void __user *)arg;
1778 int r;
1779 struct kvm_fpu *fpu = NULL;
1780 struct kvm_sregs *kvm_sregs = NULL;
1782 if (vcpu->kvm->mm != current->mm)
1783 return -EIO;
1785 #if defined(CONFIG_S390) || defined(CONFIG_PPC)
1787 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1788 * so vcpu_load() would break it.
1790 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1791 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1792 #endif
1795 vcpu_load(vcpu);
1796 switch (ioctl) {
1797 case KVM_RUN:
1798 r = -EINVAL;
1799 if (arg)
1800 goto out;
1801 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1802 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
1803 break;
1804 case KVM_GET_REGS: {
1805 struct kvm_regs *kvm_regs;
1807 r = -ENOMEM;
1808 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1809 if (!kvm_regs)
1810 goto out;
1811 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1812 if (r)
1813 goto out_free1;
1814 r = -EFAULT;
1815 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1816 goto out_free1;
1817 r = 0;
1818 out_free1:
1819 kfree(kvm_regs);
1820 break;
1822 case KVM_SET_REGS: {
1823 struct kvm_regs *kvm_regs;
1825 r = -ENOMEM;
1826 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
1827 if (IS_ERR(kvm_regs)) {
1828 r = PTR_ERR(kvm_regs);
1829 goto out;
1831 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1832 if (r)
1833 goto out_free2;
1834 r = 0;
1835 out_free2:
1836 kfree(kvm_regs);
1837 break;
1839 case KVM_GET_SREGS: {
1840 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1841 r = -ENOMEM;
1842 if (!kvm_sregs)
1843 goto out;
1844 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1845 if (r)
1846 goto out;
1847 r = -EFAULT;
1848 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1849 goto out;
1850 r = 0;
1851 break;
1853 case KVM_SET_SREGS: {
1854 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
1855 if (IS_ERR(kvm_sregs)) {
1856 r = PTR_ERR(kvm_sregs);
1857 goto out;
1859 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1860 if (r)
1861 goto out;
1862 r = 0;
1863 break;
1865 case KVM_GET_MP_STATE: {
1866 struct kvm_mp_state mp_state;
1868 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1869 if (r)
1870 goto out;
1871 r = -EFAULT;
1872 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1873 goto out;
1874 r = 0;
1875 break;
1877 case KVM_SET_MP_STATE: {
1878 struct kvm_mp_state mp_state;
1880 r = -EFAULT;
1881 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1882 goto out;
1883 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1884 if (r)
1885 goto out;
1886 r = 0;
1887 break;
1889 case KVM_TRANSLATE: {
1890 struct kvm_translation tr;
1892 r = -EFAULT;
1893 if (copy_from_user(&tr, argp, sizeof tr))
1894 goto out;
1895 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1896 if (r)
1897 goto out;
1898 r = -EFAULT;
1899 if (copy_to_user(argp, &tr, sizeof tr))
1900 goto out;
1901 r = 0;
1902 break;
1904 case KVM_SET_GUEST_DEBUG: {
1905 struct kvm_guest_debug dbg;
1907 r = -EFAULT;
1908 if (copy_from_user(&dbg, argp, sizeof dbg))
1909 goto out;
1910 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1911 if (r)
1912 goto out;
1913 r = 0;
1914 break;
1916 case KVM_SET_SIGNAL_MASK: {
1917 struct kvm_signal_mask __user *sigmask_arg = argp;
1918 struct kvm_signal_mask kvm_sigmask;
1919 sigset_t sigset, *p;
1921 p = NULL;
1922 if (argp) {
1923 r = -EFAULT;
1924 if (copy_from_user(&kvm_sigmask, argp,
1925 sizeof kvm_sigmask))
1926 goto out;
1927 r = -EINVAL;
1928 if (kvm_sigmask.len != sizeof sigset)
1929 goto out;
1930 r = -EFAULT;
1931 if (copy_from_user(&sigset, sigmask_arg->sigset,
1932 sizeof sigset))
1933 goto out;
1934 p = &sigset;
1936 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
1937 break;
1939 case KVM_GET_FPU: {
1940 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1941 r = -ENOMEM;
1942 if (!fpu)
1943 goto out;
1944 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1945 if (r)
1946 goto out;
1947 r = -EFAULT;
1948 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1949 goto out;
1950 r = 0;
1951 break;
1953 case KVM_SET_FPU: {
1954 fpu = memdup_user(argp, sizeof(*fpu));
1955 if (IS_ERR(fpu)) {
1956 r = PTR_ERR(fpu);
1957 goto out;
1959 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1960 if (r)
1961 goto out;
1962 r = 0;
1963 break;
1965 default:
1966 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1968 out:
1969 vcpu_put(vcpu);
1970 kfree(fpu);
1971 kfree(kvm_sregs);
1972 return r;
1975 #ifdef CONFIG_COMPAT
1976 static long kvm_vcpu_compat_ioctl(struct file *filp,
1977 unsigned int ioctl, unsigned long arg)
1979 struct kvm_vcpu *vcpu = filp->private_data;
1980 void __user *argp = compat_ptr(arg);
1981 int r;
1983 if (vcpu->kvm->mm != current->mm)
1984 return -EIO;
1986 switch (ioctl) {
1987 case KVM_SET_SIGNAL_MASK: {
1988 struct kvm_signal_mask __user *sigmask_arg = argp;
1989 struct kvm_signal_mask kvm_sigmask;
1990 compat_sigset_t csigset;
1991 sigset_t sigset;
1993 if (argp) {
1994 r = -EFAULT;
1995 if (copy_from_user(&kvm_sigmask, argp,
1996 sizeof kvm_sigmask))
1997 goto out;
1998 r = -EINVAL;
1999 if (kvm_sigmask.len != sizeof csigset)
2000 goto out;
2001 r = -EFAULT;
2002 if (copy_from_user(&csigset, sigmask_arg->sigset,
2003 sizeof csigset))
2004 goto out;
2006 sigset_from_compat(&sigset, &csigset);
2007 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2008 break;
2010 default:
2011 r = kvm_vcpu_ioctl(filp, ioctl, arg);
2014 out:
2015 return r;
2017 #endif
2019 static long kvm_vm_ioctl(struct file *filp,
2020 unsigned int ioctl, unsigned long arg)
2022 struct kvm *kvm = filp->private_data;
2023 void __user *argp = (void __user *)arg;
2024 int r;
2026 if (kvm->mm != current->mm)
2027 return -EIO;
2028 switch (ioctl) {
2029 case KVM_CREATE_VCPU:
2030 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2031 if (r < 0)
2032 goto out;
2033 break;
2034 case KVM_SET_USER_MEMORY_REGION: {
2035 struct kvm_userspace_memory_region kvm_userspace_mem;
2037 r = -EFAULT;
2038 if (copy_from_user(&kvm_userspace_mem, argp,
2039 sizeof kvm_userspace_mem))
2040 goto out;
2042 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
2043 if (r)
2044 goto out;
2045 break;
2047 case KVM_GET_DIRTY_LOG: {
2048 struct kvm_dirty_log log;
2050 r = -EFAULT;
2051 if (copy_from_user(&log, argp, sizeof log))
2052 goto out;
2053 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2054 if (r)
2055 goto out;
2056 break;
2058 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2059 case KVM_REGISTER_COALESCED_MMIO: {
2060 struct kvm_coalesced_mmio_zone zone;
2061 r = -EFAULT;
2062 if (copy_from_user(&zone, argp, sizeof zone))
2063 goto out;
2064 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2065 if (r)
2066 goto out;
2067 r = 0;
2068 break;
2070 case KVM_UNREGISTER_COALESCED_MMIO: {
2071 struct kvm_coalesced_mmio_zone zone;
2072 r = -EFAULT;
2073 if (copy_from_user(&zone, argp, sizeof zone))
2074 goto out;
2075 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2076 if (r)
2077 goto out;
2078 r = 0;
2079 break;
2081 #endif
2082 case KVM_IRQFD: {
2083 struct kvm_irqfd data;
2085 r = -EFAULT;
2086 if (copy_from_user(&data, argp, sizeof data))
2087 goto out;
2088 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2089 break;
2091 case KVM_IOEVENTFD: {
2092 struct kvm_ioeventfd data;
2094 r = -EFAULT;
2095 if (copy_from_user(&data, argp, sizeof data))
2096 goto out;
2097 r = kvm_ioeventfd(kvm, &data);
2098 break;
2100 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2101 case KVM_SET_BOOT_CPU_ID:
2102 r = 0;
2103 mutex_lock(&kvm->lock);
2104 if (atomic_read(&kvm->online_vcpus) != 0)
2105 r = -EBUSY;
2106 else
2107 kvm->bsp_vcpu_id = arg;
2108 mutex_unlock(&kvm->lock);
2109 break;
2110 #endif
2111 default:
2112 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2113 if (r == -ENOTTY)
2114 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
2116 out:
2117 return r;
2120 #ifdef CONFIG_COMPAT
2121 struct compat_kvm_dirty_log {
2122 __u32 slot;
2123 __u32 padding1;
2124 union {
2125 compat_uptr_t dirty_bitmap; /* one bit per page */
2126 __u64 padding2;
2130 static long kvm_vm_compat_ioctl(struct file *filp,
2131 unsigned int ioctl, unsigned long arg)
2133 struct kvm *kvm = filp->private_data;
2134 int r;
2136 if (kvm->mm != current->mm)
2137 return -EIO;
2138 switch (ioctl) {
2139 case KVM_GET_DIRTY_LOG: {
2140 struct compat_kvm_dirty_log compat_log;
2141 struct kvm_dirty_log log;
2143 r = -EFAULT;
2144 if (copy_from_user(&compat_log, (void __user *)arg,
2145 sizeof(compat_log)))
2146 goto out;
2147 log.slot = compat_log.slot;
2148 log.padding1 = compat_log.padding1;
2149 log.padding2 = compat_log.padding2;
2150 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2152 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2153 if (r)
2154 goto out;
2155 break;
2157 default:
2158 r = kvm_vm_ioctl(filp, ioctl, arg);
2161 out:
2162 return r;
2164 #endif
2166 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2168 struct page *page[1];
2169 unsigned long addr;
2170 int npages;
2171 gfn_t gfn = vmf->pgoff;
2172 struct kvm *kvm = vma->vm_file->private_data;
2174 addr = gfn_to_hva(kvm, gfn);
2175 if (kvm_is_error_hva(addr))
2176 return VM_FAULT_SIGBUS;
2178 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2179 NULL);
2180 if (unlikely(npages != 1))
2181 return VM_FAULT_SIGBUS;
2183 vmf->page = page[0];
2184 return 0;
2187 static const struct vm_operations_struct kvm_vm_vm_ops = {
2188 .fault = kvm_vm_fault,
2191 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2193 vma->vm_ops = &kvm_vm_vm_ops;
2194 return 0;
2197 static struct file_operations kvm_vm_fops = {
2198 .release = kvm_vm_release,
2199 .unlocked_ioctl = kvm_vm_ioctl,
2200 #ifdef CONFIG_COMPAT
2201 .compat_ioctl = kvm_vm_compat_ioctl,
2202 #endif
2203 .mmap = kvm_vm_mmap,
2204 .llseek = noop_llseek,
2207 static int kvm_dev_ioctl_create_vm(void)
2209 int r;
2210 struct kvm *kvm;
2212 kvm = kvm_create_vm();
2213 if (IS_ERR(kvm))
2214 return PTR_ERR(kvm);
2215 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2216 r = kvm_coalesced_mmio_init(kvm);
2217 if (r < 0) {
2218 kvm_put_kvm(kvm);
2219 return r;
2221 #endif
2222 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2223 if (r < 0)
2224 kvm_put_kvm(kvm);
2226 return r;
2229 static long kvm_dev_ioctl_check_extension_generic(long arg)
2231 switch (arg) {
2232 case KVM_CAP_USER_MEMORY:
2233 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2234 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2235 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2236 case KVM_CAP_SET_BOOT_CPU_ID:
2237 #endif
2238 case KVM_CAP_INTERNAL_ERROR_DATA:
2239 return 1;
2240 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2241 case KVM_CAP_IRQ_ROUTING:
2242 return KVM_MAX_IRQ_ROUTES;
2243 #endif
2244 default:
2245 break;
2247 return kvm_dev_ioctl_check_extension(arg);
2250 static long kvm_dev_ioctl(struct file *filp,
2251 unsigned int ioctl, unsigned long arg)
2253 long r = -EINVAL;
2255 switch (ioctl) {
2256 case KVM_GET_API_VERSION:
2257 r = -EINVAL;
2258 if (arg)
2259 goto out;
2260 r = KVM_API_VERSION;
2261 break;
2262 case KVM_CREATE_VM:
2263 r = -EINVAL;
2264 if (arg)
2265 goto out;
2266 r = kvm_dev_ioctl_create_vm();
2267 break;
2268 case KVM_CHECK_EXTENSION:
2269 r = kvm_dev_ioctl_check_extension_generic(arg);
2270 break;
2271 case KVM_GET_VCPU_MMAP_SIZE:
2272 r = -EINVAL;
2273 if (arg)
2274 goto out;
2275 r = PAGE_SIZE; /* struct kvm_run */
2276 #ifdef CONFIG_X86
2277 r += PAGE_SIZE; /* pio data page */
2278 #endif
2279 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2280 r += PAGE_SIZE; /* coalesced mmio ring page */
2281 #endif
2282 break;
2283 case KVM_TRACE_ENABLE:
2284 case KVM_TRACE_PAUSE:
2285 case KVM_TRACE_DISABLE:
2286 r = -EOPNOTSUPP;
2287 break;
2288 default:
2289 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2291 out:
2292 return r;
2295 static struct file_operations kvm_chardev_ops = {
2296 .unlocked_ioctl = kvm_dev_ioctl,
2297 .compat_ioctl = kvm_dev_ioctl,
2298 .llseek = noop_llseek,
2301 static struct miscdevice kvm_dev = {
2302 KVM_MINOR,
2303 "kvm",
2304 &kvm_chardev_ops,
2307 static void hardware_enable_nolock(void *junk)
2309 int cpu = raw_smp_processor_id();
2310 int r;
2312 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2313 return;
2315 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2317 r = kvm_arch_hardware_enable(NULL);
2319 if (r) {
2320 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2321 atomic_inc(&hardware_enable_failed);
2322 printk(KERN_INFO "kvm: enabling virtualization on "
2323 "CPU%d failed\n", cpu);
2327 static void hardware_enable(void *junk)
2329 raw_spin_lock(&kvm_lock);
2330 hardware_enable_nolock(junk);
2331 raw_spin_unlock(&kvm_lock);
2334 static void hardware_disable_nolock(void *junk)
2336 int cpu = raw_smp_processor_id();
2338 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2339 return;
2340 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2341 kvm_arch_hardware_disable(NULL);
2344 static void hardware_disable(void *junk)
2346 raw_spin_lock(&kvm_lock);
2347 hardware_disable_nolock(junk);
2348 raw_spin_unlock(&kvm_lock);
2351 static void hardware_disable_all_nolock(void)
2353 BUG_ON(!kvm_usage_count);
2355 kvm_usage_count--;
2356 if (!kvm_usage_count)
2357 on_each_cpu(hardware_disable_nolock, NULL, 1);
2360 static void hardware_disable_all(void)
2362 raw_spin_lock(&kvm_lock);
2363 hardware_disable_all_nolock();
2364 raw_spin_unlock(&kvm_lock);
2367 static int hardware_enable_all(void)
2369 int r = 0;
2371 raw_spin_lock(&kvm_lock);
2373 kvm_usage_count++;
2374 if (kvm_usage_count == 1) {
2375 atomic_set(&hardware_enable_failed, 0);
2376 on_each_cpu(hardware_enable_nolock, NULL, 1);
2378 if (atomic_read(&hardware_enable_failed)) {
2379 hardware_disable_all_nolock();
2380 r = -EBUSY;
2384 raw_spin_unlock(&kvm_lock);
2386 return r;
2389 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2390 void *v)
2392 int cpu = (long)v;
2394 if (!kvm_usage_count)
2395 return NOTIFY_OK;
2397 val &= ~CPU_TASKS_FROZEN;
2398 switch (val) {
2399 case CPU_DYING:
2400 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2401 cpu);
2402 hardware_disable(NULL);
2403 break;
2404 case CPU_STARTING:
2405 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2406 cpu);
2407 hardware_enable(NULL);
2408 break;
2410 return NOTIFY_OK;
2414 asmlinkage void kvm_spurious_fault(void)
2416 /* Fault while not rebooting. We want the trace. */
2417 BUG();
2419 EXPORT_SYMBOL_GPL(kvm_spurious_fault);
2421 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2422 void *v)
2425 * Some (well, at least mine) BIOSes hang on reboot if
2426 * in vmx root mode.
2428 * And Intel TXT required VMX off for all cpu when system shutdown.
2430 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2431 kvm_rebooting = true;
2432 on_each_cpu(hardware_disable_nolock, NULL, 1);
2433 return NOTIFY_OK;
2436 static struct notifier_block kvm_reboot_notifier = {
2437 .notifier_call = kvm_reboot,
2438 .priority = 0,
2441 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2443 int i;
2445 for (i = 0; i < bus->dev_count; i++) {
2446 struct kvm_io_device *pos = bus->range[i].dev;
2448 kvm_iodevice_destructor(pos);
2450 kfree(bus);
2453 int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
2455 const struct kvm_io_range *r1 = p1;
2456 const struct kvm_io_range *r2 = p2;
2458 if (r1->addr < r2->addr)
2459 return -1;
2460 if (r1->addr + r1->len > r2->addr + r2->len)
2461 return 1;
2462 return 0;
2465 int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
2466 gpa_t addr, int len)
2468 if (bus->dev_count == NR_IOBUS_DEVS)
2469 return -ENOSPC;
2471 bus->range[bus->dev_count++] = (struct kvm_io_range) {
2472 .addr = addr,
2473 .len = len,
2474 .dev = dev,
2477 sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
2478 kvm_io_bus_sort_cmp, NULL);
2480 return 0;
2483 int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
2484 gpa_t addr, int len)
2486 struct kvm_io_range *range, key;
2487 int off;
2489 key = (struct kvm_io_range) {
2490 .addr = addr,
2491 .len = len,
2494 range = bsearch(&key, bus->range, bus->dev_count,
2495 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
2496 if (range == NULL)
2497 return -ENOENT;
2499 off = range - bus->range;
2501 while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
2502 off--;
2504 return off;
2507 /* kvm_io_bus_write - called under kvm->slots_lock */
2508 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2509 int len, const void *val)
2511 int idx;
2512 struct kvm_io_bus *bus;
2513 struct kvm_io_range range;
2515 range = (struct kvm_io_range) {
2516 .addr = addr,
2517 .len = len,
2520 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2521 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2522 if (idx < 0)
2523 return -EOPNOTSUPP;
2525 while (idx < bus->dev_count &&
2526 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2527 if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
2528 return 0;
2529 idx++;
2532 return -EOPNOTSUPP;
2535 /* kvm_io_bus_read - called under kvm->slots_lock */
2536 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2537 int len, void *val)
2539 int idx;
2540 struct kvm_io_bus *bus;
2541 struct kvm_io_range range;
2543 range = (struct kvm_io_range) {
2544 .addr = addr,
2545 .len = len,
2548 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2549 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2550 if (idx < 0)
2551 return -EOPNOTSUPP;
2553 while (idx < bus->dev_count &&
2554 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2555 if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
2556 return 0;
2557 idx++;
2560 return -EOPNOTSUPP;
2563 /* Caller must hold slots_lock. */
2564 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2565 int len, struct kvm_io_device *dev)
2567 struct kvm_io_bus *new_bus, *bus;
2569 bus = kvm->buses[bus_idx];
2570 if (bus->dev_count > NR_IOBUS_DEVS-1)
2571 return -ENOSPC;
2573 new_bus = kmemdup(bus, sizeof(struct kvm_io_bus), GFP_KERNEL);
2574 if (!new_bus)
2575 return -ENOMEM;
2576 kvm_io_bus_insert_dev(new_bus, dev, addr, len);
2577 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2578 synchronize_srcu_expedited(&kvm->srcu);
2579 kfree(bus);
2581 return 0;
2584 /* Caller must hold slots_lock. */
2585 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2586 struct kvm_io_device *dev)
2588 int i, r;
2589 struct kvm_io_bus *new_bus, *bus;
2591 bus = kvm->buses[bus_idx];
2593 new_bus = kmemdup(bus, sizeof(*bus), GFP_KERNEL);
2594 if (!new_bus)
2595 return -ENOMEM;
2597 r = -ENOENT;
2598 for (i = 0; i < new_bus->dev_count; i++)
2599 if (new_bus->range[i].dev == dev) {
2600 r = 0;
2601 new_bus->dev_count--;
2602 new_bus->range[i] = new_bus->range[new_bus->dev_count];
2603 sort(new_bus->range, new_bus->dev_count,
2604 sizeof(struct kvm_io_range),
2605 kvm_io_bus_sort_cmp, NULL);
2606 break;
2609 if (r) {
2610 kfree(new_bus);
2611 return r;
2614 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2615 synchronize_srcu_expedited(&kvm->srcu);
2616 kfree(bus);
2617 return r;
2620 static struct notifier_block kvm_cpu_notifier = {
2621 .notifier_call = kvm_cpu_hotplug,
2624 static int vm_stat_get(void *_offset, u64 *val)
2626 unsigned offset = (long)_offset;
2627 struct kvm *kvm;
2629 *val = 0;
2630 raw_spin_lock(&kvm_lock);
2631 list_for_each_entry(kvm, &vm_list, vm_list)
2632 *val += *(u32 *)((void *)kvm + offset);
2633 raw_spin_unlock(&kvm_lock);
2634 return 0;
2637 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2639 static int vcpu_stat_get(void *_offset, u64 *val)
2641 unsigned offset = (long)_offset;
2642 struct kvm *kvm;
2643 struct kvm_vcpu *vcpu;
2644 int i;
2646 *val = 0;
2647 raw_spin_lock(&kvm_lock);
2648 list_for_each_entry(kvm, &vm_list, vm_list)
2649 kvm_for_each_vcpu(i, vcpu, kvm)
2650 *val += *(u32 *)((void *)vcpu + offset);
2652 raw_spin_unlock(&kvm_lock);
2653 return 0;
2656 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2658 static const struct file_operations *stat_fops[] = {
2659 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2660 [KVM_STAT_VM] = &vm_stat_fops,
2663 static int kvm_init_debug(void)
2665 int r = -EFAULT;
2666 struct kvm_stats_debugfs_item *p;
2668 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2669 if (kvm_debugfs_dir == NULL)
2670 goto out;
2672 for (p = debugfs_entries; p->name; ++p) {
2673 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2674 (void *)(long)p->offset,
2675 stat_fops[p->kind]);
2676 if (p->dentry == NULL)
2677 goto out_dir;
2680 return 0;
2682 out_dir:
2683 debugfs_remove_recursive(kvm_debugfs_dir);
2684 out:
2685 return r;
2688 static void kvm_exit_debug(void)
2690 struct kvm_stats_debugfs_item *p;
2692 for (p = debugfs_entries; p->name; ++p)
2693 debugfs_remove(p->dentry);
2694 debugfs_remove(kvm_debugfs_dir);
2697 static int kvm_suspend(void)
2699 if (kvm_usage_count)
2700 hardware_disable_nolock(NULL);
2701 return 0;
2704 static void kvm_resume(void)
2706 if (kvm_usage_count) {
2707 WARN_ON(raw_spin_is_locked(&kvm_lock));
2708 hardware_enable_nolock(NULL);
2712 static struct syscore_ops kvm_syscore_ops = {
2713 .suspend = kvm_suspend,
2714 .resume = kvm_resume,
2717 struct page *bad_page;
2718 pfn_t bad_pfn;
2720 static inline
2721 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2723 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2726 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2728 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2730 kvm_arch_vcpu_load(vcpu, cpu);
2733 static void kvm_sched_out(struct preempt_notifier *pn,
2734 struct task_struct *next)
2736 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2738 kvm_arch_vcpu_put(vcpu);
2741 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
2742 struct module *module)
2744 int r;
2745 int cpu;
2747 r = kvm_arch_init(opaque);
2748 if (r)
2749 goto out_fail;
2751 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2753 if (bad_page == NULL) {
2754 r = -ENOMEM;
2755 goto out;
2758 bad_pfn = page_to_pfn(bad_page);
2760 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2762 if (hwpoison_page == NULL) {
2763 r = -ENOMEM;
2764 goto out_free_0;
2767 hwpoison_pfn = page_to_pfn(hwpoison_page);
2769 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2771 if (fault_page == NULL) {
2772 r = -ENOMEM;
2773 goto out_free_0;
2776 fault_pfn = page_to_pfn(fault_page);
2778 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2779 r = -ENOMEM;
2780 goto out_free_0;
2783 r = kvm_arch_hardware_setup();
2784 if (r < 0)
2785 goto out_free_0a;
2787 for_each_online_cpu(cpu) {
2788 smp_call_function_single(cpu,
2789 kvm_arch_check_processor_compat,
2790 &r, 1);
2791 if (r < 0)
2792 goto out_free_1;
2795 r = register_cpu_notifier(&kvm_cpu_notifier);
2796 if (r)
2797 goto out_free_2;
2798 register_reboot_notifier(&kvm_reboot_notifier);
2800 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2801 if (!vcpu_align)
2802 vcpu_align = __alignof__(struct kvm_vcpu);
2803 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
2804 0, NULL);
2805 if (!kvm_vcpu_cache) {
2806 r = -ENOMEM;
2807 goto out_free_3;
2810 r = kvm_async_pf_init();
2811 if (r)
2812 goto out_free;
2814 kvm_chardev_ops.owner = module;
2815 kvm_vm_fops.owner = module;
2816 kvm_vcpu_fops.owner = module;
2818 r = misc_register(&kvm_dev);
2819 if (r) {
2820 printk(KERN_ERR "kvm: misc device register failed\n");
2821 goto out_unreg;
2824 register_syscore_ops(&kvm_syscore_ops);
2826 kvm_preempt_ops.sched_in = kvm_sched_in;
2827 kvm_preempt_ops.sched_out = kvm_sched_out;
2829 r = kvm_init_debug();
2830 if (r) {
2831 printk(KERN_ERR "kvm: create debugfs files failed\n");
2832 goto out_undebugfs;
2835 return 0;
2837 out_undebugfs:
2838 unregister_syscore_ops(&kvm_syscore_ops);
2839 out_unreg:
2840 kvm_async_pf_deinit();
2841 out_free:
2842 kmem_cache_destroy(kvm_vcpu_cache);
2843 out_free_3:
2844 unregister_reboot_notifier(&kvm_reboot_notifier);
2845 unregister_cpu_notifier(&kvm_cpu_notifier);
2846 out_free_2:
2847 out_free_1:
2848 kvm_arch_hardware_unsetup();
2849 out_free_0a:
2850 free_cpumask_var(cpus_hardware_enabled);
2851 out_free_0:
2852 if (fault_page)
2853 __free_page(fault_page);
2854 if (hwpoison_page)
2855 __free_page(hwpoison_page);
2856 __free_page(bad_page);
2857 out:
2858 kvm_arch_exit();
2859 out_fail:
2860 return r;
2862 EXPORT_SYMBOL_GPL(kvm_init);
2864 void kvm_exit(void)
2866 kvm_exit_debug();
2867 misc_deregister(&kvm_dev);
2868 kmem_cache_destroy(kvm_vcpu_cache);
2869 kvm_async_pf_deinit();
2870 unregister_syscore_ops(&kvm_syscore_ops);
2871 unregister_reboot_notifier(&kvm_reboot_notifier);
2872 unregister_cpu_notifier(&kvm_cpu_notifier);
2873 on_each_cpu(hardware_disable_nolock, NULL, 1);
2874 kvm_arch_hardware_unsetup();
2875 kvm_arch_exit();
2876 free_cpumask_var(cpus_hardware_enabled);
2877 __free_page(hwpoison_page);
2878 __free_page(bad_page);
2880 EXPORT_SYMBOL_GPL(kvm_exit);