KVM: MMU: Concurrent guest walkers
[linux-3.10.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
44 #include <asm/processor.h>
45 #include <asm/io.h>
46 #include <asm/uaccess.h>
47 #include <asm/pgtable.h>
48
49 MODULE_AUTHOR("Qumranet");
50 MODULE_LICENSE("GPL");
51
52 DEFINE_SPINLOCK(kvm_lock);
53 LIST_HEAD(vm_list);
54
55 static cpumask_t cpus_hardware_enabled;
56
57 struct kmem_cache *kvm_vcpu_cache;
58 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
59
60 static __read_mostly struct preempt_ops kvm_preempt_ops;
61
62 static struct dentry *debugfs_dir;
63
64 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
65                            unsigned long arg);
66
67 static inline int valid_vcpu(int n)
68 {
69         return likely(n >= 0 && n < KVM_MAX_VCPUS);
70 }
71
72 /*
73  * Switches to specified vcpu, until a matching vcpu_put()
74  */
75 void vcpu_load(struct kvm_vcpu *vcpu)
76 {
77         int cpu;
78
79         mutex_lock(&vcpu->mutex);
80         cpu = get_cpu();
81         preempt_notifier_register(&vcpu->preempt_notifier);
82         kvm_arch_vcpu_load(vcpu, cpu);
83         put_cpu();
84 }
85
86 void vcpu_put(struct kvm_vcpu *vcpu)
87 {
88         preempt_disable();
89         kvm_arch_vcpu_put(vcpu);
90         preempt_notifier_unregister(&vcpu->preempt_notifier);
91         preempt_enable();
92         mutex_unlock(&vcpu->mutex);
93 }
94
95 static void ack_flush(void *_completed)
96 {
97 }
98
99 void kvm_flush_remote_tlbs(struct kvm *kvm)
100 {
101         int i, cpu;
102         cpumask_t cpus;
103         struct kvm_vcpu *vcpu;
104
105         cpus_clear(cpus);
106         for (i = 0; i < KVM_MAX_VCPUS; ++i) {
107                 vcpu = kvm->vcpus[i];
108                 if (!vcpu)
109                         continue;
110                 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
111                         continue;
112                 cpu = vcpu->cpu;
113                 if (cpu != -1 && cpu != raw_smp_processor_id())
114                         cpu_set(cpu, cpus);
115         }
116         if (cpus_empty(cpus))
117                 return;
118         ++kvm->stat.remote_tlb_flush;
119         smp_call_function_mask(cpus, ack_flush, NULL, 1);
120 }
121
122 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
123 {
124         struct page *page;
125         int r;
126
127         mutex_init(&vcpu->mutex);
128         vcpu->cpu = -1;
129         vcpu->kvm = kvm;
130         vcpu->vcpu_id = id;
131         init_waitqueue_head(&vcpu->wq);
132
133         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
134         if (!page) {
135                 r = -ENOMEM;
136                 goto fail;
137         }
138         vcpu->run = page_address(page);
139
140         r = kvm_arch_vcpu_init(vcpu);
141         if (r < 0)
142                 goto fail_free_run;
143         return 0;
144
145 fail_free_run:
146         free_page((unsigned long)vcpu->run);
147 fail:
148         return r;
149 }
150 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
151
152 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
153 {
154         kvm_arch_vcpu_uninit(vcpu);
155         free_page((unsigned long)vcpu->run);
156 }
157 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
158
159 static struct kvm *kvm_create_vm(void)
160 {
161         struct kvm *kvm = kvm_arch_create_vm();
162
163         if (IS_ERR(kvm))
164                 goto out;
165
166         kvm->mm = current->mm;
167         atomic_inc(&kvm->mm->mm_count);
168         kvm_io_bus_init(&kvm->pio_bus);
169         mutex_init(&kvm->lock);
170         kvm_io_bus_init(&kvm->mmio_bus);
171         spin_lock(&kvm_lock);
172         list_add(&kvm->vm_list, &vm_list);
173         spin_unlock(&kvm_lock);
174 out:
175         return kvm;
176 }
177
178 /*
179  * Free any memory in @free but not in @dont.
180  */
181 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
182                                   struct kvm_memory_slot *dont)
183 {
184         if (!dont || free->rmap != dont->rmap)
185                 vfree(free->rmap);
186
187         if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
188                 vfree(free->dirty_bitmap);
189
190         free->npages = 0;
191         free->dirty_bitmap = NULL;
192         free->rmap = NULL;
193 }
194
195 void kvm_free_physmem(struct kvm *kvm)
196 {
197         int i;
198
199         for (i = 0; i < kvm->nmemslots; ++i)
200                 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
201 }
202
203 static void kvm_destroy_vm(struct kvm *kvm)
204 {
205         struct mm_struct *mm = kvm->mm;
206
207         spin_lock(&kvm_lock);
208         list_del(&kvm->vm_list);
209         spin_unlock(&kvm_lock);
210         kvm_io_bus_destroy(&kvm->pio_bus);
211         kvm_io_bus_destroy(&kvm->mmio_bus);
212         kvm_arch_destroy_vm(kvm);
213         mmdrop(mm);
214 }
215
216 static int kvm_vm_release(struct inode *inode, struct file *filp)
217 {
218         struct kvm *kvm = filp->private_data;
219
220         kvm_destroy_vm(kvm);
221         return 0;
222 }
223
224 /*
225  * Allocate some memory and give it an address in the guest physical address
226  * space.
227  *
228  * Discontiguous memory is allowed, mostly for framebuffers.
229  *
230  * Must be called holding mmap_sem for write.
231  */
232 int __kvm_set_memory_region(struct kvm *kvm,
233                             struct kvm_userspace_memory_region *mem,
234                             int user_alloc)
235 {
236         int r;
237         gfn_t base_gfn;
238         unsigned long npages;
239         unsigned long i;
240         struct kvm_memory_slot *memslot;
241         struct kvm_memory_slot old, new;
242
243         r = -EINVAL;
244         /* General sanity checks */
245         if (mem->memory_size & (PAGE_SIZE - 1))
246                 goto out;
247         if (mem->guest_phys_addr & (PAGE_SIZE - 1))
248                 goto out;
249         if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
250                 goto out;
251         if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
252                 goto out;
253
254         memslot = &kvm->memslots[mem->slot];
255         base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
256         npages = mem->memory_size >> PAGE_SHIFT;
257
258         if (!npages)
259                 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
260
261         new = old = *memslot;
262
263         new.base_gfn = base_gfn;
264         new.npages = npages;
265         new.flags = mem->flags;
266
267         /* Disallow changing a memory slot's size. */
268         r = -EINVAL;
269         if (npages && old.npages && npages != old.npages)
270                 goto out_free;
271
272         /* Check for overlaps */
273         r = -EEXIST;
274         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
275                 struct kvm_memory_slot *s = &kvm->memslots[i];
276
277                 if (s == memslot)
278                         continue;
279                 if (!((base_gfn + npages <= s->base_gfn) ||
280                       (base_gfn >= s->base_gfn + s->npages)))
281                         goto out_free;
282         }
283
284         /* Free page dirty bitmap if unneeded */
285         if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
286                 new.dirty_bitmap = NULL;
287
288         r = -ENOMEM;
289
290         /* Allocate if a slot is being created */
291         if (npages && !new.rmap) {
292                 new.rmap = vmalloc(npages * sizeof(struct page *));
293
294                 if (!new.rmap)
295                         goto out_free;
296
297                 memset(new.rmap, 0, npages * sizeof(*new.rmap));
298
299                 new.user_alloc = user_alloc;
300                 new.userspace_addr = mem->userspace_addr;
301         }
302
303         /* Allocate page dirty bitmap if needed */
304         if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
305                 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
306
307                 new.dirty_bitmap = vmalloc(dirty_bytes);
308                 if (!new.dirty_bitmap)
309                         goto out_free;
310                 memset(new.dirty_bitmap, 0, dirty_bytes);
311         }
312
313         if (mem->slot >= kvm->nmemslots)
314                 kvm->nmemslots = mem->slot + 1;
315
316         *memslot = new;
317
318         r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
319         if (r) {
320                 *memslot = old;
321                 goto out_free;
322         }
323
324         kvm_free_physmem_slot(&old, &new);
325         return 0;
326
327 out_free:
328         kvm_free_physmem_slot(&new, &old);
329 out:
330         return r;
331
332 }
333 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
334
335 int kvm_set_memory_region(struct kvm *kvm,
336                           struct kvm_userspace_memory_region *mem,
337                           int user_alloc)
338 {
339         int r;
340
341         down_write(&current->mm->mmap_sem);
342         r = __kvm_set_memory_region(kvm, mem, user_alloc);
343         up_write(&current->mm->mmap_sem);
344         return r;
345 }
346 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
347
348 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
349                                    struct
350                                    kvm_userspace_memory_region *mem,
351                                    int user_alloc)
352 {
353         if (mem->slot >= KVM_MEMORY_SLOTS)
354                 return -EINVAL;
355         return kvm_set_memory_region(kvm, mem, user_alloc);
356 }
357
358 int kvm_get_dirty_log(struct kvm *kvm,
359                         struct kvm_dirty_log *log, int *is_dirty)
360 {
361         struct kvm_memory_slot *memslot;
362         int r, i;
363         int n;
364         unsigned long any = 0;
365
366         r = -EINVAL;
367         if (log->slot >= KVM_MEMORY_SLOTS)
368                 goto out;
369
370         memslot = &kvm->memslots[log->slot];
371         r = -ENOENT;
372         if (!memslot->dirty_bitmap)
373                 goto out;
374
375         n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
376
377         for (i = 0; !any && i < n/sizeof(long); ++i)
378                 any = memslot->dirty_bitmap[i];
379
380         r = -EFAULT;
381         if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
382                 goto out;
383
384         if (any)
385                 *is_dirty = 1;
386
387         r = 0;
388 out:
389         return r;
390 }
391
392 int is_error_page(struct page *page)
393 {
394         return page == bad_page;
395 }
396 EXPORT_SYMBOL_GPL(is_error_page);
397
398 static inline unsigned long bad_hva(void)
399 {
400         return PAGE_OFFSET;
401 }
402
403 int kvm_is_error_hva(unsigned long addr)
404 {
405         return addr == bad_hva();
406 }
407 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
408
409 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
410 {
411         int i;
412
413         for (i = 0; i < kvm->nmemslots; ++i) {
414                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
415
416                 if (gfn >= memslot->base_gfn
417                     && gfn < memslot->base_gfn + memslot->npages)
418                         return memslot;
419         }
420         return NULL;
421 }
422
423 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
424 {
425         gfn = unalias_gfn(kvm, gfn);
426         return __gfn_to_memslot(kvm, gfn);
427 }
428
429 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
430 {
431         int i;
432
433         gfn = unalias_gfn(kvm, gfn);
434         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
435                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
436
437                 if (gfn >= memslot->base_gfn
438                     && gfn < memslot->base_gfn + memslot->npages)
439                         return 1;
440         }
441         return 0;
442 }
443 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
444
445 static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
446 {
447         struct kvm_memory_slot *slot;
448
449         gfn = unalias_gfn(kvm, gfn);
450         slot = __gfn_to_memslot(kvm, gfn);
451         if (!slot)
452                 return bad_hva();
453         return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
454 }
455
456 /*
457  * Requires current->mm->mmap_sem to be held
458  */
459 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
460 {
461         struct page *page[1];
462         unsigned long addr;
463         int npages;
464
465         might_sleep();
466
467         addr = gfn_to_hva(kvm, gfn);
468         if (kvm_is_error_hva(addr)) {
469                 get_page(bad_page);
470                 return bad_page;
471         }
472
473         npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
474                                 NULL);
475
476         if (npages != 1) {
477                 get_page(bad_page);
478                 return bad_page;
479         }
480
481         return page[0];
482 }
483
484 EXPORT_SYMBOL_GPL(gfn_to_page);
485
486 void kvm_release_page_clean(struct page *page)
487 {
488         put_page(page);
489 }
490 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
491
492 void kvm_release_page_dirty(struct page *page)
493 {
494         if (!PageReserved(page))
495                 SetPageDirty(page);
496         put_page(page);
497 }
498 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
499
500 static int next_segment(unsigned long len, int offset)
501 {
502         if (len > PAGE_SIZE - offset)
503                 return PAGE_SIZE - offset;
504         else
505                 return len;
506 }
507
508 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
509                         int len)
510 {
511         int r;
512         unsigned long addr;
513
514         addr = gfn_to_hva(kvm, gfn);
515         if (kvm_is_error_hva(addr))
516                 return -EFAULT;
517         r = copy_from_user(data, (void __user *)addr + offset, len);
518         if (r)
519                 return -EFAULT;
520         return 0;
521 }
522 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
523
524 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
525 {
526         gfn_t gfn = gpa >> PAGE_SHIFT;
527         int seg;
528         int offset = offset_in_page(gpa);
529         int ret;
530
531         while ((seg = next_segment(len, offset)) != 0) {
532                 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
533                 if (ret < 0)
534                         return ret;
535                 offset = 0;
536                 len -= seg;
537                 data += seg;
538                 ++gfn;
539         }
540         return 0;
541 }
542 EXPORT_SYMBOL_GPL(kvm_read_guest);
543
544 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
545                          int offset, int len)
546 {
547         int r;
548         unsigned long addr;
549
550         addr = gfn_to_hva(kvm, gfn);
551         if (kvm_is_error_hva(addr))
552                 return -EFAULT;
553         r = copy_to_user((void __user *)addr + offset, data, len);
554         if (r)
555                 return -EFAULT;
556         mark_page_dirty(kvm, gfn);
557         return 0;
558 }
559 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
560
561 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
562                     unsigned long len)
563 {
564         gfn_t gfn = gpa >> PAGE_SHIFT;
565         int seg;
566         int offset = offset_in_page(gpa);
567         int ret;
568
569         while ((seg = next_segment(len, offset)) != 0) {
570                 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
571                 if (ret < 0)
572                         return ret;
573                 offset = 0;
574                 len -= seg;
575                 data += seg;
576                 ++gfn;
577         }
578         return 0;
579 }
580
581 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
582 {
583         return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
584 }
585 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
586
587 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
588 {
589         gfn_t gfn = gpa >> PAGE_SHIFT;
590         int seg;
591         int offset = offset_in_page(gpa);
592         int ret;
593
594         while ((seg = next_segment(len, offset)) != 0) {
595                 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
596                 if (ret < 0)
597                         return ret;
598                 offset = 0;
599                 len -= seg;
600                 ++gfn;
601         }
602         return 0;
603 }
604 EXPORT_SYMBOL_GPL(kvm_clear_guest);
605
606 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
607 {
608         struct kvm_memory_slot *memslot;
609
610         gfn = unalias_gfn(kvm, gfn);
611         memslot = __gfn_to_memslot(kvm, gfn);
612         if (memslot && memslot->dirty_bitmap) {
613                 unsigned long rel_gfn = gfn - memslot->base_gfn;
614
615                 /* avoid RMW */
616                 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
617                         set_bit(rel_gfn, memslot->dirty_bitmap);
618         }
619 }
620
621 /*
622  * The vCPU has executed a HLT instruction with in-kernel mode enabled.
623  */
624 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
625 {
626         DECLARE_WAITQUEUE(wait, current);
627
628         add_wait_queue(&vcpu->wq, &wait);
629
630         /*
631          * We will block until either an interrupt or a signal wakes us up
632          */
633         while (!kvm_cpu_has_interrupt(vcpu)
634                && !signal_pending(current)
635                && !kvm_arch_vcpu_runnable(vcpu)) {
636                 set_current_state(TASK_INTERRUPTIBLE);
637                 vcpu_put(vcpu);
638                 schedule();
639                 vcpu_load(vcpu);
640         }
641
642         __set_current_state(TASK_RUNNING);
643         remove_wait_queue(&vcpu->wq, &wait);
644 }
645
646 void kvm_resched(struct kvm_vcpu *vcpu)
647 {
648         if (!need_resched())
649                 return;
650         cond_resched();
651 }
652 EXPORT_SYMBOL_GPL(kvm_resched);
653
654 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
655 {
656         struct kvm_vcpu *vcpu = vma->vm_file->private_data;
657         struct page *page;
658
659         if (vmf->pgoff == 0)
660                 page = virt_to_page(vcpu->run);
661         else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
662                 page = virt_to_page(vcpu->arch.pio_data);
663         else
664                 return VM_FAULT_SIGBUS;
665         get_page(page);
666         vmf->page = page;
667         return 0;
668 }
669
670 static struct vm_operations_struct kvm_vcpu_vm_ops = {
671         .fault = kvm_vcpu_fault,
672 };
673
674 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
675 {
676         vma->vm_ops = &kvm_vcpu_vm_ops;
677         return 0;
678 }
679
680 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
681 {
682         struct kvm_vcpu *vcpu = filp->private_data;
683
684         fput(vcpu->kvm->filp);
685         return 0;
686 }
687
688 static struct file_operations kvm_vcpu_fops = {
689         .release        = kvm_vcpu_release,
690         .unlocked_ioctl = kvm_vcpu_ioctl,
691         .compat_ioctl   = kvm_vcpu_ioctl,
692         .mmap           = kvm_vcpu_mmap,
693 };
694
695 /*
696  * Allocates an inode for the vcpu.
697  */
698 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
699 {
700         int fd, r;
701         struct inode *inode;
702         struct file *file;
703
704         r = anon_inode_getfd(&fd, &inode, &file,
705                              "kvm-vcpu", &kvm_vcpu_fops, vcpu);
706         if (r)
707                 return r;
708         atomic_inc(&vcpu->kvm->filp->f_count);
709         return fd;
710 }
711
712 /*
713  * Creates some virtual cpus.  Good luck creating more than one.
714  */
715 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
716 {
717         int r;
718         struct kvm_vcpu *vcpu;
719
720         if (!valid_vcpu(n))
721                 return -EINVAL;
722
723         vcpu = kvm_arch_vcpu_create(kvm, n);
724         if (IS_ERR(vcpu))
725                 return PTR_ERR(vcpu);
726
727         preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
728
729         r = kvm_arch_vcpu_setup(vcpu);
730         if (r)
731                 goto vcpu_destroy;
732
733         mutex_lock(&kvm->lock);
734         if (kvm->vcpus[n]) {
735                 r = -EEXIST;
736                 mutex_unlock(&kvm->lock);
737                 goto vcpu_destroy;
738         }
739         kvm->vcpus[n] = vcpu;
740         mutex_unlock(&kvm->lock);
741
742         /* Now it's all set up, let userspace reach it */
743         r = create_vcpu_fd(vcpu);
744         if (r < 0)
745                 goto unlink;
746         return r;
747
748 unlink:
749         mutex_lock(&kvm->lock);
750         kvm->vcpus[n] = NULL;
751         mutex_unlock(&kvm->lock);
752 vcpu_destroy:
753         kvm_arch_vcpu_destroy(vcpu);
754         return r;
755 }
756
757 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
758 {
759         if (sigset) {
760                 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
761                 vcpu->sigset_active = 1;
762                 vcpu->sigset = *sigset;
763         } else
764                 vcpu->sigset_active = 0;
765         return 0;
766 }
767
768 static long kvm_vcpu_ioctl(struct file *filp,
769                            unsigned int ioctl, unsigned long arg)
770 {
771         struct kvm_vcpu *vcpu = filp->private_data;
772         void __user *argp = (void __user *)arg;
773         int r;
774
775         if (vcpu->kvm->mm != current->mm)
776                 return -EIO;
777         switch (ioctl) {
778         case KVM_RUN:
779                 r = -EINVAL;
780                 if (arg)
781                         goto out;
782                 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
783                 break;
784         case KVM_GET_REGS: {
785                 struct kvm_regs kvm_regs;
786
787                 memset(&kvm_regs, 0, sizeof kvm_regs);
788                 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
789                 if (r)
790                         goto out;
791                 r = -EFAULT;
792                 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
793                         goto out;
794                 r = 0;
795                 break;
796         }
797         case KVM_SET_REGS: {
798                 struct kvm_regs kvm_regs;
799
800                 r = -EFAULT;
801                 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
802                         goto out;
803                 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
804                 if (r)
805                         goto out;
806                 r = 0;
807                 break;
808         }
809         case KVM_GET_SREGS: {
810                 struct kvm_sregs kvm_sregs;
811
812                 memset(&kvm_sregs, 0, sizeof kvm_sregs);
813                 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
814                 if (r)
815                         goto out;
816                 r = -EFAULT;
817                 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
818                         goto out;
819                 r = 0;
820                 break;
821         }
822         case KVM_SET_SREGS: {
823                 struct kvm_sregs kvm_sregs;
824
825                 r = -EFAULT;
826                 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
827                         goto out;
828                 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
829                 if (r)
830                         goto out;
831                 r = 0;
832                 break;
833         }
834         case KVM_TRANSLATE: {
835                 struct kvm_translation tr;
836
837                 r = -EFAULT;
838                 if (copy_from_user(&tr, argp, sizeof tr))
839                         goto out;
840                 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
841                 if (r)
842                         goto out;
843                 r = -EFAULT;
844                 if (copy_to_user(argp, &tr, sizeof tr))
845                         goto out;
846                 r = 0;
847                 break;
848         }
849         case KVM_DEBUG_GUEST: {
850                 struct kvm_debug_guest dbg;
851
852                 r = -EFAULT;
853                 if (copy_from_user(&dbg, argp, sizeof dbg))
854                         goto out;
855                 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
856                 if (r)
857                         goto out;
858                 r = 0;
859                 break;
860         }
861         case KVM_SET_SIGNAL_MASK: {
862                 struct kvm_signal_mask __user *sigmask_arg = argp;
863                 struct kvm_signal_mask kvm_sigmask;
864                 sigset_t sigset, *p;
865
866                 p = NULL;
867                 if (argp) {
868                         r = -EFAULT;
869                         if (copy_from_user(&kvm_sigmask, argp,
870                                            sizeof kvm_sigmask))
871                                 goto out;
872                         r = -EINVAL;
873                         if (kvm_sigmask.len != sizeof sigset)
874                                 goto out;
875                         r = -EFAULT;
876                         if (copy_from_user(&sigset, sigmask_arg->sigset,
877                                            sizeof sigset))
878                                 goto out;
879                         p = &sigset;
880                 }
881                 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
882                 break;
883         }
884         case KVM_GET_FPU: {
885                 struct kvm_fpu fpu;
886
887                 memset(&fpu, 0, sizeof fpu);
888                 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
889                 if (r)
890                         goto out;
891                 r = -EFAULT;
892                 if (copy_to_user(argp, &fpu, sizeof fpu))
893                         goto out;
894                 r = 0;
895                 break;
896         }
897         case KVM_SET_FPU: {
898                 struct kvm_fpu fpu;
899
900                 r = -EFAULT;
901                 if (copy_from_user(&fpu, argp, sizeof fpu))
902                         goto out;
903                 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
904                 if (r)
905                         goto out;
906                 r = 0;
907                 break;
908         }
909         default:
910                 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
911         }
912 out:
913         return r;
914 }
915
916 static long kvm_vm_ioctl(struct file *filp,
917                            unsigned int ioctl, unsigned long arg)
918 {
919         struct kvm *kvm = filp->private_data;
920         void __user *argp = (void __user *)arg;
921         int r;
922
923         if (kvm->mm != current->mm)
924                 return -EIO;
925         switch (ioctl) {
926         case KVM_CREATE_VCPU:
927                 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
928                 if (r < 0)
929                         goto out;
930                 break;
931         case KVM_SET_USER_MEMORY_REGION: {
932                 struct kvm_userspace_memory_region kvm_userspace_mem;
933
934                 r = -EFAULT;
935                 if (copy_from_user(&kvm_userspace_mem, argp,
936                                                 sizeof kvm_userspace_mem))
937                         goto out;
938
939                 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
940                 if (r)
941                         goto out;
942                 break;
943         }
944         case KVM_GET_DIRTY_LOG: {
945                 struct kvm_dirty_log log;
946
947                 r = -EFAULT;
948                 if (copy_from_user(&log, argp, sizeof log))
949                         goto out;
950                 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
951                 if (r)
952                         goto out;
953                 break;
954         }
955         default:
956                 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
957         }
958 out:
959         return r;
960 }
961
962 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
963 {
964         struct kvm *kvm = vma->vm_file->private_data;
965         struct page *page;
966
967         if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
968                 return VM_FAULT_SIGBUS;
969         page = gfn_to_page(kvm, vmf->pgoff);
970         if (is_error_page(page)) {
971                 kvm_release_page_clean(page);
972                 return VM_FAULT_SIGBUS;
973         }
974         vmf->page = page;
975         return 0;
976 }
977
978 static struct vm_operations_struct kvm_vm_vm_ops = {
979         .fault = kvm_vm_fault,
980 };
981
982 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
983 {
984         vma->vm_ops = &kvm_vm_vm_ops;
985         return 0;
986 }
987
988 static struct file_operations kvm_vm_fops = {
989         .release        = kvm_vm_release,
990         .unlocked_ioctl = kvm_vm_ioctl,
991         .compat_ioctl   = kvm_vm_ioctl,
992         .mmap           = kvm_vm_mmap,
993 };
994
995 static int kvm_dev_ioctl_create_vm(void)
996 {
997         int fd, r;
998         struct inode *inode;
999         struct file *file;
1000         struct kvm *kvm;
1001
1002         kvm = kvm_create_vm();
1003         if (IS_ERR(kvm))
1004                 return PTR_ERR(kvm);
1005         r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1006         if (r) {
1007                 kvm_destroy_vm(kvm);
1008                 return r;
1009         }
1010
1011         kvm->filp = file;
1012
1013         return fd;
1014 }
1015
1016 static long kvm_dev_ioctl(struct file *filp,
1017                           unsigned int ioctl, unsigned long arg)
1018 {
1019         void __user *argp = (void __user *)arg;
1020         long r = -EINVAL;
1021
1022         switch (ioctl) {
1023         case KVM_GET_API_VERSION:
1024                 r = -EINVAL;
1025                 if (arg)
1026                         goto out;
1027                 r = KVM_API_VERSION;
1028                 break;
1029         case KVM_CREATE_VM:
1030                 r = -EINVAL;
1031                 if (arg)
1032                         goto out;
1033                 r = kvm_dev_ioctl_create_vm();
1034                 break;
1035         case KVM_CHECK_EXTENSION:
1036                 r = kvm_dev_ioctl_check_extension((long)argp);
1037                 break;
1038         case KVM_GET_VCPU_MMAP_SIZE:
1039                 r = -EINVAL;
1040                 if (arg)
1041                         goto out;
1042                 r = 2 * PAGE_SIZE;
1043                 break;
1044         default:
1045                 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1046         }
1047 out:
1048         return r;
1049 }
1050
1051 static struct file_operations kvm_chardev_ops = {
1052         .unlocked_ioctl = kvm_dev_ioctl,
1053         .compat_ioctl   = kvm_dev_ioctl,
1054 };
1055
1056 static struct miscdevice kvm_dev = {
1057         KVM_MINOR,
1058         "kvm",
1059         &kvm_chardev_ops,
1060 };
1061
1062 static void hardware_enable(void *junk)
1063 {
1064         int cpu = raw_smp_processor_id();
1065
1066         if (cpu_isset(cpu, cpus_hardware_enabled))
1067                 return;
1068         cpu_set(cpu, cpus_hardware_enabled);
1069         kvm_arch_hardware_enable(NULL);
1070 }
1071
1072 static void hardware_disable(void *junk)
1073 {
1074         int cpu = raw_smp_processor_id();
1075
1076         if (!cpu_isset(cpu, cpus_hardware_enabled))
1077                 return;
1078         cpu_clear(cpu, cpus_hardware_enabled);
1079         decache_vcpus_on_cpu(cpu);
1080         kvm_arch_hardware_disable(NULL);
1081 }
1082
1083 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1084                            void *v)
1085 {
1086         int cpu = (long)v;
1087
1088         val &= ~CPU_TASKS_FROZEN;
1089         switch (val) {
1090         case CPU_DYING:
1091                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1092                        cpu);
1093                 hardware_disable(NULL);
1094                 break;
1095         case CPU_UP_CANCELED:
1096                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1097                        cpu);
1098                 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
1099                 break;
1100         case CPU_ONLINE:
1101                 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1102                        cpu);
1103                 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
1104                 break;
1105         }
1106         return NOTIFY_OK;
1107 }
1108
1109 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1110                       void *v)
1111 {
1112         if (val == SYS_RESTART) {
1113                 /*
1114                  * Some (well, at least mine) BIOSes hang on reboot if
1115                  * in vmx root mode.
1116                  */
1117                 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1118                 on_each_cpu(hardware_disable, NULL, 0, 1);
1119         }
1120         return NOTIFY_OK;
1121 }
1122
1123 static struct notifier_block kvm_reboot_notifier = {
1124         .notifier_call = kvm_reboot,
1125         .priority = 0,
1126 };
1127
1128 void kvm_io_bus_init(struct kvm_io_bus *bus)
1129 {
1130         memset(bus, 0, sizeof(*bus));
1131 }
1132
1133 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1134 {
1135         int i;
1136
1137         for (i = 0; i < bus->dev_count; i++) {
1138                 struct kvm_io_device *pos = bus->devs[i];
1139
1140                 kvm_iodevice_destructor(pos);
1141         }
1142 }
1143
1144 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1145 {
1146         int i;
1147
1148         for (i = 0; i < bus->dev_count; i++) {
1149                 struct kvm_io_device *pos = bus->devs[i];
1150
1151                 if (pos->in_range(pos, addr))
1152                         return pos;
1153         }
1154
1155         return NULL;
1156 }
1157
1158 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1159 {
1160         BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1161
1162         bus->devs[bus->dev_count++] = dev;
1163 }
1164
1165 static struct notifier_block kvm_cpu_notifier = {
1166         .notifier_call = kvm_cpu_hotplug,
1167         .priority = 20, /* must be > scheduler priority */
1168 };
1169
1170 static u64 vm_stat_get(void *_offset)
1171 {
1172         unsigned offset = (long)_offset;
1173         u64 total = 0;
1174         struct kvm *kvm;
1175
1176         spin_lock(&kvm_lock);
1177         list_for_each_entry(kvm, &vm_list, vm_list)
1178                 total += *(u32 *)((void *)kvm + offset);
1179         spin_unlock(&kvm_lock);
1180         return total;
1181 }
1182
1183 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1184
1185 static u64 vcpu_stat_get(void *_offset)
1186 {
1187         unsigned offset = (long)_offset;
1188         u64 total = 0;
1189         struct kvm *kvm;
1190         struct kvm_vcpu *vcpu;
1191         int i;
1192
1193         spin_lock(&kvm_lock);
1194         list_for_each_entry(kvm, &vm_list, vm_list)
1195                 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1196                         vcpu = kvm->vcpus[i];
1197                         if (vcpu)
1198                                 total += *(u32 *)((void *)vcpu + offset);
1199                 }
1200         spin_unlock(&kvm_lock);
1201         return total;
1202 }
1203
1204 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1205
1206 static struct file_operations *stat_fops[] = {
1207         [KVM_STAT_VCPU] = &vcpu_stat_fops,
1208         [KVM_STAT_VM]   = &vm_stat_fops,
1209 };
1210
1211 static void kvm_init_debug(void)
1212 {
1213         struct kvm_stats_debugfs_item *p;
1214
1215         debugfs_dir = debugfs_create_dir("kvm", NULL);
1216         for (p = debugfs_entries; p->name; ++p)
1217                 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1218                                                 (void *)(long)p->offset,
1219                                                 stat_fops[p->kind]);
1220 }
1221
1222 static void kvm_exit_debug(void)
1223 {
1224         struct kvm_stats_debugfs_item *p;
1225
1226         for (p = debugfs_entries; p->name; ++p)
1227                 debugfs_remove(p->dentry);
1228         debugfs_remove(debugfs_dir);
1229 }
1230
1231 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1232 {
1233         hardware_disable(NULL);
1234         return 0;
1235 }
1236
1237 static int kvm_resume(struct sys_device *dev)
1238 {
1239         hardware_enable(NULL);
1240         return 0;
1241 }
1242
1243 static struct sysdev_class kvm_sysdev_class = {
1244         .name = "kvm",
1245         .suspend = kvm_suspend,
1246         .resume = kvm_resume,
1247 };
1248
1249 static struct sys_device kvm_sysdev = {
1250         .id = 0,
1251         .cls = &kvm_sysdev_class,
1252 };
1253
1254 struct page *bad_page;
1255
1256 static inline
1257 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1258 {
1259         return container_of(pn, struct kvm_vcpu, preempt_notifier);
1260 }
1261
1262 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1263 {
1264         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1265
1266         kvm_arch_vcpu_load(vcpu, cpu);
1267 }
1268
1269 static void kvm_sched_out(struct preempt_notifier *pn,
1270                           struct task_struct *next)
1271 {
1272         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1273
1274         kvm_arch_vcpu_put(vcpu);
1275 }
1276
1277 int kvm_init(void *opaque, unsigned int vcpu_size,
1278                   struct module *module)
1279 {
1280         int r;
1281         int cpu;
1282
1283         kvm_init_debug();
1284
1285         r = kvm_arch_init(opaque);
1286         if (r)
1287                 goto out_fail;
1288
1289         bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1290
1291         if (bad_page == NULL) {
1292                 r = -ENOMEM;
1293                 goto out;
1294         }
1295
1296         r = kvm_arch_hardware_setup();
1297         if (r < 0)
1298                 goto out_free_0;
1299
1300         for_each_online_cpu(cpu) {
1301                 smp_call_function_single(cpu,
1302                                 kvm_arch_check_processor_compat,
1303                                 &r, 0, 1);
1304                 if (r < 0)
1305                         goto out_free_1;
1306         }
1307
1308         on_each_cpu(hardware_enable, NULL, 0, 1);
1309         r = register_cpu_notifier(&kvm_cpu_notifier);
1310         if (r)
1311                 goto out_free_2;
1312         register_reboot_notifier(&kvm_reboot_notifier);
1313
1314         r = sysdev_class_register(&kvm_sysdev_class);
1315         if (r)
1316                 goto out_free_3;
1317
1318         r = sysdev_register(&kvm_sysdev);
1319         if (r)
1320                 goto out_free_4;
1321
1322         /* A kmem cache lets us meet the alignment requirements of fx_save. */
1323         kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
1324                                            __alignof__(struct kvm_vcpu),
1325                                            0, NULL);
1326         if (!kvm_vcpu_cache) {
1327                 r = -ENOMEM;
1328                 goto out_free_5;
1329         }
1330
1331         kvm_chardev_ops.owner = module;
1332
1333         r = misc_register(&kvm_dev);
1334         if (r) {
1335                 printk(KERN_ERR "kvm: misc device register failed\n");
1336                 goto out_free;
1337         }
1338
1339         kvm_preempt_ops.sched_in = kvm_sched_in;
1340         kvm_preempt_ops.sched_out = kvm_sched_out;
1341
1342         return 0;
1343
1344 out_free:
1345         kmem_cache_destroy(kvm_vcpu_cache);
1346 out_free_5:
1347         sysdev_unregister(&kvm_sysdev);
1348 out_free_4:
1349         sysdev_class_unregister(&kvm_sysdev_class);
1350 out_free_3:
1351         unregister_reboot_notifier(&kvm_reboot_notifier);
1352         unregister_cpu_notifier(&kvm_cpu_notifier);
1353 out_free_2:
1354         on_each_cpu(hardware_disable, NULL, 0, 1);
1355 out_free_1:
1356         kvm_arch_hardware_unsetup();
1357 out_free_0:
1358         __free_page(bad_page);
1359 out:
1360         kvm_arch_exit();
1361         kvm_exit_debug();
1362 out_fail:
1363         return r;
1364 }
1365 EXPORT_SYMBOL_GPL(kvm_init);
1366
1367 void kvm_exit(void)
1368 {
1369         misc_deregister(&kvm_dev);
1370         kmem_cache_destroy(kvm_vcpu_cache);
1371         sysdev_unregister(&kvm_sysdev);
1372         sysdev_class_unregister(&kvm_sysdev_class);
1373         unregister_reboot_notifier(&kvm_reboot_notifier);
1374         unregister_cpu_notifier(&kvm_cpu_notifier);
1375         on_each_cpu(hardware_disable, NULL, 0, 1);
1376         kvm_arch_hardware_unsetup();
1377         kvm_arch_exit();
1378         kvm_exit_debug();
1379         __free_page(bad_page);
1380 }
1381 EXPORT_SYMBOL_GPL(kvm_exit);