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.
19 #include "x86_emulate.h"
20 #include "segment_descriptor.h"
22 #include <linux/kvm.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
28 #include <linux/miscdevice.h>
29 #include <linux/vmalloc.h>
30 #include <linux/reboot.h>
31 #include <linux/debugfs.h>
32 #include <linux/highmem.h>
33 #include <linux/file.h>
34 #include <linux/sysdev.h>
35 #include <linux/cpu.h>
36 #include <linux/sched.h>
37 #include <linux/cpumask.h>
38 #include <linux/smp.h>
39 #include <linux/anon_inodes.h>
41 #include <asm/processor.h>
44 #include <asm/uaccess.h>
47 MODULE_AUTHOR("Qumranet");
48 MODULE_LICENSE("GPL");
50 static DEFINE_SPINLOCK(kvm_lock);
51 static LIST_HEAD(vm_list);
53 static cpumask_t cpus_hardware_enabled;
55 struct kvm_arch_ops *kvm_arch_ops;
57 static __read_mostly struct preempt_ops kvm_preempt_ops;
59 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
61 static struct kvm_stats_debugfs_item {
64 struct dentry *dentry;
65 } debugfs_entries[] = {
66 { "pf_fixed", STAT_OFFSET(pf_fixed) },
67 { "pf_guest", STAT_OFFSET(pf_guest) },
68 { "tlb_flush", STAT_OFFSET(tlb_flush) },
69 { "invlpg", STAT_OFFSET(invlpg) },
70 { "exits", STAT_OFFSET(exits) },
71 { "io_exits", STAT_OFFSET(io_exits) },
72 { "mmio_exits", STAT_OFFSET(mmio_exits) },
73 { "signal_exits", STAT_OFFSET(signal_exits) },
74 { "irq_window", STAT_OFFSET(irq_window_exits) },
75 { "halt_exits", STAT_OFFSET(halt_exits) },
76 { "request_irq", STAT_OFFSET(request_irq_exits) },
77 { "irq_exits", STAT_OFFSET(irq_exits) },
78 { "light_exits", STAT_OFFSET(light_exits) },
79 { "efer_reload", STAT_OFFSET(efer_reload) },
83 static struct dentry *debugfs_dir;
85 #define MAX_IO_MSRS 256
87 #define CR0_RESERVED_BITS \
88 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
89 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
90 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
91 #define CR4_RESERVED_BITS \
92 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
93 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
94 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
95 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
97 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
98 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
101 // LDT or TSS descriptor in the GDT. 16 bytes.
102 struct segment_descriptor_64 {
103 struct segment_descriptor s;
110 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
113 unsigned long segment_base(u16 selector)
115 struct descriptor_table gdt;
116 struct segment_descriptor *d;
117 unsigned long table_base;
118 typedef unsigned long ul;
124 asm ("sgdt %0" : "=m"(gdt));
125 table_base = gdt.base;
127 if (selector & 4) { /* from ldt */
130 asm ("sldt %0" : "=g"(ldt_selector));
131 table_base = segment_base(ldt_selector);
133 d = (struct segment_descriptor *)(table_base + (selector & ~7));
134 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
137 && (d->type == 2 || d->type == 9 || d->type == 11))
138 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
142 EXPORT_SYMBOL_GPL(segment_base);
144 static inline int valid_vcpu(int n)
146 return likely(n >= 0 && n < KVM_MAX_VCPUS);
149 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
151 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
154 vcpu->guest_fpu_loaded = 1;
155 fx_save(vcpu->host_fx_image);
156 fx_restore(vcpu->guest_fx_image);
158 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
160 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
162 if (!vcpu->guest_fpu_loaded)
165 vcpu->guest_fpu_loaded = 0;
166 fx_save(vcpu->guest_fx_image);
167 fx_restore(vcpu->host_fx_image);
169 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
172 * Switches to specified vcpu, until a matching vcpu_put()
174 static void vcpu_load(struct kvm_vcpu *vcpu)
178 mutex_lock(&vcpu->mutex);
180 preempt_notifier_register(&vcpu->preempt_notifier);
181 kvm_arch_ops->vcpu_load(vcpu, cpu);
185 static void vcpu_put(struct kvm_vcpu *vcpu)
188 kvm_arch_ops->vcpu_put(vcpu);
189 preempt_notifier_unregister(&vcpu->preempt_notifier);
191 mutex_unlock(&vcpu->mutex);
194 static void ack_flush(void *_completed)
196 atomic_t *completed = _completed;
198 atomic_inc(completed);
201 void kvm_flush_remote_tlbs(struct kvm *kvm)
205 struct kvm_vcpu *vcpu;
208 atomic_set(&completed, 0);
211 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
212 vcpu = kvm->vcpus[i];
215 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
218 if (cpu != -1 && cpu != raw_smp_processor_id())
219 if (!cpu_isset(cpu, cpus)) {
226 * We really want smp_call_function_mask() here. But that's not
227 * available, so ipi all cpus in parallel and wait for them
230 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
231 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
232 while (atomic_read(&completed) != needed) {
238 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
243 mutex_init(&vcpu->mutex);
245 vcpu->mmu.root_hpa = INVALID_PAGE;
249 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
254 vcpu->run = page_address(page);
256 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
261 vcpu->pio_data = page_address(page);
263 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
265 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
267 r = kvm_mmu_create(vcpu);
269 goto fail_free_pio_data;
274 free_page((unsigned long)vcpu->pio_data);
276 free_page((unsigned long)vcpu->run);
280 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
282 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
284 kvm_mmu_destroy(vcpu);
285 free_page((unsigned long)vcpu->pio_data);
286 free_page((unsigned long)vcpu->run);
288 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
290 static struct kvm *kvm_create_vm(void)
292 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
295 return ERR_PTR(-ENOMEM);
297 kvm_io_bus_init(&kvm->pio_bus);
298 mutex_init(&kvm->lock);
299 INIT_LIST_HEAD(&kvm->active_mmu_pages);
300 kvm_io_bus_init(&kvm->mmio_bus);
301 spin_lock(&kvm_lock);
302 list_add(&kvm->vm_list, &vm_list);
303 spin_unlock(&kvm_lock);
307 static int kvm_dev_open(struct inode *inode, struct file *filp)
313 * Free any memory in @free but not in @dont.
315 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
316 struct kvm_memory_slot *dont)
320 if (!dont || free->phys_mem != dont->phys_mem)
321 if (free->phys_mem) {
322 for (i = 0; i < free->npages; ++i)
323 if (free->phys_mem[i])
324 __free_page(free->phys_mem[i]);
325 vfree(free->phys_mem);
328 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
329 vfree(free->dirty_bitmap);
331 free->phys_mem = NULL;
333 free->dirty_bitmap = NULL;
336 static void kvm_free_physmem(struct kvm *kvm)
340 for (i = 0; i < kvm->nmemslots; ++i)
341 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
344 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
348 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
349 if (vcpu->pio.guest_pages[i]) {
350 __free_page(vcpu->pio.guest_pages[i]);
351 vcpu->pio.guest_pages[i] = NULL;
355 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
358 kvm_mmu_unload(vcpu);
362 static void kvm_free_vcpus(struct kvm *kvm)
367 * Unpin any mmu pages first.
369 for (i = 0; i < KVM_MAX_VCPUS; ++i)
371 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
372 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
374 kvm_arch_ops->vcpu_free(kvm->vcpus[i]);
375 kvm->vcpus[i] = NULL;
381 static int kvm_dev_release(struct inode *inode, struct file *filp)
386 static void kvm_destroy_vm(struct kvm *kvm)
388 spin_lock(&kvm_lock);
389 list_del(&kvm->vm_list);
390 spin_unlock(&kvm_lock);
391 kvm_io_bus_destroy(&kvm->pio_bus);
392 kvm_io_bus_destroy(&kvm->mmio_bus);
394 kvm_free_physmem(kvm);
398 static int kvm_vm_release(struct inode *inode, struct file *filp)
400 struct kvm *kvm = filp->private_data;
406 static void inject_gp(struct kvm_vcpu *vcpu)
408 kvm_arch_ops->inject_gp(vcpu, 0);
412 * Load the pae pdptrs. Return true is they are all valid.
414 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
416 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
417 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
422 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
424 mutex_lock(&vcpu->kvm->lock);
425 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
431 pdpt = kmap_atomic(page, KM_USER0);
432 memcpy(pdpte, pdpt+offset, sizeof(pdpte));
433 kunmap_atomic(pdpt, KM_USER0);
435 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
436 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
443 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
445 mutex_unlock(&vcpu->kvm->lock);
450 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
452 if (cr0 & CR0_RESERVED_BITS) {
453 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
459 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
460 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
465 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
466 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
467 "and a clear PE flag\n");
472 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
474 if ((vcpu->shadow_efer & EFER_LME)) {
478 printk(KERN_DEBUG "set_cr0: #GP, start paging "
479 "in long mode while PAE is disabled\n");
483 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
485 printk(KERN_DEBUG "set_cr0: #GP, start paging "
486 "in long mode while CS.L == 1\n");
493 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
494 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
502 kvm_arch_ops->set_cr0(vcpu, cr0);
505 mutex_lock(&vcpu->kvm->lock);
506 kvm_mmu_reset_context(vcpu);
507 mutex_unlock(&vcpu->kvm->lock);
510 EXPORT_SYMBOL_GPL(set_cr0);
512 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
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_RESERVED_BITS) {
521 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
526 if (is_long_mode(vcpu)) {
527 if (!(cr4 & X86_CR4_PAE)) {
528 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
533 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
534 && !load_pdptrs(vcpu, vcpu->cr3)) {
535 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
540 if (cr4 & X86_CR4_VMXE) {
541 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
545 kvm_arch_ops->set_cr4(vcpu, cr4);
546 mutex_lock(&vcpu->kvm->lock);
547 kvm_mmu_reset_context(vcpu);
548 mutex_unlock(&vcpu->kvm->lock);
550 EXPORT_SYMBOL_GPL(set_cr4);
552 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
554 if (is_long_mode(vcpu)) {
555 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
556 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
562 if (cr3 & CR3_PAE_RESERVED_BITS) {
564 "set_cr3: #GP, reserved bits\n");
568 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
569 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
575 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
577 "set_cr3: #GP, reserved bits\n");
585 mutex_lock(&vcpu->kvm->lock);
587 * Does the new cr3 value map to physical memory? (Note, we
588 * catch an invalid cr3 even in real-mode, because it would
589 * cause trouble later on when we turn on paging anyway.)
591 * A real CPU would silently accept an invalid cr3 and would
592 * attempt to use it - with largely undefined (and often hard
593 * to debug) behavior on the guest side.
595 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
598 vcpu->mmu.new_cr3(vcpu);
599 mutex_unlock(&vcpu->kvm->lock);
601 EXPORT_SYMBOL_GPL(set_cr3);
603 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
605 if (cr8 & CR8_RESERVED_BITS) {
606 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
612 EXPORT_SYMBOL_GPL(set_cr8);
614 void fx_init(struct kvm_vcpu *vcpu)
616 struct __attribute__ ((__packed__)) fx_image_s {
622 u64 operand;// fpu dp
628 /* Initialize guest FPU by resetting ours and saving into guest's */
630 fx_save(vcpu->host_fx_image);
632 fx_save(vcpu->guest_fx_image);
633 fx_restore(vcpu->host_fx_image);
636 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
637 fx_image->mxcsr = 0x1f80;
638 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
639 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
641 EXPORT_SYMBOL_GPL(fx_init);
644 * Allocate some memory and give it an address in the guest physical address
647 * Discontiguous memory is allowed, mostly for framebuffers.
649 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
650 struct kvm_memory_region *mem)
654 unsigned long npages;
656 struct kvm_memory_slot *memslot;
657 struct kvm_memory_slot old, new;
658 int memory_config_version;
661 /* General sanity checks */
662 if (mem->memory_size & (PAGE_SIZE - 1))
664 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
666 if (mem->slot >= KVM_MEMORY_SLOTS)
668 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
671 memslot = &kvm->memslots[mem->slot];
672 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
673 npages = mem->memory_size >> PAGE_SHIFT;
676 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
679 mutex_lock(&kvm->lock);
681 memory_config_version = kvm->memory_config_version;
682 new = old = *memslot;
684 new.base_gfn = base_gfn;
686 new.flags = mem->flags;
688 /* Disallow changing a memory slot's size. */
690 if (npages && old.npages && npages != old.npages)
693 /* Check for overlaps */
695 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
696 struct kvm_memory_slot *s = &kvm->memslots[i];
700 if (!((base_gfn + npages <= s->base_gfn) ||
701 (base_gfn >= s->base_gfn + s->npages)))
705 * Do memory allocations outside lock. memory_config_version will
708 mutex_unlock(&kvm->lock);
710 /* Deallocate if slot is being removed */
714 /* Free page dirty bitmap if unneeded */
715 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
716 new.dirty_bitmap = NULL;
720 /* Allocate if a slot is being created */
721 if (npages && !new.phys_mem) {
722 new.phys_mem = vmalloc(npages * sizeof(struct page *));
727 memset(new.phys_mem, 0, npages * sizeof(struct page *));
728 for (i = 0; i < npages; ++i) {
729 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
731 if (!new.phys_mem[i])
733 set_page_private(new.phys_mem[i],0);
737 /* Allocate page dirty bitmap if needed */
738 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
739 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
741 new.dirty_bitmap = vmalloc(dirty_bytes);
742 if (!new.dirty_bitmap)
744 memset(new.dirty_bitmap, 0, dirty_bytes);
747 mutex_lock(&kvm->lock);
749 if (memory_config_version != kvm->memory_config_version) {
750 mutex_unlock(&kvm->lock);
751 kvm_free_physmem_slot(&new, &old);
759 if (mem->slot >= kvm->nmemslots)
760 kvm->nmemslots = mem->slot + 1;
763 ++kvm->memory_config_version;
765 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
766 kvm_flush_remote_tlbs(kvm);
768 mutex_unlock(&kvm->lock);
770 kvm_free_physmem_slot(&old, &new);
774 mutex_unlock(&kvm->lock);
776 kvm_free_physmem_slot(&new, &old);
782 * Get (and clear) the dirty memory log for a memory slot.
784 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
785 struct kvm_dirty_log *log)
787 struct kvm_memory_slot *memslot;
790 unsigned long any = 0;
792 mutex_lock(&kvm->lock);
795 * Prevent changes to guest memory configuration even while the lock
799 mutex_unlock(&kvm->lock);
801 if (log->slot >= KVM_MEMORY_SLOTS)
804 memslot = &kvm->memslots[log->slot];
806 if (!memslot->dirty_bitmap)
809 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
811 for (i = 0; !any && i < n/sizeof(long); ++i)
812 any = memslot->dirty_bitmap[i];
815 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
818 mutex_lock(&kvm->lock);
819 kvm_mmu_slot_remove_write_access(kvm, log->slot);
820 kvm_flush_remote_tlbs(kvm);
821 memset(memslot->dirty_bitmap, 0, n);
822 mutex_unlock(&kvm->lock);
827 mutex_lock(&kvm->lock);
829 mutex_unlock(&kvm->lock);
834 * Set a new alias region. Aliases map a portion of physical memory into
835 * another portion. This is useful for memory windows, for example the PC
838 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
839 struct kvm_memory_alias *alias)
842 struct kvm_mem_alias *p;
845 /* General sanity checks */
846 if (alias->memory_size & (PAGE_SIZE - 1))
848 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
850 if (alias->slot >= KVM_ALIAS_SLOTS)
852 if (alias->guest_phys_addr + alias->memory_size
853 < alias->guest_phys_addr)
855 if (alias->target_phys_addr + alias->memory_size
856 < alias->target_phys_addr)
859 mutex_lock(&kvm->lock);
861 p = &kvm->aliases[alias->slot];
862 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
863 p->npages = alias->memory_size >> PAGE_SHIFT;
864 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
866 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
867 if (kvm->aliases[n - 1].npages)
871 kvm_mmu_zap_all(kvm);
873 mutex_unlock(&kvm->lock);
881 static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
884 struct kvm_mem_alias *alias;
886 for (i = 0; i < kvm->naliases; ++i) {
887 alias = &kvm->aliases[i];
888 if (gfn >= alias->base_gfn
889 && gfn < alias->base_gfn + alias->npages)
890 return alias->target_gfn + gfn - alias->base_gfn;
895 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
899 for (i = 0; i < kvm->nmemslots; ++i) {
900 struct kvm_memory_slot *memslot = &kvm->memslots[i];
902 if (gfn >= memslot->base_gfn
903 && gfn < memslot->base_gfn + memslot->npages)
909 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
911 gfn = unalias_gfn(kvm, gfn);
912 return __gfn_to_memslot(kvm, gfn);
915 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
917 struct kvm_memory_slot *slot;
919 gfn = unalias_gfn(kvm, gfn);
920 slot = __gfn_to_memslot(kvm, gfn);
923 return slot->phys_mem[gfn - slot->base_gfn];
925 EXPORT_SYMBOL_GPL(gfn_to_page);
927 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
930 struct kvm_memory_slot *memslot;
931 unsigned long rel_gfn;
933 for (i = 0; i < kvm->nmemslots; ++i) {
934 memslot = &kvm->memslots[i];
936 if (gfn >= memslot->base_gfn
937 && gfn < memslot->base_gfn + memslot->npages) {
939 if (!memslot->dirty_bitmap)
942 rel_gfn = gfn - memslot->base_gfn;
945 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
946 set_bit(rel_gfn, memslot->dirty_bitmap);
952 int emulator_read_std(unsigned long addr,
955 struct kvm_vcpu *vcpu)
960 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
961 unsigned offset = addr & (PAGE_SIZE-1);
962 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
967 if (gpa == UNMAPPED_GVA)
968 return X86EMUL_PROPAGATE_FAULT;
969 pfn = gpa >> PAGE_SHIFT;
970 page = gfn_to_page(vcpu->kvm, pfn);
972 return X86EMUL_UNHANDLEABLE;
973 page_virt = kmap_atomic(page, KM_USER0);
975 memcpy(data, page_virt + offset, tocopy);
977 kunmap_atomic(page_virt, KM_USER0);
984 return X86EMUL_CONTINUE;
986 EXPORT_SYMBOL_GPL(emulator_read_std);
988 static int emulator_write_std(unsigned long addr,
991 struct kvm_vcpu *vcpu)
993 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
995 return X86EMUL_UNHANDLEABLE;
998 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1002 * Note that its important to have this wrapper function because
1003 * in the very near future we will be checking for MMIOs against
1004 * the LAPIC as well as the general MMIO bus
1006 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1009 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1012 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1015 static int emulator_read_emulated(unsigned long addr,
1018 struct kvm_vcpu *vcpu)
1020 struct kvm_io_device *mmio_dev;
1023 if (vcpu->mmio_read_completed) {
1024 memcpy(val, vcpu->mmio_data, bytes);
1025 vcpu->mmio_read_completed = 0;
1026 return X86EMUL_CONTINUE;
1027 } else if (emulator_read_std(addr, val, bytes, vcpu)
1028 == X86EMUL_CONTINUE)
1029 return X86EMUL_CONTINUE;
1031 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1032 if (gpa == UNMAPPED_GVA)
1033 return X86EMUL_PROPAGATE_FAULT;
1036 * Is this MMIO handled locally?
1038 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1040 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1041 return X86EMUL_CONTINUE;
1044 vcpu->mmio_needed = 1;
1045 vcpu->mmio_phys_addr = gpa;
1046 vcpu->mmio_size = bytes;
1047 vcpu->mmio_is_write = 0;
1049 return X86EMUL_UNHANDLEABLE;
1052 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1053 const void *val, int bytes)
1058 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1060 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1063 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
1064 virt = kmap_atomic(page, KM_USER0);
1065 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1066 memcpy(virt + offset_in_page(gpa), val, bytes);
1067 kunmap_atomic(virt, KM_USER0);
1071 static int emulator_write_emulated_onepage(unsigned long addr,
1074 struct kvm_vcpu *vcpu)
1076 struct kvm_io_device *mmio_dev;
1077 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1079 if (gpa == UNMAPPED_GVA) {
1080 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
1081 return X86EMUL_PROPAGATE_FAULT;
1084 if (emulator_write_phys(vcpu, gpa, val, bytes))
1085 return X86EMUL_CONTINUE;
1088 * Is this MMIO handled locally?
1090 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1092 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1093 return X86EMUL_CONTINUE;
1096 vcpu->mmio_needed = 1;
1097 vcpu->mmio_phys_addr = gpa;
1098 vcpu->mmio_size = bytes;
1099 vcpu->mmio_is_write = 1;
1100 memcpy(vcpu->mmio_data, val, bytes);
1102 return X86EMUL_CONTINUE;
1105 int emulator_write_emulated(unsigned long addr,
1108 struct kvm_vcpu *vcpu)
1110 /* Crossing a page boundary? */
1111 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1114 now = -addr & ~PAGE_MASK;
1115 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1116 if (rc != X86EMUL_CONTINUE)
1122 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1124 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1126 static int emulator_cmpxchg_emulated(unsigned long addr,
1130 struct kvm_vcpu *vcpu)
1132 static int reported;
1136 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1138 return emulator_write_emulated(addr, new, bytes, vcpu);
1141 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1143 return kvm_arch_ops->get_segment_base(vcpu, seg);
1146 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1148 return X86EMUL_CONTINUE;
1151 int emulate_clts(struct kvm_vcpu *vcpu)
1155 cr0 = vcpu->cr0 & ~X86_CR0_TS;
1156 kvm_arch_ops->set_cr0(vcpu, cr0);
1157 return X86EMUL_CONTINUE;
1160 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1162 struct kvm_vcpu *vcpu = ctxt->vcpu;
1166 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1167 return X86EMUL_CONTINUE;
1169 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1171 return X86EMUL_UNHANDLEABLE;
1175 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1177 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1180 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1182 /* FIXME: better handling */
1183 return X86EMUL_UNHANDLEABLE;
1185 return X86EMUL_CONTINUE;
1188 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1190 static int reported;
1192 unsigned long rip = ctxt->vcpu->rip;
1193 unsigned long rip_linear;
1195 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1200 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt->vcpu);
1202 printk(KERN_ERR "emulation failed but !mmio_needed?"
1203 " rip %lx %02x %02x %02x %02x\n",
1204 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1208 struct x86_emulate_ops emulate_ops = {
1209 .read_std = emulator_read_std,
1210 .write_std = emulator_write_std,
1211 .read_emulated = emulator_read_emulated,
1212 .write_emulated = emulator_write_emulated,
1213 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1216 int emulate_instruction(struct kvm_vcpu *vcpu,
1217 struct kvm_run *run,
1221 struct x86_emulate_ctxt emulate_ctxt;
1225 vcpu->mmio_fault_cr2 = cr2;
1226 kvm_arch_ops->cache_regs(vcpu);
1228 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1230 emulate_ctxt.vcpu = vcpu;
1231 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1232 emulate_ctxt.cr2 = cr2;
1233 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1234 ? X86EMUL_MODE_REAL : cs_l
1235 ? X86EMUL_MODE_PROT64 : cs_db
1236 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1238 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1239 emulate_ctxt.cs_base = 0;
1240 emulate_ctxt.ds_base = 0;
1241 emulate_ctxt.es_base = 0;
1242 emulate_ctxt.ss_base = 0;
1244 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1245 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1246 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1247 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1250 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1251 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1253 vcpu->mmio_is_write = 0;
1254 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1256 if ((r || vcpu->mmio_is_write) && run) {
1257 run->exit_reason = KVM_EXIT_MMIO;
1258 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1259 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1260 run->mmio.len = vcpu->mmio_size;
1261 run->mmio.is_write = vcpu->mmio_is_write;
1265 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1266 return EMULATE_DONE;
1267 if (!vcpu->mmio_needed) {
1268 report_emulation_failure(&emulate_ctxt);
1269 return EMULATE_FAIL;
1271 return EMULATE_DO_MMIO;
1274 kvm_arch_ops->decache_regs(vcpu);
1275 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1277 if (vcpu->mmio_is_write) {
1278 vcpu->mmio_needed = 0;
1279 return EMULATE_DO_MMIO;
1282 return EMULATE_DONE;
1284 EXPORT_SYMBOL_GPL(emulate_instruction);
1286 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1288 if (vcpu->irq_summary)
1291 vcpu->run->exit_reason = KVM_EXIT_HLT;
1292 ++vcpu->stat.halt_exits;
1295 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1297 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1299 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1301 kvm_arch_ops->cache_regs(vcpu);
1303 #ifdef CONFIG_X86_64
1304 if (is_long_mode(vcpu)) {
1305 nr = vcpu->regs[VCPU_REGS_RAX];
1306 a0 = vcpu->regs[VCPU_REGS_RDI];
1307 a1 = vcpu->regs[VCPU_REGS_RSI];
1308 a2 = vcpu->regs[VCPU_REGS_RDX];
1309 a3 = vcpu->regs[VCPU_REGS_RCX];
1310 a4 = vcpu->regs[VCPU_REGS_R8];
1311 a5 = vcpu->regs[VCPU_REGS_R9];
1315 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1316 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1317 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1318 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1319 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1320 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1321 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1325 run->hypercall.nr = nr;
1326 run->hypercall.args[0] = a0;
1327 run->hypercall.args[1] = a1;
1328 run->hypercall.args[2] = a2;
1329 run->hypercall.args[3] = a3;
1330 run->hypercall.args[4] = a4;
1331 run->hypercall.args[5] = a5;
1332 run->hypercall.ret = ret;
1333 run->hypercall.longmode = is_long_mode(vcpu);
1334 kvm_arch_ops->decache_regs(vcpu);
1337 vcpu->regs[VCPU_REGS_RAX] = ret;
1338 kvm_arch_ops->decache_regs(vcpu);
1341 EXPORT_SYMBOL_GPL(kvm_hypercall);
1343 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1345 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1348 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1350 struct descriptor_table dt = { limit, base };
1352 kvm_arch_ops->set_gdt(vcpu, &dt);
1355 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1357 struct descriptor_table dt = { limit, base };
1359 kvm_arch_ops->set_idt(vcpu, &dt);
1362 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1363 unsigned long *rflags)
1366 *rflags = kvm_arch_ops->get_rflags(vcpu);
1369 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1371 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
1382 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1387 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1388 unsigned long *rflags)
1392 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1393 *rflags = kvm_arch_ops->get_rflags(vcpu);
1402 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1405 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1410 * Register the para guest with the host:
1412 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1414 struct kvm_vcpu_para_state *para_state;
1415 hpa_t para_state_hpa, hypercall_hpa;
1416 struct page *para_state_page;
1417 unsigned char *hypercall;
1418 gpa_t hypercall_gpa;
1420 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1421 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1424 * Needs to be page aligned:
1426 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1429 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1430 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1431 if (is_error_hpa(para_state_hpa))
1434 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1435 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1436 para_state = kmap(para_state_page);
1438 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1439 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1441 para_state->host_version = KVM_PARA_API_VERSION;
1443 * We cannot support guests that try to register themselves
1444 * with a newer API version than the host supports:
1446 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1447 para_state->ret = -KVM_EINVAL;
1448 goto err_kunmap_skip;
1451 hypercall_gpa = para_state->hypercall_gpa;
1452 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1453 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1454 if (is_error_hpa(hypercall_hpa)) {
1455 para_state->ret = -KVM_EINVAL;
1456 goto err_kunmap_skip;
1459 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1460 vcpu->para_state_page = para_state_page;
1461 vcpu->para_state_gpa = para_state_gpa;
1462 vcpu->hypercall_gpa = hypercall_gpa;
1464 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1465 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1466 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1467 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1468 kunmap_atomic(hypercall, KM_USER1);
1470 para_state->ret = 0;
1472 kunmap(para_state_page);
1478 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1483 case 0xc0010010: /* SYSCFG */
1484 case 0xc0010015: /* HWCR */
1485 case MSR_IA32_PLATFORM_ID:
1486 case MSR_IA32_P5_MC_ADDR:
1487 case MSR_IA32_P5_MC_TYPE:
1488 case MSR_IA32_MC0_CTL:
1489 case MSR_IA32_MCG_STATUS:
1490 case MSR_IA32_MCG_CAP:
1491 case MSR_IA32_MC0_MISC:
1492 case MSR_IA32_MC0_MISC+4:
1493 case MSR_IA32_MC0_MISC+8:
1494 case MSR_IA32_MC0_MISC+12:
1495 case MSR_IA32_MC0_MISC+16:
1496 case MSR_IA32_UCODE_REV:
1497 case MSR_IA32_PERF_STATUS:
1498 case MSR_IA32_EBL_CR_POWERON:
1499 /* MTRR registers */
1501 case 0x200 ... 0x2ff:
1504 case 0xcd: /* fsb frequency */
1507 case MSR_IA32_APICBASE:
1508 data = vcpu->apic_base;
1510 case MSR_IA32_MISC_ENABLE:
1511 data = vcpu->ia32_misc_enable_msr;
1513 #ifdef CONFIG_X86_64
1515 data = vcpu->shadow_efer;
1519 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1525 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1528 * Reads an msr value (of 'msr_index') into 'pdata'.
1529 * Returns 0 on success, non-0 otherwise.
1530 * Assumes vcpu_load() was already called.
1532 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1534 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1537 #ifdef CONFIG_X86_64
1539 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1541 if (efer & EFER_RESERVED_BITS) {
1542 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1549 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1550 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1555 kvm_arch_ops->set_efer(vcpu, efer);
1558 efer |= vcpu->shadow_efer & EFER_LMA;
1560 vcpu->shadow_efer = efer;
1565 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1568 #ifdef CONFIG_X86_64
1570 set_efer(vcpu, data);
1573 case MSR_IA32_MC0_STATUS:
1574 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1575 __FUNCTION__, data);
1577 case MSR_IA32_MCG_STATUS:
1578 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1579 __FUNCTION__, data);
1581 case MSR_IA32_UCODE_REV:
1582 case MSR_IA32_UCODE_WRITE:
1583 case 0x200 ... 0x2ff: /* MTRRs */
1585 case MSR_IA32_APICBASE:
1586 vcpu->apic_base = data;
1588 case MSR_IA32_MISC_ENABLE:
1589 vcpu->ia32_misc_enable_msr = data;
1592 * This is the 'probe whether the host is KVM' logic:
1594 case MSR_KVM_API_MAGIC:
1595 return vcpu_register_para(vcpu, data);
1598 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1603 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1606 * Writes msr value into into the appropriate "register".
1607 * Returns 0 on success, non-0 otherwise.
1608 * Assumes vcpu_load() was already called.
1610 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1612 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1615 void kvm_resched(struct kvm_vcpu *vcpu)
1617 if (!need_resched())
1621 EXPORT_SYMBOL_GPL(kvm_resched);
1623 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1627 struct kvm_cpuid_entry *e, *best;
1629 kvm_arch_ops->cache_regs(vcpu);
1630 function = vcpu->regs[VCPU_REGS_RAX];
1631 vcpu->regs[VCPU_REGS_RAX] = 0;
1632 vcpu->regs[VCPU_REGS_RBX] = 0;
1633 vcpu->regs[VCPU_REGS_RCX] = 0;
1634 vcpu->regs[VCPU_REGS_RDX] = 0;
1636 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1637 e = &vcpu->cpuid_entries[i];
1638 if (e->function == function) {
1643 * Both basic or both extended?
1645 if (((e->function ^ function) & 0x80000000) == 0)
1646 if (!best || e->function > best->function)
1650 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1651 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1652 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1653 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1655 kvm_arch_ops->decache_regs(vcpu);
1656 kvm_arch_ops->skip_emulated_instruction(vcpu);
1658 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1660 static int pio_copy_data(struct kvm_vcpu *vcpu)
1662 void *p = vcpu->pio_data;
1665 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1667 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1670 free_pio_guest_pages(vcpu);
1673 q += vcpu->pio.guest_page_offset;
1674 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1676 memcpy(q, p, bytes);
1678 memcpy(p, q, bytes);
1679 q -= vcpu->pio.guest_page_offset;
1681 free_pio_guest_pages(vcpu);
1685 static int complete_pio(struct kvm_vcpu *vcpu)
1687 struct kvm_pio_request *io = &vcpu->pio;
1691 kvm_arch_ops->cache_regs(vcpu);
1695 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1699 r = pio_copy_data(vcpu);
1701 kvm_arch_ops->cache_regs(vcpu);
1708 delta *= io->cur_count;
1710 * The size of the register should really depend on
1711 * current address size.
1713 vcpu->regs[VCPU_REGS_RCX] -= delta;
1719 vcpu->regs[VCPU_REGS_RDI] += delta;
1721 vcpu->regs[VCPU_REGS_RSI] += delta;
1724 kvm_arch_ops->decache_regs(vcpu);
1726 io->count -= io->cur_count;
1730 kvm_arch_ops->skip_emulated_instruction(vcpu);
1734 static void kernel_pio(struct kvm_io_device *pio_dev,
1735 struct kvm_vcpu *vcpu,
1738 /* TODO: String I/O for in kernel device */
1741 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1745 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1750 static void pio_string_write(struct kvm_io_device *pio_dev,
1751 struct kvm_vcpu *vcpu)
1753 struct kvm_pio_request *io = &vcpu->pio;
1754 void *pd = vcpu->pio_data;
1757 for (i = 0; i < io->cur_count; i++) {
1758 kvm_iodevice_write(pio_dev, io->port,
1765 int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1766 int size, unsigned long count, int string, int down,
1767 gva_t address, int rep, unsigned port)
1769 unsigned now, in_page;
1773 struct kvm_io_device *pio_dev;
1775 vcpu->run->exit_reason = KVM_EXIT_IO;
1776 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1777 vcpu->run->io.size = size;
1778 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1779 vcpu->run->io.count = count;
1780 vcpu->run->io.port = port;
1781 vcpu->pio.count = count;
1782 vcpu->pio.cur_count = count;
1783 vcpu->pio.size = size;
1785 vcpu->pio.port = port;
1786 vcpu->pio.string = string;
1787 vcpu->pio.down = down;
1788 vcpu->pio.guest_page_offset = offset_in_page(address);
1789 vcpu->pio.rep = rep;
1791 pio_dev = vcpu_find_pio_dev(vcpu, port);
1793 kvm_arch_ops->cache_regs(vcpu);
1794 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1795 kvm_arch_ops->decache_regs(vcpu);
1797 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1805 kvm_arch_ops->skip_emulated_instruction(vcpu);
1809 now = min(count, PAGE_SIZE / size);
1812 in_page = PAGE_SIZE - offset_in_page(address);
1814 in_page = offset_in_page(address) + size;
1815 now = min(count, (unsigned long)in_page / size);
1818 * String I/O straddles page boundary. Pin two guest pages
1819 * so that we satisfy atomicity constraints. Do just one
1820 * transaction to avoid complexity.
1827 * String I/O in reverse. Yuck. Kill the guest, fix later.
1829 printk(KERN_ERR "kvm: guest string pio down\n");
1833 vcpu->run->io.count = now;
1834 vcpu->pio.cur_count = now;
1836 for (i = 0; i < nr_pages; ++i) {
1837 mutex_lock(&vcpu->kvm->lock);
1838 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1841 vcpu->pio.guest_pages[i] = page;
1842 mutex_unlock(&vcpu->kvm->lock);
1845 free_pio_guest_pages(vcpu);
1850 if (!vcpu->pio.in) {
1851 /* string PIO write */
1852 ret = pio_copy_data(vcpu);
1853 if (ret >= 0 && pio_dev) {
1854 pio_string_write(pio_dev, vcpu);
1856 if (vcpu->pio.count == 0)
1860 printk(KERN_ERR "no string pio read support yet, "
1861 "port %x size %d count %ld\n",
1866 EXPORT_SYMBOL_GPL(kvm_setup_pio);
1868 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1875 if (vcpu->sigset_active)
1876 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1878 /* re-sync apic's tpr */
1879 vcpu->cr8 = kvm_run->cr8;
1881 if (vcpu->pio.cur_count) {
1882 r = complete_pio(vcpu);
1887 if (vcpu->mmio_needed) {
1888 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1889 vcpu->mmio_read_completed = 1;
1890 vcpu->mmio_needed = 0;
1891 r = emulate_instruction(vcpu, kvm_run,
1892 vcpu->mmio_fault_cr2, 0);
1893 if (r == EMULATE_DO_MMIO) {
1895 * Read-modify-write. Back to userspace.
1902 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
1903 kvm_arch_ops->cache_regs(vcpu);
1904 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1905 kvm_arch_ops->decache_regs(vcpu);
1908 r = kvm_arch_ops->run(vcpu, kvm_run);
1911 if (vcpu->sigset_active)
1912 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1918 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1919 struct kvm_regs *regs)
1923 kvm_arch_ops->cache_regs(vcpu);
1925 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1926 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1927 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1928 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1929 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1930 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1931 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1932 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1933 #ifdef CONFIG_X86_64
1934 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1935 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1936 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1937 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1938 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1939 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1940 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1941 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1944 regs->rip = vcpu->rip;
1945 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1948 * Don't leak debug flags in case they were set for guest debugging
1950 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1951 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1958 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1959 struct kvm_regs *regs)
1963 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1964 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1965 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1966 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1967 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1968 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1969 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1970 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1971 #ifdef CONFIG_X86_64
1972 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1973 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1974 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1975 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1976 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1977 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1978 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1979 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1982 vcpu->rip = regs->rip;
1983 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1985 kvm_arch_ops->decache_regs(vcpu);
1992 static void get_segment(struct kvm_vcpu *vcpu,
1993 struct kvm_segment *var, int seg)
1995 return kvm_arch_ops->get_segment(vcpu, var, seg);
1998 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1999 struct kvm_sregs *sregs)
2001 struct descriptor_table dt;
2005 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2006 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2007 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2008 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2009 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2010 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2012 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2013 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2015 kvm_arch_ops->get_idt(vcpu, &dt);
2016 sregs->idt.limit = dt.limit;
2017 sregs->idt.base = dt.base;
2018 kvm_arch_ops->get_gdt(vcpu, &dt);
2019 sregs->gdt.limit = dt.limit;
2020 sregs->gdt.base = dt.base;
2022 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2023 sregs->cr0 = vcpu->cr0;
2024 sregs->cr2 = vcpu->cr2;
2025 sregs->cr3 = vcpu->cr3;
2026 sregs->cr4 = vcpu->cr4;
2027 sregs->cr8 = vcpu->cr8;
2028 sregs->efer = vcpu->shadow_efer;
2029 sregs->apic_base = vcpu->apic_base;
2031 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2032 sizeof sregs->interrupt_bitmap);
2039 static void set_segment(struct kvm_vcpu *vcpu,
2040 struct kvm_segment *var, int seg)
2042 return kvm_arch_ops->set_segment(vcpu, var, seg);
2045 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2046 struct kvm_sregs *sregs)
2048 int mmu_reset_needed = 0;
2050 struct descriptor_table dt;
2054 dt.limit = sregs->idt.limit;
2055 dt.base = sregs->idt.base;
2056 kvm_arch_ops->set_idt(vcpu, &dt);
2057 dt.limit = sregs->gdt.limit;
2058 dt.base = sregs->gdt.base;
2059 kvm_arch_ops->set_gdt(vcpu, &dt);
2061 vcpu->cr2 = sregs->cr2;
2062 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2063 vcpu->cr3 = sregs->cr3;
2065 vcpu->cr8 = sregs->cr8;
2067 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2068 #ifdef CONFIG_X86_64
2069 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2071 vcpu->apic_base = sregs->apic_base;
2073 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2075 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2076 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
2078 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2079 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
2080 if (!is_long_mode(vcpu) && is_pae(vcpu))
2081 load_pdptrs(vcpu, vcpu->cr3);
2083 if (mmu_reset_needed)
2084 kvm_mmu_reset_context(vcpu);
2086 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2087 sizeof vcpu->irq_pending);
2088 vcpu->irq_summary = 0;
2089 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2090 if (vcpu->irq_pending[i])
2091 __set_bit(i, &vcpu->irq_summary);
2093 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2094 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2095 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2096 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2097 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2098 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2100 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2101 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2109 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2110 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2112 * This list is modified at module load time to reflect the
2113 * capabilities of the host cpu.
2115 static u32 msrs_to_save[] = {
2116 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2118 #ifdef CONFIG_X86_64
2119 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2121 MSR_IA32_TIME_STAMP_COUNTER,
2124 static unsigned num_msrs_to_save;
2126 static u32 emulated_msrs[] = {
2127 MSR_IA32_MISC_ENABLE,
2130 static __init void kvm_init_msr_list(void)
2135 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2136 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2139 msrs_to_save[j] = msrs_to_save[i];
2142 num_msrs_to_save = j;
2146 * Adapt set_msr() to msr_io()'s calling convention
2148 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2150 return kvm_set_msr(vcpu, index, *data);
2154 * Read or write a bunch of msrs. All parameters are kernel addresses.
2156 * @return number of msrs set successfully.
2158 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2159 struct kvm_msr_entry *entries,
2160 int (*do_msr)(struct kvm_vcpu *vcpu,
2161 unsigned index, u64 *data))
2167 for (i = 0; i < msrs->nmsrs; ++i)
2168 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2177 * Read or write a bunch of msrs. Parameters are user addresses.
2179 * @return number of msrs set successfully.
2181 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2182 int (*do_msr)(struct kvm_vcpu *vcpu,
2183 unsigned index, u64 *data),
2186 struct kvm_msrs msrs;
2187 struct kvm_msr_entry *entries;
2192 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2196 if (msrs.nmsrs >= MAX_IO_MSRS)
2200 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2201 entries = vmalloc(size);
2206 if (copy_from_user(entries, user_msrs->entries, size))
2209 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2214 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2226 * Translate a guest virtual address to a guest physical address.
2228 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2229 struct kvm_translation *tr)
2231 unsigned long vaddr = tr->linear_address;
2235 mutex_lock(&vcpu->kvm->lock);
2236 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2237 tr->physical_address = gpa;
2238 tr->valid = gpa != UNMAPPED_GVA;
2241 mutex_unlock(&vcpu->kvm->lock);
2247 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2248 struct kvm_interrupt *irq)
2250 if (irq->irq < 0 || irq->irq >= 256)
2254 set_bit(irq->irq, vcpu->irq_pending);
2255 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2262 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2263 struct kvm_debug_guest *dbg)
2269 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2276 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2277 unsigned long address,
2280 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2281 unsigned long pgoff;
2284 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2286 page = virt_to_page(vcpu->run);
2287 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2288 page = virt_to_page(vcpu->pio_data);
2290 return NOPAGE_SIGBUS;
2293 *type = VM_FAULT_MINOR;
2298 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2299 .nopage = kvm_vcpu_nopage,
2302 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2304 vma->vm_ops = &kvm_vcpu_vm_ops;
2308 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2310 struct kvm_vcpu *vcpu = filp->private_data;
2312 fput(vcpu->kvm->filp);
2316 static struct file_operations kvm_vcpu_fops = {
2317 .release = kvm_vcpu_release,
2318 .unlocked_ioctl = kvm_vcpu_ioctl,
2319 .compat_ioctl = kvm_vcpu_ioctl,
2320 .mmap = kvm_vcpu_mmap,
2324 * Allocates an inode for the vcpu.
2326 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2329 struct inode *inode;
2332 r = anon_inode_getfd(&fd, &inode, &file,
2333 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2336 atomic_inc(&vcpu->kvm->filp->f_count);
2341 * Creates some virtual cpus. Good luck creating more than one.
2343 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2346 struct kvm_vcpu *vcpu;
2351 vcpu = kvm_arch_ops->vcpu_create(kvm, n);
2353 return PTR_ERR(vcpu);
2355 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2358 r = kvm_mmu_setup(vcpu);
2363 mutex_lock(&kvm->lock);
2364 if (kvm->vcpus[n]) {
2366 mutex_unlock(&kvm->lock);
2369 kvm->vcpus[n] = vcpu;
2370 mutex_unlock(&kvm->lock);
2372 /* Now it's all set up, let userspace reach it */
2373 r = create_vcpu_fd(vcpu);
2379 mutex_lock(&kvm->lock);
2380 kvm->vcpus[n] = NULL;
2381 mutex_unlock(&kvm->lock);
2385 kvm_mmu_unload(vcpu);
2389 kvm_arch_ops->vcpu_free(vcpu);
2393 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2397 struct kvm_cpuid_entry *e, *entry;
2399 rdmsrl(MSR_EFER, efer);
2401 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2402 e = &vcpu->cpuid_entries[i];
2403 if (e->function == 0x80000001) {
2408 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
2409 entry->edx &= ~(1 << 20);
2410 printk(KERN_INFO "kvm: guest NX capability removed\n");
2414 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2415 struct kvm_cpuid *cpuid,
2416 struct kvm_cpuid_entry __user *entries)
2421 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2424 if (copy_from_user(&vcpu->cpuid_entries, entries,
2425 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2427 vcpu->cpuid_nent = cpuid->nent;
2428 cpuid_fix_nx_cap(vcpu);
2435 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2438 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2439 vcpu->sigset_active = 1;
2440 vcpu->sigset = *sigset;
2442 vcpu->sigset_active = 0;
2447 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2448 * we have asm/x86/processor.h
2459 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2460 #ifdef CONFIG_X86_64
2461 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2463 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2467 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2469 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2473 memcpy(fpu->fpr, fxsave->st_space, 128);
2474 fpu->fcw = fxsave->cwd;
2475 fpu->fsw = fxsave->swd;
2476 fpu->ftwx = fxsave->twd;
2477 fpu->last_opcode = fxsave->fop;
2478 fpu->last_ip = fxsave->rip;
2479 fpu->last_dp = fxsave->rdp;
2480 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2487 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2489 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2493 memcpy(fxsave->st_space, fpu->fpr, 128);
2494 fxsave->cwd = fpu->fcw;
2495 fxsave->swd = fpu->fsw;
2496 fxsave->twd = fpu->ftwx;
2497 fxsave->fop = fpu->last_opcode;
2498 fxsave->rip = fpu->last_ip;
2499 fxsave->rdp = fpu->last_dp;
2500 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2507 static long kvm_vcpu_ioctl(struct file *filp,
2508 unsigned int ioctl, unsigned long arg)
2510 struct kvm_vcpu *vcpu = filp->private_data;
2511 void __user *argp = (void __user *)arg;
2519 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2521 case KVM_GET_REGS: {
2522 struct kvm_regs kvm_regs;
2524 memset(&kvm_regs, 0, sizeof kvm_regs);
2525 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2529 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2534 case KVM_SET_REGS: {
2535 struct kvm_regs kvm_regs;
2538 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2540 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2546 case KVM_GET_SREGS: {
2547 struct kvm_sregs kvm_sregs;
2549 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2550 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2554 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2559 case KVM_SET_SREGS: {
2560 struct kvm_sregs kvm_sregs;
2563 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2565 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2571 case KVM_TRANSLATE: {
2572 struct kvm_translation tr;
2575 if (copy_from_user(&tr, argp, sizeof tr))
2577 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2581 if (copy_to_user(argp, &tr, sizeof tr))
2586 case KVM_INTERRUPT: {
2587 struct kvm_interrupt irq;
2590 if (copy_from_user(&irq, argp, sizeof irq))
2592 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2598 case KVM_DEBUG_GUEST: {
2599 struct kvm_debug_guest dbg;
2602 if (copy_from_user(&dbg, argp, sizeof dbg))
2604 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2611 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2614 r = msr_io(vcpu, argp, do_set_msr, 0);
2616 case KVM_SET_CPUID: {
2617 struct kvm_cpuid __user *cpuid_arg = argp;
2618 struct kvm_cpuid cpuid;
2621 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2623 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2628 case KVM_SET_SIGNAL_MASK: {
2629 struct kvm_signal_mask __user *sigmask_arg = argp;
2630 struct kvm_signal_mask kvm_sigmask;
2631 sigset_t sigset, *p;
2636 if (copy_from_user(&kvm_sigmask, argp,
2637 sizeof kvm_sigmask))
2640 if (kvm_sigmask.len != sizeof sigset)
2643 if (copy_from_user(&sigset, sigmask_arg->sigset,
2648 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2654 memset(&fpu, 0, sizeof fpu);
2655 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2659 if (copy_to_user(argp, &fpu, sizeof fpu))
2668 if (copy_from_user(&fpu, argp, sizeof fpu))
2670 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2683 static long kvm_vm_ioctl(struct file *filp,
2684 unsigned int ioctl, unsigned long arg)
2686 struct kvm *kvm = filp->private_data;
2687 void __user *argp = (void __user *)arg;
2691 case KVM_CREATE_VCPU:
2692 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2696 case KVM_SET_MEMORY_REGION: {
2697 struct kvm_memory_region kvm_mem;
2700 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2702 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
2707 case KVM_GET_DIRTY_LOG: {
2708 struct kvm_dirty_log log;
2711 if (copy_from_user(&log, argp, sizeof log))
2713 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2718 case KVM_SET_MEMORY_ALIAS: {
2719 struct kvm_memory_alias alias;
2722 if (copy_from_user(&alias, argp, sizeof alias))
2724 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2736 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2737 unsigned long address,
2740 struct kvm *kvm = vma->vm_file->private_data;
2741 unsigned long pgoff;
2744 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2745 page = gfn_to_page(kvm, pgoff);
2747 return NOPAGE_SIGBUS;
2750 *type = VM_FAULT_MINOR;
2755 static struct vm_operations_struct kvm_vm_vm_ops = {
2756 .nopage = kvm_vm_nopage,
2759 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2761 vma->vm_ops = &kvm_vm_vm_ops;
2765 static struct file_operations kvm_vm_fops = {
2766 .release = kvm_vm_release,
2767 .unlocked_ioctl = kvm_vm_ioctl,
2768 .compat_ioctl = kvm_vm_ioctl,
2769 .mmap = kvm_vm_mmap,
2772 static int kvm_dev_ioctl_create_vm(void)
2775 struct inode *inode;
2779 kvm = kvm_create_vm();
2781 return PTR_ERR(kvm);
2782 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2784 kvm_destroy_vm(kvm);
2793 static long kvm_dev_ioctl(struct file *filp,
2794 unsigned int ioctl, unsigned long arg)
2796 void __user *argp = (void __user *)arg;
2800 case KVM_GET_API_VERSION:
2804 r = KVM_API_VERSION;
2810 r = kvm_dev_ioctl_create_vm();
2812 case KVM_GET_MSR_INDEX_LIST: {
2813 struct kvm_msr_list __user *user_msr_list = argp;
2814 struct kvm_msr_list msr_list;
2818 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2821 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2822 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2825 if (n < num_msrs_to_save)
2828 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2829 num_msrs_to_save * sizeof(u32)))
2831 if (copy_to_user(user_msr_list->indices
2832 + num_msrs_to_save * sizeof(u32),
2834 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2839 case KVM_CHECK_EXTENSION:
2841 * No extensions defined at present.
2845 case KVM_GET_VCPU_MMAP_SIZE:
2858 static struct file_operations kvm_chardev_ops = {
2859 .open = kvm_dev_open,
2860 .release = kvm_dev_release,
2861 .unlocked_ioctl = kvm_dev_ioctl,
2862 .compat_ioctl = kvm_dev_ioctl,
2865 static struct miscdevice kvm_dev = {
2872 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2875 static void decache_vcpus_on_cpu(int cpu)
2878 struct kvm_vcpu *vcpu;
2881 spin_lock(&kvm_lock);
2882 list_for_each_entry(vm, &vm_list, vm_list)
2883 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2884 vcpu = vm->vcpus[i];
2888 * If the vcpu is locked, then it is running on some
2889 * other cpu and therefore it is not cached on the
2892 * If it's not locked, check the last cpu it executed
2895 if (mutex_trylock(&vcpu->mutex)) {
2896 if (vcpu->cpu == cpu) {
2897 kvm_arch_ops->vcpu_decache(vcpu);
2900 mutex_unlock(&vcpu->mutex);
2903 spin_unlock(&kvm_lock);
2906 static void hardware_enable(void *junk)
2908 int cpu = raw_smp_processor_id();
2910 if (cpu_isset(cpu, cpus_hardware_enabled))
2912 cpu_set(cpu, cpus_hardware_enabled);
2913 kvm_arch_ops->hardware_enable(NULL);
2916 static void hardware_disable(void *junk)
2918 int cpu = raw_smp_processor_id();
2920 if (!cpu_isset(cpu, cpus_hardware_enabled))
2922 cpu_clear(cpu, cpus_hardware_enabled);
2923 decache_vcpus_on_cpu(cpu);
2924 kvm_arch_ops->hardware_disable(NULL);
2927 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2934 case CPU_DYING_FROZEN:
2935 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2937 hardware_disable(NULL);
2939 case CPU_UP_CANCELED:
2940 case CPU_UP_CANCELED_FROZEN:
2941 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2943 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
2946 case CPU_ONLINE_FROZEN:
2947 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2949 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
2955 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2958 if (val == SYS_RESTART) {
2960 * Some (well, at least mine) BIOSes hang on reboot if
2963 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2964 on_each_cpu(hardware_disable, NULL, 0, 1);
2969 static struct notifier_block kvm_reboot_notifier = {
2970 .notifier_call = kvm_reboot,
2974 void kvm_io_bus_init(struct kvm_io_bus *bus)
2976 memset(bus, 0, sizeof(*bus));
2979 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2983 for (i = 0; i < bus->dev_count; i++) {
2984 struct kvm_io_device *pos = bus->devs[i];
2986 kvm_iodevice_destructor(pos);
2990 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
2994 for (i = 0; i < bus->dev_count; i++) {
2995 struct kvm_io_device *pos = bus->devs[i];
2997 if (pos->in_range(pos, addr))
3004 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3006 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3008 bus->devs[bus->dev_count++] = dev;
3011 static struct notifier_block kvm_cpu_notifier = {
3012 .notifier_call = kvm_cpu_hotplug,
3013 .priority = 20, /* must be > scheduler priority */
3016 static u64 stat_get(void *_offset)
3018 unsigned offset = (long)_offset;
3021 struct kvm_vcpu *vcpu;
3024 spin_lock(&kvm_lock);
3025 list_for_each_entry(kvm, &vm_list, vm_list)
3026 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3027 vcpu = kvm->vcpus[i];
3029 total += *(u32 *)((void *)vcpu + offset);
3031 spin_unlock(&kvm_lock);
3035 static void stat_set(void *offset, u64 val)
3039 DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3041 static __init void kvm_init_debug(void)
3043 struct kvm_stats_debugfs_item *p;
3045 debugfs_dir = debugfs_create_dir("kvm", NULL);
3046 for (p = debugfs_entries; p->name; ++p)
3047 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3048 (void *)(long)p->offset,
3052 static void kvm_exit_debug(void)
3054 struct kvm_stats_debugfs_item *p;
3056 for (p = debugfs_entries; p->name; ++p)
3057 debugfs_remove(p->dentry);
3058 debugfs_remove(debugfs_dir);
3061 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3063 hardware_disable(NULL);
3067 static int kvm_resume(struct sys_device *dev)
3069 hardware_enable(NULL);
3073 static struct sysdev_class kvm_sysdev_class = {
3074 set_kset_name("kvm"),
3075 .suspend = kvm_suspend,
3076 .resume = kvm_resume,
3079 static struct sys_device kvm_sysdev = {
3081 .cls = &kvm_sysdev_class,
3084 hpa_t bad_page_address;
3087 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3089 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3092 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3094 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3096 kvm_arch_ops->vcpu_load(vcpu, cpu);
3099 static void kvm_sched_out(struct preempt_notifier *pn,
3100 struct task_struct *next)
3102 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3104 kvm_arch_ops->vcpu_put(vcpu);
3107 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3112 printk(KERN_ERR "kvm: already loaded the other module\n");
3116 if (!ops->cpu_has_kvm_support()) {
3117 printk(KERN_ERR "kvm: no hardware support\n");
3120 if (ops->disabled_by_bios()) {
3121 printk(KERN_ERR "kvm: disabled by bios\n");
3127 r = kvm_arch_ops->hardware_setup();
3131 on_each_cpu(hardware_enable, NULL, 0, 1);
3132 r = register_cpu_notifier(&kvm_cpu_notifier);
3135 register_reboot_notifier(&kvm_reboot_notifier);
3137 r = sysdev_class_register(&kvm_sysdev_class);
3141 r = sysdev_register(&kvm_sysdev);
3145 kvm_chardev_ops.owner = module;
3147 r = misc_register(&kvm_dev);
3149 printk (KERN_ERR "kvm: misc device register failed\n");
3153 kvm_preempt_ops.sched_in = kvm_sched_in;
3154 kvm_preempt_ops.sched_out = kvm_sched_out;
3159 sysdev_unregister(&kvm_sysdev);
3161 sysdev_class_unregister(&kvm_sysdev_class);
3163 unregister_reboot_notifier(&kvm_reboot_notifier);
3164 unregister_cpu_notifier(&kvm_cpu_notifier);
3166 on_each_cpu(hardware_disable, NULL, 0, 1);
3167 kvm_arch_ops->hardware_unsetup();
3169 kvm_arch_ops = NULL;
3173 void kvm_exit_arch(void)
3175 misc_deregister(&kvm_dev);
3176 sysdev_unregister(&kvm_sysdev);
3177 sysdev_class_unregister(&kvm_sysdev_class);
3178 unregister_reboot_notifier(&kvm_reboot_notifier);
3179 unregister_cpu_notifier(&kvm_cpu_notifier);
3180 on_each_cpu(hardware_disable, NULL, 0, 1);
3181 kvm_arch_ops->hardware_unsetup();
3182 kvm_arch_ops = NULL;
3185 static __init int kvm_init(void)
3187 static struct page *bad_page;
3190 r = kvm_mmu_module_init();
3196 kvm_init_msr_list();
3198 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3203 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3204 memset(__va(bad_page_address), 0, PAGE_SIZE);
3210 kvm_mmu_module_exit();
3215 static __exit void kvm_exit(void)
3218 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3219 kvm_mmu_module_exit();
3222 module_init(kvm_init)
3223 module_exit(kvm_exit)
3225 EXPORT_SYMBOL_GPL(kvm_init_arch);
3226 EXPORT_SYMBOL_GPL(kvm_exit_arch);