KVM: Move irq ack notifier list to arch independent code
[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->slots_lock --> 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         zalloc_cpumask_var(&cpus, GFP_ATOMIC);
742
743         spin_lock(&kvm->requests_lock);
744         me = smp_processor_id();
745         kvm_for_each_vcpu(i, vcpu, kvm) {
746                 if (test_and_set_bit(req, &vcpu->requests))
747                         continue;
748                 cpu = vcpu->cpu;
749                 if (cpus != NULL && cpu != -1 && cpu != me)
750                         cpumask_set_cpu(cpu, cpus);
751         }
752         if (unlikely(cpus == NULL))
753                 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
754         else if (!cpumask_empty(cpus))
755                 smp_call_function_many(cpus, ack_flush, NULL, 1);
756         else
757                 called = false;
758         spin_unlock(&kvm->requests_lock);
759         free_cpumask_var(cpus);
760         return called;
761 }
762
763 void kvm_flush_remote_tlbs(struct kvm *kvm)
764 {
765         if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
766                 ++kvm->stat.remote_tlb_flush;
767 }
768
769 void kvm_reload_remote_mmus(struct kvm *kvm)
770 {
771         make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
772 }
773
774 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
775 {
776         struct page *page;
777         int r;
778
779         mutex_init(&vcpu->mutex);
780         vcpu->cpu = -1;
781         vcpu->kvm = kvm;
782         vcpu->vcpu_id = id;
783         init_waitqueue_head(&vcpu->wq);
784
785         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
786         if (!page) {
787                 r = -ENOMEM;
788                 goto fail;
789         }
790         vcpu->run = page_address(page);
791
792         r = kvm_arch_vcpu_init(vcpu);
793         if (r < 0)
794                 goto fail_free_run;
795         return 0;
796
797 fail_free_run:
798         free_page((unsigned long)vcpu->run);
799 fail:
800         return r;
801 }
802 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
803
804 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
805 {
806         kvm_arch_vcpu_uninit(vcpu);
807         free_page((unsigned long)vcpu->run);
808 }
809 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
810
811 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
812 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
813 {
814         return container_of(mn, struct kvm, mmu_notifier);
815 }
816
817 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
818                                              struct mm_struct *mm,
819                                              unsigned long address)
820 {
821         struct kvm *kvm = mmu_notifier_to_kvm(mn);
822         int need_tlb_flush;
823
824         /*
825          * When ->invalidate_page runs, the linux pte has been zapped
826          * already but the page is still allocated until
827          * ->invalidate_page returns. So if we increase the sequence
828          * here the kvm page fault will notice if the spte can't be
829          * established because the page is going to be freed. If
830          * instead the kvm page fault establishes the spte before
831          * ->invalidate_page runs, kvm_unmap_hva will release it
832          * before returning.
833          *
834          * The sequence increase only need to be seen at spin_unlock
835          * time, and not at spin_lock time.
836          *
837          * Increasing the sequence after the spin_unlock would be
838          * unsafe because the kvm page fault could then establish the
839          * pte after kvm_unmap_hva returned, without noticing the page
840          * is going to be freed.
841          */
842         spin_lock(&kvm->mmu_lock);
843         kvm->mmu_notifier_seq++;
844         need_tlb_flush = kvm_unmap_hva(kvm, address);
845         spin_unlock(&kvm->mmu_lock);
846
847         /* we've to flush the tlb before the pages can be freed */
848         if (need_tlb_flush)
849                 kvm_flush_remote_tlbs(kvm);
850
851 }
852
853 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
854                                         struct mm_struct *mm,
855                                         unsigned long address,
856                                         pte_t pte)
857 {
858         struct kvm *kvm = mmu_notifier_to_kvm(mn);
859
860         spin_lock(&kvm->mmu_lock);
861         kvm->mmu_notifier_seq++;
862         kvm_set_spte_hva(kvm, address, pte);
863         spin_unlock(&kvm->mmu_lock);
864 }
865
866 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
867                                                     struct mm_struct *mm,
868                                                     unsigned long start,
869                                                     unsigned long end)
870 {
871         struct kvm *kvm = mmu_notifier_to_kvm(mn);
872         int need_tlb_flush = 0;
873
874         spin_lock(&kvm->mmu_lock);
875         /*
876          * The count increase must become visible at unlock time as no
877          * spte can be established without taking the mmu_lock and
878          * count is also read inside the mmu_lock critical section.
879          */
880         kvm->mmu_notifier_count++;
881         for (; start < end; start += PAGE_SIZE)
882                 need_tlb_flush |= kvm_unmap_hva(kvm, start);
883         spin_unlock(&kvm->mmu_lock);
884
885         /* we've to flush the tlb before the pages can be freed */
886         if (need_tlb_flush)
887                 kvm_flush_remote_tlbs(kvm);
888 }
889
890 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
891                                                   struct mm_struct *mm,
892                                                   unsigned long start,
893                                                   unsigned long end)
894 {
895         struct kvm *kvm = mmu_notifier_to_kvm(mn);
896
897         spin_lock(&kvm->mmu_lock);
898         /*
899          * This sequence increase will notify the kvm page fault that
900          * the page that is going to be mapped in the spte could have
901          * been freed.
902          */
903         kvm->mmu_notifier_seq++;
904         /*
905          * The above sequence increase must be visible before the
906          * below count decrease but both values are read by the kvm
907          * page fault under mmu_lock spinlock so we don't need to add
908          * a smb_wmb() here in between the two.
909          */
910         kvm->mmu_notifier_count--;
911         spin_unlock(&kvm->mmu_lock);
912
913         BUG_ON(kvm->mmu_notifier_count < 0);
914 }
915
916 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
917                                               struct mm_struct *mm,
918                                               unsigned long address)
919 {
920         struct kvm *kvm = mmu_notifier_to_kvm(mn);
921         int young;
922
923         spin_lock(&kvm->mmu_lock);
924         young = kvm_age_hva(kvm, address);
925         spin_unlock(&kvm->mmu_lock);
926
927         if (young)
928                 kvm_flush_remote_tlbs(kvm);
929
930         return young;
931 }
932
933 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
934                                      struct mm_struct *mm)
935 {
936         struct kvm *kvm = mmu_notifier_to_kvm(mn);
937         kvm_arch_flush_shadow(kvm);
938 }
939
940 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
941         .invalidate_page        = kvm_mmu_notifier_invalidate_page,
942         .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
943         .invalidate_range_end   = kvm_mmu_notifier_invalidate_range_end,
944         .clear_flush_young      = kvm_mmu_notifier_clear_flush_young,
945         .change_pte             = kvm_mmu_notifier_change_pte,
946         .release                = kvm_mmu_notifier_release,
947 };
948 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
949
950 static struct kvm *kvm_create_vm(void)
951 {
952         struct kvm *kvm = kvm_arch_create_vm();
953 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
954         struct page *page;
955 #endif
956
957         if (IS_ERR(kvm))
958                 goto out;
959 #ifdef CONFIG_HAVE_KVM_IRQCHIP
960         INIT_HLIST_HEAD(&kvm->mask_notifier_list);
961         INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
962 #endif
963
964 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
965         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
966         if (!page) {
967                 kfree(kvm);
968                 return ERR_PTR(-ENOMEM);
969         }
970         kvm->coalesced_mmio_ring =
971                         (struct kvm_coalesced_mmio_ring *)page_address(page);
972 #endif
973
974 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
975         {
976                 int err;
977                 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
978                 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
979                 if (err) {
980 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
981                         put_page(page);
982 #endif
983                         kfree(kvm);
984                         return ERR_PTR(err);
985                 }
986         }
987 #endif
988
989         kvm->mm = current->mm;
990         atomic_inc(&kvm->mm->mm_count);
991         spin_lock_init(&kvm->mmu_lock);
992         spin_lock_init(&kvm->requests_lock);
993         kvm_io_bus_init(&kvm->pio_bus);
994         kvm_eventfd_init(kvm);
995         mutex_init(&kvm->lock);
996         mutex_init(&kvm->irq_lock);
997         kvm_io_bus_init(&kvm->mmio_bus);
998         init_rwsem(&kvm->slots_lock);
999         atomic_set(&kvm->users_count, 1);
1000         spin_lock(&kvm_lock);
1001         list_add(&kvm->vm_list, &vm_list);
1002         spin_unlock(&kvm_lock);
1003 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1004         kvm_coalesced_mmio_init(kvm);
1005 #endif
1006 out:
1007         return kvm;
1008 }
1009
1010 /*
1011  * Free any memory in @free but not in @dont.
1012  */
1013 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1014                                   struct kvm_memory_slot *dont)
1015 {
1016         int i;
1017
1018         if (!dont || free->rmap != dont->rmap)
1019                 vfree(free->rmap);
1020
1021         if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1022                 vfree(free->dirty_bitmap);
1023
1024
1025         for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1026                 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
1027                         vfree(free->lpage_info[i]);
1028                         free->lpage_info[i] = NULL;
1029                 }
1030         }
1031
1032         free->npages = 0;
1033         free->dirty_bitmap = NULL;
1034         free->rmap = NULL;
1035 }
1036
1037 void kvm_free_physmem(struct kvm *kvm)
1038 {
1039         int i;
1040
1041         for (i = 0; i < kvm->nmemslots; ++i)
1042                 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
1043 }
1044
1045 static void kvm_destroy_vm(struct kvm *kvm)
1046 {
1047         struct mm_struct *mm = kvm->mm;
1048
1049         kvm_arch_sync_events(kvm);
1050         spin_lock(&kvm_lock);
1051         list_del(&kvm->vm_list);
1052         spin_unlock(&kvm_lock);
1053         kvm_free_irq_routing(kvm);
1054         kvm_io_bus_destroy(&kvm->pio_bus);
1055         kvm_io_bus_destroy(&kvm->mmio_bus);
1056 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1057         if (kvm->coalesced_mmio_ring != NULL)
1058                 free_page((unsigned long)kvm->coalesced_mmio_ring);
1059 #endif
1060 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1061         mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
1062 #else
1063         kvm_arch_flush_shadow(kvm);
1064 #endif
1065         kvm_arch_destroy_vm(kvm);
1066         mmdrop(mm);
1067 }
1068
1069 void kvm_get_kvm(struct kvm *kvm)
1070 {
1071         atomic_inc(&kvm->users_count);
1072 }
1073 EXPORT_SYMBOL_GPL(kvm_get_kvm);
1074
1075 void kvm_put_kvm(struct kvm *kvm)
1076 {
1077         if (atomic_dec_and_test(&kvm->users_count))
1078                 kvm_destroy_vm(kvm);
1079 }
1080 EXPORT_SYMBOL_GPL(kvm_put_kvm);
1081
1082
1083 static int kvm_vm_release(struct inode *inode, struct file *filp)
1084 {
1085         struct kvm *kvm = filp->private_data;
1086
1087         kvm_irqfd_release(kvm);
1088
1089         kvm_put_kvm(kvm);
1090         return 0;
1091 }
1092
1093 /*
1094  * Allocate some memory and give it an address in the guest physical address
1095  * space.
1096  *
1097  * Discontiguous memory is allowed, mostly for framebuffers.
1098  *
1099  * Must be called holding mmap_sem for write.
1100  */
1101 int __kvm_set_memory_region(struct kvm *kvm,
1102                             struct kvm_userspace_memory_region *mem,
1103                             int user_alloc)
1104 {
1105         int r;
1106         gfn_t base_gfn;
1107         unsigned long npages;
1108         unsigned long i;
1109         struct kvm_memory_slot *memslot;
1110         struct kvm_memory_slot old, new;
1111
1112         r = -EINVAL;
1113         /* General sanity checks */
1114         if (mem->memory_size & (PAGE_SIZE - 1))
1115                 goto out;
1116         if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1117                 goto out;
1118         if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
1119                 goto out;
1120         if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
1121                 goto out;
1122         if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1123                 goto out;
1124
1125         memslot = &kvm->memslots[mem->slot];
1126         base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1127         npages = mem->memory_size >> PAGE_SHIFT;
1128
1129         if (!npages)
1130                 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
1131
1132         new = old = *memslot;
1133
1134         new.base_gfn = base_gfn;
1135         new.npages = npages;
1136         new.flags = mem->flags;
1137
1138         /* Disallow changing a memory slot's size. */
1139         r = -EINVAL;
1140         if (npages && old.npages && npages != old.npages)
1141                 goto out_free;
1142
1143         /* Check for overlaps */
1144         r = -EEXIST;
1145         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1146                 struct kvm_memory_slot *s = &kvm->memslots[i];
1147
1148                 if (s == memslot || !s->npages)
1149                         continue;
1150                 if (!((base_gfn + npages <= s->base_gfn) ||
1151                       (base_gfn >= s->base_gfn + s->npages)))
1152                         goto out_free;
1153         }
1154
1155         /* Free page dirty bitmap if unneeded */
1156         if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
1157                 new.dirty_bitmap = NULL;
1158
1159         r = -ENOMEM;
1160
1161         /* Allocate if a slot is being created */
1162 #ifndef CONFIG_S390
1163         if (npages && !new.rmap) {
1164                 new.rmap = vmalloc(npages * sizeof(struct page *));
1165
1166                 if (!new.rmap)
1167                         goto out_free;
1168
1169                 memset(new.rmap, 0, npages * sizeof(*new.rmap));
1170
1171                 new.user_alloc = user_alloc;
1172                 /*
1173                  * hva_to_rmmap() serialzies with the mmu_lock and to be
1174                  * safe it has to ignore memslots with !user_alloc &&
1175                  * !userspace_addr.
1176                  */
1177                 if (user_alloc)
1178                         new.userspace_addr = mem->userspace_addr;
1179                 else
1180                         new.userspace_addr = 0;
1181         }
1182         if (!npages)
1183                 goto skip_lpage;
1184
1185         for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1186                 unsigned long ugfn;
1187                 unsigned long j;
1188                 int lpages;
1189                 int level = i + 2;
1190
1191                 /* Avoid unused variable warning if no large pages */
1192                 (void)level;
1193
1194                 if (new.lpage_info[i])
1195                         continue;
1196
1197                 lpages = 1 + (base_gfn + npages - 1) /
1198                              KVM_PAGES_PER_HPAGE(level);
1199                 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
1200
1201                 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
1202
1203                 if (!new.lpage_info[i])
1204                         goto out_free;
1205
1206                 memset(new.lpage_info[i], 0,
1207                        lpages * sizeof(*new.lpage_info[i]));
1208
1209                 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
1210                         new.lpage_info[i][0].write_count = 1;
1211                 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
1212                         new.lpage_info[i][lpages - 1].write_count = 1;
1213                 ugfn = new.userspace_addr >> PAGE_SHIFT;
1214                 /*
1215                  * If the gfn and userspace address are not aligned wrt each
1216                  * other, or if explicitly asked to, disable large page
1217                  * support for this slot
1218                  */
1219                 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
1220                     !largepages_enabled)
1221                         for (j = 0; j < lpages; ++j)
1222                                 new.lpage_info[i][j].write_count = 1;
1223         }
1224
1225 skip_lpage:
1226
1227         /* Allocate page dirty bitmap if needed */
1228         if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1229                 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1230
1231                 new.dirty_bitmap = vmalloc(dirty_bytes);
1232                 if (!new.dirty_bitmap)
1233                         goto out_free;
1234                 memset(new.dirty_bitmap, 0, dirty_bytes);
1235                 if (old.npages)
1236                         kvm_arch_flush_shadow(kvm);
1237         }
1238 #else  /* not defined CONFIG_S390 */
1239         new.user_alloc = user_alloc;
1240         if (user_alloc)
1241                 new.userspace_addr = mem->userspace_addr;
1242 #endif /* not defined CONFIG_S390 */
1243
1244         if (!npages)
1245                 kvm_arch_flush_shadow(kvm);
1246
1247         spin_lock(&kvm->mmu_lock);
1248         if (mem->slot >= kvm->nmemslots)
1249                 kvm->nmemslots = mem->slot + 1;
1250
1251         *memslot = new;
1252         spin_unlock(&kvm->mmu_lock);
1253
1254         r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1255         if (r) {
1256                 spin_lock(&kvm->mmu_lock);
1257                 *memslot = old;
1258                 spin_unlock(&kvm->mmu_lock);
1259                 goto out_free;
1260         }
1261
1262         kvm_free_physmem_slot(&old, npages ? &new : NULL);
1263         /* Slot deletion case: we have to update the current slot */
1264         spin_lock(&kvm->mmu_lock);
1265         if (!npages)
1266                 *memslot = old;
1267         spin_unlock(&kvm->mmu_lock);
1268 #ifdef CONFIG_DMAR
1269         /* map the pages in iommu page table */
1270         r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1271         if (r)
1272                 goto out;
1273 #endif
1274         return 0;
1275
1276 out_free:
1277         kvm_free_physmem_slot(&new, &old);
1278 out:
1279         return r;
1280
1281 }
1282 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1283
1284 int kvm_set_memory_region(struct kvm *kvm,
1285                           struct kvm_userspace_memory_region *mem,
1286                           int user_alloc)
1287 {
1288         int r;
1289
1290         down_write(&kvm->slots_lock);
1291         r = __kvm_set_memory_region(kvm, mem, user_alloc);
1292         up_write(&kvm->slots_lock);
1293         return r;
1294 }
1295 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1296
1297 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1298                                    struct
1299                                    kvm_userspace_memory_region *mem,
1300                                    int user_alloc)
1301 {
1302         if (mem->slot >= KVM_MEMORY_SLOTS)
1303                 return -EINVAL;
1304         return kvm_set_memory_region(kvm, mem, user_alloc);
1305 }
1306
1307 int kvm_get_dirty_log(struct kvm *kvm,
1308                         struct kvm_dirty_log *log, int *is_dirty)
1309 {
1310         struct kvm_memory_slot *memslot;
1311         int r, i;
1312         int n;
1313         unsigned long any = 0;
1314
1315         r = -EINVAL;
1316         if (log->slot >= KVM_MEMORY_SLOTS)
1317                 goto out;
1318
1319         memslot = &kvm->memslots[log->slot];
1320         r = -ENOENT;
1321         if (!memslot->dirty_bitmap)
1322                 goto out;
1323
1324         n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1325
1326         for (i = 0; !any && i < n/sizeof(long); ++i)
1327                 any = memslot->dirty_bitmap[i];
1328
1329         r = -EFAULT;
1330         if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1331                 goto out;
1332
1333         if (any)
1334                 *is_dirty = 1;
1335
1336         r = 0;
1337 out:
1338         return r;
1339 }
1340
1341 void kvm_disable_largepages(void)
1342 {
1343         largepages_enabled = false;
1344 }
1345 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
1346
1347 int is_error_page(struct page *page)
1348 {
1349         return page == bad_page;
1350 }
1351 EXPORT_SYMBOL_GPL(is_error_page);
1352
1353 int is_error_pfn(pfn_t pfn)
1354 {
1355         return pfn == bad_pfn;
1356 }
1357 EXPORT_SYMBOL_GPL(is_error_pfn);
1358
1359 static inline unsigned long bad_hva(void)
1360 {
1361         return PAGE_OFFSET;
1362 }
1363
1364 int kvm_is_error_hva(unsigned long addr)
1365 {
1366         return addr == bad_hva();
1367 }
1368 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1369
1370 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1371 {
1372         int i;
1373
1374         for (i = 0; i < kvm->nmemslots; ++i) {
1375                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1376
1377                 if (gfn >= memslot->base_gfn
1378                     && gfn < memslot->base_gfn + memslot->npages)
1379                         return memslot;
1380         }
1381         return NULL;
1382 }
1383 EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1384
1385 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1386 {
1387         gfn = unalias_gfn(kvm, gfn);
1388         return gfn_to_memslot_unaliased(kvm, gfn);
1389 }
1390
1391 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1392 {
1393         int i;
1394
1395         gfn = unalias_gfn(kvm, gfn);
1396         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1397                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1398
1399                 if (gfn >= memslot->base_gfn
1400                     && gfn < memslot->base_gfn + memslot->npages)
1401                         return 1;
1402         }
1403         return 0;
1404 }
1405 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1406
1407 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1408 {
1409         struct kvm_memory_slot *slot;
1410
1411         gfn = unalias_gfn(kvm, gfn);
1412         slot = gfn_to_memslot_unaliased(kvm, gfn);
1413         if (!slot)
1414                 return bad_hva();
1415         return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1416 }
1417 EXPORT_SYMBOL_GPL(gfn_to_hva);
1418
1419 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1420 {
1421         struct page *page[1];
1422         unsigned long addr;
1423         int npages;
1424         pfn_t pfn;
1425
1426         might_sleep();
1427
1428         addr = gfn_to_hva(kvm, gfn);
1429         if (kvm_is_error_hva(addr)) {
1430                 get_page(bad_page);
1431                 return page_to_pfn(bad_page);
1432         }
1433
1434         npages = get_user_pages_fast(addr, 1, 1, page);
1435
1436         if (unlikely(npages != 1)) {
1437                 struct vm_area_struct *vma;
1438
1439                 down_read(&current->mm->mmap_sem);
1440                 vma = find_vma(current->mm, addr);
1441
1442                 if (vma == NULL || addr < vma->vm_start ||
1443                     !(vma->vm_flags & VM_PFNMAP)) {
1444                         up_read(&current->mm->mmap_sem);
1445                         get_page(bad_page);
1446                         return page_to_pfn(bad_page);
1447                 }
1448
1449                 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1450                 up_read(&current->mm->mmap_sem);
1451                 BUG_ON(!kvm_is_mmio_pfn(pfn));
1452         } else
1453                 pfn = page_to_pfn(page[0]);
1454
1455         return pfn;
1456 }
1457
1458 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1459
1460 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1461 {
1462         pfn_t pfn;
1463
1464         pfn = gfn_to_pfn(kvm, gfn);
1465         if (!kvm_is_mmio_pfn(pfn))
1466                 return pfn_to_page(pfn);
1467
1468         WARN_ON(kvm_is_mmio_pfn(pfn));
1469
1470         get_page(bad_page);
1471         return bad_page;
1472 }
1473
1474 EXPORT_SYMBOL_GPL(gfn_to_page);
1475
1476 void kvm_release_page_clean(struct page *page)
1477 {
1478         kvm_release_pfn_clean(page_to_pfn(page));
1479 }
1480 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1481
1482 void kvm_release_pfn_clean(pfn_t pfn)
1483 {
1484         if (!kvm_is_mmio_pfn(pfn))
1485                 put_page(pfn_to_page(pfn));
1486 }
1487 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1488
1489 void kvm_release_page_dirty(struct page *page)
1490 {
1491         kvm_release_pfn_dirty(page_to_pfn(page));
1492 }
1493 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1494
1495 void kvm_release_pfn_dirty(pfn_t pfn)
1496 {
1497         kvm_set_pfn_dirty(pfn);
1498         kvm_release_pfn_clean(pfn);
1499 }
1500 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1501
1502 void kvm_set_page_dirty(struct page *page)
1503 {
1504         kvm_set_pfn_dirty(page_to_pfn(page));
1505 }
1506 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1507
1508 void kvm_set_pfn_dirty(pfn_t pfn)
1509 {
1510         if (!kvm_is_mmio_pfn(pfn)) {
1511                 struct page *page = pfn_to_page(pfn);
1512                 if (!PageReserved(page))
1513                         SetPageDirty(page);
1514         }
1515 }
1516 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1517
1518 void kvm_set_pfn_accessed(pfn_t pfn)
1519 {
1520         if (!kvm_is_mmio_pfn(pfn))
1521                 mark_page_accessed(pfn_to_page(pfn));
1522 }
1523 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1524
1525 void kvm_get_pfn(pfn_t pfn)
1526 {
1527         if (!kvm_is_mmio_pfn(pfn))
1528                 get_page(pfn_to_page(pfn));
1529 }
1530 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1531
1532 static int next_segment(unsigned long len, int offset)
1533 {
1534         if (len > PAGE_SIZE - offset)
1535                 return PAGE_SIZE - offset;
1536         else
1537                 return len;
1538 }
1539
1540 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1541                         int len)
1542 {
1543         int r;
1544         unsigned long addr;
1545
1546         addr = gfn_to_hva(kvm, gfn);
1547         if (kvm_is_error_hva(addr))
1548                 return -EFAULT;
1549         r = copy_from_user(data, (void __user *)addr + offset, len);
1550         if (r)
1551                 return -EFAULT;
1552         return 0;
1553 }
1554 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1555
1556 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1557 {
1558         gfn_t gfn = gpa >> PAGE_SHIFT;
1559         int seg;
1560         int offset = offset_in_page(gpa);
1561         int ret;
1562
1563         while ((seg = next_segment(len, offset)) != 0) {
1564                 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1565                 if (ret < 0)
1566                         return ret;
1567                 offset = 0;
1568                 len -= seg;
1569                 data += seg;
1570                 ++gfn;
1571         }
1572         return 0;
1573 }
1574 EXPORT_SYMBOL_GPL(kvm_read_guest);
1575
1576 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1577                           unsigned long len)
1578 {
1579         int r;
1580         unsigned long addr;
1581         gfn_t gfn = gpa >> PAGE_SHIFT;
1582         int offset = offset_in_page(gpa);
1583
1584         addr = gfn_to_hva(kvm, gfn);
1585         if (kvm_is_error_hva(addr))
1586                 return -EFAULT;
1587         pagefault_disable();
1588         r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1589         pagefault_enable();
1590         if (r)
1591                 return -EFAULT;
1592         return 0;
1593 }
1594 EXPORT_SYMBOL(kvm_read_guest_atomic);
1595
1596 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1597                          int offset, int len)
1598 {
1599         int r;
1600         unsigned long addr;
1601
1602         addr = gfn_to_hva(kvm, gfn);
1603         if (kvm_is_error_hva(addr))
1604                 return -EFAULT;
1605         r = copy_to_user((void __user *)addr + offset, data, len);
1606         if (r)
1607                 return -EFAULT;
1608         mark_page_dirty(kvm, gfn);
1609         return 0;
1610 }
1611 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1612
1613 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1614                     unsigned long len)
1615 {
1616         gfn_t gfn = gpa >> PAGE_SHIFT;
1617         int seg;
1618         int offset = offset_in_page(gpa);
1619         int ret;
1620
1621         while ((seg = next_segment(len, offset)) != 0) {
1622                 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1623                 if (ret < 0)
1624                         return ret;
1625                 offset = 0;
1626                 len -= seg;
1627                 data += seg;
1628                 ++gfn;
1629         }
1630         return 0;
1631 }
1632
1633 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1634 {
1635         return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1636 }
1637 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1638
1639 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1640 {
1641         gfn_t gfn = gpa >> PAGE_SHIFT;
1642         int seg;
1643         int offset = offset_in_page(gpa);
1644         int ret;
1645
1646         while ((seg = next_segment(len, offset)) != 0) {
1647                 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1648                 if (ret < 0)
1649                         return ret;
1650                 offset = 0;
1651                 len -= seg;
1652                 ++gfn;
1653         }
1654         return 0;
1655 }
1656 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1657
1658 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1659 {
1660         struct kvm_memory_slot *memslot;
1661
1662         gfn = unalias_gfn(kvm, gfn);
1663         memslot = gfn_to_memslot_unaliased(kvm, gfn);
1664         if (memslot && memslot->dirty_bitmap) {
1665                 unsigned long rel_gfn = gfn - memslot->base_gfn;
1666
1667                 /* avoid RMW */
1668                 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1669                         set_bit(rel_gfn, memslot->dirty_bitmap);
1670         }
1671 }
1672
1673 /*
1674  * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1675  */
1676 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1677 {
1678         DEFINE_WAIT(wait);
1679
1680         for (;;) {
1681                 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1682
1683                 if (kvm_arch_vcpu_runnable(vcpu)) {
1684                         set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1685                         break;
1686                 }
1687                 if (kvm_cpu_has_pending_timer(vcpu))
1688                         break;
1689                 if (signal_pending(current))
1690                         break;
1691
1692                 schedule();
1693         }
1694
1695         finish_wait(&vcpu->wq, &wait);
1696 }
1697
1698 void kvm_resched(struct kvm_vcpu *vcpu)
1699 {
1700         if (!need_resched())
1701                 return;
1702         cond_resched();
1703 }
1704 EXPORT_SYMBOL_GPL(kvm_resched);
1705
1706 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1707 {
1708         struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1709         struct page *page;
1710
1711         if (vmf->pgoff == 0)
1712                 page = virt_to_page(vcpu->run);
1713 #ifdef CONFIG_X86
1714         else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1715                 page = virt_to_page(vcpu->arch.pio_data);
1716 #endif
1717 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1718         else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1719                 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1720 #endif
1721         else
1722                 return VM_FAULT_SIGBUS;
1723         get_page(page);
1724         vmf->page = page;
1725         return 0;
1726 }
1727
1728 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1729         .fault = kvm_vcpu_fault,
1730 };
1731
1732 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1733 {
1734         vma->vm_ops = &kvm_vcpu_vm_ops;
1735         return 0;
1736 }
1737
1738 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1739 {
1740         struct kvm_vcpu *vcpu = filp->private_data;
1741
1742         kvm_put_kvm(vcpu->kvm);
1743         return 0;
1744 }
1745
1746 static struct file_operations kvm_vcpu_fops = {
1747         .release        = kvm_vcpu_release,
1748         .unlocked_ioctl = kvm_vcpu_ioctl,
1749         .compat_ioctl   = kvm_vcpu_ioctl,
1750         .mmap           = kvm_vcpu_mmap,
1751 };
1752
1753 /*
1754  * Allocates an inode for the vcpu.
1755  */
1756 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1757 {
1758         return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1759 }
1760
1761 /*
1762  * Creates some virtual cpus.  Good luck creating more than one.
1763  */
1764 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1765 {
1766         int r;
1767         struct kvm_vcpu *vcpu, *v;
1768
1769         vcpu = kvm_arch_vcpu_create(kvm, id);
1770         if (IS_ERR(vcpu))
1771                 return PTR_ERR(vcpu);
1772
1773         preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1774
1775         r = kvm_arch_vcpu_setup(vcpu);
1776         if (r)
1777                 return r;
1778
1779         mutex_lock(&kvm->lock);
1780         if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1781                 r = -EINVAL;
1782                 goto vcpu_destroy;
1783         }
1784
1785         kvm_for_each_vcpu(r, v, kvm)
1786                 if (v->vcpu_id == id) {
1787                         r = -EEXIST;
1788                         goto vcpu_destroy;
1789                 }
1790
1791         BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1792
1793         /* Now it's all set up, let userspace reach it */
1794         kvm_get_kvm(kvm);
1795         r = create_vcpu_fd(vcpu);
1796         if (r < 0) {
1797                 kvm_put_kvm(kvm);
1798                 goto vcpu_destroy;
1799         }
1800
1801         kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1802         smp_wmb();
1803         atomic_inc(&kvm->online_vcpus);
1804
1805 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1806         if (kvm->bsp_vcpu_id == id)
1807                 kvm->bsp_vcpu = vcpu;
1808 #endif
1809         mutex_unlock(&kvm->lock);
1810         return r;
1811
1812 vcpu_destroy:
1813         mutex_unlock(&kvm->lock);
1814         kvm_arch_vcpu_destroy(vcpu);
1815         return r;
1816 }
1817
1818 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1819 {
1820         if (sigset) {
1821                 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1822                 vcpu->sigset_active = 1;
1823                 vcpu->sigset = *sigset;
1824         } else
1825                 vcpu->sigset_active = 0;
1826         return 0;
1827 }
1828
1829 #ifdef __KVM_HAVE_MSIX
1830 static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1831                                     struct kvm_assigned_msix_nr *entry_nr)
1832 {
1833         int r = 0;
1834         struct kvm_assigned_dev_kernel *adev;
1835
1836         mutex_lock(&kvm->lock);
1837
1838         adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1839                                       entry_nr->assigned_dev_id);
1840         if (!adev) {
1841                 r = -EINVAL;
1842                 goto msix_nr_out;
1843         }
1844
1845         if (adev->entries_nr == 0) {
1846                 adev->entries_nr = entry_nr->entry_nr;
1847                 if (adev->entries_nr == 0 ||
1848                     adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1849                         r = -EINVAL;
1850                         goto msix_nr_out;
1851                 }
1852
1853                 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1854                                                 entry_nr->entry_nr,
1855                                                 GFP_KERNEL);
1856                 if (!adev->host_msix_entries) {
1857                         r = -ENOMEM;
1858                         goto msix_nr_out;
1859                 }
1860                 adev->guest_msix_entries = kzalloc(
1861                                 sizeof(struct kvm_guest_msix_entry) *
1862                                 entry_nr->entry_nr, GFP_KERNEL);
1863                 if (!adev->guest_msix_entries) {
1864                         kfree(adev->host_msix_entries);
1865                         r = -ENOMEM;
1866                         goto msix_nr_out;
1867                 }
1868         } else /* Not allowed set MSI-X number twice */
1869                 r = -EINVAL;
1870 msix_nr_out:
1871         mutex_unlock(&kvm->lock);
1872         return r;
1873 }
1874
1875 static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1876                                        struct kvm_assigned_msix_entry *entry)
1877 {
1878         int r = 0, i;
1879         struct kvm_assigned_dev_kernel *adev;
1880
1881         mutex_lock(&kvm->lock);
1882
1883         adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1884                                       entry->assigned_dev_id);
1885
1886         if (!adev) {
1887                 r = -EINVAL;
1888                 goto msix_entry_out;
1889         }
1890
1891         for (i = 0; i < adev->entries_nr; i++)
1892                 if (adev->guest_msix_entries[i].vector == 0 ||
1893                     adev->guest_msix_entries[i].entry == entry->entry) {
1894                         adev->guest_msix_entries[i].entry = entry->entry;
1895                         adev->guest_msix_entries[i].vector = entry->gsi;
1896                         adev->host_msix_entries[i].entry = entry->entry;
1897                         break;
1898                 }
1899         if (i == adev->entries_nr) {
1900                 r = -ENOSPC;
1901                 goto msix_entry_out;
1902         }
1903
1904 msix_entry_out:
1905         mutex_unlock(&kvm->lock);
1906
1907         return r;
1908 }
1909 #endif
1910
1911 static long kvm_vcpu_ioctl(struct file *filp,
1912                            unsigned int ioctl, unsigned long arg)
1913 {
1914         struct kvm_vcpu *vcpu = filp->private_data;
1915         void __user *argp = (void __user *)arg;
1916         int r;
1917         struct kvm_fpu *fpu = NULL;
1918         struct kvm_sregs *kvm_sregs = NULL;
1919
1920         if (vcpu->kvm->mm != current->mm)
1921                 return -EIO;
1922         switch (ioctl) {
1923         case KVM_RUN:
1924                 r = -EINVAL;
1925                 if (arg)
1926                         goto out;
1927                 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1928                 break;
1929         case KVM_GET_REGS: {
1930                 struct kvm_regs *kvm_regs;
1931
1932                 r = -ENOMEM;
1933                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1934                 if (!kvm_regs)
1935                         goto out;
1936                 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1937                 if (r)
1938                         goto out_free1;
1939                 r = -EFAULT;
1940                 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1941                         goto out_free1;
1942                 r = 0;
1943 out_free1:
1944                 kfree(kvm_regs);
1945                 break;
1946         }
1947         case KVM_SET_REGS: {
1948                 struct kvm_regs *kvm_regs;
1949
1950                 r = -ENOMEM;
1951                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1952                 if (!kvm_regs)
1953                         goto out;
1954                 r = -EFAULT;
1955                 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1956                         goto out_free2;
1957                 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1958                 if (r)
1959                         goto out_free2;
1960                 r = 0;
1961 out_free2:
1962                 kfree(kvm_regs);
1963                 break;
1964         }
1965         case KVM_GET_SREGS: {
1966                 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1967                 r = -ENOMEM;
1968                 if (!kvm_sregs)
1969                         goto out;
1970                 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1971                 if (r)
1972                         goto out;
1973                 r = -EFAULT;
1974                 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1975                         goto out;
1976                 r = 0;
1977                 break;
1978         }
1979         case KVM_SET_SREGS: {
1980                 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1981                 r = -ENOMEM;
1982                 if (!kvm_sregs)
1983                         goto out;
1984                 r = -EFAULT;
1985                 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1986                         goto out;
1987                 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1988                 if (r)
1989                         goto out;
1990                 r = 0;
1991                 break;
1992         }
1993         case KVM_GET_MP_STATE: {
1994                 struct kvm_mp_state mp_state;
1995
1996                 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1997                 if (r)
1998                         goto out;
1999                 r = -EFAULT;
2000                 if (copy_to_user(argp, &mp_state, sizeof mp_state))
2001                         goto out;
2002                 r = 0;
2003                 break;
2004         }
2005         case KVM_SET_MP_STATE: {
2006                 struct kvm_mp_state mp_state;
2007
2008                 r = -EFAULT;
2009                 if (copy_from_user(&mp_state, argp, sizeof mp_state))
2010                         goto out;
2011                 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
2012                 if (r)
2013                         goto out;
2014                 r = 0;
2015                 break;
2016         }
2017         case KVM_TRANSLATE: {
2018                 struct kvm_translation tr;
2019
2020                 r = -EFAULT;
2021                 if (copy_from_user(&tr, argp, sizeof tr))
2022                         goto out;
2023                 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
2024                 if (r)
2025                         goto out;
2026                 r = -EFAULT;
2027                 if (copy_to_user(argp, &tr, sizeof tr))
2028                         goto out;
2029                 r = 0;
2030                 break;
2031         }
2032         case KVM_SET_GUEST_DEBUG: {
2033                 struct kvm_guest_debug dbg;
2034
2035                 r = -EFAULT;
2036                 if (copy_from_user(&dbg, argp, sizeof dbg))
2037                         goto out;
2038                 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
2039                 if (r)
2040                         goto out;
2041                 r = 0;
2042                 break;
2043         }
2044         case KVM_SET_SIGNAL_MASK: {
2045                 struct kvm_signal_mask __user *sigmask_arg = argp;
2046                 struct kvm_signal_mask kvm_sigmask;
2047                 sigset_t sigset, *p;
2048
2049                 p = NULL;
2050                 if (argp) {
2051                         r = -EFAULT;
2052                         if (copy_from_user(&kvm_sigmask, argp,
2053                                            sizeof kvm_sigmask))
2054                                 goto out;
2055                         r = -EINVAL;
2056                         if (kvm_sigmask.len != sizeof sigset)
2057                                 goto out;
2058                         r = -EFAULT;
2059                         if (copy_from_user(&sigset, sigmask_arg->sigset,
2060                                            sizeof sigset))
2061                                 goto out;
2062                         p = &sigset;
2063                 }
2064                 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2065                 break;
2066         }
2067         case KVM_GET_FPU: {
2068                 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2069                 r = -ENOMEM;
2070                 if (!fpu)
2071                         goto out;
2072                 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
2073                 if (r)
2074                         goto out;
2075                 r = -EFAULT;
2076                 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
2077                         goto out;
2078                 r = 0;
2079                 break;
2080         }
2081         case KVM_SET_FPU: {
2082                 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2083                 r = -ENOMEM;
2084                 if (!fpu)
2085                         goto out;
2086                 r = -EFAULT;
2087                 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
2088                         goto out;
2089                 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
2090                 if (r)
2091                         goto out;
2092                 r = 0;
2093                 break;
2094         }
2095         default:
2096                 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
2097         }
2098 out:
2099         kfree(fpu);
2100         kfree(kvm_sregs);
2101         return r;
2102 }
2103
2104 static long kvm_vm_ioctl(struct file *filp,
2105                            unsigned int ioctl, unsigned long arg)
2106 {
2107         struct kvm *kvm = filp->private_data;
2108         void __user *argp = (void __user *)arg;
2109         int r;
2110
2111         if (kvm->mm != current->mm)
2112                 return -EIO;
2113         switch (ioctl) {
2114         case KVM_CREATE_VCPU:
2115                 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2116                 if (r < 0)
2117                         goto out;
2118                 break;
2119         case KVM_SET_USER_MEMORY_REGION: {
2120                 struct kvm_userspace_memory_region kvm_userspace_mem;
2121
2122                 r = -EFAULT;
2123                 if (copy_from_user(&kvm_userspace_mem, argp,
2124                                                 sizeof kvm_userspace_mem))
2125                         goto out;
2126
2127                 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
2128                 if (r)
2129                         goto out;
2130                 break;
2131         }
2132         case KVM_GET_DIRTY_LOG: {
2133                 struct kvm_dirty_log log;
2134
2135                 r = -EFAULT;
2136                 if (copy_from_user(&log, argp, sizeof log))
2137                         goto out;
2138                 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2139                 if (r)
2140                         goto out;
2141                 break;
2142         }
2143 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2144         case KVM_REGISTER_COALESCED_MMIO: {
2145                 struct kvm_coalesced_mmio_zone zone;
2146                 r = -EFAULT;
2147                 if (copy_from_user(&zone, argp, sizeof zone))
2148                         goto out;
2149                 r = -ENXIO;
2150                 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2151                 if (r)
2152                         goto out;
2153                 r = 0;
2154                 break;
2155         }
2156         case KVM_UNREGISTER_COALESCED_MMIO: {
2157                 struct kvm_coalesced_mmio_zone zone;
2158                 r = -EFAULT;
2159                 if (copy_from_user(&zone, argp, sizeof zone))
2160                         goto out;
2161                 r = -ENXIO;
2162                 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2163                 if (r)
2164                         goto out;
2165                 r = 0;
2166                 break;
2167         }
2168 #endif
2169 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
2170         case KVM_ASSIGN_PCI_DEVICE: {
2171                 struct kvm_assigned_pci_dev assigned_dev;
2172
2173                 r = -EFAULT;
2174                 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2175                         goto out;
2176                 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2177                 if (r)
2178                         goto out;
2179                 break;
2180         }
2181         case KVM_ASSIGN_IRQ: {
2182                 r = -EOPNOTSUPP;
2183                 break;
2184         }
2185 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
2186         case KVM_ASSIGN_DEV_IRQ: {
2187                 struct kvm_assigned_irq assigned_irq;
2188
2189                 r = -EFAULT;
2190                 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2191                         goto out;
2192                 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2193                 if (r)
2194                         goto out;
2195                 break;
2196         }
2197         case KVM_DEASSIGN_DEV_IRQ: {
2198                 struct kvm_assigned_irq assigned_irq;
2199
2200                 r = -EFAULT;
2201                 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2202                         goto out;
2203                 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2204                 if (r)
2205                         goto out;
2206                 break;
2207         }
2208 #endif
2209 #endif
2210 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2211         case KVM_DEASSIGN_PCI_DEVICE: {
2212                 struct kvm_assigned_pci_dev assigned_dev;
2213
2214                 r = -EFAULT;
2215                 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2216                         goto out;
2217                 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2218                 if (r)
2219                         goto out;
2220                 break;
2221         }
2222 #endif
2223 #ifdef KVM_CAP_IRQ_ROUTING
2224         case KVM_SET_GSI_ROUTING: {
2225                 struct kvm_irq_routing routing;
2226                 struct kvm_irq_routing __user *urouting;
2227                 struct kvm_irq_routing_entry *entries;
2228
2229                 r = -EFAULT;
2230                 if (copy_from_user(&routing, argp, sizeof(routing)))
2231                         goto out;
2232                 r = -EINVAL;
2233                 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2234                         goto out;
2235                 if (routing.flags)
2236                         goto out;
2237                 r = -ENOMEM;
2238                 entries = vmalloc(routing.nr * sizeof(*entries));
2239                 if (!entries)
2240                         goto out;
2241                 r = -EFAULT;
2242                 urouting = argp;
2243                 if (copy_from_user(entries, urouting->entries,
2244                                    routing.nr * sizeof(*entries)))
2245                         goto out_free_irq_routing;
2246                 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2247                                         routing.flags);
2248         out_free_irq_routing:
2249                 vfree(entries);
2250                 break;
2251         }
2252 #endif /* KVM_CAP_IRQ_ROUTING */
2253 #ifdef __KVM_HAVE_MSIX
2254         case KVM_ASSIGN_SET_MSIX_NR: {
2255                 struct kvm_assigned_msix_nr entry_nr;
2256                 r = -EFAULT;
2257                 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2258                         goto out;
2259                 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2260                 if (r)
2261                         goto out;
2262                 break;
2263         }
2264         case KVM_ASSIGN_SET_MSIX_ENTRY: {
2265                 struct kvm_assigned_msix_entry entry;
2266                 r = -EFAULT;
2267                 if (copy_from_user(&entry, argp, sizeof entry))
2268                         goto out;
2269                 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2270                 if (r)
2271                         goto out;
2272                 break;
2273         }
2274 #endif
2275         case KVM_IRQFD: {
2276                 struct kvm_irqfd data;
2277
2278                 r = -EFAULT;
2279                 if (copy_from_user(&data, argp, sizeof data))
2280                         goto out;
2281                 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2282                 break;
2283         }
2284         case KVM_IOEVENTFD: {
2285                 struct kvm_ioeventfd data;
2286
2287                 r = -EFAULT;
2288                 if (copy_from_user(&data, argp, sizeof data))
2289                         goto out;
2290                 r = kvm_ioeventfd(kvm, &data);
2291                 break;
2292         }
2293 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2294         case KVM_SET_BOOT_CPU_ID:
2295                 r = 0;
2296                 mutex_lock(&kvm->lock);
2297                 if (atomic_read(&kvm->online_vcpus) != 0)
2298                         r = -EBUSY;
2299                 else
2300                         kvm->bsp_vcpu_id = arg;
2301                 mutex_unlock(&kvm->lock);
2302                 break;
2303 #endif
2304         default:
2305                 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2306         }
2307 out:
2308         return r;
2309 }
2310
2311 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2312 {
2313         struct page *page[1];
2314         unsigned long addr;
2315         int npages;
2316         gfn_t gfn = vmf->pgoff;
2317         struct kvm *kvm = vma->vm_file->private_data;
2318
2319         addr = gfn_to_hva(kvm, gfn);
2320         if (kvm_is_error_hva(addr))
2321                 return VM_FAULT_SIGBUS;
2322
2323         npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2324                                 NULL);
2325         if (unlikely(npages != 1))
2326                 return VM_FAULT_SIGBUS;
2327
2328         vmf->page = page[0];
2329         return 0;
2330 }
2331
2332 static const struct vm_operations_struct kvm_vm_vm_ops = {
2333         .fault = kvm_vm_fault,
2334 };
2335
2336 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2337 {
2338         vma->vm_ops = &kvm_vm_vm_ops;
2339         return 0;
2340 }
2341
2342 static struct file_operations kvm_vm_fops = {
2343         .release        = kvm_vm_release,
2344         .unlocked_ioctl = kvm_vm_ioctl,
2345         .compat_ioctl   = kvm_vm_ioctl,
2346         .mmap           = kvm_vm_mmap,
2347 };
2348
2349 static int kvm_dev_ioctl_create_vm(void)
2350 {
2351         int fd;
2352         struct kvm *kvm;
2353
2354         kvm = kvm_create_vm();
2355         if (IS_ERR(kvm))
2356                 return PTR_ERR(kvm);
2357         fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
2358         if (fd < 0)
2359                 kvm_put_kvm(kvm);
2360
2361         return fd;
2362 }
2363
2364 static long kvm_dev_ioctl_check_extension_generic(long arg)
2365 {
2366         switch (arg) {
2367         case KVM_CAP_USER_MEMORY:
2368         case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2369         case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2370 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2371         case KVM_CAP_SET_BOOT_CPU_ID:
2372 #endif
2373                 return 1;
2374 #ifdef CONFIG_HAVE_KVM_IRQCHIP
2375         case KVM_CAP_IRQ_ROUTING:
2376                 return KVM_MAX_IRQ_ROUTES;
2377 #endif
2378         default:
2379                 break;
2380         }
2381         return kvm_dev_ioctl_check_extension(arg);
2382 }
2383
2384 static long kvm_dev_ioctl(struct file *filp,
2385                           unsigned int ioctl, unsigned long arg)
2386 {
2387         long r = -EINVAL;
2388
2389         switch (ioctl) {
2390         case KVM_GET_API_VERSION:
2391                 r = -EINVAL;
2392                 if (arg)
2393                         goto out;
2394                 r = KVM_API_VERSION;
2395                 break;
2396         case KVM_CREATE_VM:
2397                 r = -EINVAL;
2398                 if (arg)
2399                         goto out;
2400                 r = kvm_dev_ioctl_create_vm();
2401                 break;
2402         case KVM_CHECK_EXTENSION:
2403                 r = kvm_dev_ioctl_check_extension_generic(arg);
2404                 break;
2405         case KVM_GET_VCPU_MMAP_SIZE:
2406                 r = -EINVAL;
2407                 if (arg)
2408                         goto out;
2409                 r = PAGE_SIZE;     /* struct kvm_run */
2410 #ifdef CONFIG_X86
2411                 r += PAGE_SIZE;    /* pio data page */
2412 #endif
2413 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2414                 r += PAGE_SIZE;    /* coalesced mmio ring page */
2415 #endif
2416                 break;
2417         case KVM_TRACE_ENABLE:
2418         case KVM_TRACE_PAUSE:
2419         case KVM_TRACE_DISABLE:
2420                 r = -EOPNOTSUPP;
2421                 break;
2422         default:
2423                 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2424         }
2425 out:
2426         return r;
2427 }
2428
2429 static struct file_operations kvm_chardev_ops = {
2430         .unlocked_ioctl = kvm_dev_ioctl,
2431         .compat_ioctl   = kvm_dev_ioctl,
2432 };
2433
2434 static struct miscdevice kvm_dev = {
2435         KVM_MINOR,
2436         "kvm",
2437         &kvm_chardev_ops,
2438 };
2439
2440 static void hardware_enable(void *junk)
2441 {
2442         int cpu = raw_smp_processor_id();
2443
2444         if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2445                 return;
2446         cpumask_set_cpu(cpu, cpus_hardware_enabled);
2447         kvm_arch_hardware_enable(NULL);
2448 }
2449
2450 static void hardware_disable(void *junk)
2451 {
2452         int cpu = raw_smp_processor_id();
2453
2454         if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2455                 return;
2456         cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2457         kvm_arch_hardware_disable(NULL);
2458 }
2459
2460 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2461                            void *v)
2462 {
2463         int cpu = (long)v;
2464
2465         val &= ~CPU_TASKS_FROZEN;
2466         switch (val) {
2467         case CPU_DYING:
2468                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2469                        cpu);
2470                 hardware_disable(NULL);
2471                 break;
2472         case CPU_UP_CANCELED:
2473                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2474                        cpu);
2475                 smp_call_function_single(cpu, hardware_disable, NULL, 1);
2476                 break;
2477         case CPU_ONLINE:
2478                 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2479                        cpu);
2480                 smp_call_function_single(cpu, hardware_enable, NULL, 1);
2481                 break;
2482         }
2483         return NOTIFY_OK;
2484 }
2485
2486
2487 asmlinkage void kvm_handle_fault_on_reboot(void)
2488 {
2489         if (kvm_rebooting)
2490                 /* spin while reset goes on */
2491                 while (true)
2492                         ;
2493         /* Fault while not rebooting.  We want the trace. */
2494         BUG();
2495 }
2496 EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2497
2498 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2499                       void *v)
2500 {
2501         /*
2502          * Some (well, at least mine) BIOSes hang on reboot if
2503          * in vmx root mode.
2504          *
2505          * And Intel TXT required VMX off for all cpu when system shutdown.
2506          */
2507         printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2508         kvm_rebooting = true;
2509         on_each_cpu(hardware_disable, NULL, 1);
2510         return NOTIFY_OK;
2511 }
2512
2513 static struct notifier_block kvm_reboot_notifier = {
2514         .notifier_call = kvm_reboot,
2515         .priority = 0,
2516 };
2517
2518 void kvm_io_bus_init(struct kvm_io_bus *bus)
2519 {
2520         memset(bus, 0, sizeof(*bus));
2521 }
2522
2523 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2524 {
2525         int i;
2526
2527         for (i = 0; i < bus->dev_count; i++) {
2528                 struct kvm_io_device *pos = bus->devs[i];
2529
2530                 kvm_iodevice_destructor(pos);
2531         }
2532 }
2533
2534 /* kvm_io_bus_write - called under kvm->slots_lock */
2535 int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
2536                      int len, const void *val)
2537 {
2538         int i;
2539         for (i = 0; i < bus->dev_count; i++)
2540                 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2541                         return 0;
2542         return -EOPNOTSUPP;
2543 }
2544
2545 /* kvm_io_bus_read - called under kvm->slots_lock */
2546 int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
2547 {
2548         int i;
2549         for (i = 0; i < bus->dev_count; i++)
2550                 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2551                         return 0;
2552         return -EOPNOTSUPP;
2553 }
2554
2555 int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
2556                              struct kvm_io_device *dev)
2557 {
2558         int ret;
2559
2560         down_write(&kvm->slots_lock);
2561         ret = __kvm_io_bus_register_dev(bus, dev);
2562         up_write(&kvm->slots_lock);
2563
2564         return ret;
2565 }
2566
2567 /* An unlocked version. Caller must have write lock on slots_lock. */
2568 int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
2569                               struct kvm_io_device *dev)
2570 {
2571         if (bus->dev_count > NR_IOBUS_DEVS-1)
2572                 return -ENOSPC;
2573
2574         bus->devs[bus->dev_count++] = dev;
2575
2576         return 0;
2577 }
2578
2579 void kvm_io_bus_unregister_dev(struct kvm *kvm,
2580                                struct kvm_io_bus *bus,
2581                                struct kvm_io_device *dev)
2582 {
2583         down_write(&kvm->slots_lock);
2584         __kvm_io_bus_unregister_dev(bus, dev);
2585         up_write(&kvm->slots_lock);
2586 }
2587
2588 /* An unlocked version. Caller must have write lock on slots_lock. */
2589 void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
2590                                  struct kvm_io_device *dev)
2591 {
2592         int i;
2593
2594         for (i = 0; i < bus->dev_count; i++)
2595                 if (bus->devs[i] == dev) {
2596                         bus->devs[i] = bus->devs[--bus->dev_count];
2597                         break;
2598                 }
2599 }
2600
2601 static struct notifier_block kvm_cpu_notifier = {
2602         .notifier_call = kvm_cpu_hotplug,
2603         .priority = 20, /* must be > scheduler priority */
2604 };
2605
2606 static int vm_stat_get(void *_offset, u64 *val)
2607 {
2608         unsigned offset = (long)_offset;
2609         struct kvm *kvm;
2610
2611         *val = 0;
2612         spin_lock(&kvm_lock);
2613         list_for_each_entry(kvm, &vm_list, vm_list)
2614                 *val += *(u32 *)((void *)kvm + offset);
2615         spin_unlock(&kvm_lock);
2616         return 0;
2617 }
2618
2619 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2620
2621 static int vcpu_stat_get(void *_offset, u64 *val)
2622 {
2623         unsigned offset = (long)_offset;
2624         struct kvm *kvm;
2625         struct kvm_vcpu *vcpu;
2626         int i;
2627
2628         *val = 0;
2629         spin_lock(&kvm_lock);
2630         list_for_each_entry(kvm, &vm_list, vm_list)
2631                 kvm_for_each_vcpu(i, vcpu, kvm)
2632                         *val += *(u32 *)((void *)vcpu + offset);
2633
2634         spin_unlock(&kvm_lock);
2635         return 0;
2636 }
2637
2638 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2639
2640 static const struct file_operations *stat_fops[] = {
2641         [KVM_STAT_VCPU] = &vcpu_stat_fops,
2642         [KVM_STAT_VM]   = &vm_stat_fops,
2643 };
2644
2645 static void kvm_init_debug(void)
2646 {
2647         struct kvm_stats_debugfs_item *p;
2648
2649         kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2650         for (p = debugfs_entries; p->name; ++p)
2651                 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2652                                                 (void *)(long)p->offset,
2653                                                 stat_fops[p->kind]);
2654 }
2655
2656 static void kvm_exit_debug(void)
2657 {
2658         struct kvm_stats_debugfs_item *p;
2659
2660         for (p = debugfs_entries; p->name; ++p)
2661                 debugfs_remove(p->dentry);
2662         debugfs_remove(kvm_debugfs_dir);
2663 }
2664
2665 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2666 {
2667         hardware_disable(NULL);
2668         return 0;
2669 }
2670
2671 static int kvm_resume(struct sys_device *dev)
2672 {
2673         hardware_enable(NULL);
2674         return 0;
2675 }
2676
2677 static struct sysdev_class kvm_sysdev_class = {
2678         .name = "kvm",
2679         .suspend = kvm_suspend,
2680         .resume = kvm_resume,
2681 };
2682
2683 static struct sys_device kvm_sysdev = {
2684         .id = 0,
2685         .cls = &kvm_sysdev_class,
2686 };
2687
2688 struct page *bad_page;
2689 pfn_t bad_pfn;
2690
2691 static inline
2692 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2693 {
2694         return container_of(pn, struct kvm_vcpu, preempt_notifier);
2695 }
2696
2697 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2698 {
2699         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2700
2701         kvm_arch_vcpu_load(vcpu, cpu);
2702 }
2703
2704 static void kvm_sched_out(struct preempt_notifier *pn,
2705                           struct task_struct *next)
2706 {
2707         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2708
2709         kvm_arch_vcpu_put(vcpu);
2710 }
2711
2712 int kvm_init(void *opaque, unsigned int vcpu_size,
2713                   struct module *module)
2714 {
2715         int r;
2716         int cpu;
2717
2718         r = kvm_arch_init(opaque);
2719         if (r)
2720                 goto out_fail;
2721
2722         bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2723
2724         if (bad_page == NULL) {
2725                 r = -ENOMEM;
2726                 goto out;
2727         }
2728
2729         bad_pfn = page_to_pfn(bad_page);
2730
2731         if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2732                 r = -ENOMEM;
2733                 goto out_free_0;
2734         }
2735
2736         r = kvm_arch_hardware_setup();
2737         if (r < 0)
2738                 goto out_free_0a;
2739
2740         for_each_online_cpu(cpu) {
2741                 smp_call_function_single(cpu,
2742                                 kvm_arch_check_processor_compat,
2743                                 &r, 1);
2744                 if (r < 0)
2745                         goto out_free_1;
2746         }
2747
2748         on_each_cpu(hardware_enable, NULL, 1);
2749         r = register_cpu_notifier(&kvm_cpu_notifier);
2750         if (r)
2751                 goto out_free_2;
2752         register_reboot_notifier(&kvm_reboot_notifier);
2753
2754         r = sysdev_class_register(&kvm_sysdev_class);
2755         if (r)
2756                 goto out_free_3;
2757
2758         r = sysdev_register(&kvm_sysdev);
2759         if (r)
2760                 goto out_free_4;
2761
2762         /* A kmem cache lets us meet the alignment requirements of fx_save. */
2763         kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2764                                            __alignof__(struct kvm_vcpu),
2765                                            0, NULL);
2766         if (!kvm_vcpu_cache) {
2767                 r = -ENOMEM;
2768                 goto out_free_5;
2769         }
2770
2771         kvm_chardev_ops.owner = module;
2772         kvm_vm_fops.owner = module;
2773         kvm_vcpu_fops.owner = module;
2774
2775         r = misc_register(&kvm_dev);
2776         if (r) {
2777                 printk(KERN_ERR "kvm: misc device register failed\n");
2778                 goto out_free;
2779         }
2780
2781         kvm_preempt_ops.sched_in = kvm_sched_in;
2782         kvm_preempt_ops.sched_out = kvm_sched_out;
2783
2784         kvm_init_debug();
2785
2786         return 0;
2787
2788 out_free:
2789         kmem_cache_destroy(kvm_vcpu_cache);
2790 out_free_5:
2791         sysdev_unregister(&kvm_sysdev);
2792 out_free_4:
2793         sysdev_class_unregister(&kvm_sysdev_class);
2794 out_free_3:
2795         unregister_reboot_notifier(&kvm_reboot_notifier);
2796         unregister_cpu_notifier(&kvm_cpu_notifier);
2797 out_free_2:
2798         on_each_cpu(hardware_disable, NULL, 1);
2799 out_free_1:
2800         kvm_arch_hardware_unsetup();
2801 out_free_0a:
2802         free_cpumask_var(cpus_hardware_enabled);
2803 out_free_0:
2804         __free_page(bad_page);
2805 out:
2806         kvm_arch_exit();
2807 out_fail:
2808         return r;
2809 }
2810 EXPORT_SYMBOL_GPL(kvm_init);
2811
2812 void kvm_exit(void)
2813 {
2814         tracepoint_synchronize_unregister();
2815         kvm_exit_debug();
2816         misc_deregister(&kvm_dev);
2817         kmem_cache_destroy(kvm_vcpu_cache);
2818         sysdev_unregister(&kvm_sysdev);
2819         sysdev_class_unregister(&kvm_sysdev_class);
2820         unregister_reboot_notifier(&kvm_reboot_notifier);
2821         unregister_cpu_notifier(&kvm_cpu_notifier);
2822         on_each_cpu(hardware_disable, NULL, 1);
2823         kvm_arch_hardware_unsetup();
2824         kvm_arch_exit();
2825         free_cpumask_var(cpus_hardware_enabled);
2826         __free_page(bad_page);
2827 }
2828 EXPORT_SYMBOL_GPL(kvm_exit);