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