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