Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp124130ybi; Fri, 24 May 2019 01:01:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqwTtXeP21y2tiOMCH+LYY6CJND7iLbSIQ+D2HGf0N5CgKtzl9vGP4Ov+4no2ezNs6tc5vkL X-Received: by 2002:a65:5202:: with SMTP id o2mr477585pgp.199.1558684899447; Fri, 24 May 2019 01:01:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558684899; cv=none; d=google.com; s=arc-20160816; b=onXQl2eb4ytm3iEEeDRCTnVpzn3EPJseReON7JOB4kb8fXabEZl5FQGLAdD5Fm8hut z3SuSSls6N1/7H6etixXQ68eDX3O4pffO4xDcoao09u7XpNRgRZQ4YkIzsuKNnZursEn NnBOBmZl4PFAjqTfuJLlDl0GmznYzbdE2SyzIBRwWMTnuyrRaeHdxQ1pzwNExghadIEO pSRN4HFb+XUHf4UZm8NtmilIdCHQ/VjX43sXh5zuZW/zhzQTZkAFlHoU2n+tPvFwkgMl yujN3QCPifMX4tgr92y1uPGp01iThTdnKmG/TGlwcPRWuFZlYb1esaUoq3TGpcBIFsqH GWRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=9GUCGylEq8DDdv0bo+1iR2Zd70NQ3kGtP+pSpjojyRM=; b=F9diZ7Em5V6u9LbuC7/kQPt3fjLECOg0TItATw4kDDIwjsISm7SExBFt9Y6HuH2wDc WIAJopIdekCEg8ArGHN0/7XoDLWpg+y4PI8nseA8HOWJjEWSoHXAF9wEWjUlT6Hxn9B0 eqPghiN3HXL/i8hsDo68pfmPUcafVg72GloneqWP4JI9RfqwtM1oD73tKfLq7Xm0Yca6 KkWFIwbT1kePuNSplBxOCOZPXIv6+VSU8yAiwzdddSkez1mdkKjXYmQOMQKWY0rB4xkW IOdQ/vH8m9zne2AUSU8uNr3YEPEScbuEkatNIHOjPzyn+jwDecJxTgtv+T8QYhC5M3cb j1AA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n12si2721270plp.114.2019.05.24.01.01.20; Fri, 24 May 2019 01:01:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389298AbfEXH7V (ORCPT + 99 others); Fri, 24 May 2019 03:59:21 -0400 Received: from mga14.intel.com ([192.55.52.115]:62735 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388959AbfEXH7U (ORCPT ); Fri, 24 May 2019 03:59:20 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 May 2019 00:59:19 -0700 X-ExtLoop1: 1 Received: from tao-optiplex-7060.sh.intel.com ([10.239.13.104]) by orsmga008.jf.intel.com with ESMTP; 24 May 2019 00:59:12 -0700 From: Tao Xu To: pbonzini@redhat.com, rkrcmar@redhat.com, corbet@lwn.net, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sean.j.christopherson@intel.com Cc: x86@kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, tao3.xu@intel.com, jingqi.liu@intel.com Subject: [PATCH v2 2/3] KVM: vmx: Emulate MSR IA32_UMWAIT_CONTROL Date: Fri, 24 May 2019 15:56:36 +0800 Message-Id: <20190524075637.29496-3-tao3.xu@intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190524075637.29496-1-tao3.xu@intel.com> References: <20190524075637.29496-1-tao3.xu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org UMWAIT and TPAUSE instructions use IA32_UMWAIT_CONTROL at MSR index E1H to determines the maximum time in TSC-quanta that the processor can reside in either C0.1 or C0.2. This patch is to emulate MSR IA32_UMWAIT_CONTROL in guest and differentiate MSR_TEST_CTL between host and guest. Co-developed-by: Jingqi Liu Signed-off-by: Jingqi Liu Signed-off-by: Tao Xu --- arch/x86/kvm/vmx/vmx.c | 42 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.h | 1 + arch/x86/kvm/x86.c | 1 + 3 files changed, 44 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index a65ee7ea47b4..49e107692aee 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1676,6 +1676,14 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) #endif case MSR_EFER: return kvm_get_msr_common(vcpu, msr_info); + case MSR_IA32_UMWAIT_CONTROL: + if (!kvm_enable_usr_wait_pause(vmx->vcpu.kvm) || + (!msr_info->host_initiated && + !guest_cpuid_has(vcpu, X86_FEATURE_WAITPKG))) + return 1; + + msr_info->data = vmx->msr_ia32_umwait_control; + break; case MSR_IA32_SPEC_CTRL: if (!msr_info->host_initiated && !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) @@ -1838,6 +1846,16 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) return 1; vmcs_write64(GUEST_BNDCFGS, data); break; + case MSR_IA32_UMWAIT_CONTROL: + if (!kvm_enable_usr_wait_pause(vmx->vcpu.kvm) || + !guest_cpuid_has(vcpu, X86_FEATURE_WAITPKG)) + return 1; + + if (!data) + break; + + vmx->msr_ia32_umwait_control = data; + break; case MSR_IA32_SPEC_CTRL: if (!msr_info->host_initiated && !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) @@ -4085,6 +4103,8 @@ static void vmx_vcpu_setup(struct vcpu_vmx *vmx) ++vmx->nmsrs; } + vmx->msr_ia32_umwait_control = 0; + vm_exit_controls_init(vmx, vmx_vmexit_ctrl()); /* 22.2.1, 20.8.1 */ @@ -4123,6 +4143,8 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vmx->rmode.vm86_active = 0; vmx->spec_ctrl = 0; + vmx->msr_ia32_umwait_control = 0; + vcpu->arch.microcode_version = 0x100000000ULL; vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); kvm_set_cr8(vcpu, 0); @@ -6327,6 +6349,24 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) msrs[i].host, false); } +static void atomic_switch_ia32_umwait_control(struct vcpu_vmx *vmx) +{ + u64 host_umwait_control; + + if (!kvm_enable_usr_wait_pause(vmx->vcpu.kvm)) + return; + + if (rdmsrl_safe(MSR_IA32_UMWAIT_CONTROL, &host_umwait_control)) + return; + + if (vmx->msr_ia32_umwait_control != host_umwait_control) + add_atomic_switch_msr(vmx, MSR_IA32_UMWAIT_CONTROL, + vmx->msr_ia32_umwait_control, + host_umwait_control, false); + else + clear_atomic_switch_msr(vmx, MSR_IA32_UMWAIT_CONTROL); +} + static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val) { vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val); @@ -6435,6 +6475,8 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) atomic_switch_perf_msrs(vmx); + atomic_switch_ia32_umwait_control(vmx); + vmx_update_hv_timer(vcpu); /* diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 63d37ccce3dc..7b779f8816fb 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -194,6 +194,7 @@ struct vcpu_vmx { #endif u64 spec_ctrl; + u64 msr_ia32_umwait_control; u32 vm_entry_controls_shadow; u32 vm_exit_controls_shadow; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 38a89c878c5d..245ed4a63765 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1138,6 +1138,7 @@ static u32 msrs_to_save[] = { MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B, MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B, MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B, + MSR_IA32_UMWAIT_CONTROL, }; static unsigned num_msrs_to_save; -- 2.20.1