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