Skip to contentSkip to conversation

x86/PMU: Xen PMU PV(H) support

PATCH Superseded Boris Ostrovsky 4 participants 39 messages Feb 16, 2015 – Feb 20, 2015

v18 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

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

Changes in v18:

* Return 1 (i.e. "handled") in vpmu_do_interrupt() if PMU_CACHED is set. This is needed since we can get an interrupt while this flag is set on AMD processors when multiple counters are in use (**) (AMD processor don't mask LVTPC when PMC interrupt happens and so there is a window in vpmu_do_interrupt() until it sets the mask bit). Patch #14
* Unload both current and last_vcpu (if different) vpmu and clear this_cpu(last_vcpu) in vpmu_unload_all. Patch #5
* Make major version check for certain xenpmu_ops. Patch #5
* Make xenpmu_op()'s first argument unsigned. Patch #5
* Don't use format specifier for __stringify(). Patch #6
* Don't print generic error in vpmu_init(). Patch #6
* Don't test for VPMU existance in vpmu_initialise(). New patch #15
* Added vpmu_disabled flag to make sure VPMU doesn't get reenabled from dom0 (for example when watchdog is active). Patch #5
* Updated tags on some patches to better reflect latest reviewed status)

(**) While testing this I discovered that AMD VPMU is quite broken for
HVM: when multiple counters are in use linux dom0 often gets unexpected NMIs. This may have something to do with what I mentioned in the first bullet. However, this doesn't appear to be related to this patch series (or earlier VPMU patches) --- I can reproduce this all the way back to 4.1

Changes in v17:
* Disable VPMU when unknown CPU vendor is detected (patch #2)
* Remove unnecessary vendor tests in vendor-specific init routines (patch #14)
* Remember first CPU that starts mode change and use it to stop the cycle (patch #13)
* If vpmu ops is not present, return 0 as value for VPMU MSR read (as opposed to returning an error as was the case in previous patch.) (patch #18)
* Change slightly vpmu_do_msr() logic as result of this chage (patch #20)
* stringify VPMU version (patch #14)
* Use 'CS > 1' to mark sample as PMU_SAMPLE_USER (patch #19)

Changes in v16:

* Many changes in VPMU mode patch (#13):
* Replaced arguments to some vpmu routines (vcpu -> vpmu). New patch (#12)
* Added vpmu_unload vpmu op to completely unload vpmu data (e.g clear MSR bitmaps). This routine may be called in context switch (vpmu_switch_to()).
* Added vmx_write_guest_msr_vcpu() interface to write MSRs of non-current VCPU
* Use cpumask_cycle instead of cpumask_next
* Dropped timeout error
* Adjusted types of mode variables
* Don't allow oprofile to allocate its context on MSR access if VPMU context has already been allocated (which may happen when VMPU mode was set to off while the guest was running)
* vpmu_initialise() no longer turns off VPMU globally on failure. New patch (#2)
* vpmu_do_msr() will return 1 (failure) if vpmu_ops are not set. This is done to prevent PV guests that are not VPMU-enabled from wrongly assuming that they have access to counters (Linux check_hw_exists() will make this assumption) (patch #18)
* Add cpl field to shared structure that will be passed for HVM guests' samples (instead of PMU_SAMPLE_USER flag). Add PMU_SAMPLE_PV flag to mark whose sample is passed up. (Patches ## 10, 19, 22)

Changes in v15:

* Rewrote vpmu_force_context_switch() to use continue_hypercall_on_cpu()
* Added vpmu_init initcall that will call vendor-specific init routines
* Added a lock to vpmu_struct to serialize pmu_init()/pmu_finish()
* Use SS instead of CS for DPL (instead of RPL)
* Don't take lock for XENPMU_mode_get
* Make vmpu_mode/features an unsigned int (from uint64_t)
* Adjusted pvh_hypercall64_table[] order
* Replaced address range check [XEN_VIRT_START..XEN_VIRT_END] with guest_mode()
* A few style cleanups

Changes in v14:

* Moved struct xen_pmu_regs to pmu.h
* Moved CHECK_pmu_* to an earlier patch (when structures are first introduced)
* Added PMU_SAMPLE_REAL flag to indicate whether the sample was taken in real mode
* Simplified slightly setting rules for xenpmu_data flags
* Rewrote vpmu_force_context_switch() to again use continuations. (Returning EAGAIN to user would mean that VPMU mode may get into inconsistent state (across processors) and dealing with that is more compicated than I'd like).
* Fixed msraddr_to_bitpos() and converted it into an inline
* Replaced address range check in vmpu_do_interrupt() with guest_mode()
* No error returns from __initcall
* Rebased on top of recent VPMU changes
* Various cleanups

Changes in v13:

* Rearranged data in xenpf_symdata to eliminate a hole (no change in structure size)
* Removed unnecessary zeroing of last character in name string during symbol read hypercall
* Updated comment in access_vectors for pmu_use operation
* Compute AMD MSR bank size at runtime
* Moved a couple of BUILD_BUG_ON later, to when the structures they are checking are first used
* Added ss and eflags to xen_pmu_registers structure
* vpmu_force_context_switch() uses per-cpu tasklet pointers.
* Moved most of arch-specific VPMU initialization code into an __initcall() to avoid re-calculating/re-checking things more than once (new patch, #12)
* Replaced is_*_domain() with is_*_vcpu()
* choose_hwdom_vcpu() will now assume that hardware_domain->vcpu[idx] is always there (callers will still verify whether or not that's true)
* Fixed a couple of sampled vs. sampling tests in vpmu_do_interrupt()
* Pass CS to the guest unchanged, add pmu_flags's flag to indicate whether the sample was for a user or kernel space. Move pmu_flags from xen_pmu_data into xen_pmu_arch
* Removed local msr_content variable from vpmu_do_wrmsr()
* Re-arranged code in parse_vpmu_param()
* Made vpmu_interrupt_type checks test for value, not individual bits
* Moved PMU_SOFTIRQ definition into arch-specific header
* Moved vpmu*.c files into xen/arch/x86/cpu/ instead of xen/arch/x86/
* For hypervisor samples, report DOMID_XEN to the guest
* Changed logic to select which registers to report to the guest (include RIP check to see whether we are in the hypervisor)

Changes in v12:

* Added XSM support
* Made a valifity check before writing MSR_CORE_PERF_GLOBAL_OVF_CTRL
* Updated documentation for 'vpmu=nmi' option
* Added more text to a bunch of commit messages (per Konrad's request)

Changes in v11:

* Replaced cpu_user_regs with new xen_pmu_regs (IP, SP, CS) in xen_pmu_arch.
- as part of this re-work noticed that CS registers were set in later patch then needed. Moved those changes to appropriate place
* Added new VPMU mode (XENPMU_MODE_HV). Now XENPMU_MODE_SELF will only provide dom0 with its own samples only (i.e. no hypervisor data) and XENPMU_MODE_HV will be what XENPMU_MODE_SELF used to be.
* Kept vmx_add_guest_msr()/vmx_add_host_load_msr() as wrappers around vmx_add_msr()
* Cleaned up VPMU context switch macros (moved 'if(prev!=next)' back to context_switch())
* Dropped hypercall continuation from vpmu_force_context_switch() and replaced it with -EAGAIN error if hypercall_preempt_check() is true after 2ms.
* Kept vpmu_do_rdmsr()/vpmu_do_wrmsr as wrapperd for vpmu_do_msr()
* Move context switching patch (#13) earlier in the series (for proper bisection support)
* Various comment updates and cleanups
* Dropped a bunch of Reviewed-by and all Tested-by tags

Changes in v10:

* Swapped address and name fields of xenpf_symdata (to make it smaller on 32-bit)
* Dropped vmx_rm_guest_msr() as it requires refcountig which makes code more complicated.
* Cleaned up vlapic_reg_write()
* Call vpmu_destroy() for both HVM and PVH VCPUs
* Verify that (xen_pmu_data+PMU register bank) fit into a page
* Return error codes from arch-specific VPMU init code
* Moved VPMU-related context switch logic into inlines
* vpmu_force_context_switch() changes:
o Avoid greater than page-sized allocations
o Prevent another VCPU from starting VPMU sync while the first sync is in progress
* Avoid stack leak in do_xenpmu_op()
* Checked validity of Intel VPMU MSR values before they are committed
* Fixed MSR handling in traps.c (avoid potential accesses to Intel MSRs on AMD)
* Fixed VCPU selection in interrupt handler for 32-bit dom0 (sampled => sampling)
* Clarified commit messages (patches 2, 13, 18)
* Various cleanups

Changes in v9:

* Restore VPMU context after context_saved() is called in context_switch(). This is needed because vpmu_load() may end up calling vmx_vmcs_try_enter()->vcpu_pause() and that needs is_running to be correctly set/cleared. (patch 18, dropped review acks)
* Added patch 2 to properly manage VPMU_CONTEXT_LOADED
* Addressed most of Jan's comments.
o Keep track of time in vpmu_force_context_switch() to properly break out of a loop when using hypercall continuations
o Fixed logic in calling vpmu_do_msr() in emulate_privileged_op()
o Cleaned up vpmu_interrupt() wrt vcpu variable names to (hopefully) make it more clear which vcpu we are using
o Cleaned up vpmu_do_wrmsr()
o Did *not* replace sizeof(uint64_t) with sizeof(variable) in amd_vpmu_initialise(): throughout the code registers are declared as uint64_t and if we are to add a new type (e.g. reg_t) this should be done in a separate patch, unrelated to this series.
o Various more minor cleanups and code style fixes

Changes in v8:

* Cleaned up a bit definitions of struct xenpf_symdata and xen_pmu_params
* Added compat checks for vpmu structures
* Converted vpmu flag manipulation macros to inline routines
* Reimplemented vpmu_unload_all() to avoid long loops
* Reworked PMU fault generation and handling (new patch #12)
* Added checks for domain->vcpu[] non-NULLness
* Added more comments, renamed some routines and macros, code style cleanup

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.

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 (16):
common/symbols: Export hypervisor symbols to privileged guest x86/VPMU: Add public xenpmu.h
x86/VPMU: Make vpmu not HVM-specific
x86/VPMU: Replace vcpu with vpmu as argument to some routines x86/VPMU: Interface for setting PMU mode and flags x86/VPMU: Initialize VPMUs with __initcall
x86/VPMU: Initialize PMU for PV(H) guests
x86/VPMU: Save VPMU state for PV guests during context switch x86/VPMU: When handling MSR accesses, leave fault injection to callers 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: NMI-based VPMU support
x86/VPMU: VPMU should not exist when vpmu_initialise() is called x86/VPMU: Move VPMU files up from hvm/ directory

 docs/misc/xen-command-line.markdown                |   8 +-
 tools/flask/policy/policy/modules/xen/xen.te       |   7 +
 xen/arch/x86/cpu/Makefile                          |   1 +
 xen/arch/x86/cpu/vpmu.c                            | 987 +++++++++++++++++++++
 xen/arch/x86/{hvm/svm/vpmu.c => cpu/vpmu_amd.c}    | 275 +++---
 .../x86/{hvm/vmx/vpmu_core2.c => cpu/vpmu_intel.c} | 498 ++++++-----
 xen/arch/x86/domain.c                              |  27 +-
 xen/arch/x86/hvm/Makefile                          |   1 -
 xen/arch/x86/hvm/hvm.c                             |   1 +
 xen/arch/x86/hvm/svm/Makefile                      |   1 -
 xen/arch/x86/hvm/svm/svm.c                         |  10 +-
 xen/arch/x86/hvm/vlapic.c                          |   2 +-
 xen/arch/x86/hvm/vmx/Makefile                      |   1 -
 xen/arch/x86/hvm/vmx/vmcs.c                        |   7 +-
 xen/arch/x86/hvm/vmx/vmx.c                         |  28 +-
 xen/arch/x86/hvm/vpmu.c                            | 316 -------
 xen/arch/x86/oprofile/nmi_int.c                    |   3 +-
 xen/arch/x86/oprofile/op_model_ppro.c              |   8 +-
 xen/arch/x86/platform_hypercall.c                  |  28 +
 xen/arch/x86/traps.c                               |  62 +-
 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/Makefile                               |   3 +-
 xen/include/asm-x86/domain.h                       |   2 +
 xen/include/asm-x86/hvm/vcpu.h                     |   3 -
 xen/include/asm-x86/hvm/vmx/vmcs.h                 |   9 +-
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h           |  32 -
 xen/include/asm-x86/softirq.h                      |   3 +-
 xen/include/asm-x86/{hvm => }/vpmu.h               |  96 +-
 xen/include/public/arch-arm.h                      |   3 +
 xen/include/public/arch-x86/pmu.h                  |  97 ++
 xen/include/public/platform.h                      |  19 +
 xen/include/public/pmu.h                           |  90 ++
 xen/include/public/xen.h                           |   2 +
 xen/include/xen/hypercall.h                        |   4 +
 xen/include/xen/symbols.h                          |   3 +
 xen/include/xlat.lst                               |   6 +
 xen/include/xsm/dummy.h                            |  20 +
 xen/include/xsm/xsm.h                              |   6 +
 xen/xsm/dummy.c                                    |   1 +
 xen/xsm/flask/hooks.c                              |  28 +
 xen/xsm/flask/policy/access_vectors                |   6 +
 44 files changed, 2003 insertions(+), 764 deletions(-)
 create mode 100644 xen/arch/x86/cpu/vpmu.c
 rename xen/arch/x86/{hvm/svm/vpmu.c => cpu/vpmu_amd.c} (65%)
 rename xen/arch/x86/{hvm/vmx/vpmu_core2.c => cpu/vpmu_intel.c} (71%)
 delete mode 100644 xen/arch/x86/hvm/vpmu.c
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 rename xen/include/asm-x86/{hvm => }/vpmu.h (60%)
 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 v18 1/16 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   | 28 +++++++++++++++++++
 xen/common/symbols.c                | 54 +++++++++++++++++++++++++++++++++++++
 xen/include/public/platform.h       | 19 +++++++++++++
 xen/include/xen/symbols.h           |  3 +++
 xen/include/xlat.lst                |  1 +
 xen/xsm/flask/hooks.c               |  4 +++
 xen/xsm/flask/policy/access_vectors |  2 ++
 7 files changed, 111 insertions(+)
xen/arch/x86/platform_hypercall.c +28 -0
index b427852..e018615 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>
@@ -798,6 +799,33 @@ 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]; /* protected by xenpf_lock */
+        XEN_GUEST_HANDLE(char) nameh;
+        uint32_t namelen, copylen;
+
+        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 = strlen(name) + 1;
+
+        if ( namelen > op->u.symdata.namelen )
+            copylen = op->u.symdata.namelen;
+        else
+            copylen = namelen;
+
+        op->u.symdata.namelen = namelen;
+
+        if ( !ret && copy_to_guest(nameh, name, copylen) )
+            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 e4cf65f..2841efe 100644
@@ -585,6 +585,24 @@ struct xenpf_resource_op {
 typedef struct xenpf_resource_op xenpf_resource_op_t;
 DEFINE_XEN_GUEST_HANDLE(xenpf_resource_op_t);
 
+#define XENPF_get_symbol   63
+struct xenpf_symdata {
+    /* IN/OUT variables */
+    uint32_t namelen; /* IN:  size of name buffer                       */
+                      /* OUT: strlen(name) of hypervisor symbol (may be */
+                      /*      larger than what's been copied to guest)  */
+    uint32_t symnum;  /* IN:  Symbol to read                            */
+                      /* OUT: Next available symbol. If same as IN then */
+                      /*      we reached the end                        */
+
+    /* OUT variables */
+    XEN_GUEST_HANDLE(char) name;
+    uint64_t address;
+    char type;
+};
+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*);
@@ -614,6 +632,7 @@ struct xen_platform_op {
         struct xenpf_mem_hotadd        mem_add;
         struct xenpf_core_parking      core_parking;
         struct xenpf_resource_op       resource_op;
+        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 9c9fd9a..906e6fc 100644
@@ -89,6 +89,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
 ?	xenpf_resource_entry		platform.h
xen/xsm/flask/hooks.c +4 -0
index 65094bb..c6431b5 100644
@@ -1411,6 +1411,10 @@ static int flask_platform_op(uint32_t op)
         return avc_current_has_perm(SECINITSID_XEN, SECCLASS_XEN2,
                                     XEN2__RESOURCE_OP, NULL);
 
+    case XENPF_get_symbol:
+        return avc_has_perm(domain_sid(current->domain), SECINITSID_XEN,
+                            SECCLASS_XEN2, XEN2__GET_SYMBOL, NULL);
+
     default:
         printk("flask_platform_op: Unknown op %d\n", op);
         return -EPERM;
xen/xsm/flask/policy/access_vectors +2 -0
index 8f44b9d..3a97577 100644
@@ -84,6 +84,8 @@ class xen2
     resource_op
 # XEN_SYSCTL_psr_cmt_op
     psr_cmt_op
+# XENPF_get_symbol
+    get_symbol
 }
 
 # Classes domain and domain2 consist of operations that a domain performs on
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v18 2/16 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.

While making these updates, also:
* Remove unused vpmu_domain() macro from vpmu.h
* Convert msraddr_to_bitpos() into an inline and make it a little faster by realizing that all Intel's PMU-related MSRs are in the lower MSR range.

---
 xen/arch/x86/hvm/svm/vpmu.c              |  83 +++++++++++----------
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 123 +++++++++++++++++--------------
 xen/arch/x86/hvm/vpmu.c                  |  10 +++
 xen/arch/x86/oprofile/op_model_ppro.c    |   6 +-
 xen/include/Makefile                     |   3 +-
 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        |  91 +++++++++++++++++++++++
 xen/include/public/pmu.h                 |  38 ++++++++++
 xen/include/xlat.lst                     |   4 +
 11 files changed, 275 insertions(+), 134 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 +46 -37
index 64dc167..545962d 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,9 @@ 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
+
 /* PMU Counter MSRs. */
 static const u32 AMD_F10H_COUNTERS[] = {
     MSR_K7_PERFCTR0,
@@ -83,12 +83,14 @@ 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)    do {                                    \
+                                   (vpmu)->priv_context = (void *)-1L; \
+                               } while (0)
+#define msr_bitmap_off(vpmu)   do {                                    \
+                                   (vpmu)->priv_context = NULL;        \
+                               } while (0)
+#define is_msr_bitmap_on(vpmu) ((vpmu)->priv_context != NULL)
 
 static inline int get_pmu_reg_type(u32 addr)
 {
@@ -142,7 +144,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 +151,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 +165,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 +177,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 +201,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 +215,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 +248,7 @@ static int amd_vpmu_save(struct vcpu *v)
     context_save(v);
 
     if ( !vpmu_is_set(vpmu, VPMU_RUNNING) &&
-         has_hvm_container_vcpu(v) && ctx->msr_bitmap_set )
+         has_hvm_container_vcpu(v) && is_msr_bitmap_on(vpmu) )
         amd_vpmu_unset_msr_bitmap(v);
 
     return 1;
@@ -256,7 +259,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 +273,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;
         }
     }
@@ -303,8 +308,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_vcpu(v) &&
-             !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+        if ( has_hvm_container_vcpu(v) && is_msr_bitmap_on(vpmu) )
              amd_vpmu_set_msr_bitmap(v);
     }
 
@@ -313,8 +317,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_vcpu(v) &&
-             ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+        if ( has_hvm_container_vcpu(v) && is_msr_bitmap_on(vpmu) )
              amd_vpmu_unset_msr_bitmap(v);
         release_pmu_ownship(PMU_OWNER_HVM);
     }
@@ -355,7 +358,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;
 
@@ -385,7 +388,8 @@ static int amd_vpmu_initialise(struct vcpu *v)
 	 }
     }
 
-    ctxt = xzalloc(struct amd_vpmu_context);
+    ctxt = xzalloc_bytes(sizeof(*ctxt) +
+                         2 * sizeof(uint64_t) * num_counters);
     if ( !ctxt )
     {
         gdprintk(XENLOG_WARNING, "Insufficient memory for PMU, "
@@ -394,7 +398,11 @@ static int amd_vpmu_initialise(struct vcpu *v)
         return -ENOMEM;
     }
 
+    ctxt->counters = sizeof(*ctxt);
+    ctxt->ctrls = ctxt->counters + sizeof(uint64_t) * num_counters;
+
     vpmu->context = ctxt;
+    vpmu->priv_context = NULL;
     vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
     return 0;
 }
@@ -403,8 +411,7 @@ static void amd_vpmu_destroy(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
-    if ( has_hvm_container_vcpu(v) &&
-         ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+    if ( has_hvm_container_vcpu(v) && is_msr_bitmap_on(vpmu) )
         amd_vpmu_unset_msr_bitmap(v);
 
     xfree(vpmu->context);
@@ -421,7 +428,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;
+    const uint64_t *counter_regs = vpmu_reg_pointer(ctxt, counters);
+    const uint64_t *ctrl_regs = vpmu_reg_pointer(ctxt, ctrls);
     unsigned int i;
 
     printk("    VPMU state: 0x%x ", vpmu->flags);
@@ -451,8 +460,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 +69 -54

292 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +10 -0
index 0e6b6c0..feb5d2f 100644
@@ -32,6 +32,13 @@
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
+#include <public/pmu.h>
+
+#include <compat/pmu.h>
+CHECK_pmu_intel_ctxt;
+CHECK_pmu_amd_ctxt;
+CHECK_pmu_cntr_pair;
+CHECK_pmu_regs;
 
 /*
  * "vpmu" :     vpmu generally enabled
@@ -236,6 +243,9 @@ void vpmu_initialise(struct vcpu *v)
     if ( is_pvh_vcpu(v) )
         return;
 
+    BUILD_BUG_ON(sizeof(struct xen_pmu_intel_ctxt) > XENPMU_CTXT_PAD_SZ);
+    BUILD_BUG_ON(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 aa99e4d..ca429a1 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/Makefile +2 -1
index 94112d1..60dfbc3 100644
@@ -22,10 +22,11 @@ headers-y := \
     compat/version.h \
     compat/xen.h \
     compat/xenoprof.h
+headers-$(CONFIG_X86)     += compat/arch-x86/pmu.h
 headers-$(CONFIG_X86)     += compat/arch-x86/xen-mca.h
 headers-$(CONFIG_X86)     += compat/arch-x86/xen.h
 headers-$(CONFIG_X86)     += compat/arch-x86/xen-$(compat-arch-y).h
-headers-y                 += compat/arch-$(compat-arch-y).h compat/xlat.h
+headers-y                 += compat/arch-$(compat-arch-y).h compat/pmu.h compat/xlat.h
 headers-$(FLASK_ENABLE)   += compat/xsm/flask_op.h
 
 cppflags-y                := -include public/xen-compat.h
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 9c4e65a..83eea7e 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 {
@@ -65,7 +67,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;
 };
 
@@ -77,11 +80,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 */
-
-
 static inline void vpmu_set(struct vpmu_struct *vpmu, const u32 mask)
 {
     vpmu->flags |= mask;
xen/include/public/arch-arm.h +3 -0
index c2dcb66..f900925 100644
@@ -420,6 +420,9 @@ typedef uint64_t xen_callback_t;
 
 #endif
 
+/* Stub definition of PMU structure */
+typedef struct xen_pmu_arch {} xen_pmu_arch_t;
+
 #endif /*  __XEN_PUBLIC_ARCH_ARM_H__ */
 
 /*
xen/include/public/arch-x86/pmu.h new file +91 -0

91 lines not on this page · raw message

xen/include/public/pmu.h new file +38 -0
new file mode 100644
index 0000000..f97106d
@@ -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 pad;
+
+    xen_pmu_arch_t pmu;
+};
+
+#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:
+ */
xen/include/xlat.lst +4 -0
index 906e6fc..4c9ef97 100644
@@ -105,6 +105,10 @@
 !	vcpu_set_singleshot_timer	vcpu.h
 ?	xenoprof_init			xenoprof.h
 ?	xenoprof_passive		xenoprof.h
+?	pmu_amd_ctxt			arch-x86/pmu.h
+?	pmu_cntr_pair			arch-x86/pmu.h
+?	pmu_intel_ctxt			arch-x86/pmu.h
+?	pmu_regs			arch-x86/pmu.h
 ?	flask_access			xsm/flask_op.h
 !	flask_boolean			xsm/flask_op.h
 ?	flask_cache_stats		xsm/flask_op.h
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH v18 3/16 x86/VPMU: Make vpmu not HVM-specific

In reply to Boris Ostrovsky

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

---
 xen/include/asm-x86/domain.h   | 2 ++
 xen/include/asm-x86/hvm/vcpu.h | 3 ---
 xen/include/asm-x86/hvm/vpmu.h | 5 ++---
 3 files changed, 4 insertions(+), 6 deletions(-)
xen/include/asm-x86/domain.h +2 -0
index 00a8606..b6a369d 100644
@@ -431,6 +431,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 3d8f4dc..0faf60d 100644
@@ -151,9 +151,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 83eea7e..82bfa0e 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
#4 lore raw

PATCH v18 4/16 x86/VPMU: Replace vcpu with vpmu as argument to some routines

In reply to Boris Ostrovsky

A subsequent patch will add an inline routine to vpmu.h that will call vpmu_load(). This inline will try to access vcpu->vpmu which is not possible since struct vcpu may not be fully defined at that point. So we will have that inline pass vpmu pointer to vpmu_load() instead.

This change slightly simplifies some of vpmu code.

For symmetry also modify vpmu_save() (and vpmu_save_force()) to use vpmu instead of vcpu.

---
 xen/arch/x86/domain.c             |  4 ++--
 xen/arch/x86/hvm/svm/vpmu.c       | 23 +++++++++++------------
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 24 ++++++++++++------------
 xen/arch/x86/hvm/vpmu.c           | 31 +++++++++++++------------------
 xen/include/asm-x86/hvm/vpmu.h    | 26 +++++++++++++-------------
 5 files changed, 51 insertions(+), 57 deletions(-)
xen/arch/x86/domain.c +2 -2
index cfe7945..eb8ac3a 100644
@@ -1536,7 +1536,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
     if ( is_hvm_vcpu(prev) )
     {
         if (prev != next)
-            vpmu_save(prev);
+            vpmu_save(vcpu_vpmu(prev));
 
         if ( !list_empty(&prev->arch.hvm_vcpu.tm_list) )
             pt_save_timer(prev);
@@ -1581,7 +1581,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
 
     if (is_hvm_vcpu(next) && (prev != next) )
         /* Must be done with interrupts enabled */
-        vpmu_load(next);
+        vpmu_load(vcpu_vpmu(next));
 
     context_saved(prev);
 
xen/arch/x86/hvm/svm/vpmu.c +11 -12
index 545962d..72e2561 100644
@@ -173,10 +173,9 @@ static int amd_vpmu_do_interrupt(struct cpu_user_regs *regs)
     return 1;
 }
 
-static inline void context_load(struct vcpu *v)
+static inline void context_load(struct vpmu_struct *vpmu)
 {
     unsigned int i;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
     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);
@@ -188,9 +187,8 @@ static inline void context_load(struct vcpu *v)
     }
 }
 
-static void amd_vpmu_load(struct vcpu *v)
+static void amd_vpmu_load(struct vpmu_struct *vpmu)
 {
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
     struct xen_pmu_amd_ctxt *ctxt = vpmu->context;
     uint64_t *ctrl_regs = vpmu_reg_pointer(ctxt, ctrls);
 
@@ -208,13 +206,12 @@ static void amd_vpmu_load(struct vcpu *v)
 
     vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
 
-    context_load(v);
+    context_load(vpmu);
 }
 
-static inline void context_save(struct vcpu *v)
+static inline void context_save(struct vpmu_struct *vpmu)
 {
     unsigned int i;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
     struct xen_pmu_amd_ctxt *ctxt = vpmu->context;
     uint64_t *counter_regs = vpmu_reg_pointer(ctxt, counters);
 
@@ -223,9 +220,9 @@ static inline void context_save(struct vcpu *v)
         rdmsrl(counters[i], counter_regs[i]);
 }
 
-static int amd_vpmu_save(struct vcpu *v)
+static int amd_vpmu_save(struct vpmu_struct *vpmu)
 {
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vcpu *v;
     unsigned int i;
 
     /*
@@ -245,7 +242,9 @@ static int amd_vpmu_save(struct vcpu *v)
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
         return 0;
 
-    context_save(v);
+    context_save(vpmu);
+
+    v = vpmu_vcpu(vpmu);
 
     if ( !vpmu_is_set(vpmu, VPMU_RUNNING) &&
          has_hvm_container_vcpu(v) && is_msr_bitmap_on(vpmu) )
@@ -325,7 +324,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED)
         || vpmu_is_set(vpmu, VPMU_FROZEN) )
     {
-        context_load(v);
+        context_load(vpmu);
         vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
         vpmu_reset(vpmu, VPMU_FROZEN);
     }
@@ -346,7 +345,7 @@ static int amd_vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED)
         || vpmu_is_set(vpmu, VPMU_FROZEN) )
     {
-        context_load(v);
+        context_load(vpmu);
         vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
         vpmu_reset(vpmu, VPMU_FROZEN);
     }
xen/arch/x86/hvm/vmx/vpmu_core2.c +12 -12
index c2405bf..ad7c058 100644
@@ -287,10 +287,10 @@ static void core2_vpmu_unset_msr_bitmap(unsigned long *msr_bitmap)
     set_bit(msraddr_to_bitpos(MSR_IA32_DS_AREA), msr_bitmap);
 }
 
-static inline void __core2_vpmu_save(struct vcpu *v)
+static inline void __core2_vpmu_save(struct vpmu_struct *vpmu)
 {
     int i;
-    struct xen_pmu_intel_ctxt *core2_vpmu_cxt = vcpu_vpmu(v)->context;
+    struct xen_pmu_intel_ctxt *core2_vpmu_cxt = vpmu->context;
     uint64_t *fixed_counters = vpmu_reg_pointer(core2_vpmu_cxt, fixed_counters);
     struct xen_pmu_cntr_pair *xen_pmu_cntr_pair =
         vpmu_reg_pointer(core2_vpmu_cxt, arch_counters);
@@ -301,14 +301,16 @@ static inline void __core2_vpmu_save(struct vcpu *v)
         rdmsrl(MSR_IA32_PERFCTR0 + i, xen_pmu_cntr_pair[i].counter);
 }
 
-static int core2_vpmu_save(struct vcpu *v)
+static int core2_vpmu_save(struct vpmu_struct *vpmu)
 {
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vcpu *v;
 
     if ( !vpmu_are_all_set(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED) )
         return 0;
 
-    __core2_vpmu_save(v);
+    __core2_vpmu_save(vpmu);
+
+    v = vpmu_vcpu(vpmu);
 
     /* Unset PMU MSR bitmap to trap lazy load. */
     if ( !vpmu_is_set(vpmu, VPMU_RUNNING) &&
@@ -318,10 +320,10 @@ static int core2_vpmu_save(struct vcpu *v)
     return 1;
 }
 
-static inline void __core2_vpmu_load(struct vcpu *v)
+static inline void __core2_vpmu_load(struct vpmu_struct *vpmu)
 {
     unsigned int i, pmc_start;
-    struct xen_pmu_intel_ctxt *core2_vpmu_cxt = vcpu_vpmu(v)->context;
+    struct xen_pmu_intel_ctxt *core2_vpmu_cxt = vpmu->context;
     uint64_t *fixed_counters = vpmu_reg_pointer(core2_vpmu_cxt, fixed_counters);
     struct xen_pmu_cntr_pair *xen_pmu_cntr_pair =
         vpmu_reg_pointer(core2_vpmu_cxt, arch_counters);
@@ -344,16 +346,14 @@ static inline void __core2_vpmu_load(struct vcpu *v)
     wrmsrl(MSR_IA32_PEBS_ENABLE, core2_vpmu_cxt->pebs_enable);
 }
 
-static void core2_vpmu_load(struct vcpu *v)
+static void core2_vpmu_load(struct vpmu_struct *vpmu)
 {
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
-
     if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
         return;
 
     vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
 
-    __core2_vpmu_load(v);
+    __core2_vpmu_load(vpmu);
 }
 
 static int core2_vpmu_alloc_resource(struct vcpu *v)
@@ -418,7 +418,7 @@ static int core2_vpmu_msr_common_check(u32 msr_index, int *type, int *index)
     /* Do the lazy load staff. */
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
     {
-        __core2_vpmu_load(current);
+        __core2_vpmu_load(vpmu);
         vpmu_set(vpmu, VPMU_CONTEXT_LOADED);
         if ( has_hvm_container_vcpu(current) &&
              cpu_has_vmx_msr_bitmap )
xen/arch/x86/hvm/vpmu.c +13 -18
index feb5d2f..3045e91 100644
@@ -143,8 +143,7 @@ void vpmu_do_cpuid(unsigned int input,
 
 static void vpmu_save_force(void *arg)
 {
-    struct vcpu *v = (struct vcpu *)arg;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vpmu_struct *vpmu = (struct vpmu_struct *)arg;
 
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
         return;
@@ -152,36 +151,34 @@ static void vpmu_save_force(void *arg)
     vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
 
     if ( vpmu->arch_vpmu_ops )
-        (void)vpmu->arch_vpmu_ops->arch_vpmu_save(v);
+        vpmu->arch_vpmu_ops->arch_vpmu_save(vpmu);
 
     vpmu_reset(vpmu, VPMU_CONTEXT_SAVE);
 
     per_cpu(last_vcpu, smp_processor_id()) = NULL;
 }
 
-void vpmu_save(struct vcpu *v)
+void vpmu_save(struct vpmu_struct *vpmu)
 {
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
     int pcpu = smp_processor_id();
 
     if ( !vpmu_are_all_set(vpmu, VPMU_CONTEXT_ALLOCATED | VPMU_CONTEXT_LOADED) )
        return;
 
     vpmu->last_pcpu = pcpu;
-    per_cpu(last_vcpu, pcpu) = v;
+    per_cpu(last_vcpu, pcpu) = vpmu_vcpu(vpmu);
 
     if ( vpmu->arch_vpmu_ops )
-        if ( vpmu->arch_vpmu_ops->arch_vpmu_save(v) )
+        if ( vpmu->arch_vpmu_ops->arch_vpmu_save(vpmu) )
             vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
 
     apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
 }
 
-void vpmu_load(struct vcpu *v)
+void vpmu_load(struct vpmu_struct *vpmu)
 {
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
     int pcpu = smp_processor_id();
-    struct vcpu *prev = NULL;
+    struct vcpu *prev;
 
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
@@ -199,7 +196,7 @@ void vpmu_load(struct vcpu *v)
         if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
         {
             on_selected_cpus(cpumask_of(vpmu->last_pcpu),
-                             vpmu_save_force, (void *)v, 1);
+                             vpmu_save_force, vpmu, 1);
             vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
         }
     } 
@@ -209,15 +206,13 @@ void vpmu_load(struct vcpu *v)
 
     prev = per_cpu(last_vcpu, pcpu);
 
-    if ( prev != v && prev )
+    if ( (prev != vpmu_vcpu(vpmu)) && prev )
     {
-        vpmu = vcpu_vpmu(prev);
+        struct vpmu_struct *vpmu_prev = vcpu_vpmu(prev);
 
         /* Someone ran here before us */
-        vpmu_save_force(prev);
-        vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
-
-        vpmu = vcpu_vpmu(v);
+        vpmu_save_force(vpmu_prev);
+        vpmu_reset(vpmu_prev, VPMU_CONTEXT_LOADED);
     }
 
     local_irq_enable();
@@ -230,7 +225,7 @@ void vpmu_load(struct vcpu *v)
     {
         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->arch_vpmu_ops->arch_vpmu_load(vpmu);
     }
 }
 
xen/include/asm-x86/hvm/vpmu.h +13 -13
index 82bfa0e..897d5de 100644
@@ -44,6 +44,15 @@
 #define vpmu_reg_pointer(ctxt, offset) ((void *)((uintptr_t)ctxt + \
                                                  (uintptr_t)ctxt->offset))
 
+struct vpmu_struct {
+    u32 flags;
+    u32 last_pcpu;
+    u32 hw_lapic_lvtpc;
+    void *context;      /* May be shared with PV guest */
+    void *priv_context; /* hypervisor-only */
+    struct arch_vpmu_ops *arch_vpmu_ops;
+};
+
 /* Arch specific operations shared by all vpmus */
 struct arch_vpmu_ops {
     int (*do_wrmsr)(unsigned int msr, uint64_t msr_content,
@@ -54,23 +63,14 @@ struct arch_vpmu_ops {
                      unsigned int *eax, unsigned int *ebx,
                      unsigned int *ecx, unsigned int *edx);
     void (*arch_vpmu_destroy)(struct vcpu *v);
-    int (*arch_vpmu_save)(struct vcpu *v);
-    void (*arch_vpmu_load)(struct vcpu *v);
+    int (*arch_vpmu_save)(struct vpmu_struct *vpmu);
+    void (*arch_vpmu_load)(struct vpmu_struct *vpmu);
     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);
 
-struct vpmu_struct {
-    u32 flags;
-    u32 last_pcpu;
-    u32 hw_lapic_lvtpc;
-    void *context;      /* May be shared with PV guest */
-    void *priv_context; /* hypervisor-only */
-    struct arch_vpmu_ops *arch_vpmu_ops;
-};
-
 /* VPMU states */
 #define VPMU_CONTEXT_ALLOCATED              0x1
 #define VPMU_CONTEXT_LOADED                 0x2
@@ -109,8 +109,8 @@ void vpmu_do_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                                        unsigned int *ecx, unsigned int *edx);
 void vpmu_initialise(struct vcpu *v);
 void vpmu_destroy(struct vcpu *v);
-void vpmu_save(struct vcpu *v);
-void vpmu_load(struct vcpu *v);
+void vpmu_save(struct vpmu_struct *vpmu);
+void vpmu_load(struct vpmu_struct *vpmu);
 void vpmu_dump(struct vcpu *v);
 
 extern int acquire_pmu_ownership(int pmu_ownership);
Signature · 1 line
1.8.1.4
#5 lore raw

PATCH v18 5/16 x86/VPMU: Interface for setting PMU mode and flags

In reply to Boris Ostrovsky

Add runtime interface for setting PMU mode and flags. Three main modes are provided:
* XENPMU_MODE_OFF: PMU is not virtualized
* XENPMU_MODE_SELF: Guests can access PMU MSRs and receive PMU interrupts.
* XENPMU_MODE_HV: Same as XENPMU_MODE_SELF for non-proviledged guests, dom0 can profile itself and the hypervisor.

Note that PMU modes are different from what can be provided at Xen's boot line with 'vpmu' argument. An 'off' (or '0') value is equivalent to XENPMU_MODE_OFF. Any other value, on the other hand, will cause VPMU mode to be set to XENPMU_MODE_SELF during boot.

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

Mode and flags are set via HYPERVISOR_xenpmu_op hypercall.

---
 tools/flask/policy/policy/modules/xen/xen.te |   3 +
 xen/arch/x86/domain.c                        |   6 +-
 xen/arch/x86/hvm/svm/vpmu.c                  |  25 ++-
 xen/arch/x86/hvm/vmx/vmcs.c                  |   7 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c            |  27 ++-
 xen/arch/x86/hvm/vpmu.c                      | 240 +++++++++++++++++++++++++--
 xen/arch/x86/oprofile/nmi_int.c              |   3 +-
 xen/arch/x86/x86_64/compat/entry.S           |   4 +
 xen/arch/x86/x86_64/entry.S                  |   4 +
 xen/include/asm-x86/hvm/vmx/vmcs.h           |   7 +-
 xen/include/asm-x86/hvm/vpmu.h               |  33 +++-
 xen/include/public/pmu.h                     |  45 +++++
 xen/include/public/xen.h                     |   1 +
 xen/include/xen/hypercall.h                  |   4 +
 xen/include/xlat.lst                         |   1 +
 xen/include/xsm/dummy.h                      |  15 ++
 xen/include/xsm/xsm.h                        |   6 +
 xen/xsm/dummy.c                              |   1 +
 xen/xsm/flask/hooks.c                        |  18 ++
 xen/xsm/flask/policy/access_vectors          |   2 +
 20 files changed, 417 insertions(+), 35 deletions(-)
tools/flask/policy/policy/modules/xen/xen.te +3 -0
index c0128aa..870ff81 100644
@@ -68,6 +68,9 @@ allow dom0_t xen_t:xen2 {
     resource_op
     psr_cmt_op
 };
+allow dom0_t xen_t:xen2 {
+    pmu_ctrl
+};
 allow dom0_t xen_t:mmu memorymap;
 
 # Allow dom0 to use these domctls on itself. For domctls acting on other
xen/arch/x86/domain.c +3 -3
index eb8ac3a..b0e3c3d 100644
@@ -1536,7 +1536,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
     if ( is_hvm_vcpu(prev) )
     {
         if (prev != next)
-            vpmu_save(vcpu_vpmu(prev));
+            vpmu_switch_from(vcpu_vpmu(prev), vcpu_vpmu(next));
 
         if ( !list_empty(&prev->arch.hvm_vcpu.tm_list) )
             pt_save_timer(prev);
@@ -1579,9 +1579,9 @@ 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) )
         /* Must be done with interrupts enabled */
-        vpmu_load(vcpu_vpmu(next));
+        vpmu_switch_to(vcpu_vpmu(prev), vcpu_vpmu(next));
 
     context_saved(prev);
 
xen/arch/x86/hvm/svm/vpmu.c +23 -2
index 72e2561..2cfdf08 100644
@@ -253,6 +253,26 @@ static int amd_vpmu_save(struct vpmu_struct *vpmu)
     return 1;
 }
 
+static void amd_vpmu_unload(struct vpmu_struct *vpmu)
+{
+    struct vcpu *v;
+
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED | VPMU_FROZEN) )
+    {
+        unsigned int i;
+
+        for ( i = 0; i < num_counters; i++ )
+            wrmsrl(ctrls[i], 0);
+        context_save(vpmu);
+    }
+
+    v = vpmu_vcpu(vpmu);
+    if ( has_hvm_container_vcpu(v) && is_msr_bitmap_on(vpmu) )
+        amd_vpmu_unset_msr_bitmap(v);
+
+    vpmu_reset(vpmu, VPMU_FROZEN);
+}
+
 static void context_update(unsigned int msr, u64 msr_content)
 {
     unsigned int i;
@@ -471,17 +491,18 @@ struct arch_vpmu_ops amd_vpmu_ops = {
     .arch_vpmu_destroy = amd_vpmu_destroy,
     .arch_vpmu_save = amd_vpmu_save,
     .arch_vpmu_load = amd_vpmu_load,
+    .arch_vpmu_unload = amd_vpmu_unload,
     .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/vmcs.c +3 -4
index d614638..0183222 100644
@@ -1183,11 +1183,10 @@ int vmx_read_guest_msr(u32 msr, u64 *val)
     return -ESRCH;
 }
 
-int vmx_write_guest_msr(u32 msr, u64 val)
+int vmx_write_guest_msr_vcpu(struct vcpu *v, u32 msr, u64 val)
 {
-    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 i, msr_count = v->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = v->arch.hvm_vmx.msr_area;
 
     for ( i = 0; i < msr_count; i++ )
     {
xen/arch/x86/hvm/vmx/vpmu_core2.c +22 -5
index ad7c058..4d08d1b 100644
@@ -320,6 +320,22 @@ static int core2_vpmu_save(struct vpmu_struct *vpmu)
     return 1;
 }
 
+static void core2_vpmu_unload(struct vpmu_struct *vpmu)
+{
+    struct vcpu *v = vpmu_vcpu(vpmu);
+
+    if ( !has_hvm_container_vcpu(v) )
+        wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+    else
+        vmx_write_guest_msr_vcpu(v, MSR_CORE_PERF_GLOBAL_CTRL, 0);
+
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
+        __core2_vpmu_save(vpmu);
+
+    if ( has_hvm_container_vcpu(v) && cpu_has_vmx_msr_bitmap )
+        core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
+}
+
 static inline void __core2_vpmu_load(struct vpmu_struct *vpmu)
 {
     unsigned int i, pmc_start;
@@ -708,13 +724,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;
     static bool_t ds_warned;
 
-    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] */
     while ( boot_cpu_has(X86_FEATURE_DS) )
@@ -784,6 +800,7 @@ struct arch_vpmu_ops core2_vpmu_ops = {
     .arch_vpmu_destroy = core2_vpmu_destroy,
     .arch_vpmu_save = core2_vpmu_save,
     .arch_vpmu_load = core2_vpmu_load,
+    .arch_vpmu_unload = core2_vpmu_unload,
     .arch_vpmu_dump = core2_vpmu_dump
 };
 
@@ -826,7 +843,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;
@@ -834,7 +851,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 )
@@ -877,7 +894,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 +230 -10

316 lines not on this page · raw message

xen/arch/x86/oprofile/nmi_int.c +2 -1
index 13534d4..3c3a37c 100644
@@ -47,7 +47,8 @@ static int passive_domain_msr_op_checks(unsigned int msr, int *typep, int *index
 	if ( !model->is_arch_pmu_msr(msr, typep, indexp) )
 		return 0;
 
-	if ( !vpmu_is_set(vpmu, VPMU_PASSIVE_DOMAIN_ALLOCATED) )
+	if ( !vpmu_is_set(vpmu, VPMU_PASSIVE_DOMAIN_ALLOCATED |
+                               VPMU_CONTEXT_ALLOCATED) )
 		if ( ! model->allocated_msr(current) )
 			return 0;
 	return 1;
xen/arch/x86/x86_64/compat/entry.S +4 -0
index 5b0af61..7691a79 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
@@ -466,6 +468,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 b3d6e32..aa842ac 100644
@@ -772,6 +772,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
@@ -821,6 +823,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/vmx/vmcs.h +6 -1
index 6fce6aa..f05e2b5 100644
@@ -463,7 +463,7 @@ extern const unsigned int vmx_introspection_force_enabled_msrs_size;
 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_write_guest_msr_vcpu(struct vcpu *v, u32 msr, u64 val);
 int vmx_add_msr(u32 msr, int type);
 void vmx_vmcs_switch(struct vmcs_struct *from, struct vmcs_struct *to);
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector);
@@ -483,6 +483,11 @@ static inline int vmx_add_host_load_msr(u32 msr)
     return vmx_add_msr(msr, VMX_HOST_MSR);
 }
 
+static inline int vmx_write_guest_msr(u32 msr, u64 val)
+{
+    return vmx_write_guest_msr_vcpu(current, msr, val);
+}
+
 DECLARE_PER_CPU(bool_t, vmxon);
 
 #endif /* ASM_X86_HVM_VMX_VMCS_H__ */
xen/include/asm-x86/hvm/vpmu.h +24 -9
index 897d5de..1171b2a 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)
 
@@ -65,11 +58,12 @@ struct arch_vpmu_ops {
     void (*arch_vpmu_destroy)(struct vcpu *v);
     int (*arch_vpmu_save)(struct vpmu_struct *vpmu);
     void (*arch_vpmu_load)(struct vpmu_struct *vpmu);
+    void (*arch_vpmu_unload)(struct vpmu_struct *vpmu);
     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 *);
 
 /* VPMU states */
 #define VPMU_CONTEXT_ALLOCATED              0x1
@@ -111,10 +105,31 @@ void vpmu_initialise(struct vcpu *v);
 void vpmu_destroy(struct vcpu *v);
 void vpmu_save(struct vpmu_struct *vpmu);
 void vpmu_load(struct vpmu_struct *vpmu);
+void vpmu_unload(struct vpmu_struct *vpmu);
 void vpmu_dump(struct vcpu *v);
 
 extern int acquire_pmu_ownership(int pmu_ownership);
 extern void release_pmu_ownership(int pmu_ownership);
 
+extern unsigned int vpmu_mode;
+extern unsigned int vpmu_features;
+
+/* Context switch */
+static inline void vpmu_switch_from(struct vpmu_struct *prev,
+                                    struct vpmu_struct *next)
+{
+    if ( vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+        vpmu_save(prev);
+}
+
+static inline void vpmu_switch_to(struct vpmu_struct *prev,
+                                  struct vpmu_struct *next)
+{
+    if ( vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+        vpmu_load(next);
+    else if ( vpmu_is_set(next, VPMU_CONTEXT_LOADED | VPMU_RUNNING) )
+        vpmu_unload(next);
+}
+
 #endif /* __ASM_X86_HVM_VPMU_H_*/
 
xen/include/public/pmu.h +45 -0

51 lines not on this page · raw message

xen/include/public/xen.h +1 -0
index 3703c39..0dd3c97 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 eda8a36..ef665db 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>
 
@@ -144,6 +145,9 @@ do_tmem_op(
 extern long
 do_xenoprof_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);
 
+extern long
+do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg);
+
 #ifdef CONFIG_COMPAT
 
 extern int
xen/include/xlat.lst +1 -0
index 4c9ef97..0f972eb 100644
@@ -109,6 +109,7 @@
 ?	pmu_cntr_pair			arch-x86/pmu.h
 ?	pmu_intel_ctxt			arch-x86/pmu.h
 ?	pmu_regs			arch-x86/pmu.h
+?	pmu_params			pmu.h
 ?	flask_access			xsm/flask_op.h
 !	flask_boolean			xsm/flask_op.h
 ?	flask_cache_stats		xsm/flask_op.h
xen/include/xsm/dummy.h +15 -0
index f20e89c..c637454 100644
@@ -655,4 +655,19 @@ static XSM_INLINE int xsm_ioport_mapping(XSM_DEFAULT_ARG struct domain *d, uint3
     return xsm_default_action(action, current->domain, d);
 }
 
+static XSM_INLINE int xsm_pmu_op (XSM_DEFAULT_ARG struct domain *d, int op)
+{
+    XSM_ASSERT_ACTION(XSM_OTHER);
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    case XENPMU_mode_get:
+    case XENPMU_feature_set:
+    case XENPMU_feature_get:
+        return xsm_default_action(XSM_PRIV, d, current->domain);
+    default:
+        return -EPERM;
+    }
+}
+
 #endif /* CONFIG_X86 */
xen/include/xsm/xsm.h +6 -0
index 4ce089f..90edbb1 100644
@@ -173,6 +173,7 @@ struct xsm_operations {
     int (*unbind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
     int (*ioport_permission) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow);
     int (*ioport_mapping) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow);
+    int (*pmu_op) (struct domain *d, unsigned int op);
 #endif
 };
 
@@ -665,6 +666,11 @@ static inline int xsm_ioport_mapping (xsm_default_t def, struct domain *d, uint3
     return xsm_ops->ioport_mapping(d, s, e, allow);
 }
 
+static inline int xsm_pmu_op (xsm_default_t def, struct domain *d, int op)
+{
+    return xsm_ops->pmu_op(d, op);
+}
+
 #endif /* CONFIG_X86 */
 
 #endif /* XSM_NO_WRAPPERS */
xen/xsm/dummy.c +1 -0
index 8eb3050..94f1cf0 100644
@@ -144,5 +144,6 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, unbind_pt_irq);
     set_to_dummy_if_null(ops, ioport_permission);
     set_to_dummy_if_null(ops, ioport_mapping);
+    set_to_dummy_if_null(ops, pmu_op);
 #endif
 }
xen/xsm/flask/hooks.c +18 -0
index c6431b5..982e879 100644
@@ -1505,6 +1505,23 @@ static int flask_unbind_pt_irq (struct domain *d, struct xen_domctl_bind_pt_irq
 {
     return current_has_perm(d, SECCLASS_RESOURCE, RESOURCE__REMOVE);
 }
+
+static int flask_pmu_op (struct domain *d, unsigned int op)
+{
+    u32 dsid = domain_sid(d);
+
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    case XENPMU_mode_get:
+    case XENPMU_feature_set:
+    case XENPMU_feature_get:
+        return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_XEN2,
+                            XEN2__PMU_CTRL, NULL);
+    default:
+        return -EPERM;
+    }
+}
 #endif /* CONFIG_X86 */
 
 long do_flask_op(XEN_GUEST_HANDLE_PARAM(xsm_op_t) u_flask_op);
@@ -1627,6 +1644,7 @@ static struct xsm_operations flask_ops = {
     .unbind_pt_irq = flask_unbind_pt_irq,
     .ioport_permission = flask_ioport_permission,
     .ioport_mapping = flask_ioport_mapping,
+    .pmu_op = flask_pmu_op,
 #endif
 };
 
xen/xsm/flask/policy/access_vectors +2 -0
index 3a97577..626850d 100644
@@ -86,6 +86,8 @@ class xen2
     psr_cmt_op
 # XENPF_get_symbol
     get_symbol
+# PMU control
+    pmu_ctrl
 }
 
 # Classes domain and domain2 consist of operations that a domain performs on
Signature · 1 line
1.8.1.4
#6 lore raw

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

Am Montag 16 Februar 2015, 17:26:48 schrieb Boris Ostrovsky:

Quoted 157 lines

Add runtime interface for setting PMU mode and flags. Three main modes are provided:
* XENPMU_MODE_OFF: PMU is not virtualized
* XENPMU_MODE_SELF: Guests can access PMU MSRs and receive PMU interrupts.
* XENPMU_MODE_HV: Same as XENPMU_MODE_SELF for non-proviledged guests, dom0 can profile itself and the hypervisor.

Note that PMU modes are different from what can be provided at Xen's boot line with 'vpmu' argument. An 'off' (or '0') value is equivalent to XENPMU_MODE_OFF. Any other value, on the other hand, will cause VPMU mode to be set to XENPMU_MODE_SELF during boot.

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

Mode and flags are set via HYPERVISOR_xenpmu_op hypercall.

---
 tools/flask/policy/policy/modules/xen/xen.te |   3 +
 xen/arch/x86/domain.c                        |   6 +-
 xen/arch/x86/hvm/svm/vpmu.c                  |  25 ++-
 xen/arch/x86/hvm/vmx/vmcs.c                  |   7 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c            |  27 ++-
 xen/arch/x86/hvm/vpmu.c                      | 240 +++++++++++++++++++++++++--
 xen/arch/x86/oprofile/nmi_int.c              |   3 +-
 xen/arch/x86/x86_64/compat/entry.S           |   4 +
 xen/arch/x86/x86_64/entry.S                  |   4 +
 xen/include/asm-x86/hvm/vmx/vmcs.h           |   7 +-
 xen/include/asm-x86/hvm/vpmu.h               |  33 +++-
 xen/include/public/pmu.h                     |  45 +++++
 xen/include/public/xen.h                     |   1 +
 xen/include/xen/hypercall.h                  |   4 +
 xen/include/xlat.lst                         |   1 +
 xen/include/xsm/dummy.h                      |  15 ++
 xen/include/xsm/xsm.h                        |   6 +
 xen/xsm/dummy.c                              |   1 +
 xen/xsm/flask/hooks.c                        |  18 ++
 xen/xsm/flask/policy/access_vectors          |   2 +
 20 files changed, 417 insertions(+), 35 deletions(-)
tools/flask/policy/policy/modules/xen/xen.te +3 -0
index c0128aa..870ff81 100644
@@ -68,6 +68,9 @@ allow dom0_t xen_t:xen2 {
     resource_op
     psr_cmt_op
 };
+allow dom0_t xen_t:xen2 {
+    pmu_ctrl
+};
 allow dom0_t xen_t:mmu memorymap;
 
 # Allow dom0 to use these domctls on itself. For domctls acting on other
xen/arch/x86/domain.c +3 -3
index eb8ac3a..b0e3c3d 100644
@@ -1536,7 +1536,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
     if ( is_hvm_vcpu(prev) )
     {
         if (prev != next)
-            vpmu_save(vcpu_vpmu(prev));
+            vpmu_switch_from(vcpu_vpmu(prev), vcpu_vpmu(next));
 
         if ( !list_empty(&prev->arch.hvm_vcpu.tm_list) )
             pt_save_timer(prev);
@@ -1579,9 +1579,9 @@ 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) )
         /* Must be done with interrupts enabled */
-        vpmu_load(vcpu_vpmu(next));
+        vpmu_switch_to(vcpu_vpmu(prev), vcpu_vpmu(next));
 
     context_saved(prev);
 
xen/arch/x86/hvm/svm/vpmu.c +23 -2
index 72e2561..2cfdf08 100644
@@ -253,6 +253,26 @@ static int amd_vpmu_save(struct vpmu_struct *vpmu)
     return 1;
 }
 
+static void amd_vpmu_unload(struct vpmu_struct *vpmu)
+{
+    struct vcpu *v;
+
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED | VPMU_FROZEN) )
+    {
+        unsigned int i;
+
+        for ( i = 0; i < num_counters; i++ )
+            wrmsrl(ctrls[i], 0);
+        context_save(vpmu);
+    }
+
+    v = vpmu_vcpu(vpmu);
+    if ( has_hvm_container_vcpu(v) && is_msr_bitmap_on(vpmu) )
+        amd_vpmu_unset_msr_bitmap(v);
+
+    vpmu_reset(vpmu, VPMU_FROZEN);
+}
+
 static void context_update(unsigned int msr, u64 msr_content)
 {
     unsigned int i;
@@ -471,17 +491,18 @@ struct arch_vpmu_ops amd_vpmu_ops = {
     .arch_vpmu_destroy = amd_vpmu_destroy,
     .arch_vpmu_save = amd_vpmu_save,
     .arch_vpmu_load = amd_vpmu_load,
+    .arch_vpmu_unload = amd_vpmu_unload,
     .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/vmcs.c +3 -4
index d614638..0183222 100644
@@ -1183,11 +1183,10 @@ int vmx_read_guest_msr(u32 msr, u64 *val)
     return -ESRCH;
 }
 
-int vmx_write_guest_msr(u32 msr, u64 val)
+int vmx_write_guest_msr_vcpu(struct vcpu *v, u32 msr, u64 val)
 {
-    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 i, msr_count = v->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = v->arch.hvm_vmx.msr_area;
 
     for ( i = 0; i < msr_count; i++ )
     {
xen/arch/x86/hvm/vmx/vpmu_core2.c +0 -0
index ad7c058..4d08d1b 100644
@@ -320,6 +320,22 @@ static int core2_vpmu_save(struct vpmu_struct *vpmu)
     return 1;

xen/arch/x86/hvm/vmx/vpmu_core2.c static int core2_vpmu_save(struct vpmu_struct *vpmu) · line 330

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +8 -0
 }
 
+static void core2_vpmu_unload(struct vpmu_struct *vpmu)
+{
+    struct vcpu *v = vpmu_vcpu(vpmu);
+
+    if ( !has_hvm_container_vcpu(v) )
+        wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+    else
+        vmx_write_guest_msr_vcpu(v, MSR_CORE_PERF_GLOBAL_CTRL, 0);

For better readability maybe this if clause should be switched to

    if ( has_hvm_container_vcpu(v) )
        vmx_write_guest_msr_vcpu(v, MSR_CORE_PERF_GLOBAL_CTRL, 0);
    else
        wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
Quoted 87 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +14 -5
+
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
+        __core2_vpmu_save(vpmu);
+
+    if ( has_hvm_container_vcpu(v) && cpu_has_vmx_msr_bitmap )
+        core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
+}
+
 static inline void __core2_vpmu_load(struct vpmu_struct *vpmu)
 {
     unsigned int i, pmc_start;
@@ -708,13 +724,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;
     static bool_t ds_warned;
 
-    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] */
     while ( boot_cpu_has(X86_FEATURE_DS) )
@@ -784,6 +800,7 @@ struct arch_vpmu_ops core2_vpmu_ops = {
     .arch_vpmu_destroy = core2_vpmu_destroy,
     .arch_vpmu_save = core2_vpmu_save,
     .arch_vpmu_load = core2_vpmu_load,
+    .arch_vpmu_unload = core2_vpmu_unload,
     .arch_vpmu_dump = core2_vpmu_dump
 };
 
@@ -826,7 +843,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;
@@ -834,7 +851,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 )
@@ -877,7 +894,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 +4 -0
index 3045e91..40a89d9 100644
@@ -21,6 +21,8 @@
 #include <xen/config.h>
 #include <xen/sched.h>
 #include <xen/xenoprof.h>
+#include <xen/event.h>
+#include <xen/guest_access.h>
 #include <asm/regs.h>
 #include <asm/types.h>
 #include <asm/msr.h>
@@ -33,8 +35,10 @@
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
 #include <public/pmu.h>
+#include <xsm/xsm.h>
 
 #include <compat/pmu.h>
+CHECK_pmu_params;
 CHECK_pmu_intel_ctxt;
 CHECK_pmu_amd_ctxt;
 CHECK_pmu_cntr_pair;

xen/arch/x86/hvm/vpmu.c CHECK_pmu_regs; · line 54

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +3 -1
@@ -45,7 +49,9 @@ CHECK_pmu_regs;
  * "vpmu=off" : vpmu generally disabled
  * "vpmu=bts" : vpmu enabled and Intel BTS feature switched on.
  */
-static unsigned int __read_mostly opt_vpmu_enabled;
+unsigned int __read_mostly vpmu_mode = XENPMU_MODE_OFF;
+unsigned int __read_mostly vpmu_features = 0;
+static bool_t __read_mostly vpmu_disabled = 1;

The same readability thing here with vpmu_disabled.
Why not use vpmu_enabled = 0 as the default instead of vpmu_disabled = 1?

Quoted 81 lines
xen/arch/x86/hvm/vpmu.c +19 -7
 static void parse_vpmu_param(char *s);
 custom_param("vpmu", parse_vpmu_param);
 
@@ -59,7 +65,7 @@ static void __init parse_vpmu_param(char *s)
         break;
     default:
         if ( !strcmp(s, "bts") )
-            opt_vpmu_enabled |= VPMU_BOOT_BTS;
+            vpmu_features |= XENPMU_FEATURE_INTEL_BTS;
         else if ( *s )
         {
             printk("VPMU: unknown flag: %s - vpmu disabled!\n", s);
@@ -67,7 +73,9 @@ static void __init parse_vpmu_param(char *s)
         }
         /* fall through */
     case 1:
-        opt_vpmu_enabled |= VPMU_BOOT_ENABLED;
+        /* Default VPMU mode */
+        vpmu_mode = XENPMU_MODE_SELF;
+        vpmu_disabled = 0;
         break;
     }
 }
@@ -76,7 +84,7 @@ void vpmu_lvtpc_update(uint32_t val)
 {
     struct vpmu_struct *vpmu;
 
-    if ( !opt_vpmu_enabled )
+    if ( vpmu_mode == XENPMU_MODE_OFF )
         return;
 
     vpmu = vcpu_vpmu(current);
@@ -89,6 +97,9 @@ int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, uint64_t supported)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
+    if ( !(vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV)) )
+        return 0;
+
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_wrmsr )
         return vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content, supported);
     return 0;
@@ -98,6 +109,12 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
 
+    if ( !(vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV)) )
+    {
+        *msr_content = 0;
+        return 0;
+    }
+
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_rdmsr )
         return vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
     return 0;
@@ -249,19 +266,20 @@ void vpmu_initialise(struct vcpu *v)
     switch ( vendor )
     {
     case X86_VENDOR_AMD:
-        ret = svm_vpmu_initialise(v, opt_vpmu_enabled);
+        ret = svm_vpmu_initialise(v);
         break;
 
     case X86_VENDOR_INTEL:
-        ret = vmx_vpmu_initialise(v, opt_vpmu_enabled);
+        ret = vmx_vpmu_initialise(v);
         break;
 
     default:
-        if ( opt_vpmu_enabled )
+        if ( vpmu_mode != XENPMU_MODE_OFF )
         {
             printk(XENLOG_G_WARNING "VPMU: Unknown CPU vendor %d. "
                    "Disabling VPMU\n", vendor);
-            opt_vpmu_enabled = 0;
+            vpmu_mode = XENPMU_MODE_OFF;
+            vpmu_disabled = 1;
         }
         return;
     }

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

Quoted 9 lines
xen/arch/x86/hvm/vpmu.c +6 -0
@@ -307,13 +325,215 @@ void vpmu_dump(struct vcpu *v)
         vpmu->arch_vpmu_ops->arch_vpmu_dump(v);
 }
 
+void vpmu_unload(struct vpmu_struct *vpmu)
+{
+    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED | VPMU_RUNNING) )
+        return;
+
+    if (vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_unload)

Coding style?

Dietmar.

Quoted 567 lines
xen/arch/x86/hvm/vpmu.c +198 -2
+        vpmu->arch_vpmu_ops->arch_vpmu_unload(vpmu);
+
+    vpmu_reset(vpmu, VPMU_CONTEXT_LOADED | VPMU_RUNNING);
+}
+
+#define VPMU_INVALID_CPU (~0U)
+static unsigned int vpmu_first_unload_cpu = VPMU_INVALID_CPU;
+static unsigned int vpmu_next_unload_cpu;
+
+static long vpmu_unload_next(void *arg)
+{
+    struct vcpu *last;
+    int ret;
+    unsigned int thiscpu = smp_processor_id();
+
+    if ( thiscpu != vpmu_next_unload_cpu )
+    {
+        /* Continuation thread may have been moved due to CPU hot-unplug */
+        vpmu_mode = (unsigned long)arg;
+        vpmu_first_unload_cpu = VPMU_INVALID_CPU;
+        return -EAGAIN;
+    }
+
+    local_irq_disable(); /* so that last_vcpu doesn't change under us. */
+
+    last = this_cpu(last_vcpu);
+    if ( last )
+    {
+        vpmu_unload(vcpu_vpmu(last));
+        this_cpu(last_vcpu) = NULL;
+    }
+
+    local_irq_enable();
+
+    vpmu_next_unload_cpu = cpumask_cycle(thiscpu, &cpu_online_map);
+    if ( vpmu_next_unload_cpu == vpmu_first_unload_cpu )
+    {
+        /* We have visited everyone. */
+        vpmu_first_unload_cpu = VPMU_INVALID_CPU;
+        return 0;
+    }
+
+    while ( !cpumask_test_cpu(vpmu_first_unload_cpu, &cpu_online_map) )
+    {
+        /* First cpu was hot-unplugged */
+        vpmu_first_unload_cpu = cpumask_cycle(vpmu_first_unload_cpu,
+                                              &cpu_online_map);
+        if ( thiscpu == vpmu_first_unload_cpu )
+        {
+            vpmu_first_unload_cpu = VPMU_INVALID_CPU;
+            return 0;
+        }
+    }
+
+    ret = continue_hypercall_on_cpu(vpmu_next_unload_cpu,
+                                    vpmu_unload_next, arg);
+    if ( ret )
+    {
+        vpmu_mode = (unsigned long)arg;
+        vpmu_first_unload_cpu = VPMU_INVALID_CPU;
+    }
+
+    return ret;
+}
+
+static int vpmu_unload_all(unsigned long old_mode)
+{
+    int ret = 0;
+    struct vcpu *last;
+
+    vpmu_unload(vcpu_vpmu(current));
+
+    local_irq_disable();
+    last = this_cpu(last_vcpu);
+    if ( last && (last != current) )
+    {
+        vpmu_unload(vcpu_vpmu(last));
+        this_cpu(last_vcpu) = NULL;
+    }
+    local_irq_enable();
+
+    if ( cpumask_weight(&cpu_online_map) > 1 )
+    {
+        unsigned int thiscpu = smp_processor_id();
+
+        vpmu_first_unload_cpu = thiscpu;
+        vpmu_next_unload_cpu = cpumask_cycle(thiscpu, &cpu_online_map);
+
+        ret = continue_hypercall_on_cpu(vpmu_next_unload_cpu, vpmu_unload_next,
+                                        (void *)old_mode);
+        if ( ret )
+            vpmu_first_unload_cpu = VPMU_INVALID_CPU;
+    }
+
+    return ret;
+}
+
+long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret;
+    struct xen_pmu_params pmu_params;
+
+    if ( vpmu_disabled )
+        return -EINVAL;
+
+    ret = xsm_pmu_op(XSM_OTHER, current->domain, op);
+    if ( ret )
+        return ret;
+
+    /* Check major version when parameters are specified */
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    case XENPMU_feature_set:
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        if ( pmu_params.version.maj != XENPMU_VER_MAJ )
+            return -EINVAL;
+    }
+
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    {
+        unsigned int old_mode;
+        static DEFINE_SPINLOCK(xenpmu_mode_lock);
+
+        if ( pmu_params.val & ~(XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+            return -EINVAL;
+
+        /* 32-bit dom0 can only sample itself. */
+        if ( is_pv_32bit_vcpu(current) && (pmu_params.val & XENPMU_MODE_HV) )
+            return -EINVAL;
+
+        /*
+         * Return error if someone else is in the middle of changing mode ---
+         * this is most likely indication of two system administrators
+         * working against each other.
+         */
+        if ( !spin_trylock(&xenpmu_mode_lock) )
+            return -EAGAIN;
+        if ( vpmu_first_unload_cpu != VPMU_INVALID_CPU )
+        {
+            spin_unlock(&xenpmu_mode_lock);
+            return -EAGAIN;
+        }
+
+        old_mode = vpmu_mode;
+        vpmu_mode = pmu_params.val;
+
+        if ( vpmu_mode == XENPMU_MODE_OFF )
+        {
+            /* Make sure all (non-dom0) VCPUs have unloaded their VPMUs. */
+            ret = vpmu_unload_all(old_mode);
+            if ( ret )
+                vpmu_mode = old_mode;
+        }
+
+        spin_unlock(&xenpmu_mode_lock);
+
+        break;
+    }
+
+    case XENPMU_mode_get:
+        memset(&pmu_params, 0, sizeof(pmu_params));
+        pmu_params.val = vpmu_mode;
+
+        pmu_params.version.maj = XENPMU_VER_MAJ;
+        pmu_params.version.min = XENPMU_VER_MIN;
+
+        if ( copy_to_guest(arg, &pmu_params, 1) )
+            return -EFAULT;
+        break;
+
+    case XENPMU_feature_set:
+        if ( pmu_params.val & ~XENPMU_FEATURE_INTEL_BTS )
+            return -EINVAL;
+
+        vpmu_features = pmu_params.val;
+        break;
+
+    case XENPMU_feature_get:
+        pmu_params.val = vpmu_features;
+        if ( copy_field_to_guest(arg, &pmu_params, val) )
+            return -EFAULT;
+        break;
+
+    default:
+        ret = -EINVAL;
+    }
+
+    return ret;
+}
+
 static int __init vpmu_init(void)
 {
     /* NMI watchdog uses LVTPC and HW counter */
-    if ( opt_watchdog && opt_vpmu_enabled )
+    if ( opt_watchdog && !vpmu_disabled )
     {
         printk(XENLOG_WARNING "NMI watchdog is enabled. Turning VPMU off.\n");
-        opt_vpmu_enabled = 0;
+        vpmu_mode = XENPMU_MODE_OFF;
+        vpmu_disabled = 1;
     }
 
     return 0;
xen/arch/x86/oprofile/nmi_int.c +2 -1
index 13534d4..3c3a37c 100644
@@ -47,7 +47,8 @@ static int passive_domain_msr_op_checks(unsigned int msr, int *typep, int *index
 	if ( !model->is_arch_pmu_msr(msr, typep, indexp) )
 		return 0;
 
-	if ( !vpmu_is_set(vpmu, VPMU_PASSIVE_DOMAIN_ALLOCATED) )
+	if ( !vpmu_is_set(vpmu, VPMU_PASSIVE_DOMAIN_ALLOCATED |
+                               VPMU_CONTEXT_ALLOCATED) )
 		if ( ! model->allocated_msr(current) )
 			return 0;
 	return 1;
xen/arch/x86/x86_64/compat/entry.S +4 -0
index 5b0af61..7691a79 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
@@ -466,6 +468,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 b3d6e32..aa842ac 100644
@@ -772,6 +772,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
@@ -821,6 +823,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/vmx/vmcs.h +6 -1
index 6fce6aa..f05e2b5 100644
@@ -463,7 +463,7 @@ extern const unsigned int vmx_introspection_force_enabled_msrs_size;
 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_write_guest_msr_vcpu(struct vcpu *v, u32 msr, u64 val);
 int vmx_add_msr(u32 msr, int type);
 void vmx_vmcs_switch(struct vmcs_struct *from, struct vmcs_struct *to);
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector);
@@ -483,6 +483,11 @@ static inline int vmx_add_host_load_msr(u32 msr)
     return vmx_add_msr(msr, VMX_HOST_MSR);
 }
 
+static inline int vmx_write_guest_msr(u32 msr, u64 val)
+{
+    return vmx_write_guest_msr_vcpu(current, msr, val);
+}
+
 DECLARE_PER_CPU(bool_t, vmxon);
 
 #endif /* ASM_X86_HVM_VMX_VMCS_H__ */
xen/include/asm-x86/hvm/vpmu.h +24 -9
index 897d5de..1171b2a 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)
 
@@ -65,11 +58,12 @@ struct arch_vpmu_ops {
     void (*arch_vpmu_destroy)(struct vcpu *v);
     int (*arch_vpmu_save)(struct vpmu_struct *vpmu);
     void (*arch_vpmu_load)(struct vpmu_struct *vpmu);
+    void (*arch_vpmu_unload)(struct vpmu_struct *vpmu);
     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 *);
 
 /* VPMU states */
 #define VPMU_CONTEXT_ALLOCATED              0x1
@@ -111,10 +105,31 @@ void vpmu_initialise(struct vcpu *v);
 void vpmu_destroy(struct vcpu *v);
 void vpmu_save(struct vpmu_struct *vpmu);
 void vpmu_load(struct vpmu_struct *vpmu);
+void vpmu_unload(struct vpmu_struct *vpmu);
 void vpmu_dump(struct vcpu *v);
 
 extern int acquire_pmu_ownership(int pmu_ownership);
 extern void release_pmu_ownership(int pmu_ownership);
 
+extern unsigned int vpmu_mode;
+extern unsigned int vpmu_features;
+
+/* Context switch */
+static inline void vpmu_switch_from(struct vpmu_struct *prev,
+                                    struct vpmu_struct *next)
+{
+    if ( vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+        vpmu_save(prev);
+}
+
+static inline void vpmu_switch_to(struct vpmu_struct *prev,
+                                  struct vpmu_struct *next)
+{
+    if ( vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+        vpmu_load(next);
+    else if ( vpmu_is_set(next, VPMU_CONTEXT_LOADED | VPMU_RUNNING) )
+        vpmu_unload(next);
+}
+
 #endif /* __ASM_X86_HVM_VPMU_H_*/
 
xen/include/public/pmu.h +45 -0
index f97106d..66cc494 100644
@@ -13,6 +13,51 @@
 #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
+/* ` } */
+
+/* Parameters structure for HYPERVISOR_xenpmu_op call */
+struct xen_pmu_params {
+    /* IN/OUT parameters */
+    struct {
+        uint32_t maj;
+        uint32_t min;
+    } version;
+    uint64_t val;
+
+    /* IN parameters */
+    uint32_t vcpu;
+    uint32_t pad;
+};
+typedef struct xen_pmu_params xen_pmu_params_t;
+DEFINE_XEN_GUEST_HANDLE(xen_pmu_params_t);
+
+/* PMU modes:
+ * - XENPMU_MODE_OFF:   No PMU virtualization
+ * - XENPMU_MODE_SELF:  Guests can profile themselves
+ * - XENPMU_MODE_HV:    Guests can profile themselves, dom0 profiles
+ *                      itself and Xen
+ */
+#define XENPMU_MODE_OFF           0
+#define XENPMU_MODE_SELF          (1<<0)
+#define XENPMU_MODE_HV            (1<<1)
+
+/*
+ * PMU features:
+ * - XENPMU_FEATURE_INTEL_BTS: Intel BTS support (ignored on AMD)
+ */
+#define XENPMU_FEATURE_INTEL_BTS  1
 
 /* Shared between hypervisor and PV domain */
 struct xen_pmu_data {
xen/include/public/xen.h +1 -0
index 3703c39..0dd3c97 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 eda8a36..ef665db 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>
 
@@ -144,6 +145,9 @@ do_tmem_op(
 extern long
 do_xenoprof_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);
 
+extern long
+do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg);
+
 #ifdef CONFIG_COMPAT
 
 extern int
xen/include/xlat.lst +1 -0
index 4c9ef97..0f972eb 100644
@@ -109,6 +109,7 @@
 ?	pmu_cntr_pair			arch-x86/pmu.h
 ?	pmu_intel_ctxt			arch-x86/pmu.h
 ?	pmu_regs			arch-x86/pmu.h
+?	pmu_params			pmu.h
 ?	flask_access			xsm/flask_op.h
 !	flask_boolean			xsm/flask_op.h
 ?	flask_cache_stats		xsm/flask_op.h
xen/include/xsm/dummy.h +15 -0
index f20e89c..c637454 100644
@@ -655,4 +655,19 @@ static XSM_INLINE int xsm_ioport_mapping(XSM_DEFAULT_ARG struct domain *d, uint3
     return xsm_default_action(action, current->domain, d);
 }
 
+static XSM_INLINE int xsm_pmu_op (XSM_DEFAULT_ARG struct domain *d, int op)
+{
+    XSM_ASSERT_ACTION(XSM_OTHER);
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    case XENPMU_mode_get:
+    case XENPMU_feature_set:
+    case XENPMU_feature_get:
+        return xsm_default_action(XSM_PRIV, d, current->domain);
+    default:
+        return -EPERM;
+    }
+}
+
 #endif /* CONFIG_X86 */
xen/include/xsm/xsm.h +6 -0
index 4ce089f..90edbb1 100644
@@ -173,6 +173,7 @@ struct xsm_operations {
     int (*unbind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
     int (*ioport_permission) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow);
     int (*ioport_mapping) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow);
+    int (*pmu_op) (struct domain *d, unsigned int op);
 #endif
 };
 
@@ -665,6 +666,11 @@ static inline int xsm_ioport_mapping (xsm_default_t def, struct domain *d, uint3
     return xsm_ops->ioport_mapping(d, s, e, allow);
 }
 
+static inline int xsm_pmu_op (xsm_default_t def, struct domain *d, int op)
+{
+    return xsm_ops->pmu_op(d, op);
+}
+
 #endif /* CONFIG_X86 */
 
 #endif /* XSM_NO_WRAPPERS */
xen/xsm/dummy.c +1 -0
index 8eb3050..94f1cf0 100644
@@ -144,5 +144,6 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, unbind_pt_irq);
     set_to_dummy_if_null(ops, ioport_permission);
     set_to_dummy_if_null(ops, ioport_mapping);
+    set_to_dummy_if_null(ops, pmu_op);
 #endif
 }
xen/xsm/flask/hooks.c +18 -0
index c6431b5..982e879 100644
@@ -1505,6 +1505,23 @@ static int flask_unbind_pt_irq (struct domain *d, struct xen_domctl_bind_pt_irq
 {
     return current_has_perm(d, SECCLASS_RESOURCE, RESOURCE__REMOVE);
 }
+
+static int flask_pmu_op (struct domain *d, unsigned int op)
+{
+    u32 dsid = domain_sid(d);
+
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    case XENPMU_mode_get:
+    case XENPMU_feature_set:
+    case XENPMU_feature_get:
+        return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_XEN2,
+                            XEN2__PMU_CTRL, NULL);
+    default:
+        return -EPERM;
+    }
+}
 #endif /* CONFIG_X86 */
 
 long do_flask_op(XEN_GUEST_HANDLE_PARAM(xsm_op_t) u_flask_op);
@@ -1627,6 +1644,7 @@ static struct xsm_operations flask_ops = {
     .unbind_pt_irq = flask_unbind_pt_irq,
     .ioport_permission = flask_ioport_permission,
     .ioport_mapping = flask_ioport_mapping,
+    .pmu_op = flask_pmu_op,
 #endif
 };
 
xen/xsm/flask/policy/access_vectors +2 -0
index 3a97577..626850d 100644
@@ -86,6 +86,8 @@ class xen2
     psr_cmt_op
 # XENPF_get_symbol
     get_symbol
+# PMU control
+    pmu_ctrl
 }
 
 # Classes domain and domain2 consist of operations that a domain performs on
Signature · 1 line
Company details: http://ts.fujitsu.com/imprint.html
#7 lore raw

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

In reply to Boris Ostrovsky
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vpmu.c +1 -0
@@ -253,6 +253,26 @@ static int amd_vpmu_save(struct vpmu_struct *vpmu)
     return 1;
 }
 
+static void amd_vpmu_unload(struct vpmu_struct *vpmu)

xen/arch/x86/hvm/svm/vpmu.c static int amd_vpmu_save(struct vpmu_struct *vpmu) · line 266

Quoted 10 lines
xen/arch/x86/hvm/svm/vpmu.c +10 -0
+{
+    struct vcpu *v;
+
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED | VPMU_FROZEN) )
+    {
+        unsigned int i;
+
+        for ( i = 0; i < num_counters; i++ )
+            wrmsrl(ctrls[i], 0);
+        context_save(vpmu);

This assumes vpmu_vcpu(vpmu) == current, yet it looks like you're also calling it under different circumstances now.

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmcs.c +0 -1
@@ -1183,11 +1183,10 @@ int vmx_read_guest_msr(u32 msr, u64 *val)
     return -ESRCH;
 }
 
-int vmx_write_guest_msr(u32 msr, u64 val)

xen/arch/x86/hvm/vmx/vmcs.c int vmx_read_guest_msr(u32 msr, u64 *val) · line 1192

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +3 -3
+int vmx_write_guest_msr_vcpu(struct vcpu *v, u32 msr, u64 val)
 {
-    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 i, msr_count = v->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = v->arch.hvm_vmx.msr_area;
 
     for ( i = 0; i < msr_count; i++ )
     {

Same here - while the code itself is only accessing memory, it remains unclear whether correct behavior results when the subject vCPU is actually executing.

In both cases, if there are restrictions on the conditions under which unload can validly be done, I think you should ASSERT() those conditions (at once making them explicit).

xen/arch/x86/hvm/vmx/vmcs.c

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmcs.c +7 -0
+long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret;
+    struct xen_pmu_params pmu_params;
+
+    if ( vpmu_disabled )
+        return -EINVAL;

EOPNOTSUPP perhaps?

And I agree with Dietmar that keeping opt_vpmu_enabled instead of introducing vpmu_disabled would seem more natural, the more that the default is disabled.

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmcs.c +5 -0
+/* Context switch */
+static inline void vpmu_switch_from(struct vpmu_struct *prev,
+                                    struct vpmu_struct *next)
+{
+    if ( vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV) )

xen/arch/x86/hvm/vmx/vmcs.c

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +10 -0
+        vpmu_save(prev);
+}
+
+static inline void vpmu_switch_to(struct vpmu_struct *prev,
+                                  struct vpmu_struct *next)
+{
+    if ( vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+        vpmu_load(next);
+    else if ( vpmu_is_set(next, VPMU_CONTEXT_LOADED | VPMU_RUNNING) )
+        vpmu_unload(next);

I don't recall there having been an unload path here, and I don't see this being explained anywhere either. Wouldn't that more naturally be done in the switch-from function?

Apart from that it's also not clear why both prev and next get passed to both of these functions. Iirc a later patch may make use of that, but then that later patch should add the second function parameter.

xen/include/xlat.lst line 112

Quoted 7 lines
xen/include/xlat.lst +1 -0
@@ -109,6 +109,7 @@
 ?	pmu_cntr_pair			arch-x86/pmu.h
 ?	pmu_intel_ctxt			arch-x86/pmu.h
 ?	pmu_regs			arch-x86/pmu.h
+?	pmu_params			pmu.h

Apparently I overlooked this in earlier patches and revision: The entries in this file are (mostly) alphabetically sorted (by filename, then by structure name). Please don't make the situation worse than it already is.

Jan

#8 lore raw

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

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vpmu.c +0 -0
@@ -253,6 +253,26 @@ static int amd_vpmu_save(struct vpmu_struct *vpmu)
      return 1;
  }
 
Quoted 13 lines

xen/arch/x86/hvm/svm/vpmu.c static int amd_vpmu_save(struct vpmu_struct *vpmu) · line 266

Quoted 11 lines
xen/arch/x86/hvm/svm/vpmu.c +11 -0
+static void amd_vpmu_unload(struct vpmu_struct *vpmu)
+{
+    struct vcpu *v;
+
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED | VPMU_FROZEN) )
+    {
+        unsigned int i;
+
+        for ( i = 0; i < num_counters; i++ )
+            wrmsrl(ctrls[i], 0);
+        context_save(vpmu);

This assumes vpmu_vcpu(vpmu) == current, yet it looks like you're also calling it under different circumstances now.

This doesn't make this assumption. PMU MSRs may be loaded with values that belong to a VCPU that is not currently running if, for example, current VCPU is not using PMU. And flags test guarantees that we won't stomp on someone else's registers.

I could make a test here and write zeroes to the MSRs only if vpmu is current but I don't like leaving these MSRs with what is essentially garbage values. This routine is executed very rarely so overhead is negligible.

Quoted 16 lines
Quoted 16 lines
xen/arch/x86/hvm/vmx/vmcs.c +3 -4
@@ -1183,11 +1183,10 @@ int vmx_read_guest_msr(u32 msr, u64 *val)
      return -ESRCH;
  }
 
-int vmx_write_guest_msr(u32 msr, u64 val)
+int vmx_write_guest_msr_vcpu(struct vcpu *v, u32 msr, u64 val)
  {
-    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 i, msr_count = v->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = v->arch.hvm_vmx.msr_area;
 
Quoted 5 lines

xen/arch/x86/hvm/vmx/vmcs.c int vmx_read_guest_msr(u32 msr, u64 *val) · line 1192

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
      for ( i = 0; i < msr_count; i++ )
      {

Same here - while the code itself is only accessing memory, it remains unclear whether correct behavior results when the subject vCPU is actually executing.

Not sure what you mean here. The changes are specifically for updating MSR values (cached in VMCS) of possibly non-current VCPU.

Quoted 14 lines

In both cases, if there are restrictions on the conditions under which unload can validly be done, I think you should ASSERT() those conditions (at once making them explicit).

xen/arch/x86/hvm/vmx/vmcs.c

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmcs.c +7 -0
+long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret;
+    struct xen_pmu_params pmu_params;
+
+    if ( vpmu_disabled )
+        return -EINVAL;

EOPNOTSUPP perhaps?

Quoted 3 lines

And I agree with Dietmar that keeping opt_vpmu_enabled instead of introducing vpmu_disabled would seem more natural, the more that the default is disabled.

I can switch polarity back to enabled since both of you think it makes more sense but I don't think I should keep "opt_" prefix since this flag can be modified independently of what boot option says (e.g. when watchdog is on).

Quoted 11 lines
Quoted 11 lines
xen/arch/x86/hvm/vmx/vmcs.c +10 -0
+/* Context switch */
+static inline void vpmu_switch_from(struct vpmu_struct *prev,
+                                    struct vpmu_struct *next)
+{
+    if ( vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+        vpmu_save(prev);
+}
+
+static inline void vpmu_switch_to(struct vpmu_struct *prev,
+                                  struct vpmu_struct *next)
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmcs.c

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmcs.c +5 -0
+{
+    if ( vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+        vpmu_load(next);
+    else if ( vpmu_is_set(next, VPMU_CONTEXT_LOADED | VPMU_RUNNING) )
+        vpmu_unload(next);

I don't recall there having been an unload path here, and I don't see this being explained anywhere either. Wouldn't that more naturally be done in the switch-from function?

I had this for a couple of revisions now. And I had a very specific reason for (1) having it and (2) having it here (it had something to do with the fact that 'next' may still be LOADED after we've changed vpmu_mode to, say, OFF) . But I can't remember what it was now. If I remember why I'll add a comment, otherwise I'll drop this.

Quoted 5 lines

Apart from that it's also not clear why both prev and next get passed to both of these functions. Iirc a later patch may make use of that, but then that later patch should add the second function parameter.

Right, I was thinking of using both parameters at some point and left them here. Only one is needed.

-boris

#9 lore raw

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

Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 20.02.15 at 17:04, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vpmu.c +0 -0
@@ -253,6 +253,26 @@ static int amd_vpmu_save(struct vpmu_struct *vpmu)
      return 1;
  }
 
Quoted 13 lines

xen/arch/x86/hvm/svm/vpmu.c static int amd_vpmu_save(struct vpmu_struct *vpmu) · line 266

Quoted 11 lines
xen/arch/x86/hvm/svm/vpmu.c +11 -0
+static void amd_vpmu_unload(struct vpmu_struct *vpmu)
+{
+    struct vcpu *v;
+
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED | VPMU_FROZEN) )
+    {
+        unsigned int i;
+
+        for ( i = 0; i < num_counters; i++ )
+            wrmsrl(ctrls[i], 0);
+        context_save(vpmu);

This assumes vpmu_vcpu(vpmu) == current, yet it looks like you're also calling it under different circumstances now.

This doesn't make this assumption. PMU MSRs may be loaded with values that belong to a VCPU that is not currently running if, for example, current VCPU is not using PMU. And flags test guarantees that we won't stomp on someone else's registers.

Quoted 4 lines

I could make a test here and write zeroes to the MSRs only if vpmu is current but I don't like leaving these MSRs with what is essentially garbage values. This routine is executed very rarely so overhead is negligible.

The question wasn't on the wrmsr() - writing zero here unconditionally is fine - but on the actions context_save() takes.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/hvm/vmx/vmcs.c +3 -4
@@ -1183,11 +1183,10 @@ int vmx_read_guest_msr(u32 msr, u64 *val)
      return -ESRCH;
  }
 
-int vmx_write_guest_msr(u32 msr, u64 val)
+int vmx_write_guest_msr_vcpu(struct vcpu *v, u32 msr, u64 val)
  {
-    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 i, msr_count = v->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = v->arch.hvm_vmx.msr_area;
 
Quoted 8 lines
Quoted 5 lines

xen/arch/x86/hvm/vmx/vmcs.c int vmx_read_guest_msr(u32 msr, u64 *val) · line 1192

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
      for ( i = 0; i < msr_count; i++ )
      {

Same here - while the code itself is only accessing memory, it remains unclear whether correct behavior results when the subject vCPU is actually executing.

Not sure what you mean here. The changes are specifically for updating MSR values (cached in VMCS) of possibly non-current VCPU.

But non-current doesn't mean not-running. I.e. what if the vCPU you deal with here is active on another pCPU?

Quoted 9 lines
Quoted 9 lines

xen/arch/x86/hvm/vmx/vmcs.c

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmcs.c +7 -0
+long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret;
+    struct xen_pmu_params pmu_params;
+
+    if ( vpmu_disabled )
+        return -EINVAL;

EOPNOTSUPP perhaps?

Quoted 8 lines
Quoted 3 lines

And I agree with Dietmar that keeping opt_vpmu_enabled instead of introducing vpmu_disabled would seem more natural, the more that the default is disabled.

I can switch polarity back to enabled since both of you think it makes more sense but I don't think I should keep "opt_" prefix since this flag can be modified independently of what boot option says (e.g. when watchdog is on).

Grep through the sources, and if you don't find other examples doing so (I think you will), I'm fine with you dropping the opt_prefix (in a separate patch).

Jan

#10 lore raw

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

Quoted 27 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 20.02.15 at 17:04, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vpmu.c +0 -0
@@ -253,6 +253,26 @@ static int amd_vpmu_save(struct vpmu_struct *vpmu)
       return 1;
   }
 
Quoted 13 lines

xen/arch/x86/hvm/svm/vpmu.c static int amd_vpmu_save(struct vpmu_struct *vpmu) · line 266

Quoted 11 lines
xen/arch/x86/hvm/svm/vpmu.c +11 -0
+static void amd_vpmu_unload(struct vpmu_struct *vpmu)
+{
+    struct vcpu *v;
+
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED | VPMU_FROZEN) )
+    {
+        unsigned int i;
+
+        for ( i = 0; i < num_counters; i++ )
+            wrmsrl(ctrls[i], 0);
+        context_save(vpmu);

This assumes vpmu_vcpu(vpmu) == current, yet it looks like you're also calling it under different circumstances now.

This doesn't make this assumption. PMU MSRs may be loaded with values that belong to a VCPU that is not currently running if, for example, current VCPU is not using PMU. And flags test guarantees that we won't stomp on someone else's registers.

Quoted 6 lines
Quoted 4 lines

I could make a test here and write zeroes to the MSRs only if vpmu is current but I don't like leaving these MSRs with what is essentially garbage values. This routine is executed very rarely so overhead is negligible.

The question wasn't on the wrmsr() - writing zero here unconditionally is fine - but on the actions context_save() takes.

Same reasoning for context_save() (which is a bunch of RDMSRs) --- if flags indicate that the context is LOADED|FROZEN then contents of those MSRs belong to this vpmu, regardless of whether it is current.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 8 lines
xen/arch/x86/hvm/vmx/vmcs.c +0 -1
@@ -1183,11 +1183,10 @@ int vmx_read_guest_msr(u32 msr, u64 *val)
       return -ESRCH;
   }
 
-int vmx_write_guest_msr(u32 msr, u64 val)

xen/arch/x86/hvm/vmx/vmcs.c int vmx_read_guest_msr(u32 msr, u64 *val) · line 1192

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +3 -3
+int vmx_write_guest_msr_vcpu(struct vcpu *v, u32 msr, u64 val)
   {
-    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 i, msr_count = v->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = v->arch.hvm_vmx.msr_area;
 
       for ( i = 0; i < msr_count; i++ )
       {

Same here - while the code itself is only accessing memory, it remains unclear whether correct behavior results when the subject vCPU is actually executing.

Quoted 4 lines
Quoted 2 lines

Not sure what you mean here. The changes are specifically for updating MSR values (cached in VMCS) of possibly non-current VCPU.

But non-current doesn't mean not-running. I.e. what if the vCPU you deal with here is active on another pCPU?

I see. With VPMU (which is the only place that calls this routine) this will never happen but the interface itself allows it. I'll add an ASSERT or some other test to make sure.

-boris

#11 lore raw

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

In reply to Boris Ostrovsky
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/hvm/vpmu.c +22 -0
+long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret;
+    struct xen_pmu_params pmu_params;
+
+    if ( vpmu_disabled )
+        return -EINVAL;
+
+    ret = xsm_pmu_op(XSM_OTHER, current->domain, op);
+    if ( ret )
+        return ret;
+
+    /* Check major version when parameters are specified */
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    case XENPMU_feature_set:
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        if ( pmu_params.version.maj != XENPMU_VER_MAJ )
+            return -EINVAL;

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +10 -0
+    }
+
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    {
+        unsigned int old_mode;
+        static DEFINE_SPINLOCK(xenpmu_mode_lock);
+
+        if ( pmu_params.val & ~(XENPMU_MODE_SELF | XENPMU_MODE_HV) )

I btw also highly doubt that all compiler versions can properly track the pmu_params is not used uninitialized here.

Jan

#12 lore raw

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

Quoted 27 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/hvm/vpmu.c +26 -0
+long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
+{
+    int ret;
+    struct xen_pmu_params pmu_params;
+
+    if ( vpmu_disabled )
+        return -EINVAL;
+
+    ret = xsm_pmu_op(XSM_OTHER, current->domain, op);
+    if ( ret )
+        return ret;
+
+    /* Check major version when parameters are specified */
+    switch ( op )
+    {
+    case XENPMU_mode_set:
+    case XENPMU_feature_set:
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        if ( pmu_params.version.maj != XENPMU_VER_MAJ )
+            return -EINVAL;
+    }
+
+    switch ( op )
+    {
Quoted 8 lines

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

Quoted 6 lines
xen/arch/x86/hvm/vpmu.c +6 -0
+    case XENPMU_mode_set:
+    {
+        unsigned int old_mode;
+        static DEFINE_SPINLOCK(xenpmu_mode_lock);
+
+        if ( pmu_params.val & ~(XENPMU_MODE_SELF | XENPMU_MODE_HV) )

I btw also highly doubt that all compiler versions can properly track the pmu_params is not used uninitialized here.

I'll add a zero initializer.

-boris

#13 lore raw

PATCH v18 6/16 x86/VPMU: Initialize VPMUs with __initcall

In reply to Boris Ostrovsky

Move some VPMU initilization operations into __initcalls to avoid performing same tests and calculations for each vcpu.

---
 xen/arch/x86/hvm/svm/vpmu.c       | 112 ++++++++++++----------------
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 151 +++++++++++++++++++-------------------
 xen/arch/x86/hvm/vpmu.c           |  32 ++++++++
 xen/include/asm-x86/hvm/vpmu.h    |   2 +
 4 files changed, 157 insertions(+), 140 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +47 -65
index 2cfdf08..03474a3 100644
@@ -375,57 +375,6 @@ static int amd_vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
     return 1;
 }
 
-static int amd_vpmu_initialise(struct vcpu *v)
-{
-    struct xen_pmu_amd_ctxt *ctxt;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    uint8_t family = current_cpu_data.x86;
-
-    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
-        return 0;
-
-    if ( counters == NULL )
-    {
-         switch ( family )
-	 {
-	 case 0x15:
-	     num_counters = F15H_NUM_COUNTERS;
-	     counters = AMD_F15H_COUNTERS;
-	     ctrls = AMD_F15H_CTRLS;
-	     k7_counters_mirrored = 1;
-	     break;
-	 case 0x10:
-	 case 0x12:
-	 case 0x14:
-	 case 0x16:
-	 default:
-	     num_counters = F10H_NUM_COUNTERS;
-	     counters = AMD_F10H_COUNTERS;
-	     ctrls = AMD_F10H_CTRLS;
-	     k7_counters_mirrored = 0;
-	     break;
-	 }
-    }
-
-    ctxt = xzalloc_bytes(sizeof(*ctxt) +
-                         2 * sizeof(uint64_t) * num_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;
-    }
-
-    ctxt->counters = sizeof(*ctxt);
-    ctxt->ctrls = ctxt->counters + sizeof(uint64_t) * num_counters;
-
-    vpmu->context = ctxt;
-    vpmu->priv_context = NULL;
-    vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
-    return 0;
-}
-
 static void amd_vpmu_destroy(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
@@ -497,30 +446,63 @@ struct arch_vpmu_ops amd_vpmu_ops = {
 
 int svm_vpmu_initialise(struct vcpu *v)
 {
+    struct xen_pmu_amd_ctxt *ctxt;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    uint8_t family = current_cpu_data.x86;
-    int ret = 0;
 
-    /* vpmu enabled? */
-    if ( vpmu_mode == XENPMU_MODE_OFF )
+    if ( (vpmu_mode == XENPMU_MODE_OFF) ||
+         vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return 0;
 
-    switch ( family )
+    if ( !counters )
+        return -EINVAL;
+
+    ctxt = xzalloc_bytes(sizeof(*ctxt) +
+                         2 * sizeof(uint64_t) * num_counters);
+    if ( !ctxt )
+    {
+        printk(XENLOG_G_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->counters = sizeof(*ctxt);
+    ctxt->ctrls = ctxt->counters + sizeof(uint64_t) * num_counters;
+
+    vpmu->context = ctxt;
+    vpmu->priv_context = NULL;
+
+    vpmu->arch_vpmu_ops = &amd_vpmu_ops;
+
+    vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
+    return 0;
+}
+
+int __init amd_vpmu_init(void)
+{
+    switch ( current_cpu_data.x86 )
     {
+    case 0x15:
+        num_counters = F15H_NUM_COUNTERS;
+        counters = AMD_F15H_COUNTERS;
+        ctrls = AMD_F15H_CTRLS;
+        k7_counters_mirrored = 1;
+        break;
     case 0x10:
     case 0x12:
     case 0x14:
-    case 0x15:
     case 0x16:
-        ret = amd_vpmu_initialise(v);
-        if ( !ret )
-            vpmu->arch_vpmu_ops = &amd_vpmu_ops;
-        return ret;
+        num_counters = F10H_NUM_COUNTERS;
+        counters = AMD_F10H_COUNTERS;
+        ctrls = AMD_F10H_CTRLS;
+        k7_counters_mirrored = 0;
+        break;
+    default:
+        printk(XENLOG_WARNING "VPMU: Unsupported CPU family %#x\n",
+               current_cpu_data.x86);
+        return -EINVAL;
     }
 
-    printk("VPMU: Initialization failed. "
-           "AMD processor family %d has not "
-           "been supported\n", family);
-    return -EINVAL;
+    return 0;
 }
 
xen/arch/x86/hvm/vmx/vpmu_core2.c +76 -75

177 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +32 -0
index 40a89d9..f62fa80 100644
@@ -528,14 +528,46 @@ long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
 
 static int __init vpmu_init(void)
 {
+    int vendor = current_cpu_data.x86_vendor;
+
+    if ( vpmu_disabled )
+    {
+        printk(XENLOG_INFO "VPMU: disabled\n");
+        return 0;
+    }
+
     /* NMI watchdog uses LVTPC and HW counter */
     if ( opt_watchdog && !vpmu_disabled )
     {
         printk(XENLOG_WARNING "NMI watchdog is enabled. Turning VPMU off.\n");
         vpmu_mode = XENPMU_MODE_OFF;
         vpmu_disabled = 1;
+        return 0;
+    }
+
+    switch ( vendor )
+    {
+    case X86_VENDOR_AMD:
+        if ( amd_vpmu_init() )
+           vpmu_mode = XENPMU_MODE_OFF;
+        break;
+    case X86_VENDOR_INTEL:
+        if ( core2_vpmu_init() )
+           vpmu_mode = XENPMU_MODE_OFF;
+        break;
+    default:
+        printk(XENLOG_WARNING "VPMU: Unknown CPU vendor: %d. "
+               "Turning VPMU off.\n", vendor);
+        vpmu_mode = XENPMU_MODE_OFF;
+        break;
     }
 
+    if ( vpmu_mode != XENPMU_MODE_OFF )
+        printk(XENLOG_INFO "VPMU: version " __stringify(XENPMU_VER_MAJ) "."
+               __stringify(XENPMU_VER_MIN) "\n");
+    else
+        vpmu_disabled = 1;
+
     return 0;
 }
 __initcall(vpmu_init);
xen/include/asm-x86/hvm/vpmu.h +2 -0
index 1171b2a..cf32f82 100644
@@ -62,7 +62,9 @@ struct arch_vpmu_ops {
     void (*arch_vpmu_dump)(const struct vcpu *);
 };
 
+int core2_vpmu_init(void);
 int vmx_vpmu_initialise(struct vcpu *);
+int amd_vpmu_init(void);
 int svm_vpmu_initialise(struct vcpu *);
 
 /* VPMU states */
Signature · 1 line
1.8.1.4
#15 lore raw

PATCH v18 7/16 x86/VPMU: Initialize PMU for PV(H) guests

In reply to Boris Ostrovsky

Code for initializing/tearing down PMU for PV guests

---
 tools/flask/policy/policy/modules/xen/xen.te |   4 ++
 xen/arch/x86/domain.c                        |   2 +
 xen/arch/x86/hvm/hvm.c                       |   1 +
 xen/arch/x86/hvm/svm/svm.c                   |   4 +-
 xen/arch/x86/hvm/svm/vpmu.c                  |  44 ++++++++----
 xen/arch/x86/hvm/vmx/vmx.c                   |   4 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c            |  79 +++++++++++++++------
 xen/arch/x86/hvm/vpmu.c                      | 102 +++++++++++++++++++++++++--
 xen/common/event_channel.c                   |   1 +
 xen/include/asm-x86/hvm/vpmu.h               |   2 +
 xen/include/public/pmu.h                     |   2 +
 xen/include/public/xen.h                     |   1 +
 xen/include/xsm/dummy.h                      |   3 +
 xen/xsm/flask/hooks.c                        |   4 ++
 xen/xsm/flask/policy/access_vectors          |   2 +
 15 files changed, 212 insertions(+), 43 deletions(-)
tools/flask/policy/policy/modules/xen/xen.te +4 -0
index 870ff81..73bbe7b 100644
@@ -120,6 +120,10 @@ domain_comms(dom0_t, dom0_t)
 # Allow all domains to use (unprivileged parts of) the tmem hypercall
 allow domain_type xen_t:xen tmem_op;
 
+# Allow all domains to use PMU (but not to change its settings --- that's what
+# pmu_ctrl is for)
+allow domain_type xen_t:xen2 pmu_use;
+
 ###############################################################################
 #
 # Domain creation
xen/arch/x86/domain.c +2 -0
index b0e3c3d..838bbb7 100644
@@ -437,6 +437,8 @@ int vcpu_initialise(struct vcpu *v)
         vmce_init_vcpu(v);
     }
 
+    spin_lock_init(&v->arch.vpmu.vpmu_lock);
+
     if ( has_hvm_container_domain(d) )
     {
         rc = hvm_vcpu_initialise(v);
xen/arch/x86/hvm/hvm.c +1 -0
index b03ee4e..21f7c35 100644
@@ -4873,6 +4873,7 @@ static hvm_hypercall_t *const pvh_hypercall64_table[NR_hypercalls] = {
     HYPERCALL(hvm_op),
     HYPERCALL(sysctl),
     HYPERCALL(domctl),
+    HYPERCALL(xenpmu_op),
     [ __HYPERVISOR_arch_1 ] = (hvm_hypercall_t *)paging_domctl_continuation
 };
 
xen/arch/x86/hvm/svm/svm.c +3 -1
index a7655bd..59cca08 100644
@@ -1166,7 +1166,9 @@ static int svm_vcpu_initialise(struct vcpu *v)
         return rc;
     }
 
-    vpmu_initialise(v);
+    /* PVH's VPMU is initialized via hypercall */
+    if ( is_hvm_vcpu(v) )
+        vpmu_initialise(v);
 
     svm_guest_osvw_init(v);
 
xen/arch/x86/hvm/svm/vpmu.c +30 -14

68 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vmx.c +3 -1
index 88b7821..65b77ed 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_vcpu(v) )
+        vpmu_initialise(v);
 
     vmx_install_vlapic_mapping(v);
 
xen/arch/x86/hvm/vmx/vpmu_core2.c +56 -23

113 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +98 -4

145 lines not on this page · raw message

xen/common/event_channel.c +1 -0
index fae242d..310f590 100644
@@ -106,6 +106,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 +2 -0
index cf32f82..42a09f9 100644
@@ -44,6 +44,8 @@ struct vpmu_struct {
     void *context;      /* May be shared with PV guest */
     void *priv_context; /* hypervisor-only */
     struct arch_vpmu_ops *arch_vpmu_ops;
+    struct xen_pmu_data *xenpmu_data;
+    spinlock_t vpmu_lock;
 };
 
 /* Arch specific operations shared by all vpmus */
xen/include/public/pmu.h +2 -0
index 66cc494..afb4ca1 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 0dd3c97..a6b26fe 100644
@@ -161,6 +161,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define VIRQ_MEM_EVENT  10 /* G. (DOM0) A memory event has occured           */
 #define VIRQ_XC_RESERVED 11 /* G. Reserved for XenClient                     */
 #define VIRQ_ENOMEM     12 /* G. (DOM0) Low on heap memory       */
+#define VIRQ_XENPMU     13 /* V.  PMC interrupt                              */
 
 /* Architecture-specific VIRQ definitions. */
 #define VIRQ_ARCH_0    16
xen/include/xsm/dummy.h +3 -0
index c637454..ae47135 100644
@@ -665,6 +665,9 @@ static XSM_INLINE int xsm_pmu_op (XSM_DEFAULT_ARG struct domain *d, int op)
     case XENPMU_feature_set:
     case XENPMU_feature_get:
         return xsm_default_action(XSM_PRIV, d, current->domain);
+    case XENPMU_init:
+    case XENPMU_finish: 
+        return xsm_default_action(XSM_HOOK, d, current->domain);
     default:
         return -EPERM;
     }
xen/xsm/flask/hooks.c +4 -0
index 982e879..5011cb9 100644
@@ -1518,6 +1518,10 @@ static int flask_pmu_op (struct domain *d, unsigned int op)
     case XENPMU_feature_get:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_XEN2,
                             XEN2__PMU_CTRL, NULL);
+    case XENPMU_init:
+    case XENPMU_finish:
+        return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_XEN2,
+                            XEN2__PMU_USE, NULL);
     default:
         return -EPERM;
     }
xen/xsm/flask/policy/access_vectors +2 -0
index 626850d..ef5b867 100644
@@ -88,6 +88,8 @@ class xen2
     get_symbol
 # PMU control
     pmu_ctrl
+# PMU use (domains, including unprivileged ones, will be using this operation)
+    pmu_use
 }
 
 # Classes domain and domain2 consist of operations that a domain performs on
Signature · 1 line
1.8.1.4
#16 lore raw

Re: [PATCH v18 07/16] x86/VPMU: Initialize PMU for PV(H) guests

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 440

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/domain.c +1 -0
@@ -437,6 +437,8 @@ int vcpu_initialise(struct vcpu *v)
         vmce_init_vcpu(v);
     }
 
+    spin_lock_init(&v->arch.vpmu.vpmu_lock);

This would rather seem to belong into vpmu_initialize().

xen/arch/x86/domain.c int __init amd_vpmu_init(void) · line 520

Quoted 13 lines
xen/arch/x86/domain.c +9 -0
@@ -503,6 +509,16 @@ int __init amd_vpmu_init(void)
         return -EINVAL;
     }
 
+    if ( sizeof(struct xen_pmu_data) +
+         2 * sizeof(uint64_t) * num_counters > PAGE_SIZE )
+    {
+        printk(XENLOG_WARNING
+               "VPMU: Register bank does not fit into VPMU shared page\n");
+        counters = ctrls = NULL;
+        num_counters = 0;
+        return -ENOSPC;
+    }

Wouldn't it be more reasonable to simply lower num_counters in that case?

Quoted 32 lines
xen/arch/x86/domain.c +32 -0
+static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
+{
+    struct vcpu *v;
+    struct vpmu_struct *vpmu;
+    struct page_info *page;
+    uint64_t gfn = params->val;
+
+    if ( vpmu_mode == XENPMU_MODE_OFF )
+        return -EINVAL;
+
+    if ( (params->vcpu >= d->max_vcpus) || (d->vcpu == NULL) ||
+         (d->vcpu[params->vcpu] == NULL) )
+        return -EINVAL;
+
+    if ( v->arch.vpmu.xenpmu_data )
+        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);
+        return -EINVAL;
+    }
+
+    v = d->vcpu[params->vcpu];
+    vpmu = vcpu_vpmu(v);
+    spin_lock(&vpmu->vpmu_lock);
+
+    v->arch.vpmu.xenpmu_data = __map_domain_page_global(page);

xen/arch/x86/domain.c int __init amd_vpmu_init(void)

Quoted 10 lines
xen/arch/x86/domain.c +10 -0
+    if ( !v->arch.vpmu.xenpmu_data )
+    {
+        put_page_and_type(page);
+        spin_unlock(&vpmu->vpmu_lock);
+        return -EINVAL;
+    }
+
+    vpmu_initialise(v);
+
+    spin_unlock(&vpmu->vpmu_lock);

So what is this lock guarding against here? Certainly not overwriting of a non-NULL v->arch.vpmu.xenpmu_data (and hence leaking a page reference)...

xen/arch/x86/domain.c long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg) · line 593

Quoted 6 lines
xen/arch/x86/domain.c +1 -1
@@ -504,7 +590,7 @@ long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
 
         if ( copy_to_guest(arg, &pmu_params, 1) )
             return -EFAULT;
-        break;
+         break;

???

Jan

#17 lore raw

Re: [PATCH v18 07/16] x86/VPMU: Initialize PMU for PV(H) guests

Quoted 9 lines · Jan Beulich

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 440

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/domain.c +1 -0
@@ -437,6 +437,8 @@ int vcpu_initialise(struct vcpu *v)
          vmce_init_vcpu(v);
      }
 
+    spin_lock_init(&v->arch.vpmu.vpmu_lock);

This would rather seem to belong into vpmu_initialize().

vpmu_initialize() is called under this lock so we can't do this.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/domain.c +0 -0
@@ -503,6 +509,16 @@ int __init amd_vpmu_init(void)
          return -EINVAL;
      }
 
Quoted 11 lines

xen/arch/x86/domain.c int __init amd_vpmu_init(void) · line 520

Quoted 9 lines
xen/arch/x86/domain.c +9 -0
+    if ( sizeof(struct xen_pmu_data) +
+         2 * sizeof(uint64_t) * num_counters > PAGE_SIZE )
+    {
+        printk(XENLOG_WARNING
+               "VPMU: Register bank does not fit into VPMU shared page\n");
+        counters = ctrls = NULL;
+        num_counters = 0;
+        return -ENOSPC;
+    }

Wouldn't it be more reasonable to simply lower num_counters in that case?

AMD processors in particular have fixed number of counters per family so a guest would expect to see all of them. (on Intel we then might get away by updating CPUID leaf 0xa but I am not sure it's worth doing).

Quoted 38 lines
Quoted 38 lines
xen/arch/x86/domain.c +37 -0
+static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
+{
+    struct vcpu *v;
+    struct vpmu_struct *vpmu;
+    struct page_info *page;
+    uint64_t gfn = params->val;
+
+    if ( vpmu_mode == XENPMU_MODE_OFF )
+        return -EINVAL;
+
+    if ( (params->vcpu >= d->max_vcpus) || (d->vcpu == NULL) ||
+         (d->vcpu[params->vcpu] == NULL) )
+        return -EINVAL;
+
+    if ( v->arch.vpmu.xenpmu_data )
+        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);
+        return -EINVAL;
+    }
+
+    v = d->vcpu[params->vcpu];
+    vpmu = vcpu_vpmu(v);
+    spin_lock(&vpmu->vpmu_lock);
+
+    v->arch.vpmu.xenpmu_data = __map_domain_page_global(page);
+    if ( !v->arch.vpmu.xenpmu_data )
+    {
+        put_page_and_type(page);
+        spin_unlock(&vpmu->vpmu_lock);
+        return -EINVAL;
Quoted 8 lines

xen/arch/x86/domain.c

Quoted 5 lines
xen/arch/x86/domain.c +5 -0
+    }
+
+    vpmu_initialise(v);
+
+    spin_unlock(&vpmu->vpmu_lock);

So what is this lock guarding against here? Certainly not overwriting of a non-NULL v->arch.vpmu.xenpmu_data (and hence leaking a page reference)...

This is trying to protect a race with pvmu_finish() that could clear xenpmu_data.

(I actually think you were the one who suggested it).

-boris

Quoted 11 lines

xen/arch/x86/domain.c long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg) · line 593

Quoted 7 lines
xen/arch/x86/domain.c +1 -1
@@ -504,7 +590,7 @@ long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
 
          if ( copy_to_guest(arg, &pmu_params, 1) )
              return -EFAULT;
-        break;
+         break;

???

Jan

#18 lore raw

Re: [PATCH v18 07/16] x86/VPMU: Initialize PMU for PV(H) guests

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 20.02.15 at 17:15, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/domain.c +0 -0
@@ -437,6 +437,8 @@ int vcpu_initialise(struct vcpu *v)
          vmce_init_vcpu(v);
      }
 
Quoted 4 lines
Quoted 2 lines

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 440

Quoted 1 line
xen/arch/x86/domain.c +1 -0
+    spin_lock_init(&v->arch.vpmu.vpmu_lock);

This would rather seem to belong into vpmu_initialize().

vpmu_initialize() is called under this lock so we can't do this.

Yes, I saw that later on, but it still doesn't look well structured. Can't you bail early from vpmu_initialize() the first time through for PV(H) guests, rather than guarding the HVM invocations with is_hvm_...()?

Quoted 46 lines
Quoted 37 lines
Quoted 37 lines
xen/arch/x86/domain.c +37 -0
+static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
+{
+    struct vcpu *v;
+    struct vpmu_struct *vpmu;
+    struct page_info *page;
+    uint64_t gfn = params->val;
+
+    if ( vpmu_mode == XENPMU_MODE_OFF )
+        return -EINVAL;
+
+    if ( (params->vcpu >= d->max_vcpus) || (d->vcpu == NULL) ||
+         (d->vcpu[params->vcpu] == NULL) )
+        return -EINVAL;
+
+    if ( v->arch.vpmu.xenpmu_data )
+        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);
+        return -EINVAL;
+    }
+
+    v = d->vcpu[params->vcpu];
+    vpmu = vcpu_vpmu(v);
+    spin_lock(&vpmu->vpmu_lock);
+
+    v->arch.vpmu.xenpmu_data = __map_domain_page_global(page);
+    if ( !v->arch.vpmu.xenpmu_data )
+    {
+        put_page_and_type(page);
+        spin_unlock(&vpmu->vpmu_lock);
+        return -EINVAL;
Quoted 8 lines

xen/arch/x86/domain.c

Quoted 5 lines
xen/arch/x86/domain.c +5 -0
+    }
+
+    vpmu_initialise(v);
+
+    spin_unlock(&vpmu->vpmu_lock);

So what is this lock guarding against here? Certainly not overwriting of a non-NULL v->arch.vpmu.xenpmu_data (and hence leaking a page reference)...

Quoted 4 lines

This is trying to protect a race with pvmu_finish() that could clear xenpmu_data.

(I actually think you were the one who suggested it).

But it should also protect against a second pvpmu_init() on another pCPU.

Jan

#19 lore raw

Re: [PATCH v18 07/16] x86/VPMU: Initialize PMU for PV(H) guests

Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 20.02.15 at 17:15, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/domain.c +0 -0
@@ -437,6 +437,8 @@ int vcpu_initialise(struct vcpu *v)
           vmce_init_vcpu(v);
       }
 
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 440

Quoted 1 line
xen/arch/x86/domain.c +1 -0
+    spin_lock_init(&v->arch.vpmu.vpmu_lock);

This would rather seem to belong into vpmu_initialize().

vpmu_initialize() is called under this lock so we can't do this.

Yes, I saw that later on, but it still doesn't look well structured. Can't you bail early from vpmu_initialize() the first time through for PV(H) guests, rather than guarding the HVM invocations with is_hvm_...()?

I could but I am not sure how it would allow me to move spin_lock_init() to vpmu_initialize().

We are protecting xenpmu_data and it is supposed to be set before we get into vpmu_initialize().

Quoted 46 lines
Quoted 46 lines
Quoted 46 lines
Quoted 33 lines
xen/arch/x86/domain.c +32 -0
+static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
+{
+    struct vcpu *v;
+    struct vpmu_struct *vpmu;
+    struct page_info *page;
+    uint64_t gfn = params->val;
+
+    if ( vpmu_mode == XENPMU_MODE_OFF )
+        return -EINVAL;
+
+    if ( (params->vcpu >= d->max_vcpus) || (d->vcpu == NULL) ||
+         (d->vcpu[params->vcpu] == NULL) )
+        return -EINVAL;
+
+    if ( v->arch.vpmu.xenpmu_data )
+        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);
+        return -EINVAL;
+    }
+
+    v = d->vcpu[params->vcpu];
+    vpmu = vcpu_vpmu(v);
+    spin_lock(&vpmu->vpmu_lock);
+
+    v->arch.vpmu.xenpmu_data = __map_domain_page_global(page);

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v)

Quoted 10 lines
xen/arch/x86/domain.c +10 -0
+    if ( !v->arch.vpmu.xenpmu_data )
+    {
+        put_page_and_type(page);
+        spin_unlock(&vpmu->vpmu_lock);
+        return -EINVAL;
+    }
+
+    vpmu_initialise(v);
+
+    spin_unlock(&vpmu->vpmu_lock);

So what is this lock guarding against here? Certainly not overwriting of a non-NULL v->arch.vpmu.xenpmu_data (and hence leaking a page reference)...

Quoted 6 lines
Quoted 4 lines

This is trying to protect a race with pvmu_finish() that could clear xenpmu_data.

(I actually think you were the one who suggested it).

But it should also protect against a second pvpmu_init() on another pCPU.

Right, I will move 'if (v->arch.vpmu.xenpmu_data )' under the lock (and clean up if it is non-NULL)

-boris

#20 lore raw

PATCH v18 8/16 x86/VPMU: Save VPMU state for PV guests during context switch

In reply to Boris Ostrovsky

Save VPMU state during context switch for both HVM and PV(H) guests.

A subsequent patch ("x86/VPMU: NMI-based VPMU support") will make it possible for vpmu_switch_to() to call vmx_vmcs_try_enter()->vcpu_pause() which needs is_running to be correctly set/cleared. To prepare for that, call context_saved() before vpmu_switch_to() is executed. (Note that while this change could have been dalayed until that later patch, the changes are harmless to existing code and so we do it here)

---
 xen/arch/x86/domain.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
xen/arch/x86/domain.c +10 -12
index 838bbb7..5e33277 100644
@@ -1533,17 +1533,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_switch_from(vcpu_vpmu(prev), vcpu_vpmu(next));
-
-        if ( !list_empty(&prev->arch.hvm_vcpu.tm_list) )
-            pt_save_timer(prev);
+        _update_runstate_area(prev);
+        vpmu_switch_from(vcpu_vpmu(prev), vcpu_vpmu(next));
     }
 
+    if ( is_hvm_vcpu(prev) && !list_empty(&prev->arch.hvm_vcpu.tm_list) )
+        pt_save_timer(prev);
+
     local_irq_disable();
 
     set_current(next);
@@ -1581,15 +1578,16 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
                            !is_hardware_domain(next->domain));
     }
 
-    if ( is_hvm_vcpu(next) && (prev != next) )
-        /* Must be done with interrupts enabled */
-        vpmu_switch_to(vcpu_vpmu(prev), vcpu_vpmu(next));
-
     context_saved(prev);
 
     if ( prev != next )
+    {
         _update_runstate_area(next);
 
+        /* Must be done with interrupts enabled */
+        vpmu_switch_to(vcpu_vpmu(prev), vcpu_vpmu(next));
+    }
+
     /* Ensure that the vcpu has an up-to-date time base. */
     update_vcpu_system_time(next);
 
Signature · 1 line
1.8.1.4
#21 lore raw

PATCH v18 9/16 x86/VPMU: When handling MSR accesses, leave fault injection to callers

In reply to Boris Ostrovsky

With this patch return value of 1 of vpmu_do_msr() will now indicate whether an error was encountered during MSR processing (instead of stating that the access was to a VPMU register).

As part of this patch we also check for validity of certain MSR accesses right when we determine which register is being written, as opposed to postponing this until later.

---
 xen/arch/x86/hvm/svm/svm.c        |  6 ++-
 xen/arch/x86/hvm/svm/vpmu.c       |  6 +--
 xen/arch/x86/hvm/vmx/vmx.c        | 24 +++++++++---
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 82 ++++++++++++++-------------------------
 4 files changed, 55 insertions(+), 63 deletions(-)
xen/arch/x86/hvm/svm/svm.c +4 -2
index 59cca08..a8cb9ae 100644
@@ -1709,7 +1709,8 @@ 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);
+        if ( vpmu_do_rdmsr(msr, msr_content) )
+            goto gpf;
         break;
 
     case MSR_AMD64_DR0_ADDRESS_MASK:
@@ -1860,7 +1861,8 @@ 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, 0);
+        if ( vpmu_do_wrmsr(msr, msr_content, 0) )
+            goto gpf;
         break;
 
     case MSR_IA32_MCx_MISC(4): /* Threshold register */
xen/arch/x86/hvm/svm/vpmu.c +3 -3
index 7eeefa2..27c8a9c 100644
@@ -324,7 +324,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
         is_pmu_enabled(msr_content) && !vpmu_is_set(vpmu, VPMU_RUNNING) )
     {
         if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
-            return 1;
+            return 0;
         vpmu_set(vpmu, VPMU_RUNNING);
 
         if ( has_hvm_container_vcpu(v) && is_msr_bitmap_on(vpmu) )
@@ -354,7 +354,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
 
     /* Write to hw counters */
     wrmsrl(msr, msr_content);
-    return 1;
+    return 0;
 }
 
 static int amd_vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
@@ -372,7 +372,7 @@ static int amd_vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 
     rdmsrl(msr, *msr_content);
 
-    return 1;
+    return 0;
 }
 
 static void amd_vpmu_destroy(struct vcpu *v)
xen/arch/x86/hvm/vmx/vmx.c +18 -6
index 65b77ed..a97efa7 100644
@@ -2127,12 +2127,17 @@ 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. */
+        /* FALLTHROUGH */
+    case MSR_P6_PERFCTR(0)...MSR_P6_PERFCTR(7):
+    case MSR_P6_EVNTSEL(0)...MSR_P6_EVNTSEL(3):
+    case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+    case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+    case MSR_IA32_PEBS_ENABLE:
+    case MSR_IA32_DS_AREA:
         if ( vpmu_do_rdmsr(msr, msr_content) )
-            goto done;
+            goto gp_fault;
         break;
     default:
-        if ( vpmu_do_rdmsr(msr, msr_content) )
-            break;
         if ( passive_domain_do_rdmsr(msr, msr_content) )
             goto done;
         switch ( long_mode_do_msr_read(msr, msr_content) )
@@ -2308,7 +2313,7 @@ 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, supported) )
+            if ( vpmu_do_wrmsr(msr, msr_content, supported) )
                 break;
         }
         if ( msr_content & IA32_DEBUGCTLMSR_LBR )
@@ -2336,9 +2341,16 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
         if ( !nvmx_msr_write_intercept(msr, msr_content) )
             goto gp_fault;
         break;
+    case MSR_P6_PERFCTR(0)...MSR_P6_PERFCTR(7):
+    case MSR_P6_EVNTSEL(0)...MSR_P6_EVNTSEL(7):
+    case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+    case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+    case MSR_IA32_PEBS_ENABLE:
+    case MSR_IA32_DS_AREA:
+         if ( vpmu_do_wrmsr(msr, msr_content, 0) )
+            goto gp_fault;
+        break;
     default:
-        if ( vpmu_do_wrmsr(msr, msr_content, 0) )
-            return X86EMUL_OKAY;
         if ( passive_domain_do_wrmsr(msr, msr_content) )
             return X86EMUL_OKAY;
 
xen/arch/x86/hvm/vmx/vpmu_core2.c +30 -52

163 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#22 lore raw

PATCH v18 10/16 x86/VPMU: Add support for PMU register handling on PV guests

In reply to Boris Ostrovsky

Intercept accesses to PMU MSRs and process them in VPMU module. If vpmu ops for VCPU are not initialized (which is the case, for example, for PV guests that are not "VPMU-enlightened") access to MSRs will return failure.

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 | 53 +++++++++++++++++++++++++++++++--------
 xen/arch/x86/hvm/vpmu.c           |  3 +++
 xen/arch/x86/traps.c              | 50 +++++++++++++++++++++++++++++++++++-
 4 files changed, 96 insertions(+), 13 deletions(-)
xen/arch/x86/domain.c +1 -2
index 5e33277..d8f5f84 100644
@@ -2065,8 +2065,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 +43 -10

125 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +3 -0
index bdd66de..1d5ae8d 100644
@@ -118,6 +118,9 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_rdmsr )
         return vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
+    else
+        *msr_content = 0;
+
     return 0;
 }
 
xen/arch/x86/traps.c +49 -1

99 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#23 lore raw

PATCH v18 11/16 x86/VPMU: Handle PMU interrupts for PV guests

In reply to Boris Ostrovsky

Add support for handling PMU interrupts for PV guests.

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

Since the interrupt handler may now force VPMU context save (i.e. set VPMU_CONTEXT_SAVE flag) we need to make changes to amd_vpmu_save() which until now expected this flag to be set only when the counters were stopped.

---
 xen/arch/x86/hvm/svm/vpmu.c       |  11 +-
 xen/arch/x86/hvm/vpmu.c           | 209 ++++++++++++++++++++++++++++++++++++--
 xen/include/public/arch-x86/pmu.h |   6 ++
 xen/include/public/pmu.h          |   2 +
 xen/include/xsm/dummy.h           |   4 +-
 xen/xsm/flask/hooks.c             |   2 +
 6 files changed, 214 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +3 -8
index 27c8a9c..68113c7 100644
@@ -225,17 +225,12 @@ static int amd_vpmu_save(struct vpmu_struct *vpmu)
     struct vcpu *v;
     unsigned int i;
 
-    /*
-     * Stop the counters. If we came here via vpmu_save_force (i.e.
-     * when VPMU_CONTEXT_SAVE is set) counters are already stopped.
-     */
+    for ( i = 0; i < num_counters; i++ )
+        wrmsrl(ctrls[i], 0);
+
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_SAVE) )
     {
         vpmu_set(vpmu, VPMU_FROZEN);
-
-        for ( i = 0; i < num_counters; i++ )
-            wrmsrl(ctrls[i], 0);
-
         return 0;
     }
 
xen/arch/x86/hvm/vpmu.c +198 -11

286 lines not on this page · raw message

xen/include/public/arch-x86/pmu.h +6 -0
index 8bbe341..74a91ac 100644
@@ -51,6 +51,12 @@ struct xen_pmu_regs {
 typedef struct xen_pmu_regs xen_pmu_regs_t;
 DEFINE_XEN_GUEST_HANDLE(xen_pmu_regs_t);
 
+/* PMU flags */
+#define PMU_CACHED         (1<<0) /* PMU MSRs are cached in the context */
+#define PMU_SAMPLE_USER    (1<<1) /* Sample is from user or kernel mode */
+#define PMU_SAMPLE_REAL    (1<<2) /* Sample is from realmode */
+#define PMU_SAMPLE_PV      (1<<3) /* Sample from a PV guest */
+
 struct xen_pmu_arch {
     union {
         struct xen_pmu_regs regs;
xen/include/public/pmu.h +2 -0
index afb4ca1..db5321a 100644
@@ -27,6 +27,8 @@
 #define XENPMU_feature_set     3
 #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 */
xen/include/xsm/dummy.h +3 -1
index ae47135..1ad4ecc 100644
@@ -666,7 +666,9 @@ static XSM_INLINE int xsm_pmu_op (XSM_DEFAULT_ARG struct domain *d, int op)
     case XENPMU_feature_get:
         return xsm_default_action(XSM_PRIV, d, current->domain);
     case XENPMU_init:
-    case XENPMU_finish: 
+    case XENPMU_finish:
+    case XENPMU_lvtpc_set:
+    case XENPMU_flush:
         return xsm_default_action(XSM_HOOK, d, current->domain);
     default:
         return -EPERM;
xen/xsm/flask/hooks.c +2 -0
index 5011cb9..e47c86b 100644
@@ -1520,6 +1520,8 @@ static int flask_pmu_op (struct domain *d, unsigned int op)
                             XEN2__PMU_CTRL, NULL);
     case XENPMU_init:
     case XENPMU_finish:
+    case XENPMU_lvtpc_set:
+    case XENPMU_flush:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_XEN2,
                             XEN2__PMU_USE, NULL);
     default:
Signature · 1 line
1.8.1.4
#24 lore raw

Re: [PATCH v18 11/16] x86/VPMU: Handle PMU interrupts for PV guests

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

Quoted 11 lines · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +6 -0
index 1d5ae8d..cf4c70b 100644
 
+static struct vcpu *choose_hwdom_vcpu(void)
+{
+    unsigned idx = smp_processor_id() % hardware_domain->max_vcpus;
+
+    if ( hardware_domain->vcpu == NULL )
+        return NULL;

If d->vcpu is NULL, d->max_vcpus was 0 and you have already died with a #DE

You can guarentee that if max_vcpus > 0, d->vcpu is not NULL.

~Andrew

Quoted 4 lines
xen/arch/x86/hvm/vpmu.c +4 -0
+
+    return hardware_domain->vcpu[idx];
+}
+
#25 lore raw

Re: [PATCH v18 11/16] x86/VPMU: Handle PMU interrupts for PV guests

Quoted 14 lines · Andrew Cooper

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

Quoted 11 lines · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +6 -0
index 1d5ae8d..cf4c70b 100644
 
+static struct vcpu *choose_hwdom_vcpu(void)
+{
+    unsigned idx = smp_processor_id() % hardware_domain->max_vcpus;
+
+    if ( hardware_domain->vcpu == NULL )
+        return NULL;

If d->vcpu is NULL, d->max_vcpus was 0 and you have already died with a #DE

Quoted 1 line

You can guarentee that if max_vcpus > 0, d->vcpu is not NULL.

IIRC Jan asked for d->vcpu tests at some point. This particular one is probably indeed pointless since hardware_domain better have this as non-NULL.

-boris

Quoted 9 lines

~Andrew

Quoted 4 lines
xen/arch/x86/hvm/vpmu.c +4 -0
+
+    return hardware_domain->vcpu[idx];
+}
+
#26 lore raw

Re: [PATCH v18 11/16] x86/VPMU: Handle PMU interrupts for PV guests

Quoted 16 lines · Boris Ostrovsky
Quoted 15 lines · Andrew Cooper

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

Quoted 11 lines · Boris Ostrovsky
xen/arch/x86/hvm/vpmu.c +6 -0
index 1d5ae8d..cf4c70b 100644
 
+static struct vcpu *choose_hwdom_vcpu(void)
+{
+    unsigned idx = smp_processor_id() % hardware_domain->max_vcpus;
+
+    if ( hardware_domain->vcpu == NULL )
+        return NULL;

If d->vcpu is NULL, d->max_vcpus was 0 and you have already died with a #DE

Quoted 5 lines
Quoted 1 line

You can guarentee that if max_vcpus > 0, d->vcpu is not NULL.

IIRC Jan asked for d->vcpu tests at some point. This particular one is probably indeed pointless since hardware_domain better have this as non-NULL.

You should sanity check max_vcpus > 0 before doing the % (although I would really hope that this codepath would never trigger ahead of dom0 being set up), at which point d->vcpu[idx] is well formed.

~Andrew

#27 lore raw

PATCH v18 12/16 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/vpmu.c        | 77 ++++++++++++++++--------------------------
 xen/include/asm-x86/hvm/vpmu.h | 14 ++++++--
 2 files changed, 42 insertions(+), 49 deletions(-)
xen/arch/x86/hvm/vpmu.c +30 -47
index cf4c70b..bf9f5a8 100644
@@ -100,65 +100,48 @@ void vpmu_lvtpc_update(uint32_t val)
         apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
 }
 
-int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, uint64_t supported)
+int vpmu_do_msr(unsigned int msr, uint64_t *msr_content,
+                uint64_t supported, bool_t is_write)
 {
-    struct vcpu *curr = current;
+    struct vcpu *curr;
     struct vpmu_struct *vpmu;
+    struct arch_vpmu_ops *ops;
+    int ret = 0;
 
     if ( !(vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV)) )
-        return 0;
+        goto nop;
 
+    curr = current;
     vpmu = vcpu_vpmu(curr);
-    if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_wrmsr )
-    {
-        int ret = vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content, supported);
-
-        /*
-         * 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_vcpu(curr) && vpmu->xenpmu_data &&
-             (vpmu->xenpmu_data->pmu.pmu_flags & PMU_CACHED) )
-        {
-            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
-            vpmu->arch_vpmu_ops->arch_vpmu_save(vpmu);
-            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
-        }
-        return ret;
-    }
-
-    return 0;
-}
-
-int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
-{
-    struct vcpu *curr = current;
-    struct vpmu_struct *vpmu;
+    ops = vpmu->arch_vpmu_ops;
+    if ( !ops )
+        goto nop;
+
+    if ( is_write && ops->do_wrmsr )
+        ret = ops->do_wrmsr(msr, *msr_content, supported);
+    else if ( !is_write && ops->do_rdmsr )
+        ret = ops->do_rdmsr(msr, msr_content);
+    else
+        goto nop;
 
-    if ( !(vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV)) )
+    /*
+     * We may have received a PMU interrupt while handling MSR access
+     * and since do_wr/rdmsr may load VPMU context we should save
+     * (and unload) it again.
+     */
+    if ( !is_hvm_vcpu(curr) &&
+         vpmu->xenpmu_data && (vpmu->xenpmu_data->pmu.pmu_flags & PMU_CACHED) )
     {
-        *msr_content = 0;
-        return 0;
+        vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+        ops->arch_vpmu_save(vpmu);
+        vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
     }
 
-    vpmu = vcpu_vpmu(curr);
-    if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_rdmsr )
-    {
-        int ret = vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
+    return ret;
 
-        if ( !is_hvm_vcpu(curr) && vpmu->xenpmu_data &&
-             (vpmu->xenpmu_data->pmu.pmu_flags & PMU_CACHED) )
-        {
-            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
-            vpmu->arch_vpmu_ops->arch_vpmu_save(vpmu);
-            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
-        }
-        return ret;
-    }
-    else
+ nop:
+    if ( !is_write )
         *msr_content = 0;
-
     return 0;
 }
 
xen/include/asm-x86/hvm/vpmu.h +12 -2
index 42a09f9..2c888cc 100644
@@ -100,8 +100,8 @@ static inline bool_t vpmu_are_all_set(const struct vpmu_struct *vpmu,
 }
 
 void vpmu_lvtpc_update(uint32_t val);
-int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, uint64_t supported);
-int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content);
+int vpmu_do_msr(unsigned int msr, uint64_t *msr_content,
+                uint64_t supported, bool_t is_write);
 void 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);
@@ -112,6 +112,16 @@ void vpmu_load(struct vpmu_struct *vpmu);
 void vpmu_unload(struct vpmu_struct *vpmu);
 void vpmu_dump(struct vcpu *v);
 
+static inline int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
+                                uint64_t supported)
+{
+    return vpmu_do_msr(msr, &msr_content, supported, 1);
+}
+static inline int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
+{
+    return vpmu_do_msr(msr, msr_content, 0, 0);
+}
+
 extern int acquire_pmu_ownership(int pmu_ownership);
 extern void release_pmu_ownership(int pmu_ownership);
 
Signature · 1 line
1.8.1.4
Acked-by on v18 12/16
#28 lore raw

Re: [PATCH v18 12/16] x86/VPMU: Merge vpmu_rdmsr and vpmu_wrmsr

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:
xen/arch/x86/hvm/vpmu.c +0 -0
@@ -100,65 +100,48 @@ void vpmu_lvtpc_update(uint32_t val)
         apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +4 -2
 }
 
-int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, uint64_t supported)
+int vpmu_do_msr(unsigned int msr, uint64_t *msr_content,
+                uint64_t supported, bool_t is_write)
 {
-    struct vcpu *curr = current;
+    struct vcpu *curr;
     struct vpmu_struct *vpmu;
+    struct arch_vpmu_ops *ops;

const

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

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +3 -1
+    int ret = 0;
 
     if ( !(vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV)) )
-        return 0;
+        goto nop;
 
+    curr = current;

This should remain the initializer of the variable.

Quoted 63 lines
xen/arch/x86/hvm/vpmu.c +21 -36
     vpmu = vcpu_vpmu(curr);
-    if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_wrmsr )
-    {
-        int ret = vpmu->arch_vpmu_ops->do_wrmsr(msr, msr_content, supported);
-
-        /*
-         * 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_vcpu(curr) && vpmu->xenpmu_data &&
-             (vpmu->xenpmu_data->pmu.pmu_flags & PMU_CACHED) )
-        {
-            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
-            vpmu->arch_vpmu_ops->arch_vpmu_save(vpmu);
-            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
-        }
-        return ret;
-    }
-
-    return 0;
-}
-
-int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
-{
-    struct vcpu *curr = current;
-    struct vpmu_struct *vpmu;
+    ops = vpmu->arch_vpmu_ops;
+    if ( !ops )
+        goto nop;
+
+    if ( is_write && ops->do_wrmsr )
+        ret = ops->do_wrmsr(msr, *msr_content, supported);
+    else if ( !is_write && ops->do_rdmsr )
+        ret = ops->do_rdmsr(msr, msr_content);
+    else
+        goto nop;
 
-    if ( !(vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV)) )
+    /*
+     * We may have received a PMU interrupt while handling MSR access
+     * and since do_wr/rdmsr may load VPMU context we should save
+     * (and unload) it again.
+     */
+    if ( !is_hvm_vcpu(curr) &&
+         vpmu->xenpmu_data && (vpmu->xenpmu_data->pmu.pmu_flags & PMU_CACHED) )
     {
-        *msr_content = 0;
-        return 0;
+        vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
+        ops->arch_vpmu_save(vpmu);
+        vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
     }
 
-    vpmu = vcpu_vpmu(curr);
-    if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->do_rdmsr )
-    {
-        int ret = vpmu->arch_vpmu_ops->do_rdmsr(msr, msr_content);
+    return ret;
 
-        if ( !is_hvm_vcpu(curr) && vpmu->xenpmu_data &&
-             (vpmu->xenpmu_data->pmu.pmu_flags & PMU_CACHED) )
-        {

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +2 -7
-            vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
-            vpmu->arch_vpmu_ops->arch_vpmu_save(vpmu);
-            vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
-        }
-        return ret;
-    }
-    else
+ nop:
+    if ( !is_write )
         *msr_content = 0;

This could easily be done at the beginning of the function, avoiding the need for the (kind of odd) "nop" label.

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

Quoted 2 lines
xen/arch/x86/hvm/vpmu.c +0 -1
-
     return 0;

Please don't delete blank lines before return statements.

With those changes

Jan

#29 lore raw

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

In reply to Boris Ostrovsky

Add support for privileged PMU mode (XENPMU_MODE_ALL) 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  | 40 ++++++++++++++++++++++++++++++----------
 xen/arch/x86/traps.c     | 12 ++++++++++++
 xen/include/public/pmu.h |  3 +++
 3 files changed, 45 insertions(+), 10 deletions(-)
xen/arch/x86/hvm/vpmu.c +30 -10
index bf9f5a8..651cb00 100644
@@ -108,7 +108,9 @@ int vpmu_do_msr(unsigned int msr, uint64_t *msr_content,
     struct arch_vpmu_ops *ops;
     int ret = 0;
 
-    if ( !(vpmu_mode & (XENPMU_MODE_SELF | XENPMU_MODE_HV)) )
+    if ( (vpmu_mode == XENPMU_MODE_OFF) ||
+         ((vpmu_mode & XENPMU_MODE_ALL) &&
+         !is_hardware_domain(current->domain)) )
         goto nop;
 
     curr = current;
@@ -160,8 +162,12 @@ void vpmu_do_interrupt(struct cpu_user_regs *regs)
     struct vcpu *sampled = current, *sampling;
     struct vpmu_struct *vpmu;
 
-    /* dom0 will handle interrupt for special domains (e.g. idle domain) */
-    if ( sampled->domain->domain_id >= DOMID_FIRST_RESERVED )
+    /*
+     * dom0 will handle interrupt for special domains (e.g. idle domain) or,
+     * in XENPMU_MODE_ALL, for everyone.
+     */
+    if ( (vpmu_mode & XENPMU_MODE_ALL) ||
+         (sampled->domain->domain_id >= DOMID_FIRST_RESERVED) )
     {
         sampling = choose_hwdom_vcpu();
         if ( !sampling )
@@ -171,17 +177,18 @@ void vpmu_do_interrupt(struct cpu_user_regs *regs)
         sampling = sampled;
 
     vpmu = vcpu_vpmu(sampling);
-    if ( !is_hvm_vcpu(sampling) )
+    if ( !is_hvm_vcpu(sampling) || (vpmu_mode & XENPMU_MODE_ALL) )
     {
         /* PV(H) guest */
         const struct cpu_user_regs *cur_regs;
         uint64_t *flags = &vpmu->xenpmu_data->pmu.pmu_flags;
-        uint32_t domid = DOMID_SELF;
+        uint32_t domid;
 
         if ( !vpmu->xenpmu_data )
             return;
 
         if ( is_pvh_vcpu(sampling) &&
+             !(vpmu_mode & XENPMU_MODE_ALL) &&
              !vpmu->arch_vpmu_ops->do_interrupt(regs) )
             return;
 
@@ -198,6 +205,11 @@ void vpmu_do_interrupt(struct cpu_user_regs *regs)
         else
             *flags = PMU_SAMPLE_PV;
 
+        if ( sampled == sampling )
+            domid = DOMID_SELF;
+        else
+            domid = sampled->domain->domain_id;
+
         /* Store appropriate registers in xenpmu_data */
         /* FIXME: 32-bit PVH should go here as well */
         if ( is_pv_32bit_vcpu(sampling) )
@@ -226,7 +238,8 @@ void vpmu_do_interrupt(struct cpu_user_regs *regs)
 
             if ( (vpmu_mode & XENPMU_MODE_SELF) )
                 cur_regs = guest_cpu_user_regs();
-            else if ( !guest_mode(regs) && is_hardware_domain(sampling->domain) )
+            else if ( !guest_mode(regs) &&
+                      is_hardware_domain(sampling->domain) )
             {
                 cur_regs = regs;
                 domid = DOMID_XEN;
@@ -481,7 +494,8 @@ static int pvpmu_init(struct domain *d, xen_pmu_params_t *params)
     struct page_info *page;
     uint64_t gfn = params->val;
 
-    if ( vpmu_mode == XENPMU_MODE_OFF )
+    if ( (vpmu_mode == XENPMU_MODE_OFF) ||
+         ((vpmu_mode & XENPMU_MODE_ALL) && !is_hardware_domain(d)) )
         return -EINVAL;
 
     if ( (params->vcpu >= d->max_vcpus) || (d->vcpu == NULL) ||
@@ -564,6 +578,10 @@ void vpmu_dump(struct vcpu *v)
 
 void vpmu_unload(struct vpmu_struct *vpmu)
 {
+    if ( (vpmu_mode & XENPMU_MODE_ALL) &&
+         is_hardware_domain(vpmu_vcpu(vpmu)->domain) )
+        return;
+
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED | VPMU_RUNNING) )
         return;
 
@@ -700,11 +718,13 @@ long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
         unsigned int old_mode;
         static DEFINE_SPINLOCK(xenpmu_mode_lock);
 
-        if ( pmu_params.val & ~(XENPMU_MODE_SELF | XENPMU_MODE_HV) )
+        if ( pmu_params.val & ~(XENPMU_MODE_SELF | XENPMU_MODE_HV |
+                                XENPMU_MODE_ALL) )
             return -EINVAL;
 
         /* 32-bit dom0 can only sample itself. */
-        if ( is_pv_32bit_vcpu(current) && (pmu_params.val & XENPMU_MODE_HV) )
+        if ( is_pv_32bit_vcpu(current) &&
+             (pmu_params.val & (XENPMU_MODE_HV | XENPMU_MODE_ALL)) )
             return -EINVAL;
 
         /*
@@ -723,7 +743,7 @@ long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
         old_mode = vpmu_mode;
         vpmu_mode = pmu_params.val;
 
-        if ( vpmu_mode == XENPMU_MODE_OFF )
+        if ( (vpmu_mode == XENPMU_MODE_OFF) || (vpmu_mode == XENPMU_MODE_ALL) )
         {
             /* Make sure all (non-dom0) VCPUs have unloaded their VPMUs. */
             ret = vpmu_unload_all(old_mode);
xen/arch/x86/traps.c +12 -0
index 97fadbd..22afc71 100644
@@ -2651,6 +2651,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_ALL) &&
+                         !is_hardware_domain(v->domain) )
+                        break;
+
                     if ( vpmu_do_wrmsr(regs->ecx, msr_content, 0) )
                         goto fail;
                 }
@@ -2774,6 +2778,14 @@ 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_ALL) &&
+                         !is_hardware_domain(v->domain) )
+                    {
+                        /* Don't leak PMU MSRs to unprivileged domains */
+                        regs->eax = regs->edx = 0;
+                        break;
+                    }
+
                     if ( vpmu_do_rdmsr(regs->ecx, &msr_content) )
                         goto fail;
 
xen/include/public/pmu.h +3 -0
index db5321a..a83ae71 100644
@@ -52,10 +52,13 @@ DEFINE_XEN_GUEST_HANDLE(xen_pmu_params_t);
  * - XENPMU_MODE_SELF:  Guests can profile themselves
  * - XENPMU_MODE_HV:    Guests can profile themselves, dom0 profiles
  *                      itself and Xen
+ * - XENPMU_MODE_ALL:   Only dom0 has access to VPMU and it profiles
+ *                      everyone: itself, the hypervisor and the guests.
  */
 #define XENPMU_MODE_OFF           0
 #define XENPMU_MODE_SELF          (1<<0)
 #define XENPMU_MODE_HV            (1<<1)
+#define XENPMU_MODE_ALL           (1<<2)
 
 /*
  * PMU features:
Signature · 1 line
1.8.1.4
#30 lore raw

PATCH v18 14/16 x86/VPMU: NMI-based VPMU support

In reply to Boris Ostrovsky

Add support for using NMIs as PMU interrupts to allow profiling hypervisor when interrupts are disabled.

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()

The latter two only access PMU MSRs with {rd,wr}msrl() (not the _safe versions which would not be NMI-safe).

---
 docs/misc/xen-command-line.markdown |   8 +-
 xen/arch/x86/hvm/svm/vpmu.c         |   3 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c   |   3 +-
 xen/arch/x86/hvm/vpmu.c             | 229 ++++++++++++++++++++++++++++--------
 xen/include/asm-x86/hvm/vpmu.h      |   4 +-
 xen/include/asm-x86/softirq.h       |   3 +-
 6 files changed, 193 insertions(+), 57 deletions(-)
docs/misc/xen-command-line.markdown +6 -2
index bc316be..0ab1188 100644
@@ -1330,11 +1330,11 @@ Use Virtual Processor ID support if available.  This prevents the need for TLB
 flushes on VM entry and exit, increasing performance.
 
 ### vpmu
-> `= ( bts )`
+> `= ( [nmi,][bts] )`
 
 > Default: `off`
 
-Switch on the virtualized performance monitoring unit for HVM guests.
+Switch on the virtualized performance monitoring unit.
 
 If the current cpu isn't supported a message like
 'VPMU: Initialization failed. ...'
@@ -1348,6 +1348,10 @@ feature is switched on on Intel processors supporting this feature.
 
 Note that if **watchdog** option is also specified vpmu will be turned off.
 
+If 'vpmu=nmi' is specified the PMU interrupt will cause an NMI instead of a
+regular vector interrupt (which is the default). This can be useful for sampling
+hypervisor code that is executed with interrupts disabled.
+
 *Warning:*
 As the BTS virtualisation is not 100% safe and because of the nehalem quirk
 don't use the vpmu flag on production systems with Intel cpus!
xen/arch/x86/hvm/svm/vpmu.c +2 -1
index 68113c7..7ddce33 100644
@@ -168,7 +168,7 @@ static void amd_vpmu_unset_msr_bitmap(struct vcpu *v)
     msr_bitmap_off(vpmu);
 }
 
-static int amd_vpmu_do_interrupt(struct cpu_user_regs *regs)
+static int amd_vpmu_do_interrupt(const struct cpu_user_regs *regs)
 {
     return 1;
 }
@@ -220,6 +220,7 @@ static inline void context_save(struct vpmu_struct *vpmu)
         rdmsrl(counters[i], counter_regs[i]);
 }
 
+/* Must be NMI-safe */
 static int amd_vpmu_save(struct vpmu_struct *vpmu)
 {
     struct vcpu *v;
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -1
index 8067d83..0c7fd74 100644
@@ -305,6 +305,7 @@ static inline void __core2_vpmu_save(struct vpmu_struct *vpmu)
         rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, core2_vpmu_cxt->global_status);
 }
 
+/* Must be NMI-safe */
 static int core2_vpmu_save(struct vpmu_struct *vpmu)
 {
     struct vcpu *v = vpmu_vcpu(vpmu);
@@ -720,7 +721,7 @@ static void core2_vpmu_dump(const struct vcpu *v)
     }
 }
 
-static int core2_vpmu_do_interrupt(struct cpu_user_regs *regs)
+static int core2_vpmu_do_interrupt(const struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
     u64 msr_content;
xen/arch/x86/hvm/vpmu.c +179 -50

358 lines not on this page · raw message

xen/include/asm-x86/hvm/vpmu.h +2 -2
index 2c888cc..ed5dc8c 100644
@@ -53,7 +53,7 @@ struct arch_vpmu_ops {
     int (*do_wrmsr)(unsigned int msr, uint64_t msr_content,
                     uint64_t supported);
     int (*do_rdmsr)(unsigned int msr, uint64_t *msr_content);
-    int (*do_interrupt)(struct cpu_user_regs *regs);
+    int (*do_interrupt)(const struct cpu_user_regs *regs);
     void (*do_cpuid)(unsigned int input,
                      unsigned int *eax, unsigned int *ebx,
                      unsigned int *ecx, unsigned int *edx);
@@ -102,7 +102,7 @@ static inline bool_t vpmu_are_all_set(const struct vpmu_struct *vpmu,
 void vpmu_lvtpc_update(uint32_t val);
 int vpmu_do_msr(unsigned int msr, uint64_t *msr_content,
                 uint64_t supported, bool_t is_write);
-void vpmu_do_interrupt(struct cpu_user_regs *regs);
+int vpmu_do_interrupt(const struct cpu_user_regs *regs);
 void vpmu_do_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                                        unsigned int *ecx, unsigned int *edx);
 void vpmu_initialise(struct vcpu *v);
xen/include/asm-x86/softirq.h +2 -1
index ec787d6..fca110f 100644
@@ -8,7 +8,8 @@
 #define MACHINE_CHECK_SOFTIRQ  (NR_COMMON_SOFTIRQS + 3)
 #define PCI_SERR_SOFTIRQ       (NR_COMMON_SOFTIRQS + 4)
 #define HVM_DPCI_SOFTIRQ       (NR_COMMON_SOFTIRQS + 5)
-#define NR_ARCH_SOFTIRQS       6
+#define PMU_SOFTIRQ            (NR_COMMON_SOFTIRQS + 6)
+#define NR_ARCH_SOFTIRQS       7
 
 bool_t arch_skip_send_event_check(unsigned int cpu);
 
Signature · 1 line
1.8.1.4
#31 lore raw

Re: [PATCH v18 14/16] x86/VPMU: NMI-based VPMU support

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:

+int pmu_nmi_interrupt(const struct cpu_user_regs *regs, int cpu)

static

Review

Quoted 2 lines
(quoted diff) +2 -0
+{
+    return vpmu_do_interrupt(regs);

That function returning 1 makes do_nmi() not do _anything_ else, i.e. ignore eventual SERR or IOCHK events. That's not acceptable. I guess you'll need to make nmi_callback() return values tristate and adjust do_nmi() to deal with both cases. Albeit - the other two users are breaking this too, so perhaps do_nmi() should be adjusted in a prereq (and backportable) patch to not bail in that case. Yet a tristate may still be needed, since alternative_instructions() legitimately wants to suppress them getting handled - it would just need to invoke their handling once done with patching.

Jan

#32 lore raw

Re: [PATCH v18 14/16] x86/VPMU: NMI-based VPMU support

Quoted 6 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:

+int pmu_nmi_interrupt(const struct cpu_user_regs *regs, int cpu)

static

Review

Quoted 2 lines
(quoted diff) +2 -0
+{
+    return vpmu_do_interrupt(regs);
Quoted 9 lines

That function returning 1 makes do_nmi() not do _anything_ else, i.e. ignore eventual SERR or IOCHK events. That's not acceptable. I guess you'll need to make nmi_callback() return values tristate and adjust do_nmi() to deal with both cases. Albeit - the other two users are breaking this too, so perhaps do_nmi() should be adjusted in a prereq (and backportable) patch to not bail in that case. Yet a tristate may still be needed, since alternative_instructions() legitimately wants to suppress them getting handled - it would just need to invoke their handling once done with patching.

I think I will separate this patch from the series then and submit it later.

-boris

#33 lore raw

PATCH v18 15/16 x86/VPMU: VPMU should not exist when vpmu_initialise() is called

In reply to Boris Ostrovsky

We don't need to try to destroy it since it can't be already allocated at the time we try to initialize it.

---
 xen/arch/x86/hvm/vpmu.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/vpmu.c +2 -4
index cdead13..fa34127 100644
@@ -480,10 +480,8 @@ void vpmu_initialise(struct vcpu *v)
     BUILD_BUG_ON(sizeof(struct xen_pmu_regs) > XENPMU_REGS_PAD_SZ);
     BUILD_BUG_ON(sizeof(struct compat_pmu_regs) > XENPMU_REGS_PAD_SZ);
 
-    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
-        vpmu_destroy(v);
-    vpmu_clear(vpmu);
-    vpmu->context = NULL;
+    ASSERT(!vpmu->flags && !vpmu->context);
+
     vpmu->hw_lapic_lvtpc = vpmu_interrupt_type | APIC_LVT_MASKED;
 
     switch ( vendor )
Signature · 1 line
1.8.1.4
#34 lore raw

Re: [PATCH v18 15/16] x86/VPMU: VPMU should not exist when vpmu_initialise() is called

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 16.02.15 at 23:26, <[email protected]> wrote:

We don't need to try to destroy it since it can't be already allocated at the time we try to initialize it.

---
 xen/arch/x86/hvm/vpmu.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/vpmu.c +0 -0
index cdead13..fa34127 100644

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

Quoted 9 lines
xen/arch/x86/hvm/vpmu.c +2 -4
@@ -480,10 +480,8 @@ void vpmu_initialise(struct vcpu *v)
     BUILD_BUG_ON(sizeof(struct xen_pmu_regs) > XENPMU_REGS_PAD_SZ);
     BUILD_BUG_ON(sizeof(struct compat_pmu_regs) > XENPMU_REGS_PAD_SZ);
 
-    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
-        vpmu_destroy(v);
-    vpmu_clear(vpmu);
-    vpmu->context = NULL;
+    ASSERT(!vpmu->flags && !vpmu->context);
+

If this is legitimately a separate patch, it would seem to belong at the beginning of the series. Otherwise it should be integrated into whatever patch arranges for this no longer being possible.

Jan

#35 lore raw

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

In reply to Boris Ostrovsky

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

Specifically:

    arch/x86/hvm/vpmu.c -> arch/x86/cpu/vpmu.c
    arch/x86/hvm/svm/vpmu.c -> arch/x86/cpu/vpmu_amd.c
    arch/x86/hvm/vmx/vpmu_core2.c -> arch/x86/cpu/vpmu_intel.c
    include/asm-x86/hvm/vpmu.h -> include/asm-x86/vpmu.h
---
 xen/arch/x86/cpu/Makefile                               | 1 +
 xen/arch/x86/{hvm => cpu}/vpmu.c                        | 2 +-
 xen/arch/x86/{hvm/svm/vpmu.c => cpu/vpmu_amd.c}         | 2 +-
 xen/arch/x86/{hvm/vmx/vpmu_core2.c => cpu/vpmu_intel.c} | 2 +-
 xen/arch/x86/hvm/Makefile                               | 1 -
 xen/arch/x86/hvm/svm/Makefile                           | 1 -
 xen/arch/x86/hvm/vlapic.c                               | 2 +-
 xen/arch/x86/hvm/vmx/Makefile                           | 1 -
 xen/arch/x86/oprofile/op_model_ppro.c                   | 2 +-
 xen/arch/x86/traps.c                                    | 2 +-
 xen/include/asm-x86/hvm/vmx/vmcs.h                      | 2 +-
 xen/include/asm-x86/{hvm => }/vpmu.h                    | 0
 12 files changed, 8 insertions(+), 10 deletions(-)
 rename xen/arch/x86/{hvm => cpu}/vpmu.c (99%)
 rename xen/arch/x86/{hvm/svm/vpmu.c => cpu/vpmu_amd.c} (99%)
 rename xen/arch/x86/{hvm/vmx/vpmu_core2.c => cpu/vpmu_intel.c} (99%)
 rename xen/include/asm-x86/{hvm => }/vpmu.h (100%)
xen/arch/x86/cpu/Makefile +1 -0
index d73d93a..74f23ae 100644
@@ -7,3 +7,4 @@ obj-y += common.o
 obj-y += intel.o
 obj-y += intel_cacheinfo.o
 obj-y += mwait-idle.o
+obj-y += vpmu.o vpmu_amd.o vpmu_intel.o
xen/arch/x86/cpu/vpmu.c renamed from xen/arch/x86/hvm/vpmu.c +1 -1
similarity index 99%
rename from xen/arch/x86/hvm/vpmu.c
rename to xen/arch/x86/cpu/vpmu.c
index fa34127..affd5b8 100644
@@ -28,10 +28,10 @@
 #include <asm/msr.h>
 #include <asm/nmi.h>
 #include <asm/p2m.h>
+#include <asm/vpmu.h>
 #include <asm/hvm/support.h>
 #include <asm/hvm/vmx/vmx.h>
 #include <asm/hvm/vmx/vmcs.h>
-#include <asm/hvm/vpmu.h>
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
xen/arch/x86/cpu/vpmu_amd.c renamed from xen/arch/x86/hvm/svm/vpmu.c +1 -1
similarity index 99%
rename from xen/arch/x86/hvm/svm/vpmu.c
rename to xen/arch/x86/cpu/vpmu_amd.c
index 7ddce33..35b0b75 100644
@@ -28,8 +28,8 @@
 #include <xen/sched.h>
 #include <xen/irq.h>
 #include <asm/apic.h>
+#include <asm/vpmu.h>
 #include <asm/hvm/vlapic.h>
-#include <asm/hvm/vpmu.h>
 #include <public/pmu.h>
 
 #define MSR_F10H_EVNTSEL_GO_SHIFT   40
xen/arch/x86/cpu/vpmu_intel.c renamed from xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -1
similarity index 99%
rename from xen/arch/x86/hvm/vmx/vpmu_core2.c
rename to xen/arch/x86/cpu/vpmu_intel.c
index 0c7fd74..8c6349e 100644
@@ -30,6 +30,7 @@
 #include <asm/traps.h>
 #include <asm/msr.h>
 #include <asm/msr-index.h>
+#include <asm/vpmu.h>
 #include <asm/hvm/support.h>
 #include <asm/hvm/vlapic.h>
 #include <asm/hvm/vmx/vmx.h>
@@ -37,7 +38,6 @@
 #include <public/sched.h>
 #include <public/hvm/save.h>
 #include <public/pmu.h>
-#include <asm/hvm/vpmu.h>
 
 /*
  * See Intel SDM Vol 2a Instruction Set Reference chapter 3 for CPUID
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/vlapic.c +1 -1
index 5da6d8f..b25ca8a 100644
@@ -33,12 +33,12 @@
 #include <asm/page.h>
 #include <asm/apic.h>
 #include <asm/io_apic.h>
+#include <asm/vpmu.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/io.h>
 #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>
 
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/oprofile/op_model_ppro.c +1 -1
index ca429a1..89649d0 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 22afc71..806ecbf 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/include/asm-x86/hvm/vmx/vmcs.h +1 -1
index f05e2b5..83407af 100644
@@ -19,8 +19,8 @@
 #ifndef __ASM_X86_HVM_VMX_VMCS_H__
 #define __ASM_X86_HVM_VMX_VMCS_H__
 
+#include <asm/vpmu.h>
 #include <asm/hvm/io.h>
-#include <asm/hvm/vpmu.h>
 #include <irq_vectors.h>
 
 extern void vmcs_dump_vcpu(struct vcpu *v);
xen/include/asm-x86/vpmu.h renamed from xen/include/asm-x86/hvm/vpmu.h +0 -0
similarity index 100%
rename from xen/include/asm-x86/hvm/vpmu.h
rename to xen/include/asm-x86/vpmu.h
Signature · 1 line
1.8.1.4
#36 lore raw
In reply to Boris Ostrovsky
Quoted 19 lines · Boris Ostrovsky

Changes in v18:

* Return 1 (i.e. "handled") in vpmu_do_interrupt() if PMU_CACHED is set. This is needed since we can get an interrupt while this flag is set on AMD processors when multiple counters are in use (**) (AMD processor don't mask LVTPC when PMC interrupt happens and so there is a window in vpmu_do_interrupt() until it sets the mask bit). Patch #14
* Unload both current and last_vcpu (if different) vpmu and clear this_cpu(last_vcpu) in vpmu_unload_all. Patch #5
* Make major version check for certain xenpmu_ops. Patch #5
* Make xenpmu_op()'s first argument unsigned. Patch #5
* Don't use format specifier for __stringify(). Patch #6
* Don't print generic error in vpmu_init(). Patch #6
* Don't test for VPMU existance in vpmu_initialise(). New patch #15
* Added vpmu_disabled flag to make sure VPMU doesn't get reenabled from dom0 (for example when watchdog is active). Patch #5
* Updated tags on some patches to better reflect latest reviewed status)

Quoted 6 lines

(**) While testing this I discovered that AMD VPMU is quite broken for
HVM: when multiple counters are in use linux dom0 often gets unexpected NMIs. This may have something to do with what I mentioned in the first bullet. However, this doesn't appear to be related to this patch series (or earlier VPMU patches) --- I can reproduce this all the way back to 4.1

Do you have a public git tree with this in?

~Andrew

#37 lore raw
Quoted 20 lines · Andrew Cooper
Quoted 19 lines · Boris Ostrovsky

Changes in v18:

* Return 1 (i.e. "handled") in vpmu_do_interrupt() if PMU_CACHED is set. This is needed since we can get an interrupt while this flag is set on AMD processors when multiple counters are in use (**) (AMD processor don't mask LVTPC when PMC interrupt happens and so there is a window in vpmu_do_interrupt() until it sets the mask bit). Patch #14
* Unload both current and last_vcpu (if different) vpmu and clear this_cpu(last_vcpu) in vpmu_unload_all. Patch #5
* Make major version check for certain xenpmu_ops. Patch #5
* Make xenpmu_op()'s first argument unsigned. Patch #5
* Don't use format specifier for __stringify(). Patch #6
* Don't print generic error in vpmu_init(). Patch #6
* Don't test for VPMU existance in vpmu_initialise(). New patch #15
* Added vpmu_disabled flag to make sure VPMU doesn't get reenabled from dom0 (for example when watchdog is active). Patch #5
* Updated tags on some patches to better reflect latest reviewed status)

Quoted 7 lines
Quoted 6 lines

(**) While testing this I discovered that AMD VPMU is quite broken for
HVM: when multiple counters are in use linux dom0 often gets unexpected NMIs. This may have something to do with what I mentioned in the first bullet. However, this doesn't appear to be related to this patch series (or earlier VPMU patches) --- I can reproduce this all the way back to 4.1

Do you have a public git tree with this in?

Sigh.. no. I tried setting it up on our public site but failed miserably.

I can try again if this would be helpful (you'll need Linux changes as well).

-boris

#38 lore raw
Quoted 28 lines · Boris Ostrovsky
Quoted 27 lines · Andrew Cooper
Quoted 26 lines · Boris Ostrovsky

Changes in v18:

* Return 1 (i.e. "handled") in vpmu_do_interrupt() if PMU_CACHED is set. This is needed since we can get an interrupt while this flag is set on AMD processors when multiple counters are in use (**) (AMD processor don't mask LVTPC when PMC interrupt happens and so there is a window in vpmu_do_interrupt() until it sets the mask bit). Patch #14
* Unload both current and last_vcpu (if different) vpmu and clear this_cpu(last_vcpu) in vpmu_unload_all. Patch #5
* Make major version check for certain xenpmu_ops. Patch #5
* Make xenpmu_op()'s first argument unsigned. Patch #5
* Don't use format specifier for __stringify(). Patch #6
* Don't print generic error in vpmu_init(). Patch #6
* Don't test for VPMU existance in vpmu_initialise(). New patch #15
* Added vpmu_disabled flag to make sure VPMU doesn't get reenabled from dom0 (for example when watchdog is active). Patch #5
* Updated tags on some patches to better reflect latest reviewed status)

(**) While testing this I discovered that AMD VPMU is quite broken for
HVM: when multiple counters are in use linux dom0 often gets unexpected NMIs. This may have something to do with what I mentioned in the first bullet. However, this doesn't appear to be related to this patch series (or earlier VPMU patches) --- I can reproduce this all the way back to 4.1

Quoted 9 lines
Quoted 1 line

Do you have a public git tree with this in?

Sigh.. no. I tried setting it up on our public site but failed miserably.

I can try again if this would be helpful (you'll need Linux changes as well).

-boris

You could see about getting an account on xenbits. There is some formal way of requesting an account but I can't currently spot where the instructions are.

(I was just looking to see whether there was an easy way of getting at the series, as pulling it out of emails is moderately difficult. I can cope without.)

~Andrew