blob: 3bed82754a5dec7941365ecf158efed2d3f7a95f [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/io.h>
47#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020048#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Laurent Vivier5f94c172008-05-30 16:05:54 +020050#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51#include "coalesced_mmio.h"
52#endif
53
Xiantao Zhang8a98f662008-10-06 13:47:38 +080054#ifdef KVM_CAP_DEVICE_ASSIGNMENT
55#include <linux/pci.h>
56#include <linux/interrupt.h>
57#include "irq.h"
58#endif
59
Avi Kivity6aa8b732006-12-10 02:21:36 -080060MODULE_AUTHOR("Qumranet");
61MODULE_LICENSE("GPL");
62
Sheng Yang5319c662008-11-24 14:32:57 +080063static int msi2intx = 1;
64module_param(msi2intx, bool, 0);
65
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080066DEFINE_SPINLOCK(kvm_lock);
67LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080068
Rusty Russell7f59f492008-12-07 21:25:45 +103069static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030070
Rusty Russellc16f8622007-07-30 21:12:19 +100071struct kmem_cache *kvm_vcpu_cache;
72EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030073
Avi Kivity15ad7142007-07-11 18:17:21 +030074static __read_mostly struct preempt_ops kvm_preempt_ops;
75
Hollis Blanchard76f7c872008-04-15 16:05:42 -050076struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080077
Avi Kivitybccf2152007-02-21 18:04:26 +020078static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
79 unsigned long arg);
80
Hannes Edere8ba5d32008-11-28 17:02:42 +010081static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030082
Xiantao Zhang8a98f662008-10-06 13:47:38 +080083#ifdef KVM_CAP_DEVICE_ASSIGNMENT
84static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
85 int assigned_dev_id)
86{
87 struct list_head *ptr;
88 struct kvm_assigned_dev_kernel *match;
89
90 list_for_each(ptr, head) {
91 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
92 if (match->assigned_dev_id == assigned_dev_id)
93 return match;
94 }
95 return NULL;
96}
97
Sheng Yang2350bd12009-02-25 17:22:27 +080098static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
99 *assigned_dev, int irq)
100{
101 int i, index;
102 struct msix_entry *host_msix_entries;
103
104 host_msix_entries = assigned_dev->host_msix_entries;
105
106 index = -1;
107 for (i = 0; i < assigned_dev->entries_nr; i++)
108 if (irq == host_msix_entries[i].vector) {
109 index = i;
110 break;
111 }
112 if (index < 0) {
113 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
114 return 0;
115 }
116
117 return index;
118}
119
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800120static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
121{
122 struct kvm_assigned_dev_kernel *assigned_dev;
Sheng Yang2350bd12009-02-25 17:22:27 +0800123 struct kvm *kvm;
124 int irq, i;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800125
126 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
127 interrupt_work);
Sheng Yang2350bd12009-02-25 17:22:27 +0800128 kvm = assigned_dev->kvm;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800129
130 /* This is taken to safely inject irq inside the guest. When
131 * the interrupt injection (or the ioapic code) uses a
132 * finer-grained lock, update this
133 */
Sheng Yang2350bd12009-02-25 17:22:27 +0800134 mutex_lock(&kvm->lock);
135 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_MSIX) {
136 struct kvm_guest_msix_entry *guest_entries =
137 assigned_dev->guest_msix_entries;
138 for (i = 0; i < assigned_dev->entries_nr; i++) {
139 if (!(guest_entries[i].flags &
140 KVM_ASSIGNED_MSIX_PENDING))
141 continue;
142 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
143 kvm_set_irq(assigned_dev->kvm,
144 assigned_dev->irq_source_id,
145 guest_entries[i].vector, 1);
146 irq = assigned_dev->host_msix_entries[i].vector;
147 if (irq != 0)
148 enable_irq(irq);
149 assigned_dev->host_irq_disabled = false;
150 }
151 } else {
152 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
153 assigned_dev->guest_irq, 1);
154 if (assigned_dev->irq_requested_type &
155 KVM_ASSIGNED_DEV_GUEST_MSI) {
156 enable_irq(assigned_dev->host_irq);
157 assigned_dev->host_irq_disabled = false;
158 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800159 }
Sheng Yang2350bd12009-02-25 17:22:27 +0800160
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800161 mutex_unlock(&assigned_dev->kvm->lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800162}
163
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800164static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
165{
166 struct kvm_assigned_dev_kernel *assigned_dev =
167 (struct kvm_assigned_dev_kernel *) dev_id;
168
Sheng Yang2350bd12009-02-25 17:22:27 +0800169 if (assigned_dev->irq_requested_type == KVM_ASSIGNED_DEV_MSIX) {
170 int index = find_index_from_host_irq(assigned_dev, irq);
171 if (index < 0)
172 return IRQ_HANDLED;
173 assigned_dev->guest_msix_entries[index].flags |=
174 KVM_ASSIGNED_MSIX_PENDING;
175 }
176
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800177 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000178
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800179 disable_irq_nosync(irq);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000180 assigned_dev->host_irq_disabled = true;
181
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800182 return IRQ_HANDLED;
183}
184
185/* Ack the irq line for an assigned device */
186static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
187{
188 struct kvm_assigned_dev_kernel *dev;
189
190 if (kian->gsi == -1)
191 return;
192
193 dev = container_of(kian, struct kvm_assigned_dev_kernel,
194 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000195
Sheng Yang5550af42008-10-15 20:15:06 +0800196 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000197
198 /* The guest irq may be shared so this ack may be
199 * from another device.
200 */
201 if (dev->host_irq_disabled) {
202 enable_irq(dev->host_irq);
203 dev->host_irq_disabled = false;
204 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800205}
206
Sheng Yangba4cef32009-01-06 10:03:03 +0800207/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000208static void kvm_free_assigned_irq(struct kvm *kvm,
209 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800210{
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000211 if (!irqchip_in_kernel(kvm))
212 return;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800213
Sheng Yange19e30e2008-10-20 16:07:10 +0800214 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000215
216 if (assigned_dev->irq_source_id != -1)
217 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
218 assigned_dev->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800219
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000220 if (!assigned_dev->irq_requested_type)
221 return;
222
Sheng Yangba4cef32009-01-06 10:03:03 +0800223 /*
224 * In kvm_free_device_irq, cancel_work_sync return true if:
225 * 1. work is scheduled, and then cancelled.
226 * 2. work callback is executed.
227 *
228 * The first one ensured that the irq is disabled and no more events
229 * would happen. But for the second one, the irq may be enabled (e.g.
230 * for MSI). So we disable irq here to prevent further events.
231 *
232 * Notice this maybe result in nested disable if the interrupt type is
233 * INTx, but it's OK for we are going to free it.
234 *
235 * If this function is a part of VM destroy, please ensure that till
236 * now, the kvm state is still legal for probably we also have to wait
237 * interrupt_work done.
238 */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800239 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_MSIX) {
240 int i;
241 for (i = 0; i < assigned_dev->entries_nr; i++)
242 disable_irq_nosync(assigned_dev->
243 host_msix_entries[i].vector);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800244
Sheng Yangd510d6c2009-02-25 17:22:28 +0800245 cancel_work_sync(&assigned_dev->interrupt_work);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000246
Sheng Yangd510d6c2009-02-25 17:22:28 +0800247 for (i = 0; i < assigned_dev->entries_nr; i++)
248 free_irq(assigned_dev->host_msix_entries[i].vector,
249 (void *)assigned_dev);
250
251 assigned_dev->entries_nr = 0;
252 kfree(assigned_dev->host_msix_entries);
253 kfree(assigned_dev->guest_msix_entries);
254 pci_disable_msix(assigned_dev->dev);
255 } else {
256 /* Deal with MSI and INTx */
257 disable_irq_nosync(assigned_dev->host_irq);
258 cancel_work_sync(&assigned_dev->interrupt_work);
259
260 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
261
262 if (assigned_dev->irq_requested_type &
263 KVM_ASSIGNED_DEV_HOST_MSI)
264 pci_disable_msi(assigned_dev->dev);
265 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000266
267 assigned_dev->irq_requested_type = 0;
268}
269
270
271static void kvm_free_assigned_device(struct kvm *kvm,
272 struct kvm_assigned_dev_kernel
273 *assigned_dev)
274{
275 kvm_free_assigned_irq(kvm, assigned_dev);
276
Sheng Yang6eb55812008-10-31 12:37:41 +0800277 pci_reset_function(assigned_dev->dev);
278
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800279 pci_release_regions(assigned_dev->dev);
280 pci_disable_device(assigned_dev->dev);
281 pci_dev_put(assigned_dev->dev);
282
283 list_del(&assigned_dev->list);
284 kfree(assigned_dev);
285}
286
287void kvm_free_all_assigned_devices(struct kvm *kvm)
288{
289 struct list_head *ptr, *ptr2;
290 struct kvm_assigned_dev_kernel *assigned_dev;
291
292 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
293 assigned_dev = list_entry(ptr,
294 struct kvm_assigned_dev_kernel,
295 list);
296
297 kvm_free_assigned_device(kvm, assigned_dev);
298 }
299}
300
Sheng Yang00e3ed32008-11-24 14:32:50 +0800301static int assigned_device_update_intx(struct kvm *kvm,
302 struct kvm_assigned_dev_kernel *adev,
303 struct kvm_assigned_irq *airq)
304{
Sheng Yangfbac7812008-11-24 14:32:52 +0800305 adev->guest_irq = airq->guest_irq;
306 adev->ack_notifier.gsi = airq->guest_irq;
307
308 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800309 return 0;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800310
311 if (irqchip_in_kernel(kvm)) {
Sheng Yang5319c662008-11-24 14:32:57 +0800312 if (!msi2intx &&
Sheng Yangd7cff1c2009-01-06 16:25:10 +0800313 (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
314 free_irq(adev->host_irq, (void *)adev);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800315 pci_disable_msi(adev->dev);
316 }
317
Sheng Yang00e3ed32008-11-24 14:32:50 +0800318 if (!capable(CAP_SYS_RAWIO))
319 return -EPERM;
320
321 if (airq->host_irq)
322 adev->host_irq = airq->host_irq;
323 else
324 adev->host_irq = adev->dev->irq;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800325
326 /* Even though this is PCI, we don't want to use shared
327 * interrupts. Sharing host devices with guest-assigned devices
328 * on the same interrupt line is not a happy situation: there
329 * are going to be long delays in accepting, acking, etc.
330 */
331 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
332 0, "kvm_assigned_intx_device", (void *)adev))
333 return -EIO;
334 }
335
Sheng Yang4f906c12008-11-24 14:32:51 +0800336 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
337 KVM_ASSIGNED_DEV_HOST_INTX;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800338 return 0;
339}
340
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800341#ifdef CONFIG_X86
342static int assigned_device_update_msi(struct kvm *kvm,
343 struct kvm_assigned_dev_kernel *adev,
344 struct kvm_assigned_irq *airq)
345{
346 int r;
347
Sheng Yang79950e12009-02-10 13:57:06 +0800348 adev->guest_irq = airq->guest_irq;
Sheng Yang5319c662008-11-24 14:32:57 +0800349 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
350 /* x86 don't care upper address of guest msi message addr */
351 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
352 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
Sheng Yang5319c662008-11-24 14:32:57 +0800353 adev->ack_notifier.gsi = -1;
354 } else if (msi2intx) {
355 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
356 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
Sheng Yang5319c662008-11-24 14:32:57 +0800357 adev->ack_notifier.gsi = airq->guest_irq;
Sheng Yang17071fe2009-01-06 16:25:11 +0800358 } else {
359 /*
360 * Guest require to disable device MSI, we disable MSI and
361 * re-enable INTx by default again. Notice it's only for
362 * non-msi2intx.
363 */
364 assigned_device_update_intx(kvm, adev, airq);
365 return 0;
Sheng Yang5319c662008-11-24 14:32:57 +0800366 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800367
368 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
369 return 0;
370
371 if (irqchip_in_kernel(kvm)) {
Sheng Yang5319c662008-11-24 14:32:57 +0800372 if (!msi2intx) {
373 if (adev->irq_requested_type &
374 KVM_ASSIGNED_DEV_HOST_INTX)
375 free_irq(adev->host_irq, (void *)adev);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800376
Sheng Yang5319c662008-11-24 14:32:57 +0800377 r = pci_enable_msi(adev->dev);
378 if (r)
379 return r;
380 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800381
382 adev->host_irq = adev->dev->irq;
383 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
384 "kvm_assigned_msi_device", (void *)adev))
385 return -EIO;
386 }
387
Sheng Yang5319c662008-11-24 14:32:57 +0800388 if (!msi2intx)
389 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
390
391 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800392 return 0;
393}
394#endif
395
Sheng Yangd510d6c2009-02-25 17:22:28 +0800396#ifdef __KVM_HAVE_MSIX
397static int assigned_device_update_msix(struct kvm *kvm,
398 struct kvm_assigned_dev_kernel *adev,
399 struct kvm_assigned_irq *airq)
400{
401 /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
402 int i, r;
403
404 adev->ack_notifier.gsi = -1;
405
406 if (irqchip_in_kernel(kvm)) {
407 if (airq->flags & KVM_DEV_IRQ_ASSIGN_MASK_MSIX)
408 return -ENOTTY;
409
410 if (!(airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSIX)) {
411 /* Guest disable MSI-X */
412 kvm_free_assigned_irq(kvm, adev);
413 if (msi2intx) {
414 pci_enable_msi(adev->dev);
415 if (adev->dev->msi_enabled)
416 return assigned_device_update_msi(kvm,
417 adev, airq);
418 }
419 return assigned_device_update_intx(kvm, adev, airq);
420 }
421
422 /* host_msix_entries and guest_msix_entries should have been
423 * initialized */
424 if (adev->entries_nr == 0)
425 return -EINVAL;
426
427 kvm_free_assigned_irq(kvm, adev);
428
429 r = pci_enable_msix(adev->dev, adev->host_msix_entries,
430 adev->entries_nr);
431 if (r)
432 return r;
433
434 for (i = 0; i < adev->entries_nr; i++) {
435 r = request_irq((adev->host_msix_entries + i)->vector,
436 kvm_assigned_dev_intr, 0,
437 "kvm_assigned_msix_device",
438 (void *)adev);
439 if (r)
440 return r;
441 }
442 }
443
444 adev->irq_requested_type |= KVM_ASSIGNED_DEV_MSIX;
445
446 return 0;
447}
448#endif
449
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800450static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
451 struct kvm_assigned_irq
452 *assigned_irq)
453{
454 int r = 0;
455 struct kvm_assigned_dev_kernel *match;
Sheng Yang17071fe2009-01-06 16:25:11 +0800456 u32 current_flags = 0, changed_flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800457
458 mutex_lock(&kvm->lock);
459
460 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
461 assigned_irq->assigned_dev_id);
462 if (!match) {
463 mutex_unlock(&kvm->lock);
464 return -EINVAL;
465 }
466
Sheng Yang4f906c12008-11-24 14:32:51 +0800467 if (!match->irq_requested_type) {
Sheng Yang342ffb92008-11-24 14:32:49 +0800468 INIT_WORK(&match->interrupt_work,
469 kvm_assigned_dev_interrupt_work_handler);
470 if (irqchip_in_kernel(kvm)) {
471 /* Register ack nofitier */
472 match->ack_notifier.gsi = -1;
473 match->ack_notifier.irq_acked =
474 kvm_assigned_dev_ack_irq;
475 kvm_register_irq_ack_notifier(kvm,
476 &match->ack_notifier);
477
478 /* Request IRQ source ID */
479 r = kvm_request_irq_source_id(kvm);
480 if (r < 0)
481 goto out_release;
482 else
483 match->irq_source_id = r;
Sheng Yang5319c662008-11-24 14:32:57 +0800484
485#ifdef CONFIG_X86
486 /* Determine host device irq type, we can know the
487 * result from dev->msi_enabled */
488 if (msi2intx)
489 pci_enable_msi(match->dev);
490#endif
Sheng Yang342ffb92008-11-24 14:32:49 +0800491 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800492 }
493
Sheng Yangd510d6c2009-02-25 17:22:28 +0800494 if (match->irq_requested_type & KVM_ASSIGNED_DEV_MSIX)
495 current_flags |= KVM_DEV_IRQ_ASSIGN_ENABLE_MSIX;
496 else if ((match->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) &&
Sheng Yang17071fe2009-01-06 16:25:11 +0800497 (match->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_MSI))
498 current_flags |= KVM_DEV_IRQ_ASSIGN_ENABLE_MSI;
499
500 changed_flags = assigned_irq->flags ^ current_flags;
501
Sheng Yangd510d6c2009-02-25 17:22:28 +0800502#ifdef __KVM_HAVE_MSIX
503 if (changed_flags & KVM_DEV_IRQ_ASSIGN_MSIX_ACTION) {
504 r = assigned_device_update_msix(kvm, match, assigned_irq);
505 if (r) {
506 printk(KERN_WARNING "kvm: failed to execute "
507 "MSI-X action!\n");
508 goto out_release;
509 }
510 } else
511#endif
Sheng Yang17071fe2009-01-06 16:25:11 +0800512 if ((changed_flags & KVM_DEV_IRQ_ASSIGN_MSI_ACTION) ||
Sheng Yang5319c662008-11-24 14:32:57 +0800513 (msi2intx && match->dev->msi_enabled)) {
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800514#ifdef CONFIG_X86
515 r = assigned_device_update_msi(kvm, match, assigned_irq);
516 if (r) {
517 printk(KERN_WARNING "kvm: failed to enable "
518 "MSI device!\n");
519 goto out_release;
520 }
521#else
522 r = -ENOTTY;
523#endif
524 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
525 /* Host device IRQ 0 means don't support INTx */
Sheng Yang5319c662008-11-24 14:32:57 +0800526 if (!msi2intx) {
527 printk(KERN_WARNING
528 "kvm: wait device to enable MSI!\n");
529 r = 0;
530 } else {
531 printk(KERN_WARNING
532 "kvm: failed to enable MSI device!\n");
533 r = -ENOTTY;
534 goto out_release;
535 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800536 } else {
537 /* Non-sharing INTx mode */
538 r = assigned_device_update_intx(kvm, match, assigned_irq);
539 if (r) {
540 printk(KERN_WARNING "kvm: failed to enable "
541 "INTx device!\n");
542 goto out_release;
543 }
544 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800545
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800546 mutex_unlock(&kvm->lock);
547 return r;
548out_release:
549 mutex_unlock(&kvm->lock);
550 kvm_free_assigned_device(kvm, match);
551 return r;
552}
553
554static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
555 struct kvm_assigned_pci_dev *assigned_dev)
556{
557 int r = 0;
558 struct kvm_assigned_dev_kernel *match;
559 struct pci_dev *dev;
560
Mark McLoughlin682edb42009-02-05 18:23:46 +0000561 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800562 mutex_lock(&kvm->lock);
563
564 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
565 assigned_dev->assigned_dev_id);
566 if (match) {
567 /* device already assigned */
568 r = -EINVAL;
569 goto out;
570 }
571
572 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
573 if (match == NULL) {
574 printk(KERN_INFO "%s: Couldn't allocate memory\n",
575 __func__);
576 r = -ENOMEM;
577 goto out;
578 }
579 dev = pci_get_bus_and_slot(assigned_dev->busnr,
580 assigned_dev->devfn);
581 if (!dev) {
582 printk(KERN_INFO "%s: host device not found\n", __func__);
583 r = -EINVAL;
584 goto out_free;
585 }
586 if (pci_enable_device(dev)) {
587 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
588 r = -EBUSY;
589 goto out_put;
590 }
591 r = pci_request_regions(dev, "kvm_assigned_device");
592 if (r) {
593 printk(KERN_INFO "%s: Could not get access to device regions\n",
594 __func__);
595 goto out_disable;
596 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800597
598 pci_reset_function(dev);
599
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800600 match->assigned_dev_id = assigned_dev->assigned_dev_id;
601 match->host_busnr = assigned_dev->busnr;
602 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800603 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800604 match->dev = dev;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000605 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800606 match->kvm = kvm;
607
608 list_add(&match->list, &kvm->arch.assigned_dev_head);
609
610 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100611 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800612 r = kvm_iommu_map_guest(kvm);
613 if (r)
614 goto out_list_del;
615 }
616 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800617 if (r)
618 goto out_list_del;
619 }
620
621out:
622 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000623 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800624 return r;
625out_list_del:
626 list_del(&match->list);
627 pci_release_regions(dev);
628out_disable:
629 pci_disable_device(dev);
630out_put:
631 pci_dev_put(dev);
632out_free:
633 kfree(match);
634 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000635 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800636 return r;
637}
638#endif
639
Weidong Han0a920352008-12-02 21:24:23 +0800640#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
641static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
642 struct kvm_assigned_pci_dev *assigned_dev)
643{
644 int r = 0;
645 struct kvm_assigned_dev_kernel *match;
646
647 mutex_lock(&kvm->lock);
648
649 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
650 assigned_dev->assigned_dev_id);
651 if (!match) {
652 printk(KERN_INFO "%s: device hasn't been assigned before, "
653 "so cannot be deassigned\n", __func__);
654 r = -EINVAL;
655 goto out;
656 }
657
Weidong Han4a906e42009-02-13 17:27:51 +0800658 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800659 kvm_deassign_device(kvm, match);
660
661 kvm_free_assigned_device(kvm, match);
662
663out:
664 mutex_unlock(&kvm->lock);
665 return r;
666}
667#endif
668
James Morris5aacf0c2006-12-22 01:04:55 -0800669static inline int valid_vcpu(int n)
670{
671 return likely(n >= 0 && n < KVM_MAX_VCPUS);
672}
673
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800674inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300675{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100676 if (pfn_valid(pfn)) {
677 struct page *page = compound_head(pfn_to_page(pfn));
678 return PageReserved(page);
679 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300680
681 return true;
682}
683
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684/*
685 * Switches to specified vcpu, until a matching vcpu_put()
686 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200687void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688{
Avi Kivity15ad7142007-07-11 18:17:21 +0300689 int cpu;
690
Avi Kivitybccf2152007-02-21 18:04:26 +0200691 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300692 cpu = get_cpu();
693 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200694 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300695 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200696}
697
Carsten Otte313a3dc2007-10-11 19:16:52 +0200698void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699{
Avi Kivity15ad7142007-07-11 18:17:21 +0300700 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200701 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300702 preempt_notifier_unregister(&vcpu->preempt_notifier);
703 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704 mutex_unlock(&vcpu->mutex);
705}
706
Avi Kivityd9e368d2007-06-07 19:18:30 +0300707static void ack_flush(void *_completed)
708{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300709}
710
Rusty Russell49846892008-12-08 20:26:24 +1030711static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300712{
Avi Kivity597a5f52008-07-20 14:24:22 +0300713 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030714 cpumask_var_t cpus;
715 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300716 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300717
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030718 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
719 cpumask_clear(cpus);
720
Avi Kivity597a5f52008-07-20 14:24:22 +0300721 me = get_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000722 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
723 vcpu = kvm->vcpus[i];
724 if (!vcpu)
725 continue;
Rusty Russell49846892008-12-08 20:26:24 +1030726 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300727 continue;
728 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030729 if (cpus != NULL && cpu != -1 && cpu != me)
730 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300731 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030732 if (unlikely(cpus == NULL))
733 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
734 else if (!cpumask_empty(cpus))
735 smp_call_function_many(cpus, ack_flush, NULL, 1);
736 else
737 called = false;
Avi Kivity597a5f52008-07-20 14:24:22 +0300738 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030739 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030740 return called;
741}
742
743void kvm_flush_remote_tlbs(struct kvm *kvm)
744{
745 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
746 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300747}
748
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500749void kvm_reload_remote_mmus(struct kvm *kvm)
750{
Rusty Russell49846892008-12-08 20:26:24 +1030751 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500752}
753
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000754int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
755{
756 struct page *page;
757 int r;
758
759 mutex_init(&vcpu->mutex);
760 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000761 vcpu->kvm = kvm;
762 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300763 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000764
765 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
766 if (!page) {
767 r = -ENOMEM;
768 goto fail;
769 }
770 vcpu->run = page_address(page);
771
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800772 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000773 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800774 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000775 return 0;
776
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000777fail_free_run:
778 free_page((unsigned long)vcpu->run);
779fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000780 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000781}
782EXPORT_SYMBOL_GPL(kvm_vcpu_init);
783
784void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
785{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800786 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000787 free_page((unsigned long)vcpu->run);
788}
789EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
790
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200791#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
792static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
793{
794 return container_of(mn, struct kvm, mmu_notifier);
795}
796
797static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
798 struct mm_struct *mm,
799 unsigned long address)
800{
801 struct kvm *kvm = mmu_notifier_to_kvm(mn);
802 int need_tlb_flush;
803
804 /*
805 * When ->invalidate_page runs, the linux pte has been zapped
806 * already but the page is still allocated until
807 * ->invalidate_page returns. So if we increase the sequence
808 * here the kvm page fault will notice if the spte can't be
809 * established because the page is going to be freed. If
810 * instead the kvm page fault establishes the spte before
811 * ->invalidate_page runs, kvm_unmap_hva will release it
812 * before returning.
813 *
814 * The sequence increase only need to be seen at spin_unlock
815 * time, and not at spin_lock time.
816 *
817 * Increasing the sequence after the spin_unlock would be
818 * unsafe because the kvm page fault could then establish the
819 * pte after kvm_unmap_hva returned, without noticing the page
820 * is going to be freed.
821 */
822 spin_lock(&kvm->mmu_lock);
823 kvm->mmu_notifier_seq++;
824 need_tlb_flush = kvm_unmap_hva(kvm, address);
825 spin_unlock(&kvm->mmu_lock);
826
827 /* we've to flush the tlb before the pages can be freed */
828 if (need_tlb_flush)
829 kvm_flush_remote_tlbs(kvm);
830
831}
832
833static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
834 struct mm_struct *mm,
835 unsigned long start,
836 unsigned long end)
837{
838 struct kvm *kvm = mmu_notifier_to_kvm(mn);
839 int need_tlb_flush = 0;
840
841 spin_lock(&kvm->mmu_lock);
842 /*
843 * The count increase must become visible at unlock time as no
844 * spte can be established without taking the mmu_lock and
845 * count is also read inside the mmu_lock critical section.
846 */
847 kvm->mmu_notifier_count++;
848 for (; start < end; start += PAGE_SIZE)
849 need_tlb_flush |= kvm_unmap_hva(kvm, start);
850 spin_unlock(&kvm->mmu_lock);
851
852 /* we've to flush the tlb before the pages can be freed */
853 if (need_tlb_flush)
854 kvm_flush_remote_tlbs(kvm);
855}
856
857static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
858 struct mm_struct *mm,
859 unsigned long start,
860 unsigned long end)
861{
862 struct kvm *kvm = mmu_notifier_to_kvm(mn);
863
864 spin_lock(&kvm->mmu_lock);
865 /*
866 * This sequence increase will notify the kvm page fault that
867 * the page that is going to be mapped in the spte could have
868 * been freed.
869 */
870 kvm->mmu_notifier_seq++;
871 /*
872 * The above sequence increase must be visible before the
873 * below count decrease but both values are read by the kvm
874 * page fault under mmu_lock spinlock so we don't need to add
875 * a smb_wmb() here in between the two.
876 */
877 kvm->mmu_notifier_count--;
878 spin_unlock(&kvm->mmu_lock);
879
880 BUG_ON(kvm->mmu_notifier_count < 0);
881}
882
883static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
884 struct mm_struct *mm,
885 unsigned long address)
886{
887 struct kvm *kvm = mmu_notifier_to_kvm(mn);
888 int young;
889
890 spin_lock(&kvm->mmu_lock);
891 young = kvm_age_hva(kvm, address);
892 spin_unlock(&kvm->mmu_lock);
893
894 if (young)
895 kvm_flush_remote_tlbs(kvm);
896
897 return young;
898}
899
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100900static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
901 struct mm_struct *mm)
902{
903 struct kvm *kvm = mmu_notifier_to_kvm(mn);
904 kvm_arch_flush_shadow(kvm);
905}
906
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200907static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
908 .invalidate_page = kvm_mmu_notifier_invalidate_page,
909 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
910 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
911 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100912 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200913};
914#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
915
Avi Kivityf17abe92007-02-21 19:28:04 +0200916static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800918 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200919#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
920 struct page *page;
921#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800923 if (IS_ERR(kvm))
924 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200925#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200926 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200927 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
928#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
Laurent Vivier5f94c172008-05-30 16:05:54 +0200930#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
931 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
932 if (!page) {
933 kfree(kvm);
934 return ERR_PTR(-ENOMEM);
935 }
936 kvm->coalesced_mmio_ring =
937 (struct kvm_coalesced_mmio_ring *)page_address(page);
938#endif
939
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200940#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
941 {
942 int err;
943 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
944 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
945 if (err) {
946#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
947 put_page(page);
948#endif
949 kfree(kvm);
950 return ERR_PTR(err);
951 }
952 }
953#endif
954
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200955 kvm->mm = current->mm;
956 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500957 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300958 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800959 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400960 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200961 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300962 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000963 spin_lock(&kvm_lock);
964 list_add(&kvm->vm_list, &vm_list);
965 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200966#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
967 kvm_coalesced_mmio_init(kvm);
968#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800969out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200970 return kvm;
971}
972
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973/*
974 * Free any memory in @free but not in @dont.
975 */
976static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
977 struct kvm_memory_slot *dont)
978{
Izik Eidus290fc382007-09-27 14:11:22 +0200979 if (!dont || free->rmap != dont->rmap)
980 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981
982 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
983 vfree(free->dirty_bitmap);
984
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300985 if (!dont || free->lpage_info != dont->lpage_info)
986 vfree(free->lpage_info);
987
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000989 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500990 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300991 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992}
993
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800994void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995{
996 int i;
997
998 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000999 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000}
1001
Avi Kivityf17abe92007-02-21 19:28:04 +02001002static void kvm_destroy_vm(struct kvm *kvm)
1003{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001004 struct mm_struct *mm = kvm->mm;
1005
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001006 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001007 spin_lock(&kvm_lock);
1008 list_del(&kvm->vm_list);
1009 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001010 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001011 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001012 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001013#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1014 if (kvm->coalesced_mmio_ring != NULL)
1015 free_page((unsigned long)kvm->coalesced_mmio_ring);
1016#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001017#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1018 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
1019#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001020 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001021 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001022}
1023
Izik Eidusd39f13b2008-03-30 16:01:25 +03001024void kvm_get_kvm(struct kvm *kvm)
1025{
1026 atomic_inc(&kvm->users_count);
1027}
1028EXPORT_SYMBOL_GPL(kvm_get_kvm);
1029
1030void kvm_put_kvm(struct kvm *kvm)
1031{
1032 if (atomic_dec_and_test(&kvm->users_count))
1033 kvm_destroy_vm(kvm);
1034}
1035EXPORT_SYMBOL_GPL(kvm_put_kvm);
1036
1037
Avi Kivityf17abe92007-02-21 19:28:04 +02001038static int kvm_vm_release(struct inode *inode, struct file *filp)
1039{
1040 struct kvm *kvm = filp->private_data;
1041
Izik Eidusd39f13b2008-03-30 16:01:25 +03001042 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 return 0;
1044}
1045
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 * Allocate some memory and give it an address in the guest physical address
1048 * space.
1049 *
1050 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001051 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001052 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001054int __kvm_set_memory_region(struct kvm *kvm,
1055 struct kvm_userspace_memory_region *mem,
1056 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057{
1058 int r;
1059 gfn_t base_gfn;
1060 unsigned long npages;
Avi Kivity99894a72009-03-29 16:31:25 +03001061 int largepages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062 unsigned long i;
1063 struct kvm_memory_slot *memslot;
1064 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065
1066 r = -EINVAL;
1067 /* General sanity checks */
1068 if (mem->memory_size & (PAGE_SIZE - 1))
1069 goto out;
1070 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1071 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001072 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001073 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001074 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 goto out;
1076 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1077 goto out;
1078
1079 memslot = &kvm->memslots[mem->slot];
1080 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1081 npages = mem->memory_size >> PAGE_SHIFT;
1082
1083 if (!npages)
1084 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1085
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 new = old = *memslot;
1087
1088 new.base_gfn = base_gfn;
1089 new.npages = npages;
1090 new.flags = mem->flags;
1091
1092 /* Disallow changing a memory slot's size. */
1093 r = -EINVAL;
1094 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001095 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096
1097 /* Check for overlaps */
1098 r = -EEXIST;
1099 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1100 struct kvm_memory_slot *s = &kvm->memslots[i];
1101
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001102 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103 continue;
1104 if (!((base_gfn + npages <= s->base_gfn) ||
1105 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001106 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 /* Free page dirty bitmap if unneeded */
1110 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001111 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112
1113 r = -ENOMEM;
1114
1115 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001116#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001117 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001118 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001119
1120 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001121 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001122
Izik Eidus290fc382007-09-27 14:11:22 +02001123 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001124
Izik Eidus80b14b52007-10-25 11:54:04 +02001125 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001126 /*
1127 * hva_to_rmmap() serialzies with the mmu_lock and to be
1128 * safe it has to ignore memslots with !user_alloc &&
1129 * !userspace_addr.
1130 */
1131 if (user_alloc)
1132 new.userspace_addr = mem->userspace_addr;
1133 else
1134 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001136 if (npages && !new.lpage_info) {
Avi Kivity99894a72009-03-29 16:31:25 +03001137 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1138 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001139
1140 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1141
1142 if (!new.lpage_info)
1143 goto out_free;
1144
1145 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1146
1147 if (base_gfn % KVM_PAGES_PER_HPAGE)
1148 new.lpage_info[0].write_count = 1;
1149 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1150 new.lpage_info[largepages-1].write_count = 1;
1151 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152
1153 /* Allocate page dirty bitmap if needed */
1154 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1155 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1156
1157 new.dirty_bitmap = vmalloc(dirty_bytes);
1158 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001159 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 memset(new.dirty_bitmap, 0, dirty_bytes);
1161 }
Carsten Otteeff01142008-06-27 15:05:31 +02001162#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001164 if (!npages)
1165 kvm_arch_flush_shadow(kvm);
1166
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001167 spin_lock(&kvm->mmu_lock);
1168 if (mem->slot >= kvm->nmemslots)
1169 kvm->nmemslots = mem->slot + 1;
1170
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001172 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173
Zhang Xiantao0de10342007-11-20 16:25:04 +08001174 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1175 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001176 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001177 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001178 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001179 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001180 }
1181
Glauber Costa6f897242008-12-03 13:40:51 -02001182 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1183 /* Slot deletion case: we have to update the current slot */
1184 if (!npages)
1185 *memslot = old;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001186#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001187 /* map the pages in iommu page table */
1188 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1189 if (r)
1190 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001191#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192 return 0;
1193
Sheng Yangf78e0e22007-10-29 09:40:42 +08001194out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195 kvm_free_physmem_slot(&new, &old);
1196out:
1197 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001198
1199}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001200EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1201
1202int kvm_set_memory_region(struct kvm *kvm,
1203 struct kvm_userspace_memory_region *mem,
1204 int user_alloc)
1205{
1206 int r;
1207
Izik Eidus72dc67a2008-02-10 18:04:15 +02001208 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001209 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001210 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001211 return r;
1212}
Izik Eidus210c7c42007-10-24 23:52:57 +02001213EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1214
Carsten Otte1fe779f2007-10-29 16:08:35 +01001215int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1216 struct
1217 kvm_userspace_memory_region *mem,
1218 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001219{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001220 if (mem->slot >= KVM_MEMORY_SLOTS)
1221 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001222 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223}
1224
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001225int kvm_get_dirty_log(struct kvm *kvm,
1226 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227{
1228 struct kvm_memory_slot *memslot;
1229 int r, i;
1230 int n;
1231 unsigned long any = 0;
1232
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233 r = -EINVAL;
1234 if (log->slot >= KVM_MEMORY_SLOTS)
1235 goto out;
1236
1237 memslot = &kvm->memslots[log->slot];
1238 r = -ENOENT;
1239 if (!memslot->dirty_bitmap)
1240 goto out;
1241
Uri Lublincd1a4a92007-02-22 16:43:09 +02001242 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243
Uri Lublincd1a4a92007-02-22 16:43:09 +02001244 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 any = memslot->dirty_bitmap[i];
1246
1247 r = -EFAULT;
1248 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1249 goto out;
1250
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001251 if (any)
1252 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253
1254 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256 return r;
1257}
1258
Izik Eiduscea7bb22007-10-17 19:17:48 +02001259int is_error_page(struct page *page)
1260{
1261 return page == bad_page;
1262}
1263EXPORT_SYMBOL_GPL(is_error_page);
1264
Anthony Liguori35149e22008-04-02 14:46:56 -05001265int is_error_pfn(pfn_t pfn)
1266{
1267 return pfn == bad_pfn;
1268}
1269EXPORT_SYMBOL_GPL(is_error_pfn);
1270
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001271static inline unsigned long bad_hva(void)
1272{
1273 return PAGE_OFFSET;
1274}
1275
1276int kvm_is_error_hva(unsigned long addr)
1277{
1278 return addr == bad_hva();
1279}
1280EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1281
Izik Eidus28430992008-10-03 17:40:32 +03001282struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283{
1284 int i;
1285
1286 for (i = 0; i < kvm->nmemslots; ++i) {
1287 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1288
1289 if (gfn >= memslot->base_gfn
1290 && gfn < memslot->base_gfn + memslot->npages)
1291 return memslot;
1292 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001293 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294}
Izik Eidus28430992008-10-03 17:40:32 +03001295EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001296
1297struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1298{
1299 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001300 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001301}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302
Izik Eiduse0d62c72007-10-24 23:57:46 +02001303int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1304{
1305 int i;
1306
1307 gfn = unalias_gfn(kvm, gfn);
1308 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1309 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1310
1311 if (gfn >= memslot->base_gfn
1312 && gfn < memslot->base_gfn + memslot->npages)
1313 return 1;
1314 }
1315 return 0;
1316}
1317EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1318
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001319unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001320{
1321 struct kvm_memory_slot *slot;
1322
1323 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001324 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001325 if (!slot)
1326 return bad_hva();
1327 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1328}
Sheng Yang0d150292008-04-25 21:44:50 +08001329EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001330
Anthony Liguori35149e22008-04-02 14:46:56 -05001331pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001332{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001333 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001334 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001335 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001336 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001337
Avi Kivity60395222007-10-21 11:03:36 +02001338 might_sleep();
1339
Izik Eidus539cb662007-11-11 22:05:04 +02001340 addr = gfn_to_hva(kvm, gfn);
1341 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001342 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001343 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001344 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001345
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001346 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001347
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001348 if (unlikely(npages != 1)) {
1349 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001350
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001351 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001352 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001353
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001354 if (vma == NULL || addr < vma->vm_start ||
1355 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001356 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001357 get_page(bad_page);
1358 return page_to_pfn(bad_page);
1359 }
1360
1361 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001362 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001363 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001364 } else
1365 pfn = page_to_pfn(page[0]);
1366
1367 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001368}
1369
1370EXPORT_SYMBOL_GPL(gfn_to_pfn);
1371
1372struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1373{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001374 pfn_t pfn;
1375
1376 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001377 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001378 return pfn_to_page(pfn);
1379
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001380 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001381
1382 get_page(bad_page);
1383 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001384}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001385
Avi Kivity954bbbc2007-03-30 14:02:32 +03001386EXPORT_SYMBOL_GPL(gfn_to_page);
1387
Izik Eidusb4231d62007-11-20 11:49:33 +02001388void kvm_release_page_clean(struct page *page)
1389{
Anthony Liguori35149e22008-04-02 14:46:56 -05001390 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001391}
1392EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1393
Anthony Liguori35149e22008-04-02 14:46:56 -05001394void kvm_release_pfn_clean(pfn_t pfn)
1395{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001396 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001397 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001398}
1399EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1400
Izik Eidusb4231d62007-11-20 11:49:33 +02001401void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001402{
Anthony Liguori35149e22008-04-02 14:46:56 -05001403 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001404}
Izik Eidusb4231d62007-11-20 11:49:33 +02001405EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001406
Anthony Liguori35149e22008-04-02 14:46:56 -05001407void kvm_release_pfn_dirty(pfn_t pfn)
1408{
1409 kvm_set_pfn_dirty(pfn);
1410 kvm_release_pfn_clean(pfn);
1411}
1412EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1413
1414void kvm_set_page_dirty(struct page *page)
1415{
1416 kvm_set_pfn_dirty(page_to_pfn(page));
1417}
1418EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1419
1420void kvm_set_pfn_dirty(pfn_t pfn)
1421{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001422 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001423 struct page *page = pfn_to_page(pfn);
1424 if (!PageReserved(page))
1425 SetPageDirty(page);
1426 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001427}
1428EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1429
1430void kvm_set_pfn_accessed(pfn_t pfn)
1431{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001432 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001433 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001434}
1435EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1436
1437void kvm_get_pfn(pfn_t pfn)
1438{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001439 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001440 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001441}
1442EXPORT_SYMBOL_GPL(kvm_get_pfn);
1443
Izik Eidus195aefd2007-10-01 22:14:18 +02001444static int next_segment(unsigned long len, int offset)
1445{
1446 if (len > PAGE_SIZE - offset)
1447 return PAGE_SIZE - offset;
1448 else
1449 return len;
1450}
1451
1452int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1453 int len)
1454{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001455 int r;
1456 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001457
Izik Eiduse0506bc2007-11-11 22:10:22 +02001458 addr = gfn_to_hva(kvm, gfn);
1459 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001460 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001461 r = copy_from_user(data, (void __user *)addr + offset, len);
1462 if (r)
1463 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001464 return 0;
1465}
1466EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1467
1468int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1469{
1470 gfn_t gfn = gpa >> PAGE_SHIFT;
1471 int seg;
1472 int offset = offset_in_page(gpa);
1473 int ret;
1474
1475 while ((seg = next_segment(len, offset)) != 0) {
1476 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1477 if (ret < 0)
1478 return ret;
1479 offset = 0;
1480 len -= seg;
1481 data += seg;
1482 ++gfn;
1483 }
1484 return 0;
1485}
1486EXPORT_SYMBOL_GPL(kvm_read_guest);
1487
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001488int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1489 unsigned long len)
1490{
1491 int r;
1492 unsigned long addr;
1493 gfn_t gfn = gpa >> PAGE_SHIFT;
1494 int offset = offset_in_page(gpa);
1495
1496 addr = gfn_to_hva(kvm, gfn);
1497 if (kvm_is_error_hva(addr))
1498 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001499 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001500 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001501 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001502 if (r)
1503 return -EFAULT;
1504 return 0;
1505}
1506EXPORT_SYMBOL(kvm_read_guest_atomic);
1507
Izik Eidus195aefd2007-10-01 22:14:18 +02001508int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1509 int offset, int len)
1510{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001511 int r;
1512 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001513
Izik Eiduse0506bc2007-11-11 22:10:22 +02001514 addr = gfn_to_hva(kvm, gfn);
1515 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001516 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001517 r = copy_to_user((void __user *)addr + offset, data, len);
1518 if (r)
1519 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001520 mark_page_dirty(kvm, gfn);
1521 return 0;
1522}
1523EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1524
1525int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1526 unsigned long len)
1527{
1528 gfn_t gfn = gpa >> PAGE_SHIFT;
1529 int seg;
1530 int offset = offset_in_page(gpa);
1531 int ret;
1532
1533 while ((seg = next_segment(len, offset)) != 0) {
1534 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1535 if (ret < 0)
1536 return ret;
1537 offset = 0;
1538 len -= seg;
1539 data += seg;
1540 ++gfn;
1541 }
1542 return 0;
1543}
1544
1545int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1546{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001547 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001548}
1549EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1550
1551int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1552{
1553 gfn_t gfn = gpa >> PAGE_SHIFT;
1554 int seg;
1555 int offset = offset_in_page(gpa);
1556 int ret;
1557
1558 while ((seg = next_segment(len, offset)) != 0) {
1559 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1560 if (ret < 0)
1561 return ret;
1562 offset = 0;
1563 len -= seg;
1564 ++gfn;
1565 }
1566 return 0;
1567}
1568EXPORT_SYMBOL_GPL(kvm_clear_guest);
1569
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1571{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001572 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573
Uri Lublin3b6fff12007-10-30 10:42:09 +02001574 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001575 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001576 if (memslot && memslot->dirty_bitmap) {
1577 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578
Rusty Russell7e9d6192007-07-31 20:41:14 +10001579 /* avoid RMW */
1580 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1581 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582 }
1583}
1584
Eddie Dongb6958ce2007-07-18 12:15:21 +03001585/*
1586 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1587 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001588void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001589{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001590 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001591
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001592 for (;;) {
1593 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001594
Marcelo Tosattid7690172008-09-08 15:23:48 -03001595 if (kvm_cpu_has_interrupt(vcpu) ||
1596 kvm_cpu_has_pending_timer(vcpu) ||
1597 kvm_arch_vcpu_runnable(vcpu)) {
1598 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001599 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001600 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001601 if (signal_pending(current))
1602 break;
1603
Eddie Dongb6958ce2007-07-18 12:15:21 +03001604 vcpu_put(vcpu);
1605 schedule();
1606 vcpu_load(vcpu);
1607 }
1608
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001609 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001610}
1611
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612void kvm_resched(struct kvm_vcpu *vcpu)
1613{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001614 if (!need_resched())
1615 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617}
1618EXPORT_SYMBOL_GPL(kvm_resched);
1619
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001620static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001621{
1622 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001623 struct page *page;
1624
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001625 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001626 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001627#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001628 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001629 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001630#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001631#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1632 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1633 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1634#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001635 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001636 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001637 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001638 vmf->page = page;
1639 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001640}
1641
1642static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001643 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001644};
1645
1646static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1647{
1648 vma->vm_ops = &kvm_vcpu_vm_ops;
1649 return 0;
1650}
1651
Avi Kivitybccf2152007-02-21 18:04:26 +02001652static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1653{
1654 struct kvm_vcpu *vcpu = filp->private_data;
1655
Al Viro66c0b392008-04-19 20:33:56 +01001656 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001657 return 0;
1658}
1659
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001660static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001661 .release = kvm_vcpu_release,
1662 .unlocked_ioctl = kvm_vcpu_ioctl,
1663 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001664 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001665};
1666
1667/*
1668 * Allocates an inode for the vcpu.
1669 */
1670static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1671{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001672 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001673 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001674 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001675 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001676}
1677
Avi Kivityc5ea7662007-02-20 18:41:05 +02001678/*
1679 * Creates some virtual cpus. Good luck creating more than one.
1680 */
1681static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1682{
1683 int r;
1684 struct kvm_vcpu *vcpu;
1685
Avi Kivityc5ea7662007-02-20 18:41:05 +02001686 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001687 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001688
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001689 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001690 if (IS_ERR(vcpu))
1691 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001692
Avi Kivity15ad7142007-07-11 18:17:21 +03001693 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1694
Avi Kivity26e52152007-11-20 15:30:24 +02001695 r = kvm_arch_vcpu_setup(vcpu);
1696 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001697 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001698
Shaohua Li11ec2802007-07-23 14:51:37 +08001699 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001700 if (kvm->vcpus[n]) {
1701 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001702 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001703 }
1704 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001705 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001706
1707 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001708 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001709 r = create_vcpu_fd(vcpu);
1710 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001711 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001712 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001713
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001714unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001715 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001716 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001717vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001718 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001719 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001720 return r;
1721}
1722
Avi Kivity1961d272007-03-05 19:46:05 +02001723static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1724{
1725 if (sigset) {
1726 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1727 vcpu->sigset_active = 1;
1728 vcpu->sigset = *sigset;
1729 } else
1730 vcpu->sigset_active = 0;
1731 return 0;
1732}
1733
Sheng Yangc1e01512009-02-25 17:22:26 +08001734#ifdef __KVM_HAVE_MSIX
1735static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1736 struct kvm_assigned_msix_nr *entry_nr)
1737{
1738 int r = 0;
1739 struct kvm_assigned_dev_kernel *adev;
1740
1741 mutex_lock(&kvm->lock);
1742
1743 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1744 entry_nr->assigned_dev_id);
1745 if (!adev) {
1746 r = -EINVAL;
1747 goto msix_nr_out;
1748 }
1749
1750 if (adev->entries_nr == 0) {
1751 adev->entries_nr = entry_nr->entry_nr;
1752 if (adev->entries_nr == 0 ||
1753 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1754 r = -EINVAL;
1755 goto msix_nr_out;
1756 }
1757
1758 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1759 entry_nr->entry_nr,
1760 GFP_KERNEL);
1761 if (!adev->host_msix_entries) {
1762 r = -ENOMEM;
1763 goto msix_nr_out;
1764 }
1765 adev->guest_msix_entries = kzalloc(
1766 sizeof(struct kvm_guest_msix_entry) *
1767 entry_nr->entry_nr, GFP_KERNEL);
1768 if (!adev->guest_msix_entries) {
1769 kfree(adev->host_msix_entries);
1770 r = -ENOMEM;
1771 goto msix_nr_out;
1772 }
1773 } else /* Not allowed set MSI-X number twice */
1774 r = -EINVAL;
1775msix_nr_out:
1776 mutex_unlock(&kvm->lock);
1777 return r;
1778}
1779
1780static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1781 struct kvm_assigned_msix_entry *entry)
1782{
1783 int r = 0, i;
1784 struct kvm_assigned_dev_kernel *adev;
1785
1786 mutex_lock(&kvm->lock);
1787
1788 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1789 entry->assigned_dev_id);
1790
1791 if (!adev) {
1792 r = -EINVAL;
1793 goto msix_entry_out;
1794 }
1795
1796 for (i = 0; i < adev->entries_nr; i++)
1797 if (adev->guest_msix_entries[i].vector == 0 ||
1798 adev->guest_msix_entries[i].entry == entry->entry) {
1799 adev->guest_msix_entries[i].entry = entry->entry;
1800 adev->guest_msix_entries[i].vector = entry->gsi;
1801 adev->host_msix_entries[i].entry = entry->entry;
1802 break;
1803 }
1804 if (i == adev->entries_nr) {
1805 r = -ENOSPC;
1806 goto msix_entry_out;
1807 }
1808
1809msix_entry_out:
1810 mutex_unlock(&kvm->lock);
1811
1812 return r;
1813}
1814#endif
1815
Avi Kivitybccf2152007-02-21 18:04:26 +02001816static long kvm_vcpu_ioctl(struct file *filp,
1817 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818{
Avi Kivitybccf2152007-02-21 18:04:26 +02001819 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001820 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001821 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001822 struct kvm_fpu *fpu = NULL;
1823 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001825 if (vcpu->kvm->mm != current->mm)
1826 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001828 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001829 r = -EINVAL;
1830 if (arg)
1831 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001832 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001835 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001837 r = -ENOMEM;
1838 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1839 if (!kvm_regs)
1840 goto out;
1841 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001842 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001843 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001845 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1846 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001848out_free1:
1849 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 break;
1851 }
1852 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001853 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001855 r = -ENOMEM;
1856 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1857 if (!kvm_regs)
1858 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001860 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1861 goto out_free2;
1862 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001864 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001866out_free2:
1867 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868 break;
1869 }
1870 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001871 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1872 r = -ENOMEM;
1873 if (!kvm_sregs)
1874 goto out;
1875 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876 if (r)
1877 goto out;
1878 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001879 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 goto out;
1881 r = 0;
1882 break;
1883 }
1884 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001885 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1886 r = -ENOMEM;
1887 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001888 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001889 r = -EFAULT;
1890 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1891 goto out;
1892 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 if (r)
1894 goto out;
1895 r = 0;
1896 break;
1897 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001898 case KVM_GET_MP_STATE: {
1899 struct kvm_mp_state mp_state;
1900
1901 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1902 if (r)
1903 goto out;
1904 r = -EFAULT;
1905 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1906 goto out;
1907 r = 0;
1908 break;
1909 }
1910 case KVM_SET_MP_STATE: {
1911 struct kvm_mp_state mp_state;
1912
1913 r = -EFAULT;
1914 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1915 goto out;
1916 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1917 if (r)
1918 goto out;
1919 r = 0;
1920 break;
1921 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922 case KVM_TRANSLATE: {
1923 struct kvm_translation tr;
1924
1925 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001926 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001928 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929 if (r)
1930 goto out;
1931 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001932 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933 goto out;
1934 r = 0;
1935 break;
1936 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001937 case KVM_SET_GUEST_DEBUG: {
1938 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939
1940 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001941 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001943 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 if (r)
1945 goto out;
1946 r = 0;
1947 break;
1948 }
Avi Kivity1961d272007-03-05 19:46:05 +02001949 case KVM_SET_SIGNAL_MASK: {
1950 struct kvm_signal_mask __user *sigmask_arg = argp;
1951 struct kvm_signal_mask kvm_sigmask;
1952 sigset_t sigset, *p;
1953
1954 p = NULL;
1955 if (argp) {
1956 r = -EFAULT;
1957 if (copy_from_user(&kvm_sigmask, argp,
1958 sizeof kvm_sigmask))
1959 goto out;
1960 r = -EINVAL;
1961 if (kvm_sigmask.len != sizeof sigset)
1962 goto out;
1963 r = -EFAULT;
1964 if (copy_from_user(&sigset, sigmask_arg->sigset,
1965 sizeof sigset))
1966 goto out;
1967 p = &sigset;
1968 }
1969 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1970 break;
1971 }
Avi Kivityb8836732007-04-01 16:34:31 +03001972 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001973 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1974 r = -ENOMEM;
1975 if (!fpu)
1976 goto out;
1977 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001978 if (r)
1979 goto out;
1980 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001981 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001982 goto out;
1983 r = 0;
1984 break;
1985 }
1986 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001987 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1988 r = -ENOMEM;
1989 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001990 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001991 r = -EFAULT;
1992 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1993 goto out;
1994 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001995 if (r)
1996 goto out;
1997 r = 0;
1998 break;
1999 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002000 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002001 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002002 }
2003out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002004 kfree(fpu);
2005 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002006 return r;
2007}
2008
2009static long kvm_vm_ioctl(struct file *filp,
2010 unsigned int ioctl, unsigned long arg)
2011{
2012 struct kvm *kvm = filp->private_data;
2013 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002014 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002015
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002016 if (kvm->mm != current->mm)
2017 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002018 switch (ioctl) {
2019 case KVM_CREATE_VCPU:
2020 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2021 if (r < 0)
2022 goto out;
2023 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002024 case KVM_SET_USER_MEMORY_REGION: {
2025 struct kvm_userspace_memory_region kvm_userspace_mem;
2026
2027 r = -EFAULT;
2028 if (copy_from_user(&kvm_userspace_mem, argp,
2029 sizeof kvm_userspace_mem))
2030 goto out;
2031
2032 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033 if (r)
2034 goto out;
2035 break;
2036 }
2037 case KVM_GET_DIRTY_LOG: {
2038 struct kvm_dirty_log log;
2039
2040 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002041 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002042 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002043 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044 if (r)
2045 goto out;
2046 break;
2047 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002048#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2049 case KVM_REGISTER_COALESCED_MMIO: {
2050 struct kvm_coalesced_mmio_zone zone;
2051 r = -EFAULT;
2052 if (copy_from_user(&zone, argp, sizeof zone))
2053 goto out;
2054 r = -ENXIO;
2055 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2056 if (r)
2057 goto out;
2058 r = 0;
2059 break;
2060 }
2061 case KVM_UNREGISTER_COALESCED_MMIO: {
2062 struct kvm_coalesced_mmio_zone zone;
2063 r = -EFAULT;
2064 if (copy_from_user(&zone, argp, sizeof zone))
2065 goto out;
2066 r = -ENXIO;
2067 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2068 if (r)
2069 goto out;
2070 r = 0;
2071 break;
2072 }
2073#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002074#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2075 case KVM_ASSIGN_PCI_DEVICE: {
2076 struct kvm_assigned_pci_dev assigned_dev;
2077
2078 r = -EFAULT;
2079 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2080 goto out;
2081 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2082 if (r)
2083 goto out;
2084 break;
2085 }
2086 case KVM_ASSIGN_IRQ: {
2087 struct kvm_assigned_irq assigned_irq;
2088
2089 r = -EFAULT;
2090 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2091 goto out;
2092 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2093 if (r)
2094 goto out;
2095 break;
2096 }
2097#endif
Weidong Han0a920352008-12-02 21:24:23 +08002098#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2099 case KVM_DEASSIGN_PCI_DEVICE: {
2100 struct kvm_assigned_pci_dev assigned_dev;
2101
2102 r = -EFAULT;
2103 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2104 goto out;
2105 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2106 if (r)
2107 goto out;
2108 break;
2109 }
2110#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002111#ifdef KVM_CAP_IRQ_ROUTING
2112 case KVM_SET_GSI_ROUTING: {
2113 struct kvm_irq_routing routing;
2114 struct kvm_irq_routing __user *urouting;
2115 struct kvm_irq_routing_entry *entries;
2116
2117 r = -EFAULT;
2118 if (copy_from_user(&routing, argp, sizeof(routing)))
2119 goto out;
2120 r = -EINVAL;
2121 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2122 goto out;
2123 if (routing.flags)
2124 goto out;
2125 r = -ENOMEM;
2126 entries = vmalloc(routing.nr * sizeof(*entries));
2127 if (!entries)
2128 goto out;
2129 r = -EFAULT;
2130 urouting = argp;
2131 if (copy_from_user(entries, urouting->entries,
2132 routing.nr * sizeof(*entries)))
2133 goto out_free_irq_routing;
2134 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2135 routing.flags);
2136 out_free_irq_routing:
2137 vfree(entries);
2138 break;
2139 }
Sheng Yangc1e01512009-02-25 17:22:26 +08002140#ifdef __KVM_HAVE_MSIX
2141 case KVM_ASSIGN_SET_MSIX_NR: {
2142 struct kvm_assigned_msix_nr entry_nr;
2143 r = -EFAULT;
2144 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2145 goto out;
2146 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2147 if (r)
2148 goto out;
2149 break;
2150 }
2151 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2152 struct kvm_assigned_msix_entry entry;
2153 r = -EFAULT;
2154 if (copy_from_user(&entry, argp, sizeof entry))
2155 goto out;
2156 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2157 if (r)
2158 goto out;
2159 break;
2160 }
Avi Kivity399ec802008-11-19 13:58:46 +02002161#endif
Sheng Yangc1e01512009-02-25 17:22:26 +08002162#endif /* KVM_CAP_IRQ_ROUTING */
Avi Kivityf17abe92007-02-21 19:28:04 +02002163 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002164 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002165 }
2166out:
2167 return r;
2168}
2169
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002170static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002171{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002172 struct page *page[1];
2173 unsigned long addr;
2174 int npages;
2175 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002176 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002177
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002178 addr = gfn_to_hva(kvm, gfn);
2179 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002180 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002181
2182 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2183 NULL);
2184 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002185 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002186
2187 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002188 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002189}
2190
2191static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002192 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002193};
2194
2195static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2196{
2197 vma->vm_ops = &kvm_vm_vm_ops;
2198 return 0;
2199}
2200
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002201static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002202 .release = kvm_vm_release,
2203 .unlocked_ioctl = kvm_vm_ioctl,
2204 .compat_ioctl = kvm_vm_ioctl,
2205 .mmap = kvm_vm_mmap,
2206};
2207
2208static int kvm_dev_ioctl_create_vm(void)
2209{
Al Viro2030a422008-02-23 06:46:49 -05002210 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002211 struct kvm *kvm;
2212
Avi Kivityf17abe92007-02-21 19:28:04 +02002213 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002214 if (IS_ERR(kvm))
2215 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002216 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002217 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002218 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002219
Avi Kivityf17abe92007-02-21 19:28:04 +02002220 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002221}
2222
Avi Kivity1a811b62008-12-08 18:25:27 +02002223static long kvm_dev_ioctl_check_extension_generic(long arg)
2224{
2225 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002226 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002227 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002228 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Avi Kivity1a811b62008-12-08 18:25:27 +02002229 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002230#ifdef CONFIG_HAVE_KVM_IRQCHIP
2231 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002232 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002233#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002234 default:
2235 break;
2236 }
2237 return kvm_dev_ioctl_check_extension(arg);
2238}
2239
Avi Kivityf17abe92007-02-21 19:28:04 +02002240static long kvm_dev_ioctl(struct file *filp,
2241 unsigned int ioctl, unsigned long arg)
2242{
Avi Kivity07c45a32007-03-07 13:05:38 +02002243 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002244
2245 switch (ioctl) {
2246 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002247 r = -EINVAL;
2248 if (arg)
2249 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002250 r = KVM_API_VERSION;
2251 break;
2252 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002253 r = -EINVAL;
2254 if (arg)
2255 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002256 r = kvm_dev_ioctl_create_vm();
2257 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002258 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002259 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002260 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002261 case KVM_GET_VCPU_MMAP_SIZE:
2262 r = -EINVAL;
2263 if (arg)
2264 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002265 r = PAGE_SIZE; /* struct kvm_run */
2266#ifdef CONFIG_X86
2267 r += PAGE_SIZE; /* pio data page */
2268#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002269#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2270 r += PAGE_SIZE; /* coalesced mmio ring page */
2271#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002272 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002273 case KVM_TRACE_ENABLE:
2274 case KVM_TRACE_PAUSE:
2275 case KVM_TRACE_DISABLE:
2276 r = kvm_trace_ioctl(ioctl, arg);
2277 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002279 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 }
2281out:
2282 return r;
2283}
2284
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 .unlocked_ioctl = kvm_dev_ioctl,
2287 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288};
2289
2290static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002291 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292 "kvm",
2293 &kvm_chardev_ops,
2294};
2295
Avi Kivity1b6c0162007-05-24 13:03:52 +03002296static void hardware_enable(void *junk)
2297{
2298 int cpu = raw_smp_processor_id();
2299
Rusty Russell7f59f492008-12-07 21:25:45 +10302300 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002301 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302302 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002303 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002304}
2305
2306static void hardware_disable(void *junk)
2307{
2308 int cpu = raw_smp_processor_id();
2309
Rusty Russell7f59f492008-12-07 21:25:45 +10302310 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002311 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302312 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002313 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002314}
2315
Avi Kivity774c47f2007-02-12 00:54:47 -08002316static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2317 void *v)
2318{
2319 int cpu = (long)v;
2320
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002321 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002322 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002323 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002324 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2325 cpu);
2326 hardware_disable(NULL);
2327 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002328 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002329 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2330 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002331 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002332 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002333 case CPU_ONLINE:
2334 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2335 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002336 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002337 break;
2338 }
2339 return NOTIFY_OK;
2340}
2341
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002342
2343asmlinkage void kvm_handle_fault_on_reboot(void)
2344{
2345 if (kvm_rebooting)
2346 /* spin while reset goes on */
2347 while (true)
2348 ;
2349 /* Fault while not rebooting. We want the trace. */
2350 BUG();
2351}
2352EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2353
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002354static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002355 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002356{
2357 if (val == SYS_RESTART) {
2358 /*
2359 * Some (well, at least mine) BIOSes hang on reboot if
2360 * in vmx root mode.
2361 */
2362 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002363 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002364 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002365 }
2366 return NOTIFY_OK;
2367}
2368
2369static struct notifier_block kvm_reboot_notifier = {
2370 .notifier_call = kvm_reboot,
2371 .priority = 0,
2372};
2373
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002374void kvm_io_bus_init(struct kvm_io_bus *bus)
2375{
2376 memset(bus, 0, sizeof(*bus));
2377}
2378
2379void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2380{
2381 int i;
2382
2383 for (i = 0; i < bus->dev_count; i++) {
2384 struct kvm_io_device *pos = bus->devs[i];
2385
2386 kvm_iodevice_destructor(pos);
2387 }
2388}
2389
Laurent Vivier92760492008-05-30 16:05:53 +02002390struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2391 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002392{
2393 int i;
2394
2395 for (i = 0; i < bus->dev_count; i++) {
2396 struct kvm_io_device *pos = bus->devs[i];
2397
Laurent Vivier92760492008-05-30 16:05:53 +02002398 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002399 return pos;
2400 }
2401
2402 return NULL;
2403}
2404
2405void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2406{
2407 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2408
2409 bus->devs[bus->dev_count++] = dev;
2410}
2411
Avi Kivity774c47f2007-02-12 00:54:47 -08002412static struct notifier_block kvm_cpu_notifier = {
2413 .notifier_call = kvm_cpu_hotplug,
2414 .priority = 20, /* must be > scheduler priority */
2415};
2416
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002417static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002418{
2419 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002420 struct kvm *kvm;
2421
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002422 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002423 spin_lock(&kvm_lock);
2424 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002425 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002426 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002427 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002428}
2429
2430DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2431
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002432static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002433{
2434 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002435 struct kvm *kvm;
2436 struct kvm_vcpu *vcpu;
2437 int i;
2438
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002439 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002440 spin_lock(&kvm_lock);
2441 list_for_each_entry(kvm, &vm_list, vm_list)
2442 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002443 vcpu = kvm->vcpus[i];
2444 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002445 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002446 }
2447 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002448 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002449}
2450
Avi Kivityba1389b2007-11-18 16:24:12 +02002451DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2452
2453static struct file_operations *stat_fops[] = {
2454 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2455 [KVM_STAT_VM] = &vm_stat_fops,
2456};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002457
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002458static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459{
2460 struct kvm_stats_debugfs_item *p;
2461
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002462 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002464 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002465 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002466 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467}
2468
2469static void kvm_exit_debug(void)
2470{
2471 struct kvm_stats_debugfs_item *p;
2472
2473 for (p = debugfs_entries; p->name; ++p)
2474 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002475 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476}
2477
Avi Kivity59ae6c62007-02-12 00:54:48 -08002478static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2479{
Avi Kivity4267c412007-05-24 13:09:41 +03002480 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002481 return 0;
2482}
2483
2484static int kvm_resume(struct sys_device *dev)
2485{
Avi Kivity4267c412007-05-24 13:09:41 +03002486 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002487 return 0;
2488}
2489
2490static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01002491 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002492 .suspend = kvm_suspend,
2493 .resume = kvm_resume,
2494};
2495
2496static struct sys_device kvm_sysdev = {
2497 .id = 0,
2498 .cls = &kvm_sysdev_class,
2499};
2500
Izik Eiduscea7bb22007-10-17 19:17:48 +02002501struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002502pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503
Avi Kivity15ad7142007-07-11 18:17:21 +03002504static inline
2505struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2506{
2507 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2508}
2509
2510static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2511{
2512 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2513
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002514 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002515}
2516
2517static void kvm_sched_out(struct preempt_notifier *pn,
2518 struct task_struct *next)
2519{
2520 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2521
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002522 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002523}
2524
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002525int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002526 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527{
2528 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002529 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002531 kvm_init_debug();
2532
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002533 r = kvm_arch_init(opaque);
2534 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002535 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002536
2537 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2538
2539 if (bad_page == NULL) {
2540 r = -ENOMEM;
2541 goto out;
2542 }
2543
Anthony Liguori35149e22008-04-02 14:46:56 -05002544 bad_pfn = page_to_pfn(bad_page);
2545
Avi Kivity8437a6172009-06-06 14:52:35 -07002546 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302547 r = -ENOMEM;
2548 goto out_free_0;
2549 }
Avi Kivitya4c03642009-06-06 12:34:39 +03002550 cpumask_clear(cpus_hardware_enabled);
Rusty Russell7f59f492008-12-07 21:25:45 +10302551
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002552 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302554 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555
Yang, Sheng002c7f72007-07-31 14:23:01 +03002556 for_each_online_cpu(cpu) {
2557 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002558 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002559 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002560 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002561 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002562 }
2563
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002564 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002565 r = register_cpu_notifier(&kvm_cpu_notifier);
2566 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002567 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568 register_reboot_notifier(&kvm_reboot_notifier);
2569
Avi Kivity59ae6c62007-02-12 00:54:48 -08002570 r = sysdev_class_register(&kvm_sysdev_class);
2571 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002572 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002573
2574 r = sysdev_register(&kvm_sysdev);
2575 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002576 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002577
Rusty Russellc16f8622007-07-30 21:12:19 +10002578 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2579 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002580 __alignof__(struct kvm_vcpu),
2581 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002582 if (!kvm_vcpu_cache) {
2583 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002584 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002585 }
2586
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002588 kvm_vm_fops.owner = module;
2589 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590
2591 r = misc_register(&kvm_dev);
2592 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002593 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594 goto out_free;
2595 }
2596
Avi Kivity15ad7142007-07-11 18:17:21 +03002597 kvm_preempt_ops.sched_in = kvm_sched_in;
2598 kvm_preempt_ops.sched_out = kvm_sched_out;
Sheng Yang5319c662008-11-24 14:32:57 +08002599#ifndef CONFIG_X86
2600 msi2intx = 0;
2601#endif
Avi Kivity15ad7142007-07-11 18:17:21 +03002602
Avi Kivityc7addb92007-09-16 18:58:32 +02002603 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604
2605out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002606 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002607out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002608 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002609out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002610 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002611out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002613 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002614out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002615 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002616out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002617 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302618out_free_0a:
2619 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002620out_free_0:
2621 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002622out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002623 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002624 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002625out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 return r;
2627}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002628EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002630void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002632 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002634 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002635 sysdev_unregister(&kvm_sysdev);
2636 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002638 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002639 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002640 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002641 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302643 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002644 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002646EXPORT_SYMBOL_GPL(kvm_exit);