Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp339151yba; Thu, 16 May 2019 01:29:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqxs5my1TtT57zd7l7p6e2+uaa9BEjYZxFti9bZTZ+d3Grgl3TubetddbWwfgg2YUudxfmMJ X-Received: by 2002:a17:902:b108:: with SMTP id q8mr43721702plr.110.1557995375226; Thu, 16 May 2019 01:29:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557995375; cv=none; d=google.com; s=arc-20160816; b=efWFWD6Oq2CQPcBv8neLa+S4rLdpiTWLPE5kFC488yAvDihguysMJyhuD6MYuwYst/ kRxwwDEFocCEQms7atEulns7CS7Rxga0SdshwnMF5V8nC1PsAa2ak59CidIE5NxBm9rS u3Tfy3LPm0n0r7aHvhoMZzWGYSRiBp1PR1NnS6P7oEVQERACkYHynvUTQpD3MszqeFwc OBbdkF97mArVCAJhYUx0d7Xu2vvnq5tHhp/4mTR4KuFdaItYdEP/KdU3cnhtPauBi5h+ ZC554rJEfusHnx3mTT5h+ubkzpnr86cPPCHjgNugpfgtu1+pp9eWCTqAMJg3mdCvFd2C Eh/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=RGbLomoXAs+JgVfXUmr/YXpcxjqJ8USAcpDlvPhN/tc=; b=0g6CWStARdYjNqdbMJ+Dvm+ZjwmHsyfkYVH6JEDQ2oVeNVRDJpIoR5ZsOMjTmn1m58 enhFIhaKJnHl7oCxP1z5ljZ1sJzIrOsffzd3MdL46Arm2LJhneV0UTVHw55eQz6xT9SF +9Cz88qA6NS8HGKKIEaM7/DsfAI0adh2riDom6PPtLbHSOWfbCbjAlf2fJyVYzPse5ic Z8+kMk16RosIc/zjffmTJTCQk6Qz2e0SnwXtl/dC/jtg4ifIBmFy4nRHonGjQtqKZgWn fJ+tjLJu9nSztZKdKZ3q3sbVH2pw54s4ydvesJo2y68+4l1gy+Igk9qBM+X7+sHy8kAM x72w== 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 a3si4502573pff.117.2019.05.16.01.29.20; Thu, 16 May 2019 01:29:35 -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 S1726977AbfEPI04 (ORCPT + 99 others); Thu, 16 May 2019 04:26:56 -0400 Received: from mga14.intel.com ([192.55.52.115]:24486 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726918AbfEPI0y (ORCPT ); Thu, 16 May 2019 04:26:54 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 May 2019 01:26:54 -0700 X-ExtLoop1: 1 Received: from skl-s2.bj.intel.com ([10.240.192.102]) by orsmga005.jf.intel.com with ESMTP; 16 May 2019 01:26:51 -0700 From: Luwei Kang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, x86@kernel.org, pbonzini@redhat.com, rkrcmar@redhat.com, Luwei Kang Subject: [PATCH v1 5/6] KVM: VMX: Intel PT configration context switch using XSAVES/XRSTORS Date: Thu, 16 May 2019 16:25:13 +0800 Message-Id: <1557995114-21629-6-git-send-email-luwei.kang@intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1557995114-21629-1-git-send-email-luwei.kang@intel.com> References: <1557995114-21629-1-git-send-email-luwei.kang@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch add the support of using XSAVES/XRSTORS to do the Intel processor trace context switch. Because of native driver didn't set the XSS[bit8] to enabled the PT state in xsave area, so this patch only set this bit before XSAVE/XRSTORS intstuction executtion and restore the original value after. The flag "initialized" need to be cleared when PT is change from enabled to disabled. Guest may modify PT MSRs when PT is disabled and they are only saved in variables. We need to reload these value to HW manual when PT is enabled. Signed-off-by: Luwei Kang --- arch/x86/kvm/vmx/vmx.c | 80 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 4691665..d323e6b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1002,33 +1002,83 @@ static inline void pt_save_msr(struct pt_state *ctx, u32 addr_range) static void pt_guest_enter(struct vcpu_vmx *vmx) { + struct pt_desc *desc; + int err; + if (pt_mode == PT_MODE_SYSTEM) return; - /* - * GUEST_IA32_RTIT_CTL is already set in the VMCS. - * Save host state before VM entry. - */ - rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc->host_ctx->rtit_ctl); - if (vmx->pt_desc->guest_ctx->rtit_ctl & RTIT_CTL_TRACEEN) { - wrmsrl(MSR_IA32_RTIT_CTL, 0); - pt_save_msr(vmx->pt_desc->host_ctx, vmx->pt_desc->addr_range); - pt_load_msr(vmx->pt_desc->guest_ctx, vmx->pt_desc->addr_range); + desc = vmx->pt_desc; + + rdmsrl(MSR_IA32_RTIT_CTL, desc->host_ctx->rtit_ctl); + + if (desc->guest_ctx->rtit_ctl & RTIT_CTL_TRACEEN) { + if (likely(desc->pt_xsave)) { + wrmsrl(MSR_IA32_XSS, host_xss | XFEATURE_MASK_PT); + /* + * XSAVES instruction will clears the TeaceEn after + * saving the value of RTIT_CTL and before saving any + * other PT state. + */ + XSTATE_XSAVE(&desc->host_xs->state.xsave, + XFEATURE_MASK_PT, 0, err); + /* + * Still need to load the guest PT state manual if + * PT stste not populated in xsave area. + */ + if (desc->guest_xs->initialized) + XSTATE_XRESTORE(&desc->guest_xs->state.xsave, + XFEATURE_MASK_PT, 0); + else + pt_load_msr(desc->guest_ctx, desc->addr_range); + + wrmsrl(MSR_IA32_XSS, host_xss); + } else { + if (desc->host_ctx->rtit_ctl & RTIT_CTL_TRACEEN) + wrmsrl(MSR_IA32_RTIT_CTL, 0); + + pt_save_msr(desc->host_ctx, desc->addr_range); + pt_load_msr(desc->guest_ctx, desc->addr_range); + } } } static void pt_guest_exit(struct vcpu_vmx *vmx) { + struct pt_desc *desc; + int err; + if (pt_mode == PT_MODE_SYSTEM) return; - if (vmx->pt_desc->guest_ctx->rtit_ctl & RTIT_CTL_TRACEEN) { - pt_save_msr(vmx->pt_desc->guest_ctx, vmx->pt_desc->addr_range); - pt_load_msr(vmx->pt_desc->host_ctx, vmx->pt_desc->addr_range); - } + desc = vmx->pt_desc; + + if (desc->guest_ctx->rtit_ctl & RTIT_CTL_TRACEEN) { + if (likely(desc->pt_xsave)) { + wrmsrl(MSR_IA32_XSS, host_xss | XFEATURE_MASK_PT); + /* + * Save guest state. TraceEn is 0 before and after + * XSAVES instruction because RTIT_CTL will be cleared + * on VM-exit (VM Exit control bit25). + */ + XSTATE_XSAVE(&desc->guest_xs->state.xsave, + XFEATURE_MASK_PT, 0, err); + desc->guest_xs->initialized = 1; + /* + * Resume host PT state and PT may enabled after this + * instruction if host PT is enabled before VM-entry. + */ + XSTATE_XRESTORE(&desc->host_xs->state.xsave, + XFEATURE_MASK_PT, 0); + wrmsrl(MSR_IA32_XSS, host_xss); + } else { + pt_save_msr(desc->guest_ctx, desc->addr_range); + pt_load_msr(desc->host_ctx, desc->addr_range); - /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */ - wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc->host_ctx->rtit_ctl); + wrmsrl(MSR_IA32_RTIT_CTL, desc->host_ctx->rtit_ctl); + } + } else if (desc->host_ctx->rtit_ctl & RTIT_CTL_TRACEEN) + wrmsrl(MSR_IA32_RTIT_CTL, desc->host_ctx->rtit_ctl); } static int pt_init(struct vcpu_vmx *vmx) -- 1.8.3.1