blob: 3bee94892774e56c30f6bb60c9e5d74bcf0a6299 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Hollis Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/io.h>
49#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020050#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Laurent Vivier5f94c172008-05-30 16:05:54 +020052#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
53#include "coalesced_mmio.h"
54#endif
55
Xiantao Zhang8a98f662008-10-06 13:47:38 +080056#ifdef KVM_CAP_DEVICE_ASSIGNMENT
57#include <linux/pci.h>
58#include <linux/interrupt.h>
59#include "irq.h"
60#endif
61
Marcelo Tosatti229456f2009-06-17 09:22:14 -030062#define CREATE_TRACE_POINTS
63#include <trace/events/kvm.h>
64
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Marcelo Tosattifa40a822009-06-04 15:08:24 -030068/*
69 * Ordering of locks:
70 *
Michael S. Tsirkin22fc0292009-06-29 22:24:45 +030071 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030072 */
73
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080074DEFINE_SPINLOCK(kvm_lock);
75LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080076
Rusty Russell7f59f492008-12-07 21:25:45 +103077static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030078
Rusty Russellc16f8622007-07-30 21:12:19 +100079struct kmem_cache *kvm_vcpu_cache;
80EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030081
Avi Kivity15ad7142007-07-11 18:17:21 +030082static __read_mostly struct preempt_ops kvm_preempt_ops;
83
Hollis Blanchard76f7c872008-04-15 16:05:42 -050084struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivitybccf2152007-02-21 18:04:26 +020086static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
87 unsigned long arg);
88
Hannes Edere8ba5d32008-11-28 17:02:42 +010089static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030090
Marcelo Tosatti54dee992009-06-11 12:07:44 -030091static bool largepages_enabled = true;
92
Xiantao Zhang8a98f662008-10-06 13:47:38 +080093#ifdef KVM_CAP_DEVICE_ASSIGNMENT
94static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
95 int assigned_dev_id)
96{
97 struct list_head *ptr;
98 struct kvm_assigned_dev_kernel *match;
99
100 list_for_each(ptr, head) {
101 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
102 if (match->assigned_dev_id == assigned_dev_id)
103 return match;
104 }
105 return NULL;
106}
107
Sheng Yang2350bd12009-02-25 17:22:27 +0800108static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
109 *assigned_dev, int irq)
110{
111 int i, index;
112 struct msix_entry *host_msix_entries;
113
114 host_msix_entries = assigned_dev->host_msix_entries;
115
116 index = -1;
117 for (i = 0; i < assigned_dev->entries_nr; i++)
118 if (irq == host_msix_entries[i].vector) {
119 index = i;
120 break;
121 }
122 if (index < 0) {
123 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
124 return 0;
125 }
126
127 return index;
128}
129
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800130static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
131{
132 struct kvm_assigned_dev_kernel *assigned_dev;
Sheng Yang2350bd12009-02-25 17:22:27 +0800133 struct kvm *kvm;
Sheng Yang968a6342009-04-30 10:58:42 +0800134 int i;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800135
136 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
137 interrupt_work);
Sheng Yang2350bd12009-02-25 17:22:27 +0800138 kvm = assigned_dev->kvm;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800139
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300140 mutex_lock(&kvm->irq_lock);
Marcelo Tosatti547de292009-05-07 17:55:13 -0300141 spin_lock_irq(&assigned_dev->assigned_dev_lock);
Sheng Yange56d5322009-03-12 21:45:39 +0800142 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800143 struct kvm_guest_msix_entry *guest_entries =
144 assigned_dev->guest_msix_entries;
145 for (i = 0; i < assigned_dev->entries_nr; i++) {
146 if (!(guest_entries[i].flags &
147 KVM_ASSIGNED_MSIX_PENDING))
148 continue;
149 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
150 kvm_set_irq(assigned_dev->kvm,
151 assigned_dev->irq_source_id,
152 guest_entries[i].vector, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800153 }
Sheng Yang968a6342009-04-30 10:58:42 +0800154 } else
Sheng Yang2350bd12009-02-25 17:22:27 +0800155 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
156 assigned_dev->guest_irq, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800157
Marcelo Tosatti547de292009-05-07 17:55:13 -0300158 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300159 mutex_unlock(&assigned_dev->kvm->irq_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800160}
161
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800162static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
163{
Marcelo Tosatti547de292009-05-07 17:55:13 -0300164 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800165 struct kvm_assigned_dev_kernel *assigned_dev =
166 (struct kvm_assigned_dev_kernel *) dev_id;
167
Marcelo Tosatti547de292009-05-07 17:55:13 -0300168 spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
Sheng Yange56d5322009-03-12 21:45:39 +0800169 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800170 int index = find_index_from_host_irq(assigned_dev, irq);
171 if (index < 0)
Marcelo Tosatti547de292009-05-07 17:55:13 -0300172 goto out;
Sheng Yang2350bd12009-02-25 17:22:27 +0800173 assigned_dev->guest_msix_entries[index].flags |=
174 KVM_ASSIGNED_MSIX_PENDING;
175 }
176
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800177 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000178
Sheng Yang968a6342009-04-30 10:58:42 +0800179 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
180 disable_irq_nosync(irq);
181 assigned_dev->host_irq_disabled = true;
182 }
Mark McLoughlindefaf152008-12-02 12:16:33 +0000183
Marcelo Tosatti547de292009-05-07 17:55:13 -0300184out:
185 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800186 return IRQ_HANDLED;
187}
188
189/* Ack the irq line for an assigned device */
190static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
191{
192 struct kvm_assigned_dev_kernel *dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300193 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800194
195 if (kian->gsi == -1)
196 return;
197
198 dev = container_of(kian, struct kvm_assigned_dev_kernel,
199 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000200
Sheng Yang5550af42008-10-15 20:15:06 +0800201 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000202
203 /* The guest irq may be shared so this ack may be
204 * from another device.
205 */
Marcelo Tosatti547de292009-05-07 17:55:13 -0300206 spin_lock_irqsave(&dev->assigned_dev_lock, flags);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000207 if (dev->host_irq_disabled) {
208 enable_irq(dev->host_irq);
209 dev->host_irq_disabled = false;
210 }
Marcelo Tosatti547de292009-05-07 17:55:13 -0300211 spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800212}
213
Sheng Yange56d5322009-03-12 21:45:39 +0800214static void deassign_guest_irq(struct kvm *kvm,
215 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800216{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300217 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
Sheng Yange56d5322009-03-12 21:45:39 +0800218 assigned_dev->ack_notifier.gsi = -1;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000219
220 if (assigned_dev->irq_source_id != -1)
221 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
222 assigned_dev->irq_source_id = -1;
Sheng Yange56d5322009-03-12 21:45:39 +0800223 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
224}
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800225
Sheng Yange56d5322009-03-12 21:45:39 +0800226/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
227static void deassign_host_irq(struct kvm *kvm,
228 struct kvm_assigned_dev_kernel *assigned_dev)
229{
Sheng Yangba4cef32009-01-06 10:03:03 +0800230 /*
231 * In kvm_free_device_irq, cancel_work_sync return true if:
232 * 1. work is scheduled, and then cancelled.
233 * 2. work callback is executed.
234 *
235 * The first one ensured that the irq is disabled and no more events
236 * would happen. But for the second one, the irq may be enabled (e.g.
237 * for MSI). So we disable irq here to prevent further events.
238 *
239 * Notice this maybe result in nested disable if the interrupt type is
240 * INTx, but it's OK for we are going to free it.
241 *
242 * If this function is a part of VM destroy, please ensure that till
243 * now, the kvm state is still legal for probably we also have to wait
244 * interrupt_work done.
245 */
Sheng Yange56d5322009-03-12 21:45:39 +0800246 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yangd510d6c2009-02-25 17:22:28 +0800247 int i;
248 for (i = 0; i < assigned_dev->entries_nr; i++)
249 disable_irq_nosync(assigned_dev->
250 host_msix_entries[i].vector);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800251
Sheng Yangd510d6c2009-02-25 17:22:28 +0800252 cancel_work_sync(&assigned_dev->interrupt_work);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000253
Sheng Yangd510d6c2009-02-25 17:22:28 +0800254 for (i = 0; i < assigned_dev->entries_nr; i++)
255 free_irq(assigned_dev->host_msix_entries[i].vector,
256 (void *)assigned_dev);
257
258 assigned_dev->entries_nr = 0;
259 kfree(assigned_dev->host_msix_entries);
260 kfree(assigned_dev->guest_msix_entries);
261 pci_disable_msix(assigned_dev->dev);
262 } else {
263 /* Deal with MSI and INTx */
264 disable_irq_nosync(assigned_dev->host_irq);
265 cancel_work_sync(&assigned_dev->interrupt_work);
266
267 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
268
Sheng Yange56d5322009-03-12 21:45:39 +0800269 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800270 pci_disable_msi(assigned_dev->dev);
271 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000272
Sheng Yange56d5322009-03-12 21:45:39 +0800273 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000274}
275
Sheng Yange56d5322009-03-12 21:45:39 +0800276static int kvm_deassign_irq(struct kvm *kvm,
277 struct kvm_assigned_dev_kernel *assigned_dev,
278 unsigned long irq_requested_type)
279{
280 unsigned long guest_irq_type, host_irq_type;
281
282 if (!irqchip_in_kernel(kvm))
283 return -EINVAL;
284 /* no irq assignment to deassign */
285 if (!assigned_dev->irq_requested_type)
286 return -ENXIO;
287
288 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
289 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
290
291 if (host_irq_type)
292 deassign_host_irq(kvm, assigned_dev);
293 if (guest_irq_type)
294 deassign_guest_irq(kvm, assigned_dev);
295
296 return 0;
297}
298
299static void kvm_free_assigned_irq(struct kvm *kvm,
300 struct kvm_assigned_dev_kernel *assigned_dev)
301{
302 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
303}
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000304
305static void kvm_free_assigned_device(struct kvm *kvm,
306 struct kvm_assigned_dev_kernel
307 *assigned_dev)
308{
309 kvm_free_assigned_irq(kvm, assigned_dev);
310
Sheng Yang6eb55812008-10-31 12:37:41 +0800311 pci_reset_function(assigned_dev->dev);
312
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800313 pci_release_regions(assigned_dev->dev);
314 pci_disable_device(assigned_dev->dev);
315 pci_dev_put(assigned_dev->dev);
316
317 list_del(&assigned_dev->list);
318 kfree(assigned_dev);
319}
320
321void kvm_free_all_assigned_devices(struct kvm *kvm)
322{
323 struct list_head *ptr, *ptr2;
324 struct kvm_assigned_dev_kernel *assigned_dev;
325
326 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
327 assigned_dev = list_entry(ptr,
328 struct kvm_assigned_dev_kernel,
329 list);
330
331 kvm_free_assigned_device(kvm, assigned_dev);
332 }
333}
334
Sheng Yange56d5322009-03-12 21:45:39 +0800335static int assigned_device_enable_host_intx(struct kvm *kvm,
336 struct kvm_assigned_dev_kernel *dev)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800337{
Sheng Yange56d5322009-03-12 21:45:39 +0800338 dev->host_irq = dev->dev->irq;
339 /* Even though this is PCI, we don't want to use shared
340 * interrupts. Sharing host devices with guest-assigned devices
341 * on the same interrupt line is not a happy situation: there
342 * are going to be long delays in accepting, acking, etc.
343 */
344 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
345 0, "kvm_assigned_intx_device", (void *)dev))
346 return -EIO;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800347 return 0;
348}
349
Sheng Yange56d5322009-03-12 21:45:39 +0800350#ifdef __KVM_HAVE_MSI
351static int assigned_device_enable_host_msi(struct kvm *kvm,
352 struct kvm_assigned_dev_kernel *dev)
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800353{
354 int r;
355
Sheng Yange56d5322009-03-12 21:45:39 +0800356 if (!dev->dev->msi_enabled) {
357 r = pci_enable_msi(dev->dev);
358 if (r)
359 return r;
Sheng Yang5319c662008-11-24 14:32:57 +0800360 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800361
Sheng Yange56d5322009-03-12 21:45:39 +0800362 dev->host_irq = dev->dev->irq;
363 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
364 "kvm_assigned_msi_device", (void *)dev)) {
365 pci_disable_msi(dev->dev);
366 return -EIO;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800367 }
368
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800369 return 0;
370}
371#endif
372
Sheng Yangd510d6c2009-02-25 17:22:28 +0800373#ifdef __KVM_HAVE_MSIX
Sheng Yange56d5322009-03-12 21:45:39 +0800374static int assigned_device_enable_host_msix(struct kvm *kvm,
375 struct kvm_assigned_dev_kernel *dev)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800376{
Sheng Yange56d5322009-03-12 21:45:39 +0800377 int i, r = -EINVAL;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800378
Sheng Yange56d5322009-03-12 21:45:39 +0800379 /* host_msix_entries and guest_msix_entries should have been
380 * initialized */
381 if (dev->entries_nr == 0)
382 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800383
Sheng Yange56d5322009-03-12 21:45:39 +0800384 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
385 if (r)
386 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800387
Sheng Yange56d5322009-03-12 21:45:39 +0800388 for (i = 0; i < dev->entries_nr; i++) {
389 r = request_irq(dev->host_msix_entries[i].vector,
390 kvm_assigned_dev_intr, 0,
391 "kvm_assigned_msix_device",
392 (void *)dev);
393 /* FIXME: free requested_irq's on failure */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800394 if (r)
395 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800396 }
397
Sheng Yange56d5322009-03-12 21:45:39 +0800398 return 0;
399}
Sheng Yangd510d6c2009-02-25 17:22:28 +0800400
Sheng Yange56d5322009-03-12 21:45:39 +0800401#endif
402
403static int assigned_device_enable_guest_intx(struct kvm *kvm,
404 struct kvm_assigned_dev_kernel *dev,
405 struct kvm_assigned_irq *irq)
406{
407 dev->guest_irq = irq->guest_irq;
408 dev->ack_notifier.gsi = irq->guest_irq;
409 return 0;
410}
411
412#ifdef __KVM_HAVE_MSI
413static int assigned_device_enable_guest_msi(struct kvm *kvm,
414 struct kvm_assigned_dev_kernel *dev,
415 struct kvm_assigned_irq *irq)
416{
417 dev->guest_irq = irq->guest_irq;
418 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800419 dev->host_irq_disabled = false;
Sheng Yange56d5322009-03-12 21:45:39 +0800420 return 0;
421}
422#endif
423#ifdef __KVM_HAVE_MSIX
424static int assigned_device_enable_guest_msix(struct kvm *kvm,
425 struct kvm_assigned_dev_kernel *dev,
426 struct kvm_assigned_irq *irq)
427{
428 dev->guest_irq = irq->guest_irq;
429 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800430 dev->host_irq_disabled = false;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800431 return 0;
432}
433#endif
434
Sheng Yange56d5322009-03-12 21:45:39 +0800435static int assign_host_irq(struct kvm *kvm,
436 struct kvm_assigned_dev_kernel *dev,
437 __u32 host_irq_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800438{
Sheng Yange56d5322009-03-12 21:45:39 +0800439 int r = -EEXIST;
440
441 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
442 return r;
443
444 switch (host_irq_type) {
445 case KVM_DEV_IRQ_HOST_INTX:
446 r = assigned_device_enable_host_intx(kvm, dev);
447 break;
448#ifdef __KVM_HAVE_MSI
449 case KVM_DEV_IRQ_HOST_MSI:
450 r = assigned_device_enable_host_msi(kvm, dev);
451 break;
452#endif
453#ifdef __KVM_HAVE_MSIX
454 case KVM_DEV_IRQ_HOST_MSIX:
455 r = assigned_device_enable_host_msix(kvm, dev);
456 break;
457#endif
458 default:
459 r = -EINVAL;
460 }
461
462 if (!r)
463 dev->irq_requested_type |= host_irq_type;
464
465 return r;
466}
467
468static int assign_guest_irq(struct kvm *kvm,
469 struct kvm_assigned_dev_kernel *dev,
470 struct kvm_assigned_irq *irq,
471 unsigned long guest_irq_type)
472{
473 int id;
474 int r = -EEXIST;
475
476 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
477 return r;
478
479 id = kvm_request_irq_source_id(kvm);
480 if (id < 0)
481 return id;
482
483 dev->irq_source_id = id;
484
485 switch (guest_irq_type) {
486 case KVM_DEV_IRQ_GUEST_INTX:
487 r = assigned_device_enable_guest_intx(kvm, dev, irq);
488 break;
489#ifdef __KVM_HAVE_MSI
490 case KVM_DEV_IRQ_GUEST_MSI:
491 r = assigned_device_enable_guest_msi(kvm, dev, irq);
492 break;
493#endif
494#ifdef __KVM_HAVE_MSIX
495 case KVM_DEV_IRQ_GUEST_MSIX:
496 r = assigned_device_enable_guest_msix(kvm, dev, irq);
497 break;
498#endif
499 default:
500 r = -EINVAL;
501 }
502
503 if (!r) {
504 dev->irq_requested_type |= guest_irq_type;
505 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
506 } else
507 kvm_free_irq_source_id(kvm, dev->irq_source_id);
508
509 return r;
510}
511
512/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
513static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
514 struct kvm_assigned_irq *assigned_irq)
515{
516 int r = -EINVAL;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800517 struct kvm_assigned_dev_kernel *match;
Sheng Yange56d5322009-03-12 21:45:39 +0800518 unsigned long host_irq_type, guest_irq_type;
519
520 if (!capable(CAP_SYS_RAWIO))
521 return -EPERM;
522
523 if (!irqchip_in_kernel(kvm))
524 return r;
525
526 mutex_lock(&kvm->lock);
527 r = -ENODEV;
528 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
529 assigned_irq->assigned_dev_id);
530 if (!match)
531 goto out;
532
533 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
534 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
535
536 r = -EINVAL;
537 /* can only assign one type at a time */
538 if (hweight_long(host_irq_type) > 1)
539 goto out;
540 if (hweight_long(guest_irq_type) > 1)
541 goto out;
542 if (host_irq_type == 0 && guest_irq_type == 0)
543 goto out;
544
545 r = 0;
546 if (host_irq_type)
547 r = assign_host_irq(kvm, match, host_irq_type);
548 if (r)
549 goto out;
550
551 if (guest_irq_type)
552 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
553out:
554 mutex_unlock(&kvm->lock);
555 return r;
556}
557
558static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
559 struct kvm_assigned_irq
560 *assigned_irq)
561{
562 int r = -ENODEV;
563 struct kvm_assigned_dev_kernel *match;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800564
565 mutex_lock(&kvm->lock);
566
567 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
568 assigned_irq->assigned_dev_id);
Sheng Yange56d5322009-03-12 21:45:39 +0800569 if (!match)
570 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800571
Sheng Yange56d5322009-03-12 21:45:39 +0800572 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
573out:
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800574 mutex_unlock(&kvm->lock);
575 return r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800576}
577
578static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
579 struct kvm_assigned_pci_dev *assigned_dev)
580{
581 int r = 0;
582 struct kvm_assigned_dev_kernel *match;
583 struct pci_dev *dev;
584
Mark McLoughlin682edb42009-02-05 18:23:46 +0000585 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800586 mutex_lock(&kvm->lock);
587
588 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
589 assigned_dev->assigned_dev_id);
590 if (match) {
591 /* device already assigned */
Sheng Yange56d5322009-03-12 21:45:39 +0800592 r = -EEXIST;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800593 goto out;
594 }
595
596 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
597 if (match == NULL) {
598 printk(KERN_INFO "%s: Couldn't allocate memory\n",
599 __func__);
600 r = -ENOMEM;
601 goto out;
602 }
603 dev = pci_get_bus_and_slot(assigned_dev->busnr,
604 assigned_dev->devfn);
605 if (!dev) {
606 printk(KERN_INFO "%s: host device not found\n", __func__);
607 r = -EINVAL;
608 goto out_free;
609 }
610 if (pci_enable_device(dev)) {
611 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
612 r = -EBUSY;
613 goto out_put;
614 }
615 r = pci_request_regions(dev, "kvm_assigned_device");
616 if (r) {
617 printk(KERN_INFO "%s: Could not get access to device regions\n",
618 __func__);
619 goto out_disable;
620 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800621
622 pci_reset_function(dev);
623
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800624 match->assigned_dev_id = assigned_dev->assigned_dev_id;
625 match->host_busnr = assigned_dev->busnr;
626 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800627 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800628 match->dev = dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300629 spin_lock_init(&match->assigned_dev_lock);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000630 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800631 match->kvm = kvm;
Sheng Yange56d5322009-03-12 21:45:39 +0800632 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
633 INIT_WORK(&match->interrupt_work,
634 kvm_assigned_dev_interrupt_work_handler);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800635
636 list_add(&match->list, &kvm->arch.assigned_dev_head);
637
638 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100639 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800640 r = kvm_iommu_map_guest(kvm);
641 if (r)
642 goto out_list_del;
643 }
644 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800645 if (r)
646 goto out_list_del;
647 }
648
649out:
650 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000651 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800652 return r;
653out_list_del:
654 list_del(&match->list);
655 pci_release_regions(dev);
656out_disable:
657 pci_disable_device(dev);
658out_put:
659 pci_dev_put(dev);
660out_free:
661 kfree(match);
662 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000663 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800664 return r;
665}
666#endif
667
Weidong Han0a920352008-12-02 21:24:23 +0800668#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
669static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
670 struct kvm_assigned_pci_dev *assigned_dev)
671{
672 int r = 0;
673 struct kvm_assigned_dev_kernel *match;
674
675 mutex_lock(&kvm->lock);
676
677 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
678 assigned_dev->assigned_dev_id);
679 if (!match) {
680 printk(KERN_INFO "%s: device hasn't been assigned before, "
681 "so cannot be deassigned\n", __func__);
682 r = -EINVAL;
683 goto out;
684 }
685
Weidong Han4a906e42009-02-13 17:27:51 +0800686 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800687 kvm_deassign_device(kvm, match);
688
689 kvm_free_assigned_device(kvm, match);
690
691out:
692 mutex_unlock(&kvm->lock);
693 return r;
694}
695#endif
696
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800697inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300698{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100699 if (pfn_valid(pfn)) {
700 struct page *page = compound_head(pfn_to_page(pfn));
701 return PageReserved(page);
702 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300703
704 return true;
705}
706
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707/*
708 * Switches to specified vcpu, until a matching vcpu_put()
709 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200710void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711{
Avi Kivity15ad7142007-07-11 18:17:21 +0300712 int cpu;
713
Avi Kivitybccf2152007-02-21 18:04:26 +0200714 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300715 cpu = get_cpu();
716 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200717 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300718 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200719}
720
Carsten Otte313a3dc2007-10-11 19:16:52 +0200721void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722{
Avi Kivity15ad7142007-07-11 18:17:21 +0300723 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200724 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300725 preempt_notifier_unregister(&vcpu->preempt_notifier);
726 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727 mutex_unlock(&vcpu->mutex);
728}
729
Avi Kivityd9e368d2007-06-07 19:18:30 +0300730static void ack_flush(void *_completed)
731{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300732}
733
Rusty Russell49846892008-12-08 20:26:24 +1030734static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300735{
Avi Kivity597a5f52008-07-20 14:24:22 +0300736 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030737 cpumask_var_t cpus;
738 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300739 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300740
Li Zefan79f55992009-06-15 14:58:26 +0800741 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030742
Marcelo Tosatti84261922009-06-17 10:53:47 -0300743 spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200744 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300745 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030746 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300747 continue;
748 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030749 if (cpus != NULL && cpu != -1 && cpu != me)
750 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300751 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030752 if (unlikely(cpus == NULL))
753 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
754 else if (!cpumask_empty(cpus))
755 smp_call_function_many(cpus, ack_flush, NULL, 1);
756 else
757 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300758 spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030759 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030760 return called;
761}
762
763void kvm_flush_remote_tlbs(struct kvm *kvm)
764{
765 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
766 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300767}
768
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500769void kvm_reload_remote_mmus(struct kvm *kvm)
770{
Rusty Russell49846892008-12-08 20:26:24 +1030771 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500772}
773
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000774int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
775{
776 struct page *page;
777 int r;
778
779 mutex_init(&vcpu->mutex);
780 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000781 vcpu->kvm = kvm;
782 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300783 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000784
785 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
786 if (!page) {
787 r = -ENOMEM;
788 goto fail;
789 }
790 vcpu->run = page_address(page);
791
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800792 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000793 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800794 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000795 return 0;
796
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000797fail_free_run:
798 free_page((unsigned long)vcpu->run);
799fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000800 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000801}
802EXPORT_SYMBOL_GPL(kvm_vcpu_init);
803
804void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
805{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800806 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000807 free_page((unsigned long)vcpu->run);
808}
809EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
810
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200811#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
812static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
813{
814 return container_of(mn, struct kvm, mmu_notifier);
815}
816
817static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
818 struct mm_struct *mm,
819 unsigned long address)
820{
821 struct kvm *kvm = mmu_notifier_to_kvm(mn);
822 int need_tlb_flush;
823
824 /*
825 * When ->invalidate_page runs, the linux pte has been zapped
826 * already but the page is still allocated until
827 * ->invalidate_page returns. So if we increase the sequence
828 * here the kvm page fault will notice if the spte can't be
829 * established because the page is going to be freed. If
830 * instead the kvm page fault establishes the spte before
831 * ->invalidate_page runs, kvm_unmap_hva will release it
832 * before returning.
833 *
834 * The sequence increase only need to be seen at spin_unlock
835 * time, and not at spin_lock time.
836 *
837 * Increasing the sequence after the spin_unlock would be
838 * unsafe because the kvm page fault could then establish the
839 * pte after kvm_unmap_hva returned, without noticing the page
840 * is going to be freed.
841 */
842 spin_lock(&kvm->mmu_lock);
843 kvm->mmu_notifier_seq++;
844 need_tlb_flush = kvm_unmap_hva(kvm, address);
845 spin_unlock(&kvm->mmu_lock);
846
847 /* we've to flush the tlb before the pages can be freed */
848 if (need_tlb_flush)
849 kvm_flush_remote_tlbs(kvm);
850
851}
852
Izik Eidus3da0dd42009-09-23 21:47:18 +0300853static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
854 struct mm_struct *mm,
855 unsigned long address,
856 pte_t pte)
857{
858 struct kvm *kvm = mmu_notifier_to_kvm(mn);
859
860 spin_lock(&kvm->mmu_lock);
861 kvm->mmu_notifier_seq++;
862 kvm_set_spte_hva(kvm, address, pte);
863 spin_unlock(&kvm->mmu_lock);
864}
865
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200866static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
867 struct mm_struct *mm,
868 unsigned long start,
869 unsigned long end)
870{
871 struct kvm *kvm = mmu_notifier_to_kvm(mn);
872 int need_tlb_flush = 0;
873
874 spin_lock(&kvm->mmu_lock);
875 /*
876 * The count increase must become visible at unlock time as no
877 * spte can be established without taking the mmu_lock and
878 * count is also read inside the mmu_lock critical section.
879 */
880 kvm->mmu_notifier_count++;
881 for (; start < end; start += PAGE_SIZE)
882 need_tlb_flush |= kvm_unmap_hva(kvm, start);
883 spin_unlock(&kvm->mmu_lock);
884
885 /* we've to flush the tlb before the pages can be freed */
886 if (need_tlb_flush)
887 kvm_flush_remote_tlbs(kvm);
888}
889
890static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
891 struct mm_struct *mm,
892 unsigned long start,
893 unsigned long end)
894{
895 struct kvm *kvm = mmu_notifier_to_kvm(mn);
896
897 spin_lock(&kvm->mmu_lock);
898 /*
899 * This sequence increase will notify the kvm page fault that
900 * the page that is going to be mapped in the spte could have
901 * been freed.
902 */
903 kvm->mmu_notifier_seq++;
904 /*
905 * The above sequence increase must be visible before the
906 * below count decrease but both values are read by the kvm
907 * page fault under mmu_lock spinlock so we don't need to add
908 * a smb_wmb() here in between the two.
909 */
910 kvm->mmu_notifier_count--;
911 spin_unlock(&kvm->mmu_lock);
912
913 BUG_ON(kvm->mmu_notifier_count < 0);
914}
915
916static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
917 struct mm_struct *mm,
918 unsigned long address)
919{
920 struct kvm *kvm = mmu_notifier_to_kvm(mn);
921 int young;
922
923 spin_lock(&kvm->mmu_lock);
924 young = kvm_age_hva(kvm, address);
925 spin_unlock(&kvm->mmu_lock);
926
927 if (young)
928 kvm_flush_remote_tlbs(kvm);
929
930 return young;
931}
932
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100933static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
934 struct mm_struct *mm)
935{
936 struct kvm *kvm = mmu_notifier_to_kvm(mn);
937 kvm_arch_flush_shadow(kvm);
938}
939
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200940static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
941 .invalidate_page = kvm_mmu_notifier_invalidate_page,
942 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
943 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
944 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300945 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100946 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200947};
948#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
949
Avi Kivityf17abe92007-02-21 19:28:04 +0200950static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800952 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200953#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
954 struct page *page;
955#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800957 if (IS_ERR(kvm))
958 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200959#ifdef CONFIG_HAVE_KVM_IRQCHIP
960 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
961#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962
Laurent Vivier5f94c172008-05-30 16:05:54 +0200963#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
964 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
965 if (!page) {
966 kfree(kvm);
967 return ERR_PTR(-ENOMEM);
968 }
969 kvm->coalesced_mmio_ring =
970 (struct kvm_coalesced_mmio_ring *)page_address(page);
971#endif
972
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200973#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
974 {
975 int err;
976 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
977 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
978 if (err) {
979#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
980 put_page(page);
981#endif
982 kfree(kvm);
983 return ERR_PTR(err);
984 }
985 }
986#endif
987
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200988 kvm->mm = current->mm;
989 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500990 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300991 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300992 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400993 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800994 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300995 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400996 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200997 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300998 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000999 spin_lock(&kvm_lock);
1000 list_add(&kvm->vm_list, &vm_list);
1001 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001002#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1003 kvm_coalesced_mmio_init(kvm);
1004#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001005out:
Avi Kivityf17abe92007-02-21 19:28:04 +02001006 return kvm;
1007}
1008
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009/*
1010 * Free any memory in @free but not in @dont.
1011 */
1012static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1013 struct kvm_memory_slot *dont)
1014{
Joerg Roedelec04b262009-06-19 15:16:23 +02001015 int i;
1016
Izik Eidus290fc382007-09-27 14:11:22 +02001017 if (!dont || free->rmap != dont->rmap)
1018 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019
1020 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1021 vfree(free->dirty_bitmap);
1022
Joerg Roedelec04b262009-06-19 15:16:23 +02001023
1024 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1025 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
1026 vfree(free->lpage_info[i]);
1027 free->lpage_info[i] = NULL;
1028 }
1029 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001030
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001032 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001033 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034}
1035
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001036void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037{
1038 int i;
1039
1040 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001041 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042}
1043
Avi Kivityf17abe92007-02-21 19:28:04 +02001044static void kvm_destroy_vm(struct kvm *kvm)
1045{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001046 struct mm_struct *mm = kvm->mm;
1047
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001048 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001049 spin_lock(&kvm_lock);
1050 list_del(&kvm->vm_list);
1051 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001052 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001053 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001054 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001055#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1056 if (kvm->coalesced_mmio_ring != NULL)
1057 free_page((unsigned long)kvm->coalesced_mmio_ring);
1058#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001059#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1060 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +02001061#else
1062 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001063#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001064 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001065 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001066}
1067
Izik Eidusd39f13b2008-03-30 16:01:25 +03001068void kvm_get_kvm(struct kvm *kvm)
1069{
1070 atomic_inc(&kvm->users_count);
1071}
1072EXPORT_SYMBOL_GPL(kvm_get_kvm);
1073
1074void kvm_put_kvm(struct kvm *kvm)
1075{
1076 if (atomic_dec_and_test(&kvm->users_count))
1077 kvm_destroy_vm(kvm);
1078}
1079EXPORT_SYMBOL_GPL(kvm_put_kvm);
1080
1081
Avi Kivityf17abe92007-02-21 19:28:04 +02001082static int kvm_vm_release(struct inode *inode, struct file *filp)
1083{
1084 struct kvm *kvm = filp->private_data;
1085
Gregory Haskins721eecb2009-05-20 10:30:49 -04001086 kvm_irqfd_release(kvm);
1087
Izik Eidusd39f13b2008-03-30 16:01:25 +03001088 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 return 0;
1090}
1091
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093 * Allocate some memory and give it an address in the guest physical address
1094 * space.
1095 *
1096 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001097 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001098 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001100int __kvm_set_memory_region(struct kvm *kvm,
1101 struct kvm_userspace_memory_region *mem,
1102 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103{
1104 int r;
1105 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +02001106 unsigned long npages;
1107 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 struct kvm_memory_slot *memslot;
1109 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110
1111 r = -EINVAL;
1112 /* General sanity checks */
1113 if (mem->memory_size & (PAGE_SIZE - 1))
1114 goto out;
1115 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1116 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001117 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001118 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001119 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 goto out;
1121 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1122 goto out;
1123
1124 memslot = &kvm->memslots[mem->slot];
1125 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1126 npages = mem->memory_size >> PAGE_SHIFT;
1127
1128 if (!npages)
1129 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1130
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 new = old = *memslot;
1132
1133 new.base_gfn = base_gfn;
1134 new.npages = npages;
1135 new.flags = mem->flags;
1136
1137 /* Disallow changing a memory slot's size. */
1138 r = -EINVAL;
1139 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001140 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
1142 /* Check for overlaps */
1143 r = -EEXIST;
1144 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1145 struct kvm_memory_slot *s = &kvm->memslots[i];
1146
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001147 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 continue;
1149 if (!((base_gfn + npages <= s->base_gfn) ||
1150 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001151 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 /* Free page dirty bitmap if unneeded */
1155 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001156 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157
1158 r = -ENOMEM;
1159
1160 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001161#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001162 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001163 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001164
1165 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001166 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001167
Izik Eidus290fc382007-09-27 14:11:22 +02001168 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001169
Izik Eidus80b14b52007-10-25 11:54:04 +02001170 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001171 /*
1172 * hva_to_rmmap() serialzies with the mmu_lock and to be
1173 * safe it has to ignore memslots with !user_alloc &&
1174 * !userspace_addr.
1175 */
1176 if (user_alloc)
1177 new.userspace_addr = mem->userspace_addr;
1178 else
1179 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 }
Joerg Roedelec04b262009-06-19 15:16:23 +02001181 if (!npages)
1182 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001183
Joerg Roedelec04b262009-06-19 15:16:23 +02001184 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +02001185 unsigned long ugfn;
1186 unsigned long j;
1187 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +02001188 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001189
Joerg Roedelec04b262009-06-19 15:16:23 +02001190 /* Avoid unused variable warning if no large pages */
1191 (void)level;
1192
1193 if (new.lpage_info[i])
1194 continue;
1195
1196 lpages = 1 + (base_gfn + npages - 1) /
1197 KVM_PAGES_PER_HPAGE(level);
1198 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
1199
1200 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
1201
1202 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001203 goto out_free;
1204
Joerg Roedelec04b262009-06-19 15:16:23 +02001205 memset(new.lpage_info[i], 0,
1206 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001207
Joerg Roedelec04b262009-06-19 15:16:23 +02001208 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
1209 new.lpage_info[i][0].write_count = 1;
1210 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
1211 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +03001212 ugfn = new.userspace_addr >> PAGE_SHIFT;
1213 /*
1214 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001215 * other, or if explicitly asked to, disable large page
1216 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +03001217 */
Joerg Roedelec04b262009-06-19 15:16:23 +02001218 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001219 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +02001220 for (j = 0; j < lpages; ++j)
1221 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001222 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223
Joerg Roedelec04b262009-06-19 15:16:23 +02001224skip_lpage:
1225
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 /* Allocate page dirty bitmap if needed */
1227 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1228 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1229
1230 new.dirty_bitmap = vmalloc(dirty_bytes);
1231 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001232 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse244584f2009-06-10 19:23:24 +03001234 if (old.npages)
1235 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +02001237#else /* not defined CONFIG_S390 */
1238 new.user_alloc = user_alloc;
1239 if (user_alloc)
1240 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +02001241#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001242
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001243 if (!npages)
1244 kvm_arch_flush_shadow(kvm);
1245
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001246 spin_lock(&kvm->mmu_lock);
1247 if (mem->slot >= kvm->nmemslots)
1248 kvm->nmemslots = mem->slot + 1;
1249
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001251 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252
Zhang Xiantao0de10342007-11-20 16:25:04 +08001253 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1254 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001255 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001256 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001257 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001258 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001259 }
1260
Glauber Costa6f897242008-12-03 13:40:51 -02001261 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1262 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001263 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -02001264 if (!npages)
1265 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001266 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001267#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001268 /* map the pages in iommu page table */
1269 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1270 if (r)
1271 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001272#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273 return 0;
1274
Sheng Yangf78e0e22007-10-29 09:40:42 +08001275out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 kvm_free_physmem_slot(&new, &old);
1277out:
1278 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001279
1280}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001281EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1282
1283int kvm_set_memory_region(struct kvm *kvm,
1284 struct kvm_userspace_memory_region *mem,
1285 int user_alloc)
1286{
1287 int r;
1288
Izik Eidus72dc67a2008-02-10 18:04:15 +02001289 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001290 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001291 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001292 return r;
1293}
Izik Eidus210c7c42007-10-24 23:52:57 +02001294EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1295
Carsten Otte1fe779f2007-10-29 16:08:35 +01001296int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1297 struct
1298 kvm_userspace_memory_region *mem,
1299 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001300{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001301 if (mem->slot >= KVM_MEMORY_SLOTS)
1302 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001303 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304}
1305
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001306int kvm_get_dirty_log(struct kvm *kvm,
1307 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308{
1309 struct kvm_memory_slot *memslot;
1310 int r, i;
1311 int n;
1312 unsigned long any = 0;
1313
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 r = -EINVAL;
1315 if (log->slot >= KVM_MEMORY_SLOTS)
1316 goto out;
1317
1318 memslot = &kvm->memslots[log->slot];
1319 r = -ENOENT;
1320 if (!memslot->dirty_bitmap)
1321 goto out;
1322
Uri Lublincd1a4a92007-02-22 16:43:09 +02001323 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324
Uri Lublincd1a4a92007-02-22 16:43:09 +02001325 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326 any = memslot->dirty_bitmap[i];
1327
1328 r = -EFAULT;
1329 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1330 goto out;
1331
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001332 if (any)
1333 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334
1335 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 return r;
1338}
1339
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001340void kvm_disable_largepages(void)
1341{
1342 largepages_enabled = false;
1343}
1344EXPORT_SYMBOL_GPL(kvm_disable_largepages);
1345
Izik Eiduscea7bb22007-10-17 19:17:48 +02001346int is_error_page(struct page *page)
1347{
1348 return page == bad_page;
1349}
1350EXPORT_SYMBOL_GPL(is_error_page);
1351
Anthony Liguori35149e22008-04-02 14:46:56 -05001352int is_error_pfn(pfn_t pfn)
1353{
1354 return pfn == bad_pfn;
1355}
1356EXPORT_SYMBOL_GPL(is_error_pfn);
1357
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001358static inline unsigned long bad_hva(void)
1359{
1360 return PAGE_OFFSET;
1361}
1362
1363int kvm_is_error_hva(unsigned long addr)
1364{
1365 return addr == bad_hva();
1366}
1367EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1368
Izik Eidus28430992008-10-03 17:40:32 +03001369struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370{
1371 int i;
1372
1373 for (i = 0; i < kvm->nmemslots; ++i) {
1374 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1375
1376 if (gfn >= memslot->base_gfn
1377 && gfn < memslot->base_gfn + memslot->npages)
1378 return memslot;
1379 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001380 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381}
Izik Eidus28430992008-10-03 17:40:32 +03001382EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001383
1384struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1385{
1386 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001387 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001388}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389
Izik Eiduse0d62c72007-10-24 23:57:46 +02001390int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1391{
1392 int i;
1393
1394 gfn = unalias_gfn(kvm, gfn);
1395 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1396 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1397
1398 if (gfn >= memslot->base_gfn
1399 && gfn < memslot->base_gfn + memslot->npages)
1400 return 1;
1401 }
1402 return 0;
1403}
1404EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1405
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001406unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001407{
1408 struct kvm_memory_slot *slot;
1409
1410 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001411 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001412 if (!slot)
1413 return bad_hva();
1414 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1415}
Sheng Yang0d150292008-04-25 21:44:50 +08001416EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001417
Anthony Liguori35149e22008-04-02 14:46:56 -05001418pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001419{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001420 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001421 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001422 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001423 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001424
Avi Kivity60395222007-10-21 11:03:36 +02001425 might_sleep();
1426
Izik Eidus539cb662007-11-11 22:05:04 +02001427 addr = gfn_to_hva(kvm, gfn);
1428 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001429 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001430 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001431 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001432
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001433 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001434
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001435 if (unlikely(npages != 1)) {
1436 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001437
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001438 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001439 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001440
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001441 if (vma == NULL || addr < vma->vm_start ||
1442 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001443 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001444 get_page(bad_page);
1445 return page_to_pfn(bad_page);
1446 }
1447
1448 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001449 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001450 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001451 } else
1452 pfn = page_to_pfn(page[0]);
1453
1454 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001455}
1456
1457EXPORT_SYMBOL_GPL(gfn_to_pfn);
1458
1459struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1460{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001461 pfn_t pfn;
1462
1463 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001464 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001465 return pfn_to_page(pfn);
1466
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001467 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001468
1469 get_page(bad_page);
1470 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001471}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001472
Avi Kivity954bbbc2007-03-30 14:02:32 +03001473EXPORT_SYMBOL_GPL(gfn_to_page);
1474
Izik Eidusb4231d62007-11-20 11:49:33 +02001475void kvm_release_page_clean(struct page *page)
1476{
Anthony Liguori35149e22008-04-02 14:46:56 -05001477 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001478}
1479EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1480
Anthony Liguori35149e22008-04-02 14:46:56 -05001481void kvm_release_pfn_clean(pfn_t pfn)
1482{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001483 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001484 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001485}
1486EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1487
Izik Eidusb4231d62007-11-20 11:49:33 +02001488void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001489{
Anthony Liguori35149e22008-04-02 14:46:56 -05001490 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001491}
Izik Eidusb4231d62007-11-20 11:49:33 +02001492EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001493
Anthony Liguori35149e22008-04-02 14:46:56 -05001494void kvm_release_pfn_dirty(pfn_t pfn)
1495{
1496 kvm_set_pfn_dirty(pfn);
1497 kvm_release_pfn_clean(pfn);
1498}
1499EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1500
1501void kvm_set_page_dirty(struct page *page)
1502{
1503 kvm_set_pfn_dirty(page_to_pfn(page));
1504}
1505EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1506
1507void kvm_set_pfn_dirty(pfn_t pfn)
1508{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001509 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001510 struct page *page = pfn_to_page(pfn);
1511 if (!PageReserved(page))
1512 SetPageDirty(page);
1513 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001514}
1515EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1516
1517void kvm_set_pfn_accessed(pfn_t pfn)
1518{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001519 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001520 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001521}
1522EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1523
1524void kvm_get_pfn(pfn_t pfn)
1525{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001526 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001527 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001528}
1529EXPORT_SYMBOL_GPL(kvm_get_pfn);
1530
Izik Eidus195aefd2007-10-01 22:14:18 +02001531static int next_segment(unsigned long len, int offset)
1532{
1533 if (len > PAGE_SIZE - offset)
1534 return PAGE_SIZE - offset;
1535 else
1536 return len;
1537}
1538
1539int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1540 int len)
1541{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001542 int r;
1543 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001544
Izik Eiduse0506bc2007-11-11 22:10:22 +02001545 addr = gfn_to_hva(kvm, gfn);
1546 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001547 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001548 r = copy_from_user(data, (void __user *)addr + offset, len);
1549 if (r)
1550 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001551 return 0;
1552}
1553EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1554
1555int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1556{
1557 gfn_t gfn = gpa >> PAGE_SHIFT;
1558 int seg;
1559 int offset = offset_in_page(gpa);
1560 int ret;
1561
1562 while ((seg = next_segment(len, offset)) != 0) {
1563 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1564 if (ret < 0)
1565 return ret;
1566 offset = 0;
1567 len -= seg;
1568 data += seg;
1569 ++gfn;
1570 }
1571 return 0;
1572}
1573EXPORT_SYMBOL_GPL(kvm_read_guest);
1574
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001575int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1576 unsigned long len)
1577{
1578 int r;
1579 unsigned long addr;
1580 gfn_t gfn = gpa >> PAGE_SHIFT;
1581 int offset = offset_in_page(gpa);
1582
1583 addr = gfn_to_hva(kvm, gfn);
1584 if (kvm_is_error_hva(addr))
1585 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001586 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001587 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001588 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001589 if (r)
1590 return -EFAULT;
1591 return 0;
1592}
1593EXPORT_SYMBOL(kvm_read_guest_atomic);
1594
Izik Eidus195aefd2007-10-01 22:14:18 +02001595int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1596 int offset, int len)
1597{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001598 int r;
1599 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001600
Izik Eiduse0506bc2007-11-11 22:10:22 +02001601 addr = gfn_to_hva(kvm, gfn);
1602 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001603 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001604 r = copy_to_user((void __user *)addr + offset, data, len);
1605 if (r)
1606 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001607 mark_page_dirty(kvm, gfn);
1608 return 0;
1609}
1610EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1611
1612int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1613 unsigned long len)
1614{
1615 gfn_t gfn = gpa >> PAGE_SHIFT;
1616 int seg;
1617 int offset = offset_in_page(gpa);
1618 int ret;
1619
1620 while ((seg = next_segment(len, offset)) != 0) {
1621 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1622 if (ret < 0)
1623 return ret;
1624 offset = 0;
1625 len -= seg;
1626 data += seg;
1627 ++gfn;
1628 }
1629 return 0;
1630}
1631
1632int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1633{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001634 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001635}
1636EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1637
1638int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1639{
1640 gfn_t gfn = gpa >> PAGE_SHIFT;
1641 int seg;
1642 int offset = offset_in_page(gpa);
1643 int ret;
1644
1645 while ((seg = next_segment(len, offset)) != 0) {
1646 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1647 if (ret < 0)
1648 return ret;
1649 offset = 0;
1650 len -= seg;
1651 ++gfn;
1652 }
1653 return 0;
1654}
1655EXPORT_SYMBOL_GPL(kvm_clear_guest);
1656
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1658{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001659 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660
Uri Lublin3b6fff12007-10-30 10:42:09 +02001661 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001662 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001663 if (memslot && memslot->dirty_bitmap) {
1664 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665
Rusty Russell7e9d6192007-07-31 20:41:14 +10001666 /* avoid RMW */
1667 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1668 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669 }
1670}
1671
Eddie Dongb6958ce2007-07-18 12:15:21 +03001672/*
1673 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1674 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001675void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001676{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001677 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001678
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001679 for (;;) {
1680 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001681
Gleb Natapova1b37102009-07-09 15:33:52 +03001682 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001683 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001684 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001685 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001686 if (kvm_cpu_has_pending_timer(vcpu))
1687 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001688 if (signal_pending(current))
1689 break;
1690
Eddie Dongb6958ce2007-07-18 12:15:21 +03001691 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001692 }
1693
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001694 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001695}
1696
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697void kvm_resched(struct kvm_vcpu *vcpu)
1698{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001699 if (!need_resched())
1700 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702}
1703EXPORT_SYMBOL_GPL(kvm_resched);
1704
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001705static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001706{
1707 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001708 struct page *page;
1709
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001710 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001711 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001712#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001713 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001714 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001715#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001716#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1717 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1718 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1719#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001720 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001721 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001722 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001723 vmf->page = page;
1724 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001725}
1726
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001727static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001728 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001729};
1730
1731static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1732{
1733 vma->vm_ops = &kvm_vcpu_vm_ops;
1734 return 0;
1735}
1736
Avi Kivitybccf2152007-02-21 18:04:26 +02001737static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1738{
1739 struct kvm_vcpu *vcpu = filp->private_data;
1740
Al Viro66c0b392008-04-19 20:33:56 +01001741 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001742 return 0;
1743}
1744
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001745static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001746 .release = kvm_vcpu_release,
1747 .unlocked_ioctl = kvm_vcpu_ioctl,
1748 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001749 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001750};
1751
1752/*
1753 * Allocates an inode for the vcpu.
1754 */
1755static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1756{
Gleb Natapov73880c82009-06-09 15:56:28 +03001757 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Avi Kivitybccf2152007-02-21 18:04:26 +02001758}
1759
Avi Kivityc5ea7662007-02-20 18:41:05 +02001760/*
1761 * Creates some virtual cpus. Good luck creating more than one.
1762 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001763static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001764{
1765 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001766 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001767
Gleb Natapov73880c82009-06-09 15:56:28 +03001768 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001769 if (IS_ERR(vcpu))
1770 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001771
Avi Kivity15ad7142007-07-11 18:17:21 +03001772 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1773
Avi Kivity26e52152007-11-20 15:30:24 +02001774 r = kvm_arch_vcpu_setup(vcpu);
1775 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001776 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001777
Shaohua Li11ec2802007-07-23 14:51:37 +08001778 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001779 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1780 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001781 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001782 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001783
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001784 kvm_for_each_vcpu(r, v, kvm)
1785 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001786 r = -EEXIST;
1787 goto vcpu_destroy;
1788 }
1789
1790 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001791
1792 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001793 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001794 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001795 if (r < 0) {
1796 kvm_put_kvm(kvm);
1797 goto vcpu_destroy;
1798 }
1799
1800 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1801 smp_wmb();
1802 atomic_inc(&kvm->online_vcpus);
1803
1804#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1805 if (kvm->bsp_vcpu_id == id)
1806 kvm->bsp_vcpu = vcpu;
1807#endif
1808 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001809 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001810
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001811vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001812 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001813 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001814 return r;
1815}
1816
Avi Kivity1961d272007-03-05 19:46:05 +02001817static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1818{
1819 if (sigset) {
1820 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1821 vcpu->sigset_active = 1;
1822 vcpu->sigset = *sigset;
1823 } else
1824 vcpu->sigset_active = 0;
1825 return 0;
1826}
1827
Sheng Yangc1e01512009-02-25 17:22:26 +08001828#ifdef __KVM_HAVE_MSIX
1829static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1830 struct kvm_assigned_msix_nr *entry_nr)
1831{
1832 int r = 0;
1833 struct kvm_assigned_dev_kernel *adev;
1834
1835 mutex_lock(&kvm->lock);
1836
1837 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1838 entry_nr->assigned_dev_id);
1839 if (!adev) {
1840 r = -EINVAL;
1841 goto msix_nr_out;
1842 }
1843
1844 if (adev->entries_nr == 0) {
1845 adev->entries_nr = entry_nr->entry_nr;
1846 if (adev->entries_nr == 0 ||
1847 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1848 r = -EINVAL;
1849 goto msix_nr_out;
1850 }
1851
1852 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1853 entry_nr->entry_nr,
1854 GFP_KERNEL);
1855 if (!adev->host_msix_entries) {
1856 r = -ENOMEM;
1857 goto msix_nr_out;
1858 }
1859 adev->guest_msix_entries = kzalloc(
1860 sizeof(struct kvm_guest_msix_entry) *
1861 entry_nr->entry_nr, GFP_KERNEL);
1862 if (!adev->guest_msix_entries) {
1863 kfree(adev->host_msix_entries);
1864 r = -ENOMEM;
1865 goto msix_nr_out;
1866 }
1867 } else /* Not allowed set MSI-X number twice */
1868 r = -EINVAL;
1869msix_nr_out:
1870 mutex_unlock(&kvm->lock);
1871 return r;
1872}
1873
1874static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1875 struct kvm_assigned_msix_entry *entry)
1876{
1877 int r = 0, i;
1878 struct kvm_assigned_dev_kernel *adev;
1879
1880 mutex_lock(&kvm->lock);
1881
1882 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1883 entry->assigned_dev_id);
1884
1885 if (!adev) {
1886 r = -EINVAL;
1887 goto msix_entry_out;
1888 }
1889
1890 for (i = 0; i < adev->entries_nr; i++)
1891 if (adev->guest_msix_entries[i].vector == 0 ||
1892 adev->guest_msix_entries[i].entry == entry->entry) {
1893 adev->guest_msix_entries[i].entry = entry->entry;
1894 adev->guest_msix_entries[i].vector = entry->gsi;
1895 adev->host_msix_entries[i].entry = entry->entry;
1896 break;
1897 }
1898 if (i == adev->entries_nr) {
1899 r = -ENOSPC;
1900 goto msix_entry_out;
1901 }
1902
1903msix_entry_out:
1904 mutex_unlock(&kvm->lock);
1905
1906 return r;
1907}
1908#endif
1909
Avi Kivitybccf2152007-02-21 18:04:26 +02001910static long kvm_vcpu_ioctl(struct file *filp,
1911 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912{
Avi Kivitybccf2152007-02-21 18:04:26 +02001913 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001914 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001915 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001916 struct kvm_fpu *fpu = NULL;
1917 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001919 if (vcpu->kvm->mm != current->mm)
1920 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001922 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001923 r = -EINVAL;
1924 if (arg)
1925 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001926 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001929 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001931 r = -ENOMEM;
1932 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1933 if (!kvm_regs)
1934 goto out;
1935 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001937 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001939 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1940 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001942out_free1:
1943 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 break;
1945 }
1946 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001947 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001949 r = -ENOMEM;
1950 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1951 if (!kvm_regs)
1952 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001954 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1955 goto out_free2;
1956 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001958 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001960out_free2:
1961 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962 break;
1963 }
1964 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001965 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1966 r = -ENOMEM;
1967 if (!kvm_sregs)
1968 goto out;
1969 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 if (r)
1971 goto out;
1972 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001973 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 goto out;
1975 r = 0;
1976 break;
1977 }
1978 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001979 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1980 r = -ENOMEM;
1981 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001983 r = -EFAULT;
1984 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1985 goto out;
1986 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987 if (r)
1988 goto out;
1989 r = 0;
1990 break;
1991 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001992 case KVM_GET_MP_STATE: {
1993 struct kvm_mp_state mp_state;
1994
1995 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1996 if (r)
1997 goto out;
1998 r = -EFAULT;
1999 if (copy_to_user(argp, &mp_state, sizeof mp_state))
2000 goto out;
2001 r = 0;
2002 break;
2003 }
2004 case KVM_SET_MP_STATE: {
2005 struct kvm_mp_state mp_state;
2006
2007 r = -EFAULT;
2008 if (copy_from_user(&mp_state, argp, sizeof mp_state))
2009 goto out;
2010 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
2011 if (r)
2012 goto out;
2013 r = 0;
2014 break;
2015 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016 case KVM_TRANSLATE: {
2017 struct kvm_translation tr;
2018
2019 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002020 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08002022 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023 if (r)
2024 goto out;
2025 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002026 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027 goto out;
2028 r = 0;
2029 break;
2030 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002031 case KVM_SET_GUEST_DEBUG: {
2032 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033
2034 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002035 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002037 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002038 if (r)
2039 goto out;
2040 r = 0;
2041 break;
2042 }
Avi Kivity1961d272007-03-05 19:46:05 +02002043 case KVM_SET_SIGNAL_MASK: {
2044 struct kvm_signal_mask __user *sigmask_arg = argp;
2045 struct kvm_signal_mask kvm_sigmask;
2046 sigset_t sigset, *p;
2047
2048 p = NULL;
2049 if (argp) {
2050 r = -EFAULT;
2051 if (copy_from_user(&kvm_sigmask, argp,
2052 sizeof kvm_sigmask))
2053 goto out;
2054 r = -EINVAL;
2055 if (kvm_sigmask.len != sizeof sigset)
2056 goto out;
2057 r = -EFAULT;
2058 if (copy_from_user(&sigset, sigmask_arg->sigset,
2059 sizeof sigset))
2060 goto out;
2061 p = &sigset;
2062 }
2063 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2064 break;
2065 }
Avi Kivityb8836732007-04-01 16:34:31 +03002066 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002067 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2068 r = -ENOMEM;
2069 if (!fpu)
2070 goto out;
2071 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002072 if (r)
2073 goto out;
2074 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002075 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03002076 goto out;
2077 r = 0;
2078 break;
2079 }
2080 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002081 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2082 r = -ENOMEM;
2083 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002084 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002085 r = -EFAULT;
2086 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2087 goto out;
2088 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002089 if (r)
2090 goto out;
2091 r = 0;
2092 break;
2093 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002094 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002095 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002096 }
2097out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002098 kfree(fpu);
2099 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002100 return r;
2101}
2102
2103static long kvm_vm_ioctl(struct file *filp,
2104 unsigned int ioctl, unsigned long arg)
2105{
2106 struct kvm *kvm = filp->private_data;
2107 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002108 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002109
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002110 if (kvm->mm != current->mm)
2111 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002112 switch (ioctl) {
2113 case KVM_CREATE_VCPU:
2114 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2115 if (r < 0)
2116 goto out;
2117 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002118 case KVM_SET_USER_MEMORY_REGION: {
2119 struct kvm_userspace_memory_region kvm_userspace_mem;
2120
2121 r = -EFAULT;
2122 if (copy_from_user(&kvm_userspace_mem, argp,
2123 sizeof kvm_userspace_mem))
2124 goto out;
2125
2126 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127 if (r)
2128 goto out;
2129 break;
2130 }
2131 case KVM_GET_DIRTY_LOG: {
2132 struct kvm_dirty_log log;
2133
2134 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002135 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002137 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 if (r)
2139 goto out;
2140 break;
2141 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002142#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2143 case KVM_REGISTER_COALESCED_MMIO: {
2144 struct kvm_coalesced_mmio_zone zone;
2145 r = -EFAULT;
2146 if (copy_from_user(&zone, argp, sizeof zone))
2147 goto out;
2148 r = -ENXIO;
2149 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2150 if (r)
2151 goto out;
2152 r = 0;
2153 break;
2154 }
2155 case KVM_UNREGISTER_COALESCED_MMIO: {
2156 struct kvm_coalesced_mmio_zone zone;
2157 r = -EFAULT;
2158 if (copy_from_user(&zone, argp, sizeof zone))
2159 goto out;
2160 r = -ENXIO;
2161 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2162 if (r)
2163 goto out;
2164 r = 0;
2165 break;
2166 }
2167#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002168#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2169 case KVM_ASSIGN_PCI_DEVICE: {
2170 struct kvm_assigned_pci_dev assigned_dev;
2171
2172 r = -EFAULT;
2173 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2174 goto out;
2175 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2176 if (r)
2177 goto out;
2178 break;
2179 }
2180 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002181 r = -EOPNOTSUPP;
2182 break;
2183 }
2184#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2185 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002186 struct kvm_assigned_irq assigned_irq;
2187
2188 r = -EFAULT;
2189 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2190 goto out;
2191 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2192 if (r)
2193 goto out;
2194 break;
2195 }
Sheng Yange56d5322009-03-12 21:45:39 +08002196 case KVM_DEASSIGN_DEV_IRQ: {
2197 struct kvm_assigned_irq assigned_irq;
2198
2199 r = -EFAULT;
2200 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2201 goto out;
2202 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2203 if (r)
2204 goto out;
2205 break;
2206 }
2207#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002208#endif
Weidong Han0a920352008-12-02 21:24:23 +08002209#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2210 case KVM_DEASSIGN_PCI_DEVICE: {
2211 struct kvm_assigned_pci_dev assigned_dev;
2212
2213 r = -EFAULT;
2214 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2215 goto out;
2216 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2217 if (r)
2218 goto out;
2219 break;
2220 }
2221#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002222#ifdef KVM_CAP_IRQ_ROUTING
2223 case KVM_SET_GSI_ROUTING: {
2224 struct kvm_irq_routing routing;
2225 struct kvm_irq_routing __user *urouting;
2226 struct kvm_irq_routing_entry *entries;
2227
2228 r = -EFAULT;
2229 if (copy_from_user(&routing, argp, sizeof(routing)))
2230 goto out;
2231 r = -EINVAL;
2232 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2233 goto out;
2234 if (routing.flags)
2235 goto out;
2236 r = -ENOMEM;
2237 entries = vmalloc(routing.nr * sizeof(*entries));
2238 if (!entries)
2239 goto out;
2240 r = -EFAULT;
2241 urouting = argp;
2242 if (copy_from_user(entries, urouting->entries,
2243 routing.nr * sizeof(*entries)))
2244 goto out_free_irq_routing;
2245 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2246 routing.flags);
2247 out_free_irq_routing:
2248 vfree(entries);
2249 break;
2250 }
Avi Kivity6621fbc2009-08-10 17:00:52 +03002251#endif /* KVM_CAP_IRQ_ROUTING */
Sheng Yangc1e01512009-02-25 17:22:26 +08002252#ifdef __KVM_HAVE_MSIX
2253 case KVM_ASSIGN_SET_MSIX_NR: {
2254 struct kvm_assigned_msix_nr entry_nr;
2255 r = -EFAULT;
2256 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2257 goto out;
2258 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2259 if (r)
2260 goto out;
2261 break;
2262 }
2263 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2264 struct kvm_assigned_msix_entry entry;
2265 r = -EFAULT;
2266 if (copy_from_user(&entry, argp, sizeof entry))
2267 goto out;
2268 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2269 if (r)
2270 goto out;
2271 break;
2272 }
Avi Kivity399ec802008-11-19 13:58:46 +02002273#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04002274 case KVM_IRQFD: {
2275 struct kvm_irqfd data;
2276
2277 r = -EFAULT;
2278 if (copy_from_user(&data, argp, sizeof data))
2279 goto out;
2280 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2281 break;
2282 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002283 case KVM_IOEVENTFD: {
2284 struct kvm_ioeventfd data;
2285
2286 r = -EFAULT;
2287 if (copy_from_user(&data, argp, sizeof data))
2288 goto out;
2289 r = kvm_ioeventfd(kvm, &data);
2290 break;
2291 }
Gleb Natapov73880c82009-06-09 15:56:28 +03002292#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2293 case KVM_SET_BOOT_CPU_ID:
2294 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002295 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002296 if (atomic_read(&kvm->online_vcpus) != 0)
2297 r = -EBUSY;
2298 else
2299 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002300 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002301 break;
2302#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002303 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002304 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002305 }
2306out:
2307 return r;
2308}
2309
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002310static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002311{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002312 struct page *page[1];
2313 unsigned long addr;
2314 int npages;
2315 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002316 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002317
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002318 addr = gfn_to_hva(kvm, gfn);
2319 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002320 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002321
2322 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2323 NULL);
2324 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002325 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002326
2327 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002328 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002329}
2330
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002331static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002332 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002333};
2334
2335static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2336{
2337 vma->vm_ops = &kvm_vm_vm_ops;
2338 return 0;
2339}
2340
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002341static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002342 .release = kvm_vm_release,
2343 .unlocked_ioctl = kvm_vm_ioctl,
2344 .compat_ioctl = kvm_vm_ioctl,
2345 .mmap = kvm_vm_mmap,
2346};
2347
2348static int kvm_dev_ioctl_create_vm(void)
2349{
Al Viro2030a422008-02-23 06:46:49 -05002350 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002351 struct kvm *kvm;
2352
Avi Kivityf17abe92007-02-21 19:28:04 +02002353 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002354 if (IS_ERR(kvm))
2355 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002356 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002357 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002358 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002359
Avi Kivityf17abe92007-02-21 19:28:04 +02002360 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002361}
2362
Avi Kivity1a811b62008-12-08 18:25:27 +02002363static long kvm_dev_ioctl_check_extension_generic(long arg)
2364{
2365 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002366 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002367 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002368 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002369#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2370 case KVM_CAP_SET_BOOT_CPU_ID:
2371#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002372 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002373#ifdef CONFIG_HAVE_KVM_IRQCHIP
2374 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002375 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002376#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002377 default:
2378 break;
2379 }
2380 return kvm_dev_ioctl_check_extension(arg);
2381}
2382
Avi Kivityf17abe92007-02-21 19:28:04 +02002383static long kvm_dev_ioctl(struct file *filp,
2384 unsigned int ioctl, unsigned long arg)
2385{
Avi Kivity07c45a32007-03-07 13:05:38 +02002386 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002387
2388 switch (ioctl) {
2389 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002390 r = -EINVAL;
2391 if (arg)
2392 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002393 r = KVM_API_VERSION;
2394 break;
2395 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002396 r = -EINVAL;
2397 if (arg)
2398 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002399 r = kvm_dev_ioctl_create_vm();
2400 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002401 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002402 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002403 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002404 case KVM_GET_VCPU_MMAP_SIZE:
2405 r = -EINVAL;
2406 if (arg)
2407 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002408 r = PAGE_SIZE; /* struct kvm_run */
2409#ifdef CONFIG_X86
2410 r += PAGE_SIZE; /* pio data page */
2411#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002412#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2413 r += PAGE_SIZE; /* coalesced mmio ring page */
2414#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002415 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002416 case KVM_TRACE_ENABLE:
2417 case KVM_TRACE_PAUSE:
2418 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002419 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002420 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002422 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423 }
2424out:
2425 return r;
2426}
2427
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 .unlocked_ioctl = kvm_dev_ioctl,
2430 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431};
2432
2433static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002434 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 "kvm",
2436 &kvm_chardev_ops,
2437};
2438
Avi Kivity1b6c0162007-05-24 13:03:52 +03002439static void hardware_enable(void *junk)
2440{
2441 int cpu = raw_smp_processor_id();
2442
Rusty Russell7f59f492008-12-07 21:25:45 +10302443 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002444 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302445 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002446 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002447}
2448
2449static void hardware_disable(void *junk)
2450{
2451 int cpu = raw_smp_processor_id();
2452
Rusty Russell7f59f492008-12-07 21:25:45 +10302453 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002454 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302455 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002456 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002457}
2458
Avi Kivity774c47f2007-02-12 00:54:47 -08002459static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2460 void *v)
2461{
2462 int cpu = (long)v;
2463
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002464 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002465 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002466 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002467 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2468 cpu);
2469 hardware_disable(NULL);
2470 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002471 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002472 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2473 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002474 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002475 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002476 case CPU_ONLINE:
2477 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2478 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002479 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002480 break;
2481 }
2482 return NOTIFY_OK;
2483}
2484
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002485
2486asmlinkage void kvm_handle_fault_on_reboot(void)
2487{
2488 if (kvm_rebooting)
2489 /* spin while reset goes on */
2490 while (true)
2491 ;
2492 /* Fault while not rebooting. We want the trace. */
2493 BUG();
2494}
2495EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2496
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002497static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002498 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002499{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002500 /*
2501 * Some (well, at least mine) BIOSes hang on reboot if
2502 * in vmx root mode.
2503 *
2504 * And Intel TXT required VMX off for all cpu when system shutdown.
2505 */
2506 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2507 kvm_rebooting = true;
2508 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002509 return NOTIFY_OK;
2510}
2511
2512static struct notifier_block kvm_reboot_notifier = {
2513 .notifier_call = kvm_reboot,
2514 .priority = 0,
2515};
2516
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002517void kvm_io_bus_init(struct kvm_io_bus *bus)
2518{
2519 memset(bus, 0, sizeof(*bus));
2520}
2521
2522void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2523{
2524 int i;
2525
2526 for (i = 0; i < bus->dev_count; i++) {
2527 struct kvm_io_device *pos = bus->devs[i];
2528
2529 kvm_iodevice_destructor(pos);
2530 }
2531}
2532
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002533/* kvm_io_bus_write - called under kvm->slots_lock */
2534int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
2535 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002536{
2537 int i;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002538 for (i = 0; i < bus->dev_count; i++)
2539 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2540 return 0;
2541 return -EOPNOTSUPP;
2542}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002543
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002544/* kvm_io_bus_read - called under kvm->slots_lock */
2545int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
2546{
2547 int i;
2548 for (i = 0; i < bus->dev_count; i++)
2549 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2550 return 0;
2551 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002552}
2553
Gregory Haskins090b7af2009-07-07 17:08:44 -04002554int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002555 struct kvm_io_device *dev)
2556{
Gregory Haskins090b7af2009-07-07 17:08:44 -04002557 int ret;
2558
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002559 down_write(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002560 ret = __kvm_io_bus_register_dev(bus, dev);
2561 up_write(&kvm->slots_lock);
2562
2563 return ret;
2564}
2565
2566/* An unlocked version. Caller must have write lock on slots_lock. */
2567int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
2568 struct kvm_io_device *dev)
2569{
2570 if (bus->dev_count > NR_IOBUS_DEVS-1)
2571 return -ENOSPC;
2572
2573 bus->devs[bus->dev_count++] = dev;
2574
2575 return 0;
2576}
2577
2578void kvm_io_bus_unregister_dev(struct kvm *kvm,
2579 struct kvm_io_bus *bus,
2580 struct kvm_io_device *dev)
2581{
2582 down_write(&kvm->slots_lock);
2583 __kvm_io_bus_unregister_dev(bus, dev);
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002584 up_write(&kvm->slots_lock);
2585}
2586
2587/* An unlocked version. Caller must have write lock on slots_lock. */
Gregory Haskins090b7af2009-07-07 17:08:44 -04002588void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
2589 struct kvm_io_device *dev)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002590{
Gregory Haskins090b7af2009-07-07 17:08:44 -04002591 int i;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002592
Gregory Haskins090b7af2009-07-07 17:08:44 -04002593 for (i = 0; i < bus->dev_count; i++)
2594 if (bus->devs[i] == dev) {
2595 bus->devs[i] = bus->devs[--bus->dev_count];
2596 break;
2597 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002598}
2599
Avi Kivity774c47f2007-02-12 00:54:47 -08002600static struct notifier_block kvm_cpu_notifier = {
2601 .notifier_call = kvm_cpu_hotplug,
2602 .priority = 20, /* must be > scheduler priority */
2603};
2604
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002605static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002606{
2607 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002608 struct kvm *kvm;
2609
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002610 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002611 spin_lock(&kvm_lock);
2612 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002613 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002614 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002615 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002616}
2617
2618DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2619
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002620static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002621{
2622 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002623 struct kvm *kvm;
2624 struct kvm_vcpu *vcpu;
2625 int i;
2626
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002627 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002628 spin_lock(&kvm_lock);
2629 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002630 kvm_for_each_vcpu(i, vcpu, kvm)
2631 *val += *(u32 *)((void *)vcpu + offset);
2632
Avi Kivity1165f5f2007-04-19 17:27:43 +03002633 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002634 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002635}
2636
Avi Kivityba1389b2007-11-18 16:24:12 +02002637DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2638
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002639static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002640 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2641 [KVM_STAT_VM] = &vm_stat_fops,
2642};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002643
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002644static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645{
2646 struct kvm_stats_debugfs_item *p;
2647
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002648 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002650 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002651 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002652 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653}
2654
2655static void kvm_exit_debug(void)
2656{
2657 struct kvm_stats_debugfs_item *p;
2658
2659 for (p = debugfs_entries; p->name; ++p)
2660 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002661 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662}
2663
Avi Kivity59ae6c62007-02-12 00:54:48 -08002664static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2665{
Avi Kivity4267c412007-05-24 13:09:41 +03002666 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002667 return 0;
2668}
2669
2670static int kvm_resume(struct sys_device *dev)
2671{
Avi Kivity4267c412007-05-24 13:09:41 +03002672 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002673 return 0;
2674}
2675
2676static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002677 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002678 .suspend = kvm_suspend,
2679 .resume = kvm_resume,
2680};
2681
2682static struct sys_device kvm_sysdev = {
2683 .id = 0,
2684 .cls = &kvm_sysdev_class,
2685};
2686
Izik Eiduscea7bb22007-10-17 19:17:48 +02002687struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002688pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689
Avi Kivity15ad7142007-07-11 18:17:21 +03002690static inline
2691struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2692{
2693 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2694}
2695
2696static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2697{
2698 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2699
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002700 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002701}
2702
2703static void kvm_sched_out(struct preempt_notifier *pn,
2704 struct task_struct *next)
2705{
2706 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2707
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002708 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002709}
2710
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002711int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002712 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713{
2714 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002715 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002717 r = kvm_arch_init(opaque);
2718 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002719 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002720
2721 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2722
2723 if (bad_page == NULL) {
2724 r = -ENOMEM;
2725 goto out;
2726 }
2727
Anthony Liguori35149e22008-04-02 14:46:56 -05002728 bad_pfn = page_to_pfn(bad_page);
2729
Avi Kivity8437a612009-06-06 14:52:35 -07002730 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302731 r = -ENOMEM;
2732 goto out_free_0;
2733 }
2734
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002735 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302737 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738
Yang, Sheng002c7f72007-07-31 14:23:01 +03002739 for_each_online_cpu(cpu) {
2740 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002741 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002742 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002743 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002744 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002745 }
2746
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002747 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002748 r = register_cpu_notifier(&kvm_cpu_notifier);
2749 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002750 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 register_reboot_notifier(&kvm_reboot_notifier);
2752
Avi Kivity59ae6c62007-02-12 00:54:48 -08002753 r = sysdev_class_register(&kvm_sysdev_class);
2754 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002755 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002756
2757 r = sysdev_register(&kvm_sysdev);
2758 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002759 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002760
Rusty Russellc16f8622007-07-30 21:12:19 +10002761 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2762 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002763 __alignof__(struct kvm_vcpu),
2764 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002765 if (!kvm_vcpu_cache) {
2766 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002767 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002768 }
2769
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002771 kvm_vm_fops.owner = module;
2772 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773
2774 r = misc_register(&kvm_dev);
2775 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002776 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 goto out_free;
2778 }
2779
Avi Kivity15ad7142007-07-11 18:17:21 +03002780 kvm_preempt_ops.sched_in = kvm_sched_in;
2781 kvm_preempt_ops.sched_out = kvm_sched_out;
2782
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002783 kvm_init_debug();
2784
Avi Kivityc7addb92007-09-16 18:58:32 +02002785 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786
2787out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002788 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002789out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002790 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002791out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002792 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002793out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002795 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002796out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002797 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002798out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002799 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302800out_free_0a:
2801 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002802out_free_0:
2803 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002804out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002805 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002806out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 return r;
2808}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002809EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002811void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002813 tracepoint_synchronize_unregister();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002814 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002815 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002816 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002817 sysdev_unregister(&kvm_sysdev);
2818 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002820 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002821 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002822 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002823 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302824 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002825 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002827EXPORT_SYMBOL_GPL(kvm_exit);