blob: 792fb7fae0a3f5c3e985d949aa850e6c74066cc5 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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 Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080045
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/io.h>
48#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020049#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Laurent Vivier5f94c172008-05-30 16:05:54 +020051#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
52#include "coalesced_mmio.h"
53#endif
54
Xiantao Zhang8a98f662008-10-06 13:47:38 +080055#ifdef KVM_CAP_DEVICE_ASSIGNMENT
56#include <linux/pci.h>
57#include <linux/interrupt.h>
58#include "irq.h"
59#endif
60
Avi Kivity6aa8b732006-12-10 02:21:36 -080061MODULE_AUTHOR("Qumranet");
62MODULE_LICENSE("GPL");
63
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080064DEFINE_SPINLOCK(kvm_lock);
65LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080066
Rusty Russell7f59f492008-12-07 21:25:45 +103067static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030068
Rusty Russellc16f8622007-07-30 21:12:19 +100069struct kmem_cache *kvm_vcpu_cache;
70EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030071
Avi Kivity15ad7142007-07-11 18:17:21 +030072static __read_mostly struct preempt_ops kvm_preempt_ops;
73
Hollis Blanchard76f7c872008-04-15 16:05:42 -050074struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080075
Avi Kivitybccf2152007-02-21 18:04:26 +020076static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
77 unsigned long arg);
78
Hannes Edere8ba5d32008-11-28 17:02:42 +010079static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030080
Xiantao Zhang8a98f662008-10-06 13:47:38 +080081#ifdef KVM_CAP_DEVICE_ASSIGNMENT
82static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
83 int assigned_dev_id)
84{
85 struct list_head *ptr;
86 struct kvm_assigned_dev_kernel *match;
87
88 list_for_each(ptr, head) {
89 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
90 if (match->assigned_dev_id == assigned_dev_id)
91 return match;
92 }
93 return NULL;
94}
95
Sheng Yang2350bd12009-02-25 17:22:27 +080096static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
97 *assigned_dev, int irq)
98{
99 int i, index;
100 struct msix_entry *host_msix_entries;
101
102 host_msix_entries = assigned_dev->host_msix_entries;
103
104 index = -1;
105 for (i = 0; i < assigned_dev->entries_nr; i++)
106 if (irq == host_msix_entries[i].vector) {
107 index = i;
108 break;
109 }
110 if (index < 0) {
111 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
112 return 0;
113 }
114
115 return index;
116}
117
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800118static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
119{
120 struct kvm_assigned_dev_kernel *assigned_dev;
Sheng Yang2350bd12009-02-25 17:22:27 +0800121 struct kvm *kvm;
122 int irq, i;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800123
124 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
125 interrupt_work);
Sheng Yang2350bd12009-02-25 17:22:27 +0800126 kvm = assigned_dev->kvm;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800127
128 /* This is taken to safely inject irq inside the guest. When
129 * the interrupt injection (or the ioapic code) uses a
130 * finer-grained lock, update this
131 */
Sheng Yang2350bd12009-02-25 17:22:27 +0800132 mutex_lock(&kvm->lock);
Sheng Yange56d5322009-03-12 21:45:39 +0800133 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800134 struct kvm_guest_msix_entry *guest_entries =
135 assigned_dev->guest_msix_entries;
136 for (i = 0; i < assigned_dev->entries_nr; i++) {
137 if (!(guest_entries[i].flags &
138 KVM_ASSIGNED_MSIX_PENDING))
139 continue;
140 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
141 kvm_set_irq(assigned_dev->kvm,
142 assigned_dev->irq_source_id,
143 guest_entries[i].vector, 1);
144 irq = assigned_dev->host_msix_entries[i].vector;
145 if (irq != 0)
146 enable_irq(irq);
147 assigned_dev->host_irq_disabled = false;
148 }
149 } else {
150 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
151 assigned_dev->guest_irq, 1);
152 if (assigned_dev->irq_requested_type &
Sheng Yange56d5322009-03-12 21:45:39 +0800153 KVM_DEV_IRQ_GUEST_MSI) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800154 enable_irq(assigned_dev->host_irq);
155 assigned_dev->host_irq_disabled = false;
156 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800157 }
Sheng Yang2350bd12009-02-25 17:22:27 +0800158
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800159 mutex_unlock(&assigned_dev->kvm->lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800160}
161
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800162static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
163{
164 struct kvm_assigned_dev_kernel *assigned_dev =
165 (struct kvm_assigned_dev_kernel *) dev_id;
166
Sheng Yange56d5322009-03-12 21:45:39 +0800167 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800168 int index = find_index_from_host_irq(assigned_dev, irq);
169 if (index < 0)
170 return IRQ_HANDLED;
171 assigned_dev->guest_msix_entries[index].flags |=
172 KVM_ASSIGNED_MSIX_PENDING;
173 }
174
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800175 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000176
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800177 disable_irq_nosync(irq);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000178 assigned_dev->host_irq_disabled = true;
179
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800180 return IRQ_HANDLED;
181}
182
183/* Ack the irq line for an assigned device */
184static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
185{
186 struct kvm_assigned_dev_kernel *dev;
187
188 if (kian->gsi == -1)
189 return;
190
191 dev = container_of(kian, struct kvm_assigned_dev_kernel,
192 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000193
Sheng Yang5550af42008-10-15 20:15:06 +0800194 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000195
196 /* The guest irq may be shared so this ack may be
197 * from another device.
198 */
199 if (dev->host_irq_disabled) {
200 enable_irq(dev->host_irq);
201 dev->host_irq_disabled = false;
202 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800203}
204
Sheng Yange56d5322009-03-12 21:45:39 +0800205static void deassign_guest_irq(struct kvm *kvm,
206 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800207{
Sheng Yange19e30e2008-10-20 16:07:10 +0800208 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Sheng Yange56d5322009-03-12 21:45:39 +0800209 assigned_dev->ack_notifier.gsi = -1;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000210
211 if (assigned_dev->irq_source_id != -1)
212 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
213 assigned_dev->irq_source_id = -1;
Sheng Yange56d5322009-03-12 21:45:39 +0800214 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
215}
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800216
Sheng Yange56d5322009-03-12 21:45:39 +0800217/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
218static void deassign_host_irq(struct kvm *kvm,
219 struct kvm_assigned_dev_kernel *assigned_dev)
220{
Sheng Yangba4cef32009-01-06 10:03:03 +0800221 /*
222 * In kvm_free_device_irq, cancel_work_sync return true if:
223 * 1. work is scheduled, and then cancelled.
224 * 2. work callback is executed.
225 *
226 * The first one ensured that the irq is disabled and no more events
227 * would happen. But for the second one, the irq may be enabled (e.g.
228 * for MSI). So we disable irq here to prevent further events.
229 *
230 * Notice this maybe result in nested disable if the interrupt type is
231 * INTx, but it's OK for we are going to free it.
232 *
233 * If this function is a part of VM destroy, please ensure that till
234 * now, the kvm state is still legal for probably we also have to wait
235 * interrupt_work done.
236 */
Sheng Yange56d5322009-03-12 21:45:39 +0800237 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yangd510d6c2009-02-25 17:22:28 +0800238 int i;
239 for (i = 0; i < assigned_dev->entries_nr; i++)
240 disable_irq_nosync(assigned_dev->
241 host_msix_entries[i].vector);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800242
Sheng Yangd510d6c2009-02-25 17:22:28 +0800243 cancel_work_sync(&assigned_dev->interrupt_work);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000244
Sheng Yangd510d6c2009-02-25 17:22:28 +0800245 for (i = 0; i < assigned_dev->entries_nr; i++)
246 free_irq(assigned_dev->host_msix_entries[i].vector,
247 (void *)assigned_dev);
248
249 assigned_dev->entries_nr = 0;
250 kfree(assigned_dev->host_msix_entries);
251 kfree(assigned_dev->guest_msix_entries);
252 pci_disable_msix(assigned_dev->dev);
253 } else {
254 /* Deal with MSI and INTx */
255 disable_irq_nosync(assigned_dev->host_irq);
256 cancel_work_sync(&assigned_dev->interrupt_work);
257
258 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
259
Sheng Yange56d5322009-03-12 21:45:39 +0800260 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800261 pci_disable_msi(assigned_dev->dev);
262 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000263
Sheng Yange56d5322009-03-12 21:45:39 +0800264 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000265}
266
Sheng Yange56d5322009-03-12 21:45:39 +0800267static int kvm_deassign_irq(struct kvm *kvm,
268 struct kvm_assigned_dev_kernel *assigned_dev,
269 unsigned long irq_requested_type)
270{
271 unsigned long guest_irq_type, host_irq_type;
272
273 if (!irqchip_in_kernel(kvm))
274 return -EINVAL;
275 /* no irq assignment to deassign */
276 if (!assigned_dev->irq_requested_type)
277 return -ENXIO;
278
279 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
280 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
281
282 if (host_irq_type)
283 deassign_host_irq(kvm, assigned_dev);
284 if (guest_irq_type)
285 deassign_guest_irq(kvm, assigned_dev);
286
287 return 0;
288}
289
290static void kvm_free_assigned_irq(struct kvm *kvm,
291 struct kvm_assigned_dev_kernel *assigned_dev)
292{
293 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
294}
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000295
296static void kvm_free_assigned_device(struct kvm *kvm,
297 struct kvm_assigned_dev_kernel
298 *assigned_dev)
299{
300 kvm_free_assigned_irq(kvm, assigned_dev);
301
Sheng Yang6eb55812008-10-31 12:37:41 +0800302 pci_reset_function(assigned_dev->dev);
303
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800304 pci_release_regions(assigned_dev->dev);
305 pci_disable_device(assigned_dev->dev);
306 pci_dev_put(assigned_dev->dev);
307
308 list_del(&assigned_dev->list);
309 kfree(assigned_dev);
310}
311
312void kvm_free_all_assigned_devices(struct kvm *kvm)
313{
314 struct list_head *ptr, *ptr2;
315 struct kvm_assigned_dev_kernel *assigned_dev;
316
317 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
318 assigned_dev = list_entry(ptr,
319 struct kvm_assigned_dev_kernel,
320 list);
321
322 kvm_free_assigned_device(kvm, assigned_dev);
323 }
324}
325
Sheng Yange56d5322009-03-12 21:45:39 +0800326static int assigned_device_enable_host_intx(struct kvm *kvm,
327 struct kvm_assigned_dev_kernel *dev)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800328{
Sheng Yange56d5322009-03-12 21:45:39 +0800329 dev->host_irq = dev->dev->irq;
330 /* Even though this is PCI, we don't want to use shared
331 * interrupts. Sharing host devices with guest-assigned devices
332 * on the same interrupt line is not a happy situation: there
333 * are going to be long delays in accepting, acking, etc.
334 */
335 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
336 0, "kvm_assigned_intx_device", (void *)dev))
337 return -EIO;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800338 return 0;
339}
340
Sheng Yange56d5322009-03-12 21:45:39 +0800341#ifdef __KVM_HAVE_MSI
342static int assigned_device_enable_host_msi(struct kvm *kvm,
343 struct kvm_assigned_dev_kernel *dev)
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800344{
345 int r;
346
Sheng Yange56d5322009-03-12 21:45:39 +0800347 if (!dev->dev->msi_enabled) {
348 r = pci_enable_msi(dev->dev);
349 if (r)
350 return r;
Sheng Yang5319c662008-11-24 14:32:57 +0800351 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800352
Sheng Yange56d5322009-03-12 21:45:39 +0800353 dev->host_irq = dev->dev->irq;
354 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
355 "kvm_assigned_msi_device", (void *)dev)) {
356 pci_disable_msi(dev->dev);
357 return -EIO;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800358 }
359
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800360 return 0;
361}
362#endif
363
Sheng Yangd510d6c2009-02-25 17:22:28 +0800364#ifdef __KVM_HAVE_MSIX
Sheng Yange56d5322009-03-12 21:45:39 +0800365static int assigned_device_enable_host_msix(struct kvm *kvm,
366 struct kvm_assigned_dev_kernel *dev)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800367{
Sheng Yange56d5322009-03-12 21:45:39 +0800368 int i, r = -EINVAL;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800369
Sheng Yange56d5322009-03-12 21:45:39 +0800370 /* host_msix_entries and guest_msix_entries should have been
371 * initialized */
372 if (dev->entries_nr == 0)
373 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800374
Sheng Yange56d5322009-03-12 21:45:39 +0800375 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
376 if (r)
377 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800378
Sheng Yange56d5322009-03-12 21:45:39 +0800379 for (i = 0; i < dev->entries_nr; i++) {
380 r = request_irq(dev->host_msix_entries[i].vector,
381 kvm_assigned_dev_intr, 0,
382 "kvm_assigned_msix_device",
383 (void *)dev);
384 /* FIXME: free requested_irq's on failure */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800385 if (r)
386 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800387 }
388
Sheng Yange56d5322009-03-12 21:45:39 +0800389 return 0;
390}
Sheng Yangd510d6c2009-02-25 17:22:28 +0800391
Sheng Yange56d5322009-03-12 21:45:39 +0800392#endif
393
394static int assigned_device_enable_guest_intx(struct kvm *kvm,
395 struct kvm_assigned_dev_kernel *dev,
396 struct kvm_assigned_irq *irq)
397{
398 dev->guest_irq = irq->guest_irq;
399 dev->ack_notifier.gsi = irq->guest_irq;
400 return 0;
401}
402
403#ifdef __KVM_HAVE_MSI
404static int assigned_device_enable_guest_msi(struct kvm *kvm,
405 struct kvm_assigned_dev_kernel *dev,
406 struct kvm_assigned_irq *irq)
407{
408 dev->guest_irq = irq->guest_irq;
409 dev->ack_notifier.gsi = -1;
410 return 0;
411}
412#endif
413#ifdef __KVM_HAVE_MSIX
414static int assigned_device_enable_guest_msix(struct kvm *kvm,
415 struct kvm_assigned_dev_kernel *dev,
416 struct kvm_assigned_irq *irq)
417{
418 dev->guest_irq = irq->guest_irq;
419 dev->ack_notifier.gsi = -1;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800420 return 0;
421}
422#endif
423
Sheng Yange56d5322009-03-12 21:45:39 +0800424static int assign_host_irq(struct kvm *kvm,
425 struct kvm_assigned_dev_kernel *dev,
426 __u32 host_irq_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800427{
Sheng Yange56d5322009-03-12 21:45:39 +0800428 int r = -EEXIST;
429
430 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
431 return r;
432
433 switch (host_irq_type) {
434 case KVM_DEV_IRQ_HOST_INTX:
435 r = assigned_device_enable_host_intx(kvm, dev);
436 break;
437#ifdef __KVM_HAVE_MSI
438 case KVM_DEV_IRQ_HOST_MSI:
439 r = assigned_device_enable_host_msi(kvm, dev);
440 break;
441#endif
442#ifdef __KVM_HAVE_MSIX
443 case KVM_DEV_IRQ_HOST_MSIX:
444 r = assigned_device_enable_host_msix(kvm, dev);
445 break;
446#endif
447 default:
448 r = -EINVAL;
449 }
450
451 if (!r)
452 dev->irq_requested_type |= host_irq_type;
453
454 return r;
455}
456
457static int assign_guest_irq(struct kvm *kvm,
458 struct kvm_assigned_dev_kernel *dev,
459 struct kvm_assigned_irq *irq,
460 unsigned long guest_irq_type)
461{
462 int id;
463 int r = -EEXIST;
464
465 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
466 return r;
467
468 id = kvm_request_irq_source_id(kvm);
469 if (id < 0)
470 return id;
471
472 dev->irq_source_id = id;
473
474 switch (guest_irq_type) {
475 case KVM_DEV_IRQ_GUEST_INTX:
476 r = assigned_device_enable_guest_intx(kvm, dev, irq);
477 break;
478#ifdef __KVM_HAVE_MSI
479 case KVM_DEV_IRQ_GUEST_MSI:
480 r = assigned_device_enable_guest_msi(kvm, dev, irq);
481 break;
482#endif
483#ifdef __KVM_HAVE_MSIX
484 case KVM_DEV_IRQ_GUEST_MSIX:
485 r = assigned_device_enable_guest_msix(kvm, dev, irq);
486 break;
487#endif
488 default:
489 r = -EINVAL;
490 }
491
492 if (!r) {
493 dev->irq_requested_type |= guest_irq_type;
494 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
495 } else
496 kvm_free_irq_source_id(kvm, dev->irq_source_id);
497
498 return r;
499}
500
501/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
502static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
503 struct kvm_assigned_irq *assigned_irq)
504{
505 int r = -EINVAL;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800506 struct kvm_assigned_dev_kernel *match;
Sheng Yange56d5322009-03-12 21:45:39 +0800507 unsigned long host_irq_type, guest_irq_type;
508
509 if (!capable(CAP_SYS_RAWIO))
510 return -EPERM;
511
512 if (!irqchip_in_kernel(kvm))
513 return r;
514
515 mutex_lock(&kvm->lock);
516 r = -ENODEV;
517 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
518 assigned_irq->assigned_dev_id);
519 if (!match)
520 goto out;
521
522 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
523 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
524
525 r = -EINVAL;
526 /* can only assign one type at a time */
527 if (hweight_long(host_irq_type) > 1)
528 goto out;
529 if (hweight_long(guest_irq_type) > 1)
530 goto out;
531 if (host_irq_type == 0 && guest_irq_type == 0)
532 goto out;
533
534 r = 0;
535 if (host_irq_type)
536 r = assign_host_irq(kvm, match, host_irq_type);
537 if (r)
538 goto out;
539
540 if (guest_irq_type)
541 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
542out:
543 mutex_unlock(&kvm->lock);
544 return r;
545}
546
547static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
548 struct kvm_assigned_irq
549 *assigned_irq)
550{
551 int r = -ENODEV;
552 struct kvm_assigned_dev_kernel *match;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800553
554 mutex_lock(&kvm->lock);
555
556 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
557 assigned_irq->assigned_dev_id);
Sheng Yange56d5322009-03-12 21:45:39 +0800558 if (!match)
559 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800560
Sheng Yange56d5322009-03-12 21:45:39 +0800561 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
562out:
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800563 mutex_unlock(&kvm->lock);
564 return r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800565}
566
567static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
568 struct kvm_assigned_pci_dev *assigned_dev)
569{
570 int r = 0;
571 struct kvm_assigned_dev_kernel *match;
572 struct pci_dev *dev;
573
Mark McLoughlin682edb42009-02-05 18:23:46 +0000574 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800575 mutex_lock(&kvm->lock);
576
577 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
578 assigned_dev->assigned_dev_id);
579 if (match) {
580 /* device already assigned */
Sheng Yange56d5322009-03-12 21:45:39 +0800581 r = -EEXIST;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800582 goto out;
583 }
584
585 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
586 if (match == NULL) {
587 printk(KERN_INFO "%s: Couldn't allocate memory\n",
588 __func__);
589 r = -ENOMEM;
590 goto out;
591 }
592 dev = pci_get_bus_and_slot(assigned_dev->busnr,
593 assigned_dev->devfn);
594 if (!dev) {
595 printk(KERN_INFO "%s: host device not found\n", __func__);
596 r = -EINVAL;
597 goto out_free;
598 }
599 if (pci_enable_device(dev)) {
600 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
601 r = -EBUSY;
602 goto out_put;
603 }
604 r = pci_request_regions(dev, "kvm_assigned_device");
605 if (r) {
606 printk(KERN_INFO "%s: Could not get access to device regions\n",
607 __func__);
608 goto out_disable;
609 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800610
611 pci_reset_function(dev);
612
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800613 match->assigned_dev_id = assigned_dev->assigned_dev_id;
614 match->host_busnr = assigned_dev->busnr;
615 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800616 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800617 match->dev = dev;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000618 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800619 match->kvm = kvm;
Sheng Yange56d5322009-03-12 21:45:39 +0800620 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
621 INIT_WORK(&match->interrupt_work,
622 kvm_assigned_dev_interrupt_work_handler);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800623
624 list_add(&match->list, &kvm->arch.assigned_dev_head);
625
626 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100627 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800628 r = kvm_iommu_map_guest(kvm);
629 if (r)
630 goto out_list_del;
631 }
632 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800633 if (r)
634 goto out_list_del;
635 }
636
637out:
638 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000639 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800640 return r;
641out_list_del:
642 list_del(&match->list);
643 pci_release_regions(dev);
644out_disable:
645 pci_disable_device(dev);
646out_put:
647 pci_dev_put(dev);
648out_free:
649 kfree(match);
650 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000651 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800652 return r;
653}
654#endif
655
Weidong Han0a920352008-12-02 21:24:23 +0800656#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
657static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
658 struct kvm_assigned_pci_dev *assigned_dev)
659{
660 int r = 0;
661 struct kvm_assigned_dev_kernel *match;
662
663 mutex_lock(&kvm->lock);
664
665 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
666 assigned_dev->assigned_dev_id);
667 if (!match) {
668 printk(KERN_INFO "%s: device hasn't been assigned before, "
669 "so cannot be deassigned\n", __func__);
670 r = -EINVAL;
671 goto out;
672 }
673
Weidong Han4a906e42009-02-13 17:27:51 +0800674 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800675 kvm_deassign_device(kvm, match);
676
677 kvm_free_assigned_device(kvm, match);
678
679out:
680 mutex_unlock(&kvm->lock);
681 return r;
682}
683#endif
684
James Morris5aacf0c2006-12-22 01:04:55 -0800685static inline int valid_vcpu(int n)
686{
687 return likely(n >= 0 && n < KVM_MAX_VCPUS);
688}
689
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800690inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300691{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100692 if (pfn_valid(pfn)) {
693 struct page *page = compound_head(pfn_to_page(pfn));
694 return PageReserved(page);
695 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300696
697 return true;
698}
699
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700/*
701 * Switches to specified vcpu, until a matching vcpu_put()
702 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200703void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704{
Avi Kivity15ad7142007-07-11 18:17:21 +0300705 int cpu;
706
Avi Kivitybccf2152007-02-21 18:04:26 +0200707 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300708 cpu = get_cpu();
709 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200710 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300711 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200712}
713
Carsten Otte313a3dc2007-10-11 19:16:52 +0200714void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715{
Avi Kivity15ad7142007-07-11 18:17:21 +0300716 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200717 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300718 preempt_notifier_unregister(&vcpu->preempt_notifier);
719 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720 mutex_unlock(&vcpu->mutex);
721}
722
Avi Kivityd9e368d2007-06-07 19:18:30 +0300723static void ack_flush(void *_completed)
724{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300725}
726
Rusty Russell49846892008-12-08 20:26:24 +1030727static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300728{
Avi Kivity597a5f52008-07-20 14:24:22 +0300729 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030730 cpumask_var_t cpus;
731 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300732 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300733
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030734 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
735 cpumask_clear(cpus);
736
Avi Kivity597a5f52008-07-20 14:24:22 +0300737 me = get_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000738 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
739 vcpu = kvm->vcpus[i];
740 if (!vcpu)
741 continue;
Rusty Russell49846892008-12-08 20:26:24 +1030742 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300743 continue;
744 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030745 if (cpus != NULL && cpu != -1 && cpu != me)
746 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300747 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030748 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;
Avi Kivity597a5f52008-07-20 14:24:22 +0300754 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030755 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030756 return called;
757}
758
759void kvm_flush_remote_tlbs(struct kvm *kvm)
760{
761 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
762 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300763}
764
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500765void kvm_reload_remote_mmus(struct kvm *kvm)
766{
Rusty Russell49846892008-12-08 20:26:24 +1030767 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500768}
769
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000770int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
771{
772 struct page *page;
773 int r;
774
775 mutex_init(&vcpu->mutex);
776 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000777 vcpu->kvm = kvm;
778 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300779 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000780
781 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
782 if (!page) {
783 r = -ENOMEM;
784 goto fail;
785 }
786 vcpu->run = page_address(page);
787
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800788 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000789 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800790 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000791 return 0;
792
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000793fail_free_run:
794 free_page((unsigned long)vcpu->run);
795fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000796 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000797}
798EXPORT_SYMBOL_GPL(kvm_vcpu_init);
799
800void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
801{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800802 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000803 free_page((unsigned long)vcpu->run);
804}
805EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
806
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200807#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
808static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
809{
810 return container_of(mn, struct kvm, mmu_notifier);
811}
812
813static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
814 struct mm_struct *mm,
815 unsigned long address)
816{
817 struct kvm *kvm = mmu_notifier_to_kvm(mn);
818 int need_tlb_flush;
819
820 /*
821 * When ->invalidate_page runs, the linux pte has been zapped
822 * already but the page is still allocated until
823 * ->invalidate_page returns. So if we increase the sequence
824 * here the kvm page fault will notice if the spte can't be
825 * established because the page is going to be freed. If
826 * instead the kvm page fault establishes the spte before
827 * ->invalidate_page runs, kvm_unmap_hva will release it
828 * before returning.
829 *
830 * The sequence increase only need to be seen at spin_unlock
831 * time, and not at spin_lock time.
832 *
833 * Increasing the sequence after the spin_unlock would be
834 * unsafe because the kvm page fault could then establish the
835 * pte after kvm_unmap_hva returned, without noticing the page
836 * is going to be freed.
837 */
838 spin_lock(&kvm->mmu_lock);
839 kvm->mmu_notifier_seq++;
840 need_tlb_flush = kvm_unmap_hva(kvm, address);
841 spin_unlock(&kvm->mmu_lock);
842
843 /* we've to flush the tlb before the pages can be freed */
844 if (need_tlb_flush)
845 kvm_flush_remote_tlbs(kvm);
846
847}
848
849static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
850 struct mm_struct *mm,
851 unsigned long start,
852 unsigned long end)
853{
854 struct kvm *kvm = mmu_notifier_to_kvm(mn);
855 int need_tlb_flush = 0;
856
857 spin_lock(&kvm->mmu_lock);
858 /*
859 * The count increase must become visible at unlock time as no
860 * spte can be established without taking the mmu_lock and
861 * count is also read inside the mmu_lock critical section.
862 */
863 kvm->mmu_notifier_count++;
864 for (; start < end; start += PAGE_SIZE)
865 need_tlb_flush |= kvm_unmap_hva(kvm, start);
866 spin_unlock(&kvm->mmu_lock);
867
868 /* we've to flush the tlb before the pages can be freed */
869 if (need_tlb_flush)
870 kvm_flush_remote_tlbs(kvm);
871}
872
873static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
874 struct mm_struct *mm,
875 unsigned long start,
876 unsigned long end)
877{
878 struct kvm *kvm = mmu_notifier_to_kvm(mn);
879
880 spin_lock(&kvm->mmu_lock);
881 /*
882 * This sequence increase will notify the kvm page fault that
883 * the page that is going to be mapped in the spte could have
884 * been freed.
885 */
886 kvm->mmu_notifier_seq++;
887 /*
888 * The above sequence increase must be visible before the
889 * below count decrease but both values are read by the kvm
890 * page fault under mmu_lock spinlock so we don't need to add
891 * a smb_wmb() here in between the two.
892 */
893 kvm->mmu_notifier_count--;
894 spin_unlock(&kvm->mmu_lock);
895
896 BUG_ON(kvm->mmu_notifier_count < 0);
897}
898
899static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
900 struct mm_struct *mm,
901 unsigned long address)
902{
903 struct kvm *kvm = mmu_notifier_to_kvm(mn);
904 int young;
905
906 spin_lock(&kvm->mmu_lock);
907 young = kvm_age_hva(kvm, address);
908 spin_unlock(&kvm->mmu_lock);
909
910 if (young)
911 kvm_flush_remote_tlbs(kvm);
912
913 return young;
914}
915
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100916static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
917 struct mm_struct *mm)
918{
919 struct kvm *kvm = mmu_notifier_to_kvm(mn);
920 kvm_arch_flush_shadow(kvm);
921}
922
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200923static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
924 .invalidate_page = kvm_mmu_notifier_invalidate_page,
925 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
926 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
927 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100928 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200929};
930#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
931
Avi Kivityf17abe92007-02-21 19:28:04 +0200932static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800934 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200935#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
936 struct page *page;
937#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800939 if (IS_ERR(kvm))
940 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200941#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200942 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200943 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
944#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
Laurent Vivier5f94c172008-05-30 16:05:54 +0200946#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
947 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
948 if (!page) {
949 kfree(kvm);
950 return ERR_PTR(-ENOMEM);
951 }
952 kvm->coalesced_mmio_ring =
953 (struct kvm_coalesced_mmio_ring *)page_address(page);
954#endif
955
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200956#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
957 {
958 int err;
959 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
960 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
961 if (err) {
962#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
963 put_page(page);
964#endif
965 kfree(kvm);
966 return ERR_PTR(err);
967 }
968 }
969#endif
970
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200971 kvm->mm = current->mm;
972 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500973 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300974 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800975 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400976 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200977 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300978 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000979 spin_lock(&kvm_lock);
980 list_add(&kvm->vm_list, &vm_list);
981 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200982#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
983 kvm_coalesced_mmio_init(kvm);
984#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800985out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200986 return kvm;
987}
988
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989/*
990 * Free any memory in @free but not in @dont.
991 */
992static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
993 struct kvm_memory_slot *dont)
994{
Izik Eidus290fc382007-09-27 14:11:22 +0200995 if (!dont || free->rmap != dont->rmap)
996 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997
998 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
999 vfree(free->dirty_bitmap);
1000
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001001 if (!dont || free->lpage_info != dont->lpage_info)
1002 vfree(free->lpage_info);
1003
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001005 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001006 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001007 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008}
1009
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001010void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011{
1012 int i;
1013
1014 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001015 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016}
1017
Avi Kivityf17abe92007-02-21 19:28:04 +02001018static void kvm_destroy_vm(struct kvm *kvm)
1019{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001020 struct mm_struct *mm = kvm->mm;
1021
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001022 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001023 spin_lock(&kvm_lock);
1024 list_del(&kvm->vm_list);
1025 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001026 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001027 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001028 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001029#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1030 if (kvm->coalesced_mmio_ring != NULL)
1031 free_page((unsigned long)kvm->coalesced_mmio_ring);
1032#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001033#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1034 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
1035#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001036 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001037 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001038}
1039
Izik Eidusd39f13b2008-03-30 16:01:25 +03001040void kvm_get_kvm(struct kvm *kvm)
1041{
1042 atomic_inc(&kvm->users_count);
1043}
1044EXPORT_SYMBOL_GPL(kvm_get_kvm);
1045
1046void kvm_put_kvm(struct kvm *kvm)
1047{
1048 if (atomic_dec_and_test(&kvm->users_count))
1049 kvm_destroy_vm(kvm);
1050}
1051EXPORT_SYMBOL_GPL(kvm_put_kvm);
1052
1053
Avi Kivityf17abe92007-02-21 19:28:04 +02001054static int kvm_vm_release(struct inode *inode, struct file *filp)
1055{
1056 struct kvm *kvm = filp->private_data;
1057
Izik Eidusd39f13b2008-03-30 16:01:25 +03001058 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 return 0;
1060}
1061
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 * Allocate some memory and give it an address in the guest physical address
1064 * space.
1065 *
1066 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001067 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001068 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001070int __kvm_set_memory_region(struct kvm *kvm,
1071 struct kvm_userspace_memory_region *mem,
1072 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073{
1074 int r;
1075 gfn_t base_gfn;
1076 unsigned long npages;
Avi Kivity99894a72009-03-29 16:31:25 +03001077 int largepages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 unsigned long i;
1079 struct kvm_memory_slot *memslot;
1080 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081
1082 r = -EINVAL;
1083 /* General sanity checks */
1084 if (mem->memory_size & (PAGE_SIZE - 1))
1085 goto out;
1086 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1087 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001088 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001089 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001090 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 goto out;
1092 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1093 goto out;
1094
1095 memslot = &kvm->memslots[mem->slot];
1096 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1097 npages = mem->memory_size >> PAGE_SHIFT;
1098
1099 if (!npages)
1100 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1101
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 new = old = *memslot;
1103
1104 new.base_gfn = base_gfn;
1105 new.npages = npages;
1106 new.flags = mem->flags;
1107
1108 /* Disallow changing a memory slot's size. */
1109 r = -EINVAL;
1110 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001111 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112
1113 /* Check for overlaps */
1114 r = -EEXIST;
1115 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1116 struct kvm_memory_slot *s = &kvm->memslots[i];
1117
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001118 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 continue;
1120 if (!((base_gfn + npages <= s->base_gfn) ||
1121 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001122 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 /* Free page dirty bitmap if unneeded */
1126 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001127 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128
1129 r = -ENOMEM;
1130
1131 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001132#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001133 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001134 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001135
1136 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001137 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001138
Izik Eidus290fc382007-09-27 14:11:22 +02001139 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001140
Izik Eidus80b14b52007-10-25 11:54:04 +02001141 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001142 /*
1143 * hva_to_rmmap() serialzies with the mmu_lock and to be
1144 * safe it has to ignore memslots with !user_alloc &&
1145 * !userspace_addr.
1146 */
1147 if (user_alloc)
1148 new.userspace_addr = mem->userspace_addr;
1149 else
1150 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001152 if (npages && !new.lpage_info) {
Avi Kivity99894a72009-03-29 16:31:25 +03001153 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1154 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001155
1156 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1157
1158 if (!new.lpage_info)
1159 goto out_free;
1160
1161 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1162
1163 if (base_gfn % KVM_PAGES_PER_HPAGE)
1164 new.lpage_info[0].write_count = 1;
1165 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1166 new.lpage_info[largepages-1].write_count = 1;
1167 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168
1169 /* Allocate page dirty bitmap if needed */
1170 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1171 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1172
1173 new.dirty_bitmap = vmalloc(dirty_bytes);
1174 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001175 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 memset(new.dirty_bitmap, 0, dirty_bytes);
1177 }
Carsten Otteeff01142008-06-27 15:05:31 +02001178#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001180 if (!npages)
1181 kvm_arch_flush_shadow(kvm);
1182
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001183 spin_lock(&kvm->mmu_lock);
1184 if (mem->slot >= kvm->nmemslots)
1185 kvm->nmemslots = mem->slot + 1;
1186
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001188 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189
Zhang Xiantao0de10342007-11-20 16:25:04 +08001190 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1191 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001192 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001193 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001194 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001195 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001196 }
1197
Glauber Costa6f897242008-12-03 13:40:51 -02001198 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1199 /* Slot deletion case: we have to update the current slot */
1200 if (!npages)
1201 *memslot = old;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001202#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001203 /* map the pages in iommu page table */
1204 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1205 if (r)
1206 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001207#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208 return 0;
1209
Sheng Yangf78e0e22007-10-29 09:40:42 +08001210out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211 kvm_free_physmem_slot(&new, &old);
1212out:
1213 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001214
1215}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001216EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1217
1218int kvm_set_memory_region(struct kvm *kvm,
1219 struct kvm_userspace_memory_region *mem,
1220 int user_alloc)
1221{
1222 int r;
1223
Izik Eidus72dc67a2008-02-10 18:04:15 +02001224 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001225 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001226 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001227 return r;
1228}
Izik Eidus210c7c42007-10-24 23:52:57 +02001229EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1230
Carsten Otte1fe779f2007-10-29 16:08:35 +01001231int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1232 struct
1233 kvm_userspace_memory_region *mem,
1234 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001235{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001236 if (mem->slot >= KVM_MEMORY_SLOTS)
1237 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001238 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239}
1240
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001241int kvm_get_dirty_log(struct kvm *kvm,
1242 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243{
1244 struct kvm_memory_slot *memslot;
1245 int r, i;
1246 int n;
1247 unsigned long any = 0;
1248
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 r = -EINVAL;
1250 if (log->slot >= KVM_MEMORY_SLOTS)
1251 goto out;
1252
1253 memslot = &kvm->memslots[log->slot];
1254 r = -ENOENT;
1255 if (!memslot->dirty_bitmap)
1256 goto out;
1257
Uri Lublincd1a4a92007-02-22 16:43:09 +02001258 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259
Uri Lublincd1a4a92007-02-22 16:43:09 +02001260 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261 any = memslot->dirty_bitmap[i];
1262
1263 r = -EFAULT;
1264 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1265 goto out;
1266
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001267 if (any)
1268 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269
1270 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 return r;
1273}
1274
Izik Eiduscea7bb22007-10-17 19:17:48 +02001275int is_error_page(struct page *page)
1276{
1277 return page == bad_page;
1278}
1279EXPORT_SYMBOL_GPL(is_error_page);
1280
Anthony Liguori35149e22008-04-02 14:46:56 -05001281int is_error_pfn(pfn_t pfn)
1282{
1283 return pfn == bad_pfn;
1284}
1285EXPORT_SYMBOL_GPL(is_error_pfn);
1286
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001287static inline unsigned long bad_hva(void)
1288{
1289 return PAGE_OFFSET;
1290}
1291
1292int kvm_is_error_hva(unsigned long addr)
1293{
1294 return addr == bad_hva();
1295}
1296EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1297
Izik Eidus28430992008-10-03 17:40:32 +03001298struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299{
1300 int i;
1301
1302 for (i = 0; i < kvm->nmemslots; ++i) {
1303 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1304
1305 if (gfn >= memslot->base_gfn
1306 && gfn < memslot->base_gfn + memslot->npages)
1307 return memslot;
1308 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001309 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310}
Izik Eidus28430992008-10-03 17:40:32 +03001311EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001312
1313struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1314{
1315 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001316 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001317}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318
Izik Eiduse0d62c72007-10-24 23:57:46 +02001319int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1320{
1321 int i;
1322
1323 gfn = unalias_gfn(kvm, gfn);
1324 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1325 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1326
1327 if (gfn >= memslot->base_gfn
1328 && gfn < memslot->base_gfn + memslot->npages)
1329 return 1;
1330 }
1331 return 0;
1332}
1333EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1334
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001335unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001336{
1337 struct kvm_memory_slot *slot;
1338
1339 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001340 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001341 if (!slot)
1342 return bad_hva();
1343 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1344}
Sheng Yang0d150292008-04-25 21:44:50 +08001345EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001346
Anthony Liguori35149e22008-04-02 14:46:56 -05001347pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001348{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001349 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001350 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001351 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001352 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001353
Avi Kivity60395222007-10-21 11:03:36 +02001354 might_sleep();
1355
Izik Eidus539cb662007-11-11 22:05:04 +02001356 addr = gfn_to_hva(kvm, gfn);
1357 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001358 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001359 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001360 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001361
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001362 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001363
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001364 if (unlikely(npages != 1)) {
1365 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001366
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001367 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001368 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001369
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001370 if (vma == NULL || addr < vma->vm_start ||
1371 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001372 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001373 get_page(bad_page);
1374 return page_to_pfn(bad_page);
1375 }
1376
1377 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001378 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001379 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001380 } else
1381 pfn = page_to_pfn(page[0]);
1382
1383 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001384}
1385
1386EXPORT_SYMBOL_GPL(gfn_to_pfn);
1387
1388struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1389{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001390 pfn_t pfn;
1391
1392 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001393 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001394 return pfn_to_page(pfn);
1395
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001396 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001397
1398 get_page(bad_page);
1399 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001400}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001401
Avi Kivity954bbbc2007-03-30 14:02:32 +03001402EXPORT_SYMBOL_GPL(gfn_to_page);
1403
Izik Eidusb4231d62007-11-20 11:49:33 +02001404void kvm_release_page_clean(struct page *page)
1405{
Anthony Liguori35149e22008-04-02 14:46:56 -05001406 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001407}
1408EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1409
Anthony Liguori35149e22008-04-02 14:46:56 -05001410void kvm_release_pfn_clean(pfn_t pfn)
1411{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001412 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001413 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001414}
1415EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1416
Izik Eidusb4231d62007-11-20 11:49:33 +02001417void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001418{
Anthony Liguori35149e22008-04-02 14:46:56 -05001419 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001420}
Izik Eidusb4231d62007-11-20 11:49:33 +02001421EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001422
Anthony Liguori35149e22008-04-02 14:46:56 -05001423void kvm_release_pfn_dirty(pfn_t pfn)
1424{
1425 kvm_set_pfn_dirty(pfn);
1426 kvm_release_pfn_clean(pfn);
1427}
1428EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1429
1430void kvm_set_page_dirty(struct page *page)
1431{
1432 kvm_set_pfn_dirty(page_to_pfn(page));
1433}
1434EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1435
1436void kvm_set_pfn_dirty(pfn_t pfn)
1437{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001438 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001439 struct page *page = pfn_to_page(pfn);
1440 if (!PageReserved(page))
1441 SetPageDirty(page);
1442 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001443}
1444EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1445
1446void kvm_set_pfn_accessed(pfn_t pfn)
1447{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001448 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001449 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001450}
1451EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1452
1453void kvm_get_pfn(pfn_t pfn)
1454{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001455 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001456 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001457}
1458EXPORT_SYMBOL_GPL(kvm_get_pfn);
1459
Izik Eidus195aefd2007-10-01 22:14:18 +02001460static int next_segment(unsigned long len, int offset)
1461{
1462 if (len > PAGE_SIZE - offset)
1463 return PAGE_SIZE - offset;
1464 else
1465 return len;
1466}
1467
1468int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1469 int len)
1470{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001471 int r;
1472 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001473
Izik Eiduse0506bc2007-11-11 22:10:22 +02001474 addr = gfn_to_hva(kvm, gfn);
1475 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001476 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001477 r = copy_from_user(data, (void __user *)addr + offset, len);
1478 if (r)
1479 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001480 return 0;
1481}
1482EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1483
1484int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1485{
1486 gfn_t gfn = gpa >> PAGE_SHIFT;
1487 int seg;
1488 int offset = offset_in_page(gpa);
1489 int ret;
1490
1491 while ((seg = next_segment(len, offset)) != 0) {
1492 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1493 if (ret < 0)
1494 return ret;
1495 offset = 0;
1496 len -= seg;
1497 data += seg;
1498 ++gfn;
1499 }
1500 return 0;
1501}
1502EXPORT_SYMBOL_GPL(kvm_read_guest);
1503
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001504int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1505 unsigned long len)
1506{
1507 int r;
1508 unsigned long addr;
1509 gfn_t gfn = gpa >> PAGE_SHIFT;
1510 int offset = offset_in_page(gpa);
1511
1512 addr = gfn_to_hva(kvm, gfn);
1513 if (kvm_is_error_hva(addr))
1514 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001515 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001516 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001517 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001518 if (r)
1519 return -EFAULT;
1520 return 0;
1521}
1522EXPORT_SYMBOL(kvm_read_guest_atomic);
1523
Izik Eidus195aefd2007-10-01 22:14:18 +02001524int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1525 int offset, int len)
1526{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001527 int r;
1528 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001529
Izik Eiduse0506bc2007-11-11 22:10:22 +02001530 addr = gfn_to_hva(kvm, gfn);
1531 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001532 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001533 r = copy_to_user((void __user *)addr + offset, data, len);
1534 if (r)
1535 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001536 mark_page_dirty(kvm, gfn);
1537 return 0;
1538}
1539EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1540
1541int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1542 unsigned long len)
1543{
1544 gfn_t gfn = gpa >> PAGE_SHIFT;
1545 int seg;
1546 int offset = offset_in_page(gpa);
1547 int ret;
1548
1549 while ((seg = next_segment(len, offset)) != 0) {
1550 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1551 if (ret < 0)
1552 return ret;
1553 offset = 0;
1554 len -= seg;
1555 data += seg;
1556 ++gfn;
1557 }
1558 return 0;
1559}
1560
1561int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1562{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001563 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001564}
1565EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1566
1567int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1568{
1569 gfn_t gfn = gpa >> PAGE_SHIFT;
1570 int seg;
1571 int offset = offset_in_page(gpa);
1572 int ret;
1573
1574 while ((seg = next_segment(len, offset)) != 0) {
1575 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1576 if (ret < 0)
1577 return ret;
1578 offset = 0;
1579 len -= seg;
1580 ++gfn;
1581 }
1582 return 0;
1583}
1584EXPORT_SYMBOL_GPL(kvm_clear_guest);
1585
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1587{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001588 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589
Uri Lublin3b6fff12007-10-30 10:42:09 +02001590 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001591 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001592 if (memslot && memslot->dirty_bitmap) {
1593 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594
Rusty Russell7e9d6192007-07-31 20:41:14 +10001595 /* avoid RMW */
1596 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1597 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598 }
1599}
1600
Eddie Dongb6958ce2007-07-18 12:15:21 +03001601/*
1602 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1603 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001604void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001605{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001606 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001607
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001608 for (;;) {
1609 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001610
Marcelo Tosattid7690172008-09-08 15:23:48 -03001611 if (kvm_cpu_has_interrupt(vcpu) ||
1612 kvm_cpu_has_pending_timer(vcpu) ||
1613 kvm_arch_vcpu_runnable(vcpu)) {
1614 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001615 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001616 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001617 if (signal_pending(current))
1618 break;
1619
Eddie Dongb6958ce2007-07-18 12:15:21 +03001620 vcpu_put(vcpu);
1621 schedule();
1622 vcpu_load(vcpu);
1623 }
1624
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001625 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001626}
1627
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628void kvm_resched(struct kvm_vcpu *vcpu)
1629{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001630 if (!need_resched())
1631 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633}
1634EXPORT_SYMBOL_GPL(kvm_resched);
1635
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001636static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001637{
1638 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001639 struct page *page;
1640
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001641 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001642 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001643#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001644 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001645 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001646#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001647#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1648 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1649 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1650#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001651 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001652 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001653 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001654 vmf->page = page;
1655 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001656}
1657
1658static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001659 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001660};
1661
1662static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1663{
1664 vma->vm_ops = &kvm_vcpu_vm_ops;
1665 return 0;
1666}
1667
Avi Kivitybccf2152007-02-21 18:04:26 +02001668static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1669{
1670 struct kvm_vcpu *vcpu = filp->private_data;
1671
Al Viro66c0b392008-04-19 20:33:56 +01001672 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001673 return 0;
1674}
1675
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001676static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001677 .release = kvm_vcpu_release,
1678 .unlocked_ioctl = kvm_vcpu_ioctl,
1679 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001680 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001681};
1682
1683/*
1684 * Allocates an inode for the vcpu.
1685 */
1686static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1687{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001688 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001689 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001690 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001691 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001692}
1693
Avi Kivityc5ea7662007-02-20 18:41:05 +02001694/*
1695 * Creates some virtual cpus. Good luck creating more than one.
1696 */
1697static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1698{
1699 int r;
1700 struct kvm_vcpu *vcpu;
1701
Avi Kivityc5ea7662007-02-20 18:41:05 +02001702 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001703 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001704
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001705 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001706 if (IS_ERR(vcpu))
1707 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001708
Avi Kivity15ad7142007-07-11 18:17:21 +03001709 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1710
Avi Kivity26e52152007-11-20 15:30:24 +02001711 r = kvm_arch_vcpu_setup(vcpu);
1712 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001713 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001714
Shaohua Li11ec2802007-07-23 14:51:37 +08001715 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001716 if (kvm->vcpus[n]) {
1717 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001718 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001719 }
1720 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001721 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001722
1723 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001724 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001725 r = create_vcpu_fd(vcpu);
1726 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001727 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001728 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001729
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001730unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001731 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001732 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001733vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001734 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001735 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001736 return r;
1737}
1738
Avi Kivity1961d272007-03-05 19:46:05 +02001739static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1740{
1741 if (sigset) {
1742 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1743 vcpu->sigset_active = 1;
1744 vcpu->sigset = *sigset;
1745 } else
1746 vcpu->sigset_active = 0;
1747 return 0;
1748}
1749
Sheng Yangc1e01512009-02-25 17:22:26 +08001750#ifdef __KVM_HAVE_MSIX
1751static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1752 struct kvm_assigned_msix_nr *entry_nr)
1753{
1754 int r = 0;
1755 struct kvm_assigned_dev_kernel *adev;
1756
1757 mutex_lock(&kvm->lock);
1758
1759 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1760 entry_nr->assigned_dev_id);
1761 if (!adev) {
1762 r = -EINVAL;
1763 goto msix_nr_out;
1764 }
1765
1766 if (adev->entries_nr == 0) {
1767 adev->entries_nr = entry_nr->entry_nr;
1768 if (adev->entries_nr == 0 ||
1769 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1770 r = -EINVAL;
1771 goto msix_nr_out;
1772 }
1773
1774 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1775 entry_nr->entry_nr,
1776 GFP_KERNEL);
1777 if (!adev->host_msix_entries) {
1778 r = -ENOMEM;
1779 goto msix_nr_out;
1780 }
1781 adev->guest_msix_entries = kzalloc(
1782 sizeof(struct kvm_guest_msix_entry) *
1783 entry_nr->entry_nr, GFP_KERNEL);
1784 if (!adev->guest_msix_entries) {
1785 kfree(adev->host_msix_entries);
1786 r = -ENOMEM;
1787 goto msix_nr_out;
1788 }
1789 } else /* Not allowed set MSI-X number twice */
1790 r = -EINVAL;
1791msix_nr_out:
1792 mutex_unlock(&kvm->lock);
1793 return r;
1794}
1795
1796static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1797 struct kvm_assigned_msix_entry *entry)
1798{
1799 int r = 0, i;
1800 struct kvm_assigned_dev_kernel *adev;
1801
1802 mutex_lock(&kvm->lock);
1803
1804 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1805 entry->assigned_dev_id);
1806
1807 if (!adev) {
1808 r = -EINVAL;
1809 goto msix_entry_out;
1810 }
1811
1812 for (i = 0; i < adev->entries_nr; i++)
1813 if (adev->guest_msix_entries[i].vector == 0 ||
1814 adev->guest_msix_entries[i].entry == entry->entry) {
1815 adev->guest_msix_entries[i].entry = entry->entry;
1816 adev->guest_msix_entries[i].vector = entry->gsi;
1817 adev->host_msix_entries[i].entry = entry->entry;
1818 break;
1819 }
1820 if (i == adev->entries_nr) {
1821 r = -ENOSPC;
1822 goto msix_entry_out;
1823 }
1824
1825msix_entry_out:
1826 mutex_unlock(&kvm->lock);
1827
1828 return r;
1829}
1830#endif
1831
Avi Kivitybccf2152007-02-21 18:04:26 +02001832static long kvm_vcpu_ioctl(struct file *filp,
1833 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834{
Avi Kivitybccf2152007-02-21 18:04:26 +02001835 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001836 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001837 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001838 struct kvm_fpu *fpu = NULL;
1839 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001841 if (vcpu->kvm->mm != current->mm)
1842 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001844 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001845 r = -EINVAL;
1846 if (arg)
1847 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001848 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001851 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001853 r = -ENOMEM;
1854 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1855 if (!kvm_regs)
1856 goto out;
1857 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001859 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001861 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1862 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001864out_free1:
1865 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 break;
1867 }
1868 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001869 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001871 r = -ENOMEM;
1872 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1873 if (!kvm_regs)
1874 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001876 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1877 goto out_free2;
1878 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001880 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001882out_free2:
1883 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884 break;
1885 }
1886 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001887 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1888 r = -ENOMEM;
1889 if (!kvm_sregs)
1890 goto out;
1891 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892 if (r)
1893 goto out;
1894 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001895 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896 goto out;
1897 r = 0;
1898 break;
1899 }
1900 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001901 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1902 r = -ENOMEM;
1903 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001905 r = -EFAULT;
1906 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1907 goto out;
1908 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909 if (r)
1910 goto out;
1911 r = 0;
1912 break;
1913 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001914 case KVM_GET_MP_STATE: {
1915 struct kvm_mp_state mp_state;
1916
1917 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1918 if (r)
1919 goto out;
1920 r = -EFAULT;
1921 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1922 goto out;
1923 r = 0;
1924 break;
1925 }
1926 case KVM_SET_MP_STATE: {
1927 struct kvm_mp_state mp_state;
1928
1929 r = -EFAULT;
1930 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1931 goto out;
1932 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1933 if (r)
1934 goto out;
1935 r = 0;
1936 break;
1937 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938 case KVM_TRANSLATE: {
1939 struct kvm_translation tr;
1940
1941 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001942 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001944 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945 if (r)
1946 goto out;
1947 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001948 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 goto out;
1950 r = 0;
1951 break;
1952 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001953 case KVM_SET_GUEST_DEBUG: {
1954 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955
1956 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001957 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001959 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 if (r)
1961 goto out;
1962 r = 0;
1963 break;
1964 }
Avi Kivity1961d272007-03-05 19:46:05 +02001965 case KVM_SET_SIGNAL_MASK: {
1966 struct kvm_signal_mask __user *sigmask_arg = argp;
1967 struct kvm_signal_mask kvm_sigmask;
1968 sigset_t sigset, *p;
1969
1970 p = NULL;
1971 if (argp) {
1972 r = -EFAULT;
1973 if (copy_from_user(&kvm_sigmask, argp,
1974 sizeof kvm_sigmask))
1975 goto out;
1976 r = -EINVAL;
1977 if (kvm_sigmask.len != sizeof sigset)
1978 goto out;
1979 r = -EFAULT;
1980 if (copy_from_user(&sigset, sigmask_arg->sigset,
1981 sizeof sigset))
1982 goto out;
1983 p = &sigset;
1984 }
1985 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1986 break;
1987 }
Avi Kivityb8836732007-04-01 16:34:31 +03001988 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001989 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1990 r = -ENOMEM;
1991 if (!fpu)
1992 goto out;
1993 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001994 if (r)
1995 goto out;
1996 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001997 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001998 goto out;
1999 r = 0;
2000 break;
2001 }
2002 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002003 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2004 r = -ENOMEM;
2005 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002006 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002007 r = -EFAULT;
2008 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2009 goto out;
2010 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002011 if (r)
2012 goto out;
2013 r = 0;
2014 break;
2015 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002016 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002017 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002018 }
2019out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002020 kfree(fpu);
2021 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002022 return r;
2023}
2024
2025static long kvm_vm_ioctl(struct file *filp,
2026 unsigned int ioctl, unsigned long arg)
2027{
2028 struct kvm *kvm = filp->private_data;
2029 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002030 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002031
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002032 if (kvm->mm != current->mm)
2033 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002034 switch (ioctl) {
2035 case KVM_CREATE_VCPU:
2036 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2037 if (r < 0)
2038 goto out;
2039 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002040 case KVM_SET_USER_MEMORY_REGION: {
2041 struct kvm_userspace_memory_region kvm_userspace_mem;
2042
2043 r = -EFAULT;
2044 if (copy_from_user(&kvm_userspace_mem, argp,
2045 sizeof kvm_userspace_mem))
2046 goto out;
2047
2048 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049 if (r)
2050 goto out;
2051 break;
2052 }
2053 case KVM_GET_DIRTY_LOG: {
2054 struct kvm_dirty_log log;
2055
2056 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002057 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002059 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 if (r)
2061 goto out;
2062 break;
2063 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002064#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2065 case KVM_REGISTER_COALESCED_MMIO: {
2066 struct kvm_coalesced_mmio_zone zone;
2067 r = -EFAULT;
2068 if (copy_from_user(&zone, argp, sizeof zone))
2069 goto out;
2070 r = -ENXIO;
2071 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2072 if (r)
2073 goto out;
2074 r = 0;
2075 break;
2076 }
2077 case KVM_UNREGISTER_COALESCED_MMIO: {
2078 struct kvm_coalesced_mmio_zone zone;
2079 r = -EFAULT;
2080 if (copy_from_user(&zone, argp, sizeof zone))
2081 goto out;
2082 r = -ENXIO;
2083 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2084 if (r)
2085 goto out;
2086 r = 0;
2087 break;
2088 }
2089#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002090#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2091 case KVM_ASSIGN_PCI_DEVICE: {
2092 struct kvm_assigned_pci_dev assigned_dev;
2093
2094 r = -EFAULT;
2095 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2096 goto out;
2097 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2098 if (r)
2099 goto out;
2100 break;
2101 }
2102 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002103 r = -EOPNOTSUPP;
2104 break;
2105 }
2106#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2107 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002108 struct kvm_assigned_irq assigned_irq;
2109
2110 r = -EFAULT;
2111 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2112 goto out;
2113 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2114 if (r)
2115 goto out;
2116 break;
2117 }
Sheng Yange56d5322009-03-12 21:45:39 +08002118 case KVM_DEASSIGN_DEV_IRQ: {
2119 struct kvm_assigned_irq assigned_irq;
2120
2121 r = -EFAULT;
2122 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2123 goto out;
2124 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2125 if (r)
2126 goto out;
2127 break;
2128 }
2129#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002130#endif
Weidong Han0a920352008-12-02 21:24:23 +08002131#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2132 case KVM_DEASSIGN_PCI_DEVICE: {
2133 struct kvm_assigned_pci_dev assigned_dev;
2134
2135 r = -EFAULT;
2136 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2137 goto out;
2138 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2139 if (r)
2140 goto out;
2141 break;
2142 }
2143#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002144#ifdef KVM_CAP_IRQ_ROUTING
2145 case KVM_SET_GSI_ROUTING: {
2146 struct kvm_irq_routing routing;
2147 struct kvm_irq_routing __user *urouting;
2148 struct kvm_irq_routing_entry *entries;
2149
2150 r = -EFAULT;
2151 if (copy_from_user(&routing, argp, sizeof(routing)))
2152 goto out;
2153 r = -EINVAL;
2154 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2155 goto out;
2156 if (routing.flags)
2157 goto out;
2158 r = -ENOMEM;
2159 entries = vmalloc(routing.nr * sizeof(*entries));
2160 if (!entries)
2161 goto out;
2162 r = -EFAULT;
2163 urouting = argp;
2164 if (copy_from_user(entries, urouting->entries,
2165 routing.nr * sizeof(*entries)))
2166 goto out_free_irq_routing;
2167 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2168 routing.flags);
2169 out_free_irq_routing:
2170 vfree(entries);
2171 break;
2172 }
Sheng Yangc1e01512009-02-25 17:22:26 +08002173#ifdef __KVM_HAVE_MSIX
2174 case KVM_ASSIGN_SET_MSIX_NR: {
2175 struct kvm_assigned_msix_nr entry_nr;
2176 r = -EFAULT;
2177 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2178 goto out;
2179 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2180 if (r)
2181 goto out;
2182 break;
2183 }
2184 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2185 struct kvm_assigned_msix_entry entry;
2186 r = -EFAULT;
2187 if (copy_from_user(&entry, argp, sizeof entry))
2188 goto out;
2189 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2190 if (r)
2191 goto out;
2192 break;
2193 }
Avi Kivity399ec802008-11-19 13:58:46 +02002194#endif
Sheng Yangc1e01512009-02-25 17:22:26 +08002195#endif /* KVM_CAP_IRQ_ROUTING */
Avi Kivityf17abe92007-02-21 19:28:04 +02002196 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002197 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002198 }
2199out:
2200 return r;
2201}
2202
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002203static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002204{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002205 struct page *page[1];
2206 unsigned long addr;
2207 int npages;
2208 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002209 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002210
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002211 addr = gfn_to_hva(kvm, gfn);
2212 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002213 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002214
2215 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2216 NULL);
2217 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002218 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002219
2220 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002221 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002222}
2223
2224static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002225 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002226};
2227
2228static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2229{
2230 vma->vm_ops = &kvm_vm_vm_ops;
2231 return 0;
2232}
2233
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002234static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002235 .release = kvm_vm_release,
2236 .unlocked_ioctl = kvm_vm_ioctl,
2237 .compat_ioctl = kvm_vm_ioctl,
2238 .mmap = kvm_vm_mmap,
2239};
2240
2241static int kvm_dev_ioctl_create_vm(void)
2242{
Al Viro2030a422008-02-23 06:46:49 -05002243 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002244 struct kvm *kvm;
2245
Avi Kivityf17abe92007-02-21 19:28:04 +02002246 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002247 if (IS_ERR(kvm))
2248 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002249 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002250 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002251 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002252
Avi Kivityf17abe92007-02-21 19:28:04 +02002253 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002254}
2255
Avi Kivity1a811b62008-12-08 18:25:27 +02002256static long kvm_dev_ioctl_check_extension_generic(long arg)
2257{
2258 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002259 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002260 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002261 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Avi Kivity1a811b62008-12-08 18:25:27 +02002262 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002263#ifdef CONFIG_HAVE_KVM_IRQCHIP
2264 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002265 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002266#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002267 default:
2268 break;
2269 }
2270 return kvm_dev_ioctl_check_extension(arg);
2271}
2272
Avi Kivityf17abe92007-02-21 19:28:04 +02002273static long kvm_dev_ioctl(struct file *filp,
2274 unsigned int ioctl, unsigned long arg)
2275{
Avi Kivity07c45a32007-03-07 13:05:38 +02002276 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002277
2278 switch (ioctl) {
2279 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002280 r = -EINVAL;
2281 if (arg)
2282 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002283 r = KVM_API_VERSION;
2284 break;
2285 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002286 r = -EINVAL;
2287 if (arg)
2288 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002289 r = kvm_dev_ioctl_create_vm();
2290 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002291 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002292 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002293 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002294 case KVM_GET_VCPU_MMAP_SIZE:
2295 r = -EINVAL;
2296 if (arg)
2297 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002298 r = PAGE_SIZE; /* struct kvm_run */
2299#ifdef CONFIG_X86
2300 r += PAGE_SIZE; /* pio data page */
2301#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002302#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2303 r += PAGE_SIZE; /* coalesced mmio ring page */
2304#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002305 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002306 case KVM_TRACE_ENABLE:
2307 case KVM_TRACE_PAUSE:
2308 case KVM_TRACE_DISABLE:
2309 r = kvm_trace_ioctl(ioctl, arg);
2310 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002312 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 }
2314out:
2315 return r;
2316}
2317
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319 .unlocked_ioctl = kvm_dev_ioctl,
2320 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321};
2322
2323static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002324 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 "kvm",
2326 &kvm_chardev_ops,
2327};
2328
Avi Kivity1b6c0162007-05-24 13:03:52 +03002329static void hardware_enable(void *junk)
2330{
2331 int cpu = raw_smp_processor_id();
2332
Rusty Russell7f59f492008-12-07 21:25:45 +10302333 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002334 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302335 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002336 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002337}
2338
2339static void hardware_disable(void *junk)
2340{
2341 int cpu = raw_smp_processor_id();
2342
Rusty Russell7f59f492008-12-07 21:25:45 +10302343 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002344 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302345 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002346 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002347}
2348
Avi Kivity774c47f2007-02-12 00:54:47 -08002349static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2350 void *v)
2351{
2352 int cpu = (long)v;
2353
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002354 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002355 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002356 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002357 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2358 cpu);
2359 hardware_disable(NULL);
2360 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002361 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002362 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2363 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002364 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002365 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002366 case CPU_ONLINE:
2367 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2368 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002369 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002370 break;
2371 }
2372 return NOTIFY_OK;
2373}
2374
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002375
2376asmlinkage void kvm_handle_fault_on_reboot(void)
2377{
2378 if (kvm_rebooting)
2379 /* spin while reset goes on */
2380 while (true)
2381 ;
2382 /* Fault while not rebooting. We want the trace. */
2383 BUG();
2384}
2385EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2386
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002387static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002388 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002389{
2390 if (val == SYS_RESTART) {
2391 /*
2392 * Some (well, at least mine) BIOSes hang on reboot if
2393 * in vmx root mode.
2394 */
2395 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002396 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002397 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002398 }
2399 return NOTIFY_OK;
2400}
2401
2402static struct notifier_block kvm_reboot_notifier = {
2403 .notifier_call = kvm_reboot,
2404 .priority = 0,
2405};
2406
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002407void kvm_io_bus_init(struct kvm_io_bus *bus)
2408{
2409 memset(bus, 0, sizeof(*bus));
2410}
2411
2412void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2413{
2414 int i;
2415
2416 for (i = 0; i < bus->dev_count; i++) {
2417 struct kvm_io_device *pos = bus->devs[i];
2418
2419 kvm_iodevice_destructor(pos);
2420 }
2421}
2422
Laurent Vivier92760492008-05-30 16:05:53 +02002423struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2424 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002425{
2426 int i;
2427
2428 for (i = 0; i < bus->dev_count; i++) {
2429 struct kvm_io_device *pos = bus->devs[i];
2430
Laurent Vivier92760492008-05-30 16:05:53 +02002431 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002432 return pos;
2433 }
2434
2435 return NULL;
2436}
2437
2438void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2439{
2440 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2441
2442 bus->devs[bus->dev_count++] = dev;
2443}
2444
Avi Kivity774c47f2007-02-12 00:54:47 -08002445static struct notifier_block kvm_cpu_notifier = {
2446 .notifier_call = kvm_cpu_hotplug,
2447 .priority = 20, /* must be > scheduler priority */
2448};
2449
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002450static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002451{
2452 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002453 struct kvm *kvm;
2454
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002455 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002456 spin_lock(&kvm_lock);
2457 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002458 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002459 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002460 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002461}
2462
2463DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2464
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002465static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002466{
2467 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002468 struct kvm *kvm;
2469 struct kvm_vcpu *vcpu;
2470 int i;
2471
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002472 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002473 spin_lock(&kvm_lock);
2474 list_for_each_entry(kvm, &vm_list, vm_list)
2475 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002476 vcpu = kvm->vcpus[i];
2477 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002478 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002479 }
2480 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002481 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002482}
2483
Avi Kivityba1389b2007-11-18 16:24:12 +02002484DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2485
2486static struct file_operations *stat_fops[] = {
2487 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2488 [KVM_STAT_VM] = &vm_stat_fops,
2489};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002490
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002491static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492{
2493 struct kvm_stats_debugfs_item *p;
2494
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002495 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002497 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002498 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002499 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500}
2501
2502static void kvm_exit_debug(void)
2503{
2504 struct kvm_stats_debugfs_item *p;
2505
2506 for (p = debugfs_entries; p->name; ++p)
2507 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002508 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509}
2510
Avi Kivity59ae6c62007-02-12 00:54:48 -08002511static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2512{
Avi Kivity4267c412007-05-24 13:09:41 +03002513 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002514 return 0;
2515}
2516
2517static int kvm_resume(struct sys_device *dev)
2518{
Avi Kivity4267c412007-05-24 13:09:41 +03002519 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002520 return 0;
2521}
2522
2523static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002524 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002525 .suspend = kvm_suspend,
2526 .resume = kvm_resume,
2527};
2528
2529static struct sys_device kvm_sysdev = {
2530 .id = 0,
2531 .cls = &kvm_sysdev_class,
2532};
2533
Izik Eiduscea7bb22007-10-17 19:17:48 +02002534struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002535pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536
Avi Kivity15ad7142007-07-11 18:17:21 +03002537static inline
2538struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2539{
2540 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2541}
2542
2543static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2544{
2545 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2546
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002547 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002548}
2549
2550static void kvm_sched_out(struct preempt_notifier *pn,
2551 struct task_struct *next)
2552{
2553 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2554
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002555 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002556}
2557
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002558int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002559 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560{
2561 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002562 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002564 kvm_init_debug();
2565
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002566 r = kvm_arch_init(opaque);
2567 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002568 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002569
2570 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2571
2572 if (bad_page == NULL) {
2573 r = -ENOMEM;
2574 goto out;
2575 }
2576
Anthony Liguori35149e22008-04-02 14:46:56 -05002577 bad_pfn = page_to_pfn(bad_page);
2578
Avi Kivity8437a6172009-06-06 14:52:35 -07002579 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302580 r = -ENOMEM;
2581 goto out_free_0;
2582 }
Avi Kivitya4c03642009-06-06 12:34:39 +03002583 cpumask_clear(cpus_hardware_enabled);
Rusty Russell7f59f492008-12-07 21:25:45 +10302584
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002585 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302587 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588
Yang, Sheng002c7f72007-07-31 14:23:01 +03002589 for_each_online_cpu(cpu) {
2590 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002591 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002592 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002593 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002594 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002595 }
2596
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002597 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002598 r = register_cpu_notifier(&kvm_cpu_notifier);
2599 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002600 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601 register_reboot_notifier(&kvm_reboot_notifier);
2602
Avi Kivity59ae6c62007-02-12 00:54:48 -08002603 r = sysdev_class_register(&kvm_sysdev_class);
2604 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002605 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002606
2607 r = sysdev_register(&kvm_sysdev);
2608 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002609 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002610
Rusty Russellc16f8622007-07-30 21:12:19 +10002611 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2612 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002613 __alignof__(struct kvm_vcpu),
2614 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002615 if (!kvm_vcpu_cache) {
2616 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002617 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002618 }
2619
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002621 kvm_vm_fops.owner = module;
2622 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623
2624 r = misc_register(&kvm_dev);
2625 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002626 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627 goto out_free;
2628 }
2629
Avi Kivity15ad7142007-07-11 18:17:21 +03002630 kvm_preempt_ops.sched_in = kvm_sched_in;
2631 kvm_preempt_ops.sched_out = kvm_sched_out;
2632
Avi Kivityc7addb92007-09-16 18:58:32 +02002633 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634
2635out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002636 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002637out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002638 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002639out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002640 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002641out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002643 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002644out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002645 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002646out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002647 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302648out_free_0a:
2649 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002650out_free_0:
2651 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002652out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002653 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002654 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002655out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656 return r;
2657}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002658EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002660void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002662 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002664 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002665 sysdev_unregister(&kvm_sysdev);
2666 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002668 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002669 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002670 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002671 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302673 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002674 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002676EXPORT_SYMBOL_GPL(kvm_exit);