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