PAGEFLAGS_EXTENDED and separate page flags for Head and Tail
[linux-2.6/openmoko-kernel/knife-kernel.git] / virt / kvm / kvm_main.c
blobc82cf15730a1e85c751483f75944e8905c0baa10
1 /*
2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
18 #include "iodev.h"
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>
45 #include <asm/processor.h>
46 #include <asm/io.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
50 MODULE_AUTHOR("Qumranet");
51 MODULE_LICENSE("GPL");
53 DEFINE_SPINLOCK(kvm_lock);
54 LIST_HEAD(vm_list);
56 static cpumask_t cpus_hardware_enabled;
58 struct kmem_cache *kvm_vcpu_cache;
59 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
61 static __read_mostly struct preempt_ops kvm_preempt_ops;
63 struct dentry *kvm_debugfs_dir;
65 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
66 unsigned long arg);
68 static inline int valid_vcpu(int n)
70 return likely(n >= 0 && n < KVM_MAX_VCPUS);
74 * Switches to specified vcpu, until a matching vcpu_put()
76 void vcpu_load(struct kvm_vcpu *vcpu)
78 int cpu;
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();
87 void vcpu_put(struct kvm_vcpu *vcpu)
89 preempt_disable();
90 kvm_arch_vcpu_put(vcpu);
91 preempt_notifier_unregister(&vcpu->preempt_notifier);
92 preempt_enable();
93 mutex_unlock(&vcpu->mutex);
96 static void ack_flush(void *_completed)
100 void kvm_flush_remote_tlbs(struct kvm *kvm)
102 int i, cpu;
103 cpumask_t cpus;
104 struct kvm_vcpu *vcpu;
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);
117 if (cpus_empty(cpus))
118 return;
119 ++kvm->stat.remote_tlb_flush;
120 smp_call_function_mask(cpus, ack_flush, NULL, 1);
123 void kvm_reload_remote_mmus(struct kvm *kvm)
125 int i, cpu;
126 cpumask_t cpus;
127 struct kvm_vcpu *vcpu;
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);
140 if (cpus_empty(cpus))
141 return;
142 smp_call_function_mask(cpus, ack_flush, NULL, 1);
146 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
148 struct page *page;
149 int r;
151 mutex_init(&vcpu->mutex);
152 vcpu->cpu = -1;
153 vcpu->kvm = kvm;
154 vcpu->vcpu_id = id;
155 init_waitqueue_head(&vcpu->wq);
157 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
158 if (!page) {
159 r = -ENOMEM;
160 goto fail;
162 vcpu->run = page_address(page);
164 r = kvm_arch_vcpu_init(vcpu);
165 if (r < 0)
166 goto fail_free_run;
167 return 0;
169 fail_free_run:
170 free_page((unsigned long)vcpu->run);
171 fail:
172 return r;
174 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
176 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
178 kvm_arch_vcpu_uninit(vcpu);
179 free_page((unsigned long)vcpu->run);
181 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
183 static struct kvm *kvm_create_vm(void)
185 struct kvm *kvm = kvm_arch_create_vm();
187 if (IS_ERR(kvm))
188 goto out;
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;
206 * Free any memory in @free but not in @dont.
208 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
209 struct kvm_memory_slot *dont)
211 if (!dont || free->rmap != dont->rmap)
212 vfree(free->rmap);
214 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
215 vfree(free->dirty_bitmap);
217 if (!dont || free->lpage_info != dont->lpage_info)
218 vfree(free->lpage_info);
220 free->npages = 0;
221 free->dirty_bitmap = NULL;
222 free->rmap = NULL;
223 free->lpage_info = NULL;
226 void kvm_free_physmem(struct kvm *kvm)
228 int i;
230 for (i = 0; i < kvm->nmemslots; ++i)
231 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
234 static void kvm_destroy_vm(struct kvm *kvm)
236 struct mm_struct *mm = kvm->mm;
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);
247 void kvm_get_kvm(struct kvm *kvm)
249 atomic_inc(&kvm->users_count);
251 EXPORT_SYMBOL_GPL(kvm_get_kvm);
253 void kvm_put_kvm(struct kvm *kvm)
255 if (atomic_dec_and_test(&kvm->users_count))
256 kvm_destroy_vm(kvm);
258 EXPORT_SYMBOL_GPL(kvm_put_kvm);
261 static int kvm_vm_release(struct inode *inode, struct file *filp)
263 struct kvm *kvm = filp->private_data;
265 kvm_put_kvm(kvm);
266 return 0;
270 * Allocate some memory and give it an address in the guest physical address
271 * space.
273 * Discontiguous memory is allowed, mostly for framebuffers.
275 * Must be called holding mmap_sem for write.
277 int __kvm_set_memory_region(struct kvm *kvm,
278 struct kvm_userspace_memory_region *mem,
279 int user_alloc)
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;
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;
299 memslot = &kvm->memslots[mem->slot];
300 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
301 npages = mem->memory_size >> PAGE_SHIFT;
303 if (!npages)
304 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
306 new = old = *memslot;
308 new.base_gfn = base_gfn;
309 new.npages = npages;
310 new.flags = mem->flags;
312 /* Disallow changing a memory slot's size. */
313 r = -EINVAL;
314 if (npages && old.npages && npages != old.npages)
315 goto out_free;
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];
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;
329 /* Free page dirty bitmap if unneeded */
330 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
331 new.dirty_bitmap = NULL;
333 r = -ENOMEM;
335 /* Allocate if a slot is being created */
336 if (npages && !new.rmap) {
337 new.rmap = vmalloc(npages * sizeof(struct page *));
339 if (!new.rmap)
340 goto out_free;
342 memset(new.rmap, 0, npages * sizeof(*new.rmap));
344 new.user_alloc = user_alloc;
345 new.userspace_addr = mem->userspace_addr;
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++;
354 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
356 if (!new.lpage_info)
357 goto out_free;
359 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
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;
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;
371 new.dirty_bitmap = vmalloc(dirty_bytes);
372 if (!new.dirty_bitmap)
373 goto out_free;
374 memset(new.dirty_bitmap, 0, dirty_bytes);
377 if (mem->slot >= kvm->nmemslots)
378 kvm->nmemslots = mem->slot + 1;
380 *memslot = new;
382 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
383 if (r) {
384 *memslot = old;
385 goto out_free;
388 kvm_free_physmem_slot(&old, &new);
389 return 0;
391 out_free:
392 kvm_free_physmem_slot(&new, &old);
393 out:
394 return r;
397 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
399 int kvm_set_memory_region(struct kvm *kvm,
400 struct kvm_userspace_memory_region *mem,
401 int user_alloc)
403 int r;
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;
410 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
412 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
413 struct
414 kvm_userspace_memory_region *mem,
415 int user_alloc)
417 if (mem->slot >= KVM_MEMORY_SLOTS)
418 return -EINVAL;
419 return kvm_set_memory_region(kvm, mem, user_alloc);
422 int kvm_get_dirty_log(struct kvm *kvm,
423 struct kvm_dirty_log *log, int *is_dirty)
425 struct kvm_memory_slot *memslot;
426 int r, i;
427 int n;
428 unsigned long any = 0;
430 r = -EINVAL;
431 if (log->slot >= KVM_MEMORY_SLOTS)
432 goto out;
434 memslot = &kvm->memslots[log->slot];
435 r = -ENOENT;
436 if (!memslot->dirty_bitmap)
437 goto out;
439 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
441 for (i = 0; !any && i < n/sizeof(long); ++i)
442 any = memslot->dirty_bitmap[i];
444 r = -EFAULT;
445 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
446 goto out;
448 if (any)
449 *is_dirty = 1;
451 r = 0;
452 out:
453 return r;
456 int is_error_page(struct page *page)
458 return page == bad_page;
460 EXPORT_SYMBOL_GPL(is_error_page);
462 int is_error_pfn(pfn_t pfn)
464 return pfn == bad_pfn;
466 EXPORT_SYMBOL_GPL(is_error_pfn);
468 static inline unsigned long bad_hva(void)
470 return PAGE_OFFSET;
473 int kvm_is_error_hva(unsigned long addr)
475 return addr == bad_hva();
477 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
479 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
481 int i;
483 for (i = 0; i < kvm->nmemslots; ++i) {
484 struct kvm_memory_slot *memslot = &kvm->memslots[i];
486 if (gfn >= memslot->base_gfn
487 && gfn < memslot->base_gfn + memslot->npages)
488 return memslot;
490 return NULL;
493 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
495 gfn = unalias_gfn(kvm, gfn);
496 return __gfn_to_memslot(kvm, gfn);
499 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
501 int i;
503 gfn = unalias_gfn(kvm, gfn);
504 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
505 struct kvm_memory_slot *memslot = &kvm->memslots[i];
507 if (gfn >= memslot->base_gfn
508 && gfn < memslot->base_gfn + memslot->npages)
509 return 1;
511 return 0;
513 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
515 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
517 struct kvm_memory_slot *slot;
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);
527 * Requires current->mm->mmap_sem to be held
529 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
531 struct page *page[1];
532 unsigned long addr;
533 int npages;
535 might_sleep();
537 addr = gfn_to_hva(kvm, gfn);
538 if (kvm_is_error_hva(addr)) {
539 get_page(bad_page);
540 return page_to_pfn(bad_page);
543 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
544 NULL);
546 if (npages != 1) {
547 get_page(bad_page);
548 return page_to_pfn(bad_page);
551 return page_to_pfn(page[0]);
554 EXPORT_SYMBOL_GPL(gfn_to_pfn);
556 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
558 return pfn_to_page(gfn_to_pfn(kvm, gfn));
561 EXPORT_SYMBOL_GPL(gfn_to_page);
563 void kvm_release_page_clean(struct page *page)
565 kvm_release_pfn_clean(page_to_pfn(page));
567 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
569 void kvm_release_pfn_clean(pfn_t pfn)
571 put_page(pfn_to_page(pfn));
573 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
575 void kvm_release_page_dirty(struct page *page)
577 kvm_release_pfn_dirty(page_to_pfn(page));
579 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
581 void kvm_release_pfn_dirty(pfn_t pfn)
583 kvm_set_pfn_dirty(pfn);
584 kvm_release_pfn_clean(pfn);
586 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
588 void kvm_set_page_dirty(struct page *page)
590 kvm_set_pfn_dirty(page_to_pfn(page));
592 EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
594 void kvm_set_pfn_dirty(pfn_t pfn)
596 struct page *page = pfn_to_page(pfn);
597 if (!PageReserved(page))
598 SetPageDirty(page);
600 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
602 void kvm_set_pfn_accessed(pfn_t pfn)
604 mark_page_accessed(pfn_to_page(pfn));
606 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
608 void kvm_get_pfn(pfn_t pfn)
610 get_page(pfn_to_page(pfn));
612 EXPORT_SYMBOL_GPL(kvm_get_pfn);
614 static int next_segment(unsigned long len, int offset)
616 if (len > PAGE_SIZE - offset)
617 return PAGE_SIZE - offset;
618 else
619 return len;
622 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
623 int len)
625 int r;
626 unsigned long addr;
628 addr = gfn_to_hva(kvm, gfn);
629 if (kvm_is_error_hva(addr))
630 return -EFAULT;
631 r = copy_from_user(data, (void __user *)addr + offset, len);
632 if (r)
633 return -EFAULT;
634 return 0;
636 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
638 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
640 gfn_t gfn = gpa >> PAGE_SHIFT;
641 int seg;
642 int offset = offset_in_page(gpa);
643 int ret;
645 while ((seg = next_segment(len, offset)) != 0) {
646 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
647 if (ret < 0)
648 return ret;
649 offset = 0;
650 len -= seg;
651 data += seg;
652 ++gfn;
654 return 0;
656 EXPORT_SYMBOL_GPL(kvm_read_guest);
658 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
659 unsigned long len)
661 int r;
662 unsigned long addr;
663 gfn_t gfn = gpa >> PAGE_SHIFT;
664 int offset = offset_in_page(gpa);
666 addr = gfn_to_hva(kvm, gfn);
667 if (kvm_is_error_hva(addr))
668 return -EFAULT;
669 pagefault_disable();
670 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
671 pagefault_enable();
672 if (r)
673 return -EFAULT;
674 return 0;
676 EXPORT_SYMBOL(kvm_read_guest_atomic);
678 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
679 int offset, int len)
681 int r;
682 unsigned long addr;
684 addr = gfn_to_hva(kvm, gfn);
685 if (kvm_is_error_hva(addr))
686 return -EFAULT;
687 r = copy_to_user((void __user *)addr + offset, data, len);
688 if (r)
689 return -EFAULT;
690 mark_page_dirty(kvm, gfn);
691 return 0;
693 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
695 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
696 unsigned long len)
698 gfn_t gfn = gpa >> PAGE_SHIFT;
699 int seg;
700 int offset = offset_in_page(gpa);
701 int ret;
703 while ((seg = next_segment(len, offset)) != 0) {
704 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
705 if (ret < 0)
706 return ret;
707 offset = 0;
708 len -= seg;
709 data += seg;
710 ++gfn;
712 return 0;
715 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
717 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
719 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
721 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
723 gfn_t gfn = gpa >> PAGE_SHIFT;
724 int seg;
725 int offset = offset_in_page(gpa);
726 int ret;
728 while ((seg = next_segment(len, offset)) != 0) {
729 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
730 if (ret < 0)
731 return ret;
732 offset = 0;
733 len -= seg;
734 ++gfn;
736 return 0;
738 EXPORT_SYMBOL_GPL(kvm_clear_guest);
740 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
742 struct kvm_memory_slot *memslot;
744 gfn = unalias_gfn(kvm, gfn);
745 memslot = __gfn_to_memslot(kvm, gfn);
746 if (memslot && memslot->dirty_bitmap) {
747 unsigned long rel_gfn = gfn - memslot->base_gfn;
749 /* avoid RMW */
750 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
751 set_bit(rel_gfn, memslot->dirty_bitmap);
756 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
758 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
760 DECLARE_WAITQUEUE(wait, current);
762 add_wait_queue(&vcpu->wq, &wait);
765 * We will block until either an interrupt or a signal wakes us up
767 while (!kvm_cpu_has_interrupt(vcpu)
768 && !kvm_cpu_has_pending_timer(vcpu)
769 && !signal_pending(current)
770 && !kvm_arch_vcpu_runnable(vcpu)) {
771 set_current_state(TASK_INTERRUPTIBLE);
772 vcpu_put(vcpu);
773 schedule();
774 vcpu_load(vcpu);
777 __set_current_state(TASK_RUNNING);
778 remove_wait_queue(&vcpu->wq, &wait);
781 void kvm_resched(struct kvm_vcpu *vcpu)
783 if (!need_resched())
784 return;
785 cond_resched();
787 EXPORT_SYMBOL_GPL(kvm_resched);
789 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
791 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
792 struct page *page;
794 if (vmf->pgoff == 0)
795 page = virt_to_page(vcpu->run);
796 #ifdef CONFIG_X86
797 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
798 page = virt_to_page(vcpu->arch.pio_data);
799 #endif
800 else
801 return VM_FAULT_SIGBUS;
802 get_page(page);
803 vmf->page = page;
804 return 0;
807 static struct vm_operations_struct kvm_vcpu_vm_ops = {
808 .fault = kvm_vcpu_fault,
811 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
813 vma->vm_ops = &kvm_vcpu_vm_ops;
814 return 0;
817 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
819 struct kvm_vcpu *vcpu = filp->private_data;
821 kvm_put_kvm(vcpu->kvm);
822 return 0;
825 static const struct file_operations kvm_vcpu_fops = {
826 .release = kvm_vcpu_release,
827 .unlocked_ioctl = kvm_vcpu_ioctl,
828 .compat_ioctl = kvm_vcpu_ioctl,
829 .mmap = kvm_vcpu_mmap,
833 * Allocates an inode for the vcpu.
835 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
837 int fd, r;
838 struct inode *inode;
839 struct file *file;
841 r = anon_inode_getfd(&fd, &inode, &file,
842 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
843 if (r) {
844 kvm_put_kvm(vcpu->kvm);
845 return r;
847 return fd;
851 * Creates some virtual cpus. Good luck creating more than one.
853 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
855 int r;
856 struct kvm_vcpu *vcpu;
858 if (!valid_vcpu(n))
859 return -EINVAL;
861 vcpu = kvm_arch_vcpu_create(kvm, n);
862 if (IS_ERR(vcpu))
863 return PTR_ERR(vcpu);
865 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
867 r = kvm_arch_vcpu_setup(vcpu);
868 if (r)
869 goto vcpu_destroy;
871 mutex_lock(&kvm->lock);
872 if (kvm->vcpus[n]) {
873 r = -EEXIST;
874 mutex_unlock(&kvm->lock);
875 goto vcpu_destroy;
877 kvm->vcpus[n] = vcpu;
878 mutex_unlock(&kvm->lock);
880 /* Now it's all set up, let userspace reach it */
881 kvm_get_kvm(kvm);
882 r = create_vcpu_fd(vcpu);
883 if (r < 0)
884 goto unlink;
885 return r;
887 unlink:
888 mutex_lock(&kvm->lock);
889 kvm->vcpus[n] = NULL;
890 mutex_unlock(&kvm->lock);
891 vcpu_destroy:
892 kvm_arch_vcpu_destroy(vcpu);
893 return r;
896 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
898 if (sigset) {
899 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
900 vcpu->sigset_active = 1;
901 vcpu->sigset = *sigset;
902 } else
903 vcpu->sigset_active = 0;
904 return 0;
907 static long kvm_vcpu_ioctl(struct file *filp,
908 unsigned int ioctl, unsigned long arg)
910 struct kvm_vcpu *vcpu = filp->private_data;
911 void __user *argp = (void __user *)arg;
912 int r;
914 if (vcpu->kvm->mm != current->mm)
915 return -EIO;
916 switch (ioctl) {
917 case KVM_RUN:
918 r = -EINVAL;
919 if (arg)
920 goto out;
921 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
922 break;
923 case KVM_GET_REGS: {
924 struct kvm_regs *kvm_regs;
926 r = -ENOMEM;
927 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
928 if (!kvm_regs)
929 goto out;
930 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
931 if (r)
932 goto out_free1;
933 r = -EFAULT;
934 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
935 goto out_free1;
936 r = 0;
937 out_free1:
938 kfree(kvm_regs);
939 break;
941 case KVM_SET_REGS: {
942 struct kvm_regs *kvm_regs;
944 r = -ENOMEM;
945 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
946 if (!kvm_regs)
947 goto out;
948 r = -EFAULT;
949 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
950 goto out_free2;
951 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
952 if (r)
953 goto out_free2;
954 r = 0;
955 out_free2:
956 kfree(kvm_regs);
957 break;
959 case KVM_GET_SREGS: {
960 struct kvm_sregs kvm_sregs;
962 memset(&kvm_sregs, 0, sizeof kvm_sregs);
963 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
964 if (r)
965 goto out;
966 r = -EFAULT;
967 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
968 goto out;
969 r = 0;
970 break;
972 case KVM_SET_SREGS: {
973 struct kvm_sregs kvm_sregs;
975 r = -EFAULT;
976 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
977 goto out;
978 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
979 if (r)
980 goto out;
981 r = 0;
982 break;
984 case KVM_GET_MP_STATE: {
985 struct kvm_mp_state mp_state;
987 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
988 if (r)
989 goto out;
990 r = -EFAULT;
991 if (copy_to_user(argp, &mp_state, sizeof mp_state))
992 goto out;
993 r = 0;
994 break;
996 case KVM_SET_MP_STATE: {
997 struct kvm_mp_state mp_state;
999 r = -EFAULT;
1000 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1001 goto out;
1002 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1003 if (r)
1004 goto out;
1005 r = 0;
1006 break;
1008 case KVM_TRANSLATE: {
1009 struct kvm_translation tr;
1011 r = -EFAULT;
1012 if (copy_from_user(&tr, argp, sizeof tr))
1013 goto out;
1014 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1015 if (r)
1016 goto out;
1017 r = -EFAULT;
1018 if (copy_to_user(argp, &tr, sizeof tr))
1019 goto out;
1020 r = 0;
1021 break;
1023 case KVM_DEBUG_GUEST: {
1024 struct kvm_debug_guest dbg;
1026 r = -EFAULT;
1027 if (copy_from_user(&dbg, argp, sizeof dbg))
1028 goto out;
1029 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1030 if (r)
1031 goto out;
1032 r = 0;
1033 break;
1035 case KVM_SET_SIGNAL_MASK: {
1036 struct kvm_signal_mask __user *sigmask_arg = argp;
1037 struct kvm_signal_mask kvm_sigmask;
1038 sigset_t sigset, *p;
1040 p = NULL;
1041 if (argp) {
1042 r = -EFAULT;
1043 if (copy_from_user(&kvm_sigmask, argp,
1044 sizeof kvm_sigmask))
1045 goto out;
1046 r = -EINVAL;
1047 if (kvm_sigmask.len != sizeof sigset)
1048 goto out;
1049 r = -EFAULT;
1050 if (copy_from_user(&sigset, sigmask_arg->sigset,
1051 sizeof sigset))
1052 goto out;
1053 p = &sigset;
1055 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1056 break;
1058 case KVM_GET_FPU: {
1059 struct kvm_fpu fpu;
1061 memset(&fpu, 0, sizeof fpu);
1062 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
1063 if (r)
1064 goto out;
1065 r = -EFAULT;
1066 if (copy_to_user(argp, &fpu, sizeof fpu))
1067 goto out;
1068 r = 0;
1069 break;
1071 case KVM_SET_FPU: {
1072 struct kvm_fpu fpu;
1074 r = -EFAULT;
1075 if (copy_from_user(&fpu, argp, sizeof fpu))
1076 goto out;
1077 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
1078 if (r)
1079 goto out;
1080 r = 0;
1081 break;
1083 default:
1084 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1086 out:
1087 return r;
1090 static long kvm_vm_ioctl(struct file *filp,
1091 unsigned int ioctl, unsigned long arg)
1093 struct kvm *kvm = filp->private_data;
1094 void __user *argp = (void __user *)arg;
1095 int r;
1097 if (kvm->mm != current->mm)
1098 return -EIO;
1099 switch (ioctl) {
1100 case KVM_CREATE_VCPU:
1101 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1102 if (r < 0)
1103 goto out;
1104 break;
1105 case KVM_SET_USER_MEMORY_REGION: {
1106 struct kvm_userspace_memory_region kvm_userspace_mem;
1108 r = -EFAULT;
1109 if (copy_from_user(&kvm_userspace_mem, argp,
1110 sizeof kvm_userspace_mem))
1111 goto out;
1113 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1114 if (r)
1115 goto out;
1116 break;
1118 case KVM_GET_DIRTY_LOG: {
1119 struct kvm_dirty_log log;
1121 r = -EFAULT;
1122 if (copy_from_user(&log, argp, sizeof log))
1123 goto out;
1124 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1125 if (r)
1126 goto out;
1127 break;
1129 default:
1130 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1132 out:
1133 return r;
1136 static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1138 struct kvm *kvm = vma->vm_file->private_data;
1139 struct page *page;
1141 if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
1142 return VM_FAULT_SIGBUS;
1143 page = gfn_to_page(kvm, vmf->pgoff);
1144 if (is_error_page(page)) {
1145 kvm_release_page_clean(page);
1146 return VM_FAULT_SIGBUS;
1148 vmf->page = page;
1149 return 0;
1152 static struct vm_operations_struct kvm_vm_vm_ops = {
1153 .fault = kvm_vm_fault,
1156 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1158 vma->vm_ops = &kvm_vm_vm_ops;
1159 return 0;
1162 static const struct file_operations kvm_vm_fops = {
1163 .release = kvm_vm_release,
1164 .unlocked_ioctl = kvm_vm_ioctl,
1165 .compat_ioctl = kvm_vm_ioctl,
1166 .mmap = kvm_vm_mmap,
1169 static int kvm_dev_ioctl_create_vm(void)
1171 int fd, r;
1172 struct inode *inode;
1173 struct file *file;
1174 struct kvm *kvm;
1176 kvm = kvm_create_vm();
1177 if (IS_ERR(kvm))
1178 return PTR_ERR(kvm);
1179 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1180 if (r) {
1181 kvm_put_kvm(kvm);
1182 return r;
1185 return fd;
1188 static long kvm_dev_ioctl(struct file *filp,
1189 unsigned int ioctl, unsigned long arg)
1191 void __user *argp = (void __user *)arg;
1192 long r = -EINVAL;
1194 switch (ioctl) {
1195 case KVM_GET_API_VERSION:
1196 r = -EINVAL;
1197 if (arg)
1198 goto out;
1199 r = KVM_API_VERSION;
1200 break;
1201 case KVM_CREATE_VM:
1202 r = -EINVAL;
1203 if (arg)
1204 goto out;
1205 r = kvm_dev_ioctl_create_vm();
1206 break;
1207 case KVM_CHECK_EXTENSION:
1208 r = kvm_dev_ioctl_check_extension((long)argp);
1209 break;
1210 case KVM_GET_VCPU_MMAP_SIZE:
1211 r = -EINVAL;
1212 if (arg)
1213 goto out;
1214 r = PAGE_SIZE; /* struct kvm_run */
1215 #ifdef CONFIG_X86
1216 r += PAGE_SIZE; /* pio data page */
1217 #endif
1218 break;
1219 case KVM_TRACE_ENABLE:
1220 case KVM_TRACE_PAUSE:
1221 case KVM_TRACE_DISABLE:
1222 r = kvm_trace_ioctl(ioctl, arg);
1223 break;
1224 default:
1225 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1227 out:
1228 return r;
1231 static struct file_operations kvm_chardev_ops = {
1232 .unlocked_ioctl = kvm_dev_ioctl,
1233 .compat_ioctl = kvm_dev_ioctl,
1236 static struct miscdevice kvm_dev = {
1237 KVM_MINOR,
1238 "kvm",
1239 &kvm_chardev_ops,
1242 static void hardware_enable(void *junk)
1244 int cpu = raw_smp_processor_id();
1246 if (cpu_isset(cpu, cpus_hardware_enabled))
1247 return;
1248 cpu_set(cpu, cpus_hardware_enabled);
1249 kvm_arch_hardware_enable(NULL);
1252 static void hardware_disable(void *junk)
1254 int cpu = raw_smp_processor_id();
1256 if (!cpu_isset(cpu, cpus_hardware_enabled))
1257 return;
1258 cpu_clear(cpu, cpus_hardware_enabled);
1259 decache_vcpus_on_cpu(cpu);
1260 kvm_arch_hardware_disable(NULL);
1263 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1264 void *v)
1266 int cpu = (long)v;
1268 val &= ~CPU_TASKS_FROZEN;
1269 switch (val) {
1270 case CPU_DYING:
1271 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1272 cpu);
1273 hardware_disable(NULL);
1274 break;
1275 case CPU_UP_CANCELED:
1276 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1277 cpu);
1278 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
1279 break;
1280 case CPU_ONLINE:
1281 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1282 cpu);
1283 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
1284 break;
1286 return NOTIFY_OK;
1289 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1290 void *v)
1292 if (val == SYS_RESTART) {
1294 * Some (well, at least mine) BIOSes hang on reboot if
1295 * in vmx root mode.
1297 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1298 on_each_cpu(hardware_disable, NULL, 0, 1);
1300 return NOTIFY_OK;
1303 static struct notifier_block kvm_reboot_notifier = {
1304 .notifier_call = kvm_reboot,
1305 .priority = 0,
1308 void kvm_io_bus_init(struct kvm_io_bus *bus)
1310 memset(bus, 0, sizeof(*bus));
1313 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1315 int i;
1317 for (i = 0; i < bus->dev_count; i++) {
1318 struct kvm_io_device *pos = bus->devs[i];
1320 kvm_iodevice_destructor(pos);
1324 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1326 int i;
1328 for (i = 0; i < bus->dev_count; i++) {
1329 struct kvm_io_device *pos = bus->devs[i];
1331 if (pos->in_range(pos, addr))
1332 return pos;
1335 return NULL;
1338 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1340 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1342 bus->devs[bus->dev_count++] = dev;
1345 static struct notifier_block kvm_cpu_notifier = {
1346 .notifier_call = kvm_cpu_hotplug,
1347 .priority = 20, /* must be > scheduler priority */
1350 static int vm_stat_get(void *_offset, u64 *val)
1352 unsigned offset = (long)_offset;
1353 struct kvm *kvm;
1355 *val = 0;
1356 spin_lock(&kvm_lock);
1357 list_for_each_entry(kvm, &vm_list, vm_list)
1358 *val += *(u32 *)((void *)kvm + offset);
1359 spin_unlock(&kvm_lock);
1360 return 0;
1363 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1365 static int vcpu_stat_get(void *_offset, u64 *val)
1367 unsigned offset = (long)_offset;
1368 struct kvm *kvm;
1369 struct kvm_vcpu *vcpu;
1370 int i;
1372 *val = 0;
1373 spin_lock(&kvm_lock);
1374 list_for_each_entry(kvm, &vm_list, vm_list)
1375 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1376 vcpu = kvm->vcpus[i];
1377 if (vcpu)
1378 *val += *(u32 *)((void *)vcpu + offset);
1380 spin_unlock(&kvm_lock);
1381 return 0;
1384 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1386 static struct file_operations *stat_fops[] = {
1387 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1388 [KVM_STAT_VM] = &vm_stat_fops,
1391 static void kvm_init_debug(void)
1393 struct kvm_stats_debugfs_item *p;
1395 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
1396 for (p = debugfs_entries; p->name; ++p)
1397 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
1398 (void *)(long)p->offset,
1399 stat_fops[p->kind]);
1402 static void kvm_exit_debug(void)
1404 struct kvm_stats_debugfs_item *p;
1406 for (p = debugfs_entries; p->name; ++p)
1407 debugfs_remove(p->dentry);
1408 debugfs_remove(kvm_debugfs_dir);
1411 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1413 hardware_disable(NULL);
1414 return 0;
1417 static int kvm_resume(struct sys_device *dev)
1419 hardware_enable(NULL);
1420 return 0;
1423 static struct sysdev_class kvm_sysdev_class = {
1424 .name = "kvm",
1425 .suspend = kvm_suspend,
1426 .resume = kvm_resume,
1429 static struct sys_device kvm_sysdev = {
1430 .id = 0,
1431 .cls = &kvm_sysdev_class,
1434 struct page *bad_page;
1435 pfn_t bad_pfn;
1437 static inline
1438 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1440 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1443 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1445 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1447 kvm_arch_vcpu_load(vcpu, cpu);
1450 static void kvm_sched_out(struct preempt_notifier *pn,
1451 struct task_struct *next)
1453 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1455 kvm_arch_vcpu_put(vcpu);
1458 int kvm_init(void *opaque, unsigned int vcpu_size,
1459 struct module *module)
1461 int r;
1462 int cpu;
1464 kvm_init_debug();
1466 r = kvm_arch_init(opaque);
1467 if (r)
1468 goto out_fail;
1470 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1472 if (bad_page == NULL) {
1473 r = -ENOMEM;
1474 goto out;
1477 bad_pfn = page_to_pfn(bad_page);
1479 r = kvm_arch_hardware_setup();
1480 if (r < 0)
1481 goto out_free_0;
1483 for_each_online_cpu(cpu) {
1484 smp_call_function_single(cpu,
1485 kvm_arch_check_processor_compat,
1486 &r, 0, 1);
1487 if (r < 0)
1488 goto out_free_1;
1491 on_each_cpu(hardware_enable, NULL, 0, 1);
1492 r = register_cpu_notifier(&kvm_cpu_notifier);
1493 if (r)
1494 goto out_free_2;
1495 register_reboot_notifier(&kvm_reboot_notifier);
1497 r = sysdev_class_register(&kvm_sysdev_class);
1498 if (r)
1499 goto out_free_3;
1501 r = sysdev_register(&kvm_sysdev);
1502 if (r)
1503 goto out_free_4;
1505 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1506 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
1507 __alignof__(struct kvm_vcpu),
1508 0, NULL);
1509 if (!kvm_vcpu_cache) {
1510 r = -ENOMEM;
1511 goto out_free_5;
1514 kvm_chardev_ops.owner = module;
1516 r = misc_register(&kvm_dev);
1517 if (r) {
1518 printk(KERN_ERR "kvm: misc device register failed\n");
1519 goto out_free;
1522 kvm_preempt_ops.sched_in = kvm_sched_in;
1523 kvm_preempt_ops.sched_out = kvm_sched_out;
1525 return 0;
1527 out_free:
1528 kmem_cache_destroy(kvm_vcpu_cache);
1529 out_free_5:
1530 sysdev_unregister(&kvm_sysdev);
1531 out_free_4:
1532 sysdev_class_unregister(&kvm_sysdev_class);
1533 out_free_3:
1534 unregister_reboot_notifier(&kvm_reboot_notifier);
1535 unregister_cpu_notifier(&kvm_cpu_notifier);
1536 out_free_2:
1537 on_each_cpu(hardware_disable, NULL, 0, 1);
1538 out_free_1:
1539 kvm_arch_hardware_unsetup();
1540 out_free_0:
1541 __free_page(bad_page);
1542 out:
1543 kvm_arch_exit();
1544 kvm_exit_debug();
1545 out_fail:
1546 return r;
1548 EXPORT_SYMBOL_GPL(kvm_init);
1550 void kvm_exit(void)
1552 kvm_trace_cleanup();
1553 misc_deregister(&kvm_dev);
1554 kmem_cache_destroy(kvm_vcpu_cache);
1555 sysdev_unregister(&kvm_sysdev);
1556 sysdev_class_unregister(&kvm_sysdev_class);
1557 unregister_reboot_notifier(&kvm_reboot_notifier);
1558 unregister_cpu_notifier(&kvm_cpu_notifier);
1559 on_each_cpu(hardware_disable, NULL, 0, 1);
1560 kvm_arch_hardware_unsetup();
1561 kvm_arch_exit();
1562 kvm_exit_debug();
1563 __free_page(bad_page);
1565 EXPORT_SYMBOL_GPL(kvm_exit);