Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
| 8 | * |
| 9 | * Authors: |
| 10 | * Avi Kivity <avi@qumranet.com> |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 14 | * the COPYING file in the top-level directory. |
| 15 | * |
| 16 | */ |
| 17 | |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 18 | #include "iodev.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 20 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 21 | #include <linux/kvm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/errno.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/percpu.h> |
| 25 | #include <linux/gfp.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/vmalloc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | #include <linux/reboot.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/highmem.h> |
| 32 | #include <linux/file.h> |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 33 | #include <linux/sysdev.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 34 | #include <linux/cpu.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 35 | #include <linux/sched.h> |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 36 | #include <linux/cpumask.h> |
| 37 | #include <linux/smp.h> |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 38 | #include <linux/anon_inodes.h> |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 39 | #include <linux/profile.h> |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 40 | #include <linux/kvm_para.h> |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 41 | #include <linux/pagemap.h> |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 42 | #include <linux/mman.h> |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 43 | #include <linux/swap.h> |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 44 | #include <linux/bitops.h> |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 45 | #include <linux/spinlock.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 46 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 47 | #include <asm/processor.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 48 | #include <asm/io.h> |
| 49 | #include <asm/uaccess.h> |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 50 | #include <asm/pgtable.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 51 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 52 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 53 | #include "coalesced_mmio.h" |
| 54 | #endif |
| 55 | |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 56 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT |
| 57 | #include <linux/pci.h> |
| 58 | #include <linux/interrupt.h> |
| 59 | #include "irq.h" |
| 60 | #endif |
| 61 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 62 | MODULE_AUTHOR("Qumranet"); |
| 63 | MODULE_LICENSE("GPL"); |
| 64 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 65 | DEFINE_SPINLOCK(kvm_lock); |
| 66 | LIST_HEAD(vm_list); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 67 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 68 | static cpumask_var_t cpus_hardware_enabled; |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 69 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 70 | struct kmem_cache *kvm_vcpu_cache; |
| 71 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 72 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 73 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
| 74 | |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 75 | struct dentry *kvm_debugfs_dir; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 76 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 77 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 78 | unsigned long arg); |
| 79 | |
Hannes Eder | e8ba5d3 | 2008-11-28 17:02:42 +0100 | [diff] [blame] | 80 | static bool kvm_rebooting; |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 81 | |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 82 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT |
| 83 | static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head, |
| 84 | int assigned_dev_id) |
| 85 | { |
| 86 | struct list_head *ptr; |
| 87 | struct kvm_assigned_dev_kernel *match; |
| 88 | |
| 89 | list_for_each(ptr, head) { |
| 90 | match = list_entry(ptr, struct kvm_assigned_dev_kernel, list); |
| 91 | if (match->assigned_dev_id == assigned_dev_id) |
| 92 | return match; |
| 93 | } |
| 94 | return NULL; |
| 95 | } |
| 96 | |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 97 | static int find_index_from_host_irq(struct kvm_assigned_dev_kernel |
| 98 | *assigned_dev, int irq) |
| 99 | { |
| 100 | int i, index; |
| 101 | struct msix_entry *host_msix_entries; |
| 102 | |
| 103 | host_msix_entries = assigned_dev->host_msix_entries; |
| 104 | |
| 105 | index = -1; |
| 106 | for (i = 0; i < assigned_dev->entries_nr; i++) |
| 107 | if (irq == host_msix_entries[i].vector) { |
| 108 | index = i; |
| 109 | break; |
| 110 | } |
| 111 | if (index < 0) { |
| 112 | printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n"); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | return index; |
| 117 | } |
| 118 | |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 119 | static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work) |
| 120 | { |
| 121 | struct kvm_assigned_dev_kernel *assigned_dev; |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 122 | struct kvm *kvm; |
Sheng Yang | 968a634 | 2009-04-30 10:58:42 +0800 | [diff] [blame] | 123 | int i; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 124 | |
| 125 | assigned_dev = container_of(work, struct kvm_assigned_dev_kernel, |
| 126 | interrupt_work); |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 127 | kvm = assigned_dev->kvm; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 128 | |
| 129 | /* This is taken to safely inject irq inside the guest. When |
| 130 | * the interrupt injection (or the ioapic code) uses a |
| 131 | * finer-grained lock, update this |
| 132 | */ |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 133 | mutex_lock(&kvm->lock); |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 134 | spin_lock_irq(&assigned_dev->assigned_dev_lock); |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 135 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 136 | struct kvm_guest_msix_entry *guest_entries = |
| 137 | assigned_dev->guest_msix_entries; |
| 138 | for (i = 0; i < assigned_dev->entries_nr; i++) { |
| 139 | if (!(guest_entries[i].flags & |
| 140 | KVM_ASSIGNED_MSIX_PENDING)) |
| 141 | continue; |
| 142 | guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING; |
| 143 | kvm_set_irq(assigned_dev->kvm, |
| 144 | assigned_dev->irq_source_id, |
| 145 | guest_entries[i].vector, 1); |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 146 | } |
Sheng Yang | 968a634 | 2009-04-30 10:58:42 +0800 | [diff] [blame] | 147 | } else |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 148 | kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id, |
| 149 | assigned_dev->guest_irq, 1); |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 150 | |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 151 | spin_unlock_irq(&assigned_dev->assigned_dev_lock); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 152 | mutex_unlock(&assigned_dev->kvm->lock); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 153 | } |
| 154 | |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 155 | static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id) |
| 156 | { |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 157 | unsigned long flags; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 158 | struct kvm_assigned_dev_kernel *assigned_dev = |
| 159 | (struct kvm_assigned_dev_kernel *) dev_id; |
| 160 | |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 161 | spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags); |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 162 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 163 | int index = find_index_from_host_irq(assigned_dev, irq); |
| 164 | if (index < 0) |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 165 | goto out; |
Sheng Yang | 2350bd1 | 2009-02-25 17:22:27 +0800 | [diff] [blame] | 166 | assigned_dev->guest_msix_entries[index].flags |= |
| 167 | KVM_ASSIGNED_MSIX_PENDING; |
| 168 | } |
| 169 | |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 170 | schedule_work(&assigned_dev->interrupt_work); |
Mark McLoughlin | defaf15 | 2008-12-02 12:16:33 +0000 | [diff] [blame] | 171 | |
Sheng Yang | 968a634 | 2009-04-30 10:58:42 +0800 | [diff] [blame] | 172 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) { |
| 173 | disable_irq_nosync(irq); |
| 174 | assigned_dev->host_irq_disabled = true; |
| 175 | } |
Mark McLoughlin | defaf15 | 2008-12-02 12:16:33 +0000 | [diff] [blame] | 176 | |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 177 | out: |
| 178 | spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 179 | return IRQ_HANDLED; |
| 180 | } |
| 181 | |
| 182 | /* Ack the irq line for an assigned device */ |
| 183 | static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian) |
| 184 | { |
| 185 | struct kvm_assigned_dev_kernel *dev; |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 186 | unsigned long flags; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 187 | |
| 188 | if (kian->gsi == -1) |
| 189 | return; |
| 190 | |
| 191 | dev = container_of(kian, struct kvm_assigned_dev_kernel, |
| 192 | ack_notifier); |
Mark McLoughlin | defaf15 | 2008-12-02 12:16:33 +0000 | [diff] [blame] | 193 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 194 | kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0); |
Mark McLoughlin | defaf15 | 2008-12-02 12:16:33 +0000 | [diff] [blame] | 195 | |
| 196 | /* The guest irq may be shared so this ack may be |
| 197 | * from another device. |
| 198 | */ |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 199 | spin_lock_irqsave(&dev->assigned_dev_lock, flags); |
Mark McLoughlin | defaf15 | 2008-12-02 12:16:33 +0000 | [diff] [blame] | 200 | if (dev->host_irq_disabled) { |
| 201 | enable_irq(dev->host_irq); |
| 202 | dev->host_irq_disabled = false; |
| 203 | } |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 204 | spin_unlock_irqrestore(&dev->assigned_dev_lock, flags); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 205 | } |
| 206 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 207 | static void deassign_guest_irq(struct kvm *kvm, |
| 208 | struct kvm_assigned_dev_kernel *assigned_dev) |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 209 | { |
Sheng Yang | e19e30e | 2008-10-20 16:07:10 +0800 | [diff] [blame] | 210 | kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier); |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 211 | assigned_dev->ack_notifier.gsi = -1; |
Mark McLoughlin | f29b267 | 2008-12-01 13:57:47 +0000 | [diff] [blame] | 212 | |
| 213 | if (assigned_dev->irq_source_id != -1) |
| 214 | kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id); |
| 215 | assigned_dev->irq_source_id = -1; |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 216 | assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK); |
| 217 | } |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 218 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 219 | /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */ |
| 220 | static void deassign_host_irq(struct kvm *kvm, |
| 221 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 222 | { |
Sheng Yang | ba4cef3 | 2009-01-06 10:03:03 +0800 | [diff] [blame] | 223 | /* |
| 224 | * In kvm_free_device_irq, cancel_work_sync return true if: |
| 225 | * 1. work is scheduled, and then cancelled. |
| 226 | * 2. work callback is executed. |
| 227 | * |
| 228 | * The first one ensured that the irq is disabled and no more events |
| 229 | * would happen. But for the second one, the irq may be enabled (e.g. |
| 230 | * for MSI). So we disable irq here to prevent further events. |
| 231 | * |
| 232 | * Notice this maybe result in nested disable if the interrupt type is |
| 233 | * INTx, but it's OK for we are going to free it. |
| 234 | * |
| 235 | * If this function is a part of VM destroy, please ensure that till |
| 236 | * now, the kvm state is still legal for probably we also have to wait |
| 237 | * interrupt_work done. |
| 238 | */ |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 239 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 240 | int i; |
| 241 | for (i = 0; i < assigned_dev->entries_nr; i++) |
| 242 | disable_irq_nosync(assigned_dev-> |
| 243 | host_msix_entries[i].vector); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 244 | |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 245 | cancel_work_sync(&assigned_dev->interrupt_work); |
Mark McLoughlin | 4a643be | 2008-12-01 13:57:49 +0000 | [diff] [blame] | 246 | |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 247 | for (i = 0; i < assigned_dev->entries_nr; i++) |
| 248 | free_irq(assigned_dev->host_msix_entries[i].vector, |
| 249 | (void *)assigned_dev); |
| 250 | |
| 251 | assigned_dev->entries_nr = 0; |
| 252 | kfree(assigned_dev->host_msix_entries); |
| 253 | kfree(assigned_dev->guest_msix_entries); |
| 254 | pci_disable_msix(assigned_dev->dev); |
| 255 | } else { |
| 256 | /* Deal with MSI and INTx */ |
| 257 | disable_irq_nosync(assigned_dev->host_irq); |
| 258 | cancel_work_sync(&assigned_dev->interrupt_work); |
| 259 | |
| 260 | free_irq(assigned_dev->host_irq, (void *)assigned_dev); |
| 261 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 262 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI) |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 263 | pci_disable_msi(assigned_dev->dev); |
| 264 | } |
Mark McLoughlin | 4a643be | 2008-12-01 13:57:49 +0000 | [diff] [blame] | 265 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 266 | assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK); |
Mark McLoughlin | 4a643be | 2008-12-01 13:57:49 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 269 | static int kvm_deassign_irq(struct kvm *kvm, |
| 270 | struct kvm_assigned_dev_kernel *assigned_dev, |
| 271 | unsigned long irq_requested_type) |
| 272 | { |
| 273 | unsigned long guest_irq_type, host_irq_type; |
| 274 | |
| 275 | if (!irqchip_in_kernel(kvm)) |
| 276 | return -EINVAL; |
| 277 | /* no irq assignment to deassign */ |
| 278 | if (!assigned_dev->irq_requested_type) |
| 279 | return -ENXIO; |
| 280 | |
| 281 | host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK; |
| 282 | guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK; |
| 283 | |
| 284 | if (host_irq_type) |
| 285 | deassign_host_irq(kvm, assigned_dev); |
| 286 | if (guest_irq_type) |
| 287 | deassign_guest_irq(kvm, assigned_dev); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static void kvm_free_assigned_irq(struct kvm *kvm, |
| 293 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 294 | { |
| 295 | kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type); |
| 296 | } |
Mark McLoughlin | 4a643be | 2008-12-01 13:57:49 +0000 | [diff] [blame] | 297 | |
| 298 | static void kvm_free_assigned_device(struct kvm *kvm, |
| 299 | struct kvm_assigned_dev_kernel |
| 300 | *assigned_dev) |
| 301 | { |
| 302 | kvm_free_assigned_irq(kvm, assigned_dev); |
| 303 | |
Sheng Yang | 6eb5581 | 2008-10-31 12:37:41 +0800 | [diff] [blame] | 304 | pci_reset_function(assigned_dev->dev); |
| 305 | |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 306 | pci_release_regions(assigned_dev->dev); |
| 307 | pci_disable_device(assigned_dev->dev); |
| 308 | pci_dev_put(assigned_dev->dev); |
| 309 | |
| 310 | list_del(&assigned_dev->list); |
| 311 | kfree(assigned_dev); |
| 312 | } |
| 313 | |
| 314 | void kvm_free_all_assigned_devices(struct kvm *kvm) |
| 315 | { |
| 316 | struct list_head *ptr, *ptr2; |
| 317 | struct kvm_assigned_dev_kernel *assigned_dev; |
| 318 | |
| 319 | list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) { |
| 320 | assigned_dev = list_entry(ptr, |
| 321 | struct kvm_assigned_dev_kernel, |
| 322 | list); |
| 323 | |
| 324 | kvm_free_assigned_device(kvm, assigned_dev); |
| 325 | } |
| 326 | } |
| 327 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 328 | static int assigned_device_enable_host_intx(struct kvm *kvm, |
| 329 | struct kvm_assigned_dev_kernel *dev) |
Sheng Yang | 00e3ed3 | 2008-11-24 14:32:50 +0800 | [diff] [blame] | 330 | { |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 331 | dev->host_irq = dev->dev->irq; |
| 332 | /* Even though this is PCI, we don't want to use shared |
| 333 | * interrupts. Sharing host devices with guest-assigned devices |
| 334 | * on the same interrupt line is not a happy situation: there |
| 335 | * are going to be long delays in accepting, acking, etc. |
| 336 | */ |
| 337 | if (request_irq(dev->host_irq, kvm_assigned_dev_intr, |
| 338 | 0, "kvm_assigned_intx_device", (void *)dev)) |
| 339 | return -EIO; |
Sheng Yang | 00e3ed3 | 2008-11-24 14:32:50 +0800 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 343 | #ifdef __KVM_HAVE_MSI |
| 344 | static int assigned_device_enable_host_msi(struct kvm *kvm, |
| 345 | struct kvm_assigned_dev_kernel *dev) |
Sheng Yang | 6b9cc7f | 2008-11-24 14:32:56 +0800 | [diff] [blame] | 346 | { |
| 347 | int r; |
| 348 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 349 | if (!dev->dev->msi_enabled) { |
| 350 | r = pci_enable_msi(dev->dev); |
| 351 | if (r) |
| 352 | return r; |
Sheng Yang | 5319c66 | 2008-11-24 14:32:57 +0800 | [diff] [blame] | 353 | } |
Sheng Yang | 6b9cc7f | 2008-11-24 14:32:56 +0800 | [diff] [blame] | 354 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 355 | dev->host_irq = dev->dev->irq; |
| 356 | if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0, |
| 357 | "kvm_assigned_msi_device", (void *)dev)) { |
| 358 | pci_disable_msi(dev->dev); |
| 359 | return -EIO; |
Sheng Yang | 6b9cc7f | 2008-11-24 14:32:56 +0800 | [diff] [blame] | 360 | } |
| 361 | |
Sheng Yang | 6b9cc7f | 2008-11-24 14:32:56 +0800 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | #endif |
| 365 | |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 366 | #ifdef __KVM_HAVE_MSIX |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 367 | static int assigned_device_enable_host_msix(struct kvm *kvm, |
| 368 | struct kvm_assigned_dev_kernel *dev) |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 369 | { |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 370 | int i, r = -EINVAL; |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 371 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 372 | /* host_msix_entries and guest_msix_entries should have been |
| 373 | * initialized */ |
| 374 | if (dev->entries_nr == 0) |
| 375 | return r; |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 376 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 377 | r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr); |
| 378 | if (r) |
| 379 | return r; |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 380 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 381 | for (i = 0; i < dev->entries_nr; i++) { |
| 382 | r = request_irq(dev->host_msix_entries[i].vector, |
| 383 | kvm_assigned_dev_intr, 0, |
| 384 | "kvm_assigned_msix_device", |
| 385 | (void *)dev); |
| 386 | /* FIXME: free requested_irq's on failure */ |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 387 | if (r) |
| 388 | return r; |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 389 | } |
| 390 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 391 | return 0; |
| 392 | } |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 393 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 394 | #endif |
| 395 | |
| 396 | static int assigned_device_enable_guest_intx(struct kvm *kvm, |
| 397 | struct kvm_assigned_dev_kernel *dev, |
| 398 | struct kvm_assigned_irq *irq) |
| 399 | { |
| 400 | dev->guest_irq = irq->guest_irq; |
| 401 | dev->ack_notifier.gsi = irq->guest_irq; |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | #ifdef __KVM_HAVE_MSI |
| 406 | static int assigned_device_enable_guest_msi(struct kvm *kvm, |
| 407 | struct kvm_assigned_dev_kernel *dev, |
| 408 | struct kvm_assigned_irq *irq) |
| 409 | { |
| 410 | dev->guest_irq = irq->guest_irq; |
| 411 | dev->ack_notifier.gsi = -1; |
Sheng Yang | 968a634 | 2009-04-30 10:58:42 +0800 | [diff] [blame] | 412 | dev->host_irq_disabled = false; |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | #endif |
| 416 | #ifdef __KVM_HAVE_MSIX |
| 417 | static int assigned_device_enable_guest_msix(struct kvm *kvm, |
| 418 | struct kvm_assigned_dev_kernel *dev, |
| 419 | struct kvm_assigned_irq *irq) |
| 420 | { |
| 421 | dev->guest_irq = irq->guest_irq; |
| 422 | dev->ack_notifier.gsi = -1; |
Sheng Yang | 968a634 | 2009-04-30 10:58:42 +0800 | [diff] [blame] | 423 | dev->host_irq_disabled = false; |
Sheng Yang | d510d6c | 2009-02-25 17:22:28 +0800 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | #endif |
| 427 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 428 | static int assign_host_irq(struct kvm *kvm, |
| 429 | struct kvm_assigned_dev_kernel *dev, |
| 430 | __u32 host_irq_type) |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 431 | { |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 432 | int r = -EEXIST; |
| 433 | |
| 434 | if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK) |
| 435 | return r; |
| 436 | |
| 437 | switch (host_irq_type) { |
| 438 | case KVM_DEV_IRQ_HOST_INTX: |
| 439 | r = assigned_device_enable_host_intx(kvm, dev); |
| 440 | break; |
| 441 | #ifdef __KVM_HAVE_MSI |
| 442 | case KVM_DEV_IRQ_HOST_MSI: |
| 443 | r = assigned_device_enable_host_msi(kvm, dev); |
| 444 | break; |
| 445 | #endif |
| 446 | #ifdef __KVM_HAVE_MSIX |
| 447 | case KVM_DEV_IRQ_HOST_MSIX: |
| 448 | r = assigned_device_enable_host_msix(kvm, dev); |
| 449 | break; |
| 450 | #endif |
| 451 | default: |
| 452 | r = -EINVAL; |
| 453 | } |
| 454 | |
| 455 | if (!r) |
| 456 | dev->irq_requested_type |= host_irq_type; |
| 457 | |
| 458 | return r; |
| 459 | } |
| 460 | |
| 461 | static int assign_guest_irq(struct kvm *kvm, |
| 462 | struct kvm_assigned_dev_kernel *dev, |
| 463 | struct kvm_assigned_irq *irq, |
| 464 | unsigned long guest_irq_type) |
| 465 | { |
| 466 | int id; |
| 467 | int r = -EEXIST; |
| 468 | |
| 469 | if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK) |
| 470 | return r; |
| 471 | |
| 472 | id = kvm_request_irq_source_id(kvm); |
| 473 | if (id < 0) |
| 474 | return id; |
| 475 | |
| 476 | dev->irq_source_id = id; |
| 477 | |
| 478 | switch (guest_irq_type) { |
| 479 | case KVM_DEV_IRQ_GUEST_INTX: |
| 480 | r = assigned_device_enable_guest_intx(kvm, dev, irq); |
| 481 | break; |
| 482 | #ifdef __KVM_HAVE_MSI |
| 483 | case KVM_DEV_IRQ_GUEST_MSI: |
| 484 | r = assigned_device_enable_guest_msi(kvm, dev, irq); |
| 485 | break; |
| 486 | #endif |
| 487 | #ifdef __KVM_HAVE_MSIX |
| 488 | case KVM_DEV_IRQ_GUEST_MSIX: |
| 489 | r = assigned_device_enable_guest_msix(kvm, dev, irq); |
| 490 | break; |
| 491 | #endif |
| 492 | default: |
| 493 | r = -EINVAL; |
| 494 | } |
| 495 | |
| 496 | if (!r) { |
| 497 | dev->irq_requested_type |= guest_irq_type; |
| 498 | kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier); |
| 499 | } else |
| 500 | kvm_free_irq_source_id(kvm, dev->irq_source_id); |
| 501 | |
| 502 | return r; |
| 503 | } |
| 504 | |
| 505 | /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */ |
| 506 | static int kvm_vm_ioctl_assign_irq(struct kvm *kvm, |
| 507 | struct kvm_assigned_irq *assigned_irq) |
| 508 | { |
| 509 | int r = -EINVAL; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 510 | struct kvm_assigned_dev_kernel *match; |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 511 | unsigned long host_irq_type, guest_irq_type; |
| 512 | |
| 513 | if (!capable(CAP_SYS_RAWIO)) |
| 514 | return -EPERM; |
| 515 | |
| 516 | if (!irqchip_in_kernel(kvm)) |
| 517 | return r; |
| 518 | |
| 519 | mutex_lock(&kvm->lock); |
| 520 | r = -ENODEV; |
| 521 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 522 | assigned_irq->assigned_dev_id); |
| 523 | if (!match) |
| 524 | goto out; |
| 525 | |
| 526 | host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK); |
| 527 | guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK); |
| 528 | |
| 529 | r = -EINVAL; |
| 530 | /* can only assign one type at a time */ |
| 531 | if (hweight_long(host_irq_type) > 1) |
| 532 | goto out; |
| 533 | if (hweight_long(guest_irq_type) > 1) |
| 534 | goto out; |
| 535 | if (host_irq_type == 0 && guest_irq_type == 0) |
| 536 | goto out; |
| 537 | |
| 538 | r = 0; |
| 539 | if (host_irq_type) |
| 540 | r = assign_host_irq(kvm, match, host_irq_type); |
| 541 | if (r) |
| 542 | goto out; |
| 543 | |
| 544 | if (guest_irq_type) |
| 545 | r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type); |
| 546 | out: |
| 547 | mutex_unlock(&kvm->lock); |
| 548 | return r; |
| 549 | } |
| 550 | |
| 551 | static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm, |
| 552 | struct kvm_assigned_irq |
| 553 | *assigned_irq) |
| 554 | { |
| 555 | int r = -ENODEV; |
| 556 | struct kvm_assigned_dev_kernel *match; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 557 | |
| 558 | mutex_lock(&kvm->lock); |
| 559 | |
| 560 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 561 | assigned_irq->assigned_dev_id); |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 562 | if (!match) |
| 563 | goto out; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 564 | |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 565 | r = kvm_deassign_irq(kvm, match, assigned_irq->flags); |
| 566 | out: |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 567 | mutex_unlock(&kvm->lock); |
| 568 | return r; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | static int kvm_vm_ioctl_assign_device(struct kvm *kvm, |
| 572 | struct kvm_assigned_pci_dev *assigned_dev) |
| 573 | { |
| 574 | int r = 0; |
| 575 | struct kvm_assigned_dev_kernel *match; |
| 576 | struct pci_dev *dev; |
| 577 | |
Mark McLoughlin | 682edb4 | 2009-02-05 18:23:46 +0000 | [diff] [blame] | 578 | down_read(&kvm->slots_lock); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 579 | mutex_lock(&kvm->lock); |
| 580 | |
| 581 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 582 | assigned_dev->assigned_dev_id); |
| 583 | if (match) { |
| 584 | /* device already assigned */ |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 585 | r = -EEXIST; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 586 | goto out; |
| 587 | } |
| 588 | |
| 589 | match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL); |
| 590 | if (match == NULL) { |
| 591 | printk(KERN_INFO "%s: Couldn't allocate memory\n", |
| 592 | __func__); |
| 593 | r = -ENOMEM; |
| 594 | goto out; |
| 595 | } |
| 596 | dev = pci_get_bus_and_slot(assigned_dev->busnr, |
| 597 | assigned_dev->devfn); |
| 598 | if (!dev) { |
| 599 | printk(KERN_INFO "%s: host device not found\n", __func__); |
| 600 | r = -EINVAL; |
| 601 | goto out_free; |
| 602 | } |
| 603 | if (pci_enable_device(dev)) { |
| 604 | printk(KERN_INFO "%s: Could not enable PCI device\n", __func__); |
| 605 | r = -EBUSY; |
| 606 | goto out_put; |
| 607 | } |
| 608 | r = pci_request_regions(dev, "kvm_assigned_device"); |
| 609 | if (r) { |
| 610 | printk(KERN_INFO "%s: Could not get access to device regions\n", |
| 611 | __func__); |
| 612 | goto out_disable; |
| 613 | } |
Sheng Yang | 6eb5581 | 2008-10-31 12:37:41 +0800 | [diff] [blame] | 614 | |
| 615 | pci_reset_function(dev); |
| 616 | |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 617 | match->assigned_dev_id = assigned_dev->assigned_dev_id; |
| 618 | match->host_busnr = assigned_dev->busnr; |
| 619 | match->host_devfn = assigned_dev->devfn; |
Weidong Han | b653574 | 2008-12-08 23:29:53 +0800 | [diff] [blame] | 620 | match->flags = assigned_dev->flags; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 621 | match->dev = dev; |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 622 | spin_lock_init(&match->assigned_dev_lock); |
Mark McLoughlin | f29b267 | 2008-12-01 13:57:47 +0000 | [diff] [blame] | 623 | match->irq_source_id = -1; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 624 | match->kvm = kvm; |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 625 | match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq; |
| 626 | INIT_WORK(&match->interrupt_work, |
| 627 | kvm_assigned_dev_interrupt_work_handler); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 628 | |
| 629 | list_add(&match->list, &kvm->arch.assigned_dev_head); |
| 630 | |
| 631 | if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) { |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 632 | if (!kvm->arch.iommu_domain) { |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 633 | r = kvm_iommu_map_guest(kvm); |
| 634 | if (r) |
| 635 | goto out_list_del; |
| 636 | } |
| 637 | r = kvm_assign_device(kvm, match); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 638 | if (r) |
| 639 | goto out_list_del; |
| 640 | } |
| 641 | |
| 642 | out: |
| 643 | mutex_unlock(&kvm->lock); |
Mark McLoughlin | 682edb4 | 2009-02-05 18:23:46 +0000 | [diff] [blame] | 644 | up_read(&kvm->slots_lock); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 645 | return r; |
| 646 | out_list_del: |
| 647 | list_del(&match->list); |
| 648 | pci_release_regions(dev); |
| 649 | out_disable: |
| 650 | pci_disable_device(dev); |
| 651 | out_put: |
| 652 | pci_dev_put(dev); |
| 653 | out_free: |
| 654 | kfree(match); |
| 655 | mutex_unlock(&kvm->lock); |
Mark McLoughlin | 682edb4 | 2009-02-05 18:23:46 +0000 | [diff] [blame] | 656 | up_read(&kvm->slots_lock); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 657 | return r; |
| 658 | } |
| 659 | #endif |
| 660 | |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 661 | #ifdef KVM_CAP_DEVICE_DEASSIGNMENT |
| 662 | static int kvm_vm_ioctl_deassign_device(struct kvm *kvm, |
| 663 | struct kvm_assigned_pci_dev *assigned_dev) |
| 664 | { |
| 665 | int r = 0; |
| 666 | struct kvm_assigned_dev_kernel *match; |
| 667 | |
| 668 | mutex_lock(&kvm->lock); |
| 669 | |
| 670 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 671 | assigned_dev->assigned_dev_id); |
| 672 | if (!match) { |
| 673 | printk(KERN_INFO "%s: device hasn't been assigned before, " |
| 674 | "so cannot be deassigned\n", __func__); |
| 675 | r = -EINVAL; |
| 676 | goto out; |
| 677 | } |
| 678 | |
Weidong Han | 4a906e4 | 2009-02-13 17:27:51 +0800 | [diff] [blame] | 679 | if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 680 | kvm_deassign_device(kvm, match); |
| 681 | |
| 682 | kvm_free_assigned_device(kvm, match); |
| 683 | |
| 684 | out: |
| 685 | mutex_unlock(&kvm->lock); |
| 686 | return r; |
| 687 | } |
| 688 | #endif |
| 689 | |
James Morris | 5aacf0c | 2006-12-22 01:04:55 -0800 | [diff] [blame] | 690 | static inline int valid_vcpu(int n) |
| 691 | { |
| 692 | return likely(n >= 0 && n < KVM_MAX_VCPUS); |
| 693 | } |
| 694 | |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 695 | inline int kvm_is_mmio_pfn(pfn_t pfn) |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame] | 696 | { |
Joerg Roedel | fc5659c | 2009-02-18 14:08:58 +0100 | [diff] [blame] | 697 | if (pfn_valid(pfn)) { |
| 698 | struct page *page = compound_head(pfn_to_page(pfn)); |
| 699 | return PageReserved(page); |
| 700 | } |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame] | 701 | |
| 702 | return true; |
| 703 | } |
| 704 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 705 | /* |
| 706 | * Switches to specified vcpu, until a matching vcpu_put() |
| 707 | */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 708 | void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 709 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 710 | int cpu; |
| 711 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 712 | mutex_lock(&vcpu->mutex); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 713 | cpu = get_cpu(); |
| 714 | preempt_notifier_register(&vcpu->preempt_notifier); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 715 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 716 | put_cpu(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 719 | void vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 720 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 721 | preempt_disable(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 722 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 723 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
| 724 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 725 | mutex_unlock(&vcpu->mutex); |
| 726 | } |
| 727 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 728 | static void ack_flush(void *_completed) |
| 729 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 730 | } |
| 731 | |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 732 | static bool make_all_cpus_request(struct kvm *kvm, unsigned int req) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 733 | { |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 734 | int i, cpu, me; |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 735 | cpumask_var_t cpus; |
| 736 | bool called = true; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 737 | struct kvm_vcpu *vcpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 738 | |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 739 | if (alloc_cpumask_var(&cpus, GFP_ATOMIC)) |
| 740 | cpumask_clear(cpus); |
| 741 | |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 742 | me = get_cpu(); |
Marcelo Tosatti | 8426192 | 2009-06-17 10:53:47 -0300 | [diff] [blame] | 743 | spin_lock(&kvm->requests_lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 744 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 745 | vcpu = kvm->vcpus[i]; |
| 746 | if (!vcpu) |
| 747 | continue; |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 748 | if (test_and_set_bit(req, &vcpu->requests)) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 749 | continue; |
| 750 | cpu = vcpu->cpu; |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 751 | if (cpus != NULL && cpu != -1 && cpu != me) |
| 752 | cpumask_set_cpu(cpu, cpus); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 753 | } |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 754 | if (unlikely(cpus == NULL)) |
| 755 | smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1); |
| 756 | else if (!cpumask_empty(cpus)) |
| 757 | smp_call_function_many(cpus, ack_flush, NULL, 1); |
| 758 | else |
| 759 | called = false; |
Marcelo Tosatti | 8426192 | 2009-06-17 10:53:47 -0300 | [diff] [blame] | 760 | spin_unlock(&kvm->requests_lock); |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 761 | put_cpu(); |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 762 | free_cpumask_var(cpus); |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 763 | return called; |
| 764 | } |
| 765 | |
| 766 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
| 767 | { |
| 768 | if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH)) |
| 769 | ++kvm->stat.remote_tlb_flush; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 770 | } |
| 771 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 772 | void kvm_reload_remote_mmus(struct kvm *kvm) |
| 773 | { |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 774 | make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 775 | } |
| 776 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 777 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
| 778 | { |
| 779 | struct page *page; |
| 780 | int r; |
| 781 | |
| 782 | mutex_init(&vcpu->mutex); |
| 783 | vcpu->cpu = -1; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 784 | vcpu->kvm = kvm; |
| 785 | vcpu->vcpu_id = id; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 786 | init_waitqueue_head(&vcpu->wq); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 787 | |
| 788 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 789 | if (!page) { |
| 790 | r = -ENOMEM; |
| 791 | goto fail; |
| 792 | } |
| 793 | vcpu->run = page_address(page); |
| 794 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 795 | r = kvm_arch_vcpu_init(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 796 | if (r < 0) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 797 | goto fail_free_run; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 798 | return 0; |
| 799 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 800 | fail_free_run: |
| 801 | free_page((unsigned long)vcpu->run); |
| 802 | fail: |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 803 | return r; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 804 | } |
| 805 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); |
| 806 | |
| 807 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 808 | { |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 809 | kvm_arch_vcpu_uninit(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 810 | free_page((unsigned long)vcpu->run); |
| 811 | } |
| 812 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); |
| 813 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 814 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 815 | static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) |
| 816 | { |
| 817 | return container_of(mn, struct kvm, mmu_notifier); |
| 818 | } |
| 819 | |
| 820 | static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, |
| 821 | struct mm_struct *mm, |
| 822 | unsigned long address) |
| 823 | { |
| 824 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 825 | int need_tlb_flush; |
| 826 | |
| 827 | /* |
| 828 | * When ->invalidate_page runs, the linux pte has been zapped |
| 829 | * already but the page is still allocated until |
| 830 | * ->invalidate_page returns. So if we increase the sequence |
| 831 | * here the kvm page fault will notice if the spte can't be |
| 832 | * established because the page is going to be freed. If |
| 833 | * instead the kvm page fault establishes the spte before |
| 834 | * ->invalidate_page runs, kvm_unmap_hva will release it |
| 835 | * before returning. |
| 836 | * |
| 837 | * The sequence increase only need to be seen at spin_unlock |
| 838 | * time, and not at spin_lock time. |
| 839 | * |
| 840 | * Increasing the sequence after the spin_unlock would be |
| 841 | * unsafe because the kvm page fault could then establish the |
| 842 | * pte after kvm_unmap_hva returned, without noticing the page |
| 843 | * is going to be freed. |
| 844 | */ |
| 845 | spin_lock(&kvm->mmu_lock); |
| 846 | kvm->mmu_notifier_seq++; |
| 847 | need_tlb_flush = kvm_unmap_hva(kvm, address); |
| 848 | spin_unlock(&kvm->mmu_lock); |
| 849 | |
| 850 | /* we've to flush the tlb before the pages can be freed */ |
| 851 | if (need_tlb_flush) |
| 852 | kvm_flush_remote_tlbs(kvm); |
| 853 | |
| 854 | } |
| 855 | |
| 856 | static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, |
| 857 | struct mm_struct *mm, |
| 858 | unsigned long start, |
| 859 | unsigned long end) |
| 860 | { |
| 861 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 862 | int need_tlb_flush = 0; |
| 863 | |
| 864 | spin_lock(&kvm->mmu_lock); |
| 865 | /* |
| 866 | * The count increase must become visible at unlock time as no |
| 867 | * spte can be established without taking the mmu_lock and |
| 868 | * count is also read inside the mmu_lock critical section. |
| 869 | */ |
| 870 | kvm->mmu_notifier_count++; |
| 871 | for (; start < end; start += PAGE_SIZE) |
| 872 | need_tlb_flush |= kvm_unmap_hva(kvm, start); |
| 873 | spin_unlock(&kvm->mmu_lock); |
| 874 | |
| 875 | /* we've to flush the tlb before the pages can be freed */ |
| 876 | if (need_tlb_flush) |
| 877 | kvm_flush_remote_tlbs(kvm); |
| 878 | } |
| 879 | |
| 880 | static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, |
| 881 | struct mm_struct *mm, |
| 882 | unsigned long start, |
| 883 | unsigned long end) |
| 884 | { |
| 885 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 886 | |
| 887 | spin_lock(&kvm->mmu_lock); |
| 888 | /* |
| 889 | * This sequence increase will notify the kvm page fault that |
| 890 | * the page that is going to be mapped in the spte could have |
| 891 | * been freed. |
| 892 | */ |
| 893 | kvm->mmu_notifier_seq++; |
| 894 | /* |
| 895 | * The above sequence increase must be visible before the |
| 896 | * below count decrease but both values are read by the kvm |
| 897 | * page fault under mmu_lock spinlock so we don't need to add |
| 898 | * a smb_wmb() here in between the two. |
| 899 | */ |
| 900 | kvm->mmu_notifier_count--; |
| 901 | spin_unlock(&kvm->mmu_lock); |
| 902 | |
| 903 | BUG_ON(kvm->mmu_notifier_count < 0); |
| 904 | } |
| 905 | |
| 906 | static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, |
| 907 | struct mm_struct *mm, |
| 908 | unsigned long address) |
| 909 | { |
| 910 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 911 | int young; |
| 912 | |
| 913 | spin_lock(&kvm->mmu_lock); |
| 914 | young = kvm_age_hva(kvm, address); |
| 915 | spin_unlock(&kvm->mmu_lock); |
| 916 | |
| 917 | if (young) |
| 918 | kvm_flush_remote_tlbs(kvm); |
| 919 | |
| 920 | return young; |
| 921 | } |
| 922 | |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 923 | static void kvm_mmu_notifier_release(struct mmu_notifier *mn, |
| 924 | struct mm_struct *mm) |
| 925 | { |
| 926 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 927 | kvm_arch_flush_shadow(kvm); |
| 928 | } |
| 929 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 930 | static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { |
| 931 | .invalidate_page = kvm_mmu_notifier_invalidate_page, |
| 932 | .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, |
| 933 | .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, |
| 934 | .clear_flush_young = kvm_mmu_notifier_clear_flush_young, |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 935 | .release = kvm_mmu_notifier_release, |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 936 | }; |
| 937 | #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ |
| 938 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 939 | static struct kvm *kvm_create_vm(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 940 | { |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 941 | struct kvm *kvm = kvm_arch_create_vm(); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 942 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 943 | struct page *page; |
| 944 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 945 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 946 | if (IS_ERR(kvm)) |
| 947 | goto out; |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 948 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 949 | INIT_LIST_HEAD(&kvm->irq_routing); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 950 | INIT_HLIST_HEAD(&kvm->mask_notifier_list); |
| 951 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 952 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 953 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 954 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 955 | if (!page) { |
| 956 | kfree(kvm); |
| 957 | return ERR_PTR(-ENOMEM); |
| 958 | } |
| 959 | kvm->coalesced_mmio_ring = |
| 960 | (struct kvm_coalesced_mmio_ring *)page_address(page); |
| 961 | #endif |
| 962 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 963 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 964 | { |
| 965 | int err; |
| 966 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; |
| 967 | err = mmu_notifier_register(&kvm->mmu_notifier, current->mm); |
| 968 | if (err) { |
| 969 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 970 | put_page(page); |
| 971 | #endif |
| 972 | kfree(kvm); |
| 973 | return ERR_PTR(err); |
| 974 | } |
| 975 | } |
| 976 | #endif |
| 977 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 978 | kvm->mm = current->mm; |
| 979 | atomic_inc(&kvm->mm->mm_count); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 980 | spin_lock_init(&kvm->mmu_lock); |
Marcelo Tosatti | 8426192 | 2009-06-17 10:53:47 -0300 | [diff] [blame] | 981 | spin_lock_init(&kvm->requests_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 982 | kvm_io_bus_init(&kvm->pio_bus); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 983 | kvm_irqfd_init(kvm); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 984 | mutex_init(&kvm->lock); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 985 | kvm_io_bus_init(&kvm->mmio_bus); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 986 | init_rwsem(&kvm->slots_lock); |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 987 | atomic_set(&kvm->users_count, 1); |
Rusty Russell | 5e58cfe | 2007-07-23 17:08:21 +1000 | [diff] [blame] | 988 | spin_lock(&kvm_lock); |
| 989 | list_add(&kvm->vm_list, &vm_list); |
| 990 | spin_unlock(&kvm_lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 991 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 992 | kvm_coalesced_mmio_init(kvm); |
| 993 | #endif |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 994 | out: |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 995 | return kvm; |
| 996 | } |
| 997 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 998 | /* |
| 999 | * Free any memory in @free but not in @dont. |
| 1000 | */ |
| 1001 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 1002 | struct kvm_memory_slot *dont) |
| 1003 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1004 | if (!dont || free->rmap != dont->rmap) |
| 1005 | vfree(free->rmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1006 | |
| 1007 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
| 1008 | vfree(free->dirty_bitmap); |
| 1009 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1010 | if (!dont || free->lpage_info != dont->lpage_info) |
| 1011 | vfree(free->lpage_info); |
| 1012 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1013 | free->npages = 0; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1014 | free->dirty_bitmap = NULL; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 1015 | free->rmap = NULL; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1016 | free->lpage_info = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 1019 | void kvm_free_physmem(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1020 | { |
| 1021 | int i; |
| 1022 | |
| 1023 | for (i = 0; i < kvm->nmemslots; ++i) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1024 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1027 | static void kvm_destroy_vm(struct kvm *kvm) |
| 1028 | { |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 1029 | struct mm_struct *mm = kvm->mm; |
| 1030 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 1031 | kvm_arch_sync_events(kvm); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 1032 | spin_lock(&kvm_lock); |
| 1033 | list_del(&kvm->vm_list); |
| 1034 | spin_unlock(&kvm_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 1035 | kvm_free_irq_routing(kvm); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 1036 | kvm_io_bus_destroy(&kvm->pio_bus); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 1037 | kvm_io_bus_destroy(&kvm->mmio_bus); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1038 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 1039 | if (kvm->coalesced_mmio_ring != NULL) |
| 1040 | free_page((unsigned long)kvm->coalesced_mmio_ring); |
| 1041 | #endif |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1042 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 1043 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); |
Gleb Natapov | f00be0c | 2009-03-19 12:20:36 +0200 | [diff] [blame] | 1044 | #else |
| 1045 | kvm_arch_flush_shadow(kvm); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1046 | #endif |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 1047 | kvm_arch_destroy_vm(kvm); |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 1048 | mmdrop(mm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 1051 | void kvm_get_kvm(struct kvm *kvm) |
| 1052 | { |
| 1053 | atomic_inc(&kvm->users_count); |
| 1054 | } |
| 1055 | EXPORT_SYMBOL_GPL(kvm_get_kvm); |
| 1056 | |
| 1057 | void kvm_put_kvm(struct kvm *kvm) |
| 1058 | { |
| 1059 | if (atomic_dec_and_test(&kvm->users_count)) |
| 1060 | kvm_destroy_vm(kvm); |
| 1061 | } |
| 1062 | EXPORT_SYMBOL_GPL(kvm_put_kvm); |
| 1063 | |
| 1064 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1065 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 1066 | { |
| 1067 | struct kvm *kvm = filp->private_data; |
| 1068 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 1069 | kvm_irqfd_release(kvm); |
| 1070 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 1071 | kvm_put_kvm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1075 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1076 | * Allocate some memory and give it an address in the guest physical address |
| 1077 | * space. |
| 1078 | * |
| 1079 | * Discontiguous memory is allowed, mostly for framebuffers. |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1080 | * |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1081 | * Must be called holding mmap_sem for write. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1082 | */ |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1083 | int __kvm_set_memory_region(struct kvm *kvm, |
| 1084 | struct kvm_userspace_memory_region *mem, |
| 1085 | int user_alloc) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1086 | { |
| 1087 | int r; |
| 1088 | gfn_t base_gfn; |
Avi Kivity | ac04527 | 2009-06-08 15:52:39 +0300 | [diff] [blame] | 1089 | unsigned long npages, ugfn; |
Avi Kivity | 09f8ca74 | 2009-06-08 15:55:21 +0300 | [diff] [blame] | 1090 | unsigned long largepages, i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1091 | struct kvm_memory_slot *memslot; |
| 1092 | struct kvm_memory_slot old, new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1093 | |
| 1094 | r = -EINVAL; |
| 1095 | /* General sanity checks */ |
| 1096 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 1097 | goto out; |
| 1098 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 1099 | goto out; |
Sheng Yang | e7cacd4 | 2008-11-11 15:30:40 +0800 | [diff] [blame] | 1100 | if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1))) |
Hollis Blanchard | 7874980 | 2008-11-07 13:32:12 -0600 | [diff] [blame] | 1101 | goto out; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1102 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1103 | goto out; |
| 1104 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 1105 | goto out; |
| 1106 | |
| 1107 | memslot = &kvm->memslots[mem->slot]; |
| 1108 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 1109 | npages = mem->memory_size >> PAGE_SHIFT; |
| 1110 | |
| 1111 | if (!npages) |
| 1112 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 1113 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1114 | new = old = *memslot; |
| 1115 | |
| 1116 | new.base_gfn = base_gfn; |
| 1117 | new.npages = npages; |
| 1118 | new.flags = mem->flags; |
| 1119 | |
| 1120 | /* Disallow changing a memory slot's size. */ |
| 1121 | r = -EINVAL; |
| 1122 | if (npages && old.npages && npages != old.npages) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1123 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1124 | |
| 1125 | /* Check for overlaps */ |
| 1126 | r = -EEXIST; |
| 1127 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 1128 | struct kvm_memory_slot *s = &kvm->memslots[i]; |
| 1129 | |
Jan Kiszka | 4cd481f | 2009-04-13 11:59:32 +0200 | [diff] [blame] | 1130 | if (s == memslot || !s->npages) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1131 | continue; |
| 1132 | if (!((base_gfn + npages <= s->base_gfn) || |
| 1133 | (base_gfn >= s->base_gfn + s->npages))) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1134 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1135 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1136 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1137 | /* Free page dirty bitmap if unneeded */ |
| 1138 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1139 | new.dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1140 | |
| 1141 | r = -ENOMEM; |
| 1142 | |
| 1143 | /* Allocate if a slot is being created */ |
Carsten Otte | eff0114 | 2008-06-27 15:05:31 +0200 | [diff] [blame] | 1144 | #ifndef CONFIG_S390 |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 1145 | if (npages && !new.rmap) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1146 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1147 | |
| 1148 | if (!new.rmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1149 | goto out_free; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1150 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1151 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 1152 | |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 1153 | new.user_alloc = user_alloc; |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 1154 | /* |
| 1155 | * hva_to_rmmap() serialzies with the mmu_lock and to be |
| 1156 | * safe it has to ignore memslots with !user_alloc && |
| 1157 | * !userspace_addr. |
| 1158 | */ |
| 1159 | if (user_alloc) |
| 1160 | new.userspace_addr = mem->userspace_addr; |
| 1161 | else |
| 1162 | new.userspace_addr = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1163 | } |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1164 | if (npages && !new.lpage_info) { |
Avi Kivity | 99894a7 | 2009-03-29 16:31:25 +0300 | [diff] [blame] | 1165 | largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE; |
| 1166 | largepages -= base_gfn / KVM_PAGES_PER_HPAGE; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1167 | |
| 1168 | new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info)); |
| 1169 | |
| 1170 | if (!new.lpage_info) |
| 1171 | goto out_free; |
| 1172 | |
| 1173 | memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info)); |
| 1174 | |
| 1175 | if (base_gfn % KVM_PAGES_PER_HPAGE) |
| 1176 | new.lpage_info[0].write_count = 1; |
| 1177 | if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE) |
| 1178 | new.lpage_info[largepages-1].write_count = 1; |
Avi Kivity | ac04527 | 2009-06-08 15:52:39 +0300 | [diff] [blame] | 1179 | ugfn = new.userspace_addr >> PAGE_SHIFT; |
| 1180 | /* |
| 1181 | * If the gfn and userspace address are not aligned wrt each |
| 1182 | * other, disable large page support for this slot |
| 1183 | */ |
| 1184 | if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1)) |
| 1185 | for (i = 0; i < largepages; ++i) |
| 1186 | new.lpage_info[i].write_count = 1; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1187 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1188 | |
| 1189 | /* Allocate page dirty bitmap if needed */ |
| 1190 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
| 1191 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; |
| 1192 | |
| 1193 | new.dirty_bitmap = vmalloc(dirty_bytes); |
| 1194 | if (!new.dirty_bitmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1195 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1196 | memset(new.dirty_bitmap, 0, dirty_bytes); |
Izik Eidus | e244584 | 2009-06-10 19:23:24 +0300 | [diff] [blame] | 1197 | if (old.npages) |
| 1198 | kvm_arch_flush_shadow(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1199 | } |
Carsten Otte | eff0114 | 2008-06-27 15:05:31 +0200 | [diff] [blame] | 1200 | #endif /* not defined CONFIG_S390 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1201 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 1202 | if (!npages) |
| 1203 | kvm_arch_flush_shadow(kvm); |
| 1204 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 1205 | spin_lock(&kvm->mmu_lock); |
| 1206 | if (mem->slot >= kvm->nmemslots) |
| 1207 | kvm->nmemslots = mem->slot + 1; |
| 1208 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1209 | *memslot = new; |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 1210 | spin_unlock(&kvm->mmu_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1211 | |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 1212 | r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc); |
| 1213 | if (r) { |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 1214 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 1215 | *memslot = old; |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 1216 | spin_unlock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 1217 | goto out_free; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 1218 | } |
| 1219 | |
Glauber Costa | 6f89724 | 2008-12-03 13:40:51 -0200 | [diff] [blame] | 1220 | kvm_free_physmem_slot(&old, npages ? &new : NULL); |
| 1221 | /* Slot deletion case: we have to update the current slot */ |
Marcelo Tosatti | b43b190 | 2009-05-12 18:55:44 -0300 | [diff] [blame] | 1222 | spin_lock(&kvm->mmu_lock); |
Glauber Costa | 6f89724 | 2008-12-03 13:40:51 -0200 | [diff] [blame] | 1223 | if (!npages) |
| 1224 | *memslot = old; |
Marcelo Tosatti | b43b190 | 2009-05-12 18:55:44 -0300 | [diff] [blame] | 1225 | spin_unlock(&kvm->mmu_lock); |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 1226 | #ifdef CONFIG_DMAR |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1227 | /* map the pages in iommu page table */ |
| 1228 | r = kvm_iommu_map_pages(kvm, base_gfn, npages); |
| 1229 | if (r) |
| 1230 | goto out; |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 1231 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1232 | return 0; |
| 1233 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1234 | out_free: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1235 | kvm_free_physmem_slot(&new, &old); |
| 1236 | out: |
| 1237 | return r; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 1238 | |
| 1239 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1240 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
| 1241 | |
| 1242 | int kvm_set_memory_region(struct kvm *kvm, |
| 1243 | struct kvm_userspace_memory_region *mem, |
| 1244 | int user_alloc) |
| 1245 | { |
| 1246 | int r; |
| 1247 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1248 | down_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1249 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1250 | up_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1251 | return r; |
| 1252 | } |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 1253 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
| 1254 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1255 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 1256 | struct |
| 1257 | kvm_userspace_memory_region *mem, |
| 1258 | int user_alloc) |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 1259 | { |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1260 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 1261 | return -EINVAL; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 1262 | return kvm_set_memory_region(kvm, mem, user_alloc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1265 | int kvm_get_dirty_log(struct kvm *kvm, |
| 1266 | struct kvm_dirty_log *log, int *is_dirty) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1267 | { |
| 1268 | struct kvm_memory_slot *memslot; |
| 1269 | int r, i; |
| 1270 | int n; |
| 1271 | unsigned long any = 0; |
| 1272 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1273 | r = -EINVAL; |
| 1274 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 1275 | goto out; |
| 1276 | |
| 1277 | memslot = &kvm->memslots[log->slot]; |
| 1278 | r = -ENOENT; |
| 1279 | if (!memslot->dirty_bitmap) |
| 1280 | goto out; |
| 1281 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 1282 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1283 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 1284 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1285 | any = memslot->dirty_bitmap[i]; |
| 1286 | |
| 1287 | r = -EFAULT; |
| 1288 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 1289 | goto out; |
| 1290 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1291 | if (any) |
| 1292 | *is_dirty = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1293 | |
| 1294 | r = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1295 | out: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1296 | return r; |
| 1297 | } |
| 1298 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1299 | int is_error_page(struct page *page) |
| 1300 | { |
| 1301 | return page == bad_page; |
| 1302 | } |
| 1303 | EXPORT_SYMBOL_GPL(is_error_page); |
| 1304 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1305 | int is_error_pfn(pfn_t pfn) |
| 1306 | { |
| 1307 | return pfn == bad_pfn; |
| 1308 | } |
| 1309 | EXPORT_SYMBOL_GPL(is_error_pfn); |
| 1310 | |
Izik Eidus | f9d46eb | 2007-11-11 22:02:22 +0200 | [diff] [blame] | 1311 | static inline unsigned long bad_hva(void) |
| 1312 | { |
| 1313 | return PAGE_OFFSET; |
| 1314 | } |
| 1315 | |
| 1316 | int kvm_is_error_hva(unsigned long addr) |
| 1317 | { |
| 1318 | return addr == bad_hva(); |
| 1319 | } |
| 1320 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); |
| 1321 | |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 1322 | struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1323 | { |
| 1324 | int i; |
| 1325 | |
| 1326 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 1327 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 1328 | |
| 1329 | if (gfn >= memslot->base_gfn |
| 1330 | && gfn < memslot->base_gfn + memslot->npages) |
| 1331 | return memslot; |
| 1332 | } |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1333 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1334 | } |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 1335 | EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased); |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 1336 | |
| 1337 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 1338 | { |
| 1339 | gfn = unalias_gfn(kvm, gfn); |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 1340 | return gfn_to_memslot_unaliased(kvm, gfn); |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 1341 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1342 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1343 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
| 1344 | { |
| 1345 | int i; |
| 1346 | |
| 1347 | gfn = unalias_gfn(kvm, gfn); |
| 1348 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 1349 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 1350 | |
| 1351 | if (gfn >= memslot->base_gfn |
| 1352 | && gfn < memslot->base_gfn + memslot->npages) |
| 1353 | return 1; |
| 1354 | } |
| 1355 | return 0; |
| 1356 | } |
| 1357 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 1358 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1359 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1360 | { |
| 1361 | struct kvm_memory_slot *slot; |
| 1362 | |
| 1363 | gfn = unalias_gfn(kvm, gfn); |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 1364 | slot = gfn_to_memslot_unaliased(kvm, gfn); |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1365 | if (!slot) |
| 1366 | return bad_hva(); |
| 1367 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); |
| 1368 | } |
Sheng Yang | 0d15029 | 2008-04-25 21:44:50 +0800 | [diff] [blame] | 1369 | EXPORT_SYMBOL_GPL(gfn_to_hva); |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1370 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1371 | pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1372 | { |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 1373 | struct page *page[1]; |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1374 | unsigned long addr; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 1375 | int npages; |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1376 | pfn_t pfn; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1377 | |
Avi Kivity | 6039522 | 2007-10-21 11:03:36 +0200 | [diff] [blame] | 1378 | might_sleep(); |
| 1379 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1380 | addr = gfn_to_hva(kvm, gfn); |
| 1381 | if (kvm_is_error_hva(addr)) { |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1382 | get_page(bad_page); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1383 | return page_to_pfn(bad_page); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1384 | } |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1385 | |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 1386 | npages = get_user_pages_fast(addr, 1, 1, page); |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1387 | |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1388 | if (unlikely(npages != 1)) { |
| 1389 | struct vm_area_struct *vma; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 1390 | |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 1391 | down_read(¤t->mm->mmap_sem); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1392 | vma = find_vma(current->mm, addr); |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 1393 | |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1394 | if (vma == NULL || addr < vma->vm_start || |
| 1395 | !(vma->vm_flags & VM_PFNMAP)) { |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 1396 | up_read(¤t->mm->mmap_sem); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1397 | get_page(bad_page); |
| 1398 | return page_to_pfn(bad_page); |
| 1399 | } |
| 1400 | |
| 1401 | pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 1402 | up_read(¤t->mm->mmap_sem); |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1403 | BUG_ON(!kvm_is_mmio_pfn(pfn)); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1404 | } else |
| 1405 | pfn = page_to_pfn(page[0]); |
| 1406 | |
| 1407 | return pfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | EXPORT_SYMBOL_GPL(gfn_to_pfn); |
| 1411 | |
| 1412 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
| 1413 | { |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1414 | pfn_t pfn; |
| 1415 | |
| 1416 | pfn = gfn_to_pfn(kvm, gfn); |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1417 | if (!kvm_is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1418 | return pfn_to_page(pfn); |
| 1419 | |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1420 | WARN_ON(kvm_is_mmio_pfn(pfn)); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1421 | |
| 1422 | get_page(bad_page); |
| 1423 | return bad_page; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1424 | } |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 1425 | |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1426 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 1427 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1428 | void kvm_release_page_clean(struct page *page) |
| 1429 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1430 | kvm_release_pfn_clean(page_to_pfn(page)); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1431 | } |
| 1432 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); |
| 1433 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1434 | void kvm_release_pfn_clean(pfn_t pfn) |
| 1435 | { |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1436 | if (!kvm_is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1437 | put_page(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1438 | } |
| 1439 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); |
| 1440 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1441 | void kvm_release_page_dirty(struct page *page) |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1442 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1443 | kvm_release_pfn_dirty(page_to_pfn(page)); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1444 | } |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1445 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1446 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1447 | void kvm_release_pfn_dirty(pfn_t pfn) |
| 1448 | { |
| 1449 | kvm_set_pfn_dirty(pfn); |
| 1450 | kvm_release_pfn_clean(pfn); |
| 1451 | } |
| 1452 | EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); |
| 1453 | |
| 1454 | void kvm_set_page_dirty(struct page *page) |
| 1455 | { |
| 1456 | kvm_set_pfn_dirty(page_to_pfn(page)); |
| 1457 | } |
| 1458 | EXPORT_SYMBOL_GPL(kvm_set_page_dirty); |
| 1459 | |
| 1460 | void kvm_set_pfn_dirty(pfn_t pfn) |
| 1461 | { |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1462 | if (!kvm_is_mmio_pfn(pfn)) { |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1463 | struct page *page = pfn_to_page(pfn); |
| 1464 | if (!PageReserved(page)) |
| 1465 | SetPageDirty(page); |
| 1466 | } |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1467 | } |
| 1468 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
| 1469 | |
| 1470 | void kvm_set_pfn_accessed(pfn_t pfn) |
| 1471 | { |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1472 | if (!kvm_is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1473 | mark_page_accessed(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1474 | } |
| 1475 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); |
| 1476 | |
| 1477 | void kvm_get_pfn(pfn_t pfn) |
| 1478 | { |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1479 | if (!kvm_is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1480 | get_page(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1481 | } |
| 1482 | EXPORT_SYMBOL_GPL(kvm_get_pfn); |
| 1483 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1484 | static int next_segment(unsigned long len, int offset) |
| 1485 | { |
| 1486 | if (len > PAGE_SIZE - offset) |
| 1487 | return PAGE_SIZE - offset; |
| 1488 | else |
| 1489 | return len; |
| 1490 | } |
| 1491 | |
| 1492 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 1493 | int len) |
| 1494 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1495 | int r; |
| 1496 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1497 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1498 | addr = gfn_to_hva(kvm, gfn); |
| 1499 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1500 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1501 | r = copy_from_user(data, (void __user *)addr + offset, len); |
| 1502 | if (r) |
| 1503 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1504 | return 0; |
| 1505 | } |
| 1506 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
| 1507 | |
| 1508 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
| 1509 | { |
| 1510 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1511 | int seg; |
| 1512 | int offset = offset_in_page(gpa); |
| 1513 | int ret; |
| 1514 | |
| 1515 | while ((seg = next_segment(len, offset)) != 0) { |
| 1516 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); |
| 1517 | if (ret < 0) |
| 1518 | return ret; |
| 1519 | offset = 0; |
| 1520 | len -= seg; |
| 1521 | data += seg; |
| 1522 | ++gfn; |
| 1523 | } |
| 1524 | return 0; |
| 1525 | } |
| 1526 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 1527 | |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 1528 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
| 1529 | unsigned long len) |
| 1530 | { |
| 1531 | int r; |
| 1532 | unsigned long addr; |
| 1533 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1534 | int offset = offset_in_page(gpa); |
| 1535 | |
| 1536 | addr = gfn_to_hva(kvm, gfn); |
| 1537 | if (kvm_is_error_hva(addr)) |
| 1538 | return -EFAULT; |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 1539 | pagefault_disable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 1540 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 1541 | pagefault_enable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 1542 | if (r) |
| 1543 | return -EFAULT; |
| 1544 | return 0; |
| 1545 | } |
| 1546 | EXPORT_SYMBOL(kvm_read_guest_atomic); |
| 1547 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1548 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 1549 | int offset, int len) |
| 1550 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1551 | int r; |
| 1552 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1553 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1554 | addr = gfn_to_hva(kvm, gfn); |
| 1555 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1556 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1557 | r = copy_to_user((void __user *)addr + offset, data, len); |
| 1558 | if (r) |
| 1559 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1560 | mark_page_dirty(kvm, gfn); |
| 1561 | return 0; |
| 1562 | } |
| 1563 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
| 1564 | |
| 1565 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 1566 | unsigned long len) |
| 1567 | { |
| 1568 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1569 | int seg; |
| 1570 | int offset = offset_in_page(gpa); |
| 1571 | int ret; |
| 1572 | |
| 1573 | while ((seg = next_segment(len, offset)) != 0) { |
| 1574 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); |
| 1575 | if (ret < 0) |
| 1576 | return ret; |
| 1577 | offset = 0; |
| 1578 | len -= seg; |
| 1579 | data += seg; |
| 1580 | ++gfn; |
| 1581 | } |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
| 1586 | { |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 1587 | return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1588 | } |
| 1589 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); |
| 1590 | |
| 1591 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) |
| 1592 | { |
| 1593 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1594 | int seg; |
| 1595 | int offset = offset_in_page(gpa); |
| 1596 | int ret; |
| 1597 | |
| 1598 | while ((seg = next_segment(len, offset)) != 0) { |
| 1599 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
| 1600 | if (ret < 0) |
| 1601 | return ret; |
| 1602 | offset = 0; |
| 1603 | len -= seg; |
| 1604 | ++gfn; |
| 1605 | } |
| 1606 | return 0; |
| 1607 | } |
| 1608 | EXPORT_SYMBOL_GPL(kvm_clear_guest); |
| 1609 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1610 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 1611 | { |
Nguyen Anh Quynh | 3138994 | 2007-06-05 10:35:19 +0300 | [diff] [blame] | 1612 | struct kvm_memory_slot *memslot; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1613 | |
Uri Lublin | 3b6fff1 | 2007-10-30 10:42:09 +0200 | [diff] [blame] | 1614 | gfn = unalias_gfn(kvm, gfn); |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 1615 | memslot = gfn_to_memslot_unaliased(kvm, gfn); |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 1616 | if (memslot && memslot->dirty_bitmap) { |
| 1617 | unsigned long rel_gfn = gfn - memslot->base_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1618 | |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 1619 | /* avoid RMW */ |
| 1620 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) |
| 1621 | set_bit(rel_gfn, memslot->dirty_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1625 | /* |
| 1626 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. |
| 1627 | */ |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 1628 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1629 | { |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1630 | DEFINE_WAIT(wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1631 | |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1632 | for (;;) { |
| 1633 | prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1634 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 1635 | if ((kvm_arch_interrupt_allowed(vcpu) && |
| 1636 | kvm_cpu_has_interrupt(vcpu)) || |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 1637 | kvm_arch_vcpu_runnable(vcpu)) { |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 1638 | set_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1639 | break; |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 1640 | } |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 1641 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 1642 | break; |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1643 | if (signal_pending(current)) |
| 1644 | break; |
| 1645 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1646 | vcpu_put(vcpu); |
| 1647 | schedule(); |
| 1648 | vcpu_load(vcpu); |
| 1649 | } |
| 1650 | |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1651 | finish_wait(&vcpu->wq, &wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1652 | } |
| 1653 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1654 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 1655 | { |
Yaozu Dong | 3fca036 | 2007-04-25 16:49:19 +0300 | [diff] [blame] | 1656 | if (!need_resched()) |
| 1657 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1658 | cond_resched(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1659 | } |
| 1660 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 1661 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1662 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1663 | { |
| 1664 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1665 | struct page *page; |
| 1666 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1667 | if (vmf->pgoff == 0) |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1668 | page = virt_to_page(vcpu->run); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 1669 | #ifdef CONFIG_X86 |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1670 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1671 | page = virt_to_page(vcpu->arch.pio_data); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 1672 | #endif |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1673 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 1674 | else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET) |
| 1675 | page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); |
| 1676 | #endif |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1677 | else |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1678 | return VM_FAULT_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1679 | get_page(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1680 | vmf->page = page; |
| 1681 | return 0; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | static struct vm_operations_struct kvm_vcpu_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1685 | .fault = kvm_vcpu_fault, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1686 | }; |
| 1687 | |
| 1688 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 1689 | { |
| 1690 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1694 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 1695 | { |
| 1696 | struct kvm_vcpu *vcpu = filp->private_data; |
| 1697 | |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1698 | kvm_put_kvm(vcpu->kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1699 | return 0; |
| 1700 | } |
| 1701 | |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 1702 | static struct file_operations kvm_vcpu_fops = { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1703 | .release = kvm_vcpu_release, |
| 1704 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 1705 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1706 | .mmap = kvm_vcpu_mmap, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1707 | }; |
| 1708 | |
| 1709 | /* |
| 1710 | * Allocates an inode for the vcpu. |
| 1711 | */ |
| 1712 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 1713 | { |
Ulrich Drepper | 7d9dbca | 2008-07-23 21:29:22 -0700 | [diff] [blame] | 1714 | int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1715 | if (fd < 0) |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1716 | kvm_put_kvm(vcpu->kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1717 | return fd; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1720 | /* |
| 1721 | * Creates some virtual cpus. Good luck creating more than one. |
| 1722 | */ |
| 1723 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 1724 | { |
| 1725 | int r; |
| 1726 | struct kvm_vcpu *vcpu; |
| 1727 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1728 | if (!valid_vcpu(n)) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1729 | return -EINVAL; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1730 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1731 | vcpu = kvm_arch_vcpu_create(kvm, n); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1732 | if (IS_ERR(vcpu)) |
| 1733 | return PTR_ERR(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1734 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1735 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 1736 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 1737 | r = kvm_arch_vcpu_setup(vcpu); |
| 1738 | if (r) |
Glauber Costa | 7d8fece | 2008-09-17 23:16:59 -0300 | [diff] [blame] | 1739 | return r; |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 1740 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1741 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1742 | if (kvm->vcpus[n]) { |
| 1743 | r = -EEXIST; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1744 | goto vcpu_destroy; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1745 | } |
| 1746 | kvm->vcpus[n] = vcpu; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1747 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1748 | |
| 1749 | /* Now it's all set up, let userspace reach it */ |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1750 | kvm_get_kvm(kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1751 | r = create_vcpu_fd(vcpu); |
| 1752 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1753 | goto unlink; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1754 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1755 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1756 | unlink: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1757 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1758 | kvm->vcpus[n] = NULL; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1759 | vcpu_destroy: |
Glauber Costa | 7d8fece | 2008-09-17 23:16:59 -0300 | [diff] [blame] | 1760 | mutex_unlock(&kvm->lock); |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 1761 | kvm_arch_vcpu_destroy(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1762 | return r; |
| 1763 | } |
| 1764 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1765 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 1766 | { |
| 1767 | if (sigset) { |
| 1768 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 1769 | vcpu->sigset_active = 1; |
| 1770 | vcpu->sigset = *sigset; |
| 1771 | } else |
| 1772 | vcpu->sigset_active = 0; |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
Sheng Yang | c1e0151 | 2009-02-25 17:22:26 +0800 | [diff] [blame] | 1776 | #ifdef __KVM_HAVE_MSIX |
| 1777 | static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm, |
| 1778 | struct kvm_assigned_msix_nr *entry_nr) |
| 1779 | { |
| 1780 | int r = 0; |
| 1781 | struct kvm_assigned_dev_kernel *adev; |
| 1782 | |
| 1783 | mutex_lock(&kvm->lock); |
| 1784 | |
| 1785 | adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 1786 | entry_nr->assigned_dev_id); |
| 1787 | if (!adev) { |
| 1788 | r = -EINVAL; |
| 1789 | goto msix_nr_out; |
| 1790 | } |
| 1791 | |
| 1792 | if (adev->entries_nr == 0) { |
| 1793 | adev->entries_nr = entry_nr->entry_nr; |
| 1794 | if (adev->entries_nr == 0 || |
| 1795 | adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) { |
| 1796 | r = -EINVAL; |
| 1797 | goto msix_nr_out; |
| 1798 | } |
| 1799 | |
| 1800 | adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) * |
| 1801 | entry_nr->entry_nr, |
| 1802 | GFP_KERNEL); |
| 1803 | if (!adev->host_msix_entries) { |
| 1804 | r = -ENOMEM; |
| 1805 | goto msix_nr_out; |
| 1806 | } |
| 1807 | adev->guest_msix_entries = kzalloc( |
| 1808 | sizeof(struct kvm_guest_msix_entry) * |
| 1809 | entry_nr->entry_nr, GFP_KERNEL); |
| 1810 | if (!adev->guest_msix_entries) { |
| 1811 | kfree(adev->host_msix_entries); |
| 1812 | r = -ENOMEM; |
| 1813 | goto msix_nr_out; |
| 1814 | } |
| 1815 | } else /* Not allowed set MSI-X number twice */ |
| 1816 | r = -EINVAL; |
| 1817 | msix_nr_out: |
| 1818 | mutex_unlock(&kvm->lock); |
| 1819 | return r; |
| 1820 | } |
| 1821 | |
| 1822 | static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm, |
| 1823 | struct kvm_assigned_msix_entry *entry) |
| 1824 | { |
| 1825 | int r = 0, i; |
| 1826 | struct kvm_assigned_dev_kernel *adev; |
| 1827 | |
| 1828 | mutex_lock(&kvm->lock); |
| 1829 | |
| 1830 | adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 1831 | entry->assigned_dev_id); |
| 1832 | |
| 1833 | if (!adev) { |
| 1834 | r = -EINVAL; |
| 1835 | goto msix_entry_out; |
| 1836 | } |
| 1837 | |
| 1838 | for (i = 0; i < adev->entries_nr; i++) |
| 1839 | if (adev->guest_msix_entries[i].vector == 0 || |
| 1840 | adev->guest_msix_entries[i].entry == entry->entry) { |
| 1841 | adev->guest_msix_entries[i].entry = entry->entry; |
| 1842 | adev->guest_msix_entries[i].vector = entry->gsi; |
| 1843 | adev->host_msix_entries[i].entry = entry->entry; |
| 1844 | break; |
| 1845 | } |
| 1846 | if (i == adev->entries_nr) { |
| 1847 | r = -ENOSPC; |
| 1848 | goto msix_entry_out; |
| 1849 | } |
| 1850 | |
| 1851 | msix_entry_out: |
| 1852 | mutex_unlock(&kvm->lock); |
| 1853 | |
| 1854 | return r; |
| 1855 | } |
| 1856 | #endif |
| 1857 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1858 | static long kvm_vcpu_ioctl(struct file *filp, |
| 1859 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1860 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1861 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1862 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1863 | int r; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1864 | struct kvm_fpu *fpu = NULL; |
| 1865 | struct kvm_sregs *kvm_sregs = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1866 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 1867 | if (vcpu->kvm->mm != current->mm) |
| 1868 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1869 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1870 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1871 | r = -EINVAL; |
| 1872 | if (arg) |
| 1873 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1874 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1875 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1876 | case KVM_GET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1877 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1878 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1879 | r = -ENOMEM; |
| 1880 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); |
| 1881 | if (!kvm_regs) |
| 1882 | goto out; |
| 1883 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1884 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1885 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1886 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1887 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
| 1888 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1889 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1890 | out_free1: |
| 1891 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1892 | break; |
| 1893 | } |
| 1894 | case KVM_SET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1895 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1896 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1897 | r = -ENOMEM; |
| 1898 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); |
| 1899 | if (!kvm_regs) |
| 1900 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1901 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1902 | if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs))) |
| 1903 | goto out_free2; |
| 1904 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1905 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1906 | goto out_free2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1907 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1908 | out_free2: |
| 1909 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1910 | break; |
| 1911 | } |
| 1912 | case KVM_GET_SREGS: { |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1913 | kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
| 1914 | r = -ENOMEM; |
| 1915 | if (!kvm_sregs) |
| 1916 | goto out; |
| 1917 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1918 | if (r) |
| 1919 | goto out; |
| 1920 | r = -EFAULT; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1921 | if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1922 | goto out; |
| 1923 | r = 0; |
| 1924 | break; |
| 1925 | } |
| 1926 | case KVM_SET_SREGS: { |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1927 | kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
| 1928 | r = -ENOMEM; |
| 1929 | if (!kvm_sregs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1930 | goto out; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1931 | r = -EFAULT; |
| 1932 | if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs))) |
| 1933 | goto out; |
| 1934 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1935 | if (r) |
| 1936 | goto out; |
| 1937 | r = 0; |
| 1938 | break; |
| 1939 | } |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1940 | case KVM_GET_MP_STATE: { |
| 1941 | struct kvm_mp_state mp_state; |
| 1942 | |
| 1943 | r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state); |
| 1944 | if (r) |
| 1945 | goto out; |
| 1946 | r = -EFAULT; |
| 1947 | if (copy_to_user(argp, &mp_state, sizeof mp_state)) |
| 1948 | goto out; |
| 1949 | r = 0; |
| 1950 | break; |
| 1951 | } |
| 1952 | case KVM_SET_MP_STATE: { |
| 1953 | struct kvm_mp_state mp_state; |
| 1954 | |
| 1955 | r = -EFAULT; |
| 1956 | if (copy_from_user(&mp_state, argp, sizeof mp_state)) |
| 1957 | goto out; |
| 1958 | r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state); |
| 1959 | if (r) |
| 1960 | goto out; |
| 1961 | r = 0; |
| 1962 | break; |
| 1963 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1964 | case KVM_TRANSLATE: { |
| 1965 | struct kvm_translation tr; |
| 1966 | |
| 1967 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1968 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1969 | goto out; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 1970 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1971 | if (r) |
| 1972 | goto out; |
| 1973 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1974 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1975 | goto out; |
| 1976 | r = 0; |
| 1977 | break; |
| 1978 | } |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1979 | case KVM_SET_GUEST_DEBUG: { |
| 1980 | struct kvm_guest_debug dbg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1981 | |
| 1982 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1983 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1984 | goto out; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1985 | r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1986 | if (r) |
| 1987 | goto out; |
| 1988 | r = 0; |
| 1989 | break; |
| 1990 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1991 | case KVM_SET_SIGNAL_MASK: { |
| 1992 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 1993 | struct kvm_signal_mask kvm_sigmask; |
| 1994 | sigset_t sigset, *p; |
| 1995 | |
| 1996 | p = NULL; |
| 1997 | if (argp) { |
| 1998 | r = -EFAULT; |
| 1999 | if (copy_from_user(&kvm_sigmask, argp, |
| 2000 | sizeof kvm_sigmask)) |
| 2001 | goto out; |
| 2002 | r = -EINVAL; |
| 2003 | if (kvm_sigmask.len != sizeof sigset) |
| 2004 | goto out; |
| 2005 | r = -EFAULT; |
| 2006 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 2007 | sizeof sigset)) |
| 2008 | goto out; |
| 2009 | p = &sigset; |
| 2010 | } |
| 2011 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 2012 | break; |
| 2013 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2014 | case KVM_GET_FPU: { |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 2015 | fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
| 2016 | r = -ENOMEM; |
| 2017 | if (!fpu) |
| 2018 | goto out; |
| 2019 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2020 | if (r) |
| 2021 | goto out; |
| 2022 | r = -EFAULT; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 2023 | if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu))) |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2024 | goto out; |
| 2025 | r = 0; |
| 2026 | break; |
| 2027 | } |
| 2028 | case KVM_SET_FPU: { |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 2029 | fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
| 2030 | r = -ENOMEM; |
| 2031 | if (!fpu) |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2032 | goto out; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 2033 | r = -EFAULT; |
| 2034 | if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu))) |
| 2035 | goto out; |
| 2036 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2037 | if (r) |
| 2038 | goto out; |
| 2039 | r = 0; |
| 2040 | break; |
| 2041 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2042 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2043 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2044 | } |
| 2045 | out: |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 2046 | kfree(fpu); |
| 2047 | kfree(kvm_sregs); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2048 | return r; |
| 2049 | } |
| 2050 | |
| 2051 | static long kvm_vm_ioctl(struct file *filp, |
| 2052 | unsigned int ioctl, unsigned long arg) |
| 2053 | { |
| 2054 | struct kvm *kvm = filp->private_data; |
| 2055 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2056 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2057 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 2058 | if (kvm->mm != current->mm) |
| 2059 | return -EIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2060 | switch (ioctl) { |
| 2061 | case KVM_CREATE_VCPU: |
| 2062 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 2063 | if (r < 0) |
| 2064 | goto out; |
| 2065 | break; |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 2066 | case KVM_SET_USER_MEMORY_REGION: { |
| 2067 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2068 | |
| 2069 | r = -EFAULT; |
| 2070 | if (copy_from_user(&kvm_userspace_mem, argp, |
| 2071 | sizeof kvm_userspace_mem)) |
| 2072 | goto out; |
| 2073 | |
| 2074 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2075 | if (r) |
| 2076 | goto out; |
| 2077 | break; |
| 2078 | } |
| 2079 | case KVM_GET_DIRTY_LOG: { |
| 2080 | struct kvm_dirty_log log; |
| 2081 | |
| 2082 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2083 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2084 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 2085 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2086 | if (r) |
| 2087 | goto out; |
| 2088 | break; |
| 2089 | } |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 2090 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 2091 | case KVM_REGISTER_COALESCED_MMIO: { |
| 2092 | struct kvm_coalesced_mmio_zone zone; |
| 2093 | r = -EFAULT; |
| 2094 | if (copy_from_user(&zone, argp, sizeof zone)) |
| 2095 | goto out; |
| 2096 | r = -ENXIO; |
| 2097 | r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); |
| 2098 | if (r) |
| 2099 | goto out; |
| 2100 | r = 0; |
| 2101 | break; |
| 2102 | } |
| 2103 | case KVM_UNREGISTER_COALESCED_MMIO: { |
| 2104 | struct kvm_coalesced_mmio_zone zone; |
| 2105 | r = -EFAULT; |
| 2106 | if (copy_from_user(&zone, argp, sizeof zone)) |
| 2107 | goto out; |
| 2108 | r = -ENXIO; |
| 2109 | r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); |
| 2110 | if (r) |
| 2111 | goto out; |
| 2112 | r = 0; |
| 2113 | break; |
| 2114 | } |
| 2115 | #endif |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 2116 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT |
| 2117 | case KVM_ASSIGN_PCI_DEVICE: { |
| 2118 | struct kvm_assigned_pci_dev assigned_dev; |
| 2119 | |
| 2120 | r = -EFAULT; |
| 2121 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) |
| 2122 | goto out; |
| 2123 | r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev); |
| 2124 | if (r) |
| 2125 | goto out; |
| 2126 | break; |
| 2127 | } |
| 2128 | case KVM_ASSIGN_IRQ: { |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 2129 | r = -EOPNOTSUPP; |
| 2130 | break; |
| 2131 | } |
| 2132 | #ifdef KVM_CAP_ASSIGN_DEV_IRQ |
| 2133 | case KVM_ASSIGN_DEV_IRQ: { |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 2134 | struct kvm_assigned_irq assigned_irq; |
| 2135 | |
| 2136 | r = -EFAULT; |
| 2137 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) |
| 2138 | goto out; |
| 2139 | r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq); |
| 2140 | if (r) |
| 2141 | goto out; |
| 2142 | break; |
| 2143 | } |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 2144 | case KVM_DEASSIGN_DEV_IRQ: { |
| 2145 | struct kvm_assigned_irq assigned_irq; |
| 2146 | |
| 2147 | r = -EFAULT; |
| 2148 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) |
| 2149 | goto out; |
| 2150 | r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq); |
| 2151 | if (r) |
| 2152 | goto out; |
| 2153 | break; |
| 2154 | } |
| 2155 | #endif |
Xiantao Zhang | 8a98f66 | 2008-10-06 13:47:38 +0800 | [diff] [blame] | 2156 | #endif |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 2157 | #ifdef KVM_CAP_DEVICE_DEASSIGNMENT |
| 2158 | case KVM_DEASSIGN_PCI_DEVICE: { |
| 2159 | struct kvm_assigned_pci_dev assigned_dev; |
| 2160 | |
| 2161 | r = -EFAULT; |
| 2162 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) |
| 2163 | goto out; |
| 2164 | r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev); |
| 2165 | if (r) |
| 2166 | goto out; |
| 2167 | break; |
| 2168 | } |
| 2169 | #endif |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2170 | #ifdef KVM_CAP_IRQ_ROUTING |
| 2171 | case KVM_SET_GSI_ROUTING: { |
| 2172 | struct kvm_irq_routing routing; |
| 2173 | struct kvm_irq_routing __user *urouting; |
| 2174 | struct kvm_irq_routing_entry *entries; |
| 2175 | |
| 2176 | r = -EFAULT; |
| 2177 | if (copy_from_user(&routing, argp, sizeof(routing))) |
| 2178 | goto out; |
| 2179 | r = -EINVAL; |
| 2180 | if (routing.nr >= KVM_MAX_IRQ_ROUTES) |
| 2181 | goto out; |
| 2182 | if (routing.flags) |
| 2183 | goto out; |
| 2184 | r = -ENOMEM; |
| 2185 | entries = vmalloc(routing.nr * sizeof(*entries)); |
| 2186 | if (!entries) |
| 2187 | goto out; |
| 2188 | r = -EFAULT; |
| 2189 | urouting = argp; |
| 2190 | if (copy_from_user(entries, urouting->entries, |
| 2191 | routing.nr * sizeof(*entries))) |
| 2192 | goto out_free_irq_routing; |
| 2193 | r = kvm_set_irq_routing(kvm, entries, routing.nr, |
| 2194 | routing.flags); |
| 2195 | out_free_irq_routing: |
| 2196 | vfree(entries); |
| 2197 | break; |
| 2198 | } |
Sheng Yang | c1e0151 | 2009-02-25 17:22:26 +0800 | [diff] [blame] | 2199 | #ifdef __KVM_HAVE_MSIX |
| 2200 | case KVM_ASSIGN_SET_MSIX_NR: { |
| 2201 | struct kvm_assigned_msix_nr entry_nr; |
| 2202 | r = -EFAULT; |
| 2203 | if (copy_from_user(&entry_nr, argp, sizeof entry_nr)) |
| 2204 | goto out; |
| 2205 | r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr); |
| 2206 | if (r) |
| 2207 | goto out; |
| 2208 | break; |
| 2209 | } |
| 2210 | case KVM_ASSIGN_SET_MSIX_ENTRY: { |
| 2211 | struct kvm_assigned_msix_entry entry; |
| 2212 | r = -EFAULT; |
| 2213 | if (copy_from_user(&entry, argp, sizeof entry)) |
| 2214 | goto out; |
| 2215 | r = kvm_vm_ioctl_set_msix_entry(kvm, &entry); |
| 2216 | if (r) |
| 2217 | goto out; |
| 2218 | break; |
| 2219 | } |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2220 | #endif |
Sheng Yang | c1e0151 | 2009-02-25 17:22:26 +0800 | [diff] [blame] | 2221 | #endif /* KVM_CAP_IRQ_ROUTING */ |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 2222 | case KVM_IRQFD: { |
| 2223 | struct kvm_irqfd data; |
| 2224 | |
| 2225 | r = -EFAULT; |
| 2226 | if (copy_from_user(&data, argp, sizeof data)) |
| 2227 | goto out; |
| 2228 | r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags); |
| 2229 | break; |
| 2230 | } |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2231 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2232 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2233 | } |
| 2234 | out: |
| 2235 | return r; |
| 2236 | } |
| 2237 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 2238 | static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2239 | { |
Marcelo Tosatti | 777b3f4 | 2008-09-16 20:54:46 -0300 | [diff] [blame] | 2240 | struct page *page[1]; |
| 2241 | unsigned long addr; |
| 2242 | int npages; |
| 2243 | gfn_t gfn = vmf->pgoff; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2244 | struct kvm *kvm = vma->vm_file->private_data; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2245 | |
Marcelo Tosatti | 777b3f4 | 2008-09-16 20:54:46 -0300 | [diff] [blame] | 2246 | addr = gfn_to_hva(kvm, gfn); |
| 2247 | if (kvm_is_error_hva(addr)) |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 2248 | return VM_FAULT_SIGBUS; |
Marcelo Tosatti | 777b3f4 | 2008-09-16 20:54:46 -0300 | [diff] [blame] | 2249 | |
| 2250 | npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page, |
| 2251 | NULL); |
| 2252 | if (unlikely(npages != 1)) |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 2253 | return VM_FAULT_SIGBUS; |
Marcelo Tosatti | 777b3f4 | 2008-09-16 20:54:46 -0300 | [diff] [blame] | 2254 | |
| 2255 | vmf->page = page[0]; |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 2256 | return 0; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | static struct vm_operations_struct kvm_vm_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 2260 | .fault = kvm_vm_fault, |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2261 | }; |
| 2262 | |
| 2263 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 2264 | { |
| 2265 | vma->vm_ops = &kvm_vm_vm_ops; |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 2269 | static struct file_operations kvm_vm_fops = { |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2270 | .release = kvm_vm_release, |
| 2271 | .unlocked_ioctl = kvm_vm_ioctl, |
| 2272 | .compat_ioctl = kvm_vm_ioctl, |
| 2273 | .mmap = kvm_vm_mmap, |
| 2274 | }; |
| 2275 | |
| 2276 | static int kvm_dev_ioctl_create_vm(void) |
| 2277 | { |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 2278 | int fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2279 | struct kvm *kvm; |
| 2280 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2281 | kvm = kvm_create_vm(); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 2282 | if (IS_ERR(kvm)) |
| 2283 | return PTR_ERR(kvm); |
Ulrich Drepper | 7d9dbca | 2008-07-23 21:29:22 -0700 | [diff] [blame] | 2284 | fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 2285 | if (fd < 0) |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 2286 | kvm_put_kvm(kvm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2287 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2288 | return fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2289 | } |
| 2290 | |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 2291 | static long kvm_dev_ioctl_check_extension_generic(long arg) |
| 2292 | { |
| 2293 | switch (arg) { |
Avi Kivity | ca9edae | 2008-12-08 18:29:29 +0200 | [diff] [blame] | 2294 | case KVM_CAP_USER_MEMORY: |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 2295 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: |
Jan Kiszka | 4cd481f | 2009-04-13 11:59:32 +0200 | [diff] [blame] | 2296 | case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 2297 | return 1; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2298 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
| 2299 | case KVM_CAP_IRQ_ROUTING: |
Sheng Yang | 3646314 | 2009-03-16 16:33:43 +0800 | [diff] [blame] | 2300 | return KVM_MAX_IRQ_ROUTES; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2301 | #endif |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 2302 | default: |
| 2303 | break; |
| 2304 | } |
| 2305 | return kvm_dev_ioctl_check_extension(arg); |
| 2306 | } |
| 2307 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2308 | static long kvm_dev_ioctl(struct file *filp, |
| 2309 | unsigned int ioctl, unsigned long arg) |
| 2310 | { |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2311 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2312 | |
| 2313 | switch (ioctl) { |
| 2314 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2315 | r = -EINVAL; |
| 2316 | if (arg) |
| 2317 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2318 | r = KVM_API_VERSION; |
| 2319 | break; |
| 2320 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2321 | r = -EINVAL; |
| 2322 | if (arg) |
| 2323 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2324 | r = kvm_dev_ioctl_create_vm(); |
| 2325 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 2326 | case KVM_CHECK_EXTENSION: |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 2327 | r = kvm_dev_ioctl_check_extension_generic(arg); |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 2328 | break; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2329 | case KVM_GET_VCPU_MMAP_SIZE: |
| 2330 | r = -EINVAL; |
| 2331 | if (arg) |
| 2332 | goto out; |
Avi Kivity | adb1ff4 | 2008-01-24 15:13:08 +0200 | [diff] [blame] | 2333 | r = PAGE_SIZE; /* struct kvm_run */ |
| 2334 | #ifdef CONFIG_X86 |
| 2335 | r += PAGE_SIZE; /* pio data page */ |
| 2336 | #endif |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 2337 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 2338 | r += PAGE_SIZE; /* coalesced mmio ring page */ |
| 2339 | #endif |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2340 | break; |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 2341 | case KVM_TRACE_ENABLE: |
| 2342 | case KVM_TRACE_PAUSE: |
| 2343 | case KVM_TRACE_DISABLE: |
| 2344 | r = kvm_trace_ioctl(ioctl, arg); |
| 2345 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2346 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 2347 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2348 | } |
| 2349 | out: |
| 2350 | return r; |
| 2351 | } |
| 2352 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2353 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2354 | .unlocked_ioctl = kvm_dev_ioctl, |
| 2355 | .compat_ioctl = kvm_dev_ioctl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2356 | }; |
| 2357 | |
| 2358 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 2359 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2360 | "kvm", |
| 2361 | &kvm_chardev_ops, |
| 2362 | }; |
| 2363 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2364 | static void hardware_enable(void *junk) |
| 2365 | { |
| 2366 | int cpu = raw_smp_processor_id(); |
| 2367 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2368 | if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2369 | return; |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2370 | cpumask_set_cpu(cpu, cpus_hardware_enabled); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2371 | kvm_arch_hardware_enable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | static void hardware_disable(void *junk) |
| 2375 | { |
| 2376 | int cpu = raw_smp_processor_id(); |
| 2377 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2378 | if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2379 | return; |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2380 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2381 | kvm_arch_hardware_disable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2382 | } |
| 2383 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2384 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 2385 | void *v) |
| 2386 | { |
| 2387 | int cpu = (long)v; |
| 2388 | |
Avi Kivity | 1a6f4d7 | 2007-11-11 18:37:32 +0200 | [diff] [blame] | 2389 | val &= ~CPU_TASKS_FROZEN; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2390 | switch (val) { |
Avi Kivity | cec9ad2 | 2007-05-24 13:11:41 +0300 | [diff] [blame] | 2391 | case CPU_DYING: |
Avi Kivity | 6ec8a85 | 2007-08-19 15:57:26 +0300 | [diff] [blame] | 2392 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 2393 | cpu); |
| 2394 | hardware_disable(NULL); |
| 2395 | break; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2396 | case CPU_UP_CANCELED: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2397 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 2398 | cpu); |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 2399 | smp_call_function_single(cpu, hardware_disable, NULL, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2400 | break; |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2401 | case CPU_ONLINE: |
| 2402 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 2403 | cpu); |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 2404 | smp_call_function_single(cpu, hardware_enable, NULL, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2405 | break; |
| 2406 | } |
| 2407 | return NOTIFY_OK; |
| 2408 | } |
| 2409 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2410 | |
| 2411 | asmlinkage void kvm_handle_fault_on_reboot(void) |
| 2412 | { |
| 2413 | if (kvm_rebooting) |
| 2414 | /* spin while reset goes on */ |
| 2415 | while (true) |
| 2416 | ; |
| 2417 | /* Fault while not rebooting. We want the trace. */ |
| 2418 | BUG(); |
| 2419 | } |
| 2420 | EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot); |
| 2421 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 2422 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2423 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 2424 | { |
Sheng Yang | 8e1c181 | 2009-04-29 11:09:04 +0800 | [diff] [blame] | 2425 | /* |
| 2426 | * Some (well, at least mine) BIOSes hang on reboot if |
| 2427 | * in vmx root mode. |
| 2428 | * |
| 2429 | * And Intel TXT required VMX off for all cpu when system shutdown. |
| 2430 | */ |
| 2431 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
| 2432 | kvm_rebooting = true; |
| 2433 | on_each_cpu(hardware_disable, NULL, 1); |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 2434 | return NOTIFY_OK; |
| 2435 | } |
| 2436 | |
| 2437 | static struct notifier_block kvm_reboot_notifier = { |
| 2438 | .notifier_call = kvm_reboot, |
| 2439 | .priority = 0, |
| 2440 | }; |
| 2441 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2442 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
| 2443 | { |
| 2444 | memset(bus, 0, sizeof(*bus)); |
| 2445 | } |
| 2446 | |
| 2447 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
| 2448 | { |
| 2449 | int i; |
| 2450 | |
| 2451 | for (i = 0; i < bus->dev_count; i++) { |
| 2452 | struct kvm_io_device *pos = bus->devs[i]; |
| 2453 | |
| 2454 | kvm_iodevice_destructor(pos); |
| 2455 | } |
| 2456 | } |
| 2457 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2458 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, |
| 2459 | gpa_t addr, int len, int is_write) |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2460 | { |
| 2461 | int i; |
| 2462 | |
| 2463 | for (i = 0; i < bus->dev_count; i++) { |
| 2464 | struct kvm_io_device *pos = bus->devs[i]; |
| 2465 | |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame^] | 2466 | if (kvm_iodevice_in_range(pos, addr, len, is_write)) |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2467 | return pos; |
| 2468 | } |
| 2469 | |
| 2470 | return NULL; |
| 2471 | } |
| 2472 | |
| 2473 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) |
| 2474 | { |
| 2475 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); |
| 2476 | |
| 2477 | bus->devs[bus->dev_count++] = dev; |
| 2478 | } |
| 2479 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2480 | static struct notifier_block kvm_cpu_notifier = { |
| 2481 | .notifier_call = kvm_cpu_hotplug, |
| 2482 | .priority = 20, /* must be > scheduler priority */ |
| 2483 | }; |
| 2484 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2485 | static int vm_stat_get(void *_offset, u64 *val) |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2486 | { |
| 2487 | unsigned offset = (long)_offset; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2488 | struct kvm *kvm; |
| 2489 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2490 | *val = 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2491 | spin_lock(&kvm_lock); |
| 2492 | list_for_each_entry(kvm, &vm_list, vm_list) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2493 | *val += *(u32 *)((void *)kvm + offset); |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2494 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2495 | return 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); |
| 2499 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2500 | static int vcpu_stat_get(void *_offset, u64 *val) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2501 | { |
| 2502 | unsigned offset = (long)_offset; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2503 | struct kvm *kvm; |
| 2504 | struct kvm_vcpu *vcpu; |
| 2505 | int i; |
| 2506 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2507 | *val = 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2508 | spin_lock(&kvm_lock); |
| 2509 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 2510 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2511 | vcpu = kvm->vcpus[i]; |
| 2512 | if (vcpu) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2513 | *val += *(u32 *)((void *)vcpu + offset); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2514 | } |
| 2515 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2516 | return 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2517 | } |
| 2518 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2519 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
| 2520 | |
| 2521 | static struct file_operations *stat_fops[] = { |
| 2522 | [KVM_STAT_VCPU] = &vcpu_stat_fops, |
| 2523 | [KVM_STAT_VM] = &vm_stat_fops, |
| 2524 | }; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2525 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 2526 | static void kvm_init_debug(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2527 | { |
| 2528 | struct kvm_stats_debugfs_item *p; |
| 2529 | |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 2530 | kvm_debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2531 | for (p = debugfs_entries; p->name; ++p) |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 2532 | p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir, |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2533 | (void *)(long)p->offset, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2534 | stat_fops[p->kind]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | static void kvm_exit_debug(void) |
| 2538 | { |
| 2539 | struct kvm_stats_debugfs_item *p; |
| 2540 | |
| 2541 | for (p = debugfs_entries; p->name; ++p) |
| 2542 | debugfs_remove(p->dentry); |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 2543 | debugfs_remove(kvm_debugfs_dir); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2544 | } |
| 2545 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2546 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 2547 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 2548 | hardware_disable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2549 | return 0; |
| 2550 | } |
| 2551 | |
| 2552 | static int kvm_resume(struct sys_device *dev) |
| 2553 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 2554 | hardware_enable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2555 | return 0; |
| 2556 | } |
| 2557 | |
| 2558 | static struct sysdev_class kvm_sysdev_class = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 2559 | .name = "kvm", |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2560 | .suspend = kvm_suspend, |
| 2561 | .resume = kvm_resume, |
| 2562 | }; |
| 2563 | |
| 2564 | static struct sys_device kvm_sysdev = { |
| 2565 | .id = 0, |
| 2566 | .cls = &kvm_sysdev_class, |
| 2567 | }; |
| 2568 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 2569 | struct page *bad_page; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2570 | pfn_t bad_pfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2571 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2572 | static inline |
| 2573 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 2574 | { |
| 2575 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 2576 | } |
| 2577 | |
| 2578 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 2579 | { |
| 2580 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 2581 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2582 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 2586 | struct task_struct *next) |
| 2587 | { |
| 2588 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 2589 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2590 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2591 | } |
| 2592 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2593 | int kvm_init(void *opaque, unsigned int vcpu_size, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2594 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2595 | { |
| 2596 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2597 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2598 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2599 | kvm_init_debug(); |
| 2600 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2601 | r = kvm_arch_init(opaque); |
| 2602 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2603 | goto out_fail; |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2604 | |
| 2605 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 2606 | |
| 2607 | if (bad_page == NULL) { |
| 2608 | r = -ENOMEM; |
| 2609 | goto out; |
| 2610 | } |
| 2611 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2612 | bad_pfn = page_to_pfn(bad_page); |
| 2613 | |
Avi Kivity | 8437a617 | 2009-06-06 14:52:35 -0700 | [diff] [blame] | 2614 | if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) { |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2615 | r = -ENOMEM; |
| 2616 | goto out_free_0; |
| 2617 | } |
| 2618 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2619 | r = kvm_arch_hardware_setup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2620 | if (r < 0) |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2621 | goto out_free_0a; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2622 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2623 | for_each_online_cpu(cpu) { |
| 2624 | smp_call_function_single(cpu, |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2625 | kvm_arch_check_processor_compat, |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 2626 | &r, 1); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2627 | if (r < 0) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2628 | goto out_free_1; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2629 | } |
| 2630 | |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 2631 | on_each_cpu(hardware_enable, NULL, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2632 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 2633 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2634 | goto out_free_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2635 | register_reboot_notifier(&kvm_reboot_notifier); |
| 2636 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2637 | r = sysdev_class_register(&kvm_sysdev_class); |
| 2638 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2639 | goto out_free_3; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2640 | |
| 2641 | r = sysdev_register(&kvm_sysdev); |
| 2642 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2643 | goto out_free_4; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2644 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2645 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
| 2646 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 2647 | __alignof__(struct kvm_vcpu), |
| 2648 | 0, NULL); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2649 | if (!kvm_vcpu_cache) { |
| 2650 | r = -ENOMEM; |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2651 | goto out_free_5; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2652 | } |
| 2653 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2654 | kvm_chardev_ops.owner = module; |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 2655 | kvm_vm_fops.owner = module; |
| 2656 | kvm_vcpu_fops.owner = module; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2657 | |
| 2658 | r = misc_register(&kvm_dev); |
| 2659 | if (r) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2660 | printk(KERN_ERR "kvm: misc device register failed\n"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2661 | goto out_free; |
| 2662 | } |
| 2663 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2664 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 2665 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 2666 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2667 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2668 | |
| 2669 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2670 | kmem_cache_destroy(kvm_vcpu_cache); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2671 | out_free_5: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2672 | sysdev_unregister(&kvm_sysdev); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2673 | out_free_4: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2674 | sysdev_class_unregister(&kvm_sysdev_class); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2675 | out_free_3: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2676 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2677 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2678 | out_free_2: |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 2679 | on_each_cpu(hardware_disable, NULL, 1); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2680 | out_free_1: |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2681 | kvm_arch_hardware_unsetup(); |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2682 | out_free_0a: |
| 2683 | free_cpumask_var(cpus_hardware_enabled); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2684 | out_free_0: |
| 2685 | __free_page(bad_page); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 2686 | out: |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2687 | kvm_arch_exit(); |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2688 | kvm_exit_debug(); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2689 | out_fail: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2690 | return r; |
| 2691 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2692 | EXPORT_SYMBOL_GPL(kvm_init); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2693 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2694 | void kvm_exit(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2695 | { |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 2696 | kvm_trace_cleanup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2697 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2698 | kmem_cache_destroy(kvm_vcpu_cache); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2699 | sysdev_unregister(&kvm_sysdev); |
| 2700 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2701 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2702 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 2703 | on_each_cpu(hardware_disable, NULL, 1); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2704 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2705 | kvm_arch_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2706 | kvm_exit_debug(); |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2707 | free_cpumask_var(cpus_hardware_enabled); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 2708 | __free_page(bad_page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2709 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2710 | EXPORT_SYMBOL_GPL(kvm_exit); |