blob: dee321e5889499403be2b17e011987eaddefa861 [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;
123 int irq, 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);
146 irq = assigned_dev->host_msix_entries[i].vector;
147 if (irq != 0)
148 enable_irq(irq);
149 assigned_dev->host_irq_disabled = false;
150 }
151 } else {
152 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
153 assigned_dev->guest_irq, 1);
154 if (assigned_dev->irq_requested_type &
Sheng Yange56d5322009-03-12 21:45:39 +0800155 KVM_DEV_IRQ_GUEST_MSI) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800156 enable_irq(assigned_dev->host_irq);
157 assigned_dev->host_irq_disabled = false;
158 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800159 }
Sheng Yang2350bd12009-02-25 17:22:27 +0800160
Marcelo Tosatti547de292009-05-07 17:55:13 -0300161 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800162 mutex_unlock(&assigned_dev->kvm->lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800163}
164
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800165static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
166{
Marcelo Tosatti547de292009-05-07 17:55:13 -0300167 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800168 struct kvm_assigned_dev_kernel *assigned_dev =
169 (struct kvm_assigned_dev_kernel *) dev_id;
170
Marcelo Tosatti547de292009-05-07 17:55:13 -0300171 spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
Sheng Yange56d5322009-03-12 21:45:39 +0800172 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800173 int index = find_index_from_host_irq(assigned_dev, irq);
174 if (index < 0)
Marcelo Tosatti547de292009-05-07 17:55:13 -0300175 goto out;
Sheng Yang2350bd12009-02-25 17:22:27 +0800176 assigned_dev->guest_msix_entries[index].flags |=
177 KVM_ASSIGNED_MSIX_PENDING;
178 }
179
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800180 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000181
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800182 disable_irq_nosync(irq);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000183 assigned_dev->host_irq_disabled = true;
184
Marcelo Tosatti547de292009-05-07 17:55:13 -0300185out:
186 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800187 return IRQ_HANDLED;
188}
189
190/* Ack the irq line for an assigned device */
191static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
192{
193 struct kvm_assigned_dev_kernel *dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300194 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800195
196 if (kian->gsi == -1)
197 return;
198
199 dev = container_of(kian, struct kvm_assigned_dev_kernel,
200 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000201
Sheng Yang5550af42008-10-15 20:15:06 +0800202 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000203
204 /* The guest irq may be shared so this ack may be
205 * from another device.
206 */
Marcelo Tosatti547de292009-05-07 17:55:13 -0300207 spin_lock_irqsave(&dev->assigned_dev_lock, flags);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000208 if (dev->host_irq_disabled) {
209 enable_irq(dev->host_irq);
210 dev->host_irq_disabled = false;
211 }
Marcelo Tosatti547de292009-05-07 17:55:13 -0300212 spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800213}
214
Sheng Yange56d5322009-03-12 21:45:39 +0800215static void deassign_guest_irq(struct kvm *kvm,
216 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800217{
Sheng Yange19e30e2008-10-20 16:07:10 +0800218 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Sheng Yange56d5322009-03-12 21:45:39 +0800219 assigned_dev->ack_notifier.gsi = -1;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000220
221 if (assigned_dev->irq_source_id != -1)
222 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
223 assigned_dev->irq_source_id = -1;
Sheng Yange56d5322009-03-12 21:45:39 +0800224 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
225}
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800226
Sheng Yange56d5322009-03-12 21:45:39 +0800227/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
228static void deassign_host_irq(struct kvm *kvm,
229 struct kvm_assigned_dev_kernel *assigned_dev)
230{
Sheng Yangba4cef32009-01-06 10:03:03 +0800231 /*
232 * In kvm_free_device_irq, cancel_work_sync return true if:
233 * 1. work is scheduled, and then cancelled.
234 * 2. work callback is executed.
235 *
236 * The first one ensured that the irq is disabled and no more events
237 * would happen. But for the second one, the irq may be enabled (e.g.
238 * for MSI). So we disable irq here to prevent further events.
239 *
240 * Notice this maybe result in nested disable if the interrupt type is
241 * INTx, but it's OK for we are going to free it.
242 *
243 * If this function is a part of VM destroy, please ensure that till
244 * now, the kvm state is still legal for probably we also have to wait
245 * interrupt_work done.
246 */
Sheng Yange56d5322009-03-12 21:45:39 +0800247 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yangd510d6c2009-02-25 17:22:28 +0800248 int i;
249 for (i = 0; i < assigned_dev->entries_nr; i++)
250 disable_irq_nosync(assigned_dev->
251 host_msix_entries[i].vector);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800252
Sheng Yangd510d6c2009-02-25 17:22:28 +0800253 cancel_work_sync(&assigned_dev->interrupt_work);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000254
Sheng Yangd510d6c2009-02-25 17:22:28 +0800255 for (i = 0; i < assigned_dev->entries_nr; i++)
256 free_irq(assigned_dev->host_msix_entries[i].vector,
257 (void *)assigned_dev);
258
259 assigned_dev->entries_nr = 0;
260 kfree(assigned_dev->host_msix_entries);
261 kfree(assigned_dev->guest_msix_entries);
262 pci_disable_msix(assigned_dev->dev);
263 } else {
264 /* Deal with MSI and INTx */
265 disable_irq_nosync(assigned_dev->host_irq);
266 cancel_work_sync(&assigned_dev->interrupt_work);
267
268 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
269
Sheng Yange56d5322009-03-12 21:45:39 +0800270 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800271 pci_disable_msi(assigned_dev->dev);
272 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000273
Sheng Yange56d5322009-03-12 21:45:39 +0800274 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000275}
276
Sheng Yange56d5322009-03-12 21:45:39 +0800277static int kvm_deassign_irq(struct kvm *kvm,
278 struct kvm_assigned_dev_kernel *assigned_dev,
279 unsigned long irq_requested_type)
280{
281 unsigned long guest_irq_type, host_irq_type;
282
283 if (!irqchip_in_kernel(kvm))
284 return -EINVAL;
285 /* no irq assignment to deassign */
286 if (!assigned_dev->irq_requested_type)
287 return -ENXIO;
288
289 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
290 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
291
292 if (host_irq_type)
293 deassign_host_irq(kvm, assigned_dev);
294 if (guest_irq_type)
295 deassign_guest_irq(kvm, assigned_dev);
296
297 return 0;
298}
299
300static void kvm_free_assigned_irq(struct kvm *kvm,
301 struct kvm_assigned_dev_kernel *assigned_dev)
302{
303 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
304}
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000305
306static void kvm_free_assigned_device(struct kvm *kvm,
307 struct kvm_assigned_dev_kernel
308 *assigned_dev)
309{
310 kvm_free_assigned_irq(kvm, assigned_dev);
311
Sheng Yang6eb55812008-10-31 12:37:41 +0800312 pci_reset_function(assigned_dev->dev);
313
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800314 pci_release_regions(assigned_dev->dev);
315 pci_disable_device(assigned_dev->dev);
316 pci_dev_put(assigned_dev->dev);
317
318 list_del(&assigned_dev->list);
319 kfree(assigned_dev);
320}
321
322void kvm_free_all_assigned_devices(struct kvm *kvm)
323{
324 struct list_head *ptr, *ptr2;
325 struct kvm_assigned_dev_kernel *assigned_dev;
326
327 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
328 assigned_dev = list_entry(ptr,
329 struct kvm_assigned_dev_kernel,
330 list);
331
332 kvm_free_assigned_device(kvm, assigned_dev);
333 }
334}
335
Sheng Yange56d5322009-03-12 21:45:39 +0800336static int assigned_device_enable_host_intx(struct kvm *kvm,
337 struct kvm_assigned_dev_kernel *dev)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800338{
Sheng Yange56d5322009-03-12 21:45:39 +0800339 dev->host_irq = dev->dev->irq;
340 /* Even though this is PCI, we don't want to use shared
341 * interrupts. Sharing host devices with guest-assigned devices
342 * on the same interrupt line is not a happy situation: there
343 * are going to be long delays in accepting, acking, etc.
344 */
345 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
346 0, "kvm_assigned_intx_device", (void *)dev))
347 return -EIO;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800348 return 0;
349}
350
Sheng Yange56d5322009-03-12 21:45:39 +0800351#ifdef __KVM_HAVE_MSI
352static int assigned_device_enable_host_msi(struct kvm *kvm,
353 struct kvm_assigned_dev_kernel *dev)
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800354{
355 int r;
356
Sheng Yange56d5322009-03-12 21:45:39 +0800357 if (!dev->dev->msi_enabled) {
358 r = pci_enable_msi(dev->dev);
359 if (r)
360 return r;
Sheng Yang5319c662008-11-24 14:32:57 +0800361 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800362
Sheng Yange56d5322009-03-12 21:45:39 +0800363 dev->host_irq = dev->dev->irq;
364 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
365 "kvm_assigned_msi_device", (void *)dev)) {
366 pci_disable_msi(dev->dev);
367 return -EIO;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800368 }
369
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800370 return 0;
371}
372#endif
373
Sheng Yangd510d6c2009-02-25 17:22:28 +0800374#ifdef __KVM_HAVE_MSIX
Sheng Yange56d5322009-03-12 21:45:39 +0800375static int assigned_device_enable_host_msix(struct kvm *kvm,
376 struct kvm_assigned_dev_kernel *dev)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800377{
Sheng Yange56d5322009-03-12 21:45:39 +0800378 int i, r = -EINVAL;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800379
Sheng Yange56d5322009-03-12 21:45:39 +0800380 /* host_msix_entries and guest_msix_entries should have been
381 * initialized */
382 if (dev->entries_nr == 0)
383 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800384
Sheng Yange56d5322009-03-12 21:45:39 +0800385 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
386 if (r)
387 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800388
Sheng Yange56d5322009-03-12 21:45:39 +0800389 for (i = 0; i < dev->entries_nr; i++) {
390 r = request_irq(dev->host_msix_entries[i].vector,
391 kvm_assigned_dev_intr, 0,
392 "kvm_assigned_msix_device",
393 (void *)dev);
394 /* FIXME: free requested_irq's on failure */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800395 if (r)
396 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800397 }
398
Sheng Yange56d5322009-03-12 21:45:39 +0800399 return 0;
400}
Sheng Yangd510d6c2009-02-25 17:22:28 +0800401
Sheng Yange56d5322009-03-12 21:45:39 +0800402#endif
403
404static int assigned_device_enable_guest_intx(struct kvm *kvm,
405 struct kvm_assigned_dev_kernel *dev,
406 struct kvm_assigned_irq *irq)
407{
408 dev->guest_irq = irq->guest_irq;
409 dev->ack_notifier.gsi = irq->guest_irq;
410 return 0;
411}
412
413#ifdef __KVM_HAVE_MSI
414static int assigned_device_enable_guest_msi(struct kvm *kvm,
415 struct kvm_assigned_dev_kernel *dev,
416 struct kvm_assigned_irq *irq)
417{
418 dev->guest_irq = irq->guest_irq;
419 dev->ack_notifier.gsi = -1;
420 return 0;
421}
422#endif
423#ifdef __KVM_HAVE_MSIX
424static int assigned_device_enable_guest_msix(struct kvm *kvm,
425 struct kvm_assigned_dev_kernel *dev,
426 struct kvm_assigned_irq *irq)
427{
428 dev->guest_irq = irq->guest_irq;
429 dev->ack_notifier.gsi = -1;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800430 return 0;
431}
432#endif
433
Sheng Yange56d5322009-03-12 21:45:39 +0800434static int assign_host_irq(struct kvm *kvm,
435 struct kvm_assigned_dev_kernel *dev,
436 __u32 host_irq_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800437{
Sheng Yange56d5322009-03-12 21:45:39 +0800438 int r = -EEXIST;
439
440 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
441 return r;
442
443 switch (host_irq_type) {
444 case KVM_DEV_IRQ_HOST_INTX:
445 r = assigned_device_enable_host_intx(kvm, dev);
446 break;
447#ifdef __KVM_HAVE_MSI
448 case KVM_DEV_IRQ_HOST_MSI:
449 r = assigned_device_enable_host_msi(kvm, dev);
450 break;
451#endif
452#ifdef __KVM_HAVE_MSIX
453 case KVM_DEV_IRQ_HOST_MSIX:
454 r = assigned_device_enable_host_msix(kvm, dev);
455 break;
456#endif
457 default:
458 r = -EINVAL;
459 }
460
461 if (!r)
462 dev->irq_requested_type |= host_irq_type;
463
464 return r;
465}
466
467static int assign_guest_irq(struct kvm *kvm,
468 struct kvm_assigned_dev_kernel *dev,
469 struct kvm_assigned_irq *irq,
470 unsigned long guest_irq_type)
471{
472 int id;
473 int r = -EEXIST;
474
475 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
476 return r;
477
478 id = kvm_request_irq_source_id(kvm);
479 if (id < 0)
480 return id;
481
482 dev->irq_source_id = id;
483
484 switch (guest_irq_type) {
485 case KVM_DEV_IRQ_GUEST_INTX:
486 r = assigned_device_enable_guest_intx(kvm, dev, irq);
487 break;
488#ifdef __KVM_HAVE_MSI
489 case KVM_DEV_IRQ_GUEST_MSI:
490 r = assigned_device_enable_guest_msi(kvm, dev, irq);
491 break;
492#endif
493#ifdef __KVM_HAVE_MSIX
494 case KVM_DEV_IRQ_GUEST_MSIX:
495 r = assigned_device_enable_guest_msix(kvm, dev, irq);
496 break;
497#endif
498 default:
499 r = -EINVAL;
500 }
501
502 if (!r) {
503 dev->irq_requested_type |= guest_irq_type;
504 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
505 } else
506 kvm_free_irq_source_id(kvm, dev->irq_source_id);
507
508 return r;
509}
510
511/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
512static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
513 struct kvm_assigned_irq *assigned_irq)
514{
515 int r = -EINVAL;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800516 struct kvm_assigned_dev_kernel *match;
Sheng Yange56d5322009-03-12 21:45:39 +0800517 unsigned long host_irq_type, guest_irq_type;
518
519 if (!capable(CAP_SYS_RAWIO))
520 return -EPERM;
521
522 if (!irqchip_in_kernel(kvm))
523 return r;
524
525 mutex_lock(&kvm->lock);
526 r = -ENODEV;
527 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
528 assigned_irq->assigned_dev_id);
529 if (!match)
530 goto out;
531
532 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
533 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
534
535 r = -EINVAL;
536 /* can only assign one type at a time */
537 if (hweight_long(host_irq_type) > 1)
538 goto out;
539 if (hweight_long(guest_irq_type) > 1)
540 goto out;
541 if (host_irq_type == 0 && guest_irq_type == 0)
542 goto out;
543
544 r = 0;
545 if (host_irq_type)
546 r = assign_host_irq(kvm, match, host_irq_type);
547 if (r)
548 goto out;
549
550 if (guest_irq_type)
551 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
552out:
553 mutex_unlock(&kvm->lock);
554 return r;
555}
556
557static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
558 struct kvm_assigned_irq
559 *assigned_irq)
560{
561 int r = -ENODEV;
562 struct kvm_assigned_dev_kernel *match;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800563
564 mutex_lock(&kvm->lock);
565
566 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
567 assigned_irq->assigned_dev_id);
Sheng Yange56d5322009-03-12 21:45:39 +0800568 if (!match)
569 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800570
Sheng Yange56d5322009-03-12 21:45:39 +0800571 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
572out:
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800573 mutex_unlock(&kvm->lock);
574 return r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800575}
576
577static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
578 struct kvm_assigned_pci_dev *assigned_dev)
579{
580 int r = 0;
581 struct kvm_assigned_dev_kernel *match;
582 struct pci_dev *dev;
583
Mark McLoughlin682edb42009-02-05 18:23:46 +0000584 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800585 mutex_lock(&kvm->lock);
586
587 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
588 assigned_dev->assigned_dev_id);
589 if (match) {
590 /* device already assigned */
Sheng Yange56d5322009-03-12 21:45:39 +0800591 r = -EEXIST;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800592 goto out;
593 }
594
595 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
596 if (match == NULL) {
597 printk(KERN_INFO "%s: Couldn't allocate memory\n",
598 __func__);
599 r = -ENOMEM;
600 goto out;
601 }
602 dev = pci_get_bus_and_slot(assigned_dev->busnr,
603 assigned_dev->devfn);
604 if (!dev) {
605 printk(KERN_INFO "%s: host device not found\n", __func__);
606 r = -EINVAL;
607 goto out_free;
608 }
609 if (pci_enable_device(dev)) {
610 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
611 r = -EBUSY;
612 goto out_put;
613 }
614 r = pci_request_regions(dev, "kvm_assigned_device");
615 if (r) {
616 printk(KERN_INFO "%s: Could not get access to device regions\n",
617 __func__);
618 goto out_disable;
619 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800620
621 pci_reset_function(dev);
622
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800623 match->assigned_dev_id = assigned_dev->assigned_dev_id;
624 match->host_busnr = assigned_dev->busnr;
625 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800626 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800627 match->dev = dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300628 spin_lock_init(&match->assigned_dev_lock);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000629 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800630 match->kvm = kvm;
Sheng Yange56d5322009-03-12 21:45:39 +0800631 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
632 INIT_WORK(&match->interrupt_work,
633 kvm_assigned_dev_interrupt_work_handler);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800634
635 list_add(&match->list, &kvm->arch.assigned_dev_head);
636
637 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100638 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800639 r = kvm_iommu_map_guest(kvm);
640 if (r)
641 goto out_list_del;
642 }
643 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800644 if (r)
645 goto out_list_del;
646 }
647
648out:
649 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000650 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800651 return r;
652out_list_del:
653 list_del(&match->list);
654 pci_release_regions(dev);
655out_disable:
656 pci_disable_device(dev);
657out_put:
658 pci_dev_put(dev);
659out_free:
660 kfree(match);
661 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000662 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800663 return r;
664}
665#endif
666
Weidong Han0a920352008-12-02 21:24:23 +0800667#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
668static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
669 struct kvm_assigned_pci_dev *assigned_dev)
670{
671 int r = 0;
672 struct kvm_assigned_dev_kernel *match;
673
674 mutex_lock(&kvm->lock);
675
676 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
677 assigned_dev->assigned_dev_id);
678 if (!match) {
679 printk(KERN_INFO "%s: device hasn't been assigned before, "
680 "so cannot be deassigned\n", __func__);
681 r = -EINVAL;
682 goto out;
683 }
684
Weidong Han4a906e42009-02-13 17:27:51 +0800685 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800686 kvm_deassign_device(kvm, match);
687
688 kvm_free_assigned_device(kvm, match);
689
690out:
691 mutex_unlock(&kvm->lock);
692 return r;
693}
694#endif
695
James Morris5aacf0c2006-12-22 01:04:55 -0800696static inline int valid_vcpu(int n)
697{
698 return likely(n >= 0 && n < KVM_MAX_VCPUS);
699}
700
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800701inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300702{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100703 if (pfn_valid(pfn)) {
704 struct page *page = compound_head(pfn_to_page(pfn));
705 return PageReserved(page);
706 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300707
708 return true;
709}
710
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711/*
712 * Switches to specified vcpu, until a matching vcpu_put()
713 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200714void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715{
Avi Kivity15ad7142007-07-11 18:17:21 +0300716 int cpu;
717
Avi Kivitybccf2152007-02-21 18:04:26 +0200718 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300719 cpu = get_cpu();
720 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200721 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300722 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200723}
724
Carsten Otte313a3dc2007-10-11 19:16:52 +0200725void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726{
Avi Kivity15ad7142007-07-11 18:17:21 +0300727 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200728 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300729 preempt_notifier_unregister(&vcpu->preempt_notifier);
730 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731 mutex_unlock(&vcpu->mutex);
732}
733
Avi Kivityd9e368d2007-06-07 19:18:30 +0300734static void ack_flush(void *_completed)
735{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300736}
737
Rusty Russell49846892008-12-08 20:26:24 +1030738static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300739{
Avi Kivity597a5f52008-07-20 14:24:22 +0300740 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030741 cpumask_var_t cpus;
742 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300743 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300744
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030745 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
746 cpumask_clear(cpus);
747
Avi Kivity597a5f52008-07-20 14:24:22 +0300748 me = get_cpu();
Marcelo Tosatti84261922009-06-17 10:53:47 -0300749 spin_lock(&kvm->requests_lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000750 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
751 vcpu = kvm->vcpus[i];
752 if (!vcpu)
753 continue;
Rusty Russell49846892008-12-08 20:26:24 +1030754 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300755 continue;
756 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030757 if (cpus != NULL && cpu != -1 && cpu != me)
758 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300759 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030760 if (unlikely(cpus == NULL))
761 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
762 else if (!cpumask_empty(cpus))
763 smp_call_function_many(cpus, ack_flush, NULL, 1);
764 else
765 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300766 spin_unlock(&kvm->requests_lock);
Avi Kivity597a5f52008-07-20 14:24:22 +0300767 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030768 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030769 return called;
770}
771
772void kvm_flush_remote_tlbs(struct kvm *kvm)
773{
774 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
775 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300776}
777
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500778void kvm_reload_remote_mmus(struct kvm *kvm)
779{
Rusty Russell49846892008-12-08 20:26:24 +1030780 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500781}
782
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000783int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
784{
785 struct page *page;
786 int r;
787
788 mutex_init(&vcpu->mutex);
789 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000790 vcpu->kvm = kvm;
791 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300792 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000793
794 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
795 if (!page) {
796 r = -ENOMEM;
797 goto fail;
798 }
799 vcpu->run = page_address(page);
800
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800801 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000802 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800803 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000804 return 0;
805
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000806fail_free_run:
807 free_page((unsigned long)vcpu->run);
808fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000809 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000810}
811EXPORT_SYMBOL_GPL(kvm_vcpu_init);
812
813void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
814{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800815 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000816 free_page((unsigned long)vcpu->run);
817}
818EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
819
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200820#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
821static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
822{
823 return container_of(mn, struct kvm, mmu_notifier);
824}
825
826static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
827 struct mm_struct *mm,
828 unsigned long address)
829{
830 struct kvm *kvm = mmu_notifier_to_kvm(mn);
831 int need_tlb_flush;
832
833 /*
834 * When ->invalidate_page runs, the linux pte has been zapped
835 * already but the page is still allocated until
836 * ->invalidate_page returns. So if we increase the sequence
837 * here the kvm page fault will notice if the spte can't be
838 * established because the page is going to be freed. If
839 * instead the kvm page fault establishes the spte before
840 * ->invalidate_page runs, kvm_unmap_hva will release it
841 * before returning.
842 *
843 * The sequence increase only need to be seen at spin_unlock
844 * time, and not at spin_lock time.
845 *
846 * Increasing the sequence after the spin_unlock would be
847 * unsafe because the kvm page fault could then establish the
848 * pte after kvm_unmap_hva returned, without noticing the page
849 * is going to be freed.
850 */
851 spin_lock(&kvm->mmu_lock);
852 kvm->mmu_notifier_seq++;
853 need_tlb_flush = kvm_unmap_hva(kvm, address);
854 spin_unlock(&kvm->mmu_lock);
855
856 /* we've to flush the tlb before the pages can be freed */
857 if (need_tlb_flush)
858 kvm_flush_remote_tlbs(kvm);
859
860}
861
862static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
863 struct mm_struct *mm,
864 unsigned long start,
865 unsigned long end)
866{
867 struct kvm *kvm = mmu_notifier_to_kvm(mn);
868 int need_tlb_flush = 0;
869
870 spin_lock(&kvm->mmu_lock);
871 /*
872 * The count increase must become visible at unlock time as no
873 * spte can be established without taking the mmu_lock and
874 * count is also read inside the mmu_lock critical section.
875 */
876 kvm->mmu_notifier_count++;
877 for (; start < end; start += PAGE_SIZE)
878 need_tlb_flush |= kvm_unmap_hva(kvm, start);
879 spin_unlock(&kvm->mmu_lock);
880
881 /* we've to flush the tlb before the pages can be freed */
882 if (need_tlb_flush)
883 kvm_flush_remote_tlbs(kvm);
884}
885
886static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
887 struct mm_struct *mm,
888 unsigned long start,
889 unsigned long end)
890{
891 struct kvm *kvm = mmu_notifier_to_kvm(mn);
892
893 spin_lock(&kvm->mmu_lock);
894 /*
895 * This sequence increase will notify the kvm page fault that
896 * the page that is going to be mapped in the spte could have
897 * been freed.
898 */
899 kvm->mmu_notifier_seq++;
900 /*
901 * The above sequence increase must be visible before the
902 * below count decrease but both values are read by the kvm
903 * page fault under mmu_lock spinlock so we don't need to add
904 * a smb_wmb() here in between the two.
905 */
906 kvm->mmu_notifier_count--;
907 spin_unlock(&kvm->mmu_lock);
908
909 BUG_ON(kvm->mmu_notifier_count < 0);
910}
911
912static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
913 struct mm_struct *mm,
914 unsigned long address)
915{
916 struct kvm *kvm = mmu_notifier_to_kvm(mn);
917 int young;
918
919 spin_lock(&kvm->mmu_lock);
920 young = kvm_age_hva(kvm, address);
921 spin_unlock(&kvm->mmu_lock);
922
923 if (young)
924 kvm_flush_remote_tlbs(kvm);
925
926 return young;
927}
928
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100929static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
930 struct mm_struct *mm)
931{
932 struct kvm *kvm = mmu_notifier_to_kvm(mn);
933 kvm_arch_flush_shadow(kvm);
934}
935
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200936static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
937 .invalidate_page = kvm_mmu_notifier_invalidate_page,
938 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
939 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
940 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100941 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200942};
943#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
944
Avi Kivityf17abe92007-02-21 19:28:04 +0200945static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800947 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200948#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
949 struct page *page;
950#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800952 if (IS_ERR(kvm))
953 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200954#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200955 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200956 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
957#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958
Laurent Vivier5f94c172008-05-30 16:05:54 +0200959#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
960 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
961 if (!page) {
962 kfree(kvm);
963 return ERR_PTR(-ENOMEM);
964 }
965 kvm->coalesced_mmio_ring =
966 (struct kvm_coalesced_mmio_ring *)page_address(page);
967#endif
968
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200969#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
970 {
971 int err;
972 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
973 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
974 if (err) {
975#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
976 put_page(page);
977#endif
978 kfree(kvm);
979 return ERR_PTR(err);
980 }
981 }
982#endif
983
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200984 kvm->mm = current->mm;
985 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500986 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300987 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300988 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskins721eecb2009-05-20 10:30:49 -0400989 kvm_irqfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800990 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400991 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200992 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300993 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000994 spin_lock(&kvm_lock);
995 list_add(&kvm->vm_list, &vm_list);
996 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200997#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
998 kvm_coalesced_mmio_init(kvm);
999#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001000out:
Avi Kivityf17abe92007-02-21 19:28:04 +02001001 return kvm;
1002}
1003
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004/*
1005 * Free any memory in @free but not in @dont.
1006 */
1007static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1008 struct kvm_memory_slot *dont)
1009{
Izik Eidus290fc382007-09-27 14:11:22 +02001010 if (!dont || free->rmap != dont->rmap)
1011 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012
1013 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1014 vfree(free->dirty_bitmap);
1015
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001016 if (!dont || free->lpage_info != dont->lpage_info)
1017 vfree(free->lpage_info);
1018
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001020 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001021 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001022 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023}
1024
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001025void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026{
1027 int i;
1028
1029 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001030 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031}
1032
Avi Kivityf17abe92007-02-21 19:28:04 +02001033static void kvm_destroy_vm(struct kvm *kvm)
1034{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001035 struct mm_struct *mm = kvm->mm;
1036
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001037 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001038 spin_lock(&kvm_lock);
1039 list_del(&kvm->vm_list);
1040 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001041 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001042 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001043 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001044#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1045 if (kvm->coalesced_mmio_ring != NULL)
1046 free_page((unsigned long)kvm->coalesced_mmio_ring);
1047#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001048#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1049 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +02001050#else
1051 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001052#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001053 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001054 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001055}
1056
Izik Eidusd39f13b2008-03-30 16:01:25 +03001057void kvm_get_kvm(struct kvm *kvm)
1058{
1059 atomic_inc(&kvm->users_count);
1060}
1061EXPORT_SYMBOL_GPL(kvm_get_kvm);
1062
1063void kvm_put_kvm(struct kvm *kvm)
1064{
1065 if (atomic_dec_and_test(&kvm->users_count))
1066 kvm_destroy_vm(kvm);
1067}
1068EXPORT_SYMBOL_GPL(kvm_put_kvm);
1069
1070
Avi Kivityf17abe92007-02-21 19:28:04 +02001071static int kvm_vm_release(struct inode *inode, struct file *filp)
1072{
1073 struct kvm *kvm = filp->private_data;
1074
Gregory Haskins721eecb2009-05-20 10:30:49 -04001075 kvm_irqfd_release(kvm);
1076
Izik Eidusd39f13b2008-03-30 16:01:25 +03001077 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 return 0;
1079}
1080
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 * Allocate some memory and give it an address in the guest physical address
1083 * space.
1084 *
1085 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001086 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001087 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001089int __kvm_set_memory_region(struct kvm *kvm,
1090 struct kvm_userspace_memory_region *mem,
1091 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092{
1093 int r;
1094 gfn_t base_gfn;
Avi Kivityac045272009-06-08 15:52:39 +03001095 unsigned long npages, ugfn;
Avi Kivity09f8ca72009-06-08 15:55:21 +03001096 unsigned long largepages, i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097 struct kvm_memory_slot *memslot;
1098 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099
1100 r = -EINVAL;
1101 /* General sanity checks */
1102 if (mem->memory_size & (PAGE_SIZE - 1))
1103 goto out;
1104 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1105 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001106 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001107 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001108 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 goto out;
1110 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1111 goto out;
1112
1113 memslot = &kvm->memslots[mem->slot];
1114 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1115 npages = mem->memory_size >> PAGE_SHIFT;
1116
1117 if (!npages)
1118 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1119
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 new = old = *memslot;
1121
1122 new.base_gfn = base_gfn;
1123 new.npages = npages;
1124 new.flags = mem->flags;
1125
1126 /* Disallow changing a memory slot's size. */
1127 r = -EINVAL;
1128 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001129 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130
1131 /* Check for overlaps */
1132 r = -EEXIST;
1133 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1134 struct kvm_memory_slot *s = &kvm->memslots[i];
1135
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001136 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 continue;
1138 if (!((base_gfn + npages <= s->base_gfn) ||
1139 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001140 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 /* Free page dirty bitmap if unneeded */
1144 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001145 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146
1147 r = -ENOMEM;
1148
1149 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001150#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001151 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001152 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001153
1154 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001155 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001156
Izik Eidus290fc382007-09-27 14:11:22 +02001157 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001158
Izik Eidus80b14b52007-10-25 11:54:04 +02001159 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001160 /*
1161 * hva_to_rmmap() serialzies with the mmu_lock and to be
1162 * safe it has to ignore memslots with !user_alloc &&
1163 * !userspace_addr.
1164 */
1165 if (user_alloc)
1166 new.userspace_addr = mem->userspace_addr;
1167 else
1168 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001170 if (npages && !new.lpage_info) {
Avi Kivity99894a72009-03-29 16:31:25 +03001171 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1172 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001173
1174 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1175
1176 if (!new.lpage_info)
1177 goto out_free;
1178
1179 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1180
1181 if (base_gfn % KVM_PAGES_PER_HPAGE)
1182 new.lpage_info[0].write_count = 1;
1183 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1184 new.lpage_info[largepages-1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +03001185 ugfn = new.userspace_addr >> PAGE_SHIFT;
1186 /*
1187 * If the gfn and userspace address are not aligned wrt each
1188 * other, disable large page support for this slot
1189 */
1190 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1))
1191 for (i = 0; i < largepages; ++i)
1192 new.lpage_info[i].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001193 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194
1195 /* Allocate page dirty bitmap if needed */
1196 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1197 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1198
1199 new.dirty_bitmap = vmalloc(dirty_bytes);
1200 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001201 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +03001203 if (old.npages)
1204 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205 }
Carsten Otteeff01142008-06-27 15:05:31 +02001206#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001208 if (!npages)
1209 kvm_arch_flush_shadow(kvm);
1210
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001211 spin_lock(&kvm->mmu_lock);
1212 if (mem->slot >= kvm->nmemslots)
1213 kvm->nmemslots = mem->slot + 1;
1214
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001216 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217
Zhang Xiantao0de10342007-11-20 16:25:04 +08001218 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1219 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001220 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001221 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001222 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001223 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001224 }
1225
Glauber Costa6f897242008-12-03 13:40:51 -02001226 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1227 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001228 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -02001229 if (!npages)
1230 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001231 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001232#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001233 /* map the pages in iommu page table */
1234 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1235 if (r)
1236 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001237#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 return 0;
1239
Sheng Yangf78e0e22007-10-29 09:40:42 +08001240out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 kvm_free_physmem_slot(&new, &old);
1242out:
1243 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001244
1245}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001246EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1247
1248int kvm_set_memory_region(struct kvm *kvm,
1249 struct kvm_userspace_memory_region *mem,
1250 int user_alloc)
1251{
1252 int r;
1253
Izik Eidus72dc67a2008-02-10 18:04:15 +02001254 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001255 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001256 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001257 return r;
1258}
Izik Eidus210c7c42007-10-24 23:52:57 +02001259EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1260
Carsten Otte1fe779f2007-10-29 16:08:35 +01001261int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1262 struct
1263 kvm_userspace_memory_region *mem,
1264 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001265{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001266 if (mem->slot >= KVM_MEMORY_SLOTS)
1267 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001268 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269}
1270
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001271int kvm_get_dirty_log(struct kvm *kvm,
1272 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273{
1274 struct kvm_memory_slot *memslot;
1275 int r, i;
1276 int n;
1277 unsigned long any = 0;
1278
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279 r = -EINVAL;
1280 if (log->slot >= KVM_MEMORY_SLOTS)
1281 goto out;
1282
1283 memslot = &kvm->memslots[log->slot];
1284 r = -ENOENT;
1285 if (!memslot->dirty_bitmap)
1286 goto out;
1287
Uri Lublincd1a4a92007-02-22 16:43:09 +02001288 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289
Uri Lublincd1a4a92007-02-22 16:43:09 +02001290 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 any = memslot->dirty_bitmap[i];
1292
1293 r = -EFAULT;
1294 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1295 goto out;
1296
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001297 if (any)
1298 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299
1300 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 return r;
1303}
1304
Izik Eiduscea7bb22007-10-17 19:17:48 +02001305int is_error_page(struct page *page)
1306{
1307 return page == bad_page;
1308}
1309EXPORT_SYMBOL_GPL(is_error_page);
1310
Anthony Liguori35149e22008-04-02 14:46:56 -05001311int is_error_pfn(pfn_t pfn)
1312{
1313 return pfn == bad_pfn;
1314}
1315EXPORT_SYMBOL_GPL(is_error_pfn);
1316
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001317static inline unsigned long bad_hva(void)
1318{
1319 return PAGE_OFFSET;
1320}
1321
1322int kvm_is_error_hva(unsigned long addr)
1323{
1324 return addr == bad_hva();
1325}
1326EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1327
Izik Eidus28430992008-10-03 17:40:32 +03001328struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329{
1330 int i;
1331
1332 for (i = 0; i < kvm->nmemslots; ++i) {
1333 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1334
1335 if (gfn >= memslot->base_gfn
1336 && gfn < memslot->base_gfn + memslot->npages)
1337 return memslot;
1338 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001339 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340}
Izik Eidus28430992008-10-03 17:40:32 +03001341EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001342
1343struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1344{
1345 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001346 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001347}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348
Izik Eiduse0d62c72007-10-24 23:57:46 +02001349int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1350{
1351 int i;
1352
1353 gfn = unalias_gfn(kvm, gfn);
1354 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1355 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1356
1357 if (gfn >= memslot->base_gfn
1358 && gfn < memslot->base_gfn + memslot->npages)
1359 return 1;
1360 }
1361 return 0;
1362}
1363EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1364
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001365unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001366{
1367 struct kvm_memory_slot *slot;
1368
1369 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001370 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001371 if (!slot)
1372 return bad_hva();
1373 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1374}
Sheng Yang0d150292008-04-25 21:44:50 +08001375EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001376
Anthony Liguori35149e22008-04-02 14:46:56 -05001377pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001378{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001379 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001380 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001381 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001382 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001383
Avi Kivity60395222007-10-21 11:03:36 +02001384 might_sleep();
1385
Izik Eidus539cb662007-11-11 22:05:04 +02001386 addr = gfn_to_hva(kvm, gfn);
1387 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001388 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001389 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001390 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001391
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001392 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001393
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001394 if (unlikely(npages != 1)) {
1395 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001396
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001397 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001398 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001399
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001400 if (vma == NULL || addr < vma->vm_start ||
1401 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001402 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001403 get_page(bad_page);
1404 return page_to_pfn(bad_page);
1405 }
1406
1407 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001408 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001409 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001410 } else
1411 pfn = page_to_pfn(page[0]);
1412
1413 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001414}
1415
1416EXPORT_SYMBOL_GPL(gfn_to_pfn);
1417
1418struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1419{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001420 pfn_t pfn;
1421
1422 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001423 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001424 return pfn_to_page(pfn);
1425
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001426 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001427
1428 get_page(bad_page);
1429 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001430}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001431
Avi Kivity954bbbc22007-03-30 14:02:32 +03001432EXPORT_SYMBOL_GPL(gfn_to_page);
1433
Izik Eidusb4231d62007-11-20 11:49:33 +02001434void kvm_release_page_clean(struct page *page)
1435{
Anthony Liguori35149e22008-04-02 14:46:56 -05001436 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001437}
1438EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1439
Anthony Liguori35149e22008-04-02 14:46:56 -05001440void kvm_release_pfn_clean(pfn_t pfn)
1441{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001442 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001443 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001444}
1445EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1446
Izik Eidusb4231d62007-11-20 11:49:33 +02001447void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001448{
Anthony Liguori35149e22008-04-02 14:46:56 -05001449 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001450}
Izik Eidusb4231d62007-11-20 11:49:33 +02001451EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001452
Anthony Liguori35149e22008-04-02 14:46:56 -05001453void kvm_release_pfn_dirty(pfn_t pfn)
1454{
1455 kvm_set_pfn_dirty(pfn);
1456 kvm_release_pfn_clean(pfn);
1457}
1458EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1459
1460void kvm_set_page_dirty(struct page *page)
1461{
1462 kvm_set_pfn_dirty(page_to_pfn(page));
1463}
1464EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1465
1466void kvm_set_pfn_dirty(pfn_t pfn)
1467{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001468 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001469 struct page *page = pfn_to_page(pfn);
1470 if (!PageReserved(page))
1471 SetPageDirty(page);
1472 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001473}
1474EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1475
1476void kvm_set_pfn_accessed(pfn_t pfn)
1477{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001478 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001479 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001480}
1481EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1482
1483void kvm_get_pfn(pfn_t pfn)
1484{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001485 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001486 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001487}
1488EXPORT_SYMBOL_GPL(kvm_get_pfn);
1489
Izik Eidus195aefd2007-10-01 22:14:18 +02001490static int next_segment(unsigned long len, int offset)
1491{
1492 if (len > PAGE_SIZE - offset)
1493 return PAGE_SIZE - offset;
1494 else
1495 return len;
1496}
1497
1498int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1499 int len)
1500{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001501 int r;
1502 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001503
Izik Eiduse0506bc2007-11-11 22:10:22 +02001504 addr = gfn_to_hva(kvm, gfn);
1505 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001506 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001507 r = copy_from_user(data, (void __user *)addr + offset, len);
1508 if (r)
1509 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001510 return 0;
1511}
1512EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1513
1514int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1515{
1516 gfn_t gfn = gpa >> PAGE_SHIFT;
1517 int seg;
1518 int offset = offset_in_page(gpa);
1519 int ret;
1520
1521 while ((seg = next_segment(len, offset)) != 0) {
1522 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1523 if (ret < 0)
1524 return ret;
1525 offset = 0;
1526 len -= seg;
1527 data += seg;
1528 ++gfn;
1529 }
1530 return 0;
1531}
1532EXPORT_SYMBOL_GPL(kvm_read_guest);
1533
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001534int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1535 unsigned long len)
1536{
1537 int r;
1538 unsigned long addr;
1539 gfn_t gfn = gpa >> PAGE_SHIFT;
1540 int offset = offset_in_page(gpa);
1541
1542 addr = gfn_to_hva(kvm, gfn);
1543 if (kvm_is_error_hva(addr))
1544 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001545 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001546 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001547 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001548 if (r)
1549 return -EFAULT;
1550 return 0;
1551}
1552EXPORT_SYMBOL(kvm_read_guest_atomic);
1553
Izik Eidus195aefd2007-10-01 22:14:18 +02001554int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1555 int offset, int len)
1556{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001557 int r;
1558 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001559
Izik Eiduse0506bc2007-11-11 22:10:22 +02001560 addr = gfn_to_hva(kvm, gfn);
1561 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001562 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001563 r = copy_to_user((void __user *)addr + offset, data, len);
1564 if (r)
1565 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001566 mark_page_dirty(kvm, gfn);
1567 return 0;
1568}
1569EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1570
1571int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1572 unsigned long len)
1573{
1574 gfn_t gfn = gpa >> PAGE_SHIFT;
1575 int seg;
1576 int offset = offset_in_page(gpa);
1577 int ret;
1578
1579 while ((seg = next_segment(len, offset)) != 0) {
1580 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1581 if (ret < 0)
1582 return ret;
1583 offset = 0;
1584 len -= seg;
1585 data += seg;
1586 ++gfn;
1587 }
1588 return 0;
1589}
1590
1591int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1592{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001593 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001594}
1595EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1596
1597int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1598{
1599 gfn_t gfn = gpa >> PAGE_SHIFT;
1600 int seg;
1601 int offset = offset_in_page(gpa);
1602 int ret;
1603
1604 while ((seg = next_segment(len, offset)) != 0) {
1605 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1606 if (ret < 0)
1607 return ret;
1608 offset = 0;
1609 len -= seg;
1610 ++gfn;
1611 }
1612 return 0;
1613}
1614EXPORT_SYMBOL_GPL(kvm_clear_guest);
1615
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1617{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001618 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619
Uri Lublin3b6fff12007-10-30 10:42:09 +02001620 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001621 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001622 if (memslot && memslot->dirty_bitmap) {
1623 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624
Rusty Russell7e9d6192007-07-31 20:41:14 +10001625 /* avoid RMW */
1626 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1627 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 }
1629}
1630
Eddie Dongb6958ce2007-07-18 12:15:21 +03001631/*
1632 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1633 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001634void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001635{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001636 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001637
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001638 for (;;) {
1639 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001640
Gleb Natapov78646122009-03-23 12:12:11 +02001641 if ((kvm_arch_interrupt_allowed(vcpu) &&
1642 kvm_cpu_has_interrupt(vcpu)) ||
Gleb Natapov09cec752009-03-23 15:11:44 +02001643 kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001644 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001645 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001646 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001647 if (kvm_cpu_has_pending_timer(vcpu))
1648 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001649 if (signal_pending(current))
1650 break;
1651
Eddie Dongb6958ce2007-07-18 12:15:21 +03001652 vcpu_put(vcpu);
1653 schedule();
1654 vcpu_load(vcpu);
1655 }
1656
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001657 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001658}
1659
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660void kvm_resched(struct kvm_vcpu *vcpu)
1661{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001662 if (!need_resched())
1663 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665}
1666EXPORT_SYMBOL_GPL(kvm_resched);
1667
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001668static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001669{
1670 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001671 struct page *page;
1672
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001673 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001674 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001675#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001676 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001677 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001678#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001679#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1680 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1681 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1682#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001683 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001684 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001685 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001686 vmf->page = page;
1687 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001688}
1689
1690static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001691 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001692};
1693
1694static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1695{
1696 vma->vm_ops = &kvm_vcpu_vm_ops;
1697 return 0;
1698}
1699
Avi Kivitybccf2152007-02-21 18:04:26 +02001700static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1701{
1702 struct kvm_vcpu *vcpu = filp->private_data;
1703
Al Viro66c0b392008-04-19 20:33:56 +01001704 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001705 return 0;
1706}
1707
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001708static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001709 .release = kvm_vcpu_release,
1710 .unlocked_ioctl = kvm_vcpu_ioctl,
1711 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001712 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001713};
1714
1715/*
1716 * Allocates an inode for the vcpu.
1717 */
1718static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1719{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001720 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001721 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001722 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001723 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001724}
1725
Avi Kivityc5ea7662007-02-20 18:41:05 +02001726/*
1727 * Creates some virtual cpus. Good luck creating more than one.
1728 */
1729static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1730{
1731 int r;
1732 struct kvm_vcpu *vcpu;
1733
Avi Kivityc5ea7662007-02-20 18:41:05 +02001734 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001735 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001736
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001737 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001738 if (IS_ERR(vcpu))
1739 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001740
Avi Kivity15ad7142007-07-11 18:17:21 +03001741 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1742
Avi Kivity26e52152007-11-20 15:30:24 +02001743 r = kvm_arch_vcpu_setup(vcpu);
1744 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001745 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001746
Shaohua Li11ec2802007-07-23 14:51:37 +08001747 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001748 if (kvm->vcpus[n]) {
1749 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001750 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001751 }
1752 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001753 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001754
1755 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001756 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001757 r = create_vcpu_fd(vcpu);
1758 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001759 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001760 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001761
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001762unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001763 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001764 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001765vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001766 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001767 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001768 return r;
1769}
1770
Avi Kivity1961d272007-03-05 19:46:05 +02001771static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1772{
1773 if (sigset) {
1774 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1775 vcpu->sigset_active = 1;
1776 vcpu->sigset = *sigset;
1777 } else
1778 vcpu->sigset_active = 0;
1779 return 0;
1780}
1781
Sheng Yangc1e01512009-02-25 17:22:26 +08001782#ifdef __KVM_HAVE_MSIX
1783static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1784 struct kvm_assigned_msix_nr *entry_nr)
1785{
1786 int r = 0;
1787 struct kvm_assigned_dev_kernel *adev;
1788
1789 mutex_lock(&kvm->lock);
1790
1791 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1792 entry_nr->assigned_dev_id);
1793 if (!adev) {
1794 r = -EINVAL;
1795 goto msix_nr_out;
1796 }
1797
1798 if (adev->entries_nr == 0) {
1799 adev->entries_nr = entry_nr->entry_nr;
1800 if (adev->entries_nr == 0 ||
1801 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1802 r = -EINVAL;
1803 goto msix_nr_out;
1804 }
1805
1806 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1807 entry_nr->entry_nr,
1808 GFP_KERNEL);
1809 if (!adev->host_msix_entries) {
1810 r = -ENOMEM;
1811 goto msix_nr_out;
1812 }
1813 adev->guest_msix_entries = kzalloc(
1814 sizeof(struct kvm_guest_msix_entry) *
1815 entry_nr->entry_nr, GFP_KERNEL);
1816 if (!adev->guest_msix_entries) {
1817 kfree(adev->host_msix_entries);
1818 r = -ENOMEM;
1819 goto msix_nr_out;
1820 }
1821 } else /* Not allowed set MSI-X number twice */
1822 r = -EINVAL;
1823msix_nr_out:
1824 mutex_unlock(&kvm->lock);
1825 return r;
1826}
1827
1828static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1829 struct kvm_assigned_msix_entry *entry)
1830{
1831 int r = 0, i;
1832 struct kvm_assigned_dev_kernel *adev;
1833
1834 mutex_lock(&kvm->lock);
1835
1836 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1837 entry->assigned_dev_id);
1838
1839 if (!adev) {
1840 r = -EINVAL;
1841 goto msix_entry_out;
1842 }
1843
1844 for (i = 0; i < adev->entries_nr; i++)
1845 if (adev->guest_msix_entries[i].vector == 0 ||
1846 adev->guest_msix_entries[i].entry == entry->entry) {
1847 adev->guest_msix_entries[i].entry = entry->entry;
1848 adev->guest_msix_entries[i].vector = entry->gsi;
1849 adev->host_msix_entries[i].entry = entry->entry;
1850 break;
1851 }
1852 if (i == adev->entries_nr) {
1853 r = -ENOSPC;
1854 goto msix_entry_out;
1855 }
1856
1857msix_entry_out:
1858 mutex_unlock(&kvm->lock);
1859
1860 return r;
1861}
1862#endif
1863
Avi Kivitybccf2152007-02-21 18:04:26 +02001864static long kvm_vcpu_ioctl(struct file *filp,
1865 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866{
Avi Kivitybccf2152007-02-21 18:04:26 +02001867 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001868 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001869 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001870 struct kvm_fpu *fpu = NULL;
1871 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001873 if (vcpu->kvm->mm != current->mm)
1874 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001876 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001877 r = -EINVAL;
1878 if (arg)
1879 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001880 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001883 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001885 r = -ENOMEM;
1886 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1887 if (!kvm_regs)
1888 goto out;
1889 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001891 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001893 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1894 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001896out_free1:
1897 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898 break;
1899 }
1900 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001901 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001903 r = -ENOMEM;
1904 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1905 if (!kvm_regs)
1906 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001908 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1909 goto out_free2;
1910 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001912 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001914out_free2:
1915 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916 break;
1917 }
1918 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001919 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1920 r = -ENOMEM;
1921 if (!kvm_sregs)
1922 goto out;
1923 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924 if (r)
1925 goto out;
1926 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001927 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928 goto out;
1929 r = 0;
1930 break;
1931 }
1932 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001933 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1934 r = -ENOMEM;
1935 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001937 r = -EFAULT;
1938 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1939 goto out;
1940 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 if (r)
1942 goto out;
1943 r = 0;
1944 break;
1945 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001946 case KVM_GET_MP_STATE: {
1947 struct kvm_mp_state mp_state;
1948
1949 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1950 if (r)
1951 goto out;
1952 r = -EFAULT;
1953 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1954 goto out;
1955 r = 0;
1956 break;
1957 }
1958 case KVM_SET_MP_STATE: {
1959 struct kvm_mp_state mp_state;
1960
1961 r = -EFAULT;
1962 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1963 goto out;
1964 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1965 if (r)
1966 goto out;
1967 r = 0;
1968 break;
1969 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 case KVM_TRANSLATE: {
1971 struct kvm_translation tr;
1972
1973 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001974 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001976 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001977 if (r)
1978 goto out;
1979 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001980 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 goto out;
1982 r = 0;
1983 break;
1984 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001985 case KVM_SET_GUEST_DEBUG: {
1986 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987
1988 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001989 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001991 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992 if (r)
1993 goto out;
1994 r = 0;
1995 break;
1996 }
Avi Kivity1961d272007-03-05 19:46:05 +02001997 case KVM_SET_SIGNAL_MASK: {
1998 struct kvm_signal_mask __user *sigmask_arg = argp;
1999 struct kvm_signal_mask kvm_sigmask;
2000 sigset_t sigset, *p;
2001
2002 p = NULL;
2003 if (argp) {
2004 r = -EFAULT;
2005 if (copy_from_user(&kvm_sigmask, argp,
2006 sizeof kvm_sigmask))
2007 goto out;
2008 r = -EINVAL;
2009 if (kvm_sigmask.len != sizeof sigset)
2010 goto out;
2011 r = -EFAULT;
2012 if (copy_from_user(&sigset, sigmask_arg->sigset,
2013 sizeof sigset))
2014 goto out;
2015 p = &sigset;
2016 }
2017 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2018 break;
2019 }
Avi Kivityb8836732007-04-01 16:34:31 +03002020 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002021 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2022 r = -ENOMEM;
2023 if (!fpu)
2024 goto out;
2025 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002026 if (r)
2027 goto out;
2028 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002029 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03002030 goto out;
2031 r = 0;
2032 break;
2033 }
2034 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002035 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2036 r = -ENOMEM;
2037 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002038 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002039 r = -EFAULT;
2040 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2041 goto out;
2042 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002043 if (r)
2044 goto out;
2045 r = 0;
2046 break;
2047 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002048 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002049 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002050 }
2051out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002052 kfree(fpu);
2053 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002054 return r;
2055}
2056
2057static long kvm_vm_ioctl(struct file *filp,
2058 unsigned int ioctl, unsigned long arg)
2059{
2060 struct kvm *kvm = filp->private_data;
2061 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002062 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002063
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002064 if (kvm->mm != current->mm)
2065 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002066 switch (ioctl) {
2067 case KVM_CREATE_VCPU:
2068 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2069 if (r < 0)
2070 goto out;
2071 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002072 case KVM_SET_USER_MEMORY_REGION: {
2073 struct kvm_userspace_memory_region kvm_userspace_mem;
2074
2075 r = -EFAULT;
2076 if (copy_from_user(&kvm_userspace_mem, argp,
2077 sizeof kvm_userspace_mem))
2078 goto out;
2079
2080 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 if (r)
2082 goto out;
2083 break;
2084 }
2085 case KVM_GET_DIRTY_LOG: {
2086 struct kvm_dirty_log log;
2087
2088 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002089 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002091 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 if (r)
2093 goto out;
2094 break;
2095 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002096#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2097 case KVM_REGISTER_COALESCED_MMIO: {
2098 struct kvm_coalesced_mmio_zone zone;
2099 r = -EFAULT;
2100 if (copy_from_user(&zone, argp, sizeof zone))
2101 goto out;
2102 r = -ENXIO;
2103 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2104 if (r)
2105 goto out;
2106 r = 0;
2107 break;
2108 }
2109 case KVM_UNREGISTER_COALESCED_MMIO: {
2110 struct kvm_coalesced_mmio_zone zone;
2111 r = -EFAULT;
2112 if (copy_from_user(&zone, argp, sizeof zone))
2113 goto out;
2114 r = -ENXIO;
2115 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2116 if (r)
2117 goto out;
2118 r = 0;
2119 break;
2120 }
2121#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002122#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2123 case KVM_ASSIGN_PCI_DEVICE: {
2124 struct kvm_assigned_pci_dev assigned_dev;
2125
2126 r = -EFAULT;
2127 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2128 goto out;
2129 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2130 if (r)
2131 goto out;
2132 break;
2133 }
2134 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002135 r = -EOPNOTSUPP;
2136 break;
2137 }
2138#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2139 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002140 struct kvm_assigned_irq assigned_irq;
2141
2142 r = -EFAULT;
2143 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2144 goto out;
2145 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2146 if (r)
2147 goto out;
2148 break;
2149 }
Sheng Yange56d5322009-03-12 21:45:39 +08002150 case KVM_DEASSIGN_DEV_IRQ: {
2151 struct kvm_assigned_irq assigned_irq;
2152
2153 r = -EFAULT;
2154 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2155 goto out;
2156 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2157 if (r)
2158 goto out;
2159 break;
2160 }
2161#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002162#endif
Weidong Han0a920352008-12-02 21:24:23 +08002163#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2164 case KVM_DEASSIGN_PCI_DEVICE: {
2165 struct kvm_assigned_pci_dev assigned_dev;
2166
2167 r = -EFAULT;
2168 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2169 goto out;
2170 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2171 if (r)
2172 goto out;
2173 break;
2174 }
2175#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002176#ifdef KVM_CAP_IRQ_ROUTING
2177 case KVM_SET_GSI_ROUTING: {
2178 struct kvm_irq_routing routing;
2179 struct kvm_irq_routing __user *urouting;
2180 struct kvm_irq_routing_entry *entries;
2181
2182 r = -EFAULT;
2183 if (copy_from_user(&routing, argp, sizeof(routing)))
2184 goto out;
2185 r = -EINVAL;
2186 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2187 goto out;
2188 if (routing.flags)
2189 goto out;
2190 r = -ENOMEM;
2191 entries = vmalloc(routing.nr * sizeof(*entries));
2192 if (!entries)
2193 goto out;
2194 r = -EFAULT;
2195 urouting = argp;
2196 if (copy_from_user(entries, urouting->entries,
2197 routing.nr * sizeof(*entries)))
2198 goto out_free_irq_routing;
2199 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2200 routing.flags);
2201 out_free_irq_routing:
2202 vfree(entries);
2203 break;
2204 }
Sheng Yangc1e01512009-02-25 17:22:26 +08002205#ifdef __KVM_HAVE_MSIX
2206 case KVM_ASSIGN_SET_MSIX_NR: {
2207 struct kvm_assigned_msix_nr entry_nr;
2208 r = -EFAULT;
2209 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2210 goto out;
2211 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2212 if (r)
2213 goto out;
2214 break;
2215 }
2216 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2217 struct kvm_assigned_msix_entry entry;
2218 r = -EFAULT;
2219 if (copy_from_user(&entry, argp, sizeof entry))
2220 goto out;
2221 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2222 if (r)
2223 goto out;
2224 break;
2225 }
Avi Kivity399ec802008-11-19 13:58:46 +02002226#endif
Sheng Yangc1e01512009-02-25 17:22:26 +08002227#endif /* KVM_CAP_IRQ_ROUTING */
Gregory Haskins721eecb2009-05-20 10:30:49 -04002228 case KVM_IRQFD: {
2229 struct kvm_irqfd data;
2230
2231 r = -EFAULT;
2232 if (copy_from_user(&data, argp, sizeof data))
2233 goto out;
2234 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2235 break;
2236 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002237 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002238 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002239 }
2240out:
2241 return r;
2242}
2243
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002244static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002245{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002246 struct page *page[1];
2247 unsigned long addr;
2248 int npages;
2249 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002250 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002251
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002252 addr = gfn_to_hva(kvm, gfn);
2253 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002254 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002255
2256 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2257 NULL);
2258 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002259 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002260
2261 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002262 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002263}
2264
2265static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002266 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002267};
2268
2269static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2270{
2271 vma->vm_ops = &kvm_vm_vm_ops;
2272 return 0;
2273}
2274
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002275static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002276 .release = kvm_vm_release,
2277 .unlocked_ioctl = kvm_vm_ioctl,
2278 .compat_ioctl = kvm_vm_ioctl,
2279 .mmap = kvm_vm_mmap,
2280};
2281
2282static int kvm_dev_ioctl_create_vm(void)
2283{
Al Viro2030a422008-02-23 06:46:49 -05002284 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002285 struct kvm *kvm;
2286
Avi Kivityf17abe92007-02-21 19:28:04 +02002287 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002288 if (IS_ERR(kvm))
2289 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002290 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002291 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002292 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002293
Avi Kivityf17abe92007-02-21 19:28:04 +02002294 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002295}
2296
Avi Kivity1a811b62008-12-08 18:25:27 +02002297static long kvm_dev_ioctl_check_extension_generic(long arg)
2298{
2299 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002300 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002301 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002302 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Avi Kivity1a811b62008-12-08 18:25:27 +02002303 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002304#ifdef CONFIG_HAVE_KVM_IRQCHIP
2305 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002306 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002307#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002308 default:
2309 break;
2310 }
2311 return kvm_dev_ioctl_check_extension(arg);
2312}
2313
Avi Kivityf17abe92007-02-21 19:28:04 +02002314static long kvm_dev_ioctl(struct file *filp,
2315 unsigned int ioctl, unsigned long arg)
2316{
Avi Kivity07c45a32007-03-07 13:05:38 +02002317 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002318
2319 switch (ioctl) {
2320 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002321 r = -EINVAL;
2322 if (arg)
2323 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002324 r = KVM_API_VERSION;
2325 break;
2326 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002327 r = -EINVAL;
2328 if (arg)
2329 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002330 r = kvm_dev_ioctl_create_vm();
2331 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002332 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002333 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002334 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002335 case KVM_GET_VCPU_MMAP_SIZE:
2336 r = -EINVAL;
2337 if (arg)
2338 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002339 r = PAGE_SIZE; /* struct kvm_run */
2340#ifdef CONFIG_X86
2341 r += PAGE_SIZE; /* pio data page */
2342#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002343#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2344 r += PAGE_SIZE; /* coalesced mmio ring page */
2345#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002346 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002347 case KVM_TRACE_ENABLE:
2348 case KVM_TRACE_PAUSE:
2349 case KVM_TRACE_DISABLE:
2350 r = kvm_trace_ioctl(ioctl, arg);
2351 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002353 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354 }
2355out:
2356 return r;
2357}
2358
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360 .unlocked_ioctl = kvm_dev_ioctl,
2361 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362};
2363
2364static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002365 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 "kvm",
2367 &kvm_chardev_ops,
2368};
2369
Avi Kivity1b6c0162007-05-24 13:03:52 +03002370static void hardware_enable(void *junk)
2371{
2372 int cpu = raw_smp_processor_id();
2373
Rusty Russell7f59f492008-12-07 21:25:45 +10302374 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002375 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302376 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002377 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002378}
2379
2380static void hardware_disable(void *junk)
2381{
2382 int cpu = raw_smp_processor_id();
2383
Rusty Russell7f59f492008-12-07 21:25:45 +10302384 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002385 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302386 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002387 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002388}
2389
Avi Kivity774c47f2007-02-12 00:54:47 -08002390static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2391 void *v)
2392{
2393 int cpu = (long)v;
2394
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002395 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002396 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002397 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002398 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2399 cpu);
2400 hardware_disable(NULL);
2401 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002402 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002403 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2404 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002405 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002406 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002407 case CPU_ONLINE:
2408 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2409 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002410 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002411 break;
2412 }
2413 return NOTIFY_OK;
2414}
2415
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002416
2417asmlinkage void kvm_handle_fault_on_reboot(void)
2418{
2419 if (kvm_rebooting)
2420 /* spin while reset goes on */
2421 while (true)
2422 ;
2423 /* Fault while not rebooting. We want the trace. */
2424 BUG();
2425}
2426EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2427
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002428static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002429 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002430{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002431 /*
2432 * Some (well, at least mine) BIOSes hang on reboot if
2433 * in vmx root mode.
2434 *
2435 * And Intel TXT required VMX off for all cpu when system shutdown.
2436 */
2437 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2438 kvm_rebooting = true;
2439 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002440 return NOTIFY_OK;
2441}
2442
2443static struct notifier_block kvm_reboot_notifier = {
2444 .notifier_call = kvm_reboot,
2445 .priority = 0,
2446};
2447
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002448void kvm_io_bus_init(struct kvm_io_bus *bus)
2449{
2450 memset(bus, 0, sizeof(*bus));
2451}
2452
2453void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2454{
2455 int i;
2456
2457 for (i = 0; i < bus->dev_count; i++) {
2458 struct kvm_io_device *pos = bus->devs[i];
2459
2460 kvm_iodevice_destructor(pos);
2461 }
2462}
2463
Laurent Vivier92760492008-05-30 16:05:53 +02002464struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2465 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002466{
2467 int i;
2468
2469 for (i = 0; i < bus->dev_count; i++) {
2470 struct kvm_io_device *pos = bus->devs[i];
2471
Laurent Vivier92760492008-05-30 16:05:53 +02002472 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002473 return pos;
2474 }
2475
2476 return NULL;
2477}
2478
2479void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2480{
2481 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2482
2483 bus->devs[bus->dev_count++] = dev;
2484}
2485
Avi Kivity774c47f2007-02-12 00:54:47 -08002486static struct notifier_block kvm_cpu_notifier = {
2487 .notifier_call = kvm_cpu_hotplug,
2488 .priority = 20, /* must be > scheduler priority */
2489};
2490
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002491static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002492{
2493 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002494 struct kvm *kvm;
2495
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002496 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002497 spin_lock(&kvm_lock);
2498 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002499 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002500 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002501 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002502}
2503
2504DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2505
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002506static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002507{
2508 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002509 struct kvm *kvm;
2510 struct kvm_vcpu *vcpu;
2511 int i;
2512
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002513 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002514 spin_lock(&kvm_lock);
2515 list_for_each_entry(kvm, &vm_list, vm_list)
2516 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002517 vcpu = kvm->vcpus[i];
2518 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002519 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002520 }
2521 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002522 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002523}
2524
Avi Kivityba1389b2007-11-18 16:24:12 +02002525DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2526
2527static struct file_operations *stat_fops[] = {
2528 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2529 [KVM_STAT_VM] = &vm_stat_fops,
2530};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002531
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002532static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002533{
2534 struct kvm_stats_debugfs_item *p;
2535
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002536 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002538 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002539 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002540 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541}
2542
2543static void kvm_exit_debug(void)
2544{
2545 struct kvm_stats_debugfs_item *p;
2546
2547 for (p = debugfs_entries; p->name; ++p)
2548 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002549 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550}
2551
Avi Kivity59ae6c62007-02-12 00:54:48 -08002552static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2553{
Avi Kivity4267c412007-05-24 13:09:41 +03002554 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002555 return 0;
2556}
2557
2558static int kvm_resume(struct sys_device *dev)
2559{
Avi Kivity4267c412007-05-24 13:09:41 +03002560 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002561 return 0;
2562}
2563
2564static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002565 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002566 .suspend = kvm_suspend,
2567 .resume = kvm_resume,
2568};
2569
2570static struct sys_device kvm_sysdev = {
2571 .id = 0,
2572 .cls = &kvm_sysdev_class,
2573};
2574
Izik Eiduscea7bb22007-10-17 19:17:48 +02002575struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002576pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002577
Avi Kivity15ad7142007-07-11 18:17:21 +03002578static inline
2579struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2580{
2581 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2582}
2583
2584static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2585{
2586 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2587
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002588 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002589}
2590
2591static void kvm_sched_out(struct preempt_notifier *pn,
2592 struct task_struct *next)
2593{
2594 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2595
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002596 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002597}
2598
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002599int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002600 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601{
2602 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002603 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002605 kvm_init_debug();
2606
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002607 r = kvm_arch_init(opaque);
2608 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002609 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002610
2611 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2612
2613 if (bad_page == NULL) {
2614 r = -ENOMEM;
2615 goto out;
2616 }
2617
Anthony Liguori35149e22008-04-02 14:46:56 -05002618 bad_pfn = page_to_pfn(bad_page);
2619
Avi Kivity8437a612009-06-06 14:52:35 -07002620 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302621 r = -ENOMEM;
2622 goto out_free_0;
2623 }
2624
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002625 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302627 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628
Yang, Sheng002c7f72007-07-31 14:23:01 +03002629 for_each_online_cpu(cpu) {
2630 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002631 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002632 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002633 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002634 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002635 }
2636
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002637 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002638 r = register_cpu_notifier(&kvm_cpu_notifier);
2639 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002640 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 register_reboot_notifier(&kvm_reboot_notifier);
2642
Avi Kivity59ae6c62007-02-12 00:54:48 -08002643 r = sysdev_class_register(&kvm_sysdev_class);
2644 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002645 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002646
2647 r = sysdev_register(&kvm_sysdev);
2648 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002649 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002650
Rusty Russellc16f8622007-07-30 21:12:19 +10002651 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2652 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002653 __alignof__(struct kvm_vcpu),
2654 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002655 if (!kvm_vcpu_cache) {
2656 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002657 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002658 }
2659
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002661 kvm_vm_fops.owner = module;
2662 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663
2664 r = misc_register(&kvm_dev);
2665 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002666 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 goto out_free;
2668 }
2669
Avi Kivity15ad7142007-07-11 18:17:21 +03002670 kvm_preempt_ops.sched_in = kvm_sched_in;
2671 kvm_preempt_ops.sched_out = kvm_sched_out;
2672
Avi Kivityc7addb92007-09-16 18:58:32 +02002673 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674
2675out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002676 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002677out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002678 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002679out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002680 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002681out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002683 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002684out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002685 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002686out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002687 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302688out_free_0a:
2689 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002690out_free_0:
2691 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002692out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002693 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002694 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002695out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696 return r;
2697}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002698EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002700void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002702 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002704 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002705 sysdev_unregister(&kvm_sysdev);
2706 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002708 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002709 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002710 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002711 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302713 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002714 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002716EXPORT_SYMBOL_GPL(kvm_exit);