blob: e7644b90667e923ddd456dc82d51e8b94ef1e341 [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/io.h>
47#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020048#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Sheng Yangf64769e2008-11-24 14:32:55 +080050#ifdef CONFIG_X86
51#include <asm/msidef.h>
52#endif
53
Laurent Vivier5f94c172008-05-30 16:05:54 +020054#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
55#include "coalesced_mmio.h"
56#endif
57
Xiantao Zhang8a98f662008-10-06 13:47:38 +080058#ifdef KVM_CAP_DEVICE_ASSIGNMENT
59#include <linux/pci.h>
60#include <linux/interrupt.h>
61#include "irq.h"
62#endif
63
Avi Kivity6aa8b732006-12-10 02:21:36 -080064MODULE_AUTHOR("Qumranet");
65MODULE_LICENSE("GPL");
66
Sheng Yang5319c662008-11-24 14:32:57 +080067static int msi2intx = 1;
68module_param(msi2intx, bool, 0);
69
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080070DEFINE_SPINLOCK(kvm_lock);
71LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080072
Rusty Russell7f59f492008-12-07 21:25:45 +103073static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030074
Rusty Russellc16f8622007-07-30 21:12:19 +100075struct kmem_cache *kvm_vcpu_cache;
76EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030077
Avi Kivity15ad7142007-07-11 18:17:21 +030078static __read_mostly struct preempt_ops kvm_preempt_ops;
79
Hollis Blanchard76f7c872008-04-15 16:05:42 -050080struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080081
Avi Kivitybccf2152007-02-21 18:04:26 +020082static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
83 unsigned long arg);
84
Hannes Edere8ba5d32008-11-28 17:02:42 +010085static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030086
Xiantao Zhang8a98f662008-10-06 13:47:38 +080087#ifdef KVM_CAP_DEVICE_ASSIGNMENT
Sheng Yangf64769e2008-11-24 14:32:55 +080088
89#ifdef CONFIG_X86
90static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev)
91{
92 int vcpu_id;
93 struct kvm_vcpu *vcpu;
94 struct kvm_ioapic *ioapic = ioapic_irqchip(dev->kvm);
95 int dest_id = (dev->guest_msi.address_lo & MSI_ADDR_DEST_ID_MASK)
96 >> MSI_ADDR_DEST_ID_SHIFT;
97 int vector = (dev->guest_msi.data & MSI_DATA_VECTOR_MASK)
98 >> MSI_DATA_VECTOR_SHIFT;
99 int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
100 (unsigned long *)&dev->guest_msi.address_lo);
101 int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
102 (unsigned long *)&dev->guest_msi.data);
103 int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
104 (unsigned long *)&dev->guest_msi.data);
105 u32 deliver_bitmask;
106
107 BUG_ON(!ioapic);
108
109 deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
110 dest_id, dest_mode);
111 /* IOAPIC delivery mode value is the same as MSI here */
112 switch (delivery_mode) {
113 case IOAPIC_LOWEST_PRIORITY:
114 vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
115 deliver_bitmask);
116 if (vcpu != NULL)
117 kvm_apic_set_irq(vcpu, vector, trig_mode);
118 else
119 printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
120 break;
121 case IOAPIC_FIXED:
122 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
123 if (!(deliver_bitmask & (1 << vcpu_id)))
124 continue;
125 deliver_bitmask &= ~(1 << vcpu_id);
126 vcpu = ioapic->kvm->vcpus[vcpu_id];
127 if (vcpu)
128 kvm_apic_set_irq(vcpu, vector, trig_mode);
129 }
130 break;
131 default:
132 printk(KERN_INFO "kvm: unsupported MSI delivery mode\n");
133 }
134}
135#else
136static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev) {}
137#endif
138
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800139static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
140 int assigned_dev_id)
141{
142 struct list_head *ptr;
143 struct kvm_assigned_dev_kernel *match;
144
145 list_for_each(ptr, head) {
146 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
147 if (match->assigned_dev_id == assigned_dev_id)
148 return match;
149 }
150 return NULL;
151}
152
153static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
154{
155 struct kvm_assigned_dev_kernel *assigned_dev;
156
157 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
158 interrupt_work);
159
160 /* This is taken to safely inject irq inside the guest. When
161 * the interrupt injection (or the ioapic code) uses a
162 * finer-grained lock, update this
163 */
164 mutex_lock(&assigned_dev->kvm->lock);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800165 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_INTX)
166 kvm_set_irq(assigned_dev->kvm,
167 assigned_dev->irq_source_id,
168 assigned_dev->guest_irq, 1);
169 else if (assigned_dev->irq_requested_type &
170 KVM_ASSIGNED_DEV_GUEST_MSI) {
171 assigned_device_msi_dispatch(assigned_dev);
172 enable_irq(assigned_dev->host_irq);
173 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800174 mutex_unlock(&assigned_dev->kvm->lock);
175 kvm_put_kvm(assigned_dev->kvm);
176}
177
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800178static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
179{
180 struct kvm_assigned_dev_kernel *assigned_dev =
181 (struct kvm_assigned_dev_kernel *) dev_id;
182
183 kvm_get_kvm(assigned_dev->kvm);
184 schedule_work(&assigned_dev->interrupt_work);
185 disable_irq_nosync(irq);
186 return IRQ_HANDLED;
187}
188
189/* Ack the irq line for an assigned device */
190static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
191{
192 struct kvm_assigned_dev_kernel *dev;
193
194 if (kian->gsi == -1)
195 return;
196
197 dev = container_of(kian, struct kvm_assigned_dev_kernel,
198 ack_notifier);
Sheng Yang5550af42008-10-15 20:15:06 +0800199 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800200 enable_irq(dev->host_irq);
201}
202
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000203static void kvm_free_assigned_irq(struct kvm *kvm,
204 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800205{
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000206 if (!irqchip_in_kernel(kvm))
207 return;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800208
Sheng Yange19e30e2008-10-20 16:07:10 +0800209 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000210
211 if (assigned_dev->irq_source_id != -1)
212 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
213 assigned_dev->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800214
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000215 if (!assigned_dev->irq_requested_type)
216 return;
217
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800218 if (cancel_work_sync(&assigned_dev->interrupt_work))
219 /* We had pending work. That means we will have to take
220 * care of kvm_put_kvm.
221 */
222 kvm_put_kvm(kvm);
223
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000224 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
225
226 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
227 pci_disable_msi(assigned_dev->dev);
228
229 assigned_dev->irq_requested_type = 0;
230}
231
232
233static void kvm_free_assigned_device(struct kvm *kvm,
234 struct kvm_assigned_dev_kernel
235 *assigned_dev)
236{
237 kvm_free_assigned_irq(kvm, assigned_dev);
238
Sheng Yang6eb55812008-10-31 12:37:41 +0800239 pci_reset_function(assigned_dev->dev);
240
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800241 pci_release_regions(assigned_dev->dev);
242 pci_disable_device(assigned_dev->dev);
243 pci_dev_put(assigned_dev->dev);
244
245 list_del(&assigned_dev->list);
246 kfree(assigned_dev);
247}
248
249void kvm_free_all_assigned_devices(struct kvm *kvm)
250{
251 struct list_head *ptr, *ptr2;
252 struct kvm_assigned_dev_kernel *assigned_dev;
253
254 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
255 assigned_dev = list_entry(ptr,
256 struct kvm_assigned_dev_kernel,
257 list);
258
259 kvm_free_assigned_device(kvm, assigned_dev);
260 }
261}
262
Sheng Yang00e3ed32008-11-24 14:32:50 +0800263static int assigned_device_update_intx(struct kvm *kvm,
264 struct kvm_assigned_dev_kernel *adev,
265 struct kvm_assigned_irq *airq)
266{
Sheng Yangfbac7812008-11-24 14:32:52 +0800267 adev->guest_irq = airq->guest_irq;
268 adev->ack_notifier.gsi = airq->guest_irq;
269
270 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800271 return 0;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800272
273 if (irqchip_in_kernel(kvm)) {
Sheng Yang5319c662008-11-24 14:32:57 +0800274 if (!msi2intx &&
275 adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) {
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800276 free_irq(adev->host_irq, (void *)kvm);
277 pci_disable_msi(adev->dev);
278 }
279
Sheng Yang00e3ed32008-11-24 14:32:50 +0800280 if (!capable(CAP_SYS_RAWIO))
281 return -EPERM;
282
283 if (airq->host_irq)
284 adev->host_irq = airq->host_irq;
285 else
286 adev->host_irq = adev->dev->irq;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800287
288 /* Even though this is PCI, we don't want to use shared
289 * interrupts. Sharing host devices with guest-assigned devices
290 * on the same interrupt line is not a happy situation: there
291 * are going to be long delays in accepting, acking, etc.
292 */
293 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
294 0, "kvm_assigned_intx_device", (void *)adev))
295 return -EIO;
296 }
297
Sheng Yang4f906c12008-11-24 14:32:51 +0800298 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
299 KVM_ASSIGNED_DEV_HOST_INTX;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800300 return 0;
301}
302
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800303#ifdef CONFIG_X86
304static int assigned_device_update_msi(struct kvm *kvm,
305 struct kvm_assigned_dev_kernel *adev,
306 struct kvm_assigned_irq *airq)
307{
308 int r;
309
Sheng Yang5319c662008-11-24 14:32:57 +0800310 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
311 /* x86 don't care upper address of guest msi message addr */
312 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
313 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
314 adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
315 adev->guest_msi.data = airq->guest_msi.data;
316 adev->ack_notifier.gsi = -1;
317 } else if (msi2intx) {
318 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
319 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
320 adev->guest_irq = airq->guest_irq;
321 adev->ack_notifier.gsi = airq->guest_irq;
322 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800323
324 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
325 return 0;
326
327 if (irqchip_in_kernel(kvm)) {
Sheng Yang5319c662008-11-24 14:32:57 +0800328 if (!msi2intx) {
329 if (adev->irq_requested_type &
330 KVM_ASSIGNED_DEV_HOST_INTX)
331 free_irq(adev->host_irq, (void *)adev);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800332
Sheng Yang5319c662008-11-24 14:32:57 +0800333 r = pci_enable_msi(adev->dev);
334 if (r)
335 return r;
336 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800337
338 adev->host_irq = adev->dev->irq;
339 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
340 "kvm_assigned_msi_device", (void *)adev))
341 return -EIO;
342 }
343
Sheng Yang5319c662008-11-24 14:32:57 +0800344 if (!msi2intx)
345 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
346
347 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800348 return 0;
349}
350#endif
351
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800352static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
353 struct kvm_assigned_irq
354 *assigned_irq)
355{
356 int r = 0;
357 struct kvm_assigned_dev_kernel *match;
358
359 mutex_lock(&kvm->lock);
360
361 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
362 assigned_irq->assigned_dev_id);
363 if (!match) {
364 mutex_unlock(&kvm->lock);
365 return -EINVAL;
366 }
367
Sheng Yang4f906c12008-11-24 14:32:51 +0800368 if (!match->irq_requested_type) {
Sheng Yang342ffb92008-11-24 14:32:49 +0800369 INIT_WORK(&match->interrupt_work,
370 kvm_assigned_dev_interrupt_work_handler);
371 if (irqchip_in_kernel(kvm)) {
372 /* Register ack nofitier */
373 match->ack_notifier.gsi = -1;
374 match->ack_notifier.irq_acked =
375 kvm_assigned_dev_ack_irq;
376 kvm_register_irq_ack_notifier(kvm,
377 &match->ack_notifier);
378
379 /* Request IRQ source ID */
380 r = kvm_request_irq_source_id(kvm);
381 if (r < 0)
382 goto out_release;
383 else
384 match->irq_source_id = r;
Sheng Yang5319c662008-11-24 14:32:57 +0800385
386#ifdef CONFIG_X86
387 /* Determine host device irq type, we can know the
388 * result from dev->msi_enabled */
389 if (msi2intx)
390 pci_enable_msi(match->dev);
391#endif
Sheng Yang342ffb92008-11-24 14:32:49 +0800392 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800393 }
394
Sheng Yang5319c662008-11-24 14:32:57 +0800395 if ((!msi2intx &&
396 (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI)) ||
397 (msi2intx && match->dev->msi_enabled)) {
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800398#ifdef CONFIG_X86
399 r = assigned_device_update_msi(kvm, match, assigned_irq);
400 if (r) {
401 printk(KERN_WARNING "kvm: failed to enable "
402 "MSI device!\n");
403 goto out_release;
404 }
405#else
406 r = -ENOTTY;
407#endif
408 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
409 /* Host device IRQ 0 means don't support INTx */
Sheng Yang5319c662008-11-24 14:32:57 +0800410 if (!msi2intx) {
411 printk(KERN_WARNING
412 "kvm: wait device to enable MSI!\n");
413 r = 0;
414 } else {
415 printk(KERN_WARNING
416 "kvm: failed to enable MSI device!\n");
417 r = -ENOTTY;
418 goto out_release;
419 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800420 } else {
421 /* Non-sharing INTx mode */
422 r = assigned_device_update_intx(kvm, match, assigned_irq);
423 if (r) {
424 printk(KERN_WARNING "kvm: failed to enable "
425 "INTx device!\n");
426 goto out_release;
427 }
428 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800429
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800430 mutex_unlock(&kvm->lock);
431 return r;
432out_release:
433 mutex_unlock(&kvm->lock);
434 kvm_free_assigned_device(kvm, match);
435 return r;
436}
437
438static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
439 struct kvm_assigned_pci_dev *assigned_dev)
440{
441 int r = 0;
442 struct kvm_assigned_dev_kernel *match;
443 struct pci_dev *dev;
444
445 mutex_lock(&kvm->lock);
446
447 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
448 assigned_dev->assigned_dev_id);
449 if (match) {
450 /* device already assigned */
451 r = -EINVAL;
452 goto out;
453 }
454
455 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
456 if (match == NULL) {
457 printk(KERN_INFO "%s: Couldn't allocate memory\n",
458 __func__);
459 r = -ENOMEM;
460 goto out;
461 }
462 dev = pci_get_bus_and_slot(assigned_dev->busnr,
463 assigned_dev->devfn);
464 if (!dev) {
465 printk(KERN_INFO "%s: host device not found\n", __func__);
466 r = -EINVAL;
467 goto out_free;
468 }
469 if (pci_enable_device(dev)) {
470 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
471 r = -EBUSY;
472 goto out_put;
473 }
474 r = pci_request_regions(dev, "kvm_assigned_device");
475 if (r) {
476 printk(KERN_INFO "%s: Could not get access to device regions\n",
477 __func__);
478 goto out_disable;
479 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800480
481 pci_reset_function(dev);
482
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800483 match->assigned_dev_id = assigned_dev->assigned_dev_id;
484 match->host_busnr = assigned_dev->busnr;
485 match->host_devfn = assigned_dev->devfn;
486 match->dev = dev;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000487 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800488 match->kvm = kvm;
489
490 list_add(&match->list, &kvm->arch.assigned_dev_head);
491
492 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
493 r = kvm_iommu_map_guest(kvm, match);
494 if (r)
495 goto out_list_del;
496 }
497
498out:
499 mutex_unlock(&kvm->lock);
500 return r;
501out_list_del:
502 list_del(&match->list);
503 pci_release_regions(dev);
504out_disable:
505 pci_disable_device(dev);
506out_put:
507 pci_dev_put(dev);
508out_free:
509 kfree(match);
510 mutex_unlock(&kvm->lock);
511 return r;
512}
513#endif
514
James Morris5aacf0c2006-12-22 01:04:55 -0800515static inline int valid_vcpu(int n)
516{
517 return likely(n >= 0 && n < KVM_MAX_VCPUS);
518}
519
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800520inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300521{
522 if (pfn_valid(pfn))
523 return PageReserved(pfn_to_page(pfn));
524
525 return true;
526}
527
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528/*
529 * Switches to specified vcpu, until a matching vcpu_put()
530 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200531void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532{
Avi Kivity15ad7142007-07-11 18:17:21 +0300533 int cpu;
534
Avi Kivitybccf2152007-02-21 18:04:26 +0200535 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300536 cpu = get_cpu();
537 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200538 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300539 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200540}
541
Carsten Otte313a3dc2007-10-11 19:16:52 +0200542void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543{
Avi Kivity15ad7142007-07-11 18:17:21 +0300544 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200545 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300546 preempt_notifier_unregister(&vcpu->preempt_notifier);
547 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 mutex_unlock(&vcpu->mutex);
549}
550
Avi Kivityd9e368d2007-06-07 19:18:30 +0300551static void ack_flush(void *_completed)
552{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300553}
554
Rusty Russell49846892008-12-08 20:26:24 +1030555static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300556{
Avi Kivity597a5f52008-07-20 14:24:22 +0300557 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030558 cpumask_var_t cpus;
559 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300560 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300561
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030562 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
563 cpumask_clear(cpus);
564
Avi Kivity597a5f52008-07-20 14:24:22 +0300565 me = get_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000566 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
567 vcpu = kvm->vcpus[i];
568 if (!vcpu)
569 continue;
Rusty Russell49846892008-12-08 20:26:24 +1030570 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300571 continue;
572 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030573 if (cpus != NULL && cpu != -1 && cpu != me)
574 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300575 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030576 if (unlikely(cpus == NULL))
577 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
578 else if (!cpumask_empty(cpus))
579 smp_call_function_many(cpus, ack_flush, NULL, 1);
580 else
581 called = false;
Avi Kivity597a5f52008-07-20 14:24:22 +0300582 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030583 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030584 return called;
585}
586
587void kvm_flush_remote_tlbs(struct kvm *kvm)
588{
589 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
590 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300591}
592
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500593void kvm_reload_remote_mmus(struct kvm *kvm)
594{
Rusty Russell49846892008-12-08 20:26:24 +1030595 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500596}
597
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000598int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
599{
600 struct page *page;
601 int r;
602
603 mutex_init(&vcpu->mutex);
604 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000605 vcpu->kvm = kvm;
606 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300607 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000608
609 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
610 if (!page) {
611 r = -ENOMEM;
612 goto fail;
613 }
614 vcpu->run = page_address(page);
615
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800616 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000617 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800618 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000619 return 0;
620
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000621fail_free_run:
622 free_page((unsigned long)vcpu->run);
623fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000624 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000625}
626EXPORT_SYMBOL_GPL(kvm_vcpu_init);
627
628void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
629{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800630 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000631 free_page((unsigned long)vcpu->run);
632}
633EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
634
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200635#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
636static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
637{
638 return container_of(mn, struct kvm, mmu_notifier);
639}
640
641static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
642 struct mm_struct *mm,
643 unsigned long address)
644{
645 struct kvm *kvm = mmu_notifier_to_kvm(mn);
646 int need_tlb_flush;
647
648 /*
649 * When ->invalidate_page runs, the linux pte has been zapped
650 * already but the page is still allocated until
651 * ->invalidate_page returns. So if we increase the sequence
652 * here the kvm page fault will notice if the spte can't be
653 * established because the page is going to be freed. If
654 * instead the kvm page fault establishes the spte before
655 * ->invalidate_page runs, kvm_unmap_hva will release it
656 * before returning.
657 *
658 * The sequence increase only need to be seen at spin_unlock
659 * time, and not at spin_lock time.
660 *
661 * Increasing the sequence after the spin_unlock would be
662 * unsafe because the kvm page fault could then establish the
663 * pte after kvm_unmap_hva returned, without noticing the page
664 * is going to be freed.
665 */
666 spin_lock(&kvm->mmu_lock);
667 kvm->mmu_notifier_seq++;
668 need_tlb_flush = kvm_unmap_hva(kvm, address);
669 spin_unlock(&kvm->mmu_lock);
670
671 /* we've to flush the tlb before the pages can be freed */
672 if (need_tlb_flush)
673 kvm_flush_remote_tlbs(kvm);
674
675}
676
677static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
678 struct mm_struct *mm,
679 unsigned long start,
680 unsigned long end)
681{
682 struct kvm *kvm = mmu_notifier_to_kvm(mn);
683 int need_tlb_flush = 0;
684
685 spin_lock(&kvm->mmu_lock);
686 /*
687 * The count increase must become visible at unlock time as no
688 * spte can be established without taking the mmu_lock and
689 * count is also read inside the mmu_lock critical section.
690 */
691 kvm->mmu_notifier_count++;
692 for (; start < end; start += PAGE_SIZE)
693 need_tlb_flush |= kvm_unmap_hva(kvm, start);
694 spin_unlock(&kvm->mmu_lock);
695
696 /* we've to flush the tlb before the pages can be freed */
697 if (need_tlb_flush)
698 kvm_flush_remote_tlbs(kvm);
699}
700
701static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
702 struct mm_struct *mm,
703 unsigned long start,
704 unsigned long end)
705{
706 struct kvm *kvm = mmu_notifier_to_kvm(mn);
707
708 spin_lock(&kvm->mmu_lock);
709 /*
710 * This sequence increase will notify the kvm page fault that
711 * the page that is going to be mapped in the spte could have
712 * been freed.
713 */
714 kvm->mmu_notifier_seq++;
715 /*
716 * The above sequence increase must be visible before the
717 * below count decrease but both values are read by the kvm
718 * page fault under mmu_lock spinlock so we don't need to add
719 * a smb_wmb() here in between the two.
720 */
721 kvm->mmu_notifier_count--;
722 spin_unlock(&kvm->mmu_lock);
723
724 BUG_ON(kvm->mmu_notifier_count < 0);
725}
726
727static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
728 struct mm_struct *mm,
729 unsigned long address)
730{
731 struct kvm *kvm = mmu_notifier_to_kvm(mn);
732 int young;
733
734 spin_lock(&kvm->mmu_lock);
735 young = kvm_age_hva(kvm, address);
736 spin_unlock(&kvm->mmu_lock);
737
738 if (young)
739 kvm_flush_remote_tlbs(kvm);
740
741 return young;
742}
743
744static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
745 .invalidate_page = kvm_mmu_notifier_invalidate_page,
746 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
747 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
748 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
749};
750#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
751
Avi Kivityf17abe92007-02-21 19:28:04 +0200752static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800754 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200755#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
756 struct page *page;
757#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800759 if (IS_ERR(kvm))
760 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761
Laurent Vivier5f94c172008-05-30 16:05:54 +0200762#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
763 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
764 if (!page) {
765 kfree(kvm);
766 return ERR_PTR(-ENOMEM);
767 }
768 kvm->coalesced_mmio_ring =
769 (struct kvm_coalesced_mmio_ring *)page_address(page);
770#endif
771
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200772#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
773 {
774 int err;
775 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
776 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
777 if (err) {
778#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
779 put_page(page);
780#endif
781 kfree(kvm);
782 return ERR_PTR(err);
783 }
784 }
785#endif
786
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200787 kvm->mm = current->mm;
788 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500789 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300790 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800791 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400792 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200793 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300794 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000795 spin_lock(&kvm_lock);
796 list_add(&kvm->vm_list, &vm_list);
797 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200798#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
799 kvm_coalesced_mmio_init(kvm);
800#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800801out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200802 return kvm;
803}
804
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805/*
806 * Free any memory in @free but not in @dont.
807 */
808static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
809 struct kvm_memory_slot *dont)
810{
Izik Eidus290fc382007-09-27 14:11:22 +0200811 if (!dont || free->rmap != dont->rmap)
812 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813
814 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
815 vfree(free->dirty_bitmap);
816
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300817 if (!dont || free->lpage_info != dont->lpage_info)
818 vfree(free->lpage_info);
819
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000821 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500822 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300823 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824}
825
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800826void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827{
828 int i;
829
830 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000831 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832}
833
Avi Kivityf17abe92007-02-21 19:28:04 +0200834static void kvm_destroy_vm(struct kvm *kvm)
835{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200836 struct mm_struct *mm = kvm->mm;
837
Avi Kivity133de902007-02-12 00:54:44 -0800838 spin_lock(&kvm_lock);
839 list_del(&kvm->vm_list);
840 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300841 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400842 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200843#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
844 if (kvm->coalesced_mmio_ring != NULL)
845 free_page((unsigned long)kvm->coalesced_mmio_ring);
846#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200847#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
848 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
849#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800850 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200851 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200852}
853
Izik Eidusd39f13b2008-03-30 16:01:25 +0300854void kvm_get_kvm(struct kvm *kvm)
855{
856 atomic_inc(&kvm->users_count);
857}
858EXPORT_SYMBOL_GPL(kvm_get_kvm);
859
860void kvm_put_kvm(struct kvm *kvm)
861{
862 if (atomic_dec_and_test(&kvm->users_count))
863 kvm_destroy_vm(kvm);
864}
865EXPORT_SYMBOL_GPL(kvm_put_kvm);
866
867
Avi Kivityf17abe92007-02-21 19:28:04 +0200868static int kvm_vm_release(struct inode *inode, struct file *filp)
869{
870 struct kvm *kvm = filp->private_data;
871
Izik Eidusd39f13b2008-03-30 16:01:25 +0300872 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 return 0;
874}
875
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 * Allocate some memory and give it an address in the guest physical address
878 * space.
879 *
880 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800881 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500882 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800884int __kvm_set_memory_region(struct kvm *kvm,
885 struct kvm_userspace_memory_region *mem,
886 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887{
888 int r;
889 gfn_t base_gfn;
890 unsigned long npages;
891 unsigned long i;
892 struct kvm_memory_slot *memslot;
893 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894
895 r = -EINVAL;
896 /* General sanity checks */
897 if (mem->memory_size & (PAGE_SIZE - 1))
898 goto out;
899 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
900 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800901 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600902 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200903 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 goto out;
905 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
906 goto out;
907
908 memslot = &kvm->memslots[mem->slot];
909 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
910 npages = mem->memory_size >> PAGE_SHIFT;
911
912 if (!npages)
913 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
914
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 new = old = *memslot;
916
917 new.base_gfn = base_gfn;
918 new.npages = npages;
919 new.flags = mem->flags;
920
921 /* Disallow changing a memory slot's size. */
922 r = -EINVAL;
923 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800924 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925
926 /* Check for overlaps */
927 r = -EEXIST;
928 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
929 struct kvm_memory_slot *s = &kvm->memslots[i];
930
931 if (s == memslot)
932 continue;
933 if (!((base_gfn + npages <= s->base_gfn) ||
934 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800935 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 /* Free page dirty bitmap if unneeded */
939 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000940 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941
942 r = -ENOMEM;
943
944 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200945#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500946 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400947 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200948
949 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800950 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200951
Izik Eidus290fc382007-09-27 14:11:22 +0200952 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500953
Izik Eidus80b14b52007-10-25 11:54:04 +0200954 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200955 /*
956 * hva_to_rmmap() serialzies with the mmu_lock and to be
957 * safe it has to ignore memslots with !user_alloc &&
958 * !userspace_addr.
959 */
960 if (user_alloc)
961 new.userspace_addr = mem->userspace_addr;
962 else
963 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300965 if (npages && !new.lpage_info) {
966 int largepages = npages / KVM_PAGES_PER_HPAGE;
967 if (npages % KVM_PAGES_PER_HPAGE)
968 largepages++;
969 if (base_gfn % KVM_PAGES_PER_HPAGE)
970 largepages++;
971
972 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
973
974 if (!new.lpage_info)
975 goto out_free;
976
977 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
978
979 if (base_gfn % KVM_PAGES_PER_HPAGE)
980 new.lpage_info[0].write_count = 1;
981 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
982 new.lpage_info[largepages-1].write_count = 1;
983 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984
985 /* Allocate page dirty bitmap if needed */
986 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
987 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
988
989 new.dirty_bitmap = vmalloc(dirty_bytes);
990 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800991 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 memset(new.dirty_bitmap, 0, dirty_bytes);
993 }
Carsten Otteeff01142008-06-27 15:05:31 +0200994#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300996 if (!npages)
997 kvm_arch_flush_shadow(kvm);
998
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200999 spin_lock(&kvm->mmu_lock);
1000 if (mem->slot >= kvm->nmemslots)
1001 kvm->nmemslots = mem->slot + 1;
1002
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001004 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005
Zhang Xiantao0de10342007-11-20 16:25:04 +08001006 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1007 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001008 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001009 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001010 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001011 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001012 }
1013
Glauber Costa6f897242008-12-03 13:40:51 -02001014 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1015 /* Slot deletion case: we have to update the current slot */
1016 if (!npages)
1017 *memslot = old;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001018#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001019 /* map the pages in iommu page table */
1020 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1021 if (r)
1022 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001023#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 return 0;
1025
Sheng Yangf78e0e22007-10-29 09:40:42 +08001026out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 kvm_free_physmem_slot(&new, &old);
1028out:
1029 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001030
1031}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001032EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1033
1034int kvm_set_memory_region(struct kvm *kvm,
1035 struct kvm_userspace_memory_region *mem,
1036 int user_alloc)
1037{
1038 int r;
1039
Izik Eidus72dc67a2008-02-10 18:04:15 +02001040 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001041 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001042 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001043 return r;
1044}
Izik Eidus210c7c42007-10-24 23:52:57 +02001045EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1046
Carsten Otte1fe779f2007-10-29 16:08:35 +01001047int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1048 struct
1049 kvm_userspace_memory_region *mem,
1050 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001051{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001052 if (mem->slot >= KVM_MEMORY_SLOTS)
1053 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001054 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055}
1056
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001057int kvm_get_dirty_log(struct kvm *kvm,
1058 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059{
1060 struct kvm_memory_slot *memslot;
1061 int r, i;
1062 int n;
1063 unsigned long any = 0;
1064
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 r = -EINVAL;
1066 if (log->slot >= KVM_MEMORY_SLOTS)
1067 goto out;
1068
1069 memslot = &kvm->memslots[log->slot];
1070 r = -ENOENT;
1071 if (!memslot->dirty_bitmap)
1072 goto out;
1073
Uri Lublincd1a4a92007-02-22 16:43:09 +02001074 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075
Uri Lublincd1a4a92007-02-22 16:43:09 +02001076 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 any = memslot->dirty_bitmap[i];
1078
1079 r = -EFAULT;
1080 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1081 goto out;
1082
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001083 if (any)
1084 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085
1086 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 return r;
1089}
1090
Izik Eiduscea7bb22007-10-17 19:17:48 +02001091int is_error_page(struct page *page)
1092{
1093 return page == bad_page;
1094}
1095EXPORT_SYMBOL_GPL(is_error_page);
1096
Anthony Liguori35149e22008-04-02 14:46:56 -05001097int is_error_pfn(pfn_t pfn)
1098{
1099 return pfn == bad_pfn;
1100}
1101EXPORT_SYMBOL_GPL(is_error_pfn);
1102
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001103static inline unsigned long bad_hva(void)
1104{
1105 return PAGE_OFFSET;
1106}
1107
1108int kvm_is_error_hva(unsigned long addr)
1109{
1110 return addr == bad_hva();
1111}
1112EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1113
Izik Eidus28430992008-10-03 17:40:32 +03001114struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115{
1116 int i;
1117
1118 for (i = 0; i < kvm->nmemslots; ++i) {
1119 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1120
1121 if (gfn >= memslot->base_gfn
1122 && gfn < memslot->base_gfn + memslot->npages)
1123 return memslot;
1124 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001125 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126}
Izik Eidus28430992008-10-03 17:40:32 +03001127EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001128
1129struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1130{
1131 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001132 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001133}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134
Izik Eiduse0d62c72007-10-24 23:57:46 +02001135int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1136{
1137 int i;
1138
1139 gfn = unalias_gfn(kvm, gfn);
1140 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1141 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1142
1143 if (gfn >= memslot->base_gfn
1144 && gfn < memslot->base_gfn + memslot->npages)
1145 return 1;
1146 }
1147 return 0;
1148}
1149EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1150
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001151unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001152{
1153 struct kvm_memory_slot *slot;
1154
1155 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001156 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001157 if (!slot)
1158 return bad_hva();
1159 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1160}
Sheng Yang0d150292008-04-25 21:44:50 +08001161EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001162
Anthony Liguori35149e22008-04-02 14:46:56 -05001163pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001164{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001165 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001166 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001167 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001168 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001169
Avi Kivity60395222007-10-21 11:03:36 +02001170 might_sleep();
1171
Izik Eidus539cb662007-11-11 22:05:04 +02001172 addr = gfn_to_hva(kvm, gfn);
1173 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001174 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001175 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001176 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001177
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001178 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001179
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001180 if (unlikely(npages != 1)) {
1181 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001182
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001183 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001184 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001185
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001186 if (vma == NULL || addr < vma->vm_start ||
1187 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001188 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001189 get_page(bad_page);
1190 return page_to_pfn(bad_page);
1191 }
1192
1193 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001194 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001195 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001196 } else
1197 pfn = page_to_pfn(page[0]);
1198
1199 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001200}
1201
1202EXPORT_SYMBOL_GPL(gfn_to_pfn);
1203
1204struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1205{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001206 pfn_t pfn;
1207
1208 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001209 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001210 return pfn_to_page(pfn);
1211
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001212 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001213
1214 get_page(bad_page);
1215 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001216}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001217
Avi Kivity954bbbc2007-03-30 14:02:32 +03001218EXPORT_SYMBOL_GPL(gfn_to_page);
1219
Izik Eidusb4231d62007-11-20 11:49:33 +02001220void kvm_release_page_clean(struct page *page)
1221{
Anthony Liguori35149e22008-04-02 14:46:56 -05001222 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001223}
1224EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1225
Anthony Liguori35149e22008-04-02 14:46:56 -05001226void kvm_release_pfn_clean(pfn_t pfn)
1227{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001228 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001229 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001230}
1231EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1232
Izik Eidusb4231d62007-11-20 11:49:33 +02001233void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001234{
Anthony Liguori35149e22008-04-02 14:46:56 -05001235 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001236}
Izik Eidusb4231d62007-11-20 11:49:33 +02001237EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001238
Anthony Liguori35149e22008-04-02 14:46:56 -05001239void kvm_release_pfn_dirty(pfn_t pfn)
1240{
1241 kvm_set_pfn_dirty(pfn);
1242 kvm_release_pfn_clean(pfn);
1243}
1244EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1245
1246void kvm_set_page_dirty(struct page *page)
1247{
1248 kvm_set_pfn_dirty(page_to_pfn(page));
1249}
1250EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1251
1252void kvm_set_pfn_dirty(pfn_t pfn)
1253{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001254 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001255 struct page *page = pfn_to_page(pfn);
1256 if (!PageReserved(page))
1257 SetPageDirty(page);
1258 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001259}
1260EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1261
1262void kvm_set_pfn_accessed(pfn_t pfn)
1263{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001264 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001265 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001266}
1267EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1268
1269void kvm_get_pfn(pfn_t pfn)
1270{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001271 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001272 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001273}
1274EXPORT_SYMBOL_GPL(kvm_get_pfn);
1275
Izik Eidus195aefd2007-10-01 22:14:18 +02001276static int next_segment(unsigned long len, int offset)
1277{
1278 if (len > PAGE_SIZE - offset)
1279 return PAGE_SIZE - offset;
1280 else
1281 return len;
1282}
1283
1284int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1285 int len)
1286{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001287 int r;
1288 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001289
Izik Eiduse0506bc2007-11-11 22:10:22 +02001290 addr = gfn_to_hva(kvm, gfn);
1291 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001292 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001293 r = copy_from_user(data, (void __user *)addr + offset, len);
1294 if (r)
1295 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001296 return 0;
1297}
1298EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1299
1300int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1301{
1302 gfn_t gfn = gpa >> PAGE_SHIFT;
1303 int seg;
1304 int offset = offset_in_page(gpa);
1305 int ret;
1306
1307 while ((seg = next_segment(len, offset)) != 0) {
1308 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1309 if (ret < 0)
1310 return ret;
1311 offset = 0;
1312 len -= seg;
1313 data += seg;
1314 ++gfn;
1315 }
1316 return 0;
1317}
1318EXPORT_SYMBOL_GPL(kvm_read_guest);
1319
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001320int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1321 unsigned long len)
1322{
1323 int r;
1324 unsigned long addr;
1325 gfn_t gfn = gpa >> PAGE_SHIFT;
1326 int offset = offset_in_page(gpa);
1327
1328 addr = gfn_to_hva(kvm, gfn);
1329 if (kvm_is_error_hva(addr))
1330 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001331 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001332 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001333 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001334 if (r)
1335 return -EFAULT;
1336 return 0;
1337}
1338EXPORT_SYMBOL(kvm_read_guest_atomic);
1339
Izik Eidus195aefd2007-10-01 22:14:18 +02001340int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1341 int offset, int len)
1342{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001343 int r;
1344 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001345
Izik Eiduse0506bc2007-11-11 22:10:22 +02001346 addr = gfn_to_hva(kvm, gfn);
1347 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001348 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001349 r = copy_to_user((void __user *)addr + offset, data, len);
1350 if (r)
1351 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001352 mark_page_dirty(kvm, gfn);
1353 return 0;
1354}
1355EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1356
1357int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1358 unsigned long len)
1359{
1360 gfn_t gfn = gpa >> PAGE_SHIFT;
1361 int seg;
1362 int offset = offset_in_page(gpa);
1363 int ret;
1364
1365 while ((seg = next_segment(len, offset)) != 0) {
1366 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1367 if (ret < 0)
1368 return ret;
1369 offset = 0;
1370 len -= seg;
1371 data += seg;
1372 ++gfn;
1373 }
1374 return 0;
1375}
1376
1377int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1378{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001379 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001380}
1381EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1382
1383int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1384{
1385 gfn_t gfn = gpa >> PAGE_SHIFT;
1386 int seg;
1387 int offset = offset_in_page(gpa);
1388 int ret;
1389
1390 while ((seg = next_segment(len, offset)) != 0) {
1391 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1392 if (ret < 0)
1393 return ret;
1394 offset = 0;
1395 len -= seg;
1396 ++gfn;
1397 }
1398 return 0;
1399}
1400EXPORT_SYMBOL_GPL(kvm_clear_guest);
1401
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1403{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001404 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405
Uri Lublin3b6fff12007-10-30 10:42:09 +02001406 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001407 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001408 if (memslot && memslot->dirty_bitmap) {
1409 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410
Rusty Russell7e9d6192007-07-31 20:41:14 +10001411 /* avoid RMW */
1412 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1413 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414 }
1415}
1416
Eddie Dongb6958ce2007-07-18 12:15:21 +03001417/*
1418 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1419 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001420void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001421{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001422 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001423
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001424 for (;;) {
1425 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001426
Marcelo Tosattid7690172008-09-08 15:23:48 -03001427 if (kvm_cpu_has_interrupt(vcpu) ||
1428 kvm_cpu_has_pending_timer(vcpu) ||
1429 kvm_arch_vcpu_runnable(vcpu)) {
1430 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001431 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001432 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001433 if (signal_pending(current))
1434 break;
1435
Eddie Dongb6958ce2007-07-18 12:15:21 +03001436 vcpu_put(vcpu);
1437 schedule();
1438 vcpu_load(vcpu);
1439 }
1440
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001441 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001442}
1443
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444void kvm_resched(struct kvm_vcpu *vcpu)
1445{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001446 if (!need_resched())
1447 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449}
1450EXPORT_SYMBOL_GPL(kvm_resched);
1451
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001452static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001453{
1454 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001455 struct page *page;
1456
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001457 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001458 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001459#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001460 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001461 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001462#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001463#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1464 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1465 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1466#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001467 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001468 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001469 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001470 vmf->page = page;
1471 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001472}
1473
1474static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001475 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001476};
1477
1478static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1479{
1480 vma->vm_ops = &kvm_vcpu_vm_ops;
1481 return 0;
1482}
1483
Avi Kivitybccf2152007-02-21 18:04:26 +02001484static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1485{
1486 struct kvm_vcpu *vcpu = filp->private_data;
1487
Al Viro66c0b392008-04-19 20:33:56 +01001488 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001489 return 0;
1490}
1491
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001492static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001493 .release = kvm_vcpu_release,
1494 .unlocked_ioctl = kvm_vcpu_ioctl,
1495 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001496 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001497};
1498
1499/*
1500 * Allocates an inode for the vcpu.
1501 */
1502static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1503{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001504 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001505 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001506 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001507 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001508}
1509
Avi Kivityc5ea7662007-02-20 18:41:05 +02001510/*
1511 * Creates some virtual cpus. Good luck creating more than one.
1512 */
1513static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1514{
1515 int r;
1516 struct kvm_vcpu *vcpu;
1517
Avi Kivityc5ea7662007-02-20 18:41:05 +02001518 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001519 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001520
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001521 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001522 if (IS_ERR(vcpu))
1523 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001524
Avi Kivity15ad7142007-07-11 18:17:21 +03001525 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1526
Avi Kivity26e52152007-11-20 15:30:24 +02001527 r = kvm_arch_vcpu_setup(vcpu);
1528 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001529 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001530
Shaohua Li11ec2802007-07-23 14:51:37 +08001531 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001532 if (kvm->vcpus[n]) {
1533 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001534 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001535 }
1536 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001537 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001538
1539 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001540 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001541 r = create_vcpu_fd(vcpu);
1542 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001543 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001544 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001545
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001546unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001547 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001548 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001549vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001550 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001551 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001552 return r;
1553}
1554
Avi Kivity1961d272007-03-05 19:46:05 +02001555static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1556{
1557 if (sigset) {
1558 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1559 vcpu->sigset_active = 1;
1560 vcpu->sigset = *sigset;
1561 } else
1562 vcpu->sigset_active = 0;
1563 return 0;
1564}
1565
Avi Kivitybccf2152007-02-21 18:04:26 +02001566static long kvm_vcpu_ioctl(struct file *filp,
1567 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568{
Avi Kivitybccf2152007-02-21 18:04:26 +02001569 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001570 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001571 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001572 struct kvm_fpu *fpu = NULL;
1573 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001575 if (vcpu->kvm->mm != current->mm)
1576 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001578 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001579 r = -EINVAL;
1580 if (arg)
1581 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001582 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001585 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001587 r = -ENOMEM;
1588 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1589 if (!kvm_regs)
1590 goto out;
1591 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001593 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001595 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1596 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001598out_free1:
1599 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600 break;
1601 }
1602 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001603 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001605 r = -ENOMEM;
1606 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1607 if (!kvm_regs)
1608 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001610 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1611 goto out_free2;
1612 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001614 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001616out_free2:
1617 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 break;
1619 }
1620 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001621 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1622 r = -ENOMEM;
1623 if (!kvm_sregs)
1624 goto out;
1625 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 if (r)
1627 goto out;
1628 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001629 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630 goto out;
1631 r = 0;
1632 break;
1633 }
1634 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001635 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1636 r = -ENOMEM;
1637 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001639 r = -EFAULT;
1640 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1641 goto out;
1642 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 if (r)
1644 goto out;
1645 r = 0;
1646 break;
1647 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001648 case KVM_GET_MP_STATE: {
1649 struct kvm_mp_state mp_state;
1650
1651 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1652 if (r)
1653 goto out;
1654 r = -EFAULT;
1655 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1656 goto out;
1657 r = 0;
1658 break;
1659 }
1660 case KVM_SET_MP_STATE: {
1661 struct kvm_mp_state mp_state;
1662
1663 r = -EFAULT;
1664 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1665 goto out;
1666 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1667 if (r)
1668 goto out;
1669 r = 0;
1670 break;
1671 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 case KVM_TRANSLATE: {
1673 struct kvm_translation tr;
1674
1675 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001676 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001678 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 if (r)
1680 goto out;
1681 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001682 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 goto out;
1684 r = 0;
1685 break;
1686 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687 case KVM_DEBUG_GUEST: {
1688 struct kvm_debug_guest dbg;
1689
1690 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001691 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001693 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694 if (r)
1695 goto out;
1696 r = 0;
1697 break;
1698 }
Avi Kivity1961d272007-03-05 19:46:05 +02001699 case KVM_SET_SIGNAL_MASK: {
1700 struct kvm_signal_mask __user *sigmask_arg = argp;
1701 struct kvm_signal_mask kvm_sigmask;
1702 sigset_t sigset, *p;
1703
1704 p = NULL;
1705 if (argp) {
1706 r = -EFAULT;
1707 if (copy_from_user(&kvm_sigmask, argp,
1708 sizeof kvm_sigmask))
1709 goto out;
1710 r = -EINVAL;
1711 if (kvm_sigmask.len != sizeof sigset)
1712 goto out;
1713 r = -EFAULT;
1714 if (copy_from_user(&sigset, sigmask_arg->sigset,
1715 sizeof sigset))
1716 goto out;
1717 p = &sigset;
1718 }
1719 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1720 break;
1721 }
Avi Kivityb8836732007-04-01 16:34:31 +03001722 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001723 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1724 r = -ENOMEM;
1725 if (!fpu)
1726 goto out;
1727 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001728 if (r)
1729 goto out;
1730 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001731 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001732 goto out;
1733 r = 0;
1734 break;
1735 }
1736 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001737 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1738 r = -ENOMEM;
1739 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001740 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001741 r = -EFAULT;
1742 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1743 goto out;
1744 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001745 if (r)
1746 goto out;
1747 r = 0;
1748 break;
1749 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001750 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001751 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001752 }
1753out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001754 kfree(fpu);
1755 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001756 return r;
1757}
1758
1759static long kvm_vm_ioctl(struct file *filp,
1760 unsigned int ioctl, unsigned long arg)
1761{
1762 struct kvm *kvm = filp->private_data;
1763 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001764 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001765
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001766 if (kvm->mm != current->mm)
1767 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001768 switch (ioctl) {
1769 case KVM_CREATE_VCPU:
1770 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1771 if (r < 0)
1772 goto out;
1773 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001774 case KVM_SET_USER_MEMORY_REGION: {
1775 struct kvm_userspace_memory_region kvm_userspace_mem;
1776
1777 r = -EFAULT;
1778 if (copy_from_user(&kvm_userspace_mem, argp,
1779 sizeof kvm_userspace_mem))
1780 goto out;
1781
1782 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783 if (r)
1784 goto out;
1785 break;
1786 }
1787 case KVM_GET_DIRTY_LOG: {
1788 struct kvm_dirty_log log;
1789
1790 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001791 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001793 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 if (r)
1795 goto out;
1796 break;
1797 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001798#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1799 case KVM_REGISTER_COALESCED_MMIO: {
1800 struct kvm_coalesced_mmio_zone zone;
1801 r = -EFAULT;
1802 if (copy_from_user(&zone, argp, sizeof zone))
1803 goto out;
1804 r = -ENXIO;
1805 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1806 if (r)
1807 goto out;
1808 r = 0;
1809 break;
1810 }
1811 case KVM_UNREGISTER_COALESCED_MMIO: {
1812 struct kvm_coalesced_mmio_zone zone;
1813 r = -EFAULT;
1814 if (copy_from_user(&zone, argp, sizeof zone))
1815 goto out;
1816 r = -ENXIO;
1817 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1818 if (r)
1819 goto out;
1820 r = 0;
1821 break;
1822 }
1823#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001824#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1825 case KVM_ASSIGN_PCI_DEVICE: {
1826 struct kvm_assigned_pci_dev assigned_dev;
1827
1828 r = -EFAULT;
1829 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1830 goto out;
1831 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1832 if (r)
1833 goto out;
1834 break;
1835 }
1836 case KVM_ASSIGN_IRQ: {
1837 struct kvm_assigned_irq assigned_irq;
1838
1839 r = -EFAULT;
1840 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1841 goto out;
1842 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1843 if (r)
1844 goto out;
1845 break;
1846 }
1847#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001848 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001849 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001850 }
1851out:
1852 return r;
1853}
1854
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001855static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001856{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001857 struct page *page[1];
1858 unsigned long addr;
1859 int npages;
1860 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001861 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001862
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001863 addr = gfn_to_hva(kvm, gfn);
1864 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001865 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001866
1867 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1868 NULL);
1869 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001870 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001871
1872 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001873 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001874}
1875
1876static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001877 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001878};
1879
1880static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1881{
1882 vma->vm_ops = &kvm_vm_vm_ops;
1883 return 0;
1884}
1885
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001886static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001887 .release = kvm_vm_release,
1888 .unlocked_ioctl = kvm_vm_ioctl,
1889 .compat_ioctl = kvm_vm_ioctl,
1890 .mmap = kvm_vm_mmap,
1891};
1892
1893static int kvm_dev_ioctl_create_vm(void)
1894{
Al Viro2030a422008-02-23 06:46:49 -05001895 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001896 struct kvm *kvm;
1897
Avi Kivityf17abe92007-02-21 19:28:04 +02001898 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001899 if (IS_ERR(kvm))
1900 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001901 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001902 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001903 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001904
Avi Kivityf17abe92007-02-21 19:28:04 +02001905 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001906}
1907
1908static long kvm_dev_ioctl(struct file *filp,
1909 unsigned int ioctl, unsigned long arg)
1910{
Avi Kivity07c45a32007-03-07 13:05:38 +02001911 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001912
1913 switch (ioctl) {
1914 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001915 r = -EINVAL;
1916 if (arg)
1917 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001918 r = KVM_API_VERSION;
1919 break;
1920 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001921 r = -EINVAL;
1922 if (arg)
1923 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001924 r = kvm_dev_ioctl_create_vm();
1925 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001926 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001927 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001928 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001929 case KVM_GET_VCPU_MMAP_SIZE:
1930 r = -EINVAL;
1931 if (arg)
1932 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001933 r = PAGE_SIZE; /* struct kvm_run */
1934#ifdef CONFIG_X86
1935 r += PAGE_SIZE; /* pio data page */
1936#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001937#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1938 r += PAGE_SIZE; /* coalesced mmio ring page */
1939#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001940 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001941 case KVM_TRACE_ENABLE:
1942 case KVM_TRACE_PAUSE:
1943 case KVM_TRACE_DISABLE:
1944 r = kvm_trace_ioctl(ioctl, arg);
1945 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001947 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 }
1949out:
1950 return r;
1951}
1952
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 .unlocked_ioctl = kvm_dev_ioctl,
1955 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956};
1957
1958static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001959 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 "kvm",
1961 &kvm_chardev_ops,
1962};
1963
Avi Kivity1b6c0162007-05-24 13:03:52 +03001964static void hardware_enable(void *junk)
1965{
1966 int cpu = raw_smp_processor_id();
1967
Rusty Russell7f59f492008-12-07 21:25:45 +10301968 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001969 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301970 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001971 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001972}
1973
1974static void hardware_disable(void *junk)
1975{
1976 int cpu = raw_smp_processor_id();
1977
Rusty Russell7f59f492008-12-07 21:25:45 +10301978 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001979 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301980 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001981 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001982}
1983
Avi Kivity774c47f2007-02-12 00:54:47 -08001984static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1985 void *v)
1986{
1987 int cpu = (long)v;
1988
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001989 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001990 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001991 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001992 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1993 cpu);
1994 hardware_disable(NULL);
1995 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001996 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001997 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1998 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001999 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002000 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002001 case CPU_ONLINE:
2002 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2003 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002004 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002005 break;
2006 }
2007 return NOTIFY_OK;
2008}
2009
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002010
2011asmlinkage void kvm_handle_fault_on_reboot(void)
2012{
2013 if (kvm_rebooting)
2014 /* spin while reset goes on */
2015 while (true)
2016 ;
2017 /* Fault while not rebooting. We want the trace. */
2018 BUG();
2019}
2020EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2021
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002022static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002023 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002024{
2025 if (val == SYS_RESTART) {
2026 /*
2027 * Some (well, at least mine) BIOSes hang on reboot if
2028 * in vmx root mode.
2029 */
2030 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002031 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002032 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002033 }
2034 return NOTIFY_OK;
2035}
2036
2037static struct notifier_block kvm_reboot_notifier = {
2038 .notifier_call = kvm_reboot,
2039 .priority = 0,
2040};
2041
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002042void kvm_io_bus_init(struct kvm_io_bus *bus)
2043{
2044 memset(bus, 0, sizeof(*bus));
2045}
2046
2047void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2048{
2049 int i;
2050
2051 for (i = 0; i < bus->dev_count; i++) {
2052 struct kvm_io_device *pos = bus->devs[i];
2053
2054 kvm_iodevice_destructor(pos);
2055 }
2056}
2057
Laurent Vivier92760492008-05-30 16:05:53 +02002058struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2059 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002060{
2061 int i;
2062
2063 for (i = 0; i < bus->dev_count; i++) {
2064 struct kvm_io_device *pos = bus->devs[i];
2065
Laurent Vivier92760492008-05-30 16:05:53 +02002066 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002067 return pos;
2068 }
2069
2070 return NULL;
2071}
2072
2073void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2074{
2075 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2076
2077 bus->devs[bus->dev_count++] = dev;
2078}
2079
Avi Kivity774c47f2007-02-12 00:54:47 -08002080static struct notifier_block kvm_cpu_notifier = {
2081 .notifier_call = kvm_cpu_hotplug,
2082 .priority = 20, /* must be > scheduler priority */
2083};
2084
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002085static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002086{
2087 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002088 struct kvm *kvm;
2089
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002090 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002091 spin_lock(&kvm_lock);
2092 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002093 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002094 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002095 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002096}
2097
2098DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2099
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002100static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002101{
2102 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002103 struct kvm *kvm;
2104 struct kvm_vcpu *vcpu;
2105 int i;
2106
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002107 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002108 spin_lock(&kvm_lock);
2109 list_for_each_entry(kvm, &vm_list, vm_list)
2110 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002111 vcpu = kvm->vcpus[i];
2112 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002113 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002114 }
2115 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002116 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002117}
2118
Avi Kivityba1389b2007-11-18 16:24:12 +02002119DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2120
2121static struct file_operations *stat_fops[] = {
2122 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2123 [KVM_STAT_VM] = &vm_stat_fops,
2124};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002125
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002126static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127{
2128 struct kvm_stats_debugfs_item *p;
2129
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002130 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002132 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002133 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002134 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135}
2136
2137static void kvm_exit_debug(void)
2138{
2139 struct kvm_stats_debugfs_item *p;
2140
2141 for (p = debugfs_entries; p->name; ++p)
2142 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002143 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144}
2145
Avi Kivity59ae6c62007-02-12 00:54:48 -08002146static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2147{
Avi Kivity4267c412007-05-24 13:09:41 +03002148 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002149 return 0;
2150}
2151
2152static int kvm_resume(struct sys_device *dev)
2153{
Avi Kivity4267c412007-05-24 13:09:41 +03002154 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002155 return 0;
2156}
2157
2158static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002159 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002160 .suspend = kvm_suspend,
2161 .resume = kvm_resume,
2162};
2163
2164static struct sys_device kvm_sysdev = {
2165 .id = 0,
2166 .cls = &kvm_sysdev_class,
2167};
2168
Izik Eiduscea7bb22007-10-17 19:17:48 +02002169struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002170pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171
Avi Kivity15ad7142007-07-11 18:17:21 +03002172static inline
2173struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2174{
2175 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2176}
2177
2178static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2179{
2180 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2181
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002182 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002183}
2184
2185static void kvm_sched_out(struct preempt_notifier *pn,
2186 struct task_struct *next)
2187{
2188 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2189
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002190 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002191}
2192
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002193int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002194 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195{
2196 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002197 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002199 kvm_init_debug();
2200
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002201 r = kvm_arch_init(opaque);
2202 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002203 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002204
2205 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2206
2207 if (bad_page == NULL) {
2208 r = -ENOMEM;
2209 goto out;
2210 }
2211
Anthony Liguori35149e22008-04-02 14:46:56 -05002212 bad_pfn = page_to_pfn(bad_page);
2213
Rusty Russell7f59f492008-12-07 21:25:45 +10302214 if (!alloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2215 r = -ENOMEM;
2216 goto out_free_0;
2217 }
2218
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002219 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302221 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222
Yang, Sheng002c7f72007-07-31 14:23:01 +03002223 for_each_online_cpu(cpu) {
2224 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002225 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002226 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002227 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002228 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002229 }
2230
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002231 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002232 r = register_cpu_notifier(&kvm_cpu_notifier);
2233 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002234 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235 register_reboot_notifier(&kvm_reboot_notifier);
2236
Avi Kivity59ae6c62007-02-12 00:54:48 -08002237 r = sysdev_class_register(&kvm_sysdev_class);
2238 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002239 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002240
2241 r = sysdev_register(&kvm_sysdev);
2242 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002243 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002244
Rusty Russellc16f8622007-07-30 21:12:19 +10002245 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2246 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002247 __alignof__(struct kvm_vcpu),
2248 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002249 if (!kvm_vcpu_cache) {
2250 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002251 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002252 }
2253
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002255 kvm_vm_fops.owner = module;
2256 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257
2258 r = misc_register(&kvm_dev);
2259 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002260 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 goto out_free;
2262 }
2263
Avi Kivity15ad7142007-07-11 18:17:21 +03002264 kvm_preempt_ops.sched_in = kvm_sched_in;
2265 kvm_preempt_ops.sched_out = kvm_sched_out;
Sheng Yang5319c662008-11-24 14:32:57 +08002266#ifndef CONFIG_X86
2267 msi2intx = 0;
2268#endif
Avi Kivity15ad7142007-07-11 18:17:21 +03002269
Avi Kivityc7addb92007-09-16 18:58:32 +02002270 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271
2272out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002273 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002274out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002275 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002276out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002277 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002278out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002280 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002281out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002282 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002283out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002284 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302285out_free_0a:
2286 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002287out_free_0:
2288 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002289out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002290 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002291 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002292out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293 return r;
2294}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002295EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002297void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002299 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002301 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002302 sysdev_unregister(&kvm_sysdev);
2303 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002305 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002306 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002307 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002308 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302310 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002311 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002313EXPORT_SYMBOL_GPL(kvm_exit);