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 void hardware_disable(void *ignored);
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_RESEVED_BITS 0xffffffff1ffaffc0ULL
88 #define LMSW_GUEST_MASK 0x0eULL
89 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
90 #define CR8_RESEVED_BITS (~0x0fULL)
91 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
94 // LDT or TSS descriptor in the GDT. 16 bytes.
95 struct segment_descriptor_64 {
96 struct segment_descriptor s;
103 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
106 unsigned long segment_base(u16 selector)
108 struct descriptor_table gdt;
109 struct segment_descriptor *d;
110 unsigned long table_base;
111 typedef unsigned long ul;
117 asm ("sgdt %0" : "=m"(gdt));
118 table_base = gdt.base;
120 if (selector & 4) { /* from ldt */
123 asm ("sldt %0" : "=g"(ldt_selector));
124 table_base = segment_base(ldt_selector);
126 d = (struct segment_descriptor *)(table_base + (selector & ~7));
127 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
130 && (d->type == 2 || d->type == 9 || d->type == 11))
131 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
135 EXPORT_SYMBOL_GPL(segment_base);
137 static inline int valid_vcpu(int n)
139 return likely(n >= 0 && n < KVM_MAX_VCPUS);
142 int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
145 unsigned char *host_buf = dest;
146 unsigned long req_size = size;
154 paddr = gva_to_hpa(vcpu, addr);
156 if (is_error_hpa(paddr))
159 guest_buf = (hva_t)kmap_atomic(
160 pfn_to_page(paddr >> PAGE_SHIFT),
162 offset = addr & ~PAGE_MASK;
164 now = min(size, PAGE_SIZE - offset);
165 memcpy(host_buf, (void*)guest_buf, now);
169 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
171 return req_size - size;
173 EXPORT_SYMBOL_GPL(kvm_read_guest);
175 int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
178 unsigned char *host_buf = data;
179 unsigned long req_size = size;
188 paddr = gva_to_hpa(vcpu, addr);
190 if (is_error_hpa(paddr))
193 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
194 mark_page_dirty(vcpu->kvm, gfn);
195 guest_buf = (hva_t)kmap_atomic(
196 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
197 offset = addr & ~PAGE_MASK;
199 now = min(size, PAGE_SIZE - offset);
200 memcpy((void*)guest_buf, host_buf, now);
204 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
206 return req_size - size;
208 EXPORT_SYMBOL_GPL(kvm_write_guest);
210 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
212 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
215 vcpu->guest_fpu_loaded = 1;
216 fx_save(vcpu->host_fx_image);
217 fx_restore(vcpu->guest_fx_image);
219 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
221 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
223 if (!vcpu->guest_fpu_loaded)
226 vcpu->guest_fpu_loaded = 0;
227 fx_save(vcpu->guest_fx_image);
228 fx_restore(vcpu->host_fx_image);
230 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
233 * Switches to specified vcpu, until a matching vcpu_put()
235 static void vcpu_load(struct kvm_vcpu *vcpu)
237 mutex_lock(&vcpu->mutex);
238 kvm_arch_ops->vcpu_load(vcpu);
241 static void vcpu_put(struct kvm_vcpu *vcpu)
243 kvm_arch_ops->vcpu_put(vcpu);
244 mutex_unlock(&vcpu->mutex);
247 static void ack_flush(void *_completed)
249 atomic_t *completed = _completed;
251 atomic_inc(completed);
254 void kvm_flush_remote_tlbs(struct kvm *kvm)
258 struct kvm_vcpu *vcpu;
261 atomic_set(&completed, 0);
264 for (i = 0; i < kvm->nvcpus; ++i) {
265 vcpu = &kvm->vcpus[i];
266 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
269 if (cpu != -1 && cpu != raw_smp_processor_id())
270 if (!cpu_isset(cpu, cpus)) {
277 * We really want smp_call_function_mask() here. But that's not
278 * available, so ipi all cpus in parallel and wait for them
281 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
282 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
283 while (atomic_read(&completed) != needed) {
289 static struct kvm *kvm_create_vm(void)
291 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
295 return ERR_PTR(-ENOMEM);
297 kvm_io_bus_init(&kvm->pio_bus);
298 spin_lock_init(&kvm->lock);
299 INIT_LIST_HEAD(&kvm->active_mmu_pages);
300 spin_lock(&kvm_lock);
301 list_add(&kvm->vm_list, &vm_list);
302 spin_unlock(&kvm_lock);
303 kvm_io_bus_init(&kvm->mmio_bus);
304 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
305 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
307 mutex_init(&vcpu->mutex);
310 vcpu->mmu.root_hpa = INVALID_PAGE;
315 static int kvm_dev_open(struct inode *inode, struct file *filp)
321 * Free any memory in @free but not in @dont.
323 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
324 struct kvm_memory_slot *dont)
328 if (!dont || free->phys_mem != dont->phys_mem)
329 if (free->phys_mem) {
330 for (i = 0; i < free->npages; ++i)
331 if (free->phys_mem[i])
332 __free_page(free->phys_mem[i]);
333 vfree(free->phys_mem);
336 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
337 vfree(free->dirty_bitmap);
339 free->phys_mem = NULL;
341 free->dirty_bitmap = NULL;
344 static void kvm_free_physmem(struct kvm *kvm)
348 for (i = 0; i < kvm->nmemslots; ++i)
349 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
352 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
356 for (i = 0; i < 2; ++i)
357 if (vcpu->pio.guest_pages[i]) {
358 __free_page(vcpu->pio.guest_pages[i]);
359 vcpu->pio.guest_pages[i] = NULL;
363 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
369 kvm_mmu_unload(vcpu);
373 static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
379 kvm_mmu_destroy(vcpu);
381 kvm_arch_ops->vcpu_free(vcpu);
382 free_page((unsigned long)vcpu->run);
384 free_page((unsigned long)vcpu->pio_data);
385 vcpu->pio_data = NULL;
386 free_pio_guest_pages(vcpu);
389 static void kvm_free_vcpus(struct kvm *kvm)
394 * Unpin any mmu pages first.
396 for (i = 0; i < KVM_MAX_VCPUS; ++i)
397 kvm_unload_vcpu_mmu(&kvm->vcpus[i]);
398 for (i = 0; i < KVM_MAX_VCPUS; ++i)
399 kvm_free_vcpu(&kvm->vcpus[i]);
402 static int kvm_dev_release(struct inode *inode, struct file *filp)
407 static void kvm_destroy_vm(struct kvm *kvm)
409 spin_lock(&kvm_lock);
410 list_del(&kvm->vm_list);
411 spin_unlock(&kvm_lock);
412 kvm_io_bus_destroy(&kvm->pio_bus);
413 kvm_io_bus_destroy(&kvm->mmio_bus);
415 kvm_free_physmem(kvm);
419 static int kvm_vm_release(struct inode *inode, struct file *filp)
421 struct kvm *kvm = filp->private_data;
427 static void inject_gp(struct kvm_vcpu *vcpu)
429 kvm_arch_ops->inject_gp(vcpu, 0);
433 * Load the pae pdptrs. Return true is they are all valid.
435 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
437 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
438 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
445 spin_lock(&vcpu->kvm->lock);
446 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
447 /* FIXME: !page - emulate? 0xff? */
448 pdpt = kmap_atomic(page, KM_USER0);
451 for (i = 0; i < 4; ++i) {
452 pdpte = pdpt[offset + i];
453 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
459 for (i = 0; i < 4; ++i)
460 vcpu->pdptrs[i] = pdpt[offset + i];
463 kunmap_atomic(pdpt, KM_USER0);
464 spin_unlock(&vcpu->kvm->lock);
469 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
471 if (cr0 & CR0_RESEVED_BITS) {
472 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
478 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
479 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
484 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
485 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
486 "and a clear PE flag\n");
491 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
493 if ((vcpu->shadow_efer & EFER_LME)) {
497 printk(KERN_DEBUG "set_cr0: #GP, start paging "
498 "in long mode while PAE is disabled\n");
502 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
504 printk(KERN_DEBUG "set_cr0: #GP, start paging "
505 "in long mode while CS.L == 1\n");
512 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
513 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
521 kvm_arch_ops->set_cr0(vcpu, cr0);
524 spin_lock(&vcpu->kvm->lock);
525 kvm_mmu_reset_context(vcpu);
526 spin_unlock(&vcpu->kvm->lock);
529 EXPORT_SYMBOL_GPL(set_cr0);
531 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
533 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
535 EXPORT_SYMBOL_GPL(lmsw);
537 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
539 if (cr4 & CR4_RESEVED_BITS) {
540 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
545 if (is_long_mode(vcpu)) {
546 if (!(cr4 & CR4_PAE_MASK)) {
547 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
552 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
553 && !load_pdptrs(vcpu, vcpu->cr3)) {
554 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
558 if (cr4 & CR4_VMXE_MASK) {
559 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
563 kvm_arch_ops->set_cr4(vcpu, cr4);
564 spin_lock(&vcpu->kvm->lock);
565 kvm_mmu_reset_context(vcpu);
566 spin_unlock(&vcpu->kvm->lock);
568 EXPORT_SYMBOL_GPL(set_cr4);
570 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
572 if (is_long_mode(vcpu)) {
573 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
574 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
579 if (cr3 & CR3_RESEVED_BITS) {
580 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
584 if (is_paging(vcpu) && is_pae(vcpu) &&
585 !load_pdptrs(vcpu, cr3)) {
586 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
594 spin_lock(&vcpu->kvm->lock);
596 * Does the new cr3 value map to physical memory? (Note, we
597 * catch an invalid cr3 even in real-mode, because it would
598 * cause trouble later on when we turn on paging anyway.)
600 * A real CPU would silently accept an invalid cr3 and would
601 * attempt to use it - with largely undefined (and often hard
602 * to debug) behavior on the guest side.
604 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
607 vcpu->mmu.new_cr3(vcpu);
608 spin_unlock(&vcpu->kvm->lock);
610 EXPORT_SYMBOL_GPL(set_cr3);
612 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
614 if ( cr8 & CR8_RESEVED_BITS) {
615 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
621 EXPORT_SYMBOL_GPL(set_cr8);
623 void fx_init(struct kvm_vcpu *vcpu)
625 struct __attribute__ ((__packed__)) fx_image_s {
631 u64 operand;// fpu dp
637 fx_save(vcpu->host_fx_image);
639 fx_save(vcpu->guest_fx_image);
640 fx_restore(vcpu->host_fx_image);
642 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
643 fx_image->mxcsr = 0x1f80;
644 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
645 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
647 EXPORT_SYMBOL_GPL(fx_init);
650 * Allocate some memory and give it an address in the guest physical address
653 * Discontiguous memory is allowed, mostly for framebuffers.
655 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
656 struct kvm_memory_region *mem)
660 unsigned long npages;
662 struct kvm_memory_slot *memslot;
663 struct kvm_memory_slot old, new;
664 int memory_config_version;
667 /* General sanity checks */
668 if (mem->memory_size & (PAGE_SIZE - 1))
670 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
672 if (mem->slot >= KVM_MEMORY_SLOTS)
674 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
677 memslot = &kvm->memslots[mem->slot];
678 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
679 npages = mem->memory_size >> PAGE_SHIFT;
682 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
685 spin_lock(&kvm->lock);
687 memory_config_version = kvm->memory_config_version;
688 new = old = *memslot;
690 new.base_gfn = base_gfn;
692 new.flags = mem->flags;
694 /* Disallow changing a memory slot's size. */
696 if (npages && old.npages && npages != old.npages)
699 /* Check for overlaps */
701 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
702 struct kvm_memory_slot *s = &kvm->memslots[i];
706 if (!((base_gfn + npages <= s->base_gfn) ||
707 (base_gfn >= s->base_gfn + s->npages)))
711 * Do memory allocations outside lock. memory_config_version will
714 spin_unlock(&kvm->lock);
716 /* Deallocate if slot is being removed */
720 /* Free page dirty bitmap if unneeded */
721 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
722 new.dirty_bitmap = NULL;
726 /* Allocate if a slot is being created */
727 if (npages && !new.phys_mem) {
728 new.phys_mem = vmalloc(npages * sizeof(struct page *));
733 memset(new.phys_mem, 0, npages * sizeof(struct page *));
734 for (i = 0; i < npages; ++i) {
735 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
737 if (!new.phys_mem[i])
739 set_page_private(new.phys_mem[i],0);
743 /* Allocate page dirty bitmap if needed */
744 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
745 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
747 new.dirty_bitmap = vmalloc(dirty_bytes);
748 if (!new.dirty_bitmap)
750 memset(new.dirty_bitmap, 0, dirty_bytes);
753 spin_lock(&kvm->lock);
755 if (memory_config_version != kvm->memory_config_version) {
756 spin_unlock(&kvm->lock);
757 kvm_free_physmem_slot(&new, &old);
765 if (mem->slot >= kvm->nmemslots)
766 kvm->nmemslots = mem->slot + 1;
769 ++kvm->memory_config_version;
771 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
772 kvm_flush_remote_tlbs(kvm);
774 spin_unlock(&kvm->lock);
776 kvm_free_physmem_slot(&old, &new);
780 spin_unlock(&kvm->lock);
782 kvm_free_physmem_slot(&new, &old);
788 * Get (and clear) the dirty memory log for a memory slot.
790 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
791 struct kvm_dirty_log *log)
793 struct kvm_memory_slot *memslot;
796 unsigned long any = 0;
798 spin_lock(&kvm->lock);
801 * Prevent changes to guest memory configuration even while the lock
805 spin_unlock(&kvm->lock);
807 if (log->slot >= KVM_MEMORY_SLOTS)
810 memslot = &kvm->memslots[log->slot];
812 if (!memslot->dirty_bitmap)
815 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
817 for (i = 0; !any && i < n/sizeof(long); ++i)
818 any = memslot->dirty_bitmap[i];
821 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
824 spin_lock(&kvm->lock);
825 kvm_mmu_slot_remove_write_access(kvm, log->slot);
826 kvm_flush_remote_tlbs(kvm);
827 memset(memslot->dirty_bitmap, 0, n);
828 spin_unlock(&kvm->lock);
833 spin_lock(&kvm->lock);
835 spin_unlock(&kvm->lock);
840 * Set a new alias region. Aliases map a portion of physical memory into
841 * another portion. This is useful for memory windows, for example the PC
844 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
845 struct kvm_memory_alias *alias)
848 struct kvm_mem_alias *p;
851 /* General sanity checks */
852 if (alias->memory_size & (PAGE_SIZE - 1))
854 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
856 if (alias->slot >= KVM_ALIAS_SLOTS)
858 if (alias->guest_phys_addr + alias->memory_size
859 < alias->guest_phys_addr)
861 if (alias->target_phys_addr + alias->memory_size
862 < alias->target_phys_addr)
865 spin_lock(&kvm->lock);
867 p = &kvm->aliases[alias->slot];
868 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
869 p->npages = alias->memory_size >> PAGE_SHIFT;
870 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
872 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
873 if (kvm->aliases[n - 1].npages)
877 kvm_mmu_zap_all(kvm);
879 spin_unlock(&kvm->lock);
887 static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
890 struct kvm_mem_alias *alias;
892 for (i = 0; i < kvm->naliases; ++i) {
893 alias = &kvm->aliases[i];
894 if (gfn >= alias->base_gfn
895 && gfn < alias->base_gfn + alias->npages)
896 return alias->target_gfn + gfn - alias->base_gfn;
901 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
905 for (i = 0; i < kvm->nmemslots; ++i) {
906 struct kvm_memory_slot *memslot = &kvm->memslots[i];
908 if (gfn >= memslot->base_gfn
909 && gfn < memslot->base_gfn + memslot->npages)
915 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
917 gfn = unalias_gfn(kvm, gfn);
918 return __gfn_to_memslot(kvm, gfn);
921 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
923 struct kvm_memory_slot *slot;
925 gfn = unalias_gfn(kvm, gfn);
926 slot = __gfn_to_memslot(kvm, gfn);
929 return slot->phys_mem[gfn - slot->base_gfn];
931 EXPORT_SYMBOL_GPL(gfn_to_page);
933 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
936 struct kvm_memory_slot *memslot;
937 unsigned long rel_gfn;
939 for (i = 0; i < kvm->nmemslots; ++i) {
940 memslot = &kvm->memslots[i];
942 if (gfn >= memslot->base_gfn
943 && gfn < memslot->base_gfn + memslot->npages) {
945 if (!memslot->dirty_bitmap)
948 rel_gfn = gfn - memslot->base_gfn;
951 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
952 set_bit(rel_gfn, memslot->dirty_bitmap);
958 static int emulator_read_std(unsigned long addr,
961 struct x86_emulate_ctxt *ctxt)
963 struct kvm_vcpu *vcpu = ctxt->vcpu;
967 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
968 unsigned offset = addr & (PAGE_SIZE-1);
969 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
974 if (gpa == UNMAPPED_GVA)
975 return X86EMUL_PROPAGATE_FAULT;
976 pfn = gpa >> PAGE_SHIFT;
977 page = gfn_to_page(vcpu->kvm, pfn);
979 return X86EMUL_UNHANDLEABLE;
980 page_virt = kmap_atomic(page, KM_USER0);
982 memcpy(data, page_virt + offset, tocopy);
984 kunmap_atomic(page_virt, KM_USER0);
991 return X86EMUL_CONTINUE;
994 static int emulator_write_std(unsigned long addr,
997 struct x86_emulate_ctxt *ctxt)
999 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1001 return X86EMUL_UNHANDLEABLE;
1004 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1008 * Note that its important to have this wrapper function because
1009 * in the very near future we will be checking for MMIOs against
1010 * the LAPIC as well as the general MMIO bus
1012 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1015 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1018 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1021 static int emulator_read_emulated(unsigned long addr,
1024 struct x86_emulate_ctxt *ctxt)
1026 struct kvm_vcpu *vcpu = ctxt->vcpu;
1027 struct kvm_io_device *mmio_dev;
1030 if (vcpu->mmio_read_completed) {
1031 memcpy(val, vcpu->mmio_data, bytes);
1032 vcpu->mmio_read_completed = 0;
1033 return X86EMUL_CONTINUE;
1034 } else if (emulator_read_std(addr, val, bytes, ctxt)
1035 == X86EMUL_CONTINUE)
1036 return X86EMUL_CONTINUE;
1038 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1039 if (gpa == UNMAPPED_GVA)
1040 return X86EMUL_PROPAGATE_FAULT;
1043 * Is this MMIO handled locally?
1045 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1047 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1048 return X86EMUL_CONTINUE;
1051 vcpu->mmio_needed = 1;
1052 vcpu->mmio_phys_addr = gpa;
1053 vcpu->mmio_size = bytes;
1054 vcpu->mmio_is_write = 0;
1056 return X86EMUL_UNHANDLEABLE;
1059 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1060 const void *val, int bytes)
1064 unsigned offset = offset_in_page(gpa);
1066 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1068 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1071 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
1072 virt = kmap_atomic(page, KM_USER0);
1073 if (memcmp(virt + offset_in_page(gpa), val, bytes)) {
1074 kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
1075 memcpy(virt + offset_in_page(gpa), val, bytes);
1077 kunmap_atomic(virt, KM_USER0);
1081 static int emulator_write_emulated(unsigned long addr,
1084 struct x86_emulate_ctxt *ctxt)
1086 struct kvm_vcpu *vcpu = ctxt->vcpu;
1087 struct kvm_io_device *mmio_dev;
1088 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1090 if (gpa == UNMAPPED_GVA) {
1091 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
1092 return X86EMUL_PROPAGATE_FAULT;
1095 if (emulator_write_phys(vcpu, gpa, val, bytes))
1096 return X86EMUL_CONTINUE;
1099 * Is this MMIO handled locally?
1101 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1103 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1104 return X86EMUL_CONTINUE;
1107 vcpu->mmio_needed = 1;
1108 vcpu->mmio_phys_addr = gpa;
1109 vcpu->mmio_size = bytes;
1110 vcpu->mmio_is_write = 1;
1111 memcpy(vcpu->mmio_data, val, bytes);
1113 return X86EMUL_CONTINUE;
1116 static int emulator_cmpxchg_emulated(unsigned long addr,
1120 struct x86_emulate_ctxt *ctxt)
1122 static int reported;
1126 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1128 return emulator_write_emulated(addr, new, bytes, ctxt);
1131 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1133 return kvm_arch_ops->get_segment_base(vcpu, seg);
1136 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1138 return X86EMUL_CONTINUE;
1141 int emulate_clts(struct kvm_vcpu *vcpu)
1145 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
1146 kvm_arch_ops->set_cr0(vcpu, cr0);
1147 return X86EMUL_CONTINUE;
1150 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1152 struct kvm_vcpu *vcpu = ctxt->vcpu;
1156 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1157 return X86EMUL_CONTINUE;
1159 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1161 return X86EMUL_UNHANDLEABLE;
1165 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1167 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1170 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1172 /* FIXME: better handling */
1173 return X86EMUL_UNHANDLEABLE;
1175 return X86EMUL_CONTINUE;
1178 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1180 static int reported;
1182 unsigned long rip = ctxt->vcpu->rip;
1183 unsigned long rip_linear;
1185 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1190 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1192 printk(KERN_ERR "emulation failed but !mmio_needed?"
1193 " rip %lx %02x %02x %02x %02x\n",
1194 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1198 struct x86_emulate_ops emulate_ops = {
1199 .read_std = emulator_read_std,
1200 .write_std = emulator_write_std,
1201 .read_emulated = emulator_read_emulated,
1202 .write_emulated = emulator_write_emulated,
1203 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1206 int emulate_instruction(struct kvm_vcpu *vcpu,
1207 struct kvm_run *run,
1211 struct x86_emulate_ctxt emulate_ctxt;
1215 vcpu->mmio_fault_cr2 = cr2;
1216 kvm_arch_ops->cache_regs(vcpu);
1218 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1220 emulate_ctxt.vcpu = vcpu;
1221 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1222 emulate_ctxt.cr2 = cr2;
1223 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1224 ? X86EMUL_MODE_REAL : cs_l
1225 ? X86EMUL_MODE_PROT64 : cs_db
1226 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1228 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1229 emulate_ctxt.cs_base = 0;
1230 emulate_ctxt.ds_base = 0;
1231 emulate_ctxt.es_base = 0;
1232 emulate_ctxt.ss_base = 0;
1234 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1235 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1236 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1237 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1240 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1241 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1243 vcpu->mmio_is_write = 0;
1244 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1246 if ((r || vcpu->mmio_is_write) && run) {
1247 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1248 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1249 run->mmio.len = vcpu->mmio_size;
1250 run->mmio.is_write = vcpu->mmio_is_write;
1254 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1255 return EMULATE_DONE;
1256 if (!vcpu->mmio_needed) {
1257 report_emulation_failure(&emulate_ctxt);
1258 return EMULATE_FAIL;
1260 return EMULATE_DO_MMIO;
1263 kvm_arch_ops->decache_regs(vcpu);
1264 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1266 if (vcpu->mmio_is_write) {
1267 vcpu->mmio_needed = 0;
1268 return EMULATE_DO_MMIO;
1271 return EMULATE_DONE;
1273 EXPORT_SYMBOL_GPL(emulate_instruction);
1275 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1277 if (vcpu->irq_summary)
1280 vcpu->run->exit_reason = KVM_EXIT_HLT;
1281 ++vcpu->stat.halt_exits;
1284 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1286 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1288 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1290 kvm_arch_ops->cache_regs(vcpu);
1292 #ifdef CONFIG_X86_64
1293 if (is_long_mode(vcpu)) {
1294 nr = vcpu->regs[VCPU_REGS_RAX];
1295 a0 = vcpu->regs[VCPU_REGS_RDI];
1296 a1 = vcpu->regs[VCPU_REGS_RSI];
1297 a2 = vcpu->regs[VCPU_REGS_RDX];
1298 a3 = vcpu->regs[VCPU_REGS_RCX];
1299 a4 = vcpu->regs[VCPU_REGS_R8];
1300 a5 = vcpu->regs[VCPU_REGS_R9];
1304 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1305 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1306 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1307 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1308 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1309 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1310 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1314 run->hypercall.args[0] = a0;
1315 run->hypercall.args[1] = a1;
1316 run->hypercall.args[2] = a2;
1317 run->hypercall.args[3] = a3;
1318 run->hypercall.args[4] = a4;
1319 run->hypercall.args[5] = a5;
1320 run->hypercall.ret = ret;
1321 run->hypercall.longmode = is_long_mode(vcpu);
1322 kvm_arch_ops->decache_regs(vcpu);
1325 vcpu->regs[VCPU_REGS_RAX] = ret;
1326 kvm_arch_ops->decache_regs(vcpu);
1329 EXPORT_SYMBOL_GPL(kvm_hypercall);
1331 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1333 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1336 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1338 struct descriptor_table dt = { limit, base };
1340 kvm_arch_ops->set_gdt(vcpu, &dt);
1343 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1345 struct descriptor_table dt = { limit, base };
1347 kvm_arch_ops->set_idt(vcpu, &dt);
1350 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1351 unsigned long *rflags)
1354 *rflags = kvm_arch_ops->get_rflags(vcpu);
1357 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1359 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
1370 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1375 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1376 unsigned long *rflags)
1380 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1381 *rflags = kvm_arch_ops->get_rflags(vcpu);
1390 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1393 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1398 * Register the para guest with the host:
1400 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1402 struct kvm_vcpu_para_state *para_state;
1403 hpa_t para_state_hpa, hypercall_hpa;
1404 struct page *para_state_page;
1405 unsigned char *hypercall;
1406 gpa_t hypercall_gpa;
1408 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1409 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1412 * Needs to be page aligned:
1414 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1417 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1418 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1419 if (is_error_hpa(para_state_hpa))
1422 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1423 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1424 para_state = kmap_atomic(para_state_page, KM_USER0);
1426 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1427 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1429 para_state->host_version = KVM_PARA_API_VERSION;
1431 * We cannot support guests that try to register themselves
1432 * with a newer API version than the host supports:
1434 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1435 para_state->ret = -KVM_EINVAL;
1436 goto err_kunmap_skip;
1439 hypercall_gpa = para_state->hypercall_gpa;
1440 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1441 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1442 if (is_error_hpa(hypercall_hpa)) {
1443 para_state->ret = -KVM_EINVAL;
1444 goto err_kunmap_skip;
1447 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1448 vcpu->para_state_page = para_state_page;
1449 vcpu->para_state_gpa = para_state_gpa;
1450 vcpu->hypercall_gpa = hypercall_gpa;
1452 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1453 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1454 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1455 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1456 kunmap_atomic(hypercall, KM_USER1);
1458 para_state->ret = 0;
1460 kunmap_atomic(para_state, KM_USER0);
1466 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1471 case 0xc0010010: /* SYSCFG */
1472 case 0xc0010015: /* HWCR */
1473 case MSR_IA32_PLATFORM_ID:
1474 case MSR_IA32_P5_MC_ADDR:
1475 case MSR_IA32_P5_MC_TYPE:
1476 case MSR_IA32_MC0_CTL:
1477 case MSR_IA32_MCG_STATUS:
1478 case MSR_IA32_MCG_CAP:
1479 case MSR_IA32_MC0_MISC:
1480 case MSR_IA32_MC0_MISC+4:
1481 case MSR_IA32_MC0_MISC+8:
1482 case MSR_IA32_MC0_MISC+12:
1483 case MSR_IA32_MC0_MISC+16:
1484 case MSR_IA32_UCODE_REV:
1485 case MSR_IA32_PERF_STATUS:
1486 case MSR_IA32_EBL_CR_POWERON:
1487 /* MTRR registers */
1489 case 0x200 ... 0x2ff:
1492 case 0xcd: /* fsb frequency */
1495 case MSR_IA32_APICBASE:
1496 data = vcpu->apic_base;
1498 case MSR_IA32_MISC_ENABLE:
1499 data = vcpu->ia32_misc_enable_msr;
1501 #ifdef CONFIG_X86_64
1503 data = vcpu->shadow_efer;
1507 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1513 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1516 * Reads an msr value (of 'msr_index') into 'pdata'.
1517 * Returns 0 on success, non-0 otherwise.
1518 * Assumes vcpu_load() was already called.
1520 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1522 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1525 #ifdef CONFIG_X86_64
1527 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1529 if (efer & EFER_RESERVED_BITS) {
1530 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1537 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1538 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1543 kvm_arch_ops->set_efer(vcpu, efer);
1546 efer |= vcpu->shadow_efer & EFER_LMA;
1548 vcpu->shadow_efer = efer;
1553 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1556 #ifdef CONFIG_X86_64
1558 set_efer(vcpu, data);
1561 case MSR_IA32_MC0_STATUS:
1562 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1563 __FUNCTION__, data);
1565 case MSR_IA32_MCG_STATUS:
1566 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1567 __FUNCTION__, data);
1569 case MSR_IA32_UCODE_REV:
1570 case MSR_IA32_UCODE_WRITE:
1571 case 0x200 ... 0x2ff: /* MTRRs */
1573 case MSR_IA32_APICBASE:
1574 vcpu->apic_base = data;
1576 case MSR_IA32_MISC_ENABLE:
1577 vcpu->ia32_misc_enable_msr = data;
1580 * This is the 'probe whether the host is KVM' logic:
1582 case MSR_KVM_API_MAGIC:
1583 return vcpu_register_para(vcpu, data);
1586 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1591 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1594 * Writes msr value into into the appropriate "register".
1595 * Returns 0 on success, non-0 otherwise.
1596 * Assumes vcpu_load() was already called.
1598 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1600 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1603 void kvm_resched(struct kvm_vcpu *vcpu)
1605 if (!need_resched())
1611 EXPORT_SYMBOL_GPL(kvm_resched);
1613 void load_msrs(struct vmx_msr_entry *e, int n)
1617 for (i = 0; i < n; ++i)
1618 wrmsrl(e[i].index, e[i].data);
1620 EXPORT_SYMBOL_GPL(load_msrs);
1622 void save_msrs(struct vmx_msr_entry *e, int n)
1626 for (i = 0; i < n; ++i)
1627 rdmsrl(e[i].index, e[i].data);
1629 EXPORT_SYMBOL_GPL(save_msrs);
1631 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1635 struct kvm_cpuid_entry *e, *best;
1637 kvm_arch_ops->cache_regs(vcpu);
1638 function = vcpu->regs[VCPU_REGS_RAX];
1639 vcpu->regs[VCPU_REGS_RAX] = 0;
1640 vcpu->regs[VCPU_REGS_RBX] = 0;
1641 vcpu->regs[VCPU_REGS_RCX] = 0;
1642 vcpu->regs[VCPU_REGS_RDX] = 0;
1644 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1645 e = &vcpu->cpuid_entries[i];
1646 if (e->function == function) {
1651 * Both basic or both extended?
1653 if (((e->function ^ function) & 0x80000000) == 0)
1654 if (!best || e->function > best->function)
1658 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1659 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1660 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1661 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1663 kvm_arch_ops->decache_regs(vcpu);
1664 kvm_arch_ops->skip_emulated_instruction(vcpu);
1666 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1668 static int pio_copy_data(struct kvm_vcpu *vcpu)
1670 void *p = vcpu->pio_data;
1673 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1675 kvm_arch_ops->vcpu_put(vcpu);
1676 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1679 kvm_arch_ops->vcpu_load(vcpu);
1680 free_pio_guest_pages(vcpu);
1683 q += vcpu->pio.guest_page_offset;
1684 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1686 memcpy(q, p, bytes);
1688 memcpy(p, q, bytes);
1689 q -= vcpu->pio.guest_page_offset;
1691 kvm_arch_ops->vcpu_load(vcpu);
1692 free_pio_guest_pages(vcpu);
1696 static int complete_pio(struct kvm_vcpu *vcpu)
1698 struct kvm_pio_request *io = &vcpu->pio;
1702 kvm_arch_ops->cache_regs(vcpu);
1706 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1710 r = pio_copy_data(vcpu);
1712 kvm_arch_ops->cache_regs(vcpu);
1719 delta *= io->cur_count;
1721 * The size of the register should really depend on
1722 * current address size.
1724 vcpu->regs[VCPU_REGS_RCX] -= delta;
1730 vcpu->regs[VCPU_REGS_RDI] += delta;
1732 vcpu->regs[VCPU_REGS_RSI] += delta;
1735 kvm_arch_ops->decache_regs(vcpu);
1737 io->count -= io->cur_count;
1741 kvm_arch_ops->skip_emulated_instruction(vcpu);
1745 void kernel_pio(struct kvm_io_device *pio_dev, struct kvm_vcpu *vcpu)
1747 /* TODO: String I/O for in kernel device */
1750 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1754 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1759 int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1760 int size, unsigned long count, int string, int down,
1761 gva_t address, int rep, unsigned port)
1763 unsigned now, in_page;
1767 struct kvm_io_device *pio_dev;
1769 vcpu->run->exit_reason = KVM_EXIT_IO;
1770 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1771 vcpu->run->io.size = size;
1772 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1773 vcpu->run->io.count = count;
1774 vcpu->run->io.port = port;
1775 vcpu->pio.count = count;
1776 vcpu->pio.cur_count = count;
1777 vcpu->pio.size = size;
1779 vcpu->pio.port = port;
1780 vcpu->pio.string = string;
1781 vcpu->pio.down = down;
1782 vcpu->pio.guest_page_offset = offset_in_page(address);
1783 vcpu->pio.rep = rep;
1785 pio_dev = vcpu_find_pio_dev(vcpu, port);
1787 kvm_arch_ops->cache_regs(vcpu);
1788 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1789 kvm_arch_ops->decache_regs(vcpu);
1791 kernel_pio(pio_dev, vcpu);
1797 /* TODO: String I/O for in kernel device */
1799 printk(KERN_ERR "kvm_setup_pio: no string io support\n");
1802 kvm_arch_ops->skip_emulated_instruction(vcpu);
1806 now = min(count, PAGE_SIZE / size);
1809 in_page = PAGE_SIZE - offset_in_page(address);
1811 in_page = offset_in_page(address) + size;
1812 now = min(count, (unsigned long)in_page / size);
1815 * String I/O straddles page boundary. Pin two guest pages
1816 * so that we satisfy atomicity constraints. Do just one
1817 * transaction to avoid complexity.
1824 * String I/O in reverse. Yuck. Kill the guest, fix later.
1826 printk(KERN_ERR "kvm: guest string pio down\n");
1830 vcpu->run->io.count = now;
1831 vcpu->pio.cur_count = now;
1833 for (i = 0; i < nr_pages; ++i) {
1834 spin_lock(&vcpu->kvm->lock);
1835 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1838 vcpu->pio.guest_pages[i] = page;
1839 spin_unlock(&vcpu->kvm->lock);
1842 free_pio_guest_pages(vcpu);
1848 return pio_copy_data(vcpu);
1851 EXPORT_SYMBOL_GPL(kvm_setup_pio);
1853 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1860 if (vcpu->sigset_active)
1861 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1863 /* re-sync apic's tpr */
1864 vcpu->cr8 = kvm_run->cr8;
1866 if (vcpu->pio.cur_count) {
1867 r = complete_pio(vcpu);
1872 if (vcpu->mmio_needed) {
1873 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1874 vcpu->mmio_read_completed = 1;
1875 vcpu->mmio_needed = 0;
1876 r = emulate_instruction(vcpu, kvm_run,
1877 vcpu->mmio_fault_cr2, 0);
1878 if (r == EMULATE_DO_MMIO) {
1880 * Read-modify-write. Back to userspace.
1882 kvm_run->exit_reason = KVM_EXIT_MMIO;
1888 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
1889 kvm_arch_ops->cache_regs(vcpu);
1890 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1891 kvm_arch_ops->decache_regs(vcpu);
1894 r = kvm_arch_ops->run(vcpu, kvm_run);
1897 if (vcpu->sigset_active)
1898 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1904 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1905 struct kvm_regs *regs)
1909 kvm_arch_ops->cache_regs(vcpu);
1911 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1912 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1913 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1914 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1915 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1916 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1917 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1918 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1919 #ifdef CONFIG_X86_64
1920 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1921 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1922 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1923 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1924 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1925 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1926 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1927 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1930 regs->rip = vcpu->rip;
1931 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1934 * Don't leak debug flags in case they were set for guest debugging
1936 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1937 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1944 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1945 struct kvm_regs *regs)
1949 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1950 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1951 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1952 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1953 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1954 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1955 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1956 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1957 #ifdef CONFIG_X86_64
1958 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1959 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1960 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1961 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1962 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1963 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1964 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1965 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1968 vcpu->rip = regs->rip;
1969 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1971 kvm_arch_ops->decache_regs(vcpu);
1978 static void get_segment(struct kvm_vcpu *vcpu,
1979 struct kvm_segment *var, int seg)
1981 return kvm_arch_ops->get_segment(vcpu, var, seg);
1984 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1985 struct kvm_sregs *sregs)
1987 struct descriptor_table dt;
1991 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1992 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1993 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1994 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1995 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1996 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1998 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1999 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2001 kvm_arch_ops->get_idt(vcpu, &dt);
2002 sregs->idt.limit = dt.limit;
2003 sregs->idt.base = dt.base;
2004 kvm_arch_ops->get_gdt(vcpu, &dt);
2005 sregs->gdt.limit = dt.limit;
2006 sregs->gdt.base = dt.base;
2008 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2009 sregs->cr0 = vcpu->cr0;
2010 sregs->cr2 = vcpu->cr2;
2011 sregs->cr3 = vcpu->cr3;
2012 sregs->cr4 = vcpu->cr4;
2013 sregs->cr8 = vcpu->cr8;
2014 sregs->efer = vcpu->shadow_efer;
2015 sregs->apic_base = vcpu->apic_base;
2017 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2018 sizeof sregs->interrupt_bitmap);
2025 static void set_segment(struct kvm_vcpu *vcpu,
2026 struct kvm_segment *var, int seg)
2028 return kvm_arch_ops->set_segment(vcpu, var, seg);
2031 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2032 struct kvm_sregs *sregs)
2034 int mmu_reset_needed = 0;
2036 struct descriptor_table dt;
2040 dt.limit = sregs->idt.limit;
2041 dt.base = sregs->idt.base;
2042 kvm_arch_ops->set_idt(vcpu, &dt);
2043 dt.limit = sregs->gdt.limit;
2044 dt.base = sregs->gdt.base;
2045 kvm_arch_ops->set_gdt(vcpu, &dt);
2047 vcpu->cr2 = sregs->cr2;
2048 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2049 vcpu->cr3 = sregs->cr3;
2051 vcpu->cr8 = sregs->cr8;
2053 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2054 #ifdef CONFIG_X86_64
2055 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2057 vcpu->apic_base = sregs->apic_base;
2059 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2061 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2062 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
2064 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2065 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
2066 if (!is_long_mode(vcpu) && is_pae(vcpu))
2067 load_pdptrs(vcpu, vcpu->cr3);
2069 if (mmu_reset_needed)
2070 kvm_mmu_reset_context(vcpu);
2072 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2073 sizeof vcpu->irq_pending);
2074 vcpu->irq_summary = 0;
2075 for (i = 0; i < NR_IRQ_WORDS; ++i)
2076 if (vcpu->irq_pending[i])
2077 __set_bit(i, &vcpu->irq_summary);
2079 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2080 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2081 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2082 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2083 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2084 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2086 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2087 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2095 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2096 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2098 * This list is modified at module load time to reflect the
2099 * capabilities of the host cpu.
2101 static u32 msrs_to_save[] = {
2102 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2104 #ifdef CONFIG_X86_64
2105 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2107 MSR_IA32_TIME_STAMP_COUNTER,
2110 static unsigned num_msrs_to_save;
2112 static u32 emulated_msrs[] = {
2113 MSR_IA32_MISC_ENABLE,
2116 static __init void kvm_init_msr_list(void)
2121 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2122 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2125 msrs_to_save[j] = msrs_to_save[i];
2128 num_msrs_to_save = j;
2132 * Adapt set_msr() to msr_io()'s calling convention
2134 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2136 return kvm_set_msr(vcpu, index, *data);
2140 * Read or write a bunch of msrs. All parameters are kernel addresses.
2142 * @return number of msrs set successfully.
2144 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2145 struct kvm_msr_entry *entries,
2146 int (*do_msr)(struct kvm_vcpu *vcpu,
2147 unsigned index, u64 *data))
2153 for (i = 0; i < msrs->nmsrs; ++i)
2154 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2163 * Read or write a bunch of msrs. Parameters are user addresses.
2165 * @return number of msrs set successfully.
2167 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2168 int (*do_msr)(struct kvm_vcpu *vcpu,
2169 unsigned index, u64 *data),
2172 struct kvm_msrs msrs;
2173 struct kvm_msr_entry *entries;
2178 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2182 if (msrs.nmsrs >= MAX_IO_MSRS)
2186 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2187 entries = vmalloc(size);
2192 if (copy_from_user(entries, user_msrs->entries, size))
2195 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2200 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2212 * Translate a guest virtual address to a guest physical address.
2214 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2215 struct kvm_translation *tr)
2217 unsigned long vaddr = tr->linear_address;
2221 spin_lock(&vcpu->kvm->lock);
2222 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2223 tr->physical_address = gpa;
2224 tr->valid = gpa != UNMAPPED_GVA;
2227 spin_unlock(&vcpu->kvm->lock);
2233 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2234 struct kvm_interrupt *irq)
2236 if (irq->irq < 0 || irq->irq >= 256)
2240 set_bit(irq->irq, vcpu->irq_pending);
2241 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2248 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2249 struct kvm_debug_guest *dbg)
2255 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2262 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2263 unsigned long address,
2266 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2267 unsigned long pgoff;
2270 *type = VM_FAULT_MINOR;
2271 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2273 page = virt_to_page(vcpu->run);
2274 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2275 page = virt_to_page(vcpu->pio_data);
2277 return NOPAGE_SIGBUS;
2282 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2283 .nopage = kvm_vcpu_nopage,
2286 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2288 vma->vm_ops = &kvm_vcpu_vm_ops;
2292 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2294 struct kvm_vcpu *vcpu = filp->private_data;
2296 fput(vcpu->kvm->filp);
2300 static struct file_operations kvm_vcpu_fops = {
2301 .release = kvm_vcpu_release,
2302 .unlocked_ioctl = kvm_vcpu_ioctl,
2303 .compat_ioctl = kvm_vcpu_ioctl,
2304 .mmap = kvm_vcpu_mmap,
2308 * Allocates an inode for the vcpu.
2310 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2313 struct inode *inode;
2316 r = anon_inode_getfd(&fd, &inode, &file,
2317 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2320 atomic_inc(&vcpu->kvm->filp->f_count);
2325 * Creates some virtual cpus. Good luck creating more than one.
2327 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2330 struct kvm_vcpu *vcpu;
2337 vcpu = &kvm->vcpus[n];
2339 mutex_lock(&vcpu->mutex);
2342 mutex_unlock(&vcpu->mutex);
2346 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2350 vcpu->run = page_address(page);
2352 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2356 vcpu->pio_data = page_address(page);
2358 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2360 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2363 r = kvm_arch_ops->vcpu_create(vcpu);
2365 goto out_free_vcpus;
2367 r = kvm_mmu_create(vcpu);
2369 goto out_free_vcpus;
2371 kvm_arch_ops->vcpu_load(vcpu);
2372 r = kvm_mmu_setup(vcpu);
2374 r = kvm_arch_ops->vcpu_setup(vcpu);
2378 goto out_free_vcpus;
2380 r = create_vcpu_fd(vcpu);
2382 goto out_free_vcpus;
2384 spin_lock(&kvm_lock);
2385 if (n >= kvm->nvcpus)
2386 kvm->nvcpus = n + 1;
2387 spin_unlock(&kvm_lock);
2392 kvm_free_vcpu(vcpu);
2394 free_page((unsigned long)vcpu->run);
2397 mutex_unlock(&vcpu->mutex);
2402 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2406 struct kvm_cpuid_entry *e, *entry;
2408 rdmsrl(MSR_EFER, efer);
2410 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2411 e = &vcpu->cpuid_entries[i];
2412 if (e->function == 0x80000001) {
2417 if (entry && (entry->edx & EFER_NX) && !(efer & EFER_NX)) {
2418 entry->edx &= ~(1 << 20);
2419 printk(KERN_INFO ": guest NX capability removed\n");
2423 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2424 struct kvm_cpuid *cpuid,
2425 struct kvm_cpuid_entry __user *entries)
2430 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2433 if (copy_from_user(&vcpu->cpuid_entries, entries,
2434 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2436 vcpu->cpuid_nent = cpuid->nent;
2437 cpuid_fix_nx_cap(vcpu);
2444 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2447 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2448 vcpu->sigset_active = 1;
2449 vcpu->sigset = *sigset;
2451 vcpu->sigset_active = 0;
2456 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2457 * we have asm/x86/processor.h
2468 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2469 #ifdef CONFIG_X86_64
2470 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2472 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2476 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2478 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2482 memcpy(fpu->fpr, fxsave->st_space, 128);
2483 fpu->fcw = fxsave->cwd;
2484 fpu->fsw = fxsave->swd;
2485 fpu->ftwx = fxsave->twd;
2486 fpu->last_opcode = fxsave->fop;
2487 fpu->last_ip = fxsave->rip;
2488 fpu->last_dp = fxsave->rdp;
2489 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2496 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2498 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2502 memcpy(fxsave->st_space, fpu->fpr, 128);
2503 fxsave->cwd = fpu->fcw;
2504 fxsave->swd = fpu->fsw;
2505 fxsave->twd = fpu->ftwx;
2506 fxsave->fop = fpu->last_opcode;
2507 fxsave->rip = fpu->last_ip;
2508 fxsave->rdp = fpu->last_dp;
2509 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2516 static long kvm_vcpu_ioctl(struct file *filp,
2517 unsigned int ioctl, unsigned long arg)
2519 struct kvm_vcpu *vcpu = filp->private_data;
2520 void __user *argp = (void __user *)arg;
2528 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2530 case KVM_GET_REGS: {
2531 struct kvm_regs kvm_regs;
2533 memset(&kvm_regs, 0, sizeof kvm_regs);
2534 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2538 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2543 case KVM_SET_REGS: {
2544 struct kvm_regs kvm_regs;
2547 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2549 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2555 case KVM_GET_SREGS: {
2556 struct kvm_sregs kvm_sregs;
2558 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2559 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2563 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2568 case KVM_SET_SREGS: {
2569 struct kvm_sregs kvm_sregs;
2572 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2574 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2580 case KVM_TRANSLATE: {
2581 struct kvm_translation tr;
2584 if (copy_from_user(&tr, argp, sizeof tr))
2586 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2590 if (copy_to_user(argp, &tr, sizeof tr))
2595 case KVM_INTERRUPT: {
2596 struct kvm_interrupt irq;
2599 if (copy_from_user(&irq, argp, sizeof irq))
2601 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2607 case KVM_DEBUG_GUEST: {
2608 struct kvm_debug_guest dbg;
2611 if (copy_from_user(&dbg, argp, sizeof dbg))
2613 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2620 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2623 r = msr_io(vcpu, argp, do_set_msr, 0);
2625 case KVM_SET_CPUID: {
2626 struct kvm_cpuid __user *cpuid_arg = argp;
2627 struct kvm_cpuid cpuid;
2630 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2632 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2637 case KVM_SET_SIGNAL_MASK: {
2638 struct kvm_signal_mask __user *sigmask_arg = argp;
2639 struct kvm_signal_mask kvm_sigmask;
2640 sigset_t sigset, *p;
2645 if (copy_from_user(&kvm_sigmask, argp,
2646 sizeof kvm_sigmask))
2649 if (kvm_sigmask.len != sizeof sigset)
2652 if (copy_from_user(&sigset, sigmask_arg->sigset,
2657 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2663 memset(&fpu, 0, sizeof fpu);
2664 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2668 if (copy_to_user(argp, &fpu, sizeof fpu))
2677 if (copy_from_user(&fpu, argp, sizeof fpu))
2679 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2692 static long kvm_vm_ioctl(struct file *filp,
2693 unsigned int ioctl, unsigned long arg)
2695 struct kvm *kvm = filp->private_data;
2696 void __user *argp = (void __user *)arg;
2700 case KVM_CREATE_VCPU:
2701 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2705 case KVM_SET_MEMORY_REGION: {
2706 struct kvm_memory_region kvm_mem;
2709 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2711 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
2716 case KVM_GET_DIRTY_LOG: {
2717 struct kvm_dirty_log log;
2720 if (copy_from_user(&log, argp, sizeof log))
2722 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2727 case KVM_SET_MEMORY_ALIAS: {
2728 struct kvm_memory_alias alias;
2731 if (copy_from_user(&alias, argp, sizeof alias))
2733 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2745 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2746 unsigned long address,
2749 struct kvm *kvm = vma->vm_file->private_data;
2750 unsigned long pgoff;
2753 *type = VM_FAULT_MINOR;
2754 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2755 page = gfn_to_page(kvm, pgoff);
2757 return NOPAGE_SIGBUS;
2762 static struct vm_operations_struct kvm_vm_vm_ops = {
2763 .nopage = kvm_vm_nopage,
2766 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2768 vma->vm_ops = &kvm_vm_vm_ops;
2772 static struct file_operations kvm_vm_fops = {
2773 .release = kvm_vm_release,
2774 .unlocked_ioctl = kvm_vm_ioctl,
2775 .compat_ioctl = kvm_vm_ioctl,
2776 .mmap = kvm_vm_mmap,
2779 static int kvm_dev_ioctl_create_vm(void)
2782 struct inode *inode;
2786 kvm = kvm_create_vm();
2788 return PTR_ERR(kvm);
2789 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2791 kvm_destroy_vm(kvm);
2800 static long kvm_dev_ioctl(struct file *filp,
2801 unsigned int ioctl, unsigned long arg)
2803 void __user *argp = (void __user *)arg;
2807 case KVM_GET_API_VERSION:
2811 r = KVM_API_VERSION;
2817 r = kvm_dev_ioctl_create_vm();
2819 case KVM_GET_MSR_INDEX_LIST: {
2820 struct kvm_msr_list __user *user_msr_list = argp;
2821 struct kvm_msr_list msr_list;
2825 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2828 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2829 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2832 if (n < num_msrs_to_save)
2835 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2836 num_msrs_to_save * sizeof(u32)))
2838 if (copy_to_user(user_msr_list->indices
2839 + num_msrs_to_save * sizeof(u32),
2841 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2846 case KVM_CHECK_EXTENSION:
2848 * No extensions defined at present.
2852 case KVM_GET_VCPU_MMAP_SIZE:
2865 static struct file_operations kvm_chardev_ops = {
2866 .open = kvm_dev_open,
2867 .release = kvm_dev_release,
2868 .unlocked_ioctl = kvm_dev_ioctl,
2869 .compat_ioctl = kvm_dev_ioctl,
2872 static struct miscdevice kvm_dev = {
2878 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2881 if (val == SYS_RESTART) {
2883 * Some (well, at least mine) BIOSes hang on reboot if
2886 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2887 on_each_cpu(hardware_disable, NULL, 0, 1);
2892 static struct notifier_block kvm_reboot_notifier = {
2893 .notifier_call = kvm_reboot,
2898 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2901 static void decache_vcpus_on_cpu(int cpu)
2904 struct kvm_vcpu *vcpu;
2907 spin_lock(&kvm_lock);
2908 list_for_each_entry(vm, &vm_list, vm_list)
2909 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2910 vcpu = &vm->vcpus[i];
2912 * If the vcpu is locked, then it is running on some
2913 * other cpu and therefore it is not cached on the
2916 * If it's not locked, check the last cpu it executed
2919 if (mutex_trylock(&vcpu->mutex)) {
2920 if (vcpu->cpu == cpu) {
2921 kvm_arch_ops->vcpu_decache(vcpu);
2924 mutex_unlock(&vcpu->mutex);
2927 spin_unlock(&kvm_lock);
2930 static void hardware_enable(void *junk)
2932 int cpu = raw_smp_processor_id();
2934 if (cpu_isset(cpu, cpus_hardware_enabled))
2936 cpu_set(cpu, cpus_hardware_enabled);
2937 kvm_arch_ops->hardware_enable(NULL);
2940 static void hardware_disable(void *junk)
2942 int cpu = raw_smp_processor_id();
2944 if (!cpu_isset(cpu, cpus_hardware_enabled))
2946 cpu_clear(cpu, cpus_hardware_enabled);
2947 decache_vcpus_on_cpu(cpu);
2948 kvm_arch_ops->hardware_disable(NULL);
2951 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2958 case CPU_DYING_FROZEN:
2959 case CPU_UP_CANCELED:
2960 case CPU_UP_CANCELED_FROZEN:
2961 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2963 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
2966 case CPU_ONLINE_FROZEN:
2967 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2969 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
2975 void kvm_io_bus_init(struct kvm_io_bus *bus)
2977 memset(bus, 0, sizeof(*bus));
2980 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2984 for (i = 0; i < bus->dev_count; i++) {
2985 struct kvm_io_device *pos = bus->devs[i];
2987 kvm_iodevice_destructor(pos);
2991 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
2995 for (i = 0; i < bus->dev_count; i++) {
2996 struct kvm_io_device *pos = bus->devs[i];
2998 if (pos->in_range(pos, addr))
3005 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3007 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3009 bus->devs[bus->dev_count++] = dev;
3012 static struct notifier_block kvm_cpu_notifier = {
3013 .notifier_call = kvm_cpu_hotplug,
3014 .priority = 20, /* must be > scheduler priority */
3017 static u64 stat_get(void *_offset)
3019 unsigned offset = (long)_offset;
3022 struct kvm_vcpu *vcpu;
3025 spin_lock(&kvm_lock);
3026 list_for_each_entry(kvm, &vm_list, vm_list)
3027 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3028 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;
3086 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3091 printk(KERN_ERR "kvm: already loaded the other module\n");
3095 if (!ops->cpu_has_kvm_support()) {
3096 printk(KERN_ERR "kvm: no hardware support\n");
3099 if (ops->disabled_by_bios()) {
3100 printk(KERN_ERR "kvm: disabled by bios\n");
3106 r = kvm_arch_ops->hardware_setup();
3110 on_each_cpu(hardware_enable, NULL, 0, 1);
3111 r = register_cpu_notifier(&kvm_cpu_notifier);
3114 register_reboot_notifier(&kvm_reboot_notifier);
3116 r = sysdev_class_register(&kvm_sysdev_class);
3120 r = sysdev_register(&kvm_sysdev);
3124 kvm_chardev_ops.owner = module;
3126 r = misc_register(&kvm_dev);
3128 printk (KERN_ERR "kvm: misc device register failed\n");
3135 sysdev_unregister(&kvm_sysdev);
3137 sysdev_class_unregister(&kvm_sysdev_class);
3139 unregister_reboot_notifier(&kvm_reboot_notifier);
3140 unregister_cpu_notifier(&kvm_cpu_notifier);
3142 on_each_cpu(hardware_disable, NULL, 0, 1);
3143 kvm_arch_ops->hardware_unsetup();
3145 kvm_arch_ops = NULL;
3149 void kvm_exit_arch(void)
3151 misc_deregister(&kvm_dev);
3152 sysdev_unregister(&kvm_sysdev);
3153 sysdev_class_unregister(&kvm_sysdev_class);
3154 unregister_reboot_notifier(&kvm_reboot_notifier);
3155 unregister_cpu_notifier(&kvm_cpu_notifier);
3156 on_each_cpu(hardware_disable, NULL, 0, 1);
3157 kvm_arch_ops->hardware_unsetup();
3158 kvm_arch_ops = NULL;
3161 static __init int kvm_init(void)
3163 static struct page *bad_page;
3166 r = kvm_mmu_module_init();
3172 kvm_init_msr_list();
3174 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3179 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3180 memset(__va(bad_page_address), 0, PAGE_SIZE);
3186 kvm_mmu_module_exit();
3191 static __exit void kvm_exit(void)
3194 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3195 kvm_mmu_module_exit();
3198 module_init(kvm_init)
3199 module_exit(kvm_exit)
3201 EXPORT_SYMBOL_GPL(kvm_init_arch);
3202 EXPORT_SYMBOL_GPL(kvm_exit_arch);