Merge commit 'v2.6.30.8' into mini2440-stable-v2.6.30
[linux-2.6/mini2440.git] / virt / kvm / kvm_main.c
blobad381351e26591363c94a7009d5ce9d54095681e
1 /*
2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
18 #include "iodev.h"
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/percpu.h>
25 #include <linux/gfp.h>
26 #include <linux/mm.h>
27 #include <linux/miscdevice.h>
28 #include <linux/vmalloc.h>
29 #include <linux/reboot.h>
30 #include <linux/debugfs.h>
31 #include <linux/highmem.h>
32 #include <linux/file.h>
33 #include <linux/sysdev.h>
34 #include <linux/cpu.h>
35 #include <linux/sched.h>
36 #include <linux/cpumask.h>
37 #include <linux/smp.h>
38 #include <linux/anon_inodes.h>
39 #include <linux/profile.h>
40 #include <linux/kvm_para.h>
41 #include <linux/pagemap.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
45 #include <asm/processor.h>
46 #include <asm/io.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
50 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51 #include "coalesced_mmio.h"
52 #endif
54 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
55 #include <linux/pci.h>
56 #include <linux/interrupt.h>
57 #include "irq.h"
58 #endif
60 MODULE_AUTHOR("Qumranet");
61 MODULE_LICENSE("GPL");
63 static int msi2intx = 1;
64 module_param(msi2intx, bool, 0);
66 DEFINE_SPINLOCK(kvm_lock);
67 LIST_HEAD(vm_list);
69 static cpumask_var_t cpus_hardware_enabled;
71 struct kmem_cache *kvm_vcpu_cache;
72 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
74 static __read_mostly struct preempt_ops kvm_preempt_ops;
76 struct dentry *kvm_debugfs_dir;
78 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
79 unsigned long arg);
81 static bool kvm_rebooting;
83 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
84 static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
85 int assigned_dev_id)
87 struct list_head *ptr;
88 struct kvm_assigned_dev_kernel *match;
90 list_for_each(ptr, head) {
91 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
92 if (match->assigned_dev_id == assigned_dev_id)
93 return match;
95 return NULL;
98 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
100 struct kvm_assigned_dev_kernel *assigned_dev;
102 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
103 interrupt_work);
105 /* This is taken to safely inject irq inside the guest. When
106 * the interrupt injection (or the ioapic code) uses a
107 * finer-grained lock, update this
109 mutex_lock(&assigned_dev->kvm->lock);
110 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
111 assigned_dev->guest_irq, 1);
113 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_MSI) {
114 enable_irq(assigned_dev->host_irq);
115 assigned_dev->host_irq_disabled = false;
117 mutex_unlock(&assigned_dev->kvm->lock);
120 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
122 struct kvm_assigned_dev_kernel *assigned_dev =
123 (struct kvm_assigned_dev_kernel *) dev_id;
125 schedule_work(&assigned_dev->interrupt_work);
127 disable_irq_nosync(irq);
128 assigned_dev->host_irq_disabled = true;
130 return IRQ_HANDLED;
133 /* Ack the irq line for an assigned device */
134 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
136 struct kvm_assigned_dev_kernel *dev;
138 if (kian->gsi == -1)
139 return;
141 dev = container_of(kian, struct kvm_assigned_dev_kernel,
142 ack_notifier);
144 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
146 /* The guest irq may be shared so this ack may be
147 * from another device.
149 if (dev->host_irq_disabled) {
150 enable_irq(dev->host_irq);
151 dev->host_irq_disabled = false;
155 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
156 static void kvm_free_assigned_irq(struct kvm *kvm,
157 struct kvm_assigned_dev_kernel *assigned_dev)
159 if (!irqchip_in_kernel(kvm))
160 return;
162 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
164 if (assigned_dev->irq_source_id != -1)
165 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
166 assigned_dev->irq_source_id = -1;
168 if (!assigned_dev->irq_requested_type)
169 return;
172 * In kvm_free_device_irq, cancel_work_sync return true if:
173 * 1. work is scheduled, and then cancelled.
174 * 2. work callback is executed.
176 * The first one ensured that the irq is disabled and no more events
177 * would happen. But for the second one, the irq may be enabled (e.g.
178 * for MSI). So we disable irq here to prevent further events.
180 * Notice this maybe result in nested disable if the interrupt type is
181 * INTx, but it's OK for we are going to free it.
183 * If this function is a part of VM destroy, please ensure that till
184 * now, the kvm state is still legal for probably we also have to wait
185 * interrupt_work done.
187 disable_irq_nosync(assigned_dev->host_irq);
188 cancel_work_sync(&assigned_dev->interrupt_work);
190 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
192 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
193 pci_disable_msi(assigned_dev->dev);
195 assigned_dev->irq_requested_type = 0;
199 static void kvm_free_assigned_device(struct kvm *kvm,
200 struct kvm_assigned_dev_kernel
201 *assigned_dev)
203 kvm_free_assigned_irq(kvm, assigned_dev);
205 pci_reset_function(assigned_dev->dev);
207 pci_release_regions(assigned_dev->dev);
208 pci_disable_device(assigned_dev->dev);
209 pci_dev_put(assigned_dev->dev);
211 list_del(&assigned_dev->list);
212 kfree(assigned_dev);
215 void kvm_free_all_assigned_devices(struct kvm *kvm)
217 struct list_head *ptr, *ptr2;
218 struct kvm_assigned_dev_kernel *assigned_dev;
220 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
221 assigned_dev = list_entry(ptr,
222 struct kvm_assigned_dev_kernel,
223 list);
225 kvm_free_assigned_device(kvm, assigned_dev);
229 static int assigned_device_update_intx(struct kvm *kvm,
230 struct kvm_assigned_dev_kernel *adev,
231 struct kvm_assigned_irq *airq)
233 adev->guest_irq = airq->guest_irq;
234 adev->ack_notifier.gsi = airq->guest_irq;
236 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
237 return 0;
239 if (irqchip_in_kernel(kvm)) {
240 if (!msi2intx &&
241 (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
242 free_irq(adev->host_irq, (void *)adev);
243 pci_disable_msi(adev->dev);
246 if (!capable(CAP_SYS_RAWIO))
247 return -EPERM;
249 if (airq->host_irq)
250 adev->host_irq = airq->host_irq;
251 else
252 adev->host_irq = adev->dev->irq;
254 /* Even though this is PCI, we don't want to use shared
255 * interrupts. Sharing host devices with guest-assigned devices
256 * on the same interrupt line is not a happy situation: there
257 * are going to be long delays in accepting, acking, etc.
259 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
260 0, "kvm_assigned_intx_device", (void *)adev))
261 return -EIO;
264 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
265 KVM_ASSIGNED_DEV_HOST_INTX;
266 return 0;
269 #ifdef CONFIG_X86
270 static int assigned_device_update_msi(struct kvm *kvm,
271 struct kvm_assigned_dev_kernel *adev,
272 struct kvm_assigned_irq *airq)
274 int r;
276 adev->guest_irq = airq->guest_irq;
277 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
278 /* x86 don't care upper address of guest msi message addr */
279 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
280 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
281 adev->ack_notifier.gsi = -1;
282 } else if (msi2intx) {
283 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
284 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
285 adev->ack_notifier.gsi = airq->guest_irq;
286 } else {
288 * Guest require to disable device MSI, we disable MSI and
289 * re-enable INTx by default again. Notice it's only for
290 * non-msi2intx.
292 assigned_device_update_intx(kvm, adev, airq);
293 return 0;
296 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
297 return 0;
299 if (irqchip_in_kernel(kvm)) {
300 if (!msi2intx) {
301 if (adev->irq_requested_type &
302 KVM_ASSIGNED_DEV_HOST_INTX)
303 free_irq(adev->host_irq, (void *)adev);
305 r = pci_enable_msi(adev->dev);
306 if (r)
307 return r;
310 adev->host_irq = adev->dev->irq;
311 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
312 "kvm_assigned_msi_device", (void *)adev))
313 return -EIO;
316 if (!msi2intx)
317 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
319 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
320 return 0;
322 #endif
324 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
325 struct kvm_assigned_irq
326 *assigned_irq)
328 int r = 0;
329 struct kvm_assigned_dev_kernel *match;
330 u32 current_flags = 0, changed_flags;
332 mutex_lock(&kvm->lock);
334 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
335 assigned_irq->assigned_dev_id);
336 if (!match) {
337 mutex_unlock(&kvm->lock);
338 return -EINVAL;
341 if (!match->irq_requested_type) {
342 INIT_WORK(&match->interrupt_work,
343 kvm_assigned_dev_interrupt_work_handler);
344 if (irqchip_in_kernel(kvm)) {
345 /* Register ack nofitier */
346 match->ack_notifier.gsi = -1;
347 match->ack_notifier.irq_acked =
348 kvm_assigned_dev_ack_irq;
349 kvm_register_irq_ack_notifier(kvm,
350 &match->ack_notifier);
352 /* Request IRQ source ID */
353 r = kvm_request_irq_source_id(kvm);
354 if (r < 0)
355 goto out_release;
356 else
357 match->irq_source_id = r;
359 #ifdef CONFIG_X86
360 /* Determine host device irq type, we can know the
361 * result from dev->msi_enabled */
362 if (msi2intx)
363 pci_enable_msi(match->dev);
364 #endif
368 if ((match->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) &&
369 (match->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_MSI))
370 current_flags |= KVM_DEV_IRQ_ASSIGN_ENABLE_MSI;
372 changed_flags = assigned_irq->flags ^ current_flags;
374 if ((changed_flags & KVM_DEV_IRQ_ASSIGN_MSI_ACTION) ||
375 (msi2intx && match->dev->msi_enabled)) {
376 #ifdef CONFIG_X86
377 r = assigned_device_update_msi(kvm, match, assigned_irq);
378 if (r) {
379 printk(KERN_WARNING "kvm: failed to enable "
380 "MSI device!\n");
381 goto out_release;
383 #else
384 r = -ENOTTY;
385 #endif
386 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
387 /* Host device IRQ 0 means don't support INTx */
388 if (!msi2intx) {
389 printk(KERN_WARNING
390 "kvm: wait device to enable MSI!\n");
391 r = 0;
392 } else {
393 printk(KERN_WARNING
394 "kvm: failed to enable MSI device!\n");
395 r = -ENOTTY;
396 goto out_release;
398 } else {
399 /* Non-sharing INTx mode */
400 r = assigned_device_update_intx(kvm, match, assigned_irq);
401 if (r) {
402 printk(KERN_WARNING "kvm: failed to enable "
403 "INTx device!\n");
404 goto out_release;
408 mutex_unlock(&kvm->lock);
409 return r;
410 out_release:
411 mutex_unlock(&kvm->lock);
412 kvm_free_assigned_device(kvm, match);
413 return r;
416 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
417 struct kvm_assigned_pci_dev *assigned_dev)
419 int r = 0;
420 struct kvm_assigned_dev_kernel *match;
421 struct pci_dev *dev;
423 down_read(&kvm->slots_lock);
424 mutex_lock(&kvm->lock);
426 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
427 assigned_dev->assigned_dev_id);
428 if (match) {
429 /* device already assigned */
430 r = -EINVAL;
431 goto out;
434 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
435 if (match == NULL) {
436 printk(KERN_INFO "%s: Couldn't allocate memory\n",
437 __func__);
438 r = -ENOMEM;
439 goto out;
441 dev = pci_get_bus_and_slot(assigned_dev->busnr,
442 assigned_dev->devfn);
443 if (!dev) {
444 printk(KERN_INFO "%s: host device not found\n", __func__);
445 r = -EINVAL;
446 goto out_free;
448 if (pci_enable_device(dev)) {
449 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
450 r = -EBUSY;
451 goto out_put;
453 r = pci_request_regions(dev, "kvm_assigned_device");
454 if (r) {
455 printk(KERN_INFO "%s: Could not get access to device regions\n",
456 __func__);
457 goto out_disable;
460 pci_reset_function(dev);
462 match->assigned_dev_id = assigned_dev->assigned_dev_id;
463 match->host_busnr = assigned_dev->busnr;
464 match->host_devfn = assigned_dev->devfn;
465 match->flags = assigned_dev->flags;
466 match->dev = dev;
467 match->irq_source_id = -1;
468 match->kvm = kvm;
470 list_add(&match->list, &kvm->arch.assigned_dev_head);
472 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
473 if (!kvm->arch.iommu_domain) {
474 r = kvm_iommu_map_guest(kvm);
475 if (r)
476 goto out_list_del;
478 r = kvm_assign_device(kvm, match);
479 if (r)
480 goto out_list_del;
483 out:
484 mutex_unlock(&kvm->lock);
485 up_read(&kvm->slots_lock);
486 return r;
487 out_list_del:
488 list_del(&match->list);
489 pci_release_regions(dev);
490 out_disable:
491 pci_disable_device(dev);
492 out_put:
493 pci_dev_put(dev);
494 out_free:
495 kfree(match);
496 mutex_unlock(&kvm->lock);
497 up_read(&kvm->slots_lock);
498 return r;
500 #endif
502 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
503 static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
504 struct kvm_assigned_pci_dev *assigned_dev)
506 int r = 0;
507 struct kvm_assigned_dev_kernel *match;
509 mutex_lock(&kvm->lock);
511 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
512 assigned_dev->assigned_dev_id);
513 if (!match) {
514 printk(KERN_INFO "%s: device hasn't been assigned before, "
515 "so cannot be deassigned\n", __func__);
516 r = -EINVAL;
517 goto out;
520 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
521 kvm_deassign_device(kvm, match);
523 kvm_free_assigned_device(kvm, match);
525 out:
526 mutex_unlock(&kvm->lock);
527 return r;
529 #endif
531 static inline int valid_vcpu(int n)
533 return likely(n >= 0 && n < KVM_MAX_VCPUS);
536 inline int kvm_is_mmio_pfn(pfn_t pfn)
538 if (pfn_valid(pfn)) {
539 struct page *page = compound_head(pfn_to_page(pfn));
540 return PageReserved(page);
543 return true;
547 * Switches to specified vcpu, until a matching vcpu_put()
549 void vcpu_load(struct kvm_vcpu *vcpu)
551 int cpu;
553 mutex_lock(&vcpu->mutex);
554 cpu = get_cpu();
555 preempt_notifier_register(&vcpu->preempt_notifier);
556 kvm_arch_vcpu_load(vcpu, cpu);
557 put_cpu();
560 void vcpu_put(struct kvm_vcpu *vcpu)
562 preempt_disable();
563 kvm_arch_vcpu_put(vcpu);
564 preempt_notifier_unregister(&vcpu->preempt_notifier);
565 preempt_enable();
566 mutex_unlock(&vcpu->mutex);
569 static void ack_flush(void *_completed)
573 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
575 int i, cpu, me;
576 cpumask_var_t cpus;
577 bool called = true;
578 struct kvm_vcpu *vcpu;
580 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
581 cpumask_clear(cpus);
583 me = get_cpu();
584 spin_lock(&kvm->requests_lock);
585 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
586 vcpu = kvm->vcpus[i];
587 if (!vcpu)
588 continue;
589 if (test_and_set_bit(req, &vcpu->requests))
590 continue;
591 cpu = vcpu->cpu;
592 if (cpus != NULL && cpu != -1 && cpu != me)
593 cpumask_set_cpu(cpu, cpus);
595 if (unlikely(cpus == NULL))
596 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
597 else if (!cpumask_empty(cpus))
598 smp_call_function_many(cpus, ack_flush, NULL, 1);
599 else
600 called = false;
601 spin_unlock(&kvm->requests_lock);
602 put_cpu();
603 free_cpumask_var(cpus);
604 return called;
607 void kvm_flush_remote_tlbs(struct kvm *kvm)
609 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
610 ++kvm->stat.remote_tlb_flush;
613 void kvm_reload_remote_mmus(struct kvm *kvm)
615 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
618 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
620 struct page *page;
621 int r;
623 mutex_init(&vcpu->mutex);
624 vcpu->cpu = -1;
625 vcpu->kvm = kvm;
626 vcpu->vcpu_id = id;
627 init_waitqueue_head(&vcpu->wq);
629 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
630 if (!page) {
631 r = -ENOMEM;
632 goto fail;
634 vcpu->run = page_address(page);
636 r = kvm_arch_vcpu_init(vcpu);
637 if (r < 0)
638 goto fail_free_run;
639 return 0;
641 fail_free_run:
642 free_page((unsigned long)vcpu->run);
643 fail:
644 return r;
646 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
648 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
650 kvm_arch_vcpu_uninit(vcpu);
651 free_page((unsigned long)vcpu->run);
653 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
655 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
656 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
658 return container_of(mn, struct kvm, mmu_notifier);
661 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
662 struct mm_struct *mm,
663 unsigned long address)
665 struct kvm *kvm = mmu_notifier_to_kvm(mn);
666 int need_tlb_flush;
669 * When ->invalidate_page runs, the linux pte has been zapped
670 * already but the page is still allocated until
671 * ->invalidate_page returns. So if we increase the sequence
672 * here the kvm page fault will notice if the spte can't be
673 * established because the page is going to be freed. If
674 * instead the kvm page fault establishes the spte before
675 * ->invalidate_page runs, kvm_unmap_hva will release it
676 * before returning.
678 * The sequence increase only need to be seen at spin_unlock
679 * time, and not at spin_lock time.
681 * Increasing the sequence after the spin_unlock would be
682 * unsafe because the kvm page fault could then establish the
683 * pte after kvm_unmap_hva returned, without noticing the page
684 * is going to be freed.
686 spin_lock(&kvm->mmu_lock);
687 kvm->mmu_notifier_seq++;
688 need_tlb_flush = kvm_unmap_hva(kvm, address);
689 spin_unlock(&kvm->mmu_lock);
691 /* we've to flush the tlb before the pages can be freed */
692 if (need_tlb_flush)
693 kvm_flush_remote_tlbs(kvm);
697 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
698 struct mm_struct *mm,
699 unsigned long start,
700 unsigned long end)
702 struct kvm *kvm = mmu_notifier_to_kvm(mn);
703 int need_tlb_flush = 0;
705 spin_lock(&kvm->mmu_lock);
707 * The count increase must become visible at unlock time as no
708 * spte can be established without taking the mmu_lock and
709 * count is also read inside the mmu_lock critical section.
711 kvm->mmu_notifier_count++;
712 for (; start < end; start += PAGE_SIZE)
713 need_tlb_flush |= kvm_unmap_hva(kvm, start);
714 spin_unlock(&kvm->mmu_lock);
716 /* we've to flush the tlb before the pages can be freed */
717 if (need_tlb_flush)
718 kvm_flush_remote_tlbs(kvm);
721 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
722 struct mm_struct *mm,
723 unsigned long start,
724 unsigned long end)
726 struct kvm *kvm = mmu_notifier_to_kvm(mn);
728 spin_lock(&kvm->mmu_lock);
730 * This sequence increase will notify the kvm page fault that
731 * the page that is going to be mapped in the spte could have
732 * been freed.
734 kvm->mmu_notifier_seq++;
736 * The above sequence increase must be visible before the
737 * below count decrease but both values are read by the kvm
738 * page fault under mmu_lock spinlock so we don't need to add
739 * a smb_wmb() here in between the two.
741 kvm->mmu_notifier_count--;
742 spin_unlock(&kvm->mmu_lock);
744 BUG_ON(kvm->mmu_notifier_count < 0);
747 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
748 struct mm_struct *mm,
749 unsigned long address)
751 struct kvm *kvm = mmu_notifier_to_kvm(mn);
752 int young;
754 spin_lock(&kvm->mmu_lock);
755 young = kvm_age_hva(kvm, address);
756 spin_unlock(&kvm->mmu_lock);
758 if (young)
759 kvm_flush_remote_tlbs(kvm);
761 return young;
764 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
765 struct mm_struct *mm)
767 struct kvm *kvm = mmu_notifier_to_kvm(mn);
768 kvm_arch_flush_shadow(kvm);
771 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
772 .invalidate_page = kvm_mmu_notifier_invalidate_page,
773 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
774 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
775 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
776 .release = kvm_mmu_notifier_release,
778 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
780 static struct kvm *kvm_create_vm(void)
782 struct kvm *kvm = kvm_arch_create_vm();
783 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
784 struct page *page;
785 #endif
787 if (IS_ERR(kvm))
788 goto out;
789 #ifdef CONFIG_HAVE_KVM_IRQCHIP
790 INIT_LIST_HEAD(&kvm->irq_routing);
791 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
792 #endif
794 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
795 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
796 if (!page) {
797 kfree(kvm);
798 return ERR_PTR(-ENOMEM);
800 kvm->coalesced_mmio_ring =
801 (struct kvm_coalesced_mmio_ring *)page_address(page);
802 #endif
804 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
806 int err;
807 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
808 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
809 if (err) {
810 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
811 put_page(page);
812 #endif
813 kfree(kvm);
814 return ERR_PTR(err);
817 #endif
819 kvm->mm = current->mm;
820 atomic_inc(&kvm->mm->mm_count);
821 spin_lock_init(&kvm->mmu_lock);
822 spin_lock_init(&kvm->requests_lock);
823 kvm_io_bus_init(&kvm->pio_bus);
824 mutex_init(&kvm->lock);
825 kvm_io_bus_init(&kvm->mmio_bus);
826 init_rwsem(&kvm->slots_lock);
827 atomic_set(&kvm->users_count, 1);
828 spin_lock(&kvm_lock);
829 list_add(&kvm->vm_list, &vm_list);
830 spin_unlock(&kvm_lock);
831 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
832 kvm_coalesced_mmio_init(kvm);
833 #endif
834 out:
835 return kvm;
839 * Free any memory in @free but not in @dont.
841 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
842 struct kvm_memory_slot *dont)
844 if (!dont || free->rmap != dont->rmap)
845 vfree(free->rmap);
847 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
848 vfree(free->dirty_bitmap);
850 if (!dont || free->lpage_info != dont->lpage_info)
851 vfree(free->lpage_info);
853 free->npages = 0;
854 free->dirty_bitmap = NULL;
855 free->rmap = NULL;
856 free->lpage_info = NULL;
859 void kvm_free_physmem(struct kvm *kvm)
861 int i;
863 for (i = 0; i < kvm->nmemslots; ++i)
864 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
867 static void kvm_destroy_vm(struct kvm *kvm)
869 struct mm_struct *mm = kvm->mm;
871 kvm_arch_sync_events(kvm);
872 spin_lock(&kvm_lock);
873 list_del(&kvm->vm_list);
874 spin_unlock(&kvm_lock);
875 kvm_free_irq_routing(kvm);
876 kvm_io_bus_destroy(&kvm->pio_bus);
877 kvm_io_bus_destroy(&kvm->mmio_bus);
878 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
879 if (kvm->coalesced_mmio_ring != NULL)
880 free_page((unsigned long)kvm->coalesced_mmio_ring);
881 #endif
882 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
883 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
884 #else
885 kvm_arch_flush_shadow(kvm);
886 #endif
887 kvm_arch_destroy_vm(kvm);
888 mmdrop(mm);
891 void kvm_get_kvm(struct kvm *kvm)
893 atomic_inc(&kvm->users_count);
895 EXPORT_SYMBOL_GPL(kvm_get_kvm);
897 void kvm_put_kvm(struct kvm *kvm)
899 if (atomic_dec_and_test(&kvm->users_count))
900 kvm_destroy_vm(kvm);
902 EXPORT_SYMBOL_GPL(kvm_put_kvm);
905 static int kvm_vm_release(struct inode *inode, struct file *filp)
907 struct kvm *kvm = filp->private_data;
909 kvm_put_kvm(kvm);
910 return 0;
914 * Allocate some memory and give it an address in the guest physical address
915 * space.
917 * Discontiguous memory is allowed, mostly for framebuffers.
919 * Must be called holding mmap_sem for write.
921 int __kvm_set_memory_region(struct kvm *kvm,
922 struct kvm_userspace_memory_region *mem,
923 int user_alloc)
925 int r;
926 gfn_t base_gfn;
927 unsigned long npages, ugfn;
928 unsigned long largepages, i;
929 struct kvm_memory_slot *memslot;
930 struct kvm_memory_slot old, new;
932 r = -EINVAL;
933 /* General sanity checks */
934 if (mem->memory_size & (PAGE_SIZE - 1))
935 goto out;
936 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
937 goto out;
938 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
939 goto out;
940 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
941 goto out;
942 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
943 goto out;
945 memslot = &kvm->memslots[mem->slot];
946 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
947 npages = mem->memory_size >> PAGE_SHIFT;
949 if (!npages)
950 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
952 new = old = *memslot;
954 new.base_gfn = base_gfn;
955 new.npages = npages;
956 new.flags = mem->flags;
958 /* Disallow changing a memory slot's size. */
959 r = -EINVAL;
960 if (npages && old.npages && npages != old.npages)
961 goto out_free;
963 /* Check for overlaps */
964 r = -EEXIST;
965 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
966 struct kvm_memory_slot *s = &kvm->memslots[i];
968 if (s == memslot || !s->npages)
969 continue;
970 if (!((base_gfn + npages <= s->base_gfn) ||
971 (base_gfn >= s->base_gfn + s->npages)))
972 goto out_free;
975 /* Free page dirty bitmap if unneeded */
976 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
977 new.dirty_bitmap = NULL;
979 r = -ENOMEM;
981 /* Allocate if a slot is being created */
982 #ifndef CONFIG_S390
983 if (npages && !new.rmap) {
984 new.rmap = vmalloc(npages * sizeof(struct page *));
986 if (!new.rmap)
987 goto out_free;
989 memset(new.rmap, 0, npages * sizeof(*new.rmap));
991 new.user_alloc = user_alloc;
993 * hva_to_rmmap() serialzies with the mmu_lock and to be
994 * safe it has to ignore memslots with !user_alloc &&
995 * !userspace_addr.
997 if (user_alloc)
998 new.userspace_addr = mem->userspace_addr;
999 else
1000 new.userspace_addr = 0;
1002 if (npages && !new.lpage_info) {
1003 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1004 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
1006 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1008 if (!new.lpage_info)
1009 goto out_free;
1011 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1013 if (base_gfn % KVM_PAGES_PER_HPAGE)
1014 new.lpage_info[0].write_count = 1;
1015 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1016 new.lpage_info[largepages-1].write_count = 1;
1017 ugfn = new.userspace_addr >> PAGE_SHIFT;
1019 * If the gfn and userspace address are not aligned wrt each
1020 * other, disable large page support for this slot
1022 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1))
1023 for (i = 0; i < largepages; ++i)
1024 new.lpage_info[i].write_count = 1;
1027 /* Allocate page dirty bitmap if needed */
1028 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1029 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1031 new.dirty_bitmap = vmalloc(dirty_bytes);
1032 if (!new.dirty_bitmap)
1033 goto out_free;
1034 memset(new.dirty_bitmap, 0, dirty_bytes);
1035 if (old.npages)
1036 kvm_arch_flush_shadow(kvm);
1038 #endif /* not defined CONFIG_S390 */
1040 if (!npages)
1041 kvm_arch_flush_shadow(kvm);
1043 spin_lock(&kvm->mmu_lock);
1044 if (mem->slot >= kvm->nmemslots)
1045 kvm->nmemslots = mem->slot + 1;
1047 *memslot = new;
1048 spin_unlock(&kvm->mmu_lock);
1050 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1051 if (r) {
1052 spin_lock(&kvm->mmu_lock);
1053 *memslot = old;
1054 spin_unlock(&kvm->mmu_lock);
1055 goto out_free;
1058 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1059 /* Slot deletion case: we have to update the current slot */
1060 spin_lock(&kvm->mmu_lock);
1061 if (!npages)
1062 *memslot = old;
1063 spin_unlock(&kvm->mmu_lock);
1064 #ifdef CONFIG_DMAR
1065 /* map the pages in iommu page table */
1066 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1067 if (r)
1068 goto out;
1069 #endif
1070 return 0;
1072 out_free:
1073 kvm_free_physmem_slot(&new, &old);
1074 out:
1075 return r;
1078 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1080 int kvm_set_memory_region(struct kvm *kvm,
1081 struct kvm_userspace_memory_region *mem,
1082 int user_alloc)
1084 int r;
1086 down_write(&kvm->slots_lock);
1087 r = __kvm_set_memory_region(kvm, mem, user_alloc);
1088 up_write(&kvm->slots_lock);
1089 return r;
1091 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1093 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1094 struct
1095 kvm_userspace_memory_region *mem,
1096 int user_alloc)
1098 if (mem->slot >= KVM_MEMORY_SLOTS)
1099 return -EINVAL;
1100 return kvm_set_memory_region(kvm, mem, user_alloc);
1103 int kvm_get_dirty_log(struct kvm *kvm,
1104 struct kvm_dirty_log *log, int *is_dirty)
1106 struct kvm_memory_slot *memslot;
1107 int r, i;
1108 int n;
1109 unsigned long any = 0;
1111 r = -EINVAL;
1112 if (log->slot >= KVM_MEMORY_SLOTS)
1113 goto out;
1115 memslot = &kvm->memslots[log->slot];
1116 r = -ENOENT;
1117 if (!memslot->dirty_bitmap)
1118 goto out;
1120 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1122 for (i = 0; !any && i < n/sizeof(long); ++i)
1123 any = memslot->dirty_bitmap[i];
1125 r = -EFAULT;
1126 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1127 goto out;
1129 if (any)
1130 *is_dirty = 1;
1132 r = 0;
1133 out:
1134 return r;
1137 int is_error_page(struct page *page)
1139 return page == bad_page;
1141 EXPORT_SYMBOL_GPL(is_error_page);
1143 int is_error_pfn(pfn_t pfn)
1145 return pfn == bad_pfn;
1147 EXPORT_SYMBOL_GPL(is_error_pfn);
1149 static inline unsigned long bad_hva(void)
1151 return PAGE_OFFSET;
1154 int kvm_is_error_hva(unsigned long addr)
1156 return addr == bad_hva();
1158 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1160 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1162 int i;
1164 for (i = 0; i < kvm->nmemslots; ++i) {
1165 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1167 if (gfn >= memslot->base_gfn
1168 && gfn < memslot->base_gfn + memslot->npages)
1169 return memslot;
1171 return NULL;
1173 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1175 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1177 gfn = unalias_gfn(kvm, gfn);
1178 return gfn_to_memslot_unaliased(kvm, gfn);
1181 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1183 int i;
1185 gfn = unalias_gfn(kvm, gfn);
1186 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1187 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1189 if (gfn >= memslot->base_gfn
1190 && gfn < memslot->base_gfn + memslot->npages)
1191 return 1;
1193 return 0;
1195 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1197 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1199 struct kvm_memory_slot *slot;
1201 gfn = unalias_gfn(kvm, gfn);
1202 slot = gfn_to_memslot_unaliased(kvm, gfn);
1203 if (!slot)
1204 return bad_hva();
1205 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1207 EXPORT_SYMBOL_GPL(gfn_to_hva);
1209 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1211 struct page *page[1];
1212 unsigned long addr;
1213 int npages;
1214 pfn_t pfn;
1216 might_sleep();
1218 addr = gfn_to_hva(kvm, gfn);
1219 if (kvm_is_error_hva(addr)) {
1220 get_page(bad_page);
1221 return page_to_pfn(bad_page);
1224 npages = get_user_pages_fast(addr, 1, 1, page);
1226 if (unlikely(npages != 1)) {
1227 struct vm_area_struct *vma;
1229 down_read(&current->mm->mmap_sem);
1230 vma = find_vma(current->mm, addr);
1232 if (vma == NULL || addr < vma->vm_start ||
1233 !(vma->vm_flags & VM_PFNMAP)) {
1234 up_read(&current->mm->mmap_sem);
1235 get_page(bad_page);
1236 return page_to_pfn(bad_page);
1239 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1240 up_read(&current->mm->mmap_sem);
1241 BUG_ON(!kvm_is_mmio_pfn(pfn));
1242 } else
1243 pfn = page_to_pfn(page[0]);
1245 return pfn;
1248 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1250 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1252 pfn_t pfn;
1254 pfn = gfn_to_pfn(kvm, gfn);
1255 if (!kvm_is_mmio_pfn(pfn))
1256 return pfn_to_page(pfn);
1258 WARN_ON(kvm_is_mmio_pfn(pfn));
1260 get_page(bad_page);
1261 return bad_page;
1264 EXPORT_SYMBOL_GPL(gfn_to_page);
1266 void kvm_release_page_clean(struct page *page)
1268 kvm_release_pfn_clean(page_to_pfn(page));
1270 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1272 void kvm_release_pfn_clean(pfn_t pfn)
1274 if (!kvm_is_mmio_pfn(pfn))
1275 put_page(pfn_to_page(pfn));
1277 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1279 void kvm_release_page_dirty(struct page *page)
1281 kvm_release_pfn_dirty(page_to_pfn(page));
1283 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1285 void kvm_release_pfn_dirty(pfn_t pfn)
1287 kvm_set_pfn_dirty(pfn);
1288 kvm_release_pfn_clean(pfn);
1290 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1292 void kvm_set_page_dirty(struct page *page)
1294 kvm_set_pfn_dirty(page_to_pfn(page));
1296 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1298 void kvm_set_pfn_dirty(pfn_t pfn)
1300 if (!kvm_is_mmio_pfn(pfn)) {
1301 struct page *page = pfn_to_page(pfn);
1302 if (!PageReserved(page))
1303 SetPageDirty(page);
1306 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1308 void kvm_set_pfn_accessed(pfn_t pfn)
1310 if (!kvm_is_mmio_pfn(pfn))
1311 mark_page_accessed(pfn_to_page(pfn));
1313 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1315 void kvm_get_pfn(pfn_t pfn)
1317 if (!kvm_is_mmio_pfn(pfn))
1318 get_page(pfn_to_page(pfn));
1320 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1322 static int next_segment(unsigned long len, int offset)
1324 if (len > PAGE_SIZE - offset)
1325 return PAGE_SIZE - offset;
1326 else
1327 return len;
1330 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1331 int len)
1333 int r;
1334 unsigned long addr;
1336 addr = gfn_to_hva(kvm, gfn);
1337 if (kvm_is_error_hva(addr))
1338 return -EFAULT;
1339 r = copy_from_user(data, (void __user *)addr + offset, len);
1340 if (r)
1341 return -EFAULT;
1342 return 0;
1344 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1346 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1348 gfn_t gfn = gpa >> PAGE_SHIFT;
1349 int seg;
1350 int offset = offset_in_page(gpa);
1351 int ret;
1353 while ((seg = next_segment(len, offset)) != 0) {
1354 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1355 if (ret < 0)
1356 return ret;
1357 offset = 0;
1358 len -= seg;
1359 data += seg;
1360 ++gfn;
1362 return 0;
1364 EXPORT_SYMBOL_GPL(kvm_read_guest);
1366 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1367 unsigned long len)
1369 int r;
1370 unsigned long addr;
1371 gfn_t gfn = gpa >> PAGE_SHIFT;
1372 int offset = offset_in_page(gpa);
1374 addr = gfn_to_hva(kvm, gfn);
1375 if (kvm_is_error_hva(addr))
1376 return -EFAULT;
1377 pagefault_disable();
1378 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1379 pagefault_enable();
1380 if (r)
1381 return -EFAULT;
1382 return 0;
1384 EXPORT_SYMBOL(kvm_read_guest_atomic);
1386 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1387 int offset, int len)
1389 int r;
1390 unsigned long addr;
1392 addr = gfn_to_hva(kvm, gfn);
1393 if (kvm_is_error_hva(addr))
1394 return -EFAULT;
1395 r = copy_to_user((void __user *)addr + offset, data, len);
1396 if (r)
1397 return -EFAULT;
1398 mark_page_dirty(kvm, gfn);
1399 return 0;
1401 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1403 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1404 unsigned long len)
1406 gfn_t gfn = gpa >> PAGE_SHIFT;
1407 int seg;
1408 int offset = offset_in_page(gpa);
1409 int ret;
1411 while ((seg = next_segment(len, offset)) != 0) {
1412 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1413 if (ret < 0)
1414 return ret;
1415 offset = 0;
1416 len -= seg;
1417 data += seg;
1418 ++gfn;
1420 return 0;
1423 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1425 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1427 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1429 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1431 gfn_t gfn = gpa >> PAGE_SHIFT;
1432 int seg;
1433 int offset = offset_in_page(gpa);
1434 int ret;
1436 while ((seg = next_segment(len, offset)) != 0) {
1437 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1438 if (ret < 0)
1439 return ret;
1440 offset = 0;
1441 len -= seg;
1442 ++gfn;
1444 return 0;
1446 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1448 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1450 struct kvm_memory_slot *memslot;
1452 gfn = unalias_gfn(kvm, gfn);
1453 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1454 if (memslot && memslot->dirty_bitmap) {
1455 unsigned long rel_gfn = gfn - memslot->base_gfn;
1457 /* avoid RMW */
1458 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1459 set_bit(rel_gfn, memslot->dirty_bitmap);
1464 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1466 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1468 DEFINE_WAIT(wait);
1470 for (;;) {
1471 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1473 if (kvm_cpu_has_interrupt(vcpu) ||
1474 kvm_cpu_has_pending_timer(vcpu) ||
1475 kvm_arch_vcpu_runnable(vcpu)) {
1476 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1477 break;
1479 if (signal_pending(current))
1480 break;
1482 vcpu_put(vcpu);
1483 schedule();
1484 vcpu_load(vcpu);
1487 finish_wait(&vcpu->wq, &wait);
1490 void kvm_resched(struct kvm_vcpu *vcpu)
1492 if (!need_resched())
1493 return;
1494 cond_resched();
1496 EXPORT_SYMBOL_GPL(kvm_resched);
1498 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1500 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1501 struct page *page;
1503 if (vmf->pgoff == 0)
1504 page = virt_to_page(vcpu->run);
1505 #ifdef CONFIG_X86
1506 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1507 page = virt_to_page(vcpu->arch.pio_data);
1508 #endif
1509 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1510 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1511 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1512 #endif
1513 else
1514 return VM_FAULT_SIGBUS;
1515 get_page(page);
1516 vmf->page = page;
1517 return 0;
1520 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1521 .fault = kvm_vcpu_fault,
1524 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1526 vma->vm_ops = &kvm_vcpu_vm_ops;
1527 return 0;
1530 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1532 struct kvm_vcpu *vcpu = filp->private_data;
1534 kvm_put_kvm(vcpu->kvm);
1535 return 0;
1538 static struct file_operations kvm_vcpu_fops = {
1539 .release = kvm_vcpu_release,
1540 .unlocked_ioctl = kvm_vcpu_ioctl,
1541 .compat_ioctl = kvm_vcpu_ioctl,
1542 .mmap = kvm_vcpu_mmap,
1546 * Allocates an inode for the vcpu.
1548 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1550 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1551 if (fd < 0)
1552 kvm_put_kvm(vcpu->kvm);
1553 return fd;
1557 * Creates some virtual cpus. Good luck creating more than one.
1559 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1561 int r;
1562 struct kvm_vcpu *vcpu;
1564 if (!valid_vcpu(n))
1565 return -EINVAL;
1567 vcpu = kvm_arch_vcpu_create(kvm, n);
1568 if (IS_ERR(vcpu))
1569 return PTR_ERR(vcpu);
1571 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1573 r = kvm_arch_vcpu_setup(vcpu);
1574 if (r)
1575 return r;
1577 mutex_lock(&kvm->lock);
1578 if (kvm->vcpus[n]) {
1579 r = -EEXIST;
1580 goto vcpu_destroy;
1582 kvm->vcpus[n] = vcpu;
1583 mutex_unlock(&kvm->lock);
1585 /* Now it's all set up, let userspace reach it */
1586 kvm_get_kvm(kvm);
1587 r = create_vcpu_fd(vcpu);
1588 if (r < 0)
1589 goto unlink;
1590 return r;
1592 unlink:
1593 mutex_lock(&kvm->lock);
1594 kvm->vcpus[n] = NULL;
1595 vcpu_destroy:
1596 mutex_unlock(&kvm->lock);
1597 kvm_arch_vcpu_destroy(vcpu);
1598 return r;
1601 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1603 if (sigset) {
1604 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1605 vcpu->sigset_active = 1;
1606 vcpu->sigset = *sigset;
1607 } else
1608 vcpu->sigset_active = 0;
1609 return 0;
1612 static long kvm_vcpu_ioctl(struct file *filp,
1613 unsigned int ioctl, unsigned long arg)
1615 struct kvm_vcpu *vcpu = filp->private_data;
1616 void __user *argp = (void __user *)arg;
1617 int r;
1618 struct kvm_fpu *fpu = NULL;
1619 struct kvm_sregs *kvm_sregs = NULL;
1621 if (vcpu->kvm->mm != current->mm)
1622 return -EIO;
1623 switch (ioctl) {
1624 case KVM_RUN:
1625 r = -EINVAL;
1626 if (arg)
1627 goto out;
1628 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1629 break;
1630 case KVM_GET_REGS: {
1631 struct kvm_regs *kvm_regs;
1633 r = -ENOMEM;
1634 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1635 if (!kvm_regs)
1636 goto out;
1637 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1638 if (r)
1639 goto out_free1;
1640 r = -EFAULT;
1641 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1642 goto out_free1;
1643 r = 0;
1644 out_free1:
1645 kfree(kvm_regs);
1646 break;
1648 case KVM_SET_REGS: {
1649 struct kvm_regs *kvm_regs;
1651 r = -ENOMEM;
1652 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1653 if (!kvm_regs)
1654 goto out;
1655 r = -EFAULT;
1656 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1657 goto out_free2;
1658 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1659 if (r)
1660 goto out_free2;
1661 r = 0;
1662 out_free2:
1663 kfree(kvm_regs);
1664 break;
1666 case KVM_GET_SREGS: {
1667 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1668 r = -ENOMEM;
1669 if (!kvm_sregs)
1670 goto out;
1671 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1672 if (r)
1673 goto out;
1674 r = -EFAULT;
1675 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1676 goto out;
1677 r = 0;
1678 break;
1680 case KVM_SET_SREGS: {
1681 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1682 r = -ENOMEM;
1683 if (!kvm_sregs)
1684 goto out;
1685 r = -EFAULT;
1686 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1687 goto out;
1688 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1689 if (r)
1690 goto out;
1691 r = 0;
1692 break;
1694 case KVM_GET_MP_STATE: {
1695 struct kvm_mp_state mp_state;
1697 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1698 if (r)
1699 goto out;
1700 r = -EFAULT;
1701 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1702 goto out;
1703 r = 0;
1704 break;
1706 case KVM_SET_MP_STATE: {
1707 struct kvm_mp_state mp_state;
1709 r = -EFAULT;
1710 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1711 goto out;
1712 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1713 if (r)
1714 goto out;
1715 r = 0;
1716 break;
1718 case KVM_TRANSLATE: {
1719 struct kvm_translation tr;
1721 r = -EFAULT;
1722 if (copy_from_user(&tr, argp, sizeof tr))
1723 goto out;
1724 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1725 if (r)
1726 goto out;
1727 r = -EFAULT;
1728 if (copy_to_user(argp, &tr, sizeof tr))
1729 goto out;
1730 r = 0;
1731 break;
1733 case KVM_SET_GUEST_DEBUG: {
1734 struct kvm_guest_debug dbg;
1736 r = -EFAULT;
1737 if (copy_from_user(&dbg, argp, sizeof dbg))
1738 goto out;
1739 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1740 if (r)
1741 goto out;
1742 r = 0;
1743 break;
1745 case KVM_SET_SIGNAL_MASK: {
1746 struct kvm_signal_mask __user *sigmask_arg = argp;
1747 struct kvm_signal_mask kvm_sigmask;
1748 sigset_t sigset, *p;
1750 p = NULL;
1751 if (argp) {
1752 r = -EFAULT;
1753 if (copy_from_user(&kvm_sigmask, argp,
1754 sizeof kvm_sigmask))
1755 goto out;
1756 r = -EINVAL;
1757 if (kvm_sigmask.len != sizeof sigset)
1758 goto out;
1759 r = -EFAULT;
1760 if (copy_from_user(&sigset, sigmask_arg->sigset,
1761 sizeof sigset))
1762 goto out;
1763 p = &sigset;
1765 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1766 break;
1768 case KVM_GET_FPU: {
1769 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1770 r = -ENOMEM;
1771 if (!fpu)
1772 goto out;
1773 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1774 if (r)
1775 goto out;
1776 r = -EFAULT;
1777 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1778 goto out;
1779 r = 0;
1780 break;
1782 case KVM_SET_FPU: {
1783 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1784 r = -ENOMEM;
1785 if (!fpu)
1786 goto out;
1787 r = -EFAULT;
1788 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1789 goto out;
1790 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1791 if (r)
1792 goto out;
1793 r = 0;
1794 break;
1796 default:
1797 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1799 out:
1800 kfree(fpu);
1801 kfree(kvm_sregs);
1802 return r;
1805 static long kvm_vm_ioctl(struct file *filp,
1806 unsigned int ioctl, unsigned long arg)
1808 struct kvm *kvm = filp->private_data;
1809 void __user *argp = (void __user *)arg;
1810 int r;
1812 if (kvm->mm != current->mm)
1813 return -EIO;
1814 switch (ioctl) {
1815 case KVM_CREATE_VCPU:
1816 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1817 if (r < 0)
1818 goto out;
1819 break;
1820 case KVM_SET_USER_MEMORY_REGION: {
1821 struct kvm_userspace_memory_region kvm_userspace_mem;
1823 r = -EFAULT;
1824 if (copy_from_user(&kvm_userspace_mem, argp,
1825 sizeof kvm_userspace_mem))
1826 goto out;
1828 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1829 if (r)
1830 goto out;
1831 break;
1833 case KVM_GET_DIRTY_LOG: {
1834 struct kvm_dirty_log log;
1836 r = -EFAULT;
1837 if (copy_from_user(&log, argp, sizeof log))
1838 goto out;
1839 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1840 if (r)
1841 goto out;
1842 break;
1844 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1845 case KVM_REGISTER_COALESCED_MMIO: {
1846 struct kvm_coalesced_mmio_zone zone;
1847 r = -EFAULT;
1848 if (copy_from_user(&zone, argp, sizeof zone))
1849 goto out;
1850 r = -ENXIO;
1851 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1852 if (r)
1853 goto out;
1854 r = 0;
1855 break;
1857 case KVM_UNREGISTER_COALESCED_MMIO: {
1858 struct kvm_coalesced_mmio_zone zone;
1859 r = -EFAULT;
1860 if (copy_from_user(&zone, argp, sizeof zone))
1861 goto out;
1862 r = -ENXIO;
1863 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1864 if (r)
1865 goto out;
1866 r = 0;
1867 break;
1869 #endif
1870 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1871 case KVM_ASSIGN_PCI_DEVICE: {
1872 struct kvm_assigned_pci_dev assigned_dev;
1874 r = -EFAULT;
1875 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1876 goto out;
1877 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1878 if (r)
1879 goto out;
1880 break;
1882 case KVM_ASSIGN_IRQ: {
1883 struct kvm_assigned_irq assigned_irq;
1885 r = -EFAULT;
1886 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1887 goto out;
1888 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1889 if (r)
1890 goto out;
1891 break;
1893 #endif
1894 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1895 case KVM_DEASSIGN_PCI_DEVICE: {
1896 struct kvm_assigned_pci_dev assigned_dev;
1898 r = -EFAULT;
1899 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1900 goto out;
1901 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
1902 if (r)
1903 goto out;
1904 break;
1906 #endif
1907 #ifdef KVM_CAP_IRQ_ROUTING
1908 case KVM_SET_GSI_ROUTING: {
1909 struct kvm_irq_routing routing;
1910 struct kvm_irq_routing __user *urouting;
1911 struct kvm_irq_routing_entry *entries;
1913 r = -EFAULT;
1914 if (copy_from_user(&routing, argp, sizeof(routing)))
1915 goto out;
1916 r = -EINVAL;
1917 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
1918 goto out;
1919 if (routing.flags)
1920 goto out;
1921 r = -ENOMEM;
1922 entries = vmalloc(routing.nr * sizeof(*entries));
1923 if (!entries)
1924 goto out;
1925 r = -EFAULT;
1926 urouting = argp;
1927 if (copy_from_user(entries, urouting->entries,
1928 routing.nr * sizeof(*entries)))
1929 goto out_free_irq_routing;
1930 r = kvm_set_irq_routing(kvm, entries, routing.nr,
1931 routing.flags);
1932 out_free_irq_routing:
1933 vfree(entries);
1934 break;
1936 #endif
1937 default:
1938 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1940 out:
1941 return r;
1944 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1946 struct page *page[1];
1947 unsigned long addr;
1948 int npages;
1949 gfn_t gfn = vmf->pgoff;
1950 struct kvm *kvm = vma->vm_file->private_data;
1952 addr = gfn_to_hva(kvm, gfn);
1953 if (kvm_is_error_hva(addr))
1954 return VM_FAULT_SIGBUS;
1956 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1957 NULL);
1958 if (unlikely(npages != 1))
1959 return VM_FAULT_SIGBUS;
1961 vmf->page = page[0];
1962 return 0;
1965 static struct vm_operations_struct kvm_vm_vm_ops = {
1966 .fault = kvm_vm_fault,
1969 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1971 vma->vm_ops = &kvm_vm_vm_ops;
1972 return 0;
1975 static struct file_operations kvm_vm_fops = {
1976 .release = kvm_vm_release,
1977 .unlocked_ioctl = kvm_vm_ioctl,
1978 .compat_ioctl = kvm_vm_ioctl,
1979 .mmap = kvm_vm_mmap,
1982 static int kvm_dev_ioctl_create_vm(void)
1984 int fd;
1985 struct kvm *kvm;
1987 kvm = kvm_create_vm();
1988 if (IS_ERR(kvm))
1989 return PTR_ERR(kvm);
1990 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1991 if (fd < 0)
1992 kvm_put_kvm(kvm);
1994 return fd;
1997 static long kvm_dev_ioctl_check_extension_generic(long arg)
1999 switch (arg) {
2000 case KVM_CAP_USER_MEMORY:
2001 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2002 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2003 return 1;
2004 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2005 case KVM_CAP_IRQ_ROUTING:
2006 return KVM_MAX_IRQ_ROUTES;
2007 #endif
2008 default:
2009 break;
2011 return kvm_dev_ioctl_check_extension(arg);
2014 static long kvm_dev_ioctl(struct file *filp,
2015 unsigned int ioctl, unsigned long arg)
2017 long r = -EINVAL;
2019 switch (ioctl) {
2020 case KVM_GET_API_VERSION:
2021 r = -EINVAL;
2022 if (arg)
2023 goto out;
2024 r = KVM_API_VERSION;
2025 break;
2026 case KVM_CREATE_VM:
2027 r = -EINVAL;
2028 if (arg)
2029 goto out;
2030 r = kvm_dev_ioctl_create_vm();
2031 break;
2032 case KVM_CHECK_EXTENSION:
2033 r = kvm_dev_ioctl_check_extension_generic(arg);
2034 break;
2035 case KVM_GET_VCPU_MMAP_SIZE:
2036 r = -EINVAL;
2037 if (arg)
2038 goto out;
2039 r = PAGE_SIZE; /* struct kvm_run */
2040 #ifdef CONFIG_X86
2041 r += PAGE_SIZE; /* pio data page */
2042 #endif
2043 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2044 r += PAGE_SIZE; /* coalesced mmio ring page */
2045 #endif
2046 break;
2047 case KVM_TRACE_ENABLE:
2048 case KVM_TRACE_PAUSE:
2049 case KVM_TRACE_DISABLE:
2050 r = kvm_trace_ioctl(ioctl, arg);
2051 break;
2052 default:
2053 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2055 out:
2056 return r;
2059 static struct file_operations kvm_chardev_ops = {
2060 .unlocked_ioctl = kvm_dev_ioctl,
2061 .compat_ioctl = kvm_dev_ioctl,
2064 static struct miscdevice kvm_dev = {
2065 KVM_MINOR,
2066 "kvm",
2067 &kvm_chardev_ops,
2070 static void hardware_enable(void *junk)
2072 int cpu = raw_smp_processor_id();
2074 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2075 return;
2076 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2077 kvm_arch_hardware_enable(NULL);
2080 static void hardware_disable(void *junk)
2082 int cpu = raw_smp_processor_id();
2084 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2085 return;
2086 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2087 kvm_arch_hardware_disable(NULL);
2090 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2091 void *v)
2093 int cpu = (long)v;
2095 val &= ~CPU_TASKS_FROZEN;
2096 switch (val) {
2097 case CPU_DYING:
2098 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2099 cpu);
2100 hardware_disable(NULL);
2101 break;
2102 case CPU_UP_CANCELED:
2103 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2104 cpu);
2105 smp_call_function_single(cpu, hardware_disable, NULL, 1);
2106 break;
2107 case CPU_ONLINE:
2108 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2109 cpu);
2110 smp_call_function_single(cpu, hardware_enable, NULL, 1);
2111 break;
2113 return NOTIFY_OK;
2117 asmlinkage void kvm_handle_fault_on_reboot(void)
2119 if (kvm_rebooting)
2120 /* spin while reset goes on */
2121 while (true)
2123 /* Fault while not rebooting. We want the trace. */
2124 BUG();
2126 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2128 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2129 void *v)
2131 if (val == SYS_RESTART) {
2133 * Some (well, at least mine) BIOSes hang on reboot if
2134 * in vmx root mode.
2136 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2137 kvm_rebooting = true;
2138 on_each_cpu(hardware_disable, NULL, 1);
2140 return NOTIFY_OK;
2143 static struct notifier_block kvm_reboot_notifier = {
2144 .notifier_call = kvm_reboot,
2145 .priority = 0,
2148 void kvm_io_bus_init(struct kvm_io_bus *bus)
2150 memset(bus, 0, sizeof(*bus));
2153 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2155 int i;
2157 for (i = 0; i < bus->dev_count; i++) {
2158 struct kvm_io_device *pos = bus->devs[i];
2160 kvm_iodevice_destructor(pos);
2164 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2165 gpa_t addr, int len, int is_write)
2167 int i;
2169 for (i = 0; i < bus->dev_count; i++) {
2170 struct kvm_io_device *pos = bus->devs[i];
2172 if (pos->in_range(pos, addr, len, is_write))
2173 return pos;
2176 return NULL;
2179 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2181 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2183 bus->devs[bus->dev_count++] = dev;
2186 static struct notifier_block kvm_cpu_notifier = {
2187 .notifier_call = kvm_cpu_hotplug,
2188 .priority = 20, /* must be > scheduler priority */
2191 static int vm_stat_get(void *_offset, u64 *val)
2193 unsigned offset = (long)_offset;
2194 struct kvm *kvm;
2196 *val = 0;
2197 spin_lock(&kvm_lock);
2198 list_for_each_entry(kvm, &vm_list, vm_list)
2199 *val += *(u32 *)((void *)kvm + offset);
2200 spin_unlock(&kvm_lock);
2201 return 0;
2204 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2206 static int vcpu_stat_get(void *_offset, u64 *val)
2208 unsigned offset = (long)_offset;
2209 struct kvm *kvm;
2210 struct kvm_vcpu *vcpu;
2211 int i;
2213 *val = 0;
2214 spin_lock(&kvm_lock);
2215 list_for_each_entry(kvm, &vm_list, vm_list)
2216 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2217 vcpu = kvm->vcpus[i];
2218 if (vcpu)
2219 *val += *(u32 *)((void *)vcpu + offset);
2221 spin_unlock(&kvm_lock);
2222 return 0;
2225 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2227 static struct file_operations *stat_fops[] = {
2228 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2229 [KVM_STAT_VM] = &vm_stat_fops,
2232 static void kvm_init_debug(void)
2234 struct kvm_stats_debugfs_item *p;
2236 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2237 for (p = debugfs_entries; p->name; ++p)
2238 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2239 (void *)(long)p->offset,
2240 stat_fops[p->kind]);
2243 static void kvm_exit_debug(void)
2245 struct kvm_stats_debugfs_item *p;
2247 for (p = debugfs_entries; p->name; ++p)
2248 debugfs_remove(p->dentry);
2249 debugfs_remove(kvm_debugfs_dir);
2252 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2254 hardware_disable(NULL);
2255 return 0;
2258 static int kvm_resume(struct sys_device *dev)
2260 hardware_enable(NULL);
2261 return 0;
2264 static struct sysdev_class kvm_sysdev_class = {
2265 .name = "kvm",
2266 .suspend = kvm_suspend,
2267 .resume = kvm_resume,
2270 static struct sys_device kvm_sysdev = {
2271 .id = 0,
2272 .cls = &kvm_sysdev_class,
2275 struct page *bad_page;
2276 pfn_t bad_pfn;
2278 static inline
2279 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2281 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2284 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2286 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2288 kvm_arch_vcpu_load(vcpu, cpu);
2291 static void kvm_sched_out(struct preempt_notifier *pn,
2292 struct task_struct *next)
2294 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2296 kvm_arch_vcpu_put(vcpu);
2299 int kvm_init(void *opaque, unsigned int vcpu_size,
2300 struct module *module)
2302 int r;
2303 int cpu;
2305 kvm_init_debug();
2307 r = kvm_arch_init(opaque);
2308 if (r)
2309 goto out_fail;
2311 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2313 if (bad_page == NULL) {
2314 r = -ENOMEM;
2315 goto out;
2318 bad_pfn = page_to_pfn(bad_page);
2320 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2321 r = -ENOMEM;
2322 goto out_free_0;
2324 cpumask_clear(cpus_hardware_enabled);
2326 r = kvm_arch_hardware_setup();
2327 if (r < 0)
2328 goto out_free_0a;
2330 for_each_online_cpu(cpu) {
2331 smp_call_function_single(cpu,
2332 kvm_arch_check_processor_compat,
2333 &r, 1);
2334 if (r < 0)
2335 goto out_free_1;
2338 on_each_cpu(hardware_enable, NULL, 1);
2339 r = register_cpu_notifier(&kvm_cpu_notifier);
2340 if (r)
2341 goto out_free_2;
2342 register_reboot_notifier(&kvm_reboot_notifier);
2344 r = sysdev_class_register(&kvm_sysdev_class);
2345 if (r)
2346 goto out_free_3;
2348 r = sysdev_register(&kvm_sysdev);
2349 if (r)
2350 goto out_free_4;
2352 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2353 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2354 __alignof__(struct kvm_vcpu),
2355 0, NULL);
2356 if (!kvm_vcpu_cache) {
2357 r = -ENOMEM;
2358 goto out_free_5;
2361 kvm_chardev_ops.owner = module;
2362 kvm_vm_fops.owner = module;
2363 kvm_vcpu_fops.owner = module;
2365 r = misc_register(&kvm_dev);
2366 if (r) {
2367 printk(KERN_ERR "kvm: misc device register failed\n");
2368 goto out_free;
2371 kvm_preempt_ops.sched_in = kvm_sched_in;
2372 kvm_preempt_ops.sched_out = kvm_sched_out;
2373 #ifndef CONFIG_X86
2374 msi2intx = 0;
2375 #endif
2377 return 0;
2379 out_free:
2380 kmem_cache_destroy(kvm_vcpu_cache);
2381 out_free_5:
2382 sysdev_unregister(&kvm_sysdev);
2383 out_free_4:
2384 sysdev_class_unregister(&kvm_sysdev_class);
2385 out_free_3:
2386 unregister_reboot_notifier(&kvm_reboot_notifier);
2387 unregister_cpu_notifier(&kvm_cpu_notifier);
2388 out_free_2:
2389 on_each_cpu(hardware_disable, NULL, 1);
2390 out_free_1:
2391 kvm_arch_hardware_unsetup();
2392 out_free_0a:
2393 free_cpumask_var(cpus_hardware_enabled);
2394 out_free_0:
2395 __free_page(bad_page);
2396 out:
2397 kvm_arch_exit();
2398 kvm_exit_debug();
2399 out_fail:
2400 return r;
2402 EXPORT_SYMBOL_GPL(kvm_init);
2404 void kvm_exit(void)
2406 kvm_trace_cleanup();
2407 misc_deregister(&kvm_dev);
2408 kmem_cache_destroy(kvm_vcpu_cache);
2409 sysdev_unregister(&kvm_sysdev);
2410 sysdev_class_unregister(&kvm_sysdev_class);
2411 unregister_reboot_notifier(&kvm_reboot_notifier);
2412 unregister_cpu_notifier(&kvm_cpu_notifier);
2413 on_each_cpu(hardware_disable, NULL, 1);
2414 kvm_arch_hardware_unsetup();
2415 kvm_arch_exit();
2416 kvm_exit_debug();
2417 free_cpumask_var(cpus_hardware_enabled);
2418 __free_page(bad_page);
2420 EXPORT_SYMBOL_GPL(kvm_exit);