blob: cdca63917e772cdb56a58046ac1d6f2a2f5d9bcd [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>
Alexander Grafc8240bd2009-10-30 05:47:26 +000051#include <asm-generic/bitops/le.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080052
Laurent Vivier5f94c172008-05-30 16:05:54 +020053#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
54#include "coalesced_mmio.h"
55#endif
56
Xiantao Zhang8a98f662008-10-06 13:47:38 +080057#ifdef KVM_CAP_DEVICE_ASSIGNMENT
58#include <linux/pci.h>
59#include <linux/interrupt.h>
60#include "irq.h"
61#endif
62
Marcelo Tosatti229456f2009-06-17 09:22:14 -030063#define CREATE_TRACE_POINTS
64#include <trace/events/kvm.h>
65
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Marcelo Tosattifa40a822009-06-04 15:08:24 -030069/*
70 * Ordering of locks:
71 *
Michael S. Tsirkin22fc0292009-06-29 22:24:45 +030072 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030073 */
74
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080075DEFINE_SPINLOCK(kvm_lock);
76LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080077
Rusty Russell7f59f492008-12-07 21:25:45 +103078static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030079
Rusty Russellc16f8622007-07-30 21:12:19 +100080struct kmem_cache *kvm_vcpu_cache;
81EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030082
Avi Kivity15ad7142007-07-11 18:17:21 +030083static __read_mostly struct preempt_ops kvm_preempt_ops;
84
Hollis Blanchard76f7c872008-04-15 16:05:42 -050085struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080086
Avi Kivitybccf2152007-02-21 18:04:26 +020087static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
88 unsigned long arg);
89
Hannes Edere8ba5d32008-11-28 17:02:42 +010090static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030091
Marcelo Tosatti54dee992009-06-11 12:07:44 -030092static bool largepages_enabled = true;
93
Xiantao Zhang8a98f662008-10-06 13:47:38 +080094#ifdef KVM_CAP_DEVICE_ASSIGNMENT
95static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
96 int assigned_dev_id)
97{
98 struct list_head *ptr;
99 struct kvm_assigned_dev_kernel *match;
100
101 list_for_each(ptr, head) {
102 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
103 if (match->assigned_dev_id == assigned_dev_id)
104 return match;
105 }
106 return NULL;
107}
108
Sheng Yang2350bd12009-02-25 17:22:27 +0800109static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
110 *assigned_dev, int irq)
111{
112 int i, index;
113 struct msix_entry *host_msix_entries;
114
115 host_msix_entries = assigned_dev->host_msix_entries;
116
117 index = -1;
118 for (i = 0; i < assigned_dev->entries_nr; i++)
119 if (irq == host_msix_entries[i].vector) {
120 index = i;
121 break;
122 }
123 if (index < 0) {
124 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
125 return 0;
126 }
127
128 return index;
129}
130
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800131static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
132{
133 struct kvm_assigned_dev_kernel *assigned_dev;
Sheng Yang2350bd12009-02-25 17:22:27 +0800134 struct kvm *kvm;
Sheng Yang968a6342009-04-30 10:58:42 +0800135 int i;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800136
137 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
138 interrupt_work);
Sheng Yang2350bd12009-02-25 17:22:27 +0800139 kvm = assigned_dev->kvm;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800140
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300141 mutex_lock(&kvm->irq_lock);
Marcelo Tosatti547de292009-05-07 17:55:13 -0300142 spin_lock_irq(&assigned_dev->assigned_dev_lock);
Sheng Yange56d5322009-03-12 21:45:39 +0800143 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800144 struct kvm_guest_msix_entry *guest_entries =
145 assigned_dev->guest_msix_entries;
146 for (i = 0; i < assigned_dev->entries_nr; i++) {
147 if (!(guest_entries[i].flags &
148 KVM_ASSIGNED_MSIX_PENDING))
149 continue;
150 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
151 kvm_set_irq(assigned_dev->kvm,
152 assigned_dev->irq_source_id,
153 guest_entries[i].vector, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800154 }
Sheng Yang968a6342009-04-30 10:58:42 +0800155 } else
Sheng Yang2350bd12009-02-25 17:22:27 +0800156 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
157 assigned_dev->guest_irq, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800158
Marcelo Tosatti547de292009-05-07 17:55:13 -0300159 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300160 mutex_unlock(&assigned_dev->kvm->irq_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800161}
162
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800163static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
164{
Marcelo Tosatti547de292009-05-07 17:55:13 -0300165 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800166 struct kvm_assigned_dev_kernel *assigned_dev =
167 (struct kvm_assigned_dev_kernel *) dev_id;
168
Marcelo Tosatti547de292009-05-07 17:55:13 -0300169 spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
Sheng Yange56d5322009-03-12 21:45:39 +0800170 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800171 int index = find_index_from_host_irq(assigned_dev, irq);
172 if (index < 0)
Marcelo Tosatti547de292009-05-07 17:55:13 -0300173 goto out;
Sheng Yang2350bd12009-02-25 17:22:27 +0800174 assigned_dev->guest_msix_entries[index].flags |=
175 KVM_ASSIGNED_MSIX_PENDING;
176 }
177
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800178 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000179
Sheng Yang968a6342009-04-30 10:58:42 +0800180 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
181 disable_irq_nosync(irq);
182 assigned_dev->host_irq_disabled = true;
183 }
Mark McLoughlindefaf152008-12-02 12:16:33 +0000184
Marcelo Tosatti547de292009-05-07 17:55:13 -0300185out:
186 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800187 return IRQ_HANDLED;
188}
189
190/* Ack the irq line for an assigned device */
191static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
192{
193 struct kvm_assigned_dev_kernel *dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300194 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800195
196 if (kian->gsi == -1)
197 return;
198
199 dev = container_of(kian, struct kvm_assigned_dev_kernel,
200 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000201
Sheng Yang5550af42008-10-15 20:15:06 +0800202 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000203
204 /* The guest irq may be shared so this ack may be
205 * from another device.
206 */
Marcelo Tosatti547de292009-05-07 17:55:13 -0300207 spin_lock_irqsave(&dev->assigned_dev_lock, flags);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000208 if (dev->host_irq_disabled) {
209 enable_irq(dev->host_irq);
210 dev->host_irq_disabled = false;
211 }
Marcelo Tosatti547de292009-05-07 17:55:13 -0300212 spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800213}
214
Sheng Yange56d5322009-03-12 21:45:39 +0800215static void deassign_guest_irq(struct kvm *kvm,
216 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800217{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300218 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
Sheng Yange56d5322009-03-12 21:45:39 +0800219 assigned_dev->ack_notifier.gsi = -1;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000220
221 if (assigned_dev->irq_source_id != -1)
222 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
223 assigned_dev->irq_source_id = -1;
Sheng Yange56d5322009-03-12 21:45:39 +0800224 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
225}
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800226
Sheng Yange56d5322009-03-12 21:45:39 +0800227/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
228static void deassign_host_irq(struct kvm *kvm,
229 struct kvm_assigned_dev_kernel *assigned_dev)
230{
Sheng Yangba4cef32009-01-06 10:03:03 +0800231 /*
232 * In kvm_free_device_irq, cancel_work_sync return true if:
233 * 1. work is scheduled, and then cancelled.
234 * 2. work callback is executed.
235 *
236 * The first one ensured that the irq is disabled and no more events
237 * would happen. But for the second one, the irq may be enabled (e.g.
238 * for MSI). So we disable irq here to prevent further events.
239 *
240 * Notice this maybe result in nested disable if the interrupt type is
241 * INTx, but it's OK for we are going to free it.
242 *
243 * If this function is a part of VM destroy, please ensure that till
244 * now, the kvm state is still legal for probably we also have to wait
245 * interrupt_work done.
246 */
Sheng Yange56d5322009-03-12 21:45:39 +0800247 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yangd510d6c2009-02-25 17:22:28 +0800248 int i;
249 for (i = 0; i < assigned_dev->entries_nr; i++)
250 disable_irq_nosync(assigned_dev->
251 host_msix_entries[i].vector);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800252
Sheng Yangd510d6c2009-02-25 17:22:28 +0800253 cancel_work_sync(&assigned_dev->interrupt_work);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000254
Sheng Yangd510d6c2009-02-25 17:22:28 +0800255 for (i = 0; i < assigned_dev->entries_nr; i++)
256 free_irq(assigned_dev->host_msix_entries[i].vector,
257 (void *)assigned_dev);
258
259 assigned_dev->entries_nr = 0;
260 kfree(assigned_dev->host_msix_entries);
261 kfree(assigned_dev->guest_msix_entries);
262 pci_disable_msix(assigned_dev->dev);
263 } else {
264 /* Deal with MSI and INTx */
265 disable_irq_nosync(assigned_dev->host_irq);
266 cancel_work_sync(&assigned_dev->interrupt_work);
267
268 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
269
Sheng Yange56d5322009-03-12 21:45:39 +0800270 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800271 pci_disable_msi(assigned_dev->dev);
272 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000273
Sheng Yange56d5322009-03-12 21:45:39 +0800274 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000275}
276
Sheng Yange56d5322009-03-12 21:45:39 +0800277static int kvm_deassign_irq(struct kvm *kvm,
278 struct kvm_assigned_dev_kernel *assigned_dev,
279 unsigned long irq_requested_type)
280{
281 unsigned long guest_irq_type, host_irq_type;
282
283 if (!irqchip_in_kernel(kvm))
284 return -EINVAL;
285 /* no irq assignment to deassign */
286 if (!assigned_dev->irq_requested_type)
287 return -ENXIO;
288
289 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
290 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
291
292 if (host_irq_type)
293 deassign_host_irq(kvm, assigned_dev);
294 if (guest_irq_type)
295 deassign_guest_irq(kvm, assigned_dev);
296
297 return 0;
298}
299
300static void kvm_free_assigned_irq(struct kvm *kvm,
301 struct kvm_assigned_dev_kernel *assigned_dev)
302{
303 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
304}
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000305
306static void kvm_free_assigned_device(struct kvm *kvm,
307 struct kvm_assigned_dev_kernel
308 *assigned_dev)
309{
310 kvm_free_assigned_irq(kvm, assigned_dev);
311
Sheng Yang6eb55812008-10-31 12:37:41 +0800312 pci_reset_function(assigned_dev->dev);
313
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800314 pci_release_regions(assigned_dev->dev);
315 pci_disable_device(assigned_dev->dev);
316 pci_dev_put(assigned_dev->dev);
317
318 list_del(&assigned_dev->list);
319 kfree(assigned_dev);
320}
321
322void kvm_free_all_assigned_devices(struct kvm *kvm)
323{
324 struct list_head *ptr, *ptr2;
325 struct kvm_assigned_dev_kernel *assigned_dev;
326
327 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
328 assigned_dev = list_entry(ptr,
329 struct kvm_assigned_dev_kernel,
330 list);
331
332 kvm_free_assigned_device(kvm, assigned_dev);
333 }
334}
335
Sheng Yange56d5322009-03-12 21:45:39 +0800336static int assigned_device_enable_host_intx(struct kvm *kvm,
337 struct kvm_assigned_dev_kernel *dev)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800338{
Sheng Yange56d5322009-03-12 21:45:39 +0800339 dev->host_irq = dev->dev->irq;
340 /* Even though this is PCI, we don't want to use shared
341 * interrupts. Sharing host devices with guest-assigned devices
342 * on the same interrupt line is not a happy situation: there
343 * are going to be long delays in accepting, acking, etc.
344 */
345 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
346 0, "kvm_assigned_intx_device", (void *)dev))
347 return -EIO;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800348 return 0;
349}
350
Sheng Yange56d5322009-03-12 21:45:39 +0800351#ifdef __KVM_HAVE_MSI
352static int assigned_device_enable_host_msi(struct kvm *kvm,
353 struct kvm_assigned_dev_kernel *dev)
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800354{
355 int r;
356
Sheng Yange56d5322009-03-12 21:45:39 +0800357 if (!dev->dev->msi_enabled) {
358 r = pci_enable_msi(dev->dev);
359 if (r)
360 return r;
Sheng Yang5319c662008-11-24 14:32:57 +0800361 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800362
Sheng Yange56d5322009-03-12 21:45:39 +0800363 dev->host_irq = dev->dev->irq;
364 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
365 "kvm_assigned_msi_device", (void *)dev)) {
366 pci_disable_msi(dev->dev);
367 return -EIO;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800368 }
369
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800370 return 0;
371}
372#endif
373
Sheng Yangd510d6c2009-02-25 17:22:28 +0800374#ifdef __KVM_HAVE_MSIX
Sheng Yange56d5322009-03-12 21:45:39 +0800375static int assigned_device_enable_host_msix(struct kvm *kvm,
376 struct kvm_assigned_dev_kernel *dev)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800377{
Sheng Yange56d5322009-03-12 21:45:39 +0800378 int i, r = -EINVAL;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800379
Sheng Yange56d5322009-03-12 21:45:39 +0800380 /* host_msix_entries and guest_msix_entries should have been
381 * initialized */
382 if (dev->entries_nr == 0)
383 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800384
Sheng Yange56d5322009-03-12 21:45:39 +0800385 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
386 if (r)
387 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800388
Sheng Yange56d5322009-03-12 21:45:39 +0800389 for (i = 0; i < dev->entries_nr; i++) {
390 r = request_irq(dev->host_msix_entries[i].vector,
391 kvm_assigned_dev_intr, 0,
392 "kvm_assigned_msix_device",
393 (void *)dev);
394 /* FIXME: free requested_irq's on failure */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800395 if (r)
396 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800397 }
398
Sheng Yange56d5322009-03-12 21:45:39 +0800399 return 0;
400}
Sheng Yangd510d6c2009-02-25 17:22:28 +0800401
Sheng Yange56d5322009-03-12 21:45:39 +0800402#endif
403
404static int assigned_device_enable_guest_intx(struct kvm *kvm,
405 struct kvm_assigned_dev_kernel *dev,
406 struct kvm_assigned_irq *irq)
407{
408 dev->guest_irq = irq->guest_irq;
409 dev->ack_notifier.gsi = irq->guest_irq;
410 return 0;
411}
412
413#ifdef __KVM_HAVE_MSI
414static int assigned_device_enable_guest_msi(struct kvm *kvm,
415 struct kvm_assigned_dev_kernel *dev,
416 struct kvm_assigned_irq *irq)
417{
418 dev->guest_irq = irq->guest_irq;
419 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800420 dev->host_irq_disabled = false;
Sheng Yange56d5322009-03-12 21:45:39 +0800421 return 0;
422}
423#endif
424#ifdef __KVM_HAVE_MSIX
425static int assigned_device_enable_guest_msix(struct kvm *kvm,
426 struct kvm_assigned_dev_kernel *dev,
427 struct kvm_assigned_irq *irq)
428{
429 dev->guest_irq = irq->guest_irq;
430 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800431 dev->host_irq_disabled = false;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800432 return 0;
433}
434#endif
435
Sheng Yange56d5322009-03-12 21:45:39 +0800436static int assign_host_irq(struct kvm *kvm,
437 struct kvm_assigned_dev_kernel *dev,
438 __u32 host_irq_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800439{
Sheng Yange56d5322009-03-12 21:45:39 +0800440 int r = -EEXIST;
441
442 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
443 return r;
444
445 switch (host_irq_type) {
446 case KVM_DEV_IRQ_HOST_INTX:
447 r = assigned_device_enable_host_intx(kvm, dev);
448 break;
449#ifdef __KVM_HAVE_MSI
450 case KVM_DEV_IRQ_HOST_MSI:
451 r = assigned_device_enable_host_msi(kvm, dev);
452 break;
453#endif
454#ifdef __KVM_HAVE_MSIX
455 case KVM_DEV_IRQ_HOST_MSIX:
456 r = assigned_device_enable_host_msix(kvm, dev);
457 break;
458#endif
459 default:
460 r = -EINVAL;
461 }
462
463 if (!r)
464 dev->irq_requested_type |= host_irq_type;
465
466 return r;
467}
468
469static int assign_guest_irq(struct kvm *kvm,
470 struct kvm_assigned_dev_kernel *dev,
471 struct kvm_assigned_irq *irq,
472 unsigned long guest_irq_type)
473{
474 int id;
475 int r = -EEXIST;
476
477 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
478 return r;
479
480 id = kvm_request_irq_source_id(kvm);
481 if (id < 0)
482 return id;
483
484 dev->irq_source_id = id;
485
486 switch (guest_irq_type) {
487 case KVM_DEV_IRQ_GUEST_INTX:
488 r = assigned_device_enable_guest_intx(kvm, dev, irq);
489 break;
490#ifdef __KVM_HAVE_MSI
491 case KVM_DEV_IRQ_GUEST_MSI:
492 r = assigned_device_enable_guest_msi(kvm, dev, irq);
493 break;
494#endif
495#ifdef __KVM_HAVE_MSIX
496 case KVM_DEV_IRQ_GUEST_MSIX:
497 r = assigned_device_enable_guest_msix(kvm, dev, irq);
498 break;
499#endif
500 default:
501 r = -EINVAL;
502 }
503
504 if (!r) {
505 dev->irq_requested_type |= guest_irq_type;
506 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
507 } else
508 kvm_free_irq_source_id(kvm, dev->irq_source_id);
509
510 return r;
511}
512
513/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
514static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
515 struct kvm_assigned_irq *assigned_irq)
516{
517 int r = -EINVAL;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800518 struct kvm_assigned_dev_kernel *match;
Sheng Yange56d5322009-03-12 21:45:39 +0800519 unsigned long host_irq_type, guest_irq_type;
520
521 if (!capable(CAP_SYS_RAWIO))
522 return -EPERM;
523
524 if (!irqchip_in_kernel(kvm))
525 return r;
526
527 mutex_lock(&kvm->lock);
528 r = -ENODEV;
529 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
530 assigned_irq->assigned_dev_id);
531 if (!match)
532 goto out;
533
534 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
535 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
536
537 r = -EINVAL;
538 /* can only assign one type at a time */
539 if (hweight_long(host_irq_type) > 1)
540 goto out;
541 if (hweight_long(guest_irq_type) > 1)
542 goto out;
543 if (host_irq_type == 0 && guest_irq_type == 0)
544 goto out;
545
546 r = 0;
547 if (host_irq_type)
548 r = assign_host_irq(kvm, match, host_irq_type);
549 if (r)
550 goto out;
551
552 if (guest_irq_type)
553 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
554out:
555 mutex_unlock(&kvm->lock);
556 return r;
557}
558
559static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
560 struct kvm_assigned_irq
561 *assigned_irq)
562{
563 int r = -ENODEV;
564 struct kvm_assigned_dev_kernel *match;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800565
566 mutex_lock(&kvm->lock);
567
568 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
569 assigned_irq->assigned_dev_id);
Sheng Yange56d5322009-03-12 21:45:39 +0800570 if (!match)
571 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800572
Sheng Yange56d5322009-03-12 21:45:39 +0800573 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
574out:
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800575 mutex_unlock(&kvm->lock);
576 return r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800577}
578
579static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
580 struct kvm_assigned_pci_dev *assigned_dev)
581{
582 int r = 0;
583 struct kvm_assigned_dev_kernel *match;
584 struct pci_dev *dev;
585
Mark McLoughlin682edb42009-02-05 18:23:46 +0000586 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800587 mutex_lock(&kvm->lock);
588
589 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
590 assigned_dev->assigned_dev_id);
591 if (match) {
592 /* device already assigned */
Sheng Yange56d5322009-03-12 21:45:39 +0800593 r = -EEXIST;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800594 goto out;
595 }
596
597 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
598 if (match == NULL) {
599 printk(KERN_INFO "%s: Couldn't allocate memory\n",
600 __func__);
601 r = -ENOMEM;
602 goto out;
603 }
604 dev = pci_get_bus_and_slot(assigned_dev->busnr,
605 assigned_dev->devfn);
606 if (!dev) {
607 printk(KERN_INFO "%s: host device not found\n", __func__);
608 r = -EINVAL;
609 goto out_free;
610 }
611 if (pci_enable_device(dev)) {
612 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
613 r = -EBUSY;
614 goto out_put;
615 }
616 r = pci_request_regions(dev, "kvm_assigned_device");
617 if (r) {
618 printk(KERN_INFO "%s: Could not get access to device regions\n",
619 __func__);
620 goto out_disable;
621 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800622
623 pci_reset_function(dev);
624
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800625 match->assigned_dev_id = assigned_dev->assigned_dev_id;
626 match->host_busnr = assigned_dev->busnr;
627 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800628 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800629 match->dev = dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300630 spin_lock_init(&match->assigned_dev_lock);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000631 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800632 match->kvm = kvm;
Sheng Yange56d5322009-03-12 21:45:39 +0800633 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
634 INIT_WORK(&match->interrupt_work,
635 kvm_assigned_dev_interrupt_work_handler);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800636
637 list_add(&match->list, &kvm->arch.assigned_dev_head);
638
639 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100640 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800641 r = kvm_iommu_map_guest(kvm);
642 if (r)
643 goto out_list_del;
644 }
645 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800646 if (r)
647 goto out_list_del;
648 }
649
650out:
651 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000652 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800653 return r;
654out_list_del:
655 list_del(&match->list);
656 pci_release_regions(dev);
657out_disable:
658 pci_disable_device(dev);
659out_put:
660 pci_dev_put(dev);
661out_free:
662 kfree(match);
663 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000664 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800665 return r;
666}
667#endif
668
Weidong Han0a920352008-12-02 21:24:23 +0800669#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
670static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
671 struct kvm_assigned_pci_dev *assigned_dev)
672{
673 int r = 0;
674 struct kvm_assigned_dev_kernel *match;
675
676 mutex_lock(&kvm->lock);
677
678 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
679 assigned_dev->assigned_dev_id);
680 if (!match) {
681 printk(KERN_INFO "%s: device hasn't been assigned before, "
682 "so cannot be deassigned\n", __func__);
683 r = -EINVAL;
684 goto out;
685 }
686
Weidong Han4a906e42009-02-13 17:27:51 +0800687 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800688 kvm_deassign_device(kvm, match);
689
690 kvm_free_assigned_device(kvm, match);
691
692out:
693 mutex_unlock(&kvm->lock);
694 return r;
695}
696#endif
697
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800698inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300699{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100700 if (pfn_valid(pfn)) {
701 struct page *page = compound_head(pfn_to_page(pfn));
702 return PageReserved(page);
703 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300704
705 return true;
706}
707
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708/*
709 * Switches to specified vcpu, until a matching vcpu_put()
710 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200711void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712{
Avi Kivity15ad7142007-07-11 18:17:21 +0300713 int cpu;
714
Avi Kivitybccf2152007-02-21 18:04:26 +0200715 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300716 cpu = get_cpu();
717 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200718 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300719 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200720}
721
Carsten Otte313a3dc2007-10-11 19:16:52 +0200722void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723{
Avi Kivity15ad7142007-07-11 18:17:21 +0300724 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200725 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300726 preempt_notifier_unregister(&vcpu->preempt_notifier);
727 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 mutex_unlock(&vcpu->mutex);
729}
730
Avi Kivityd9e368d2007-06-07 19:18:30 +0300731static void ack_flush(void *_completed)
732{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300733}
734
Rusty Russell49846892008-12-08 20:26:24 +1030735static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300736{
Avi Kivity597a5f52008-07-20 14:24:22 +0300737 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030738 cpumask_var_t cpus;
739 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300740 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300741
Li Zefan79f55992009-06-15 14:58:26 +0800742 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030743
Marcelo Tosatti84261922009-06-17 10:53:47 -0300744 spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200745 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300746 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030747 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300748 continue;
749 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030750 if (cpus != NULL && cpu != -1 && cpu != me)
751 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300752 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030753 if (unlikely(cpus == NULL))
754 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
755 else if (!cpumask_empty(cpus))
756 smp_call_function_many(cpus, ack_flush, NULL, 1);
757 else
758 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300759 spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030760 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030761 return called;
762}
763
764void kvm_flush_remote_tlbs(struct kvm *kvm)
765{
766 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
767 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300768}
769
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500770void kvm_reload_remote_mmus(struct kvm *kvm)
771{
Rusty Russell49846892008-12-08 20:26:24 +1030772 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500773}
774
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000775int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
776{
777 struct page *page;
778 int r;
779
780 mutex_init(&vcpu->mutex);
781 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000782 vcpu->kvm = kvm;
783 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300784 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000785
786 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
787 if (!page) {
788 r = -ENOMEM;
789 goto fail;
790 }
791 vcpu->run = page_address(page);
792
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800793 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000794 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800795 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000796 return 0;
797
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000798fail_free_run:
799 free_page((unsigned long)vcpu->run);
800fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000801 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000802}
803EXPORT_SYMBOL_GPL(kvm_vcpu_init);
804
805void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
806{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800807 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000808 free_page((unsigned long)vcpu->run);
809}
810EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
811
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200812#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
813static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
814{
815 return container_of(mn, struct kvm, mmu_notifier);
816}
817
818static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
819 struct mm_struct *mm,
820 unsigned long address)
821{
822 struct kvm *kvm = mmu_notifier_to_kvm(mn);
823 int need_tlb_flush;
824
825 /*
826 * When ->invalidate_page runs, the linux pte has been zapped
827 * already but the page is still allocated until
828 * ->invalidate_page returns. So if we increase the sequence
829 * here the kvm page fault will notice if the spte can't be
830 * established because the page is going to be freed. If
831 * instead the kvm page fault establishes the spte before
832 * ->invalidate_page runs, kvm_unmap_hva will release it
833 * before returning.
834 *
835 * The sequence increase only need to be seen at spin_unlock
836 * time, and not at spin_lock time.
837 *
838 * Increasing the sequence after the spin_unlock would be
839 * unsafe because the kvm page fault could then establish the
840 * pte after kvm_unmap_hva returned, without noticing the page
841 * is going to be freed.
842 */
843 spin_lock(&kvm->mmu_lock);
844 kvm->mmu_notifier_seq++;
845 need_tlb_flush = kvm_unmap_hva(kvm, address);
846 spin_unlock(&kvm->mmu_lock);
847
848 /* we've to flush the tlb before the pages can be freed */
849 if (need_tlb_flush)
850 kvm_flush_remote_tlbs(kvm);
851
852}
853
Izik Eidus3da0dd42009-09-23 21:47:18 +0300854static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
855 struct mm_struct *mm,
856 unsigned long address,
857 pte_t pte)
858{
859 struct kvm *kvm = mmu_notifier_to_kvm(mn);
860
861 spin_lock(&kvm->mmu_lock);
862 kvm->mmu_notifier_seq++;
863 kvm_set_spte_hva(kvm, address, pte);
864 spin_unlock(&kvm->mmu_lock);
865}
866
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200867static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
868 struct mm_struct *mm,
869 unsigned long start,
870 unsigned long end)
871{
872 struct kvm *kvm = mmu_notifier_to_kvm(mn);
873 int need_tlb_flush = 0;
874
875 spin_lock(&kvm->mmu_lock);
876 /*
877 * The count increase must become visible at unlock time as no
878 * spte can be established without taking the mmu_lock and
879 * count is also read inside the mmu_lock critical section.
880 */
881 kvm->mmu_notifier_count++;
882 for (; start < end; start += PAGE_SIZE)
883 need_tlb_flush |= kvm_unmap_hva(kvm, start);
884 spin_unlock(&kvm->mmu_lock);
885
886 /* we've to flush the tlb before the pages can be freed */
887 if (need_tlb_flush)
888 kvm_flush_remote_tlbs(kvm);
889}
890
891static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
892 struct mm_struct *mm,
893 unsigned long start,
894 unsigned long end)
895{
896 struct kvm *kvm = mmu_notifier_to_kvm(mn);
897
898 spin_lock(&kvm->mmu_lock);
899 /*
900 * This sequence increase will notify the kvm page fault that
901 * the page that is going to be mapped in the spte could have
902 * been freed.
903 */
904 kvm->mmu_notifier_seq++;
905 /*
906 * The above sequence increase must be visible before the
907 * below count decrease but both values are read by the kvm
908 * page fault under mmu_lock spinlock so we don't need to add
909 * a smb_wmb() here in between the two.
910 */
911 kvm->mmu_notifier_count--;
912 spin_unlock(&kvm->mmu_lock);
913
914 BUG_ON(kvm->mmu_notifier_count < 0);
915}
916
917static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
918 struct mm_struct *mm,
919 unsigned long address)
920{
921 struct kvm *kvm = mmu_notifier_to_kvm(mn);
922 int young;
923
924 spin_lock(&kvm->mmu_lock);
925 young = kvm_age_hva(kvm, address);
926 spin_unlock(&kvm->mmu_lock);
927
928 if (young)
929 kvm_flush_remote_tlbs(kvm);
930
931 return young;
932}
933
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100934static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
935 struct mm_struct *mm)
936{
937 struct kvm *kvm = mmu_notifier_to_kvm(mn);
938 kvm_arch_flush_shadow(kvm);
939}
940
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200941static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
942 .invalidate_page = kvm_mmu_notifier_invalidate_page,
943 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
944 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
945 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300946 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100947 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200948};
949#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
950
Avi Kivityf17abe92007-02-21 19:28:04 +0200951static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800953 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200954#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
955 struct page *page;
956#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800958 if (IS_ERR(kvm))
959 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200960#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200961 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200962 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
963#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964
Laurent Vivier5f94c172008-05-30 16:05:54 +0200965#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
966 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
967 if (!page) {
968 kfree(kvm);
969 return ERR_PTR(-ENOMEM);
970 }
971 kvm->coalesced_mmio_ring =
972 (struct kvm_coalesced_mmio_ring *)page_address(page);
973#endif
974
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200975#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
976 {
977 int err;
978 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
979 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
980 if (err) {
981#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
982 put_page(page);
983#endif
984 kfree(kvm);
985 return ERR_PTR(err);
986 }
987 }
988#endif
989
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200990 kvm->mm = current->mm;
991 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500992 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300993 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300994 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400995 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800996 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300997 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400998 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200999 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +03001000 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +10001001 spin_lock(&kvm_lock);
1002 list_add(&kvm->vm_list, &vm_list);
1003 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001004#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1005 kvm_coalesced_mmio_init(kvm);
1006#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001007out:
Avi Kivityf17abe92007-02-21 19:28:04 +02001008 return kvm;
1009}
1010
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011/*
1012 * Free any memory in @free but not in @dont.
1013 */
1014static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1015 struct kvm_memory_slot *dont)
1016{
Joerg Roedelec04b262009-06-19 15:16:23 +02001017 int i;
1018
Izik Eidus290fc382007-09-27 14:11:22 +02001019 if (!dont || free->rmap != dont->rmap)
1020 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
1022 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1023 vfree(free->dirty_bitmap);
1024
Joerg Roedelec04b262009-06-19 15:16:23 +02001025
1026 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1027 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
1028 vfree(free->lpage_info[i]);
1029 free->lpage_info[i] = NULL;
1030 }
1031 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001032
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001034 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001035 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036}
1037
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001038void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039{
1040 int i;
1041
1042 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001043 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044}
1045
Avi Kivityf17abe92007-02-21 19:28:04 +02001046static void kvm_destroy_vm(struct kvm *kvm)
1047{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001048 struct mm_struct *mm = kvm->mm;
1049
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001050 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001051 spin_lock(&kvm_lock);
1052 list_del(&kvm->vm_list);
1053 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001054 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001055 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001056 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001057#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1058 if (kvm->coalesced_mmio_ring != NULL)
1059 free_page((unsigned long)kvm->coalesced_mmio_ring);
1060#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001061#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1062 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +02001063#else
1064 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001065#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001066 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001067 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001068}
1069
Izik Eidusd39f13b2008-03-30 16:01:25 +03001070void kvm_get_kvm(struct kvm *kvm)
1071{
1072 atomic_inc(&kvm->users_count);
1073}
1074EXPORT_SYMBOL_GPL(kvm_get_kvm);
1075
1076void kvm_put_kvm(struct kvm *kvm)
1077{
1078 if (atomic_dec_and_test(&kvm->users_count))
1079 kvm_destroy_vm(kvm);
1080}
1081EXPORT_SYMBOL_GPL(kvm_put_kvm);
1082
1083
Avi Kivityf17abe92007-02-21 19:28:04 +02001084static int kvm_vm_release(struct inode *inode, struct file *filp)
1085{
1086 struct kvm *kvm = filp->private_data;
1087
Gregory Haskins721eecb2009-05-20 10:30:49 -04001088 kvm_irqfd_release(kvm);
1089
Izik Eidusd39f13b2008-03-30 16:01:25 +03001090 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 return 0;
1092}
1093
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 * Allocate some memory and give it an address in the guest physical address
1096 * space.
1097 *
1098 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001099 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001100 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001102int __kvm_set_memory_region(struct kvm *kvm,
1103 struct kvm_userspace_memory_region *mem,
1104 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105{
1106 int r;
1107 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +02001108 unsigned long npages;
1109 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 struct kvm_memory_slot *memslot;
1111 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112
1113 r = -EINVAL;
1114 /* General sanity checks */
1115 if (mem->memory_size & (PAGE_SIZE - 1))
1116 goto out;
1117 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1118 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001119 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001120 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001121 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122 goto out;
1123 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1124 goto out;
1125
1126 memslot = &kvm->memslots[mem->slot];
1127 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1128 npages = mem->memory_size >> PAGE_SHIFT;
1129
1130 if (!npages)
1131 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1132
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 new = old = *memslot;
1134
1135 new.base_gfn = base_gfn;
1136 new.npages = npages;
1137 new.flags = mem->flags;
1138
1139 /* Disallow changing a memory slot's size. */
1140 r = -EINVAL;
1141 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001142 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143
1144 /* Check for overlaps */
1145 r = -EEXIST;
1146 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1147 struct kvm_memory_slot *s = &kvm->memslots[i];
1148
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001149 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 continue;
1151 if (!((base_gfn + npages <= s->base_gfn) ||
1152 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001153 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 /* Free page dirty bitmap if unneeded */
1157 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001158 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159
1160 r = -ENOMEM;
1161
1162 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001163#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001164 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001165 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001166
1167 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001168 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001169
Izik Eidus290fc382007-09-27 14:11:22 +02001170 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001171
Izik Eidus80b14b52007-10-25 11:54:04 +02001172 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001173 /*
1174 * hva_to_rmmap() serialzies with the mmu_lock and to be
1175 * safe it has to ignore memslots with !user_alloc &&
1176 * !userspace_addr.
1177 */
1178 if (user_alloc)
1179 new.userspace_addr = mem->userspace_addr;
1180 else
1181 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 }
Joerg Roedelec04b262009-06-19 15:16:23 +02001183 if (!npages)
1184 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001185
Joerg Roedelec04b262009-06-19 15:16:23 +02001186 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +02001187 unsigned long ugfn;
1188 unsigned long j;
1189 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +02001190 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001191
Joerg Roedelec04b262009-06-19 15:16:23 +02001192 /* Avoid unused variable warning if no large pages */
1193 (void)level;
1194
1195 if (new.lpage_info[i])
1196 continue;
1197
1198 lpages = 1 + (base_gfn + npages - 1) /
1199 KVM_PAGES_PER_HPAGE(level);
1200 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
1201
1202 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
1203
1204 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001205 goto out_free;
1206
Joerg Roedelec04b262009-06-19 15:16:23 +02001207 memset(new.lpage_info[i], 0,
1208 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001209
Joerg Roedelec04b262009-06-19 15:16:23 +02001210 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
1211 new.lpage_info[i][0].write_count = 1;
1212 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
1213 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +03001214 ugfn = new.userspace_addr >> PAGE_SHIFT;
1215 /*
1216 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001217 * other, or if explicitly asked to, disable large page
1218 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +03001219 */
Joerg Roedelec04b262009-06-19 15:16:23 +02001220 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001221 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +02001222 for (j = 0; j < lpages; ++j)
1223 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001224 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225
Joerg Roedelec04b262009-06-19 15:16:23 +02001226skip_lpage:
1227
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 /* Allocate page dirty bitmap if needed */
1229 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1230 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1231
1232 new.dirty_bitmap = vmalloc(dirty_bytes);
1233 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001234 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +03001236 if (old.npages)
1237 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +02001239#else /* not defined CONFIG_S390 */
1240 new.user_alloc = user_alloc;
1241 if (user_alloc)
1242 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +02001243#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001245 if (!npages)
1246 kvm_arch_flush_shadow(kvm);
1247
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001248 spin_lock(&kvm->mmu_lock);
1249 if (mem->slot >= kvm->nmemslots)
1250 kvm->nmemslots = mem->slot + 1;
1251
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001253 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254
Zhang Xiantao0de10342007-11-20 16:25:04 +08001255 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1256 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001257 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001258 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001259 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001260 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001261 }
1262
Glauber Costa6f897242008-12-03 13:40:51 -02001263 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1264 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001265 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -02001266 if (!npages)
1267 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001268 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001269#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001270 /* map the pages in iommu page table */
1271 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1272 if (r)
1273 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001274#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275 return 0;
1276
Sheng Yangf78e0e22007-10-29 09:40:42 +08001277out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278 kvm_free_physmem_slot(&new, &old);
1279out:
1280 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001281
1282}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001283EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1284
1285int kvm_set_memory_region(struct kvm *kvm,
1286 struct kvm_userspace_memory_region *mem,
1287 int user_alloc)
1288{
1289 int r;
1290
Izik Eidus72dc67a2008-02-10 18:04:15 +02001291 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001292 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001293 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001294 return r;
1295}
Izik Eidus210c7c42007-10-24 23:52:57 +02001296EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1297
Carsten Otte1fe779f2007-10-29 16:08:35 +01001298int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1299 struct
1300 kvm_userspace_memory_region *mem,
1301 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001302{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001303 if (mem->slot >= KVM_MEMORY_SLOTS)
1304 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001305 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306}
1307
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001308int kvm_get_dirty_log(struct kvm *kvm,
1309 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310{
1311 struct kvm_memory_slot *memslot;
1312 int r, i;
1313 int n;
1314 unsigned long any = 0;
1315
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 r = -EINVAL;
1317 if (log->slot >= KVM_MEMORY_SLOTS)
1318 goto out;
1319
1320 memslot = &kvm->memslots[log->slot];
1321 r = -ENOENT;
1322 if (!memslot->dirty_bitmap)
1323 goto out;
1324
Uri Lublincd1a4a92007-02-22 16:43:09 +02001325 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326
Uri Lublincd1a4a92007-02-22 16:43:09 +02001327 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328 any = memslot->dirty_bitmap[i];
1329
1330 r = -EFAULT;
1331 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1332 goto out;
1333
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001334 if (any)
1335 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336
1337 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339 return r;
1340}
1341
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001342void kvm_disable_largepages(void)
1343{
1344 largepages_enabled = false;
1345}
1346EXPORT_SYMBOL_GPL(kvm_disable_largepages);
1347
Izik Eiduscea7bb22007-10-17 19:17:48 +02001348int is_error_page(struct page *page)
1349{
1350 return page == bad_page;
1351}
1352EXPORT_SYMBOL_GPL(is_error_page);
1353
Anthony Liguori35149e22008-04-02 14:46:56 -05001354int is_error_pfn(pfn_t pfn)
1355{
1356 return pfn == bad_pfn;
1357}
1358EXPORT_SYMBOL_GPL(is_error_pfn);
1359
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001360static inline unsigned long bad_hva(void)
1361{
1362 return PAGE_OFFSET;
1363}
1364
1365int kvm_is_error_hva(unsigned long addr)
1366{
1367 return addr == bad_hva();
1368}
1369EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1370
Izik Eidus28430992008-10-03 17:40:32 +03001371struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372{
1373 int i;
1374
1375 for (i = 0; i < kvm->nmemslots; ++i) {
1376 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1377
1378 if (gfn >= memslot->base_gfn
1379 && gfn < memslot->base_gfn + memslot->npages)
1380 return memslot;
1381 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001382 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383}
Izik Eidus28430992008-10-03 17:40:32 +03001384EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001385
1386struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1387{
1388 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001389 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001390}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391
Izik Eiduse0d62c72007-10-24 23:57:46 +02001392int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1393{
1394 int i;
1395
1396 gfn = unalias_gfn(kvm, gfn);
1397 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1398 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1399
1400 if (gfn >= memslot->base_gfn
1401 && gfn < memslot->base_gfn + memslot->npages)
1402 return 1;
1403 }
1404 return 0;
1405}
1406EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1407
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001408unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001409{
1410 struct kvm_memory_slot *slot;
1411
1412 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001413 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001414 if (!slot)
1415 return bad_hva();
1416 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1417}
Sheng Yang0d150292008-04-25 21:44:50 +08001418EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001419
Anthony Liguori35149e22008-04-02 14:46:56 -05001420pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001421{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001422 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001423 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001424 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001425 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001426
Avi Kivity60395222007-10-21 11:03:36 +02001427 might_sleep();
1428
Izik Eidus539cb662007-11-11 22:05:04 +02001429 addr = gfn_to_hva(kvm, gfn);
1430 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001431 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001432 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001433 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001434
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001435 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001436
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001437 if (unlikely(npages != 1)) {
1438 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001439
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001440 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001441 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001442
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001443 if (vma == NULL || addr < vma->vm_start ||
1444 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001445 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001446 get_page(bad_page);
1447 return page_to_pfn(bad_page);
1448 }
1449
1450 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001451 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001452 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001453 } else
1454 pfn = page_to_pfn(page[0]);
1455
1456 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001457}
1458
1459EXPORT_SYMBOL_GPL(gfn_to_pfn);
1460
1461struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1462{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001463 pfn_t pfn;
1464
1465 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001466 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001467 return pfn_to_page(pfn);
1468
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001469 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001470
1471 get_page(bad_page);
1472 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001473}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001474
Avi Kivity954bbbc2007-03-30 14:02:32 +03001475EXPORT_SYMBOL_GPL(gfn_to_page);
1476
Izik Eidusb4231d62007-11-20 11:49:33 +02001477void kvm_release_page_clean(struct page *page)
1478{
Anthony Liguori35149e22008-04-02 14:46:56 -05001479 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001480}
1481EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1482
Anthony Liguori35149e22008-04-02 14:46:56 -05001483void kvm_release_pfn_clean(pfn_t pfn)
1484{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001485 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001486 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001487}
1488EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1489
Izik Eidusb4231d62007-11-20 11:49:33 +02001490void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001491{
Anthony Liguori35149e22008-04-02 14:46:56 -05001492 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001493}
Izik Eidusb4231d62007-11-20 11:49:33 +02001494EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001495
Anthony Liguori35149e22008-04-02 14:46:56 -05001496void kvm_release_pfn_dirty(pfn_t pfn)
1497{
1498 kvm_set_pfn_dirty(pfn);
1499 kvm_release_pfn_clean(pfn);
1500}
1501EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1502
1503void kvm_set_page_dirty(struct page *page)
1504{
1505 kvm_set_pfn_dirty(page_to_pfn(page));
1506}
1507EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1508
1509void kvm_set_pfn_dirty(pfn_t pfn)
1510{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001511 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001512 struct page *page = pfn_to_page(pfn);
1513 if (!PageReserved(page))
1514 SetPageDirty(page);
1515 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001516}
1517EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1518
1519void kvm_set_pfn_accessed(pfn_t pfn)
1520{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001521 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001522 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001523}
1524EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1525
1526void kvm_get_pfn(pfn_t pfn)
1527{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001528 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001529 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001530}
1531EXPORT_SYMBOL_GPL(kvm_get_pfn);
1532
Izik Eidus195aefd2007-10-01 22:14:18 +02001533static int next_segment(unsigned long len, int offset)
1534{
1535 if (len > PAGE_SIZE - offset)
1536 return PAGE_SIZE - offset;
1537 else
1538 return len;
1539}
1540
1541int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1542 int len)
1543{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001544 int r;
1545 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001546
Izik Eiduse0506bc2007-11-11 22:10:22 +02001547 addr = gfn_to_hva(kvm, gfn);
1548 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001549 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001550 r = copy_from_user(data, (void __user *)addr + offset, len);
1551 if (r)
1552 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001553 return 0;
1554}
1555EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1556
1557int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1558{
1559 gfn_t gfn = gpa >> PAGE_SHIFT;
1560 int seg;
1561 int offset = offset_in_page(gpa);
1562 int ret;
1563
1564 while ((seg = next_segment(len, offset)) != 0) {
1565 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1566 if (ret < 0)
1567 return ret;
1568 offset = 0;
1569 len -= seg;
1570 data += seg;
1571 ++gfn;
1572 }
1573 return 0;
1574}
1575EXPORT_SYMBOL_GPL(kvm_read_guest);
1576
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001577int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1578 unsigned long len)
1579{
1580 int r;
1581 unsigned long addr;
1582 gfn_t gfn = gpa >> PAGE_SHIFT;
1583 int offset = offset_in_page(gpa);
1584
1585 addr = gfn_to_hva(kvm, gfn);
1586 if (kvm_is_error_hva(addr))
1587 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001588 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001589 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001590 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001591 if (r)
1592 return -EFAULT;
1593 return 0;
1594}
1595EXPORT_SYMBOL(kvm_read_guest_atomic);
1596
Izik Eidus195aefd2007-10-01 22:14:18 +02001597int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1598 int offset, int len)
1599{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001600 int r;
1601 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001602
Izik Eiduse0506bc2007-11-11 22:10:22 +02001603 addr = gfn_to_hva(kvm, gfn);
1604 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001605 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001606 r = copy_to_user((void __user *)addr + offset, data, len);
1607 if (r)
1608 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001609 mark_page_dirty(kvm, gfn);
1610 return 0;
1611}
1612EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1613
1614int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1615 unsigned long len)
1616{
1617 gfn_t gfn = gpa >> PAGE_SHIFT;
1618 int seg;
1619 int offset = offset_in_page(gpa);
1620 int ret;
1621
1622 while ((seg = next_segment(len, offset)) != 0) {
1623 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1624 if (ret < 0)
1625 return ret;
1626 offset = 0;
1627 len -= seg;
1628 data += seg;
1629 ++gfn;
1630 }
1631 return 0;
1632}
1633
1634int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1635{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001636 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001637}
1638EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1639
1640int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1641{
1642 gfn_t gfn = gpa >> PAGE_SHIFT;
1643 int seg;
1644 int offset = offset_in_page(gpa);
1645 int ret;
1646
1647 while ((seg = next_segment(len, offset)) != 0) {
1648 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1649 if (ret < 0)
1650 return ret;
1651 offset = 0;
1652 len -= seg;
1653 ++gfn;
1654 }
1655 return 0;
1656}
1657EXPORT_SYMBOL_GPL(kvm_clear_guest);
1658
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1660{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001661 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662
Uri Lublin3b6fff12007-10-30 10:42:09 +02001663 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001664 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001665 if (memslot && memslot->dirty_bitmap) {
1666 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667
Rusty Russell7e9d6192007-07-31 20:41:14 +10001668 /* avoid RMW */
Alexander Grafc8240bd2009-10-30 05:47:26 +00001669 if (!generic_test_le_bit(rel_gfn, memslot->dirty_bitmap))
1670 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 }
1672}
1673
Eddie Dongb6958ce2007-07-18 12:15:21 +03001674/*
1675 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1676 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001677void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001678{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001679 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001680
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001681 for (;;) {
1682 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001683
Gleb Natapova1b37102009-07-09 15:33:52 +03001684 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001685 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001686 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001687 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001688 if (kvm_cpu_has_pending_timer(vcpu))
1689 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001690 if (signal_pending(current))
1691 break;
1692
Eddie Dongb6958ce2007-07-18 12:15:21 +03001693 vcpu_put(vcpu);
1694 schedule();
1695 vcpu_load(vcpu);
1696 }
1697
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001698 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001699}
1700
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701void kvm_resched(struct kvm_vcpu *vcpu)
1702{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001703 if (!need_resched())
1704 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706}
1707EXPORT_SYMBOL_GPL(kvm_resched);
1708
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001709static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001710{
1711 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001712 struct page *page;
1713
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001714 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001715 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001716#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001717 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001718 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001719#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001720#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1721 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1722 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1723#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001724 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001725 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001726 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001727 vmf->page = page;
1728 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001729}
1730
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001731static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001732 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001733};
1734
1735static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1736{
1737 vma->vm_ops = &kvm_vcpu_vm_ops;
1738 return 0;
1739}
1740
Avi Kivitybccf2152007-02-21 18:04:26 +02001741static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1742{
1743 struct kvm_vcpu *vcpu = filp->private_data;
1744
Al Viro66c0b392008-04-19 20:33:56 +01001745 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001746 return 0;
1747}
1748
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001749static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001750 .release = kvm_vcpu_release,
1751 .unlocked_ioctl = kvm_vcpu_ioctl,
1752 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001753 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001754};
1755
1756/*
1757 * Allocates an inode for the vcpu.
1758 */
1759static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1760{
Gleb Natapov73880c82009-06-09 15:56:28 +03001761 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Avi Kivitybccf2152007-02-21 18:04:26 +02001762}
1763
Avi Kivityc5ea7662007-02-20 18:41:05 +02001764/*
1765 * Creates some virtual cpus. Good luck creating more than one.
1766 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001767static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001768{
1769 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001770 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001771
Gleb Natapov73880c82009-06-09 15:56:28 +03001772 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001773 if (IS_ERR(vcpu))
1774 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001775
Avi Kivity15ad7142007-07-11 18:17:21 +03001776 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1777
Avi Kivity26e52152007-11-20 15:30:24 +02001778 r = kvm_arch_vcpu_setup(vcpu);
1779 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001780 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001781
Shaohua Li11ec2802007-07-23 14:51:37 +08001782 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001783 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1784 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001785 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001786 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001787
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001788 kvm_for_each_vcpu(r, v, kvm)
1789 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001790 r = -EEXIST;
1791 goto vcpu_destroy;
1792 }
1793
1794 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001795
1796 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001797 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001798 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001799 if (r < 0) {
1800 kvm_put_kvm(kvm);
1801 goto vcpu_destroy;
1802 }
1803
1804 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1805 smp_wmb();
1806 atomic_inc(&kvm->online_vcpus);
1807
1808#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1809 if (kvm->bsp_vcpu_id == id)
1810 kvm->bsp_vcpu = vcpu;
1811#endif
1812 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001813 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001814
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001815vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001816 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001817 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001818 return r;
1819}
1820
Avi Kivity1961d272007-03-05 19:46:05 +02001821static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1822{
1823 if (sigset) {
1824 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1825 vcpu->sigset_active = 1;
1826 vcpu->sigset = *sigset;
1827 } else
1828 vcpu->sigset_active = 0;
1829 return 0;
1830}
1831
Sheng Yangc1e01512009-02-25 17:22:26 +08001832#ifdef __KVM_HAVE_MSIX
1833static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1834 struct kvm_assigned_msix_nr *entry_nr)
1835{
1836 int r = 0;
1837 struct kvm_assigned_dev_kernel *adev;
1838
1839 mutex_lock(&kvm->lock);
1840
1841 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1842 entry_nr->assigned_dev_id);
1843 if (!adev) {
1844 r = -EINVAL;
1845 goto msix_nr_out;
1846 }
1847
1848 if (adev->entries_nr == 0) {
1849 adev->entries_nr = entry_nr->entry_nr;
1850 if (adev->entries_nr == 0 ||
1851 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1852 r = -EINVAL;
1853 goto msix_nr_out;
1854 }
1855
1856 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1857 entry_nr->entry_nr,
1858 GFP_KERNEL);
1859 if (!adev->host_msix_entries) {
1860 r = -ENOMEM;
1861 goto msix_nr_out;
1862 }
1863 adev->guest_msix_entries = kzalloc(
1864 sizeof(struct kvm_guest_msix_entry) *
1865 entry_nr->entry_nr, GFP_KERNEL);
1866 if (!adev->guest_msix_entries) {
1867 kfree(adev->host_msix_entries);
1868 r = -ENOMEM;
1869 goto msix_nr_out;
1870 }
1871 } else /* Not allowed set MSI-X number twice */
1872 r = -EINVAL;
1873msix_nr_out:
1874 mutex_unlock(&kvm->lock);
1875 return r;
1876}
1877
1878static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1879 struct kvm_assigned_msix_entry *entry)
1880{
1881 int r = 0, i;
1882 struct kvm_assigned_dev_kernel *adev;
1883
1884 mutex_lock(&kvm->lock);
1885
1886 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1887 entry->assigned_dev_id);
1888
1889 if (!adev) {
1890 r = -EINVAL;
1891 goto msix_entry_out;
1892 }
1893
1894 for (i = 0; i < adev->entries_nr; i++)
1895 if (adev->guest_msix_entries[i].vector == 0 ||
1896 adev->guest_msix_entries[i].entry == entry->entry) {
1897 adev->guest_msix_entries[i].entry = entry->entry;
1898 adev->guest_msix_entries[i].vector = entry->gsi;
1899 adev->host_msix_entries[i].entry = entry->entry;
1900 break;
1901 }
1902 if (i == adev->entries_nr) {
1903 r = -ENOSPC;
1904 goto msix_entry_out;
1905 }
1906
1907msix_entry_out:
1908 mutex_unlock(&kvm->lock);
1909
1910 return r;
1911}
1912#endif
1913
Avi Kivitybccf2152007-02-21 18:04:26 +02001914static long kvm_vcpu_ioctl(struct file *filp,
1915 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916{
Avi Kivitybccf2152007-02-21 18:04:26 +02001917 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001918 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001919 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001920 struct kvm_fpu *fpu = NULL;
1921 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001923 if (vcpu->kvm->mm != current->mm)
1924 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001926 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001927 r = -EINVAL;
1928 if (arg)
1929 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001930 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001933 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001935 r = -ENOMEM;
1936 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1937 if (!kvm_regs)
1938 goto out;
1939 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001941 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001943 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1944 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001946out_free1:
1947 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 break;
1949 }
1950 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001951 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001953 r = -ENOMEM;
1954 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1955 if (!kvm_regs)
1956 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001958 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1959 goto out_free2;
1960 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001962 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001964out_free2:
1965 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 break;
1967 }
1968 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001969 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1970 r = -ENOMEM;
1971 if (!kvm_sregs)
1972 goto out;
1973 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 if (r)
1975 goto out;
1976 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001977 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 goto out;
1979 r = 0;
1980 break;
1981 }
1982 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001983 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1984 r = -ENOMEM;
1985 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001987 r = -EFAULT;
1988 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1989 goto out;
1990 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991 if (r)
1992 goto out;
1993 r = 0;
1994 break;
1995 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001996 case KVM_GET_MP_STATE: {
1997 struct kvm_mp_state mp_state;
1998
1999 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
2000 if (r)
2001 goto out;
2002 r = -EFAULT;
2003 if (copy_to_user(argp, &mp_state, sizeof mp_state))
2004 goto out;
2005 r = 0;
2006 break;
2007 }
2008 case KVM_SET_MP_STATE: {
2009 struct kvm_mp_state mp_state;
2010
2011 r = -EFAULT;
2012 if (copy_from_user(&mp_state, argp, sizeof mp_state))
2013 goto out;
2014 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
2015 if (r)
2016 goto out;
2017 r = 0;
2018 break;
2019 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020 case KVM_TRANSLATE: {
2021 struct kvm_translation tr;
2022
2023 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002024 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08002026 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027 if (r)
2028 goto out;
2029 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002030 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031 goto out;
2032 r = 0;
2033 break;
2034 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002035 case KVM_SET_GUEST_DEBUG: {
2036 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037
2038 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002039 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002041 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002042 if (r)
2043 goto out;
2044 r = 0;
2045 break;
2046 }
Avi Kivity1961d272007-03-05 19:46:05 +02002047 case KVM_SET_SIGNAL_MASK: {
2048 struct kvm_signal_mask __user *sigmask_arg = argp;
2049 struct kvm_signal_mask kvm_sigmask;
2050 sigset_t sigset, *p;
2051
2052 p = NULL;
2053 if (argp) {
2054 r = -EFAULT;
2055 if (copy_from_user(&kvm_sigmask, argp,
2056 sizeof kvm_sigmask))
2057 goto out;
2058 r = -EINVAL;
2059 if (kvm_sigmask.len != sizeof sigset)
2060 goto out;
2061 r = -EFAULT;
2062 if (copy_from_user(&sigset, sigmask_arg->sigset,
2063 sizeof sigset))
2064 goto out;
2065 p = &sigset;
2066 }
2067 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2068 break;
2069 }
Avi Kivityb8836732007-04-01 16:34:31 +03002070 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002071 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2072 r = -ENOMEM;
2073 if (!fpu)
2074 goto out;
2075 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002076 if (r)
2077 goto out;
2078 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002079 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03002080 goto out;
2081 r = 0;
2082 break;
2083 }
2084 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002085 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2086 r = -ENOMEM;
2087 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002088 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002089 r = -EFAULT;
2090 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2091 goto out;
2092 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002093 if (r)
2094 goto out;
2095 r = 0;
2096 break;
2097 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002098 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002099 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002100 }
2101out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002102 kfree(fpu);
2103 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002104 return r;
2105}
2106
2107static long kvm_vm_ioctl(struct file *filp,
2108 unsigned int ioctl, unsigned long arg)
2109{
2110 struct kvm *kvm = filp->private_data;
2111 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002112 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002113
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002114 if (kvm->mm != current->mm)
2115 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002116 switch (ioctl) {
2117 case KVM_CREATE_VCPU:
2118 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2119 if (r < 0)
2120 goto out;
2121 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002122 case KVM_SET_USER_MEMORY_REGION: {
2123 struct kvm_userspace_memory_region kvm_userspace_mem;
2124
2125 r = -EFAULT;
2126 if (copy_from_user(&kvm_userspace_mem, argp,
2127 sizeof kvm_userspace_mem))
2128 goto out;
2129
2130 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 if (r)
2132 goto out;
2133 break;
2134 }
2135 case KVM_GET_DIRTY_LOG: {
2136 struct kvm_dirty_log log;
2137
2138 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002139 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002141 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142 if (r)
2143 goto out;
2144 break;
2145 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002146#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2147 case KVM_REGISTER_COALESCED_MMIO: {
2148 struct kvm_coalesced_mmio_zone zone;
2149 r = -EFAULT;
2150 if (copy_from_user(&zone, argp, sizeof zone))
2151 goto out;
2152 r = -ENXIO;
2153 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2154 if (r)
2155 goto out;
2156 r = 0;
2157 break;
2158 }
2159 case KVM_UNREGISTER_COALESCED_MMIO: {
2160 struct kvm_coalesced_mmio_zone zone;
2161 r = -EFAULT;
2162 if (copy_from_user(&zone, argp, sizeof zone))
2163 goto out;
2164 r = -ENXIO;
2165 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2166 if (r)
2167 goto out;
2168 r = 0;
2169 break;
2170 }
2171#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002172#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2173 case KVM_ASSIGN_PCI_DEVICE: {
2174 struct kvm_assigned_pci_dev assigned_dev;
2175
2176 r = -EFAULT;
2177 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2178 goto out;
2179 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2180 if (r)
2181 goto out;
2182 break;
2183 }
2184 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002185 r = -EOPNOTSUPP;
2186 break;
2187 }
2188#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2189 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002190 struct kvm_assigned_irq assigned_irq;
2191
2192 r = -EFAULT;
2193 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2194 goto out;
2195 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2196 if (r)
2197 goto out;
2198 break;
2199 }
Sheng Yange56d5322009-03-12 21:45:39 +08002200 case KVM_DEASSIGN_DEV_IRQ: {
2201 struct kvm_assigned_irq assigned_irq;
2202
2203 r = -EFAULT;
2204 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2205 goto out;
2206 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2207 if (r)
2208 goto out;
2209 break;
2210 }
2211#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002212#endif
Weidong Han0a920352008-12-02 21:24:23 +08002213#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2214 case KVM_DEASSIGN_PCI_DEVICE: {
2215 struct kvm_assigned_pci_dev assigned_dev;
2216
2217 r = -EFAULT;
2218 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2219 goto out;
2220 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2221 if (r)
2222 goto out;
2223 break;
2224 }
2225#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002226#ifdef KVM_CAP_IRQ_ROUTING
2227 case KVM_SET_GSI_ROUTING: {
2228 struct kvm_irq_routing routing;
2229 struct kvm_irq_routing __user *urouting;
2230 struct kvm_irq_routing_entry *entries;
2231
2232 r = -EFAULT;
2233 if (copy_from_user(&routing, argp, sizeof(routing)))
2234 goto out;
2235 r = -EINVAL;
2236 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2237 goto out;
2238 if (routing.flags)
2239 goto out;
2240 r = -ENOMEM;
2241 entries = vmalloc(routing.nr * sizeof(*entries));
2242 if (!entries)
2243 goto out;
2244 r = -EFAULT;
2245 urouting = argp;
2246 if (copy_from_user(entries, urouting->entries,
2247 routing.nr * sizeof(*entries)))
2248 goto out_free_irq_routing;
2249 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2250 routing.flags);
2251 out_free_irq_routing:
2252 vfree(entries);
2253 break;
2254 }
Avi Kivity6621fbc2009-08-10 17:00:52 +03002255#endif /* KVM_CAP_IRQ_ROUTING */
Sheng Yangc1e01512009-02-25 17:22:26 +08002256#ifdef __KVM_HAVE_MSIX
2257 case KVM_ASSIGN_SET_MSIX_NR: {
2258 struct kvm_assigned_msix_nr entry_nr;
2259 r = -EFAULT;
2260 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2261 goto out;
2262 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2263 if (r)
2264 goto out;
2265 break;
2266 }
2267 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2268 struct kvm_assigned_msix_entry entry;
2269 r = -EFAULT;
2270 if (copy_from_user(&entry, argp, sizeof entry))
2271 goto out;
2272 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2273 if (r)
2274 goto out;
2275 break;
2276 }
Avi Kivity399ec802008-11-19 13:58:46 +02002277#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04002278 case KVM_IRQFD: {
2279 struct kvm_irqfd data;
2280
2281 r = -EFAULT;
2282 if (copy_from_user(&data, argp, sizeof data))
2283 goto out;
2284 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2285 break;
2286 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002287 case KVM_IOEVENTFD: {
2288 struct kvm_ioeventfd data;
2289
2290 r = -EFAULT;
2291 if (copy_from_user(&data, argp, sizeof data))
2292 goto out;
2293 r = kvm_ioeventfd(kvm, &data);
2294 break;
2295 }
Gleb Natapov73880c82009-06-09 15:56:28 +03002296#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2297 case KVM_SET_BOOT_CPU_ID:
2298 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002299 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002300 if (atomic_read(&kvm->online_vcpus) != 0)
2301 r = -EBUSY;
2302 else
2303 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002304 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002305 break;
2306#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002307 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002308 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002309 }
2310out:
2311 return r;
2312}
2313
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002314static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002315{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002316 struct page *page[1];
2317 unsigned long addr;
2318 int npages;
2319 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002320 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002321
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002322 addr = gfn_to_hva(kvm, gfn);
2323 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002324 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002325
2326 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2327 NULL);
2328 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002329 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002330
2331 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002332 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002333}
2334
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002335static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002336 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002337};
2338
2339static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2340{
2341 vma->vm_ops = &kvm_vm_vm_ops;
2342 return 0;
2343}
2344
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002345static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002346 .release = kvm_vm_release,
2347 .unlocked_ioctl = kvm_vm_ioctl,
2348 .compat_ioctl = kvm_vm_ioctl,
2349 .mmap = kvm_vm_mmap,
2350};
2351
2352static int kvm_dev_ioctl_create_vm(void)
2353{
Al Viro2030a422008-02-23 06:46:49 -05002354 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002355 struct kvm *kvm;
2356
Avi Kivityf17abe92007-02-21 19:28:04 +02002357 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002358 if (IS_ERR(kvm))
2359 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002360 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002361 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002362 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002363
Avi Kivityf17abe92007-02-21 19:28:04 +02002364 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002365}
2366
Avi Kivity1a811b62008-12-08 18:25:27 +02002367static long kvm_dev_ioctl_check_extension_generic(long arg)
2368{
2369 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002370 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002371 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002372 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002373#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2374 case KVM_CAP_SET_BOOT_CPU_ID:
2375#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002376 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002377#ifdef CONFIG_HAVE_KVM_IRQCHIP
2378 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002379 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002380#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002381 default:
2382 break;
2383 }
2384 return kvm_dev_ioctl_check_extension(arg);
2385}
2386
Avi Kivityf17abe92007-02-21 19:28:04 +02002387static long kvm_dev_ioctl(struct file *filp,
2388 unsigned int ioctl, unsigned long arg)
2389{
Avi Kivity07c45a32007-03-07 13:05:38 +02002390 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002391
2392 switch (ioctl) {
2393 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002394 r = -EINVAL;
2395 if (arg)
2396 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002397 r = KVM_API_VERSION;
2398 break;
2399 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002400 r = -EINVAL;
2401 if (arg)
2402 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002403 r = kvm_dev_ioctl_create_vm();
2404 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002405 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002406 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002407 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002408 case KVM_GET_VCPU_MMAP_SIZE:
2409 r = -EINVAL;
2410 if (arg)
2411 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002412 r = PAGE_SIZE; /* struct kvm_run */
2413#ifdef CONFIG_X86
2414 r += PAGE_SIZE; /* pio data page */
2415#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002416#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2417 r += PAGE_SIZE; /* coalesced mmio ring page */
2418#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002419 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002420 case KVM_TRACE_ENABLE:
2421 case KVM_TRACE_PAUSE:
2422 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002423 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002424 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002426 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 }
2428out:
2429 return r;
2430}
2431
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 .unlocked_ioctl = kvm_dev_ioctl,
2434 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435};
2436
2437static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002438 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439 "kvm",
2440 &kvm_chardev_ops,
2441};
2442
Avi Kivity1b6c0162007-05-24 13:03:52 +03002443static void hardware_enable(void *junk)
2444{
2445 int cpu = raw_smp_processor_id();
2446
Rusty Russell7f59f492008-12-07 21:25:45 +10302447 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002448 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302449 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002450 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002451}
2452
2453static void hardware_disable(void *junk)
2454{
2455 int cpu = raw_smp_processor_id();
2456
Rusty Russell7f59f492008-12-07 21:25:45 +10302457 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002458 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302459 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002460 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002461}
2462
Avi Kivity774c47f2007-02-12 00:54:47 -08002463static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2464 void *v)
2465{
2466 int cpu = (long)v;
2467
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002468 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002469 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002470 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03002471 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2472 cpu);
2473 hardware_disable(NULL);
2474 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002475 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002476 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2477 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002478 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002479 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002480 case CPU_ONLINE:
2481 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2482 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002483 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002484 break;
2485 }
2486 return NOTIFY_OK;
2487}
2488
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002489
2490asmlinkage void kvm_handle_fault_on_reboot(void)
2491{
2492 if (kvm_rebooting)
2493 /* spin while reset goes on */
2494 while (true)
2495 ;
2496 /* Fault while not rebooting. We want the trace. */
2497 BUG();
2498}
2499EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2500
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002501static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002502 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002503{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002504 /*
2505 * Some (well, at least mine) BIOSes hang on reboot if
2506 * in vmx root mode.
2507 *
2508 * And Intel TXT required VMX off for all cpu when system shutdown.
2509 */
2510 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2511 kvm_rebooting = true;
2512 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002513 return NOTIFY_OK;
2514}
2515
2516static struct notifier_block kvm_reboot_notifier = {
2517 .notifier_call = kvm_reboot,
2518 .priority = 0,
2519};
2520
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002521void kvm_io_bus_init(struct kvm_io_bus *bus)
2522{
2523 memset(bus, 0, sizeof(*bus));
2524}
2525
2526void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2527{
2528 int i;
2529
2530 for (i = 0; i < bus->dev_count; i++) {
2531 struct kvm_io_device *pos = bus->devs[i];
2532
2533 kvm_iodevice_destructor(pos);
2534 }
2535}
2536
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002537/* kvm_io_bus_write - called under kvm->slots_lock */
2538int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
2539 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002540{
2541 int i;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002542 for (i = 0; i < bus->dev_count; i++)
2543 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2544 return 0;
2545 return -EOPNOTSUPP;
2546}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002547
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002548/* kvm_io_bus_read - called under kvm->slots_lock */
2549int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
2550{
2551 int i;
2552 for (i = 0; i < bus->dev_count; i++)
2553 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2554 return 0;
2555 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002556}
2557
Gregory Haskins090b7af2009-07-07 17:08:44 -04002558int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002559 struct kvm_io_device *dev)
2560{
Gregory Haskins090b7af2009-07-07 17:08:44 -04002561 int ret;
2562
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002563 down_write(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002564 ret = __kvm_io_bus_register_dev(bus, dev);
2565 up_write(&kvm->slots_lock);
2566
2567 return ret;
2568}
2569
2570/* An unlocked version. Caller must have write lock on slots_lock. */
2571int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
2572 struct kvm_io_device *dev)
2573{
2574 if (bus->dev_count > NR_IOBUS_DEVS-1)
2575 return -ENOSPC;
2576
2577 bus->devs[bus->dev_count++] = dev;
2578
2579 return 0;
2580}
2581
2582void kvm_io_bus_unregister_dev(struct kvm *kvm,
2583 struct kvm_io_bus *bus,
2584 struct kvm_io_device *dev)
2585{
2586 down_write(&kvm->slots_lock);
2587 __kvm_io_bus_unregister_dev(bus, dev);
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002588 up_write(&kvm->slots_lock);
2589}
2590
2591/* An unlocked version. Caller must have write lock on slots_lock. */
Gregory Haskins090b7af2009-07-07 17:08:44 -04002592void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
2593 struct kvm_io_device *dev)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002594{
Gregory Haskins090b7af2009-07-07 17:08:44 -04002595 int i;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002596
Gregory Haskins090b7af2009-07-07 17:08:44 -04002597 for (i = 0; i < bus->dev_count; i++)
2598 if (bus->devs[i] == dev) {
2599 bus->devs[i] = bus->devs[--bus->dev_count];
2600 break;
2601 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002602}
2603
Avi Kivity774c47f2007-02-12 00:54:47 -08002604static struct notifier_block kvm_cpu_notifier = {
2605 .notifier_call = kvm_cpu_hotplug,
2606 .priority = 20, /* must be > scheduler priority */
2607};
2608
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002609static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002610{
2611 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002612 struct kvm *kvm;
2613
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002614 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002615 spin_lock(&kvm_lock);
2616 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002617 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002618 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002619 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002620}
2621
2622DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2623
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002624static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002625{
2626 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002627 struct kvm *kvm;
2628 struct kvm_vcpu *vcpu;
2629 int i;
2630
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002631 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002632 spin_lock(&kvm_lock);
2633 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002634 kvm_for_each_vcpu(i, vcpu, kvm)
2635 *val += *(u32 *)((void *)vcpu + offset);
2636
Avi Kivity1165f5f2007-04-19 17:27:43 +03002637 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002638 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002639}
2640
Avi Kivityba1389b2007-11-18 16:24:12 +02002641DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2642
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002643static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002644 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2645 [KVM_STAT_VM] = &vm_stat_fops,
2646};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002647
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002648static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649{
2650 struct kvm_stats_debugfs_item *p;
2651
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002652 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002654 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002655 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002656 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657}
2658
2659static void kvm_exit_debug(void)
2660{
2661 struct kvm_stats_debugfs_item *p;
2662
2663 for (p = debugfs_entries; p->name; ++p)
2664 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002665 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002666}
2667
Avi Kivity59ae6c62007-02-12 00:54:48 -08002668static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2669{
Avi Kivity4267c412007-05-24 13:09:41 +03002670 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002671 return 0;
2672}
2673
2674static int kvm_resume(struct sys_device *dev)
2675{
Avi Kivity4267c412007-05-24 13:09:41 +03002676 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002677 return 0;
2678}
2679
2680static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002681 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002682 .suspend = kvm_suspend,
2683 .resume = kvm_resume,
2684};
2685
2686static struct sys_device kvm_sysdev = {
2687 .id = 0,
2688 .cls = &kvm_sysdev_class,
2689};
2690
Izik Eiduscea7bb22007-10-17 19:17:48 +02002691struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002692pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693
Avi Kivity15ad7142007-07-11 18:17:21 +03002694static inline
2695struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2696{
2697 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2698}
2699
2700static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2701{
2702 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2703
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002704 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002705}
2706
2707static void kvm_sched_out(struct preempt_notifier *pn,
2708 struct task_struct *next)
2709{
2710 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2711
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002712 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002713}
2714
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002715int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002716 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717{
2718 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002719 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002721 r = kvm_arch_init(opaque);
2722 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002723 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002724
2725 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2726
2727 if (bad_page == NULL) {
2728 r = -ENOMEM;
2729 goto out;
2730 }
2731
Anthony Liguori35149e22008-04-02 14:46:56 -05002732 bad_pfn = page_to_pfn(bad_page);
2733
Avi Kivity8437a612009-06-06 14:52:35 -07002734 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302735 r = -ENOMEM;
2736 goto out_free_0;
2737 }
2738
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002739 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302741 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742
Yang, Sheng002c7f72007-07-31 14:23:01 +03002743 for_each_online_cpu(cpu) {
2744 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002745 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002746 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002747 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002748 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002749 }
2750
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002751 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002752 r = register_cpu_notifier(&kvm_cpu_notifier);
2753 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002754 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 register_reboot_notifier(&kvm_reboot_notifier);
2756
Avi Kivity59ae6c62007-02-12 00:54:48 -08002757 r = sysdev_class_register(&kvm_sysdev_class);
2758 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002759 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002760
2761 r = sysdev_register(&kvm_sysdev);
2762 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002763 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002764
Rusty Russellc16f8622007-07-30 21:12:19 +10002765 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2766 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002767 __alignof__(struct kvm_vcpu),
2768 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002769 if (!kvm_vcpu_cache) {
2770 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002771 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002772 }
2773
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002775 kvm_vm_fops.owner = module;
2776 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777
2778 r = misc_register(&kvm_dev);
2779 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002780 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 goto out_free;
2782 }
2783
Avi Kivity15ad7142007-07-11 18:17:21 +03002784 kvm_preempt_ops.sched_in = kvm_sched_in;
2785 kvm_preempt_ops.sched_out = kvm_sched_out;
2786
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002787 kvm_init_debug();
2788
Avi Kivityc7addb92007-09-16 18:58:32 +02002789 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790
2791out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002792 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002793out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002794 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002795out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002796 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002797out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002799 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002800out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002801 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002802out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002803 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302804out_free_0a:
2805 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002806out_free_0:
2807 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002808out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002809 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002810out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 return r;
2812}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002813EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002815void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002817 tracepoint_synchronize_unregister();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002818 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002820 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002821 sysdev_unregister(&kvm_sysdev);
2822 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002824 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002825 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002826 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002827 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302828 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002829 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002831EXPORT_SYMBOL_GPL(kvm_exit);