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