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