From 0aa159fb220397d7ef90a19cd638a0c5c1c5176a Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 8 Apr 2010 20:44:37 +0300 Subject: [PATCH] kvm: Setup VCPU MSRs Use the KVM_SET_MSRS ioctl() to set up model-specific registers (MSRs) to a known state. The MSRs are initialized to zero for now because I'm not sure what are the right values. Signed-off-by: Pekka Enberg --- tools/kvm/include/kvm/kvm.h | 1 + tools/kvm/kvm.c | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index dd5a3bb40562..cddcad0a9f1d 100644 --- a/tools/kvm/include/kvm/kvm.h +++ b/tools/kvm/include/kvm/kvm.h @@ -26,6 +26,7 @@ struct kvm { struct kvm_regs regs; struct kvm_sregs sregs; struct kvm_fpu fpu; + struct kvm_msrs *msrs; /* dynamically allocated */ struct interrupt_table interrupt_table; }; diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c index 4bea85cd6407..bfff0b9177a1 100644 --- a/tools/kvm/kvm.c +++ b/tools/kvm/kvm.c @@ -378,6 +378,53 @@ static inline uint32_t selector_to_base(uint16_t selector) return (uint32_t)selector * 16; } +static struct kvm_msrs *kvm_msrs__new(size_t nmsrs) +{ + struct kvm_msrs *self = calloc(1, sizeof(*self) + (sizeof(struct kvm_msr_entry) * nmsrs)); + + if (!self) + die("out of memory"); + + return self; +} + +#define MSR_IA32_TIME_STAMP_COUNTER 0x10 + +#define MSR_IA32_SYSENTER_CS 0x174 +#define MSR_IA32_SYSENTER_ESP 0x175 +#define MSR_IA32_SYSENTER_EIP 0x176 + +#define MSR_IA32_STAR 0xc0000081 +#define MSR_IA32_LSTAR 0xc0000082 +#define MSR_IA32_CSTAR 0xc0000083 +#define MSR_IA32_FMASK 0xc0000084 +#define MSR_IA32_KERNEL_GS_BASE 0xc0000102 + +#define KVM_MSR_ENTRY(_index, _data) \ + (struct kvm_msr_entry) { .index = _index, .data = _data } + +static void kvm__setup_msrs(struct kvm *self) +{ + unsigned long ndx = 0; + + self->msrs = kvm_msrs__new(100); + + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_CS, 0x0); + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_ESP, 0x0); + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_EIP, 0x0); + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_STAR, 0x0); + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_CSTAR, 0x0); + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_KERNEL_GS_BASE, 0x0); + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_FMASK, 0x0); + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_LSTAR, 0x0); + self->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_TIME_STAMP_COUNTER, 0x0); + + self->msrs->nmsrs = ndx; + + if (ioctl(self->vcpu_fd, KVM_SET_MSRS, self->msrs) < 0) + die_perror("KVM_SET_MSRS failed"); +} + static void kvm__setup_fpu(struct kvm *self) { self->fpu = (struct kvm_fpu) { @@ -494,6 +541,8 @@ void kvm__reset_vcpu(struct kvm *self) kvm__setup_regs(self); kvm__setup_fpu(self); + + kvm__setup_msrs(self); } void kvm__run(struct kvm *self) -- 2.39.5