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)
201 atomic_t *completed = _completed;
203 atomic_inc(completed);
206 void kvm_flush_remote_tlbs(struct kvm *kvm)
210 struct kvm_vcpu *vcpu;
213 atomic_set(&completed, 0);
216 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
217 vcpu = kvm->vcpus[i];
220 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
223 if (cpu != -1 && cpu != raw_smp_processor_id())
224 if (!cpu_isset(cpu, cpus)) {
231 * We really want smp_call_function_mask() here. But that's not
232 * available, so ipi all cpus in parallel and wait for them
235 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
236 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
237 while (atomic_read(&completed) != needed) {
243 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
248 mutex_init(&vcpu->mutex);
250 vcpu->mmu.root_hpa = INVALID_PAGE;
253 if (!irqchip_in_kernel(kvm) || id == 0)
254 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
256 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
257 init_waitqueue_head(&vcpu->wq);
259 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
264 vcpu->run = page_address(page);
266 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
271 vcpu->pio_data = page_address(page);
273 r = kvm_mmu_create(vcpu);
275 goto fail_free_pio_data;
280 free_page((unsigned long)vcpu->pio_data);
282 free_page((unsigned long)vcpu->run);
286 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
288 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
290 kvm_mmu_destroy(vcpu);
292 hrtimer_cancel(&vcpu->apic->timer.dev);
293 kvm_free_apic(vcpu->apic);
294 free_page((unsigned long)vcpu->pio_data);
295 free_page((unsigned long)vcpu->run);
297 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
299 static struct kvm *kvm_create_vm(void)
301 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
304 return ERR_PTR(-ENOMEM);
306 kvm_io_bus_init(&kvm->pio_bus);
307 mutex_init(&kvm->lock);
308 INIT_LIST_HEAD(&kvm->active_mmu_pages);
309 kvm_io_bus_init(&kvm->mmio_bus);
310 spin_lock(&kvm_lock);
311 list_add(&kvm->vm_list, &vm_list);
312 spin_unlock(&kvm_lock);
317 * Free any memory in @free but not in @dont.
319 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
320 struct kvm_memory_slot *dont)
324 if (!dont || free->phys_mem != dont->phys_mem)
325 if (free->phys_mem) {
326 for (i = 0; i < free->npages; ++i)
327 if (free->phys_mem[i])
328 __free_page(free->phys_mem[i]);
329 vfree(free->phys_mem);
332 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
333 vfree(free->dirty_bitmap);
335 free->phys_mem = NULL;
337 free->dirty_bitmap = NULL;
340 static void kvm_free_physmem(struct kvm *kvm)
344 for (i = 0; i < kvm->nmemslots; ++i)
345 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
348 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
352 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
353 if (vcpu->pio.guest_pages[i]) {
354 __free_page(vcpu->pio.guest_pages[i]);
355 vcpu->pio.guest_pages[i] = NULL;
359 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
362 kvm_mmu_unload(vcpu);
366 static void kvm_free_vcpus(struct kvm *kvm)
371 * Unpin any mmu pages first.
373 for (i = 0; i < KVM_MAX_VCPUS; ++i)
375 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
376 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
378 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
379 kvm->vcpus[i] = NULL;
385 static void kvm_destroy_vm(struct kvm *kvm)
387 spin_lock(&kvm_lock);
388 list_del(&kvm->vm_list);
389 spin_unlock(&kvm_lock);
390 kvm_io_bus_destroy(&kvm->pio_bus);
391 kvm_io_bus_destroy(&kvm->mmio_bus);
395 kvm_free_physmem(kvm);
399 static int kvm_vm_release(struct inode *inode, struct file *filp)
401 struct kvm *kvm = filp->private_data;
407 static void inject_gp(struct kvm_vcpu *vcpu)
409 kvm_x86_ops->inject_gp(vcpu, 0);
413 * Load the pae pdptrs. Return true is they are all valid.
415 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
417 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
418 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
423 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
425 mutex_lock(&vcpu->kvm->lock);
426 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
432 pdpt = kmap_atomic(page, KM_USER0);
433 memcpy(pdpte, pdpt+offset, sizeof(pdpte));
434 kunmap_atomic(pdpt, KM_USER0);
436 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
437 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
444 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
446 mutex_unlock(&vcpu->kvm->lock);
451 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
453 if (cr0 & CR0_RESERVED_BITS) {
454 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
460 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
461 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
466 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
467 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
468 "and a clear PE flag\n");
473 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
475 if ((vcpu->shadow_efer & EFER_LME)) {
479 printk(KERN_DEBUG "set_cr0: #GP, start paging "
480 "in long mode while PAE is disabled\n");
484 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
486 printk(KERN_DEBUG "set_cr0: #GP, start paging "
487 "in long mode while CS.L == 1\n");
494 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
495 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
503 kvm_x86_ops->set_cr0(vcpu, cr0);
506 mutex_lock(&vcpu->kvm->lock);
507 kvm_mmu_reset_context(vcpu);
508 mutex_unlock(&vcpu->kvm->lock);
511 EXPORT_SYMBOL_GPL(set_cr0);
513 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
515 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
517 EXPORT_SYMBOL_GPL(lmsw);
519 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
521 if (cr4 & CR4_RESERVED_BITS) {
522 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
527 if (is_long_mode(vcpu)) {
528 if (!(cr4 & X86_CR4_PAE)) {
529 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
534 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
535 && !load_pdptrs(vcpu, vcpu->cr3)) {
536 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
541 if (cr4 & X86_CR4_VMXE) {
542 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
546 kvm_x86_ops->set_cr4(vcpu, cr4);
548 mutex_lock(&vcpu->kvm->lock);
549 kvm_mmu_reset_context(vcpu);
550 mutex_unlock(&vcpu->kvm->lock);
552 EXPORT_SYMBOL_GPL(set_cr4);
554 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
556 if (is_long_mode(vcpu)) {
557 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
558 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
564 if (cr3 & CR3_PAE_RESERVED_BITS) {
566 "set_cr3: #GP, reserved bits\n");
570 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
571 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
577 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
579 "set_cr3: #GP, reserved bits\n");
586 mutex_lock(&vcpu->kvm->lock);
588 * Does the new cr3 value map to physical memory? (Note, we
589 * catch an invalid cr3 even in real-mode, because it would
590 * cause trouble later on when we turn on paging anyway.)
592 * A real CPU would silently accept an invalid cr3 and would
593 * attempt to use it - with largely undefined (and often hard
594 * to debug) behavior on the guest side.
596 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
600 vcpu->mmu.new_cr3(vcpu);
602 mutex_unlock(&vcpu->kvm->lock);
604 EXPORT_SYMBOL_GPL(set_cr3);
606 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
608 if (cr8 & CR8_RESERVED_BITS) {
609 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_tpr(vcpu, cr8);
618 EXPORT_SYMBOL_GPL(set_cr8);
620 unsigned long get_cr8(struct kvm_vcpu *vcpu)
622 if (irqchip_in_kernel(vcpu->kvm))
623 return kvm_lapic_get_cr8(vcpu);
627 EXPORT_SYMBOL_GPL(get_cr8);
629 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
631 if (irqchip_in_kernel(vcpu->kvm))
632 return vcpu->apic_base;
634 return vcpu->apic_base;
636 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
638 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
640 /* TODO: reserve bits check */
641 if (irqchip_in_kernel(vcpu->kvm))
642 kvm_lapic_set_base(vcpu, data);
644 vcpu->apic_base = data;
646 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
648 void fx_init(struct kvm_vcpu *vcpu)
650 unsigned after_mxcsr_mask;
652 /* Initialize guest FPU by resetting ours and saving into guest's */
654 fx_save(&vcpu->host_fx_image);
656 fx_save(&vcpu->guest_fx_image);
657 fx_restore(&vcpu->host_fx_image);
660 vcpu->cr0 |= X86_CR0_ET;
661 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
662 vcpu->guest_fx_image.mxcsr = 0x1f80;
663 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
664 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
666 EXPORT_SYMBOL_GPL(fx_init);
669 * Allocate some memory and give it an address in the guest physical address
672 * Discontiguous memory is allowed, mostly for framebuffers.
674 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
675 struct kvm_memory_region *mem)
679 unsigned long npages;
681 struct kvm_memory_slot *memslot;
682 struct kvm_memory_slot old, new;
685 /* General sanity checks */
686 if (mem->memory_size & (PAGE_SIZE - 1))
688 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
690 if (mem->slot >= KVM_MEMORY_SLOTS)
692 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
695 memslot = &kvm->memslots[mem->slot];
696 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
697 npages = mem->memory_size >> PAGE_SHIFT;
700 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
702 mutex_lock(&kvm->lock);
704 new = old = *memslot;
706 new.base_gfn = base_gfn;
708 new.flags = mem->flags;
710 /* Disallow changing a memory slot's size. */
712 if (npages && old.npages && npages != old.npages)
715 /* Check for overlaps */
717 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
718 struct kvm_memory_slot *s = &kvm->memslots[i];
722 if (!((base_gfn + npages <= s->base_gfn) ||
723 (base_gfn >= s->base_gfn + s->npages)))
727 /* Deallocate if slot is being removed */
731 /* Free page dirty bitmap if unneeded */
732 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
733 new.dirty_bitmap = NULL;
737 /* Allocate if a slot is being created */
738 if (npages && !new.phys_mem) {
739 new.phys_mem = vmalloc(npages * sizeof(struct page *));
744 memset(new.phys_mem, 0, npages * sizeof(struct page *));
745 for (i = 0; i < npages; ++i) {
746 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
748 if (!new.phys_mem[i])
750 set_page_private(new.phys_mem[i],0);
754 /* Allocate page dirty bitmap if needed */
755 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
756 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
758 new.dirty_bitmap = vmalloc(dirty_bytes);
759 if (!new.dirty_bitmap)
761 memset(new.dirty_bitmap, 0, dirty_bytes);
764 if (mem->slot >= kvm->nmemslots)
765 kvm->nmemslots = mem->slot + 1;
769 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
770 kvm_flush_remote_tlbs(kvm);
772 mutex_unlock(&kvm->lock);
774 kvm_free_physmem_slot(&old, &new);
778 mutex_unlock(&kvm->lock);
779 kvm_free_physmem_slot(&new, &old);
785 * Get (and clear) the dirty memory log for a memory slot.
787 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
788 struct kvm_dirty_log *log)
790 struct kvm_memory_slot *memslot;
793 unsigned long any = 0;
795 mutex_lock(&kvm->lock);
798 if (log->slot >= KVM_MEMORY_SLOTS)
801 memslot = &kvm->memslots[log->slot];
803 if (!memslot->dirty_bitmap)
806 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
808 for (i = 0; !any && i < n/sizeof(long); ++i)
809 any = memslot->dirty_bitmap[i];
812 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
815 /* If nothing is dirty, don't bother messing with page tables. */
817 kvm_mmu_slot_remove_write_access(kvm, log->slot);
818 kvm_flush_remote_tlbs(kvm);
819 memset(memslot->dirty_bitmap, 0, n);
825 mutex_unlock(&kvm->lock);
830 * Set a new alias region. Aliases map a portion of physical memory into
831 * another portion. This is useful for memory windows, for example the PC
834 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
835 struct kvm_memory_alias *alias)
838 struct kvm_mem_alias *p;
841 /* General sanity checks */
842 if (alias->memory_size & (PAGE_SIZE - 1))
844 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
846 if (alias->slot >= KVM_ALIAS_SLOTS)
848 if (alias->guest_phys_addr + alias->memory_size
849 < alias->guest_phys_addr)
851 if (alias->target_phys_addr + alias->memory_size
852 < alias->target_phys_addr)
855 mutex_lock(&kvm->lock);
857 p = &kvm->aliases[alias->slot];
858 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
859 p->npages = alias->memory_size >> PAGE_SHIFT;
860 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
862 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
863 if (kvm->aliases[n - 1].npages)
867 kvm_mmu_zap_all(kvm);
869 mutex_unlock(&kvm->lock);
877 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
882 switch (chip->chip_id) {
883 case KVM_IRQCHIP_PIC_MASTER:
884 memcpy (&chip->chip.pic,
885 &pic_irqchip(kvm)->pics[0],
886 sizeof(struct kvm_pic_state));
888 case KVM_IRQCHIP_PIC_SLAVE:
889 memcpy (&chip->chip.pic,
890 &pic_irqchip(kvm)->pics[1],
891 sizeof(struct kvm_pic_state));
893 case KVM_IRQCHIP_IOAPIC:
894 memcpy (&chip->chip.ioapic,
896 sizeof(struct kvm_ioapic_state));
905 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
910 switch (chip->chip_id) {
911 case KVM_IRQCHIP_PIC_MASTER:
912 memcpy (&pic_irqchip(kvm)->pics[0],
914 sizeof(struct kvm_pic_state));
916 case KVM_IRQCHIP_PIC_SLAVE:
917 memcpy (&pic_irqchip(kvm)->pics[1],
919 sizeof(struct kvm_pic_state));
921 case KVM_IRQCHIP_IOAPIC:
922 memcpy (ioapic_irqchip(kvm),
924 sizeof(struct kvm_ioapic_state));
930 kvm_pic_update_irq(pic_irqchip(kvm));
934 static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
937 struct kvm_mem_alias *alias;
939 for (i = 0; i < kvm->naliases; ++i) {
940 alias = &kvm->aliases[i];
941 if (gfn >= alias->base_gfn
942 && gfn < alias->base_gfn + alias->npages)
943 return alias->target_gfn + gfn - alias->base_gfn;
948 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
952 for (i = 0; i < kvm->nmemslots; ++i) {
953 struct kvm_memory_slot *memslot = &kvm->memslots[i];
955 if (gfn >= memslot->base_gfn
956 && gfn < memslot->base_gfn + memslot->npages)
962 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
964 gfn = unalias_gfn(kvm, gfn);
965 return __gfn_to_memslot(kvm, gfn);
968 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
970 struct kvm_memory_slot *slot;
972 gfn = unalias_gfn(kvm, gfn);
973 slot = __gfn_to_memslot(kvm, gfn);
976 return slot->phys_mem[gfn - slot->base_gfn];
978 EXPORT_SYMBOL_GPL(gfn_to_page);
980 /* WARNING: Does not work on aliased pages. */
981 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
983 struct kvm_memory_slot *memslot;
985 memslot = __gfn_to_memslot(kvm, gfn);
986 if (memslot && memslot->dirty_bitmap) {
987 unsigned long rel_gfn = gfn - memslot->base_gfn;
990 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
991 set_bit(rel_gfn, memslot->dirty_bitmap);
995 int emulator_read_std(unsigned long addr,
998 struct kvm_vcpu *vcpu)
1003 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1004 unsigned offset = addr & (PAGE_SIZE-1);
1005 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1010 if (gpa == UNMAPPED_GVA)
1011 return X86EMUL_PROPAGATE_FAULT;
1012 pfn = gpa >> PAGE_SHIFT;
1013 page = gfn_to_page(vcpu->kvm, pfn);
1015 return X86EMUL_UNHANDLEABLE;
1016 page_virt = kmap_atomic(page, KM_USER0);
1018 memcpy(data, page_virt + offset, tocopy);
1020 kunmap_atomic(page_virt, KM_USER0);
1027 return X86EMUL_CONTINUE;
1029 EXPORT_SYMBOL_GPL(emulator_read_std);
1031 static int emulator_write_std(unsigned long addr,
1034 struct kvm_vcpu *vcpu)
1036 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
1037 return X86EMUL_UNHANDLEABLE;
1041 * Only apic need an MMIO device hook, so shortcut now..
1043 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1046 struct kvm_io_device *dev;
1049 dev = &vcpu->apic->dev;
1050 if (dev->in_range(dev, addr))
1056 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1059 struct kvm_io_device *dev;
1061 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1063 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1067 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1070 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1073 static int emulator_read_emulated(unsigned long addr,
1076 struct kvm_vcpu *vcpu)
1078 struct kvm_io_device *mmio_dev;
1081 if (vcpu->mmio_read_completed) {
1082 memcpy(val, vcpu->mmio_data, bytes);
1083 vcpu->mmio_read_completed = 0;
1084 return X86EMUL_CONTINUE;
1085 } else if (emulator_read_std(addr, val, bytes, vcpu)
1086 == X86EMUL_CONTINUE)
1087 return X86EMUL_CONTINUE;
1089 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1090 if (gpa == UNMAPPED_GVA)
1091 return X86EMUL_PROPAGATE_FAULT;
1094 * Is this MMIO handled locally?
1096 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1098 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1099 return X86EMUL_CONTINUE;
1102 vcpu->mmio_needed = 1;
1103 vcpu->mmio_phys_addr = gpa;
1104 vcpu->mmio_size = bytes;
1105 vcpu->mmio_is_write = 0;
1107 return X86EMUL_UNHANDLEABLE;
1110 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1111 const void *val, int bytes)
1116 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1118 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1121 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
1122 virt = kmap_atomic(page, KM_USER0);
1123 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1124 memcpy(virt + offset_in_page(gpa), val, bytes);
1125 kunmap_atomic(virt, KM_USER0);
1129 static int emulator_write_emulated_onepage(unsigned long addr,
1132 struct kvm_vcpu *vcpu)
1134 struct kvm_io_device *mmio_dev;
1135 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1137 if (gpa == UNMAPPED_GVA) {
1138 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
1139 return X86EMUL_PROPAGATE_FAULT;
1142 if (emulator_write_phys(vcpu, gpa, val, bytes))
1143 return X86EMUL_CONTINUE;
1146 * Is this MMIO handled locally?
1148 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1150 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1151 return X86EMUL_CONTINUE;
1154 vcpu->mmio_needed = 1;
1155 vcpu->mmio_phys_addr = gpa;
1156 vcpu->mmio_size = bytes;
1157 vcpu->mmio_is_write = 1;
1158 memcpy(vcpu->mmio_data, val, bytes);
1160 return X86EMUL_CONTINUE;
1163 int emulator_write_emulated(unsigned long addr,
1166 struct kvm_vcpu *vcpu)
1168 /* Crossing a page boundary? */
1169 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1172 now = -addr & ~PAGE_MASK;
1173 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1174 if (rc != X86EMUL_CONTINUE)
1180 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1182 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1184 static int emulator_cmpxchg_emulated(unsigned long addr,
1188 struct kvm_vcpu *vcpu)
1190 static int reported;
1194 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1196 return emulator_write_emulated(addr, new, bytes, vcpu);
1199 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1201 return kvm_x86_ops->get_segment_base(vcpu, seg);
1204 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1206 return X86EMUL_CONTINUE;
1209 int emulate_clts(struct kvm_vcpu *vcpu)
1211 vcpu->cr0 &= ~X86_CR0_TS;
1212 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0);
1213 return X86EMUL_CONTINUE;
1216 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1218 struct kvm_vcpu *vcpu = ctxt->vcpu;
1222 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1223 return X86EMUL_CONTINUE;
1225 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1226 return X86EMUL_UNHANDLEABLE;
1230 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1232 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1235 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1237 /* FIXME: better handling */
1238 return X86EMUL_UNHANDLEABLE;
1240 return X86EMUL_CONTINUE;
1243 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1245 static int reported;
1247 unsigned long rip = vcpu->rip;
1248 unsigned long rip_linear;
1250 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1255 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1257 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1258 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1261 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1263 struct x86_emulate_ops emulate_ops = {
1264 .read_std = emulator_read_std,
1265 .write_std = emulator_write_std,
1266 .read_emulated = emulator_read_emulated,
1267 .write_emulated = emulator_write_emulated,
1268 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1271 int emulate_instruction(struct kvm_vcpu *vcpu,
1272 struct kvm_run *run,
1276 struct x86_emulate_ctxt emulate_ctxt;
1280 vcpu->mmio_fault_cr2 = cr2;
1281 kvm_x86_ops->cache_regs(vcpu);
1283 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1285 emulate_ctxt.vcpu = vcpu;
1286 emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1287 emulate_ctxt.cr2 = cr2;
1288 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1289 ? X86EMUL_MODE_REAL : cs_l
1290 ? X86EMUL_MODE_PROT64 : cs_db
1291 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1293 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1294 emulate_ctxt.cs_base = 0;
1295 emulate_ctxt.ds_base = 0;
1296 emulate_ctxt.es_base = 0;
1297 emulate_ctxt.ss_base = 0;
1299 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1300 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1301 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1302 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1305 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1306 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1308 vcpu->mmio_is_write = 0;
1309 vcpu->pio.string = 0;
1310 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1311 if (vcpu->pio.string)
1312 return EMULATE_DO_MMIO;
1314 if ((r || vcpu->mmio_is_write) && run) {
1315 run->exit_reason = KVM_EXIT_MMIO;
1316 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1317 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1318 run->mmio.len = vcpu->mmio_size;
1319 run->mmio.is_write = vcpu->mmio_is_write;
1323 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1324 return EMULATE_DONE;
1325 if (!vcpu->mmio_needed) {
1326 kvm_report_emulation_failure(vcpu, "mmio");
1327 return EMULATE_FAIL;
1329 return EMULATE_DO_MMIO;
1332 kvm_x86_ops->decache_regs(vcpu);
1333 kvm_x86_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1335 if (vcpu->mmio_is_write) {
1336 vcpu->mmio_needed = 0;
1337 return EMULATE_DO_MMIO;
1340 return EMULATE_DONE;
1342 EXPORT_SYMBOL_GPL(emulate_instruction);
1345 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1347 static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1349 DECLARE_WAITQUEUE(wait, current);
1351 add_wait_queue(&vcpu->wq, &wait);
1354 * We will block until either an interrupt or a signal wakes us up
1356 while (!kvm_cpu_has_interrupt(vcpu)
1357 && !signal_pending(current)
1358 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1359 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
1360 set_current_state(TASK_INTERRUPTIBLE);
1366 __set_current_state(TASK_RUNNING);
1367 remove_wait_queue(&vcpu->wq, &wait);
1370 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1372 ++vcpu->stat.halt_exits;
1373 if (irqchip_in_kernel(vcpu->kvm)) {
1374 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1375 kvm_vcpu_block(vcpu);
1376 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1380 vcpu->run->exit_reason = KVM_EXIT_HLT;
1384 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1386 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1388 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1390 kvm_x86_ops->cache_regs(vcpu);
1392 #ifdef CONFIG_X86_64
1393 if (is_long_mode(vcpu)) {
1394 nr = vcpu->regs[VCPU_REGS_RAX];
1395 a0 = vcpu->regs[VCPU_REGS_RDI];
1396 a1 = vcpu->regs[VCPU_REGS_RSI];
1397 a2 = vcpu->regs[VCPU_REGS_RDX];
1398 a3 = vcpu->regs[VCPU_REGS_RCX];
1399 a4 = vcpu->regs[VCPU_REGS_R8];
1400 a5 = vcpu->regs[VCPU_REGS_R9];
1404 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1405 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1406 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1407 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1408 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1409 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1410 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1414 run->hypercall.nr = nr;
1415 run->hypercall.args[0] = a0;
1416 run->hypercall.args[1] = a1;
1417 run->hypercall.args[2] = a2;
1418 run->hypercall.args[3] = a3;
1419 run->hypercall.args[4] = a4;
1420 run->hypercall.args[5] = a5;
1421 run->hypercall.ret = ret;
1422 run->hypercall.longmode = is_long_mode(vcpu);
1423 kvm_x86_ops->decache_regs(vcpu);
1426 vcpu->regs[VCPU_REGS_RAX] = ret;
1427 kvm_x86_ops->decache_regs(vcpu);
1430 EXPORT_SYMBOL_GPL(kvm_hypercall);
1432 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1434 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1437 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1439 struct descriptor_table dt = { limit, base };
1441 kvm_x86_ops->set_gdt(vcpu, &dt);
1444 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1446 struct descriptor_table dt = { limit, base };
1448 kvm_x86_ops->set_idt(vcpu, &dt);
1451 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1452 unsigned long *rflags)
1455 *rflags = kvm_x86_ops->get_rflags(vcpu);
1458 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1460 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
1471 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1476 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1477 unsigned long *rflags)
1481 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1482 *rflags = kvm_x86_ops->get_rflags(vcpu);
1491 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1494 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1499 * Register the para guest with the host:
1501 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1503 struct kvm_vcpu_para_state *para_state;
1504 hpa_t para_state_hpa, hypercall_hpa;
1505 struct page *para_state_page;
1506 unsigned char *hypercall;
1507 gpa_t hypercall_gpa;
1509 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1510 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1513 * Needs to be page aligned:
1515 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1518 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1519 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1520 if (is_error_hpa(para_state_hpa))
1523 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1524 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1525 para_state = kmap(para_state_page);
1527 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1528 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1530 para_state->host_version = KVM_PARA_API_VERSION;
1532 * We cannot support guests that try to register themselves
1533 * with a newer API version than the host supports:
1535 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1536 para_state->ret = -KVM_EINVAL;
1537 goto err_kunmap_skip;
1540 hypercall_gpa = para_state->hypercall_gpa;
1541 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1542 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1543 if (is_error_hpa(hypercall_hpa)) {
1544 para_state->ret = -KVM_EINVAL;
1545 goto err_kunmap_skip;
1548 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1549 vcpu->para_state_page = para_state_page;
1550 vcpu->para_state_gpa = para_state_gpa;
1551 vcpu->hypercall_gpa = hypercall_gpa;
1553 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1554 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1555 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1556 kvm_x86_ops->patch_hypercall(vcpu, hypercall);
1557 kunmap_atomic(hypercall, KM_USER1);
1559 para_state->ret = 0;
1561 kunmap(para_state_page);
1567 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1572 case 0xc0010010: /* SYSCFG */
1573 case 0xc0010015: /* HWCR */
1574 case MSR_IA32_PLATFORM_ID:
1575 case MSR_IA32_P5_MC_ADDR:
1576 case MSR_IA32_P5_MC_TYPE:
1577 case MSR_IA32_MC0_CTL:
1578 case MSR_IA32_MCG_STATUS:
1579 case MSR_IA32_MCG_CAP:
1580 case MSR_IA32_MC0_MISC:
1581 case MSR_IA32_MC0_MISC+4:
1582 case MSR_IA32_MC0_MISC+8:
1583 case MSR_IA32_MC0_MISC+12:
1584 case MSR_IA32_MC0_MISC+16:
1585 case MSR_IA32_UCODE_REV:
1586 case MSR_IA32_PERF_STATUS:
1587 case MSR_IA32_EBL_CR_POWERON:
1588 /* MTRR registers */
1590 case 0x200 ... 0x2ff:
1593 case 0xcd: /* fsb frequency */
1596 case MSR_IA32_APICBASE:
1597 data = kvm_get_apic_base(vcpu);
1599 case MSR_IA32_MISC_ENABLE:
1600 data = vcpu->ia32_misc_enable_msr;
1602 #ifdef CONFIG_X86_64
1604 data = vcpu->shadow_efer;
1608 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1614 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1617 * Reads an msr value (of 'msr_index') into 'pdata'.
1618 * Returns 0 on success, non-0 otherwise.
1619 * Assumes vcpu_load() was already called.
1621 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1623 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1626 #ifdef CONFIG_X86_64
1628 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1630 if (efer & EFER_RESERVED_BITS) {
1631 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1638 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1639 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1644 kvm_x86_ops->set_efer(vcpu, efer);
1647 efer |= vcpu->shadow_efer & EFER_LMA;
1649 vcpu->shadow_efer = efer;
1654 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1657 #ifdef CONFIG_X86_64
1659 set_efer(vcpu, data);
1662 case MSR_IA32_MC0_STATUS:
1663 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1664 __FUNCTION__, data);
1666 case MSR_IA32_MCG_STATUS:
1667 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1668 __FUNCTION__, data);
1670 case MSR_IA32_UCODE_REV:
1671 case MSR_IA32_UCODE_WRITE:
1672 case 0x200 ... 0x2ff: /* MTRRs */
1674 case MSR_IA32_APICBASE:
1675 kvm_set_apic_base(vcpu, data);
1677 case MSR_IA32_MISC_ENABLE:
1678 vcpu->ia32_misc_enable_msr = data;
1681 * This is the 'probe whether the host is KVM' logic:
1683 case MSR_KVM_API_MAGIC:
1684 return vcpu_register_para(vcpu, data);
1687 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
1692 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1695 * Writes msr value into into the appropriate "register".
1696 * Returns 0 on success, non-0 otherwise.
1697 * Assumes vcpu_load() was already called.
1699 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1701 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
1704 void kvm_resched(struct kvm_vcpu *vcpu)
1706 if (!need_resched())
1710 EXPORT_SYMBOL_GPL(kvm_resched);
1712 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1716 struct kvm_cpuid_entry *e, *best;
1718 kvm_x86_ops->cache_regs(vcpu);
1719 function = vcpu->regs[VCPU_REGS_RAX];
1720 vcpu->regs[VCPU_REGS_RAX] = 0;
1721 vcpu->regs[VCPU_REGS_RBX] = 0;
1722 vcpu->regs[VCPU_REGS_RCX] = 0;
1723 vcpu->regs[VCPU_REGS_RDX] = 0;
1725 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1726 e = &vcpu->cpuid_entries[i];
1727 if (e->function == function) {
1732 * Both basic or both extended?
1734 if (((e->function ^ function) & 0x80000000) == 0)
1735 if (!best || e->function > best->function)
1739 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1740 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1741 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1742 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1744 kvm_x86_ops->decache_regs(vcpu);
1745 kvm_x86_ops->skip_emulated_instruction(vcpu);
1747 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1749 static int pio_copy_data(struct kvm_vcpu *vcpu)
1751 void *p = vcpu->pio_data;
1754 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1756 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1759 free_pio_guest_pages(vcpu);
1762 q += vcpu->pio.guest_page_offset;
1763 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1765 memcpy(q, p, bytes);
1767 memcpy(p, q, bytes);
1768 q -= vcpu->pio.guest_page_offset;
1770 free_pio_guest_pages(vcpu);
1774 static int complete_pio(struct kvm_vcpu *vcpu)
1776 struct kvm_pio_request *io = &vcpu->pio;
1780 kvm_x86_ops->cache_regs(vcpu);
1784 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1788 r = pio_copy_data(vcpu);
1790 kvm_x86_ops->cache_regs(vcpu);
1797 delta *= io->cur_count;
1799 * The size of the register should really depend on
1800 * current address size.
1802 vcpu->regs[VCPU_REGS_RCX] -= delta;
1808 vcpu->regs[VCPU_REGS_RDI] += delta;
1810 vcpu->regs[VCPU_REGS_RSI] += delta;
1813 kvm_x86_ops->decache_regs(vcpu);
1815 io->count -= io->cur_count;
1821 static void kernel_pio(struct kvm_io_device *pio_dev,
1822 struct kvm_vcpu *vcpu,
1825 /* TODO: String I/O for in kernel device */
1827 mutex_lock(&vcpu->kvm->lock);
1829 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1833 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1836 mutex_unlock(&vcpu->kvm->lock);
1839 static void pio_string_write(struct kvm_io_device *pio_dev,
1840 struct kvm_vcpu *vcpu)
1842 struct kvm_pio_request *io = &vcpu->pio;
1843 void *pd = vcpu->pio_data;
1846 mutex_lock(&vcpu->kvm->lock);
1847 for (i = 0; i < io->cur_count; i++) {
1848 kvm_iodevice_write(pio_dev, io->port,
1853 mutex_unlock(&vcpu->kvm->lock);
1856 int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1857 int size, unsigned port)
1859 struct kvm_io_device *pio_dev;
1861 vcpu->run->exit_reason = KVM_EXIT_IO;
1862 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1863 vcpu->run->io.size = vcpu->pio.size = size;
1864 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1865 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1866 vcpu->run->io.port = vcpu->pio.port = port;
1868 vcpu->pio.string = 0;
1870 vcpu->pio.guest_page_offset = 0;
1873 kvm_x86_ops->cache_regs(vcpu);
1874 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1875 kvm_x86_ops->decache_regs(vcpu);
1877 kvm_x86_ops->skip_emulated_instruction(vcpu);
1879 pio_dev = vcpu_find_pio_dev(vcpu, port);
1881 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1887 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1889 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1890 int size, unsigned long count, int down,
1891 gva_t address, int rep, unsigned port)
1893 unsigned now, in_page;
1897 struct kvm_io_device *pio_dev;
1899 vcpu->run->exit_reason = KVM_EXIT_IO;
1900 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1901 vcpu->run->io.size = vcpu->pio.size = size;
1902 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1903 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1904 vcpu->run->io.port = vcpu->pio.port = port;
1906 vcpu->pio.string = 1;
1907 vcpu->pio.down = down;
1908 vcpu->pio.guest_page_offset = offset_in_page(address);
1909 vcpu->pio.rep = rep;
1912 kvm_x86_ops->skip_emulated_instruction(vcpu);
1917 in_page = PAGE_SIZE - offset_in_page(address);
1919 in_page = offset_in_page(address) + size;
1920 now = min(count, (unsigned long)in_page / size);
1923 * String I/O straddles page boundary. Pin two guest pages
1924 * so that we satisfy atomicity constraints. Do just one
1925 * transaction to avoid complexity.
1932 * String I/O in reverse. Yuck. Kill the guest, fix later.
1934 pr_unimpl(vcpu, "guest string pio down\n");
1938 vcpu->run->io.count = now;
1939 vcpu->pio.cur_count = now;
1941 if (vcpu->pio.cur_count == vcpu->pio.count)
1942 kvm_x86_ops->skip_emulated_instruction(vcpu);
1944 for (i = 0; i < nr_pages; ++i) {
1945 mutex_lock(&vcpu->kvm->lock);
1946 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1949 vcpu->pio.guest_pages[i] = page;
1950 mutex_unlock(&vcpu->kvm->lock);
1953 free_pio_guest_pages(vcpu);
1958 pio_dev = vcpu_find_pio_dev(vcpu, port);
1959 if (!vcpu->pio.in) {
1960 /* string PIO write */
1961 ret = pio_copy_data(vcpu);
1962 if (ret >= 0 && pio_dev) {
1963 pio_string_write(pio_dev, vcpu);
1965 if (vcpu->pio.count == 0)
1969 pr_unimpl(vcpu, "no string pio read support yet, "
1970 "port %x size %d count %ld\n",
1975 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
1978 * Check if userspace requested an interrupt window, and that the
1979 * interrupt window is open.
1981 * No need to exit to userspace if we already have an interrupt queued.
1983 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
1984 struct kvm_run *kvm_run)
1986 return (!vcpu->irq_summary &&
1987 kvm_run->request_interrupt_window &&
1988 vcpu->interrupt_window_open &&
1989 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
1992 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1993 struct kvm_run *kvm_run)
1995 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
1996 kvm_run->cr8 = get_cr8(vcpu);
1997 kvm_run->apic_base = kvm_get_apic_base(vcpu);
1998 if (irqchip_in_kernel(vcpu->kvm))
1999 kvm_run->ready_for_interrupt_injection = 1;
2001 kvm_run->ready_for_interrupt_injection =
2002 (vcpu->interrupt_window_open &&
2003 vcpu->irq_summary == 0);
2006 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2010 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2011 printk("vcpu %d received sipi with vector # %x\n",
2012 vcpu->vcpu_id, vcpu->sipi_vector);
2013 kvm_lapic_reset(vcpu);
2014 kvm_x86_ops->vcpu_reset(vcpu);
2015 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2019 if (vcpu->guest_debug.enabled)
2020 kvm_x86_ops->guest_debug_pre(vcpu);
2023 r = kvm_mmu_reload(vcpu);
2029 kvm_x86_ops->prepare_guest_switch(vcpu);
2030 kvm_load_guest_fpu(vcpu);
2032 local_irq_disable();
2034 if (signal_pending(current)) {
2038 kvm_run->exit_reason = KVM_EXIT_INTR;
2039 ++vcpu->stat.signal_exits;
2043 if (irqchip_in_kernel(vcpu->kvm))
2044 kvm_x86_ops->inject_pending_irq(vcpu);
2045 else if (!vcpu->mmio_read_completed)
2046 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2048 vcpu->guest_mode = 1;
2051 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2052 kvm_x86_ops->tlb_flush(vcpu);
2054 kvm_x86_ops->run(vcpu, kvm_run);
2056 vcpu->guest_mode = 0;
2064 * Profile KVM exit RIPs:
2066 if (unlikely(prof_on == KVM_PROFILING)) {
2067 kvm_x86_ops->cache_regs(vcpu);
2068 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2071 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2074 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2076 kvm_run->exit_reason = KVM_EXIT_INTR;
2077 ++vcpu->stat.request_irq_exits;
2080 if (!need_resched()) {
2081 ++vcpu->stat.light_exits;
2092 post_kvm_run_save(vcpu, kvm_run);
2098 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2105 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2106 kvm_vcpu_block(vcpu);
2111 if (vcpu->sigset_active)
2112 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2114 /* re-sync apic's tpr */
2115 if (!irqchip_in_kernel(vcpu->kvm))
2116 set_cr8(vcpu, kvm_run->cr8);
2118 if (vcpu->pio.cur_count) {
2119 r = complete_pio(vcpu);
2124 if (vcpu->mmio_needed) {
2125 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2126 vcpu->mmio_read_completed = 1;
2127 vcpu->mmio_needed = 0;
2128 r = emulate_instruction(vcpu, kvm_run,
2129 vcpu->mmio_fault_cr2, 0);
2130 if (r == EMULATE_DO_MMIO) {
2132 * Read-modify-write. Back to userspace.
2139 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2140 kvm_x86_ops->cache_regs(vcpu);
2141 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
2142 kvm_x86_ops->decache_regs(vcpu);
2145 r = __vcpu_run(vcpu, kvm_run);
2148 if (vcpu->sigset_active)
2149 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2155 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2156 struct kvm_regs *regs)
2160 kvm_x86_ops->cache_regs(vcpu);
2162 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2163 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2164 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2165 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2166 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2167 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2168 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2169 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
2170 #ifdef CONFIG_X86_64
2171 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2172 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2173 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2174 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2175 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2176 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2177 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2178 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2181 regs->rip = vcpu->rip;
2182 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2185 * Don't leak debug flags in case they were set for guest debugging
2187 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2188 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2195 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2196 struct kvm_regs *regs)
2200 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2201 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2202 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2203 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2204 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2205 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2206 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2207 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
2208 #ifdef CONFIG_X86_64
2209 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2210 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2211 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2212 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2213 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2214 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2215 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2216 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2219 vcpu->rip = regs->rip;
2220 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2222 kvm_x86_ops->decache_regs(vcpu);
2229 static void get_segment(struct kvm_vcpu *vcpu,
2230 struct kvm_segment *var, int seg)
2232 return kvm_x86_ops->get_segment(vcpu, var, seg);
2235 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2236 struct kvm_sregs *sregs)
2238 struct descriptor_table dt;
2243 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2244 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2245 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2246 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2247 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2248 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2250 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2251 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2253 kvm_x86_ops->get_idt(vcpu, &dt);
2254 sregs->idt.limit = dt.limit;
2255 sregs->idt.base = dt.base;
2256 kvm_x86_ops->get_gdt(vcpu, &dt);
2257 sregs->gdt.limit = dt.limit;
2258 sregs->gdt.base = dt.base;
2260 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2261 sregs->cr0 = vcpu->cr0;
2262 sregs->cr2 = vcpu->cr2;
2263 sregs->cr3 = vcpu->cr3;
2264 sregs->cr4 = vcpu->cr4;
2265 sregs->cr8 = get_cr8(vcpu);
2266 sregs->efer = vcpu->shadow_efer;
2267 sregs->apic_base = kvm_get_apic_base(vcpu);
2269 if (irqchip_in_kernel(vcpu->kvm)) {
2270 memset(sregs->interrupt_bitmap, 0,
2271 sizeof sregs->interrupt_bitmap);
2272 pending_vec = kvm_x86_ops->get_irq(vcpu);
2273 if (pending_vec >= 0)
2274 set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
2276 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2277 sizeof sregs->interrupt_bitmap);
2284 static void set_segment(struct kvm_vcpu *vcpu,
2285 struct kvm_segment *var, int seg)
2287 return kvm_x86_ops->set_segment(vcpu, var, seg);
2290 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2291 struct kvm_sregs *sregs)
2293 int mmu_reset_needed = 0;
2294 int i, pending_vec, max_bits;
2295 struct descriptor_table dt;
2299 dt.limit = sregs->idt.limit;
2300 dt.base = sregs->idt.base;
2301 kvm_x86_ops->set_idt(vcpu, &dt);
2302 dt.limit = sregs->gdt.limit;
2303 dt.base = sregs->gdt.base;
2304 kvm_x86_ops->set_gdt(vcpu, &dt);
2306 vcpu->cr2 = sregs->cr2;
2307 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2308 vcpu->cr3 = sregs->cr3;
2310 set_cr8(vcpu, sregs->cr8);
2312 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2313 #ifdef CONFIG_X86_64
2314 kvm_x86_ops->set_efer(vcpu, sregs->efer);
2316 kvm_set_apic_base(vcpu, sregs->apic_base);
2318 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2320 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2321 vcpu->cr0 = sregs->cr0;
2322 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
2324 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2325 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2326 if (!is_long_mode(vcpu) && is_pae(vcpu))
2327 load_pdptrs(vcpu, vcpu->cr3);
2329 if (mmu_reset_needed)
2330 kvm_mmu_reset_context(vcpu);
2332 if (!irqchip_in_kernel(vcpu->kvm)) {
2333 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2334 sizeof vcpu->irq_pending);
2335 vcpu->irq_summary = 0;
2336 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2337 if (vcpu->irq_pending[i])
2338 __set_bit(i, &vcpu->irq_summary);
2340 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2341 pending_vec = find_first_bit(
2342 (const unsigned long *)sregs->interrupt_bitmap,
2344 /* Only pending external irq is handled here */
2345 if (pending_vec < max_bits) {
2346 kvm_x86_ops->set_irq(vcpu, pending_vec);
2347 printk("Set back pending irq %d\n", pending_vec);
2351 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2352 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2353 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2354 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2355 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2356 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2358 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2359 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2366 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2368 struct kvm_segment cs;
2370 get_segment(vcpu, &cs, VCPU_SREG_CS);
2374 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2377 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2378 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2380 * This list is modified at module load time to reflect the
2381 * capabilities of the host cpu.
2383 static u32 msrs_to_save[] = {
2384 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2386 #ifdef CONFIG_X86_64
2387 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2389 MSR_IA32_TIME_STAMP_COUNTER,
2392 static unsigned num_msrs_to_save;
2394 static u32 emulated_msrs[] = {
2395 MSR_IA32_MISC_ENABLE,
2398 static __init void kvm_init_msr_list(void)
2403 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2404 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2407 msrs_to_save[j] = msrs_to_save[i];
2410 num_msrs_to_save = j;
2414 * Adapt set_msr() to msr_io()'s calling convention
2416 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2418 return kvm_set_msr(vcpu, index, *data);
2422 * Read or write a bunch of msrs. All parameters are kernel addresses.
2424 * @return number of msrs set successfully.
2426 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2427 struct kvm_msr_entry *entries,
2428 int (*do_msr)(struct kvm_vcpu *vcpu,
2429 unsigned index, u64 *data))
2435 for (i = 0; i < msrs->nmsrs; ++i)
2436 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2445 * Read or write a bunch of msrs. Parameters are user addresses.
2447 * @return number of msrs set successfully.
2449 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2450 int (*do_msr)(struct kvm_vcpu *vcpu,
2451 unsigned index, u64 *data),
2454 struct kvm_msrs msrs;
2455 struct kvm_msr_entry *entries;
2460 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2464 if (msrs.nmsrs >= MAX_IO_MSRS)
2468 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2469 entries = vmalloc(size);
2474 if (copy_from_user(entries, user_msrs->entries, size))
2477 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2482 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2494 * Translate a guest virtual address to a guest physical address.
2496 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2497 struct kvm_translation *tr)
2499 unsigned long vaddr = tr->linear_address;
2503 mutex_lock(&vcpu->kvm->lock);
2504 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2505 tr->physical_address = gpa;
2506 tr->valid = gpa != UNMAPPED_GVA;
2509 mutex_unlock(&vcpu->kvm->lock);
2515 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2516 struct kvm_interrupt *irq)
2518 if (irq->irq < 0 || irq->irq >= 256)
2520 if (irqchip_in_kernel(vcpu->kvm))
2524 set_bit(irq->irq, vcpu->irq_pending);
2525 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2532 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2533 struct kvm_debug_guest *dbg)
2539 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2546 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2547 unsigned long address,
2550 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2551 unsigned long pgoff;
2554 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2556 page = virt_to_page(vcpu->run);
2557 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2558 page = virt_to_page(vcpu->pio_data);
2560 return NOPAGE_SIGBUS;
2563 *type = VM_FAULT_MINOR;
2568 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2569 .nopage = kvm_vcpu_nopage,
2572 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2574 vma->vm_ops = &kvm_vcpu_vm_ops;
2578 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2580 struct kvm_vcpu *vcpu = filp->private_data;
2582 fput(vcpu->kvm->filp);
2586 static struct file_operations kvm_vcpu_fops = {
2587 .release = kvm_vcpu_release,
2588 .unlocked_ioctl = kvm_vcpu_ioctl,
2589 .compat_ioctl = kvm_vcpu_ioctl,
2590 .mmap = kvm_vcpu_mmap,
2594 * Allocates an inode for the vcpu.
2596 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2599 struct inode *inode;
2602 r = anon_inode_getfd(&fd, &inode, &file,
2603 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2606 atomic_inc(&vcpu->kvm->filp->f_count);
2611 * Creates some virtual cpus. Good luck creating more than one.
2613 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2616 struct kvm_vcpu *vcpu;
2621 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
2623 return PTR_ERR(vcpu);
2625 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2627 /* We do fxsave: this must be aligned. */
2628 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2631 r = kvm_mmu_setup(vcpu);
2636 mutex_lock(&kvm->lock);
2637 if (kvm->vcpus[n]) {
2639 mutex_unlock(&kvm->lock);
2642 kvm->vcpus[n] = vcpu;
2643 mutex_unlock(&kvm->lock);
2645 /* Now it's all set up, let userspace reach it */
2646 r = create_vcpu_fd(vcpu);
2652 mutex_lock(&kvm->lock);
2653 kvm->vcpus[n] = NULL;
2654 mutex_unlock(&kvm->lock);
2658 kvm_mmu_unload(vcpu);
2662 kvm_x86_ops->vcpu_free(vcpu);
2666 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2670 struct kvm_cpuid_entry *e, *entry;
2672 rdmsrl(MSR_EFER, efer);
2674 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2675 e = &vcpu->cpuid_entries[i];
2676 if (e->function == 0x80000001) {
2681 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
2682 entry->edx &= ~(1 << 20);
2683 printk(KERN_INFO "kvm: guest NX capability removed\n");
2687 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2688 struct kvm_cpuid *cpuid,
2689 struct kvm_cpuid_entry __user *entries)
2694 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2697 if (copy_from_user(&vcpu->cpuid_entries, entries,
2698 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2700 vcpu->cpuid_nent = cpuid->nent;
2701 cpuid_fix_nx_cap(vcpu);
2708 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2711 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2712 vcpu->sigset_active = 1;
2713 vcpu->sigset = *sigset;
2715 vcpu->sigset_active = 0;
2720 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2721 * we have asm/x86/processor.h
2732 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2733 #ifdef CONFIG_X86_64
2734 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2736 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2740 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2742 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2746 memcpy(fpu->fpr, fxsave->st_space, 128);
2747 fpu->fcw = fxsave->cwd;
2748 fpu->fsw = fxsave->swd;
2749 fpu->ftwx = fxsave->twd;
2750 fpu->last_opcode = fxsave->fop;
2751 fpu->last_ip = fxsave->rip;
2752 fpu->last_dp = fxsave->rdp;
2753 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2760 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2762 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2766 memcpy(fxsave->st_space, fpu->fpr, 128);
2767 fxsave->cwd = fpu->fcw;
2768 fxsave->swd = fpu->fsw;
2769 fxsave->twd = fpu->ftwx;
2770 fxsave->fop = fpu->last_opcode;
2771 fxsave->rip = fpu->last_ip;
2772 fxsave->rdp = fpu->last_dp;
2773 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2780 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2781 struct kvm_lapic_state *s)
2784 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2790 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2791 struct kvm_lapic_state *s)
2794 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2795 kvm_apic_post_state_restore(vcpu);
2801 static long kvm_vcpu_ioctl(struct file *filp,
2802 unsigned int ioctl, unsigned long arg)
2804 struct kvm_vcpu *vcpu = filp->private_data;
2805 void __user *argp = (void __user *)arg;
2813 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2815 case KVM_GET_REGS: {
2816 struct kvm_regs kvm_regs;
2818 memset(&kvm_regs, 0, sizeof kvm_regs);
2819 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2823 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2828 case KVM_SET_REGS: {
2829 struct kvm_regs kvm_regs;
2832 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2834 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2840 case KVM_GET_SREGS: {
2841 struct kvm_sregs kvm_sregs;
2843 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2844 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2848 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2853 case KVM_SET_SREGS: {
2854 struct kvm_sregs kvm_sregs;
2857 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2859 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2865 case KVM_TRANSLATE: {
2866 struct kvm_translation tr;
2869 if (copy_from_user(&tr, argp, sizeof tr))
2871 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2875 if (copy_to_user(argp, &tr, sizeof tr))
2880 case KVM_INTERRUPT: {
2881 struct kvm_interrupt irq;
2884 if (copy_from_user(&irq, argp, sizeof irq))
2886 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2892 case KVM_DEBUG_GUEST: {
2893 struct kvm_debug_guest dbg;
2896 if (copy_from_user(&dbg, argp, sizeof dbg))
2898 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2905 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2908 r = msr_io(vcpu, argp, do_set_msr, 0);
2910 case KVM_SET_CPUID: {
2911 struct kvm_cpuid __user *cpuid_arg = argp;
2912 struct kvm_cpuid cpuid;
2915 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2917 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2922 case KVM_SET_SIGNAL_MASK: {
2923 struct kvm_signal_mask __user *sigmask_arg = argp;
2924 struct kvm_signal_mask kvm_sigmask;
2925 sigset_t sigset, *p;
2930 if (copy_from_user(&kvm_sigmask, argp,
2931 sizeof kvm_sigmask))
2934 if (kvm_sigmask.len != sizeof sigset)
2937 if (copy_from_user(&sigset, sigmask_arg->sigset,
2942 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2948 memset(&fpu, 0, sizeof fpu);
2949 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2953 if (copy_to_user(argp, &fpu, sizeof fpu))
2962 if (copy_from_user(&fpu, argp, sizeof fpu))
2964 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2970 case KVM_GET_LAPIC: {
2971 struct kvm_lapic_state lapic;
2973 memset(&lapic, 0, sizeof lapic);
2974 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
2978 if (copy_to_user(argp, &lapic, sizeof lapic))
2983 case KVM_SET_LAPIC: {
2984 struct kvm_lapic_state lapic;
2987 if (copy_from_user(&lapic, argp, sizeof lapic))
2989 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
3002 static long kvm_vm_ioctl(struct file *filp,
3003 unsigned int ioctl, unsigned long arg)
3005 struct kvm *kvm = filp->private_data;
3006 void __user *argp = (void __user *)arg;
3010 case KVM_CREATE_VCPU:
3011 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
3015 case KVM_SET_MEMORY_REGION: {
3016 struct kvm_memory_region kvm_mem;
3019 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
3021 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
3026 case KVM_GET_DIRTY_LOG: {
3027 struct kvm_dirty_log log;
3030 if (copy_from_user(&log, argp, sizeof log))
3032 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
3037 case KVM_SET_MEMORY_ALIAS: {
3038 struct kvm_memory_alias alias;
3041 if (copy_from_user(&alias, argp, sizeof alias))
3043 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3048 case KVM_CREATE_IRQCHIP:
3050 kvm->vpic = kvm_create_pic(kvm);
3052 r = kvm_ioapic_init(kvm);
3062 case KVM_IRQ_LINE: {
3063 struct kvm_irq_level irq_event;
3066 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3068 if (irqchip_in_kernel(kvm)) {
3069 mutex_lock(&kvm->lock);
3070 if (irq_event.irq < 16)
3071 kvm_pic_set_irq(pic_irqchip(kvm),
3074 kvm_ioapic_set_irq(kvm->vioapic,
3077 mutex_unlock(&kvm->lock);
3082 case KVM_GET_IRQCHIP: {
3083 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3084 struct kvm_irqchip chip;
3087 if (copy_from_user(&chip, argp, sizeof chip))
3090 if (!irqchip_in_kernel(kvm))
3092 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3096 if (copy_to_user(argp, &chip, sizeof chip))
3101 case KVM_SET_IRQCHIP: {
3102 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3103 struct kvm_irqchip chip;
3106 if (copy_from_user(&chip, argp, sizeof chip))
3109 if (!irqchip_in_kernel(kvm))
3111 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3124 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3125 unsigned long address,
3128 struct kvm *kvm = vma->vm_file->private_data;
3129 unsigned long pgoff;
3132 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
3133 page = gfn_to_page(kvm, pgoff);
3135 return NOPAGE_SIGBUS;
3138 *type = VM_FAULT_MINOR;
3143 static struct vm_operations_struct kvm_vm_vm_ops = {
3144 .nopage = kvm_vm_nopage,
3147 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3149 vma->vm_ops = &kvm_vm_vm_ops;
3153 static struct file_operations kvm_vm_fops = {
3154 .release = kvm_vm_release,
3155 .unlocked_ioctl = kvm_vm_ioctl,
3156 .compat_ioctl = kvm_vm_ioctl,
3157 .mmap = kvm_vm_mmap,
3160 static int kvm_dev_ioctl_create_vm(void)
3163 struct inode *inode;
3167 kvm = kvm_create_vm();
3169 return PTR_ERR(kvm);
3170 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3172 kvm_destroy_vm(kvm);
3181 static long kvm_dev_ioctl(struct file *filp,
3182 unsigned int ioctl, unsigned long arg)
3184 void __user *argp = (void __user *)arg;
3188 case KVM_GET_API_VERSION:
3192 r = KVM_API_VERSION;
3198 r = kvm_dev_ioctl_create_vm();
3200 case KVM_GET_MSR_INDEX_LIST: {
3201 struct kvm_msr_list __user *user_msr_list = argp;
3202 struct kvm_msr_list msr_list;
3206 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3209 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
3210 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3213 if (n < num_msrs_to_save)
3216 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3217 num_msrs_to_save * sizeof(u32)))
3219 if (copy_to_user(user_msr_list->indices
3220 + num_msrs_to_save * sizeof(u32),
3222 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
3227 case KVM_CHECK_EXTENSION: {
3228 int ext = (long)argp;
3231 case KVM_CAP_IRQCHIP:
3241 case KVM_GET_VCPU_MMAP_SIZE:
3254 static struct file_operations kvm_chardev_ops = {
3255 .unlocked_ioctl = kvm_dev_ioctl,
3256 .compat_ioctl = kvm_dev_ioctl,
3259 static struct miscdevice kvm_dev = {
3266 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3269 static void decache_vcpus_on_cpu(int cpu)
3272 struct kvm_vcpu *vcpu;
3275 spin_lock(&kvm_lock);
3276 list_for_each_entry(vm, &vm_list, vm_list)
3277 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3278 vcpu = vm->vcpus[i];
3282 * If the vcpu is locked, then it is running on some
3283 * other cpu and therefore it is not cached on the
3286 * If it's not locked, check the last cpu it executed
3289 if (mutex_trylock(&vcpu->mutex)) {
3290 if (vcpu->cpu == cpu) {
3291 kvm_x86_ops->vcpu_decache(vcpu);
3294 mutex_unlock(&vcpu->mutex);
3297 spin_unlock(&kvm_lock);
3300 static void hardware_enable(void *junk)
3302 int cpu = raw_smp_processor_id();
3304 if (cpu_isset(cpu, cpus_hardware_enabled))
3306 cpu_set(cpu, cpus_hardware_enabled);
3307 kvm_x86_ops->hardware_enable(NULL);
3310 static void hardware_disable(void *junk)
3312 int cpu = raw_smp_processor_id();
3314 if (!cpu_isset(cpu, cpus_hardware_enabled))
3316 cpu_clear(cpu, cpus_hardware_enabled);
3317 decache_vcpus_on_cpu(cpu);
3318 kvm_x86_ops->hardware_disable(NULL);
3321 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3328 case CPU_DYING_FROZEN:
3329 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3331 hardware_disable(NULL);
3333 case CPU_UP_CANCELED:
3334 case CPU_UP_CANCELED_FROZEN:
3335 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3337 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
3340 case CPU_ONLINE_FROZEN:
3341 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3343 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
3349 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3352 if (val == SYS_RESTART) {
3354 * Some (well, at least mine) BIOSes hang on reboot if
3357 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3358 on_each_cpu(hardware_disable, NULL, 0, 1);
3363 static struct notifier_block kvm_reboot_notifier = {
3364 .notifier_call = kvm_reboot,
3368 void kvm_io_bus_init(struct kvm_io_bus *bus)
3370 memset(bus, 0, sizeof(*bus));
3373 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3377 for (i = 0; i < bus->dev_count; i++) {
3378 struct kvm_io_device *pos = bus->devs[i];
3380 kvm_iodevice_destructor(pos);
3384 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3388 for (i = 0; i < bus->dev_count; i++) {
3389 struct kvm_io_device *pos = bus->devs[i];
3391 if (pos->in_range(pos, addr))
3398 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3400 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3402 bus->devs[bus->dev_count++] = dev;
3405 static struct notifier_block kvm_cpu_notifier = {
3406 .notifier_call = kvm_cpu_hotplug,
3407 .priority = 20, /* must be > scheduler priority */
3410 static u64 stat_get(void *_offset)
3412 unsigned offset = (long)_offset;
3415 struct kvm_vcpu *vcpu;
3418 spin_lock(&kvm_lock);
3419 list_for_each_entry(kvm, &vm_list, vm_list)
3420 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3421 vcpu = kvm->vcpus[i];
3423 total += *(u32 *)((void *)vcpu + offset);
3425 spin_unlock(&kvm_lock);
3429 DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
3431 static __init void kvm_init_debug(void)
3433 struct kvm_stats_debugfs_item *p;
3435 debugfs_dir = debugfs_create_dir("kvm", NULL);
3436 for (p = debugfs_entries; p->name; ++p)
3437 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3438 (void *)(long)p->offset,
3442 static void kvm_exit_debug(void)
3444 struct kvm_stats_debugfs_item *p;
3446 for (p = debugfs_entries; p->name; ++p)
3447 debugfs_remove(p->dentry);
3448 debugfs_remove(debugfs_dir);
3451 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3453 hardware_disable(NULL);
3457 static int kvm_resume(struct sys_device *dev)
3459 hardware_enable(NULL);
3463 static struct sysdev_class kvm_sysdev_class = {
3464 set_kset_name("kvm"),
3465 .suspend = kvm_suspend,
3466 .resume = kvm_resume,
3469 static struct sys_device kvm_sysdev = {
3471 .cls = &kvm_sysdev_class,
3474 hpa_t bad_page_address;
3477 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3479 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3482 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3484 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3486 kvm_x86_ops->vcpu_load(vcpu, cpu);
3489 static void kvm_sched_out(struct preempt_notifier *pn,
3490 struct task_struct *next)
3492 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3494 kvm_x86_ops->vcpu_put(vcpu);
3497 int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
3498 struct module *module)
3504 printk(KERN_ERR "kvm: already loaded the other module\n");
3508 if (!ops->cpu_has_kvm_support()) {
3509 printk(KERN_ERR "kvm: no hardware support\n");
3512 if (ops->disabled_by_bios()) {
3513 printk(KERN_ERR "kvm: disabled by bios\n");
3519 r = kvm_x86_ops->hardware_setup();
3523 for_each_online_cpu(cpu) {
3524 smp_call_function_single(cpu,
3525 kvm_x86_ops->check_processor_compatibility,
3531 on_each_cpu(hardware_enable, NULL, 0, 1);
3532 r = register_cpu_notifier(&kvm_cpu_notifier);
3535 register_reboot_notifier(&kvm_reboot_notifier);
3537 r = sysdev_class_register(&kvm_sysdev_class);
3541 r = sysdev_register(&kvm_sysdev);
3545 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3546 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3547 __alignof__(struct kvm_vcpu), 0, 0);
3548 if (!kvm_vcpu_cache) {
3553 kvm_chardev_ops.owner = module;
3555 r = misc_register(&kvm_dev);
3557 printk (KERN_ERR "kvm: misc device register failed\n");
3561 kvm_preempt_ops.sched_in = kvm_sched_in;
3562 kvm_preempt_ops.sched_out = kvm_sched_out;
3567 kmem_cache_destroy(kvm_vcpu_cache);
3569 sysdev_unregister(&kvm_sysdev);
3571 sysdev_class_unregister(&kvm_sysdev_class);
3573 unregister_reboot_notifier(&kvm_reboot_notifier);
3574 unregister_cpu_notifier(&kvm_cpu_notifier);
3576 on_each_cpu(hardware_disable, NULL, 0, 1);
3578 kvm_x86_ops->hardware_unsetup();
3584 void kvm_exit_x86(void)
3586 misc_deregister(&kvm_dev);
3587 kmem_cache_destroy(kvm_vcpu_cache);
3588 sysdev_unregister(&kvm_sysdev);
3589 sysdev_class_unregister(&kvm_sysdev_class);
3590 unregister_reboot_notifier(&kvm_reboot_notifier);
3591 unregister_cpu_notifier(&kvm_cpu_notifier);
3592 on_each_cpu(hardware_disable, NULL, 0, 1);
3593 kvm_x86_ops->hardware_unsetup();
3597 static __init int kvm_init(void)
3599 static struct page *bad_page;
3602 r = kvm_mmu_module_init();
3608 kvm_init_msr_list();
3610 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3615 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3616 memset(__va(bad_page_address), 0, PAGE_SIZE);
3622 kvm_mmu_module_exit();
3627 static __exit void kvm_exit(void)
3630 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3631 kvm_mmu_module_exit();
3634 module_init(kvm_init)
3635 module_exit(kvm_exit)
3637 EXPORT_SYMBOL_GPL(kvm_init_x86);
3638 EXPORT_SYMBOL_GPL(kvm_exit_x86);