xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Luwei Kang <luwei.kang@intel.com>
To: xen-devel@lists.xen.org
Cc: kevin.tian@intel.com, sstabellini@kernel.org,
	wei.liu2@citrix.com, jbeulich@suse.com,
	George.Dunlap@eu.citrix.com, andrew.cooper3@citrix.com,
	tim@xen.org, jun.nakajima@intel.com,
	Luwei Kang <luwei.kang@intel.com>
Subject: [PATCH RESEND v1 5/7] x86: Implement Intel Processor Trace context switch
Date: Tue, 16 Jan 2018 02:12:31 +0800	[thread overview]
Message-ID: <1516039953-2988-6-git-send-email-luwei.kang@intel.com> (raw)
In-Reply-To: <1516039953-2988-1-git-send-email-luwei.kang@intel.com>

Load/Store Intel processor trace register in context switch.
MSR IA32_RTIT_CTL is loaded/stored automatically from VMCS.
When Intel PT is supported in guest, we need load/restore
PT MSRs only when PT is enabled in guest.

Signed-off-by: Luwei Kang <luwei.kang@intel.com>
---
 xen/arch/x86/cpu/intel_pt.c        | 69 ++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/vmx/vmx.c         |  4 +++
 xen/include/asm-x86/hvm/vmx/vmcs.h |  2 ++
 xen/include/asm-x86/intel_pt.h     |  4 +++
 4 files changed, 79 insertions(+)

diff --git a/xen/arch/x86/cpu/intel_pt.c b/xen/arch/x86/cpu/intel_pt.c
index 520e0ca..c0e9e68 100644
--- a/xen/arch/x86/cpu/intel_pt.c
+++ b/xen/arch/x86/cpu/intel_pt.c
@@ -21,7 +21,76 @@
 #include <xen/types.h>
 #include <xen/cache.h>
 #include <xen/init.h>
+#include <asm/hvm/vmx/vmx.h>
+#include <asm/intel_pt.h>
 
 /* intel_pt: Flag to enable Intel Processor Trace (default on). */
 bool_t __read_mostly opt_intel_pt = 1;
 boolean_param("intel_pt", opt_intel_pt);
+
+static inline void pt_load_msr(struct pt_ctx *ctx, u32 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->addr[i]);
+}
+
+static inline void pt_save_msr(struct pt_ctx *ctx, u32 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->addr[i]);
+}
+
+void pt_guest_enter(struct vcpu *v)
+{
+    struct pt_desc *pt = &v->arch.hvm_vmx.pt_desc;
+
+    if ( pt->intel_pt_enabled &&
+       (pt->guest_pt_ctx.ctl & MSR_IA32_RTIT_CTL_TRACEEN) )
+        pt_load_msr(&pt->guest_pt_ctx, pt->addr_num);
+}
+
+void pt_guest_exit(struct vcpu *v)
+{
+    struct pt_desc *pt = &v->arch.hvm_vmx.pt_desc;
+
+    if ( pt->intel_pt_enabled &&
+       (pt->guest_pt_ctx.ctl & MSR_IA32_RTIT_CTL_TRACEEN) )
+        pt_save_msr(&pt->guest_pt_ctx, pt->addr_num);
+}
+
+void pt_vcpu_init(struct vcpu *v)
+{
+    struct pt_desc *pt = &v->arch.hvm_vmx.pt_desc;
+    unsigned int eax, ebx, ecx, edx;
+
+    memset(pt, 0, sizeof(struct pt_desc));
+    pt->intel_pt_enabled = false;
+
+    if ( !cpu_has_intel_pt || !opt_intel_pt ||
+         !(v->arch.hvm_vmx.secondary_exec_control & SECONDARY_EXEC_PT_USE_GPA) )
+        return;
+
+    /* get the number of address ranges */
+    if ( cpuid_eax(0x14) == 1 )
+        cpuid_count(0x14, 1, &eax, &ebx, &ecx, &edx);
+    else
+        return;
+
+    pt->addr_num = eax & 0x7;
+    pt->guest_pt_ctx.output_mask = 0x7F;
+    pt->intel_pt_enabled = true;
+
+    vmx_vmcs_enter(v);
+    __vmwrite(GUEST_IA32_RTIT_CTL, 0);
+    vmx_vmcs_exit(v);
+}
diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c
index e036303..f386933 100644
--- a/xen/arch/x86/hvm/vmx/vmx.c
+++ b/xen/arch/x86/hvm/vmx/vmx.c
@@ -467,6 +467,8 @@ static int vmx_vcpu_initialise(struct vcpu *v)
     if ( v->vcpu_id == 0 )
         v->arch.user_regs.rax = 1;
 
+    pt_vcpu_init(v);
+
     return 0;
 }
 
@@ -3513,6 +3515,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     __vmread(GUEST_RSP,    &regs->rsp);
     __vmread(GUEST_RFLAGS, &regs->rflags);
 
+    pt_guest_exit(v);
     hvm_invalidate_regs_fields(regs);
 
     if ( paging_mode_hap(v->domain) )
@@ -4281,6 +4284,7 @@ bool vmx_vmenter_helper(const struct cpu_user_regs *regs)
         }
     }
 
+    pt_guest_enter(curr);
  out:
     if ( unlikely(curr->arch.hvm_vmx.lbr_fixup_enabled) )
         lbr_fixup();
diff --git a/xen/include/asm-x86/hvm/vmx/vmcs.h b/xen/include/asm-x86/hvm/vmx/vmcs.h
index 33ec3e6..46c386f 100644
--- a/xen/include/asm-x86/hvm/vmx/vmcs.h
+++ b/xen/include/asm-x86/hvm/vmx/vmcs.h
@@ -421,6 +421,8 @@ enum vmcs_field {
     GUEST_PDPTE0                    = 0x0000280a,
 #define GUEST_PDPTE(n) (GUEST_PDPTE0 + (n) * 2) /* n = 0...3 */
     GUEST_BNDCFGS                   = 0x00002812,
+    GUEST_IA32_RTIT_CTL             = 0x00002814,
+    GUEST_IA32_RTIT_CTL_HIGH        = 0x00002815,
     HOST_PAT                        = 0x00002c00,
     HOST_EFER                       = 0x00002c02,
     HOST_PERF_GLOBAL_CTRL           = 0x00002c04,
diff --git a/xen/include/asm-x86/intel_pt.h b/xen/include/asm-x86/intel_pt.h
index 909e22f..9505c8f 100644
--- a/xen/include/asm-x86/intel_pt.h
+++ b/xen/include/asm-x86/intel_pt.h
@@ -40,4 +40,8 @@ struct pt_desc {
 
 extern bool_t opt_intel_pt;
 
+void pt_vcpu_init(struct vcpu *v);
+void pt_guest_enter(struct vcpu *v);
+void pt_guest_exit(struct vcpu *v);
+
 #endif /* __ASM_X86_HVM_INTEL_PT_H_ */
-- 
1.8.3.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2018-01-15 18:12 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-15 18:12 [PATCH RESEND v1 0/7] Intel Processor Trace virtulization enabling Luwei Kang
2018-01-15 18:12 ` [PATCH RESEND v1 1/7] x86: add a flag to enable Intel processor trace Luwei Kang
2018-03-09 16:53   ` Wei Liu
2018-03-12  9:25     ` Kang, Luwei
2018-04-26 12:09   ` Wei Liu
2018-04-27  8:22     ` Kang, Luwei
2018-04-27  8:32       ` Wei Liu
2018-04-27 13:03       ` Jan Beulich
2018-04-27 23:16         ` Kang, Luwei
2018-04-26 12:29   ` Jan Beulich
2018-04-27  9:01     ` Kang, Luwei
2018-04-27 12:15       ` Jan Beulich
2018-04-27 23:18         ` Kang, Luwei
2018-01-15 18:12 ` [PATCH RESEND v1 2/7] x86: configure vmcs for Intel processor trace virtualization Luwei Kang
2018-04-26 12:34   ` Jan Beulich
2018-04-28  1:07     ` Kang, Luwei
2018-04-30  7:42       ` Jan Beulich
2018-05-02  7:22         ` Kang, Luwei
2018-05-02  9:09           ` Jan Beulich
2018-05-02  9:22             ` Kang, Luwei
2018-01-15 18:12 ` [PATCH RESEND v1 3/7] x86: add intel proecessor trace support for cpuid Luwei Kang
2018-04-30 15:43   ` Konrad Rzeszutek Wilk
2018-05-02  7:32     ` Kang, Luwei
2018-01-15 18:12 ` [PATCH RESEND v1 4/7] x86: add intel processor trace context Luwei Kang
2018-04-26 12:11   ` Wei Liu
2018-04-26 12:59   ` Jan Beulich
2018-04-28  1:26     ` Kang, Luwei
2018-01-15 18:12 ` Luwei Kang [this message]
2018-04-26 12:11   ` [PATCH RESEND v1 5/7] x86: Implement Intel Processor Trace context switch Wei Liu
2018-04-27  8:53     ` Kang, Luwei
2018-05-02 15:19       ` Wei Liu
2018-05-02 15:43         ` Jan Beulich
2018-05-02 16:15           ` Wei Liu
2018-05-02 16:51             ` Andrew Cooper
2018-05-03  7:27               ` Jan Beulich
2018-05-03  7:26             ` Jan Beulich
2018-05-03  7:51               ` Wei Liu
2018-04-26 13:12   ` Jan Beulich
2018-04-28  2:56     ` Kang, Luwei
2018-01-15 18:12 ` [PATCH RESEND v1 6/7] x86: Implement Intel Processor Trace MSRs read/write Luwei Kang
2018-04-26 13:20   ` Jan Beulich
2018-04-27 12:26   ` Jan Beulich
2018-05-03  5:22     ` Kang, Luwei
2018-05-03  7:33       ` Jan Beulich
2018-05-03  9:40         ` Kang, Luwei
2018-05-03 11:36           ` Jan Beulich
2018-05-04  3:53             ` Kang, Luwei
2018-05-04 12:06               ` Jan Beulich
2018-05-10  9:06                 ` Kang, Luwei
2018-01-15 18:12 ` [PATCH RESEND v1 7/7] x86: Disable Intel Processor Trace when VMXON in L1 guest Luwei Kang
2018-01-16  8:41 ` [PATCH RESEND v1 0/7] Intel Processor Trace virtulization enabling Jan Beulich
2018-01-16  9:02   ` Kang, Luwei
2018-01-16  9:30     ` Jan Beulich
2018-01-16  9:45       ` Kang, Luwei
2018-04-26 12:12 ` Wei Liu
2018-05-03  4:06   ` Kang, Luwei
2018-05-03  5:55     ` Razvan Cojocaru
2018-05-03  8:06     ` Wei Liu
2018-05-04  4:10       ` Kang, Luwei
2018-05-03  9:49   ` Kang, Luwei
2018-05-03 10:01     ` Andrew Cooper
2018-05-04  3:08       ` Kang, Luwei
2018-05-10  9:26       ` Kang, Luwei
2018-05-10  9:56         ` Andrew Cooper
2018-05-15  2:50           ` Kang, Luwei
2018-04-30 15:42 ` Konrad Rzeszutek Wilk
2018-05-02  7:27   ` Kang, Luwei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1516039953-2988-6-git-send-email-luwei.kang@intel.com \
    --to=luwei.kang@intel.com \
    --cc=George.Dunlap@eu.citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=jun.nakajima@intel.com \
    --cc=kevin.tian@intel.com \
    --cc=sstabellini@kernel.org \
    --cc=tim@xen.org \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).