]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - virt/kvm/kvm_main.c
KVM: Move device assignment logic to common code
[thirdparty/kernel/stable.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.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
e2174021 18#include "iodev.h"
6aa8b732 19
edf88417 20#include <linux/kvm_host.h>
6aa8b732
AK
21#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
6aa8b732
AK
24#include <linux/percpu.h>
25#include <linux/gfp.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>
6aa8b732 44
e495606d 45#include <asm/processor.h>
e495606d
AK
46#include <asm/io.h>
47#include <asm/uaccess.h>
3e021bf5 48#include <asm/pgtable.h>
6aa8b732 49
5f94c174
LV
50#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51#include "coalesced_mmio.h"
52#endif
53
8a98f664
XZ
54#ifdef KVM_CAP_DEVICE_ASSIGNMENT
55#include <linux/pci.h>
56#include <linux/interrupt.h>
57#include "irq.h"
58#endif
59
6aa8b732
AK
60MODULE_AUTHOR("Qumranet");
61MODULE_LICENSE("GPL");
62
e9b11c17
ZX
63DEFINE_SPINLOCK(kvm_lock);
64LIST_HEAD(vm_list);
133de902 65
1b6c0168
AK
66static cpumask_t cpus_hardware_enabled;
67
c16f862d
RR
68struct kmem_cache *kvm_vcpu_cache;
69EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
1165f5fe 70
15ad7146
AK
71static __read_mostly struct preempt_ops kvm_preempt_ops;
72
76f7c879 73struct dentry *kvm_debugfs_dir;
6aa8b732 74
bccf2150
AK
75static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
76 unsigned long arg);
77
4ecac3fd
AK
78bool kvm_rebooting;
79
8a98f664
XZ
80#ifdef KVM_CAP_DEVICE_ASSIGNMENT
81static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
82 int assigned_dev_id)
83{
84 struct list_head *ptr;
85 struct kvm_assigned_dev_kernel *match;
86
87 list_for_each(ptr, head) {
88 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
89 if (match->assigned_dev_id == assigned_dev_id)
90 return match;
91 }
92 return NULL;
93}
94
95static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
96{
97 struct kvm_assigned_dev_kernel *assigned_dev;
98
99 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
100 interrupt_work);
101
102 /* This is taken to safely inject irq inside the guest. When
103 * the interrupt injection (or the ioapic code) uses a
104 * finer-grained lock, update this
105 */
106 mutex_lock(&assigned_dev->kvm->lock);
107 kvm_set_irq(assigned_dev->kvm,
108 assigned_dev->guest_irq, 1);
109 mutex_unlock(&assigned_dev->kvm->lock);
110 kvm_put_kvm(assigned_dev->kvm);
111}
112
113/* FIXME: Implement the OR logic needed to make shared interrupts on
114 * this line behave properly
115 */
116static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
117{
118 struct kvm_assigned_dev_kernel *assigned_dev =
119 (struct kvm_assigned_dev_kernel *) dev_id;
120
121 kvm_get_kvm(assigned_dev->kvm);
122 schedule_work(&assigned_dev->interrupt_work);
123 disable_irq_nosync(irq);
124 return IRQ_HANDLED;
125}
126
127/* Ack the irq line for an assigned device */
128static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
129{
130 struct kvm_assigned_dev_kernel *dev;
131
132 if (kian->gsi == -1)
133 return;
134
135 dev = container_of(kian, struct kvm_assigned_dev_kernel,
136 ack_notifier);
137 kvm_set_irq(dev->kvm, dev->guest_irq, 0);
138 enable_irq(dev->host_irq);
139}
140
141static void kvm_free_assigned_device(struct kvm *kvm,
142 struct kvm_assigned_dev_kernel
143 *assigned_dev)
144{
145 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
146 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
147
148 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
149
150 if (cancel_work_sync(&assigned_dev->interrupt_work))
151 /* We had pending work. That means we will have to take
152 * care of kvm_put_kvm.
153 */
154 kvm_put_kvm(kvm);
155
156 pci_release_regions(assigned_dev->dev);
157 pci_disable_device(assigned_dev->dev);
158 pci_dev_put(assigned_dev->dev);
159
160 list_del(&assigned_dev->list);
161 kfree(assigned_dev);
162}
163
164void kvm_free_all_assigned_devices(struct kvm *kvm)
165{
166 struct list_head *ptr, *ptr2;
167 struct kvm_assigned_dev_kernel *assigned_dev;
168
169 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
170 assigned_dev = list_entry(ptr,
171 struct kvm_assigned_dev_kernel,
172 list);
173
174 kvm_free_assigned_device(kvm, assigned_dev);
175 }
176}
177
178static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
179 struct kvm_assigned_irq
180 *assigned_irq)
181{
182 int r = 0;
183 struct kvm_assigned_dev_kernel *match;
184
185 mutex_lock(&kvm->lock);
186
187 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
188 assigned_irq->assigned_dev_id);
189 if (!match) {
190 mutex_unlock(&kvm->lock);
191 return -EINVAL;
192 }
193
194 if (match->irq_requested) {
195 match->guest_irq = assigned_irq->guest_irq;
196 match->ack_notifier.gsi = assigned_irq->guest_irq;
197 mutex_unlock(&kvm->lock);
198 return 0;
199 }
200
201 INIT_WORK(&match->interrupt_work,
202 kvm_assigned_dev_interrupt_work_handler);
203
204 if (irqchip_in_kernel(kvm)) {
205 if (!capable(CAP_SYS_RAWIO)) {
206 r = -EPERM;
207 goto out_release;
208 }
209
210 if (assigned_irq->host_irq)
211 match->host_irq = assigned_irq->host_irq;
212 else
213 match->host_irq = match->dev->irq;
214 match->guest_irq = assigned_irq->guest_irq;
215 match->ack_notifier.gsi = assigned_irq->guest_irq;
216 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
217 kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
218
219 /* Even though this is PCI, we don't want to use shared
220 * interrupts. Sharing host devices with guest-assigned devices
221 * on the same interrupt line is not a happy situation: there
222 * are going to be long delays in accepting, acking, etc.
223 */
224 if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
225 "kvm_assigned_device", (void *)match)) {
226 r = -EIO;
227 goto out_release;
228 }
229 }
230
231 match->irq_requested = true;
232 mutex_unlock(&kvm->lock);
233 return r;
234out_release:
235 mutex_unlock(&kvm->lock);
236 kvm_free_assigned_device(kvm, match);
237 return r;
238}
239
240static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
241 struct kvm_assigned_pci_dev *assigned_dev)
242{
243 int r = 0;
244 struct kvm_assigned_dev_kernel *match;
245 struct pci_dev *dev;
246
247 mutex_lock(&kvm->lock);
248
249 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
250 assigned_dev->assigned_dev_id);
251 if (match) {
252 /* device already assigned */
253 r = -EINVAL;
254 goto out;
255 }
256
257 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
258 if (match == NULL) {
259 printk(KERN_INFO "%s: Couldn't allocate memory\n",
260 __func__);
261 r = -ENOMEM;
262 goto out;
263 }
264 dev = pci_get_bus_and_slot(assigned_dev->busnr,
265 assigned_dev->devfn);
266 if (!dev) {
267 printk(KERN_INFO "%s: host device not found\n", __func__);
268 r = -EINVAL;
269 goto out_free;
270 }
271 if (pci_enable_device(dev)) {
272 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
273 r = -EBUSY;
274 goto out_put;
275 }
276 r = pci_request_regions(dev, "kvm_assigned_device");
277 if (r) {
278 printk(KERN_INFO "%s: Could not get access to device regions\n",
279 __func__);
280 goto out_disable;
281 }
282 match->assigned_dev_id = assigned_dev->assigned_dev_id;
283 match->host_busnr = assigned_dev->busnr;
284 match->host_devfn = assigned_dev->devfn;
285 match->dev = dev;
286
287 match->kvm = kvm;
288
289 list_add(&match->list, &kvm->arch.assigned_dev_head);
290
291 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
292 r = kvm_iommu_map_guest(kvm, match);
293 if (r)
294 goto out_list_del;
295 }
296
297out:
298 mutex_unlock(&kvm->lock);
299 return r;
300out_list_del:
301 list_del(&match->list);
302 pci_release_regions(dev);
303out_disable:
304 pci_disable_device(dev);
305out_put:
306 pci_dev_put(dev);
307out_free:
308 kfree(match);
309 mutex_unlock(&kvm->lock);
310 return r;
311}
312#endif
313
5aacf0ca
JM
314static inline int valid_vcpu(int n)
315{
316 return likely(n >= 0 && n < KVM_MAX_VCPUS);
317}
318
e5fcfc82 319static inline int is_mmio_pfn(pfn_t pfn)
cbff90a7
BAY
320{
321 if (pfn_valid(pfn))
322 return PageReserved(pfn_to_page(pfn));
323
324 return true;
325}
326
bccf2150
AK
327/*
328 * Switches to specified vcpu, until a matching vcpu_put()
329 */
313a3dc7 330void vcpu_load(struct kvm_vcpu *vcpu)
6aa8b732 331{
15ad7146
AK
332 int cpu;
333
bccf2150 334 mutex_lock(&vcpu->mutex);
15ad7146
AK
335 cpu = get_cpu();
336 preempt_notifier_register(&vcpu->preempt_notifier);
313a3dc7 337 kvm_arch_vcpu_load(vcpu, cpu);
15ad7146 338 put_cpu();
6aa8b732
AK
339}
340
313a3dc7 341void vcpu_put(struct kvm_vcpu *vcpu)
6aa8b732 342{
15ad7146 343 preempt_disable();
313a3dc7 344 kvm_arch_vcpu_put(vcpu);
15ad7146
AK
345 preempt_notifier_unregister(&vcpu->preempt_notifier);
346 preempt_enable();
6aa8b732
AK
347 mutex_unlock(&vcpu->mutex);
348}
349
d9e368d6
AK
350static void ack_flush(void *_completed)
351{
d9e368d6
AK
352}
353
354void kvm_flush_remote_tlbs(struct kvm *kvm)
355{
597a5f55 356 int i, cpu, me;
d9e368d6
AK
357 cpumask_t cpus;
358 struct kvm_vcpu *vcpu;
d9e368d6 359
597a5f55 360 me = get_cpu();
d9e368d6 361 cpus_clear(cpus);
fb3f0f51
RR
362 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
363 vcpu = kvm->vcpus[i];
364 if (!vcpu)
365 continue;
3176bc3e 366 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
d9e368d6
AK
367 continue;
368 cpu = vcpu->cpu;
597a5f55 369 if (cpu != -1 && cpu != me)
49d3bd7e 370 cpu_set(cpu, cpus);
d9e368d6 371 }
0f74a24c 372 if (cpus_empty(cpus))
597a5f55 373 goto out;
0f74a24c 374 ++kvm->stat.remote_tlb_flush;
49d3bd7e 375 smp_call_function_mask(cpus, ack_flush, NULL, 1);
597a5f55
AK
376out:
377 put_cpu();
d9e368d6
AK
378}
379
2e53d63a
MT
380void kvm_reload_remote_mmus(struct kvm *kvm)
381{
597a5f55 382 int i, cpu, me;
2e53d63a
MT
383 cpumask_t cpus;
384 struct kvm_vcpu *vcpu;
385
597a5f55 386 me = get_cpu();
2e53d63a
MT
387 cpus_clear(cpus);
388 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
389 vcpu = kvm->vcpus[i];
390 if (!vcpu)
391 continue;
392 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
393 continue;
394 cpu = vcpu->cpu;
597a5f55 395 if (cpu != -1 && cpu != me)
2e53d63a
MT
396 cpu_set(cpu, cpus);
397 }
398 if (cpus_empty(cpus))
597a5f55 399 goto out;
2e53d63a 400 smp_call_function_mask(cpus, ack_flush, NULL, 1);
597a5f55
AK
401out:
402 put_cpu();
2e53d63a
MT
403}
404
405
fb3f0f51
RR
406int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
407{
408 struct page *page;
409 int r;
410
411 mutex_init(&vcpu->mutex);
412 vcpu->cpu = -1;
fb3f0f51
RR
413 vcpu->kvm = kvm;
414 vcpu->vcpu_id = id;
b6958ce4 415 init_waitqueue_head(&vcpu->wq);
fb3f0f51
RR
416
417 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
418 if (!page) {
419 r = -ENOMEM;
420 goto fail;
421 }
422 vcpu->run = page_address(page);
423
e9b11c17 424 r = kvm_arch_vcpu_init(vcpu);
fb3f0f51 425 if (r < 0)
e9b11c17 426 goto fail_free_run;
fb3f0f51
RR
427 return 0;
428
fb3f0f51
RR
429fail_free_run:
430 free_page((unsigned long)vcpu->run);
431fail:
76fafa5e 432 return r;
fb3f0f51
RR
433}
434EXPORT_SYMBOL_GPL(kvm_vcpu_init);
435
436void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
437{
e9b11c17 438 kvm_arch_vcpu_uninit(vcpu);
fb3f0f51
RR
439 free_page((unsigned long)vcpu->run);
440}
441EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
442
e930bffe
AA
443#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
444static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
445{
446 return container_of(mn, struct kvm, mmu_notifier);
447}
448
449static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
450 struct mm_struct *mm,
451 unsigned long address)
452{
453 struct kvm *kvm = mmu_notifier_to_kvm(mn);
454 int need_tlb_flush;
455
456 /*
457 * When ->invalidate_page runs, the linux pte has been zapped
458 * already but the page is still allocated until
459 * ->invalidate_page returns. So if we increase the sequence
460 * here the kvm page fault will notice if the spte can't be
461 * established because the page is going to be freed. If
462 * instead the kvm page fault establishes the spte before
463 * ->invalidate_page runs, kvm_unmap_hva will release it
464 * before returning.
465 *
466 * The sequence increase only need to be seen at spin_unlock
467 * time, and not at spin_lock time.
468 *
469 * Increasing the sequence after the spin_unlock would be
470 * unsafe because the kvm page fault could then establish the
471 * pte after kvm_unmap_hva returned, without noticing the page
472 * is going to be freed.
473 */
474 spin_lock(&kvm->mmu_lock);
475 kvm->mmu_notifier_seq++;
476 need_tlb_flush = kvm_unmap_hva(kvm, address);
477 spin_unlock(&kvm->mmu_lock);
478
479 /* we've to flush the tlb before the pages can be freed */
480 if (need_tlb_flush)
481 kvm_flush_remote_tlbs(kvm);
482
483}
484
485static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
486 struct mm_struct *mm,
487 unsigned long start,
488 unsigned long end)
489{
490 struct kvm *kvm = mmu_notifier_to_kvm(mn);
491 int need_tlb_flush = 0;
492
493 spin_lock(&kvm->mmu_lock);
494 /*
495 * The count increase must become visible at unlock time as no
496 * spte can be established without taking the mmu_lock and
497 * count is also read inside the mmu_lock critical section.
498 */
499 kvm->mmu_notifier_count++;
500 for (; start < end; start += PAGE_SIZE)
501 need_tlb_flush |= kvm_unmap_hva(kvm, start);
502 spin_unlock(&kvm->mmu_lock);
503
504 /* we've to flush the tlb before the pages can be freed */
505 if (need_tlb_flush)
506 kvm_flush_remote_tlbs(kvm);
507}
508
509static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
510 struct mm_struct *mm,
511 unsigned long start,
512 unsigned long end)
513{
514 struct kvm *kvm = mmu_notifier_to_kvm(mn);
515
516 spin_lock(&kvm->mmu_lock);
517 /*
518 * This sequence increase will notify the kvm page fault that
519 * the page that is going to be mapped in the spte could have
520 * been freed.
521 */
522 kvm->mmu_notifier_seq++;
523 /*
524 * The above sequence increase must be visible before the
525 * below count decrease but both values are read by the kvm
526 * page fault under mmu_lock spinlock so we don't need to add
527 * a smb_wmb() here in between the two.
528 */
529 kvm->mmu_notifier_count--;
530 spin_unlock(&kvm->mmu_lock);
531
532 BUG_ON(kvm->mmu_notifier_count < 0);
533}
534
535static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
536 struct mm_struct *mm,
537 unsigned long address)
538{
539 struct kvm *kvm = mmu_notifier_to_kvm(mn);
540 int young;
541
542 spin_lock(&kvm->mmu_lock);
543 young = kvm_age_hva(kvm, address);
544 spin_unlock(&kvm->mmu_lock);
545
546 if (young)
547 kvm_flush_remote_tlbs(kvm);
548
549 return young;
550}
551
552static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
553 .invalidate_page = kvm_mmu_notifier_invalidate_page,
554 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
555 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
556 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
557};
558#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
559
f17abe9a 560static struct kvm *kvm_create_vm(void)
6aa8b732 561{
d19a9cd2 562 struct kvm *kvm = kvm_arch_create_vm();
5f94c174
LV
563#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
564 struct page *page;
565#endif
6aa8b732 566
d19a9cd2
ZX
567 if (IS_ERR(kvm))
568 goto out;
6aa8b732 569
5f94c174
LV
570#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
571 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
572 if (!page) {
573 kfree(kvm);
574 return ERR_PTR(-ENOMEM);
575 }
576 kvm->coalesced_mmio_ring =
577 (struct kvm_coalesced_mmio_ring *)page_address(page);
578#endif
579
e930bffe
AA
580#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
581 {
582 int err;
583 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
584 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
585 if (err) {
586#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
587 put_page(page);
588#endif
589 kfree(kvm);
590 return ERR_PTR(err);
591 }
592 }
593#endif
594
6d4e4c4f
AK
595 kvm->mm = current->mm;
596 atomic_inc(&kvm->mm->mm_count);
aaee2c94 597 spin_lock_init(&kvm->mmu_lock);
74906345 598 kvm_io_bus_init(&kvm->pio_bus);
11ec2804 599 mutex_init(&kvm->lock);
2eeb2e94 600 kvm_io_bus_init(&kvm->mmio_bus);
72dc67a6 601 init_rwsem(&kvm->slots_lock);
d39f13b0 602 atomic_set(&kvm->users_count, 1);
5e58cfe4
RR
603 spin_lock(&kvm_lock);
604 list_add(&kvm->vm_list, &vm_list);
605 spin_unlock(&kvm_lock);
5f94c174
LV
606#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
607 kvm_coalesced_mmio_init(kvm);
608#endif
d19a9cd2 609out:
f17abe9a
AK
610 return kvm;
611}
612
6aa8b732
AK
613/*
614 * Free any memory in @free but not in @dont.
615 */
616static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
617 struct kvm_memory_slot *dont)
618{
290fc38d
IE
619 if (!dont || free->rmap != dont->rmap)
620 vfree(free->rmap);
6aa8b732
AK
621
622 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
623 vfree(free->dirty_bitmap);
624
05da4558
MT
625 if (!dont || free->lpage_info != dont->lpage_info)
626 vfree(free->lpage_info);
627
6aa8b732 628 free->npages = 0;
8b6d44c7 629 free->dirty_bitmap = NULL;
8d4e1288 630 free->rmap = NULL;
05da4558 631 free->lpage_info = NULL;
6aa8b732
AK
632}
633
d19a9cd2 634void kvm_free_physmem(struct kvm *kvm)
6aa8b732
AK
635{
636 int i;
637
638 for (i = 0; i < kvm->nmemslots; ++i)
8b6d44c7 639 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
6aa8b732
AK
640}
641
f17abe9a
AK
642static void kvm_destroy_vm(struct kvm *kvm)
643{
6d4e4c4f
AK
644 struct mm_struct *mm = kvm->mm;
645
133de902
AK
646 spin_lock(&kvm_lock);
647 list_del(&kvm->vm_list);
648 spin_unlock(&kvm_lock);
74906345 649 kvm_io_bus_destroy(&kvm->pio_bus);
2eeb2e94 650 kvm_io_bus_destroy(&kvm->mmio_bus);
5f94c174
LV
651#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
652 if (kvm->coalesced_mmio_ring != NULL)
653 free_page((unsigned long)kvm->coalesced_mmio_ring);
e930bffe
AA
654#endif
655#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
656 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
5f94c174 657#endif
d19a9cd2 658 kvm_arch_destroy_vm(kvm);
6d4e4c4f 659 mmdrop(mm);
f17abe9a
AK
660}
661
d39f13b0
IE
662void kvm_get_kvm(struct kvm *kvm)
663{
664 atomic_inc(&kvm->users_count);
665}
666EXPORT_SYMBOL_GPL(kvm_get_kvm);
667
668void kvm_put_kvm(struct kvm *kvm)
669{
670 if (atomic_dec_and_test(&kvm->users_count))
671 kvm_destroy_vm(kvm);
672}
673EXPORT_SYMBOL_GPL(kvm_put_kvm);
674
675
f17abe9a
AK
676static int kvm_vm_release(struct inode *inode, struct file *filp)
677{
678 struct kvm *kvm = filp->private_data;
679
d39f13b0 680 kvm_put_kvm(kvm);
6aa8b732
AK
681 return 0;
682}
683
6aa8b732
AK
684/*
685 * Allocate some memory and give it an address in the guest physical address
686 * space.
687 *
688 * Discontiguous memory is allowed, mostly for framebuffers.
f78e0e2e 689 *
10589a46 690 * Must be called holding mmap_sem for write.
6aa8b732 691 */
f78e0e2e
SY
692int __kvm_set_memory_region(struct kvm *kvm,
693 struct kvm_userspace_memory_region *mem,
694 int user_alloc)
6aa8b732
AK
695{
696 int r;
697 gfn_t base_gfn;
698 unsigned long npages;
699 unsigned long i;
700 struct kvm_memory_slot *memslot;
701 struct kvm_memory_slot old, new;
6aa8b732
AK
702
703 r = -EINVAL;
704 /* General sanity checks */
705 if (mem->memory_size & (PAGE_SIZE - 1))
706 goto out;
707 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
708 goto out;
e0d62c7f 709 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
6aa8b732
AK
710 goto out;
711 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
712 goto out;
713
714 memslot = &kvm->memslots[mem->slot];
715 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
716 npages = mem->memory_size >> PAGE_SHIFT;
717
718 if (!npages)
719 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
720
6aa8b732
AK
721 new = old = *memslot;
722
723 new.base_gfn = base_gfn;
724 new.npages = npages;
725 new.flags = mem->flags;
726
727 /* Disallow changing a memory slot's size. */
728 r = -EINVAL;
729 if (npages && old.npages && npages != old.npages)
f78e0e2e 730 goto out_free;
6aa8b732
AK
731
732 /* Check for overlaps */
733 r = -EEXIST;
734 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
735 struct kvm_memory_slot *s = &kvm->memslots[i];
736
737 if (s == memslot)
738 continue;
739 if (!((base_gfn + npages <= s->base_gfn) ||
740 (base_gfn >= s->base_gfn + s->npages)))
f78e0e2e 741 goto out_free;
6aa8b732 742 }
6aa8b732 743
6aa8b732
AK
744 /* Free page dirty bitmap if unneeded */
745 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 746 new.dirty_bitmap = NULL;
6aa8b732
AK
747
748 r = -ENOMEM;
749
750 /* Allocate if a slot is being created */
eff0114a 751#ifndef CONFIG_S390
8d4e1288 752 if (npages && !new.rmap) {
d77c26fc 753 new.rmap = vmalloc(npages * sizeof(struct page *));
290fc38d
IE
754
755 if (!new.rmap)
f78e0e2e 756 goto out_free;
290fc38d 757
290fc38d 758 memset(new.rmap, 0, npages * sizeof(*new.rmap));
8d4e1288 759
80b14b5b 760 new.user_alloc = user_alloc;
604b38ac
AA
761 /*
762 * hva_to_rmmap() serialzies with the mmu_lock and to be
763 * safe it has to ignore memslots with !user_alloc &&
764 * !userspace_addr.
765 */
766 if (user_alloc)
767 new.userspace_addr = mem->userspace_addr;
768 else
769 new.userspace_addr = 0;
6aa8b732 770 }
05da4558
MT
771 if (npages && !new.lpage_info) {
772 int largepages = npages / KVM_PAGES_PER_HPAGE;
773 if (npages % KVM_PAGES_PER_HPAGE)
774 largepages++;
775 if (base_gfn % KVM_PAGES_PER_HPAGE)
776 largepages++;
777
778 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
779
780 if (!new.lpage_info)
781 goto out_free;
782
783 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
784
785 if (base_gfn % KVM_PAGES_PER_HPAGE)
786 new.lpage_info[0].write_count = 1;
787 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
788 new.lpage_info[largepages-1].write_count = 1;
789 }
6aa8b732
AK
790
791 /* Allocate page dirty bitmap if needed */
792 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
793 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
794
795 new.dirty_bitmap = vmalloc(dirty_bytes);
796 if (!new.dirty_bitmap)
f78e0e2e 797 goto out_free;
6aa8b732
AK
798 memset(new.dirty_bitmap, 0, dirty_bytes);
799 }
eff0114a 800#endif /* not defined CONFIG_S390 */
6aa8b732 801
34d4cb8f
MT
802 if (!npages)
803 kvm_arch_flush_shadow(kvm);
804
604b38ac
AA
805 spin_lock(&kvm->mmu_lock);
806 if (mem->slot >= kvm->nmemslots)
807 kvm->nmemslots = mem->slot + 1;
808
3ad82a7e 809 *memslot = new;
604b38ac 810 spin_unlock(&kvm->mmu_lock);
3ad82a7e 811
0de10343
ZX
812 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
813 if (r) {
604b38ac 814 spin_lock(&kvm->mmu_lock);
0de10343 815 *memslot = old;
604b38ac 816 spin_unlock(&kvm->mmu_lock);
0de10343 817 goto out_free;
82ce2c96
IE
818 }
819
6aa8b732 820 kvm_free_physmem_slot(&old, &new);
8a98f664 821#ifdef CONFIG_DMAR
62c476c7
BAY
822 /* map the pages in iommu page table */
823 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
824 if (r)
825 goto out;
8a98f664 826#endif
6aa8b732
AK
827 return 0;
828
f78e0e2e 829out_free:
6aa8b732
AK
830 kvm_free_physmem_slot(&new, &old);
831out:
832 return r;
210c7c4d
IE
833
834}
f78e0e2e
SY
835EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
836
837int kvm_set_memory_region(struct kvm *kvm,
838 struct kvm_userspace_memory_region *mem,
839 int user_alloc)
840{
841 int r;
842
72dc67a6 843 down_write(&kvm->slots_lock);
f78e0e2e 844 r = __kvm_set_memory_region(kvm, mem, user_alloc);
72dc67a6 845 up_write(&kvm->slots_lock);
f78e0e2e
SY
846 return r;
847}
210c7c4d
IE
848EXPORT_SYMBOL_GPL(kvm_set_memory_region);
849
1fe779f8
CO
850int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
851 struct
852 kvm_userspace_memory_region *mem,
853 int user_alloc)
210c7c4d 854{
e0d62c7f
IE
855 if (mem->slot >= KVM_MEMORY_SLOTS)
856 return -EINVAL;
210c7c4d 857 return kvm_set_memory_region(kvm, mem, user_alloc);
6aa8b732
AK
858}
859
5bb064dc
ZX
860int kvm_get_dirty_log(struct kvm *kvm,
861 struct kvm_dirty_log *log, int *is_dirty)
6aa8b732
AK
862{
863 struct kvm_memory_slot *memslot;
864 int r, i;
865 int n;
866 unsigned long any = 0;
867
6aa8b732
AK
868 r = -EINVAL;
869 if (log->slot >= KVM_MEMORY_SLOTS)
870 goto out;
871
872 memslot = &kvm->memslots[log->slot];
873 r = -ENOENT;
874 if (!memslot->dirty_bitmap)
875 goto out;
876
cd1a4a98 877 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
6aa8b732 878
cd1a4a98 879 for (i = 0; !any && i < n/sizeof(long); ++i)
6aa8b732
AK
880 any = memslot->dirty_bitmap[i];
881
882 r = -EFAULT;
883 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
884 goto out;
885
5bb064dc
ZX
886 if (any)
887 *is_dirty = 1;
6aa8b732
AK
888
889 r = 0;
6aa8b732 890out:
6aa8b732
AK
891 return r;
892}
893
cea7bb21
IE
894int is_error_page(struct page *page)
895{
896 return page == bad_page;
897}
898EXPORT_SYMBOL_GPL(is_error_page);
899
35149e21
AL
900int is_error_pfn(pfn_t pfn)
901{
902 return pfn == bad_pfn;
903}
904EXPORT_SYMBOL_GPL(is_error_pfn);
905
f9d46eb0
IE
906static inline unsigned long bad_hva(void)
907{
908 return PAGE_OFFSET;
909}
910
911int kvm_is_error_hva(unsigned long addr)
912{
913 return addr == bad_hva();
914}
915EXPORT_SYMBOL_GPL(kvm_is_error_hva);
916
e8207547 917static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
6aa8b732
AK
918{
919 int i;
920
921 for (i = 0; i < kvm->nmemslots; ++i) {
922 struct kvm_memory_slot *memslot = &kvm->memslots[i];
923
924 if (gfn >= memslot->base_gfn
925 && gfn < memslot->base_gfn + memslot->npages)
926 return memslot;
927 }
8b6d44c7 928 return NULL;
6aa8b732 929}
e8207547
AK
930
931struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
932{
933 gfn = unalias_gfn(kvm, gfn);
934 return __gfn_to_memslot(kvm, gfn);
935}
6aa8b732 936
e0d62c7f
IE
937int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
938{
939 int i;
940
941 gfn = unalias_gfn(kvm, gfn);
942 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
943 struct kvm_memory_slot *memslot = &kvm->memslots[i];
944
945 if (gfn >= memslot->base_gfn
946 && gfn < memslot->base_gfn + memslot->npages)
947 return 1;
948 }
949 return 0;
950}
951EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
952
05da4558 953unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
539cb660
IE
954{
955 struct kvm_memory_slot *slot;
956
957 gfn = unalias_gfn(kvm, gfn);
958 slot = __gfn_to_memslot(kvm, gfn);
959 if (!slot)
960 return bad_hva();
961 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
962}
0d150298 963EXPORT_SYMBOL_GPL(gfn_to_hva);
539cb660 964
35149e21 965pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
954bbbc2 966{
8d4e1288 967 struct page *page[1];
539cb660 968 unsigned long addr;
8d4e1288 969 int npages;
2e2e3738 970 pfn_t pfn;
954bbbc2 971
60395224
AK
972 might_sleep();
973
539cb660
IE
974 addr = gfn_to_hva(kvm, gfn);
975 if (kvm_is_error_hva(addr)) {
8a7ae055 976 get_page(bad_page);
35149e21 977 return page_to_pfn(bad_page);
8a7ae055 978 }
8d4e1288 979
4c2155ce 980 npages = get_user_pages_fast(addr, 1, 1, page);
539cb660 981
2e2e3738
AL
982 if (unlikely(npages != 1)) {
983 struct vm_area_struct *vma;
984
4c2155ce 985 down_read(&current->mm->mmap_sem);
2e2e3738 986 vma = find_vma(current->mm, addr);
4c2155ce 987
2e2e3738
AL
988 if (vma == NULL || addr < vma->vm_start ||
989 !(vma->vm_flags & VM_PFNMAP)) {
4c2155ce 990 up_read(&current->mm->mmap_sem);
2e2e3738
AL
991 get_page(bad_page);
992 return page_to_pfn(bad_page);
993 }
994
995 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
4c2155ce 996 up_read(&current->mm->mmap_sem);
cbff90a7 997 BUG_ON(!is_mmio_pfn(pfn));
2e2e3738
AL
998 } else
999 pfn = page_to_pfn(page[0]);
8d4e1288 1000
2e2e3738 1001 return pfn;
35149e21
AL
1002}
1003
1004EXPORT_SYMBOL_GPL(gfn_to_pfn);
1005
1006struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1007{
2e2e3738
AL
1008 pfn_t pfn;
1009
1010 pfn = gfn_to_pfn(kvm, gfn);
cbff90a7 1011 if (!is_mmio_pfn(pfn))
2e2e3738
AL
1012 return pfn_to_page(pfn);
1013
cbff90a7 1014 WARN_ON(is_mmio_pfn(pfn));
2e2e3738
AL
1015
1016 get_page(bad_page);
1017 return bad_page;
954bbbc2 1018}
aab61cc0 1019
954bbbc2
AK
1020EXPORT_SYMBOL_GPL(gfn_to_page);
1021
b4231d61
IE
1022void kvm_release_page_clean(struct page *page)
1023{
35149e21 1024 kvm_release_pfn_clean(page_to_pfn(page));
b4231d61
IE
1025}
1026EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1027
35149e21
AL
1028void kvm_release_pfn_clean(pfn_t pfn)
1029{
cbff90a7 1030 if (!is_mmio_pfn(pfn))
2e2e3738 1031 put_page(pfn_to_page(pfn));
35149e21
AL
1032}
1033EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1034
b4231d61 1035void kvm_release_page_dirty(struct page *page)
8a7ae055 1036{
35149e21
AL
1037 kvm_release_pfn_dirty(page_to_pfn(page));
1038}
1039EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1040
1041void kvm_release_pfn_dirty(pfn_t pfn)
1042{
1043 kvm_set_pfn_dirty(pfn);
1044 kvm_release_pfn_clean(pfn);
1045}
1046EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1047
1048void kvm_set_page_dirty(struct page *page)
1049{
1050 kvm_set_pfn_dirty(page_to_pfn(page));
1051}
1052EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1053
1054void kvm_set_pfn_dirty(pfn_t pfn)
1055{
cbff90a7 1056 if (!is_mmio_pfn(pfn)) {
2e2e3738
AL
1057 struct page *page = pfn_to_page(pfn);
1058 if (!PageReserved(page))
1059 SetPageDirty(page);
1060 }
8a7ae055 1061}
35149e21
AL
1062EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1063
1064void kvm_set_pfn_accessed(pfn_t pfn)
1065{
cbff90a7 1066 if (!is_mmio_pfn(pfn))
2e2e3738 1067 mark_page_accessed(pfn_to_page(pfn));
35149e21
AL
1068}
1069EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1070
1071void kvm_get_pfn(pfn_t pfn)
1072{
cbff90a7 1073 if (!is_mmio_pfn(pfn))
2e2e3738 1074 get_page(pfn_to_page(pfn));
35149e21
AL
1075}
1076EXPORT_SYMBOL_GPL(kvm_get_pfn);
8a7ae055 1077
195aefde
IE
1078static int next_segment(unsigned long len, int offset)
1079{
1080 if (len > PAGE_SIZE - offset)
1081 return PAGE_SIZE - offset;
1082 else
1083 return len;
1084}
1085
1086int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1087 int len)
1088{
e0506bcb
IE
1089 int r;
1090 unsigned long addr;
195aefde 1091
e0506bcb
IE
1092 addr = gfn_to_hva(kvm, gfn);
1093 if (kvm_is_error_hva(addr))
1094 return -EFAULT;
1095 r = copy_from_user(data, (void __user *)addr + offset, len);
1096 if (r)
195aefde 1097 return -EFAULT;
195aefde
IE
1098 return 0;
1099}
1100EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1101
1102int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1103{
1104 gfn_t gfn = gpa >> PAGE_SHIFT;
1105 int seg;
1106 int offset = offset_in_page(gpa);
1107 int ret;
1108
1109 while ((seg = next_segment(len, offset)) != 0) {
1110 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1111 if (ret < 0)
1112 return ret;
1113 offset = 0;
1114 len -= seg;
1115 data += seg;
1116 ++gfn;
1117 }
1118 return 0;
1119}
1120EXPORT_SYMBOL_GPL(kvm_read_guest);
1121
7ec54588
MT
1122int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1123 unsigned long len)
1124{
1125 int r;
1126 unsigned long addr;
1127 gfn_t gfn = gpa >> PAGE_SHIFT;
1128 int offset = offset_in_page(gpa);
1129
1130 addr = gfn_to_hva(kvm, gfn);
1131 if (kvm_is_error_hva(addr))
1132 return -EFAULT;
0aac03f0 1133 pagefault_disable();
7ec54588 1134 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
0aac03f0 1135 pagefault_enable();
7ec54588
MT
1136 if (r)
1137 return -EFAULT;
1138 return 0;
1139}
1140EXPORT_SYMBOL(kvm_read_guest_atomic);
1141
195aefde
IE
1142int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1143 int offset, int len)
1144{
e0506bcb
IE
1145 int r;
1146 unsigned long addr;
195aefde 1147
e0506bcb
IE
1148 addr = gfn_to_hva(kvm, gfn);
1149 if (kvm_is_error_hva(addr))
1150 return -EFAULT;
1151 r = copy_to_user((void __user *)addr + offset, data, len);
1152 if (r)
195aefde 1153 return -EFAULT;
195aefde
IE
1154 mark_page_dirty(kvm, gfn);
1155 return 0;
1156}
1157EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1158
1159int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1160 unsigned long len)
1161{
1162 gfn_t gfn = gpa >> PAGE_SHIFT;
1163 int seg;
1164 int offset = offset_in_page(gpa);
1165 int ret;
1166
1167 while ((seg = next_segment(len, offset)) != 0) {
1168 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1169 if (ret < 0)
1170 return ret;
1171 offset = 0;
1172 len -= seg;
1173 data += seg;
1174 ++gfn;
1175 }
1176 return 0;
1177}
1178
1179int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1180{
3e021bf5 1181 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
195aefde
IE
1182}
1183EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1184
1185int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1186{
1187 gfn_t gfn = gpa >> PAGE_SHIFT;
1188 int seg;
1189 int offset = offset_in_page(gpa);
1190 int ret;
1191
1192 while ((seg = next_segment(len, offset)) != 0) {
1193 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1194 if (ret < 0)
1195 return ret;
1196 offset = 0;
1197 len -= seg;
1198 ++gfn;
1199 }
1200 return 0;
1201}
1202EXPORT_SYMBOL_GPL(kvm_clear_guest);
1203
6aa8b732
AK
1204void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1205{
31389947 1206 struct kvm_memory_slot *memslot;
6aa8b732 1207
3b6fff19 1208 gfn = unalias_gfn(kvm, gfn);
7e9d619d
RR
1209 memslot = __gfn_to_memslot(kvm, gfn);
1210 if (memslot && memslot->dirty_bitmap) {
1211 unsigned long rel_gfn = gfn - memslot->base_gfn;
6aa8b732 1212
7e9d619d
RR
1213 /* avoid RMW */
1214 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1215 set_bit(rel_gfn, memslot->dirty_bitmap);
6aa8b732
AK
1216 }
1217}
1218
b6958ce4
ED
1219/*
1220 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1221 */
8776e519 1222void kvm_vcpu_block(struct kvm_vcpu *vcpu)
d3bef15f 1223{
e5c239cf
MT
1224 DEFINE_WAIT(wait);
1225
1226 for (;;) {
1227 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1228
d7690175
MT
1229 if (kvm_cpu_has_interrupt(vcpu) ||
1230 kvm_cpu_has_pending_timer(vcpu) ||
1231 kvm_arch_vcpu_runnable(vcpu)) {
1232 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
e5c239cf 1233 break;
d7690175 1234 }
e5c239cf
MT
1235 if (signal_pending(current))
1236 break;
1237
b6958ce4
ED
1238 vcpu_put(vcpu);
1239 schedule();
1240 vcpu_load(vcpu);
1241 }
d3bef15f 1242
e5c239cf 1243 finish_wait(&vcpu->wq, &wait);
b6958ce4
ED
1244}
1245
6aa8b732
AK
1246void kvm_resched(struct kvm_vcpu *vcpu)
1247{
3fca0365
YD
1248 if (!need_resched())
1249 return;
6aa8b732 1250 cond_resched();
6aa8b732
AK
1251}
1252EXPORT_SYMBOL_GPL(kvm_resched);
1253
e4a533a4 1254static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
9a2bb7f4
AK
1255{
1256 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
9a2bb7f4
AK
1257 struct page *page;
1258
e4a533a4 1259 if (vmf->pgoff == 0)
039576c0 1260 page = virt_to_page(vcpu->run);
09566765 1261#ifdef CONFIG_X86
e4a533a4 1262 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
ad312c7c 1263 page = virt_to_page(vcpu->arch.pio_data);
5f94c174
LV
1264#endif
1265#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1266 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1267 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
09566765 1268#endif
039576c0 1269 else
e4a533a4 1270 return VM_FAULT_SIGBUS;
9a2bb7f4 1271 get_page(page);
e4a533a4
NP
1272 vmf->page = page;
1273 return 0;
9a2bb7f4
AK
1274}
1275
1276static struct vm_operations_struct kvm_vcpu_vm_ops = {
e4a533a4 1277 .fault = kvm_vcpu_fault,
9a2bb7f4
AK
1278};
1279
1280static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1281{
1282 vma->vm_ops = &kvm_vcpu_vm_ops;
1283 return 0;
1284}
1285
bccf2150
AK
1286static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1287{
1288 struct kvm_vcpu *vcpu = filp->private_data;
1289
66c0b394 1290 kvm_put_kvm(vcpu->kvm);
bccf2150
AK
1291 return 0;
1292}
1293
5c502742 1294static const struct file_operations kvm_vcpu_fops = {
bccf2150
AK
1295 .release = kvm_vcpu_release,
1296 .unlocked_ioctl = kvm_vcpu_ioctl,
1297 .compat_ioctl = kvm_vcpu_ioctl,
9a2bb7f4 1298 .mmap = kvm_vcpu_mmap,
bccf2150
AK
1299};
1300
1301/*
1302 * Allocates an inode for the vcpu.
1303 */
1304static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1305{
7d9dbca3 1306 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
2030a42c 1307 if (fd < 0)
66c0b394 1308 kvm_put_kvm(vcpu->kvm);
bccf2150 1309 return fd;
bccf2150
AK
1310}
1311
c5ea7660
AK
1312/*
1313 * Creates some virtual cpus. Good luck creating more than one.
1314 */
1315static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1316{
1317 int r;
1318 struct kvm_vcpu *vcpu;
1319
c5ea7660 1320 if (!valid_vcpu(n))
fb3f0f51 1321 return -EINVAL;
c5ea7660 1322
e9b11c17 1323 vcpu = kvm_arch_vcpu_create(kvm, n);
fb3f0f51
RR
1324 if (IS_ERR(vcpu))
1325 return PTR_ERR(vcpu);
c5ea7660 1326
15ad7146
AK
1327 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1328
26e5215f
AK
1329 r = kvm_arch_vcpu_setup(vcpu);
1330 if (r)
7d8fece6 1331 return r;
26e5215f 1332
11ec2804 1333 mutex_lock(&kvm->lock);
fb3f0f51
RR
1334 if (kvm->vcpus[n]) {
1335 r = -EEXIST;
e9b11c17 1336 goto vcpu_destroy;
fb3f0f51
RR
1337 }
1338 kvm->vcpus[n] = vcpu;
11ec2804 1339 mutex_unlock(&kvm->lock);
c5ea7660 1340
fb3f0f51 1341 /* Now it's all set up, let userspace reach it */
66c0b394 1342 kvm_get_kvm(kvm);
bccf2150
AK
1343 r = create_vcpu_fd(vcpu);
1344 if (r < 0)
fb3f0f51
RR
1345 goto unlink;
1346 return r;
39c3b86e 1347
fb3f0f51 1348unlink:
11ec2804 1349 mutex_lock(&kvm->lock);
fb3f0f51 1350 kvm->vcpus[n] = NULL;
e9b11c17 1351vcpu_destroy:
7d8fece6 1352 mutex_unlock(&kvm->lock);
d40ccc62 1353 kvm_arch_vcpu_destroy(vcpu);
c5ea7660
AK
1354 return r;
1355}
1356
1961d276
AK
1357static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1358{
1359 if (sigset) {
1360 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1361 vcpu->sigset_active = 1;
1362 vcpu->sigset = *sigset;
1363 } else
1364 vcpu->sigset_active = 0;
1365 return 0;
1366}
1367
bccf2150
AK
1368static long kvm_vcpu_ioctl(struct file *filp,
1369 unsigned int ioctl, unsigned long arg)
6aa8b732 1370{
bccf2150 1371 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 1372 void __user *argp = (void __user *)arg;
313a3dc7 1373 int r;
fa3795a7
DH
1374 struct kvm_fpu *fpu = NULL;
1375 struct kvm_sregs *kvm_sregs = NULL;
6aa8b732 1376
6d4e4c4f
AK
1377 if (vcpu->kvm->mm != current->mm)
1378 return -EIO;
6aa8b732 1379 switch (ioctl) {
9a2bb7f4 1380 case KVM_RUN:
f0fe5108
AK
1381 r = -EINVAL;
1382 if (arg)
1383 goto out;
b6c7a5dc 1384 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
6aa8b732 1385 break;
6aa8b732 1386 case KVM_GET_REGS: {
3e4bb3ac 1387 struct kvm_regs *kvm_regs;
6aa8b732 1388
3e4bb3ac
XZ
1389 r = -ENOMEM;
1390 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1391 if (!kvm_regs)
6aa8b732 1392 goto out;
3e4bb3ac
XZ
1393 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1394 if (r)
1395 goto out_free1;
6aa8b732 1396 r = -EFAULT;
3e4bb3ac
XZ
1397 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1398 goto out_free1;
6aa8b732 1399 r = 0;
3e4bb3ac
XZ
1400out_free1:
1401 kfree(kvm_regs);
6aa8b732
AK
1402 break;
1403 }
1404 case KVM_SET_REGS: {
3e4bb3ac 1405 struct kvm_regs *kvm_regs;
6aa8b732 1406
3e4bb3ac
XZ
1407 r = -ENOMEM;
1408 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1409 if (!kvm_regs)
6aa8b732 1410 goto out;
3e4bb3ac
XZ
1411 r = -EFAULT;
1412 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1413 goto out_free2;
1414 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
6aa8b732 1415 if (r)
3e4bb3ac 1416 goto out_free2;
6aa8b732 1417 r = 0;
3e4bb3ac
XZ
1418out_free2:
1419 kfree(kvm_regs);
6aa8b732
AK
1420 break;
1421 }
1422 case KVM_GET_SREGS: {
fa3795a7
DH
1423 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1424 r = -ENOMEM;
1425 if (!kvm_sregs)
1426 goto out;
1427 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
6aa8b732
AK
1428 if (r)
1429 goto out;
1430 r = -EFAULT;
fa3795a7 1431 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
6aa8b732
AK
1432 goto out;
1433 r = 0;
1434 break;
1435 }
1436 case KVM_SET_SREGS: {
fa3795a7
DH
1437 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1438 r = -ENOMEM;
1439 if (!kvm_sregs)
1440 goto out;
6aa8b732 1441 r = -EFAULT;
fa3795a7 1442 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
6aa8b732 1443 goto out;
fa3795a7 1444 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
6aa8b732
AK
1445 if (r)
1446 goto out;
1447 r = 0;
1448 break;
1449 }
62d9f0db
MT
1450 case KVM_GET_MP_STATE: {
1451 struct kvm_mp_state mp_state;
1452
1453 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1454 if (r)
1455 goto out;
1456 r = -EFAULT;
1457 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1458 goto out;
1459 r = 0;
1460 break;
1461 }
1462 case KVM_SET_MP_STATE: {
1463 struct kvm_mp_state mp_state;
1464
1465 r = -EFAULT;
1466 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1467 goto out;
1468 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1469 if (r)
1470 goto out;
1471 r = 0;
1472 break;
1473 }
6aa8b732
AK
1474 case KVM_TRANSLATE: {
1475 struct kvm_translation tr;
1476
1477 r = -EFAULT;
2f366987 1478 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 1479 goto out;
8b006791 1480 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
1481 if (r)
1482 goto out;
1483 r = -EFAULT;
2f366987 1484 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
1485 goto out;
1486 r = 0;
1487 break;
1488 }
6aa8b732
AK
1489 case KVM_DEBUG_GUEST: {
1490 struct kvm_debug_guest dbg;
1491
1492 r = -EFAULT;
2f366987 1493 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 1494 goto out;
b6c7a5dc 1495 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
6aa8b732
AK
1496 if (r)
1497 goto out;
1498 r = 0;
1499 break;
1500 }
1961d276
AK
1501 case KVM_SET_SIGNAL_MASK: {
1502 struct kvm_signal_mask __user *sigmask_arg = argp;
1503 struct kvm_signal_mask kvm_sigmask;
1504 sigset_t sigset, *p;
1505
1506 p = NULL;
1507 if (argp) {
1508 r = -EFAULT;
1509 if (copy_from_user(&kvm_sigmask, argp,
1510 sizeof kvm_sigmask))
1511 goto out;
1512 r = -EINVAL;
1513 if (kvm_sigmask.len != sizeof sigset)
1514 goto out;
1515 r = -EFAULT;
1516 if (copy_from_user(&sigset, sigmask_arg->sigset,
1517 sizeof sigset))
1518 goto out;
1519 p = &sigset;
1520 }
1521 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1522 break;
1523 }
b8836737 1524 case KVM_GET_FPU: {
fa3795a7
DH
1525 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1526 r = -ENOMEM;
1527 if (!fpu)
1528 goto out;
1529 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
b8836737
AK
1530 if (r)
1531 goto out;
1532 r = -EFAULT;
fa3795a7 1533 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
b8836737
AK
1534 goto out;
1535 r = 0;
1536 break;
1537 }
1538 case KVM_SET_FPU: {
fa3795a7
DH
1539 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1540 r = -ENOMEM;
1541 if (!fpu)
1542 goto out;
b8836737 1543 r = -EFAULT;
fa3795a7 1544 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
b8836737 1545 goto out;
fa3795a7 1546 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
b8836737
AK
1547 if (r)
1548 goto out;
1549 r = 0;
1550 break;
1551 }
bccf2150 1552 default:
313a3dc7 1553 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
bccf2150
AK
1554 }
1555out:
fa3795a7
DH
1556 kfree(fpu);
1557 kfree(kvm_sregs);
bccf2150
AK
1558 return r;
1559}
1560
1561static long kvm_vm_ioctl(struct file *filp,
1562 unsigned int ioctl, unsigned long arg)
1563{
1564 struct kvm *kvm = filp->private_data;
1565 void __user *argp = (void __user *)arg;
1fe779f8 1566 int r;
bccf2150 1567
6d4e4c4f
AK
1568 if (kvm->mm != current->mm)
1569 return -EIO;
bccf2150
AK
1570 switch (ioctl) {
1571 case KVM_CREATE_VCPU:
1572 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1573 if (r < 0)
1574 goto out;
1575 break;
6fc138d2
IE
1576 case KVM_SET_USER_MEMORY_REGION: {
1577 struct kvm_userspace_memory_region kvm_userspace_mem;
1578
1579 r = -EFAULT;
1580 if (copy_from_user(&kvm_userspace_mem, argp,
1581 sizeof kvm_userspace_mem))
1582 goto out;
1583
1584 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
6aa8b732
AK
1585 if (r)
1586 goto out;
1587 break;
1588 }
1589 case KVM_GET_DIRTY_LOG: {
1590 struct kvm_dirty_log log;
1591
1592 r = -EFAULT;
2f366987 1593 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 1594 goto out;
2c6f5df9 1595 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
1596 if (r)
1597 goto out;
1598 break;
1599 }
5f94c174
LV
1600#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1601 case KVM_REGISTER_COALESCED_MMIO: {
1602 struct kvm_coalesced_mmio_zone zone;
1603 r = -EFAULT;
1604 if (copy_from_user(&zone, argp, sizeof zone))
1605 goto out;
1606 r = -ENXIO;
1607 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1608 if (r)
1609 goto out;
1610 r = 0;
1611 break;
1612 }
1613 case KVM_UNREGISTER_COALESCED_MMIO: {
1614 struct kvm_coalesced_mmio_zone zone;
1615 r = -EFAULT;
1616 if (copy_from_user(&zone, argp, sizeof zone))
1617 goto out;
1618 r = -ENXIO;
1619 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1620 if (r)
1621 goto out;
1622 r = 0;
1623 break;
1624 }
8a98f664
XZ
1625#endif
1626#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1627 case KVM_ASSIGN_PCI_DEVICE: {
1628 struct kvm_assigned_pci_dev assigned_dev;
1629
1630 r = -EFAULT;
1631 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1632 goto out;
1633 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1634 if (r)
1635 goto out;
1636 break;
1637 }
1638 case KVM_ASSIGN_IRQ: {
1639 struct kvm_assigned_irq assigned_irq;
1640
1641 r = -EFAULT;
1642 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1643 goto out;
1644 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1645 if (r)
1646 goto out;
1647 break;
1648 }
5f94c174 1649#endif
f17abe9a 1650 default:
1fe779f8 1651 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
f17abe9a
AK
1652 }
1653out:
1654 return r;
1655}
1656
e4a533a4 1657static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
f17abe9a 1658{
777b3f49
MT
1659 struct page *page[1];
1660 unsigned long addr;
1661 int npages;
1662 gfn_t gfn = vmf->pgoff;
f17abe9a 1663 struct kvm *kvm = vma->vm_file->private_data;
f17abe9a 1664
777b3f49
MT
1665 addr = gfn_to_hva(kvm, gfn);
1666 if (kvm_is_error_hva(addr))
e4a533a4 1667 return VM_FAULT_SIGBUS;
777b3f49
MT
1668
1669 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1670 NULL);
1671 if (unlikely(npages != 1))
e4a533a4 1672 return VM_FAULT_SIGBUS;
777b3f49
MT
1673
1674 vmf->page = page[0];
e4a533a4 1675 return 0;
f17abe9a
AK
1676}
1677
1678static struct vm_operations_struct kvm_vm_vm_ops = {
e4a533a4 1679 .fault = kvm_vm_fault,
f17abe9a
AK
1680};
1681
1682static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1683{
1684 vma->vm_ops = &kvm_vm_vm_ops;
1685 return 0;
1686}
1687
5c502742 1688static const struct file_operations kvm_vm_fops = {
f17abe9a
AK
1689 .release = kvm_vm_release,
1690 .unlocked_ioctl = kvm_vm_ioctl,
1691 .compat_ioctl = kvm_vm_ioctl,
1692 .mmap = kvm_vm_mmap,
1693};
1694
1695static int kvm_dev_ioctl_create_vm(void)
1696{
2030a42c 1697 int fd;
f17abe9a
AK
1698 struct kvm *kvm;
1699
f17abe9a 1700 kvm = kvm_create_vm();
d6d28168
AK
1701 if (IS_ERR(kvm))
1702 return PTR_ERR(kvm);
7d9dbca3 1703 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
2030a42c 1704 if (fd < 0)
66c0b394 1705 kvm_put_kvm(kvm);
f17abe9a 1706
f17abe9a 1707 return fd;
f17abe9a
AK
1708}
1709
1710static long kvm_dev_ioctl(struct file *filp,
1711 unsigned int ioctl, unsigned long arg)
1712{
07c45a36 1713 long r = -EINVAL;
f17abe9a
AK
1714
1715 switch (ioctl) {
1716 case KVM_GET_API_VERSION:
f0fe5108
AK
1717 r = -EINVAL;
1718 if (arg)
1719 goto out;
f17abe9a
AK
1720 r = KVM_API_VERSION;
1721 break;
1722 case KVM_CREATE_VM:
f0fe5108
AK
1723 r = -EINVAL;
1724 if (arg)
1725 goto out;
f17abe9a
AK
1726 r = kvm_dev_ioctl_create_vm();
1727 break;
018d00d2 1728 case KVM_CHECK_EXTENSION:
1e1c65e0 1729 r = kvm_dev_ioctl_check_extension(arg);
5d308f45 1730 break;
07c45a36
AK
1731 case KVM_GET_VCPU_MMAP_SIZE:
1732 r = -EINVAL;
1733 if (arg)
1734 goto out;
adb1ff46
AK
1735 r = PAGE_SIZE; /* struct kvm_run */
1736#ifdef CONFIG_X86
1737 r += PAGE_SIZE; /* pio data page */
5f94c174
LV
1738#endif
1739#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1740 r += PAGE_SIZE; /* coalesced mmio ring page */
adb1ff46 1741#endif
07c45a36 1742 break;
d4c9ff2d
FEL
1743 case KVM_TRACE_ENABLE:
1744 case KVM_TRACE_PAUSE:
1745 case KVM_TRACE_DISABLE:
1746 r = kvm_trace_ioctl(ioctl, arg);
1747 break;
6aa8b732 1748 default:
043405e1 1749 return kvm_arch_dev_ioctl(filp, ioctl, arg);
6aa8b732
AK
1750 }
1751out:
1752 return r;
1753}
1754
6aa8b732 1755static struct file_operations kvm_chardev_ops = {
6aa8b732
AK
1756 .unlocked_ioctl = kvm_dev_ioctl,
1757 .compat_ioctl = kvm_dev_ioctl,
6aa8b732
AK
1758};
1759
1760static struct miscdevice kvm_dev = {
bbe4432e 1761 KVM_MINOR,
6aa8b732
AK
1762 "kvm",
1763 &kvm_chardev_ops,
1764};
1765
1b6c0168
AK
1766static void hardware_enable(void *junk)
1767{
1768 int cpu = raw_smp_processor_id();
1769
1770 if (cpu_isset(cpu, cpus_hardware_enabled))
1771 return;
1772 cpu_set(cpu, cpus_hardware_enabled);
e9b11c17 1773 kvm_arch_hardware_enable(NULL);
1b6c0168
AK
1774}
1775
1776static void hardware_disable(void *junk)
1777{
1778 int cpu = raw_smp_processor_id();
1779
1780 if (!cpu_isset(cpu, cpus_hardware_enabled))
1781 return;
1782 cpu_clear(cpu, cpus_hardware_enabled);
e9b11c17 1783 kvm_arch_hardware_disable(NULL);
1b6c0168
AK
1784}
1785
774c47f1
AK
1786static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1787 void *v)
1788{
1789 int cpu = (long)v;
1790
1a6f4d7f 1791 val &= ~CPU_TASKS_FROZEN;
774c47f1 1792 switch (val) {
cec9ad27 1793 case CPU_DYING:
6ec8a856
AK
1794 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1795 cpu);
1796 hardware_disable(NULL);
1797 break;
774c47f1 1798 case CPU_UP_CANCELED:
43934a38
JK
1799 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1800 cpu);
8691e5a8 1801 smp_call_function_single(cpu, hardware_disable, NULL, 1);
774c47f1 1802 break;
43934a38
JK
1803 case CPU_ONLINE:
1804 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1805 cpu);
8691e5a8 1806 smp_call_function_single(cpu, hardware_enable, NULL, 1);
774c47f1
AK
1807 break;
1808 }
1809 return NOTIFY_OK;
1810}
1811
4ecac3fd
AK
1812
1813asmlinkage void kvm_handle_fault_on_reboot(void)
1814{
1815 if (kvm_rebooting)
1816 /* spin while reset goes on */
1817 while (true)
1818 ;
1819 /* Fault while not rebooting. We want the trace. */
1820 BUG();
1821}
1822EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1823
9a2b85c6 1824static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
d77c26fc 1825 void *v)
9a2b85c6
RR
1826{
1827 if (val == SYS_RESTART) {
1828 /*
1829 * Some (well, at least mine) BIOSes hang on reboot if
1830 * in vmx root mode.
1831 */
1832 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
4ecac3fd 1833 kvm_rebooting = true;
15c8b6c1 1834 on_each_cpu(hardware_disable, NULL, 1);
9a2b85c6
RR
1835 }
1836 return NOTIFY_OK;
1837}
1838
1839static struct notifier_block kvm_reboot_notifier = {
1840 .notifier_call = kvm_reboot,
1841 .priority = 0,
1842};
1843
2eeb2e94
GH
1844void kvm_io_bus_init(struct kvm_io_bus *bus)
1845{
1846 memset(bus, 0, sizeof(*bus));
1847}
1848
1849void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1850{
1851 int i;
1852
1853 for (i = 0; i < bus->dev_count; i++) {
1854 struct kvm_io_device *pos = bus->devs[i];
1855
1856 kvm_iodevice_destructor(pos);
1857 }
1858}
1859
92760499
LV
1860struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1861 gpa_t addr, int len, int is_write)
2eeb2e94
GH
1862{
1863 int i;
1864
1865 for (i = 0; i < bus->dev_count; i++) {
1866 struct kvm_io_device *pos = bus->devs[i];
1867
92760499 1868 if (pos->in_range(pos, addr, len, is_write))
2eeb2e94
GH
1869 return pos;
1870 }
1871
1872 return NULL;
1873}
1874
1875void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1876{
1877 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1878
1879 bus->devs[bus->dev_count++] = dev;
1880}
1881
774c47f1
AK
1882static struct notifier_block kvm_cpu_notifier = {
1883 .notifier_call = kvm_cpu_hotplug,
1884 .priority = 20, /* must be > scheduler priority */
1885};
1886
8b88b099 1887static int vm_stat_get(void *_offset, u64 *val)
ba1389b7
AK
1888{
1889 unsigned offset = (long)_offset;
ba1389b7
AK
1890 struct kvm *kvm;
1891
8b88b099 1892 *val = 0;
ba1389b7
AK
1893 spin_lock(&kvm_lock);
1894 list_for_each_entry(kvm, &vm_list, vm_list)
8b88b099 1895 *val += *(u32 *)((void *)kvm + offset);
ba1389b7 1896 spin_unlock(&kvm_lock);
8b88b099 1897 return 0;
ba1389b7
AK
1898}
1899
1900DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1901
8b88b099 1902static int vcpu_stat_get(void *_offset, u64 *val)
1165f5fe
AK
1903{
1904 unsigned offset = (long)_offset;
1165f5fe
AK
1905 struct kvm *kvm;
1906 struct kvm_vcpu *vcpu;
1907 int i;
1908
8b88b099 1909 *val = 0;
1165f5fe
AK
1910 spin_lock(&kvm_lock);
1911 list_for_each_entry(kvm, &vm_list, vm_list)
1912 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
fb3f0f51
RR
1913 vcpu = kvm->vcpus[i];
1914 if (vcpu)
8b88b099 1915 *val += *(u32 *)((void *)vcpu + offset);
1165f5fe
AK
1916 }
1917 spin_unlock(&kvm_lock);
8b88b099 1918 return 0;
1165f5fe
AK
1919}
1920
ba1389b7
AK
1921DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1922
1923static struct file_operations *stat_fops[] = {
1924 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1925 [KVM_STAT_VM] = &vm_stat_fops,
1926};
1165f5fe 1927
a16b043c 1928static void kvm_init_debug(void)
6aa8b732
AK
1929{
1930 struct kvm_stats_debugfs_item *p;
1931
76f7c879 1932 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732 1933 for (p = debugfs_entries; p->name; ++p)
76f7c879 1934 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1165f5fe 1935 (void *)(long)p->offset,
ba1389b7 1936 stat_fops[p->kind]);
6aa8b732
AK
1937}
1938
1939static void kvm_exit_debug(void)
1940{
1941 struct kvm_stats_debugfs_item *p;
1942
1943 for (p = debugfs_entries; p->name; ++p)
1944 debugfs_remove(p->dentry);
76f7c879 1945 debugfs_remove(kvm_debugfs_dir);
6aa8b732
AK
1946}
1947
59ae6c6b
AK
1948static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1949{
4267c41a 1950 hardware_disable(NULL);
59ae6c6b
AK
1951 return 0;
1952}
1953
1954static int kvm_resume(struct sys_device *dev)
1955{
4267c41a 1956 hardware_enable(NULL);
59ae6c6b
AK
1957 return 0;
1958}
1959
1960static struct sysdev_class kvm_sysdev_class = {
af5ca3f4 1961 .name = "kvm",
59ae6c6b
AK
1962 .suspend = kvm_suspend,
1963 .resume = kvm_resume,
1964};
1965
1966static struct sys_device kvm_sysdev = {
1967 .id = 0,
1968 .cls = &kvm_sysdev_class,
1969};
1970
cea7bb21 1971struct page *bad_page;
35149e21 1972pfn_t bad_pfn;
6aa8b732 1973
15ad7146
AK
1974static inline
1975struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1976{
1977 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1978}
1979
1980static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1981{
1982 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1983
e9b11c17 1984 kvm_arch_vcpu_load(vcpu, cpu);
15ad7146
AK
1985}
1986
1987static void kvm_sched_out(struct preempt_notifier *pn,
1988 struct task_struct *next)
1989{
1990 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1991
e9b11c17 1992 kvm_arch_vcpu_put(vcpu);
15ad7146
AK
1993}
1994
f8c16bba 1995int kvm_init(void *opaque, unsigned int vcpu_size,
c16f862d 1996 struct module *module)
6aa8b732
AK
1997{
1998 int r;
002c7f7c 1999 int cpu;
6aa8b732 2000
cb498ea2
ZX
2001 kvm_init_debug();
2002
f8c16bba
ZX
2003 r = kvm_arch_init(opaque);
2004 if (r)
d2308784 2005 goto out_fail;
cb498ea2
ZX
2006
2007 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2008
2009 if (bad_page == NULL) {
2010 r = -ENOMEM;
2011 goto out;
2012 }
2013
35149e21
AL
2014 bad_pfn = page_to_pfn(bad_page);
2015
e9b11c17 2016 r = kvm_arch_hardware_setup();
6aa8b732 2017 if (r < 0)
d2308784 2018 goto out_free_0;
6aa8b732 2019
002c7f7c
YS
2020 for_each_online_cpu(cpu) {
2021 smp_call_function_single(cpu,
e9b11c17 2022 kvm_arch_check_processor_compat,
8691e5a8 2023 &r, 1);
002c7f7c 2024 if (r < 0)
d2308784 2025 goto out_free_1;
002c7f7c
YS
2026 }
2027
15c8b6c1 2028 on_each_cpu(hardware_enable, NULL, 1);
774c47f1
AK
2029 r = register_cpu_notifier(&kvm_cpu_notifier);
2030 if (r)
d2308784 2031 goto out_free_2;
6aa8b732
AK
2032 register_reboot_notifier(&kvm_reboot_notifier);
2033
59ae6c6b
AK
2034 r = sysdev_class_register(&kvm_sysdev_class);
2035 if (r)
d2308784 2036 goto out_free_3;
59ae6c6b
AK
2037
2038 r = sysdev_register(&kvm_sysdev);
2039 if (r)
d2308784 2040 goto out_free_4;
59ae6c6b 2041
c16f862d
RR
2042 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2043 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
56919c5c
JP
2044 __alignof__(struct kvm_vcpu),
2045 0, NULL);
c16f862d
RR
2046 if (!kvm_vcpu_cache) {
2047 r = -ENOMEM;
d2308784 2048 goto out_free_5;
c16f862d
RR
2049 }
2050
6aa8b732
AK
2051 kvm_chardev_ops.owner = module;
2052
2053 r = misc_register(&kvm_dev);
2054 if (r) {
d77c26fc 2055 printk(KERN_ERR "kvm: misc device register failed\n");
6aa8b732
AK
2056 goto out_free;
2057 }
2058
15ad7146
AK
2059 kvm_preempt_ops.sched_in = kvm_sched_in;
2060 kvm_preempt_ops.sched_out = kvm_sched_out;
2061
c7addb90 2062 return 0;
6aa8b732
AK
2063
2064out_free:
c16f862d 2065 kmem_cache_destroy(kvm_vcpu_cache);
d2308784 2066out_free_5:
59ae6c6b 2067 sysdev_unregister(&kvm_sysdev);
d2308784 2068out_free_4:
59ae6c6b 2069 sysdev_class_unregister(&kvm_sysdev_class);
d2308784 2070out_free_3:
6aa8b732 2071 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1 2072 unregister_cpu_notifier(&kvm_cpu_notifier);
d2308784 2073out_free_2:
15c8b6c1 2074 on_each_cpu(hardware_disable, NULL, 1);
d2308784 2075out_free_1:
e9b11c17 2076 kvm_arch_hardware_unsetup();
d2308784
ZX
2077out_free_0:
2078 __free_page(bad_page);
ca45aaae 2079out:
f8c16bba 2080 kvm_arch_exit();
cb498ea2 2081 kvm_exit_debug();
d2308784 2082out_fail:
6aa8b732
AK
2083 return r;
2084}
cb498ea2 2085EXPORT_SYMBOL_GPL(kvm_init);
6aa8b732 2086
cb498ea2 2087void kvm_exit(void)
6aa8b732 2088{
d4c9ff2d 2089 kvm_trace_cleanup();
6aa8b732 2090 misc_deregister(&kvm_dev);
c16f862d 2091 kmem_cache_destroy(kvm_vcpu_cache);
59ae6c6b
AK
2092 sysdev_unregister(&kvm_sysdev);
2093 sysdev_class_unregister(&kvm_sysdev_class);
6aa8b732 2094 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 2095 unregister_cpu_notifier(&kvm_cpu_notifier);
15c8b6c1 2096 on_each_cpu(hardware_disable, NULL, 1);
e9b11c17 2097 kvm_arch_hardware_unsetup();
f8c16bba 2098 kvm_arch_exit();
6aa8b732 2099 kvm_exit_debug();
cea7bb21 2100 __free_page(bad_page);
6aa8b732 2101}
cb498ea2 2102EXPORT_SYMBOL_GPL(kvm_exit);