KVM: Disable large pages on misaligned memory slots
[linux-2.6.git] / virt / kvm / kvm_main.c
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
18 #include "iodev.h"
19
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/percpu.h>
25 #include <linux/gfp.h>
26 #include <linux/mm.h>
27 #include <linux/miscdevice.h>
28 #include <linux/vmalloc.h>
29 #include <linux/reboot.h>
30 #include <linux/debugfs.h>
31 #include <linux/highmem.h>
32 #include <linux/file.h>
33 #include <linux/sysdev.h>
34 #include <linux/cpu.h>
35 #include <linux/sched.h>
36 #include <linux/cpumask.h>
37 #include <linux/smp.h>
38 #include <linux/anon_inodes.h>
39 #include <linux/profile.h>
40 #include <linux/kvm_para.h>
41 #include <linux/pagemap.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44 #include <linux/bitops.h>
45 #include <linux/spinlock.h>
46
47 #include <asm/processor.h>
48 #include <asm/io.h>
49 #include <asm/uaccess.h>
50 #include <asm/pgtable.h>
51
52 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
53 #include "coalesced_mmio.h"
54 #endif
55
56 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
57 #include <linux/pci.h>
58 #include <linux/interrupt.h>
59 #include "irq.h"
60 #endif
61
62 MODULE_AUTHOR("Qumranet");
63 MODULE_LICENSE("GPL");
64
65 DEFINE_SPINLOCK(kvm_lock);
66 LIST_HEAD(vm_list);
67
68 static cpumask_var_t cpus_hardware_enabled;
69
70 struct kmem_cache *kvm_vcpu_cache;
71 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
72
73 static __read_mostly struct preempt_ops kvm_preempt_ops;
74
75 struct dentry *kvm_debugfs_dir;
76
77 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
78                            unsigned long arg);
79
80 static bool kvm_rebooting;
81
82 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
83 static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
84                                                       int assigned_dev_id)
85 {
86         struct list_head *ptr;
87         struct kvm_assigned_dev_kernel *match;
88
89         list_for_each(ptr, head) {
90                 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
91                 if (match->assigned_dev_id == assigned_dev_id)
92                         return match;
93         }
94         return NULL;
95 }
96
97 static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
98                                     *assigned_dev, int irq)
99 {
100         int i, index;
101         struct msix_entry *host_msix_entries;
102
103         host_msix_entries = assigned_dev->host_msix_entries;
104
105         index = -1;
106         for (i = 0; i < assigned_dev->entries_nr; i++)
107                 if (irq == host_msix_entries[i].vector) {
108                         index = i;
109                         break;
110                 }
111         if (index < 0) {
112                 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
113                 return 0;
114         }
115
116         return index;
117 }
118
119 static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
120 {
121         struct kvm_assigned_dev_kernel *assigned_dev;
122         struct kvm *kvm;
123         int irq, i;
124
125         assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
126                                     interrupt_work);
127         kvm = assigned_dev->kvm;
128
129         /* This is taken to safely inject irq inside the guest. When
130          * the interrupt injection (or the ioapic code) uses a
131          * finer-grained lock, update this
132          */
133         mutex_lock(&kvm->lock);
134         spin_lock_irq(&assigned_dev->assigned_dev_lock);
135         if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
136                 struct kvm_guest_msix_entry *guest_entries =
137                         assigned_dev->guest_msix_entries;
138                 for (i = 0; i < assigned_dev->entries_nr; i++) {
139                         if (!(guest_entries[i].flags &
140                                         KVM_ASSIGNED_MSIX_PENDING))
141                                 continue;
142                         guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
143                         kvm_set_irq(assigned_dev->kvm,
144                                     assigned_dev->irq_source_id,
145                                     guest_entries[i].vector, 1);
146                         irq = assigned_dev->host_msix_entries[i].vector;
147                         if (irq != 0)
148                                 enable_irq(irq);
149                         assigned_dev->host_irq_disabled = false;
150                 }
151         } else {
152                 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
153                             assigned_dev->guest_irq, 1);
154                 if (assigned_dev->irq_requested_type &
155                                 KVM_DEV_IRQ_GUEST_MSI) {
156                         enable_irq(assigned_dev->host_irq);
157                         assigned_dev->host_irq_disabled = false;
158                 }
159         }
160
161         spin_unlock_irq(&assigned_dev->assigned_dev_lock);
162         mutex_unlock(&assigned_dev->kvm->lock);
163 }
164
165 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
166 {
167         unsigned long flags;
168         struct kvm_assigned_dev_kernel *assigned_dev =
169                 (struct kvm_assigned_dev_kernel *) dev_id;
170
171         spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
172         if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
173                 int index = find_index_from_host_irq(assigned_dev, irq);
174                 if (index < 0)
175                         goto out;
176                 assigned_dev->guest_msix_entries[index].flags |=
177                         KVM_ASSIGNED_MSIX_PENDING;
178         }
179
180         schedule_work(&assigned_dev->interrupt_work);
181
182         disable_irq_nosync(irq);
183         assigned_dev->host_irq_disabled = true;
184
185 out:
186         spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
187         return IRQ_HANDLED;
188 }
189
190 /* Ack the irq line for an assigned device */
191 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
192 {
193         struct kvm_assigned_dev_kernel *dev;
194         unsigned long flags;
195
196         if (kian->gsi == -1)
197                 return;
198
199         dev = container_of(kian, struct kvm_assigned_dev_kernel,
200                            ack_notifier);
201
202         kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
203
204         /* The guest irq may be shared so this ack may be
205          * from another device.
206          */
207         spin_lock_irqsave(&dev->assigned_dev_lock, flags);
208         if (dev->host_irq_disabled) {
209                 enable_irq(dev->host_irq);
210                 dev->host_irq_disabled = false;
211         }
212         spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
213 }
214
215 static void deassign_guest_irq(struct kvm *kvm,
216                                struct kvm_assigned_dev_kernel *assigned_dev)
217 {
218         kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
219         assigned_dev->ack_notifier.gsi = -1;
220
221         if (assigned_dev->irq_source_id != -1)
222                 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
223         assigned_dev->irq_source_id = -1;
224         assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
225 }
226
227 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
228 static void deassign_host_irq(struct kvm *kvm,
229                               struct kvm_assigned_dev_kernel *assigned_dev)
230 {
231         /*
232          * In kvm_free_device_irq, cancel_work_sync return true if:
233          * 1. work is scheduled, and then cancelled.
234          * 2. work callback is executed.
235          *
236          * The first one ensured that the irq is disabled and no more events
237          * would happen. But for the second one, the irq may be enabled (e.g.
238          * for MSI). So we disable irq here to prevent further events.
239          *
240          * Notice this maybe result in nested disable if the interrupt type is
241          * INTx, but it's OK for we are going to free it.
242          *
243          * If this function is a part of VM destroy, please ensure that till
244          * now, the kvm state is still legal for probably we also have to wait
245          * interrupt_work done.
246          */
247         if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
248                 int i;
249                 for (i = 0; i < assigned_dev->entries_nr; i++)
250                         disable_irq_nosync(assigned_dev->
251                                            host_msix_entries[i].vector);
252
253                 cancel_work_sync(&assigned_dev->interrupt_work);
254
255                 for (i = 0; i < assigned_dev->entries_nr; i++)
256                         free_irq(assigned_dev->host_msix_entries[i].vector,
257                                  (void *)assigned_dev);
258
259                 assigned_dev->entries_nr = 0;
260                 kfree(assigned_dev->host_msix_entries);
261                 kfree(assigned_dev->guest_msix_entries);
262                 pci_disable_msix(assigned_dev->dev);
263         } else {
264                 /* Deal with MSI and INTx */
265                 disable_irq_nosync(assigned_dev->host_irq);
266                 cancel_work_sync(&assigned_dev->interrupt_work);
267
268                 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
269
270                 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
271                         pci_disable_msi(assigned_dev->dev);
272         }
273
274         assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
275 }
276
277 static int kvm_deassign_irq(struct kvm *kvm,
278                             struct kvm_assigned_dev_kernel *assigned_dev,
279                             unsigned long irq_requested_type)
280 {
281         unsigned long guest_irq_type, host_irq_type;
282
283         if (!irqchip_in_kernel(kvm))
284                 return -EINVAL;
285         /* no irq assignment to deassign */
286         if (!assigned_dev->irq_requested_type)
287                 return -ENXIO;
288
289         host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
290         guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
291
292         if (host_irq_type)
293                 deassign_host_irq(kvm, assigned_dev);
294         if (guest_irq_type)
295                 deassign_guest_irq(kvm, assigned_dev);
296
297         return 0;
298 }
299
300 static void kvm_free_assigned_irq(struct kvm *kvm,
301                                   struct kvm_assigned_dev_kernel *assigned_dev)
302 {
303         kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
304 }
305
306 static void kvm_free_assigned_device(struct kvm *kvm,
307                                      struct kvm_assigned_dev_kernel
308                                      *assigned_dev)
309 {
310         kvm_free_assigned_irq(kvm, assigned_dev);
311
312         pci_reset_function(assigned_dev->dev);
313
314         pci_release_regions(assigned_dev->dev);
315         pci_disable_device(assigned_dev->dev);
316         pci_dev_put(assigned_dev->dev);
317
318         list_del(&assigned_dev->list);
319         kfree(assigned_dev);
320 }
321
322 void kvm_free_all_assigned_devices(struct kvm *kvm)
323 {
324         struct list_head *ptr, *ptr2;
325         struct kvm_assigned_dev_kernel *assigned_dev;
326
327         list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
328                 assigned_dev = list_entry(ptr,
329                                           struct kvm_assigned_dev_kernel,
330                                           list);
331
332                 kvm_free_assigned_device(kvm, assigned_dev);
333         }
334 }
335
336 static int assigned_device_enable_host_intx(struct kvm *kvm,
337                                             struct kvm_assigned_dev_kernel *dev)
338 {
339         dev->host_irq = dev->dev->irq;
340         /* Even though this is PCI, we don't want to use shared
341          * interrupts. Sharing host devices with guest-assigned devices
342          * on the same interrupt line is not a happy situation: there
343          * are going to be long delays in accepting, acking, etc.
344          */
345         if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
346                         0, "kvm_assigned_intx_device", (void *)dev))
347                 return -EIO;
348         return 0;
349 }
350
351 #ifdef __KVM_HAVE_MSI
352 static int assigned_device_enable_host_msi(struct kvm *kvm,
353                                            struct kvm_assigned_dev_kernel *dev)
354 {
355         int r;
356
357         if (!dev->dev->msi_enabled) {
358                 r = pci_enable_msi(dev->dev);
359                 if (r)
360                         return r;
361         }
362
363         dev->host_irq = dev->dev->irq;
364         if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
365                         "kvm_assigned_msi_device", (void *)dev)) {
366                 pci_disable_msi(dev->dev);
367                 return -EIO;
368         }
369
370         return 0;
371 }
372 #endif
373
374 #ifdef __KVM_HAVE_MSIX
375 static int assigned_device_enable_host_msix(struct kvm *kvm,
376                                             struct kvm_assigned_dev_kernel *dev)
377 {
378         int i, r = -EINVAL;
379
380         /* host_msix_entries and guest_msix_entries should have been
381          * initialized */
382         if (dev->entries_nr == 0)
383                 return r;
384
385         r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
386         if (r)
387                 return r;
388
389         for (i = 0; i < dev->entries_nr; i++) {
390                 r = request_irq(dev->host_msix_entries[i].vector,
391                                 kvm_assigned_dev_intr, 0,
392                                 "kvm_assigned_msix_device",
393                                 (void *)dev);
394                 /* FIXME: free requested_irq's on failure */
395                 if (r)
396                         return r;
397         }
398
399         return 0;
400 }
401
402 #endif
403
404 static int assigned_device_enable_guest_intx(struct kvm *kvm,
405                                 struct kvm_assigned_dev_kernel *dev,
406                                 struct kvm_assigned_irq *irq)
407 {
408         dev->guest_irq = irq->guest_irq;
409         dev->ack_notifier.gsi = irq->guest_irq;
410         return 0;
411 }
412
413 #ifdef __KVM_HAVE_MSI
414 static int assigned_device_enable_guest_msi(struct kvm *kvm,
415                         struct kvm_assigned_dev_kernel *dev,
416                         struct kvm_assigned_irq *irq)
417 {
418         dev->guest_irq = irq->guest_irq;
419         dev->ack_notifier.gsi = -1;
420         return 0;
421 }
422 #endif
423 #ifdef __KVM_HAVE_MSIX
424 static int assigned_device_enable_guest_msix(struct kvm *kvm,
425                         struct kvm_assigned_dev_kernel *dev,
426                         struct kvm_assigned_irq *irq)
427 {
428         dev->guest_irq = irq->guest_irq;
429         dev->ack_notifier.gsi = -1;
430         return 0;
431 }
432 #endif
433
434 static int assign_host_irq(struct kvm *kvm,
435                            struct kvm_assigned_dev_kernel *dev,
436                            __u32 host_irq_type)
437 {
438         int r = -EEXIST;
439
440         if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
441                 return r;
442
443         switch (host_irq_type) {
444         case KVM_DEV_IRQ_HOST_INTX:
445                 r = assigned_device_enable_host_intx(kvm, dev);
446                 break;
447 #ifdef __KVM_HAVE_MSI
448         case KVM_DEV_IRQ_HOST_MSI:
449                 r = assigned_device_enable_host_msi(kvm, dev);
450                 break;
451 #endif
452 #ifdef __KVM_HAVE_MSIX
453         case KVM_DEV_IRQ_HOST_MSIX:
454                 r = assigned_device_enable_host_msix(kvm, dev);
455                 break;
456 #endif
457         default:
458                 r = -EINVAL;
459         }
460
461         if (!r)
462                 dev->irq_requested_type |= host_irq_type;
463
464         return r;
465 }
466
467 static int assign_guest_irq(struct kvm *kvm,
468                             struct kvm_assigned_dev_kernel *dev,
469                             struct kvm_assigned_irq *irq,
470                             unsigned long guest_irq_type)
471 {
472         int id;
473         int r = -EEXIST;
474
475         if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
476                 return r;
477
478         id = kvm_request_irq_source_id(kvm);
479         if (id < 0)
480                 return id;
481
482         dev->irq_source_id = id;
483
484         switch (guest_irq_type) {
485         case KVM_DEV_IRQ_GUEST_INTX:
486                 r = assigned_device_enable_guest_intx(kvm, dev, irq);
487                 break;
488 #ifdef __KVM_HAVE_MSI
489         case KVM_DEV_IRQ_GUEST_MSI:
490                 r = assigned_device_enable_guest_msi(kvm, dev, irq);
491                 break;
492 #endif
493 #ifdef __KVM_HAVE_MSIX
494         case KVM_DEV_IRQ_GUEST_MSIX:
495                 r = assigned_device_enable_guest_msix(kvm, dev, irq);
496                 break;
497 #endif
498         default:
499                 r = -EINVAL;
500         }
501
502         if (!r) {
503                 dev->irq_requested_type |= guest_irq_type;
504                 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
505         } else
506                 kvm_free_irq_source_id(kvm, dev->irq_source_id);
507
508         return r;
509 }
510
511 /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
512 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
513                                    struct kvm_assigned_irq *assigned_irq)
514 {
515         int r = -EINVAL;
516         struct kvm_assigned_dev_kernel *match;
517         unsigned long host_irq_type, guest_irq_type;
518
519         if (!capable(CAP_SYS_RAWIO))
520                 return -EPERM;
521
522         if (!irqchip_in_kernel(kvm))
523                 return r;
524
525         mutex_lock(&kvm->lock);
526         r = -ENODEV;
527         match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
528                                       assigned_irq->assigned_dev_id);
529         if (!match)
530                 goto out;
531
532         host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
533         guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
534
535         r = -EINVAL;
536         /* can only assign one type at a time */
537         if (hweight_long(host_irq_type) > 1)
538                 goto out;
539         if (hweight_long(guest_irq_type) > 1)
540                 goto out;
541         if (host_irq_type == 0 && guest_irq_type == 0)
542                 goto out;
543
544         r = 0;
545         if (host_irq_type)
546                 r = assign_host_irq(kvm, match, host_irq_type);
547         if (r)
548                 goto out;
549
550         if (guest_irq_type)
551                 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
552 out:
553         mutex_unlock(&kvm->lock);
554         return r;
555 }
556
557 static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
558                                          struct kvm_assigned_irq
559                                          *assigned_irq)
560 {
561         int r = -ENODEV;
562         struct kvm_assigned_dev_kernel *match;
563
564         mutex_lock(&kvm->lock);
565
566         match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
567                                       assigned_irq->assigned_dev_id);
568         if (!match)
569                 goto out;
570
571         r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
572 out:
573         mutex_unlock(&kvm->lock);
574         return r;
575 }
576
577 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
578                                       struct kvm_assigned_pci_dev *assigned_dev)
579 {
580         int r = 0;
581         struct kvm_assigned_dev_kernel *match;
582         struct pci_dev *dev;
583
584         down_read(&kvm->slots_lock);
585         mutex_lock(&kvm->lock);
586
587         match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
588                                       assigned_dev->assigned_dev_id);
589         if (match) {
590                 /* device already assigned */
591                 r = -EEXIST;
592                 goto out;
593         }
594
595         match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
596         if (match == NULL) {
597                 printk(KERN_INFO "%s: Couldn't allocate memory\n",
598                        __func__);
599                 r = -ENOMEM;
600                 goto out;
601         }
602         dev = pci_get_bus_and_slot(assigned_dev->busnr,
603                                    assigned_dev->devfn);
604         if (!dev) {
605                 printk(KERN_INFO "%s: host device not found\n", __func__);
606                 r = -EINVAL;
607                 goto out_free;
608         }
609         if (pci_enable_device(dev)) {
610                 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
611                 r = -EBUSY;
612                 goto out_put;
613         }
614         r = pci_request_regions(dev, "kvm_assigned_device");
615         if (r) {
616                 printk(KERN_INFO "%s: Could not get access to device regions\n",
617                        __func__);
618                 goto out_disable;
619         }
620
621         pci_reset_function(dev);
622
623         match->assigned_dev_id = assigned_dev->assigned_dev_id;
624         match->host_busnr = assigned_dev->busnr;
625         match->host_devfn = assigned_dev->devfn;
626         match->flags = assigned_dev->flags;
627         match->dev = dev;
628         spin_lock_init(&match->assigned_dev_lock);
629         match->irq_source_id = -1;
630         match->kvm = kvm;
631         match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
632         INIT_WORK(&match->interrupt_work,
633                   kvm_assigned_dev_interrupt_work_handler);
634
635         list_add(&match->list, &kvm->arch.assigned_dev_head);
636
637         if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
638                 if (!kvm->arch.iommu_domain) {
639                         r = kvm_iommu_map_guest(kvm);
640                         if (r)
641                                 goto out_list_del;
642                 }
643                 r = kvm_assign_device(kvm, match);
644                 if (r)
645                         goto out_list_del;
646         }
647
648 out:
649         mutex_unlock(&kvm->lock);
650         up_read(&kvm->slots_lock);
651         return r;
652 out_list_del:
653         list_del(&match->list);
654         pci_release_regions(dev);
655 out_disable:
656         pci_disable_device(dev);
657 out_put:
658         pci_dev_put(dev);
659 out_free:
660         kfree(match);
661         mutex_unlock(&kvm->lock);
662         up_read(&kvm->slots_lock);
663         return r;
664 }
665 #endif
666
667 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
668 static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
669                 struct kvm_assigned_pci_dev *assigned_dev)
670 {
671         int r = 0;
672         struct kvm_assigned_dev_kernel *match;
673
674         mutex_lock(&kvm->lock);
675
676         match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
677                                       assigned_dev->assigned_dev_id);
678         if (!match) {
679                 printk(KERN_INFO "%s: device hasn't been assigned before, "
680                   "so cannot be deassigned\n", __func__);
681                 r = -EINVAL;
682                 goto out;
683         }
684
685         if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
686                 kvm_deassign_device(kvm, match);
687
688         kvm_free_assigned_device(kvm, match);
689
690 out:
691         mutex_unlock(&kvm->lock);
692         return r;
693 }
694 #endif
695
696 static inline int valid_vcpu(int n)
697 {
698         return likely(n >= 0 && n < KVM_MAX_VCPUS);
699 }
700
701 inline int kvm_is_mmio_pfn(pfn_t pfn)
702 {
703         if (pfn_valid(pfn)) {
704                 struct page *page = compound_head(pfn_to_page(pfn));
705                 return PageReserved(page);
706         }
707
708         return true;
709 }
710
711 /*
712  * Switches to specified vcpu, until a matching vcpu_put()
713  */
714 void vcpu_load(struct kvm_vcpu *vcpu)
715 {
716         int cpu;
717
718         mutex_lock(&vcpu->mutex);
719         cpu = get_cpu();
720         preempt_notifier_register(&vcpu->preempt_notifier);
721         kvm_arch_vcpu_load(vcpu, cpu);
722         put_cpu();
723 }
724
725 void vcpu_put(struct kvm_vcpu *vcpu)
726 {
727         preempt_disable();
728         kvm_arch_vcpu_put(vcpu);
729         preempt_notifier_unregister(&vcpu->preempt_notifier);
730         preempt_enable();
731         mutex_unlock(&vcpu->mutex);
732 }
733
734 static void ack_flush(void *_completed)
735 {
736 }
737
738 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
739 {
740         int i, cpu, me;
741         cpumask_var_t cpus;
742         bool called = true;
743         struct kvm_vcpu *vcpu;
744
745         if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
746                 cpumask_clear(cpus);
747
748         me = get_cpu();
749         for (i = 0; i < KVM_MAX_VCPUS; ++i) {
750                 vcpu = kvm->vcpus[i];
751                 if (!vcpu)
752                         continue;
753                 if (test_and_set_bit(req, &vcpu->requests))
754                         continue;
755                 cpu = vcpu->cpu;
756                 if (cpus != NULL && cpu != -1 && cpu != me)
757                         cpumask_set_cpu(cpu, cpus);
758         }
759         if (unlikely(cpus == NULL))
760                 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
761         else if (!cpumask_empty(cpus))
762                 smp_call_function_many(cpus, ack_flush, NULL, 1);
763         else
764                 called = false;
765         put_cpu();
766         free_cpumask_var(cpus);
767         return called;
768 }
769
770 void kvm_flush_remote_tlbs(struct kvm *kvm)
771 {
772         if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
773                 ++kvm->stat.remote_tlb_flush;
774 }
775
776 void kvm_reload_remote_mmus(struct kvm *kvm)
777 {
778         make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
779 }
780
781 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
782 {
783         struct page *page;
784         int r;
785
786         mutex_init(&vcpu->mutex);
787         vcpu->cpu = -1;
788         vcpu->kvm = kvm;
789         vcpu->vcpu_id = id;
790         init_waitqueue_head(&vcpu->wq);
791
792         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
793         if (!page) {
794                 r = -ENOMEM;
795                 goto fail;
796         }
797         vcpu->run = page_address(page);
798
799         r = kvm_arch_vcpu_init(vcpu);
800         if (r < 0)
801                 goto fail_free_run;
802         return 0;
803
804 fail_free_run:
805         free_page((unsigned long)vcpu->run);
806 fail:
807         return r;
808 }
809 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
810
811 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
812 {
813         kvm_arch_vcpu_uninit(vcpu);
814         free_page((unsigned long)vcpu->run);
815 }
816 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
817
818 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
819 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
820 {
821         return container_of(mn, struct kvm, mmu_notifier);
822 }
823
824 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
825                                              struct mm_struct *mm,
826                                              unsigned long address)
827 {
828         struct kvm *kvm = mmu_notifier_to_kvm(mn);
829         int need_tlb_flush;
830
831         /*
832          * When ->invalidate_page runs, the linux pte has been zapped
833          * already but the page is still allocated until
834          * ->invalidate_page returns. So if we increase the sequence
835          * here the kvm page fault will notice if the spte can't be
836          * established because the page is going to be freed. If
837          * instead the kvm page fault establishes the spte before
838          * ->invalidate_page runs, kvm_unmap_hva will release it
839          * before returning.
840          *
841          * The sequence increase only need to be seen at spin_unlock
842          * time, and not at spin_lock time.
843          *
844          * Increasing the sequence after the spin_unlock would be
845          * unsafe because the kvm page fault could then establish the
846          * pte after kvm_unmap_hva returned, without noticing the page
847          * is going to be freed.
848          */
849         spin_lock(&kvm->mmu_lock);
850         kvm->mmu_notifier_seq++;
851         need_tlb_flush = kvm_unmap_hva(kvm, address);
852         spin_unlock(&kvm->mmu_lock);
853
854         /* we've to flush the tlb before the pages can be freed */
855         if (need_tlb_flush)
856                 kvm_flush_remote_tlbs(kvm);
857
858 }
859
860 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
861                                                     struct mm_struct *mm,
862                                                     unsigned long start,
863                                                     unsigned long end)
864 {
865         struct kvm *kvm = mmu_notifier_to_kvm(mn);
866         int need_tlb_flush = 0;
867
868         spin_lock(&kvm->mmu_lock);
869         /*
870          * The count increase must become visible at unlock time as no
871          * spte can be established without taking the mmu_lock and
872          * count is also read inside the mmu_lock critical section.
873          */
874         kvm->mmu_notifier_count++;
875         for (; start < end; start += PAGE_SIZE)
876                 need_tlb_flush |= kvm_unmap_hva(kvm, start);
877         spin_unlock(&kvm->mmu_lock);
878
879         /* we've to flush the tlb before the pages can be freed */
880         if (need_tlb_flush)
881                 kvm_flush_remote_tlbs(kvm);
882 }
883
884 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
885                                                   struct mm_struct *mm,
886                                                   unsigned long start,
887                                                   unsigned long end)
888 {
889         struct kvm *kvm = mmu_notifier_to_kvm(mn);
890
891         spin_lock(&kvm->mmu_lock);
892         /*
893          * This sequence increase will notify the kvm page fault that
894          * the page that is going to be mapped in the spte could have
895          * been freed.
896          */
897         kvm->mmu_notifier_seq++;
898         /*
899          * The above sequence increase must be visible before the
900          * below count decrease but both values are read by the kvm
901          * page fault under mmu_lock spinlock so we don't need to add
902          * a smb_wmb() here in between the two.
903          */
904         kvm->mmu_notifier_count--;
905         spin_unlock(&kvm->mmu_lock);
906
907         BUG_ON(kvm->mmu_notifier_count < 0);
908 }
909
910 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
911                                               struct mm_struct *mm,
912                                               unsigned long address)
913 {
914         struct kvm *kvm = mmu_notifier_to_kvm(mn);
915         int young;
916
917         spin_lock(&kvm->mmu_lock);
918         young = kvm_age_hva(kvm, address);
919         spin_unlock(&kvm->mmu_lock);
920
921         if (young)
922                 kvm_flush_remote_tlbs(kvm);
923
924         return young;
925 }
926
927 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
928                                      struct mm_struct *mm)
929 {
930         struct kvm *kvm = mmu_notifier_to_kvm(mn);
931         kvm_arch_flush_shadow(kvm);
932 }
933
934 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
935         .invalidate_page        = kvm_mmu_notifier_invalidate_page,
936         .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
937         .invalidate_range_end   = kvm_mmu_notifier_invalidate_range_end,
938         .clear_flush_young      = kvm_mmu_notifier_clear_flush_young,
939         .release                = kvm_mmu_notifier_release,
940 };
941 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
942
943 static struct kvm *kvm_create_vm(void)
944 {
945         struct kvm *kvm = kvm_arch_create_vm();
946 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
947         struct page *page;
948 #endif
949
950         if (IS_ERR(kvm))
951                 goto out;
952 #ifdef CONFIG_HAVE_KVM_IRQCHIP
953         INIT_LIST_HEAD(&kvm->irq_routing);
954         INIT_HLIST_HEAD(&kvm->mask_notifier_list);
955 #endif
956
957 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
958         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
959         if (!page) {
960                 kfree(kvm);
961                 return ERR_PTR(-ENOMEM);
962         }
963         kvm->coalesced_mmio_ring =
964                         (struct kvm_coalesced_mmio_ring *)page_address(page);
965 #endif
966
967 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
968         {
969                 int err;
970                 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
971                 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
972                 if (err) {
973 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
974                         put_page(page);
975 #endif
976                         kfree(kvm);
977                         return ERR_PTR(err);
978                 }
979         }
980 #endif
981
982         kvm->mm = current->mm;
983         atomic_inc(&kvm->mm->mm_count);
984         spin_lock_init(&kvm->mmu_lock);
985         kvm_io_bus_init(&kvm->pio_bus);
986         mutex_init(&kvm->lock);
987         kvm_io_bus_init(&kvm->mmio_bus);
988         init_rwsem(&kvm->slots_lock);
989         atomic_set(&kvm->users_count, 1);
990         spin_lock(&kvm_lock);
991         list_add(&kvm->vm_list, &vm_list);
992         spin_unlock(&kvm_lock);
993 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
994         kvm_coalesced_mmio_init(kvm);
995 #endif
996 out:
997         return kvm;
998 }
999
1000 /*
1001  * Free any memory in @free but not in @dont.
1002  */
1003 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1004                                   struct kvm_memory_slot *dont)
1005 {
1006         if (!dont || free->rmap != dont->rmap)
1007                 vfree(free->rmap);
1008
1009         if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1010                 vfree(free->dirty_bitmap);
1011
1012         if (!dont || free->lpage_info != dont->lpage_info)
1013                 vfree(free->lpage_info);
1014
1015         free->npages = 0;
1016         free->dirty_bitmap = NULL;
1017         free->rmap = NULL;
1018         free->lpage_info = NULL;
1019 }
1020
1021 void kvm_free_physmem(struct kvm *kvm)
1022 {
1023         int i;
1024
1025         for (i = 0; i < kvm->nmemslots; ++i)
1026                 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
1027 }
1028
1029 static void kvm_destroy_vm(struct kvm *kvm)
1030 {
1031         struct mm_struct *mm = kvm->mm;
1032
1033         kvm_arch_sync_events(kvm);
1034         spin_lock(&kvm_lock);
1035         list_del(&kvm->vm_list);
1036         spin_unlock(&kvm_lock);
1037         kvm_free_irq_routing(kvm);
1038         kvm_io_bus_destroy(&kvm->pio_bus);
1039         kvm_io_bus_destroy(&kvm->mmio_bus);
1040 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1041         if (kvm->coalesced_mmio_ring != NULL)
1042                 free_page((unsigned long)kvm->coalesced_mmio_ring);
1043 #endif
1044 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1045         mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
1046 #else
1047         kvm_arch_flush_shadow(kvm);
1048 #endif
1049         kvm_arch_destroy_vm(kvm);
1050         mmdrop(mm);
1051 }
1052
1053 void kvm_get_kvm(struct kvm *kvm)
1054 {
1055         atomic_inc(&kvm->users_count);
1056 }
1057 EXPORT_SYMBOL_GPL(kvm_get_kvm);
1058
1059 void kvm_put_kvm(struct kvm *kvm)
1060 {
1061         if (atomic_dec_and_test(&kvm->users_count))
1062                 kvm_destroy_vm(kvm);
1063 }
1064 EXPORT_SYMBOL_GPL(kvm_put_kvm);
1065
1066
1067 static int kvm_vm_release(struct inode *inode, struct file *filp)
1068 {
1069         struct kvm *kvm = filp->private_data;
1070
1071         kvm_put_kvm(kvm);
1072         return 0;
1073 }
1074
1075 /*
1076  * Allocate some memory and give it an address in the guest physical address
1077  * space.
1078  *
1079  * Discontiguous memory is allowed, mostly for framebuffers.
1080  *
1081  * Must be called holding mmap_sem for write.
1082  */
1083 int __kvm_set_memory_region(struct kvm *kvm,
1084                             struct kvm_userspace_memory_region *mem,
1085                             int user_alloc)
1086 {
1087         int r;
1088         gfn_t base_gfn;
1089         unsigned long npages, ugfn;
1090         int largepages;
1091         unsigned long i;
1092         struct kvm_memory_slot *memslot;
1093         struct kvm_memory_slot old, new;
1094
1095         r = -EINVAL;
1096         /* General sanity checks */
1097         if (mem->memory_size & (PAGE_SIZE - 1))
1098                 goto out;
1099         if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1100                 goto out;
1101         if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
1102                 goto out;
1103         if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
1104                 goto out;
1105         if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1106                 goto out;
1107
1108         memslot = &kvm->memslots[mem->slot];
1109         base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1110         npages = mem->memory_size >> PAGE_SHIFT;
1111
1112         if (!npages)
1113                 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1114
1115         new = old = *memslot;
1116
1117         new.base_gfn = base_gfn;
1118         new.npages = npages;
1119         new.flags = mem->flags;
1120
1121         /* Disallow changing a memory slot's size. */
1122         r = -EINVAL;
1123         if (npages && old.npages && npages != old.npages)
1124                 goto out_free;
1125
1126         /* Check for overlaps */
1127         r = -EEXIST;
1128         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1129                 struct kvm_memory_slot *s = &kvm->memslots[i];
1130
1131                 if (s == memslot || !s->npages)
1132                         continue;
1133                 if (!((base_gfn + npages <= s->base_gfn) ||
1134                       (base_gfn >= s->base_gfn + s->npages)))
1135                         goto out_free;
1136         }
1137
1138         /* Free page dirty bitmap if unneeded */
1139         if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
1140                 new.dirty_bitmap = NULL;
1141
1142         r = -ENOMEM;
1143
1144         /* Allocate if a slot is being created */
1145 #ifndef CONFIG_S390
1146         if (npages && !new.rmap) {
1147                 new.rmap = vmalloc(npages * sizeof(struct page *));
1148
1149                 if (!new.rmap)
1150                         goto out_free;
1151
1152                 memset(new.rmap, 0, npages * sizeof(*new.rmap));
1153
1154                 new.user_alloc = user_alloc;
1155                 /*
1156                  * hva_to_rmmap() serialzies with the mmu_lock and to be
1157                  * safe it has to ignore memslots with !user_alloc &&
1158                  * !userspace_addr.
1159                  */
1160                 if (user_alloc)
1161                         new.userspace_addr = mem->userspace_addr;
1162                 else
1163                         new.userspace_addr = 0;
1164         }
1165         if (npages && !new.lpage_info) {
1166                 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1167                 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
1168
1169                 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1170
1171                 if (!new.lpage_info)
1172                         goto out_free;
1173
1174                 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1175
1176                 if (base_gfn % KVM_PAGES_PER_HPAGE)
1177                         new.lpage_info[0].write_count = 1;
1178                 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1179                         new.lpage_info[largepages-1].write_count = 1;
1180                 ugfn = new.userspace_addr >> PAGE_SHIFT;
1181                 /*
1182                  * If the gfn and userspace address are not aligned wrt each
1183                  * other, disable large page support for this slot
1184                  */
1185                 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1))
1186                         for (i = 0; i < largepages; ++i)
1187                                 new.lpage_info[i].write_count = 1;
1188         }
1189
1190         /* Allocate page dirty bitmap if needed */
1191         if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1192                 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1193
1194                 new.dirty_bitmap = vmalloc(dirty_bytes);
1195                 if (!new.dirty_bitmap)
1196                         goto out_free;
1197                 memset(new.dirty_bitmap, 0, dirty_bytes);
1198         }
1199 #endif /* not defined CONFIG_S390 */
1200
1201         if (!npages)
1202                 kvm_arch_flush_shadow(kvm);
1203
1204         spin_lock(&kvm->mmu_lock);
1205         if (mem->slot >= kvm->nmemslots)
1206                 kvm->nmemslots = mem->slot + 1;
1207
1208         *memslot = new;
1209         spin_unlock(&kvm->mmu_lock);
1210
1211         r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1212         if (r) {
1213                 spin_lock(&kvm->mmu_lock);
1214                 *memslot = old;
1215                 spin_unlock(&kvm->mmu_lock);
1216                 goto out_free;
1217         }
1218
1219         kvm_free_physmem_slot(&old, npages ? &new : NULL);
1220         /* Slot deletion case: we have to update the current slot */
1221         spin_lock(&kvm->mmu_lock);
1222         if (!npages)
1223                 *memslot = old;
1224         spin_unlock(&kvm->mmu_lock);
1225 #ifdef CONFIG_DMAR
1226         /* map the pages in iommu page table */
1227         r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1228         if (r)
1229                 goto out;
1230 #endif
1231         return 0;
1232
1233 out_free:
1234         kvm_free_physmem_slot(&new, &old);
1235 out:
1236         return r;
1237
1238 }
1239 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1240
1241 int kvm_set_memory_region(struct kvm *kvm,
1242                           struct kvm_userspace_memory_region *mem,
1243                           int user_alloc)
1244 {
1245         int r;
1246
1247         down_write(&kvm->slots_lock);
1248         r = __kvm_set_memory_region(kvm, mem, user_alloc);
1249         up_write(&kvm->slots_lock);
1250         return r;
1251 }
1252 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1253
1254 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1255                                    struct
1256                                    kvm_userspace_memory_region *mem,
1257                                    int user_alloc)
1258 {
1259         if (mem->slot >= KVM_MEMORY_SLOTS)
1260                 return -EINVAL;
1261         return kvm_set_memory_region(kvm, mem, user_alloc);
1262 }
1263
1264 int kvm_get_dirty_log(struct kvm *kvm,
1265                         struct kvm_dirty_log *log, int *is_dirty)
1266 {
1267         struct kvm_memory_slot *memslot;
1268         int r, i;
1269         int n;
1270         unsigned long any = 0;
1271
1272         r = -EINVAL;
1273         if (log->slot >= KVM_MEMORY_SLOTS)
1274                 goto out;
1275
1276         memslot = &kvm->memslots[log->slot];
1277         r = -ENOENT;
1278         if (!memslot->dirty_bitmap)
1279                 goto out;
1280
1281         n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1282
1283         for (i = 0; !any && i < n/sizeof(long); ++i)
1284                 any = memslot->dirty_bitmap[i];
1285
1286         r = -EFAULT;
1287         if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1288                 goto out;
1289
1290         if (any)
1291                 *is_dirty = 1;
1292
1293         r = 0;
1294 out:
1295         return r;
1296 }
1297
1298 int is_error_page(struct page *page)
1299 {
1300         return page == bad_page;
1301 }
1302 EXPORT_SYMBOL_GPL(is_error_page);
1303
1304 int is_error_pfn(pfn_t pfn)
1305 {
1306         return pfn == bad_pfn;
1307 }
1308 EXPORT_SYMBOL_GPL(is_error_pfn);
1309
1310 static inline unsigned long bad_hva(void)
1311 {
1312         return PAGE_OFFSET;
1313 }
1314
1315 int kvm_is_error_hva(unsigned long addr)
1316 {
1317         return addr == bad_hva();
1318 }
1319 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1320
1321 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1322 {
1323         int i;
1324
1325         for (i = 0; i < kvm->nmemslots; ++i) {
1326                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1327
1328                 if (gfn >= memslot->base_gfn
1329                     && gfn < memslot->base_gfn + memslot->npages)
1330                         return memslot;
1331         }
1332         return NULL;
1333 }
1334 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1335
1336 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1337 {
1338         gfn = unalias_gfn(kvm, gfn);
1339         return gfn_to_memslot_unaliased(kvm, gfn);
1340 }
1341
1342 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1343 {
1344         int i;
1345
1346         gfn = unalias_gfn(kvm, gfn);
1347         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1348                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1349
1350                 if (gfn >= memslot->base_gfn
1351                     && gfn < memslot->base_gfn + memslot->npages)
1352                         return 1;
1353         }
1354         return 0;
1355 }
1356 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1357
1358 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1359 {
1360         struct kvm_memory_slot *slot;
1361
1362         gfn = unalias_gfn(kvm, gfn);
1363         slot = gfn_to_memslot_unaliased(kvm, gfn);
1364         if (!slot)
1365                 return bad_hva();
1366         return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1367 }
1368 EXPORT_SYMBOL_GPL(gfn_to_hva);
1369
1370 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1371 {
1372         struct page *page[1];
1373         unsigned long addr;
1374         int npages;
1375         pfn_t pfn;
1376
1377         might_sleep();
1378
1379         addr = gfn_to_hva(kvm, gfn);
1380         if (kvm_is_error_hva(addr)) {
1381                 get_page(bad_page);
1382                 return page_to_pfn(bad_page);
1383         }
1384
1385         npages = get_user_pages_fast(addr, 1, 1, page);
1386
1387         if (unlikely(npages != 1)) {
1388                 struct vm_area_struct *vma;
1389
1390                 down_read(&current->mm->mmap_sem);
1391                 vma = find_vma(current->mm, addr);
1392
1393                 if (vma == NULL || addr < vma->vm_start ||
1394                     !(vma->vm_flags & VM_PFNMAP)) {
1395                         up_read(&current->mm->mmap_sem);
1396                         get_page(bad_page);
1397                         return page_to_pfn(bad_page);
1398                 }
1399
1400                 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1401                 up_read(&current->mm->mmap_sem);
1402                 BUG_ON(!kvm_is_mmio_pfn(pfn));
1403         } else
1404                 pfn = page_to_pfn(page[0]);
1405
1406         return pfn;
1407 }
1408
1409 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1410
1411 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1412 {
1413         pfn_t pfn;
1414
1415         pfn = gfn_to_pfn(kvm, gfn);
1416         if (!kvm_is_mmio_pfn(pfn))
1417                 return pfn_to_page(pfn);
1418
1419         WARN_ON(kvm_is_mmio_pfn(pfn));
1420
1421         get_page(bad_page);
1422         return bad_page;
1423 }
1424
1425 EXPORT_SYMBOL_GPL(gfn_to_page);
1426
1427 void kvm_release_page_clean(struct page *page)
1428 {
1429         kvm_release_pfn_clean(page_to_pfn(page));
1430 }
1431 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1432
1433 void kvm_release_pfn_clean(pfn_t pfn)
1434 {
1435         if (!kvm_is_mmio_pfn(pfn))
1436                 put_page(pfn_to_page(pfn));
1437 }
1438 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1439
1440 void kvm_release_page_dirty(struct page *page)
1441 {
1442         kvm_release_pfn_dirty(page_to_pfn(page));
1443 }
1444 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1445
1446 void kvm_release_pfn_dirty(pfn_t pfn)
1447 {
1448         kvm_set_pfn_dirty(pfn);
1449         kvm_release_pfn_clean(pfn);
1450 }
1451 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1452
1453 void kvm_set_page_dirty(struct page *page)
1454 {
1455         kvm_set_pfn_dirty(page_to_pfn(page));
1456 }
1457 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1458
1459 void kvm_set_pfn_dirty(pfn_t pfn)
1460 {
1461         if (!kvm_is_mmio_pfn(pfn)) {
1462                 struct page *page = pfn_to_page(pfn);
1463                 if (!PageReserved(page))
1464                         SetPageDirty(page);
1465         }
1466 }
1467 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1468
1469 void kvm_set_pfn_accessed(pfn_t pfn)
1470 {
1471         if (!kvm_is_mmio_pfn(pfn))
1472                 mark_page_accessed(pfn_to_page(pfn));
1473 }
1474 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1475
1476 void kvm_get_pfn(pfn_t pfn)
1477 {
1478         if (!kvm_is_mmio_pfn(pfn))
1479                 get_page(pfn_to_page(pfn));
1480 }
1481 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1482
1483 static int next_segment(unsigned long len, int offset)
1484 {
1485         if (len > PAGE_SIZE - offset)
1486                 return PAGE_SIZE - offset;
1487         else
1488                 return len;
1489 }
1490
1491 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1492                         int len)
1493 {
1494         int r;
1495         unsigned long addr;
1496
1497         addr = gfn_to_hva(kvm, gfn);
1498         if (kvm_is_error_hva(addr))
1499                 return -EFAULT;
1500         r = copy_from_user(data, (void __user *)addr + offset, len);
1501         if (r)
1502                 return -EFAULT;
1503         return 0;
1504 }
1505 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1506
1507 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1508 {
1509         gfn_t gfn = gpa >> PAGE_SHIFT;
1510         int seg;
1511         int offset = offset_in_page(gpa);
1512         int ret;
1513
1514         while ((seg = next_segment(len, offset)) != 0) {
1515                 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1516                 if (ret < 0)
1517                         return ret;
1518                 offset = 0;
1519                 len -= seg;
1520                 data += seg;
1521                 ++gfn;
1522         }
1523         return 0;
1524 }
1525 EXPORT_SYMBOL_GPL(kvm_read_guest);
1526
1527 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1528                           unsigned long len)
1529 {
1530         int r;
1531         unsigned long addr;
1532         gfn_t gfn = gpa >> PAGE_SHIFT;
1533         int offset = offset_in_page(gpa);
1534
1535         addr = gfn_to_hva(kvm, gfn);
1536         if (kvm_is_error_hva(addr))
1537                 return -EFAULT;
1538         pagefault_disable();
1539         r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1540         pagefault_enable();
1541         if (r)
1542                 return -EFAULT;
1543         return 0;
1544 }
1545 EXPORT_SYMBOL(kvm_read_guest_atomic);
1546
1547 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1548                          int offset, int len)
1549 {
1550         int r;
1551         unsigned long addr;
1552
1553         addr = gfn_to_hva(kvm, gfn);
1554         if (kvm_is_error_hva(addr))
1555                 return -EFAULT;
1556         r = copy_to_user((void __user *)addr + offset, data, len);
1557         if (r)
1558                 return -EFAULT;
1559         mark_page_dirty(kvm, gfn);
1560         return 0;
1561 }
1562 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1563
1564 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1565                     unsigned long len)
1566 {
1567         gfn_t gfn = gpa >> PAGE_SHIFT;
1568         int seg;
1569         int offset = offset_in_page(gpa);
1570         int ret;
1571
1572         while ((seg = next_segment(len, offset)) != 0) {
1573                 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1574                 if (ret < 0)
1575                         return ret;
1576                 offset = 0;
1577                 len -= seg;
1578                 data += seg;
1579                 ++gfn;
1580         }
1581         return 0;
1582 }
1583
1584 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1585 {
1586         return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1587 }
1588 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1589
1590 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1591 {
1592         gfn_t gfn = gpa >> PAGE_SHIFT;
1593         int seg;
1594         int offset = offset_in_page(gpa);
1595         int ret;
1596
1597         while ((seg = next_segment(len, offset)) != 0) {
1598                 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1599                 if (ret < 0)
1600                         return ret;
1601                 offset = 0;
1602                 len -= seg;
1603                 ++gfn;
1604         }
1605         return 0;
1606 }
1607 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1608
1609 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1610 {
1611         struct kvm_memory_slot *memslot;
1612
1613         gfn = unalias_gfn(kvm, gfn);
1614         memslot = gfn_to_memslot_unaliased(kvm, gfn);
1615         if (memslot && memslot->dirty_bitmap) {
1616                 unsigned long rel_gfn = gfn - memslot->base_gfn;
1617
1618                 /* avoid RMW */
1619                 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1620                         set_bit(rel_gfn, memslot->dirty_bitmap);
1621         }
1622 }
1623
1624 /*
1625  * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1626  */
1627 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1628 {
1629         DEFINE_WAIT(wait);
1630
1631         for (;;) {
1632                 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1633
1634                 if ((kvm_arch_interrupt_allowed(vcpu) &&
1635                                         kvm_cpu_has_interrupt(vcpu)) ||
1636                                 kvm_arch_vcpu_runnable(vcpu)) {
1637                         set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1638                         break;
1639                 }
1640                 if (kvm_cpu_has_pending_timer(vcpu))
1641                         break;
1642                 if (signal_pending(current))
1643                         break;
1644
1645                 vcpu_put(vcpu);
1646                 schedule();
1647                 vcpu_load(vcpu);
1648         }
1649
1650         finish_wait(&vcpu->wq, &wait);
1651 }
1652
1653 void kvm_resched(struct kvm_vcpu *vcpu)
1654 {
1655         if (!need_resched())
1656                 return;
1657         cond_resched();
1658 }
1659 EXPORT_SYMBOL_GPL(kvm_resched);
1660
1661 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1662 {
1663         struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1664         struct page *page;
1665
1666         if (vmf->pgoff == 0)
1667                 page = virt_to_page(vcpu->run);
1668 #ifdef CONFIG_X86
1669         else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1670                 page = virt_to_page(vcpu->arch.pio_data);
1671 #endif
1672 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1673         else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1674                 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1675 #endif
1676         else
1677                 return VM_FAULT_SIGBUS;
1678         get_page(page);
1679         vmf->page = page;
1680         return 0;
1681 }
1682
1683 static struct vm_operations_struct kvm_vcpu_vm_ops = {
1684         .fault = kvm_vcpu_fault,
1685 };
1686
1687 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1688 {
1689         vma->vm_ops = &kvm_vcpu_vm_ops;
1690         return 0;
1691 }
1692
1693 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1694 {
1695         struct kvm_vcpu *vcpu = filp->private_data;
1696
1697         kvm_put_kvm(vcpu->kvm);
1698         return 0;
1699 }
1700
1701 static struct file_operations kvm_vcpu_fops = {
1702         .release        = kvm_vcpu_release,
1703         .unlocked_ioctl = kvm_vcpu_ioctl,
1704         .compat_ioctl   = kvm_vcpu_ioctl,
1705         .mmap           = kvm_vcpu_mmap,
1706 };
1707
1708 /*
1709  * Allocates an inode for the vcpu.
1710  */
1711 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1712 {
1713         int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1714         if (fd < 0)
1715                 kvm_put_kvm(vcpu->kvm);
1716         return fd;
1717 }
1718
1719 /*
1720  * Creates some virtual cpus.  Good luck creating more than one.
1721  */
1722 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1723 {
1724         int r;
1725         struct kvm_vcpu *vcpu;
1726
1727         if (!valid_vcpu(n))
1728                 return -EINVAL;
1729
1730         vcpu = kvm_arch_vcpu_create(kvm, n);
1731         if (IS_ERR(vcpu))
1732                 return PTR_ERR(vcpu);
1733
1734         preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1735
1736         r = kvm_arch_vcpu_setup(vcpu);
1737         if (r)
1738                 return r;
1739
1740         mutex_lock(&kvm->lock);
1741         if (kvm->vcpus[n]) {
1742                 r = -EEXIST;
1743                 goto vcpu_destroy;
1744         }
1745         kvm->vcpus[n] = vcpu;
1746         mutex_unlock(&kvm->lock);
1747
1748         /* Now it's all set up, let userspace reach it */
1749         kvm_get_kvm(kvm);
1750         r = create_vcpu_fd(vcpu);
1751         if (r < 0)
1752                 goto unlink;
1753         return r;
1754
1755 unlink:
1756         mutex_lock(&kvm->lock);
1757         kvm->vcpus[n] = NULL;
1758 vcpu_destroy:
1759         mutex_unlock(&kvm->lock);
1760         kvm_arch_vcpu_destroy(vcpu);
1761         return r;
1762 }
1763
1764 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1765 {
1766         if (sigset) {
1767                 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1768                 vcpu->sigset_active = 1;
1769                 vcpu->sigset = *sigset;
1770         } else
1771                 vcpu->sigset_active = 0;
1772         return 0;
1773 }
1774
1775 #ifdef __KVM_HAVE_MSIX
1776 static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1777                                     struct kvm_assigned_msix_nr *entry_nr)
1778 {
1779         int r = 0;
1780         struct kvm_assigned_dev_kernel *adev;
1781
1782         mutex_lock(&kvm->lock);
1783
1784         adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1785                                       entry_nr->assigned_dev_id);
1786         if (!adev) {
1787                 r = -EINVAL;
1788                 goto msix_nr_out;
1789         }
1790
1791         if (adev->entries_nr == 0) {
1792                 adev->entries_nr = entry_nr->entry_nr;
1793                 if (adev->entries_nr == 0 ||
1794                     adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1795                         r = -EINVAL;
1796                         goto msix_nr_out;
1797                 }
1798
1799                 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1800                                                 entry_nr->entry_nr,
1801                                                 GFP_KERNEL);
1802                 if (!adev->host_msix_entries) {
1803                         r = -ENOMEM;
1804                         goto msix_nr_out;
1805                 }
1806                 adev->guest_msix_entries = kzalloc(
1807                                 sizeof(struct kvm_guest_msix_entry) *
1808                                 entry_nr->entry_nr, GFP_KERNEL);
1809                 if (!adev->guest_msix_entries) {
1810                         kfree(adev->host_msix_entries);
1811                         r = -ENOMEM;
1812                         goto msix_nr_out;
1813                 }
1814         } else /* Not allowed set MSI-X number twice */
1815                 r = -EINVAL;
1816 msix_nr_out:
1817         mutex_unlock(&kvm->lock);
1818         return r;
1819 }
1820
1821 static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1822                                        struct kvm_assigned_msix_entry *entry)
1823 {
1824         int r = 0, i;
1825         struct kvm_assigned_dev_kernel *adev;
1826
1827         mutex_lock(&kvm->lock);
1828
1829         adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1830                                       entry->assigned_dev_id);
1831
1832         if (!adev) {
1833                 r = -EINVAL;
1834                 goto msix_entry_out;
1835         }
1836
1837         for (i = 0; i < adev->entries_nr; i++)
1838                 if (adev->guest_msix_entries[i].vector == 0 ||
1839                     adev->guest_msix_entries[i].entry == entry->entry) {
1840                         adev->guest_msix_entries[i].entry = entry->entry;
1841                         adev->guest_msix_entries[i].vector = entry->gsi;
1842                         adev->host_msix_entries[i].entry = entry->entry;
1843                         break;
1844                 }
1845         if (i == adev->entries_nr) {
1846                 r = -ENOSPC;
1847                 goto msix_entry_out;
1848         }
1849
1850 msix_entry_out:
1851         mutex_unlock(&kvm->lock);
1852
1853         return r;
1854 }
1855 #endif
1856
1857 static long kvm_vcpu_ioctl(struct file *filp,
1858                            unsigned int ioctl, unsigned long arg)
1859 {
1860         struct kvm_vcpu *vcpu = filp->private_data;
1861         void __user *argp = (void __user *)arg;
1862         int r;
1863         struct kvm_fpu *fpu = NULL;
1864         struct kvm_sregs *kvm_sregs = NULL;
1865
1866         if (vcpu->kvm->mm != current->mm)
1867                 return -EIO;
1868         switch (ioctl) {
1869         case KVM_RUN:
1870                 r = -EINVAL;
1871                 if (arg)
1872                         goto out;
1873                 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1874                 break;
1875         case KVM_GET_REGS: {
1876                 struct kvm_regs *kvm_regs;
1877
1878                 r = -ENOMEM;
1879                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1880                 if (!kvm_regs)
1881                         goto out;
1882                 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1883                 if (r)
1884                         goto out_free1;
1885                 r = -EFAULT;
1886                 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1887                         goto out_free1;
1888                 r = 0;
1889 out_free1:
1890                 kfree(kvm_regs);
1891                 break;
1892         }
1893         case KVM_SET_REGS: {
1894                 struct kvm_regs *kvm_regs;
1895
1896                 r = -ENOMEM;
1897                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1898                 if (!kvm_regs)
1899                         goto out;
1900                 r = -EFAULT;
1901                 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1902                         goto out_free2;
1903                 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1904                 if (r)
1905                         goto out_free2;
1906                 r = 0;
1907 out_free2:
1908                 kfree(kvm_regs);
1909                 break;
1910         }
1911         case KVM_GET_SREGS: {
1912                 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1913                 r = -ENOMEM;
1914                 if (!kvm_sregs)
1915                         goto out;
1916                 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1917                 if (r)
1918                         goto out;
1919                 r = -EFAULT;
1920                 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1921                         goto out;
1922                 r = 0;
1923                 break;
1924         }
1925         case KVM_SET_SREGS: {
1926                 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1927                 r = -ENOMEM;
1928                 if (!kvm_sregs)
1929                         goto out;
1930                 r = -EFAULT;
1931                 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1932                         goto out;
1933                 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1934                 if (r)
1935                         goto out;
1936                 r = 0;
1937                 break;
1938         }
1939         case KVM_GET_MP_STATE: {
1940                 struct kvm_mp_state mp_state;
1941
1942                 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1943                 if (r)
1944                         goto out;
1945                 r = -EFAULT;
1946                 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1947                         goto out;
1948                 r = 0;
1949                 break;
1950         }
1951         case KVM_SET_MP_STATE: {
1952                 struct kvm_mp_state mp_state;
1953
1954                 r = -EFAULT;
1955                 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1956                         goto out;
1957                 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1958                 if (r)
1959                         goto out;
1960                 r = 0;
1961                 break;
1962         }
1963         case KVM_TRANSLATE: {
1964                 struct kvm_translation tr;
1965
1966                 r = -EFAULT;
1967                 if (copy_from_user(&tr, argp, sizeof tr))
1968                         goto out;
1969                 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1970                 if (r)
1971                         goto out;
1972                 r = -EFAULT;
1973                 if (copy_to_user(argp, &tr, sizeof tr))
1974                         goto out;
1975                 r = 0;
1976                 break;
1977         }
1978         case KVM_SET_GUEST_DEBUG: {
1979                 struct kvm_guest_debug dbg;
1980
1981                 r = -EFAULT;
1982                 if (copy_from_user(&dbg, argp, sizeof dbg))
1983                         goto out;
1984                 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
1985                 if (r)
1986                         goto out;
1987                 r = 0;
1988                 break;
1989         }
1990         case KVM_SET_SIGNAL_MASK: {
1991                 struct kvm_signal_mask __user *sigmask_arg = argp;
1992                 struct kvm_signal_mask kvm_sigmask;
1993                 sigset_t sigset, *p;
1994
1995                 p = NULL;
1996                 if (argp) {
1997                         r = -EFAULT;
1998                         if (copy_from_user(&kvm_sigmask, argp,
1999                                            sizeof kvm_sigmask))
2000                                 goto out;
2001                         r = -EINVAL;
2002                         if (kvm_sigmask.len != sizeof sigset)
2003                                 goto out;
2004                         r = -EFAULT;
2005                         if (copy_from_user(&sigset, sigmask_arg->sigset,
2006                                            sizeof sigset))
2007                                 goto out;
2008                         p = &sigset;
2009                 }
2010                 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2011                 break;
2012         }
2013         case KVM_GET_FPU: {
2014                 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2015                 r = -ENOMEM;
2016                 if (!fpu)
2017                         goto out;
2018                 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
2019                 if (r)
2020                         goto out;
2021                 r = -EFAULT;
2022                 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
2023                         goto out;
2024                 r = 0;
2025                 break;
2026         }
2027         case KVM_SET_FPU: {
2028                 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2029                 r = -ENOMEM;
2030                 if (!fpu)
2031                         goto out;
2032                 r = -EFAULT;
2033                 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2034                         goto out;
2035                 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
2036                 if (r)
2037                         goto out;
2038                 r = 0;
2039                 break;
2040         }
2041         default:
2042                 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
2043         }
2044 out:
2045         kfree(fpu);
2046         kfree(kvm_sregs);
2047         return r;
2048 }
2049
2050 static long kvm_vm_ioctl(struct file *filp,
2051                            unsigned int ioctl, unsigned long arg)
2052 {
2053         struct kvm *kvm = filp->private_data;
2054         void __user *argp = (void __user *)arg;
2055         int r;
2056
2057         if (kvm->mm != current->mm)
2058                 return -EIO;
2059         switch (ioctl) {
2060         case KVM_CREATE_VCPU:
2061                 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2062                 if (r < 0)
2063                         goto out;
2064                 break;
2065         case KVM_SET_USER_MEMORY_REGION: {
2066                 struct kvm_userspace_memory_region kvm_userspace_mem;
2067
2068                 r = -EFAULT;
2069                 if (copy_from_user(&kvm_userspace_mem, argp,
2070                                                 sizeof kvm_userspace_mem))
2071                         goto out;
2072
2073                 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
2074                 if (r)
2075                         goto out;
2076                 break;
2077         }
2078         case KVM_GET_DIRTY_LOG: {
2079                 struct kvm_dirty_log log;
2080
2081                 r = -EFAULT;
2082                 if (copy_from_user(&log, argp, sizeof log))
2083                         goto out;
2084                 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2085                 if (r)
2086                         goto out;
2087                 break;
2088         }
2089 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2090         case KVM_REGISTER_COALESCED_MMIO: {
2091                 struct kvm_coalesced_mmio_zone zone;
2092                 r = -EFAULT;
2093                 if (copy_from_user(&zone, argp, sizeof zone))
2094                         goto out;
2095                 r = -ENXIO;
2096                 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2097                 if (r)
2098                         goto out;
2099                 r = 0;
2100                 break;
2101         }
2102         case KVM_UNREGISTER_COALESCED_MMIO: {
2103                 struct kvm_coalesced_mmio_zone zone;
2104                 r = -EFAULT;
2105                 if (copy_from_user(&zone, argp, sizeof zone))
2106                         goto out;
2107                 r = -ENXIO;
2108                 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2109                 if (r)
2110                         goto out;
2111                 r = 0;
2112                 break;
2113         }
2114 #endif
2115 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
2116         case KVM_ASSIGN_PCI_DEVICE: {
2117                 struct kvm_assigned_pci_dev assigned_dev;
2118
2119                 r = -EFAULT;
2120                 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2121                         goto out;
2122                 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2123                 if (r)
2124                         goto out;
2125                 break;
2126         }
2127         case KVM_ASSIGN_IRQ: {
2128                 r = -EOPNOTSUPP;
2129                 break;
2130         }
2131 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
2132         case KVM_ASSIGN_DEV_IRQ: {
2133                 struct kvm_assigned_irq assigned_irq;
2134
2135                 r = -EFAULT;
2136                 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2137                         goto out;
2138                 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2139                 if (r)
2140                         goto out;
2141                 break;
2142         }
2143         case KVM_DEASSIGN_DEV_IRQ: {
2144                 struct kvm_assigned_irq assigned_irq;
2145
2146                 r = -EFAULT;
2147                 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2148                         goto out;
2149                 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2150                 if (r)
2151                         goto out;
2152                 break;
2153         }
2154 #endif
2155 #endif
2156 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2157         case KVM_DEASSIGN_PCI_DEVICE: {
2158                 struct kvm_assigned_pci_dev assigned_dev;
2159
2160                 r = -EFAULT;
2161                 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2162                         goto out;
2163                 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2164                 if (r)
2165                         goto out;
2166                 break;
2167         }
2168 #endif
2169 #ifdef KVM_CAP_IRQ_ROUTING
2170         case KVM_SET_GSI_ROUTING: {
2171                 struct kvm_irq_routing routing;
2172                 struct kvm_irq_routing __user *urouting;
2173                 struct kvm_irq_routing_entry *entries;
2174
2175                 r = -EFAULT;
2176                 if (copy_from_user(&routing, argp, sizeof(routing)))
2177                         goto out;
2178                 r = -EINVAL;
2179                 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2180                         goto out;
2181                 if (routing.flags)
2182                         goto out;
2183                 r = -ENOMEM;
2184                 entries = vmalloc(routing.nr * sizeof(*entries));
2185                 if (!entries)
2186                         goto out;
2187                 r = -EFAULT;
2188                 urouting = argp;
2189                 if (copy_from_user(entries, urouting->entries,
2190                                    routing.nr * sizeof(*entries)))
2191                         goto out_free_irq_routing;
2192                 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2193                                         routing.flags);
2194         out_free_irq_routing:
2195                 vfree(entries);
2196                 break;
2197         }
2198 #ifdef __KVM_HAVE_MSIX
2199         case KVM_ASSIGN_SET_MSIX_NR: {
2200                 struct kvm_assigned_msix_nr entry_nr;
2201                 r = -EFAULT;
2202                 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2203                         goto out;
2204                 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2205                 if (r)
2206                         goto out;
2207                 break;
2208         }
2209         case KVM_ASSIGN_SET_MSIX_ENTRY: {
2210                 struct kvm_assigned_msix_entry entry;
2211                 r = -EFAULT;
2212                 if (copy_from_user(&entry, argp, sizeof entry))
2213                         goto out;
2214                 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2215                 if (r)
2216                         goto out;
2217                 break;
2218         }
2219 #endif
2220 #endif /* KVM_CAP_IRQ_ROUTING */
2221         default:
2222                 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2223         }
2224 out:
2225         return r;
2226 }
2227
2228 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2229 {
2230         struct page *page[1];
2231         unsigned long addr;
2232         int npages;
2233         gfn_t gfn = vmf->pgoff;
2234         struct kvm *kvm = vma->vm_file->private_data;
2235
2236         addr = gfn_to_hva(kvm, gfn);
2237         if (kvm_is_error_hva(addr))
2238                 return VM_FAULT_SIGBUS;
2239
2240         npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2241                                 NULL);
2242         if (unlikely(npages != 1))
2243                 return VM_FAULT_SIGBUS;
2244
2245         vmf->page = page[0];
2246         return 0;
2247 }
2248
2249 static struct vm_operations_struct kvm_vm_vm_ops = {
2250         .fault = kvm_vm_fault,
2251 };
2252
2253 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2254 {
2255         vma->vm_ops = &kvm_vm_vm_ops;
2256         return 0;
2257 }
2258
2259 static struct file_operations kvm_vm_fops = {
2260         .release        = kvm_vm_release,
2261         .unlocked_ioctl = kvm_vm_ioctl,
2262         .compat_ioctl   = kvm_vm_ioctl,
2263         .mmap           = kvm_vm_mmap,
2264 };
2265
2266 static int kvm_dev_ioctl_create_vm(void)
2267 {
2268         int fd;
2269         struct kvm *kvm;
2270
2271         kvm = kvm_create_vm();
2272         if (IS_ERR(kvm))
2273                 return PTR_ERR(kvm);
2274         fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
2275         if (fd < 0)
2276                 kvm_put_kvm(kvm);
2277
2278         return fd;
2279 }
2280
2281 static long kvm_dev_ioctl_check_extension_generic(long arg)
2282 {
2283         switch (arg) {
2284         case KVM_CAP_USER_MEMORY:
2285         case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2286         case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2287                 return 1;
2288 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2289         case KVM_CAP_IRQ_ROUTING:
2290                 return KVM_MAX_IRQ_ROUTES;
2291 #endif
2292         default:
2293                 break;
2294         }
2295         return kvm_dev_ioctl_check_extension(arg);
2296 }
2297
2298 static long kvm_dev_ioctl(struct file *filp,
2299                           unsigned int ioctl, unsigned long arg)
2300 {
2301         long r = -EINVAL;
2302
2303         switch (ioctl) {
2304         case KVM_GET_API_VERSION:
2305                 r = -EINVAL;
2306                 if (arg)
2307                         goto out;
2308                 r = KVM_API_VERSION;
2309                 break;
2310         case KVM_CREATE_VM:
2311                 r = -EINVAL;
2312                 if (arg)
2313                         goto out;
2314                 r = kvm_dev_ioctl_create_vm();
2315                 break;
2316         case KVM_CHECK_EXTENSION:
2317                 r = kvm_dev_ioctl_check_extension_generic(arg);
2318                 break;
2319         case KVM_GET_VCPU_MMAP_SIZE:
2320                 r = -EINVAL;
2321                 if (arg)
2322                         goto out;
2323                 r = PAGE_SIZE;     /* struct kvm_run */
2324 #ifdef CONFIG_X86
2325                 r += PAGE_SIZE;    /* pio data page */
2326 #endif
2327 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2328                 r += PAGE_SIZE;    /* coalesced mmio ring page */
2329 #endif
2330                 break;
2331         case KVM_TRACE_ENABLE:
2332         case KVM_TRACE_PAUSE:
2333         case KVM_TRACE_DISABLE:
2334                 r = kvm_trace_ioctl(ioctl, arg);
2335                 break;
2336         default:
2337                 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2338         }
2339 out:
2340         return r;
2341 }
2342
2343 static struct file_operations kvm_chardev_ops = {
2344         .unlocked_ioctl = kvm_dev_ioctl,
2345         .compat_ioctl   = kvm_dev_ioctl,
2346 };
2347
2348 static struct miscdevice kvm_dev = {
2349         KVM_MINOR,
2350         "kvm",
2351         &kvm_chardev_ops,
2352 };
2353
2354 static void hardware_enable(void *junk)
2355 {
2356         int cpu = raw_smp_processor_id();
2357
2358         if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2359                 return;
2360         cpumask_set_cpu(cpu, cpus_hardware_enabled);
2361         kvm_arch_hardware_enable(NULL);
2362 }
2363
2364 static void hardware_disable(void *junk)
2365 {
2366         int cpu = raw_smp_processor_id();
2367
2368         if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2369                 return;
2370         cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2371         kvm_arch_hardware_disable(NULL);
2372 }
2373
2374 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2375                            void *v)
2376 {
2377         int cpu = (long)v;
2378
2379         val &= ~CPU_TASKS_FROZEN;
2380         switch (val) {
2381         case CPU_DYING:
2382                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2383                        cpu);
2384                 hardware_disable(NULL);
2385                 break;
2386         case CPU_UP_CANCELED:
2387                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2388                        cpu);
2389                 smp_call_function_single(cpu, hardware_disable, NULL, 1);
2390                 break;
2391         case CPU_ONLINE:
2392                 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2393                        cpu);
2394                 smp_call_function_single(cpu, hardware_enable, NULL, 1);
2395                 break;
2396         }
2397         return NOTIFY_OK;
2398 }
2399
2400
2401 asmlinkage void kvm_handle_fault_on_reboot(void)
2402 {
2403         if (kvm_rebooting)
2404                 /* spin while reset goes on */
2405                 while (true)
2406                         ;
2407         /* Fault while not rebooting.  We want the trace. */
2408         BUG();
2409 }
2410 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2411
2412 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2413                       void *v)
2414 {
2415         /*
2416          * Some (well, at least mine) BIOSes hang on reboot if
2417          * in vmx root mode.
2418          *
2419          * And Intel TXT required VMX off for all cpu when system shutdown.
2420          */
2421         printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2422         kvm_rebooting = true;
2423         on_each_cpu(hardware_disable, NULL, 1);
2424         return NOTIFY_OK;
2425 }
2426
2427 static struct notifier_block kvm_reboot_notifier = {
2428         .notifier_call = kvm_reboot,
2429         .priority = 0,
2430 };
2431
2432 void kvm_io_bus_init(struct kvm_io_bus *bus)
2433 {
2434         memset(bus, 0, sizeof(*bus));
2435 }
2436
2437 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2438 {
2439         int i;
2440
2441         for (i = 0; i < bus->dev_count; i++) {
2442                 struct kvm_io_device *pos = bus->devs[i];
2443
2444                 kvm_iodevice_destructor(pos);
2445         }
2446 }
2447
2448 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2449                                           gpa_t addr, int len, int is_write)
2450 {
2451         int i;
2452
2453         for (i = 0; i < bus->dev_count; i++) {
2454                 struct kvm_io_device *pos = bus->devs[i];
2455
2456                 if (pos->in_range(pos, addr, len, is_write))
2457                         return pos;
2458         }
2459
2460         return NULL;
2461 }
2462
2463 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2464 {
2465         BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2466
2467         bus->devs[bus->dev_count++] = dev;
2468 }
2469
2470 static struct notifier_block kvm_cpu_notifier = {
2471         .notifier_call = kvm_cpu_hotplug,
2472         .priority = 20, /* must be > scheduler priority */
2473 };
2474
2475 static int vm_stat_get(void *_offset, u64 *val)
2476 {
2477         unsigned offset = (long)_offset;
2478         struct kvm *kvm;
2479
2480         *val = 0;
2481         spin_lock(&kvm_lock);
2482         list_for_each_entry(kvm, &vm_list, vm_list)
2483                 *val += *(u32 *)((void *)kvm + offset);
2484         spin_unlock(&kvm_lock);
2485         return 0;
2486 }
2487
2488 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2489
2490 static int vcpu_stat_get(void *_offset, u64 *val)
2491 {
2492         unsigned offset = (long)_offset;
2493         struct kvm *kvm;
2494         struct kvm_vcpu *vcpu;
2495         int i;
2496
2497         *val = 0;
2498         spin_lock(&kvm_lock);
2499         list_for_each_entry(kvm, &vm_list, vm_list)
2500                 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2501                         vcpu = kvm->vcpus[i];
2502                         if (vcpu)
2503                                 *val += *(u32 *)((void *)vcpu + offset);
2504                 }
2505         spin_unlock(&kvm_lock);
2506         return 0;
2507 }
2508
2509 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2510
2511 static struct file_operations *stat_fops[] = {
2512         [KVM_STAT_VCPU] = &vcpu_stat_fops,
2513         [KVM_STAT_VM]   = &vm_stat_fops,
2514 };
2515
2516 static void kvm_init_debug(void)
2517 {
2518         struct kvm_stats_debugfs_item *p;
2519
2520         kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2521         for (p = debugfs_entries; p->name; ++p)
2522                 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2523                                                 (void *)(long)p->offset,
2524                                                 stat_fops[p->kind]);
2525 }
2526
2527 static void kvm_exit_debug(void)
2528 {
2529         struct kvm_stats_debugfs_item *p;
2530
2531         for (p = debugfs_entries; p->name; ++p)
2532                 debugfs_remove(p->dentry);
2533         debugfs_remove(kvm_debugfs_dir);
2534 }
2535
2536 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2537 {
2538         hardware_disable(NULL);
2539         return 0;
2540 }
2541
2542 static int kvm_resume(struct sys_device *dev)
2543 {
2544         hardware_enable(NULL);
2545         return 0;
2546 }
2547
2548 static struct sysdev_class kvm_sysdev_class = {
2549         .name = "kvm",
2550         .suspend = kvm_suspend,
2551         .resume = kvm_resume,
2552 };
2553
2554 static struct sys_device kvm_sysdev = {
2555         .id = 0,
2556         .cls = &kvm_sysdev_class,
2557 };
2558
2559 struct page *bad_page;
2560 pfn_t bad_pfn;
2561
2562 static inline
2563 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2564 {
2565         return container_of(pn, struct kvm_vcpu, preempt_notifier);
2566 }
2567
2568 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2569 {
2570         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2571
2572         kvm_arch_vcpu_load(vcpu, cpu);
2573 }
2574
2575 static void kvm_sched_out(struct preempt_notifier *pn,
2576                           struct task_struct *next)
2577 {
2578         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2579
2580         kvm_arch_vcpu_put(vcpu);
2581 }
2582
2583 int kvm_init(void *opaque, unsigned int vcpu_size,
2584                   struct module *module)
2585 {
2586         int r;
2587         int cpu;
2588
2589         kvm_init_debug();
2590
2591         r = kvm_arch_init(opaque);
2592         if (r)
2593                 goto out_fail;
2594
2595         bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2596
2597         if (bad_page == NULL) {
2598                 r = -ENOMEM;
2599                 goto out;
2600         }
2601
2602         bad_pfn = page_to_pfn(bad_page);
2603
2604         if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2605                 r = -ENOMEM;
2606                 goto out_free_0;
2607         }
2608         cpumask_clear(cpus_hardware_enabled);
2609
2610         r = kvm_arch_hardware_setup();
2611         if (r < 0)
2612                 goto out_free_0a;
2613
2614         for_each_online_cpu(cpu) {
2615                 smp_call_function_single(cpu,
2616                                 kvm_arch_check_processor_compat,
2617                                 &r, 1);
2618                 if (r < 0)
2619                         goto out_free_1;
2620         }
2621
2622         on_each_cpu(hardware_enable, NULL, 1);
2623         r = register_cpu_notifier(&kvm_cpu_notifier);
2624         if (r)
2625                 goto out_free_2;
2626         register_reboot_notifier(&kvm_reboot_notifier);
2627
2628         r = sysdev_class_register(&kvm_sysdev_class);
2629         if (r)
2630                 goto out_free_3;
2631
2632         r = sysdev_register(&kvm_sysdev);
2633         if (r)
2634                 goto out_free_4;
2635
2636         /* A kmem cache lets us meet the alignment requirements of fx_save. */
2637         kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2638                                            __alignof__(struct kvm_vcpu),
2639                                            0, NULL);
2640         if (!kvm_vcpu_cache) {
2641                 r = -ENOMEM;
2642                 goto out_free_5;
2643         }
2644
2645         kvm_chardev_ops.owner = module;
2646         kvm_vm_fops.owner = module;
2647         kvm_vcpu_fops.owner = module;
2648
2649         r = misc_register(&kvm_dev);
2650         if (r) {
2651                 printk(KERN_ERR "kvm: misc device register failed\n");
2652                 goto out_free;
2653         }
2654
2655         kvm_preempt_ops.sched_in = kvm_sched_in;
2656         kvm_preempt_ops.sched_out = kvm_sched_out;
2657
2658         return 0;
2659
2660 out_free:
2661         kmem_cache_destroy(kvm_vcpu_cache);
2662 out_free_5:
2663         sysdev_unregister(&kvm_sysdev);
2664 out_free_4:
2665         sysdev_class_unregister(&kvm_sysdev_class);
2666 out_free_3:
2667         unregister_reboot_notifier(&kvm_reboot_notifier);
2668         unregister_cpu_notifier(&kvm_cpu_notifier);
2669 out_free_2:
2670         on_each_cpu(hardware_disable, NULL, 1);
2671 out_free_1:
2672         kvm_arch_hardware_unsetup();
2673 out_free_0a:
2674         free_cpumask_var(cpus_hardware_enabled);
2675 out_free_0:
2676         __free_page(bad_page);
2677 out:
2678         kvm_arch_exit();
2679         kvm_exit_debug();
2680 out_fail:
2681         return r;
2682 }
2683 EXPORT_SYMBOL_GPL(kvm_init);
2684
2685 void kvm_exit(void)
2686 {
2687         kvm_trace_cleanup();
2688         misc_deregister(&kvm_dev);
2689         kmem_cache_destroy(kvm_vcpu_cache);
2690         sysdev_unregister(&kvm_sysdev);
2691         sysdev_class_unregister(&kvm_sysdev_class);
2692         unregister_reboot_notifier(&kvm_reboot_notifier);
2693         unregister_cpu_notifier(&kvm_cpu_notifier);
2694         on_each_cpu(hardware_disable, NULL, 1);
2695         kvm_arch_hardware_unsetup();
2696         kvm_arch_exit();
2697         kvm_exit_debug();
2698         free_cpumask_var(cpus_hardware_enabled);
2699         __free_page(bad_page);
2700 }
2701 EXPORT_SYMBOL_GPL(kvm_exit);