Skip to contentSkip to conversation

x86/PMU: Xen PMU PV(H) support

PATCH Superseded Boris Ostrovsky 5 participants 58 messages Jun 6, 2014 – Jun 17, 2014

v7 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

v7 cover x86/PMU: Xen PMU PV(H) support

Here is the seventh version of PV(H) PMU patches.

Changes in v7:

* When reading hypervisor symbols make the caller pass buffer length (as opposed to having this length be part of the API). Make the hypervisor buffer static, make xensyms_read() return zero-length string on end-of-symbols. Make 'type' field of xenpf_symdata a char, drop compat_pf_symdata definition.
* Spread PVH support across patches as opposed to lumping it into a separate patch
* Rename vpmu_is_set_all() to vpmu_are_all_set()
* Split VPMU cleanup patch in two
* Use memmove when copying VMX guest and host MSRs
* Make padding of xen_arch_pmu's context union a constand that does not depend on arch context size.
* Set interface version to 0.1
* Check pointer validity in pvpmu_init/destroy()
* Fixed crash in core2_vpmu_dump()
* Fixed crash in vmx_add_msr()
* Break handling of Intel and AMD MSRs in traps.c into separate cases
* Pass full CS selector to guests
* Add lock in pvpmu init code to prevent potential race

Changes in v6:

* Two new patches:
o Merge VMX MSR add/remove routines in vmcs.c (patch 5)
o Merge VPMU read/write MSR routines in vpmu.c (patch 14)
* Check for pending NMI softirq after saving VPMU context to prevent a newly-scheduled guest from overwriting sampled_vcpu written by de-scheduled VPCU.
* Keep track of enabled counters on Intel. This was removed in earlier patches and was a mistake. As result of this change struct vpmu will have a pointer to private context data (i.e. data that is not exposed to a PV(H) guest). Use this private pointer on SVM as well for storing MSR bitmap status (it was unnecessarily exposed to PV guests earlier).
Dropped Reviewed-by: and Tested-by: tags from patch 4 since it needs to be reviewed agan (core2_vpmu_do_wrmsr() routine, mostly)
* Replaced references to dom0 with hardware_domain (and is_control_domain with is_hardware_domain for consistency)
* Prevent non-privileged domains from reading PMU MSRs in VPMU_PRIV_MODE
* Reverted unnecessary changes in vpmu_initialise()'s switch statement
* Fixed comment in vpmu_do_interrupt

Changes in v5:

* Dropped patch number 2 ("Stop AMD counters when called from vpmu_save_force()") as no longer needed
* Added patch number 2 that marks context as loaded before PMU registers are loaded. This prevents situation where a PMU interrupt may occur while context is still viewed as not loaded. (This is really a bug fix for exsiting VPMU code)
* Renamed xenpmu.h files to pmu.h
* More careful use of is_pv_domain(), is_hvm_domain(, is_pvh_domain and has_hvm_container_domain(). Also explicitly disabled support for PVH until patch 16 to make distinction between usage of the above macros more clear.
* Added support for disabling VPMU support during runtime.
* Disable VPMUs for non-privileged domains when switching to privileged profiling mode
* Added ARM stub for xen_arch_pmu_t
* Separated vpmu_mode from vpmu_features
* Moved CS register query to make sure we use appropriate query mechanism for various guest types.
* LVTPC is now set from value in shared area, not copied from dom0
* Various code and comments cleanup as suggested by Jan.

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

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

Boris Ostrovsky (19):
common/symbols: Export hypervisor symbols to privileged guest
VPMU: Mark context LOADED before registers are loaded x86/VPMU: Set MSR bitmaps only for HVM/PVH guests x86/VPMU: Make vpmu marcos a bit more efficient intel/VPMU: Clean up Intel VPMU code
vmx: Merge MSR management routines
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: Merge vpmu_rdmsr and vpmu_wrmsr
x86/VPMU: Add privileged PMU mode
x86/VPMU: Save VPMU state for PV guests during context switch x86/VPMU: NMI-based VPMU support
x86/VPMU: Move VPMU files up from hvm/ directory

 xen/arch/x86/Makefile                    |   1 +
 xen/arch/x86/domain.c                    |  15 +-
 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/svm.c               |  17 +-
 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              | 113 ++--
 xen/arch/x86/hvm/vmx/vmx.c               |  24 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 935 ------------------------------
 xen/arch/x86/hvm/vpmu.c                  | 266 ---------
 xen/arch/x86/oprofile/op_model_ppro.c    |   8 +-
 xen/arch/x86/platform_hypercall.c        |  21 +
 xen/arch/x86/traps.c                     |  56 +-
 xen/arch/x86/vpmu.c                      | 750 ++++++++++++++++++++++++
 xen/arch/x86/vpmu_amd.c                  | 512 +++++++++++++++++
 xen/arch/x86/vpmu_intel.c                | 954 +++++++++++++++++++++++++++++++
 xen/arch/x86/x86_64/compat/entry.S       |   4 +
 xen/arch/x86/x86_64/entry.S              |   4 +
 xen/common/event_channel.c               |   1 +
 xen/common/symbols.c                     |  54 ++
 xen/include/asm-x86/domain.h             |   2 +
 xen/include/asm-x86/hvm/vcpu.h           |   3 -
 xen/include/asm-x86/hvm/vmx/vmcs.h       |  10 +-
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  51 --
 xen/include/asm-x86/hvm/vpmu.h           | 104 ----
 xen/include/asm-x86/vpmu.h               | 102 ++++
 xen/include/public/arch-arm.h            |   3 +
 xen/include/public/arch-x86/pmu.h        |  58 ++
 xen/include/public/platform.h            |  19 +
 xen/include/public/pmu.h                 |  99 ++++
 xen/include/public/xen.h                 |   2 +
 xen/include/xen/hypercall.h              |   4 +
 xen/include/xen/softirq.h                |   1 +
 xen/include/xen/symbols.h                |   3 +
 xen/include/xlat.lst                     |   1 +
 38 files changed, 2779 insertions(+), 1923 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/pmu.h
 create mode 100644 xen/include/public/pmu.h
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v7 1/19 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 | 21 +++++++++++++++
 xen/common/symbols.c              | 54 +++++++++++++++++++++++++++++++++++++++
 xen/include/public/platform.h     | 19 ++++++++++++++
 xen/include/xen/symbols.h         |  3 +++
 xen/include/xlat.lst              |  1 +
 5 files changed, 98 insertions(+)
xen/arch/x86/platform_hypercall.c +21 -0
index 2162811..2c602e7 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,26 @@ ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
     }
     break;
 
+    case XENPF_get_symbol:
+    {
+        static char name[KSYM_NAME_LEN + 1];
+        XEN_GUEST_HANDLE(char) nameh;
+        uint32_t namelen;
+
+        guest_from_compat_handle(nameh, op->u.symdata.name);
+
+        ret = xensyms_read(&op->u.symdata.symnum, &op->u.symdata.type,
+                           &op->u.symdata.address, name);
+
+        namelen = min((uint32_t)strlen(name), op->u.symdata.namelen - 1);
+        name[namelen] = '\0'; /* if strlen(name) >= op->u.symdata.namelen */
+        if ( !ret && copy_to_guest(nameh, name, namelen) )
+            ret = -EFAULT;
+        if ( !ret && __copy_field_to_guest(u_xenpf_op, op, u.symdata) )
+            ret = -EFAULT;
+    }
+    break;
+
     default:
         ret = -ENOSYS;
         break;
xen/common/symbols.c +54 -0

63 lines not on this page · raw message

xen/include/public/platform.h +19 -0
index 053b9fa..afe301c 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
+struct xenpf_symdata {
+    /* IN variables */
+    uint32_t namelen; /* size of 'name' buffer */
+
+    /* IN/OUT variables */
+    uint32_t symnum;  /* IN:  Symbol to read                            */
+                      /* OUT: Next available symbol. If same as IN then */
+                      /*      we reached the end                        */
+
+    /* OUT variables */
+    char type;
+    uint64_t address;
+    XEN_GUEST_HANDLE(char) name;
+};
+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 +3 -0
index 87cd77d..1fa0537 100644
@@ -11,4 +11,7 @@ const char *symbols_lookup(unsigned long addr,
                            unsigned long *offset,
                            char *namebuf);
 
+int xensyms_read(uint32_t *symnum, char *type,
+                 uint64_t *address, char *name);
+
 #endif /*_XEN_SYMBOLS_H*/
xen/include/xlat.lst +1 -0
index 9a35dd7..c8fafef 100644
@@ -86,6 +86,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 v7 01/19] common/symbols: Export hypervisor symbols to privileged guest

Quoted 27 lines · Boris Ostrovsky

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

---
 xen/arch/x86/platform_hypercall.c | 21 +++++++++++++++
 xen/common/symbols.c              | 54 +++++++++++++++++++++++++++++++++++++++
 xen/include/public/platform.h     | 19 ++++++++++++++
 xen/include/xen/symbols.h         |  3 +++
 xen/include/xlat.lst              |  1 +
 5 files changed, 98 insertions(+)
xen/arch/x86/platform_hypercall.c +1 -0
index 2162811..2c602e7 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>

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

Quoted 6 lines
xen/arch/x86/platform_hypercall.c +3 -0
@@ -601,6 +602,26 @@ ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
     }
     break;
 
+    case XENPF_get_symbol:
+    {
+        static char name[KSYM_NAME_LEN + 1];

There needs to be some concurrency protection surrounding the use of this static. The symbol mutext in xensyms_read() is not sufficient.

As it currently stands, two vcpus issuing this hypercall at the time will collide and both will get junk back.

Quoted 12 lines
xen/include/public/platform.h +4 -0
index 053b9fa..afe301c 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
+struct xenpf_symdata {
+    /* IN variables */
+    uint32_t namelen; /* size of 'name' buffer */

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

Quoted 10 lines
xen/include/public/platform.h +10 -0
+
+    /* IN/OUT variables */
+    uint32_t symnum;  /* IN:  Symbol to read                            */
+                      /* OUT: Next available symbol. If same as IN then */
+                      /*      we reached the end                        */
+
+    /* OUT variables */
+    char type;
+    uint64_t address;
+    XEN_GUEST_HANDLE(char) name;

Please can we avoid introducing non-explicitly-width'd types into the public ABI.

If you swap name and type, use XEN_GUEST_HANDLE_64() and promote type to a uint32, it should be 32/64 bit safe and not need translating.

~Andrew

#3 lore raw

Re: [PATCH v7 01/19] common/symbols: Export hypervisor symbols to privileged guest

Quoted 31 lines · Andrew Cooper
Quoted 30 lines · Boris Ostrovsky

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

---
  xen/arch/x86/platform_hypercall.c | 21 +++++++++++++++
  xen/common/symbols.c              | 54 +++++++++++++++++++++++++++++++++++++++
  xen/include/public/platform.h     | 19 ++++++++++++++
  xen/include/xen/symbols.h         |  3 +++
  xen/include/xlat.lst              |  1 +
  5 files changed, 98 insertions(+)
xen/arch/x86/platform_hypercall.c +1 -0
index 2162811..2c602e7 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,26 @@ ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
      }
      break;
 
Quoted 8 lines

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

Quoted 3 lines
xen/arch/x86/platform_hypercall.c +3 -0
+    case XENPF_get_symbol:
+    {
+        static char name[KSYM_NAME_LEN + 1];

There needs to be some concurrency protection surrounding the use of this static. The symbol mutext in xensyms_read() is not sufficient.

As it currently stands, two vcpus issuing this hypercall at the time will collide and both will get junk back.

We should be protected by xenpf_lock here.

Quoted 26 lines
Quoted 13 lines
xen/include/public/platform.h +4 -0
index 053b9fa..afe301c 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
+struct xenpf_symdata {
+    /* IN variables */
+    uint32_t namelen; /* size of 'name' buffer */

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

Quoted 10 lines
xen/include/public/platform.h +10 -0
+
+    /* IN/OUT variables */
+    uint32_t symnum;  /* IN:  Symbol to read                            */
+                      /* OUT: Next available symbol. If same as IN then */
+                      /*      we reached the end                        */
+
+    /* OUT variables */
+    char type;
+    uint64_t address;
+    XEN_GUEST_HANDLE(char) name;

Please can we avoid introducing non-explicitly-width'd types into the public ABI.

Quoted 2 lines

If you swap name and type, use XEN_GUEST_HANDLE_64() and promote type to a uint32, it should be 32/64 bit safe and not need translating.

I thought we are going via compat_platform_op for 32-bit guests and thus pass pack(4)'d structures so translating is not needed. Is that not true?

(I did test this on 32-bit dom0 and it worked fine, although that alone is probably not a good argument).

-boris

#4 lore raw

Re: [PATCH v7 01/19] common/symbols: Export hypervisor symbols to privileged guest

Quoted 41 lines · Boris Ostrovsky
Quoted 40 lines · Andrew Cooper
Quoted 30 lines · Boris Ostrovsky

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

---
  xen/arch/x86/platform_hypercall.c | 21 +++++++++++++++
  xen/common/symbols.c              | 54
+++++++++++++++++++++++++++++++++++++++
  xen/include/public/platform.h     | 19 ++++++++++++++
  xen/include/xen/symbols.h         |  3 +++
  xen/include/xlat.lst              |  1 +
  5 files changed, 98 insertions(+)
xen/arch/x86/platform_hypercall.c +1 -0
index 2162811..2c602e7 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>

xen/arch/x86/platform_hypercall.c ret_t · line 607

Quoted 6 lines
xen/arch/x86/platform_hypercall.c +2 -0
@@ -601,6 +602,26 @@ ret_t
 do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
      }
      break;
  +    case XENPF_get_symbol:
+    {
+        static char name[KSYM_NAME_LEN + 1];

There needs to be some concurrency protection surrounding the use of this static. The symbol mutext in xensyms_read() is not sufficient.

Quoted 5 lines
Quoted 2 lines

As it currently stands, two vcpus issuing this hypercall at the time will collide and both will get junk back.

We should be protected by xenpf_lock here.

So we are. Perhaps worth leaving a small comment indicating as such?

Quoted 34 lines
Quoted 31 lines
Quoted 23 lines
xen/include/public/platform.h +6 -0
index 053b9fa..afe301c 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
+struct xenpf_symdata {
+    /* IN variables */
+    uint32_t namelen; /* size of 'name' buffer */
+
+    /* IN/OUT variables */
+    uint32_t symnum;  /* IN:  Symbol to

read */
+ /* OUT: Next available symbol. If same as IN
then */
+ /* we reached the
end */

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

Quoted 5 lines
xen/include/public/platform.h +5 -0
+
+    /* OUT variables */
+    char type;
+    uint64_t address;
+    XEN_GUEST_HANDLE(char) name;

Please can we avoid introducing non-explicitly-width'd types into the public ABI.

Quoted 2 lines

If you swap name and type, use XEN_GUEST_HANDLE_64() and promote type to a uint32, it should be 32/64 bit safe and not need translating.

Quoted 8 lines

I thought we are going via compat_platform_op for 32-bit guests and thus pass pack(4)'d structures so translating is not needed. Is that not true?

(I did test this on 32-bit dom0 and it worked fine, although that alone is probably not a good argument).

-boris

Erm, pass. Each time I try to get my head around the compat interface, a new supprise pops up. platform_op appears to be different to other hypercall handlers I am more used to.

If it works, then probably best not to play with it.

~Andrew

#5 lore raw

Re: [PATCH v7 01/19] common/symbols: Export hypervisor symbols to privileged guest

In reply to Boris Ostrovsky

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

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:39, <[email protected]> wrote:
xen/include/public/platform.h +4 -0
@@ -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
+struct xenpf_symdata {
+    /* IN variables */
+    uint32_t namelen; /* size of 'name' buffer */

I think if you want this to be an IN it should be named "namesize" or "namesz"; when being "namelen" one wold naturally expect it to be an IN/OUT (which I think I'd prefer).

Jan

#6 lore raw

PATCH v7 2/19 VPMU: Mark context LOADED before registers are loaded

In reply to Boris Ostrovsky

Because a PMU interrupt may be generated as soon as PMU registers are loaded (or, more precisely, as soon as HW PMU is "armed") we don't want to delay marking context as LOADED until after registers are loaded. Otherwise during interrupt handling VPMU_CONTEXT_LOADED may not be set and this could be confusing.

(Technically, only SVM needs this change right now since VMX will "arm" PMU later, during VMRUN when global control register is loaded from VMCS. However, both AMD and Intel code will require this patch when we introduce PV VPMU).

---
 xen/arch/x86/hvm/svm/vpmu.c       | 2 ++
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 2 ++
 xen/arch/x86/hvm/vpmu.c           | 3 +--
 3 files changed, 5 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +2 -0
index 66a3815..3ac7d53 100644
@@ -203,6 +203,8 @@ static void amd_vpmu_load(struct vcpu *v)
         return;
     }
 
+    vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
+
     context_load(v);
 }
 
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -0
index 3129ebd..ccd14d9 100644
@@ -369,6 +369,8 @@ static void core2_vpmu_load(struct vcpu *v)
     if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
         return;
 
+    vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
+
     __core2_vpmu_load(v);
 }
 
xen/arch/x86/hvm/vpmu.c +1 -2
index 21fbaba..63765fa 100644
@@ -211,10 +211,9 @@ void vpmu_load(struct vcpu *v)
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_load )
     {
         apic_write_around(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+        /* Arch code needs to set VPMU_CONTEXT_LOADED */
         vpmu->arch_vpmu_ops->arch_vpmu_load(v);
     }
-
-    vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
 }
 
 void vpmu_initialise(struct vcpu *v)
Signature · 1 line
1.8.1.4
Reviewed-by on v7 2/19
#7 lore raw

Re: [PATCH v7 02/19] VPMU: Mark context LOADED before registers are loaded

Quoted 5 lines · Boris Ostrovsky

Because a PMU interrupt may be generated as soon as PMU registers are loaded (or, more precisely, as soon as HW PMU is "armed") we don't want to delay marking context as LOADED until after registers are loaded. Otherwise during interrupt handling VPMU_CONTEXT_LOADED may not be set and this could be confusing.

Quoted 8 lines

(Technically, only SVM needs this change right now since VMX will "arm" PMU later, during VMRUN when global control register is loaded from VMCS. However, both AMD and Intel code will require this patch when we introduce PV VPMU).

Quoted 48 lines
---
 xen/arch/x86/hvm/svm/vpmu.c       | 2 ++
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 2 ++
 xen/arch/x86/hvm/vpmu.c           | 3 +--
 3 files changed, 5 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +2 -0
index 66a3815..3ac7d53 100644
@@ -203,6 +203,8 @@ static void amd_vpmu_load(struct vcpu *v)
         return;
     }
 
+    vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
+
     context_load(v);
 }
 
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -0
index 3129ebd..ccd14d9 100644
@@ -369,6 +369,8 @@ static void core2_vpmu_load(struct vcpu *v)
     if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
         return;
 
+    vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
+
     __core2_vpmu_load(v);
 }
 
xen/arch/x86/hvm/vpmu.c +1 -2
index 21fbaba..63765fa 100644
@@ -211,10 +211,9 @@ void vpmu_load(struct vcpu *v)
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_load )
     {
         apic_write_around(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+        /* Arch code needs to set VPMU_CONTEXT_LOADED */
         vpmu->arch_vpmu_ops->arch_vpmu_load(v);
     }
-
-    vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
 }
 
 void vpmu_initialise(struct vcpu *v)
#8 lore raw

Re: [PATCH v7 02/19] VPMU: Mark context LOADED before registers are loaded

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:59, <[email protected]> wrote:
Quoted 9 lines · Boris Ostrovsky

Because a PMU interrupt may be generated as soon as PMU registers are loaded (or, more precisely, as soon as HW PMU is "armed") we don't want to delay marking context as LOADED until after registers are loaded. Otherwise during interrupt handling VPMU_CONTEXT_LOADED may not be set and this could be confusing.

(Technically, only SVM needs this change right now since VMX will "arm" PMU later, during VMRUN when global control register is loaded from VMCS. However, both AMD and Intel code will require this patch when we introduce PV VPMU).

Quoted 6 lines
Quoted 4 lines

With this not having seen other comments, does it make sense to apply this on its own, while you address comments you got on subsequent patches?

Thanks, Jan

#9 lore raw

Re: [PATCH v7 02/19] VPMU: Mark context LOADED before registers are loaded

Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:59, <[email protected]> wrote:
Quoted 9 lines · Boris Ostrovsky

Because a PMU interrupt may be generated as soon as PMU registers are loaded (or, more precisely, as soon as HW PMU is "armed") we don't want to delay marking context as LOADED until after registers are loaded. Otherwise during interrupt handling VPMU_CONTEXT_LOADED may not be set and this could be confusing.

(Technically, only SVM needs this change right now since VMX will "arm" PMU later, during VMRUN when global control register is loaded from VMCS. However, both AMD and Intel code will require this patch when we introduce PV VPMU).

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

With this not having seen other comments, does it make sense to apply this on its own, while you address comments you got on subsequent patches?

Yes, this can go in, especially given that it's the first patch affecting VPMU.

Thanks.
-boris

#10 lore raw

PATCH v7 3/19 x86/VPMU: Set MSR bitmaps only for HVM/PVH guests

In reply to Boris Ostrovsky

In preparation for making VPMU code shared with PV make sure that we we update MSR bitmaps only for PV guests

---
 xen/arch/x86/hvm/svm/vpmu.c       | 21 +++++++++++++--------
 xen/arch/x86/hvm/vmx/vpmu_core2.c |  8 +++++---
 2 files changed, 18 insertions(+), 11 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +13 -8
index 3ac7d53..f76c81e 100644
@@ -244,7 +244,8 @@ static int amd_vpmu_save(struct vcpu *v)
 
     context_save(v);
 
-    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) && ctx->msr_bitmap_set )
+    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) &&
+         has_hvm_container_domain(v->domain) && ctx->msr_bitmap_set )
         amd_vpmu_unset_msr_bitmap(v);
 
     return 1;
@@ -284,8 +285,9 @@ 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) &&
-        !(is_guest_mode(msr_content)) )
+    if ( has_hvm_container_domain(v->domain) &&
+         (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
+         !(is_guest_mode(msr_content)) )
     {
         set_guest_mode(msr_content);
     }
@@ -300,8 +302,9 @@ 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 )
-            amd_vpmu_set_msr_bitmap(v);
+        if ( has_hvm_container_domain(v->domain) &&
+             !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+             amd_vpmu_set_msr_bitmap(v);
     }
 
     /* stop saving & restore if guest stops first counter */
@@ -311,8 +314,9 @@ 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 )
-            amd_vpmu_unset_msr_bitmap(v);
+        if ( has_hvm_container_domain(v->domain) &&
+             ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+             amd_vpmu_unset_msr_bitmap(v);
         release_pmu_ownship(PMU_OWNER_HVM);
     }
 
@@ -403,7 +407,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 ( has_hvm_container_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 +5 -3
index ccd14d9..f5f249f 100644
@@ -335,7 +335,8 @@ static int core2_vpmu_save(struct vcpu *v)
     __core2_vpmu_save(v);
 
     /* Unset PMU MSR bitmap to trap lazy load. */
-    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) && cpu_has_vmx_msr_bitmap )
+    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) &&
+         has_hvm_container_domain(v->domain) && cpu_has_vmx_msr_bitmap)
         core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
 
     return 1;
@@ -448,7 +449,8 @@ 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 ( has_hvm_container_domain(current->domain) &&
+             cpu_has_vmx_msr_bitmap )
             core2_vpmu_set_msr_bitmap(current->arch.hvm_vmx.msr_bitmap);
     }
     return 1;
@@ -815,7 +817,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 ( has_hvm_container_domain(v->domain) && cpu_has_vmx_msr_bitmap )
         core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
     release_pmu_ownship(PMU_OWNER_HVM);
     vpmu_reset(vpmu, VPMU_CONTEXT_ALLOCATED);
Signature · 1 line
1.8.1.4
#11 lore raw

Re: [PATCH v7 03/19] x86/VPMU: Set MSR bitmaps only for HVM/PVH guests

Quoted 2 lines · Boris Ostrovsky

In preparation for making VPMU code shared with PV make sure that we we update MSR bitmaps only for PV guests

Don't you mean "only for HVM guests" ?

~Andrew

#12 lore raw

Re: [PATCH v7 03/19] x86/VPMU: Set MSR bitmaps only for HVM/PVH guests

Quoted 4 lines · Andrew Cooper
Quoted 2 lines · Boris Ostrovsky

In preparation for making VPMU code shared with PV make sure that we we update MSR bitmaps only for PV guests

Don't you mean "only for HVM guests" ?

Right. ("only for for HVM/PVH guests")

-boris

#13 lore raw

PATCH v7 4/19 x86/VPMU: Make vpmu marcos a bit more efficient

In reply to Boris Ostrovsky

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

---
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 5 +----
 xen/arch/x86/hvm/vpmu.c           | 3 +--
 xen/include/asm-x86/hvm/vpmu.h    | 9 +++++----
 3 files changed, 7 insertions(+), 10 deletions(-)
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -4
index f5f249f..bf3cd33 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_are_all_set(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED) )
         return 0;
 
     __core2_vpmu_save(v);
xen/arch/x86/hvm/vpmu.c +1 -2
index 63765fa..d456aa6 100644
@@ -143,8 +143,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_are_all_set(vpmu, VPMU_CONTEXT_ALLOCATED | VPMU_CONTEXT_LOADED) )
        return;
 
     vpmu->last_pcpu = pcpu;
xen/include/asm-x86/hvm/vpmu.h +5 -4
index 40f63fb..7cd7266 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_are_all_set(_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
#14 lore raw

Re: [PATCH v7 04/19] x86/VPMU: Make vpmu marcos a bit more efficient

Quoted 1 line · Boris Ostrovsky

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

Modify how? It appears to only be an introduction of "vcpu_are_all_set()".

Quoted 49 lines
---
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 5 +----
 xen/arch/x86/hvm/vpmu.c           | 3 +--
 xen/include/asm-x86/hvm/vpmu.h    | 9 +++++----
 3 files changed, 7 insertions(+), 10 deletions(-)
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -4
index f5f249f..bf3cd33 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_are_all_set(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED) )
         return 0;
 
     __core2_vpmu_save(v);
xen/arch/x86/hvm/vpmu.c +1 -2
index 63765fa..d456aa6 100644
@@ -143,8 +143,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_are_all_set(vpmu, VPMU_CONTEXT_ALLOCATED | VPMU_CONTEXT_LOADED) )
        return;
 
     vpmu->last_pcpu = pcpu;
xen/include/asm-x86/hvm/vpmu.h +0 -0
index 40f63fb..7cd7266 100644
@@ -81,10 +81,11 @@ struct vpmu_struct {
 #define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
 

xen/include/asm-x86/hvm/vpmu.h struct vpmu_struct { · line 88

Quoted 10 lines
xen/include/asm-x86/hvm/vpmu.h +5 -4
 
-#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_are_all_set(_vpmu, _x) (((_vpmu)->flags & (_x)) == (_x))
+#define vpmu_clear(_vpmu)           ((_vpmu)->flags = 0)

These macros' implicit types don't quite match their implementation.

set, reset and clear are implicitly void. (and frankly clear and reset are confusing given the other bitopt nomenclature, but I don't suggest changing them).

is_set and are_all_set are implicitly bool, but don't have !!'s

I realise I am straying vastly into personal preference here, but I feel these would be much nicer as static inlines with proper types, and const correctness for {is,are_all}_set().

~Andrew

Quoted 3 lines
xen/include/asm-x86/hvm/vpmu.h +0 -0
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content);
 int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content);
#15 lore raw

Re: [PATCH v7 04/19] x86/VPMU: Make vpmu marcos a bit more efficient

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

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

Modify how? It appears to only be an introduction of "vcpu_are_all_set()".

I meant "vpmu_is_set(foo) + vpmu_is_set(bar)" ==> vpmu_are_all_set(foo|bar).

So probably "Introduce macro that allows testing multiple bits at one".

Quoted 22 lines
xen/include/asm-x86/hvm/vpmu.h +5 -4
@@ -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_are_all_set(_vpmu, _x) (((_vpmu)->flags & (_x)) == (_x))
+#define vpmu_clear(_vpmu)           ((_vpmu)->flags = 0)

These macros' implicit types don't quite match their implementation.

set, reset and clear are implicitly void. (and frankly clear and reset are confusing given the other bitopt nomenclature, but I don't suggest changing them).

Quoted 6 lines

is_set and are_all_set are implicitly bool, but don't have !!'s

I realise I am straying vastly into personal preference here, but I feel these would be much nicer as static inlines with proper types, and const correctness for {is,are_all}_set().

I don't have a preference here. If there is no difference binary-wise (code size) between the two I can change these to inlines.

-boris

#16 lore raw

PATCH v7 5/19 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              |  51 +++++
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 337 ++++++++++++++-----------------
 xen/include/asm-x86/hvm/vmx/vmcs.h       |   2 +
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  19 --
 4 files changed, 205 insertions(+), 204 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +51 -0
index 7564895..50259d3 100644
@@ -1208,6 +1208,32 @@ 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;
+
+    --msr_count;
+    memmove(&msr_area[idx], &msr_area[idx + 1],
+            sizeof(struct vmx_msr_entry) * (msr_count - idx));
+    msr_area[msr_count].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;
@@ -1238,6 +1264,31 @@ 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;
+
+    --msr_count;
+    memmove(&msr_area[idx], &msr_area[idx + 1],
+            sizeof(struct vmx_msr_entry) * (msr_count - idx));
+    msr_area[msr_count].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 +152 -185

584 lines not on this page · raw message

xen/include/asm-x86/hvm/vmx/vmcs.h +2 -0
index 445b39f..50befe1 100644
@@ -480,7 +480,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
#17 lore raw

Re: [PATCH v7 05/19] intel/VPMU: Clean up Intel VPMU code

Quoted 52 lines · 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/vpmu_core2.c +3 -30
@@ -125,75 +143,42 @@ static void handle_pmc_quirk(u64 msr_content)
     }
 }
 
-static const u32 core2_fix_counters_msr[] = {
-    MSR_CORE_PERF_FIXED_CTR0,
-    MSR_CORE_PERF_FIXED_CTR1,
-    MSR_CORE_PERF_FIXED_CTR2
-};
-
 /*
- * MSR_CORE_PERF_FIXED_CTR_CTRL contains the configuration of all fixed
- * counters. 4 bits for every counter.
+ * Read the number of general counters via CPUID.EAX[0xa].EAX[8..15]
  */
-#define FIXED_CTR_CTRL_BITS 4
-#define FIXED_CTR_CTRL_MASK ((1 << FIXED_CTR_CTRL_BITS) - 1)
-
-/* The index into the core2_ctrls_msr[] of this MSR used in core2_vpmu_dump() */
-#define MSR_CORE_PERF_FIXED_CTR_CTRL_IDX 0
-
-/* Core 2 Non-architectual Performance Control MSRs. */
-static const u32 core2_ctrls_msr[] = {
-    MSR_CORE_PERF_FIXED_CTR_CTRL,
-    MSR_IA32_PEBS_ENABLE,
-    MSR_IA32_DS_AREA
-};
-
-struct pmumsr {
-    unsigned int num;
-    const u32 *msr;
-};
-
-static const struct pmumsr core2_fix_counters = {
-    VPMU_CORE2_NUM_FIXED,
-    core2_fix_counters_msr
-};
+static int core2_get_arch_pmc_count(void)
+{

xen/arch/x86/hvm/vmx/vpmu_core2.c static void handle_pmc_quirk(u64 msr_content) · line 155

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +4 -5
+    u32 eax;
 
-static const struct pmumsr core2_ctrls = {
-    VPMU_CORE2_NUM_CTRLS,
-    core2_ctrls_msr
-};
-static int arch_pmc_cnt;
+    eax = cpuid_eax(0xa);
+    return ( (eax & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT );
+}

This (and later) can be made much simpler. The style guidelines easily permit:

static int core2_get_arch_pmc_count(void)

{
    return (cpuid_eax(0xa) & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT;
}

Unrelated to this code itself, I wonder whether Xen should gain some mnemonics for cpuid leaves.

Quoted 19 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +4 -10
 /*
- * Read the number of general counters via CPUID.EAX[0xa].EAX[8..15]
+ * Read the number of fixed counters via CPUID.EDX[0xa].EDX[0..4]
  */
-static int core2_get_pmc_count(void)
+static int core2_get_fixed_pmc_count(void)
 {
-    u32 eax, ebx, ecx, edx;
-
-    if ( arch_pmc_cnt == 0 )
-    {
-        cpuid(0xa, &eax, &ebx, &ecx, &edx);
-        arch_pmc_cnt = (eax & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT;
-    }
+    u32 eax;
 
-    return arch_pmc_cnt;
+    eax = cpuid_eax(0xa);

xen/arch/x86/hvm/vmx/vpmu_core2.c static void handle_pmc_quirk(u64 msr_content) · line 171

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +3 -3
+    return ( (eax & PMU_FIXED_NR_MASK) >> PMU_FIXED_NR_SHIFT );
 }
 
 static u64 core2_calc_intial_glb_ctrl_msr(void)
 {
-    int arch_pmc_bits = (1 << core2_get_pmc_count()) - 1;
-    u64 fix_pmc_bits  = (1 << 3) - 1;
-    return ((fix_pmc_bits << 32) | arch_pmc_bits);
+    int arch_pmc_bits = (1 << arch_pmc_cnt) - 1;
+    u64 fix_pmc_bits  = (1 << fixed_pmc_cnt) - 1;

Newline after variable declarations.

xen/arch/x86/hvm/vmx/vpmu_core2.c static void handle_pmc_quirk(u64 msr_content) · line 172

Quoted 1 line
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -0
+    return ( (fix_pmc_bits << 32) | arch_pmc_bits );

Redundant brackets.

Quoted 28 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -5
 }
 
 /* edx bits 5-12: Bit width of fixed-function performance counters  */
 static int core2_get_bitwidth_fix_count(void)
 {
-    u32 eax, ebx, ecx, edx;
+    u32 edx;
 
-    cpuid(0xa, &eax, &ebx, &ecx, &edx);
-    return ((edx & PMU_FIXED_WIDTH_MASK) >> PMU_FIXED_WIDTH_SHIFT);
+    edx = cpuid_edx(0xa);
+    return ( (edx & PMU_FIXED_WIDTH_MASK) >> PMU_FIXED_WIDTH_SHIFT );
 }
 
 static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index)
@@ -201,9 +186,9 @@ static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index)
     int i;
     u32 msr_index_pmc;
 
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
     {
-        if ( core2_fix_counters.msr[i] == msr_index )
+        if ( msr_index == MSR_CORE_PERF_FIXED_CTR0 + i )
         {
             *type = MSR_TYPE_COUNTER;
             *index = i;

xen/arch/x86/hvm/vmx/vpmu_core2.c static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index) · line 199

Quoted 5 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -1
@@ -211,14 +196,12 @@ static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index)
         }
     }
 
-    for ( i = 0; i < core2_ctrls.num; i++ )
+    if ( (msr_index == MSR_CORE_PERF_FIXED_CTR_CTRL ) ||

Stray space before the closing bracket

Quoted 18 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +4 -6
+        (msr_index == MSR_IA32_DS_AREA) ||
+        (msr_index == MSR_IA32_PEBS_ENABLE) )
     {
-        if ( core2_ctrls.msr[i] == msr_index )
-        {
-            *type = MSR_TYPE_CTRL;
-            *index = i;
-            return 1;
-        }
+        *type = MSR_TYPE_CTRL;
+        return 1;
     }
 
     if ( (msr_index == MSR_CORE_PERF_GLOBAL_CTRL) ||
@@ -275,26 +258,28 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
     }
 
     /* Allow Read PMU Non-global Controls Directly. */

xen/arch/x86/hvm/vmx/vpmu_core2.c static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap) · line 266

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +6 -4
-    for ( i = 0; i < core2_ctrls.num; i++ )
-        clear_bit(msraddr_to_bitpos(core2_ctrls.msr[i]), msr_bitmap);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
-        clear_bit(msraddr_to_bitpos(MSR_P6_EVNTSEL0+i), msr_bitmap);
+    for ( i = 0; i < arch_pmc_cnt; i++ )
+         clear_bit(msraddr_to_bitpos(MSR_P6_EVNTSEL0 + i), msr_bitmap);
+
+    clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR_CTRL), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_PEBS_ENABLE), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_DS_AREA), msr_bitmap);

This code looks as if "clear_msr_in_map(MSR_$FOO, msr_bitmap)" would be a useful helper.

Quoted 18 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +10 -0
@@ -801,18 +759,27 @@ static int core2_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
         }
     }
 func_out:
+
+    arch_pmc_cnt = core2_get_arch_pmc_count();
+    fixed_pmc_cnt = core2_get_fixed_pmc_count();
+    if ( fixed_pmc_cnt > VPMU_CORE2_MAX_FIXED_PMCS )
+    {
+        fixed_pmc_cnt = VPMU_CORE2_MAX_FIXED_PMCS;
+        printk(XENLOG_G_WARNING "Limiting number of fixed counters to %d\n",
+               fixed_pmc_cnt);
+    }
     check_pmc_quirk();
+
     return 0;
 }
 

xen/arch/x86/hvm/vmx/vpmu_core2.c static int core2_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags) · line 783

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -2
 static void core2_vpmu_destroy(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    struct core2_vpmu_context *core2_vpmu_cxt = vpmu->context;
 
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
-    xfree(core2_vpmu_cxt->pmu_enable);
+
     xfree(vpmu->context);

Is it really right to bail if !VPMU_CONTEXT_ALLOCATED ? A stray vpmu_clear() would result in a memory leak.

i.e. can VPMU_CONTEXT_ALLOCATED be deemed from "vpmu->context != NULL" instead?

~Andrew

#18 lore raw

Re: [PATCH v7 05/19] intel/VPMU: Clean up Intel VPMU code

Quoted 16 lines · Andrew Cooper
xen/arch/x86/hvm/vmx/vpmu_core2.c +6 -5
+static int core2_get_arch_pmc_count(void)
+{
+    u32 eax;
 
-static const struct pmumsr core2_ctrls = {
-    VPMU_CORE2_NUM_CTRLS,
-    core2_ctrls_msr
-};
-static int arch_pmc_cnt;
+    eax = cpuid_eax(0xa);
+    return ( (eax & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT );
+}

This (and later) can be made much simpler. The style guidelines easily permit:

Quoted 7 lines

static int core2_get_arch_pmc_count(void)

{
     return (cpuid_eax(0xa) & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT;
}

Unrelated to this code itself, I wonder whether Xen should gain some mnemonics for cpuid leaves.

Not sure what you mean here.

Quoted 14 lines
  
  static void core2_vpmu_destroy(struct vcpu *v)
  {
      struct vpmu_struct *vpmu = vcpu_vpmu(v);
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -2
-    struct core2_vpmu_context *core2_vpmu_cxt = vpmu->context;
 
      if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
          return;
-    xfree(core2_vpmu_cxt->pmu_enable);
+
      xfree(vpmu->context);

Is it really right to bail if !VPMU_CONTEXT_ALLOCATED ? A stray vpmu_clear() would result in a memory leak.

Quoted 2 lines

i.e. can VPMU_CONTEXT_ALLOCATED be deemed from "vpmu->context != NULL" instead?

I think so.

But the flag is used throughout VPMU code to indicate availability of context so for consistency I think we should continue using it here. And I don't want to drop it as a flag completely since checking for it is a tiny bit faster than for NULL pointer: we often check multiple flags at once.

-boris

#19 lore raw

Re: [PATCH v7 05/19] intel/VPMU: Clean up Intel VPMU code

In reply to Andrew Cooper
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 20:34, <[email protected]> wrote:

xen/arch/x86/hvm/vmx/vpmu_core2.c static void handle_pmc_quirk(u64 msr_content) · line 155

Quoted 4 lines · Boris Ostrovsky
xen/arch/x86/hvm/vmx/vpmu_core2.c +3 -1
-static int arch_pmc_cnt;
+    eax = cpuid_eax(0xa);
+    return ( (eax & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT );
+}

This (and later) can be made much simpler. The style guidelines easily permit:

Quoted 4 lines

static int core2_get_arch_pmc_count(void)

{
    return (cpuid_eax(0xa) & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT;
}

The above can be further simplified using MASK_EXTR(), at once eliminating the need for *_SHIFT constants.

Quoted 2 lines

Unrelated to this code itself, I wonder whether Xen should gain some mnemonics for cpuid leaves.

We already have XSTATE_CPUID, so it would indeed seem desirable to name 0xa too.

Jan

#20 lore raw

PATCH v7 6/19 vmx: Merge MSR management routines

In reply to Boris Ostrovsky

vmx_add_host_load_msr()/vmx_rm_guest_msr() and vmx_add_guest_msr()/vmx_rm_guest_msr() share fair amount of code. Merge them to simplify code maintenance.

---
 xen/arch/x86/hvm/vmx/vmcs.c        | 156 +++++++++++++++++--------------------
 xen/arch/x86/hvm/vmx/vmx.c         |   4 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c  |   8 +-
 xen/include/asm-x86/hvm/vmx/vmcs.h |  10 ++-
 4 files changed, 85 insertions(+), 93 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +73 -83
index 50259d3..436e8f4 100644
@@ -1176,117 +1176,107 @@ int vmx_write_guest_msr(u32 msr, u64 val)
     return -ESRCH;
 }
 
-int vmx_add_guest_msr(u32 msr)
+int vmx_add_msr(u32 msr, u8 type)
 {
     struct vcpu *curr = current;
-    unsigned int i, msr_count = curr->arch.hvm_vmx.msr_count;
-    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.msr_area;
+    unsigned int idx, *msr_count;
+    struct vmx_msr_entry **msr_area;
 
-    if ( msr_area == NULL )
+    ASSERT( (type == VMX_GUEST_MSR) || (type == VMX_HOST_MSR) );
+
+    if ( type == VMX_GUEST_MSR )
     {
-        if ( (msr_area = alloc_xenheap_page()) == NULL )
-            return -ENOMEM;
-        curr->arch.hvm_vmx.msr_area = msr_area;
-        __vmwrite(VM_EXIT_MSR_STORE_ADDR, virt_to_maddr(msr_area));
-        __vmwrite(VM_ENTRY_MSR_LOAD_ADDR, virt_to_maddr(msr_area));
+        msr_count = &curr->arch.hvm_vmx.msr_count;
+        msr_area = &curr->arch.hvm_vmx.msr_area;
+    }
+    else
+    {
+        msr_count = &curr->arch.hvm_vmx.host_msr_count;
+        msr_area = &curr->arch.hvm_vmx.host_msr_area;
     }
 
-    for ( i = 0; i < msr_count; i++ )
-        if ( msr_area[i].index == msr )
-            return 0;
-
-    if ( msr_count == (PAGE_SIZE / sizeof(struct vmx_msr_entry)) )
-        return -ENOSPC;
-
-    msr_area[msr_count].index = msr;
-    msr_area[msr_count].mbz   = 0;
-    msr_area[msr_count].data  = 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);
-
-    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;
-
-    --msr_count;
-    memmove(&msr_area[idx], &msr_area[idx + 1],
-            sizeof(struct vmx_msr_entry) * (msr_count - idx));
-    msr_area[msr_count].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;
-    unsigned int i, 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 )
+    if ( *msr_area == NULL )
     {
-        if ( (msr_area = alloc_xenheap_page()) == NULL )
+        if ( (*msr_area = alloc_xenheap_page()) == NULL )
             return -ENOMEM;
-        curr->arch.hvm_vmx.host_msr_area = msr_area;
-        __vmwrite(VM_EXIT_MSR_LOAD_ADDR, virt_to_maddr(msr_area));
+
+        if ( type == VMX_GUEST_MSR )
+        {
+            __vmwrite(VM_EXIT_MSR_STORE_ADDR, virt_to_maddr(*msr_area));
+            __vmwrite(VM_ENTRY_MSR_LOAD_ADDR, virt_to_maddr(*msr_area));
+        }
+        else
+            __vmwrite(VM_EXIT_MSR_LOAD_ADDR, virt_to_maddr(*msr_area));
     }
 
-    for ( i = 0; i < msr_count; i++ )
-        if ( msr_area[i].index == msr )
+    for ( idx = 0; idx < *msr_count; idx++ )
+        if ( (*msr_area)[idx].index == msr )
             return 0;
 
-    if ( msr_count == (PAGE_SIZE / sizeof(struct vmx_msr_entry)) )
+    if ( *msr_count == (PAGE_SIZE / sizeof(struct vmx_msr_entry)) )
         return -ENOSPC;
 
-    msr_area[msr_count].index = msr;
-    msr_area[msr_count].mbz   = 0;
-    rdmsrl(msr, msr_area[msr_count].data);
-    curr->arch.hvm_vmx.host_msr_count = ++msr_count;
-    __vmwrite(VM_EXIT_MSR_LOAD_COUNT, msr_count);
+    (*msr_area)[*msr_count].index = msr;
+    (*msr_area)[*msr_count].mbz   = 0;
+    (*msr_count)++;
+    if ( type == VMX_GUEST_MSR )
+    {
+        (*msr_area)[*msr_count - 1].data  = 0;
+        __vmwrite(VM_EXIT_MSR_STORE_COUNT, *msr_count);
+        __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, *msr_count);
+    }
+    else
+    {
+        rdmsrl(msr, (*msr_area)[*msr_count - 1].data);
+        __vmwrite(VM_EXIT_MSR_LOAD_COUNT, *msr_count);
+    }
 
     return 0;
 }
 
-void vmx_rm_host_load_msr(u32 msr)
+void vmx_rm_msr(u32 msr, u8 type)
 {
     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;
+    unsigned int idx, *msr_count;
+    struct vmx_msr_entry **msr_area;
 
-    if ( msr_area == NULL )
+    ASSERT( (type == VMX_GUEST_MSR) || (type == VMX_HOST_MSR) );
+    
+    if ( type == VMX_GUEST_MSR )
+    {
+        msr_count = &curr->arch.hvm_vmx.msr_count;
+        msr_area = &curr->arch.hvm_vmx.msr_area;
+    }
+    else
+    {
+        msr_count = &curr->arch.hvm_vmx.host_msr_count;
+        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 )
+    for ( idx = 0; idx < *msr_count; idx++ )
+        if ( (*msr_area)[idx].index == msr )
             break;
 
-    if ( idx == msr_count )
+    if ( idx == *msr_count )
         return;
 
-    --msr_count;
-    memmove(&msr_area[idx], &msr_area[idx + 1],
-            sizeof(struct vmx_msr_entry) * (msr_count - idx));
-    msr_area[msr_count].index = 0;
+    --(*msr_count);
+    memmove(&(*msr_area)[idx], &(*msr_area)[idx + 1],
+            sizeof(struct vmx_msr_entry) * (*msr_count - idx));
+    msr_area[(*msr_count)]->index = 0;
 
-    curr->arch.hvm_vmx.host_msr_count = msr_count;
-    __vmwrite(VM_EXIT_MSR_LOAD_COUNT, msr_count);
+    if ( type == VMX_GUEST_MSR )
+    {
+        __vmwrite(VM_EXIT_MSR_STORE_COUNT, *msr_count);
+        __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, *msr_count);
+    }
+    else
+    {
+        __vmwrite(VM_EXIT_MSR_LOAD_COUNT, *msr_count);
+    }
 }
 
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector)
xen/arch/x86/hvm/vmx/vmx.c +2 -2
index d45fb7f..6f55c9e 100644
@@ -2254,12 +2254,12 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
 
             for ( ; (rc == 0) && lbr->count; lbr++ )
                 for ( i = 0; (rc == 0) && (i < lbr->count); i++ )
-                    if ( (rc = vmx_add_guest_msr(lbr->base + i)) == 0 )
+                    if ( (rc = vmx_add_msr(lbr->base + i, VMX_GUEST_MSR)) == 0 )
                         vmx_disable_intercept_for_msr(v, lbr->base + i, MSR_TYPE_R | MSR_TYPE_W);
         }
 
         if ( (rc < 0) ||
-             (vmx_add_host_load_msr(msr) < 0) )
+             (vmx_add_msr(msr, VMX_HOST_MSR) < 0) )
             hvm_inject_hw_exception(TRAP_machine_check, 0);
         else
         {
xen/arch/x86/hvm/vmx/vpmu_core2.c +4 -4
index 6df9f56..7c49f8a 100644
@@ -370,10 +370,10 @@ static int core2_vpmu_alloc_resource(struct vcpu *v)
         return 0;
 
     wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
-    if ( vmx_add_host_load_msr(MSR_CORE_PERF_GLOBAL_CTRL) )
+    if ( vmx_add_msr(MSR_CORE_PERF_GLOBAL_CTRL, VMX_HOST_MSR) )
         goto out_err;
 
-    if ( vmx_add_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL) )
+    if ( vmx_add_msr(MSR_CORE_PERF_GLOBAL_CTRL, VMX_GUEST_MSR) )
         goto out_err;
     vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL,
                  core2_calc_intial_glb_ctrl_msr());
@@ -390,8 +390,8 @@ static int core2_vpmu_alloc_resource(struct vcpu *v)
     return 1;
 
 out_err:
-    vmx_rm_host_load_msr(MSR_CORE_PERF_GLOBAL_CTRL);
-    vmx_rm_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL);
+    vmx_rm_msr(MSR_CORE_PERF_GLOBAL_CTRL, VMX_HOST_MSR);
+    vmx_rm_msr(MSR_CORE_PERF_GLOBAL_CTRL, VMX_GUEST_MSR);
     release_pmu_ownship(PMU_OWNER_HVM);
 
     printk("Failed to allocate VPMU resources for domain %u vcpu %u\n",
xen/include/asm-x86/hvm/vmx/vmcs.h +6 -4
index 50befe1..dd34b2c 100644
@@ -475,14 +475,16 @@ enum vmcs_field {
 
 #define MSR_TYPE_R 1
 #define MSR_TYPE_W 2
+
+#define VMX_GUEST_MSR 0
+#define VMX_HOST_MSR  1
+
 void vmx_disable_intercept_for_msr(struct vcpu *v, u32 msr, int type);
 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);
+int vmx_add_msr(u32 msr, u8 type);
+void vmx_rm_msr(u32 msr, u8 type);
 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);
Signature · 1 line
1.8.1.4
#21 lore raw

Re: [PATCH v7 06/19] vmx: Merge MSR management routines

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:

+ ASSERT( (type == VMX_GUEST_MSR) || (type == VMX_HOST_MSR) );

As hinted at by Andrew already, I think you need to do a global coding style cleanup pass through this series, making sure you have (possibly among other things) spaces/parentheses where they belong and, apart from ambiguous cases where it's a matter of taste, none where none should be. The example here has too many spaces - ASSERT, other than if/for/while, is not a keyword.

Jan

#22 lore raw

PATCH v7 7/19 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           |  8 ++++++++
 xen/include/asm-x86/hvm/vpmu.h    |  1 +
 5 files changed, 13 insertions(+), 22 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +0 -4
index f76c81e..5c0f5df 100644
@@ -299,8 +299,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 ( has_hvm_container_domain(v->domain) &&
              !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
@@ -311,8 +309,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 ( has_hvm_container_domain(v->domain) &&
              ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
xen/arch/x86/hvm/vlapic.c +4 -1
index cd7e872..f1b543c 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>
 
@@ -734,8 +735,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 7c49f8a..94cfd6d 100644
@@ -529,19 +529,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;
@@ -707,10 +694,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 +8 -0
index d456aa6..4ddac7d 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);
xen/include/asm-x86/hvm/vpmu.h +1 -0
index 7cd7266..fe181fc 100644
@@ -87,6 +87,7 @@ struct vpmu_struct {
 #define vpmu_are_all_set(_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
#23 lore raw

PATCH v7 8/19 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 94cfd6d..c7322f5 100644
@@ -165,13 +165,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)
 {
@@ -375,8 +368,7 @@ static int core2_vpmu_alloc_resource(struct vcpu *v)
 
     if ( vmx_add_msr(MSR_CORE_PERF_GLOBAL_CTRL, VMX_GUEST_MSR) )
         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
#24 lore raw

PATCH v7 9/19 x86/VPMU: Add public xenpmu.h

In reply to Boris Ostrovsky

Add pmu.h header files, 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              |  81 +++++++++++----------
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 117 +++++++++++++++++--------------
 xen/arch/x86/hvm/vpmu.c                  |   5 ++
 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           |  16 ++---
 xen/include/public/arch-arm.h            |   3 +
 xen/include/public/arch-x86/pmu.h        |  58 +++++++++++++++
 xen/include/public/pmu.h                 |  38 ++++++++++
 9 files changed, 224 insertions(+), 132 deletions(-)
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 create mode 100644 xen/include/public/arch-x86/pmu.h
 create mode 100644 xen/include/public/pmu.h
xen/arch/x86/hvm/svm/vpmu.c +44 -37
index 5c0f5df..b5ca577 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/pmu.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,12 +84,10 @@ 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;
-};
+/* Use private context as a flag for MSR bitmap */
+#define msr_bitmap_on(vpmu)    { (vpmu)->priv_context = (void *)-1L; }
+#define msr_bitmap_off(vpmu)   { (vpmu)->priv_context = NULL; }
+#define is_msr_bitmap_on(vpmu) ((vpmu)->priv_context != NULL)
 
 static inline int get_pmu_reg_type(u32 addr)
 {
@@ -142,7 +141,6 @@ 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;
 
     for ( i = 0; i < num_counters; i++ )
     {
@@ -150,14 +148,13 @@ static void amd_vpmu_set_msr_bitmap(struct vcpu *v)
         svm_intercept_msr(v, ctrls[i], MSR_INTERCEPT_WRITE);
     }
 
-    ctxt->msr_bitmap_set = 1;
+    msr_bitmap_on(vpmu);
 }
 
 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;
 
     for ( i = 0; i < num_counters; i++ )
     {
@@ -165,7 +162,7 @@ static void amd_vpmu_unset_msr_bitmap(struct vcpu *v)
         svm_intercept_msr(v, ctrls[i], MSR_INTERCEPT_RW);
     }
 
-    ctxt->msr_bitmap_set = 0;
+    msr_bitmap_off(vpmu);
 }
 
 static int amd_vpmu_do_interrupt(struct cpu_user_regs *regs)
@@ -177,19 +174,22 @@ 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;
+    uint64_t *ctrl_regs = vpmu_reg_pointer(ctxt, ctrls);
 
     vpmu_reset(vpmu, VPMU_FROZEN);
 
@@ -198,7 +198,7 @@ static void amd_vpmu_load(struct vcpu *v)
         unsigned int i;
 
         for ( i = 0; i < num_counters; i++ )
-            wrmsrl(ctrls[i], ctxt->ctrls[i]);
+            wrmsrl(ctrls[i], ctrl_regs[i]);
 
         return;
     }
@@ -212,17 +212,17 @@ 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;
     unsigned int i;
 
     /*
@@ -245,7 +245,7 @@ static int amd_vpmu_save(struct vcpu *v)
     context_save(v);
 
     if ( !vpmu_is_set(vpmu, VPMU_RUNNING) &&
-         has_hvm_container_domain(v->domain) && ctx->msr_bitmap_set )
+         has_hvm_container_domain(v->domain) && is_msr_bitmap_on(vpmu) )
         amd_vpmu_unset_msr_bitmap(v);
 
     return 1;
@@ -256,7 +256,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)) )
@@ -268,12 +270,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;
         }
     }
@@ -300,8 +302,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
             return 1;
         vpmu_set(vpmu, VPMU_RUNNING);
 
-        if ( has_hvm_container_domain(v->domain) &&
-             !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+        if ( has_hvm_container_domain(v->domain) && is_msr_bitmap_on(vpmu) )
              amd_vpmu_set_msr_bitmap(v);
     }
 
@@ -310,8 +311,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         (is_pmu_enabled(msr_content) == 0) && vpmu_is_set(vpmu, VPMU_RUNNING) )
     {
         vpmu_reset(vpmu, VPMU_RUNNING);
-        if ( has_hvm_container_domain(v->domain) &&
-             ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+        if ( has_hvm_container_domain(v->domain) && is_msr_bitmap_on(vpmu) )
              amd_vpmu_unset_msr_bitmap(v);
         release_pmu_ownship(PMU_OWNER_HVM);
     }
@@ -352,7 +352,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;
 
@@ -382,7 +382,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, "
@@ -391,7 +393,11 @@ 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->priv_context = NULL;
     vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
     return 0;
 }
@@ -403,8 +409,7 @@ static void amd_vpmu_destroy(struct vcpu *v)
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
 
-    if ( has_hvm_container_domain(v->domain) &&
-         ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+    if ( has_hvm_container_domain(v->domain) && is_msr_bitmap_on(vpmu) )
         amd_vpmu_unset_msr_bitmap(v);
 
     xfree(vpmu->context);
@@ -421,7 +426,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);
@@ -451,8 +458,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 +64 -53

286 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +5 -0
index 4ddac7d..56b48be 100644
@@ -31,6 +31,7 @@
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
+#include <public/pmu.h>
 
 /*
  * "vpmu" :     vpmu generally enabled
@@ -228,6 +229,10 @@ void vpmu_initialise(struct vcpu *v)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     uint8_t vendor = current_cpu_data.x86_vendor;
 
+    BUILD_BUG_ON((sizeof(struct xen_pmu_intel_ctxt) > XENPMU_CTXT_PAD_SZ) ||
+                 (sizeof(struct xen_pmu_amd_ctxt) > XENPMU_CTXT_PAD_SZ));
+
+
     if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         vpmu_destroy(v);
     vpmu_clear(vpmu);
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 +7 -9
index fe181fc..83b5461 100644
@@ -22,6 +22,8 @@
 #ifndef __ASM_X86_HVM_VPMU_H_
 #define __ASM_X86_HVM_VPMU_H_
 
+#include <public/pmu.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 {
@@ -64,7 +66,8 @@ struct vpmu_struct {
     u32 flags;
     u32 last_pcpu;
     u32 hw_lapic_lvtpc;
-    void *context;
+    void *context;      /* May be shared with PV guest */
+    void *priv_context; /* hypervisor-only */
     struct arch_vpmu_ops *arch_vpmu_ops;
 };
 
@@ -76,11 +79,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-arm.h +3 -0
index 7496556..e982b53 100644
@@ -388,6 +388,9 @@ typedef uint64_t xen_callback_t;
 
 #endif
 
+/* Stub definition of PMU structure */
+typedef struct xen_arch_pmu {} xen_arch_pmu_t;
+
 #endif /*  __XEN_PUBLIC_ARCH_ARM_H__ */
 
 /*
xen/include/public/arch-x86/pmu.h new file +58 -0

58 lines not on this page · raw message

xen/include/public/pmu.h new file +38 -0
new file mode 100644
index 0000000..d237c25
@@ -0,0 +1,38 @@
+#ifndef __XEN_PUBLIC_PMU_H__
+#define __XEN_PUBLIC_PMU_H__
+
+#include "xen.h"
+#if defined(__i386__) || defined(__x86_64__)
+#include "arch-x86/pmu.h"
+#elif defined (__arm__) || defined (__aarch64__)
+#include "arch-arm.h"
+#else
+#error "Unsupported architecture"
+#endif
+
+#define XENPMU_VER_MAJ    0
+#define XENPMU_VER_MIN    1
+
+
+/* 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_PMU_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
#25 lore raw

Re: [PATCH v7 09/19] x86/VPMU: Add public xenpmu.h

Quoted 27 lines · Boris Ostrovsky

Add pmu.h header files, 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              |  81 +++++++++++----------
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 117 +++++++++++++++++--------------
 xen/arch/x86/hvm/vpmu.c                  |   5 ++
 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           |  16 ++---
 xen/include/public/arch-arm.h            |   3 +
 xen/include/public/arch-x86/pmu.h        |  58 +++++++++++++++
 xen/include/public/pmu.h                 |  38 ++++++++++
 9 files changed, 224 insertions(+), 132 deletions(-)
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 create mode 100644 xen/include/public/arch-x86/pmu.h
 create mode 100644 xen/include/public/pmu.h

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

Quoted 7 lines
xen/arch/x86/hvm/svm/vpmu.c +3 -1
@@ -382,7 +382,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);

Two lots of counters? If so, then * 2 would be better.

Quoted 26 lines
xen/arch/x86/hvm/svm/vpmu.c +5 -2
     if ( !ctxt )
     {
         gdprintk(XENLOG_WARNING, "Insufficient memory for PMU, "
@@ -391,7 +393,11 @@ 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->priv_context = NULL;
     vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
     return 0;
 }
@@ -403,8 +409,7 @@ static void amd_vpmu_destroy(struct vcpu *v)
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
 
-    if ( has_hvm_container_domain(v->domain) &&
-         ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+    if ( has_hvm_container_domain(v->domain) && is_msr_bitmap_on(vpmu) )
         amd_vpmu_unset_msr_bitmap(v);
 
     xfree(vpmu->context);

xen/arch/x86/hvm/svm/vpmu.c static void amd_vpmu_destroy(struct vcpu *v) · line 431

Quoted 7 lines
xen/arch/x86/hvm/svm/vpmu.c +3 -1
@@ -421,7 +426,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);

const for both of these. The pointer arithmatic in vpmu_reg_pointer() hides what would otherwise should be a compiler error.

Quoted 13 lines
xen/arch/x86/hvm/vpmu.c +1 -0
index 4ddac7d..56b48be 100644
@@ -31,6 +31,7 @@
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
+#include <public/pmu.h>
 
 /*
  * "vpmu" :     vpmu generally enabled

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

Quoted 6 lines
xen/arch/x86/hvm/vpmu.c +3 -0
@@ -228,6 +229,10 @@ void vpmu_initialise(struct vcpu *v)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     uint8_t vendor = current_cpu_data.x86_vendor;
 
+    BUILD_BUG_ON((sizeof(struct xen_pmu_intel_ctxt) > XENPMU_CTXT_PAD_SZ) ||
+                 (sizeof(struct xen_pmu_amd_ctxt) > XENPMU_CTXT_PAD_SZ));
+

Can this be two BUILD_BUG_ON()s so the compiler tells you which structure is too big?

~Andrew

#26 lore raw

Re: [PATCH v7 09/19] x86/VPMU: Add public xenpmu.h

In reply to Boris Ostrovsky

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

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:
xen/arch/x86/hvm/vpmu.c +2 -0
@@ -228,6 +229,10 @@ void vpmu_initialise(struct vcpu *v)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     uint8_t vendor = current_cpu_data.x86_vendor;
 
+    BUILD_BUG_ON((sizeof(struct xen_pmu_intel_ctxt) > XENPMU_CTXT_PAD_SZ) ||
+                 (sizeof(struct xen_pmu_amd_ctxt) > XENPMU_CTXT_PAD_SZ));

This should be two BUILD_BUG_ON()s, so that if one triggers there's no ambiguity in which of the structures grew too large.

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

Quoted 3 lines
xen/include/public/arch-x86/pmu.h new file +3 -0
+struct xen_pmu_amd_ctxt {
+    uint32_t counters;       /* Offset to counter MSRs */
+    uint32_t ctrls;          /* Offset to control MSRs */

Offsets relative to what?

xen/include/public/arch-x86/pmu.h line 27

Quoted 10 lines
xen/include/public/arch-x86/pmu.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;
+    uint32_t fixed_counters;  /* Offset to fixed counter MSRs */
+    uint32_t arch_counters;   /* Offset to architectural counter MSRs */

Same question here.

Quoted 4 lines
xen/include/public/arch-x86/pmu.h new file +4 -0
+struct xen_arch_pmu {
+    union {
+        struct cpu_user_regs regs;
+        uint8_t pad[256];

xen/include/public/arch-x86/pmu.h line 43

Quoted 10 lines
xen/include/public/arch-x86/pmu.h new file +10 -0
+    } r;
+    union {
+        uint32_t lapic_lvtpc;
+        uint64_t pad;
+    } l;
+    union {
+        struct xen_pmu_amd_ctxt amd;
+        struct xen_pmu_intel_ctxt intel;
+#define XENPMU_CTXT_PAD_SZ  128
+        uint8_t pad[XENPMU_CTXT_PAD_SZ];

So assuming the variable size MSR arrays follow here, is 128 enough for forward compatibility? Or is 128 indeed only expected to cover the fixed size base structures (whichever way it is should perhaps be made explicit by way of adding a comment)?

Jan

#27 lore raw

Re: [PATCH v7 09/19] x86/VPMU: Add public xenpmu.h

Quoted 10 lines · Jan Beulich

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

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:
xen/arch/x86/hvm/vpmu.c +2 -0
@@ -228,6 +229,10 @@ void vpmu_initialise(struct vcpu *v)
      struct vpmu_struct *vpmu = vcpu_vpmu(v);
      uint8_t vendor = current_cpu_data.x86_vendor;
 
+    BUILD_BUG_ON((sizeof(struct xen_pmu_intel_ctxt) > XENPMU_CTXT_PAD_SZ) ||
+                 (sizeof(struct xen_pmu_amd_ctxt) > XENPMU_CTXT_PAD_SZ));

This should be two BUILD_BUG_ON()s, so that if one triggers there's no ambiguity in which of the structures grew too large.

Quoted 4 lines

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

Quoted 3 lines
xen/include/public/arch-x86/pmu.h new file +3 -0
+struct xen_pmu_amd_ctxt {
+    uint32_t counters;       /* Offset to counter MSRs */
+    uint32_t ctrls;          /* Offset to control MSRs */

Offsets relative to what?

Relative to context union of xen_arch_pmu (i.e. xen_pmu_amd_ctxt). I'll add a comment.

Quoted 23 lines

xen/include/public/arch-x86/pmu.h line 27

Quoted 11 lines
xen/include/public/arch-x86/pmu.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;
+    uint32_t fixed_counters;  /* Offset to fixed counter MSRs */
+    uint32_t arch_counters;   /* Offset to architectural counter MSRs */

Same question here.

Quoted 10 lines
xen/include/public/arch-x86/pmu.h new file +10 -0
+struct xen_arch_pmu {
+    union {
+        struct cpu_user_regs regs;
+        uint8_t pad[256];
+    } r;
+    union {
+        uint32_t lapic_lvtpc;
+        uint64_t pad;
+    } l;
+    union {
Quoted 8 lines

xen/include/public/arch-x86/pmu.h line 43

Quoted 4 lines
xen/include/public/arch-x86/pmu.h new file +4 -0
+        struct xen_pmu_amd_ctxt amd;
+        struct xen_pmu_intel_ctxt intel;
+#define XENPMU_CTXT_PAD_SZ  128
+        uint8_t pad[XENPMU_CTXT_PAD_SZ];

So assuming the variable size MSR arrays follow here, is 128 enough for forward compatibility? Or is 128 indeed only expected to cover the fixed size base structures (whichever way it is should perhaps be made explicit by way of adding a comment)?

128 is for fixed part only (which is currently 64 bytes Intel, which is larger). Will add a comment.

-boris

#28 lore raw

PATCH v7 10/19 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 abf55fb..07b7ba8 100644
@@ -403,6 +403,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 db37232..be4bed0 100644
@@ -150,9 +150,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 83b5461..001e20b 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
Acked-by on v7 10/19
#29 lore raw

Re: [PATCH v7 10/19] x86/VPMU: Make vpmu not HVM-specific

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:

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

I pointed this out on an earlier version of the series, perhaps on another patch (but comments like this should be taken to apply globally): An ack by a non-maintainer of any of the code being modified is bogus/confusing - it should either be Reviewed-by or left off.

Quoted 2 lines
#30 lore raw

Is: Reviewed-by, Acked-by rules. Was:Re: [PATCH v7 10/19] x86/VPMU: Make vpmu not HVM-specific

Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:

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

Quoted 8 lines
Quoted 2 lines

I pointed this out on an earlier version of the series, perhaps on another patch (but comments like this should be taken to apply globally): An ack by a non-maintainer of any of the code being modified is bogus/confusing - it should either be Reviewed-by or left off.

That is at odds with what the Linux style has:
---
13) When to use Acked-by: and Cc:

The Signed-off-by: tag indicates that the signer was involved in the development of the patch, or that he/she was in the patch's delivery path.

If a person was not directly involved in the preparation or handling of a patch but wishes to signify and record their approval of it then they can arrange to have an Acked-by: line added to the patch's changelog.

Acked-by: is often used by the maintainer of the affected code when that maintainer neither contributed to nor forwarded the patch.

Acked-by: is not as formal as Signed-off-by:. It is a record that the acker has at least reviewed the patch and has indicated acceptance. Hence patch mergers will sometimes manually convert an acker's "yep, looks good to me" into an Acked-by:.

Acked-by: does not necessarily indicate acknowledgement of the entire patch. For example, if a patch affects multiple subsystems and has an Acked-by: from one subsystem maintainer then this usually indicates acknowledgement of just the part which affects that maintainer's code. Judgement should be used here. When in doubt people should refer to the original discussion in the mailing list archives.

If a person has had the opportunity to comment on a patch, but has not provided such comments, you may optionally add a "Cc:" tag to the patch. This is the only tag which might be added without an explicit action by the person it names. This tag documents that potentially interested parties have been included in the discussion

---

Which does allow non-maintainers to provide 'Acked-by'. Which
is a lesser version of Reviewed-by by a non-maintainer. As 'Reviewed-by' has:

Reviewer's statement of oversight

By offering my Reviewed-by: tag, I state that:

(a) I have carried out a technical review of this patch to evaluate its appropriateness and readiness for inclusion into the mainline kernel.

(b) Any problems, concerns, or questions relating to the patch have been communicated back to the submitter. I am satisfied with the submitter's response to my comments.

(c) While there may be things that could be improved with this submission, I believe that it is, at this time, (1) a worthwhile modification to the kernel, and (2) free of known issues which would argue against its inclusion.

(d) While I have reviewed the patch and believe it to be sound, I do not (unless explicitly stated elsewhere) make any warranties or guarantees that it will achieve its stated purpose or function properly in any given situation.

Now obviously Xen != Linux, but if we want to this 'Acked-by' or 'Reviewed-by' policy to be different from Linux (which I think we should not), we should have it written somewhere so folks do not get confused.

And I think George has a different opinion of when Reviewed-by get carried on patches that undergo modifications. Sadly I don't see anything in the Linux Documentation/Submitting* to explain what the rules are for that.

Quoted 11 lines
Quoted 3 lines

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

#31 lore raw

Re: Is: Reviewed-by, Acked-by rules. Was:Re: [PATCH v7 10/19] x86/VPMU: Make vpmu not HVM-specific

Quoted 9 lines
Quoted 1 line
Quoted 1 line

Konrad Rzeszutek Wilk <[email protected]> 06/11/14 3:57 PM >>>

Quoted 5 lines · Jan Beulich

I pointed this out on an earlier version of the series, perhaps on another patch (but comments like this should be taken to apply globally): An ack by a non-maintainer of any of the code being modified is bogus/confusing - it should either be Reviewed-by or left off.

That is at odds with what the Linux style has:

I think you said mostly the same when I made a similar statement a couple of months ago; nothing has changed since then afaict.

Quoted 4 lines

Now obviously Xen != Linux, but if we want to this 'Acked-by' or 'Reviewed-by' policy to be different from Linux (which I think we should not), we should have it written somewhere so folks do not get confused.

Exactly - Xen != Linux. We've been following the maintainer-only-ack style for quite a while now, with infrequent acks from non-maintainers just ignored (or applied) silently. With this series however, I felt it would be better to mention the misuse as it has been on more than one patch.

Quoted 4 lines

And I think George has a different opinion of when Reviewed-by get carried on patches that undergo modifications. Sadly I don't see anything in the Linux Documentation/Submitting* to explain what the rules are for that.

I don't recall having a "different" opinion here - at least I agree: With non-benign changes to a patch, acks and reviews should be dropped. Fixing bugs in a patch - which I think is the recent case you recall - is clearly a non-benign change.

In fact I would go farther and suggest that more than a week or two old tags should also get dropped. There have been a number of cases not that long ago that new versions of patch series got posted weeks if not months after the most recent earlier versions. And among them have been cases where I wasn't really certain whether a tag with my name was actually validly there, or valid anymore.

Jan

#32 lore raw

Re: Is: Reviewed-by, Acked-by rules. Was:Re: [PATCH v7 10/19] x86/VPMU: Make vpmu not HVM-specific

Quoted 18 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Konrad Rzeszutek Wilk <[email protected]> 06/11/14 3:57 PM >>>

Quoted 5 lines · Jan Beulich

I pointed this out on an earlier version of the series, perhaps on another patch (but comments like this should be taken to apply globally): An ack by a non-maintainer of any of the code being modified is bogus/confusing - it should either be Reviewed-by or left off.

That is at odds with what the Linux style has:

I think you said mostly the same when I made a similar statement a couple of months ago; nothing has changed since then afaict.

Quoted 4 lines

Now obviously Xen != Linux, but if we want to this 'Acked-by' or 'Reviewed-by' policy to be different from Linux (which I think we should not), we should have it written somewhere so folks do not get confused.

Quoted 4 lines

Exactly - Xen != Linux. We've been following the maintainer-only-ack style for quite a while now, with infrequent acks from non-maintainers just ignored (or applied) silently. With this series however, I felt it would be better to mention the misuse as it has been on more than one patch.

I'm not sure why you *object* to non-maintainer Acks, Jan -- people who are either pushing the patch or applying the patch need to know who the maintainers are anyway, if for no other reason to know when a "Reviewed-by" counts as a maintainer Ack or not.

It seems to me there are several times when non-maintainer Acks are useful; for example, when someone has previously objected to a patch, and is now OK with it, but has not actually reviewed it.

Quoted 10 lines
Quoted 4 lines

And I think George has a different opinion of when Reviewed-by get carried on patches that undergo modifications. Sadly I don't see anything in the Linux Documentation/Submitting* to explain what the rules are for that.

I don't recall having a "different" opinion here - at least I agree: With non-benign changes to a patch, acks and reviews should be dropped. Fixing bugs in a patch - which I think is the recent case you recall - is clearly a non-benign change.

Quoted 6 lines

In fact I would go farther and suggest that more than a week or two old tags should also get dropped. There have been a number of cases not that long ago that new versions of patch series got posted weeks if not months after the most recent earlier versions. And among them have been cases where I wasn't really certain whether a tag with my name was actually validly there, or valid anymore.

FWIW, one example was Dario's soft-affinity series; when he was here before the hackathon we actually discussed what the protocol should be for that. I didn't think it was clear, so I said he should retain them, and people could complain if that's not what they wanted. :-)

For my part, I tend to appreciate being reminded of what my opinion was the last time around, so I don't think time itself should expire my tags. The tags should expire when either the patch itself, or the code it depends on, has changed in a functionally significant way. It should be the job of the person submitting the code to keep track of that.

The only risk to doing things this way, I suppose, would be if code the patch depends on had changed in a way that was incompatible, and the patch author didn't notice; the "Reviewed-by" might lull the maintainer into a false sense of security.

-George

#33 lore raw

Re: Is: Reviewed-by, Acked-by rules. Was:Re: [PATCH v7 10/19] x86/VPMU: Make vpmu not HVM-specific

Quoted 9 lines
Quoted 1 line
Quoted 1 line

George Dunlap <[email protected]> 06/12/14 1:10 PM >>>

I'm not sure why you *object* to non-maintainer Acks, Jan -- people who are either pushing the patch or applying the patch need to know who the maintainers are anyway, if for no other reason to know when a "Reviewed-by" counts as a maintainer Ack or not.

It seems to me there are several times when non-maintainer Acks are useful; for example, when someone has previously objected to a patch, and is now OK with it, but has not actually reviewed it.

This is valid point. But other than unusual (I would think) situations like this - what meaning would an ack of a non-maintainer really have? It's not been a proper review (which, by refusing non-maintainer acks I would hope we might get more of), and it also doesn't count for anything else from a purely abstract pov. And I guess - back to the point you make - there are other ways to express that reasons for previous objection have got eliminated. After all - along the lines of what you said - the person pushing a patch is expected to have followed an eventual discussion relatively closely anyway.

Quoted 17 lines
Quoted 10 lines
Quoted 4 lines

And I think George has a different opinion of when Reviewed-by get carried on patches that undergo modifications. Sadly I don't see anything in the Linux Documentation/Submitting* to explain what the rules are for that.

I don't recall having a "different" opinion here - at least I agree: With non-benign changes to a patch, acks and reviews should be dropped. Fixing bugs in a patch - which I think is the recent case you recall - is clearly a non-benign change.

Quoted 6 lines

In fact I would go farther and suggest that more than a week or two old tags should also get dropped. There have been a number of cases not that long ago that new versions of patch series got posted weeks if not months after the most recent earlier versions. And among them have been cases where I wasn't really certain whether a tag with my name was actually validly there, or valid anymore.

Quoted 4 lines

FWIW, one example was Dario's soft-affinity series; when he was here before the hackathon we actually discussed what the protocol should be for that. I didn't think it was clear, so I said he should retain them, and people could complain if that's not what they wanted. :-)

And iirc you then were the one who complained...

Quoted 6 lines

For my part, I tend to appreciate being reminded of what my opinion was the last time around, so I don't think time itself should expire my tags. The tags should expire when either the patch itself, or the code it depends on, has changed in a functionally significant way. It should be the job of the person submitting the code to keep track of that.

Yes, I appreciate that there are reasons to retain old tags, and I didn't mean to push (or initiate the pushing of) any new policy here. I merely wanted to express that, as you say:

Quoted 4 lines

The only risk to doing things this way, I suppose, would be if code the patch depends on had changed in a way that was incompatible, and the patch author didn't notice; the "Reviewed-by" might lull the maintainer into a false sense of security.

... there's a risk with retaining tags over long periods of time. Ideally of
course things would get followed up within shorter time distances, but I know very well that this isn't always possible.

Jan

#34 lore raw

PATCH v7 11/19 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/domain.c              |   4 +-
 xen/arch/x86/hvm/svm/vpmu.c        |   4 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c  |  10 +--
 xen/arch/x86/hvm/vpmu.c            | 121 ++++++++++++++++++++++++++++++++++---
 xen/arch/x86/x86_64/compat/entry.S |   4 ++
 xen/arch/x86/x86_64/entry.S        |   4 ++
 xen/include/asm-x86/hvm/vpmu.h     |  14 ++---
 xen/include/public/pmu.h           |  48 +++++++++++++++
 xen/include/public/xen.h           |   1 +
 xen/include/xen/hypercall.h        |   4 ++
 10 files changed, 188 insertions(+), 26 deletions(-)
xen/arch/x86/domain.c +2 -2
index e896210..a3ac1e2 100644
@@ -1482,7 +1482,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
 
     if ( is_hvm_vcpu(prev) )
     {
-        if (prev != next)
+        if ( (prev != next) && (vpmu_mode & XENPMU_MODE_ON) )
             vpmu_save(prev);
 
         if ( !list_empty(&prev->arch.hvm_vcpu.tm_list) )
@@ -1526,7 +1526,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
                            !is_hardware_domain(next->domain));
     }
 
-    if (is_hvm_vcpu(next) && (prev != next) )
+    if ( is_hvm_vcpu(next) && (prev != next) && (vpmu_mode & XENPMU_MODE_ON) )
         /* Must be done with interrupts enabled */
         vpmu_load(next);
 
xen/arch/x86/hvm/svm/vpmu.c +2 -2
index b5ca577..afeb32c 100644
@@ -473,14 +473,14 @@ struct arch_vpmu_ops amd_vpmu_ops = {
     .arch_vpmu_dump = amd_vpmu_dump
 };
 
-int svm_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
+int svm_vpmu_initialise(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     uint8_t family = current_cpu_data.x86;
     int ret = 0;
 
     /* vpmu enabled? */
-    if ( !vpmu_flags )
+    if ( vpmu_mode == XENPMU_MODE_OFF )
         return 0;
 
     switch ( family )
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -5
index b5bc8ea..336a425 100644
@@ -705,13 +705,13 @@ static int core2_vpmu_do_interrupt(struct cpu_user_regs *regs)
     return 1;
 }
 
-static int core2_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
+static int core2_vpmu_initialise(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     u64 msr_content;
     struct cpuinfo_x86 *c = &current_cpu_data;
 
-    if ( !(vpmu_flags & VPMU_BOOT_BTS) )
+    if ( !(vpmu_features & XENPMU_FEATURE_INTEL_BTS) )
         goto func_out;
     /* Check the 'Debug Store' feature in the CPUID.EAX[1]:EDX[21] */
     if ( cpu_has(c, X86_FEATURE_DS) )
@@ -823,7 +823,7 @@ struct arch_vpmu_ops core2_no_vpmu_ops = {
     .do_cpuid = core2_no_vpmu_do_cpuid,
 };
 
-int vmx_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
+int vmx_vpmu_initialise(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     uint8_t family = current_cpu_data.x86;
@@ -831,7 +831,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_mode == XENPMU_MODE_OFF )
         return 0;
 
     if ( family == 6 )
@@ -874,7 +874,7 @@ int vmx_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
         /* future: */
         case 0x3d:
         case 0x4e:
-            ret = core2_vpmu_initialise(v, vpmu_flags);
+            ret = core2_vpmu_initialise(v);
             if ( !ret )
                 vpmu->arch_vpmu_ops = &core2_vpmu_ops;
             return ret;
xen/arch/x86/hvm/vpmu.c +113 -8

174 lines not on this page · raw message

xen/arch/x86/x86_64/compat/entry.S +4 -0
index ac594c9..8587c46 100644
@@ -417,6 +417,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
@@ -465,6 +467,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 4796e65..cd4fe47 100644
@@ -769,6 +769,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
@@ -817,6 +819,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 +5 -9
index 001e20b..12a45c4 100644
@@ -24,13 +24,6 @@
 
 #include <public/pmu.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)
 
@@ -58,8 +51,8 @@ struct arch_vpmu_ops {
     void (*arch_vpmu_dump)(const struct vcpu *);
 };
 
-int vmx_vpmu_initialise(struct vcpu *, unsigned int flags);
-int svm_vpmu_initialise(struct vcpu *, unsigned int flags);
+int vmx_vpmu_initialise(struct vcpu *);
+int svm_vpmu_initialise(struct vcpu *);
 
 struct vpmu_struct {
     u32 flags;
@@ -99,5 +92,8 @@ void vpmu_dump(struct vcpu *v);
 extern int acquire_pmu_ownership(int pmu_ownership);
 extern void release_pmu_ownership(int pmu_ownership);
 
+extern uint64_t vpmu_mode;
+extern uint64_t vpmu_features;
+
 #endif /* __ASM_X86_HVM_VPMU_H_*/
 
xen/include/public/pmu.h +48 -0

54 lines not on this page · raw message

xen/include/public/xen.h +1 -0
index a6a2092..0766790 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/xen/hypercall.h +4 -0
index a9e5229..cf34547 100644
@@ -14,6 +14,7 @@
 #include <public/event_channel.h>
 #include <public/tmem.h>
 #include <public/version.h>
+#include <public/pmu.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
#35 lore raw

Re: [PATCH v7 11/19] x86/VPMU: Interface for setting PMU mode and flags

Quoted 42 lines · 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/vpmu.c +22 -0
@@ -275,3 +283,100 @@ void vpmu_dump(struct vcpu *v)
         vpmu->arch_vpmu_ops->arch_vpmu_dump(v);
 }
 
+/* Unload VPMU contexts */
+static void vpmu_unload_all(void)
+{
+    struct domain *d;
+    struct vcpu *v;
+    struct vpmu_struct *vpmu;
+
+    for_each_domain( d )
+    {
+        for_each_vcpu ( d, v )
+        {
+            if ( v != current )
+                vcpu_pause(v);
+            vpmu = vcpu_vpmu(v);
+
+            if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
+            {
+                if ( v != current )
+                    vcpu_unpause(v);
+                continue;
+            }
+

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+            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);
+
+            if ( v != current )
+                vcpu_unpause(v);
+        }
+    }
+}

This is a very long function without any preemption.

To reduce its impact greatly, would it be better to have a global mode controlling vcpu context switching? You could enable "VCPU_GLOBAL_DISABLING", then IPI all cpus at the same time to save their vpmu context. Once the IPI is complete, all vpmu context is clean.

This avoids a double loop and all the vcpu_pause/unpause()s.

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

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

This is probably going to want to be XSM'd up for permissions?

Quoted 22 lines
xen/include/public/pmu.h +13 -0
index d237c25..d41049f 100644
@@ -13,6 +13,54 @@
 #define XENPMU_VER_MAJ    0
 #define XENPMU_VER_MIN    1
 
+/*
+ * ` enum neg_errnoval
+ * ` HYPERVISOR_xenpmu_op(enum xenpmu_op cmd, struct xenpmu_params *args);
+ *
+ * @cmd  == XENPMU_* (PMU operation)
+ * @args == struct xenpmu_params
+ */
+/* ` enum xenpmu_op { */
+#define XENPMU_mode_get        0 /* Also used for getting PMU version */
+#define XENPMU_mode_set        1
+#define XENPMU_feature_get     2
+#define XENPMU_feature_set     3
+/* ` } */

xen/include/public/pmu.h line 38

Quoted 10 lines
xen/include/public/pmu.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;

introducing "struct version" into the namespace is very antisocial.

xen/include/public/pmu.h line 47

Quoted 9 lines
xen/include/public/pmu.h +9 -0
+    } v;
+    union {
+        uint64_t val;
+        XEN_GUEST_HANDLE(void) valp;
+    } d;
+
+    /* IN parameters */
+    uint64_t vcpu;
+};

Bits are cheep these days.

struct xen_pmu_params {
    struct { uint32_t maj, min; } version;
    XEN_GUEST_HANDLE_64(void) valp;
    uint64_t vcpu;
};

Comes with far less padding/unions and is still 32/64bit safe.

#36 lore raw

Re: [PATCH v7 11/19] x86/VPMU: Interface for setting PMU mode and flags

Quoted 55 lines · Andrew Cooper
Quoted 42 lines · 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/vpmu.c +22 -0
@@ -275,3 +283,100 @@ void vpmu_dump(struct vcpu *v)
          vpmu->arch_vpmu_ops->arch_vpmu_dump(v);
  }
 
+/* Unload VPMU contexts */
+static void vpmu_unload_all(void)
+{
+    struct domain *d;
+    struct vcpu *v;
+    struct vpmu_struct *vpmu;
+
+    for_each_domain( d )
+    {
+        for_each_vcpu ( d, v )
+        {
+            if ( v != current )
+                vcpu_pause(v);
+            vpmu = vcpu_vpmu(v);
+
+            if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
+            {
+                if ( v != current )
+                    vcpu_unpause(v);
+                continue;
+            }
+

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+            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);
+
+            if ( v != current )
+                vcpu_unpause(v);
+        }
+    }
+}

This is a very long function without any preemption.

Quoted 6 lines

To reduce its impact greatly, would it be better to have a global mode controlling vcpu context switching? You could enable "VCPU_GLOBAL_DISABLING", then IPI all cpus at the same time to save their vpmu context. Once the IPI is complete, all vpmu context is clean.

This avoids a double loop and all the vcpu_pause/unpause()s.

I can see the problem but I am not sure what you are suggesting will work (or perhaps I don't quite understand what you are suggesting). If I IPI a processor may be in the middle of working on the VPMU context.

Let me think about this.

Quoted 8 lines

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

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

This is probably going to want to be XSM'd up for permissions?

I haven't looked at this since earlier versions of the patch but when I did it turned out we were out of bits in the access vector (or whatever that structure was). I in fact mentioned this problem in the cover letter for the series.

-boris

#37 lore raw

Re: [PATCH v7 11/19] x86/VPMU: Interface for setting PMU mode and flags

In reply to Boris Ostrovsky
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:
xen/arch/x86/x86_64/compat/entry.S +2 -0
@@ -417,6 +417,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

xen/arch/x86/x86_64/compat/entry.S ENTRY(compat_hypercall_args_table) · line 474

Quoted 8 lines
xen/arch/x86/x86_64/compat/entry.S +2 -0
@@ -465,6 +467,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

I'm pretty certain I said on an earlier version of the patch series
already: If you use the same handler for native and compat mode hypercalls, you should add structure layout verification (see the ? entries in xen/include/xlat.lst and how what they produce is being used in various places). And the fact that the interface structure contains a handle already makes clear that this check will fail - I don't think there's a way around having at least a thin compat mode wrapper here.

Jan

#38 lore raw

Re: [PATCH v7 11/19] x86/VPMU: Interface for setting PMU mode and flags

Quoted 21 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:
xen/arch/x86/x86_64/compat/entry.S +2 -0
@@ -417,6 +417,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

xen/arch/x86/x86_64/compat/entry.S ENTRY(compat_hypercall_args_table) · line 474

Quoted 8 lines
xen/arch/x86/x86_64/compat/entry.S +2 -0
@@ -465,6 +467,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
Quoted 8 lines

I'm pretty certain I said on an earlier version of the patch series
already: If you use the same handler for native and compat mode hypercalls, you should add structure layout verification (see the ? entries in xen/include/xlat.lst and how what they produce is being used in various places). And the fact that the interface structure contains a handle already makes clear that this check will fail - I don't think there's a way around having at least a thin compat mode wrapper here.

Right. And I decided to drop the handle since noone is using it now anyway and I don't want to add a compat layer for something that may never get used.

I have a question though. I want to add a compat check for

struct xen_pmu_arch {
    union {
        struct cpu_user_regs regs;
        uint8_t pad[256];
    } r;
    union {
        uint32_t lapic_lvtpc;
        uint64_t pad;
    } l;
    union {
        struct xen_pmu_amd_ctxt amd;
        struct xen_pmu_intel_ctxt intel;
        uint8_t pad[128];
    } c;
};

which is a shared structure (i.e. it's not used across a hypercall). The problem I am having is that cpu_user_regs needs to be translated. Does the xlat framework allow checking a structure that needs one of its fields translated? I went through the scripts and it appears that it doesn't.

Thanks.
-boris

#39 lore raw

Re: [PATCH v7 11/19] x86/VPMU: Interface for setting PMU mode and flags

Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 17.06.14 at 17:01, <[email protected]> wrote:

I have a question though. I want to add a compat check for

struct xen_pmu_arch {
     union {
         struct cpu_user_regs regs;
         uint8_t pad[256];
     } r;
     union {
         uint32_t lapic_lvtpc;
         uint64_t pad;
     } l;
     union {
         struct xen_pmu_amd_ctxt amd;
         struct xen_pmu_intel_ctxt intel;
         uint8_t pad[128];
     } c;
};
Quoted 5 lines

which is a shared structure (i.e. it's not used across a hypercall). The problem I am having is that cpu_user_regs needs to be translated. Does the xlat framework allow checking a structure that needs one of its fields translated? I went through the scripts and it appears that it doesn't.

Indeed this is an all-or-nothing thing. Naming (and perhaps splitting out) the latter two sub-unions may allow checking them individually though.

Jan

#40 lore raw

PATCH v7 12/19 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/hvm.c            |  3 +-
 xen/arch/x86/hvm/svm/svm.c        |  8 +++-
 xen/arch/x86/hvm/svm/vpmu.c       | 37 +++++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c        |  9 ++++-
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 67 ++++++++++++++++++++++----------
 xen/arch/x86/hvm/vpmu.c           | 82 ++++++++++++++++++++++++++++++++++++++-
 xen/common/event_channel.c        |  1 +
 xen/include/asm-x86/hvm/vpmu.h    |  1 +
 xen/include/public/pmu.h          |  2 +
 xen/include/public/xen.h          |  1 +
 10 files changed, 169 insertions(+), 42 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -1
index d2190be..fab54ff 100644
@@ -4031,7 +4031,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/svm/svm.c +6 -2
index 870e4ee..7358626 100644
@@ -1150,7 +1150,9 @@ static int svm_vcpu_initialise(struct vcpu *v)
         return rc;
     }
 
-    vpmu_initialise(v);
+    /* PVH's VPMU is initialized via hypercall */
+    if ( is_hvm_domain(v->domain) )
+        vpmu_initialise(v);
 
     svm_guest_osvw_init(v);
 
@@ -1159,7 +1161,9 @@ static int svm_vcpu_initialise(struct vcpu *v)
 
 static void svm_vcpu_destroy(struct vcpu *v)
 {
-    vpmu_destroy(v);
+    /* PVH's VPMU destroyed via hypercall */
+    if ( is_hvm_domain(v->domain) )
+        vpmu_destroy(v);
     svm_destroy_vmcb(v);
     passive_domain_destroy(v);
 }
xen/arch/x86/hvm/svm/vpmu.c +22 -15
index afeb32c..96833ea 100644
@@ -382,16 +382,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_hvm_domain(v->domain) )
     {
-        gdprintk(XENLOG_WARNING, "Insufficient memory for PMU, "
-            " PMU feature is unavailable on domain %d vcpu %d.\n",
-            v->vcpu_id, v->domain->domain_id);
-        return -ENOMEM;
+        ctxt = xzalloc_bytes(sizeof(struct 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;
@@ -409,17 +414,19 @@ static void amd_vpmu_destroy(struct vcpu *v)
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
 
-    if ( has_hvm_container_domain(v->domain) && is_msr_bitmap_on(vpmu) )
-        amd_vpmu_unset_msr_bitmap(v);
+    if ( has_hvm_container_domain(v->domain) )
+    {
+        if ( is_msr_bitmap_on(vpmu) )
+            amd_vpmu_unset_msr_bitmap(v);
 
-    xfree(vpmu->context);
-    vpmu_reset(vpmu, VPMU_CONTEXT_ALLOCATED);
+        if ( is_hvm_domain(v->domain) )
+            xfree(vpmu->context);
 
-    if ( vpmu_is_set(vpmu, VPMU_RUNNING) )
-    {
-        vpmu_reset(vpmu, VPMU_RUNNING);
         release_pmu_ownship(PMU_OWNER_HVM);
     }
+
+    vpmu->context = NULL;
+    vpmu_clear(vpmu);
 }
 
 /* VPMU part of the 'q' keyhandler */
xen/arch/x86/hvm/vmx/vmx.c +7 -2
index 6f55c9e..5313112 100644
@@ -116,7 +116,9 @@ static int vmx_vcpu_initialise(struct vcpu *v)
         return rc;
     }
 
-    vpmu_initialise(v);
+    /* PVH's VPMU is initialized via hypercall */
+    if ( is_hvm_domain(v->domain) )
+        vpmu_initialise(v);
 
     vmx_install_vlapic_mapping(v);
 
@@ -130,7 +132,10 @@ static int vmx_vcpu_initialise(struct vcpu *v)
 static void vmx_vcpu_destroy(struct vcpu *v)
 {
     vmx_destroy_vmcs(v);
-    vpmu_destroy(v);
+
+    /* PVH's VPMU is destroyed via hypercall */
+    if ( is_hvm_domain(v->domain) )
+        vpmu_destroy(v);
     passive_domain_destroy(v);
 }
 
xen/arch/x86/hvm/vmx/vpmu_core2.c +46 -21

101 lines not on this page · raw message

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

111 lines not on this page · raw message

xen/common/event_channel.c +1 -0
index 6853842..7f9b8c9 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 12a45c4..24fb39b 100644
@@ -61,6 +61,7 @@ struct vpmu_struct {
     void *context;      /* May be shared with PV guest */
     void *priv_context; /* hypervisor-only */
     struct arch_vpmu_ops *arch_vpmu_ops;
+    xen_pmu_data_t *xenpmu_data;
 };
 
 /* VPMU states */
xen/include/public/pmu.h +2 -0
index d41049f..6da3596 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/public/xen.h +1 -0
index 0766790..e4d0b79 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
Signature · 1 line
1.8.1.4
#41 lore raw

Re: [PATCH v7 12/19] x86/VPMU: Initialize PMU for PV guests

Quoted 1 line · Boris Ostrovsky

Code for initializing/tearing down PMU for PV guests

PVH guests judging by the content?

Quoted 37 lines
---
 xen/arch/x86/hvm/hvm.c            |  3 +-
 xen/arch/x86/hvm/svm/svm.c        |  8 +++-
 xen/arch/x86/hvm/svm/vpmu.c       | 37 +++++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c        |  9 ++++-
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 67 ++++++++++++++++++++++----------
 xen/arch/x86/hvm/vpmu.c           | 82 ++++++++++++++++++++++++++++++++++++++-
 xen/common/event_channel.c        |  1 +
 xen/include/asm-x86/hvm/vpmu.h    |  1 +
 xen/include/public/pmu.h          |  2 +
 xen/include/public/xen.h          |  1 +
 10 files changed, 169 insertions(+), 42 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -1
index d2190be..fab54ff 100644
@@ -4031,7 +4031,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/svm/svm.c +0 -0
index 870e4ee..7358626 100644

xen/arch/x86/hvm/svm/svm.c static int svm_vcpu_initialise(struct vcpu *v) · line 1156

Quoted 8 lines
xen/arch/x86/hvm/svm/svm.c +3 -1
@@ -1150,7 +1150,9 @@ static int svm_vcpu_initialise(struct vcpu *v)
         return rc;
     }
 
-    vpmu_initialise(v);
+    /* PVH's VPMU is initialized via hypercall */
+    if ( is_hvm_domain(v->domain) )
+        vpmu_initialise(v);
 

With the wish to merge HVM and PVH, is this wise? What does hypercall-based setup gain us over MSR based setup, given that we have to provide the MSR access to HVM guests anyway?

~Andrew

#42 lore raw

Re: [PATCH v7 12/19] x86/VPMU: Initialize PMU for PV guests

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

Code for initializing/tearing down PMU for PV guests

PVH guests judging by the content?

PV and PVH actually.

Quoted 40 lines
Quoted 40 lines
---
  xen/arch/x86/hvm/hvm.c            |  3 +-
  xen/arch/x86/hvm/svm/svm.c        |  8 +++-
  xen/arch/x86/hvm/svm/vpmu.c       | 37 +++++++++++-------
  xen/arch/x86/hvm/vmx/vmx.c        |  9 ++++-
  xen/arch/x86/hvm/vmx/vpmu_core2.c | 67 ++++++++++++++++++++++----------
  xen/arch/x86/hvm/vpmu.c           | 82 ++++++++++++++++++++++++++++++++++++++-
  xen/common/event_channel.c        |  1 +
  xen/include/asm-x86/hvm/vpmu.h    |  1 +
  xen/include/public/pmu.h          |  2 +
  xen/include/public/xen.h          |  1 +
  10 files changed, 169 insertions(+), 42 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -1
index d2190be..fab54ff 100644
@@ -4031,7 +4031,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/svm/svm.c +0 -0
index 870e4ee..7358626 100644
@@ -1150,7 +1150,9 @@ static int svm_vcpu_initialise(struct vcpu *v)
          return rc;
      }
 
Quoted 8 lines

xen/arch/x86/hvm/svm/svm.c static int svm_vcpu_initialise(struct vcpu *v) · line 1156

Quoted 5 lines
xen/arch/x86/hvm/svm/svm.c +3 -1
-    vpmu_initialise(v);
+    /* PVH's VPMU is initialized via hypercall */
+    if ( is_hvm_domain(v->domain) )
+        vpmu_initialise(v);
 

With the wish to merge HVM and PVH, is this wise? What does hypercall-based setup gain us over MSR based setup, given that we have to provide the MSR access to HVM guests anyway?

Interrupts.

We have to go via event channel and the handler for VIRQ_PMU in Linux wants to access MSRs via shared page (i.e. not do actual MSR access). That page is set up duting the hypercall.

Once we add APIC support for PVH I think most of PMU support will follow HVM code. But for now we have to do it separately.

-boris

#43 lore raw

Re: [PATCH v7 12/19] x86/VPMU: Initialize PMU for PV guests

In reply to Boris Ostrovsky

xen/arch/x86/hvm/hvm.c static hvm_hypercall_t *const pvh_hypercall64_table[NR_hypercalls] = { · line 4035

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:

Code for initializing/tearing down PMU for PV guests
...

xen/arch/x86/hvm/hvm.c +2 -1
@@ -4031,7 +4031,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)

How is this change related to the subject and description?

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
+{
+    struct vcpu *v;
+    struct page_info *page;
+    uint64_t gfn = params->d.val;
+
+    if ( !params || params->vcpu >= d->max_vcpus )
+        return -EINVAL;
+
+    page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);

xen/arch/x86/hvm/hvm.c static hvm_hypercall_t *const pvh_hypercall64_table[NR_hypercalls] = {

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+    if ( !page )
+        return -EINVAL;
+
+    if ( !get_page_type(page, PGT_writable_page) )
+    {
+        put_page(page);
+        return -EINVAL;
+    }
+
+    v = d->vcpu[params->vcpu];

I'm sure I asked this before - what guarantees (now as well as going forward) that you get non-NULL here? Various existing code paths properly verify both d->vcpu and d->vcpu[...] not being NULL, so you should do this consistently too.

Jan

#44 lore raw

Re: [PATCH v7 12/19] x86/VPMU: Initialize PMU for PV guests

Quoted 13 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c static hvm_hypercall_t *const pvh_hypercall64_table[NR_hypercalls] = { · line 4035

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:

Code for initializing/tearing down PMU for PV guests
...

xen/arch/x86/hvm/hvm.c +2 -1
@@ -4031,7 +4031,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)

How is this change related to the subject and description?

Yes, this is result of my eliminating PVH patch as a standalone one and spreading its content over other patches. I'll update the text.

Quoted 17 lines
Quoted 17 lines
xen/arch/x86/hvm/hvm.c +16 -0
+static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
+{
+    struct vcpu *v;
+    struct page_info *page;
+    uint64_t gfn = params->d.val;
+
+    if ( !params || params->vcpu >= d->max_vcpus )
+        return -EINVAL;
+
+    page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);
+    if ( !page )
+        return -EINVAL;
+
+    if ( !get_page_type(page, PGT_writable_page) )
+    {
+        put_page(page);
Quoted 8 lines

xen/arch/x86/hvm/hvm.c

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +4 -0
+        return -EINVAL;
+    }
+
+    v = d->vcpu[params->vcpu];

I'm sure I asked this before - what guarantees (now as well as going forward) that you get non-NULL here? Various existing code paths properly verify both d->vcpu and d->vcpu[...] not being NULL, so you should do this consistently too.

Ah, I thought you were referring to params check last time. I'll add vcpu checks as well.

-boris

#45 lore raw

Re: [PATCH v7 12/19] x86/VPMU: Initialize PMU for PV guests

Quoted 46 lines
Quoted 1 line
Quoted 1 line · On 11.06.14 at 14:49, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c static hvm_hypercall_t *const

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 06.06.14 at 19:40, <[email protected]> wrote:

Code for initializing/tearing down PMU for PV guests
...

xen/arch/x86/hvm/hvm.c +0 -0
@@ -4031,7 +4031,8 @@ static hvm_hypercall_t *const 

pvh_hypercall64_table[NR_hypercalls] = {

Quoted 7 lines

xen/arch/x86/hvm/hvm.c static hvm_hypercall_t *const pvh_hypercall64_table[NR_hypercalls] = { · line 4035

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +2 -1
      [ __HYPERVISOR_physdev_op ]      = (hvm_hypercall_t *)hvm_physdev_op,
      HYPERCALL(hvm_op),
      HYPERCALL(sysctl),
-    HYPERCALL(domctl)
+    HYPERCALL(domctl),
+    HYPERCALL(xenpmu_op)

How is this change related to the subject and description?

Yes, this is result of my eliminating PVH patch as a standalone one and spreading its content over other patches. I'll update the text.

Quoted 17 lines
Quoted 17 lines
xen/arch/x86/hvm/hvm.c +16 -0
+static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
+{
+    struct vcpu *v;
+    struct page_info *page;
+    uint64_t gfn = params->d.val;
+
+    if ( !params || params->vcpu >= d->max_vcpus )
+        return -EINVAL;
+
+    page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);
+    if ( !page )
+        return -EINVAL;
+
+    if ( !get_page_type(page, PGT_writable_page) )
+    {
+        put_page(page);
Quoted 8 lines

xen/arch/x86/hvm/hvm.c

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +4 -0
+        return -EINVAL;
+    }
+
+    v = d->vcpu[params->vcpu];

I'm sure I asked this before - what guarantees (now as well as going forward) that you get non-NULL here? Various existing code paths properly verify both d->vcpu and d->vcpu[...] not being NULL, so you should do this consistently too.

Quoted 2 lines

Ah, I thought you were referring to params check last time. I'll add vcpu checks as well.

And drop checking param against NULL - I don't think that check makes any sense.

Jan

#46 lore raw

PATCH v7 13/19 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 | 69 ++++++++++++++++++++++++++++++---------
 xen/arch/x86/hvm/vpmu.c           |  7 ++++
 xen/arch/x86/traps.c              | 48 +++++++++++++++++++++++++--
 xen/include/public/pmu.h          |  1 +
 5 files changed, 108 insertions(+), 20 deletions(-)
xen/arch/x86/domain.c +1 -2
index a3ac1e2..bb759dd 100644
@@ -2012,8 +2012,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 +54 -15

172 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +7 -0
index bceffa6..ca0534b 100644
@@ -456,6 +456,13 @@ 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 ( current->arch.vpmu.xenpmu_data == NULL )
+            return -EINVAL;
+        vpmu_lvtpc_update(current->arch.vpmu.xenpmu_data->pmu.l.lapic_lvtpc);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/arch/x86/traps.c +45 -3

97 lines not on this page · raw message

xen/include/public/pmu.h +1 -0
index 6da3596..7341de9 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
#47 lore raw

Re: [PATCH v7 13/19] x86/VPMU: Add support for PMU register handling on PV guests

Quoted 33 lines · 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 | 69 ++++++++++++++++++++++++++++++---------
 xen/arch/x86/hvm/vpmu.c           |  7 ++++
 xen/arch/x86/traps.c              | 48 +++++++++++++++++++++++++--
 xen/include/public/pmu.h          |  1 +
 5 files changed, 108 insertions(+), 20 deletions(-)
xen/arch/x86/domain.c +1 -2
index a3ac1e2..bb759dd 100644
@@ -2012,8 +2012,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 +0 -0
@@ -440,9 +454,16 @@ static int core2_vpmu_msr_common_check(u32 msr_index, int *type, int *index)
     return 1;

xen/arch/x86/hvm/vmx/vpmu_core2.c static int core2_vpmu_msr_common_check(u32 msr_index, int *type, int *index) · line 464

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +8 -0
 }
 
+static void inject_trap(struct vcpu *v, unsigned int trapno)
+{
+    if ( has_hvm_container_domain(v->domain) )
+        hvm_inject_hw_exception(trapno, 0);
+    else
+        send_guest_trap(v->domain, v->vcpu_id, trapno);
+}
+

send_guest_trap() cant be used to send anything other than an nmi or mce, but you appear to be using it to try and send #PFs ?

Quoted 79 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +17 -8
 static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
-    u64 global_ctrl;
     int i, tmp;
     int type = -1, index = -1;
     struct vcpu *v = current;
@@ -466,7 +487,7 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
                 if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_BTS) )
                     return 1;
                 gdprintk(XENLOG_WARNING, "Debug Store is not supported on this cpu\n");
-                hvm_inject_hw_exception(TRAP_gp_fault, 0);
+                inject_trap(v, TRAP_gp_fault);
                 return 0;
             }
         }
@@ -479,11 +500,12 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     {
     case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
         core2_vpmu_cxt->global_status &= ~msr_content;
+        wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, msr_content);
         return 1;
     case MSR_CORE_PERF_GLOBAL_STATUS:
         gdprintk(XENLOG_INFO, "Can not write readonly MSR: "
                  "MSR_PERF_GLOBAL_STATUS(0x38E)!\n");
-        hvm_inject_hw_exception(TRAP_gp_fault, 0);
+        inject_trap(v, TRAP_gp_fault);
         return 1;
     case MSR_IA32_PEBS_ENABLE:
         if ( msr_content & 1 )
@@ -499,7 +521,7 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
                 gdprintk(XENLOG_WARNING,
                          "Illegal address for IA32_DS_AREA: %#" PRIx64 "x\n",
                          msr_content);
-                hvm_inject_hw_exception(TRAP_gp_fault, 0);
+                inject_trap(v, TRAP_gp_fault);
                 return 1;
             }
             core2_vpmu_cxt->ds_area = msr_content;
@@ -508,10 +530,14 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         gdprintk(XENLOG_WARNING, "Guest setting of DTS is ignored.\n");
         return 1;
     case MSR_CORE_PERF_GLOBAL_CTRL:
-        global_ctrl = msr_content;
+        core2_vpmu_cxt->global_ctrl = msr_content;
         break;
     case MSR_CORE_PERF_FIXED_CTR_CTRL:
-        vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
+        if ( has_hvm_container_domain(v->domain) )
+            vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL,
+                               &core2_vpmu_cxt->global_ctrl);
+        else
+            rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, core2_vpmu_cxt->global_ctrl);
         *enabled_cntrs &= ~(((1ULL << fixed_pmc_cnt) - 1) << 32);
         if ( msr_content != 0 )
         {
@@ -533,7 +559,11 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
             struct xen_pmu_cntr_pair *xen_pmu_cntr_pair =
                 vpmu_reg_pointer(core2_vpmu_cxt, arch_counters);
 
-            vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
+            if ( has_hvm_container_domain(v->domain) )
+                vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL,
+                                   &core2_vpmu_cxt->global_ctrl);
+            else
+                rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, core2_vpmu_cxt->global_ctrl);
 
             if ( msr_content & (1ULL << 22) )
                 *enabled_cntrs |= 1ULL << tmp;
@@ -544,7 +574,8 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         }
     }
 
-    if ((global_ctrl & *enabled_cntrs) || (core2_vpmu_cxt->ds_area != 0)  )
+    if ( (core2_vpmu_cxt->global_ctrl & *enabled_cntrs) ||
+         (core2_vpmu_cxt->ds_area != 0) )
         vpmu_set(vpmu, VPMU_RUNNING);
     else
         vpmu_reset(vpmu, VPMU_RUNNING);

xen/arch/x86/hvm/vmx/vpmu_core2.c static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content) · line 591

Quoted 5 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -1
@@ -557,7 +588,7 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         {
         case MSR_TYPE_ARCH_CTRL:      /* MSR_P6_EVNTSEL[0,...] */
             mask = ~((1ull << 32) - 1);
-            if (msr_content & mask)
+            if ( msr_content & mask )

It would be helpful not to mix large functional changes and code cleanup. While I appreciate the cleanup, could it be moved to a different patch? The same comment applies to several other patches as well.

~Andrew

Quoted 187 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +14 -6
                 inject_gp = 1;
             break;
         case MSR_TYPE_CTRL:           /* IA32_FIXED_CTR_CTRL */
@@ -565,22 +596,27 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
                 break;
             /* 4 bits per counter, currently 3 fixed counters implemented. */
             mask = ~((1ull << (fixed_pmc_cnt * FIXED_CTR_CTRL_BITS)) - 1);
-            if (msr_content & mask)
+            if ( msr_content & mask )
                 inject_gp = 1;
             break;
         case MSR_TYPE_COUNTER:        /* IA32_FIXED_CTR[0-2] */
             mask = ~((1ull << core2_get_bitwidth_fix_count()) - 1);
-            if (msr_content & mask)
+            if ( msr_content & mask )
                 inject_gp = 1;
             break;
         }
-        if (inject_gp)
-            hvm_inject_hw_exception(TRAP_gp_fault, 0);
+        if ( inject_gp )
+            inject_trap(v, TRAP_gp_fault);
         else
             wrmsrl(msr, msr_content);
     }
     else
-        vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+    {
+       if ( has_hvm_container_domain(v->domain) )
+           vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+       else
+           wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+    }
 
     return 1;
 }
@@ -604,7 +640,10 @@ static int core2_vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
             *msr_content = core2_vpmu_cxt->global_status;
             break;
         case MSR_CORE_PERF_GLOBAL_CTRL:
-            vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+            if ( has_hvm_container_domain(v->domain) )
+                vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+            else
+                rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, *msr_content);
             break;
         default:
             rdmsrl(msr, *msr_content);
xen/arch/x86/hvm/vpmu.c +7 -0
index bceffa6..ca0534b 100644
@@ -456,6 +456,13 @@ 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 ( current->arch.vpmu.xenpmu_data == NULL )
+            return -EINVAL;
+        vpmu_lvtpc_update(current->arch.vpmu.xenpmu_data->pmu.l.lapic_lvtpc);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/arch/x86/traps.c +45 -3
index 136821f..57d06c9 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>
 
@@ -877,8 +878,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 */
@@ -894,6 +897,9 @@ void pv_cpuid(struct cpu_user_regs *regs)
     }
 
  out:
+    /* VPMU may decide to modify some of the leaves */
+    vpmu_do_cpuid(regs->eax, &a, &b, &c, &d);
+
     regs->eax = a;
     regs->ebx = b;
     regs->ecx = c;
@@ -1921,6 +1927,7 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
     char io_emul_stub[32];
     void (*io_emul)(struct cpu_user_regs *) __attribute__((__regparm__(1)));
     uint64_t val, msr_content;
+    bool_t vpmu_msr;
 
     if ( !read_descriptor(regs->cs, v, regs,
                           &code_base, &code_limit, &ar,
@@ -2411,6 +2418,7 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         uint32_t eax = regs->eax;
         uint32_t edx = regs->edx;
         msr_content = ((uint64_t)edx << 32) | eax;
+        vpmu_msr = 0;
         switch ( (u32)regs->ecx )
         {
         case MSR_FS_BASE:
@@ -2547,7 +2555,19 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             if ( v->arch.debugreg[7] & DR7_ACTIVE_MASK )
                 wrmsrl(regs->_ecx, msr_content);
             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:
+            if ( boot_cpu_data.x86_vendor == X86_VENDOR_INTEL )
+                vpmu_msr = 1;
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( vpmu_msr || (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) )
+            {
+                if ( !vpmu_do_wrmsr(regs->ecx, msr_content) )
+                    goto invalid;
+                break;
+            }
         default:
             if ( wrmsr_hypervisor_regs(regs->ecx, msr_content) == 1 )
                 break;
@@ -2579,6 +2599,8 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         break;
 
     case 0x32: /* RDMSR */
+        vpmu_msr = 0;
+
         switch ( (u32)regs->ecx )
         {
         case MSR_FS_BASE:
@@ -2649,7 +2671,27 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
                             [regs->_ecx - MSR_AMD64_DR1_ADDRESS_MASK + 1];
             regs->edx = 0;
             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:
+            if ( boot_cpu_data.x86_vendor == X86_VENDOR_INTEL )
+                vpmu_msr = 1;
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( vpmu_msr || (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) )
+            {
+                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/pmu.h +1 -0
index 6da3596..7341de9 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 */
#48 lore raw

Re: [PATCH v7 13/19] x86/VPMU: Add support for PMU register handling on PV guests

Quoted 11 lines · Andrew Cooper
  }
xen/arch/x86/hvm/vmx/vpmu_core2.c +8 -0
+static void inject_trap(struct vcpu *v, unsigned int trapno)
+{
+    if ( has_hvm_container_domain(v->domain) )
+        hvm_inject_hw_exception(trapno, 0);
+    else
+        send_guest_trap(v->domain, v->vcpu_id, trapno);
+}
+
Quoted 2 lines

send_guest_trap() cant be used to send anything other than an nmi or mce, but you appear to be using it to try and send #PFs ?

Yes, this is wrong. I'll need to find right interface.

Quoted 9 lines

Review

Quoted 6 lines
          {
          case MSR_TYPE_ARCH_CTRL:      /* MSR_P6_EVNTSEL[0,...] */
              mask = ~((1ull << 32) - 1);
(quoted diff) +1 -1
-            if (msr_content & mask)
+            if ( msr_content & mask )

It would be helpful not to mix large functional changes and code cleanup. While I appreciate the cleanup, could it be moved to a different patch? The same comment applies to several other patches as well.

I *think* this is the only instance of me doing this. I'll see if there are other places.

-boris

#49 lore raw

PATCH v7 14/19 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  | 134 +++++++++++++++++++++++++++++++++++++++++++++--
 xen/include/public/pmu.h |   7 +++
 2 files changed, 136 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/vpmu.c +129 -5
index ca0534b..ce842e4 100644
@@ -76,7 +76,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(H) guests if PMU interrupt is pending */
+    if ( is_hvm_domain(current->domain) ||
+         !(current->arch.vpmu.xenpmu_data &&
+           (current->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED)) )
+        apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
 }
 
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
@@ -87,7 +92,23 @@ int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         return 0;
 
     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;
 }
 
@@ -99,14 +120,109 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
         return 0;
 
     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;
 }
 
+static struct vcpu *choose_dom0_vcpu(void)
+{
+    struct vcpu *v;
+    unsigned idx = smp_processor_id() % hardware_domain->max_vcpus;
+
+    v = hardware_domain->vcpu[idx];
+
+    /*
+     * If index is not populated search downwards the vcpu array until
+     * a valid vcpu can be found
+     */
+    while ( !v && idx-- )
+        v = hardware_domain->vcpu[idx];
+
+    return v;
+}
+
 int vpmu_do_interrupt(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vpmu_struct *vpmu;
+
+    /* dom0 will handle interrupt for special domains (e.g. idle domain) */
+    if ( v->domain->domain_id >= DOMID_FIRST_RESERVED )
+    {
+        v = choose_dom0_vcpu();
+        if ( !v )
+            return 0;
+    }
+
+    vpmu = vcpu_vpmu(v);
+    if ( !is_hvm_domain(v->domain) )
+    {
+        /* PV(H) 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;
+
+        if ( is_pvh_domain(current->domain) &&
+             !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);
+        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 = (void *)&v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            XLAT_cpu_user_regs(cmp, gregs);
+        }
+        else if ( !is_hardware_domain(current->domain) &&
+                 !is_idle_vcpu(current) )
+        {
+            /* PV(H) 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;
+    }
 
     if ( vpmu->arch_vpmu_ops )
     {
@@ -225,7 +341,9 @@ 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) )
+    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) ||
+         (!is_hvm_domain(v->domain) &&
+          vpmu->xenpmu_data->pmu_flags & PMU_CACHED) )
         return;
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_load )
@@ -463,6 +581,12 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
         vpmu_lvtpc_update(current->arch.vpmu.xenpmu_data->pmu.l.lapic_lvtpc);
         ret = 0;
         break;
+    case XENPMU_flush:
+        current->arch.vpmu.xenpmu_data->pmu_flags &= ~PMU_CACHED;
+        vpmu_lvtpc_update(current->arch.vpmu.xenpmu_data->pmu.l.lapic_lvtpc);
+        vpmu_load(current);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/include/public/pmu.h +7 -0
index 7341de9..67ce5de 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 */
@@ -65,6 +66,12 @@ DEFINE_XEN_GUEST_HANDLE(xen_pmu_params_t);
  */
 #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
#50 lore raw

Re: [PATCH v7 14/19] x86/VPMU: Handle PMU interrupts for PV guests

Quoted 21 lines · 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  | 134 +++++++++++++++++++++++++++++++++++++++++++++--
 xen/include/public/pmu.h |   7 +++
 2 files changed, 136 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/vpmu.c +0 -0
index ca0534b..ce842e4 100644

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +6 -1
@@ -76,7 +76,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(H) guests if PMU interrupt is pending */
+    if ( is_hvm_domain(current->domain) ||
+         !(current->arch.vpmu.xenpmu_data &&
+           (current->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED)) )
+        apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);

Repeatedly referencing current is expensive (as it is an assembly block which frobs the stack pointer). Please use "struct vcpu *curr = current;".

Although these accesses look as if they should be through the vpmu pointer anyway.

Quoted 39 lines
xen/arch/x86/hvm/vpmu.c +23 -2
 }
 
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
@@ -87,7 +92,23 @@ int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         return 0;
 
     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;
 }
 
@@ -99,14 +120,109 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
         return 0;
 
     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) )
+        {

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +7 -0
+            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;
 }
 
+static struct vcpu *choose_dom0_vcpu(void)

Probably want to rename this function in line with the s/dom0/hardware_domain/ which has happened in the tree since you first posted a series.

Quoted 11 lines
xen/arch/x86/hvm/vpmu.c +11 -0
+{
+    struct vcpu *v;
+    unsigned idx = smp_processor_id() % hardware_domain->max_vcpus;
+
+    v = hardware_domain->vcpu[idx];
+
+    /*
+     * If index is not populated search downwards the vcpu array until
+     * a valid vcpu can be found
+     */
+    while ( !v && idx-- )

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +6 -1
+        v = hardware_domain->vcpu[idx];
+
+    return v;
+}
+
 int vpmu_do_interrupt(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vpmu_struct *vpmu;

Perhaps an ASSERT(system_state == SYS_STATE_active) to sanity check the environment before we start assuming that dom0s vcpus are constructed?

~Andrew

Quoted 119 lines
xen/arch/x86/hvm/vpmu.c +76 -1
+
+    /* dom0 will handle interrupt for special domains (e.g. idle domain) */
+    if ( v->domain->domain_id >= DOMID_FIRST_RESERVED )
+    {
+        v = choose_dom0_vcpu();
+        if ( !v )
+            return 0;
+    }
+
+    vpmu = vcpu_vpmu(v);
+    if ( !is_hvm_domain(v->domain) )
+    {
+        /* PV(H) 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;
+
+        if ( is_pvh_domain(current->domain) &&
+             !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);
+        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 = (void *)&v->arch.vpmu.xenpmu_data->pmu.r.regs;
+            XLAT_cpu_user_regs(cmp, gregs);
+        }
+        else if ( !is_hardware_domain(current->domain) &&
+                 !is_idle_vcpu(current) )
+        {
+            /* PV(H) 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;
+    }
 
     if ( vpmu->arch_vpmu_ops )
     {
@@ -225,7 +341,9 @@ 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) )
+    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) ||
+         (!is_hvm_domain(v->domain) &&
+          vpmu->xenpmu_data->pmu_flags & PMU_CACHED) )
         return;
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_load )
@@ -463,6 +581,12 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
         vpmu_lvtpc_update(current->arch.vpmu.xenpmu_data->pmu.l.lapic_lvtpc);
         ret = 0;
         break;
+    case XENPMU_flush:
+        current->arch.vpmu.xenpmu_data->pmu_flags &= ~PMU_CACHED;
+        vpmu_lvtpc_update(current->arch.vpmu.xenpmu_data->pmu.l.lapic_lvtpc);
+        vpmu_load(current);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/include/public/pmu.h +7 -0
index 7341de9..67ce5de 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 */
@@ -65,6 +66,12 @@ DEFINE_XEN_GUEST_HANDLE(xen_pmu_params_t);
  */
 #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;
#51 lore raw

Re: [PATCH v7 14/19] x86/VPMU: Handle PMU interrupts for PV guests

Quoted 9 lines · Andrew Cooper
xen/arch/x86/hvm/vpmu.c +2 -1
+
  int vpmu_do_interrupt(struct cpu_user_regs *regs)
  {
      struct vcpu *v = current;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vpmu_struct *vpmu;

Perhaps an ASSERT(system_state == SYS_STATE_active) to sanity check the environment before we start assuming that dom0s vcpus are constructed?

I think this may not work --- if we are being suspended we first change state to SYS_STATE_suspend and then start pausing domains. We may get a PMU interrupt between these two.

-boris

#52 lore raw

PATCH v7 15/19 x86/VPMU: Merge vpmu_rdmsr and vpmu_wrmsr

In reply to Boris Ostrovsky

The two routines share most of their logic.

---
 xen/arch/x86/hvm/svm/svm.c     |  9 ++++++---
 xen/arch/x86/hvm/vmx/vmx.c     | 11 +++++++----
 xen/arch/x86/hvm/vpmu.c        | 42 +++++++++++++++---------------------------
 xen/arch/x86/traps.c           |  4 ++--
 xen/include/asm-x86/hvm/vpmu.h |  6 ++++--
 5 files changed, 34 insertions(+), 38 deletions(-)
xen/arch/x86/hvm/svm/svm.c +6 -3
index 7358626..46161e7 100644
@@ -1634,7 +1634,7 @@ static int svm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
     case MSR_AMD_FAM15H_EVNTSEL3:
     case MSR_AMD_FAM15H_EVNTSEL4:
     case MSR_AMD_FAM15H_EVNTSEL5:
-        vpmu_do_rdmsr(msr, msr_content);
+        vpmu_do_msr(msr, msr_content, VPMU_MSR_READ);
         break;
 
     case MSR_AMD64_DR0_ADDRESS_MASK:
@@ -1785,9 +1785,12 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
     case MSR_AMD_FAM15H_EVNTSEL3:
     case MSR_AMD_FAM15H_EVNTSEL4:
     case MSR_AMD_FAM15H_EVNTSEL5:
-        vpmu_do_wrmsr(msr, msr_content);
-        break;
+    {
+        uint64_t msr_val = msr_content;
 
+        vpmu_do_msr(msr, &msr_val, VPMU_MSR_WRITE);
+        break;
+    }
     case MSR_IA32_MCx_MISC(4): /* Threshold register */
     case MSR_F10_MC4_MISC1 ... MSR_F10_MC4_MISC3:
         /*
xen/arch/x86/hvm/vmx/vmx.c +7 -4
index 5313112..b620b98 100644
@@ -2070,11 +2070,11 @@ static int vmx_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
         *msr_content |= MSR_IA32_MISC_ENABLE_BTS_UNAVAIL |
                        MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL;
         /* Perhaps vpmu will change some bits. */
-        if ( vpmu_do_rdmsr(msr, msr_content) )
+        if ( vpmu_do_msr(msr, msr_content, VPMU_MSR_READ) )
             goto done;
         break;
     default:
-        if ( vpmu_do_rdmsr(msr, msr_content) )
+        if ( vpmu_do_msr(msr, msr_content, VPMU_MSR_READ) )
             break;
         if ( passive_domain_do_rdmsr(msr, msr_content) )
             goto done;
@@ -2225,6 +2225,7 @@ void vmx_vlapic_msr_changed(struct vcpu *v)
 static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
 {
     struct vcpu *v = current;
+    uint64_t msr_val;
 
     HVM_DBG_LOG(DBG_LEVEL_1, "ecx=%#x, msr_value=%#"PRIx64, msr, msr_content);
 
@@ -2248,7 +2249,8 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
         if ( msr_content & ~supported )
         {
             /* Perhaps some other bits are supported in vpmu. */
-            if ( !vpmu_do_wrmsr(msr, msr_content) )
+            msr_val = msr_content;
+            if ( !vpmu_do_msr(msr, &msr_val, VPMU_MSR_WRITE) )
                 break;
         }
         if ( msr_content & IA32_DEBUGCTLMSR_LBR )
@@ -2279,7 +2281,8 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
             goto gp_fault;
         break;
     default:
-        if ( vpmu_do_wrmsr(msr, msr_content) )
+        msr_val = msr_content;
+        if ( vpmu_do_msr(msr, &msr_val, VPMU_MSR_WRITE) )
             return X86EMUL_OKAY;
         if ( passive_domain_do_wrmsr(msr, msr_content) )
             return X86EMUL_OKAY;
xen/arch/x86/hvm/vpmu.c +15 -27

66 lines not on this page · raw message

xen/arch/x86/traps.c +2 -2
index 57d06c9..85b5b15 100644
@@ -2564,7 +2564,7 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
             if ( vpmu_msr || (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) )
             {
-                if ( !vpmu_do_wrmsr(regs->ecx, msr_content) )
+                if ( !vpmu_do_msr(regs->ecx, &msr_content, VPMU_MSR_WRITE) )
                     goto invalid;
                 break;
             }
@@ -2684,7 +2684,7 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
             if ( vpmu_msr || (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) )
             {
-                if ( vpmu_do_rdmsr(regs->ecx, &msr_content) )
+                if ( vpmu_do_msr(regs->ecx, &msr_content, VPMU_MSR_READ) )
                 {
                     regs->eax = (uint32_t)msr_content;
                     regs->edx = (uint32_t)(msr_content >> 32);
xen/include/asm-x86/hvm/vpmu.h +4 -2
index 24fb39b..0cfde19 100644
@@ -78,9 +78,11 @@ struct vpmu_struct {
 #define vpmu_are_all_set(_vpmu, _x) (((_vpmu)->flags & (_x)) == (_x))
 #define vpmu_clear(_vpmu)           ((_vpmu)->flags = 0)
 
+#define VPMU_MSR_READ  0
+#define VPMU_MSR_WRITE 1
+
 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_msr(unsigned int msr, uint64_t *msr_content, uint8_t rw);
 int vpmu_do_interrupt(struct cpu_user_regs *regs);
 void vpmu_do_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                                        unsigned int *ecx, unsigned int *edx);
Signature · 1 line
1.8.1.4
#53 lore raw

PATCH v7 16/19 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  | 99 +++++++++++++++++++++++++++++++++++-------------
 xen/arch/x86/traps.c     | 10 +++++
 xen/include/public/pmu.h |  3 ++
 3 files changed, 86 insertions(+), 26 deletions(-)
xen/arch/x86/hvm/vpmu.c +73 -26
index 4452ed6..8f98182 100644
@@ -88,7 +88,9 @@ int vpmu_do_msr(unsigned int msr, uint64_t *msr_content, uint8_t rw)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
-    if ( !(vpmu_mode & XENPMU_MODE_ON) )
+    if ( (vpmu_mode == XENPMU_MODE_OFF) ||
+         ((vpmu_mode & XENPMU_MODE_PRIV) &&
+          !is_hardware_domain(current->domain)) )
         return 0;
 
     ASSERT((rw == VPMU_MSR_READ) || (rw == VPMU_MSR_WRITE));
@@ -145,8 +147,12 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
     struct vcpu *v = current;
     struct vpmu_struct *vpmu;
 
-    /* dom0 will handle interrupt for special domains (e.g. idle domain) */
-    if ( v->domain->domain_id >= DOMID_FIRST_RESERVED )
+    /*
+     * dom0 will handle interrupt for special domains (e.g. idle domain) or,
+     * in XENPMU_MODE_PRIV, for everyone.
+     */
+    if ( (vpmu_mode & XENPMU_MODE_PRIV) ||
+         (v->domain->domain_id >= DOMID_FIRST_RESERVED) )
     {
         v = choose_dom0_vcpu();
         if ( !v )
@@ -154,7 +160,7 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
     }
 
     vpmu = vcpu_vpmu(v);
-    if ( !is_hvm_domain(v->domain) )
+    if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
     {
         /* PV(H) guest or dom0 is doing system profiling */
         const struct cpu_user_regs *gregs;
@@ -163,7 +169,8 @@ int vpmu_do_interrupt(struct cpu_user_regs *regs)
         if ( v->arch.vpmu.xenpmu_data->pmu_flags & PMU_CACHED )
             return 1;
 
-        if ( is_pvh_domain(current->domain) &&
+        if ( !(vpmu_mode & XENPMU_MODE_PRIV) &&
+             is_pvh_domain(current->domain) &&
              !vpmu->arch_vpmu_ops->do_interrupt(regs) )
             return 0;
 
@@ -172,32 +179,67 @@ 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;
+            /* 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();
+                gregs = guest_cpu_user_regs();
 
-            cmp = (void *)&v->arch.vpmu.xenpmu_data->pmu.r.regs;
-            XLAT_cpu_user_regs(cmp, gregs);
+                cmp = (void *)&v->arch.vpmu.xenpmu_data->pmu.r.regs;
+                XLAT_cpu_user_regs(cmp, gregs);
+
+                /* Adjust RPL for kernel mode */
+                if ((cmp->cs & 3) == 1)
+                    cmp->cs &= ~3;
+            }
+            else
+            {
+                if ( !is_hardware_domain(current->domain) &&
+                     !is_idle_vcpu(current) )
+                {
+                    /* 64-bit unprivileged PV(H) 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));
+
+                if ( !is_pvh_domain(current->domain) )
+                {
+                    if ( current->arch.flags & TF_kernel_mode )
+                        v->arch.vpmu.xenpmu_data->pmu.r.regs.cs &= ~3;
+                }
+                else
+                {
+                    struct segment_register seg_cs;
+
+                    hvm_get_segment_register(current, x86_seg_cs, &seg_cs);
+                    v->arch.vpmu.xenpmu_data->pmu.r.regs.cs = seg_cs.sel;
+                }
+            }
         }
-        else if ( !is_hardware_domain(current->domain) &&
-                 !is_idle_vcpu(current) )
+        else
         {
-            /* PV(H) guest */
+            /* HVM guest */
+            struct segment_register seg_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, &seg_cs);
+            v->arch.vpmu.xenpmu_data->pmu.r.regs.cs = seg_cs.sel;
         }
-        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;
@@ -505,15 +547,20 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
         if ( copy_from_guest(&pmu_params, arg, 1) )
             return -EFAULT;
 
-        if ( pmu_params.d.val & ~XENPMU_MODE_ON )
+        if ( (pmu_params.d.val & ~(XENPMU_MODE_ON | XENPMU_MODE_PRIV)) ||
+             ((pmu_params.d.val & XENPMU_MODE_ON) &&
+              (pmu_params.d.val & XENPMU_MODE_PRIV)) )
             return -EINVAL;
 
         vpmu_mode = pmu_params.d.val;
-        if ( vpmu_mode == XENPMU_MODE_OFF )
+
+        if ( (vpmu_mode == XENPMU_MODE_OFF) || (vpmu_mode & XENPMU_MODE_PRIV) )
             /*
              * After this VPMU context will never be loaded during context
-             * switch. We also prevent PMU MSR accesses (which can load
-             * context) when VPMU is disabled.
+             * switch. Because PMU MSR accesses load VPMU context we don't
+             * allow them when VPMU is off and, for non-provileged domains,
+             * when we are in privileged mode. (We do want these accesses to
+             * load VPMU context for control domain in this mode)
              */
             vpmu_unload_all();
 
xen/arch/x86/traps.c +10 -0
index 85b5b15..8206662 100644
@@ -2564,6 +2564,10 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
             if ( vpmu_msr || (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) )
             {
+                if ( (vpmu_mode & XENPMU_MODE_PRIV) &&
+                     !is_hardware_domain(v->domain) )
+                    break;
+
                 if ( !vpmu_do_msr(regs->ecx, &msr_content, VPMU_MSR_WRITE) )
                     goto invalid;
                 break;
@@ -2690,6 +2694,12 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
                     regs->edx = (uint32_t)(msr_content >> 32);
                     break;
                 }
+                else if ( !is_hardware_domain(v->domain) )
+                {
+                    /* Don't leak PMU MSRs to unprivileged domains */
+                    regs->eax = regs->edx = 0;
+                    break;
+                }
                 goto rdmsr_normal;
             }
         default:
xen/include/public/pmu.h +3 -0
index 67ce5de..c170a1e 100644
@@ -56,9 +56,12 @@ 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_MODE_OFF           0
 #define XENPMU_MODE_ON            (1<<0)
+#define XENPMU_MODE_PRIV          (1<<1)
 
 /*
  * PMU features:
Signature · 1 line
1.8.1.4
#54 lore raw

PATCH v7 17/19 x86/VPMU: Save VPMU state for PV guests during context switch

In reply to Boris Ostrovsky

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

---
 xen/arch/x86/domain.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
xen/arch/x86/domain.c +5 -7
index bb759dd..e9e5335 100644
@@ -1478,16 +1478,14 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
     }
 
     if ( prev != next )
-        _update_runstate_area(prev);
-
-    if ( is_hvm_vcpu(prev) )
     {
-        if ( (prev != next) && (vpmu_mode & XENPMU_MODE_ON) )
+        _update_runstate_area(prev);
+        if ( vpmu_mode & XENPMU_MODE_ON )
             vpmu_save(prev);
+    }
 
-        if ( !list_empty(&prev->arch.hvm_vcpu.tm_list) )
+    if ( is_hvm_vcpu(prev) &&  !list_empty(&prev->arch.hvm_vcpu.tm_list) )
             pt_save_timer(prev);
-    }
 
     local_irq_disable();
 
@@ -1526,7 +1524,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
                            !is_hardware_domain(next->domain));
     }
 
-    if ( is_hvm_vcpu(next) && (prev != next) && (vpmu_mode & XENPMU_MODE_ON) )
+    if ( (prev != next) && (vpmu_mode & XENPMU_MODE_ON) )
         /* Must be done with interrupts enabled */
         vpmu_load(next);
 
Signature · 1 line
1.8.1.4
#55 lore raw

PATCH v7 18/19 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()
* For HVM guests it's VLAPIC accesses and hvm_get_segment_register() (the later can be called in privileged profiling mode when the interrupted guest is an HVM one).

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 only access PMU MSRs with {rd,wr}msrl() (not the _safe versions which would not be NMI-safe).

---
 xen/arch/x86/hvm/svm/vpmu.c       |   1 +
 xen/arch/x86/hvm/vmx/vpmu_core2.c |   1 +
 xen/arch/x86/hvm/vpmu.c           | 188 +++++++++++++++++++++++++++++++-------
 xen/include/xen/softirq.h         |   1 +
 4 files changed, 158 insertions(+), 33 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +1 -0
index 96833ea..06dfc01 100644
@@ -220,6 +220,7 @@ static inline void context_save(struct vcpu *v)
         rdmsrl(counters[i], counter_regs[i]);
 }
 
+/* Must be NMI-safe */
 static int amd_vpmu_save(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -0
index 3b56d3d..ac4998b 100644
@@ -303,6 +303,7 @@ static inline void __core2_vpmu_save(struct vcpu *v)
         rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, core2_vpmu_cxt->global_status);
 }
 
+/* Must be NMI-safe */
 static int core2_vpmu_save(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
xen/arch/x86/hvm/vpmu.c +155 -33

289 lines not on this page · raw message

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
#56 lore raw

PATCH v7 19/19 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           | 512 ------------------
 xen/arch/x86/hvm/vlapic.c             |   2 +-
 xen/arch/x86/hvm/vmx/Makefile         |   1 -
 xen/arch/x86/hvm/vmx/vpmu_core2.c     | 954 ----------------------------------
 xen/arch/x86/hvm/vpmu.c               | 750 --------------------------
 xen/arch/x86/oprofile/op_model_ppro.c |   2 +-
 xen/arch/x86/traps.c                  |   2 +-
 xen/arch/x86/vpmu.c                   | 750 ++++++++++++++++++++++++++
 xen/arch/x86/vpmu_amd.c               | 512 ++++++++++++++++++
 xen/arch/x86/vpmu_intel.c             | 954 ++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/vmx/vmcs.h    |   2 +-
 xen/include/asm-x86/hvm/vpmu.h        | 102 ----
 xen/include/asm-x86/vpmu.h            | 102 ++++
 16 files changed, 2323 insertions(+), 2325 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 -512

512 lines not on this page · raw message

xen/arch/x86/hvm/vlapic.c +1 -1
index f1b543c..b3fe4d3 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 -954

954 lines not on this page · raw message

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

750 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 8206662..ced38ad 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 +750 -0

739 lines not on this page · raw message

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

512 lines not on this page · raw message

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

954 lines not on this page · raw message

xen/include/asm-x86/hvm/vmx/vmcs.h +1 -1
index dd34b2c..a9823fa 100644

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

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

102 lines not on this page · raw message

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

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

#57 lore raw

Re: [PATCH v7 19/19] x86/VPMU: Move VPMU files up from hvm/ directory

Quoted 9 lines · 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
Quoted 4 lines

Use -M please, to vastly reduce the size of this patch.

~Andrew