KVM: add KVM_USERSPACE_IRQ_SOURCE_ID assertions
[deliverable/linux.git] / virt / kvm / kvm_main.c
CommitLineData
6aa8b732
AK
1/*
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
e2174021 18#include "iodev.h"
6aa8b732 19
edf88417 20#include <linux/kvm_host.h>
6aa8b732
AK
21#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
6aa8b732
AK
24#include <linux/percpu.h>
25#include <linux/gfp.h>
6aa8b732
AK
26#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
6aa8b732 29#include <linux/reboot.h>
6aa8b732
AK
30#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
59ae6c6b 33#include <linux/sysdev.h>
774c47f1 34#include <linux/cpu.h>
e8edc6e0 35#include <linux/sched.h>
d9e368d6
AK
36#include <linux/cpumask.h>
37#include <linux/smp.h>
d6d28168 38#include <linux/anon_inodes.h>
04d2cc77 39#include <linux/profile.h>
7aa81cc0 40#include <linux/kvm_para.h>
6fc138d2 41#include <linux/pagemap.h>
8d4e1288 42#include <linux/mman.h>
35149e21 43#include <linux/swap.h>
6aa8b732 44
e495606d 45#include <asm/processor.h>
e495606d
AK
46#include <asm/io.h>
47#include <asm/uaccess.h>
3e021bf5 48#include <asm/pgtable.h>
6aa8b732 49
f64769eb
SY
50#ifdef CONFIG_X86
51#include <asm/msidef.h>
52#endif
53
5f94c174
LV
54#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
55#include "coalesced_mmio.h"
56#endif
57
8a98f664
XZ
58#ifdef KVM_CAP_DEVICE_ASSIGNMENT
59#include <linux/pci.h>
60#include <linux/interrupt.h>
61#include "irq.h"
62#endif
63
6aa8b732
AK
64MODULE_AUTHOR("Qumranet");
65MODULE_LICENSE("GPL");
66
5319c662
SY
67static int msi2intx = 1;
68module_param(msi2intx, bool, 0);
69
e9b11c17
ZX
70DEFINE_SPINLOCK(kvm_lock);
71LIST_HEAD(vm_list);
133de902 72
1b6c0168
AK
73static cpumask_t cpus_hardware_enabled;
74
c16f862d
RR
75struct kmem_cache *kvm_vcpu_cache;
76EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
1165f5fe 77
15ad7146
AK
78static __read_mostly struct preempt_ops kvm_preempt_ops;
79
76f7c879 80struct dentry *kvm_debugfs_dir;
6aa8b732 81
bccf2150
AK
82static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
83 unsigned long arg);
84
e8ba5d31 85static bool kvm_rebooting;
4ecac3fd 86
8a98f664 87#ifdef KVM_CAP_DEVICE_ASSIGNMENT
f64769eb
SY
88
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
8a98f664
XZ
139static 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);
6b9cc7fd
SY
165 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 }
8a98f664
XZ
174 mutex_unlock(&assigned_dev->kvm->lock);
175 kvm_put_kvm(assigned_dev->kvm);
176}
177
8a98f664
XZ
178static 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);
5550af4d 199 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
8a98f664
XZ
200 enable_irq(dev->host_irq);
201}
202
203static void kvm_free_assigned_device(struct kvm *kvm,
204 struct kvm_assigned_dev_kernel
205 *assigned_dev)
206{
4f906c19 207 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested_type)
8a98f664 208 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
6b9cc7fd
SY
209 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
210 pci_disable_msi(assigned_dev->dev);
8a98f664 211
e19e30ef 212 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
f29b2673
MM
213
214 if (assigned_dev->irq_source_id != -1)
215 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
216 assigned_dev->irq_source_id = -1;
8a98f664
XZ
217
218 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
6eb55818
SY
224 pci_reset_function(assigned_dev->dev);
225
8a98f664
XZ
226 pci_release_regions(assigned_dev->dev);
227 pci_disable_device(assigned_dev->dev);
228 pci_dev_put(assigned_dev->dev);
229
230 list_del(&assigned_dev->list);
231 kfree(assigned_dev);
232}
233
234void kvm_free_all_assigned_devices(struct kvm *kvm)
235{
236 struct list_head *ptr, *ptr2;
237 struct kvm_assigned_dev_kernel *assigned_dev;
238
239 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
240 assigned_dev = list_entry(ptr,
241 struct kvm_assigned_dev_kernel,
242 list);
243
244 kvm_free_assigned_device(kvm, assigned_dev);
245 }
246}
247
00e3ed39
SY
248static int assigned_device_update_intx(struct kvm *kvm,
249 struct kvm_assigned_dev_kernel *adev,
250 struct kvm_assigned_irq *airq)
251{
fbac7818
SY
252 adev->guest_irq = airq->guest_irq;
253 adev->ack_notifier.gsi = airq->guest_irq;
254
255 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
00e3ed39 256 return 0;
00e3ed39
SY
257
258 if (irqchip_in_kernel(kvm)) {
5319c662
SY
259 if (!msi2intx &&
260 adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) {
6b9cc7fd
SY
261 free_irq(adev->host_irq, (void *)kvm);
262 pci_disable_msi(adev->dev);
263 }
264
00e3ed39
SY
265 if (!capable(CAP_SYS_RAWIO))
266 return -EPERM;
267
268 if (airq->host_irq)
269 adev->host_irq = airq->host_irq;
270 else
271 adev->host_irq = adev->dev->irq;
00e3ed39
SY
272
273 /* Even though this is PCI, we don't want to use shared
274 * interrupts. Sharing host devices with guest-assigned devices
275 * on the same interrupt line is not a happy situation: there
276 * are going to be long delays in accepting, acking, etc.
277 */
278 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
279 0, "kvm_assigned_intx_device", (void *)adev))
280 return -EIO;
281 }
282
4f906c19
SY
283 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
284 KVM_ASSIGNED_DEV_HOST_INTX;
00e3ed39
SY
285 return 0;
286}
287
6b9cc7fd
SY
288#ifdef CONFIG_X86
289static int assigned_device_update_msi(struct kvm *kvm,
290 struct kvm_assigned_dev_kernel *adev,
291 struct kvm_assigned_irq *airq)
292{
293 int r;
294
5319c662
SY
295 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
296 /* x86 don't care upper address of guest msi message addr */
297 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
298 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
299 adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
300 adev->guest_msi.data = airq->guest_msi.data;
301 adev->ack_notifier.gsi = -1;
302 } else if (msi2intx) {
303 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
304 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
305 adev->guest_irq = airq->guest_irq;
306 adev->ack_notifier.gsi = airq->guest_irq;
307 }
6b9cc7fd
SY
308
309 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
310 return 0;
311
312 if (irqchip_in_kernel(kvm)) {
5319c662
SY
313 if (!msi2intx) {
314 if (adev->irq_requested_type &
315 KVM_ASSIGNED_DEV_HOST_INTX)
316 free_irq(adev->host_irq, (void *)adev);
317
318 r = pci_enable_msi(adev->dev);
319 if (r)
320 return r;
321 }
6b9cc7fd
SY
322
323 adev->host_irq = adev->dev->irq;
324 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
325 "kvm_assigned_msi_device", (void *)adev))
326 return -EIO;
327 }
328
5319c662
SY
329 if (!msi2intx)
330 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
331
332 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
6b9cc7fd
SY
333 return 0;
334}
335#endif
336
8a98f664
XZ
337static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
338 struct kvm_assigned_irq
339 *assigned_irq)
340{
341 int r = 0;
342 struct kvm_assigned_dev_kernel *match;
343
344 mutex_lock(&kvm->lock);
345
346 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
347 assigned_irq->assigned_dev_id);
348 if (!match) {
349 mutex_unlock(&kvm->lock);
350 return -EINVAL;
351 }
352
4f906c19 353 if (!match->irq_requested_type) {
342ffb93
SY
354 INIT_WORK(&match->interrupt_work,
355 kvm_assigned_dev_interrupt_work_handler);
356 if (irqchip_in_kernel(kvm)) {
357 /* Register ack nofitier */
358 match->ack_notifier.gsi = -1;
359 match->ack_notifier.irq_acked =
360 kvm_assigned_dev_ack_irq;
361 kvm_register_irq_ack_notifier(kvm,
362 &match->ack_notifier);
363
364 /* Request IRQ source ID */
365 r = kvm_request_irq_source_id(kvm);
366 if (r < 0)
367 goto out_release;
368 else
369 match->irq_source_id = r;
5319c662
SY
370
371#ifdef CONFIG_X86
372 /* Determine host device irq type, we can know the
373 * result from dev->msi_enabled */
374 if (msi2intx)
375 pci_enable_msi(match->dev);
376#endif
342ffb93 377 }
8a98f664
XZ
378 }
379
5319c662
SY
380 if ((!msi2intx &&
381 (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI)) ||
382 (msi2intx && match->dev->msi_enabled)) {
6b9cc7fd
SY
383#ifdef CONFIG_X86
384 r = assigned_device_update_msi(kvm, match, assigned_irq);
385 if (r) {
386 printk(KERN_WARNING "kvm: failed to enable "
387 "MSI device!\n");
388 goto out_release;
389 }
390#else
391 r = -ENOTTY;
392#endif
393 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
394 /* Host device IRQ 0 means don't support INTx */
5319c662
SY
395 if (!msi2intx) {
396 printk(KERN_WARNING
397 "kvm: wait device to enable MSI!\n");
398 r = 0;
399 } else {
400 printk(KERN_WARNING
401 "kvm: failed to enable MSI device!\n");
402 r = -ENOTTY;
403 goto out_release;
404 }
6b9cc7fd
SY
405 } else {
406 /* Non-sharing INTx mode */
407 r = assigned_device_update_intx(kvm, match, assigned_irq);
408 if (r) {
409 printk(KERN_WARNING "kvm: failed to enable "
410 "INTx device!\n");
411 goto out_release;
412 }
413 }
8a98f664 414
8a98f664
XZ
415 mutex_unlock(&kvm->lock);
416 return r;
417out_release:
418 mutex_unlock(&kvm->lock);
419 kvm_free_assigned_device(kvm, match);
420 return r;
421}
422
423static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
424 struct kvm_assigned_pci_dev *assigned_dev)
425{
426 int r = 0;
427 struct kvm_assigned_dev_kernel *match;
428 struct pci_dev *dev;
429
430 mutex_lock(&kvm->lock);
431
432 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
433 assigned_dev->assigned_dev_id);
434 if (match) {
435 /* device already assigned */
436 r = -EINVAL;
437 goto out;
438 }
439
440 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
441 if (match == NULL) {
442 printk(KERN_INFO "%s: Couldn't allocate memory\n",
443 __func__);
444 r = -ENOMEM;
445 goto out;
446 }
447 dev = pci_get_bus_and_slot(assigned_dev->busnr,
448 assigned_dev->devfn);
449 if (!dev) {
450 printk(KERN_INFO "%s: host device not found\n", __func__);
451 r = -EINVAL;
452 goto out_free;
453 }
454 if (pci_enable_device(dev)) {
455 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
456 r = -EBUSY;
457 goto out_put;
458 }
459 r = pci_request_regions(dev, "kvm_assigned_device");
460 if (r) {
461 printk(KERN_INFO "%s: Could not get access to device regions\n",
462 __func__);
463 goto out_disable;
464 }
6eb55818
SY
465
466 pci_reset_function(dev);
467
8a98f664
XZ
468 match->assigned_dev_id = assigned_dev->assigned_dev_id;
469 match->host_busnr = assigned_dev->busnr;
470 match->host_devfn = assigned_dev->devfn;
471 match->dev = dev;
f29b2673 472 match->irq_source_id = -1;
8a98f664
XZ
473 match->kvm = kvm;
474
475 list_add(&match->list, &kvm->arch.assigned_dev_head);
476
477 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
478 r = kvm_iommu_map_guest(kvm, match);
479 if (r)
480 goto out_list_del;
481 }
482
483out:
484 mutex_unlock(&kvm->lock);
485 return r;
486out_list_del:
487 list_del(&match->list);
488 pci_release_regions(dev);
489out_disable:
490 pci_disable_device(dev);
491out_put:
492 pci_dev_put(dev);
493out_free:
494 kfree(match);
495 mutex_unlock(&kvm->lock);
496 return r;
497}
498#endif
499
5aacf0ca
JM
500static inline int valid_vcpu(int n)
501{
502 return likely(n >= 0 && n < KVM_MAX_VCPUS);
503}
504
c77fb9dc 505inline int kvm_is_mmio_pfn(pfn_t pfn)
cbff90a7
BAY
506{
507 if (pfn_valid(pfn))
508 return PageReserved(pfn_to_page(pfn));
509
510 return true;
511}
512
bccf2150
AK
513/*
514 * Switches to specified vcpu, until a matching vcpu_put()
515 */
313a3dc7 516void vcpu_load(struct kvm_vcpu *vcpu)
6aa8b732 517{
15ad7146
AK
518 int cpu;
519
bccf2150 520 mutex_lock(&vcpu->mutex);
15ad7146
AK
521 cpu = get_cpu();
522 preempt_notifier_register(&vcpu->preempt_notifier);
313a3dc7 523 kvm_arch_vcpu_load(vcpu, cpu);
15ad7146 524 put_cpu();
6aa8b732
AK
525}
526
313a3dc7 527void vcpu_put(struct kvm_vcpu *vcpu)
6aa8b732 528{
15ad7146 529 preempt_disable();
313a3dc7 530 kvm_arch_vcpu_put(vcpu);
15ad7146
AK
531 preempt_notifier_unregister(&vcpu->preempt_notifier);
532 preempt_enable();
6aa8b732
AK
533 mutex_unlock(&vcpu->mutex);
534}
535
d9e368d6
AK
536static void ack_flush(void *_completed)
537{
d9e368d6
AK
538}
539
540void kvm_flush_remote_tlbs(struct kvm *kvm)
541{
597a5f55 542 int i, cpu, me;
d9e368d6
AK
543 cpumask_t cpus;
544 struct kvm_vcpu *vcpu;
d9e368d6 545
597a5f55 546 me = get_cpu();
d9e368d6 547 cpus_clear(cpus);
fb3f0f51
RR
548 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
549 vcpu = kvm->vcpus[i];
550 if (!vcpu)
551 continue;
3176bc3e 552 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
d9e368d6
AK
553 continue;
554 cpu = vcpu->cpu;
597a5f55 555 if (cpu != -1 && cpu != me)
49d3bd7e 556 cpu_set(cpu, cpus);
d9e368d6 557 }
0f74a24c 558 if (cpus_empty(cpus))
597a5f55 559 goto out;
0f74a24c 560 ++kvm->stat.remote_tlb_flush;
49d3bd7e 561 smp_call_function_mask(cpus, ack_flush, NULL, 1);
597a5f55
AK
562out:
563 put_cpu();
d9e368d6
AK
564}
565
2e53d63a
MT
566void kvm_reload_remote_mmus(struct kvm *kvm)
567{
597a5f55 568 int i, cpu, me;
2e53d63a
MT
569 cpumask_t cpus;
570 struct kvm_vcpu *vcpu;
571
597a5f55 572 me = get_cpu();
2e53d63a
MT
573 cpus_clear(cpus);
574 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
575 vcpu = kvm->vcpus[i];
576 if (!vcpu)
577 continue;
578 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
579 continue;
580 cpu = vcpu->cpu;
597a5f55 581 if (cpu != -1 && cpu != me)
2e53d63a
MT
582 cpu_set(cpu, cpus);
583 }
584 if (cpus_empty(cpus))
597a5f55 585 goto out;
2e53d63a 586 smp_call_function_mask(cpus, ack_flush, NULL, 1);
597a5f55
AK
587out:
588 put_cpu();
2e53d63a
MT
589}
590
591
fb3f0f51
RR
592int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
593{
594 struct page *page;
595 int r;
596
597 mutex_init(&vcpu->mutex);
598 vcpu->cpu = -1;
fb3f0f51
RR
599 vcpu->kvm = kvm;
600 vcpu->vcpu_id = id;
b6958ce4 601 init_waitqueue_head(&vcpu->wq);
fb3f0f51
RR
602
603 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
604 if (!page) {
605 r = -ENOMEM;
606 goto fail;
607 }
608 vcpu->run = page_address(page);
609
e9b11c17 610 r = kvm_arch_vcpu_init(vcpu);
fb3f0f51 611 if (r < 0)
e9b11c17 612 goto fail_free_run;
fb3f0f51
RR
613 return 0;
614
fb3f0f51
RR
615fail_free_run:
616 free_page((unsigned long)vcpu->run);
617fail:
76fafa5e 618 return r;
fb3f0f51
RR
619}
620EXPORT_SYMBOL_GPL(kvm_vcpu_init);
621
622void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
623{
e9b11c17 624 kvm_arch_vcpu_uninit(vcpu);
fb3f0f51
RR
625 free_page((unsigned long)vcpu->run);
626}
627EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
628
e930bffe
AA
629#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
630static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
631{
632 return container_of(mn, struct kvm, mmu_notifier);
633}
634
635static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
636 struct mm_struct *mm,
637 unsigned long address)
638{
639 struct kvm *kvm = mmu_notifier_to_kvm(mn);
640 int need_tlb_flush;
641
642 /*
643 * When ->invalidate_page runs, the linux pte has been zapped
644 * already but the page is still allocated until
645 * ->invalidate_page returns. So if we increase the sequence
646 * here the kvm page fault will notice if the spte can't be
647 * established because the page is going to be freed. If
648 * instead the kvm page fault establishes the spte before
649 * ->invalidate_page runs, kvm_unmap_hva will release it
650 * before returning.
651 *
652 * The sequence increase only need to be seen at spin_unlock
653 * time, and not at spin_lock time.
654 *
655 * Increasing the sequence after the spin_unlock would be
656 * unsafe because the kvm page fault could then establish the
657 * pte after kvm_unmap_hva returned, without noticing the page
658 * is going to be freed.
659 */
660 spin_lock(&kvm->mmu_lock);
661 kvm->mmu_notifier_seq++;
662 need_tlb_flush = kvm_unmap_hva(kvm, address);
663 spin_unlock(&kvm->mmu_lock);
664
665 /* we've to flush the tlb before the pages can be freed */
666 if (need_tlb_flush)
667 kvm_flush_remote_tlbs(kvm);
668
669}
670
671static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
672 struct mm_struct *mm,
673 unsigned long start,
674 unsigned long end)
675{
676 struct kvm *kvm = mmu_notifier_to_kvm(mn);
677 int need_tlb_flush = 0;
678
679 spin_lock(&kvm->mmu_lock);
680 /*
681 * The count increase must become visible at unlock time as no
682 * spte can be established without taking the mmu_lock and
683 * count is also read inside the mmu_lock critical section.
684 */
685 kvm->mmu_notifier_count++;
686 for (; start < end; start += PAGE_SIZE)
687 need_tlb_flush |= kvm_unmap_hva(kvm, start);
688 spin_unlock(&kvm->mmu_lock);
689
690 /* we've to flush the tlb before the pages can be freed */
691 if (need_tlb_flush)
692 kvm_flush_remote_tlbs(kvm);
693}
694
695static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
696 struct mm_struct *mm,
697 unsigned long start,
698 unsigned long end)
699{
700 struct kvm *kvm = mmu_notifier_to_kvm(mn);
701
702 spin_lock(&kvm->mmu_lock);
703 /*
704 * This sequence increase will notify the kvm page fault that
705 * the page that is going to be mapped in the spte could have
706 * been freed.
707 */
708 kvm->mmu_notifier_seq++;
709 /*
710 * The above sequence increase must be visible before the
711 * below count decrease but both values are read by the kvm
712 * page fault under mmu_lock spinlock so we don't need to add
713 * a smb_wmb() here in between the two.
714 */
715 kvm->mmu_notifier_count--;
716 spin_unlock(&kvm->mmu_lock);
717
718 BUG_ON(kvm->mmu_notifier_count < 0);
719}
720
721static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
722 struct mm_struct *mm,
723 unsigned long address)
724{
725 struct kvm *kvm = mmu_notifier_to_kvm(mn);
726 int young;
727
728 spin_lock(&kvm->mmu_lock);
729 young = kvm_age_hva(kvm, address);
730 spin_unlock(&kvm->mmu_lock);
731
732 if (young)
733 kvm_flush_remote_tlbs(kvm);
734
735 return young;
736}
737
738static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
739 .invalidate_page = kvm_mmu_notifier_invalidate_page,
740 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
741 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
742 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
743};
744#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
745
f17abe9a 746static struct kvm *kvm_create_vm(void)
6aa8b732 747{
d19a9cd2 748 struct kvm *kvm = kvm_arch_create_vm();
5f94c174
LV
749#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
750 struct page *page;
751#endif
6aa8b732 752
d19a9cd2
ZX
753 if (IS_ERR(kvm))
754 goto out;
6aa8b732 755
5f94c174
LV
756#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
757 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
758 if (!page) {
759 kfree(kvm);
760 return ERR_PTR(-ENOMEM);
761 }
762 kvm->coalesced_mmio_ring =
763 (struct kvm_coalesced_mmio_ring *)page_address(page);
764#endif
765
e930bffe
AA
766#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
767 {
768 int err;
769 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
770 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
771 if (err) {
772#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
773 put_page(page);
774#endif
775 kfree(kvm);
776 return ERR_PTR(err);
777 }
778 }
779#endif
780
6d4e4c4f
AK
781 kvm->mm = current->mm;
782 atomic_inc(&kvm->mm->mm_count);
aaee2c94 783 spin_lock_init(&kvm->mmu_lock);
74906345 784 kvm_io_bus_init(&kvm->pio_bus);
11ec2804 785 mutex_init(&kvm->lock);
2eeb2e94 786 kvm_io_bus_init(&kvm->mmio_bus);
72dc67a6 787 init_rwsem(&kvm->slots_lock);
d39f13b0 788 atomic_set(&kvm->users_count, 1);
5e58cfe4
RR
789 spin_lock(&kvm_lock);
790 list_add(&kvm->vm_list, &vm_list);
791 spin_unlock(&kvm_lock);
5f94c174
LV
792#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
793 kvm_coalesced_mmio_init(kvm);
794#endif
d19a9cd2 795out:
f17abe9a
AK
796 return kvm;
797}
798
6aa8b732
AK
799/*
800 * Free any memory in @free but not in @dont.
801 */
802static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
803 struct kvm_memory_slot *dont)
804{
290fc38d
IE
805 if (!dont || free->rmap != dont->rmap)
806 vfree(free->rmap);
6aa8b732
AK
807
808 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
809 vfree(free->dirty_bitmap);
810
05da4558
MT
811 if (!dont || free->lpage_info != dont->lpage_info)
812 vfree(free->lpage_info);
813
6aa8b732 814 free->npages = 0;
8b6d44c7 815 free->dirty_bitmap = NULL;
8d4e1288 816 free->rmap = NULL;
05da4558 817 free->lpage_info = NULL;
6aa8b732
AK
818}
819
d19a9cd2 820void kvm_free_physmem(struct kvm *kvm)
6aa8b732
AK
821{
822 int i;
823
824 for (i = 0; i < kvm->nmemslots; ++i)
8b6d44c7 825 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
6aa8b732
AK
826}
827
f17abe9a
AK
828static void kvm_destroy_vm(struct kvm *kvm)
829{
6d4e4c4f
AK
830 struct mm_struct *mm = kvm->mm;
831
133de902
AK
832 spin_lock(&kvm_lock);
833 list_del(&kvm->vm_list);
834 spin_unlock(&kvm_lock);
74906345 835 kvm_io_bus_destroy(&kvm->pio_bus);
2eeb2e94 836 kvm_io_bus_destroy(&kvm->mmio_bus);
5f94c174
LV
837#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
838 if (kvm->coalesced_mmio_ring != NULL)
839 free_page((unsigned long)kvm->coalesced_mmio_ring);
e930bffe
AA
840#endif
841#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
842 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
5f94c174 843#endif
d19a9cd2 844 kvm_arch_destroy_vm(kvm);
6d4e4c4f 845 mmdrop(mm);
f17abe9a
AK
846}
847
d39f13b0
IE
848void kvm_get_kvm(struct kvm *kvm)
849{
850 atomic_inc(&kvm->users_count);
851}
852EXPORT_SYMBOL_GPL(kvm_get_kvm);
853
854void kvm_put_kvm(struct kvm *kvm)
855{
856 if (atomic_dec_and_test(&kvm->users_count))
857 kvm_destroy_vm(kvm);
858}
859EXPORT_SYMBOL_GPL(kvm_put_kvm);
860
861
f17abe9a
AK
862static int kvm_vm_release(struct inode *inode, struct file *filp)
863{
864 struct kvm *kvm = filp->private_data;
865
d39f13b0 866 kvm_put_kvm(kvm);
6aa8b732
AK
867 return 0;
868}
869
6aa8b732
AK
870/*
871 * Allocate some memory and give it an address in the guest physical address
872 * space.
873 *
874 * Discontiguous memory is allowed, mostly for framebuffers.
f78e0e2e 875 *
10589a46 876 * Must be called holding mmap_sem for write.
6aa8b732 877 */
f78e0e2e
SY
878int __kvm_set_memory_region(struct kvm *kvm,
879 struct kvm_userspace_memory_region *mem,
880 int user_alloc)
6aa8b732
AK
881{
882 int r;
883 gfn_t base_gfn;
884 unsigned long npages;
885 unsigned long i;
886 struct kvm_memory_slot *memslot;
887 struct kvm_memory_slot old, new;
6aa8b732
AK
888
889 r = -EINVAL;
890 /* General sanity checks */
891 if (mem->memory_size & (PAGE_SIZE - 1))
892 goto out;
893 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
894 goto out;
e7cacd40 895 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
78749809 896 goto out;
e0d62c7f 897 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
6aa8b732
AK
898 goto out;
899 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
900 goto out;
901
902 memslot = &kvm->memslots[mem->slot];
903 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
904 npages = mem->memory_size >> PAGE_SHIFT;
905
906 if (!npages)
907 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
908
6aa8b732
AK
909 new = old = *memslot;
910
911 new.base_gfn = base_gfn;
912 new.npages = npages;
913 new.flags = mem->flags;
914
915 /* Disallow changing a memory slot's size. */
916 r = -EINVAL;
917 if (npages && old.npages && npages != old.npages)
f78e0e2e 918 goto out_free;
6aa8b732
AK
919
920 /* Check for overlaps */
921 r = -EEXIST;
922 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
923 struct kvm_memory_slot *s = &kvm->memslots[i];
924
925 if (s == memslot)
926 continue;
927 if (!((base_gfn + npages <= s->base_gfn) ||
928 (base_gfn >= s->base_gfn + s->npages)))
f78e0e2e 929 goto out_free;
6aa8b732 930 }
6aa8b732 931
6aa8b732
AK
932 /* Free page dirty bitmap if unneeded */
933 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 934 new.dirty_bitmap = NULL;
6aa8b732
AK
935
936 r = -ENOMEM;
937
938 /* Allocate if a slot is being created */
eff0114a 939#ifndef CONFIG_S390
8d4e1288 940 if (npages && !new.rmap) {
d77c26fc 941 new.rmap = vmalloc(npages * sizeof(struct page *));
290fc38d
IE
942
943 if (!new.rmap)
f78e0e2e 944 goto out_free;
290fc38d 945
290fc38d 946 memset(new.rmap, 0, npages * sizeof(*new.rmap));
8d4e1288 947
80b14b5b 948 new.user_alloc = user_alloc;
604b38ac
AA
949 /*
950 * hva_to_rmmap() serialzies with the mmu_lock and to be
951 * safe it has to ignore memslots with !user_alloc &&
952 * !userspace_addr.
953 */
954 if (user_alloc)
955 new.userspace_addr = mem->userspace_addr;
956 else
957 new.userspace_addr = 0;
6aa8b732 958 }
05da4558
MT
959 if (npages && !new.lpage_info) {
960 int largepages = npages / KVM_PAGES_PER_HPAGE;
961 if (npages % KVM_PAGES_PER_HPAGE)
962 largepages++;
963 if (base_gfn % KVM_PAGES_PER_HPAGE)
964 largepages++;
965
966 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
967
968 if (!new.lpage_info)
969 goto out_free;
970
971 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
972
973 if (base_gfn % KVM_PAGES_PER_HPAGE)
974 new.lpage_info[0].write_count = 1;
975 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
976 new.lpage_info[largepages-1].write_count = 1;
977 }
6aa8b732
AK
978
979 /* Allocate page dirty bitmap if needed */
980 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
981 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
982
983 new.dirty_bitmap = vmalloc(dirty_bytes);
984 if (!new.dirty_bitmap)
f78e0e2e 985 goto out_free;
6aa8b732
AK
986 memset(new.dirty_bitmap, 0, dirty_bytes);
987 }
eff0114a 988#endif /* not defined CONFIG_S390 */
6aa8b732 989
34d4cb8f
MT
990 if (!npages)
991 kvm_arch_flush_shadow(kvm);
992
604b38ac
AA
993 spin_lock(&kvm->mmu_lock);
994 if (mem->slot >= kvm->nmemslots)
995 kvm->nmemslots = mem->slot + 1;
996
3ad82a7e 997 *memslot = new;
604b38ac 998 spin_unlock(&kvm->mmu_lock);
3ad82a7e 999
0de10343
ZX
1000 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1001 if (r) {
604b38ac 1002 spin_lock(&kvm->mmu_lock);
0de10343 1003 *memslot = old;
604b38ac 1004 spin_unlock(&kvm->mmu_lock);
0de10343 1005 goto out_free;
82ce2c96
IE
1006 }
1007
6aa8b732 1008 kvm_free_physmem_slot(&old, &new);
8a98f664 1009#ifdef CONFIG_DMAR
62c476c7
BAY
1010 /* map the pages in iommu page table */
1011 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1012 if (r)
1013 goto out;
8a98f664 1014#endif
6aa8b732
AK
1015 return 0;
1016
f78e0e2e 1017out_free:
6aa8b732
AK
1018 kvm_free_physmem_slot(&new, &old);
1019out:
1020 return r;
210c7c4d
IE
1021
1022}
f78e0e2e
SY
1023EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1024
1025int kvm_set_memory_region(struct kvm *kvm,
1026 struct kvm_userspace_memory_region *mem,
1027 int user_alloc)
1028{
1029 int r;
1030
72dc67a6 1031 down_write(&kvm->slots_lock);
f78e0e2e 1032 r = __kvm_set_memory_region(kvm, mem, user_alloc);
72dc67a6 1033 up_write(&kvm->slots_lock);
f78e0e2e
SY
1034 return r;
1035}
210c7c4d
IE
1036EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1037
1fe779f8
CO
1038int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1039 struct
1040 kvm_userspace_memory_region *mem,
1041 int user_alloc)
210c7c4d 1042{
e0d62c7f
IE
1043 if (mem->slot >= KVM_MEMORY_SLOTS)
1044 return -EINVAL;
210c7c4d 1045 return kvm_set_memory_region(kvm, mem, user_alloc);
6aa8b732
AK
1046}
1047
5bb064dc
ZX
1048int kvm_get_dirty_log(struct kvm *kvm,
1049 struct kvm_dirty_log *log, int *is_dirty)
6aa8b732
AK
1050{
1051 struct kvm_memory_slot *memslot;
1052 int r, i;
1053 int n;
1054 unsigned long any = 0;
1055
6aa8b732
AK
1056 r = -EINVAL;
1057 if (log->slot >= KVM_MEMORY_SLOTS)
1058 goto out;
1059
1060 memslot = &kvm->memslots[log->slot];
1061 r = -ENOENT;
1062 if (!memslot->dirty_bitmap)
1063 goto out;
1064
cd1a4a98 1065 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
6aa8b732 1066
cd1a4a98 1067 for (i = 0; !any && i < n/sizeof(long); ++i)
6aa8b732
AK
1068 any = memslot->dirty_bitmap[i];
1069
1070 r = -EFAULT;
1071 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1072 goto out;
1073
5bb064dc
ZX
1074 if (any)
1075 *is_dirty = 1;
6aa8b732
AK
1076
1077 r = 0;
6aa8b732 1078out:
6aa8b732
AK
1079 return r;
1080}
1081
cea7bb21
IE
1082int is_error_page(struct page *page)
1083{
1084 return page == bad_page;
1085}
1086EXPORT_SYMBOL_GPL(is_error_page);
1087
35149e21
AL
1088int is_error_pfn(pfn_t pfn)
1089{
1090 return pfn == bad_pfn;
1091}
1092EXPORT_SYMBOL_GPL(is_error_pfn);
1093
f9d46eb0
IE
1094static inline unsigned long bad_hva(void)
1095{
1096 return PAGE_OFFSET;
1097}
1098
1099int kvm_is_error_hva(unsigned long addr)
1100{
1101 return addr == bad_hva();
1102}
1103EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1104
2843099f 1105struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
6aa8b732
AK
1106{
1107 int i;
1108
1109 for (i = 0; i < kvm->nmemslots; ++i) {
1110 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1111
1112 if (gfn >= memslot->base_gfn
1113 && gfn < memslot->base_gfn + memslot->npages)
1114 return memslot;
1115 }
8b6d44c7 1116 return NULL;
6aa8b732 1117}
2843099f 1118EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
e8207547
AK
1119
1120struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1121{
1122 gfn = unalias_gfn(kvm, gfn);
2843099f 1123 return gfn_to_memslot_unaliased(kvm, gfn);
e8207547 1124}
6aa8b732 1125
e0d62c7f
IE
1126int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1127{
1128 int i;
1129
1130 gfn = unalias_gfn(kvm, gfn);
1131 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1132 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1133
1134 if (gfn >= memslot->base_gfn
1135 && gfn < memslot->base_gfn + memslot->npages)
1136 return 1;
1137 }
1138 return 0;
1139}
1140EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1141
05da4558 1142unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
539cb660
IE
1143{
1144 struct kvm_memory_slot *slot;
1145
1146 gfn = unalias_gfn(kvm, gfn);
2843099f 1147 slot = gfn_to_memslot_unaliased(kvm, gfn);
539cb660
IE
1148 if (!slot)
1149 return bad_hva();
1150 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1151}
0d150298 1152EXPORT_SYMBOL_GPL(gfn_to_hva);
539cb660 1153
35149e21 1154pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
954bbbc2 1155{
8d4e1288 1156 struct page *page[1];
539cb660 1157 unsigned long addr;
8d4e1288 1158 int npages;
2e2e3738 1159 pfn_t pfn;
954bbbc2 1160
60395224
AK
1161 might_sleep();
1162
539cb660
IE
1163 addr = gfn_to_hva(kvm, gfn);
1164 if (kvm_is_error_hva(addr)) {
8a7ae055 1165 get_page(bad_page);
35149e21 1166 return page_to_pfn(bad_page);
8a7ae055 1167 }
8d4e1288 1168
4c2155ce 1169 npages = get_user_pages_fast(addr, 1, 1, page);
539cb660 1170
2e2e3738
AL
1171 if (unlikely(npages != 1)) {
1172 struct vm_area_struct *vma;
1173
4c2155ce 1174 down_read(&current->mm->mmap_sem);
2e2e3738 1175 vma = find_vma(current->mm, addr);
4c2155ce 1176
2e2e3738
AL
1177 if (vma == NULL || addr < vma->vm_start ||
1178 !(vma->vm_flags & VM_PFNMAP)) {
4c2155ce 1179 up_read(&current->mm->mmap_sem);
2e2e3738
AL
1180 get_page(bad_page);
1181 return page_to_pfn(bad_page);
1182 }
1183
1184 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
4c2155ce 1185 up_read(&current->mm->mmap_sem);
c77fb9dc 1186 BUG_ON(!kvm_is_mmio_pfn(pfn));
2e2e3738
AL
1187 } else
1188 pfn = page_to_pfn(page[0]);
8d4e1288 1189
2e2e3738 1190 return pfn;
35149e21
AL
1191}
1192
1193EXPORT_SYMBOL_GPL(gfn_to_pfn);
1194
1195struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1196{
2e2e3738
AL
1197 pfn_t pfn;
1198
1199 pfn = gfn_to_pfn(kvm, gfn);
c77fb9dc 1200 if (!kvm_is_mmio_pfn(pfn))
2e2e3738
AL
1201 return pfn_to_page(pfn);
1202
c77fb9dc 1203 WARN_ON(kvm_is_mmio_pfn(pfn));
2e2e3738
AL
1204
1205 get_page(bad_page);
1206 return bad_page;
954bbbc2 1207}
aab61cc0 1208
954bbbc2
AK
1209EXPORT_SYMBOL_GPL(gfn_to_page);
1210
b4231d61
IE
1211void kvm_release_page_clean(struct page *page)
1212{
35149e21 1213 kvm_release_pfn_clean(page_to_pfn(page));
b4231d61
IE
1214}
1215EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1216
35149e21
AL
1217void kvm_release_pfn_clean(pfn_t pfn)
1218{
c77fb9dc 1219 if (!kvm_is_mmio_pfn(pfn))
2e2e3738 1220 put_page(pfn_to_page(pfn));
35149e21
AL
1221}
1222EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1223
b4231d61 1224void kvm_release_page_dirty(struct page *page)
8a7ae055 1225{
35149e21
AL
1226 kvm_release_pfn_dirty(page_to_pfn(page));
1227}
1228EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1229
1230void kvm_release_pfn_dirty(pfn_t pfn)
1231{
1232 kvm_set_pfn_dirty(pfn);
1233 kvm_release_pfn_clean(pfn);
1234}
1235EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1236
1237void kvm_set_page_dirty(struct page *page)
1238{
1239 kvm_set_pfn_dirty(page_to_pfn(page));
1240}
1241EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1242
1243void kvm_set_pfn_dirty(pfn_t pfn)
1244{
c77fb9dc 1245 if (!kvm_is_mmio_pfn(pfn)) {
2e2e3738
AL
1246 struct page *page = pfn_to_page(pfn);
1247 if (!PageReserved(page))
1248 SetPageDirty(page);
1249 }
8a7ae055 1250}
35149e21
AL
1251EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1252
1253void kvm_set_pfn_accessed(pfn_t pfn)
1254{
c77fb9dc 1255 if (!kvm_is_mmio_pfn(pfn))
2e2e3738 1256 mark_page_accessed(pfn_to_page(pfn));
35149e21
AL
1257}
1258EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1259
1260void kvm_get_pfn(pfn_t pfn)
1261{
c77fb9dc 1262 if (!kvm_is_mmio_pfn(pfn))
2e2e3738 1263 get_page(pfn_to_page(pfn));
35149e21
AL
1264}
1265EXPORT_SYMBOL_GPL(kvm_get_pfn);
8a7ae055 1266
195aefde
IE
1267static int next_segment(unsigned long len, int offset)
1268{
1269 if (len > PAGE_SIZE - offset)
1270 return PAGE_SIZE - offset;
1271 else
1272 return len;
1273}
1274
1275int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1276 int len)
1277{
e0506bcb
IE
1278 int r;
1279 unsigned long addr;
195aefde 1280
e0506bcb
IE
1281 addr = gfn_to_hva(kvm, gfn);
1282 if (kvm_is_error_hva(addr))
1283 return -EFAULT;
1284 r = copy_from_user(data, (void __user *)addr + offset, len);
1285 if (r)
195aefde 1286 return -EFAULT;
195aefde
IE
1287 return 0;
1288}
1289EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1290
1291int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1292{
1293 gfn_t gfn = gpa >> PAGE_SHIFT;
1294 int seg;
1295 int offset = offset_in_page(gpa);
1296 int ret;
1297
1298 while ((seg = next_segment(len, offset)) != 0) {
1299 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1300 if (ret < 0)
1301 return ret;
1302 offset = 0;
1303 len -= seg;
1304 data += seg;
1305 ++gfn;
1306 }
1307 return 0;
1308}
1309EXPORT_SYMBOL_GPL(kvm_read_guest);
1310
7ec54588
MT
1311int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1312 unsigned long len)
1313{
1314 int r;
1315 unsigned long addr;
1316 gfn_t gfn = gpa >> PAGE_SHIFT;
1317 int offset = offset_in_page(gpa);
1318
1319 addr = gfn_to_hva(kvm, gfn);
1320 if (kvm_is_error_hva(addr))
1321 return -EFAULT;
0aac03f0 1322 pagefault_disable();
7ec54588 1323 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
0aac03f0 1324 pagefault_enable();
7ec54588
MT
1325 if (r)
1326 return -EFAULT;
1327 return 0;
1328}
1329EXPORT_SYMBOL(kvm_read_guest_atomic);
1330
195aefde
IE
1331int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1332 int offset, int len)
1333{
e0506bcb
IE
1334 int r;
1335 unsigned long addr;
195aefde 1336
e0506bcb
IE
1337 addr = gfn_to_hva(kvm, gfn);
1338 if (kvm_is_error_hva(addr))
1339 return -EFAULT;
1340 r = copy_to_user((void __user *)addr + offset, data, len);
1341 if (r)
195aefde 1342 return -EFAULT;
195aefde
IE
1343 mark_page_dirty(kvm, gfn);
1344 return 0;
1345}
1346EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1347
1348int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1349 unsigned long len)
1350{
1351 gfn_t gfn = gpa >> PAGE_SHIFT;
1352 int seg;
1353 int offset = offset_in_page(gpa);
1354 int ret;
1355
1356 while ((seg = next_segment(len, offset)) != 0) {
1357 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1358 if (ret < 0)
1359 return ret;
1360 offset = 0;
1361 len -= seg;
1362 data += seg;
1363 ++gfn;
1364 }
1365 return 0;
1366}
1367
1368int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1369{
3e021bf5 1370 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
195aefde
IE
1371}
1372EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1373
1374int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1375{
1376 gfn_t gfn = gpa >> PAGE_SHIFT;
1377 int seg;
1378 int offset = offset_in_page(gpa);
1379 int ret;
1380
1381 while ((seg = next_segment(len, offset)) != 0) {
1382 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1383 if (ret < 0)
1384 return ret;
1385 offset = 0;
1386 len -= seg;
1387 ++gfn;
1388 }
1389 return 0;
1390}
1391EXPORT_SYMBOL_GPL(kvm_clear_guest);
1392
6aa8b732
AK
1393void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1394{
31389947 1395 struct kvm_memory_slot *memslot;
6aa8b732 1396
3b6fff19 1397 gfn = unalias_gfn(kvm, gfn);
2843099f 1398 memslot = gfn_to_memslot_unaliased(kvm, gfn);
7e9d619d
RR
1399 if (memslot && memslot->dirty_bitmap) {
1400 unsigned long rel_gfn = gfn - memslot->base_gfn;
6aa8b732 1401
7e9d619d
RR
1402 /* avoid RMW */
1403 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1404 set_bit(rel_gfn, memslot->dirty_bitmap);
6aa8b732
AK
1405 }
1406}
1407
b6958ce4
ED
1408/*
1409 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1410 */
8776e519 1411void kvm_vcpu_block(struct kvm_vcpu *vcpu)
d3bef15f 1412{
e5c239cf
MT
1413 DEFINE_WAIT(wait);
1414
1415 for (;;) {
1416 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1417
d7690175
MT
1418 if (kvm_cpu_has_interrupt(vcpu) ||
1419 kvm_cpu_has_pending_timer(vcpu) ||
1420 kvm_arch_vcpu_runnable(vcpu)) {
1421 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
e5c239cf 1422 break;
d7690175 1423 }
e5c239cf
MT
1424 if (signal_pending(current))
1425 break;
1426
b6958ce4
ED
1427 vcpu_put(vcpu);
1428 schedule();
1429 vcpu_load(vcpu);
1430 }
d3bef15f 1431
e5c239cf 1432 finish_wait(&vcpu->wq, &wait);
b6958ce4
ED
1433}
1434
6aa8b732
AK
1435void kvm_resched(struct kvm_vcpu *vcpu)
1436{
3fca0365
YD
1437 if (!need_resched())
1438 return;
6aa8b732 1439 cond_resched();
6aa8b732
AK
1440}
1441EXPORT_SYMBOL_GPL(kvm_resched);
1442
e4a533a4 1443static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
9a2bb7f4
AK
1444{
1445 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
9a2bb7f4
AK
1446 struct page *page;
1447
e4a533a4 1448 if (vmf->pgoff == 0)
039576c0 1449 page = virt_to_page(vcpu->run);
09566765 1450#ifdef CONFIG_X86
e4a533a4 1451 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
ad312c7c 1452 page = virt_to_page(vcpu->arch.pio_data);
5f94c174
LV
1453#endif
1454#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1455 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1456 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
09566765 1457#endif
039576c0 1458 else
e4a533a4 1459 return VM_FAULT_SIGBUS;
9a2bb7f4 1460 get_page(page);
e4a533a4 1461 vmf->page = page;
1462 return 0;
9a2bb7f4
AK
1463}
1464
1465static struct vm_operations_struct kvm_vcpu_vm_ops = {
e4a533a4 1466 .fault = kvm_vcpu_fault,
9a2bb7f4
AK
1467};
1468
1469static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1470{
1471 vma->vm_ops = &kvm_vcpu_vm_ops;
1472 return 0;
1473}
1474
bccf2150
AK
1475static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1476{
1477 struct kvm_vcpu *vcpu = filp->private_data;
1478
66c0b394 1479 kvm_put_kvm(vcpu->kvm);
bccf2150
AK
1480 return 0;
1481}
1482
5c502742 1483static const struct file_operations kvm_vcpu_fops = {
bccf2150
AK
1484 .release = kvm_vcpu_release,
1485 .unlocked_ioctl = kvm_vcpu_ioctl,
1486 .compat_ioctl = kvm_vcpu_ioctl,
9a2bb7f4 1487 .mmap = kvm_vcpu_mmap,
bccf2150
AK
1488};
1489
1490/*
1491 * Allocates an inode for the vcpu.
1492 */
1493static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1494{
7d9dbca3 1495 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
2030a42c 1496 if (fd < 0)
66c0b394 1497 kvm_put_kvm(vcpu->kvm);
bccf2150 1498 return fd;
bccf2150
AK
1499}
1500
c5ea7660
AK
1501/*
1502 * Creates some virtual cpus. Good luck creating more than one.
1503 */
1504static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1505{
1506 int r;
1507 struct kvm_vcpu *vcpu;
1508
c5ea7660 1509 if (!valid_vcpu(n))
fb3f0f51 1510 return -EINVAL;
c5ea7660 1511
e9b11c17 1512 vcpu = kvm_arch_vcpu_create(kvm, n);
fb3f0f51
RR
1513 if (IS_ERR(vcpu))
1514 return PTR_ERR(vcpu);
c5ea7660 1515
15ad7146
AK
1516 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1517
26e5215f
AK
1518 r = kvm_arch_vcpu_setup(vcpu);
1519 if (r)
7d8fece6 1520 return r;
26e5215f 1521
11ec2804 1522 mutex_lock(&kvm->lock);
fb3f0f51
RR
1523 if (kvm->vcpus[n]) {
1524 r = -EEXIST;
e9b11c17 1525 goto vcpu_destroy;
fb3f0f51
RR
1526 }
1527 kvm->vcpus[n] = vcpu;
11ec2804 1528 mutex_unlock(&kvm->lock);
c5ea7660 1529
fb3f0f51 1530 /* Now it's all set up, let userspace reach it */
66c0b394 1531 kvm_get_kvm(kvm);
bccf2150
AK
1532 r = create_vcpu_fd(vcpu);
1533 if (r < 0)
fb3f0f51
RR
1534 goto unlink;
1535 return r;
39c3b86e 1536
fb3f0f51 1537unlink:
11ec2804 1538 mutex_lock(&kvm->lock);
fb3f0f51 1539 kvm->vcpus[n] = NULL;
e9b11c17 1540vcpu_destroy:
7d8fece6 1541 mutex_unlock(&kvm->lock);
d40ccc62 1542 kvm_arch_vcpu_destroy(vcpu);
c5ea7660
AK
1543 return r;
1544}
1545
1961d276
AK
1546static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1547{
1548 if (sigset) {
1549 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1550 vcpu->sigset_active = 1;
1551 vcpu->sigset = *sigset;
1552 } else
1553 vcpu->sigset_active = 0;
1554 return 0;
1555}
1556
bccf2150
AK
1557static long kvm_vcpu_ioctl(struct file *filp,
1558 unsigned int ioctl, unsigned long arg)
6aa8b732 1559{
bccf2150 1560 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 1561 void __user *argp = (void __user *)arg;
313a3dc7 1562 int r;
fa3795a7
DH
1563 struct kvm_fpu *fpu = NULL;
1564 struct kvm_sregs *kvm_sregs = NULL;
6aa8b732 1565
6d4e4c4f
AK
1566 if (vcpu->kvm->mm != current->mm)
1567 return -EIO;
6aa8b732 1568 switch (ioctl) {
9a2bb7f4 1569 case KVM_RUN:
f0fe5108
AK
1570 r = -EINVAL;
1571 if (arg)
1572 goto out;
b6c7a5dc 1573 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
6aa8b732 1574 break;
6aa8b732 1575 case KVM_GET_REGS: {
3e4bb3ac 1576 struct kvm_regs *kvm_regs;
6aa8b732 1577
3e4bb3ac
XZ
1578 r = -ENOMEM;
1579 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1580 if (!kvm_regs)
6aa8b732 1581 goto out;
3e4bb3ac
XZ
1582 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1583 if (r)
1584 goto out_free1;
6aa8b732 1585 r = -EFAULT;
3e4bb3ac
XZ
1586 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1587 goto out_free1;
6aa8b732 1588 r = 0;
3e4bb3ac
XZ
1589out_free1:
1590 kfree(kvm_regs);
6aa8b732
AK
1591 break;
1592 }
1593 case KVM_SET_REGS: {
3e4bb3ac 1594 struct kvm_regs *kvm_regs;
6aa8b732 1595
3e4bb3ac
XZ
1596 r = -ENOMEM;
1597 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1598 if (!kvm_regs)
6aa8b732 1599 goto out;
3e4bb3ac
XZ
1600 r = -EFAULT;
1601 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1602 goto out_free2;
1603 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
6aa8b732 1604 if (r)
3e4bb3ac 1605 goto out_free2;
6aa8b732 1606 r = 0;
3e4bb3ac
XZ
1607out_free2:
1608 kfree(kvm_regs);
6aa8b732
AK
1609 break;
1610 }
1611 case KVM_GET_SREGS: {
fa3795a7
DH
1612 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1613 r = -ENOMEM;
1614 if (!kvm_sregs)
1615 goto out;
1616 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
6aa8b732
AK
1617 if (r)
1618 goto out;
1619 r = -EFAULT;
fa3795a7 1620 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
6aa8b732
AK
1621 goto out;
1622 r = 0;
1623 break;
1624 }
1625 case KVM_SET_SREGS: {
fa3795a7
DH
1626 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1627 r = -ENOMEM;
1628 if (!kvm_sregs)
1629 goto out;
6aa8b732 1630 r = -EFAULT;
fa3795a7 1631 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
6aa8b732 1632 goto out;
fa3795a7 1633 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
6aa8b732
AK
1634 if (r)
1635 goto out;
1636 r = 0;
1637 break;
1638 }
62d9f0db
MT
1639 case KVM_GET_MP_STATE: {
1640 struct kvm_mp_state mp_state;
1641
1642 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1643 if (r)
1644 goto out;
1645 r = -EFAULT;
1646 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1647 goto out;
1648 r = 0;
1649 break;
1650 }
1651 case KVM_SET_MP_STATE: {
1652 struct kvm_mp_state mp_state;
1653
1654 r = -EFAULT;
1655 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1656 goto out;
1657 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1658 if (r)
1659 goto out;
1660 r = 0;
1661 break;
1662 }
6aa8b732
AK
1663 case KVM_TRANSLATE: {
1664 struct kvm_translation tr;
1665
1666 r = -EFAULT;
2f366987 1667 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 1668 goto out;
8b006791 1669 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
1670 if (r)
1671 goto out;
1672 r = -EFAULT;
2f366987 1673 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
1674 goto out;
1675 r = 0;
1676 break;
1677 }
6aa8b732
AK
1678 case KVM_DEBUG_GUEST: {
1679 struct kvm_debug_guest dbg;
1680
1681 r = -EFAULT;
2f366987 1682 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 1683 goto out;
b6c7a5dc 1684 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
6aa8b732
AK
1685 if (r)
1686 goto out;
1687 r = 0;
1688 break;
1689 }
1961d276
AK
1690 case KVM_SET_SIGNAL_MASK: {
1691 struct kvm_signal_mask __user *sigmask_arg = argp;
1692 struct kvm_signal_mask kvm_sigmask;
1693 sigset_t sigset, *p;
1694
1695 p = NULL;
1696 if (argp) {
1697 r = -EFAULT;
1698 if (copy_from_user(&kvm_sigmask, argp,
1699 sizeof kvm_sigmask))
1700 goto out;
1701 r = -EINVAL;
1702 if (kvm_sigmask.len != sizeof sigset)
1703 goto out;
1704 r = -EFAULT;
1705 if (copy_from_user(&sigset, sigmask_arg->sigset,
1706 sizeof sigset))
1707 goto out;
1708 p = &sigset;
1709 }
1710 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1711 break;
1712 }
b8836737 1713 case KVM_GET_FPU: {
fa3795a7
DH
1714 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1715 r = -ENOMEM;
1716 if (!fpu)
1717 goto out;
1718 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
b8836737
AK
1719 if (r)
1720 goto out;
1721 r = -EFAULT;
fa3795a7 1722 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
b8836737
AK
1723 goto out;
1724 r = 0;
1725 break;
1726 }
1727 case KVM_SET_FPU: {
fa3795a7
DH
1728 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1729 r = -ENOMEM;
1730 if (!fpu)
1731 goto out;
b8836737 1732 r = -EFAULT;
fa3795a7 1733 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
b8836737 1734 goto out;
fa3795a7 1735 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
b8836737
AK
1736 if (r)
1737 goto out;
1738 r = 0;
1739 break;
1740 }
bccf2150 1741 default:
313a3dc7 1742 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
bccf2150
AK
1743 }
1744out:
fa3795a7
DH
1745 kfree(fpu);
1746 kfree(kvm_sregs);
bccf2150
AK
1747 return r;
1748}
1749
1750static long kvm_vm_ioctl(struct file *filp,
1751 unsigned int ioctl, unsigned long arg)
1752{
1753 struct kvm *kvm = filp->private_data;
1754 void __user *argp = (void __user *)arg;
1fe779f8 1755 int r;
bccf2150 1756
6d4e4c4f
AK
1757 if (kvm->mm != current->mm)
1758 return -EIO;
bccf2150
AK
1759 switch (ioctl) {
1760 case KVM_CREATE_VCPU:
1761 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1762 if (r < 0)
1763 goto out;
1764 break;
6fc138d2
IE
1765 case KVM_SET_USER_MEMORY_REGION: {
1766 struct kvm_userspace_memory_region kvm_userspace_mem;
1767
1768 r = -EFAULT;
1769 if (copy_from_user(&kvm_userspace_mem, argp,
1770 sizeof kvm_userspace_mem))
1771 goto out;
1772
1773 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
6aa8b732
AK
1774 if (r)
1775 goto out;
1776 break;
1777 }
1778 case KVM_GET_DIRTY_LOG: {
1779 struct kvm_dirty_log log;
1780
1781 r = -EFAULT;
2f366987 1782 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 1783 goto out;
2c6f5df9 1784 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
1785 if (r)
1786 goto out;
1787 break;
1788 }
5f94c174
LV
1789#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1790 case KVM_REGISTER_COALESCED_MMIO: {
1791 struct kvm_coalesced_mmio_zone zone;
1792 r = -EFAULT;
1793 if (copy_from_user(&zone, argp, sizeof zone))
1794 goto out;
1795 r = -ENXIO;
1796 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1797 if (r)
1798 goto out;
1799 r = 0;
1800 break;
1801 }
1802 case KVM_UNREGISTER_COALESCED_MMIO: {
1803 struct kvm_coalesced_mmio_zone zone;
1804 r = -EFAULT;
1805 if (copy_from_user(&zone, argp, sizeof zone))
1806 goto out;
1807 r = -ENXIO;
1808 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1809 if (r)
1810 goto out;
1811 r = 0;
1812 break;
1813 }
8a98f664
XZ
1814#endif
1815#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1816 case KVM_ASSIGN_PCI_DEVICE: {
1817 struct kvm_assigned_pci_dev assigned_dev;
1818
1819 r = -EFAULT;
1820 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1821 goto out;
1822 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1823 if (r)
1824 goto out;
1825 break;
1826 }
1827 case KVM_ASSIGN_IRQ: {
1828 struct kvm_assigned_irq assigned_irq;
1829
1830 r = -EFAULT;
1831 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1832 goto out;
1833 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1834 if (r)
1835 goto out;
1836 break;
1837 }
5f94c174 1838#endif
f17abe9a 1839 default:
1fe779f8 1840 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
f17abe9a
AK
1841 }
1842out:
1843 return r;
1844}
1845
e4a533a4 1846static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
f17abe9a 1847{
777b3f49
MT
1848 struct page *page[1];
1849 unsigned long addr;
1850 int npages;
1851 gfn_t gfn = vmf->pgoff;
f17abe9a 1852 struct kvm *kvm = vma->vm_file->private_data;
f17abe9a 1853
777b3f49
MT
1854 addr = gfn_to_hva(kvm, gfn);
1855 if (kvm_is_error_hva(addr))
e4a533a4 1856 return VM_FAULT_SIGBUS;
777b3f49
MT
1857
1858 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1859 NULL);
1860 if (unlikely(npages != 1))
e4a533a4 1861 return VM_FAULT_SIGBUS;
777b3f49
MT
1862
1863 vmf->page = page[0];
e4a533a4 1864 return 0;
f17abe9a
AK
1865}
1866
1867static struct vm_operations_struct kvm_vm_vm_ops = {
e4a533a4 1868 .fault = kvm_vm_fault,
f17abe9a
AK
1869};
1870
1871static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1872{
1873 vma->vm_ops = &kvm_vm_vm_ops;
1874 return 0;
1875}
1876
5c502742 1877static const struct file_operations kvm_vm_fops = {
f17abe9a
AK
1878 .release = kvm_vm_release,
1879 .unlocked_ioctl = kvm_vm_ioctl,
1880 .compat_ioctl = kvm_vm_ioctl,
1881 .mmap = kvm_vm_mmap,
1882};
1883
1884static int kvm_dev_ioctl_create_vm(void)
1885{
2030a42c 1886 int fd;
f17abe9a
AK
1887 struct kvm *kvm;
1888
f17abe9a 1889 kvm = kvm_create_vm();
d6d28168
AK
1890 if (IS_ERR(kvm))
1891 return PTR_ERR(kvm);
7d9dbca3 1892 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
2030a42c 1893 if (fd < 0)
66c0b394 1894 kvm_put_kvm(kvm);
f17abe9a 1895
f17abe9a 1896 return fd;
f17abe9a
AK
1897}
1898
1899static long kvm_dev_ioctl(struct file *filp,
1900 unsigned int ioctl, unsigned long arg)
1901{
07c45a36 1902 long r = -EINVAL;
f17abe9a
AK
1903
1904 switch (ioctl) {
1905 case KVM_GET_API_VERSION:
f0fe5108
AK
1906 r = -EINVAL;
1907 if (arg)
1908 goto out;
f17abe9a
AK
1909 r = KVM_API_VERSION;
1910 break;
1911 case KVM_CREATE_VM:
f0fe5108
AK
1912 r = -EINVAL;
1913 if (arg)
1914 goto out;
f17abe9a
AK
1915 r = kvm_dev_ioctl_create_vm();
1916 break;
018d00d2 1917 case KVM_CHECK_EXTENSION:
1e1c65e0 1918 r = kvm_dev_ioctl_check_extension(arg);
5d308f45 1919 break;
07c45a36
AK
1920 case KVM_GET_VCPU_MMAP_SIZE:
1921 r = -EINVAL;
1922 if (arg)
1923 goto out;
adb1ff46
AK
1924 r = PAGE_SIZE; /* struct kvm_run */
1925#ifdef CONFIG_X86
1926 r += PAGE_SIZE; /* pio data page */
5f94c174
LV
1927#endif
1928#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1929 r += PAGE_SIZE; /* coalesced mmio ring page */
adb1ff46 1930#endif
07c45a36 1931 break;
d4c9ff2d
FEL
1932 case KVM_TRACE_ENABLE:
1933 case KVM_TRACE_PAUSE:
1934 case KVM_TRACE_DISABLE:
1935 r = kvm_trace_ioctl(ioctl, arg);
1936 break;
6aa8b732 1937 default:
043405e1 1938 return kvm_arch_dev_ioctl(filp, ioctl, arg);
6aa8b732
AK
1939 }
1940out:
1941 return r;
1942}
1943
6aa8b732 1944static struct file_operations kvm_chardev_ops = {
6aa8b732
AK
1945 .unlocked_ioctl = kvm_dev_ioctl,
1946 .compat_ioctl = kvm_dev_ioctl,
6aa8b732
AK
1947};
1948
1949static struct miscdevice kvm_dev = {
bbe4432e 1950 KVM_MINOR,
6aa8b732
AK
1951 "kvm",
1952 &kvm_chardev_ops,
1953};
1954
1b6c0168
AK
1955static void hardware_enable(void *junk)
1956{
1957 int cpu = raw_smp_processor_id();
1958
1959 if (cpu_isset(cpu, cpus_hardware_enabled))
1960 return;
1961 cpu_set(cpu, cpus_hardware_enabled);
e9b11c17 1962 kvm_arch_hardware_enable(NULL);
1b6c0168
AK
1963}
1964
1965static void hardware_disable(void *junk)
1966{
1967 int cpu = raw_smp_processor_id();
1968
1969 if (!cpu_isset(cpu, cpus_hardware_enabled))
1970 return;
1971 cpu_clear(cpu, cpus_hardware_enabled);
e9b11c17 1972 kvm_arch_hardware_disable(NULL);
1b6c0168
AK
1973}
1974
774c47f1
AK
1975static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1976 void *v)
1977{
1978 int cpu = (long)v;
1979
1a6f4d7f 1980 val &= ~CPU_TASKS_FROZEN;
774c47f1 1981 switch (val) {
cec9ad27 1982 case CPU_DYING:
6ec8a856
AK
1983 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1984 cpu);
1985 hardware_disable(NULL);
1986 break;
774c47f1 1987 case CPU_UP_CANCELED:
43934a38
JK
1988 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1989 cpu);
8691e5a8 1990 smp_call_function_single(cpu, hardware_disable, NULL, 1);
774c47f1 1991 break;
43934a38
JK
1992 case CPU_ONLINE:
1993 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1994 cpu);
8691e5a8 1995 smp_call_function_single(cpu, hardware_enable, NULL, 1);
774c47f1
AK
1996 break;
1997 }
1998 return NOTIFY_OK;
1999}
2000
4ecac3fd
AK
2001
2002asmlinkage void kvm_handle_fault_on_reboot(void)
2003{
2004 if (kvm_rebooting)
2005 /* spin while reset goes on */
2006 while (true)
2007 ;
2008 /* Fault while not rebooting. We want the trace. */
2009 BUG();
2010}
2011EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2012
9a2b85c6 2013static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
d77c26fc 2014 void *v)
9a2b85c6
RR
2015{
2016 if (val == SYS_RESTART) {
2017 /*
2018 * Some (well, at least mine) BIOSes hang on reboot if
2019 * in vmx root mode.
2020 */
2021 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
4ecac3fd 2022 kvm_rebooting = true;
15c8b6c1 2023 on_each_cpu(hardware_disable, NULL, 1);
9a2b85c6
RR
2024 }
2025 return NOTIFY_OK;
2026}
2027
2028static struct notifier_block kvm_reboot_notifier = {
2029 .notifier_call = kvm_reboot,
2030 .priority = 0,
2031};
2032
2eeb2e94
GH
2033void kvm_io_bus_init(struct kvm_io_bus *bus)
2034{
2035 memset(bus, 0, sizeof(*bus));
2036}
2037
2038void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2039{
2040 int i;
2041
2042 for (i = 0; i < bus->dev_count; i++) {
2043 struct kvm_io_device *pos = bus->devs[i];
2044
2045 kvm_iodevice_destructor(pos);
2046 }
2047}
2048
92760499
LV
2049struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2050 gpa_t addr, int len, int is_write)
2eeb2e94
GH
2051{
2052 int i;
2053
2054 for (i = 0; i < bus->dev_count; i++) {
2055 struct kvm_io_device *pos = bus->devs[i];
2056
92760499 2057 if (pos->in_range(pos, addr, len, is_write))
2eeb2e94
GH
2058 return pos;
2059 }
2060
2061 return NULL;
2062}
2063
2064void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2065{
2066 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2067
2068 bus->devs[bus->dev_count++] = dev;
2069}
2070
774c47f1
AK
2071static struct notifier_block kvm_cpu_notifier = {
2072 .notifier_call = kvm_cpu_hotplug,
2073 .priority = 20, /* must be > scheduler priority */
2074};
2075
8b88b099 2076static int vm_stat_get(void *_offset, u64 *val)
ba1389b7
AK
2077{
2078 unsigned offset = (long)_offset;
ba1389b7
AK
2079 struct kvm *kvm;
2080
8b88b099 2081 *val = 0;
ba1389b7
AK
2082 spin_lock(&kvm_lock);
2083 list_for_each_entry(kvm, &vm_list, vm_list)
8b88b099 2084 *val += *(u32 *)((void *)kvm + offset);
ba1389b7 2085 spin_unlock(&kvm_lock);
8b88b099 2086 return 0;
ba1389b7
AK
2087}
2088
2089DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2090
8b88b099 2091static int vcpu_stat_get(void *_offset, u64 *val)
1165f5fe
AK
2092{
2093 unsigned offset = (long)_offset;
1165f5fe
AK
2094 struct kvm *kvm;
2095 struct kvm_vcpu *vcpu;
2096 int i;
2097
8b88b099 2098 *val = 0;
1165f5fe
AK
2099 spin_lock(&kvm_lock);
2100 list_for_each_entry(kvm, &vm_list, vm_list)
2101 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
fb3f0f51
RR
2102 vcpu = kvm->vcpus[i];
2103 if (vcpu)
8b88b099 2104 *val += *(u32 *)((void *)vcpu + offset);
1165f5fe
AK
2105 }
2106 spin_unlock(&kvm_lock);
8b88b099 2107 return 0;
1165f5fe
AK
2108}
2109
ba1389b7
AK
2110DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2111
2112static struct file_operations *stat_fops[] = {
2113 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2114 [KVM_STAT_VM] = &vm_stat_fops,
2115};
1165f5fe 2116
a16b043c 2117static void kvm_init_debug(void)
6aa8b732
AK
2118{
2119 struct kvm_stats_debugfs_item *p;
2120
76f7c879 2121 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732 2122 for (p = debugfs_entries; p->name; ++p)
76f7c879 2123 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1165f5fe 2124 (void *)(long)p->offset,
ba1389b7 2125 stat_fops[p->kind]);
6aa8b732
AK
2126}
2127
2128static void kvm_exit_debug(void)
2129{
2130 struct kvm_stats_debugfs_item *p;
2131
2132 for (p = debugfs_entries; p->name; ++p)
2133 debugfs_remove(p->dentry);
76f7c879 2134 debugfs_remove(kvm_debugfs_dir);
6aa8b732
AK
2135}
2136
59ae6c6b
AK
2137static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2138{
4267c41a 2139 hardware_disable(NULL);
59ae6c6b
AK
2140 return 0;
2141}
2142
2143static int kvm_resume(struct sys_device *dev)
2144{
4267c41a 2145 hardware_enable(NULL);
59ae6c6b
AK
2146 return 0;
2147}
2148
2149static struct sysdev_class kvm_sysdev_class = {
af5ca3f4 2150 .name = "kvm",
59ae6c6b
AK
2151 .suspend = kvm_suspend,
2152 .resume = kvm_resume,
2153};
2154
2155static struct sys_device kvm_sysdev = {
2156 .id = 0,
2157 .cls = &kvm_sysdev_class,
2158};
2159
cea7bb21 2160struct page *bad_page;
35149e21 2161pfn_t bad_pfn;
6aa8b732 2162
15ad7146
AK
2163static inline
2164struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2165{
2166 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2167}
2168
2169static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2170{
2171 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2172
e9b11c17 2173 kvm_arch_vcpu_load(vcpu, cpu);
15ad7146
AK
2174}
2175
2176static void kvm_sched_out(struct preempt_notifier *pn,
2177 struct task_struct *next)
2178{
2179 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2180
e9b11c17 2181 kvm_arch_vcpu_put(vcpu);
15ad7146
AK
2182}
2183
f8c16bba 2184int kvm_init(void *opaque, unsigned int vcpu_size,
c16f862d 2185 struct module *module)
6aa8b732
AK
2186{
2187 int r;
002c7f7c 2188 int cpu;
6aa8b732 2189
cb498ea2
ZX
2190 kvm_init_debug();
2191
f8c16bba
ZX
2192 r = kvm_arch_init(opaque);
2193 if (r)
d2308784 2194 goto out_fail;
cb498ea2
ZX
2195
2196 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2197
2198 if (bad_page == NULL) {
2199 r = -ENOMEM;
2200 goto out;
2201 }
2202
35149e21
AL
2203 bad_pfn = page_to_pfn(bad_page);
2204
e9b11c17 2205 r = kvm_arch_hardware_setup();
6aa8b732 2206 if (r < 0)
d2308784 2207 goto out_free_0;
6aa8b732 2208
002c7f7c
YS
2209 for_each_online_cpu(cpu) {
2210 smp_call_function_single(cpu,
e9b11c17 2211 kvm_arch_check_processor_compat,
8691e5a8 2212 &r, 1);
002c7f7c 2213 if (r < 0)
d2308784 2214 goto out_free_1;
002c7f7c
YS
2215 }
2216
15c8b6c1 2217 on_each_cpu(hardware_enable, NULL, 1);
774c47f1
AK
2218 r = register_cpu_notifier(&kvm_cpu_notifier);
2219 if (r)
d2308784 2220 goto out_free_2;
6aa8b732
AK
2221 register_reboot_notifier(&kvm_reboot_notifier);
2222
59ae6c6b
AK
2223 r = sysdev_class_register(&kvm_sysdev_class);
2224 if (r)
d2308784 2225 goto out_free_3;
59ae6c6b
AK
2226
2227 r = sysdev_register(&kvm_sysdev);
2228 if (r)
d2308784 2229 goto out_free_4;
59ae6c6b 2230
c16f862d
RR
2231 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2232 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
56919c5c
JP
2233 __alignof__(struct kvm_vcpu),
2234 0, NULL);
c16f862d
RR
2235 if (!kvm_vcpu_cache) {
2236 r = -ENOMEM;
d2308784 2237 goto out_free_5;
c16f862d
RR
2238 }
2239
6aa8b732
AK
2240 kvm_chardev_ops.owner = module;
2241
2242 r = misc_register(&kvm_dev);
2243 if (r) {
d77c26fc 2244 printk(KERN_ERR "kvm: misc device register failed\n");
6aa8b732
AK
2245 goto out_free;
2246 }
2247
15ad7146
AK
2248 kvm_preempt_ops.sched_in = kvm_sched_in;
2249 kvm_preempt_ops.sched_out = kvm_sched_out;
5319c662
SY
2250#ifndef CONFIG_X86
2251 msi2intx = 0;
2252#endif
15ad7146 2253
c7addb90 2254 return 0;
6aa8b732
AK
2255
2256out_free:
c16f862d 2257 kmem_cache_destroy(kvm_vcpu_cache);
d2308784 2258out_free_5:
59ae6c6b 2259 sysdev_unregister(&kvm_sysdev);
d2308784 2260out_free_4:
59ae6c6b 2261 sysdev_class_unregister(&kvm_sysdev_class);
d2308784 2262out_free_3:
6aa8b732 2263 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1 2264 unregister_cpu_notifier(&kvm_cpu_notifier);
d2308784 2265out_free_2:
15c8b6c1 2266 on_each_cpu(hardware_disable, NULL, 1);
d2308784 2267out_free_1:
e9b11c17 2268 kvm_arch_hardware_unsetup();
d2308784
ZX
2269out_free_0:
2270 __free_page(bad_page);
ca45aaae 2271out:
f8c16bba 2272 kvm_arch_exit();
cb498ea2 2273 kvm_exit_debug();
d2308784 2274out_fail:
6aa8b732
AK
2275 return r;
2276}
cb498ea2 2277EXPORT_SYMBOL_GPL(kvm_init);
6aa8b732 2278
cb498ea2 2279void kvm_exit(void)
6aa8b732 2280{
d4c9ff2d 2281 kvm_trace_cleanup();
6aa8b732 2282 misc_deregister(&kvm_dev);
c16f862d 2283 kmem_cache_destroy(kvm_vcpu_cache);
59ae6c6b
AK
2284 sysdev_unregister(&kvm_sysdev);
2285 sysdev_class_unregister(&kvm_sysdev_class);
6aa8b732 2286 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 2287 unregister_cpu_notifier(&kvm_cpu_notifier);
15c8b6c1 2288 on_each_cpu(hardware_disable, NULL, 1);
e9b11c17 2289 kvm_arch_hardware_unsetup();
f8c16bba 2290 kvm_arch_exit();
6aa8b732 2291 kvm_exit_debug();
cea7bb21 2292 __free_page(bad_page);
6aa8b732 2293}
cb498ea2 2294EXPORT_SYMBOL_GPL(kvm_exit);
This page took 0.417085 seconds and 5 git commands to generate.