blob: c12c95b1b641299cbe65d2330e7068bd01cb8c92 [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
Marcelo Tosatti229456f2009-06-17 09:22:14 -030062#define CREATE_TRACE_POINTS
63#include <trace/events/kvm.h>
64
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Marcelo Tosattifa40a822009-06-04 15:08:24 -030068/*
69 * Ordering of locks:
70 *
Michael S. Tsirkin22fc0292009-06-29 22:24:45 +030071 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030072 */
73
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080074DEFINE_SPINLOCK(kvm_lock);
75LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080076
Rusty Russell7f59f492008-12-07 21:25:45 +103077static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030078
Rusty Russellc16f8622007-07-30 21:12:19 +100079struct kmem_cache *kvm_vcpu_cache;
80EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030081
Avi Kivity15ad7142007-07-11 18:17:21 +030082static __read_mostly struct preempt_ops kvm_preempt_ops;
83
Hollis Blanchard76f7c872008-04-15 16:05:42 -050084struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivitybccf2152007-02-21 18:04:26 +020086static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
87 unsigned long arg);
88
Hannes Edere8ba5d32008-11-28 17:02:42 +010089static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030090
Marcelo Tosatti54dee992009-06-11 12:07:44 -030091static bool largepages_enabled = true;
92
Xiantao Zhang8a98f662008-10-06 13:47:38 +080093#ifdef KVM_CAP_DEVICE_ASSIGNMENT
94static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
95 int assigned_dev_id)
96{
97 struct list_head *ptr;
98 struct kvm_assigned_dev_kernel *match;
99
100 list_for_each(ptr, head) {
101 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
102 if (match->assigned_dev_id == assigned_dev_id)
103 return match;
104 }
105 return NULL;
106}
107
Sheng Yang2350bd12009-02-25 17:22:27 +0800108static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
109 *assigned_dev, int irq)
110{
111 int i, index;
112 struct msix_entry *host_msix_entries;
113
114 host_msix_entries = assigned_dev->host_msix_entries;
115
116 index = -1;
117 for (i = 0; i < assigned_dev->entries_nr; i++)
118 if (irq == host_msix_entries[i].vector) {
119 index = i;
120 break;
121 }
122 if (index < 0) {
123 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
124 return 0;
125 }
126
127 return index;
128}
129
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800130static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
131{
132 struct kvm_assigned_dev_kernel *assigned_dev;
Sheng Yang2350bd12009-02-25 17:22:27 +0800133 struct kvm *kvm;
Sheng Yang968a6342009-04-30 10:58:42 +0800134 int i;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800135
136 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
137 interrupt_work);
Sheng Yang2350bd12009-02-25 17:22:27 +0800138 kvm = assigned_dev->kvm;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800139
Marcelo Tosatti547de292009-05-07 17:55:13 -0300140 spin_lock_irq(&assigned_dev->assigned_dev_lock);
Sheng Yange56d5322009-03-12 21:45:39 +0800141 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800142 struct kvm_guest_msix_entry *guest_entries =
143 assigned_dev->guest_msix_entries;
144 for (i = 0; i < assigned_dev->entries_nr; i++) {
145 if (!(guest_entries[i].flags &
146 KVM_ASSIGNED_MSIX_PENDING))
147 continue;
148 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
149 kvm_set_irq(assigned_dev->kvm,
150 assigned_dev->irq_source_id,
151 guest_entries[i].vector, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800152 }
Sheng Yang968a6342009-04-30 10:58:42 +0800153 } else
Sheng Yang2350bd12009-02-25 17:22:27 +0800154 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
155 assigned_dev->guest_irq, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800156
Marcelo Tosatti547de292009-05-07 17:55:13 -0300157 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800158}
159
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800160static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
161{
Marcelo Tosatti547de292009-05-07 17:55:13 -0300162 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800163 struct kvm_assigned_dev_kernel *assigned_dev =
164 (struct kvm_assigned_dev_kernel *) dev_id;
165
Marcelo Tosatti547de292009-05-07 17:55:13 -0300166 spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
Sheng Yange56d5322009-03-12 21:45:39 +0800167 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800168 int index = find_index_from_host_irq(assigned_dev, irq);
169 if (index < 0)
Marcelo Tosatti547de292009-05-07 17:55:13 -0300170 goto out;
Sheng Yang2350bd12009-02-25 17:22:27 +0800171 assigned_dev->guest_msix_entries[index].flags |=
172 KVM_ASSIGNED_MSIX_PENDING;
173 }
174
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800175 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000176
Sheng Yang968a6342009-04-30 10:58:42 +0800177 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
178 disable_irq_nosync(irq);
179 assigned_dev->host_irq_disabled = true;
180 }
Mark McLoughlindefaf152008-12-02 12:16:33 +0000181
Marcelo Tosatti547de292009-05-07 17:55:13 -0300182out:
183 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800184 return IRQ_HANDLED;
185}
186
187/* Ack the irq line for an assigned device */
188static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
189{
190 struct kvm_assigned_dev_kernel *dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300191 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800192
193 if (kian->gsi == -1)
194 return;
195
196 dev = container_of(kian, struct kvm_assigned_dev_kernel,
197 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000198
Sheng Yang5550af42008-10-15 20:15:06 +0800199 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000200
201 /* The guest irq may be shared so this ack may be
202 * from another device.
203 */
Marcelo Tosatti547de292009-05-07 17:55:13 -0300204 spin_lock_irqsave(&dev->assigned_dev_lock, flags);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000205 if (dev->host_irq_disabled) {
206 enable_irq(dev->host_irq);
207 dev->host_irq_disabled = false;
208 }
Marcelo Tosatti547de292009-05-07 17:55:13 -0300209 spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800210}
211
Sheng Yange56d5322009-03-12 21:45:39 +0800212static void deassign_guest_irq(struct kvm *kvm,
213 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800214{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300215 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
Sheng Yange56d5322009-03-12 21:45:39 +0800216 assigned_dev->ack_notifier.gsi = -1;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000217
218 if (assigned_dev->irq_source_id != -1)
219 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
220 assigned_dev->irq_source_id = -1;
Sheng Yange56d5322009-03-12 21:45:39 +0800221 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
222}
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800223
Sheng Yange56d5322009-03-12 21:45:39 +0800224/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
225static void deassign_host_irq(struct kvm *kvm,
226 struct kvm_assigned_dev_kernel *assigned_dev)
227{
Sheng Yangba4cef32009-01-06 10:03:03 +0800228 /*
229 * In kvm_free_device_irq, cancel_work_sync return true if:
230 * 1. work is scheduled, and then cancelled.
231 * 2. work callback is executed.
232 *
233 * The first one ensured that the irq is disabled and no more events
234 * would happen. But for the second one, the irq may be enabled (e.g.
235 * for MSI). So we disable irq here to prevent further events.
236 *
237 * Notice this maybe result in nested disable if the interrupt type is
238 * INTx, but it's OK for we are going to free it.
239 *
240 * If this function is a part of VM destroy, please ensure that till
241 * now, the kvm state is still legal for probably we also have to wait
242 * interrupt_work done.
243 */
Sheng Yange56d5322009-03-12 21:45:39 +0800244 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yangd510d6c2009-02-25 17:22:28 +0800245 int i;
246 for (i = 0; i < assigned_dev->entries_nr; i++)
247 disable_irq_nosync(assigned_dev->
248 host_msix_entries[i].vector);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800249
Sheng Yangd510d6c2009-02-25 17:22:28 +0800250 cancel_work_sync(&assigned_dev->interrupt_work);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000251
Sheng Yangd510d6c2009-02-25 17:22:28 +0800252 for (i = 0; i < assigned_dev->entries_nr; i++)
253 free_irq(assigned_dev->host_msix_entries[i].vector,
254 (void *)assigned_dev);
255
256 assigned_dev->entries_nr = 0;
257 kfree(assigned_dev->host_msix_entries);
258 kfree(assigned_dev->guest_msix_entries);
259 pci_disable_msix(assigned_dev->dev);
260 } else {
261 /* Deal with MSI and INTx */
262 disable_irq_nosync(assigned_dev->host_irq);
263 cancel_work_sync(&assigned_dev->interrupt_work);
264
265 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
266
Sheng Yange56d5322009-03-12 21:45:39 +0800267 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800268 pci_disable_msi(assigned_dev->dev);
269 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000270
Sheng Yange56d5322009-03-12 21:45:39 +0800271 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000272}
273
Sheng Yange56d5322009-03-12 21:45:39 +0800274static int kvm_deassign_irq(struct kvm *kvm,
275 struct kvm_assigned_dev_kernel *assigned_dev,
276 unsigned long irq_requested_type)
277{
278 unsigned long guest_irq_type, host_irq_type;
279
280 if (!irqchip_in_kernel(kvm))
281 return -EINVAL;
282 /* no irq assignment to deassign */
283 if (!assigned_dev->irq_requested_type)
284 return -ENXIO;
285
286 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
287 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
288
289 if (host_irq_type)
290 deassign_host_irq(kvm, assigned_dev);
291 if (guest_irq_type)
292 deassign_guest_irq(kvm, assigned_dev);
293
294 return 0;
295}
296
297static void kvm_free_assigned_irq(struct kvm *kvm,
298 struct kvm_assigned_dev_kernel *assigned_dev)
299{
300 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
301}
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000302
303static void kvm_free_assigned_device(struct kvm *kvm,
304 struct kvm_assigned_dev_kernel
305 *assigned_dev)
306{
307 kvm_free_assigned_irq(kvm, assigned_dev);
308
Sheng Yang6eb55812008-10-31 12:37:41 +0800309 pci_reset_function(assigned_dev->dev);
310
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800311 pci_release_regions(assigned_dev->dev);
312 pci_disable_device(assigned_dev->dev);
313 pci_dev_put(assigned_dev->dev);
314
315 list_del(&assigned_dev->list);
316 kfree(assigned_dev);
317}
318
319void kvm_free_all_assigned_devices(struct kvm *kvm)
320{
321 struct list_head *ptr, *ptr2;
322 struct kvm_assigned_dev_kernel *assigned_dev;
323
324 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
325 assigned_dev = list_entry(ptr,
326 struct kvm_assigned_dev_kernel,
327 list);
328
329 kvm_free_assigned_device(kvm, assigned_dev);
330 }
331}
332
Sheng Yange56d5322009-03-12 21:45:39 +0800333static int assigned_device_enable_host_intx(struct kvm *kvm,
334 struct kvm_assigned_dev_kernel *dev)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800335{
Sheng Yange56d5322009-03-12 21:45:39 +0800336 dev->host_irq = dev->dev->irq;
337 /* Even though this is PCI, we don't want to use shared
338 * interrupts. Sharing host devices with guest-assigned devices
339 * on the same interrupt line is not a happy situation: there
340 * are going to be long delays in accepting, acking, etc.
341 */
342 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
343 0, "kvm_assigned_intx_device", (void *)dev))
344 return -EIO;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800345 return 0;
346}
347
Sheng Yange56d5322009-03-12 21:45:39 +0800348#ifdef __KVM_HAVE_MSI
349static int assigned_device_enable_host_msi(struct kvm *kvm,
350 struct kvm_assigned_dev_kernel *dev)
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800351{
352 int r;
353
Sheng Yange56d5322009-03-12 21:45:39 +0800354 if (!dev->dev->msi_enabled) {
355 r = pci_enable_msi(dev->dev);
356 if (r)
357 return r;
Sheng Yang5319c662008-11-24 14:32:57 +0800358 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800359
Sheng Yange56d5322009-03-12 21:45:39 +0800360 dev->host_irq = dev->dev->irq;
361 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
362 "kvm_assigned_msi_device", (void *)dev)) {
363 pci_disable_msi(dev->dev);
364 return -EIO;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800365 }
366
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800367 return 0;
368}
369#endif
370
Sheng Yangd510d6c2009-02-25 17:22:28 +0800371#ifdef __KVM_HAVE_MSIX
Sheng Yange56d5322009-03-12 21:45:39 +0800372static int assigned_device_enable_host_msix(struct kvm *kvm,
373 struct kvm_assigned_dev_kernel *dev)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800374{
Sheng Yange56d5322009-03-12 21:45:39 +0800375 int i, r = -EINVAL;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800376
Sheng Yange56d5322009-03-12 21:45:39 +0800377 /* host_msix_entries and guest_msix_entries should have been
378 * initialized */
379 if (dev->entries_nr == 0)
380 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800381
Sheng Yange56d5322009-03-12 21:45:39 +0800382 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
383 if (r)
384 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800385
Sheng Yange56d5322009-03-12 21:45:39 +0800386 for (i = 0; i < dev->entries_nr; i++) {
387 r = request_irq(dev->host_msix_entries[i].vector,
388 kvm_assigned_dev_intr, 0,
389 "kvm_assigned_msix_device",
390 (void *)dev);
391 /* FIXME: free requested_irq's on failure */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800392 if (r)
393 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800394 }
395
Sheng Yange56d5322009-03-12 21:45:39 +0800396 return 0;
397}
Sheng Yangd510d6c2009-02-25 17:22:28 +0800398
Sheng Yange56d5322009-03-12 21:45:39 +0800399#endif
400
401static int assigned_device_enable_guest_intx(struct kvm *kvm,
402 struct kvm_assigned_dev_kernel *dev,
403 struct kvm_assigned_irq *irq)
404{
405 dev->guest_irq = irq->guest_irq;
406 dev->ack_notifier.gsi = irq->guest_irq;
407 return 0;
408}
409
410#ifdef __KVM_HAVE_MSI
411static int assigned_device_enable_guest_msi(struct kvm *kvm,
412 struct kvm_assigned_dev_kernel *dev,
413 struct kvm_assigned_irq *irq)
414{
415 dev->guest_irq = irq->guest_irq;
416 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800417 dev->host_irq_disabled = false;
Sheng Yange56d5322009-03-12 21:45:39 +0800418 return 0;
419}
420#endif
421#ifdef __KVM_HAVE_MSIX
422static int assigned_device_enable_guest_msix(struct kvm *kvm,
423 struct kvm_assigned_dev_kernel *dev,
424 struct kvm_assigned_irq *irq)
425{
426 dev->guest_irq = irq->guest_irq;
427 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800428 dev->host_irq_disabled = false;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800429 return 0;
430}
431#endif
432
Sheng Yange56d5322009-03-12 21:45:39 +0800433static int assign_host_irq(struct kvm *kvm,
434 struct kvm_assigned_dev_kernel *dev,
435 __u32 host_irq_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800436{
Sheng Yange56d5322009-03-12 21:45:39 +0800437 int r = -EEXIST;
438
439 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
440 return r;
441
442 switch (host_irq_type) {
443 case KVM_DEV_IRQ_HOST_INTX:
444 r = assigned_device_enable_host_intx(kvm, dev);
445 break;
446#ifdef __KVM_HAVE_MSI
447 case KVM_DEV_IRQ_HOST_MSI:
448 r = assigned_device_enable_host_msi(kvm, dev);
449 break;
450#endif
451#ifdef __KVM_HAVE_MSIX
452 case KVM_DEV_IRQ_HOST_MSIX:
453 r = assigned_device_enable_host_msix(kvm, dev);
454 break;
455#endif
456 default:
457 r = -EINVAL;
458 }
459
460 if (!r)
461 dev->irq_requested_type |= host_irq_type;
462
463 return r;
464}
465
466static int assign_guest_irq(struct kvm *kvm,
467 struct kvm_assigned_dev_kernel *dev,
468 struct kvm_assigned_irq *irq,
469 unsigned long guest_irq_type)
470{
471 int id;
472 int r = -EEXIST;
473
474 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
475 return r;
476
477 id = kvm_request_irq_source_id(kvm);
478 if (id < 0)
479 return id;
480
481 dev->irq_source_id = id;
482
483 switch (guest_irq_type) {
484 case KVM_DEV_IRQ_GUEST_INTX:
485 r = assigned_device_enable_guest_intx(kvm, dev, irq);
486 break;
487#ifdef __KVM_HAVE_MSI
488 case KVM_DEV_IRQ_GUEST_MSI:
489 r = assigned_device_enable_guest_msi(kvm, dev, irq);
490 break;
491#endif
492#ifdef __KVM_HAVE_MSIX
493 case KVM_DEV_IRQ_GUEST_MSIX:
494 r = assigned_device_enable_guest_msix(kvm, dev, irq);
495 break;
496#endif
497 default:
498 r = -EINVAL;
499 }
500
501 if (!r) {
502 dev->irq_requested_type |= guest_irq_type;
503 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
504 } else
505 kvm_free_irq_source_id(kvm, dev->irq_source_id);
506
507 return r;
508}
509
510/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
511static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
512 struct kvm_assigned_irq *assigned_irq)
513{
514 int r = -EINVAL;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800515 struct kvm_assigned_dev_kernel *match;
Sheng Yange56d5322009-03-12 21:45:39 +0800516 unsigned long host_irq_type, guest_irq_type;
517
518 if (!capable(CAP_SYS_RAWIO))
519 return -EPERM;
520
521 if (!irqchip_in_kernel(kvm))
522 return r;
523
524 mutex_lock(&kvm->lock);
525 r = -ENODEV;
526 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
527 assigned_irq->assigned_dev_id);
528 if (!match)
529 goto out;
530
531 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
532 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
533
534 r = -EINVAL;
535 /* can only assign one type at a time */
536 if (hweight_long(host_irq_type) > 1)
537 goto out;
538 if (hweight_long(guest_irq_type) > 1)
539 goto out;
540 if (host_irq_type == 0 && guest_irq_type == 0)
541 goto out;
542
543 r = 0;
544 if (host_irq_type)
545 r = assign_host_irq(kvm, match, host_irq_type);
546 if (r)
547 goto out;
548
549 if (guest_irq_type)
550 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
551out:
552 mutex_unlock(&kvm->lock);
553 return r;
554}
555
556static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
557 struct kvm_assigned_irq
558 *assigned_irq)
559{
560 int r = -ENODEV;
561 struct kvm_assigned_dev_kernel *match;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800562
563 mutex_lock(&kvm->lock);
564
565 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
566 assigned_irq->assigned_dev_id);
Sheng Yange56d5322009-03-12 21:45:39 +0800567 if (!match)
568 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800569
Sheng Yange56d5322009-03-12 21:45:39 +0800570 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
571out:
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800572 mutex_unlock(&kvm->lock);
573 return r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800574}
575
576static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
577 struct kvm_assigned_pci_dev *assigned_dev)
578{
579 int r = 0;
580 struct kvm_assigned_dev_kernel *match;
581 struct pci_dev *dev;
582
Mark McLoughlin682edb42009-02-05 18:23:46 +0000583 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800584 mutex_lock(&kvm->lock);
585
586 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
587 assigned_dev->assigned_dev_id);
588 if (match) {
589 /* device already assigned */
Sheng Yange56d5322009-03-12 21:45:39 +0800590 r = -EEXIST;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800591 goto out;
592 }
593
594 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
595 if (match == NULL) {
596 printk(KERN_INFO "%s: Couldn't allocate memory\n",
597 __func__);
598 r = -ENOMEM;
599 goto out;
600 }
601 dev = pci_get_bus_and_slot(assigned_dev->busnr,
602 assigned_dev->devfn);
603 if (!dev) {
604 printk(KERN_INFO "%s: host device not found\n", __func__);
605 r = -EINVAL;
606 goto out_free;
607 }
608 if (pci_enable_device(dev)) {
609 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
610 r = -EBUSY;
611 goto out_put;
612 }
613 r = pci_request_regions(dev, "kvm_assigned_device");
614 if (r) {
615 printk(KERN_INFO "%s: Could not get access to device regions\n",
616 __func__);
617 goto out_disable;
618 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800619
620 pci_reset_function(dev);
621
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800622 match->assigned_dev_id = assigned_dev->assigned_dev_id;
623 match->host_busnr = assigned_dev->busnr;
624 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800625 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800626 match->dev = dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300627 spin_lock_init(&match->assigned_dev_lock);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000628 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800629 match->kvm = kvm;
Sheng Yange56d5322009-03-12 21:45:39 +0800630 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
631 INIT_WORK(&match->interrupt_work,
632 kvm_assigned_dev_interrupt_work_handler);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800633
634 list_add(&match->list, &kvm->arch.assigned_dev_head);
635
636 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100637 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800638 r = kvm_iommu_map_guest(kvm);
639 if (r)
640 goto out_list_del;
641 }
642 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800643 if (r)
644 goto out_list_del;
645 }
646
647out:
648 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000649 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800650 return r;
651out_list_del:
652 list_del(&match->list);
653 pci_release_regions(dev);
654out_disable:
655 pci_disable_device(dev);
656out_put:
657 pci_dev_put(dev);
658out_free:
659 kfree(match);
660 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000661 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800662 return r;
663}
664#endif
665
Weidong Han0a920352008-12-02 21:24:23 +0800666#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
667static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
668 struct kvm_assigned_pci_dev *assigned_dev)
669{
670 int r = 0;
671 struct kvm_assigned_dev_kernel *match;
672
673 mutex_lock(&kvm->lock);
674
675 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
676 assigned_dev->assigned_dev_id);
677 if (!match) {
678 printk(KERN_INFO "%s: device hasn't been assigned before, "
679 "so cannot be deassigned\n", __func__);
680 r = -EINVAL;
681 goto out;
682 }
683
Weidong Han4a906e42009-02-13 17:27:51 +0800684 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800685 kvm_deassign_device(kvm, match);
686
687 kvm_free_assigned_device(kvm, match);
688
689out:
690 mutex_unlock(&kvm->lock);
691 return r;
692}
693#endif
694
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800695inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300696{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100697 if (pfn_valid(pfn)) {
698 struct page *page = compound_head(pfn_to_page(pfn));
699 return PageReserved(page);
700 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300701
702 return true;
703}
704
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705/*
706 * Switches to specified vcpu, until a matching vcpu_put()
707 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200708void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709{
Avi Kivity15ad7142007-07-11 18:17:21 +0300710 int cpu;
711
Avi Kivitybccf2152007-02-21 18:04:26 +0200712 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300713 cpu = get_cpu();
714 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200715 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300716 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200717}
718
Carsten Otte313a3dc2007-10-11 19:16:52 +0200719void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720{
Avi Kivity15ad7142007-07-11 18:17:21 +0300721 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200722 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300723 preempt_notifier_unregister(&vcpu->preempt_notifier);
724 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 mutex_unlock(&vcpu->mutex);
726}
727
Avi Kivityd9e368d2007-06-07 19:18:30 +0300728static void ack_flush(void *_completed)
729{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300730}
731
Rusty Russell49846892008-12-08 20:26:24 +1030732static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300733{
Avi Kivity597a5f52008-07-20 14:24:22 +0300734 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030735 cpumask_var_t cpus;
736 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300737 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300738
Li Zefan79f55992009-06-15 14:58:26 +0800739 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030740
Marcelo Tosatti84261922009-06-17 10:53:47 -0300741 spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200742 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300743 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030744 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300745 continue;
746 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030747 if (cpus != NULL && cpu != -1 && cpu != me)
748 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300749 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030750 if (unlikely(cpus == NULL))
751 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
752 else if (!cpumask_empty(cpus))
753 smp_call_function_many(cpus, ack_flush, NULL, 1);
754 else
755 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300756 spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030757 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030758 return called;
759}
760
761void kvm_flush_remote_tlbs(struct kvm *kvm)
762{
763 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
764 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300765}
766
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500767void kvm_reload_remote_mmus(struct kvm *kvm)
768{
Rusty Russell49846892008-12-08 20:26:24 +1030769 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500770}
771
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000772int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
773{
774 struct page *page;
775 int r;
776
777 mutex_init(&vcpu->mutex);
778 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000779 vcpu->kvm = kvm;
780 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300781 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000782
783 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
784 if (!page) {
785 r = -ENOMEM;
786 goto fail;
787 }
788 vcpu->run = page_address(page);
789
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800790 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000791 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800792 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000793 return 0;
794
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000795fail_free_run:
796 free_page((unsigned long)vcpu->run);
797fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000798 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000799}
800EXPORT_SYMBOL_GPL(kvm_vcpu_init);
801
802void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
803{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800804 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000805 free_page((unsigned long)vcpu->run);
806}
807EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
808
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200809#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
810static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
811{
812 return container_of(mn, struct kvm, mmu_notifier);
813}
814
815static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
816 struct mm_struct *mm,
817 unsigned long address)
818{
819 struct kvm *kvm = mmu_notifier_to_kvm(mn);
820 int need_tlb_flush;
821
822 /*
823 * When ->invalidate_page runs, the linux pte has been zapped
824 * already but the page is still allocated until
825 * ->invalidate_page returns. So if we increase the sequence
826 * here the kvm page fault will notice if the spte can't be
827 * established because the page is going to be freed. If
828 * instead the kvm page fault establishes the spte before
829 * ->invalidate_page runs, kvm_unmap_hva will release it
830 * before returning.
831 *
832 * The sequence increase only need to be seen at spin_unlock
833 * time, and not at spin_lock time.
834 *
835 * Increasing the sequence after the spin_unlock would be
836 * unsafe because the kvm page fault could then establish the
837 * pte after kvm_unmap_hva returned, without noticing the page
838 * is going to be freed.
839 */
840 spin_lock(&kvm->mmu_lock);
841 kvm->mmu_notifier_seq++;
842 need_tlb_flush = kvm_unmap_hva(kvm, address);
843 spin_unlock(&kvm->mmu_lock);
844
845 /* we've to flush the tlb before the pages can be freed */
846 if (need_tlb_flush)
847 kvm_flush_remote_tlbs(kvm);
848
849}
850
Izik Eidus3da0dd42009-09-23 21:47:18 +0300851static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
852 struct mm_struct *mm,
853 unsigned long address,
854 pte_t pte)
855{
856 struct kvm *kvm = mmu_notifier_to_kvm(mn);
857
858 spin_lock(&kvm->mmu_lock);
859 kvm->mmu_notifier_seq++;
860 kvm_set_spte_hva(kvm, address, pte);
861 spin_unlock(&kvm->mmu_lock);
862}
863
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200864static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
865 struct mm_struct *mm,
866 unsigned long start,
867 unsigned long end)
868{
869 struct kvm *kvm = mmu_notifier_to_kvm(mn);
870 int need_tlb_flush = 0;
871
872 spin_lock(&kvm->mmu_lock);
873 /*
874 * The count increase must become visible at unlock time as no
875 * spte can be established without taking the mmu_lock and
876 * count is also read inside the mmu_lock critical section.
877 */
878 kvm->mmu_notifier_count++;
879 for (; start < end; start += PAGE_SIZE)
880 need_tlb_flush |= kvm_unmap_hva(kvm, start);
881 spin_unlock(&kvm->mmu_lock);
882
883 /* we've to flush the tlb before the pages can be freed */
884 if (need_tlb_flush)
885 kvm_flush_remote_tlbs(kvm);
886}
887
888static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
889 struct mm_struct *mm,
890 unsigned long start,
891 unsigned long end)
892{
893 struct kvm *kvm = mmu_notifier_to_kvm(mn);
894
895 spin_lock(&kvm->mmu_lock);
896 /*
897 * This sequence increase will notify the kvm page fault that
898 * the page that is going to be mapped in the spte could have
899 * been freed.
900 */
901 kvm->mmu_notifier_seq++;
902 /*
903 * The above sequence increase must be visible before the
904 * below count decrease but both values are read by the kvm
905 * page fault under mmu_lock spinlock so we don't need to add
906 * a smb_wmb() here in between the two.
907 */
908 kvm->mmu_notifier_count--;
909 spin_unlock(&kvm->mmu_lock);
910
911 BUG_ON(kvm->mmu_notifier_count < 0);
912}
913
914static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
915 struct mm_struct *mm,
916 unsigned long address)
917{
918 struct kvm *kvm = mmu_notifier_to_kvm(mn);
919 int young;
920
921 spin_lock(&kvm->mmu_lock);
922 young = kvm_age_hva(kvm, address);
923 spin_unlock(&kvm->mmu_lock);
924
925 if (young)
926 kvm_flush_remote_tlbs(kvm);
927
928 return young;
929}
930
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100931static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
932 struct mm_struct *mm)
933{
934 struct kvm *kvm = mmu_notifier_to_kvm(mn);
935 kvm_arch_flush_shadow(kvm);
936}
937
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200938static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
939 .invalidate_page = kvm_mmu_notifier_invalidate_page,
940 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
941 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
942 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300943 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100944 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200945};
946#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
947
Avi Kivityf17abe92007-02-21 19:28:04 +0200948static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800950 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200951#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
952 struct page *page;
953#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800955 if (IS_ERR(kvm))
956 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200957#ifdef CONFIG_HAVE_KVM_IRQCHIP
958 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300959 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200960#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961
Laurent Vivier5f94c172008-05-30 16:05:54 +0200962#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
963 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
964 if (!page) {
965 kfree(kvm);
966 return ERR_PTR(-ENOMEM);
967 }
968 kvm->coalesced_mmio_ring =
969 (struct kvm_coalesced_mmio_ring *)page_address(page);
970#endif
971
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200972#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
973 {
974 int err;
975 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
976 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
977 if (err) {
978#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
979 put_page(page);
980#endif
981 kfree(kvm);
982 return ERR_PTR(err);
983 }
984 }
985#endif
986
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200987 kvm->mm = current->mm;
988 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500989 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300990 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300991 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400992 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800993 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300994 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400995 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200996 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300997 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000998 spin_lock(&kvm_lock);
999 list_add(&kvm->vm_list, &vm_list);
1000 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001001#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1002 kvm_coalesced_mmio_init(kvm);
1003#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001004out:
Avi Kivityf17abe92007-02-21 19:28:04 +02001005 return kvm;
1006}
1007
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008/*
1009 * Free any memory in @free but not in @dont.
1010 */
1011static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1012 struct kvm_memory_slot *dont)
1013{
Joerg Roedelec04b262009-06-19 15:16:23 +02001014 int i;
1015
Izik Eidus290fc382007-09-27 14:11:22 +02001016 if (!dont || free->rmap != dont->rmap)
1017 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018
1019 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1020 vfree(free->dirty_bitmap);
1021
Joerg Roedelec04b262009-06-19 15:16:23 +02001022
1023 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1024 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
1025 vfree(free->lpage_info[i]);
1026 free->lpage_info[i] = NULL;
1027 }
1028 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001029
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001031 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001032 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033}
1034
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001035void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036{
1037 int i;
1038
1039 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001040 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041}
1042
Avi Kivityf17abe92007-02-21 19:28:04 +02001043static void kvm_destroy_vm(struct kvm *kvm)
1044{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001045 struct mm_struct *mm = kvm->mm;
1046
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001047 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001048 spin_lock(&kvm_lock);
1049 list_del(&kvm->vm_list);
1050 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001051 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001052 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001053 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001054#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1055 if (kvm->coalesced_mmio_ring != NULL)
1056 free_page((unsigned long)kvm->coalesced_mmio_ring);
1057#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001058#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1059 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +02001060#else
1061 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001062#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001063 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001064 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001065}
1066
Izik Eidusd39f13b2008-03-30 16:01:25 +03001067void kvm_get_kvm(struct kvm *kvm)
1068{
1069 atomic_inc(&kvm->users_count);
1070}
1071EXPORT_SYMBOL_GPL(kvm_get_kvm);
1072
1073void kvm_put_kvm(struct kvm *kvm)
1074{
1075 if (atomic_dec_and_test(&kvm->users_count))
1076 kvm_destroy_vm(kvm);
1077}
1078EXPORT_SYMBOL_GPL(kvm_put_kvm);
1079
1080
Avi Kivityf17abe92007-02-21 19:28:04 +02001081static int kvm_vm_release(struct inode *inode, struct file *filp)
1082{
1083 struct kvm *kvm = filp->private_data;
1084
Gregory Haskins721eecb2009-05-20 10:30:49 -04001085 kvm_irqfd_release(kvm);
1086
Izik Eidusd39f13b2008-03-30 16:01:25 +03001087 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 return 0;
1089}
1090
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 * Allocate some memory and give it an address in the guest physical address
1093 * space.
1094 *
1095 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001096 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001097 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001099int __kvm_set_memory_region(struct kvm *kvm,
1100 struct kvm_userspace_memory_region *mem,
1101 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102{
1103 int r;
1104 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +02001105 unsigned long npages;
1106 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 struct kvm_memory_slot *memslot;
1108 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109
1110 r = -EINVAL;
1111 /* General sanity checks */
1112 if (mem->memory_size & (PAGE_SIZE - 1))
1113 goto out;
1114 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1115 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001116 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001117 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001118 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 goto out;
1120 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1121 goto out;
1122
1123 memslot = &kvm->memslots[mem->slot];
1124 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1125 npages = mem->memory_size >> PAGE_SHIFT;
1126
1127 if (!npages)
1128 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1129
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 new = old = *memslot;
1131
1132 new.base_gfn = base_gfn;
1133 new.npages = npages;
1134 new.flags = mem->flags;
1135
1136 /* Disallow changing a memory slot's size. */
1137 r = -EINVAL;
1138 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001139 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140
1141 /* Check for overlaps */
1142 r = -EEXIST;
1143 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1144 struct kvm_memory_slot *s = &kvm->memslots[i];
1145
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001146 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 continue;
1148 if (!((base_gfn + npages <= s->base_gfn) ||
1149 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001150 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 /* Free page dirty bitmap if unneeded */
1154 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001155 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156
1157 r = -ENOMEM;
1158
1159 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001160#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001161 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001162 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001163
1164 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001165 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001166
Izik Eidus290fc382007-09-27 14:11:22 +02001167 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001168
Izik Eidus80b14b52007-10-25 11:54:04 +02001169 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001170 /*
1171 * hva_to_rmmap() serialzies with the mmu_lock and to be
1172 * safe it has to ignore memslots with !user_alloc &&
1173 * !userspace_addr.
1174 */
1175 if (user_alloc)
1176 new.userspace_addr = mem->userspace_addr;
1177 else
1178 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179 }
Joerg Roedelec04b262009-06-19 15:16:23 +02001180 if (!npages)
1181 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001182
Joerg Roedelec04b262009-06-19 15:16:23 +02001183 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +02001184 unsigned long ugfn;
1185 unsigned long j;
1186 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +02001187 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001188
Joerg Roedelec04b262009-06-19 15:16:23 +02001189 /* Avoid unused variable warning if no large pages */
1190 (void)level;
1191
1192 if (new.lpage_info[i])
1193 continue;
1194
1195 lpages = 1 + (base_gfn + npages - 1) /
1196 KVM_PAGES_PER_HPAGE(level);
1197 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
1198
1199 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
1200
1201 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001202 goto out_free;
1203
Joerg Roedelec04b262009-06-19 15:16:23 +02001204 memset(new.lpage_info[i], 0,
1205 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001206
Joerg Roedelec04b262009-06-19 15:16:23 +02001207 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
1208 new.lpage_info[i][0].write_count = 1;
1209 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
1210 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +03001211 ugfn = new.userspace_addr >> PAGE_SHIFT;
1212 /*
1213 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001214 * other, or if explicitly asked to, disable large page
1215 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +03001216 */
Joerg Roedelec04b262009-06-19 15:16:23 +02001217 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001218 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +02001219 for (j = 0; j < lpages; ++j)
1220 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001221 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222
Joerg Roedelec04b262009-06-19 15:16:23 +02001223skip_lpage:
1224
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 /* Allocate page dirty bitmap if needed */
1226 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1227 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1228
1229 new.dirty_bitmap = vmalloc(dirty_bytes);
1230 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001231 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse244584f2009-06-10 19:23:24 +03001233 if (old.npages)
1234 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +02001236#else /* not defined CONFIG_S390 */
1237 new.user_alloc = user_alloc;
1238 if (user_alloc)
1239 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +02001240#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001242 if (!npages)
1243 kvm_arch_flush_shadow(kvm);
1244
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001245 spin_lock(&kvm->mmu_lock);
1246 if (mem->slot >= kvm->nmemslots)
1247 kvm->nmemslots = mem->slot + 1;
1248
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001250 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251
Zhang Xiantao0de10342007-11-20 16:25:04 +08001252 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1253 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001254 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001255 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001256 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001257 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001258 }
1259
Glauber Costa6f897242008-12-03 13:40:51 -02001260 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1261 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001262 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -02001263 if (!npages)
1264 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001265 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001266#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001267 /* map the pages in iommu page table */
1268 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1269 if (r)
1270 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001271#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 return 0;
1273
Sheng Yangf78e0e22007-10-29 09:40:42 +08001274out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275 kvm_free_physmem_slot(&new, &old);
1276out:
1277 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001278
1279}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001280EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1281
1282int kvm_set_memory_region(struct kvm *kvm,
1283 struct kvm_userspace_memory_region *mem,
1284 int user_alloc)
1285{
1286 int r;
1287
Izik Eidus72dc67a2008-02-10 18:04:15 +02001288 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001289 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001290 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001291 return r;
1292}
Izik Eidus210c7c42007-10-24 23:52:57 +02001293EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1294
Carsten Otte1fe779f2007-10-29 16:08:35 +01001295int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1296 struct
1297 kvm_userspace_memory_region *mem,
1298 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001299{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001300 if (mem->slot >= KVM_MEMORY_SLOTS)
1301 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001302 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303}
1304
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001305int kvm_get_dirty_log(struct kvm *kvm,
1306 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307{
1308 struct kvm_memory_slot *memslot;
1309 int r, i;
1310 int n;
1311 unsigned long any = 0;
1312
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 r = -EINVAL;
1314 if (log->slot >= KVM_MEMORY_SLOTS)
1315 goto out;
1316
1317 memslot = &kvm->memslots[log->slot];
1318 r = -ENOENT;
1319 if (!memslot->dirty_bitmap)
1320 goto out;
1321
Uri Lublincd1a4a92007-02-22 16:43:09 +02001322 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323
Uri Lublincd1a4a92007-02-22 16:43:09 +02001324 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 any = memslot->dirty_bitmap[i];
1326
1327 r = -EFAULT;
1328 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1329 goto out;
1330
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001331 if (any)
1332 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
1334 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 return r;
1337}
1338
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001339void kvm_disable_largepages(void)
1340{
1341 largepages_enabled = false;
1342}
1343EXPORT_SYMBOL_GPL(kvm_disable_largepages);
1344
Izik Eiduscea7bb22007-10-17 19:17:48 +02001345int is_error_page(struct page *page)
1346{
1347 return page == bad_page;
1348}
1349EXPORT_SYMBOL_GPL(is_error_page);
1350
Anthony Liguori35149e22008-04-02 14:46:56 -05001351int is_error_pfn(pfn_t pfn)
1352{
1353 return pfn == bad_pfn;
1354}
1355EXPORT_SYMBOL_GPL(is_error_pfn);
1356
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001357static inline unsigned long bad_hva(void)
1358{
1359 return PAGE_OFFSET;
1360}
1361
1362int kvm_is_error_hva(unsigned long addr)
1363{
1364 return addr == bad_hva();
1365}
1366EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1367
Izik Eidus28430992008-10-03 17:40:32 +03001368struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369{
1370 int i;
1371
1372 for (i = 0; i < kvm->nmemslots; ++i) {
1373 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1374
1375 if (gfn >= memslot->base_gfn
1376 && gfn < memslot->base_gfn + memslot->npages)
1377 return memslot;
1378 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001379 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380}
Izik Eidus28430992008-10-03 17:40:32 +03001381EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001382
1383struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1384{
1385 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001386 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001387}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388
Izik Eiduse0d62c72007-10-24 23:57:46 +02001389int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1390{
1391 int i;
1392
1393 gfn = unalias_gfn(kvm, gfn);
1394 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1395 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1396
1397 if (gfn >= memslot->base_gfn
1398 && gfn < memslot->base_gfn + memslot->npages)
1399 return 1;
1400 }
1401 return 0;
1402}
1403EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1404
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001405unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001406{
1407 struct kvm_memory_slot *slot;
1408
1409 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001410 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001411 if (!slot)
1412 return bad_hva();
1413 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1414}
Sheng Yang0d150292008-04-25 21:44:50 +08001415EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001416
Anthony Liguori35149e22008-04-02 14:46:56 -05001417pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001418{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001419 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001420 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001421 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001422 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001423
Avi Kivity60395222007-10-21 11:03:36 +02001424 might_sleep();
1425
Izik Eidus539cb662007-11-11 22:05:04 +02001426 addr = gfn_to_hva(kvm, gfn);
1427 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001428 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001429 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001430 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001431
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001432 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001433
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001434 if (unlikely(npages != 1)) {
1435 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001436
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001437 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001438 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001439
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001440 if (vma == NULL || addr < vma->vm_start ||
1441 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001442 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001443 get_page(bad_page);
1444 return page_to_pfn(bad_page);
1445 }
1446
1447 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001448 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001449 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001450 } else
1451 pfn = page_to_pfn(page[0]);
1452
1453 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001454}
1455
1456EXPORT_SYMBOL_GPL(gfn_to_pfn);
1457
1458struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1459{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001460 pfn_t pfn;
1461
1462 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001463 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001464 return pfn_to_page(pfn);
1465
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001466 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001467
1468 get_page(bad_page);
1469 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001470}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001471
Avi Kivity954bbbc2007-03-30 14:02:32 +03001472EXPORT_SYMBOL_GPL(gfn_to_page);
1473
Izik Eidusb4231d62007-11-20 11:49:33 +02001474void kvm_release_page_clean(struct page *page)
1475{
Anthony Liguori35149e22008-04-02 14:46:56 -05001476 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001477}
1478EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1479
Anthony Liguori35149e22008-04-02 14:46:56 -05001480void kvm_release_pfn_clean(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 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001484}
1485EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1486
Izik Eidusb4231d62007-11-20 11:49:33 +02001487void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001488{
Anthony Liguori35149e22008-04-02 14:46:56 -05001489 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001490}
Izik Eidusb4231d62007-11-20 11:49:33 +02001491EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001492
Anthony Liguori35149e22008-04-02 14:46:56 -05001493void kvm_release_pfn_dirty(pfn_t pfn)
1494{
1495 kvm_set_pfn_dirty(pfn);
1496 kvm_release_pfn_clean(pfn);
1497}
1498EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1499
1500void kvm_set_page_dirty(struct page *page)
1501{
1502 kvm_set_pfn_dirty(page_to_pfn(page));
1503}
1504EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1505
1506void kvm_set_pfn_dirty(pfn_t pfn)
1507{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001508 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001509 struct page *page = pfn_to_page(pfn);
1510 if (!PageReserved(page))
1511 SetPageDirty(page);
1512 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001513}
1514EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1515
1516void kvm_set_pfn_accessed(pfn_t pfn)
1517{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001518 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001519 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001520}
1521EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1522
1523void kvm_get_pfn(pfn_t pfn)
1524{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001525 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001526 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001527}
1528EXPORT_SYMBOL_GPL(kvm_get_pfn);
1529
Izik Eidus195aefd2007-10-01 22:14:18 +02001530static int next_segment(unsigned long len, int offset)
1531{
1532 if (len > PAGE_SIZE - offset)
1533 return PAGE_SIZE - offset;
1534 else
1535 return len;
1536}
1537
1538int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1539 int len)
1540{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001541 int r;
1542 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001543
Izik Eiduse0506bc2007-11-11 22:10:22 +02001544 addr = gfn_to_hva(kvm, gfn);
1545 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001546 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001547 r = copy_from_user(data, (void __user *)addr + offset, len);
1548 if (r)
1549 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001550 return 0;
1551}
1552EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1553
1554int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1555{
1556 gfn_t gfn = gpa >> PAGE_SHIFT;
1557 int seg;
1558 int offset = offset_in_page(gpa);
1559 int ret;
1560
1561 while ((seg = next_segment(len, offset)) != 0) {
1562 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1563 if (ret < 0)
1564 return ret;
1565 offset = 0;
1566 len -= seg;
1567 data += seg;
1568 ++gfn;
1569 }
1570 return 0;
1571}
1572EXPORT_SYMBOL_GPL(kvm_read_guest);
1573
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001574int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1575 unsigned long len)
1576{
1577 int r;
1578 unsigned long addr;
1579 gfn_t gfn = gpa >> PAGE_SHIFT;
1580 int offset = offset_in_page(gpa);
1581
1582 addr = gfn_to_hva(kvm, gfn);
1583 if (kvm_is_error_hva(addr))
1584 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001585 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001586 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001587 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001588 if (r)
1589 return -EFAULT;
1590 return 0;
1591}
1592EXPORT_SYMBOL(kvm_read_guest_atomic);
1593
Izik Eidus195aefd2007-10-01 22:14:18 +02001594int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1595 int offset, int len)
1596{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001597 int r;
1598 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001599
Izik Eiduse0506bc2007-11-11 22:10:22 +02001600 addr = gfn_to_hva(kvm, gfn);
1601 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001602 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001603 r = copy_to_user((void __user *)addr + offset, data, len);
1604 if (r)
1605 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001606 mark_page_dirty(kvm, gfn);
1607 return 0;
1608}
1609EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1610
1611int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1612 unsigned long len)
1613{
1614 gfn_t gfn = gpa >> PAGE_SHIFT;
1615 int seg;
1616 int offset = offset_in_page(gpa);
1617 int ret;
1618
1619 while ((seg = next_segment(len, offset)) != 0) {
1620 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1621 if (ret < 0)
1622 return ret;
1623 offset = 0;
1624 len -= seg;
1625 data += seg;
1626 ++gfn;
1627 }
1628 return 0;
1629}
1630
1631int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1632{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001633 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001634}
1635EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1636
1637int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1638{
1639 gfn_t gfn = gpa >> PAGE_SHIFT;
1640 int seg;
1641 int offset = offset_in_page(gpa);
1642 int ret;
1643
1644 while ((seg = next_segment(len, offset)) != 0) {
1645 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1646 if (ret < 0)
1647 return ret;
1648 offset = 0;
1649 len -= seg;
1650 ++gfn;
1651 }
1652 return 0;
1653}
1654EXPORT_SYMBOL_GPL(kvm_clear_guest);
1655
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1657{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001658 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659
Uri Lublin3b6fff12007-10-30 10:42:09 +02001660 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001661 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001662 if (memslot && memslot->dirty_bitmap) {
1663 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664
Rusty Russell7e9d6192007-07-31 20:41:14 +10001665 /* avoid RMW */
1666 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1667 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 }
1669}
1670
Eddie Dongb6958ce2007-07-18 12:15:21 +03001671/*
1672 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1673 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001674void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001675{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001676 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001677
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001678 for (;;) {
1679 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001680
Gleb Natapova1b37102009-07-09 15:33:52 +03001681 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001682 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001683 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001684 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001685 if (kvm_cpu_has_pending_timer(vcpu))
1686 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001687 if (signal_pending(current))
1688 break;
1689
Eddie Dongb6958ce2007-07-18 12:15:21 +03001690 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001691 }
1692
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001693 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001694}
1695
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696void kvm_resched(struct kvm_vcpu *vcpu)
1697{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001698 if (!need_resched())
1699 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701}
1702EXPORT_SYMBOL_GPL(kvm_resched);
1703
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001704static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001705{
1706 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001707 struct page *page;
1708
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001709 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001710 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001711#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001712 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001713 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001714#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001715#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1716 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1717 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1718#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001719 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001720 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001721 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001722 vmf->page = page;
1723 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001724}
1725
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001726static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001727 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001728};
1729
1730static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1731{
1732 vma->vm_ops = &kvm_vcpu_vm_ops;
1733 return 0;
1734}
1735
Avi Kivitybccf2152007-02-21 18:04:26 +02001736static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1737{
1738 struct kvm_vcpu *vcpu = filp->private_data;
1739
Al Viro66c0b392008-04-19 20:33:56 +01001740 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001741 return 0;
1742}
1743
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001744static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001745 .release = kvm_vcpu_release,
1746 .unlocked_ioctl = kvm_vcpu_ioctl,
1747 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001748 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001749};
1750
1751/*
1752 * Allocates an inode for the vcpu.
1753 */
1754static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1755{
Gleb Natapov73880c82009-06-09 15:56:28 +03001756 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Avi Kivitybccf2152007-02-21 18:04:26 +02001757}
1758
Avi Kivityc5ea7662007-02-20 18:41:05 +02001759/*
1760 * Creates some virtual cpus. Good luck creating more than one.
1761 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001762static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001763{
1764 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001765 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001766
Gleb Natapov73880c82009-06-09 15:56:28 +03001767 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001768 if (IS_ERR(vcpu))
1769 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001770
Avi Kivity15ad7142007-07-11 18:17:21 +03001771 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1772
Avi Kivity26e52152007-11-20 15:30:24 +02001773 r = kvm_arch_vcpu_setup(vcpu);
1774 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001775 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001776
Shaohua Li11ec2802007-07-23 14:51:37 +08001777 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001778 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1779 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001780 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001781 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001782
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001783 kvm_for_each_vcpu(r, v, kvm)
1784 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001785 r = -EEXIST;
1786 goto vcpu_destroy;
1787 }
1788
1789 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001790
1791 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001792 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001793 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001794 if (r < 0) {
1795 kvm_put_kvm(kvm);
1796 goto vcpu_destroy;
1797 }
1798
1799 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1800 smp_wmb();
1801 atomic_inc(&kvm->online_vcpus);
1802
1803#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1804 if (kvm->bsp_vcpu_id == id)
1805 kvm->bsp_vcpu = vcpu;
1806#endif
1807 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001808 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001809
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001810vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001811 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001812 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001813 return r;
1814}
1815
Avi Kivity1961d272007-03-05 19:46:05 +02001816static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1817{
1818 if (sigset) {
1819 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1820 vcpu->sigset_active = 1;
1821 vcpu->sigset = *sigset;
1822 } else
1823 vcpu->sigset_active = 0;
1824 return 0;
1825}
1826
Sheng Yangc1e01512009-02-25 17:22:26 +08001827#ifdef __KVM_HAVE_MSIX
1828static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1829 struct kvm_assigned_msix_nr *entry_nr)
1830{
1831 int r = 0;
1832 struct kvm_assigned_dev_kernel *adev;
1833
1834 mutex_lock(&kvm->lock);
1835
1836 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1837 entry_nr->assigned_dev_id);
1838 if (!adev) {
1839 r = -EINVAL;
1840 goto msix_nr_out;
1841 }
1842
1843 if (adev->entries_nr == 0) {
1844 adev->entries_nr = entry_nr->entry_nr;
1845 if (adev->entries_nr == 0 ||
1846 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1847 r = -EINVAL;
1848 goto msix_nr_out;
1849 }
1850
1851 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1852 entry_nr->entry_nr,
1853 GFP_KERNEL);
1854 if (!adev->host_msix_entries) {
1855 r = -ENOMEM;
1856 goto msix_nr_out;
1857 }
1858 adev->guest_msix_entries = kzalloc(
1859 sizeof(struct kvm_guest_msix_entry) *
1860 entry_nr->entry_nr, GFP_KERNEL);
1861 if (!adev->guest_msix_entries) {
1862 kfree(adev->host_msix_entries);
1863 r = -ENOMEM;
1864 goto msix_nr_out;
1865 }
1866 } else /* Not allowed set MSI-X number twice */
1867 r = -EINVAL;
1868msix_nr_out:
1869 mutex_unlock(&kvm->lock);
1870 return r;
1871}
1872
1873static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1874 struct kvm_assigned_msix_entry *entry)
1875{
1876 int r = 0, i;
1877 struct kvm_assigned_dev_kernel *adev;
1878
1879 mutex_lock(&kvm->lock);
1880
1881 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1882 entry->assigned_dev_id);
1883
1884 if (!adev) {
1885 r = -EINVAL;
1886 goto msix_entry_out;
1887 }
1888
1889 for (i = 0; i < adev->entries_nr; i++)
1890 if (adev->guest_msix_entries[i].vector == 0 ||
1891 adev->guest_msix_entries[i].entry == entry->entry) {
1892 adev->guest_msix_entries[i].entry = entry->entry;
1893 adev->guest_msix_entries[i].vector = entry->gsi;
1894 adev->host_msix_entries[i].entry = entry->entry;
1895 break;
1896 }
1897 if (i == adev->entries_nr) {
1898 r = -ENOSPC;
1899 goto msix_entry_out;
1900 }
1901
1902msix_entry_out:
1903 mutex_unlock(&kvm->lock);
1904
1905 return r;
1906}
1907#endif
1908
Avi Kivitybccf2152007-02-21 18:04:26 +02001909static long kvm_vcpu_ioctl(struct file *filp,
1910 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911{
Avi Kivitybccf2152007-02-21 18:04:26 +02001912 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001913 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001914 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001915 struct kvm_fpu *fpu = NULL;
1916 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001918 if (vcpu->kvm->mm != current->mm)
1919 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001921 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001922 r = -EINVAL;
1923 if (arg)
1924 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001925 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001928 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001930 r = -ENOMEM;
1931 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1932 if (!kvm_regs)
1933 goto out;
1934 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001936 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001938 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1939 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001941out_free1:
1942 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 break;
1944 }
1945 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001946 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001948 r = -ENOMEM;
1949 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1950 if (!kvm_regs)
1951 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001953 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1954 goto out_free2;
1955 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001957 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001959out_free2:
1960 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 break;
1962 }
1963 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001964 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1965 r = -ENOMEM;
1966 if (!kvm_sregs)
1967 goto out;
1968 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 if (r)
1970 goto out;
1971 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001972 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 goto out;
1974 r = 0;
1975 break;
1976 }
1977 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001978 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1979 r = -ENOMEM;
1980 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001982 r = -EFAULT;
1983 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1984 goto out;
1985 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986 if (r)
1987 goto out;
1988 r = 0;
1989 break;
1990 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001991 case KVM_GET_MP_STATE: {
1992 struct kvm_mp_state mp_state;
1993
1994 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1995 if (r)
1996 goto out;
1997 r = -EFAULT;
1998 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1999 goto out;
2000 r = 0;
2001 break;
2002 }
2003 case KVM_SET_MP_STATE: {
2004 struct kvm_mp_state mp_state;
2005
2006 r = -EFAULT;
2007 if (copy_from_user(&mp_state, argp, sizeof mp_state))
2008 goto out;
2009 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
2010 if (r)
2011 goto out;
2012 r = 0;
2013 break;
2014 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015 case KVM_TRANSLATE: {
2016 struct kvm_translation tr;
2017
2018 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002019 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08002021 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022 if (r)
2023 goto out;
2024 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002025 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 goto out;
2027 r = 0;
2028 break;
2029 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002030 case KVM_SET_GUEST_DEBUG: {
2031 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032
2033 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002034 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002036 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 if (r)
2038 goto out;
2039 r = 0;
2040 break;
2041 }
Avi Kivity1961d272007-03-05 19:46:05 +02002042 case KVM_SET_SIGNAL_MASK: {
2043 struct kvm_signal_mask __user *sigmask_arg = argp;
2044 struct kvm_signal_mask kvm_sigmask;
2045 sigset_t sigset, *p;
2046
2047 p = NULL;
2048 if (argp) {
2049 r = -EFAULT;
2050 if (copy_from_user(&kvm_sigmask, argp,
2051 sizeof kvm_sigmask))
2052 goto out;
2053 r = -EINVAL;
2054 if (kvm_sigmask.len != sizeof sigset)
2055 goto out;
2056 r = -EFAULT;
2057 if (copy_from_user(&sigset, sigmask_arg->sigset,
2058 sizeof sigset))
2059 goto out;
2060 p = &sigset;
2061 }
2062 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2063 break;
2064 }
Avi Kivityb8836732007-04-01 16:34:31 +03002065 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002066 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2067 r = -ENOMEM;
2068 if (!fpu)
2069 goto out;
2070 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002071 if (r)
2072 goto out;
2073 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002074 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03002075 goto out;
2076 r = 0;
2077 break;
2078 }
2079 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002080 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2081 r = -ENOMEM;
2082 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002083 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002084 r = -EFAULT;
2085 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2086 goto out;
2087 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002088 if (r)
2089 goto out;
2090 r = 0;
2091 break;
2092 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002093 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002094 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002095 }
2096out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002097 kfree(fpu);
2098 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002099 return r;
2100}
2101
2102static long kvm_vm_ioctl(struct file *filp,
2103 unsigned int ioctl, unsigned long arg)
2104{
2105 struct kvm *kvm = filp->private_data;
2106 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002107 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002108
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002109 if (kvm->mm != current->mm)
2110 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002111 switch (ioctl) {
2112 case KVM_CREATE_VCPU:
2113 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2114 if (r < 0)
2115 goto out;
2116 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002117 case KVM_SET_USER_MEMORY_REGION: {
2118 struct kvm_userspace_memory_region kvm_userspace_mem;
2119
2120 r = -EFAULT;
2121 if (copy_from_user(&kvm_userspace_mem, argp,
2122 sizeof kvm_userspace_mem))
2123 goto out;
2124
2125 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 if (r)
2127 goto out;
2128 break;
2129 }
2130 case KVM_GET_DIRTY_LOG: {
2131 struct kvm_dirty_log log;
2132
2133 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002134 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002136 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137 if (r)
2138 goto out;
2139 break;
2140 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002141#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2142 case KVM_REGISTER_COALESCED_MMIO: {
2143 struct kvm_coalesced_mmio_zone zone;
2144 r = -EFAULT;
2145 if (copy_from_user(&zone, argp, sizeof zone))
2146 goto out;
2147 r = -ENXIO;
2148 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2149 if (r)
2150 goto out;
2151 r = 0;
2152 break;
2153 }
2154 case KVM_UNREGISTER_COALESCED_MMIO: {
2155 struct kvm_coalesced_mmio_zone zone;
2156 r = -EFAULT;
2157 if (copy_from_user(&zone, argp, sizeof zone))
2158 goto out;
2159 r = -ENXIO;
2160 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2161 if (r)
2162 goto out;
2163 r = 0;
2164 break;
2165 }
2166#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002167#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2168 case KVM_ASSIGN_PCI_DEVICE: {
2169 struct kvm_assigned_pci_dev assigned_dev;
2170
2171 r = -EFAULT;
2172 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2173 goto out;
2174 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2175 if (r)
2176 goto out;
2177 break;
2178 }
2179 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002180 r = -EOPNOTSUPP;
2181 break;
2182 }
2183#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2184 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002185 struct kvm_assigned_irq assigned_irq;
2186
2187 r = -EFAULT;
2188 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2189 goto out;
2190 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2191 if (r)
2192 goto out;
2193 break;
2194 }
Sheng Yange56d5322009-03-12 21:45:39 +08002195 case KVM_DEASSIGN_DEV_IRQ: {
2196 struct kvm_assigned_irq assigned_irq;
2197
2198 r = -EFAULT;
2199 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2200 goto out;
2201 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2202 if (r)
2203 goto out;
2204 break;
2205 }
2206#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002207#endif
Weidong Han0a920352008-12-02 21:24:23 +08002208#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2209 case KVM_DEASSIGN_PCI_DEVICE: {
2210 struct kvm_assigned_pci_dev assigned_dev;
2211
2212 r = -EFAULT;
2213 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2214 goto out;
2215 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2216 if (r)
2217 goto out;
2218 break;
2219 }
2220#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002221#ifdef KVM_CAP_IRQ_ROUTING
2222 case KVM_SET_GSI_ROUTING: {
2223 struct kvm_irq_routing routing;
2224 struct kvm_irq_routing __user *urouting;
2225 struct kvm_irq_routing_entry *entries;
2226
2227 r = -EFAULT;
2228 if (copy_from_user(&routing, argp, sizeof(routing)))
2229 goto out;
2230 r = -EINVAL;
2231 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2232 goto out;
2233 if (routing.flags)
2234 goto out;
2235 r = -ENOMEM;
2236 entries = vmalloc(routing.nr * sizeof(*entries));
2237 if (!entries)
2238 goto out;
2239 r = -EFAULT;
2240 urouting = argp;
2241 if (copy_from_user(entries, urouting->entries,
2242 routing.nr * sizeof(*entries)))
2243 goto out_free_irq_routing;
2244 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2245 routing.flags);
2246 out_free_irq_routing:
2247 vfree(entries);
2248 break;
2249 }
Avi Kivity6621fbc2009-08-10 17:00:52 +03002250#endif /* KVM_CAP_IRQ_ROUTING */
Sheng Yangc1e01512009-02-25 17:22:26 +08002251#ifdef __KVM_HAVE_MSIX
2252 case KVM_ASSIGN_SET_MSIX_NR: {
2253 struct kvm_assigned_msix_nr entry_nr;
2254 r = -EFAULT;
2255 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2256 goto out;
2257 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2258 if (r)
2259 goto out;
2260 break;
2261 }
2262 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2263 struct kvm_assigned_msix_entry entry;
2264 r = -EFAULT;
2265 if (copy_from_user(&entry, argp, sizeof entry))
2266 goto out;
2267 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2268 if (r)
2269 goto out;
2270 break;
2271 }
Avi Kivity399ec802008-11-19 13:58:46 +02002272#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04002273 case KVM_IRQFD: {
2274 struct kvm_irqfd data;
2275
2276 r = -EFAULT;
2277 if (copy_from_user(&data, argp, sizeof data))
2278 goto out;
2279 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2280 break;
2281 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002282 case KVM_IOEVENTFD: {
2283 struct kvm_ioeventfd data;
2284
2285 r = -EFAULT;
2286 if (copy_from_user(&data, argp, sizeof data))
2287 goto out;
2288 r = kvm_ioeventfd(kvm, &data);
2289 break;
2290 }
Gleb Natapov73880c82009-06-09 15:56:28 +03002291#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2292 case KVM_SET_BOOT_CPU_ID:
2293 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002294 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002295 if (atomic_read(&kvm->online_vcpus) != 0)
2296 r = -EBUSY;
2297 else
2298 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002299 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002300 break;
2301#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002302 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002303 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002304 }
2305out:
2306 return r;
2307}
2308
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002309static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002310{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002311 struct page *page[1];
2312 unsigned long addr;
2313 int npages;
2314 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002315 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002316
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002317 addr = gfn_to_hva(kvm, gfn);
2318 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002319 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002320
2321 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2322 NULL);
2323 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002324 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002325
2326 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002327 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002328}
2329
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002330static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002331 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002332};
2333
2334static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2335{
2336 vma->vm_ops = &kvm_vm_vm_ops;
2337 return 0;
2338}
2339
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002340static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002341 .release = kvm_vm_release,
2342 .unlocked_ioctl = kvm_vm_ioctl,
2343 .compat_ioctl = kvm_vm_ioctl,
2344 .mmap = kvm_vm_mmap,
2345};
2346
2347static int kvm_dev_ioctl_create_vm(void)
2348{
Al Viro2030a422008-02-23 06:46:49 -05002349 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002350 struct kvm *kvm;
2351
Avi Kivityf17abe92007-02-21 19:28:04 +02002352 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002353 if (IS_ERR(kvm))
2354 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002355 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002356 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002357 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002358
Avi Kivityf17abe92007-02-21 19:28:04 +02002359 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002360}
2361
Avi Kivity1a811b62008-12-08 18:25:27 +02002362static long kvm_dev_ioctl_check_extension_generic(long arg)
2363{
2364 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002365 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002366 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002367 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002368#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2369 case KVM_CAP_SET_BOOT_CPU_ID:
2370#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002371 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002372#ifdef CONFIG_HAVE_KVM_IRQCHIP
2373 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002374 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002375#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002376 default:
2377 break;
2378 }
2379 return kvm_dev_ioctl_check_extension(arg);
2380}
2381
Avi Kivityf17abe92007-02-21 19:28:04 +02002382static long kvm_dev_ioctl(struct file *filp,
2383 unsigned int ioctl, unsigned long arg)
2384{
Avi Kivity07c45a32007-03-07 13:05:38 +02002385 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002386
2387 switch (ioctl) {
2388 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002389 r = -EINVAL;
2390 if (arg)
2391 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002392 r = KVM_API_VERSION;
2393 break;
2394 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002395 r = -EINVAL;
2396 if (arg)
2397 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002398 r = kvm_dev_ioctl_create_vm();
2399 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002400 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002401 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002402 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002403 case KVM_GET_VCPU_MMAP_SIZE:
2404 r = -EINVAL;
2405 if (arg)
2406 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002407 r = PAGE_SIZE; /* struct kvm_run */
2408#ifdef CONFIG_X86
2409 r += PAGE_SIZE; /* pio data page */
2410#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002411#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2412 r += PAGE_SIZE; /* coalesced mmio ring page */
2413#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002414 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002415 case KVM_TRACE_ENABLE:
2416 case KVM_TRACE_PAUSE:
2417 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002418 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002419 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002421 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422 }
2423out:
2424 return r;
2425}
2426
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428 .unlocked_ioctl = kvm_dev_ioctl,
2429 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430};
2431
2432static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002433 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434 "kvm",
2435 &kvm_chardev_ops,
2436};
2437
Avi Kivity1b6c0162007-05-24 13:03:52 +03002438static void hardware_enable(void *junk)
2439{
2440 int cpu = raw_smp_processor_id();
2441
Rusty Russell7f59f492008-12-07 21:25:45 +10302442 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002443 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302444 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002445 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002446}
2447
2448static void hardware_disable(void *junk)
2449{
2450 int cpu = raw_smp_processor_id();
2451
Rusty Russell7f59f492008-12-07 21:25:45 +10302452 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002453 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302454 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002455 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002456}
2457
Avi Kivity774c47f2007-02-12 00:54:47 -08002458static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2459 void *v)
2460{
2461 int cpu = (long)v;
2462
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002463 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002464 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002465 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002466 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2467 cpu);
2468 hardware_disable(NULL);
2469 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002470 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002471 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2472 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002473 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002474 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002475 case CPU_ONLINE:
2476 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2477 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002478 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002479 break;
2480 }
2481 return NOTIFY_OK;
2482}
2483
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002484
2485asmlinkage void kvm_handle_fault_on_reboot(void)
2486{
2487 if (kvm_rebooting)
2488 /* spin while reset goes on */
2489 while (true)
2490 ;
2491 /* Fault while not rebooting. We want the trace. */
2492 BUG();
2493}
2494EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2495
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002496static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002497 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002498{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002499 /*
2500 * Some (well, at least mine) BIOSes hang on reboot if
2501 * in vmx root mode.
2502 *
2503 * And Intel TXT required VMX off for all cpu when system shutdown.
2504 */
2505 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2506 kvm_rebooting = true;
2507 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002508 return NOTIFY_OK;
2509}
2510
2511static struct notifier_block kvm_reboot_notifier = {
2512 .notifier_call = kvm_reboot,
2513 .priority = 0,
2514};
2515
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002516void kvm_io_bus_init(struct kvm_io_bus *bus)
2517{
2518 memset(bus, 0, sizeof(*bus));
2519}
2520
2521void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2522{
2523 int i;
2524
2525 for (i = 0; i < bus->dev_count; i++) {
2526 struct kvm_io_device *pos = bus->devs[i];
2527
2528 kvm_iodevice_destructor(pos);
2529 }
2530}
2531
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002532/* kvm_io_bus_write - called under kvm->slots_lock */
2533int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
2534 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002535{
2536 int i;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002537 for (i = 0; i < bus->dev_count; i++)
2538 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2539 return 0;
2540 return -EOPNOTSUPP;
2541}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002542
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002543/* kvm_io_bus_read - called under kvm->slots_lock */
2544int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
2545{
2546 int i;
2547 for (i = 0; i < bus->dev_count; i++)
2548 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2549 return 0;
2550 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002551}
2552
Gregory Haskins090b7af2009-07-07 17:08:44 -04002553int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002554 struct kvm_io_device *dev)
2555{
Gregory Haskins090b7af2009-07-07 17:08:44 -04002556 int ret;
2557
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002558 down_write(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002559 ret = __kvm_io_bus_register_dev(bus, dev);
2560 up_write(&kvm->slots_lock);
2561
2562 return ret;
2563}
2564
2565/* An unlocked version. Caller must have write lock on slots_lock. */
2566int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
2567 struct kvm_io_device *dev)
2568{
2569 if (bus->dev_count > NR_IOBUS_DEVS-1)
2570 return -ENOSPC;
2571
2572 bus->devs[bus->dev_count++] = dev;
2573
2574 return 0;
2575}
2576
2577void kvm_io_bus_unregister_dev(struct kvm *kvm,
2578 struct kvm_io_bus *bus,
2579 struct kvm_io_device *dev)
2580{
2581 down_write(&kvm->slots_lock);
2582 __kvm_io_bus_unregister_dev(bus, dev);
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002583 up_write(&kvm->slots_lock);
2584}
2585
2586/* An unlocked version. Caller must have write lock on slots_lock. */
Gregory Haskins090b7af2009-07-07 17:08:44 -04002587void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
2588 struct kvm_io_device *dev)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002589{
Gregory Haskins090b7af2009-07-07 17:08:44 -04002590 int i;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002591
Gregory Haskins090b7af2009-07-07 17:08:44 -04002592 for (i = 0; i < bus->dev_count; i++)
2593 if (bus->devs[i] == dev) {
2594 bus->devs[i] = bus->devs[--bus->dev_count];
2595 break;
2596 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002597}
2598
Avi Kivity774c47f2007-02-12 00:54:47 -08002599static struct notifier_block kvm_cpu_notifier = {
2600 .notifier_call = kvm_cpu_hotplug,
2601 .priority = 20, /* must be > scheduler priority */
2602};
2603
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002604static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002605{
2606 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002607 struct kvm *kvm;
2608
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002609 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002610 spin_lock(&kvm_lock);
2611 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002612 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002613 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002614 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002615}
2616
2617DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2618
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002619static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002620{
2621 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002622 struct kvm *kvm;
2623 struct kvm_vcpu *vcpu;
2624 int i;
2625
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002626 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002627 spin_lock(&kvm_lock);
2628 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002629 kvm_for_each_vcpu(i, vcpu, kvm)
2630 *val += *(u32 *)((void *)vcpu + offset);
2631
Avi Kivity1165f5f2007-04-19 17:27:43 +03002632 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002633 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002634}
2635
Avi Kivityba1389b2007-11-18 16:24:12 +02002636DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2637
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002638static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002639 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2640 [KVM_STAT_VM] = &vm_stat_fops,
2641};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002642
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002643static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644{
2645 struct kvm_stats_debugfs_item *p;
2646
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002647 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002649 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002650 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002651 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652}
2653
2654static void kvm_exit_debug(void)
2655{
2656 struct kvm_stats_debugfs_item *p;
2657
2658 for (p = debugfs_entries; p->name; ++p)
2659 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002660 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661}
2662
Avi Kivity59ae6c62007-02-12 00:54:48 -08002663static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2664{
Avi Kivity4267c412007-05-24 13:09:41 +03002665 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002666 return 0;
2667}
2668
2669static int kvm_resume(struct sys_device *dev)
2670{
Avi Kivity4267c412007-05-24 13:09:41 +03002671 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002672 return 0;
2673}
2674
2675static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002676 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002677 .suspend = kvm_suspend,
2678 .resume = kvm_resume,
2679};
2680
2681static struct sys_device kvm_sysdev = {
2682 .id = 0,
2683 .cls = &kvm_sysdev_class,
2684};
2685
Izik Eiduscea7bb22007-10-17 19:17:48 +02002686struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002687pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688
Avi Kivity15ad7142007-07-11 18:17:21 +03002689static inline
2690struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2691{
2692 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2693}
2694
2695static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2696{
2697 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2698
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002699 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002700}
2701
2702static void kvm_sched_out(struct preempt_notifier *pn,
2703 struct task_struct *next)
2704{
2705 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2706
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002707 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002708}
2709
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002710int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002711 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712{
2713 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002714 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002716 r = kvm_arch_init(opaque);
2717 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002718 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002719
2720 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2721
2722 if (bad_page == NULL) {
2723 r = -ENOMEM;
2724 goto out;
2725 }
2726
Anthony Liguori35149e22008-04-02 14:46:56 -05002727 bad_pfn = page_to_pfn(bad_page);
2728
Avi Kivity8437a612009-06-06 14:52:35 -07002729 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302730 r = -ENOMEM;
2731 goto out_free_0;
2732 }
2733
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002734 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302736 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737
Yang, Sheng002c7f72007-07-31 14:23:01 +03002738 for_each_online_cpu(cpu) {
2739 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002740 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002741 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002742 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002743 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002744 }
2745
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002746 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002747 r = register_cpu_notifier(&kvm_cpu_notifier);
2748 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002749 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 register_reboot_notifier(&kvm_reboot_notifier);
2751
Avi Kivity59ae6c62007-02-12 00:54:48 -08002752 r = sysdev_class_register(&kvm_sysdev_class);
2753 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002754 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002755
2756 r = sysdev_register(&kvm_sysdev);
2757 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002758 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002759
Rusty Russellc16f8622007-07-30 21:12:19 +10002760 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2761 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002762 __alignof__(struct kvm_vcpu),
2763 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002764 if (!kvm_vcpu_cache) {
2765 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002766 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002767 }
2768
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002770 kvm_vm_fops.owner = module;
2771 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772
2773 r = misc_register(&kvm_dev);
2774 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002775 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 goto out_free;
2777 }
2778
Avi Kivity15ad7142007-07-11 18:17:21 +03002779 kvm_preempt_ops.sched_in = kvm_sched_in;
2780 kvm_preempt_ops.sched_out = kvm_sched_out;
2781
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002782 kvm_init_debug();
2783
Avi Kivityc7addb92007-09-16 18:58:32 +02002784 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785
2786out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002787 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002788out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002789 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002790out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002791 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002792out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002794 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002795out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002796 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002797out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002798 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302799out_free_0a:
2800 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002801out_free_0:
2802 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002803out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002804 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002805out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 return r;
2807}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002808EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002810void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002812 tracepoint_synchronize_unregister();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002813 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002815 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002816 sysdev_unregister(&kvm_sysdev);
2817 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002819 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002820 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002821 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002822 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302823 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002824 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002826EXPORT_SYMBOL_GPL(kvm_exit);