Skip to contentSkip to conversation

x86/PMU: Xen PMU PV support

PATCH Superseded Boris Ostrovsky 2 participants 40 messages Jan 6, 2014 – Jan 16, 2014

v3 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

v3 cover x86/PMU: Xen PMU PV support

Here is the much delayed version 3 of PV PMU patches.

The following patch series adds PMU support in Xen for PV guests. There is a companion patchset for Linux kernel. In addition, another set of changes will be provided (later) for userland perf code.

This version has following limitations:
* For accurate profiling of dom0/Xen dom0 VCPUs should be pinned.
* Hypervisor code is only profiled on processors that have running dom0 VCPUs on them.
* No backtrace support.
* Will fail to load under XSM: we ran out of bits in permissions vector and this needs to be fixed separately

A few notes that may help reviewing:

* A shared data structure (xenpmu_data_t) between each PV VPCU and hypervisor CPU is used for passing registers' values as well as PMU state at the time of PMU interrupt.
* PMU interrupts are taken by hypervisor either as NMIs or a regular vector interrupt for both HVM and PV. The interrupts are sent as NMIs to HVM guests and as virtual interrupts to PV guests
* Guest's interrupt handler does not read/write PMU MSRs directly. Instead, it accesses xenpmu_data_t and flushes it to HW it before returning.
* PMU mode is controlled at runtime via /sys/hypervisor/pmu/pmu/{pmu_mode,pmu_flags} in addition to 'vpmu' boot option (which is preserved for back compatibility). The following modes are provided:
* disable: VPMU is off
* enable: VPMU is on. Guests can profile themselves, dom0 profiles itself and Xen
* priv_enable: dom0 only profiling. dom0 collects samples for everyone. Sampling in guests is suspended.
* /proc/xen/xensyms file exports hypervisor's symbols to dom0 (similar to /proc/kallsyms)
* VPMU infrastructure is now used for both HVM and PV and therefore has been moved up from hvm subtree

Changes in v3:

* Moved PMU MSR banks out from architectural context data structures to allow for future expansion without protocol changes
* PMU interrupts can be either NMIs or regular vector interrupts (the latter is the default)
* Context is now marked as PMU_CACHED by the hypervisor code to avoid certain race conditions with the guest
* Fixed races with PV guest in MSR access handlers
* More Intel VPMU cleanup
* Moved NMI-unsafe code from NMI handler
* Dropped changes to vcpu->is_running
* Added LVTPC apic handling (cached for PV guests)
* Separated privileged profiling mode into a standalone patch
* Separated NMI handling into a standalone patch

Changes in v2:

* Xen symbols are exported as data structure (as opoosed to a set of formatted strings in v1). Even though one symbol per hypercall is returned performance appears to be acceptable: reading whole file from dom0 userland takes on average about twice as long as reading /proc/kallsyms
* More cleanup of Intel VPMU code to simplify publicly exported structures
* There is an architecture-independent and x86-specific public include files (ARM has a stub)
* General cleanup of public include files to make them more presentable (and to make auto doc generation better)
* Setting of vcpu->is_running is now done on ARM in schedule_tail as well (making changes to common/schedule.c architecture-independent). Note that this is not tested since I don't have access to ARM hardware.
* PCPU ID of interrupted processor is now passed to PV guest

Boris Ostrovsky (16):
common/symbols: Export hypervisor symbols to PV guest x86/VPMU: Stop AMD counters when called from vpmu_save_force() x86/VPMU: Minor VPMU cleanup
intel/VPMU: Clean up Intel VPMU code
x86/VPMU: Handle APIC_LVTPC accesses
intel/VPMU: MSR_CORE_PERF_GLOBAL_CTRL should be initialized to zero x86/VPMU: Add public xenpmu.h
x86/VPMU: Make vpmu not HVM-specific
x86/VPMU: Interface for setting PMU mode and flags x86/VPMU: Initialize PMU for PV guests
x86/VPMU: Add support for PMU register handling on PV guests x86/VPMU: Handle PMU interrupts for PV guests
x86/VPMU: Add privileged PMU mode
x86/VPMU: Save VPMU state for PV guests during context switch x86/VPMU: NMI-based VPMU support
x86/VPMU: Move VPMU files up from hvm/ directory

 xen/arch/x86/Makefile                    |   1 +
 xen/arch/x86/domain.c                    |  17 +-
 xen/arch/x86/hvm/Makefile                |   1 -
 xen/arch/x86/hvm/svm/Makefile            |   1 -
 xen/arch/x86/hvm/svm/vpmu.c              | 494 ----------------
 xen/arch/x86/hvm/vlapic.c                |   5 +-
 xen/arch/x86/hvm/vmx/Makefile            |   1 -
 xen/arch/x86/hvm/vmx/vmcs.c              |  55 ++
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 931 ------------------------------
 xen/arch/x86/hvm/vpmu.c                  | 266 ---------
 xen/arch/x86/oprofile/op_model_ppro.c    |   8 +-
 xen/arch/x86/platform_hypercall.c        |  18 +
 xen/arch/x86/traps.c                     |  34 +-
 xen/arch/x86/vpmu.c                      | 621 ++++++++++++++++++++
 xen/arch/x86/vpmu_amd.c                  | 499 ++++++++++++++++
 xen/arch/x86/vpmu_intel.c                | 936 +++++++++++++++++++++++++++++++
 xen/arch/x86/x86_64/compat/entry.S       |   4 +
 xen/arch/x86/x86_64/entry.S              |   4 +
 xen/arch/x86/x86_64/platform_hypercall.c |   2 +
 xen/common/event_channel.c               |   1 +
 xen/common/symbols.c                     |  50 +-
 xen/common/vsprintf.c                    |   2 +-
 xen/include/asm-x86/domain.h             |   2 +
 xen/include/asm-x86/hvm/vcpu.h           |   3 -
 xen/include/asm-x86/hvm/vmx/vmcs.h       |   4 +-
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  51 --
 xen/include/asm-x86/hvm/vpmu.h           | 104 ----
 xen/include/asm-x86/vpmu.h               |  95 ++++
 xen/include/public/arch-x86/xenpmu.h     |  74 +++
 xen/include/public/platform.h            |  19 +
 xen/include/public/xen.h                 |   2 +
 xen/include/public/xenpmu.h              | 109 ++++
 xen/include/xen/hypercall.h              |   4 +
 xen/include/xen/softirq.h                |   1 +
 xen/include/xen/symbols.h                |   7 +-
 35 files changed, 2554 insertions(+), 1872 deletions(-)
 delete mode 100644 xen/arch/x86/hvm/svm/vpmu.c
 delete mode 100644 xen/arch/x86/hvm/vmx/vpmu_core2.c
 delete mode 100644 xen/arch/x86/hvm/vpmu.c
 create mode 100644 xen/arch/x86/vpmu.c
 create mode 100644 xen/arch/x86/vpmu_amd.c
 create mode 100644 xen/arch/x86/vpmu_intel.c
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 delete mode 100644 xen/include/asm-x86/hvm/vpmu.h
 create mode 100644 xen/include/asm-x86/vpmu.h
 create mode 100644 xen/include/public/arch-x86/xenpmu.h
 create mode 100644 xen/include/public/xenpmu.h
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v3 1/16 common/symbols: Export hypervisor symbols to PV guest

Export Xen's symbols as {<address><type><name>} triplet via new XENPF_get_symbol hypercall

---
 xen/arch/x86/platform_hypercall.c        | 18 ++++++++++++
 xen/arch/x86/x86_64/platform_hypercall.c |  2 ++
 xen/common/symbols.c                     | 50 +++++++++++++++++++++++++++++++-
 xen/common/vsprintf.c                    |  2 +-
 xen/include/public/platform.h            | 19 ++++++++++++
 xen/include/xen/symbols.h                |  7 +++--
 6 files changed, 94 insertions(+), 4 deletions(-)
xen/arch/x86/platform_hypercall.c +18 -0
index 2162811..cdb6886 100644
@@ -23,6 +23,7 @@
 #include <xen/cpu.h>
 #include <xen/pmstat.h>
 #include <xen/irq.h>
+#include <xen/symbols.h>
 #include <asm/current.h>
 #include <public/platform.h>
 #include <acpi/cpufreq/processor_perf.h>
@@ -601,6 +602,23 @@ ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
     }
     break;
 
+    case XENPF_get_symbol:
+    {
+        char name[XEN_KSYM_NAME_LEN + 1];
+        XEN_GUEST_HANDLE_64(char) nameh;
+
+        guest_from_compat_handle(nameh, op->u.symdata.u.name);
+
+        ret = xensyms_read(&op->u.symdata.symnum, &op->u.symdata.type,
+                           &op->u.symdata.address, name);
+
+        if ( !ret && copy_to_guest(nameh, name, XEN_KSYM_NAME_LEN + 1) )
+            ret = -EFAULT;
+        if ( !ret && __copy_field_to_guest(u_xenpf_op, op, u.symdata) )
+            ret = -EFAULT;
+    }
+    break;
+ 
     default:
         ret = -ENOSYS;
         break;
xen/arch/x86/x86_64/platform_hypercall.c +2 -0
index b6f380e..795837f 100644
@@ -32,6 +32,8 @@ CHECK_pf_pcpu_version;
 CHECK_pf_enter_acpi_sleep;
 #undef xen_pf_enter_acpi_sleep
 
+#define xenpf_symdata   compat_pf_symdata
+
 #define COMPAT
 #define _XEN_GUEST_HANDLE(t) XEN_GUEST_HANDLE(t)
 #define _XEN_GUEST_HANDLE_PARAM(t) XEN_GUEST_HANDLE_PARAM(t)
xen/common/symbols.c +49 -1

65 lines not on this page · raw message

xen/common/vsprintf.c +1 -1
index 1a6198e..c5ae187 100644
@@ -275,7 +275,7 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     case 'S': /* Symbol name unconditionally with offset and size */
     {
         unsigned long sym_size, sym_offset;
-        char namebuf[KSYM_NAME_LEN+1];
+        char namebuf[XEN_KSYM_NAME_LEN+1];
 
         /* Advance parents fmt string, as we have consumed 's' or 'S' */
         ++*fmt_ptr;
xen/include/public/platform.h +19 -0
index 4341f54..ba9da49 100644
@@ -527,6 +527,24 @@ struct xenpf_core_parking {
 typedef struct xenpf_core_parking xenpf_core_parking_t;
 DEFINE_XEN_GUEST_HANDLE(xenpf_core_parking_t);
 
+#define XENPF_get_symbol   61
+#define XEN_KSYM_NAME_LEN 127
+struct xenpf_symdata {
+    /* IN variables */
+    uint32_t symnum;
+
+    /* OUT variables */
+    uint32_t type;
+    uint64_t address;
+
+    union {
+        XEN_GUEST_HANDLE(char) name;
+        uint64_t pad;
+    } u;
+};
+typedef struct xenpf_symdata xenpf_symdata_t;
+DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t);
+
 /*
  * ` enum neg_errnoval
  * ` HYPERVISOR_platform_op(const struct xen_platform_op*);
@@ -553,6 +571,7 @@ struct xen_platform_op {
         struct xenpf_cpu_hotadd        cpu_add;
         struct xenpf_mem_hotadd        mem_add;
         struct xenpf_core_parking      core_parking;
+        struct xenpf_symdata           symdata;
         uint8_t                        pad[128];
     } u;
 };
xen/include/xen/symbols.h +5 -2
index 87cd77d..adbf91d 100644
@@ -2,8 +2,8 @@
 #define _XEN_SYMBOLS_H
 
 #include <xen/types.h>
-
-#define KSYM_NAME_LEN 127
+#include <public/xen.h>
+#include <public/platform.h>
 
 /* Lookup an address. */
 const char *symbols_lookup(unsigned long addr,
@@ -11,4 +11,7 @@ const char *symbols_lookup(unsigned long addr,
                            unsigned long *offset,
                            char *namebuf);
 
+extern int xensyms_read(uint32_t *symnum, uint32_t *type,
+                        uint64_t *address, char *name);
+
 #endif /*_XEN_SYMBOLS_H*/
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [PATCH v3 01/16] common/symbols: Export hypervisor symbols to PV guest

Quoted 1 line
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

The subject seems odd - rather than PV I would think it should say privileged.

xen/arch/x86/x86_64/platform_hypercall.c CHECK_pf_pcpu_version; · line 36

Quoted 8 lines
xen/arch/x86/x86_64/platform_hypercall.c +2 -0
@@ -32,6 +32,8 @@ CHECK_pf_pcpu_version;
 CHECK_pf_enter_acpi_sleep;
 #undef xen_pf_enter_acpi_sleep
 
+#define xenpf_symdata   compat_pf_symdata
+

This should be done like other cases (see context above): Add a CHECK_ invocation, which in turn requires an entry in include/xlat.lst.

Jan

#3 lore raw

Re: [PATCH v3 01/16] common/symbols: Export hypervisor symbols to PV guest

Quoted 11 lines · Jan Beulich

xen/arch/x86/x86_64/platform_hypercall.c CHECK_pf_pcpu_version; · line 36

Quoted 9 lines
xen/arch/x86/x86_64/platform_hypercall.c +2 -0
@@ -32,6 +32,8 @@ CHECK_pf_pcpu_version;
  CHECK_pf_enter_acpi_sleep;
  #undef xen_pf_enter_acpi_sleep
 
+#define xenpf_symdata   compat_pf_symdata
+

This should be done like other cases (see context above): Add a CHECK_ invocation, which in turn requires an entry in include/xlat.lst.

Based on your comment to my attempt to patch compat.h, is this really needed? Since xenpf_symdata contains a handle we shouldn't subject it to checking, should we?

-boris

#4 lore raw

Re: [PATCH v3 01/16] common/symbols: Export hypervisor symbols to PV guest

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 7 lines · Jan Beulich
Quoted 7 lines
xen/arch/x86/x86_64/platform_hypercall.c +0 -0
@@ -32,6 +32,8 @@ CHECK_pf_pcpu_version;
  CHECK_pf_enter_acpi_sleep;
  #undef xen_pf_enter_acpi_sleep
 
Quoted 8 lines
Quoted 4 lines

xen/arch/x86/x86_64/platform_hypercall.c CHECK_pf_pcpu_version; · line 36

Quoted 2 lines
xen/arch/x86/x86_64/platform_hypercall.c +2 -0
+#define xenpf_symdata   compat_pf_symdata
+

This should be done like other cases (see context above): Add a CHECK_ invocation, which in turn requires an entry in include/xlat.lst.

Based on your comment to my attempt to patch compat.h, is this really needed? Since xenpf_symdata contains a handle we shouldn't subject it to checking, should we?

Correct. You nevertheless should add the new type to include/xlat.lst.

Jan

#5 lore raw

PATCH v3 2/16 x86/VPMU: Stop AMD counters when called from vpmu_save_force()

In reply to Boris Ostrovsky

Change amd_vpmu_save() algorithm to accommodate cases when we need to stop counters from vpmu_save_force() (needed by subsequent PMU patches).

---
 xen/arch/x86/hvm/svm/vpmu.c | 14 ++++----------
 xen/arch/x86/hvm/vpmu.c     | 12 ++++++------
 2 files changed, 10 insertions(+), 16 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +4 -10
index 66a3815..bec40d8 100644
@@ -223,22 +223,16 @@ static int amd_vpmu_save(struct vcpu *v)
     struct amd_vpmu_context *ctx = vpmu->context;
     unsigned int i;
 
-    /*
-     * Stop the counters. If we came here via vpmu_save_force (i.e.
-     * when VPMU_CONTEXT_SAVE is set) counters are already stopped.
-     */
-    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_SAVE) )
+    if ( !vpmu_is_set(vpmu, VPMU_FROZEN) )
     {
-        vpmu_set(vpmu, VPMU_FROZEN);
-
         for ( i = 0; i < num_counters; i++ )
             wrmsrl(ctrls[i], 0);
 
-        return 0;
+        vpmu_set(vpmu, VPMU_FROZEN);
     }
 
-    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
-        return 0;
+    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_SAVE) )
+            return 0;
 
     context_save(v);
 
xen/arch/x86/hvm/vpmu.c +6 -6
index 21fbaba..a4e3664 100644
@@ -127,13 +127,19 @@ static void vpmu_save_force(void *arg)
     struct vcpu *v = (struct vcpu *)arg;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
+    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+        return;
+
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
         return;
 
+    vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+
     if ( vpmu->arch_vpmu_ops )
         (void)vpmu->arch_vpmu_ops->arch_vpmu_save(v);
 
     vpmu_reset(vpmu, VPMU_CONTEXT_SAVE);
+    vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
 
     per_cpu(last_vcpu, smp_processor_id()) = NULL;
 }
@@ -177,12 +183,8 @@ void vpmu_load(struct vcpu *v)
          * before saving the context.
          */
         if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
-        {
-            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
             on_selected_cpus(cpumask_of(vpmu->last_pcpu),
                              vpmu_save_force, (void *)v, 1);
-            vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
-        }
     } 
 
     /* Prevent forced context save from remote CPU */
@@ -195,9 +197,7 @@ void vpmu_load(struct vcpu *v)
         vpmu = vcpu_vpmu(prev);
 
         /* Someone ran here before us */
-        vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
         vpmu_save_force(prev);
-        vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
 
         vpmu = vcpu_vpmu(v);
     }
Signature · 1 line
1.8.1.4
#6 lore raw

PATCH v3 3/16 x86/VPMU: Minor VPMU cleanup

In reply to Boris Ostrovsky

Update macros that modify VPMU flags to allow changing multiple bits at once.

Make sure that we only touch MSR bitmap on HVM guests (both VMX and SVM). This is needed by subsequent PMU patches.

---
 xen/arch/x86/hvm/svm/vpmu.c       | 14 +++++++++-----
 xen/arch/x86/hvm/vmx/vpmu_core2.c |  9 +++------
 xen/arch/x86/hvm/vpmu.c           | 11 +++--------
 xen/include/asm-x86/hvm/vpmu.h    |  9 +++++----
 4 files changed, 20 insertions(+), 23 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +9 -5
index bec40d8..842bce7 100644
@@ -236,7 +236,8 @@ static int amd_vpmu_save(struct vcpu *v)
 
     context_save(v);
 
-    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) && ctx->msr_bitmap_set )
+    if ( is_hvm_domain(v->domain) && 
+        !vpmu_is_set(vpmu, VPMU_RUNNING) && ctx->msr_bitmap_set )
         amd_vpmu_unset_msr_bitmap(v);
 
     return 1;
@@ -276,7 +277,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
     /* For all counters, enable guest only mode for HVM guest */
-    if ( (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
+    if ( is_hvm_domain(v->domain) && (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
         !(is_guest_mode(msr_content)) )
     {
         set_guest_mode(msr_content);
@@ -292,7 +293,8 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         apic_write(APIC_LVTPC, PMU_APIC_VECTOR);
         vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
 
-        if ( !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+        if ( is_hvm_domain(v->domain) &&
+             !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
             amd_vpmu_set_msr_bitmap(v);
     }
 
@@ -303,7 +305,8 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
         vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
         vpmu_reset(vpmu, VPMU_RUNNING);
-        if ( ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+        if ( is_hvm_domain(v->domain) &&
+             ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
             amd_vpmu_unset_msr_bitmap(v);
         release_pmu_ownship(PMU_OWNER_HVM);
     }
@@ -395,7 +398,8 @@ static void amd_vpmu_destroy(struct vcpu *v)
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
 
-    if ( ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+    if ( is_hvm_domain(v->domain) &&
+         ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
         amd_vpmu_unset_msr_bitmap(v);
 
     xfree(vpmu->context);
xen/arch/x86/hvm/vmx/vpmu_core2.c +3 -6
index ee26362..a1f1561 100644
@@ -326,10 +326,7 @@ static int core2_vpmu_save(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
-    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_SAVE) )
-        return 0;
-
-    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) ) 
+    if ( !vpmu_is_set_all(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED) )
         return 0;
 
     __core2_vpmu_save(v);
@@ -446,7 +443,7 @@ static int core2_vpmu_msr_common_check(u32 msr_index, int *type, int *index)
     {
         __core2_vpmu_load(current);
         vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
-        if ( cpu_has_vmx_msr_bitmap )
+        if ( cpu_has_vmx_msr_bitmap && is_hvm_domain(current->domain) )
             core2_vpmu_set_msr_bitmap(current->arch.hvm_vmx.msr_bitmap);
     }
     return 1;
@@ -813,7 +810,7 @@ static void core2_vpmu_destroy(struct vcpu *v)
         return;
     xfree(core2_vpmu_cxt->pmu_enable);
     xfree(vpmu->context);
-    if ( cpu_has_vmx_msr_bitmap )
+    if ( cpu_has_vmx_msr_bitmap && is_hvm_domain(v->domain) )
         core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
     release_pmu_ownship(PMU_OWNER_HVM);
     vpmu_reset(vpmu, VPMU_CONTEXT_ALLOCATED);
xen/arch/x86/hvm/vpmu.c +3 -8
index a4e3664..d6a9ff6 100644
@@ -127,10 +127,7 @@ static void vpmu_save_force(void *arg)
     struct vcpu *v = (struct vcpu *)arg;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
-    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
-        return;
-
-    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
+    if ( !vpmu_is_set_all(vpmu, VPMU_CONTEXT_ALLOCATED | VPMU_CONTEXT_LOADED) )
         return;
 
     vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
@@ -138,8 +135,7 @@ static void vpmu_save_force(void *arg)
     if ( vpmu->arch_vpmu_ops )
         (void)vpmu->arch_vpmu_ops->arch_vpmu_save(v);
 
-    vpmu_reset(vpmu, VPMU_CONTEXT_SAVE);
-    vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
+    vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
 
     per_cpu(last_vcpu, smp_processor_id()) = NULL;
 }
@@ -149,8 +145,7 @@ void vpmu_save(struct vcpu *v)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     int pcpu = smp_processor_id();
 
-    if ( !(vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) &&
-           vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED)) )
+    if ( !vpmu_is_set_all(vpmu, VPMU_CONTEXT_ALLOCATED | VPMU_CONTEXT_LOADED) )
        return;
 
     vpmu->last_pcpu = pcpu;
xen/include/asm-x86/hvm/vpmu.h +5 -4
index 40f63fb..2a713be 100644
@@ -81,10 +81,11 @@ struct vpmu_struct {
 #define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
 
 
-#define vpmu_set(_vpmu, _x)    ((_vpmu)->flags |= (_x))
-#define vpmu_reset(_vpmu, _x)  ((_vpmu)->flags &= ~(_x))
-#define vpmu_is_set(_vpmu, _x) ((_vpmu)->flags & (_x))
-#define vpmu_clear(_vpmu)      ((_vpmu)->flags = 0)
+#define vpmu_set(_vpmu, _x)         ((_vpmu)->flags |= (_x))
+#define vpmu_reset(_vpmu, _x)       ((_vpmu)->flags &= ~(_x))
+#define vpmu_is_set(_vpmu, _x)      ((_vpmu)->flags & (_x))
+#define vpmu_is_set_all(_vpmu, _x)  (((_vpmu)->flags & (_x)) == (_x))
+#define vpmu_clear(_vpmu)           ((_vpmu)->flags = 0)
 
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content);
 int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content);
Signature · 1 line
1.8.1.4
#7 lore raw

PATCH v3 4/16 intel/VPMU: Clean up Intel VPMU code

In reply to Boris Ostrovsky

Remove struct pmumsr and core2_pmu_enable. Replace static MSR structures with fields in core2_vpmu_context.

Call core2_get_pmc_count() once, during initialization.

Properly clean up when core2_vpmu_alloc_resource() fails and add routines to remove MSRs from VMCS.

---
 xen/arch/x86/hvm/vmx/vmcs.c              |  55 ++++++
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 310 ++++++++++++++-----------------
 xen/include/asm-x86/hvm/vmx/vmcs.h       |   2 +
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  19 --
 4 files changed, 199 insertions(+), 187 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +55 -0
index 44f33cb..5f86b17 100644
@@ -1205,6 +1205,34 @@ int vmx_add_guest_msr(u32 msr)
     return 0;
 }
 
+void vmx_rm_guest_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int idx, msr_count = curr->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.msr_area;
+
+    if ( msr_area == NULL )
+        return;
+
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+
+    for ( ; idx < msr_count - 1; idx++ )
+    {
+        msr_area[idx].index = msr_area[idx + 1].index;
+        msr_area[idx].data = msr_area[idx + 1].data;
+    }
+    msr_area[msr_count - 1].index = 0;
+
+    curr->arch.hvm_vmx.msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_STORE_COUNT, msr_count);
+    __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, msr_count);
+}
+
 int vmx_add_host_load_msr(u32 msr)
 {
     struct vcpu *curr = current;
@@ -1235,6 +1263,33 @@ int vmx_add_host_load_msr(u32 msr)
     return 0;
 }
 
+void vmx_rm_host_load_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int idx,  msr_count = curr->arch.hvm_vmx.host_msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.host_msr_area;
+
+    if ( msr_area == NULL )
+        return;
+
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+
+    for ( ; idx < msr_count - 1; idx++ )
+    {
+        msr_area[idx].index = msr_area[idx + 1].index;
+        msr_area[idx].data = msr_area[idx + 1].data;
+    }
+    msr_area[msr_count - 1].index = 0;
+
+    curr->arch.hvm_vmx.host_msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_LOAD_COUNT, msr_count);
+}
+
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector)
 {
     if ( !test_and_set_bit(vector, v->arch.hvm_vmx.eoi_exit_bitmap) )
xen/arch/x86/hvm/vmx/vpmu_core2.c +142 -168

570 lines not on this page · raw message

xen/include/asm-x86/hvm/vmx/vmcs.h +2 -0
index ebaba5c..ed81cfb 100644
@@ -473,7 +473,9 @@ void vmx_enable_intercept_for_msr(struct vcpu *v, u32 msr, int type);
 int vmx_read_guest_msr(u32 msr, u64 *val);
 int vmx_write_guest_msr(u32 msr, u64 val);
 int vmx_add_guest_msr(u32 msr);
+void vmx_rm_guest_msr(u32 msr);
 int vmx_add_host_load_msr(u32 msr);
+void vmx_rm_host_load_msr(u32 msr);
 void vmx_vmcs_switch(struct vmcs_struct *from, struct vmcs_struct *to);
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector);
 void vmx_clear_eoi_exit_bitmap(struct vcpu *v, u8 vector);
xen/include/asm-x86/hvm/vmx/vpmu_core2.h +0 -19
index 60b05fd..410372d 100644
@@ -23,29 +23,10 @@
 #ifndef __ASM_X86_HVM_VPMU_CORE_H_
 #define __ASM_X86_HVM_VPMU_CORE_H_
 
-/* Currently only 3 fixed counters are supported. */
-#define VPMU_CORE2_NUM_FIXED 3
-/* Currently only 3 Non-architectual Performance Control MSRs */
-#define VPMU_CORE2_NUM_CTRLS 3
-
 struct arch_msr_pair {
     u64 counter;
     u64 control;
 };
 
-struct core2_pmu_enable {
-    char ds_area_enable;
-    char fixed_ctr_enable[VPMU_CORE2_NUM_FIXED];
-    char arch_pmc_enable[1];
-};
-
-struct core2_vpmu_context {
-    struct core2_pmu_enable *pmu_enable;
-    u64 fix_counters[VPMU_CORE2_NUM_FIXED];
-    u64 ctrls[VPMU_CORE2_NUM_CTRLS];
-    u64 global_ovf_status;
-    struct arch_msr_pair arch_msr_pair[1];
-};
-
 #endif /* __ASM_X86_HVM_VPMU_CORE_H_ */
 
Signature · 1 line
1.8.1.4
#8 lore raw

PATCH v3 5/16 x86/VPMU: Handle APIC_LVTPC accesses

In reply to Boris Ostrovsky

Update APIC_LVTPC vector when HVM guest writes to it.

---
 xen/arch/x86/hvm/svm/vpmu.c       |  4 ----
 xen/arch/x86/hvm/vlapic.c         |  5 ++++-
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 17 -----------------
 xen/arch/x86/hvm/vpmu.c           | 16 +++++++++++++---
 xen/include/asm-x86/hvm/vpmu.h    |  1 +
 5 files changed, 18 insertions(+), 25 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +0 -4
index 842bce7..1f7d6b7 100644
@@ -290,8 +290,6 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
             return 1;
         vpmu_set(vpmu, VPMU_RUNNING);
-        apic_write(APIC_LVTPC, PMU_APIC_VECTOR);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
 
         if ( is_hvm_domain(v->domain) &&
              !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
@@ -302,8 +300,6 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     if ( (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
         (is_pmu_enabled(msr_content) == 0) && vpmu_is_set(vpmu, VPMU_RUNNING) )
     {
-        apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
         vpmu_reset(vpmu, VPMU_RUNNING);
         if ( is_hvm_domain(v->domain) &&
              ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
xen/arch/x86/hvm/vlapic.c +4 -1
index bc06010..d954f4f 100644
@@ -38,6 +38,7 @@
 #include <asm/hvm/support.h>
 #include <asm/hvm/vmx/vmx.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/hvm/vpmu.h>
 #include <public/hvm/ioreq.h>
 #include <public/hvm/params.h>
 
@@ -732,8 +733,10 @@ static int vlapic_reg_write(struct vcpu *v,
             vlapic_adjust_i8259_target(v->domain);
             pt_may_unmask_irq(v->domain, NULL);
         }
-        if ( (offset == APIC_LVTT) && !(val & APIC_LVT_MASKED) )
+        else if ( (offset == APIC_LVTT) && !(val & APIC_LVT_MASKED) )
             pt_may_unmask_irq(NULL, &vlapic->pt);
+        else if ( offset == APIC_LVTPC )
+            vpmu_lvtpc_update(val);
         break;
 
     case APIC_TMICT:
xen/arch/x86/hvm/vmx/vpmu_core2.c +0 -17
index 89212ec..7fd2420 100644
@@ -532,19 +532,6 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     else
         vpmu_reset(vpmu, VPMU_RUNNING);
 
-    /* Setup LVTPC in local apic */
-    if ( vpmu_is_set(vpmu, VPMU_RUNNING) &&
-         is_vlapic_lvtpc_enabled(vcpu_vlapic(v)) )
-    {
-        apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
-    }
-    else
-    {
-        apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
-    }
-
     if ( type != MSR_TYPE_GLOBAL )
     {
         u64 mask;
@@ -710,10 +697,6 @@ static int core2_vpmu_do_interrupt(struct cpu_user_regs *regs)
             return 0;
     }
 
-    /* HW sets the MASK bit when performance counter interrupt occurs*/
-    vpmu->hw_lapic_lvtpc = apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED;
-    apic_write_around(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
-
     return 1;
 }
 
xen/arch/x86/hvm/vpmu.c +13 -3
index d6a9ff6..2c1201b 100644
@@ -64,6 +64,14 @@ static void __init parse_vpmu_param(char *s)
     }
 }
 
+void vpmu_lvtpc_update(uint32_t val)
+{
+    struct vpmu_struct *vpmu = vcpu_vpmu(current);
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
+    apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+}
+
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
@@ -227,19 +235,21 @@ void vpmu_initialise(struct vcpu *v)
     case X86_VENDOR_AMD:
         if ( svm_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
             opt_vpmu_enabled = 0;
-        break;
+        return;
 
     case X86_VENDOR_INTEL:
         if ( vmx_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
             opt_vpmu_enabled = 0;
-        break;
+        return;
 
     default:
         printk("VPMU: Initialization failed. "
                "Unknown CPU vendor %d\n", vendor);
         opt_vpmu_enabled = 0;
-        break;
+        return;
     }
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
 }
 
 void vpmu_destroy(struct vcpu *v)
xen/include/asm-x86/hvm/vpmu.h +1 -0
index 2a713be..7ee0f01 100644
@@ -87,6 +87,7 @@ struct vpmu_struct {
 #define vpmu_is_set_all(_vpmu, _x)  (((_vpmu)->flags & (_x)) == (_x))
 #define vpmu_clear(_vpmu)           ((_vpmu)->flags = 0)
 
+void vpmu_lvtpc_update(uint32_t val);
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content);
 int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content);
 int vpmu_do_interrupt(struct cpu_user_regs *regs);
Signature · 1 line
1.8.1.4
#9 lore raw

Re: [PATCH v3 05/16] x86/VPMU: Handle APIC_LVTPC accesses

Quoted 131 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Update APIC_LVTPC vector when HVM guest writes to it.

---
 xen/arch/x86/hvm/svm/vpmu.c       |  4 ----
 xen/arch/x86/hvm/vlapic.c         |  5 ++++-
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 17 -----------------
 xen/arch/x86/hvm/vpmu.c           | 16 +++++++++++++---
 xen/include/asm-x86/hvm/vpmu.h    |  1 +
 5 files changed, 18 insertions(+), 25 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +0 -2
index 842bce7..1f7d6b7 100644
@@ -290,8 +290,6 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t 
 msr_content)
         if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
             return 1;
         vpmu_set(vpmu, VPMU_RUNNING);
-        apic_write(APIC_LVTPC, PMU_APIC_VECTOR);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
 
         if ( is_hvm_domain(v->domain) &&
              !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
xen/arch/x86/hvm/svm/vpmu.c +0 -2
@@ -302,8 +300,6 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t 
 msr_content)
     if ( (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
         (is_pmu_enabled(msr_content) == 0) && vpmu_is_set(vpmu, VPMU_RUNNING) 
 )
     {
-        apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
         vpmu_reset(vpmu, VPMU_RUNNING);
         if ( is_hvm_domain(v->domain) &&
              ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
xen/arch/x86/hvm/vlapic.c +4 -1
index bc06010..d954f4f 100644
@@ -38,6 +38,7 @@
 #include <asm/hvm/support.h>
 #include <asm/hvm/vmx/vmx.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/hvm/vpmu.h>
 #include <public/hvm/ioreq.h>
 #include <public/hvm/params.h>
 
@@ -732,8 +733,10 @@ static int vlapic_reg_write(struct vcpu *v,
             vlapic_adjust_i8259_target(v->domain);
             pt_may_unmask_irq(v->domain, NULL);
         }
-        if ( (offset == APIC_LVTT) && !(val & APIC_LVT_MASKED) )
+        else if ( (offset == APIC_LVTT) && !(val & APIC_LVT_MASKED) )
             pt_may_unmask_irq(NULL, &vlapic->pt);
+        else if ( offset == APIC_LVTPC )
+            vpmu_lvtpc_update(val);
         break;
 
     case APIC_TMICT:
xen/arch/x86/hvm/vmx/vpmu_core2.c +0 -13
index 89212ec..7fd2420 100644
@@ -532,19 +532,6 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, 
 uint64_t msr_content)
     else
         vpmu_reset(vpmu, VPMU_RUNNING);
 
-    /* Setup LVTPC in local apic */
-    if ( vpmu_is_set(vpmu, VPMU_RUNNING) &&
-         is_vlapic_lvtpc_enabled(vcpu_vlapic(v)) )
-    {
-        apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
-    }
-    else
-    {
-        apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
-    }
-
     if ( type != MSR_TYPE_GLOBAL )
     {
         u64 mask;
xen/arch/x86/hvm/vmx/vpmu_core2.c +0 -4
@@ -710,10 +697,6 @@ static int core2_vpmu_do_interrupt(struct cpu_user_regs 
 *regs)
             return 0;
     }
 
-    /* HW sets the MASK bit when performance counter interrupt occurs*/
-    vpmu->hw_lapic_lvtpc = apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED;
-    apic_write_around(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
-
     return 1;
 }
xen/arch/x86/hvm/vpmu.c +10 -2
index d6a9ff6..2c1201b 100644
@@ -64,6 +64,14 @@ static void __init parse_vpmu_param(char *s)
     }
 }
 
+void vpmu_lvtpc_update(uint32_t val)
+{
+    struct vpmu_struct *vpmu = vcpu_vpmu(current);
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
+    apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+}
+
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
@@ -227,19 +235,21 @@ void vpmu_initialise(struct vcpu *v)
     case X86_VENDOR_AMD:
         if ( svm_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
             opt_vpmu_enabled = 0;
-        break;
+        return;
 
     case X86_VENDOR_INTEL:
         if ( vmx_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
             opt_vpmu_enabled = 0;
-        break;
+        return;
 

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 253

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +3 -1
     default:
         printk("VPMU: Initialization failed. "
                "Unknown CPU vendor %d\n", vendor);
         opt_vpmu_enabled = 0;
-        break;
+        return;
     }
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
 }

So what is this good for? All code paths above use "return" now, hence how would execution get here?

Jan

#10 lore raw

Re: [PATCH v3 05/16] x86/VPMU: Handle APIC_LVTPC accesses

Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Update APIC_LVTPC vector when HVM guest writes to it.

...

Quoted 24 lines
Quoted 24 lines
xen/arch/x86/hvm/vpmu.c +10 -2
+void vpmu_lvtpc_update(uint32_t val)
+{
+    struct vpmu_struct *vpmu = vcpu_vpmu(current);
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
+    apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+}
+
  int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
  {
      struct vpmu_struct *vpmu = vcpu_vpmu(current);
@@ -227,19 +235,21 @@ void vpmu_initialise(struct vcpu *v)
      case X86_VENDOR_AMD:
          if ( svm_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
              opt_vpmu_enabled = 0;
-        break;
+        return;
 
      case X86_VENDOR_INTEL:
          if ( vmx_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
              opt_vpmu_enabled = 0;
-        break;
+        return;
 
Quoted 12 lines

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 253

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +3 -1
      default:
          printk("VPMU: Initialization failed. "
                 "Unknown CPU vendor %d\n", vendor);
          opt_vpmu_enabled = 0;
-        break;
+        return;
      }
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
  }

So what is this good for? All code paths above use "return" now, hence how would execution get here?

Yes, it shouldn't be here. I could move it up but since we are now updating LVTPC explicitly when a guest is writing APIC I think I can just drop it altogether.

-boris

#11 lore raw

Re: [PATCH v3 05/16] x86/VPMU: Handle APIC_LVTPC accesses

Quoted 46 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Update APIC_LVTPC vector when HVM guest writes to it.

...

Quoted 24 lines
Quoted 24 lines
xen/arch/x86/hvm/vpmu.c +10 -2
+void vpmu_lvtpc_update(uint32_t val)
+{
+    struct vpmu_struct *vpmu = vcpu_vpmu(current);
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
+    apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+}
+
  int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
  {
      struct vpmu_struct *vpmu = vcpu_vpmu(current);
@@ -227,19 +235,21 @@ void vpmu_initialise(struct vcpu *v)
      case X86_VENDOR_AMD:
          if ( svm_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
              opt_vpmu_enabled = 0;
-        break;
+        return;
 
      case X86_VENDOR_INTEL:
          if ( vmx_vpmu_initialise(v, opt_vpmu_enabled) != 0 )
              opt_vpmu_enabled = 0;
-        break;
+        return;
 
Quoted 12 lines

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 253

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +3 -1
      default:
          printk("VPMU: Initialization failed. "
                 "Unknown CPU vendor %d\n", vendor);
          opt_vpmu_enabled = 0;
-        break;
+        return;
      }
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
  }

So what is this good for? All code paths above use "return" now, hence how would execution get here?

Quoted 3 lines

Yes, it shouldn't be here. I could move it up but since we are now updating LVTPC explicitly when a guest is writing APIC I think I can just drop it altogether.

As long as the mask bit gets properly set somewhere, not adding it here or anywhere else seems fine to me.

Jan

#12 lore raw

PATCH v3 6/16 intel/VPMU: MSR_CORE_PERF_GLOBAL_CTRL should be initialized to zero

In reply to Boris Ostrovsky

MSR_CORE_PERF_GLOBAL_CTRL register should be set zero initially. It is up to the guest to set it so that counters are enabled.

---
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -9
index 7fd2420..6b4cd61 100644
@@ -164,13 +164,6 @@ static int core2_get_fixed_pmc_count(void)
     return ( (eax & PMU_FIXED_NR_MASK) >> PMU_FIXED_NR_SHIFT );
 }
 
-static u64 core2_calc_intial_glb_ctrl_msr(void)
-{
-    int arch_pmc_bits = (1 << arch_pmc_cnt) - 1;
-    u64 fix_pmc_bits  = (1 << fixed_pmc_cnt) - 1;
-    return ( (fix_pmc_bits << 32) | arch_pmc_bits );
-}
-
 /* edx bits 5-12: Bit width of fixed-function performance counters  */
 static int core2_get_bitwidth_fix_count(void)
 {
@@ -371,8 +364,7 @@ static int core2_vpmu_alloc_resource(struct vcpu *v)
 
     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());
+    vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
 
     core2_vpmu_cxt = xzalloc_bytes(sizeof(struct core2_vpmu_context) +
                     (arch_pmc_cnt-1)*sizeof(struct arch_msr_pair));
Signature · 1 line
1.8.1.4
#13 lore raw

PATCH v3 7/16 x86/VPMU: Add public xenpmu.h

In reply to Boris Ostrovsky

Add xenpmu.h header file, move various macros and structures that will be shared between hypervisor and PV guests to it.

Move MSR banks out of architectural PMU structures to allow for larger sizes in the future. The banks are allocated immediately after the context and PMU structures store offsets to them.

---
 xen/arch/x86/hvm/svm/vpmu.c              | 47 +++++++++++---------
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 70 +++++++++++++++---------------
 xen/arch/x86/hvm/vpmu.c                  |  1 +
 xen/arch/x86/oprofile/op_model_ppro.c    |  6 ++-
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h | 32 --------------
 xen/include/asm-x86/hvm/vpmu.h           | 10 +----
 xen/include/public/arch-x86/xenpmu.h     | 74 ++++++++++++++++++++++++++++++++
 xen/include/public/xenpmu.h              | 38 ++++++++++++++++
 8 files changed, 183 insertions(+), 95 deletions(-)
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 create mode 100644 xen/include/public/arch-x86/xenpmu.h
 create mode 100644 xen/include/public/xenpmu.h
xen/arch/x86/hvm/svm/vpmu.c +27 -20
index 1f7d6b7..78979aa 100644
@@ -30,10 +30,7 @@
 #include <asm/apic.h>
 #include <asm/hvm/vlapic.h>
 #include <asm/hvm/vpmu.h>
-
-#define F10H_NUM_COUNTERS 4
-#define F15H_NUM_COUNTERS 6
-#define MAX_NUM_COUNTERS F15H_NUM_COUNTERS
+#include <public/xenpmu.h>
 
 #define MSR_F10H_EVNTSEL_GO_SHIFT   40
 #define MSR_F10H_EVNTSEL_EN_SHIFT   22
@@ -49,6 +46,10 @@ static const u32 __read_mostly *counters;
 static const u32 __read_mostly *ctrls;
 static bool_t __read_mostly k7_counters_mirrored;
 
+#define F10H_NUM_COUNTERS   4
+#define F15H_NUM_COUNTERS   6
+#define AMD_MAX_COUNTERS    6
+
 /* PMU Counter MSRs. */
 static const u32 AMD_F10H_COUNTERS[] = {
     MSR_K7_PERFCTR0,
@@ -83,13 +84,6 @@ static const u32 AMD_F15H_CTRLS[] = {
     MSR_AMD_FAM15H_EVNTSEL5
 };
 
-/* storage for context switching */
-struct amd_vpmu_context {
-    u64 counters[MAX_NUM_COUNTERS];
-    u64 ctrls[MAX_NUM_COUNTERS];
-    bool_t msr_bitmap_set;
-};
-
 static inline int get_pmu_reg_type(u32 addr)
 {
     if ( (addr >= MSR_K7_EVNTSEL0) && (addr <= MSR_K7_EVNTSEL3) )
@@ -178,11 +172,13 @@ static inline void context_load(struct vcpu *v)
     unsigned int i;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     struct amd_vpmu_context *ctxt = vpmu->context;
+    uint64_t *counter_regs = vpmu_reg_pointer(ctxt, counters);
+    uint64_t *ctrl_regs = vpmu_reg_pointer(ctxt, ctrls);
 
     for ( i = 0; i < num_counters; i++ )
     {
-        wrmsrl(counters[i], ctxt->counters[i]);
-        wrmsrl(ctrls[i], ctxt->ctrls[i]);
+        wrmsrl(counters[i], counter_regs[i]);
+        wrmsrl(ctrls[i], ctrl_regs[i]);
     }
 }
 
@@ -196,9 +192,10 @@ static void amd_vpmu_load(struct vcpu *v)
     if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
     {
         unsigned int i;
+	uint64_t *ctrl_regs = vpmu_reg_pointer(ctxt, ctrls);
 
         for ( i = 0; i < num_counters; i++ )
-            wrmsrl(ctrls[i], ctxt->ctrls[i]);
+            wrmsrl(ctrls[i], ctrl_regs[i]);
 
         return;
     }
@@ -211,10 +208,11 @@ static inline void context_save(struct vcpu *v)
     unsigned int i;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     struct amd_vpmu_context *ctxt = vpmu->context;
+    uint64_t *counter_regs = vpmu_reg_pointer(ctxt, counters);
 
     /* No need to save controls -- they are saved in amd_vpmu_do_wrmsr */
     for ( i = 0; i < num_counters; i++ )
-        rdmsrl(counters[i], ctxt->counters[i]);
+        rdmsrl(counters[i], counter_regs[i]);
 }
 
 static int amd_vpmu_save(struct vcpu *v)
@@ -249,6 +247,8 @@ static void context_update(unsigned int msr, u64 msr_content)
     struct vcpu *v = current;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     struct amd_vpmu_context *ctxt = vpmu->context;
+    uint64_t *counter_regs = vpmu_reg_pointer(ctxt, counters);
+    uint64_t *ctrl_regs = vpmu_reg_pointer(ctxt, ctrls);
 
     if ( k7_counters_mirrored &&
         ((msr >= MSR_K7_EVNTSEL0) && (msr <= MSR_K7_PERFCTR3)) )
@@ -260,12 +260,12 @@ static void context_update(unsigned int msr, u64 msr_content)
     {
        if ( msr == ctrls[i] )
        {
-           ctxt->ctrls[i] = msr_content;
+           ctrl_regs[i] = msr_content;
            return;
        }
         else if (msr == counters[i] )
         {
-            ctxt->counters[i] = msr_content;
+            counter_regs[i] = msr_content;
             return;
         }
     }
@@ -373,7 +373,9 @@ static int amd_vpmu_initialise(struct vcpu *v)
 	 }
     }
 
-    ctxt = xzalloc(struct amd_vpmu_context);
+    ctxt = xzalloc_bytes(sizeof(struct amd_vpmu_context) + 
+			 sizeof(uint64_t) * AMD_MAX_COUNTERS + 
+			 sizeof(uint64_t) * AMD_MAX_COUNTERS);
     if ( !ctxt )
     {
         gdprintk(XENLOG_WARNING, "Insufficient memory for PMU, "
@@ -382,6 +384,9 @@ static int amd_vpmu_initialise(struct vcpu *v)
         return -ENOMEM;
     }
 
+    ctxt->counters = sizeof(struct amd_vpmu_context);
+    ctxt->ctrls = ctxt->counters + sizeof(uint64_t) * AMD_MAX_COUNTERS;
+
     vpmu->context = ctxt;
     vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
     return 0;
@@ -413,6 +418,8 @@ static void amd_vpmu_dump(const struct vcpu *v)
 {
     const struct vpmu_struct *vpmu = vcpu_vpmu(v);
     const struct amd_vpmu_context *ctxt = vpmu->context;
+    uint64_t *counter_regs = vpmu_reg_pointer(ctxt, counters);
+    uint64_t *ctrl_regs = vpmu_reg_pointer(ctxt, ctrls);
     unsigned int i;
 
     printk("    VPMU state: 0x%x ", vpmu->flags);
@@ -442,8 +449,8 @@ static void amd_vpmu_dump(const struct vcpu *v)
         rdmsrl(ctrls[i], ctrl);
         rdmsrl(counters[i], cntr);
         printk("      %#x: %#lx (%#lx in HW)    %#x: %#lx (%#lx in HW)\n",
-               ctrls[i], ctxt->ctrls[i], ctrl,
-               counters[i], ctxt->counters[i], cntr);
+               ctrls[i], ctrl_regs[i], ctrl,
+               counters[i], counter_regs[i], cntr);
     }
 }
 
xen/arch/x86/hvm/vmx/vpmu_core2.c +36 -34

179 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +1 -0
index 2c1201b..99eb654 100644
@@ -31,6 +31,7 @@
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
+#include <public/xenpmu.h>
 
 /*
  * "vpmu" :     vpmu generally enabled
xen/arch/x86/oprofile/op_model_ppro.c +5 -1
index 3225937..5aae2e7 100644
@@ -20,11 +20,15 @@
 #include <asm/regs.h>
 #include <asm/current.h>
 #include <asm/hvm/vpmu.h>
-#include <asm/hvm/vmx/vpmu_core2.h>
 
 #include "op_x86_model.h"
 #include "op_counter.h"
 
+struct arch_msr_pair {
+    u64 counter;
+    u64 control;
+};
+
 /*
  * Intel "Architectural Performance Monitoring" CPUID
  * detection/enumeration details:
xen/include/asm-x86/hvm/vmx/vpmu_core2.h deleted +0 -32
deleted file mode 100644
index 410372d..0000000
@@ -1,32 +0,0 @@
-
-/*
- * vpmu_core2.h: CORE 2 specific PMU virtualization for HVM domain.
- *
- * Copyright (c) 2007, Intel Corporation.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms and conditions of the GNU General Public License,
- * version 2, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307 USA.
- *
- * Author: Haitao Shan <[email protected]>
- */
-
-#ifndef __ASM_X86_HVM_VPMU_CORE_H_
-#define __ASM_X86_HVM_VPMU_CORE_H_
-
-struct arch_msr_pair {
-    u64 counter;
-    u64 control;
-};
-
-#endif /* __ASM_X86_HVM_VPMU_CORE_H_ */
-
xen/include/asm-x86/hvm/vpmu.h +2 -8
index 7ee0f01..2698686 100644
@@ -22,6 +22,8 @@
 #ifndef __ASM_X86_HVM_VPMU_H_
 #define __ASM_X86_HVM_VPMU_H_
 
+#include <public/xenpmu.h>
+
 /*
  * Flag bits given as a string on the hypervisor boot parameter 'vpmu'.
  * See arch/x86/hvm/vpmu.c.
@@ -29,12 +31,9 @@
 #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, \
                                           arch.hvm_vcpu.vpmu))
-#define vpmu_domain(vpmu) (vpmu_vcpu(vpmu)->domain)
 
 #define MSR_TYPE_COUNTER            0
 #define MSR_TYPE_CTRL               1
@@ -76,11 +75,6 @@ struct vpmu_struct {
 #define VPMU_FROZEN                         0x10  /* Stop counters while VCPU is not running */
 #define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x20
 
-/* VPMU features */
-#define VPMU_CPU_HAS_DS                     0x100 /* Has Debug Store */
-#define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
-
-
 #define vpmu_set(_vpmu, _x)         ((_vpmu)->flags |= (_x))
 #define vpmu_reset(_vpmu, _x)       ((_vpmu)->flags &= ~(_x))
 #define vpmu_is_set(_vpmu, _x)      ((_vpmu)->flags & (_x))
xen/include/public/arch-x86/xenpmu.h new file +74 -0

74 lines not on this page · raw message

xen/include/public/xenpmu.h new file +38 -0
new file mode 100644
index 0000000..bcadd15
@@ -0,0 +1,38 @@
+#ifndef __XEN_PUBLIC_XENPMU_H__
+#define __XEN_PUBLIC_XENPMU_H__
+
+#include "xen.h"
+#if defined(__i386__) || defined(__x86_64__)
+#include "arch-x86/xenpmu.h"
+#elif defined (__arm__) || defined (__aarch64__)
+#include "arch-arm.h"
+#else
+#error "Unsupported architecture"
+#endif
+
+#define XENPMU_VER_MAJ    0
+#define XENPMU_VER_MIN    0
+
+
+/* Shared between hypervisor and PV domain */
+struct xenpmu_data {
+    uint32_t domain_id;
+    uint32_t vcpu_id;
+    uint32_t pcpu_id;
+    uint32_t pmu_flags;
+
+    arch_xenpmu_t pmu;
+};
+typedef struct xenpmu_data xenpmu_data_t;
+
+#endif /* __XEN_PUBLIC_XENPMU_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 1 line
1.8.1.4
#14 lore raw

Re: [PATCH v3 07/16] x86/VPMU: Add public xenpmu.h

xen/include/public/arch-x86/xenpmu.h line 5

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/public/arch-x86/xenpmu.h new file +5 -0
@@ -0,0 +1,74 @@
+#ifndef __XEN_PUBLIC_ARCH_X86_PMU_H__
+#define __XEN_PUBLIC_ARCH_X86_PMU_H__
+
+/* x86-specific PMU definitions */
+#include "xen.h"

The comment looks like it is describing the #include, which it clearly isn't. Please have a blank line between them.

xen/include/public/arch-x86/xenpmu.h line 9

Quoted 4 lines
xen/include/public/arch-x86/xenpmu.h new file +4 -0
+
+/* Start of PMU register bank */
+#define vpmu_reg_pointer(ctxt, offset) ((void *)((uintptr_t)ctxt + \
+                                                 (uintptr_t)ctxt->offset))

This doesn't seem to belong in a public header.

xen/include/public/arch-x86/xenpmu.h line 16

Quoted 7 lines
xen/include/public/arch-x86/xenpmu.h new file +7 -0
+
+/* AMD PMU registers and structures */
+struct amd_vpmu_context {
+    uint64_t counters;      /* Offset to counter MSRs */
+    uint64_t ctrls;         /* Offset to control MSRs */
+    uint8_t msr_bitmap_set; /* Used by HVM only */
+};

sizeof() of this structure will differ between 32- and 64-bit guests - are you intending to do the necessary translation even though it seems rather easy to avoid having to do so?

xen/include/public/arch-x86/xenpmu.h line 23

Quoted 7 lines
xen/include/public/arch-x86/xenpmu.h new file +7 -0
+
+/* Intel PMU registers and structures */
+struct arch_cntr_pair {
+    uint64_t counter;
+    uint64_t control;
+};
+struct core2_vpmu_context {

Blank line missing between the two structures.

Quoted 7 lines
xen/include/public/arch-x86/xenpmu.h new file +7 -0
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t fixed_ctrl;
+    uint64_t ds_area;
+    uint64_t pebs_enable;
+    uint64_t debugctl;

xen/include/public/arch-x86/xenpmu.h line 40

Quoted 10 lines
xen/include/public/arch-x86/xenpmu.h new file +10 -0
+    uint64_t fixed_counters;  /* Offset to fixed counter MSRs */
+    uint64_t arch_counters;   /* Offset to architectural counter MSRs */
+};
+
+/* ANSI-C does not support anonymous unions */
+#if !defined(__GNUC__) || defined(__STRICT_ANSI__)
+#define __ANON_UNION_NAME(x) x
+#else
+#define __ANON_UNION_NAME(x)
+#endif

Why? And if really needed, why here?

Quoted 10 lines
xen/include/public/arch-x86/xenpmu.h new file +10 -0
+
+#define XENPMU_MAX_CTXT_SZ        (sizeof(struct amd_vpmu_context) > \
+                                    sizeof(struct core2_vpmu_context) ? \
+                                     sizeof(struct amd_vpmu_context) : \
+                                     sizeof(struct core2_vpmu_context))
+#define XENPMU_CTXT_PAD_SZ        (((XENPMU_MAX_CTXT_SZ + 64) & ~63) + 128)
+struct arch_xenpmu {
+    union {
+        struct cpu_user_regs regs;
+        uint8_t pad1[256];

xen/include/public/arch-x86/xenpmu.h line 60

Quoted 10 lines
xen/include/public/arch-x86/xenpmu.h new file +10 -0
+    } __ANON_UNION_NAME(r);
+    union {
+        uint32_t lapic_lvtpc;
+        uint64_t pad2;
+    } __ANON_UNION_NAME(l);
+    union {
+        struct amd_vpmu_context amd;
+        struct core2_vpmu_context intel;
+        uint8_t pad3[XENPMU_CTXT_PAD_SZ];
+    } __ANON_UNION_NAME(c);

I don't think there's be a severe problem if you simply always had names on these unions.

xen/include/public/arch-x86/xenpmu.h line 62

Quoted 2 lines
xen/include/public/arch-x86/xenpmu.h new file +2 -0
+};
+typedef struct arch_xenpmu arch_xenpmu_t;

Overall you should also prefix all types added to global scope with "xen". I know this wasn't done consistently for older headers, but we shouldn't be extending this name space cluttering.

Jan

#15 lore raw

Re: [PATCH v3 07/16] x86/VPMU: Add public xenpmu.h

Quoted 3 lines · Jan Beulich
Quoted 3 lines
xen/include/public/arch-x86/xenpmu.h new file +0 -0
@@ -0,0 +1,74 @@

....

Quoted 10 lines

xen/include/public/arch-x86/xenpmu.h line 16

Quoted 7 lines
xen/include/public/arch-x86/xenpmu.h new file +7 -0
+
+/* AMD PMU registers and structures */
+struct amd_vpmu_context {
+    uint64_t counters;      /* Offset to counter MSRs */
+    uint64_t ctrls;         /* Offset to control MSRs */
+    uint8_t msr_bitmap_set; /* Used by HVM only */
+};

sizeof() of this structure will differ between 32- and 64-bit guests - are you intending to do the necessary translation even though it seems rather easy to avoid having to do so?

'msr_bitmap_set' field is actually never used by PV and it's the last one in the structure which is why I didn't bother to make it bigger. But you are right, I should fix this to avoid problems in the future.

Quoted 20 lines

xen/include/public/arch-x86/xenpmu.h line 23

Quoted 8 lines
xen/include/public/arch-x86/xenpmu.h new file +7 -0
+
+/* Intel PMU registers and structures */
+struct arch_cntr_pair {
+    uint64_t counter;
+    uint64_t control;
+};
+struct core2_vpmu_context {

Blank line missing between the two structures.

Quoted 10 lines
xen/include/public/arch-x86/xenpmu.h new file +10 -0
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t fixed_ctrl;
+    uint64_t ds_area;
+    uint64_t pebs_enable;
+    uint64_t debugctl;
+    uint64_t fixed_counters;  /* Offset to fixed counter MSRs */
+    uint64_t arch_counters;   /* Offset to architectural counter MSRs */
+};
Quoted 8 lines

xen/include/public/arch-x86/xenpmu.h line 40

Quoted 7 lines
xen/include/public/arch-x86/xenpmu.h new file +7 -0
+
+/* ANSI-C does not support anonymous unions */
+#if !defined(__GNUC__) || defined(__STRICT_ANSI__)
+#define __ANON_UNION_NAME(x) x
+#else
+#define __ANON_UNION_NAME(x)
+#endif

Why? And if really needed, why here?

I'll drop this. I thought anonymous unions looked better but now that I look at it again I think the ifdefs are rather ugly too.

Quoted 24 lines
Quoted 11 lines
xen/include/public/arch-x86/xenpmu.h new file +10 -0
+
+#define XENPMU_MAX_CTXT_SZ        (sizeof(struct amd_vpmu_context) > \
+                                    sizeof(struct core2_vpmu_context) ? \
+                                     sizeof(struct amd_vpmu_context) : \
+                                     sizeof(struct core2_vpmu_context))
+#define XENPMU_CTXT_PAD_SZ        (((XENPMU_MAX_CTXT_SZ + 64) & ~63) + 128)
+struct arch_xenpmu {
+    union {
+        struct cpu_user_regs regs;
+        uint8_t pad1[256];

xen/include/public/arch-x86/xenpmu.h line 60

Quoted 10 lines
xen/include/public/arch-x86/xenpmu.h new file +10 -0
+    } __ANON_UNION_NAME(r);
+    union {
+        uint32_t lapic_lvtpc;
+        uint64_t pad2;
+    } __ANON_UNION_NAME(l);
+    union {
+        struct amd_vpmu_context amd;
+        struct core2_vpmu_context intel;
+        uint8_t pad3[XENPMU_CTXT_PAD_SZ];
+    } __ANON_UNION_NAME(c);

I don't think there's be a severe problem if you simply always had names on these unions.

Quoted 5 lines

xen/include/public/arch-x86/xenpmu.h

Quoted 2 lines
xen/include/public/arch-x86/xenpmu.h new file +2 -0
+};
+typedef struct arch_xenpmu arch_xenpmu_t;

Overall you should also prefix all types added to global scope with "xen". I know this wasn't done consistently for older headers, but we shouldn't be extending this name space cluttering.

You mean something like arch_xenpmu ==> xen_arch_pmu ?

-boris

#16 lore raw

Re: [PATCH v3 07/16] x86/VPMU: Add public xenpmu.h

Quoted 7 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 3 lines · Jan Beulich

Overall you should also prefix all types added to global scope with "xen". I know this wasn't done consistently for older headers, but we shouldn't be extending this name space cluttering.

You mean something like arch_xenpmu ==> xen_arch_pmu ?

Yes.

Jan

#17 lore raw

PATCH v3 8/16 x86/VPMU: Make vpmu not HVM-specific

In reply to Boris Ostrovsky

vpmu structure will be used for both HVM and PV guests. Move it from hvm_vcpu to arch_vcpu.

---
 xen/include/asm-x86/domain.h   | 2 ++
 xen/include/asm-x86/hvm/vcpu.h | 3 ---
 xen/include/asm-x86/hvm/vpmu.h | 5 ++---
 3 files changed, 4 insertions(+), 6 deletions(-)
xen/include/asm-x86/domain.h +2 -0
index 9d39061..f352a84 100644
@@ -396,6 +396,8 @@ struct arch_vcpu
     void (*ctxt_switch_from) (struct vcpu *);
     void (*ctxt_switch_to) (struct vcpu *);
 
+    struct vpmu_struct vpmu;
+
     /* Virtual Machine Extensions */
     union {
         struct pv_vcpu pv_vcpu;
xen/include/asm-x86/hvm/vcpu.h +0 -3
index 122ab0d..9beeaa9 100644
@@ -152,9 +152,6 @@ struct hvm_vcpu {
     u32                 msr_tsc_aux;
     u64                 msr_tsc_adjust;
 
-    /* VPMU */
-    struct vpmu_struct  vpmu;
-
     union {
         struct arch_vmx_struct vmx;
         struct arch_svm_struct svm;
xen/include/asm-x86/hvm/vpmu.h +2 -3
index 2698686..6d641ea 100644
@@ -31,9 +31,8 @@
 #define VPMU_BOOT_ENABLED 0x1    /* vpmu generally enabled. */
 #define VPMU_BOOT_BTS     0x2    /* Intel BTS feature wanted. */
 
-#define vcpu_vpmu(vcpu)   (&((vcpu)->arch.hvm_vcpu.vpmu))
-#define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, \
-                                          arch.hvm_vcpu.vpmu))
+#define vcpu_vpmu(vcpu)   (&((vcpu)->arch.vpmu))
+#define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, arch.vpmu))
 
 #define MSR_TYPE_COUNTER            0
 #define MSR_TYPE_CTRL               1
Signature · 1 line
1.8.1.4
#18 lore raw

PATCH v3 9/16 x86/VPMU: Interface for setting PMU mode and flags

In reply to Boris Ostrovsky

Add runtime interface for setting PMU mode and flags. Three main modes are provided:
* PMU off
* PMU on: Guests can access PMU MSRs and receive PMU interrupts. dom0 profiles itself and the hypervisor.
* dom0-only PMU: dom0 collects samples for both itself and guests.

For feature flags only Intel's BTS is currently supported.

Mode and flags are set via HYPERVISOR_xenpmu_op hypercall.

---
 xen/arch/x86/hvm/svm/vpmu.c        |  2 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c  |  4 +-
 xen/arch/x86/hvm/vpmu.c            | 77 ++++++++++++++++++++++++++++++++++----
 xen/arch/x86/x86_64/compat/entry.S |  4 ++
 xen/arch/x86/x86_64/entry.S        |  4 ++
 xen/include/asm-x86/hvm/vpmu.h     |  9 +----
 xen/include/public/xen.h           |  1 +
 xen/include/public/xenpmu.h        | 58 ++++++++++++++++++++++++++++
 xen/include/xen/hypercall.h        |  4 ++
 9 files changed, 145 insertions(+), 18 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +1 -1
index 78979aa..1369d74 100644
@@ -471,7 +471,7 @@ int svm_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
     int ret = 0;
 
     /* vpmu enabled? */
-    if ( !vpmu_flags )
+    if ( vpmu_flags == XENPMU_MODE_OFF )
         return 0;
 
     switch ( family )
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -2
index e756d87..5670924 100644
@@ -706,7 +706,7 @@ static int core2_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
     u64 msr_content;
     struct cpuinfo_x86 *c = &current_cpu_data;
 
-    if ( !(vpmu_flags & VPMU_BOOT_BTS) )
+    if ( !(vpmu_flags & (XENPMU_FEATURE_INTEL_BTS << XENPMU_FEATURE_SHIFT)) )
         goto func_out;
     /* Check the 'Debug Store' feature in the CPUID.EAX[1]:EDX[21] */
     if ( cpu_has(c, X86_FEATURE_DS) )
@@ -825,7 +825,7 @@ int vmx_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
     int ret = 0;
 
     vpmu->arch_vpmu_ops = &core2_no_vpmu_ops;
-    if ( !vpmu_flags )
+    if ( vpmu_flags == XENPMU_MODE_OFF )
         return 0;
 
     if ( family == 6 )
xen/arch/x86/hvm/vpmu.c +69 -8

118 lines not on this page · raw message

xen/arch/x86/x86_64/compat/entry.S +4 -0
index 594b0b9..07c736d 100644
@@ -416,6 +416,8 @@ ENTRY(compat_hypercall_table)
         .quad do_domctl
         .quad compat_kexec_op
         .quad do_tmem_op
+        .quad do_ni_hypercall           /* reserved for XenClient */
+        .quad do_xenpmu_op              /* 40 */
         .rept __HYPERVISOR_arch_0-((.-compat_hypercall_table)/8)
         .quad compat_ni_hypercall
         .endr
@@ -464,6 +466,8 @@ ENTRY(compat_hypercall_args_table)
         .byte 1 /* do_domctl                */
         .byte 2 /* compat_kexec_op          */
         .byte 1 /* do_tmem_op               */
+        .byte 0 /* reserved for XenClient   */
+        .byte 2 /* do_xenpmu_op             */  /* 40 */
         .rept __HYPERVISOR_arch_0-(.-compat_hypercall_args_table)
         .byte 0 /* compat_ni_hypercall      */
         .endr
xen/arch/x86/x86_64/entry.S +4 -0
index 3ea4683..c36ffce 100644
@@ -757,6 +757,8 @@ ENTRY(hypercall_table)
         .quad do_domctl
         .quad do_kexec_op
         .quad do_tmem_op
+        .quad do_ni_hypercall       /* reserved for XenClient */
+        .quad do_xenpmu_op          /* 40 */
         .rept __HYPERVISOR_arch_0-((.-hypercall_table)/8)
         .quad do_ni_hypercall
         .endr
@@ -805,6 +807,8 @@ ENTRY(hypercall_args_table)
         .byte 1 /* do_domctl            */
         .byte 2 /* do_kexec             */
         .byte 1 /* do_tmem_op           */
+        .byte 0 /* reserved for XenClient */
+        .byte 2 /* do_xenpmu_op         */  /* 40 */
         .rept __HYPERVISOR_arch_0-(.-hypercall_args_table)
         .byte 0 /* do_ni_hypercall      */
         .endr
xen/include/asm-x86/hvm/vpmu.h +2 -7
index 6d641ea..022a171 100644
@@ -24,13 +24,6 @@
 
 #include <public/xenpmu.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 vcpu_vpmu(vcpu)   (&((vcpu)->arch.vpmu))
 #define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, arch.vpmu))
 
@@ -95,5 +88,7 @@ void vpmu_dump(struct vcpu *v);
 extern int acquire_pmu_ownership(int pmu_ownership);
 extern void release_pmu_ownership(int pmu_ownership);
 
+extern uint32_t vpmu_mode;
+
 #endif /* __ASM_X86_HVM_VPMU_H_*/
 
xen/include/public/xen.h +1 -0
index 8c5697e..a00ab21 100644
@@ -101,6 +101,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define __HYPERVISOR_kexec_op             37
 #define __HYPERVISOR_tmem_op              38
 #define __HYPERVISOR_xc_reserved_op       39 /* reserved for XenClient */
+#define __HYPERVISOR_xenpmu_op            40
 
 /* Architecture-specific hypercall definitions. */
 #define __HYPERVISOR_arch_0               48
xen/include/public/xenpmu.h +58 -0

64 lines not on this page · raw message

xen/include/xen/hypercall.h +4 -0
index a9e5229..4b49084 100644
@@ -14,6 +14,7 @@
 #include <public/event_channel.h>
 #include <public/tmem.h>
 #include <public/version.h>
+#include <public/xenpmu.h>
 #include <asm/hypercall.h>
 #include <xsm/xsm.h>
 
@@ -139,6 +140,9 @@ do_tmem_op(
 extern long
 do_xenoprof_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);
 
+extern long
+do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xenpmu_params_t) arg);
+
 #ifdef CONFIG_COMPAT
 
 extern int
Signature · 1 line
1.8.1.4
#19 lore raw

Re: [PATCH v3 09/16] x86/VPMU: Interface for setting PMU mode and flags

Quoted 17 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/public/xenpmu.h +10 -0
@@ -13,6 +13,64 @@
 #define XENPMU_VER_MAJ    0
 #define XENPMU_VER_MIN    0
 
+/*
+ * ` enum neg_errnoval
+ * ` HYPERVISOR_xenpmu_op(enum xenpmu_op cmd, struct xenpmu_params *args);
+ *
+ * @cmd  == XENPMU_* (PMU operation)
+ * @args == struct xenpmu_params
+ */
+/* ` enum xenpmu_op { */
+#define XENPMU_mode_get        0 /* Also used for getting PMU version */
+#define XENPMU_mode_set        1

xen/include/public/xenpmu.h line 35

Quoted 10 lines
xen/include/public/xenpmu.h +10 -0
+#define XENPMU_feature_get     2
+#define XENPMU_feature_set     3
+/* ` } */
+
+/* ANSI-C does not support anonymous unions */
+#if !defined(__GNUC__) || defined(__STRICT_ANSI__)
+#define __ANON_UNION_NAME(x) x
+#else
+#define __ANON_UNION_NAME(x)
+#endif

Same comment as on the earlier incarnation in another patch of this series: This shouldn't be in a public header, or if it really needs to be, then properly prefixed.

Jan

#20 lore raw

PATCH v3 10/16 x86/VPMU: Initialize PMU for PV guests

In reply to Boris Ostrovsky

Code for initializing/tearing down PMU for PV guests

---
 xen/arch/x86/hvm/svm/vpmu.c       | 38 +++++++++++----------
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 51 ++++++++++++++++++----------
 xen/arch/x86/hvm/vpmu.c           | 71 ++++++++++++++++++++++++++++++++++++++-
 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       |  2 ++
 xen/include/xen/softirq.h         |  1 +
 8 files changed, 131 insertions(+), 35 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +21 -17
index 1369d74..f35aee2 100644
@@ -373,16 +373,21 @@ static int amd_vpmu_initialise(struct vcpu *v)
 	 }
     }
 
-    ctxt = xzalloc_bytes(sizeof(struct amd_vpmu_context) + 
-			 sizeof(uint64_t) * AMD_MAX_COUNTERS + 
-			 sizeof(uint64_t) * AMD_MAX_COUNTERS);
-    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_bytes(sizeof(struct amd_vpmu_context) + 
+                             sizeof(uint64_t) * AMD_MAX_COUNTERS + 
+                             sizeof(uint64_t) * AMD_MAX_COUNTERS);
+        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;
 
     ctxt->counters = sizeof(struct amd_vpmu_context);
     ctxt->ctrls = ctxt->counters + sizeof(uint64_t) * AMD_MAX_COUNTERS;
@@ -399,18 +404,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 */
xen/arch/x86/hvm/vmx/vpmu_core2.c +34 -17

73 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +70 -1

96 lines not on this page · raw message

xen/common/event_channel.c +1 -0
index 34efd24..daf381c 100644
@@ -108,6 +108,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:
xen/include/asm-x86/hvm/vpmu.h +1 -0
index 022a171..4b0ae38 100644
@@ -57,6 +57,7 @@ struct vpmu_struct {
     u32 hw_lapic_lvtpc;
     void *context;
     struct arch_vpmu_ops *arch_vpmu_ops;
+    xenpmu_data_t *xenpmu_data;
 };
 
 /* VPMU states */
xen/include/public/xen.h +1 -0
index a00ab21..2eb5fd7 100644
@@ -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
xen/include/public/xenpmu.h +2 -0
index faf05fc..37592f8 100644
@@ -25,6 +25,8 @@
 #define XENPMU_mode_set        1
 #define XENPMU_feature_get     2
 #define XENPMU_feature_set     3
+#define XENPMU_init            4
+#define XENPMU_finish          5
 /* ` } */
 
 /* ANSI-C does not support anonymous unions */
xen/include/xen/softirq.h +1 -0
index 0c0d481..5829fa4 100644
@@ -8,6 +8,7 @@ enum {
     NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ,
     RCU_SOFTIRQ,
     TASKLET_SOFTIRQ,
+    PMU_SOFTIRQ,
     NR_COMMON_SOFTIRQS
 };
 
Signature · 1 line
1.8.1.4
#21 lore raw

Re: [PATCH v3 10/16] x86/VPMU: Initialize PMU for PV guests

xen/arch/x86/hvm/svm/vpmu.c static int amd_vpmu_initialise(struct vcpu *v) · line 376

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/svm/vpmu.c +1 -4
@@ -373,16 +373,21 @@ static int amd_vpmu_initialise(struct vcpu *v)
 	 }
     }
 
-    ctxt = xzalloc_bytes(sizeof(struct amd_vpmu_context) + 
-			 sizeof(uint64_t) * AMD_MAX_COUNTERS + 
-			 sizeof(uint64_t) * AMD_MAX_COUNTERS);
-    if ( !ctxt )
+    if ( is_hvm_domain(v->domain) )

Here and elsewhere - did you consider whether is_hvm_domain() is still the correct thing here now that PVH code is in?

Jan

#22 lore raw

Re: [PATCH v3 10/16] x86/VPMU: Initialize PMU for PV guests

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/svm/vpmu.c +0 -0
@@ -373,16 +373,21 @@ static int amd_vpmu_initialise(struct vcpu *v)
  	 }
      }
 
Quoted 8 lines

xen/arch/x86/hvm/svm/vpmu.c static int amd_vpmu_initialise(struct vcpu *v) · line 376

Quoted 5 lines
xen/arch/x86/hvm/svm/vpmu.c +1 -4
-    ctxt = xzalloc_bytes(sizeof(struct amd_vpmu_context) +
-			 sizeof(uint64_t) * AMD_MAX_COUNTERS +
-			 sizeof(uint64_t) * AMD_MAX_COUNTERS);
-    if ( !ctxt )
+    if ( is_hvm_domain(v->domain) )

Here and elsewhere - did you consider whether is_hvm_domain() is still the correct thing here now that PVH code is in?

I actually know that this is broken --- I ran a PVH guest a couple of days ago and it exploded because of this check.

-boris

#23 lore raw

Re: [PATCH v3 10/16] x86/VPMU: Initialize PMU for PV guests

In reply to Boris Ostrovsky

xen/arch/x86/hvm/vmx/vpmu_core2.c func_out: · line 763

Quoted 8 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vmx/vpmu_core2.c +3 -0
@@ -750,6 +758,11 @@ func_out:
     fixed_pmc_cnt = core2_get_fixed_pmc_count();
     check_pmc_quirk();
 
+    /* PV domains can allocate resources immediately */
+    if ( !is_hvm_domain(v->domain) )
+        if ( !core2_vpmu_alloc_resource(v) )

Please use a single if() for conditions like this.

Jan

Quoted 5 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -0
+            return 1;
+
     return 0;
 }
 
#24 lore raw

PATCH v3 11/16 x86/VPMU: Add support for PMU register handling on PV guests

In reply to Boris Ostrovsky

Intercept accesses to PMU MSRs and process them in VPMU module.

Dump VPMU state for all domains (HVM and PV) when requested.

---
 xen/arch/x86/domain.c             |  3 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 60 ++++++++++++++++++++++++++++++++-------
 xen/arch/x86/hvm/vpmu.c           |  8 ++++++
 xen/arch/x86/traps.c              | 30 ++++++++++++++++++--
 xen/include/public/xenpmu.h       |  1 +
 5 files changed, 88 insertions(+), 14 deletions(-)
xen/arch/x86/domain.c +1 -2
index da8e522..25572d5 100644
@@ -1972,8 +1972,7 @@ void arch_dump_vcpu_info(struct vcpu *v)
 {
     paging_dump_vcpu_info(v);
 
-    if ( is_hvm_vcpu(v) )
-        vpmu_dump(v);
+    vpmu_dump(v);
 }
 
 void domain_cpuid(
xen/arch/x86/hvm/vmx/vpmu_core2.c +50 -10

146 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +8 -0
index 1a97b0e..2cc37cc 100644
@@ -396,6 +396,14 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xenpmu_params_t) arg)
             return -EFAULT;
         pvpmu_finish(current->domain, &pmu_params);
         break;
+
+    case XENPMU_lvtpc_set:
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        vpmu_lvtpc_update((uint32_t)pmu_params.val);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/arch/x86/traps.c +28 -2
index 3f7a3c7..8a3353f 100644
@@ -72,6 +72,7 @@
 #include <asm/apic.h>
 #include <asm/mc146818rtc.h>
 #include <asm/hpet.h>
+#include <asm/hvm/vpmu.h>
 #include <public/arch-x86/cpuid.h>
 #include <xsm/xsm.h>
 
@@ -866,7 +867,6 @@ void pv_cpuid(struct cpu_user_regs *regs)
         break;
 
     case 0x00000005: /* MONITOR/MWAIT */
-    case 0x0000000a: /* Architectural Performance Monitor Features */
     case 0x0000000b: /* Extended Topology Enumeration */
     case 0x8000000a: /* SVM revision and features */
     case 0x8000001b: /* Instruction Based Sampling */
@@ -875,7 +875,9 @@ void pv_cpuid(struct cpu_user_regs *regs)
     unsupported:
         a = b = c = d = 0;
         break;
-
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        vpmu_do_cpuid(0xa, &a, &b, &c, &d);
+        break;
     default:
         (void)cpuid_hypervisor_leaves(regs->eax, 0, &a, &b, &c, &d);
         break;
@@ -2499,6 +2501,14 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             if ( wrmsr_safe(regs->ecx, msr_content) != 0 )
                 goto fail;
             break;
+        case MSR_P6_PERFCTR0...MSR_P6_PERFCTR1:
+        case MSR_P6_EVNTSEL0...MSR_P6_EVNTSEL1:
+        case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+        case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( !vpmu_do_wrmsr(regs->ecx, msr_content) )
+                goto invalid;
+            break;
         default:
             if ( wrmsr_hypervisor_regs(regs->ecx, msr_content) == 1 )
                 break;
@@ -2587,6 +2597,22 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             regs->eax = (uint32_t)msr_content;
             regs->edx = (uint32_t)(msr_content >> 32);
             break;
+        case MSR_IA32_PERF_CAPABILITIES:
+            /* No extra capabilities are supported */
+            regs->eax = regs->edx = 0;
+            break;
+        case MSR_P6_PERFCTR0...MSR_P6_PERFCTR1:
+        case MSR_P6_EVNTSEL0...MSR_P6_EVNTSEL1:
+        case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+        case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( vpmu_do_rdmsr(regs->ecx, &msr_content) ) 
+            {
+                regs->eax = (uint32_t)msr_content;
+                regs->edx = (uint32_t)(msr_content >> 32);
+                break;
+            }
+            goto rdmsr_normal;
         default:
             if ( rdmsr_hypervisor_regs(regs->ecx, &val) )
             {
xen/include/public/xenpmu.h +1 -0
index 37592f8..bad5211 100644
@@ -27,6 +27,7 @@
 #define XENPMU_feature_set     3
 #define XENPMU_init            4
 #define XENPMU_finish          5
+#define XENPMU_lvtpc_set       6
 /* ` } */
 
 /* ANSI-C does not support anonymous unions */
Signature · 1 line
1.8.1.4
#25 lore raw

Re: [PATCH v3 11/16] x86/VPMU: Add support for PMU register handling on PV guests

Quoted 10 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/traps.c +0 -1
@@ -866,7 +867,6 @@ void pv_cpuid(struct cpu_user_regs *regs)
         break;
 
     case 0x00000005: /* MONITOR/MWAIT */
-    case 0x0000000a: /* Architectural Performance Monitor Features */
     case 0x0000000b: /* Extended Topology Enumeration */
     case 0x8000000a: /* SVM revision and features */
     case 0x8000001b: /* Instruction Based Sampling */

xen/arch/x86/traps.c void pv_cpuid(struct cpu_user_regs *regs) · line 881

Quoted 8 lines
xen/arch/x86/traps.c +3 -1
@@ -875,7 +875,9 @@ void pv_cpuid(struct cpu_user_regs *regs)
     unsupported:
         a = b = c = d = 0;
         break;
-
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        vpmu_do_cpuid(0xa, &a, &b, &c, &d);
+        break;
     default:

Rather than removing a blank line here, you ought to insert a second one so that there's one before _and_ after the added code block.

Furthermore the need to pass 0xa as the first argument suggests that you're not in line with the intentions of vpmu_do_cpuid():
Either you drop the first parameter from the function, or you get your code in line with the existing caller.

Jan

#26 lore raw

Re: [PATCH v3 11/16] x86/VPMU: Add support for PMU register handling on PV guests

Quoted 22 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/traps.c +0 -1
@@ -866,7 +867,6 @@ void pv_cpuid(struct cpu_user_regs *regs)
          break;
 
      case 0x00000005: /* MONITOR/MWAIT */
-    case 0x0000000a: /* Architectural Performance Monitor Features */
      case 0x0000000b: /* Extended Topology Enumeration */
      case 0x8000000a: /* SVM revision and features */
      case 0x8000001b: /* Instruction Based Sampling */

xen/arch/x86/traps.c void pv_cpuid(struct cpu_user_regs *regs) · line 881

Quoted 8 lines
xen/arch/x86/traps.c +3 -1
@@ -875,7 +875,9 @@ void pv_cpuid(struct cpu_user_regs *regs)
      unsupported:
          a = b = c = d = 0;
          break;
-
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        vpmu_do_cpuid(0xa, &a, &b, &c, &d);
+        break;
      default:

Rather than removing a blank line here, you ought to insert a second one so that there's one before _and_ after the added code block.

Quoted 4 lines

Furthermore the need to pass 0xa as the first argument suggests that you're not in line with the intentions of vpmu_do_cpuid():
Either you drop the first parameter from the function, or you get your code in line with the existing caller.

Not sure I understand the problem. We fill a, b, c and d with HW values for dom0 and then call vpmu_do_cpuid() to adjust them if needed. And whether or not this is needed is based on the first argument.

(There is a bug in this routine in that it looks like I am not calling vpmu_do_cpuid() for non-dom0 PV guest but that's a different issue).

-boris

#27 lore raw

Re: [PATCH v3 11/16] x86/VPMU: Add support for PMU register handling on PV guests

Quoted 16 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 14 lines · Jan Beulich

xen/arch/x86/traps.c void pv_cpuid(struct cpu_user_regs *regs) · line 881

Quoted 10 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/traps.c +3 -1
@@ -875,7 +875,9 @@ void pv_cpuid(struct cpu_user_regs *regs)
      unsupported:
          a = b = c = d = 0;
          break;
-
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        vpmu_do_cpuid(0xa, &a, &b, &c, &d);
+        break;
      default:

Rather than removing a blank line here, you ought to insert a second one so that there's one before _and_ after the added code block.

Quoted 8 lines
Quoted 4 lines

Furthermore the need to pass 0xa as the first argument suggests that you're not in line with the intentions of vpmu_do_cpuid():
Either you drop the first parameter from the function, or you get your code in line with the existing caller.

Not sure I understand the problem. We fill a, b, c and d with HW values for dom0 and then call vpmu_do_cpuid() to adjust them if needed. And whether or not this is needed is based on the first argument.

Did you look at the other call site? Iirc it calls the function for all input values (and hence the need for the current first parameter of the function). I take it that the expectation was that further perfctr related leaves might be added later and/or perfctr code may need to override more than just leaf 0xa values.

Jan

#28 lore raw

Re: [PATCH v3 11/16] x86/VPMU: Add support for PMU register handling on PV guests

Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 18 lines · Jan Beulich

xen/arch/x86/traps.c void pv_cpuid(struct cpu_user_regs *regs) · line 881

Quoted 10 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/traps.c +3 -1
@@ -875,7 +875,9 @@ void pv_cpuid(struct cpu_user_regs *regs)
       unsupported:
           a = b = c = d = 0;
           break;
-
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        vpmu_do_cpuid(0xa, &a, &b, &c, &d);
+        break;
       default:

Rather than removing a blank line here, you ought to insert a second one so that there's one before _and_ after the added code block.

Furthermore the need to pass 0xa as the first argument suggests that you're not in line with the intentions of vpmu_do_cpuid():
Either you drop the first parameter from the function, or you get your code in line with the existing caller.

Quoted 9 lines
Quoted 3 lines

Not sure I understand the problem. We fill a, b, c and d with HW values for dom0 and then call vpmu_do_cpuid() to adjust them if needed. And whether or not this is needed is based on the first argument.

Did you look at the other call site? Iirc it calls the function for all input values (and hence the need for the current first parameter of the function). I take it that the expectation was that further perfctr related leaves might be added later and/or perfctr code may need to override more than just leaf 0xa values.

Oh, you meant the problem is that I don't cover leaves *other* than 0xa.

I can move vpmu_do_cpuid() down to the exit path ("out" label) so that all cpuid calls are checked. This will add latency to every cpuid invocation in the guest but I guess it should be negligible (and this is what HVM does already).

Incidentally, this will also solve my non-dom0 CPUID bug that I mentioned.

-boris

#29 lore raw

PATCH v3 12/16 x86/VPMU: Handle PMU interrupts for PV guests

In reply to Boris Ostrovsky

Add support for handling PMU interrupts for PV guests.

VPMU for the interrupted VCPU is unloaded until the guest issues XENPMU_flush hypercall. This allows the guest to access PMU MSR values that are stored in VPMU context which is shared between hypervisor and domain, thus avoiding traps to hypervisor.

---
 xen/arch/x86/hvm/vpmu.c     | 117 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/public/xenpmu.h |   7 +++
 2 files changed, 117 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/vpmu.c +110 -7
index 2cc37cc..666067d 100644
@@ -74,7 +74,12 @@ void vpmu_lvtpc_update(uint32_t val)
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
     vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
-    apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+
+    /* Postpone APIC updates for PV guests if PMU interrupt is pending */
+    if (is_hvm_domain(current->domain) ||
+        !(current->arch.vpmu.xenpmu_data &&
+          current->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED) )
+        apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
 }
 
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
@@ -82,7 +87,23 @@ int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_wrmsr )
-        return vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content);
+    {
+        int val = vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content);
+
+        /*
+         * We may have received a PMU interrupt during WRMSR handling
+         * and since do_wrmsr may load VPMU context we should save
+         * (and unload) it again.
+         */
+        if ( !is_hvm_domain(current->domain) &&
+             current->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED )
+        {
+            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+            (void)vpmu->arch_vpmu_ops->arch_vpmu_save(current);
+            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+        }
+        return val;
+    }
     return 0;
 }
 
@@ -91,16 +112,87 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_rdmsr )
-        return vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
+    {
+        int val = vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
+
+        if ( !is_hvm_domain(current->domain) &&
+             current->arch.vpmu.xenpmu_data->pmu_flags)
+        {
+            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+            (void)vpmu->arch_vpmu_ops->arch_vpmu_save(current);
+            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+        }
+        return val;
+    }
     return 0;
 }
 
 int vpmu_do_interrupt(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vpmu_struct *vpmu;
 
-    if ( vpmu->arch_vpmu_ops )
+    /* dom0 will handle this interrupt */
+    if ( v->domain->domain_id >= DOMID_FIRST_RESERVED )
+    {
+        if ( smp_processor_id() >= dom0->max_vcpus )
+            return 0;
+        v = dom0->vcpu[smp_processor_id()];
+    }
+
+    vpmu = vcpu_vpmu(v);
+    if ( !is_hvm_domain(v->domain) )
+    {
+        /* PV guest or dom0 is doing system profiling */
+        void *p;
+        struct cpu_user_regs *gregs;
+        int err;
+
+        if (v->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED)
+            return 1;
+
+        /* PV guest will be reading PMU MSRs from xenpmu_data */
+        vpmu_set(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+        err = vpmu->arch_vpmu_ops->arch_vpmu_save(v);
+        vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+
+        /* Store appropriate registers in xenpmu_data */
+        p = &v->arch.vpmu.xenpmu_data->pmu.regs;
+        if ( is_pv_32bit_domain(current->domain) )
+        {
+            /*
+             * 32-bit dom0 cannot process Xen's addresses (which are 64 bit)
+             * and therefore we treat it the same way as a non-priviledged
+             * PV 32-bit domain.
+             */
+            struct compat_cpu_user_regs cmp;
+
+            gregs = guest_cpu_user_regs();
+            XLAT_cpu_user_regs(&cmp, gregs);
+            memcpy(p, &cmp, sizeof(struct compat_cpu_user_regs));
+        }
+        else if ( (current->domain != dom0) && !is_idle_vcpu(current) )
+        {
+            /* PV guest */
+            gregs = guest_cpu_user_regs();
+            memcpy(p, gregs, sizeof(struct cpu_user_regs));
+        }
+        else
+            memcpy(p, regs, sizeof(struct cpu_user_regs));
+
+        v->arch.vpmu.xenpmu_data->domain_id = current->domain->domain_id;
+        v->arch.vpmu.xenpmu_data->vcpu_id = current->vcpu_id;
+        v->arch.vpmu.xenpmu_data->pcpu_id = smp_processor_id();
+
+        v->arch.vpmu.xenpmu_data->pmu_flags |= PMU_CACHED;
+        apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc | APIC_LVT_MASKED);
+        vpmu->hw_lapic_lvtpc |= APIC_LVT_MASKED;
+
+        send_guest_vcpu_virq(v, VIRQ_XENPMU);
+
+        return 1;
+    }
+    else if ( vpmu->arch_vpmu_ops )
     {
         struct vlapic *vlapic = vcpu_vlapic(v);
         u32 vlapic_lvtpc;
@@ -212,8 +304,13 @@ void vpmu_load(struct vcpu *v)
 
     local_irq_enable();
 
-    /* Only when PMU is counting, we load PMU context immediately. */
-    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) )
+    /* 
+     * Only when PMU is counting and is not cached (for PV guests) do
+     * we load PMU context immediately.
+     */
+    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) ||
+         (!is_hvm_domain(v->domain) &&
+          vpmu->xenpmu_data->pmu_flags & PMU_CACHED) )
         return;
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_load )
@@ -404,6 +501,12 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xenpmu_params_t) arg)
         vpmu_lvtpc_update((uint32_t)pmu_params.val);
         ret = 0;
         break;
+    case XENPMU_flush:
+        current->arch.vpmu.xenpmu_data->pmu_flags &= ~PMU_CACHED;
+        vpmu_load(current);
+        vpmu_lvtpc_update(current->arch.vpmu.xenpmu_data->pmu.lapic_lvtpc);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/include/public/xenpmu.h +7 -0
index bad5211..f4913c6 100644
@@ -28,6 +28,7 @@
 #define XENPMU_init            4
 #define XENPMU_finish          5
 #define XENPMU_lvtpc_set       6
+#define XENPMU_flush           7 /* Write cached MSR values to HW     */
 /* ` } */
 
 /* ANSI-C does not support anonymous unions */
@@ -75,6 +76,12 @@ DEFINE_XEN_GUEST_HANDLE(xenpmu_params_t);
 #define XENPMU_FEATURE_MASK       ((uint32_t)(~XENPMU_MODE_MASK))
 #define XENPMU_FEATURE_INTEL_BTS  1
 
+/*
+ * PMU MSRs are cached in the context so the PV guest doesn't need to trap to
+ * the hypervisor
+ */
+#define PMU_CACHED 1
+
 /* Shared between hypervisor and PV domain */
 struct xenpmu_data {
     uint32_t domain_id;
Signature · 1 line
1.8.1.4
#30 lore raw

Re: [PATCH v3 12/16] x86/VPMU: Handle PMU interrupts for PV guests

xen/arch/x86/hvm/vpmu.c int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) · line 91

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +2 -1
@@ -82,7 +87,23 @@ int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_wrmsr )
-        return vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content);
+    {
+        int val = vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content);

"val" is a misleading name here.

xen/arch/x86/hvm/vpmu.c int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) · line 99

Quoted 8 lines
xen/arch/x86/hvm/vpmu.c +8 -0
+
+        /*
+         * We may have received a PMU interrupt during WRMSR handling
+         * and since do_wrmsr may load VPMU context we should save
+         * (and unload) it again.
+         */
+        if ( !is_hvm_domain(current->domain) &&
+             current->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED )

I'd suggest parenthesizing the operands of &.

xen/arch/x86/hvm/vpmu.c int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) · line 102

Quoted 3 lines
xen/arch/x86/hvm/vpmu.c +3 -0
+        {
+            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+            (void)vpmu->arch_vpmu_ops->arch_vpmu_save(current);

What's this cast good for?

Quoted 8 lines
xen/arch/x86/hvm/vpmu.c +4 -0
+            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+        }
+        return val;
+    }
     return 0;
 }
 

xen/arch/x86/hvm/vpmu.c int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content) · line 119

Quoted 9 lines
xen/arch/x86/hvm/vpmu.c +5 -1
@@ -91,16 +112,87 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_rdmsr )
-        return vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
+    {
+        int val = vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
+
+        if ( !is_hvm_domain(current->domain) &&
+             current->arch.vpmu.xenpmu_data->pmu_flags)

Coding style (here and elsewhere).

xen/arch/x86/hvm/vpmu.c int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content) · line 141

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +7 -0
+    /* dom0 will handle this interrupt */
+    if ( v->domain->domain_id >= DOMID_FIRST_RESERVED )
+    {
+        if ( smp_processor_id() >= dom0->max_vcpus )
+            return 0;
+        v = dom0->vcpu[smp_processor_id()];
+    }

Ugly new uses of "dom0". And the correlation between smp_processor_id() and dom0->max_vcpus doesn't look sane either.

xen/arch/x86/hvm/vpmu.c int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content) · line 148

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +7 -0
+
+    vpmu = vcpu_vpmu(v);
+    if ( !is_hvm_domain(v->domain) )
+    {
+        /* PV guest or dom0 is doing system profiling */
+        void *p;
+        struct cpu_user_regs *gregs;

const.

xen/arch/x86/hvm/vpmu.c int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content) · line 160

Quoted 12 lines
xen/arch/x86/hvm/vpmu.c +12 -0
+        int err;
+
+        if (v->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED)
+            return 1;
+
+        /* PV guest will be reading PMU MSRs from xenpmu_data */
+        vpmu_set(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+        err = vpmu->arch_vpmu_ops->arch_vpmu_save(v);
+        vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+
+        /* Store appropriate registers in xenpmu_data */
+        p = &v->arch.vpmu.xenpmu_data->pmu.regs;

This and the code below should be done in a type safe manner if at all possible. I.e. p should not be void *, but a union of pointers or a pointer to a union.

xen/arch/x86/hvm/vpmu.c int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content) · line 172

Quoted 12 lines
xen/arch/x86/hvm/vpmu.c +12 -0
+        if ( is_pv_32bit_domain(current->domain) )
+        {
+            /*
+             * 32-bit dom0 cannot process Xen's addresses (which are 64 bit)
+             * and therefore we treat it the same way as a non-priviledged
+             * PV 32-bit domain.
+             */
+            struct compat_cpu_user_regs cmp;
+
+            gregs = guest_cpu_user_regs();
+            XLAT_cpu_user_regs(&cmp, gregs);
+            memcpy(p, &cmp, sizeof(struct compat_cpu_user_regs));

And then there would be no point in using an intermediate variable here.

Review int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content) · line 159

Quoted 2 lines
(quoted diff) +2 -0
+        }
+        else if ( (current->domain != dom0) && !is_idle_vcpu(current) )

Did you perhaps mean !is_control_domain() or
!is_hardware_domain()?

Jan

#31 lore raw

Re: [PATCH v3 12/16] x86/VPMU: Handle PMU interrupts for PV guests

Quoted 6 lines · Jan Beulich

xen/arch/x86/hvm/vpmu.c int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) · line 102

Quoted 5 lines
xen/arch/x86/hvm/vpmu.c +3 -0
@@ -82,7 +87,23 @@ int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
...
+        {
+            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+            (void)vpmu->arch_vpmu_ops->arch_vpmu_save(current);

What's this cast good for?

These routines return an int that indicates whether this was full context save or just that the counters have been stopped. Depending on where the routines are called from we may ignore the return value (having VPMU_CONTEXT_SAVE forces "full save").

I did it so that compiler won't complain about ignoring return value (I don't think it does now but I imagine some version still may).

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +4 -0
+            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+        }
+        return val;
+    }
      return 0;
  }

...

Quoted 10 lines

xen/arch/x86/hvm/vpmu.c int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content) · line 141

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +7 -0
+    /* dom0 will handle this interrupt */
+    if ( v->domain->domain_id >= DOMID_FIRST_RESERVED )
+    {
+        if ( smp_processor_id() >= dom0->max_vcpus )
+            return 0;
+        v = dom0->vcpu[smp_processor_id()];
+    }

Ugly new uses of "dom0". And the correlation between smp_processor_id() and dom0->max_vcpus doesn't look sane either.

Yes, this needs to be fixed. Maybe

    v = dom0->vcpu[smp_processor_id() % dom0->max_vcpus];

And drop the check. (And the same change in pmu_softnmi() in a later patch).

Not sure what to do about dom0. What else can I use instead? I need to choose dom0's vcpu.

Thanks.
-boris

#32 lore raw

Re: [PATCH v3 12/16] x86/VPMU: Handle PMU interrupts for PV guests

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 6 lines · Jan Beulich

xen/arch/x86/hvm/vpmu.c int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) · line 102

Quoted 5 lines
xen/arch/x86/hvm/vpmu.c +3 -0
@@ -82,7 +87,23 @@ int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
...
+        {
+            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+            (void)vpmu->arch_vpmu_ops->arch_vpmu_save(current);

What's this cast good for?

Quoted 7 lines

These routines return an int that indicates whether this was full context save or just that the counters have been stopped. Depending on where the routines are called from we may ignore the return value (having VPMU_CONTEXT_SAVE forces "full save").

I did it so that compiler won't complain about ignoring return value (I don't think it does now but I imagine some version still may).

If any were, we'd have many more places where such casts to void would be needed. The only place where such make sense are macros that produce a value which is to be ignored.

Quoted 11 lines
Quoted 10 lines

xen/arch/x86/hvm/vpmu.c int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content) · line 141

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +7 -0
+    /* dom0 will handle this interrupt */
+    if ( v->domain->domain_id >= DOMID_FIRST_RESERVED )
+    {
+        if ( smp_processor_id() >= dom0->max_vcpus )
+            return 0;
+        v = dom0->vcpu[smp_processor_id()];
+    }

Ugly new uses of "dom0". And the correlation between smp_processor_id() and dom0->max_vcpus doesn't look sane either.

Quoted 5 lines

Yes, this needs to be fixed. Maybe

     v = dom0->vcpu[smp_processor_id() % dom0->max_vcpus];

And drop the check. (And the same change in pmu_softnmi() in a later patch).

Looks odd, but as long as the kernel side can cope...

Quoted 2 lines

Not sure what to do about dom0. What else can I use instead? I need to choose dom0's vcpu.

Just think the disaggregation way - ultimately it may not be Dom0 that handles this. But I said "ugly", not "unacceptable", knowing that for the time being you likely have no reasonable alternative.

Jan

#33 lore raw

PATCH v3 13/16 x86/VPMU: Add privileged PMU mode

In reply to Boris Ostrovsky

Add support for privileged PMU mode which allows privileged domain (dom0) profile both itself (and the hypervisor) and the guests. While this mode is on profiling in guests is disabled.

---
 xen/arch/x86/hvm/vpmu.c     | 65 ++++++++++++++++++++++++++++++++-------------
 xen/arch/x86/traps.c        |  6 ++++-
 xen/include/public/xenpmu.h |  3 +++
 3 files changed, 55 insertions(+), 19 deletions(-)
xen/arch/x86/hvm/vpmu.c +47 -18
index 666067d..0cbede4 100644
@@ -86,6 +86,9 @@ int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
+    if ( (vpmu_mode & XENPMU_MODE_PRIV) && !is_control_domain(current->domain) )
+        return 0;
+
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_wrmsr )
     {
         int val = vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content);
@@ -111,6 +114,9 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
+    if ( (vpmu_mode & XENPMU_MODE_PRIV) && !is_control_domain(current->domain) )
+        return 0;
+
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_rdmsr )
     {
         int val = vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
@@ -133,7 +139,8 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
     struct vpmu_struct *vpmu;
 
     /* dom0 will handle this interrupt */
-    if ( v->domain->domain_id >= DOMID_FIRST_RESERVED )
+    if ( (vpmu_mode & XENPMU_MODE_PRIV) ||
+         (v->domain->domain_id >= DOMID_FIRST_RESERVED) )
     {
         if ( smp_processor_id() >= dom0->max_vcpus )
             return 0;
@@ -141,7 +148,10 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
     }
 
     vpmu = vcpu_vpmu(v);
-    if ( !is_hvm_domain(v->domain) )
+    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+        return 0;
+
+    if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
     {
         /* PV guest or dom0 is doing system profiling */
         void *p;
@@ -158,27 +168,45 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
 
         /* Store appropriate registers in xenpmu_data */
         p = &v->arch.vpmu.xenpmu_data->pmu.regs;
-        if ( is_pv_32bit_domain(current->domain) )
+        if ( !is_hvm_domain(current->domain) )
         {
-            /*
-             * 32-bit dom0 cannot process Xen's addresses (which are 64 bit)
-             * and therefore we treat it the same way as a non-priviledged
-             * PV 32-bit domain.
-             */
-            struct compat_cpu_user_regs cmp;
-
-            gregs = guest_cpu_user_regs();
-            XLAT_cpu_user_regs(&cmp, gregs);
-            memcpy(p, &cmp, sizeof(struct compat_cpu_user_regs));
+            if ( is_pv_32bit_domain(current->domain) )
+            {
+                /*
+                 * 32-bit dom0 cannot process Xen's addresses (which are 64 bit)
+                 * and therefore we treat it the same way as a non-priviledged
+                 * PV 32-bit domain.
+                 */
+                struct compat_cpu_user_regs cmp;
+
+                gregs = guest_cpu_user_regs();
+                XLAT_cpu_user_regs(&cmp, gregs);
+                memcpy(p, &cmp, sizeof(struct compat_cpu_user_regs));
+            }
+            else if ( !is_control_domain(current->domain) &&
+                      !is_idle_vcpu(current) )
+            {
+                /* PV guest */
+                gregs = guest_cpu_user_regs();
+                memcpy(p, gregs, sizeof(struct cpu_user_regs));
+            }
+            else
+                memcpy(p, regs, sizeof(struct cpu_user_regs));
+
+            ((struct cpu_user_regs *)p)->cs =
+                    (current->arch.flags & TF_kernel_mode) ? 0 : 0x3;
         }
-        else if ( (current->domain != dom0) && !is_idle_vcpu(current) )
+        else 
         {
-            /* PV guest */
+            /* HVM guest */
+            struct segment_register cs;
+
             gregs = guest_cpu_user_regs();
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+
             memcpy(p, gregs, sizeof(struct cpu_user_regs));
+            ((struct cpu_user_regs *)p)->cs = cs.attr.fields.dpl;
         }
-        else
-            memcpy(p, regs, sizeof(struct cpu_user_regs));
 
         v->arch.vpmu.xenpmu_data->domain_id = current->domain->domain_id;
         v->arch.vpmu.xenpmu_data->vcpu_id = current->vcpu_id;
@@ -441,7 +469,8 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xenpmu_params_t) arg)
             return -EFAULT;
 
         mode = (uint32_t)pmu_params.val & XENPMU_MODE_MASK;
-        if ( mode & ~XENPMU_MODE_ON )
+        if ( (mode & ~(XENPMU_MODE_ON | XENPMU_MODE_PRIV)) ||
+             ((mode & XENPMU_MODE_ON) && (mode & XENPMU_MODE_PRIV)) )
             return -EINVAL;
 
         vpmu_mode &= ~XENPMU_MODE_MASK;
xen/arch/x86/traps.c +5 -1
index 8a3353f..3ff24e5 100644
@@ -2507,7 +2507,11 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
         case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
             if ( !vpmu_do_wrmsr(regs->ecx, msr_content) )
-                goto invalid;
+            {
+                if ( (vpmu_mode & XENPMU_MODE_PRIV) &&
+                      is_control_domain(v->domain) )
+                    goto invalid;
+            }
             break;
         default:
             if ( wrmsr_hypervisor_regs(regs->ecx, msr_content) == 1 )
xen/include/public/xenpmu.h +3 -0
index f4913c6..c02eb66 100644
@@ -63,11 +63,14 @@ DEFINE_XEN_GUEST_HANDLE(xenpmu_params_t);
  * - XENPMU_MODE_OFF:   No PMU virtualization
  * - XENPMU_MODE_ON:    Guests can profile themselves, dom0 profiles
  *                      itself and Xen
+ * - XENPMU_MODE_PRIV:  Only dom0 has access to VPMU and it profiles
+ *                      everyone: itself, the hypervisor and the guests.
  */
 #define XENPMU_FEATURE_SHIFT      16
 #define XENPMU_MODE_MASK          ((1U << XENPMU_FEATURE_SHIFT) - 1)
 #define XENPMU_MODE_OFF           0
 #define XENPMU_MODE_ON            (1<<0)
+#define XENPMU_MODE_PRIV          (1<<1)
 
 /*
  * PMU features:
Signature · 1 line
1.8.1.4
#34 lore raw

PATCH v3 14/16 x86/VPMU: Save VPMU state for PV guests during context switch

In reply to Boris Ostrovsky

Save VPMU state during context switch for both HVM and PV guests unless we are in PMU privileged mode (i.e. dom0 is doing all profiling).

---
 xen/arch/x86/domain.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
xen/arch/x86/domain.c +6 -8
index 25572d5..daf0313 100644
@@ -1444,17 +1444,15 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
     }
 
     if (prev != next)
-        update_runstate_area(prev);
-
-    if ( is_hvm_vcpu(prev) )
     {
-        if (prev != next)
+        update_runstate_area(prev);
+        if ( !(vpmu_mode & XENPMU_MODE_PRIV) || prev->domain != dom0 )
             vpmu_save(prev);
-
-        if ( !list_empty(&prev->arch.hvm_vcpu.tm_list) )
-            pt_save_timer(prev);
     }
 
+    if ( is_hvm_vcpu(prev) && !list_empty(&prev->arch.hvm_vcpu.tm_list) )
+        pt_save_timer(prev);
+
     local_irq_disable();
 
     set_current(next);
@@ -1491,7 +1489,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
                            (next->domain->domain_id != 0));
     }
 
-    if (is_hvm_vcpu(next) && (prev != next) )
+    if ( prev != next && !(vpmu_mode & XENPMU_MODE_PRIV) )
         /* Must be done with interrupts enabled */
         vpmu_load(next);
 
Signature · 1 line
1.8.1.4
#35 lore raw

Re: [PATCH v3 14/16] x86/VPMU: Save VPMU state for PV guests during context switch

Quoted 3 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Save VPMU state during context switch for both HVM and PV guests unless we are in PMU privileged mode (i.e. dom0 is doing all profiling).

This description doesn't seem to be in line with ...

xen/arch/x86/domain.c void context_switch(struct vcpu *prev, struct vcpu *next) · line 1449

Quoted 13 lines
xen/arch/x86/domain.c +2 -4
@@ -1444,17 +1444,15 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
     }
 
     if (prev != next)
-        update_runstate_area(prev);
-
-    if ( is_hvm_vcpu(prev) )
     {
-        if (prev != next)
+        update_runstate_area(prev);
+        if ( !(vpmu_mode & XENPMU_MODE_PRIV) || prev->domain != dom0 )

... this condition: vpmu_save() is being called when in privileged mode and the switched out domain is other than Dom0 (yet above you say all prifiling is done by Dom0 in that mode).

Apart from that - the latter condition likely wants to become !is_control_domain() to be in line with earlier patches (although, as can be seem by an earlier similar comment of mine, you aren't really consistent throughout your patches in the regard, which needs to be fixed).

Jan

#36 lore raw

PATCH v3 15/16 x86/VPMU: NMI-based VPMU support

In reply to Boris Ostrovsky

Add support for using NMIs as PMU interrupts

---
 xen/arch/x86/hvm/vpmu.c | 119 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 99 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/vpmu.c +99 -20
index 0cbede4..ff05ff5 100644
@@ -35,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>
 
 /*
@@ -47,33 +48,57 @@ static void parse_vpmu_param(char *s);
 custom_param("vpmu", parse_vpmu_param);
 
 static DEFINE_PER_CPU(struct vcpu *, last_vcpu);
+static DEFINE_PER_CPU(struct vcpu *, sampled_vcpu);
+
+uint32_t vpmu_apic_vector = PMU_APIC_VECTOR;
 
 static void __init parse_vpmu_param(char *s)
 {
-    switch ( parse_bool(s) )
-    {
-    case 0:
-        break;
-    default:
-        if ( !strcmp(s, "bts") )
-            vpmu_mode |= XENPMU_FEATURE_INTEL_BTS << XENPMU_FEATURE_SHIFT;
-        else if ( *s )
+    char *ss;
+
+    vpmu_mode = XENPMU_MODE_ON;
+    if (*s == '\0')
+        return;
+
+    do {
+        ss = strchr(s, ',');
+        if ( ss )
+            *ss = '\0';
+
+        switch  (parse_bool(s) )
         {
-            printk("VPMU: unknown flag: %s - vpmu disabled!\n", s);
+        case 0:
+            vpmu_mode = XENPMU_MODE_OFF;
+            return;
+        case -1:
+            if ( !strcmp(s, "nmi") )
+                vpmu_apic_vector = APIC_DM_NMI;
+            else if ( !strcmp(s, "bts") )
+                vpmu_mode |= XENPMU_FEATURE_INTEL_BTS << XENPMU_FEATURE_SHIFT;
+            else if ( !strcmp(s, "priv") )
+            {
+                vpmu_mode &= ~XENPMU_MODE_ON;
+                vpmu_mode |= XENPMU_MODE_PRIV;
+            }
+            else
+            {
+                printk("VPMU: unknown flag: %s - vpmu disabled!\n", s);
+                vpmu_mode = XENPMU_MODE_OFF;
+                return;
+            }
+        default:
             break;
         }
-        /* fall through */
-    case 1:
-        vpmu_mode |= XENPMU_MODE_ON;
-        break;
-    }
+
+        s = ss + 1;
+    } while ( ss );
 }
 
 void vpmu_lvtpc_update(uint32_t val)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
+    vpmu->hw_lapic_lvtpc = vpmu_apic_vector | (val & APIC_LVT_MASKED);
 
     /* Postpone APIC updates for PV guests if PMU interrupt is pending */
     if (is_hvm_domain(current->domain) ||
@@ -202,10 +227,14 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
             struct segment_register cs;
 
             gregs = guest_cpu_user_regs();
-            hvm_get_segment_register(current, x86_seg_cs, &cs);
 
             memcpy(p, gregs, sizeof(struct cpu_user_regs));
-            ((struct cpu_user_regs *)p)->cs = cs.attr.fields.dpl;
+            /* This is unsafe in NMI context, we'll do it in softint handler */
+            if ( vpmu_apic_vector != APIC_DM_NMI )
+            {
+                hvm_get_segment_register(current, x86_seg_cs, &cs);
+                ((struct cpu_user_regs *)p)->cs = cs.attr.fields.dpl;
+            }
         }
 
         v->arch.vpmu.xenpmu_data->domain_id = current->domain->domain_id;
@@ -216,7 +245,13 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
         apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc | APIC_LVT_MASKED);
         vpmu->hw_lapic_lvtpc |= APIC_LVT_MASKED;
 
-        send_guest_vcpu_virq(v, VIRQ_XENPMU);
+        if ( vpmu_apic_vector == APIC_DM_NMI )
+        {
+            per_cpu(sampled_vcpu, smp_processor_id()) = current;
+            raise_softirq(PMU_SOFTIRQ);
+        }
+        else
+            send_guest_vcpu_virq(v, VIRQ_XENPMU);
 
         return 1;
     }
@@ -288,7 +323,7 @@ void vpmu_save(struct vcpu *v)
         if ( vpmu->arch_vpmu_ops->arch_vpmu_save(v) )
             vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
 
-    apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
+    apic_write(APIC_LVTPC, vpmu_apic_vector | APIC_LVT_MASKED);
 }
 
 void vpmu_load(struct vcpu *v)
@@ -379,7 +414,7 @@ void vpmu_initialise(struct vcpu *v)
         return;
     }
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+    vpmu->hw_lapic_lvtpc = vpmu_apic_vector | APIC_LVT_MASKED;
 }
 
 void vpmu_destroy(struct vcpu *v)
@@ -405,10 +440,40 @@ void vpmu_dump(struct vcpu *v)
         vpmu->arch_vpmu_ops->arch_vpmu_dump(v);
 }
 
+/* Process the softirq set by PMU NMI handler */
+static void pmu_softnmi(void)
+{
+    struct cpu_user_regs *regs;
+    struct vcpu *v, *sampled = per_cpu(sampled_vcpu, smp_processor_id());
+
+    if ( vpmu_mode & XENPMU_MODE_PRIV ||
+         sampled->domain->domain_id >= DOMID_FIRST_RESERVED )
+        v = dom0->vcpu[smp_processor_id()];
+    else
+        v = sampled;
+
+    regs = &v->arch.vpmu.xenpmu_data->pmu.regs;
+    if ( is_hvm_domain(sampled->domain) )
+    {
+        struct segment_register cs;
+
+        hvm_get_segment_register(sampled, x86_seg_cs, &cs);
+        regs->cs = cs.attr.fields.dpl;
+    }
+
+    send_guest_vcpu_virq(v, VIRQ_XENPMU);
+}
+
+int pmu_nmi_interrupt(struct cpu_user_regs *regs, int cpu)
+{
+    return vpmu_do_interrupt(regs);
+}
+
 static int pvpmu_init(struct domain *d, xenpmu_params_t *params)
 {
     struct vcpu *v;
     uint64_t mfn = params->val;
+    static int pvpmu_initted = 0;
 
     if ( params->vcpu < 0 || params->vcpu >= d->max_vcpus )
         return -EINVAL;
@@ -417,6 +482,20 @@ static int pvpmu_init(struct domain *d, xenpmu_params_t *params)
         !get_page_and_type(mfn_to_page(mfn), d, PGT_writable_page) )
         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_softnmi);
+
+        pvpmu_initted = 1;
+    }
+
     v = d->vcpu[params->vcpu];
     v->arch.vpmu.xenpmu_data = map_domain_page_global(mfn);
     memset(v->arch.vpmu.xenpmu_data, 0, PAGE_SIZE);
Signature · 1 line
1.8.1.4
#37 lore raw

Re: [PATCH v3 15/16] x86/VPMU: NMI-based VPMU support

Quoted 2 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Add support for using NMIs as PMU interrupts

This needs significant extension, detailing how all the handling involved is safe in NMI context.

xen/arch/x86/hvm/vpmu.c int vpmu_do_interrupt(struct cpu_user_regs *regs) · line 233

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +2 -2
@@ -202,10 +227,14 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
             struct segment_register cs;
 
             gregs = guest_cpu_user_regs();
-            hvm_get_segment_register(current, x86_seg_cs, &cs);
 
             memcpy(p, gregs, sizeof(struct cpu_user_regs));
-            ((struct cpu_user_regs *)p)->cs = cs.attr.fields.dpl;
+            /* This is unsafe in NMI context, we'll do it in softint handler */
+            if ( vpmu_apic_vector != APIC_DM_NMI )

Even if correct at present, I'd strongly suggest against != here (and below), in favor of &.

Quoted 13 lines
xen/arch/x86/hvm/vpmu.c +13 -0
+/* Process the softirq set by PMU NMI handler */
+static void pmu_softnmi(void)
+{
+    struct cpu_user_regs *regs;
+    struct vcpu *v, *sampled = per_cpu(sampled_vcpu, smp_processor_id());
+
+    if ( vpmu_mode & XENPMU_MODE_PRIV ||
+         sampled->domain->domain_id >= DOMID_FIRST_RESERVED )
+        v = dom0->vcpu[smp_processor_id()];
+    else
+        v = sampled;
+
+    regs = &v->arch.vpmu.xenpmu_data->pmu.regs;

xen/arch/x86/hvm/vpmu.c void vpmu_dump(struct vcpu *v) · line 465

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+    if ( is_hvm_domain(sampled->domain) )
+    {
+        struct segment_register cs;
+
+        hvm_get_segment_register(sampled, x86_seg_cs, &cs);
+        regs->cs = cs.attr.fields.dpl;
+    }
+
+    send_guest_vcpu_virq(v, VIRQ_XENPMU);
+}

Perhaps I should have asked this on an earlier patch already:
How is this supposed to work for a 32-bit HVM guest? struct cpu_user_regs is clearly different for it than what the hypervisor or a 64-bit HVM guest would use.

Jan

#38 lore raw

Re: [PATCH v3 15/16] x86/VPMU: NMI-based VPMU support

Quoted 20 lines · Jan Beulich
Quoted 20 lines
xen/arch/x86/hvm/vpmu.c +19 -0
+/* Process the softirq set by PMU NMI handler */
+static void pmu_softnmi(void)
+{
+    struct cpu_user_regs *regs;
+    struct vcpu *v, *sampled = per_cpu(sampled_vcpu, smp_processor_id());
+
+    if ( vpmu_mode & XENPMU_MODE_PRIV ||
+         sampled->domain->domain_id >= DOMID_FIRST_RESERVED )
+        v = dom0->vcpu[smp_processor_id()];
+    else
+        v = sampled;
+
+    regs = &v->arch.vpmu.xenpmu_data->pmu.regs;
+    if ( is_hvm_domain(sampled->domain) )
+    {
+        struct segment_register cs;
+
+        hvm_get_segment_register(sampled, x86_seg_cs, &cs);
+        regs->cs = cs.attr.fields.dpl;
Quoted 8 lines

xen/arch/x86/hvm/vpmu.c void vpmu_dump(struct vcpu *v) · line 465

Quoted 4 lines
xen/arch/x86/hvm/vpmu.c +4 -0
+    }
+
+    send_guest_vcpu_virq(v, VIRQ_XENPMU);
+}

Perhaps I should have asked this on an earlier patch already:
How is this supposed to work for a 32-bit HVM guest? struct cpu_user_regs is clearly different for it than what the hypervisor or a 64-bit HVM guest would use.

Right, I need to XLAT_cpu_user_regs() for 32-bit HVM guest in vpmu_interrupt() (I already do it for a PV guest).

Thanks.
-boris

#39 lore raw

PATCH v3 16/16 x86/VPMU: Move VPMU files up from hvm/ directory

In reply to Boris Ostrovsky

Since PMU is now not HVM specific we can move VPMU-related files up from arch/x86/hvm/ directory.

Specifically:

    arch/x86/hvm/vpmu.c -> arch/x86/vpmu.c
    arch/x86/hvm/svm/vpmu.c -> arch/x86/vpmu_amd.c
    arch/x86/hvm/vmx/vpmu_core2.c -> arch/x86/vpmu_intel.c
    include/asm-x86/hvm/vpmu.h -> include/asm-x86/vpmu.h
---
 xen/arch/x86/Makefile                 |   1 +
 xen/arch/x86/hvm/Makefile             |   1 -
 xen/arch/x86/hvm/svm/Makefile         |   1 -
 xen/arch/x86/hvm/svm/vpmu.c           | 499 ------------------
 xen/arch/x86/hvm/vlapic.c             |   2 +-
 xen/arch/x86/hvm/vmx/Makefile         |   1 -
 xen/arch/x86/hvm/vmx/vpmu_core2.c     | 936 ----------------------------------
 xen/arch/x86/hvm/vpmu.c               | 621 ----------------------
 xen/arch/x86/oprofile/op_model_ppro.c |   2 +-
 xen/arch/x86/traps.c                  |   2 +-
 xen/arch/x86/vpmu.c                   | 621 ++++++++++++++++++++++
 xen/arch/x86/vpmu_amd.c               | 499 ++++++++++++++++++
 xen/arch/x86/vpmu_intel.c             | 936 ++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/vmx/vmcs.h    |   2 +-
 xen/include/asm-x86/hvm/vpmu.h        |  95 ----
 xen/include/asm-x86/vpmu.h            |  95 ++++
 16 files changed, 2156 insertions(+), 2158 deletions(-)
 delete mode 100644 xen/arch/x86/hvm/svm/vpmu.c
 delete mode 100644 xen/arch/x86/hvm/vmx/vpmu_core2.c
 delete mode 100644 xen/arch/x86/hvm/vpmu.c
 create mode 100644 xen/arch/x86/vpmu.c
 create mode 100644 xen/arch/x86/vpmu_amd.c
 create mode 100644 xen/arch/x86/vpmu_intel.c
 delete mode 100644 xen/include/asm-x86/hvm/vpmu.h
 create mode 100644 xen/include/asm-x86/vpmu.h
xen/arch/x86/Makefile +1 -0
index d502bdf..cf85dda 100644
@@ -58,6 +58,7 @@ obj-y += crash.o
 obj-y += tboot.o
 obj-y += hpet.o
 obj-y += xstate.o
+obj-y += vpmu.o vpmu_amd.o vpmu_intel.o
 
 obj-$(crash_debug) += gdbstub.o
 
xen/arch/x86/hvm/Makefile +0 -1
index eea5555..742b83b 100644
@@ -22,4 +22,3 @@ obj-y += vlapic.o
 obj-y += vmsi.o
 obj-y += vpic.o
 obj-y += vpt.o
-obj-y += vpmu.o
\ No newline at end of file
xen/arch/x86/hvm/svm/Makefile +0 -1
index a10a55e..760d295 100644
@@ -6,4 +6,3 @@ obj-y += nestedsvm.o
 obj-y += svm.o
 obj-y += svmdebug.o
 obj-y += vmcb.o
-obj-y += vpmu.o
xen/arch/x86/hvm/svm/vpmu.c deleted +0 -499

499 lines not on this page · raw message

xen/arch/x86/hvm/vlapic.c +1 -1
index d954f4f..d49ed3a 100644
@@ -38,7 +38,7 @@
 #include <asm/hvm/support.h>
 #include <asm/hvm/vmx/vmx.h>
 #include <asm/hvm/nestedhvm.h>
-#include <asm/hvm/vpmu.h>
+#include <asm/vpmu.h>
 #include <public/hvm/ioreq.h>
 #include <public/hvm/params.h>
 
xen/arch/x86/hvm/vmx/Makefile +0 -1
index 373b3d9..04a29ce 100644
@@ -3,5 +3,4 @@ obj-y += intr.o
 obj-y += realmode.o
 obj-y += vmcs.o
 obj-y += vmx.o
-obj-y += vpmu_core2.o
 obj-y += vvmx.o
xen/arch/x86/hvm/vmx/vpmu_core2.c deleted +0 -936

936 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c deleted +0 -621

621 lines not on this page · raw message

xen/arch/x86/oprofile/op_model_ppro.c +1 -1
index 5aae2e7..bf5d9a5 100644
@@ -19,7 +19,7 @@
 #include <asm/processor.h>
 #include <asm/regs.h>
 #include <asm/current.h>
-#include <asm/hvm/vpmu.h>
+#include <asm/vpmu.h>
 
 #include "op_x86_model.h"
 #include "op_counter.h"
xen/arch/x86/traps.c +1 -1
index 3ff24e5..ba062f5 100644
@@ -72,7 +72,7 @@
 #include <asm/apic.h>
 #include <asm/mc146818rtc.h>
 #include <asm/hpet.h>
-#include <asm/hvm/vpmu.h>
+#include <asm/vpmu.h>
 #include <public/arch-x86/cpuid.h>
 #include <xsm/xsm.h>
 
xen/arch/x86/vpmu.c new file +621 -0

621 lines not on this page · raw message

xen/arch/x86/vpmu_amd.c new file +499 -0

278 lines not on this page · raw message

xen/arch/x86/vpmu_intel.c new file +936 -0

936 lines not on this page · raw message

xen/include/asm-x86/hvm/vmx/vmcs.h +1 -1
index ed81cfb..d27df39 100644

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/hvm/vpmu.h deleted +0 -95

95 lines not on this page · raw message

xen/include/asm-x86/vpmu.h new file +95 -0

95 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.8.1.4

Part of the diff is not shown here. View the raw message.