blob: f4732bd2816cf340e8bbd96030fcfeb943c00b48 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
9 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
19#include "iodev.h"
20
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>
52
53#include <asm/processor.h>
54#include <asm/io.h>
55#include <asm/ioctl.h>
56#include <asm/uaccess.h>
57#include <asm/pgtable.h>
58
59#include "coalesced_mmio.h"
60#include "async_pf.h"
61
62#define CREATE_TRACE_POINTS
63#include <trace/events/kvm.h>
64
65MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
68/*
69 * Ordering of locks:
70 *
71 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
72 */
73
74DEFINE_RAW_SPINLOCK(kvm_lock);
75LIST_HEAD(vm_list);
76
77static cpumask_var_t cpus_hardware_enabled;
78static int kvm_usage_count = 0;
79static atomic_t hardware_enable_failed;
80
81struct kmem_cache *kvm_vcpu_cache;
82EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
83
84static __read_mostly struct preempt_ops kvm_preempt_ops;
85
86struct dentry *kvm_debugfs_dir;
87
88static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
89 unsigned long arg);
90#ifdef CONFIG_COMPAT
91static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
92 unsigned long arg);
93#endif
94static int hardware_enable_all(void);
95static void hardware_disable_all(void);
96
97static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
98
99bool kvm_rebooting;
100EXPORT_SYMBOL_GPL(kvm_rebooting);
101
102static bool largepages_enabled = true;
103
104static struct page *hwpoison_page;
105static pfn_t hwpoison_pfn;
106
107struct page *fault_page;
108pfn_t fault_pfn;
109
110inline int kvm_is_mmio_pfn(pfn_t pfn)
111{
112 if (pfn_valid(pfn)) {
113 int reserved;
114 struct page *tail = pfn_to_page(pfn);
115 struct page *head = compound_trans_head(tail);
116 reserved = PageReserved(head);
117 if (head != tail) {
118 /*
119 * "head" is not a dangling pointer
120 * (compound_trans_head takes care of that)
121 * but the hugepage may have been splitted
122 * from under us (and we may not hold a
123 * reference count on the head page so it can
124 * be reused before we run PageReferenced), so
125 * we've to check PageTail before returning
126 * what we just read.
127 */
128 smp_rmb();
129 if (PageTail(tail))
130 return reserved;
131 }
132 return PageReserved(tail);
133 }
134
135 return true;
136}
137
138/*
139 * Switches to specified vcpu, until a matching vcpu_put()
140 */
141void vcpu_load(struct kvm_vcpu *vcpu)
142{
143 int cpu;
144
145 mutex_lock(&vcpu->mutex);
146 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
147 /* The thread running this VCPU changed. */
148 struct pid *oldpid = vcpu->pid;
149 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
150 rcu_assign_pointer(vcpu->pid, newpid);
151 synchronize_rcu();
152 put_pid(oldpid);
153 }
154 cpu = get_cpu();
155 preempt_notifier_register(&vcpu->preempt_notifier);
156 kvm_arch_vcpu_load(vcpu, cpu);
157 put_cpu();
158}
159
160void vcpu_put(struct kvm_vcpu *vcpu)
161{
162 preempt_disable();
163 kvm_arch_vcpu_put(vcpu);
164 preempt_notifier_unregister(&vcpu->preempt_notifier);
165 preempt_enable();
166 mutex_unlock(&vcpu->mutex);
167}
168
169static void ack_flush(void *_completed)
170{
171}
172
173static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
174{
175 int i, cpu, me;
176 cpumask_var_t cpus;
177 bool called = true;
178 struct kvm_vcpu *vcpu;
179
180 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
181
182 me = get_cpu();
183 kvm_for_each_vcpu(i, vcpu, kvm) {
184 kvm_make_request(req, vcpu);
185 cpu = vcpu->cpu;
186
187 /* Set ->requests bit before we read ->mode */
188 smp_mb();
189
190 if (cpus != NULL && cpu != -1 && cpu != me &&
191 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
192 cpumask_set_cpu(cpu, cpus);
193 }
194 if (unlikely(cpus == NULL))
195 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
196 else if (!cpumask_empty(cpus))
197 smp_call_function_many(cpus, ack_flush, NULL, 1);
198 else
199 called = false;
200 put_cpu();
201 free_cpumask_var(cpus);
202 return called;
203}
204
205void kvm_flush_remote_tlbs(struct kvm *kvm)
206{
207 long dirty_count = kvm->tlbs_dirty;
208
209 smp_mb();
210 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
211 ++kvm->stat.remote_tlb_flush;
212 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
213}
214
215void kvm_reload_remote_mmus(struct kvm *kvm)
216{
217 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
218}
219
220int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
221{
222 struct page *page;
223 int r;
224
225 mutex_init(&vcpu->mutex);
226 vcpu->cpu = -1;
227 vcpu->kvm = kvm;
228 vcpu->vcpu_id = id;
229 vcpu->pid = NULL;
230 init_waitqueue_head(&vcpu->wq);
231 kvm_async_pf_vcpu_init(vcpu);
232
233 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
234 if (!page) {
235 r = -ENOMEM;
236 goto fail;
237 }
238 vcpu->run = page_address(page);
239
240 r = kvm_arch_vcpu_init(vcpu);
241 if (r < 0)
242 goto fail_free_run;
243 return 0;
244
245fail_free_run:
246 free_page((unsigned long)vcpu->run);
247fail:
248 return r;
249}
250EXPORT_SYMBOL_GPL(kvm_vcpu_init);
251
252void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
253{
254 put_pid(vcpu->pid);
255 kvm_arch_vcpu_uninit(vcpu);
256 free_page((unsigned long)vcpu->run);
257}
258EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
259
260#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
261static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
262{
263 return container_of(mn, struct kvm, mmu_notifier);
264}
265
266static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
267 struct mm_struct *mm,
268 unsigned long address)
269{
270 struct kvm *kvm = mmu_notifier_to_kvm(mn);
271 int need_tlb_flush, idx;
272
273 /*
274 * When ->invalidate_page runs, the linux pte has been zapped
275 * already but the page is still allocated until
276 * ->invalidate_page returns. So if we increase the sequence
277 * here the kvm page fault will notice if the spte can't be
278 * established because the page is going to be freed. If
279 * instead the kvm page fault establishes the spte before
280 * ->invalidate_page runs, kvm_unmap_hva will release it
281 * before returning.
282 *
283 * The sequence increase only need to be seen at spin_unlock
284 * time, and not at spin_lock time.
285 *
286 * Increasing the sequence after the spin_unlock would be
287 * unsafe because the kvm page fault could then establish the
288 * pte after kvm_unmap_hva returned, without noticing the page
289 * is going to be freed.
290 */
291 idx = srcu_read_lock(&kvm->srcu);
292 spin_lock(&kvm->mmu_lock);
293
294 kvm->mmu_notifier_seq++;
295 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
296 /* we've to flush the tlb before the pages can be freed */
297 if (need_tlb_flush)
298 kvm_flush_remote_tlbs(kvm);
299
300 spin_unlock(&kvm->mmu_lock);
301 srcu_read_unlock(&kvm->srcu, idx);
302}
303
304static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
305 struct mm_struct *mm,
306 unsigned long address,
307 pte_t pte)
308{
309 struct kvm *kvm = mmu_notifier_to_kvm(mn);
310 int idx;
311
312 idx = srcu_read_lock(&kvm->srcu);
313 spin_lock(&kvm->mmu_lock);
314 kvm->mmu_notifier_seq++;
315 kvm_set_spte_hva(kvm, address, pte);
316 spin_unlock(&kvm->mmu_lock);
317 srcu_read_unlock(&kvm->srcu, idx);
318}
319
320static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
321 struct mm_struct *mm,
322 unsigned long start,
323 unsigned long end)
324{
325 struct kvm *kvm = mmu_notifier_to_kvm(mn);
326 int need_tlb_flush = 0, idx;
327
328 idx = srcu_read_lock(&kvm->srcu);
329 spin_lock(&kvm->mmu_lock);
330 /*
331 * The count increase must become visible at unlock time as no
332 * spte can be established without taking the mmu_lock and
333 * count is also read inside the mmu_lock critical section.
334 */
335 kvm->mmu_notifier_count++;
336 for (; start < end; start += PAGE_SIZE)
337 need_tlb_flush |= kvm_unmap_hva(kvm, start);
338 need_tlb_flush |= kvm->tlbs_dirty;
339 /* we've to flush the tlb before the pages can be freed */
340 if (need_tlb_flush)
341 kvm_flush_remote_tlbs(kvm);
342
343 spin_unlock(&kvm->mmu_lock);
344 srcu_read_unlock(&kvm->srcu, idx);
345}
346
347static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
348 struct mm_struct *mm,
349 unsigned long start,
350 unsigned long end)
351{
352 struct kvm *kvm = mmu_notifier_to_kvm(mn);
353
354 spin_lock(&kvm->mmu_lock);
355 /*
356 * This sequence increase will notify the kvm page fault that
357 * the page that is going to be mapped in the spte could have
358 * been freed.
359 */
360 kvm->mmu_notifier_seq++;
361 smp_wmb();
362 /*
363 * The above sequence increase must be visible before the
364 * below count decrease, which is ensured by the smp_wmb above
365 * in conjunction with the smp_rmb in mmu_notifier_retry().
366 */
367 kvm->mmu_notifier_count--;
368 spin_unlock(&kvm->mmu_lock);
369
370 BUG_ON(kvm->mmu_notifier_count < 0);
371}
372
373static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
374 struct mm_struct *mm,
375 unsigned long address)
376{
377 struct kvm *kvm = mmu_notifier_to_kvm(mn);
378 int young, idx;
379
380 idx = srcu_read_lock(&kvm->srcu);
381 spin_lock(&kvm->mmu_lock);
382
383 young = kvm_age_hva(kvm, address);
384 if (young)
385 kvm_flush_remote_tlbs(kvm);
386
387 spin_unlock(&kvm->mmu_lock);
388 srcu_read_unlock(&kvm->srcu, idx);
389
390 return young;
391}
392
393static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
394 struct mm_struct *mm,
395 unsigned long address)
396{
397 struct kvm *kvm = mmu_notifier_to_kvm(mn);
398 int young, idx;
399
400 idx = srcu_read_lock(&kvm->srcu);
401 spin_lock(&kvm->mmu_lock);
402 young = kvm_test_age_hva(kvm, address);
403 spin_unlock(&kvm->mmu_lock);
404 srcu_read_unlock(&kvm->srcu, idx);
405
406 return young;
407}
408
409static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
410 struct mm_struct *mm)
411{
412 struct kvm *kvm = mmu_notifier_to_kvm(mn);
413 int idx;
414
415 idx = srcu_read_lock(&kvm->srcu);
416 kvm_arch_flush_shadow(kvm);
417 srcu_read_unlock(&kvm->srcu, idx);
418}
419
420static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
421 .invalidate_page = kvm_mmu_notifier_invalidate_page,
422 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
423 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
424 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
425 .test_young = kvm_mmu_notifier_test_young,
426 .change_pte = kvm_mmu_notifier_change_pte,
427 .release = kvm_mmu_notifier_release,
428};
429
430static int kvm_init_mmu_notifier(struct kvm *kvm)
431{
432 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
433 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
434}
435
436#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
437
438static int kvm_init_mmu_notifier(struct kvm *kvm)
439{
440 return 0;
441}
442
443#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
444
445static void kvm_init_memslots_id(struct kvm *kvm)
446{
447 int i;
448 struct kvm_memslots *slots = kvm->memslots;
449
450 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
451 slots->id_to_index[i] = slots->memslots[i].id = i;
452}
453
454static struct kvm *kvm_create_vm(unsigned long type)
455{
456 int r, i;
457 struct kvm *kvm = kvm_arch_alloc_vm();
458
459 if (!kvm)
460 return ERR_PTR(-ENOMEM);
461
462 r = kvm_arch_init_vm(kvm, type);
463 if (r)
464 goto out_err_nodisable;
465
466 r = hardware_enable_all();
467 if (r)
468 goto out_err_nodisable;
469
470#ifdef CONFIG_HAVE_KVM_IRQCHIP
471 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
472 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
473#endif
474
475 r = -ENOMEM;
476 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
477 if (!kvm->memslots)
478 goto out_err_nosrcu;
479 kvm_init_memslots_id(kvm);
480 if (init_srcu_struct(&kvm->srcu))
481 goto out_err_nosrcu;
482 for (i = 0; i < KVM_NR_BUSES; i++) {
483 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
484 GFP_KERNEL);
485 if (!kvm->buses[i])
486 goto out_err;
487 }
488
489 spin_lock_init(&kvm->mmu_lock);
490 kvm->mm = current->mm;
491 atomic_inc(&kvm->mm->mm_count);
492 kvm_eventfd_init(kvm);
493 mutex_init(&kvm->lock);
494 mutex_init(&kvm->irq_lock);
495 mutex_init(&kvm->slots_lock);
496 atomic_set(&kvm->users_count, 1);
497
498 r = kvm_init_mmu_notifier(kvm);
499 if (r)
500 goto out_err;
501
502 raw_spin_lock(&kvm_lock);
503 list_add(&kvm->vm_list, &vm_list);
504 raw_spin_unlock(&kvm_lock);
505
506 return kvm;
507
508out_err:
509 cleanup_srcu_struct(&kvm->srcu);
510out_err_nosrcu:
511 hardware_disable_all();
512out_err_nodisable:
513 for (i = 0; i < KVM_NR_BUSES; i++)
514 kfree(kvm->buses[i]);
515 kfree(kvm->memslots);
516 kvm_arch_free_vm(kvm);
517 return ERR_PTR(r);
518}
519
520static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
521{
522 if (!memslot->dirty_bitmap)
523 return;
524
525 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
526 vfree(memslot->dirty_bitmap_head);
527 else
528 kfree(memslot->dirty_bitmap_head);
529
530 memslot->dirty_bitmap = NULL;
531 memslot->dirty_bitmap_head = NULL;
532}
533
534/*
535 * Free any memory in @free but not in @dont.
536 */
537static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
538 struct kvm_memory_slot *dont)
539{
540 if (!dont || free->rmap != dont->rmap)
541 vfree(free->rmap);
542
543 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
544 kvm_destroy_dirty_bitmap(free);
545
546 kvm_arch_free_memslot(free, dont);
547
548 free->npages = 0;
549 free->rmap = NULL;
550}
551
552void kvm_free_physmem(struct kvm *kvm)
553{
554 struct kvm_memslots *slots = kvm->memslots;
555 struct kvm_memory_slot *memslot;
556
557 kvm_for_each_memslot(memslot, slots)
558 kvm_free_physmem_slot(memslot, NULL);
559
560 kfree(kvm->memslots);
561}
562
563static void kvm_destroy_vm(struct kvm *kvm)
564{
565 int i;
566 struct mm_struct *mm = kvm->mm;
567
568 kvm_arch_sync_events(kvm);
569 raw_spin_lock(&kvm_lock);
570 list_del(&kvm->vm_list);
571 raw_spin_unlock(&kvm_lock);
572 kvm_free_irq_routing(kvm);
573 for (i = 0; i < KVM_NR_BUSES; i++)
574 kvm_io_bus_destroy(kvm->buses[i]);
575 kvm_coalesced_mmio_free(kvm);
576#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
577 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
578#else
579 kvm_arch_flush_shadow(kvm);
580#endif
581 kvm_arch_destroy_vm(kvm);
582 kvm_free_physmem(kvm);
583 cleanup_srcu_struct(&kvm->srcu);
584 kvm_arch_free_vm(kvm);
585 hardware_disable_all();
586 mmdrop(mm);
587}
588
589void kvm_get_kvm(struct kvm *kvm)
590{
591 atomic_inc(&kvm->users_count);
592}
593EXPORT_SYMBOL_GPL(kvm_get_kvm);
594
595void kvm_put_kvm(struct kvm *kvm)
596{
597 if (atomic_dec_and_test(&kvm->users_count))
598 kvm_destroy_vm(kvm);
599}
600EXPORT_SYMBOL_GPL(kvm_put_kvm);
601
602
603static int kvm_vm_release(struct inode *inode, struct file *filp)
604{
605 struct kvm *kvm = filp->private_data;
606
607 kvm_irqfd_release(kvm);
608
609 kvm_put_kvm(kvm);
610 return 0;
611}
612
613/*
614 * Allocation size is twice as large as the actual dirty bitmap size.
615 * This makes it possible to do double buffering: see x86's
616 * kvm_vm_ioctl_get_dirty_log().
617 */
618static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
619{
620#ifndef CONFIG_S390
621 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
622
623 if (dirty_bytes > PAGE_SIZE)
624 memslot->dirty_bitmap = vzalloc(dirty_bytes);
625 else
626 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
627
628 if (!memslot->dirty_bitmap)
629 return -ENOMEM;
630
631 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
632 memslot->nr_dirty_pages = 0;
633#endif /* !CONFIG_S390 */
634 return 0;
635}
636
637static int cmp_memslot(const void *slot1, const void *slot2)
638{
639 struct kvm_memory_slot *s1, *s2;
640
641 s1 = (struct kvm_memory_slot *)slot1;
642 s2 = (struct kvm_memory_slot *)slot2;
643
644 if (s1->npages < s2->npages)
645 return 1;
646 if (s1->npages > s2->npages)
647 return -1;
648
649 return 0;
650}
651
652/*
653 * Sort the memslots base on its size, so the larger slots
654 * will get better fit.
655 */
656static void sort_memslots(struct kvm_memslots *slots)
657{
658 int i;
659
660 sort(slots->memslots, KVM_MEM_SLOTS_NUM,
661 sizeof(struct kvm_memory_slot), cmp_memslot, NULL);
662
663 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
664 slots->id_to_index[slots->memslots[i].id] = i;
665}
666
667void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new)
668{
669 if (new) {
670 int id = new->id;
671 struct kvm_memory_slot *old = id_to_memslot(slots, id);
672 unsigned long npages = old->npages;
673
674 *old = *new;
675 if (new->npages != npages)
676 sort_memslots(slots);
677 }
678
679 slots->generation++;
680}
681
682/*
683 * Allocate some memory and give it an address in the guest physical address
684 * space.
685 *
686 * Discontiguous memory is allowed, mostly for framebuffers.
687 *
688 * Must be called holding mmap_sem for write.
689 */
690int __kvm_set_memory_region(struct kvm *kvm,
691 struct kvm_userspace_memory_region *mem,
692 int user_alloc)
693{
694 int r;
695 gfn_t base_gfn;
696 unsigned long npages;
697 struct kvm_memory_slot *memslot, *slot;
698 struct kvm_memory_slot old, new;
699 struct kvm_memslots *slots, *old_memslots;
700
701 r = -EINVAL;
702 /* General sanity checks */
703 if (mem->memory_size & (PAGE_SIZE - 1))
704 goto out;
705 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
706 goto out;
707 /* We can read the guest memory with __xxx_user() later on. */
708 if (user_alloc &&
709 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
710 !access_ok(VERIFY_WRITE,
711 (void __user *)(unsigned long)mem->userspace_addr,
712 mem->memory_size)))
713 goto out;
714 if (mem->slot >= KVM_MEM_SLOTS_NUM)
715 goto out;
716 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
717 goto out;
718
719 memslot = id_to_memslot(kvm->memslots, mem->slot);
720 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
721 npages = mem->memory_size >> PAGE_SHIFT;
722
723 r = -EINVAL;
724 if (npages > KVM_MEM_MAX_NR_PAGES)
725 goto out;
726
727 if (!npages)
728 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
729
730 new = old = *memslot;
731
732 new.id = mem->slot;
733 new.base_gfn = base_gfn;
734 new.npages = npages;
735 new.flags = mem->flags;
736
737 /* Disallow changing a memory slot's size. */
738 r = -EINVAL;
739 if (npages && old.npages && npages != old.npages)
740 goto out_free;
741
742 /* Check for overlaps */
743 r = -EEXIST;
744 kvm_for_each_memslot(slot, kvm->memslots) {
745 if (slot->id >= KVM_MEMORY_SLOTS || slot == memslot)
746 continue;
747 if (!((base_gfn + npages <= slot->base_gfn) ||
748 (base_gfn >= slot->base_gfn + slot->npages)))
749 goto out_free;
750 }
751
752 /* Free page dirty bitmap if unneeded */
753 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
754 new.dirty_bitmap = NULL;
755
756 r = -ENOMEM;
757
758 /* Allocate if a slot is being created */
759 if (npages && !old.npages) {
760 new.user_alloc = user_alloc;
761 new.userspace_addr = mem->userspace_addr;
762#ifndef CONFIG_S390
763 new.rmap = vzalloc(npages * sizeof(*new.rmap));
764 if (!new.rmap)
765 goto out_free;
766#endif /* not defined CONFIG_S390 */
767 if (kvm_arch_create_memslot(&new, npages))
768 goto out_free;
769 }
770
771 /* Allocate page dirty bitmap if needed */
772 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
773 if (kvm_create_dirty_bitmap(&new) < 0)
774 goto out_free;
775 /* destroy any largepage mappings for dirty tracking */
776 }
777
778 if (!npages || base_gfn != old.base_gfn) {
779 struct kvm_memory_slot *slot;
780
781 r = -ENOMEM;
782 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
783 GFP_KERNEL);
784 if (!slots)
785 goto out_free;
786 slot = id_to_memslot(slots, mem->slot);
787 slot->flags |= KVM_MEMSLOT_INVALID;
788
789 update_memslots(slots, NULL);
790
791 old_memslots = kvm->memslots;
792 rcu_assign_pointer(kvm->memslots, slots);
793 synchronize_srcu_expedited(&kvm->srcu);
794 /* slot was deleted or moved, clear iommu mapping */
795 kvm_iommu_unmap_pages(kvm, &old);
796 /* From this point no new shadow pages pointing to a deleted,
797 * or moved, memslot will be created.
798 *
799 * validation of sp->gfn happens in:
800 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
801 * - kvm_is_visible_gfn (mmu_check_roots)
802 */
803 kvm_arch_flush_shadow(kvm);
804 kfree(old_memslots);
805 }
806
807 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
808 if (r)
809 goto out_free;
810
811 r = -ENOMEM;
812 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
813 GFP_KERNEL);
814 if (!slots)
815 goto out_free;
816
817 /* map new memory slot into the iommu */
818 if (npages) {
819 r = kvm_iommu_map_pages(kvm, &new);
820 if (r)
821 goto out_slots;
822 }
823
824 /* actual memory is freed via old in kvm_free_physmem_slot below */
825 if (!npages) {
826 new.rmap = NULL;
827 new.dirty_bitmap = NULL;
828 memset(&new.arch, 0, sizeof(new.arch));
829 }
830
831 update_memslots(slots, &new);
832 old_memslots = kvm->memslots;
833 rcu_assign_pointer(kvm->memslots, slots);
834 synchronize_srcu_expedited(&kvm->srcu);
835
836 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
837
838 /*
839 * If the new memory slot is created, we need to clear all
840 * mmio sptes.
841 */
842 if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
843 kvm_arch_flush_shadow(kvm);
844
845 kvm_free_physmem_slot(&old, &new);
846 kfree(old_memslots);
847
848 return 0;
849
850out_slots:
851 kfree(slots);
852out_free:
853 kvm_free_physmem_slot(&new, &old);
854out:
855 return r;
856
857}
858EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
859
860int kvm_set_memory_region(struct kvm *kvm,
861 struct kvm_userspace_memory_region *mem,
862 int user_alloc)
863{
864 int r;
865
866 mutex_lock(&kvm->slots_lock);
867 r = __kvm_set_memory_region(kvm, mem, user_alloc);
868 mutex_unlock(&kvm->slots_lock);
869 return r;
870}
871EXPORT_SYMBOL_GPL(kvm_set_memory_region);
872
873int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
874 struct
875 kvm_userspace_memory_region *mem,
876 int user_alloc)
877{
878 if (mem->slot >= KVM_MEMORY_SLOTS)
879 return -EINVAL;
880 return kvm_set_memory_region(kvm, mem, user_alloc);
881}
882
883int kvm_get_dirty_log(struct kvm *kvm,
884 struct kvm_dirty_log *log, int *is_dirty)
885{
886 struct kvm_memory_slot *memslot;
887 int r, i;
888 unsigned long n;
889 unsigned long any = 0;
890
891 r = -EINVAL;
892 if (log->slot >= KVM_MEMORY_SLOTS)
893 goto out;
894
895 memslot = id_to_memslot(kvm->memslots, log->slot);
896 r = -ENOENT;
897 if (!memslot->dirty_bitmap)
898 goto out;
899
900 n = kvm_dirty_bitmap_bytes(memslot);
901
902 for (i = 0; !any && i < n/sizeof(long); ++i)
903 any = memslot->dirty_bitmap[i];
904
905 r = -EFAULT;
906 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
907 goto out;
908
909 if (any)
910 *is_dirty = 1;
911
912 r = 0;
913out:
914 return r;
915}
916
917bool kvm_largepages_enabled(void)
918{
919 return largepages_enabled;
920}
921
922void kvm_disable_largepages(void)
923{
924 largepages_enabled = false;
925}
926EXPORT_SYMBOL_GPL(kvm_disable_largepages);
927
928int is_error_page(struct page *page)
929{
930 return page == bad_page || page == hwpoison_page || page == fault_page;
931}
932EXPORT_SYMBOL_GPL(is_error_page);
933
934int is_error_pfn(pfn_t pfn)
935{
936 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
937}
938EXPORT_SYMBOL_GPL(is_error_pfn);
939
940int is_hwpoison_pfn(pfn_t pfn)
941{
942 return pfn == hwpoison_pfn;
943}
944EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
945
946int is_fault_pfn(pfn_t pfn)
947{
948 return pfn == fault_pfn;
949}
950EXPORT_SYMBOL_GPL(is_fault_pfn);
951
952int is_noslot_pfn(pfn_t pfn)
953{
954 return pfn == bad_pfn;
955}
956EXPORT_SYMBOL_GPL(is_noslot_pfn);
957
958int is_invalid_pfn(pfn_t pfn)
959{
960 return pfn == hwpoison_pfn || pfn == fault_pfn;
961}
962EXPORT_SYMBOL_GPL(is_invalid_pfn);
963
964static inline unsigned long bad_hva(void)
965{
966 return PAGE_OFFSET;
967}
968
969int kvm_is_error_hva(unsigned long addr)
970{
971 return addr == bad_hva();
972}
973EXPORT_SYMBOL_GPL(kvm_is_error_hva);
974
975struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
976{
977 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
978}
979EXPORT_SYMBOL_GPL(gfn_to_memslot);
980
981int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
982{
983 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
984
985 if (!memslot || memslot->id >= KVM_MEMORY_SLOTS ||
986 memslot->flags & KVM_MEMSLOT_INVALID)
987 return 0;
988
989 return 1;
990}
991EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
992
993unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
994{
995 struct vm_area_struct *vma;
996 unsigned long addr, size;
997
998 size = PAGE_SIZE;
999
1000 addr = gfn_to_hva(kvm, gfn);
1001 if (kvm_is_error_hva(addr))
1002 return PAGE_SIZE;
1003
1004 down_read(&current->mm->mmap_sem);
1005 vma = find_vma(current->mm, addr);
1006 if (!vma)
1007 goto out;
1008
1009 size = vma_kernel_pagesize(vma);
1010
1011out:
1012 up_read(&current->mm->mmap_sem);
1013
1014 return size;
1015}
1016
1017static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1018 gfn_t *nr_pages)
1019{
1020 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
1021 return bad_hva();
1022
1023 if (nr_pages)
1024 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1025
1026 return gfn_to_hva_memslot(slot, gfn);
1027}
1028
1029unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1030{
1031 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
1032}
1033EXPORT_SYMBOL_GPL(gfn_to_hva);
1034
1035static pfn_t get_fault_pfn(void)
1036{
1037 get_page(fault_page);
1038 return fault_pfn;
1039}
1040
1041int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1042 unsigned long start, int write, struct page **page)
1043{
1044 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1045
1046 if (write)
1047 flags |= FOLL_WRITE;
1048
1049 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1050}
1051
1052static inline int check_user_page_hwpoison(unsigned long addr)
1053{
1054 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1055
1056 rc = __get_user_pages(current, current->mm, addr, 1,
1057 flags, NULL, NULL, NULL);
1058 return rc == -EHWPOISON;
1059}
1060
1061static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
1062 bool *async, bool write_fault, bool *writable)
1063{
1064 struct page *page[1];
1065 int npages = 0;
1066 pfn_t pfn;
1067
1068 /* we can do it either atomically or asynchronously, not both */
1069 BUG_ON(atomic && async);
1070
1071 BUG_ON(!write_fault && !writable);
1072
1073 if (writable)
1074 *writable = true;
1075
1076 if (atomic || async)
1077 npages = __get_user_pages_fast(addr, 1, 1, page);
1078
1079 if (unlikely(npages != 1) && !atomic) {
1080 might_sleep();
1081
1082 if (writable)
1083 *writable = write_fault;
1084
1085 if (async) {
1086 down_read(&current->mm->mmap_sem);
1087 npages = get_user_page_nowait(current, current->mm,
1088 addr, write_fault, page);
1089 up_read(&current->mm->mmap_sem);
1090 } else
1091 npages = get_user_pages_fast(addr, 1, write_fault,
1092 page);
1093
1094 /* map read fault as writable if possible */
1095 if (unlikely(!write_fault) && npages == 1) {
1096 struct page *wpage[1];
1097
1098 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1099 if (npages == 1) {
1100 *writable = true;
1101 put_page(page[0]);
1102 page[0] = wpage[0];
1103 }
1104 npages = 1;
1105 }
1106 }
1107
1108 if (unlikely(npages != 1)) {
1109 struct vm_area_struct *vma;
1110
1111 if (atomic)
1112 return get_fault_pfn();
1113
1114 down_read(&current->mm->mmap_sem);
1115 if (npages == -EHWPOISON ||
1116 (!async && check_user_page_hwpoison(addr))) {
1117 up_read(&current->mm->mmap_sem);
1118 get_page(hwpoison_page);
1119 return page_to_pfn(hwpoison_page);
1120 }
1121
1122 vma = find_vma_intersection(current->mm, addr, addr+1);
1123
1124 if (vma == NULL)
1125 pfn = get_fault_pfn();
1126 else if ((vma->vm_flags & VM_PFNMAP)) {
1127 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1128 vma->vm_pgoff;
1129 BUG_ON(!kvm_is_mmio_pfn(pfn));
1130 } else {
1131 if (async && (vma->vm_flags & VM_WRITE))
1132 *async = true;
1133 pfn = get_fault_pfn();
1134 }
1135 up_read(&current->mm->mmap_sem);
1136 } else
1137 pfn = page_to_pfn(page[0]);
1138
1139 return pfn;
1140}
1141
1142pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1143{
1144 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
1145}
1146EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1147
1148static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1149 bool write_fault, bool *writable)
1150{
1151 unsigned long addr;
1152
1153 if (async)
1154 *async = false;
1155
1156 addr = gfn_to_hva(kvm, gfn);
1157 if (kvm_is_error_hva(addr)) {
1158 get_page(bad_page);
1159 return page_to_pfn(bad_page);
1160 }
1161
1162 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
1163}
1164
1165pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1166{
1167 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
1168}
1169EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1170
1171pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1172 bool write_fault, bool *writable)
1173{
1174 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
1175}
1176EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1177
1178pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1179{
1180 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
1181}
1182EXPORT_SYMBOL_GPL(gfn_to_pfn);
1183
1184pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1185 bool *writable)
1186{
1187 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1188}
1189EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1190
1191pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1192 struct kvm_memory_slot *slot, gfn_t gfn)
1193{
1194 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
1195 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
1196}
1197
1198int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1199 int nr_pages)
1200{
1201 unsigned long addr;
1202 gfn_t entry;
1203
1204 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
1205 if (kvm_is_error_hva(addr))
1206 return -1;
1207
1208 if (entry < nr_pages)
1209 return 0;
1210
1211 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1212}
1213EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1214
1215struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1216{
1217 pfn_t pfn;
1218
1219 pfn = gfn_to_pfn(kvm, gfn);
1220 if (!kvm_is_mmio_pfn(pfn))
1221 return pfn_to_page(pfn);
1222
1223 WARN_ON(kvm_is_mmio_pfn(pfn));
1224
1225 get_page(bad_page);
1226 return bad_page;
1227}
1228
1229EXPORT_SYMBOL_GPL(gfn_to_page);
1230
1231void kvm_release_page_clean(struct page *page)
1232{
1233 kvm_release_pfn_clean(page_to_pfn(page));
1234}
1235EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1236
1237void kvm_release_pfn_clean(pfn_t pfn)
1238{
1239 if (!kvm_is_mmio_pfn(pfn))
1240 put_page(pfn_to_page(pfn));
1241}
1242EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1243
1244void kvm_release_page_dirty(struct page *page)
1245{
1246 kvm_release_pfn_dirty(page_to_pfn(page));
1247}
1248EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1249
1250void kvm_release_pfn_dirty(pfn_t pfn)
1251{
1252 kvm_set_pfn_dirty(pfn);
1253 kvm_release_pfn_clean(pfn);
1254}
1255EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1256
1257void kvm_set_page_dirty(struct page *page)
1258{
1259 kvm_set_pfn_dirty(page_to_pfn(page));
1260}
1261EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1262
1263void kvm_set_pfn_dirty(pfn_t pfn)
1264{
1265 if (!kvm_is_mmio_pfn(pfn)) {
1266 struct page *page = pfn_to_page(pfn);
1267 if (!PageReserved(page))
1268 SetPageDirty(page);
1269 }
1270}
1271EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1272
1273void kvm_set_pfn_accessed(pfn_t pfn)
1274{
1275 if (!kvm_is_mmio_pfn(pfn))
1276 mark_page_accessed(pfn_to_page(pfn));
1277}
1278EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1279
1280void kvm_get_pfn(pfn_t pfn)
1281{
1282 if (!kvm_is_mmio_pfn(pfn))
1283 get_page(pfn_to_page(pfn));
1284}
1285EXPORT_SYMBOL_GPL(kvm_get_pfn);
1286
1287static int next_segment(unsigned long len, int offset)
1288{
1289 if (len > PAGE_SIZE - offset)
1290 return PAGE_SIZE - offset;
1291 else
1292 return len;
1293}
1294
1295int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1296 int len)
1297{
1298 int r;
1299 unsigned long addr;
1300
1301 addr = gfn_to_hva(kvm, gfn);
1302 if (kvm_is_error_hva(addr))
1303 return -EFAULT;
1304 r = __copy_from_user(data, (void __user *)addr + offset, len);
1305 if (r)
1306 return -EFAULT;
1307 return 0;
1308}
1309EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1310
1311int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1312{
1313 gfn_t gfn = gpa >> PAGE_SHIFT;
1314 int seg;
1315 int offset = offset_in_page(gpa);
1316 int ret;
1317
1318 while ((seg = next_segment(len, offset)) != 0) {
1319 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1320 if (ret < 0)
1321 return ret;
1322 offset = 0;
1323 len -= seg;
1324 data += seg;
1325 ++gfn;
1326 }
1327 return 0;
1328}
1329EXPORT_SYMBOL_GPL(kvm_read_guest);
1330
1331int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1332 unsigned long len)
1333{
1334 int r;
1335 unsigned long addr;
1336 gfn_t gfn = gpa >> PAGE_SHIFT;
1337 int offset = offset_in_page(gpa);
1338
1339 addr = gfn_to_hva(kvm, gfn);
1340 if (kvm_is_error_hva(addr))
1341 return -EFAULT;
1342 pagefault_disable();
1343 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1344 pagefault_enable();
1345 if (r)
1346 return -EFAULT;
1347 return 0;
1348}
1349EXPORT_SYMBOL(kvm_read_guest_atomic);
1350
1351int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1352 int offset, int len)
1353{
1354 int r;
1355 unsigned long addr;
1356
1357 addr = gfn_to_hva(kvm, gfn);
1358 if (kvm_is_error_hva(addr))
1359 return -EFAULT;
1360 r = __copy_to_user((void __user *)addr + offset, data, len);
1361 if (r)
1362 return -EFAULT;
1363 mark_page_dirty(kvm, gfn);
1364 return 0;
1365}
1366EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1367
1368int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1369 unsigned long len)
1370{
1371 gfn_t gfn = gpa >> PAGE_SHIFT;
1372 int seg;
1373 int offset = offset_in_page(gpa);
1374 int ret;
1375
1376 while ((seg = next_segment(len, offset)) != 0) {
1377 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1378 if (ret < 0)
1379 return ret;
1380 offset = 0;
1381 len -= seg;
1382 data += seg;
1383 ++gfn;
1384 }
1385 return 0;
1386}
1387
1388int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1389 gpa_t gpa, unsigned long len)
1390{
1391 struct kvm_memslots *slots = kvm_memslots(kvm);
1392 int offset = offset_in_page(gpa);
1393 gfn_t start_gfn = gpa >> PAGE_SHIFT;
1394 gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
1395 gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
1396 gfn_t nr_pages_avail;
1397
1398 ghc->gpa = gpa;
1399 ghc->generation = slots->generation;
1400 ghc->len = len;
1401 ghc->memslot = gfn_to_memslot(kvm, start_gfn);
1402 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, &nr_pages_avail);
1403 if (!kvm_is_error_hva(ghc->hva) && nr_pages_avail >= nr_pages_needed) {
1404 ghc->hva += offset;
1405 } else {
1406 /*
1407 * If the requested region crosses two memslots, we still
1408 * verify that the entire region is valid here.
1409 */
1410 while (start_gfn <= end_gfn) {
1411 ghc->memslot = gfn_to_memslot(kvm, start_gfn);
1412 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
1413 &nr_pages_avail);
1414 if (kvm_is_error_hva(ghc->hva))
1415 return -EFAULT;
1416 start_gfn += nr_pages_avail;
1417 }
1418 /* Use the slow path for cross page reads and writes. */
1419 ghc->memslot = NULL;
1420 }
1421 return 0;
1422}
1423EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1424
1425int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1426 void *data, unsigned long len)
1427{
1428 struct kvm_memslots *slots = kvm_memslots(kvm);
1429 int r;
1430
1431 BUG_ON(len > ghc->len);
1432
1433 if (slots->generation != ghc->generation)
1434 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
1435
1436 if (unlikely(!ghc->memslot))
1437 return kvm_write_guest(kvm, ghc->gpa, data, len);
1438
1439 if (kvm_is_error_hva(ghc->hva))
1440 return -EFAULT;
1441
1442 r = __copy_to_user((void __user *)ghc->hva, data, len);
1443 if (r)
1444 return -EFAULT;
1445 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1446
1447 return 0;
1448}
1449EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1450
1451int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1452 void *data, unsigned long len)
1453{
1454 struct kvm_memslots *slots = kvm_memslots(kvm);
1455 int r;
1456
1457 BUG_ON(len > ghc->len);
1458
1459 if (slots->generation != ghc->generation)
1460 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
1461
1462 if (unlikely(!ghc->memslot))
1463 return kvm_read_guest(kvm, ghc->gpa, data, len);
1464
1465 if (kvm_is_error_hva(ghc->hva))
1466 return -EFAULT;
1467
1468 r = __copy_from_user(data, (void __user *)ghc->hva, len);
1469 if (r)
1470 return -EFAULT;
1471
1472 return 0;
1473}
1474EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
1475
1476int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1477{
1478 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1479 offset, len);
1480}
1481EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1482
1483int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1484{
1485 gfn_t gfn = gpa >> PAGE_SHIFT;
1486 int seg;
1487 int offset = offset_in_page(gpa);
1488 int ret;
1489
1490 while ((seg = next_segment(len, offset)) != 0) {
1491 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1492 if (ret < 0)
1493 return ret;
1494 offset = 0;
1495 len -= seg;
1496 ++gfn;
1497 }
1498 return 0;
1499}
1500EXPORT_SYMBOL_GPL(kvm_clear_guest);
1501
1502void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1503 gfn_t gfn)
1504{
1505 if (memslot && memslot->dirty_bitmap) {
1506 unsigned long rel_gfn = gfn - memslot->base_gfn;
1507
1508 if (!test_and_set_bit_le(rel_gfn, memslot->dirty_bitmap))
1509 memslot->nr_dirty_pages++;
1510 }
1511}
1512
1513void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1514{
1515 struct kvm_memory_slot *memslot;
1516
1517 memslot = gfn_to_memslot(kvm, gfn);
1518 mark_page_dirty_in_slot(kvm, memslot, gfn);
1519}
1520
1521/*
1522 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1523 */
1524void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1525{
1526 DEFINE_WAIT(wait);
1527
1528 for (;;) {
1529 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1530
1531 if (kvm_arch_vcpu_runnable(vcpu)) {
1532 kvm_make_request(KVM_REQ_UNHALT, vcpu);
1533 break;
1534 }
1535 if (kvm_cpu_has_pending_timer(vcpu))
1536 break;
1537 if (signal_pending(current))
1538 break;
1539
1540 schedule();
1541 }
1542
1543 finish_wait(&vcpu->wq, &wait);
1544}
1545
1546void kvm_resched(struct kvm_vcpu *vcpu)
1547{
1548 if (!need_resched())
1549 return;
1550 cond_resched();
1551}
1552EXPORT_SYMBOL_GPL(kvm_resched);
1553
1554void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1555{
1556 struct kvm *kvm = me->kvm;
1557 struct kvm_vcpu *vcpu;
1558 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1559 int yielded = 0;
1560 int pass;
1561 int i;
1562
1563 /*
1564 * We boost the priority of a VCPU that is runnable but not
1565 * currently running, because it got preempted by something
1566 * else and called schedule in __vcpu_run. Hopefully that
1567 * VCPU is holding the lock that we need and will release it.
1568 * We approximate round-robin by starting at the last boosted VCPU.
1569 */
1570 for (pass = 0; pass < 2 && !yielded; pass++) {
1571 kvm_for_each_vcpu(i, vcpu, kvm) {
1572 struct task_struct *task = NULL;
1573 struct pid *pid;
1574 if (!pass && i < last_boosted_vcpu) {
1575 i = last_boosted_vcpu;
1576 continue;
1577 } else if (pass && i > last_boosted_vcpu)
1578 break;
1579 if (vcpu == me)
1580 continue;
1581 if (waitqueue_active(&vcpu->wq))
1582 continue;
1583 rcu_read_lock();
1584 pid = rcu_dereference(vcpu->pid);
1585 if (pid)
1586 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1587 rcu_read_unlock();
1588 if (!task)
1589 continue;
1590 if (task->flags & PF_VCPU) {
1591 put_task_struct(task);
1592 continue;
1593 }
1594 if (yield_to(task, 1)) {
1595 put_task_struct(task);
1596 kvm->last_boosted_vcpu = i;
1597 yielded = 1;
1598 break;
1599 }
1600 put_task_struct(task);
1601 }
1602 }
1603}
1604EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1605
1606static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1607{
1608 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1609 struct page *page;
1610
1611 if (vmf->pgoff == 0)
1612 page = virt_to_page(vcpu->run);
1613#ifdef CONFIG_X86
1614 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1615 page = virt_to_page(vcpu->arch.pio_data);
1616#endif
1617#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1618 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1619 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1620#endif
1621 else
1622 return kvm_arch_vcpu_fault(vcpu, vmf);
1623 get_page(page);
1624 vmf->page = page;
1625 return 0;
1626}
1627
1628static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1629 .fault = kvm_vcpu_fault,
1630};
1631
1632static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1633{
1634 vma->vm_ops = &kvm_vcpu_vm_ops;
1635 return 0;
1636}
1637
1638static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1639{
1640 struct kvm_vcpu *vcpu = filp->private_data;
1641
1642 kvm_put_kvm(vcpu->kvm);
1643 return 0;
1644}
1645
1646static struct file_operations kvm_vcpu_fops = {
1647 .release = kvm_vcpu_release,
1648 .unlocked_ioctl = kvm_vcpu_ioctl,
1649#ifdef CONFIG_COMPAT
1650 .compat_ioctl = kvm_vcpu_compat_ioctl,
1651#endif
1652 .mmap = kvm_vcpu_mmap,
1653 .llseek = noop_llseek,
1654};
1655
1656/*
1657 * Allocates an inode for the vcpu.
1658 */
1659static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1660{
1661 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
1662}
1663
1664/*
1665 * Creates some virtual cpus. Good luck creating more than one.
1666 */
1667static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1668{
1669 int r;
1670 struct kvm_vcpu *vcpu, *v;
1671
1672 if (id >= KVM_MAX_VCPUS)
1673 return -EINVAL;
1674
1675 vcpu = kvm_arch_vcpu_create(kvm, id);
1676 if (IS_ERR(vcpu))
1677 return PTR_ERR(vcpu);
1678
1679 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1680
1681 r = kvm_arch_vcpu_setup(vcpu);
1682 if (r)
1683 goto vcpu_destroy;
1684
1685 mutex_lock(&kvm->lock);
1686 if (!kvm_vcpu_compatible(vcpu)) {
1687 r = -EINVAL;
1688 goto unlock_vcpu_destroy;
1689 }
1690 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1691 r = -EINVAL;
1692 goto unlock_vcpu_destroy;
1693 }
1694
1695 kvm_for_each_vcpu(r, v, kvm)
1696 if (v->vcpu_id == id) {
1697 r = -EEXIST;
1698 goto unlock_vcpu_destroy;
1699 }
1700
1701 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1702
1703 /* Now it's all set up, let userspace reach it */
1704 kvm_get_kvm(kvm);
1705 r = create_vcpu_fd(vcpu);
1706 if (r < 0) {
1707 kvm_put_kvm(kvm);
1708 goto unlock_vcpu_destroy;
1709 }
1710
1711 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1712 smp_wmb();
1713 atomic_inc(&kvm->online_vcpus);
1714
1715 mutex_unlock(&kvm->lock);
1716 return r;
1717
1718unlock_vcpu_destroy:
1719 mutex_unlock(&kvm->lock);
1720vcpu_destroy:
1721 kvm_arch_vcpu_destroy(vcpu);
1722 return r;
1723}
1724
1725static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1726{
1727 if (sigset) {
1728 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1729 vcpu->sigset_active = 1;
1730 vcpu->sigset = *sigset;
1731 } else
1732 vcpu->sigset_active = 0;
1733 return 0;
1734}
1735
1736static long kvm_vcpu_ioctl(struct file *filp,
1737 unsigned int ioctl, unsigned long arg)
1738{
1739 struct kvm_vcpu *vcpu = filp->private_data;
1740 void __user *argp = (void __user *)arg;
1741 int r;
1742 struct kvm_fpu *fpu = NULL;
1743 struct kvm_sregs *kvm_sregs = NULL;
1744
1745 if (vcpu->kvm->mm != current->mm)
1746 return -EIO;
1747
1748 if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
1749 return -EINVAL;
1750
1751#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1752 /*
1753 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1754 * so vcpu_load() would break it.
1755 */
1756 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1757 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1758#endif
1759
1760
1761 vcpu_load(vcpu);
1762 switch (ioctl) {
1763 case KVM_RUN:
1764 r = -EINVAL;
1765 if (arg)
1766 goto out;
1767 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1768 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
1769 break;
1770 case KVM_GET_REGS: {
1771 struct kvm_regs *kvm_regs;
1772
1773 r = -ENOMEM;
1774 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1775 if (!kvm_regs)
1776 goto out;
1777 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1778 if (r)
1779 goto out_free1;
1780 r = -EFAULT;
1781 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1782 goto out_free1;
1783 r = 0;
1784out_free1:
1785 kfree(kvm_regs);
1786 break;
1787 }
1788 case KVM_SET_REGS: {
1789 struct kvm_regs *kvm_regs;
1790
1791 r = -ENOMEM;
1792 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
1793 if (IS_ERR(kvm_regs)) {
1794 r = PTR_ERR(kvm_regs);
1795 goto out;
1796 }
1797 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1798 if (r)
1799 goto out_free2;
1800 r = 0;
1801out_free2:
1802 kfree(kvm_regs);
1803 break;
1804 }
1805 case KVM_GET_SREGS: {
1806 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1807 r = -ENOMEM;
1808 if (!kvm_sregs)
1809 goto out;
1810 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1811 if (r)
1812 goto out;
1813 r = -EFAULT;
1814 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1815 goto out;
1816 r = 0;
1817 break;
1818 }
1819 case KVM_SET_SREGS: {
1820 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
1821 if (IS_ERR(kvm_sregs)) {
1822 r = PTR_ERR(kvm_sregs);
1823 goto out;
1824 }
1825 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1826 if (r)
1827 goto out;
1828 r = 0;
1829 break;
1830 }
1831 case KVM_GET_MP_STATE: {
1832 struct kvm_mp_state mp_state;
1833
1834 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1835 if (r)
1836 goto out;
1837 r = -EFAULT;
1838 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1839 goto out;
1840 r = 0;
1841 break;
1842 }
1843 case KVM_SET_MP_STATE: {
1844 struct kvm_mp_state mp_state;
1845
1846 r = -EFAULT;
1847 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1848 goto out;
1849 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1850 if (r)
1851 goto out;
1852 r = 0;
1853 break;
1854 }
1855 case KVM_TRANSLATE: {
1856 struct kvm_translation tr;
1857
1858 r = -EFAULT;
1859 if (copy_from_user(&tr, argp, sizeof tr))
1860 goto out;
1861 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1862 if (r)
1863 goto out;
1864 r = -EFAULT;
1865 if (copy_to_user(argp, &tr, sizeof tr))
1866 goto out;
1867 r = 0;
1868 break;
1869 }
1870 case KVM_SET_GUEST_DEBUG: {
1871 struct kvm_guest_debug dbg;
1872
1873 r = -EFAULT;
1874 if (copy_from_user(&dbg, argp, sizeof dbg))
1875 goto out;
1876 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1877 if (r)
1878 goto out;
1879 r = 0;
1880 break;
1881 }
1882 case KVM_SET_SIGNAL_MASK: {
1883 struct kvm_signal_mask __user *sigmask_arg = argp;
1884 struct kvm_signal_mask kvm_sigmask;
1885 sigset_t sigset, *p;
1886
1887 p = NULL;
1888 if (argp) {
1889 r = -EFAULT;
1890 if (copy_from_user(&kvm_sigmask, argp,
1891 sizeof kvm_sigmask))
1892 goto out;
1893 r = -EINVAL;
1894 if (kvm_sigmask.len != sizeof sigset)
1895 goto out;
1896 r = -EFAULT;
1897 if (copy_from_user(&sigset, sigmask_arg->sigset,
1898 sizeof sigset))
1899 goto out;
1900 p = &sigset;
1901 }
1902 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
1903 break;
1904 }
1905 case KVM_GET_FPU: {
1906 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1907 r = -ENOMEM;
1908 if (!fpu)
1909 goto out;
1910 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1911 if (r)
1912 goto out;
1913 r = -EFAULT;
1914 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1915 goto out;
1916 r = 0;
1917 break;
1918 }
1919 case KVM_SET_FPU: {
1920 fpu = memdup_user(argp, sizeof(*fpu));
1921 if (IS_ERR(fpu)) {
1922 r = PTR_ERR(fpu);
1923 goto out;
1924 }
1925 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1926 if (r)
1927 goto out;
1928 r = 0;
1929 break;
1930 }
1931 default:
1932 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1933 }
1934out:
1935 vcpu_put(vcpu);
1936 kfree(fpu);
1937 kfree(kvm_sregs);
1938 return r;
1939}
1940
1941#ifdef CONFIG_COMPAT
1942static long kvm_vcpu_compat_ioctl(struct file *filp,
1943 unsigned int ioctl, unsigned long arg)
1944{
1945 struct kvm_vcpu *vcpu = filp->private_data;
1946 void __user *argp = compat_ptr(arg);
1947 int r;
1948
1949 if (vcpu->kvm->mm != current->mm)
1950 return -EIO;
1951
1952 switch (ioctl) {
1953 case KVM_SET_SIGNAL_MASK: {
1954 struct kvm_signal_mask __user *sigmask_arg = argp;
1955 struct kvm_signal_mask kvm_sigmask;
1956 compat_sigset_t csigset;
1957 sigset_t sigset;
1958
1959 if (argp) {
1960 r = -EFAULT;
1961 if (copy_from_user(&kvm_sigmask, argp,
1962 sizeof kvm_sigmask))
1963 goto out;
1964 r = -EINVAL;
1965 if (kvm_sigmask.len != sizeof csigset)
1966 goto out;
1967 r = -EFAULT;
1968 if (copy_from_user(&csigset, sigmask_arg->sigset,
1969 sizeof csigset))
1970 goto out;
1971 }
1972 sigset_from_compat(&sigset, &csigset);
1973 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1974 break;
1975 }
1976 default:
1977 r = kvm_vcpu_ioctl(filp, ioctl, arg);
1978 }
1979
1980out:
1981 return r;
1982}
1983#endif
1984
1985static long kvm_vm_ioctl(struct file *filp,
1986 unsigned int ioctl, unsigned long arg)
1987{
1988 struct kvm *kvm = filp->private_data;
1989 void __user *argp = (void __user *)arg;
1990 int r;
1991
1992 if (kvm->mm != current->mm)
1993 return -EIO;
1994 switch (ioctl) {
1995 case KVM_CREATE_VCPU:
1996 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1997 if (r < 0)
1998 goto out;
1999 break;
2000 case KVM_SET_USER_MEMORY_REGION: {
2001 struct kvm_userspace_memory_region kvm_userspace_mem;
2002
2003 r = -EFAULT;
2004 if (copy_from_user(&kvm_userspace_mem, argp,
2005 sizeof kvm_userspace_mem))
2006 goto out;
2007
2008 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
2009 if (r)
2010 goto out;
2011 break;
2012 }
2013 case KVM_GET_DIRTY_LOG: {
2014 struct kvm_dirty_log log;
2015
2016 r = -EFAULT;
2017 if (copy_from_user(&log, argp, sizeof log))
2018 goto out;
2019 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2020 if (r)
2021 goto out;
2022 break;
2023 }
2024#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2025 case KVM_REGISTER_COALESCED_MMIO: {
2026 struct kvm_coalesced_mmio_zone zone;
2027 r = -EFAULT;
2028 if (copy_from_user(&zone, argp, sizeof zone))
2029 goto out;
2030 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2031 if (r)
2032 goto out;
2033 r = 0;
2034 break;
2035 }
2036 case KVM_UNREGISTER_COALESCED_MMIO: {
2037 struct kvm_coalesced_mmio_zone zone;
2038 r = -EFAULT;
2039 if (copy_from_user(&zone, argp, sizeof zone))
2040 goto out;
2041 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2042 if (r)
2043 goto out;
2044 r = 0;
2045 break;
2046 }
2047#endif
2048 case KVM_IRQFD: {
2049 struct kvm_irqfd data;
2050
2051 r = -EFAULT;
2052 if (copy_from_user(&data, argp, sizeof data))
2053 goto out;
2054 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2055 break;
2056 }
2057 case KVM_IOEVENTFD: {
2058 struct kvm_ioeventfd data;
2059
2060 r = -EFAULT;
2061 if (copy_from_user(&data, argp, sizeof data))
2062 goto out;
2063 r = kvm_ioeventfd(kvm, &data);
2064 break;
2065 }
2066#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2067 case KVM_SET_BOOT_CPU_ID:
2068 r = 0;
2069 mutex_lock(&kvm->lock);
2070 if (atomic_read(&kvm->online_vcpus) != 0)
2071 r = -EBUSY;
2072 else
2073 kvm->bsp_vcpu_id = arg;
2074 mutex_unlock(&kvm->lock);
2075 break;
2076#endif
2077 default:
2078 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2079 if (r == -ENOTTY)
2080 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
2081 }
2082out:
2083 return r;
2084}
2085
2086#ifdef CONFIG_COMPAT
2087struct compat_kvm_dirty_log {
2088 __u32 slot;
2089 __u32 padding1;
2090 union {
2091 compat_uptr_t dirty_bitmap; /* one bit per page */
2092 __u64 padding2;
2093 };
2094};
2095
2096static long kvm_vm_compat_ioctl(struct file *filp,
2097 unsigned int ioctl, unsigned long arg)
2098{
2099 struct kvm *kvm = filp->private_data;
2100 int r;
2101
2102 if (kvm->mm != current->mm)
2103 return -EIO;
2104 switch (ioctl) {
2105 case KVM_GET_DIRTY_LOG: {
2106 struct compat_kvm_dirty_log compat_log;
2107 struct kvm_dirty_log log;
2108
2109 r = -EFAULT;
2110 if (copy_from_user(&compat_log, (void __user *)arg,
2111 sizeof(compat_log)))
2112 goto out;
2113 log.slot = compat_log.slot;
2114 log.padding1 = compat_log.padding1;
2115 log.padding2 = compat_log.padding2;
2116 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2117
2118 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2119 if (r)
2120 goto out;
2121 break;
2122 }
2123 default:
2124 r = kvm_vm_ioctl(filp, ioctl, arg);
2125 }
2126
2127out:
2128 return r;
2129}
2130#endif
2131
2132static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2133{
2134 struct page *page[1];
2135 unsigned long addr;
2136 int npages;
2137 gfn_t gfn = vmf->pgoff;
2138 struct kvm *kvm = vma->vm_file->private_data;
2139
2140 addr = gfn_to_hva(kvm, gfn);
2141 if (kvm_is_error_hva(addr))
2142 return VM_FAULT_SIGBUS;
2143
2144 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2145 NULL);
2146 if (unlikely(npages != 1))
2147 return VM_FAULT_SIGBUS;
2148
2149 vmf->page = page[0];
2150 return 0;
2151}
2152
2153static const struct vm_operations_struct kvm_vm_vm_ops = {
2154 .fault = kvm_vm_fault,
2155};
2156
2157static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2158{
2159 vma->vm_ops = &kvm_vm_vm_ops;
2160 return 0;
2161}
2162
2163static struct file_operations kvm_vm_fops = {
2164 .release = kvm_vm_release,
2165 .unlocked_ioctl = kvm_vm_ioctl,
2166#ifdef CONFIG_COMPAT
2167 .compat_ioctl = kvm_vm_compat_ioctl,
2168#endif
2169 .mmap = kvm_vm_mmap,
2170 .llseek = noop_llseek,
2171};
2172
2173static int kvm_dev_ioctl_create_vm(unsigned long type)
2174{
2175 int r;
2176 struct kvm *kvm;
2177
2178 kvm = kvm_create_vm(type);
2179 if (IS_ERR(kvm))
2180 return PTR_ERR(kvm);
2181#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2182 r = kvm_coalesced_mmio_init(kvm);
2183 if (r < 0) {
2184 kvm_put_kvm(kvm);
2185 return r;
2186 }
2187#endif
2188 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2189 if (r < 0)
2190 kvm_put_kvm(kvm);
2191
2192 return r;
2193}
2194
2195static long kvm_dev_ioctl_check_extension_generic(long arg)
2196{
2197 switch (arg) {
2198 case KVM_CAP_USER_MEMORY:
2199 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2200 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2201#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2202 case KVM_CAP_SET_BOOT_CPU_ID:
2203#endif
2204 case KVM_CAP_INTERNAL_ERROR_DATA:
2205 return 1;
2206#ifdef CONFIG_HAVE_KVM_IRQCHIP
2207 case KVM_CAP_IRQ_ROUTING:
2208 return KVM_MAX_IRQ_ROUTES;
2209#endif
2210 default:
2211 break;
2212 }
2213 return kvm_dev_ioctl_check_extension(arg);
2214}
2215
2216static long kvm_dev_ioctl(struct file *filp,
2217 unsigned int ioctl, unsigned long arg)
2218{
2219 long r = -EINVAL;
2220
2221 switch (ioctl) {
2222 case KVM_GET_API_VERSION:
2223 r = -EINVAL;
2224 if (arg)
2225 goto out;
2226 r = KVM_API_VERSION;
2227 break;
2228 case KVM_CREATE_VM:
2229 r = kvm_dev_ioctl_create_vm(arg);
2230 break;
2231 case KVM_CHECK_EXTENSION:
2232 r = kvm_dev_ioctl_check_extension_generic(arg);
2233 break;
2234 case KVM_GET_VCPU_MMAP_SIZE:
2235 r = -EINVAL;
2236 if (arg)
2237 goto out;
2238 r = PAGE_SIZE; /* struct kvm_run */
2239#ifdef CONFIG_X86
2240 r += PAGE_SIZE; /* pio data page */
2241#endif
2242#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2243 r += PAGE_SIZE; /* coalesced mmio ring page */
2244#endif
2245 break;
2246 case KVM_TRACE_ENABLE:
2247 case KVM_TRACE_PAUSE:
2248 case KVM_TRACE_DISABLE:
2249 r = -EOPNOTSUPP;
2250 break;
2251 default:
2252 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2253 }
2254out:
2255 return r;
2256}
2257
2258static struct file_operations kvm_chardev_ops = {
2259 .unlocked_ioctl = kvm_dev_ioctl,
2260 .compat_ioctl = kvm_dev_ioctl,
2261 .llseek = noop_llseek,
2262};
2263
2264static struct miscdevice kvm_dev = {
2265 KVM_MINOR,
2266 "kvm",
2267 &kvm_chardev_ops,
2268};
2269
2270static void hardware_enable_nolock(void *junk)
2271{
2272 int cpu = raw_smp_processor_id();
2273 int r;
2274
2275 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2276 return;
2277
2278 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2279
2280 r = kvm_arch_hardware_enable(NULL);
2281
2282 if (r) {
2283 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2284 atomic_inc(&hardware_enable_failed);
2285 printk(KERN_INFO "kvm: enabling virtualization on "
2286 "CPU%d failed\n", cpu);
2287 }
2288}
2289
2290static void hardware_enable(void *junk)
2291{
2292 raw_spin_lock(&kvm_lock);
2293 hardware_enable_nolock(junk);
2294 raw_spin_unlock(&kvm_lock);
2295}
2296
2297static void hardware_disable_nolock(void *junk)
2298{
2299 int cpu = raw_smp_processor_id();
2300
2301 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2302 return;
2303 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2304 kvm_arch_hardware_disable(NULL);
2305}
2306
2307static void hardware_disable(void *junk)
2308{
2309 raw_spin_lock(&kvm_lock);
2310 hardware_disable_nolock(junk);
2311 raw_spin_unlock(&kvm_lock);
2312}
2313
2314static void hardware_disable_all_nolock(void)
2315{
2316 BUG_ON(!kvm_usage_count);
2317
2318 kvm_usage_count--;
2319 if (!kvm_usage_count)
2320 on_each_cpu(hardware_disable_nolock, NULL, 1);
2321}
2322
2323static void hardware_disable_all(void)
2324{
2325 raw_spin_lock(&kvm_lock);
2326 hardware_disable_all_nolock();
2327 raw_spin_unlock(&kvm_lock);
2328}
2329
2330static int hardware_enable_all(void)
2331{
2332 int r = 0;
2333
2334 raw_spin_lock(&kvm_lock);
2335
2336 kvm_usage_count++;
2337 if (kvm_usage_count == 1) {
2338 atomic_set(&hardware_enable_failed, 0);
2339 on_each_cpu(hardware_enable_nolock, NULL, 1);
2340
2341 if (atomic_read(&hardware_enable_failed)) {
2342 hardware_disable_all_nolock();
2343 r = -EBUSY;
2344 }
2345 }
2346
2347 raw_spin_unlock(&kvm_lock);
2348
2349 return r;
2350}
2351
2352static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2353 void *v)
2354{
2355 int cpu = (long)v;
2356
2357 if (!kvm_usage_count)
2358 return NOTIFY_OK;
2359
2360 val &= ~CPU_TASKS_FROZEN;
2361 switch (val) {
2362 case CPU_DYING:
2363 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2364 cpu);
2365 hardware_disable(NULL);
2366 break;
2367 case CPU_STARTING:
2368 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2369 cpu);
2370 hardware_enable(NULL);
2371 break;
2372 }
2373 return NOTIFY_OK;
2374}
2375
2376
2377asmlinkage void kvm_spurious_fault(void)
2378{
2379 /* Fault while not rebooting. We want the trace. */
2380 BUG();
2381}
2382EXPORT_SYMBOL_GPL(kvm_spurious_fault);
2383
2384static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2385 void *v)
2386{
2387 /*
2388 * Some (well, at least mine) BIOSes hang on reboot if
2389 * in vmx root mode.
2390 *
2391 * And Intel TXT required VMX off for all cpu when system shutdown.
2392 */
2393 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2394 kvm_rebooting = true;
2395 on_each_cpu(hardware_disable_nolock, NULL, 1);
2396 return NOTIFY_OK;
2397}
2398
2399static struct notifier_block kvm_reboot_notifier = {
2400 .notifier_call = kvm_reboot,
2401 .priority = 0,
2402};
2403
2404static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2405{
2406 int i;
2407
2408 for (i = 0; i < bus->dev_count; i++) {
2409 struct kvm_io_device *pos = bus->range[i].dev;
2410
2411 kvm_iodevice_destructor(pos);
2412 }
2413 kfree(bus);
2414}
2415
2416int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
2417{
2418 const struct kvm_io_range *r1 = p1;
2419 const struct kvm_io_range *r2 = p2;
2420
2421 if (r1->addr < r2->addr)
2422 return -1;
2423 if (r1->addr + r1->len > r2->addr + r2->len)
2424 return 1;
2425 return 0;
2426}
2427
2428int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
2429 gpa_t addr, int len)
2430{
2431 if (bus->dev_count == NR_IOBUS_DEVS)
2432 return -ENOSPC;
2433
2434 bus->range[bus->dev_count++] = (struct kvm_io_range) {
2435 .addr = addr,
2436 .len = len,
2437 .dev = dev,
2438 };
2439
2440 sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
2441 kvm_io_bus_sort_cmp, NULL);
2442
2443 return 0;
2444}
2445
2446int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
2447 gpa_t addr, int len)
2448{
2449 struct kvm_io_range *range, key;
2450 int off;
2451
2452 key = (struct kvm_io_range) {
2453 .addr = addr,
2454 .len = len,
2455 };
2456
2457 range = bsearch(&key, bus->range, bus->dev_count,
2458 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
2459 if (range == NULL)
2460 return -ENOENT;
2461
2462 off = range - bus->range;
2463
2464 while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
2465 off--;
2466
2467 return off;
2468}
2469
2470/* kvm_io_bus_write - called under kvm->slots_lock */
2471int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2472 int len, const void *val)
2473{
2474 int idx;
2475 struct kvm_io_bus *bus;
2476 struct kvm_io_range range;
2477
2478 range = (struct kvm_io_range) {
2479 .addr = addr,
2480 .len = len,
2481 };
2482
2483 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2484 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2485 if (idx < 0)
2486 return -EOPNOTSUPP;
2487
2488 while (idx < bus->dev_count &&
2489 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2490 if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
2491 return 0;
2492 idx++;
2493 }
2494
2495 return -EOPNOTSUPP;
2496}
2497
2498/* kvm_io_bus_read - called under kvm->slots_lock */
2499int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2500 int len, void *val)
2501{
2502 int idx;
2503 struct kvm_io_bus *bus;
2504 struct kvm_io_range range;
2505
2506 range = (struct kvm_io_range) {
2507 .addr = addr,
2508 .len = len,
2509 };
2510
2511 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2512 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2513 if (idx < 0)
2514 return -EOPNOTSUPP;
2515
2516 while (idx < bus->dev_count &&
2517 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2518 if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
2519 return 0;
2520 idx++;
2521 }
2522
2523 return -EOPNOTSUPP;
2524}
2525
2526/* Caller must hold slots_lock. */
2527int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2528 int len, struct kvm_io_device *dev)
2529{
2530 struct kvm_io_bus *new_bus, *bus;
2531
2532 bus = kvm->buses[bus_idx];
2533 if (bus->dev_count > NR_IOBUS_DEVS-1)
2534 return -ENOSPC;
2535
2536 new_bus = kmemdup(bus, sizeof(struct kvm_io_bus), GFP_KERNEL);
2537 if (!new_bus)
2538 return -ENOMEM;
2539 kvm_io_bus_insert_dev(new_bus, dev, addr, len);
2540 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2541 synchronize_srcu_expedited(&kvm->srcu);
2542 kfree(bus);
2543
2544 return 0;
2545}
2546
2547/* Caller must hold slots_lock. */
2548int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2549 struct kvm_io_device *dev)
2550{
2551 int i, r;
2552 struct kvm_io_bus *new_bus, *bus;
2553
2554 bus = kvm->buses[bus_idx];
2555
2556 new_bus = kmemdup(bus, sizeof(*bus), GFP_KERNEL);
2557 if (!new_bus)
2558 return -ENOMEM;
2559
2560 r = -ENOENT;
2561 for (i = 0; i < new_bus->dev_count; i++)
2562 if (new_bus->range[i].dev == dev) {
2563 r = 0;
2564 new_bus->dev_count--;
2565 new_bus->range[i] = new_bus->range[new_bus->dev_count];
2566 sort(new_bus->range, new_bus->dev_count,
2567 sizeof(struct kvm_io_range),
2568 kvm_io_bus_sort_cmp, NULL);
2569 break;
2570 }
2571
2572 if (r) {
2573 kfree(new_bus);
2574 return r;
2575 }
2576
2577 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2578 synchronize_srcu_expedited(&kvm->srcu);
2579 kfree(bus);
2580 return r;
2581}
2582
2583static struct notifier_block kvm_cpu_notifier = {
2584 .notifier_call = kvm_cpu_hotplug,
2585};
2586
2587static int vm_stat_get(void *_offset, u64 *val)
2588{
2589 unsigned offset = (long)_offset;
2590 struct kvm *kvm;
2591
2592 *val = 0;
2593 raw_spin_lock(&kvm_lock);
2594 list_for_each_entry(kvm, &vm_list, vm_list)
2595 *val += *(u32 *)((void *)kvm + offset);
2596 raw_spin_unlock(&kvm_lock);
2597 return 0;
2598}
2599
2600DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2601
2602static int vcpu_stat_get(void *_offset, u64 *val)
2603{
2604 unsigned offset = (long)_offset;
2605 struct kvm *kvm;
2606 struct kvm_vcpu *vcpu;
2607 int i;
2608
2609 *val = 0;
2610 raw_spin_lock(&kvm_lock);
2611 list_for_each_entry(kvm, &vm_list, vm_list)
2612 kvm_for_each_vcpu(i, vcpu, kvm)
2613 *val += *(u32 *)((void *)vcpu + offset);
2614
2615 raw_spin_unlock(&kvm_lock);
2616 return 0;
2617}
2618
2619DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2620
2621static const struct file_operations *stat_fops[] = {
2622 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2623 [KVM_STAT_VM] = &vm_stat_fops,
2624};
2625
2626static int kvm_init_debug(void)
2627{
2628 int r = -EFAULT;
2629 struct kvm_stats_debugfs_item *p;
2630
2631 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2632 if (kvm_debugfs_dir == NULL)
2633 goto out;
2634
2635 for (p = debugfs_entries; p->name; ++p) {
2636 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2637 (void *)(long)p->offset,
2638 stat_fops[p->kind]);
2639 if (p->dentry == NULL)
2640 goto out_dir;
2641 }
2642
2643 return 0;
2644
2645out_dir:
2646 debugfs_remove_recursive(kvm_debugfs_dir);
2647out:
2648 return r;
2649}
2650
2651static void kvm_exit_debug(void)
2652{
2653 struct kvm_stats_debugfs_item *p;
2654
2655 for (p = debugfs_entries; p->name; ++p)
2656 debugfs_remove(p->dentry);
2657 debugfs_remove(kvm_debugfs_dir);
2658}
2659
2660static int kvm_suspend(void)
2661{
2662 if (kvm_usage_count)
2663 hardware_disable_nolock(NULL);
2664 return 0;
2665}
2666
2667static void kvm_resume(void)
2668{
2669 if (kvm_usage_count) {
2670 WARN_ON(raw_spin_is_locked(&kvm_lock));
2671 hardware_enable_nolock(NULL);
2672 }
2673}
2674
2675static struct syscore_ops kvm_syscore_ops = {
2676 .suspend = kvm_suspend,
2677 .resume = kvm_resume,
2678};
2679
2680struct page *bad_page;
2681pfn_t bad_pfn;
2682
2683static inline
2684struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2685{
2686 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2687}
2688
2689static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2690{
2691 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2692
2693 kvm_arch_vcpu_load(vcpu, cpu);
2694}
2695
2696static void kvm_sched_out(struct preempt_notifier *pn,
2697 struct task_struct *next)
2698{
2699 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2700
2701 kvm_arch_vcpu_put(vcpu);
2702}
2703
2704int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
2705 struct module *module)
2706{
2707 int r;
2708 int cpu;
2709
2710 r = kvm_arch_init(opaque);
2711 if (r)
2712 goto out_fail;
2713
2714 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2715
2716 if (bad_page == NULL) {
2717 r = -ENOMEM;
2718 goto out;
2719 }
2720
2721 bad_pfn = page_to_pfn(bad_page);
2722
2723 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2724
2725 if (hwpoison_page == NULL) {
2726 r = -ENOMEM;
2727 goto out_free_0;
2728 }
2729
2730 hwpoison_pfn = page_to_pfn(hwpoison_page);
2731
2732 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2733
2734 if (fault_page == NULL) {
2735 r = -ENOMEM;
2736 goto out_free_0;
2737 }
2738
2739 fault_pfn = page_to_pfn(fault_page);
2740
2741 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2742 r = -ENOMEM;
2743 goto out_free_0;
2744 }
2745
2746 r = kvm_arch_hardware_setup();
2747 if (r < 0)
2748 goto out_free_0a;
2749
2750 for_each_online_cpu(cpu) {
2751 smp_call_function_single(cpu,
2752 kvm_arch_check_processor_compat,
2753 &r, 1);
2754 if (r < 0)
2755 goto out_free_1;
2756 }
2757
2758 r = register_cpu_notifier(&kvm_cpu_notifier);
2759 if (r)
2760 goto out_free_2;
2761 register_reboot_notifier(&kvm_reboot_notifier);
2762
2763 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2764 if (!vcpu_align)
2765 vcpu_align = __alignof__(struct kvm_vcpu);
2766 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
2767 0, NULL);
2768 if (!kvm_vcpu_cache) {
2769 r = -ENOMEM;
2770 goto out_free_3;
2771 }
2772
2773 r = kvm_async_pf_init();
2774 if (r)
2775 goto out_free;
2776
2777 kvm_chardev_ops.owner = module;
2778 kvm_vm_fops.owner = module;
2779 kvm_vcpu_fops.owner = module;
2780
2781 r = misc_register(&kvm_dev);
2782 if (r) {
2783 printk(KERN_ERR "kvm: misc device register failed\n");
2784 goto out_unreg;
2785 }
2786
2787 register_syscore_ops(&kvm_syscore_ops);
2788
2789 kvm_preempt_ops.sched_in = kvm_sched_in;
2790 kvm_preempt_ops.sched_out = kvm_sched_out;
2791
2792 r = kvm_init_debug();
2793 if (r) {
2794 printk(KERN_ERR "kvm: create debugfs files failed\n");
2795 goto out_undebugfs;
2796 }
2797
2798 return 0;
2799
2800out_undebugfs:
2801 unregister_syscore_ops(&kvm_syscore_ops);
2802out_unreg:
2803 kvm_async_pf_deinit();
2804out_free:
2805 kmem_cache_destroy(kvm_vcpu_cache);
2806out_free_3:
2807 unregister_reboot_notifier(&kvm_reboot_notifier);
2808 unregister_cpu_notifier(&kvm_cpu_notifier);
2809out_free_2:
2810out_free_1:
2811 kvm_arch_hardware_unsetup();
2812out_free_0a:
2813 free_cpumask_var(cpus_hardware_enabled);
2814out_free_0:
2815 if (fault_page)
2816 __free_page(fault_page);
2817 if (hwpoison_page)
2818 __free_page(hwpoison_page);
2819 __free_page(bad_page);
2820out:
2821 kvm_arch_exit();
2822out_fail:
2823 return r;
2824}
2825EXPORT_SYMBOL_GPL(kvm_init);
2826
2827void kvm_exit(void)
2828{
2829 kvm_exit_debug();
2830 misc_deregister(&kvm_dev);
2831 kmem_cache_destroy(kvm_vcpu_cache);
2832 kvm_async_pf_deinit();
2833 unregister_syscore_ops(&kvm_syscore_ops);
2834 unregister_reboot_notifier(&kvm_reboot_notifier);
2835 unregister_cpu_notifier(&kvm_cpu_notifier);
2836 on_each_cpu(hardware_disable_nolock, NULL, 1);
2837 kvm_arch_hardware_unsetup();
2838 kvm_arch_exit();
2839 free_cpumask_var(cpus_hardware_enabled);
2840 __free_page(hwpoison_page);
2841 __free_page(bad_page);
2842}
2843EXPORT_SYMBOL_GPL(kvm_exit);