blob: 0d481b28244805062194c4b1bec5c06064f9a9fd [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
Marcelo Tosattifa40a822009-06-04 15:08:24 -030065/*
66 * Ordering of locks:
67 *
68 * kvm->lock --> kvm->irq_lock
69 */
70
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080071DEFINE_SPINLOCK(kvm_lock);
72LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080073
Rusty Russell7f59f492008-12-07 21:25:45 +103074static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030075
Rusty Russellc16f8622007-07-30 21:12:19 +100076struct kmem_cache *kvm_vcpu_cache;
77EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030078
Avi Kivity15ad7142007-07-11 18:17:21 +030079static __read_mostly struct preempt_ops kvm_preempt_ops;
80
Hollis Blanchard76f7c872008-04-15 16:05:42 -050081struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080082
Avi Kivitybccf2152007-02-21 18:04:26 +020083static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
84 unsigned long arg);
85
Hannes Edere8ba5d32008-11-28 17:02:42 +010086static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030087
Xiantao Zhang8a98f662008-10-06 13:47:38 +080088#ifdef KVM_CAP_DEVICE_ASSIGNMENT
89static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
90 int assigned_dev_id)
91{
92 struct list_head *ptr;
93 struct kvm_assigned_dev_kernel *match;
94
95 list_for_each(ptr, head) {
96 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
97 if (match->assigned_dev_id == assigned_dev_id)
98 return match;
99 }
100 return NULL;
101}
102
Sheng Yang2350bd12009-02-25 17:22:27 +0800103static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
104 *assigned_dev, int irq)
105{
106 int i, index;
107 struct msix_entry *host_msix_entries;
108
109 host_msix_entries = assigned_dev->host_msix_entries;
110
111 index = -1;
112 for (i = 0; i < assigned_dev->entries_nr; i++)
113 if (irq == host_msix_entries[i].vector) {
114 index = i;
115 break;
116 }
117 if (index < 0) {
118 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
119 return 0;
120 }
121
122 return index;
123}
124
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800125static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
126{
127 struct kvm_assigned_dev_kernel *assigned_dev;
Sheng Yang2350bd12009-02-25 17:22:27 +0800128 struct kvm *kvm;
Sheng Yang968a6342009-04-30 10:58:42 +0800129 int i;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800130
131 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
132 interrupt_work);
Sheng Yang2350bd12009-02-25 17:22:27 +0800133 kvm = assigned_dev->kvm;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800134
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300135 mutex_lock(&kvm->irq_lock);
Marcelo Tosatti547de292009-05-07 17:55:13 -0300136 spin_lock_irq(&assigned_dev->assigned_dev_lock);
Sheng Yange56d5322009-03-12 21:45:39 +0800137 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800138 struct kvm_guest_msix_entry *guest_entries =
139 assigned_dev->guest_msix_entries;
140 for (i = 0; i < assigned_dev->entries_nr; i++) {
141 if (!(guest_entries[i].flags &
142 KVM_ASSIGNED_MSIX_PENDING))
143 continue;
144 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
145 kvm_set_irq(assigned_dev->kvm,
146 assigned_dev->irq_source_id,
147 guest_entries[i].vector, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800148 }
Sheng Yang968a6342009-04-30 10:58:42 +0800149 } else
Sheng Yang2350bd12009-02-25 17:22:27 +0800150 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
151 assigned_dev->guest_irq, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800152
Marcelo Tosatti547de292009-05-07 17:55:13 -0300153 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300154 mutex_unlock(&assigned_dev->kvm->irq_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800155}
156
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800157static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
158{
Marcelo Tosatti547de292009-05-07 17:55:13 -0300159 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800160 struct kvm_assigned_dev_kernel *assigned_dev =
161 (struct kvm_assigned_dev_kernel *) dev_id;
162
Marcelo Tosatti547de292009-05-07 17:55:13 -0300163 spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
Sheng Yange56d5322009-03-12 21:45:39 +0800164 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800165 int index = find_index_from_host_irq(assigned_dev, irq);
166 if (index < 0)
Marcelo Tosatti547de292009-05-07 17:55:13 -0300167 goto out;
Sheng Yang2350bd12009-02-25 17:22:27 +0800168 assigned_dev->guest_msix_entries[index].flags |=
169 KVM_ASSIGNED_MSIX_PENDING;
170 }
171
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800172 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000173
Sheng Yang968a6342009-04-30 10:58:42 +0800174 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
175 disable_irq_nosync(irq);
176 assigned_dev->host_irq_disabled = true;
177 }
Mark McLoughlindefaf152008-12-02 12:16:33 +0000178
Marcelo Tosatti547de292009-05-07 17:55:13 -0300179out:
180 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800181 return IRQ_HANDLED;
182}
183
184/* Ack the irq line for an assigned device */
185static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
186{
187 struct kvm_assigned_dev_kernel *dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300188 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800189
190 if (kian->gsi == -1)
191 return;
192
193 dev = container_of(kian, struct kvm_assigned_dev_kernel,
194 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000195
Sheng Yang5550af42008-10-15 20:15:06 +0800196 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000197
198 /* The guest irq may be shared so this ack may be
199 * from another device.
200 */
Marcelo Tosatti547de292009-05-07 17:55:13 -0300201 spin_lock_irqsave(&dev->assigned_dev_lock, flags);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000202 if (dev->host_irq_disabled) {
203 enable_irq(dev->host_irq);
204 dev->host_irq_disabled = false;
205 }
Marcelo Tosatti547de292009-05-07 17:55:13 -0300206 spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800207}
208
Sheng Yange56d5322009-03-12 21:45:39 +0800209static void deassign_guest_irq(struct kvm *kvm,
210 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800211{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300212 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
Sheng Yange56d5322009-03-12 21:45:39 +0800213 assigned_dev->ack_notifier.gsi = -1;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000214
215 if (assigned_dev->irq_source_id != -1)
216 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
217 assigned_dev->irq_source_id = -1;
Sheng Yange56d5322009-03-12 21:45:39 +0800218 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
219}
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800220
Sheng Yange56d5322009-03-12 21:45:39 +0800221/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
222static void deassign_host_irq(struct kvm *kvm,
223 struct kvm_assigned_dev_kernel *assigned_dev)
224{
Sheng Yangba4cef32009-01-06 10:03:03 +0800225 /*
226 * In kvm_free_device_irq, cancel_work_sync return true if:
227 * 1. work is scheduled, and then cancelled.
228 * 2. work callback is executed.
229 *
230 * The first one ensured that the irq is disabled and no more events
231 * would happen. But for the second one, the irq may be enabled (e.g.
232 * for MSI). So we disable irq here to prevent further events.
233 *
234 * Notice this maybe result in nested disable if the interrupt type is
235 * INTx, but it's OK for we are going to free it.
236 *
237 * If this function is a part of VM destroy, please ensure that till
238 * now, the kvm state is still legal for probably we also have to wait
239 * interrupt_work done.
240 */
Sheng Yange56d5322009-03-12 21:45:39 +0800241 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yangd510d6c2009-02-25 17:22:28 +0800242 int i;
243 for (i = 0; i < assigned_dev->entries_nr; i++)
244 disable_irq_nosync(assigned_dev->
245 host_msix_entries[i].vector);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800246
Sheng Yangd510d6c2009-02-25 17:22:28 +0800247 cancel_work_sync(&assigned_dev->interrupt_work);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000248
Sheng Yangd510d6c2009-02-25 17:22:28 +0800249 for (i = 0; i < assigned_dev->entries_nr; i++)
250 free_irq(assigned_dev->host_msix_entries[i].vector,
251 (void *)assigned_dev);
252
253 assigned_dev->entries_nr = 0;
254 kfree(assigned_dev->host_msix_entries);
255 kfree(assigned_dev->guest_msix_entries);
256 pci_disable_msix(assigned_dev->dev);
257 } else {
258 /* Deal with MSI and INTx */
259 disable_irq_nosync(assigned_dev->host_irq);
260 cancel_work_sync(&assigned_dev->interrupt_work);
261
262 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
263
Sheng Yange56d5322009-03-12 21:45:39 +0800264 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800265 pci_disable_msi(assigned_dev->dev);
266 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000267
Sheng Yange56d5322009-03-12 21:45:39 +0800268 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000269}
270
Sheng Yange56d5322009-03-12 21:45:39 +0800271static int kvm_deassign_irq(struct kvm *kvm,
272 struct kvm_assigned_dev_kernel *assigned_dev,
273 unsigned long irq_requested_type)
274{
275 unsigned long guest_irq_type, host_irq_type;
276
277 if (!irqchip_in_kernel(kvm))
278 return -EINVAL;
279 /* no irq assignment to deassign */
280 if (!assigned_dev->irq_requested_type)
281 return -ENXIO;
282
283 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
284 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
285
286 if (host_irq_type)
287 deassign_host_irq(kvm, assigned_dev);
288 if (guest_irq_type)
289 deassign_guest_irq(kvm, assigned_dev);
290
291 return 0;
292}
293
294static void kvm_free_assigned_irq(struct kvm *kvm,
295 struct kvm_assigned_dev_kernel *assigned_dev)
296{
297 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
298}
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000299
300static void kvm_free_assigned_device(struct kvm *kvm,
301 struct kvm_assigned_dev_kernel
302 *assigned_dev)
303{
304 kvm_free_assigned_irq(kvm, assigned_dev);
305
Sheng Yang6eb55812008-10-31 12:37:41 +0800306 pci_reset_function(assigned_dev->dev);
307
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800308 pci_release_regions(assigned_dev->dev);
309 pci_disable_device(assigned_dev->dev);
310 pci_dev_put(assigned_dev->dev);
311
312 list_del(&assigned_dev->list);
313 kfree(assigned_dev);
314}
315
316void kvm_free_all_assigned_devices(struct kvm *kvm)
317{
318 struct list_head *ptr, *ptr2;
319 struct kvm_assigned_dev_kernel *assigned_dev;
320
321 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
322 assigned_dev = list_entry(ptr,
323 struct kvm_assigned_dev_kernel,
324 list);
325
326 kvm_free_assigned_device(kvm, assigned_dev);
327 }
328}
329
Sheng Yange56d5322009-03-12 21:45:39 +0800330static int assigned_device_enable_host_intx(struct kvm *kvm,
331 struct kvm_assigned_dev_kernel *dev)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800332{
Sheng Yange56d5322009-03-12 21:45:39 +0800333 dev->host_irq = dev->dev->irq;
334 /* Even though this is PCI, we don't want to use shared
335 * interrupts. Sharing host devices with guest-assigned devices
336 * on the same interrupt line is not a happy situation: there
337 * are going to be long delays in accepting, acking, etc.
338 */
339 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
340 0, "kvm_assigned_intx_device", (void *)dev))
341 return -EIO;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800342 return 0;
343}
344
Sheng Yange56d5322009-03-12 21:45:39 +0800345#ifdef __KVM_HAVE_MSI
346static int assigned_device_enable_host_msi(struct kvm *kvm,
347 struct kvm_assigned_dev_kernel *dev)
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800348{
349 int r;
350
Sheng Yange56d5322009-03-12 21:45:39 +0800351 if (!dev->dev->msi_enabled) {
352 r = pci_enable_msi(dev->dev);
353 if (r)
354 return r;
Sheng Yang5319c662008-11-24 14:32:57 +0800355 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800356
Sheng Yange56d5322009-03-12 21:45:39 +0800357 dev->host_irq = dev->dev->irq;
358 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
359 "kvm_assigned_msi_device", (void *)dev)) {
360 pci_disable_msi(dev->dev);
361 return -EIO;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800362 }
363
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800364 return 0;
365}
366#endif
367
Sheng Yangd510d6c2009-02-25 17:22:28 +0800368#ifdef __KVM_HAVE_MSIX
Sheng Yange56d5322009-03-12 21:45:39 +0800369static int assigned_device_enable_host_msix(struct kvm *kvm,
370 struct kvm_assigned_dev_kernel *dev)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800371{
Sheng Yange56d5322009-03-12 21:45:39 +0800372 int i, r = -EINVAL;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800373
Sheng Yange56d5322009-03-12 21:45:39 +0800374 /* host_msix_entries and guest_msix_entries should have been
375 * initialized */
376 if (dev->entries_nr == 0)
377 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800378
Sheng Yange56d5322009-03-12 21:45:39 +0800379 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
380 if (r)
381 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800382
Sheng Yange56d5322009-03-12 21:45:39 +0800383 for (i = 0; i < dev->entries_nr; i++) {
384 r = request_irq(dev->host_msix_entries[i].vector,
385 kvm_assigned_dev_intr, 0,
386 "kvm_assigned_msix_device",
387 (void *)dev);
388 /* FIXME: free requested_irq's on failure */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800389 if (r)
390 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800391 }
392
Sheng Yange56d5322009-03-12 21:45:39 +0800393 return 0;
394}
Sheng Yangd510d6c2009-02-25 17:22:28 +0800395
Sheng Yange56d5322009-03-12 21:45:39 +0800396#endif
397
398static int assigned_device_enable_guest_intx(struct kvm *kvm,
399 struct kvm_assigned_dev_kernel *dev,
400 struct kvm_assigned_irq *irq)
401{
402 dev->guest_irq = irq->guest_irq;
403 dev->ack_notifier.gsi = irq->guest_irq;
404 return 0;
405}
406
407#ifdef __KVM_HAVE_MSI
408static int assigned_device_enable_guest_msi(struct kvm *kvm,
409 struct kvm_assigned_dev_kernel *dev,
410 struct kvm_assigned_irq *irq)
411{
412 dev->guest_irq = irq->guest_irq;
413 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800414 dev->host_irq_disabled = false;
Sheng Yange56d5322009-03-12 21:45:39 +0800415 return 0;
416}
417#endif
418#ifdef __KVM_HAVE_MSIX
419static int assigned_device_enable_guest_msix(struct kvm *kvm,
420 struct kvm_assigned_dev_kernel *dev,
421 struct kvm_assigned_irq *irq)
422{
423 dev->guest_irq = irq->guest_irq;
424 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800425 dev->host_irq_disabled = false;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800426 return 0;
427}
428#endif
429
Sheng Yange56d5322009-03-12 21:45:39 +0800430static int assign_host_irq(struct kvm *kvm,
431 struct kvm_assigned_dev_kernel *dev,
432 __u32 host_irq_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800433{
Sheng Yange56d5322009-03-12 21:45:39 +0800434 int r = -EEXIST;
435
436 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
437 return r;
438
439 switch (host_irq_type) {
440 case KVM_DEV_IRQ_HOST_INTX:
441 r = assigned_device_enable_host_intx(kvm, dev);
442 break;
443#ifdef __KVM_HAVE_MSI
444 case KVM_DEV_IRQ_HOST_MSI:
445 r = assigned_device_enable_host_msi(kvm, dev);
446 break;
447#endif
448#ifdef __KVM_HAVE_MSIX
449 case KVM_DEV_IRQ_HOST_MSIX:
450 r = assigned_device_enable_host_msix(kvm, dev);
451 break;
452#endif
453 default:
454 r = -EINVAL;
455 }
456
457 if (!r)
458 dev->irq_requested_type |= host_irq_type;
459
460 return r;
461}
462
463static int assign_guest_irq(struct kvm *kvm,
464 struct kvm_assigned_dev_kernel *dev,
465 struct kvm_assigned_irq *irq,
466 unsigned long guest_irq_type)
467{
468 int id;
469 int r = -EEXIST;
470
471 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
472 return r;
473
474 id = kvm_request_irq_source_id(kvm);
475 if (id < 0)
476 return id;
477
478 dev->irq_source_id = id;
479
480 switch (guest_irq_type) {
481 case KVM_DEV_IRQ_GUEST_INTX:
482 r = assigned_device_enable_guest_intx(kvm, dev, irq);
483 break;
484#ifdef __KVM_HAVE_MSI
485 case KVM_DEV_IRQ_GUEST_MSI:
486 r = assigned_device_enable_guest_msi(kvm, dev, irq);
487 break;
488#endif
489#ifdef __KVM_HAVE_MSIX
490 case KVM_DEV_IRQ_GUEST_MSIX:
491 r = assigned_device_enable_guest_msix(kvm, dev, irq);
492 break;
493#endif
494 default:
495 r = -EINVAL;
496 }
497
498 if (!r) {
499 dev->irq_requested_type |= guest_irq_type;
500 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
501 } else
502 kvm_free_irq_source_id(kvm, dev->irq_source_id);
503
504 return r;
505}
506
507/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
508static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
509 struct kvm_assigned_irq *assigned_irq)
510{
511 int r = -EINVAL;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800512 struct kvm_assigned_dev_kernel *match;
Sheng Yange56d5322009-03-12 21:45:39 +0800513 unsigned long host_irq_type, guest_irq_type;
514
515 if (!capable(CAP_SYS_RAWIO))
516 return -EPERM;
517
518 if (!irqchip_in_kernel(kvm))
519 return r;
520
521 mutex_lock(&kvm->lock);
522 r = -ENODEV;
523 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
524 assigned_irq->assigned_dev_id);
525 if (!match)
526 goto out;
527
528 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
529 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
530
531 r = -EINVAL;
532 /* can only assign one type at a time */
533 if (hweight_long(host_irq_type) > 1)
534 goto out;
535 if (hweight_long(guest_irq_type) > 1)
536 goto out;
537 if (host_irq_type == 0 && guest_irq_type == 0)
538 goto out;
539
540 r = 0;
541 if (host_irq_type)
542 r = assign_host_irq(kvm, match, host_irq_type);
543 if (r)
544 goto out;
545
546 if (guest_irq_type)
547 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
548out:
549 mutex_unlock(&kvm->lock);
550 return r;
551}
552
553static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
554 struct kvm_assigned_irq
555 *assigned_irq)
556{
557 int r = -ENODEV;
558 struct kvm_assigned_dev_kernel *match;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800559
560 mutex_lock(&kvm->lock);
561
562 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
563 assigned_irq->assigned_dev_id);
Sheng Yange56d5322009-03-12 21:45:39 +0800564 if (!match)
565 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800566
Sheng Yange56d5322009-03-12 21:45:39 +0800567 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
568out:
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800569 mutex_unlock(&kvm->lock);
570 return r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800571}
572
573static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
574 struct kvm_assigned_pci_dev *assigned_dev)
575{
576 int r = 0;
577 struct kvm_assigned_dev_kernel *match;
578 struct pci_dev *dev;
579
Mark McLoughlin682edb42009-02-05 18:23:46 +0000580 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800581 mutex_lock(&kvm->lock);
582
583 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
584 assigned_dev->assigned_dev_id);
585 if (match) {
586 /* device already assigned */
Sheng Yange56d5322009-03-12 21:45:39 +0800587 r = -EEXIST;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800588 goto out;
589 }
590
591 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
592 if (match == NULL) {
593 printk(KERN_INFO "%s: Couldn't allocate memory\n",
594 __func__);
595 r = -ENOMEM;
596 goto out;
597 }
598 dev = pci_get_bus_and_slot(assigned_dev->busnr,
599 assigned_dev->devfn);
600 if (!dev) {
601 printk(KERN_INFO "%s: host device not found\n", __func__);
602 r = -EINVAL;
603 goto out_free;
604 }
605 if (pci_enable_device(dev)) {
606 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
607 r = -EBUSY;
608 goto out_put;
609 }
610 r = pci_request_regions(dev, "kvm_assigned_device");
611 if (r) {
612 printk(KERN_INFO "%s: Could not get access to device regions\n",
613 __func__);
614 goto out_disable;
615 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800616
617 pci_reset_function(dev);
618
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800619 match->assigned_dev_id = assigned_dev->assigned_dev_id;
620 match->host_busnr = assigned_dev->busnr;
621 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800622 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800623 match->dev = dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300624 spin_lock_init(&match->assigned_dev_lock);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000625 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800626 match->kvm = kvm;
Sheng Yange56d5322009-03-12 21:45:39 +0800627 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
628 INIT_WORK(&match->interrupt_work,
629 kvm_assigned_dev_interrupt_work_handler);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800630
631 list_add(&match->list, &kvm->arch.assigned_dev_head);
632
633 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100634 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800635 r = kvm_iommu_map_guest(kvm);
636 if (r)
637 goto out_list_del;
638 }
639 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800640 if (r)
641 goto out_list_del;
642 }
643
644out:
645 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000646 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800647 return r;
648out_list_del:
649 list_del(&match->list);
650 pci_release_regions(dev);
651out_disable:
652 pci_disable_device(dev);
653out_put:
654 pci_dev_put(dev);
655out_free:
656 kfree(match);
657 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000658 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800659 return r;
660}
661#endif
662
Weidong Han0a920352008-12-02 21:24:23 +0800663#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
664static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
665 struct kvm_assigned_pci_dev *assigned_dev)
666{
667 int r = 0;
668 struct kvm_assigned_dev_kernel *match;
669
670 mutex_lock(&kvm->lock);
671
672 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
673 assigned_dev->assigned_dev_id);
674 if (!match) {
675 printk(KERN_INFO "%s: device hasn't been assigned before, "
676 "so cannot be deassigned\n", __func__);
677 r = -EINVAL;
678 goto out;
679 }
680
Weidong Han4a906e42009-02-13 17:27:51 +0800681 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800682 kvm_deassign_device(kvm, match);
683
684 kvm_free_assigned_device(kvm, match);
685
686out:
687 mutex_unlock(&kvm->lock);
688 return r;
689}
690#endif
691
James Morris5aacf0c2006-12-22 01:04:55 -0800692static inline int valid_vcpu(int n)
693{
694 return likely(n >= 0 && n < KVM_MAX_VCPUS);
695}
696
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800697inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300698{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100699 if (pfn_valid(pfn)) {
700 struct page *page = compound_head(pfn_to_page(pfn));
701 return PageReserved(page);
702 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300703
704 return true;
705}
706
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707/*
708 * Switches to specified vcpu, until a matching vcpu_put()
709 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200710void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711{
Avi Kivity15ad7142007-07-11 18:17:21 +0300712 int cpu;
713
Avi Kivitybccf2152007-02-21 18:04:26 +0200714 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300715 cpu = get_cpu();
716 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200717 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300718 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200719}
720
Carsten Otte313a3dc2007-10-11 19:16:52 +0200721void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722{
Avi Kivity15ad7142007-07-11 18:17:21 +0300723 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200724 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300725 preempt_notifier_unregister(&vcpu->preempt_notifier);
726 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727 mutex_unlock(&vcpu->mutex);
728}
729
Avi Kivityd9e368d2007-06-07 19:18:30 +0300730static void ack_flush(void *_completed)
731{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300732}
733
Rusty Russell49846892008-12-08 20:26:24 +1030734static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300735{
Avi Kivity597a5f52008-07-20 14:24:22 +0300736 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030737 cpumask_var_t cpus;
738 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300739 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300740
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030741 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
742 cpumask_clear(cpus);
743
Avi Kivity597a5f52008-07-20 14:24:22 +0300744 me = get_cpu();
Marcelo Tosatti84261922009-06-17 10:53:47 -0300745 spin_lock(&kvm->requests_lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000746 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
747 vcpu = kvm->vcpus[i];
748 if (!vcpu)
749 continue;
Rusty Russell49846892008-12-08 20:26:24 +1030750 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300751 continue;
752 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030753 if (cpus != NULL && cpu != -1 && cpu != me)
754 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300755 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030756 if (unlikely(cpus == NULL))
757 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
758 else if (!cpumask_empty(cpus))
759 smp_call_function_many(cpus, ack_flush, NULL, 1);
760 else
761 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300762 spin_unlock(&kvm->requests_lock);
Avi Kivity597a5f52008-07-20 14:24:22 +0300763 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030764 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030765 return called;
766}
767
768void kvm_flush_remote_tlbs(struct kvm *kvm)
769{
770 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
771 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300772}
773
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500774void kvm_reload_remote_mmus(struct kvm *kvm)
775{
Rusty Russell49846892008-12-08 20:26:24 +1030776 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500777}
778
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000779int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
780{
781 struct page *page;
782 int r;
783
784 mutex_init(&vcpu->mutex);
785 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000786 vcpu->kvm = kvm;
787 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300788 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000789
790 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
791 if (!page) {
792 r = -ENOMEM;
793 goto fail;
794 }
795 vcpu->run = page_address(page);
796
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800797 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000798 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800799 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000800 return 0;
801
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000802fail_free_run:
803 free_page((unsigned long)vcpu->run);
804fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000805 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000806}
807EXPORT_SYMBOL_GPL(kvm_vcpu_init);
808
809void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
810{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800811 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000812 free_page((unsigned long)vcpu->run);
813}
814EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
815
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200816#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
817static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
818{
819 return container_of(mn, struct kvm, mmu_notifier);
820}
821
822static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
823 struct mm_struct *mm,
824 unsigned long address)
825{
826 struct kvm *kvm = mmu_notifier_to_kvm(mn);
827 int need_tlb_flush;
828
829 /*
830 * When ->invalidate_page runs, the linux pte has been zapped
831 * already but the page is still allocated until
832 * ->invalidate_page returns. So if we increase the sequence
833 * here the kvm page fault will notice if the spte can't be
834 * established because the page is going to be freed. If
835 * instead the kvm page fault establishes the spte before
836 * ->invalidate_page runs, kvm_unmap_hva will release it
837 * before returning.
838 *
839 * The sequence increase only need to be seen at spin_unlock
840 * time, and not at spin_lock time.
841 *
842 * Increasing the sequence after the spin_unlock would be
843 * unsafe because the kvm page fault could then establish the
844 * pte after kvm_unmap_hva returned, without noticing the page
845 * is going to be freed.
846 */
847 spin_lock(&kvm->mmu_lock);
848 kvm->mmu_notifier_seq++;
849 need_tlb_flush = kvm_unmap_hva(kvm, address);
850 spin_unlock(&kvm->mmu_lock);
851
852 /* we've to flush the tlb before the pages can be freed */
853 if (need_tlb_flush)
854 kvm_flush_remote_tlbs(kvm);
855
856}
857
858static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
859 struct mm_struct *mm,
860 unsigned long start,
861 unsigned long end)
862{
863 struct kvm *kvm = mmu_notifier_to_kvm(mn);
864 int need_tlb_flush = 0;
865
866 spin_lock(&kvm->mmu_lock);
867 /*
868 * The count increase must become visible at unlock time as no
869 * spte can be established without taking the mmu_lock and
870 * count is also read inside the mmu_lock critical section.
871 */
872 kvm->mmu_notifier_count++;
873 for (; start < end; start += PAGE_SIZE)
874 need_tlb_flush |= kvm_unmap_hva(kvm, start);
875 spin_unlock(&kvm->mmu_lock);
876
877 /* we've to flush the tlb before the pages can be freed */
878 if (need_tlb_flush)
879 kvm_flush_remote_tlbs(kvm);
880}
881
882static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
883 struct mm_struct *mm,
884 unsigned long start,
885 unsigned long end)
886{
887 struct kvm *kvm = mmu_notifier_to_kvm(mn);
888
889 spin_lock(&kvm->mmu_lock);
890 /*
891 * This sequence increase will notify the kvm page fault that
892 * the page that is going to be mapped in the spte could have
893 * been freed.
894 */
895 kvm->mmu_notifier_seq++;
896 /*
897 * The above sequence increase must be visible before the
898 * below count decrease but both values are read by the kvm
899 * page fault under mmu_lock spinlock so we don't need to add
900 * a smb_wmb() here in between the two.
901 */
902 kvm->mmu_notifier_count--;
903 spin_unlock(&kvm->mmu_lock);
904
905 BUG_ON(kvm->mmu_notifier_count < 0);
906}
907
908static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
909 struct mm_struct *mm,
910 unsigned long address)
911{
912 struct kvm *kvm = mmu_notifier_to_kvm(mn);
913 int young;
914
915 spin_lock(&kvm->mmu_lock);
916 young = kvm_age_hva(kvm, address);
917 spin_unlock(&kvm->mmu_lock);
918
919 if (young)
920 kvm_flush_remote_tlbs(kvm);
921
922 return young;
923}
924
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100925static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
926 struct mm_struct *mm)
927{
928 struct kvm *kvm = mmu_notifier_to_kvm(mn);
929 kvm_arch_flush_shadow(kvm);
930}
931
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200932static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
933 .invalidate_page = kvm_mmu_notifier_invalidate_page,
934 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
935 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
936 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100937 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200938};
939#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
940
Avi Kivityf17abe92007-02-21 19:28:04 +0200941static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800943 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200944#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
945 struct page *page;
946#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800948 if (IS_ERR(kvm))
949 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200950#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200951 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200952 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
953#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954
Laurent Vivier5f94c172008-05-30 16:05:54 +0200955#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
956 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
957 if (!page) {
958 kfree(kvm);
959 return ERR_PTR(-ENOMEM);
960 }
961 kvm->coalesced_mmio_ring =
962 (struct kvm_coalesced_mmio_ring *)page_address(page);
963#endif
964
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200965#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
966 {
967 int err;
968 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
969 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
970 if (err) {
971#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
972 put_page(page);
973#endif
974 kfree(kvm);
975 return ERR_PTR(err);
976 }
977 }
978#endif
979
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200980 kvm->mm = current->mm;
981 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500982 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300983 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300984 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskins721eecb2009-05-20 10:30:49 -0400985 kvm_irqfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800986 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300987 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400988 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200989 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300990 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000991 spin_lock(&kvm_lock);
992 list_add(&kvm->vm_list, &vm_list);
993 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200994#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
995 kvm_coalesced_mmio_init(kvm);
996#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800997out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200998 return kvm;
999}
1000
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001/*
1002 * Free any memory in @free but not in @dont.
1003 */
1004static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1005 struct kvm_memory_slot *dont)
1006{
Izik Eidus290fc382007-09-27 14:11:22 +02001007 if (!dont || free->rmap != dont->rmap)
1008 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009
1010 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1011 vfree(free->dirty_bitmap);
1012
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001013 if (!dont || free->lpage_info != dont->lpage_info)
1014 vfree(free->lpage_info);
1015
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001017 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001018 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001019 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020}
1021
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001022void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023{
1024 int i;
1025
1026 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001027 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028}
1029
Avi Kivityf17abe92007-02-21 19:28:04 +02001030static void kvm_destroy_vm(struct kvm *kvm)
1031{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001032 struct mm_struct *mm = kvm->mm;
1033
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001034 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001035 spin_lock(&kvm_lock);
1036 list_del(&kvm->vm_list);
1037 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001038 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001039 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001040 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001041#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1042 if (kvm->coalesced_mmio_ring != NULL)
1043 free_page((unsigned long)kvm->coalesced_mmio_ring);
1044#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001045#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1046 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +02001047#else
1048 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001049#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001050 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001051 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001052}
1053
Izik Eidusd39f13b2008-03-30 16:01:25 +03001054void kvm_get_kvm(struct kvm *kvm)
1055{
1056 atomic_inc(&kvm->users_count);
1057}
1058EXPORT_SYMBOL_GPL(kvm_get_kvm);
1059
1060void kvm_put_kvm(struct kvm *kvm)
1061{
1062 if (atomic_dec_and_test(&kvm->users_count))
1063 kvm_destroy_vm(kvm);
1064}
1065EXPORT_SYMBOL_GPL(kvm_put_kvm);
1066
1067
Avi Kivityf17abe92007-02-21 19:28:04 +02001068static int kvm_vm_release(struct inode *inode, struct file *filp)
1069{
1070 struct kvm *kvm = filp->private_data;
1071
Gregory Haskins721eecb2009-05-20 10:30:49 -04001072 kvm_irqfd_release(kvm);
1073
Izik Eidusd39f13b2008-03-30 16:01:25 +03001074 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 return 0;
1076}
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 * Allocate some memory and give it an address in the guest physical address
1080 * space.
1081 *
1082 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001083 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001084 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001086int __kvm_set_memory_region(struct kvm *kvm,
1087 struct kvm_userspace_memory_region *mem,
1088 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089{
1090 int r;
1091 gfn_t base_gfn;
Avi Kivityac045272009-06-08 15:52:39 +03001092 unsigned long npages, ugfn;
Avi Kivity09f8ca72009-06-08 15:55:21 +03001093 unsigned long largepages, i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094 struct kvm_memory_slot *memslot;
1095 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096
1097 r = -EINVAL;
1098 /* General sanity checks */
1099 if (mem->memory_size & (PAGE_SIZE - 1))
1100 goto out;
1101 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1102 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001103 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001104 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001105 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 goto out;
1107 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1108 goto out;
1109
1110 memslot = &kvm->memslots[mem->slot];
1111 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1112 npages = mem->memory_size >> PAGE_SHIFT;
1113
1114 if (!npages)
1115 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1116
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 new = old = *memslot;
1118
1119 new.base_gfn = base_gfn;
1120 new.npages = npages;
1121 new.flags = mem->flags;
1122
1123 /* Disallow changing a memory slot's size. */
1124 r = -EINVAL;
1125 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001126 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127
1128 /* Check for overlaps */
1129 r = -EEXIST;
1130 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1131 struct kvm_memory_slot *s = &kvm->memslots[i];
1132
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001133 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 continue;
1135 if (!((base_gfn + npages <= s->base_gfn) ||
1136 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001137 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140 /* Free page dirty bitmap if unneeded */
1141 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001142 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143
1144 r = -ENOMEM;
1145
1146 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001147#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001148 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001149 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001150
1151 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001152 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001153
Izik Eidus290fc382007-09-27 14:11:22 +02001154 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001155
Izik Eidus80b14b52007-10-25 11:54:04 +02001156 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001157 /*
1158 * hva_to_rmmap() serialzies with the mmu_lock and to be
1159 * safe it has to ignore memslots with !user_alloc &&
1160 * !userspace_addr.
1161 */
1162 if (user_alloc)
1163 new.userspace_addr = mem->userspace_addr;
1164 else
1165 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001167 if (npages && !new.lpage_info) {
Avi Kivity99894a72009-03-29 16:31:25 +03001168 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1169 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001170
1171 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1172
1173 if (!new.lpage_info)
1174 goto out_free;
1175
1176 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1177
1178 if (base_gfn % KVM_PAGES_PER_HPAGE)
1179 new.lpage_info[0].write_count = 1;
1180 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1181 new.lpage_info[largepages-1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +03001182 ugfn = new.userspace_addr >> PAGE_SHIFT;
1183 /*
1184 * If the gfn and userspace address are not aligned wrt each
1185 * other, disable large page support for this slot
1186 */
1187 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1))
1188 for (i = 0; i < largepages; ++i)
1189 new.lpage_info[i].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001190 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191
1192 /* Allocate page dirty bitmap if needed */
1193 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1194 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1195
1196 new.dirty_bitmap = vmalloc(dirty_bytes);
1197 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001198 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +03001200 if (old.npages)
1201 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202 }
Carsten Otteeff01142008-06-27 15:05:31 +02001203#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001205 if (!npages)
1206 kvm_arch_flush_shadow(kvm);
1207
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001208 spin_lock(&kvm->mmu_lock);
1209 if (mem->slot >= kvm->nmemslots)
1210 kvm->nmemslots = mem->slot + 1;
1211
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001213 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214
Zhang Xiantao0de10342007-11-20 16:25:04 +08001215 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1216 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001217 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001218 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001219 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001220 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001221 }
1222
Glauber Costa6f897242008-12-03 13:40:51 -02001223 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1224 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001225 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -02001226 if (!npages)
1227 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001228 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001229#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001230 /* map the pages in iommu page table */
1231 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1232 if (r)
1233 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001234#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235 return 0;
1236
Sheng Yangf78e0e22007-10-29 09:40:42 +08001237out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 kvm_free_physmem_slot(&new, &old);
1239out:
1240 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001241
1242}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001243EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1244
1245int kvm_set_memory_region(struct kvm *kvm,
1246 struct kvm_userspace_memory_region *mem,
1247 int user_alloc)
1248{
1249 int r;
1250
Izik Eidus72dc67a2008-02-10 18:04:15 +02001251 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001252 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001253 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001254 return r;
1255}
Izik Eidus210c7c42007-10-24 23:52:57 +02001256EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1257
Carsten Otte1fe779f2007-10-29 16:08:35 +01001258int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1259 struct
1260 kvm_userspace_memory_region *mem,
1261 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001262{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001263 if (mem->slot >= KVM_MEMORY_SLOTS)
1264 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001265 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266}
1267
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001268int kvm_get_dirty_log(struct kvm *kvm,
1269 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270{
1271 struct kvm_memory_slot *memslot;
1272 int r, i;
1273 int n;
1274 unsigned long any = 0;
1275
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 r = -EINVAL;
1277 if (log->slot >= KVM_MEMORY_SLOTS)
1278 goto out;
1279
1280 memslot = &kvm->memslots[log->slot];
1281 r = -ENOENT;
1282 if (!memslot->dirty_bitmap)
1283 goto out;
1284
Uri Lublincd1a4a92007-02-22 16:43:09 +02001285 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286
Uri Lublincd1a4a92007-02-22 16:43:09 +02001287 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288 any = memslot->dirty_bitmap[i];
1289
1290 r = -EFAULT;
1291 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1292 goto out;
1293
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001294 if (any)
1295 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296
1297 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 return r;
1300}
1301
Izik Eiduscea7bb22007-10-17 19:17:48 +02001302int is_error_page(struct page *page)
1303{
1304 return page == bad_page;
1305}
1306EXPORT_SYMBOL_GPL(is_error_page);
1307
Anthony Liguori35149e22008-04-02 14:46:56 -05001308int is_error_pfn(pfn_t pfn)
1309{
1310 return pfn == bad_pfn;
1311}
1312EXPORT_SYMBOL_GPL(is_error_pfn);
1313
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001314static inline unsigned long bad_hva(void)
1315{
1316 return PAGE_OFFSET;
1317}
1318
1319int kvm_is_error_hva(unsigned long addr)
1320{
1321 return addr == bad_hva();
1322}
1323EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1324
Izik Eidus28430992008-10-03 17:40:32 +03001325struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326{
1327 int i;
1328
1329 for (i = 0; i < kvm->nmemslots; ++i) {
1330 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1331
1332 if (gfn >= memslot->base_gfn
1333 && gfn < memslot->base_gfn + memslot->npages)
1334 return memslot;
1335 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001336 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337}
Izik Eidus28430992008-10-03 17:40:32 +03001338EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001339
1340struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1341{
1342 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001343 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001344}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345
Izik Eiduse0d62c72007-10-24 23:57:46 +02001346int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1347{
1348 int i;
1349
1350 gfn = unalias_gfn(kvm, gfn);
1351 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1352 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1353
1354 if (gfn >= memslot->base_gfn
1355 && gfn < memslot->base_gfn + memslot->npages)
1356 return 1;
1357 }
1358 return 0;
1359}
1360EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1361
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001362unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001363{
1364 struct kvm_memory_slot *slot;
1365
1366 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001367 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001368 if (!slot)
1369 return bad_hva();
1370 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1371}
Sheng Yang0d150292008-04-25 21:44:50 +08001372EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001373
Anthony Liguori35149e22008-04-02 14:46:56 -05001374pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001375{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001376 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001377 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001378 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001379 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001380
Avi Kivity60395222007-10-21 11:03:36 +02001381 might_sleep();
1382
Izik Eidus539cb662007-11-11 22:05:04 +02001383 addr = gfn_to_hva(kvm, gfn);
1384 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001385 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001386 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001387 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001388
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001389 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001390
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001391 if (unlikely(npages != 1)) {
1392 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001393
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001394 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001395 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001396
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001397 if (vma == NULL || addr < vma->vm_start ||
1398 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001399 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001400 get_page(bad_page);
1401 return page_to_pfn(bad_page);
1402 }
1403
1404 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001405 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001406 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001407 } else
1408 pfn = page_to_pfn(page[0]);
1409
1410 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001411}
1412
1413EXPORT_SYMBOL_GPL(gfn_to_pfn);
1414
1415struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1416{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001417 pfn_t pfn;
1418
1419 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001420 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001421 return pfn_to_page(pfn);
1422
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001423 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001424
1425 get_page(bad_page);
1426 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001427}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001428
Avi Kivity954bbbc2007-03-30 14:02:32 +03001429EXPORT_SYMBOL_GPL(gfn_to_page);
1430
Izik Eidusb4231d62007-11-20 11:49:33 +02001431void kvm_release_page_clean(struct page *page)
1432{
Anthony Liguori35149e22008-04-02 14:46:56 -05001433 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001434}
1435EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1436
Anthony Liguori35149e22008-04-02 14:46:56 -05001437void kvm_release_pfn_clean(pfn_t pfn)
1438{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001439 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001440 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001441}
1442EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1443
Izik Eidusb4231d62007-11-20 11:49:33 +02001444void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001445{
Anthony Liguori35149e22008-04-02 14:46:56 -05001446 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001447}
Izik Eidusb4231d62007-11-20 11:49:33 +02001448EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001449
Anthony Liguori35149e22008-04-02 14:46:56 -05001450void kvm_release_pfn_dirty(pfn_t pfn)
1451{
1452 kvm_set_pfn_dirty(pfn);
1453 kvm_release_pfn_clean(pfn);
1454}
1455EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1456
1457void kvm_set_page_dirty(struct page *page)
1458{
1459 kvm_set_pfn_dirty(page_to_pfn(page));
1460}
1461EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1462
1463void kvm_set_pfn_dirty(pfn_t pfn)
1464{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001465 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001466 struct page *page = pfn_to_page(pfn);
1467 if (!PageReserved(page))
1468 SetPageDirty(page);
1469 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001470}
1471EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1472
1473void kvm_set_pfn_accessed(pfn_t pfn)
1474{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001475 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001476 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001477}
1478EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1479
1480void kvm_get_pfn(pfn_t pfn)
1481{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001482 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001483 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001484}
1485EXPORT_SYMBOL_GPL(kvm_get_pfn);
1486
Izik Eidus195aefd2007-10-01 22:14:18 +02001487static int next_segment(unsigned long len, int offset)
1488{
1489 if (len > PAGE_SIZE - offset)
1490 return PAGE_SIZE - offset;
1491 else
1492 return len;
1493}
1494
1495int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1496 int len)
1497{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001498 int r;
1499 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001500
Izik Eiduse0506bc2007-11-11 22:10:22 +02001501 addr = gfn_to_hva(kvm, gfn);
1502 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001503 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001504 r = copy_from_user(data, (void __user *)addr + offset, len);
1505 if (r)
1506 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001507 return 0;
1508}
1509EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1510
1511int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1512{
1513 gfn_t gfn = gpa >> PAGE_SHIFT;
1514 int seg;
1515 int offset = offset_in_page(gpa);
1516 int ret;
1517
1518 while ((seg = next_segment(len, offset)) != 0) {
1519 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1520 if (ret < 0)
1521 return ret;
1522 offset = 0;
1523 len -= seg;
1524 data += seg;
1525 ++gfn;
1526 }
1527 return 0;
1528}
1529EXPORT_SYMBOL_GPL(kvm_read_guest);
1530
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001531int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1532 unsigned long len)
1533{
1534 int r;
1535 unsigned long addr;
1536 gfn_t gfn = gpa >> PAGE_SHIFT;
1537 int offset = offset_in_page(gpa);
1538
1539 addr = gfn_to_hva(kvm, gfn);
1540 if (kvm_is_error_hva(addr))
1541 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001542 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001543 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001544 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001545 if (r)
1546 return -EFAULT;
1547 return 0;
1548}
1549EXPORT_SYMBOL(kvm_read_guest_atomic);
1550
Izik Eidus195aefd2007-10-01 22:14:18 +02001551int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1552 int offset, int len)
1553{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001554 int r;
1555 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001556
Izik Eiduse0506bc2007-11-11 22:10:22 +02001557 addr = gfn_to_hva(kvm, gfn);
1558 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001559 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001560 r = copy_to_user((void __user *)addr + offset, data, len);
1561 if (r)
1562 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001563 mark_page_dirty(kvm, gfn);
1564 return 0;
1565}
1566EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1567
1568int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1569 unsigned long len)
1570{
1571 gfn_t gfn = gpa >> PAGE_SHIFT;
1572 int seg;
1573 int offset = offset_in_page(gpa);
1574 int ret;
1575
1576 while ((seg = next_segment(len, offset)) != 0) {
1577 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1578 if (ret < 0)
1579 return ret;
1580 offset = 0;
1581 len -= seg;
1582 data += seg;
1583 ++gfn;
1584 }
1585 return 0;
1586}
1587
1588int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1589{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001590 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001591}
1592EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1593
1594int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1595{
1596 gfn_t gfn = gpa >> PAGE_SHIFT;
1597 int seg;
1598 int offset = offset_in_page(gpa);
1599 int ret;
1600
1601 while ((seg = next_segment(len, offset)) != 0) {
1602 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1603 if (ret < 0)
1604 return ret;
1605 offset = 0;
1606 len -= seg;
1607 ++gfn;
1608 }
1609 return 0;
1610}
1611EXPORT_SYMBOL_GPL(kvm_clear_guest);
1612
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1614{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001615 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616
Uri Lublin3b6fff12007-10-30 10:42:09 +02001617 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001618 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001619 if (memslot && memslot->dirty_bitmap) {
1620 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621
Rusty Russell7e9d6192007-07-31 20:41:14 +10001622 /* avoid RMW */
1623 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1624 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 }
1626}
1627
Eddie Dongb6958ce2007-07-18 12:15:21 +03001628/*
1629 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1630 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001631void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001632{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001633 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001634
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001635 for (;;) {
1636 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001637
Gleb Natapov78646122009-03-23 12:12:11 +02001638 if ((kvm_arch_interrupt_allowed(vcpu) &&
1639 kvm_cpu_has_interrupt(vcpu)) ||
Gleb Natapov09cec752009-03-23 15:11:44 +02001640 kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001641 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001642 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001643 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001644 if (kvm_cpu_has_pending_timer(vcpu))
1645 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001646 if (signal_pending(current))
1647 break;
1648
Eddie Dongb6958ce2007-07-18 12:15:21 +03001649 vcpu_put(vcpu);
1650 schedule();
1651 vcpu_load(vcpu);
1652 }
1653
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001654 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001655}
1656
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657void kvm_resched(struct kvm_vcpu *vcpu)
1658{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001659 if (!need_resched())
1660 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662}
1663EXPORT_SYMBOL_GPL(kvm_resched);
1664
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001665static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001666{
1667 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001668 struct page *page;
1669
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001670 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001671 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001672#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001673 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001674 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001675#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001676#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1677 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1678 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1679#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001680 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001681 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001682 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001683 vmf->page = page;
1684 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001685}
1686
1687static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001688 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001689};
1690
1691static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1692{
1693 vma->vm_ops = &kvm_vcpu_vm_ops;
1694 return 0;
1695}
1696
Avi Kivitybccf2152007-02-21 18:04:26 +02001697static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1698{
1699 struct kvm_vcpu *vcpu = filp->private_data;
1700
Al Viro66c0b392008-04-19 20:33:56 +01001701 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001702 return 0;
1703}
1704
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001705static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001706 .release = kvm_vcpu_release,
1707 .unlocked_ioctl = kvm_vcpu_ioctl,
1708 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001709 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001710};
1711
1712/*
1713 * Allocates an inode for the vcpu.
1714 */
1715static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1716{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001717 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001718 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001719 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001720 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001721}
1722
Avi Kivityc5ea7662007-02-20 18:41:05 +02001723/*
1724 * Creates some virtual cpus. Good luck creating more than one.
1725 */
1726static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1727{
1728 int r;
1729 struct kvm_vcpu *vcpu;
1730
Avi Kivityc5ea7662007-02-20 18:41:05 +02001731 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001732 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001733
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001734 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001735 if (IS_ERR(vcpu))
1736 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001737
Avi Kivity15ad7142007-07-11 18:17:21 +03001738 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1739
Avi Kivity26e52152007-11-20 15:30:24 +02001740 r = kvm_arch_vcpu_setup(vcpu);
1741 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001742 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001743
Shaohua Li11ec2802007-07-23 14:51:37 +08001744 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001745 if (kvm->vcpus[n]) {
1746 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001747 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001748 }
1749 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001750 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001751
1752 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001753 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001754 r = create_vcpu_fd(vcpu);
1755 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001756 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001757 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001758
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001759unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001760 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001761 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001762vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001763 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001764 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001765 return r;
1766}
1767
Avi Kivity1961d272007-03-05 19:46:05 +02001768static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1769{
1770 if (sigset) {
1771 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1772 vcpu->sigset_active = 1;
1773 vcpu->sigset = *sigset;
1774 } else
1775 vcpu->sigset_active = 0;
1776 return 0;
1777}
1778
Sheng Yangc1e01512009-02-25 17:22:26 +08001779#ifdef __KVM_HAVE_MSIX
1780static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1781 struct kvm_assigned_msix_nr *entry_nr)
1782{
1783 int r = 0;
1784 struct kvm_assigned_dev_kernel *adev;
1785
1786 mutex_lock(&kvm->lock);
1787
1788 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1789 entry_nr->assigned_dev_id);
1790 if (!adev) {
1791 r = -EINVAL;
1792 goto msix_nr_out;
1793 }
1794
1795 if (adev->entries_nr == 0) {
1796 adev->entries_nr = entry_nr->entry_nr;
1797 if (adev->entries_nr == 0 ||
1798 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1799 r = -EINVAL;
1800 goto msix_nr_out;
1801 }
1802
1803 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1804 entry_nr->entry_nr,
1805 GFP_KERNEL);
1806 if (!adev->host_msix_entries) {
1807 r = -ENOMEM;
1808 goto msix_nr_out;
1809 }
1810 adev->guest_msix_entries = kzalloc(
1811 sizeof(struct kvm_guest_msix_entry) *
1812 entry_nr->entry_nr, GFP_KERNEL);
1813 if (!adev->guest_msix_entries) {
1814 kfree(adev->host_msix_entries);
1815 r = -ENOMEM;
1816 goto msix_nr_out;
1817 }
1818 } else /* Not allowed set MSI-X number twice */
1819 r = -EINVAL;
1820msix_nr_out:
1821 mutex_unlock(&kvm->lock);
1822 return r;
1823}
1824
1825static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1826 struct kvm_assigned_msix_entry *entry)
1827{
1828 int r = 0, i;
1829 struct kvm_assigned_dev_kernel *adev;
1830
1831 mutex_lock(&kvm->lock);
1832
1833 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1834 entry->assigned_dev_id);
1835
1836 if (!adev) {
1837 r = -EINVAL;
1838 goto msix_entry_out;
1839 }
1840
1841 for (i = 0; i < adev->entries_nr; i++)
1842 if (adev->guest_msix_entries[i].vector == 0 ||
1843 adev->guest_msix_entries[i].entry == entry->entry) {
1844 adev->guest_msix_entries[i].entry = entry->entry;
1845 adev->guest_msix_entries[i].vector = entry->gsi;
1846 adev->host_msix_entries[i].entry = entry->entry;
1847 break;
1848 }
1849 if (i == adev->entries_nr) {
1850 r = -ENOSPC;
1851 goto msix_entry_out;
1852 }
1853
1854msix_entry_out:
1855 mutex_unlock(&kvm->lock);
1856
1857 return r;
1858}
1859#endif
1860
Avi Kivitybccf2152007-02-21 18:04:26 +02001861static long kvm_vcpu_ioctl(struct file *filp,
1862 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863{
Avi Kivitybccf2152007-02-21 18:04:26 +02001864 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001865 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001866 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001867 struct kvm_fpu *fpu = NULL;
1868 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001870 if (vcpu->kvm->mm != current->mm)
1871 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001873 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001874 r = -EINVAL;
1875 if (arg)
1876 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001877 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001880 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001882 r = -ENOMEM;
1883 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1884 if (!kvm_regs)
1885 goto out;
1886 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001888 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001890 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1891 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001893out_free1:
1894 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895 break;
1896 }
1897 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001898 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001900 r = -ENOMEM;
1901 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1902 if (!kvm_regs)
1903 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001905 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1906 goto out_free2;
1907 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001909 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001911out_free2:
1912 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 break;
1914 }
1915 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001916 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1917 r = -ENOMEM;
1918 if (!kvm_sregs)
1919 goto out;
1920 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 if (r)
1922 goto out;
1923 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001924 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925 goto out;
1926 r = 0;
1927 break;
1928 }
1929 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001930 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1931 r = -ENOMEM;
1932 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001934 r = -EFAULT;
1935 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1936 goto out;
1937 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938 if (r)
1939 goto out;
1940 r = 0;
1941 break;
1942 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001943 case KVM_GET_MP_STATE: {
1944 struct kvm_mp_state mp_state;
1945
1946 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1947 if (r)
1948 goto out;
1949 r = -EFAULT;
1950 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1951 goto out;
1952 r = 0;
1953 break;
1954 }
1955 case KVM_SET_MP_STATE: {
1956 struct kvm_mp_state mp_state;
1957
1958 r = -EFAULT;
1959 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1960 goto out;
1961 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1962 if (r)
1963 goto out;
1964 r = 0;
1965 break;
1966 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967 case KVM_TRANSLATE: {
1968 struct kvm_translation tr;
1969
1970 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001971 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001973 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 if (r)
1975 goto out;
1976 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001977 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 goto out;
1979 r = 0;
1980 break;
1981 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001982 case KVM_SET_GUEST_DEBUG: {
1983 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984
1985 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001986 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001988 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 if (r)
1990 goto out;
1991 r = 0;
1992 break;
1993 }
Avi Kivity1961d272007-03-05 19:46:05 +02001994 case KVM_SET_SIGNAL_MASK: {
1995 struct kvm_signal_mask __user *sigmask_arg = argp;
1996 struct kvm_signal_mask kvm_sigmask;
1997 sigset_t sigset, *p;
1998
1999 p = NULL;
2000 if (argp) {
2001 r = -EFAULT;
2002 if (copy_from_user(&kvm_sigmask, argp,
2003 sizeof kvm_sigmask))
2004 goto out;
2005 r = -EINVAL;
2006 if (kvm_sigmask.len != sizeof sigset)
2007 goto out;
2008 r = -EFAULT;
2009 if (copy_from_user(&sigset, sigmask_arg->sigset,
2010 sizeof sigset))
2011 goto out;
2012 p = &sigset;
2013 }
2014 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2015 break;
2016 }
Avi Kivityb8836732007-04-01 16:34:31 +03002017 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002018 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2019 r = -ENOMEM;
2020 if (!fpu)
2021 goto out;
2022 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002023 if (r)
2024 goto out;
2025 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002026 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03002027 goto out;
2028 r = 0;
2029 break;
2030 }
2031 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002032 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2033 r = -ENOMEM;
2034 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002035 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002036 r = -EFAULT;
2037 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2038 goto out;
2039 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002040 if (r)
2041 goto out;
2042 r = 0;
2043 break;
2044 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002045 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002046 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002047 }
2048out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002049 kfree(fpu);
2050 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002051 return r;
2052}
2053
2054static long kvm_vm_ioctl(struct file *filp,
2055 unsigned int ioctl, unsigned long arg)
2056{
2057 struct kvm *kvm = filp->private_data;
2058 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002059 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002060
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002061 if (kvm->mm != current->mm)
2062 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002063 switch (ioctl) {
2064 case KVM_CREATE_VCPU:
2065 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2066 if (r < 0)
2067 goto out;
2068 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002069 case KVM_SET_USER_MEMORY_REGION: {
2070 struct kvm_userspace_memory_region kvm_userspace_mem;
2071
2072 r = -EFAULT;
2073 if (copy_from_user(&kvm_userspace_mem, argp,
2074 sizeof kvm_userspace_mem))
2075 goto out;
2076
2077 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 if (r)
2079 goto out;
2080 break;
2081 }
2082 case KVM_GET_DIRTY_LOG: {
2083 struct kvm_dirty_log log;
2084
2085 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002086 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002088 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089 if (r)
2090 goto out;
2091 break;
2092 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002093#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2094 case KVM_REGISTER_COALESCED_MMIO: {
2095 struct kvm_coalesced_mmio_zone zone;
2096 r = -EFAULT;
2097 if (copy_from_user(&zone, argp, sizeof zone))
2098 goto out;
2099 r = -ENXIO;
2100 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2101 if (r)
2102 goto out;
2103 r = 0;
2104 break;
2105 }
2106 case KVM_UNREGISTER_COALESCED_MMIO: {
2107 struct kvm_coalesced_mmio_zone zone;
2108 r = -EFAULT;
2109 if (copy_from_user(&zone, argp, sizeof zone))
2110 goto out;
2111 r = -ENXIO;
2112 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2113 if (r)
2114 goto out;
2115 r = 0;
2116 break;
2117 }
2118#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002119#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2120 case KVM_ASSIGN_PCI_DEVICE: {
2121 struct kvm_assigned_pci_dev assigned_dev;
2122
2123 r = -EFAULT;
2124 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2125 goto out;
2126 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2127 if (r)
2128 goto out;
2129 break;
2130 }
2131 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002132 r = -EOPNOTSUPP;
2133 break;
2134 }
2135#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2136 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002137 struct kvm_assigned_irq assigned_irq;
2138
2139 r = -EFAULT;
2140 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2141 goto out;
2142 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2143 if (r)
2144 goto out;
2145 break;
2146 }
Sheng Yange56d5322009-03-12 21:45:39 +08002147 case KVM_DEASSIGN_DEV_IRQ: {
2148 struct kvm_assigned_irq assigned_irq;
2149
2150 r = -EFAULT;
2151 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2152 goto out;
2153 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2154 if (r)
2155 goto out;
2156 break;
2157 }
2158#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002159#endif
Weidong Han0a920352008-12-02 21:24:23 +08002160#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2161 case KVM_DEASSIGN_PCI_DEVICE: {
2162 struct kvm_assigned_pci_dev assigned_dev;
2163
2164 r = -EFAULT;
2165 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2166 goto out;
2167 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2168 if (r)
2169 goto out;
2170 break;
2171 }
2172#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002173#ifdef KVM_CAP_IRQ_ROUTING
2174 case KVM_SET_GSI_ROUTING: {
2175 struct kvm_irq_routing routing;
2176 struct kvm_irq_routing __user *urouting;
2177 struct kvm_irq_routing_entry *entries;
2178
2179 r = -EFAULT;
2180 if (copy_from_user(&routing, argp, sizeof(routing)))
2181 goto out;
2182 r = -EINVAL;
2183 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2184 goto out;
2185 if (routing.flags)
2186 goto out;
2187 r = -ENOMEM;
2188 entries = vmalloc(routing.nr * sizeof(*entries));
2189 if (!entries)
2190 goto out;
2191 r = -EFAULT;
2192 urouting = argp;
2193 if (copy_from_user(entries, urouting->entries,
2194 routing.nr * sizeof(*entries)))
2195 goto out_free_irq_routing;
2196 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2197 routing.flags);
2198 out_free_irq_routing:
2199 vfree(entries);
2200 break;
2201 }
Sheng Yangc1e01512009-02-25 17:22:26 +08002202#ifdef __KVM_HAVE_MSIX
2203 case KVM_ASSIGN_SET_MSIX_NR: {
2204 struct kvm_assigned_msix_nr entry_nr;
2205 r = -EFAULT;
2206 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2207 goto out;
2208 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2209 if (r)
2210 goto out;
2211 break;
2212 }
2213 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2214 struct kvm_assigned_msix_entry entry;
2215 r = -EFAULT;
2216 if (copy_from_user(&entry, argp, sizeof entry))
2217 goto out;
2218 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2219 if (r)
2220 goto out;
2221 break;
2222 }
Avi Kivity399ec802008-11-19 13:58:46 +02002223#endif
Sheng Yangc1e01512009-02-25 17:22:26 +08002224#endif /* KVM_CAP_IRQ_ROUTING */
Gregory Haskins721eecb2009-05-20 10:30:49 -04002225 case KVM_IRQFD: {
2226 struct kvm_irqfd data;
2227
2228 r = -EFAULT;
2229 if (copy_from_user(&data, argp, sizeof data))
2230 goto out;
2231 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2232 break;
2233 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002234 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002235 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002236 }
2237out:
2238 return r;
2239}
2240
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002241static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002242{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002243 struct page *page[1];
2244 unsigned long addr;
2245 int npages;
2246 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002247 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002248
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002249 addr = gfn_to_hva(kvm, gfn);
2250 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002251 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002252
2253 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2254 NULL);
2255 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002256 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002257
2258 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002259 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002260}
2261
2262static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002263 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002264};
2265
2266static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2267{
2268 vma->vm_ops = &kvm_vm_vm_ops;
2269 return 0;
2270}
2271
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002272static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002273 .release = kvm_vm_release,
2274 .unlocked_ioctl = kvm_vm_ioctl,
2275 .compat_ioctl = kvm_vm_ioctl,
2276 .mmap = kvm_vm_mmap,
2277};
2278
2279static int kvm_dev_ioctl_create_vm(void)
2280{
Al Viro2030a422008-02-23 06:46:49 -05002281 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002282 struct kvm *kvm;
2283
Avi Kivityf17abe92007-02-21 19:28:04 +02002284 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002285 if (IS_ERR(kvm))
2286 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002287 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002288 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002289 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002290
Avi Kivityf17abe92007-02-21 19:28:04 +02002291 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002292}
2293
Avi Kivity1a811b62008-12-08 18:25:27 +02002294static long kvm_dev_ioctl_check_extension_generic(long arg)
2295{
2296 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002297 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002298 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002299 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Avi Kivity1a811b62008-12-08 18:25:27 +02002300 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002301#ifdef CONFIG_HAVE_KVM_IRQCHIP
2302 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002303 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002304#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002305 default:
2306 break;
2307 }
2308 return kvm_dev_ioctl_check_extension(arg);
2309}
2310
Avi Kivityf17abe92007-02-21 19:28:04 +02002311static long kvm_dev_ioctl(struct file *filp,
2312 unsigned int ioctl, unsigned long arg)
2313{
Avi Kivity07c45a32007-03-07 13:05:38 +02002314 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002315
2316 switch (ioctl) {
2317 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002318 r = -EINVAL;
2319 if (arg)
2320 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002321 r = KVM_API_VERSION;
2322 break;
2323 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002324 r = -EINVAL;
2325 if (arg)
2326 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002327 r = kvm_dev_ioctl_create_vm();
2328 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002329 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002330 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002331 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002332 case KVM_GET_VCPU_MMAP_SIZE:
2333 r = -EINVAL;
2334 if (arg)
2335 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002336 r = PAGE_SIZE; /* struct kvm_run */
2337#ifdef CONFIG_X86
2338 r += PAGE_SIZE; /* pio data page */
2339#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002340#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2341 r += PAGE_SIZE; /* coalesced mmio ring page */
2342#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002343 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002344 case KVM_TRACE_ENABLE:
2345 case KVM_TRACE_PAUSE:
2346 case KVM_TRACE_DISABLE:
2347 r = kvm_trace_ioctl(ioctl, arg);
2348 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002350 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 }
2352out:
2353 return r;
2354}
2355
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357 .unlocked_ioctl = kvm_dev_ioctl,
2358 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359};
2360
2361static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002362 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363 "kvm",
2364 &kvm_chardev_ops,
2365};
2366
Avi Kivity1b6c0162007-05-24 13:03:52 +03002367static void hardware_enable(void *junk)
2368{
2369 int cpu = raw_smp_processor_id();
2370
Rusty Russell7f59f492008-12-07 21:25:45 +10302371 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002372 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302373 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002374 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002375}
2376
2377static void hardware_disable(void *junk)
2378{
2379 int cpu = raw_smp_processor_id();
2380
Rusty Russell7f59f492008-12-07 21:25:45 +10302381 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002382 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302383 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002384 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002385}
2386
Avi Kivity774c47f2007-02-12 00:54:47 -08002387static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2388 void *v)
2389{
2390 int cpu = (long)v;
2391
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002392 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002393 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002394 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03002395 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2396 cpu);
2397 hardware_disable(NULL);
2398 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002399 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002400 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2401 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002402 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002403 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002404 case CPU_ONLINE:
2405 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2406 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002407 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002408 break;
2409 }
2410 return NOTIFY_OK;
2411}
2412
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002413
2414asmlinkage void kvm_handle_fault_on_reboot(void)
2415{
2416 if (kvm_rebooting)
2417 /* spin while reset goes on */
2418 while (true)
2419 ;
2420 /* Fault while not rebooting. We want the trace. */
2421 BUG();
2422}
2423EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2424
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002425static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002426 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002427{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002428 /*
2429 * Some (well, at least mine) BIOSes hang on reboot if
2430 * in vmx root mode.
2431 *
2432 * And Intel TXT required VMX off for all cpu when system shutdown.
2433 */
2434 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2435 kvm_rebooting = true;
2436 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002437 return NOTIFY_OK;
2438}
2439
2440static struct notifier_block kvm_reboot_notifier = {
2441 .notifier_call = kvm_reboot,
2442 .priority = 0,
2443};
2444
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002445void kvm_io_bus_init(struct kvm_io_bus *bus)
2446{
2447 memset(bus, 0, sizeof(*bus));
2448}
2449
2450void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2451{
2452 int i;
2453
2454 for (i = 0; i < bus->dev_count; i++) {
2455 struct kvm_io_device *pos = bus->devs[i];
2456
2457 kvm_iodevice_destructor(pos);
2458 }
2459}
2460
Laurent Vivier92760492008-05-30 16:05:53 +02002461struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2462 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002463{
2464 int i;
2465
2466 for (i = 0; i < bus->dev_count; i++) {
2467 struct kvm_io_device *pos = bus->devs[i];
2468
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002469 if (kvm_iodevice_in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002470 return pos;
2471 }
2472
2473 return NULL;
2474}
2475
2476void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2477{
2478 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2479
2480 bus->devs[bus->dev_count++] = dev;
2481}
2482
Avi Kivity774c47f2007-02-12 00:54:47 -08002483static struct notifier_block kvm_cpu_notifier = {
2484 .notifier_call = kvm_cpu_hotplug,
2485 .priority = 20, /* must be > scheduler priority */
2486};
2487
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002488static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002489{
2490 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002491 struct kvm *kvm;
2492
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002493 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002494 spin_lock(&kvm_lock);
2495 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002496 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002497 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002498 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002499}
2500
2501DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2502
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002503static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002504{
2505 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002506 struct kvm *kvm;
2507 struct kvm_vcpu *vcpu;
2508 int i;
2509
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002510 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002511 spin_lock(&kvm_lock);
2512 list_for_each_entry(kvm, &vm_list, vm_list)
2513 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002514 vcpu = kvm->vcpus[i];
2515 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002516 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002517 }
2518 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002519 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002520}
2521
Avi Kivityba1389b2007-11-18 16:24:12 +02002522DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2523
2524static struct file_operations *stat_fops[] = {
2525 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2526 [KVM_STAT_VM] = &vm_stat_fops,
2527};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002528
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002529static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530{
2531 struct kvm_stats_debugfs_item *p;
2532
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002533 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002535 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002536 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002537 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538}
2539
2540static void kvm_exit_debug(void)
2541{
2542 struct kvm_stats_debugfs_item *p;
2543
2544 for (p = debugfs_entries; p->name; ++p)
2545 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002546 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547}
2548
Avi Kivity59ae6c62007-02-12 00:54:48 -08002549static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2550{
Avi Kivity4267c412007-05-24 13:09:41 +03002551 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002552 return 0;
2553}
2554
2555static int kvm_resume(struct sys_device *dev)
2556{
Avi Kivity4267c412007-05-24 13:09:41 +03002557 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002558 return 0;
2559}
2560
2561static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002562 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002563 .suspend = kvm_suspend,
2564 .resume = kvm_resume,
2565};
2566
2567static struct sys_device kvm_sysdev = {
2568 .id = 0,
2569 .cls = &kvm_sysdev_class,
2570};
2571
Izik Eiduscea7bb22007-10-17 19:17:48 +02002572struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002573pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574
Avi Kivity15ad7142007-07-11 18:17:21 +03002575static inline
2576struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2577{
2578 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2579}
2580
2581static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2582{
2583 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2584
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002585 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002586}
2587
2588static void kvm_sched_out(struct preempt_notifier *pn,
2589 struct task_struct *next)
2590{
2591 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2592
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002593 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002594}
2595
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002596int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002597 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598{
2599 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002600 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002602 kvm_init_debug();
2603
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002604 r = kvm_arch_init(opaque);
2605 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002606 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002607
2608 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2609
2610 if (bad_page == NULL) {
2611 r = -ENOMEM;
2612 goto out;
2613 }
2614
Anthony Liguori35149e22008-04-02 14:46:56 -05002615 bad_pfn = page_to_pfn(bad_page);
2616
Avi Kivity8437a612009-06-06 14:52:35 -07002617 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302618 r = -ENOMEM;
2619 goto out_free_0;
2620 }
2621
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002622 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302624 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625
Yang, Sheng002c7f72007-07-31 14:23:01 +03002626 for_each_online_cpu(cpu) {
2627 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002628 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002629 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002630 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002631 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002632 }
2633
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002634 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002635 r = register_cpu_notifier(&kvm_cpu_notifier);
2636 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002637 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 register_reboot_notifier(&kvm_reboot_notifier);
2639
Avi Kivity59ae6c62007-02-12 00:54:48 -08002640 r = sysdev_class_register(&kvm_sysdev_class);
2641 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002642 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002643
2644 r = sysdev_register(&kvm_sysdev);
2645 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002646 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002647
Rusty Russellc16f8622007-07-30 21:12:19 +10002648 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2649 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002650 __alignof__(struct kvm_vcpu),
2651 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002652 if (!kvm_vcpu_cache) {
2653 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002654 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002655 }
2656
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002658 kvm_vm_fops.owner = module;
2659 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660
2661 r = misc_register(&kvm_dev);
2662 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002663 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 goto out_free;
2665 }
2666
Avi Kivity15ad7142007-07-11 18:17:21 +03002667 kvm_preempt_ops.sched_in = kvm_sched_in;
2668 kvm_preempt_ops.sched_out = kvm_sched_out;
2669
Avi Kivityc7addb92007-09-16 18:58:32 +02002670 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671
2672out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002673 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002674out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002675 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002676out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002677 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002678out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002680 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002681out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002682 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002683out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002684 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302685out_free_0a:
2686 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002687out_free_0:
2688 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002689out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002690 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002691 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002692out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 return r;
2694}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002695EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002697void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002699 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002701 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002702 sysdev_unregister(&kvm_sysdev);
2703 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002705 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002706 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002707 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002708 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302710 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002711 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002713EXPORT_SYMBOL_GPL(kvm_exit);