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