Skip to contentSkip to conversation

V2 vpmu: Add the BTS extension

PATCH In review Dietmar Hahn 2 participants 7 messages Mar 7, 2012 – Mar 16, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 V2 vpmu: Add the BTS extension

Add the BTS functionality to the existing vpmu implementation for Intel cpus.

 xen/arch/x86/hvm/svm/vpmu.c              |    9 +-
 xen/arch/x86/hvm/vmx/vmx.c               |   12 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c        |  181 +++++++++++++++++++++++++-----
 xen/arch/x86/hvm/vpmu.c                  |   37 +++++-
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |    1 +
 xen/include/asm-x86/hvm/vpmu.h           |   16 ++-
 xen/include/asm-x86/msr-index.h          |    5 +
 7 files changed, 216 insertions(+), 45 deletions(-)

# HG changeset patch
# User Dietmar Hahn <[email protected]> # Date 1331111599 -3600
# Node ID 639568e24ab6bbf6a865191168092b1e84902584 # Parent 313da9e9c1cc230a827d9239f7b8deb9f35c2aeb [mq]: patch_vpmu_bts_V2.diff

xen/arch/x86/hvm/svm/vpmu.c +6 -3
diff -r 313da9e9c1cc -r 639568e24ab6 xen/arch/x86/hvm/svm/vpmu.c
@@ -363,10 +363,11 @@ struct arch_vpmu_ops amd_vpmu_ops = {
     .arch_vpmu_load = amd_vpmu_restore
 };
 
-int svm_vpmu_initialise(struct vcpu *v)
+int svm_vpmu_initialise(struct vcpu *v, int vpmu_flags /* unused */)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     uint8_t family = current_cpu_data.x86;
+    int ret = 0;
 
     switch ( family )
     {
@@ -374,8 +375,10 @@ int svm_vpmu_initialise(struct vcpu *v)
     case 0x12:
     case 0x14:
     case 0x15:
-        vpmu->arch_vpmu_ops = &amd_vpmu_ops;
-        return amd_vpmu_initialise(v);
+        ret = amd_vpmu_initialise(v);
+        if ( !ret )
+            vpmu->arch_vpmu_ops = &amd_vpmu_ops;
+        return ret;
     }
 
     printk("VPMU: Initialization failed. "
xen/arch/x86/hvm/vmx/vmx.c +10 -2
diff -r 313da9e9c1cc -r 639568e24ab6 xen/arch/x86/hvm/vmx/vmx.c
@@ -1833,6 +1833,9 @@ static int vmx_msr_read_intercept(unsign
         /* Debug Trace Store is not supported. */
         *msr_content |= MSR_IA32_MISC_ENABLE_BTS_UNAVAIL |
                        MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL;
+        /* Perhaps vpmu will change some bits. */
+        if ( vpmu_do_rdmsr(msr, msr_content) )
+            goto done;
         break;
     default:
         if ( vpmu_do_rdmsr(msr, msr_content) )
@@ -1960,9 +1963,14 @@ static int vmx_msr_write_intercept(unsig
         int i, rc = 0;
         uint64_t supported = IA32_DEBUGCTLMSR_LBR | IA32_DEBUGCTLMSR_BTF;
 
-        if ( !msr_content || (msr_content & ~supported) )
+        if ( !msr_content )
             break;
-
+        if ( msr_content & ~supported )
+        {
+            /* Perhaps some other bits are supported in vpmu. */
+            if ( !vpmu_do_wrmsr(msr, msr_content) )
+                break;
+        }
         if ( msr_content & IA32_DEBUGCTLMSR_LBR )
         {
             const struct lbr_info *lbr = last_branch_msr_get();
xen/arch/x86/hvm/vmx/vpmu_core2.c +147 -34

257 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +33 -4
diff -r 313da9e9c1cc -r 639568e24ab6 xen/arch/x86/hvm/vpmu.c
@@ -32,8 +32,37 @@
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 
-static bool_t __read_mostly opt_vpmu_enabled;
-boolean_param("vpmu", opt_vpmu_enabled);
+
+/*
+ * "vpmu" :     vpmu generally enabled
+ * "vpmu=off" : vpmu generally disabled
+ * "vpmu=bts" : vpmu enabled and Intel BTS feature switched on.
+ */
+static int __read_mostly opt_vpmu_enabled;
+static void parse_vpmu_param(char *s);
+custom_param("vpmu", parse_vpmu_param);
+
+static void __init parse_vpmu_param(char *s)
+{
+
+    if ( !parse_bool(s) )
+    {
+        opt_vpmu_enabled = 0;
+    }
+    else
+    {
+        opt_vpmu_enabled |= VPMU_BOOT_ENABLED;
+        if ( !strcmp(s, "bts") )
+        {
+            opt_vpmu_enabled |= VPMU_BOOT_BTS;
+        }
+        else
+        {
+            printk("VPMU: unknown vpmu flag: %s - vpmu disabled!\n", s);
+            opt_vpmu_enabled = 0;
+        }
+    }
+}
 
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
@@ -104,12 +133,12 @@ void vpmu_initialise(struct vcpu *v)
     switch ( vendor )
     {
     case X86_VENDOR_AMD:
-        if ( svm_vpmu_initialise(v) != 0 )
+        if ( svm_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
             opt_vpmu_enabled = 0;
         break;
 
     case X86_VENDOR_INTEL:
-        if ( vmx_vpmu_initialise(v) != 0 )
+        if ( vmx_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
             opt_vpmu_enabled = 0;
         break;
 
xen/include/asm-x86/hvm/vmx/vpmu_core2.h +1 -0
diff -r 313da9e9c1cc -r 639568e24ab6 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
@@ -29,6 +29,7 @@ struct arch_msr_pair {
 };
 
 struct core2_pmu_enable {
+    char ds_area_enable;
     char fixed_ctr_enable[3];
     char arch_pmc_enable[1];
 };
xen/include/asm-x86/hvm/vpmu.h +14 -2
diff -r 313da9e9c1cc -r 639568e24ab6 xen/include/asm-x86/hvm/vpmu.h
@@ -22,6 +22,15 @@
 #ifndef __ASM_X86_HVM_VPMU_H_
 #define __ASM_X86_HVM_VPMU_H_
 
+
+/*
+ * Flag bits given as a string on the hypervisor boot parameter 'vpmu'.
+ * See arch/x86/hvm/vpmu.c.
+ */
+#define VPMU_BOOT_ENABLED 0x1    /* vpmu generally enabled. */
+#define VPMU_BOOT_BTS     0x2    /* Intel BTS feature wanted. */
+
+
 #define msraddr_to_bitpos(x) (((x)&0xffff) + ((x)>>31)*0x2000)
 #define vcpu_vpmu(vcpu)   (&((vcpu)->arch.hvm_vcpu.vpmu))
 #define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, \
@@ -48,8 +57,8 @@ struct arch_vpmu_ops {
     void (*arch_vpmu_load)(struct vcpu *v);
 };
 
-int vmx_vpmu_initialise(struct vcpu *v);
-int svm_vpmu_initialise(struct vcpu *v);
+int vmx_vpmu_initialise(struct vcpu *v, int vpmu_flags);
+int svm_vpmu_initialise(struct vcpu *v, int vpmu_flags);
 
 struct vpmu_struct {
     u32 flags;
@@ -61,6 +70,9 @@ struct vpmu_struct {
 #define VPMU_CONTEXT_LOADED                 0x2
 #define VPMU_RUNNING                        0x4
 #define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x8
+#define VPMU_CPU_HAS_DS                     0x10 /* Has Debug Store */
+#define VPMU_CPU_HAS_BTS                    0x20 /* Has Branche Trace Store */
+
 
 #define vpmu_set(_vpmu, _x)    ((_vpmu)->flags |= (_x))
 #define vpmu_reset(_vpmu, _x)  ((_vpmu)->flags &= ~(_x))
xen/include/asm-x86/msr-index.h +5 -0
diff -r 313da9e9c1cc -r 639568e24ab6 xen/include/asm-x86/msr-index.h
@@ -67,6 +67,11 @@
 #define MSR_IA32_DEBUGCTLMSR		0x000001d9
 #define IA32_DEBUGCTLMSR_LBR		(1<<0) /* Last Branch Record */
 #define IA32_DEBUGCTLMSR_BTF		(1<<1) /* Single Step on Branches */
+#define IA32_DEBUGCTLMSR_TR		(1<<6) /* Trace Message Enable */
+#define IA32_DEBUGCTLMSR_BTS		(1<<7) /* Branch Trace Store */
+#define IA32_DEBUGCTLMSR_BTINT		(1<<8) /* Branch Trace Interrupt */
+#define IA32_DEBUGCTLMSR_BTS_OFF_OS	(1<<9)  /* BTS off if CPL 0 */
+#define IA32_DEBUGCTLMSR_BTS_OFF_USR	(1<<10) /* BTS off if CPL > 0 */
 
 #define MSR_IA32_LASTBRANCHFROMIP	0x000001db
 #define MSR_IA32_LASTBRANCHTOIP		0x000001dc

--
Company details: http://ts.fujitsu.com/imprint.html

#1 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Dietmar Hahn

Add the BTS functionality to the existing vpmu implementation for Intel cpus.

I was about to get this committed, but I noticed some inconsistency:

Quoted 8 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -2
@@ -425,8 +449,26 @@ static int core2_vpmu_do_wrmsr(unsigned 
                      "which is not supported.\n");
         return 1;
     case MSR_IA32_DS_AREA:
-        gdprintk(XENLOG_WARNING, "Guest setting of DTS is ignored.\n");
-        return 1;
+        if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) )
+        {

xen/arch/x86/hvm/vmx/vpmu_core2.c static int core2_vpmu_do_wrmsr(unsigned · line 463

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +10 -0
+            if (!msr_content || !is_canonical_address(msr_content))
+            {
+                gdprintk(XENLOG_WARNING, "Illegal address for IA32_DS_AREA: 0x%lx\n",
+                                                            msr_content);
+                vmx_inject_hw_exception(TRAP_gp_fault, 0);
+                return 1;
+            }
+            else
+            {
+                core2_vpmu_cxt->pmu_enable->ds_area_enable = msr_content ? 1 : 0;

In the if() above you already exclude msr_content == 0, so why the conditional here? Or is it the other way around - the conditional here is correct, and the respective part of the if() clause above is wrong?

As I did already make a few other cleanup adjustments to your patch, please just let me know.

Jan

Quoted 11 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +8 -0
+                break;
+            }
+        }
+        else
+        {
+            gdprintk(XENLOG_WARNING, "Guest setting of DTS is ignored.\n");
+            return 1;
+        }
     case MSR_CORE_PERF_GLOBAL_CTRL:
         global_ctrl = msr_content;
         for ( i = 0; i < core2_get_pmc_count(); i++ )
#2 lore raw

Am Dienstag 13 März 2012, 14:22:45 schrieb Jan Beulich:

Quoted 32 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Dietmar Hahn

Add the BTS functionality to the existing vpmu implementation for Intel cpus.

I was about to get this committed, but I noticed some inconsistency:

Quoted 8 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -2
@@ -425,8 +449,26 @@ static int core2_vpmu_do_wrmsr(unsigned 
                      "which is not supported.\n");
         return 1;
     case MSR_IA32_DS_AREA:
-        gdprintk(XENLOG_WARNING, "Guest setting of DTS is ignored.\n");
-        return 1;
+        if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) )
+        {

xen/arch/x86/hvm/vmx/vpmu_core2.c static int core2_vpmu_do_wrmsr(unsigned · line 463

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +10 -0
+            if (!msr_content || !is_canonical_address(msr_content))
+            {
+                gdprintk(XENLOG_WARNING, "Illegal address for IA32_DS_AREA: 0x%lx\n",
+                                                            msr_content);
+                vmx_inject_hw_exception(TRAP_gp_fault, 0);
+                return 1;
+            }
+            else
+            {
+                core2_vpmu_cxt->pmu_enable->ds_area_enable = msr_content ? 1 : 0;

In the if() above you already exclude msr_content == 0, so why the conditional here? Or is it the other way around - the conditional here is correct, and the respective part of the if() clause above is wrong?

Quoted 2 lines

As I did already make a few other cleanup adjustments to your patch, please just let me know.

You are right. I'am always impressed how you guys looked at the code to see such things ;-)
OK I think it's remained from my experiments with valid addresses. As in my tests 0x0 can be used to write to the MSR_IA32_DS_AREA the check of !msr_content in the if (... can be removed so it's possible to write 0x0 into the msr.
But this worked only on boxes I was able to test ...
Thanks!

Dietmar.

Quoted 14 lines

Jan

Quoted 11 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +8 -0
+                break;
+            }
+        }
+        else
+        {
+            gdprintk(XENLOG_WARNING, "Guest setting of DTS is ignored.\n");
+            return 1;
+        }
     case MSR_CORE_PERF_GLOBAL_CTRL:
         global_ctrl = msr_content;
         for ( i = 0; i < core2_get_pmc_count(); i++ )
Signature · 1 line
Company details: http://ts.fujitsu.com/imprint.html
#3 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Dietmar Hahn

Am Dienstag 13 März 2012, 14:22:45 schrieb Jan Beulich:

Quoted 2 lines

As I did already make a few other cleanup adjustments to your patch, please just let me know.

You are right. I'am always impressed how you guys looked at the code to see such things ;-)
OK I think it's remained from my experiments with valid addresses. As in my tests 0x0 can be used to write to the MSR_IA32_DS_AREA the check of !msr_content in the if (... can be removed so it's possible to write 0x0 into the msr.

Committed both patches, but please take a look since - as indicated - I made a few adjustments.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw

Am Donnerstag 15 März 2012, 14:15:03 schrieb Jan Beulich:

Quoted 3 lines

Committed both patches, but please take a look since - as indicated - I made a few adjustments.

Thanks for the adjustments Jan, works very well!
Only a small fix for console logging beauty ;-) :

xen/arch/x86/hvm/vmx/vpmu_core2.c Fri Mar 16 09:40:16 2012 +0100 renamed from xen/arch/x86/hvm/vmx/vpmu_core2.c Thu Mar 15 15:20:37 2012 +0000 +1 -1
diff -r 1b68427875f7 xen/arch/x86/hvm/vmx/vpmu_core2.c
@@ -690,7 +690,7 @@ static int core2_vpmu_initialise(struct 
             printk("******************************************************\n");
             printk("** WARNING: Emulation of BTS Feature is switched on **\n");
             printk("** Using this processor feature in a virtualized    **\n");
-            printk("** environment is not 100%% safe.                   **\n");
+            printk("** environment is not 100%% safe.                    **\n");
             printk("** Setting the DS buffer address with wrong values  **\n");
             printk("** may lead to hypervisor hangs or crashes.         **\n");
             printk("** It is NOT recommended for production use!        **\n");

Dietmar.

Quoted 2 lines

Jan

Signature · 6 lines
Company details: http://ts.fujitsu.com/imprint.html

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#5 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Dietmar Hahn

Am Donnerstag 15 März 2012, 14:15:03 schrieb Jan Beulich:

Quoted 3 lines

Committed both patches, but please take a look since - as indicated - I made a few adjustments.

Thanks for the adjustments Jan, works very well!
Only a small fix for console logging beauty ;-) :

Thanks. I took the liberty to add you S-o-b on this small a fix without you explicit stating so.

Jan

Quoted 21 lines

diff -r 1b68427875f7 xen/arch/x86/hvm/vmx/vpmu_core2.c

xen/arch/x86/hvm/vmx/vpmu_core2.c Fri Mar 16 09:40:16 2012 +0100 renamed from xen/arch/x86/hvm/vmx/vpmu_core2.c Thu Mar 15 15:20:37 2012 +0000 +0 -1
@@ -690,7 +690,7 @@ static int core2_vpmu_initialise(struct 
 
 printk("******************************************************\n");
             printk("** WARNING: Emulation of BTS Feature is switched on 
 **\n");
             printk("** Using this processor feature in a virtualized    
 **\n");
-            printk("** environment is not 100%% safe.                   

**\n");
+ printk("** environment is not 100%% safe.
**\n");
printk("** Setting the DS buffer address with wrong values
**\n");
printk("** may lead to hypervisor hangs or crashes.
**\n");
printk("** It is NOT recommended for production use!
**\n");

Quoted 8 lines

Dietmar.

Quoted 2 lines

Jan

Signature · 1 line
Company details: http://ts.fujitsu.com/imprint.html 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw

Am Freitag 16 März 2012, 09:16:27 schrieb Jan Beulich:

Quoted 11 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Dietmar Hahn

Am Donnerstag 15 März 2012, 14:15:03 schrieb Jan Beulich:

Quoted 3 lines

Committed both patches, but please take a look since - as indicated - I made a few adjustments.

Thanks for the adjustments Jan, works very well!
Only a small fix for console logging beauty ;-) :

Thanks. I took the liberty to add you S-o-b on this small a fix without you explicit stating so.

Yes, sure!

Dietmar.

Quoted 2 lines

Jan

Signature · 6 lines
Company details: http://ts.fujitsu.com/imprint.html

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel