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