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