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_exits", &kvm_stat.irq_exits },
65 static struct dentry *debugfs_dir;
67 #define MAX_IO_MSRS 256
69 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
70 #define LMSW_GUEST_MASK 0x0eULL
71 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
72 #define CR8_RESEVED_BITS (~0x0fULL)
73 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
76 // LDT or TSS descriptor in the GDT. 16 bytes.
77 struct segment_descriptor_64 {
78 struct segment_descriptor s;
85 unsigned long segment_base(u16 selector)
87 struct descriptor_table gdt;
88 struct segment_descriptor *d;
89 unsigned long table_base;
90 typedef unsigned long ul;
96 asm ("sgdt %0" : "=m"(gdt));
97 table_base = gdt.base;
99 if (selector & 4) { /* from ldt */
102 asm ("sldt %0" : "=g"(ldt_selector));
103 table_base = segment_base(ldt_selector);
105 d = (struct segment_descriptor *)(table_base + (selector & ~7));
106 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
109 && (d->type == 2 || d->type == 9 || d->type == 11))
110 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
114 EXPORT_SYMBOL_GPL(segment_base);
116 static inline int valid_vcpu(int n)
118 return likely(n >= 0 && n < KVM_MAX_VCPUS);
121 int kvm_read_guest(struct kvm_vcpu *vcpu,
126 unsigned char *host_buf = dest;
127 unsigned long req_size = size;
135 paddr = gva_to_hpa(vcpu, addr);
137 if (is_error_hpa(paddr))
140 guest_buf = (hva_t)kmap_atomic(
141 pfn_to_page(paddr >> PAGE_SHIFT),
143 offset = addr & ~PAGE_MASK;
145 now = min(size, PAGE_SIZE - offset);
146 memcpy(host_buf, (void*)guest_buf, now);
150 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
152 return req_size - size;
154 EXPORT_SYMBOL_GPL(kvm_read_guest);
156 int kvm_write_guest(struct kvm_vcpu *vcpu,
161 unsigned char *host_buf = data;
162 unsigned long req_size = size;
170 paddr = gva_to_hpa(vcpu, addr);
172 if (is_error_hpa(paddr))
175 guest_buf = (hva_t)kmap_atomic(
176 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
177 offset = addr & ~PAGE_MASK;
179 now = min(size, PAGE_SIZE - offset);
180 memcpy((void*)guest_buf, host_buf, now);
184 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
186 return req_size - size;
188 EXPORT_SYMBOL_GPL(kvm_write_guest);
190 static int vcpu_slot(struct kvm_vcpu *vcpu)
192 return vcpu - vcpu->kvm->vcpus;
196 * Switches to specified vcpu, until a matching vcpu_put()
198 static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot)
200 struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot];
202 mutex_lock(&vcpu->mutex);
203 if (unlikely(!vcpu->vmcs)) {
204 mutex_unlock(&vcpu->mutex);
207 return kvm_arch_ops->vcpu_load(vcpu);
210 static void vcpu_put(struct kvm_vcpu *vcpu)
212 kvm_arch_ops->vcpu_put(vcpu);
213 mutex_unlock(&vcpu->mutex);
216 static int kvm_dev_open(struct inode *inode, struct file *filp)
218 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
224 spin_lock_init(&kvm->lock);
225 INIT_LIST_HEAD(&kvm->active_mmu_pages);
226 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
227 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
229 mutex_init(&vcpu->mutex);
230 vcpu->mmu.root_hpa = INVALID_PAGE;
231 INIT_LIST_HEAD(&vcpu->free_pages);
233 filp->private_data = kvm;
238 * Free any memory in @free but not in @dont.
240 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
241 struct kvm_memory_slot *dont)
245 if (!dont || free->phys_mem != dont->phys_mem)
246 if (free->phys_mem) {
247 for (i = 0; i < free->npages; ++i)
248 __free_page(free->phys_mem[i]);
249 vfree(free->phys_mem);
252 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
253 vfree(free->dirty_bitmap);
257 free->dirty_bitmap = 0;
260 static void kvm_free_physmem(struct kvm *kvm)
264 for (i = 0; i < kvm->nmemslots; ++i)
265 kvm_free_physmem_slot(&kvm->memslots[i], 0);
268 static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
270 kvm_arch_ops->vcpu_free(vcpu);
271 kvm_mmu_destroy(vcpu);
274 static void kvm_free_vcpus(struct kvm *kvm)
278 for (i = 0; i < KVM_MAX_VCPUS; ++i)
279 kvm_free_vcpu(&kvm->vcpus[i]);
282 static int kvm_dev_release(struct inode *inode, struct file *filp)
284 struct kvm *kvm = filp->private_data;
287 kvm_free_physmem(kvm);
292 static void inject_gp(struct kvm_vcpu *vcpu)
294 kvm_arch_ops->inject_gp(vcpu, 0);
297 static int pdptrs_have_reserved_bits_set(struct kvm_vcpu *vcpu,
300 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
301 unsigned offset = (cr3 & (PAGE_SIZE-1)) >> 5;
305 struct kvm_memory_slot *memslot;
307 spin_lock(&vcpu->kvm->lock);
308 memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
309 /* FIXME: !memslot - emulate? 0xff? */
310 pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
312 for (i = 0; i < 4; ++i) {
313 pdpte = pdpt[offset + i];
314 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull))
318 kunmap_atomic(pdpt, KM_USER0);
319 spin_unlock(&vcpu->kvm->lock);
324 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
326 if (cr0 & CR0_RESEVED_BITS) {
327 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
333 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
334 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
339 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
340 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
341 "and a clear PE flag\n");
346 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
348 if ((vcpu->shadow_efer & EFER_LME)) {
352 printk(KERN_DEBUG "set_cr0: #GP, start paging "
353 "in long mode while PAE is disabled\n");
357 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
359 printk(KERN_DEBUG "set_cr0: #GP, start paging "
360 "in long mode while CS.L == 1\n");
368 pdptrs_have_reserved_bits_set(vcpu, vcpu->cr3)) {
369 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
377 kvm_arch_ops->set_cr0(vcpu, cr0);
380 spin_lock(&vcpu->kvm->lock);
381 kvm_mmu_reset_context(vcpu);
382 spin_unlock(&vcpu->kvm->lock);
385 EXPORT_SYMBOL_GPL(set_cr0);
387 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
389 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
391 EXPORT_SYMBOL_GPL(lmsw);
393 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
395 if (cr4 & CR4_RESEVED_BITS) {
396 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
401 if (kvm_arch_ops->is_long_mode(vcpu)) {
402 if (!(cr4 & CR4_PAE_MASK)) {
403 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
408 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
409 && pdptrs_have_reserved_bits_set(vcpu, vcpu->cr3)) {
410 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
414 if (cr4 & CR4_VMXE_MASK) {
415 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
419 kvm_arch_ops->set_cr4(vcpu, cr4);
420 spin_lock(&vcpu->kvm->lock);
421 kvm_mmu_reset_context(vcpu);
422 spin_unlock(&vcpu->kvm->lock);
424 EXPORT_SYMBOL_GPL(set_cr4);
426 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
428 if (kvm_arch_ops->is_long_mode(vcpu)) {
429 if ( cr3 & CR3_L_MODE_RESEVED_BITS) {
430 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
435 if (cr3 & CR3_RESEVED_BITS) {
436 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
440 if (is_paging(vcpu) && is_pae(vcpu) &&
441 pdptrs_have_reserved_bits_set(vcpu, cr3)) {
442 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
450 spin_lock(&vcpu->kvm->lock);
451 vcpu->mmu.new_cr3(vcpu);
452 spin_unlock(&vcpu->kvm->lock);
454 EXPORT_SYMBOL_GPL(set_cr3);
456 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
458 if ( cr8 & CR8_RESEVED_BITS) {
459 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
465 EXPORT_SYMBOL_GPL(set_cr8);
467 void fx_init(struct kvm_vcpu *vcpu)
469 struct __attribute__ ((__packed__)) fx_image_s {
475 u64 operand;// fpu dp
481 fx_save(vcpu->host_fx_image);
483 fx_save(vcpu->guest_fx_image);
484 fx_restore(vcpu->host_fx_image);
486 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
487 fx_image->mxcsr = 0x1f80;
488 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
489 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
491 EXPORT_SYMBOL_GPL(fx_init);
494 * Creates some virtual cpus. Good luck creating more than one.
496 static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n)
499 struct kvm_vcpu *vcpu;
505 vcpu = &kvm->vcpus[n];
507 mutex_lock(&vcpu->mutex);
510 mutex_unlock(&vcpu->mutex);
514 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
516 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
518 vcpu->cpu = -1; /* First load will set up TR */
520 r = kvm_arch_ops->vcpu_create(vcpu);
524 kvm_arch_ops->vcpu_load(vcpu);
526 r = kvm_arch_ops->vcpu_setup(vcpu);
528 r = kvm_mmu_init(vcpu);
539 mutex_unlock(&vcpu->mutex);
545 * Allocate some memory and give it an address in the guest physical address
548 * Discontiguous memory is allowed, mostly for framebuffers.
550 static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm,
551 struct kvm_memory_region *mem)
555 unsigned long npages;
557 struct kvm_memory_slot *memslot;
558 struct kvm_memory_slot old, new;
559 int memory_config_version;
562 /* General sanity checks */
563 if (mem->memory_size & (PAGE_SIZE - 1))
565 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
567 if (mem->slot >= KVM_MEMORY_SLOTS)
569 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
572 memslot = &kvm->memslots[mem->slot];
573 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
574 npages = mem->memory_size >> PAGE_SHIFT;
577 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
580 spin_lock(&kvm->lock);
582 memory_config_version = kvm->memory_config_version;
583 new = old = *memslot;
585 new.base_gfn = base_gfn;
587 new.flags = mem->flags;
589 /* Disallow changing a memory slot's size. */
591 if (npages && old.npages && npages != old.npages)
594 /* Check for overlaps */
596 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
597 struct kvm_memory_slot *s = &kvm->memslots[i];
601 if (!((base_gfn + npages <= s->base_gfn) ||
602 (base_gfn >= s->base_gfn + s->npages)))
606 * Do memory allocations outside lock. memory_config_version will
609 spin_unlock(&kvm->lock);
611 /* Deallocate if slot is being removed */
615 /* Free page dirty bitmap if unneeded */
616 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
617 new.dirty_bitmap = 0;
621 /* Allocate if a slot is being created */
622 if (npages && !new.phys_mem) {
623 new.phys_mem = vmalloc(npages * sizeof(struct page *));
628 memset(new.phys_mem, 0, npages * sizeof(struct page *));
629 for (i = 0; i < npages; ++i) {
630 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
632 if (!new.phys_mem[i])
637 /* Allocate page dirty bitmap if needed */
638 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
639 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
641 new.dirty_bitmap = vmalloc(dirty_bytes);
642 if (!new.dirty_bitmap)
644 memset(new.dirty_bitmap, 0, dirty_bytes);
647 spin_lock(&kvm->lock);
649 if (memory_config_version != kvm->memory_config_version) {
650 spin_unlock(&kvm->lock);
651 kvm_free_physmem_slot(&new, &old);
659 if (mem->slot >= kvm->nmemslots)
660 kvm->nmemslots = mem->slot + 1;
663 ++kvm->memory_config_version;
665 spin_unlock(&kvm->lock);
667 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
668 struct kvm_vcpu *vcpu;
670 vcpu = vcpu_load(kvm, i);
673 kvm_mmu_reset_context(vcpu);
677 kvm_free_physmem_slot(&old, &new);
681 spin_unlock(&kvm->lock);
683 kvm_free_physmem_slot(&new, &old);
689 * Get (and clear) the dirty memory log for a memory slot.
691 static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm,
692 struct kvm_dirty_log *log)
694 struct kvm_memory_slot *memslot;
697 unsigned long any = 0;
699 spin_lock(&kvm->lock);
702 * Prevent changes to guest memory configuration even while the lock
706 spin_unlock(&kvm->lock);
708 if (log->slot >= KVM_MEMORY_SLOTS)
711 memslot = &kvm->memslots[log->slot];
713 if (!memslot->dirty_bitmap)
716 n = ALIGN(memslot->npages, 8) / 8;
718 for (i = 0; !any && i < n; ++i)
719 any = memslot->dirty_bitmap[i];
722 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
727 spin_lock(&kvm->lock);
728 kvm_mmu_slot_remove_write_access(kvm, log->slot);
729 spin_unlock(&kvm->lock);
730 memset(memslot->dirty_bitmap, 0, n);
731 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
732 struct kvm_vcpu *vcpu = vcpu_load(kvm, i);
736 kvm_arch_ops->tlb_flush(vcpu);
744 spin_lock(&kvm->lock);
746 spin_unlock(&kvm->lock);
750 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
754 for (i = 0; i < kvm->nmemslots; ++i) {
755 struct kvm_memory_slot *memslot = &kvm->memslots[i];
757 if (gfn >= memslot->base_gfn
758 && gfn < memslot->base_gfn + memslot->npages)
763 EXPORT_SYMBOL_GPL(gfn_to_memslot);
765 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
768 struct kvm_memory_slot *memslot = 0;
769 unsigned long rel_gfn;
771 for (i = 0; i < kvm->nmemslots; ++i) {
772 memslot = &kvm->memslots[i];
774 if (gfn >= memslot->base_gfn
775 && gfn < memslot->base_gfn + memslot->npages) {
777 if (!memslot || !memslot->dirty_bitmap)
780 rel_gfn = gfn - memslot->base_gfn;
783 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
784 set_bit(rel_gfn, memslot->dirty_bitmap);
790 static int emulator_read_std(unsigned long addr,
793 struct x86_emulate_ctxt *ctxt)
795 struct kvm_vcpu *vcpu = ctxt->vcpu;
799 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
800 unsigned offset = addr & (PAGE_SIZE-1);
801 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
803 struct kvm_memory_slot *memslot;
806 if (gpa == UNMAPPED_GVA)
807 return X86EMUL_PROPAGATE_FAULT;
808 pfn = gpa >> PAGE_SHIFT;
809 memslot = gfn_to_memslot(vcpu->kvm, pfn);
811 return X86EMUL_UNHANDLEABLE;
812 page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
814 memcpy(data, page + offset, tocopy);
816 kunmap_atomic(page, KM_USER0);
823 return X86EMUL_CONTINUE;
826 static int emulator_write_std(unsigned long addr,
829 struct x86_emulate_ctxt *ctxt)
831 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
833 return X86EMUL_UNHANDLEABLE;
836 static int emulator_read_emulated(unsigned long addr,
839 struct x86_emulate_ctxt *ctxt)
841 struct kvm_vcpu *vcpu = ctxt->vcpu;
843 if (vcpu->mmio_read_completed) {
844 memcpy(val, vcpu->mmio_data, bytes);
845 vcpu->mmio_read_completed = 0;
846 return X86EMUL_CONTINUE;
847 } else if (emulator_read_std(addr, val, bytes, ctxt)
849 return X86EMUL_CONTINUE;
851 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
852 if (gpa == UNMAPPED_GVA)
853 return vcpu_printf(vcpu, "not present\n"), X86EMUL_PROPAGATE_FAULT;
854 vcpu->mmio_needed = 1;
855 vcpu->mmio_phys_addr = gpa;
856 vcpu->mmio_size = bytes;
857 vcpu->mmio_is_write = 0;
859 return X86EMUL_UNHANDLEABLE;
863 static int emulator_write_emulated(unsigned long addr,
866 struct x86_emulate_ctxt *ctxt)
868 struct kvm_vcpu *vcpu = ctxt->vcpu;
869 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
871 if (gpa == UNMAPPED_GVA)
872 return X86EMUL_PROPAGATE_FAULT;
874 vcpu->mmio_needed = 1;
875 vcpu->mmio_phys_addr = gpa;
876 vcpu->mmio_size = bytes;
877 vcpu->mmio_is_write = 1;
878 memcpy(vcpu->mmio_data, &val, bytes);
880 return X86EMUL_CONTINUE;
883 static int emulator_cmpxchg_emulated(unsigned long addr,
887 struct x86_emulate_ctxt *ctxt)
893 printk(KERN_WARNING "kvm: emulating exchange as write\n");
895 return emulator_write_emulated(addr, new, bytes, ctxt);
898 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
900 return kvm_arch_ops->get_segment_base(vcpu, seg);
903 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
905 spin_lock(&vcpu->kvm->lock);
906 vcpu->mmu.inval_page(vcpu, address);
907 spin_unlock(&vcpu->kvm->lock);
908 kvm_arch_ops->invlpg(vcpu, address);
909 return X86EMUL_CONTINUE;
912 int emulate_clts(struct kvm_vcpu *vcpu)
914 unsigned long cr0 = vcpu->cr0;
917 kvm_arch_ops->set_cr0(vcpu, cr0);
918 return X86EMUL_CONTINUE;
921 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
923 struct kvm_vcpu *vcpu = ctxt->vcpu;
927 *dest = kvm_arch_ops->get_dr(vcpu, dr);
928 return X86EMUL_CONTINUE;
930 printk(KERN_DEBUG "%s: unexpected dr %u\n",
932 return X86EMUL_UNHANDLEABLE;
936 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
938 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
941 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
943 /* FIXME: better handling */
944 return X86EMUL_UNHANDLEABLE;
946 return X86EMUL_CONTINUE;
949 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
953 unsigned long rip = ctxt->vcpu->rip;
954 unsigned long rip_linear;
956 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
961 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
963 printk(KERN_ERR "emulation failed but !mmio_needed?"
964 " rip %lx %02x %02x %02x %02x\n",
965 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
969 struct x86_emulate_ops emulate_ops = {
970 .read_std = emulator_read_std,
971 .write_std = emulator_write_std,
972 .read_emulated = emulator_read_emulated,
973 .write_emulated = emulator_write_emulated,
974 .cmpxchg_emulated = emulator_cmpxchg_emulated,
977 int emulate_instruction(struct kvm_vcpu *vcpu,
982 struct x86_emulate_ctxt emulate_ctxt;
986 kvm_arch_ops->cache_regs(vcpu);
988 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
990 emulate_ctxt.vcpu = vcpu;
991 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
992 emulate_ctxt.cr2 = cr2;
993 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
994 ? X86EMUL_MODE_REAL : cs_l
995 ? X86EMUL_MODE_PROT64 : cs_db
996 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
998 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
999 emulate_ctxt.cs_base = 0;
1000 emulate_ctxt.ds_base = 0;
1001 emulate_ctxt.es_base = 0;
1002 emulate_ctxt.ss_base = 0;
1004 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1005 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1006 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1007 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1010 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1011 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1013 vcpu->mmio_is_write = 0;
1014 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1016 if ((r || vcpu->mmio_is_write) && run) {
1017 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1018 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1019 run->mmio.len = vcpu->mmio_size;
1020 run->mmio.is_write = vcpu->mmio_is_write;
1024 if (!vcpu->mmio_needed) {
1025 report_emulation_failure(&emulate_ctxt);
1026 return EMULATE_FAIL;
1028 return EMULATE_DO_MMIO;
1031 kvm_arch_ops->decache_regs(vcpu);
1032 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1034 if (vcpu->mmio_is_write)
1035 return EMULATE_DO_MMIO;
1037 return EMULATE_DONE;
1039 EXPORT_SYMBOL_GPL(emulate_instruction);
1041 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1043 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1046 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1048 struct descriptor_table dt = { limit, base };
1050 kvm_arch_ops->set_gdt(vcpu, &dt);
1053 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1055 struct descriptor_table dt = { limit, base };
1057 kvm_arch_ops->set_idt(vcpu, &dt);
1060 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1061 unsigned long *rflags)
1064 *rflags = kvm_arch_ops->get_rflags(vcpu);
1067 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1079 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1084 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1085 unsigned long *rflags)
1089 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1090 *rflags = kvm_arch_ops->get_rflags(vcpu);
1099 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1102 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1107 * Reads an msr value (of 'msr_index') into 'pdata'.
1108 * Returns 0 on success, non-0 otherwise.
1109 * Assumes vcpu_load() was already called.
1111 static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1113 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1116 #ifdef CONFIG_X86_64
1118 void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1120 if (efer & EFER_RESERVED_BITS) {
1121 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1128 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1129 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1134 kvm_arch_ops->set_efer(vcpu, efer);
1137 efer |= vcpu->shadow_efer & EFER_LMA;
1139 vcpu->shadow_efer = efer;
1141 EXPORT_SYMBOL_GPL(set_efer);
1146 * Writes msr value into into the appropriate "register".
1147 * Returns 0 on success, non-0 otherwise.
1148 * Assumes vcpu_load() was already called.
1150 static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1152 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1155 void kvm_resched(struct kvm_vcpu *vcpu)
1159 /* Cannot fail - no vcpu unplug yet. */
1160 vcpu_load(vcpu->kvm, vcpu_slot(vcpu));
1162 EXPORT_SYMBOL_GPL(kvm_resched);
1164 void load_msrs(struct vmx_msr_entry *e, int n)
1168 for (i = 0; i < n; ++i)
1169 wrmsrl(e[i].index, e[i].data);
1171 EXPORT_SYMBOL_GPL(load_msrs);
1173 void save_msrs(struct vmx_msr_entry *e, int n)
1177 for (i = 0; i < n; ++i)
1178 rdmsrl(e[i].index, e[i].data);
1180 EXPORT_SYMBOL_GPL(save_msrs);
1182 static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run)
1184 struct kvm_vcpu *vcpu;
1187 if (!valid_vcpu(kvm_run->vcpu))
1190 vcpu = vcpu_load(kvm, kvm_run->vcpu);
1194 if (kvm_run->emulated) {
1195 kvm_arch_ops->skip_emulated_instruction(vcpu);
1196 kvm_run->emulated = 0;
1199 if (kvm_run->mmio_completed) {
1200 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1201 vcpu->mmio_read_completed = 1;
1204 vcpu->mmio_needed = 0;
1206 r = kvm_arch_ops->run(vcpu, kvm_run);
1212 static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs)
1214 struct kvm_vcpu *vcpu;
1216 if (!valid_vcpu(regs->vcpu))
1219 vcpu = vcpu_load(kvm, regs->vcpu);
1223 kvm_arch_ops->cache_regs(vcpu);
1225 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1226 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1227 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1228 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1229 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1230 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1231 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1232 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1233 #ifdef CONFIG_X86_64
1234 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1235 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1236 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1237 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1238 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1239 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1240 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1241 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1244 regs->rip = vcpu->rip;
1245 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1248 * Don't leak debug flags in case they were set for guest debugging
1250 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1251 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1258 static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs)
1260 struct kvm_vcpu *vcpu;
1262 if (!valid_vcpu(regs->vcpu))
1265 vcpu = vcpu_load(kvm, regs->vcpu);
1269 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1270 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1271 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1272 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1273 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1274 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1275 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1276 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1277 #ifdef CONFIG_X86_64
1278 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1279 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1280 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1281 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1282 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1283 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1284 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1285 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1288 vcpu->rip = regs->rip;
1289 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1291 kvm_arch_ops->decache_regs(vcpu);
1298 static void get_segment(struct kvm_vcpu *vcpu,
1299 struct kvm_segment *var, int seg)
1301 return kvm_arch_ops->get_segment(vcpu, var, seg);
1304 static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1306 struct kvm_vcpu *vcpu;
1307 struct descriptor_table dt;
1309 if (!valid_vcpu(sregs->vcpu))
1311 vcpu = vcpu_load(kvm, sregs->vcpu);
1315 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1316 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1317 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1318 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1319 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1320 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1322 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1323 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1325 kvm_arch_ops->get_idt(vcpu, &dt);
1326 sregs->idt.limit = dt.limit;
1327 sregs->idt.base = dt.base;
1328 kvm_arch_ops->get_gdt(vcpu, &dt);
1329 sregs->gdt.limit = dt.limit;
1330 sregs->gdt.base = dt.base;
1332 sregs->cr0 = vcpu->cr0;
1333 sregs->cr2 = vcpu->cr2;
1334 sregs->cr3 = vcpu->cr3;
1335 sregs->cr4 = vcpu->cr4;
1336 sregs->cr8 = vcpu->cr8;
1337 sregs->efer = vcpu->shadow_efer;
1338 sregs->apic_base = vcpu->apic_base;
1340 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1341 sizeof sregs->interrupt_bitmap);
1348 static void set_segment(struct kvm_vcpu *vcpu,
1349 struct kvm_segment *var, int seg)
1351 return kvm_arch_ops->set_segment(vcpu, var, seg);
1354 static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1356 struct kvm_vcpu *vcpu;
1357 int mmu_reset_needed = 0;
1359 struct descriptor_table dt;
1361 if (!valid_vcpu(sregs->vcpu))
1363 vcpu = vcpu_load(kvm, sregs->vcpu);
1367 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1368 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1369 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1370 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1371 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1372 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1374 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1375 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1377 dt.limit = sregs->idt.limit;
1378 dt.base = sregs->idt.base;
1379 kvm_arch_ops->set_idt(vcpu, &dt);
1380 dt.limit = sregs->gdt.limit;
1381 dt.base = sregs->gdt.base;
1382 kvm_arch_ops->set_gdt(vcpu, &dt);
1384 vcpu->cr2 = sregs->cr2;
1385 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1386 vcpu->cr3 = sregs->cr3;
1388 vcpu->cr8 = sregs->cr8;
1390 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
1391 #ifdef CONFIG_X86_64
1392 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1394 vcpu->apic_base = sregs->apic_base;
1396 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1397 kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
1399 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1400 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1402 if (mmu_reset_needed)
1403 kvm_mmu_reset_context(vcpu);
1405 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1406 sizeof vcpu->irq_pending);
1407 vcpu->irq_summary = 0;
1408 for (i = 0; i < NR_IRQ_WORDS; ++i)
1409 if (vcpu->irq_pending[i])
1410 __set_bit(i, &vcpu->irq_summary);
1418 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1419 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1421 * This list is modified at module load time to reflect the
1422 * capabilities of the host cpu.
1424 static u32 msrs_to_save[] = {
1425 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1427 #ifdef CONFIG_X86_64
1428 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1430 MSR_IA32_TIME_STAMP_COUNTER,
1433 static unsigned num_msrs_to_save;
1435 static __init void kvm_init_msr_list(void)
1440 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1441 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1444 msrs_to_save[j] = msrs_to_save[i];
1447 num_msrs_to_save = j;
1451 * Adapt set_msr() to msr_io()'s calling convention
1453 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1455 return set_msr(vcpu, index, *data);
1459 * Read or write a bunch of msrs. All parameters are kernel addresses.
1461 * @return number of msrs set successfully.
1463 static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs,
1464 struct kvm_msr_entry *entries,
1465 int (*do_msr)(struct kvm_vcpu *vcpu,
1466 unsigned index, u64 *data))
1468 struct kvm_vcpu *vcpu;
1471 if (!valid_vcpu(msrs->vcpu))
1474 vcpu = vcpu_load(kvm, msrs->vcpu);
1478 for (i = 0; i < msrs->nmsrs; ++i)
1479 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1488 * Read or write a bunch of msrs. Parameters are user addresses.
1490 * @return number of msrs set successfully.
1492 static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs,
1493 int (*do_msr)(struct kvm_vcpu *vcpu,
1494 unsigned index, u64 *data),
1497 struct kvm_msrs msrs;
1498 struct kvm_msr_entry *entries;
1503 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1507 if (msrs.nmsrs >= MAX_IO_MSRS)
1511 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1512 entries = vmalloc(size);
1517 if (copy_from_user(entries, user_msrs->entries, size))
1520 r = n = __msr_io(kvm, &msrs, entries, do_msr);
1525 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1537 * Translate a guest virtual address to a guest physical address.
1539 static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr)
1541 unsigned long vaddr = tr->linear_address;
1542 struct kvm_vcpu *vcpu;
1545 vcpu = vcpu_load(kvm, tr->vcpu);
1548 spin_lock(&kvm->lock);
1549 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
1550 tr->physical_address = gpa;
1551 tr->valid = gpa != UNMAPPED_GVA;
1554 spin_unlock(&kvm->lock);
1560 static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq)
1562 struct kvm_vcpu *vcpu;
1564 if (!valid_vcpu(irq->vcpu))
1566 if (irq->irq < 0 || irq->irq >= 256)
1568 vcpu = vcpu_load(kvm, irq->vcpu);
1572 set_bit(irq->irq, vcpu->irq_pending);
1573 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
1580 static int kvm_dev_ioctl_debug_guest(struct kvm *kvm,
1581 struct kvm_debug_guest *dbg)
1583 struct kvm_vcpu *vcpu;
1586 if (!valid_vcpu(dbg->vcpu))
1588 vcpu = vcpu_load(kvm, dbg->vcpu);
1592 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
1599 static long kvm_dev_ioctl(struct file *filp,
1600 unsigned int ioctl, unsigned long arg)
1602 struct kvm *kvm = filp->private_data;
1606 case KVM_CREATE_VCPU: {
1607 r = kvm_dev_ioctl_create_vcpu(kvm, arg);
1613 struct kvm_run kvm_run;
1616 if (copy_from_user(&kvm_run, (void *)arg, sizeof kvm_run))
1618 r = kvm_dev_ioctl_run(kvm, &kvm_run);
1622 if (copy_to_user((void *)arg, &kvm_run, sizeof kvm_run))
1627 case KVM_GET_REGS: {
1628 struct kvm_regs kvm_regs;
1631 if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs))
1633 r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs);
1637 if (copy_to_user((void *)arg, &kvm_regs, sizeof kvm_regs))
1642 case KVM_SET_REGS: {
1643 struct kvm_regs kvm_regs;
1646 if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs))
1648 r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs);
1654 case KVM_GET_SREGS: {
1655 struct kvm_sregs kvm_sregs;
1658 if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs))
1660 r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs);
1664 if (copy_to_user((void *)arg, &kvm_sregs, sizeof kvm_sregs))
1669 case KVM_SET_SREGS: {
1670 struct kvm_sregs kvm_sregs;
1673 if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs))
1675 r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs);
1681 case KVM_TRANSLATE: {
1682 struct kvm_translation tr;
1685 if (copy_from_user(&tr, (void *)arg, sizeof tr))
1687 r = kvm_dev_ioctl_translate(kvm, &tr);
1691 if (copy_to_user((void *)arg, &tr, sizeof tr))
1696 case KVM_INTERRUPT: {
1697 struct kvm_interrupt irq;
1700 if (copy_from_user(&irq, (void *)arg, sizeof irq))
1702 r = kvm_dev_ioctl_interrupt(kvm, &irq);
1708 case KVM_DEBUG_GUEST: {
1709 struct kvm_debug_guest dbg;
1712 if (copy_from_user(&dbg, (void *)arg, sizeof dbg))
1714 r = kvm_dev_ioctl_debug_guest(kvm, &dbg);
1720 case KVM_SET_MEMORY_REGION: {
1721 struct kvm_memory_region kvm_mem;
1724 if (copy_from_user(&kvm_mem, (void *)arg, sizeof kvm_mem))
1726 r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem);
1731 case KVM_GET_DIRTY_LOG: {
1732 struct kvm_dirty_log log;
1735 if (copy_from_user(&log, (void *)arg, sizeof log))
1737 r = kvm_dev_ioctl_get_dirty_log(kvm, &log);
1743 r = msr_io(kvm, (void __user *)arg, get_msr, 1);
1746 r = msr_io(kvm, (void __user *)arg, do_set_msr, 0);
1748 case KVM_GET_MSR_INDEX_LIST: {
1749 struct kvm_msr_list __user *user_msr_list = (void __user *)arg;
1750 struct kvm_msr_list msr_list;
1754 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1757 msr_list.nmsrs = num_msrs_to_save;
1758 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1761 if (n < num_msrs_to_save)
1764 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1765 num_msrs_to_save * sizeof(u32)))
1776 static struct page *kvm_dev_nopage(struct vm_area_struct *vma,
1777 unsigned long address,
1780 struct kvm *kvm = vma->vm_file->private_data;
1781 unsigned long pgoff;
1782 struct kvm_memory_slot *slot;
1785 *type = VM_FAULT_MINOR;
1786 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1787 slot = gfn_to_memslot(kvm, pgoff);
1789 return NOPAGE_SIGBUS;
1790 page = gfn_to_page(slot, pgoff);
1792 return NOPAGE_SIGBUS;
1797 static struct vm_operations_struct kvm_dev_vm_ops = {
1798 .nopage = kvm_dev_nopage,
1801 static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma)
1803 vma->vm_ops = &kvm_dev_vm_ops;
1807 static struct file_operations kvm_chardev_ops = {
1808 .open = kvm_dev_open,
1809 .release = kvm_dev_release,
1810 .unlocked_ioctl = kvm_dev_ioctl,
1811 .compat_ioctl = kvm_dev_ioctl,
1812 .mmap = kvm_dev_mmap,
1815 static struct miscdevice kvm_dev = {
1821 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
1824 if (val == SYS_RESTART) {
1826 * Some (well, at least mine) BIOSes hang on reboot if
1829 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1830 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
1835 static struct notifier_block kvm_reboot_notifier = {
1836 .notifier_call = kvm_reboot,
1840 static __init void kvm_init_debug(void)
1842 struct kvm_stats_debugfs_item *p;
1844 debugfs_dir = debugfs_create_dir("kvm", 0);
1845 for (p = debugfs_entries; p->name; ++p)
1846 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
1850 static void kvm_exit_debug(void)
1852 struct kvm_stats_debugfs_item *p;
1854 for (p = debugfs_entries; p->name; ++p)
1855 debugfs_remove(p->dentry);
1856 debugfs_remove(debugfs_dir);
1859 hpa_t bad_page_address;
1861 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
1867 if (!kvm_arch_ops->cpu_has_kvm_support()) {
1868 printk(KERN_ERR "kvm: no hardware support\n");
1871 if (kvm_arch_ops->disabled_by_bios()) {
1872 printk(KERN_ERR "kvm: disabled by bios\n");
1876 r = kvm_arch_ops->hardware_setup();
1880 on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1);
1881 register_reboot_notifier(&kvm_reboot_notifier);
1883 kvm_chardev_ops.owner = module;
1885 r = misc_register(&kvm_dev);
1887 printk (KERN_ERR "kvm: misc device register failed\n");
1894 unregister_reboot_notifier(&kvm_reboot_notifier);
1895 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
1896 kvm_arch_ops->hardware_unsetup();
1900 void kvm_exit_arch(void)
1902 misc_deregister(&kvm_dev);
1904 unregister_reboot_notifier(&kvm_reboot_notifier);
1905 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
1906 kvm_arch_ops->hardware_unsetup();
1909 static __init int kvm_init(void)
1911 static struct page *bad_page;
1916 kvm_init_msr_list();
1918 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
1923 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
1924 memset(__va(bad_page_address), 0, PAGE_SIZE);
1933 static __exit void kvm_exit(void)
1936 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
1939 module_init(kvm_init)
1940 module_exit(kvm_exit)
1942 EXPORT_SYMBOL_GPL(kvm_init_arch);
1943 EXPORT_SYMBOL_GPL(kvm_exit_arch);