KVM: Simplify kvm_clear_guest_page()
[linux-2.6/linux-2.6-openrd.git] / drivers / kvm / kvm_main.c
blobe83a178dc80f4bc9d17005eb52da2a6cc11fef71
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 "kvm.h"
19 #include "x86.h"
20 #include "irq.h"
22 #include <linux/kvm.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
27 #include <linux/mm.h>
28 #include <linux/miscdevice.h>
29 #include <linux/vmalloc.h>
30 #include <linux/reboot.h>
31 #include <linux/debugfs.h>
32 #include <linux/highmem.h>
33 #include <linux/file.h>
34 #include <linux/sysdev.h>
35 #include <linux/cpu.h>
36 #include <linux/sched.h>
37 #include <linux/cpumask.h>
38 #include <linux/smp.h>
39 #include <linux/anon_inodes.h>
40 #include <linux/profile.h>
41 #include <linux/kvm_para.h>
42 #include <linux/pagemap.h>
43 #include <linux/mman.h>
45 #include <asm/processor.h>
46 #include <asm/io.h>
47 #include <asm/uaccess.h>
48 #include <asm/desc.h>
49 #include <asm/pgtable.h>
51 MODULE_AUTHOR("Qumranet");
52 MODULE_LICENSE("GPL");
54 DEFINE_SPINLOCK(kvm_lock);
55 LIST_HEAD(vm_list);
57 static cpumask_t cpus_hardware_enabled;
59 struct kmem_cache *kvm_vcpu_cache;
60 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
62 static __read_mostly struct preempt_ops kvm_preempt_ops;
64 static struct dentry *debugfs_dir;
66 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
67 unsigned long arg);
69 static inline int valid_vcpu(int n)
71 return likely(n >= 0 && n < KVM_MAX_VCPUS);
75 * Switches to specified vcpu, until a matching vcpu_put()
77 void vcpu_load(struct kvm_vcpu *vcpu)
79 int cpu;
81 mutex_lock(&vcpu->mutex);
82 cpu = get_cpu();
83 preempt_notifier_register(&vcpu->preempt_notifier);
84 kvm_arch_vcpu_load(vcpu, cpu);
85 put_cpu();
88 void vcpu_put(struct kvm_vcpu *vcpu)
90 preempt_disable();
91 kvm_arch_vcpu_put(vcpu);
92 preempt_notifier_unregister(&vcpu->preempt_notifier);
93 preempt_enable();
94 mutex_unlock(&vcpu->mutex);
97 static void ack_flush(void *_completed)
101 void kvm_flush_remote_tlbs(struct kvm *kvm)
103 int i, cpu;
104 cpumask_t cpus;
105 struct kvm_vcpu *vcpu;
107 cpus_clear(cpus);
108 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
109 vcpu = kvm->vcpus[i];
110 if (!vcpu)
111 continue;
112 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
113 continue;
114 cpu = vcpu->cpu;
115 if (cpu != -1 && cpu != raw_smp_processor_id())
116 cpu_set(cpu, cpus);
118 smp_call_function_mask(cpus, ack_flush, NULL, 1);
121 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
123 struct page *page;
124 int r;
126 mutex_init(&vcpu->mutex);
127 vcpu->cpu = -1;
128 vcpu->kvm = kvm;
129 vcpu->vcpu_id = id;
130 init_waitqueue_head(&vcpu->wq);
132 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
133 if (!page) {
134 r = -ENOMEM;
135 goto fail;
137 vcpu->run = page_address(page);
139 r = kvm_arch_vcpu_init(vcpu);
140 if (r < 0)
141 goto fail_free_run;
142 return 0;
144 fail_free_run:
145 free_page((unsigned long)vcpu->run);
146 fail:
147 return r;
149 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
151 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
153 kvm_arch_vcpu_uninit(vcpu);
154 free_page((unsigned long)vcpu->run);
156 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
158 static struct kvm *kvm_create_vm(void)
160 struct kvm *kvm = kvm_arch_create_vm();
162 if (IS_ERR(kvm))
163 goto out;
165 kvm_io_bus_init(&kvm->pio_bus);
166 mutex_init(&kvm->lock);
167 kvm_io_bus_init(&kvm->mmio_bus);
168 spin_lock(&kvm_lock);
169 list_add(&kvm->vm_list, &vm_list);
170 spin_unlock(&kvm_lock);
171 out:
172 return kvm;
176 * Free any memory in @free but not in @dont.
178 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
179 struct kvm_memory_slot *dont)
181 if (!dont || free->rmap != dont->rmap)
182 vfree(free->rmap);
184 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
185 vfree(free->dirty_bitmap);
187 free->npages = 0;
188 free->dirty_bitmap = NULL;
189 free->rmap = NULL;
192 void kvm_free_physmem(struct kvm *kvm)
194 int i;
196 for (i = 0; i < kvm->nmemslots; ++i)
197 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
200 static void kvm_destroy_vm(struct kvm *kvm)
202 spin_lock(&kvm_lock);
203 list_del(&kvm->vm_list);
204 spin_unlock(&kvm_lock);
205 kvm_io_bus_destroy(&kvm->pio_bus);
206 kvm_io_bus_destroy(&kvm->mmio_bus);
207 kvm_arch_destroy_vm(kvm);
210 static int kvm_vm_release(struct inode *inode, struct file *filp)
212 struct kvm *kvm = filp->private_data;
214 kvm_destroy_vm(kvm);
215 return 0;
219 * Allocate some memory and give it an address in the guest physical address
220 * space.
222 * Discontiguous memory is allowed, mostly for framebuffers.
224 * Must be called holding kvm->lock.
226 int __kvm_set_memory_region(struct kvm *kvm,
227 struct kvm_userspace_memory_region *mem,
228 int user_alloc)
230 int r;
231 gfn_t base_gfn;
232 unsigned long npages;
233 unsigned long i;
234 struct kvm_memory_slot *memslot;
235 struct kvm_memory_slot old, new;
237 r = -EINVAL;
238 /* General sanity checks */
239 if (mem->memory_size & (PAGE_SIZE - 1))
240 goto out;
241 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
242 goto out;
243 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
244 goto out;
245 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
246 goto out;
248 memslot = &kvm->memslots[mem->slot];
249 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
250 npages = mem->memory_size >> PAGE_SHIFT;
252 if (!npages)
253 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
255 new = old = *memslot;
257 new.base_gfn = base_gfn;
258 new.npages = npages;
259 new.flags = mem->flags;
261 /* Disallow changing a memory slot's size. */
262 r = -EINVAL;
263 if (npages && old.npages && npages != old.npages)
264 goto out_free;
266 /* Check for overlaps */
267 r = -EEXIST;
268 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
269 struct kvm_memory_slot *s = &kvm->memslots[i];
271 if (s == memslot)
272 continue;
273 if (!((base_gfn + npages <= s->base_gfn) ||
274 (base_gfn >= s->base_gfn + s->npages)))
275 goto out_free;
278 /* Free page dirty bitmap if unneeded */
279 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
280 new.dirty_bitmap = NULL;
282 r = -ENOMEM;
284 /* Allocate if a slot is being created */
285 if (npages && !new.rmap) {
286 new.rmap = vmalloc(npages * sizeof(struct page *));
288 if (!new.rmap)
289 goto out_free;
291 memset(new.rmap, 0, npages * sizeof(*new.rmap));
293 new.user_alloc = user_alloc;
294 if (user_alloc)
295 new.userspace_addr = mem->userspace_addr;
296 else {
297 down_write(&current->mm->mmap_sem);
298 new.userspace_addr = do_mmap(NULL, 0,
299 npages * PAGE_SIZE,
300 PROT_READ | PROT_WRITE,
301 MAP_SHARED | MAP_ANONYMOUS,
303 up_write(&current->mm->mmap_sem);
305 if (IS_ERR((void *)new.userspace_addr))
306 goto out_free;
308 } else {
309 if (!old.user_alloc && old.rmap) {
310 int ret;
312 down_write(&current->mm->mmap_sem);
313 ret = do_munmap(current->mm, old.userspace_addr,
314 old.npages * PAGE_SIZE);
315 up_write(&current->mm->mmap_sem);
316 if (ret < 0)
317 printk(KERN_WARNING
318 "kvm_vm_ioctl_set_memory_region: "
319 "failed to munmap memory\n");
323 /* Allocate page dirty bitmap if needed */
324 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
325 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
327 new.dirty_bitmap = vmalloc(dirty_bytes);
328 if (!new.dirty_bitmap)
329 goto out_free;
330 memset(new.dirty_bitmap, 0, dirty_bytes);
333 if (mem->slot >= kvm->nmemslots)
334 kvm->nmemslots = mem->slot + 1;
336 if (!kvm->n_requested_mmu_pages) {
337 unsigned int n_pages;
339 if (npages) {
340 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
341 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
342 n_pages);
343 } else {
344 unsigned int nr_mmu_pages;
346 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
347 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
348 nr_mmu_pages = max(nr_mmu_pages,
349 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
350 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
354 *memslot = new;
356 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
357 kvm_flush_remote_tlbs(kvm);
359 kvm_free_physmem_slot(&old, &new);
360 return 0;
362 out_free:
363 kvm_free_physmem_slot(&new, &old);
364 out:
365 return r;
368 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
370 int kvm_set_memory_region(struct kvm *kvm,
371 struct kvm_userspace_memory_region *mem,
372 int user_alloc)
374 int r;
376 mutex_lock(&kvm->lock);
377 r = __kvm_set_memory_region(kvm, mem, user_alloc);
378 mutex_unlock(&kvm->lock);
379 return r;
381 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
383 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
384 struct
385 kvm_userspace_memory_region *mem,
386 int user_alloc)
388 if (mem->slot >= KVM_MEMORY_SLOTS)
389 return -EINVAL;
390 return kvm_set_memory_region(kvm, mem, user_alloc);
393 int kvm_get_dirty_log(struct kvm *kvm,
394 struct kvm_dirty_log *log, int *is_dirty)
396 struct kvm_memory_slot *memslot;
397 int r, i;
398 int n;
399 unsigned long any = 0;
401 r = -EINVAL;
402 if (log->slot >= KVM_MEMORY_SLOTS)
403 goto out;
405 memslot = &kvm->memslots[log->slot];
406 r = -ENOENT;
407 if (!memslot->dirty_bitmap)
408 goto out;
410 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
412 for (i = 0; !any && i < n/sizeof(long); ++i)
413 any = memslot->dirty_bitmap[i];
415 r = -EFAULT;
416 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
417 goto out;
419 if (any)
420 *is_dirty = 1;
422 r = 0;
423 out:
424 return r;
427 int is_error_page(struct page *page)
429 return page == bad_page;
431 EXPORT_SYMBOL_GPL(is_error_page);
433 static inline unsigned long bad_hva(void)
435 return PAGE_OFFSET;
438 int kvm_is_error_hva(unsigned long addr)
440 return addr == bad_hva();
442 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
444 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
446 int i;
447 struct kvm_mem_alias *alias;
449 for (i = 0; i < kvm->naliases; ++i) {
450 alias = &kvm->aliases[i];
451 if (gfn >= alias->base_gfn
452 && gfn < alias->base_gfn + alias->npages)
453 return alias->target_gfn + gfn - alias->base_gfn;
455 return gfn;
458 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
460 int i;
462 for (i = 0; i < kvm->nmemslots; ++i) {
463 struct kvm_memory_slot *memslot = &kvm->memslots[i];
465 if (gfn >= memslot->base_gfn
466 && gfn < memslot->base_gfn + memslot->npages)
467 return memslot;
469 return NULL;
472 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
474 gfn = unalias_gfn(kvm, gfn);
475 return __gfn_to_memslot(kvm, gfn);
478 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
480 int i;
482 gfn = unalias_gfn(kvm, gfn);
483 for (i = 0; i < KVM_MEMORY_SLOTS; ++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 1;
490 return 0;
492 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
494 static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
496 struct kvm_memory_slot *slot;
498 gfn = unalias_gfn(kvm, gfn);
499 slot = __gfn_to_memslot(kvm, gfn);
500 if (!slot)
501 return bad_hva();
502 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
506 * Requires current->mm->mmap_sem to be held
508 static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn)
510 struct page *page[1];
511 unsigned long addr;
512 int npages;
514 might_sleep();
516 addr = gfn_to_hva(kvm, gfn);
517 if (kvm_is_error_hva(addr)) {
518 get_page(bad_page);
519 return bad_page;
522 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
523 NULL);
525 if (npages != 1) {
526 get_page(bad_page);
527 return bad_page;
530 return page[0];
533 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
535 struct page *page;
537 down_read(&current->mm->mmap_sem);
538 page = __gfn_to_page(kvm, gfn);
539 up_read(&current->mm->mmap_sem);
541 return page;
544 EXPORT_SYMBOL_GPL(gfn_to_page);
546 void kvm_release_page(struct page *page)
548 if (!PageReserved(page))
549 SetPageDirty(page);
550 put_page(page);
552 EXPORT_SYMBOL_GPL(kvm_release_page);
554 static int next_segment(unsigned long len, int offset)
556 if (len > PAGE_SIZE - offset)
557 return PAGE_SIZE - offset;
558 else
559 return len;
562 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
563 int len)
565 int r;
566 unsigned long addr;
568 addr = gfn_to_hva(kvm, gfn);
569 if (kvm_is_error_hva(addr))
570 return -EFAULT;
571 r = copy_from_user(data, (void __user *)addr + offset, len);
572 if (r)
573 return -EFAULT;
574 return 0;
576 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
578 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
580 gfn_t gfn = gpa >> PAGE_SHIFT;
581 int seg;
582 int offset = offset_in_page(gpa);
583 int ret;
585 while ((seg = next_segment(len, offset)) != 0) {
586 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
587 if (ret < 0)
588 return ret;
589 offset = 0;
590 len -= seg;
591 data += seg;
592 ++gfn;
594 return 0;
596 EXPORT_SYMBOL_GPL(kvm_read_guest);
598 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
599 int offset, int len)
601 int r;
602 unsigned long addr;
604 addr = gfn_to_hva(kvm, gfn);
605 if (kvm_is_error_hva(addr))
606 return -EFAULT;
607 r = copy_to_user((void __user *)addr + offset, data, len);
608 if (r)
609 return -EFAULT;
610 mark_page_dirty(kvm, gfn);
611 return 0;
613 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
615 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
616 unsigned long len)
618 gfn_t gfn = gpa >> PAGE_SHIFT;
619 int seg;
620 int offset = offset_in_page(gpa);
621 int ret;
623 while ((seg = next_segment(len, offset)) != 0) {
624 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
625 if (ret < 0)
626 return ret;
627 offset = 0;
628 len -= seg;
629 data += seg;
630 ++gfn;
632 return 0;
635 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
637 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
639 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
641 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
643 gfn_t gfn = gpa >> PAGE_SHIFT;
644 int seg;
645 int offset = offset_in_page(gpa);
646 int ret;
648 while ((seg = next_segment(len, offset)) != 0) {
649 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
650 if (ret < 0)
651 return ret;
652 offset = 0;
653 len -= seg;
654 ++gfn;
656 return 0;
658 EXPORT_SYMBOL_GPL(kvm_clear_guest);
660 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
662 struct kvm_memory_slot *memslot;
664 gfn = unalias_gfn(kvm, gfn);
665 memslot = __gfn_to_memslot(kvm, gfn);
666 if (memslot && memslot->dirty_bitmap) {
667 unsigned long rel_gfn = gfn - memslot->base_gfn;
669 /* avoid RMW */
670 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
671 set_bit(rel_gfn, memslot->dirty_bitmap);
676 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
678 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
680 DECLARE_WAITQUEUE(wait, current);
682 add_wait_queue(&vcpu->wq, &wait);
685 * We will block until either an interrupt or a signal wakes us up
687 while (!kvm_cpu_has_interrupt(vcpu)
688 && !signal_pending(current)
689 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
690 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
691 set_current_state(TASK_INTERRUPTIBLE);
692 vcpu_put(vcpu);
693 schedule();
694 vcpu_load(vcpu);
697 __set_current_state(TASK_RUNNING);
698 remove_wait_queue(&vcpu->wq, &wait);
701 void kvm_resched(struct kvm_vcpu *vcpu)
703 if (!need_resched())
704 return;
705 cond_resched();
707 EXPORT_SYMBOL_GPL(kvm_resched);
709 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
710 struct kvm_interrupt *irq)
712 if (irq->irq < 0 || irq->irq >= 256)
713 return -EINVAL;
714 if (irqchip_in_kernel(vcpu->kvm))
715 return -ENXIO;
716 vcpu_load(vcpu);
718 set_bit(irq->irq, vcpu->irq_pending);
719 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
721 vcpu_put(vcpu);
723 return 0;
726 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
727 unsigned long address,
728 int *type)
730 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
731 unsigned long pgoff;
732 struct page *page;
734 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
735 if (pgoff == 0)
736 page = virt_to_page(vcpu->run);
737 else if (pgoff == KVM_PIO_PAGE_OFFSET)
738 page = virt_to_page(vcpu->pio_data);
739 else
740 return NOPAGE_SIGBUS;
741 get_page(page);
742 if (type != NULL)
743 *type = VM_FAULT_MINOR;
745 return page;
748 static struct vm_operations_struct kvm_vcpu_vm_ops = {
749 .nopage = kvm_vcpu_nopage,
752 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
754 vma->vm_ops = &kvm_vcpu_vm_ops;
755 return 0;
758 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
760 struct kvm_vcpu *vcpu = filp->private_data;
762 fput(vcpu->kvm->filp);
763 return 0;
766 static struct file_operations kvm_vcpu_fops = {
767 .release = kvm_vcpu_release,
768 .unlocked_ioctl = kvm_vcpu_ioctl,
769 .compat_ioctl = kvm_vcpu_ioctl,
770 .mmap = kvm_vcpu_mmap,
774 * Allocates an inode for the vcpu.
776 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
778 int fd, r;
779 struct inode *inode;
780 struct file *file;
782 r = anon_inode_getfd(&fd, &inode, &file,
783 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
784 if (r)
785 return r;
786 atomic_inc(&vcpu->kvm->filp->f_count);
787 return fd;
791 * Creates some virtual cpus. Good luck creating more than one.
793 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
795 int r;
796 struct kvm_vcpu *vcpu;
798 if (!valid_vcpu(n))
799 return -EINVAL;
801 vcpu = kvm_arch_vcpu_create(kvm, n);
802 if (IS_ERR(vcpu))
803 return PTR_ERR(vcpu);
805 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
807 mutex_lock(&kvm->lock);
808 if (kvm->vcpus[n]) {
809 r = -EEXIST;
810 mutex_unlock(&kvm->lock);
811 goto vcpu_destroy;
813 kvm->vcpus[n] = vcpu;
814 mutex_unlock(&kvm->lock);
816 /* Now it's all set up, let userspace reach it */
817 r = create_vcpu_fd(vcpu);
818 if (r < 0)
819 goto unlink;
820 return r;
822 unlink:
823 mutex_lock(&kvm->lock);
824 kvm->vcpus[n] = NULL;
825 mutex_unlock(&kvm->lock);
826 vcpu_destroy:
827 kvm_arch_vcpu_destory(vcpu);
828 return r;
831 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
833 if (sigset) {
834 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
835 vcpu->sigset_active = 1;
836 vcpu->sigset = *sigset;
837 } else
838 vcpu->sigset_active = 0;
839 return 0;
842 static long kvm_vcpu_ioctl(struct file *filp,
843 unsigned int ioctl, unsigned long arg)
845 struct kvm_vcpu *vcpu = filp->private_data;
846 void __user *argp = (void __user *)arg;
847 int r;
849 switch (ioctl) {
850 case KVM_RUN:
851 r = -EINVAL;
852 if (arg)
853 goto out;
854 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
855 break;
856 case KVM_GET_REGS: {
857 struct kvm_regs kvm_regs;
859 memset(&kvm_regs, 0, sizeof kvm_regs);
860 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
861 if (r)
862 goto out;
863 r = -EFAULT;
864 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
865 goto out;
866 r = 0;
867 break;
869 case KVM_SET_REGS: {
870 struct kvm_regs kvm_regs;
872 r = -EFAULT;
873 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
874 goto out;
875 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
876 if (r)
877 goto out;
878 r = 0;
879 break;
881 case KVM_GET_SREGS: {
882 struct kvm_sregs kvm_sregs;
884 memset(&kvm_sregs, 0, sizeof kvm_sregs);
885 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
886 if (r)
887 goto out;
888 r = -EFAULT;
889 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
890 goto out;
891 r = 0;
892 break;
894 case KVM_SET_SREGS: {
895 struct kvm_sregs kvm_sregs;
897 r = -EFAULT;
898 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
899 goto out;
900 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
901 if (r)
902 goto out;
903 r = 0;
904 break;
906 case KVM_TRANSLATE: {
907 struct kvm_translation tr;
909 r = -EFAULT;
910 if (copy_from_user(&tr, argp, sizeof tr))
911 goto out;
912 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
913 if (r)
914 goto out;
915 r = -EFAULT;
916 if (copy_to_user(argp, &tr, sizeof tr))
917 goto out;
918 r = 0;
919 break;
921 case KVM_INTERRUPT: {
922 struct kvm_interrupt irq;
924 r = -EFAULT;
925 if (copy_from_user(&irq, argp, sizeof irq))
926 goto out;
927 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
928 if (r)
929 goto out;
930 r = 0;
931 break;
933 case KVM_DEBUG_GUEST: {
934 struct kvm_debug_guest dbg;
936 r = -EFAULT;
937 if (copy_from_user(&dbg, argp, sizeof dbg))
938 goto out;
939 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
940 if (r)
941 goto out;
942 r = 0;
943 break;
945 case KVM_SET_SIGNAL_MASK: {
946 struct kvm_signal_mask __user *sigmask_arg = argp;
947 struct kvm_signal_mask kvm_sigmask;
948 sigset_t sigset, *p;
950 p = NULL;
951 if (argp) {
952 r = -EFAULT;
953 if (copy_from_user(&kvm_sigmask, argp,
954 sizeof kvm_sigmask))
955 goto out;
956 r = -EINVAL;
957 if (kvm_sigmask.len != sizeof sigset)
958 goto out;
959 r = -EFAULT;
960 if (copy_from_user(&sigset, sigmask_arg->sigset,
961 sizeof sigset))
962 goto out;
963 p = &sigset;
965 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
966 break;
968 case KVM_GET_FPU: {
969 struct kvm_fpu fpu;
971 memset(&fpu, 0, sizeof fpu);
972 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
973 if (r)
974 goto out;
975 r = -EFAULT;
976 if (copy_to_user(argp, &fpu, sizeof fpu))
977 goto out;
978 r = 0;
979 break;
981 case KVM_SET_FPU: {
982 struct kvm_fpu fpu;
984 r = -EFAULT;
985 if (copy_from_user(&fpu, argp, sizeof fpu))
986 goto out;
987 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
988 if (r)
989 goto out;
990 r = 0;
991 break;
993 default:
994 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
996 out:
997 return r;
1000 static long kvm_vm_ioctl(struct file *filp,
1001 unsigned int ioctl, unsigned long arg)
1003 struct kvm *kvm = filp->private_data;
1004 void __user *argp = (void __user *)arg;
1005 int r;
1007 switch (ioctl) {
1008 case KVM_CREATE_VCPU:
1009 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1010 if (r < 0)
1011 goto out;
1012 break;
1013 case KVM_SET_USER_MEMORY_REGION: {
1014 struct kvm_userspace_memory_region kvm_userspace_mem;
1016 r = -EFAULT;
1017 if (copy_from_user(&kvm_userspace_mem, argp,
1018 sizeof kvm_userspace_mem))
1019 goto out;
1021 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1022 if (r)
1023 goto out;
1024 break;
1026 case KVM_GET_DIRTY_LOG: {
1027 struct kvm_dirty_log log;
1029 r = -EFAULT;
1030 if (copy_from_user(&log, argp, sizeof log))
1031 goto out;
1032 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1033 if (r)
1034 goto out;
1035 break;
1037 default:
1038 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1040 out:
1041 return r;
1044 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
1045 unsigned long address,
1046 int *type)
1048 struct kvm *kvm = vma->vm_file->private_data;
1049 unsigned long pgoff;
1050 struct page *page;
1052 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1053 if (!kvm_is_visible_gfn(kvm, pgoff))
1054 return NOPAGE_SIGBUS;
1055 /* current->mm->mmap_sem is already held so call lockless version */
1056 page = __gfn_to_page(kvm, pgoff);
1057 if (is_error_page(page)) {
1058 kvm_release_page(page);
1059 return NOPAGE_SIGBUS;
1061 if (type != NULL)
1062 *type = VM_FAULT_MINOR;
1064 return page;
1067 static struct vm_operations_struct kvm_vm_vm_ops = {
1068 .nopage = kvm_vm_nopage,
1071 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1073 vma->vm_ops = &kvm_vm_vm_ops;
1074 return 0;
1077 static struct file_operations kvm_vm_fops = {
1078 .release = kvm_vm_release,
1079 .unlocked_ioctl = kvm_vm_ioctl,
1080 .compat_ioctl = kvm_vm_ioctl,
1081 .mmap = kvm_vm_mmap,
1084 static int kvm_dev_ioctl_create_vm(void)
1086 int fd, r;
1087 struct inode *inode;
1088 struct file *file;
1089 struct kvm *kvm;
1091 kvm = kvm_create_vm();
1092 if (IS_ERR(kvm))
1093 return PTR_ERR(kvm);
1094 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1095 if (r) {
1096 kvm_destroy_vm(kvm);
1097 return r;
1100 kvm->filp = file;
1102 return fd;
1105 static long kvm_dev_ioctl(struct file *filp,
1106 unsigned int ioctl, unsigned long arg)
1108 void __user *argp = (void __user *)arg;
1109 long r = -EINVAL;
1111 switch (ioctl) {
1112 case KVM_GET_API_VERSION:
1113 r = -EINVAL;
1114 if (arg)
1115 goto out;
1116 r = KVM_API_VERSION;
1117 break;
1118 case KVM_CREATE_VM:
1119 r = -EINVAL;
1120 if (arg)
1121 goto out;
1122 r = kvm_dev_ioctl_create_vm();
1123 break;
1124 case KVM_CHECK_EXTENSION:
1125 r = kvm_dev_ioctl_check_extension((long)argp);
1126 break;
1127 case KVM_GET_VCPU_MMAP_SIZE:
1128 r = -EINVAL;
1129 if (arg)
1130 goto out;
1131 r = 2 * PAGE_SIZE;
1132 break;
1133 default:
1134 return kvm_arch_dev_ioctl(filp, ioctl, arg);
1136 out:
1137 return r;
1140 static struct file_operations kvm_chardev_ops = {
1141 .unlocked_ioctl = kvm_dev_ioctl,
1142 .compat_ioctl = kvm_dev_ioctl,
1145 static struct miscdevice kvm_dev = {
1146 KVM_MINOR,
1147 "kvm",
1148 &kvm_chardev_ops,
1151 static void hardware_enable(void *junk)
1153 int cpu = raw_smp_processor_id();
1155 if (cpu_isset(cpu, cpus_hardware_enabled))
1156 return;
1157 cpu_set(cpu, cpus_hardware_enabled);
1158 kvm_arch_hardware_enable(NULL);
1161 static void hardware_disable(void *junk)
1163 int cpu = raw_smp_processor_id();
1165 if (!cpu_isset(cpu, cpus_hardware_enabled))
1166 return;
1167 cpu_clear(cpu, cpus_hardware_enabled);
1168 decache_vcpus_on_cpu(cpu);
1169 kvm_arch_hardware_disable(NULL);
1172 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1173 void *v)
1175 int cpu = (long)v;
1177 val &= ~CPU_TASKS_FROZEN;
1178 switch (val) {
1179 case CPU_DYING:
1180 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1181 cpu);
1182 hardware_disable(NULL);
1183 break;
1184 case CPU_UP_CANCELED:
1185 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1186 cpu);
1187 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
1188 break;
1189 case CPU_ONLINE:
1190 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1191 cpu);
1192 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
1193 break;
1195 return NOTIFY_OK;
1198 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1199 void *v)
1201 if (val == SYS_RESTART) {
1203 * Some (well, at least mine) BIOSes hang on reboot if
1204 * in vmx root mode.
1206 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1207 on_each_cpu(hardware_disable, NULL, 0, 1);
1209 return NOTIFY_OK;
1212 static struct notifier_block kvm_reboot_notifier = {
1213 .notifier_call = kvm_reboot,
1214 .priority = 0,
1217 void kvm_io_bus_init(struct kvm_io_bus *bus)
1219 memset(bus, 0, sizeof(*bus));
1222 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1224 int i;
1226 for (i = 0; i < bus->dev_count; i++) {
1227 struct kvm_io_device *pos = bus->devs[i];
1229 kvm_iodevice_destructor(pos);
1233 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1235 int i;
1237 for (i = 0; i < bus->dev_count; i++) {
1238 struct kvm_io_device *pos = bus->devs[i];
1240 if (pos->in_range(pos, addr))
1241 return pos;
1244 return NULL;
1247 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1249 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1251 bus->devs[bus->dev_count++] = dev;
1254 static struct notifier_block kvm_cpu_notifier = {
1255 .notifier_call = kvm_cpu_hotplug,
1256 .priority = 20, /* must be > scheduler priority */
1259 static u64 vm_stat_get(void *_offset)
1261 unsigned offset = (long)_offset;
1262 u64 total = 0;
1263 struct kvm *kvm;
1265 spin_lock(&kvm_lock);
1266 list_for_each_entry(kvm, &vm_list, vm_list)
1267 total += *(u32 *)((void *)kvm + offset);
1268 spin_unlock(&kvm_lock);
1269 return total;
1272 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1274 static u64 vcpu_stat_get(void *_offset)
1276 unsigned offset = (long)_offset;
1277 u64 total = 0;
1278 struct kvm *kvm;
1279 struct kvm_vcpu *vcpu;
1280 int i;
1282 spin_lock(&kvm_lock);
1283 list_for_each_entry(kvm, &vm_list, vm_list)
1284 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1285 vcpu = kvm->vcpus[i];
1286 if (vcpu)
1287 total += *(u32 *)((void *)vcpu + offset);
1289 spin_unlock(&kvm_lock);
1290 return total;
1293 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1295 static struct file_operations *stat_fops[] = {
1296 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1297 [KVM_STAT_VM] = &vm_stat_fops,
1300 static void kvm_init_debug(void)
1302 struct kvm_stats_debugfs_item *p;
1304 debugfs_dir = debugfs_create_dir("kvm", NULL);
1305 for (p = debugfs_entries; p->name; ++p)
1306 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1307 (void *)(long)p->offset,
1308 stat_fops[p->kind]);
1311 static void kvm_exit_debug(void)
1313 struct kvm_stats_debugfs_item *p;
1315 for (p = debugfs_entries; p->name; ++p)
1316 debugfs_remove(p->dentry);
1317 debugfs_remove(debugfs_dir);
1320 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1322 hardware_disable(NULL);
1323 return 0;
1326 static int kvm_resume(struct sys_device *dev)
1328 hardware_enable(NULL);
1329 return 0;
1332 static struct sysdev_class kvm_sysdev_class = {
1333 .name = "kvm",
1334 .suspend = kvm_suspend,
1335 .resume = kvm_resume,
1338 static struct sys_device kvm_sysdev = {
1339 .id = 0,
1340 .cls = &kvm_sysdev_class,
1343 struct page *bad_page;
1345 static inline
1346 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1348 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1351 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1353 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1355 kvm_arch_vcpu_load(vcpu, cpu);
1358 static void kvm_sched_out(struct preempt_notifier *pn,
1359 struct task_struct *next)
1361 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1363 kvm_arch_vcpu_put(vcpu);
1366 int kvm_init(void *opaque, unsigned int vcpu_size,
1367 struct module *module)
1369 int r;
1370 int cpu;
1372 kvm_init_debug();
1374 r = kvm_arch_init(opaque);
1375 if (r)
1376 goto out4;
1378 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1380 if (bad_page == NULL) {
1381 r = -ENOMEM;
1382 goto out;
1385 r = kvm_arch_hardware_setup();
1386 if (r < 0)
1387 goto out;
1389 for_each_online_cpu(cpu) {
1390 smp_call_function_single(cpu,
1391 kvm_arch_check_processor_compat,
1392 &r, 0, 1);
1393 if (r < 0)
1394 goto out_free_0;
1397 on_each_cpu(hardware_enable, NULL, 0, 1);
1398 r = register_cpu_notifier(&kvm_cpu_notifier);
1399 if (r)
1400 goto out_free_1;
1401 register_reboot_notifier(&kvm_reboot_notifier);
1403 r = sysdev_class_register(&kvm_sysdev_class);
1404 if (r)
1405 goto out_free_2;
1407 r = sysdev_register(&kvm_sysdev);
1408 if (r)
1409 goto out_free_3;
1411 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1412 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
1413 __alignof__(struct kvm_vcpu),
1414 0, NULL);
1415 if (!kvm_vcpu_cache) {
1416 r = -ENOMEM;
1417 goto out_free_4;
1420 kvm_chardev_ops.owner = module;
1422 r = misc_register(&kvm_dev);
1423 if (r) {
1424 printk(KERN_ERR "kvm: misc device register failed\n");
1425 goto out_free;
1428 kvm_preempt_ops.sched_in = kvm_sched_in;
1429 kvm_preempt_ops.sched_out = kvm_sched_out;
1431 return 0;
1433 out_free:
1434 kmem_cache_destroy(kvm_vcpu_cache);
1435 out_free_4:
1436 sysdev_unregister(&kvm_sysdev);
1437 out_free_3:
1438 sysdev_class_unregister(&kvm_sysdev_class);
1439 out_free_2:
1440 unregister_reboot_notifier(&kvm_reboot_notifier);
1441 unregister_cpu_notifier(&kvm_cpu_notifier);
1442 out_free_1:
1443 on_each_cpu(hardware_disable, NULL, 0, 1);
1444 out_free_0:
1445 kvm_arch_hardware_unsetup();
1446 out:
1447 kvm_arch_exit();
1448 kvm_exit_debug();
1449 out4:
1450 return r;
1452 EXPORT_SYMBOL_GPL(kvm_init);
1454 void kvm_exit(void)
1456 misc_deregister(&kvm_dev);
1457 kmem_cache_destroy(kvm_vcpu_cache);
1458 sysdev_unregister(&kvm_sysdev);
1459 sysdev_class_unregister(&kvm_sysdev_class);
1460 unregister_reboot_notifier(&kvm_reboot_notifier);
1461 unregister_cpu_notifier(&kvm_cpu_notifier);
1462 on_each_cpu(hardware_disable, NULL, 0, 1);
1463 kvm_arch_hardware_unsetup();
1464 kvm_arch_exit();
1465 kvm_exit_debug();
1466 __free_page(bad_page);
1468 EXPORT_SYMBOL_GPL(kvm_exit);