blob: d47e660fb709aab04a45eb680a4f062a3eec2c5e [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Hollis Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/io.h>
49#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020050#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Laurent Vivier5f94c172008-05-30 16:05:54 +020052#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
53#include "coalesced_mmio.h"
54#endif
55
Xiantao Zhang8a98f662008-10-06 13:47:38 +080056#ifdef KVM_CAP_DEVICE_ASSIGNMENT
57#include <linux/pci.h>
58#include <linux/interrupt.h>
59#include "irq.h"
60#endif
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062MODULE_AUTHOR("Qumranet");
63MODULE_LICENSE("GPL");
64
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080065DEFINE_SPINLOCK(kvm_lock);
66LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080067
Rusty Russell7f59f492008-12-07 21:25:45 +103068static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030069
Rusty Russellc16f8622007-07-30 21:12:19 +100070struct kmem_cache *kvm_vcpu_cache;
71EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030072
Avi Kivity15ad7142007-07-11 18:17:21 +030073static __read_mostly struct preempt_ops kvm_preempt_ops;
74
Hollis Blanchard76f7c872008-04-15 16:05:42 -050075struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080076
Avi Kivitybccf2152007-02-21 18:04:26 +020077static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
78 unsigned long arg);
79
Hannes Edere8ba5d32008-11-28 17:02:42 +010080static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030081
Xiantao Zhang8a98f662008-10-06 13:47:38 +080082#ifdef KVM_CAP_DEVICE_ASSIGNMENT
83static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
84 int assigned_dev_id)
85{
86 struct list_head *ptr;
87 struct kvm_assigned_dev_kernel *match;
88
89 list_for_each(ptr, head) {
90 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
91 if (match->assigned_dev_id == assigned_dev_id)
92 return match;
93 }
94 return NULL;
95}
96
Sheng Yang2350bd12009-02-25 17:22:27 +080097static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
98 *assigned_dev, int irq)
99{
100 int i, index;
101 struct msix_entry *host_msix_entries;
102
103 host_msix_entries = assigned_dev->host_msix_entries;
104
105 index = -1;
106 for (i = 0; i < assigned_dev->entries_nr; i++)
107 if (irq == host_msix_entries[i].vector) {
108 index = i;
109 break;
110 }
111 if (index < 0) {
112 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
113 return 0;
114 }
115
116 return index;
117}
118
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800119static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
120{
121 struct kvm_assigned_dev_kernel *assigned_dev;
Sheng Yang2350bd12009-02-25 17:22:27 +0800122 struct kvm *kvm;
Sheng Yang968a6342009-04-30 10:58:42 +0800123 int i;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800124
125 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
126 interrupt_work);
Sheng Yang2350bd12009-02-25 17:22:27 +0800127 kvm = assigned_dev->kvm;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800128
129 /* This is taken to safely inject irq inside the guest. When
130 * the interrupt injection (or the ioapic code) uses a
131 * finer-grained lock, update this
132 */
Sheng Yang2350bd12009-02-25 17:22:27 +0800133 mutex_lock(&kvm->lock);
Marcelo Tosatti547de292009-05-07 17:55:13 -0300134 spin_lock_irq(&assigned_dev->assigned_dev_lock);
Sheng Yange56d5322009-03-12 21:45:39 +0800135 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800136 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);
Sheng Yang2350bd12009-02-25 17:22:27 +0800146 }
Sheng Yang968a6342009-04-30 10:58:42 +0800147 } else
Sheng Yang2350bd12009-02-25 17:22:27 +0800148 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
149 assigned_dev->guest_irq, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800150
Marcelo Tosatti547de292009-05-07 17:55:13 -0300151 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800152 mutex_unlock(&assigned_dev->kvm->lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800153}
154
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800155static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
156{
Marcelo Tosatti547de292009-05-07 17:55:13 -0300157 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800158 struct kvm_assigned_dev_kernel *assigned_dev =
159 (struct kvm_assigned_dev_kernel *) dev_id;
160
Marcelo Tosatti547de292009-05-07 17:55:13 -0300161 spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
Sheng Yange56d5322009-03-12 21:45:39 +0800162 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800163 int index = find_index_from_host_irq(assigned_dev, irq);
164 if (index < 0)
Marcelo Tosatti547de292009-05-07 17:55:13 -0300165 goto out;
Sheng Yang2350bd12009-02-25 17:22:27 +0800166 assigned_dev->guest_msix_entries[index].flags |=
167 KVM_ASSIGNED_MSIX_PENDING;
168 }
169
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800170 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000171
Sheng Yang968a6342009-04-30 10:58:42 +0800172 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
173 disable_irq_nosync(irq);
174 assigned_dev->host_irq_disabled = true;
175 }
Mark McLoughlindefaf152008-12-02 12:16:33 +0000176
Marcelo Tosatti547de292009-05-07 17:55:13 -0300177out:
178 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800179 return IRQ_HANDLED;
180}
181
182/* Ack the irq line for an assigned device */
183static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
184{
185 struct kvm_assigned_dev_kernel *dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300186 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800187
188 if (kian->gsi == -1)
189 return;
190
191 dev = container_of(kian, struct kvm_assigned_dev_kernel,
192 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000193
Sheng Yang5550af42008-10-15 20:15:06 +0800194 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000195
196 /* The guest irq may be shared so this ack may be
197 * from another device.
198 */
Marcelo Tosatti547de292009-05-07 17:55:13 -0300199 spin_lock_irqsave(&dev->assigned_dev_lock, flags);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000200 if (dev->host_irq_disabled) {
201 enable_irq(dev->host_irq);
202 dev->host_irq_disabled = false;
203 }
Marcelo Tosatti547de292009-05-07 17:55:13 -0300204 spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800205}
206
Sheng Yange56d5322009-03-12 21:45:39 +0800207static void deassign_guest_irq(struct kvm *kvm,
208 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800209{
Sheng Yange19e30e2008-10-20 16:07:10 +0800210 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Sheng Yange56d5322009-03-12 21:45:39 +0800211 assigned_dev->ack_notifier.gsi = -1;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000212
213 if (assigned_dev->irq_source_id != -1)
214 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
215 assigned_dev->irq_source_id = -1;
Sheng Yange56d5322009-03-12 21:45:39 +0800216 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
217}
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800218
Sheng Yange56d5322009-03-12 21:45:39 +0800219/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
220static void deassign_host_irq(struct kvm *kvm,
221 struct kvm_assigned_dev_kernel *assigned_dev)
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 Yange56d5322009-03-12 21:45:39 +0800239 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yangd510d6c2009-02-25 17:22:28 +0800240 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
Sheng Yange56d5322009-03-12 21:45:39 +0800262 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800263 pci_disable_msi(assigned_dev->dev);
264 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000265
Sheng Yange56d5322009-03-12 21:45:39 +0800266 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000267}
268
Sheng Yange56d5322009-03-12 21:45:39 +0800269static int kvm_deassign_irq(struct kvm *kvm,
270 struct kvm_assigned_dev_kernel *assigned_dev,
271 unsigned long irq_requested_type)
272{
273 unsigned long guest_irq_type, host_irq_type;
274
275 if (!irqchip_in_kernel(kvm))
276 return -EINVAL;
277 /* no irq assignment to deassign */
278 if (!assigned_dev->irq_requested_type)
279 return -ENXIO;
280
281 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
282 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
283
284 if (host_irq_type)
285 deassign_host_irq(kvm, assigned_dev);
286 if (guest_irq_type)
287 deassign_guest_irq(kvm, assigned_dev);
288
289 return 0;
290}
291
292static void kvm_free_assigned_irq(struct kvm *kvm,
293 struct kvm_assigned_dev_kernel *assigned_dev)
294{
295 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
296}
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000297
298static void kvm_free_assigned_device(struct kvm *kvm,
299 struct kvm_assigned_dev_kernel
300 *assigned_dev)
301{
302 kvm_free_assigned_irq(kvm, assigned_dev);
303
Sheng Yang6eb55812008-10-31 12:37:41 +0800304 pci_reset_function(assigned_dev->dev);
305
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800306 pci_release_regions(assigned_dev->dev);
307 pci_disable_device(assigned_dev->dev);
308 pci_dev_put(assigned_dev->dev);
309
310 list_del(&assigned_dev->list);
311 kfree(assigned_dev);
312}
313
314void kvm_free_all_assigned_devices(struct kvm *kvm)
315{
316 struct list_head *ptr, *ptr2;
317 struct kvm_assigned_dev_kernel *assigned_dev;
318
319 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
320 assigned_dev = list_entry(ptr,
321 struct kvm_assigned_dev_kernel,
322 list);
323
324 kvm_free_assigned_device(kvm, assigned_dev);
325 }
326}
327
Sheng Yange56d5322009-03-12 21:45:39 +0800328static int assigned_device_enable_host_intx(struct kvm *kvm,
329 struct kvm_assigned_dev_kernel *dev)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800330{
Sheng Yange56d5322009-03-12 21:45:39 +0800331 dev->host_irq = dev->dev->irq;
332 /* Even though this is PCI, we don't want to use shared
333 * interrupts. Sharing host devices with guest-assigned devices
334 * on the same interrupt line is not a happy situation: there
335 * are going to be long delays in accepting, acking, etc.
336 */
337 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
338 0, "kvm_assigned_intx_device", (void *)dev))
339 return -EIO;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800340 return 0;
341}
342
Sheng Yange56d5322009-03-12 21:45:39 +0800343#ifdef __KVM_HAVE_MSI
344static int assigned_device_enable_host_msi(struct kvm *kvm,
345 struct kvm_assigned_dev_kernel *dev)
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800346{
347 int r;
348
Sheng Yange56d5322009-03-12 21:45:39 +0800349 if (!dev->dev->msi_enabled) {
350 r = pci_enable_msi(dev->dev);
351 if (r)
352 return r;
Sheng Yang5319c662008-11-24 14:32:57 +0800353 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800354
Sheng Yange56d5322009-03-12 21:45:39 +0800355 dev->host_irq = dev->dev->irq;
356 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
357 "kvm_assigned_msi_device", (void *)dev)) {
358 pci_disable_msi(dev->dev);
359 return -EIO;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800360 }
361
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800362 return 0;
363}
364#endif
365
Sheng Yangd510d6c2009-02-25 17:22:28 +0800366#ifdef __KVM_HAVE_MSIX
Sheng Yange56d5322009-03-12 21:45:39 +0800367static int assigned_device_enable_host_msix(struct kvm *kvm,
368 struct kvm_assigned_dev_kernel *dev)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800369{
Sheng Yange56d5322009-03-12 21:45:39 +0800370 int i, r = -EINVAL;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800371
Sheng Yange56d5322009-03-12 21:45:39 +0800372 /* host_msix_entries and guest_msix_entries should have been
373 * initialized */
374 if (dev->entries_nr == 0)
375 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800376
Sheng Yange56d5322009-03-12 21:45:39 +0800377 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
378 if (r)
379 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800380
Sheng Yange56d5322009-03-12 21:45:39 +0800381 for (i = 0; i < dev->entries_nr; i++) {
382 r = request_irq(dev->host_msix_entries[i].vector,
383 kvm_assigned_dev_intr, 0,
384 "kvm_assigned_msix_device",
385 (void *)dev);
386 /* FIXME: free requested_irq's on failure */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800387 if (r)
388 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800389 }
390
Sheng Yange56d5322009-03-12 21:45:39 +0800391 return 0;
392}
Sheng Yangd510d6c2009-02-25 17:22:28 +0800393
Sheng Yange56d5322009-03-12 21:45:39 +0800394#endif
395
396static int assigned_device_enable_guest_intx(struct kvm *kvm,
397 struct kvm_assigned_dev_kernel *dev,
398 struct kvm_assigned_irq *irq)
399{
400 dev->guest_irq = irq->guest_irq;
401 dev->ack_notifier.gsi = irq->guest_irq;
402 return 0;
403}
404
405#ifdef __KVM_HAVE_MSI
406static int assigned_device_enable_guest_msi(struct kvm *kvm,
407 struct kvm_assigned_dev_kernel *dev,
408 struct kvm_assigned_irq *irq)
409{
410 dev->guest_irq = irq->guest_irq;
411 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800412 dev->host_irq_disabled = false;
Sheng Yange56d5322009-03-12 21:45:39 +0800413 return 0;
414}
415#endif
416#ifdef __KVM_HAVE_MSIX
417static int assigned_device_enable_guest_msix(struct kvm *kvm,
418 struct kvm_assigned_dev_kernel *dev,
419 struct kvm_assigned_irq *irq)
420{
421 dev->guest_irq = irq->guest_irq;
422 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800423 dev->host_irq_disabled = false;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800424 return 0;
425}
426#endif
427
Sheng Yange56d5322009-03-12 21:45:39 +0800428static int assign_host_irq(struct kvm *kvm,
429 struct kvm_assigned_dev_kernel *dev,
430 __u32 host_irq_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800431{
Sheng Yange56d5322009-03-12 21:45:39 +0800432 int r = -EEXIST;
433
434 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
435 return r;
436
437 switch (host_irq_type) {
438 case KVM_DEV_IRQ_HOST_INTX:
439 r = assigned_device_enable_host_intx(kvm, dev);
440 break;
441#ifdef __KVM_HAVE_MSI
442 case KVM_DEV_IRQ_HOST_MSI:
443 r = assigned_device_enable_host_msi(kvm, dev);
444 break;
445#endif
446#ifdef __KVM_HAVE_MSIX
447 case KVM_DEV_IRQ_HOST_MSIX:
448 r = assigned_device_enable_host_msix(kvm, dev);
449 break;
450#endif
451 default:
452 r = -EINVAL;
453 }
454
455 if (!r)
456 dev->irq_requested_type |= host_irq_type;
457
458 return r;
459}
460
461static int assign_guest_irq(struct kvm *kvm,
462 struct kvm_assigned_dev_kernel *dev,
463 struct kvm_assigned_irq *irq,
464 unsigned long guest_irq_type)
465{
466 int id;
467 int r = -EEXIST;
468
469 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
470 return r;
471
472 id = kvm_request_irq_source_id(kvm);
473 if (id < 0)
474 return id;
475
476 dev->irq_source_id = id;
477
478 switch (guest_irq_type) {
479 case KVM_DEV_IRQ_GUEST_INTX:
480 r = assigned_device_enable_guest_intx(kvm, dev, irq);
481 break;
482#ifdef __KVM_HAVE_MSI
483 case KVM_DEV_IRQ_GUEST_MSI:
484 r = assigned_device_enable_guest_msi(kvm, dev, irq);
485 break;
486#endif
487#ifdef __KVM_HAVE_MSIX
488 case KVM_DEV_IRQ_GUEST_MSIX:
489 r = assigned_device_enable_guest_msix(kvm, dev, irq);
490 break;
491#endif
492 default:
493 r = -EINVAL;
494 }
495
496 if (!r) {
497 dev->irq_requested_type |= guest_irq_type;
498 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
499 } else
500 kvm_free_irq_source_id(kvm, dev->irq_source_id);
501
502 return r;
503}
504
505/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
506static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
507 struct kvm_assigned_irq *assigned_irq)
508{
509 int r = -EINVAL;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800510 struct kvm_assigned_dev_kernel *match;
Sheng Yange56d5322009-03-12 21:45:39 +0800511 unsigned long host_irq_type, guest_irq_type;
512
513 if (!capable(CAP_SYS_RAWIO))
514 return -EPERM;
515
516 if (!irqchip_in_kernel(kvm))
517 return r;
518
519 mutex_lock(&kvm->lock);
520 r = -ENODEV;
521 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
522 assigned_irq->assigned_dev_id);
523 if (!match)
524 goto out;
525
526 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
527 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
528
529 r = -EINVAL;
530 /* can only assign one type at a time */
531 if (hweight_long(host_irq_type) > 1)
532 goto out;
533 if (hweight_long(guest_irq_type) > 1)
534 goto out;
535 if (host_irq_type == 0 && guest_irq_type == 0)
536 goto out;
537
538 r = 0;
539 if (host_irq_type)
540 r = assign_host_irq(kvm, match, host_irq_type);
541 if (r)
542 goto out;
543
544 if (guest_irq_type)
545 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
546out:
547 mutex_unlock(&kvm->lock);
548 return r;
549}
550
551static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
552 struct kvm_assigned_irq
553 *assigned_irq)
554{
555 int r = -ENODEV;
556 struct kvm_assigned_dev_kernel *match;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800557
558 mutex_lock(&kvm->lock);
559
560 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
561 assigned_irq->assigned_dev_id);
Sheng Yange56d5322009-03-12 21:45:39 +0800562 if (!match)
563 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800564
Sheng Yange56d5322009-03-12 21:45:39 +0800565 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
566out:
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800567 mutex_unlock(&kvm->lock);
568 return r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800569}
570
571static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
572 struct kvm_assigned_pci_dev *assigned_dev)
573{
574 int r = 0;
575 struct kvm_assigned_dev_kernel *match;
576 struct pci_dev *dev;
577
Mark McLoughlin682edb42009-02-05 18:23:46 +0000578 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800579 mutex_lock(&kvm->lock);
580
581 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
582 assigned_dev->assigned_dev_id);
583 if (match) {
584 /* device already assigned */
Sheng Yange56d5322009-03-12 21:45:39 +0800585 r = -EEXIST;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800586 goto out;
587 }
588
589 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
590 if (match == NULL) {
591 printk(KERN_INFO "%s: Couldn't allocate memory\n",
592 __func__);
593 r = -ENOMEM;
594 goto out;
595 }
596 dev = pci_get_bus_and_slot(assigned_dev->busnr,
597 assigned_dev->devfn);
598 if (!dev) {
599 printk(KERN_INFO "%s: host device not found\n", __func__);
600 r = -EINVAL;
601 goto out_free;
602 }
603 if (pci_enable_device(dev)) {
604 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
605 r = -EBUSY;
606 goto out_put;
607 }
608 r = pci_request_regions(dev, "kvm_assigned_device");
609 if (r) {
610 printk(KERN_INFO "%s: Could not get access to device regions\n",
611 __func__);
612 goto out_disable;
613 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800614
615 pci_reset_function(dev);
616
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800617 match->assigned_dev_id = assigned_dev->assigned_dev_id;
618 match->host_busnr = assigned_dev->busnr;
619 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800620 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800621 match->dev = dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300622 spin_lock_init(&match->assigned_dev_lock);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000623 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800624 match->kvm = kvm;
Sheng Yange56d5322009-03-12 21:45:39 +0800625 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
626 INIT_WORK(&match->interrupt_work,
627 kvm_assigned_dev_interrupt_work_handler);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800628
629 list_add(&match->list, &kvm->arch.assigned_dev_head);
630
631 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100632 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800633 r = kvm_iommu_map_guest(kvm);
634 if (r)
635 goto out_list_del;
636 }
637 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800638 if (r)
639 goto out_list_del;
640 }
641
642out:
643 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000644 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800645 return r;
646out_list_del:
647 list_del(&match->list);
648 pci_release_regions(dev);
649out_disable:
650 pci_disable_device(dev);
651out_put:
652 pci_dev_put(dev);
653out_free:
654 kfree(match);
655 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000656 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800657 return r;
658}
659#endif
660
Weidong Han0a920352008-12-02 21:24:23 +0800661#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
662static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
663 struct kvm_assigned_pci_dev *assigned_dev)
664{
665 int r = 0;
666 struct kvm_assigned_dev_kernel *match;
667
668 mutex_lock(&kvm->lock);
669
670 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
671 assigned_dev->assigned_dev_id);
672 if (!match) {
673 printk(KERN_INFO "%s: device hasn't been assigned before, "
674 "so cannot be deassigned\n", __func__);
675 r = -EINVAL;
676 goto out;
677 }
678
Weidong Han4a906e42009-02-13 17:27:51 +0800679 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800680 kvm_deassign_device(kvm, match);
681
682 kvm_free_assigned_device(kvm, match);
683
684out:
685 mutex_unlock(&kvm->lock);
686 return r;
687}
688#endif
689
James Morris5aacf0c2006-12-22 01:04:55 -0800690static inline int valid_vcpu(int n)
691{
692 return likely(n >= 0 && n < KVM_MAX_VCPUS);
693}
694
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800695inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300696{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100697 if (pfn_valid(pfn)) {
698 struct page *page = compound_head(pfn_to_page(pfn));
699 return PageReserved(page);
700 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300701
702 return true;
703}
704
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705/*
706 * Switches to specified vcpu, until a matching vcpu_put()
707 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200708void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709{
Avi Kivity15ad7142007-07-11 18:17:21 +0300710 int cpu;
711
Avi Kivitybccf2152007-02-21 18:04:26 +0200712 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300713 cpu = get_cpu();
714 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200715 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300716 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200717}
718
Carsten Otte313a3dc2007-10-11 19:16:52 +0200719void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720{
Avi Kivity15ad7142007-07-11 18:17:21 +0300721 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200722 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300723 preempt_notifier_unregister(&vcpu->preempt_notifier);
724 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 mutex_unlock(&vcpu->mutex);
726}
727
Avi Kivityd9e368d2007-06-07 19:18:30 +0300728static void ack_flush(void *_completed)
729{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300730}
731
Rusty Russell49846892008-12-08 20:26:24 +1030732static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300733{
Avi Kivity597a5f52008-07-20 14:24:22 +0300734 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030735 cpumask_var_t cpus;
736 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300737 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300738
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030739 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
740 cpumask_clear(cpus);
741
Avi Kivity597a5f52008-07-20 14:24:22 +0300742 me = get_cpu();
Marcelo Tosatti84261922009-06-17 10:53:47 -0300743 spin_lock(&kvm->requests_lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000744 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
745 vcpu = kvm->vcpus[i];
746 if (!vcpu)
747 continue;
Rusty Russell49846892008-12-08 20:26:24 +1030748 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300749 continue;
750 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030751 if (cpus != NULL && cpu != -1 && cpu != me)
752 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300753 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030754 if (unlikely(cpus == NULL))
755 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
756 else if (!cpumask_empty(cpus))
757 smp_call_function_many(cpus, ack_flush, NULL, 1);
758 else
759 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300760 spin_unlock(&kvm->requests_lock);
Avi Kivity597a5f52008-07-20 14:24:22 +0300761 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030762 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030763 return called;
764}
765
766void kvm_flush_remote_tlbs(struct kvm *kvm)
767{
768 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
769 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300770}
771
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500772void kvm_reload_remote_mmus(struct kvm *kvm)
773{
Rusty Russell49846892008-12-08 20:26:24 +1030774 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500775}
776
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000777int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
778{
779 struct page *page;
780 int r;
781
782 mutex_init(&vcpu->mutex);
783 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000784 vcpu->kvm = kvm;
785 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300786 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000787
788 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
789 if (!page) {
790 r = -ENOMEM;
791 goto fail;
792 }
793 vcpu->run = page_address(page);
794
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800795 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000796 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800797 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000798 return 0;
799
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000800fail_free_run:
801 free_page((unsigned long)vcpu->run);
802fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000803 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000804}
805EXPORT_SYMBOL_GPL(kvm_vcpu_init);
806
807void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
808{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800809 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000810 free_page((unsigned long)vcpu->run);
811}
812EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
813
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200814#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
815static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
816{
817 return container_of(mn, struct kvm, mmu_notifier);
818}
819
820static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
821 struct mm_struct *mm,
822 unsigned long address)
823{
824 struct kvm *kvm = mmu_notifier_to_kvm(mn);
825 int need_tlb_flush;
826
827 /*
828 * When ->invalidate_page runs, the linux pte has been zapped
829 * already but the page is still allocated until
830 * ->invalidate_page returns. So if we increase the sequence
831 * here the kvm page fault will notice if the spte can't be
832 * established because the page is going to be freed. If
833 * instead the kvm page fault establishes the spte before
834 * ->invalidate_page runs, kvm_unmap_hva will release it
835 * before returning.
836 *
837 * The sequence increase only need to be seen at spin_unlock
838 * time, and not at spin_lock time.
839 *
840 * Increasing the sequence after the spin_unlock would be
841 * unsafe because the kvm page fault could then establish the
842 * pte after kvm_unmap_hva returned, without noticing the page
843 * is going to be freed.
844 */
845 spin_lock(&kvm->mmu_lock);
846 kvm->mmu_notifier_seq++;
847 need_tlb_flush = kvm_unmap_hva(kvm, address);
848 spin_unlock(&kvm->mmu_lock);
849
850 /* we've to flush the tlb before the pages can be freed */
851 if (need_tlb_flush)
852 kvm_flush_remote_tlbs(kvm);
853
854}
855
856static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
857 struct mm_struct *mm,
858 unsigned long start,
859 unsigned long end)
860{
861 struct kvm *kvm = mmu_notifier_to_kvm(mn);
862 int need_tlb_flush = 0;
863
864 spin_lock(&kvm->mmu_lock);
865 /*
866 * The count increase must become visible at unlock time as no
867 * spte can be established without taking the mmu_lock and
868 * count is also read inside the mmu_lock critical section.
869 */
870 kvm->mmu_notifier_count++;
871 for (; start < end; start += PAGE_SIZE)
872 need_tlb_flush |= kvm_unmap_hva(kvm, start);
873 spin_unlock(&kvm->mmu_lock);
874
875 /* we've to flush the tlb before the pages can be freed */
876 if (need_tlb_flush)
877 kvm_flush_remote_tlbs(kvm);
878}
879
880static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
881 struct mm_struct *mm,
882 unsigned long start,
883 unsigned long end)
884{
885 struct kvm *kvm = mmu_notifier_to_kvm(mn);
886
887 spin_lock(&kvm->mmu_lock);
888 /*
889 * This sequence increase will notify the kvm page fault that
890 * the page that is going to be mapped in the spte could have
891 * been freed.
892 */
893 kvm->mmu_notifier_seq++;
894 /*
895 * The above sequence increase must be visible before the
896 * below count decrease but both values are read by the kvm
897 * page fault under mmu_lock spinlock so we don't need to add
898 * a smb_wmb() here in between the two.
899 */
900 kvm->mmu_notifier_count--;
901 spin_unlock(&kvm->mmu_lock);
902
903 BUG_ON(kvm->mmu_notifier_count < 0);
904}
905
906static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
907 struct mm_struct *mm,
908 unsigned long address)
909{
910 struct kvm *kvm = mmu_notifier_to_kvm(mn);
911 int young;
912
913 spin_lock(&kvm->mmu_lock);
914 young = kvm_age_hva(kvm, address);
915 spin_unlock(&kvm->mmu_lock);
916
917 if (young)
918 kvm_flush_remote_tlbs(kvm);
919
920 return young;
921}
922
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100923static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
924 struct mm_struct *mm)
925{
926 struct kvm *kvm = mmu_notifier_to_kvm(mn);
927 kvm_arch_flush_shadow(kvm);
928}
929
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200930static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
931 .invalidate_page = kvm_mmu_notifier_invalidate_page,
932 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
933 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
934 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100935 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200936};
937#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
938
Avi Kivityf17abe92007-02-21 19:28:04 +0200939static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800941 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200942#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
943 struct page *page;
944#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800946 if (IS_ERR(kvm))
947 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200948#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200949 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200950 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
951#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952
Laurent Vivier5f94c172008-05-30 16:05:54 +0200953#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
954 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
955 if (!page) {
956 kfree(kvm);
957 return ERR_PTR(-ENOMEM);
958 }
959 kvm->coalesced_mmio_ring =
960 (struct kvm_coalesced_mmio_ring *)page_address(page);
961#endif
962
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200963#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
964 {
965 int err;
966 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
967 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
968 if (err) {
969#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
970 put_page(page);
971#endif
972 kfree(kvm);
973 return ERR_PTR(err);
974 }
975 }
976#endif
977
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200978 kvm->mm = current->mm;
979 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500980 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300981 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300982 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskins721eecb2009-05-20 10:30:49 -0400983 kvm_irqfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800984 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300985 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400986 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200987 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300988 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000989 spin_lock(&kvm_lock);
990 list_add(&kvm->vm_list, &vm_list);
991 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200992#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
993 kvm_coalesced_mmio_init(kvm);
994#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800995out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200996 return kvm;
997}
998
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999/*
1000 * Free any memory in @free but not in @dont.
1001 */
1002static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1003 struct kvm_memory_slot *dont)
1004{
Izik Eidus290fc382007-09-27 14:11:22 +02001005 if (!dont || free->rmap != dont->rmap)
1006 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007
1008 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1009 vfree(free->dirty_bitmap);
1010
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001011 if (!dont || free->lpage_info != dont->lpage_info)
1012 vfree(free->lpage_info);
1013
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001015 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001016 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001017 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018}
1019
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001020void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021{
1022 int i;
1023
1024 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001025 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026}
1027
Avi Kivityf17abe92007-02-21 19:28:04 +02001028static void kvm_destroy_vm(struct kvm *kvm)
1029{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001030 struct mm_struct *mm = kvm->mm;
1031
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001032 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001033 spin_lock(&kvm_lock);
1034 list_del(&kvm->vm_list);
1035 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001036 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001037 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001038 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001039#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1040 if (kvm->coalesced_mmio_ring != NULL)
1041 free_page((unsigned long)kvm->coalesced_mmio_ring);
1042#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001043#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1044 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +02001045#else
1046 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001047#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001048 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001049 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001050}
1051
Izik Eidusd39f13b2008-03-30 16:01:25 +03001052void kvm_get_kvm(struct kvm *kvm)
1053{
1054 atomic_inc(&kvm->users_count);
1055}
1056EXPORT_SYMBOL_GPL(kvm_get_kvm);
1057
1058void kvm_put_kvm(struct kvm *kvm)
1059{
1060 if (atomic_dec_and_test(&kvm->users_count))
1061 kvm_destroy_vm(kvm);
1062}
1063EXPORT_SYMBOL_GPL(kvm_put_kvm);
1064
1065
Avi Kivityf17abe92007-02-21 19:28:04 +02001066static int kvm_vm_release(struct inode *inode, struct file *filp)
1067{
1068 struct kvm *kvm = filp->private_data;
1069
Gregory Haskins721eecb2009-05-20 10:30:49 -04001070 kvm_irqfd_release(kvm);
1071
Izik Eidusd39f13b2008-03-30 16:01:25 +03001072 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 return 0;
1074}
1075
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 * Allocate some memory and give it an address in the guest physical address
1078 * space.
1079 *
1080 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001081 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001082 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001084int __kvm_set_memory_region(struct kvm *kvm,
1085 struct kvm_userspace_memory_region *mem,
1086 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087{
1088 int r;
1089 gfn_t base_gfn;
Avi Kivityac045272009-06-08 15:52:39 +03001090 unsigned long npages, ugfn;
Avi Kivity09f8ca72009-06-08 15:55:21 +03001091 unsigned long largepages, i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 struct kvm_memory_slot *memslot;
1093 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094
1095 r = -EINVAL;
1096 /* General sanity checks */
1097 if (mem->memory_size & (PAGE_SIZE - 1))
1098 goto out;
1099 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1100 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001101 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001102 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001103 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 goto out;
1105 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1106 goto out;
1107
1108 memslot = &kvm->memslots[mem->slot];
1109 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1110 npages = mem->memory_size >> PAGE_SHIFT;
1111
1112 if (!npages)
1113 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1114
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 new = old = *memslot;
1116
1117 new.base_gfn = base_gfn;
1118 new.npages = npages;
1119 new.flags = mem->flags;
1120
1121 /* Disallow changing a memory slot's size. */
1122 r = -EINVAL;
1123 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001124 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125
1126 /* Check for overlaps */
1127 r = -EEXIST;
1128 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1129 struct kvm_memory_slot *s = &kvm->memslots[i];
1130
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001131 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132 continue;
1133 if (!((base_gfn + npages <= s->base_gfn) ||
1134 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001135 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 /* Free page dirty bitmap if unneeded */
1139 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001140 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
1142 r = -ENOMEM;
1143
1144 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001145#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001146 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001147 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001148
1149 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001150 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001151
Izik Eidus290fc382007-09-27 14:11:22 +02001152 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001153
Izik Eidus80b14b52007-10-25 11:54:04 +02001154 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001155 /*
1156 * hva_to_rmmap() serialzies with the mmu_lock and to be
1157 * safe it has to ignore memslots with !user_alloc &&
1158 * !userspace_addr.
1159 */
1160 if (user_alloc)
1161 new.userspace_addr = mem->userspace_addr;
1162 else
1163 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001165 if (npages && !new.lpage_info) {
Avi Kivity99894a72009-03-29 16:31:25 +03001166 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1167 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001168
1169 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1170
1171 if (!new.lpage_info)
1172 goto out_free;
1173
1174 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1175
1176 if (base_gfn % KVM_PAGES_PER_HPAGE)
1177 new.lpage_info[0].write_count = 1;
1178 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1179 new.lpage_info[largepages-1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +03001180 ugfn = new.userspace_addr >> PAGE_SHIFT;
1181 /*
1182 * If the gfn and userspace address are not aligned wrt each
1183 * other, disable large page support for this slot
1184 */
1185 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1))
1186 for (i = 0; i < largepages; ++i)
1187 new.lpage_info[i].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001188 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189
1190 /* Allocate page dirty bitmap if needed */
1191 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1192 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1193
1194 new.dirty_bitmap = vmalloc(dirty_bytes);
1195 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001196 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +03001198 if (old.npages)
1199 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200 }
Carsten Otteeff01142008-06-27 15:05:31 +02001201#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001203 if (!npages)
1204 kvm_arch_flush_shadow(kvm);
1205
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001206 spin_lock(&kvm->mmu_lock);
1207 if (mem->slot >= kvm->nmemslots)
1208 kvm->nmemslots = mem->slot + 1;
1209
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001211 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212
Zhang Xiantao0de10342007-11-20 16:25:04 +08001213 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1214 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001215 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001216 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001217 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001218 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001219 }
1220
Glauber Costa6f897242008-12-03 13:40:51 -02001221 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1222 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001223 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -02001224 if (!npages)
1225 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001226 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001227#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001228 /* map the pages in iommu page table */
1229 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1230 if (r)
1231 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001232#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233 return 0;
1234
Sheng Yangf78e0e22007-10-29 09:40:42 +08001235out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 kvm_free_physmem_slot(&new, &old);
1237out:
1238 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001239
1240}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001241EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1242
1243int kvm_set_memory_region(struct kvm *kvm,
1244 struct kvm_userspace_memory_region *mem,
1245 int user_alloc)
1246{
1247 int r;
1248
Izik Eidus72dc67a2008-02-10 18:04:15 +02001249 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001250 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001251 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001252 return r;
1253}
Izik Eidus210c7c42007-10-24 23:52:57 +02001254EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1255
Carsten Otte1fe779f2007-10-29 16:08:35 +01001256int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1257 struct
1258 kvm_userspace_memory_region *mem,
1259 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001260{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001261 if (mem->slot >= KVM_MEMORY_SLOTS)
1262 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001263 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264}
1265
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001266int kvm_get_dirty_log(struct kvm *kvm,
1267 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268{
1269 struct kvm_memory_slot *memslot;
1270 int r, i;
1271 int n;
1272 unsigned long any = 0;
1273
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274 r = -EINVAL;
1275 if (log->slot >= KVM_MEMORY_SLOTS)
1276 goto out;
1277
1278 memslot = &kvm->memslots[log->slot];
1279 r = -ENOENT;
1280 if (!memslot->dirty_bitmap)
1281 goto out;
1282
Uri Lublincd1a4a92007-02-22 16:43:09 +02001283 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284
Uri Lublincd1a4a92007-02-22 16:43:09 +02001285 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286 any = memslot->dirty_bitmap[i];
1287
1288 r = -EFAULT;
1289 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1290 goto out;
1291
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001292 if (any)
1293 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294
1295 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 return r;
1298}
1299
Izik Eiduscea7bb22007-10-17 19:17:48 +02001300int is_error_page(struct page *page)
1301{
1302 return page == bad_page;
1303}
1304EXPORT_SYMBOL_GPL(is_error_page);
1305
Anthony Liguori35149e22008-04-02 14:46:56 -05001306int is_error_pfn(pfn_t pfn)
1307{
1308 return pfn == bad_pfn;
1309}
1310EXPORT_SYMBOL_GPL(is_error_pfn);
1311
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001312static inline unsigned long bad_hva(void)
1313{
1314 return PAGE_OFFSET;
1315}
1316
1317int kvm_is_error_hva(unsigned long addr)
1318{
1319 return addr == bad_hva();
1320}
1321EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1322
Izik Eidus28430992008-10-03 17:40:32 +03001323struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324{
1325 int i;
1326
1327 for (i = 0; i < kvm->nmemslots; ++i) {
1328 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1329
1330 if (gfn >= memslot->base_gfn
1331 && gfn < memslot->base_gfn + memslot->npages)
1332 return memslot;
1333 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001334 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335}
Izik Eidus28430992008-10-03 17:40:32 +03001336EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001337
1338struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1339{
1340 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001341 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001342}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343
Izik Eiduse0d62c72007-10-24 23:57:46 +02001344int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1345{
1346 int i;
1347
1348 gfn = unalias_gfn(kvm, gfn);
1349 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1350 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1351
1352 if (gfn >= memslot->base_gfn
1353 && gfn < memslot->base_gfn + memslot->npages)
1354 return 1;
1355 }
1356 return 0;
1357}
1358EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1359
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001360unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001361{
1362 struct kvm_memory_slot *slot;
1363
1364 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001365 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001366 if (!slot)
1367 return bad_hva();
1368 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1369}
Sheng Yang0d150292008-04-25 21:44:50 +08001370EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001371
Anthony Liguori35149e22008-04-02 14:46:56 -05001372pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001373{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001374 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001375 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001376 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001377 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001378
Avi Kivity60395222007-10-21 11:03:36 +02001379 might_sleep();
1380
Izik Eidus539cb662007-11-11 22:05:04 +02001381 addr = gfn_to_hva(kvm, gfn);
1382 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001383 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001384 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001385 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001386
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001387 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001388
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001389 if (unlikely(npages != 1)) {
1390 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001391
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001392 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001393 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001394
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001395 if (vma == NULL || addr < vma->vm_start ||
1396 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001397 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001398 get_page(bad_page);
1399 return page_to_pfn(bad_page);
1400 }
1401
1402 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001403 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001404 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001405 } else
1406 pfn = page_to_pfn(page[0]);
1407
1408 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001409}
1410
1411EXPORT_SYMBOL_GPL(gfn_to_pfn);
1412
1413struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1414{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001415 pfn_t pfn;
1416
1417 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001418 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001419 return pfn_to_page(pfn);
1420
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001421 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001422
1423 get_page(bad_page);
1424 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001425}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001426
Avi Kivity954bbbc22007-03-30 14:02:32 +03001427EXPORT_SYMBOL_GPL(gfn_to_page);
1428
Izik Eidusb4231d62007-11-20 11:49:33 +02001429void kvm_release_page_clean(struct page *page)
1430{
Anthony Liguori35149e22008-04-02 14:46:56 -05001431 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001432}
1433EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1434
Anthony Liguori35149e22008-04-02 14:46:56 -05001435void kvm_release_pfn_clean(pfn_t pfn)
1436{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001437 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001438 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001439}
1440EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1441
Izik Eidusb4231d62007-11-20 11:49:33 +02001442void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001443{
Anthony Liguori35149e22008-04-02 14:46:56 -05001444 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001445}
Izik Eidusb4231d62007-11-20 11:49:33 +02001446EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001447
Anthony Liguori35149e22008-04-02 14:46:56 -05001448void kvm_release_pfn_dirty(pfn_t pfn)
1449{
1450 kvm_set_pfn_dirty(pfn);
1451 kvm_release_pfn_clean(pfn);
1452}
1453EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1454
1455void kvm_set_page_dirty(struct page *page)
1456{
1457 kvm_set_pfn_dirty(page_to_pfn(page));
1458}
1459EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1460
1461void kvm_set_pfn_dirty(pfn_t pfn)
1462{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001463 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001464 struct page *page = pfn_to_page(pfn);
1465 if (!PageReserved(page))
1466 SetPageDirty(page);
1467 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001468}
1469EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1470
1471void kvm_set_pfn_accessed(pfn_t pfn)
1472{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001473 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001474 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001475}
1476EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1477
1478void kvm_get_pfn(pfn_t pfn)
1479{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001480 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001481 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001482}
1483EXPORT_SYMBOL_GPL(kvm_get_pfn);
1484
Izik Eidus195aefd2007-10-01 22:14:18 +02001485static int next_segment(unsigned long len, int offset)
1486{
1487 if (len > PAGE_SIZE - offset)
1488 return PAGE_SIZE - offset;
1489 else
1490 return len;
1491}
1492
1493int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1494 int len)
1495{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001496 int r;
1497 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001498
Izik Eiduse0506bc2007-11-11 22:10:22 +02001499 addr = gfn_to_hva(kvm, gfn);
1500 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001501 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001502 r = copy_from_user(data, (void __user *)addr + offset, len);
1503 if (r)
1504 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001505 return 0;
1506}
1507EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1508
1509int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1510{
1511 gfn_t gfn = gpa >> PAGE_SHIFT;
1512 int seg;
1513 int offset = offset_in_page(gpa);
1514 int ret;
1515
1516 while ((seg = next_segment(len, offset)) != 0) {
1517 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1518 if (ret < 0)
1519 return ret;
1520 offset = 0;
1521 len -= seg;
1522 data += seg;
1523 ++gfn;
1524 }
1525 return 0;
1526}
1527EXPORT_SYMBOL_GPL(kvm_read_guest);
1528
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001529int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1530 unsigned long len)
1531{
1532 int r;
1533 unsigned long addr;
1534 gfn_t gfn = gpa >> PAGE_SHIFT;
1535 int offset = offset_in_page(gpa);
1536
1537 addr = gfn_to_hva(kvm, gfn);
1538 if (kvm_is_error_hva(addr))
1539 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001540 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001541 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001542 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001543 if (r)
1544 return -EFAULT;
1545 return 0;
1546}
1547EXPORT_SYMBOL(kvm_read_guest_atomic);
1548
Izik Eidus195aefd2007-10-01 22:14:18 +02001549int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1550 int offset, int len)
1551{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001552 int r;
1553 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001554
Izik Eiduse0506bc2007-11-11 22:10:22 +02001555 addr = gfn_to_hva(kvm, gfn);
1556 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001557 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001558 r = copy_to_user((void __user *)addr + offset, data, len);
1559 if (r)
1560 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001561 mark_page_dirty(kvm, gfn);
1562 return 0;
1563}
1564EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1565
1566int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1567 unsigned long len)
1568{
1569 gfn_t gfn = gpa >> PAGE_SHIFT;
1570 int seg;
1571 int offset = offset_in_page(gpa);
1572 int ret;
1573
1574 while ((seg = next_segment(len, offset)) != 0) {
1575 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1576 if (ret < 0)
1577 return ret;
1578 offset = 0;
1579 len -= seg;
1580 data += seg;
1581 ++gfn;
1582 }
1583 return 0;
1584}
1585
1586int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1587{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001588 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001589}
1590EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1591
1592int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1593{
1594 gfn_t gfn = gpa >> PAGE_SHIFT;
1595 int seg;
1596 int offset = offset_in_page(gpa);
1597 int ret;
1598
1599 while ((seg = next_segment(len, offset)) != 0) {
1600 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1601 if (ret < 0)
1602 return ret;
1603 offset = 0;
1604 len -= seg;
1605 ++gfn;
1606 }
1607 return 0;
1608}
1609EXPORT_SYMBOL_GPL(kvm_clear_guest);
1610
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1612{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001613 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614
Uri Lublin3b6fff12007-10-30 10:42:09 +02001615 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001616 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001617 if (memslot && memslot->dirty_bitmap) {
1618 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619
Rusty Russell7e9d6192007-07-31 20:41:14 +10001620 /* avoid RMW */
1621 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1622 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 }
1624}
1625
Eddie Dongb6958ce2007-07-18 12:15:21 +03001626/*
1627 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1628 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001629void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001630{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001631 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001632
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001633 for (;;) {
1634 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001635
Gleb Natapov78646122009-03-23 12:12:11 +02001636 if ((kvm_arch_interrupt_allowed(vcpu) &&
1637 kvm_cpu_has_interrupt(vcpu)) ||
Gleb Natapov09cec752009-03-23 15:11:44 +02001638 kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001639 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001640 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001641 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001642 if (kvm_cpu_has_pending_timer(vcpu))
1643 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001644 if (signal_pending(current))
1645 break;
1646
Eddie Dongb6958ce2007-07-18 12:15:21 +03001647 vcpu_put(vcpu);
1648 schedule();
1649 vcpu_load(vcpu);
1650 }
1651
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001652 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001653}
1654
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655void kvm_resched(struct kvm_vcpu *vcpu)
1656{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001657 if (!need_resched())
1658 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660}
1661EXPORT_SYMBOL_GPL(kvm_resched);
1662
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001663static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001664{
1665 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001666 struct page *page;
1667
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001668 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001669 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001670#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001671 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001672 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001673#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001674#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1675 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1676 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1677#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001678 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001679 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001680 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001681 vmf->page = page;
1682 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001683}
1684
1685static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001686 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001687};
1688
1689static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1690{
1691 vma->vm_ops = &kvm_vcpu_vm_ops;
1692 return 0;
1693}
1694
Avi Kivitybccf2152007-02-21 18:04:26 +02001695static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1696{
1697 struct kvm_vcpu *vcpu = filp->private_data;
1698
Al Viro66c0b392008-04-19 20:33:56 +01001699 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001700 return 0;
1701}
1702
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001703static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001704 .release = kvm_vcpu_release,
1705 .unlocked_ioctl = kvm_vcpu_ioctl,
1706 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001707 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001708};
1709
1710/*
1711 * Allocates an inode for the vcpu.
1712 */
1713static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1714{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001715 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001716 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001717 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001718 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001719}
1720
Avi Kivityc5ea7662007-02-20 18:41:05 +02001721/*
1722 * Creates some virtual cpus. Good luck creating more than one.
1723 */
1724static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1725{
1726 int r;
1727 struct kvm_vcpu *vcpu;
1728
Avi Kivityc5ea7662007-02-20 18:41:05 +02001729 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001730 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001731
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001732 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001733 if (IS_ERR(vcpu))
1734 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001735
Avi Kivity15ad7142007-07-11 18:17:21 +03001736 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1737
Avi Kivity26e52152007-11-20 15:30:24 +02001738 r = kvm_arch_vcpu_setup(vcpu);
1739 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001740 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001741
Shaohua Li11ec2802007-07-23 14:51:37 +08001742 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001743 if (kvm->vcpus[n]) {
1744 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001745 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001746 }
1747 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001748 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001749
1750 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001751 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001752 r = create_vcpu_fd(vcpu);
1753 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001754 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001755 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001756
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001757unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001758 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001759 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001760vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001761 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001762 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001763 return r;
1764}
1765
Avi Kivity1961d272007-03-05 19:46:05 +02001766static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1767{
1768 if (sigset) {
1769 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1770 vcpu->sigset_active = 1;
1771 vcpu->sigset = *sigset;
1772 } else
1773 vcpu->sigset_active = 0;
1774 return 0;
1775}
1776
Sheng Yangc1e01512009-02-25 17:22:26 +08001777#ifdef __KVM_HAVE_MSIX
1778static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1779 struct kvm_assigned_msix_nr *entry_nr)
1780{
1781 int r = 0;
1782 struct kvm_assigned_dev_kernel *adev;
1783
1784 mutex_lock(&kvm->lock);
1785
1786 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1787 entry_nr->assigned_dev_id);
1788 if (!adev) {
1789 r = -EINVAL;
1790 goto msix_nr_out;
1791 }
1792
1793 if (adev->entries_nr == 0) {
1794 adev->entries_nr = entry_nr->entry_nr;
1795 if (adev->entries_nr == 0 ||
1796 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1797 r = -EINVAL;
1798 goto msix_nr_out;
1799 }
1800
1801 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1802 entry_nr->entry_nr,
1803 GFP_KERNEL);
1804 if (!adev->host_msix_entries) {
1805 r = -ENOMEM;
1806 goto msix_nr_out;
1807 }
1808 adev->guest_msix_entries = kzalloc(
1809 sizeof(struct kvm_guest_msix_entry) *
1810 entry_nr->entry_nr, GFP_KERNEL);
1811 if (!adev->guest_msix_entries) {
1812 kfree(adev->host_msix_entries);
1813 r = -ENOMEM;
1814 goto msix_nr_out;
1815 }
1816 } else /* Not allowed set MSI-X number twice */
1817 r = -EINVAL;
1818msix_nr_out:
1819 mutex_unlock(&kvm->lock);
1820 return r;
1821}
1822
1823static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1824 struct kvm_assigned_msix_entry *entry)
1825{
1826 int r = 0, i;
1827 struct kvm_assigned_dev_kernel *adev;
1828
1829 mutex_lock(&kvm->lock);
1830
1831 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1832 entry->assigned_dev_id);
1833
1834 if (!adev) {
1835 r = -EINVAL;
1836 goto msix_entry_out;
1837 }
1838
1839 for (i = 0; i < adev->entries_nr; i++)
1840 if (adev->guest_msix_entries[i].vector == 0 ||
1841 adev->guest_msix_entries[i].entry == entry->entry) {
1842 adev->guest_msix_entries[i].entry = entry->entry;
1843 adev->guest_msix_entries[i].vector = entry->gsi;
1844 adev->host_msix_entries[i].entry = entry->entry;
1845 break;
1846 }
1847 if (i == adev->entries_nr) {
1848 r = -ENOSPC;
1849 goto msix_entry_out;
1850 }
1851
1852msix_entry_out:
1853 mutex_unlock(&kvm->lock);
1854
1855 return r;
1856}
1857#endif
1858
Avi Kivitybccf2152007-02-21 18:04:26 +02001859static long kvm_vcpu_ioctl(struct file *filp,
1860 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861{
Avi Kivitybccf2152007-02-21 18:04:26 +02001862 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001863 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001864 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001865 struct kvm_fpu *fpu = NULL;
1866 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001868 if (vcpu->kvm->mm != current->mm)
1869 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001871 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001872 r = -EINVAL;
1873 if (arg)
1874 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001875 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001878 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001880 r = -ENOMEM;
1881 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1882 if (!kvm_regs)
1883 goto out;
1884 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001886 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001888 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1889 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001891out_free1:
1892 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 break;
1894 }
1895 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001896 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001898 r = -ENOMEM;
1899 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1900 if (!kvm_regs)
1901 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001903 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1904 goto out_free2;
1905 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001907 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001909out_free2:
1910 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 break;
1912 }
1913 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001914 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1915 r = -ENOMEM;
1916 if (!kvm_sregs)
1917 goto out;
1918 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 if (r)
1920 goto out;
1921 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001922 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923 goto out;
1924 r = 0;
1925 break;
1926 }
1927 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001928 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1929 r = -ENOMEM;
1930 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001932 r = -EFAULT;
1933 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1934 goto out;
1935 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 if (r)
1937 goto out;
1938 r = 0;
1939 break;
1940 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001941 case KVM_GET_MP_STATE: {
1942 struct kvm_mp_state mp_state;
1943
1944 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1945 if (r)
1946 goto out;
1947 r = -EFAULT;
1948 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1949 goto out;
1950 r = 0;
1951 break;
1952 }
1953 case KVM_SET_MP_STATE: {
1954 struct kvm_mp_state mp_state;
1955
1956 r = -EFAULT;
1957 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1958 goto out;
1959 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1960 if (r)
1961 goto out;
1962 r = 0;
1963 break;
1964 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 case KVM_TRANSLATE: {
1966 struct kvm_translation tr;
1967
1968 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001969 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001971 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 if (r)
1973 goto out;
1974 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001975 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976 goto out;
1977 r = 0;
1978 break;
1979 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001980 case KVM_SET_GUEST_DEBUG: {
1981 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982
1983 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001984 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001986 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987 if (r)
1988 goto out;
1989 r = 0;
1990 break;
1991 }
Avi Kivity1961d272007-03-05 19:46:05 +02001992 case KVM_SET_SIGNAL_MASK: {
1993 struct kvm_signal_mask __user *sigmask_arg = argp;
1994 struct kvm_signal_mask kvm_sigmask;
1995 sigset_t sigset, *p;
1996
1997 p = NULL;
1998 if (argp) {
1999 r = -EFAULT;
2000 if (copy_from_user(&kvm_sigmask, argp,
2001 sizeof kvm_sigmask))
2002 goto out;
2003 r = -EINVAL;
2004 if (kvm_sigmask.len != sizeof sigset)
2005 goto out;
2006 r = -EFAULT;
2007 if (copy_from_user(&sigset, sigmask_arg->sigset,
2008 sizeof sigset))
2009 goto out;
2010 p = &sigset;
2011 }
2012 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2013 break;
2014 }
Avi Kivityb8836732007-04-01 16:34:31 +03002015 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002016 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2017 r = -ENOMEM;
2018 if (!fpu)
2019 goto out;
2020 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002021 if (r)
2022 goto out;
2023 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002024 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03002025 goto out;
2026 r = 0;
2027 break;
2028 }
2029 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002030 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2031 r = -ENOMEM;
2032 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002033 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002034 r = -EFAULT;
2035 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2036 goto out;
2037 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002038 if (r)
2039 goto out;
2040 r = 0;
2041 break;
2042 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002043 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002044 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002045 }
2046out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002047 kfree(fpu);
2048 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002049 return r;
2050}
2051
2052static long kvm_vm_ioctl(struct file *filp,
2053 unsigned int ioctl, unsigned long arg)
2054{
2055 struct kvm *kvm = filp->private_data;
2056 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002057 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002058
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002059 if (kvm->mm != current->mm)
2060 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002061 switch (ioctl) {
2062 case KVM_CREATE_VCPU:
2063 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2064 if (r < 0)
2065 goto out;
2066 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002067 case KVM_SET_USER_MEMORY_REGION: {
2068 struct kvm_userspace_memory_region kvm_userspace_mem;
2069
2070 r = -EFAULT;
2071 if (copy_from_user(&kvm_userspace_mem, argp,
2072 sizeof kvm_userspace_mem))
2073 goto out;
2074
2075 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076 if (r)
2077 goto out;
2078 break;
2079 }
2080 case KVM_GET_DIRTY_LOG: {
2081 struct kvm_dirty_log log;
2082
2083 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002084 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002086 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 if (r)
2088 goto out;
2089 break;
2090 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002091#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2092 case KVM_REGISTER_COALESCED_MMIO: {
2093 struct kvm_coalesced_mmio_zone zone;
2094 r = -EFAULT;
2095 if (copy_from_user(&zone, argp, sizeof zone))
2096 goto out;
2097 r = -ENXIO;
2098 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2099 if (r)
2100 goto out;
2101 r = 0;
2102 break;
2103 }
2104 case KVM_UNREGISTER_COALESCED_MMIO: {
2105 struct kvm_coalesced_mmio_zone zone;
2106 r = -EFAULT;
2107 if (copy_from_user(&zone, argp, sizeof zone))
2108 goto out;
2109 r = -ENXIO;
2110 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2111 if (r)
2112 goto out;
2113 r = 0;
2114 break;
2115 }
2116#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002117#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2118 case KVM_ASSIGN_PCI_DEVICE: {
2119 struct kvm_assigned_pci_dev assigned_dev;
2120
2121 r = -EFAULT;
2122 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2123 goto out;
2124 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2125 if (r)
2126 goto out;
2127 break;
2128 }
2129 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002130 r = -EOPNOTSUPP;
2131 break;
2132 }
2133#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2134 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002135 struct kvm_assigned_irq assigned_irq;
2136
2137 r = -EFAULT;
2138 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2139 goto out;
2140 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2141 if (r)
2142 goto out;
2143 break;
2144 }
Sheng Yange56d5322009-03-12 21:45:39 +08002145 case KVM_DEASSIGN_DEV_IRQ: {
2146 struct kvm_assigned_irq assigned_irq;
2147
2148 r = -EFAULT;
2149 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2150 goto out;
2151 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2152 if (r)
2153 goto out;
2154 break;
2155 }
2156#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002157#endif
Weidong Han0a920352008-12-02 21:24:23 +08002158#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2159 case KVM_DEASSIGN_PCI_DEVICE: {
2160 struct kvm_assigned_pci_dev assigned_dev;
2161
2162 r = -EFAULT;
2163 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2164 goto out;
2165 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2166 if (r)
2167 goto out;
2168 break;
2169 }
2170#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002171#ifdef KVM_CAP_IRQ_ROUTING
2172 case KVM_SET_GSI_ROUTING: {
2173 struct kvm_irq_routing routing;
2174 struct kvm_irq_routing __user *urouting;
2175 struct kvm_irq_routing_entry *entries;
2176
2177 r = -EFAULT;
2178 if (copy_from_user(&routing, argp, sizeof(routing)))
2179 goto out;
2180 r = -EINVAL;
2181 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2182 goto out;
2183 if (routing.flags)
2184 goto out;
2185 r = -ENOMEM;
2186 entries = vmalloc(routing.nr * sizeof(*entries));
2187 if (!entries)
2188 goto out;
2189 r = -EFAULT;
2190 urouting = argp;
2191 if (copy_from_user(entries, urouting->entries,
2192 routing.nr * sizeof(*entries)))
2193 goto out_free_irq_routing;
2194 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2195 routing.flags);
2196 out_free_irq_routing:
2197 vfree(entries);
2198 break;
2199 }
Sheng Yangc1e01512009-02-25 17:22:26 +08002200#ifdef __KVM_HAVE_MSIX
2201 case KVM_ASSIGN_SET_MSIX_NR: {
2202 struct kvm_assigned_msix_nr entry_nr;
2203 r = -EFAULT;
2204 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2205 goto out;
2206 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2207 if (r)
2208 goto out;
2209 break;
2210 }
2211 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2212 struct kvm_assigned_msix_entry entry;
2213 r = -EFAULT;
2214 if (copy_from_user(&entry, argp, sizeof entry))
2215 goto out;
2216 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2217 if (r)
2218 goto out;
2219 break;
2220 }
Avi Kivity399ec802008-11-19 13:58:46 +02002221#endif
Sheng Yangc1e01512009-02-25 17:22:26 +08002222#endif /* KVM_CAP_IRQ_ROUTING */
Gregory Haskins721eecb2009-05-20 10:30:49 -04002223 case KVM_IRQFD: {
2224 struct kvm_irqfd data;
2225
2226 r = -EFAULT;
2227 if (copy_from_user(&data, argp, sizeof data))
2228 goto out;
2229 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2230 break;
2231 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002232 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002233 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002234 }
2235out:
2236 return r;
2237}
2238
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002239static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002240{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002241 struct page *page[1];
2242 unsigned long addr;
2243 int npages;
2244 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002245 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002246
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002247 addr = gfn_to_hva(kvm, gfn);
2248 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002249 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002250
2251 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2252 NULL);
2253 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002254 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002255
2256 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002257 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002258}
2259
2260static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002261 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002262};
2263
2264static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2265{
2266 vma->vm_ops = &kvm_vm_vm_ops;
2267 return 0;
2268}
2269
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002270static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002271 .release = kvm_vm_release,
2272 .unlocked_ioctl = kvm_vm_ioctl,
2273 .compat_ioctl = kvm_vm_ioctl,
2274 .mmap = kvm_vm_mmap,
2275};
2276
2277static int kvm_dev_ioctl_create_vm(void)
2278{
Al Viro2030a422008-02-23 06:46:49 -05002279 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002280 struct kvm *kvm;
2281
Avi Kivityf17abe92007-02-21 19:28:04 +02002282 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002283 if (IS_ERR(kvm))
2284 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002285 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002286 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002287 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002288
Avi Kivityf17abe92007-02-21 19:28:04 +02002289 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002290}
2291
Avi Kivity1a811b62008-12-08 18:25:27 +02002292static long kvm_dev_ioctl_check_extension_generic(long arg)
2293{
2294 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002295 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002296 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002297 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Avi Kivity1a811b62008-12-08 18:25:27 +02002298 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002299#ifdef CONFIG_HAVE_KVM_IRQCHIP
2300 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002301 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002302#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002303 default:
2304 break;
2305 }
2306 return kvm_dev_ioctl_check_extension(arg);
2307}
2308
Avi Kivityf17abe92007-02-21 19:28:04 +02002309static long kvm_dev_ioctl(struct file *filp,
2310 unsigned int ioctl, unsigned long arg)
2311{
Avi Kivity07c45a32007-03-07 13:05:38 +02002312 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002313
2314 switch (ioctl) {
2315 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002316 r = -EINVAL;
2317 if (arg)
2318 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002319 r = KVM_API_VERSION;
2320 break;
2321 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002322 r = -EINVAL;
2323 if (arg)
2324 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002325 r = kvm_dev_ioctl_create_vm();
2326 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002327 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002328 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002329 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002330 case KVM_GET_VCPU_MMAP_SIZE:
2331 r = -EINVAL;
2332 if (arg)
2333 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002334 r = PAGE_SIZE; /* struct kvm_run */
2335#ifdef CONFIG_X86
2336 r += PAGE_SIZE; /* pio data page */
2337#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002338#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2339 r += PAGE_SIZE; /* coalesced mmio ring page */
2340#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002341 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002342 case KVM_TRACE_ENABLE:
2343 case KVM_TRACE_PAUSE:
2344 case KVM_TRACE_DISABLE:
2345 r = kvm_trace_ioctl(ioctl, arg);
2346 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002348 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 }
2350out:
2351 return r;
2352}
2353
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 .unlocked_ioctl = kvm_dev_ioctl,
2356 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357};
2358
2359static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002360 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361 "kvm",
2362 &kvm_chardev_ops,
2363};
2364
Avi Kivity1b6c0162007-05-24 13:03:52 +03002365static void hardware_enable(void *junk)
2366{
2367 int cpu = raw_smp_processor_id();
2368
Rusty Russell7f59f492008-12-07 21:25:45 +10302369 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002370 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302371 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002372 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002373}
2374
2375static void hardware_disable(void *junk)
2376{
2377 int cpu = raw_smp_processor_id();
2378
Rusty Russell7f59f492008-12-07 21:25:45 +10302379 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002380 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302381 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002382 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002383}
2384
Avi Kivity774c47f2007-02-12 00:54:47 -08002385static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2386 void *v)
2387{
2388 int cpu = (long)v;
2389
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002390 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002391 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002392 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002393 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2394 cpu);
2395 hardware_disable(NULL);
2396 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002397 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002398 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2399 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002400 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002401 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002402 case CPU_ONLINE:
2403 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2404 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002405 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002406 break;
2407 }
2408 return NOTIFY_OK;
2409}
2410
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002411
2412asmlinkage void kvm_handle_fault_on_reboot(void)
2413{
2414 if (kvm_rebooting)
2415 /* spin while reset goes on */
2416 while (true)
2417 ;
2418 /* Fault while not rebooting. We want the trace. */
2419 BUG();
2420}
2421EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2422
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002423static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002424 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002425{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002426 /*
2427 * Some (well, at least mine) BIOSes hang on reboot if
2428 * in vmx root mode.
2429 *
2430 * And Intel TXT required VMX off for all cpu when system shutdown.
2431 */
2432 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2433 kvm_rebooting = true;
2434 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002435 return NOTIFY_OK;
2436}
2437
2438static struct notifier_block kvm_reboot_notifier = {
2439 .notifier_call = kvm_reboot,
2440 .priority = 0,
2441};
2442
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002443void kvm_io_bus_init(struct kvm_io_bus *bus)
2444{
2445 memset(bus, 0, sizeof(*bus));
2446}
2447
2448void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2449{
2450 int i;
2451
2452 for (i = 0; i < bus->dev_count; i++) {
2453 struct kvm_io_device *pos = bus->devs[i];
2454
2455 kvm_iodevice_destructor(pos);
2456 }
2457}
2458
Laurent Vivier92760492008-05-30 16:05:53 +02002459struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2460 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002461{
2462 int i;
2463
2464 for (i = 0; i < bus->dev_count; i++) {
2465 struct kvm_io_device *pos = bus->devs[i];
2466
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002467 if (kvm_iodevice_in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002468 return pos;
2469 }
2470
2471 return NULL;
2472}
2473
2474void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2475{
2476 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2477
2478 bus->devs[bus->dev_count++] = dev;
2479}
2480
Avi Kivity774c47f2007-02-12 00:54:47 -08002481static struct notifier_block kvm_cpu_notifier = {
2482 .notifier_call = kvm_cpu_hotplug,
2483 .priority = 20, /* must be > scheduler priority */
2484};
2485
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002486static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002487{
2488 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002489 struct kvm *kvm;
2490
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002491 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002492 spin_lock(&kvm_lock);
2493 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002494 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002495 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002496 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002497}
2498
2499DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2500
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002501static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002502{
2503 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002504 struct kvm *kvm;
2505 struct kvm_vcpu *vcpu;
2506 int i;
2507
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002508 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002509 spin_lock(&kvm_lock);
2510 list_for_each_entry(kvm, &vm_list, vm_list)
2511 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002512 vcpu = kvm->vcpus[i];
2513 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002514 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002515 }
2516 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002517 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002518}
2519
Avi Kivityba1389b2007-11-18 16:24:12 +02002520DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2521
2522static struct file_operations *stat_fops[] = {
2523 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2524 [KVM_STAT_VM] = &vm_stat_fops,
2525};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002526
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002527static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528{
2529 struct kvm_stats_debugfs_item *p;
2530
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002531 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002533 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002534 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002535 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536}
2537
2538static void kvm_exit_debug(void)
2539{
2540 struct kvm_stats_debugfs_item *p;
2541
2542 for (p = debugfs_entries; p->name; ++p)
2543 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002544 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545}
2546
Avi Kivity59ae6c62007-02-12 00:54:48 -08002547static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2548{
Avi Kivity4267c412007-05-24 13:09:41 +03002549 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002550 return 0;
2551}
2552
2553static int kvm_resume(struct sys_device *dev)
2554{
Avi Kivity4267c412007-05-24 13:09:41 +03002555 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002556 return 0;
2557}
2558
2559static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002560 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002561 .suspend = kvm_suspend,
2562 .resume = kvm_resume,
2563};
2564
2565static struct sys_device kvm_sysdev = {
2566 .id = 0,
2567 .cls = &kvm_sysdev_class,
2568};
2569
Izik Eiduscea7bb22007-10-17 19:17:48 +02002570struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002571pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572
Avi Kivity15ad7142007-07-11 18:17:21 +03002573static inline
2574struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2575{
2576 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2577}
2578
2579static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2580{
2581 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2582
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002583 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002584}
2585
2586static void kvm_sched_out(struct preempt_notifier *pn,
2587 struct task_struct *next)
2588{
2589 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2590
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002591 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002592}
2593
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002594int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002595 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596{
2597 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002598 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002600 kvm_init_debug();
2601
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002602 r = kvm_arch_init(opaque);
2603 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002604 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002605
2606 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2607
2608 if (bad_page == NULL) {
2609 r = -ENOMEM;
2610 goto out;
2611 }
2612
Anthony Liguori35149e22008-04-02 14:46:56 -05002613 bad_pfn = page_to_pfn(bad_page);
2614
Avi Kivity8437a612009-06-06 14:52:35 -07002615 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302616 r = -ENOMEM;
2617 goto out_free_0;
2618 }
2619
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002620 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302622 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623
Yang, Sheng002c7f72007-07-31 14:23:01 +03002624 for_each_online_cpu(cpu) {
2625 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002626 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002627 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002628 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002629 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002630 }
2631
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002632 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002633 r = register_cpu_notifier(&kvm_cpu_notifier);
2634 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002635 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 register_reboot_notifier(&kvm_reboot_notifier);
2637
Avi Kivity59ae6c62007-02-12 00:54:48 -08002638 r = sysdev_class_register(&kvm_sysdev_class);
2639 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002640 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002641
2642 r = sysdev_register(&kvm_sysdev);
2643 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002644 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002645
Rusty Russellc16f8622007-07-30 21:12:19 +10002646 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2647 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002648 __alignof__(struct kvm_vcpu),
2649 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002650 if (!kvm_vcpu_cache) {
2651 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002652 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002653 }
2654
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002656 kvm_vm_fops.owner = module;
2657 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658
2659 r = misc_register(&kvm_dev);
2660 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002661 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 goto out_free;
2663 }
2664
Avi Kivity15ad7142007-07-11 18:17:21 +03002665 kvm_preempt_ops.sched_in = kvm_sched_in;
2666 kvm_preempt_ops.sched_out = kvm_sched_out;
2667
Avi Kivityc7addb92007-09-16 18:58:32 +02002668 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669
2670out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002671 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002672out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002673 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002674out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002675 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002676out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002678 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002679out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002680 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002681out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002682 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302683out_free_0a:
2684 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002685out_free_0:
2686 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002687out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002688 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002689 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002690out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 return r;
2692}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002693EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002695void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002697 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002699 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002700 sysdev_unregister(&kvm_sysdev);
2701 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002703 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002704 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002705 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002706 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302708 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002709 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002711EXPORT_SYMBOL_GPL(kvm_exit);