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