blob: 0edc366ecf89aaa3e4a3f7e3df121f13e6e4d7f5 [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 *
71 * kvm->lock --> kvm->irq_lock
72 */
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
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030741 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
742 cpumask_clear(cpus);
743
Avi Kivity597a5f52008-07-20 14:24:22 +0300744 me = get_cpu();
Marcelo Tosatti84261922009-06-17 10:53:47 -0300745 spin_lock(&kvm->requests_lock);
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300746 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030747 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300748 continue;
749 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030750 if (cpus != NULL && cpu != -1 && cpu != me)
751 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300752 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030753 if (unlikely(cpus == NULL))
754 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
755 else if (!cpumask_empty(cpus))
756 smp_call_function_many(cpus, ack_flush, NULL, 1);
757 else
758 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300759 spin_unlock(&kvm->requests_lock);
Avi Kivity597a5f52008-07-20 14:24:22 +0300760 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030761 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030762 return called;
763}
764
765void kvm_flush_remote_tlbs(struct kvm *kvm)
766{
767 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
768 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300769}
770
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500771void kvm_reload_remote_mmus(struct kvm *kvm)
772{
Rusty Russell49846892008-12-08 20:26:24 +1030773 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500774}
775
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000776int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
777{
778 struct page *page;
779 int r;
780
781 mutex_init(&vcpu->mutex);
782 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000783 vcpu->kvm = kvm;
784 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300785 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000786
787 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
788 if (!page) {
789 r = -ENOMEM;
790 goto fail;
791 }
792 vcpu->run = page_address(page);
793
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800794 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000795 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800796 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000797 return 0;
798
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000799fail_free_run:
800 free_page((unsigned long)vcpu->run);
801fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000802 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000803}
804EXPORT_SYMBOL_GPL(kvm_vcpu_init);
805
806void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
807{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800808 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000809 free_page((unsigned long)vcpu->run);
810}
811EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
812
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200813#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
814static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
815{
816 return container_of(mn, struct kvm, mmu_notifier);
817}
818
819static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
820 struct mm_struct *mm,
821 unsigned long address)
822{
823 struct kvm *kvm = mmu_notifier_to_kvm(mn);
824 int need_tlb_flush;
825
826 /*
827 * When ->invalidate_page runs, the linux pte has been zapped
828 * already but the page is still allocated until
829 * ->invalidate_page returns. So if we increase the sequence
830 * here the kvm page fault will notice if the spte can't be
831 * established because the page is going to be freed. If
832 * instead the kvm page fault establishes the spte before
833 * ->invalidate_page runs, kvm_unmap_hva will release it
834 * before returning.
835 *
836 * The sequence increase only need to be seen at spin_unlock
837 * time, and not at spin_lock time.
838 *
839 * Increasing the sequence after the spin_unlock would be
840 * unsafe because the kvm page fault could then establish the
841 * pte after kvm_unmap_hva returned, without noticing the page
842 * is going to be freed.
843 */
844 spin_lock(&kvm->mmu_lock);
845 kvm->mmu_notifier_seq++;
846 need_tlb_flush = kvm_unmap_hva(kvm, address);
847 spin_unlock(&kvm->mmu_lock);
848
849 /* we've to flush the tlb before the pages can be freed */
850 if (need_tlb_flush)
851 kvm_flush_remote_tlbs(kvm);
852
853}
854
855static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
856 struct mm_struct *mm,
857 unsigned long start,
858 unsigned long end)
859{
860 struct kvm *kvm = mmu_notifier_to_kvm(mn);
861 int need_tlb_flush = 0;
862
863 spin_lock(&kvm->mmu_lock);
864 /*
865 * The count increase must become visible at unlock time as no
866 * spte can be established without taking the mmu_lock and
867 * count is also read inside the mmu_lock critical section.
868 */
869 kvm->mmu_notifier_count++;
870 for (; start < end; start += PAGE_SIZE)
871 need_tlb_flush |= kvm_unmap_hva(kvm, start);
872 spin_unlock(&kvm->mmu_lock);
873
874 /* we've to flush the tlb before the pages can be freed */
875 if (need_tlb_flush)
876 kvm_flush_remote_tlbs(kvm);
877}
878
879static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
880 struct mm_struct *mm,
881 unsigned long start,
882 unsigned long end)
883{
884 struct kvm *kvm = mmu_notifier_to_kvm(mn);
885
886 spin_lock(&kvm->mmu_lock);
887 /*
888 * This sequence increase will notify the kvm page fault that
889 * the page that is going to be mapped in the spte could have
890 * been freed.
891 */
892 kvm->mmu_notifier_seq++;
893 /*
894 * The above sequence increase must be visible before the
895 * below count decrease but both values are read by the kvm
896 * page fault under mmu_lock spinlock so we don't need to add
897 * a smb_wmb() here in between the two.
898 */
899 kvm->mmu_notifier_count--;
900 spin_unlock(&kvm->mmu_lock);
901
902 BUG_ON(kvm->mmu_notifier_count < 0);
903}
904
905static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
906 struct mm_struct *mm,
907 unsigned long address)
908{
909 struct kvm *kvm = mmu_notifier_to_kvm(mn);
910 int young;
911
912 spin_lock(&kvm->mmu_lock);
913 young = kvm_age_hva(kvm, address);
914 spin_unlock(&kvm->mmu_lock);
915
916 if (young)
917 kvm_flush_remote_tlbs(kvm);
918
919 return young;
920}
921
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100922static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
923 struct mm_struct *mm)
924{
925 struct kvm *kvm = mmu_notifier_to_kvm(mn);
926 kvm_arch_flush_shadow(kvm);
927}
928
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200929static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
930 .invalidate_page = kvm_mmu_notifier_invalidate_page,
931 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
932 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
933 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100934 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200935};
936#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
937
Avi Kivityf17abe92007-02-21 19:28:04 +0200938static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800940 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200941#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
942 struct page *page;
943#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800945 if (IS_ERR(kvm))
946 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200947#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200948 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200949 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
950#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951
Laurent Vivier5f94c172008-05-30 16:05:54 +0200952#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
953 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
954 if (!page) {
955 kfree(kvm);
956 return ERR_PTR(-ENOMEM);
957 }
958 kvm->coalesced_mmio_ring =
959 (struct kvm_coalesced_mmio_ring *)page_address(page);
960#endif
961
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200962#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
963 {
964 int err;
965 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
966 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
967 if (err) {
968#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
969 put_page(page);
970#endif
971 kfree(kvm);
972 return ERR_PTR(err);
973 }
974 }
975#endif
976
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200977 kvm->mm = current->mm;
978 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500979 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300980 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300981 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400982 kvm_irqfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800983 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300984 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400985 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200986 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300987 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000988 spin_lock(&kvm_lock);
989 list_add(&kvm->vm_list, &vm_list);
990 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200991#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
992 kvm_coalesced_mmio_init(kvm);
993#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800994out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200995 return kvm;
996}
997
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998/*
999 * Free any memory in @free but not in @dont.
1000 */
1001static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1002 struct kvm_memory_slot *dont)
1003{
Joerg Roedelec04b262009-06-19 15:16:23 +02001004 int i;
1005
Izik Eidus290fc382007-09-27 14:11:22 +02001006 if (!dont || free->rmap != dont->rmap)
1007 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008
1009 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1010 vfree(free->dirty_bitmap);
1011
Joerg Roedelec04b262009-06-19 15:16:23 +02001012
1013 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1014 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
1015 vfree(free->lpage_info[i]);
1016 free->lpage_info[i] = NULL;
1017 }
1018 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001019
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001021 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001022 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023}
1024
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001025void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026{
1027 int i;
1028
1029 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001030 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031}
1032
Avi Kivityf17abe92007-02-21 19:28:04 +02001033static void kvm_destroy_vm(struct kvm *kvm)
1034{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001035 struct mm_struct *mm = kvm->mm;
1036
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001037 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001038 spin_lock(&kvm_lock);
1039 list_del(&kvm->vm_list);
1040 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001041 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001042 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001043 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001044#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1045 if (kvm->coalesced_mmio_ring != NULL)
1046 free_page((unsigned long)kvm->coalesced_mmio_ring);
1047#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001048#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1049 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +02001050#else
1051 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001052#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001053 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001054 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001055}
1056
Izik Eidusd39f13b2008-03-30 16:01:25 +03001057void kvm_get_kvm(struct kvm *kvm)
1058{
1059 atomic_inc(&kvm->users_count);
1060}
1061EXPORT_SYMBOL_GPL(kvm_get_kvm);
1062
1063void kvm_put_kvm(struct kvm *kvm)
1064{
1065 if (atomic_dec_and_test(&kvm->users_count))
1066 kvm_destroy_vm(kvm);
1067}
1068EXPORT_SYMBOL_GPL(kvm_put_kvm);
1069
1070
Avi Kivityf17abe92007-02-21 19:28:04 +02001071static int kvm_vm_release(struct inode *inode, struct file *filp)
1072{
1073 struct kvm *kvm = filp->private_data;
1074
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001075 kvm_irqfd_release(kvm);
1076
Izik Eidusd39f13b2008-03-30 16:01:25 +03001077 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 return 0;
1079}
1080
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 * Allocate some memory and give it an address in the guest physical address
1083 * space.
1084 *
1085 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001086 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001087 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001089int __kvm_set_memory_region(struct kvm *kvm,
1090 struct kvm_userspace_memory_region *mem,
1091 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092{
1093 int r;
1094 gfn_t base_gfn;
Avi Kivityac045272009-06-08 15:52:39 +03001095 unsigned long npages, ugfn;
Joerg Roedelec04b262009-06-19 15:16:23 +02001096 int lpages;
1097 unsigned long i, j;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 struct kvm_memory_slot *memslot;
1099 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100
1101 r = -EINVAL;
1102 /* General sanity checks */
1103 if (mem->memory_size & (PAGE_SIZE - 1))
1104 goto out;
1105 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1106 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001107 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001108 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001109 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 goto out;
1111 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1112 goto out;
1113
1114 memslot = &kvm->memslots[mem->slot];
1115 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1116 npages = mem->memory_size >> PAGE_SHIFT;
1117
1118 if (!npages)
1119 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1120
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 new = old = *memslot;
1122
1123 new.base_gfn = base_gfn;
1124 new.npages = npages;
1125 new.flags = mem->flags;
1126
1127 /* Disallow changing a memory slot's size. */
1128 r = -EINVAL;
1129 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001130 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131
1132 /* Check for overlaps */
1133 r = -EEXIST;
1134 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1135 struct kvm_memory_slot *s = &kvm->memslots[i];
1136
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001137 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 continue;
1139 if (!((base_gfn + npages <= s->base_gfn) ||
1140 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001141 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 /* Free page dirty bitmap if unneeded */
1145 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001146 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147
1148 r = -ENOMEM;
1149
1150 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001151#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001152 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001153 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001154
1155 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001156 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001157
Izik Eidus290fc382007-09-27 14:11:22 +02001158 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001159
Izik Eidus80b14b52007-10-25 11:54:04 +02001160 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001161 /*
1162 * hva_to_rmmap() serialzies with the mmu_lock and to be
1163 * safe it has to ignore memslots with !user_alloc &&
1164 * !userspace_addr.
1165 */
1166 if (user_alloc)
1167 new.userspace_addr = mem->userspace_addr;
1168 else
1169 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170 }
Joerg Roedelec04b262009-06-19 15:16:23 +02001171 if (!npages)
1172 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001173
Joerg Roedelec04b262009-06-19 15:16:23 +02001174 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1175 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001176
Joerg Roedelec04b262009-06-19 15:16:23 +02001177 /* Avoid unused variable warning if no large pages */
1178 (void)level;
1179
1180 if (new.lpage_info[i])
1181 continue;
1182
1183 lpages = 1 + (base_gfn + npages - 1) /
1184 KVM_PAGES_PER_HPAGE(level);
1185 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
1186
1187 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
1188
1189 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001190 goto out_free;
1191
Joerg Roedelec04b262009-06-19 15:16:23 +02001192 memset(new.lpage_info[i], 0,
1193 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001194
Joerg Roedelec04b262009-06-19 15:16:23 +02001195 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
1196 new.lpage_info[i][0].write_count = 1;
1197 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
1198 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +03001199 ugfn = new.userspace_addr >> PAGE_SHIFT;
1200 /*
1201 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001202 * other, or if explicitly asked to, disable large page
1203 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +03001204 */
Joerg Roedelec04b262009-06-19 15:16:23 +02001205 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001206 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +02001207 for (j = 0; j < lpages; ++j)
1208 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001209 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210
Joerg Roedelec04b262009-06-19 15:16:23 +02001211skip_lpage:
1212
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 /* Allocate page dirty bitmap if needed */
1214 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1215 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1216
1217 new.dirty_bitmap = vmalloc(dirty_bytes);
1218 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001219 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +03001221 if (old.npages)
1222 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +02001224#else /* not defined CONFIG_S390 */
1225 new.user_alloc = user_alloc;
1226 if (user_alloc)
1227 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +02001228#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001230 if (!npages)
1231 kvm_arch_flush_shadow(kvm);
1232
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001233 spin_lock(&kvm->mmu_lock);
1234 if (mem->slot >= kvm->nmemslots)
1235 kvm->nmemslots = mem->slot + 1;
1236
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001238 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239
Zhang Xiantao0de10342007-11-20 16:25:04 +08001240 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1241 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001242 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001243 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001244 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001245 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001246 }
1247
Glauber Costa6f897242008-12-03 13:40:51 -02001248 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1249 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001250 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -02001251 if (!npages)
1252 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001253 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001254#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001255 /* map the pages in iommu page table */
1256 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1257 if (r)
1258 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001259#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260 return 0;
1261
Sheng Yangf78e0e22007-10-29 09:40:42 +08001262out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263 kvm_free_physmem_slot(&new, &old);
1264out:
1265 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001266
1267}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001268EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1269
1270int kvm_set_memory_region(struct kvm *kvm,
1271 struct kvm_userspace_memory_region *mem,
1272 int user_alloc)
1273{
1274 int r;
1275
Izik Eidus72dc67a2008-02-10 18:04:15 +02001276 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001277 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001278 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001279 return r;
1280}
Izik Eidus210c7c42007-10-24 23:52:57 +02001281EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1282
Carsten Otte1fe779f2007-10-29 16:08:35 +01001283int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1284 struct
1285 kvm_userspace_memory_region *mem,
1286 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001287{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001288 if (mem->slot >= KVM_MEMORY_SLOTS)
1289 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001290 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291}
1292
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001293int kvm_get_dirty_log(struct kvm *kvm,
1294 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295{
1296 struct kvm_memory_slot *memslot;
1297 int r, i;
1298 int n;
1299 unsigned long any = 0;
1300
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 r = -EINVAL;
1302 if (log->slot >= KVM_MEMORY_SLOTS)
1303 goto out;
1304
1305 memslot = &kvm->memslots[log->slot];
1306 r = -ENOENT;
1307 if (!memslot->dirty_bitmap)
1308 goto out;
1309
Uri Lublincd1a4a92007-02-22 16:43:09 +02001310 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311
Uri Lublincd1a4a92007-02-22 16:43:09 +02001312 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 any = memslot->dirty_bitmap[i];
1314
1315 r = -EFAULT;
1316 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1317 goto out;
1318
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001319 if (any)
1320 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321
1322 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 return r;
1325}
1326
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001327void kvm_disable_largepages(void)
1328{
1329 largepages_enabled = false;
1330}
1331EXPORT_SYMBOL_GPL(kvm_disable_largepages);
1332
Izik Eiduscea7bb22007-10-17 19:17:48 +02001333int is_error_page(struct page *page)
1334{
1335 return page == bad_page;
1336}
1337EXPORT_SYMBOL_GPL(is_error_page);
1338
Anthony Liguori35149e22008-04-02 14:46:56 -05001339int is_error_pfn(pfn_t pfn)
1340{
1341 return pfn == bad_pfn;
1342}
1343EXPORT_SYMBOL_GPL(is_error_pfn);
1344
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001345static inline unsigned long bad_hva(void)
1346{
1347 return PAGE_OFFSET;
1348}
1349
1350int kvm_is_error_hva(unsigned long addr)
1351{
1352 return addr == bad_hva();
1353}
1354EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1355
Izik Eidus28430992008-10-03 17:40:32 +03001356struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357{
1358 int i;
1359
1360 for (i = 0; i < kvm->nmemslots; ++i) {
1361 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1362
1363 if (gfn >= memslot->base_gfn
1364 && gfn < memslot->base_gfn + memslot->npages)
1365 return memslot;
1366 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001367 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368}
Izik Eidus28430992008-10-03 17:40:32 +03001369EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001370
1371struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1372{
1373 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001374 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001375}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376
Izik Eiduse0d62c72007-10-24 23:57:46 +02001377int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1378{
1379 int i;
1380
1381 gfn = unalias_gfn(kvm, gfn);
1382 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1383 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1384
1385 if (gfn >= memslot->base_gfn
1386 && gfn < memslot->base_gfn + memslot->npages)
1387 return 1;
1388 }
1389 return 0;
1390}
1391EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1392
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001393unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001394{
1395 struct kvm_memory_slot *slot;
1396
1397 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001398 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001399 if (!slot)
1400 return bad_hva();
1401 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1402}
Sheng Yang0d150292008-04-25 21:44:50 +08001403EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001404
Anthony Liguori35149e22008-04-02 14:46:56 -05001405pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001406{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001407 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001408 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001409 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001410 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001411
Avi Kivity60395222007-10-21 11:03:36 +02001412 might_sleep();
1413
Izik Eidus539cb662007-11-11 22:05:04 +02001414 addr = gfn_to_hva(kvm, gfn);
1415 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001416 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001417 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001418 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001419
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001420 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001421
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001422 if (unlikely(npages != 1)) {
1423 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001424
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001425 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001426 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001427
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001428 if (vma == NULL || addr < vma->vm_start ||
1429 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001430 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001431 get_page(bad_page);
1432 return page_to_pfn(bad_page);
1433 }
1434
1435 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001436 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001437 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001438 } else
1439 pfn = page_to_pfn(page[0]);
1440
1441 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001442}
1443
1444EXPORT_SYMBOL_GPL(gfn_to_pfn);
1445
1446struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1447{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001448 pfn_t pfn;
1449
1450 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001451 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001452 return pfn_to_page(pfn);
1453
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001454 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001455
1456 get_page(bad_page);
1457 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001458}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001459
Avi Kivity954bbbc2007-03-30 14:02:32 +03001460EXPORT_SYMBOL_GPL(gfn_to_page);
1461
Izik Eidusb4231d62007-11-20 11:49:33 +02001462void kvm_release_page_clean(struct page *page)
1463{
Anthony Liguori35149e22008-04-02 14:46:56 -05001464 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001465}
1466EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1467
Anthony Liguori35149e22008-04-02 14:46:56 -05001468void kvm_release_pfn_clean(pfn_t pfn)
1469{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001470 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001471 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001472}
1473EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1474
Izik Eidusb4231d62007-11-20 11:49:33 +02001475void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001476{
Anthony Liguori35149e22008-04-02 14:46:56 -05001477 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001478}
Izik Eidusb4231d62007-11-20 11:49:33 +02001479EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001480
Anthony Liguori35149e22008-04-02 14:46:56 -05001481void kvm_release_pfn_dirty(pfn_t pfn)
1482{
1483 kvm_set_pfn_dirty(pfn);
1484 kvm_release_pfn_clean(pfn);
1485}
1486EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1487
1488void kvm_set_page_dirty(struct page *page)
1489{
1490 kvm_set_pfn_dirty(page_to_pfn(page));
1491}
1492EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1493
1494void kvm_set_pfn_dirty(pfn_t pfn)
1495{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001496 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001497 struct page *page = pfn_to_page(pfn);
1498 if (!PageReserved(page))
1499 SetPageDirty(page);
1500 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001501}
1502EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1503
1504void kvm_set_pfn_accessed(pfn_t pfn)
1505{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001506 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001507 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001508}
1509EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1510
1511void kvm_get_pfn(pfn_t pfn)
1512{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001513 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001514 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001515}
1516EXPORT_SYMBOL_GPL(kvm_get_pfn);
1517
Izik Eidus195aefd2007-10-01 22:14:18 +02001518static int next_segment(unsigned long len, int offset)
1519{
1520 if (len > PAGE_SIZE - offset)
1521 return PAGE_SIZE - offset;
1522 else
1523 return len;
1524}
1525
1526int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1527 int len)
1528{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001529 int r;
1530 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001531
Izik Eiduse0506bc2007-11-11 22:10:22 +02001532 addr = gfn_to_hva(kvm, gfn);
1533 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001534 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001535 r = copy_from_user(data, (void __user *)addr + offset, len);
1536 if (r)
1537 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001538 return 0;
1539}
1540EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1541
1542int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1543{
1544 gfn_t gfn = gpa >> PAGE_SHIFT;
1545 int seg;
1546 int offset = offset_in_page(gpa);
1547 int ret;
1548
1549 while ((seg = next_segment(len, offset)) != 0) {
1550 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1551 if (ret < 0)
1552 return ret;
1553 offset = 0;
1554 len -= seg;
1555 data += seg;
1556 ++gfn;
1557 }
1558 return 0;
1559}
1560EXPORT_SYMBOL_GPL(kvm_read_guest);
1561
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001562int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1563 unsigned long len)
1564{
1565 int r;
1566 unsigned long addr;
1567 gfn_t gfn = gpa >> PAGE_SHIFT;
1568 int offset = offset_in_page(gpa);
1569
1570 addr = gfn_to_hva(kvm, gfn);
1571 if (kvm_is_error_hva(addr))
1572 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001573 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001574 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001575 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001576 if (r)
1577 return -EFAULT;
1578 return 0;
1579}
1580EXPORT_SYMBOL(kvm_read_guest_atomic);
1581
Izik Eidus195aefd2007-10-01 22:14:18 +02001582int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1583 int offset, int len)
1584{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001585 int r;
1586 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001587
Izik Eiduse0506bc2007-11-11 22:10:22 +02001588 addr = gfn_to_hva(kvm, gfn);
1589 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001590 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001591 r = copy_to_user((void __user *)addr + offset, data, len);
1592 if (r)
1593 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001594 mark_page_dirty(kvm, gfn);
1595 return 0;
1596}
1597EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1598
1599int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1600 unsigned long len)
1601{
1602 gfn_t gfn = gpa >> PAGE_SHIFT;
1603 int seg;
1604 int offset = offset_in_page(gpa);
1605 int ret;
1606
1607 while ((seg = next_segment(len, offset)) != 0) {
1608 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1609 if (ret < 0)
1610 return ret;
1611 offset = 0;
1612 len -= seg;
1613 data += seg;
1614 ++gfn;
1615 }
1616 return 0;
1617}
1618
1619int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1620{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001621 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001622}
1623EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1624
1625int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1626{
1627 gfn_t gfn = gpa >> PAGE_SHIFT;
1628 int seg;
1629 int offset = offset_in_page(gpa);
1630 int ret;
1631
1632 while ((seg = next_segment(len, offset)) != 0) {
1633 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1634 if (ret < 0)
1635 return ret;
1636 offset = 0;
1637 len -= seg;
1638 ++gfn;
1639 }
1640 return 0;
1641}
1642EXPORT_SYMBOL_GPL(kvm_clear_guest);
1643
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1645{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001646 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647
Uri Lublin3b6fff12007-10-30 10:42:09 +02001648 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001649 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001650 if (memslot && memslot->dirty_bitmap) {
1651 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652
Rusty Russell7e9d6192007-07-31 20:41:14 +10001653 /* avoid RMW */
1654 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1655 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 }
1657}
1658
Eddie Dongb6958ce2007-07-18 12:15:21 +03001659/*
1660 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1661 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001662void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001663{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001664 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001665
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001666 for (;;) {
1667 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001668
Gleb Natapov78646122009-03-23 12:12:11 +02001669 if ((kvm_arch_interrupt_allowed(vcpu) &&
1670 kvm_cpu_has_interrupt(vcpu)) ||
Gleb Natapov09cec752009-03-23 15:11:44 +02001671 kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001672 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001673 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001674 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001675 if (kvm_cpu_has_pending_timer(vcpu))
1676 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001677 if (signal_pending(current))
1678 break;
1679
Eddie Dongb6958ce2007-07-18 12:15:21 +03001680 vcpu_put(vcpu);
1681 schedule();
1682 vcpu_load(vcpu);
1683 }
1684
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001685 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001686}
1687
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688void kvm_resched(struct kvm_vcpu *vcpu)
1689{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001690 if (!need_resched())
1691 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693}
1694EXPORT_SYMBOL_GPL(kvm_resched);
1695
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001696static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001697{
1698 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001699 struct page *page;
1700
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001701 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001702 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001703#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001704 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001705 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001706#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001707#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1708 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1709 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1710#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001711 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001712 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001713 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001714 vmf->page = page;
1715 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001716}
1717
1718static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001719 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001720};
1721
1722static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1723{
1724 vma->vm_ops = &kvm_vcpu_vm_ops;
1725 return 0;
1726}
1727
Avi Kivitybccf2152007-02-21 18:04:26 +02001728static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1729{
1730 struct kvm_vcpu *vcpu = filp->private_data;
1731
Al Viro66c0b392008-04-19 20:33:56 +01001732 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001733 return 0;
1734}
1735
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001736static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001737 .release = kvm_vcpu_release,
1738 .unlocked_ioctl = kvm_vcpu_ioctl,
1739 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001740 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001741};
1742
1743/*
1744 * Allocates an inode for the vcpu.
1745 */
1746static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1747{
Gleb Natapov73880c82009-06-09 15:56:28 +03001748 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Avi Kivitybccf2152007-02-21 18:04:26 +02001749}
1750
Avi Kivityc5ea7662007-02-20 18:41:05 +02001751/*
1752 * Creates some virtual cpus. Good luck creating more than one.
1753 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001754static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001755{
1756 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001757 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001758
Gleb Natapov73880c82009-06-09 15:56:28 +03001759 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001760 if (IS_ERR(vcpu))
1761 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001762
Avi Kivity15ad7142007-07-11 18:17:21 +03001763 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1764
Avi Kivity26e52152007-11-20 15:30:24 +02001765 r = kvm_arch_vcpu_setup(vcpu);
1766 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001767 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001768
Shaohua Li11ec2802007-07-23 14:51:37 +08001769 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001770 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1771 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001772 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001773 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001774
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001775 kvm_for_each_vcpu(r, v, kvm)
1776 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001777 r = -EEXIST;
1778 goto vcpu_destroy;
1779 }
1780
1781 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001782
1783 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001784 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001785 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001786 if (r < 0) {
1787 kvm_put_kvm(kvm);
1788 goto vcpu_destroy;
1789 }
1790
1791 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1792 smp_wmb();
1793 atomic_inc(&kvm->online_vcpus);
1794
1795#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1796 if (kvm->bsp_vcpu_id == id)
1797 kvm->bsp_vcpu = vcpu;
1798#endif
1799 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001800 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001801
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001802vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001803 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001804 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001805 return r;
1806}
1807
Avi Kivity1961d272007-03-05 19:46:05 +02001808static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1809{
1810 if (sigset) {
1811 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1812 vcpu->sigset_active = 1;
1813 vcpu->sigset = *sigset;
1814 } else
1815 vcpu->sigset_active = 0;
1816 return 0;
1817}
1818
Sheng Yangc1e01512009-02-25 17:22:26 +08001819#ifdef __KVM_HAVE_MSIX
1820static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1821 struct kvm_assigned_msix_nr *entry_nr)
1822{
1823 int r = 0;
1824 struct kvm_assigned_dev_kernel *adev;
1825
1826 mutex_lock(&kvm->lock);
1827
1828 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1829 entry_nr->assigned_dev_id);
1830 if (!adev) {
1831 r = -EINVAL;
1832 goto msix_nr_out;
1833 }
1834
1835 if (adev->entries_nr == 0) {
1836 adev->entries_nr = entry_nr->entry_nr;
1837 if (adev->entries_nr == 0 ||
1838 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1839 r = -EINVAL;
1840 goto msix_nr_out;
1841 }
1842
1843 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1844 entry_nr->entry_nr,
1845 GFP_KERNEL);
1846 if (!adev->host_msix_entries) {
1847 r = -ENOMEM;
1848 goto msix_nr_out;
1849 }
1850 adev->guest_msix_entries = kzalloc(
1851 sizeof(struct kvm_guest_msix_entry) *
1852 entry_nr->entry_nr, GFP_KERNEL);
1853 if (!adev->guest_msix_entries) {
1854 kfree(adev->host_msix_entries);
1855 r = -ENOMEM;
1856 goto msix_nr_out;
1857 }
1858 } else /* Not allowed set MSI-X number twice */
1859 r = -EINVAL;
1860msix_nr_out:
1861 mutex_unlock(&kvm->lock);
1862 return r;
1863}
1864
1865static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1866 struct kvm_assigned_msix_entry *entry)
1867{
1868 int r = 0, i;
1869 struct kvm_assigned_dev_kernel *adev;
1870
1871 mutex_lock(&kvm->lock);
1872
1873 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1874 entry->assigned_dev_id);
1875
1876 if (!adev) {
1877 r = -EINVAL;
1878 goto msix_entry_out;
1879 }
1880
1881 for (i = 0; i < adev->entries_nr; i++)
1882 if (adev->guest_msix_entries[i].vector == 0 ||
1883 adev->guest_msix_entries[i].entry == entry->entry) {
1884 adev->guest_msix_entries[i].entry = entry->entry;
1885 adev->guest_msix_entries[i].vector = entry->gsi;
1886 adev->host_msix_entries[i].entry = entry->entry;
1887 break;
1888 }
1889 if (i == adev->entries_nr) {
1890 r = -ENOSPC;
1891 goto msix_entry_out;
1892 }
1893
1894msix_entry_out:
1895 mutex_unlock(&kvm->lock);
1896
1897 return r;
1898}
1899#endif
1900
Avi Kivitybccf2152007-02-21 18:04:26 +02001901static long kvm_vcpu_ioctl(struct file *filp,
1902 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903{
Avi Kivitybccf2152007-02-21 18:04:26 +02001904 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001905 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001906 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001907 struct kvm_fpu *fpu = NULL;
1908 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001910 if (vcpu->kvm->mm != current->mm)
1911 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001913 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001914 r = -EINVAL;
1915 if (arg)
1916 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001917 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001920 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001922 r = -ENOMEM;
1923 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1924 if (!kvm_regs)
1925 goto out;
1926 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001928 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001930 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1931 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001933out_free1:
1934 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 break;
1936 }
1937 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001938 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001940 r = -ENOMEM;
1941 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1942 if (!kvm_regs)
1943 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001945 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1946 goto out_free2;
1947 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001949 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001951out_free2:
1952 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 break;
1954 }
1955 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001956 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1957 r = -ENOMEM;
1958 if (!kvm_sregs)
1959 goto out;
1960 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 if (r)
1962 goto out;
1963 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001964 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 goto out;
1966 r = 0;
1967 break;
1968 }
1969 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001970 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1971 r = -ENOMEM;
1972 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001974 r = -EFAULT;
1975 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1976 goto out;
1977 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 if (r)
1979 goto out;
1980 r = 0;
1981 break;
1982 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001983 case KVM_GET_MP_STATE: {
1984 struct kvm_mp_state mp_state;
1985
1986 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1987 if (r)
1988 goto out;
1989 r = -EFAULT;
1990 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1991 goto out;
1992 r = 0;
1993 break;
1994 }
1995 case KVM_SET_MP_STATE: {
1996 struct kvm_mp_state mp_state;
1997
1998 r = -EFAULT;
1999 if (copy_from_user(&mp_state, argp, sizeof mp_state))
2000 goto out;
2001 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
2002 if (r)
2003 goto out;
2004 r = 0;
2005 break;
2006 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002007 case KVM_TRANSLATE: {
2008 struct kvm_translation tr;
2009
2010 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002011 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08002013 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014 if (r)
2015 goto out;
2016 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002017 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 goto out;
2019 r = 0;
2020 break;
2021 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002022 case KVM_SET_GUEST_DEBUG: {
2023 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024
2025 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002026 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002028 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029 if (r)
2030 goto out;
2031 r = 0;
2032 break;
2033 }
Avi Kivity1961d272007-03-05 19:46:05 +02002034 case KVM_SET_SIGNAL_MASK: {
2035 struct kvm_signal_mask __user *sigmask_arg = argp;
2036 struct kvm_signal_mask kvm_sigmask;
2037 sigset_t sigset, *p;
2038
2039 p = NULL;
2040 if (argp) {
2041 r = -EFAULT;
2042 if (copy_from_user(&kvm_sigmask, argp,
2043 sizeof kvm_sigmask))
2044 goto out;
2045 r = -EINVAL;
2046 if (kvm_sigmask.len != sizeof sigset)
2047 goto out;
2048 r = -EFAULT;
2049 if (copy_from_user(&sigset, sigmask_arg->sigset,
2050 sizeof sigset))
2051 goto out;
2052 p = &sigset;
2053 }
2054 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2055 break;
2056 }
Avi Kivityb8836732007-04-01 16:34:31 +03002057 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002058 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2059 r = -ENOMEM;
2060 if (!fpu)
2061 goto out;
2062 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002063 if (r)
2064 goto out;
2065 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002066 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03002067 goto out;
2068 r = 0;
2069 break;
2070 }
2071 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002072 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2073 r = -ENOMEM;
2074 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002075 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002076 r = -EFAULT;
2077 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2078 goto out;
2079 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002080 if (r)
2081 goto out;
2082 r = 0;
2083 break;
2084 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002085 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002086 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002087 }
2088out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002089 kfree(fpu);
2090 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002091 return r;
2092}
2093
2094static long kvm_vm_ioctl(struct file *filp,
2095 unsigned int ioctl, unsigned long arg)
2096{
2097 struct kvm *kvm = filp->private_data;
2098 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002099 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002100
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002101 if (kvm->mm != current->mm)
2102 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002103 switch (ioctl) {
2104 case KVM_CREATE_VCPU:
2105 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2106 if (r < 0)
2107 goto out;
2108 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002109 case KVM_SET_USER_MEMORY_REGION: {
2110 struct kvm_userspace_memory_region kvm_userspace_mem;
2111
2112 r = -EFAULT;
2113 if (copy_from_user(&kvm_userspace_mem, argp,
2114 sizeof kvm_userspace_mem))
2115 goto out;
2116
2117 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 if (r)
2119 goto out;
2120 break;
2121 }
2122 case KVM_GET_DIRTY_LOG: {
2123 struct kvm_dirty_log log;
2124
2125 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002126 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002128 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129 if (r)
2130 goto out;
2131 break;
2132 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002133#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2134 case KVM_REGISTER_COALESCED_MMIO: {
2135 struct kvm_coalesced_mmio_zone zone;
2136 r = -EFAULT;
2137 if (copy_from_user(&zone, argp, sizeof zone))
2138 goto out;
2139 r = -ENXIO;
2140 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2141 if (r)
2142 goto out;
2143 r = 0;
2144 break;
2145 }
2146 case KVM_UNREGISTER_COALESCED_MMIO: {
2147 struct kvm_coalesced_mmio_zone zone;
2148 r = -EFAULT;
2149 if (copy_from_user(&zone, argp, sizeof zone))
2150 goto out;
2151 r = -ENXIO;
2152 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2153 if (r)
2154 goto out;
2155 r = 0;
2156 break;
2157 }
2158#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002159#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2160 case KVM_ASSIGN_PCI_DEVICE: {
2161 struct kvm_assigned_pci_dev assigned_dev;
2162
2163 r = -EFAULT;
2164 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2165 goto out;
2166 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2167 if (r)
2168 goto out;
2169 break;
2170 }
2171 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002172 r = -EOPNOTSUPP;
2173 break;
2174 }
2175#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2176 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002177 struct kvm_assigned_irq assigned_irq;
2178
2179 r = -EFAULT;
2180 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2181 goto out;
2182 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2183 if (r)
2184 goto out;
2185 break;
2186 }
Sheng Yange56d5322009-03-12 21:45:39 +08002187 case KVM_DEASSIGN_DEV_IRQ: {
2188 struct kvm_assigned_irq assigned_irq;
2189
2190 r = -EFAULT;
2191 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2192 goto out;
2193 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2194 if (r)
2195 goto out;
2196 break;
2197 }
2198#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002199#endif
Weidong Han0a920352008-12-02 21:24:23 +08002200#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2201 case KVM_DEASSIGN_PCI_DEVICE: {
2202 struct kvm_assigned_pci_dev assigned_dev;
2203
2204 r = -EFAULT;
2205 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2206 goto out;
2207 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2208 if (r)
2209 goto out;
2210 break;
2211 }
2212#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002213#ifdef KVM_CAP_IRQ_ROUTING
2214 case KVM_SET_GSI_ROUTING: {
2215 struct kvm_irq_routing routing;
2216 struct kvm_irq_routing __user *urouting;
2217 struct kvm_irq_routing_entry *entries;
2218
2219 r = -EFAULT;
2220 if (copy_from_user(&routing, argp, sizeof(routing)))
2221 goto out;
2222 r = -EINVAL;
2223 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2224 goto out;
2225 if (routing.flags)
2226 goto out;
2227 r = -ENOMEM;
2228 entries = vmalloc(routing.nr * sizeof(*entries));
2229 if (!entries)
2230 goto out;
2231 r = -EFAULT;
2232 urouting = argp;
2233 if (copy_from_user(entries, urouting->entries,
2234 routing.nr * sizeof(*entries)))
2235 goto out_free_irq_routing;
2236 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2237 routing.flags);
2238 out_free_irq_routing:
2239 vfree(entries);
2240 break;
2241 }
Sheng Yangc1e01512009-02-25 17:22:26 +08002242#ifdef __KVM_HAVE_MSIX
2243 case KVM_ASSIGN_SET_MSIX_NR: {
2244 struct kvm_assigned_msix_nr entry_nr;
2245 r = -EFAULT;
2246 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2247 goto out;
2248 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2249 if (r)
2250 goto out;
2251 break;
2252 }
2253 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2254 struct kvm_assigned_msix_entry entry;
2255 r = -EFAULT;
2256 if (copy_from_user(&entry, argp, sizeof entry))
2257 goto out;
2258 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2259 if (r)
2260 goto out;
2261 break;
2262 }
Avi Kivity399ec802008-11-19 13:58:46 +02002263#endif
Sheng Yangc1e01512009-02-25 17:22:26 +08002264#endif /* KVM_CAP_IRQ_ROUTING */
Gregory Haskins721eecbf2009-05-20 10:30:49 -04002265 case KVM_IRQFD: {
2266 struct kvm_irqfd data;
2267
2268 r = -EFAULT;
2269 if (copy_from_user(&data, argp, sizeof data))
2270 goto out;
2271 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2272 break;
2273 }
Gleb Natapov73880c82009-06-09 15:56:28 +03002274#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2275 case KVM_SET_BOOT_CPU_ID:
2276 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002277 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002278 if (atomic_read(&kvm->online_vcpus) != 0)
2279 r = -EBUSY;
2280 else
2281 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002282 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002283 break;
2284#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002285 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002286 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002287 }
2288out:
2289 return r;
2290}
2291
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002292static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002293{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002294 struct page *page[1];
2295 unsigned long addr;
2296 int npages;
2297 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002298 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002299
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002300 addr = gfn_to_hva(kvm, gfn);
2301 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002302 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002303
2304 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2305 NULL);
2306 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002307 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002308
2309 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002310 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002311}
2312
2313static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002314 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002315};
2316
2317static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2318{
2319 vma->vm_ops = &kvm_vm_vm_ops;
2320 return 0;
2321}
2322
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002323static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002324 .release = kvm_vm_release,
2325 .unlocked_ioctl = kvm_vm_ioctl,
2326 .compat_ioctl = kvm_vm_ioctl,
2327 .mmap = kvm_vm_mmap,
2328};
2329
2330static int kvm_dev_ioctl_create_vm(void)
2331{
Al Viro2030a422008-02-23 06:46:49 -05002332 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002333 struct kvm *kvm;
2334
Avi Kivityf17abe92007-02-21 19:28:04 +02002335 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002336 if (IS_ERR(kvm))
2337 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002338 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002339 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002340 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002341
Avi Kivityf17abe92007-02-21 19:28:04 +02002342 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002343}
2344
Avi Kivity1a811b62008-12-08 18:25:27 +02002345static long kvm_dev_ioctl_check_extension_generic(long arg)
2346{
2347 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002348 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002349 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002350 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002351#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2352 case KVM_CAP_SET_BOOT_CPU_ID:
2353#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002354 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002355#ifdef CONFIG_HAVE_KVM_IRQCHIP
2356 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002357 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002358#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002359 default:
2360 break;
2361 }
2362 return kvm_dev_ioctl_check_extension(arg);
2363}
2364
Avi Kivityf17abe92007-02-21 19:28:04 +02002365static long kvm_dev_ioctl(struct file *filp,
2366 unsigned int ioctl, unsigned long arg)
2367{
Avi Kivity07c45a32007-03-07 13:05:38 +02002368 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002369
2370 switch (ioctl) {
2371 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002372 r = -EINVAL;
2373 if (arg)
2374 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002375 r = KVM_API_VERSION;
2376 break;
2377 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002378 r = -EINVAL;
2379 if (arg)
2380 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002381 r = kvm_dev_ioctl_create_vm();
2382 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002383 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002384 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002385 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002386 case KVM_GET_VCPU_MMAP_SIZE:
2387 r = -EINVAL;
2388 if (arg)
2389 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002390 r = PAGE_SIZE; /* struct kvm_run */
2391#ifdef CONFIG_X86
2392 r += PAGE_SIZE; /* pio data page */
2393#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002394#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2395 r += PAGE_SIZE; /* coalesced mmio ring page */
2396#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002397 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002398 case KVM_TRACE_ENABLE:
2399 case KVM_TRACE_PAUSE:
2400 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002401 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002402 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002404 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 }
2406out:
2407 return r;
2408}
2409
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411 .unlocked_ioctl = kvm_dev_ioctl,
2412 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413};
2414
2415static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002416 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417 "kvm",
2418 &kvm_chardev_ops,
2419};
2420
Avi Kivity1b6c0162007-05-24 13:03:52 +03002421static void hardware_enable(void *junk)
2422{
2423 int cpu = raw_smp_processor_id();
2424
Rusty Russell7f59f492008-12-07 21:25:45 +10302425 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002426 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302427 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002428 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002429}
2430
2431static void hardware_disable(void *junk)
2432{
2433 int cpu = raw_smp_processor_id();
2434
Rusty Russell7f59f492008-12-07 21:25:45 +10302435 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002436 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302437 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002438 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002439}
2440
Avi Kivity774c47f2007-02-12 00:54:47 -08002441static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2442 void *v)
2443{
2444 int cpu = (long)v;
2445
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002446 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002447 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002448 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002449 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2450 cpu);
2451 hardware_disable(NULL);
2452 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002453 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002454 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2455 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002456 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002457 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002458 case CPU_ONLINE:
2459 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2460 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002461 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002462 break;
2463 }
2464 return NOTIFY_OK;
2465}
2466
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002467
2468asmlinkage void kvm_handle_fault_on_reboot(void)
2469{
2470 if (kvm_rebooting)
2471 /* spin while reset goes on */
2472 while (true)
2473 ;
2474 /* Fault while not rebooting. We want the trace. */
2475 BUG();
2476}
2477EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2478
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002479static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002480 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002481{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002482 /*
2483 * Some (well, at least mine) BIOSes hang on reboot if
2484 * in vmx root mode.
2485 *
2486 * And Intel TXT required VMX off for all cpu when system shutdown.
2487 */
2488 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2489 kvm_rebooting = true;
2490 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002491 return NOTIFY_OK;
2492}
2493
2494static struct notifier_block kvm_reboot_notifier = {
2495 .notifier_call = kvm_reboot,
2496 .priority = 0,
2497};
2498
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002499void kvm_io_bus_init(struct kvm_io_bus *bus)
2500{
2501 memset(bus, 0, sizeof(*bus));
2502}
2503
2504void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2505{
2506 int i;
2507
2508 for (i = 0; i < bus->dev_count; i++) {
2509 struct kvm_io_device *pos = bus->devs[i];
2510
2511 kvm_iodevice_destructor(pos);
2512 }
2513}
2514
Laurent Vivier92760492008-05-30 16:05:53 +02002515struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2516 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002517{
2518 int i;
2519
2520 for (i = 0; i < bus->dev_count; i++) {
2521 struct kvm_io_device *pos = bus->devs[i];
2522
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002523 if (kvm_iodevice_in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002524 return pos;
2525 }
2526
2527 return NULL;
2528}
2529
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002530void kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
2531 struct kvm_io_device *dev)
2532{
2533 down_write(&kvm->slots_lock);
2534 __kvm_io_bus_register_dev(bus, dev);
2535 up_write(&kvm->slots_lock);
2536}
2537
2538/* An unlocked version. Caller must have write lock on slots_lock. */
2539void __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
2540 struct kvm_io_device *dev)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002541{
2542 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2543
2544 bus->devs[bus->dev_count++] = dev;
2545}
2546
Avi Kivity774c47f2007-02-12 00:54:47 -08002547static struct notifier_block kvm_cpu_notifier = {
2548 .notifier_call = kvm_cpu_hotplug,
2549 .priority = 20, /* must be > scheduler priority */
2550};
2551
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002552static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002553{
2554 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002555 struct kvm *kvm;
2556
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002557 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002558 spin_lock(&kvm_lock);
2559 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002560 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002561 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002562 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002563}
2564
2565DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2566
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002567static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002568{
2569 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002570 struct kvm *kvm;
2571 struct kvm_vcpu *vcpu;
2572 int i;
2573
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002574 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002575 spin_lock(&kvm_lock);
2576 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002577 kvm_for_each_vcpu(i, vcpu, kvm)
2578 *val += *(u32 *)((void *)vcpu + offset);
2579
Avi Kivity1165f5f2007-04-19 17:27:43 +03002580 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002581 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002582}
2583
Avi Kivityba1389b2007-11-18 16:24:12 +02002584DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2585
2586static struct file_operations *stat_fops[] = {
2587 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2588 [KVM_STAT_VM] = &vm_stat_fops,
2589};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002590
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002591static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592{
2593 struct kvm_stats_debugfs_item *p;
2594
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002595 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002597 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002598 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002599 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600}
2601
2602static void kvm_exit_debug(void)
2603{
2604 struct kvm_stats_debugfs_item *p;
2605
2606 for (p = debugfs_entries; p->name; ++p)
2607 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002608 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609}
2610
Avi Kivity59ae6c62007-02-12 00:54:48 -08002611static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2612{
Avi Kivity4267c412007-05-24 13:09:41 +03002613 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002614 return 0;
2615}
2616
2617static int kvm_resume(struct sys_device *dev)
2618{
Avi Kivity4267c412007-05-24 13:09:41 +03002619 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002620 return 0;
2621}
2622
2623static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002624 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002625 .suspend = kvm_suspend,
2626 .resume = kvm_resume,
2627};
2628
2629static struct sys_device kvm_sysdev = {
2630 .id = 0,
2631 .cls = &kvm_sysdev_class,
2632};
2633
Izik Eiduscea7bb22007-10-17 19:17:48 +02002634struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002635pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636
Avi Kivity15ad7142007-07-11 18:17:21 +03002637static inline
2638struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2639{
2640 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2641}
2642
2643static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2644{
2645 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2646
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002647 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002648}
2649
2650static void kvm_sched_out(struct preempt_notifier *pn,
2651 struct task_struct *next)
2652{
2653 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2654
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002655 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002656}
2657
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002658int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002659 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660{
2661 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002662 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002664 kvm_init_debug();
2665
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002666 r = kvm_arch_init(opaque);
2667 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002668 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002669
2670 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2671
2672 if (bad_page == NULL) {
2673 r = -ENOMEM;
2674 goto out;
2675 }
2676
Anthony Liguori35149e22008-04-02 14:46:56 -05002677 bad_pfn = page_to_pfn(bad_page);
2678
Avi Kivity8437a6172009-06-06 14:52:35 -07002679 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302680 r = -ENOMEM;
2681 goto out_free_0;
2682 }
2683
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002684 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302686 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687
Yang, Sheng002c7f72007-07-31 14:23:01 +03002688 for_each_online_cpu(cpu) {
2689 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002690 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002691 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002692 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002693 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002694 }
2695
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002696 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002697 r = register_cpu_notifier(&kvm_cpu_notifier);
2698 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002699 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 register_reboot_notifier(&kvm_reboot_notifier);
2701
Avi Kivity59ae6c62007-02-12 00:54:48 -08002702 r = sysdev_class_register(&kvm_sysdev_class);
2703 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002704 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002705
2706 r = sysdev_register(&kvm_sysdev);
2707 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002708 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002709
Rusty Russellc16f8622007-07-30 21:12:19 +10002710 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2711 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002712 __alignof__(struct kvm_vcpu),
2713 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002714 if (!kvm_vcpu_cache) {
2715 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002716 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002717 }
2718
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002720 kvm_vm_fops.owner = module;
2721 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722
2723 r = misc_register(&kvm_dev);
2724 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002725 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 goto out_free;
2727 }
2728
Avi Kivity15ad7142007-07-11 18:17:21 +03002729 kvm_preempt_ops.sched_in = kvm_sched_in;
2730 kvm_preempt_ops.sched_out = kvm_sched_out;
2731
Avi Kivityc7addb92007-09-16 18:58:32 +02002732 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733
2734out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002735 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002736out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002737 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002738out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002739 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002740out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002742 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002743out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002744 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002745out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002746 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302747out_free_0a:
2748 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002749out_free_0:
2750 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002751out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002752 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002753 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002754out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 return r;
2756}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002757EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002759void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002761 tracepoint_synchronize_unregister();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002763 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002764 sysdev_unregister(&kvm_sysdev);
2765 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002767 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002768 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002769 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002770 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302772 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002773 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002775EXPORT_SYMBOL_GPL(kvm_exit);