[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v1 09/13] x86/PMU: Initialize PMU for PV guests



Code for initializing/deinitializing PMU, including setting up interrupt
handlers.

Signed-off-by: Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx>
---
 xen/arch/x86/hvm/svm/vpmu.c       |  34 ++++++------
 xen/arch/x86/hvm/vmx/vpmu_core2.c |  58 +++++++++++++-------
 xen/arch/x86/hvm/vpmu.c           | 110 +++++++++++++++++++++++++++++++++++++-
 xen/common/event_channel.c        |   1 +
 xen/include/asm-x86/hvm/vpmu.h    |   1 +
 xen/include/public/xen.h          |   1 +
 xen/include/public/xenpmu.h       |  23 ++++++--
 xen/include/xen/softirq.h         |   1 +
 8 files changed, 189 insertions(+), 40 deletions(-)

diff --git a/xen/arch/x86/hvm/svm/vpmu.c b/xen/arch/x86/hvm/svm/vpmu.c
index 4477f63..c39c7a2 100644
--- a/xen/arch/x86/hvm/svm/vpmu.c
+++ b/xen/arch/x86/hvm/svm/vpmu.c
@@ -367,14 +367,19 @@ static int amd_vpmu_initialise(struct vcpu *v)
         }
     }
 
-    ctxt = xzalloc(struct amd_vpmu_context);
-    if ( !ctxt )
+    if ( is_hvm_domain(v->domain) )
     {
-        gdprintk(XENLOG_WARNING, "Insufficient memory for PMU, "
-            " PMU feature is unavailable on domain %d vcpu %d.\n",
-            v->vcpu_id, v->domain->domain_id);
-        return -ENOMEM;
+        ctxt = xzalloc(struct amd_vpmu_context);
+        if ( !ctxt )
+        {
+            gdprintk(XENLOG_WARNING, "Insufficient memory for PMU, "
+                " PMU feature is unavailable on domain %d vcpu %d.\n",
+                v->vcpu_id, v->domain->domain_id);
+                return -ENOMEM;
+        }
     }
+    else
+        ctxt = &v->arch.vpmu.xenpmu_data->pmu.amd;
 
     vpmu->context = ctxt;
     vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
@@ -388,18 +393,17 @@ static void amd_vpmu_destroy(struct vcpu *v)
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
 
-    if ( is_hvm_domain(v->domain) &&
-         ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
-        amd_vpmu_unset_msr_bitmap(v);
-
-    xfree(vpmu->context);
-    vpmu_reset(vpmu, VPMU_CONTEXT_ALLOCATED);
-
-    if ( vpmu_is_set(vpmu, VPMU_RUNNING) )
+    if ( is_hvm_domain(v->domain) )
     {
-        vpmu_reset(vpmu, VPMU_RUNNING);
+        if ( ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+            amd_vpmu_unset_msr_bitmap(v);
+
+        xfree(vpmu->context);
         release_pmu_ownship(PMU_OWNER_HVM);
     }
+
+    vpmu->context = NULL;
+    vpmu_clear(vpmu);
 }
 
 /* VPMU part of the 'q' keyhandler */
diff --git a/xen/arch/x86/hvm/vmx/vpmu_core2.c 
b/xen/arch/x86/hvm/vmx/vpmu_core2.c
index 66325d5..ecaa799 100644
--- a/xen/arch/x86/hvm/vmx/vpmu_core2.c
+++ b/xen/arch/x86/hvm/vmx/vpmu_core2.c
@@ -320,25 +320,33 @@ static int core2_vpmu_alloc_resource(struct vcpu *v)
     struct core2_vpmu_context *core2_vpmu_cxt;
     struct core2_pmu_enable *pmu_enable = NULL;
 
-    if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
+    pmu_enable = xzalloc_bytes(sizeof(struct core2_pmu_enable));
+    if ( !pmu_enable )
         return 0;
+ 
+    if ( is_hvm_domain(v->domain) )
+    {
+        if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
+            goto out_err;
 
-    wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
-    if ( vmx_add_host_load_msr(MSR_CORE_PERF_GLOBAL_CTRL) )
-        goto out_err;
+        wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+        if ( vmx_add_host_load_msr(MSR_CORE_PERF_GLOBAL_CTRL) )
+            goto out_err;
 
-    if ( vmx_add_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL) )
-        goto out_err;
-    vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL,
-                 core2_calc_intial_glb_ctrl_msr());
+        if ( vmx_add_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL) )
+            goto out_err;
+        vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL,
+                            core2_calc_intial_glb_ctrl_msr());
 
-    pmu_enable = xzalloc_bytes(sizeof(struct core2_pmu_enable));
-    if ( !pmu_enable )
-        goto out_err;
-
-    core2_vpmu_cxt = xzalloc_bytes(sizeof(struct core2_vpmu_context));
-    if ( !core2_vpmu_cxt )
-        goto out_err;
+        core2_vpmu_cxt = xzalloc_bytes(sizeof(struct core2_vpmu_context));
+        if ( !core2_vpmu_cxt )
+            goto out_err;
+    }
+    else
+    {
+        core2_vpmu_cxt = &v->arch.vpmu.xenpmu_data->pmu.intel;
+        vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
+    }
 
     core2_vpmu_cxt->pmu_enable = pmu_enable;
     vpmu->context = (void *)core2_vpmu_cxt;
@@ -748,6 +756,11 @@ func_out:
     arch_pmc_cnt = core2_get_pmc_count();
     check_pmc_quirk();
 
+    /* PV domains can allocate resources immediately */
+    if ( !is_hvm_domain(v->domain) )
+        if ( !core2_vpmu_alloc_resource(v) )
+            return 1;
+
     return 0;
 }
 
@@ -758,12 +771,17 @@ static void core2_vpmu_destroy(struct vcpu *v)
 
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
-    xfree(core2_vpmu_cxt->pmu_enable);
-    xfree(vpmu->context);
-    if ( cpu_has_vmx_msr_bitmap && is_hvm_domain(v->domain) )
-        core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
+
+    if ( is_hvm_domain(v->domain) )
+    {
+        xfree(core2_vpmu_cxt->pmu_enable);
+        xfree(vpmu->context);
+        if ( cpu_has_vmx_msr_bitmap )
+            core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
+    }
+
     release_pmu_ownship(PMU_OWNER_HVM);
-    vpmu_reset(vpmu, VPMU_CONTEXT_ALLOCATED);
+    vpmu_clear(vpmu);
 }
 
 struct arch_vpmu_ops core2_vpmu_ops = {
diff --git a/xen/arch/x86/hvm/vpmu.c b/xen/arch/x86/hvm/vpmu.c
index 820576e..04cc114 100644
--- a/xen/arch/x86/hvm/vpmu.c
+++ b/xen/arch/x86/hvm/vpmu.c
@@ -21,6 +21,9 @@
 #include <xen/config.h>
 #include <xen/sched.h>
 #include <xen/xenoprof.h>
+#include <xen/event.h>
+#include <xen/softirq.h>
+#include <xen/hypercall.h>
 #include <xen/guest_access.h>
 #include <asm/regs.h>
 #include <asm/types.h>
@@ -32,6 +35,7 @@
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
+#include <asm/nmi.h>
 #include <public/xenpmu.h>
 
 /*
@@ -249,7 +253,13 @@ void vpmu_destroy(struct vcpu *v)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_destroy )
+    {
+        /* Unload VPMU first. This will stop counters from running */
+        on_selected_cpus(cpumask_of(vcpu_vpmu(v)->last_pcpu),
+                         vpmu_save_force, (void *)v, 1);
+
         vpmu->arch_vpmu_ops->arch_vpmu_destroy(v);
+    }
 }
 
 /* Dump some vpmu informations on console. Used in keyhandler dump_domains(). 
*/
@@ -261,6 +271,92 @@ void vpmu_dump(struct vcpu *v)
         vpmu->arch_vpmu_ops->arch_vpmu_dump(v);
 }
 
+int pmu_nmi_interrupt(struct cpu_user_regs *regs, int cpu)
+{
+    return vpmu_do_interrupt(regs);
+}
+
+/* Process the softirq set by PMU NMI handler */
+void pmu_virq(void)
+{
+    struct vcpu *v = current;
+
+    if ( (vpmu_mode & VPMU_PRIV) ||
+        (v->domain->domain_id >= DOMID_FIRST_RESERVED) )
+    {
+        if ( smp_processor_id() >= dom0->max_vcpus )
+        {
+            printk(KERN_WARNING "PMU softirq on unexpected processor %d\n",
+                smp_processor_id());
+            return;
+        }
+        v = dom0->vcpu[smp_processor_id()];
+    }
+
+    send_guest_vcpu_virq(v, VIRQ_XENPMU);
+}
+
+static int pvpmu_init(struct domain *d, xenpmu_params_t *params)
+{
+    struct vcpu *v;
+    static int pvpmu_initted = 0;
+
+    if ( params->vcpu < 0 || params->vcpu >= d->max_vcpus )
+        return -EINVAL;
+
+    if ( !pvpmu_initted )
+    {
+        if (reserve_lapic_nmi() == 0)
+            set_nmi_callback(pmu_nmi_interrupt);
+        else
+        {
+            printk("Failed to reserve PMU NMI\n");
+            return -EBUSY;
+        }
+        open_softirq(PMU_SOFTIRQ, pmu_virq);
+        pvpmu_initted = 1;
+    }
+
+    if ( !mfn_valid(params->mfn) ||
+        !get_page_and_type(mfn_to_page(params->mfn), d, PGT_writable_page) )
+        return -EINVAL;
+
+    v = d->vcpu[params->vcpu];
+    v->arch.vpmu.xenpmu_data = map_domain_page_global(params->mfn);
+    memset(v->arch.vpmu.xenpmu_data, 0, PAGE_SIZE);
+
+    vpmu_initialise(v);
+
+    return 0;
+}
+
+static void pvpmu_finish(struct domain *d, xenpmu_params_t *params)
+{
+    struct vcpu *v;
+    uint64_t mfn;
+
+    if ( params->vcpu < 0 || params->vcpu >= d->max_vcpus )
+        return;
+
+    v = d->vcpu[params->vcpu];
+    if (v != current)
+        vcpu_pause(v);
+
+    if ( v->arch.vpmu.xenpmu_data )
+    {
+        mfn = domain_page_map_to_mfn(v->arch.vpmu.xenpmu_data);
+        if ( mfn_valid(mfn) )
+        {
+            unmap_domain_page_global(v->arch.vpmu.xenpmu_data);
+            put_page_and_type(mfn_to_page(mfn));
+        }
+    }
+    vpmu_destroy(v);
+
+    if (v != current)
+        vcpu_unpause(v);
+}
+
 long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
     int ret = -EINVAL;
@@ -317,7 +413,19 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg)
             return -EFAULT;
         ret = 0;
         break;
-     }
+
+    case XENPMU_init:
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+        ret = pvpmu_init(current->domain, &pmu_params);
+        break;
+
+    case XENPMU_finish:
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+        pvpmu_finish(current->domain, &pmu_params);
+        break;
+    }
 
     return ret;
 }
diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
index 64c976b..9ee6e5a 100644
--- a/xen/common/event_channel.c
+++ b/xen/common/event_channel.c
@@ -107,6 +107,7 @@ static int virq_is_global(uint32_t virq)
     case VIRQ_TIMER:
     case VIRQ_DEBUG:
     case VIRQ_XENOPROF:
+    case VIRQ_XENPMU:
         rc = 0;
         break;
     case VIRQ_ARCH_0 ... VIRQ_ARCH_7:
diff --git a/xen/include/asm-x86/hvm/vpmu.h b/xen/include/asm-x86/hvm/vpmu.h
index cc45c70..46dfbc6 100644
--- a/xen/include/asm-x86/hvm/vpmu.h
+++ b/xen/include/asm-x86/hvm/vpmu.h
@@ -59,6 +59,7 @@ struct vpmu_struct {
     u32 hw_lapic_lvtpc;
     void *context;
     struct arch_vpmu_ops *arch_vpmu_ops;
+    xenpmu_data_t *xenpmu_data;
 };
 
 /* VPMU states */
diff --git a/xen/include/public/xen.h b/xen/include/public/xen.h
index 7f56560..91d3db2 100644
--- a/xen/include/public/xen.h
+++ b/xen/include/public/xen.h
@@ -161,6 +161,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define VIRQ_MEM_EVENT  10 /* G. (DOM0) A memory event has occured           */
 #define VIRQ_XC_RESERVED 11 /* G. Reserved for XenClient                     */
 #define VIRQ_ENOMEM     12 /* G. (DOM0) Low on heap memory       */
+#define VIRQ_XENPMU     13 /* V.  PMC interrupt                              */
 
 /* Architecture-specific VIRQ definitions. */
 #define VIRQ_ARCH_0    16
diff --git a/xen/include/public/xenpmu.h b/xen/include/public/xenpmu.h
index 7240a30..ffaf3fe 100644
--- a/xen/include/public/xenpmu.h
+++ b/xen/include/public/xenpmu.h
@@ -13,6 +13,8 @@
 #define XENPMU_mode_set        1
 #define XENPMU_flags_get       2
 #define XENPMU_flags_set       3
+#define XENPMU_init            4
+#define XENPMU_finish          5
 
 /* Parameters structure for HYPERVISOR_xenpmu_op call */
 typedef struct xenpmu_params {
@@ -24,6 +26,8 @@ typedef struct xenpmu_params {
     uint64_t pad;
     };
     uint64_t control;
+    uint64_t mfn;
+    uint64_t vcpu;
 } xenpmu_params_t;
 
 
@@ -83,11 +87,14 @@ struct arch_msr_pair {
     uint64_t control;
 };
 struct core2_vpmu_context {
-    struct core2_pmu_enable *pmu_enable;
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t global_ovf_status;
     uint64_t fix_counters[VPMU_CORE2_NUM_FIXED];
     uint64_t ctrls[VPMU_CORE2_NUM_CTRLS];
-    uint64_t global_ovf_status;
     struct arch_msr_pair arch_msr_pair[VPMU_CORE2_MAX_ARCH_PMCS];
+    struct core2_pmu_enable *pmu_enable;
 };
 
 /* PMU flags */
@@ -95,14 +102,22 @@ struct core2_vpmu_context {
 
 /* Shared between hypervisor and PV domain */
 typedef struct xenpmu_data {
-    struct cpu_user_regs regs;
-    uint16_t domain_id;
+    union {
+        struct cpu_user_regs regs;
+        uint8_t pad[256];
+    };
+    uint32_t domain_id;
     uint32_t vcpu_id;
     uint32_t pcpu_id;
     uint32_t pmu_flags;
     union {
         struct amd_vpmu_context amd;
         struct core2_vpmu_context intel;
+#define MAX(x,y) ((x) > (y) ? (x) : (y))
+#define MAX_CTXT_SZ     MAX(sizeof(struct amd_vpmu_context),\
+                            sizeof(struct core2_vpmu_context))
+#define PMU_PAD_SIZE        ((MAX_CTXT_SZ + 64) & ~63) + 128
+        uint8_t pad[PMU_PAD_SIZE]; /* a bit more than necessary */
     } pmu;
 } xenpmu_data_t;
 
diff --git a/xen/include/xen/softirq.h b/xen/include/xen/softirq.h
index 0c0d481..5829fa4 100644
--- a/xen/include/xen/softirq.h
+++ b/xen/include/xen/softirq.h
@@ -8,6 +8,7 @@ enum {
     NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ,
     RCU_SOFTIRQ,
     TASKLET_SOFTIRQ,
+    PMU_SOFTIRQ,
     NR_COMMON_SOFTIRQS
 };
 
-- 
1.8.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.