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.
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.
20 #include <linux/kvm.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <asm/processor.h>
24 #include <linux/percpu.h>
25 #include <linux/gfp.h>
28 #include <linux/miscdevice.h>
29 #include <linux/vmalloc.h>
30 #include <asm/uaccess.h>
31 #include <linux/reboot.h>
33 #include <linux/debugfs.h>
34 #include <linux/highmem.h>
35 #include <linux/file.h>
38 #include "x86_emulate.h"
39 #include "segment_descriptor.h"
41 MODULE_AUTHOR("Qumranet");
42 MODULE_LICENSE("GPL");
44 struct kvm_arch_ops *kvm_arch_ops;
45 struct kvm_stat kvm_stat;
46 EXPORT_SYMBOL_GPL(kvm_stat);
48 static struct kvm_stats_debugfs_item {
51 struct dentry *dentry;
52 } debugfs_entries[] = {
53 { "pf_fixed", &kvm_stat.pf_fixed },
54 { "pf_guest", &kvm_stat.pf_guest },
55 { "tlb_flush", &kvm_stat.tlb_flush },
56 { "invlpg", &kvm_stat.invlpg },
57 { "exits", &kvm_stat.exits },
58 { "io_exits", &kvm_stat.io_exits },
59 { "mmio_exits", &kvm_stat.mmio_exits },
60 { "signal_exits", &kvm_stat.signal_exits },
61 { "irq_window", &kvm_stat.irq_window_exits },
62 { "halt_exits", &kvm_stat.halt_exits },
63 { "request_irq", &kvm_stat.request_irq_exits },
64 { "irq_exits", &kvm_stat.irq_exits },
68 static struct dentry *debugfs_dir;
70 #define MAX_IO_MSRS 256
72 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
73 #define LMSW_GUEST_MASK 0x0eULL
74 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
75 #define CR8_RESEVED_BITS (~0x0fULL)
76 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
79 // LDT or TSS descriptor in the GDT. 16 bytes.
80 struct segment_descriptor_64 {
81 struct segment_descriptor s;
88 unsigned long segment_base(u16 selector)
90 struct descriptor_table gdt;
91 struct segment_descriptor *d;
92 unsigned long table_base;
93 typedef unsigned long ul;
99 asm ("sgdt %0" : "=m"(gdt));
100 table_base = gdt.base;
102 if (selector & 4) { /* from ldt */
105 asm ("sldt %0" : "=g"(ldt_selector));
106 table_base = segment_base(ldt_selector);
108 d = (struct segment_descriptor *)(table_base + (selector & ~7));
109 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
112 && (d->type == 2 || d->type == 9 || d->type == 11))
113 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
117 EXPORT_SYMBOL_GPL(segment_base);
119 static inline int valid_vcpu(int n)
121 return likely(n >= 0 && n < KVM_MAX_VCPUS);
124 int kvm_read_guest(struct kvm_vcpu *vcpu,
129 unsigned char *host_buf = dest;
130 unsigned long req_size = size;
138 paddr = gva_to_hpa(vcpu, addr);
140 if (is_error_hpa(paddr))
143 guest_buf = (hva_t)kmap_atomic(
144 pfn_to_page(paddr >> PAGE_SHIFT),
146 offset = addr & ~PAGE_MASK;
148 now = min(size, PAGE_SIZE - offset);
149 memcpy(host_buf, (void*)guest_buf, now);
153 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
155 return req_size - size;
157 EXPORT_SYMBOL_GPL(kvm_read_guest);
159 int kvm_write_guest(struct kvm_vcpu *vcpu,
164 unsigned char *host_buf = data;
165 unsigned long req_size = size;
173 paddr = gva_to_hpa(vcpu, addr);
175 if (is_error_hpa(paddr))
178 guest_buf = (hva_t)kmap_atomic(
179 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
180 offset = addr & ~PAGE_MASK;
182 now = min(size, PAGE_SIZE - offset);
183 memcpy((void*)guest_buf, host_buf, now);
187 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
189 return req_size - size;
191 EXPORT_SYMBOL_GPL(kvm_write_guest);
193 static int vcpu_slot(struct kvm_vcpu *vcpu)
195 return vcpu - vcpu->kvm->vcpus;
199 * Switches to specified vcpu, until a matching vcpu_put()
201 static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot)
203 struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot];
205 mutex_lock(&vcpu->mutex);
206 if (unlikely(!vcpu->vmcs)) {
207 mutex_unlock(&vcpu->mutex);
210 return kvm_arch_ops->vcpu_load(vcpu);
213 static void vcpu_put(struct kvm_vcpu *vcpu)
215 kvm_arch_ops->vcpu_put(vcpu);
216 mutex_unlock(&vcpu->mutex);
219 static int kvm_dev_open(struct inode *inode, struct file *filp)
221 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
227 spin_lock_init(&kvm->lock);
228 INIT_LIST_HEAD(&kvm->active_mmu_pages);
229 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
230 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
232 mutex_init(&vcpu->mutex);
234 vcpu->mmu.root_hpa = INVALID_PAGE;
235 INIT_LIST_HEAD(&vcpu->free_pages);
237 filp->private_data = kvm;
242 * Free any memory in @free but not in @dont.
244 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
245 struct kvm_memory_slot *dont)
249 if (!dont || free->phys_mem != dont->phys_mem)
250 if (free->phys_mem) {
251 for (i = 0; i < free->npages; ++i)
252 if (free->phys_mem[i])
253 __free_page(free->phys_mem[i]);
254 vfree(free->phys_mem);
257 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
258 vfree(free->dirty_bitmap);
262 free->dirty_bitmap = 0;
265 static void kvm_free_physmem(struct kvm *kvm)
269 for (i = 0; i < kvm->nmemslots; ++i)
270 kvm_free_physmem_slot(&kvm->memslots[i], 0);
273 static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
275 vcpu_load(vcpu->kvm, vcpu_slot(vcpu));
276 kvm_mmu_destroy(vcpu);
278 kvm_arch_ops->vcpu_free(vcpu);
281 static void kvm_free_vcpus(struct kvm *kvm)
285 for (i = 0; i < KVM_MAX_VCPUS; ++i)
286 kvm_free_vcpu(&kvm->vcpus[i]);
289 static int kvm_dev_release(struct inode *inode, struct file *filp)
291 struct kvm *kvm = filp->private_data;
294 kvm_free_physmem(kvm);
299 static void inject_gp(struct kvm_vcpu *vcpu)
301 kvm_arch_ops->inject_gp(vcpu, 0);
305 * Load the pae pdptrs. Return true is they are all valid.
307 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
309 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
310 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
315 struct kvm_memory_slot *memslot;
317 spin_lock(&vcpu->kvm->lock);
318 memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
319 /* FIXME: !memslot - emulate? 0xff? */
320 pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
323 for (i = 0; i < 4; ++i) {
324 pdpte = pdpt[offset + i];
325 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
331 for (i = 0; i < 4; ++i)
332 vcpu->pdptrs[i] = pdpt[offset + i];
335 kunmap_atomic(pdpt, KM_USER0);
336 spin_unlock(&vcpu->kvm->lock);
341 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
343 if (cr0 & CR0_RESEVED_BITS) {
344 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
350 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
351 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
356 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
357 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
358 "and a clear PE flag\n");
363 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
365 if ((vcpu->shadow_efer & EFER_LME)) {
369 printk(KERN_DEBUG "set_cr0: #GP, start paging "
370 "in long mode while PAE is disabled\n");
374 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
376 printk(KERN_DEBUG "set_cr0: #GP, start paging "
377 "in long mode while CS.L == 1\n");
384 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
385 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
393 kvm_arch_ops->set_cr0(vcpu, cr0);
396 spin_lock(&vcpu->kvm->lock);
397 kvm_mmu_reset_context(vcpu);
398 spin_unlock(&vcpu->kvm->lock);
401 EXPORT_SYMBOL_GPL(set_cr0);
403 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
405 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
406 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
408 EXPORT_SYMBOL_GPL(lmsw);
410 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
412 if (cr4 & CR4_RESEVED_BITS) {
413 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
418 if (is_long_mode(vcpu)) {
419 if (!(cr4 & CR4_PAE_MASK)) {
420 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
425 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
426 && !load_pdptrs(vcpu, vcpu->cr3)) {
427 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
431 if (cr4 & CR4_VMXE_MASK) {
432 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
436 kvm_arch_ops->set_cr4(vcpu, cr4);
437 spin_lock(&vcpu->kvm->lock);
438 kvm_mmu_reset_context(vcpu);
439 spin_unlock(&vcpu->kvm->lock);
441 EXPORT_SYMBOL_GPL(set_cr4);
443 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
445 if (is_long_mode(vcpu)) {
446 if ( cr3 & CR3_L_MODE_RESEVED_BITS) {
447 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
452 if (cr3 & CR3_RESEVED_BITS) {
453 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
457 if (is_paging(vcpu) && is_pae(vcpu) &&
458 !load_pdptrs(vcpu, cr3)) {
459 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
467 spin_lock(&vcpu->kvm->lock);
469 * Does the new cr3 value map to physical memory? (Note, we
470 * catch an invalid cr3 even in real-mode, because it would
471 * cause trouble later on when we turn on paging anyway.)
473 * A real CPU would silently accept an invalid cr3 and would
474 * attempt to use it - with largely undefined (and often hard
475 * to debug) behavior on the guest side.
477 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
480 vcpu->mmu.new_cr3(vcpu);
481 spin_unlock(&vcpu->kvm->lock);
483 EXPORT_SYMBOL_GPL(set_cr3);
485 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
487 if ( cr8 & CR8_RESEVED_BITS) {
488 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
494 EXPORT_SYMBOL_GPL(set_cr8);
496 void fx_init(struct kvm_vcpu *vcpu)
498 struct __attribute__ ((__packed__)) fx_image_s {
504 u64 operand;// fpu dp
510 fx_save(vcpu->host_fx_image);
512 fx_save(vcpu->guest_fx_image);
513 fx_restore(vcpu->host_fx_image);
515 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
516 fx_image->mxcsr = 0x1f80;
517 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
518 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
520 EXPORT_SYMBOL_GPL(fx_init);
523 * Creates some virtual cpus. Good luck creating more than one.
525 static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n)
528 struct kvm_vcpu *vcpu;
534 vcpu = &kvm->vcpus[n];
536 mutex_lock(&vcpu->mutex);
539 mutex_unlock(&vcpu->mutex);
543 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
545 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
547 vcpu->cpu = -1; /* First load will set up TR */
548 r = kvm_arch_ops->vcpu_create(vcpu);
552 r = kvm_mmu_create(vcpu);
556 kvm_arch_ops->vcpu_load(vcpu);
557 r = kvm_mmu_setup(vcpu);
559 r = kvm_arch_ops->vcpu_setup(vcpu);
569 mutex_unlock(&vcpu->mutex);
575 * Allocate some memory and give it an address in the guest physical address
578 * Discontiguous memory is allowed, mostly for framebuffers.
580 static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm,
581 struct kvm_memory_region *mem)
585 unsigned long npages;
587 struct kvm_memory_slot *memslot;
588 struct kvm_memory_slot old, new;
589 int memory_config_version;
592 /* General sanity checks */
593 if (mem->memory_size & (PAGE_SIZE - 1))
595 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
597 if (mem->slot >= KVM_MEMORY_SLOTS)
599 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
602 memslot = &kvm->memslots[mem->slot];
603 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
604 npages = mem->memory_size >> PAGE_SHIFT;
607 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
610 spin_lock(&kvm->lock);
612 memory_config_version = kvm->memory_config_version;
613 new = old = *memslot;
615 new.base_gfn = base_gfn;
617 new.flags = mem->flags;
619 /* Disallow changing a memory slot's size. */
621 if (npages && old.npages && npages != old.npages)
624 /* Check for overlaps */
626 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
627 struct kvm_memory_slot *s = &kvm->memslots[i];
631 if (!((base_gfn + npages <= s->base_gfn) ||
632 (base_gfn >= s->base_gfn + s->npages)))
636 * Do memory allocations outside lock. memory_config_version will
639 spin_unlock(&kvm->lock);
641 /* Deallocate if slot is being removed */
645 /* Free page dirty bitmap if unneeded */
646 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
647 new.dirty_bitmap = 0;
651 /* Allocate if a slot is being created */
652 if (npages && !new.phys_mem) {
653 new.phys_mem = vmalloc(npages * sizeof(struct page *));
658 memset(new.phys_mem, 0, npages * sizeof(struct page *));
659 for (i = 0; i < npages; ++i) {
660 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
662 if (!new.phys_mem[i])
664 new.phys_mem[i]->private = 0;
668 /* Allocate page dirty bitmap if needed */
669 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
670 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
672 new.dirty_bitmap = vmalloc(dirty_bytes);
673 if (!new.dirty_bitmap)
675 memset(new.dirty_bitmap, 0, dirty_bytes);
678 spin_lock(&kvm->lock);
680 if (memory_config_version != kvm->memory_config_version) {
681 spin_unlock(&kvm->lock);
682 kvm_free_physmem_slot(&new, &old);
690 if (mem->slot >= kvm->nmemslots)
691 kvm->nmemslots = mem->slot + 1;
694 ++kvm->memory_config_version;
696 spin_unlock(&kvm->lock);
698 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
699 struct kvm_vcpu *vcpu;
701 vcpu = vcpu_load(kvm, i);
704 kvm_mmu_reset_context(vcpu);
708 kvm_free_physmem_slot(&old, &new);
712 spin_unlock(&kvm->lock);
714 kvm_free_physmem_slot(&new, &old);
719 static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
721 spin_lock(&vcpu->kvm->lock);
722 kvm_mmu_slot_remove_write_access(vcpu, slot);
723 spin_unlock(&vcpu->kvm->lock);
727 * Get (and clear) the dirty memory log for a memory slot.
729 static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm,
730 struct kvm_dirty_log *log)
732 struct kvm_memory_slot *memslot;
736 unsigned long any = 0;
738 spin_lock(&kvm->lock);
741 * Prevent changes to guest memory configuration even while the lock
745 spin_unlock(&kvm->lock);
747 if (log->slot >= KVM_MEMORY_SLOTS)
750 memslot = &kvm->memslots[log->slot];
752 if (!memslot->dirty_bitmap)
755 n = ALIGN(memslot->npages, 8) / 8;
757 for (i = 0; !any && i < n; ++i)
758 any = memslot->dirty_bitmap[i];
761 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
767 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
768 struct kvm_vcpu *vcpu = vcpu_load(kvm, i);
773 do_remove_write_access(vcpu, log->slot);
774 memset(memslot->dirty_bitmap, 0, n);
777 kvm_arch_ops->tlb_flush(vcpu);
785 spin_lock(&kvm->lock);
787 spin_unlock(&kvm->lock);
791 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
795 for (i = 0; i < kvm->nmemslots; ++i) {
796 struct kvm_memory_slot *memslot = &kvm->memslots[i];
798 if (gfn >= memslot->base_gfn
799 && gfn < memslot->base_gfn + memslot->npages)
804 EXPORT_SYMBOL_GPL(gfn_to_memslot);
806 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
809 struct kvm_memory_slot *memslot = 0;
810 unsigned long rel_gfn;
812 for (i = 0; i < kvm->nmemslots; ++i) {
813 memslot = &kvm->memslots[i];
815 if (gfn >= memslot->base_gfn
816 && gfn < memslot->base_gfn + memslot->npages) {
818 if (!memslot || !memslot->dirty_bitmap)
821 rel_gfn = gfn - memslot->base_gfn;
824 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
825 set_bit(rel_gfn, memslot->dirty_bitmap);
831 static int emulator_read_std(unsigned long addr,
834 struct x86_emulate_ctxt *ctxt)
836 struct kvm_vcpu *vcpu = ctxt->vcpu;
840 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
841 unsigned offset = addr & (PAGE_SIZE-1);
842 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
844 struct kvm_memory_slot *memslot;
847 if (gpa == UNMAPPED_GVA)
848 return X86EMUL_PROPAGATE_FAULT;
849 pfn = gpa >> PAGE_SHIFT;
850 memslot = gfn_to_memslot(vcpu->kvm, pfn);
852 return X86EMUL_UNHANDLEABLE;
853 page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
855 memcpy(data, page + offset, tocopy);
857 kunmap_atomic(page, KM_USER0);
864 return X86EMUL_CONTINUE;
867 static int emulator_write_std(unsigned long addr,
870 struct x86_emulate_ctxt *ctxt)
872 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
874 return X86EMUL_UNHANDLEABLE;
877 static int emulator_read_emulated(unsigned long addr,
880 struct x86_emulate_ctxt *ctxt)
882 struct kvm_vcpu *vcpu = ctxt->vcpu;
884 if (vcpu->mmio_read_completed) {
885 memcpy(val, vcpu->mmio_data, bytes);
886 vcpu->mmio_read_completed = 0;
887 return X86EMUL_CONTINUE;
888 } else if (emulator_read_std(addr, val, bytes, ctxt)
890 return X86EMUL_CONTINUE;
892 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
893 if (gpa == UNMAPPED_GVA)
894 return vcpu_printf(vcpu, "not present\n"), X86EMUL_PROPAGATE_FAULT;
895 vcpu->mmio_needed = 1;
896 vcpu->mmio_phys_addr = gpa;
897 vcpu->mmio_size = bytes;
898 vcpu->mmio_is_write = 0;
900 return X86EMUL_UNHANDLEABLE;
904 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
905 unsigned long val, int bytes)
907 struct kvm_memory_slot *m;
911 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
913 m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
916 page = gfn_to_page(m, gpa >> PAGE_SHIFT);
917 kvm_mmu_pre_write(vcpu, gpa, bytes);
918 virt = kmap_atomic(page, KM_USER0);
919 memcpy(virt + offset_in_page(gpa), &val, bytes);
920 kunmap_atomic(virt, KM_USER0);
921 kvm_mmu_post_write(vcpu, gpa, bytes);
925 static int emulator_write_emulated(unsigned long addr,
928 struct x86_emulate_ctxt *ctxt)
930 struct kvm_vcpu *vcpu = ctxt->vcpu;
931 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
933 if (gpa == UNMAPPED_GVA)
934 return X86EMUL_PROPAGATE_FAULT;
936 if (emulator_write_phys(vcpu, gpa, val, bytes))
937 return X86EMUL_CONTINUE;
939 vcpu->mmio_needed = 1;
940 vcpu->mmio_phys_addr = gpa;
941 vcpu->mmio_size = bytes;
942 vcpu->mmio_is_write = 1;
943 memcpy(vcpu->mmio_data, &val, bytes);
945 return X86EMUL_CONTINUE;
948 static int emulator_cmpxchg_emulated(unsigned long addr,
952 struct x86_emulate_ctxt *ctxt)
958 printk(KERN_WARNING "kvm: emulating exchange as write\n");
960 return emulator_write_emulated(addr, new, bytes, ctxt);
965 static int emulator_cmpxchg8b_emulated(unsigned long addr,
966 unsigned long old_lo,
967 unsigned long old_hi,
968 unsigned long new_lo,
969 unsigned long new_hi,
970 struct x86_emulate_ctxt *ctxt)
977 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
979 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
980 if (r != X86EMUL_CONTINUE)
982 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
987 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
989 return kvm_arch_ops->get_segment_base(vcpu, seg);
992 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
994 return X86EMUL_CONTINUE;
997 int emulate_clts(struct kvm_vcpu *vcpu)
1001 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1002 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
1003 kvm_arch_ops->set_cr0(vcpu, cr0);
1004 return X86EMUL_CONTINUE;
1007 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1009 struct kvm_vcpu *vcpu = ctxt->vcpu;
1013 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1014 return X86EMUL_CONTINUE;
1016 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1018 return X86EMUL_UNHANDLEABLE;
1022 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1024 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1027 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1029 /* FIXME: better handling */
1030 return X86EMUL_UNHANDLEABLE;
1032 return X86EMUL_CONTINUE;
1035 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1037 static int reported;
1039 unsigned long rip = ctxt->vcpu->rip;
1040 unsigned long rip_linear;
1042 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1047 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1049 printk(KERN_ERR "emulation failed but !mmio_needed?"
1050 " rip %lx %02x %02x %02x %02x\n",
1051 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1055 struct x86_emulate_ops emulate_ops = {
1056 .read_std = emulator_read_std,
1057 .write_std = emulator_write_std,
1058 .read_emulated = emulator_read_emulated,
1059 .write_emulated = emulator_write_emulated,
1060 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1061 #ifdef CONFIG_X86_32
1062 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1066 int emulate_instruction(struct kvm_vcpu *vcpu,
1067 struct kvm_run *run,
1071 struct x86_emulate_ctxt emulate_ctxt;
1075 kvm_arch_ops->cache_regs(vcpu);
1077 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1079 emulate_ctxt.vcpu = vcpu;
1080 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1081 emulate_ctxt.cr2 = cr2;
1082 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1083 ? X86EMUL_MODE_REAL : cs_l
1084 ? X86EMUL_MODE_PROT64 : cs_db
1085 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1087 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1088 emulate_ctxt.cs_base = 0;
1089 emulate_ctxt.ds_base = 0;
1090 emulate_ctxt.es_base = 0;
1091 emulate_ctxt.ss_base = 0;
1093 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1094 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1095 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1096 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1099 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1100 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1102 vcpu->mmio_is_write = 0;
1103 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1105 if ((r || vcpu->mmio_is_write) && run) {
1106 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1107 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1108 run->mmio.len = vcpu->mmio_size;
1109 run->mmio.is_write = vcpu->mmio_is_write;
1113 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1114 return EMULATE_DONE;
1115 if (!vcpu->mmio_needed) {
1116 report_emulation_failure(&emulate_ctxt);
1117 return EMULATE_FAIL;
1119 return EMULATE_DO_MMIO;
1122 kvm_arch_ops->decache_regs(vcpu);
1123 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1125 if (vcpu->mmio_is_write)
1126 return EMULATE_DO_MMIO;
1128 return EMULATE_DONE;
1130 EXPORT_SYMBOL_GPL(emulate_instruction);
1132 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1134 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1137 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1139 struct descriptor_table dt = { limit, base };
1141 kvm_arch_ops->set_gdt(vcpu, &dt);
1144 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1146 struct descriptor_table dt = { limit, base };
1148 kvm_arch_ops->set_idt(vcpu, &dt);
1151 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1152 unsigned long *rflags)
1155 *rflags = kvm_arch_ops->get_rflags(vcpu);
1158 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1160 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1171 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1176 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1177 unsigned long *rflags)
1181 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1182 *rflags = kvm_arch_ops->get_rflags(vcpu);
1191 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1194 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1198 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1203 case 0xc0010010: /* SYSCFG */
1204 case 0xc0010015: /* HWCR */
1205 case MSR_IA32_PLATFORM_ID:
1206 case MSR_IA32_P5_MC_ADDR:
1207 case MSR_IA32_P5_MC_TYPE:
1208 case MSR_IA32_MC0_CTL:
1209 case MSR_IA32_MCG_STATUS:
1210 case MSR_IA32_MCG_CAP:
1211 case MSR_IA32_MC0_MISC:
1212 case MSR_IA32_MC0_MISC+4:
1213 case MSR_IA32_MC0_MISC+8:
1214 case MSR_IA32_MC0_MISC+12:
1215 case MSR_IA32_MC0_MISC+16:
1216 case MSR_IA32_UCODE_REV:
1217 case MSR_IA32_PERF_STATUS:
1218 /* MTRR registers */
1220 case 0x200 ... 0x2ff:
1223 case 0xcd: /* fsb frequency */
1226 case MSR_IA32_APICBASE:
1227 data = vcpu->apic_base;
1229 #ifdef CONFIG_X86_64
1231 data = vcpu->shadow_efer;
1235 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1241 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1244 * Reads an msr value (of 'msr_index') into 'pdata'.
1245 * Returns 0 on success, non-0 otherwise.
1246 * Assumes vcpu_load() was already called.
1248 static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1250 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1253 #ifdef CONFIG_X86_64
1255 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1257 if (efer & EFER_RESERVED_BITS) {
1258 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1265 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1266 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1271 kvm_arch_ops->set_efer(vcpu, efer);
1274 efer |= vcpu->shadow_efer & EFER_LMA;
1276 vcpu->shadow_efer = efer;
1281 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1284 #ifdef CONFIG_X86_64
1286 set_efer(vcpu, data);
1289 case MSR_IA32_MC0_STATUS:
1290 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1291 __FUNCTION__, data);
1293 case MSR_IA32_UCODE_REV:
1294 case MSR_IA32_UCODE_WRITE:
1295 case 0x200 ... 0x2ff: /* MTRRs */
1297 case MSR_IA32_APICBASE:
1298 vcpu->apic_base = data;
1301 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1306 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1309 * Writes msr value into into the appropriate "register".
1310 * Returns 0 on success, non-0 otherwise.
1311 * Assumes vcpu_load() was already called.
1313 static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1315 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1318 void kvm_resched(struct kvm_vcpu *vcpu)
1322 /* Cannot fail - no vcpu unplug yet. */
1323 vcpu_load(vcpu->kvm, vcpu_slot(vcpu));
1325 EXPORT_SYMBOL_GPL(kvm_resched);
1327 void load_msrs(struct vmx_msr_entry *e, int n)
1331 for (i = 0; i < n; ++i)
1332 wrmsrl(e[i].index, e[i].data);
1334 EXPORT_SYMBOL_GPL(load_msrs);
1336 void save_msrs(struct vmx_msr_entry *e, int n)
1340 for (i = 0; i < n; ++i)
1341 rdmsrl(e[i].index, e[i].data);
1343 EXPORT_SYMBOL_GPL(save_msrs);
1345 static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run)
1347 struct kvm_vcpu *vcpu;
1350 if (!valid_vcpu(kvm_run->vcpu))
1353 vcpu = vcpu_load(kvm, kvm_run->vcpu);
1357 if (kvm_run->emulated) {
1358 kvm_arch_ops->skip_emulated_instruction(vcpu);
1359 kvm_run->emulated = 0;
1362 if (kvm_run->mmio_completed) {
1363 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1364 vcpu->mmio_read_completed = 1;
1367 vcpu->mmio_needed = 0;
1369 r = kvm_arch_ops->run(vcpu, kvm_run);
1375 static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs)
1377 struct kvm_vcpu *vcpu;
1379 if (!valid_vcpu(regs->vcpu))
1382 vcpu = vcpu_load(kvm, regs->vcpu);
1386 kvm_arch_ops->cache_regs(vcpu);
1388 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1389 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1390 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1391 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1392 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1393 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1394 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1395 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1396 #ifdef CONFIG_X86_64
1397 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1398 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1399 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1400 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1401 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1402 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1403 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1404 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1407 regs->rip = vcpu->rip;
1408 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1411 * Don't leak debug flags in case they were set for guest debugging
1413 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1414 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1421 static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs)
1423 struct kvm_vcpu *vcpu;
1425 if (!valid_vcpu(regs->vcpu))
1428 vcpu = vcpu_load(kvm, regs->vcpu);
1432 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1433 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1434 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1435 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1436 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1437 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1438 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1439 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1440 #ifdef CONFIG_X86_64
1441 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1442 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1443 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1444 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1445 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1446 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1447 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1448 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1451 vcpu->rip = regs->rip;
1452 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1454 kvm_arch_ops->decache_regs(vcpu);
1461 static void get_segment(struct kvm_vcpu *vcpu,
1462 struct kvm_segment *var, int seg)
1464 return kvm_arch_ops->get_segment(vcpu, var, seg);
1467 static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1469 struct kvm_vcpu *vcpu;
1470 struct descriptor_table dt;
1472 if (!valid_vcpu(sregs->vcpu))
1474 vcpu = vcpu_load(kvm, sregs->vcpu);
1478 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1479 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1480 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1481 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1482 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1483 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1485 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1486 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1488 kvm_arch_ops->get_idt(vcpu, &dt);
1489 sregs->idt.limit = dt.limit;
1490 sregs->idt.base = dt.base;
1491 kvm_arch_ops->get_gdt(vcpu, &dt);
1492 sregs->gdt.limit = dt.limit;
1493 sregs->gdt.base = dt.base;
1495 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1496 sregs->cr0 = vcpu->cr0;
1497 sregs->cr2 = vcpu->cr2;
1498 sregs->cr3 = vcpu->cr3;
1499 sregs->cr4 = vcpu->cr4;
1500 sregs->cr8 = vcpu->cr8;
1501 sregs->efer = vcpu->shadow_efer;
1502 sregs->apic_base = vcpu->apic_base;
1504 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1505 sizeof sregs->interrupt_bitmap);
1512 static void set_segment(struct kvm_vcpu *vcpu,
1513 struct kvm_segment *var, int seg)
1515 return kvm_arch_ops->set_segment(vcpu, var, seg);
1518 static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1520 struct kvm_vcpu *vcpu;
1521 int mmu_reset_needed = 0;
1523 struct descriptor_table dt;
1525 if (!valid_vcpu(sregs->vcpu))
1527 vcpu = vcpu_load(kvm, sregs->vcpu);
1531 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1532 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1533 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1534 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1535 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1536 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1538 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1539 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1541 dt.limit = sregs->idt.limit;
1542 dt.base = sregs->idt.base;
1543 kvm_arch_ops->set_idt(vcpu, &dt);
1544 dt.limit = sregs->gdt.limit;
1545 dt.base = sregs->gdt.base;
1546 kvm_arch_ops->set_gdt(vcpu, &dt);
1548 vcpu->cr2 = sregs->cr2;
1549 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1550 vcpu->cr3 = sregs->cr3;
1552 vcpu->cr8 = sregs->cr8;
1554 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
1555 #ifdef CONFIG_X86_64
1556 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1558 vcpu->apic_base = sregs->apic_base;
1560 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1562 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1563 kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
1565 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1566 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1567 if (!is_long_mode(vcpu) && is_pae(vcpu))
1568 load_pdptrs(vcpu, vcpu->cr3);
1570 if (mmu_reset_needed)
1571 kvm_mmu_reset_context(vcpu);
1573 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1574 sizeof vcpu->irq_pending);
1575 vcpu->irq_summary = 0;
1576 for (i = 0; i < NR_IRQ_WORDS; ++i)
1577 if (vcpu->irq_pending[i])
1578 __set_bit(i, &vcpu->irq_summary);
1586 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1587 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1589 * This list is modified at module load time to reflect the
1590 * capabilities of the host cpu.
1592 static u32 msrs_to_save[] = {
1593 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1595 #ifdef CONFIG_X86_64
1596 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1598 MSR_IA32_TIME_STAMP_COUNTER,
1601 static unsigned num_msrs_to_save;
1603 static __init void kvm_init_msr_list(void)
1608 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1609 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1612 msrs_to_save[j] = msrs_to_save[i];
1615 num_msrs_to_save = j;
1619 * Adapt set_msr() to msr_io()'s calling convention
1621 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1623 return set_msr(vcpu, index, *data);
1627 * Read or write a bunch of msrs. All parameters are kernel addresses.
1629 * @return number of msrs set successfully.
1631 static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs,
1632 struct kvm_msr_entry *entries,
1633 int (*do_msr)(struct kvm_vcpu *vcpu,
1634 unsigned index, u64 *data))
1636 struct kvm_vcpu *vcpu;
1639 if (!valid_vcpu(msrs->vcpu))
1642 vcpu = vcpu_load(kvm, msrs->vcpu);
1646 for (i = 0; i < msrs->nmsrs; ++i)
1647 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1656 * Read or write a bunch of msrs. Parameters are user addresses.
1658 * @return number of msrs set successfully.
1660 static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs,
1661 int (*do_msr)(struct kvm_vcpu *vcpu,
1662 unsigned index, u64 *data),
1665 struct kvm_msrs msrs;
1666 struct kvm_msr_entry *entries;
1671 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1675 if (msrs.nmsrs >= MAX_IO_MSRS)
1679 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1680 entries = vmalloc(size);
1685 if (copy_from_user(entries, user_msrs->entries, size))
1688 r = n = __msr_io(kvm, &msrs, entries, do_msr);
1693 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1705 * Translate a guest virtual address to a guest physical address.
1707 static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr)
1709 unsigned long vaddr = tr->linear_address;
1710 struct kvm_vcpu *vcpu;
1713 vcpu = vcpu_load(kvm, tr->vcpu);
1716 spin_lock(&kvm->lock);
1717 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
1718 tr->physical_address = gpa;
1719 tr->valid = gpa != UNMAPPED_GVA;
1722 spin_unlock(&kvm->lock);
1728 static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq)
1730 struct kvm_vcpu *vcpu;
1732 if (!valid_vcpu(irq->vcpu))
1734 if (irq->irq < 0 || irq->irq >= 256)
1736 vcpu = vcpu_load(kvm, irq->vcpu);
1740 set_bit(irq->irq, vcpu->irq_pending);
1741 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
1748 static int kvm_dev_ioctl_debug_guest(struct kvm *kvm,
1749 struct kvm_debug_guest *dbg)
1751 struct kvm_vcpu *vcpu;
1754 if (!valid_vcpu(dbg->vcpu))
1756 vcpu = vcpu_load(kvm, dbg->vcpu);
1760 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
1767 static long kvm_dev_ioctl(struct file *filp,
1768 unsigned int ioctl, unsigned long arg)
1770 struct kvm *kvm = filp->private_data;
1774 case KVM_GET_API_VERSION:
1775 r = KVM_API_VERSION;
1777 case KVM_CREATE_VCPU: {
1778 r = kvm_dev_ioctl_create_vcpu(kvm, arg);
1784 struct kvm_run kvm_run;
1787 if (copy_from_user(&kvm_run, (void *)arg, sizeof kvm_run))
1789 r = kvm_dev_ioctl_run(kvm, &kvm_run);
1790 if (r < 0 && r != -EINTR)
1792 if (copy_to_user((void *)arg, &kvm_run, sizeof kvm_run)) {
1798 case KVM_GET_REGS: {
1799 struct kvm_regs kvm_regs;
1802 if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs))
1804 r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs);
1808 if (copy_to_user((void *)arg, &kvm_regs, sizeof kvm_regs))
1813 case KVM_SET_REGS: {
1814 struct kvm_regs kvm_regs;
1817 if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs))
1819 r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs);
1825 case KVM_GET_SREGS: {
1826 struct kvm_sregs kvm_sregs;
1829 if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs))
1831 r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs);
1835 if (copy_to_user((void *)arg, &kvm_sregs, sizeof kvm_sregs))
1840 case KVM_SET_SREGS: {
1841 struct kvm_sregs kvm_sregs;
1844 if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs))
1846 r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs);
1852 case KVM_TRANSLATE: {
1853 struct kvm_translation tr;
1856 if (copy_from_user(&tr, (void *)arg, sizeof tr))
1858 r = kvm_dev_ioctl_translate(kvm, &tr);
1862 if (copy_to_user((void *)arg, &tr, sizeof tr))
1867 case KVM_INTERRUPT: {
1868 struct kvm_interrupt irq;
1871 if (copy_from_user(&irq, (void *)arg, sizeof irq))
1873 r = kvm_dev_ioctl_interrupt(kvm, &irq);
1879 case KVM_DEBUG_GUEST: {
1880 struct kvm_debug_guest dbg;
1883 if (copy_from_user(&dbg, (void *)arg, sizeof dbg))
1885 r = kvm_dev_ioctl_debug_guest(kvm, &dbg);
1891 case KVM_SET_MEMORY_REGION: {
1892 struct kvm_memory_region kvm_mem;
1895 if (copy_from_user(&kvm_mem, (void *)arg, sizeof kvm_mem))
1897 r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem);
1902 case KVM_GET_DIRTY_LOG: {
1903 struct kvm_dirty_log log;
1906 if (copy_from_user(&log, (void *)arg, sizeof log))
1908 r = kvm_dev_ioctl_get_dirty_log(kvm, &log);
1914 r = msr_io(kvm, (void __user *)arg, get_msr, 1);
1917 r = msr_io(kvm, (void __user *)arg, do_set_msr, 0);
1919 case KVM_GET_MSR_INDEX_LIST: {
1920 struct kvm_msr_list __user *user_msr_list = (void __user *)arg;
1921 struct kvm_msr_list msr_list;
1925 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1928 msr_list.nmsrs = num_msrs_to_save;
1929 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1932 if (n < num_msrs_to_save)
1935 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1936 num_msrs_to_save * sizeof(u32)))
1948 static struct page *kvm_dev_nopage(struct vm_area_struct *vma,
1949 unsigned long address,
1952 struct kvm *kvm = vma->vm_file->private_data;
1953 unsigned long pgoff;
1954 struct kvm_memory_slot *slot;
1957 *type = VM_FAULT_MINOR;
1958 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1959 slot = gfn_to_memslot(kvm, pgoff);
1961 return NOPAGE_SIGBUS;
1962 page = gfn_to_page(slot, pgoff);
1964 return NOPAGE_SIGBUS;
1969 static struct vm_operations_struct kvm_dev_vm_ops = {
1970 .nopage = kvm_dev_nopage,
1973 static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma)
1975 vma->vm_ops = &kvm_dev_vm_ops;
1979 static struct file_operations kvm_chardev_ops = {
1980 .open = kvm_dev_open,
1981 .release = kvm_dev_release,
1982 .unlocked_ioctl = kvm_dev_ioctl,
1983 .compat_ioctl = kvm_dev_ioctl,
1984 .mmap = kvm_dev_mmap,
1987 static struct miscdevice kvm_dev = {
1993 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1996 if (val == SYS_RESTART) {
1998 * Some (well, at least mine) BIOSes hang on reboot if
2001 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2002 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
2007 static struct notifier_block kvm_reboot_notifier = {
2008 .notifier_call = kvm_reboot,
2012 static __init void kvm_init_debug(void)
2014 struct kvm_stats_debugfs_item *p;
2016 debugfs_dir = debugfs_create_dir("kvm", 0);
2017 for (p = debugfs_entries; p->name; ++p)
2018 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2022 static void kvm_exit_debug(void)
2024 struct kvm_stats_debugfs_item *p;
2026 for (p = debugfs_entries; p->name; ++p)
2027 debugfs_remove(p->dentry);
2028 debugfs_remove(debugfs_dir);
2031 hpa_t bad_page_address;
2033 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2038 printk(KERN_ERR "kvm: already loaded the other module\n");
2042 if (!ops->cpu_has_kvm_support()) {
2043 printk(KERN_ERR "kvm: no hardware support\n");
2046 if (ops->disabled_by_bios()) {
2047 printk(KERN_ERR "kvm: disabled by bios\n");
2053 r = kvm_arch_ops->hardware_setup();
2057 on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1);
2058 register_reboot_notifier(&kvm_reboot_notifier);
2060 kvm_chardev_ops.owner = module;
2062 r = misc_register(&kvm_dev);
2064 printk (KERN_ERR "kvm: misc device register failed\n");
2071 unregister_reboot_notifier(&kvm_reboot_notifier);
2072 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
2073 kvm_arch_ops->hardware_unsetup();
2077 void kvm_exit_arch(void)
2079 misc_deregister(&kvm_dev);
2081 unregister_reboot_notifier(&kvm_reboot_notifier);
2082 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
2083 kvm_arch_ops->hardware_unsetup();
2084 kvm_arch_ops = NULL;
2087 static __init int kvm_init(void)
2089 static struct page *bad_page;
2094 kvm_init_msr_list();
2096 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2101 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2102 memset(__va(bad_page_address), 0, PAGE_SIZE);
2111 static __exit void kvm_exit(void)
2114 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
2117 module_init(kvm_init)
2118 module_exit(kvm_exit)
2120 EXPORT_SYMBOL_GPL(kvm_init_arch);
2121 EXPORT_SYMBOL_GPL(kvm_exit_arch);