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