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