blob: 48d5e697bf44b34b7966aa92ead3f3b6b6429a60 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Hollis Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/io.h>
49#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020050#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Laurent Vivier5f94c172008-05-30 16:05:54 +020052#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
53#include "coalesced_mmio.h"
54#endif
55
Xiantao Zhang8a98f662008-10-06 13:47:38 +080056#ifdef KVM_CAP_DEVICE_ASSIGNMENT
57#include <linux/pci.h>
58#include <linux/interrupt.h>
59#include "irq.h"
60#endif
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062MODULE_AUTHOR("Qumranet");
63MODULE_LICENSE("GPL");
64
Marcelo Tosattifa40a822009-06-04 15:08:24 -030065/*
66 * Ordering of locks:
67 *
68 * kvm->lock --> kvm->irq_lock
69 */
70
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080071DEFINE_SPINLOCK(kvm_lock);
72LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080073
Rusty Russell7f59f492008-12-07 21:25:45 +103074static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030075
Rusty Russellc16f8622007-07-30 21:12:19 +100076struct kmem_cache *kvm_vcpu_cache;
77EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030078
Avi Kivity15ad7142007-07-11 18:17:21 +030079static __read_mostly struct preempt_ops kvm_preempt_ops;
80
Hollis Blanchard76f7c872008-04-15 16:05:42 -050081struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080082
Avi Kivitybccf2152007-02-21 18:04:26 +020083static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
84 unsigned long arg);
85
Hannes Edere8ba5d32008-11-28 17:02:42 +010086static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030087
Marcelo Tosatti54dee992009-06-11 12:07:44 -030088static bool largepages_enabled = true;
89
Xiantao Zhang8a98f662008-10-06 13:47:38 +080090#ifdef KVM_CAP_DEVICE_ASSIGNMENT
91static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
92 int assigned_dev_id)
93{
94 struct list_head *ptr;
95 struct kvm_assigned_dev_kernel *match;
96
97 list_for_each(ptr, head) {
98 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
99 if (match->assigned_dev_id == assigned_dev_id)
100 return match;
101 }
102 return NULL;
103}
104
Sheng Yang2350bd12009-02-25 17:22:27 +0800105static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
106 *assigned_dev, int irq)
107{
108 int i, index;
109 struct msix_entry *host_msix_entries;
110
111 host_msix_entries = assigned_dev->host_msix_entries;
112
113 index = -1;
114 for (i = 0; i < assigned_dev->entries_nr; i++)
115 if (irq == host_msix_entries[i].vector) {
116 index = i;
117 break;
118 }
119 if (index < 0) {
120 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
121 return 0;
122 }
123
124 return index;
125}
126
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800127static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
128{
129 struct kvm_assigned_dev_kernel *assigned_dev;
Sheng Yang2350bd12009-02-25 17:22:27 +0800130 struct kvm *kvm;
Sheng Yang968a6342009-04-30 10:58:42 +0800131 int i;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800132
133 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
134 interrupt_work);
Sheng Yang2350bd12009-02-25 17:22:27 +0800135 kvm = assigned_dev->kvm;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800136
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300137 mutex_lock(&kvm->irq_lock);
Marcelo Tosatti547de292009-05-07 17:55:13 -0300138 spin_lock_irq(&assigned_dev->assigned_dev_lock);
Sheng Yange56d5322009-03-12 21:45:39 +0800139 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800140 struct kvm_guest_msix_entry *guest_entries =
141 assigned_dev->guest_msix_entries;
142 for (i = 0; i < assigned_dev->entries_nr; i++) {
143 if (!(guest_entries[i].flags &
144 KVM_ASSIGNED_MSIX_PENDING))
145 continue;
146 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
147 kvm_set_irq(assigned_dev->kvm,
148 assigned_dev->irq_source_id,
149 guest_entries[i].vector, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800150 }
Sheng Yang968a6342009-04-30 10:58:42 +0800151 } else
Sheng Yang2350bd12009-02-25 17:22:27 +0800152 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
153 assigned_dev->guest_irq, 1);
Sheng Yang2350bd12009-02-25 17:22:27 +0800154
Marcelo Tosatti547de292009-05-07 17:55:13 -0300155 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300156 mutex_unlock(&assigned_dev->kvm->irq_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800157}
158
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800159static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
160{
Marcelo Tosatti547de292009-05-07 17:55:13 -0300161 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800162 struct kvm_assigned_dev_kernel *assigned_dev =
163 (struct kvm_assigned_dev_kernel *) dev_id;
164
Marcelo Tosatti547de292009-05-07 17:55:13 -0300165 spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
Sheng Yange56d5322009-03-12 21:45:39 +0800166 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yang2350bd12009-02-25 17:22:27 +0800167 int index = find_index_from_host_irq(assigned_dev, irq);
168 if (index < 0)
Marcelo Tosatti547de292009-05-07 17:55:13 -0300169 goto out;
Sheng Yang2350bd12009-02-25 17:22:27 +0800170 assigned_dev->guest_msix_entries[index].flags |=
171 KVM_ASSIGNED_MSIX_PENDING;
172 }
173
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800174 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000175
Sheng Yang968a6342009-04-30 10:58:42 +0800176 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
177 disable_irq_nosync(irq);
178 assigned_dev->host_irq_disabled = true;
179 }
Mark McLoughlindefaf152008-12-02 12:16:33 +0000180
Marcelo Tosatti547de292009-05-07 17:55:13 -0300181out:
182 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800183 return IRQ_HANDLED;
184}
185
186/* Ack the irq line for an assigned device */
187static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
188{
189 struct kvm_assigned_dev_kernel *dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300190 unsigned long flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800191
192 if (kian->gsi == -1)
193 return;
194
195 dev = container_of(kian, struct kvm_assigned_dev_kernel,
196 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000197
Sheng Yang5550af42008-10-15 20:15:06 +0800198 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000199
200 /* The guest irq may be shared so this ack may be
201 * from another device.
202 */
Marcelo Tosatti547de292009-05-07 17:55:13 -0300203 spin_lock_irqsave(&dev->assigned_dev_lock, flags);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000204 if (dev->host_irq_disabled) {
205 enable_irq(dev->host_irq);
206 dev->host_irq_disabled = false;
207 }
Marcelo Tosatti547de292009-05-07 17:55:13 -0300208 spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800209}
210
Sheng Yange56d5322009-03-12 21:45:39 +0800211static void deassign_guest_irq(struct kvm *kvm,
212 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800213{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300214 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
Sheng Yange56d5322009-03-12 21:45:39 +0800215 assigned_dev->ack_notifier.gsi = -1;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000216
217 if (assigned_dev->irq_source_id != -1)
218 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
219 assigned_dev->irq_source_id = -1;
Sheng Yange56d5322009-03-12 21:45:39 +0800220 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
221}
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800222
Sheng Yange56d5322009-03-12 21:45:39 +0800223/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
224static void deassign_host_irq(struct kvm *kvm,
225 struct kvm_assigned_dev_kernel *assigned_dev)
226{
Sheng Yangba4cef32009-01-06 10:03:03 +0800227 /*
228 * In kvm_free_device_irq, cancel_work_sync return true if:
229 * 1. work is scheduled, and then cancelled.
230 * 2. work callback is executed.
231 *
232 * The first one ensured that the irq is disabled and no more events
233 * would happen. But for the second one, the irq may be enabled (e.g.
234 * for MSI). So we disable irq here to prevent further events.
235 *
236 * Notice this maybe result in nested disable if the interrupt type is
237 * INTx, but it's OK for we are going to free it.
238 *
239 * If this function is a part of VM destroy, please ensure that till
240 * now, the kvm state is still legal for probably we also have to wait
241 * interrupt_work done.
242 */
Sheng Yange56d5322009-03-12 21:45:39 +0800243 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Sheng Yangd510d6c2009-02-25 17:22:28 +0800244 int i;
245 for (i = 0; i < assigned_dev->entries_nr; i++)
246 disable_irq_nosync(assigned_dev->
247 host_msix_entries[i].vector);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800248
Sheng Yangd510d6c2009-02-25 17:22:28 +0800249 cancel_work_sync(&assigned_dev->interrupt_work);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000250
Sheng Yangd510d6c2009-02-25 17:22:28 +0800251 for (i = 0; i < assigned_dev->entries_nr; i++)
252 free_irq(assigned_dev->host_msix_entries[i].vector,
253 (void *)assigned_dev);
254
255 assigned_dev->entries_nr = 0;
256 kfree(assigned_dev->host_msix_entries);
257 kfree(assigned_dev->guest_msix_entries);
258 pci_disable_msix(assigned_dev->dev);
259 } else {
260 /* Deal with MSI and INTx */
261 disable_irq_nosync(assigned_dev->host_irq);
262 cancel_work_sync(&assigned_dev->interrupt_work);
263
264 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
265
Sheng Yange56d5322009-03-12 21:45:39 +0800266 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800267 pci_disable_msi(assigned_dev->dev);
268 }
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000269
Sheng Yange56d5322009-03-12 21:45:39 +0800270 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000271}
272
Sheng Yange56d5322009-03-12 21:45:39 +0800273static int kvm_deassign_irq(struct kvm *kvm,
274 struct kvm_assigned_dev_kernel *assigned_dev,
275 unsigned long irq_requested_type)
276{
277 unsigned long guest_irq_type, host_irq_type;
278
279 if (!irqchip_in_kernel(kvm))
280 return -EINVAL;
281 /* no irq assignment to deassign */
282 if (!assigned_dev->irq_requested_type)
283 return -ENXIO;
284
285 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
286 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
287
288 if (host_irq_type)
289 deassign_host_irq(kvm, assigned_dev);
290 if (guest_irq_type)
291 deassign_guest_irq(kvm, assigned_dev);
292
293 return 0;
294}
295
296static void kvm_free_assigned_irq(struct kvm *kvm,
297 struct kvm_assigned_dev_kernel *assigned_dev)
298{
299 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
300}
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000301
302static void kvm_free_assigned_device(struct kvm *kvm,
303 struct kvm_assigned_dev_kernel
304 *assigned_dev)
305{
306 kvm_free_assigned_irq(kvm, assigned_dev);
307
Sheng Yang6eb55812008-10-31 12:37:41 +0800308 pci_reset_function(assigned_dev->dev);
309
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800310 pci_release_regions(assigned_dev->dev);
311 pci_disable_device(assigned_dev->dev);
312 pci_dev_put(assigned_dev->dev);
313
314 list_del(&assigned_dev->list);
315 kfree(assigned_dev);
316}
317
318void kvm_free_all_assigned_devices(struct kvm *kvm)
319{
320 struct list_head *ptr, *ptr2;
321 struct kvm_assigned_dev_kernel *assigned_dev;
322
323 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
324 assigned_dev = list_entry(ptr,
325 struct kvm_assigned_dev_kernel,
326 list);
327
328 kvm_free_assigned_device(kvm, assigned_dev);
329 }
330}
331
Sheng Yange56d5322009-03-12 21:45:39 +0800332static int assigned_device_enable_host_intx(struct kvm *kvm,
333 struct kvm_assigned_dev_kernel *dev)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800334{
Sheng Yange56d5322009-03-12 21:45:39 +0800335 dev->host_irq = dev->dev->irq;
336 /* Even though this is PCI, we don't want to use shared
337 * interrupts. Sharing host devices with guest-assigned devices
338 * on the same interrupt line is not a happy situation: there
339 * are going to be long delays in accepting, acking, etc.
340 */
341 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
342 0, "kvm_assigned_intx_device", (void *)dev))
343 return -EIO;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800344 return 0;
345}
346
Sheng Yange56d5322009-03-12 21:45:39 +0800347#ifdef __KVM_HAVE_MSI
348static int assigned_device_enable_host_msi(struct kvm *kvm,
349 struct kvm_assigned_dev_kernel *dev)
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800350{
351 int r;
352
Sheng Yange56d5322009-03-12 21:45:39 +0800353 if (!dev->dev->msi_enabled) {
354 r = pci_enable_msi(dev->dev);
355 if (r)
356 return r;
Sheng Yang5319c662008-11-24 14:32:57 +0800357 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800358
Sheng Yange56d5322009-03-12 21:45:39 +0800359 dev->host_irq = dev->dev->irq;
360 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
361 "kvm_assigned_msi_device", (void *)dev)) {
362 pci_disable_msi(dev->dev);
363 return -EIO;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800364 }
365
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800366 return 0;
367}
368#endif
369
Sheng Yangd510d6c2009-02-25 17:22:28 +0800370#ifdef __KVM_HAVE_MSIX
Sheng Yange56d5322009-03-12 21:45:39 +0800371static int assigned_device_enable_host_msix(struct kvm *kvm,
372 struct kvm_assigned_dev_kernel *dev)
Sheng Yangd510d6c2009-02-25 17:22:28 +0800373{
Sheng Yange56d5322009-03-12 21:45:39 +0800374 int i, r = -EINVAL;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800375
Sheng Yange56d5322009-03-12 21:45:39 +0800376 /* host_msix_entries and guest_msix_entries should have been
377 * initialized */
378 if (dev->entries_nr == 0)
379 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800380
Sheng Yange56d5322009-03-12 21:45:39 +0800381 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
382 if (r)
383 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800384
Sheng Yange56d5322009-03-12 21:45:39 +0800385 for (i = 0; i < dev->entries_nr; i++) {
386 r = request_irq(dev->host_msix_entries[i].vector,
387 kvm_assigned_dev_intr, 0,
388 "kvm_assigned_msix_device",
389 (void *)dev);
390 /* FIXME: free requested_irq's on failure */
Sheng Yangd510d6c2009-02-25 17:22:28 +0800391 if (r)
392 return r;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800393 }
394
Sheng Yange56d5322009-03-12 21:45:39 +0800395 return 0;
396}
Sheng Yangd510d6c2009-02-25 17:22:28 +0800397
Sheng Yange56d5322009-03-12 21:45:39 +0800398#endif
399
400static int assigned_device_enable_guest_intx(struct kvm *kvm,
401 struct kvm_assigned_dev_kernel *dev,
402 struct kvm_assigned_irq *irq)
403{
404 dev->guest_irq = irq->guest_irq;
405 dev->ack_notifier.gsi = irq->guest_irq;
406 return 0;
407}
408
409#ifdef __KVM_HAVE_MSI
410static int assigned_device_enable_guest_msi(struct kvm *kvm,
411 struct kvm_assigned_dev_kernel *dev,
412 struct kvm_assigned_irq *irq)
413{
414 dev->guest_irq = irq->guest_irq;
415 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800416 dev->host_irq_disabled = false;
Sheng Yange56d5322009-03-12 21:45:39 +0800417 return 0;
418}
419#endif
420#ifdef __KVM_HAVE_MSIX
421static int assigned_device_enable_guest_msix(struct kvm *kvm,
422 struct kvm_assigned_dev_kernel *dev,
423 struct kvm_assigned_irq *irq)
424{
425 dev->guest_irq = irq->guest_irq;
426 dev->ack_notifier.gsi = -1;
Sheng Yang968a6342009-04-30 10:58:42 +0800427 dev->host_irq_disabled = false;
Sheng Yangd510d6c2009-02-25 17:22:28 +0800428 return 0;
429}
430#endif
431
Sheng Yange56d5322009-03-12 21:45:39 +0800432static int assign_host_irq(struct kvm *kvm,
433 struct kvm_assigned_dev_kernel *dev,
434 __u32 host_irq_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800435{
Sheng Yange56d5322009-03-12 21:45:39 +0800436 int r = -EEXIST;
437
438 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
439 return r;
440
441 switch (host_irq_type) {
442 case KVM_DEV_IRQ_HOST_INTX:
443 r = assigned_device_enable_host_intx(kvm, dev);
444 break;
445#ifdef __KVM_HAVE_MSI
446 case KVM_DEV_IRQ_HOST_MSI:
447 r = assigned_device_enable_host_msi(kvm, dev);
448 break;
449#endif
450#ifdef __KVM_HAVE_MSIX
451 case KVM_DEV_IRQ_HOST_MSIX:
452 r = assigned_device_enable_host_msix(kvm, dev);
453 break;
454#endif
455 default:
456 r = -EINVAL;
457 }
458
459 if (!r)
460 dev->irq_requested_type |= host_irq_type;
461
462 return r;
463}
464
465static int assign_guest_irq(struct kvm *kvm,
466 struct kvm_assigned_dev_kernel *dev,
467 struct kvm_assigned_irq *irq,
468 unsigned long guest_irq_type)
469{
470 int id;
471 int r = -EEXIST;
472
473 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
474 return r;
475
476 id = kvm_request_irq_source_id(kvm);
477 if (id < 0)
478 return id;
479
480 dev->irq_source_id = id;
481
482 switch (guest_irq_type) {
483 case KVM_DEV_IRQ_GUEST_INTX:
484 r = assigned_device_enable_guest_intx(kvm, dev, irq);
485 break;
486#ifdef __KVM_HAVE_MSI
487 case KVM_DEV_IRQ_GUEST_MSI:
488 r = assigned_device_enable_guest_msi(kvm, dev, irq);
489 break;
490#endif
491#ifdef __KVM_HAVE_MSIX
492 case KVM_DEV_IRQ_GUEST_MSIX:
493 r = assigned_device_enable_guest_msix(kvm, dev, irq);
494 break;
495#endif
496 default:
497 r = -EINVAL;
498 }
499
500 if (!r) {
501 dev->irq_requested_type |= guest_irq_type;
502 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
503 } else
504 kvm_free_irq_source_id(kvm, dev->irq_source_id);
505
506 return r;
507}
508
509/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
510static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
511 struct kvm_assigned_irq *assigned_irq)
512{
513 int r = -EINVAL;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800514 struct kvm_assigned_dev_kernel *match;
Sheng Yange56d5322009-03-12 21:45:39 +0800515 unsigned long host_irq_type, guest_irq_type;
516
517 if (!capable(CAP_SYS_RAWIO))
518 return -EPERM;
519
520 if (!irqchip_in_kernel(kvm))
521 return r;
522
523 mutex_lock(&kvm->lock);
524 r = -ENODEV;
525 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
526 assigned_irq->assigned_dev_id);
527 if (!match)
528 goto out;
529
530 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
531 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
532
533 r = -EINVAL;
534 /* can only assign one type at a time */
535 if (hweight_long(host_irq_type) > 1)
536 goto out;
537 if (hweight_long(guest_irq_type) > 1)
538 goto out;
539 if (host_irq_type == 0 && guest_irq_type == 0)
540 goto out;
541
542 r = 0;
543 if (host_irq_type)
544 r = assign_host_irq(kvm, match, host_irq_type);
545 if (r)
546 goto out;
547
548 if (guest_irq_type)
549 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
550out:
551 mutex_unlock(&kvm->lock);
552 return r;
553}
554
555static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
556 struct kvm_assigned_irq
557 *assigned_irq)
558{
559 int r = -ENODEV;
560 struct kvm_assigned_dev_kernel *match;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800561
562 mutex_lock(&kvm->lock);
563
564 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
565 assigned_irq->assigned_dev_id);
Sheng Yange56d5322009-03-12 21:45:39 +0800566 if (!match)
567 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800568
Sheng Yange56d5322009-03-12 21:45:39 +0800569 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
570out:
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800571 mutex_unlock(&kvm->lock);
572 return r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800573}
574
575static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
576 struct kvm_assigned_pci_dev *assigned_dev)
577{
578 int r = 0;
579 struct kvm_assigned_dev_kernel *match;
580 struct pci_dev *dev;
581
Mark McLoughlin682edb42009-02-05 18:23:46 +0000582 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800583 mutex_lock(&kvm->lock);
584
585 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
586 assigned_dev->assigned_dev_id);
587 if (match) {
588 /* device already assigned */
Sheng Yange56d5322009-03-12 21:45:39 +0800589 r = -EEXIST;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800590 goto out;
591 }
592
593 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
594 if (match == NULL) {
595 printk(KERN_INFO "%s: Couldn't allocate memory\n",
596 __func__);
597 r = -ENOMEM;
598 goto out;
599 }
600 dev = pci_get_bus_and_slot(assigned_dev->busnr,
601 assigned_dev->devfn);
602 if (!dev) {
603 printk(KERN_INFO "%s: host device not found\n", __func__);
604 r = -EINVAL;
605 goto out_free;
606 }
607 if (pci_enable_device(dev)) {
608 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
609 r = -EBUSY;
610 goto out_put;
611 }
612 r = pci_request_regions(dev, "kvm_assigned_device");
613 if (r) {
614 printk(KERN_INFO "%s: Could not get access to device regions\n",
615 __func__);
616 goto out_disable;
617 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800618
619 pci_reset_function(dev);
620
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800621 match->assigned_dev_id = assigned_dev->assigned_dev_id;
622 match->host_busnr = assigned_dev->busnr;
623 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800624 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800625 match->dev = dev;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300626 spin_lock_init(&match->assigned_dev_lock);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000627 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800628 match->kvm = kvm;
Sheng Yange56d5322009-03-12 21:45:39 +0800629 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
630 INIT_WORK(&match->interrupt_work,
631 kvm_assigned_dev_interrupt_work_handler);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800632
633 list_add(&match->list, &kvm->arch.assigned_dev_head);
634
635 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100636 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800637 r = kvm_iommu_map_guest(kvm);
638 if (r)
639 goto out_list_del;
640 }
641 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800642 if (r)
643 goto out_list_del;
644 }
645
646out:
647 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000648 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800649 return r;
650out_list_del:
651 list_del(&match->list);
652 pci_release_regions(dev);
653out_disable:
654 pci_disable_device(dev);
655out_put:
656 pci_dev_put(dev);
657out_free:
658 kfree(match);
659 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000660 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800661 return r;
662}
663#endif
664
Weidong Han0a920352008-12-02 21:24:23 +0800665#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
666static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
667 struct kvm_assigned_pci_dev *assigned_dev)
668{
669 int r = 0;
670 struct kvm_assigned_dev_kernel *match;
671
672 mutex_lock(&kvm->lock);
673
674 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
675 assigned_dev->assigned_dev_id);
676 if (!match) {
677 printk(KERN_INFO "%s: device hasn't been assigned before, "
678 "so cannot be deassigned\n", __func__);
679 r = -EINVAL;
680 goto out;
681 }
682
Weidong Han4a906e42009-02-13 17:27:51 +0800683 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
Weidong Han0a920352008-12-02 21:24:23 +0800684 kvm_deassign_device(kvm, match);
685
686 kvm_free_assigned_device(kvm, match);
687
688out:
689 mutex_unlock(&kvm->lock);
690 return r;
691}
692#endif
693
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800694inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300695{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100696 if (pfn_valid(pfn)) {
697 struct page *page = compound_head(pfn_to_page(pfn));
698 return PageReserved(page);
699 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300700
701 return true;
702}
703
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704/*
705 * Switches to specified vcpu, until a matching vcpu_put()
706 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200707void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708{
Avi Kivity15ad7142007-07-11 18:17:21 +0300709 int cpu;
710
Avi Kivitybccf2152007-02-21 18:04:26 +0200711 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300712 cpu = get_cpu();
713 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200714 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300715 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200716}
717
Carsten Otte313a3dc2007-10-11 19:16:52 +0200718void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719{
Avi Kivity15ad7142007-07-11 18:17:21 +0300720 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200721 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300722 preempt_notifier_unregister(&vcpu->preempt_notifier);
723 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 mutex_unlock(&vcpu->mutex);
725}
726
Avi Kivityd9e368d2007-06-07 19:18:30 +0300727static void ack_flush(void *_completed)
728{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300729}
730
Rusty Russell49846892008-12-08 20:26:24 +1030731static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300732{
Avi Kivity597a5f52008-07-20 14:24:22 +0300733 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030734 cpumask_var_t cpus;
735 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300736 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300737
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030738 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
739 cpumask_clear(cpus);
740
Avi Kivity597a5f52008-07-20 14:24:22 +0300741 me = get_cpu();
Marcelo Tosatti84261922009-06-17 10:53:47 -0300742 spin_lock(&kvm->requests_lock);
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300743 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030744 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300745 continue;
746 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030747 if (cpus != NULL && cpu != -1 && cpu != me)
748 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300749 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030750 if (unlikely(cpus == NULL))
751 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
752 else if (!cpumask_empty(cpus))
753 smp_call_function_many(cpus, ack_flush, NULL, 1);
754 else
755 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300756 spin_unlock(&kvm->requests_lock);
Avi Kivity597a5f52008-07-20 14:24:22 +0300757 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030758 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030759 return called;
760}
761
762void kvm_flush_remote_tlbs(struct kvm *kvm)
763{
764 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
765 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300766}
767
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500768void kvm_reload_remote_mmus(struct kvm *kvm)
769{
Rusty Russell49846892008-12-08 20:26:24 +1030770 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500771}
772
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000773int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
774{
775 struct page *page;
776 int r;
777
778 mutex_init(&vcpu->mutex);
779 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000780 vcpu->kvm = kvm;
781 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300782 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000783
784 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
785 if (!page) {
786 r = -ENOMEM;
787 goto fail;
788 }
789 vcpu->run = page_address(page);
790
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800791 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000792 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800793 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000794 return 0;
795
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000796fail_free_run:
797 free_page((unsigned long)vcpu->run);
798fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000799 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000800}
801EXPORT_SYMBOL_GPL(kvm_vcpu_init);
802
803void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
804{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800805 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000806 free_page((unsigned long)vcpu->run);
807}
808EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
809
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200810#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
811static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
812{
813 return container_of(mn, struct kvm, mmu_notifier);
814}
815
816static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
817 struct mm_struct *mm,
818 unsigned long address)
819{
820 struct kvm *kvm = mmu_notifier_to_kvm(mn);
821 int need_tlb_flush;
822
823 /*
824 * When ->invalidate_page runs, the linux pte has been zapped
825 * already but the page is still allocated until
826 * ->invalidate_page returns. So if we increase the sequence
827 * here the kvm page fault will notice if the spte can't be
828 * established because the page is going to be freed. If
829 * instead the kvm page fault establishes the spte before
830 * ->invalidate_page runs, kvm_unmap_hva will release it
831 * before returning.
832 *
833 * The sequence increase only need to be seen at spin_unlock
834 * time, and not at spin_lock time.
835 *
836 * Increasing the sequence after the spin_unlock would be
837 * unsafe because the kvm page fault could then establish the
838 * pte after kvm_unmap_hva returned, without noticing the page
839 * is going to be freed.
840 */
841 spin_lock(&kvm->mmu_lock);
842 kvm->mmu_notifier_seq++;
843 need_tlb_flush = kvm_unmap_hva(kvm, address);
844 spin_unlock(&kvm->mmu_lock);
845
846 /* we've to flush the tlb before the pages can be freed */
847 if (need_tlb_flush)
848 kvm_flush_remote_tlbs(kvm);
849
850}
851
852static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
853 struct mm_struct *mm,
854 unsigned long start,
855 unsigned long end)
856{
857 struct kvm *kvm = mmu_notifier_to_kvm(mn);
858 int need_tlb_flush = 0;
859
860 spin_lock(&kvm->mmu_lock);
861 /*
862 * The count increase must become visible at unlock time as no
863 * spte can be established without taking the mmu_lock and
864 * count is also read inside the mmu_lock critical section.
865 */
866 kvm->mmu_notifier_count++;
867 for (; start < end; start += PAGE_SIZE)
868 need_tlb_flush |= kvm_unmap_hva(kvm, start);
869 spin_unlock(&kvm->mmu_lock);
870
871 /* we've to flush the tlb before the pages can be freed */
872 if (need_tlb_flush)
873 kvm_flush_remote_tlbs(kvm);
874}
875
876static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
877 struct mm_struct *mm,
878 unsigned long start,
879 unsigned long end)
880{
881 struct kvm *kvm = mmu_notifier_to_kvm(mn);
882
883 spin_lock(&kvm->mmu_lock);
884 /*
885 * This sequence increase will notify the kvm page fault that
886 * the page that is going to be mapped in the spte could have
887 * been freed.
888 */
889 kvm->mmu_notifier_seq++;
890 /*
891 * The above sequence increase must be visible before the
892 * below count decrease but both values are read by the kvm
893 * page fault under mmu_lock spinlock so we don't need to add
894 * a smb_wmb() here in between the two.
895 */
896 kvm->mmu_notifier_count--;
897 spin_unlock(&kvm->mmu_lock);
898
899 BUG_ON(kvm->mmu_notifier_count < 0);
900}
901
902static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
903 struct mm_struct *mm,
904 unsigned long address)
905{
906 struct kvm *kvm = mmu_notifier_to_kvm(mn);
907 int young;
908
909 spin_lock(&kvm->mmu_lock);
910 young = kvm_age_hva(kvm, address);
911 spin_unlock(&kvm->mmu_lock);
912
913 if (young)
914 kvm_flush_remote_tlbs(kvm);
915
916 return young;
917}
918
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100919static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
920 struct mm_struct *mm)
921{
922 struct kvm *kvm = mmu_notifier_to_kvm(mn);
923 kvm_arch_flush_shadow(kvm);
924}
925
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200926static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
927 .invalidate_page = kvm_mmu_notifier_invalidate_page,
928 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
929 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
930 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100931 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200932};
933#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
934
Avi Kivityf17abe92007-02-21 19:28:04 +0200935static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800937 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200938#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
939 struct page *page;
940#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800942 if (IS_ERR(kvm))
943 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200944#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200945 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200946 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
947#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948
Laurent Vivier5f94c172008-05-30 16:05:54 +0200949#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
950 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
951 if (!page) {
952 kfree(kvm);
953 return ERR_PTR(-ENOMEM);
954 }
955 kvm->coalesced_mmio_ring =
956 (struct kvm_coalesced_mmio_ring *)page_address(page);
957#endif
958
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200959#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
960 {
961 int err;
962 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
963 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
964 if (err) {
965#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
966 put_page(page);
967#endif
968 kfree(kvm);
969 return ERR_PTR(err);
970 }
971 }
972#endif
973
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200974 kvm->mm = current->mm;
975 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500976 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300977 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300978 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskins721eecb2009-05-20 10:30:49 -0400979 kvm_irqfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800980 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300981 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400982 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200983 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300984 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000985 spin_lock(&kvm_lock);
986 list_add(&kvm->vm_list, &vm_list);
987 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200988#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
989 kvm_coalesced_mmio_init(kvm);
990#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800991out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200992 return kvm;
993}
994
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995/*
996 * Free any memory in @free but not in @dont.
997 */
998static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
999 struct kvm_memory_slot *dont)
1000{
Izik Eidus290fc382007-09-27 14:11:22 +02001001 if (!dont || free->rmap != dont->rmap)
1002 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003
1004 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1005 vfree(free->dirty_bitmap);
1006
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001007 if (!dont || free->lpage_info != dont->lpage_info)
1008 vfree(free->lpage_info);
1009
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +00001011 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001012 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001013 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014}
1015
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001016void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017{
1018 int i;
1019
1020 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +00001021 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022}
1023
Avi Kivityf17abe92007-02-21 19:28:04 +02001024static void kvm_destroy_vm(struct kvm *kvm)
1025{
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001026 struct mm_struct *mm = kvm->mm;
1027
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001028 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -08001029 spin_lock(&kvm_lock);
1030 list_del(&kvm->vm_list);
1031 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02001032 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +03001033 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001034 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +02001035#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1036 if (kvm->coalesced_mmio_ring != NULL)
1037 free_page((unsigned long)kvm->coalesced_mmio_ring);
1038#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001039#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1040 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +02001041#else
1042 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001043#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08001044 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001045 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001046}
1047
Izik Eidusd39f13b2008-03-30 16:01:25 +03001048void kvm_get_kvm(struct kvm *kvm)
1049{
1050 atomic_inc(&kvm->users_count);
1051}
1052EXPORT_SYMBOL_GPL(kvm_get_kvm);
1053
1054void kvm_put_kvm(struct kvm *kvm)
1055{
1056 if (atomic_dec_and_test(&kvm->users_count))
1057 kvm_destroy_vm(kvm);
1058}
1059EXPORT_SYMBOL_GPL(kvm_put_kvm);
1060
1061
Avi Kivityf17abe92007-02-21 19:28:04 +02001062static int kvm_vm_release(struct inode *inode, struct file *filp)
1063{
1064 struct kvm *kvm = filp->private_data;
1065
Gregory Haskins721eecb2009-05-20 10:30:49 -04001066 kvm_irqfd_release(kvm);
1067
Izik Eidusd39f13b2008-03-30 16:01:25 +03001068 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 return 0;
1070}
1071
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 * Allocate some memory and give it an address in the guest physical address
1074 * space.
1075 *
1076 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001077 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001078 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001080int __kvm_set_memory_region(struct kvm *kvm,
1081 struct kvm_userspace_memory_region *mem,
1082 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083{
1084 int r;
1085 gfn_t base_gfn;
Avi Kivityac045272009-06-08 15:52:39 +03001086 unsigned long npages, ugfn;
Avi Kivity09f8ca72009-06-08 15:55:21 +03001087 unsigned long largepages, i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 struct kvm_memory_slot *memslot;
1089 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090
1091 r = -EINVAL;
1092 /* General sanity checks */
1093 if (mem->memory_size & (PAGE_SIZE - 1))
1094 goto out;
1095 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1096 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +08001097 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -06001098 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001099 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 goto out;
1101 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1102 goto out;
1103
1104 memslot = &kvm->memslots[mem->slot];
1105 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1106 npages = mem->memory_size >> PAGE_SHIFT;
1107
1108 if (!npages)
1109 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1110
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 new = old = *memslot;
1112
1113 new.base_gfn = base_gfn;
1114 new.npages = npages;
1115 new.flags = mem->flags;
1116
1117 /* Disallow changing a memory slot's size. */
1118 r = -EINVAL;
1119 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001120 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121
1122 /* Check for overlaps */
1123 r = -EEXIST;
1124 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1125 struct kvm_memory_slot *s = &kvm->memslots[i];
1126
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001127 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 continue;
1129 if (!((base_gfn + npages <= s->base_gfn) ||
1130 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001131 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 /* Free page dirty bitmap if unneeded */
1135 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001136 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137
1138 r = -ENOMEM;
1139
1140 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001141#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001142 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001143 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001144
1145 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001146 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001147
Izik Eidus290fc382007-09-27 14:11:22 +02001148 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001149
Izik Eidus80b14b52007-10-25 11:54:04 +02001150 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001151 /*
1152 * hva_to_rmmap() serialzies with the mmu_lock and to be
1153 * safe it has to ignore memslots with !user_alloc &&
1154 * !userspace_addr.
1155 */
1156 if (user_alloc)
1157 new.userspace_addr = mem->userspace_addr;
1158 else
1159 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001161 if (npages && !new.lpage_info) {
Avi Kivity99894a72009-03-29 16:31:25 +03001162 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1163 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001164
1165 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1166
1167 if (!new.lpage_info)
1168 goto out_free;
1169
1170 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1171
1172 if (base_gfn % KVM_PAGES_PER_HPAGE)
1173 new.lpage_info[0].write_count = 1;
1174 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1175 new.lpage_info[largepages-1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +03001176 ugfn = new.userspace_addr >> PAGE_SHIFT;
1177 /*
1178 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001179 * other, or if explicitly asked to, disable large page
1180 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +03001181 */
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001182 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1) ||
1183 !largepages_enabled)
Avi Kivityac045272009-06-08 15:52:39 +03001184 for (i = 0; i < largepages; ++i)
1185 new.lpage_info[i].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001186 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187
1188 /* Allocate page dirty bitmap if needed */
1189 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1190 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1191
1192 new.dirty_bitmap = vmalloc(dirty_bytes);
1193 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001194 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +03001196 if (old.npages)
1197 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198 }
Carsten Otteeff01142008-06-27 15:05:31 +02001199#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001201 if (!npages)
1202 kvm_arch_flush_shadow(kvm);
1203
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001204 spin_lock(&kvm->mmu_lock);
1205 if (mem->slot >= kvm->nmemslots)
1206 kvm->nmemslots = mem->slot + 1;
1207
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001209 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210
Zhang Xiantao0de10342007-11-20 16:25:04 +08001211 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1212 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001213 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001214 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001215 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001216 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001217 }
1218
Glauber Costa6f897242008-12-03 13:40:51 -02001219 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1220 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001221 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -02001222 if (!npages)
1223 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -03001224 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001225#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001226 /* map the pages in iommu page table */
1227 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1228 if (r)
1229 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001230#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 return 0;
1232
Sheng Yangf78e0e22007-10-29 09:40:42 +08001233out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234 kvm_free_physmem_slot(&new, &old);
1235out:
1236 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001237
1238}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001239EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1240
1241int kvm_set_memory_region(struct kvm *kvm,
1242 struct kvm_userspace_memory_region *mem,
1243 int user_alloc)
1244{
1245 int r;
1246
Izik Eidus72dc67a2008-02-10 18:04:15 +02001247 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001248 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001249 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001250 return r;
1251}
Izik Eidus210c7c42007-10-24 23:52:57 +02001252EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1253
Carsten Otte1fe779f2007-10-29 16:08:35 +01001254int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1255 struct
1256 kvm_userspace_memory_region *mem,
1257 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001258{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001259 if (mem->slot >= KVM_MEMORY_SLOTS)
1260 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001261 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262}
1263
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001264int kvm_get_dirty_log(struct kvm *kvm,
1265 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266{
1267 struct kvm_memory_slot *memslot;
1268 int r, i;
1269 int n;
1270 unsigned long any = 0;
1271
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 r = -EINVAL;
1273 if (log->slot >= KVM_MEMORY_SLOTS)
1274 goto out;
1275
1276 memslot = &kvm->memslots[log->slot];
1277 r = -ENOENT;
1278 if (!memslot->dirty_bitmap)
1279 goto out;
1280
Uri Lublincd1a4a92007-02-22 16:43:09 +02001281 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282
Uri Lublincd1a4a92007-02-22 16:43:09 +02001283 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 any = memslot->dirty_bitmap[i];
1285
1286 r = -EFAULT;
1287 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1288 goto out;
1289
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001290 if (any)
1291 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292
1293 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295 return r;
1296}
1297
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001298void kvm_disable_largepages(void)
1299{
1300 largepages_enabled = false;
1301}
1302EXPORT_SYMBOL_GPL(kvm_disable_largepages);
1303
Izik Eiduscea7bb22007-10-17 19:17:48 +02001304int is_error_page(struct page *page)
1305{
1306 return page == bad_page;
1307}
1308EXPORT_SYMBOL_GPL(is_error_page);
1309
Anthony Liguori35149e22008-04-02 14:46:56 -05001310int is_error_pfn(pfn_t pfn)
1311{
1312 return pfn == bad_pfn;
1313}
1314EXPORT_SYMBOL_GPL(is_error_pfn);
1315
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001316static inline unsigned long bad_hva(void)
1317{
1318 return PAGE_OFFSET;
1319}
1320
1321int kvm_is_error_hva(unsigned long addr)
1322{
1323 return addr == bad_hva();
1324}
1325EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1326
Izik Eidus28430992008-10-03 17:40:32 +03001327struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328{
1329 int i;
1330
1331 for (i = 0; i < kvm->nmemslots; ++i) {
1332 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1333
1334 if (gfn >= memslot->base_gfn
1335 && gfn < memslot->base_gfn + memslot->npages)
1336 return memslot;
1337 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001338 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339}
Izik Eidus28430992008-10-03 17:40:32 +03001340EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001341
1342struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1343{
1344 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001345 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001346}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347
Izik Eiduse0d62c72007-10-24 23:57:46 +02001348int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1349{
1350 int i;
1351
1352 gfn = unalias_gfn(kvm, gfn);
1353 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1354 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1355
1356 if (gfn >= memslot->base_gfn
1357 && gfn < memslot->base_gfn + memslot->npages)
1358 return 1;
1359 }
1360 return 0;
1361}
1362EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1363
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001364unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001365{
1366 struct kvm_memory_slot *slot;
1367
1368 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001369 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001370 if (!slot)
1371 return bad_hva();
1372 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1373}
Sheng Yang0d150292008-04-25 21:44:50 +08001374EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001375
Anthony Liguori35149e22008-04-02 14:46:56 -05001376pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001377{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001378 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001379 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001380 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001381 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001382
Avi Kivity60395222007-10-21 11:03:36 +02001383 might_sleep();
1384
Izik Eidus539cb662007-11-11 22:05:04 +02001385 addr = gfn_to_hva(kvm, gfn);
1386 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001387 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001388 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001389 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001390
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001391 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001392
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001393 if (unlikely(npages != 1)) {
1394 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001395
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001396 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001397 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001398
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001399 if (vma == NULL || addr < vma->vm_start ||
1400 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001401 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001402 get_page(bad_page);
1403 return page_to_pfn(bad_page);
1404 }
1405
1406 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001407 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001408 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001409 } else
1410 pfn = page_to_pfn(page[0]);
1411
1412 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001413}
1414
1415EXPORT_SYMBOL_GPL(gfn_to_pfn);
1416
1417struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1418{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001419 pfn_t pfn;
1420
1421 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001422 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001423 return pfn_to_page(pfn);
1424
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001425 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001426
1427 get_page(bad_page);
1428 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001429}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001430
Avi Kivity954bbbc22007-03-30 14:02:32 +03001431EXPORT_SYMBOL_GPL(gfn_to_page);
1432
Izik Eidusb4231d62007-11-20 11:49:33 +02001433void kvm_release_page_clean(struct page *page)
1434{
Anthony Liguori35149e22008-04-02 14:46:56 -05001435 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001436}
1437EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1438
Anthony Liguori35149e22008-04-02 14:46:56 -05001439void kvm_release_pfn_clean(pfn_t pfn)
1440{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001441 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001442 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001443}
1444EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1445
Izik Eidusb4231d62007-11-20 11:49:33 +02001446void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001447{
Anthony Liguori35149e22008-04-02 14:46:56 -05001448 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001449}
Izik Eidusb4231d62007-11-20 11:49:33 +02001450EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001451
Anthony Liguori35149e22008-04-02 14:46:56 -05001452void kvm_release_pfn_dirty(pfn_t pfn)
1453{
1454 kvm_set_pfn_dirty(pfn);
1455 kvm_release_pfn_clean(pfn);
1456}
1457EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1458
1459void kvm_set_page_dirty(struct page *page)
1460{
1461 kvm_set_pfn_dirty(page_to_pfn(page));
1462}
1463EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1464
1465void kvm_set_pfn_dirty(pfn_t pfn)
1466{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001467 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001468 struct page *page = pfn_to_page(pfn);
1469 if (!PageReserved(page))
1470 SetPageDirty(page);
1471 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001472}
1473EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1474
1475void kvm_set_pfn_accessed(pfn_t pfn)
1476{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001477 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001478 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001479}
1480EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1481
1482void kvm_get_pfn(pfn_t pfn)
1483{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001484 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001485 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001486}
1487EXPORT_SYMBOL_GPL(kvm_get_pfn);
1488
Izik Eidus195aefd2007-10-01 22:14:18 +02001489static int next_segment(unsigned long len, int offset)
1490{
1491 if (len > PAGE_SIZE - offset)
1492 return PAGE_SIZE - offset;
1493 else
1494 return len;
1495}
1496
1497int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1498 int len)
1499{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001500 int r;
1501 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001502
Izik Eiduse0506bc2007-11-11 22:10:22 +02001503 addr = gfn_to_hva(kvm, gfn);
1504 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001505 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001506 r = copy_from_user(data, (void __user *)addr + offset, len);
1507 if (r)
1508 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001509 return 0;
1510}
1511EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1512
1513int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1514{
1515 gfn_t gfn = gpa >> PAGE_SHIFT;
1516 int seg;
1517 int offset = offset_in_page(gpa);
1518 int ret;
1519
1520 while ((seg = next_segment(len, offset)) != 0) {
1521 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1522 if (ret < 0)
1523 return ret;
1524 offset = 0;
1525 len -= seg;
1526 data += seg;
1527 ++gfn;
1528 }
1529 return 0;
1530}
1531EXPORT_SYMBOL_GPL(kvm_read_guest);
1532
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001533int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1534 unsigned long len)
1535{
1536 int r;
1537 unsigned long addr;
1538 gfn_t gfn = gpa >> PAGE_SHIFT;
1539 int offset = offset_in_page(gpa);
1540
1541 addr = gfn_to_hva(kvm, gfn);
1542 if (kvm_is_error_hva(addr))
1543 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001544 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001545 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001546 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001547 if (r)
1548 return -EFAULT;
1549 return 0;
1550}
1551EXPORT_SYMBOL(kvm_read_guest_atomic);
1552
Izik Eidus195aefd2007-10-01 22:14:18 +02001553int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1554 int offset, int len)
1555{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001556 int r;
1557 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001558
Izik Eiduse0506bc2007-11-11 22:10:22 +02001559 addr = gfn_to_hva(kvm, gfn);
1560 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001561 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001562 r = copy_to_user((void __user *)addr + offset, data, len);
1563 if (r)
1564 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001565 mark_page_dirty(kvm, gfn);
1566 return 0;
1567}
1568EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1569
1570int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1571 unsigned long len)
1572{
1573 gfn_t gfn = gpa >> PAGE_SHIFT;
1574 int seg;
1575 int offset = offset_in_page(gpa);
1576 int ret;
1577
1578 while ((seg = next_segment(len, offset)) != 0) {
1579 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1580 if (ret < 0)
1581 return ret;
1582 offset = 0;
1583 len -= seg;
1584 data += seg;
1585 ++gfn;
1586 }
1587 return 0;
1588}
1589
1590int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1591{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001592 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001593}
1594EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1595
1596int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1597{
1598 gfn_t gfn = gpa >> PAGE_SHIFT;
1599 int seg;
1600 int offset = offset_in_page(gpa);
1601 int ret;
1602
1603 while ((seg = next_segment(len, offset)) != 0) {
1604 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1605 if (ret < 0)
1606 return ret;
1607 offset = 0;
1608 len -= seg;
1609 ++gfn;
1610 }
1611 return 0;
1612}
1613EXPORT_SYMBOL_GPL(kvm_clear_guest);
1614
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1616{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001617 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618
Uri Lublin3b6fff12007-10-30 10:42:09 +02001619 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001620 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001621 if (memslot && memslot->dirty_bitmap) {
1622 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623
Rusty Russell7e9d6192007-07-31 20:41:14 +10001624 /* avoid RMW */
1625 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1626 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 }
1628}
1629
Eddie Dongb6958ce2007-07-18 12:15:21 +03001630/*
1631 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1632 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001633void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001634{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001635 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001636
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001637 for (;;) {
1638 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001639
Gleb Natapov78646122009-03-23 12:12:11 +02001640 if ((kvm_arch_interrupt_allowed(vcpu) &&
1641 kvm_cpu_has_interrupt(vcpu)) ||
Gleb Natapov09cec752009-03-23 15:11:44 +02001642 kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001643 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001644 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001645 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001646 if (kvm_cpu_has_pending_timer(vcpu))
1647 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001648 if (signal_pending(current))
1649 break;
1650
Eddie Dongb6958ce2007-07-18 12:15:21 +03001651 vcpu_put(vcpu);
1652 schedule();
1653 vcpu_load(vcpu);
1654 }
1655
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001656 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001657}
1658
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659void kvm_resched(struct kvm_vcpu *vcpu)
1660{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001661 if (!need_resched())
1662 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664}
1665EXPORT_SYMBOL_GPL(kvm_resched);
1666
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001667static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001668{
1669 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001670 struct page *page;
1671
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001672 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001673 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001674#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001675 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001676 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001677#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001678#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1679 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1680 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1681#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001682 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001683 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001684 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001685 vmf->page = page;
1686 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001687}
1688
1689static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001690 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001691};
1692
1693static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1694{
1695 vma->vm_ops = &kvm_vcpu_vm_ops;
1696 return 0;
1697}
1698
Avi Kivitybccf2152007-02-21 18:04:26 +02001699static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1700{
1701 struct kvm_vcpu *vcpu = filp->private_data;
1702
Al Viro66c0b392008-04-19 20:33:56 +01001703 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001704 return 0;
1705}
1706
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001707static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001708 .release = kvm_vcpu_release,
1709 .unlocked_ioctl = kvm_vcpu_ioctl,
1710 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001711 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001712};
1713
1714/*
1715 * Allocates an inode for the vcpu.
1716 */
1717static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1718{
Gleb Natapov73880c82009-06-09 15:56:28 +03001719 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Avi Kivitybccf2152007-02-21 18:04:26 +02001720}
1721
Avi Kivityc5ea7662007-02-20 18:41:05 +02001722/*
1723 * Creates some virtual cpus. Good luck creating more than one.
1724 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001725static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001726{
1727 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001728 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001729
Gleb Natapov73880c82009-06-09 15:56:28 +03001730 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001731 if (IS_ERR(vcpu))
1732 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001733
Avi Kivity15ad7142007-07-11 18:17:21 +03001734 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1735
Avi Kivity26e52152007-11-20 15:30:24 +02001736 r = kvm_arch_vcpu_setup(vcpu);
1737 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001738 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001739
Shaohua Li11ec2802007-07-23 14:51:37 +08001740 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001741 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1742 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001743 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001744 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001745
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001746 kvm_for_each_vcpu(r, v, kvm)
1747 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001748 r = -EEXIST;
1749 goto vcpu_destroy;
1750 }
1751
1752 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001753
1754 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001755 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001756 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001757 if (r < 0) {
1758 kvm_put_kvm(kvm);
1759 goto vcpu_destroy;
1760 }
1761
1762 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1763 smp_wmb();
1764 atomic_inc(&kvm->online_vcpus);
1765
1766#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1767 if (kvm->bsp_vcpu_id == id)
1768 kvm->bsp_vcpu = vcpu;
1769#endif
1770 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001771 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001772
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001773vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001774 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001775 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001776 return r;
1777}
1778
Avi Kivity1961d272007-03-05 19:46:05 +02001779static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1780{
1781 if (sigset) {
1782 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1783 vcpu->sigset_active = 1;
1784 vcpu->sigset = *sigset;
1785 } else
1786 vcpu->sigset_active = 0;
1787 return 0;
1788}
1789
Sheng Yangc1e01512009-02-25 17:22:26 +08001790#ifdef __KVM_HAVE_MSIX
1791static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1792 struct kvm_assigned_msix_nr *entry_nr)
1793{
1794 int r = 0;
1795 struct kvm_assigned_dev_kernel *adev;
1796
1797 mutex_lock(&kvm->lock);
1798
1799 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1800 entry_nr->assigned_dev_id);
1801 if (!adev) {
1802 r = -EINVAL;
1803 goto msix_nr_out;
1804 }
1805
1806 if (adev->entries_nr == 0) {
1807 adev->entries_nr = entry_nr->entry_nr;
1808 if (adev->entries_nr == 0 ||
1809 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1810 r = -EINVAL;
1811 goto msix_nr_out;
1812 }
1813
1814 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1815 entry_nr->entry_nr,
1816 GFP_KERNEL);
1817 if (!adev->host_msix_entries) {
1818 r = -ENOMEM;
1819 goto msix_nr_out;
1820 }
1821 adev->guest_msix_entries = kzalloc(
1822 sizeof(struct kvm_guest_msix_entry) *
1823 entry_nr->entry_nr, GFP_KERNEL);
1824 if (!adev->guest_msix_entries) {
1825 kfree(adev->host_msix_entries);
1826 r = -ENOMEM;
1827 goto msix_nr_out;
1828 }
1829 } else /* Not allowed set MSI-X number twice */
1830 r = -EINVAL;
1831msix_nr_out:
1832 mutex_unlock(&kvm->lock);
1833 return r;
1834}
1835
1836static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1837 struct kvm_assigned_msix_entry *entry)
1838{
1839 int r = 0, i;
1840 struct kvm_assigned_dev_kernel *adev;
1841
1842 mutex_lock(&kvm->lock);
1843
1844 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1845 entry->assigned_dev_id);
1846
1847 if (!adev) {
1848 r = -EINVAL;
1849 goto msix_entry_out;
1850 }
1851
1852 for (i = 0; i < adev->entries_nr; i++)
1853 if (adev->guest_msix_entries[i].vector == 0 ||
1854 adev->guest_msix_entries[i].entry == entry->entry) {
1855 adev->guest_msix_entries[i].entry = entry->entry;
1856 adev->guest_msix_entries[i].vector = entry->gsi;
1857 adev->host_msix_entries[i].entry = entry->entry;
1858 break;
1859 }
1860 if (i == adev->entries_nr) {
1861 r = -ENOSPC;
1862 goto msix_entry_out;
1863 }
1864
1865msix_entry_out:
1866 mutex_unlock(&kvm->lock);
1867
1868 return r;
1869}
1870#endif
1871
Avi Kivitybccf2152007-02-21 18:04:26 +02001872static long kvm_vcpu_ioctl(struct file *filp,
1873 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001874{
Avi Kivitybccf2152007-02-21 18:04:26 +02001875 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001876 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001877 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001878 struct kvm_fpu *fpu = NULL;
1879 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001881 if (vcpu->kvm->mm != current->mm)
1882 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001884 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001885 r = -EINVAL;
1886 if (arg)
1887 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001888 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001891 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001893 r = -ENOMEM;
1894 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1895 if (!kvm_regs)
1896 goto out;
1897 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001899 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001901 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1902 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001904out_free1:
1905 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 break;
1907 }
1908 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001909 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001911 r = -ENOMEM;
1912 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1913 if (!kvm_regs)
1914 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001916 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1917 goto out_free2;
1918 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001920 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001922out_free2:
1923 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924 break;
1925 }
1926 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001927 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1928 r = -ENOMEM;
1929 if (!kvm_sregs)
1930 goto out;
1931 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 if (r)
1933 goto out;
1934 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001935 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 goto out;
1937 r = 0;
1938 break;
1939 }
1940 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001941 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1942 r = -ENOMEM;
1943 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001945 r = -EFAULT;
1946 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1947 goto out;
1948 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 if (r)
1950 goto out;
1951 r = 0;
1952 break;
1953 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001954 case KVM_GET_MP_STATE: {
1955 struct kvm_mp_state mp_state;
1956
1957 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1958 if (r)
1959 goto out;
1960 r = -EFAULT;
1961 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1962 goto out;
1963 r = 0;
1964 break;
1965 }
1966 case KVM_SET_MP_STATE: {
1967 struct kvm_mp_state mp_state;
1968
1969 r = -EFAULT;
1970 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1971 goto out;
1972 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1973 if (r)
1974 goto out;
1975 r = 0;
1976 break;
1977 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 case KVM_TRANSLATE: {
1979 struct kvm_translation tr;
1980
1981 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001982 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001984 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 if (r)
1986 goto out;
1987 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001988 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 goto out;
1990 r = 0;
1991 break;
1992 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001993 case KVM_SET_GUEST_DEBUG: {
1994 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995
1996 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001997 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001999 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000 if (r)
2001 goto out;
2002 r = 0;
2003 break;
2004 }
Avi Kivity1961d272007-03-05 19:46:05 +02002005 case KVM_SET_SIGNAL_MASK: {
2006 struct kvm_signal_mask __user *sigmask_arg = argp;
2007 struct kvm_signal_mask kvm_sigmask;
2008 sigset_t sigset, *p;
2009
2010 p = NULL;
2011 if (argp) {
2012 r = -EFAULT;
2013 if (copy_from_user(&kvm_sigmask, argp,
2014 sizeof kvm_sigmask))
2015 goto out;
2016 r = -EINVAL;
2017 if (kvm_sigmask.len != sizeof sigset)
2018 goto out;
2019 r = -EFAULT;
2020 if (copy_from_user(&sigset, sigmask_arg->sigset,
2021 sizeof sigset))
2022 goto out;
2023 p = &sigset;
2024 }
2025 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2026 break;
2027 }
Avi Kivityb8836732007-04-01 16:34:31 +03002028 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002029 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2030 r = -ENOMEM;
2031 if (!fpu)
2032 goto out;
2033 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002034 if (r)
2035 goto out;
2036 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002037 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03002038 goto out;
2039 r = 0;
2040 break;
2041 }
2042 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07002043 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2044 r = -ENOMEM;
2045 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03002046 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07002047 r = -EFAULT;
2048 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2049 goto out;
2050 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03002051 if (r)
2052 goto out;
2053 r = 0;
2054 break;
2055 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002056 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002057 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002058 }
2059out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07002060 kfree(fpu);
2061 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02002062 return r;
2063}
2064
2065static long kvm_vm_ioctl(struct file *filp,
2066 unsigned int ioctl, unsigned long arg)
2067{
2068 struct kvm *kvm = filp->private_data;
2069 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002070 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002071
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002072 if (kvm->mm != current->mm)
2073 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002074 switch (ioctl) {
2075 case KVM_CREATE_VCPU:
2076 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2077 if (r < 0)
2078 goto out;
2079 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002080 case KVM_SET_USER_MEMORY_REGION: {
2081 struct kvm_userspace_memory_region kvm_userspace_mem;
2082
2083 r = -EFAULT;
2084 if (copy_from_user(&kvm_userspace_mem, argp,
2085 sizeof kvm_userspace_mem))
2086 goto out;
2087
2088 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089 if (r)
2090 goto out;
2091 break;
2092 }
2093 case KVM_GET_DIRTY_LOG: {
2094 struct kvm_dirty_log log;
2095
2096 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002097 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002099 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100 if (r)
2101 goto out;
2102 break;
2103 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002104#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2105 case KVM_REGISTER_COALESCED_MMIO: {
2106 struct kvm_coalesced_mmio_zone zone;
2107 r = -EFAULT;
2108 if (copy_from_user(&zone, argp, sizeof zone))
2109 goto out;
2110 r = -ENXIO;
2111 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2112 if (r)
2113 goto out;
2114 r = 0;
2115 break;
2116 }
2117 case KVM_UNREGISTER_COALESCED_MMIO: {
2118 struct kvm_coalesced_mmio_zone zone;
2119 r = -EFAULT;
2120 if (copy_from_user(&zone, argp, sizeof zone))
2121 goto out;
2122 r = -ENXIO;
2123 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2124 if (r)
2125 goto out;
2126 r = 0;
2127 break;
2128 }
2129#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002130#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2131 case KVM_ASSIGN_PCI_DEVICE: {
2132 struct kvm_assigned_pci_dev assigned_dev;
2133
2134 r = -EFAULT;
2135 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2136 goto out;
2137 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2138 if (r)
2139 goto out;
2140 break;
2141 }
2142 case KVM_ASSIGN_IRQ: {
Sheng Yange56d5322009-03-12 21:45:39 +08002143 r = -EOPNOTSUPP;
2144 break;
2145 }
2146#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2147 case KVM_ASSIGN_DEV_IRQ: {
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002148 struct kvm_assigned_irq assigned_irq;
2149
2150 r = -EFAULT;
2151 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2152 goto out;
2153 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2154 if (r)
2155 goto out;
2156 break;
2157 }
Sheng Yange56d5322009-03-12 21:45:39 +08002158 case KVM_DEASSIGN_DEV_IRQ: {
2159 struct kvm_assigned_irq assigned_irq;
2160
2161 r = -EFAULT;
2162 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2163 goto out;
2164 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2165 if (r)
2166 goto out;
2167 break;
2168 }
2169#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08002170#endif
Weidong Han0a920352008-12-02 21:24:23 +08002171#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2172 case KVM_DEASSIGN_PCI_DEVICE: {
2173 struct kvm_assigned_pci_dev assigned_dev;
2174
2175 r = -EFAULT;
2176 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2177 goto out;
2178 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2179 if (r)
2180 goto out;
2181 break;
2182 }
2183#endif
Avi Kivity399ec802008-11-19 13:58:46 +02002184#ifdef KVM_CAP_IRQ_ROUTING
2185 case KVM_SET_GSI_ROUTING: {
2186 struct kvm_irq_routing routing;
2187 struct kvm_irq_routing __user *urouting;
2188 struct kvm_irq_routing_entry *entries;
2189
2190 r = -EFAULT;
2191 if (copy_from_user(&routing, argp, sizeof(routing)))
2192 goto out;
2193 r = -EINVAL;
2194 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2195 goto out;
2196 if (routing.flags)
2197 goto out;
2198 r = -ENOMEM;
2199 entries = vmalloc(routing.nr * sizeof(*entries));
2200 if (!entries)
2201 goto out;
2202 r = -EFAULT;
2203 urouting = argp;
2204 if (copy_from_user(entries, urouting->entries,
2205 routing.nr * sizeof(*entries)))
2206 goto out_free_irq_routing;
2207 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2208 routing.flags);
2209 out_free_irq_routing:
2210 vfree(entries);
2211 break;
2212 }
Sheng Yangc1e01512009-02-25 17:22:26 +08002213#ifdef __KVM_HAVE_MSIX
2214 case KVM_ASSIGN_SET_MSIX_NR: {
2215 struct kvm_assigned_msix_nr entry_nr;
2216 r = -EFAULT;
2217 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2218 goto out;
2219 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2220 if (r)
2221 goto out;
2222 break;
2223 }
2224 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2225 struct kvm_assigned_msix_entry entry;
2226 r = -EFAULT;
2227 if (copy_from_user(&entry, argp, sizeof entry))
2228 goto out;
2229 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2230 if (r)
2231 goto out;
2232 break;
2233 }
Avi Kivity399ec802008-11-19 13:58:46 +02002234#endif
Sheng Yangc1e01512009-02-25 17:22:26 +08002235#endif /* KVM_CAP_IRQ_ROUTING */
Gregory Haskins721eecb2009-05-20 10:30:49 -04002236 case KVM_IRQFD: {
2237 struct kvm_irqfd data;
2238
2239 r = -EFAULT;
2240 if (copy_from_user(&data, argp, sizeof data))
2241 goto out;
2242 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2243 break;
2244 }
Gleb Natapov73880c82009-06-09 15:56:28 +03002245#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2246 case KVM_SET_BOOT_CPU_ID:
2247 r = 0;
2248 if (atomic_read(&kvm->online_vcpus) != 0)
2249 r = -EBUSY;
2250 else
2251 kvm->bsp_vcpu_id = arg;
2252 break;
2253#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002254 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002255 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002256 }
2257out:
2258 return r;
2259}
2260
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002261static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002262{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002263 struct page *page[1];
2264 unsigned long addr;
2265 int npages;
2266 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002267 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002268
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002269 addr = gfn_to_hva(kvm, gfn);
2270 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002271 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002272
2273 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2274 NULL);
2275 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002276 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002277
2278 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002279 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002280}
2281
2282static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002283 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002284};
2285
2286static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2287{
2288 vma->vm_ops = &kvm_vm_vm_ops;
2289 return 0;
2290}
2291
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002292static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002293 .release = kvm_vm_release,
2294 .unlocked_ioctl = kvm_vm_ioctl,
2295 .compat_ioctl = kvm_vm_ioctl,
2296 .mmap = kvm_vm_mmap,
2297};
2298
2299static int kvm_dev_ioctl_create_vm(void)
2300{
Al Viro2030a422008-02-23 06:46:49 -05002301 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002302 struct kvm *kvm;
2303
Avi Kivityf17abe92007-02-21 19:28:04 +02002304 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002305 if (IS_ERR(kvm))
2306 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07002307 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05002308 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002309 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002310
Avi Kivityf17abe92007-02-21 19:28:04 +02002311 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002312}
2313
Avi Kivity1a811b62008-12-08 18:25:27 +02002314static long kvm_dev_ioctl_check_extension_generic(long arg)
2315{
2316 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002317 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002318 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002319 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002320#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2321 case KVM_CAP_SET_BOOT_CPU_ID:
2322#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002323 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002324#ifdef CONFIG_HAVE_KVM_IRQCHIP
2325 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002326 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002327#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002328 default:
2329 break;
2330 }
2331 return kvm_dev_ioctl_check_extension(arg);
2332}
2333
Avi Kivityf17abe92007-02-21 19:28:04 +02002334static long kvm_dev_ioctl(struct file *filp,
2335 unsigned int ioctl, unsigned long arg)
2336{
Avi Kivity07c45a32007-03-07 13:05:38 +02002337 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002338
2339 switch (ioctl) {
2340 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002341 r = -EINVAL;
2342 if (arg)
2343 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002344 r = KVM_API_VERSION;
2345 break;
2346 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002347 r = -EINVAL;
2348 if (arg)
2349 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002350 r = kvm_dev_ioctl_create_vm();
2351 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002352 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002353 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002354 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002355 case KVM_GET_VCPU_MMAP_SIZE:
2356 r = -EINVAL;
2357 if (arg)
2358 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002359 r = PAGE_SIZE; /* struct kvm_run */
2360#ifdef CONFIG_X86
2361 r += PAGE_SIZE; /* pio data page */
2362#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002363#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2364 r += PAGE_SIZE; /* coalesced mmio ring page */
2365#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002366 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002367 case KVM_TRACE_ENABLE:
2368 case KVM_TRACE_PAUSE:
2369 case KVM_TRACE_DISABLE:
2370 r = kvm_trace_ioctl(ioctl, arg);
2371 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002373 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374 }
2375out:
2376 return r;
2377}
2378
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 .unlocked_ioctl = kvm_dev_ioctl,
2381 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382};
2383
2384static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002385 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 "kvm",
2387 &kvm_chardev_ops,
2388};
2389
Avi Kivity1b6c0162007-05-24 13:03:52 +03002390static void hardware_enable(void *junk)
2391{
2392 int cpu = raw_smp_processor_id();
2393
Rusty Russell7f59f492008-12-07 21:25:45 +10302394 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002395 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302396 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002397 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002398}
2399
2400static void hardware_disable(void *junk)
2401{
2402 int cpu = raw_smp_processor_id();
2403
Rusty Russell7f59f492008-12-07 21:25:45 +10302404 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002405 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302406 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002407 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002408}
2409
Avi Kivity774c47f2007-02-12 00:54:47 -08002410static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2411 void *v)
2412{
2413 int cpu = (long)v;
2414
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002415 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002416 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002417 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002418 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2419 cpu);
2420 hardware_disable(NULL);
2421 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002422 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002423 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2424 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002425 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002426 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002427 case CPU_ONLINE:
2428 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2429 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002430 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002431 break;
2432 }
2433 return NOTIFY_OK;
2434}
2435
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002436
2437asmlinkage void kvm_handle_fault_on_reboot(void)
2438{
2439 if (kvm_rebooting)
2440 /* spin while reset goes on */
2441 while (true)
2442 ;
2443 /* Fault while not rebooting. We want the trace. */
2444 BUG();
2445}
2446EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2447
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002448static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002449 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002450{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002451 /*
2452 * Some (well, at least mine) BIOSes hang on reboot if
2453 * in vmx root mode.
2454 *
2455 * And Intel TXT required VMX off for all cpu when system shutdown.
2456 */
2457 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2458 kvm_rebooting = true;
2459 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002460 return NOTIFY_OK;
2461}
2462
2463static struct notifier_block kvm_reboot_notifier = {
2464 .notifier_call = kvm_reboot,
2465 .priority = 0,
2466};
2467
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002468void kvm_io_bus_init(struct kvm_io_bus *bus)
2469{
2470 memset(bus, 0, sizeof(*bus));
2471}
2472
2473void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2474{
2475 int i;
2476
2477 for (i = 0; i < bus->dev_count; i++) {
2478 struct kvm_io_device *pos = bus->devs[i];
2479
2480 kvm_iodevice_destructor(pos);
2481 }
2482}
2483
Laurent Vivier92760492008-05-30 16:05:53 +02002484struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2485 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002486{
2487 int i;
2488
2489 for (i = 0; i < bus->dev_count; i++) {
2490 struct kvm_io_device *pos = bus->devs[i];
2491
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002492 if (kvm_iodevice_in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002493 return pos;
2494 }
2495
2496 return NULL;
2497}
2498
2499void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2500{
2501 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2502
2503 bus->devs[bus->dev_count++] = dev;
2504}
2505
Avi Kivity774c47f2007-02-12 00:54:47 -08002506static struct notifier_block kvm_cpu_notifier = {
2507 .notifier_call = kvm_cpu_hotplug,
2508 .priority = 20, /* must be > scheduler priority */
2509};
2510
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002511static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002512{
2513 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002514 struct kvm *kvm;
2515
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002516 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002517 spin_lock(&kvm_lock);
2518 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002519 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002520 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002521 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002522}
2523
2524DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2525
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002526static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002527{
2528 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002529 struct kvm *kvm;
2530 struct kvm_vcpu *vcpu;
2531 int i;
2532
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002533 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002534 spin_lock(&kvm_lock);
2535 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002536 kvm_for_each_vcpu(i, vcpu, kvm)
2537 *val += *(u32 *)((void *)vcpu + offset);
2538
Avi Kivity1165f5f2007-04-19 17:27:43 +03002539 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002540 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002541}
2542
Avi Kivityba1389b2007-11-18 16:24:12 +02002543DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2544
2545static struct file_operations *stat_fops[] = {
2546 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2547 [KVM_STAT_VM] = &vm_stat_fops,
2548};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002549
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002550static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551{
2552 struct kvm_stats_debugfs_item *p;
2553
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002554 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002556 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002557 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002558 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559}
2560
2561static void kvm_exit_debug(void)
2562{
2563 struct kvm_stats_debugfs_item *p;
2564
2565 for (p = debugfs_entries; p->name; ++p)
2566 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002567 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568}
2569
Avi Kivity59ae6c62007-02-12 00:54:48 -08002570static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2571{
Avi Kivity4267c412007-05-24 13:09:41 +03002572 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002573 return 0;
2574}
2575
2576static int kvm_resume(struct sys_device *dev)
2577{
Avi Kivity4267c412007-05-24 13:09:41 +03002578 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002579 return 0;
2580}
2581
2582static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002583 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002584 .suspend = kvm_suspend,
2585 .resume = kvm_resume,
2586};
2587
2588static struct sys_device kvm_sysdev = {
2589 .id = 0,
2590 .cls = &kvm_sysdev_class,
2591};
2592
Izik Eiduscea7bb22007-10-17 19:17:48 +02002593struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002594pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595
Avi Kivity15ad7142007-07-11 18:17:21 +03002596static inline
2597struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2598{
2599 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2600}
2601
2602static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2603{
2604 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2605
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002606 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002607}
2608
2609static void kvm_sched_out(struct preempt_notifier *pn,
2610 struct task_struct *next)
2611{
2612 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2613
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002614 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002615}
2616
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002617int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002618 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619{
2620 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002621 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002623 kvm_init_debug();
2624
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002625 r = kvm_arch_init(opaque);
2626 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002627 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002628
2629 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2630
2631 if (bad_page == NULL) {
2632 r = -ENOMEM;
2633 goto out;
2634 }
2635
Anthony Liguori35149e22008-04-02 14:46:56 -05002636 bad_pfn = page_to_pfn(bad_page);
2637
Avi Kivity8437a612009-06-06 14:52:35 -07002638 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302639 r = -ENOMEM;
2640 goto out_free_0;
2641 }
2642
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002643 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302645 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646
Yang, Sheng002c7f72007-07-31 14:23:01 +03002647 for_each_online_cpu(cpu) {
2648 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002649 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002650 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002651 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002652 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002653 }
2654
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002655 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002656 r = register_cpu_notifier(&kvm_cpu_notifier);
2657 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002658 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 register_reboot_notifier(&kvm_reboot_notifier);
2660
Avi Kivity59ae6c62007-02-12 00:54:48 -08002661 r = sysdev_class_register(&kvm_sysdev_class);
2662 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002663 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002664
2665 r = sysdev_register(&kvm_sysdev);
2666 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002667 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002668
Rusty Russellc16f8622007-07-30 21:12:19 +10002669 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2670 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002671 __alignof__(struct kvm_vcpu),
2672 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002673 if (!kvm_vcpu_cache) {
2674 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002675 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002676 }
2677
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002679 kvm_vm_fops.owner = module;
2680 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681
2682 r = misc_register(&kvm_dev);
2683 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002684 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685 goto out_free;
2686 }
2687
Avi Kivity15ad7142007-07-11 18:17:21 +03002688 kvm_preempt_ops.sched_in = kvm_sched_in;
2689 kvm_preempt_ops.sched_out = kvm_sched_out;
2690
Avi Kivityc7addb92007-09-16 18:58:32 +02002691 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692
2693out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002694 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002695out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002696 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002697out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002698 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002699out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002701 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002702out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002703 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002704out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002705 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302706out_free_0a:
2707 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002708out_free_0:
2709 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002710out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002711 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002712 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002713out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 return r;
2715}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002716EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002718void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002720 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002722 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002723 sysdev_unregister(&kvm_sysdev);
2724 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002726 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002727 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002728 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002729 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302731 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002732 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002734EXPORT_SYMBOL_GPL(kvm_exit);