From: Joerg Roedel Subject: [PATCH 1/6] KVM: SVM: Implement infrastructure for TSC_RATE_MSR Date: Thu, 24 Mar 2011 08:40:19 +0100 Message-ID: <1300952424-32014-2-git-send-email-joerg.roedel@amd.com> References: <1300952424-32014-1-git-send-email-joerg.roedel@amd.com> Mime-Version: 1.0 Content-Type: text/plain Cc: Zachary Amsden , , Joerg Roedel To: Avi Kivity , Marcelo Tosatti Return-path: Received: from ch1outboundpool.messaging.microsoft.com ([216.32.181.186]:23635 "EHLO ch1outboundpool.messaging.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933338Ab1CXHkg (ORCPT ); Thu, 24 Mar 2011 03:40:36 -0400 In-Reply-To: <1300952424-32014-1-git-send-email-joerg.roedel@amd.com> Sender: kvm-owner@vger.kernel.org List-ID: This patch enhances the kvm_amd module with functions to support the TSC_RATE_MSR which can be used to set a given tsc frequency for the guest vcpu. Signed-off-by: Joerg Roedel --- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/svm.c | 44 +++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletions(-) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index fd5a1f3..a7b3e40 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -114,6 +114,7 @@ complete list. */ #define MSR_AMD64_PATCH_LEVEL 0x0000008b +#define MSR_AMD64_TSC_RATIO 0xc0000104 #define MSR_AMD64_NB_CFG 0xc001001f #define MSR_AMD64_PATCH_LOADER 0xc0010020 #define MSR_AMD64_OSVW_ID_LENGTH 0xc0010140 diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 2a19322..4e5da50 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -63,6 +63,8 @@ MODULE_LICENSE("GPL"); #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) +#define TSC_RATIO_RSVD 0xffffff0000000000ULL + static bool erratum_383_found __read_mostly; static const u32 host_save_user_msrs[] = { @@ -144,6 +146,12 @@ struct vcpu_svm { unsigned int3_injected; unsigned long int3_rip; u32 apf_reason; + + struct { + bool enabled; + u64 ratio; + } tsc_scale; + }; #define MSR_INVALID 0xffffffffU @@ -854,6 +862,32 @@ static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) seg->base = 0; } +static u64 __scale_tsc(u64 ratio, u64 tsc) +{ + u64 mult, frac, _tsc; + + mult = ratio >> 32; + frac = ratio & ((1ULL << 32) - 1); + + _tsc = tsc; + _tsc *= mult; + _tsc += (tsc >> 32) * frac; + _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32; + + return _tsc; +} + +static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) +{ + struct vcpu_svm *svm = to_svm(vcpu); + u64 _tsc = tsc; + + if (svm->tsc_scale.enabled) + _tsc = __scale_tsc(svm->tsc_scale.ratio, tsc); + + return _tsc; +} + static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) { struct vcpu_svm *svm = to_svm(vcpu); @@ -1141,6 +1175,9 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); + + if (static_cpu_has(X86_FEATURE_TSCRATEMSR) && svm->tsc_scale.enabled) + wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_scale.ratio); } static void svm_vcpu_put(struct kvm_vcpu *vcpu) @@ -1161,6 +1198,9 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu) #endif for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); + + if (static_cpu_has(X86_FEATURE_TSCRATEMSR) && svm->tsc_scale.enabled) + wrmsr(MSR_AMD64_TSC_RATIO, 0, 1); } static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) @@ -2813,7 +2853,9 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data) case MSR_IA32_TSC: { struct vmcb *vmcb = get_host_vmcb(svm); - *data = vmcb->control.tsc_offset + native_read_tsc(); + *data = vmcb->control.tsc_offset + + svm_scale_tsc(vcpu, native_read_tsc()); + break; } case MSR_STAR: -- 1.7.1