KVM: Fix dirty bit tracking for slots with large pages
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / virt / kvm / kvm_main.c
blob148982913805d052704b01d9e196d5aaae7c64a3
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 #endif
885 kvm_arch_destroy_vm(kvm);
886 mmdrop(mm);
889 void kvm_get_kvm(struct kvm *kvm)
891 atomic_inc(&kvm->users_count);
893 EXPORT_SYMBOL_GPL(kvm_get_kvm);
895 void kvm_put_kvm(struct kvm *kvm)
897 if (atomic_dec_and_test(&kvm->users_count))
898 kvm_destroy_vm(kvm);
900 EXPORT_SYMBOL_GPL(kvm_put_kvm);
903 static int kvm_vm_release(struct inode *inode, struct file *filp)
905 struct kvm *kvm = filp->private_data;
907 kvm_put_kvm(kvm);
908 return 0;
912 * Allocate some memory and give it an address in the guest physical address
913 * space.
915 * Discontiguous memory is allowed, mostly for framebuffers.
917 * Must be called holding mmap_sem for write.
919 int __kvm_set_memory_region(struct kvm *kvm,
920 struct kvm_userspace_memory_region *mem,
921 int user_alloc)
923 int r;
924 gfn_t base_gfn;
925 unsigned long npages, ugfn;
926 unsigned long largepages, i;
927 struct kvm_memory_slot *memslot;
928 struct kvm_memory_slot old, new;
930 r = -EINVAL;
931 /* General sanity checks */
932 if (mem->memory_size & (PAGE_SIZE - 1))
933 goto out;
934 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
935 goto out;
936 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
937 goto out;
938 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
939 goto out;
940 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
941 goto out;
943 memslot = &kvm->memslots[mem->slot];
944 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
945 npages = mem->memory_size >> PAGE_SHIFT;
947 if (!npages)
948 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
950 new = old = *memslot;
952 new.base_gfn = base_gfn;
953 new.npages = npages;
954 new.flags = mem->flags;
956 /* Disallow changing a memory slot's size. */
957 r = -EINVAL;
958 if (npages && old.npages && npages != old.npages)
959 goto out_free;
961 /* Check for overlaps */
962 r = -EEXIST;
963 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
964 struct kvm_memory_slot *s = &kvm->memslots[i];
966 if (s == memslot || !s->npages)
967 continue;
968 if (!((base_gfn + npages <= s->base_gfn) ||
969 (base_gfn >= s->base_gfn + s->npages)))
970 goto out_free;
973 /* Free page dirty bitmap if unneeded */
974 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
975 new.dirty_bitmap = NULL;
977 r = -ENOMEM;
979 /* Allocate if a slot is being created */
980 #ifndef CONFIG_S390
981 if (npages && !new.rmap) {
982 new.rmap = vmalloc(npages * sizeof(struct page *));
984 if (!new.rmap)
985 goto out_free;
987 memset(new.rmap, 0, npages * sizeof(*new.rmap));
989 new.user_alloc = user_alloc;
991 * hva_to_rmmap() serialzies with the mmu_lock and to be
992 * safe it has to ignore memslots with !user_alloc &&
993 * !userspace_addr.
995 if (user_alloc)
996 new.userspace_addr = mem->userspace_addr;
997 else
998 new.userspace_addr = 0;
1000 if (npages && !new.lpage_info) {
1001 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1002 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
1004 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1006 if (!new.lpage_info)
1007 goto out_free;
1009 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1011 if (base_gfn % KVM_PAGES_PER_HPAGE)
1012 new.lpage_info[0].write_count = 1;
1013 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1014 new.lpage_info[largepages-1].write_count = 1;
1015 ugfn = new.userspace_addr >> PAGE_SHIFT;
1017 * If the gfn and userspace address are not aligned wrt each
1018 * other, disable large page support for this slot
1020 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1))
1021 for (i = 0; i < largepages; ++i)
1022 new.lpage_info[i].write_count = 1;
1025 /* Allocate page dirty bitmap if needed */
1026 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1027 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1029 new.dirty_bitmap = vmalloc(dirty_bytes);
1030 if (!new.dirty_bitmap)
1031 goto out_free;
1032 memset(new.dirty_bitmap, 0, dirty_bytes);
1033 if (old.npages)
1034 kvm_arch_flush_shadow(kvm);
1036 #endif /* not defined CONFIG_S390 */
1038 if (!npages)
1039 kvm_arch_flush_shadow(kvm);
1041 spin_lock(&kvm->mmu_lock);
1042 if (mem->slot >= kvm->nmemslots)
1043 kvm->nmemslots = mem->slot + 1;
1045 *memslot = new;
1046 spin_unlock(&kvm->mmu_lock);
1048 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1049 if (r) {
1050 spin_lock(&kvm->mmu_lock);
1051 *memslot = old;
1052 spin_unlock(&kvm->mmu_lock);
1053 goto out_free;
1056 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1057 /* Slot deletion case: we have to update the current slot */
1058 if (!npages)
1059 *memslot = old;
1060 #ifdef CONFIG_DMAR
1061 /* map the pages in iommu page table */
1062 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1063 if (r)
1064 goto out;
1065 #endif
1066 return 0;
1068 out_free:
1069 kvm_free_physmem_slot(&new, &old);
1070 out:
1071 return r;
1074 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1076 int kvm_set_memory_region(struct kvm *kvm,
1077 struct kvm_userspace_memory_region *mem,
1078 int user_alloc)
1080 int r;
1082 down_write(&kvm->slots_lock);
1083 r = __kvm_set_memory_region(kvm, mem, user_alloc);
1084 up_write(&kvm->slots_lock);
1085 return r;
1087 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1089 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1090 struct
1091 kvm_userspace_memory_region *mem,
1092 int user_alloc)
1094 if (mem->slot >= KVM_MEMORY_SLOTS)
1095 return -EINVAL;
1096 return kvm_set_memory_region(kvm, mem, user_alloc);
1099 int kvm_get_dirty_log(struct kvm *kvm,
1100 struct kvm_dirty_log *log, int *is_dirty)
1102 struct kvm_memory_slot *memslot;
1103 int r, i;
1104 int n;
1105 unsigned long any = 0;
1107 r = -EINVAL;
1108 if (log->slot >= KVM_MEMORY_SLOTS)
1109 goto out;
1111 memslot = &kvm->memslots[log->slot];
1112 r = -ENOENT;
1113 if (!memslot->dirty_bitmap)
1114 goto out;
1116 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1118 for (i = 0; !any && i < n/sizeof(long); ++i)
1119 any = memslot->dirty_bitmap[i];
1121 r = -EFAULT;
1122 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1123 goto out;
1125 if (any)
1126 *is_dirty = 1;
1128 r = 0;
1129 out:
1130 return r;
1133 int is_error_page(struct page *page)
1135 return page == bad_page;
1137 EXPORT_SYMBOL_GPL(is_error_page);
1139 int is_error_pfn(pfn_t pfn)
1141 return pfn == bad_pfn;
1143 EXPORT_SYMBOL_GPL(is_error_pfn);
1145 static inline unsigned long bad_hva(void)
1147 return PAGE_OFFSET;
1150 int kvm_is_error_hva(unsigned long addr)
1152 return addr == bad_hva();
1154 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1156 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1158 int i;
1160 for (i = 0; i < kvm->nmemslots; ++i) {
1161 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1163 if (gfn >= memslot->base_gfn
1164 && gfn < memslot->base_gfn + memslot->npages)
1165 return memslot;
1167 return NULL;
1169 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1171 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1173 gfn = unalias_gfn(kvm, gfn);
1174 return gfn_to_memslot_unaliased(kvm, gfn);
1177 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1179 int i;
1181 gfn = unalias_gfn(kvm, gfn);
1182 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1183 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1185 if (gfn >= memslot->base_gfn
1186 && gfn < memslot->base_gfn + memslot->npages)
1187 return 1;
1189 return 0;
1191 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1193 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1195 struct kvm_memory_slot *slot;
1197 gfn = unalias_gfn(kvm, gfn);
1198 slot = gfn_to_memslot_unaliased(kvm, gfn);
1199 if (!slot)
1200 return bad_hva();
1201 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1203 EXPORT_SYMBOL_GPL(gfn_to_hva);
1205 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1207 struct page *page[1];
1208 unsigned long addr;
1209 int npages;
1210 pfn_t pfn;
1212 might_sleep();
1214 addr = gfn_to_hva(kvm, gfn);
1215 if (kvm_is_error_hva(addr)) {
1216 get_page(bad_page);
1217 return page_to_pfn(bad_page);
1220 npages = get_user_pages_fast(addr, 1, 1, page);
1222 if (unlikely(npages != 1)) {
1223 struct vm_area_struct *vma;
1225 down_read(&current->mm->mmap_sem);
1226 vma = find_vma(current->mm, addr);
1228 if (vma == NULL || addr < vma->vm_start ||
1229 !(vma->vm_flags & VM_PFNMAP)) {
1230 up_read(&current->mm->mmap_sem);
1231 get_page(bad_page);
1232 return page_to_pfn(bad_page);
1235 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1236 up_read(&current->mm->mmap_sem);
1237 BUG_ON(!kvm_is_mmio_pfn(pfn));
1238 } else
1239 pfn = page_to_pfn(page[0]);
1241 return pfn;
1244 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1246 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1248 pfn_t pfn;
1250 pfn = gfn_to_pfn(kvm, gfn);
1251 if (!kvm_is_mmio_pfn(pfn))
1252 return pfn_to_page(pfn);
1254 WARN_ON(kvm_is_mmio_pfn(pfn));
1256 get_page(bad_page);
1257 return bad_page;
1260 EXPORT_SYMBOL_GPL(gfn_to_page);
1262 void kvm_release_page_clean(struct page *page)
1264 kvm_release_pfn_clean(page_to_pfn(page));
1266 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1268 void kvm_release_pfn_clean(pfn_t pfn)
1270 if (!kvm_is_mmio_pfn(pfn))
1271 put_page(pfn_to_page(pfn));
1273 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1275 void kvm_release_page_dirty(struct page *page)
1277 kvm_release_pfn_dirty(page_to_pfn(page));
1279 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1281 void kvm_release_pfn_dirty(pfn_t pfn)
1283 kvm_set_pfn_dirty(pfn);
1284 kvm_release_pfn_clean(pfn);
1286 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1288 void kvm_set_page_dirty(struct page *page)
1290 kvm_set_pfn_dirty(page_to_pfn(page));
1292 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1294 void kvm_set_pfn_dirty(pfn_t pfn)
1296 if (!kvm_is_mmio_pfn(pfn)) {
1297 struct page *page = pfn_to_page(pfn);
1298 if (!PageReserved(page))
1299 SetPageDirty(page);
1302 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1304 void kvm_set_pfn_accessed(pfn_t pfn)
1306 if (!kvm_is_mmio_pfn(pfn))
1307 mark_page_accessed(pfn_to_page(pfn));
1309 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1311 void kvm_get_pfn(pfn_t pfn)
1313 if (!kvm_is_mmio_pfn(pfn))
1314 get_page(pfn_to_page(pfn));
1316 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1318 static int next_segment(unsigned long len, int offset)
1320 if (len > PAGE_SIZE - offset)
1321 return PAGE_SIZE - offset;
1322 else
1323 return len;
1326 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1327 int len)
1329 int r;
1330 unsigned long addr;
1332 addr = gfn_to_hva(kvm, gfn);
1333 if (kvm_is_error_hva(addr))
1334 return -EFAULT;
1335 r = copy_from_user(data, (void __user *)addr + offset, len);
1336 if (r)
1337 return -EFAULT;
1338 return 0;
1340 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1342 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1344 gfn_t gfn = gpa >> PAGE_SHIFT;
1345 int seg;
1346 int offset = offset_in_page(gpa);
1347 int ret;
1349 while ((seg = next_segment(len, offset)) != 0) {
1350 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1351 if (ret < 0)
1352 return ret;
1353 offset = 0;
1354 len -= seg;
1355 data += seg;
1356 ++gfn;
1358 return 0;
1360 EXPORT_SYMBOL_GPL(kvm_read_guest);
1362 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1363 unsigned long len)
1365 int r;
1366 unsigned long addr;
1367 gfn_t gfn = gpa >> PAGE_SHIFT;
1368 int offset = offset_in_page(gpa);
1370 addr = gfn_to_hva(kvm, gfn);
1371 if (kvm_is_error_hva(addr))
1372 return -EFAULT;
1373 pagefault_disable();
1374 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1375 pagefault_enable();
1376 if (r)
1377 return -EFAULT;
1378 return 0;
1380 EXPORT_SYMBOL(kvm_read_guest_atomic);
1382 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1383 int offset, int len)
1385 int r;
1386 unsigned long addr;
1388 addr = gfn_to_hva(kvm, gfn);
1389 if (kvm_is_error_hva(addr))
1390 return -EFAULT;
1391 r = copy_to_user((void __user *)addr + offset, data, len);
1392 if (r)
1393 return -EFAULT;
1394 mark_page_dirty(kvm, gfn);
1395 return 0;
1397 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1399 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1400 unsigned long len)
1402 gfn_t gfn = gpa >> PAGE_SHIFT;
1403 int seg;
1404 int offset = offset_in_page(gpa);
1405 int ret;
1407 while ((seg = next_segment(len, offset)) != 0) {
1408 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1409 if (ret < 0)
1410 return ret;
1411 offset = 0;
1412 len -= seg;
1413 data += seg;
1414 ++gfn;
1416 return 0;
1419 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1421 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1423 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1425 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1427 gfn_t gfn = gpa >> PAGE_SHIFT;
1428 int seg;
1429 int offset = offset_in_page(gpa);
1430 int ret;
1432 while ((seg = next_segment(len, offset)) != 0) {
1433 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1434 if (ret < 0)
1435 return ret;
1436 offset = 0;
1437 len -= seg;
1438 ++gfn;
1440 return 0;
1442 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1444 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1446 struct kvm_memory_slot *memslot;
1448 gfn = unalias_gfn(kvm, gfn);
1449 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1450 if (memslot && memslot->dirty_bitmap) {
1451 unsigned long rel_gfn = gfn - memslot->base_gfn;
1453 /* avoid RMW */
1454 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1455 set_bit(rel_gfn, memslot->dirty_bitmap);
1460 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1462 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1464 DEFINE_WAIT(wait);
1466 for (;;) {
1467 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1469 if (kvm_cpu_has_interrupt(vcpu) ||
1470 kvm_cpu_has_pending_timer(vcpu) ||
1471 kvm_arch_vcpu_runnable(vcpu)) {
1472 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1473 break;
1475 if (signal_pending(current))
1476 break;
1478 vcpu_put(vcpu);
1479 schedule();
1480 vcpu_load(vcpu);
1483 finish_wait(&vcpu->wq, &wait);
1486 void kvm_resched(struct kvm_vcpu *vcpu)
1488 if (!need_resched())
1489 return;
1490 cond_resched();
1492 EXPORT_SYMBOL_GPL(kvm_resched);
1494 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1496 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1497 struct page *page;
1499 if (vmf->pgoff == 0)
1500 page = virt_to_page(vcpu->run);
1501 #ifdef CONFIG_X86
1502 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1503 page = virt_to_page(vcpu->arch.pio_data);
1504 #endif
1505 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1506 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1507 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1508 #endif
1509 else
1510 return VM_FAULT_SIGBUS;
1511 get_page(page);
1512 vmf->page = page;
1513 return 0;
1516 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1517 .fault = kvm_vcpu_fault,
1520 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1522 vma->vm_ops = &kvm_vcpu_vm_ops;
1523 return 0;
1526 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1528 struct kvm_vcpu *vcpu = filp->private_data;
1530 kvm_put_kvm(vcpu->kvm);
1531 return 0;
1534 static struct file_operations kvm_vcpu_fops = {
1535 .release = kvm_vcpu_release,
1536 .unlocked_ioctl = kvm_vcpu_ioctl,
1537 .compat_ioctl = kvm_vcpu_ioctl,
1538 .mmap = kvm_vcpu_mmap,
1542 * Allocates an inode for the vcpu.
1544 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1546 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1547 if (fd < 0)
1548 kvm_put_kvm(vcpu->kvm);
1549 return fd;
1553 * Creates some virtual cpus. Good luck creating more than one.
1555 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1557 int r;
1558 struct kvm_vcpu *vcpu;
1560 if (!valid_vcpu(n))
1561 return -EINVAL;
1563 vcpu = kvm_arch_vcpu_create(kvm, n);
1564 if (IS_ERR(vcpu))
1565 return PTR_ERR(vcpu);
1567 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1569 r = kvm_arch_vcpu_setup(vcpu);
1570 if (r)
1571 return r;
1573 mutex_lock(&kvm->lock);
1574 if (kvm->vcpus[n]) {
1575 r = -EEXIST;
1576 goto vcpu_destroy;
1578 kvm->vcpus[n] = vcpu;
1579 mutex_unlock(&kvm->lock);
1581 /* Now it's all set up, let userspace reach it */
1582 kvm_get_kvm(kvm);
1583 r = create_vcpu_fd(vcpu);
1584 if (r < 0)
1585 goto unlink;
1586 return r;
1588 unlink:
1589 mutex_lock(&kvm->lock);
1590 kvm->vcpus[n] = NULL;
1591 vcpu_destroy:
1592 mutex_unlock(&kvm->lock);
1593 kvm_arch_vcpu_destroy(vcpu);
1594 return r;
1597 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1599 if (sigset) {
1600 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1601 vcpu->sigset_active = 1;
1602 vcpu->sigset = *sigset;
1603 } else
1604 vcpu->sigset_active = 0;
1605 return 0;
1608 static long kvm_vcpu_ioctl(struct file *filp,
1609 unsigned int ioctl, unsigned long arg)
1611 struct kvm_vcpu *vcpu = filp->private_data;
1612 void __user *argp = (void __user *)arg;
1613 int r;
1614 struct kvm_fpu *fpu = NULL;
1615 struct kvm_sregs *kvm_sregs = NULL;
1617 if (vcpu->kvm->mm != current->mm)
1618 return -EIO;
1619 switch (ioctl) {
1620 case KVM_RUN:
1621 r = -EINVAL;
1622 if (arg)
1623 goto out;
1624 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1625 break;
1626 case KVM_GET_REGS: {
1627 struct kvm_regs *kvm_regs;
1629 r = -ENOMEM;
1630 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1631 if (!kvm_regs)
1632 goto out;
1633 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1634 if (r)
1635 goto out_free1;
1636 r = -EFAULT;
1637 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1638 goto out_free1;
1639 r = 0;
1640 out_free1:
1641 kfree(kvm_regs);
1642 break;
1644 case KVM_SET_REGS: {
1645 struct kvm_regs *kvm_regs;
1647 r = -ENOMEM;
1648 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1649 if (!kvm_regs)
1650 goto out;
1651 r = -EFAULT;
1652 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1653 goto out_free2;
1654 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1655 if (r)
1656 goto out_free2;
1657 r = 0;
1658 out_free2:
1659 kfree(kvm_regs);
1660 break;
1662 case KVM_GET_SREGS: {
1663 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1664 r = -ENOMEM;
1665 if (!kvm_sregs)
1666 goto out;
1667 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1668 if (r)
1669 goto out;
1670 r = -EFAULT;
1671 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1672 goto out;
1673 r = 0;
1674 break;
1676 case KVM_SET_SREGS: {
1677 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1678 r = -ENOMEM;
1679 if (!kvm_sregs)
1680 goto out;
1681 r = -EFAULT;
1682 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1683 goto out;
1684 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1685 if (r)
1686 goto out;
1687 r = 0;
1688 break;
1690 case KVM_GET_MP_STATE: {
1691 struct kvm_mp_state mp_state;
1693 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1694 if (r)
1695 goto out;
1696 r = -EFAULT;
1697 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1698 goto out;
1699 r = 0;
1700 break;
1702 case KVM_SET_MP_STATE: {
1703 struct kvm_mp_state mp_state;
1705 r = -EFAULT;
1706 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1707 goto out;
1708 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1709 if (r)
1710 goto out;
1711 r = 0;
1712 break;
1714 case KVM_TRANSLATE: {
1715 struct kvm_translation tr;
1717 r = -EFAULT;
1718 if (copy_from_user(&tr, argp, sizeof tr))
1719 goto out;
1720 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1721 if (r)
1722 goto out;
1723 r = -EFAULT;
1724 if (copy_to_user(argp, &tr, sizeof tr))
1725 goto out;
1726 r = 0;
1727 break;
1729 case KVM_SET_GUEST_DEBUG: {
1730 struct kvm_guest_debug dbg;
1732 r = -EFAULT;
1733 if (copy_from_user(&dbg, argp, sizeof dbg))
1734 goto out;
1735 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1736 if (r)
1737 goto out;
1738 r = 0;
1739 break;
1741 case KVM_SET_SIGNAL_MASK: {
1742 struct kvm_signal_mask __user *sigmask_arg = argp;
1743 struct kvm_signal_mask kvm_sigmask;
1744 sigset_t sigset, *p;
1746 p = NULL;
1747 if (argp) {
1748 r = -EFAULT;
1749 if (copy_from_user(&kvm_sigmask, argp,
1750 sizeof kvm_sigmask))
1751 goto out;
1752 r = -EINVAL;
1753 if (kvm_sigmask.len != sizeof sigset)
1754 goto out;
1755 r = -EFAULT;
1756 if (copy_from_user(&sigset, sigmask_arg->sigset,
1757 sizeof sigset))
1758 goto out;
1759 p = &sigset;
1761 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1762 break;
1764 case KVM_GET_FPU: {
1765 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1766 r = -ENOMEM;
1767 if (!fpu)
1768 goto out;
1769 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1770 if (r)
1771 goto out;
1772 r = -EFAULT;
1773 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1774 goto out;
1775 r = 0;
1776 break;
1778 case KVM_SET_FPU: {
1779 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1780 r = -ENOMEM;
1781 if (!fpu)
1782 goto out;
1783 r = -EFAULT;
1784 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1785 goto out;
1786 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1787 if (r)
1788 goto out;
1789 r = 0;
1790 break;
1792 default:
1793 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1795 out:
1796 kfree(fpu);
1797 kfree(kvm_sregs);
1798 return r;
1801 static long kvm_vm_ioctl(struct file *filp,
1802 unsigned int ioctl, unsigned long arg)
1804 struct kvm *kvm = filp->private_data;
1805 void __user *argp = (void __user *)arg;
1806 int r;
1808 if (kvm->mm != current->mm)
1809 return -EIO;
1810 switch (ioctl) {
1811 case KVM_CREATE_VCPU:
1812 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1813 if (r < 0)
1814 goto out;
1815 break;
1816 case KVM_SET_USER_MEMORY_REGION: {
1817 struct kvm_userspace_memory_region kvm_userspace_mem;
1819 r = -EFAULT;
1820 if (copy_from_user(&kvm_userspace_mem, argp,
1821 sizeof kvm_userspace_mem))
1822 goto out;
1824 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1825 if (r)
1826 goto out;
1827 break;
1829 case KVM_GET_DIRTY_LOG: {
1830 struct kvm_dirty_log log;
1832 r = -EFAULT;
1833 if (copy_from_user(&log, argp, sizeof log))
1834 goto out;
1835 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1836 if (r)
1837 goto out;
1838 break;
1840 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1841 case KVM_REGISTER_COALESCED_MMIO: {
1842 struct kvm_coalesced_mmio_zone zone;
1843 r = -EFAULT;
1844 if (copy_from_user(&zone, argp, sizeof zone))
1845 goto out;
1846 r = -ENXIO;
1847 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1848 if (r)
1849 goto out;
1850 r = 0;
1851 break;
1853 case KVM_UNREGISTER_COALESCED_MMIO: {
1854 struct kvm_coalesced_mmio_zone zone;
1855 r = -EFAULT;
1856 if (copy_from_user(&zone, argp, sizeof zone))
1857 goto out;
1858 r = -ENXIO;
1859 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1860 if (r)
1861 goto out;
1862 r = 0;
1863 break;
1865 #endif
1866 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1867 case KVM_ASSIGN_PCI_DEVICE: {
1868 struct kvm_assigned_pci_dev assigned_dev;
1870 r = -EFAULT;
1871 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1872 goto out;
1873 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1874 if (r)
1875 goto out;
1876 break;
1878 case KVM_ASSIGN_IRQ: {
1879 struct kvm_assigned_irq assigned_irq;
1881 r = -EFAULT;
1882 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1883 goto out;
1884 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1885 if (r)
1886 goto out;
1887 break;
1889 #endif
1890 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1891 case KVM_DEASSIGN_PCI_DEVICE: {
1892 struct kvm_assigned_pci_dev assigned_dev;
1894 r = -EFAULT;
1895 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1896 goto out;
1897 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
1898 if (r)
1899 goto out;
1900 break;
1902 #endif
1903 #ifdef KVM_CAP_IRQ_ROUTING
1904 case KVM_SET_GSI_ROUTING: {
1905 struct kvm_irq_routing routing;
1906 struct kvm_irq_routing __user *urouting;
1907 struct kvm_irq_routing_entry *entries;
1909 r = -EFAULT;
1910 if (copy_from_user(&routing, argp, sizeof(routing)))
1911 goto out;
1912 r = -EINVAL;
1913 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
1914 goto out;
1915 if (routing.flags)
1916 goto out;
1917 r = -ENOMEM;
1918 entries = vmalloc(routing.nr * sizeof(*entries));
1919 if (!entries)
1920 goto out;
1921 r = -EFAULT;
1922 urouting = argp;
1923 if (copy_from_user(entries, urouting->entries,
1924 routing.nr * sizeof(*entries)))
1925 goto out_free_irq_routing;
1926 r = kvm_set_irq_routing(kvm, entries, routing.nr,
1927 routing.flags);
1928 out_free_irq_routing:
1929 vfree(entries);
1930 break;
1932 #endif
1933 default:
1934 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1936 out:
1937 return r;
1940 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1942 struct page *page[1];
1943 unsigned long addr;
1944 int npages;
1945 gfn_t gfn = vmf->pgoff;
1946 struct kvm *kvm = vma->vm_file->private_data;
1948 addr = gfn_to_hva(kvm, gfn);
1949 if (kvm_is_error_hva(addr))
1950 return VM_FAULT_SIGBUS;
1952 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1953 NULL);
1954 if (unlikely(npages != 1))
1955 return VM_FAULT_SIGBUS;
1957 vmf->page = page[0];
1958 return 0;
1961 static struct vm_operations_struct kvm_vm_vm_ops = {
1962 .fault = kvm_vm_fault,
1965 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1967 vma->vm_ops = &kvm_vm_vm_ops;
1968 return 0;
1971 static struct file_operations kvm_vm_fops = {
1972 .release = kvm_vm_release,
1973 .unlocked_ioctl = kvm_vm_ioctl,
1974 .compat_ioctl = kvm_vm_ioctl,
1975 .mmap = kvm_vm_mmap,
1978 static int kvm_dev_ioctl_create_vm(void)
1980 int fd;
1981 struct kvm *kvm;
1983 kvm = kvm_create_vm();
1984 if (IS_ERR(kvm))
1985 return PTR_ERR(kvm);
1986 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1987 if (fd < 0)
1988 kvm_put_kvm(kvm);
1990 return fd;
1993 static long kvm_dev_ioctl_check_extension_generic(long arg)
1995 switch (arg) {
1996 case KVM_CAP_USER_MEMORY:
1997 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
1998 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
1999 return 1;
2000 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2001 case KVM_CAP_IRQ_ROUTING:
2002 return KVM_MAX_IRQ_ROUTES;
2003 #endif
2004 default:
2005 break;
2007 return kvm_dev_ioctl_check_extension(arg);
2010 static long kvm_dev_ioctl(struct file *filp,
2011 unsigned int ioctl, unsigned long arg)
2013 long r = -EINVAL;
2015 switch (ioctl) {
2016 case KVM_GET_API_VERSION:
2017 r = -EINVAL;
2018 if (arg)
2019 goto out;
2020 r = KVM_API_VERSION;
2021 break;
2022 case KVM_CREATE_VM:
2023 r = -EINVAL;
2024 if (arg)
2025 goto out;
2026 r = kvm_dev_ioctl_create_vm();
2027 break;
2028 case KVM_CHECK_EXTENSION:
2029 r = kvm_dev_ioctl_check_extension_generic(arg);
2030 break;
2031 case KVM_GET_VCPU_MMAP_SIZE:
2032 r = -EINVAL;
2033 if (arg)
2034 goto out;
2035 r = PAGE_SIZE; /* struct kvm_run */
2036 #ifdef CONFIG_X86
2037 r += PAGE_SIZE; /* pio data page */
2038 #endif
2039 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2040 r += PAGE_SIZE; /* coalesced mmio ring page */
2041 #endif
2042 break;
2043 case KVM_TRACE_ENABLE:
2044 case KVM_TRACE_PAUSE:
2045 case KVM_TRACE_DISABLE:
2046 r = kvm_trace_ioctl(ioctl, arg);
2047 break;
2048 default:
2049 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2051 out:
2052 return r;
2055 static struct file_operations kvm_chardev_ops = {
2056 .unlocked_ioctl = kvm_dev_ioctl,
2057 .compat_ioctl = kvm_dev_ioctl,
2060 static struct miscdevice kvm_dev = {
2061 KVM_MINOR,
2062 "kvm",
2063 &kvm_chardev_ops,
2066 static void hardware_enable(void *junk)
2068 int cpu = raw_smp_processor_id();
2070 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2071 return;
2072 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2073 kvm_arch_hardware_enable(NULL);
2076 static void hardware_disable(void *junk)
2078 int cpu = raw_smp_processor_id();
2080 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2081 return;
2082 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2083 kvm_arch_hardware_disable(NULL);
2086 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2087 void *v)
2089 int cpu = (long)v;
2091 val &= ~CPU_TASKS_FROZEN;
2092 switch (val) {
2093 case CPU_DYING:
2094 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2095 cpu);
2096 hardware_disable(NULL);
2097 break;
2098 case CPU_UP_CANCELED:
2099 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2100 cpu);
2101 smp_call_function_single(cpu, hardware_disable, NULL, 1);
2102 break;
2103 case CPU_ONLINE:
2104 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2105 cpu);
2106 smp_call_function_single(cpu, hardware_enable, NULL, 1);
2107 break;
2109 return NOTIFY_OK;
2113 asmlinkage void kvm_handle_fault_on_reboot(void)
2115 if (kvm_rebooting)
2116 /* spin while reset goes on */
2117 while (true)
2119 /* Fault while not rebooting. We want the trace. */
2120 BUG();
2122 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2124 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2125 void *v)
2127 if (val == SYS_RESTART) {
2129 * Some (well, at least mine) BIOSes hang on reboot if
2130 * in vmx root mode.
2132 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2133 kvm_rebooting = true;
2134 on_each_cpu(hardware_disable, NULL, 1);
2136 return NOTIFY_OK;
2139 static struct notifier_block kvm_reboot_notifier = {
2140 .notifier_call = kvm_reboot,
2141 .priority = 0,
2144 void kvm_io_bus_init(struct kvm_io_bus *bus)
2146 memset(bus, 0, sizeof(*bus));
2149 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2151 int i;
2153 for (i = 0; i < bus->dev_count; i++) {
2154 struct kvm_io_device *pos = bus->devs[i];
2156 kvm_iodevice_destructor(pos);
2160 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2161 gpa_t addr, int len, int is_write)
2163 int i;
2165 for (i = 0; i < bus->dev_count; i++) {
2166 struct kvm_io_device *pos = bus->devs[i];
2168 if (pos->in_range(pos, addr, len, is_write))
2169 return pos;
2172 return NULL;
2175 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2177 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2179 bus->devs[bus->dev_count++] = dev;
2182 static struct notifier_block kvm_cpu_notifier = {
2183 .notifier_call = kvm_cpu_hotplug,
2184 .priority = 20, /* must be > scheduler priority */
2187 static int vm_stat_get(void *_offset, u64 *val)
2189 unsigned offset = (long)_offset;
2190 struct kvm *kvm;
2192 *val = 0;
2193 spin_lock(&kvm_lock);
2194 list_for_each_entry(kvm, &vm_list, vm_list)
2195 *val += *(u32 *)((void *)kvm + offset);
2196 spin_unlock(&kvm_lock);
2197 return 0;
2200 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2202 static int vcpu_stat_get(void *_offset, u64 *val)
2204 unsigned offset = (long)_offset;
2205 struct kvm *kvm;
2206 struct kvm_vcpu *vcpu;
2207 int i;
2209 *val = 0;
2210 spin_lock(&kvm_lock);
2211 list_for_each_entry(kvm, &vm_list, vm_list)
2212 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2213 vcpu = kvm->vcpus[i];
2214 if (vcpu)
2215 *val += *(u32 *)((void *)vcpu + offset);
2217 spin_unlock(&kvm_lock);
2218 return 0;
2221 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2223 static struct file_operations *stat_fops[] = {
2224 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2225 [KVM_STAT_VM] = &vm_stat_fops,
2228 static void kvm_init_debug(void)
2230 struct kvm_stats_debugfs_item *p;
2232 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2233 for (p = debugfs_entries; p->name; ++p)
2234 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2235 (void *)(long)p->offset,
2236 stat_fops[p->kind]);
2239 static void kvm_exit_debug(void)
2241 struct kvm_stats_debugfs_item *p;
2243 for (p = debugfs_entries; p->name; ++p)
2244 debugfs_remove(p->dentry);
2245 debugfs_remove(kvm_debugfs_dir);
2248 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2250 hardware_disable(NULL);
2251 return 0;
2254 static int kvm_resume(struct sys_device *dev)
2256 hardware_enable(NULL);
2257 return 0;
2260 static struct sysdev_class kvm_sysdev_class = {
2261 .name = "kvm",
2262 .suspend = kvm_suspend,
2263 .resume = kvm_resume,
2266 static struct sys_device kvm_sysdev = {
2267 .id = 0,
2268 .cls = &kvm_sysdev_class,
2271 struct page *bad_page;
2272 pfn_t bad_pfn;
2274 static inline
2275 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2277 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2280 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2282 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2284 kvm_arch_vcpu_load(vcpu, cpu);
2287 static void kvm_sched_out(struct preempt_notifier *pn,
2288 struct task_struct *next)
2290 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2292 kvm_arch_vcpu_put(vcpu);
2295 int kvm_init(void *opaque, unsigned int vcpu_size,
2296 struct module *module)
2298 int r;
2299 int cpu;
2301 kvm_init_debug();
2303 r = kvm_arch_init(opaque);
2304 if (r)
2305 goto out_fail;
2307 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2309 if (bad_page == NULL) {
2310 r = -ENOMEM;
2311 goto out;
2314 bad_pfn = page_to_pfn(bad_page);
2316 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2317 r = -ENOMEM;
2318 goto out_free_0;
2320 cpumask_clear(cpus_hardware_enabled);
2322 r = kvm_arch_hardware_setup();
2323 if (r < 0)
2324 goto out_free_0a;
2326 for_each_online_cpu(cpu) {
2327 smp_call_function_single(cpu,
2328 kvm_arch_check_processor_compat,
2329 &r, 1);
2330 if (r < 0)
2331 goto out_free_1;
2334 on_each_cpu(hardware_enable, NULL, 1);
2335 r = register_cpu_notifier(&kvm_cpu_notifier);
2336 if (r)
2337 goto out_free_2;
2338 register_reboot_notifier(&kvm_reboot_notifier);
2340 r = sysdev_class_register(&kvm_sysdev_class);
2341 if (r)
2342 goto out_free_3;
2344 r = sysdev_register(&kvm_sysdev);
2345 if (r)
2346 goto out_free_4;
2348 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2349 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2350 __alignof__(struct kvm_vcpu),
2351 0, NULL);
2352 if (!kvm_vcpu_cache) {
2353 r = -ENOMEM;
2354 goto out_free_5;
2357 kvm_chardev_ops.owner = module;
2358 kvm_vm_fops.owner = module;
2359 kvm_vcpu_fops.owner = module;
2361 r = misc_register(&kvm_dev);
2362 if (r) {
2363 printk(KERN_ERR "kvm: misc device register failed\n");
2364 goto out_free;
2367 kvm_preempt_ops.sched_in = kvm_sched_in;
2368 kvm_preempt_ops.sched_out = kvm_sched_out;
2369 #ifndef CONFIG_X86
2370 msi2intx = 0;
2371 #endif
2373 return 0;
2375 out_free:
2376 kmem_cache_destroy(kvm_vcpu_cache);
2377 out_free_5:
2378 sysdev_unregister(&kvm_sysdev);
2379 out_free_4:
2380 sysdev_class_unregister(&kvm_sysdev_class);
2381 out_free_3:
2382 unregister_reboot_notifier(&kvm_reboot_notifier);
2383 unregister_cpu_notifier(&kvm_cpu_notifier);
2384 out_free_2:
2385 on_each_cpu(hardware_disable, NULL, 1);
2386 out_free_1:
2387 kvm_arch_hardware_unsetup();
2388 out_free_0a:
2389 free_cpumask_var(cpus_hardware_enabled);
2390 out_free_0:
2391 __free_page(bad_page);
2392 out:
2393 kvm_arch_exit();
2394 kvm_exit_debug();
2395 out_fail:
2396 return r;
2398 EXPORT_SYMBOL_GPL(kvm_init);
2400 void kvm_exit(void)
2402 kvm_trace_cleanup();
2403 misc_deregister(&kvm_dev);
2404 kmem_cache_destroy(kvm_vcpu_cache);
2405 sysdev_unregister(&kvm_sysdev);
2406 sysdev_class_unregister(&kvm_sysdev_class);
2407 unregister_reboot_notifier(&kvm_reboot_notifier);
2408 unregister_cpu_notifier(&kvm_cpu_notifier);
2409 on_each_cpu(hardware_disable, NULL, 1);
2410 kvm_arch_hardware_unsetup();
2411 kvm_arch_exit();
2412 kvm_exit_debug();
2413 free_cpumask_var(cpus_hardware_enabled);
2414 __free_page(bad_page);
2416 EXPORT_SYMBOL_GPL(kvm_exit);