Skip to contentSkip to conversation

x86/PMU: Xen PMU PV support

PATCH Superseded Boris Ostrovsky 2 participants 48 messages Jan 21, 2014 – Feb 4, 2014

v4 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

v4 cover x86/PMU: Xen PMU PV support

Here is the fourth version PV (and now PVH) PMU patches.

The following patch series adds PMU support in Xen for PV(H) 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 regular vector interrupts for both HVM and PV(H). The interrupts are sent as NMIs to HVM guests and as virtual interrupts to PV(H) guests
* PV 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 HVM, PV and PVH and therefore has been moved up from hvm subtree

Changes in v4:

* Added support for PVH guests:
o changes in pvpmu_init() to accommodate both PV and PVH guests, still in patch 10
o more careful use of is_hvm_domain
o Additional patch (16)
* Moved HVM interrupt handling out of vpmu_do_interrupt() for NMI-safe handling
* Fixed dom0's VCPU selection in privileged mode
* Added a cast in register copy for 32-bit PV guests cpu_user_regs_t in vpmu_do_interrupt. (don't want to expose compat_cpu_user_regs in a public header)
* Renamed public structures by prefixing them with "xen_"
* Added an entry for xenpf_symdata in xlat.lst
* Fixed pv_cpuid check for vpmu-specific cpuid adjustments
* Varios code style fixes
* Eliminated anonymous unions
* Added more verbiage to NMI patch description

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 (17):
common/symbols: Export hypervisor symbols to privileged 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: Suport for PVH guests
x86/VPMU: Move VPMU files up from hvm/ directory

 xen/arch/x86/Makefile                    |   1 +
 xen/arch/x86/domain.c                    |  18 +-
 xen/arch/x86/hvm/Makefile                |   1 -
 xen/arch/x86/hvm/hvm.c                   |   3 +-
 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/vmx.c               |   4 +-
 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                     |  35 +-
 xen/arch/x86/vpmu.c                      | 671 ++++++++++++++++++++++
 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               |  98 ++++
 xen/include/public/arch-x86/xenpmu.h     |  66 +++
 xen/include/public/platform.h            |  19 +
 xen/include/public/xen.h                 |   2 +
 xen/include/public/xenpmu.h              | 102 ++++
 xen/include/xen/hypercall.h              |   4 +
 xen/include/xen/softirq.h                |   1 +
 xen/include/xen/symbols.h                |   7 +-
 xen/include/xlat.lst                     |   1 +
 38 files changed, 2601 insertions(+), 1873 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 v4 1/17 common/symbols: Export hypervisor symbols to privileged 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 +++--
 xen/include/xlat.lst                     |  1 +
 7 files changed, 95 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*/
xen/include/xlat.lst +1 -0
index f00cef3..cf89583 100644
@@ -84,6 +84,7 @@
 ?	processor_px			platform.h
 !	psd_package			platform.h
 ?	xenpf_enter_acpi_sleep		platform.h
+!	xenpf_symdata			platform.h
 ?	xenpf_pcpuinfo			platform.h
 ?	xenpf_pcpu_version		platform.h
 !	sched_poll			sched.h
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [PATCH v4 01/17] common/symbols: Export hypervisor symbols to privileged guest

xen/arch/x86/platform_hypercall.c ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op) · line 608

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/platform_hypercall.c +4 -0
@@ -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;

Why _64?

xen/arch/x86/platform_hypercall.c ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op) · line 615

Quoted 7 lines
xen/arch/x86/platform_hypercall.c +7 -0
+
+        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) )

Afaict symbols_expand_symbol() always zero terminates its output, so I can't see why you're not properly using strlen() here. The way you do it now you're leaking hypervisor stack contents to the caller.

Quoted 10 lines
xen/common/symbols.c +10 -0
+int xensyms_read(uint32_t *symnum, uint32_t *type, uint64_t *address, char *name)
+{
+    if ( *symnum > symbols_num_syms )
+        return -ERANGE;
+    if ( *symnum == symbols_num_syms )
+        return 0;
+
+    spin_lock(&symbols_mutex);
+
+    if ( *symnum == 0 )

xen/common/symbols.c const char *symbols_lookup(unsigned long addr, · line 193

Quoted 10 lines
xen/common/symbols.c +10 -0
+        next_offset = next_symbol = 0;
+    if ( next_symbol != *symnum )
+        /* Non-sequential access */
+        next_offset = get_symbol_offset(*symnum);
+
+    *type = symbols_get_symbol_type(next_offset);
+    next_offset = symbols_expand_symbol(next_offset, name);
+    *address = symbols_offsets[*symnum] + SYMBOLS_ORIGIN;
+
+    next_symbol = ++(*symnum);

Pointless parentheses.

Quoted 4 lines
xen/include/public/platform.h +4 -0
+#define XENPF_get_symbol   61
+#define XEN_KSYM_NAME_LEN 127
+struct xenpf_symdata {
+    /* IN variables */

xen/include/public/platform.h struct xenpf_core_parking { · line 543

Quoted 10 lines
xen/include/public/platform.h +10 -0
+    uint32_t symnum;
+
+    /* OUT variables */
+    uint32_t type;
+    uint64_t address;
+
+    union {
+        XEN_GUEST_HANDLE(char) name;
+        uint64_t pad;
+    } u;

Since you need to do translation anyway, I don't see what good the padding field (and hence the union) here does.

xen/include/xen/symbols.h line 5

Quoted 9 lines
xen/include/xen/symbols.h +1 -2
@@ -2,8 +2,8 @@
 #define _XEN_SYMBOLS_H
 
 #include <xen/types.h>
-
-#define KSYM_NAME_LEN 127
+#include <public/xen.h>

I don't think you really need this one.

xen/include/xen/symbols.h const char *symbols_lookup(unsigned long addr, · line 16

Quoted 11 lines
xen/include/xen/symbols.h +4 -0
+#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);
+

Please be consistent at least within individual files: There's no "extern" in the existing function declaration here, so there shouldn't be one here.

Jan

#3 lore raw

PATCH v4 2/17 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
#4 lore raw

PATCH v4 3/17 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..84b8a36 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_pv_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_pv_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_pv_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_pv_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_pv_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..5368670 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_pv_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_pv_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
#5 lore raw

Re: [PATCH v4 03/17] x86/VPMU: Minor VPMU cleanup

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

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/svm/vpmu.c +1 -1
@@ -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_pv_domain(v->domain) && 

If I understand the intentions right, this is supposed to be is_hvm_container_domain(). See the mail archives or talk to George D if you want to learn about the intended distinction between the two.

Further this is inconsistent with the patch description saying "Make sure that we only touch MSR bitmap on HVM guests", as that would exclude PVH ones. With the three models in place now, you have to be careful to not cause confusion by imprecise statements.

Jan

#6 lore raw

PATCH v4 4/17 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
#7 lore raw

PATCH v4 5/17 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           | 14 +++++++++++---
 xen/include/asm-x86/hvm/vpmu.h    |  1 +
 5 files changed, 16 insertions(+), 25 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +0 -4
index 84b8a36..f6c542b 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_pv_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_pv_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 8d920c0..a966b91 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 +11 -3
index d6a9ff6..0770bcf 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,18 +235,18 @@ 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;
     }
 }
 
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
#8 lore raw

PATCH v4 6/17 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 a966b91..217c1f7 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
#9 lore raw

PATCH v4 7/17 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              | 71 ++++++++++++++------------
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 87 +++++++++++++++++---------------
 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           | 13 ++---
 xen/include/public/arch-x86/xenpmu.h     | 66 ++++++++++++++++++++++++
 xen/include/public/xenpmu.h              | 38 ++++++++++++++
 8 files changed, 199 insertions(+), 115 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 +39 -32
index f6c542b..bf7f1f6 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) )
@@ -142,7 +136,7 @@ static void amd_vpmu_set_msr_bitmap(struct vcpu *v)
 {
     unsigned int i;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    struct amd_vpmu_context *ctxt = vpmu->context;
+    struct xen_pmu_amd_ctxt *ctxt = vpmu->context;
 
     for ( i = 0; i < num_counters; i++ )
     {
@@ -157,7 +151,7 @@ static void amd_vpmu_unset_msr_bitmap(struct vcpu *v)
 {
     unsigned int i;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    struct amd_vpmu_context *ctxt = vpmu->context;
+    struct xen_pmu_amd_ctxt *ctxt = vpmu->context;
 
     for ( i = 0; i < num_counters; i++ )
     {
@@ -177,28 +171,31 @@ 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;
+    struct xen_pmu_amd_ctxt *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]);
     }
 }
 
 static void amd_vpmu_load(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    struct amd_vpmu_context *ctxt = vpmu->context;
+    struct xen_pmu_amd_ctxt *ctxt = vpmu->context;
 
     vpmu_reset(vpmu, VPMU_FROZEN);
 
     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;
     }
@@ -210,17 +207,18 @@ 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;
+    struct xen_pmu_amd_ctxt *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)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    struct amd_vpmu_context *ctx = vpmu->context;
+    struct xen_pmu_amd_ctxt *ctx = vpmu->context;
     unsigned int i;
 
     if ( !vpmu_is_set(vpmu, VPMU_FROZEN) )
@@ -248,7 +246,9 @@ static void context_update(unsigned int msr, u64 msr_content)
     unsigned int i;
     struct vcpu *v = current;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    struct amd_vpmu_context *ctxt = vpmu->context;
+    struct xen_pmu_amd_ctxt *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;
         }
     }
@@ -292,7 +292,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         vpmu_set(vpmu, VPMU_RUNNING);
 
         if ( !is_pv_domain(v->domain) &&
-             !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+             !((struct xen_pmu_amd_ctxt *)vpmu->context)->msr_bitmap_set )
             amd_vpmu_set_msr_bitmap(v);
     }
 
@@ -302,7 +302,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     {
         vpmu_reset(vpmu, VPMU_RUNNING);
         if ( !is_pv_domain(v->domain) &&
-             ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+             ((struct xen_pmu_amd_ctxt *)vpmu->context)->msr_bitmap_set )
             amd_vpmu_unset_msr_bitmap(v);
         release_pmu_ownship(PMU_OWNER_HVM);
     }
@@ -343,7 +343,7 @@ static int amd_vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 
 static int amd_vpmu_initialise(struct vcpu *v)
 {
-    struct amd_vpmu_context *ctxt;
+    struct xen_pmu_amd_ctxt *ctxt;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     uint8_t family = current_cpu_data.x86;
 
@@ -373,7 +373,9 @@ static int amd_vpmu_initialise(struct vcpu *v)
 	 }
     }
 
-    ctxt = xzalloc(struct amd_vpmu_context);
+    ctxt = xzalloc_bytes(sizeof(struct xen_pmu_amd_ctxt) + 
+			 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 xen_pmu_amd_ctxt);
+    ctxt->ctrls = ctxt->counters + sizeof(uint64_t) * AMD_MAX_COUNTERS;
+
     vpmu->context = ctxt;
     vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
     return 0;
@@ -395,7 +400,7 @@ static void amd_vpmu_destroy(struct vcpu *v)
         return;
 
     if ( !is_pv_domain(v->domain) &&
-         ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+         ((struct xen_pmu_amd_ctxt *)vpmu->context)->msr_bitmap_set )
         amd_vpmu_unset_msr_bitmap(v);
 
     xfree(vpmu->context);
@@ -412,7 +417,9 @@ static void amd_vpmu_destroy(struct vcpu *v)
 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;
+    const struct xen_pmu_amd_ctxt *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 +45 -42

221 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +1 -0
index 0770bcf..8c263a5 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 +5 -8
index 7ee0f01..9992887 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
@@ -42,6 +41,9 @@
 #define MSR_TYPE_ARCH_COUNTER       3
 #define MSR_TYPE_ARCH_CTRL          4
 
+/* Start of PMU register bank */
+#define vpmu_reg_pointer(ctxt, offset) ((void *)((uintptr_t)ctxt + \
+                                                 (uintptr_t)ctxt->offset))
 
 /* Arch specific operations shared by all vpmus */
 struct arch_vpmu_ops {
@@ -76,11 +78,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 +66 -0

66 lines not on this page · raw message

xen/include/public/xenpmu.h new file +38 -0
new file mode 100644
index 0000000..4757db9
@@ -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 xen_pmu_data {
+    uint32_t domain_id;
+    uint32_t vcpu_id;
+    uint32_t pcpu_id;
+    uint32_t pmu_flags;
+
+    xen_arch_pmu_t pmu;
+};
+typedef struct xen_pmu_data xen_pmu_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
#10 lore raw

Re: [PATCH v4 07/17] x86/VPMU: Add public xenpmu.h

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

Add xenpmu.h header file,

To me, naming a public Xen header (other than the core one) xen*.h is redundant. There's no information lost if you just called it pmu.h.

Also I think you ought to use plural here.

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

Quoted 9 lines
xen/include/public/arch-x86/xenpmu.h new file +6 -0
@@ -0,0 +1,66 @@
+#ifndef __XEN_PUBLIC_ARCH_X86_PMU_H__
+#define __XEN_PUBLIC_ARCH_X86_PMU_H__
+
+/* x86-specific PMU definitions */
+
+#include "xen.h"

Why?

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

Quoted 10 lines
xen/include/public/arch-x86/xenpmu.h new file +10 -0
+struct xen_pmu_intel_ctxt {
+    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 */

I think these last two could easily be uint32_t.

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

Quoted 9 lines
xen/include/public/arch-x86/xenpmu.h new file +9 -0
+/* Shared between hypervisor and PV domain */
+struct xen_pmu_data {
+    uint32_t domain_id;
+    uint32_t vcpu_id;
+    uint32_t pcpu_id;
+    uint32_t pmu_flags;
+
+    xen_arch_pmu_t pmu;
+};

So if this got included by an architecture independent source file on ARM, how would this build? You at least need a stub definition there for xen_arch_pmu_t afaict (if already give the impression - further up - that you're supporting ARM compilation of this header).

Jan

#11 lore raw

Re: [PATCH v4 07/17] x86/VPMU: Add public xenpmu.h

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

Add xenpmu.h header file,

To me, naming a public Xen header (other than the core one) xen*.h is redundant. There's no information lost if you just called it pmu.h.

I was trying to keep filename and top-level data structures the same (although now that I changed xenpmu_ prefix to xen_pmu_ they no longer are).

Quoted 2 lines

Also I think you ought to use plural here.

I'd prefer to keep the arch-independent and -dependent file names the same.

...

Quoted 13 lines

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

Quoted 11 lines
xen/include/public/arch-x86/xenpmu.h new file +10 -0
+struct xen_pmu_intel_ctxt {
+    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 */

I think these last two could easily be uint32_t.

Quoted 13 lines

xen/include/public/xenpmu.h line 25

Quoted 9 lines
xen/include/public/xenpmu.h new file +9 -0
+/* Shared between hypervisor and PV domain */
+struct xen_pmu_data {
+    uint32_t domain_id;
+    uint32_t vcpu_id;
+    uint32_t pcpu_id;
+    uint32_t pmu_flags;
+
+    xen_arch_pmu_t pmu;
+};

So if this got included by an architecture independent source file on ARM, how would this build? You at least need a stub definition there for xen_arch_pmu_t afaict (if already give the impression - further up - that you're supporting ARM compilation of this header).

I was supposed to have an entry in arch-arm.h but dropped it somewhere along the way. I'll put it back.

-boris

#12 lore raw

Re: [PATCH v4 07/17] x86/VPMU: Add public xenpmu.h

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

Add xenpmu.h header file,

To me, naming a public Xen header (other than the core one) xen*.h is redundant. There's no information lost if you just called it pmu.h.

Quoted 8 lines

I was trying to keep filename and top-level data structures the same (although now that I changed xenpmu_ prefix to xen_pmu_ they no longer are).

Quoted 2 lines

Also I think you ought to use plural here.

I'd prefer to keep the arch-independent and -dependent file names the same.

Right, that's appreciated. Nevertheless it's two of them, i.e. "Add pmu.h header files, ..."

Jan

#13 lore raw

PATCH v4 8/17 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 9992887..8646fd6 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
#14 lore raw

Re: [PATCH v4 08/17] x86/VPMU: Make vpmu not HVM-specific

xen/include/asm-x86/hvm/vpmu.h line 35

Quoted 6 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/asm-x86/hvm/vpmu.h +2 -3
-#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))

If you already edit this, I'd prefer if you also stripped the various redundant parentheses to make them better readable:

#define vcpu_vpmu(vcpu) (&(vcpu)->arch.vpmu)
#define vpmu_vcpu(vpmu) container_of(vpmu, struct vcpu, arch.vpmu)

Jan

#15 lore raw

PATCH v4 9/17 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        | 51 +++++++++++++++++++++++++
 xen/include/xen/hypercall.h        |  4 ++
 9 files changed, 138 insertions(+), 18 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +1 -1
index bf7f1f6..3dd6911 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 3c3bedc..9e0e743 100644
@@ -707,7 +707,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) )
@@ -826,7 +826,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 8646fd6..8c5c772 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))
 
@@ -98,5 +91,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 +51 -0

57 lines not on this page · raw message

xen/include/xen/hypercall.h +4 -0
index a9e5229..acf50e8 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(xen_pmu_params_t) arg);
+
 #ifdef CONFIG_COMPAT
 
 extern int
Signature · 1 line
1.8.1.4
#16 lore raw

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

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +8 -0
+long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret = -EINVAL;
+    xen_pmu_params_t pmu_params;
+    uint32_t mode;
+
+    switch ( op )
+    {

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+    case XENPMU_mode_set:
+        if ( !is_control_domain(current->domain) )
+            return -EPERM;
+
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        mode = (uint32_t)pmu_params.d.val & XENPMU_MODE_MASK;
+        if ( mode & ~XENPMU_MODE_ON )
+            return -EINVAL;

Please, if you add a new interface, think carefully about future extension room: Here you ignore the upper 32 bits of .val instead of making sure they're zero, thus making it impossible to assign them some meaning later on.

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

Quoted 12 lines
xen/arch/x86/hvm/vpmu.c +12 -0
+
+        vpmu_mode &= ~XENPMU_MODE_MASK;
+        vpmu_mode |= mode;
+
+        ret = 0;
+        break;
+
+    case XENPMU_mode_get:
+        pmu_params.d.val = vpmu_mode & XENPMU_MODE_MASK;
+        pmu_params.v.version.maj = XENPMU_VER_MAJ;
+        pmu_params.v.version.min = XENPMU_VER_MIN;
+        if ( copy_to_guest(arg, &pmu_params, 1) )

__copy_to_guest().

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

Quoted 13 lines
xen/arch/x86/hvm/vpmu.c +13 -0
+            return -EFAULT;
+        ret = 0;
+        break;
+
+    case XENPMU_feature_set:
+        if ( !is_control_domain(current->domain) )
+            return -EPERM;
+
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        if ( (uint32_t)pmu_params.d.val & ~XENPMU_FEATURE_INTEL_BTS )
+            return -EINVAL;

See above.

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+
+        vpmu_mode &= ~XENPMU_FEATURE_MASK;
+        vpmu_mode |= (uint32_t)pmu_params.d.val << XENPMU_FEATURE_SHIFT;
+
+        ret = 0;
+        break;
+
+    case XENPMU_feature_get:
+        pmu_params.d.val = vpmu_mode & XENPMU_FEATURE_MASK;
+        if ( copy_to_guest(arg, &pmu_params, 1) )

See above.

xen/include/public/xen.h DEFINE_XEN_GUEST_HANDLE(xen_ulong_t); · line 107

Quoted 10 lines
xen/include/public/xen.h +1 -0
@@ -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

Are you certain this wouldn't better be an architecture-specific hypercall? Just like with Machine Check, I don't think all architectures are guaranteed to have (or ever get) performance monitoring capabilities.

xen/include/public/xen.h

Quoted 10 lines
xen/include/public/xen.h +10 -0
+/* Parameters structure for HYPERVISOR_xenpmu_op call */
+struct xen_pmu_params {
+    /* IN/OUT parameters */
+    union {
+        struct version {
+            uint8_t maj;
+            uint8_t min;
+        } version;
+        uint64_t pad;
+    } v;

Looking at the implementation above I don't see this ever being an IN parameter.

Jan

#17 lore raw

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

Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +14 -0
+long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret = -EINVAL;
+    xen_pmu_params_t pmu_params;
+    uint32_t mode;
+
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+        if ( !is_control_domain(current->domain) )
+            return -EPERM;
+
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
Quoted 8 lines

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

Quoted 4 lines
xen/arch/x86/hvm/vpmu.c +4 -0
+
+        mode = (uint32_t)pmu_params.d.val & XENPMU_MODE_MASK;
+        if ( mode & ~XENPMU_MODE_ON )
+            return -EINVAL;

Please, if you add a new interface, think carefully about future extension room: Here you ignore the upper 32 bits of .val instead of making sure they're zero, thus making it impossible to assign them some meaning later on.

I think I can leave this as is for now --- I am storing VPMU mode and VPMU features in the Xen-private vpmu_mode, which is a 64-bit value.

What I probably should do is remove XENPMU_MODE_MASK (and XENPMU_FEATURE_SHIFT and XENPMU_FEATURE_MASK) from the public header since Linux passes down 64-bit pmu_params.d.val without any format assumptions anyway.

Quoted 9 lines
Quoted 9 lines
xen/include/public/xen.h +1 -0
@@ -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
 
Quoted 6 lines

xen/include/public/xen.h DEFINE_XEN_GUEST_HANDLE(xen_ulong_t); · line 107

Quoted 2 lines
xen/include/public/xen.h +0 -0
  /* Architecture-specific hypercall definitions. */
  #define __HYPERVISOR_arch_0               48

Are you certain this wouldn't better be an architecture-specific hypercall? Just like with Machine Check, I don't think all architectures are guaranteed to have (or ever get) performance monitoring capabilities.

An architecture doesn't necessarily need to have HW performance monitoring support. In principle this interface can be used for passing any performance-related data (e.g. collected by the hypervisor) to the guest.

Quoted 4 lines
Quoted 4 lines
xen/include/public/xen.h +4 -0
+/* Parameters structure for HYPERVISOR_xenpmu_op call */
+struct xen_pmu_params {
+    /* IN/OUT parameters */
+    union {
Quoted 8 lines

xen/include/public/xen.h

Quoted 6 lines
xen/include/public/xen.h +6 -0
+        struct version {
+            uint8_t maj;
+            uint8_t min;
+        } version;
+        uint64_t pad;
+    } v;

Looking at the implementation above I don't see this ever being an IN parameter.

Currently Xen doesn't care about version but in the future a guest may specify what version of PMU it wants to use (I hope this day will never come though...)

-boris

#18 lore raw

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

Quoted 26 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +14 -0
+long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret = -EINVAL;
+    xen_pmu_params_t pmu_params;
+    uint32_t mode;
+
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+        if ( !is_control_domain(current->domain) )
+            return -EPERM;
+
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
Quoted 8 lines

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

Quoted 4 lines
xen/arch/x86/hvm/vpmu.c +4 -0
+
+        mode = (uint32_t)pmu_params.d.val & XENPMU_MODE_MASK;
+        if ( mode & ~XENPMU_MODE_ON )
+            return -EINVAL;

Please, if you add a new interface, think carefully about future extension room: Here you ignore the upper 32 bits of .val instead of making sure they're zero, thus making it impossible to assign them some meaning later on.

Quoted 2 lines

I think I can leave this as is for now --- I am storing VPMU mode and VPMU features in the Xen-private vpmu_mode, which is a 64-bit value.

You should drop the cast to a 32-bit value at the very least - "leave this as is for now" reads like you don#t need to make any changes.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/include/public/xenpmu.h +4 -0
+/* Parameters structure for HYPERVISOR_xenpmu_op call */
+struct xen_pmu_params {
+    /* IN/OUT parameters */
+    union {
Quoted 8 lines

xen/include/public/xenpmu.h line 39

Quoted 6 lines
xen/include/public/xenpmu.h +6 -0
+        struct version {
+            uint8_t maj;
+            uint8_t min;
+        } version;
+        uint64_t pad;
+    } v;

Looking at the implementation above I don't see this ever being an IN parameter.

Quoted 3 lines

Currently Xen doesn't care about version but in the future a guest may specify what version of PMU it wants to use (I hope this day will never come though...)

At which time you'd need to add something like a set-version sub-op, which would then also be the time to make this as IN/OUT. Right now it is just IN and hence should be marked as such.

Jan

#19 lore raw

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

Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 22 lines
Quoted 8 lines
xen/arch/x86/hvm/vpmu.c +8 -0
+long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret = -EINVAL;
+    xen_pmu_params_t pmu_params;
+    uint32_t mode;
+
+    switch ( op )
+    {

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+    case XENPMU_mode_set:
+        if ( !is_control_domain(current->domain) )
+            return -EPERM;
+
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        mode = (uint32_t)pmu_params.d.val & XENPMU_MODE_MASK;
+        if ( mode & ~XENPMU_MODE_ON )
+            return -EINVAL;

Please, if you add a new interface, think carefully about future extension room: Here you ignore the upper 32 bits of .val instead of making sure they're zero, thus making it impossible to assign them some meaning later on.

Quoted 5 lines
Quoted 2 lines

I think I can leave this as is for now --- I am storing VPMU mode and VPMU features in the Xen-private vpmu_mode, which is a 64-bit value.

You should drop the cast to a 32-bit value at the very least - "leave this as is for now" reads like you don#t need to make any changes.

mode is stored in the lower 32 bits of vpmu_mode variable a few lines below

     vpmu_mode &= ~XENPMU_MODE_MASK; // XENPMU_MODE_MASK - 0xffffffff
     vpmu_mode |= mode;

so the cast needs to happen somewhere. I can move it to the line above although I am not sure what difference that would make.

-boris

#20 lore raw

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

Quoted 30 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 22 lines
Quoted 8 lines
xen/arch/x86/hvm/vpmu.c +8 -0
+long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret = -EINVAL;
+    xen_pmu_params_t pmu_params;
+    uint32_t mode;
+
+    switch ( op )
+    {

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+    case XENPMU_mode_set:
+        if ( !is_control_domain(current->domain) )
+            return -EPERM;
+
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        mode = (uint32_t)pmu_params.d.val & XENPMU_MODE_MASK;
+        if ( mode & ~XENPMU_MODE_ON )
+            return -EINVAL;

Please, if you add a new interface, think carefully about future extension room: Here you ignore the upper 32 bits of .val instead of making sure they're zero, thus making it impossible to assign them some meaning later on.

Quoted 5 lines
Quoted 2 lines

I think I can leave this as is for now --- I am storing VPMU mode and VPMU features in the Xen-private vpmu_mode, which is a 64-bit value.

You should drop the cast to a 32-bit value at the very least - "leave this as is for now" reads like you don#t need to make any changes.

Quoted 8 lines

mode is stored in the lower 32 bits of vpmu_mode variable a few lines below

      vpmu_mode &= ~XENPMU_MODE_MASK; // XENPMU_MODE_MASK - 0xffffffff
      vpmu_mode |= mode;

so the cast needs to happen somewhere. I can move it to the line above although I
am not sure what difference that would make.

I don't really care what you do here, so long as you don't ignore data passed into the hypercall.

Jan

#21 lore raw

PATCH v4 10/17 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 | 50 ++++++++++++++++---------
 xen/arch/x86/hvm/vpmu.c           | 77 ++++++++++++++++++++++++++++++++++++++-
 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, 136 insertions(+), 35 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +21 -17
index 3dd6911..e2bff67 100644
@@ -373,16 +373,21 @@ static int amd_vpmu_initialise(struct vcpu *v)
 	 }
     }
 
-    ctxt = xzalloc_bytes(sizeof(struct xen_pmu_amd_ctxt) + 
-			 sizeof(uint64_t) * AMD_MAX_COUNTERS + 
-			 sizeof(uint64_t) * AMD_MAX_COUNTERS);
-    if ( !ctxt )
+    if ( !is_pv_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 xen_pmu_amd_ctxt) + 
+                             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.c.amd;
 
     ctxt->counters = sizeof(struct xen_pmu_amd_ctxt);
     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_pv_domain(v->domain) &&
-         ((struct xen_pmu_amd_ctxt *)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_pv_domain(v->domain) )
     {
-        vpmu_reset(vpmu, VPMU_RUNNING);
+        if ( ((struct xen_pmu_amd_ctxt *)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 +33 -17

72 lines not on this page · raw message

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

106 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 8c5c772..29bb977 100644
@@ -60,6 +60,7 @@ struct vpmu_struct {
     u32 hw_lapic_lvtpc;
     void *context;
     struct arch_vpmu_ops *arch_vpmu_ops;
+    xen_pmu_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 fac29a6..9424313 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
 /* ` } */
 
 /* Parameters structure for HYPERVISOR_xenpmu_op call */
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
#22 lore raw

Re: [PATCH v4 10/17] x86/VPMU: Initialize PMU for PV guests

Quoted 16 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +15 -0
+static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
+{
+    struct vcpu *v;
+    struct page_info *page;
+    uint64_t gmfn = params->d.val;
+
+    if ( params->vcpu < 0 || params->vcpu >= d->max_vcpus )
+        return -EINVAL;
+
+    page = get_page_from_gfn(d, gmfn, NULL, P2M_ALLOC);
+    if ( !page )
+        return -EINVAL;
+
+    v = d->vcpu[params->vcpu];
+    v->arch.vpmu.xenpmu_data = __map_domain_page_global(page);

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+    if ( !v->arch.vpmu.xenpmu_data )
+    {
+        put_page(page);
+        return -EINVAL;
+    }
+
+    vpmu_initialise(v);
+
+    return 0;
+}

This being for a PV guest, you need to obtain a write type reference to the page, or else you risk the guest re-using the page for something that mustn't be written to in uncontrolled ways (like a page or descriptor table). See e.g. map_vcpu_info().

Jan

#23 lore raw

PATCH v4 11/17 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              | 31 +++++++++++++++++++-
 xen/include/public/xenpmu.h       |  1 +
 5 files changed, 90 insertions(+), 13 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 23b3040..d32325c 100644
@@ -400,6 +400,14 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_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.d.val);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/arch/x86/traps.c +30 -1
index 3f7a3c7..7ff8401 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>
 
@@ -865,8 +866,10 @@ void pv_cpuid(struct cpu_user_regs *regs)
         __clear_bit(X86_FEATURE_TOPOEXT % 32, &c);
         break;
 
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        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 */
@@ -882,6 +885,8 @@ void pv_cpuid(struct cpu_user_regs *regs)
     }
 
  out:
+    vpmu_do_cpuid(regs->eax, &a, &b, &c, &d);
+
     regs->eax = a;
     regs->ebx = b;
     regs->ecx = c;
@@ -2499,6 +2504,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 +2600,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 9424313..c22cd18 100644
@@ -27,6 +27,7 @@
 #define XENPMU_feature_set     3
 #define XENPMU_init            4
 #define XENPMU_finish          5
+#define XENPMU_lvtpc_set       6
 /* ` } */
 
 /* Parameters structure for HYPERVISOR_xenpmu_op call */
Signature · 1 line
1.8.1.4
#24 lore raw

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

xen/arch/x86/hvm/vpmu.c long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg) · line 408

Quoted 13 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +6 -0
@@ -400,6 +400,14 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_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.d.val);

Once again, please don't ignore (parts of) hypercall input values.

Jan

#25 lore raw

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

Quoted 14 lines · Jan Beulich

xen/arch/x86/hvm/vpmu.c long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg) · line 408

Quoted 13 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +6 -0
@@ -400,6 +400,14 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_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.d.val);

Once again, please don't ignore (parts of) hypercall input values.

I can actually pass this value in the shared area where I already have a uint32_t for LVTPC. It will also save us from doing the copy.

-boris

#26 lore raw

PATCH v4 12/17 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     | 116 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/public/xenpmu.h |   7 +++
 2 files changed, 116 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/vpmu.c +109 -7
index d32325c..aead6af 100644
@@ -75,7 +75,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_pv_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)
@@ -83,7 +88,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 ret = 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);
+            vpmu->arch_vpmu_ops->arch_vpmu_save(current);
+            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+        }
+        return ret;
+    }
     return 0;
 }
 
@@ -92,16 +113,86 @@ 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 ret = vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
+
+        if ( !is_hvm_domain(current->domain) &&
+            (current->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED) )
+        {
+            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+            vpmu->arch_vpmu_ops->arch_vpmu_save(current);
+            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
+        }
+        return ret;
+    }
     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 )
+        v = dom0->vcpu[smp_processor_id() % dom0->max_vcpus];
+
+    vpmu = vcpu_vpmu(v);
+    if ( !is_hvm_domain(v->domain) )
+    {
+        /* PV guest or dom0 is doing system profiling */
+        const 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 */
+        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();
+
+            cmp = (struct compat_cpu_user_regs *)
+                    &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            XLAT_cpu_user_regs(cmp, gregs);
+        }
+        else if ( !is_control_domain(current->domain) &&
+                 !is_idle_vcpu(current) )
+        {
+            /* PV guest */
+            gregs = guest_cpu_user_regs();
+            memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                   gregs, sizeof(struct cpu_user_regs));
+        }
+        else
+            memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                   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;
@@ -213,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_pv_domain(v->domain) &&
+          vpmu->xenpmu_data->pmu_flags & PMU_CACHED) )
         return;
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_load )
@@ -408,6 +504,12 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
         vpmu_lvtpc_update((uint32_t)pmu_params.d.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.l.lapic_lvtpc);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/include/public/xenpmu.h +7 -0
index c22cd18..df85209 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     */
 /* ` } */
 
 /* Parameters structure for HYPERVISOR_xenpmu_op call */
@@ -68,6 +69,12 @@ DEFINE_XEN_GUEST_HANDLE(xen_pmu_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 xen_pmu_data {
     uint32_t domain_id;
Signature · 1 line
1.8.1.4
#27 lore raw

Re: [PATCH v4 12/17] x86/VPMU: Handle PMU interrupts for PV guests

Quoted 31 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

int vpmu_do_interrupt(struct cpu_user_regs *regs)

 {
     struct vcpu *v = current;
xen/arch/x86/hvm/vpmu.c +24 -2
-    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 )
+        v = dom0->vcpu[smp_processor_id() % dom0->max_vcpus];
+
+    vpmu = vcpu_vpmu(v);
+    if ( !is_hvm_domain(v->domain) )
+    {
+        /* PV guest or dom0 is doing system profiling */
+        const 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 */
+        if ( is_pv_32bit_domain(current->domain) )
+        {
+            /*

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+             * 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();
+
+            cmp = (struct compat_cpu_user_regs *)
+                    &v->arch.vpmu.xenpmu_data->pmu.r.regs;

Deliberate type changes like this can easily (and more readably as well as more forward compatibly) be done using (void *).

Quoted 17 lines
xen/arch/x86/hvm/vpmu.c +17 -0
+            XLAT_cpu_user_regs(cmp, gregs);
+        }
+        else if ( !is_control_domain(current->domain) &&
+                 !is_idle_vcpu(current) )
+        {
+            /* PV guest */
+            gregs = guest_cpu_user_regs();
+            memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                   gregs, sizeof(struct cpu_user_regs));
+        }
+        else
+            memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                   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();

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+
+        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 )

If the previous (and only) if() branch returns unconditionally, using "else if" is more confusing then clarifying imo (and in any case needlessly growing the patch, even if just by a bit).

Jan

#28 lore raw

Re: [PATCH v4 12/17] x86/VPMU: Handle PMU interrupts for PV guests

Quoted 66 lines · Jan Beulich
Quoted 31 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

int vpmu_do_interrupt(struct cpu_user_regs *regs)

  {
      struct vcpu *v = current;
xen/arch/x86/hvm/vpmu.c +24 -2
-    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 )
+        v = dom0->vcpu[smp_processor_id() % dom0->max_vcpus];
+
+    vpmu = vcpu_vpmu(v);
+    if ( !is_hvm_domain(v->domain) )
+    {
+        /* PV guest or dom0 is doing system profiling */
+        const 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 */
+        if ( is_pv_32bit_domain(current->domain) )
+        {
+            /*

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+             * 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();
+
+            cmp = (struct compat_cpu_user_regs *)
+                    &v->arch.vpmu.xenpmu_data->pmu.r.regs;

Deliberate type changes like this can easily (and more readably as well as more forward compatibly) be done using (void *).

Quoted 22 lines
xen/arch/x86/hvm/vpmu.c +22 -0
+            XLAT_cpu_user_regs(cmp, gregs);
+        }
+        else if ( !is_control_domain(current->domain) &&
+                 !is_idle_vcpu(current) )
+        {
+            /* PV guest */
+            gregs = guest_cpu_user_regs();
+            memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                   gregs, sizeof(struct cpu_user_regs));
+        }
+        else
+            memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                   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;
+
Quoted 8 lines

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

Quoted 5 lines
xen/arch/x86/hvm/vpmu.c +5 -0
+        send_guest_vcpu_virq(v, VIRQ_XENPMU);
+
+        return 1;
+    }
+    else if ( vpmu->arch_vpmu_ops )

If the previous (and only) if() branch returns unconditionally, using "else if" is more confusing then clarifying imo (and in any case needlessly growing the patch, even if just by a bit).

Not sure I understand what you are saying here.

Here is the code structure:

int vpmu_do_interrupt(struct cpu_user_regs *regs)

{
     if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
{
        // work
        return 1;
}
    else if ( vpmu->arch_vpmu_ops )
{
        if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) )
            return 0;

        // other work
        return 1;
}
    return 0;
}

What do you propose?

-boris

#29 lore raw

Re: [PATCH v4 12/17] x86/VPMU: Handle PMU interrupts for PV guests

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

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

Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +3 -0
+        return 1;
+    }
+    else if ( vpmu->arch_vpmu_ops )

If the previous (and only) if() branch returns unconditionally, using "else if" is more confusing then clarifying imo (and in any case needlessly growing the patch, even if just by a bit).

Not sure I understand what you are saying here.

Here is the code structure:

int vpmu_do_interrupt(struct cpu_user_regs *regs)

Quoted 8 lines
{
      if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
{
         // work
         return 1;
}
     else if ( vpmu->arch_vpmu_ops )

{

      if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
{
         // work
         return 1;
}
     if ( vpmu->arch_vpmu_ops )
{
         ...

Jan

#30 lore raw

PATCH v4 13/17 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     | 88 ++++++++++++++++++++++++++++++++-------------
 xen/arch/x86/traps.c        |  6 +++-
 xen/include/public/xenpmu.h |  3 ++
 3 files changed, 72 insertions(+), 25 deletions(-)
xen/arch/x86/hvm/vpmu.c +64 -24
index aead6af..214300d 100644
@@ -87,6 +87,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 ret = vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content);
@@ -112,6 +115,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 ret = vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
@@ -134,14 +140,18 @@ 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) )
         v = dom0->vcpu[smp_processor_id() % dom0->max_vcpus];
 
     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 */
-        const struct cpu_user_regs *gregs;
+        struct cpu_user_regs *gregs;
         int err;
 
         if ( v->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED )
@@ -152,33 +162,62 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
         err = vpmu->arch_vpmu_ops->arch_vpmu_save(v);
         vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
 
-        /* Store appropriate registers in xenpmu_data */
-        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();
-
-            cmp = (struct compat_cpu_user_regs *)
-                    &v->arch.vpmu.xenpmu_data->pmu.r.regs;
-            XLAT_cpu_user_regs(cmp, gregs);
+            uint16_t cs = (current->arch.flags & TF_kernel_mode) ? 0 : 0x3;
+
+            /* Store appropriate registers in xenpmu_data */
+            if ( is_pv_32bit_domain(current->domain) )
+            {
+                gregs = guest_cpu_user_regs();
+
+                if ( (vpmu_mode & XENPMU_MODE_PRIV) &&
+                     !is_pv_32bit_domain(v->domain) )
+                    memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                           gregs, sizeof(struct cpu_user_regs));
+                else 
+                {
+                    /*
+                     * 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;
+
+                    cmp = (struct compat_cpu_user_regs *)
+                        &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+                    XLAT_cpu_user_regs(cmp, gregs);
+                }
+            }
+            else if ( !is_control_domain(current->domain) &&
+                      !is_idle_vcpu(current) )
+            {
+                /* PV guest */
+                gregs = guest_cpu_user_regs();
+                memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                       gregs, sizeof(struct cpu_user_regs));
+            }
+            else
+                memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                       regs, sizeof(struct cpu_user_regs));
+
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs;
         }
-        else if ( !is_control_domain(current->domain) &&
-                 !is_idle_vcpu(current) )
+        else
         {
-            /* PV guest */
+            /* HVM guest */
+            struct segment_register cs;
+
             gregs = guest_cpu_user_regs();
             memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
                    gregs, sizeof(struct cpu_user_regs));
+
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs.attr.fields.dpl;
         }
-        else
-            memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
-                   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;
@@ -444,7 +483,8 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
             return -EFAULT;
 
         mode = (uint32_t)pmu_params.d.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 7ff8401..1854230 100644
@@ -2510,7 +2510,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 df85209..f715f30 100644
@@ -56,11 +56,14 @@ DEFINE_XEN_GUEST_HANDLE(xen_pmu_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
#31 lore raw

Re: [PATCH v4 13/17] x86/VPMU: Add privileged PMU mode

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +1 -5
@@ -152,33 +162,62 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
         err = vpmu->arch_vpmu_ops->arch_vpmu_save(v);
         vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
 
-        /* Store appropriate registers in xenpmu_data */
-        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

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +1 -9
-             * PV 32-bit domain.
-             */
-            struct compat_cpu_user_regs *cmp;
-
-            gregs = guest_cpu_user_regs();
-
-            cmp = (struct compat_cpu_user_regs *)
-                    &v->arch.vpmu.xenpmu_data->pmu.r.regs;
-            XLAT_cpu_user_regs(cmp, gregs);
+            uint16_t cs = (current->arch.flags & TF_kernel_mode) ? 0 : 0x3;

The surrounding if checks !hvm, i.e. both PV and PVH can make it here. But TF_kernel_mode is meaningful for PV only.

Quoted 29 lines
xen/arch/x86/hvm/vpmu.c +29 -0
+
+            /* Store appropriate registers in xenpmu_data */
+            if ( is_pv_32bit_domain(current->domain) )
+            {
+                gregs = guest_cpu_user_regs();
+
+                if ( (vpmu_mode & XENPMU_MODE_PRIV) &&
+                     !is_pv_32bit_domain(v->domain) )
+                    memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                           gregs, sizeof(struct cpu_user_regs));
+                else 
+                {
+                    /*
+                     * 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;
+
+                    cmp = (struct compat_cpu_user_regs *)
+                        &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+                    XLAT_cpu_user_regs(cmp, gregs);
+                }
+            }
+            else if ( !is_control_domain(current->domain) &&
+                      !is_idle_vcpu(current) )
+            {
+                /* PV guest */

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+                gregs = guest_cpu_user_regs();
+                memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                       gregs, sizeof(struct cpu_user_regs));
+            }
+            else
+                memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                       regs, sizeof(struct cpu_user_regs));
+
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs;

And now you store a NUL selector (i.e. just the RPL bits) into the output field?

Quoted 6 lines
xen/arch/x86/hvm/vpmu.c +1 -3
         }
-        else if ( !is_control_domain(current->domain) &&
-                 !is_idle_vcpu(current) )
+        else
         {
-            /* PV guest */

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +7 -0
+            /* HVM guest */
+            struct segment_register cs;
+
             gregs = guest_cpu_user_regs();
             memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
                    gregs, sizeof(struct cpu_user_regs));
+
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs.attr.fields.dpl;

And here too? If that's intended, a code comment is a must.

Jan

#32 lore raw

Re: [PATCH v4 13/17] x86/VPMU: Add privileged PMU mode

Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +1 -9
@@ -152,33 +162,62 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
          err = vpmu->arch_vpmu_ops->arch_vpmu_save(v);
          vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
 
-        /* Store appropriate registers in xenpmu_data */
-        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;
-
Quoted 8 lines

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

Quoted 6 lines
xen/arch/x86/hvm/vpmu.c +1 -5
-            gregs = guest_cpu_user_regs();
-
-            cmp = (struct compat_cpu_user_regs *)
-                    &v->arch.vpmu.xenpmu_data->pmu.r.regs;
-            XLAT_cpu_user_regs(cmp, gregs);
+            uint16_t cs = (current->arch.flags & TF_kernel_mode) ? 0 : 0x3;

The surrounding if checks !hvm, i.e. both PV and PVH can make it here. But TF_kernel_mode is meaningful for PV only.

As of this patch PVH doesn't work and you won't get into this code path. And the later patch (#16) addresses this. (Although it unnecessary calculates cs in the line above for PVH, only to call hvm_get_segment_register() later. I'll move it to !pvh clause there).

Quoted 51 lines
Quoted 30 lines
xen/arch/x86/hvm/vpmu.c +29 -0
+
+            /* Store appropriate registers in xenpmu_data */
+            if ( is_pv_32bit_domain(current->domain) )
+            {
+                gregs = guest_cpu_user_regs();
+
+                if ( (vpmu_mode & XENPMU_MODE_PRIV) &&
+                     !is_pv_32bit_domain(v->domain) )
+                    memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                           gregs, sizeof(struct cpu_user_regs));
+                else
+                {
+                    /*
+                     * 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;
+
+                    cmp = (struct compat_cpu_user_regs *)
+                        &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+                    XLAT_cpu_user_regs(cmp, gregs);
+                }
+            }
+            else if ( !is_control_domain(current->domain) &&
+                      !is_idle_vcpu(current) )
+            {
+                /* PV guest */

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+                gregs = guest_cpu_user_regs();
+                memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                       gregs, sizeof(struct cpu_user_regs));
+            }
+            else
+                memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
+                       regs, sizeof(struct cpu_user_regs));
+
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs;

And now you store a NUL selector (i.e. just the RPL bits) into the output field?

Quoted 9 lines
xen/arch/x86/hvm/vpmu.c +4 -3
          }
-        else if ( !is_control_domain(current->domain) &&
-                 !is_idle_vcpu(current) )
+        else
          {
-            /* PV guest */
+            /* HVM guest */
+            struct segment_register cs;
+
Quoted 8 lines

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

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +4 -0
              gregs = guest_cpu_user_regs();
              memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
                     gregs, sizeof(struct cpu_user_regs));
+
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs.attr.fields.dpl;

And here too? If that's intended, a code comment is a must.

This is HVM-only path, PVH or PV don't go here so cs should be valid.

-boris

#33 lore raw

Re: [PATCH v4 13/17] x86/VPMU: Add privileged PMU mode

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

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

Quoted 3 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +2 -0
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs;

And now you store a NUL selector (i.e. just the RPL bits) into the output field?

Quoted 9 lines
          }
xen/arch/x86/hvm/vpmu.c +4 -3
-        else if ( !is_control_domain(current->domain) &&
-                 !is_idle_vcpu(current) )
+        else
          {
-            /* PV guest */
+            /* HVM guest */
+            struct segment_register cs;
+
Quoted 8 lines

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

Quoted 7 lines
              gregs = guest_cpu_user_regs();
              memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
                     gregs, sizeof(struct cpu_user_regs));
xen/arch/x86/hvm/vpmu.c +4 -0
+
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs.attr.fields.dpl;

And here too? If that's intended, a code comment is a must.

Quoted 1 line

This is HVM-only path, PVH or PV don't go here so cs should be valid.

Isn't the reply of mine a few lines up in PV code? And why would the selector being wrong for HVM be okay?

Jan

#34 lore raw

Re: [PATCH v4 13/17] x86/VPMU: Add privileged PMU mode

Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 22 lines · Jan Beulich

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

Quoted 3 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +2 -0
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs;

And now you store a NUL selector (i.e. just the RPL bits) into the output field?

Quoted 6 lines
           }
xen/arch/x86/hvm/vpmu.c +1 -3
-        else if ( !is_control_domain(current->domain) &&
-                 !is_idle_vcpu(current) )
+        else
           {
-            /* PV guest */

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +7 -0
+            /* HVM guest */
+            struct segment_register cs;
+
               gregs = guest_cpu_user_regs();
               memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
                      gregs, sizeof(struct cpu_user_regs));
+
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs.attr.fields.dpl;

And here too? If that's intended, a code comment is a must.

Quoted 3 lines
Quoted 1 line

This is HVM-only path, PVH or PV don't go here so cs should be valid.

Isn't the reply of mine a few lines up in PV code? And why would the selector being wrong for HVM be okay?

This clause is for privileged profiling: we are in PV clause even though the interrupt is taken by an HVM guest.

The diff is somewhat difficult to follow so here is the flow:

   // in privileged mode 'v' is dom0's CPU.
   if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
   {
        if ( !is_hvm_domain(current->domain) )
        {
             // either PV (including dom0) or Xen is interrupted
        }
        else
        {
             // This is the clause we are discussing. 'current' is HVM
             hvm_get_segment_register(current, x86_seg_cs, &cs);
        }
        send_guest_vcpu_virq(v, VIRQ_XENPMU);
        return 1;
   }

So I think CS should be correct for the guest, no?

-boris

#35 lore raw

Re: [PATCH v4 13/17] x86/VPMU: Add privileged PMU mode

Quoted 52 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 22 lines · Jan Beulich

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

Quoted 3 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +2 -0
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs;

And now you store a NUL selector (i.e. just the RPL bits) into the output field?

Quoted 6 lines
           }
xen/arch/x86/hvm/vpmu.c +1 -3
-        else if ( !is_control_domain(current->domain) &&
-                 !is_idle_vcpu(current) )
+        else
           {
-            /* PV guest */

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +7 -0
+            /* HVM guest */
+            struct segment_register cs;
+
               gregs = guest_cpu_user_regs();
               memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
                      gregs, sizeof(struct cpu_user_regs));
+
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            gregs->cs = cs.attr.fields.dpl;

And here too? If that's intended, a code comment is a must.

Quoted 3 lines
Quoted 1 line

This is HVM-only path, PVH or PV don't go here so cs should be valid.

Isn't the reply of mine a few lines up in PV code? And why would the selector being wrong for HVM be okay?

This clause is for privileged profiling: we are in PV clause even though the interrupt is taken by an HVM guest.

The diff is somewhat difficult to follow so here is the flow:

    // in privileged mode 'v' is dom0's CPU.
    if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
    {
         if ( !is_hvm_domain(current->domain) )
         {
              // either PV (including dom0) or Xen is interrupted
         }
         else
         {
              // This is the clause we are discussing. 'current' is HVM
              hvm_get_segment_register(current, x86_seg_cs, &cs);
         }
         send_guest_vcpu_virq(v, VIRQ_XENPMU);
         return 1;
    }
Quoted 1 line

So I think CS should be correct for the guest, no?

Honestly - I can't tell. All I can tell is that there's a bogus setting of cs to 0 or 3 (depending on whether in kernel mode) or to the dpl field of the descriptor read from the hardware. All of which are wrong without a cleat comment stating why doing it this way is okay/acceptable/necessary-for-the-time-being.

So either drop this bogus code, or comment it properly.

Jan

#36 lore raw

PATCH v4 14/17 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) and the switched out domain is not the control domain. The latter condition is needed because me may have just turned the privileged PMU mode on and thus need to save last domain.

---
 xen/arch/x86/domain.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
xen/arch/x86/domain.c +7 -8
index 25572d5..124c0e7 100644
@@ -1444,17 +1444,16 @@ 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) ||
+             !is_control_domain(prev->domain) )
             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 +1490,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
#37 lore raw

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

Quoted 7 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) and the switched
out domain is not the control domain. The latter condition is needed because me may have just turned the privileged PMU mode on and thus need to save last domain.

While this is understandable, ...

Quoted 5 lines
xen/arch/x86/domain.c +0 -0
@@ -1444,17 +1444,16 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
     }
 

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

Quoted 10 lines
xen/arch/x86/domain.c +3 -4
     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) ||
+             !is_control_domain(prev->domain) )
             vpmu_save(prev);

... I'd really like you to investigate ways to achieve the same effect without this extra second condition added to the context switch path. E.g. by synchronously issuing a save on all affected vCPU-s when privileged mode gets turned on.

Jan

#38 lore raw

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

Quoted 14 lines · Jan Beulich
Quoted 7 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) and the switched
out domain is not the control domain. The latter condition is needed because me may have just turned the privileged PMU mode on and thus need to save last domain.

While this is understandable, ...

Quoted 5 lines
xen/arch/x86/domain.c +0 -0
@@ -1444,17 +1444,16 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
      }
 
Quoted 14 lines

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

Quoted 10 lines
xen/arch/x86/domain.c +3 -4
      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) ||
+             !is_control_domain(prev->domain) )
              vpmu_save(prev);

... I'd really like you to investigate ways to achieve the same effect without this extra second condition added to the context switch path. E.g. by synchronously issuing a save on all affected vCPU-s when privileged mode gets turned on.

Yes, I should do something like that.

-boris

#39 lore raw

PATCH v4 15/17 x86/VPMU: NMI-based VPMU support

In reply to Boris Ostrovsky

Add support for using NMIs as PMU interrupts.

Most of processing is still performed by vpmu_do_interrupt(). However, since certain operations are not NMI-safe we defer them to a softint that vpmu_do_interrupt() will schedule:
* For PV guests that would be send_guest_vcpu_virq() and hvm_get_segment_register().
* For HVM guests it's VLAPIC accesses.

---
 xen/arch/x86/hvm/vpmu.c | 169 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 135 insertions(+), 34 deletions(-)
xen/arch/x86/hvm/vpmu.c +135 -34
index 214300d..e76b538 100644
@@ -36,6 +36,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>
 
 /*
@@ -48,33 +49,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_pv_domain(current->domain) ||
@@ -83,6 +108,24 @@ void vpmu_lvtpc_update(uint32_t val)
         apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
 }
 
+static void vpmu_send_nmi(struct vcpu *v)
+{
+    struct vlapic *vlapic = vcpu_vlapic(v);
+    u32 vlapic_lvtpc;
+    unsigned char int_vec;
+
+    if ( !is_vlapic_lvtpc_enabled(vlapic) )
+        return;
+
+    vlapic_lvtpc = vlapic_get_reg(vlapic, APIC_LVTPC);
+    int_vec = vlapic_lvtpc & APIC_VECTOR_MASK;
+
+    if ( GET_APIC_DELIVERY_MODE(vlapic_lvtpc) == APIC_MODE_FIXED )
+        vlapic_set_irq(vcpu_vlapic(v), int_vec, 0);
+    else
+        v->nmi_pending = 1;
+}
+
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
@@ -134,6 +177,7 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
     return 0;
 }
 
+/* This routine may be called in NMI context */
 int vpmu_do_interrupt(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
@@ -214,9 +258,13 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
             memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
                    gregs, sizeof(struct cpu_user_regs));
 
-            hvm_get_segment_register(current, x86_seg_cs, &cs);
-            gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
-            gregs->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);
+                gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+                gregs->cs = cs.attr.fields.dpl;
+            }
         }
 
         v->arch.vpmu.xenpmu_data->domain_id = current->domain->domain_id;
@@ -227,29 +275,29 @@ 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;
     }
     else if ( vpmu->arch_vpmu_ops )
     {
-        struct vlapic *vlapic = vcpu_vlapic(v);
-        u32 vlapic_lvtpc;
-        unsigned char int_vec;
-
         if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) )
             return 0;
 
-        if ( !is_vlapic_lvtpc_enabled(vlapic) )
-            return 1;
-
-        vlapic_lvtpc = vlapic_get_reg(vlapic, APIC_LVTPC);
-        int_vec = vlapic_lvtpc & APIC_VECTOR_MASK;
-
-        if ( GET_APIC_DELIVERY_MODE(vlapic_lvtpc) == APIC_MODE_FIXED )
-            vlapic_set_irq(vcpu_vlapic(v), int_vec, 0);
+        if ( vpmu_apic_vector & APIC_DM_NMI )
+        {
+            per_cpu(sampled_vcpu, smp_processor_id()) = current;
+            raise_softirq(PMU_SOFTIRQ);
+        }
         else
-            v->nmi_pending = 1;
+            vpmu_send_nmi(v);
+
         return 1;
     }
 
@@ -299,7 +347,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)
@@ -414,12 +462,50 @@ 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() % dom0->max_vcpus];
+    else
+    {
+        if ( is_hvm_domain(sampled->domain) )
+        {
+            vpmu_send_nmi(sampled);
+            return;
+        }
+        v = sampled;
+    }
+
+    regs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
+    if ( !is_pv_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, xen_pmu_params_t *params)
 {
     struct vcpu *v;
     struct page_info *page;
     uint64_t gmfn = params->d.val;
-
+    static int pvpmu_initted = 0;
+ 
     if ( params->vcpu < 0 || params->vcpu >= d->max_vcpus )
         return -EINVAL;
 
@@ -435,6 +521,21 @@ static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
         return -EINVAL;
     }
 
+    if ( !pvpmu_initted )
+    {
+        if (reserve_lapic_nmi() == 0)
+            set_nmi_callback(pmu_nmi_interrupt);
+        else
+        {
+            printk("Failed to reserve PMU NMI\n");
+            put_page(page);
+            return -EBUSY;
+        }
+        open_softirq(PMU_SOFTIRQ, pmu_softnmi);
+
+        pvpmu_initted = 1;
+    }
+
     vpmu_initialise(v);
 
     return 0;
Signature · 1 line
1.8.1.4
#40 lore raw

Re: [PATCH v4 15/17] x86/VPMU: NMI-based VPMU support

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Add support for using NMIs as PMU interrupts.

Most of processing is still performed by vpmu_do_interrupt(). However, since certain operations are not NMI-safe we defer them to a softint that vpmu_do_interrupt()
will schedule:
* For PV guests that would be send_guest_vcpu_virq() and hvm_get_segment_register().

Makes no sense - why would hvm_get_segment_register() be of any relevance to PV guests?

And then I'm still missing a reasonable level of analysis that the previously non-NMI-only interrupt handler is now safe to use in NMI context.

Quoted 1 line

+uint32_t vpmu_apic_vector = PMU_APIC_VECTOR;

Considering that you store APIC_DM_NMI into this variable in the NMI case, it needs to be named differently (or else I'd be tempted to convert it to uint8_t the first time I stumble across it).

xen/arch/x86/hvm/vpmu.c void vpmu_lvtpc_update(uint32_t val) · line 113

Quoted 3 lines
xen/arch/x86/hvm/vpmu.c +3 -0
+static void vpmu_send_nmi(struct vcpu *v)
+{
+    struct vlapic *vlapic = vcpu_vlapic(v);

Please ASSERT() that you have HVM data available before doing anything that would be unsafe in PV (and maybe PVH?) context. This will then at once serve as documentation, clarifying that the function must only be used for suitable vCPU-s.

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

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +7 -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 ||

() around the & please.

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

Quoted 7 lines

static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)

 {
     struct vcpu *v;
     struct page_info *page;
     uint64_t gmfn = params->d.val;
xen/arch/x86/hvm/vpmu.c +1 -1
-
+    static int pvpmu_initted = 0;

bool_t? __read_mostly?

Jan

#41 lore raw

Re: [PATCH v4 15/17] x86/VPMU: NMI-based VPMU support

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

Add support for using NMIs as PMU interrupts.

Most of processing is still performed by vpmu_do_interrupt(). However, since certain operations are not NMI-safe we defer them to a softint that vpmu_do_interrupt()
will schedule:
* For PV guests that would be send_guest_vcpu_virq() and hvm_get_segment_register().

Makes no sense - why would hvm_get_segment_register() be of any relevance to PV guests?

Poorly written explanation. What I meant here is that if we are in privileged profiling mode and the interrupted guest is an HVM one then we'll need to get CS for that guest, not for the guest doing profiling (i.e. dom0). I'll rewrite this.

Quoted 4 lines

And then I'm still missing a reasonable level of analysis that the previously non-NMI-only interrupt handler is now safe to use in NMI context.

How about this?

With send_guest_vcpu_virq() and hvm_get_segment_register() for PV(H) and vlapic accesses for HVM moved to sofint, the only routines/macros that vpmu_do_interrupt() calls in NMI mode are:
* memcpy()
* querying domain type (is_XX_domain())
* guest_cpu_user_regs()
* XLAT_cpu_user_regs()
* raise_softirq()
* vcpu_vpmu()
* vpmu_ops->arch_vpmu_save()
* vpmu_ops->do_interrupt() (in the future for PVH support)

The latter two can only access PMU MSRs.

-boris

#42 lore raw

Re: [PATCH v4 15/17] x86/VPMU: NMI-based VPMU support

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

And then I'm still missing a reasonable level of analysis that the previously non-NMI-only interrupt handler is now safe to use in NMI context.

How about this?

Looks okay, except ...

Quoted 12 lines

With send_guest_vcpu_virq() and hvm_get_segment_register() for PV(H) and vlapic accesses for HVM moved to sofint, the only routines/macros that vpmu_do_interrupt() calls in NMI mode are:
* memcpy()
* querying domain type (is_XX_domain())
* guest_cpu_user_regs()
* XLAT_cpu_user_regs()
* raise_softirq()
* vcpu_vpmu()
* vpmu_ops->arch_vpmu_save()
* vpmu_ops->do_interrupt() (in the future for PVH support)

Quoted 1 line

The latter two can only access PMU MSRs.

... that this additionally needs to exclude things like {rd,wr}msr_safe() (i.e. stuff raising exceptions that normally get recovered from).

Jan

#43 lore raw

Re: [PATCH v4 15/17] x86/VPMU: NMI-based VPMU support

Quoted 20 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 3 lines · Jan Beulich

And then I'm still missing a reasonable level of analysis that the previously non-NMI-only interrupt handler is now safe to use in NMI context.

How about this?

Looks okay, except ...

Quoted 12 lines

With send_guest_vcpu_virq() and hvm_get_segment_register() for PV(H) and vlapic accesses for HVM moved to sofint, the only routines/macros that vpmu_do_interrupt() calls in NMI mode are:
* memcpy()
* querying domain type (is_XX_domain())
* guest_cpu_user_regs()
* XLAT_cpu_user_regs()
* raise_softirq()
* vcpu_vpmu()
* vpmu_ops->arch_vpmu_save()
* vpmu_ops->do_interrupt() (in the future for PVH support)

Quoted 4 lines
Quoted 1 line

The latter two can only access PMU MSRs.

... that this additionally needs to exclude things like {rd,wr}msr_safe() (i.e. stuff raising exceptions that normally get recovered from).

I'll add that. And probably a comment in vendor-specific code to remind people that these routines need to be NMI-safe.

-boris

#44 lore raw

PATCH v4 16/17 x86/VPMU: Suport for PVH guests

In reply to Boris Ostrovsky

Add support for PVH guests. Most of operations are performed as in an HVM guest. However, interrupt management is done in PV-like manner.

---
 xen/arch/x86/hvm/hvm.c     |  3 ++-
 xen/arch/x86/hvm/vmx/vmx.c |  4 +++-
 xen/arch/x86/hvm/vpmu.c    | 22 ++++++++++++++++++----
 3 files changed, 23 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -1
index 69f7e74..1e50c35 100644
@@ -3451,7 +3451,8 @@ static hvm_hypercall_t *const pvh_hypercall64_table[NR_hypercalls] = {
     [ __HYPERVISOR_physdev_op ]      = (hvm_hypercall_t *)hvm_physdev_op,
     HYPERCALL(hvm_op),
     HYPERCALL(sysctl),
-    HYPERCALL(domctl)
+    HYPERCALL(domctl),
+    HYPERCALL(xenpmu_op)
 };
 
 int hvm_do_hypercall(struct cpu_user_regs *regs)
xen/arch/x86/hvm/vmx/vmx.c +3 -1
index dfff628..59b8ef1 100644
@@ -112,7 +112,9 @@ static int vmx_vcpu_initialise(struct vcpu *v)
         return rc;
     }
 
-    vpmu_initialise(v);
+    /* PVH will initialize VPMU using PV path */
+    if ( is_hvm_domain(v->domain) )
+        vpmu_initialise(v);
 
     vmx_install_vlapic_mapping(v);
 
xen/arch/x86/hvm/vpmu.c +18 -4
index e76b538..f736de0 100644
@@ -37,6 +37,7 @@
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
 #include <asm/nmi.h>
+#include <asm/p2m.h>
 #include <public/xenpmu.h>
 
 /*
@@ -194,13 +195,17 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
 
     if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
     {
-        /* PV guest or dom0 is doing system profiling */
+        /* PV(H) guest or dom0 is doing system profiling */
         struct cpu_user_regs *gregs;
         int err;
 
         if ( v->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED )
             return 1;
 
+        if ( is_pvh_domain(current->domain) && !(vpmu_mode & XENPMU_MODE_PRIV) )
+            if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) )
+                return 0;
+
         /* 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);
@@ -237,7 +242,7 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
             else if ( !is_control_domain(current->domain) &&
                       !is_idle_vcpu(current) )
             {
-                /* PV guest */
+                /* PV(H) guest */
                 gregs = guest_cpu_user_regs();
                 memcpy(&v->arch.vpmu.xenpmu_data->pmu.r.regs,
                        gregs, sizeof(struct cpu_user_regs));
@@ -247,7 +252,15 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
                        regs, sizeof(struct cpu_user_regs));
 
             gregs = &v->arch.vpmu.xenpmu_data->pmu.r.regs;
-            gregs->cs = cs;
+            if ( !is_pvh_domain(current->domain) )
+                gregs->cs = cs;
+            else if ( !(vpmu_apic_vector & APIC_DM_NMI) )
+            {
+                struct segment_register seg_cs;
+
+                hvm_get_segment_register(current, x86_seg_cs, &seg_cs);
+                gregs->cs = seg_cs.attr.fields.dpl;
+            }
         }
         else
         {
@@ -271,7 +284,8 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
         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;
+        if ( !is_pvh_domain(current->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
+            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;
 
Signature · 1 line
1.8.1.4
#45 lore raw

Re: [PATCH v4 16/17] x86/VPMU: Suport for PVH guests

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

Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +3 -0
+        if ( is_pvh_domain(current->domain) && !(vpmu_mode & XENPMU_MODE_PRIV) )
+            if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) )
+                return 0;

Please fold chained if()s like this one.

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

Quoted 6 lines
xen/arch/x86/hvm/vpmu.c +1 -1
@@ -237,7 +242,7 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
             else if ( !is_control_domain(current->domain) &&
                       !is_idle_vcpu(current) )
             {
-                /* PV guest */
+                /* PV(H) guest */

I would have expected PVH guests to use the HVM paths here, not the PV ones. Can you clarify why you do it the other way around?

Jan

#46 lore raw

Re: [PATCH v4 16/17] x86/VPMU: Suport for PVH guests

Quoted 6 lines · Jan Beulich

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

Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +3 -0
+        if ( is_pvh_domain(current->domain) && !(vpmu_mode & XENPMU_MODE_PRIV) )
+            if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) )
+                return 0;

Please fold chained if()s like this one.

Quoted 8 lines

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

Quoted 6 lines
xen/arch/x86/hvm/vpmu.c +1 -1
@@ -237,7 +242,7 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
              else if ( !is_control_domain(current->domain) &&
                        !is_idle_vcpu(current) )
              {
-                /* PV guest */
+                /* PV(H) guest */

I would have expected PVH guests to use the HVM paths here, not the PV ones. Can you clarify why you do it the other way around?

I could go either way but because PVH uses event channels for interrupts I went with PV path. To use HVM route I'd need to send NMIs to the guest and that is currently not quite working.

-boris

#47 lore raw

PATCH v4 17/17 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               | 671 ------------------------
 xen/arch/x86/oprofile/op_model_ppro.c |   2 +-
 xen/arch/x86/traps.c                  |   2 +-
 xen/arch/x86/vpmu.c                   | 671 ++++++++++++++++++++++++
 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        |  98 ----
 xen/include/asm-x86/vpmu.h            |  98 ++++
 16 files changed, 2209 insertions(+), 2211 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 -671

671 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 1854230..11f6821 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 +671 -0

671 lines not on this page · raw message

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

178 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 -98

98 lines not on this page · raw message

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

98 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.