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