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;
56 struct kmem_cache *kvm_vcpu_cache;
57 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
59 static __read_mostly struct preempt_ops kvm_preempt_ops;
61 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
63 static struct kvm_stats_debugfs_item {
66 struct dentry *dentry;
67 } debugfs_entries[] = {
68 { "pf_fixed", STAT_OFFSET(pf_fixed) },
69 { "pf_guest", STAT_OFFSET(pf_guest) },
70 { "tlb_flush", STAT_OFFSET(tlb_flush) },
71 { "invlpg", STAT_OFFSET(invlpg) },
72 { "exits", STAT_OFFSET(exits) },
73 { "io_exits", STAT_OFFSET(io_exits) },
74 { "mmio_exits", STAT_OFFSET(mmio_exits) },
75 { "signal_exits", STAT_OFFSET(signal_exits) },
76 { "irq_window", STAT_OFFSET(irq_window_exits) },
77 { "halt_exits", STAT_OFFSET(halt_exits) },
78 { "request_irq", STAT_OFFSET(request_irq_exits) },
79 { "irq_exits", STAT_OFFSET(irq_exits) },
80 { "light_exits", STAT_OFFSET(light_exits) },
81 { "efer_reload", STAT_OFFSET(efer_reload) },
85 static struct dentry *debugfs_dir;
87 #define MAX_IO_MSRS 256
89 #define CR0_RESERVED_BITS \
90 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
91 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
92 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
93 #define CR4_RESERVED_BITS \
94 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
95 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
96 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
97 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
99 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
100 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
103 // LDT or TSS descriptor in the GDT. 16 bytes.
104 struct segment_descriptor_64 {
105 struct segment_descriptor s;
112 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
115 unsigned long segment_base(u16 selector)
117 struct descriptor_table gdt;
118 struct segment_descriptor *d;
119 unsigned long table_base;
120 typedef unsigned long ul;
126 asm ("sgdt %0" : "=m"(gdt));
127 table_base = gdt.base;
129 if (selector & 4) { /* from ldt */
132 asm ("sldt %0" : "=g"(ldt_selector));
133 table_base = segment_base(ldt_selector);
135 d = (struct segment_descriptor *)(table_base + (selector & ~7));
136 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
139 && (d->type == 2 || d->type == 9 || d->type == 11))
140 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
144 EXPORT_SYMBOL_GPL(segment_base);
146 static inline int valid_vcpu(int n)
148 return likely(n >= 0 && n < KVM_MAX_VCPUS);
151 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
153 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
156 vcpu->guest_fpu_loaded = 1;
157 fx_save(&vcpu->host_fx_image);
158 fx_restore(&vcpu->guest_fx_image);
160 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
162 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
164 if (!vcpu->guest_fpu_loaded)
167 vcpu->guest_fpu_loaded = 0;
168 fx_save(&vcpu->guest_fx_image);
169 fx_restore(&vcpu->host_fx_image);
171 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
174 * Switches to specified vcpu, until a matching vcpu_put()
176 static void vcpu_load(struct kvm_vcpu *vcpu)
180 mutex_lock(&vcpu->mutex);
182 preempt_notifier_register(&vcpu->preempt_notifier);
183 kvm_arch_ops->vcpu_load(vcpu, cpu);
187 static void vcpu_put(struct kvm_vcpu *vcpu)
190 kvm_arch_ops->vcpu_put(vcpu);
191 preempt_notifier_unregister(&vcpu->preempt_notifier);
193 mutex_unlock(&vcpu->mutex);
196 static void ack_flush(void *_completed)
198 atomic_t *completed = _completed;
200 atomic_inc(completed);
203 void kvm_flush_remote_tlbs(struct kvm *kvm)
207 struct kvm_vcpu *vcpu;
210 atomic_set(&completed, 0);
213 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
214 vcpu = kvm->vcpus[i];
217 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
220 if (cpu != -1 && cpu != raw_smp_processor_id())
221 if (!cpu_isset(cpu, cpus)) {
228 * We really want smp_call_function_mask() here. But that's not
229 * available, so ipi all cpus in parallel and wait for them
232 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
233 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
234 while (atomic_read(&completed) != needed) {
240 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
245 mutex_init(&vcpu->mutex);
247 vcpu->mmu.root_hpa = INVALID_PAGE;
251 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
256 vcpu->run = page_address(page);
258 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
263 vcpu->pio_data = page_address(page);
265 r = kvm_mmu_create(vcpu);
267 goto fail_free_pio_data;
272 free_page((unsigned long)vcpu->pio_data);
274 free_page((unsigned long)vcpu->run);
278 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
280 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
282 kvm_mmu_destroy(vcpu);
283 free_page((unsigned long)vcpu->pio_data);
284 free_page((unsigned long)vcpu->run);
286 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
288 static struct kvm *kvm_create_vm(void)
290 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
293 return ERR_PTR(-ENOMEM);
295 kvm_io_bus_init(&kvm->pio_bus);
296 mutex_init(&kvm->lock);
297 INIT_LIST_HEAD(&kvm->active_mmu_pages);
298 kvm_io_bus_init(&kvm->mmio_bus);
299 spin_lock(&kvm_lock);
300 list_add(&kvm->vm_list, &vm_list);
301 spin_unlock(&kvm_lock);
306 * Free any memory in @free but not in @dont.
308 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
309 struct kvm_memory_slot *dont)
313 if (!dont || free->phys_mem != dont->phys_mem)
314 if (free->phys_mem) {
315 for (i = 0; i < free->npages; ++i)
316 if (free->phys_mem[i])
317 __free_page(free->phys_mem[i]);
318 vfree(free->phys_mem);
321 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
322 vfree(free->dirty_bitmap);
324 free->phys_mem = NULL;
326 free->dirty_bitmap = NULL;
329 static void kvm_free_physmem(struct kvm *kvm)
333 for (i = 0; i < kvm->nmemslots; ++i)
334 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
337 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
341 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
342 if (vcpu->pio.guest_pages[i]) {
343 __free_page(vcpu->pio.guest_pages[i]);
344 vcpu->pio.guest_pages[i] = NULL;
348 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
351 kvm_mmu_unload(vcpu);
355 static void kvm_free_vcpus(struct kvm *kvm)
360 * Unpin any mmu pages first.
362 for (i = 0; i < KVM_MAX_VCPUS; ++i)
364 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
365 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
367 kvm_arch_ops->vcpu_free(kvm->vcpus[i]);
368 kvm->vcpus[i] = NULL;
374 static void kvm_destroy_vm(struct kvm *kvm)
376 spin_lock(&kvm_lock);
377 list_del(&kvm->vm_list);
378 spin_unlock(&kvm_lock);
379 kvm_io_bus_destroy(&kvm->pio_bus);
380 kvm_io_bus_destroy(&kvm->mmio_bus);
382 kvm_free_physmem(kvm);
386 static int kvm_vm_release(struct inode *inode, struct file *filp)
388 struct kvm *kvm = filp->private_data;
394 static void inject_gp(struct kvm_vcpu *vcpu)
396 kvm_arch_ops->inject_gp(vcpu, 0);
400 * Load the pae pdptrs. Return true is they are all valid.
402 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
404 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
405 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
410 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
412 mutex_lock(&vcpu->kvm->lock);
413 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
419 pdpt = kmap_atomic(page, KM_USER0);
420 memcpy(pdpte, pdpt+offset, sizeof(pdpte));
421 kunmap_atomic(pdpt, KM_USER0);
423 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
424 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
431 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
433 mutex_unlock(&vcpu->kvm->lock);
438 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
440 if (cr0 & CR0_RESERVED_BITS) {
441 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
447 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
448 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
453 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
454 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
455 "and a clear PE flag\n");
460 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
462 if ((vcpu->shadow_efer & EFER_LME)) {
466 printk(KERN_DEBUG "set_cr0: #GP, start paging "
467 "in long mode while PAE is disabled\n");
471 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
473 printk(KERN_DEBUG "set_cr0: #GP, start paging "
474 "in long mode while CS.L == 1\n");
481 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
482 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
490 kvm_arch_ops->set_cr0(vcpu, cr0);
493 mutex_lock(&vcpu->kvm->lock);
494 kvm_mmu_reset_context(vcpu);
495 mutex_unlock(&vcpu->kvm->lock);
498 EXPORT_SYMBOL_GPL(set_cr0);
500 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
502 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
504 EXPORT_SYMBOL_GPL(lmsw);
506 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
508 if (cr4 & CR4_RESERVED_BITS) {
509 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
514 if (is_long_mode(vcpu)) {
515 if (!(cr4 & X86_CR4_PAE)) {
516 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
521 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
522 && !load_pdptrs(vcpu, vcpu->cr3)) {
523 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
528 if (cr4 & X86_CR4_VMXE) {
529 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
533 kvm_arch_ops->set_cr4(vcpu, cr4);
534 mutex_lock(&vcpu->kvm->lock);
535 kvm_mmu_reset_context(vcpu);
536 mutex_unlock(&vcpu->kvm->lock);
538 EXPORT_SYMBOL_GPL(set_cr4);
540 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
542 if (is_long_mode(vcpu)) {
543 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
544 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
550 if (cr3 & CR3_PAE_RESERVED_BITS) {
552 "set_cr3: #GP, reserved bits\n");
556 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
557 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
563 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
565 "set_cr3: #GP, reserved bits\n");
572 mutex_lock(&vcpu->kvm->lock);
574 * Does the new cr3 value map to physical memory? (Note, we
575 * catch an invalid cr3 even in real-mode, because it would
576 * cause trouble later on when we turn on paging anyway.)
578 * A real CPU would silently accept an invalid cr3 and would
579 * attempt to use it - with largely undefined (and often hard
580 * to debug) behavior on the guest side.
582 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
586 vcpu->mmu.new_cr3(vcpu);
588 mutex_unlock(&vcpu->kvm->lock);
590 EXPORT_SYMBOL_GPL(set_cr3);
592 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
594 if (cr8 & CR8_RESERVED_BITS) {
595 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
601 EXPORT_SYMBOL_GPL(set_cr8);
603 void fx_init(struct kvm_vcpu *vcpu)
605 unsigned after_mxcsr_mask;
607 /* Initialize guest FPU by resetting ours and saving into guest's */
609 fx_save(&vcpu->host_fx_image);
611 fx_save(&vcpu->guest_fx_image);
612 fx_restore(&vcpu->host_fx_image);
615 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
616 vcpu->guest_fx_image.mxcsr = 0x1f80;
617 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
618 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
620 EXPORT_SYMBOL_GPL(fx_init);
623 * Allocate some memory and give it an address in the guest physical address
626 * Discontiguous memory is allowed, mostly for framebuffers.
628 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
629 struct kvm_memory_region *mem)
633 unsigned long npages;
635 struct kvm_memory_slot *memslot;
636 struct kvm_memory_slot old, new;
637 int memory_config_version;
640 /* General sanity checks */
641 if (mem->memory_size & (PAGE_SIZE - 1))
643 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
645 if (mem->slot >= KVM_MEMORY_SLOTS)
647 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
650 memslot = &kvm->memslots[mem->slot];
651 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
652 npages = mem->memory_size >> PAGE_SHIFT;
655 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
658 mutex_lock(&kvm->lock);
660 memory_config_version = kvm->memory_config_version;
661 new = old = *memslot;
663 new.base_gfn = base_gfn;
665 new.flags = mem->flags;
667 /* Disallow changing a memory slot's size. */
669 if (npages && old.npages && npages != old.npages)
672 /* Check for overlaps */
674 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
675 struct kvm_memory_slot *s = &kvm->memslots[i];
679 if (!((base_gfn + npages <= s->base_gfn) ||
680 (base_gfn >= s->base_gfn + s->npages)))
684 * Do memory allocations outside lock. memory_config_version will
687 mutex_unlock(&kvm->lock);
689 /* Deallocate if slot is being removed */
693 /* Free page dirty bitmap if unneeded */
694 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
695 new.dirty_bitmap = NULL;
699 /* Allocate if a slot is being created */
700 if (npages && !new.phys_mem) {
701 new.phys_mem = vmalloc(npages * sizeof(struct page *));
706 memset(new.phys_mem, 0, npages * sizeof(struct page *));
707 for (i = 0; i < npages; ++i) {
708 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
710 if (!new.phys_mem[i])
712 set_page_private(new.phys_mem[i],0);
716 /* Allocate page dirty bitmap if needed */
717 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
718 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
720 new.dirty_bitmap = vmalloc(dirty_bytes);
721 if (!new.dirty_bitmap)
723 memset(new.dirty_bitmap, 0, dirty_bytes);
726 mutex_lock(&kvm->lock);
728 if (memory_config_version != kvm->memory_config_version) {
729 mutex_unlock(&kvm->lock);
730 kvm_free_physmem_slot(&new, &old);
738 if (mem->slot >= kvm->nmemslots)
739 kvm->nmemslots = mem->slot + 1;
742 ++kvm->memory_config_version;
744 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
745 kvm_flush_remote_tlbs(kvm);
747 mutex_unlock(&kvm->lock);
749 kvm_free_physmem_slot(&old, &new);
753 mutex_unlock(&kvm->lock);
755 kvm_free_physmem_slot(&new, &old);
761 * Get (and clear) the dirty memory log for a memory slot.
763 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
764 struct kvm_dirty_log *log)
766 struct kvm_memory_slot *memslot;
769 unsigned long any = 0;
771 mutex_lock(&kvm->lock);
774 * Prevent changes to guest memory configuration even while the lock
778 mutex_unlock(&kvm->lock);
780 if (log->slot >= KVM_MEMORY_SLOTS)
783 memslot = &kvm->memslots[log->slot];
785 if (!memslot->dirty_bitmap)
788 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
790 for (i = 0; !any && i < n/sizeof(long); ++i)
791 any = memslot->dirty_bitmap[i];
794 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
797 /* If nothing is dirty, don't bother messing with page tables. */
799 mutex_lock(&kvm->lock);
800 kvm_mmu_slot_remove_write_access(kvm, log->slot);
801 kvm_flush_remote_tlbs(kvm);
802 memset(memslot->dirty_bitmap, 0, n);
803 mutex_unlock(&kvm->lock);
809 mutex_lock(&kvm->lock);
811 mutex_unlock(&kvm->lock);
816 * Set a new alias region. Aliases map a portion of physical memory into
817 * another portion. This is useful for memory windows, for example the PC
820 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
821 struct kvm_memory_alias *alias)
824 struct kvm_mem_alias *p;
827 /* General sanity checks */
828 if (alias->memory_size & (PAGE_SIZE - 1))
830 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
832 if (alias->slot >= KVM_ALIAS_SLOTS)
834 if (alias->guest_phys_addr + alias->memory_size
835 < alias->guest_phys_addr)
837 if (alias->target_phys_addr + alias->memory_size
838 < alias->target_phys_addr)
841 mutex_lock(&kvm->lock);
843 p = &kvm->aliases[alias->slot];
844 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
845 p->npages = alias->memory_size >> PAGE_SHIFT;
846 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
848 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
849 if (kvm->aliases[n - 1].npages)
853 kvm_mmu_zap_all(kvm);
855 mutex_unlock(&kvm->lock);
863 static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
866 struct kvm_mem_alias *alias;
868 for (i = 0; i < kvm->naliases; ++i) {
869 alias = &kvm->aliases[i];
870 if (gfn >= alias->base_gfn
871 && gfn < alias->base_gfn + alias->npages)
872 return alias->target_gfn + gfn - alias->base_gfn;
877 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
881 for (i = 0; i < kvm->nmemslots; ++i) {
882 struct kvm_memory_slot *memslot = &kvm->memslots[i];
884 if (gfn >= memslot->base_gfn
885 && gfn < memslot->base_gfn + memslot->npages)
891 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
893 gfn = unalias_gfn(kvm, gfn);
894 return __gfn_to_memslot(kvm, gfn);
897 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
899 struct kvm_memory_slot *slot;
901 gfn = unalias_gfn(kvm, gfn);
902 slot = __gfn_to_memslot(kvm, gfn);
905 return slot->phys_mem[gfn - slot->base_gfn];
907 EXPORT_SYMBOL_GPL(gfn_to_page);
909 /* WARNING: Does not work on aliased pages. */
910 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
912 struct kvm_memory_slot *memslot;
914 memslot = __gfn_to_memslot(kvm, gfn);
915 if (memslot && memslot->dirty_bitmap) {
916 unsigned long rel_gfn = gfn - memslot->base_gfn;
919 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
920 set_bit(rel_gfn, memslot->dirty_bitmap);
924 int emulator_read_std(unsigned long addr,
927 struct kvm_vcpu *vcpu)
932 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
933 unsigned offset = addr & (PAGE_SIZE-1);
934 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
939 if (gpa == UNMAPPED_GVA)
940 return X86EMUL_PROPAGATE_FAULT;
941 pfn = gpa >> PAGE_SHIFT;
942 page = gfn_to_page(vcpu->kvm, pfn);
944 return X86EMUL_UNHANDLEABLE;
945 page_virt = kmap_atomic(page, KM_USER0);
947 memcpy(data, page_virt + offset, tocopy);
949 kunmap_atomic(page_virt, KM_USER0);
956 return X86EMUL_CONTINUE;
958 EXPORT_SYMBOL_GPL(emulator_read_std);
960 static int emulator_write_std(unsigned long addr,
963 struct kvm_vcpu *vcpu)
965 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
966 return X86EMUL_UNHANDLEABLE;
969 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
973 * Note that its important to have this wrapper function because
974 * in the very near future we will be checking for MMIOs against
975 * the LAPIC as well as the general MMIO bus
977 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
980 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
983 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
986 static int emulator_read_emulated(unsigned long addr,
989 struct kvm_vcpu *vcpu)
991 struct kvm_io_device *mmio_dev;
994 if (vcpu->mmio_read_completed) {
995 memcpy(val, vcpu->mmio_data, bytes);
996 vcpu->mmio_read_completed = 0;
997 return X86EMUL_CONTINUE;
998 } else if (emulator_read_std(addr, val, bytes, vcpu)
1000 return X86EMUL_CONTINUE;
1002 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1003 if (gpa == UNMAPPED_GVA)
1004 return X86EMUL_PROPAGATE_FAULT;
1007 * Is this MMIO handled locally?
1009 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1011 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1012 return X86EMUL_CONTINUE;
1015 vcpu->mmio_needed = 1;
1016 vcpu->mmio_phys_addr = gpa;
1017 vcpu->mmio_size = bytes;
1018 vcpu->mmio_is_write = 0;
1020 return X86EMUL_UNHANDLEABLE;
1023 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1024 const void *val, int bytes)
1029 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1031 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1034 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
1035 virt = kmap_atomic(page, KM_USER0);
1036 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1037 memcpy(virt + offset_in_page(gpa), val, bytes);
1038 kunmap_atomic(virt, KM_USER0);
1042 static int emulator_write_emulated_onepage(unsigned long addr,
1045 struct kvm_vcpu *vcpu)
1047 struct kvm_io_device *mmio_dev;
1048 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1050 if (gpa == UNMAPPED_GVA) {
1051 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
1052 return X86EMUL_PROPAGATE_FAULT;
1055 if (emulator_write_phys(vcpu, gpa, val, bytes))
1056 return X86EMUL_CONTINUE;
1059 * Is this MMIO handled locally?
1061 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1063 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1064 return X86EMUL_CONTINUE;
1067 vcpu->mmio_needed = 1;
1068 vcpu->mmio_phys_addr = gpa;
1069 vcpu->mmio_size = bytes;
1070 vcpu->mmio_is_write = 1;
1071 memcpy(vcpu->mmio_data, val, bytes);
1073 return X86EMUL_CONTINUE;
1076 int emulator_write_emulated(unsigned long addr,
1079 struct kvm_vcpu *vcpu)
1081 /* Crossing a page boundary? */
1082 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1085 now = -addr & ~PAGE_MASK;
1086 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1087 if (rc != X86EMUL_CONTINUE)
1093 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1095 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1097 static int emulator_cmpxchg_emulated(unsigned long addr,
1101 struct kvm_vcpu *vcpu)
1103 static int reported;
1107 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1109 return emulator_write_emulated(addr, new, bytes, vcpu);
1112 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1114 return kvm_arch_ops->get_segment_base(vcpu, seg);
1117 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1119 return X86EMUL_CONTINUE;
1122 int emulate_clts(struct kvm_vcpu *vcpu)
1126 cr0 = vcpu->cr0 & ~X86_CR0_TS;
1127 kvm_arch_ops->set_cr0(vcpu, cr0);
1128 return X86EMUL_CONTINUE;
1131 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1133 struct kvm_vcpu *vcpu = ctxt->vcpu;
1137 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1138 return X86EMUL_CONTINUE;
1140 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1141 return X86EMUL_UNHANDLEABLE;
1145 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1147 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1150 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1152 /* FIXME: better handling */
1153 return X86EMUL_UNHANDLEABLE;
1155 return X86EMUL_CONTINUE;
1158 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1160 static int reported;
1162 unsigned long rip = ctxt->vcpu->rip;
1163 unsigned long rip_linear;
1165 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1170 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt->vcpu);
1172 printk(KERN_ERR "emulation failed but !mmio_needed?"
1173 " rip %lx %02x %02x %02x %02x\n",
1174 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1178 struct x86_emulate_ops emulate_ops = {
1179 .read_std = emulator_read_std,
1180 .write_std = emulator_write_std,
1181 .read_emulated = emulator_read_emulated,
1182 .write_emulated = emulator_write_emulated,
1183 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1186 int emulate_instruction(struct kvm_vcpu *vcpu,
1187 struct kvm_run *run,
1191 struct x86_emulate_ctxt emulate_ctxt;
1195 vcpu->mmio_fault_cr2 = cr2;
1196 kvm_arch_ops->cache_regs(vcpu);
1198 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1200 emulate_ctxt.vcpu = vcpu;
1201 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1202 emulate_ctxt.cr2 = cr2;
1203 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1204 ? X86EMUL_MODE_REAL : cs_l
1205 ? X86EMUL_MODE_PROT64 : cs_db
1206 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1208 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1209 emulate_ctxt.cs_base = 0;
1210 emulate_ctxt.ds_base = 0;
1211 emulate_ctxt.es_base = 0;
1212 emulate_ctxt.ss_base = 0;
1214 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1215 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1216 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1217 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1220 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1221 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1223 vcpu->mmio_is_write = 0;
1224 vcpu->pio.string = 0;
1225 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1226 if (vcpu->pio.string)
1227 return EMULATE_DO_MMIO;
1229 if ((r || vcpu->mmio_is_write) && run) {
1230 run->exit_reason = KVM_EXIT_MMIO;
1231 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1232 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1233 run->mmio.len = vcpu->mmio_size;
1234 run->mmio.is_write = vcpu->mmio_is_write;
1238 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1239 return EMULATE_DONE;
1240 if (!vcpu->mmio_needed) {
1241 report_emulation_failure(&emulate_ctxt);
1242 return EMULATE_FAIL;
1244 return EMULATE_DO_MMIO;
1247 kvm_arch_ops->decache_regs(vcpu);
1248 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1250 if (vcpu->mmio_is_write) {
1251 vcpu->mmio_needed = 0;
1252 return EMULATE_DO_MMIO;
1255 return EMULATE_DONE;
1257 EXPORT_SYMBOL_GPL(emulate_instruction);
1259 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1261 if (vcpu->irq_summary)
1264 vcpu->run->exit_reason = KVM_EXIT_HLT;
1265 ++vcpu->stat.halt_exits;
1268 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1270 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1272 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1274 kvm_arch_ops->cache_regs(vcpu);
1276 #ifdef CONFIG_X86_64
1277 if (is_long_mode(vcpu)) {
1278 nr = vcpu->regs[VCPU_REGS_RAX];
1279 a0 = vcpu->regs[VCPU_REGS_RDI];
1280 a1 = vcpu->regs[VCPU_REGS_RSI];
1281 a2 = vcpu->regs[VCPU_REGS_RDX];
1282 a3 = vcpu->regs[VCPU_REGS_RCX];
1283 a4 = vcpu->regs[VCPU_REGS_R8];
1284 a5 = vcpu->regs[VCPU_REGS_R9];
1288 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1289 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1290 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1291 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1292 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1293 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1294 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1298 run->hypercall.nr = nr;
1299 run->hypercall.args[0] = a0;
1300 run->hypercall.args[1] = a1;
1301 run->hypercall.args[2] = a2;
1302 run->hypercall.args[3] = a3;
1303 run->hypercall.args[4] = a4;
1304 run->hypercall.args[5] = a5;
1305 run->hypercall.ret = ret;
1306 run->hypercall.longmode = is_long_mode(vcpu);
1307 kvm_arch_ops->decache_regs(vcpu);
1310 vcpu->regs[VCPU_REGS_RAX] = ret;
1311 kvm_arch_ops->decache_regs(vcpu);
1314 EXPORT_SYMBOL_GPL(kvm_hypercall);
1316 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1318 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1321 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1323 struct descriptor_table dt = { limit, base };
1325 kvm_arch_ops->set_gdt(vcpu, &dt);
1328 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1330 struct descriptor_table dt = { limit, base };
1332 kvm_arch_ops->set_idt(vcpu, &dt);
1335 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1336 unsigned long *rflags)
1339 *rflags = kvm_arch_ops->get_rflags(vcpu);
1342 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1344 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
1355 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1360 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1361 unsigned long *rflags)
1365 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1366 *rflags = kvm_arch_ops->get_rflags(vcpu);
1375 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1378 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1383 * Register the para guest with the host:
1385 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1387 struct kvm_vcpu_para_state *para_state;
1388 hpa_t para_state_hpa, hypercall_hpa;
1389 struct page *para_state_page;
1390 unsigned char *hypercall;
1391 gpa_t hypercall_gpa;
1393 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1394 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1397 * Needs to be page aligned:
1399 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1402 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1403 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1404 if (is_error_hpa(para_state_hpa))
1407 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1408 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1409 para_state = kmap(para_state_page);
1411 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1412 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1414 para_state->host_version = KVM_PARA_API_VERSION;
1416 * We cannot support guests that try to register themselves
1417 * with a newer API version than the host supports:
1419 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1420 para_state->ret = -KVM_EINVAL;
1421 goto err_kunmap_skip;
1424 hypercall_gpa = para_state->hypercall_gpa;
1425 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1426 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1427 if (is_error_hpa(hypercall_hpa)) {
1428 para_state->ret = -KVM_EINVAL;
1429 goto err_kunmap_skip;
1432 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1433 vcpu->para_state_page = para_state_page;
1434 vcpu->para_state_gpa = para_state_gpa;
1435 vcpu->hypercall_gpa = hypercall_gpa;
1437 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1438 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1439 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1440 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1441 kunmap_atomic(hypercall, KM_USER1);
1443 para_state->ret = 0;
1445 kunmap(para_state_page);
1451 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1456 case 0xc0010010: /* SYSCFG */
1457 case 0xc0010015: /* HWCR */
1458 case MSR_IA32_PLATFORM_ID:
1459 case MSR_IA32_P5_MC_ADDR:
1460 case MSR_IA32_P5_MC_TYPE:
1461 case MSR_IA32_MC0_CTL:
1462 case MSR_IA32_MCG_STATUS:
1463 case MSR_IA32_MCG_CAP:
1464 case MSR_IA32_MC0_MISC:
1465 case MSR_IA32_MC0_MISC+4:
1466 case MSR_IA32_MC0_MISC+8:
1467 case MSR_IA32_MC0_MISC+12:
1468 case MSR_IA32_MC0_MISC+16:
1469 case MSR_IA32_UCODE_REV:
1470 case MSR_IA32_PERF_STATUS:
1471 case MSR_IA32_EBL_CR_POWERON:
1472 /* MTRR registers */
1474 case 0x200 ... 0x2ff:
1477 case 0xcd: /* fsb frequency */
1480 case MSR_IA32_APICBASE:
1481 data = vcpu->apic_base;
1483 case MSR_IA32_MISC_ENABLE:
1484 data = vcpu->ia32_misc_enable_msr;
1486 #ifdef CONFIG_X86_64
1488 data = vcpu->shadow_efer;
1492 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1498 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1501 * Reads an msr value (of 'msr_index') into 'pdata'.
1502 * Returns 0 on success, non-0 otherwise.
1503 * Assumes vcpu_load() was already called.
1505 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1507 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1510 #ifdef CONFIG_X86_64
1512 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1514 if (efer & EFER_RESERVED_BITS) {
1515 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1522 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1523 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1528 kvm_arch_ops->set_efer(vcpu, efer);
1531 efer |= vcpu->shadow_efer & EFER_LMA;
1533 vcpu->shadow_efer = efer;
1538 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1541 #ifdef CONFIG_X86_64
1543 set_efer(vcpu, data);
1546 case MSR_IA32_MC0_STATUS:
1547 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1548 __FUNCTION__, data);
1550 case MSR_IA32_MCG_STATUS:
1551 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1552 __FUNCTION__, data);
1554 case MSR_IA32_UCODE_REV:
1555 case MSR_IA32_UCODE_WRITE:
1556 case 0x200 ... 0x2ff: /* MTRRs */
1558 case MSR_IA32_APICBASE:
1559 vcpu->apic_base = data;
1561 case MSR_IA32_MISC_ENABLE:
1562 vcpu->ia32_misc_enable_msr = data;
1565 * This is the 'probe whether the host is KVM' logic:
1567 case MSR_KVM_API_MAGIC:
1568 return vcpu_register_para(vcpu, data);
1571 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
1576 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1579 * Writes msr value into into the appropriate "register".
1580 * Returns 0 on success, non-0 otherwise.
1581 * Assumes vcpu_load() was already called.
1583 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1585 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1588 void kvm_resched(struct kvm_vcpu *vcpu)
1590 if (!need_resched())
1594 EXPORT_SYMBOL_GPL(kvm_resched);
1596 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1600 struct kvm_cpuid_entry *e, *best;
1602 kvm_arch_ops->cache_regs(vcpu);
1603 function = vcpu->regs[VCPU_REGS_RAX];
1604 vcpu->regs[VCPU_REGS_RAX] = 0;
1605 vcpu->regs[VCPU_REGS_RBX] = 0;
1606 vcpu->regs[VCPU_REGS_RCX] = 0;
1607 vcpu->regs[VCPU_REGS_RDX] = 0;
1609 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1610 e = &vcpu->cpuid_entries[i];
1611 if (e->function == function) {
1616 * Both basic or both extended?
1618 if (((e->function ^ function) & 0x80000000) == 0)
1619 if (!best || e->function > best->function)
1623 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1624 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1625 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1626 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1628 kvm_arch_ops->decache_regs(vcpu);
1629 kvm_arch_ops->skip_emulated_instruction(vcpu);
1631 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1633 static int pio_copy_data(struct kvm_vcpu *vcpu)
1635 void *p = vcpu->pio_data;
1638 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1640 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1643 free_pio_guest_pages(vcpu);
1646 q += vcpu->pio.guest_page_offset;
1647 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1649 memcpy(q, p, bytes);
1651 memcpy(p, q, bytes);
1652 q -= vcpu->pio.guest_page_offset;
1654 free_pio_guest_pages(vcpu);
1658 static int complete_pio(struct kvm_vcpu *vcpu)
1660 struct kvm_pio_request *io = &vcpu->pio;
1664 kvm_arch_ops->cache_regs(vcpu);
1668 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1672 r = pio_copy_data(vcpu);
1674 kvm_arch_ops->cache_regs(vcpu);
1681 delta *= io->cur_count;
1683 * The size of the register should really depend on
1684 * current address size.
1686 vcpu->regs[VCPU_REGS_RCX] -= delta;
1692 vcpu->regs[VCPU_REGS_RDI] += delta;
1694 vcpu->regs[VCPU_REGS_RSI] += delta;
1697 kvm_arch_ops->decache_regs(vcpu);
1699 io->count -= io->cur_count;
1703 kvm_arch_ops->skip_emulated_instruction(vcpu);
1707 static void kernel_pio(struct kvm_io_device *pio_dev,
1708 struct kvm_vcpu *vcpu,
1711 /* TODO: String I/O for in kernel device */
1714 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1718 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1723 static void pio_string_write(struct kvm_io_device *pio_dev,
1724 struct kvm_vcpu *vcpu)
1726 struct kvm_pio_request *io = &vcpu->pio;
1727 void *pd = vcpu->pio_data;
1730 for (i = 0; i < io->cur_count; i++) {
1731 kvm_iodevice_write(pio_dev, io->port,
1738 int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1739 int size, unsigned long count, int string, int down,
1740 gva_t address, int rep, unsigned port)
1742 unsigned now, in_page;
1746 struct kvm_io_device *pio_dev;
1748 vcpu->run->exit_reason = KVM_EXIT_IO;
1749 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1750 vcpu->run->io.size = size;
1751 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1752 vcpu->run->io.count = count;
1753 vcpu->run->io.port = port;
1754 vcpu->pio.count = count;
1755 vcpu->pio.cur_count = count;
1756 vcpu->pio.size = size;
1758 vcpu->pio.port = port;
1759 vcpu->pio.string = string;
1760 vcpu->pio.down = down;
1761 vcpu->pio.guest_page_offset = offset_in_page(address);
1762 vcpu->pio.rep = rep;
1764 pio_dev = vcpu_find_pio_dev(vcpu, port);
1766 kvm_arch_ops->cache_regs(vcpu);
1767 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1768 kvm_arch_ops->decache_regs(vcpu);
1770 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1778 kvm_arch_ops->skip_emulated_instruction(vcpu);
1783 in_page = PAGE_SIZE - offset_in_page(address);
1785 in_page = offset_in_page(address) + size;
1786 now = min(count, (unsigned long)in_page / size);
1789 * String I/O straddles page boundary. Pin two guest pages
1790 * so that we satisfy atomicity constraints. Do just one
1791 * transaction to avoid complexity.
1798 * String I/O in reverse. Yuck. Kill the guest, fix later.
1800 pr_unimpl(vcpu, "guest string pio down\n");
1804 vcpu->run->io.count = now;
1805 vcpu->pio.cur_count = now;
1807 for (i = 0; i < nr_pages; ++i) {
1808 mutex_lock(&vcpu->kvm->lock);
1809 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1812 vcpu->pio.guest_pages[i] = page;
1813 mutex_unlock(&vcpu->kvm->lock);
1816 free_pio_guest_pages(vcpu);
1821 if (!vcpu->pio.in) {
1822 /* string PIO write */
1823 ret = pio_copy_data(vcpu);
1824 if (ret >= 0 && pio_dev) {
1825 pio_string_write(pio_dev, vcpu);
1827 if (vcpu->pio.count == 0)
1831 pr_unimpl(vcpu, "no string pio read support yet, "
1832 "port %x size %d count %ld\n",
1837 EXPORT_SYMBOL_GPL(kvm_setup_pio);
1839 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1846 if (vcpu->sigset_active)
1847 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1849 /* re-sync apic's tpr */
1850 vcpu->cr8 = kvm_run->cr8;
1852 if (vcpu->pio.cur_count) {
1853 r = complete_pio(vcpu);
1858 if (vcpu->mmio_needed) {
1859 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1860 vcpu->mmio_read_completed = 1;
1861 vcpu->mmio_needed = 0;
1862 r = emulate_instruction(vcpu, kvm_run,
1863 vcpu->mmio_fault_cr2, 0);
1864 if (r == EMULATE_DO_MMIO) {
1866 * Read-modify-write. Back to userspace.
1873 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
1874 kvm_arch_ops->cache_regs(vcpu);
1875 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1876 kvm_arch_ops->decache_regs(vcpu);
1879 r = kvm_arch_ops->run(vcpu, kvm_run);
1882 if (vcpu->sigset_active)
1883 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1889 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1890 struct kvm_regs *regs)
1894 kvm_arch_ops->cache_regs(vcpu);
1896 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1897 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1898 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1899 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1900 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1901 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1902 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1903 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1904 #ifdef CONFIG_X86_64
1905 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1906 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1907 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1908 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1909 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1910 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1911 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1912 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1915 regs->rip = vcpu->rip;
1916 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1919 * Don't leak debug flags in case they were set for guest debugging
1921 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1922 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1929 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1930 struct kvm_regs *regs)
1934 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1935 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1936 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1937 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1938 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1939 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1940 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1941 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1942 #ifdef CONFIG_X86_64
1943 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1944 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1945 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1946 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1947 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1948 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1949 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1950 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1953 vcpu->rip = regs->rip;
1954 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1956 kvm_arch_ops->decache_regs(vcpu);
1963 static void get_segment(struct kvm_vcpu *vcpu,
1964 struct kvm_segment *var, int seg)
1966 return kvm_arch_ops->get_segment(vcpu, var, seg);
1969 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1970 struct kvm_sregs *sregs)
1972 struct descriptor_table dt;
1976 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1977 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1978 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1979 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1980 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1981 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1983 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1984 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1986 kvm_arch_ops->get_idt(vcpu, &dt);
1987 sregs->idt.limit = dt.limit;
1988 sregs->idt.base = dt.base;
1989 kvm_arch_ops->get_gdt(vcpu, &dt);
1990 sregs->gdt.limit = dt.limit;
1991 sregs->gdt.base = dt.base;
1993 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
1994 sregs->cr0 = vcpu->cr0;
1995 sregs->cr2 = vcpu->cr2;
1996 sregs->cr3 = vcpu->cr3;
1997 sregs->cr4 = vcpu->cr4;
1998 sregs->cr8 = vcpu->cr8;
1999 sregs->efer = vcpu->shadow_efer;
2000 sregs->apic_base = vcpu->apic_base;
2002 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2003 sizeof sregs->interrupt_bitmap);
2010 static void set_segment(struct kvm_vcpu *vcpu,
2011 struct kvm_segment *var, int seg)
2013 return kvm_arch_ops->set_segment(vcpu, var, seg);
2016 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2017 struct kvm_sregs *sregs)
2019 int mmu_reset_needed = 0;
2021 struct descriptor_table dt;
2025 dt.limit = sregs->idt.limit;
2026 dt.base = sregs->idt.base;
2027 kvm_arch_ops->set_idt(vcpu, &dt);
2028 dt.limit = sregs->gdt.limit;
2029 dt.base = sregs->gdt.base;
2030 kvm_arch_ops->set_gdt(vcpu, &dt);
2032 vcpu->cr2 = sregs->cr2;
2033 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2034 vcpu->cr3 = sregs->cr3;
2036 vcpu->cr8 = sregs->cr8;
2038 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2039 #ifdef CONFIG_X86_64
2040 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2042 vcpu->apic_base = sregs->apic_base;
2044 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2046 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2047 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
2049 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2050 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
2051 if (!is_long_mode(vcpu) && is_pae(vcpu))
2052 load_pdptrs(vcpu, vcpu->cr3);
2054 if (mmu_reset_needed)
2055 kvm_mmu_reset_context(vcpu);
2057 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2058 sizeof vcpu->irq_pending);
2059 vcpu->irq_summary = 0;
2060 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2061 if (vcpu->irq_pending[i])
2062 __set_bit(i, &vcpu->irq_summary);
2064 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2065 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2066 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2067 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2068 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2069 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2071 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2072 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2080 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2081 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2083 * This list is modified at module load time to reflect the
2084 * capabilities of the host cpu.
2086 static u32 msrs_to_save[] = {
2087 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2089 #ifdef CONFIG_X86_64
2090 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2092 MSR_IA32_TIME_STAMP_COUNTER,
2095 static unsigned num_msrs_to_save;
2097 static u32 emulated_msrs[] = {
2098 MSR_IA32_MISC_ENABLE,
2101 static __init void kvm_init_msr_list(void)
2106 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2107 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2110 msrs_to_save[j] = msrs_to_save[i];
2113 num_msrs_to_save = j;
2117 * Adapt set_msr() to msr_io()'s calling convention
2119 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2121 return kvm_set_msr(vcpu, index, *data);
2125 * Read or write a bunch of msrs. All parameters are kernel addresses.
2127 * @return number of msrs set successfully.
2129 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2130 struct kvm_msr_entry *entries,
2131 int (*do_msr)(struct kvm_vcpu *vcpu,
2132 unsigned index, u64 *data))
2138 for (i = 0; i < msrs->nmsrs; ++i)
2139 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2148 * Read or write a bunch of msrs. Parameters are user addresses.
2150 * @return number of msrs set successfully.
2152 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2153 int (*do_msr)(struct kvm_vcpu *vcpu,
2154 unsigned index, u64 *data),
2157 struct kvm_msrs msrs;
2158 struct kvm_msr_entry *entries;
2163 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2167 if (msrs.nmsrs >= MAX_IO_MSRS)
2171 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2172 entries = vmalloc(size);
2177 if (copy_from_user(entries, user_msrs->entries, size))
2180 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2185 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2197 * Translate a guest virtual address to a guest physical address.
2199 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2200 struct kvm_translation *tr)
2202 unsigned long vaddr = tr->linear_address;
2206 mutex_lock(&vcpu->kvm->lock);
2207 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2208 tr->physical_address = gpa;
2209 tr->valid = gpa != UNMAPPED_GVA;
2212 mutex_unlock(&vcpu->kvm->lock);
2218 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2219 struct kvm_interrupt *irq)
2221 if (irq->irq < 0 || irq->irq >= 256)
2225 set_bit(irq->irq, vcpu->irq_pending);
2226 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2233 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2234 struct kvm_debug_guest *dbg)
2240 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2247 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2248 unsigned long address,
2251 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2252 unsigned long pgoff;
2255 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2257 page = virt_to_page(vcpu->run);
2258 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2259 page = virt_to_page(vcpu->pio_data);
2261 return NOPAGE_SIGBUS;
2264 *type = VM_FAULT_MINOR;
2269 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2270 .nopage = kvm_vcpu_nopage,
2273 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2275 vma->vm_ops = &kvm_vcpu_vm_ops;
2279 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2281 struct kvm_vcpu *vcpu = filp->private_data;
2283 fput(vcpu->kvm->filp);
2287 static struct file_operations kvm_vcpu_fops = {
2288 .release = kvm_vcpu_release,
2289 .unlocked_ioctl = kvm_vcpu_ioctl,
2290 .compat_ioctl = kvm_vcpu_ioctl,
2291 .mmap = kvm_vcpu_mmap,
2295 * Allocates an inode for the vcpu.
2297 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2300 struct inode *inode;
2303 r = anon_inode_getfd(&fd, &inode, &file,
2304 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2307 atomic_inc(&vcpu->kvm->filp->f_count);
2312 * Creates some virtual cpus. Good luck creating more than one.
2314 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2317 struct kvm_vcpu *vcpu;
2322 vcpu = kvm_arch_ops->vcpu_create(kvm, n);
2324 return PTR_ERR(vcpu);
2326 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2328 /* We do fxsave: this must be aligned. */
2329 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2332 r = kvm_mmu_setup(vcpu);
2337 mutex_lock(&kvm->lock);
2338 if (kvm->vcpus[n]) {
2340 mutex_unlock(&kvm->lock);
2343 kvm->vcpus[n] = vcpu;
2344 mutex_unlock(&kvm->lock);
2346 /* Now it's all set up, let userspace reach it */
2347 r = create_vcpu_fd(vcpu);
2353 mutex_lock(&kvm->lock);
2354 kvm->vcpus[n] = NULL;
2355 mutex_unlock(&kvm->lock);
2359 kvm_mmu_unload(vcpu);
2363 kvm_arch_ops->vcpu_free(vcpu);
2367 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2371 struct kvm_cpuid_entry *e, *entry;
2373 rdmsrl(MSR_EFER, efer);
2375 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2376 e = &vcpu->cpuid_entries[i];
2377 if (e->function == 0x80000001) {
2382 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
2383 entry->edx &= ~(1 << 20);
2384 printk(KERN_INFO "kvm: guest NX capability removed\n");
2388 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2389 struct kvm_cpuid *cpuid,
2390 struct kvm_cpuid_entry __user *entries)
2395 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2398 if (copy_from_user(&vcpu->cpuid_entries, entries,
2399 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2401 vcpu->cpuid_nent = cpuid->nent;
2402 cpuid_fix_nx_cap(vcpu);
2409 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2412 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2413 vcpu->sigset_active = 1;
2414 vcpu->sigset = *sigset;
2416 vcpu->sigset_active = 0;
2421 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2422 * we have asm/x86/processor.h
2433 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2434 #ifdef CONFIG_X86_64
2435 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2437 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2441 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2443 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2447 memcpy(fpu->fpr, fxsave->st_space, 128);
2448 fpu->fcw = fxsave->cwd;
2449 fpu->fsw = fxsave->swd;
2450 fpu->ftwx = fxsave->twd;
2451 fpu->last_opcode = fxsave->fop;
2452 fpu->last_ip = fxsave->rip;
2453 fpu->last_dp = fxsave->rdp;
2454 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2461 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2463 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2467 memcpy(fxsave->st_space, fpu->fpr, 128);
2468 fxsave->cwd = fpu->fcw;
2469 fxsave->swd = fpu->fsw;
2470 fxsave->twd = fpu->ftwx;
2471 fxsave->fop = fpu->last_opcode;
2472 fxsave->rip = fpu->last_ip;
2473 fxsave->rdp = fpu->last_dp;
2474 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2481 static long kvm_vcpu_ioctl(struct file *filp,
2482 unsigned int ioctl, unsigned long arg)
2484 struct kvm_vcpu *vcpu = filp->private_data;
2485 void __user *argp = (void __user *)arg;
2493 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2495 case KVM_GET_REGS: {
2496 struct kvm_regs kvm_regs;
2498 memset(&kvm_regs, 0, sizeof kvm_regs);
2499 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2503 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2508 case KVM_SET_REGS: {
2509 struct kvm_regs kvm_regs;
2512 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2514 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2520 case KVM_GET_SREGS: {
2521 struct kvm_sregs kvm_sregs;
2523 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2524 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2528 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2533 case KVM_SET_SREGS: {
2534 struct kvm_sregs kvm_sregs;
2537 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2539 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2545 case KVM_TRANSLATE: {
2546 struct kvm_translation tr;
2549 if (copy_from_user(&tr, argp, sizeof tr))
2551 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2555 if (copy_to_user(argp, &tr, sizeof tr))
2560 case KVM_INTERRUPT: {
2561 struct kvm_interrupt irq;
2564 if (copy_from_user(&irq, argp, sizeof irq))
2566 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2572 case KVM_DEBUG_GUEST: {
2573 struct kvm_debug_guest dbg;
2576 if (copy_from_user(&dbg, argp, sizeof dbg))
2578 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2585 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2588 r = msr_io(vcpu, argp, do_set_msr, 0);
2590 case KVM_SET_CPUID: {
2591 struct kvm_cpuid __user *cpuid_arg = argp;
2592 struct kvm_cpuid cpuid;
2595 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2597 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2602 case KVM_SET_SIGNAL_MASK: {
2603 struct kvm_signal_mask __user *sigmask_arg = argp;
2604 struct kvm_signal_mask kvm_sigmask;
2605 sigset_t sigset, *p;
2610 if (copy_from_user(&kvm_sigmask, argp,
2611 sizeof kvm_sigmask))
2614 if (kvm_sigmask.len != sizeof sigset)
2617 if (copy_from_user(&sigset, sigmask_arg->sigset,
2622 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2628 memset(&fpu, 0, sizeof fpu);
2629 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2633 if (copy_to_user(argp, &fpu, sizeof fpu))
2642 if (copy_from_user(&fpu, argp, sizeof fpu))
2644 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2657 static long kvm_vm_ioctl(struct file *filp,
2658 unsigned int ioctl, unsigned long arg)
2660 struct kvm *kvm = filp->private_data;
2661 void __user *argp = (void __user *)arg;
2665 case KVM_CREATE_VCPU:
2666 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2670 case KVM_SET_MEMORY_REGION: {
2671 struct kvm_memory_region kvm_mem;
2674 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2676 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
2681 case KVM_GET_DIRTY_LOG: {
2682 struct kvm_dirty_log log;
2685 if (copy_from_user(&log, argp, sizeof log))
2687 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2692 case KVM_SET_MEMORY_ALIAS: {
2693 struct kvm_memory_alias alias;
2696 if (copy_from_user(&alias, argp, sizeof alias))
2698 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2710 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2711 unsigned long address,
2714 struct kvm *kvm = vma->vm_file->private_data;
2715 unsigned long pgoff;
2718 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2719 page = gfn_to_page(kvm, pgoff);
2721 return NOPAGE_SIGBUS;
2724 *type = VM_FAULT_MINOR;
2729 static struct vm_operations_struct kvm_vm_vm_ops = {
2730 .nopage = kvm_vm_nopage,
2733 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2735 vma->vm_ops = &kvm_vm_vm_ops;
2739 static struct file_operations kvm_vm_fops = {
2740 .release = kvm_vm_release,
2741 .unlocked_ioctl = kvm_vm_ioctl,
2742 .compat_ioctl = kvm_vm_ioctl,
2743 .mmap = kvm_vm_mmap,
2746 static int kvm_dev_ioctl_create_vm(void)
2749 struct inode *inode;
2753 kvm = kvm_create_vm();
2755 return PTR_ERR(kvm);
2756 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2758 kvm_destroy_vm(kvm);
2767 static long kvm_dev_ioctl(struct file *filp,
2768 unsigned int ioctl, unsigned long arg)
2770 void __user *argp = (void __user *)arg;
2774 case KVM_GET_API_VERSION:
2778 r = KVM_API_VERSION;
2784 r = kvm_dev_ioctl_create_vm();
2786 case KVM_GET_MSR_INDEX_LIST: {
2787 struct kvm_msr_list __user *user_msr_list = argp;
2788 struct kvm_msr_list msr_list;
2792 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2795 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2796 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2799 if (n < num_msrs_to_save)
2802 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2803 num_msrs_to_save * sizeof(u32)))
2805 if (copy_to_user(user_msr_list->indices
2806 + num_msrs_to_save * sizeof(u32),
2808 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2813 case KVM_CHECK_EXTENSION:
2815 * No extensions defined at present.
2819 case KVM_GET_VCPU_MMAP_SIZE:
2832 static struct file_operations kvm_chardev_ops = {
2833 .unlocked_ioctl = kvm_dev_ioctl,
2834 .compat_ioctl = kvm_dev_ioctl,
2837 static struct miscdevice kvm_dev = {
2844 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2847 static void decache_vcpus_on_cpu(int cpu)
2850 struct kvm_vcpu *vcpu;
2853 spin_lock(&kvm_lock);
2854 list_for_each_entry(vm, &vm_list, vm_list)
2855 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2856 vcpu = vm->vcpus[i];
2860 * If the vcpu is locked, then it is running on some
2861 * other cpu and therefore it is not cached on the
2864 * If it's not locked, check the last cpu it executed
2867 if (mutex_trylock(&vcpu->mutex)) {
2868 if (vcpu->cpu == cpu) {
2869 kvm_arch_ops->vcpu_decache(vcpu);
2872 mutex_unlock(&vcpu->mutex);
2875 spin_unlock(&kvm_lock);
2878 static void hardware_enable(void *junk)
2880 int cpu = raw_smp_processor_id();
2882 if (cpu_isset(cpu, cpus_hardware_enabled))
2884 cpu_set(cpu, cpus_hardware_enabled);
2885 kvm_arch_ops->hardware_enable(NULL);
2888 static void hardware_disable(void *junk)
2890 int cpu = raw_smp_processor_id();
2892 if (!cpu_isset(cpu, cpus_hardware_enabled))
2894 cpu_clear(cpu, cpus_hardware_enabled);
2895 decache_vcpus_on_cpu(cpu);
2896 kvm_arch_ops->hardware_disable(NULL);
2899 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2906 case CPU_DYING_FROZEN:
2907 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2909 hardware_disable(NULL);
2911 case CPU_UP_CANCELED:
2912 case CPU_UP_CANCELED_FROZEN:
2913 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2915 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
2918 case CPU_ONLINE_FROZEN:
2919 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2921 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
2927 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2930 if (val == SYS_RESTART) {
2932 * Some (well, at least mine) BIOSes hang on reboot if
2935 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2936 on_each_cpu(hardware_disable, NULL, 0, 1);
2941 static struct notifier_block kvm_reboot_notifier = {
2942 .notifier_call = kvm_reboot,
2946 void kvm_io_bus_init(struct kvm_io_bus *bus)
2948 memset(bus, 0, sizeof(*bus));
2951 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2955 for (i = 0; i < bus->dev_count; i++) {
2956 struct kvm_io_device *pos = bus->devs[i];
2958 kvm_iodevice_destructor(pos);
2962 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
2966 for (i = 0; i < bus->dev_count; i++) {
2967 struct kvm_io_device *pos = bus->devs[i];
2969 if (pos->in_range(pos, addr))
2976 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2978 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2980 bus->devs[bus->dev_count++] = dev;
2983 static struct notifier_block kvm_cpu_notifier = {
2984 .notifier_call = kvm_cpu_hotplug,
2985 .priority = 20, /* must be > scheduler priority */
2988 static u64 stat_get(void *_offset)
2990 unsigned offset = (long)_offset;
2993 struct kvm_vcpu *vcpu;
2996 spin_lock(&kvm_lock);
2997 list_for_each_entry(kvm, &vm_list, vm_list)
2998 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2999 vcpu = kvm->vcpus[i];
3001 total += *(u32 *)((void *)vcpu + offset);
3003 spin_unlock(&kvm_lock);
3007 DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
3009 static __init void kvm_init_debug(void)
3011 struct kvm_stats_debugfs_item *p;
3013 debugfs_dir = debugfs_create_dir("kvm", NULL);
3014 for (p = debugfs_entries; p->name; ++p)
3015 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3016 (void *)(long)p->offset,
3020 static void kvm_exit_debug(void)
3022 struct kvm_stats_debugfs_item *p;
3024 for (p = debugfs_entries; p->name; ++p)
3025 debugfs_remove(p->dentry);
3026 debugfs_remove(debugfs_dir);
3029 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3031 hardware_disable(NULL);
3035 static int kvm_resume(struct sys_device *dev)
3037 hardware_enable(NULL);
3041 static struct sysdev_class kvm_sysdev_class = {
3042 set_kset_name("kvm"),
3043 .suspend = kvm_suspend,
3044 .resume = kvm_resume,
3047 static struct sys_device kvm_sysdev = {
3049 .cls = &kvm_sysdev_class,
3052 hpa_t bad_page_address;
3055 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3057 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3060 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3062 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3064 kvm_arch_ops->vcpu_load(vcpu, cpu);
3067 static void kvm_sched_out(struct preempt_notifier *pn,
3068 struct task_struct *next)
3070 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3072 kvm_arch_ops->vcpu_put(vcpu);
3075 int kvm_init_arch(struct kvm_arch_ops *ops, unsigned int vcpu_size,
3076 struct module *module)
3082 printk(KERN_ERR "kvm: already loaded the other module\n");
3086 if (!ops->cpu_has_kvm_support()) {
3087 printk(KERN_ERR "kvm: no hardware support\n");
3090 if (ops->disabled_by_bios()) {
3091 printk(KERN_ERR "kvm: disabled by bios\n");
3097 r = kvm_arch_ops->hardware_setup();
3101 for_each_online_cpu(cpu) {
3102 smp_call_function_single(cpu,
3103 kvm_arch_ops->check_processor_compatibility,
3109 on_each_cpu(hardware_enable, NULL, 0, 1);
3110 r = register_cpu_notifier(&kvm_cpu_notifier);
3113 register_reboot_notifier(&kvm_reboot_notifier);
3115 r = sysdev_class_register(&kvm_sysdev_class);
3119 r = sysdev_register(&kvm_sysdev);
3123 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3124 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3125 __alignof__(struct kvm_vcpu), 0, 0);
3126 if (!kvm_vcpu_cache) {
3131 kvm_chardev_ops.owner = module;
3133 r = misc_register(&kvm_dev);
3135 printk (KERN_ERR "kvm: misc device register failed\n");
3139 kvm_preempt_ops.sched_in = kvm_sched_in;
3140 kvm_preempt_ops.sched_out = kvm_sched_out;
3145 kmem_cache_destroy(kvm_vcpu_cache);
3147 sysdev_unregister(&kvm_sysdev);
3149 sysdev_class_unregister(&kvm_sysdev_class);
3151 unregister_reboot_notifier(&kvm_reboot_notifier);
3152 unregister_cpu_notifier(&kvm_cpu_notifier);
3154 on_each_cpu(hardware_disable, NULL, 0, 1);
3156 kvm_arch_ops->hardware_unsetup();
3158 kvm_arch_ops = NULL;
3162 void kvm_exit_arch(void)
3164 misc_deregister(&kvm_dev);
3165 kmem_cache_destroy(kvm_vcpu_cache);
3166 sysdev_unregister(&kvm_sysdev);
3167 sysdev_class_unregister(&kvm_sysdev_class);
3168 unregister_reboot_notifier(&kvm_reboot_notifier);
3169 unregister_cpu_notifier(&kvm_cpu_notifier);
3170 on_each_cpu(hardware_disable, NULL, 0, 1);
3171 kvm_arch_ops->hardware_unsetup();
3172 kvm_arch_ops = NULL;
3175 static __init int kvm_init(void)
3177 static struct page *bad_page;
3180 r = kvm_mmu_module_init();
3186 kvm_init_msr_list();
3188 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3193 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3194 memset(__va(bad_page_address), 0, PAGE_SIZE);
3200 kvm_mmu_module_exit();
3205 static __exit void kvm_exit(void)
3208 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3209 kvm_mmu_module_exit();
3212 module_init(kvm_init)
3213 module_exit(kvm_exit)
3215 EXPORT_SYMBOL_GPL(kvm_init_arch);
3216 EXPORT_SYMBOL_GPL(kvm_exit_arch);