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