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