blob: e41d39d6b0efd8e33bfd035b9083fe23f7642973 [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
Avi Kivity1b6c0162007-05-24 13:03:52 +030073static cpumask_t cpus_hardware_enabled;
74
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 Yangfbac78182008-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
555void kvm_flush_remote_tlbs(struct kvm *kvm)
556{
Avi Kivity597a5f52008-07-20 14:24:22 +0300557 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300558 cpumask_t cpus;
559 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300560
Avi Kivity597a5f52008-07-20 14:24:22 +0300561 me = get_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300562 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000563 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
564 vcpu = kvm->vcpus[i];
565 if (!vcpu)
566 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200567 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300568 continue;
569 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300570 if (cpu != -1 && cpu != me)
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200571 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300572 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200573 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300574 goto out;
Avi Kivity0f74a242007-11-20 23:01:14 +0200575 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200576 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300577out:
578 put_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300579}
580
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500581void kvm_reload_remote_mmus(struct kvm *kvm)
582{
Avi Kivity597a5f52008-07-20 14:24:22 +0300583 int i, cpu, me;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500584 cpumask_t cpus;
585 struct kvm_vcpu *vcpu;
586
Avi Kivity597a5f52008-07-20 14:24:22 +0300587 me = get_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500588 cpus_clear(cpus);
589 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
590 vcpu = kvm->vcpus[i];
591 if (!vcpu)
592 continue;
593 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
594 continue;
595 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300596 if (cpu != -1 && cpu != me)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500597 cpu_set(cpu, cpus);
598 }
599 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300600 goto out;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500601 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300602out:
603 put_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500604}
605
606
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000607int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
608{
609 struct page *page;
610 int r;
611
612 mutex_init(&vcpu->mutex);
613 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000614 vcpu->kvm = kvm;
615 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300616 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000617
618 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
619 if (!page) {
620 r = -ENOMEM;
621 goto fail;
622 }
623 vcpu->run = page_address(page);
624
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800625 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000626 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800627 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000628 return 0;
629
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000630fail_free_run:
631 free_page((unsigned long)vcpu->run);
632fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000633 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000634}
635EXPORT_SYMBOL_GPL(kvm_vcpu_init);
636
637void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
638{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800639 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000640 free_page((unsigned long)vcpu->run);
641}
642EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
643
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200644#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
645static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
646{
647 return container_of(mn, struct kvm, mmu_notifier);
648}
649
650static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
651 struct mm_struct *mm,
652 unsigned long address)
653{
654 struct kvm *kvm = mmu_notifier_to_kvm(mn);
655 int need_tlb_flush;
656
657 /*
658 * When ->invalidate_page runs, the linux pte has been zapped
659 * already but the page is still allocated until
660 * ->invalidate_page returns. So if we increase the sequence
661 * here the kvm page fault will notice if the spte can't be
662 * established because the page is going to be freed. If
663 * instead the kvm page fault establishes the spte before
664 * ->invalidate_page runs, kvm_unmap_hva will release it
665 * before returning.
666 *
667 * The sequence increase only need to be seen at spin_unlock
668 * time, and not at spin_lock time.
669 *
670 * Increasing the sequence after the spin_unlock would be
671 * unsafe because the kvm page fault could then establish the
672 * pte after kvm_unmap_hva returned, without noticing the page
673 * is going to be freed.
674 */
675 spin_lock(&kvm->mmu_lock);
676 kvm->mmu_notifier_seq++;
677 need_tlb_flush = kvm_unmap_hva(kvm, address);
678 spin_unlock(&kvm->mmu_lock);
679
680 /* we've to flush the tlb before the pages can be freed */
681 if (need_tlb_flush)
682 kvm_flush_remote_tlbs(kvm);
683
684}
685
686static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
687 struct mm_struct *mm,
688 unsigned long start,
689 unsigned long end)
690{
691 struct kvm *kvm = mmu_notifier_to_kvm(mn);
692 int need_tlb_flush = 0;
693
694 spin_lock(&kvm->mmu_lock);
695 /*
696 * The count increase must become visible at unlock time as no
697 * spte can be established without taking the mmu_lock and
698 * count is also read inside the mmu_lock critical section.
699 */
700 kvm->mmu_notifier_count++;
701 for (; start < end; start += PAGE_SIZE)
702 need_tlb_flush |= kvm_unmap_hva(kvm, start);
703 spin_unlock(&kvm->mmu_lock);
704
705 /* we've to flush the tlb before the pages can be freed */
706 if (need_tlb_flush)
707 kvm_flush_remote_tlbs(kvm);
708}
709
710static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
711 struct mm_struct *mm,
712 unsigned long start,
713 unsigned long end)
714{
715 struct kvm *kvm = mmu_notifier_to_kvm(mn);
716
717 spin_lock(&kvm->mmu_lock);
718 /*
719 * This sequence increase will notify the kvm page fault that
720 * the page that is going to be mapped in the spte could have
721 * been freed.
722 */
723 kvm->mmu_notifier_seq++;
724 /*
725 * The above sequence increase must be visible before the
726 * below count decrease but both values are read by the kvm
727 * page fault under mmu_lock spinlock so we don't need to add
728 * a smb_wmb() here in between the two.
729 */
730 kvm->mmu_notifier_count--;
731 spin_unlock(&kvm->mmu_lock);
732
733 BUG_ON(kvm->mmu_notifier_count < 0);
734}
735
736static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
737 struct mm_struct *mm,
738 unsigned long address)
739{
740 struct kvm *kvm = mmu_notifier_to_kvm(mn);
741 int young;
742
743 spin_lock(&kvm->mmu_lock);
744 young = kvm_age_hva(kvm, address);
745 spin_unlock(&kvm->mmu_lock);
746
747 if (young)
748 kvm_flush_remote_tlbs(kvm);
749
750 return young;
751}
752
753static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
754 .invalidate_page = kvm_mmu_notifier_invalidate_page,
755 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
756 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
757 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
758};
759#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
760
Avi Kivityf17abe92007-02-21 19:28:04 +0200761static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800763 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200764#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
765 struct page *page;
766#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800768 if (IS_ERR(kvm))
769 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770
Laurent Vivier5f94c172008-05-30 16:05:54 +0200771#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
772 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
773 if (!page) {
774 kfree(kvm);
775 return ERR_PTR(-ENOMEM);
776 }
777 kvm->coalesced_mmio_ring =
778 (struct kvm_coalesced_mmio_ring *)page_address(page);
779#endif
780
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200781#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
782 {
783 int err;
784 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
785 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
786 if (err) {
787#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
788 put_page(page);
789#endif
790 kfree(kvm);
791 return ERR_PTR(err);
792 }
793 }
794#endif
795
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200796 kvm->mm = current->mm;
797 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500798 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300799 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800800 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400801 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200802 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300803 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000804 spin_lock(&kvm_lock);
805 list_add(&kvm->vm_list, &vm_list);
806 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200807#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
808 kvm_coalesced_mmio_init(kvm);
809#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800810out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200811 return kvm;
812}
813
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814/*
815 * Free any memory in @free but not in @dont.
816 */
817static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
818 struct kvm_memory_slot *dont)
819{
Izik Eidus290fc382007-09-27 14:11:22 +0200820 if (!dont || free->rmap != dont->rmap)
821 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822
823 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
824 vfree(free->dirty_bitmap);
825
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300826 if (!dont || free->lpage_info != dont->lpage_info)
827 vfree(free->lpage_info);
828
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000830 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500831 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300832 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833}
834
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800835void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836{
837 int i;
838
839 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000840 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841}
842
Avi Kivityf17abe92007-02-21 19:28:04 +0200843static void kvm_destroy_vm(struct kvm *kvm)
844{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200845 struct mm_struct *mm = kvm->mm;
846
Avi Kivity133de902007-02-12 00:54:44 -0800847 spin_lock(&kvm_lock);
848 list_del(&kvm->vm_list);
849 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300850 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400851 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200852#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
853 if (kvm->coalesced_mmio_ring != NULL)
854 free_page((unsigned long)kvm->coalesced_mmio_ring);
855#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200856#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
857 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
858#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800859 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200860 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200861}
862
Izik Eidusd39f13b2008-03-30 16:01:25 +0300863void kvm_get_kvm(struct kvm *kvm)
864{
865 atomic_inc(&kvm->users_count);
866}
867EXPORT_SYMBOL_GPL(kvm_get_kvm);
868
869void kvm_put_kvm(struct kvm *kvm)
870{
871 if (atomic_dec_and_test(&kvm->users_count))
872 kvm_destroy_vm(kvm);
873}
874EXPORT_SYMBOL_GPL(kvm_put_kvm);
875
876
Avi Kivityf17abe92007-02-21 19:28:04 +0200877static int kvm_vm_release(struct inode *inode, struct file *filp)
878{
879 struct kvm *kvm = filp->private_data;
880
Izik Eidusd39f13b2008-03-30 16:01:25 +0300881 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 return 0;
883}
884
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 * Allocate some memory and give it an address in the guest physical address
887 * space.
888 *
889 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800890 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500891 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800893int __kvm_set_memory_region(struct kvm *kvm,
894 struct kvm_userspace_memory_region *mem,
895 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896{
897 int r;
898 gfn_t base_gfn;
899 unsigned long npages;
900 unsigned long i;
901 struct kvm_memory_slot *memslot;
902 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903
904 r = -EINVAL;
905 /* General sanity checks */
906 if (mem->memory_size & (PAGE_SIZE - 1))
907 goto out;
908 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
909 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800910 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600911 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200912 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 goto out;
914 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
915 goto out;
916
917 memslot = &kvm->memslots[mem->slot];
918 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
919 npages = mem->memory_size >> PAGE_SHIFT;
920
921 if (!npages)
922 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
923
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 new = old = *memslot;
925
926 new.base_gfn = base_gfn;
927 new.npages = npages;
928 new.flags = mem->flags;
929
930 /* Disallow changing a memory slot's size. */
931 r = -EINVAL;
932 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800933 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
935 /* Check for overlaps */
936 r = -EEXIST;
937 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
938 struct kvm_memory_slot *s = &kvm->memslots[i];
939
940 if (s == memslot)
941 continue;
942 if (!((base_gfn + npages <= s->base_gfn) ||
943 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800944 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 /* Free page dirty bitmap if unneeded */
948 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000949 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950
951 r = -ENOMEM;
952
953 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200954#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500955 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400956 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200957
958 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800959 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200960
Izik Eidus290fc382007-09-27 14:11:22 +0200961 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500962
Izik Eidus80b14b52007-10-25 11:54:04 +0200963 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200964 /*
965 * hva_to_rmmap() serialzies with the mmu_lock and to be
966 * safe it has to ignore memslots with !user_alloc &&
967 * !userspace_addr.
968 */
969 if (user_alloc)
970 new.userspace_addr = mem->userspace_addr;
971 else
972 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300974 if (npages && !new.lpage_info) {
975 int largepages = npages / KVM_PAGES_PER_HPAGE;
976 if (npages % KVM_PAGES_PER_HPAGE)
977 largepages++;
978 if (base_gfn % KVM_PAGES_PER_HPAGE)
979 largepages++;
980
981 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
982
983 if (!new.lpage_info)
984 goto out_free;
985
986 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
987
988 if (base_gfn % KVM_PAGES_PER_HPAGE)
989 new.lpage_info[0].write_count = 1;
990 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
991 new.lpage_info[largepages-1].write_count = 1;
992 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993
994 /* Allocate page dirty bitmap if needed */
995 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
996 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
997
998 new.dirty_bitmap = vmalloc(dirty_bytes);
999 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001000 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 memset(new.dirty_bitmap, 0, dirty_bytes);
1002 }
Carsten Otteeff01142008-06-27 15:05:31 +02001003#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001005 if (!npages)
1006 kvm_arch_flush_shadow(kvm);
1007
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001008 spin_lock(&kvm->mmu_lock);
1009 if (mem->slot >= kvm->nmemslots)
1010 kvm->nmemslots = mem->slot + 1;
1011
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001013 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
Zhang Xiantao0de10342007-11-20 16:25:04 +08001015 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1016 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001017 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001018 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001019 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001020 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001021 }
1022
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 kvm_free_physmem_slot(&old, &new);
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001024#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001025 /* map the pages in iommu page table */
1026 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1027 if (r)
1028 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001029#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 return 0;
1031
Sheng Yangf78e0e22007-10-29 09:40:42 +08001032out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 kvm_free_physmem_slot(&new, &old);
1034out:
1035 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001036
1037}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001038EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1039
1040int kvm_set_memory_region(struct kvm *kvm,
1041 struct kvm_userspace_memory_region *mem,
1042 int user_alloc)
1043{
1044 int r;
1045
Izik Eidus72dc67a2008-02-10 18:04:15 +02001046 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001047 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001048 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001049 return r;
1050}
Izik Eidus210c7c42007-10-24 23:52:57 +02001051EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1052
Carsten Otte1fe779f2007-10-29 16:08:35 +01001053int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1054 struct
1055 kvm_userspace_memory_region *mem,
1056 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001057{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001058 if (mem->slot >= KVM_MEMORY_SLOTS)
1059 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001060 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061}
1062
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001063int kvm_get_dirty_log(struct kvm *kvm,
1064 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065{
1066 struct kvm_memory_slot *memslot;
1067 int r, i;
1068 int n;
1069 unsigned long any = 0;
1070
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 r = -EINVAL;
1072 if (log->slot >= KVM_MEMORY_SLOTS)
1073 goto out;
1074
1075 memslot = &kvm->memslots[log->slot];
1076 r = -ENOENT;
1077 if (!memslot->dirty_bitmap)
1078 goto out;
1079
Uri Lublincd1a4a92007-02-22 16:43:09 +02001080 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081
Uri Lublincd1a4a92007-02-22 16:43:09 +02001082 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083 any = memslot->dirty_bitmap[i];
1084
1085 r = -EFAULT;
1086 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1087 goto out;
1088
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001089 if (any)
1090 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
1092 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094 return r;
1095}
1096
Izik Eiduscea7bb22007-10-17 19:17:48 +02001097int is_error_page(struct page *page)
1098{
1099 return page == bad_page;
1100}
1101EXPORT_SYMBOL_GPL(is_error_page);
1102
Anthony Liguori35149e22008-04-02 14:46:56 -05001103int is_error_pfn(pfn_t pfn)
1104{
1105 return pfn == bad_pfn;
1106}
1107EXPORT_SYMBOL_GPL(is_error_pfn);
1108
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001109static inline unsigned long bad_hva(void)
1110{
1111 return PAGE_OFFSET;
1112}
1113
1114int kvm_is_error_hva(unsigned long addr)
1115{
1116 return addr == bad_hva();
1117}
1118EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1119
Izik Eidus28430992008-10-03 17:40:32 +03001120struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121{
1122 int i;
1123
1124 for (i = 0; i < kvm->nmemslots; ++i) {
1125 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1126
1127 if (gfn >= memslot->base_gfn
1128 && gfn < memslot->base_gfn + memslot->npages)
1129 return memslot;
1130 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001131 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132}
Izik Eidus28430992008-10-03 17:40:32 +03001133EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001134
1135struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1136{
1137 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001138 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001139}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140
Izik Eiduse0d62c72007-10-24 23:57:46 +02001141int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1142{
1143 int i;
1144
1145 gfn = unalias_gfn(kvm, gfn);
1146 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1147 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1148
1149 if (gfn >= memslot->base_gfn
1150 && gfn < memslot->base_gfn + memslot->npages)
1151 return 1;
1152 }
1153 return 0;
1154}
1155EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1156
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001157unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001158{
1159 struct kvm_memory_slot *slot;
1160
1161 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001162 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001163 if (!slot)
1164 return bad_hva();
1165 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1166}
Sheng Yang0d150292008-04-25 21:44:50 +08001167EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001168
Anthony Liguori35149e22008-04-02 14:46:56 -05001169pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001170{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001171 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001172 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001173 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001174 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001175
Avi Kivity60395222007-10-21 11:03:36 +02001176 might_sleep();
1177
Izik Eidus539cb662007-11-11 22:05:04 +02001178 addr = gfn_to_hva(kvm, gfn);
1179 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001180 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001181 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001182 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001183
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001184 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001185
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001186 if (unlikely(npages != 1)) {
1187 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001188
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001189 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001190 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001191
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001192 if (vma == NULL || addr < vma->vm_start ||
1193 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001194 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001195 get_page(bad_page);
1196 return page_to_pfn(bad_page);
1197 }
1198
1199 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001200 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001201 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001202 } else
1203 pfn = page_to_pfn(page[0]);
1204
1205 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001206}
1207
1208EXPORT_SYMBOL_GPL(gfn_to_pfn);
1209
1210struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1211{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001212 pfn_t pfn;
1213
1214 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001215 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001216 return pfn_to_page(pfn);
1217
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001218 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001219
1220 get_page(bad_page);
1221 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001222}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001223
Avi Kivity954bbbc2007-03-30 14:02:32 +03001224EXPORT_SYMBOL_GPL(gfn_to_page);
1225
Izik Eidusb4231d62007-11-20 11:49:33 +02001226void kvm_release_page_clean(struct page *page)
1227{
Anthony Liguori35149e22008-04-02 14:46:56 -05001228 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001229}
1230EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1231
Anthony Liguori35149e22008-04-02 14:46:56 -05001232void kvm_release_pfn_clean(pfn_t pfn)
1233{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001234 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001235 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001236}
1237EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1238
Izik Eidusb4231d62007-11-20 11:49:33 +02001239void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001240{
Anthony Liguori35149e22008-04-02 14:46:56 -05001241 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001242}
Izik Eidusb4231d62007-11-20 11:49:33 +02001243EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001244
Anthony Liguori35149e22008-04-02 14:46:56 -05001245void kvm_release_pfn_dirty(pfn_t pfn)
1246{
1247 kvm_set_pfn_dirty(pfn);
1248 kvm_release_pfn_clean(pfn);
1249}
1250EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1251
1252void kvm_set_page_dirty(struct page *page)
1253{
1254 kvm_set_pfn_dirty(page_to_pfn(page));
1255}
1256EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1257
1258void kvm_set_pfn_dirty(pfn_t pfn)
1259{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001260 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001261 struct page *page = pfn_to_page(pfn);
1262 if (!PageReserved(page))
1263 SetPageDirty(page);
1264 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001265}
1266EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1267
1268void kvm_set_pfn_accessed(pfn_t pfn)
1269{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001270 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001271 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001272}
1273EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1274
1275void kvm_get_pfn(pfn_t pfn)
1276{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001277 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001278 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001279}
1280EXPORT_SYMBOL_GPL(kvm_get_pfn);
1281
Izik Eidus195aefd2007-10-01 22:14:18 +02001282static int next_segment(unsigned long len, int offset)
1283{
1284 if (len > PAGE_SIZE - offset)
1285 return PAGE_SIZE - offset;
1286 else
1287 return len;
1288}
1289
1290int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1291 int len)
1292{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001293 int r;
1294 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001295
Izik Eiduse0506bc2007-11-11 22:10:22 +02001296 addr = gfn_to_hva(kvm, gfn);
1297 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001298 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001299 r = copy_from_user(data, (void __user *)addr + offset, len);
1300 if (r)
1301 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001302 return 0;
1303}
1304EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1305
1306int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1307{
1308 gfn_t gfn = gpa >> PAGE_SHIFT;
1309 int seg;
1310 int offset = offset_in_page(gpa);
1311 int ret;
1312
1313 while ((seg = next_segment(len, offset)) != 0) {
1314 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1315 if (ret < 0)
1316 return ret;
1317 offset = 0;
1318 len -= seg;
1319 data += seg;
1320 ++gfn;
1321 }
1322 return 0;
1323}
1324EXPORT_SYMBOL_GPL(kvm_read_guest);
1325
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001326int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1327 unsigned long len)
1328{
1329 int r;
1330 unsigned long addr;
1331 gfn_t gfn = gpa >> PAGE_SHIFT;
1332 int offset = offset_in_page(gpa);
1333
1334 addr = gfn_to_hva(kvm, gfn);
1335 if (kvm_is_error_hva(addr))
1336 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001337 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001338 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001339 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001340 if (r)
1341 return -EFAULT;
1342 return 0;
1343}
1344EXPORT_SYMBOL(kvm_read_guest_atomic);
1345
Izik Eidus195aefd2007-10-01 22:14:18 +02001346int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1347 int offset, int len)
1348{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001349 int r;
1350 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001351
Izik Eiduse0506bc2007-11-11 22:10:22 +02001352 addr = gfn_to_hva(kvm, gfn);
1353 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001354 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001355 r = copy_to_user((void __user *)addr + offset, data, len);
1356 if (r)
1357 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001358 mark_page_dirty(kvm, gfn);
1359 return 0;
1360}
1361EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1362
1363int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1364 unsigned long len)
1365{
1366 gfn_t gfn = gpa >> PAGE_SHIFT;
1367 int seg;
1368 int offset = offset_in_page(gpa);
1369 int ret;
1370
1371 while ((seg = next_segment(len, offset)) != 0) {
1372 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1373 if (ret < 0)
1374 return ret;
1375 offset = 0;
1376 len -= seg;
1377 data += seg;
1378 ++gfn;
1379 }
1380 return 0;
1381}
1382
1383int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1384{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001385 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001386}
1387EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1388
1389int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1390{
1391 gfn_t gfn = gpa >> PAGE_SHIFT;
1392 int seg;
1393 int offset = offset_in_page(gpa);
1394 int ret;
1395
1396 while ((seg = next_segment(len, offset)) != 0) {
1397 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1398 if (ret < 0)
1399 return ret;
1400 offset = 0;
1401 len -= seg;
1402 ++gfn;
1403 }
1404 return 0;
1405}
1406EXPORT_SYMBOL_GPL(kvm_clear_guest);
1407
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1409{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001410 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411
Uri Lublin3b6fff12007-10-30 10:42:09 +02001412 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001413 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001414 if (memslot && memslot->dirty_bitmap) {
1415 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416
Rusty Russell7e9d6192007-07-31 20:41:14 +10001417 /* avoid RMW */
1418 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1419 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 }
1421}
1422
Eddie Dongb6958ce2007-07-18 12:15:21 +03001423/*
1424 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1425 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001426void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001427{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001428 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001429
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001430 for (;;) {
1431 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001432
Marcelo Tosattid7690172008-09-08 15:23:48 -03001433 if (kvm_cpu_has_interrupt(vcpu) ||
1434 kvm_cpu_has_pending_timer(vcpu) ||
1435 kvm_arch_vcpu_runnable(vcpu)) {
1436 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001437 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001438 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001439 if (signal_pending(current))
1440 break;
1441
Eddie Dongb6958ce2007-07-18 12:15:21 +03001442 vcpu_put(vcpu);
1443 schedule();
1444 vcpu_load(vcpu);
1445 }
1446
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001447 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001448}
1449
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450void kvm_resched(struct kvm_vcpu *vcpu)
1451{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001452 if (!need_resched())
1453 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455}
1456EXPORT_SYMBOL_GPL(kvm_resched);
1457
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001458static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001459{
1460 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001461 struct page *page;
1462
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001463 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001464 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001465#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001466 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001467 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001468#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001469#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1470 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1471 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1472#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001473 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001474 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001475 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001476 vmf->page = page;
1477 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001478}
1479
1480static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001481 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001482};
1483
1484static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1485{
1486 vma->vm_ops = &kvm_vcpu_vm_ops;
1487 return 0;
1488}
1489
Avi Kivitybccf2152007-02-21 18:04:26 +02001490static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1491{
1492 struct kvm_vcpu *vcpu = filp->private_data;
1493
Al Viro66c0b392008-04-19 20:33:56 +01001494 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001495 return 0;
1496}
1497
Jan Engelhardt5c502742008-01-22 20:46:14 +01001498static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001499 .release = kvm_vcpu_release,
1500 .unlocked_ioctl = kvm_vcpu_ioctl,
1501 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001502 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001503};
1504
1505/*
1506 * Allocates an inode for the vcpu.
1507 */
1508static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1509{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001510 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001511 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001512 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001513 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001514}
1515
Avi Kivityc5ea7662007-02-20 18:41:05 +02001516/*
1517 * Creates some virtual cpus. Good luck creating more than one.
1518 */
1519static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1520{
1521 int r;
1522 struct kvm_vcpu *vcpu;
1523
Avi Kivityc5ea7662007-02-20 18:41:05 +02001524 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001525 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001526
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001527 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001528 if (IS_ERR(vcpu))
1529 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001530
Avi Kivity15ad7142007-07-11 18:17:21 +03001531 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1532
Avi Kivity26e52152007-11-20 15:30:24 +02001533 r = kvm_arch_vcpu_setup(vcpu);
1534 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001535 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001536
Shaohua Li11ec2802007-07-23 14:51:37 +08001537 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001538 if (kvm->vcpus[n]) {
1539 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001540 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001541 }
1542 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001543 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001544
1545 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001546 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001547 r = create_vcpu_fd(vcpu);
1548 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001549 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001550 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001551
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001552unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001553 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001554 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001555vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001556 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001557 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001558 return r;
1559}
1560
Avi Kivity1961d272007-03-05 19:46:05 +02001561static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1562{
1563 if (sigset) {
1564 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1565 vcpu->sigset_active = 1;
1566 vcpu->sigset = *sigset;
1567 } else
1568 vcpu->sigset_active = 0;
1569 return 0;
1570}
1571
Avi Kivitybccf2152007-02-21 18:04:26 +02001572static long kvm_vcpu_ioctl(struct file *filp,
1573 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574{
Avi Kivitybccf2152007-02-21 18:04:26 +02001575 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001576 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001577 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001578 struct kvm_fpu *fpu = NULL;
1579 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001581 if (vcpu->kvm->mm != current->mm)
1582 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001584 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001585 r = -EINVAL;
1586 if (arg)
1587 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001588 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001591 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001593 r = -ENOMEM;
1594 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1595 if (!kvm_regs)
1596 goto out;
1597 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001599 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001601 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1602 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001604out_free1:
1605 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606 break;
1607 }
1608 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001609 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001611 r = -ENOMEM;
1612 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1613 if (!kvm_regs)
1614 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001616 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1617 goto out_free2;
1618 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001620 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001622out_free2:
1623 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624 break;
1625 }
1626 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001627 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1628 r = -ENOMEM;
1629 if (!kvm_sregs)
1630 goto out;
1631 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 if (r)
1633 goto out;
1634 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001635 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636 goto out;
1637 r = 0;
1638 break;
1639 }
1640 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001641 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1642 r = -ENOMEM;
1643 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001645 r = -EFAULT;
1646 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1647 goto out;
1648 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 if (r)
1650 goto out;
1651 r = 0;
1652 break;
1653 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001654 case KVM_GET_MP_STATE: {
1655 struct kvm_mp_state mp_state;
1656
1657 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1658 if (r)
1659 goto out;
1660 r = -EFAULT;
1661 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1662 goto out;
1663 r = 0;
1664 break;
1665 }
1666 case KVM_SET_MP_STATE: {
1667 struct kvm_mp_state mp_state;
1668
1669 r = -EFAULT;
1670 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1671 goto out;
1672 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1673 if (r)
1674 goto out;
1675 r = 0;
1676 break;
1677 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 case KVM_TRANSLATE: {
1679 struct kvm_translation tr;
1680
1681 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001682 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001684 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 if (r)
1686 goto out;
1687 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001688 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689 goto out;
1690 r = 0;
1691 break;
1692 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 case KVM_DEBUG_GUEST: {
1694 struct kvm_debug_guest dbg;
1695
1696 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001697 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001699 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700 if (r)
1701 goto out;
1702 r = 0;
1703 break;
1704 }
Avi Kivity1961d272007-03-05 19:46:05 +02001705 case KVM_SET_SIGNAL_MASK: {
1706 struct kvm_signal_mask __user *sigmask_arg = argp;
1707 struct kvm_signal_mask kvm_sigmask;
1708 sigset_t sigset, *p;
1709
1710 p = NULL;
1711 if (argp) {
1712 r = -EFAULT;
1713 if (copy_from_user(&kvm_sigmask, argp,
1714 sizeof kvm_sigmask))
1715 goto out;
1716 r = -EINVAL;
1717 if (kvm_sigmask.len != sizeof sigset)
1718 goto out;
1719 r = -EFAULT;
1720 if (copy_from_user(&sigset, sigmask_arg->sigset,
1721 sizeof sigset))
1722 goto out;
1723 p = &sigset;
1724 }
1725 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1726 break;
1727 }
Avi Kivityb8836732007-04-01 16:34:31 +03001728 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001729 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1730 r = -ENOMEM;
1731 if (!fpu)
1732 goto out;
1733 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001734 if (r)
1735 goto out;
1736 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001737 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001738 goto out;
1739 r = 0;
1740 break;
1741 }
1742 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001743 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1744 r = -ENOMEM;
1745 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001746 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001747 r = -EFAULT;
1748 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1749 goto out;
1750 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001751 if (r)
1752 goto out;
1753 r = 0;
1754 break;
1755 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001756 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001757 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001758 }
1759out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001760 kfree(fpu);
1761 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001762 return r;
1763}
1764
1765static long kvm_vm_ioctl(struct file *filp,
1766 unsigned int ioctl, unsigned long arg)
1767{
1768 struct kvm *kvm = filp->private_data;
1769 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001770 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001771
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001772 if (kvm->mm != current->mm)
1773 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001774 switch (ioctl) {
1775 case KVM_CREATE_VCPU:
1776 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1777 if (r < 0)
1778 goto out;
1779 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001780 case KVM_SET_USER_MEMORY_REGION: {
1781 struct kvm_userspace_memory_region kvm_userspace_mem;
1782
1783 r = -EFAULT;
1784 if (copy_from_user(&kvm_userspace_mem, argp,
1785 sizeof kvm_userspace_mem))
1786 goto out;
1787
1788 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789 if (r)
1790 goto out;
1791 break;
1792 }
1793 case KVM_GET_DIRTY_LOG: {
1794 struct kvm_dirty_log log;
1795
1796 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001797 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001799 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 if (r)
1801 goto out;
1802 break;
1803 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001804#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1805 case KVM_REGISTER_COALESCED_MMIO: {
1806 struct kvm_coalesced_mmio_zone zone;
1807 r = -EFAULT;
1808 if (copy_from_user(&zone, argp, sizeof zone))
1809 goto out;
1810 r = -ENXIO;
1811 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1812 if (r)
1813 goto out;
1814 r = 0;
1815 break;
1816 }
1817 case KVM_UNREGISTER_COALESCED_MMIO: {
1818 struct kvm_coalesced_mmio_zone zone;
1819 r = -EFAULT;
1820 if (copy_from_user(&zone, argp, sizeof zone))
1821 goto out;
1822 r = -ENXIO;
1823 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1824 if (r)
1825 goto out;
1826 r = 0;
1827 break;
1828 }
1829#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001830#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1831 case KVM_ASSIGN_PCI_DEVICE: {
1832 struct kvm_assigned_pci_dev assigned_dev;
1833
1834 r = -EFAULT;
1835 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1836 goto out;
1837 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1838 if (r)
1839 goto out;
1840 break;
1841 }
1842 case KVM_ASSIGN_IRQ: {
1843 struct kvm_assigned_irq assigned_irq;
1844
1845 r = -EFAULT;
1846 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1847 goto out;
1848 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1849 if (r)
1850 goto out;
1851 break;
1852 }
1853#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001854 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001855 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001856 }
1857out:
1858 return r;
1859}
1860
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001861static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001862{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001863 struct page *page[1];
1864 unsigned long addr;
1865 int npages;
1866 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001867 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001868
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001869 addr = gfn_to_hva(kvm, gfn);
1870 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001871 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001872
1873 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1874 NULL);
1875 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001876 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001877
1878 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001879 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001880}
1881
1882static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001883 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001884};
1885
1886static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1887{
1888 vma->vm_ops = &kvm_vm_vm_ops;
1889 return 0;
1890}
1891
Jan Engelhardt5c502742008-01-22 20:46:14 +01001892static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001893 .release = kvm_vm_release,
1894 .unlocked_ioctl = kvm_vm_ioctl,
1895 .compat_ioctl = kvm_vm_ioctl,
1896 .mmap = kvm_vm_mmap,
1897};
1898
1899static int kvm_dev_ioctl_create_vm(void)
1900{
Al Viro2030a422008-02-23 06:46:49 -05001901 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001902 struct kvm *kvm;
1903
Avi Kivityf17abe92007-02-21 19:28:04 +02001904 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001905 if (IS_ERR(kvm))
1906 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001907 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001908 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001909 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001910
Avi Kivityf17abe92007-02-21 19:28:04 +02001911 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001912}
1913
1914static long kvm_dev_ioctl(struct file *filp,
1915 unsigned int ioctl, unsigned long arg)
1916{
Avi Kivity07c45a32007-03-07 13:05:38 +02001917 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001918
1919 switch (ioctl) {
1920 case KVM_GET_API_VERSION:
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_API_VERSION;
1925 break;
1926 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001927 r = -EINVAL;
1928 if (arg)
1929 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001930 r = kvm_dev_ioctl_create_vm();
1931 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001932 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001933 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001934 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001935 case KVM_GET_VCPU_MMAP_SIZE:
1936 r = -EINVAL;
1937 if (arg)
1938 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001939 r = PAGE_SIZE; /* struct kvm_run */
1940#ifdef CONFIG_X86
1941 r += PAGE_SIZE; /* pio data page */
1942#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001943#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1944 r += PAGE_SIZE; /* coalesced mmio ring page */
1945#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001946 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001947 case KVM_TRACE_ENABLE:
1948 case KVM_TRACE_PAUSE:
1949 case KVM_TRACE_DISABLE:
1950 r = kvm_trace_ioctl(ioctl, arg);
1951 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001953 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 }
1955out:
1956 return r;
1957}
1958
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 .unlocked_ioctl = kvm_dev_ioctl,
1961 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962};
1963
1964static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001965 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 "kvm",
1967 &kvm_chardev_ops,
1968};
1969
Avi Kivity1b6c0162007-05-24 13:03:52 +03001970static void hardware_enable(void *junk)
1971{
1972 int cpu = raw_smp_processor_id();
1973
1974 if (cpu_isset(cpu, cpus_hardware_enabled))
1975 return;
1976 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001977 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001978}
1979
1980static void hardware_disable(void *junk)
1981{
1982 int cpu = raw_smp_processor_id();
1983
1984 if (!cpu_isset(cpu, cpus_hardware_enabled))
1985 return;
1986 cpu_clear(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001987 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001988}
1989
Avi Kivity774c47f2007-02-12 00:54:47 -08001990static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1991 void *v)
1992{
1993 int cpu = (long)v;
1994
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001995 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001996 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001997 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001998 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1999 cpu);
2000 hardware_disable(NULL);
2001 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002002 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002003 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2004 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002005 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002006 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002007 case CPU_ONLINE:
2008 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2009 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002010 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002011 break;
2012 }
2013 return NOTIFY_OK;
2014}
2015
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002016
2017asmlinkage void kvm_handle_fault_on_reboot(void)
2018{
2019 if (kvm_rebooting)
2020 /* spin while reset goes on */
2021 while (true)
2022 ;
2023 /* Fault while not rebooting. We want the trace. */
2024 BUG();
2025}
2026EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2027
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002028static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002029 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002030{
2031 if (val == SYS_RESTART) {
2032 /*
2033 * Some (well, at least mine) BIOSes hang on reboot if
2034 * in vmx root mode.
2035 */
2036 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002037 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002038 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002039 }
2040 return NOTIFY_OK;
2041}
2042
2043static struct notifier_block kvm_reboot_notifier = {
2044 .notifier_call = kvm_reboot,
2045 .priority = 0,
2046};
2047
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002048void kvm_io_bus_init(struct kvm_io_bus *bus)
2049{
2050 memset(bus, 0, sizeof(*bus));
2051}
2052
2053void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2054{
2055 int i;
2056
2057 for (i = 0; i < bus->dev_count; i++) {
2058 struct kvm_io_device *pos = bus->devs[i];
2059
2060 kvm_iodevice_destructor(pos);
2061 }
2062}
2063
Laurent Vivier92760492008-05-30 16:05:53 +02002064struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2065 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002066{
2067 int i;
2068
2069 for (i = 0; i < bus->dev_count; i++) {
2070 struct kvm_io_device *pos = bus->devs[i];
2071
Laurent Vivier92760492008-05-30 16:05:53 +02002072 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002073 return pos;
2074 }
2075
2076 return NULL;
2077}
2078
2079void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2080{
2081 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2082
2083 bus->devs[bus->dev_count++] = dev;
2084}
2085
Avi Kivity774c47f2007-02-12 00:54:47 -08002086static struct notifier_block kvm_cpu_notifier = {
2087 .notifier_call = kvm_cpu_hotplug,
2088 .priority = 20, /* must be > scheduler priority */
2089};
2090
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002091static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002092{
2093 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002094 struct kvm *kvm;
2095
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002096 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002097 spin_lock(&kvm_lock);
2098 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002099 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002100 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002101 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002102}
2103
2104DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2105
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002106static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002107{
2108 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002109 struct kvm *kvm;
2110 struct kvm_vcpu *vcpu;
2111 int i;
2112
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002113 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002114 spin_lock(&kvm_lock);
2115 list_for_each_entry(kvm, &vm_list, vm_list)
2116 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002117 vcpu = kvm->vcpus[i];
2118 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002119 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002120 }
2121 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002122 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002123}
2124
Avi Kivityba1389b2007-11-18 16:24:12 +02002125DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2126
2127static struct file_operations *stat_fops[] = {
2128 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2129 [KVM_STAT_VM] = &vm_stat_fops,
2130};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002131
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002132static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133{
2134 struct kvm_stats_debugfs_item *p;
2135
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002136 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002138 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002139 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002140 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141}
2142
2143static void kvm_exit_debug(void)
2144{
2145 struct kvm_stats_debugfs_item *p;
2146
2147 for (p = debugfs_entries; p->name; ++p)
2148 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002149 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150}
2151
Avi Kivity59ae6c62007-02-12 00:54:48 -08002152static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2153{
Avi Kivity4267c412007-05-24 13:09:41 +03002154 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002155 return 0;
2156}
2157
2158static int kvm_resume(struct sys_device *dev)
2159{
Avi Kivity4267c412007-05-24 13:09:41 +03002160 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002161 return 0;
2162}
2163
2164static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01002165 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002166 .suspend = kvm_suspend,
2167 .resume = kvm_resume,
2168};
2169
2170static struct sys_device kvm_sysdev = {
2171 .id = 0,
2172 .cls = &kvm_sysdev_class,
2173};
2174
Izik Eiduscea7bb22007-10-17 19:17:48 +02002175struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002176pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177
Avi Kivity15ad7142007-07-11 18:17:21 +03002178static inline
2179struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2180{
2181 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2182}
2183
2184static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2185{
2186 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2187
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002188 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002189}
2190
2191static void kvm_sched_out(struct preempt_notifier *pn,
2192 struct task_struct *next)
2193{
2194 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2195
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002196 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002197}
2198
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002199int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002200 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201{
2202 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002203 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002205 kvm_init_debug();
2206
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002207 r = kvm_arch_init(opaque);
2208 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002209 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002210
2211 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2212
2213 if (bad_page == NULL) {
2214 r = -ENOMEM;
2215 goto out;
2216 }
2217
Anthony Liguori35149e22008-04-02 14:46:56 -05002218 bad_pfn = page_to_pfn(bad_page);
2219
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002220 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002222 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223
Yang, Sheng002c7f72007-07-31 14:23:01 +03002224 for_each_online_cpu(cpu) {
2225 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002226 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002227 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002228 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002229 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002230 }
2231
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002232 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002233 r = register_cpu_notifier(&kvm_cpu_notifier);
2234 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002235 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 register_reboot_notifier(&kvm_reboot_notifier);
2237
Avi Kivity59ae6c62007-02-12 00:54:48 -08002238 r = sysdev_class_register(&kvm_sysdev_class);
2239 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002240 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002241
2242 r = sysdev_register(&kvm_sysdev);
2243 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002244 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002245
Rusty Russellc16f8622007-07-30 21:12:19 +10002246 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2247 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002248 __alignof__(struct kvm_vcpu),
2249 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002250 if (!kvm_vcpu_cache) {
2251 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002252 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002253 }
2254
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 kvm_chardev_ops.owner = module;
2256
2257 r = misc_register(&kvm_dev);
2258 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002259 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 goto out_free;
2261 }
2262
Avi Kivity15ad7142007-07-11 18:17:21 +03002263 kvm_preempt_ops.sched_in = kvm_sched_in;
2264 kvm_preempt_ops.sched_out = kvm_sched_out;
Sheng Yang5319c662008-11-24 14:32:57 +08002265#ifndef CONFIG_X86
2266 msi2intx = 0;
2267#endif
Avi Kivity15ad7142007-07-11 18:17:21 +03002268
Avi Kivityc7addb92007-09-16 18:58:32 +02002269 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270
2271out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002272 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002273out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002274 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002275out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002276 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002277out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002279 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002280out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002281 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002282out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002283 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002284out_free_0:
2285 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002286out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002287 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002288 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002289out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 return r;
2291}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002292EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002294void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002296 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002298 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002299 sysdev_unregister(&kvm_sysdev);
2300 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002302 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002303 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002304 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002305 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02002307 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002309EXPORT_SYMBOL_GPL(kvm_exit);