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