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 <linux/magic.h>
24 #include <asm/processor.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
29 #include <linux/miscdevice.h>
30 #include <linux/vmalloc.h>
31 #include <asm/uaccess.h>
32 #include <linux/reboot.h>
34 #include <linux/debugfs.h>
35 #include <linux/highmem.h>
36 #include <linux/file.h>
38 #include <linux/sysdev.h>
39 #include <linux/cpu.h>
40 #include <linux/file.h>
42 #include <linux/mount.h>
44 #include "x86_emulate.h"
45 #include "segment_descriptor.h"
47 MODULE_AUTHOR("Qumranet");
48 MODULE_LICENSE("GPL");
50 static DEFINE_SPINLOCK(kvm_lock);
51 static LIST_HEAD(vm_list);
53 struct kvm_arch_ops *kvm_arch_ops;
54 struct kvm_stat kvm_stat;
55 EXPORT_SYMBOL_GPL(kvm_stat);
57 static struct kvm_stats_debugfs_item {
60 struct dentry *dentry;
61 } debugfs_entries[] = {
62 { "pf_fixed", &kvm_stat.pf_fixed },
63 { "pf_guest", &kvm_stat.pf_guest },
64 { "tlb_flush", &kvm_stat.tlb_flush },
65 { "invlpg", &kvm_stat.invlpg },
66 { "exits", &kvm_stat.exits },
67 { "io_exits", &kvm_stat.io_exits },
68 { "mmio_exits", &kvm_stat.mmio_exits },
69 { "signal_exits", &kvm_stat.signal_exits },
70 { "irq_window", &kvm_stat.irq_window_exits },
71 { "halt_exits", &kvm_stat.halt_exits },
72 { "request_irq", &kvm_stat.request_irq_exits },
73 { "irq_exits", &kvm_stat.irq_exits },
77 static struct dentry *debugfs_dir;
79 struct vfsmount *kvmfs_mnt;
81 #define MAX_IO_MSRS 256
83 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
84 #define LMSW_GUEST_MASK 0x0eULL
85 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
86 #define CR8_RESEVED_BITS (~0x0fULL)
87 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
90 // LDT or TSS descriptor in the GDT. 16 bytes.
91 struct segment_descriptor_64 {
92 struct segment_descriptor s;
99 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
102 static struct inode *kvmfs_inode(struct file_operations *fops)
105 struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
113 * Mark the inode dirty from the very beginning,
114 * that way it will never be moved to the dirty
115 * list because mark_inode_dirty() will think
116 * that it already _is_ on the dirty list.
118 inode->i_state = I_DIRTY;
119 inode->i_mode = S_IRUSR | S_IWUSR;
120 inode->i_uid = current->fsuid;
121 inode->i_gid = current->fsgid;
122 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
126 return ERR_PTR(error);
129 static struct file *kvmfs_file(struct inode *inode, void *private_data)
131 struct file *file = get_empty_filp();
134 return ERR_PTR(-ENFILE);
136 file->f_path.mnt = mntget(kvmfs_mnt);
137 file->f_path.dentry = d_alloc_anon(inode);
138 if (!file->f_path.dentry)
139 return ERR_PTR(-ENOMEM);
140 file->f_mapping = inode->i_mapping;
143 file->f_flags = O_RDWR;
144 file->f_op = inode->i_fop;
145 file->f_mode = FMODE_READ | FMODE_WRITE;
147 file->private_data = private_data;
151 unsigned long segment_base(u16 selector)
153 struct descriptor_table gdt;
154 struct segment_descriptor *d;
155 unsigned long table_base;
156 typedef unsigned long ul;
162 asm ("sgdt %0" : "=m"(gdt));
163 table_base = gdt.base;
165 if (selector & 4) { /* from ldt */
168 asm ("sldt %0" : "=g"(ldt_selector));
169 table_base = segment_base(ldt_selector);
171 d = (struct segment_descriptor *)(table_base + (selector & ~7));
172 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
175 && (d->type == 2 || d->type == 9 || d->type == 11))
176 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
180 EXPORT_SYMBOL_GPL(segment_base);
182 static inline int valid_vcpu(int n)
184 return likely(n >= 0 && n < KVM_MAX_VCPUS);
187 int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
190 unsigned char *host_buf = dest;
191 unsigned long req_size = size;
199 paddr = gva_to_hpa(vcpu, addr);
201 if (is_error_hpa(paddr))
204 guest_buf = (hva_t)kmap_atomic(
205 pfn_to_page(paddr >> PAGE_SHIFT),
207 offset = addr & ~PAGE_MASK;
209 now = min(size, PAGE_SIZE - offset);
210 memcpy(host_buf, (void*)guest_buf, now);
214 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
216 return req_size - size;
218 EXPORT_SYMBOL_GPL(kvm_read_guest);
220 int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
223 unsigned char *host_buf = data;
224 unsigned long req_size = size;
233 paddr = gva_to_hpa(vcpu, addr);
235 if (is_error_hpa(paddr))
238 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
239 mark_page_dirty(vcpu->kvm, gfn);
240 guest_buf = (hva_t)kmap_atomic(
241 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
242 offset = addr & ~PAGE_MASK;
244 now = min(size, PAGE_SIZE - offset);
245 memcpy((void*)guest_buf, host_buf, now);
249 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
251 return req_size - size;
253 EXPORT_SYMBOL_GPL(kvm_write_guest);
256 * Switches to specified vcpu, until a matching vcpu_put()
258 static void vcpu_load(struct kvm_vcpu *vcpu)
260 mutex_lock(&vcpu->mutex);
261 kvm_arch_ops->vcpu_load(vcpu);
265 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
266 * if the slot is not populated.
268 static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
270 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
272 mutex_lock(&vcpu->mutex);
274 mutex_unlock(&vcpu->mutex);
277 kvm_arch_ops->vcpu_load(vcpu);
281 static void vcpu_put(struct kvm_vcpu *vcpu)
283 kvm_arch_ops->vcpu_put(vcpu);
284 mutex_unlock(&vcpu->mutex);
287 static struct kvm *kvm_create_vm(void)
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
293 return ERR_PTR(-ENOMEM);
295 spin_lock_init(&kvm->lock);
296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
297 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
298 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
300 mutex_init(&vcpu->mutex);
303 vcpu->mmu.root_hpa = INVALID_PAGE;
304 INIT_LIST_HEAD(&vcpu->free_pages);
305 spin_lock(&kvm_lock);
306 list_add(&kvm->vm_list, &vm_list);
307 spin_unlock(&kvm_lock);
312 static int kvm_dev_open(struct inode *inode, struct file *filp)
318 * Free any memory in @free but not in @dont.
320 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
321 struct kvm_memory_slot *dont)
325 if (!dont || free->phys_mem != dont->phys_mem)
326 if (free->phys_mem) {
327 for (i = 0; i < free->npages; ++i)
328 if (free->phys_mem[i])
329 __free_page(free->phys_mem[i]);
330 vfree(free->phys_mem);
333 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
334 vfree(free->dirty_bitmap);
336 free->phys_mem = NULL;
338 free->dirty_bitmap = NULL;
341 static void kvm_free_physmem(struct kvm *kvm)
345 for (i = 0; i < kvm->nmemslots; ++i)
346 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
349 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
353 for (i = 0; i < 2; ++i)
354 if (vcpu->pio.guest_pages[i]) {
355 __free_page(vcpu->pio.guest_pages[i]);
356 vcpu->pio.guest_pages[i] = NULL;
360 static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
366 kvm_mmu_destroy(vcpu);
368 kvm_arch_ops->vcpu_free(vcpu);
369 free_page((unsigned long)vcpu->run);
371 free_page((unsigned long)vcpu->pio_data);
372 vcpu->pio_data = NULL;
373 free_pio_guest_pages(vcpu);
376 static void kvm_free_vcpus(struct kvm *kvm)
380 for (i = 0; i < KVM_MAX_VCPUS; ++i)
381 kvm_free_vcpu(&kvm->vcpus[i]);
384 static int kvm_dev_release(struct inode *inode, struct file *filp)
389 static void kvm_destroy_vm(struct kvm *kvm)
391 spin_lock(&kvm_lock);
392 list_del(&kvm->vm_list);
393 spin_unlock(&kvm_lock);
395 kvm_free_physmem(kvm);
399 static int kvm_vm_release(struct inode *inode, struct file *filp)
401 struct kvm *kvm = filp->private_data;
407 static void inject_gp(struct kvm_vcpu *vcpu)
409 kvm_arch_ops->inject_gp(vcpu, 0);
413 * Load the pae pdptrs. Return true is they are all valid.
415 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
417 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
418 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
425 spin_lock(&vcpu->kvm->lock);
426 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
427 /* FIXME: !page - emulate? 0xff? */
428 pdpt = kmap_atomic(page, KM_USER0);
431 for (i = 0; i < 4; ++i) {
432 pdpte = pdpt[offset + i];
433 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
439 for (i = 0; i < 4; ++i)
440 vcpu->pdptrs[i] = pdpt[offset + i];
443 kunmap_atomic(pdpt, KM_USER0);
444 spin_unlock(&vcpu->kvm->lock);
449 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
451 if (cr0 & CR0_RESEVED_BITS) {
452 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
458 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
459 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
464 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
465 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
466 "and a clear PE flag\n");
471 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
473 if ((vcpu->shadow_efer & EFER_LME)) {
477 printk(KERN_DEBUG "set_cr0: #GP, start paging "
478 "in long mode while PAE is disabled\n");
482 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
484 printk(KERN_DEBUG "set_cr0: #GP, start paging "
485 "in long mode while CS.L == 1\n");
492 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
493 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
501 kvm_arch_ops->set_cr0(vcpu, cr0);
504 spin_lock(&vcpu->kvm->lock);
505 kvm_mmu_reset_context(vcpu);
506 spin_unlock(&vcpu->kvm->lock);
509 EXPORT_SYMBOL_GPL(set_cr0);
511 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
513 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
514 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
516 EXPORT_SYMBOL_GPL(lmsw);
518 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
520 if (cr4 & CR4_RESEVED_BITS) {
521 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
526 if (is_long_mode(vcpu)) {
527 if (!(cr4 & CR4_PAE_MASK)) {
528 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
533 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
534 && !load_pdptrs(vcpu, vcpu->cr3)) {
535 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
539 if (cr4 & CR4_VMXE_MASK) {
540 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
544 kvm_arch_ops->set_cr4(vcpu, cr4);
545 spin_lock(&vcpu->kvm->lock);
546 kvm_mmu_reset_context(vcpu);
547 spin_unlock(&vcpu->kvm->lock);
549 EXPORT_SYMBOL_GPL(set_cr4);
551 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
553 if (is_long_mode(vcpu)) {
554 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
555 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
560 if (cr3 & CR3_RESEVED_BITS) {
561 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
565 if (is_paging(vcpu) && is_pae(vcpu) &&
566 !load_pdptrs(vcpu, cr3)) {
567 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
575 spin_lock(&vcpu->kvm->lock);
577 * Does the new cr3 value map to physical memory? (Note, we
578 * catch an invalid cr3 even in real-mode, because it would
579 * cause trouble later on when we turn on paging anyway.)
581 * A real CPU would silently accept an invalid cr3 and would
582 * attempt to use it - with largely undefined (and often hard
583 * to debug) behavior on the guest side.
585 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
588 vcpu->mmu.new_cr3(vcpu);
589 spin_unlock(&vcpu->kvm->lock);
591 EXPORT_SYMBOL_GPL(set_cr3);
593 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
595 if ( cr8 & CR8_RESEVED_BITS) {
596 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
602 EXPORT_SYMBOL_GPL(set_cr8);
604 void fx_init(struct kvm_vcpu *vcpu)
606 struct __attribute__ ((__packed__)) fx_image_s {
612 u64 operand;// fpu dp
618 fx_save(vcpu->host_fx_image);
620 fx_save(vcpu->guest_fx_image);
621 fx_restore(vcpu->host_fx_image);
623 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
624 fx_image->mxcsr = 0x1f80;
625 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
626 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
628 EXPORT_SYMBOL_GPL(fx_init);
630 static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
632 spin_lock(&vcpu->kvm->lock);
633 kvm_mmu_slot_remove_write_access(vcpu, slot);
634 spin_unlock(&vcpu->kvm->lock);
638 * Allocate some memory and give it an address in the guest physical address
641 * Discontiguous memory is allowed, mostly for framebuffers.
643 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
644 struct kvm_memory_region *mem)
648 unsigned long npages;
650 struct kvm_memory_slot *memslot;
651 struct kvm_memory_slot old, new;
652 int memory_config_version;
655 /* General sanity checks */
656 if (mem->memory_size & (PAGE_SIZE - 1))
658 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
660 if (mem->slot >= KVM_MEMORY_SLOTS)
662 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
665 memslot = &kvm->memslots[mem->slot];
666 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
667 npages = mem->memory_size >> PAGE_SHIFT;
670 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
673 spin_lock(&kvm->lock);
675 memory_config_version = kvm->memory_config_version;
676 new = old = *memslot;
678 new.base_gfn = base_gfn;
680 new.flags = mem->flags;
682 /* Disallow changing a memory slot's size. */
684 if (npages && old.npages && npages != old.npages)
687 /* Check for overlaps */
689 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
690 struct kvm_memory_slot *s = &kvm->memslots[i];
694 if (!((base_gfn + npages <= s->base_gfn) ||
695 (base_gfn >= s->base_gfn + s->npages)))
699 * Do memory allocations outside lock. memory_config_version will
702 spin_unlock(&kvm->lock);
704 /* Deallocate if slot is being removed */
708 /* Free page dirty bitmap if unneeded */
709 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
710 new.dirty_bitmap = NULL;
714 /* Allocate if a slot is being created */
715 if (npages && !new.phys_mem) {
716 new.phys_mem = vmalloc(npages * sizeof(struct page *));
721 memset(new.phys_mem, 0, npages * sizeof(struct page *));
722 for (i = 0; i < npages; ++i) {
723 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
725 if (!new.phys_mem[i])
727 set_page_private(new.phys_mem[i],0);
731 /* Allocate page dirty bitmap if needed */
732 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
733 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
735 new.dirty_bitmap = vmalloc(dirty_bytes);
736 if (!new.dirty_bitmap)
738 memset(new.dirty_bitmap, 0, dirty_bytes);
741 spin_lock(&kvm->lock);
743 if (memory_config_version != kvm->memory_config_version) {
744 spin_unlock(&kvm->lock);
745 kvm_free_physmem_slot(&new, &old);
753 if (mem->slot >= kvm->nmemslots)
754 kvm->nmemslots = mem->slot + 1;
757 ++kvm->memory_config_version;
759 spin_unlock(&kvm->lock);
761 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
762 struct kvm_vcpu *vcpu;
764 vcpu = vcpu_load_slot(kvm, i);
767 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
768 do_remove_write_access(vcpu, mem->slot);
769 kvm_mmu_reset_context(vcpu);
773 kvm_free_physmem_slot(&old, &new);
777 spin_unlock(&kvm->lock);
779 kvm_free_physmem_slot(&new, &old);
785 * Get (and clear) the dirty memory log for a memory slot.
787 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
788 struct kvm_dirty_log *log)
790 struct kvm_memory_slot *memslot;
794 unsigned long any = 0;
796 spin_lock(&kvm->lock);
799 * Prevent changes to guest memory configuration even while the lock
803 spin_unlock(&kvm->lock);
805 if (log->slot >= KVM_MEMORY_SLOTS)
808 memslot = &kvm->memslots[log->slot];
810 if (!memslot->dirty_bitmap)
813 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
815 for (i = 0; !any && i < n/sizeof(long); ++i)
816 any = memslot->dirty_bitmap[i];
819 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
824 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
825 struct kvm_vcpu *vcpu;
827 vcpu = vcpu_load_slot(kvm, i);
831 do_remove_write_access(vcpu, log->slot);
832 memset(memslot->dirty_bitmap, 0, n);
835 kvm_arch_ops->tlb_flush(vcpu);
843 spin_lock(&kvm->lock);
845 spin_unlock(&kvm->lock);
849 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
853 for (i = 0; i < kvm->nmemslots; ++i) {
854 struct kvm_memory_slot *memslot = &kvm->memslots[i];
856 if (gfn >= memslot->base_gfn
857 && gfn < memslot->base_gfn + memslot->npages)
862 EXPORT_SYMBOL_GPL(gfn_to_memslot);
864 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
866 struct kvm_memory_slot *slot;
868 slot = gfn_to_memslot(kvm, gfn);
871 return slot->phys_mem[gfn - slot->base_gfn];
873 EXPORT_SYMBOL_GPL(gfn_to_page);
875 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
878 struct kvm_memory_slot *memslot = NULL;
879 unsigned long rel_gfn;
881 for (i = 0; i < kvm->nmemslots; ++i) {
882 memslot = &kvm->memslots[i];
884 if (gfn >= memslot->base_gfn
885 && gfn < memslot->base_gfn + memslot->npages) {
887 if (!memslot || !memslot->dirty_bitmap)
890 rel_gfn = gfn - memslot->base_gfn;
893 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
894 set_bit(rel_gfn, memslot->dirty_bitmap);
900 static int emulator_read_std(unsigned long addr,
903 struct x86_emulate_ctxt *ctxt)
905 struct kvm_vcpu *vcpu = ctxt->vcpu;
909 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
910 unsigned offset = addr & (PAGE_SIZE-1);
911 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
916 if (gpa == UNMAPPED_GVA)
917 return X86EMUL_PROPAGATE_FAULT;
918 pfn = gpa >> PAGE_SHIFT;
919 page = gfn_to_page(vcpu->kvm, pfn);
921 return X86EMUL_UNHANDLEABLE;
922 page_virt = kmap_atomic(page, KM_USER0);
924 memcpy(data, page_virt + offset, tocopy);
926 kunmap_atomic(page_virt, KM_USER0);
933 return X86EMUL_CONTINUE;
936 static int emulator_write_std(unsigned long addr,
939 struct x86_emulate_ctxt *ctxt)
941 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
943 return X86EMUL_UNHANDLEABLE;
946 static int emulator_read_emulated(unsigned long addr,
949 struct x86_emulate_ctxt *ctxt)
951 struct kvm_vcpu *vcpu = ctxt->vcpu;
953 if (vcpu->mmio_read_completed) {
954 memcpy(val, vcpu->mmio_data, bytes);
955 vcpu->mmio_read_completed = 0;
956 return X86EMUL_CONTINUE;
957 } else if (emulator_read_std(addr, val, bytes, ctxt)
959 return X86EMUL_CONTINUE;
961 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
963 if (gpa == UNMAPPED_GVA)
964 return X86EMUL_PROPAGATE_FAULT;
965 vcpu->mmio_needed = 1;
966 vcpu->mmio_phys_addr = gpa;
967 vcpu->mmio_size = bytes;
968 vcpu->mmio_is_write = 0;
970 return X86EMUL_UNHANDLEABLE;
974 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
975 unsigned long val, int bytes)
980 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
982 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
985 kvm_mmu_pre_write(vcpu, gpa, bytes);
986 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
987 virt = kmap_atomic(page, KM_USER0);
988 memcpy(virt + offset_in_page(gpa), &val, bytes);
989 kunmap_atomic(virt, KM_USER0);
990 kvm_mmu_post_write(vcpu, gpa, bytes);
994 static int emulator_write_emulated(unsigned long addr,
997 struct x86_emulate_ctxt *ctxt)
999 struct kvm_vcpu *vcpu = ctxt->vcpu;
1000 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1002 if (gpa == UNMAPPED_GVA)
1003 return X86EMUL_PROPAGATE_FAULT;
1005 if (emulator_write_phys(vcpu, gpa, val, bytes))
1006 return X86EMUL_CONTINUE;
1008 vcpu->mmio_needed = 1;
1009 vcpu->mmio_phys_addr = gpa;
1010 vcpu->mmio_size = bytes;
1011 vcpu->mmio_is_write = 1;
1012 memcpy(vcpu->mmio_data, &val, bytes);
1014 return X86EMUL_CONTINUE;
1017 static int emulator_cmpxchg_emulated(unsigned long addr,
1021 struct x86_emulate_ctxt *ctxt)
1023 static int reported;
1027 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1029 return emulator_write_emulated(addr, new, bytes, ctxt);
1032 #ifdef CONFIG_X86_32
1034 static int emulator_cmpxchg8b_emulated(unsigned long addr,
1035 unsigned long old_lo,
1036 unsigned long old_hi,
1037 unsigned long new_lo,
1038 unsigned long new_hi,
1039 struct x86_emulate_ctxt *ctxt)
1041 static int reported;
1046 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
1048 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
1049 if (r != X86EMUL_CONTINUE)
1051 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
1056 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1058 return kvm_arch_ops->get_segment_base(vcpu, seg);
1061 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1063 return X86EMUL_CONTINUE;
1066 int emulate_clts(struct kvm_vcpu *vcpu)
1070 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1071 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
1072 kvm_arch_ops->set_cr0(vcpu, cr0);
1073 return X86EMUL_CONTINUE;
1076 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1078 struct kvm_vcpu *vcpu = ctxt->vcpu;
1082 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1083 return X86EMUL_CONTINUE;
1085 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1087 return X86EMUL_UNHANDLEABLE;
1091 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1093 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1096 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1098 /* FIXME: better handling */
1099 return X86EMUL_UNHANDLEABLE;
1101 return X86EMUL_CONTINUE;
1104 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1106 static int reported;
1108 unsigned long rip = ctxt->vcpu->rip;
1109 unsigned long rip_linear;
1111 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1116 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1118 printk(KERN_ERR "emulation failed but !mmio_needed?"
1119 " rip %lx %02x %02x %02x %02x\n",
1120 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1124 struct x86_emulate_ops emulate_ops = {
1125 .read_std = emulator_read_std,
1126 .write_std = emulator_write_std,
1127 .read_emulated = emulator_read_emulated,
1128 .write_emulated = emulator_write_emulated,
1129 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1130 #ifdef CONFIG_X86_32
1131 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1135 int emulate_instruction(struct kvm_vcpu *vcpu,
1136 struct kvm_run *run,
1140 struct x86_emulate_ctxt emulate_ctxt;
1144 kvm_arch_ops->cache_regs(vcpu);
1146 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1148 emulate_ctxt.vcpu = vcpu;
1149 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1150 emulate_ctxt.cr2 = cr2;
1151 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1152 ? X86EMUL_MODE_REAL : cs_l
1153 ? X86EMUL_MODE_PROT64 : cs_db
1154 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1156 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1157 emulate_ctxt.cs_base = 0;
1158 emulate_ctxt.ds_base = 0;
1159 emulate_ctxt.es_base = 0;
1160 emulate_ctxt.ss_base = 0;
1162 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1163 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1164 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1165 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1168 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1169 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1171 vcpu->mmio_is_write = 0;
1172 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1174 if ((r || vcpu->mmio_is_write) && run) {
1175 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1176 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1177 run->mmio.len = vcpu->mmio_size;
1178 run->mmio.is_write = vcpu->mmio_is_write;
1182 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1183 return EMULATE_DONE;
1184 if (!vcpu->mmio_needed) {
1185 report_emulation_failure(&emulate_ctxt);
1186 return EMULATE_FAIL;
1188 return EMULATE_DO_MMIO;
1191 kvm_arch_ops->decache_regs(vcpu);
1192 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1194 if (vcpu->mmio_is_write)
1195 return EMULATE_DO_MMIO;
1197 return EMULATE_DONE;
1199 EXPORT_SYMBOL_GPL(emulate_instruction);
1201 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1203 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1205 kvm_arch_ops->cache_regs(vcpu);
1207 #ifdef CONFIG_X86_64
1208 if (is_long_mode(vcpu)) {
1209 nr = vcpu->regs[VCPU_REGS_RAX];
1210 a0 = vcpu->regs[VCPU_REGS_RDI];
1211 a1 = vcpu->regs[VCPU_REGS_RSI];
1212 a2 = vcpu->regs[VCPU_REGS_RDX];
1213 a3 = vcpu->regs[VCPU_REGS_RCX];
1214 a4 = vcpu->regs[VCPU_REGS_R8];
1215 a5 = vcpu->regs[VCPU_REGS_R9];
1219 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1220 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1221 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1222 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1223 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1224 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1225 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1229 run->hypercall.args[0] = a0;
1230 run->hypercall.args[1] = a1;
1231 run->hypercall.args[2] = a2;
1232 run->hypercall.args[3] = a3;
1233 run->hypercall.args[4] = a4;
1234 run->hypercall.args[5] = a5;
1235 run->hypercall.ret = ret;
1236 run->hypercall.longmode = is_long_mode(vcpu);
1237 kvm_arch_ops->decache_regs(vcpu);
1240 vcpu->regs[VCPU_REGS_RAX] = ret;
1241 kvm_arch_ops->decache_regs(vcpu);
1244 EXPORT_SYMBOL_GPL(kvm_hypercall);
1246 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1248 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1251 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1253 struct descriptor_table dt = { limit, base };
1255 kvm_arch_ops->set_gdt(vcpu, &dt);
1258 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1260 struct descriptor_table dt = { limit, base };
1262 kvm_arch_ops->set_idt(vcpu, &dt);
1265 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1266 unsigned long *rflags)
1269 *rflags = kvm_arch_ops->get_rflags(vcpu);
1272 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1274 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1285 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1290 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1291 unsigned long *rflags)
1295 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1296 *rflags = kvm_arch_ops->get_rflags(vcpu);
1305 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1308 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1313 * Register the para guest with the host:
1315 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1317 struct kvm_vcpu_para_state *para_state;
1318 hpa_t para_state_hpa, hypercall_hpa;
1319 struct page *para_state_page;
1320 unsigned char *hypercall;
1321 gpa_t hypercall_gpa;
1323 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1324 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1327 * Needs to be page aligned:
1329 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1332 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1333 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1334 if (is_error_hpa(para_state_hpa))
1337 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1338 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1339 para_state = kmap_atomic(para_state_page, KM_USER0);
1341 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1342 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1344 para_state->host_version = KVM_PARA_API_VERSION;
1346 * We cannot support guests that try to register themselves
1347 * with a newer API version than the host supports:
1349 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1350 para_state->ret = -KVM_EINVAL;
1351 goto err_kunmap_skip;
1354 hypercall_gpa = para_state->hypercall_gpa;
1355 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1356 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1357 if (is_error_hpa(hypercall_hpa)) {
1358 para_state->ret = -KVM_EINVAL;
1359 goto err_kunmap_skip;
1362 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1363 vcpu->para_state_page = para_state_page;
1364 vcpu->para_state_gpa = para_state_gpa;
1365 vcpu->hypercall_gpa = hypercall_gpa;
1367 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1368 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1369 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1370 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1371 kunmap_atomic(hypercall, KM_USER1);
1373 para_state->ret = 0;
1375 kunmap_atomic(para_state, KM_USER0);
1381 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1386 case 0xc0010010: /* SYSCFG */
1387 case 0xc0010015: /* HWCR */
1388 case MSR_IA32_PLATFORM_ID:
1389 case MSR_IA32_P5_MC_ADDR:
1390 case MSR_IA32_P5_MC_TYPE:
1391 case MSR_IA32_MC0_CTL:
1392 case MSR_IA32_MCG_STATUS:
1393 case MSR_IA32_MCG_CAP:
1394 case MSR_IA32_MC0_MISC:
1395 case MSR_IA32_MC0_MISC+4:
1396 case MSR_IA32_MC0_MISC+8:
1397 case MSR_IA32_MC0_MISC+12:
1398 case MSR_IA32_MC0_MISC+16:
1399 case MSR_IA32_UCODE_REV:
1400 case MSR_IA32_PERF_STATUS:
1401 /* MTRR registers */
1403 case 0x200 ... 0x2ff:
1406 case 0xcd: /* fsb frequency */
1409 case MSR_IA32_APICBASE:
1410 data = vcpu->apic_base;
1412 case MSR_IA32_MISC_ENABLE:
1413 data = vcpu->ia32_misc_enable_msr;
1415 #ifdef CONFIG_X86_64
1417 data = vcpu->shadow_efer;
1421 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1427 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1430 * Reads an msr value (of 'msr_index') into 'pdata'.
1431 * Returns 0 on success, non-0 otherwise.
1432 * Assumes vcpu_load() was already called.
1434 static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1436 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1439 #ifdef CONFIG_X86_64
1441 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1443 if (efer & EFER_RESERVED_BITS) {
1444 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1451 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1452 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1457 kvm_arch_ops->set_efer(vcpu, efer);
1460 efer |= vcpu->shadow_efer & EFER_LMA;
1462 vcpu->shadow_efer = efer;
1467 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1470 #ifdef CONFIG_X86_64
1472 set_efer(vcpu, data);
1475 case MSR_IA32_MC0_STATUS:
1476 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1477 __FUNCTION__, data);
1479 case MSR_IA32_MCG_STATUS:
1480 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1481 __FUNCTION__, data);
1483 case MSR_IA32_UCODE_REV:
1484 case MSR_IA32_UCODE_WRITE:
1485 case 0x200 ... 0x2ff: /* MTRRs */
1487 case MSR_IA32_APICBASE:
1488 vcpu->apic_base = data;
1490 case MSR_IA32_MISC_ENABLE:
1491 vcpu->ia32_misc_enable_msr = data;
1494 * This is the 'probe whether the host is KVM' logic:
1496 case MSR_KVM_API_MAGIC:
1497 return vcpu_register_para(vcpu, data);
1500 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1505 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1508 * Writes msr value into into the appropriate "register".
1509 * Returns 0 on success, non-0 otherwise.
1510 * Assumes vcpu_load() was already called.
1512 static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1514 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1517 void kvm_resched(struct kvm_vcpu *vcpu)
1523 EXPORT_SYMBOL_GPL(kvm_resched);
1525 void load_msrs(struct vmx_msr_entry *e, int n)
1529 for (i = 0; i < n; ++i)
1530 wrmsrl(e[i].index, e[i].data);
1532 EXPORT_SYMBOL_GPL(load_msrs);
1534 void save_msrs(struct vmx_msr_entry *e, int n)
1538 for (i = 0; i < n; ++i)
1539 rdmsrl(e[i].index, e[i].data);
1541 EXPORT_SYMBOL_GPL(save_msrs);
1543 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1547 struct kvm_cpuid_entry *e, *best;
1549 kvm_arch_ops->cache_regs(vcpu);
1550 function = vcpu->regs[VCPU_REGS_RAX];
1551 vcpu->regs[VCPU_REGS_RAX] = 0;
1552 vcpu->regs[VCPU_REGS_RBX] = 0;
1553 vcpu->regs[VCPU_REGS_RCX] = 0;
1554 vcpu->regs[VCPU_REGS_RDX] = 0;
1556 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1557 e = &vcpu->cpuid_entries[i];
1558 if (e->function == function) {
1563 * Both basic or both extended?
1565 if (((e->function ^ function) & 0x80000000) == 0)
1566 if (!best || e->function > best->function)
1570 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1571 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1572 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1573 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1575 kvm_arch_ops->decache_regs(vcpu);
1576 kvm_arch_ops->skip_emulated_instruction(vcpu);
1578 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1580 static int pio_copy_data(struct kvm_vcpu *vcpu)
1582 void *p = vcpu->pio_data;
1585 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1587 kvm_arch_ops->vcpu_put(vcpu);
1588 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1591 kvm_arch_ops->vcpu_load(vcpu);
1592 free_pio_guest_pages(vcpu);
1595 q += vcpu->pio.guest_page_offset;
1596 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1598 memcpy(q, p, bytes);
1600 memcpy(p, q, bytes);
1601 q -= vcpu->pio.guest_page_offset;
1603 kvm_arch_ops->vcpu_load(vcpu);
1604 free_pio_guest_pages(vcpu);
1608 static int complete_pio(struct kvm_vcpu *vcpu)
1610 struct kvm_pio_request *io = &vcpu->pio;
1614 kvm_arch_ops->cache_regs(vcpu);
1618 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1622 r = pio_copy_data(vcpu);
1624 kvm_arch_ops->cache_regs(vcpu);
1631 delta *= io->cur_count;
1633 * The size of the register should really depend on
1634 * current address size.
1636 vcpu->regs[VCPU_REGS_RCX] -= delta;
1642 vcpu->regs[VCPU_REGS_RDI] += delta;
1644 vcpu->regs[VCPU_REGS_RSI] += delta;
1647 vcpu->run->io_completed = 0;
1649 kvm_arch_ops->decache_regs(vcpu);
1651 io->count -= io->cur_count;
1655 kvm_arch_ops->skip_emulated_instruction(vcpu);
1659 int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1660 int size, unsigned long count, int string, int down,
1661 gva_t address, int rep, unsigned port)
1663 unsigned now, in_page;
1668 vcpu->run->exit_reason = KVM_EXIT_IO;
1669 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1670 vcpu->run->io.size = size;
1671 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1672 vcpu->run->io.count = count;
1673 vcpu->run->io.port = port;
1674 vcpu->pio.count = count;
1675 vcpu->pio.cur_count = count;
1676 vcpu->pio.size = size;
1678 vcpu->pio.string = string;
1679 vcpu->pio.down = down;
1680 vcpu->pio.guest_page_offset = offset_in_page(address);
1681 vcpu->pio.rep = rep;
1684 kvm_arch_ops->cache_regs(vcpu);
1685 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1686 kvm_arch_ops->decache_regs(vcpu);
1691 kvm_arch_ops->skip_emulated_instruction(vcpu);
1695 now = min(count, PAGE_SIZE / size);
1698 in_page = PAGE_SIZE - offset_in_page(address);
1700 in_page = offset_in_page(address) + size;
1701 now = min(count, (unsigned long)in_page / size);
1704 * String I/O straddles page boundary. Pin two guest pages
1705 * so that we satisfy atomicity constraints. Do just one
1706 * transaction to avoid complexity.
1713 * String I/O in reverse. Yuck. Kill the guest, fix later.
1715 printk(KERN_ERR "kvm: guest string pio down\n");
1719 vcpu->run->io.count = now;
1720 vcpu->pio.cur_count = now;
1722 for (i = 0; i < nr_pages; ++i) {
1723 spin_lock(&vcpu->kvm->lock);
1724 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1727 vcpu->pio.guest_pages[i] = page;
1728 spin_unlock(&vcpu->kvm->lock);
1731 free_pio_guest_pages(vcpu);
1737 return pio_copy_data(vcpu);
1740 EXPORT_SYMBOL_GPL(kvm_setup_pio);
1742 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1749 if (vcpu->sigset_active)
1750 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1752 /* re-sync apic's tpr */
1753 vcpu->cr8 = kvm_run->cr8;
1755 if (kvm_run->io_completed) {
1756 if (vcpu->pio.cur_count) {
1757 r = complete_pio(vcpu);
1761 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1762 vcpu->mmio_read_completed = 1;
1766 vcpu->mmio_needed = 0;
1768 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
1769 kvm_arch_ops->cache_regs(vcpu);
1770 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1771 kvm_arch_ops->decache_regs(vcpu);
1774 r = kvm_arch_ops->run(vcpu, kvm_run);
1777 if (vcpu->sigset_active)
1778 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1784 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1785 struct kvm_regs *regs)
1789 kvm_arch_ops->cache_regs(vcpu);
1791 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1792 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1793 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1794 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1795 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1796 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1797 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1798 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1799 #ifdef CONFIG_X86_64
1800 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1801 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1802 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1803 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1804 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1805 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1806 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1807 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1810 regs->rip = vcpu->rip;
1811 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1814 * Don't leak debug flags in case they were set for guest debugging
1816 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1817 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1824 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1825 struct kvm_regs *regs)
1829 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1830 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1831 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1832 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1833 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1834 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1835 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1836 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1837 #ifdef CONFIG_X86_64
1838 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1839 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1840 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1841 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1842 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1843 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1844 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1845 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1848 vcpu->rip = regs->rip;
1849 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1851 kvm_arch_ops->decache_regs(vcpu);
1858 static void get_segment(struct kvm_vcpu *vcpu,
1859 struct kvm_segment *var, int seg)
1861 return kvm_arch_ops->get_segment(vcpu, var, seg);
1864 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1865 struct kvm_sregs *sregs)
1867 struct descriptor_table dt;
1871 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1872 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1873 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1874 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1875 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1876 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1878 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1879 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1881 kvm_arch_ops->get_idt(vcpu, &dt);
1882 sregs->idt.limit = dt.limit;
1883 sregs->idt.base = dt.base;
1884 kvm_arch_ops->get_gdt(vcpu, &dt);
1885 sregs->gdt.limit = dt.limit;
1886 sregs->gdt.base = dt.base;
1888 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1889 sregs->cr0 = vcpu->cr0;
1890 sregs->cr2 = vcpu->cr2;
1891 sregs->cr3 = vcpu->cr3;
1892 sregs->cr4 = vcpu->cr4;
1893 sregs->cr8 = vcpu->cr8;
1894 sregs->efer = vcpu->shadow_efer;
1895 sregs->apic_base = vcpu->apic_base;
1897 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1898 sizeof sregs->interrupt_bitmap);
1905 static void set_segment(struct kvm_vcpu *vcpu,
1906 struct kvm_segment *var, int seg)
1908 return kvm_arch_ops->set_segment(vcpu, var, seg);
1911 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1912 struct kvm_sregs *sregs)
1914 int mmu_reset_needed = 0;
1916 struct descriptor_table dt;
1920 dt.limit = sregs->idt.limit;
1921 dt.base = sregs->idt.base;
1922 kvm_arch_ops->set_idt(vcpu, &dt);
1923 dt.limit = sregs->gdt.limit;
1924 dt.base = sregs->gdt.base;
1925 kvm_arch_ops->set_gdt(vcpu, &dt);
1927 vcpu->cr2 = sregs->cr2;
1928 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1929 vcpu->cr3 = sregs->cr3;
1931 vcpu->cr8 = sregs->cr8;
1933 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
1934 #ifdef CONFIG_X86_64
1935 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1937 vcpu->apic_base = sregs->apic_base;
1939 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1941 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1942 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
1944 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1945 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1946 if (!is_long_mode(vcpu) && is_pae(vcpu))
1947 load_pdptrs(vcpu, vcpu->cr3);
1949 if (mmu_reset_needed)
1950 kvm_mmu_reset_context(vcpu);
1952 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1953 sizeof vcpu->irq_pending);
1954 vcpu->irq_summary = 0;
1955 for (i = 0; i < NR_IRQ_WORDS; ++i)
1956 if (vcpu->irq_pending[i])
1957 __set_bit(i, &vcpu->irq_summary);
1959 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1960 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1961 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1962 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1963 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1964 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1966 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1967 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1975 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1976 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1978 * This list is modified at module load time to reflect the
1979 * capabilities of the host cpu.
1981 static u32 msrs_to_save[] = {
1982 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1984 #ifdef CONFIG_X86_64
1985 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1987 MSR_IA32_TIME_STAMP_COUNTER,
1990 static unsigned num_msrs_to_save;
1992 static u32 emulated_msrs[] = {
1993 MSR_IA32_MISC_ENABLE,
1996 static __init void kvm_init_msr_list(void)
2001 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2002 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2005 msrs_to_save[j] = msrs_to_save[i];
2008 num_msrs_to_save = j;
2012 * Adapt set_msr() to msr_io()'s calling convention
2014 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2016 return set_msr(vcpu, index, *data);
2020 * Read or write a bunch of msrs. All parameters are kernel addresses.
2022 * @return number of msrs set successfully.
2024 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2025 struct kvm_msr_entry *entries,
2026 int (*do_msr)(struct kvm_vcpu *vcpu,
2027 unsigned index, u64 *data))
2033 for (i = 0; i < msrs->nmsrs; ++i)
2034 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2043 * Read or write a bunch of msrs. Parameters are user addresses.
2045 * @return number of msrs set successfully.
2047 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2048 int (*do_msr)(struct kvm_vcpu *vcpu,
2049 unsigned index, u64 *data),
2052 struct kvm_msrs msrs;
2053 struct kvm_msr_entry *entries;
2058 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2062 if (msrs.nmsrs >= MAX_IO_MSRS)
2066 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2067 entries = vmalloc(size);
2072 if (copy_from_user(entries, user_msrs->entries, size))
2075 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2080 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2092 * Translate a guest virtual address to a guest physical address.
2094 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2095 struct kvm_translation *tr)
2097 unsigned long vaddr = tr->linear_address;
2101 spin_lock(&vcpu->kvm->lock);
2102 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2103 tr->physical_address = gpa;
2104 tr->valid = gpa != UNMAPPED_GVA;
2107 spin_unlock(&vcpu->kvm->lock);
2113 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2114 struct kvm_interrupt *irq)
2116 if (irq->irq < 0 || irq->irq >= 256)
2120 set_bit(irq->irq, vcpu->irq_pending);
2121 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2128 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2129 struct kvm_debug_guest *dbg)
2135 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2142 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2143 unsigned long address,
2146 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2147 unsigned long pgoff;
2150 *type = VM_FAULT_MINOR;
2151 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2153 page = virt_to_page(vcpu->run);
2154 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2155 page = virt_to_page(vcpu->pio_data);
2157 return NOPAGE_SIGBUS;
2162 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2163 .nopage = kvm_vcpu_nopage,
2166 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2168 vma->vm_ops = &kvm_vcpu_vm_ops;
2172 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2174 struct kvm_vcpu *vcpu = filp->private_data;
2176 fput(vcpu->kvm->filp);
2180 static struct file_operations kvm_vcpu_fops = {
2181 .release = kvm_vcpu_release,
2182 .unlocked_ioctl = kvm_vcpu_ioctl,
2183 .compat_ioctl = kvm_vcpu_ioctl,
2184 .mmap = kvm_vcpu_mmap,
2188 * Allocates an inode for the vcpu.
2190 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2193 struct inode *inode;
2196 atomic_inc(&vcpu->kvm->filp->f_count);
2197 inode = kvmfs_inode(&kvm_vcpu_fops);
2198 if (IS_ERR(inode)) {
2203 file = kvmfs_file(inode, vcpu);
2209 r = get_unused_fd();
2213 fd_install(fd, file);
2222 fput(vcpu->kvm->filp);
2227 * Creates some virtual cpus. Good luck creating more than one.
2229 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2232 struct kvm_vcpu *vcpu;
2239 vcpu = &kvm->vcpus[n];
2241 mutex_lock(&vcpu->mutex);
2244 mutex_unlock(&vcpu->mutex);
2248 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2252 vcpu->run = page_address(page);
2254 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2258 vcpu->pio_data = page_address(page);
2260 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2262 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2264 r = kvm_arch_ops->vcpu_create(vcpu);
2266 goto out_free_vcpus;
2268 r = kvm_mmu_create(vcpu);
2270 goto out_free_vcpus;
2272 kvm_arch_ops->vcpu_load(vcpu);
2273 r = kvm_mmu_setup(vcpu);
2275 r = kvm_arch_ops->vcpu_setup(vcpu);
2279 goto out_free_vcpus;
2281 r = create_vcpu_fd(vcpu);
2283 goto out_free_vcpus;
2288 kvm_free_vcpu(vcpu);
2290 free_page((unsigned long)vcpu->run);
2293 mutex_unlock(&vcpu->mutex);
2298 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2299 struct kvm_cpuid *cpuid,
2300 struct kvm_cpuid_entry __user *entries)
2305 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2308 if (copy_from_user(&vcpu->cpuid_entries, entries,
2309 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2311 vcpu->cpuid_nent = cpuid->nent;
2318 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2321 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2322 vcpu->sigset_active = 1;
2323 vcpu->sigset = *sigset;
2325 vcpu->sigset_active = 0;
2329 static long kvm_vcpu_ioctl(struct file *filp,
2330 unsigned int ioctl, unsigned long arg)
2332 struct kvm_vcpu *vcpu = filp->private_data;
2333 void __user *argp = (void __user *)arg;
2341 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2343 case KVM_GET_REGS: {
2344 struct kvm_regs kvm_regs;
2346 memset(&kvm_regs, 0, sizeof kvm_regs);
2347 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2351 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2356 case KVM_SET_REGS: {
2357 struct kvm_regs kvm_regs;
2360 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2362 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2368 case KVM_GET_SREGS: {
2369 struct kvm_sregs kvm_sregs;
2371 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2372 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2376 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2381 case KVM_SET_SREGS: {
2382 struct kvm_sregs kvm_sregs;
2385 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2387 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2393 case KVM_TRANSLATE: {
2394 struct kvm_translation tr;
2397 if (copy_from_user(&tr, argp, sizeof tr))
2399 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2403 if (copy_to_user(argp, &tr, sizeof tr))
2408 case KVM_INTERRUPT: {
2409 struct kvm_interrupt irq;
2412 if (copy_from_user(&irq, argp, sizeof irq))
2414 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2420 case KVM_DEBUG_GUEST: {
2421 struct kvm_debug_guest dbg;
2424 if (copy_from_user(&dbg, argp, sizeof dbg))
2426 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2433 r = msr_io(vcpu, argp, get_msr, 1);
2436 r = msr_io(vcpu, argp, do_set_msr, 0);
2438 case KVM_SET_CPUID: {
2439 struct kvm_cpuid __user *cpuid_arg = argp;
2440 struct kvm_cpuid cpuid;
2443 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2445 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2450 case KVM_SET_SIGNAL_MASK: {
2451 struct kvm_signal_mask __user *sigmask_arg = argp;
2452 struct kvm_signal_mask kvm_sigmask;
2453 sigset_t sigset, *p;
2458 if (copy_from_user(&kvm_sigmask, argp,
2459 sizeof kvm_sigmask))
2462 if (kvm_sigmask.len != sizeof sigset)
2465 if (copy_from_user(&sigset, sigmask_arg->sigset,
2470 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2480 static long kvm_vm_ioctl(struct file *filp,
2481 unsigned int ioctl, unsigned long arg)
2483 struct kvm *kvm = filp->private_data;
2484 void __user *argp = (void __user *)arg;
2488 case KVM_CREATE_VCPU:
2489 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2493 case KVM_SET_MEMORY_REGION: {
2494 struct kvm_memory_region kvm_mem;
2497 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2499 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
2504 case KVM_GET_DIRTY_LOG: {
2505 struct kvm_dirty_log log;
2508 if (copy_from_user(&log, argp, sizeof log))
2510 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2522 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2523 unsigned long address,
2526 struct kvm *kvm = vma->vm_file->private_data;
2527 unsigned long pgoff;
2530 *type = VM_FAULT_MINOR;
2531 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2532 page = gfn_to_page(kvm, pgoff);
2534 return NOPAGE_SIGBUS;
2539 static struct vm_operations_struct kvm_vm_vm_ops = {
2540 .nopage = kvm_vm_nopage,
2543 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2545 vma->vm_ops = &kvm_vm_vm_ops;
2549 static struct file_operations kvm_vm_fops = {
2550 .release = kvm_vm_release,
2551 .unlocked_ioctl = kvm_vm_ioctl,
2552 .compat_ioctl = kvm_vm_ioctl,
2553 .mmap = kvm_vm_mmap,
2556 static int kvm_dev_ioctl_create_vm(void)
2559 struct inode *inode;
2563 inode = kvmfs_inode(&kvm_vm_fops);
2564 if (IS_ERR(inode)) {
2569 kvm = kvm_create_vm();
2575 file = kvmfs_file(inode, kvm);
2582 r = get_unused_fd();
2586 fd_install(fd, file);
2593 kvm_destroy_vm(kvm);
2600 static long kvm_dev_ioctl(struct file *filp,
2601 unsigned int ioctl, unsigned long arg)
2603 void __user *argp = (void __user *)arg;
2607 case KVM_GET_API_VERSION:
2611 r = KVM_API_VERSION;
2617 r = kvm_dev_ioctl_create_vm();
2619 case KVM_GET_MSR_INDEX_LIST: {
2620 struct kvm_msr_list __user *user_msr_list = argp;
2621 struct kvm_msr_list msr_list;
2625 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2628 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2629 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2632 if (n < num_msrs_to_save)
2635 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2636 num_msrs_to_save * sizeof(u32)))
2638 if (copy_to_user(user_msr_list->indices
2639 + num_msrs_to_save * sizeof(u32),
2641 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2646 case KVM_CHECK_EXTENSION:
2648 * No extensions defined at present.
2652 case KVM_GET_VCPU_MMAP_SIZE:
2665 static struct file_operations kvm_chardev_ops = {
2666 .open = kvm_dev_open,
2667 .release = kvm_dev_release,
2668 .unlocked_ioctl = kvm_dev_ioctl,
2669 .compat_ioctl = kvm_dev_ioctl,
2672 static struct miscdevice kvm_dev = {
2678 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2681 if (val == SYS_RESTART) {
2683 * Some (well, at least mine) BIOSes hang on reboot if
2686 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2687 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
2692 static struct notifier_block kvm_reboot_notifier = {
2693 .notifier_call = kvm_reboot,
2698 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2701 static void decache_vcpus_on_cpu(int cpu)
2704 struct kvm_vcpu *vcpu;
2707 spin_lock(&kvm_lock);
2708 list_for_each_entry(vm, &vm_list, vm_list)
2709 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2710 vcpu = &vm->vcpus[i];
2712 * If the vcpu is locked, then it is running on some
2713 * other cpu and therefore it is not cached on the
2716 * If it's not locked, check the last cpu it executed
2719 if (mutex_trylock(&vcpu->mutex)) {
2720 if (vcpu->cpu == cpu) {
2721 kvm_arch_ops->vcpu_decache(vcpu);
2724 mutex_unlock(&vcpu->mutex);
2727 spin_unlock(&kvm_lock);
2730 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2736 case CPU_DOWN_PREPARE:
2737 case CPU_UP_CANCELED:
2738 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2740 decache_vcpus_on_cpu(cpu);
2741 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2745 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2747 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2754 static struct notifier_block kvm_cpu_notifier = {
2755 .notifier_call = kvm_cpu_hotplug,
2756 .priority = 20, /* must be > scheduler priority */
2759 static __init void kvm_init_debug(void)
2761 struct kvm_stats_debugfs_item *p;
2763 debugfs_dir = debugfs_create_dir("kvm", NULL);
2764 for (p = debugfs_entries; p->name; ++p)
2765 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2769 static void kvm_exit_debug(void)
2771 struct kvm_stats_debugfs_item *p;
2773 for (p = debugfs_entries; p->name; ++p)
2774 debugfs_remove(p->dentry);
2775 debugfs_remove(debugfs_dir);
2778 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2780 decache_vcpus_on_cpu(raw_smp_processor_id());
2781 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
2785 static int kvm_resume(struct sys_device *dev)
2787 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
2791 static struct sysdev_class kvm_sysdev_class = {
2792 set_kset_name("kvm"),
2793 .suspend = kvm_suspend,
2794 .resume = kvm_resume,
2797 static struct sys_device kvm_sysdev = {
2799 .cls = &kvm_sysdev_class,
2802 hpa_t bad_page_address;
2804 static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
2805 const char *dev_name, void *data, struct vfsmount *mnt)
2807 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
2810 static struct file_system_type kvm_fs_type = {
2812 .get_sb = kvmfs_get_sb,
2813 .kill_sb = kill_anon_super,
2816 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2821 printk(KERN_ERR "kvm: already loaded the other module\n");
2825 if (!ops->cpu_has_kvm_support()) {
2826 printk(KERN_ERR "kvm: no hardware support\n");
2829 if (ops->disabled_by_bios()) {
2830 printk(KERN_ERR "kvm: disabled by bios\n");
2836 r = kvm_arch_ops->hardware_setup();
2840 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
2841 r = register_cpu_notifier(&kvm_cpu_notifier);
2844 register_reboot_notifier(&kvm_reboot_notifier);
2846 r = sysdev_class_register(&kvm_sysdev_class);
2850 r = sysdev_register(&kvm_sysdev);
2854 kvm_chardev_ops.owner = module;
2856 r = misc_register(&kvm_dev);
2858 printk (KERN_ERR "kvm: misc device register failed\n");
2865 sysdev_unregister(&kvm_sysdev);
2867 sysdev_class_unregister(&kvm_sysdev_class);
2869 unregister_reboot_notifier(&kvm_reboot_notifier);
2870 unregister_cpu_notifier(&kvm_cpu_notifier);
2872 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
2873 kvm_arch_ops->hardware_unsetup();
2875 kvm_arch_ops = NULL;
2879 void kvm_exit_arch(void)
2881 misc_deregister(&kvm_dev);
2882 sysdev_unregister(&kvm_sysdev);
2883 sysdev_class_unregister(&kvm_sysdev_class);
2884 unregister_reboot_notifier(&kvm_reboot_notifier);
2885 unregister_cpu_notifier(&kvm_cpu_notifier);
2886 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
2887 kvm_arch_ops->hardware_unsetup();
2888 kvm_arch_ops = NULL;
2891 static __init int kvm_init(void)
2893 static struct page *bad_page;
2896 r = register_filesystem(&kvm_fs_type);
2900 kvmfs_mnt = kern_mount(&kvm_fs_type);
2901 r = PTR_ERR(kvmfs_mnt);
2902 if (IS_ERR(kvmfs_mnt))
2906 kvm_init_msr_list();
2908 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2913 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2914 memset(__va(bad_page_address), 0, PAGE_SIZE);
2922 unregister_filesystem(&kvm_fs_type);
2927 static __exit void kvm_exit(void)
2930 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
2932 unregister_filesystem(&kvm_fs_type);
2935 module_init(kvm_init)
2936 module_exit(kvm_exit)
2938 EXPORT_SYMBOL_GPL(kvm_init_arch);
2939 EXPORT_SYMBOL_GPL(kvm_exit_arch);