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>
43 #include <linux/sched.h>
45 #include "x86_emulate.h"
46 #include "segment_descriptor.h"
48 MODULE_AUTHOR("Qumranet");
49 MODULE_LICENSE("GPL");
51 static DEFINE_SPINLOCK(kvm_lock);
52 static LIST_HEAD(vm_list);
54 struct kvm_arch_ops *kvm_arch_ops;
56 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
58 static struct kvm_stats_debugfs_item {
61 struct dentry *dentry;
62 } debugfs_entries[] = {
63 { "pf_fixed", STAT_OFFSET(pf_fixed) },
64 { "pf_guest", STAT_OFFSET(pf_guest) },
65 { "tlb_flush", STAT_OFFSET(tlb_flush) },
66 { "invlpg", STAT_OFFSET(invlpg) },
67 { "exits", STAT_OFFSET(exits) },
68 { "io_exits", STAT_OFFSET(io_exits) },
69 { "mmio_exits", STAT_OFFSET(mmio_exits) },
70 { "signal_exits", STAT_OFFSET(signal_exits) },
71 { "irq_window", STAT_OFFSET(irq_window_exits) },
72 { "halt_exits", STAT_OFFSET(halt_exits) },
73 { "request_irq", STAT_OFFSET(request_irq_exits) },
74 { "irq_exits", STAT_OFFSET(irq_exits) },
75 { "light_exits", STAT_OFFSET(light_exits) },
79 static struct dentry *debugfs_dir;
81 struct vfsmount *kvmfs_mnt;
83 #define MAX_IO_MSRS 256
85 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
86 #define LMSW_GUEST_MASK 0x0eULL
87 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
88 #define CR8_RESEVED_BITS (~0x0fULL)
89 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
92 // LDT or TSS descriptor in the GDT. 16 bytes.
93 struct segment_descriptor_64 {
94 struct segment_descriptor s;
101 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
104 static struct inode *kvmfs_inode(struct file_operations *fops)
107 struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
115 * Mark the inode dirty from the very beginning,
116 * that way it will never be moved to the dirty
117 * list because mark_inode_dirty() will think
118 * that it already _is_ on the dirty list.
120 inode->i_state = I_DIRTY;
121 inode->i_mode = S_IRUSR | S_IWUSR;
122 inode->i_uid = current->fsuid;
123 inode->i_gid = current->fsgid;
124 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
128 return ERR_PTR(error);
131 static struct file *kvmfs_file(struct inode *inode, void *private_data)
133 struct file *file = get_empty_filp();
136 return ERR_PTR(-ENFILE);
138 file->f_path.mnt = mntget(kvmfs_mnt);
139 file->f_path.dentry = d_alloc_anon(inode);
140 if (!file->f_path.dentry)
141 return ERR_PTR(-ENOMEM);
142 file->f_mapping = inode->i_mapping;
145 file->f_flags = O_RDWR;
146 file->f_op = inode->i_fop;
147 file->f_mode = FMODE_READ | FMODE_WRITE;
149 file->private_data = private_data;
153 unsigned long segment_base(u16 selector)
155 struct descriptor_table gdt;
156 struct segment_descriptor *d;
157 unsigned long table_base;
158 typedef unsigned long ul;
164 asm ("sgdt %0" : "=m"(gdt));
165 table_base = gdt.base;
167 if (selector & 4) { /* from ldt */
170 asm ("sldt %0" : "=g"(ldt_selector));
171 table_base = segment_base(ldt_selector);
173 d = (struct segment_descriptor *)(table_base + (selector & ~7));
174 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
177 && (d->type == 2 || d->type == 9 || d->type == 11))
178 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
182 EXPORT_SYMBOL_GPL(segment_base);
184 static inline int valid_vcpu(int n)
186 return likely(n >= 0 && n < KVM_MAX_VCPUS);
189 int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
192 unsigned char *host_buf = dest;
193 unsigned long req_size = size;
201 paddr = gva_to_hpa(vcpu, addr);
203 if (is_error_hpa(paddr))
206 guest_buf = (hva_t)kmap_atomic(
207 pfn_to_page(paddr >> PAGE_SHIFT),
209 offset = addr & ~PAGE_MASK;
211 now = min(size, PAGE_SIZE - offset);
212 memcpy(host_buf, (void*)guest_buf, now);
216 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
218 return req_size - size;
220 EXPORT_SYMBOL_GPL(kvm_read_guest);
222 int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
225 unsigned char *host_buf = data;
226 unsigned long req_size = size;
235 paddr = gva_to_hpa(vcpu, addr);
237 if (is_error_hpa(paddr))
240 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
241 mark_page_dirty(vcpu->kvm, gfn);
242 guest_buf = (hva_t)kmap_atomic(
243 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
244 offset = addr & ~PAGE_MASK;
246 now = min(size, PAGE_SIZE - offset);
247 memcpy((void*)guest_buf, host_buf, now);
251 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
253 return req_size - size;
255 EXPORT_SYMBOL_GPL(kvm_write_guest);
257 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
259 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
262 vcpu->guest_fpu_loaded = 1;
263 fx_save(vcpu->host_fx_image);
264 fx_restore(vcpu->guest_fx_image);
266 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
268 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
270 if (!vcpu->guest_fpu_loaded)
273 vcpu->guest_fpu_loaded = 0;
274 fx_save(vcpu->guest_fx_image);
275 fx_restore(vcpu->host_fx_image);
277 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
280 * Switches to specified vcpu, until a matching vcpu_put()
282 static void vcpu_load(struct kvm_vcpu *vcpu)
284 mutex_lock(&vcpu->mutex);
285 kvm_arch_ops->vcpu_load(vcpu);
289 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
290 * if the slot is not populated.
292 static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
294 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
296 mutex_lock(&vcpu->mutex);
298 mutex_unlock(&vcpu->mutex);
301 kvm_arch_ops->vcpu_load(vcpu);
305 static void vcpu_put(struct kvm_vcpu *vcpu)
307 kvm_arch_ops->vcpu_put(vcpu);
308 mutex_unlock(&vcpu->mutex);
311 static struct kvm *kvm_create_vm(void)
313 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
317 return ERR_PTR(-ENOMEM);
319 spin_lock_init(&kvm->lock);
320 INIT_LIST_HEAD(&kvm->active_mmu_pages);
321 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
322 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
324 mutex_init(&vcpu->mutex);
327 vcpu->mmu.root_hpa = INVALID_PAGE;
328 INIT_LIST_HEAD(&vcpu->free_pages);
329 spin_lock(&kvm_lock);
330 list_add(&kvm->vm_list, &vm_list);
331 spin_unlock(&kvm_lock);
336 static int kvm_dev_open(struct inode *inode, struct file *filp)
342 * Free any memory in @free but not in @dont.
344 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
345 struct kvm_memory_slot *dont)
349 if (!dont || free->phys_mem != dont->phys_mem)
350 if (free->phys_mem) {
351 for (i = 0; i < free->npages; ++i)
352 if (free->phys_mem[i])
353 __free_page(free->phys_mem[i]);
354 vfree(free->phys_mem);
357 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
358 vfree(free->dirty_bitmap);
360 free->phys_mem = NULL;
362 free->dirty_bitmap = NULL;
365 static void kvm_free_physmem(struct kvm *kvm)
369 for (i = 0; i < kvm->nmemslots; ++i)
370 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
373 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
377 for (i = 0; i < 2; ++i)
378 if (vcpu->pio.guest_pages[i]) {
379 __free_page(vcpu->pio.guest_pages[i]);
380 vcpu->pio.guest_pages[i] = NULL;
384 static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
390 kvm_mmu_destroy(vcpu);
392 kvm_arch_ops->vcpu_free(vcpu);
393 free_page((unsigned long)vcpu->run);
395 free_page((unsigned long)vcpu->pio_data);
396 vcpu->pio_data = NULL;
397 free_pio_guest_pages(vcpu);
400 static void kvm_free_vcpus(struct kvm *kvm)
404 for (i = 0; i < KVM_MAX_VCPUS; ++i)
405 kvm_free_vcpu(&kvm->vcpus[i]);
408 static int kvm_dev_release(struct inode *inode, struct file *filp)
413 static void kvm_destroy_vm(struct kvm *kvm)
415 spin_lock(&kvm_lock);
416 list_del(&kvm->vm_list);
417 spin_unlock(&kvm_lock);
419 kvm_free_physmem(kvm);
423 static int kvm_vm_release(struct inode *inode, struct file *filp)
425 struct kvm *kvm = filp->private_data;
431 static void inject_gp(struct kvm_vcpu *vcpu)
433 kvm_arch_ops->inject_gp(vcpu, 0);
437 * Load the pae pdptrs. Return true is they are all valid.
439 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
441 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
442 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
449 spin_lock(&vcpu->kvm->lock);
450 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
451 /* FIXME: !page - emulate? 0xff? */
452 pdpt = kmap_atomic(page, KM_USER0);
455 for (i = 0; i < 4; ++i) {
456 pdpte = pdpt[offset + i];
457 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
463 for (i = 0; i < 4; ++i)
464 vcpu->pdptrs[i] = pdpt[offset + i];
467 kunmap_atomic(pdpt, KM_USER0);
468 spin_unlock(&vcpu->kvm->lock);
473 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
475 if (cr0 & CR0_RESEVED_BITS) {
476 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
482 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
483 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
488 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
489 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
490 "and a clear PE flag\n");
495 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
497 if ((vcpu->shadow_efer & EFER_LME)) {
501 printk(KERN_DEBUG "set_cr0: #GP, start paging "
502 "in long mode while PAE is disabled\n");
506 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
508 printk(KERN_DEBUG "set_cr0: #GP, start paging "
509 "in long mode while CS.L == 1\n");
516 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
517 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
525 kvm_arch_ops->set_cr0(vcpu, cr0);
528 spin_lock(&vcpu->kvm->lock);
529 kvm_mmu_reset_context(vcpu);
530 spin_unlock(&vcpu->kvm->lock);
533 EXPORT_SYMBOL_GPL(set_cr0);
535 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
537 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
539 EXPORT_SYMBOL_GPL(lmsw);
541 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
543 if (cr4 & CR4_RESEVED_BITS) {
544 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
549 if (is_long_mode(vcpu)) {
550 if (!(cr4 & CR4_PAE_MASK)) {
551 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
556 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
557 && !load_pdptrs(vcpu, vcpu->cr3)) {
558 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
562 if (cr4 & CR4_VMXE_MASK) {
563 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
567 kvm_arch_ops->set_cr4(vcpu, cr4);
568 spin_lock(&vcpu->kvm->lock);
569 kvm_mmu_reset_context(vcpu);
570 spin_unlock(&vcpu->kvm->lock);
572 EXPORT_SYMBOL_GPL(set_cr4);
574 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
576 if (is_long_mode(vcpu)) {
577 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
578 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
583 if (cr3 & CR3_RESEVED_BITS) {
584 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
588 if (is_paging(vcpu) && is_pae(vcpu) &&
589 !load_pdptrs(vcpu, cr3)) {
590 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
598 spin_lock(&vcpu->kvm->lock);
600 * Does the new cr3 value map to physical memory? (Note, we
601 * catch an invalid cr3 even in real-mode, because it would
602 * cause trouble later on when we turn on paging anyway.)
604 * A real CPU would silently accept an invalid cr3 and would
605 * attempt to use it - with largely undefined (and often hard
606 * to debug) behavior on the guest side.
608 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
611 vcpu->mmu.new_cr3(vcpu);
612 spin_unlock(&vcpu->kvm->lock);
614 EXPORT_SYMBOL_GPL(set_cr3);
616 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
618 if ( cr8 & CR8_RESEVED_BITS) {
619 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
625 EXPORT_SYMBOL_GPL(set_cr8);
627 void fx_init(struct kvm_vcpu *vcpu)
629 struct __attribute__ ((__packed__)) fx_image_s {
635 u64 operand;// fpu dp
641 fx_save(vcpu->host_fx_image);
643 fx_save(vcpu->guest_fx_image);
644 fx_restore(vcpu->host_fx_image);
646 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
647 fx_image->mxcsr = 0x1f80;
648 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
649 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
651 EXPORT_SYMBOL_GPL(fx_init);
653 static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
655 spin_lock(&vcpu->kvm->lock);
656 kvm_mmu_slot_remove_write_access(vcpu, slot);
657 spin_unlock(&vcpu->kvm->lock);
661 * Allocate some memory and give it an address in the guest physical address
664 * Discontiguous memory is allowed, mostly for framebuffers.
666 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
667 struct kvm_memory_region *mem)
671 unsigned long npages;
673 struct kvm_memory_slot *memslot;
674 struct kvm_memory_slot old, new;
675 int memory_config_version;
678 /* General sanity checks */
679 if (mem->memory_size & (PAGE_SIZE - 1))
681 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
683 if (mem->slot >= KVM_MEMORY_SLOTS)
685 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
688 memslot = &kvm->memslots[mem->slot];
689 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
690 npages = mem->memory_size >> PAGE_SHIFT;
693 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
696 spin_lock(&kvm->lock);
698 memory_config_version = kvm->memory_config_version;
699 new = old = *memslot;
701 new.base_gfn = base_gfn;
703 new.flags = mem->flags;
705 /* Disallow changing a memory slot's size. */
707 if (npages && old.npages && npages != old.npages)
710 /* Check for overlaps */
712 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
713 struct kvm_memory_slot *s = &kvm->memslots[i];
717 if (!((base_gfn + npages <= s->base_gfn) ||
718 (base_gfn >= s->base_gfn + s->npages)))
722 * Do memory allocations outside lock. memory_config_version will
725 spin_unlock(&kvm->lock);
727 /* Deallocate if slot is being removed */
731 /* Free page dirty bitmap if unneeded */
732 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
733 new.dirty_bitmap = NULL;
737 /* Allocate if a slot is being created */
738 if (npages && !new.phys_mem) {
739 new.phys_mem = vmalloc(npages * sizeof(struct page *));
744 memset(new.phys_mem, 0, npages * sizeof(struct page *));
745 for (i = 0; i < npages; ++i) {
746 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
748 if (!new.phys_mem[i])
750 set_page_private(new.phys_mem[i],0);
754 /* Allocate page dirty bitmap if needed */
755 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
756 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
758 new.dirty_bitmap = vmalloc(dirty_bytes);
759 if (!new.dirty_bitmap)
761 memset(new.dirty_bitmap, 0, dirty_bytes);
764 spin_lock(&kvm->lock);
766 if (memory_config_version != kvm->memory_config_version) {
767 spin_unlock(&kvm->lock);
768 kvm_free_physmem_slot(&new, &old);
776 if (mem->slot >= kvm->nmemslots)
777 kvm->nmemslots = mem->slot + 1;
780 ++kvm->memory_config_version;
782 spin_unlock(&kvm->lock);
784 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
785 struct kvm_vcpu *vcpu;
787 vcpu = vcpu_load_slot(kvm, i);
790 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
791 do_remove_write_access(vcpu, mem->slot);
792 kvm_mmu_reset_context(vcpu);
796 kvm_free_physmem_slot(&old, &new);
800 spin_unlock(&kvm->lock);
802 kvm_free_physmem_slot(&new, &old);
808 * Get (and clear) the dirty memory log for a memory slot.
810 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
811 struct kvm_dirty_log *log)
813 struct kvm_memory_slot *memslot;
817 unsigned long any = 0;
819 spin_lock(&kvm->lock);
822 * Prevent changes to guest memory configuration even while the lock
826 spin_unlock(&kvm->lock);
828 if (log->slot >= KVM_MEMORY_SLOTS)
831 memslot = &kvm->memslots[log->slot];
833 if (!memslot->dirty_bitmap)
836 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
838 for (i = 0; !any && i < n/sizeof(long); ++i)
839 any = memslot->dirty_bitmap[i];
842 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
847 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
848 struct kvm_vcpu *vcpu;
850 vcpu = vcpu_load_slot(kvm, i);
854 do_remove_write_access(vcpu, log->slot);
855 memset(memslot->dirty_bitmap, 0, n);
858 kvm_arch_ops->tlb_flush(vcpu);
866 spin_lock(&kvm->lock);
868 spin_unlock(&kvm->lock);
873 * Set a new alias region. Aliases map a portion of physical memory into
874 * another portion. This is useful for memory windows, for example the PC
877 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
878 struct kvm_memory_alias *alias)
881 struct kvm_mem_alias *p;
884 /* General sanity checks */
885 if (alias->memory_size & (PAGE_SIZE - 1))
887 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
889 if (alias->slot >= KVM_ALIAS_SLOTS)
891 if (alias->guest_phys_addr + alias->memory_size
892 < alias->guest_phys_addr)
894 if (alias->target_phys_addr + alias->memory_size
895 < alias->target_phys_addr)
898 spin_lock(&kvm->lock);
900 p = &kvm->aliases[alias->slot];
901 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
902 p->npages = alias->memory_size >> PAGE_SHIFT;
903 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
905 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
906 if (kvm->aliases[n - 1].npages)
910 spin_unlock(&kvm->lock);
912 vcpu_load(&kvm->vcpus[0]);
913 spin_lock(&kvm->lock);
914 kvm_mmu_zap_all(&kvm->vcpus[0]);
915 spin_unlock(&kvm->lock);
916 vcpu_put(&kvm->vcpus[0]);
924 static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
927 struct kvm_mem_alias *alias;
929 for (i = 0; i < kvm->naliases; ++i) {
930 alias = &kvm->aliases[i];
931 if (gfn >= alias->base_gfn
932 && gfn < alias->base_gfn + alias->npages)
933 return alias->target_gfn + gfn - alias->base_gfn;
938 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
942 for (i = 0; i < kvm->nmemslots; ++i) {
943 struct kvm_memory_slot *memslot = &kvm->memslots[i];
945 if (gfn >= memslot->base_gfn
946 && gfn < memslot->base_gfn + memslot->npages)
952 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
954 gfn = unalias_gfn(kvm, gfn);
955 return __gfn_to_memslot(kvm, gfn);
958 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
960 struct kvm_memory_slot *slot;
962 gfn = unalias_gfn(kvm, gfn);
963 slot = __gfn_to_memslot(kvm, gfn);
966 return slot->phys_mem[gfn - slot->base_gfn];
968 EXPORT_SYMBOL_GPL(gfn_to_page);
970 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
973 struct kvm_memory_slot *memslot = NULL;
974 unsigned long rel_gfn;
976 for (i = 0; i < kvm->nmemslots; ++i) {
977 memslot = &kvm->memslots[i];
979 if (gfn >= memslot->base_gfn
980 && gfn < memslot->base_gfn + memslot->npages) {
982 if (!memslot || !memslot->dirty_bitmap)
985 rel_gfn = gfn - memslot->base_gfn;
988 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
989 set_bit(rel_gfn, memslot->dirty_bitmap);
995 static int emulator_read_std(unsigned long addr,
998 struct x86_emulate_ctxt *ctxt)
1000 struct kvm_vcpu *vcpu = ctxt->vcpu;
1004 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1005 unsigned offset = addr & (PAGE_SIZE-1);
1006 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1011 if (gpa == UNMAPPED_GVA)
1012 return X86EMUL_PROPAGATE_FAULT;
1013 pfn = gpa >> PAGE_SHIFT;
1014 page = gfn_to_page(vcpu->kvm, pfn);
1016 return X86EMUL_UNHANDLEABLE;
1017 page_virt = kmap_atomic(page, KM_USER0);
1019 memcpy(data, page_virt + offset, tocopy);
1021 kunmap_atomic(page_virt, KM_USER0);
1028 return X86EMUL_CONTINUE;
1031 static int emulator_write_std(unsigned long addr,
1034 struct x86_emulate_ctxt *ctxt)
1036 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1038 return X86EMUL_UNHANDLEABLE;
1041 static int emulator_read_emulated(unsigned long addr,
1044 struct x86_emulate_ctxt *ctxt)
1046 struct kvm_vcpu *vcpu = ctxt->vcpu;
1048 if (vcpu->mmio_read_completed) {
1049 memcpy(val, vcpu->mmio_data, bytes);
1050 vcpu->mmio_read_completed = 0;
1051 return X86EMUL_CONTINUE;
1052 } else if (emulator_read_std(addr, val, bytes, ctxt)
1053 == X86EMUL_CONTINUE)
1054 return X86EMUL_CONTINUE;
1056 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1058 if (gpa == UNMAPPED_GVA)
1059 return X86EMUL_PROPAGATE_FAULT;
1060 vcpu->mmio_needed = 1;
1061 vcpu->mmio_phys_addr = gpa;
1062 vcpu->mmio_size = bytes;
1063 vcpu->mmio_is_write = 0;
1065 return X86EMUL_UNHANDLEABLE;
1069 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1070 const void *val, int bytes)
1075 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1077 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1080 kvm_mmu_pre_write(vcpu, gpa, bytes);
1081 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
1082 virt = kmap_atomic(page, KM_USER0);
1083 memcpy(virt + offset_in_page(gpa), val, bytes);
1084 kunmap_atomic(virt, KM_USER0);
1085 kvm_mmu_post_write(vcpu, gpa, bytes);
1089 static int emulator_write_emulated(unsigned long addr,
1092 struct x86_emulate_ctxt *ctxt)
1094 struct kvm_vcpu *vcpu = ctxt->vcpu;
1095 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1097 if (gpa == UNMAPPED_GVA) {
1098 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
1099 return X86EMUL_PROPAGATE_FAULT;
1102 if (emulator_write_phys(vcpu, gpa, val, bytes))
1103 return X86EMUL_CONTINUE;
1105 vcpu->mmio_needed = 1;
1106 vcpu->mmio_phys_addr = gpa;
1107 vcpu->mmio_size = bytes;
1108 vcpu->mmio_is_write = 1;
1109 memcpy(vcpu->mmio_data, val, bytes);
1111 return X86EMUL_CONTINUE;
1114 static int emulator_cmpxchg_emulated(unsigned long addr,
1118 struct x86_emulate_ctxt *ctxt)
1120 static int reported;
1124 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1126 return emulator_write_emulated(addr, new, bytes, ctxt);
1129 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1131 return kvm_arch_ops->get_segment_base(vcpu, seg);
1134 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1136 return X86EMUL_CONTINUE;
1139 int emulate_clts(struct kvm_vcpu *vcpu)
1143 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
1144 kvm_arch_ops->set_cr0(vcpu, cr0);
1145 return X86EMUL_CONTINUE;
1148 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1150 struct kvm_vcpu *vcpu = ctxt->vcpu;
1154 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1155 return X86EMUL_CONTINUE;
1157 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1159 return X86EMUL_UNHANDLEABLE;
1163 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1165 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1168 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1170 /* FIXME: better handling */
1171 return X86EMUL_UNHANDLEABLE;
1173 return X86EMUL_CONTINUE;
1176 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1178 static int reported;
1180 unsigned long rip = ctxt->vcpu->rip;
1181 unsigned long rip_linear;
1183 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1188 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1190 printk(KERN_ERR "emulation failed but !mmio_needed?"
1191 " rip %lx %02x %02x %02x %02x\n",
1192 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1196 struct x86_emulate_ops emulate_ops = {
1197 .read_std = emulator_read_std,
1198 .write_std = emulator_write_std,
1199 .read_emulated = emulator_read_emulated,
1200 .write_emulated = emulator_write_emulated,
1201 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1204 int emulate_instruction(struct kvm_vcpu *vcpu,
1205 struct kvm_run *run,
1209 struct x86_emulate_ctxt emulate_ctxt;
1213 vcpu->mmio_fault_cr2 = cr2;
1214 kvm_arch_ops->cache_regs(vcpu);
1216 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1218 emulate_ctxt.vcpu = vcpu;
1219 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1220 emulate_ctxt.cr2 = cr2;
1221 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1222 ? X86EMUL_MODE_REAL : cs_l
1223 ? X86EMUL_MODE_PROT64 : cs_db
1224 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1226 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1227 emulate_ctxt.cs_base = 0;
1228 emulate_ctxt.ds_base = 0;
1229 emulate_ctxt.es_base = 0;
1230 emulate_ctxt.ss_base = 0;
1232 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1233 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1234 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1235 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1238 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1239 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1241 vcpu->mmio_is_write = 0;
1242 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1244 if ((r || vcpu->mmio_is_write) && run) {
1245 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1246 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1247 run->mmio.len = vcpu->mmio_size;
1248 run->mmio.is_write = vcpu->mmio_is_write;
1252 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1253 return EMULATE_DONE;
1254 if (!vcpu->mmio_needed) {
1255 report_emulation_failure(&emulate_ctxt);
1256 return EMULATE_FAIL;
1258 return EMULATE_DO_MMIO;
1261 kvm_arch_ops->decache_regs(vcpu);
1262 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1264 if (vcpu->mmio_is_write) {
1265 vcpu->mmio_needed = 0;
1266 return EMULATE_DO_MMIO;
1269 return EMULATE_DONE;
1271 EXPORT_SYMBOL_GPL(emulate_instruction);
1273 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1275 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1277 kvm_arch_ops->cache_regs(vcpu);
1279 #ifdef CONFIG_X86_64
1280 if (is_long_mode(vcpu)) {
1281 nr = vcpu->regs[VCPU_REGS_RAX];
1282 a0 = vcpu->regs[VCPU_REGS_RDI];
1283 a1 = vcpu->regs[VCPU_REGS_RSI];
1284 a2 = vcpu->regs[VCPU_REGS_RDX];
1285 a3 = vcpu->regs[VCPU_REGS_RCX];
1286 a4 = vcpu->regs[VCPU_REGS_R8];
1287 a5 = vcpu->regs[VCPU_REGS_R9];
1291 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1292 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1293 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1294 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1295 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1296 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1297 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1301 run->hypercall.args[0] = a0;
1302 run->hypercall.args[1] = a1;
1303 run->hypercall.args[2] = a2;
1304 run->hypercall.args[3] = a3;
1305 run->hypercall.args[4] = a4;
1306 run->hypercall.args[5] = a5;
1307 run->hypercall.ret = ret;
1308 run->hypercall.longmode = is_long_mode(vcpu);
1309 kvm_arch_ops->decache_regs(vcpu);
1312 vcpu->regs[VCPU_REGS_RAX] = ret;
1313 kvm_arch_ops->decache_regs(vcpu);
1316 EXPORT_SYMBOL_GPL(kvm_hypercall);
1318 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1320 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1323 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1325 struct descriptor_table dt = { limit, base };
1327 kvm_arch_ops->set_gdt(vcpu, &dt);
1330 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1332 struct descriptor_table dt = { limit, base };
1334 kvm_arch_ops->set_idt(vcpu, &dt);
1337 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1338 unsigned long *rflags)
1341 *rflags = kvm_arch_ops->get_rflags(vcpu);
1344 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1346 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
1357 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1362 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1363 unsigned long *rflags)
1367 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1368 *rflags = kvm_arch_ops->get_rflags(vcpu);
1377 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1380 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1385 * Register the para guest with the host:
1387 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1389 struct kvm_vcpu_para_state *para_state;
1390 hpa_t para_state_hpa, hypercall_hpa;
1391 struct page *para_state_page;
1392 unsigned char *hypercall;
1393 gpa_t hypercall_gpa;
1395 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1396 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1399 * Needs to be page aligned:
1401 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1404 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1405 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1406 if (is_error_hpa(para_state_hpa))
1409 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1410 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1411 para_state = kmap_atomic(para_state_page, KM_USER0);
1413 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1414 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1416 para_state->host_version = KVM_PARA_API_VERSION;
1418 * We cannot support guests that try to register themselves
1419 * with a newer API version than the host supports:
1421 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1422 para_state->ret = -KVM_EINVAL;
1423 goto err_kunmap_skip;
1426 hypercall_gpa = para_state->hypercall_gpa;
1427 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1428 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1429 if (is_error_hpa(hypercall_hpa)) {
1430 para_state->ret = -KVM_EINVAL;
1431 goto err_kunmap_skip;
1434 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1435 vcpu->para_state_page = para_state_page;
1436 vcpu->para_state_gpa = para_state_gpa;
1437 vcpu->hypercall_gpa = hypercall_gpa;
1439 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1440 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1441 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1442 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1443 kunmap_atomic(hypercall, KM_USER1);
1445 para_state->ret = 0;
1447 kunmap_atomic(para_state, KM_USER0);
1453 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1458 case 0xc0010010: /* SYSCFG */
1459 case 0xc0010015: /* HWCR */
1460 case MSR_IA32_PLATFORM_ID:
1461 case MSR_IA32_P5_MC_ADDR:
1462 case MSR_IA32_P5_MC_TYPE:
1463 case MSR_IA32_MC0_CTL:
1464 case MSR_IA32_MCG_STATUS:
1465 case MSR_IA32_MCG_CAP:
1466 case MSR_IA32_MC0_MISC:
1467 case MSR_IA32_MC0_MISC+4:
1468 case MSR_IA32_MC0_MISC+8:
1469 case MSR_IA32_MC0_MISC+12:
1470 case MSR_IA32_MC0_MISC+16:
1471 case MSR_IA32_UCODE_REV:
1472 case MSR_IA32_PERF_STATUS:
1473 /* MTRR registers */
1475 case 0x200 ... 0x2ff:
1478 case 0xcd: /* fsb frequency */
1481 case MSR_IA32_APICBASE:
1482 data = vcpu->apic_base;
1484 case MSR_IA32_MISC_ENABLE:
1485 data = vcpu->ia32_misc_enable_msr;
1487 #ifdef CONFIG_X86_64
1489 data = vcpu->shadow_efer;
1493 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1499 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1502 * Reads an msr value (of 'msr_index') into 'pdata'.
1503 * Returns 0 on success, non-0 otherwise.
1504 * Assumes vcpu_load() was already called.
1506 static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1508 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1511 #ifdef CONFIG_X86_64
1513 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1515 if (efer & EFER_RESERVED_BITS) {
1516 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1523 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1524 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1529 kvm_arch_ops->set_efer(vcpu, efer);
1532 efer |= vcpu->shadow_efer & EFER_LMA;
1534 vcpu->shadow_efer = efer;
1539 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1542 #ifdef CONFIG_X86_64
1544 set_efer(vcpu, data);
1547 case MSR_IA32_MC0_STATUS:
1548 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1549 __FUNCTION__, data);
1551 case MSR_IA32_MCG_STATUS:
1552 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1553 __FUNCTION__, data);
1555 case MSR_IA32_UCODE_REV:
1556 case MSR_IA32_UCODE_WRITE:
1557 case 0x200 ... 0x2ff: /* MTRRs */
1559 case MSR_IA32_APICBASE:
1560 vcpu->apic_base = data;
1562 case MSR_IA32_MISC_ENABLE:
1563 vcpu->ia32_misc_enable_msr = data;
1566 * This is the 'probe whether the host is KVM' logic:
1568 case MSR_KVM_API_MAGIC:
1569 return vcpu_register_para(vcpu, data);
1572 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1577 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1580 * Writes msr value into into the appropriate "register".
1581 * Returns 0 on success, non-0 otherwise.
1582 * Assumes vcpu_load() was already called.
1584 static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1586 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1589 void kvm_resched(struct kvm_vcpu *vcpu)
1591 if (!need_resched())
1597 EXPORT_SYMBOL_GPL(kvm_resched);
1599 void load_msrs(struct vmx_msr_entry *e, int n)
1603 for (i = 0; i < n; ++i)
1604 wrmsrl(e[i].index, e[i].data);
1606 EXPORT_SYMBOL_GPL(load_msrs);
1608 void save_msrs(struct vmx_msr_entry *e, int n)
1612 for (i = 0; i < n; ++i)
1613 rdmsrl(e[i].index, e[i].data);
1615 EXPORT_SYMBOL_GPL(save_msrs);
1617 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1621 struct kvm_cpuid_entry *e, *best;
1623 kvm_arch_ops->cache_regs(vcpu);
1624 function = vcpu->regs[VCPU_REGS_RAX];
1625 vcpu->regs[VCPU_REGS_RAX] = 0;
1626 vcpu->regs[VCPU_REGS_RBX] = 0;
1627 vcpu->regs[VCPU_REGS_RCX] = 0;
1628 vcpu->regs[VCPU_REGS_RDX] = 0;
1630 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1631 e = &vcpu->cpuid_entries[i];
1632 if (e->function == function) {
1637 * Both basic or both extended?
1639 if (((e->function ^ function) & 0x80000000) == 0)
1640 if (!best || e->function > best->function)
1644 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1645 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1646 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1647 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1649 kvm_arch_ops->decache_regs(vcpu);
1650 kvm_arch_ops->skip_emulated_instruction(vcpu);
1652 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1654 static int pio_copy_data(struct kvm_vcpu *vcpu)
1656 void *p = vcpu->pio_data;
1659 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1661 kvm_arch_ops->vcpu_put(vcpu);
1662 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1665 kvm_arch_ops->vcpu_load(vcpu);
1666 free_pio_guest_pages(vcpu);
1669 q += vcpu->pio.guest_page_offset;
1670 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1672 memcpy(q, p, bytes);
1674 memcpy(p, q, bytes);
1675 q -= vcpu->pio.guest_page_offset;
1677 kvm_arch_ops->vcpu_load(vcpu);
1678 free_pio_guest_pages(vcpu);
1682 static int complete_pio(struct kvm_vcpu *vcpu)
1684 struct kvm_pio_request *io = &vcpu->pio;
1688 kvm_arch_ops->cache_regs(vcpu);
1692 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1696 r = pio_copy_data(vcpu);
1698 kvm_arch_ops->cache_regs(vcpu);
1705 delta *= io->cur_count;
1707 * The size of the register should really depend on
1708 * current address size.
1710 vcpu->regs[VCPU_REGS_RCX] -= delta;
1716 vcpu->regs[VCPU_REGS_RDI] += delta;
1718 vcpu->regs[VCPU_REGS_RSI] += delta;
1721 kvm_arch_ops->decache_regs(vcpu);
1723 io->count -= io->cur_count;
1727 kvm_arch_ops->skip_emulated_instruction(vcpu);
1731 int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1732 int size, unsigned long count, int string, int down,
1733 gva_t address, int rep, unsigned port)
1735 unsigned now, in_page;
1740 vcpu->run->exit_reason = KVM_EXIT_IO;
1741 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1742 vcpu->run->io.size = size;
1743 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1744 vcpu->run->io.count = count;
1745 vcpu->run->io.port = port;
1746 vcpu->pio.count = count;
1747 vcpu->pio.cur_count = count;
1748 vcpu->pio.size = size;
1750 vcpu->pio.string = string;
1751 vcpu->pio.down = down;
1752 vcpu->pio.guest_page_offset = offset_in_page(address);
1753 vcpu->pio.rep = rep;
1756 kvm_arch_ops->cache_regs(vcpu);
1757 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1758 kvm_arch_ops->decache_regs(vcpu);
1763 kvm_arch_ops->skip_emulated_instruction(vcpu);
1767 now = min(count, PAGE_SIZE / size);
1770 in_page = PAGE_SIZE - offset_in_page(address);
1772 in_page = offset_in_page(address) + size;
1773 now = min(count, (unsigned long)in_page / size);
1776 * String I/O straddles page boundary. Pin two guest pages
1777 * so that we satisfy atomicity constraints. Do just one
1778 * transaction to avoid complexity.
1785 * String I/O in reverse. Yuck. Kill the guest, fix later.
1787 printk(KERN_ERR "kvm: guest string pio down\n");
1791 vcpu->run->io.count = now;
1792 vcpu->pio.cur_count = now;
1794 for (i = 0; i < nr_pages; ++i) {
1795 spin_lock(&vcpu->kvm->lock);
1796 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1799 vcpu->pio.guest_pages[i] = page;
1800 spin_unlock(&vcpu->kvm->lock);
1803 free_pio_guest_pages(vcpu);
1809 return pio_copy_data(vcpu);
1812 EXPORT_SYMBOL_GPL(kvm_setup_pio);
1814 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1821 if (vcpu->sigset_active)
1822 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1824 /* re-sync apic's tpr */
1825 vcpu->cr8 = kvm_run->cr8;
1827 if (vcpu->pio.cur_count) {
1828 r = complete_pio(vcpu);
1833 if (vcpu->mmio_needed) {
1834 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1835 vcpu->mmio_read_completed = 1;
1836 vcpu->mmio_needed = 0;
1837 r = emulate_instruction(vcpu, kvm_run,
1838 vcpu->mmio_fault_cr2, 0);
1839 if (r == EMULATE_DO_MMIO) {
1841 * Read-modify-write. Back to userspace.
1843 kvm_run->exit_reason = KVM_EXIT_MMIO;
1849 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
1850 kvm_arch_ops->cache_regs(vcpu);
1851 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1852 kvm_arch_ops->decache_regs(vcpu);
1855 r = kvm_arch_ops->run(vcpu, kvm_run);
1858 if (vcpu->sigset_active)
1859 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1865 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1866 struct kvm_regs *regs)
1870 kvm_arch_ops->cache_regs(vcpu);
1872 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1873 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1874 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1875 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1876 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1877 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1878 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1879 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1880 #ifdef CONFIG_X86_64
1881 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1882 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1883 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1884 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1885 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1886 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1887 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1888 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1891 regs->rip = vcpu->rip;
1892 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1895 * Don't leak debug flags in case they were set for guest debugging
1897 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1898 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1905 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1906 struct kvm_regs *regs)
1910 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1911 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1912 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1913 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1914 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1915 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1916 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1917 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1918 #ifdef CONFIG_X86_64
1919 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1920 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1921 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1922 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1923 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1924 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1925 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1926 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1929 vcpu->rip = regs->rip;
1930 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1932 kvm_arch_ops->decache_regs(vcpu);
1939 static void get_segment(struct kvm_vcpu *vcpu,
1940 struct kvm_segment *var, int seg)
1942 return kvm_arch_ops->get_segment(vcpu, var, seg);
1945 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1946 struct kvm_sregs *sregs)
1948 struct descriptor_table dt;
1952 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1953 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1954 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1955 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1956 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1957 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1959 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1960 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1962 kvm_arch_ops->get_idt(vcpu, &dt);
1963 sregs->idt.limit = dt.limit;
1964 sregs->idt.base = dt.base;
1965 kvm_arch_ops->get_gdt(vcpu, &dt);
1966 sregs->gdt.limit = dt.limit;
1967 sregs->gdt.base = dt.base;
1969 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
1970 sregs->cr0 = vcpu->cr0;
1971 sregs->cr2 = vcpu->cr2;
1972 sregs->cr3 = vcpu->cr3;
1973 sregs->cr4 = vcpu->cr4;
1974 sregs->cr8 = vcpu->cr8;
1975 sregs->efer = vcpu->shadow_efer;
1976 sregs->apic_base = vcpu->apic_base;
1978 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1979 sizeof sregs->interrupt_bitmap);
1986 static void set_segment(struct kvm_vcpu *vcpu,
1987 struct kvm_segment *var, int seg)
1989 return kvm_arch_ops->set_segment(vcpu, var, seg);
1992 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1993 struct kvm_sregs *sregs)
1995 int mmu_reset_needed = 0;
1997 struct descriptor_table dt;
2001 dt.limit = sregs->idt.limit;
2002 dt.base = sregs->idt.base;
2003 kvm_arch_ops->set_idt(vcpu, &dt);
2004 dt.limit = sregs->gdt.limit;
2005 dt.base = sregs->gdt.base;
2006 kvm_arch_ops->set_gdt(vcpu, &dt);
2008 vcpu->cr2 = sregs->cr2;
2009 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2010 vcpu->cr3 = sregs->cr3;
2012 vcpu->cr8 = sregs->cr8;
2014 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2015 #ifdef CONFIG_X86_64
2016 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2018 vcpu->apic_base = sregs->apic_base;
2020 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2022 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2023 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
2025 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2026 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
2027 if (!is_long_mode(vcpu) && is_pae(vcpu))
2028 load_pdptrs(vcpu, vcpu->cr3);
2030 if (mmu_reset_needed)
2031 kvm_mmu_reset_context(vcpu);
2033 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2034 sizeof vcpu->irq_pending);
2035 vcpu->irq_summary = 0;
2036 for (i = 0; i < NR_IRQ_WORDS; ++i)
2037 if (vcpu->irq_pending[i])
2038 __set_bit(i, &vcpu->irq_summary);
2040 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2041 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2042 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2043 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2044 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2045 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2047 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2048 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2056 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2057 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2059 * This list is modified at module load time to reflect the
2060 * capabilities of the host cpu.
2062 static u32 msrs_to_save[] = {
2063 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2065 #ifdef CONFIG_X86_64
2066 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2068 MSR_IA32_TIME_STAMP_COUNTER,
2071 static unsigned num_msrs_to_save;
2073 static u32 emulated_msrs[] = {
2074 MSR_IA32_MISC_ENABLE,
2077 static __init void kvm_init_msr_list(void)
2082 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2083 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2086 msrs_to_save[j] = msrs_to_save[i];
2089 num_msrs_to_save = j;
2093 * Adapt set_msr() to msr_io()'s calling convention
2095 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2097 return set_msr(vcpu, index, *data);
2101 * Read or write a bunch of msrs. All parameters are kernel addresses.
2103 * @return number of msrs set successfully.
2105 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2106 struct kvm_msr_entry *entries,
2107 int (*do_msr)(struct kvm_vcpu *vcpu,
2108 unsigned index, u64 *data))
2114 for (i = 0; i < msrs->nmsrs; ++i)
2115 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2124 * Read or write a bunch of msrs. Parameters are user addresses.
2126 * @return number of msrs set successfully.
2128 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2129 int (*do_msr)(struct kvm_vcpu *vcpu,
2130 unsigned index, u64 *data),
2133 struct kvm_msrs msrs;
2134 struct kvm_msr_entry *entries;
2139 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2143 if (msrs.nmsrs >= MAX_IO_MSRS)
2147 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2148 entries = vmalloc(size);
2153 if (copy_from_user(entries, user_msrs->entries, size))
2156 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2161 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2173 * Translate a guest virtual address to a guest physical address.
2175 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2176 struct kvm_translation *tr)
2178 unsigned long vaddr = tr->linear_address;
2182 spin_lock(&vcpu->kvm->lock);
2183 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2184 tr->physical_address = gpa;
2185 tr->valid = gpa != UNMAPPED_GVA;
2188 spin_unlock(&vcpu->kvm->lock);
2194 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2195 struct kvm_interrupt *irq)
2197 if (irq->irq < 0 || irq->irq >= 256)
2201 set_bit(irq->irq, vcpu->irq_pending);
2202 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2209 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2210 struct kvm_debug_guest *dbg)
2216 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2223 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2224 unsigned long address,
2227 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2228 unsigned long pgoff;
2231 *type = VM_FAULT_MINOR;
2232 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2234 page = virt_to_page(vcpu->run);
2235 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2236 page = virt_to_page(vcpu->pio_data);
2238 return NOPAGE_SIGBUS;
2243 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2244 .nopage = kvm_vcpu_nopage,
2247 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2249 vma->vm_ops = &kvm_vcpu_vm_ops;
2253 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2255 struct kvm_vcpu *vcpu = filp->private_data;
2257 fput(vcpu->kvm->filp);
2261 static struct file_operations kvm_vcpu_fops = {
2262 .release = kvm_vcpu_release,
2263 .unlocked_ioctl = kvm_vcpu_ioctl,
2264 .compat_ioctl = kvm_vcpu_ioctl,
2265 .mmap = kvm_vcpu_mmap,
2269 * Allocates an inode for the vcpu.
2271 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2274 struct inode *inode;
2277 atomic_inc(&vcpu->kvm->filp->f_count);
2278 inode = kvmfs_inode(&kvm_vcpu_fops);
2279 if (IS_ERR(inode)) {
2284 file = kvmfs_file(inode, vcpu);
2290 r = get_unused_fd();
2294 fd_install(fd, file);
2303 fput(vcpu->kvm->filp);
2308 * Creates some virtual cpus. Good luck creating more than one.
2310 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2313 struct kvm_vcpu *vcpu;
2320 vcpu = &kvm->vcpus[n];
2322 mutex_lock(&vcpu->mutex);
2325 mutex_unlock(&vcpu->mutex);
2329 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2333 vcpu->run = page_address(page);
2335 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2339 vcpu->pio_data = page_address(page);
2341 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2343 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2346 r = kvm_arch_ops->vcpu_create(vcpu);
2348 goto out_free_vcpus;
2350 r = kvm_mmu_create(vcpu);
2352 goto out_free_vcpus;
2354 kvm_arch_ops->vcpu_load(vcpu);
2355 r = kvm_mmu_setup(vcpu);
2357 r = kvm_arch_ops->vcpu_setup(vcpu);
2361 goto out_free_vcpus;
2363 r = create_vcpu_fd(vcpu);
2365 goto out_free_vcpus;
2370 kvm_free_vcpu(vcpu);
2372 free_page((unsigned long)vcpu->run);
2375 mutex_unlock(&vcpu->mutex);
2380 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2381 struct kvm_cpuid *cpuid,
2382 struct kvm_cpuid_entry __user *entries)
2387 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2390 if (copy_from_user(&vcpu->cpuid_entries, entries,
2391 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2393 vcpu->cpuid_nent = cpuid->nent;
2400 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2403 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2404 vcpu->sigset_active = 1;
2405 vcpu->sigset = *sigset;
2407 vcpu->sigset_active = 0;
2412 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2413 * we have asm/x86/processor.h
2424 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2425 #ifdef CONFIG_X86_64
2426 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2428 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2432 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2434 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2438 memcpy(fpu->fpr, fxsave->st_space, 128);
2439 fpu->fcw = fxsave->cwd;
2440 fpu->fsw = fxsave->swd;
2441 fpu->ftwx = fxsave->twd;
2442 fpu->last_opcode = fxsave->fop;
2443 fpu->last_ip = fxsave->rip;
2444 fpu->last_dp = fxsave->rdp;
2445 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2452 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2454 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2458 memcpy(fxsave->st_space, fpu->fpr, 128);
2459 fxsave->cwd = fpu->fcw;
2460 fxsave->swd = fpu->fsw;
2461 fxsave->twd = fpu->ftwx;
2462 fxsave->fop = fpu->last_opcode;
2463 fxsave->rip = fpu->last_ip;
2464 fxsave->rdp = fpu->last_dp;
2465 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2472 static long kvm_vcpu_ioctl(struct file *filp,
2473 unsigned int ioctl, unsigned long arg)
2475 struct kvm_vcpu *vcpu = filp->private_data;
2476 void __user *argp = (void __user *)arg;
2484 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2486 case KVM_GET_REGS: {
2487 struct kvm_regs kvm_regs;
2489 memset(&kvm_regs, 0, sizeof kvm_regs);
2490 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2494 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2499 case KVM_SET_REGS: {
2500 struct kvm_regs kvm_regs;
2503 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2505 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2511 case KVM_GET_SREGS: {
2512 struct kvm_sregs kvm_sregs;
2514 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2515 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2519 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2524 case KVM_SET_SREGS: {
2525 struct kvm_sregs kvm_sregs;
2528 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2530 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2536 case KVM_TRANSLATE: {
2537 struct kvm_translation tr;
2540 if (copy_from_user(&tr, argp, sizeof tr))
2542 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2546 if (copy_to_user(argp, &tr, sizeof tr))
2551 case KVM_INTERRUPT: {
2552 struct kvm_interrupt irq;
2555 if (copy_from_user(&irq, argp, sizeof irq))
2557 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2563 case KVM_DEBUG_GUEST: {
2564 struct kvm_debug_guest dbg;
2567 if (copy_from_user(&dbg, argp, sizeof dbg))
2569 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2576 r = msr_io(vcpu, argp, get_msr, 1);
2579 r = msr_io(vcpu, argp, do_set_msr, 0);
2581 case KVM_SET_CPUID: {
2582 struct kvm_cpuid __user *cpuid_arg = argp;
2583 struct kvm_cpuid cpuid;
2586 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2588 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2593 case KVM_SET_SIGNAL_MASK: {
2594 struct kvm_signal_mask __user *sigmask_arg = argp;
2595 struct kvm_signal_mask kvm_sigmask;
2596 sigset_t sigset, *p;
2601 if (copy_from_user(&kvm_sigmask, argp,
2602 sizeof kvm_sigmask))
2605 if (kvm_sigmask.len != sizeof sigset)
2608 if (copy_from_user(&sigset, sigmask_arg->sigset,
2613 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2619 memset(&fpu, 0, sizeof fpu);
2620 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2624 if (copy_to_user(argp, &fpu, sizeof fpu))
2633 if (copy_from_user(&fpu, argp, sizeof fpu))
2635 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2648 static long kvm_vm_ioctl(struct file *filp,
2649 unsigned int ioctl, unsigned long arg)
2651 struct kvm *kvm = filp->private_data;
2652 void __user *argp = (void __user *)arg;
2656 case KVM_CREATE_VCPU:
2657 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2661 case KVM_SET_MEMORY_REGION: {
2662 struct kvm_memory_region kvm_mem;
2665 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2667 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
2672 case KVM_GET_DIRTY_LOG: {
2673 struct kvm_dirty_log log;
2676 if (copy_from_user(&log, argp, sizeof log))
2678 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2683 case KVM_SET_MEMORY_ALIAS: {
2684 struct kvm_memory_alias alias;
2687 if (copy_from_user(&alias, argp, sizeof alias))
2689 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2701 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2702 unsigned long address,
2705 struct kvm *kvm = vma->vm_file->private_data;
2706 unsigned long pgoff;
2709 *type = VM_FAULT_MINOR;
2710 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2711 page = gfn_to_page(kvm, pgoff);
2713 return NOPAGE_SIGBUS;
2718 static struct vm_operations_struct kvm_vm_vm_ops = {
2719 .nopage = kvm_vm_nopage,
2722 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2724 vma->vm_ops = &kvm_vm_vm_ops;
2728 static struct file_operations kvm_vm_fops = {
2729 .release = kvm_vm_release,
2730 .unlocked_ioctl = kvm_vm_ioctl,
2731 .compat_ioctl = kvm_vm_ioctl,
2732 .mmap = kvm_vm_mmap,
2735 static int kvm_dev_ioctl_create_vm(void)
2738 struct inode *inode;
2742 inode = kvmfs_inode(&kvm_vm_fops);
2743 if (IS_ERR(inode)) {
2748 kvm = kvm_create_vm();
2754 file = kvmfs_file(inode, kvm);
2761 r = get_unused_fd();
2765 fd_install(fd, file);
2772 kvm_destroy_vm(kvm);
2779 static long kvm_dev_ioctl(struct file *filp,
2780 unsigned int ioctl, unsigned long arg)
2782 void __user *argp = (void __user *)arg;
2786 case KVM_GET_API_VERSION:
2790 r = KVM_API_VERSION;
2796 r = kvm_dev_ioctl_create_vm();
2798 case KVM_GET_MSR_INDEX_LIST: {
2799 struct kvm_msr_list __user *user_msr_list = argp;
2800 struct kvm_msr_list msr_list;
2804 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2807 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2808 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2811 if (n < num_msrs_to_save)
2814 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2815 num_msrs_to_save * sizeof(u32)))
2817 if (copy_to_user(user_msr_list->indices
2818 + num_msrs_to_save * sizeof(u32),
2820 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2825 case KVM_CHECK_EXTENSION:
2827 * No extensions defined at present.
2831 case KVM_GET_VCPU_MMAP_SIZE:
2844 static struct file_operations kvm_chardev_ops = {
2845 .open = kvm_dev_open,
2846 .release = kvm_dev_release,
2847 .unlocked_ioctl = kvm_dev_ioctl,
2848 .compat_ioctl = kvm_dev_ioctl,
2851 static struct miscdevice kvm_dev = {
2857 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2860 if (val == SYS_RESTART) {
2862 * Some (well, at least mine) BIOSes hang on reboot if
2865 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2866 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
2871 static struct notifier_block kvm_reboot_notifier = {
2872 .notifier_call = kvm_reboot,
2877 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2880 static void decache_vcpus_on_cpu(int cpu)
2883 struct kvm_vcpu *vcpu;
2886 spin_lock(&kvm_lock);
2887 list_for_each_entry(vm, &vm_list, vm_list)
2888 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2889 vcpu = &vm->vcpus[i];
2891 * If the vcpu is locked, then it is running on some
2892 * other cpu and therefore it is not cached on the
2895 * If it's not locked, check the last cpu it executed
2898 if (mutex_trylock(&vcpu->mutex)) {
2899 if (vcpu->cpu == cpu) {
2900 kvm_arch_ops->vcpu_decache(vcpu);
2903 mutex_unlock(&vcpu->mutex);
2906 spin_unlock(&kvm_lock);
2909 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2915 case CPU_DOWN_PREPARE:
2916 case CPU_DOWN_PREPARE_FROZEN:
2917 case CPU_UP_CANCELED:
2918 case CPU_UP_CANCELED_FROZEN:
2919 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2921 decache_vcpus_on_cpu(cpu);
2922 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2926 case CPU_ONLINE_FROZEN:
2927 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2929 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2936 static struct notifier_block kvm_cpu_notifier = {
2937 .notifier_call = kvm_cpu_hotplug,
2938 .priority = 20, /* must be > scheduler priority */
2941 static u64 stat_get(void *_offset)
2943 unsigned offset = (long)_offset;
2946 struct kvm_vcpu *vcpu;
2949 spin_lock(&kvm_lock);
2950 list_for_each_entry(kvm, &vm_list, vm_list)
2951 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2952 vcpu = &kvm->vcpus[i];
2953 total += *(u32 *)((void *)vcpu + offset);
2955 spin_unlock(&kvm_lock);
2959 static void stat_set(void *offset, u64 val)
2963 DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
2965 static __init void kvm_init_debug(void)
2967 struct kvm_stats_debugfs_item *p;
2969 debugfs_dir = debugfs_create_dir("kvm", NULL);
2970 for (p = debugfs_entries; p->name; ++p)
2971 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
2972 (void *)(long)p->offset,
2976 static void kvm_exit_debug(void)
2978 struct kvm_stats_debugfs_item *p;
2980 for (p = debugfs_entries; p->name; ++p)
2981 debugfs_remove(p->dentry);
2982 debugfs_remove(debugfs_dir);
2985 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2987 decache_vcpus_on_cpu(raw_smp_processor_id());
2988 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
2992 static int kvm_resume(struct sys_device *dev)
2994 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
2998 static struct sysdev_class kvm_sysdev_class = {
2999 set_kset_name("kvm"),
3000 .suspend = kvm_suspend,
3001 .resume = kvm_resume,
3004 static struct sys_device kvm_sysdev = {
3006 .cls = &kvm_sysdev_class,
3009 hpa_t bad_page_address;
3011 static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
3012 const char *dev_name, void *data, struct vfsmount *mnt)
3014 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
3017 static struct file_system_type kvm_fs_type = {
3019 .get_sb = kvmfs_get_sb,
3020 .kill_sb = kill_anon_super,
3023 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3028 printk(KERN_ERR "kvm: already loaded the other module\n");
3032 if (!ops->cpu_has_kvm_support()) {
3033 printk(KERN_ERR "kvm: no hardware support\n");
3036 if (ops->disabled_by_bios()) {
3037 printk(KERN_ERR "kvm: disabled by bios\n");
3043 r = kvm_arch_ops->hardware_setup();
3047 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
3048 r = register_cpu_notifier(&kvm_cpu_notifier);
3051 register_reboot_notifier(&kvm_reboot_notifier);
3053 r = sysdev_class_register(&kvm_sysdev_class);
3057 r = sysdev_register(&kvm_sysdev);
3061 kvm_chardev_ops.owner = module;
3063 r = misc_register(&kvm_dev);
3065 printk (KERN_ERR "kvm: misc device register failed\n");
3072 sysdev_unregister(&kvm_sysdev);
3074 sysdev_class_unregister(&kvm_sysdev_class);
3076 unregister_reboot_notifier(&kvm_reboot_notifier);
3077 unregister_cpu_notifier(&kvm_cpu_notifier);
3079 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
3080 kvm_arch_ops->hardware_unsetup();
3082 kvm_arch_ops = NULL;
3086 void kvm_exit_arch(void)
3088 misc_deregister(&kvm_dev);
3089 sysdev_unregister(&kvm_sysdev);
3090 sysdev_class_unregister(&kvm_sysdev_class);
3091 unregister_reboot_notifier(&kvm_reboot_notifier);
3092 unregister_cpu_notifier(&kvm_cpu_notifier);
3093 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
3094 kvm_arch_ops->hardware_unsetup();
3095 kvm_arch_ops = NULL;
3098 static __init int kvm_init(void)
3100 static struct page *bad_page;
3103 r = kvm_mmu_module_init();
3107 r = register_filesystem(&kvm_fs_type);
3111 kvmfs_mnt = kern_mount(&kvm_fs_type);
3112 r = PTR_ERR(kvmfs_mnt);
3113 if (IS_ERR(kvmfs_mnt))
3117 kvm_init_msr_list();
3119 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3124 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3125 memset(__va(bad_page_address), 0, PAGE_SIZE);
3133 unregister_filesystem(&kvm_fs_type);
3135 kvm_mmu_module_exit();
3140 static __exit void kvm_exit(void)
3143 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3145 unregister_filesystem(&kvm_fs_type);
3146 kvm_mmu_module_exit();
3149 module_init(kvm_init)
3150 module_exit(kvm_exit)
3152 EXPORT_SYMBOL_GPL(kvm_init_arch);
3153 EXPORT_SYMBOL_GPL(kvm_exit_arch);