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