Received: by 10.223.164.221 with SMTP id h29csp3779566wrb; Tue, 31 Oct 2017 04:52:09 -0700 (PDT) X-Google-Smtp-Source: ABhQp+T0UcbD/XhCB/RTb2wAqwfEETi7Xd//cdkKDqCgA6qxPKQqKCUPKfIa8CK9/omDYLZ44uVq X-Received: by 10.98.247.26 with SMTP id h26mr1775754pfi.233.1509450729664; Tue, 31 Oct 2017 04:52:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509450729; cv=none; d=google.com; s=arc-20160816; b=DcMrVos2BP1LlzRcXk2DjFvIEzmH0J+8ai4a+0oqrpgC93bas6ztFo6bF76G57fVIF mw2UBShKV0tR3ljbw4VDBD08MbeWZf+2NcMhWFtxtD8eIdpfFIOE+fWLmdhOrSmKn53l PC43ZtEbnfwytibQpnV2u9IzFurb4z+TeTQiXxq56aRyRQYtAwWf+2pZ8uOVtWGLwYgB 3rB4Qnchhrxebu29zJ2aph3pMOUu3CIYB60sm6+TQ1pYrQAuE8+fyB8C8VriYK+WFA+e EFFBABXz7mRDD3FwyCn2Z0zC/cOrYHW3b6Tr39ffKDxAFxfEOzKXbcaRH8szDh9p1YA4 3JPw== 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=vs99j4zyiD1rxK8xLE6o9APlbOKKMFEulzN4NP3l9iY=; b=HRb3LYcDJSMzRa35DmskUWNiJZ/LLl+/NiyZSZ6/08Oufa42uSFvGFJR5R9/7MSuRR f0r+FOJwWFitK2+5wfpAAc1rDJPTA6nqnt4/B11NyJWjnIG9EXkcx7aEhCImYkMQD1d7 mgEAAL2F2zbAP90ibu2ZBjxyPaqto6DVrodSMzpPPx61qh7CapVKeEySJ2bmIfSPSJME Rj9SyGaxVYhxTmEJhJJhC9dNCn7KvP0qPLprb93o0J/hwiZ39y6JTA3bJzIwF4N+NtZG PqIL9Ag0v1H0mlFwqSN2U4rs/7yzLoUSNwXtNiMtjf5RCccMF8OQPJrVk8qlZErliW1N S6Pw== 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 g1si1379176plb.462.2017.10.31.04.51.56; Tue, 31 Oct 2017 04:52:09 -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 S932559AbdJaLuP (ORCPT + 99 others); Tue, 31 Oct 2017 07:50:15 -0400 Received: from mga02.intel.com ([134.134.136.20]:48302 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932296AbdJaLtY (ORCPT ); Tue, 31 Oct 2017 07:49:24 -0400 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 Oct 2017 04:49:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,323,1505804400"; d="scan'208";a="915804352" Received: from vmm.bj.intel.com ([10.238.135.172]) by FMSMGA003.fm.intel.com with ESMTP; 31 Oct 2017 04:49:14 -0700 From: Luwei Kang To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, rkrcmar@redhat.com, tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, x86@kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Luwei Kang Subject: [patch v2 6/8] KVM: x86: Implement Intel processor trace context switch Date: Tue, 31 Oct 2017 06:05:15 +0800 Message-Id: <1509401117-15521-7-git-send-email-luwei.kang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1509401117-15521-1-git-send-email-luwei.kang@intel.com> References: <1509401117-15521-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, other MSRs are loaded/stored manaully. Signed-off-by: Chao Peng Signed-off-by: Luwei Kang --- arch/x86/kvm/vmx.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 7eaf774..091120e 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2131,6 +2131,61 @@ static unsigned long segment_base(u16 selector) } #endif +static inline void pt_load_msr(struct pt_ctx *ctx, unsigned int addr_num) +{ + 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 < addr_num; i++) + wrmsrl(MSR_IA32_RTIT_ADDR0_A + i, ctx->addrs[i]); +} + +static inline void pt_save_msr(struct pt_ctx *ctx, unsigned int addr_num) +{ + 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 < addr_num; i++) + rdmsrl(MSR_IA32_RTIT_ADDR0_A + i, ctx->addrs[i]); +} + +static void pt_guest_enter(struct vcpu_vmx *vmx) +{ + u64 ctl; + + if (pt_mode == PT_MODE_HOST || pt_mode == PT_MODE_HOST_GUEST) { + rdmsrl(MSR_IA32_RTIT_CTL, ctl); + vmx->pt_desc.host.ctl = ctl; + if (ctl & RTIT_CTL_TRACEEN) { + ctl &= ~RTIT_CTL_TRACEEN; + wrmsrl(MSR_IA32_RTIT_CTL, ctl); + } + } + + if (pt_mode == PT_MODE_HOST_GUEST) { + pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_num); + pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_num); + } +} + +static void pt_guest_exit(struct vcpu_vmx *vmx) +{ + if (pt_mode == PT_MODE_HOST_GUEST) { + pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_num); + pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_num); + wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); + } + + if (pt_mode == PT_MODE_HOST) + 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); @@ -5618,6 +5673,13 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx) vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); } + if (pt_mode == PT_MODE_HOST_GUEST) { + u32 eax, ebx, ecx, edx; + + cpuid_count(0x14, 1, &eax, &ebx, &ecx, &edx); + vmx->pt_desc.addr_num = eax & 0x7; + } + return 0; } @@ -5712,6 +5774,15 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) update_exception_bitmap(vcpu); vpid_sync_context(vmx->vpid); + + if (pt_mode == PT_MODE_HOST_GUEST) { + memset(&vmx->pt_desc.host, 0, sizeof(vmx->pt_desc.host)); + memset(&vmx->pt_desc.guest, 0, sizeof(vmx->pt_desc.guest)); + /* Bit[6~0] are forced to 1, writes are ignored. */ + vmx->pt_desc.guest.output_mask = 0x7F; + /* Clear the status of IA32_RTIT_CTL in VMCS guest state. */ + vmcs_write32(GUEST_IA32_RTIT_CTL, 0); + } } /* @@ -9382,6 +9453,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); debugctlmsr = get_debugctlmsr(); @@ -9517,6 +9590,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 From 1583986286760274828@xxx Mon Nov 13 21:03:59 +0000 2017 X-GM-THRID: 1583986286760274828 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread