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