2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
20 #include "x86_emulate.h"
21 #include "segment_descriptor.h"
24 #include <linux/kvm.h>
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/percpu.h>
28 #include <linux/gfp.h>
30 #include <linux/miscdevice.h>
31 #include <linux/vmalloc.h>
32 #include <linux/reboot.h>
33 #include <linux/debugfs.h>
34 #include <linux/highmem.h>
35 #include <linux/file.h>
36 #include <linux/sysdev.h>
37 #include <linux/cpu.h>
38 #include <linux/sched.h>
39 #include <linux/cpumask.h>
40 #include <linux/smp.h>
41 #include <linux/anon_inodes.h>
42 #include <linux/profile.h>
43 #include <linux/kvm_para.h>
44 #include <linux/pagemap.h>
46 #include <asm/processor.h>
49 #include <asm/uaccess.h>
52 MODULE_AUTHOR("Qumranet");
53 MODULE_LICENSE("GPL");
55 static DEFINE_SPINLOCK(kvm_lock);
56 static LIST_HEAD(vm_list);
58 static cpumask_t cpus_hardware_enabled;
60 struct kvm_x86_ops *kvm_x86_ops;
61 struct kmem_cache *kvm_vcpu_cache;
62 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
64 static __read_mostly struct preempt_ops kvm_preempt_ops;
66 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
68 static struct kvm_stats_debugfs_item {
71 struct dentry *dentry;
72 } debugfs_entries[] = {
73 { "pf_fixed", STAT_OFFSET(pf_fixed) },
74 { "pf_guest", STAT_OFFSET(pf_guest) },
75 { "tlb_flush", STAT_OFFSET(tlb_flush) },
76 { "invlpg", STAT_OFFSET(invlpg) },
77 { "exits", STAT_OFFSET(exits) },
78 { "io_exits", STAT_OFFSET(io_exits) },
79 { "mmio_exits", STAT_OFFSET(mmio_exits) },
80 { "signal_exits", STAT_OFFSET(signal_exits) },
81 { "irq_window", STAT_OFFSET(irq_window_exits) },
82 { "halt_exits", STAT_OFFSET(halt_exits) },
83 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
84 { "request_irq", STAT_OFFSET(request_irq_exits) },
85 { "irq_exits", STAT_OFFSET(irq_exits) },
86 { "light_exits", STAT_OFFSET(light_exits) },
87 { "efer_reload", STAT_OFFSET(efer_reload) },
91 static struct dentry *debugfs_dir;
93 #define CR0_RESERVED_BITS \
94 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
95 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
96 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
97 #define CR4_RESERVED_BITS \
98 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
99 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
100 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
101 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
103 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
104 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
107 /* LDT or TSS descriptor in the GDT. 16 bytes. */
108 struct segment_descriptor_64 {
109 struct segment_descriptor s;
116 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
119 unsigned long segment_base(u16 selector)
121 struct descriptor_table gdt;
122 struct segment_descriptor *d;
123 unsigned long table_base;
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 | ((unsigned long)d->base_mid << 16) |
140 ((unsigned long)d->base_high << 24);
142 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
143 v |= ((unsigned long) \
144 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
148 EXPORT_SYMBOL_GPL(segment_base);
150 static inline int valid_vcpu(int n)
152 return likely(n >= 0 && n < KVM_MAX_VCPUS);
155 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
157 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
160 vcpu->guest_fpu_loaded = 1;
161 fx_save(&vcpu->host_fx_image);
162 fx_restore(&vcpu->guest_fx_image);
164 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
166 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
168 if (!vcpu->guest_fpu_loaded)
171 vcpu->guest_fpu_loaded = 0;
172 fx_save(&vcpu->guest_fx_image);
173 fx_restore(&vcpu->host_fx_image);
175 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
178 * Switches to specified vcpu, until a matching vcpu_put()
180 void vcpu_load(struct kvm_vcpu *vcpu)
184 mutex_lock(&vcpu->mutex);
186 preempt_notifier_register(&vcpu->preempt_notifier);
187 kvm_arch_vcpu_load(vcpu, cpu);
191 void vcpu_put(struct kvm_vcpu *vcpu)
194 kvm_arch_vcpu_put(vcpu);
195 preempt_notifier_unregister(&vcpu->preempt_notifier);
197 mutex_unlock(&vcpu->mutex);
200 static void ack_flush(void *_completed)
204 void kvm_flush_remote_tlbs(struct kvm *kvm)
208 struct kvm_vcpu *vcpu;
211 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
212 vcpu = kvm->vcpus[i];
215 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
218 if (cpu != -1 && cpu != raw_smp_processor_id())
221 smp_call_function_mask(cpus, ack_flush, NULL, 1);
224 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
229 mutex_init(&vcpu->mutex);
231 vcpu->mmu.root_hpa = INVALID_PAGE;
234 if (!irqchip_in_kernel(kvm) || id == 0)
235 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
237 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
238 init_waitqueue_head(&vcpu->wq);
240 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
245 vcpu->run = page_address(page);
247 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
252 vcpu->pio_data = page_address(page);
254 r = kvm_mmu_create(vcpu);
256 goto fail_free_pio_data;
258 if (irqchip_in_kernel(kvm)) {
259 r = kvm_create_lapic(vcpu);
261 goto fail_mmu_destroy;
267 kvm_mmu_destroy(vcpu);
269 free_page((unsigned long)vcpu->pio_data);
271 free_page((unsigned long)vcpu->run);
275 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
277 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
279 kvm_free_lapic(vcpu);
280 kvm_mmu_destroy(vcpu);
281 free_page((unsigned long)vcpu->pio_data);
282 free_page((unsigned long)vcpu->run);
284 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
286 static struct kvm *kvm_create_vm(void)
288 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
291 return ERR_PTR(-ENOMEM);
293 kvm_io_bus_init(&kvm->pio_bus);
294 mutex_init(&kvm->lock);
295 INIT_LIST_HEAD(&kvm->active_mmu_pages);
296 kvm_io_bus_init(&kvm->mmio_bus);
297 spin_lock(&kvm_lock);
298 list_add(&kvm->vm_list, &vm_list);
299 spin_unlock(&kvm_lock);
303 static void kvm_free_userspace_physmem(struct kvm_memory_slot *free)
307 for (i = 0; i < free->npages; ++i) {
308 if (free->phys_mem[i]) {
309 if (!PageReserved(free->phys_mem[i]))
310 SetPageDirty(free->phys_mem[i]);
311 page_cache_release(free->phys_mem[i]);
316 static void kvm_free_kernel_physmem(struct kvm_memory_slot *free)
320 for (i = 0; i < free->npages; ++i)
321 if (free->phys_mem[i])
322 __free_page(free->phys_mem[i]);
326 * Free any memory in @free but not in @dont.
328 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
329 struct kvm_memory_slot *dont)
331 if (!dont || free->phys_mem != dont->phys_mem)
332 if (free->phys_mem) {
333 if (free->user_alloc)
334 kvm_free_userspace_physmem(free);
336 kvm_free_kernel_physmem(free);
337 vfree(free->phys_mem);
339 if (!dont || free->rmap != dont->rmap)
342 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
343 vfree(free->dirty_bitmap);
345 free->phys_mem = NULL;
347 free->dirty_bitmap = NULL;
350 static void kvm_free_physmem(struct kvm *kvm)
354 for (i = 0; i < kvm->nmemslots; ++i)
355 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
358 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
362 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
363 if (vcpu->pio.guest_pages[i]) {
364 __free_page(vcpu->pio.guest_pages[i]);
365 vcpu->pio.guest_pages[i] = NULL;
369 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
372 kvm_mmu_unload(vcpu);
376 static void kvm_free_vcpus(struct kvm *kvm)
381 * Unpin any mmu pages first.
383 for (i = 0; i < KVM_MAX_VCPUS; ++i)
385 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
386 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
388 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
389 kvm->vcpus[i] = NULL;
395 static void kvm_destroy_vm(struct kvm *kvm)
397 spin_lock(&kvm_lock);
398 list_del(&kvm->vm_list);
399 spin_unlock(&kvm_lock);
400 kvm_io_bus_destroy(&kvm->pio_bus);
401 kvm_io_bus_destroy(&kvm->mmio_bus);
405 kvm_free_physmem(kvm);
409 static int kvm_vm_release(struct inode *inode, struct file *filp)
411 struct kvm *kvm = filp->private_data;
417 static void inject_gp(struct kvm_vcpu *vcpu)
419 kvm_x86_ops->inject_gp(vcpu, 0);
423 * Load the pae pdptrs. Return true is they are all valid.
425 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
427 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
428 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
431 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
433 mutex_lock(&vcpu->kvm->lock);
434 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
435 offset * sizeof(u64), sizeof(pdpte));
440 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
441 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
448 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
450 mutex_unlock(&vcpu->kvm->lock);
455 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
457 if (cr0 & CR0_RESERVED_BITS) {
458 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
464 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
465 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
470 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
471 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
472 "and a clear PE flag\n");
477 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
479 if ((vcpu->shadow_efer & EFER_LME)) {
483 printk(KERN_DEBUG "set_cr0: #GP, start paging "
484 "in long mode while PAE is disabled\n");
488 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
490 printk(KERN_DEBUG "set_cr0: #GP, start paging "
491 "in long mode while CS.L == 1\n");
498 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
499 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
507 kvm_x86_ops->set_cr0(vcpu, cr0);
510 mutex_lock(&vcpu->kvm->lock);
511 kvm_mmu_reset_context(vcpu);
512 mutex_unlock(&vcpu->kvm->lock);
515 EXPORT_SYMBOL_GPL(set_cr0);
517 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
519 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
521 EXPORT_SYMBOL_GPL(lmsw);
523 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
525 if (cr4 & CR4_RESERVED_BITS) {
526 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
531 if (is_long_mode(vcpu)) {
532 if (!(cr4 & X86_CR4_PAE)) {
533 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
538 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
539 && !load_pdptrs(vcpu, vcpu->cr3)) {
540 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
545 if (cr4 & X86_CR4_VMXE) {
546 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
550 kvm_x86_ops->set_cr4(vcpu, cr4);
552 mutex_lock(&vcpu->kvm->lock);
553 kvm_mmu_reset_context(vcpu);
554 mutex_unlock(&vcpu->kvm->lock);
556 EXPORT_SYMBOL_GPL(set_cr4);
558 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
560 if (is_long_mode(vcpu)) {
561 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
562 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
568 if (cr3 & CR3_PAE_RESERVED_BITS) {
570 "set_cr3: #GP, reserved bits\n");
574 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
575 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
582 * We don't check reserved bits in nonpae mode, because
583 * this isn't enforced, and VMware depends on this.
587 mutex_lock(&vcpu->kvm->lock);
589 * Does the new cr3 value map to physical memory? (Note, we
590 * catch an invalid cr3 even in real-mode, because it would
591 * cause trouble later on when we turn on paging anyway.)
593 * A real CPU would silently accept an invalid cr3 and would
594 * attempt to use it - with largely undefined (and often hard
595 * to debug) behavior on the guest side.
597 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
601 vcpu->mmu.new_cr3(vcpu);
603 mutex_unlock(&vcpu->kvm->lock);
605 EXPORT_SYMBOL_GPL(set_cr3);
607 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
609 if (cr8 & CR8_RESERVED_BITS) {
610 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
614 if (irqchip_in_kernel(vcpu->kvm))
615 kvm_lapic_set_tpr(vcpu, cr8);
619 EXPORT_SYMBOL_GPL(set_cr8);
621 unsigned long get_cr8(struct kvm_vcpu *vcpu)
623 if (irqchip_in_kernel(vcpu->kvm))
624 return kvm_lapic_get_cr8(vcpu);
628 EXPORT_SYMBOL_GPL(get_cr8);
630 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
632 if (irqchip_in_kernel(vcpu->kvm))
633 return vcpu->apic_base;
635 return vcpu->apic_base;
637 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
639 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
641 /* TODO: reserve bits check */
642 if (irqchip_in_kernel(vcpu->kvm))
643 kvm_lapic_set_base(vcpu, data);
645 vcpu->apic_base = data;
647 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
649 void fx_init(struct kvm_vcpu *vcpu)
651 unsigned after_mxcsr_mask;
653 /* Initialize guest FPU by resetting ours and saving into guest's */
655 fx_save(&vcpu->host_fx_image);
657 fx_save(&vcpu->guest_fx_image);
658 fx_restore(&vcpu->host_fx_image);
661 vcpu->cr0 |= X86_CR0_ET;
662 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
663 vcpu->guest_fx_image.mxcsr = 0x1f80;
664 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
665 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
667 EXPORT_SYMBOL_GPL(fx_init);
670 * Allocate some memory and give it an address in the guest physical address
673 * Discontiguous memory is allowed, mostly for framebuffers.
675 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
677 kvm_userspace_memory_region *mem,
682 unsigned long npages;
684 struct kvm_memory_slot *memslot;
685 struct kvm_memory_slot old, new;
688 /* General sanity checks */
689 if (mem->memory_size & (PAGE_SIZE - 1))
691 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
693 if (mem->slot >= KVM_MEMORY_SLOTS)
695 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
698 memslot = &kvm->memslots[mem->slot];
699 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
700 npages = mem->memory_size >> PAGE_SHIFT;
703 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
705 mutex_lock(&kvm->lock);
707 new = old = *memslot;
709 new.base_gfn = base_gfn;
711 new.flags = mem->flags;
713 /* Disallow changing a memory slot's size. */
715 if (npages && old.npages && npages != old.npages)
718 /* Check for overlaps */
720 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
721 struct kvm_memory_slot *s = &kvm->memslots[i];
725 if (!((base_gfn + npages <= s->base_gfn) ||
726 (base_gfn >= s->base_gfn + s->npages)))
730 /* Deallocate if slot is being removed */
734 /* Free page dirty bitmap if unneeded */
735 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
736 new.dirty_bitmap = NULL;
740 /* Allocate if a slot is being created */
741 if (npages && !new.phys_mem) {
742 new.phys_mem = vmalloc(npages * sizeof(struct page *));
747 new.rmap = vmalloc(npages * sizeof(struct page *));
752 memset(new.phys_mem, 0, npages * sizeof(struct page *));
753 memset(new.rmap, 0, npages * sizeof(*new.rmap));
755 unsigned long pages_num;
758 down_read(¤t->mm->mmap_sem);
760 pages_num = get_user_pages(current, current->mm,
762 npages, 1, 1, new.phys_mem,
765 up_read(¤t->mm->mmap_sem);
766 if (pages_num != npages)
769 for (i = 0; i < npages; ++i) {
770 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
772 if (!new.phys_mem[i])
778 /* Allocate page dirty bitmap if needed */
779 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
780 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
782 new.dirty_bitmap = vmalloc(dirty_bytes);
783 if (!new.dirty_bitmap)
785 memset(new.dirty_bitmap, 0, dirty_bytes);
788 if (mem->slot >= kvm->nmemslots)
789 kvm->nmemslots = mem->slot + 1;
791 if (!kvm->n_requested_mmu_pages) {
792 unsigned int n_pages;
795 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
796 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
799 unsigned int nr_mmu_pages;
801 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
802 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
803 nr_mmu_pages = max(nr_mmu_pages,
804 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
805 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
811 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
812 kvm_flush_remote_tlbs(kvm);
814 mutex_unlock(&kvm->lock);
816 kvm_free_physmem_slot(&old, &new);
820 mutex_unlock(&kvm->lock);
821 kvm_free_physmem_slot(&new, &old);
826 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
827 u32 kvm_nr_mmu_pages)
829 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
832 mutex_lock(&kvm->lock);
834 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
835 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
837 mutex_unlock(&kvm->lock);
841 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
843 return kvm->n_alloc_mmu_pages;
847 * Get (and clear) the dirty memory log for a memory slot.
849 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
850 struct kvm_dirty_log *log)
852 struct kvm_memory_slot *memslot;
855 unsigned long any = 0;
857 mutex_lock(&kvm->lock);
860 if (log->slot >= KVM_MEMORY_SLOTS)
863 memslot = &kvm->memslots[log->slot];
865 if (!memslot->dirty_bitmap)
868 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
870 for (i = 0; !any && i < n/sizeof(long); ++i)
871 any = memslot->dirty_bitmap[i];
874 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
877 /* If nothing is dirty, don't bother messing with page tables. */
879 kvm_mmu_slot_remove_write_access(kvm, log->slot);
880 kvm_flush_remote_tlbs(kvm);
881 memset(memslot->dirty_bitmap, 0, n);
887 mutex_unlock(&kvm->lock);
892 * Set a new alias region. Aliases map a portion of physical memory into
893 * another portion. This is useful for memory windows, for example the PC
896 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
897 struct kvm_memory_alias *alias)
900 struct kvm_mem_alias *p;
903 /* General sanity checks */
904 if (alias->memory_size & (PAGE_SIZE - 1))
906 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
908 if (alias->slot >= KVM_ALIAS_SLOTS)
910 if (alias->guest_phys_addr + alias->memory_size
911 < alias->guest_phys_addr)
913 if (alias->target_phys_addr + alias->memory_size
914 < alias->target_phys_addr)
917 mutex_lock(&kvm->lock);
919 p = &kvm->aliases[alias->slot];
920 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
921 p->npages = alias->memory_size >> PAGE_SHIFT;
922 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
924 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
925 if (kvm->aliases[n - 1].npages)
929 kvm_mmu_zap_all(kvm);
931 mutex_unlock(&kvm->lock);
939 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
944 switch (chip->chip_id) {
945 case KVM_IRQCHIP_PIC_MASTER:
946 memcpy(&chip->chip.pic,
947 &pic_irqchip(kvm)->pics[0],
948 sizeof(struct kvm_pic_state));
950 case KVM_IRQCHIP_PIC_SLAVE:
951 memcpy(&chip->chip.pic,
952 &pic_irqchip(kvm)->pics[1],
953 sizeof(struct kvm_pic_state));
955 case KVM_IRQCHIP_IOAPIC:
956 memcpy(&chip->chip.ioapic,
958 sizeof(struct kvm_ioapic_state));
967 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
972 switch (chip->chip_id) {
973 case KVM_IRQCHIP_PIC_MASTER:
974 memcpy(&pic_irqchip(kvm)->pics[0],
976 sizeof(struct kvm_pic_state));
978 case KVM_IRQCHIP_PIC_SLAVE:
979 memcpy(&pic_irqchip(kvm)->pics[1],
981 sizeof(struct kvm_pic_state));
983 case KVM_IRQCHIP_IOAPIC:
984 memcpy(ioapic_irqchip(kvm),
986 sizeof(struct kvm_ioapic_state));
992 kvm_pic_update_irq(pic_irqchip(kvm));
996 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
999 struct kvm_mem_alias *alias;
1001 for (i = 0; i < kvm->naliases; ++i) {
1002 alias = &kvm->aliases[i];
1003 if (gfn >= alias->base_gfn
1004 && gfn < alias->base_gfn + alias->npages)
1005 return alias->target_gfn + gfn - alias->base_gfn;
1010 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1014 for (i = 0; i < kvm->nmemslots; ++i) {
1015 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1017 if (gfn >= memslot->base_gfn
1018 && gfn < memslot->base_gfn + memslot->npages)
1024 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1026 gfn = unalias_gfn(kvm, gfn);
1027 return __gfn_to_memslot(kvm, gfn);
1030 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1032 struct kvm_memory_slot *slot;
1034 gfn = unalias_gfn(kvm, gfn);
1035 slot = __gfn_to_memslot(kvm, gfn);
1038 return slot->phys_mem[gfn - slot->base_gfn];
1040 EXPORT_SYMBOL_GPL(gfn_to_page);
1042 static int next_segment(unsigned long len, int offset)
1044 if (len > PAGE_SIZE - offset)
1045 return PAGE_SIZE - offset;
1050 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1056 page = gfn_to_page(kvm, gfn);
1059 page_virt = kmap_atomic(page, KM_USER0);
1061 memcpy(data, page_virt + offset, len);
1063 kunmap_atomic(page_virt, KM_USER0);
1066 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1068 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1070 gfn_t gfn = gpa >> PAGE_SHIFT;
1072 int offset = offset_in_page(gpa);
1075 while ((seg = next_segment(len, offset)) != 0) {
1076 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1086 EXPORT_SYMBOL_GPL(kvm_read_guest);
1088 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1089 int offset, int len)
1094 page = gfn_to_page(kvm, gfn);
1097 page_virt = kmap_atomic(page, KM_USER0);
1099 memcpy(page_virt + offset, data, len);
1101 kunmap_atomic(page_virt, KM_USER0);
1102 mark_page_dirty(kvm, gfn);
1105 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1107 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1110 gfn_t gfn = gpa >> PAGE_SHIFT;
1112 int offset = offset_in_page(gpa);
1115 while ((seg = next_segment(len, offset)) != 0) {
1116 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1127 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1132 page = gfn_to_page(kvm, gfn);
1135 page_virt = kmap_atomic(page, KM_USER0);
1137 memset(page_virt + offset, 0, len);
1139 kunmap_atomic(page_virt, KM_USER0);
1142 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1144 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1146 gfn_t gfn = gpa >> PAGE_SHIFT;
1148 int offset = offset_in_page(gpa);
1151 while ((seg = next_segment(len, offset)) != 0) {
1152 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1161 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1163 /* WARNING: Does not work on aliased pages. */
1164 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1166 struct kvm_memory_slot *memslot;
1168 memslot = __gfn_to_memslot(kvm, gfn);
1169 if (memslot && memslot->dirty_bitmap) {
1170 unsigned long rel_gfn = gfn - memslot->base_gfn;
1173 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1174 set_bit(rel_gfn, memslot->dirty_bitmap);
1178 int emulator_read_std(unsigned long addr,
1181 struct kvm_vcpu *vcpu)
1186 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1187 unsigned offset = addr & (PAGE_SIZE-1);
1188 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1191 if (gpa == UNMAPPED_GVA)
1192 return X86EMUL_PROPAGATE_FAULT;
1193 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1195 return X86EMUL_UNHANDLEABLE;
1202 return X86EMUL_CONTINUE;
1204 EXPORT_SYMBOL_GPL(emulator_read_std);
1206 static int emulator_write_std(unsigned long addr,
1209 struct kvm_vcpu *vcpu)
1211 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
1212 return X86EMUL_UNHANDLEABLE;
1216 * Only apic need an MMIO device hook, so shortcut now..
1218 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1221 struct kvm_io_device *dev;
1224 dev = &vcpu->apic->dev;
1225 if (dev->in_range(dev, addr))
1231 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1234 struct kvm_io_device *dev;
1236 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1238 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1242 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1245 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1248 static int emulator_read_emulated(unsigned long addr,
1251 struct kvm_vcpu *vcpu)
1253 struct kvm_io_device *mmio_dev;
1256 if (vcpu->mmio_read_completed) {
1257 memcpy(val, vcpu->mmio_data, bytes);
1258 vcpu->mmio_read_completed = 0;
1259 return X86EMUL_CONTINUE;
1260 } else if (emulator_read_std(addr, val, bytes, vcpu)
1261 == X86EMUL_CONTINUE)
1262 return X86EMUL_CONTINUE;
1264 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1265 if (gpa == UNMAPPED_GVA)
1266 return X86EMUL_PROPAGATE_FAULT;
1269 * Is this MMIO handled locally?
1271 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1273 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1274 return X86EMUL_CONTINUE;
1277 vcpu->mmio_needed = 1;
1278 vcpu->mmio_phys_addr = gpa;
1279 vcpu->mmio_size = bytes;
1280 vcpu->mmio_is_write = 0;
1282 return X86EMUL_UNHANDLEABLE;
1285 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1286 const void *val, int bytes)
1290 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1293 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1297 static int emulator_write_emulated_onepage(unsigned long addr,
1300 struct kvm_vcpu *vcpu)
1302 struct kvm_io_device *mmio_dev;
1303 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1305 if (gpa == UNMAPPED_GVA) {
1306 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
1307 return X86EMUL_PROPAGATE_FAULT;
1310 if (emulator_write_phys(vcpu, gpa, val, bytes))
1311 return X86EMUL_CONTINUE;
1314 * Is this MMIO handled locally?
1316 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1318 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1319 return X86EMUL_CONTINUE;
1322 vcpu->mmio_needed = 1;
1323 vcpu->mmio_phys_addr = gpa;
1324 vcpu->mmio_size = bytes;
1325 vcpu->mmio_is_write = 1;
1326 memcpy(vcpu->mmio_data, val, bytes);
1328 return X86EMUL_CONTINUE;
1331 int emulator_write_emulated(unsigned long addr,
1334 struct kvm_vcpu *vcpu)
1336 /* Crossing a page boundary? */
1337 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1340 now = -addr & ~PAGE_MASK;
1341 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1342 if (rc != X86EMUL_CONTINUE)
1348 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1350 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1352 static int emulator_cmpxchg_emulated(unsigned long addr,
1356 struct kvm_vcpu *vcpu)
1358 static int reported;
1362 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1364 return emulator_write_emulated(addr, new, bytes, vcpu);
1367 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1369 return kvm_x86_ops->get_segment_base(vcpu, seg);
1372 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1374 return X86EMUL_CONTINUE;
1377 int emulate_clts(struct kvm_vcpu *vcpu)
1379 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
1380 return X86EMUL_CONTINUE;
1383 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1385 struct kvm_vcpu *vcpu = ctxt->vcpu;
1389 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1390 return X86EMUL_CONTINUE;
1392 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1393 return X86EMUL_UNHANDLEABLE;
1397 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1399 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1402 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1404 /* FIXME: better handling */
1405 return X86EMUL_UNHANDLEABLE;
1407 return X86EMUL_CONTINUE;
1410 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1412 static int reported;
1414 unsigned long rip = vcpu->rip;
1415 unsigned long rip_linear;
1417 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1422 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1424 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1425 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1428 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1430 struct x86_emulate_ops emulate_ops = {
1431 .read_std = emulator_read_std,
1432 .write_std = emulator_write_std,
1433 .read_emulated = emulator_read_emulated,
1434 .write_emulated = emulator_write_emulated,
1435 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1438 int emulate_instruction(struct kvm_vcpu *vcpu,
1439 struct kvm_run *run,
1446 vcpu->mmio_fault_cr2 = cr2;
1447 kvm_x86_ops->cache_regs(vcpu);
1449 vcpu->mmio_is_write = 0;
1450 vcpu->pio.string = 0;
1454 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1456 vcpu->emulate_ctxt.vcpu = vcpu;
1457 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1458 vcpu->emulate_ctxt.cr2 = cr2;
1459 vcpu->emulate_ctxt.mode =
1460 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1461 ? X86EMUL_MODE_REAL : cs_l
1462 ? X86EMUL_MODE_PROT64 : cs_db
1463 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1465 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1466 vcpu->emulate_ctxt.cs_base = 0;
1467 vcpu->emulate_ctxt.ds_base = 0;
1468 vcpu->emulate_ctxt.es_base = 0;
1469 vcpu->emulate_ctxt.ss_base = 0;
1471 vcpu->emulate_ctxt.cs_base =
1472 get_segment_base(vcpu, VCPU_SREG_CS);
1473 vcpu->emulate_ctxt.ds_base =
1474 get_segment_base(vcpu, VCPU_SREG_DS);
1475 vcpu->emulate_ctxt.es_base =
1476 get_segment_base(vcpu, VCPU_SREG_ES);
1477 vcpu->emulate_ctxt.ss_base =
1478 get_segment_base(vcpu, VCPU_SREG_SS);
1481 vcpu->emulate_ctxt.gs_base =
1482 get_segment_base(vcpu, VCPU_SREG_GS);
1483 vcpu->emulate_ctxt.fs_base =
1484 get_segment_base(vcpu, VCPU_SREG_FS);
1486 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
1488 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1489 return EMULATE_DONE;
1490 return EMULATE_FAIL;
1494 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
1496 if (vcpu->pio.string)
1497 return EMULATE_DO_MMIO;
1499 if ((r || vcpu->mmio_is_write) && run) {
1500 run->exit_reason = KVM_EXIT_MMIO;
1501 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1502 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1503 run->mmio.len = vcpu->mmio_size;
1504 run->mmio.is_write = vcpu->mmio_is_write;
1508 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1509 return EMULATE_DONE;
1510 if (!vcpu->mmio_needed) {
1511 kvm_report_emulation_failure(vcpu, "mmio");
1512 return EMULATE_FAIL;
1514 return EMULATE_DO_MMIO;
1517 kvm_x86_ops->decache_regs(vcpu);
1518 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
1520 if (vcpu->mmio_is_write) {
1521 vcpu->mmio_needed = 0;
1522 return EMULATE_DO_MMIO;
1525 return EMULATE_DONE;
1527 EXPORT_SYMBOL_GPL(emulate_instruction);
1530 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1532 static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1534 DECLARE_WAITQUEUE(wait, current);
1536 add_wait_queue(&vcpu->wq, &wait);
1539 * We will block until either an interrupt or a signal wakes us up
1541 while (!kvm_cpu_has_interrupt(vcpu)
1542 && !signal_pending(current)
1543 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1544 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
1545 set_current_state(TASK_INTERRUPTIBLE);
1551 __set_current_state(TASK_RUNNING);
1552 remove_wait_queue(&vcpu->wq, &wait);
1555 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1557 ++vcpu->stat.halt_exits;
1558 if (irqchip_in_kernel(vcpu->kvm)) {
1559 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1560 kvm_vcpu_block(vcpu);
1561 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1565 vcpu->run->exit_reason = KVM_EXIT_HLT;
1569 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1571 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
1573 unsigned long nr, a0, a1, a2, a3, ret;
1575 kvm_x86_ops->cache_regs(vcpu);
1577 nr = vcpu->regs[VCPU_REGS_RAX];
1578 a0 = vcpu->regs[VCPU_REGS_RBX];
1579 a1 = vcpu->regs[VCPU_REGS_RCX];
1580 a2 = vcpu->regs[VCPU_REGS_RDX];
1581 a3 = vcpu->regs[VCPU_REGS_RSI];
1583 if (!is_long_mode(vcpu)) {
1596 vcpu->regs[VCPU_REGS_RAX] = ret;
1597 kvm_x86_ops->decache_regs(vcpu);
1600 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1602 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1604 char instruction[3];
1607 mutex_lock(&vcpu->kvm->lock);
1610 * Blow out the MMU to ensure that no other VCPU has an active mapping
1611 * to ensure that the updated hypercall appears atomically across all
1614 kvm_mmu_zap_all(vcpu->kvm);
1616 kvm_x86_ops->cache_regs(vcpu);
1617 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1618 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1619 != X86EMUL_CONTINUE)
1622 mutex_unlock(&vcpu->kvm->lock);
1627 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1629 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1632 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1634 struct descriptor_table dt = { limit, base };
1636 kvm_x86_ops->set_gdt(vcpu, &dt);
1639 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1641 struct descriptor_table dt = { limit, base };
1643 kvm_x86_ops->set_idt(vcpu, &dt);
1646 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1647 unsigned long *rflags)
1650 *rflags = kvm_x86_ops->get_rflags(vcpu);
1653 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1655 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
1666 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1671 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1672 unsigned long *rflags)
1676 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1677 *rflags = kvm_x86_ops->get_rflags(vcpu);
1686 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1689 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1693 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1698 case 0xc0010010: /* SYSCFG */
1699 case 0xc0010015: /* HWCR */
1700 case MSR_IA32_PLATFORM_ID:
1701 case MSR_IA32_P5_MC_ADDR:
1702 case MSR_IA32_P5_MC_TYPE:
1703 case MSR_IA32_MC0_CTL:
1704 case MSR_IA32_MCG_STATUS:
1705 case MSR_IA32_MCG_CAP:
1706 case MSR_IA32_MC0_MISC:
1707 case MSR_IA32_MC0_MISC+4:
1708 case MSR_IA32_MC0_MISC+8:
1709 case MSR_IA32_MC0_MISC+12:
1710 case MSR_IA32_MC0_MISC+16:
1711 case MSR_IA32_UCODE_REV:
1712 case MSR_IA32_PERF_STATUS:
1713 case MSR_IA32_EBL_CR_POWERON:
1714 /* MTRR registers */
1716 case 0x200 ... 0x2ff:
1719 case 0xcd: /* fsb frequency */
1722 case MSR_IA32_APICBASE:
1723 data = kvm_get_apic_base(vcpu);
1725 case MSR_IA32_MISC_ENABLE:
1726 data = vcpu->ia32_misc_enable_msr;
1728 #ifdef CONFIG_X86_64
1730 data = vcpu->shadow_efer;
1734 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1740 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1743 * Reads an msr value (of 'msr_index') into 'pdata'.
1744 * Returns 0 on success, non-0 otherwise.
1745 * Assumes vcpu_load() was already called.
1747 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1749 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1752 #ifdef CONFIG_X86_64
1754 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1756 if (efer & EFER_RESERVED_BITS) {
1757 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1764 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1765 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1770 kvm_x86_ops->set_efer(vcpu, efer);
1773 efer |= vcpu->shadow_efer & EFER_LMA;
1775 vcpu->shadow_efer = efer;
1780 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1783 #ifdef CONFIG_X86_64
1785 set_efer(vcpu, data);
1788 case MSR_IA32_MC0_STATUS:
1789 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1790 __FUNCTION__, data);
1792 case MSR_IA32_MCG_STATUS:
1793 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1794 __FUNCTION__, data);
1796 case MSR_IA32_UCODE_REV:
1797 case MSR_IA32_UCODE_WRITE:
1798 case 0x200 ... 0x2ff: /* MTRRs */
1800 case MSR_IA32_APICBASE:
1801 kvm_set_apic_base(vcpu, data);
1803 case MSR_IA32_MISC_ENABLE:
1804 vcpu->ia32_misc_enable_msr = data;
1807 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
1812 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1815 * Writes msr value into into the appropriate "register".
1816 * Returns 0 on success, non-0 otherwise.
1817 * Assumes vcpu_load() was already called.
1819 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1821 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
1824 void kvm_resched(struct kvm_vcpu *vcpu)
1826 if (!need_resched())
1830 EXPORT_SYMBOL_GPL(kvm_resched);
1832 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1836 struct kvm_cpuid_entry *e, *best;
1838 kvm_x86_ops->cache_regs(vcpu);
1839 function = vcpu->regs[VCPU_REGS_RAX];
1840 vcpu->regs[VCPU_REGS_RAX] = 0;
1841 vcpu->regs[VCPU_REGS_RBX] = 0;
1842 vcpu->regs[VCPU_REGS_RCX] = 0;
1843 vcpu->regs[VCPU_REGS_RDX] = 0;
1845 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1846 e = &vcpu->cpuid_entries[i];
1847 if (e->function == function) {
1852 * Both basic or both extended?
1854 if (((e->function ^ function) & 0x80000000) == 0)
1855 if (!best || e->function > best->function)
1859 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1860 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1861 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1862 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1864 kvm_x86_ops->decache_regs(vcpu);
1865 kvm_x86_ops->skip_emulated_instruction(vcpu);
1867 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1869 static int pio_copy_data(struct kvm_vcpu *vcpu)
1871 void *p = vcpu->pio_data;
1874 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1876 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1879 free_pio_guest_pages(vcpu);
1882 q += vcpu->pio.guest_page_offset;
1883 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1885 memcpy(q, p, bytes);
1887 memcpy(p, q, bytes);
1888 q -= vcpu->pio.guest_page_offset;
1890 free_pio_guest_pages(vcpu);
1894 static int complete_pio(struct kvm_vcpu *vcpu)
1896 struct kvm_pio_request *io = &vcpu->pio;
1900 kvm_x86_ops->cache_regs(vcpu);
1904 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1908 r = pio_copy_data(vcpu);
1910 kvm_x86_ops->cache_regs(vcpu);
1917 delta *= io->cur_count;
1919 * The size of the register should really depend on
1920 * current address size.
1922 vcpu->regs[VCPU_REGS_RCX] -= delta;
1928 vcpu->regs[VCPU_REGS_RDI] += delta;
1930 vcpu->regs[VCPU_REGS_RSI] += delta;
1933 kvm_x86_ops->decache_regs(vcpu);
1935 io->count -= io->cur_count;
1941 static void kernel_pio(struct kvm_io_device *pio_dev,
1942 struct kvm_vcpu *vcpu,
1945 /* TODO: String I/O for in kernel device */
1947 mutex_lock(&vcpu->kvm->lock);
1949 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1953 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1956 mutex_unlock(&vcpu->kvm->lock);
1959 static void pio_string_write(struct kvm_io_device *pio_dev,
1960 struct kvm_vcpu *vcpu)
1962 struct kvm_pio_request *io = &vcpu->pio;
1963 void *pd = vcpu->pio_data;
1966 mutex_lock(&vcpu->kvm->lock);
1967 for (i = 0; i < io->cur_count; i++) {
1968 kvm_iodevice_write(pio_dev, io->port,
1973 mutex_unlock(&vcpu->kvm->lock);
1976 int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1977 int size, unsigned port)
1979 struct kvm_io_device *pio_dev;
1981 vcpu->run->exit_reason = KVM_EXIT_IO;
1982 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1983 vcpu->run->io.size = vcpu->pio.size = size;
1984 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1985 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1986 vcpu->run->io.port = vcpu->pio.port = port;
1988 vcpu->pio.string = 0;
1990 vcpu->pio.guest_page_offset = 0;
1993 kvm_x86_ops->cache_regs(vcpu);
1994 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1995 kvm_x86_ops->decache_regs(vcpu);
1997 kvm_x86_ops->skip_emulated_instruction(vcpu);
1999 pio_dev = vcpu_find_pio_dev(vcpu, port);
2001 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
2007 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2009 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2010 int size, unsigned long count, int down,
2011 gva_t address, int rep, unsigned port)
2013 unsigned now, in_page;
2017 struct kvm_io_device *pio_dev;
2019 vcpu->run->exit_reason = KVM_EXIT_IO;
2020 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2021 vcpu->run->io.size = vcpu->pio.size = size;
2022 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2023 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2024 vcpu->run->io.port = vcpu->pio.port = port;
2026 vcpu->pio.string = 1;
2027 vcpu->pio.down = down;
2028 vcpu->pio.guest_page_offset = offset_in_page(address);
2029 vcpu->pio.rep = rep;
2032 kvm_x86_ops->skip_emulated_instruction(vcpu);
2037 in_page = PAGE_SIZE - offset_in_page(address);
2039 in_page = offset_in_page(address) + size;
2040 now = min(count, (unsigned long)in_page / size);
2043 * String I/O straddles page boundary. Pin two guest pages
2044 * so that we satisfy atomicity constraints. Do just one
2045 * transaction to avoid complexity.
2052 * String I/O in reverse. Yuck. Kill the guest, fix later.
2054 pr_unimpl(vcpu, "guest string pio down\n");
2058 vcpu->run->io.count = now;
2059 vcpu->pio.cur_count = now;
2061 if (vcpu->pio.cur_count == vcpu->pio.count)
2062 kvm_x86_ops->skip_emulated_instruction(vcpu);
2064 for (i = 0; i < nr_pages; ++i) {
2065 mutex_lock(&vcpu->kvm->lock);
2066 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2069 vcpu->pio.guest_pages[i] = page;
2070 mutex_unlock(&vcpu->kvm->lock);
2073 free_pio_guest_pages(vcpu);
2078 pio_dev = vcpu_find_pio_dev(vcpu, port);
2079 if (!vcpu->pio.in) {
2080 /* string PIO write */
2081 ret = pio_copy_data(vcpu);
2082 if (ret >= 0 && pio_dev) {
2083 pio_string_write(pio_dev, vcpu);
2085 if (vcpu->pio.count == 0)
2089 pr_unimpl(vcpu, "no string pio read support yet, "
2090 "port %x size %d count %ld\n",
2095 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2098 * Check if userspace requested an interrupt window, and that the
2099 * interrupt window is open.
2101 * No need to exit to userspace if we already have an interrupt queued.
2103 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2104 struct kvm_run *kvm_run)
2106 return (!vcpu->irq_summary &&
2107 kvm_run->request_interrupt_window &&
2108 vcpu->interrupt_window_open &&
2109 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2112 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2113 struct kvm_run *kvm_run)
2115 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2116 kvm_run->cr8 = get_cr8(vcpu);
2117 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2118 if (irqchip_in_kernel(vcpu->kvm))
2119 kvm_run->ready_for_interrupt_injection = 1;
2121 kvm_run->ready_for_interrupt_injection =
2122 (vcpu->interrupt_window_open &&
2123 vcpu->irq_summary == 0);
2126 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2130 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2131 pr_debug("vcpu %d received sipi with vector # %x\n",
2132 vcpu->vcpu_id, vcpu->sipi_vector);
2133 kvm_lapic_reset(vcpu);
2134 kvm_x86_ops->vcpu_reset(vcpu);
2135 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2139 if (vcpu->guest_debug.enabled)
2140 kvm_x86_ops->guest_debug_pre(vcpu);
2143 r = kvm_mmu_reload(vcpu);
2149 kvm_x86_ops->prepare_guest_switch(vcpu);
2150 kvm_load_guest_fpu(vcpu);
2152 local_irq_disable();
2154 if (signal_pending(current)) {
2158 kvm_run->exit_reason = KVM_EXIT_INTR;
2159 ++vcpu->stat.signal_exits;
2163 if (irqchip_in_kernel(vcpu->kvm))
2164 kvm_x86_ops->inject_pending_irq(vcpu);
2165 else if (!vcpu->mmio_read_completed)
2166 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2168 vcpu->guest_mode = 1;
2172 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2173 kvm_x86_ops->tlb_flush(vcpu);
2175 kvm_x86_ops->run(vcpu, kvm_run);
2177 vcpu->guest_mode = 0;
2183 * We must have an instruction between local_irq_enable() and
2184 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2185 * the interrupt shadow. The stat.exits increment will do nicely.
2186 * But we need to prevent reordering, hence this barrier():
2195 * Profile KVM exit RIPs:
2197 if (unlikely(prof_on == KVM_PROFILING)) {
2198 kvm_x86_ops->cache_regs(vcpu);
2199 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2202 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2205 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2207 kvm_run->exit_reason = KVM_EXIT_INTR;
2208 ++vcpu->stat.request_irq_exits;
2211 if (!need_resched()) {
2212 ++vcpu->stat.light_exits;
2223 post_kvm_run_save(vcpu, kvm_run);
2229 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2236 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2237 kvm_vcpu_block(vcpu);
2242 if (vcpu->sigset_active)
2243 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2245 /* re-sync apic's tpr */
2246 if (!irqchip_in_kernel(vcpu->kvm))
2247 set_cr8(vcpu, kvm_run->cr8);
2249 if (vcpu->pio.cur_count) {
2250 r = complete_pio(vcpu);
2255 if (vcpu->mmio_needed) {
2256 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2257 vcpu->mmio_read_completed = 1;
2258 vcpu->mmio_needed = 0;
2259 r = emulate_instruction(vcpu, kvm_run,
2260 vcpu->mmio_fault_cr2, 0, 1);
2261 if (r == EMULATE_DO_MMIO) {
2263 * Read-modify-write. Back to userspace.
2270 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2271 kvm_x86_ops->cache_regs(vcpu);
2272 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
2273 kvm_x86_ops->decache_regs(vcpu);
2276 r = __vcpu_run(vcpu, kvm_run);
2279 if (vcpu->sigset_active)
2280 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2286 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2287 struct kvm_regs *regs)
2291 kvm_x86_ops->cache_regs(vcpu);
2293 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2294 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2295 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2296 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2297 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2298 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2299 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2300 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
2301 #ifdef CONFIG_X86_64
2302 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2303 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2304 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2305 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2306 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2307 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2308 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2309 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2312 regs->rip = vcpu->rip;
2313 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2316 * Don't leak debug flags in case they were set for guest debugging
2318 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2319 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2326 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2327 struct kvm_regs *regs)
2331 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2332 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2333 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2334 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2335 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2336 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2337 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2338 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
2339 #ifdef CONFIG_X86_64
2340 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2341 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2342 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2343 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2344 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2345 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2346 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2347 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2350 vcpu->rip = regs->rip;
2351 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2353 kvm_x86_ops->decache_regs(vcpu);
2360 static void get_segment(struct kvm_vcpu *vcpu,
2361 struct kvm_segment *var, int seg)
2363 return kvm_x86_ops->get_segment(vcpu, var, seg);
2366 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2367 struct kvm_sregs *sregs)
2369 struct descriptor_table dt;
2374 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2375 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2376 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2377 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2378 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2379 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2381 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2382 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2384 kvm_x86_ops->get_idt(vcpu, &dt);
2385 sregs->idt.limit = dt.limit;
2386 sregs->idt.base = dt.base;
2387 kvm_x86_ops->get_gdt(vcpu, &dt);
2388 sregs->gdt.limit = dt.limit;
2389 sregs->gdt.base = dt.base;
2391 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2392 sregs->cr0 = vcpu->cr0;
2393 sregs->cr2 = vcpu->cr2;
2394 sregs->cr3 = vcpu->cr3;
2395 sregs->cr4 = vcpu->cr4;
2396 sregs->cr8 = get_cr8(vcpu);
2397 sregs->efer = vcpu->shadow_efer;
2398 sregs->apic_base = kvm_get_apic_base(vcpu);
2400 if (irqchip_in_kernel(vcpu->kvm)) {
2401 memset(sregs->interrupt_bitmap, 0,
2402 sizeof sregs->interrupt_bitmap);
2403 pending_vec = kvm_x86_ops->get_irq(vcpu);
2404 if (pending_vec >= 0)
2405 set_bit(pending_vec,
2406 (unsigned long *)sregs->interrupt_bitmap);
2408 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2409 sizeof sregs->interrupt_bitmap);
2416 static void set_segment(struct kvm_vcpu *vcpu,
2417 struct kvm_segment *var, int seg)
2419 return kvm_x86_ops->set_segment(vcpu, var, seg);
2422 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2423 struct kvm_sregs *sregs)
2425 int mmu_reset_needed = 0;
2426 int i, pending_vec, max_bits;
2427 struct descriptor_table dt;
2431 dt.limit = sregs->idt.limit;
2432 dt.base = sregs->idt.base;
2433 kvm_x86_ops->set_idt(vcpu, &dt);
2434 dt.limit = sregs->gdt.limit;
2435 dt.base = sregs->gdt.base;
2436 kvm_x86_ops->set_gdt(vcpu, &dt);
2438 vcpu->cr2 = sregs->cr2;
2439 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2440 vcpu->cr3 = sregs->cr3;
2442 set_cr8(vcpu, sregs->cr8);
2444 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2445 #ifdef CONFIG_X86_64
2446 kvm_x86_ops->set_efer(vcpu, sregs->efer);
2448 kvm_set_apic_base(vcpu, sregs->apic_base);
2450 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2452 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2453 vcpu->cr0 = sregs->cr0;
2454 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
2456 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2457 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2458 if (!is_long_mode(vcpu) && is_pae(vcpu))
2459 load_pdptrs(vcpu, vcpu->cr3);
2461 if (mmu_reset_needed)
2462 kvm_mmu_reset_context(vcpu);
2464 if (!irqchip_in_kernel(vcpu->kvm)) {
2465 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2466 sizeof vcpu->irq_pending);
2467 vcpu->irq_summary = 0;
2468 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2469 if (vcpu->irq_pending[i])
2470 __set_bit(i, &vcpu->irq_summary);
2472 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2473 pending_vec = find_first_bit(
2474 (const unsigned long *)sregs->interrupt_bitmap,
2476 /* Only pending external irq is handled here */
2477 if (pending_vec < max_bits) {
2478 kvm_x86_ops->set_irq(vcpu, pending_vec);
2479 pr_debug("Set back pending irq %d\n",
2484 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2485 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2486 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2487 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2488 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2489 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2491 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2492 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2499 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2501 struct kvm_segment cs;
2503 get_segment(vcpu, &cs, VCPU_SREG_CS);
2507 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2510 * Translate a guest virtual address to a guest physical address.
2512 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2513 struct kvm_translation *tr)
2515 unsigned long vaddr = tr->linear_address;
2519 mutex_lock(&vcpu->kvm->lock);
2520 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2521 tr->physical_address = gpa;
2522 tr->valid = gpa != UNMAPPED_GVA;
2525 mutex_unlock(&vcpu->kvm->lock);
2531 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2532 struct kvm_interrupt *irq)
2534 if (irq->irq < 0 || irq->irq >= 256)
2536 if (irqchip_in_kernel(vcpu->kvm))
2540 set_bit(irq->irq, vcpu->irq_pending);
2541 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2548 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2549 struct kvm_debug_guest *dbg)
2555 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2562 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2563 unsigned long address,
2566 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2567 unsigned long pgoff;
2570 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2572 page = virt_to_page(vcpu->run);
2573 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2574 page = virt_to_page(vcpu->pio_data);
2576 return NOPAGE_SIGBUS;
2579 *type = VM_FAULT_MINOR;
2584 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2585 .nopage = kvm_vcpu_nopage,
2588 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2590 vma->vm_ops = &kvm_vcpu_vm_ops;
2594 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2596 struct kvm_vcpu *vcpu = filp->private_data;
2598 fput(vcpu->kvm->filp);
2602 static struct file_operations kvm_vcpu_fops = {
2603 .release = kvm_vcpu_release,
2604 .unlocked_ioctl = kvm_vcpu_ioctl,
2605 .compat_ioctl = kvm_vcpu_ioctl,
2606 .mmap = kvm_vcpu_mmap,
2610 * Allocates an inode for the vcpu.
2612 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2615 struct inode *inode;
2618 r = anon_inode_getfd(&fd, &inode, &file,
2619 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2622 atomic_inc(&vcpu->kvm->filp->f_count);
2627 * Creates some virtual cpus. Good luck creating more than one.
2629 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2632 struct kvm_vcpu *vcpu;
2637 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
2639 return PTR_ERR(vcpu);
2641 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2643 /* We do fxsave: this must be aligned. */
2644 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2647 r = kvm_mmu_setup(vcpu);
2652 mutex_lock(&kvm->lock);
2653 if (kvm->vcpus[n]) {
2655 mutex_unlock(&kvm->lock);
2658 kvm->vcpus[n] = vcpu;
2659 mutex_unlock(&kvm->lock);
2661 /* Now it's all set up, let userspace reach it */
2662 r = create_vcpu_fd(vcpu);
2668 mutex_lock(&kvm->lock);
2669 kvm->vcpus[n] = NULL;
2670 mutex_unlock(&kvm->lock);
2674 kvm_mmu_unload(vcpu);
2678 kvm_x86_ops->vcpu_free(vcpu);
2682 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2685 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2686 vcpu->sigset_active = 1;
2687 vcpu->sigset = *sigset;
2689 vcpu->sigset_active = 0;
2694 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2695 * we have asm/x86/processor.h
2706 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2707 #ifdef CONFIG_X86_64
2708 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2710 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2714 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2716 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2720 memcpy(fpu->fpr, fxsave->st_space, 128);
2721 fpu->fcw = fxsave->cwd;
2722 fpu->fsw = fxsave->swd;
2723 fpu->ftwx = fxsave->twd;
2724 fpu->last_opcode = fxsave->fop;
2725 fpu->last_ip = fxsave->rip;
2726 fpu->last_dp = fxsave->rdp;
2727 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2734 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2736 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2740 memcpy(fxsave->st_space, fpu->fpr, 128);
2741 fxsave->cwd = fpu->fcw;
2742 fxsave->swd = fpu->fsw;
2743 fxsave->twd = fpu->ftwx;
2744 fxsave->fop = fpu->last_opcode;
2745 fxsave->rip = fpu->last_ip;
2746 fxsave->rdp = fpu->last_dp;
2747 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2754 static long kvm_vcpu_ioctl(struct file *filp,
2755 unsigned int ioctl, unsigned long arg)
2757 struct kvm_vcpu *vcpu = filp->private_data;
2758 void __user *argp = (void __user *)arg;
2766 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2768 case KVM_GET_REGS: {
2769 struct kvm_regs kvm_regs;
2771 memset(&kvm_regs, 0, sizeof kvm_regs);
2772 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2776 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2781 case KVM_SET_REGS: {
2782 struct kvm_regs kvm_regs;
2785 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2787 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2793 case KVM_GET_SREGS: {
2794 struct kvm_sregs kvm_sregs;
2796 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2797 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2801 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2806 case KVM_SET_SREGS: {
2807 struct kvm_sregs kvm_sregs;
2810 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2812 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2818 case KVM_TRANSLATE: {
2819 struct kvm_translation tr;
2822 if (copy_from_user(&tr, argp, sizeof tr))
2824 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2828 if (copy_to_user(argp, &tr, sizeof tr))
2833 case KVM_INTERRUPT: {
2834 struct kvm_interrupt irq;
2837 if (copy_from_user(&irq, argp, sizeof irq))
2839 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2845 case KVM_DEBUG_GUEST: {
2846 struct kvm_debug_guest dbg;
2849 if (copy_from_user(&dbg, argp, sizeof dbg))
2851 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2857 case KVM_SET_SIGNAL_MASK: {
2858 struct kvm_signal_mask __user *sigmask_arg = argp;
2859 struct kvm_signal_mask kvm_sigmask;
2860 sigset_t sigset, *p;
2865 if (copy_from_user(&kvm_sigmask, argp,
2866 sizeof kvm_sigmask))
2869 if (kvm_sigmask.len != sizeof sigset)
2872 if (copy_from_user(&sigset, sigmask_arg->sigset,
2877 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2883 memset(&fpu, 0, sizeof fpu);
2884 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2888 if (copy_to_user(argp, &fpu, sizeof fpu))
2897 if (copy_from_user(&fpu, argp, sizeof fpu))
2899 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2906 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
2912 static long kvm_vm_ioctl(struct file *filp,
2913 unsigned int ioctl, unsigned long arg)
2915 struct kvm *kvm = filp->private_data;
2916 void __user *argp = (void __user *)arg;
2920 case KVM_CREATE_VCPU:
2921 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2925 case KVM_SET_MEMORY_REGION: {
2926 struct kvm_memory_region kvm_mem;
2927 struct kvm_userspace_memory_region kvm_userspace_mem;
2930 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2932 kvm_userspace_mem.slot = kvm_mem.slot;
2933 kvm_userspace_mem.flags = kvm_mem.flags;
2934 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2935 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2936 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2941 case KVM_SET_USER_MEMORY_REGION: {
2942 struct kvm_userspace_memory_region kvm_userspace_mem;
2945 if (copy_from_user(&kvm_userspace_mem, argp,
2946 sizeof kvm_userspace_mem))
2949 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
2954 case KVM_SET_NR_MMU_PAGES:
2955 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2959 case KVM_GET_NR_MMU_PAGES:
2960 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2962 case KVM_GET_DIRTY_LOG: {
2963 struct kvm_dirty_log log;
2966 if (copy_from_user(&log, argp, sizeof log))
2968 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2973 case KVM_SET_MEMORY_ALIAS: {
2974 struct kvm_memory_alias alias;
2977 if (copy_from_user(&alias, argp, sizeof alias))
2979 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2984 case KVM_CREATE_IRQCHIP:
2986 kvm->vpic = kvm_create_pic(kvm);
2988 r = kvm_ioapic_init(kvm);
2997 case KVM_IRQ_LINE: {
2998 struct kvm_irq_level irq_event;
3001 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3003 if (irqchip_in_kernel(kvm)) {
3004 mutex_lock(&kvm->lock);
3005 if (irq_event.irq < 16)
3006 kvm_pic_set_irq(pic_irqchip(kvm),
3009 kvm_ioapic_set_irq(kvm->vioapic,
3012 mutex_unlock(&kvm->lock);
3017 case KVM_GET_IRQCHIP: {
3018 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3019 struct kvm_irqchip chip;
3022 if (copy_from_user(&chip, argp, sizeof chip))
3025 if (!irqchip_in_kernel(kvm))
3027 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3031 if (copy_to_user(argp, &chip, sizeof chip))
3036 case KVM_SET_IRQCHIP: {
3037 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3038 struct kvm_irqchip chip;
3041 if (copy_from_user(&chip, argp, sizeof chip))
3044 if (!irqchip_in_kernel(kvm))
3046 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3059 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3060 unsigned long address,
3063 struct kvm *kvm = vma->vm_file->private_data;
3064 unsigned long pgoff;
3067 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
3068 page = gfn_to_page(kvm, pgoff);
3070 return NOPAGE_SIGBUS;
3073 *type = VM_FAULT_MINOR;
3078 static struct vm_operations_struct kvm_vm_vm_ops = {
3079 .nopage = kvm_vm_nopage,
3082 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3084 vma->vm_ops = &kvm_vm_vm_ops;
3088 static struct file_operations kvm_vm_fops = {
3089 .release = kvm_vm_release,
3090 .unlocked_ioctl = kvm_vm_ioctl,
3091 .compat_ioctl = kvm_vm_ioctl,
3092 .mmap = kvm_vm_mmap,
3095 static int kvm_dev_ioctl_create_vm(void)
3098 struct inode *inode;
3102 kvm = kvm_create_vm();
3104 return PTR_ERR(kvm);
3105 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3107 kvm_destroy_vm(kvm);
3116 static long kvm_dev_ioctl(struct file *filp,
3117 unsigned int ioctl, unsigned long arg)
3119 void __user *argp = (void __user *)arg;
3123 case KVM_GET_API_VERSION:
3127 r = KVM_API_VERSION;
3133 r = kvm_dev_ioctl_create_vm();
3135 case KVM_CHECK_EXTENSION: {
3136 int ext = (long)argp;
3139 case KVM_CAP_IRQCHIP:
3141 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
3142 case KVM_CAP_USER_MEMORY:
3151 case KVM_GET_VCPU_MMAP_SIZE:
3158 return kvm_arch_dev_ioctl(filp, ioctl, arg);
3164 static struct file_operations kvm_chardev_ops = {
3165 .unlocked_ioctl = kvm_dev_ioctl,
3166 .compat_ioctl = kvm_dev_ioctl,
3169 static struct miscdevice kvm_dev = {
3176 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3179 static void decache_vcpus_on_cpu(int cpu)
3182 struct kvm_vcpu *vcpu;
3185 spin_lock(&kvm_lock);
3186 list_for_each_entry(vm, &vm_list, vm_list)
3187 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3188 vcpu = vm->vcpus[i];
3192 * If the vcpu is locked, then it is running on some
3193 * other cpu and therefore it is not cached on the
3196 * If it's not locked, check the last cpu it executed
3199 if (mutex_trylock(&vcpu->mutex)) {
3200 if (vcpu->cpu == cpu) {
3201 kvm_x86_ops->vcpu_decache(vcpu);
3204 mutex_unlock(&vcpu->mutex);
3207 spin_unlock(&kvm_lock);
3210 static void hardware_enable(void *junk)
3212 int cpu = raw_smp_processor_id();
3214 if (cpu_isset(cpu, cpus_hardware_enabled))
3216 cpu_set(cpu, cpus_hardware_enabled);
3217 kvm_x86_ops->hardware_enable(NULL);
3220 static void hardware_disable(void *junk)
3222 int cpu = raw_smp_processor_id();
3224 if (!cpu_isset(cpu, cpus_hardware_enabled))
3226 cpu_clear(cpu, cpus_hardware_enabled);
3227 decache_vcpus_on_cpu(cpu);
3228 kvm_x86_ops->hardware_disable(NULL);
3231 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3238 case CPU_DYING_FROZEN:
3239 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3241 hardware_disable(NULL);
3243 case CPU_UP_CANCELED:
3244 case CPU_UP_CANCELED_FROZEN:
3245 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3247 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
3250 case CPU_ONLINE_FROZEN:
3251 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3253 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
3259 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3262 if (val == SYS_RESTART) {
3264 * Some (well, at least mine) BIOSes hang on reboot if
3267 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3268 on_each_cpu(hardware_disable, NULL, 0, 1);
3273 static struct notifier_block kvm_reboot_notifier = {
3274 .notifier_call = kvm_reboot,
3278 void kvm_io_bus_init(struct kvm_io_bus *bus)
3280 memset(bus, 0, sizeof(*bus));
3283 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3287 for (i = 0; i < bus->dev_count; i++) {
3288 struct kvm_io_device *pos = bus->devs[i];
3290 kvm_iodevice_destructor(pos);
3294 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3298 for (i = 0; i < bus->dev_count; i++) {
3299 struct kvm_io_device *pos = bus->devs[i];
3301 if (pos->in_range(pos, addr))
3308 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3310 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3312 bus->devs[bus->dev_count++] = dev;
3315 static struct notifier_block kvm_cpu_notifier = {
3316 .notifier_call = kvm_cpu_hotplug,
3317 .priority = 20, /* must be > scheduler priority */
3320 static u64 stat_get(void *_offset)
3322 unsigned offset = (long)_offset;
3325 struct kvm_vcpu *vcpu;
3328 spin_lock(&kvm_lock);
3329 list_for_each_entry(kvm, &vm_list, vm_list)
3330 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3331 vcpu = kvm->vcpus[i];
3333 total += *(u32 *)((void *)vcpu + offset);
3335 spin_unlock(&kvm_lock);
3339 DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
3341 static __init void kvm_init_debug(void)
3343 struct kvm_stats_debugfs_item *p;
3345 debugfs_dir = debugfs_create_dir("kvm", NULL);
3346 for (p = debugfs_entries; p->name; ++p)
3347 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3348 (void *)(long)p->offset,
3352 static void kvm_exit_debug(void)
3354 struct kvm_stats_debugfs_item *p;
3356 for (p = debugfs_entries; p->name; ++p)
3357 debugfs_remove(p->dentry);
3358 debugfs_remove(debugfs_dir);
3361 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3363 hardware_disable(NULL);
3367 static int kvm_resume(struct sys_device *dev)
3369 hardware_enable(NULL);
3373 static struct sysdev_class kvm_sysdev_class = {
3375 .suspend = kvm_suspend,
3376 .resume = kvm_resume,
3379 static struct sys_device kvm_sysdev = {
3381 .cls = &kvm_sysdev_class,
3384 hpa_t bad_page_address;
3387 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3389 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3392 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3394 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3396 kvm_x86_ops->vcpu_load(vcpu, cpu);
3399 static void kvm_sched_out(struct preempt_notifier *pn,
3400 struct task_struct *next)
3402 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3404 kvm_x86_ops->vcpu_put(vcpu);
3407 int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
3408 struct module *module)
3414 printk(KERN_ERR "kvm: already loaded the other module\n");
3418 if (!ops->cpu_has_kvm_support()) {
3419 printk(KERN_ERR "kvm: no hardware support\n");
3422 if (ops->disabled_by_bios()) {
3423 printk(KERN_ERR "kvm: disabled by bios\n");
3429 r = kvm_x86_ops->hardware_setup();
3433 for_each_online_cpu(cpu) {
3434 smp_call_function_single(cpu,
3435 kvm_x86_ops->check_processor_compatibility,
3441 on_each_cpu(hardware_enable, NULL, 0, 1);
3442 r = register_cpu_notifier(&kvm_cpu_notifier);
3445 register_reboot_notifier(&kvm_reboot_notifier);
3447 r = sysdev_class_register(&kvm_sysdev_class);
3451 r = sysdev_register(&kvm_sysdev);
3455 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3456 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3457 __alignof__(struct kvm_vcpu), 0, 0);
3458 if (!kvm_vcpu_cache) {
3463 kvm_chardev_ops.owner = module;
3465 r = misc_register(&kvm_dev);
3467 printk(KERN_ERR "kvm: misc device register failed\n");
3471 kvm_preempt_ops.sched_in = kvm_sched_in;
3472 kvm_preempt_ops.sched_out = kvm_sched_out;
3474 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3479 kmem_cache_destroy(kvm_vcpu_cache);
3481 sysdev_unregister(&kvm_sysdev);
3483 sysdev_class_unregister(&kvm_sysdev_class);
3485 unregister_reboot_notifier(&kvm_reboot_notifier);
3486 unregister_cpu_notifier(&kvm_cpu_notifier);
3488 on_each_cpu(hardware_disable, NULL, 0, 1);
3490 kvm_x86_ops->hardware_unsetup();
3495 EXPORT_SYMBOL_GPL(kvm_init_x86);
3497 void kvm_exit_x86(void)
3499 misc_deregister(&kvm_dev);
3500 kmem_cache_destroy(kvm_vcpu_cache);
3501 sysdev_unregister(&kvm_sysdev);
3502 sysdev_class_unregister(&kvm_sysdev_class);
3503 unregister_reboot_notifier(&kvm_reboot_notifier);
3504 unregister_cpu_notifier(&kvm_cpu_notifier);
3505 on_each_cpu(hardware_disable, NULL, 0, 1);
3506 kvm_x86_ops->hardware_unsetup();
3509 EXPORT_SYMBOL_GPL(kvm_exit_x86);
3511 static __init int kvm_init(void)
3513 static struct page *bad_page;
3516 r = kvm_mmu_module_init();
3524 bad_page = alloc_page(GFP_KERNEL);
3526 if (bad_page == NULL) {
3531 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3532 memset(__va(bad_page_address), 0, PAGE_SIZE);
3538 kvm_mmu_module_exit();
3543 static __exit void kvm_exit(void)
3546 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3547 kvm_mmu_module_exit();
3550 module_init(kvm_init)
3551 module_exit(kvm_exit)