KVM: Handle vma regions with no backing page
[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         pfn_t pfn;
536
537         might_sleep();
538
539         addr = gfn_to_hva(kvm, gfn);
540         if (kvm_is_error_hva(addr)) {
541                 get_page(bad_page);
542                 return page_to_pfn(bad_page);
543         }
544
545         npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
546                                 NULL);
547
548         if (unlikely(npages != 1)) {
549                 struct vm_area_struct *vma;
550
551                 vma = find_vma(current->mm, addr);
552                 if (vma == NULL || addr < vma->vm_start ||
553                     !(vma->vm_flags & VM_PFNMAP)) {
554                         get_page(bad_page);
555                         return page_to_pfn(bad_page);
556                 }
557
558                 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
559                 BUG_ON(pfn_valid(pfn));
560         } else
561                 pfn = page_to_pfn(page[0]);
562
563         return pfn;
564 }
565
566 EXPORT_SYMBOL_GPL(gfn_to_pfn);
567
568 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
569 {
570         pfn_t pfn;
571
572         pfn = gfn_to_pfn(kvm, gfn);
573         if (pfn_valid(pfn))
574                 return pfn_to_page(pfn);
575
576         WARN_ON(!pfn_valid(pfn));
577
578         get_page(bad_page);
579         return bad_page;
580 }
581
582 EXPORT_SYMBOL_GPL(gfn_to_page);
583
584 void kvm_release_page_clean(struct page *page)
585 {
586         kvm_release_pfn_clean(page_to_pfn(page));
587 }
588 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
589
590 void kvm_release_pfn_clean(pfn_t pfn)
591 {
592         if (pfn_valid(pfn))
593                 put_page(pfn_to_page(pfn));
594 }
595 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
596
597 void kvm_release_page_dirty(struct page *page)
598 {
599         kvm_release_pfn_dirty(page_to_pfn(page));
600 }
601 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
602
603 void kvm_release_pfn_dirty(pfn_t pfn)
604 {
605         kvm_set_pfn_dirty(pfn);
606         kvm_release_pfn_clean(pfn);
607 }
608 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
609
610 void kvm_set_page_dirty(struct page *page)
611 {
612         kvm_set_pfn_dirty(page_to_pfn(page));
613 }
614 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
615
616 void kvm_set_pfn_dirty(pfn_t pfn)
617 {
618         if (pfn_valid(pfn)) {
619                 struct page *page = pfn_to_page(pfn);
620                 if (!PageReserved(page))
621                         SetPageDirty(page);
622         }
623 }
624 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
625
626 void kvm_set_pfn_accessed(pfn_t pfn)
627 {
628         if (pfn_valid(pfn))
629                 mark_page_accessed(pfn_to_page(pfn));
630 }
631 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
632
633 void kvm_get_pfn(pfn_t pfn)
634 {
635         if (pfn_valid(pfn))
636                 get_page(pfn_to_page(pfn));
637 }
638 EXPORT_SYMBOL_GPL(kvm_get_pfn);
639
640 static int next_segment(unsigned long len, int offset)
641 {
642         if (len > PAGE_SIZE - offset)
643                 return PAGE_SIZE - offset;
644         else
645                 return len;
646 }
647
648 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
649                         int len)
650 {
651         int r;
652         unsigned long addr;
653
654         addr = gfn_to_hva(kvm, gfn);
655         if (kvm_is_error_hva(addr))
656                 return -EFAULT;
657         r = copy_from_user(data, (void __user *)addr + offset, len);
658         if (r)
659                 return -EFAULT;
660         return 0;
661 }
662 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
663
664 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
665 {
666         gfn_t gfn = gpa >> PAGE_SHIFT;
667         int seg;
668         int offset = offset_in_page(gpa);
669         int ret;
670
671         while ((seg = next_segment(len, offset)) != 0) {
672                 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
673                 if (ret < 0)
674                         return ret;
675                 offset = 0;
676                 len -= seg;
677                 data += seg;
678                 ++gfn;
679         }
680         return 0;
681 }
682 EXPORT_SYMBOL_GPL(kvm_read_guest);
683
684 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
685                           unsigned long len)
686 {
687         int r;
688         unsigned long addr;
689         gfn_t gfn = gpa >> PAGE_SHIFT;
690         int offset = offset_in_page(gpa);
691
692         addr = gfn_to_hva(kvm, gfn);
693         if (kvm_is_error_hva(addr))
694                 return -EFAULT;
695         pagefault_disable();
696         r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
697         pagefault_enable();
698         if (r)
699                 return -EFAULT;
700         return 0;
701 }
702 EXPORT_SYMBOL(kvm_read_guest_atomic);
703
704 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
705                          int offset, int len)
706 {
707         int r;
708         unsigned long addr;
709
710         addr = gfn_to_hva(kvm, gfn);
711         if (kvm_is_error_hva(addr))
712                 return -EFAULT;
713         r = copy_to_user((void __user *)addr + offset, data, len);
714         if (r)
715                 return -EFAULT;
716         mark_page_dirty(kvm, gfn);
717         return 0;
718 }
719 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
720
721 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
722                     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_write_guest_page(kvm, gfn, data, offset, seg);
731                 if (ret < 0)
732                         return ret;
733                 offset = 0;
734                 len -= seg;
735                 data += seg;
736                 ++gfn;
737         }
738         return 0;
739 }
740
741 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
742 {
743         return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
744 }
745 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
746
747 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
748 {
749         gfn_t gfn = gpa >> PAGE_SHIFT;
750         int seg;
751         int offset = offset_in_page(gpa);
752         int ret;
753
754         while ((seg = next_segment(len, offset)) != 0) {
755                 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
756                 if (ret < 0)
757                         return ret;
758                 offset = 0;
759                 len -= seg;
760                 ++gfn;
761         }
762         return 0;
763 }
764 EXPORT_SYMBOL_GPL(kvm_clear_guest);
765
766 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
767 {
768         struct kvm_memory_slot *memslot;
769
770         gfn = unalias_gfn(kvm, gfn);
771         memslot = __gfn_to_memslot(kvm, gfn);
772         if (memslot && memslot->dirty_bitmap) {
773                 unsigned long rel_gfn = gfn - memslot->base_gfn;
774
775                 /* avoid RMW */
776                 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
777                         set_bit(rel_gfn, memslot->dirty_bitmap);
778         }
779 }
780
781 /*
782  * The vCPU has executed a HLT instruction with in-kernel mode enabled.
783  */
784 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
785 {
786         DEFINE_WAIT(wait);
787
788         for (;;) {
789                 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
790
791                 if (kvm_cpu_has_interrupt(vcpu))
792                         break;
793                 if (kvm_cpu_has_pending_timer(vcpu))
794                         break;
795                 if (kvm_arch_vcpu_runnable(vcpu))
796                         break;
797                 if (signal_pending(current))
798                         break;
799
800                 vcpu_put(vcpu);
801                 schedule();
802                 vcpu_load(vcpu);
803         }
804
805         finish_wait(&vcpu->wq, &wait);
806 }
807
808 void kvm_resched(struct kvm_vcpu *vcpu)
809 {
810         if (!need_resched())
811                 return;
812         cond_resched();
813 }
814 EXPORT_SYMBOL_GPL(kvm_resched);
815
816 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
817 {
818         struct kvm_vcpu *vcpu = vma->vm_file->private_data;
819         struct page *page;
820
821         if (vmf->pgoff == 0)
822                 page = virt_to_page(vcpu->run);
823 #ifdef CONFIG_X86
824         else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
825                 page = virt_to_page(vcpu->arch.pio_data);
826 #endif
827         else
828                 return VM_FAULT_SIGBUS;
829         get_page(page);
830         vmf->page = page;
831         return 0;
832 }
833
834 static struct vm_operations_struct kvm_vcpu_vm_ops = {
835         .fault = kvm_vcpu_fault,
836 };
837
838 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
839 {
840         vma->vm_ops = &kvm_vcpu_vm_ops;
841         return 0;
842 }
843
844 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
845 {
846         struct kvm_vcpu *vcpu = filp->private_data;
847
848         kvm_put_kvm(vcpu->kvm);
849         return 0;
850 }
851
852 static const struct file_operations kvm_vcpu_fops = {
853         .release        = kvm_vcpu_release,
854         .unlocked_ioctl = kvm_vcpu_ioctl,
855         .compat_ioctl   = kvm_vcpu_ioctl,
856         .mmap           = kvm_vcpu_mmap,
857 };
858
859 /*
860  * Allocates an inode for the vcpu.
861  */
862 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
863 {
864         int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu);
865         if (fd < 0)
866                 kvm_put_kvm(vcpu->kvm);
867         return fd;
868 }
869
870 /*
871  * Creates some virtual cpus.  Good luck creating more than one.
872  */
873 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
874 {
875         int r;
876         struct kvm_vcpu *vcpu;
877
878         if (!valid_vcpu(n))
879                 return -EINVAL;
880
881         vcpu = kvm_arch_vcpu_create(kvm, n);
882         if (IS_ERR(vcpu))
883                 return PTR_ERR(vcpu);
884
885         preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
886
887         r = kvm_arch_vcpu_setup(vcpu);
888         if (r)
889                 goto vcpu_destroy;
890
891         mutex_lock(&kvm->lock);
892         if (kvm->vcpus[n]) {
893                 r = -EEXIST;
894                 mutex_unlock(&kvm->lock);
895                 goto vcpu_destroy;
896         }
897         kvm->vcpus[n] = vcpu;
898         mutex_unlock(&kvm->lock);
899
900         /* Now it's all set up, let userspace reach it */
901         kvm_get_kvm(kvm);
902         r = create_vcpu_fd(vcpu);
903         if (r < 0)
904                 goto unlink;
905         return r;
906
907 unlink:
908         mutex_lock(&kvm->lock);
909         kvm->vcpus[n] = NULL;
910         mutex_unlock(&kvm->lock);
911 vcpu_destroy:
912         kvm_arch_vcpu_destroy(vcpu);
913         return r;
914 }
915
916 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
917 {
918         if (sigset) {
919                 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
920                 vcpu->sigset_active = 1;
921                 vcpu->sigset = *sigset;
922         } else
923                 vcpu->sigset_active = 0;
924         return 0;
925 }
926
927 static long kvm_vcpu_ioctl(struct file *filp,
928                            unsigned int ioctl, unsigned long arg)
929 {
930         struct kvm_vcpu *vcpu = filp->private_data;
931         void __user *argp = (void __user *)arg;
932         int r;
933
934         if (vcpu->kvm->mm != current->mm)
935                 return -EIO;
936         switch (ioctl) {
937         case KVM_RUN:
938                 r = -EINVAL;
939                 if (arg)
940                         goto out;
941                 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
942                 break;
943         case KVM_GET_REGS: {
944                 struct kvm_regs *kvm_regs;
945
946                 r = -ENOMEM;
947                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
948                 if (!kvm_regs)
949                         goto out;
950                 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
951                 if (r)
952                         goto out_free1;
953                 r = -EFAULT;
954                 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
955                         goto out_free1;
956                 r = 0;
957 out_free1:
958                 kfree(kvm_regs);
959                 break;
960         }
961         case KVM_SET_REGS: {
962                 struct kvm_regs *kvm_regs;
963
964                 r = -ENOMEM;
965                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
966                 if (!kvm_regs)
967                         goto out;
968                 r = -EFAULT;
969                 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
970                         goto out_free2;
971                 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
972                 if (r)
973                         goto out_free2;
974                 r = 0;
975 out_free2:
976                 kfree(kvm_regs);
977                 break;
978         }
979         case KVM_GET_SREGS: {
980                 struct kvm_sregs kvm_sregs;
981
982                 memset(&kvm_sregs, 0, sizeof kvm_sregs);
983                 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
984                 if (r)
985                         goto out;
986                 r = -EFAULT;
987                 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
988                         goto out;
989                 r = 0;
990                 break;
991         }
992         case KVM_SET_SREGS: {
993                 struct kvm_sregs kvm_sregs;
994
995                 r = -EFAULT;
996                 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
997                         goto out;
998                 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
999                 if (r)
1000                         goto out;
1001                 r = 0;
1002                 break;
1003         }
1004         case KVM_GET_MP_STATE: {
1005                 struct kvm_mp_state mp_state;
1006
1007                 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1008                 if (r)
1009                         goto out;
1010                 r = -EFAULT;
1011                 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1012                         goto out;
1013                 r = 0;
1014                 break;
1015         }
1016         case KVM_SET_MP_STATE: {
1017                 struct kvm_mp_state mp_state;
1018
1019                 r = -EFAULT;
1020                 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1021                         goto out;
1022                 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1023                 if (r)
1024                         goto out;
1025                 r = 0;
1026                 break;
1027         }
1028         case KVM_TRANSLATE: {
1029                 struct kvm_translation tr;
1030
1031                 r = -EFAULT;
1032                 if (copy_from_user(&tr, argp, sizeof tr))
1033                         goto out;
1034                 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1035                 if (r)
1036                         goto out;
1037                 r = -EFAULT;
1038                 if (copy_to_user(argp, &tr, sizeof tr))
1039                         goto out;
1040                 r = 0;
1041                 break;
1042         }
1043         case KVM_DEBUG_GUEST: {
1044                 struct kvm_debug_guest dbg;
1045
1046                 r = -EFAULT;
1047                 if (copy_from_user(&dbg, argp, sizeof dbg))
1048                         goto out;
1049                 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1050                 if (r)
1051                         goto out;
1052                 r = 0;
1053                 break;
1054         }
1055         case KVM_SET_SIGNAL_MASK: {
1056                 struct kvm_signal_mask __user *sigmask_arg = argp;
1057                 struct kvm_signal_mask kvm_sigmask;
1058                 sigset_t sigset, *p;
1059
1060                 p = NULL;
1061                 if (argp) {
1062                         r = -EFAULT;
1063                         if (copy_from_user(&kvm_sigmask, argp,
1064                                            sizeof kvm_sigmask))
1065                                 goto out;
1066                         r = -EINVAL;
1067                         if (kvm_sigmask.len != sizeof sigset)
1068                                 goto out;
1069                         r = -EFAULT;
1070                         if (copy_from_user(&sigset, sigmask_arg->sigset,
1071                                            sizeof sigset))
1072                                 goto out;
1073                         p = &sigset;
1074                 }
1075                 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1076                 break;
1077         }
1078         case KVM_GET_FPU: {
1079                 struct kvm_fpu fpu;
1080
1081                 memset(&fpu, 0, sizeof fpu);
1082                 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
1083                 if (r)
1084                         goto out;
1085                 r = -EFAULT;
1086                 if (copy_to_user(argp, &fpu, sizeof fpu))
1087                         goto out;
1088                 r = 0;
1089                 break;
1090         }
1091         case KVM_SET_FPU: {
1092                 struct kvm_fpu fpu;
1093
1094                 r = -EFAULT;
1095                 if (copy_from_user(&fpu, argp, sizeof fpu))
1096                         goto out;
1097                 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
1098                 if (r)
1099                         goto out;
1100                 r = 0;
1101                 break;
1102         }
1103         default:
1104                 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1105         }
1106 out:
1107         return r;
1108 }
1109
1110 static long kvm_vm_ioctl(struct file *filp,
1111                            unsigned int ioctl, unsigned long arg)
1112 {
1113         struct kvm *kvm = filp->private_data;
1114         void __user *argp = (void __user *)arg;
1115         int r;
1116
1117         if (kvm->mm != current->mm)
1118                 return -EIO;
1119         switch (ioctl) {
1120         case KVM_CREATE_VCPU:
1121                 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1122                 if (r < 0)
1123                         goto out;
1124                 break;
1125         case KVM_SET_USER_MEMORY_REGION: {
1126                 struct kvm_userspace_memory_region kvm_userspace_mem;
1127
1128                 r = -EFAULT;
1129                 if (copy_from_user(&kvm_userspace_mem, argp,
1130                                                 sizeof kvm_userspace_mem))
1131                         goto out;
1132
1133                 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1134                 if (r)
1135                         goto out;
1136                 break;
1137         }
1138         case KVM_GET_DIRTY_LOG: {
1139                 struct kvm_dirty_log log;
1140
1141                 r = -EFAULT;
1142                 if (copy_from_user(&log, argp, sizeof log))
1143                         goto out;
1144                 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1145                 if (r)
1146                         goto out;
1147                 break;
1148         }
1149         default:
1150                 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1151         }
1152 out:
1153         return r;
1154 }
1155
1156 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1157 {
1158         struct kvm *kvm = vma->vm_file->private_data;
1159         struct page *page;
1160
1161         if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
1162                 return VM_FAULT_SIGBUS;
1163         page = gfn_to_page(kvm, vmf->pgoff);
1164         if (is_error_page(page)) {
1165                 kvm_release_page_clean(page);
1166                 return VM_FAULT_SIGBUS;
1167         }
1168         vmf->page = page;
1169         return 0;
1170 }
1171
1172 static struct vm_operations_struct kvm_vm_vm_ops = {
1173         .fault = kvm_vm_fault,
1174 };
1175
1176 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1177 {
1178         vma->vm_ops = &kvm_vm_vm_ops;
1179         return 0;
1180 }
1181
1182 static const struct file_operations kvm_vm_fops = {
1183         .release        = kvm_vm_release,
1184         .unlocked_ioctl = kvm_vm_ioctl,
1185         .compat_ioctl   = kvm_vm_ioctl,
1186         .mmap           = kvm_vm_mmap,
1187 };
1188
1189 static int kvm_dev_ioctl_create_vm(void)
1190 {
1191         int fd;
1192         struct kvm *kvm;
1193
1194         kvm = kvm_create_vm();
1195         if (IS_ERR(kvm))
1196                 return PTR_ERR(kvm);
1197         fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm);
1198         if (fd < 0)
1199                 kvm_put_kvm(kvm);
1200
1201         return fd;
1202 }
1203
1204 static long kvm_dev_ioctl(struct file *filp,
1205                           unsigned int ioctl, unsigned long arg)
1206 {
1207         long r = -EINVAL;
1208
1209         switch (ioctl) {
1210         case KVM_GET_API_VERSION:
1211                 r = -EINVAL;
1212                 if (arg)
1213                         goto out;
1214                 r = KVM_API_VERSION;
1215                 break;
1216         case KVM_CREATE_VM:
1217                 r = -EINVAL;
1218                 if (arg)
1219                         goto out;
1220                 r = kvm_dev_ioctl_create_vm();
1221                 break;
1222         case KVM_CHECK_EXTENSION:
1223                 r = kvm_dev_ioctl_check_extension(arg);
1224                 break;
1225         case KVM_GET_VCPU_MMAP_SIZE:
1226                 r = -EINVAL;
1227                 if (arg)
1228                         goto out;
1229                 r = PAGE_SIZE;     /* struct kvm_run */
1230 #ifdef CONFIG_X86
1231                 r += PAGE_SIZE;    /* pio data page */
1232 #endif
1233                 break;
1234         case KVM_TRACE_ENABLE:
1235         case KVM_TRACE_PAUSE:
1236         case KVM_TRACE_DISABLE:
1237                 r = kvm_trace_ioctl(ioctl, arg);
1238                 break;
1239         default:
1240                 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1241         }
1242 out:
1243         return r;
1244 }
1245
1246 static struct file_operations kvm_chardev_ops = {
1247         .unlocked_ioctl = kvm_dev_ioctl,
1248         .compat_ioctl   = kvm_dev_ioctl,
1249 };
1250
1251 static struct miscdevice kvm_dev = {
1252         KVM_MINOR,
1253         "kvm",
1254         &kvm_chardev_ops,
1255 };
1256
1257 static void hardware_enable(void *junk)
1258 {
1259         int cpu = raw_smp_processor_id();
1260
1261         if (cpu_isset(cpu, cpus_hardware_enabled))
1262                 return;
1263         cpu_set(cpu, cpus_hardware_enabled);
1264         kvm_arch_hardware_enable(NULL);
1265 }
1266
1267 static void hardware_disable(void *junk)
1268 {
1269         int cpu = raw_smp_processor_id();
1270
1271         if (!cpu_isset(cpu, cpus_hardware_enabled))
1272                 return;
1273         cpu_clear(cpu, cpus_hardware_enabled);
1274         decache_vcpus_on_cpu(cpu);
1275         kvm_arch_hardware_disable(NULL);
1276 }
1277
1278 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1279                            void *v)
1280 {
1281         int cpu = (long)v;
1282
1283         val &= ~CPU_TASKS_FROZEN;
1284         switch (val) {
1285         case CPU_DYING:
1286                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1287                        cpu);
1288                 hardware_disable(NULL);
1289                 break;
1290         case CPU_UP_CANCELED:
1291                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1292                        cpu);
1293                 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1294                 break;
1295         case CPU_ONLINE:
1296                 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1297                        cpu);
1298                 smp_call_function_single(cpu, hardware_enable, NULL, 1);
1299                 break;
1300         }
1301         return NOTIFY_OK;
1302 }
1303
1304 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1305                       void *v)
1306 {
1307         if (val == SYS_RESTART) {
1308                 /*
1309                  * Some (well, at least mine) BIOSes hang on reboot if
1310                  * in vmx root mode.
1311                  */
1312                 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1313                 on_each_cpu(hardware_disable, NULL, 1);
1314         }
1315         return NOTIFY_OK;
1316 }
1317
1318 static struct notifier_block kvm_reboot_notifier = {
1319         .notifier_call = kvm_reboot,
1320         .priority = 0,
1321 };
1322
1323 void kvm_io_bus_init(struct kvm_io_bus *bus)
1324 {
1325         memset(bus, 0, sizeof(*bus));
1326 }
1327
1328 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1329 {
1330         int i;
1331
1332         for (i = 0; i < bus->dev_count; i++) {
1333                 struct kvm_io_device *pos = bus->devs[i];
1334
1335                 kvm_iodevice_destructor(pos);
1336         }
1337 }
1338
1339 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1340 {
1341         int i;
1342
1343         for (i = 0; i < bus->dev_count; i++) {
1344                 struct kvm_io_device *pos = bus->devs[i];
1345
1346                 if (pos->in_range(pos, addr))
1347                         return pos;
1348         }
1349
1350         return NULL;
1351 }
1352
1353 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1354 {
1355         BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1356
1357         bus->devs[bus->dev_count++] = dev;
1358 }
1359
1360 static struct notifier_block kvm_cpu_notifier = {
1361         .notifier_call = kvm_cpu_hotplug,
1362         .priority = 20, /* must be > scheduler priority */
1363 };
1364
1365 static int vm_stat_get(void *_offset, u64 *val)
1366 {
1367         unsigned offset = (long)_offset;
1368         struct kvm *kvm;
1369
1370         *val = 0;
1371         spin_lock(&kvm_lock);
1372         list_for_each_entry(kvm, &vm_list, vm_list)
1373                 *val += *(u32 *)((void *)kvm + offset);
1374         spin_unlock(&kvm_lock);
1375         return 0;
1376 }
1377
1378 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1379
1380 static int vcpu_stat_get(void *_offset, u64 *val)
1381 {
1382         unsigned offset = (long)_offset;
1383         struct kvm *kvm;
1384         struct kvm_vcpu *vcpu;
1385         int i;
1386
1387         *val = 0;
1388         spin_lock(&kvm_lock);
1389         list_for_each_entry(kvm, &vm_list, vm_list)
1390                 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1391                         vcpu = kvm->vcpus[i];
1392                         if (vcpu)
1393                                 *val += *(u32 *)((void *)vcpu + offset);
1394                 }
1395         spin_unlock(&kvm_lock);
1396         return 0;
1397 }
1398
1399 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1400
1401 static struct file_operations *stat_fops[] = {
1402         [KVM_STAT_VCPU] = &vcpu_stat_fops,
1403         [KVM_STAT_VM]   = &vm_stat_fops,
1404 };
1405
1406 static void kvm_init_debug(void)
1407 {
1408         struct kvm_stats_debugfs_item *p;
1409
1410         kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
1411         for (p = debugfs_entries; p->name; ++p)
1412                 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1413                                                 (void *)(long)p->offset,
1414                                                 stat_fops[p->kind]);
1415 }
1416
1417 static void kvm_exit_debug(void)
1418 {
1419         struct kvm_stats_debugfs_item *p;
1420
1421         for (p = debugfs_entries; p->name; ++p)
1422                 debugfs_remove(p->dentry);
1423         debugfs_remove(kvm_debugfs_dir);
1424 }
1425
1426 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1427 {
1428         hardware_disable(NULL);
1429         return 0;
1430 }
1431
1432 static int kvm_resume(struct sys_device *dev)
1433 {
1434         hardware_enable(NULL);
1435         return 0;
1436 }
1437
1438 static struct sysdev_class kvm_sysdev_class = {
1439         .name = "kvm",
1440         .suspend = kvm_suspend,
1441         .resume = kvm_resume,
1442 };
1443
1444 static struct sys_device kvm_sysdev = {
1445         .id = 0,
1446         .cls = &kvm_sysdev_class,
1447 };
1448
1449 struct page *bad_page;
1450 pfn_t bad_pfn;
1451
1452 static inline
1453 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1454 {
1455         return container_of(pn, struct kvm_vcpu, preempt_notifier);
1456 }
1457
1458 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1459 {
1460         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1461
1462         kvm_arch_vcpu_load(vcpu, cpu);
1463 }
1464
1465 static void kvm_sched_out(struct preempt_notifier *pn,
1466                           struct task_struct *next)
1467 {
1468         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1469
1470         kvm_arch_vcpu_put(vcpu);
1471 }
1472
1473 int kvm_init(void *opaque, unsigned int vcpu_size,
1474                   struct module *module)
1475 {
1476         int r;
1477         int cpu;
1478
1479         kvm_init_debug();
1480
1481         r = kvm_arch_init(opaque);
1482         if (r)
1483                 goto out_fail;
1484
1485         bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1486
1487         if (bad_page == NULL) {
1488                 r = -ENOMEM;
1489                 goto out;
1490         }
1491
1492         bad_pfn = page_to_pfn(bad_page);
1493
1494         r = kvm_arch_hardware_setup();
1495         if (r < 0)
1496                 goto out_free_0;
1497
1498         for_each_online_cpu(cpu) {
1499                 smp_call_function_single(cpu,
1500                                 kvm_arch_check_processor_compat,
1501                                 &r, 1);
1502                 if (r < 0)
1503                         goto out_free_1;
1504         }
1505
1506         on_each_cpu(hardware_enable, NULL, 1);
1507         r = register_cpu_notifier(&kvm_cpu_notifier);
1508         if (r)
1509                 goto out_free_2;
1510         register_reboot_notifier(&kvm_reboot_notifier);
1511
1512         r = sysdev_class_register(&kvm_sysdev_class);
1513         if (r)
1514                 goto out_free_3;
1515
1516         r = sysdev_register(&kvm_sysdev);
1517         if (r)
1518                 goto out_free_4;
1519
1520         /* A kmem cache lets us meet the alignment requirements of fx_save. */
1521         kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
1522                                            __alignof__(struct kvm_vcpu),
1523                                            0, NULL);
1524         if (!kvm_vcpu_cache) {
1525                 r = -ENOMEM;
1526                 goto out_free_5;
1527         }
1528
1529         kvm_chardev_ops.owner = module;
1530
1531         r = misc_register(&kvm_dev);
1532         if (r) {
1533                 printk(KERN_ERR "kvm: misc device register failed\n");
1534                 goto out_free;
1535         }
1536
1537         kvm_preempt_ops.sched_in = kvm_sched_in;
1538         kvm_preempt_ops.sched_out = kvm_sched_out;
1539
1540         return 0;
1541
1542 out_free:
1543         kmem_cache_destroy(kvm_vcpu_cache);
1544 out_free_5:
1545         sysdev_unregister(&kvm_sysdev);
1546 out_free_4:
1547         sysdev_class_unregister(&kvm_sysdev_class);
1548 out_free_3:
1549         unregister_reboot_notifier(&kvm_reboot_notifier);
1550         unregister_cpu_notifier(&kvm_cpu_notifier);
1551 out_free_2:
1552         on_each_cpu(hardware_disable, NULL, 1);
1553 out_free_1:
1554         kvm_arch_hardware_unsetup();
1555 out_free_0:
1556         __free_page(bad_page);
1557 out:
1558         kvm_arch_exit();
1559         kvm_exit_debug();
1560 out_fail:
1561         return r;
1562 }
1563 EXPORT_SYMBOL_GPL(kvm_init);
1564
1565 void kvm_exit(void)
1566 {
1567         kvm_trace_cleanup();
1568         misc_deregister(&kvm_dev);
1569         kmem_cache_destroy(kvm_vcpu_cache);
1570         sysdev_unregister(&kvm_sysdev);
1571         sysdev_class_unregister(&kvm_sysdev_class);
1572         unregister_reboot_notifier(&kvm_reboot_notifier);
1573         unregister_cpu_notifier(&kvm_cpu_notifier);
1574         on_each_cpu(hardware_disable, NULL, 1);
1575         kvm_arch_hardware_unsetup();
1576         kvm_arch_exit();
1577         kvm_exit_debug();
1578         __free_page(bad_page);
1579 }
1580 EXPORT_SYMBOL_GPL(kvm_exit);