USB: otg: adding nop usb transceiver
[linux-2.6/mini2440.git] / virt / kvm / kvm_main.c
blob29a667ce35b0ebc1840f1fcbb6ae3cf3313fa3b8
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 CONFIG_X86
51 #include <asm/msidef.h>
52 #endif
54 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
55 #include "coalesced_mmio.h"
56 #endif
58 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
59 #include <linux/pci.h>
60 #include <linux/interrupt.h>
61 #include "irq.h"
62 #endif
64 MODULE_AUTHOR("Qumranet");
65 MODULE_LICENSE("GPL");
67 static int msi2intx = 1;
68 module_param(msi2intx, bool, 0);
70 DEFINE_SPINLOCK(kvm_lock);
71 LIST_HEAD(vm_list);
73 static cpumask_var_t cpus_hardware_enabled;
75 struct kmem_cache *kvm_vcpu_cache;
76 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
78 static __read_mostly struct preempt_ops kvm_preempt_ops;
80 struct dentry *kvm_debugfs_dir;
82 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
83 unsigned long arg);
85 static bool kvm_rebooting;
87 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
89 #ifdef CONFIG_X86
90 static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev)
92 int vcpu_id;
93 struct kvm_vcpu *vcpu;
94 struct kvm_ioapic *ioapic = ioapic_irqchip(dev->kvm);
95 int dest_id = (dev->guest_msi.address_lo & MSI_ADDR_DEST_ID_MASK)
96 >> MSI_ADDR_DEST_ID_SHIFT;
97 int vector = (dev->guest_msi.data & MSI_DATA_VECTOR_MASK)
98 >> MSI_DATA_VECTOR_SHIFT;
99 int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
100 (unsigned long *)&dev->guest_msi.address_lo);
101 int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
102 (unsigned long *)&dev->guest_msi.data);
103 int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
104 (unsigned long *)&dev->guest_msi.data);
105 u32 deliver_bitmask;
107 BUG_ON(!ioapic);
109 deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
110 dest_id, dest_mode);
111 /* IOAPIC delivery mode value is the same as MSI here */
112 switch (delivery_mode) {
113 case IOAPIC_LOWEST_PRIORITY:
114 vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
115 deliver_bitmask);
116 if (vcpu != NULL)
117 kvm_apic_set_irq(vcpu, vector, trig_mode);
118 else
119 printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
120 break;
121 case IOAPIC_FIXED:
122 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
123 if (!(deliver_bitmask & (1 << vcpu_id)))
124 continue;
125 deliver_bitmask &= ~(1 << vcpu_id);
126 vcpu = ioapic->kvm->vcpus[vcpu_id];
127 if (vcpu)
128 kvm_apic_set_irq(vcpu, vector, trig_mode);
130 break;
131 default:
132 printk(KERN_INFO "kvm: unsupported MSI delivery mode\n");
135 #else
136 static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev) {}
137 #endif
139 static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
140 int assigned_dev_id)
142 struct list_head *ptr;
143 struct kvm_assigned_dev_kernel *match;
145 list_for_each(ptr, head) {
146 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
147 if (match->assigned_dev_id == assigned_dev_id)
148 return match;
150 return NULL;
153 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
155 struct kvm_assigned_dev_kernel *assigned_dev;
157 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
158 interrupt_work);
160 /* This is taken to safely inject irq inside the guest. When
161 * the interrupt injection (or the ioapic code) uses a
162 * finer-grained lock, update this
164 mutex_lock(&assigned_dev->kvm->lock);
165 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_INTX)
166 kvm_set_irq(assigned_dev->kvm,
167 assigned_dev->irq_source_id,
168 assigned_dev->guest_irq, 1);
169 else if (assigned_dev->irq_requested_type &
170 KVM_ASSIGNED_DEV_GUEST_MSI) {
171 assigned_device_msi_dispatch(assigned_dev);
172 enable_irq(assigned_dev->host_irq);
173 assigned_dev->host_irq_disabled = false;
175 mutex_unlock(&assigned_dev->kvm->lock);
178 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
180 struct kvm_assigned_dev_kernel *assigned_dev =
181 (struct kvm_assigned_dev_kernel *) dev_id;
183 schedule_work(&assigned_dev->interrupt_work);
185 disable_irq_nosync(irq);
186 assigned_dev->host_irq_disabled = true;
188 return IRQ_HANDLED;
191 /* Ack the irq line for an assigned device */
192 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
194 struct kvm_assigned_dev_kernel *dev;
196 if (kian->gsi == -1)
197 return;
199 dev = container_of(kian, struct kvm_assigned_dev_kernel,
200 ack_notifier);
202 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
204 /* The guest irq may be shared so this ack may be
205 * from another device.
207 if (dev->host_irq_disabled) {
208 enable_irq(dev->host_irq);
209 dev->host_irq_disabled = false;
213 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
214 static void kvm_free_assigned_irq(struct kvm *kvm,
215 struct kvm_assigned_dev_kernel *assigned_dev)
217 if (!irqchip_in_kernel(kvm))
218 return;
220 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
222 if (assigned_dev->irq_source_id != -1)
223 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
224 assigned_dev->irq_source_id = -1;
226 if (!assigned_dev->irq_requested_type)
227 return;
230 * In kvm_free_device_irq, cancel_work_sync return true if:
231 * 1. work is scheduled, and then cancelled.
232 * 2. work callback is executed.
234 * The first one ensured that the irq is disabled and no more events
235 * would happen. But for the second one, the irq may be enabled (e.g.
236 * for MSI). So we disable irq here to prevent further events.
238 * Notice this maybe result in nested disable if the interrupt type is
239 * INTx, but it's OK for we are going to free it.
241 * If this function is a part of VM destroy, please ensure that till
242 * now, the kvm state is still legal for probably we also have to wait
243 * interrupt_work done.
245 disable_irq_nosync(assigned_dev->host_irq);
246 cancel_work_sync(&assigned_dev->interrupt_work);
248 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
250 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
251 pci_disable_msi(assigned_dev->dev);
253 assigned_dev->irq_requested_type = 0;
257 static void kvm_free_assigned_device(struct kvm *kvm,
258 struct kvm_assigned_dev_kernel
259 *assigned_dev)
261 kvm_free_assigned_irq(kvm, assigned_dev);
263 pci_reset_function(assigned_dev->dev);
265 pci_release_regions(assigned_dev->dev);
266 pci_disable_device(assigned_dev->dev);
267 pci_dev_put(assigned_dev->dev);
269 list_del(&assigned_dev->list);
270 kfree(assigned_dev);
273 void kvm_free_all_assigned_devices(struct kvm *kvm)
275 struct list_head *ptr, *ptr2;
276 struct kvm_assigned_dev_kernel *assigned_dev;
278 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
279 assigned_dev = list_entry(ptr,
280 struct kvm_assigned_dev_kernel,
281 list);
283 kvm_free_assigned_device(kvm, assigned_dev);
287 static int assigned_device_update_intx(struct kvm *kvm,
288 struct kvm_assigned_dev_kernel *adev,
289 struct kvm_assigned_irq *airq)
291 adev->guest_irq = airq->guest_irq;
292 adev->ack_notifier.gsi = airq->guest_irq;
294 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
295 return 0;
297 if (irqchip_in_kernel(kvm)) {
298 if (!msi2intx &&
299 (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
300 free_irq(adev->host_irq, (void *)adev);
301 pci_disable_msi(adev->dev);
304 if (!capable(CAP_SYS_RAWIO))
305 return -EPERM;
307 if (airq->host_irq)
308 adev->host_irq = airq->host_irq;
309 else
310 adev->host_irq = adev->dev->irq;
312 /* Even though this is PCI, we don't want to use shared
313 * interrupts. Sharing host devices with guest-assigned devices
314 * on the same interrupt line is not a happy situation: there
315 * are going to be long delays in accepting, acking, etc.
317 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
318 0, "kvm_assigned_intx_device", (void *)adev))
319 return -EIO;
322 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
323 KVM_ASSIGNED_DEV_HOST_INTX;
324 return 0;
327 #ifdef CONFIG_X86
328 static int assigned_device_update_msi(struct kvm *kvm,
329 struct kvm_assigned_dev_kernel *adev,
330 struct kvm_assigned_irq *airq)
332 int r;
334 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
335 /* x86 don't care upper address of guest msi message addr */
336 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
337 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
338 adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
339 adev->guest_msi.data = airq->guest_msi.data;
340 adev->ack_notifier.gsi = -1;
341 } else if (msi2intx) {
342 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
343 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
344 adev->guest_irq = airq->guest_irq;
345 adev->ack_notifier.gsi = airq->guest_irq;
348 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
349 return 0;
351 if (irqchip_in_kernel(kvm)) {
352 if (!msi2intx) {
353 if (adev->irq_requested_type &
354 KVM_ASSIGNED_DEV_HOST_INTX)
355 free_irq(adev->host_irq, (void *)adev);
357 r = pci_enable_msi(adev->dev);
358 if (r)
359 return r;
362 adev->host_irq = adev->dev->irq;
363 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
364 "kvm_assigned_msi_device", (void *)adev))
365 return -EIO;
368 if (!msi2intx)
369 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
371 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
372 return 0;
374 #endif
376 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
377 struct kvm_assigned_irq
378 *assigned_irq)
380 int r = 0;
381 struct kvm_assigned_dev_kernel *match;
383 mutex_lock(&kvm->lock);
385 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
386 assigned_irq->assigned_dev_id);
387 if (!match) {
388 mutex_unlock(&kvm->lock);
389 return -EINVAL;
392 if (!match->irq_requested_type) {
393 INIT_WORK(&match->interrupt_work,
394 kvm_assigned_dev_interrupt_work_handler);
395 if (irqchip_in_kernel(kvm)) {
396 /* Register ack nofitier */
397 match->ack_notifier.gsi = -1;
398 match->ack_notifier.irq_acked =
399 kvm_assigned_dev_ack_irq;
400 kvm_register_irq_ack_notifier(kvm,
401 &match->ack_notifier);
403 /* Request IRQ source ID */
404 r = kvm_request_irq_source_id(kvm);
405 if (r < 0)
406 goto out_release;
407 else
408 match->irq_source_id = r;
410 #ifdef CONFIG_X86
411 /* Determine host device irq type, we can know the
412 * result from dev->msi_enabled */
413 if (msi2intx)
414 pci_enable_msi(match->dev);
415 #endif
419 if ((!msi2intx &&
420 (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI)) ||
421 (msi2intx && match->dev->msi_enabled)) {
422 #ifdef CONFIG_X86
423 r = assigned_device_update_msi(kvm, match, assigned_irq);
424 if (r) {
425 printk(KERN_WARNING "kvm: failed to enable "
426 "MSI device!\n");
427 goto out_release;
429 #else
430 r = -ENOTTY;
431 #endif
432 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
433 /* Host device IRQ 0 means don't support INTx */
434 if (!msi2intx) {
435 printk(KERN_WARNING
436 "kvm: wait device to enable MSI!\n");
437 r = 0;
438 } else {
439 printk(KERN_WARNING
440 "kvm: failed to enable MSI device!\n");
441 r = -ENOTTY;
442 goto out_release;
444 } else {
445 /* Non-sharing INTx mode */
446 r = assigned_device_update_intx(kvm, match, assigned_irq);
447 if (r) {
448 printk(KERN_WARNING "kvm: failed to enable "
449 "INTx device!\n");
450 goto out_release;
454 mutex_unlock(&kvm->lock);
455 return r;
456 out_release:
457 mutex_unlock(&kvm->lock);
458 kvm_free_assigned_device(kvm, match);
459 return r;
462 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
463 struct kvm_assigned_pci_dev *assigned_dev)
465 int r = 0;
466 struct kvm_assigned_dev_kernel *match;
467 struct pci_dev *dev;
469 down_read(&kvm->slots_lock);
470 mutex_lock(&kvm->lock);
472 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
473 assigned_dev->assigned_dev_id);
474 if (match) {
475 /* device already assigned */
476 r = -EINVAL;
477 goto out;
480 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
481 if (match == NULL) {
482 printk(KERN_INFO "%s: Couldn't allocate memory\n",
483 __func__);
484 r = -ENOMEM;
485 goto out;
487 dev = pci_get_bus_and_slot(assigned_dev->busnr,
488 assigned_dev->devfn);
489 if (!dev) {
490 printk(KERN_INFO "%s: host device not found\n", __func__);
491 r = -EINVAL;
492 goto out_free;
494 if (pci_enable_device(dev)) {
495 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
496 r = -EBUSY;
497 goto out_put;
499 r = pci_request_regions(dev, "kvm_assigned_device");
500 if (r) {
501 printk(KERN_INFO "%s: Could not get access to device regions\n",
502 __func__);
503 goto out_disable;
506 pci_reset_function(dev);
508 match->assigned_dev_id = assigned_dev->assigned_dev_id;
509 match->host_busnr = assigned_dev->busnr;
510 match->host_devfn = assigned_dev->devfn;
511 match->flags = assigned_dev->flags;
512 match->dev = dev;
513 match->irq_source_id = -1;
514 match->kvm = kvm;
516 list_add(&match->list, &kvm->arch.assigned_dev_head);
518 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
519 if (!kvm->arch.iommu_domain) {
520 r = kvm_iommu_map_guest(kvm);
521 if (r)
522 goto out_list_del;
524 r = kvm_assign_device(kvm, match);
525 if (r)
526 goto out_list_del;
529 out:
530 mutex_unlock(&kvm->lock);
531 up_read(&kvm->slots_lock);
532 return r;
533 out_list_del:
534 list_del(&match->list);
535 pci_release_regions(dev);
536 out_disable:
537 pci_disable_device(dev);
538 out_put:
539 pci_dev_put(dev);
540 out_free:
541 kfree(match);
542 mutex_unlock(&kvm->lock);
543 up_read(&kvm->slots_lock);
544 return r;
546 #endif
548 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
549 static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
550 struct kvm_assigned_pci_dev *assigned_dev)
552 int r = 0;
553 struct kvm_assigned_dev_kernel *match;
555 mutex_lock(&kvm->lock);
557 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
558 assigned_dev->assigned_dev_id);
559 if (!match) {
560 printk(KERN_INFO "%s: device hasn't been assigned before, "
561 "so cannot be deassigned\n", __func__);
562 r = -EINVAL;
563 goto out;
566 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
567 kvm_deassign_device(kvm, match);
569 kvm_free_assigned_device(kvm, match);
571 out:
572 mutex_unlock(&kvm->lock);
573 return r;
575 #endif
577 static inline int valid_vcpu(int n)
579 return likely(n >= 0 && n < KVM_MAX_VCPUS);
582 inline int kvm_is_mmio_pfn(pfn_t pfn)
584 if (pfn_valid(pfn))
585 return PageReserved(pfn_to_page(pfn));
587 return true;
591 * Switches to specified vcpu, until a matching vcpu_put()
593 void vcpu_load(struct kvm_vcpu *vcpu)
595 int cpu;
597 mutex_lock(&vcpu->mutex);
598 cpu = get_cpu();
599 preempt_notifier_register(&vcpu->preempt_notifier);
600 kvm_arch_vcpu_load(vcpu, cpu);
601 put_cpu();
604 void vcpu_put(struct kvm_vcpu *vcpu)
606 preempt_disable();
607 kvm_arch_vcpu_put(vcpu);
608 preempt_notifier_unregister(&vcpu->preempt_notifier);
609 preempt_enable();
610 mutex_unlock(&vcpu->mutex);
613 static void ack_flush(void *_completed)
617 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
619 int i, cpu, me;
620 cpumask_var_t cpus;
621 bool called = true;
622 struct kvm_vcpu *vcpu;
624 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
625 cpumask_clear(cpus);
627 me = get_cpu();
628 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
629 vcpu = kvm->vcpus[i];
630 if (!vcpu)
631 continue;
632 if (test_and_set_bit(req, &vcpu->requests))
633 continue;
634 cpu = vcpu->cpu;
635 if (cpus != NULL && cpu != -1 && cpu != me)
636 cpumask_set_cpu(cpu, cpus);
638 if (unlikely(cpus == NULL))
639 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
640 else if (!cpumask_empty(cpus))
641 smp_call_function_many(cpus, ack_flush, NULL, 1);
642 else
643 called = false;
644 put_cpu();
645 free_cpumask_var(cpus);
646 return called;
649 void kvm_flush_remote_tlbs(struct kvm *kvm)
651 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
652 ++kvm->stat.remote_tlb_flush;
655 void kvm_reload_remote_mmus(struct kvm *kvm)
657 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
660 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
662 struct page *page;
663 int r;
665 mutex_init(&vcpu->mutex);
666 vcpu->cpu = -1;
667 vcpu->kvm = kvm;
668 vcpu->vcpu_id = id;
669 init_waitqueue_head(&vcpu->wq);
671 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
672 if (!page) {
673 r = -ENOMEM;
674 goto fail;
676 vcpu->run = page_address(page);
678 r = kvm_arch_vcpu_init(vcpu);
679 if (r < 0)
680 goto fail_free_run;
681 return 0;
683 fail_free_run:
684 free_page((unsigned long)vcpu->run);
685 fail:
686 return r;
688 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
690 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
692 kvm_arch_vcpu_uninit(vcpu);
693 free_page((unsigned long)vcpu->run);
695 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
697 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
698 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
700 return container_of(mn, struct kvm, mmu_notifier);
703 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
704 struct mm_struct *mm,
705 unsigned long address)
707 struct kvm *kvm = mmu_notifier_to_kvm(mn);
708 int need_tlb_flush;
711 * When ->invalidate_page runs, the linux pte has been zapped
712 * already but the page is still allocated until
713 * ->invalidate_page returns. So if we increase the sequence
714 * here the kvm page fault will notice if the spte can't be
715 * established because the page is going to be freed. If
716 * instead the kvm page fault establishes the spte before
717 * ->invalidate_page runs, kvm_unmap_hva will release it
718 * before returning.
720 * The sequence increase only need to be seen at spin_unlock
721 * time, and not at spin_lock time.
723 * Increasing the sequence after the spin_unlock would be
724 * unsafe because the kvm page fault could then establish the
725 * pte after kvm_unmap_hva returned, without noticing the page
726 * is going to be freed.
728 spin_lock(&kvm->mmu_lock);
729 kvm->mmu_notifier_seq++;
730 need_tlb_flush = kvm_unmap_hva(kvm, address);
731 spin_unlock(&kvm->mmu_lock);
733 /* we've to flush the tlb before the pages can be freed */
734 if (need_tlb_flush)
735 kvm_flush_remote_tlbs(kvm);
739 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
740 struct mm_struct *mm,
741 unsigned long start,
742 unsigned long end)
744 struct kvm *kvm = mmu_notifier_to_kvm(mn);
745 int need_tlb_flush = 0;
747 spin_lock(&kvm->mmu_lock);
749 * The count increase must become visible at unlock time as no
750 * spte can be established without taking the mmu_lock and
751 * count is also read inside the mmu_lock critical section.
753 kvm->mmu_notifier_count++;
754 for (; start < end; start += PAGE_SIZE)
755 need_tlb_flush |= kvm_unmap_hva(kvm, start);
756 spin_unlock(&kvm->mmu_lock);
758 /* we've to flush the tlb before the pages can be freed */
759 if (need_tlb_flush)
760 kvm_flush_remote_tlbs(kvm);
763 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
764 struct mm_struct *mm,
765 unsigned long start,
766 unsigned long end)
768 struct kvm *kvm = mmu_notifier_to_kvm(mn);
770 spin_lock(&kvm->mmu_lock);
772 * This sequence increase will notify the kvm page fault that
773 * the page that is going to be mapped in the spte could have
774 * been freed.
776 kvm->mmu_notifier_seq++;
778 * The above sequence increase must be visible before the
779 * below count decrease but both values are read by the kvm
780 * page fault under mmu_lock spinlock so we don't need to add
781 * a smb_wmb() here in between the two.
783 kvm->mmu_notifier_count--;
784 spin_unlock(&kvm->mmu_lock);
786 BUG_ON(kvm->mmu_notifier_count < 0);
789 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
790 struct mm_struct *mm,
791 unsigned long address)
793 struct kvm *kvm = mmu_notifier_to_kvm(mn);
794 int young;
796 spin_lock(&kvm->mmu_lock);
797 young = kvm_age_hva(kvm, address);
798 spin_unlock(&kvm->mmu_lock);
800 if (young)
801 kvm_flush_remote_tlbs(kvm);
803 return young;
806 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
807 struct mm_struct *mm)
809 struct kvm *kvm = mmu_notifier_to_kvm(mn);
810 kvm_arch_flush_shadow(kvm);
813 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
814 .invalidate_page = kvm_mmu_notifier_invalidate_page,
815 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
816 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
817 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
818 .release = kvm_mmu_notifier_release,
820 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
822 static struct kvm *kvm_create_vm(void)
824 struct kvm *kvm = kvm_arch_create_vm();
825 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
826 struct page *page;
827 #endif
829 if (IS_ERR(kvm))
830 goto out;
832 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
833 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
834 if (!page) {
835 kfree(kvm);
836 return ERR_PTR(-ENOMEM);
838 kvm->coalesced_mmio_ring =
839 (struct kvm_coalesced_mmio_ring *)page_address(page);
840 #endif
842 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
844 int err;
845 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
846 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
847 if (err) {
848 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
849 put_page(page);
850 #endif
851 kfree(kvm);
852 return ERR_PTR(err);
855 #endif
857 kvm->mm = current->mm;
858 atomic_inc(&kvm->mm->mm_count);
859 spin_lock_init(&kvm->mmu_lock);
860 kvm_io_bus_init(&kvm->pio_bus);
861 mutex_init(&kvm->lock);
862 kvm_io_bus_init(&kvm->mmio_bus);
863 init_rwsem(&kvm->slots_lock);
864 atomic_set(&kvm->users_count, 1);
865 spin_lock(&kvm_lock);
866 list_add(&kvm->vm_list, &vm_list);
867 spin_unlock(&kvm_lock);
868 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
869 kvm_coalesced_mmio_init(kvm);
870 #endif
871 out:
872 return kvm;
876 * Free any memory in @free but not in @dont.
878 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
879 struct kvm_memory_slot *dont)
881 if (!dont || free->rmap != dont->rmap)
882 vfree(free->rmap);
884 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
885 vfree(free->dirty_bitmap);
887 if (!dont || free->lpage_info != dont->lpage_info)
888 vfree(free->lpage_info);
890 free->npages = 0;
891 free->dirty_bitmap = NULL;
892 free->rmap = NULL;
893 free->lpage_info = NULL;
896 void kvm_free_physmem(struct kvm *kvm)
898 int i;
900 for (i = 0; i < kvm->nmemslots; ++i)
901 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
904 static void kvm_destroy_vm(struct kvm *kvm)
906 struct mm_struct *mm = kvm->mm;
908 kvm_arch_sync_events(kvm);
909 spin_lock(&kvm_lock);
910 list_del(&kvm->vm_list);
911 spin_unlock(&kvm_lock);
912 kvm_io_bus_destroy(&kvm->pio_bus);
913 kvm_io_bus_destroy(&kvm->mmio_bus);
914 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
915 if (kvm->coalesced_mmio_ring != NULL)
916 free_page((unsigned long)kvm->coalesced_mmio_ring);
917 #endif
918 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
919 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
920 #endif
921 kvm_arch_destroy_vm(kvm);
922 mmdrop(mm);
925 void kvm_get_kvm(struct kvm *kvm)
927 atomic_inc(&kvm->users_count);
929 EXPORT_SYMBOL_GPL(kvm_get_kvm);
931 void kvm_put_kvm(struct kvm *kvm)
933 if (atomic_dec_and_test(&kvm->users_count))
934 kvm_destroy_vm(kvm);
936 EXPORT_SYMBOL_GPL(kvm_put_kvm);
939 static int kvm_vm_release(struct inode *inode, struct file *filp)
941 struct kvm *kvm = filp->private_data;
943 kvm_put_kvm(kvm);
944 return 0;
948 * Allocate some memory and give it an address in the guest physical address
949 * space.
951 * Discontiguous memory is allowed, mostly for framebuffers.
953 * Must be called holding mmap_sem for write.
955 int __kvm_set_memory_region(struct kvm *kvm,
956 struct kvm_userspace_memory_region *mem,
957 int user_alloc)
959 int r;
960 gfn_t base_gfn;
961 unsigned long npages;
962 unsigned long i;
963 struct kvm_memory_slot *memslot;
964 struct kvm_memory_slot old, new;
966 r = -EINVAL;
967 /* General sanity checks */
968 if (mem->memory_size & (PAGE_SIZE - 1))
969 goto out;
970 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
971 goto out;
972 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
973 goto out;
974 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
975 goto out;
976 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
977 goto out;
979 memslot = &kvm->memslots[mem->slot];
980 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
981 npages = mem->memory_size >> PAGE_SHIFT;
983 if (!npages)
984 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
986 new = old = *memslot;
988 new.base_gfn = base_gfn;
989 new.npages = npages;
990 new.flags = mem->flags;
992 /* Disallow changing a memory slot's size. */
993 r = -EINVAL;
994 if (npages && old.npages && npages != old.npages)
995 goto out_free;
997 /* Check for overlaps */
998 r = -EEXIST;
999 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1000 struct kvm_memory_slot *s = &kvm->memslots[i];
1002 if (s == memslot)
1003 continue;
1004 if (!((base_gfn + npages <= s->base_gfn) ||
1005 (base_gfn >= s->base_gfn + s->npages)))
1006 goto out_free;
1009 /* Free page dirty bitmap if unneeded */
1010 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
1011 new.dirty_bitmap = NULL;
1013 r = -ENOMEM;
1015 /* Allocate if a slot is being created */
1016 #ifndef CONFIG_S390
1017 if (npages && !new.rmap) {
1018 new.rmap = vmalloc(npages * sizeof(struct page *));
1020 if (!new.rmap)
1021 goto out_free;
1023 memset(new.rmap, 0, npages * sizeof(*new.rmap));
1025 new.user_alloc = user_alloc;
1027 * hva_to_rmmap() serialzies with the mmu_lock and to be
1028 * safe it has to ignore memslots with !user_alloc &&
1029 * !userspace_addr.
1031 if (user_alloc)
1032 new.userspace_addr = mem->userspace_addr;
1033 else
1034 new.userspace_addr = 0;
1036 if (npages && !new.lpage_info) {
1037 int largepages = npages / KVM_PAGES_PER_HPAGE;
1038 if (npages % KVM_PAGES_PER_HPAGE)
1039 largepages++;
1040 if (base_gfn % KVM_PAGES_PER_HPAGE)
1041 largepages++;
1043 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1045 if (!new.lpage_info)
1046 goto out_free;
1048 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1050 if (base_gfn % KVM_PAGES_PER_HPAGE)
1051 new.lpage_info[0].write_count = 1;
1052 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1053 new.lpage_info[largepages-1].write_count = 1;
1056 /* Allocate page dirty bitmap if needed */
1057 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1058 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1060 new.dirty_bitmap = vmalloc(dirty_bytes);
1061 if (!new.dirty_bitmap)
1062 goto out_free;
1063 memset(new.dirty_bitmap, 0, dirty_bytes);
1065 #endif /* not defined CONFIG_S390 */
1067 if (!npages)
1068 kvm_arch_flush_shadow(kvm);
1070 spin_lock(&kvm->mmu_lock);
1071 if (mem->slot >= kvm->nmemslots)
1072 kvm->nmemslots = mem->slot + 1;
1074 *memslot = new;
1075 spin_unlock(&kvm->mmu_lock);
1077 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1078 if (r) {
1079 spin_lock(&kvm->mmu_lock);
1080 *memslot = old;
1081 spin_unlock(&kvm->mmu_lock);
1082 goto out_free;
1085 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1086 /* Slot deletion case: we have to update the current slot */
1087 if (!npages)
1088 *memslot = old;
1089 #ifdef CONFIG_DMAR
1090 /* map the pages in iommu page table */
1091 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1092 if (r)
1093 goto out;
1094 #endif
1095 return 0;
1097 out_free:
1098 kvm_free_physmem_slot(&new, &old);
1099 out:
1100 return r;
1103 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1105 int kvm_set_memory_region(struct kvm *kvm,
1106 struct kvm_userspace_memory_region *mem,
1107 int user_alloc)
1109 int r;
1111 down_write(&kvm->slots_lock);
1112 r = __kvm_set_memory_region(kvm, mem, user_alloc);
1113 up_write(&kvm->slots_lock);
1114 return r;
1116 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1118 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1119 struct
1120 kvm_userspace_memory_region *mem,
1121 int user_alloc)
1123 if (mem->slot >= KVM_MEMORY_SLOTS)
1124 return -EINVAL;
1125 return kvm_set_memory_region(kvm, mem, user_alloc);
1128 int kvm_get_dirty_log(struct kvm *kvm,
1129 struct kvm_dirty_log *log, int *is_dirty)
1131 struct kvm_memory_slot *memslot;
1132 int r, i;
1133 int n;
1134 unsigned long any = 0;
1136 r = -EINVAL;
1137 if (log->slot >= KVM_MEMORY_SLOTS)
1138 goto out;
1140 memslot = &kvm->memslots[log->slot];
1141 r = -ENOENT;
1142 if (!memslot->dirty_bitmap)
1143 goto out;
1145 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1147 for (i = 0; !any && i < n/sizeof(long); ++i)
1148 any = memslot->dirty_bitmap[i];
1150 r = -EFAULT;
1151 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1152 goto out;
1154 if (any)
1155 *is_dirty = 1;
1157 r = 0;
1158 out:
1159 return r;
1162 int is_error_page(struct page *page)
1164 return page == bad_page;
1166 EXPORT_SYMBOL_GPL(is_error_page);
1168 int is_error_pfn(pfn_t pfn)
1170 return pfn == bad_pfn;
1172 EXPORT_SYMBOL_GPL(is_error_pfn);
1174 static inline unsigned long bad_hva(void)
1176 return PAGE_OFFSET;
1179 int kvm_is_error_hva(unsigned long addr)
1181 return addr == bad_hva();
1183 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1185 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1187 int i;
1189 for (i = 0; i < kvm->nmemslots; ++i) {
1190 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1192 if (gfn >= memslot->base_gfn
1193 && gfn < memslot->base_gfn + memslot->npages)
1194 return memslot;
1196 return NULL;
1198 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1200 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1202 gfn = unalias_gfn(kvm, gfn);
1203 return gfn_to_memslot_unaliased(kvm, gfn);
1206 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1208 int i;
1210 gfn = unalias_gfn(kvm, gfn);
1211 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1212 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1214 if (gfn >= memslot->base_gfn
1215 && gfn < memslot->base_gfn + memslot->npages)
1216 return 1;
1218 return 0;
1220 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1222 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1224 struct kvm_memory_slot *slot;
1226 gfn = unalias_gfn(kvm, gfn);
1227 slot = gfn_to_memslot_unaliased(kvm, gfn);
1228 if (!slot)
1229 return bad_hva();
1230 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1232 EXPORT_SYMBOL_GPL(gfn_to_hva);
1234 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1236 struct page *page[1];
1237 unsigned long addr;
1238 int npages;
1239 pfn_t pfn;
1241 might_sleep();
1243 addr = gfn_to_hva(kvm, gfn);
1244 if (kvm_is_error_hva(addr)) {
1245 get_page(bad_page);
1246 return page_to_pfn(bad_page);
1249 npages = get_user_pages_fast(addr, 1, 1, page);
1251 if (unlikely(npages != 1)) {
1252 struct vm_area_struct *vma;
1254 down_read(&current->mm->mmap_sem);
1255 vma = find_vma(current->mm, addr);
1257 if (vma == NULL || addr < vma->vm_start ||
1258 !(vma->vm_flags & VM_PFNMAP)) {
1259 up_read(&current->mm->mmap_sem);
1260 get_page(bad_page);
1261 return page_to_pfn(bad_page);
1264 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1265 up_read(&current->mm->mmap_sem);
1266 BUG_ON(!kvm_is_mmio_pfn(pfn));
1267 } else
1268 pfn = page_to_pfn(page[0]);
1270 return pfn;
1273 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1275 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1277 pfn_t pfn;
1279 pfn = gfn_to_pfn(kvm, gfn);
1280 if (!kvm_is_mmio_pfn(pfn))
1281 return pfn_to_page(pfn);
1283 WARN_ON(kvm_is_mmio_pfn(pfn));
1285 get_page(bad_page);
1286 return bad_page;
1289 EXPORT_SYMBOL_GPL(gfn_to_page);
1291 void kvm_release_page_clean(struct page *page)
1293 kvm_release_pfn_clean(page_to_pfn(page));
1295 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1297 void kvm_release_pfn_clean(pfn_t pfn)
1299 if (!kvm_is_mmio_pfn(pfn))
1300 put_page(pfn_to_page(pfn));
1302 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1304 void kvm_release_page_dirty(struct page *page)
1306 kvm_release_pfn_dirty(page_to_pfn(page));
1308 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1310 void kvm_release_pfn_dirty(pfn_t pfn)
1312 kvm_set_pfn_dirty(pfn);
1313 kvm_release_pfn_clean(pfn);
1315 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1317 void kvm_set_page_dirty(struct page *page)
1319 kvm_set_pfn_dirty(page_to_pfn(page));
1321 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1323 void kvm_set_pfn_dirty(pfn_t pfn)
1325 if (!kvm_is_mmio_pfn(pfn)) {
1326 struct page *page = pfn_to_page(pfn);
1327 if (!PageReserved(page))
1328 SetPageDirty(page);
1331 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1333 void kvm_set_pfn_accessed(pfn_t pfn)
1335 if (!kvm_is_mmio_pfn(pfn))
1336 mark_page_accessed(pfn_to_page(pfn));
1338 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1340 void kvm_get_pfn(pfn_t pfn)
1342 if (!kvm_is_mmio_pfn(pfn))
1343 get_page(pfn_to_page(pfn));
1345 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1347 static int next_segment(unsigned long len, int offset)
1349 if (len > PAGE_SIZE - offset)
1350 return PAGE_SIZE - offset;
1351 else
1352 return len;
1355 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1356 int len)
1358 int r;
1359 unsigned long addr;
1361 addr = gfn_to_hva(kvm, gfn);
1362 if (kvm_is_error_hva(addr))
1363 return -EFAULT;
1364 r = copy_from_user(data, (void __user *)addr + offset, len);
1365 if (r)
1366 return -EFAULT;
1367 return 0;
1369 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1371 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1373 gfn_t gfn = gpa >> PAGE_SHIFT;
1374 int seg;
1375 int offset = offset_in_page(gpa);
1376 int ret;
1378 while ((seg = next_segment(len, offset)) != 0) {
1379 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1380 if (ret < 0)
1381 return ret;
1382 offset = 0;
1383 len -= seg;
1384 data += seg;
1385 ++gfn;
1387 return 0;
1389 EXPORT_SYMBOL_GPL(kvm_read_guest);
1391 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1392 unsigned long len)
1394 int r;
1395 unsigned long addr;
1396 gfn_t gfn = gpa >> PAGE_SHIFT;
1397 int offset = offset_in_page(gpa);
1399 addr = gfn_to_hva(kvm, gfn);
1400 if (kvm_is_error_hva(addr))
1401 return -EFAULT;
1402 pagefault_disable();
1403 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1404 pagefault_enable();
1405 if (r)
1406 return -EFAULT;
1407 return 0;
1409 EXPORT_SYMBOL(kvm_read_guest_atomic);
1411 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1412 int offset, int len)
1414 int r;
1415 unsigned long addr;
1417 addr = gfn_to_hva(kvm, gfn);
1418 if (kvm_is_error_hva(addr))
1419 return -EFAULT;
1420 r = copy_to_user((void __user *)addr + offset, data, len);
1421 if (r)
1422 return -EFAULT;
1423 mark_page_dirty(kvm, gfn);
1424 return 0;
1426 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1428 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1429 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_write_guest_page(kvm, gfn, data, offset, seg);
1438 if (ret < 0)
1439 return ret;
1440 offset = 0;
1441 len -= seg;
1442 data += seg;
1443 ++gfn;
1445 return 0;
1448 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1450 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1452 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1454 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1456 gfn_t gfn = gpa >> PAGE_SHIFT;
1457 int seg;
1458 int offset = offset_in_page(gpa);
1459 int ret;
1461 while ((seg = next_segment(len, offset)) != 0) {
1462 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1463 if (ret < 0)
1464 return ret;
1465 offset = 0;
1466 len -= seg;
1467 ++gfn;
1469 return 0;
1471 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1473 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1475 struct kvm_memory_slot *memslot;
1477 gfn = unalias_gfn(kvm, gfn);
1478 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1479 if (memslot && memslot->dirty_bitmap) {
1480 unsigned long rel_gfn = gfn - memslot->base_gfn;
1482 /* avoid RMW */
1483 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1484 set_bit(rel_gfn, memslot->dirty_bitmap);
1489 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1491 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1493 DEFINE_WAIT(wait);
1495 for (;;) {
1496 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1498 if (kvm_cpu_has_interrupt(vcpu) ||
1499 kvm_cpu_has_pending_timer(vcpu) ||
1500 kvm_arch_vcpu_runnable(vcpu)) {
1501 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1502 break;
1504 if (signal_pending(current))
1505 break;
1507 vcpu_put(vcpu);
1508 schedule();
1509 vcpu_load(vcpu);
1512 finish_wait(&vcpu->wq, &wait);
1515 void kvm_resched(struct kvm_vcpu *vcpu)
1517 if (!need_resched())
1518 return;
1519 cond_resched();
1521 EXPORT_SYMBOL_GPL(kvm_resched);
1523 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1525 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1526 struct page *page;
1528 if (vmf->pgoff == 0)
1529 page = virt_to_page(vcpu->run);
1530 #ifdef CONFIG_X86
1531 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1532 page = virt_to_page(vcpu->arch.pio_data);
1533 #endif
1534 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1535 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1536 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1537 #endif
1538 else
1539 return VM_FAULT_SIGBUS;
1540 get_page(page);
1541 vmf->page = page;
1542 return 0;
1545 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1546 .fault = kvm_vcpu_fault,
1549 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1551 vma->vm_ops = &kvm_vcpu_vm_ops;
1552 return 0;
1555 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1557 struct kvm_vcpu *vcpu = filp->private_data;
1559 kvm_put_kvm(vcpu->kvm);
1560 return 0;
1563 static struct file_operations kvm_vcpu_fops = {
1564 .release = kvm_vcpu_release,
1565 .unlocked_ioctl = kvm_vcpu_ioctl,
1566 .compat_ioctl = kvm_vcpu_ioctl,
1567 .mmap = kvm_vcpu_mmap,
1571 * Allocates an inode for the vcpu.
1573 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1575 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1576 if (fd < 0)
1577 kvm_put_kvm(vcpu->kvm);
1578 return fd;
1582 * Creates some virtual cpus. Good luck creating more than one.
1584 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1586 int r;
1587 struct kvm_vcpu *vcpu;
1589 if (!valid_vcpu(n))
1590 return -EINVAL;
1592 vcpu = kvm_arch_vcpu_create(kvm, n);
1593 if (IS_ERR(vcpu))
1594 return PTR_ERR(vcpu);
1596 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1598 r = kvm_arch_vcpu_setup(vcpu);
1599 if (r)
1600 return r;
1602 mutex_lock(&kvm->lock);
1603 if (kvm->vcpus[n]) {
1604 r = -EEXIST;
1605 goto vcpu_destroy;
1607 kvm->vcpus[n] = vcpu;
1608 mutex_unlock(&kvm->lock);
1610 /* Now it's all set up, let userspace reach it */
1611 kvm_get_kvm(kvm);
1612 r = create_vcpu_fd(vcpu);
1613 if (r < 0)
1614 goto unlink;
1615 return r;
1617 unlink:
1618 mutex_lock(&kvm->lock);
1619 kvm->vcpus[n] = NULL;
1620 vcpu_destroy:
1621 mutex_unlock(&kvm->lock);
1622 kvm_arch_vcpu_destroy(vcpu);
1623 return r;
1626 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1628 if (sigset) {
1629 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1630 vcpu->sigset_active = 1;
1631 vcpu->sigset = *sigset;
1632 } else
1633 vcpu->sigset_active = 0;
1634 return 0;
1637 static long kvm_vcpu_ioctl(struct file *filp,
1638 unsigned int ioctl, unsigned long arg)
1640 struct kvm_vcpu *vcpu = filp->private_data;
1641 void __user *argp = (void __user *)arg;
1642 int r;
1643 struct kvm_fpu *fpu = NULL;
1644 struct kvm_sregs *kvm_sregs = NULL;
1646 if (vcpu->kvm->mm != current->mm)
1647 return -EIO;
1648 switch (ioctl) {
1649 case KVM_RUN:
1650 r = -EINVAL;
1651 if (arg)
1652 goto out;
1653 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1654 break;
1655 case KVM_GET_REGS: {
1656 struct kvm_regs *kvm_regs;
1658 r = -ENOMEM;
1659 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1660 if (!kvm_regs)
1661 goto out;
1662 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1663 if (r)
1664 goto out_free1;
1665 r = -EFAULT;
1666 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1667 goto out_free1;
1668 r = 0;
1669 out_free1:
1670 kfree(kvm_regs);
1671 break;
1673 case KVM_SET_REGS: {
1674 struct kvm_regs *kvm_regs;
1676 r = -ENOMEM;
1677 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1678 if (!kvm_regs)
1679 goto out;
1680 r = -EFAULT;
1681 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1682 goto out_free2;
1683 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1684 if (r)
1685 goto out_free2;
1686 r = 0;
1687 out_free2:
1688 kfree(kvm_regs);
1689 break;
1691 case KVM_GET_SREGS: {
1692 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1693 r = -ENOMEM;
1694 if (!kvm_sregs)
1695 goto out;
1696 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1697 if (r)
1698 goto out;
1699 r = -EFAULT;
1700 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1701 goto out;
1702 r = 0;
1703 break;
1705 case KVM_SET_SREGS: {
1706 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1707 r = -ENOMEM;
1708 if (!kvm_sregs)
1709 goto out;
1710 r = -EFAULT;
1711 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1712 goto out;
1713 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1714 if (r)
1715 goto out;
1716 r = 0;
1717 break;
1719 case KVM_GET_MP_STATE: {
1720 struct kvm_mp_state mp_state;
1722 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1723 if (r)
1724 goto out;
1725 r = -EFAULT;
1726 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1727 goto out;
1728 r = 0;
1729 break;
1731 case KVM_SET_MP_STATE: {
1732 struct kvm_mp_state mp_state;
1734 r = -EFAULT;
1735 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1736 goto out;
1737 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1738 if (r)
1739 goto out;
1740 r = 0;
1741 break;
1743 case KVM_TRANSLATE: {
1744 struct kvm_translation tr;
1746 r = -EFAULT;
1747 if (copy_from_user(&tr, argp, sizeof tr))
1748 goto out;
1749 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1750 if (r)
1751 goto out;
1752 r = -EFAULT;
1753 if (copy_to_user(argp, &tr, sizeof tr))
1754 goto out;
1755 r = 0;
1756 break;
1758 case KVM_DEBUG_GUEST: {
1759 struct kvm_debug_guest dbg;
1761 r = -EFAULT;
1762 if (copy_from_user(&dbg, argp, sizeof dbg))
1763 goto out;
1764 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1765 if (r)
1766 goto out;
1767 r = 0;
1768 break;
1770 case KVM_SET_SIGNAL_MASK: {
1771 struct kvm_signal_mask __user *sigmask_arg = argp;
1772 struct kvm_signal_mask kvm_sigmask;
1773 sigset_t sigset, *p;
1775 p = NULL;
1776 if (argp) {
1777 r = -EFAULT;
1778 if (copy_from_user(&kvm_sigmask, argp,
1779 sizeof kvm_sigmask))
1780 goto out;
1781 r = -EINVAL;
1782 if (kvm_sigmask.len != sizeof sigset)
1783 goto out;
1784 r = -EFAULT;
1785 if (copy_from_user(&sigset, sigmask_arg->sigset,
1786 sizeof sigset))
1787 goto out;
1788 p = &sigset;
1790 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1791 break;
1793 case KVM_GET_FPU: {
1794 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1795 r = -ENOMEM;
1796 if (!fpu)
1797 goto out;
1798 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1799 if (r)
1800 goto out;
1801 r = -EFAULT;
1802 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1803 goto out;
1804 r = 0;
1805 break;
1807 case KVM_SET_FPU: {
1808 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1809 r = -ENOMEM;
1810 if (!fpu)
1811 goto out;
1812 r = -EFAULT;
1813 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1814 goto out;
1815 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1816 if (r)
1817 goto out;
1818 r = 0;
1819 break;
1821 default:
1822 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1824 out:
1825 kfree(fpu);
1826 kfree(kvm_sregs);
1827 return r;
1830 static long kvm_vm_ioctl(struct file *filp,
1831 unsigned int ioctl, unsigned long arg)
1833 struct kvm *kvm = filp->private_data;
1834 void __user *argp = (void __user *)arg;
1835 int r;
1837 if (kvm->mm != current->mm)
1838 return -EIO;
1839 switch (ioctl) {
1840 case KVM_CREATE_VCPU:
1841 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1842 if (r < 0)
1843 goto out;
1844 break;
1845 case KVM_SET_USER_MEMORY_REGION: {
1846 struct kvm_userspace_memory_region kvm_userspace_mem;
1848 r = -EFAULT;
1849 if (copy_from_user(&kvm_userspace_mem, argp,
1850 sizeof kvm_userspace_mem))
1851 goto out;
1853 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1854 if (r)
1855 goto out;
1856 break;
1858 case KVM_GET_DIRTY_LOG: {
1859 struct kvm_dirty_log log;
1861 r = -EFAULT;
1862 if (copy_from_user(&log, argp, sizeof log))
1863 goto out;
1864 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1865 if (r)
1866 goto out;
1867 break;
1869 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1870 case KVM_REGISTER_COALESCED_MMIO: {
1871 struct kvm_coalesced_mmio_zone zone;
1872 r = -EFAULT;
1873 if (copy_from_user(&zone, argp, sizeof zone))
1874 goto out;
1875 r = -ENXIO;
1876 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1877 if (r)
1878 goto out;
1879 r = 0;
1880 break;
1882 case KVM_UNREGISTER_COALESCED_MMIO: {
1883 struct kvm_coalesced_mmio_zone zone;
1884 r = -EFAULT;
1885 if (copy_from_user(&zone, argp, sizeof zone))
1886 goto out;
1887 r = -ENXIO;
1888 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1889 if (r)
1890 goto out;
1891 r = 0;
1892 break;
1894 #endif
1895 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1896 case KVM_ASSIGN_PCI_DEVICE: {
1897 struct kvm_assigned_pci_dev assigned_dev;
1899 r = -EFAULT;
1900 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1901 goto out;
1902 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1903 if (r)
1904 goto out;
1905 break;
1907 case KVM_ASSIGN_IRQ: {
1908 struct kvm_assigned_irq assigned_irq;
1910 r = -EFAULT;
1911 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1912 goto out;
1913 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1914 if (r)
1915 goto out;
1916 break;
1918 #endif
1919 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1920 case KVM_DEASSIGN_PCI_DEVICE: {
1921 struct kvm_assigned_pci_dev assigned_dev;
1923 r = -EFAULT;
1924 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1925 goto out;
1926 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
1927 if (r)
1928 goto out;
1929 break;
1931 #endif
1932 default:
1933 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1935 out:
1936 return r;
1939 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1941 struct page *page[1];
1942 unsigned long addr;
1943 int npages;
1944 gfn_t gfn = vmf->pgoff;
1945 struct kvm *kvm = vma->vm_file->private_data;
1947 addr = gfn_to_hva(kvm, gfn);
1948 if (kvm_is_error_hva(addr))
1949 return VM_FAULT_SIGBUS;
1951 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1952 NULL);
1953 if (unlikely(npages != 1))
1954 return VM_FAULT_SIGBUS;
1956 vmf->page = page[0];
1957 return 0;
1960 static struct vm_operations_struct kvm_vm_vm_ops = {
1961 .fault = kvm_vm_fault,
1964 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1966 vma->vm_ops = &kvm_vm_vm_ops;
1967 return 0;
1970 static struct file_operations kvm_vm_fops = {
1971 .release = kvm_vm_release,
1972 .unlocked_ioctl = kvm_vm_ioctl,
1973 .compat_ioctl = kvm_vm_ioctl,
1974 .mmap = kvm_vm_mmap,
1977 static int kvm_dev_ioctl_create_vm(void)
1979 int fd;
1980 struct kvm *kvm;
1982 kvm = kvm_create_vm();
1983 if (IS_ERR(kvm))
1984 return PTR_ERR(kvm);
1985 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1986 if (fd < 0)
1987 kvm_put_kvm(kvm);
1989 return fd;
1992 static long kvm_dev_ioctl_check_extension_generic(long arg)
1994 switch (arg) {
1995 case KVM_CAP_USER_MEMORY:
1996 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
1997 return 1;
1998 default:
1999 break;
2001 return kvm_dev_ioctl_check_extension(arg);
2004 static long kvm_dev_ioctl(struct file *filp,
2005 unsigned int ioctl, unsigned long arg)
2007 long r = -EINVAL;
2009 switch (ioctl) {
2010 case KVM_GET_API_VERSION:
2011 r = -EINVAL;
2012 if (arg)
2013 goto out;
2014 r = KVM_API_VERSION;
2015 break;
2016 case KVM_CREATE_VM:
2017 r = -EINVAL;
2018 if (arg)
2019 goto out;
2020 r = kvm_dev_ioctl_create_vm();
2021 break;
2022 case KVM_CHECK_EXTENSION:
2023 r = kvm_dev_ioctl_check_extension_generic(arg);
2024 break;
2025 case KVM_GET_VCPU_MMAP_SIZE:
2026 r = -EINVAL;
2027 if (arg)
2028 goto out;
2029 r = PAGE_SIZE; /* struct kvm_run */
2030 #ifdef CONFIG_X86
2031 r += PAGE_SIZE; /* pio data page */
2032 #endif
2033 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2034 r += PAGE_SIZE; /* coalesced mmio ring page */
2035 #endif
2036 break;
2037 case KVM_TRACE_ENABLE:
2038 case KVM_TRACE_PAUSE:
2039 case KVM_TRACE_DISABLE:
2040 r = kvm_trace_ioctl(ioctl, arg);
2041 break;
2042 default:
2043 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2045 out:
2046 return r;
2049 static struct file_operations kvm_chardev_ops = {
2050 .unlocked_ioctl = kvm_dev_ioctl,
2051 .compat_ioctl = kvm_dev_ioctl,
2054 static struct miscdevice kvm_dev = {
2055 KVM_MINOR,
2056 "kvm",
2057 &kvm_chardev_ops,
2060 static void hardware_enable(void *junk)
2062 int cpu = raw_smp_processor_id();
2064 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2065 return;
2066 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2067 kvm_arch_hardware_enable(NULL);
2070 static void hardware_disable(void *junk)
2072 int cpu = raw_smp_processor_id();
2074 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2075 return;
2076 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2077 kvm_arch_hardware_disable(NULL);
2080 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2081 void *v)
2083 int cpu = (long)v;
2085 val &= ~CPU_TASKS_FROZEN;
2086 switch (val) {
2087 case CPU_DYING:
2088 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2089 cpu);
2090 hardware_disable(NULL);
2091 break;
2092 case CPU_UP_CANCELED:
2093 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2094 cpu);
2095 smp_call_function_single(cpu, hardware_disable, NULL, 1);
2096 break;
2097 case CPU_ONLINE:
2098 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2099 cpu);
2100 smp_call_function_single(cpu, hardware_enable, NULL, 1);
2101 break;
2103 return NOTIFY_OK;
2107 asmlinkage void kvm_handle_fault_on_reboot(void)
2109 if (kvm_rebooting)
2110 /* spin while reset goes on */
2111 while (true)
2113 /* Fault while not rebooting. We want the trace. */
2114 BUG();
2116 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2118 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2119 void *v)
2121 if (val == SYS_RESTART) {
2123 * Some (well, at least mine) BIOSes hang on reboot if
2124 * in vmx root mode.
2126 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2127 kvm_rebooting = true;
2128 on_each_cpu(hardware_disable, NULL, 1);
2130 return NOTIFY_OK;
2133 static struct notifier_block kvm_reboot_notifier = {
2134 .notifier_call = kvm_reboot,
2135 .priority = 0,
2138 void kvm_io_bus_init(struct kvm_io_bus *bus)
2140 memset(bus, 0, sizeof(*bus));
2143 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2145 int i;
2147 for (i = 0; i < bus->dev_count; i++) {
2148 struct kvm_io_device *pos = bus->devs[i];
2150 kvm_iodevice_destructor(pos);
2154 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2155 gpa_t addr, int len, int is_write)
2157 int i;
2159 for (i = 0; i < bus->dev_count; i++) {
2160 struct kvm_io_device *pos = bus->devs[i];
2162 if (pos->in_range(pos, addr, len, is_write))
2163 return pos;
2166 return NULL;
2169 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2171 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2173 bus->devs[bus->dev_count++] = dev;
2176 static struct notifier_block kvm_cpu_notifier = {
2177 .notifier_call = kvm_cpu_hotplug,
2178 .priority = 20, /* must be > scheduler priority */
2181 static int vm_stat_get(void *_offset, u64 *val)
2183 unsigned offset = (long)_offset;
2184 struct kvm *kvm;
2186 *val = 0;
2187 spin_lock(&kvm_lock);
2188 list_for_each_entry(kvm, &vm_list, vm_list)
2189 *val += *(u32 *)((void *)kvm + offset);
2190 spin_unlock(&kvm_lock);
2191 return 0;
2194 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2196 static int vcpu_stat_get(void *_offset, u64 *val)
2198 unsigned offset = (long)_offset;
2199 struct kvm *kvm;
2200 struct kvm_vcpu *vcpu;
2201 int i;
2203 *val = 0;
2204 spin_lock(&kvm_lock);
2205 list_for_each_entry(kvm, &vm_list, vm_list)
2206 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2207 vcpu = kvm->vcpus[i];
2208 if (vcpu)
2209 *val += *(u32 *)((void *)vcpu + offset);
2211 spin_unlock(&kvm_lock);
2212 return 0;
2215 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2217 static struct file_operations *stat_fops[] = {
2218 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2219 [KVM_STAT_VM] = &vm_stat_fops,
2222 static void kvm_init_debug(void)
2224 struct kvm_stats_debugfs_item *p;
2226 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2227 for (p = debugfs_entries; p->name; ++p)
2228 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2229 (void *)(long)p->offset,
2230 stat_fops[p->kind]);
2233 static void kvm_exit_debug(void)
2235 struct kvm_stats_debugfs_item *p;
2237 for (p = debugfs_entries; p->name; ++p)
2238 debugfs_remove(p->dentry);
2239 debugfs_remove(kvm_debugfs_dir);
2242 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2244 hardware_disable(NULL);
2245 return 0;
2248 static int kvm_resume(struct sys_device *dev)
2250 hardware_enable(NULL);
2251 return 0;
2254 static struct sysdev_class kvm_sysdev_class = {
2255 .name = "kvm",
2256 .suspend = kvm_suspend,
2257 .resume = kvm_resume,
2260 static struct sys_device kvm_sysdev = {
2261 .id = 0,
2262 .cls = &kvm_sysdev_class,
2265 struct page *bad_page;
2266 pfn_t bad_pfn;
2268 static inline
2269 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2271 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2274 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2276 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2278 kvm_arch_vcpu_load(vcpu, cpu);
2281 static void kvm_sched_out(struct preempt_notifier *pn,
2282 struct task_struct *next)
2284 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2286 kvm_arch_vcpu_put(vcpu);
2289 int kvm_init(void *opaque, unsigned int vcpu_size,
2290 struct module *module)
2292 int r;
2293 int cpu;
2295 kvm_init_debug();
2297 r = kvm_arch_init(opaque);
2298 if (r)
2299 goto out_fail;
2301 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2303 if (bad_page == NULL) {
2304 r = -ENOMEM;
2305 goto out;
2308 bad_pfn = page_to_pfn(bad_page);
2310 if (!alloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2311 r = -ENOMEM;
2312 goto out_free_0;
2315 r = kvm_arch_hardware_setup();
2316 if (r < 0)
2317 goto out_free_0a;
2319 for_each_online_cpu(cpu) {
2320 smp_call_function_single(cpu,
2321 kvm_arch_check_processor_compat,
2322 &r, 1);
2323 if (r < 0)
2324 goto out_free_1;
2327 on_each_cpu(hardware_enable, NULL, 1);
2328 r = register_cpu_notifier(&kvm_cpu_notifier);
2329 if (r)
2330 goto out_free_2;
2331 register_reboot_notifier(&kvm_reboot_notifier);
2333 r = sysdev_class_register(&kvm_sysdev_class);
2334 if (r)
2335 goto out_free_3;
2337 r = sysdev_register(&kvm_sysdev);
2338 if (r)
2339 goto out_free_4;
2341 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2342 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2343 __alignof__(struct kvm_vcpu),
2344 0, NULL);
2345 if (!kvm_vcpu_cache) {
2346 r = -ENOMEM;
2347 goto out_free_5;
2350 kvm_chardev_ops.owner = module;
2351 kvm_vm_fops.owner = module;
2352 kvm_vcpu_fops.owner = module;
2354 r = misc_register(&kvm_dev);
2355 if (r) {
2356 printk(KERN_ERR "kvm: misc device register failed\n");
2357 goto out_free;
2360 kvm_preempt_ops.sched_in = kvm_sched_in;
2361 kvm_preempt_ops.sched_out = kvm_sched_out;
2362 #ifndef CONFIG_X86
2363 msi2intx = 0;
2364 #endif
2366 return 0;
2368 out_free:
2369 kmem_cache_destroy(kvm_vcpu_cache);
2370 out_free_5:
2371 sysdev_unregister(&kvm_sysdev);
2372 out_free_4:
2373 sysdev_class_unregister(&kvm_sysdev_class);
2374 out_free_3:
2375 unregister_reboot_notifier(&kvm_reboot_notifier);
2376 unregister_cpu_notifier(&kvm_cpu_notifier);
2377 out_free_2:
2378 on_each_cpu(hardware_disable, NULL, 1);
2379 out_free_1:
2380 kvm_arch_hardware_unsetup();
2381 out_free_0a:
2382 free_cpumask_var(cpus_hardware_enabled);
2383 out_free_0:
2384 __free_page(bad_page);
2385 out:
2386 kvm_arch_exit();
2387 kvm_exit_debug();
2388 out_fail:
2389 return r;
2391 EXPORT_SYMBOL_GPL(kvm_init);
2393 void kvm_exit(void)
2395 kvm_trace_cleanup();
2396 misc_deregister(&kvm_dev);
2397 kmem_cache_destroy(kvm_vcpu_cache);
2398 sysdev_unregister(&kvm_sysdev);
2399 sysdev_class_unregister(&kvm_sysdev_class);
2400 unregister_reboot_notifier(&kvm_reboot_notifier);
2401 unregister_cpu_notifier(&kvm_cpu_notifier);
2402 on_each_cpu(hardware_disable, NULL, 1);
2403 kvm_arch_hardware_unsetup();
2404 kvm_arch_exit();
2405 kvm_exit_debug();
2406 free_cpumask_var(cpus_hardware_enabled);
2407 __free_page(bad_page);
2409 EXPORT_SYMBOL_GPL(kvm_exit);