|
@@ -1,8 +1,7 @@
|
|
|
/*
|
|
|
* Kernel-based Virtual Machine driver for Linux
|
|
|
*
|
|
|
- * This module enables machines with Intel VT-x extensions to run virtual
|
|
|
- * machines without emulation or binary translation.
|
|
|
+ * derived from drivers/kvm/kvm_main.c
|
|
|
*
|
|
|
* Copyright (C) 2006 Qumranet, Inc.
|
|
|
*
|
|
@@ -15,80 +14,22 @@
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
-#include "kvm.h"
|
|
|
-#include "x86_emulate.h"
|
|
|
+#include <linux/kvm_host.h>
|
|
|
#include "segment_descriptor.h"
|
|
|
#include "irq.h"
|
|
|
+#include "mmu.h"
|
|
|
|
|
|
#include <linux/kvm.h>
|
|
|
-#include <linux/module.h>
|
|
|
-#include <linux/errno.h>
|
|
|
-#include <linux/percpu.h>
|
|
|
-#include <linux/gfp.h>
|
|
|
-#include <linux/mm.h>
|
|
|
-#include <linux/miscdevice.h>
|
|
|
+#include <linux/fs.h>
|
|
|
#include <linux/vmalloc.h>
|
|
|
-#include <linux/reboot.h>
|
|
|
-#include <linux/debugfs.h>
|
|
|
+#include <linux/module.h>
|
|
|
+#include <linux/mman.h>
|
|
|
#include <linux/highmem.h>
|
|
|
-#include <linux/file.h>
|
|
|
-#include <linux/sysdev.h>
|
|
|
-#include <linux/cpu.h>
|
|
|
-#include <linux/sched.h>
|
|
|
-#include <linux/cpumask.h>
|
|
|
-#include <linux/smp.h>
|
|
|
-#include <linux/anon_inodes.h>
|
|
|
-#include <linux/profile.h>
|
|
|
-
|
|
|
-#include <asm/processor.h>
|
|
|
-#include <asm/msr.h>
|
|
|
-#include <asm/io.h>
|
|
|
-#include <asm/uaccess.h>
|
|
|
-#include <asm/desc.h>
|
|
|
-
|
|
|
-MODULE_AUTHOR("Qumranet");
|
|
|
-MODULE_LICENSE("GPL");
|
|
|
-
|
|
|
-static DEFINE_SPINLOCK(kvm_lock);
|
|
|
-static LIST_HEAD(vm_list);
|
|
|
-
|
|
|
-static cpumask_t cpus_hardware_enabled;
|
|
|
-
|
|
|
-struct kvm_x86_ops *kvm_x86_ops;
|
|
|
-struct kmem_cache *kvm_vcpu_cache;
|
|
|
-EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
|
|
|
-
|
|
|
-static __read_mostly struct preempt_ops kvm_preempt_ops;
|
|
|
-
|
|
|
-#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
|
|
|
-
|
|
|
-static struct kvm_stats_debugfs_item {
|
|
|
- const char *name;
|
|
|
- int offset;
|
|
|
- struct dentry *dentry;
|
|
|
-} debugfs_entries[] = {
|
|
|
- { "pf_fixed", STAT_OFFSET(pf_fixed) },
|
|
|
- { "pf_guest", STAT_OFFSET(pf_guest) },
|
|
|
- { "tlb_flush", STAT_OFFSET(tlb_flush) },
|
|
|
- { "invlpg", STAT_OFFSET(invlpg) },
|
|
|
- { "exits", STAT_OFFSET(exits) },
|
|
|
- { "io_exits", STAT_OFFSET(io_exits) },
|
|
|
- { "mmio_exits", STAT_OFFSET(mmio_exits) },
|
|
|
- { "signal_exits", STAT_OFFSET(signal_exits) },
|
|
|
- { "irq_window", STAT_OFFSET(irq_window_exits) },
|
|
|
- { "halt_exits", STAT_OFFSET(halt_exits) },
|
|
|
- { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
|
|
|
- { "request_irq", STAT_OFFSET(request_irq_exits) },
|
|
|
- { "irq_exits", STAT_OFFSET(irq_exits) },
|
|
|
- { "light_exits", STAT_OFFSET(light_exits) },
|
|
|
- { "efer_reload", STAT_OFFSET(efer_reload) },
|
|
|
- { NULL }
|
|
|
-};
|
|
|
|
|
|
-static struct dentry *debugfs_dir;
|
|
|
+#include <asm/uaccess.h>
|
|
|
+#include <asm/msr.h>
|
|
|
|
|
|
#define MAX_IO_MSRS 256
|
|
|
-
|
|
|
#define CR0_RESERVED_BITS \
|
|
|
(~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
|
|
|
| X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
|
|
@@ -102,317 +43,151 @@ static struct dentry *debugfs_dir;
|
|
|
#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
|
|
|
#define EFER_RESERVED_BITS 0xfffffffffffff2fe
|
|
|
|
|
|
-#ifdef CONFIG_X86_64
|
|
|
-// LDT or TSS descriptor in the GDT. 16 bytes.
|
|
|
-struct segment_descriptor_64 {
|
|
|
- struct segment_descriptor s;
|
|
|
- u32 base_higher;
|
|
|
- u32 pad_zero;
|
|
|
-};
|
|
|
+#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
|
|
|
+#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
|
|
|
|
|
|
-#endif
|
|
|
+struct kvm_x86_ops *kvm_x86_ops;
|
|
|
+
|
|
|
+struct kvm_stats_debugfs_item debugfs_entries[] = {
|
|
|
+ { "pf_fixed", VCPU_STAT(pf_fixed) },
|
|
|
+ { "pf_guest", VCPU_STAT(pf_guest) },
|
|
|
+ { "tlb_flush", VCPU_STAT(tlb_flush) },
|
|
|
+ { "invlpg", VCPU_STAT(invlpg) },
|
|
|
+ { "exits", VCPU_STAT(exits) },
|
|
|
+ { "io_exits", VCPU_STAT(io_exits) },
|
|
|
+ { "mmio_exits", VCPU_STAT(mmio_exits) },
|
|
|
+ { "signal_exits", VCPU_STAT(signal_exits) },
|
|
|
+ { "irq_window", VCPU_STAT(irq_window_exits) },
|
|
|
+ { "halt_exits", VCPU_STAT(halt_exits) },
|
|
|
+ { "halt_wakeup", VCPU_STAT(halt_wakeup) },
|
|
|
+ { "request_irq", VCPU_STAT(request_irq_exits) },
|
|
|
+ { "irq_exits", VCPU_STAT(irq_exits) },
|
|
|
+ { "host_state_reload", VCPU_STAT(host_state_reload) },
|
|
|
+ { "efer_reload", VCPU_STAT(efer_reload) },
|
|
|
+ { "fpu_reload", VCPU_STAT(fpu_reload) },
|
|
|
+ { "insn_emulation", VCPU_STAT(insn_emulation) },
|
|
|
+ { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
|
|
|
+ { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
|
|
|
+ { "mmu_pte_write", VM_STAT(mmu_pte_write) },
|
|
|
+ { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
|
|
|
+ { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
|
|
|
+ { "mmu_flooded", VM_STAT(mmu_flooded) },
|
|
|
+ { "mmu_recycled", VM_STAT(mmu_recycled) },
|
|
|
+ { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
|
|
|
+ { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
|
|
|
+ { NULL }
|
|
|
+};
|
|
|
|
|
|
-static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
|
|
|
- unsigned long arg);
|
|
|
|
|
|
unsigned long segment_base(u16 selector)
|
|
|
{
|
|
|
struct descriptor_table gdt;
|
|
|
struct segment_descriptor *d;
|
|
|
unsigned long table_base;
|
|
|
- typedef unsigned long ul;
|
|
|
unsigned long v;
|
|
|
|
|
|
if (selector == 0)
|
|
|
return 0;
|
|
|
|
|
|
- asm ("sgdt %0" : "=m"(gdt));
|
|
|
+ asm("sgdt %0" : "=m"(gdt));
|
|
|
table_base = gdt.base;
|
|
|
|
|
|
if (selector & 4) { /* from ldt */
|
|
|
u16 ldt_selector;
|
|
|
|
|
|
- asm ("sldt %0" : "=g"(ldt_selector));
|
|
|
+ asm("sldt %0" : "=g"(ldt_selector));
|
|
|
table_base = segment_base(ldt_selector);
|
|
|
}
|
|
|
d = (struct segment_descriptor *)(table_base + (selector & ~7));
|
|
|
- v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
|
|
|
+ v = d->base_low | ((unsigned long)d->base_mid << 16) |
|
|
|
+ ((unsigned long)d->base_high << 24);
|
|
|
#ifdef CONFIG_X86_64
|
|
|
- if (d->system == 0
|
|
|
- && (d->type == 2 || d->type == 9 || d->type == 11))
|
|
|
- v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
|
|
|
+ if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
|
|
|
+ v |= ((unsigned long) \
|
|
|
+ ((struct segment_descriptor_64 *)d)->base_higher) << 32;
|
|
|
#endif
|
|
|
return v;
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(segment_base);
|
|
|
|
|
|
-static inline int valid_vcpu(int n)
|
|
|
-{
|
|
|
- return likely(n >= 0 && n < KVM_MAX_VCPUS);
|
|
|
-}
|
|
|
-
|
|
|
-void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
|
|
|
- return;
|
|
|
-
|
|
|
- vcpu->guest_fpu_loaded = 1;
|
|
|
- fx_save(&vcpu->host_fx_image);
|
|
|
- fx_restore(&vcpu->guest_fx_image);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
|
|
|
-
|
|
|
-void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- if (!vcpu->guest_fpu_loaded)
|
|
|
- return;
|
|
|
-
|
|
|
- vcpu->guest_fpu_loaded = 0;
|
|
|
- fx_save(&vcpu->guest_fx_image);
|
|
|
- fx_restore(&vcpu->host_fx_image);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
|
|
|
-
|
|
|
-/*
|
|
|
- * Switches to specified vcpu, until a matching vcpu_put()
|
|
|
- */
|
|
|
-static void vcpu_load(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- int cpu;
|
|
|
-
|
|
|
- mutex_lock(&vcpu->mutex);
|
|
|
- cpu = get_cpu();
|
|
|
- preempt_notifier_register(&vcpu->preempt_notifier);
|
|
|
- kvm_x86_ops->vcpu_load(vcpu, cpu);
|
|
|
- put_cpu();
|
|
|
-}
|
|
|
-
|
|
|
-static void vcpu_put(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- preempt_disable();
|
|
|
- kvm_x86_ops->vcpu_put(vcpu);
|
|
|
- preempt_notifier_unregister(&vcpu->preempt_notifier);
|
|
|
- preempt_enable();
|
|
|
- mutex_unlock(&vcpu->mutex);
|
|
|
-}
|
|
|
-
|
|
|
-static void ack_flush(void *_completed)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-void kvm_flush_remote_tlbs(struct kvm *kvm)
|
|
|
-{
|
|
|
- int i, cpu;
|
|
|
- cpumask_t cpus;
|
|
|
- struct kvm_vcpu *vcpu;
|
|
|
-
|
|
|
- cpus_clear(cpus);
|
|
|
- for (i = 0; i < KVM_MAX_VCPUS; ++i) {
|
|
|
- vcpu = kvm->vcpus[i];
|
|
|
- if (!vcpu)
|
|
|
- continue;
|
|
|
- if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
|
|
|
- continue;
|
|
|
- cpu = vcpu->cpu;
|
|
|
- if (cpu != -1 && cpu != raw_smp_processor_id())
|
|
|
- cpu_set(cpu, cpus);
|
|
|
- }
|
|
|
- smp_call_function_mask(cpus, ack_flush, NULL, 1);
|
|
|
-}
|
|
|
-
|
|
|
-int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
|
|
|
+u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- struct page *page;
|
|
|
- int r;
|
|
|
-
|
|
|
- mutex_init(&vcpu->mutex);
|
|
|
- vcpu->cpu = -1;
|
|
|
- vcpu->mmu.root_hpa = INVALID_PAGE;
|
|
|
- vcpu->kvm = kvm;
|
|
|
- vcpu->vcpu_id = id;
|
|
|
- if (!irqchip_in_kernel(kvm) || id == 0)
|
|
|
- vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
|
|
|
+ if (irqchip_in_kernel(vcpu->kvm))
|
|
|
+ return vcpu->arch.apic_base;
|
|
|
else
|
|
|
- vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
|
|
|
- init_waitqueue_head(&vcpu->wq);
|
|
|
-
|
|
|
- page = alloc_page(GFP_KERNEL | __GFP_ZERO);
|
|
|
- if (!page) {
|
|
|
- r = -ENOMEM;
|
|
|
- goto fail;
|
|
|
- }
|
|
|
- vcpu->run = page_address(page);
|
|
|
-
|
|
|
- page = alloc_page(GFP_KERNEL | __GFP_ZERO);
|
|
|
- if (!page) {
|
|
|
- r = -ENOMEM;
|
|
|
- goto fail_free_run;
|
|
|
- }
|
|
|
- vcpu->pio_data = page_address(page);
|
|
|
-
|
|
|
- r = kvm_mmu_create(vcpu);
|
|
|
- if (r < 0)
|
|
|
- goto fail_free_pio_data;
|
|
|
-
|
|
|
- return 0;
|
|
|
-
|
|
|
-fail_free_pio_data:
|
|
|
- free_page((unsigned long)vcpu->pio_data);
|
|
|
-fail_free_run:
|
|
|
- free_page((unsigned long)vcpu->run);
|
|
|
-fail:
|
|
|
- return -ENOMEM;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_vcpu_init);
|
|
|
-
|
|
|
-void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- kvm_mmu_destroy(vcpu);
|
|
|
- if (vcpu->apic)
|
|
|
- hrtimer_cancel(&vcpu->apic->timer.dev);
|
|
|
- kvm_free_apic(vcpu->apic);
|
|
|
- free_page((unsigned long)vcpu->pio_data);
|
|
|
- free_page((unsigned long)vcpu->run);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
|
|
|
-
|
|
|
-static struct kvm *kvm_create_vm(void)
|
|
|
-{
|
|
|
- struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
|
|
|
-
|
|
|
- if (!kvm)
|
|
|
- return ERR_PTR(-ENOMEM);
|
|
|
-
|
|
|
- kvm_io_bus_init(&kvm->pio_bus);
|
|
|
- mutex_init(&kvm->lock);
|
|
|
- INIT_LIST_HEAD(&kvm->active_mmu_pages);
|
|
|
- kvm_io_bus_init(&kvm->mmio_bus);
|
|
|
- spin_lock(&kvm_lock);
|
|
|
- list_add(&kvm->vm_list, &vm_list);
|
|
|
- spin_unlock(&kvm_lock);
|
|
|
- return kvm;
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * Free any memory in @free but not in @dont.
|
|
|
- */
|
|
|
-static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
|
|
|
- struct kvm_memory_slot *dont)
|
|
|
-{
|
|
|
- int i;
|
|
|
-
|
|
|
- if (!dont || free->phys_mem != dont->phys_mem)
|
|
|
- if (free->phys_mem) {
|
|
|
- for (i = 0; i < free->npages; ++i)
|
|
|
- if (free->phys_mem[i])
|
|
|
- __free_page(free->phys_mem[i]);
|
|
|
- vfree(free->phys_mem);
|
|
|
- }
|
|
|
-
|
|
|
- if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
|
|
|
- vfree(free->dirty_bitmap);
|
|
|
-
|
|
|
- free->phys_mem = NULL;
|
|
|
- free->npages = 0;
|
|
|
- free->dirty_bitmap = NULL;
|
|
|
-}
|
|
|
-
|
|
|
-static void kvm_free_physmem(struct kvm *kvm)
|
|
|
-{
|
|
|
- int i;
|
|
|
-
|
|
|
- for (i = 0; i < kvm->nmemslots; ++i)
|
|
|
- kvm_free_physmem_slot(&kvm->memslots[i], NULL);
|
|
|
+ return vcpu->arch.apic_base;
|
|
|
}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_get_apic_base);
|
|
|
|
|
|
-static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
|
|
|
+void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
|
|
|
{
|
|
|
- int i;
|
|
|
-
|
|
|
- for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
|
|
|
- if (vcpu->pio.guest_pages[i]) {
|
|
|
- __free_page(vcpu->pio.guest_pages[i]);
|
|
|
- vcpu->pio.guest_pages[i] = NULL;
|
|
|
- }
|
|
|
+ /* TODO: reserve bits check */
|
|
|
+ if (irqchip_in_kernel(vcpu->kvm))
|
|
|
+ kvm_lapic_set_base(vcpu, data);
|
|
|
+ else
|
|
|
+ vcpu->arch.apic_base = data;
|
|
|
}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_set_apic_base);
|
|
|
|
|
|
-static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
|
|
|
+void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
|
|
|
{
|
|
|
- vcpu_load(vcpu);
|
|
|
- kvm_mmu_unload(vcpu);
|
|
|
- vcpu_put(vcpu);
|
|
|
+ WARN_ON(vcpu->arch.exception.pending);
|
|
|
+ vcpu->arch.exception.pending = true;
|
|
|
+ vcpu->arch.exception.has_error_code = false;
|
|
|
+ vcpu->arch.exception.nr = nr;
|
|
|
}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_queue_exception);
|
|
|
|
|
|
-static void kvm_free_vcpus(struct kvm *kvm)
|
|
|
+void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
|
|
|
+ u32 error_code)
|
|
|
{
|
|
|
- unsigned int i;
|
|
|
-
|
|
|
- /*
|
|
|
- * Unpin any mmu pages first.
|
|
|
- */
|
|
|
- for (i = 0; i < KVM_MAX_VCPUS; ++i)
|
|
|
- if (kvm->vcpus[i])
|
|
|
- kvm_unload_vcpu_mmu(kvm->vcpus[i]);
|
|
|
- for (i = 0; i < KVM_MAX_VCPUS; ++i) {
|
|
|
- if (kvm->vcpus[i]) {
|
|
|
- kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
|
|
|
- kvm->vcpus[i] = NULL;
|
|
|
- }
|
|
|
+ ++vcpu->stat.pf_guest;
|
|
|
+ if (vcpu->arch.exception.pending && vcpu->arch.exception.nr == PF_VECTOR) {
|
|
|
+ printk(KERN_DEBUG "kvm: inject_page_fault:"
|
|
|
+ " double fault 0x%lx\n", addr);
|
|
|
+ vcpu->arch.exception.nr = DF_VECTOR;
|
|
|
+ vcpu->arch.exception.error_code = 0;
|
|
|
+ return;
|
|
|
}
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-static void kvm_destroy_vm(struct kvm *kvm)
|
|
|
-{
|
|
|
- spin_lock(&kvm_lock);
|
|
|
- list_del(&kvm->vm_list);
|
|
|
- spin_unlock(&kvm_lock);
|
|
|
- kvm_io_bus_destroy(&kvm->pio_bus);
|
|
|
- kvm_io_bus_destroy(&kvm->mmio_bus);
|
|
|
- kfree(kvm->vpic);
|
|
|
- kfree(kvm->vioapic);
|
|
|
- kvm_free_vcpus(kvm);
|
|
|
- kvm_free_physmem(kvm);
|
|
|
- kfree(kvm);
|
|
|
+ vcpu->arch.cr2 = addr;
|
|
|
+ kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
|
|
|
}
|
|
|
|
|
|
-static int kvm_vm_release(struct inode *inode, struct file *filp)
|
|
|
+void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
|
|
|
{
|
|
|
- struct kvm *kvm = filp->private_data;
|
|
|
-
|
|
|
- kvm_destroy_vm(kvm);
|
|
|
- return 0;
|
|
|
+ WARN_ON(vcpu->arch.exception.pending);
|
|
|
+ vcpu->arch.exception.pending = true;
|
|
|
+ vcpu->arch.exception.has_error_code = true;
|
|
|
+ vcpu->arch.exception.nr = nr;
|
|
|
+ vcpu->arch.exception.error_code = error_code;
|
|
|
}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
|
|
|
|
|
|
-static void inject_gp(struct kvm_vcpu *vcpu)
|
|
|
+static void __queue_exception(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- kvm_x86_ops->inject_gp(vcpu, 0);
|
|
|
+ kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
|
|
|
+ vcpu->arch.exception.has_error_code,
|
|
|
+ vcpu->arch.exception.error_code);
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* Load the pae pdptrs. Return true is they are all valid.
|
|
|
*/
|
|
|
-static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
|
|
|
+int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
|
|
|
{
|
|
|
gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
|
|
|
unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
|
|
|
int i;
|
|
|
- u64 *pdpt;
|
|
|
int ret;
|
|
|
- struct page *page;
|
|
|
- u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
|
|
|
+ u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
|
|
|
|
|
|
- mutex_lock(&vcpu->kvm->lock);
|
|
|
- page = gfn_to_page(vcpu->kvm, pdpt_gfn);
|
|
|
- if (!page) {
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
|
|
|
+ offset * sizeof(u64), sizeof(pdpte));
|
|
|
+ if (ret < 0) {
|
|
|
ret = 0;
|
|
|
goto out;
|
|
|
}
|
|
|
-
|
|
|
- pdpt = kmap_atomic(page, KM_USER0);
|
|
|
- memcpy(pdpte, pdpt+offset, sizeof(pdpte));
|
|
|
- kunmap_atomic(pdpt, KM_USER0);
|
|
|
-
|
|
|
for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
|
|
|
if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
|
|
|
ret = 0;
|
|
@@ -421,78 +196,96 @@ static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
|
|
|
}
|
|
|
ret = 1;
|
|
|
|
|
|
- memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
|
|
|
+ memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
|
|
|
out:
|
|
|
- mutex_unlock(&vcpu->kvm->lock);
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
+static bool pdptrs_changed(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
|
|
|
+ bool changed = true;
|
|
|
+ int r;
|
|
|
+
|
|
|
+ if (is_long_mode(vcpu) || !is_pae(vcpu))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
|
|
|
+ if (r < 0)
|
|
|
+ goto out;
|
|
|
+ changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
|
|
|
+out:
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
+
|
|
|
+ return changed;
|
|
|
+}
|
|
|
+
|
|
|
void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
|
|
|
{
|
|
|
if (cr0 & CR0_RESERVED_BITS) {
|
|
|
printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
|
|
|
- cr0, vcpu->cr0);
|
|
|
- inject_gp(vcpu);
|
|
|
+ cr0, vcpu->arch.cr0);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
|
|
|
printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
|
|
|
printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
|
|
|
"and a clear PE flag\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
|
|
|
#ifdef CONFIG_X86_64
|
|
|
- if ((vcpu->shadow_efer & EFER_LME)) {
|
|
|
+ if ((vcpu->arch.shadow_efer & EFER_LME)) {
|
|
|
int cs_db, cs_l;
|
|
|
|
|
|
if (!is_pae(vcpu)) {
|
|
|
printk(KERN_DEBUG "set_cr0: #GP, start paging "
|
|
|
"in long mode while PAE is disabled\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
|
|
|
if (cs_l) {
|
|
|
printk(KERN_DEBUG "set_cr0: #GP, start paging "
|
|
|
"in long mode while CS.L == 1\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
} else
|
|
|
#endif
|
|
|
- if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
|
|
|
+ if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
|
|
|
printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
|
|
|
"reserved bits\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
kvm_x86_ops->set_cr0(vcpu, cr0);
|
|
|
- vcpu->cr0 = cr0;
|
|
|
+ vcpu->arch.cr0 = cr0;
|
|
|
|
|
|
- mutex_lock(&vcpu->kvm->lock);
|
|
|
kvm_mmu_reset_context(vcpu);
|
|
|
- mutex_unlock(&vcpu->kvm->lock);
|
|
|
return;
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(set_cr0);
|
|
|
|
|
|
void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
|
|
|
{
|
|
|
- set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
|
|
|
+ set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(lmsw);
|
|
|
|
|
@@ -500,7 +293,7 @@ void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
|
|
|
{
|
|
|
if (cr4 & CR4_RESERVED_BITS) {
|
|
|
printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -508,35 +301,38 @@ void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
|
|
|
if (!(cr4 & X86_CR4_PAE)) {
|
|
|
printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
|
|
|
"in long mode\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
} else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
|
|
|
- && !load_pdptrs(vcpu, vcpu->cr3)) {
|
|
|
+ && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
|
|
|
printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (cr4 & X86_CR4_VMXE) {
|
|
|
printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
kvm_x86_ops->set_cr4(vcpu, cr4);
|
|
|
- vcpu->cr4 = cr4;
|
|
|
- mutex_lock(&vcpu->kvm->lock);
|
|
|
+ vcpu->arch.cr4 = cr4;
|
|
|
kvm_mmu_reset_context(vcpu);
|
|
|
- mutex_unlock(&vcpu->kvm->lock);
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(set_cr4);
|
|
|
|
|
|
void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
|
|
|
{
|
|
|
+ if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
|
|
|
+ kvm_mmu_flush_tlb(vcpu);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (is_long_mode(vcpu)) {
|
|
|
if (cr3 & CR3_L_MODE_RESERVED_BITS) {
|
|
|
printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
} else {
|
|
@@ -544,26 +340,23 @@ void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
|
|
|
if (cr3 & CR3_PAE_RESERVED_BITS) {
|
|
|
printk(KERN_DEBUG
|
|
|
"set_cr3: #GP, reserved bits\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
|
|
|
printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
|
|
|
"reserved bits\n");
|
|
|
- inject_gp(vcpu);
|
|
|
- return;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (cr3 & CR3_NONPAE_RESERVED_BITS) {
|
|
|
- printk(KERN_DEBUG
|
|
|
- "set_cr3: #GP, reserved bits\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
+ /*
|
|
|
+ * We don't check reserved bits in nonpae mode, because
|
|
|
+ * this isn't enforced, and VMware depends on this.
|
|
|
+ */
|
|
|
}
|
|
|
|
|
|
- mutex_lock(&vcpu->kvm->lock);
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
/*
|
|
|
* Does the new cr3 value map to physical memory? (Note, we
|
|
|
* catch an invalid cr3 even in real-mode, because it would
|
|
@@ -574,12 +367,12 @@ void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
|
|
|
* to debug) behavior on the guest side.
|
|
|
*/
|
|
|
if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
else {
|
|
|
- vcpu->cr3 = cr3;
|
|
|
- vcpu->mmu.new_cr3(vcpu);
|
|
|
+ vcpu->arch.cr3 = cr3;
|
|
|
+ vcpu->arch.mmu.new_cr3(vcpu);
|
|
|
}
|
|
|
- mutex_unlock(&vcpu->kvm->lock);
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(set_cr3);
|
|
|
|
|
@@ -587,13 +380,13 @@ void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
|
|
|
{
|
|
|
if (cr8 & CR8_RESERVED_BITS) {
|
|
|
printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return;
|
|
|
}
|
|
|
if (irqchip_in_kernel(vcpu->kvm))
|
|
|
kvm_lapic_set_tpr(vcpu, cr8);
|
|
|
else
|
|
|
- vcpu->cr8 = cr8;
|
|
|
+ vcpu->arch.cr8 = cr8;
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(set_cr8);
|
|
|
|
|
@@ -602,1157 +395,1589 @@ unsigned long get_cr8(struct kvm_vcpu *vcpu)
|
|
|
if (irqchip_in_kernel(vcpu->kvm))
|
|
|
return kvm_lapic_get_cr8(vcpu);
|
|
|
else
|
|
|
- return vcpu->cr8;
|
|
|
+ return vcpu->arch.cr8;
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(get_cr8);
|
|
|
|
|
|
-u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- if (irqchip_in_kernel(vcpu->kvm))
|
|
|
- return vcpu->apic_base;
|
|
|
- else
|
|
|
- return vcpu->apic_base;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_get_apic_base);
|
|
|
-
|
|
|
-void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
|
|
|
-{
|
|
|
- /* TODO: reserve bits check */
|
|
|
- if (irqchip_in_kernel(vcpu->kvm))
|
|
|
- kvm_lapic_set_base(vcpu, data);
|
|
|
- else
|
|
|
- vcpu->apic_base = data;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_set_apic_base);
|
|
|
-
|
|
|
-void fx_init(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- unsigned after_mxcsr_mask;
|
|
|
-
|
|
|
- /* Initialize guest FPU by resetting ours and saving into guest's */
|
|
|
- preempt_disable();
|
|
|
- fx_save(&vcpu->host_fx_image);
|
|
|
- fpu_init();
|
|
|
- fx_save(&vcpu->guest_fx_image);
|
|
|
- fx_restore(&vcpu->host_fx_image);
|
|
|
- preempt_enable();
|
|
|
-
|
|
|
- vcpu->cr0 |= X86_CR0_ET;
|
|
|
- after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
|
|
|
- vcpu->guest_fx_image.mxcsr = 0x1f80;
|
|
|
- memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
|
|
|
- 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(fx_init);
|
|
|
-
|
|
|
/*
|
|
|
- * Allocate some memory and give it an address in the guest physical address
|
|
|
- * space.
|
|
|
+ * List of msr numbers which we expose to userspace through KVM_GET_MSRS
|
|
|
+ * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
|
|
|
*
|
|
|
- * Discontiguous memory is allowed, mostly for framebuffers.
|
|
|
+ * This list is modified at module load time to reflect the
|
|
|
+ * capabilities of the host cpu.
|
|
|
*/
|
|
|
-static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
|
|
|
- struct kvm_memory_region *mem)
|
|
|
-{
|
|
|
- int r;
|
|
|
- gfn_t base_gfn;
|
|
|
- unsigned long npages;
|
|
|
- unsigned long i;
|
|
|
- struct kvm_memory_slot *memslot;
|
|
|
- struct kvm_memory_slot old, new;
|
|
|
-
|
|
|
- r = -EINVAL;
|
|
|
- /* General sanity checks */
|
|
|
- if (mem->memory_size & (PAGE_SIZE - 1))
|
|
|
- goto out;
|
|
|
- if (mem->guest_phys_addr & (PAGE_SIZE - 1))
|
|
|
- goto out;
|
|
|
- if (mem->slot >= KVM_MEMORY_SLOTS)
|
|
|
- goto out;
|
|
|
- if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
|
|
|
- goto out;
|
|
|
-
|
|
|
- memslot = &kvm->memslots[mem->slot];
|
|
|
- base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
|
|
|
- npages = mem->memory_size >> PAGE_SHIFT;
|
|
|
-
|
|
|
- if (!npages)
|
|
|
- mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
|
|
|
+static u32 msrs_to_save[] = {
|
|
|
+ MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
|
|
|
+ MSR_K6_STAR,
|
|
|
+#ifdef CONFIG_X86_64
|
|
|
+ MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
|
|
|
+#endif
|
|
|
+ MSR_IA32_TIME_STAMP_COUNTER,
|
|
|
+};
|
|
|
|
|
|
- mutex_lock(&kvm->lock);
|
|
|
+static unsigned num_msrs_to_save;
|
|
|
|
|
|
- new = old = *memslot;
|
|
|
+static u32 emulated_msrs[] = {
|
|
|
+ MSR_IA32_MISC_ENABLE,
|
|
|
+};
|
|
|
|
|
|
- new.base_gfn = base_gfn;
|
|
|
- new.npages = npages;
|
|
|
- new.flags = mem->flags;
|
|
|
+#ifdef CONFIG_X86_64
|
|
|
|
|
|
- /* Disallow changing a memory slot's size. */
|
|
|
- r = -EINVAL;
|
|
|
- if (npages && old.npages && npages != old.npages)
|
|
|
- goto out_unlock;
|
|
|
-
|
|
|
- /* Check for overlaps */
|
|
|
- r = -EEXIST;
|
|
|
- for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
|
|
|
- struct kvm_memory_slot *s = &kvm->memslots[i];
|
|
|
+static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
|
|
|
+{
|
|
|
+ if (efer & EFER_RESERVED_BITS) {
|
|
|
+ printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
|
|
|
+ efer);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- if (s == memslot)
|
|
|
- continue;
|
|
|
- if (!((base_gfn + npages <= s->base_gfn) ||
|
|
|
- (base_gfn >= s->base_gfn + s->npages)))
|
|
|
- goto out_unlock;
|
|
|
+ if (is_paging(vcpu)
|
|
|
+ && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
|
|
|
+ printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- /* Deallocate if slot is being removed */
|
|
|
- if (!npages)
|
|
|
- new.phys_mem = NULL;
|
|
|
+ kvm_x86_ops->set_efer(vcpu, efer);
|
|
|
|
|
|
- /* Free page dirty bitmap if unneeded */
|
|
|
- if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
|
|
|
- new.dirty_bitmap = NULL;
|
|
|
+ efer &= ~EFER_LMA;
|
|
|
+ efer |= vcpu->arch.shadow_efer & EFER_LMA;
|
|
|
|
|
|
- r = -ENOMEM;
|
|
|
+ vcpu->arch.shadow_efer = efer;
|
|
|
+}
|
|
|
|
|
|
- /* Allocate if a slot is being created */
|
|
|
- if (npages && !new.phys_mem) {
|
|
|
- new.phys_mem = vmalloc(npages * sizeof(struct page *));
|
|
|
+#endif
|
|
|
|
|
|
- if (!new.phys_mem)
|
|
|
- goto out_unlock;
|
|
|
+/*
|
|
|
+ * Writes msr value into into the appropriate "register".
|
|
|
+ * Returns 0 on success, non-0 otherwise.
|
|
|
+ * Assumes vcpu_load() was already called.
|
|
|
+ */
|
|
|
+int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
|
|
|
+{
|
|
|
+ return kvm_x86_ops->set_msr(vcpu, msr_index, data);
|
|
|
+}
|
|
|
|
|
|
- memset(new.phys_mem, 0, npages * sizeof(struct page *));
|
|
|
- for (i = 0; i < npages; ++i) {
|
|
|
- new.phys_mem[i] = alloc_page(GFP_HIGHUSER
|
|
|
- | __GFP_ZERO);
|
|
|
- if (!new.phys_mem[i])
|
|
|
- goto out_unlock;
|
|
|
- set_page_private(new.phys_mem[i],0);
|
|
|
- }
|
|
|
- }
|
|
|
+/*
|
|
|
+ * Adapt set_msr() to msr_io()'s calling convention
|
|
|
+ */
|
|
|
+static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
|
|
|
+{
|
|
|
+ return kvm_set_msr(vcpu, index, *data);
|
|
|
+}
|
|
|
|
|
|
- /* Allocate page dirty bitmap if needed */
|
|
|
- if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
|
|
|
- unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
|
|
|
|
|
|
- new.dirty_bitmap = vmalloc(dirty_bytes);
|
|
|
- if (!new.dirty_bitmap)
|
|
|
- goto out_unlock;
|
|
|
- memset(new.dirty_bitmap, 0, dirty_bytes);
|
|
|
+int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
|
|
|
+{
|
|
|
+ switch (msr) {
|
|
|
+#ifdef CONFIG_X86_64
|
|
|
+ case MSR_EFER:
|
|
|
+ set_efer(vcpu, data);
|
|
|
+ break;
|
|
|
+#endif
|
|
|
+ case MSR_IA32_MC0_STATUS:
|
|
|
+ pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
|
|
|
+ __FUNCTION__, data);
|
|
|
+ break;
|
|
|
+ case MSR_IA32_MCG_STATUS:
|
|
|
+ pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
|
|
|
+ __FUNCTION__, data);
|
|
|
+ break;
|
|
|
+ case MSR_IA32_UCODE_REV:
|
|
|
+ case MSR_IA32_UCODE_WRITE:
|
|
|
+ case 0x200 ... 0x2ff: /* MTRRs */
|
|
|
+ break;
|
|
|
+ case MSR_IA32_APICBASE:
|
|
|
+ kvm_set_apic_base(vcpu, data);
|
|
|
+ break;
|
|
|
+ case MSR_IA32_MISC_ENABLE:
|
|
|
+ vcpu->arch.ia32_misc_enable_msr = data;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
|
|
|
+ return 1;
|
|
|
}
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_set_msr_common);
|
|
|
|
|
|
- if (mem->slot >= kvm->nmemslots)
|
|
|
- kvm->nmemslots = mem->slot + 1;
|
|
|
-
|
|
|
- *memslot = new;
|
|
|
|
|
|
- kvm_mmu_slot_remove_write_access(kvm, mem->slot);
|
|
|
- kvm_flush_remote_tlbs(kvm);
|
|
|
+/*
|
|
|
+ * Reads an msr value (of 'msr_index') into 'pdata'.
|
|
|
+ * Returns 0 on success, non-0 otherwise.
|
|
|
+ * Assumes vcpu_load() was already called.
|
|
|
+ */
|
|
|
+int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
|
|
|
+{
|
|
|
+ return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
|
|
|
+}
|
|
|
|
|
|
- mutex_unlock(&kvm->lock);
|
|
|
+int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
|
|
|
+{
|
|
|
+ u64 data;
|
|
|
|
|
|
- kvm_free_physmem_slot(&old, &new);
|
|
|
+ switch (msr) {
|
|
|
+ case 0xc0010010: /* SYSCFG */
|
|
|
+ case 0xc0010015: /* HWCR */
|
|
|
+ case MSR_IA32_PLATFORM_ID:
|
|
|
+ case MSR_IA32_P5_MC_ADDR:
|
|
|
+ case MSR_IA32_P5_MC_TYPE:
|
|
|
+ case MSR_IA32_MC0_CTL:
|
|
|
+ case MSR_IA32_MCG_STATUS:
|
|
|
+ case MSR_IA32_MCG_CAP:
|
|
|
+ case MSR_IA32_MC0_MISC:
|
|
|
+ case MSR_IA32_MC0_MISC+4:
|
|
|
+ case MSR_IA32_MC0_MISC+8:
|
|
|
+ case MSR_IA32_MC0_MISC+12:
|
|
|
+ case MSR_IA32_MC0_MISC+16:
|
|
|
+ case MSR_IA32_UCODE_REV:
|
|
|
+ case MSR_IA32_PERF_STATUS:
|
|
|
+ case MSR_IA32_EBL_CR_POWERON:
|
|
|
+ /* MTRR registers */
|
|
|
+ case 0xfe:
|
|
|
+ case 0x200 ... 0x2ff:
|
|
|
+ data = 0;
|
|
|
+ break;
|
|
|
+ case 0xcd: /* fsb frequency */
|
|
|
+ data = 3;
|
|
|
+ break;
|
|
|
+ case MSR_IA32_APICBASE:
|
|
|
+ data = kvm_get_apic_base(vcpu);
|
|
|
+ break;
|
|
|
+ case MSR_IA32_MISC_ENABLE:
|
|
|
+ data = vcpu->arch.ia32_misc_enable_msr;
|
|
|
+ break;
|
|
|
+#ifdef CONFIG_X86_64
|
|
|
+ case MSR_EFER:
|
|
|
+ data = vcpu->arch.shadow_efer;
|
|
|
+ break;
|
|
|
+#endif
|
|
|
+ default:
|
|
|
+ pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ *pdata = data;
|
|
|
return 0;
|
|
|
-
|
|
|
-out_unlock:
|
|
|
- mutex_unlock(&kvm->lock);
|
|
|
- kvm_free_physmem_slot(&new, &old);
|
|
|
-out:
|
|
|
- return r;
|
|
|
}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_get_msr_common);
|
|
|
|
|
|
/*
|
|
|
- * Get (and clear) the dirty memory log for a memory slot.
|
|
|
+ * Read or write a bunch of msrs. All parameters are kernel addresses.
|
|
|
+ *
|
|
|
+ * @return number of msrs set successfully.
|
|
|
*/
|
|
|
-static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
|
|
|
- struct kvm_dirty_log *log)
|
|
|
+static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
|
|
|
+ struct kvm_msr_entry *entries,
|
|
|
+ int (*do_msr)(struct kvm_vcpu *vcpu,
|
|
|
+ unsigned index, u64 *data))
|
|
|
{
|
|
|
- struct kvm_memory_slot *memslot;
|
|
|
- int r, i;
|
|
|
- int n;
|
|
|
- unsigned long any = 0;
|
|
|
-
|
|
|
- mutex_lock(&kvm->lock);
|
|
|
-
|
|
|
- r = -EINVAL;
|
|
|
- if (log->slot >= KVM_MEMORY_SLOTS)
|
|
|
- goto out;
|
|
|
-
|
|
|
- memslot = &kvm->memslots[log->slot];
|
|
|
- r = -ENOENT;
|
|
|
- if (!memslot->dirty_bitmap)
|
|
|
- goto out;
|
|
|
-
|
|
|
- n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
|
|
|
-
|
|
|
- for (i = 0; !any && i < n/sizeof(long); ++i)
|
|
|
- any = memslot->dirty_bitmap[i];
|
|
|
+ int i;
|
|
|
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
|
|
|
- goto out;
|
|
|
+ vcpu_load(vcpu);
|
|
|
|
|
|
- /* If nothing is dirty, don't bother messing with page tables. */
|
|
|
- if (any) {
|
|
|
- kvm_mmu_slot_remove_write_access(kvm, log->slot);
|
|
|
- kvm_flush_remote_tlbs(kvm);
|
|
|
- memset(memslot->dirty_bitmap, 0, n);
|
|
|
- }
|
|
|
+ for (i = 0; i < msrs->nmsrs; ++i)
|
|
|
+ if (do_msr(vcpu, entries[i].index, &entries[i].data))
|
|
|
+ break;
|
|
|
|
|
|
- r = 0;
|
|
|
+ vcpu_put(vcpu);
|
|
|
|
|
|
-out:
|
|
|
- mutex_unlock(&kvm->lock);
|
|
|
- return r;
|
|
|
+ return i;
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
- * Set a new alias region. Aliases map a portion of physical memory into
|
|
|
- * another portion. This is useful for memory windows, for example the PC
|
|
|
- * VGA region.
|
|
|
+ * Read or write a bunch of msrs. Parameters are user addresses.
|
|
|
+ *
|
|
|
+ * @return number of msrs set successfully.
|
|
|
*/
|
|
|
-static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
|
|
|
- struct kvm_memory_alias *alias)
|
|
|
+static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
|
|
|
+ int (*do_msr)(struct kvm_vcpu *vcpu,
|
|
|
+ unsigned index, u64 *data),
|
|
|
+ int writeback)
|
|
|
{
|
|
|
+ struct kvm_msrs msrs;
|
|
|
+ struct kvm_msr_entry *entries;
|
|
|
int r, n;
|
|
|
- struct kvm_mem_alias *p;
|
|
|
+ unsigned size;
|
|
|
|
|
|
- r = -EINVAL;
|
|
|
- /* General sanity checks */
|
|
|
- if (alias->memory_size & (PAGE_SIZE - 1))
|
|
|
- goto out;
|
|
|
- if (alias->guest_phys_addr & (PAGE_SIZE - 1))
|
|
|
- goto out;
|
|
|
- if (alias->slot >= KVM_ALIAS_SLOTS)
|
|
|
- goto out;
|
|
|
- if (alias->guest_phys_addr + alias->memory_size
|
|
|
- < alias->guest_phys_addr)
|
|
|
- goto out;
|
|
|
- if (alias->target_phys_addr + alias->memory_size
|
|
|
- < alias->target_phys_addr)
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&msrs, user_msrs, sizeof msrs))
|
|
|
goto out;
|
|
|
|
|
|
- mutex_lock(&kvm->lock);
|
|
|
+ r = -E2BIG;
|
|
|
+ if (msrs.nmsrs >= MAX_IO_MSRS)
|
|
|
+ goto out;
|
|
|
|
|
|
- p = &kvm->aliases[alias->slot];
|
|
|
- p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
|
|
|
- p->npages = alias->memory_size >> PAGE_SHIFT;
|
|
|
- p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
|
|
|
+ r = -ENOMEM;
|
|
|
+ size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
|
|
|
+ entries = vmalloc(size);
|
|
|
+ if (!entries)
|
|
|
+ goto out;
|
|
|
|
|
|
- for (n = KVM_ALIAS_SLOTS; n > 0; --n)
|
|
|
- if (kvm->aliases[n - 1].npages)
|
|
|
- break;
|
|
|
- kvm->naliases = n;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(entries, user_msrs->entries, size))
|
|
|
+ goto out_free;
|
|
|
|
|
|
- kvm_mmu_zap_all(kvm);
|
|
|
+ r = n = __msr_io(vcpu, &msrs, entries, do_msr);
|
|
|
+ if (r < 0)
|
|
|
+ goto out_free;
|
|
|
|
|
|
- mutex_unlock(&kvm->lock);
|
|
|
+ r = -EFAULT;
|
|
|
+ if (writeback && copy_to_user(user_msrs->entries, entries, size))
|
|
|
+ goto out_free;
|
|
|
|
|
|
- return 0;
|
|
|
+ r = n;
|
|
|
|
|
|
+out_free:
|
|
|
+ vfree(entries);
|
|
|
out:
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
-static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
|
|
|
+/*
|
|
|
+ * Make sure that a cpu that is being hot-unplugged does not have any vcpus
|
|
|
+ * cached on it.
|
|
|
+ */
|
|
|
+void decache_vcpus_on_cpu(int cpu)
|
|
|
+{
|
|
|
+ struct kvm *vm;
|
|
|
+ struct kvm_vcpu *vcpu;
|
|
|
+ int i;
|
|
|
+
|
|
|
+ spin_lock(&kvm_lock);
|
|
|
+ list_for_each_entry(vm, &vm_list, vm_list)
|
|
|
+ for (i = 0; i < KVM_MAX_VCPUS; ++i) {
|
|
|
+ vcpu = vm->vcpus[i];
|
|
|
+ if (!vcpu)
|
|
|
+ continue;
|
|
|
+ /*
|
|
|
+ * If the vcpu is locked, then it is running on some
|
|
|
+ * other cpu and therefore it is not cached on the
|
|
|
+ * cpu in question.
|
|
|
+ *
|
|
|
+ * If it's not locked, check the last cpu it executed
|
|
|
+ * on.
|
|
|
+ */
|
|
|
+ if (mutex_trylock(&vcpu->mutex)) {
|
|
|
+ if (vcpu->cpu == cpu) {
|
|
|
+ kvm_x86_ops->vcpu_decache(vcpu);
|
|
|
+ vcpu->cpu = -1;
|
|
|
+ }
|
|
|
+ mutex_unlock(&vcpu->mutex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ spin_unlock(&kvm_lock);
|
|
|
+}
|
|
|
+
|
|
|
+int kvm_dev_ioctl_check_extension(long ext)
|
|
|
{
|
|
|
int r;
|
|
|
|
|
|
- r = 0;
|
|
|
- switch (chip->chip_id) {
|
|
|
- case KVM_IRQCHIP_PIC_MASTER:
|
|
|
- memcpy (&chip->chip.pic,
|
|
|
- &pic_irqchip(kvm)->pics[0],
|
|
|
- sizeof(struct kvm_pic_state));
|
|
|
- break;
|
|
|
- case KVM_IRQCHIP_PIC_SLAVE:
|
|
|
- memcpy (&chip->chip.pic,
|
|
|
- &pic_irqchip(kvm)->pics[1],
|
|
|
- sizeof(struct kvm_pic_state));
|
|
|
+ switch (ext) {
|
|
|
+ case KVM_CAP_IRQCHIP:
|
|
|
+ case KVM_CAP_HLT:
|
|
|
+ case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
|
|
|
+ case KVM_CAP_USER_MEMORY:
|
|
|
+ case KVM_CAP_SET_TSS_ADDR:
|
|
|
+ case KVM_CAP_EXT_CPUID:
|
|
|
+ r = 1;
|
|
|
break;
|
|
|
- case KVM_IRQCHIP_IOAPIC:
|
|
|
- memcpy (&chip->chip.ioapic,
|
|
|
- ioapic_irqchip(kvm),
|
|
|
- sizeof(struct kvm_ioapic_state));
|
|
|
+ case KVM_CAP_VAPIC:
|
|
|
+ r = !kvm_x86_ops->cpu_has_accelerated_tpr();
|
|
|
break;
|
|
|
default:
|
|
|
- r = -EINVAL;
|
|
|
+ r = 0;
|
|
|
break;
|
|
|
}
|
|
|
return r;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
-static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
|
|
|
+long kvm_arch_dev_ioctl(struct file *filp,
|
|
|
+ unsigned int ioctl, unsigned long arg)
|
|
|
{
|
|
|
- int r;
|
|
|
+ void __user *argp = (void __user *)arg;
|
|
|
+ long r;
|
|
|
|
|
|
- r = 0;
|
|
|
- switch (chip->chip_id) {
|
|
|
- case KVM_IRQCHIP_PIC_MASTER:
|
|
|
- memcpy (&pic_irqchip(kvm)->pics[0],
|
|
|
- &chip->chip.pic,
|
|
|
- sizeof(struct kvm_pic_state));
|
|
|
- break;
|
|
|
- case KVM_IRQCHIP_PIC_SLAVE:
|
|
|
- memcpy (&pic_irqchip(kvm)->pics[1],
|
|
|
- &chip->chip.pic,
|
|
|
- sizeof(struct kvm_pic_state));
|
|
|
- break;
|
|
|
- case KVM_IRQCHIP_IOAPIC:
|
|
|
- memcpy (ioapic_irqchip(kvm),
|
|
|
- &chip->chip.ioapic,
|
|
|
- sizeof(struct kvm_ioapic_state));
|
|
|
+ switch (ioctl) {
|
|
|
+ case KVM_GET_MSR_INDEX_LIST: {
|
|
|
+ struct kvm_msr_list __user *user_msr_list = argp;
|
|
|
+ struct kvm_msr_list msr_list;
|
|
|
+ unsigned n;
|
|
|
+
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
|
|
|
+ goto out;
|
|
|
+ n = msr_list.nmsrs;
|
|
|
+ msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
|
|
|
+ if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
|
|
|
+ goto out;
|
|
|
+ r = -E2BIG;
|
|
|
+ if (n < num_msrs_to_save)
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_to_user(user_msr_list->indices, &msrs_to_save,
|
|
|
+ num_msrs_to_save * sizeof(u32)))
|
|
|
+ goto out;
|
|
|
+ if (copy_to_user(user_msr_list->indices
|
|
|
+ + num_msrs_to_save * sizeof(u32),
|
|
|
+ &emulated_msrs,
|
|
|
+ ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
break;
|
|
|
+ }
|
|
|
default:
|
|
|
r = -EINVAL;
|
|
|
- break;
|
|
|
}
|
|
|
- kvm_pic_update_irq(pic_irqchip(kvm));
|
|
|
+out:
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
-static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
|
|
|
+void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
|
|
|
{
|
|
|
- int i;
|
|
|
- struct kvm_mem_alias *alias;
|
|
|
+ kvm_x86_ops->vcpu_load(vcpu, cpu);
|
|
|
+}
|
|
|
|
|
|
- for (i = 0; i < kvm->naliases; ++i) {
|
|
|
- alias = &kvm->aliases[i];
|
|
|
- if (gfn >= alias->base_gfn
|
|
|
- && gfn < alias->base_gfn + alias->npages)
|
|
|
- return alias->target_gfn + gfn - alias->base_gfn;
|
|
|
- }
|
|
|
- return gfn;
|
|
|
+void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ kvm_x86_ops->vcpu_put(vcpu);
|
|
|
+ kvm_put_guest_fpu(vcpu);
|
|
|
}
|
|
|
|
|
|
-static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
|
|
|
+static int is_efer_nx(void)
|
|
|
{
|
|
|
- int i;
|
|
|
+ u64 efer;
|
|
|
+
|
|
|
+ rdmsrl(MSR_EFER, efer);
|
|
|
+ return efer & EFER_NX;
|
|
|
+}
|
|
|
|
|
|
- for (i = 0; i < kvm->nmemslots; ++i) {
|
|
|
- struct kvm_memory_slot *memslot = &kvm->memslots[i];
|
|
|
+static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ struct kvm_cpuid_entry2 *e, *entry;
|
|
|
|
|
|
- if (gfn >= memslot->base_gfn
|
|
|
- && gfn < memslot->base_gfn + memslot->npages)
|
|
|
- return memslot;
|
|
|
+ entry = NULL;
|
|
|
+ for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
|
|
|
+ e = &vcpu->arch.cpuid_entries[i];
|
|
|
+ if (e->function == 0x80000001) {
|
|
|
+ entry = e;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
|
|
|
+ entry->edx &= ~(1 << 20);
|
|
|
+ printk(KERN_INFO "kvm: guest NX capability removed\n");
|
|
|
}
|
|
|
- return NULL;
|
|
|
}
|
|
|
|
|
|
-struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
|
|
|
+/* when an old userspace process fills a new kernel module */
|
|
|
+static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_cpuid *cpuid,
|
|
|
+ struct kvm_cpuid_entry __user *entries)
|
|
|
{
|
|
|
- gfn = unalias_gfn(kvm, gfn);
|
|
|
- return __gfn_to_memslot(kvm, gfn);
|
|
|
+ int r, i;
|
|
|
+ struct kvm_cpuid_entry *cpuid_entries;
|
|
|
+
|
|
|
+ r = -E2BIG;
|
|
|
+ if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
|
|
|
+ goto out;
|
|
|
+ r = -ENOMEM;
|
|
|
+ cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
|
|
|
+ if (!cpuid_entries)
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(cpuid_entries, entries,
|
|
|
+ cpuid->nent * sizeof(struct kvm_cpuid_entry)))
|
|
|
+ goto out_free;
|
|
|
+ for (i = 0; i < cpuid->nent; i++) {
|
|
|
+ vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
|
|
|
+ vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
|
|
|
+ vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
|
|
|
+ vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
|
|
|
+ vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
|
|
|
+ vcpu->arch.cpuid_entries[i].index = 0;
|
|
|
+ vcpu->arch.cpuid_entries[i].flags = 0;
|
|
|
+ vcpu->arch.cpuid_entries[i].padding[0] = 0;
|
|
|
+ vcpu->arch.cpuid_entries[i].padding[1] = 0;
|
|
|
+ vcpu->arch.cpuid_entries[i].padding[2] = 0;
|
|
|
+ }
|
|
|
+ vcpu->arch.cpuid_nent = cpuid->nent;
|
|
|
+ cpuid_fix_nx_cap(vcpu);
|
|
|
+ r = 0;
|
|
|
+
|
|
|
+out_free:
|
|
|
+ vfree(cpuid_entries);
|
|
|
+out:
|
|
|
+ return r;
|
|
|
}
|
|
|
|
|
|
-struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
|
|
|
+static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_cpuid2 *cpuid,
|
|
|
+ struct kvm_cpuid_entry2 __user *entries)
|
|
|
{
|
|
|
- struct kvm_memory_slot *slot;
|
|
|
+ int r;
|
|
|
+
|
|
|
+ r = -E2BIG;
|
|
|
+ if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
|
|
|
+ cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
|
|
|
+ goto out;
|
|
|
+ vcpu->arch.cpuid_nent = cpuid->nent;
|
|
|
+ return 0;
|
|
|
|
|
|
- gfn = unalias_gfn(kvm, gfn);
|
|
|
- slot = __gfn_to_memslot(kvm, gfn);
|
|
|
- if (!slot)
|
|
|
- return NULL;
|
|
|
- return slot->phys_mem[gfn - slot->base_gfn];
|
|
|
+out:
|
|
|
+ return r;
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(gfn_to_page);
|
|
|
|
|
|
-/* WARNING: Does not work on aliased pages. */
|
|
|
-void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
|
|
|
+static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_cpuid2 *cpuid,
|
|
|
+ struct kvm_cpuid_entry2 __user *entries)
|
|
|
{
|
|
|
- struct kvm_memory_slot *memslot;
|
|
|
+ int r;
|
|
|
+
|
|
|
+ r = -E2BIG;
|
|
|
+ if (cpuid->nent < vcpu->arch.cpuid_nent)
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
|
|
|
+ vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
|
|
|
+ goto out;
|
|
|
+ return 0;
|
|
|
|
|
|
- memslot = __gfn_to_memslot(kvm, gfn);
|
|
|
- if (memslot && memslot->dirty_bitmap) {
|
|
|
- unsigned long rel_gfn = gfn - memslot->base_gfn;
|
|
|
+out:
|
|
|
+ cpuid->nent = vcpu->arch.cpuid_nent;
|
|
|
+ return r;
|
|
|
+}
|
|
|
|
|
|
- /* avoid RMW */
|
|
|
- if (!test_bit(rel_gfn, memslot->dirty_bitmap))
|
|
|
- set_bit(rel_gfn, memslot->dirty_bitmap);
|
|
|
+static inline u32 bit(int bitno)
|
|
|
+{
|
|
|
+ return 1 << (bitno & 31);
|
|
|
+}
|
|
|
+
|
|
|
+static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
|
|
|
+ u32 index)
|
|
|
+{
|
|
|
+ entry->function = function;
|
|
|
+ entry->index = index;
|
|
|
+ cpuid_count(entry->function, entry->index,
|
|
|
+ &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
|
|
|
+ entry->flags = 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
|
|
|
+ u32 index, int *nent, int maxnent)
|
|
|
+{
|
|
|
+ const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
|
|
|
+ bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
|
|
|
+ bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
|
|
|
+ bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
|
|
|
+ bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
|
|
|
+ bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
|
|
|
+ bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
|
|
|
+ bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
|
|
|
+ bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
|
|
|
+ bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
|
|
|
+ const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
|
|
|
+ bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
|
|
|
+ bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
|
|
|
+ bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
|
|
|
+ bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
|
|
|
+ bit(X86_FEATURE_PGE) |
|
|
|
+ bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
|
|
|
+ bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
|
|
|
+ bit(X86_FEATURE_SYSCALL) |
|
|
|
+ (bit(X86_FEATURE_NX) && is_efer_nx()) |
|
|
|
+#ifdef CONFIG_X86_64
|
|
|
+ bit(X86_FEATURE_LM) |
|
|
|
+#endif
|
|
|
+ bit(X86_FEATURE_MMXEXT) |
|
|
|
+ bit(X86_FEATURE_3DNOWEXT) |
|
|
|
+ bit(X86_FEATURE_3DNOW);
|
|
|
+ const u32 kvm_supported_word3_x86_features =
|
|
|
+ bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
|
|
|
+ const u32 kvm_supported_word6_x86_features =
|
|
|
+ bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
|
|
|
+
|
|
|
+ /* all func 2 cpuid_count() should be called on the same cpu */
|
|
|
+ get_cpu();
|
|
|
+ do_cpuid_1_ent(entry, function, index);
|
|
|
+ ++*nent;
|
|
|
+
|
|
|
+ switch (function) {
|
|
|
+ case 0:
|
|
|
+ entry->eax = min(entry->eax, (u32)0xb);
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ entry->edx &= kvm_supported_word0_x86_features;
|
|
|
+ entry->ecx &= kvm_supported_word3_x86_features;
|
|
|
+ break;
|
|
|
+ /* function 2 entries are STATEFUL. That is, repeated cpuid commands
|
|
|
+ * may return different values. This forces us to get_cpu() before
|
|
|
+ * issuing the first command, and also to emulate this annoying behavior
|
|
|
+ * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
|
|
|
+ case 2: {
|
|
|
+ int t, times = entry->eax & 0xff;
|
|
|
+
|
|
|
+ entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
|
|
|
+ for (t = 1; t < times && *nent < maxnent; ++t) {
|
|
|
+ do_cpuid_1_ent(&entry[t], function, 0);
|
|
|
+ entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
|
|
|
+ ++*nent;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ /* function 4 and 0xb have additional index. */
|
|
|
+ case 4: {
|
|
|
+ int index, cache_type;
|
|
|
+
|
|
|
+ entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
|
|
|
+ /* read more entries until cache_type is zero */
|
|
|
+ for (index = 1; *nent < maxnent; ++index) {
|
|
|
+ cache_type = entry[index - 1].eax & 0x1f;
|
|
|
+ if (!cache_type)
|
|
|
+ break;
|
|
|
+ do_cpuid_1_ent(&entry[index], function, index);
|
|
|
+ entry[index].flags |=
|
|
|
+ KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
|
|
|
+ ++*nent;
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
+ case 0xb: {
|
|
|
+ int index, level_type;
|
|
|
+
|
|
|
+ entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
|
|
|
+ /* read more entries until level_type is zero */
|
|
|
+ for (index = 1; *nent < maxnent; ++index) {
|
|
|
+ level_type = entry[index - 1].ecx & 0xff;
|
|
|
+ if (!level_type)
|
|
|
+ break;
|
|
|
+ do_cpuid_1_ent(&entry[index], function, index);
|
|
|
+ entry[index].flags |=
|
|
|
+ KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
|
|
|
+ ++*nent;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 0x80000000:
|
|
|
+ entry->eax = min(entry->eax, 0x8000001a);
|
|
|
+ break;
|
|
|
+ case 0x80000001:
|
|
|
+ entry->edx &= kvm_supported_word1_x86_features;
|
|
|
+ entry->ecx &= kvm_supported_word6_x86_features;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ put_cpu();
|
|
|
}
|
|
|
|
|
|
-int emulator_read_std(unsigned long addr,
|
|
|
- void *val,
|
|
|
- unsigned int bytes,
|
|
|
- struct kvm_vcpu *vcpu)
|
|
|
+static int kvm_vm_ioctl_get_supported_cpuid(struct kvm *kvm,
|
|
|
+ struct kvm_cpuid2 *cpuid,
|
|
|
+ struct kvm_cpuid_entry2 __user *entries)
|
|
|
{
|
|
|
- void *data = val;
|
|
|
-
|
|
|
- while (bytes) {
|
|
|
- gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
|
|
|
- unsigned offset = addr & (PAGE_SIZE-1);
|
|
|
- unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
|
|
|
- unsigned long pfn;
|
|
|
- struct page *page;
|
|
|
- void *page_virt;
|
|
|
-
|
|
|
- if (gpa == UNMAPPED_GVA)
|
|
|
- return X86EMUL_PROPAGATE_FAULT;
|
|
|
- pfn = gpa >> PAGE_SHIFT;
|
|
|
- page = gfn_to_page(vcpu->kvm, pfn);
|
|
|
- if (!page)
|
|
|
- return X86EMUL_UNHANDLEABLE;
|
|
|
- page_virt = kmap_atomic(page, KM_USER0);
|
|
|
+ struct kvm_cpuid_entry2 *cpuid_entries;
|
|
|
+ int limit, nent = 0, r = -E2BIG;
|
|
|
+ u32 func;
|
|
|
|
|
|
- memcpy(data, page_virt + offset, tocopy);
|
|
|
+ if (cpuid->nent < 1)
|
|
|
+ goto out;
|
|
|
+ r = -ENOMEM;
|
|
|
+ cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
|
|
|
+ if (!cpuid_entries)
|
|
|
+ goto out;
|
|
|
|
|
|
- kunmap_atomic(page_virt, KM_USER0);
|
|
|
+ do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
|
|
|
+ limit = cpuid_entries[0].eax;
|
|
|
+ for (func = 1; func <= limit && nent < cpuid->nent; ++func)
|
|
|
+ do_cpuid_ent(&cpuid_entries[nent], func, 0,
|
|
|
+ &nent, cpuid->nent);
|
|
|
+ r = -E2BIG;
|
|
|
+ if (nent >= cpuid->nent)
|
|
|
+ goto out_free;
|
|
|
|
|
|
- bytes -= tocopy;
|
|
|
- data += tocopy;
|
|
|
- addr += tocopy;
|
|
|
- }
|
|
|
+ do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
|
|
|
+ limit = cpuid_entries[nent - 1].eax;
|
|
|
+ for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
|
|
|
+ do_cpuid_ent(&cpuid_entries[nent], func, 0,
|
|
|
+ &nent, cpuid->nent);
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_to_user(entries, cpuid_entries,
|
|
|
+ nent * sizeof(struct kvm_cpuid_entry2)))
|
|
|
+ goto out_free;
|
|
|
+ cpuid->nent = nent;
|
|
|
+ r = 0;
|
|
|
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
+out_free:
|
|
|
+ vfree(cpuid_entries);
|
|
|
+out:
|
|
|
+ return r;
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(emulator_read_std);
|
|
|
|
|
|
-static int emulator_write_std(unsigned long addr,
|
|
|
- const void *val,
|
|
|
- unsigned int bytes,
|
|
|
- struct kvm_vcpu *vcpu)
|
|
|
+static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_lapic_state *s)
|
|
|
{
|
|
|
- pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
|
|
|
- return X86EMUL_UNHANDLEABLE;
|
|
|
+ vcpu_load(vcpu);
|
|
|
+ memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
|
|
|
+ vcpu_put(vcpu);
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
- * Only apic need an MMIO device hook, so shortcut now..
|
|
|
- */
|
|
|
-static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
|
|
|
- gpa_t addr)
|
|
|
+static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_lapic_state *s)
|
|
|
{
|
|
|
- struct kvm_io_device *dev;
|
|
|
+ vcpu_load(vcpu);
|
|
|
+ memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
|
|
|
+ kvm_apic_post_state_restore(vcpu);
|
|
|
+ vcpu_put(vcpu);
|
|
|
|
|
|
- if (vcpu->apic) {
|
|
|
- dev = &vcpu->apic->dev;
|
|
|
- if (dev->in_range(dev, addr))
|
|
|
- return dev;
|
|
|
- }
|
|
|
- return NULL;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
|
|
|
- gpa_t addr)
|
|
|
+static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_interrupt *irq)
|
|
|
{
|
|
|
- struct kvm_io_device *dev;
|
|
|
+ if (irq->irq < 0 || irq->irq >= 256)
|
|
|
+ return -EINVAL;
|
|
|
+ if (irqchip_in_kernel(vcpu->kvm))
|
|
|
+ return -ENXIO;
|
|
|
+ vcpu_load(vcpu);
|
|
|
|
|
|
- dev = vcpu_find_pervcpu_dev(vcpu, addr);
|
|
|
- if (dev == NULL)
|
|
|
- dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
|
|
|
- return dev;
|
|
|
+ set_bit(irq->irq, vcpu->arch.irq_pending);
|
|
|
+ set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
|
|
|
+
|
|
|
+ vcpu_put(vcpu);
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
|
|
|
- gpa_t addr)
|
|
|
+static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_tpr_access_ctl *tac)
|
|
|
{
|
|
|
- return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
|
|
|
+ if (tac->flags)
|
|
|
+ return -EINVAL;
|
|
|
+ vcpu->arch.tpr_access_reporting = !!tac->enabled;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-static int emulator_read_emulated(unsigned long addr,
|
|
|
- void *val,
|
|
|
- unsigned int bytes,
|
|
|
- struct kvm_vcpu *vcpu)
|
|
|
+long kvm_arch_vcpu_ioctl(struct file *filp,
|
|
|
+ unsigned int ioctl, unsigned long arg)
|
|
|
{
|
|
|
- struct kvm_io_device *mmio_dev;
|
|
|
- gpa_t gpa;
|
|
|
-
|
|
|
- if (vcpu->mmio_read_completed) {
|
|
|
- memcpy(val, vcpu->mmio_data, bytes);
|
|
|
- vcpu->mmio_read_completed = 0;
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
- } else if (emulator_read_std(addr, val, bytes, vcpu)
|
|
|
- == X86EMUL_CONTINUE)
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
+ struct kvm_vcpu *vcpu = filp->private_data;
|
|
|
+ void __user *argp = (void __user *)arg;
|
|
|
+ int r;
|
|
|
|
|
|
- gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
|
|
|
- if (gpa == UNMAPPED_GVA)
|
|
|
- return X86EMUL_PROPAGATE_FAULT;
|
|
|
+ switch (ioctl) {
|
|
|
+ case KVM_GET_LAPIC: {
|
|
|
+ struct kvm_lapic_state lapic;
|
|
|
|
|
|
- /*
|
|
|
- * Is this MMIO handled locally?
|
|
|
- */
|
|
|
- mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
|
|
|
- if (mmio_dev) {
|
|
|
- kvm_iodevice_read(mmio_dev, gpa, bytes, val);
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
+ memset(&lapic, 0, sizeof lapic);
|
|
|
+ r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_to_user(argp, &lapic, sizeof lapic))
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ break;
|
|
|
}
|
|
|
+ case KVM_SET_LAPIC: {
|
|
|
+ struct kvm_lapic_state lapic;
|
|
|
|
|
|
- vcpu->mmio_needed = 1;
|
|
|
- vcpu->mmio_phys_addr = gpa;
|
|
|
- vcpu->mmio_size = bytes;
|
|
|
- vcpu->mmio_is_write = 0;
|
|
|
-
|
|
|
- return X86EMUL_UNHANDLEABLE;
|
|
|
-}
|
|
|
-
|
|
|
-static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
|
|
|
- const void *val, int bytes)
|
|
|
-{
|
|
|
- struct page *page;
|
|
|
- void *virt;
|
|
|
-
|
|
|
- if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
|
|
|
- return 0;
|
|
|
- page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
|
|
|
- if (!page)
|
|
|
- return 0;
|
|
|
- mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
|
|
|
- virt = kmap_atomic(page, KM_USER0);
|
|
|
- kvm_mmu_pte_write(vcpu, gpa, val, bytes);
|
|
|
- memcpy(virt + offset_in_page(gpa), val, bytes);
|
|
|
- kunmap_atomic(virt, KM_USER0);
|
|
|
- return 1;
|
|
|
-}
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&lapic, argp, sizeof lapic))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case KVM_INTERRUPT: {
|
|
|
+ struct kvm_interrupt irq;
|
|
|
|
|
|
-static int emulator_write_emulated_onepage(unsigned long addr,
|
|
|
- const void *val,
|
|
|
- unsigned int bytes,
|
|
|
- struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- struct kvm_io_device *mmio_dev;
|
|
|
- gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&irq, argp, sizeof irq))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case KVM_SET_CPUID: {
|
|
|
+ struct kvm_cpuid __user *cpuid_arg = argp;
|
|
|
+ struct kvm_cpuid cpuid;
|
|
|
|
|
|
- if (gpa == UNMAPPED_GVA) {
|
|
|
- kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
|
|
|
- return X86EMUL_PROPAGATE_FAULT;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ break;
|
|
|
}
|
|
|
+ case KVM_SET_CPUID2: {
|
|
|
+ struct kvm_cpuid2 __user *cpuid_arg = argp;
|
|
|
+ struct kvm_cpuid2 cpuid;
|
|
|
|
|
|
- if (emulator_write_phys(vcpu, gpa, val, bytes))
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
|
|
|
+ cpuid_arg->entries);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case KVM_GET_CPUID2: {
|
|
|
+ struct kvm_cpuid2 __user *cpuid_arg = argp;
|
|
|
+ struct kvm_cpuid2 cpuid;
|
|
|
|
|
|
- /*
|
|
|
- * Is this MMIO handled locally?
|
|
|
- */
|
|
|
- mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
|
|
|
- if (mmio_dev) {
|
|
|
- kvm_iodevice_write(mmio_dev, gpa, bytes, val);
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
|
|
|
+ cpuid_arg->entries);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ break;
|
|
|
}
|
|
|
+ case KVM_GET_MSRS:
|
|
|
+ r = msr_io(vcpu, argp, kvm_get_msr, 1);
|
|
|
+ break;
|
|
|
+ case KVM_SET_MSRS:
|
|
|
+ r = msr_io(vcpu, argp, do_set_msr, 0);
|
|
|
+ break;
|
|
|
+ case KVM_TPR_ACCESS_REPORTING: {
|
|
|
+ struct kvm_tpr_access_ctl tac;
|
|
|
|
|
|
- vcpu->mmio_needed = 1;
|
|
|
- vcpu->mmio_phys_addr = gpa;
|
|
|
- vcpu->mmio_size = bytes;
|
|
|
- vcpu->mmio_is_write = 1;
|
|
|
- memcpy(vcpu->mmio_data, val, bytes);
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&tac, argp, sizeof tac))
|
|
|
+ goto out;
|
|
|
+ r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_to_user(argp, &tac, sizeof tac))
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ break;
|
|
|
+ };
|
|
|
+ case KVM_SET_VAPIC_ADDR: {
|
|
|
+ struct kvm_vapic_addr va;
|
|
|
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
+ r = -EINVAL;
|
|
|
+ if (!irqchip_in_kernel(vcpu->kvm))
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&va, argp, sizeof va))
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ r = -EINVAL;
|
|
|
+ }
|
|
|
+out:
|
|
|
+ return r;
|
|
|
}
|
|
|
|
|
|
-int emulator_write_emulated(unsigned long addr,
|
|
|
- const void *val,
|
|
|
- unsigned int bytes,
|
|
|
- struct kvm_vcpu *vcpu)
|
|
|
+static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
|
|
|
{
|
|
|
- /* Crossing a page boundary? */
|
|
|
- if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
|
|
|
- int rc, now;
|
|
|
+ int ret;
|
|
|
|
|
|
- now = -addr & ~PAGE_MASK;
|
|
|
- rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
|
|
|
- if (rc != X86EMUL_CONTINUE)
|
|
|
- return rc;
|
|
|
- addr += now;
|
|
|
- val += now;
|
|
|
- bytes -= now;
|
|
|
- }
|
|
|
- return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
|
|
|
+ if (addr > (unsigned int)(-3 * PAGE_SIZE))
|
|
|
+ return -1;
|
|
|
+ ret = kvm_x86_ops->set_tss_addr(kvm, addr);
|
|
|
+ return ret;
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(emulator_write_emulated);
|
|
|
|
|
|
-static int emulator_cmpxchg_emulated(unsigned long addr,
|
|
|
- const void *old,
|
|
|
- const void *new,
|
|
|
- unsigned int bytes,
|
|
|
- struct kvm_vcpu *vcpu)
|
|
|
+static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
|
|
|
+ u32 kvm_nr_mmu_pages)
|
|
|
{
|
|
|
- static int reported;
|
|
|
+ if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
|
|
|
+ return -EINVAL;
|
|
|
|
|
|
- if (!reported) {
|
|
|
- reported = 1;
|
|
|
- printk(KERN_WARNING "kvm: emulating exchange as write\n");
|
|
|
- }
|
|
|
- return emulator_write_emulated(addr, new, bytes, vcpu);
|
|
|
-}
|
|
|
+ down_write(¤t->mm->mmap_sem);
|
|
|
|
|
|
-static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
|
|
|
-{
|
|
|
- return kvm_x86_ops->get_segment_base(vcpu, seg);
|
|
|
-}
|
|
|
+ kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
|
|
|
+ kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
|
|
|
|
|
|
-int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
|
|
|
-{
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
+ up_write(¤t->mm->mmap_sem);
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-int emulate_clts(struct kvm_vcpu *vcpu)
|
|
|
+static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
|
|
|
{
|
|
|
- kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
+ return kvm->arch.n_alloc_mmu_pages;
|
|
|
}
|
|
|
|
|
|
-int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
|
|
|
+gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
|
|
|
{
|
|
|
- struct kvm_vcpu *vcpu = ctxt->vcpu;
|
|
|
+ int i;
|
|
|
+ struct kvm_mem_alias *alias;
|
|
|
|
|
|
- switch (dr) {
|
|
|
- case 0 ... 3:
|
|
|
- *dest = kvm_x86_ops->get_dr(vcpu, dr);
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
- default:
|
|
|
- pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
|
|
|
- return X86EMUL_UNHANDLEABLE;
|
|
|
+ for (i = 0; i < kvm->arch.naliases; ++i) {
|
|
|
+ alias = &kvm->arch.aliases[i];
|
|
|
+ if (gfn >= alias->base_gfn
|
|
|
+ && gfn < alias->base_gfn + alias->npages)
|
|
|
+ return alias->target_gfn + gfn - alias->base_gfn;
|
|
|
}
|
|
|
+ return gfn;
|
|
|
}
|
|
|
|
|
|
-int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
|
|
|
+/*
|
|
|
+ * Set a new alias region. Aliases map a portion of physical memory into
|
|
|
+ * another portion. This is useful for memory windows, for example the PC
|
|
|
+ * VGA region.
|
|
|
+ */
|
|
|
+static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
|
|
|
+ struct kvm_memory_alias *alias)
|
|
|
{
|
|
|
- unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
|
|
|
- int exception;
|
|
|
+ int r, n;
|
|
|
+ struct kvm_mem_alias *p;
|
|
|
|
|
|
- kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
|
|
|
- if (exception) {
|
|
|
- /* FIXME: better handling */
|
|
|
- return X86EMUL_UNHANDLEABLE;
|
|
|
- }
|
|
|
- return X86EMUL_CONTINUE;
|
|
|
-}
|
|
|
+ r = -EINVAL;
|
|
|
+ /* General sanity checks */
|
|
|
+ if (alias->memory_size & (PAGE_SIZE - 1))
|
|
|
+ goto out;
|
|
|
+ if (alias->guest_phys_addr & (PAGE_SIZE - 1))
|
|
|
+ goto out;
|
|
|
+ if (alias->slot >= KVM_ALIAS_SLOTS)
|
|
|
+ goto out;
|
|
|
+ if (alias->guest_phys_addr + alias->memory_size
|
|
|
+ < alias->guest_phys_addr)
|
|
|
+ goto out;
|
|
|
+ if (alias->target_phys_addr + alias->memory_size
|
|
|
+ < alias->target_phys_addr)
|
|
|
+ goto out;
|
|
|
|
|
|
-void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
|
|
|
-{
|
|
|
- static int reported;
|
|
|
- u8 opcodes[4];
|
|
|
- unsigned long rip = vcpu->rip;
|
|
|
- unsigned long rip_linear;
|
|
|
+ down_write(¤t->mm->mmap_sem);
|
|
|
|
|
|
- rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
|
|
|
+ p = &kvm->arch.aliases[alias->slot];
|
|
|
+ p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
|
|
|
+ p->npages = alias->memory_size >> PAGE_SHIFT;
|
|
|
+ p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
|
|
|
|
|
|
- if (reported)
|
|
|
- return;
|
|
|
+ for (n = KVM_ALIAS_SLOTS; n > 0; --n)
|
|
|
+ if (kvm->arch.aliases[n - 1].npages)
|
|
|
+ break;
|
|
|
+ kvm->arch.naliases = n;
|
|
|
|
|
|
- emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
|
|
|
+ kvm_mmu_zap_all(kvm);
|
|
|
|
|
|
- printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
|
|
|
- context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
|
|
|
- reported = 1;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
|
|
|
+ up_write(¤t->mm->mmap_sem);
|
|
|
|
|
|
-struct x86_emulate_ops emulate_ops = {
|
|
|
- .read_std = emulator_read_std,
|
|
|
- .write_std = emulator_write_std,
|
|
|
- .read_emulated = emulator_read_emulated,
|
|
|
- .write_emulated = emulator_write_emulated,
|
|
|
- .cmpxchg_emulated = emulator_cmpxchg_emulated,
|
|
|
-};
|
|
|
+ return 0;
|
|
|
|
|
|
-int emulate_instruction(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_run *run,
|
|
|
- unsigned long cr2,
|
|
|
- u16 error_code)
|
|
|
+out:
|
|
|
+ return r;
|
|
|
+}
|
|
|
+
|
|
|
+static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
|
|
|
{
|
|
|
- struct x86_emulate_ctxt emulate_ctxt;
|
|
|
int r;
|
|
|
- int cs_db, cs_l;
|
|
|
|
|
|
- vcpu->mmio_fault_cr2 = cr2;
|
|
|
- kvm_x86_ops->cache_regs(vcpu);
|
|
|
-
|
|
|
- kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
|
|
|
-
|
|
|
- emulate_ctxt.vcpu = vcpu;
|
|
|
- emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
|
|
|
- emulate_ctxt.cr2 = cr2;
|
|
|
- emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
|
|
|
- ? X86EMUL_MODE_REAL : cs_l
|
|
|
- ? X86EMUL_MODE_PROT64 : cs_db
|
|
|
- ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
|
|
|
-
|
|
|
- if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
|
|
|
- emulate_ctxt.cs_base = 0;
|
|
|
- emulate_ctxt.ds_base = 0;
|
|
|
- emulate_ctxt.es_base = 0;
|
|
|
- emulate_ctxt.ss_base = 0;
|
|
|
- } else {
|
|
|
- emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
|
|
|
- emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
|
|
|
- emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
|
|
|
- emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
|
|
|
+ r = 0;
|
|
|
+ switch (chip->chip_id) {
|
|
|
+ case KVM_IRQCHIP_PIC_MASTER:
|
|
|
+ memcpy(&chip->chip.pic,
|
|
|
+ &pic_irqchip(kvm)->pics[0],
|
|
|
+ sizeof(struct kvm_pic_state));
|
|
|
+ break;
|
|
|
+ case KVM_IRQCHIP_PIC_SLAVE:
|
|
|
+ memcpy(&chip->chip.pic,
|
|
|
+ &pic_irqchip(kvm)->pics[1],
|
|
|
+ sizeof(struct kvm_pic_state));
|
|
|
+ break;
|
|
|
+ case KVM_IRQCHIP_IOAPIC:
|
|
|
+ memcpy(&chip->chip.ioapic,
|
|
|
+ ioapic_irqchip(kvm),
|
|
|
+ sizeof(struct kvm_ioapic_state));
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ r = -EINVAL;
|
|
|
+ break;
|
|
|
}
|
|
|
+ return r;
|
|
|
+}
|
|
|
|
|
|
- emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
|
|
|
- emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
|
|
|
-
|
|
|
- vcpu->mmio_is_write = 0;
|
|
|
- vcpu->pio.string = 0;
|
|
|
- r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
|
|
|
- if (vcpu->pio.string)
|
|
|
- return EMULATE_DO_MMIO;
|
|
|
+static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
|
|
|
+{
|
|
|
+ int r;
|
|
|
|
|
|
- if ((r || vcpu->mmio_is_write) && run) {
|
|
|
- run->exit_reason = KVM_EXIT_MMIO;
|
|
|
- run->mmio.phys_addr = vcpu->mmio_phys_addr;
|
|
|
- memcpy(run->mmio.data, vcpu->mmio_data, 8);
|
|
|
- run->mmio.len = vcpu->mmio_size;
|
|
|
- run->mmio.is_write = vcpu->mmio_is_write;
|
|
|
+ r = 0;
|
|
|
+ switch (chip->chip_id) {
|
|
|
+ case KVM_IRQCHIP_PIC_MASTER:
|
|
|
+ memcpy(&pic_irqchip(kvm)->pics[0],
|
|
|
+ &chip->chip.pic,
|
|
|
+ sizeof(struct kvm_pic_state));
|
|
|
+ break;
|
|
|
+ case KVM_IRQCHIP_PIC_SLAVE:
|
|
|
+ memcpy(&pic_irqchip(kvm)->pics[1],
|
|
|
+ &chip->chip.pic,
|
|
|
+ sizeof(struct kvm_pic_state));
|
|
|
+ break;
|
|
|
+ case KVM_IRQCHIP_IOAPIC:
|
|
|
+ memcpy(ioapic_irqchip(kvm),
|
|
|
+ &chip->chip.ioapic,
|
|
|
+ sizeof(struct kvm_ioapic_state));
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ r = -EINVAL;
|
|
|
+ break;
|
|
|
}
|
|
|
+ kvm_pic_update_irq(pic_irqchip(kvm));
|
|
|
+ return r;
|
|
|
+}
|
|
|
|
|
|
- if (r) {
|
|
|
- if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
|
|
|
- return EMULATE_DONE;
|
|
|
- if (!vcpu->mmio_needed) {
|
|
|
- kvm_report_emulation_failure(vcpu, "mmio");
|
|
|
- return EMULATE_FAIL;
|
|
|
- }
|
|
|
- return EMULATE_DO_MMIO;
|
|
|
- }
|
|
|
+/*
|
|
|
+ * Get (and clear) the dirty memory log for a memory slot.
|
|
|
+ */
|
|
|
+int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
|
|
|
+ struct kvm_dirty_log *log)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+ int n;
|
|
|
+ struct kvm_memory_slot *memslot;
|
|
|
+ int is_dirty = 0;
|
|
|
|
|
|
- kvm_x86_ops->decache_regs(vcpu);
|
|
|
- kvm_x86_ops->set_rflags(vcpu, emulate_ctxt.eflags);
|
|
|
+ down_write(¤t->mm->mmap_sem);
|
|
|
|
|
|
- if (vcpu->mmio_is_write) {
|
|
|
- vcpu->mmio_needed = 0;
|
|
|
- return EMULATE_DO_MMIO;
|
|
|
- }
|
|
|
+ r = kvm_get_dirty_log(kvm, log, &is_dirty);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
|
|
|
- return EMULATE_DONE;
|
|
|
+ /* If nothing is dirty, don't bother messing with page tables. */
|
|
|
+ if (is_dirty) {
|
|
|
+ kvm_mmu_slot_remove_write_access(kvm, log->slot);
|
|
|
+ kvm_flush_remote_tlbs(kvm);
|
|
|
+ memslot = &kvm->memslots[log->slot];
|
|
|
+ n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
|
|
|
+ memset(memslot->dirty_bitmap, 0, n);
|
|
|
+ }
|
|
|
+ r = 0;
|
|
|
+out:
|
|
|
+ up_write(¤t->mm->mmap_sem);
|
|
|
+ return r;
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(emulate_instruction);
|
|
|
|
|
|
-/*
|
|
|
- * The vCPU has executed a HLT instruction with in-kernel mode enabled.
|
|
|
- */
|
|
|
-static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
|
|
|
+long kvm_arch_vm_ioctl(struct file *filp,
|
|
|
+ unsigned int ioctl, unsigned long arg)
|
|
|
{
|
|
|
- DECLARE_WAITQUEUE(wait, current);
|
|
|
+ struct kvm *kvm = filp->private_data;
|
|
|
+ void __user *argp = (void __user *)arg;
|
|
|
+ int r = -EINVAL;
|
|
|
|
|
|
- add_wait_queue(&vcpu->wq, &wait);
|
|
|
+ switch (ioctl) {
|
|
|
+ case KVM_SET_TSS_ADDR:
|
|
|
+ r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
|
|
|
+ if (r < 0)
|
|
|
+ goto out;
|
|
|
+ break;
|
|
|
+ case KVM_SET_MEMORY_REGION: {
|
|
|
+ struct kvm_memory_region kvm_mem;
|
|
|
+ struct kvm_userspace_memory_region kvm_userspace_mem;
|
|
|
|
|
|
- /*
|
|
|
- * We will block until either an interrupt or a signal wakes us up
|
|
|
- */
|
|
|
- while (!kvm_cpu_has_interrupt(vcpu)
|
|
|
- && !signal_pending(current)
|
|
|
- && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
|
|
|
- && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
|
|
|
- set_current_state(TASK_INTERRUPTIBLE);
|
|
|
- vcpu_put(vcpu);
|
|
|
- schedule();
|
|
|
- vcpu_load(vcpu);
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
|
|
|
+ goto out;
|
|
|
+ kvm_userspace_mem.slot = kvm_mem.slot;
|
|
|
+ kvm_userspace_mem.flags = kvm_mem.flags;
|
|
|
+ kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
|
|
|
+ kvm_userspace_mem.memory_size = kvm_mem.memory_size;
|
|
|
+ r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ break;
|
|
|
}
|
|
|
+ case KVM_SET_NR_MMU_PAGES:
|
|
|
+ r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ break;
|
|
|
+ case KVM_GET_NR_MMU_PAGES:
|
|
|
+ r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
|
|
|
+ break;
|
|
|
+ case KVM_SET_MEMORY_ALIAS: {
|
|
|
+ struct kvm_memory_alias alias;
|
|
|
|
|
|
- __set_current_state(TASK_RUNNING);
|
|
|
- remove_wait_queue(&vcpu->wq, &wait);
|
|
|
-}
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&alias, argp, sizeof alias))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case KVM_CREATE_IRQCHIP:
|
|
|
+ r = -ENOMEM;
|
|
|
+ kvm->arch.vpic = kvm_create_pic(kvm);
|
|
|
+ if (kvm->arch.vpic) {
|
|
|
+ r = kvm_ioapic_init(kvm);
|
|
|
+ if (r) {
|
|
|
+ kfree(kvm->arch.vpic);
|
|
|
+ kvm->arch.vpic = NULL;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ } else
|
|
|
+ goto out;
|
|
|
+ break;
|
|
|
+ case KVM_IRQ_LINE: {
|
|
|
+ struct kvm_irq_level irq_event;
|
|
|
|
|
|
-int kvm_emulate_halt(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- ++vcpu->stat.halt_exits;
|
|
|
- if (irqchip_in_kernel(vcpu->kvm)) {
|
|
|
- vcpu->mp_state = VCPU_MP_STATE_HALTED;
|
|
|
- kvm_vcpu_block(vcpu);
|
|
|
- if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
|
|
|
- return -EINTR;
|
|
|
- return 1;
|
|
|
- } else {
|
|
|
- vcpu->run->exit_reason = KVM_EXIT_HLT;
|
|
|
- return 0;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&irq_event, argp, sizeof irq_event))
|
|
|
+ goto out;
|
|
|
+ if (irqchip_in_kernel(kvm)) {
|
|
|
+ mutex_lock(&kvm->lock);
|
|
|
+ if (irq_event.irq < 16)
|
|
|
+ kvm_pic_set_irq(pic_irqchip(kvm),
|
|
|
+ irq_event.irq,
|
|
|
+ irq_event.level);
|
|
|
+ kvm_ioapic_set_irq(kvm->arch.vioapic,
|
|
|
+ irq_event.irq,
|
|
|
+ irq_event.level);
|
|
|
+ mutex_unlock(&kvm->lock);
|
|
|
+ r = 0;
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_emulate_halt);
|
|
|
+ case KVM_GET_IRQCHIP: {
|
|
|
+ /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
|
|
|
+ struct kvm_irqchip chip;
|
|
|
|
|
|
-int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
|
|
|
-{
|
|
|
- unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&chip, argp, sizeof chip))
|
|
|
+ goto out;
|
|
|
+ r = -ENXIO;
|
|
|
+ if (!irqchip_in_kernel(kvm))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_to_user(argp, &chip, sizeof chip))
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case KVM_SET_IRQCHIP: {
|
|
|
+ /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
|
|
|
+ struct kvm_irqchip chip;
|
|
|
|
|
|
- kvm_x86_ops->cache_regs(vcpu);
|
|
|
- ret = -KVM_EINVAL;
|
|
|
-#ifdef CONFIG_X86_64
|
|
|
- if (is_long_mode(vcpu)) {
|
|
|
- nr = vcpu->regs[VCPU_REGS_RAX];
|
|
|
- a0 = vcpu->regs[VCPU_REGS_RDI];
|
|
|
- a1 = vcpu->regs[VCPU_REGS_RSI];
|
|
|
- a2 = vcpu->regs[VCPU_REGS_RDX];
|
|
|
- a3 = vcpu->regs[VCPU_REGS_RCX];
|
|
|
- a4 = vcpu->regs[VCPU_REGS_R8];
|
|
|
- a5 = vcpu->regs[VCPU_REGS_R9];
|
|
|
- } else
|
|
|
-#endif
|
|
|
- {
|
|
|
- nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
|
|
|
- a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
|
|
|
- a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
|
|
|
- a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
|
|
|
- a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
|
|
|
- a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
|
|
|
- a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&chip, argp, sizeof chip))
|
|
|
+ goto out;
|
|
|
+ r = -ENXIO;
|
|
|
+ if (!irqchip_in_kernel(kvm))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case KVM_GET_SUPPORTED_CPUID: {
|
|
|
+ struct kvm_cpuid2 __user *cpuid_arg = argp;
|
|
|
+ struct kvm_cpuid2 cpuid;
|
|
|
+
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
|
|
|
+ goto out;
|
|
|
+ r = kvm_vm_ioctl_get_supported_cpuid(kvm, &cpuid,
|
|
|
+ cpuid_arg->entries);
|
|
|
+ if (r)
|
|
|
+ goto out;
|
|
|
+
|
|
|
+ r = -EFAULT;
|
|
|
+ if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
|
|
|
+ goto out;
|
|
|
+ r = 0;
|
|
|
+ break;
|
|
|
}
|
|
|
- switch (nr) {
|
|
|
default:
|
|
|
- run->hypercall.nr = nr;
|
|
|
- run->hypercall.args[0] = a0;
|
|
|
- run->hypercall.args[1] = a1;
|
|
|
- run->hypercall.args[2] = a2;
|
|
|
- run->hypercall.args[3] = a3;
|
|
|
- run->hypercall.args[4] = a4;
|
|
|
- run->hypercall.args[5] = a5;
|
|
|
- run->hypercall.ret = ret;
|
|
|
- run->hypercall.longmode = is_long_mode(vcpu);
|
|
|
- kvm_x86_ops->decache_regs(vcpu);
|
|
|
- return 0;
|
|
|
+ ;
|
|
|
}
|
|
|
- vcpu->regs[VCPU_REGS_RAX] = ret;
|
|
|
- kvm_x86_ops->decache_regs(vcpu);
|
|
|
- return 1;
|
|
|
+out:
|
|
|
+ return r;
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_hypercall);
|
|
|
|
|
|
-static u64 mk_cr_64(u64 curr_cr, u32 new_val)
|
|
|
+static void kvm_init_msr_list(void)
|
|
|
{
|
|
|
- return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
|
|
|
+ u32 dummy[2];
|
|
|
+ unsigned i, j;
|
|
|
+
|
|
|
+ for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
|
|
|
+ if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
|
|
|
+ continue;
|
|
|
+ if (j < i)
|
|
|
+ msrs_to_save[j] = msrs_to_save[i];
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ num_msrs_to_save = j;
|
|
|
}
|
|
|
|
|
|
-void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
|
|
|
+/*
|
|
|
+ * Only apic need an MMIO device hook, so shortcut now..
|
|
|
+ */
|
|
|
+static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
|
|
|
+ gpa_t addr)
|
|
|
{
|
|
|
- struct descriptor_table dt = { limit, base };
|
|
|
+ struct kvm_io_device *dev;
|
|
|
|
|
|
- kvm_x86_ops->set_gdt(vcpu, &dt);
|
|
|
+ if (vcpu->arch.apic) {
|
|
|
+ dev = &vcpu->arch.apic->dev;
|
|
|
+ if (dev->in_range(dev, addr))
|
|
|
+ return dev;
|
|
|
+ }
|
|
|
+ return NULL;
|
|
|
}
|
|
|
|
|
|
-void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
|
|
|
+
|
|
|
+static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
|
|
|
+ gpa_t addr)
|
|
|
{
|
|
|
- struct descriptor_table dt = { limit, base };
|
|
|
+ struct kvm_io_device *dev;
|
|
|
|
|
|
- kvm_x86_ops->set_idt(vcpu, &dt);
|
|
|
+ dev = vcpu_find_pervcpu_dev(vcpu, addr);
|
|
|
+ if (dev == NULL)
|
|
|
+ dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
|
|
|
+ return dev;
|
|
|
}
|
|
|
|
|
|
-void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
|
|
|
- unsigned long *rflags)
|
|
|
+int emulator_read_std(unsigned long addr,
|
|
|
+ void *val,
|
|
|
+ unsigned int bytes,
|
|
|
+ struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- lmsw(vcpu, msw);
|
|
|
- *rflags = kvm_x86_ops->get_rflags(vcpu);
|
|
|
-}
|
|
|
+ void *data = val;
|
|
|
+ int r = X86EMUL_CONTINUE;
|
|
|
|
|
|
-unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
|
|
|
-{
|
|
|
- kvm_x86_ops->decache_cr4_guest_bits(vcpu);
|
|
|
- switch (cr) {
|
|
|
- case 0:
|
|
|
- return vcpu->cr0;
|
|
|
- case 2:
|
|
|
- return vcpu->cr2;
|
|
|
- case 3:
|
|
|
- return vcpu->cr3;
|
|
|
- case 4:
|
|
|
- return vcpu->cr4;
|
|
|
- default:
|
|
|
- vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
|
|
|
- return 0;
|
|
|
- }
|
|
|
-}
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ while (bytes) {
|
|
|
+ gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
|
|
|
+ unsigned offset = addr & (PAGE_SIZE-1);
|
|
|
+ unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
|
|
|
+ int ret;
|
|
|
|
|
|
-void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
|
|
|
- unsigned long *rflags)
|
|
|
-{
|
|
|
- switch (cr) {
|
|
|
- case 0:
|
|
|
- set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
|
|
|
- *rflags = kvm_x86_ops->get_rflags(vcpu);
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- vcpu->cr2 = val;
|
|
|
- break;
|
|
|
- case 3:
|
|
|
- set_cr3(vcpu, val);
|
|
|
- break;
|
|
|
- case 4:
|
|
|
- set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
|
|
|
- break;
|
|
|
- default:
|
|
|
- vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
|
|
|
+ if (gpa == UNMAPPED_GVA) {
|
|
|
+ r = X86EMUL_PROPAGATE_FAULT;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
|
|
|
+ if (ret < 0) {
|
|
|
+ r = X86EMUL_UNHANDLEABLE;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ bytes -= tocopy;
|
|
|
+ data += tocopy;
|
|
|
+ addr += tocopy;
|
|
|
}
|
|
|
+out:
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
+ return r;
|
|
|
}
|
|
|
+EXPORT_SYMBOL_GPL(emulator_read_std);
|
|
|
|
|
|
-/*
|
|
|
- * Register the para guest with the host:
|
|
|
- */
|
|
|
-static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
|
|
|
+static int emulator_read_emulated(unsigned long addr,
|
|
|
+ void *val,
|
|
|
+ unsigned int bytes,
|
|
|
+ struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- struct kvm_vcpu_para_state *para_state;
|
|
|
- hpa_t para_state_hpa, hypercall_hpa;
|
|
|
- struct page *para_state_page;
|
|
|
- unsigned char *hypercall;
|
|
|
- gpa_t hypercall_gpa;
|
|
|
-
|
|
|
- printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
|
|
|
- printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
|
|
|
+ struct kvm_io_device *mmio_dev;
|
|
|
+ gpa_t gpa;
|
|
|
|
|
|
- /*
|
|
|
- * Needs to be page aligned:
|
|
|
- */
|
|
|
- if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
|
|
|
- goto err_gp;
|
|
|
+ if (vcpu->mmio_read_completed) {
|
|
|
+ memcpy(val, vcpu->mmio_data, bytes);
|
|
|
+ vcpu->mmio_read_completed = 0;
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
+ }
|
|
|
|
|
|
- para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
|
|
|
- printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
|
|
|
- if (is_error_hpa(para_state_hpa))
|
|
|
- goto err_gp;
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
|
|
|
- mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
|
|
|
- para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
|
|
|
- para_state = kmap(para_state_page);
|
|
|
+ /* For APIC access vmexit */
|
|
|
+ if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
|
|
|
+ goto mmio;
|
|
|
|
|
|
- printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
|
|
|
- printk(KERN_DEBUG ".... size: %d\n", para_state->size);
|
|
|
+ if (emulator_read_std(addr, val, bytes, vcpu)
|
|
|
+ == X86EMUL_CONTINUE)
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
+ if (gpa == UNMAPPED_GVA)
|
|
|
+ return X86EMUL_PROPAGATE_FAULT;
|
|
|
|
|
|
- para_state->host_version = KVM_PARA_API_VERSION;
|
|
|
+mmio:
|
|
|
/*
|
|
|
- * We cannot support guests that try to register themselves
|
|
|
- * with a newer API version than the host supports:
|
|
|
+ * Is this MMIO handled locally?
|
|
|
*/
|
|
|
- if (para_state->guest_version > KVM_PARA_API_VERSION) {
|
|
|
- para_state->ret = -KVM_EINVAL;
|
|
|
- goto err_kunmap_skip;
|
|
|
+ mutex_lock(&vcpu->kvm->lock);
|
|
|
+ mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
|
|
|
+ if (mmio_dev) {
|
|
|
+ kvm_iodevice_read(mmio_dev, gpa, bytes, val);
|
|
|
+ mutex_unlock(&vcpu->kvm->lock);
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
}
|
|
|
+ mutex_unlock(&vcpu->kvm->lock);
|
|
|
|
|
|
- hypercall_gpa = para_state->hypercall_gpa;
|
|
|
- hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
|
|
|
- printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
|
|
|
- if (is_error_hpa(hypercall_hpa)) {
|
|
|
- para_state->ret = -KVM_EINVAL;
|
|
|
- goto err_kunmap_skip;
|
|
|
- }
|
|
|
+ vcpu->mmio_needed = 1;
|
|
|
+ vcpu->mmio_phys_addr = gpa;
|
|
|
+ vcpu->mmio_size = bytes;
|
|
|
+ vcpu->mmio_is_write = 0;
|
|
|
|
|
|
- printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
|
|
|
- vcpu->para_state_page = para_state_page;
|
|
|
- vcpu->para_state_gpa = para_state_gpa;
|
|
|
- vcpu->hypercall_gpa = hypercall_gpa;
|
|
|
+ return X86EMUL_UNHANDLEABLE;
|
|
|
+}
|
|
|
|
|
|
- mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
|
|
|
- hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
|
|
|
- KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
|
|
|
- kvm_x86_ops->patch_hypercall(vcpu, hypercall);
|
|
|
- kunmap_atomic(hypercall, KM_USER1);
|
|
|
+static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
|
|
|
+ const void *val, int bytes)
|
|
|
+{
|
|
|
+ int ret;
|
|
|
|
|
|
- para_state->ret = 0;
|
|
|
-err_kunmap_skip:
|
|
|
- kunmap(para_state_page);
|
|
|
- return 0;
|
|
|
-err_gp:
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
|
|
|
+ if (ret < 0) {
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ kvm_mmu_pte_write(vcpu, gpa, val, bytes);
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
-int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
|
|
|
+static int emulator_write_emulated_onepage(unsigned long addr,
|
|
|
+ const void *val,
|
|
|
+ unsigned int bytes,
|
|
|
+ struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- u64 data;
|
|
|
+ struct kvm_io_device *mmio_dev;
|
|
|
+ gpa_t gpa;
|
|
|
|
|
|
- switch (msr) {
|
|
|
- case 0xc0010010: /* SYSCFG */
|
|
|
- case 0xc0010015: /* HWCR */
|
|
|
- case MSR_IA32_PLATFORM_ID:
|
|
|
- case MSR_IA32_P5_MC_ADDR:
|
|
|
- case MSR_IA32_P5_MC_TYPE:
|
|
|
- case MSR_IA32_MC0_CTL:
|
|
|
- case MSR_IA32_MCG_STATUS:
|
|
|
- case MSR_IA32_MCG_CAP:
|
|
|
- case MSR_IA32_MC0_MISC:
|
|
|
- case MSR_IA32_MC0_MISC+4:
|
|
|
- case MSR_IA32_MC0_MISC+8:
|
|
|
- case MSR_IA32_MC0_MISC+12:
|
|
|
- case MSR_IA32_MC0_MISC+16:
|
|
|
- case MSR_IA32_UCODE_REV:
|
|
|
- case MSR_IA32_PERF_STATUS:
|
|
|
- case MSR_IA32_EBL_CR_POWERON:
|
|
|
- /* MTRR registers */
|
|
|
- case 0xfe:
|
|
|
- case 0x200 ... 0x2ff:
|
|
|
- data = 0;
|
|
|
- break;
|
|
|
- case 0xcd: /* fsb frequency */
|
|
|
- data = 3;
|
|
|
- break;
|
|
|
- case MSR_IA32_APICBASE:
|
|
|
- data = kvm_get_apic_base(vcpu);
|
|
|
- break;
|
|
|
- case MSR_IA32_MISC_ENABLE:
|
|
|
- data = vcpu->ia32_misc_enable_msr;
|
|
|
- break;
|
|
|
-#ifdef CONFIG_X86_64
|
|
|
- case MSR_EFER:
|
|
|
- data = vcpu->shadow_efer;
|
|
|
- break;
|
|
|
-#endif
|
|
|
- default:
|
|
|
- pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
|
|
|
- return 1;
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
+
|
|
|
+ if (gpa == UNMAPPED_GVA) {
|
|
|
+ kvm_inject_page_fault(vcpu, addr, 2);
|
|
|
+ return X86EMUL_PROPAGATE_FAULT;
|
|
|
}
|
|
|
- *pdata = data;
|
|
|
- return 0;
|
|
|
+
|
|
|
+ /* For APIC access vmexit */
|
|
|
+ if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
|
|
|
+ goto mmio;
|
|
|
+
|
|
|
+ if (emulator_write_phys(vcpu, gpa, val, bytes))
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
+
|
|
|
+mmio:
|
|
|
+ /*
|
|
|
+ * Is this MMIO handled locally?
|
|
|
+ */
|
|
|
+ mutex_lock(&vcpu->kvm->lock);
|
|
|
+ mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
|
|
|
+ if (mmio_dev) {
|
|
|
+ kvm_iodevice_write(mmio_dev, gpa, bytes, val);
|
|
|
+ mutex_unlock(&vcpu->kvm->lock);
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
+ }
|
|
|
+ mutex_unlock(&vcpu->kvm->lock);
|
|
|
+
|
|
|
+ vcpu->mmio_needed = 1;
|
|
|
+ vcpu->mmio_phys_addr = gpa;
|
|
|
+ vcpu->mmio_size = bytes;
|
|
|
+ vcpu->mmio_is_write = 1;
|
|
|
+ memcpy(vcpu->mmio_data, val, bytes);
|
|
|
+
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_get_msr_common);
|
|
|
|
|
|
-/*
|
|
|
- * Reads an msr value (of 'msr_index') into 'pdata'.
|
|
|
- * Returns 0 on success, non-0 otherwise.
|
|
|
- * Assumes vcpu_load() was already called.
|
|
|
- */
|
|
|
-int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
|
|
|
+int emulator_write_emulated(unsigned long addr,
|
|
|
+ const void *val,
|
|
|
+ unsigned int bytes,
|
|
|
+ struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
|
|
|
-}
|
|
|
+ /* Crossing a page boundary? */
|
|
|
+ if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
|
|
|
+ int rc, now;
|
|
|
|
|
|
-#ifdef CONFIG_X86_64
|
|
|
+ now = -addr & ~PAGE_MASK;
|
|
|
+ rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
|
|
|
+ if (rc != X86EMUL_CONTINUE)
|
|
|
+ return rc;
|
|
|
+ addr += now;
|
|
|
+ val += now;
|
|
|
+ bytes -= now;
|
|
|
+ }
|
|
|
+ return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(emulator_write_emulated);
|
|
|
|
|
|
-static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
|
|
|
+static int emulator_cmpxchg_emulated(unsigned long addr,
|
|
|
+ const void *old,
|
|
|
+ const void *new,
|
|
|
+ unsigned int bytes,
|
|
|
+ struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- if (efer & EFER_RESERVED_BITS) {
|
|
|
- printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
|
|
|
- efer);
|
|
|
- inject_gp(vcpu);
|
|
|
- return;
|
|
|
- }
|
|
|
+ static int reported;
|
|
|
|
|
|
- if (is_paging(vcpu)
|
|
|
- && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
|
|
|
- printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
|
|
|
- inject_gp(vcpu);
|
|
|
- return;
|
|
|
+ if (!reported) {
|
|
|
+ reported = 1;
|
|
|
+ printk(KERN_WARNING "kvm: emulating exchange as write\n");
|
|
|
}
|
|
|
+#ifndef CONFIG_X86_64
|
|
|
+ /* guests cmpxchg8b have to be emulated atomically */
|
|
|
+ if (bytes == 8) {
|
|
|
+ gpa_t gpa;
|
|
|
+ struct page *page;
|
|
|
+ char *addr;
|
|
|
+ u64 val;
|
|
|
|
|
|
- kvm_x86_ops->set_efer(vcpu, efer);
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
|
|
|
|
|
|
- efer &= ~EFER_LMA;
|
|
|
- efer |= vcpu->shadow_efer & EFER_LMA;
|
|
|
+ if (gpa == UNMAPPED_GVA ||
|
|
|
+ (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
|
|
|
+ goto emul_write;
|
|
|
|
|
|
- vcpu->shadow_efer = efer;
|
|
|
-}
|
|
|
+ if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
|
|
|
+ goto emul_write;
|
|
|
|
|
|
+ val = *(u64 *)new;
|
|
|
+ page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
|
|
|
+ addr = kmap_atomic(page, KM_USER0);
|
|
|
+ set_64bit((u64 *)(addr + offset_in_page(gpa)), val);
|
|
|
+ kunmap_atomic(addr, KM_USER0);
|
|
|
+ kvm_release_page_dirty(page);
|
|
|
+ emul_write:
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
+ }
|
|
|
#endif
|
|
|
|
|
|
-int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
|
|
|
-{
|
|
|
- switch (msr) {
|
|
|
-#ifdef CONFIG_X86_64
|
|
|
- case MSR_EFER:
|
|
|
- set_efer(vcpu, data);
|
|
|
- break;
|
|
|
-#endif
|
|
|
- case MSR_IA32_MC0_STATUS:
|
|
|
- pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
|
|
|
- __FUNCTION__, data);
|
|
|
- break;
|
|
|
- case MSR_IA32_MCG_STATUS:
|
|
|
- pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
|
|
|
- __FUNCTION__, data);
|
|
|
- break;
|
|
|
- case MSR_IA32_UCODE_REV:
|
|
|
- case MSR_IA32_UCODE_WRITE:
|
|
|
- case 0x200 ... 0x2ff: /* MTRRs */
|
|
|
- break;
|
|
|
- case MSR_IA32_APICBASE:
|
|
|
- kvm_set_apic_base(vcpu, data);
|
|
|
- break;
|
|
|
- case MSR_IA32_MISC_ENABLE:
|
|
|
- vcpu->ia32_misc_enable_msr = data;
|
|
|
- break;
|
|
|
- /*
|
|
|
- * This is the 'probe whether the host is KVM' logic:
|
|
|
- */
|
|
|
- case MSR_KVM_API_MAGIC:
|
|
|
- return vcpu_register_para(vcpu, data);
|
|
|
+ return emulator_write_emulated(addr, new, bytes, vcpu);
|
|
|
+}
|
|
|
|
|
|
- default:
|
|
|
- pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
|
|
|
- return 1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
+static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
|
|
|
+{
|
|
|
+ return kvm_x86_ops->get_segment_base(vcpu, seg);
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_set_msr_common);
|
|
|
|
|
|
-/*
|
|
|
- * Writes msr value into into the appropriate "register".
|
|
|
- * Returns 0 on success, non-0 otherwise.
|
|
|
- * Assumes vcpu_load() was already called.
|
|
|
- */
|
|
|
-int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
|
|
|
+int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
|
|
|
{
|
|
|
- return kvm_x86_ops->set_msr(vcpu, msr_index, data);
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
}
|
|
|
|
|
|
-void kvm_resched(struct kvm_vcpu *vcpu)
|
|
|
+int emulate_clts(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- if (!need_resched())
|
|
|
- return;
|
|
|
- cond_resched();
|
|
|
+ kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_resched);
|
|
|
|
|
|
-void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
|
|
|
+int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
|
|
|
{
|
|
|
- int i;
|
|
|
- u32 function;
|
|
|
- struct kvm_cpuid_entry *e, *best;
|
|
|
+ struct kvm_vcpu *vcpu = ctxt->vcpu;
|
|
|
|
|
|
- kvm_x86_ops->cache_regs(vcpu);
|
|
|
- function = vcpu->regs[VCPU_REGS_RAX];
|
|
|
- vcpu->regs[VCPU_REGS_RAX] = 0;
|
|
|
- vcpu->regs[VCPU_REGS_RBX] = 0;
|
|
|
- vcpu->regs[VCPU_REGS_RCX] = 0;
|
|
|
- vcpu->regs[VCPU_REGS_RDX] = 0;
|
|
|
- best = NULL;
|
|
|
- for (i = 0; i < vcpu->cpuid_nent; ++i) {
|
|
|
- e = &vcpu->cpuid_entries[i];
|
|
|
- if (e->function == function) {
|
|
|
- best = e;
|
|
|
- break;
|
|
|
+ switch (dr) {
|
|
|
+ case 0 ... 3:
|
|
|
+ *dest = kvm_x86_ops->get_dr(vcpu, dr);
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
+ default:
|
|
|
+ pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
|
|
|
+ return X86EMUL_UNHANDLEABLE;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
|
|
|
+{
|
|
|
+ unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
|
|
|
+ int exception;
|
|
|
+
|
|
|
+ kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
|
|
|
+ if (exception) {
|
|
|
+ /* FIXME: better handling */
|
|
|
+ return X86EMUL_UNHANDLEABLE;
|
|
|
+ }
|
|
|
+ return X86EMUL_CONTINUE;
|
|
|
+}
|
|
|
+
|
|
|
+void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
|
|
|
+{
|
|
|
+ static int reported;
|
|
|
+ u8 opcodes[4];
|
|
|
+ unsigned long rip = vcpu->arch.rip;
|
|
|
+ unsigned long rip_linear;
|
|
|
+
|
|
|
+ rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
|
|
|
+
|
|
|
+ if (reported)
|
|
|
+ return;
|
|
|
+
|
|
|
+ emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
|
|
|
+
|
|
|
+ printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
|
|
|
+ context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
|
|
|
+ reported = 1;
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
|
|
|
+
|
|
|
+struct x86_emulate_ops emulate_ops = {
|
|
|
+ .read_std = emulator_read_std,
|
|
|
+ .read_emulated = emulator_read_emulated,
|
|
|
+ .write_emulated = emulator_write_emulated,
|
|
|
+ .cmpxchg_emulated = emulator_cmpxchg_emulated,
|
|
|
+};
|
|
|
+
|
|
|
+int emulate_instruction(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_run *run,
|
|
|
+ unsigned long cr2,
|
|
|
+ u16 error_code,
|
|
|
+ int emulation_type)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+ struct decode_cache *c;
|
|
|
+
|
|
|
+ vcpu->arch.mmio_fault_cr2 = cr2;
|
|
|
+ kvm_x86_ops->cache_regs(vcpu);
|
|
|
+
|
|
|
+ vcpu->mmio_is_write = 0;
|
|
|
+ vcpu->arch.pio.string = 0;
|
|
|
+
|
|
|
+ if (!(emulation_type & EMULTYPE_NO_DECODE)) {
|
|
|
+ int cs_db, cs_l;
|
|
|
+ kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
|
|
|
+
|
|
|
+ vcpu->arch.emulate_ctxt.vcpu = vcpu;
|
|
|
+ vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
|
|
|
+ vcpu->arch.emulate_ctxt.mode =
|
|
|
+ (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
|
|
|
+ ? X86EMUL_MODE_REAL : cs_l
|
|
|
+ ? X86EMUL_MODE_PROT64 : cs_db
|
|
|
+ ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
|
|
|
+
|
|
|
+ if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
|
|
|
+ vcpu->arch.emulate_ctxt.cs_base = 0;
|
|
|
+ vcpu->arch.emulate_ctxt.ds_base = 0;
|
|
|
+ vcpu->arch.emulate_ctxt.es_base = 0;
|
|
|
+ vcpu->arch.emulate_ctxt.ss_base = 0;
|
|
|
+ } else {
|
|
|
+ vcpu->arch.emulate_ctxt.cs_base =
|
|
|
+ get_segment_base(vcpu, VCPU_SREG_CS);
|
|
|
+ vcpu->arch.emulate_ctxt.ds_base =
|
|
|
+ get_segment_base(vcpu, VCPU_SREG_DS);
|
|
|
+ vcpu->arch.emulate_ctxt.es_base =
|
|
|
+ get_segment_base(vcpu, VCPU_SREG_ES);
|
|
|
+ vcpu->arch.emulate_ctxt.ss_base =
|
|
|
+ get_segment_base(vcpu, VCPU_SREG_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ vcpu->arch.emulate_ctxt.gs_base =
|
|
|
+ get_segment_base(vcpu, VCPU_SREG_GS);
|
|
|
+ vcpu->arch.emulate_ctxt.fs_base =
|
|
|
+ get_segment_base(vcpu, VCPU_SREG_FS);
|
|
|
+
|
|
|
+ r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
|
|
|
+
|
|
|
+ /* Reject the instructions other than VMCALL/VMMCALL when
|
|
|
+ * try to emulate invalid opcode */
|
|
|
+ c = &vcpu->arch.emulate_ctxt.decode;
|
|
|
+ if ((emulation_type & EMULTYPE_TRAP_UD) &&
|
|
|
+ (!(c->twobyte && c->b == 0x01 &&
|
|
|
+ (c->modrm_reg == 0 || c->modrm_reg == 3) &&
|
|
|
+ c->modrm_mod == 3 && c->modrm_rm == 1)))
|
|
|
+ return EMULATE_FAIL;
|
|
|
+
|
|
|
+ ++vcpu->stat.insn_emulation;
|
|
|
+ if (r) {
|
|
|
+ ++vcpu->stat.insn_emulation_fail;
|
|
|
+ if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
|
|
|
+ return EMULATE_DONE;
|
|
|
+ return EMULATE_FAIL;
|
|
|
}
|
|
|
- /*
|
|
|
- * Both basic or both extended?
|
|
|
- */
|
|
|
- if (((e->function ^ function) & 0x80000000) == 0)
|
|
|
- if (!best || e->function > best->function)
|
|
|
- best = e;
|
|
|
}
|
|
|
- if (best) {
|
|
|
- vcpu->regs[VCPU_REGS_RAX] = best->eax;
|
|
|
- vcpu->regs[VCPU_REGS_RBX] = best->ebx;
|
|
|
- vcpu->regs[VCPU_REGS_RCX] = best->ecx;
|
|
|
- vcpu->regs[VCPU_REGS_RDX] = best->edx;
|
|
|
+
|
|
|
+ r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
|
|
|
+
|
|
|
+ if (vcpu->arch.pio.string)
|
|
|
+ return EMULATE_DO_MMIO;
|
|
|
+
|
|
|
+ if ((r || vcpu->mmio_is_write) && run) {
|
|
|
+ run->exit_reason = KVM_EXIT_MMIO;
|
|
|
+ run->mmio.phys_addr = vcpu->mmio_phys_addr;
|
|
|
+ memcpy(run->mmio.data, vcpu->mmio_data, 8);
|
|
|
+ run->mmio.len = vcpu->mmio_size;
|
|
|
+ run->mmio.is_write = vcpu->mmio_is_write;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (r) {
|
|
|
+ if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
|
|
|
+ return EMULATE_DONE;
|
|
|
+ if (!vcpu->mmio_needed) {
|
|
|
+ kvm_report_emulation_failure(vcpu, "mmio");
|
|
|
+ return EMULATE_FAIL;
|
|
|
+ }
|
|
|
+ return EMULATE_DO_MMIO;
|
|
|
}
|
|
|
+
|
|
|
kvm_x86_ops->decache_regs(vcpu);
|
|
|
- kvm_x86_ops->skip_emulated_instruction(vcpu);
|
|
|
+ kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
|
|
|
+
|
|
|
+ if (vcpu->mmio_is_write) {
|
|
|
+ vcpu->mmio_needed = 0;
|
|
|
+ return EMULATE_DO_MMIO;
|
|
|
+ }
|
|
|
+
|
|
|
+ return EMULATE_DONE;
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(emulate_instruction);
|
|
|
+
|
|
|
+static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+
|
|
|
+ for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
|
|
|
+ if (vcpu->arch.pio.guest_pages[i]) {
|
|
|
+ kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
|
|
|
+ vcpu->arch.pio.guest_pages[i] = NULL;
|
|
|
+ }
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
|
|
|
|
|
|
static int pio_copy_data(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- void *p = vcpu->pio_data;
|
|
|
+ void *p = vcpu->arch.pio_data;
|
|
|
void *q;
|
|
|
unsigned bytes;
|
|
|
- int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
|
|
|
+ int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
|
|
|
|
|
|
- q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
|
|
|
+ q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
|
|
|
PAGE_KERNEL);
|
|
|
if (!q) {
|
|
|
free_pio_guest_pages(vcpu);
|
|
|
return -ENOMEM;
|
|
|
}
|
|
|
- q += vcpu->pio.guest_page_offset;
|
|
|
- bytes = vcpu->pio.size * vcpu->pio.cur_count;
|
|
|
- if (vcpu->pio.in)
|
|
|
+ q += vcpu->arch.pio.guest_page_offset;
|
|
|
+ bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
|
|
|
+ if (vcpu->arch.pio.in)
|
|
|
memcpy(q, p, bytes);
|
|
|
else
|
|
|
memcpy(p, q, bytes);
|
|
|
- q -= vcpu->pio.guest_page_offset;
|
|
|
+ q -= vcpu->arch.pio.guest_page_offset;
|
|
|
vunmap(q);
|
|
|
free_pio_guest_pages(vcpu);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static int complete_pio(struct kvm_vcpu *vcpu)
|
|
|
+int complete_pio(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- struct kvm_pio_request *io = &vcpu->pio;
|
|
|
+ struct kvm_pio_request *io = &vcpu->arch.pio;
|
|
|
long delta;
|
|
|
int r;
|
|
|
|
|
@@ -1760,7 +1985,7 @@ static int complete_pio(struct kvm_vcpu *vcpu)
|
|
|
|
|
|
if (!io->string) {
|
|
|
if (io->in)
|
|
|
- memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
|
|
|
+ memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
|
|
|
io->size);
|
|
|
} else {
|
|
|
if (io->in) {
|
|
@@ -1778,15 +2003,15 @@ static int complete_pio(struct kvm_vcpu *vcpu)
|
|
|
* The size of the register should really depend on
|
|
|
* current address size.
|
|
|
*/
|
|
|
- vcpu->regs[VCPU_REGS_RCX] -= delta;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
|
|
|
}
|
|
|
if (io->down)
|
|
|
delta = -delta;
|
|
|
delta *= io->size;
|
|
|
if (io->in)
|
|
|
- vcpu->regs[VCPU_REGS_RDI] += delta;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RDI] += delta;
|
|
|
else
|
|
|
- vcpu->regs[VCPU_REGS_RSI] += delta;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RSI] += delta;
|
|
|
}
|
|
|
|
|
|
kvm_x86_ops->decache_regs(vcpu);
|
|
@@ -1804,13 +2029,13 @@ static void kernel_pio(struct kvm_io_device *pio_dev,
|
|
|
/* TODO: String I/O for in kernel device */
|
|
|
|
|
|
mutex_lock(&vcpu->kvm->lock);
|
|
|
- if (vcpu->pio.in)
|
|
|
- kvm_iodevice_read(pio_dev, vcpu->pio.port,
|
|
|
- vcpu->pio.size,
|
|
|
+ if (vcpu->arch.pio.in)
|
|
|
+ kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
|
|
|
+ vcpu->arch.pio.size,
|
|
|
pd);
|
|
|
else
|
|
|
- kvm_iodevice_write(pio_dev, vcpu->pio.port,
|
|
|
- vcpu->pio.size,
|
|
|
+ kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
|
|
|
+ vcpu->arch.pio.size,
|
|
|
pd);
|
|
|
mutex_unlock(&vcpu->kvm->lock);
|
|
|
}
|
|
@@ -1818,8 +2043,8 @@ static void kernel_pio(struct kvm_io_device *pio_dev,
|
|
|
static void pio_string_write(struct kvm_io_device *pio_dev,
|
|
|
struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- struct kvm_pio_request *io = &vcpu->pio;
|
|
|
- void *pd = vcpu->pio_data;
|
|
|
+ struct kvm_pio_request *io = &vcpu->arch.pio;
|
|
|
+ void *pd = vcpu->arch.pio_data;
|
|
|
int i;
|
|
|
|
|
|
mutex_lock(&vcpu->kvm->lock);
|
|
@@ -1832,32 +2057,38 @@ static void pio_string_write(struct kvm_io_device *pio_dev,
|
|
|
mutex_unlock(&vcpu->kvm->lock);
|
|
|
}
|
|
|
|
|
|
-int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
|
|
|
+static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
|
|
|
+ gpa_t addr)
|
|
|
+{
|
|
|
+ return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
|
|
|
+}
|
|
|
+
|
|
|
+int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
|
|
|
int size, unsigned port)
|
|
|
{
|
|
|
struct kvm_io_device *pio_dev;
|
|
|
|
|
|
vcpu->run->exit_reason = KVM_EXIT_IO;
|
|
|
vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
|
|
|
- vcpu->run->io.size = vcpu->pio.size = size;
|
|
|
+ vcpu->run->io.size = vcpu->arch.pio.size = size;
|
|
|
vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
|
|
|
- vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
|
|
|
- vcpu->run->io.port = vcpu->pio.port = port;
|
|
|
- vcpu->pio.in = in;
|
|
|
- vcpu->pio.string = 0;
|
|
|
- vcpu->pio.down = 0;
|
|
|
- vcpu->pio.guest_page_offset = 0;
|
|
|
- vcpu->pio.rep = 0;
|
|
|
+ vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
|
|
|
+ vcpu->run->io.port = vcpu->arch.pio.port = port;
|
|
|
+ vcpu->arch.pio.in = in;
|
|
|
+ vcpu->arch.pio.string = 0;
|
|
|
+ vcpu->arch.pio.down = 0;
|
|
|
+ vcpu->arch.pio.guest_page_offset = 0;
|
|
|
+ vcpu->arch.pio.rep = 0;
|
|
|
|
|
|
kvm_x86_ops->cache_regs(vcpu);
|
|
|
- memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
|
|
|
+ memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
|
|
|
kvm_x86_ops->decache_regs(vcpu);
|
|
|
|
|
|
kvm_x86_ops->skip_emulated_instruction(vcpu);
|
|
|
|
|
|
pio_dev = vcpu_find_pio_dev(vcpu, port);
|
|
|
if (pio_dev) {
|
|
|
- kernel_pio(pio_dev, vcpu, vcpu->pio_data);
|
|
|
+ kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
|
|
|
complete_pio(vcpu);
|
|
|
return 1;
|
|
|
}
|
|
@@ -1877,15 +2108,15 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
|
|
|
|
|
|
vcpu->run->exit_reason = KVM_EXIT_IO;
|
|
|
vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
|
|
|
- vcpu->run->io.size = vcpu->pio.size = size;
|
|
|
+ vcpu->run->io.size = vcpu->arch.pio.size = size;
|
|
|
vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
|
|
|
- vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
|
|
|
- vcpu->run->io.port = vcpu->pio.port = port;
|
|
|
- vcpu->pio.in = in;
|
|
|
- vcpu->pio.string = 1;
|
|
|
- vcpu->pio.down = down;
|
|
|
- vcpu->pio.guest_page_offset = offset_in_page(address);
|
|
|
- vcpu->pio.rep = rep;
|
|
|
+ vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
|
|
|
+ vcpu->run->io.port = vcpu->arch.pio.port = port;
|
|
|
+ vcpu->arch.pio.in = in;
|
|
|
+ vcpu->arch.pio.string = 1;
|
|
|
+ vcpu->arch.pio.down = down;
|
|
|
+ vcpu->arch.pio.guest_page_offset = offset_in_page(address);
|
|
|
+ vcpu->arch.pio.rep = rep;
|
|
|
|
|
|
if (!count) {
|
|
|
kvm_x86_ops->skip_emulated_instruction(vcpu);
|
|
@@ -1911,37 +2142,35 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
|
|
|
* String I/O in reverse. Yuck. Kill the guest, fix later.
|
|
|
*/
|
|
|
pr_unimpl(vcpu, "guest string pio down\n");
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
return 1;
|
|
|
}
|
|
|
vcpu->run->io.count = now;
|
|
|
- vcpu->pio.cur_count = now;
|
|
|
+ vcpu->arch.pio.cur_count = now;
|
|
|
|
|
|
- if (vcpu->pio.cur_count == vcpu->pio.count)
|
|
|
+ if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
|
|
|
kvm_x86_ops->skip_emulated_instruction(vcpu);
|
|
|
|
|
|
for (i = 0; i < nr_pages; ++i) {
|
|
|
- mutex_lock(&vcpu->kvm->lock);
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
page = gva_to_page(vcpu, address + i * PAGE_SIZE);
|
|
|
- if (page)
|
|
|
- get_page(page);
|
|
|
- vcpu->pio.guest_pages[i] = page;
|
|
|
- mutex_unlock(&vcpu->kvm->lock);
|
|
|
+ vcpu->arch.pio.guest_pages[i] = page;
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
if (!page) {
|
|
|
- inject_gp(vcpu);
|
|
|
+ kvm_inject_gp(vcpu, 0);
|
|
|
free_pio_guest_pages(vcpu);
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pio_dev = vcpu_find_pio_dev(vcpu, port);
|
|
|
- if (!vcpu->pio.in) {
|
|
|
+ if (!vcpu->arch.pio.in) {
|
|
|
/* string PIO write */
|
|
|
ret = pio_copy_data(vcpu);
|
|
|
if (ret >= 0 && pio_dev) {
|
|
|
pio_string_write(pio_dev, vcpu);
|
|
|
complete_pio(vcpu);
|
|
|
- if (vcpu->pio.count == 0)
|
|
|
+ if (vcpu->arch.pio.count == 0)
|
|
|
ret = 1;
|
|
|
}
|
|
|
} else if (pio_dev)
|
|
@@ -1953,124 +2182,437 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
|
|
|
|
|
|
-/*
|
|
|
- * Check if userspace requested an interrupt window, and that the
|
|
|
- * interrupt window is open.
|
|
|
- *
|
|
|
- * No need to exit to userspace if we already have an interrupt queued.
|
|
|
- */
|
|
|
-static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_run *kvm_run)
|
|
|
-{
|
|
|
- return (!vcpu->irq_summary &&
|
|
|
- kvm_run->request_interrupt_window &&
|
|
|
- vcpu->interrupt_window_open &&
|
|
|
- (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
|
|
|
-}
|
|
|
-
|
|
|
-static void post_kvm_run_save(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_run *kvm_run)
|
|
|
-{
|
|
|
- kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
|
|
|
- kvm_run->cr8 = get_cr8(vcpu);
|
|
|
- kvm_run->apic_base = kvm_get_apic_base(vcpu);
|
|
|
- if (irqchip_in_kernel(vcpu->kvm))
|
|
|
- kvm_run->ready_for_interrupt_injection = 1;
|
|
|
- else
|
|
|
- kvm_run->ready_for_interrupt_injection =
|
|
|
- (vcpu->interrupt_window_open &&
|
|
|
- vcpu->irq_summary == 0);
|
|
|
-}
|
|
|
-
|
|
|
-static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
|
|
|
+int kvm_arch_init(void *opaque)
|
|
|
{
|
|
|
int r;
|
|
|
+ struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
|
|
|
|
|
|
- if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
|
|
|
- printk("vcpu %d received sipi with vector # %x\n",
|
|
|
- vcpu->vcpu_id, vcpu->sipi_vector);
|
|
|
- kvm_lapic_reset(vcpu);
|
|
|
- kvm_x86_ops->vcpu_reset(vcpu);
|
|
|
- vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
|
|
|
+ if (kvm_x86_ops) {
|
|
|
+ printk(KERN_ERR "kvm: already loaded the other module\n");
|
|
|
+ r = -EEXIST;
|
|
|
+ goto out;
|
|
|
}
|
|
|
|
|
|
-preempted:
|
|
|
- if (vcpu->guest_debug.enabled)
|
|
|
- kvm_x86_ops->guest_debug_pre(vcpu);
|
|
|
+ if (!ops->cpu_has_kvm_support()) {
|
|
|
+ printk(KERN_ERR "kvm: no hardware support\n");
|
|
|
+ r = -EOPNOTSUPP;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ if (ops->disabled_by_bios()) {
|
|
|
+ printk(KERN_ERR "kvm: disabled by bios\n");
|
|
|
+ r = -EOPNOTSUPP;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
|
|
|
-again:
|
|
|
- r = kvm_mmu_reload(vcpu);
|
|
|
- if (unlikely(r))
|
|
|
+ r = kvm_mmu_module_init();
|
|
|
+ if (r)
|
|
|
goto out;
|
|
|
|
|
|
- preempt_disable();
|
|
|
+ kvm_init_msr_list();
|
|
|
|
|
|
- kvm_x86_ops->prepare_guest_switch(vcpu);
|
|
|
- kvm_load_guest_fpu(vcpu);
|
|
|
+ kvm_x86_ops = ops;
|
|
|
+ kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
|
|
|
+ return 0;
|
|
|
|
|
|
- local_irq_disable();
|
|
|
+out:
|
|
|
+ return r;
|
|
|
+}
|
|
|
|
|
|
- if (signal_pending(current)) {
|
|
|
- local_irq_enable();
|
|
|
- preempt_enable();
|
|
|
- r = -EINTR;
|
|
|
- kvm_run->exit_reason = KVM_EXIT_INTR;
|
|
|
- ++vcpu->stat.signal_exits;
|
|
|
- goto out;
|
|
|
+void kvm_arch_exit(void)
|
|
|
+{
|
|
|
+ kvm_x86_ops = NULL;
|
|
|
+ kvm_mmu_module_exit();
|
|
|
+}
|
|
|
+
|
|
|
+int kvm_emulate_halt(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ ++vcpu->stat.halt_exits;
|
|
|
+ if (irqchip_in_kernel(vcpu->kvm)) {
|
|
|
+ vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
|
|
|
+ kvm_vcpu_block(vcpu);
|
|
|
+ if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
|
|
|
+ return -EINTR;
|
|
|
+ return 1;
|
|
|
+ } else {
|
|
|
+ vcpu->run->exit_reason = KVM_EXIT_HLT;
|
|
|
+ return 0;
|
|
|
}
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_emulate_halt);
|
|
|
|
|
|
- if (irqchip_in_kernel(vcpu->kvm))
|
|
|
- kvm_x86_ops->inject_pending_irq(vcpu);
|
|
|
- else if (!vcpu->mmio_read_completed)
|
|
|
- kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
|
|
|
+int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ unsigned long nr, a0, a1, a2, a3, ret;
|
|
|
|
|
|
- vcpu->guest_mode = 1;
|
|
|
- kvm_guest_enter();
|
|
|
+ kvm_x86_ops->cache_regs(vcpu);
|
|
|
|
|
|
- if (vcpu->requests)
|
|
|
- if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
|
|
|
- kvm_x86_ops->tlb_flush(vcpu);
|
|
|
+ nr = vcpu->arch.regs[VCPU_REGS_RAX];
|
|
|
+ a0 = vcpu->arch.regs[VCPU_REGS_RBX];
|
|
|
+ a1 = vcpu->arch.regs[VCPU_REGS_RCX];
|
|
|
+ a2 = vcpu->arch.regs[VCPU_REGS_RDX];
|
|
|
+ a3 = vcpu->arch.regs[VCPU_REGS_RSI];
|
|
|
|
|
|
- kvm_x86_ops->run(vcpu, kvm_run);
|
|
|
+ if (!is_long_mode(vcpu)) {
|
|
|
+ nr &= 0xFFFFFFFF;
|
|
|
+ a0 &= 0xFFFFFFFF;
|
|
|
+ a1 &= 0xFFFFFFFF;
|
|
|
+ a2 &= 0xFFFFFFFF;
|
|
|
+ a3 &= 0xFFFFFFFF;
|
|
|
+ }
|
|
|
|
|
|
- vcpu->guest_mode = 0;
|
|
|
- local_irq_enable();
|
|
|
+ switch (nr) {
|
|
|
+ case KVM_HC_VAPIC_POLL_IRQ:
|
|
|
+ ret = 0;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ ret = -KVM_ENOSYS;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RAX] = ret;
|
|
|
+ kvm_x86_ops->decache_regs(vcpu);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
|
|
|
+
|
|
|
+int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ char instruction[3];
|
|
|
+ int ret = 0;
|
|
|
|
|
|
- ++vcpu->stat.exits;
|
|
|
|
|
|
/*
|
|
|
- * We must have an instruction between local_irq_enable() and
|
|
|
- * kvm_guest_exit(), so the timer interrupt isn't delayed by
|
|
|
- * the interrupt shadow. The stat.exits increment will do nicely.
|
|
|
- * But we need to prevent reordering, hence this barrier():
|
|
|
+ * Blow out the MMU to ensure that no other VCPU has an active mapping
|
|
|
+ * to ensure that the updated hypercall appears atomically across all
|
|
|
+ * VCPUs.
|
|
|
*/
|
|
|
- barrier();
|
|
|
+ kvm_mmu_zap_all(vcpu->kvm);
|
|
|
|
|
|
- kvm_guest_exit();
|
|
|
+ kvm_x86_ops->cache_regs(vcpu);
|
|
|
+ kvm_x86_ops->patch_hypercall(vcpu, instruction);
|
|
|
+ if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
|
|
|
+ != X86EMUL_CONTINUE)
|
|
|
+ ret = -EFAULT;
|
|
|
|
|
|
- preempt_enable();
|
|
|
+ return ret;
|
|
|
+}
|
|
|
|
|
|
- /*
|
|
|
- * Profile KVM exit RIPs:
|
|
|
- */
|
|
|
- if (unlikely(prof_on == KVM_PROFILING)) {
|
|
|
- kvm_x86_ops->cache_regs(vcpu);
|
|
|
- profile_hit(KVM_PROFILING, (void *)vcpu->rip);
|
|
|
- }
|
|
|
+static u64 mk_cr_64(u64 curr_cr, u32 new_val)
|
|
|
+{
|
|
|
+ return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
|
|
|
+}
|
|
|
|
|
|
- r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
|
|
|
+void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
|
|
|
+{
|
|
|
+ struct descriptor_table dt = { limit, base };
|
|
|
|
|
|
- if (r > 0) {
|
|
|
- if (dm_request_for_irq_injection(vcpu, kvm_run)) {
|
|
|
- r = -EINTR;
|
|
|
- kvm_run->exit_reason = KVM_EXIT_INTR;
|
|
|
- ++vcpu->stat.request_irq_exits;
|
|
|
- goto out;
|
|
|
- }
|
|
|
- if (!need_resched()) {
|
|
|
- ++vcpu->stat.light_exits;
|
|
|
- goto again;
|
|
|
+ kvm_x86_ops->set_gdt(vcpu, &dt);
|
|
|
+}
|
|
|
+
|
|
|
+void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
|
|
|
+{
|
|
|
+ struct descriptor_table dt = { limit, base };
|
|
|
+
|
|
|
+ kvm_x86_ops->set_idt(vcpu, &dt);
|
|
|
+}
|
|
|
+
|
|
|
+void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
|
|
|
+ unsigned long *rflags)
|
|
|
+{
|
|
|
+ lmsw(vcpu, msw);
|
|
|
+ *rflags = kvm_x86_ops->get_rflags(vcpu);
|
|
|
+}
|
|
|
+
|
|
|
+unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
|
|
|
+{
|
|
|
+ kvm_x86_ops->decache_cr4_guest_bits(vcpu);
|
|
|
+ switch (cr) {
|
|
|
+ case 0:
|
|
|
+ return vcpu->arch.cr0;
|
|
|
+ case 2:
|
|
|
+ return vcpu->arch.cr2;
|
|
|
+ case 3:
|
|
|
+ return vcpu->arch.cr3;
|
|
|
+ case 4:
|
|
|
+ return vcpu->arch.cr4;
|
|
|
+ case 8:
|
|
|
+ return get_cr8(vcpu);
|
|
|
+ default:
|
|
|
+ vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
|
|
|
+ unsigned long *rflags)
|
|
|
+{
|
|
|
+ switch (cr) {
|
|
|
+ case 0:
|
|
|
+ set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
|
|
|
+ *rflags = kvm_x86_ops->get_rflags(vcpu);
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ vcpu->arch.cr2 = val;
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ set_cr3(vcpu, val);
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
|
|
|
+ break;
|
|
|
+ case 8:
|
|
|
+ set_cr8(vcpu, val & 0xfUL);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
|
|
|
+{
|
|
|
+ struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
|
|
|
+ int j, nent = vcpu->arch.cpuid_nent;
|
|
|
+
|
|
|
+ e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
|
|
|
+ /* when no next entry is found, the current entry[i] is reselected */
|
|
|
+ for (j = i + 1; j == i; j = (j + 1) % nent) {
|
|
|
+ struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
|
|
|
+ if (ej->function == e->function) {
|
|
|
+ ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
|
|
|
+ return j;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0; /* silence gcc, even though control never reaches here */
|
|
|
+}
|
|
|
+
|
|
|
+/* find an entry with matching function, matching index (if needed), and that
|
|
|
+ * should be read next (if it's stateful) */
|
|
|
+static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
|
|
|
+ u32 function, u32 index)
|
|
|
+{
|
|
|
+ if (e->function != function)
|
|
|
+ return 0;
|
|
|
+ if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
|
|
|
+ return 0;
|
|
|
+ if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
|
|
|
+ !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
|
|
|
+ return 0;
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ u32 function, index;
|
|
|
+ struct kvm_cpuid_entry2 *e, *best;
|
|
|
+
|
|
|
+ kvm_x86_ops->cache_regs(vcpu);
|
|
|
+ function = vcpu->arch.regs[VCPU_REGS_RAX];
|
|
|
+ index = vcpu->arch.regs[VCPU_REGS_RCX];
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RAX] = 0;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RBX] = 0;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RCX] = 0;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RDX] = 0;
|
|
|
+ best = NULL;
|
|
|
+ for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
|
|
|
+ e = &vcpu->arch.cpuid_entries[i];
|
|
|
+ if (is_matching_cpuid_entry(e, function, index)) {
|
|
|
+ if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
|
|
|
+ move_to_next_stateful_cpuid_entry(vcpu, i);
|
|
|
+ best = e;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ * Both basic or both extended?
|
|
|
+ */
|
|
|
+ if (((e->function ^ function) & 0x80000000) == 0)
|
|
|
+ if (!best || e->function > best->function)
|
|
|
+ best = e;
|
|
|
+ }
|
|
|
+ if (best) {
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
|
|
|
+ }
|
|
|
+ kvm_x86_ops->decache_regs(vcpu);
|
|
|
+ kvm_x86_ops->skip_emulated_instruction(vcpu);
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
|
|
|
+
|
|
|
+/*
|
|
|
+ * Check if userspace requested an interrupt window, and that the
|
|
|
+ * interrupt window is open.
|
|
|
+ *
|
|
|
+ * No need to exit to userspace if we already have an interrupt queued.
|
|
|
+ */
|
|
|
+static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_run *kvm_run)
|
|
|
+{
|
|
|
+ return (!vcpu->arch.irq_summary &&
|
|
|
+ kvm_run->request_interrupt_window &&
|
|
|
+ vcpu->arch.interrupt_window_open &&
|
|
|
+ (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
|
|
|
+}
|
|
|
+
|
|
|
+static void post_kvm_run_save(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_run *kvm_run)
|
|
|
+{
|
|
|
+ kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
|
|
|
+ kvm_run->cr8 = get_cr8(vcpu);
|
|
|
+ kvm_run->apic_base = kvm_get_apic_base(vcpu);
|
|
|
+ if (irqchip_in_kernel(vcpu->kvm))
|
|
|
+ kvm_run->ready_for_interrupt_injection = 1;
|
|
|
+ else
|
|
|
+ kvm_run->ready_for_interrupt_injection =
|
|
|
+ (vcpu->arch.interrupt_window_open &&
|
|
|
+ vcpu->arch.irq_summary == 0);
|
|
|
+}
|
|
|
+
|
|
|
+static void vapic_enter(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ struct kvm_lapic *apic = vcpu->arch.apic;
|
|
|
+ struct page *page;
|
|
|
+
|
|
|
+ if (!apic || !apic->vapic_addr)
|
|
|
+ return;
|
|
|
+
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
|
|
|
+ vcpu->arch.apic->vapic_page = page;
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
+}
|
|
|
+
|
|
|
+static void vapic_exit(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ struct kvm_lapic *apic = vcpu->arch.apic;
|
|
|
+
|
|
|
+ if (!apic || !apic->vapic_addr)
|
|
|
+ return;
|
|
|
+
|
|
|
+ kvm_release_page_dirty(apic->vapic_page);
|
|
|
+ mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
|
|
|
+}
|
|
|
+
|
|
|
+static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+
|
|
|
+ if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
|
|
|
+ pr_debug("vcpu %d received sipi with vector # %x\n",
|
|
|
+ vcpu->vcpu_id, vcpu->arch.sipi_vector);
|
|
|
+ kvm_lapic_reset(vcpu);
|
|
|
+ r = kvm_x86_ops->vcpu_reset(vcpu);
|
|
|
+ if (r)
|
|
|
+ return r;
|
|
|
+ vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
|
|
|
+ }
|
|
|
+
|
|
|
+ vapic_enter(vcpu);
|
|
|
+
|
|
|
+preempted:
|
|
|
+ if (vcpu->guest_debug.enabled)
|
|
|
+ kvm_x86_ops->guest_debug_pre(vcpu);
|
|
|
+
|
|
|
+again:
|
|
|
+ r = kvm_mmu_reload(vcpu);
|
|
|
+ if (unlikely(r))
|
|
|
+ goto out;
|
|
|
+
|
|
|
+ if (vcpu->requests) {
|
|
|
+ if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
|
|
|
+ __kvm_migrate_apic_timer(vcpu);
|
|
|
+ if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
|
|
|
+ &vcpu->requests)) {
|
|
|
+ kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
|
|
|
+ r = 0;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ kvm_inject_pending_timer_irqs(vcpu);
|
|
|
+
|
|
|
+ preempt_disable();
|
|
|
+
|
|
|
+ kvm_x86_ops->prepare_guest_switch(vcpu);
|
|
|
+ kvm_load_guest_fpu(vcpu);
|
|
|
+
|
|
|
+ local_irq_disable();
|
|
|
+
|
|
|
+ if (need_resched()) {
|
|
|
+ local_irq_enable();
|
|
|
+ preempt_enable();
|
|
|
+ r = 1;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (signal_pending(current)) {
|
|
|
+ local_irq_enable();
|
|
|
+ preempt_enable();
|
|
|
+ r = -EINTR;
|
|
|
+ kvm_run->exit_reason = KVM_EXIT_INTR;
|
|
|
+ ++vcpu->stat.signal_exits;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (vcpu->arch.exception.pending)
|
|
|
+ __queue_exception(vcpu);
|
|
|
+ else if (irqchip_in_kernel(vcpu->kvm))
|
|
|
+ kvm_x86_ops->inject_pending_irq(vcpu);
|
|
|
+ else
|
|
|
+ kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
|
|
|
+
|
|
|
+ kvm_lapic_sync_to_vapic(vcpu);
|
|
|
+
|
|
|
+ vcpu->guest_mode = 1;
|
|
|
+ kvm_guest_enter();
|
|
|
+
|
|
|
+ if (vcpu->requests)
|
|
|
+ if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
|
|
|
+ kvm_x86_ops->tlb_flush(vcpu);
|
|
|
+
|
|
|
+ kvm_x86_ops->run(vcpu, kvm_run);
|
|
|
+
|
|
|
+ vcpu->guest_mode = 0;
|
|
|
+ local_irq_enable();
|
|
|
+
|
|
|
+ ++vcpu->stat.exits;
|
|
|
+
|
|
|
+ /*
|
|
|
+ * We must have an instruction between local_irq_enable() and
|
|
|
+ * kvm_guest_exit(), so the timer interrupt isn't delayed by
|
|
|
+ * the interrupt shadow. The stat.exits increment will do nicely.
|
|
|
+ * But we need to prevent reordering, hence this barrier():
|
|
|
+ */
|
|
|
+ barrier();
|
|
|
+
|
|
|
+ kvm_guest_exit();
|
|
|
+
|
|
|
+ preempt_enable();
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Profile KVM exit RIPs:
|
|
|
+ */
|
|
|
+ if (unlikely(prof_on == KVM_PROFILING)) {
|
|
|
+ kvm_x86_ops->cache_regs(vcpu);
|
|
|
+ profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
|
|
|
+ vcpu->arch.exception.pending = false;
|
|
|
+
|
|
|
+ kvm_lapic_sync_from_vapic(vcpu);
|
|
|
+
|
|
|
+ r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
|
|
|
+
|
|
|
+ if (r > 0) {
|
|
|
+ if (dm_request_for_irq_injection(vcpu, kvm_run)) {
|
|
|
+ r = -EINTR;
|
|
|
+ kvm_run->exit_reason = KVM_EXIT_INTR;
|
|
|
+ ++vcpu->stat.request_irq_exits;
|
|
|
+ goto out;
|
|
|
}
|
|
|
+ if (!need_resched())
|
|
|
+ goto again;
|
|
|
}
|
|
|
|
|
|
out:
|
|
@@ -2081,18 +2623,19 @@ out:
|
|
|
|
|
|
post_kvm_run_save(vcpu, kvm_run);
|
|
|
|
|
|
+ vapic_exit(vcpu);
|
|
|
+
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
|
|
|
+int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
|
|
|
{
|
|
|
int r;
|
|
|
sigset_t sigsaved;
|
|
|
|
|
|
vcpu_load(vcpu);
|
|
|
|
|
|
- if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
|
|
|
+ if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
|
|
|
kvm_vcpu_block(vcpu);
|
|
|
vcpu_put(vcpu);
|
|
|
return -EAGAIN;
|
|
@@ -2105,18 +2648,19 @@ static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
|
|
|
if (!irqchip_in_kernel(vcpu->kvm))
|
|
|
set_cr8(vcpu, kvm_run->cr8);
|
|
|
|
|
|
- if (vcpu->pio.cur_count) {
|
|
|
+ if (vcpu->arch.pio.cur_count) {
|
|
|
r = complete_pio(vcpu);
|
|
|
if (r)
|
|
|
goto out;
|
|
|
}
|
|
|
-
|
|
|
+#if CONFIG_HAS_IOMEM
|
|
|
if (vcpu->mmio_needed) {
|
|
|
memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
|
|
|
vcpu->mmio_read_completed = 1;
|
|
|
vcpu->mmio_needed = 0;
|
|
|
r = emulate_instruction(vcpu, kvm_run,
|
|
|
- vcpu->mmio_fault_cr2, 0);
|
|
|
+ vcpu->arch.mmio_fault_cr2, 0,
|
|
|
+ EMULTYPE_NO_DECODE);
|
|
|
if (r == EMULATE_DO_MMIO) {
|
|
|
/*
|
|
|
* Read-modify-write. Back to userspace.
|
|
@@ -2125,10 +2669,10 @@ static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
|
|
|
goto out;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+#endif
|
|
|
if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
|
|
|
kvm_x86_ops->cache_regs(vcpu);
|
|
|
- vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
|
|
|
kvm_x86_ops->decache_regs(vcpu);
|
|
|
}
|
|
|
|
|
@@ -2142,33 +2686,32 @@ out:
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
-static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_regs *regs)
|
|
|
+int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
|
|
|
{
|
|
|
vcpu_load(vcpu);
|
|
|
|
|
|
kvm_x86_ops->cache_regs(vcpu);
|
|
|
|
|
|
- regs->rax = vcpu->regs[VCPU_REGS_RAX];
|
|
|
- regs->rbx = vcpu->regs[VCPU_REGS_RBX];
|
|
|
- regs->rcx = vcpu->regs[VCPU_REGS_RCX];
|
|
|
- regs->rdx = vcpu->regs[VCPU_REGS_RDX];
|
|
|
- regs->rsi = vcpu->regs[VCPU_REGS_RSI];
|
|
|
- regs->rdi = vcpu->regs[VCPU_REGS_RDI];
|
|
|
- regs->rsp = vcpu->regs[VCPU_REGS_RSP];
|
|
|
- regs->rbp = vcpu->regs[VCPU_REGS_RBP];
|
|
|
+ regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
|
|
|
+ regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
|
|
|
+ regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
|
|
|
+ regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
|
|
|
+ regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
|
|
|
+ regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
|
|
|
+ regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
|
|
|
+ regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
|
|
|
#ifdef CONFIG_X86_64
|
|
|
- regs->r8 = vcpu->regs[VCPU_REGS_R8];
|
|
|
- regs->r9 = vcpu->regs[VCPU_REGS_R9];
|
|
|
- regs->r10 = vcpu->regs[VCPU_REGS_R10];
|
|
|
- regs->r11 = vcpu->regs[VCPU_REGS_R11];
|
|
|
- regs->r12 = vcpu->regs[VCPU_REGS_R12];
|
|
|
- regs->r13 = vcpu->regs[VCPU_REGS_R13];
|
|
|
- regs->r14 = vcpu->regs[VCPU_REGS_R14];
|
|
|
- regs->r15 = vcpu->regs[VCPU_REGS_R15];
|
|
|
+ regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
|
|
|
+ regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
|
|
|
+ regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
|
|
|
+ regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
|
|
|
+ regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
|
|
|
+ regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
|
|
|
+ regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
|
|
|
+ regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
|
|
|
#endif
|
|
|
|
|
|
- regs->rip = vcpu->rip;
|
|
|
+ regs->rip = vcpu->arch.rip;
|
|
|
regs->rflags = kvm_x86_ops->get_rflags(vcpu);
|
|
|
|
|
|
/*
|
|
@@ -2182,31 +2725,30 @@ static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_regs *regs)
|
|
|
+int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
|
|
|
{
|
|
|
vcpu_load(vcpu);
|
|
|
|
|
|
- vcpu->regs[VCPU_REGS_RAX] = regs->rax;
|
|
|
- vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
|
|
|
- vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
|
|
|
- vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
|
|
|
- vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
|
|
|
- vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
|
|
|
- vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
|
|
|
- vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
|
|
|
#ifdef CONFIG_X86_64
|
|
|
- vcpu->regs[VCPU_REGS_R8] = regs->r8;
|
|
|
- vcpu->regs[VCPU_REGS_R9] = regs->r9;
|
|
|
- vcpu->regs[VCPU_REGS_R10] = regs->r10;
|
|
|
- vcpu->regs[VCPU_REGS_R11] = regs->r11;
|
|
|
- vcpu->regs[VCPU_REGS_R12] = regs->r12;
|
|
|
- vcpu->regs[VCPU_REGS_R13] = regs->r13;
|
|
|
- vcpu->regs[VCPU_REGS_R14] = regs->r14;
|
|
|
- vcpu->regs[VCPU_REGS_R15] = regs->r15;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
|
|
|
+ vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
|
|
|
#endif
|
|
|
|
|
|
- vcpu->rip = regs->rip;
|
|
|
+ vcpu->arch.rip = regs->rip;
|
|
|
kvm_x86_ops->set_rflags(vcpu, regs->rflags);
|
|
|
|
|
|
kvm_x86_ops->decache_regs(vcpu);
|
|
@@ -2222,8 +2764,18 @@ static void get_segment(struct kvm_vcpu *vcpu,
|
|
|
return kvm_x86_ops->get_segment(vcpu, var, seg);
|
|
|
}
|
|
|
|
|
|
-static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_sregs *sregs)
|
|
|
+void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
|
|
|
+{
|
|
|
+ struct kvm_segment cs;
|
|
|
+
|
|
|
+ get_segment(vcpu, &cs, VCPU_SREG_CS);
|
|
|
+ *db = cs.db;
|
|
|
+ *l = cs.l;
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
|
|
|
+
|
|
|
+int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_sregs *sregs)
|
|
|
{
|
|
|
struct descriptor_table dt;
|
|
|
int pending_vec;
|
|
@@ -2248,12 +2800,12 @@ static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
|
|
|
sregs->gdt.base = dt.base;
|
|
|
|
|
|
kvm_x86_ops->decache_cr4_guest_bits(vcpu);
|
|
|
- sregs->cr0 = vcpu->cr0;
|
|
|
- sregs->cr2 = vcpu->cr2;
|
|
|
- sregs->cr3 = vcpu->cr3;
|
|
|
- sregs->cr4 = vcpu->cr4;
|
|
|
+ sregs->cr0 = vcpu->arch.cr0;
|
|
|
+ sregs->cr2 = vcpu->arch.cr2;
|
|
|
+ sregs->cr3 = vcpu->arch.cr3;
|
|
|
+ sregs->cr4 = vcpu->arch.cr4;
|
|
|
sregs->cr8 = get_cr8(vcpu);
|
|
|
- sregs->efer = vcpu->shadow_efer;
|
|
|
+ sregs->efer = vcpu->arch.shadow_efer;
|
|
|
sregs->apic_base = kvm_get_apic_base(vcpu);
|
|
|
|
|
|
if (irqchip_in_kernel(vcpu->kvm)) {
|
|
@@ -2261,9 +2813,10 @@ static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
|
|
|
sizeof sregs->interrupt_bitmap);
|
|
|
pending_vec = kvm_x86_ops->get_irq(vcpu);
|
|
|
if (pending_vec >= 0)
|
|
|
- set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
|
|
|
+ set_bit(pending_vec,
|
|
|
+ (unsigned long *)sregs->interrupt_bitmap);
|
|
|
} else
|
|
|
- memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
|
|
|
+ memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
|
|
|
sizeof sregs->interrupt_bitmap);
|
|
|
|
|
|
vcpu_put(vcpu);
|
|
@@ -2277,8 +2830,8 @@ static void set_segment(struct kvm_vcpu *vcpu,
|
|
|
return kvm_x86_ops->set_segment(vcpu, var, seg);
|
|
|
}
|
|
|
|
|
|
-static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_sregs *sregs)
|
|
|
+int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_sregs *sregs)
|
|
|
{
|
|
|
int mmu_reset_needed = 0;
|
|
|
int i, pending_vec, max_bits;
|
|
@@ -2293,13 +2846,13 @@ static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
|
|
|
dt.base = sregs->gdt.base;
|
|
|
kvm_x86_ops->set_gdt(vcpu, &dt);
|
|
|
|
|
|
- vcpu->cr2 = sregs->cr2;
|
|
|
- mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
|
|
|
- vcpu->cr3 = sregs->cr3;
|
|
|
+ vcpu->arch.cr2 = sregs->cr2;
|
|
|
+ mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
|
|
|
+ vcpu->arch.cr3 = sregs->cr3;
|
|
|
|
|
|
set_cr8(vcpu, sregs->cr8);
|
|
|
|
|
|
- mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
|
|
|
+ mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
|
|
|
#ifdef CONFIG_X86_64
|
|
|
kvm_x86_ops->set_efer(vcpu, sregs->efer);
|
|
|
#endif
|
|
@@ -2307,25 +2860,25 @@ static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
|
|
|
|
|
|
kvm_x86_ops->decache_cr4_guest_bits(vcpu);
|
|
|
|
|
|
- mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
|
|
|
- vcpu->cr0 = sregs->cr0;
|
|
|
+ mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
|
|
|
+ vcpu->arch.cr0 = sregs->cr0;
|
|
|
kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
|
|
|
|
|
|
- mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
|
|
|
+ mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
|
|
|
kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
|
|
|
if (!is_long_mode(vcpu) && is_pae(vcpu))
|
|
|
- load_pdptrs(vcpu, vcpu->cr3);
|
|
|
+ load_pdptrs(vcpu, vcpu->arch.cr3);
|
|
|
|
|
|
if (mmu_reset_needed)
|
|
|
kvm_mmu_reset_context(vcpu);
|
|
|
|
|
|
if (!irqchip_in_kernel(vcpu->kvm)) {
|
|
|
- memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
|
|
|
- sizeof vcpu->irq_pending);
|
|
|
- vcpu->irq_summary = 0;
|
|
|
- for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
|
|
|
- if (vcpu->irq_pending[i])
|
|
|
- __set_bit(i, &vcpu->irq_summary);
|
|
|
+ memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
|
|
|
+ sizeof vcpu->arch.irq_pending);
|
|
|
+ vcpu->arch.irq_summary = 0;
|
|
|
+ for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
|
|
|
+ if (vcpu->arch.irq_pending[i])
|
|
|
+ __set_bit(i, &vcpu->arch.irq_summary);
|
|
|
} else {
|
|
|
max_bits = (sizeof sregs->interrupt_bitmap) << 3;
|
|
|
pending_vec = find_first_bit(
|
|
@@ -2334,1295 +2887,401 @@ static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
|
|
|
/* Only pending external irq is handled here */
|
|
|
if (pending_vec < max_bits) {
|
|
|
kvm_x86_ops->set_irq(vcpu, pending_vec);
|
|
|
- printk("Set back pending irq %d\n", pending_vec);
|
|
|
+ pr_debug("Set back pending irq %d\n",
|
|
|
+ pending_vec);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
|
|
|
set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
|
|
|
- set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
|
|
|
- set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
|
|
|
- set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
|
|
|
- set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
|
|
|
-
|
|
|
- set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
|
|
|
- set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
|
|
|
-
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
|
|
|
-{
|
|
|
- struct kvm_segment cs;
|
|
|
-
|
|
|
- get_segment(vcpu, &cs, VCPU_SREG_CS);
|
|
|
- *db = cs.db;
|
|
|
- *l = cs.l;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
|
|
|
-
|
|
|
-/*
|
|
|
- * List of msr numbers which we expose to userspace through KVM_GET_MSRS
|
|
|
- * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
|
|
|
- *
|
|
|
- * This list is modified at module load time to reflect the
|
|
|
- * capabilities of the host cpu.
|
|
|
- */
|
|
|
-static u32 msrs_to_save[] = {
|
|
|
- MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
|
|
|
- MSR_K6_STAR,
|
|
|
-#ifdef CONFIG_X86_64
|
|
|
- MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
|
|
|
-#endif
|
|
|
- MSR_IA32_TIME_STAMP_COUNTER,
|
|
|
-};
|
|
|
-
|
|
|
-static unsigned num_msrs_to_save;
|
|
|
-
|
|
|
-static u32 emulated_msrs[] = {
|
|
|
- MSR_IA32_MISC_ENABLE,
|
|
|
-};
|
|
|
-
|
|
|
-static __init void kvm_init_msr_list(void)
|
|
|
-{
|
|
|
- u32 dummy[2];
|
|
|
- unsigned i, j;
|
|
|
-
|
|
|
- for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
|
|
|
- if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
|
|
|
- continue;
|
|
|
- if (j < i)
|
|
|
- msrs_to_save[j] = msrs_to_save[i];
|
|
|
- j++;
|
|
|
- }
|
|
|
- num_msrs_to_save = j;
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * Adapt set_msr() to msr_io()'s calling convention
|
|
|
- */
|
|
|
-static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
|
|
|
-{
|
|
|
- return kvm_set_msr(vcpu, index, *data);
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * Read or write a bunch of msrs. All parameters are kernel addresses.
|
|
|
- *
|
|
|
- * @return number of msrs set successfully.
|
|
|
- */
|
|
|
-static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
|
|
|
- struct kvm_msr_entry *entries,
|
|
|
- int (*do_msr)(struct kvm_vcpu *vcpu,
|
|
|
- unsigned index, u64 *data))
|
|
|
-{
|
|
|
- int i;
|
|
|
-
|
|
|
- vcpu_load(vcpu);
|
|
|
-
|
|
|
- for (i = 0; i < msrs->nmsrs; ++i)
|
|
|
- if (do_msr(vcpu, entries[i].index, &entries[i].data))
|
|
|
- break;
|
|
|
-
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return i;
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * Read or write a bunch of msrs. Parameters are user addresses.
|
|
|
- *
|
|
|
- * @return number of msrs set successfully.
|
|
|
- */
|
|
|
-static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
|
|
|
- int (*do_msr)(struct kvm_vcpu *vcpu,
|
|
|
- unsigned index, u64 *data),
|
|
|
- int writeback)
|
|
|
-{
|
|
|
- struct kvm_msrs msrs;
|
|
|
- struct kvm_msr_entry *entries;
|
|
|
- int r, n;
|
|
|
- unsigned size;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&msrs, user_msrs, sizeof msrs))
|
|
|
- goto out;
|
|
|
-
|
|
|
- r = -E2BIG;
|
|
|
- if (msrs.nmsrs >= MAX_IO_MSRS)
|
|
|
- goto out;
|
|
|
-
|
|
|
- r = -ENOMEM;
|
|
|
- size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
|
|
|
- entries = vmalloc(size);
|
|
|
- if (!entries)
|
|
|
- goto out;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(entries, user_msrs->entries, size))
|
|
|
- goto out_free;
|
|
|
-
|
|
|
- r = n = __msr_io(vcpu, &msrs, entries, do_msr);
|
|
|
- if (r < 0)
|
|
|
- goto out_free;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (writeback && copy_to_user(user_msrs->entries, entries, size))
|
|
|
- goto out_free;
|
|
|
-
|
|
|
- r = n;
|
|
|
-
|
|
|
-out_free:
|
|
|
- vfree(entries);
|
|
|
-out:
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * Translate a guest virtual address to a guest physical address.
|
|
|
- */
|
|
|
-static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_translation *tr)
|
|
|
-{
|
|
|
- unsigned long vaddr = tr->linear_address;
|
|
|
- gpa_t gpa;
|
|
|
-
|
|
|
- vcpu_load(vcpu);
|
|
|
- mutex_lock(&vcpu->kvm->lock);
|
|
|
- gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
|
|
|
- tr->physical_address = gpa;
|
|
|
- tr->valid = gpa != UNMAPPED_GVA;
|
|
|
- tr->writeable = 1;
|
|
|
- tr->usermode = 0;
|
|
|
- mutex_unlock(&vcpu->kvm->lock);
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_interrupt *irq)
|
|
|
-{
|
|
|
- if (irq->irq < 0 || irq->irq >= 256)
|
|
|
- return -EINVAL;
|
|
|
- if (irqchip_in_kernel(vcpu->kvm))
|
|
|
- return -ENXIO;
|
|
|
- vcpu_load(vcpu);
|
|
|
-
|
|
|
- set_bit(irq->irq, vcpu->irq_pending);
|
|
|
- set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
|
|
|
-
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_debug_guest *dbg)
|
|
|
-{
|
|
|
- int r;
|
|
|
-
|
|
|
- vcpu_load(vcpu);
|
|
|
-
|
|
|
- r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
|
|
|
-
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
|
|
|
- unsigned long address,
|
|
|
- int *type)
|
|
|
-{
|
|
|
- struct kvm_vcpu *vcpu = vma->vm_file->private_data;
|
|
|
- unsigned long pgoff;
|
|
|
- struct page *page;
|
|
|
-
|
|
|
- pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
|
|
|
- if (pgoff == 0)
|
|
|
- page = virt_to_page(vcpu->run);
|
|
|
- else if (pgoff == KVM_PIO_PAGE_OFFSET)
|
|
|
- page = virt_to_page(vcpu->pio_data);
|
|
|
- else
|
|
|
- return NOPAGE_SIGBUS;
|
|
|
- get_page(page);
|
|
|
- if (type != NULL)
|
|
|
- *type = VM_FAULT_MINOR;
|
|
|
-
|
|
|
- return page;
|
|
|
-}
|
|
|
-
|
|
|
-static struct vm_operations_struct kvm_vcpu_vm_ops = {
|
|
|
- .nopage = kvm_vcpu_nopage,
|
|
|
-};
|
|
|
-
|
|
|
-static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
-{
|
|
|
- vma->vm_ops = &kvm_vcpu_vm_ops;
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int kvm_vcpu_release(struct inode *inode, struct file *filp)
|
|
|
-{
|
|
|
- struct kvm_vcpu *vcpu = filp->private_data;
|
|
|
-
|
|
|
- fput(vcpu->kvm->filp);
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static struct file_operations kvm_vcpu_fops = {
|
|
|
- .release = kvm_vcpu_release,
|
|
|
- .unlocked_ioctl = kvm_vcpu_ioctl,
|
|
|
- .compat_ioctl = kvm_vcpu_ioctl,
|
|
|
- .mmap = kvm_vcpu_mmap,
|
|
|
-};
|
|
|
-
|
|
|
-/*
|
|
|
- * Allocates an inode for the vcpu.
|
|
|
- */
|
|
|
-static int create_vcpu_fd(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- int fd, r;
|
|
|
- struct inode *inode;
|
|
|
- struct file *file;
|
|
|
-
|
|
|
- r = anon_inode_getfd(&fd, &inode, &file,
|
|
|
- "kvm-vcpu", &kvm_vcpu_fops, vcpu);
|
|
|
- if (r)
|
|
|
- return r;
|
|
|
- atomic_inc(&vcpu->kvm->filp->f_count);
|
|
|
- return fd;
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * Creates some virtual cpus. Good luck creating more than one.
|
|
|
- */
|
|
|
-static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
|
|
|
-{
|
|
|
- int r;
|
|
|
- struct kvm_vcpu *vcpu;
|
|
|
-
|
|
|
- if (!valid_vcpu(n))
|
|
|
- return -EINVAL;
|
|
|
-
|
|
|
- vcpu = kvm_x86_ops->vcpu_create(kvm, n);
|
|
|
- if (IS_ERR(vcpu))
|
|
|
- return PTR_ERR(vcpu);
|
|
|
-
|
|
|
- preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
|
|
|
-
|
|
|
- /* We do fxsave: this must be aligned. */
|
|
|
- BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
|
|
|
-
|
|
|
- vcpu_load(vcpu);
|
|
|
- r = kvm_mmu_setup(vcpu);
|
|
|
- vcpu_put(vcpu);
|
|
|
- if (r < 0)
|
|
|
- goto free_vcpu;
|
|
|
-
|
|
|
- mutex_lock(&kvm->lock);
|
|
|
- if (kvm->vcpus[n]) {
|
|
|
- r = -EEXIST;
|
|
|
- mutex_unlock(&kvm->lock);
|
|
|
- goto mmu_unload;
|
|
|
- }
|
|
|
- kvm->vcpus[n] = vcpu;
|
|
|
- mutex_unlock(&kvm->lock);
|
|
|
-
|
|
|
- /* Now it's all set up, let userspace reach it */
|
|
|
- r = create_vcpu_fd(vcpu);
|
|
|
- if (r < 0)
|
|
|
- goto unlink;
|
|
|
- return r;
|
|
|
-
|
|
|
-unlink:
|
|
|
- mutex_lock(&kvm->lock);
|
|
|
- kvm->vcpus[n] = NULL;
|
|
|
- mutex_unlock(&kvm->lock);
|
|
|
-
|
|
|
-mmu_unload:
|
|
|
- vcpu_load(vcpu);
|
|
|
- kvm_mmu_unload(vcpu);
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
-free_vcpu:
|
|
|
- kvm_x86_ops->vcpu_free(vcpu);
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
|
|
|
-{
|
|
|
- u64 efer;
|
|
|
- int i;
|
|
|
- struct kvm_cpuid_entry *e, *entry;
|
|
|
-
|
|
|
- rdmsrl(MSR_EFER, efer);
|
|
|
- entry = NULL;
|
|
|
- for (i = 0; i < vcpu->cpuid_nent; ++i) {
|
|
|
- e = &vcpu->cpuid_entries[i];
|
|
|
- if (e->function == 0x80000001) {
|
|
|
- entry = e;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
|
|
|
- entry->edx &= ~(1 << 20);
|
|
|
- printk(KERN_INFO "kvm: guest NX capability removed\n");
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_cpuid *cpuid,
|
|
|
- struct kvm_cpuid_entry __user *entries)
|
|
|
-{
|
|
|
- int r;
|
|
|
-
|
|
|
- r = -E2BIG;
|
|
|
- if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&vcpu->cpuid_entries, entries,
|
|
|
- cpuid->nent * sizeof(struct kvm_cpuid_entry)))
|
|
|
- goto out;
|
|
|
- vcpu->cpuid_nent = cpuid->nent;
|
|
|
- cpuid_fix_nx_cap(vcpu);
|
|
|
- return 0;
|
|
|
-
|
|
|
-out:
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
|
|
|
-{
|
|
|
- if (sigset) {
|
|
|
- sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
|
|
|
- vcpu->sigset_active = 1;
|
|
|
- vcpu->sigset = *sigset;
|
|
|
- } else
|
|
|
- vcpu->sigset_active = 0;
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
|
|
|
- * we have asm/x86/processor.h
|
|
|
- */
|
|
|
-struct fxsave {
|
|
|
- u16 cwd;
|
|
|
- u16 swd;
|
|
|
- u16 twd;
|
|
|
- u16 fop;
|
|
|
- u64 rip;
|
|
|
- u64 rdp;
|
|
|
- u32 mxcsr;
|
|
|
- u32 mxcsr_mask;
|
|
|
- u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
|
|
|
-#ifdef CONFIG_X86_64
|
|
|
- u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
|
|
|
-#else
|
|
|
- u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
|
|
|
-#endif
|
|
|
-};
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
|
|
|
-{
|
|
|
- struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
|
|
|
-
|
|
|
- vcpu_load(vcpu);
|
|
|
-
|
|
|
- memcpy(fpu->fpr, fxsave->st_space, 128);
|
|
|
- fpu->fcw = fxsave->cwd;
|
|
|
- fpu->fsw = fxsave->swd;
|
|
|
- fpu->ftwx = fxsave->twd;
|
|
|
- fpu->last_opcode = fxsave->fop;
|
|
|
- fpu->last_ip = fxsave->rip;
|
|
|
- fpu->last_dp = fxsave->rdp;
|
|
|
- memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
|
|
|
-
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
|
|
|
-{
|
|
|
- struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
|
|
|
-
|
|
|
- vcpu_load(vcpu);
|
|
|
-
|
|
|
- memcpy(fxsave->st_space, fpu->fpr, 128);
|
|
|
- fxsave->cwd = fpu->fcw;
|
|
|
- fxsave->swd = fpu->fsw;
|
|
|
- fxsave->twd = fpu->ftwx;
|
|
|
- fxsave->fop = fpu->last_opcode;
|
|
|
- fxsave->rip = fpu->last_ip;
|
|
|
- fxsave->rdp = fpu->last_dp;
|
|
|
- memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
|
|
|
-
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_lapic_state *s)
|
|
|
-{
|
|
|
- vcpu_load(vcpu);
|
|
|
- memcpy(s->regs, vcpu->apic->regs, sizeof *s);
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
|
|
|
- struct kvm_lapic_state *s)
|
|
|
-{
|
|
|
- vcpu_load(vcpu);
|
|
|
- memcpy(vcpu->apic->regs, s->regs, sizeof *s);
|
|
|
- kvm_apic_post_state_restore(vcpu);
|
|
|
- vcpu_put(vcpu);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static long kvm_vcpu_ioctl(struct file *filp,
|
|
|
- unsigned int ioctl, unsigned long arg)
|
|
|
-{
|
|
|
- struct kvm_vcpu *vcpu = filp->private_data;
|
|
|
- void __user *argp = (void __user *)arg;
|
|
|
- int r = -EINVAL;
|
|
|
-
|
|
|
- switch (ioctl) {
|
|
|
- case KVM_RUN:
|
|
|
- r = -EINVAL;
|
|
|
- if (arg)
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
|
|
|
- break;
|
|
|
- case KVM_GET_REGS: {
|
|
|
- struct kvm_regs kvm_regs;
|
|
|
-
|
|
|
- memset(&kvm_regs, 0, sizeof kvm_regs);
|
|
|
- r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_SET_REGS: {
|
|
|
- struct kvm_regs kvm_regs;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_GET_SREGS: {
|
|
|
- struct kvm_sregs kvm_sregs;
|
|
|
-
|
|
|
- memset(&kvm_sregs, 0, sizeof kvm_sregs);
|
|
|
- r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_SET_SREGS: {
|
|
|
- struct kvm_sregs kvm_sregs;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_TRANSLATE: {
|
|
|
- struct kvm_translation tr;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&tr, argp, sizeof tr))
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_translate(vcpu, &tr);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_to_user(argp, &tr, sizeof tr))
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_INTERRUPT: {
|
|
|
- struct kvm_interrupt irq;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&irq, argp, sizeof irq))
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_DEBUG_GUEST: {
|
|
|
- struct kvm_debug_guest dbg;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&dbg, argp, sizeof dbg))
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_GET_MSRS:
|
|
|
- r = msr_io(vcpu, argp, kvm_get_msr, 1);
|
|
|
- break;
|
|
|
- case KVM_SET_MSRS:
|
|
|
- r = msr_io(vcpu, argp, do_set_msr, 0);
|
|
|
- break;
|
|
|
- case KVM_SET_CPUID: {
|
|
|
- struct kvm_cpuid __user *cpuid_arg = argp;
|
|
|
- struct kvm_cpuid cpuid;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_SET_SIGNAL_MASK: {
|
|
|
- struct kvm_signal_mask __user *sigmask_arg = argp;
|
|
|
- struct kvm_signal_mask kvm_sigmask;
|
|
|
- sigset_t sigset, *p;
|
|
|
-
|
|
|
- p = NULL;
|
|
|
- if (argp) {
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&kvm_sigmask, argp,
|
|
|
- sizeof kvm_sigmask))
|
|
|
- goto out;
|
|
|
- r = -EINVAL;
|
|
|
- if (kvm_sigmask.len != sizeof sigset)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&sigset, sigmask_arg->sigset,
|
|
|
- sizeof sigset))
|
|
|
- goto out;
|
|
|
- p = &sigset;
|
|
|
- }
|
|
|
- r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_GET_FPU: {
|
|
|
- struct kvm_fpu fpu;
|
|
|
-
|
|
|
- memset(&fpu, 0, sizeof fpu);
|
|
|
- r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_to_user(argp, &fpu, sizeof fpu))
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_SET_FPU: {
|
|
|
- struct kvm_fpu fpu;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&fpu, argp, sizeof fpu))
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_GET_LAPIC: {
|
|
|
- struct kvm_lapic_state lapic;
|
|
|
-
|
|
|
- memset(&lapic, 0, sizeof lapic);
|
|
|
- r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_to_user(argp, &lapic, sizeof lapic))
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_SET_LAPIC: {
|
|
|
- struct kvm_lapic_state lapic;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&lapic, argp, sizeof lapic))
|
|
|
- goto out;
|
|
|
- r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- default:
|
|
|
- ;
|
|
|
- }
|
|
|
-out:
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-static long kvm_vm_ioctl(struct file *filp,
|
|
|
- unsigned int ioctl, unsigned long arg)
|
|
|
-{
|
|
|
- struct kvm *kvm = filp->private_data;
|
|
|
- void __user *argp = (void __user *)arg;
|
|
|
- int r = -EINVAL;
|
|
|
-
|
|
|
- switch (ioctl) {
|
|
|
- case KVM_CREATE_VCPU:
|
|
|
- r = kvm_vm_ioctl_create_vcpu(kvm, arg);
|
|
|
- if (r < 0)
|
|
|
- goto out;
|
|
|
- break;
|
|
|
- case KVM_SET_MEMORY_REGION: {
|
|
|
- struct kvm_memory_region kvm_mem;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
|
|
|
- goto out;
|
|
|
- r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_GET_DIRTY_LOG: {
|
|
|
- struct kvm_dirty_log log;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&log, argp, sizeof log))
|
|
|
- goto out;
|
|
|
- r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_SET_MEMORY_ALIAS: {
|
|
|
- struct kvm_memory_alias alias;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&alias, argp, sizeof alias))
|
|
|
- goto out;
|
|
|
- r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_CREATE_IRQCHIP:
|
|
|
- r = -ENOMEM;
|
|
|
- kvm->vpic = kvm_create_pic(kvm);
|
|
|
- if (kvm->vpic) {
|
|
|
- r = kvm_ioapic_init(kvm);
|
|
|
- if (r) {
|
|
|
- kfree(kvm->vpic);
|
|
|
- kvm->vpic = NULL;
|
|
|
- goto out;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- goto out;
|
|
|
- break;
|
|
|
- case KVM_IRQ_LINE: {
|
|
|
- struct kvm_irq_level irq_event;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&irq_event, argp, sizeof irq_event))
|
|
|
- goto out;
|
|
|
- if (irqchip_in_kernel(kvm)) {
|
|
|
- mutex_lock(&kvm->lock);
|
|
|
- if (irq_event.irq < 16)
|
|
|
- kvm_pic_set_irq(pic_irqchip(kvm),
|
|
|
- irq_event.irq,
|
|
|
- irq_event.level);
|
|
|
- kvm_ioapic_set_irq(kvm->vioapic,
|
|
|
- irq_event.irq,
|
|
|
- irq_event.level);
|
|
|
- mutex_unlock(&kvm->lock);
|
|
|
- r = 0;
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_GET_IRQCHIP: {
|
|
|
- /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
|
|
|
- struct kvm_irqchip chip;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&chip, argp, sizeof chip))
|
|
|
- goto out;
|
|
|
- r = -ENXIO;
|
|
|
- if (!irqchip_in_kernel(kvm))
|
|
|
- goto out;
|
|
|
- r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_to_user(argp, &chip, sizeof chip))
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_SET_IRQCHIP: {
|
|
|
- /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
|
|
|
- struct kvm_irqchip chip;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&chip, argp, sizeof chip))
|
|
|
- goto out;
|
|
|
- r = -ENXIO;
|
|
|
- if (!irqchip_in_kernel(kvm))
|
|
|
- goto out;
|
|
|
- r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
|
|
|
- if (r)
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- default:
|
|
|
- ;
|
|
|
- }
|
|
|
-out:
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
|
|
|
- unsigned long address,
|
|
|
- int *type)
|
|
|
-{
|
|
|
- struct kvm *kvm = vma->vm_file->private_data;
|
|
|
- unsigned long pgoff;
|
|
|
- struct page *page;
|
|
|
-
|
|
|
- pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
|
|
|
- page = gfn_to_page(kvm, pgoff);
|
|
|
- if (!page)
|
|
|
- return NOPAGE_SIGBUS;
|
|
|
- get_page(page);
|
|
|
- if (type != NULL)
|
|
|
- *type = VM_FAULT_MINOR;
|
|
|
-
|
|
|
- return page;
|
|
|
-}
|
|
|
-
|
|
|
-static struct vm_operations_struct kvm_vm_vm_ops = {
|
|
|
- .nopage = kvm_vm_nopage,
|
|
|
-};
|
|
|
-
|
|
|
-static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
-{
|
|
|
- vma->vm_ops = &kvm_vm_vm_ops;
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static struct file_operations kvm_vm_fops = {
|
|
|
- .release = kvm_vm_release,
|
|
|
- .unlocked_ioctl = kvm_vm_ioctl,
|
|
|
- .compat_ioctl = kvm_vm_ioctl,
|
|
|
- .mmap = kvm_vm_mmap,
|
|
|
-};
|
|
|
-
|
|
|
-static int kvm_dev_ioctl_create_vm(void)
|
|
|
-{
|
|
|
- int fd, r;
|
|
|
- struct inode *inode;
|
|
|
- struct file *file;
|
|
|
- struct kvm *kvm;
|
|
|
-
|
|
|
- kvm = kvm_create_vm();
|
|
|
- if (IS_ERR(kvm))
|
|
|
- return PTR_ERR(kvm);
|
|
|
- r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
|
|
|
- if (r) {
|
|
|
- kvm_destroy_vm(kvm);
|
|
|
- return r;
|
|
|
- }
|
|
|
-
|
|
|
- kvm->filp = file;
|
|
|
-
|
|
|
- return fd;
|
|
|
-}
|
|
|
-
|
|
|
-static long kvm_dev_ioctl(struct file *filp,
|
|
|
- unsigned int ioctl, unsigned long arg)
|
|
|
-{
|
|
|
- void __user *argp = (void __user *)arg;
|
|
|
- long r = -EINVAL;
|
|
|
-
|
|
|
- switch (ioctl) {
|
|
|
- case KVM_GET_API_VERSION:
|
|
|
- r = -EINVAL;
|
|
|
- if (arg)
|
|
|
- goto out;
|
|
|
- r = KVM_API_VERSION;
|
|
|
- break;
|
|
|
- case KVM_CREATE_VM:
|
|
|
- r = -EINVAL;
|
|
|
- if (arg)
|
|
|
- goto out;
|
|
|
- r = kvm_dev_ioctl_create_vm();
|
|
|
- break;
|
|
|
- case KVM_GET_MSR_INDEX_LIST: {
|
|
|
- struct kvm_msr_list __user *user_msr_list = argp;
|
|
|
- struct kvm_msr_list msr_list;
|
|
|
- unsigned n;
|
|
|
-
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
|
|
|
- goto out;
|
|
|
- n = msr_list.nmsrs;
|
|
|
- msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
|
|
|
- if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
|
|
|
- goto out;
|
|
|
- r = -E2BIG;
|
|
|
- if (n < num_msrs_to_save)
|
|
|
- goto out;
|
|
|
- r = -EFAULT;
|
|
|
- if (copy_to_user(user_msr_list->indices, &msrs_to_save,
|
|
|
- num_msrs_to_save * sizeof(u32)))
|
|
|
- goto out;
|
|
|
- if (copy_to_user(user_msr_list->indices
|
|
|
- + num_msrs_to_save * sizeof(u32),
|
|
|
- &emulated_msrs,
|
|
|
- ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
|
|
|
- goto out;
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_CHECK_EXTENSION: {
|
|
|
- int ext = (long)argp;
|
|
|
-
|
|
|
- switch (ext) {
|
|
|
- case KVM_CAP_IRQCHIP:
|
|
|
- case KVM_CAP_HLT:
|
|
|
- r = 1;
|
|
|
- break;
|
|
|
- default:
|
|
|
- r = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- case KVM_GET_VCPU_MMAP_SIZE:
|
|
|
- r = -EINVAL;
|
|
|
- if (arg)
|
|
|
- goto out;
|
|
|
- r = 2 * PAGE_SIZE;
|
|
|
- break;
|
|
|
- default:
|
|
|
- ;
|
|
|
- }
|
|
|
-out:
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-static struct file_operations kvm_chardev_ops = {
|
|
|
- .unlocked_ioctl = kvm_dev_ioctl,
|
|
|
- .compat_ioctl = kvm_dev_ioctl,
|
|
|
-};
|
|
|
-
|
|
|
-static struct miscdevice kvm_dev = {
|
|
|
- KVM_MINOR,
|
|
|
- "kvm",
|
|
|
- &kvm_chardev_ops,
|
|
|
-};
|
|
|
-
|
|
|
-/*
|
|
|
- * Make sure that a cpu that is being hot-unplugged does not have any vcpus
|
|
|
- * cached on it.
|
|
|
- */
|
|
|
-static void decache_vcpus_on_cpu(int cpu)
|
|
|
-{
|
|
|
- struct kvm *vm;
|
|
|
- struct kvm_vcpu *vcpu;
|
|
|
- int i;
|
|
|
-
|
|
|
- spin_lock(&kvm_lock);
|
|
|
- list_for_each_entry(vm, &vm_list, vm_list)
|
|
|
- for (i = 0; i < KVM_MAX_VCPUS; ++i) {
|
|
|
- vcpu = vm->vcpus[i];
|
|
|
- if (!vcpu)
|
|
|
- continue;
|
|
|
- /*
|
|
|
- * If the vcpu is locked, then it is running on some
|
|
|
- * other cpu and therefore it is not cached on the
|
|
|
- * cpu in question.
|
|
|
- *
|
|
|
- * If it's not locked, check the last cpu it executed
|
|
|
- * on.
|
|
|
- */
|
|
|
- if (mutex_trylock(&vcpu->mutex)) {
|
|
|
- if (vcpu->cpu == cpu) {
|
|
|
- kvm_x86_ops->vcpu_decache(vcpu);
|
|
|
- vcpu->cpu = -1;
|
|
|
- }
|
|
|
- mutex_unlock(&vcpu->mutex);
|
|
|
- }
|
|
|
- }
|
|
|
- spin_unlock(&kvm_lock);
|
|
|
-}
|
|
|
+ set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
|
|
|
+ set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
|
|
|
+ set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
|
|
|
+ set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
|
|
|
|
|
|
-static void hardware_enable(void *junk)
|
|
|
-{
|
|
|
- int cpu = raw_smp_processor_id();
|
|
|
+ set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
|
|
|
+ set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
|
|
|
|
|
|
- if (cpu_isset(cpu, cpus_hardware_enabled))
|
|
|
- return;
|
|
|
- cpu_set(cpu, cpus_hardware_enabled);
|
|
|
- kvm_x86_ops->hardware_enable(NULL);
|
|
|
+ vcpu_put(vcpu);
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-static void hardware_disable(void *junk)
|
|
|
+int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_debug_guest *dbg)
|
|
|
{
|
|
|
- int cpu = raw_smp_processor_id();
|
|
|
+ int r;
|
|
|
|
|
|
- if (!cpu_isset(cpu, cpus_hardware_enabled))
|
|
|
- return;
|
|
|
- cpu_clear(cpu, cpus_hardware_enabled);
|
|
|
- decache_vcpus_on_cpu(cpu);
|
|
|
- kvm_x86_ops->hardware_disable(NULL);
|
|
|
-}
|
|
|
+ vcpu_load(vcpu);
|
|
|
|
|
|
-static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
|
|
|
- void *v)
|
|
|
-{
|
|
|
- int cpu = (long)v;
|
|
|
+ r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
|
|
|
|
|
|
- switch (val) {
|
|
|
- case CPU_DYING:
|
|
|
- case CPU_DYING_FROZEN:
|
|
|
- printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
|
|
|
- cpu);
|
|
|
- hardware_disable(NULL);
|
|
|
- break;
|
|
|
- case CPU_UP_CANCELED:
|
|
|
- case CPU_UP_CANCELED_FROZEN:
|
|
|
- printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
|
|
|
- cpu);
|
|
|
- smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
|
|
|
- break;
|
|
|
- case CPU_ONLINE:
|
|
|
- case CPU_ONLINE_FROZEN:
|
|
|
- printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
|
|
|
- cpu);
|
|
|
- smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
|
|
|
- break;
|
|
|
- }
|
|
|
- return NOTIFY_OK;
|
|
|
-}
|
|
|
+ vcpu_put(vcpu);
|
|
|
|
|
|
-static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
|
|
|
- void *v)
|
|
|
-{
|
|
|
- if (val == SYS_RESTART) {
|
|
|
- /*
|
|
|
- * Some (well, at least mine) BIOSes hang on reboot if
|
|
|
- * in vmx root mode.
|
|
|
- */
|
|
|
- printk(KERN_INFO "kvm: exiting hardware virtualization\n");
|
|
|
- on_each_cpu(hardware_disable, NULL, 0, 1);
|
|
|
- }
|
|
|
- return NOTIFY_OK;
|
|
|
+ return r;
|
|
|
}
|
|
|
|
|
|
-static struct notifier_block kvm_reboot_notifier = {
|
|
|
- .notifier_call = kvm_reboot,
|
|
|
- .priority = 0,
|
|
|
+/*
|
|
|
+ * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
|
|
|
+ * we have asm/x86/processor.h
|
|
|
+ */
|
|
|
+struct fxsave {
|
|
|
+ u16 cwd;
|
|
|
+ u16 swd;
|
|
|
+ u16 twd;
|
|
|
+ u16 fop;
|
|
|
+ u64 rip;
|
|
|
+ u64 rdp;
|
|
|
+ u32 mxcsr;
|
|
|
+ u32 mxcsr_mask;
|
|
|
+ u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
|
|
|
+#ifdef CONFIG_X86_64
|
|
|
+ u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
|
|
|
+#else
|
|
|
+ u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
|
|
|
+#endif
|
|
|
};
|
|
|
|
|
|
-void kvm_io_bus_init(struct kvm_io_bus *bus)
|
|
|
+/*
|
|
|
+ * Translate a guest virtual address to a guest physical address.
|
|
|
+ */
|
|
|
+int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
|
|
|
+ struct kvm_translation *tr)
|
|
|
{
|
|
|
- memset(bus, 0, sizeof(*bus));
|
|
|
+ unsigned long vaddr = tr->linear_address;
|
|
|
+ gpa_t gpa;
|
|
|
+
|
|
|
+ vcpu_load(vcpu);
|
|
|
+ down_read(¤t->mm->mmap_sem);
|
|
|
+ gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
|
|
|
+ up_read(¤t->mm->mmap_sem);
|
|
|
+ tr->physical_address = gpa;
|
|
|
+ tr->valid = gpa != UNMAPPED_GVA;
|
|
|
+ tr->writeable = 1;
|
|
|
+ tr->usermode = 0;
|
|
|
+ vcpu_put(vcpu);
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-void kvm_io_bus_destroy(struct kvm_io_bus *bus)
|
|
|
+int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
|
|
|
{
|
|
|
- int i;
|
|
|
+ struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
|
|
|
+
|
|
|
+ vcpu_load(vcpu);
|
|
|
+
|
|
|
+ memcpy(fpu->fpr, fxsave->st_space, 128);
|
|
|
+ fpu->fcw = fxsave->cwd;
|
|
|
+ fpu->fsw = fxsave->swd;
|
|
|
+ fpu->ftwx = fxsave->twd;
|
|
|
+ fpu->last_opcode = fxsave->fop;
|
|
|
+ fpu->last_ip = fxsave->rip;
|
|
|
+ fpu->last_dp = fxsave->rdp;
|
|
|
+ memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
|
|
|
|
|
|
- for (i = 0; i < bus->dev_count; i++) {
|
|
|
- struct kvm_io_device *pos = bus->devs[i];
|
|
|
+ vcpu_put(vcpu);
|
|
|
|
|
|
- kvm_iodevice_destructor(pos);
|
|
|
- }
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
|
|
|
+int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
|
|
|
{
|
|
|
- int i;
|
|
|
+ struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
|
|
|
|
|
|
- for (i = 0; i < bus->dev_count; i++) {
|
|
|
- struct kvm_io_device *pos = bus->devs[i];
|
|
|
+ vcpu_load(vcpu);
|
|
|
|
|
|
- if (pos->in_range(pos, addr))
|
|
|
- return pos;
|
|
|
- }
|
|
|
+ memcpy(fxsave->st_space, fpu->fpr, 128);
|
|
|
+ fxsave->cwd = fpu->fcw;
|
|
|
+ fxsave->swd = fpu->fsw;
|
|
|
+ fxsave->twd = fpu->ftwx;
|
|
|
+ fxsave->fop = fpu->last_opcode;
|
|
|
+ fxsave->rip = fpu->last_ip;
|
|
|
+ fxsave->rdp = fpu->last_dp;
|
|
|
+ memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
|
|
|
|
|
|
- return NULL;
|
|
|
+ vcpu_put(vcpu);
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
|
|
|
+void fx_init(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
|
|
|
+ unsigned after_mxcsr_mask;
|
|
|
|
|
|
- bus->devs[bus->dev_count++] = dev;
|
|
|
-}
|
|
|
+ /* Initialize guest FPU by resetting ours and saving into guest's */
|
|
|
+ preempt_disable();
|
|
|
+ fx_save(&vcpu->arch.host_fx_image);
|
|
|
+ fpu_init();
|
|
|
+ fx_save(&vcpu->arch.guest_fx_image);
|
|
|
+ fx_restore(&vcpu->arch.host_fx_image);
|
|
|
+ preempt_enable();
|
|
|
|
|
|
-static struct notifier_block kvm_cpu_notifier = {
|
|
|
- .notifier_call = kvm_cpu_hotplug,
|
|
|
- .priority = 20, /* must be > scheduler priority */
|
|
|
-};
|
|
|
+ vcpu->arch.cr0 |= X86_CR0_ET;
|
|
|
+ after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
|
|
|
+ vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
|
|
|
+ memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
|
|
|
+ 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
|
|
|
+}
|
|
|
+EXPORT_SYMBOL_GPL(fx_init);
|
|
|
|
|
|
-static u64 stat_get(void *_offset)
|
|
|
+void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- unsigned offset = (long)_offset;
|
|
|
- u64 total = 0;
|
|
|
- struct kvm *kvm;
|
|
|
- struct kvm_vcpu *vcpu;
|
|
|
- int i;
|
|
|
+ if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
|
|
|
+ return;
|
|
|
|
|
|
- spin_lock(&kvm_lock);
|
|
|
- list_for_each_entry(kvm, &vm_list, vm_list)
|
|
|
- for (i = 0; i < KVM_MAX_VCPUS; ++i) {
|
|
|
- vcpu = kvm->vcpus[i];
|
|
|
- if (vcpu)
|
|
|
- total += *(u32 *)((void *)vcpu + offset);
|
|
|
- }
|
|
|
- spin_unlock(&kvm_lock);
|
|
|
- return total;
|
|
|
+ vcpu->guest_fpu_loaded = 1;
|
|
|
+ fx_save(&vcpu->arch.host_fx_image);
|
|
|
+ fx_restore(&vcpu->arch.guest_fx_image);
|
|
|
}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
|
|
|
|
|
|
-DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
|
|
|
-
|
|
|
-static __init void kvm_init_debug(void)
|
|
|
+void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- struct kvm_stats_debugfs_item *p;
|
|
|
+ if (!vcpu->guest_fpu_loaded)
|
|
|
+ return;
|
|
|
|
|
|
- debugfs_dir = debugfs_create_dir("kvm", NULL);
|
|
|
- for (p = debugfs_entries; p->name; ++p)
|
|
|
- p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
|
|
|
- (void *)(long)p->offset,
|
|
|
- &stat_fops);
|
|
|
+ vcpu->guest_fpu_loaded = 0;
|
|
|
+ fx_save(&vcpu->arch.guest_fx_image);
|
|
|
+ fx_restore(&vcpu->arch.host_fx_image);
|
|
|
+ ++vcpu->stat.fpu_reload;
|
|
|
}
|
|
|
+EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
|
|
|
|
|
|
-static void kvm_exit_debug(void)
|
|
|
+void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- struct kvm_stats_debugfs_item *p;
|
|
|
-
|
|
|
- for (p = debugfs_entries; p->name; ++p)
|
|
|
- debugfs_remove(p->dentry);
|
|
|
- debugfs_remove(debugfs_dir);
|
|
|
+ kvm_x86_ops->vcpu_free(vcpu);
|
|
|
}
|
|
|
|
|
|
-static int kvm_suspend(struct sys_device *dev, pm_message_t state)
|
|
|
+struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
|
|
|
+ unsigned int id)
|
|
|
{
|
|
|
- hardware_disable(NULL);
|
|
|
- return 0;
|
|
|
+ return kvm_x86_ops->vcpu_create(kvm, id);
|
|
|
}
|
|
|
|
|
|
-static int kvm_resume(struct sys_device *dev)
|
|
|
+int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- hardware_enable(NULL);
|
|
|
+ int r;
|
|
|
+
|
|
|
+ /* We do fxsave: this must be aligned. */
|
|
|
+ BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
|
|
|
+
|
|
|
+ vcpu_load(vcpu);
|
|
|
+ r = kvm_arch_vcpu_reset(vcpu);
|
|
|
+ if (r == 0)
|
|
|
+ r = kvm_mmu_setup(vcpu);
|
|
|
+ vcpu_put(vcpu);
|
|
|
+ if (r < 0)
|
|
|
+ goto free_vcpu;
|
|
|
+
|
|
|
return 0;
|
|
|
+free_vcpu:
|
|
|
+ kvm_x86_ops->vcpu_free(vcpu);
|
|
|
+ return r;
|
|
|
}
|
|
|
|
|
|
-static struct sysdev_class kvm_sysdev_class = {
|
|
|
- .name = "kvm",
|
|
|
- .suspend = kvm_suspend,
|
|
|
- .resume = kvm_resume,
|
|
|
-};
|
|
|
+void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ vcpu_load(vcpu);
|
|
|
+ kvm_mmu_unload(vcpu);
|
|
|
+ vcpu_put(vcpu);
|
|
|
|
|
|
-static struct sys_device kvm_sysdev = {
|
|
|
- .id = 0,
|
|
|
- .cls = &kvm_sysdev_class,
|
|
|
-};
|
|
|
+ kvm_x86_ops->vcpu_free(vcpu);
|
|
|
+}
|
|
|
|
|
|
-hpa_t bad_page_address;
|
|
|
+int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ return kvm_x86_ops->vcpu_reset(vcpu);
|
|
|
+}
|
|
|
|
|
|
-static inline
|
|
|
-struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
|
|
|
+void kvm_arch_hardware_enable(void *garbage)
|
|
|
{
|
|
|
- return container_of(pn, struct kvm_vcpu, preempt_notifier);
|
|
|
+ kvm_x86_ops->hardware_enable(garbage);
|
|
|
}
|
|
|
|
|
|
-static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
|
|
|
+void kvm_arch_hardware_disable(void *garbage)
|
|
|
{
|
|
|
- struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
|
|
|
+ kvm_x86_ops->hardware_disable(garbage);
|
|
|
+}
|
|
|
|
|
|
- kvm_x86_ops->vcpu_load(vcpu, cpu);
|
|
|
+int kvm_arch_hardware_setup(void)
|
|
|
+{
|
|
|
+ return kvm_x86_ops->hardware_setup();
|
|
|
}
|
|
|
|
|
|
-static void kvm_sched_out(struct preempt_notifier *pn,
|
|
|
- struct task_struct *next)
|
|
|
+void kvm_arch_hardware_unsetup(void)
|
|
|
{
|
|
|
- struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
|
|
|
+ kvm_x86_ops->hardware_unsetup();
|
|
|
+}
|
|
|
|
|
|
- kvm_x86_ops->vcpu_put(vcpu);
|
|
|
+void kvm_arch_check_processor_compat(void *rtn)
|
|
|
+{
|
|
|
+ kvm_x86_ops->check_processor_compatibility(rtn);
|
|
|
}
|
|
|
|
|
|
-int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
|
|
|
- struct module *module)
|
|
|
+int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
+ struct page *page;
|
|
|
+ struct kvm *kvm;
|
|
|
int r;
|
|
|
- int cpu;
|
|
|
|
|
|
- if (kvm_x86_ops) {
|
|
|
- printk(KERN_ERR "kvm: already loaded the other module\n");
|
|
|
- return -EEXIST;
|
|
|
- }
|
|
|
+ BUG_ON(vcpu->kvm == NULL);
|
|
|
+ kvm = vcpu->kvm;
|
|
|
|
|
|
- if (!ops->cpu_has_kvm_support()) {
|
|
|
- printk(KERN_ERR "kvm: no hardware support\n");
|
|
|
- return -EOPNOTSUPP;
|
|
|
- }
|
|
|
- if (ops->disabled_by_bios()) {
|
|
|
- printk(KERN_ERR "kvm: disabled by bios\n");
|
|
|
- return -EOPNOTSUPP;
|
|
|
- }
|
|
|
+ vcpu->arch.mmu.root_hpa = INVALID_PAGE;
|
|
|
+ if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
|
|
|
+ vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
|
|
|
+ else
|
|
|
+ vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
|
|
|
|
|
|
- kvm_x86_ops = ops;
|
|
|
+ page = alloc_page(GFP_KERNEL | __GFP_ZERO);
|
|
|
+ if (!page) {
|
|
|
+ r = -ENOMEM;
|
|
|
+ goto fail;
|
|
|
+ }
|
|
|
+ vcpu->arch.pio_data = page_address(page);
|
|
|
|
|
|
- r = kvm_x86_ops->hardware_setup();
|
|
|
+ r = kvm_mmu_create(vcpu);
|
|
|
if (r < 0)
|
|
|
- goto out;
|
|
|
+ goto fail_free_pio_data;
|
|
|
|
|
|
- for_each_online_cpu(cpu) {
|
|
|
- smp_call_function_single(cpu,
|
|
|
- kvm_x86_ops->check_processor_compatibility,
|
|
|
- &r, 0, 1);
|
|
|
+ if (irqchip_in_kernel(kvm)) {
|
|
|
+ r = kvm_create_lapic(vcpu);
|
|
|
if (r < 0)
|
|
|
- goto out_free_0;
|
|
|
+ goto fail_mmu_destroy;
|
|
|
}
|
|
|
|
|
|
- on_each_cpu(hardware_enable, NULL, 0, 1);
|
|
|
- r = register_cpu_notifier(&kvm_cpu_notifier);
|
|
|
- if (r)
|
|
|
- goto out_free_1;
|
|
|
- register_reboot_notifier(&kvm_reboot_notifier);
|
|
|
-
|
|
|
- r = sysdev_class_register(&kvm_sysdev_class);
|
|
|
- if (r)
|
|
|
- goto out_free_2;
|
|
|
-
|
|
|
- r = sysdev_register(&kvm_sysdev);
|
|
|
- if (r)
|
|
|
- goto out_free_3;
|
|
|
+ return 0;
|
|
|
|
|
|
- /* A kmem cache lets us meet the alignment requirements of fx_save. */
|
|
|
- kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
|
|
|
- __alignof__(struct kvm_vcpu), 0, 0);
|
|
|
- if (!kvm_vcpu_cache) {
|
|
|
- r = -ENOMEM;
|
|
|
- goto out_free_4;
|
|
|
- }
|
|
|
+fail_mmu_destroy:
|
|
|
+ kvm_mmu_destroy(vcpu);
|
|
|
+fail_free_pio_data:
|
|
|
+ free_page((unsigned long)vcpu->arch.pio_data);
|
|
|
+fail:
|
|
|
+ return r;
|
|
|
+}
|
|
|
|
|
|
- kvm_chardev_ops.owner = module;
|
|
|
+void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ kvm_free_lapic(vcpu);
|
|
|
+ kvm_mmu_destroy(vcpu);
|
|
|
+ free_page((unsigned long)vcpu->arch.pio_data);
|
|
|
+}
|
|
|
|
|
|
- r = misc_register(&kvm_dev);
|
|
|
- if (r) {
|
|
|
- printk (KERN_ERR "kvm: misc device register failed\n");
|
|
|
- goto out_free;
|
|
|
- }
|
|
|
+struct kvm *kvm_arch_create_vm(void)
|
|
|
+{
|
|
|
+ struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
|
|
|
|
|
|
- kvm_preempt_ops.sched_in = kvm_sched_in;
|
|
|
- kvm_preempt_ops.sched_out = kvm_sched_out;
|
|
|
+ if (!kvm)
|
|
|
+ return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
- return r;
|
|
|
+ INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
|
|
|
|
|
|
-out_free:
|
|
|
- kmem_cache_destroy(kvm_vcpu_cache);
|
|
|
-out_free_4:
|
|
|
- sysdev_unregister(&kvm_sysdev);
|
|
|
-out_free_3:
|
|
|
- sysdev_class_unregister(&kvm_sysdev_class);
|
|
|
-out_free_2:
|
|
|
- unregister_reboot_notifier(&kvm_reboot_notifier);
|
|
|
- unregister_cpu_notifier(&kvm_cpu_notifier);
|
|
|
-out_free_1:
|
|
|
- on_each_cpu(hardware_disable, NULL, 0, 1);
|
|
|
-out_free_0:
|
|
|
- kvm_x86_ops->hardware_unsetup();
|
|
|
-out:
|
|
|
- kvm_x86_ops = NULL;
|
|
|
- return r;
|
|
|
+ return kvm;
|
|
|
}
|
|
|
|
|
|
-void kvm_exit_x86(void)
|
|
|
+static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
|
|
|
{
|
|
|
- misc_deregister(&kvm_dev);
|
|
|
- kmem_cache_destroy(kvm_vcpu_cache);
|
|
|
- sysdev_unregister(&kvm_sysdev);
|
|
|
- sysdev_class_unregister(&kvm_sysdev_class);
|
|
|
- unregister_reboot_notifier(&kvm_reboot_notifier);
|
|
|
- unregister_cpu_notifier(&kvm_cpu_notifier);
|
|
|
- on_each_cpu(hardware_disable, NULL, 0, 1);
|
|
|
- kvm_x86_ops->hardware_unsetup();
|
|
|
- kvm_x86_ops = NULL;
|
|
|
+ vcpu_load(vcpu);
|
|
|
+ kvm_mmu_unload(vcpu);
|
|
|
+ vcpu_put(vcpu);
|
|
|
}
|
|
|
|
|
|
-static __init int kvm_init(void)
|
|
|
+static void kvm_free_vcpus(struct kvm *kvm)
|
|
|
{
|
|
|
- static struct page *bad_page;
|
|
|
- int r;
|
|
|
+ unsigned int i;
|
|
|
|
|
|
- r = kvm_mmu_module_init();
|
|
|
- if (r)
|
|
|
- goto out4;
|
|
|
+ /*
|
|
|
+ * Unpin any mmu pages first.
|
|
|
+ */
|
|
|
+ for (i = 0; i < KVM_MAX_VCPUS; ++i)
|
|
|
+ if (kvm->vcpus[i])
|
|
|
+ kvm_unload_vcpu_mmu(kvm->vcpus[i]);
|
|
|
+ for (i = 0; i < KVM_MAX_VCPUS; ++i) {
|
|
|
+ if (kvm->vcpus[i]) {
|
|
|
+ kvm_arch_vcpu_free(kvm->vcpus[i]);
|
|
|
+ kvm->vcpus[i] = NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- kvm_init_debug();
|
|
|
+}
|
|
|
|
|
|
- kvm_init_msr_list();
|
|
|
+void kvm_arch_destroy_vm(struct kvm *kvm)
|
|
|
+{
|
|
|
+ kfree(kvm->arch.vpic);
|
|
|
+ kfree(kvm->arch.vioapic);
|
|
|
+ kvm_free_vcpus(kvm);
|
|
|
+ kvm_free_physmem(kvm);
|
|
|
+ kfree(kvm);
|
|
|
+}
|
|
|
|
|
|
- if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
|
|
|
- r = -ENOMEM;
|
|
|
- goto out;
|
|
|
+int kvm_arch_set_memory_region(struct kvm *kvm,
|
|
|
+ struct kvm_userspace_memory_region *mem,
|
|
|
+ struct kvm_memory_slot old,
|
|
|
+ int user_alloc)
|
|
|
+{
|
|
|
+ int npages = mem->memory_size >> PAGE_SHIFT;
|
|
|
+ struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
|
|
|
+
|
|
|
+ /*To keep backward compatibility with older userspace,
|
|
|
+ *x86 needs to hanlde !user_alloc case.
|
|
|
+ */
|
|
|
+ if (!user_alloc) {
|
|
|
+ if (npages && !old.rmap) {
|
|
|
+ memslot->userspace_addr = do_mmap(NULL, 0,
|
|
|
+ npages * PAGE_SIZE,
|
|
|
+ PROT_READ | PROT_WRITE,
|
|
|
+ MAP_SHARED | MAP_ANONYMOUS,
|
|
|
+ 0);
|
|
|
+
|
|
|
+ if (IS_ERR((void *)memslot->userspace_addr))
|
|
|
+ return PTR_ERR((void *)memslot->userspace_addr);
|
|
|
+ } else {
|
|
|
+ if (!old.user_alloc && old.rmap) {
|
|
|
+ int ret;
|
|
|
+
|
|
|
+ ret = do_munmap(current->mm, old.userspace_addr,
|
|
|
+ old.npages * PAGE_SIZE);
|
|
|
+ if (ret < 0)
|
|
|
+ printk(KERN_WARNING
|
|
|
+ "kvm_vm_ioctl_set_memory_region: "
|
|
|
+ "failed to munmap memory\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!kvm->arch.n_requested_mmu_pages) {
|
|
|
+ unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
|
|
|
+ kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
|
|
|
}
|
|
|
|
|
|
- bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
|
|
|
- memset(__va(bad_page_address), 0, PAGE_SIZE);
|
|
|
+ kvm_mmu_slot_remove_write_access(kvm, mem->slot);
|
|
|
+ kvm_flush_remote_tlbs(kvm);
|
|
|
|
|
|
return 0;
|
|
|
+}
|
|
|
|
|
|
-out:
|
|
|
- kvm_exit_debug();
|
|
|
- kvm_mmu_module_exit();
|
|
|
-out4:
|
|
|
- return r;
|
|
|
+int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
|
|
|
+ || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
|
|
|
}
|
|
|
|
|
|
-static __exit void kvm_exit(void)
|
|
|
+static void vcpu_kick_intr(void *info)
|
|
|
{
|
|
|
- kvm_exit_debug();
|
|
|
- __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
|
|
|
- kvm_mmu_module_exit();
|
|
|
+#ifdef DEBUG
|
|
|
+ struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
|
|
|
+ printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
-module_init(kvm_init)
|
|
|
-module_exit(kvm_exit)
|
|
|
+void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
|
|
|
+{
|
|
|
+ int ipi_pcpu = vcpu->cpu;
|
|
|
|
|
|
-EXPORT_SYMBOL_GPL(kvm_init_x86);
|
|
|
-EXPORT_SYMBOL_GPL(kvm_exit_x86);
|
|
|
+ if (waitqueue_active(&vcpu->wq)) {
|
|
|
+ wake_up_interruptible(&vcpu->wq);
|
|
|
+ ++vcpu->stat.halt_wakeup;
|
|
|
+ }
|
|
|
+ if (vcpu->guest_mode)
|
|
|
+ smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
|
|
|
+}
|