Received: by 10.213.65.68 with SMTP id h4csp866162imn; Tue, 20 Mar 2018 18:15:27 -0700 (PDT) X-Google-Smtp-Source: AG47ELuo3LT+fgiSUnHED6CVqBAhgdJskioynr+DxG31FOgS5qmkmzehdiaQaMjUOIZ+eLc8smb6 X-Received: by 2002:a17:902:28c3:: with SMTP id f61-v6mr18751250plb.346.1521594927141; Tue, 20 Mar 2018 18:15:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521594927; cv=none; d=google.com; s=arc-20160816; b=RSwhwq2Xz+dVdOxVc4SX3hTp19e6jYxD9xS5IsZ9k1CH89yJ4nG1DKh6jCn+0o9Js3 mBbwAfY+Bjoa3Ci+ZpgsjWQxn8nebpY1QceFM5DCxeGA4+FDSW1yn8hEkByvT0zCDwuW I+bt8XW9ihKy+PRDdRxfrMCHTeF91tpwJRYblb7ajyuOmE/FPOiNxhUhfLW/Ut/Fjl3H SSGvEMjvSklNcSVb0U9+/Ah7zCr5gwzc2+3Faj1dgwTzRGdJNjhUhcvTYCdXaCHAurGl XrBsbgPZa2EhXMtPlHEkQ0zzIUxBdVJU7OE22guzSvNiN9hO5BdAk3RJVpkpVgespFnZ ke4g== 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:arc-authentication-results; bh=jkLrtAch6nMQIOpIgFEqjSx/aobZezDSuDVwe59WMhI=; b=NSCi5g77eqnbMS+2B/1rRDH8k3UMcBz6jvAmCX4bSaxCKCxVYzI9b1gPFw1LCWluHN ynmMJXEUy7ddKnTM599pIxBj7UNdo+qCyzm0wRh5VF285Jn9NlwhNFsIikAp+rGQrPY6 s6/X4L9T5y7nKGnS+eeTDu9+XCJi6Omw/RmRGU192ONyhNpm0G7FZWdbl6lIdZ607hTF fpHcMS+2zaZKOHu1zB22J61URv1O+jOWRXvf2o1FgdWsJAb9u7Osg867eC6Ra27EtudP DVNcWLyBREkR+adKufSaver45bnnUp7gVYKbzkYSj3+//3NwnHdVvAsl/k3SaA+G+GBx wtLw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i127si2179901pfc.148.2018.03.20.18.15.13; Tue, 20 Mar 2018 18:15:27 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752090AbeCUBN5 (ORCPT + 99 others); Tue, 20 Mar 2018 21:13:57 -0400 Received: from mga09.intel.com ([134.134.136.24]:19664 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751798AbeCUBLX (ORCPT ); Tue, 20 Mar 2018 21:11:23 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Mar 2018 18:11:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,338,1517904000"; d="scan'208";a="39779377" Received: from vmm.bj.intel.com ([10.238.135.172]) by fmsmga001.fm.intel.com with ESMTP; 20 Mar 2018 18:11:21 -0700 From: Luwei Kang To: kvm@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, x86@kernel.org, pbonzini@redhat.com, rkrcmar@redhat.com, linux-kernel@vger.kernel.org, joro@8bytes.org, Chao Peng , Luwei Kang Subject: [PATCH v6 07/11] KVM: x86: Implement Intel Processor Trace context switch Date: Tue, 20 Mar 2018 19:21:54 +0800 Message-Id: <1521544918-31084-8-git-send-email-luwei.kang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1521544918-31084-1-git-send-email-luwei.kang@intel.com> References: <1521544918-31084-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 From: Chao Peng Load/Store Intel processor trace register in context switch. MSR IA32_RTIT_CTL is loaded/stored automatically from VMCS. In HOST mode, we just need to restore the status of IA32_RTIT_CTL. In HOST_GUEST mode, we need load/resore PT MSRs only when PT is enabled in guest. Signed-off-by: Chao Peng Signed-off-by: Luwei Kang --- arch/x86/kvm/vmx.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 940df0e..34bcb30 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2187,6 +2187,55 @@ static unsigned long segment_base(u16 selector) } #endif +static inline void pt_load_msr(struct pt_ctx *ctx, u32 range_cnt) +{ + u32 i; + + wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status); + wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); + wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); + wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); + for (i = 0; i < range_cnt; i++) + wrmsrl(MSR_IA32_RTIT_ADDR0_A + i, ctx->addrs[i]); +} + +static inline void pt_save_msr(struct pt_ctx *ctx, u32 range_cnt) +{ + u32 i; + + rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status); + rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); + rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); + rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); + for (i = 0; i < range_cnt; i++) + rdmsrl(MSR_IA32_RTIT_ADDR0_A + i, ctx->addrs[i]); +} + +static void pt_guest_enter(struct vcpu_vmx *vmx) +{ + if (pt_mode == PT_MODE_HOST || pt_mode == PT_MODE_HOST_GUEST) + rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); + + if (pt_mode == PT_MODE_HOST_GUEST && + vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { + wrmsrl(MSR_IA32_RTIT_CTL, 0); + pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.range_cnt); + pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.range_cnt); + } +} + +static void pt_guest_exit(struct vcpu_vmx *vmx) +{ + if (pt_mode == PT_MODE_HOST_GUEST && + vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { + pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.range_cnt); + pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.range_cnt); + } + + if (pt_mode == PT_MODE_HOST || pt_mode == PT_MODE_HOST_GUEST) + wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); +} + static void vmx_save_host_state(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); @@ -5912,6 +5961,13 @@ static void vmx_vcpu_setup(struct vcpu_vmx *vmx) vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); } + + if (pt_mode == PT_MODE_HOST_GUEST) { + memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc)); + /* Bit[6~0] are forced to 1, writes are ignored. */ + vmx->pt_desc.guest.output_mask = 0x7F; + vmcs_write64(GUEST_IA32_RTIT_CTL, 0); + } } static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) @@ -9632,6 +9688,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.pkru != vmx->host_pkru) __write_pkru(vcpu->arch.pkru); + pt_guest_enter(vmx); + atomic_switch_perf_msrs(vmx); vmx_arm_hv_timer(vcpu); @@ -9811,6 +9869,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) | (1 << VCPU_EXREG_CR3)); vcpu->arch.regs_dirty = 0; + pt_guest_exit(vmx); + /* * eager fpu is enabled if PKEY is supported and CR4 is switched * back on host, so it is safe to read guest PKRU from current -- 1.8.3.1