KVM: s390: add cpu model support
[linux-2.6-block.git] / virt / kvm / kvm_main.c
CommitLineData
6aa8b732
AK
1/*
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.
9611c187 8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6aa8b732
AK
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
e2174021 19#include "iodev.h"
6aa8b732 20
edf88417 21#include <linux/kvm_host.h>
6aa8b732
AK
22#include <linux/kvm.h>
23#include <linux/module.h>
24#include <linux/errno.h>
6aa8b732 25#include <linux/percpu.h>
6aa8b732
AK
26#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
6aa8b732 29#include <linux/reboot.h>
6aa8b732
AK
30#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
fb3600cc 33#include <linux/syscore_ops.h>
774c47f1 34#include <linux/cpu.h>
e8edc6e0 35#include <linux/sched.h>
d9e368d6
AK
36#include <linux/cpumask.h>
37#include <linux/smp.h>
d6d28168 38#include <linux/anon_inodes.h>
04d2cc77 39#include <linux/profile.h>
7aa81cc0 40#include <linux/kvm_para.h>
6fc138d2 41#include <linux/pagemap.h>
8d4e1288 42#include <linux/mman.h>
35149e21 43#include <linux/swap.h>
e56d532f 44#include <linux/bitops.h>
547de29e 45#include <linux/spinlock.h>
6ff5894c 46#include <linux/compat.h>
bc6678a3 47#include <linux/srcu.h>
8f0b1ab6 48#include <linux/hugetlb.h>
5a0e3ad6 49#include <linux/slab.h>
743eeb0b
SL
50#include <linux/sort.h>
51#include <linux/bsearch.h>
6aa8b732 52
e495606d 53#include <asm/processor.h>
e495606d 54#include <asm/io.h>
2ea75be3 55#include <asm/ioctl.h>
e495606d 56#include <asm/uaccess.h>
3e021bf5 57#include <asm/pgtable.h>
6aa8b732 58
5f94c174 59#include "coalesced_mmio.h"
af585b92 60#include "async_pf.h"
3c3c29fd 61#include "vfio.h"
5f94c174 62
229456fc
MT
63#define CREATE_TRACE_POINTS
64#include <trace/events/kvm.h>
65
6aa8b732
AK
66MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
f7819512
PB
69unsigned int halt_poll_ns = 0;
70module_param(halt_poll_ns, uint, S_IRUGO | S_IWUSR);
71
fa40a821
MT
72/*
73 * Ordering of locks:
74 *
fae3a353 75 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
fa40a821
MT
76 */
77
2f303b74 78DEFINE_SPINLOCK(kvm_lock);
4a937f96 79static DEFINE_RAW_SPINLOCK(kvm_count_lock);
e9b11c17 80LIST_HEAD(vm_list);
133de902 81
7f59f492 82static cpumask_var_t cpus_hardware_enabled;
10474ae8
AG
83static int kvm_usage_count = 0;
84static atomic_t hardware_enable_failed;
1b6c0168 85
c16f862d
RR
86struct kmem_cache *kvm_vcpu_cache;
87EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
1165f5fe 88
15ad7146
AK
89static __read_mostly struct preempt_ops kvm_preempt_ops;
90
76f7c879 91struct dentry *kvm_debugfs_dir;
6aa8b732 92
bccf2150
AK
93static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
94 unsigned long arg);
1dda606c
AG
95#ifdef CONFIG_COMPAT
96static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
97 unsigned long arg);
98#endif
10474ae8
AG
99static int hardware_enable_all(void);
100static void hardware_disable_all(void);
bccf2150 101
e93f8a0f 102static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
7940876e
SH
103
104static void kvm_release_pfn_dirty(pfn_t pfn);
105static void mark_page_dirty_in_slot(struct kvm *kvm,
106 struct kvm_memory_slot *memslot, gfn_t gfn);
e93f8a0f 107
52480137 108__visible bool kvm_rebooting;
b7c4145b 109EXPORT_SYMBOL_GPL(kvm_rebooting);
4ecac3fd 110
54dee993
MT
111static bool largepages_enabled = true;
112
bf4bea8e 113bool kvm_is_reserved_pfn(pfn_t pfn)
cbff90a7 114{
11feeb49 115 if (pfn_valid(pfn))
bf4bea8e 116 return PageReserved(pfn_to_page(pfn));
cbff90a7
BAY
117
118 return true;
119}
120
bccf2150
AK
121/*
122 * Switches to specified vcpu, until a matching vcpu_put()
123 */
9fc77441 124int vcpu_load(struct kvm_vcpu *vcpu)
6aa8b732 125{
15ad7146
AK
126 int cpu;
127
9fc77441
MT
128 if (mutex_lock_killable(&vcpu->mutex))
129 return -EINTR;
15ad7146
AK
130 cpu = get_cpu();
131 preempt_notifier_register(&vcpu->preempt_notifier);
313a3dc7 132 kvm_arch_vcpu_load(vcpu, cpu);
15ad7146 133 put_cpu();
9fc77441 134 return 0;
6aa8b732
AK
135}
136
313a3dc7 137void vcpu_put(struct kvm_vcpu *vcpu)
6aa8b732 138{
15ad7146 139 preempt_disable();
313a3dc7 140 kvm_arch_vcpu_put(vcpu);
15ad7146
AK
141 preempt_notifier_unregister(&vcpu->preempt_notifier);
142 preempt_enable();
6aa8b732
AK
143 mutex_unlock(&vcpu->mutex);
144}
145
d9e368d6
AK
146static void ack_flush(void *_completed)
147{
d9e368d6
AK
148}
149
445b8236 150bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
d9e368d6 151{
597a5f55 152 int i, cpu, me;
6ef7a1bc
RR
153 cpumask_var_t cpus;
154 bool called = true;
d9e368d6 155 struct kvm_vcpu *vcpu;
d9e368d6 156
79f55997 157 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
6ef7a1bc 158
3cba4130 159 me = get_cpu();
988a2cae 160 kvm_for_each_vcpu(i, vcpu, kvm) {
3cba4130 161 kvm_make_request(req, vcpu);
d9e368d6 162 cpu = vcpu->cpu;
6b7e2d09
XG
163
164 /* Set ->requests bit before we read ->mode */
165 smp_mb();
166
167 if (cpus != NULL && cpu != -1 && cpu != me &&
168 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
6ef7a1bc 169 cpumask_set_cpu(cpu, cpus);
49846896 170 }
6ef7a1bc
RR
171 if (unlikely(cpus == NULL))
172 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
173 else if (!cpumask_empty(cpus))
174 smp_call_function_many(cpus, ack_flush, NULL, 1);
175 else
176 called = false;
3cba4130 177 put_cpu();
6ef7a1bc 178 free_cpumask_var(cpus);
49846896 179 return called;
d9e368d6
AK
180}
181
a6d51016 182#ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL
49846896 183void kvm_flush_remote_tlbs(struct kvm *kvm)
2e53d63a 184{
a086f6a1
XG
185 long dirty_count = kvm->tlbs_dirty;
186
187 smp_mb();
445b8236 188 if (kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
49846896 189 ++kvm->stat.remote_tlb_flush;
a086f6a1 190 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
2e53d63a 191}
2ba9f0d8 192EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
a6d51016 193#endif
2e53d63a 194
49846896
RR
195void kvm_reload_remote_mmus(struct kvm *kvm)
196{
445b8236 197 kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
49846896 198}
2e53d63a 199
d828199e
MT
200void kvm_make_mclock_inprogress_request(struct kvm *kvm)
201{
445b8236 202 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
d828199e
MT
203}
204
3d81bc7e 205void kvm_make_scan_ioapic_request(struct kvm *kvm)
c7c9c56c 206{
445b8236 207 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
c7c9c56c
YZ
208}
209
fb3f0f51
RR
210int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
211{
212 struct page *page;
213 int r;
214
215 mutex_init(&vcpu->mutex);
216 vcpu->cpu = -1;
fb3f0f51
RR
217 vcpu->kvm = kvm;
218 vcpu->vcpu_id = id;
34bb10b7 219 vcpu->pid = NULL;
b6958ce4 220 init_waitqueue_head(&vcpu->wq);
af585b92 221 kvm_async_pf_vcpu_init(vcpu);
fb3f0f51
RR
222
223 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
224 if (!page) {
225 r = -ENOMEM;
226 goto fail;
227 }
228 vcpu->run = page_address(page);
229
4c088493
R
230 kvm_vcpu_set_in_spin_loop(vcpu, false);
231 kvm_vcpu_set_dy_eligible(vcpu, false);
3a08a8f9 232 vcpu->preempted = false;
4c088493 233
e9b11c17 234 r = kvm_arch_vcpu_init(vcpu);
fb3f0f51 235 if (r < 0)
e9b11c17 236 goto fail_free_run;
fb3f0f51
RR
237 return 0;
238
fb3f0f51
RR
239fail_free_run:
240 free_page((unsigned long)vcpu->run);
241fail:
76fafa5e 242 return r;
fb3f0f51
RR
243}
244EXPORT_SYMBOL_GPL(kvm_vcpu_init);
245
246void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
247{
34bb10b7 248 put_pid(vcpu->pid);
e9b11c17 249 kvm_arch_vcpu_uninit(vcpu);
fb3f0f51
RR
250 free_page((unsigned long)vcpu->run);
251}
252EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
253
e930bffe
AA
254#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
255static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
256{
257 return container_of(mn, struct kvm, mmu_notifier);
258}
259
260static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
261 struct mm_struct *mm,
262 unsigned long address)
263{
264 struct kvm *kvm = mmu_notifier_to_kvm(mn);
bc6678a3 265 int need_tlb_flush, idx;
e930bffe
AA
266
267 /*
268 * When ->invalidate_page runs, the linux pte has been zapped
269 * already but the page is still allocated until
270 * ->invalidate_page returns. So if we increase the sequence
271 * here the kvm page fault will notice if the spte can't be
272 * established because the page is going to be freed. If
273 * instead the kvm page fault establishes the spte before
274 * ->invalidate_page runs, kvm_unmap_hva will release it
275 * before returning.
276 *
277 * The sequence increase only need to be seen at spin_unlock
278 * time, and not at spin_lock time.
279 *
280 * Increasing the sequence after the spin_unlock would be
281 * unsafe because the kvm page fault could then establish the
282 * pte after kvm_unmap_hva returned, without noticing the page
283 * is going to be freed.
284 */
bc6678a3 285 idx = srcu_read_lock(&kvm->srcu);
e930bffe 286 spin_lock(&kvm->mmu_lock);
565f3be2 287
e930bffe 288 kvm->mmu_notifier_seq++;
a4ee1ca4 289 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
e930bffe
AA
290 /* we've to flush the tlb before the pages can be freed */
291 if (need_tlb_flush)
292 kvm_flush_remote_tlbs(kvm);
293
565f3be2 294 spin_unlock(&kvm->mmu_lock);
fe71557a
TC
295
296 kvm_arch_mmu_notifier_invalidate_page(kvm, address);
297
565f3be2 298 srcu_read_unlock(&kvm->srcu, idx);
e930bffe
AA
299}
300
3da0dd43
IE
301static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
302 struct mm_struct *mm,
303 unsigned long address,
304 pte_t pte)
305{
306 struct kvm *kvm = mmu_notifier_to_kvm(mn);
bc6678a3 307 int idx;
3da0dd43 308
bc6678a3 309 idx = srcu_read_lock(&kvm->srcu);
3da0dd43
IE
310 spin_lock(&kvm->mmu_lock);
311 kvm->mmu_notifier_seq++;
312 kvm_set_spte_hva(kvm, address, pte);
313 spin_unlock(&kvm->mmu_lock);
bc6678a3 314 srcu_read_unlock(&kvm->srcu, idx);
3da0dd43
IE
315}
316
e930bffe
AA
317static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
318 struct mm_struct *mm,
319 unsigned long start,
320 unsigned long end)
321{
322 struct kvm *kvm = mmu_notifier_to_kvm(mn);
bc6678a3 323 int need_tlb_flush = 0, idx;
e930bffe 324
bc6678a3 325 idx = srcu_read_lock(&kvm->srcu);
e930bffe
AA
326 spin_lock(&kvm->mmu_lock);
327 /*
328 * The count increase must become visible at unlock time as no
329 * spte can be established without taking the mmu_lock and
330 * count is also read inside the mmu_lock critical section.
331 */
332 kvm->mmu_notifier_count++;
b3ae2096 333 need_tlb_flush = kvm_unmap_hva_range(kvm, start, end);
a4ee1ca4 334 need_tlb_flush |= kvm->tlbs_dirty;
e930bffe
AA
335 /* we've to flush the tlb before the pages can be freed */
336 if (need_tlb_flush)
337 kvm_flush_remote_tlbs(kvm);
565f3be2
TY
338
339 spin_unlock(&kvm->mmu_lock);
340 srcu_read_unlock(&kvm->srcu, idx);
e930bffe
AA
341}
342
343static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
344 struct mm_struct *mm,
345 unsigned long start,
346 unsigned long end)
347{
348 struct kvm *kvm = mmu_notifier_to_kvm(mn);
349
350 spin_lock(&kvm->mmu_lock);
351 /*
352 * This sequence increase will notify the kvm page fault that
353 * the page that is going to be mapped in the spte could have
354 * been freed.
355 */
356 kvm->mmu_notifier_seq++;
a355aa54 357 smp_wmb();
e930bffe
AA
358 /*
359 * The above sequence increase must be visible before the
a355aa54
PM
360 * below count decrease, which is ensured by the smp_wmb above
361 * in conjunction with the smp_rmb in mmu_notifier_retry().
e930bffe
AA
362 */
363 kvm->mmu_notifier_count--;
364 spin_unlock(&kvm->mmu_lock);
365
366 BUG_ON(kvm->mmu_notifier_count < 0);
367}
368
369static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
370 struct mm_struct *mm,
57128468
ALC
371 unsigned long start,
372 unsigned long end)
e930bffe
AA
373{
374 struct kvm *kvm = mmu_notifier_to_kvm(mn);
bc6678a3 375 int young, idx;
e930bffe 376
bc6678a3 377 idx = srcu_read_lock(&kvm->srcu);
e930bffe 378 spin_lock(&kvm->mmu_lock);
e930bffe 379
57128468 380 young = kvm_age_hva(kvm, start, end);
e930bffe
AA
381 if (young)
382 kvm_flush_remote_tlbs(kvm);
383
565f3be2
TY
384 spin_unlock(&kvm->mmu_lock);
385 srcu_read_unlock(&kvm->srcu, idx);
386
e930bffe
AA
387 return young;
388}
389
8ee53820
AA
390static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
391 struct mm_struct *mm,
392 unsigned long address)
393{
394 struct kvm *kvm = mmu_notifier_to_kvm(mn);
395 int young, idx;
396
397 idx = srcu_read_lock(&kvm->srcu);
398 spin_lock(&kvm->mmu_lock);
399 young = kvm_test_age_hva(kvm, address);
400 spin_unlock(&kvm->mmu_lock);
401 srcu_read_unlock(&kvm->srcu, idx);
402
403 return young;
404}
405
85db06e5
MT
406static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
407 struct mm_struct *mm)
408{
409 struct kvm *kvm = mmu_notifier_to_kvm(mn);
eda2beda
LJ
410 int idx;
411
412 idx = srcu_read_lock(&kvm->srcu);
2df72e9b 413 kvm_arch_flush_shadow_all(kvm);
eda2beda 414 srcu_read_unlock(&kvm->srcu, idx);
85db06e5
MT
415}
416
e930bffe
AA
417static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
418 .invalidate_page = kvm_mmu_notifier_invalidate_page,
419 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
420 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
421 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
8ee53820 422 .test_young = kvm_mmu_notifier_test_young,
3da0dd43 423 .change_pte = kvm_mmu_notifier_change_pte,
85db06e5 424 .release = kvm_mmu_notifier_release,
e930bffe 425};
4c07b0a4
AK
426
427static int kvm_init_mmu_notifier(struct kvm *kvm)
428{
429 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
430 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
431}
432
433#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
434
435static int kvm_init_mmu_notifier(struct kvm *kvm)
436{
437 return 0;
438}
439
e930bffe
AA
440#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
441
bf3e05bc
XG
442static void kvm_init_memslots_id(struct kvm *kvm)
443{
444 int i;
445 struct kvm_memslots *slots = kvm->memslots;
446
447 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
f85e2cb5 448 slots->id_to_index[i] = slots->memslots[i].id = i;
bf3e05bc
XG
449}
450
e08b9637 451static struct kvm *kvm_create_vm(unsigned long type)
6aa8b732 452{
d89f5eff
JK
453 int r, i;
454 struct kvm *kvm = kvm_arch_alloc_vm();
6aa8b732 455
d89f5eff
JK
456 if (!kvm)
457 return ERR_PTR(-ENOMEM);
458
e08b9637 459 r = kvm_arch_init_vm(kvm, type);
d89f5eff 460 if (r)
719d93cd 461 goto out_err_no_disable;
10474ae8
AG
462
463 r = hardware_enable_all();
464 if (r)
719d93cd 465 goto out_err_no_disable;
10474ae8 466
c77dcacb 467#ifdef CONFIG_HAVE_KVM_IRQFD
136bdfee 468 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
75858a84 469#endif
6aa8b732 470
1e702d9a
AW
471 BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
472
46a26bf5
MT
473 r = -ENOMEM;
474 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
475 if (!kvm->memslots)
719d93cd 476 goto out_err_no_srcu;
00f034a1
PB
477
478 /*
479 * Init kvm generation close to the maximum to easily test the
480 * code of handling generation number wrap-around.
481 */
482 kvm->memslots->generation = -150;
483
bf3e05bc 484 kvm_init_memslots_id(kvm);
bc6678a3 485 if (init_srcu_struct(&kvm->srcu))
719d93cd
CB
486 goto out_err_no_srcu;
487 if (init_srcu_struct(&kvm->irq_srcu))
488 goto out_err_no_irq_srcu;
e93f8a0f
MT
489 for (i = 0; i < KVM_NR_BUSES; i++) {
490 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
491 GFP_KERNEL);
57e7fbee 492 if (!kvm->buses[i])
e93f8a0f 493 goto out_err;
e93f8a0f 494 }
e930bffe 495
74b5c5bf 496 spin_lock_init(&kvm->mmu_lock);
6d4e4c4f
AK
497 kvm->mm = current->mm;
498 atomic_inc(&kvm->mm->mm_count);
d34e6b17 499 kvm_eventfd_init(kvm);
11ec2804 500 mutex_init(&kvm->lock);
60eead79 501 mutex_init(&kvm->irq_lock);
79fac95e 502 mutex_init(&kvm->slots_lock);
d39f13b0 503 atomic_set(&kvm->users_count, 1);
07f0a7bd 504 INIT_LIST_HEAD(&kvm->devices);
74b5c5bf
MW
505
506 r = kvm_init_mmu_notifier(kvm);
507 if (r)
508 goto out_err;
509
2f303b74 510 spin_lock(&kvm_lock);
5e58cfe4 511 list_add(&kvm->vm_list, &vm_list);
2f303b74 512 spin_unlock(&kvm_lock);
d89f5eff 513
f17abe9a 514 return kvm;
10474ae8
AG
515
516out_err:
719d93cd
CB
517 cleanup_srcu_struct(&kvm->irq_srcu);
518out_err_no_irq_srcu:
57e7fbee 519 cleanup_srcu_struct(&kvm->srcu);
719d93cd 520out_err_no_srcu:
10474ae8 521 hardware_disable_all();
719d93cd 522out_err_no_disable:
e93f8a0f
MT
523 for (i = 0; i < KVM_NR_BUSES; i++)
524 kfree(kvm->buses[i]);
46a26bf5 525 kfree(kvm->memslots);
d89f5eff 526 kvm_arch_free_vm(kvm);
10474ae8 527 return ERR_PTR(r);
f17abe9a
AK
528}
529
92eca8fa
TY
530/*
531 * Avoid using vmalloc for a small buffer.
532 * Should not be used when the size is statically known.
533 */
c1a7b32a 534void *kvm_kvzalloc(unsigned long size)
92eca8fa
TY
535{
536 if (size > PAGE_SIZE)
537 return vzalloc(size);
538 else
539 return kzalloc(size, GFP_KERNEL);
540}
541
c1a7b32a 542void kvm_kvfree(const void *addr)
92eca8fa
TY
543{
544 if (is_vmalloc_addr(addr))
545 vfree(addr);
546 else
547 kfree(addr);
548}
549
a36a57b1
TY
550static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
551{
552 if (!memslot->dirty_bitmap)
553 return;
554
92eca8fa 555 kvm_kvfree(memslot->dirty_bitmap);
a36a57b1
TY
556 memslot->dirty_bitmap = NULL;
557}
558
6aa8b732
AK
559/*
560 * Free any memory in @free but not in @dont.
561 */
5587027c 562static void kvm_free_physmem_slot(struct kvm *kvm, struct kvm_memory_slot *free,
6aa8b732
AK
563 struct kvm_memory_slot *dont)
564{
6aa8b732 565 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
a36a57b1 566 kvm_destroy_dirty_bitmap(free);
6aa8b732 567
5587027c 568 kvm_arch_free_memslot(kvm, free, dont);
05da4558 569
6aa8b732 570 free->npages = 0;
6aa8b732
AK
571}
572
7940876e 573static void kvm_free_physmem(struct kvm *kvm)
6aa8b732 574{
46a26bf5 575 struct kvm_memslots *slots = kvm->memslots;
be6ba0f0 576 struct kvm_memory_slot *memslot;
46a26bf5 577
be6ba0f0 578 kvm_for_each_memslot(memslot, slots)
5587027c 579 kvm_free_physmem_slot(kvm, memslot, NULL);
6aa8b732 580
46a26bf5 581 kfree(kvm->memslots);
6aa8b732
AK
582}
583
07f0a7bd
SW
584static void kvm_destroy_devices(struct kvm *kvm)
585{
586 struct list_head *node, *tmp;
587
588 list_for_each_safe(node, tmp, &kvm->devices) {
589 struct kvm_device *dev =
590 list_entry(node, struct kvm_device, vm_node);
591
592 list_del(node);
593 dev->ops->destroy(dev);
594 }
595}
596
f17abe9a
AK
597static void kvm_destroy_vm(struct kvm *kvm)
598{
e93f8a0f 599 int i;
6d4e4c4f
AK
600 struct mm_struct *mm = kvm->mm;
601
ad8ba2cd 602 kvm_arch_sync_events(kvm);
2f303b74 603 spin_lock(&kvm_lock);
133de902 604 list_del(&kvm->vm_list);
2f303b74 605 spin_unlock(&kvm_lock);
399ec807 606 kvm_free_irq_routing(kvm);
e93f8a0f
MT
607 for (i = 0; i < KVM_NR_BUSES; i++)
608 kvm_io_bus_destroy(kvm->buses[i]);
980da6ce 609 kvm_coalesced_mmio_free(kvm);
e930bffe
AA
610#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
611 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
f00be0ca 612#else
2df72e9b 613 kvm_arch_flush_shadow_all(kvm);
5f94c174 614#endif
d19a9cd2 615 kvm_arch_destroy_vm(kvm);
07f0a7bd 616 kvm_destroy_devices(kvm);
d89f5eff 617 kvm_free_physmem(kvm);
820b3fcd 618 cleanup_srcu_struct(&kvm->irq_srcu);
d89f5eff
JK
619 cleanup_srcu_struct(&kvm->srcu);
620 kvm_arch_free_vm(kvm);
10474ae8 621 hardware_disable_all();
6d4e4c4f 622 mmdrop(mm);
f17abe9a
AK
623}
624
d39f13b0
IE
625void kvm_get_kvm(struct kvm *kvm)
626{
627 atomic_inc(&kvm->users_count);
628}
629EXPORT_SYMBOL_GPL(kvm_get_kvm);
630
631void kvm_put_kvm(struct kvm *kvm)
632{
633 if (atomic_dec_and_test(&kvm->users_count))
634 kvm_destroy_vm(kvm);
635}
636EXPORT_SYMBOL_GPL(kvm_put_kvm);
637
638
f17abe9a
AK
639static int kvm_vm_release(struct inode *inode, struct file *filp)
640{
641 struct kvm *kvm = filp->private_data;
642
721eecbf
GH
643 kvm_irqfd_release(kvm);
644
d39f13b0 645 kvm_put_kvm(kvm);
6aa8b732
AK
646 return 0;
647}
648
515a0127
TY
649/*
650 * Allocation size is twice as large as the actual dirty bitmap size.
93474b25 651 * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
515a0127 652 */
a36a57b1
TY
653static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
654{
515a0127 655 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
a36a57b1 656
92eca8fa 657 memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes);
a36a57b1
TY
658 if (!memslot->dirty_bitmap)
659 return -ENOMEM;
660
a36a57b1
TY
661 return 0;
662}
663
bf3e05bc 664/*
0e60b079
IM
665 * Insert memslot and re-sort memslots based on their GFN,
666 * so binary search could be used to lookup GFN.
667 * Sorting algorithm takes advantage of having initially
668 * sorted array and known changed memslot position.
bf3e05bc 669 */
5cc15027
PB
670static void update_memslots(struct kvm_memslots *slots,
671 struct kvm_memory_slot *new)
bf3e05bc 672{
8593176c
PB
673 int id = new->id;
674 int i = slots->id_to_index[id];
063584d4 675 struct kvm_memory_slot *mslots = slots->memslots;
f85e2cb5 676
8593176c 677 WARN_ON(mslots[i].id != id);
9c1a5d38 678 if (!new->npages) {
dbaff309 679 WARN_ON(!mslots[i].npages);
0e60b079 680 new->base_gfn = 0;
b0165f1b 681 new->flags = 0;
9c1a5d38
IM
682 if (mslots[i].npages)
683 slots->used_slots--;
684 } else {
685 if (!mslots[i].npages)
686 slots->used_slots++;
687 }
0e60b079 688
7f379cff 689 while (i < KVM_MEM_SLOTS_NUM - 1 &&
0e60b079
IM
690 new->base_gfn <= mslots[i + 1].base_gfn) {
691 if (!mslots[i + 1].npages)
692 break;
7f379cff
IM
693 mslots[i] = mslots[i + 1];
694 slots->id_to_index[mslots[i].id] = i;
695 i++;
696 }
efbeec70
PB
697
698 /*
699 * The ">=" is needed when creating a slot with base_gfn == 0,
700 * so that it moves before all those with base_gfn == npages == 0.
701 *
702 * On the other hand, if new->npages is zero, the above loop has
703 * already left i pointing to the beginning of the empty part of
704 * mslots, and the ">=" would move the hole backwards in this
705 * case---which is wrong. So skip the loop when deleting a slot.
706 */
707 if (new->npages) {
708 while (i > 0 &&
709 new->base_gfn >= mslots[i - 1].base_gfn) {
710 mslots[i] = mslots[i - 1];
711 slots->id_to_index[mslots[i].id] = i;
712 i--;
713 }
dbaff309
PB
714 } else
715 WARN_ON_ONCE(i != slots->used_slots);
f85e2cb5 716
8593176c
PB
717 mslots[i] = *new;
718 slots->id_to_index[mslots[i].id] = i;
bf3e05bc
XG
719}
720
a50d64d6
XG
721static int check_memory_region_flags(struct kvm_userspace_memory_region *mem)
722{
4d8b81ab
XG
723 u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
724
0f8a4de3 725#ifdef __KVM_HAVE_READONLY_MEM
4d8b81ab
XG
726 valid_flags |= KVM_MEM_READONLY;
727#endif
728
729 if (mem->flags & ~valid_flags)
a50d64d6
XG
730 return -EINVAL;
731
732 return 0;
733}
734
7ec4fb44 735static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
5cc15027 736 struct kvm_memslots *slots)
7ec4fb44
GN
737{
738 struct kvm_memslots *old_memslots = kvm->memslots;
739
ee3d1570
DM
740 /*
741 * Set the low bit in the generation, which disables SPTE caching
742 * until the end of synchronize_srcu_expedited.
743 */
744 WARN_ON(old_memslots->generation & 1);
745 slots->generation = old_memslots->generation + 1;
746
7ec4fb44
GN
747 rcu_assign_pointer(kvm->memslots, slots);
748 synchronize_srcu_expedited(&kvm->srcu);
e59dbe09 749
ee3d1570
DM
750 /*
751 * Increment the new memslot generation a second time. This prevents
752 * vm exits that race with memslot updates from caching a memslot
753 * generation that will (potentially) be valid forever.
754 */
755 slots->generation++;
756
e59dbe09
TY
757 kvm_arch_memslots_updated(kvm);
758
759 return old_memslots;
7ec4fb44
GN
760}
761
6aa8b732
AK
762/*
763 * Allocate some memory and give it an address in the guest physical address
764 * space.
765 *
766 * Discontiguous memory is allowed, mostly for framebuffers.
f78e0e2e 767 *
02d5d55b 768 * Must be called holding kvm->slots_lock for write.
6aa8b732 769 */
f78e0e2e 770int __kvm_set_memory_region(struct kvm *kvm,
47ae31e2 771 struct kvm_userspace_memory_region *mem)
6aa8b732 772{
8234b22e 773 int r;
6aa8b732 774 gfn_t base_gfn;
28bcb112 775 unsigned long npages;
a843fac2 776 struct kvm_memory_slot *slot;
6aa8b732 777 struct kvm_memory_slot old, new;
b7f69c55 778 struct kvm_memslots *slots = NULL, *old_memslots;
f64c0398 779 enum kvm_mr_change change;
6aa8b732 780
a50d64d6
XG
781 r = check_memory_region_flags(mem);
782 if (r)
783 goto out;
784
6aa8b732
AK
785 r = -EINVAL;
786 /* General sanity checks */
787 if (mem->memory_size & (PAGE_SIZE - 1))
788 goto out;
789 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
790 goto out;
fa3d315a 791 /* We can read the guest memory with __xxx_user() later on. */
47ae31e2 792 if ((mem->slot < KVM_USER_MEM_SLOTS) &&
fa3d315a 793 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
9e3bb6b6
HC
794 !access_ok(VERIFY_WRITE,
795 (void __user *)(unsigned long)mem->userspace_addr,
796 mem->memory_size)))
78749809 797 goto out;
93a5cef0 798 if (mem->slot >= KVM_MEM_SLOTS_NUM)
6aa8b732
AK
799 goto out;
800 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
801 goto out;
802
a843fac2 803 slot = id_to_memslot(kvm->memslots, mem->slot);
6aa8b732
AK
804 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
805 npages = mem->memory_size >> PAGE_SHIFT;
806
660c22c4
TY
807 if (npages > KVM_MEM_MAX_NR_PAGES)
808 goto out;
809
6aa8b732
AK
810 if (!npages)
811 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
812
a843fac2 813 new = old = *slot;
6aa8b732 814
e36d96f7 815 new.id = mem->slot;
6aa8b732
AK
816 new.base_gfn = base_gfn;
817 new.npages = npages;
818 new.flags = mem->flags;
819
f64c0398
TY
820 if (npages) {
821 if (!old.npages)
822 change = KVM_MR_CREATE;
823 else { /* Modify an existing slot. */
824 if ((mem->userspace_addr != old.userspace_addr) ||
75d61fbc
TY
825 (npages != old.npages) ||
826 ((new.flags ^ old.flags) & KVM_MEM_READONLY))
f64c0398
TY
827 goto out;
828
829 if (base_gfn != old.base_gfn)
830 change = KVM_MR_MOVE;
831 else if (new.flags != old.flags)
832 change = KVM_MR_FLAGS_ONLY;
833 else { /* Nothing to change. */
834 r = 0;
835 goto out;
836 }
837 }
838 } else if (old.npages) {
839 change = KVM_MR_DELETE;
840 } else /* Modify a non-existent slot: disallowed. */
0ea75e1d 841 goto out;
6aa8b732 842
f64c0398 843 if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
0a706bee
TY
844 /* Check for overlaps */
845 r = -EEXIST;
846 kvm_for_each_memslot(slot, kvm->memslots) {
a843fac2
TY
847 if ((slot->id >= KVM_USER_MEM_SLOTS) ||
848 (slot->id == mem->slot))
0a706bee
TY
849 continue;
850 if (!((base_gfn + npages <= slot->base_gfn) ||
851 (base_gfn >= slot->base_gfn + slot->npages)))
852 goto out;
853 }
6aa8b732 854 }
6aa8b732 855
6aa8b732
AK
856 /* Free page dirty bitmap if unneeded */
857 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 858 new.dirty_bitmap = NULL;
6aa8b732
AK
859
860 r = -ENOMEM;
f64c0398 861 if (change == KVM_MR_CREATE) {
189a2f7b 862 new.userspace_addr = mem->userspace_addr;
d89cc617 863
5587027c 864 if (kvm_arch_create_memslot(kvm, &new, npages))
db3fe4eb 865 goto out_free;
6aa8b732 866 }
ec04b260 867
6aa8b732
AK
868 /* Allocate page dirty bitmap if needed */
869 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
a36a57b1 870 if (kvm_create_dirty_bitmap(&new) < 0)
f78e0e2e 871 goto out_free;
6aa8b732
AK
872 }
873
f2a81036
PB
874 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
875 GFP_KERNEL);
876 if (!slots)
877 goto out_free;
878
f64c0398 879 if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
28a37544
XG
880 slot = id_to_memslot(slots, mem->slot);
881 slot->flags |= KVM_MEMSLOT_INVALID;
882
5cc15027 883 old_memslots = install_new_memslots(kvm, slots);
bc6678a3 884
e40f193f
AW
885 /* slot was deleted or moved, clear iommu mapping */
886 kvm_iommu_unmap_pages(kvm, &old);
12d6e753
MT
887 /* From this point no new shadow pages pointing to a deleted,
888 * or moved, memslot will be created.
bc6678a3
MT
889 *
890 * validation of sp->gfn happens in:
891 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
892 * - kvm_is_visible_gfn (mmu_check_roots)
893 */
2df72e9b 894 kvm_arch_flush_shadow_memslot(kvm, slot);
f2a81036
PB
895
896 /*
897 * We can re-use the old_memslots from above, the only difference
898 * from the currently installed memslots is the invalid flag. This
899 * will get overwritten by update_memslots anyway.
900 */
b7f69c55 901 slots = old_memslots;
bc6678a3 902 }
34d4cb8f 903
7b6195a9 904 r = kvm_arch_prepare_memory_region(kvm, &new, mem, change);
f7784b8e 905 if (r)
b7f69c55 906 goto out_slots;
f7784b8e 907
bc6678a3 908 /* actual memory is freed via old in kvm_free_physmem_slot below */
f64c0398 909 if (change == KVM_MR_DELETE) {
bc6678a3 910 new.dirty_bitmap = NULL;
db3fe4eb 911 memset(&new.arch, 0, sizeof(new.arch));
bc6678a3
MT
912 }
913
5cc15027
PB
914 update_memslots(slots, &new);
915 old_memslots = install_new_memslots(kvm, slots);
3ad82a7e 916
8482644a 917 kvm_arch_commit_memory_region(kvm, mem, &old, change);
82ce2c96 918
5587027c 919 kvm_free_physmem_slot(kvm, &old, &new);
bc6678a3
MT
920 kfree(old_memslots);
921
261874b0
AW
922 /*
923 * IOMMU mapping: New slots need to be mapped. Old slots need to be
75d61fbc
TY
924 * un-mapped and re-mapped if their base changes. Since base change
925 * unmapping is handled above with slot deletion, mapping alone is
926 * needed here. Anything else the iommu might care about for existing
927 * slots (size changes, userspace addr changes and read-only flag
928 * changes) is disallowed above, so any other attribute changes getting
929 * here can be skipped.
261874b0 930 */
75d61fbc
TY
931 if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
932 r = kvm_iommu_map_pages(kvm, &new);
e0230e13 933 return r;
bc6678a3
MT
934 }
935
6aa8b732
AK
936 return 0;
937
e40f193f
AW
938out_slots:
939 kfree(slots);
f78e0e2e 940out_free:
5587027c 941 kvm_free_physmem_slot(kvm, &new, &old);
6aa8b732
AK
942out:
943 return r;
210c7c4d 944}
f78e0e2e
SY
945EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
946
947int kvm_set_memory_region(struct kvm *kvm,
47ae31e2 948 struct kvm_userspace_memory_region *mem)
f78e0e2e
SY
949{
950 int r;
951
79fac95e 952 mutex_lock(&kvm->slots_lock);
47ae31e2 953 r = __kvm_set_memory_region(kvm, mem);
79fac95e 954 mutex_unlock(&kvm->slots_lock);
f78e0e2e
SY
955 return r;
956}
210c7c4d
IE
957EXPORT_SYMBOL_GPL(kvm_set_memory_region);
958
7940876e
SH
959static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
960 struct kvm_userspace_memory_region *mem)
210c7c4d 961{
bbacc0c1 962 if (mem->slot >= KVM_USER_MEM_SLOTS)
e0d62c7f 963 return -EINVAL;
47ae31e2 964 return kvm_set_memory_region(kvm, mem);
6aa8b732
AK
965}
966
5bb064dc
ZX
967int kvm_get_dirty_log(struct kvm *kvm,
968 struct kvm_dirty_log *log, int *is_dirty)
6aa8b732
AK
969{
970 struct kvm_memory_slot *memslot;
971 int r, i;
87bf6e7d 972 unsigned long n;
6aa8b732
AK
973 unsigned long any = 0;
974
6aa8b732 975 r = -EINVAL;
bbacc0c1 976 if (log->slot >= KVM_USER_MEM_SLOTS)
6aa8b732
AK
977 goto out;
978
28a37544 979 memslot = id_to_memslot(kvm->memslots, log->slot);
6aa8b732
AK
980 r = -ENOENT;
981 if (!memslot->dirty_bitmap)
982 goto out;
983
87bf6e7d 984 n = kvm_dirty_bitmap_bytes(memslot);
6aa8b732 985
cd1a4a98 986 for (i = 0; !any && i < n/sizeof(long); ++i)
6aa8b732
AK
987 any = memslot->dirty_bitmap[i];
988
989 r = -EFAULT;
990 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
991 goto out;
992
5bb064dc
ZX
993 if (any)
994 *is_dirty = 1;
6aa8b732
AK
995
996 r = 0;
6aa8b732 997out:
6aa8b732
AK
998 return r;
999}
2ba9f0d8 1000EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
6aa8b732 1001
ba0513b5
MS
1002#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
1003/**
1004 * kvm_get_dirty_log_protect - get a snapshot of dirty pages, and if any pages
1005 * are dirty write protect them for next write.
1006 * @kvm: pointer to kvm instance
1007 * @log: slot id and address to which we copy the log
1008 * @is_dirty: flag set if any page is dirty
1009 *
1010 * We need to keep it in mind that VCPU threads can write to the bitmap
1011 * concurrently. So, to avoid losing track of dirty pages we keep the
1012 * following order:
1013 *
1014 * 1. Take a snapshot of the bit and clear it if needed.
1015 * 2. Write protect the corresponding page.
1016 * 3. Copy the snapshot to the userspace.
1017 * 4. Upon return caller flushes TLB's if needed.
1018 *
1019 * Between 2 and 4, the guest may write to the page using the remaining TLB
1020 * entry. This is not a problem because the page is reported dirty using
1021 * the snapshot taken before and step 4 ensures that writes done after
1022 * exiting to userspace will be logged for the next call.
1023 *
1024 */
1025int kvm_get_dirty_log_protect(struct kvm *kvm,
1026 struct kvm_dirty_log *log, bool *is_dirty)
1027{
1028 struct kvm_memory_slot *memslot;
1029 int r, i;
1030 unsigned long n;
1031 unsigned long *dirty_bitmap;
1032 unsigned long *dirty_bitmap_buffer;
1033
1034 r = -EINVAL;
1035 if (log->slot >= KVM_USER_MEM_SLOTS)
1036 goto out;
1037
1038 memslot = id_to_memslot(kvm->memslots, log->slot);
1039
1040 dirty_bitmap = memslot->dirty_bitmap;
1041 r = -ENOENT;
1042 if (!dirty_bitmap)
1043 goto out;
1044
1045 n = kvm_dirty_bitmap_bytes(memslot);
1046
1047 dirty_bitmap_buffer = dirty_bitmap + n / sizeof(long);
1048 memset(dirty_bitmap_buffer, 0, n);
1049
1050 spin_lock(&kvm->mmu_lock);
1051 *is_dirty = false;
1052 for (i = 0; i < n / sizeof(long); i++) {
1053 unsigned long mask;
1054 gfn_t offset;
1055
1056 if (!dirty_bitmap[i])
1057 continue;
1058
1059 *is_dirty = true;
1060
1061 mask = xchg(&dirty_bitmap[i], 0);
1062 dirty_bitmap_buffer[i] = mask;
1063
1064 offset = i * BITS_PER_LONG;
3b0f1d01 1065 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, offset,
ba0513b5
MS
1066 mask);
1067 }
1068
1069 spin_unlock(&kvm->mmu_lock);
1070
1071 r = -EFAULT;
1072 if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
1073 goto out;
1074
1075 r = 0;
1076out:
1077 return r;
1078}
1079EXPORT_SYMBOL_GPL(kvm_get_dirty_log_protect);
1080#endif
1081
db3fe4eb
TY
1082bool kvm_largepages_enabled(void)
1083{
1084 return largepages_enabled;
1085}
1086
54dee993
MT
1087void kvm_disable_largepages(void)
1088{
1089 largepages_enabled = false;
1090}
1091EXPORT_SYMBOL_GPL(kvm_disable_largepages);
1092
49c7754c
GN
1093struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1094{
1095 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
1096}
a1f4d395 1097EXPORT_SYMBOL_GPL(gfn_to_memslot);
6aa8b732 1098
e0d62c7f
IE
1099int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1100{
bf3e05bc 1101 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
e0d62c7f 1102
bbacc0c1 1103 if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS ||
bf3e05bc
XG
1104 memslot->flags & KVM_MEMSLOT_INVALID)
1105 return 0;
e0d62c7f 1106
bf3e05bc 1107 return 1;
e0d62c7f
IE
1108}
1109EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1110
8f0b1ab6
JR
1111unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
1112{
1113 struct vm_area_struct *vma;
1114 unsigned long addr, size;
1115
1116 size = PAGE_SIZE;
1117
1118 addr = gfn_to_hva(kvm, gfn);
1119 if (kvm_is_error_hva(addr))
1120 return PAGE_SIZE;
1121
1122 down_read(&current->mm->mmap_sem);
1123 vma = find_vma(current->mm, addr);
1124 if (!vma)
1125 goto out;
1126
1127 size = vma_kernel_pagesize(vma);
1128
1129out:
1130 up_read(&current->mm->mmap_sem);
1131
1132 return size;
1133}
1134
4d8b81ab
XG
1135static bool memslot_is_readonly(struct kvm_memory_slot *slot)
1136{
1137 return slot->flags & KVM_MEM_READONLY;
1138}
1139
4d8b81ab
XG
1140static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1141 gfn_t *nr_pages, bool write)
539cb660 1142{
bc6678a3 1143 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
ca3a490c 1144 return KVM_HVA_ERR_BAD;
48987781 1145
4d8b81ab
XG
1146 if (memslot_is_readonly(slot) && write)
1147 return KVM_HVA_ERR_RO_BAD;
48987781
XG
1148
1149 if (nr_pages)
1150 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1151
4d8b81ab 1152 return __gfn_to_hva_memslot(slot, gfn);
539cb660 1153}
48987781 1154
4d8b81ab
XG
1155static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1156 gfn_t *nr_pages)
1157{
1158 return __gfn_to_hva_many(slot, gfn, nr_pages, true);
539cb660 1159}
48987781 1160
4d8b81ab 1161unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
7940876e 1162 gfn_t gfn)
4d8b81ab
XG
1163{
1164 return gfn_to_hva_many(slot, gfn, NULL);
1165}
1166EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
1167
48987781
XG
1168unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1169{
49c7754c 1170 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
48987781 1171}
0d150298 1172EXPORT_SYMBOL_GPL(gfn_to_hva);
539cb660 1173
86ab8cff 1174/*
ba6a3541
PB
1175 * If writable is set to false, the hva returned by this function is only
1176 * allowed to be read.
86ab8cff 1177 */
64d83126
CD
1178unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot,
1179 gfn_t gfn, bool *writable)
86ab8cff 1180{
a2ac07fe
GN
1181 unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
1182
1183 if (!kvm_is_error_hva(hva) && writable)
ba6a3541
PB
1184 *writable = !memslot_is_readonly(slot);
1185
a2ac07fe 1186 return hva;
86ab8cff
XG
1187}
1188
64d83126
CD
1189unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
1190{
1191 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
1192
1193 return gfn_to_hva_memslot_prot(slot, gfn, writable);
1194}
1195
86ab8cff 1196static int kvm_read_hva(void *data, void __user *hva, int len)
8030089f 1197{
86ab8cff
XG
1198 return __copy_from_user(data, hva, len);
1199}
1200
1201static int kvm_read_hva_atomic(void *data, void __user *hva, int len)
1202{
1203 return __copy_from_user_inatomic(data, hva, len);
8030089f
GN
1204}
1205
39369f7a 1206static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
0857b9e9
GN
1207 unsigned long start, int write, struct page **page)
1208{
1209 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1210
1211 if (write)
1212 flags |= FOLL_WRITE;
1213
1214 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1215}
1216
234b239b
ALC
1217int kvm_get_user_page_io(struct task_struct *tsk, struct mm_struct *mm,
1218 unsigned long addr, bool write_fault,
1219 struct page **pagep)
1220{
1221 int npages;
1222 int locked = 1;
1223 int flags = FOLL_TOUCH | FOLL_HWPOISON |
1224 (pagep ? FOLL_GET : 0) |
1225 (write_fault ? FOLL_WRITE : 0);
1226
1227 /*
1228 * If retrying the fault, we get here *not* having allowed the filemap
1229 * to wait on the page lock. We should now allow waiting on the IO with
1230 * the mmap semaphore released.
1231 */
1232 down_read(&mm->mmap_sem);
1233 npages = __get_user_pages(tsk, mm, addr, 1, flags, pagep, NULL,
1234 &locked);
1235 if (!locked) {
bb0ca6ac 1236 VM_BUG_ON(npages);
234b239b
ALC
1237
1238 if (!pagep)
1239 return 0;
1240
1241 /*
1242 * The previous call has now waited on the IO. Now we can
1243 * retry and complete. Pass TRIED to ensure we do not re
1244 * schedule async IO (see e.g. filemap_fault).
1245 */
1246 down_read(&mm->mmap_sem);
1247 npages = __get_user_pages(tsk, mm, addr, 1, flags | FOLL_TRIED,
1248 pagep, NULL, NULL);
1249 }
1250 up_read(&mm->mmap_sem);
1251 return npages;
1252}
1253
fafc3dba
HY
1254static inline int check_user_page_hwpoison(unsigned long addr)
1255{
1256 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1257
1258 rc = __get_user_pages(current, current->mm, addr, 1,
1259 flags, NULL, NULL, NULL);
1260 return rc == -EHWPOISON;
1261}
1262
2fc84311
XG
1263/*
1264 * The atomic path to get the writable pfn which will be stored in @pfn,
1265 * true indicates success, otherwise false is returned.
1266 */
1267static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
1268 bool write_fault, bool *writable, pfn_t *pfn)
954bbbc2 1269{
8d4e1288 1270 struct page *page[1];
2fc84311 1271 int npages;
954bbbc2 1272
2fc84311
XG
1273 if (!(async || atomic))
1274 return false;
af585b92 1275
12ce13fe
XG
1276 /*
1277 * Fast pin a writable pfn only if it is a write fault request
1278 * or the caller allows to map a writable pfn for a read fault
1279 * request.
1280 */
1281 if (!(write_fault || writable))
1282 return false;
612819c3 1283
2fc84311
XG
1284 npages = __get_user_pages_fast(addr, 1, 1, page);
1285 if (npages == 1) {
1286 *pfn = page_to_pfn(page[0]);
612819c3 1287
2fc84311
XG
1288 if (writable)
1289 *writable = true;
1290 return true;
1291 }
af585b92 1292
2fc84311
XG
1293 return false;
1294}
612819c3 1295
2fc84311
XG
1296/*
1297 * The slow path to get the pfn of the specified host virtual address,
1298 * 1 indicates success, -errno is returned if error is detected.
1299 */
1300static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
1301 bool *writable, pfn_t *pfn)
1302{
1303 struct page *page[1];
1304 int npages = 0;
612819c3 1305
2fc84311
XG
1306 might_sleep();
1307
1308 if (writable)
1309 *writable = write_fault;
1310
1311 if (async) {
1312 down_read(&current->mm->mmap_sem);
1313 npages = get_user_page_nowait(current, current->mm,
1314 addr, write_fault, page);
1315 up_read(&current->mm->mmap_sem);
234b239b
ALC
1316 } else {
1317 /*
1318 * By now we have tried gup_fast, and possibly async_pf, and we
1319 * are certainly not atomic. Time to retry the gup, allowing
1320 * mmap semaphore to be relinquished in the case of IO.
1321 */
1322 npages = kvm_get_user_page_io(current, current->mm, addr,
1323 write_fault, page);
1324 }
2fc84311
XG
1325 if (npages != 1)
1326 return npages;
1327
1328 /* map read fault as writable if possible */
12ce13fe 1329 if (unlikely(!write_fault) && writable) {
2fc84311
XG
1330 struct page *wpage[1];
1331
1332 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1333 if (npages == 1) {
1334 *writable = true;
1335 put_page(page[0]);
1336 page[0] = wpage[0];
612819c3 1337 }
2fc84311
XG
1338
1339 npages = 1;
887c08ac 1340 }
2fc84311
XG
1341 *pfn = page_to_pfn(page[0]);
1342 return npages;
1343}
539cb660 1344
4d8b81ab
XG
1345static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
1346{
1347 if (unlikely(!(vma->vm_flags & VM_READ)))
1348 return false;
2e2e3738 1349
4d8b81ab
XG
1350 if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
1351 return false;
887c08ac 1352
4d8b81ab
XG
1353 return true;
1354}
bf998156 1355
12ce13fe
XG
1356/*
1357 * Pin guest page in memory and return its pfn.
1358 * @addr: host virtual address which maps memory to the guest
1359 * @atomic: whether this function can sleep
1360 * @async: whether this function need to wait IO complete if the
1361 * host page is not in the memory
1362 * @write_fault: whether we should get a writable host page
1363 * @writable: whether it allows to map a writable host page for !@write_fault
1364 *
1365 * The function will map a writable host page for these two cases:
1366 * 1): @write_fault = true
1367 * 2): @write_fault = false && @writable, @writable will tell the caller
1368 * whether the mapping is writable.
1369 */
2fc84311
XG
1370static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
1371 bool write_fault, bool *writable)
1372{
1373 struct vm_area_struct *vma;
1374 pfn_t pfn = 0;
1375 int npages;
2e2e3738 1376
2fc84311
XG
1377 /* we can do it either atomically or asynchronously, not both */
1378 BUG_ON(atomic && async);
8d4e1288 1379
2fc84311
XG
1380 if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
1381 return pfn;
1382
1383 if (atomic)
1384 return KVM_PFN_ERR_FAULT;
1385
1386 npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
1387 if (npages == 1)
1388 return pfn;
8d4e1288 1389
2fc84311
XG
1390 down_read(&current->mm->mmap_sem);
1391 if (npages == -EHWPOISON ||
1392 (!async && check_user_page_hwpoison(addr))) {
1393 pfn = KVM_PFN_ERR_HWPOISON;
1394 goto exit;
1395 }
1396
1397 vma = find_vma_intersection(current->mm, addr, addr + 1);
1398
1399 if (vma == NULL)
1400 pfn = KVM_PFN_ERR_FAULT;
1401 else if ((vma->vm_flags & VM_PFNMAP)) {
1402 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1403 vma->vm_pgoff;
bf4bea8e 1404 BUG_ON(!kvm_is_reserved_pfn(pfn));
2fc84311 1405 } else {
4d8b81ab 1406 if (async && vma_is_valid(vma, write_fault))
2fc84311
XG
1407 *async = true;
1408 pfn = KVM_PFN_ERR_FAULT;
1409 }
1410exit:
1411 up_read(&current->mm->mmap_sem);
2e2e3738 1412 return pfn;
35149e21
AL
1413}
1414
4d8b81ab
XG
1415static pfn_t
1416__gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic,
1417 bool *async, bool write_fault, bool *writable)
887c08ac 1418{
4d8b81ab
XG
1419 unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
1420
1421 if (addr == KVM_HVA_ERR_RO_BAD)
1422 return KVM_PFN_ERR_RO_FAULT;
1423
1424 if (kvm_is_error_hva(addr))
81c52c56 1425 return KVM_PFN_NOSLOT;
4d8b81ab
XG
1426
1427 /* Do not map writable pfn in the readonly memslot. */
1428 if (writable && memslot_is_readonly(slot)) {
1429 *writable = false;
1430 writable = NULL;
1431 }
1432
1433 return hva_to_pfn(addr, atomic, async, write_fault,
1434 writable);
887c08ac 1435}
887c08ac 1436
612819c3
MT
1437static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1438 bool write_fault, bool *writable)
506f0d6f 1439{
4d8b81ab 1440 struct kvm_memory_slot *slot;
506f0d6f 1441
af585b92
GN
1442 if (async)
1443 *async = false;
1444
4d8b81ab 1445 slot = gfn_to_memslot(kvm, gfn);
506f0d6f 1446
4d8b81ab
XG
1447 return __gfn_to_pfn_memslot(slot, gfn, atomic, async, write_fault,
1448 writable);
365fb3fd
XG
1449}
1450
1451pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1452{
612819c3 1453 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
365fb3fd
XG
1454}
1455EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1456
612819c3
MT
1457pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1458 bool write_fault, bool *writable)
af585b92 1459{
612819c3 1460 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
af585b92
GN
1461}
1462EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1463
365fb3fd
XG
1464pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1465{
612819c3 1466 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
506f0d6f 1467}
35149e21
AL
1468EXPORT_SYMBOL_GPL(gfn_to_pfn);
1469
612819c3
MT
1470pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1471 bool *writable)
1472{
1473 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1474}
1475EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1476
d5661048 1477pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
506f0d6f 1478{
4d8b81ab 1479 return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
506f0d6f
MT
1480}
1481
037d92dc 1482pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
506f0d6f 1483{
4d8b81ab 1484 return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
506f0d6f 1485}
037d92dc 1486EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
506f0d6f 1487
48987781
XG
1488int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1489 int nr_pages)
1490{
1491 unsigned long addr;
1492 gfn_t entry;
1493
49c7754c 1494 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
48987781
XG
1495 if (kvm_is_error_hva(addr))
1496 return -1;
1497
1498 if (entry < nr_pages)
1499 return 0;
1500
1501 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1502}
1503EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1504
a2766325
XG
1505static struct page *kvm_pfn_to_page(pfn_t pfn)
1506{
81c52c56 1507 if (is_error_noslot_pfn(pfn))
cb9aaa30 1508 return KVM_ERR_PTR_BAD_PAGE;
a2766325 1509
bf4bea8e 1510 if (kvm_is_reserved_pfn(pfn)) {
cb9aaa30 1511 WARN_ON(1);
6cede2e6 1512 return KVM_ERR_PTR_BAD_PAGE;
cb9aaa30 1513 }
a2766325
XG
1514
1515 return pfn_to_page(pfn);
1516}
1517
35149e21
AL
1518struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1519{
2e2e3738
AL
1520 pfn_t pfn;
1521
1522 pfn = gfn_to_pfn(kvm, gfn);
2e2e3738 1523
a2766325 1524 return kvm_pfn_to_page(pfn);
954bbbc2 1525}
aab61cc0 1526
954bbbc2
AK
1527EXPORT_SYMBOL_GPL(gfn_to_page);
1528
b4231d61
IE
1529void kvm_release_page_clean(struct page *page)
1530{
32cad84f
XG
1531 WARN_ON(is_error_page(page));
1532
35149e21 1533 kvm_release_pfn_clean(page_to_pfn(page));
b4231d61
IE
1534}
1535EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1536
35149e21
AL
1537void kvm_release_pfn_clean(pfn_t pfn)
1538{
bf4bea8e 1539 if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn))
2e2e3738 1540 put_page(pfn_to_page(pfn));
35149e21
AL
1541}
1542EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1543
b4231d61 1544void kvm_release_page_dirty(struct page *page)
8a7ae055 1545{
a2766325
XG
1546 WARN_ON(is_error_page(page));
1547
35149e21
AL
1548 kvm_release_pfn_dirty(page_to_pfn(page));
1549}
1550EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1551
7940876e 1552static void kvm_release_pfn_dirty(pfn_t pfn)
35149e21
AL
1553{
1554 kvm_set_pfn_dirty(pfn);
1555 kvm_release_pfn_clean(pfn);
1556}
35149e21
AL
1557
1558void kvm_set_pfn_dirty(pfn_t pfn)
1559{
bf4bea8e 1560 if (!kvm_is_reserved_pfn(pfn)) {
2e2e3738
AL
1561 struct page *page = pfn_to_page(pfn);
1562 if (!PageReserved(page))
1563 SetPageDirty(page);
1564 }
8a7ae055 1565}
35149e21
AL
1566EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1567
1568void kvm_set_pfn_accessed(pfn_t pfn)
1569{
bf4bea8e 1570 if (!kvm_is_reserved_pfn(pfn))
2e2e3738 1571 mark_page_accessed(pfn_to_page(pfn));
35149e21
AL
1572}
1573EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1574
1575void kvm_get_pfn(pfn_t pfn)
1576{
bf4bea8e 1577 if (!kvm_is_reserved_pfn(pfn))
2e2e3738 1578 get_page(pfn_to_page(pfn));
35149e21
AL
1579}
1580EXPORT_SYMBOL_GPL(kvm_get_pfn);
8a7ae055 1581
195aefde
IE
1582static int next_segment(unsigned long len, int offset)
1583{
1584 if (len > PAGE_SIZE - offset)
1585 return PAGE_SIZE - offset;
1586 else
1587 return len;
1588}
1589
1590int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1591 int len)
1592{
e0506bcb
IE
1593 int r;
1594 unsigned long addr;
195aefde 1595
ba6a3541 1596 addr = gfn_to_hva_prot(kvm, gfn, NULL);
e0506bcb
IE
1597 if (kvm_is_error_hva(addr))
1598 return -EFAULT;
86ab8cff 1599 r = kvm_read_hva(data, (void __user *)addr + offset, len);
e0506bcb 1600 if (r)
195aefde 1601 return -EFAULT;
195aefde
IE
1602 return 0;
1603}
1604EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1605
1606int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1607{
1608 gfn_t gfn = gpa >> PAGE_SHIFT;
1609 int seg;
1610 int offset = offset_in_page(gpa);
1611 int ret;
1612
1613 while ((seg = next_segment(len, offset)) != 0) {
1614 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1615 if (ret < 0)
1616 return ret;
1617 offset = 0;
1618 len -= seg;
1619 data += seg;
1620 ++gfn;
1621 }
1622 return 0;
1623}
1624EXPORT_SYMBOL_GPL(kvm_read_guest);
1625
7ec54588
MT
1626int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1627 unsigned long len)
1628{
1629 int r;
1630 unsigned long addr;
1631 gfn_t gfn = gpa >> PAGE_SHIFT;
1632 int offset = offset_in_page(gpa);
1633
ba6a3541 1634 addr = gfn_to_hva_prot(kvm, gfn, NULL);
7ec54588
MT
1635 if (kvm_is_error_hva(addr))
1636 return -EFAULT;
0aac03f0 1637 pagefault_disable();
86ab8cff 1638 r = kvm_read_hva_atomic(data, (void __user *)addr + offset, len);
0aac03f0 1639 pagefault_enable();
7ec54588
MT
1640 if (r)
1641 return -EFAULT;
1642 return 0;
1643}
1644EXPORT_SYMBOL(kvm_read_guest_atomic);
1645
195aefde
IE
1646int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1647 int offset, int len)
1648{
e0506bcb
IE
1649 int r;
1650 unsigned long addr;
195aefde 1651
e0506bcb
IE
1652 addr = gfn_to_hva(kvm, gfn);
1653 if (kvm_is_error_hva(addr))
1654 return -EFAULT;
8b0cedff 1655 r = __copy_to_user((void __user *)addr + offset, data, len);
e0506bcb 1656 if (r)
195aefde 1657 return -EFAULT;
195aefde
IE
1658 mark_page_dirty(kvm, gfn);
1659 return 0;
1660}
1661EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1662
1663int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1664 unsigned long len)
1665{
1666 gfn_t gfn = gpa >> PAGE_SHIFT;
1667 int seg;
1668 int offset = offset_in_page(gpa);
1669 int ret;
1670
1671 while ((seg = next_segment(len, offset)) != 0) {
1672 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1673 if (ret < 0)
1674 return ret;
1675 offset = 0;
1676 len -= seg;
1677 data += seg;
1678 ++gfn;
1679 }
1680 return 0;
1681}
ff651cb6 1682EXPORT_SYMBOL_GPL(kvm_write_guest);
195aefde 1683
49c7754c 1684int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
8f964525 1685 gpa_t gpa, unsigned long len)
49c7754c
GN
1686{
1687 struct kvm_memslots *slots = kvm_memslots(kvm);
1688 int offset = offset_in_page(gpa);
8f964525
AH
1689 gfn_t start_gfn = gpa >> PAGE_SHIFT;
1690 gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
1691 gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
1692 gfn_t nr_pages_avail;
49c7754c
GN
1693
1694 ghc->gpa = gpa;
1695 ghc->generation = slots->generation;
8f964525
AH
1696 ghc->len = len;
1697 ghc->memslot = gfn_to_memslot(kvm, start_gfn);
1698 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, &nr_pages_avail);
1699 if (!kvm_is_error_hva(ghc->hva) && nr_pages_avail >= nr_pages_needed) {
49c7754c 1700 ghc->hva += offset;
8f964525
AH
1701 } else {
1702 /*
1703 * If the requested region crosses two memslots, we still
1704 * verify that the entire region is valid here.
1705 */
1706 while (start_gfn <= end_gfn) {
1707 ghc->memslot = gfn_to_memslot(kvm, start_gfn);
1708 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
1709 &nr_pages_avail);
1710 if (kvm_is_error_hva(ghc->hva))
1711 return -EFAULT;
1712 start_gfn += nr_pages_avail;
1713 }
1714 /* Use the slow path for cross page reads and writes. */
1715 ghc->memslot = NULL;
1716 }
49c7754c
GN
1717 return 0;
1718}
1719EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1720
1721int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1722 void *data, unsigned long len)
1723{
1724 struct kvm_memslots *slots = kvm_memslots(kvm);
1725 int r;
1726
8f964525
AH
1727 BUG_ON(len > ghc->len);
1728
49c7754c 1729 if (slots->generation != ghc->generation)
8f964525
AH
1730 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
1731
1732 if (unlikely(!ghc->memslot))
1733 return kvm_write_guest(kvm, ghc->gpa, data, len);
49c7754c
GN
1734
1735 if (kvm_is_error_hva(ghc->hva))
1736 return -EFAULT;
1737
8b0cedff 1738 r = __copy_to_user((void __user *)ghc->hva, data, len);
49c7754c
GN
1739 if (r)
1740 return -EFAULT;
1741 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1742
1743 return 0;
1744}
1745EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1746
e03b644f
GN
1747int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1748 void *data, unsigned long len)
1749{
1750 struct kvm_memslots *slots = kvm_memslots(kvm);
1751 int r;
1752
8f964525
AH
1753 BUG_ON(len > ghc->len);
1754
e03b644f 1755 if (slots->generation != ghc->generation)
8f964525
AH
1756 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
1757
1758 if (unlikely(!ghc->memslot))
1759 return kvm_read_guest(kvm, ghc->gpa, data, len);
e03b644f
GN
1760
1761 if (kvm_is_error_hva(ghc->hva))
1762 return -EFAULT;
1763
1764 r = __copy_from_user(data, (void __user *)ghc->hva, len);
1765 if (r)
1766 return -EFAULT;
1767
1768 return 0;
1769}
1770EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
1771
195aefde
IE
1772int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1773{
8a3caa6d
HC
1774 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
1775
1776 return kvm_write_guest_page(kvm, gfn, zero_page, offset, len);
195aefde
IE
1777}
1778EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1779
1780int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1781{
1782 gfn_t gfn = gpa >> PAGE_SHIFT;
1783 int seg;
1784 int offset = offset_in_page(gpa);
1785 int ret;
1786
1787 while ((seg = next_segment(len, offset)) != 0) {
1788 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1789 if (ret < 0)
1790 return ret;
1791 offset = 0;
1792 len -= seg;
1793 ++gfn;
1794 }
1795 return 0;
1796}
1797EXPORT_SYMBOL_GPL(kvm_clear_guest);
1798
7940876e
SH
1799static void mark_page_dirty_in_slot(struct kvm *kvm,
1800 struct kvm_memory_slot *memslot,
1801 gfn_t gfn)
6aa8b732 1802{
7e9d619d
RR
1803 if (memslot && memslot->dirty_bitmap) {
1804 unsigned long rel_gfn = gfn - memslot->base_gfn;
6aa8b732 1805
b74ca3b3 1806 set_bit_le(rel_gfn, memslot->dirty_bitmap);
6aa8b732
AK
1807 }
1808}
1809
49c7754c
GN
1810void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1811{
1812 struct kvm_memory_slot *memslot;
1813
1814 memslot = gfn_to_memslot(kvm, gfn);
1815 mark_page_dirty_in_slot(kvm, memslot, gfn);
1816}
2ba9f0d8 1817EXPORT_SYMBOL_GPL(mark_page_dirty);
49c7754c 1818
f7819512
PB
1819static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
1820{
1821 if (kvm_arch_vcpu_runnable(vcpu)) {
1822 kvm_make_request(KVM_REQ_UNHALT, vcpu);
1823 return -EINTR;
1824 }
1825 if (kvm_cpu_has_pending_timer(vcpu))
1826 return -EINTR;
1827 if (signal_pending(current))
1828 return -EINTR;
1829
1830 return 0;
1831}
1832
b6958ce4
ED
1833/*
1834 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1835 */
8776e519 1836void kvm_vcpu_block(struct kvm_vcpu *vcpu)
d3bef15f 1837{
f7819512 1838 ktime_t start, cur;
e5c239cf 1839 DEFINE_WAIT(wait);
f7819512
PB
1840 bool waited = false;
1841
1842 start = cur = ktime_get();
1843 if (halt_poll_ns) {
1844 ktime_t stop = ktime_add_ns(ktime_get(), halt_poll_ns);
1845 do {
1846 /*
1847 * This sets KVM_REQ_UNHALT if an interrupt
1848 * arrives.
1849 */
1850 if (kvm_vcpu_check_block(vcpu) < 0) {
1851 ++vcpu->stat.halt_successful_poll;
1852 goto out;
1853 }
1854 cur = ktime_get();
1855 } while (single_task_running() && ktime_before(cur, stop));
1856 }
e5c239cf
MT
1857
1858 for (;;) {
1859 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1860
f7819512 1861 if (kvm_vcpu_check_block(vcpu) < 0)
e5c239cf
MT
1862 break;
1863
f7819512 1864 waited = true;
b6958ce4 1865 schedule();
b6958ce4 1866 }
d3bef15f 1867
e5c239cf 1868 finish_wait(&vcpu->wq, &wait);
f7819512
PB
1869 cur = ktime_get();
1870
1871out:
1872 trace_kvm_vcpu_wakeup(ktime_to_ns(cur) - ktime_to_ns(start), waited);
b6958ce4 1873}
2ba9f0d8 1874EXPORT_SYMBOL_GPL(kvm_vcpu_block);
b6958ce4 1875
8c84780d 1876#ifndef CONFIG_S390
b6d33834
CD
1877/*
1878 * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
1879 */
1880void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1881{
1882 int me;
1883 int cpu = vcpu->cpu;
1884 wait_queue_head_t *wqp;
1885
1886 wqp = kvm_arch_vcpu_wq(vcpu);
1887 if (waitqueue_active(wqp)) {
1888 wake_up_interruptible(wqp);
1889 ++vcpu->stat.halt_wakeup;
1890 }
1891
1892 me = get_cpu();
1893 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
1894 if (kvm_arch_vcpu_should_kick(vcpu))
1895 smp_send_reschedule(cpu);
1896 put_cpu();
1897}
a20ed54d 1898EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
8c84780d 1899#endif /* !CONFIG_S390 */
b6d33834 1900
fa93384f 1901int kvm_vcpu_yield_to(struct kvm_vcpu *target)
41628d33
KW
1902{
1903 struct pid *pid;
1904 struct task_struct *task = NULL;
fa93384f 1905 int ret = 0;
41628d33
KW
1906
1907 rcu_read_lock();
1908 pid = rcu_dereference(target->pid);
1909 if (pid)
27fbe64b 1910 task = get_pid_task(pid, PIDTYPE_PID);
41628d33
KW
1911 rcu_read_unlock();
1912 if (!task)
c45c528e 1913 return ret;
c45c528e 1914 ret = yield_to(task, 1);
41628d33 1915 put_task_struct(task);
c45c528e
R
1916
1917 return ret;
41628d33
KW
1918}
1919EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
1920
06e48c51
R
1921/*
1922 * Helper that checks whether a VCPU is eligible for directed yield.
1923 * Most eligible candidate to yield is decided by following heuristics:
1924 *
1925 * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
1926 * (preempted lock holder), indicated by @in_spin_loop.
1927 * Set at the beiginning and cleared at the end of interception/PLE handler.
1928 *
1929 * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
1930 * chance last time (mostly it has become eligible now since we have probably
1931 * yielded to lockholder in last iteration. This is done by toggling
1932 * @dy_eligible each time a VCPU checked for eligibility.)
1933 *
1934 * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
1935 * to preempted lock-holder could result in wrong VCPU selection and CPU
1936 * burning. Giving priority for a potential lock-holder increases lock
1937 * progress.
1938 *
1939 * Since algorithm is based on heuristics, accessing another VCPU data without
1940 * locking does not harm. It may result in trying to yield to same VCPU, fail
1941 * and continue with next VCPU and so on.
1942 */
7940876e 1943static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
06e48c51 1944{
4a55dd72 1945#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
06e48c51
R
1946 bool eligible;
1947
1948 eligible = !vcpu->spin_loop.in_spin_loop ||
34656113 1949 vcpu->spin_loop.dy_eligible;
06e48c51
R
1950
1951 if (vcpu->spin_loop.in_spin_loop)
1952 kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
1953
1954 return eligible;
4a55dd72
SW
1955#else
1956 return true;
06e48c51 1957#endif
4a55dd72 1958}
c45c528e 1959
217ece61 1960void kvm_vcpu_on_spin(struct kvm_vcpu *me)
d255f4f2 1961{
217ece61
RR
1962 struct kvm *kvm = me->kvm;
1963 struct kvm_vcpu *vcpu;
1964 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1965 int yielded = 0;
c45c528e 1966 int try = 3;
217ece61
RR
1967 int pass;
1968 int i;
d255f4f2 1969
4c088493 1970 kvm_vcpu_set_in_spin_loop(me, true);
217ece61
RR
1971 /*
1972 * We boost the priority of a VCPU that is runnable but not
1973 * currently running, because it got preempted by something
1974 * else and called schedule in __vcpu_run. Hopefully that
1975 * VCPU is holding the lock that we need and will release it.
1976 * We approximate round-robin by starting at the last boosted VCPU.
1977 */
c45c528e 1978 for (pass = 0; pass < 2 && !yielded && try; pass++) {
217ece61 1979 kvm_for_each_vcpu(i, vcpu, kvm) {
5cfc2aab 1980 if (!pass && i <= last_boosted_vcpu) {
217ece61
RR
1981 i = last_boosted_vcpu;
1982 continue;
1983 } else if (pass && i > last_boosted_vcpu)
1984 break;
7bc7ae25
R
1985 if (!ACCESS_ONCE(vcpu->preempted))
1986 continue;
217ece61
RR
1987 if (vcpu == me)
1988 continue;
98f4a146 1989 if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
217ece61 1990 continue;
06e48c51
R
1991 if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
1992 continue;
c45c528e
R
1993
1994 yielded = kvm_vcpu_yield_to(vcpu);
1995 if (yielded > 0) {
217ece61 1996 kvm->last_boosted_vcpu = i;
217ece61 1997 break;
c45c528e
R
1998 } else if (yielded < 0) {
1999 try--;
2000 if (!try)
2001 break;
217ece61 2002 }
217ece61
RR
2003 }
2004 }
4c088493 2005 kvm_vcpu_set_in_spin_loop(me, false);
06e48c51
R
2006
2007 /* Ensure vcpu is not eligible during next spinloop */
2008 kvm_vcpu_set_dy_eligible(me, false);
d255f4f2
ZE
2009}
2010EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
2011
e4a533a4 2012static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
9a2bb7f4
AK
2013{
2014 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
9a2bb7f4
AK
2015 struct page *page;
2016
e4a533a4 2017 if (vmf->pgoff == 0)
039576c0 2018 page = virt_to_page(vcpu->run);
09566765 2019#ifdef CONFIG_X86
e4a533a4 2020 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
ad312c7c 2021 page = virt_to_page(vcpu->arch.pio_data);
5f94c174
LV
2022#endif
2023#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2024 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
2025 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
09566765 2026#endif
039576c0 2027 else
5b1c1493 2028 return kvm_arch_vcpu_fault(vcpu, vmf);
9a2bb7f4 2029 get_page(page);
e4a533a4 2030 vmf->page = page;
2031 return 0;
9a2bb7f4
AK
2032}
2033
f0f37e2f 2034static const struct vm_operations_struct kvm_vcpu_vm_ops = {
e4a533a4 2035 .fault = kvm_vcpu_fault,
9a2bb7f4
AK
2036};
2037
2038static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2039{
2040 vma->vm_ops = &kvm_vcpu_vm_ops;
2041 return 0;
2042}
2043
bccf2150
AK
2044static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2045{
2046 struct kvm_vcpu *vcpu = filp->private_data;
2047
66c0b394 2048 kvm_put_kvm(vcpu->kvm);
bccf2150
AK
2049 return 0;
2050}
2051
3d3aab1b 2052static struct file_operations kvm_vcpu_fops = {
bccf2150
AK
2053 .release = kvm_vcpu_release,
2054 .unlocked_ioctl = kvm_vcpu_ioctl,
1dda606c
AG
2055#ifdef CONFIG_COMPAT
2056 .compat_ioctl = kvm_vcpu_compat_ioctl,
2057#endif
9a2bb7f4 2058 .mmap = kvm_vcpu_mmap,
6038f373 2059 .llseek = noop_llseek,
bccf2150
AK
2060};
2061
2062/*
2063 * Allocates an inode for the vcpu.
2064 */
2065static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2066{
24009b05 2067 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
bccf2150
AK
2068}
2069
c5ea7660
AK
2070/*
2071 * Creates some virtual cpus. Good luck creating more than one.
2072 */
73880c80 2073static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
c5ea7660
AK
2074{
2075 int r;
988a2cae 2076 struct kvm_vcpu *vcpu, *v;
c5ea7660 2077
338c7dba
AH
2078 if (id >= KVM_MAX_VCPUS)
2079 return -EINVAL;
2080
73880c80 2081 vcpu = kvm_arch_vcpu_create(kvm, id);
fb3f0f51
RR
2082 if (IS_ERR(vcpu))
2083 return PTR_ERR(vcpu);
c5ea7660 2084
15ad7146
AK
2085 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2086
26e5215f
AK
2087 r = kvm_arch_vcpu_setup(vcpu);
2088 if (r)
d780592b 2089 goto vcpu_destroy;
26e5215f 2090
11ec2804 2091 mutex_lock(&kvm->lock);
3e515705
AK
2092 if (!kvm_vcpu_compatible(vcpu)) {
2093 r = -EINVAL;
2094 goto unlock_vcpu_destroy;
2095 }
73880c80
GN
2096 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
2097 r = -EINVAL;
d780592b 2098 goto unlock_vcpu_destroy;
fb3f0f51 2099 }
73880c80 2100
988a2cae
GN
2101 kvm_for_each_vcpu(r, v, kvm)
2102 if (v->vcpu_id == id) {
73880c80 2103 r = -EEXIST;
d780592b 2104 goto unlock_vcpu_destroy;
73880c80
GN
2105 }
2106
2107 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
c5ea7660 2108
fb3f0f51 2109 /* Now it's all set up, let userspace reach it */
66c0b394 2110 kvm_get_kvm(kvm);
bccf2150 2111 r = create_vcpu_fd(vcpu);
73880c80
GN
2112 if (r < 0) {
2113 kvm_put_kvm(kvm);
d780592b 2114 goto unlock_vcpu_destroy;
73880c80
GN
2115 }
2116
2117 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
2118 smp_wmb();
2119 atomic_inc(&kvm->online_vcpus);
2120
73880c80 2121 mutex_unlock(&kvm->lock);
42897d86 2122 kvm_arch_vcpu_postcreate(vcpu);
fb3f0f51 2123 return r;
39c3b86e 2124
d780592b 2125unlock_vcpu_destroy:
7d8fece6 2126 mutex_unlock(&kvm->lock);
d780592b 2127vcpu_destroy:
d40ccc62 2128 kvm_arch_vcpu_destroy(vcpu);
c5ea7660
AK
2129 return r;
2130}
2131
1961d276
AK
2132static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2133{
2134 if (sigset) {
2135 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2136 vcpu->sigset_active = 1;
2137 vcpu->sigset = *sigset;
2138 } else
2139 vcpu->sigset_active = 0;
2140 return 0;
2141}
2142
bccf2150
AK
2143static long kvm_vcpu_ioctl(struct file *filp,
2144 unsigned int ioctl, unsigned long arg)
6aa8b732 2145{
bccf2150 2146 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 2147 void __user *argp = (void __user *)arg;
313a3dc7 2148 int r;
fa3795a7
DH
2149 struct kvm_fpu *fpu = NULL;
2150 struct kvm_sregs *kvm_sregs = NULL;
6aa8b732 2151
6d4e4c4f
AK
2152 if (vcpu->kvm->mm != current->mm)
2153 return -EIO;
2122ff5e 2154
2ea75be3
DM
2155 if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
2156 return -EINVAL;
2157
2f4d9b54 2158#if defined(CONFIG_S390) || defined(CONFIG_PPC) || defined(CONFIG_MIPS)
2122ff5e
AK
2159 /*
2160 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
2161 * so vcpu_load() would break it.
2162 */
2163 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
2164 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
2165#endif
2166
2167
9fc77441
MT
2168 r = vcpu_load(vcpu);
2169 if (r)
2170 return r;
6aa8b732 2171 switch (ioctl) {
9a2bb7f4 2172 case KVM_RUN:
f0fe5108
AK
2173 r = -EINVAL;
2174 if (arg)
2175 goto out;
7a72f7a1
CB
2176 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
2177 /* The thread running this VCPU changed. */
2178 struct pid *oldpid = vcpu->pid;
2179 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
2180 rcu_assign_pointer(vcpu->pid, newpid);
2181 if (oldpid)
2182 synchronize_rcu();
2183 put_pid(oldpid);
2184 }
b6c7a5dc 2185 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
64be5007 2186 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
6aa8b732 2187 break;
6aa8b732 2188 case KVM_GET_REGS: {
3e4bb3ac 2189 struct kvm_regs *kvm_regs;
6aa8b732 2190
3e4bb3ac
XZ
2191 r = -ENOMEM;
2192 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
2193 if (!kvm_regs)
6aa8b732 2194 goto out;
3e4bb3ac
XZ
2195 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
2196 if (r)
2197 goto out_free1;
6aa8b732 2198 r = -EFAULT;
3e4bb3ac
XZ
2199 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
2200 goto out_free1;
6aa8b732 2201 r = 0;
3e4bb3ac
XZ
2202out_free1:
2203 kfree(kvm_regs);
6aa8b732
AK
2204 break;
2205 }
2206 case KVM_SET_REGS: {
3e4bb3ac 2207 struct kvm_regs *kvm_regs;
6aa8b732 2208
3e4bb3ac 2209 r = -ENOMEM;
ff5c2c03
SL
2210 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
2211 if (IS_ERR(kvm_regs)) {
2212 r = PTR_ERR(kvm_regs);
6aa8b732 2213 goto out;
ff5c2c03 2214 }
3e4bb3ac 2215 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
3e4bb3ac 2216 kfree(kvm_regs);
6aa8b732
AK
2217 break;
2218 }
2219 case KVM_GET_SREGS: {
fa3795a7
DH
2220 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
2221 r = -ENOMEM;
2222 if (!kvm_sregs)
2223 goto out;
2224 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
6aa8b732
AK
2225 if (r)
2226 goto out;
2227 r = -EFAULT;
fa3795a7 2228 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
6aa8b732
AK
2229 goto out;
2230 r = 0;
2231 break;
2232 }
2233 case KVM_SET_SREGS: {
ff5c2c03
SL
2234 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
2235 if (IS_ERR(kvm_sregs)) {
2236 r = PTR_ERR(kvm_sregs);
18595411 2237 kvm_sregs = NULL;
6aa8b732 2238 goto out;
ff5c2c03 2239 }
fa3795a7 2240 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
6aa8b732
AK
2241 break;
2242 }
62d9f0db
MT
2243 case KVM_GET_MP_STATE: {
2244 struct kvm_mp_state mp_state;
2245
2246 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
2247 if (r)
2248 goto out;
2249 r = -EFAULT;
2250 if (copy_to_user(argp, &mp_state, sizeof mp_state))
2251 goto out;
2252 r = 0;
2253 break;
2254 }
2255 case KVM_SET_MP_STATE: {
2256 struct kvm_mp_state mp_state;
2257
2258 r = -EFAULT;
2259 if (copy_from_user(&mp_state, argp, sizeof mp_state))
2260 goto out;
2261 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
62d9f0db
MT
2262 break;
2263 }
6aa8b732
AK
2264 case KVM_TRANSLATE: {
2265 struct kvm_translation tr;
2266
2267 r = -EFAULT;
2f366987 2268 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 2269 goto out;
8b006791 2270 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
2271 if (r)
2272 goto out;
2273 r = -EFAULT;
2f366987 2274 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
2275 goto out;
2276 r = 0;
2277 break;
2278 }
d0bfb940
JK
2279 case KVM_SET_GUEST_DEBUG: {
2280 struct kvm_guest_debug dbg;
6aa8b732
AK
2281
2282 r = -EFAULT;
2f366987 2283 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 2284 goto out;
d0bfb940 2285 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
6aa8b732
AK
2286 break;
2287 }
1961d276
AK
2288 case KVM_SET_SIGNAL_MASK: {
2289 struct kvm_signal_mask __user *sigmask_arg = argp;
2290 struct kvm_signal_mask kvm_sigmask;
2291 sigset_t sigset, *p;
2292
2293 p = NULL;
2294 if (argp) {
2295 r = -EFAULT;
2296 if (copy_from_user(&kvm_sigmask, argp,
2297 sizeof kvm_sigmask))
2298 goto out;
2299 r = -EINVAL;
2300 if (kvm_sigmask.len != sizeof sigset)
2301 goto out;
2302 r = -EFAULT;
2303 if (copy_from_user(&sigset, sigmask_arg->sigset,
2304 sizeof sigset))
2305 goto out;
2306 p = &sigset;
2307 }
376d41ff 2308 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
1961d276
AK
2309 break;
2310 }
b8836737 2311 case KVM_GET_FPU: {
fa3795a7
DH
2312 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2313 r = -ENOMEM;
2314 if (!fpu)
2315 goto out;
2316 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
b8836737
AK
2317 if (r)
2318 goto out;
2319 r = -EFAULT;
fa3795a7 2320 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
b8836737
AK
2321 goto out;
2322 r = 0;
2323 break;
2324 }
2325 case KVM_SET_FPU: {
ff5c2c03
SL
2326 fpu = memdup_user(argp, sizeof(*fpu));
2327 if (IS_ERR(fpu)) {
2328 r = PTR_ERR(fpu);
18595411 2329 fpu = NULL;
b8836737 2330 goto out;
ff5c2c03 2331 }
fa3795a7 2332 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
b8836737
AK
2333 break;
2334 }
bccf2150 2335 default:
313a3dc7 2336 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
bccf2150
AK
2337 }
2338out:
2122ff5e 2339 vcpu_put(vcpu);
fa3795a7
DH
2340 kfree(fpu);
2341 kfree(kvm_sregs);
bccf2150
AK
2342 return r;
2343}
2344
1dda606c
AG
2345#ifdef CONFIG_COMPAT
2346static long kvm_vcpu_compat_ioctl(struct file *filp,
2347 unsigned int ioctl, unsigned long arg)
2348{
2349 struct kvm_vcpu *vcpu = filp->private_data;
2350 void __user *argp = compat_ptr(arg);
2351 int r;
2352
2353 if (vcpu->kvm->mm != current->mm)
2354 return -EIO;
2355
2356 switch (ioctl) {
2357 case KVM_SET_SIGNAL_MASK: {
2358 struct kvm_signal_mask __user *sigmask_arg = argp;
2359 struct kvm_signal_mask kvm_sigmask;
2360 compat_sigset_t csigset;
2361 sigset_t sigset;
2362
2363 if (argp) {
2364 r = -EFAULT;
2365 if (copy_from_user(&kvm_sigmask, argp,
2366 sizeof kvm_sigmask))
2367 goto out;
2368 r = -EINVAL;
2369 if (kvm_sigmask.len != sizeof csigset)
2370 goto out;
2371 r = -EFAULT;
2372 if (copy_from_user(&csigset, sigmask_arg->sigset,
2373 sizeof csigset))
2374 goto out;
760a9a30
AC
2375 sigset_from_compat(&sigset, &csigset);
2376 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2377 } else
2378 r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
1dda606c
AG
2379 break;
2380 }
2381 default:
2382 r = kvm_vcpu_ioctl(filp, ioctl, arg);
2383 }
2384
2385out:
2386 return r;
2387}
2388#endif
2389
852b6d57
SW
2390static int kvm_device_ioctl_attr(struct kvm_device *dev,
2391 int (*accessor)(struct kvm_device *dev,
2392 struct kvm_device_attr *attr),
2393 unsigned long arg)
2394{
2395 struct kvm_device_attr attr;
2396
2397 if (!accessor)
2398 return -EPERM;
2399
2400 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2401 return -EFAULT;
2402
2403 return accessor(dev, &attr);
2404}
2405
2406static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
2407 unsigned long arg)
2408{
2409 struct kvm_device *dev = filp->private_data;
2410
2411 switch (ioctl) {
2412 case KVM_SET_DEVICE_ATTR:
2413 return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
2414 case KVM_GET_DEVICE_ATTR:
2415 return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
2416 case KVM_HAS_DEVICE_ATTR:
2417 return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
2418 default:
2419 if (dev->ops->ioctl)
2420 return dev->ops->ioctl(dev, ioctl, arg);
2421
2422 return -ENOTTY;
2423 }
2424}
2425
852b6d57
SW
2426static int kvm_device_release(struct inode *inode, struct file *filp)
2427{
2428 struct kvm_device *dev = filp->private_data;
2429 struct kvm *kvm = dev->kvm;
2430
852b6d57
SW
2431 kvm_put_kvm(kvm);
2432 return 0;
2433}
2434
2435static const struct file_operations kvm_device_fops = {
2436 .unlocked_ioctl = kvm_device_ioctl,
db6ae615
SW
2437#ifdef CONFIG_COMPAT
2438 .compat_ioctl = kvm_device_ioctl,
2439#endif
852b6d57
SW
2440 .release = kvm_device_release,
2441};
2442
2443struct kvm_device *kvm_device_from_filp(struct file *filp)
2444{
2445 if (filp->f_op != &kvm_device_fops)
2446 return NULL;
2447
2448 return filp->private_data;
2449}
2450
d60eacb0 2451static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
5df554ad 2452#ifdef CONFIG_KVM_MPIC
d60eacb0
WD
2453 [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops,
2454 [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops,
5975a2e0 2455#endif
d60eacb0 2456
5975a2e0 2457#ifdef CONFIG_KVM_XICS
d60eacb0 2458 [KVM_DEV_TYPE_XICS] = &kvm_xics_ops,
ec53500f 2459#endif
d60eacb0
WD
2460};
2461
2462int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type)
2463{
2464 if (type >= ARRAY_SIZE(kvm_device_ops_table))
2465 return -ENOSPC;
2466
2467 if (kvm_device_ops_table[type] != NULL)
2468 return -EEXIST;
2469
2470 kvm_device_ops_table[type] = ops;
2471 return 0;
2472}
2473
571ee1b6
WL
2474void kvm_unregister_device_ops(u32 type)
2475{
2476 if (kvm_device_ops_table[type] != NULL)
2477 kvm_device_ops_table[type] = NULL;
2478}
2479
852b6d57
SW
2480static int kvm_ioctl_create_device(struct kvm *kvm,
2481 struct kvm_create_device *cd)
2482{
2483 struct kvm_device_ops *ops = NULL;
2484 struct kvm_device *dev;
2485 bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
2486 int ret;
2487
d60eacb0
WD
2488 if (cd->type >= ARRAY_SIZE(kvm_device_ops_table))
2489 return -ENODEV;
2490
2491 ops = kvm_device_ops_table[cd->type];
2492 if (ops == NULL)
852b6d57 2493 return -ENODEV;
852b6d57
SW
2494
2495 if (test)
2496 return 0;
2497
2498 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2499 if (!dev)
2500 return -ENOMEM;
2501
2502 dev->ops = ops;
2503 dev->kvm = kvm;
852b6d57
SW
2504
2505 ret = ops->create(dev, cd->type);
2506 if (ret < 0) {
2507 kfree(dev);
2508 return ret;
2509 }
2510
24009b05 2511 ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
852b6d57
SW
2512 if (ret < 0) {
2513 ops->destroy(dev);
2514 return ret;
2515 }
2516
07f0a7bd 2517 list_add(&dev->vm_node, &kvm->devices);
852b6d57
SW
2518 kvm_get_kvm(kvm);
2519 cd->fd = ret;
2520 return 0;
2521}
2522
92b591a4
AG
2523static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
2524{
2525 switch (arg) {
2526 case KVM_CAP_USER_MEMORY:
2527 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2528 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2529#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2530 case KVM_CAP_SET_BOOT_CPU_ID:
2531#endif
2532 case KVM_CAP_INTERNAL_ERROR_DATA:
2533#ifdef CONFIG_HAVE_KVM_MSI
2534 case KVM_CAP_SIGNAL_MSI:
2535#endif
297e2105 2536#ifdef CONFIG_HAVE_KVM_IRQFD
92b591a4
AG
2537 case KVM_CAP_IRQFD_RESAMPLE:
2538#endif
2539 case KVM_CAP_CHECK_EXTENSION_VM:
2540 return 1;
2541#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
2542 case KVM_CAP_IRQ_ROUTING:
2543 return KVM_MAX_IRQ_ROUTES;
2544#endif
2545 default:
2546 break;
2547 }
2548 return kvm_vm_ioctl_check_extension(kvm, arg);
2549}
2550
bccf2150
AK
2551static long kvm_vm_ioctl(struct file *filp,
2552 unsigned int ioctl, unsigned long arg)
2553{
2554 struct kvm *kvm = filp->private_data;
2555 void __user *argp = (void __user *)arg;
1fe779f8 2556 int r;
bccf2150 2557
6d4e4c4f
AK
2558 if (kvm->mm != current->mm)
2559 return -EIO;
bccf2150
AK
2560 switch (ioctl) {
2561 case KVM_CREATE_VCPU:
2562 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
bccf2150 2563 break;
6fc138d2
IE
2564 case KVM_SET_USER_MEMORY_REGION: {
2565 struct kvm_userspace_memory_region kvm_userspace_mem;
2566
2567 r = -EFAULT;
2568 if (copy_from_user(&kvm_userspace_mem, argp,
2569 sizeof kvm_userspace_mem))
2570 goto out;
2571
47ae31e2 2572 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
6aa8b732
AK
2573 break;
2574 }
2575 case KVM_GET_DIRTY_LOG: {
2576 struct kvm_dirty_log log;
2577
2578 r = -EFAULT;
2f366987 2579 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 2580 goto out;
2c6f5df9 2581 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
2582 break;
2583 }
5f94c174
LV
2584#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2585 case KVM_REGISTER_COALESCED_MMIO: {
2586 struct kvm_coalesced_mmio_zone zone;
2587 r = -EFAULT;
2588 if (copy_from_user(&zone, argp, sizeof zone))
2589 goto out;
5f94c174 2590 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
5f94c174
LV
2591 break;
2592 }
2593 case KVM_UNREGISTER_COALESCED_MMIO: {
2594 struct kvm_coalesced_mmio_zone zone;
2595 r = -EFAULT;
2596 if (copy_from_user(&zone, argp, sizeof zone))
2597 goto out;
5f94c174 2598 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
5f94c174
LV
2599 break;
2600 }
2601#endif
721eecbf
GH
2602 case KVM_IRQFD: {
2603 struct kvm_irqfd data;
2604
2605 r = -EFAULT;
2606 if (copy_from_user(&data, argp, sizeof data))
2607 goto out;
d4db2935 2608 r = kvm_irqfd(kvm, &data);
721eecbf
GH
2609 break;
2610 }
d34e6b17
GH
2611 case KVM_IOEVENTFD: {
2612 struct kvm_ioeventfd data;
2613
2614 r = -EFAULT;
2615 if (copy_from_user(&data, argp, sizeof data))
2616 goto out;
2617 r = kvm_ioeventfd(kvm, &data);
2618 break;
2619 }
73880c80
GN
2620#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2621 case KVM_SET_BOOT_CPU_ID:
2622 r = 0;
894a9c55 2623 mutex_lock(&kvm->lock);
73880c80
GN
2624 if (atomic_read(&kvm->online_vcpus) != 0)
2625 r = -EBUSY;
2626 else
2627 kvm->bsp_vcpu_id = arg;
894a9c55 2628 mutex_unlock(&kvm->lock);
73880c80 2629 break;
07975ad3
JK
2630#endif
2631#ifdef CONFIG_HAVE_KVM_MSI
2632 case KVM_SIGNAL_MSI: {
2633 struct kvm_msi msi;
2634
2635 r = -EFAULT;
2636 if (copy_from_user(&msi, argp, sizeof msi))
2637 goto out;
2638 r = kvm_send_userspace_msi(kvm, &msi);
2639 break;
2640 }
23d43cf9
CD
2641#endif
2642#ifdef __KVM_HAVE_IRQ_LINE
2643 case KVM_IRQ_LINE_STATUS:
2644 case KVM_IRQ_LINE: {
2645 struct kvm_irq_level irq_event;
2646
2647 r = -EFAULT;
2648 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2649 goto out;
2650
aa2fbe6d
YZ
2651 r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
2652 ioctl == KVM_IRQ_LINE_STATUS);
23d43cf9
CD
2653 if (r)
2654 goto out;
2655
2656 r = -EFAULT;
2657 if (ioctl == KVM_IRQ_LINE_STATUS) {
2658 if (copy_to_user(argp, &irq_event, sizeof irq_event))
2659 goto out;
2660 }
2661
2662 r = 0;
2663 break;
2664 }
73880c80 2665#endif
aa8d5944
AG
2666#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
2667 case KVM_SET_GSI_ROUTING: {
2668 struct kvm_irq_routing routing;
2669 struct kvm_irq_routing __user *urouting;
2670 struct kvm_irq_routing_entry *entries;
2671
2672 r = -EFAULT;
2673 if (copy_from_user(&routing, argp, sizeof(routing)))
2674 goto out;
2675 r = -EINVAL;
2676 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2677 goto out;
2678 if (routing.flags)
2679 goto out;
2680 r = -ENOMEM;
2681 entries = vmalloc(routing.nr * sizeof(*entries));
2682 if (!entries)
2683 goto out;
2684 r = -EFAULT;
2685 urouting = argp;
2686 if (copy_from_user(entries, urouting->entries,
2687 routing.nr * sizeof(*entries)))
2688 goto out_free_irq_routing;
2689 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2690 routing.flags);
2691 out_free_irq_routing:
2692 vfree(entries);
2693 break;
2694 }
2695#endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
852b6d57
SW
2696 case KVM_CREATE_DEVICE: {
2697 struct kvm_create_device cd;
2698
2699 r = -EFAULT;
2700 if (copy_from_user(&cd, argp, sizeof(cd)))
2701 goto out;
2702
2703 r = kvm_ioctl_create_device(kvm, &cd);
2704 if (r)
2705 goto out;
2706
2707 r = -EFAULT;
2708 if (copy_to_user(argp, &cd, sizeof(cd)))
2709 goto out;
2710
2711 r = 0;
2712 break;
2713 }
92b591a4
AG
2714 case KVM_CHECK_EXTENSION:
2715 r = kvm_vm_ioctl_check_extension_generic(kvm, arg);
2716 break;
f17abe9a 2717 default:
1fe779f8 2718 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
f17abe9a
AK
2719 }
2720out:
2721 return r;
2722}
2723
6ff5894c
AB
2724#ifdef CONFIG_COMPAT
2725struct compat_kvm_dirty_log {
2726 __u32 slot;
2727 __u32 padding1;
2728 union {
2729 compat_uptr_t dirty_bitmap; /* one bit per page */
2730 __u64 padding2;
2731 };
2732};
2733
2734static long kvm_vm_compat_ioctl(struct file *filp,
2735 unsigned int ioctl, unsigned long arg)
2736{
2737 struct kvm *kvm = filp->private_data;
2738 int r;
2739
2740 if (kvm->mm != current->mm)
2741 return -EIO;
2742 switch (ioctl) {
2743 case KVM_GET_DIRTY_LOG: {
2744 struct compat_kvm_dirty_log compat_log;
2745 struct kvm_dirty_log log;
2746
2747 r = -EFAULT;
2748 if (copy_from_user(&compat_log, (void __user *)arg,
2749 sizeof(compat_log)))
2750 goto out;
2751 log.slot = compat_log.slot;
2752 log.padding1 = compat_log.padding1;
2753 log.padding2 = compat_log.padding2;
2754 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2755
2756 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6ff5894c
AB
2757 break;
2758 }
2759 default:
2760 r = kvm_vm_ioctl(filp, ioctl, arg);
2761 }
2762
2763out:
2764 return r;
2765}
2766#endif
2767
3d3aab1b 2768static struct file_operations kvm_vm_fops = {
f17abe9a
AK
2769 .release = kvm_vm_release,
2770 .unlocked_ioctl = kvm_vm_ioctl,
6ff5894c
AB
2771#ifdef CONFIG_COMPAT
2772 .compat_ioctl = kvm_vm_compat_ioctl,
2773#endif
6038f373 2774 .llseek = noop_llseek,
f17abe9a
AK
2775};
2776
e08b9637 2777static int kvm_dev_ioctl_create_vm(unsigned long type)
f17abe9a 2778{
aac87636 2779 int r;
f17abe9a
AK
2780 struct kvm *kvm;
2781
e08b9637 2782 kvm = kvm_create_vm(type);
d6d28168
AK
2783 if (IS_ERR(kvm))
2784 return PTR_ERR(kvm);
6ce5a090
TY
2785#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2786 r = kvm_coalesced_mmio_init(kvm);
2787 if (r < 0) {
2788 kvm_put_kvm(kvm);
2789 return r;
2790 }
2791#endif
24009b05 2792 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR | O_CLOEXEC);
aac87636 2793 if (r < 0)
66c0b394 2794 kvm_put_kvm(kvm);
f17abe9a 2795
aac87636 2796 return r;
f17abe9a
AK
2797}
2798
2799static long kvm_dev_ioctl(struct file *filp,
2800 unsigned int ioctl, unsigned long arg)
2801{
07c45a36 2802 long r = -EINVAL;
f17abe9a
AK
2803
2804 switch (ioctl) {
2805 case KVM_GET_API_VERSION:
f0fe5108
AK
2806 if (arg)
2807 goto out;
f17abe9a
AK
2808 r = KVM_API_VERSION;
2809 break;
2810 case KVM_CREATE_VM:
e08b9637 2811 r = kvm_dev_ioctl_create_vm(arg);
f17abe9a 2812 break;
018d00d2 2813 case KVM_CHECK_EXTENSION:
784aa3d7 2814 r = kvm_vm_ioctl_check_extension_generic(NULL, arg);
5d308f45 2815 break;
07c45a36 2816 case KVM_GET_VCPU_MMAP_SIZE:
07c45a36
AK
2817 if (arg)
2818 goto out;
adb1ff46
AK
2819 r = PAGE_SIZE; /* struct kvm_run */
2820#ifdef CONFIG_X86
2821 r += PAGE_SIZE; /* pio data page */
5f94c174
LV
2822#endif
2823#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2824 r += PAGE_SIZE; /* coalesced mmio ring page */
adb1ff46 2825#endif
07c45a36 2826 break;
d4c9ff2d
FEL
2827 case KVM_TRACE_ENABLE:
2828 case KVM_TRACE_PAUSE:
2829 case KVM_TRACE_DISABLE:
2023a29c 2830 r = -EOPNOTSUPP;
d4c9ff2d 2831 break;
6aa8b732 2832 default:
043405e1 2833 return kvm_arch_dev_ioctl(filp, ioctl, arg);
6aa8b732
AK
2834 }
2835out:
2836 return r;
2837}
2838
6aa8b732 2839static struct file_operations kvm_chardev_ops = {
6aa8b732
AK
2840 .unlocked_ioctl = kvm_dev_ioctl,
2841 .compat_ioctl = kvm_dev_ioctl,
6038f373 2842 .llseek = noop_llseek,
6aa8b732
AK
2843};
2844
2845static struct miscdevice kvm_dev = {
bbe4432e 2846 KVM_MINOR,
6aa8b732
AK
2847 "kvm",
2848 &kvm_chardev_ops,
2849};
2850
75b7127c 2851static void hardware_enable_nolock(void *junk)
1b6c0168
AK
2852{
2853 int cpu = raw_smp_processor_id();
10474ae8 2854 int r;
1b6c0168 2855
7f59f492 2856 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
1b6c0168 2857 return;
10474ae8 2858
7f59f492 2859 cpumask_set_cpu(cpu, cpus_hardware_enabled);
10474ae8 2860
13a34e06 2861 r = kvm_arch_hardware_enable();
10474ae8
AG
2862
2863 if (r) {
2864 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2865 atomic_inc(&hardware_enable_failed);
2866 printk(KERN_INFO "kvm: enabling virtualization on "
2867 "CPU%d failed\n", cpu);
2868 }
1b6c0168
AK
2869}
2870
4fa92fb2 2871static void hardware_enable(void)
75b7127c 2872{
4a937f96 2873 raw_spin_lock(&kvm_count_lock);
4fa92fb2
PB
2874 if (kvm_usage_count)
2875 hardware_enable_nolock(NULL);
4a937f96 2876 raw_spin_unlock(&kvm_count_lock);
75b7127c
TY
2877}
2878
2879static void hardware_disable_nolock(void *junk)
1b6c0168
AK
2880{
2881 int cpu = raw_smp_processor_id();
2882
7f59f492 2883 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
1b6c0168 2884 return;
7f59f492 2885 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
13a34e06 2886 kvm_arch_hardware_disable();
1b6c0168
AK
2887}
2888
4fa92fb2 2889static void hardware_disable(void)
75b7127c 2890{
4a937f96 2891 raw_spin_lock(&kvm_count_lock);
4fa92fb2
PB
2892 if (kvm_usage_count)
2893 hardware_disable_nolock(NULL);
4a937f96 2894 raw_spin_unlock(&kvm_count_lock);
75b7127c
TY
2895}
2896
10474ae8
AG
2897static void hardware_disable_all_nolock(void)
2898{
2899 BUG_ON(!kvm_usage_count);
2900
2901 kvm_usage_count--;
2902 if (!kvm_usage_count)
75b7127c 2903 on_each_cpu(hardware_disable_nolock, NULL, 1);
10474ae8
AG
2904}
2905
2906static void hardware_disable_all(void)
2907{
4a937f96 2908 raw_spin_lock(&kvm_count_lock);
10474ae8 2909 hardware_disable_all_nolock();
4a937f96 2910 raw_spin_unlock(&kvm_count_lock);
10474ae8
AG
2911}
2912
2913static int hardware_enable_all(void)
2914{
2915 int r = 0;
2916
4a937f96 2917 raw_spin_lock(&kvm_count_lock);
10474ae8
AG
2918
2919 kvm_usage_count++;
2920 if (kvm_usage_count == 1) {
2921 atomic_set(&hardware_enable_failed, 0);
75b7127c 2922 on_each_cpu(hardware_enable_nolock, NULL, 1);
10474ae8
AG
2923
2924 if (atomic_read(&hardware_enable_failed)) {
2925 hardware_disable_all_nolock();
2926 r = -EBUSY;
2927 }
2928 }
2929
4a937f96 2930 raw_spin_unlock(&kvm_count_lock);
10474ae8
AG
2931
2932 return r;
2933}
2934
774c47f1
AK
2935static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2936 void *v)
2937{
2938 int cpu = (long)v;
2939
1a6f4d7f 2940 val &= ~CPU_TASKS_FROZEN;
774c47f1 2941 switch (val) {
cec9ad27 2942 case CPU_DYING:
6ec8a856
AK
2943 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2944 cpu);
4fa92fb2 2945 hardware_disable();
6ec8a856 2946 break;
da908f2f 2947 case CPU_STARTING:
43934a38
JK
2948 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2949 cpu);
4fa92fb2 2950 hardware_enable();
774c47f1
AK
2951 break;
2952 }
2953 return NOTIFY_OK;
2954}
2955
9a2b85c6 2956static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
d77c26fc 2957 void *v)
9a2b85c6 2958{
8e1c1815
SY
2959 /*
2960 * Some (well, at least mine) BIOSes hang on reboot if
2961 * in vmx root mode.
2962 *
2963 * And Intel TXT required VMX off for all cpu when system shutdown.
2964 */
2965 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2966 kvm_rebooting = true;
75b7127c 2967 on_each_cpu(hardware_disable_nolock, NULL, 1);
9a2b85c6
RR
2968 return NOTIFY_OK;
2969}
2970
2971static struct notifier_block kvm_reboot_notifier = {
2972 .notifier_call = kvm_reboot,
2973 .priority = 0,
2974};
2975
e93f8a0f 2976static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2eeb2e94
GH
2977{
2978 int i;
2979
2980 for (i = 0; i < bus->dev_count; i++) {
743eeb0b 2981 struct kvm_io_device *pos = bus->range[i].dev;
2eeb2e94
GH
2982
2983 kvm_iodevice_destructor(pos);
2984 }
e93f8a0f 2985 kfree(bus);
2eeb2e94
GH
2986}
2987
c21fbff1
PB
2988static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
2989 const struct kvm_io_range *r2)
743eeb0b 2990{
743eeb0b
SL
2991 if (r1->addr < r2->addr)
2992 return -1;
2993 if (r1->addr + r1->len > r2->addr + r2->len)
2994 return 1;
2995 return 0;
2996}
2997
a343c9b7
PB
2998static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
2999{
c21fbff1 3000 return kvm_io_bus_cmp(p1, p2);
a343c9b7
PB
3001}
3002
39369f7a 3003static int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
743eeb0b
SL
3004 gpa_t addr, int len)
3005{
743eeb0b
SL
3006 bus->range[bus->dev_count++] = (struct kvm_io_range) {
3007 .addr = addr,
3008 .len = len,
3009 .dev = dev,
3010 };
3011
3012 sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
3013 kvm_io_bus_sort_cmp, NULL);
3014
3015 return 0;
3016}
3017
39369f7a 3018static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
743eeb0b
SL
3019 gpa_t addr, int len)
3020{
3021 struct kvm_io_range *range, key;
3022 int off;
3023
3024 key = (struct kvm_io_range) {
3025 .addr = addr,
3026 .len = len,
3027 };
3028
3029 range = bsearch(&key, bus->range, bus->dev_count,
3030 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
3031 if (range == NULL)
3032 return -ENOENT;
3033
3034 off = range - bus->range;
3035
c21fbff1 3036 while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
743eeb0b
SL
3037 off--;
3038
3039 return off;
3040}
3041
126a5af5
CH
3042static int __kvm_io_bus_write(struct kvm_io_bus *bus,
3043 struct kvm_io_range *range, const void *val)
3044{
3045 int idx;
3046
3047 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
3048 if (idx < 0)
3049 return -EOPNOTSUPP;
3050
3051 while (idx < bus->dev_count &&
c21fbff1 3052 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
126a5af5
CH
3053 if (!kvm_iodevice_write(bus->range[idx].dev, range->addr,
3054 range->len, val))
3055 return idx;
3056 idx++;
3057 }
3058
3059 return -EOPNOTSUPP;
3060}
3061
bda9020e 3062/* kvm_io_bus_write - called under kvm->slots_lock */
e93f8a0f 3063int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
bda9020e 3064 int len, const void *val)
2eeb2e94 3065{
90d83dc3 3066 struct kvm_io_bus *bus;
743eeb0b 3067 struct kvm_io_range range;
126a5af5 3068 int r;
743eeb0b
SL
3069
3070 range = (struct kvm_io_range) {
3071 .addr = addr,
3072 .len = len,
3073 };
90d83dc3
LJ
3074
3075 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
126a5af5
CH
3076 r = __kvm_io_bus_write(bus, &range, val);
3077 return r < 0 ? r : 0;
3078}
3079
3080/* kvm_io_bus_write_cookie - called under kvm->slots_lock */
3081int kvm_io_bus_write_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
3082 int len, const void *val, long cookie)
3083{
3084 struct kvm_io_bus *bus;
3085 struct kvm_io_range range;
3086
3087 range = (struct kvm_io_range) {
3088 .addr = addr,
3089 .len = len,
3090 };
3091
3092 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
3093
3094 /* First try the device referenced by cookie. */
3095 if ((cookie >= 0) && (cookie < bus->dev_count) &&
c21fbff1 3096 (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
126a5af5
CH
3097 if (!kvm_iodevice_write(bus->range[cookie].dev, addr, len,
3098 val))
3099 return cookie;
3100
3101 /*
3102 * cookie contained garbage; fall back to search and return the
3103 * correct cookie value.
3104 */
3105 return __kvm_io_bus_write(bus, &range, val);
3106}
3107
3108static int __kvm_io_bus_read(struct kvm_io_bus *bus, struct kvm_io_range *range,
3109 void *val)
3110{
3111 int idx;
3112
3113 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
743eeb0b
SL
3114 if (idx < 0)
3115 return -EOPNOTSUPP;
3116
3117 while (idx < bus->dev_count &&
c21fbff1 3118 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
126a5af5
CH
3119 if (!kvm_iodevice_read(bus->range[idx].dev, range->addr,
3120 range->len, val))
3121 return idx;
743eeb0b
SL
3122 idx++;
3123 }
3124
bda9020e
MT
3125 return -EOPNOTSUPP;
3126}
68c3b4d1 3127EXPORT_SYMBOL_GPL(kvm_io_bus_write);
2eeb2e94 3128
bda9020e 3129/* kvm_io_bus_read - called under kvm->slots_lock */
e93f8a0f
MT
3130int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
3131 int len, void *val)
bda9020e 3132{
90d83dc3 3133 struct kvm_io_bus *bus;
743eeb0b 3134 struct kvm_io_range range;
126a5af5 3135 int r;
743eeb0b
SL
3136
3137 range = (struct kvm_io_range) {
3138 .addr = addr,
3139 .len = len,
3140 };
e93f8a0f 3141
90d83dc3 3142 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
126a5af5
CH
3143 r = __kvm_io_bus_read(bus, &range, val);
3144 return r < 0 ? r : 0;
3145}
743eeb0b 3146
2eeb2e94 3147
79fac95e 3148/* Caller must hold slots_lock. */
743eeb0b
SL
3149int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
3150 int len, struct kvm_io_device *dev)
6c474694 3151{
e93f8a0f 3152 struct kvm_io_bus *new_bus, *bus;
090b7aff 3153
e93f8a0f 3154 bus = kvm->buses[bus_idx];
6ea34c9b
AK
3155 /* exclude ioeventfd which is limited by maximum fd */
3156 if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
090b7aff 3157 return -ENOSPC;
2eeb2e94 3158
a1300716
AK
3159 new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
3160 sizeof(struct kvm_io_range)), GFP_KERNEL);
e93f8a0f
MT
3161 if (!new_bus)
3162 return -ENOMEM;
a1300716
AK
3163 memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
3164 sizeof(struct kvm_io_range)));
743eeb0b 3165 kvm_io_bus_insert_dev(new_bus, dev, addr, len);
e93f8a0f
MT
3166 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
3167 synchronize_srcu_expedited(&kvm->srcu);
3168 kfree(bus);
090b7aff
GH
3169
3170 return 0;
3171}
3172
79fac95e 3173/* Caller must hold slots_lock. */
e93f8a0f
MT
3174int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
3175 struct kvm_io_device *dev)
090b7aff 3176{
e93f8a0f
MT
3177 int i, r;
3178 struct kvm_io_bus *new_bus, *bus;
090b7aff 3179
cdfca7b3 3180 bus = kvm->buses[bus_idx];
e93f8a0f 3181 r = -ENOENT;
a1300716
AK
3182 for (i = 0; i < bus->dev_count; i++)
3183 if (bus->range[i].dev == dev) {
e93f8a0f 3184 r = 0;
090b7aff
GH
3185 break;
3186 }
e93f8a0f 3187
a1300716 3188 if (r)
e93f8a0f 3189 return r;
a1300716
AK
3190
3191 new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count - 1) *
3192 sizeof(struct kvm_io_range)), GFP_KERNEL);
3193 if (!new_bus)
3194 return -ENOMEM;
3195
3196 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
3197 new_bus->dev_count--;
3198 memcpy(new_bus->range + i, bus->range + i + 1,
3199 (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
e93f8a0f
MT
3200
3201 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
3202 synchronize_srcu_expedited(&kvm->srcu);
3203 kfree(bus);
3204 return r;
2eeb2e94
GH
3205}
3206
774c47f1
AK
3207static struct notifier_block kvm_cpu_notifier = {
3208 .notifier_call = kvm_cpu_hotplug,
774c47f1
AK
3209};
3210
8b88b099 3211static int vm_stat_get(void *_offset, u64 *val)
ba1389b7
AK
3212{
3213 unsigned offset = (long)_offset;
ba1389b7
AK
3214 struct kvm *kvm;
3215
8b88b099 3216 *val = 0;
2f303b74 3217 spin_lock(&kvm_lock);
ba1389b7 3218 list_for_each_entry(kvm, &vm_list, vm_list)
8b88b099 3219 *val += *(u32 *)((void *)kvm + offset);
2f303b74 3220 spin_unlock(&kvm_lock);
8b88b099 3221 return 0;
ba1389b7
AK
3222}
3223
3224DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
3225
8b88b099 3226static int vcpu_stat_get(void *_offset, u64 *val)
1165f5fe
AK
3227{
3228 unsigned offset = (long)_offset;
1165f5fe
AK
3229 struct kvm *kvm;
3230 struct kvm_vcpu *vcpu;
3231 int i;
3232
8b88b099 3233 *val = 0;
2f303b74 3234 spin_lock(&kvm_lock);
1165f5fe 3235 list_for_each_entry(kvm, &vm_list, vm_list)
988a2cae
GN
3236 kvm_for_each_vcpu(i, vcpu, kvm)
3237 *val += *(u32 *)((void *)vcpu + offset);
3238
2f303b74 3239 spin_unlock(&kvm_lock);
8b88b099 3240 return 0;
1165f5fe
AK
3241}
3242
ba1389b7
AK
3243DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
3244
828c0950 3245static const struct file_operations *stat_fops[] = {
ba1389b7
AK
3246 [KVM_STAT_VCPU] = &vcpu_stat_fops,
3247 [KVM_STAT_VM] = &vm_stat_fops,
3248};
1165f5fe 3249
4f69b680 3250static int kvm_init_debug(void)
6aa8b732 3251{
0c8eb04a 3252 int r = -EEXIST;
6aa8b732
AK
3253 struct kvm_stats_debugfs_item *p;
3254
76f7c879 3255 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
4f69b680
H
3256 if (kvm_debugfs_dir == NULL)
3257 goto out;
3258
3259 for (p = debugfs_entries; p->name; ++p) {
76f7c879 3260 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1165f5fe 3261 (void *)(long)p->offset,
ba1389b7 3262 stat_fops[p->kind]);
4f69b680
H
3263 if (p->dentry == NULL)
3264 goto out_dir;
3265 }
3266
3267 return 0;
3268
3269out_dir:
3270 debugfs_remove_recursive(kvm_debugfs_dir);
3271out:
3272 return r;
6aa8b732
AK
3273}
3274
3275static void kvm_exit_debug(void)
3276{
3277 struct kvm_stats_debugfs_item *p;
3278
3279 for (p = debugfs_entries; p->name; ++p)
3280 debugfs_remove(p->dentry);
76f7c879 3281 debugfs_remove(kvm_debugfs_dir);
6aa8b732
AK
3282}
3283
fb3600cc 3284static int kvm_suspend(void)
59ae6c6b 3285{
10474ae8 3286 if (kvm_usage_count)
75b7127c 3287 hardware_disable_nolock(NULL);
59ae6c6b
AK
3288 return 0;
3289}
3290
fb3600cc 3291static void kvm_resume(void)
59ae6c6b 3292{
ca84d1a2 3293 if (kvm_usage_count) {
4a937f96 3294 WARN_ON(raw_spin_is_locked(&kvm_count_lock));
75b7127c 3295 hardware_enable_nolock(NULL);
ca84d1a2 3296 }
59ae6c6b
AK
3297}
3298
fb3600cc 3299static struct syscore_ops kvm_syscore_ops = {
59ae6c6b
AK
3300 .suspend = kvm_suspend,
3301 .resume = kvm_resume,
3302};
3303
15ad7146
AK
3304static inline
3305struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3306{
3307 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3308}
3309
3310static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3311{
3312 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3a08a8f9
R
3313 if (vcpu->preempted)
3314 vcpu->preempted = false;
15ad7146 3315
e790d9ef
RK
3316 kvm_arch_sched_in(vcpu, cpu);
3317
e9b11c17 3318 kvm_arch_vcpu_load(vcpu, cpu);
15ad7146
AK
3319}
3320
3321static void kvm_sched_out(struct preempt_notifier *pn,
3322 struct task_struct *next)
3323{
3324 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3325
3a08a8f9
R
3326 if (current->state == TASK_RUNNING)
3327 vcpu->preempted = true;
e9b11c17 3328 kvm_arch_vcpu_put(vcpu);
15ad7146
AK
3329}
3330
0ee75bea 3331int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
c16f862d 3332 struct module *module)
6aa8b732
AK
3333{
3334 int r;
002c7f7c 3335 int cpu;
6aa8b732 3336
f8c16bba
ZX
3337 r = kvm_arch_init(opaque);
3338 if (r)
d2308784 3339 goto out_fail;
cb498ea2 3340
7dac16c3
AH
3341 /*
3342 * kvm_arch_init makes sure there's at most one caller
3343 * for architectures that support multiple implementations,
3344 * like intel and amd on x86.
3345 * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
3346 * conflicts in case kvm is already setup for another implementation.
3347 */
3348 r = kvm_irqfd_init();
3349 if (r)
3350 goto out_irqfd;
3351
8437a617 3352 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
7f59f492
RR
3353 r = -ENOMEM;
3354 goto out_free_0;
3355 }
3356
e9b11c17 3357 r = kvm_arch_hardware_setup();
6aa8b732 3358 if (r < 0)
7f59f492 3359 goto out_free_0a;
6aa8b732 3360
002c7f7c
YS
3361 for_each_online_cpu(cpu) {
3362 smp_call_function_single(cpu,
e9b11c17 3363 kvm_arch_check_processor_compat,
8691e5a8 3364 &r, 1);
002c7f7c 3365 if (r < 0)
d2308784 3366 goto out_free_1;
002c7f7c
YS
3367 }
3368
774c47f1
AK
3369 r = register_cpu_notifier(&kvm_cpu_notifier);
3370 if (r)
d2308784 3371 goto out_free_2;
6aa8b732
AK
3372 register_reboot_notifier(&kvm_reboot_notifier);
3373
c16f862d 3374 /* A kmem cache lets us meet the alignment requirements of fx_save. */
0ee75bea
AK
3375 if (!vcpu_align)
3376 vcpu_align = __alignof__(struct kvm_vcpu);
3377 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
56919c5c 3378 0, NULL);
c16f862d
RR
3379 if (!kvm_vcpu_cache) {
3380 r = -ENOMEM;
fb3600cc 3381 goto out_free_3;
c16f862d
RR
3382 }
3383
af585b92
GN
3384 r = kvm_async_pf_init();
3385 if (r)
3386 goto out_free;
3387
6aa8b732 3388 kvm_chardev_ops.owner = module;
3d3aab1b
CB
3389 kvm_vm_fops.owner = module;
3390 kvm_vcpu_fops.owner = module;
6aa8b732
AK
3391
3392 r = misc_register(&kvm_dev);
3393 if (r) {
d77c26fc 3394 printk(KERN_ERR "kvm: misc device register failed\n");
af585b92 3395 goto out_unreg;
6aa8b732
AK
3396 }
3397
fb3600cc
RW
3398 register_syscore_ops(&kvm_syscore_ops);
3399
15ad7146
AK
3400 kvm_preempt_ops.sched_in = kvm_sched_in;
3401 kvm_preempt_ops.sched_out = kvm_sched_out;
3402
4f69b680
H
3403 r = kvm_init_debug();
3404 if (r) {
3405 printk(KERN_ERR "kvm: create debugfs files failed\n");
3406 goto out_undebugfs;
3407 }
0ea4ed8e 3408
3c3c29fd
PB
3409 r = kvm_vfio_ops_init();
3410 WARN_ON(r);
3411
c7addb90 3412 return 0;
6aa8b732 3413
4f69b680
H
3414out_undebugfs:
3415 unregister_syscore_ops(&kvm_syscore_ops);
afc2f792 3416 misc_deregister(&kvm_dev);
af585b92
GN
3417out_unreg:
3418 kvm_async_pf_deinit();
6aa8b732 3419out_free:
c16f862d 3420 kmem_cache_destroy(kvm_vcpu_cache);
d2308784 3421out_free_3:
6aa8b732 3422 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1 3423 unregister_cpu_notifier(&kvm_cpu_notifier);
d2308784 3424out_free_2:
d2308784 3425out_free_1:
e9b11c17 3426 kvm_arch_hardware_unsetup();
7f59f492
RR
3427out_free_0a:
3428 free_cpumask_var(cpus_hardware_enabled);
d2308784 3429out_free_0:
a0f155e9
CH
3430 kvm_irqfd_exit();
3431out_irqfd:
7dac16c3
AH
3432 kvm_arch_exit();
3433out_fail:
6aa8b732
AK
3434 return r;
3435}
cb498ea2 3436EXPORT_SYMBOL_GPL(kvm_init);
6aa8b732 3437
cb498ea2 3438void kvm_exit(void)
6aa8b732 3439{
0ea4ed8e 3440 kvm_exit_debug();
6aa8b732 3441 misc_deregister(&kvm_dev);
c16f862d 3442 kmem_cache_destroy(kvm_vcpu_cache);
af585b92 3443 kvm_async_pf_deinit();
fb3600cc 3444 unregister_syscore_ops(&kvm_syscore_ops);
6aa8b732 3445 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 3446 unregister_cpu_notifier(&kvm_cpu_notifier);
75b7127c 3447 on_each_cpu(hardware_disable_nolock, NULL, 1);
e9b11c17 3448 kvm_arch_hardware_unsetup();
f8c16bba 3449 kvm_arch_exit();
a0f155e9 3450 kvm_irqfd_exit();
7f59f492 3451 free_cpumask_var(cpus_hardware_enabled);
571ee1b6 3452 kvm_vfio_ops_exit();
6aa8b732 3453}
cb498ea2 3454EXPORT_SYMBOL_GPL(kvm_exit);