KVM: SVM: check for progress after IRET interception
[deliverable/linux.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
217ece61 1487void kvm_vcpu_on_spin(struct kvm_vcpu *me)
d255f4f2 1488{
217ece61
RR
1489 struct kvm *kvm = me->kvm;
1490 struct kvm_vcpu *vcpu;
1491 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1492 int yielded = 0;
1493 int pass;
1494 int i;
d255f4f2 1495
217ece61
RR
1496 /*
1497 * We boost the priority of a VCPU that is runnable but not
1498 * currently running, because it got preempted by something
1499 * else and called schedule in __vcpu_run. Hopefully that
1500 * VCPU is holding the lock that we need and will release it.
1501 * We approximate round-robin by starting at the last boosted VCPU.
1502 */
1503 for (pass = 0; pass < 2 && !yielded; pass++) {
1504 kvm_for_each_vcpu(i, vcpu, kvm) {
1505 struct task_struct *task = NULL;
1506 struct pid *pid;
1507 if (!pass && i < last_boosted_vcpu) {
1508 i = last_boosted_vcpu;
1509 continue;
1510 } else if (pass && i > last_boosted_vcpu)
1511 break;
1512 if (vcpu == me)
1513 continue;
1514 if (waitqueue_active(&vcpu->wq))
1515 continue;
1516 rcu_read_lock();
1517 pid = rcu_dereference(vcpu->pid);
1518 if (pid)
1519 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1520 rcu_read_unlock();
1521 if (!task)
1522 continue;
1523 if (task->flags & PF_VCPU) {
1524 put_task_struct(task);
1525 continue;
1526 }
1527 if (yield_to(task, 1)) {
1528 put_task_struct(task);
1529 kvm->last_boosted_vcpu = i;
1530 yielded = 1;
1531 break;
1532 }
1533 put_task_struct(task);
1534 }
1535 }
d255f4f2
ZE
1536}
1537EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1538
e4a533a4 1539static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
9a2bb7f4
AK
1540{
1541 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
9a2bb7f4
AK
1542 struct page *page;
1543
e4a533a4 1544 if (vmf->pgoff == 0)
039576c0 1545 page = virt_to_page(vcpu->run);
09566765 1546#ifdef CONFIG_X86
e4a533a4 1547 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
ad312c7c 1548 page = virt_to_page(vcpu->arch.pio_data);
5f94c174
LV
1549#endif
1550#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1551 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1552 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
09566765 1553#endif
039576c0 1554 else
e4a533a4 1555 return VM_FAULT_SIGBUS;
9a2bb7f4 1556 get_page(page);
e4a533a4 1557 vmf->page = page;
1558 return 0;
9a2bb7f4
AK
1559}
1560
f0f37e2f 1561static const struct vm_operations_struct kvm_vcpu_vm_ops = {
e4a533a4 1562 .fault = kvm_vcpu_fault,
9a2bb7f4
AK
1563};
1564
1565static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1566{
1567 vma->vm_ops = &kvm_vcpu_vm_ops;
1568 return 0;
1569}
1570
bccf2150
AK
1571static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1572{
1573 struct kvm_vcpu *vcpu = filp->private_data;
1574
66c0b394 1575 kvm_put_kvm(vcpu->kvm);
bccf2150
AK
1576 return 0;
1577}
1578
3d3aab1b 1579static struct file_operations kvm_vcpu_fops = {
bccf2150
AK
1580 .release = kvm_vcpu_release,
1581 .unlocked_ioctl = kvm_vcpu_ioctl,
1582 .compat_ioctl = kvm_vcpu_ioctl,
9a2bb7f4 1583 .mmap = kvm_vcpu_mmap,
6038f373 1584 .llseek = noop_llseek,
bccf2150
AK
1585};
1586
1587/*
1588 * Allocates an inode for the vcpu.
1589 */
1590static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1591{
628ff7c1 1592 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
bccf2150
AK
1593}
1594
c5ea7660
AK
1595/*
1596 * Creates some virtual cpus. Good luck creating more than one.
1597 */
73880c80 1598static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
c5ea7660
AK
1599{
1600 int r;
988a2cae 1601 struct kvm_vcpu *vcpu, *v;
c5ea7660 1602
73880c80 1603 vcpu = kvm_arch_vcpu_create(kvm, id);
fb3f0f51
RR
1604 if (IS_ERR(vcpu))
1605 return PTR_ERR(vcpu);
c5ea7660 1606
15ad7146
AK
1607 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1608
26e5215f
AK
1609 r = kvm_arch_vcpu_setup(vcpu);
1610 if (r)
7d8fece6 1611 return r;
26e5215f 1612
11ec2804 1613 mutex_lock(&kvm->lock);
73880c80
GN
1614 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1615 r = -EINVAL;
e9b11c17 1616 goto vcpu_destroy;
fb3f0f51 1617 }
73880c80 1618
988a2cae
GN
1619 kvm_for_each_vcpu(r, v, kvm)
1620 if (v->vcpu_id == id) {
73880c80
GN
1621 r = -EEXIST;
1622 goto vcpu_destroy;
1623 }
1624
1625 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
c5ea7660 1626
fb3f0f51 1627 /* Now it's all set up, let userspace reach it */
66c0b394 1628 kvm_get_kvm(kvm);
bccf2150 1629 r = create_vcpu_fd(vcpu);
73880c80
GN
1630 if (r < 0) {
1631 kvm_put_kvm(kvm);
1632 goto vcpu_destroy;
1633 }
1634
1635 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1636 smp_wmb();
1637 atomic_inc(&kvm->online_vcpus);
1638
1639#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1640 if (kvm->bsp_vcpu_id == id)
1641 kvm->bsp_vcpu = vcpu;
1642#endif
1643 mutex_unlock(&kvm->lock);
fb3f0f51 1644 return r;
39c3b86e 1645
e9b11c17 1646vcpu_destroy:
7d8fece6 1647 mutex_unlock(&kvm->lock);
d40ccc62 1648 kvm_arch_vcpu_destroy(vcpu);
c5ea7660
AK
1649 return r;
1650}
1651
1961d276
AK
1652static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1653{
1654 if (sigset) {
1655 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1656 vcpu->sigset_active = 1;
1657 vcpu->sigset = *sigset;
1658 } else
1659 vcpu->sigset_active = 0;
1660 return 0;
1661}
1662
bccf2150
AK
1663static long kvm_vcpu_ioctl(struct file *filp,
1664 unsigned int ioctl, unsigned long arg)
6aa8b732 1665{
bccf2150 1666 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 1667 void __user *argp = (void __user *)arg;
313a3dc7 1668 int r;
fa3795a7
DH
1669 struct kvm_fpu *fpu = NULL;
1670 struct kvm_sregs *kvm_sregs = NULL;
6aa8b732 1671
6d4e4c4f
AK
1672 if (vcpu->kvm->mm != current->mm)
1673 return -EIO;
2122ff5e
AK
1674
1675#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1676 /*
1677 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1678 * so vcpu_load() would break it.
1679 */
1680 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1681 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1682#endif
1683
1684
1685 vcpu_load(vcpu);
6aa8b732 1686 switch (ioctl) {
9a2bb7f4 1687 case KVM_RUN:
f0fe5108
AK
1688 r = -EINVAL;
1689 if (arg)
1690 goto out;
b6c7a5dc 1691 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
64be5007 1692 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
6aa8b732 1693 break;
6aa8b732 1694 case KVM_GET_REGS: {
3e4bb3ac 1695 struct kvm_regs *kvm_regs;
6aa8b732 1696
3e4bb3ac
XZ
1697 r = -ENOMEM;
1698 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1699 if (!kvm_regs)
6aa8b732 1700 goto out;
3e4bb3ac
XZ
1701 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1702 if (r)
1703 goto out_free1;
6aa8b732 1704 r = -EFAULT;
3e4bb3ac
XZ
1705 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1706 goto out_free1;
6aa8b732 1707 r = 0;
3e4bb3ac
XZ
1708out_free1:
1709 kfree(kvm_regs);
6aa8b732
AK
1710 break;
1711 }
1712 case KVM_SET_REGS: {
3e4bb3ac 1713 struct kvm_regs *kvm_regs;
6aa8b732 1714
3e4bb3ac
XZ
1715 r = -ENOMEM;
1716 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1717 if (!kvm_regs)
6aa8b732 1718 goto out;
3e4bb3ac
XZ
1719 r = -EFAULT;
1720 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1721 goto out_free2;
1722 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
6aa8b732 1723 if (r)
3e4bb3ac 1724 goto out_free2;
6aa8b732 1725 r = 0;
3e4bb3ac
XZ
1726out_free2:
1727 kfree(kvm_regs);
6aa8b732
AK
1728 break;
1729 }
1730 case KVM_GET_SREGS: {
fa3795a7
DH
1731 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1732 r = -ENOMEM;
1733 if (!kvm_sregs)
1734 goto out;
1735 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
6aa8b732
AK
1736 if (r)
1737 goto out;
1738 r = -EFAULT;
fa3795a7 1739 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
6aa8b732
AK
1740 goto out;
1741 r = 0;
1742 break;
1743 }
1744 case KVM_SET_SREGS: {
fa3795a7
DH
1745 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1746 r = -ENOMEM;
1747 if (!kvm_sregs)
1748 goto out;
6aa8b732 1749 r = -EFAULT;
fa3795a7 1750 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
6aa8b732 1751 goto out;
fa3795a7 1752 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
6aa8b732
AK
1753 if (r)
1754 goto out;
1755 r = 0;
1756 break;
1757 }
62d9f0db
MT
1758 case KVM_GET_MP_STATE: {
1759 struct kvm_mp_state mp_state;
1760
1761 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1762 if (r)
1763 goto out;
1764 r = -EFAULT;
1765 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1766 goto out;
1767 r = 0;
1768 break;
1769 }
1770 case KVM_SET_MP_STATE: {
1771 struct kvm_mp_state mp_state;
1772
1773 r = -EFAULT;
1774 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1775 goto out;
1776 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1777 if (r)
1778 goto out;
1779 r = 0;
1780 break;
1781 }
6aa8b732
AK
1782 case KVM_TRANSLATE: {
1783 struct kvm_translation tr;
1784
1785 r = -EFAULT;
2f366987 1786 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 1787 goto out;
8b006791 1788 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
1789 if (r)
1790 goto out;
1791 r = -EFAULT;
2f366987 1792 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
1793 goto out;
1794 r = 0;
1795 break;
1796 }
d0bfb940
JK
1797 case KVM_SET_GUEST_DEBUG: {
1798 struct kvm_guest_debug dbg;
6aa8b732
AK
1799
1800 r = -EFAULT;
2f366987 1801 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 1802 goto out;
d0bfb940 1803 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
6aa8b732
AK
1804 if (r)
1805 goto out;
1806 r = 0;
1807 break;
1808 }
1961d276
AK
1809 case KVM_SET_SIGNAL_MASK: {
1810 struct kvm_signal_mask __user *sigmask_arg = argp;
1811 struct kvm_signal_mask kvm_sigmask;
1812 sigset_t sigset, *p;
1813
1814 p = NULL;
1815 if (argp) {
1816 r = -EFAULT;
1817 if (copy_from_user(&kvm_sigmask, argp,
1818 sizeof kvm_sigmask))
1819 goto out;
1820 r = -EINVAL;
1821 if (kvm_sigmask.len != sizeof sigset)
1822 goto out;
1823 r = -EFAULT;
1824 if (copy_from_user(&sigset, sigmask_arg->sigset,
1825 sizeof sigset))
1826 goto out;
1827 p = &sigset;
1828 }
376d41ff 1829 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
1961d276
AK
1830 break;
1831 }
b8836737 1832 case KVM_GET_FPU: {
fa3795a7
DH
1833 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1834 r = -ENOMEM;
1835 if (!fpu)
1836 goto out;
1837 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
b8836737
AK
1838 if (r)
1839 goto out;
1840 r = -EFAULT;
fa3795a7 1841 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
b8836737
AK
1842 goto out;
1843 r = 0;
1844 break;
1845 }
1846 case KVM_SET_FPU: {
fa3795a7
DH
1847 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1848 r = -ENOMEM;
1849 if (!fpu)
1850 goto out;
b8836737 1851 r = -EFAULT;
fa3795a7 1852 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
b8836737 1853 goto out;
fa3795a7 1854 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
b8836737
AK
1855 if (r)
1856 goto out;
1857 r = 0;
1858 break;
1859 }
bccf2150 1860 default:
313a3dc7 1861 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
bccf2150
AK
1862 }
1863out:
2122ff5e 1864 vcpu_put(vcpu);
fa3795a7
DH
1865 kfree(fpu);
1866 kfree(kvm_sregs);
bccf2150
AK
1867 return r;
1868}
1869
1870static long kvm_vm_ioctl(struct file *filp,
1871 unsigned int ioctl, unsigned long arg)
1872{
1873 struct kvm *kvm = filp->private_data;
1874 void __user *argp = (void __user *)arg;
1fe779f8 1875 int r;
bccf2150 1876
6d4e4c4f
AK
1877 if (kvm->mm != current->mm)
1878 return -EIO;
bccf2150
AK
1879 switch (ioctl) {
1880 case KVM_CREATE_VCPU:
1881 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1882 if (r < 0)
1883 goto out;
1884 break;
6fc138d2
IE
1885 case KVM_SET_USER_MEMORY_REGION: {
1886 struct kvm_userspace_memory_region kvm_userspace_mem;
1887
1888 r = -EFAULT;
1889 if (copy_from_user(&kvm_userspace_mem, argp,
1890 sizeof kvm_userspace_mem))
1891 goto out;
1892
1893 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
6aa8b732
AK
1894 if (r)
1895 goto out;
1896 break;
1897 }
1898 case KVM_GET_DIRTY_LOG: {
1899 struct kvm_dirty_log log;
1900
1901 r = -EFAULT;
2f366987 1902 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 1903 goto out;
2c6f5df9 1904 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
1905 if (r)
1906 goto out;
1907 break;
1908 }
5f94c174
LV
1909#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1910 case KVM_REGISTER_COALESCED_MMIO: {
1911 struct kvm_coalesced_mmio_zone zone;
1912 r = -EFAULT;
1913 if (copy_from_user(&zone, argp, sizeof zone))
1914 goto out;
5f94c174
LV
1915 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1916 if (r)
1917 goto out;
1918 r = 0;
1919 break;
1920 }
1921 case KVM_UNREGISTER_COALESCED_MMIO: {
1922 struct kvm_coalesced_mmio_zone zone;
1923 r = -EFAULT;
1924 if (copy_from_user(&zone, argp, sizeof zone))
1925 goto out;
5f94c174
LV
1926 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1927 if (r)
1928 goto out;
1929 r = 0;
1930 break;
1931 }
1932#endif
721eecbf
GH
1933 case KVM_IRQFD: {
1934 struct kvm_irqfd data;
1935
1936 r = -EFAULT;
1937 if (copy_from_user(&data, argp, sizeof data))
1938 goto out;
1939 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1940 break;
1941 }
d34e6b17
GH
1942 case KVM_IOEVENTFD: {
1943 struct kvm_ioeventfd data;
1944
1945 r = -EFAULT;
1946 if (copy_from_user(&data, argp, sizeof data))
1947 goto out;
1948 r = kvm_ioeventfd(kvm, &data);
1949 break;
1950 }
73880c80
GN
1951#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1952 case KVM_SET_BOOT_CPU_ID:
1953 r = 0;
894a9c55 1954 mutex_lock(&kvm->lock);
73880c80
GN
1955 if (atomic_read(&kvm->online_vcpus) != 0)
1956 r = -EBUSY;
1957 else
1958 kvm->bsp_vcpu_id = arg;
894a9c55 1959 mutex_unlock(&kvm->lock);
73880c80
GN
1960 break;
1961#endif
f17abe9a 1962 default:
1fe779f8 1963 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
bfd99ff5
AK
1964 if (r == -ENOTTY)
1965 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
f17abe9a
AK
1966 }
1967out:
1968 return r;
1969}
1970
6ff5894c
AB
1971#ifdef CONFIG_COMPAT
1972struct compat_kvm_dirty_log {
1973 __u32 slot;
1974 __u32 padding1;
1975 union {
1976 compat_uptr_t dirty_bitmap; /* one bit per page */
1977 __u64 padding2;
1978 };
1979};
1980
1981static long kvm_vm_compat_ioctl(struct file *filp,
1982 unsigned int ioctl, unsigned long arg)
1983{
1984 struct kvm *kvm = filp->private_data;
1985 int r;
1986
1987 if (kvm->mm != current->mm)
1988 return -EIO;
1989 switch (ioctl) {
1990 case KVM_GET_DIRTY_LOG: {
1991 struct compat_kvm_dirty_log compat_log;
1992 struct kvm_dirty_log log;
1993
1994 r = -EFAULT;
1995 if (copy_from_user(&compat_log, (void __user *)arg,
1996 sizeof(compat_log)))
1997 goto out;
1998 log.slot = compat_log.slot;
1999 log.padding1 = compat_log.padding1;
2000 log.padding2 = compat_log.padding2;
2001 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2002
2003 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2004 if (r)
2005 goto out;
2006 break;
2007 }
2008 default:
2009 r = kvm_vm_ioctl(filp, ioctl, arg);
2010 }
2011
2012out:
2013 return r;
2014}
2015#endif
2016
e4a533a4 2017static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
f17abe9a 2018{
777b3f49
MT
2019 struct page *page[1];
2020 unsigned long addr;
2021 int npages;
2022 gfn_t gfn = vmf->pgoff;
f17abe9a 2023 struct kvm *kvm = vma->vm_file->private_data;
f17abe9a 2024
777b3f49
MT
2025 addr = gfn_to_hva(kvm, gfn);
2026 if (kvm_is_error_hva(addr))
e4a533a4 2027 return VM_FAULT_SIGBUS;
777b3f49
MT
2028
2029 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2030 NULL);
2031 if (unlikely(npages != 1))
e4a533a4 2032 return VM_FAULT_SIGBUS;
777b3f49
MT
2033
2034 vmf->page = page[0];
e4a533a4 2035 return 0;
f17abe9a
AK
2036}
2037
f0f37e2f 2038static const struct vm_operations_struct kvm_vm_vm_ops = {
e4a533a4 2039 .fault = kvm_vm_fault,
f17abe9a
AK
2040};
2041
2042static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2043{
2044 vma->vm_ops = &kvm_vm_vm_ops;
2045 return 0;
2046}
2047
3d3aab1b 2048static struct file_operations kvm_vm_fops = {
f17abe9a
AK
2049 .release = kvm_vm_release,
2050 .unlocked_ioctl = kvm_vm_ioctl,
6ff5894c
AB
2051#ifdef CONFIG_COMPAT
2052 .compat_ioctl = kvm_vm_compat_ioctl,
2053#endif
f17abe9a 2054 .mmap = kvm_vm_mmap,
6038f373 2055 .llseek = noop_llseek,
f17abe9a
AK
2056};
2057
2058static int kvm_dev_ioctl_create_vm(void)
2059{
aac87636 2060 int r;
f17abe9a
AK
2061 struct kvm *kvm;
2062
f17abe9a 2063 kvm = kvm_create_vm();
d6d28168
AK
2064 if (IS_ERR(kvm))
2065 return PTR_ERR(kvm);
6ce5a090
TY
2066#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2067 r = kvm_coalesced_mmio_init(kvm);
2068 if (r < 0) {
2069 kvm_put_kvm(kvm);
2070 return r;
2071 }
2072#endif
aac87636
HC
2073 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2074 if (r < 0)
66c0b394 2075 kvm_put_kvm(kvm);
f17abe9a 2076
aac87636 2077 return r;
f17abe9a
AK
2078}
2079
1a811b61
AK
2080static long kvm_dev_ioctl_check_extension_generic(long arg)
2081{
2082 switch (arg) {
ca9edaee 2083 case KVM_CAP_USER_MEMORY:
1a811b61 2084 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
4cd481f6 2085 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
73880c80
GN
2086#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2087 case KVM_CAP_SET_BOOT_CPU_ID:
2088#endif
a9c7399d 2089 case KVM_CAP_INTERNAL_ERROR_DATA:
1a811b61 2090 return 1;
399ec807
AK
2091#ifdef CONFIG_HAVE_KVM_IRQCHIP
2092 case KVM_CAP_IRQ_ROUTING:
36463146 2093 return KVM_MAX_IRQ_ROUTES;
399ec807 2094#endif
1a811b61
AK
2095 default:
2096 break;
2097 }
2098 return kvm_dev_ioctl_check_extension(arg);
2099}
2100
f17abe9a
AK
2101static long kvm_dev_ioctl(struct file *filp,
2102 unsigned int ioctl, unsigned long arg)
2103{
07c45a36 2104 long r = -EINVAL;
f17abe9a
AK
2105
2106 switch (ioctl) {
2107 case KVM_GET_API_VERSION:
f0fe5108
AK
2108 r = -EINVAL;
2109 if (arg)
2110 goto out;
f17abe9a
AK
2111 r = KVM_API_VERSION;
2112 break;
2113 case KVM_CREATE_VM:
f0fe5108
AK
2114 r = -EINVAL;
2115 if (arg)
2116 goto out;
f17abe9a
AK
2117 r = kvm_dev_ioctl_create_vm();
2118 break;
018d00d2 2119 case KVM_CHECK_EXTENSION:
1a811b61 2120 r = kvm_dev_ioctl_check_extension_generic(arg);
5d308f45 2121 break;
07c45a36
AK
2122 case KVM_GET_VCPU_MMAP_SIZE:
2123 r = -EINVAL;
2124 if (arg)
2125 goto out;
adb1ff46
AK
2126 r = PAGE_SIZE; /* struct kvm_run */
2127#ifdef CONFIG_X86
2128 r += PAGE_SIZE; /* pio data page */
5f94c174
LV
2129#endif
2130#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2131 r += PAGE_SIZE; /* coalesced mmio ring page */
adb1ff46 2132#endif
07c45a36 2133 break;
d4c9ff2d
FEL
2134 case KVM_TRACE_ENABLE:
2135 case KVM_TRACE_PAUSE:
2136 case KVM_TRACE_DISABLE:
2023a29c 2137 r = -EOPNOTSUPP;
d4c9ff2d 2138 break;
6aa8b732 2139 default:
043405e1 2140 return kvm_arch_dev_ioctl(filp, ioctl, arg);
6aa8b732
AK
2141 }
2142out:
2143 return r;
2144}
2145
6aa8b732 2146static struct file_operations kvm_chardev_ops = {
6aa8b732
AK
2147 .unlocked_ioctl = kvm_dev_ioctl,
2148 .compat_ioctl = kvm_dev_ioctl,
6038f373 2149 .llseek = noop_llseek,
6aa8b732
AK
2150};
2151
2152static struct miscdevice kvm_dev = {
bbe4432e 2153 KVM_MINOR,
6aa8b732
AK
2154 "kvm",
2155 &kvm_chardev_ops,
2156};
2157
75b7127c 2158static void hardware_enable_nolock(void *junk)
1b6c0168
AK
2159{
2160 int cpu = raw_smp_processor_id();
10474ae8 2161 int r;
1b6c0168 2162
7f59f492 2163 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
1b6c0168 2164 return;
10474ae8 2165
7f59f492 2166 cpumask_set_cpu(cpu, cpus_hardware_enabled);
10474ae8
AG
2167
2168 r = kvm_arch_hardware_enable(NULL);
2169
2170 if (r) {
2171 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2172 atomic_inc(&hardware_enable_failed);
2173 printk(KERN_INFO "kvm: enabling virtualization on "
2174 "CPU%d failed\n", cpu);
2175 }
1b6c0168
AK
2176}
2177
75b7127c
TY
2178static void hardware_enable(void *junk)
2179{
2180 spin_lock(&kvm_lock);
2181 hardware_enable_nolock(junk);
2182 spin_unlock(&kvm_lock);
2183}
2184
2185static void hardware_disable_nolock(void *junk)
1b6c0168
AK
2186{
2187 int cpu = raw_smp_processor_id();
2188
7f59f492 2189 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
1b6c0168 2190 return;
7f59f492 2191 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
e9b11c17 2192 kvm_arch_hardware_disable(NULL);
1b6c0168
AK
2193}
2194
75b7127c
TY
2195static void hardware_disable(void *junk)
2196{
2197 spin_lock(&kvm_lock);
2198 hardware_disable_nolock(junk);
2199 spin_unlock(&kvm_lock);
2200}
2201
10474ae8
AG
2202static void hardware_disable_all_nolock(void)
2203{
2204 BUG_ON(!kvm_usage_count);
2205
2206 kvm_usage_count--;
2207 if (!kvm_usage_count)
75b7127c 2208 on_each_cpu(hardware_disable_nolock, NULL, 1);
10474ae8
AG
2209}
2210
2211static void hardware_disable_all(void)
2212{
2213 spin_lock(&kvm_lock);
2214 hardware_disable_all_nolock();
2215 spin_unlock(&kvm_lock);
2216}
2217
2218static int hardware_enable_all(void)
2219{
2220 int r = 0;
2221
2222 spin_lock(&kvm_lock);
2223
2224 kvm_usage_count++;
2225 if (kvm_usage_count == 1) {
2226 atomic_set(&hardware_enable_failed, 0);
75b7127c 2227 on_each_cpu(hardware_enable_nolock, NULL, 1);
10474ae8
AG
2228
2229 if (atomic_read(&hardware_enable_failed)) {
2230 hardware_disable_all_nolock();
2231 r = -EBUSY;
2232 }
2233 }
2234
2235 spin_unlock(&kvm_lock);
2236
2237 return r;
2238}
2239
774c47f1
AK
2240static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2241 void *v)
2242{
2243 int cpu = (long)v;
2244
10474ae8
AG
2245 if (!kvm_usage_count)
2246 return NOTIFY_OK;
2247
1a6f4d7f 2248 val &= ~CPU_TASKS_FROZEN;
774c47f1 2249 switch (val) {
cec9ad27 2250 case CPU_DYING:
6ec8a856
AK
2251 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2252 cpu);
2253 hardware_disable(NULL);
2254 break;
da908f2f 2255 case CPU_STARTING:
43934a38
JK
2256 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2257 cpu);
da908f2f 2258 hardware_enable(NULL);
774c47f1
AK
2259 break;
2260 }
2261 return NOTIFY_OK;
2262}
2263
4ecac3fd 2264
b7c4145b 2265asmlinkage void kvm_spurious_fault(void)
4ecac3fd 2266{
4ecac3fd
AK
2267 /* Fault while not rebooting. We want the trace. */
2268 BUG();
2269}
b7c4145b 2270EXPORT_SYMBOL_GPL(kvm_spurious_fault);
4ecac3fd 2271
9a2b85c6 2272static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
d77c26fc 2273 void *v)
9a2b85c6 2274{
8e1c1815
SY
2275 /*
2276 * Some (well, at least mine) BIOSes hang on reboot if
2277 * in vmx root mode.
2278 *
2279 * And Intel TXT required VMX off for all cpu when system shutdown.
2280 */
2281 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2282 kvm_rebooting = true;
75b7127c 2283 on_each_cpu(hardware_disable_nolock, NULL, 1);
9a2b85c6
RR
2284 return NOTIFY_OK;
2285}
2286
2287static struct notifier_block kvm_reboot_notifier = {
2288 .notifier_call = kvm_reboot,
2289 .priority = 0,
2290};
2291
e93f8a0f 2292static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2eeb2e94
GH
2293{
2294 int i;
2295
2296 for (i = 0; i < bus->dev_count; i++) {
2297 struct kvm_io_device *pos = bus->devs[i];
2298
2299 kvm_iodevice_destructor(pos);
2300 }
e93f8a0f 2301 kfree(bus);
2eeb2e94
GH
2302}
2303
bda9020e 2304/* kvm_io_bus_write - called under kvm->slots_lock */
e93f8a0f 2305int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
bda9020e 2306 int len, const void *val)
2eeb2e94
GH
2307{
2308 int i;
90d83dc3
LJ
2309 struct kvm_io_bus *bus;
2310
2311 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
bda9020e
MT
2312 for (i = 0; i < bus->dev_count; i++)
2313 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2314 return 0;
2315 return -EOPNOTSUPP;
2316}
2eeb2e94 2317
bda9020e 2318/* kvm_io_bus_read - called under kvm->slots_lock */
e93f8a0f
MT
2319int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2320 int len, void *val)
bda9020e
MT
2321{
2322 int i;
90d83dc3 2323 struct kvm_io_bus *bus;
e93f8a0f 2324
90d83dc3 2325 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
bda9020e
MT
2326 for (i = 0; i < bus->dev_count; i++)
2327 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2328 return 0;
2329 return -EOPNOTSUPP;
2eeb2e94
GH
2330}
2331
79fac95e 2332/* Caller must hold slots_lock. */
e93f8a0f
MT
2333int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2334 struct kvm_io_device *dev)
6c474694 2335{
e93f8a0f 2336 struct kvm_io_bus *new_bus, *bus;
090b7aff 2337
e93f8a0f 2338 bus = kvm->buses[bus_idx];
090b7aff
GH
2339 if (bus->dev_count > NR_IOBUS_DEVS-1)
2340 return -ENOSPC;
2eeb2e94 2341
e93f8a0f
MT
2342 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2343 if (!new_bus)
2344 return -ENOMEM;
2345 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2346 new_bus->devs[new_bus->dev_count++] = dev;
2347 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2348 synchronize_srcu_expedited(&kvm->srcu);
2349 kfree(bus);
090b7aff
GH
2350
2351 return 0;
2352}
2353
79fac95e 2354/* Caller must hold slots_lock. */
e93f8a0f
MT
2355int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2356 struct kvm_io_device *dev)
090b7aff 2357{
e93f8a0f
MT
2358 int i, r;
2359 struct kvm_io_bus *new_bus, *bus;
090b7aff 2360
e93f8a0f
MT
2361 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2362 if (!new_bus)
2363 return -ENOMEM;
090b7aff 2364
e93f8a0f
MT
2365 bus = kvm->buses[bus_idx];
2366 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2367
2368 r = -ENOENT;
2369 for (i = 0; i < new_bus->dev_count; i++)
2370 if (new_bus->devs[i] == dev) {
2371 r = 0;
2372 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
090b7aff
GH
2373 break;
2374 }
e93f8a0f
MT
2375
2376 if (r) {
2377 kfree(new_bus);
2378 return r;
2379 }
2380
2381 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2382 synchronize_srcu_expedited(&kvm->srcu);
2383 kfree(bus);
2384 return r;
2eeb2e94
GH
2385}
2386
774c47f1
AK
2387static struct notifier_block kvm_cpu_notifier = {
2388 .notifier_call = kvm_cpu_hotplug,
774c47f1
AK
2389};
2390
8b88b099 2391static int vm_stat_get(void *_offset, u64 *val)
ba1389b7
AK
2392{
2393 unsigned offset = (long)_offset;
ba1389b7
AK
2394 struct kvm *kvm;
2395
8b88b099 2396 *val = 0;
ba1389b7
AK
2397 spin_lock(&kvm_lock);
2398 list_for_each_entry(kvm, &vm_list, vm_list)
8b88b099 2399 *val += *(u32 *)((void *)kvm + offset);
ba1389b7 2400 spin_unlock(&kvm_lock);
8b88b099 2401 return 0;
ba1389b7
AK
2402}
2403
2404DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2405
8b88b099 2406static int vcpu_stat_get(void *_offset, u64 *val)
1165f5fe
AK
2407{
2408 unsigned offset = (long)_offset;
1165f5fe
AK
2409 struct kvm *kvm;
2410 struct kvm_vcpu *vcpu;
2411 int i;
2412
8b88b099 2413 *val = 0;
1165f5fe
AK
2414 spin_lock(&kvm_lock);
2415 list_for_each_entry(kvm, &vm_list, vm_list)
988a2cae
GN
2416 kvm_for_each_vcpu(i, vcpu, kvm)
2417 *val += *(u32 *)((void *)vcpu + offset);
2418
1165f5fe 2419 spin_unlock(&kvm_lock);
8b88b099 2420 return 0;
1165f5fe
AK
2421}
2422
ba1389b7
AK
2423DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2424
828c0950 2425static const struct file_operations *stat_fops[] = {
ba1389b7
AK
2426 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2427 [KVM_STAT_VM] = &vm_stat_fops,
2428};
1165f5fe 2429
a16b043c 2430static void kvm_init_debug(void)
6aa8b732
AK
2431{
2432 struct kvm_stats_debugfs_item *p;
2433
76f7c879 2434 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732 2435 for (p = debugfs_entries; p->name; ++p)
76f7c879 2436 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1165f5fe 2437 (void *)(long)p->offset,
ba1389b7 2438 stat_fops[p->kind]);
6aa8b732
AK
2439}
2440
2441static void kvm_exit_debug(void)
2442{
2443 struct kvm_stats_debugfs_item *p;
2444
2445 for (p = debugfs_entries; p->name; ++p)
2446 debugfs_remove(p->dentry);
76f7c879 2447 debugfs_remove(kvm_debugfs_dir);
6aa8b732
AK
2448}
2449
59ae6c6b
AK
2450static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2451{
10474ae8 2452 if (kvm_usage_count)
75b7127c 2453 hardware_disable_nolock(NULL);
59ae6c6b
AK
2454 return 0;
2455}
2456
2457static int kvm_resume(struct sys_device *dev)
2458{
ca84d1a2
ZA
2459 if (kvm_usage_count) {
2460 WARN_ON(spin_is_locked(&kvm_lock));
75b7127c 2461 hardware_enable_nolock(NULL);
ca84d1a2 2462 }
59ae6c6b
AK
2463 return 0;
2464}
2465
2466static struct sysdev_class kvm_sysdev_class = {
af5ca3f4 2467 .name = "kvm",
59ae6c6b
AK
2468 .suspend = kvm_suspend,
2469 .resume = kvm_resume,
2470};
2471
2472static struct sys_device kvm_sysdev = {
2473 .id = 0,
2474 .cls = &kvm_sysdev_class,
2475};
2476
cea7bb21 2477struct page *bad_page;
35149e21 2478pfn_t bad_pfn;
6aa8b732 2479
15ad7146
AK
2480static inline
2481struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2482{
2483 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2484}
2485
2486static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2487{
2488 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2489
e9b11c17 2490 kvm_arch_vcpu_load(vcpu, cpu);
15ad7146
AK
2491}
2492
2493static void kvm_sched_out(struct preempt_notifier *pn,
2494 struct task_struct *next)
2495{
2496 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2497
e9b11c17 2498 kvm_arch_vcpu_put(vcpu);
15ad7146
AK
2499}
2500
0ee75bea 2501int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
c16f862d 2502 struct module *module)
6aa8b732
AK
2503{
2504 int r;
002c7f7c 2505 int cpu;
6aa8b732 2506
f8c16bba
ZX
2507 r = kvm_arch_init(opaque);
2508 if (r)
d2308784 2509 goto out_fail;
cb498ea2
ZX
2510
2511 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2512
2513 if (bad_page == NULL) {
2514 r = -ENOMEM;
2515 goto out;
2516 }
2517
35149e21
AL
2518 bad_pfn = page_to_pfn(bad_page);
2519
bf998156
HY
2520 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2521
2522 if (hwpoison_page == NULL) {
2523 r = -ENOMEM;
2524 goto out_free_0;
2525 }
2526
2527 hwpoison_pfn = page_to_pfn(hwpoison_page);
2528
edba23e5
GN
2529 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2530
2531 if (fault_page == NULL) {
2532 r = -ENOMEM;
2533 goto out_free_0;
2534 }
2535
2536 fault_pfn = page_to_pfn(fault_page);
2537
8437a617 2538 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
7f59f492
RR
2539 r = -ENOMEM;
2540 goto out_free_0;
2541 }
2542
e9b11c17 2543 r = kvm_arch_hardware_setup();
6aa8b732 2544 if (r < 0)
7f59f492 2545 goto out_free_0a;
6aa8b732 2546
002c7f7c
YS
2547 for_each_online_cpu(cpu) {
2548 smp_call_function_single(cpu,
e9b11c17 2549 kvm_arch_check_processor_compat,
8691e5a8 2550 &r, 1);
002c7f7c 2551 if (r < 0)
d2308784 2552 goto out_free_1;
002c7f7c
YS
2553 }
2554
774c47f1
AK
2555 r = register_cpu_notifier(&kvm_cpu_notifier);
2556 if (r)
d2308784 2557 goto out_free_2;
6aa8b732
AK
2558 register_reboot_notifier(&kvm_reboot_notifier);
2559
59ae6c6b
AK
2560 r = sysdev_class_register(&kvm_sysdev_class);
2561 if (r)
d2308784 2562 goto out_free_3;
59ae6c6b
AK
2563
2564 r = sysdev_register(&kvm_sysdev);
2565 if (r)
d2308784 2566 goto out_free_4;
59ae6c6b 2567
c16f862d 2568 /* A kmem cache lets us meet the alignment requirements of fx_save. */
0ee75bea
AK
2569 if (!vcpu_align)
2570 vcpu_align = __alignof__(struct kvm_vcpu);
2571 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
56919c5c 2572 0, NULL);
c16f862d
RR
2573 if (!kvm_vcpu_cache) {
2574 r = -ENOMEM;
d2308784 2575 goto out_free_5;
c16f862d
RR
2576 }
2577
af585b92
GN
2578 r = kvm_async_pf_init();
2579 if (r)
2580 goto out_free;
2581
6aa8b732 2582 kvm_chardev_ops.owner = module;
3d3aab1b
CB
2583 kvm_vm_fops.owner = module;
2584 kvm_vcpu_fops.owner = module;
6aa8b732
AK
2585
2586 r = misc_register(&kvm_dev);
2587 if (r) {
d77c26fc 2588 printk(KERN_ERR "kvm: misc device register failed\n");
af585b92 2589 goto out_unreg;
6aa8b732
AK
2590 }
2591
15ad7146
AK
2592 kvm_preempt_ops.sched_in = kvm_sched_in;
2593 kvm_preempt_ops.sched_out = kvm_sched_out;
2594
0ea4ed8e
DW
2595 kvm_init_debug();
2596
c7addb90 2597 return 0;
6aa8b732 2598
af585b92
GN
2599out_unreg:
2600 kvm_async_pf_deinit();
6aa8b732 2601out_free:
c16f862d 2602 kmem_cache_destroy(kvm_vcpu_cache);
d2308784 2603out_free_5:
59ae6c6b 2604 sysdev_unregister(&kvm_sysdev);
d2308784 2605out_free_4:
59ae6c6b 2606 sysdev_class_unregister(&kvm_sysdev_class);
d2308784 2607out_free_3:
6aa8b732 2608 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1 2609 unregister_cpu_notifier(&kvm_cpu_notifier);
d2308784 2610out_free_2:
d2308784 2611out_free_1:
e9b11c17 2612 kvm_arch_hardware_unsetup();
7f59f492
RR
2613out_free_0a:
2614 free_cpumask_var(cpus_hardware_enabled);
d2308784 2615out_free_0:
edba23e5
GN
2616 if (fault_page)
2617 __free_page(fault_page);
bf998156
HY
2618 if (hwpoison_page)
2619 __free_page(hwpoison_page);
d2308784 2620 __free_page(bad_page);
ca45aaae 2621out:
f8c16bba 2622 kvm_arch_exit();
d2308784 2623out_fail:
6aa8b732
AK
2624 return r;
2625}
cb498ea2 2626EXPORT_SYMBOL_GPL(kvm_init);
6aa8b732 2627
cb498ea2 2628void kvm_exit(void)
6aa8b732 2629{
0ea4ed8e 2630 kvm_exit_debug();
6aa8b732 2631 misc_deregister(&kvm_dev);
c16f862d 2632 kmem_cache_destroy(kvm_vcpu_cache);
af585b92 2633 kvm_async_pf_deinit();
59ae6c6b
AK
2634 sysdev_unregister(&kvm_sysdev);
2635 sysdev_class_unregister(&kvm_sysdev_class);
6aa8b732 2636 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 2637 unregister_cpu_notifier(&kvm_cpu_notifier);
75b7127c 2638 on_each_cpu(hardware_disable_nolock, NULL, 1);
e9b11c17 2639 kvm_arch_hardware_unsetup();
f8c16bba 2640 kvm_arch_exit();
7f59f492 2641 free_cpumask_var(cpus_hardware_enabled);
bf998156 2642 __free_page(hwpoison_page);
cea7bb21 2643 __free_page(bad_page);
6aa8b732 2644}
cb498ea2 2645EXPORT_SYMBOL_GPL(kvm_exit);
This page took 0.627269 seconds and 5 git commands to generate.