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"
23 #include <linux/kvm.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/percpu.h>
27 #include <linux/gfp.h>
29 #include <linux/miscdevice.h>
30 #include <linux/vmalloc.h>
31 #include <linux/reboot.h>
32 #include <linux/debugfs.h>
33 #include <linux/highmem.h>
34 #include <linux/file.h>
35 #include <linux/sysdev.h>
36 #include <linux/cpu.h>
37 #include <linux/sched.h>
38 #include <linux/cpumask.h>
39 #include <linux/smp.h>
40 #include <linux/anon_inodes.h>
41 #include <linux/profile.h>
43 #include <asm/processor.h>
46 #include <asm/uaccess.h>
49 MODULE_AUTHOR("Qumranet");
50 MODULE_LICENSE("GPL");
52 static DEFINE_SPINLOCK(kvm_lock);
53 static LIST_HEAD(vm_list);
55 static cpumask_t cpus_hardware_enabled;
57 struct kvm_x86_ops *kvm_x86_ops;
58 struct kmem_cache *kvm_vcpu_cache;
59 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
61 static __read_mostly struct preempt_ops kvm_preempt_ops;
63 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
65 static struct kvm_stats_debugfs_item {
68 struct dentry *dentry;
69 } debugfs_entries[] = {
70 { "pf_fixed", STAT_OFFSET(pf_fixed) },
71 { "pf_guest", STAT_OFFSET(pf_guest) },
72 { "tlb_flush", STAT_OFFSET(tlb_flush) },
73 { "invlpg", STAT_OFFSET(invlpg) },
74 { "exits", STAT_OFFSET(exits) },
75 { "io_exits", STAT_OFFSET(io_exits) },
76 { "mmio_exits", STAT_OFFSET(mmio_exits) },
77 { "signal_exits", STAT_OFFSET(signal_exits) },
78 { "irq_window", STAT_OFFSET(irq_window_exits) },
79 { "halt_exits", STAT_OFFSET(halt_exits) },
80 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
81 { "request_irq", STAT_OFFSET(request_irq_exits) },
82 { "irq_exits", STAT_OFFSET(irq_exits) },
83 { "light_exits", STAT_OFFSET(light_exits) },
84 { "efer_reload", STAT_OFFSET(efer_reload) },
88 static struct dentry *debugfs_dir;
90 #define MAX_IO_MSRS 256
92 #define CR0_RESERVED_BITS \
93 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
94 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
95 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
96 #define CR4_RESERVED_BITS \
97 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
98 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
99 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
100 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
102 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
103 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
106 // LDT or TSS descriptor in the GDT. 16 bytes.
107 struct segment_descriptor_64 {
108 struct segment_descriptor s;
115 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
118 unsigned long segment_base(u16 selector)
120 struct descriptor_table gdt;
121 struct segment_descriptor *d;
122 unsigned long table_base;
123 typedef unsigned long ul;
129 asm ("sgdt %0" : "=m"(gdt));
130 table_base = gdt.base;
132 if (selector & 4) { /* from ldt */
135 asm ("sldt %0" : "=g"(ldt_selector));
136 table_base = segment_base(ldt_selector);
138 d = (struct segment_descriptor *)(table_base + (selector & ~7));
139 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
142 && (d->type == 2 || d->type == 9 || d->type == 11))
143 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
147 EXPORT_SYMBOL_GPL(segment_base);
149 static inline int valid_vcpu(int n)
151 return likely(n >= 0 && n < KVM_MAX_VCPUS);
154 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
156 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
159 vcpu->guest_fpu_loaded = 1;
160 fx_save(&vcpu->host_fx_image);
161 fx_restore(&vcpu->guest_fx_image);
163 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
165 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
167 if (!vcpu->guest_fpu_loaded)
170 vcpu->guest_fpu_loaded = 0;
171 fx_save(&vcpu->guest_fx_image);
172 fx_restore(&vcpu->host_fx_image);
174 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
177 * Switches to specified vcpu, until a matching vcpu_put()
179 static void vcpu_load(struct kvm_vcpu *vcpu)
183 mutex_lock(&vcpu->mutex);
185 preempt_notifier_register(&vcpu->preempt_notifier);
186 kvm_x86_ops->vcpu_load(vcpu, cpu);
190 static void vcpu_put(struct kvm_vcpu *vcpu)
193 kvm_x86_ops->vcpu_put(vcpu);
194 preempt_notifier_unregister(&vcpu->preempt_notifier);
196 mutex_unlock(&vcpu->mutex);
199 static void ack_flush(void *_completed)
203 void kvm_flush_remote_tlbs(struct kvm *kvm)
207 struct kvm_vcpu *vcpu;
210 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
211 vcpu = kvm->vcpus[i];
214 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
217 if (cpu != -1 && cpu != raw_smp_processor_id())
220 smp_call_function_mask(cpus, ack_flush, NULL, 1);
223 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
228 mutex_init(&vcpu->mutex);
230 vcpu->mmu.root_hpa = INVALID_PAGE;
233 if (!irqchip_in_kernel(kvm) || id == 0)
234 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
236 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
237 init_waitqueue_head(&vcpu->wq);
239 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
244 vcpu->run = page_address(page);
246 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
251 vcpu->pio_data = page_address(page);
253 r = kvm_mmu_create(vcpu);
255 goto fail_free_pio_data;
260 free_page((unsigned long)vcpu->pio_data);
262 free_page((unsigned long)vcpu->run);
266 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
268 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
270 kvm_mmu_destroy(vcpu);
272 hrtimer_cancel(&vcpu->apic->timer.dev);
273 kvm_free_apic(vcpu->apic);
274 free_page((unsigned long)vcpu->pio_data);
275 free_page((unsigned long)vcpu->run);
277 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
279 static struct kvm *kvm_create_vm(void)
281 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
284 return ERR_PTR(-ENOMEM);
286 kvm_io_bus_init(&kvm->pio_bus);
287 mutex_init(&kvm->lock);
288 INIT_LIST_HEAD(&kvm->active_mmu_pages);
289 kvm_io_bus_init(&kvm->mmio_bus);
290 spin_lock(&kvm_lock);
291 list_add(&kvm->vm_list, &vm_list);
292 spin_unlock(&kvm_lock);
297 * Free any memory in @free but not in @dont.
299 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
300 struct kvm_memory_slot *dont)
304 if (!dont || free->phys_mem != dont->phys_mem)
305 if (free->phys_mem) {
306 for (i = 0; i < free->npages; ++i)
307 if (free->phys_mem[i])
308 __free_page(free->phys_mem[i]);
309 vfree(free->phys_mem);
312 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
313 vfree(free->dirty_bitmap);
315 free->phys_mem = NULL;
317 free->dirty_bitmap = NULL;
320 static void kvm_free_physmem(struct kvm *kvm)
324 for (i = 0; i < kvm->nmemslots; ++i)
325 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
328 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
332 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
333 if (vcpu->pio.guest_pages[i]) {
334 __free_page(vcpu->pio.guest_pages[i]);
335 vcpu->pio.guest_pages[i] = NULL;
339 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
342 kvm_mmu_unload(vcpu);
346 static void kvm_free_vcpus(struct kvm *kvm)
351 * Unpin any mmu pages first.
353 for (i = 0; i < KVM_MAX_VCPUS; ++i)
355 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
356 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
358 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
359 kvm->vcpus[i] = NULL;
365 static void kvm_destroy_vm(struct kvm *kvm)
367 spin_lock(&kvm_lock);
368 list_del(&kvm->vm_list);
369 spin_unlock(&kvm_lock);
370 kvm_io_bus_destroy(&kvm->pio_bus);
371 kvm_io_bus_destroy(&kvm->mmio_bus);
375 kvm_free_physmem(kvm);
379 static int kvm_vm_release(struct inode *inode, struct file *filp)
381 struct kvm *kvm = filp->private_data;
387 static void inject_gp(struct kvm_vcpu *vcpu)
389 kvm_x86_ops->inject_gp(vcpu, 0);
393 * Load the pae pdptrs. Return true is they are all valid.
395 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
397 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
398 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
403 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
405 mutex_lock(&vcpu->kvm->lock);
406 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
412 pdpt = kmap_atomic(page, KM_USER0);
413 memcpy(pdpte, pdpt+offset, sizeof(pdpte));
414 kunmap_atomic(pdpt, KM_USER0);
416 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
417 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
424 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
426 mutex_unlock(&vcpu->kvm->lock);
431 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
433 if (cr0 & CR0_RESERVED_BITS) {
434 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
440 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
441 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
446 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
447 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
448 "and a clear PE flag\n");
453 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
455 if ((vcpu->shadow_efer & EFER_LME)) {
459 printk(KERN_DEBUG "set_cr0: #GP, start paging "
460 "in long mode while PAE is disabled\n");
464 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
466 printk(KERN_DEBUG "set_cr0: #GP, start paging "
467 "in long mode while CS.L == 1\n");
474 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
475 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
483 kvm_x86_ops->set_cr0(vcpu, cr0);
486 mutex_lock(&vcpu->kvm->lock);
487 kvm_mmu_reset_context(vcpu);
488 mutex_unlock(&vcpu->kvm->lock);
491 EXPORT_SYMBOL_GPL(set_cr0);
493 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
495 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
497 EXPORT_SYMBOL_GPL(lmsw);
499 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
501 if (cr4 & CR4_RESERVED_BITS) {
502 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
507 if (is_long_mode(vcpu)) {
508 if (!(cr4 & X86_CR4_PAE)) {
509 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
514 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
515 && !load_pdptrs(vcpu, vcpu->cr3)) {
516 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
521 if (cr4 & X86_CR4_VMXE) {
522 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
526 kvm_x86_ops->set_cr4(vcpu, cr4);
528 mutex_lock(&vcpu->kvm->lock);
529 kvm_mmu_reset_context(vcpu);
530 mutex_unlock(&vcpu->kvm->lock);
532 EXPORT_SYMBOL_GPL(set_cr4);
534 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
536 if (is_long_mode(vcpu)) {
537 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
538 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
544 if (cr3 & CR3_PAE_RESERVED_BITS) {
546 "set_cr3: #GP, reserved bits\n");
550 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
551 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
557 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
559 "set_cr3: #GP, reserved bits\n");
566 mutex_lock(&vcpu->kvm->lock);
568 * Does the new cr3 value map to physical memory? (Note, we
569 * catch an invalid cr3 even in real-mode, because it would
570 * cause trouble later on when we turn on paging anyway.)
572 * A real CPU would silently accept an invalid cr3 and would
573 * attempt to use it - with largely undefined (and often hard
574 * to debug) behavior on the guest side.
576 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
580 vcpu->mmu.new_cr3(vcpu);
582 mutex_unlock(&vcpu->kvm->lock);
584 EXPORT_SYMBOL_GPL(set_cr3);
586 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
588 if (cr8 & CR8_RESERVED_BITS) {
589 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
593 if (irqchip_in_kernel(vcpu->kvm))
594 kvm_lapic_set_tpr(vcpu, cr8);
598 EXPORT_SYMBOL_GPL(set_cr8);
600 unsigned long get_cr8(struct kvm_vcpu *vcpu)
602 if (irqchip_in_kernel(vcpu->kvm))
603 return kvm_lapic_get_cr8(vcpu);
607 EXPORT_SYMBOL_GPL(get_cr8);
609 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
611 if (irqchip_in_kernel(vcpu->kvm))
612 return vcpu->apic_base;
614 return vcpu->apic_base;
616 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
618 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
620 /* TODO: reserve bits check */
621 if (irqchip_in_kernel(vcpu->kvm))
622 kvm_lapic_set_base(vcpu, data);
624 vcpu->apic_base = data;
626 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
628 void fx_init(struct kvm_vcpu *vcpu)
630 unsigned after_mxcsr_mask;
632 /* Initialize guest FPU by resetting ours and saving into guest's */
634 fx_save(&vcpu->host_fx_image);
636 fx_save(&vcpu->guest_fx_image);
637 fx_restore(&vcpu->host_fx_image);
640 vcpu->cr0 |= X86_CR0_ET;
641 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
642 vcpu->guest_fx_image.mxcsr = 0x1f80;
643 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
644 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
646 EXPORT_SYMBOL_GPL(fx_init);
649 * Allocate some memory and give it an address in the guest physical address
652 * Discontiguous memory is allowed, mostly for framebuffers.
654 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
655 struct kvm_memory_region *mem)
659 unsigned long npages;
661 struct kvm_memory_slot *memslot;
662 struct kvm_memory_slot old, new;
665 /* General sanity checks */
666 if (mem->memory_size & (PAGE_SIZE - 1))
668 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
670 if (mem->slot >= KVM_MEMORY_SLOTS)
672 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
675 memslot = &kvm->memslots[mem->slot];
676 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
677 npages = mem->memory_size >> PAGE_SHIFT;
680 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
682 mutex_lock(&kvm->lock);
684 new = old = *memslot;
686 new.base_gfn = base_gfn;
688 new.flags = mem->flags;
690 /* Disallow changing a memory slot's size. */
692 if (npages && old.npages && npages != old.npages)
695 /* Check for overlaps */
697 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
698 struct kvm_memory_slot *s = &kvm->memslots[i];
702 if (!((base_gfn + npages <= s->base_gfn) ||
703 (base_gfn >= s->base_gfn + s->npages)))
707 /* Deallocate if slot is being removed */
711 /* Free page dirty bitmap if unneeded */
712 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
713 new.dirty_bitmap = NULL;
717 /* Allocate if a slot is being created */
718 if (npages && !new.phys_mem) {
719 new.phys_mem = vmalloc(npages * sizeof(struct page *));
724 memset(new.phys_mem, 0, npages * sizeof(struct page *));
725 for (i = 0; i < npages; ++i) {
726 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
728 if (!new.phys_mem[i])
730 set_page_private(new.phys_mem[i],0);
734 /* Allocate page dirty bitmap if needed */
735 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
736 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
738 new.dirty_bitmap = vmalloc(dirty_bytes);
739 if (!new.dirty_bitmap)
741 memset(new.dirty_bitmap, 0, dirty_bytes);
744 if (mem->slot >= kvm->nmemslots)
745 kvm->nmemslots = mem->slot + 1;
749 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
750 kvm_flush_remote_tlbs(kvm);
752 mutex_unlock(&kvm->lock);
754 kvm_free_physmem_slot(&old, &new);
758 mutex_unlock(&kvm->lock);
759 kvm_free_physmem_slot(&new, &old);
765 * Get (and clear) the dirty memory log for a memory slot.
767 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
768 struct kvm_dirty_log *log)
770 struct kvm_memory_slot *memslot;
773 unsigned long any = 0;
775 mutex_lock(&kvm->lock);
778 if (log->slot >= KVM_MEMORY_SLOTS)
781 memslot = &kvm->memslots[log->slot];
783 if (!memslot->dirty_bitmap)
786 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
788 for (i = 0; !any && i < n/sizeof(long); ++i)
789 any = memslot->dirty_bitmap[i];
792 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
795 /* If nothing is dirty, don't bother messing with page tables. */
797 kvm_mmu_slot_remove_write_access(kvm, log->slot);
798 kvm_flush_remote_tlbs(kvm);
799 memset(memslot->dirty_bitmap, 0, n);
805 mutex_unlock(&kvm->lock);
810 * Set a new alias region. Aliases map a portion of physical memory into
811 * another portion. This is useful for memory windows, for example the PC
814 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
815 struct kvm_memory_alias *alias)
818 struct kvm_mem_alias *p;
821 /* General sanity checks */
822 if (alias->memory_size & (PAGE_SIZE - 1))
824 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
826 if (alias->slot >= KVM_ALIAS_SLOTS)
828 if (alias->guest_phys_addr + alias->memory_size
829 < alias->guest_phys_addr)
831 if (alias->target_phys_addr + alias->memory_size
832 < alias->target_phys_addr)
835 mutex_lock(&kvm->lock);
837 p = &kvm->aliases[alias->slot];
838 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
839 p->npages = alias->memory_size >> PAGE_SHIFT;
840 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
842 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
843 if (kvm->aliases[n - 1].npages)
847 kvm_mmu_zap_all(kvm);
849 mutex_unlock(&kvm->lock);
857 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
862 switch (chip->chip_id) {
863 case KVM_IRQCHIP_PIC_MASTER:
864 memcpy (&chip->chip.pic,
865 &pic_irqchip(kvm)->pics[0],
866 sizeof(struct kvm_pic_state));
868 case KVM_IRQCHIP_PIC_SLAVE:
869 memcpy (&chip->chip.pic,
870 &pic_irqchip(kvm)->pics[1],
871 sizeof(struct kvm_pic_state));
873 case KVM_IRQCHIP_IOAPIC:
874 memcpy (&chip->chip.ioapic,
876 sizeof(struct kvm_ioapic_state));
885 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
890 switch (chip->chip_id) {
891 case KVM_IRQCHIP_PIC_MASTER:
892 memcpy (&pic_irqchip(kvm)->pics[0],
894 sizeof(struct kvm_pic_state));
896 case KVM_IRQCHIP_PIC_SLAVE:
897 memcpy (&pic_irqchip(kvm)->pics[1],
899 sizeof(struct kvm_pic_state));
901 case KVM_IRQCHIP_IOAPIC:
902 memcpy (ioapic_irqchip(kvm),
904 sizeof(struct kvm_ioapic_state));
910 kvm_pic_update_irq(pic_irqchip(kvm));
914 static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
917 struct kvm_mem_alias *alias;
919 for (i = 0; i < kvm->naliases; ++i) {
920 alias = &kvm->aliases[i];
921 if (gfn >= alias->base_gfn
922 && gfn < alias->base_gfn + alias->npages)
923 return alias->target_gfn + gfn - alias->base_gfn;
928 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
932 for (i = 0; i < kvm->nmemslots; ++i) {
933 struct kvm_memory_slot *memslot = &kvm->memslots[i];
935 if (gfn >= memslot->base_gfn
936 && gfn < memslot->base_gfn + memslot->npages)
942 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
944 gfn = unalias_gfn(kvm, gfn);
945 return __gfn_to_memslot(kvm, gfn);
948 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
950 struct kvm_memory_slot *slot;
952 gfn = unalias_gfn(kvm, gfn);
953 slot = __gfn_to_memslot(kvm, gfn);
956 return slot->phys_mem[gfn - slot->base_gfn];
958 EXPORT_SYMBOL_GPL(gfn_to_page);
960 /* WARNING: Does not work on aliased pages. */
961 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
963 struct kvm_memory_slot *memslot;
965 memslot = __gfn_to_memslot(kvm, gfn);
966 if (memslot && memslot->dirty_bitmap) {
967 unsigned long rel_gfn = gfn - memslot->base_gfn;
970 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
971 set_bit(rel_gfn, memslot->dirty_bitmap);
975 int emulator_read_std(unsigned long addr,
978 struct kvm_vcpu *vcpu)
983 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
984 unsigned offset = addr & (PAGE_SIZE-1);
985 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
990 if (gpa == UNMAPPED_GVA)
991 return X86EMUL_PROPAGATE_FAULT;
992 pfn = gpa >> PAGE_SHIFT;
993 page = gfn_to_page(vcpu->kvm, pfn);
995 return X86EMUL_UNHANDLEABLE;
996 page_virt = kmap_atomic(page, KM_USER0);
998 memcpy(data, page_virt + offset, tocopy);
1000 kunmap_atomic(page_virt, KM_USER0);
1007 return X86EMUL_CONTINUE;
1009 EXPORT_SYMBOL_GPL(emulator_read_std);
1011 static int emulator_write_std(unsigned long addr,
1014 struct kvm_vcpu *vcpu)
1016 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
1017 return X86EMUL_UNHANDLEABLE;
1021 * Only apic need an MMIO device hook, so shortcut now..
1023 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1026 struct kvm_io_device *dev;
1029 dev = &vcpu->apic->dev;
1030 if (dev->in_range(dev, addr))
1036 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1039 struct kvm_io_device *dev;
1041 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1043 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1047 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1050 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1053 static int emulator_read_emulated(unsigned long addr,
1056 struct kvm_vcpu *vcpu)
1058 struct kvm_io_device *mmio_dev;
1061 if (vcpu->mmio_read_completed) {
1062 memcpy(val, vcpu->mmio_data, bytes);
1063 vcpu->mmio_read_completed = 0;
1064 return X86EMUL_CONTINUE;
1065 } else if (emulator_read_std(addr, val, bytes, vcpu)
1066 == X86EMUL_CONTINUE)
1067 return X86EMUL_CONTINUE;
1069 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1070 if (gpa == UNMAPPED_GVA)
1071 return X86EMUL_PROPAGATE_FAULT;
1074 * Is this MMIO handled locally?
1076 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1078 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1079 return X86EMUL_CONTINUE;
1082 vcpu->mmio_needed = 1;
1083 vcpu->mmio_phys_addr = gpa;
1084 vcpu->mmio_size = bytes;
1085 vcpu->mmio_is_write = 0;
1087 return X86EMUL_UNHANDLEABLE;
1090 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1091 const void *val, int bytes)
1096 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1098 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1101 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
1102 virt = kmap_atomic(page, KM_USER0);
1103 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1104 memcpy(virt + offset_in_page(gpa), val, bytes);
1105 kunmap_atomic(virt, KM_USER0);
1109 static int emulator_write_emulated_onepage(unsigned long addr,
1112 struct kvm_vcpu *vcpu)
1114 struct kvm_io_device *mmio_dev;
1115 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1117 if (gpa == UNMAPPED_GVA) {
1118 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
1119 return X86EMUL_PROPAGATE_FAULT;
1122 if (emulator_write_phys(vcpu, gpa, val, bytes))
1123 return X86EMUL_CONTINUE;
1126 * Is this MMIO handled locally?
1128 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1130 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1131 return X86EMUL_CONTINUE;
1134 vcpu->mmio_needed = 1;
1135 vcpu->mmio_phys_addr = gpa;
1136 vcpu->mmio_size = bytes;
1137 vcpu->mmio_is_write = 1;
1138 memcpy(vcpu->mmio_data, val, bytes);
1140 return X86EMUL_CONTINUE;
1143 int emulator_write_emulated(unsigned long addr,
1146 struct kvm_vcpu *vcpu)
1148 /* Crossing a page boundary? */
1149 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1152 now = -addr & ~PAGE_MASK;
1153 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1154 if (rc != X86EMUL_CONTINUE)
1160 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1162 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1164 static int emulator_cmpxchg_emulated(unsigned long addr,
1168 struct kvm_vcpu *vcpu)
1170 static int reported;
1174 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1176 return emulator_write_emulated(addr, new, bytes, vcpu);
1179 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1181 return kvm_x86_ops->get_segment_base(vcpu, seg);
1184 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1186 return X86EMUL_CONTINUE;
1189 int emulate_clts(struct kvm_vcpu *vcpu)
1191 vcpu->cr0 &= ~X86_CR0_TS;
1192 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0);
1193 return X86EMUL_CONTINUE;
1196 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1198 struct kvm_vcpu *vcpu = ctxt->vcpu;
1202 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1203 return X86EMUL_CONTINUE;
1205 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1206 return X86EMUL_UNHANDLEABLE;
1210 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1212 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1215 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1217 /* FIXME: better handling */
1218 return X86EMUL_UNHANDLEABLE;
1220 return X86EMUL_CONTINUE;
1223 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1225 static int reported;
1227 unsigned long rip = vcpu->rip;
1228 unsigned long rip_linear;
1230 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1235 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1237 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1238 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1241 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1243 struct x86_emulate_ops emulate_ops = {
1244 .read_std = emulator_read_std,
1245 .write_std = emulator_write_std,
1246 .read_emulated = emulator_read_emulated,
1247 .write_emulated = emulator_write_emulated,
1248 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1251 int emulate_instruction(struct kvm_vcpu *vcpu,
1252 struct kvm_run *run,
1256 struct x86_emulate_ctxt emulate_ctxt;
1260 vcpu->mmio_fault_cr2 = cr2;
1261 kvm_x86_ops->cache_regs(vcpu);
1263 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1265 emulate_ctxt.vcpu = vcpu;
1266 emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1267 emulate_ctxt.cr2 = cr2;
1268 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1269 ? X86EMUL_MODE_REAL : cs_l
1270 ? X86EMUL_MODE_PROT64 : cs_db
1271 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1273 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1274 emulate_ctxt.cs_base = 0;
1275 emulate_ctxt.ds_base = 0;
1276 emulate_ctxt.es_base = 0;
1277 emulate_ctxt.ss_base = 0;
1279 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1280 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1281 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1282 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1285 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1286 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1288 vcpu->mmio_is_write = 0;
1289 vcpu->pio.string = 0;
1290 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1291 if (vcpu->pio.string)
1292 return EMULATE_DO_MMIO;
1294 if ((r || vcpu->mmio_is_write) && run) {
1295 run->exit_reason = KVM_EXIT_MMIO;
1296 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1297 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1298 run->mmio.len = vcpu->mmio_size;
1299 run->mmio.is_write = vcpu->mmio_is_write;
1303 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1304 return EMULATE_DONE;
1305 if (!vcpu->mmio_needed) {
1306 kvm_report_emulation_failure(vcpu, "mmio");
1307 return EMULATE_FAIL;
1309 return EMULATE_DO_MMIO;
1312 kvm_x86_ops->decache_regs(vcpu);
1313 kvm_x86_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1315 if (vcpu->mmio_is_write) {
1316 vcpu->mmio_needed = 0;
1317 return EMULATE_DO_MMIO;
1320 return EMULATE_DONE;
1322 EXPORT_SYMBOL_GPL(emulate_instruction);
1325 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1327 static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1329 DECLARE_WAITQUEUE(wait, current);
1331 add_wait_queue(&vcpu->wq, &wait);
1334 * We will block until either an interrupt or a signal wakes us up
1336 while (!kvm_cpu_has_interrupt(vcpu)
1337 && !signal_pending(current)
1338 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1339 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
1340 set_current_state(TASK_INTERRUPTIBLE);
1346 __set_current_state(TASK_RUNNING);
1347 remove_wait_queue(&vcpu->wq, &wait);
1350 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1352 ++vcpu->stat.halt_exits;
1353 if (irqchip_in_kernel(vcpu->kvm)) {
1354 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1355 kvm_vcpu_block(vcpu);
1356 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1360 vcpu->run->exit_reason = KVM_EXIT_HLT;
1364 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1366 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1368 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1370 kvm_x86_ops->cache_regs(vcpu);
1372 #ifdef CONFIG_X86_64
1373 if (is_long_mode(vcpu)) {
1374 nr = vcpu->regs[VCPU_REGS_RAX];
1375 a0 = vcpu->regs[VCPU_REGS_RDI];
1376 a1 = vcpu->regs[VCPU_REGS_RSI];
1377 a2 = vcpu->regs[VCPU_REGS_RDX];
1378 a3 = vcpu->regs[VCPU_REGS_RCX];
1379 a4 = vcpu->regs[VCPU_REGS_R8];
1380 a5 = vcpu->regs[VCPU_REGS_R9];
1384 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1385 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1386 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1387 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1388 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1389 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1390 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1394 run->hypercall.nr = nr;
1395 run->hypercall.args[0] = a0;
1396 run->hypercall.args[1] = a1;
1397 run->hypercall.args[2] = a2;
1398 run->hypercall.args[3] = a3;
1399 run->hypercall.args[4] = a4;
1400 run->hypercall.args[5] = a5;
1401 run->hypercall.ret = ret;
1402 run->hypercall.longmode = is_long_mode(vcpu);
1403 kvm_x86_ops->decache_regs(vcpu);
1406 vcpu->regs[VCPU_REGS_RAX] = ret;
1407 kvm_x86_ops->decache_regs(vcpu);
1410 EXPORT_SYMBOL_GPL(kvm_hypercall);
1412 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1414 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1417 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1419 struct descriptor_table dt = { limit, base };
1421 kvm_x86_ops->set_gdt(vcpu, &dt);
1424 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1426 struct descriptor_table dt = { limit, base };
1428 kvm_x86_ops->set_idt(vcpu, &dt);
1431 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1432 unsigned long *rflags)
1435 *rflags = kvm_x86_ops->get_rflags(vcpu);
1438 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1440 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
1451 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1456 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1457 unsigned long *rflags)
1461 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1462 *rflags = kvm_x86_ops->get_rflags(vcpu);
1471 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1474 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1479 * Register the para guest with the host:
1481 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1483 struct kvm_vcpu_para_state *para_state;
1484 hpa_t para_state_hpa, hypercall_hpa;
1485 struct page *para_state_page;
1486 unsigned char *hypercall;
1487 gpa_t hypercall_gpa;
1489 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1490 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1493 * Needs to be page aligned:
1495 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1498 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1499 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1500 if (is_error_hpa(para_state_hpa))
1503 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1504 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1505 para_state = kmap(para_state_page);
1507 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1508 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1510 para_state->host_version = KVM_PARA_API_VERSION;
1512 * We cannot support guests that try to register themselves
1513 * with a newer API version than the host supports:
1515 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1516 para_state->ret = -KVM_EINVAL;
1517 goto err_kunmap_skip;
1520 hypercall_gpa = para_state->hypercall_gpa;
1521 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1522 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1523 if (is_error_hpa(hypercall_hpa)) {
1524 para_state->ret = -KVM_EINVAL;
1525 goto err_kunmap_skip;
1528 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1529 vcpu->para_state_page = para_state_page;
1530 vcpu->para_state_gpa = para_state_gpa;
1531 vcpu->hypercall_gpa = hypercall_gpa;
1533 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1534 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1535 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1536 kvm_x86_ops->patch_hypercall(vcpu, hypercall);
1537 kunmap_atomic(hypercall, KM_USER1);
1539 para_state->ret = 0;
1541 kunmap(para_state_page);
1547 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1552 case 0xc0010010: /* SYSCFG */
1553 case 0xc0010015: /* HWCR */
1554 case MSR_IA32_PLATFORM_ID:
1555 case MSR_IA32_P5_MC_ADDR:
1556 case MSR_IA32_P5_MC_TYPE:
1557 case MSR_IA32_MC0_CTL:
1558 case MSR_IA32_MCG_STATUS:
1559 case MSR_IA32_MCG_CAP:
1560 case MSR_IA32_MC0_MISC:
1561 case MSR_IA32_MC0_MISC+4:
1562 case MSR_IA32_MC0_MISC+8:
1563 case MSR_IA32_MC0_MISC+12:
1564 case MSR_IA32_MC0_MISC+16:
1565 case MSR_IA32_UCODE_REV:
1566 case MSR_IA32_PERF_STATUS:
1567 case MSR_IA32_EBL_CR_POWERON:
1568 /* MTRR registers */
1570 case 0x200 ... 0x2ff:
1573 case 0xcd: /* fsb frequency */
1576 case MSR_IA32_APICBASE:
1577 data = kvm_get_apic_base(vcpu);
1579 case MSR_IA32_MISC_ENABLE:
1580 data = vcpu->ia32_misc_enable_msr;
1582 #ifdef CONFIG_X86_64
1584 data = vcpu->shadow_efer;
1588 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1594 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1597 * Reads an msr value (of 'msr_index') into 'pdata'.
1598 * Returns 0 on success, non-0 otherwise.
1599 * Assumes vcpu_load() was already called.
1601 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1603 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1606 #ifdef CONFIG_X86_64
1608 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1610 if (efer & EFER_RESERVED_BITS) {
1611 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1618 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1619 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1624 kvm_x86_ops->set_efer(vcpu, efer);
1627 efer |= vcpu->shadow_efer & EFER_LMA;
1629 vcpu->shadow_efer = efer;
1634 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1637 #ifdef CONFIG_X86_64
1639 set_efer(vcpu, data);
1642 case MSR_IA32_MC0_STATUS:
1643 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1644 __FUNCTION__, data);
1646 case MSR_IA32_MCG_STATUS:
1647 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1648 __FUNCTION__, data);
1650 case MSR_IA32_UCODE_REV:
1651 case MSR_IA32_UCODE_WRITE:
1652 case 0x200 ... 0x2ff: /* MTRRs */
1654 case MSR_IA32_APICBASE:
1655 kvm_set_apic_base(vcpu, data);
1657 case MSR_IA32_MISC_ENABLE:
1658 vcpu->ia32_misc_enable_msr = data;
1661 * This is the 'probe whether the host is KVM' logic:
1663 case MSR_KVM_API_MAGIC:
1664 return vcpu_register_para(vcpu, data);
1667 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
1672 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1675 * Writes msr value into into the appropriate "register".
1676 * Returns 0 on success, non-0 otherwise.
1677 * Assumes vcpu_load() was already called.
1679 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1681 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
1684 void kvm_resched(struct kvm_vcpu *vcpu)
1686 if (!need_resched())
1690 EXPORT_SYMBOL_GPL(kvm_resched);
1692 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1696 struct kvm_cpuid_entry *e, *best;
1698 kvm_x86_ops->cache_regs(vcpu);
1699 function = vcpu->regs[VCPU_REGS_RAX];
1700 vcpu->regs[VCPU_REGS_RAX] = 0;
1701 vcpu->regs[VCPU_REGS_RBX] = 0;
1702 vcpu->regs[VCPU_REGS_RCX] = 0;
1703 vcpu->regs[VCPU_REGS_RDX] = 0;
1705 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1706 e = &vcpu->cpuid_entries[i];
1707 if (e->function == function) {
1712 * Both basic or both extended?
1714 if (((e->function ^ function) & 0x80000000) == 0)
1715 if (!best || e->function > best->function)
1719 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1720 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1721 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1722 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1724 kvm_x86_ops->decache_regs(vcpu);
1725 kvm_x86_ops->skip_emulated_instruction(vcpu);
1727 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1729 static int pio_copy_data(struct kvm_vcpu *vcpu)
1731 void *p = vcpu->pio_data;
1734 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1736 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1739 free_pio_guest_pages(vcpu);
1742 q += vcpu->pio.guest_page_offset;
1743 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1745 memcpy(q, p, bytes);
1747 memcpy(p, q, bytes);
1748 q -= vcpu->pio.guest_page_offset;
1750 free_pio_guest_pages(vcpu);
1754 static int complete_pio(struct kvm_vcpu *vcpu)
1756 struct kvm_pio_request *io = &vcpu->pio;
1760 kvm_x86_ops->cache_regs(vcpu);
1764 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1768 r = pio_copy_data(vcpu);
1770 kvm_x86_ops->cache_regs(vcpu);
1777 delta *= io->cur_count;
1779 * The size of the register should really depend on
1780 * current address size.
1782 vcpu->regs[VCPU_REGS_RCX] -= delta;
1788 vcpu->regs[VCPU_REGS_RDI] += delta;
1790 vcpu->regs[VCPU_REGS_RSI] += delta;
1793 kvm_x86_ops->decache_regs(vcpu);
1795 io->count -= io->cur_count;
1801 static void kernel_pio(struct kvm_io_device *pio_dev,
1802 struct kvm_vcpu *vcpu,
1805 /* TODO: String I/O for in kernel device */
1807 mutex_lock(&vcpu->kvm->lock);
1809 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1813 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1816 mutex_unlock(&vcpu->kvm->lock);
1819 static void pio_string_write(struct kvm_io_device *pio_dev,
1820 struct kvm_vcpu *vcpu)
1822 struct kvm_pio_request *io = &vcpu->pio;
1823 void *pd = vcpu->pio_data;
1826 mutex_lock(&vcpu->kvm->lock);
1827 for (i = 0; i < io->cur_count; i++) {
1828 kvm_iodevice_write(pio_dev, io->port,
1833 mutex_unlock(&vcpu->kvm->lock);
1836 int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1837 int size, unsigned port)
1839 struct kvm_io_device *pio_dev;
1841 vcpu->run->exit_reason = KVM_EXIT_IO;
1842 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1843 vcpu->run->io.size = vcpu->pio.size = size;
1844 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1845 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1846 vcpu->run->io.port = vcpu->pio.port = port;
1848 vcpu->pio.string = 0;
1850 vcpu->pio.guest_page_offset = 0;
1853 kvm_x86_ops->cache_regs(vcpu);
1854 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1855 kvm_x86_ops->decache_regs(vcpu);
1857 kvm_x86_ops->skip_emulated_instruction(vcpu);
1859 pio_dev = vcpu_find_pio_dev(vcpu, port);
1861 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1867 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1869 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1870 int size, unsigned long count, int down,
1871 gva_t address, int rep, unsigned port)
1873 unsigned now, in_page;
1877 struct kvm_io_device *pio_dev;
1879 vcpu->run->exit_reason = KVM_EXIT_IO;
1880 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1881 vcpu->run->io.size = vcpu->pio.size = size;
1882 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1883 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1884 vcpu->run->io.port = vcpu->pio.port = port;
1886 vcpu->pio.string = 1;
1887 vcpu->pio.down = down;
1888 vcpu->pio.guest_page_offset = offset_in_page(address);
1889 vcpu->pio.rep = rep;
1892 kvm_x86_ops->skip_emulated_instruction(vcpu);
1897 in_page = PAGE_SIZE - offset_in_page(address);
1899 in_page = offset_in_page(address) + size;
1900 now = min(count, (unsigned long)in_page / size);
1903 * String I/O straddles page boundary. Pin two guest pages
1904 * so that we satisfy atomicity constraints. Do just one
1905 * transaction to avoid complexity.
1912 * String I/O in reverse. Yuck. Kill the guest, fix later.
1914 pr_unimpl(vcpu, "guest string pio down\n");
1918 vcpu->run->io.count = now;
1919 vcpu->pio.cur_count = now;
1921 if (vcpu->pio.cur_count == vcpu->pio.count)
1922 kvm_x86_ops->skip_emulated_instruction(vcpu);
1924 for (i = 0; i < nr_pages; ++i) {
1925 mutex_lock(&vcpu->kvm->lock);
1926 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1929 vcpu->pio.guest_pages[i] = page;
1930 mutex_unlock(&vcpu->kvm->lock);
1933 free_pio_guest_pages(vcpu);
1938 pio_dev = vcpu_find_pio_dev(vcpu, port);
1939 if (!vcpu->pio.in) {
1940 /* string PIO write */
1941 ret = pio_copy_data(vcpu);
1942 if (ret >= 0 && pio_dev) {
1943 pio_string_write(pio_dev, vcpu);
1945 if (vcpu->pio.count == 0)
1949 pr_unimpl(vcpu, "no string pio read support yet, "
1950 "port %x size %d count %ld\n",
1955 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
1958 * Check if userspace requested an interrupt window, and that the
1959 * interrupt window is open.
1961 * No need to exit to userspace if we already have an interrupt queued.
1963 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
1964 struct kvm_run *kvm_run)
1966 return (!vcpu->irq_summary &&
1967 kvm_run->request_interrupt_window &&
1968 vcpu->interrupt_window_open &&
1969 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
1972 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1973 struct kvm_run *kvm_run)
1975 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
1976 kvm_run->cr8 = get_cr8(vcpu);
1977 kvm_run->apic_base = kvm_get_apic_base(vcpu);
1978 if (irqchip_in_kernel(vcpu->kvm))
1979 kvm_run->ready_for_interrupt_injection = 1;
1981 kvm_run->ready_for_interrupt_injection =
1982 (vcpu->interrupt_window_open &&
1983 vcpu->irq_summary == 0);
1986 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1990 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
1991 printk("vcpu %d received sipi with vector # %x\n",
1992 vcpu->vcpu_id, vcpu->sipi_vector);
1993 kvm_lapic_reset(vcpu);
1994 kvm_x86_ops->vcpu_reset(vcpu);
1995 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
1999 if (vcpu->guest_debug.enabled)
2000 kvm_x86_ops->guest_debug_pre(vcpu);
2003 r = kvm_mmu_reload(vcpu);
2009 kvm_x86_ops->prepare_guest_switch(vcpu);
2010 kvm_load_guest_fpu(vcpu);
2012 local_irq_disable();
2014 if (signal_pending(current)) {
2018 kvm_run->exit_reason = KVM_EXIT_INTR;
2019 ++vcpu->stat.signal_exits;
2023 if (irqchip_in_kernel(vcpu->kvm))
2024 kvm_x86_ops->inject_pending_irq(vcpu);
2025 else if (!vcpu->mmio_read_completed)
2026 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2028 vcpu->guest_mode = 1;
2032 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2033 kvm_x86_ops->tlb_flush(vcpu);
2035 kvm_x86_ops->run(vcpu, kvm_run);
2037 vcpu->guest_mode = 0;
2043 * We must have an instruction between local_irq_enable() and
2044 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2045 * the interrupt shadow. The stat.exits increment will do nicely.
2046 * But we need to prevent reordering, hence this barrier():
2055 * Profile KVM exit RIPs:
2057 if (unlikely(prof_on == KVM_PROFILING)) {
2058 kvm_x86_ops->cache_regs(vcpu);
2059 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2062 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2065 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2067 kvm_run->exit_reason = KVM_EXIT_INTR;
2068 ++vcpu->stat.request_irq_exits;
2071 if (!need_resched()) {
2072 ++vcpu->stat.light_exits;
2083 post_kvm_run_save(vcpu, kvm_run);
2089 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2096 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2097 kvm_vcpu_block(vcpu);
2102 if (vcpu->sigset_active)
2103 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2105 /* re-sync apic's tpr */
2106 if (!irqchip_in_kernel(vcpu->kvm))
2107 set_cr8(vcpu, kvm_run->cr8);
2109 if (vcpu->pio.cur_count) {
2110 r = complete_pio(vcpu);
2115 if (vcpu->mmio_needed) {
2116 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2117 vcpu->mmio_read_completed = 1;
2118 vcpu->mmio_needed = 0;
2119 r = emulate_instruction(vcpu, kvm_run,
2120 vcpu->mmio_fault_cr2, 0);
2121 if (r == EMULATE_DO_MMIO) {
2123 * Read-modify-write. Back to userspace.
2130 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2131 kvm_x86_ops->cache_regs(vcpu);
2132 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
2133 kvm_x86_ops->decache_regs(vcpu);
2136 r = __vcpu_run(vcpu, kvm_run);
2139 if (vcpu->sigset_active)
2140 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2146 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2147 struct kvm_regs *regs)
2151 kvm_x86_ops->cache_regs(vcpu);
2153 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2154 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2155 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2156 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2157 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2158 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2159 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2160 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
2161 #ifdef CONFIG_X86_64
2162 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2163 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2164 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2165 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2166 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2167 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2168 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2169 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2172 regs->rip = vcpu->rip;
2173 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2176 * Don't leak debug flags in case they were set for guest debugging
2178 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2179 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2186 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2187 struct kvm_regs *regs)
2191 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2192 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2193 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2194 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2195 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2196 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2197 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2198 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
2199 #ifdef CONFIG_X86_64
2200 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2201 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2202 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2203 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2204 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2205 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2206 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2207 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2210 vcpu->rip = regs->rip;
2211 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2213 kvm_x86_ops->decache_regs(vcpu);
2220 static void get_segment(struct kvm_vcpu *vcpu,
2221 struct kvm_segment *var, int seg)
2223 return kvm_x86_ops->get_segment(vcpu, var, seg);
2226 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2227 struct kvm_sregs *sregs)
2229 struct descriptor_table dt;
2234 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2235 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2236 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2237 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2238 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2239 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2241 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2242 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2244 kvm_x86_ops->get_idt(vcpu, &dt);
2245 sregs->idt.limit = dt.limit;
2246 sregs->idt.base = dt.base;
2247 kvm_x86_ops->get_gdt(vcpu, &dt);
2248 sregs->gdt.limit = dt.limit;
2249 sregs->gdt.base = dt.base;
2251 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2252 sregs->cr0 = vcpu->cr0;
2253 sregs->cr2 = vcpu->cr2;
2254 sregs->cr3 = vcpu->cr3;
2255 sregs->cr4 = vcpu->cr4;
2256 sregs->cr8 = get_cr8(vcpu);
2257 sregs->efer = vcpu->shadow_efer;
2258 sregs->apic_base = kvm_get_apic_base(vcpu);
2260 if (irqchip_in_kernel(vcpu->kvm)) {
2261 memset(sregs->interrupt_bitmap, 0,
2262 sizeof sregs->interrupt_bitmap);
2263 pending_vec = kvm_x86_ops->get_irq(vcpu);
2264 if (pending_vec >= 0)
2265 set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
2267 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2268 sizeof sregs->interrupt_bitmap);
2275 static void set_segment(struct kvm_vcpu *vcpu,
2276 struct kvm_segment *var, int seg)
2278 return kvm_x86_ops->set_segment(vcpu, var, seg);
2281 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2282 struct kvm_sregs *sregs)
2284 int mmu_reset_needed = 0;
2285 int i, pending_vec, max_bits;
2286 struct descriptor_table dt;
2290 dt.limit = sregs->idt.limit;
2291 dt.base = sregs->idt.base;
2292 kvm_x86_ops->set_idt(vcpu, &dt);
2293 dt.limit = sregs->gdt.limit;
2294 dt.base = sregs->gdt.base;
2295 kvm_x86_ops->set_gdt(vcpu, &dt);
2297 vcpu->cr2 = sregs->cr2;
2298 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2299 vcpu->cr3 = sregs->cr3;
2301 set_cr8(vcpu, sregs->cr8);
2303 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2304 #ifdef CONFIG_X86_64
2305 kvm_x86_ops->set_efer(vcpu, sregs->efer);
2307 kvm_set_apic_base(vcpu, sregs->apic_base);
2309 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2311 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2312 vcpu->cr0 = sregs->cr0;
2313 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
2315 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2316 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2317 if (!is_long_mode(vcpu) && is_pae(vcpu))
2318 load_pdptrs(vcpu, vcpu->cr3);
2320 if (mmu_reset_needed)
2321 kvm_mmu_reset_context(vcpu);
2323 if (!irqchip_in_kernel(vcpu->kvm)) {
2324 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2325 sizeof vcpu->irq_pending);
2326 vcpu->irq_summary = 0;
2327 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2328 if (vcpu->irq_pending[i])
2329 __set_bit(i, &vcpu->irq_summary);
2331 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2332 pending_vec = find_first_bit(
2333 (const unsigned long *)sregs->interrupt_bitmap,
2335 /* Only pending external irq is handled here */
2336 if (pending_vec < max_bits) {
2337 kvm_x86_ops->set_irq(vcpu, pending_vec);
2338 printk("Set back pending irq %d\n", pending_vec);
2342 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2343 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2344 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2345 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2346 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2347 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2349 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2350 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2357 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2359 struct kvm_segment cs;
2361 get_segment(vcpu, &cs, VCPU_SREG_CS);
2365 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2368 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2369 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2371 * This list is modified at module load time to reflect the
2372 * capabilities of the host cpu.
2374 static u32 msrs_to_save[] = {
2375 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2377 #ifdef CONFIG_X86_64
2378 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2380 MSR_IA32_TIME_STAMP_COUNTER,
2383 static unsigned num_msrs_to_save;
2385 static u32 emulated_msrs[] = {
2386 MSR_IA32_MISC_ENABLE,
2389 static __init void kvm_init_msr_list(void)
2394 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2395 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2398 msrs_to_save[j] = msrs_to_save[i];
2401 num_msrs_to_save = j;
2405 * Adapt set_msr() to msr_io()'s calling convention
2407 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2409 return kvm_set_msr(vcpu, index, *data);
2413 * Read or write a bunch of msrs. All parameters are kernel addresses.
2415 * @return number of msrs set successfully.
2417 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2418 struct kvm_msr_entry *entries,
2419 int (*do_msr)(struct kvm_vcpu *vcpu,
2420 unsigned index, u64 *data))
2426 for (i = 0; i < msrs->nmsrs; ++i)
2427 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2436 * Read or write a bunch of msrs. Parameters are user addresses.
2438 * @return number of msrs set successfully.
2440 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2441 int (*do_msr)(struct kvm_vcpu *vcpu,
2442 unsigned index, u64 *data),
2445 struct kvm_msrs msrs;
2446 struct kvm_msr_entry *entries;
2451 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2455 if (msrs.nmsrs >= MAX_IO_MSRS)
2459 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2460 entries = vmalloc(size);
2465 if (copy_from_user(entries, user_msrs->entries, size))
2468 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2473 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2485 * Translate a guest virtual address to a guest physical address.
2487 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2488 struct kvm_translation *tr)
2490 unsigned long vaddr = tr->linear_address;
2494 mutex_lock(&vcpu->kvm->lock);
2495 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2496 tr->physical_address = gpa;
2497 tr->valid = gpa != UNMAPPED_GVA;
2500 mutex_unlock(&vcpu->kvm->lock);
2506 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2507 struct kvm_interrupt *irq)
2509 if (irq->irq < 0 || irq->irq >= 256)
2511 if (irqchip_in_kernel(vcpu->kvm))
2515 set_bit(irq->irq, vcpu->irq_pending);
2516 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2523 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2524 struct kvm_debug_guest *dbg)
2530 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2537 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2538 unsigned long address,
2541 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2542 unsigned long pgoff;
2545 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2547 page = virt_to_page(vcpu->run);
2548 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2549 page = virt_to_page(vcpu->pio_data);
2551 return NOPAGE_SIGBUS;
2554 *type = VM_FAULT_MINOR;
2559 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2560 .nopage = kvm_vcpu_nopage,
2563 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2565 vma->vm_ops = &kvm_vcpu_vm_ops;
2569 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2571 struct kvm_vcpu *vcpu = filp->private_data;
2573 fput(vcpu->kvm->filp);
2577 static struct file_operations kvm_vcpu_fops = {
2578 .release = kvm_vcpu_release,
2579 .unlocked_ioctl = kvm_vcpu_ioctl,
2580 .compat_ioctl = kvm_vcpu_ioctl,
2581 .mmap = kvm_vcpu_mmap,
2585 * Allocates an inode for the vcpu.
2587 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2590 struct inode *inode;
2593 r = anon_inode_getfd(&fd, &inode, &file,
2594 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2597 atomic_inc(&vcpu->kvm->filp->f_count);
2602 * Creates some virtual cpus. Good luck creating more than one.
2604 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2607 struct kvm_vcpu *vcpu;
2612 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
2614 return PTR_ERR(vcpu);
2616 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2618 /* We do fxsave: this must be aligned. */
2619 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2622 r = kvm_mmu_setup(vcpu);
2627 mutex_lock(&kvm->lock);
2628 if (kvm->vcpus[n]) {
2630 mutex_unlock(&kvm->lock);
2633 kvm->vcpus[n] = vcpu;
2634 mutex_unlock(&kvm->lock);
2636 /* Now it's all set up, let userspace reach it */
2637 r = create_vcpu_fd(vcpu);
2643 mutex_lock(&kvm->lock);
2644 kvm->vcpus[n] = NULL;
2645 mutex_unlock(&kvm->lock);
2649 kvm_mmu_unload(vcpu);
2653 kvm_x86_ops->vcpu_free(vcpu);
2657 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2661 struct kvm_cpuid_entry *e, *entry;
2663 rdmsrl(MSR_EFER, efer);
2665 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2666 e = &vcpu->cpuid_entries[i];
2667 if (e->function == 0x80000001) {
2672 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
2673 entry->edx &= ~(1 << 20);
2674 printk(KERN_INFO "kvm: guest NX capability removed\n");
2678 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2679 struct kvm_cpuid *cpuid,
2680 struct kvm_cpuid_entry __user *entries)
2685 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2688 if (copy_from_user(&vcpu->cpuid_entries, entries,
2689 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2691 vcpu->cpuid_nent = cpuid->nent;
2692 cpuid_fix_nx_cap(vcpu);
2699 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2702 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2703 vcpu->sigset_active = 1;
2704 vcpu->sigset = *sigset;
2706 vcpu->sigset_active = 0;
2711 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2712 * we have asm/x86/processor.h
2723 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2724 #ifdef CONFIG_X86_64
2725 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2727 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2731 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2733 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2737 memcpy(fpu->fpr, fxsave->st_space, 128);
2738 fpu->fcw = fxsave->cwd;
2739 fpu->fsw = fxsave->swd;
2740 fpu->ftwx = fxsave->twd;
2741 fpu->last_opcode = fxsave->fop;
2742 fpu->last_ip = fxsave->rip;
2743 fpu->last_dp = fxsave->rdp;
2744 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2751 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2753 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2757 memcpy(fxsave->st_space, fpu->fpr, 128);
2758 fxsave->cwd = fpu->fcw;
2759 fxsave->swd = fpu->fsw;
2760 fxsave->twd = fpu->ftwx;
2761 fxsave->fop = fpu->last_opcode;
2762 fxsave->rip = fpu->last_ip;
2763 fxsave->rdp = fpu->last_dp;
2764 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2771 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2772 struct kvm_lapic_state *s)
2775 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2781 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2782 struct kvm_lapic_state *s)
2785 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2786 kvm_apic_post_state_restore(vcpu);
2792 static long kvm_vcpu_ioctl(struct file *filp,
2793 unsigned int ioctl, unsigned long arg)
2795 struct kvm_vcpu *vcpu = filp->private_data;
2796 void __user *argp = (void __user *)arg;
2804 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2806 case KVM_GET_REGS: {
2807 struct kvm_regs kvm_regs;
2809 memset(&kvm_regs, 0, sizeof kvm_regs);
2810 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2814 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2819 case KVM_SET_REGS: {
2820 struct kvm_regs kvm_regs;
2823 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2825 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2831 case KVM_GET_SREGS: {
2832 struct kvm_sregs kvm_sregs;
2834 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2835 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2839 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2844 case KVM_SET_SREGS: {
2845 struct kvm_sregs kvm_sregs;
2848 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2850 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2856 case KVM_TRANSLATE: {
2857 struct kvm_translation tr;
2860 if (copy_from_user(&tr, argp, sizeof tr))
2862 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2866 if (copy_to_user(argp, &tr, sizeof tr))
2871 case KVM_INTERRUPT: {
2872 struct kvm_interrupt irq;
2875 if (copy_from_user(&irq, argp, sizeof irq))
2877 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2883 case KVM_DEBUG_GUEST: {
2884 struct kvm_debug_guest dbg;
2887 if (copy_from_user(&dbg, argp, sizeof dbg))
2889 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2896 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2899 r = msr_io(vcpu, argp, do_set_msr, 0);
2901 case KVM_SET_CPUID: {
2902 struct kvm_cpuid __user *cpuid_arg = argp;
2903 struct kvm_cpuid cpuid;
2906 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2908 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2913 case KVM_SET_SIGNAL_MASK: {
2914 struct kvm_signal_mask __user *sigmask_arg = argp;
2915 struct kvm_signal_mask kvm_sigmask;
2916 sigset_t sigset, *p;
2921 if (copy_from_user(&kvm_sigmask, argp,
2922 sizeof kvm_sigmask))
2925 if (kvm_sigmask.len != sizeof sigset)
2928 if (copy_from_user(&sigset, sigmask_arg->sigset,
2933 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2939 memset(&fpu, 0, sizeof fpu);
2940 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2944 if (copy_to_user(argp, &fpu, sizeof fpu))
2953 if (copy_from_user(&fpu, argp, sizeof fpu))
2955 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2961 case KVM_GET_LAPIC: {
2962 struct kvm_lapic_state lapic;
2964 memset(&lapic, 0, sizeof lapic);
2965 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
2969 if (copy_to_user(argp, &lapic, sizeof lapic))
2974 case KVM_SET_LAPIC: {
2975 struct kvm_lapic_state lapic;
2978 if (copy_from_user(&lapic, argp, sizeof lapic))
2980 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
2993 static long kvm_vm_ioctl(struct file *filp,
2994 unsigned int ioctl, unsigned long arg)
2996 struct kvm *kvm = filp->private_data;
2997 void __user *argp = (void __user *)arg;
3001 case KVM_CREATE_VCPU:
3002 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
3006 case KVM_SET_MEMORY_REGION: {
3007 struct kvm_memory_region kvm_mem;
3010 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
3012 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
3017 case KVM_GET_DIRTY_LOG: {
3018 struct kvm_dirty_log log;
3021 if (copy_from_user(&log, argp, sizeof log))
3023 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
3028 case KVM_SET_MEMORY_ALIAS: {
3029 struct kvm_memory_alias alias;
3032 if (copy_from_user(&alias, argp, sizeof alias))
3034 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3039 case KVM_CREATE_IRQCHIP:
3041 kvm->vpic = kvm_create_pic(kvm);
3043 r = kvm_ioapic_init(kvm);
3053 case KVM_IRQ_LINE: {
3054 struct kvm_irq_level irq_event;
3057 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3059 if (irqchip_in_kernel(kvm)) {
3060 mutex_lock(&kvm->lock);
3061 if (irq_event.irq < 16)
3062 kvm_pic_set_irq(pic_irqchip(kvm),
3065 kvm_ioapic_set_irq(kvm->vioapic,
3068 mutex_unlock(&kvm->lock);
3073 case KVM_GET_IRQCHIP: {
3074 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3075 struct kvm_irqchip chip;
3078 if (copy_from_user(&chip, argp, sizeof chip))
3081 if (!irqchip_in_kernel(kvm))
3083 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3087 if (copy_to_user(argp, &chip, sizeof chip))
3092 case KVM_SET_IRQCHIP: {
3093 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3094 struct kvm_irqchip chip;
3097 if (copy_from_user(&chip, argp, sizeof chip))
3100 if (!irqchip_in_kernel(kvm))
3102 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3115 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3116 unsigned long address,
3119 struct kvm *kvm = vma->vm_file->private_data;
3120 unsigned long pgoff;
3123 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
3124 page = gfn_to_page(kvm, pgoff);
3126 return NOPAGE_SIGBUS;
3129 *type = VM_FAULT_MINOR;
3134 static struct vm_operations_struct kvm_vm_vm_ops = {
3135 .nopage = kvm_vm_nopage,
3138 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3140 vma->vm_ops = &kvm_vm_vm_ops;
3144 static struct file_operations kvm_vm_fops = {
3145 .release = kvm_vm_release,
3146 .unlocked_ioctl = kvm_vm_ioctl,
3147 .compat_ioctl = kvm_vm_ioctl,
3148 .mmap = kvm_vm_mmap,
3151 static int kvm_dev_ioctl_create_vm(void)
3154 struct inode *inode;
3158 kvm = kvm_create_vm();
3160 return PTR_ERR(kvm);
3161 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3163 kvm_destroy_vm(kvm);
3172 static long kvm_dev_ioctl(struct file *filp,
3173 unsigned int ioctl, unsigned long arg)
3175 void __user *argp = (void __user *)arg;
3179 case KVM_GET_API_VERSION:
3183 r = KVM_API_VERSION;
3189 r = kvm_dev_ioctl_create_vm();
3191 case KVM_GET_MSR_INDEX_LIST: {
3192 struct kvm_msr_list __user *user_msr_list = argp;
3193 struct kvm_msr_list msr_list;
3197 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3200 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
3201 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3204 if (n < num_msrs_to_save)
3207 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3208 num_msrs_to_save * sizeof(u32)))
3210 if (copy_to_user(user_msr_list->indices
3211 + num_msrs_to_save * sizeof(u32),
3213 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
3218 case KVM_CHECK_EXTENSION: {
3219 int ext = (long)argp;
3222 case KVM_CAP_IRQCHIP:
3232 case KVM_GET_VCPU_MMAP_SIZE:
3245 static struct file_operations kvm_chardev_ops = {
3246 .unlocked_ioctl = kvm_dev_ioctl,
3247 .compat_ioctl = kvm_dev_ioctl,
3250 static struct miscdevice kvm_dev = {
3257 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3260 static void decache_vcpus_on_cpu(int cpu)
3263 struct kvm_vcpu *vcpu;
3266 spin_lock(&kvm_lock);
3267 list_for_each_entry(vm, &vm_list, vm_list)
3268 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3269 vcpu = vm->vcpus[i];
3273 * If the vcpu is locked, then it is running on some
3274 * other cpu and therefore it is not cached on the
3277 * If it's not locked, check the last cpu it executed
3280 if (mutex_trylock(&vcpu->mutex)) {
3281 if (vcpu->cpu == cpu) {
3282 kvm_x86_ops->vcpu_decache(vcpu);
3285 mutex_unlock(&vcpu->mutex);
3288 spin_unlock(&kvm_lock);
3291 static void hardware_enable(void *junk)
3293 int cpu = raw_smp_processor_id();
3295 if (cpu_isset(cpu, cpus_hardware_enabled))
3297 cpu_set(cpu, cpus_hardware_enabled);
3298 kvm_x86_ops->hardware_enable(NULL);
3301 static void hardware_disable(void *junk)
3303 int cpu = raw_smp_processor_id();
3305 if (!cpu_isset(cpu, cpus_hardware_enabled))
3307 cpu_clear(cpu, cpus_hardware_enabled);
3308 decache_vcpus_on_cpu(cpu);
3309 kvm_x86_ops->hardware_disable(NULL);
3312 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3319 case CPU_DYING_FROZEN:
3320 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3322 hardware_disable(NULL);
3324 case CPU_UP_CANCELED:
3325 case CPU_UP_CANCELED_FROZEN:
3326 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3328 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
3331 case CPU_ONLINE_FROZEN:
3332 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3334 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
3340 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3343 if (val == SYS_RESTART) {
3345 * Some (well, at least mine) BIOSes hang on reboot if
3348 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3349 on_each_cpu(hardware_disable, NULL, 0, 1);
3354 static struct notifier_block kvm_reboot_notifier = {
3355 .notifier_call = kvm_reboot,
3359 void kvm_io_bus_init(struct kvm_io_bus *bus)
3361 memset(bus, 0, sizeof(*bus));
3364 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3368 for (i = 0; i < bus->dev_count; i++) {
3369 struct kvm_io_device *pos = bus->devs[i];
3371 kvm_iodevice_destructor(pos);
3375 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3379 for (i = 0; i < bus->dev_count; i++) {
3380 struct kvm_io_device *pos = bus->devs[i];
3382 if (pos->in_range(pos, addr))
3389 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3391 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3393 bus->devs[bus->dev_count++] = dev;
3396 static struct notifier_block kvm_cpu_notifier = {
3397 .notifier_call = kvm_cpu_hotplug,
3398 .priority = 20, /* must be > scheduler priority */
3401 static u64 stat_get(void *_offset)
3403 unsigned offset = (long)_offset;
3406 struct kvm_vcpu *vcpu;
3409 spin_lock(&kvm_lock);
3410 list_for_each_entry(kvm, &vm_list, vm_list)
3411 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3412 vcpu = kvm->vcpus[i];
3414 total += *(u32 *)((void *)vcpu + offset);
3416 spin_unlock(&kvm_lock);
3420 DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
3422 static __init void kvm_init_debug(void)
3424 struct kvm_stats_debugfs_item *p;
3426 debugfs_dir = debugfs_create_dir("kvm", NULL);
3427 for (p = debugfs_entries; p->name; ++p)
3428 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3429 (void *)(long)p->offset,
3433 static void kvm_exit_debug(void)
3435 struct kvm_stats_debugfs_item *p;
3437 for (p = debugfs_entries; p->name; ++p)
3438 debugfs_remove(p->dentry);
3439 debugfs_remove(debugfs_dir);
3442 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3444 hardware_disable(NULL);
3448 static int kvm_resume(struct sys_device *dev)
3450 hardware_enable(NULL);
3454 static struct sysdev_class kvm_sysdev_class = {
3455 set_kset_name("kvm"),
3456 .suspend = kvm_suspend,
3457 .resume = kvm_resume,
3460 static struct sys_device kvm_sysdev = {
3462 .cls = &kvm_sysdev_class,
3465 hpa_t bad_page_address;
3468 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3470 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3473 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3475 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3477 kvm_x86_ops->vcpu_load(vcpu, cpu);
3480 static void kvm_sched_out(struct preempt_notifier *pn,
3481 struct task_struct *next)
3483 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3485 kvm_x86_ops->vcpu_put(vcpu);
3488 int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
3489 struct module *module)
3495 printk(KERN_ERR "kvm: already loaded the other module\n");
3499 if (!ops->cpu_has_kvm_support()) {
3500 printk(KERN_ERR "kvm: no hardware support\n");
3503 if (ops->disabled_by_bios()) {
3504 printk(KERN_ERR "kvm: disabled by bios\n");
3510 r = kvm_x86_ops->hardware_setup();
3514 for_each_online_cpu(cpu) {
3515 smp_call_function_single(cpu,
3516 kvm_x86_ops->check_processor_compatibility,
3522 on_each_cpu(hardware_enable, NULL, 0, 1);
3523 r = register_cpu_notifier(&kvm_cpu_notifier);
3526 register_reboot_notifier(&kvm_reboot_notifier);
3528 r = sysdev_class_register(&kvm_sysdev_class);
3532 r = sysdev_register(&kvm_sysdev);
3536 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3537 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3538 __alignof__(struct kvm_vcpu), 0, 0);
3539 if (!kvm_vcpu_cache) {
3544 kvm_chardev_ops.owner = module;
3546 r = misc_register(&kvm_dev);
3548 printk (KERN_ERR "kvm: misc device register failed\n");
3552 kvm_preempt_ops.sched_in = kvm_sched_in;
3553 kvm_preempt_ops.sched_out = kvm_sched_out;
3558 kmem_cache_destroy(kvm_vcpu_cache);
3560 sysdev_unregister(&kvm_sysdev);
3562 sysdev_class_unregister(&kvm_sysdev_class);
3564 unregister_reboot_notifier(&kvm_reboot_notifier);
3565 unregister_cpu_notifier(&kvm_cpu_notifier);
3567 on_each_cpu(hardware_disable, NULL, 0, 1);
3569 kvm_x86_ops->hardware_unsetup();
3575 void kvm_exit_x86(void)
3577 misc_deregister(&kvm_dev);
3578 kmem_cache_destroy(kvm_vcpu_cache);
3579 sysdev_unregister(&kvm_sysdev);
3580 sysdev_class_unregister(&kvm_sysdev_class);
3581 unregister_reboot_notifier(&kvm_reboot_notifier);
3582 unregister_cpu_notifier(&kvm_cpu_notifier);
3583 on_each_cpu(hardware_disable, NULL, 0, 1);
3584 kvm_x86_ops->hardware_unsetup();
3588 static __init int kvm_init(void)
3590 static struct page *bad_page;
3593 r = kvm_mmu_module_init();
3599 kvm_init_msr_list();
3601 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3606 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3607 memset(__va(bad_page_address), 0, PAGE_SIZE);
3613 kvm_mmu_module_exit();
3618 static __exit void kvm_exit(void)
3621 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3622 kvm_mmu_module_exit();
3625 module_init(kvm_init)
3626 module_exit(kvm_exit)
3628 EXPORT_SYMBOL_GPL(kvm_init_x86);
3629 EXPORT_SYMBOL_GPL(kvm_exit_x86);