Skip to contentSkip to conversation

x86/PMU: Xen PMU PV support

PATCH Superseded Boris Ostrovsky 6 participants 51 messages Sep 20, 2013 – Sep 30, 2013

v2 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86/PMU: Xen PMU PV support

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

Linux patches will be updated later.

===================================================================

Boris Ostrovsky (13):
Export hypervisor symbols
Set VCPU's is_running flag closer to when the VCPU is dispatched x86/PMU: Stop AMD counters when called from vpmu_save_force() x86/VPMU: Minor VPMU cleanup
intel/VPMU: Clean up Intel VPMU code
x86/PMU: Add public xenpmu.h
x86/PMU: Make vpmu not HVM-specific
x86/PMU: Interface for setting PMU mode and flags x86/PMU: Initialize PMU for PV guests
x86/PMU: Add support for PMU registes handling on PV guests x86/PMU: Handle PMU interrupts for PV guests
x86/PMU: Save VPMU state for PV guests during context switch x86/PMU: Move vpmu files up from hvm directory

 xen/arch/arm/domain.c                          |   1 +
 xen/arch/x86/Makefile                          |   1 +
 xen/arch/x86/apic.c                            |  13 -
 xen/arch/x86/domain.c                          |  18 +-
 xen/arch/x86/hvm/Makefile                      |   1 -
 xen/arch/x86/hvm/svm/Makefile                  |   1 -
 xen/arch/x86/hvm/svm/entry.S                   |   2 +
 xen/arch/x86/hvm/svm/vpmu.c                    | 494 -------------
 xen/arch/x86/hvm/vmx/Makefile                  |   1 -
 xen/arch/x86/hvm/vmx/entry.S                   |   1 +
 xen/arch/x86/hvm/vmx/vmcs.c                    |  59 ++
 xen/arch/x86/hvm/vmx/vpmu_core2.c              | 894 -----------------------
 xen/arch/x86/hvm/vpmu.c                        | 266 -------
 xen/arch/x86/oprofile/op_model_ppro.c          |   8 +-
 xen/arch/x86/platform_hypercall.c              |   9 +
 xen/arch/x86/traps.c                           |  39 +-
 xen/arch/x86/vpmu.c                            | 549 +++++++++++++++
 xen/arch/x86/vpmu_amd.c                        | 489 +++++++++++++
 xen/arch/x86/vpmu_intel.c                      | 936 +++++++++++++++++++++++++
 xen/arch/x86/x86_64/asm-offsets.c              |   1 +
 xen/arch/x86/x86_64/compat/entry.S             |   4 +
 xen/arch/x86/x86_64/entry.S                    |   4 +
 xen/arch/x86/x86_64/platform_hypercall.c       |   2 +-
 xen/common/event_channel.c                     |   1 +
 xen/common/schedule.c                          |   8 +-
 xen/common/symbols-dummy.c                     |   1 +
 xen/common/symbols.c                           |  58 +-
 xen/include/asm-x86/domain.h                   |   3 +
 xen/include/asm-x86/hvm/vcpu.h                 |   3 -
 xen/include/asm-x86/hvm/vmx/vmcs.h             |   3 +-
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h       |  51 --
 xen/include/asm-x86/hvm/vpmu.h                 | 104 ---
 xen/include/asm-x86/irq.h                      |   1 -
 xen/include/asm-x86/mach-default/irq_vectors.h |   1 -
 xen/include/asm-x86/vpmu.h                     |  96 +++
 xen/include/public/arch-x86/xenpmu-x86.h       |  62 ++
 xen/include/public/platform.h                  |  22 +
 xen/include/public/xen.h                       |   2 +
 xen/include/public/xenpmu.h                    |  95 +++
 xen/include/xen/hypercall.h                    |   4 +
 xen/include/xen/softirq.h                      |   1 +
 xen/include/xen/symbols.h                      |   4 +
 xen/tools/symbols.c                            |   4 +
 43 files changed, 2467 insertions(+), 1850 deletions(-)
 delete mode 100644 xen/arch/x86/hvm/svm/vpmu.c
 delete mode 100644 xen/arch/x86/hvm/vmx/vpmu_core2.c
 delete mode 100644 xen/arch/x86/hvm/vpmu.c
 create mode 100644 xen/arch/x86/vpmu.c
 create mode 100644 xen/arch/x86/vpmu_amd.c
 create mode 100644 xen/arch/x86/vpmu_intel.c
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 delete mode 100644 xen/include/asm-x86/hvm/vpmu.h
 create mode 100644 xen/include/asm-x86/vpmu.h
 create mode 100644 xen/include/public/arch-x86/xenpmu-x86.h
 create mode 100644 xen/include/public/xenpmu.h
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v2 1/13 Export hypervisor symbols

Export Xen's symbols in format similar to Linux' /proc/kallsyms.

---
 xen/arch/x86/platform_hypercall.c        |  9 +++++
 xen/arch/x86/x86_64/platform_hypercall.c |  2 +-
 xen/common/symbols-dummy.c               |  1 +
 xen/common/symbols.c                     | 58 ++++++++++++++++++++++++++++++--
 xen/include/public/platform.h            | 22 ++++++++++++
 xen/include/xen/symbols.h                |  4 +++
 xen/tools/symbols.c                      |  4 +++
 7 files changed, 97 insertions(+), 3 deletions(-)
xen/arch/x86/platform_hypercall.c +9 -0
index 7175a82..39376fe 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>
@@ -597,6 +598,14 @@ ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
     }
     break;
 
+    case XENPF_get_symbols:
+    {
+        ret = xensyms_read(&op->u.symdata);
+        if ( ret >= 0 && __copy_field_to_guest(u_xenpf_op, op, u.symdata) )
+            ret = -EFAULT;
+    }
+    break;
+ 
     default:
         ret = -ENOSYS;
         break;
xen/arch/x86/x86_64/platform_hypercall.c +1 -1
index aa2ad54..9ef705a 100644
@@ -35,7 +35,7 @@ CHECK_pf_pcpu_version;
 #undef xen_pf_pcpu_version
 
 #define xenpf_enter_acpi_sleep compat_pf_enter_acpi_sleep
-
+#define xenpf_symdata	compat_pf_symdata
 #define COMPAT
 #define _XEN_GUEST_HANDLE(t) XEN_GUEST_HANDLE(t)
 #define _XEN_GUEST_HANDLE_PARAM(t) XEN_GUEST_HANDLE_PARAM(t)
xen/common/symbols-dummy.c +1 -0
index 5090c3b..52a86c7 100644
@@ -12,6 +12,7 @@ const unsigned int symbols_offsets[1];
 const unsigned long symbols_addresses[1];
 #endif
 const unsigned int symbols_num_syms;
+const unsigned long symbols_names_bytes;
 const u8 symbols_names[1];
 
 const u8 symbols_token_table[1];
xen/common/symbols.c +56 -2

91 lines not on this page · raw message

xen/include/public/platform.h +22 -0
index 4341f54..870e14b 100644
@@ -527,6 +527,27 @@ struct xenpf_core_parking {
 typedef struct xenpf_core_parking xenpf_core_parking_t;
 DEFINE_XEN_GUEST_HANDLE(xenpf_core_parking_t);
 
+#define XENPF_get_symbols  61
+
+struct xenpf_symdata {
+    /* IN variables */
+    uint64_t xen_symnum;
+
+    /* OUT variables */
+    uint64_t address;
+    uint64_t type;
+    /*
+     * KSYM_NAME_LEN is 128 bytes. However, we cannot be larger than pad in
+     * xen_platform_op below (which is 128 bytes as well). Since the largest
+     * symbol is around 50 bytes it's probably more trouble than it's worth
+     * to try to deal with symbols that are close to 128 bytes in length.
+     */
+#define XEN_SYMS_MAX_LEN (128 - 3 * 8)
+    char name[XEN_SYMS_MAX_LEN];
+};
+typedef struct xenpf_symdata xenpf_symdata_t;
+DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t);
+
 /*
  * ` enum neg_errnoval
  * ` HYPERVISOR_platform_op(const struct xen_platform_op*);
@@ -553,6 +574,7 @@ struct xen_platform_op {
         struct xenpf_cpu_hotadd        cpu_add;
         struct xenpf_mem_hotadd        mem_add;
         struct xenpf_core_parking      core_parking;
+        struct xenpf_symdata           symdata;
         uint8_t                        pad[128];
     } u;
 };
xen/include/xen/symbols.h +4 -0
index 37cf6bf..c8df28f 100644
@@ -2,6 +2,8 @@
 #define _XEN_SYMBOLS_H
 
 #include <xen/types.h>
+#include <public/xen.h>
+#include <public/platform.h>
 
 #define KSYM_NAME_LEN 127
 
@@ -34,4 +36,6 @@ do {						\
 	__print_symbol(fmt, addr);		\
 } while(0)
 
+extern int xensyms_read(struct xenpf_symdata *symdata);
+
 #endif /*_XEN_SYMBOLS_H*/
xen/tools/symbols.c +4 -0
index f39c906..818204d 100644
@@ -272,6 +272,10 @@ static void write_src(void)
 	}
 	printf("\n");
 
+	output_label("symbols_names_bytes");
+	printf("\t.long\t%d\n", off);
+	printf("\n");
+
 	output_label("symbols_markers");
 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
 		printf("\t.long\t%d\n", markers[i]);
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [PATCH v2 01/13] Export hypervisor symbols

On Fri, Sep 20, 2013 at 05:42:00AM -0400, Boris Ostrovsky wrote:

I think the title needs a prefix of say: "xen/platform_op:" or such.

Quoted 1 line

Export Xen's symbols in format similar to Linux' /proc/kallsyms.

Which is?

I see in Linux:

000000000000bd58 D xen_cr3
...
000000000000e978 d soft_lockup_hrtimer_cnt
..
ffffffff8101cef0 T eager_fpu_init
ffffffff8101d010 t ptrace_triggered

I know that the first column is the EIP (thought some of them seem to be based on some offset). The last one is pretty obvious too.

But the middle one:

konrad@phenom:~$ cat /proc/kallsyms  | awk '{print $2}' | sort | uniq
b
B
d
D
r
R
t
T
V
W

Could you explain what those mean? And are they part of this? Or does Xen not carry those?

Quoted 248 lines
---
 xen/arch/x86/platform_hypercall.c        |  9 +++++
 xen/arch/x86/x86_64/platform_hypercall.c |  2 +-
 xen/common/symbols-dummy.c               |  1 +
 xen/common/symbols.c                     | 58 ++++++++++++++++++++++++++++++--
 xen/include/public/platform.h            | 22 ++++++++++++
 xen/include/xen/symbols.h                |  4 +++
 xen/tools/symbols.c                      |  4 +++
 7 files changed, 97 insertions(+), 3 deletions(-)
xen/arch/x86/platform_hypercall.c +9 -0
index 7175a82..39376fe 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>
@@ -597,6 +598,14 @@ ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
     }
     break;
 
+    case XENPF_get_symbols:
+    {
+        ret = xensyms_read(&op->u.symdata);
+        if ( ret >= 0 && __copy_field_to_guest(u_xenpf_op, op, u.symdata) )
+            ret = -EFAULT;
+    }
+    break;
+ 
     default:
         ret = -ENOSYS;
         break;
xen/arch/x86/x86_64/platform_hypercall.c +1 -1
index aa2ad54..9ef705a 100644
@@ -35,7 +35,7 @@ CHECK_pf_pcpu_version;
 #undef xen_pf_pcpu_version
 
 #define xenpf_enter_acpi_sleep compat_pf_enter_acpi_sleep
-
+#define xenpf_symdata	compat_pf_symdata
 #define COMPAT
 #define _XEN_GUEST_HANDLE(t) XEN_GUEST_HANDLE(t)
 #define _XEN_GUEST_HANDLE_PARAM(t) XEN_GUEST_HANDLE_PARAM(t)
xen/common/symbols-dummy.c +1 -0
index 5090c3b..52a86c7 100644
@@ -12,6 +12,7 @@ const unsigned int symbols_offsets[1];
 const unsigned long symbols_addresses[1];
 #endif
 const unsigned int symbols_num_syms;
+const unsigned long symbols_names_bytes;
 const u8 symbols_names[1];
 
 const u8 symbols_token_table[1];
xen/common/symbols.c +56 -2
index 83b2b58..e74a585 100644
@@ -17,6 +17,8 @@
 #include <xen/lib.h>
 #include <xen/string.h>
 #include <xen/spinlock.h>
+#include <public/platform.h>
+#include <xen/guest_access.h>
 
 #ifdef SYMBOLS_ORIGIN
 extern const unsigned int symbols_offsets[1];
@@ -26,6 +28,7 @@ extern const unsigned long symbols_addresses[];
 #define symbols_address(n) symbols_addresses[n]
 #endif
 extern const unsigned int symbols_num_syms;
+extern const unsigned long symbols_names_bytes;
 extern const u8 symbols_names[];
 
 extern const u8 symbols_token_table[];
@@ -35,7 +38,8 @@ extern const unsigned int symbols_markers[];
 
 /* expand a compressed symbol data into the resulting uncompressed string,
    given the offset to where the symbol is in the compressed stream */
-static unsigned int symbols_expand_symbol(unsigned int off, char *result)
+static unsigned int symbols_expand_symbol(unsigned int off, char *result,
+                                          int maxlen)
 {
     int len, skipped_first = 0;
     const u8 *tptr, *data;
@@ -49,6 +53,9 @@ static unsigned int symbols_expand_symbol(unsigned int off, char *result)
      * the compressed stream */
     off += len + 1;
 
+    if (maxlen < len)
+        len = maxlen;
+
     /* for every byte on the compressed symbol data, copy the table
        entry for that byte */
     while(len) {
@@ -129,7 +136,7 @@ const char *symbols_lookup(unsigned long addr,
         --low;
 
         /* Grab name */
-    symbols_expand_symbol(get_symbol_offset(low), namebuf);
+    symbols_expand_symbol(get_symbol_offset(low), namebuf, sizeof(namebuf));
 
     /* Search for next non-aliased symbol */
     for (i = low + 1; i < symbols_num_syms; i++) {
@@ -174,3 +181,50 @@ void __print_symbol(const char *fmt, unsigned long address)
 
     spin_unlock_irqrestore(&lock, flags);
 }
+
+/*
+ * Get symbol type information. This is encoded as a single char at the
+ * beginning of the symbol name.
+ */
+static char symbols_get_symbol_type(unsigned int off)
+{
+    /*
+     * Get just the first code, look it up in the token table,
+     * and return the first char from this token.
+     */
+    return symbols_token_table[symbols_token_index[symbols_names[off + 1]]];
+}
+
+/*
+ * Symbols are most likely accessed sequentially so we remember position from
+ * previous read. This can help us avoid extra call to get_symbol_offset().
+ */
+static uint64_t next_symbol, next_offset;
+static DEFINE_SPINLOCK(symbols_mutex);
+
+int xensyms_read(struct xenpf_symdata *symdata)
+{
+    if ( symdata->xen_symnum > symbols_num_syms )
+        return -EINVAL;
+    else if ( symdata->xen_symnum == symbols_num_syms )
+        return 0;
+
+    spin_lock(&symbols_mutex);
+
+    if ( symdata->xen_symnum == 0 )
+        next_offset = next_symbol = 0;
+    else if ( next_symbol != symdata->xen_symnum )
+        /* Non-sequential access */
+        next_offset = get_symbol_offset(symdata->xen_symnum);
+
+    symdata->type = symbols_get_symbol_type(next_offset);
+    next_offset = symbols_expand_symbol(next_offset, symdata->name,
+        sizeof(symdata->name));
+    symdata->address = symbols_offsets[symdata->xen_symnum] + SYMBOLS_ORIGIN;
+
+    next_symbol = symdata->xen_symnum + 1;
+
+    spin_unlock(&symbols_mutex);
+
+    return strlen(symdata->name);
+}
xen/include/public/platform.h +22 -0
index 4341f54..870e14b 100644
@@ -527,6 +527,27 @@ struct xenpf_core_parking {
 typedef struct xenpf_core_parking xenpf_core_parking_t;
 DEFINE_XEN_GUEST_HANDLE(xenpf_core_parking_t);
 
+#define XENPF_get_symbols  61
+
+struct xenpf_symdata {
+    /* IN variables */
+    uint64_t xen_symnum;
+
+    /* OUT variables */
+    uint64_t address;
+    uint64_t type;
+    /*
+     * KSYM_NAME_LEN is 128 bytes. However, we cannot be larger than pad in
+     * xen_platform_op below (which is 128 bytes as well). Since the largest
+     * symbol is around 50 bytes it's probably more trouble than it's worth
+     * to try to deal with symbols that are close to 128 bytes in length.
+     */
+#define XEN_SYMS_MAX_LEN (128 - 3 * 8)
+    char name[XEN_SYMS_MAX_LEN];
+};
+typedef struct xenpf_symdata xenpf_symdata_t;
+DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t);
+
 /*
  * ` enum neg_errnoval
  * ` HYPERVISOR_platform_op(const struct xen_platform_op*);
@@ -553,6 +574,7 @@ struct xen_platform_op {
         struct xenpf_cpu_hotadd        cpu_add;
         struct xenpf_mem_hotadd        mem_add;
         struct xenpf_core_parking      core_parking;
+        struct xenpf_symdata           symdata;
         uint8_t                        pad[128];
     } u;
 };
xen/include/xen/symbols.h +4 -0
index 37cf6bf..c8df28f 100644
@@ -2,6 +2,8 @@
 #define _XEN_SYMBOLS_H
 
 #include <xen/types.h>
+#include <public/xen.h>
+#include <public/platform.h>
 
 #define KSYM_NAME_LEN 127
 
@@ -34,4 +36,6 @@ do {						\
 	__print_symbol(fmt, addr);		\
 } while(0)
 
+extern int xensyms_read(struct xenpf_symdata *symdata);
+
 #endif /*_XEN_SYMBOLS_H*/
xen/tools/symbols.c +4 -0
index f39c906..818204d 100644
@@ -272,6 +272,10 @@ static void write_src(void)
 	}
 	printf("\n");
 
+	output_label("symbols_names_bytes");
+	printf("\t.long\t%d\n", off);
+	printf("\n");
+
 	output_label("symbols_markers");
 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
 		printf("\t.long\t%d\n", markers[i]);
Signature · 7 lines
1.8.1.4


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

Re: [PATCH v2 01/13] Export hypervisor symbols

Quoted 32 lines · Konrad Rzeszutek Wilk

On Fri, Sep 20, 2013 at 05:42:00AM -0400, Boris Ostrovsky wrote:

I think the title needs a prefix of say: "xen/platform_op:" or such.

Quoted 1 line

Export Xen's symbols in format similar to Linux' /proc/kallsyms.

Which is?

I see in Linux:

000000000000bd58 D xen_cr3
...
000000000000e978 d soft_lockup_hrtimer_cnt
..
ffffffff8101cef0 T eager_fpu_init
ffffffff8101d010 t ptrace_triggered

I know that the first column is the EIP (thought some of them seem to be based on some offset). The last one is pretty obvious too.

But the middle one:

konrad@phenom:~$ cat /proc/kallsyms  | awk '{print $2}' | sort | uniq
b
B
d
D
r
R
t
T
V
W
Quoted 2 lines

Could you explain what those mean? And are they part of this? Or does Xen not carry those?

These are symbol types, described in 'man nm'. For Xen we should only see 't' and 'T' which denote local or global text symbol.

So the format would be
<address> <type> <symbol name>

In any case, this is not a comlpetely correct commit message, it really describes to v1 implementation. With v2 Xen will return these three values in a structure, which has nothing to do with *format* of how these symbols are presented. I'll re-phrase it.

(And there other remnants of v1 in this patch that I just noticed, such as changes to xen/tools/symbols.c which are no longer needed)

-boris

Quoted 248 lines
Quoted 248 lines
---
  xen/arch/x86/platform_hypercall.c        |  9 +++++
  xen/arch/x86/x86_64/platform_hypercall.c |  2 +-
  xen/common/symbols-dummy.c               |  1 +
  xen/common/symbols.c                     | 58 ++++++++++++++++++++++++++++++--
  xen/include/public/platform.h            | 22 ++++++++++++
  xen/include/xen/symbols.h                |  4 +++
  xen/tools/symbols.c                      |  4 +++
  7 files changed, 97 insertions(+), 3 deletions(-)
xen/arch/x86/platform_hypercall.c +9 -0
index 7175a82..39376fe 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>
@@ -597,6 +598,14 @@ ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
      }
      break;
 
+    case XENPF_get_symbols:
+    {
+        ret = xensyms_read(&op->u.symdata);
+        if ( ret >= 0 && __copy_field_to_guest(u_xenpf_op, op, u.symdata) )
+            ret = -EFAULT;
+    }
+    break;
+
      default:
          ret = -ENOSYS;
          break;
xen/arch/x86/x86_64/platform_hypercall.c +1 -1
index aa2ad54..9ef705a 100644
@@ -35,7 +35,7 @@ CHECK_pf_pcpu_version;
  #undef xen_pf_pcpu_version
 
  #define xenpf_enter_acpi_sleep compat_pf_enter_acpi_sleep
-
+#define xenpf_symdata	compat_pf_symdata
  #define COMPAT
  #define _XEN_GUEST_HANDLE(t) XEN_GUEST_HANDLE(t)
  #define _XEN_GUEST_HANDLE_PARAM(t) XEN_GUEST_HANDLE_PARAM(t)
xen/common/symbols-dummy.c +1 -0
index 5090c3b..52a86c7 100644
@@ -12,6 +12,7 @@ const unsigned int symbols_offsets[1];
  const unsigned long symbols_addresses[1];
  #endif
  const unsigned int symbols_num_syms;
+const unsigned long symbols_names_bytes;
  const u8 symbols_names[1];
 
  const u8 symbols_token_table[1];
xen/common/symbols.c +56 -2
index 83b2b58..e74a585 100644
@@ -17,6 +17,8 @@
  #include <xen/lib.h>
  #include <xen/string.h>
  #include <xen/spinlock.h>
+#include <public/platform.h>
+#include <xen/guest_access.h>
 
  #ifdef SYMBOLS_ORIGIN
  extern const unsigned int symbols_offsets[1];
@@ -26,6 +28,7 @@ extern const unsigned long symbols_addresses[];
  #define symbols_address(n) symbols_addresses[n]
  #endif
  extern const unsigned int symbols_num_syms;
+extern const unsigned long symbols_names_bytes;
  extern const u8 symbols_names[];
 
  extern const u8 symbols_token_table[];
@@ -35,7 +38,8 @@ extern const unsigned int symbols_markers[];
 
  /* expand a compressed symbol data into the resulting uncompressed string,
     given the offset to where the symbol is in the compressed stream */
-static unsigned int symbols_expand_symbol(unsigned int off, char *result)
+static unsigned int symbols_expand_symbol(unsigned int off, char *result,
+                                          int maxlen)
  {
      int len, skipped_first = 0;
      const u8 *tptr, *data;
@@ -49,6 +53,9 @@ static unsigned int symbols_expand_symbol(unsigned int off, char *result)
       * the compressed stream */
      off += len + 1;
 
+    if (maxlen < len)
+        len = maxlen;
+
      /* for every byte on the compressed symbol data, copy the table
         entry for that byte */
      while(len) {
@@ -129,7 +136,7 @@ const char *symbols_lookup(unsigned long addr,
          --low;
 
          /* Grab name */
-    symbols_expand_symbol(get_symbol_offset(low), namebuf);
+    symbols_expand_symbol(get_symbol_offset(low), namebuf, sizeof(namebuf));
 
      /* Search for next non-aliased symbol */
      for (i = low + 1; i < symbols_num_syms; i++) {
@@ -174,3 +181,50 @@ void __print_symbol(const char *fmt, unsigned long address)
 
      spin_unlock_irqrestore(&lock, flags);
  }
+
+/*
+ * Get symbol type information. This is encoded as a single char at the
+ * beginning of the symbol name.
+ */
+static char symbols_get_symbol_type(unsigned int off)
+{
+    /*
+     * Get just the first code, look it up in the token table,
+     * and return the first char from this token.
+     */
+    return symbols_token_table[symbols_token_index[symbols_names[off + 1]]];
+}
+
+/*
+ * Symbols are most likely accessed sequentially so we remember position from
+ * previous read. This can help us avoid extra call to get_symbol_offset().
+ */
+static uint64_t next_symbol, next_offset;
+static DEFINE_SPINLOCK(symbols_mutex);
+
+int xensyms_read(struct xenpf_symdata *symdata)
+{
+    if ( symdata->xen_symnum > symbols_num_syms )
+        return -EINVAL;
+    else if ( symdata->xen_symnum == symbols_num_syms )
+        return 0;
+
+    spin_lock(&symbols_mutex);
+
+    if ( symdata->xen_symnum == 0 )
+        next_offset = next_symbol = 0;
+    else if ( next_symbol != symdata->xen_symnum )
+        /* Non-sequential access */
+        next_offset = get_symbol_offset(symdata->xen_symnum);
+
+    symdata->type = symbols_get_symbol_type(next_offset);
+    next_offset = symbols_expand_symbol(next_offset, symdata->name,
+        sizeof(symdata->name));
+    symdata->address = symbols_offsets[symdata->xen_symnum] + SYMBOLS_ORIGIN;
+
+    next_symbol = symdata->xen_symnum + 1;
+
+    spin_unlock(&symbols_mutex);
+
+    return strlen(symdata->name);
+}
xen/include/public/platform.h +22 -0
index 4341f54..870e14b 100644
@@ -527,6 +527,27 @@ struct xenpf_core_parking {
  typedef struct xenpf_core_parking xenpf_core_parking_t;
  DEFINE_XEN_GUEST_HANDLE(xenpf_core_parking_t);
 
+#define XENPF_get_symbols  61
+
+struct xenpf_symdata {
+    /* IN variables */
+    uint64_t xen_symnum;
+
+    /* OUT variables */
+    uint64_t address;
+    uint64_t type;
+    /*
+     * KSYM_NAME_LEN is 128 bytes. However, we cannot be larger than pad in
+     * xen_platform_op below (which is 128 bytes as well). Since the largest
+     * symbol is around 50 bytes it's probably more trouble than it's worth
+     * to try to deal with symbols that are close to 128 bytes in length.
+     */
+#define XEN_SYMS_MAX_LEN (128 - 3 * 8)
+    char name[XEN_SYMS_MAX_LEN];
+};
+typedef struct xenpf_symdata xenpf_symdata_t;
+DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t);
+
  /*
   * ` enum neg_errnoval
   * ` HYPERVISOR_platform_op(const struct xen_platform_op*);
@@ -553,6 +574,7 @@ struct xen_platform_op {
          struct xenpf_cpu_hotadd        cpu_add;
          struct xenpf_mem_hotadd        mem_add;
          struct xenpf_core_parking      core_parking;
+        struct xenpf_symdata           symdata;
          uint8_t                        pad[128];
      } u;
  };
xen/include/xen/symbols.h +4 -0
index 37cf6bf..c8df28f 100644
@@ -2,6 +2,8 @@
  #define _XEN_SYMBOLS_H
 
  #include <xen/types.h>
+#include <public/xen.h>
+#include <public/platform.h>
 
  #define KSYM_NAME_LEN 127
 
@@ -34,4 +36,6 @@ do {						\
  	__print_symbol(fmt, addr);		\
  } while(0)
 
+extern int xensyms_read(struct xenpf_symdata *symdata);
+
  #endif /*_XEN_SYMBOLS_H*/
xen/tools/symbols.c +4 -0
index f39c906..818204d 100644
@@ -272,6 +272,10 @@ static void write_src(void)
  	}
  	printf("\n");
 
+	output_label("symbols_names_bytes");
+	printf("\t.long\t%d\n", off);
+	printf("\n");
+
  	output_label("symbols_markers");
  	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
  		printf("\t.long\t%d\n", markers[i]);
Signature · 7 lines
1.8.1.4


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

Re: [PATCH v2 01/13] Export hypervisor symbols

Quoted 33 lines · Boris Ostrovsky
Quoted 32 lines · Konrad Rzeszutek Wilk

On Fri, Sep 20, 2013 at 05:42:00AM -0400, Boris Ostrovsky wrote:

I think the title needs a prefix of say: "xen/platform_op:" or such.

Quoted 1 line

Export Xen's symbols in format similar to Linux' /proc/kallsyms.

Which is?

I see in Linux:

000000000000bd58 D xen_cr3
...
000000000000e978 d soft_lockup_hrtimer_cnt
..
ffffffff8101cef0 T eager_fpu_init
ffffffff8101d010 t ptrace_triggered

I know that the first column is the EIP (thought some of them seem to be based on some offset). The last one is pretty obvious too.

But the middle one:

konrad@phenom:~$ cat /proc/kallsyms  | awk '{print $2}' | sort | uniq
b
B
d
D
r
R
t
T
V
W
Quoted 4 lines
Quoted 2 lines

Could you explain what those mean? And are they part of this? Or does Xen not carry those?

These are symbol types, described in 'man nm'. For Xen we should

<sigh> I was doing 'man kallsyms' which of course would not give me that.

Quoted 9 lines

only see 't' and 'T' which denote local or global text symbol.

So the format would be
<address> <type> <symbol name>

In any case, this is not a comlpetely correct commit message, it really describes to v1 implementation. With v2 Xen will return these three values in a structure, which has nothing to do with *format* of how these symbols are presented. I'll re-phrase it.

Thank you. Sorry for the noise - it didn't occur for me to check 'nm' :-(

Quoted 3 lines

(And there other remnants of v1 in this patch that I just noticed, such as changes to xen/tools/symbols.c which are no longer needed)

#5 lore raw

Re: [PATCH v2 01/13] Export hypervisor symbols

In reply to Boris Ostrovsky

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

Quoted 8 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/platform_hypercall.c +7 -0
+    case XENPF_get_symbols:
+    {
+        ret = xensyms_read(&op->u.symdata);
+        if ( ret >= 0 && __copy_field_to_guest(u_xenpf_op, op, u.symdata) )
+            ret = -EFAULT;
+    }
+    break;

This yields a positive return value if a symbol was found, 0 if none was found, and negative on error. Can we avoid this non-standard first aspect?

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

Quoted 7 lines
xen/arch/x86/x86_64/platform_hypercall.c +0 -1
@@ -35,7 +35,7 @@ CHECK_pf_pcpu_version;
 #undef xen_pf_pcpu_version
 
 #define xenpf_enter_acpi_sleep compat_pf_enter_acpi_sleep
-

Please retain this blank line.

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

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

This needs to be accompanied by an entry in xen/include/xlat.lst, and a respective CHECK_ invocation. I admit that the line immediately above is incomplete too, and hence served as a bad example (I'm preparing a fix as I write this).

xen/arch/x86/x86_64/platform_hypercall.c CHECK_pf_pcpu_version;

Quoted 7 lines
xen/arch/x86/x86_64/platform_hypercall.c +7 -0
+static uint64_t next_symbol, next_offset;
+static DEFINE_SPINLOCK(symbols_mutex);
+
+int xensyms_read(struct xenpf_symdata *symdata)
+{
+    if ( symdata->xen_symnum > symbols_num_syms )
+        return -EINVAL;

This should be a more specific error code (-ERANGE perhaps).

Quoted 1 line

+ else if ( symdata->xen_symnum == symbols_num_syms )

Pointless "else".

Quoted 10 lines
xen/arch/x86/x86_64/platform_hypercall.c +10 -0
+        return 0;
+
+    spin_lock(&symbols_mutex);
+
+    if ( symdata->xen_symnum == 0 )
+        next_offset = next_symbol = 0;
+    else if ( next_symbol != symdata->xen_symnum )
+        /* Non-sequential access */
+        next_offset = get_symbol_offset(symdata->xen_symnum);
+

xen/arch/x86/x86_64/platform_hypercall.c

Quoted 10 lines
xen/arch/x86/x86_64/platform_hypercall.c +10 -0
+    symdata->type = symbols_get_symbol_type(next_offset);
+    next_offset = symbols_expand_symbol(next_offset, symdata->name,
+        sizeof(symdata->name));
+    symdata->address = symbols_offsets[symdata->xen_symnum] + SYMBOLS_ORIGIN;
+
+    next_symbol = symdata->xen_symnum + 1;
+
+    spin_unlock(&symbols_mutex);
+
+    return strlen(symdata->name);

Altogether the changes you do appear to allow the nul terminator to be written outside of the passed in array. Hence (a) you need to avoid corrupting memory and (b) whether using strlen() here is appropriate depends on how you deal with (a).

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

Quoted 7 lines
xen/include/public/platform.h +1 -0
@@ -527,6 +527,27 @@ struct xenpf_core_parking {
 typedef struct xenpf_core_parking xenpf_core_parking_t;
 DEFINE_XEN_GUEST_HANDLE(xenpf_core_parking_t);
 
+#define XENPF_get_symbols  61

Now that you retrieve them one at a time, perhaps better XENPF_get_symbol?

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

Quoted 8 lines
xen/include/public/platform.h +8 -0
+
+struct xenpf_symdata {
+    /* IN variables */
+    uint64_t xen_symnum;
+
+    /* OUT variables */
+    uint64_t address;
+    uint64_t type;

"type" and "xen_symnum" could easily be less than 64 bits wide.

Also, what's the point of the "xen_" prefix in the latter?

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

Quoted 8 lines
xen/include/public/platform.h +8 -0
+    /*
+     * KSYM_NAME_LEN is 128 bytes. However, we cannot be larger than pad in
+     * xen_platform_op below (which is 128 bytes as well). Since the largest
+     * symbol is around 50 bytes it's probably more trouble than it's worth
+     * to try to deal with symbols that are close to 128 bytes in length.
+     */
+#define XEN_SYMS_MAX_LEN (128 - 3 * 8)
+    char name[XEN_SYMS_MAX_LEN];

No, this ought to be a handle pointing to a char array. Symbols alone might be only around 50 bytes, but the moment we get to the point of prefixing static symbols with their file names this will break.

Jan

#6 lore raw

Re: [PATCH v2 01/13] Export hypervisor symbols

Quoted 11 lines · Jan Beulich

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

Quoted 8 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/platform_hypercall.c +7 -0
+    case XENPF_get_symbols:
+    {
+        ret = xensyms_read(&op->u.symdata);
+        if ( ret >= 0 && __copy_field_to_guest(u_xenpf_op, op, u.symdata) )
+            ret = -EFAULT;
+    }
+    break;

This yields a positive return value if a symbol was found, 0 if none was found, and negative on error. Can we avoid this non-standard first aspect?

We need to know on the caller side when EOF is reached. There is no good error code that I can see that would be appropriate here. ERANGE or ENFILE are the closest I can imagine but EOF is not really an error so I am not sure this would be the right thing.

We could look at first byte of the string and see where it's a 0 but that's also somewhat non-standard. Encoding type or address as an invalid token also doesn't look nice.

Quoted 17 lines
Quoted 17 lines
xen/common/symbols.c +16 -0
+        return 0;
+
+    spin_lock(&symbols_mutex);
+
+    if ( symdata->xen_symnum == 0 )
+        next_offset = next_symbol = 0;
+    else if ( next_symbol != symdata->xen_symnum )
+        /* Non-sequential access */
+        next_offset = get_symbol_offset(symdata->xen_symnum);
+
+    symdata->type = symbols_get_symbol_type(next_offset);
+    next_offset = symbols_expand_symbol(next_offset, symdata->name,
+        sizeof(symdata->name));
+    symdata->address = symbols_offsets[symdata->xen_symnum] + SYMBOLS_ORIGIN;
+
+    next_symbol = symdata->xen_symnum + 1;
Quoted 8 lines

xen/common/symbols.c void __print_symbol(const char *fmt, unsigned long address) · line 229

Quoted 4 lines
xen/common/symbols.c +4 -0
+
+    spin_unlock(&symbols_mutex);
+
+    return strlen(symdata->name);

Altogether the changes you do appear to allow the nul terminator to be written outside of the passed in array. Hence (a) you need to avoid corrupting memory and (b) whether using strlen() here is appropriate depends on how you deal with (a).

Yes, I should have passed 'sizeof(symdata->name)-1' to symbols_expand_symbol() (I was thinking of strlen instead of sizeof)

Quoted 9 lines

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

Quoted 8 lines
xen/include/public/platform.h +8 -0
+
+struct xenpf_symdata {
+    /* IN variables */
+    uint64_t xen_symnum;
+
+    /* OUT variables */
+    uint64_t address;
+    uint64_t type;

"type" and "xen_symnum" could easily be less than 64 bits wide.

I am trying to avoid 32-bit compatibility issues here. You will see sometimes unnecessary uint64_t in other structures well for the same reason.

-boris

#7 lore raw

Re: [PATCH v2 01/13] Export hypervisor symbols

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

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

Quoted 8 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/platform_hypercall.c +7 -0
+    case XENPF_get_symbols:
+    {
+        ret = xensyms_read(&op->u.symdata);
+        if ( ret >= 0 && __copy_field_to_guest(u_xenpf_op, op, u.symdata) )
+            ret = -EFAULT;
+    }
+    break;

This yields a positive return value if a symbol was found, 0 if none was found, and negative on error. Can we avoid this non-standard first aspect?

We need to know on the caller side when EOF is reached. There is no good error code that I can see that would be appropriate here. ERANGE or ENFILE are the closest I can imagine but EOF is not really an error so I am not sure
this would be the right thing.

Quoted 4 lines

We could look at first byte of the string and see where it's a 0 but that's also
somewhat non-standard. Encoding type or address as an invalid token also doesn't look nice.

Why don't you simply have the hypercall increment the passed in symbol index. And if it didn't get incremented the called will know there was no data returned.

Quoted 10 lines
Quoted 9 lines

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

Quoted 8 lines
xen/include/public/platform.h +8 -0
+
+struct xenpf_symdata {
+    /* IN variables */
+    uint64_t xen_symnum;
+
+    /* OUT variables */
+    uint64_t address;
+    uint64_t type;

"type" and "xen_symnum" could easily be less than 64 bits wide.

Quoted 3 lines

I am trying to avoid 32-bit compatibility issues here. You will see sometimes
unnecessary uint64_t in other structures well for the same reason.

I'd encourage you to avoid that, and use padding fields instead.

Jan

#8 lore raw

PATCH v2 2/13 Set VCPU's is_running flag closer to when the VCPU is dispatched

In reply to Boris Ostrovsky

An interrupt handler happening during new VCPU scheduling may want to know who was on the (physical) processor at the point of the interrupt. Just looking at 'current' may not be accurate since there is a window of time when 'current' points to new VCPU and its is_running flag is set but the VCPU has not been dispatched yet. More importantly, on Intel processors, if the handler wants to examine certain state of an HVM VCPU (such as segment registers) the VMCS pointer is not set yet.

This patch will move setting the is_running flag to a later point.

---
 xen/arch/arm/domain.c             | 1 +
 xen/arch/x86/domain.c             | 1 +
 xen/arch/x86/hvm/svm/entry.S      | 2 ++
 xen/arch/x86/hvm/vmx/entry.S      | 1 +
 xen/arch/x86/x86_64/asm-offsets.c | 1 +
 xen/common/schedule.c             | 8 ++++++--
 6 files changed, 12 insertions(+), 2 deletions(-)
xen/arch/arm/domain.c +1 -0
index 373c7b3..94a6bd4 100644
@@ -229,6 +229,7 @@ static void schedule_tail(struct vcpu *prev)
     ctxt_switch_from(prev);
 
     ctxt_switch_to(current);
+    current->is_running = 1;
 
     local_irq_enable();
 
xen/arch/x86/domain.c +1 -0
index 874742c..e119d7b 100644
@@ -142,6 +142,7 @@ static void continue_nonidle_domain(struct vcpu *v)
 {
     check_wakeup_from_wait();
     mark_regs_dirty(guest_cpu_user_regs());
+    v->is_running = 1;
     reset_stack_and_jump(ret_from_intr);
 }
 
xen/arch/x86/hvm/svm/entry.S +2 -0
index 1969629..728e773 100644
@@ -74,6 +74,8 @@ UNLIKELY_END(svm_trace)
 
         mov  VCPU_svm_vmcb_pa(%rbx),%rax
 
+        movb $1,VCPU_is_running(%rbx)
+
         pop  %r15
         pop  %r14
         pop  %r13
xen/arch/x86/hvm/vmx/entry.S +1 -0
index 496a62c..9e33f45 100644
@@ -125,6 +125,7 @@ UNLIKELY_END(realmode)
         mov  $GUEST_RFLAGS,%eax
         VMWRITE(UREGS_eflags)
 
+        movb $1,VCPU_is_running(%rbx)
         cmpb $0,VCPU_vmx_launched(%rbx)
         pop  %r15
         pop  %r14
xen/arch/x86/x86_64/asm-offsets.c +1 -0
index b0098b3..9fa06c0 100644
@@ -86,6 +86,7 @@ void __dummy__(void)
     OFFSET(VCPU_kernel_sp, struct vcpu, arch.pv_vcpu.kernel_sp);
     OFFSET(VCPU_kernel_ss, struct vcpu, arch.pv_vcpu.kernel_ss);
     OFFSET(VCPU_guest_context_flags, struct vcpu, arch.vgc_flags);
+    OFFSET(VCPU_is_running, struct vcpu, is_running);
     OFFSET(VCPU_nmi_pending, struct vcpu, nmi_pending);
     OFFSET(VCPU_mce_pending, struct vcpu, mce_pending);
     OFFSET(VCPU_nmi_old_mask, struct vcpu, nmi_state.old_mask);
xen/common/schedule.c +6 -2
index a8398bd..32c26e8 100644
@@ -1219,8 +1219,12 @@ static void schedule(void)
      * switch, else lost_records resume will not work properly.
      */
 
-    ASSERT(!next->is_running);
-    next->is_running = 1;
+    if ( is_idle_vcpu(next) )
+    /* Non-idle cpus set is_running right before they start running. */
+    {
+        ASSERT(!next->is_running);
+        next->is_running = 1;
+    }
 
     pcpu_schedule_unlock_irq(cpu);
 
Signature · 1 line
1.8.1.4
#9 lore raw

Re: [PATCH v2 02/13] Set VCPU's is_running flag closer to when the VCPU is dispatched

Quoted 10 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

An interrupt handler happening during new VCPU scheduling may want to know who was on the (physical) processor at the point of the interrupt. Just looking at 'current' may not be accurate since there is a window of time when 'current' points to new VCPU and its is_running flag is set but the VCPU has not been dispatched yet. More importantly, on Intel processors, if the handler wants to examine certain state of an HVM VCPU (such as segment registers) the VMCS pointer is not set yet.

This patch will move setting the is_running flag to a later point.

As said on v1 already - I'm all but convinced that this is not going to break something in subtle ways. Without you assuring us that you carefully inspected _all_ uses of this flag, I don't think this can go in.

Jan

#10 lore raw

Re: [PATCH v2 02/13] Set VCPU's is_running flag closer to when the VCPU is dispatched

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

An interrupt handler happening during new VCPU scheduling may want to know who was on the (physical) processor at the point of the interrupt. Just looking at 'current' may not be accurate since there is a window of time when 'current' points to new VCPU and its is_running flag is set but the VCPU has not been dispatched yet. More importantly, on Intel processors, if the handler
wants to examine certain state of an HVM VCPU (such as segment registers) the VMCS pointer is not set yet.

Quoted 5 lines
Quoted 1 line

This patch will move setting the is_running flag to a later point.

As said on v1 already - I'm all but convinced that this is not going to break something in subtle ways. Without you assuring us that you carefully inspected _all_ uses of this flag, I don't think this can go in.

It's very definitely not safe. It would break vcpu_sleep_sync() for example -- which depends on ->is_running being set inside the scheduler lock. Otherwise, if vcpu_sleep_nosync() occurs immediately after dropping the lock in schedule() then vcpu_sleep_sync() can see !v->is_running even though v is in the process of being activated to run.

It's a subtle flag, messing with it is unlikely ever to be the right answer.

-- Keir

Quoted 7 lines

Jan

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

#11 lore raw

PATCH v2 3/13 x86/PMU: Stop AMD counters when called from vpmu_save_force()

In reply to Boris Ostrovsky

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

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

PATCH v2 4/13 x86/VPMU: Minor VPMU cleanup

In reply to Boris Ostrovsky

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

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

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

PATCH v2 5/13 intel/VPMU: Clean up Intel VPMU code

In reply to Boris Ostrovsky

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

Call core2_get_pmc_count() once, during initialization.

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

---
 xen/arch/x86/hvm/vmx/vmcs.c              |  59 +++++++
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 289 ++++++++++++++-----------------
 xen/include/asm-x86/hvm/vmx/vmcs.h       |   2 +
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  19 --
 4 files changed, 191 insertions(+), 178 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +59 -0
index de9f592..756bc13 100644
@@ -1136,6 +1136,36 @@ int vmx_add_guest_msr(u32 msr)
     return 0;
 }
 
+void vmx_rm_guest_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int i, idx, msr_count = curr->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.msr_area;
+
+    if ( msr_area == NULL )
+        return;
+    
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+    
+    for ( i = idx; i < msr_count - 1; i++ )
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);
+    }
+    msr_area[msr_count - 1].index = 0;
+
+    curr->arch.hvm_vmx.msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_STORE_COUNT, msr_count);
+    __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, msr_count);
+
+    return;
+}
+
 int vmx_add_host_load_msr(u32 msr)
 {
     struct vcpu *curr = current;
@@ -1166,6 +1196,35 @@ int vmx_add_host_load_msr(u32 msr)
     return 0;
 }
 
+void vmx_rm_host_load_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int i, idx,  msr_count = curr->arch.hvm_vmx.host_msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.host_msr_area;
+
+    if ( msr_area == NULL )
+        return;
+
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+    
+    for ( i = idx; i < msr_count - 1; i++ )
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);
+    }
+    msr_area[msr_count - 1].index = 0;    
+
+    curr->arch.hvm_vmx.host_msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_LOAD_COUNT, msr_count);
+
+    return;
+}
+
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector)
 {
     int index, offset, changed;
xen/arch/x86/hvm/vmx/vpmu_core2.c +130 -159

537 lines not on this page · raw message

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

Re: [PATCH v2 05/13] intel/VPMU: Clean up Intel VPMU code

Am Freitag 20 September 2013, 05:42:04 schrieb Boris Ostrovsky:

Quoted 23 lines

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

Call core2_get_pmc_count() once, during initialization.

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

---
 xen/arch/x86/hvm/vmx/vmcs.c              |  59 +++++++
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 289 ++++++++++++++-----------------
 xen/include/asm-x86/hvm/vmx/vmcs.h       |   2 +
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  19 --
 4 files changed, 191 insertions(+), 178 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
index de9f592..756bc13 100644
@@ -1136,6 +1136,36 @@ int vmx_add_guest_msr(u32 msr)
     return 0;
 }

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1147

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +9 -0
 
+void vmx_rm_guest_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int i, idx, msr_count = curr->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.msr_area;
+
+    if ( msr_area == NULL )
+        return;
+    

unneeded spaces

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1154

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmcs.c +7 -0
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+    

unneeded spaces

Quoted 27 lines
xen/arch/x86/hvm/vmx/vmcs.c +20 -0
+    for ( i = idx; i < msr_count - 1; i++ )
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);
+    }
+    msr_area[msr_count - 1].index = 0;
+
+    curr->arch.hvm_vmx.msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_STORE_COUNT, msr_count);
+    __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, msr_count);
+
+    return;
+}
+
 int vmx_add_host_load_msr(u32 msr)
 {
     struct vcpu *curr = current;
@@ -1166,6 +1196,35 @@ int vmx_add_host_load_msr(u32 msr)
     return 0;
 }
 
+void vmx_rm_host_load_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int i, idx,  msr_count = curr->arch.hvm_vmx.host_msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.host_msr_area;
+

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_host_load_msr(u32 msr) · line 1214

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +10 -0
+    if ( msr_area == NULL )
+        return;
+
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+    

unneeded spaces

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1160

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmcs.c +6 -0
+    for ( i = idx; i < msr_count - 1; i++ )
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);
+    }
+    msr_area[msr_count - 1].index = 0;    

trailing spaces

Quoted 551 lines
xen/arch/x86/hvm/vmx/vmcs.c +7 -0
+
+    curr->arch.hvm_vmx.host_msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_LOAD_COUNT, msr_count);
+
+    return;
+}
+
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector)
 {
     int index, offset, changed;
xen/arch/x86/hvm/vmx/vpmu_core2.c +123 -157
index 101888d..50f784f 100644
@@ -65,6 +65,26 @@
 #define PMU_FIXED_WIDTH_MASK     (((1 << PMU_FIXED_WIDTH_BITS) -1) << PMU_FIXED_WIDTH_SHIFT)
 
 /*
+ * MSR_CORE_PERF_FIXED_CTR_CTRL contains the configuration of all fixed
+ * counters. 4 bits for every counter.
+ */
+#define FIXED_CTR_CTRL_BITS 4
+#define FIXED_CTR_CTRL_MASK ((1 << FIXED_CTR_CTRL_BITS) - 1)
+
+#define VPMU_CORE2_MAX_FIXED_PMCS     4
+struct core2_vpmu_context {
+    u64 fixed_ctrl;
+    u64 ds_area;
+    u64 pebs_enable;
+    u64 global_ovf_status;
+    u64 fix_counters[VPMU_CORE2_MAX_FIXED_PMCS];
+    struct arch_msr_pair arch_msr_pair[1];
+};
+
+static int arch_pmc_cnt; /* Number of general-purpose performance counters */
+static int fixed_pmc_cnt; /* Number of fixed performance counters */
+
+/*
  * QUIRK to workaround an issue on various family 6 cpus.
  * The issue leads to endless PMC interrupt loops on the processor.
  * If the interrupt handler is running and a pmc reaches the value 0, this
@@ -84,11 +104,8 @@ static void check_pmc_quirk(void)
         is_pmc_quirk = 0;    
 }
 
-static int core2_get_pmc_count(void);
 static void handle_pmc_quirk(u64 msr_content)
 {
-    int num_gen_pmc = core2_get_pmc_count();
-    int num_fix_pmc  = 3;
     int i;
     u64 val;
 
@@ -96,7 +113,7 @@ static void handle_pmc_quirk(u64 msr_content)
         return;
 
     val = msr_content;
-    for ( i = 0; i < num_gen_pmc; i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
     {
         if ( val & 0x1 )
         {
@@ -108,7 +125,7 @@ static void handle_pmc_quirk(u64 msr_content)
         val >>= 1;
     }
     val = msr_content >> 32;
-    for ( i = 0; i < num_fix_pmc; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
     {
         if ( val & 0x1 )
         {
@@ -121,65 +138,32 @@ static void handle_pmc_quirk(u64 msr_content)
     }
 }
 
-static const u32 core2_fix_counters_msr[] = {
-    MSR_CORE_PERF_FIXED_CTR0,
-    MSR_CORE_PERF_FIXED_CTR1,
-    MSR_CORE_PERF_FIXED_CTR2
-};
-
 /*
- * MSR_CORE_PERF_FIXED_CTR_CTRL contains the configuration of all fixed
- * counters. 4 bits for every counter.
+ * Read the number of general counters via CPUID.EAX[0xa].EAX[8..15]
  */
-#define FIXED_CTR_CTRL_BITS 4
-#define FIXED_CTR_CTRL_MASK ((1 << FIXED_CTR_CTRL_BITS) - 1)
-
-/* The index into the core2_ctrls_msr[] of this MSR used in core2_vpmu_dump() */
-#define MSR_CORE_PERF_FIXED_CTR_CTRL_IDX 0
-
-/* Core 2 Non-architectual Performance Control MSRs. */
-static const u32 core2_ctrls_msr[] = {
-    MSR_CORE_PERF_FIXED_CTR_CTRL,
-    MSR_IA32_PEBS_ENABLE,
-    MSR_IA32_DS_AREA
-};
-
-struct pmumsr {
-    unsigned int num;
-    const u32 *msr;
-};
-
-static const struct pmumsr core2_fix_counters = {
-    VPMU_CORE2_NUM_FIXED,
-    core2_fix_counters_msr
-};
+static int core2_get_arch_pmc_count(void)
+{
+    u32 eax, ebx, ecx, edx;
 
-static const struct pmumsr core2_ctrls = {
-    VPMU_CORE2_NUM_CTRLS,
-    core2_ctrls_msr
-};
-static int arch_pmc_cnt;
+    cpuid(0xa, &eax, &ebx, &ecx, &edx);
+    return ( (eax & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT );
+}
 
 /*
- * Read the number of general counters via CPUID.EAX[0xa].EAX[8..15]
+ * Read the number of fixed counters via CPUID.EDX[0xa].EDX[0..4]
  */
-static int core2_get_pmc_count(void)
+static int core2_get_fixed_pmc_count(void)
 {
     u32 eax, ebx, ecx, edx;
 
-    if ( arch_pmc_cnt == 0 )
-    {
-        cpuid(0xa, &eax, &ebx, &ecx, &edx);
-        arch_pmc_cnt = (eax & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT;
-    }
-
-    return arch_pmc_cnt;
+    cpuid(0xa, &eax, &ebx, &ecx, &edx);
+    return ( (eax & PMU_FIXED_NR_MASK) >> PMU_FIXED_NR_SHIFT );
 }
 
 static u64 core2_calc_intial_glb_ctrl_msr(void)
 {
-    int arch_pmc_bits = (1 << core2_get_pmc_count()) - 1;
-    u64 fix_pmc_bits  = (1 << 3) - 1;
+    int arch_pmc_bits = (1 << arch_pmc_cnt) - 1;
+    u64 fix_pmc_bits  = (1 << fixed_pmc_cnt) - 1;
     return ((fix_pmc_bits << 32) | arch_pmc_bits);
 }
 
@@ -196,9 +180,9 @@ static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index)
 {
     int i;
 
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
     {
-        if ( core2_fix_counters.msr[i] == msr_index )
+        if ( msr_index == MSR_CORE_PERF_FIXED_CTR0 + i )
         {
             *type = MSR_TYPE_COUNTER;
             *index = i;
@@ -206,14 +190,12 @@ static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index)
         }
     }
 
-    for ( i = 0; i < core2_ctrls.num; i++ )
+    if ( (msr_index == MSR_CORE_PERF_FIXED_CTR_CTRL ) ||
+        (msr_index == MSR_IA32_DS_AREA) ||
+        (msr_index == MSR_IA32_PEBS_ENABLE) )
     {
-        if ( core2_ctrls.msr[i] == msr_index )
-        {
-            *type = MSR_TYPE_CTRL;
-            *index = i;
-            return 1;
-        }
+        *type = MSR_TYPE_CTRL;
+        return 1;
     }
 
     if ( (msr_index == MSR_CORE_PERF_GLOBAL_CTRL) ||
@@ -225,7 +207,7 @@ static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index)
     }
 
     if ( (msr_index >= MSR_IA32_PERFCTR0) &&
-         (msr_index < (MSR_IA32_PERFCTR0 + core2_get_pmc_count())) )
+         (msr_index < (MSR_IA32_PERFCTR0 + arch_pmc_cnt)) )
     {
         *type = MSR_TYPE_ARCH_COUNTER;
         *index = msr_index - MSR_IA32_PERFCTR0;
@@ -233,7 +215,7 @@ static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index)
     }
 
     if ( (msr_index >= MSR_P6_EVNTSEL0) &&
-         (msr_index < (MSR_P6_EVNTSEL0 + core2_get_pmc_count())) )
+         (msr_index < (MSR_P6_EVNTSEL0 + arch_pmc_cnt)) )
     {
         *type = MSR_TYPE_ARCH_CTRL;
         *index = msr_index - MSR_P6_EVNTSEL0;
@@ -248,13 +230,13 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
     int i;
 
     /* Allow Read/Write PMU Counters MSR Directly. */
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
     {
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]), msr_bitmap);
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]),
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i), msr_bitmap);
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i),
                   msr_bitmap + 0x800/BYTES_PER_LONG);
     }
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
     {
         clear_bit(msraddr_to_bitpos(MSR_IA32_PERFCTR0+i), msr_bitmap);
         clear_bit(msraddr_to_bitpos(MSR_IA32_PERFCTR0+i),
@@ -262,32 +244,37 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
     }
 
     /* Allow Read PMU Non-global Controls Directly. */
-    for ( i = 0; i < core2_ctrls.num; i++ )
-        clear_bit(msraddr_to_bitpos(core2_ctrls.msr[i]), msr_bitmap);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
         clear_bit(msraddr_to_bitpos(MSR_P6_EVNTSEL0+i), msr_bitmap);
+
+    clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR_CTRL), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_PEBS_ENABLE), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_DS_AREA), msr_bitmap);
 }
 
 static void core2_vpmu_unset_msr_bitmap(unsigned long *msr_bitmap)
 {
     int i;
 
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
     {
-        set_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]), msr_bitmap);
-        set_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]),
+        set_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i), msr_bitmap);
+        set_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i),
                 msr_bitmap + 0x800/BYTES_PER_LONG);
     }
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
     {
         set_bit(msraddr_to_bitpos(MSR_IA32_PERFCTR0+i), msr_bitmap);
         set_bit(msraddr_to_bitpos(MSR_IA32_PERFCTR0+i),
                 msr_bitmap + 0x800/BYTES_PER_LONG);
     }
-    for ( i = 0; i < core2_ctrls.num; i++ )
-        set_bit(msraddr_to_bitpos(core2_ctrls.msr[i]), msr_bitmap);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+
+    for ( i = 0; i < arch_pmc_cnt; i++ )
         set_bit(msraddr_to_bitpos(MSR_P6_EVNTSEL0+i), msr_bitmap);
+
+    set_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR_CTRL), msr_bitmap);
+    set_bit(msraddr_to_bitpos(MSR_IA32_PEBS_ENABLE), msr_bitmap);
+    set_bit(msraddr_to_bitpos(MSR_IA32_DS_AREA), msr_bitmap);
 }
 
 static inline void __core2_vpmu_save(struct vcpu *v)
@@ -295,10 +282,10 @@ static inline void __core2_vpmu_save(struct vcpu *v)
     int i;
     struct core2_vpmu_context *core2_vpmu_cxt = vcpu_vpmu(v)->context;
 
-    for ( i = 0; i < core2_fix_counters.num; i++ )
-        rdmsrl(core2_fix_counters.msr[i], core2_vpmu_cxt->fix_counters[i]);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
-        rdmsrl(MSR_IA32_PERFCTR0+i, core2_vpmu_cxt->arch_msr_pair[i].counter);
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
+        rdmsrl(MSR_CORE_PERF_FIXED_CTR0 + i, core2_vpmu_cxt->fix_counters[i]);
+    for ( i = 0; i < arch_pmc_cnt; i++ )
+        rdmsrl(MSR_IA32_PERFCTR0 + i, core2_vpmu_cxt->arch_msr_pair[i].counter);
 }
 
 static int core2_vpmu_save(struct vcpu *v)
@@ -322,14 +309,16 @@ static inline void __core2_vpmu_load(struct vcpu *v)
     int i;
     struct core2_vpmu_context *core2_vpmu_cxt = vcpu_vpmu(v)->context;
 
-    for ( i = 0; i < core2_fix_counters.num; i++ )
-        wrmsrl(core2_fix_counters.msr[i], core2_vpmu_cxt->fix_counters[i]);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
-        wrmsrl(MSR_IA32_PERFCTR0+i, core2_vpmu_cxt->arch_msr_pair[i].counter);
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
+        wrmsrl(MSR_CORE_PERF_FIXED_CTR0 + i, core2_vpmu_cxt->fix_counters[i]);
+    for ( i = 0; i < arch_pmc_cnt; i++ )
+        wrmsrl(MSR_IA32_PERFCTR0 + i, core2_vpmu_cxt->arch_msr_pair[i].counter);
 
-    for ( i = 0; i < core2_ctrls.num; i++ )
-        wrmsrl(core2_ctrls.msr[i], core2_vpmu_cxt->ctrls[i]);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+    wrmsrl(MSR_CORE_PERF_FIXED_CTR_CTRL, core2_vpmu_cxt->fixed_ctrl);
+    wrmsrl(MSR_IA32_DS_AREA, core2_vpmu_cxt->ds_area);
+    wrmsrl(MSR_IA32_PEBS_ENABLE, core2_vpmu_cxt->pebs_enable);
+
+    for ( i = 0; i < arch_pmc_cnt; i++ )
         wrmsrl(MSR_P6_EVNTSEL0+i, core2_vpmu_cxt->arch_msr_pair[i].control);
 }
 
@@ -347,56 +336,39 @@ static int core2_vpmu_alloc_resource(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     struct core2_vpmu_context *core2_vpmu_cxt;
-    struct core2_pmu_enable *pmu_enable;
 
     if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
         return 0;
 
     wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
     if ( vmx_add_host_load_msr(MSR_CORE_PERF_GLOBAL_CTRL) )
-        return 0;
+        goto out_err;
 
     if ( vmx_add_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL) )
-        return 0;
+        goto out_err;
     vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL,
                  core2_calc_intial_glb_ctrl_msr());
 
-    pmu_enable = xzalloc_bytes(sizeof(struct core2_pmu_enable) +
-                               core2_get_pmc_count() - 1);
-    if ( !pmu_enable )
-        goto out1;
-
     core2_vpmu_cxt = xzalloc_bytes(sizeof(struct core2_vpmu_context) +
-                    (core2_get_pmc_count()-1)*sizeof(struct arch_msr_pair));
+                    (arch_pmc_cnt-1)*sizeof(struct arch_msr_pair));
     if ( !core2_vpmu_cxt )
-        goto out2;
-    core2_vpmu_cxt->pmu_enable = pmu_enable;
+        goto out_err;
+
     vpmu->context = (void *)core2_vpmu_cxt;
 
+    vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
+
     return 1;
- out2:
-    xfree(pmu_enable);
- out1:
-    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 0;
-}
 
-static void core2_vpmu_save_msr_context(struct vcpu *v, int type,
-                                       int index, u64 msr_data)
-{
-    struct core2_vpmu_context *core2_vpmu_cxt = vcpu_vpmu(v)->context;
+out_err:
+    vmx_rm_host_load_msr(MSR_CORE_PERF_GLOBAL_CTRL);
+    vmx_rm_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL);
+    release_pmu_ownship(PMU_OWNER_HVM);
 
-    switch ( type )
-    {
-    case MSR_TYPE_CTRL:
-        core2_vpmu_cxt->ctrls[index] = msr_data;
-        break;
-    case MSR_TYPE_ARCH_CTRL:
-        core2_vpmu_cxt->arch_msr_pair[index].control = msr_data;
-        break;
-    }
+    printk("Failed to allocate VPMU resources for domain %u vcpu %u\n",
+           v->vcpu_id, v->domain->domain_id);
+
+    return 0;
 }
 
 static int core2_vpmu_msr_common_check(u32 msr_index, int *type, int *index)
@@ -407,10 +379,8 @@ static int core2_vpmu_msr_common_check(u32 msr_index, int *type, int *index)
         return 0;
 
     if ( unlikely(!vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED)) &&
-	 (vpmu->context != NULL ||
-	  !core2_vpmu_alloc_resource(current)) )
+         !core2_vpmu_alloc_resource(current) )
         return 0;
-    vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
 
     /* Do the lazy load staff. */
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
@@ -426,7 +396,7 @@ static int core2_vpmu_msr_common_check(u32 msr_index, int *type, int *index)
 static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     u64 global_ctrl, non_global_ctrl;
-    char pmu_enable = 0;
+    unsigned pmu_enable = 0;
     int i, tmp;
     int type = -1, index = -1;
     struct vcpu *v = current;
@@ -471,6 +441,7 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         if ( msr_content & 1 )
             gdprintk(XENLOG_WARNING, "Guest is trying to enable PEBS, "
                      "which is not supported.\n");
+        core2_vpmu_cxt->pebs_enable = msr_content;
         return 1;
     case MSR_IA32_DS_AREA:
         if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) )
@@ -483,27 +454,25 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
                 hvm_inject_hw_exception(TRAP_gp_fault, 0);
                 return 1;
             }
-            core2_vpmu_cxt->pmu_enable->ds_area_enable = msr_content ? 1 : 0;
+            core2_vpmu_cxt->ds_area = msr_content;
             break;
         }
         gdprintk(XENLOG_WARNING, "Guest setting of DTS is ignored.\n");
         return 1;
     case MSR_CORE_PERF_GLOBAL_CTRL:
         global_ctrl = msr_content;
-        for ( i = 0; i < core2_get_pmc_count(); i++ )
+        for ( i = 0; i < arch_pmc_cnt; i++ )
         {
             rdmsrl(MSR_P6_EVNTSEL0+i, non_global_ctrl);
-            core2_vpmu_cxt->pmu_enable->arch_pmc_enable[i] =
-                    global_ctrl & (non_global_ctrl >> 22) & 1;
+            pmu_enable += global_ctrl & (non_global_ctrl >> 22) & 1;
             global_ctrl >>= 1;
         }
 
         rdmsrl(MSR_CORE_PERF_FIXED_CTR_CTRL, non_global_ctrl);
         global_ctrl = msr_content >> 32;
-        for ( i = 0; i < core2_fix_counters.num; i++ )
+        for ( i = 0; i < fixed_pmc_cnt; i++ )
         {
-            core2_vpmu_cxt->pmu_enable->fixed_ctr_enable[i] =
-                (global_ctrl & 1) & ((non_global_ctrl & 0x3)? 1: 0);
+            pmu_enable += (global_ctrl & 1) & ((non_global_ctrl & 0x3)? 1: 0);
             non_global_ctrl >>= FIXED_CTR_CTRL_BITS;
             global_ctrl >>= 1;
         }
@@ -512,27 +481,27 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         non_global_ctrl = msr_content;
         vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
         global_ctrl >>= 32;
-        for ( i = 0; i < core2_fix_counters.num; i++ )
+        for ( i = 0; i < fixed_pmc_cnt; i++ )
         {
-            core2_vpmu_cxt->pmu_enable->fixed_ctr_enable[i] =
-                (global_ctrl & 1) & ((non_global_ctrl & 0x3)? 1: 0);
+            pmu_enable += (global_ctrl & 1) & ((non_global_ctrl & 0x3)? 1: 0);
             non_global_ctrl >>= 4;
             global_ctrl >>= 1;
         }
+        core2_vpmu_cxt->fixed_ctrl = msr_content;
         break;
     default:
         tmp = msr - MSR_P6_EVNTSEL0;
-        vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
-        if ( tmp >= 0 && tmp < core2_get_pmc_count() )
-            core2_vpmu_cxt->pmu_enable->arch_pmc_enable[tmp] =
-                (global_ctrl >> tmp) & (msr_content >> 22) & 1;
+        if ( tmp >= 0 && tmp < arch_pmc_cnt )
+        {
+            vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
+            core2_vpmu_cxt->arch_msr_pair[tmp].control = msr_content;
+            for ( i = 0; i < arch_pmc_cnt && !pmu_enable; i++ )
+                pmu_enable += (global_ctrl >> i) &
+                    (core2_vpmu_cxt->arch_msr_pair[i].control >> 22) & 1;
+        }
     }
 
-    for ( i = 0; i < core2_fix_counters.num; i++ )
-        pmu_enable |= core2_vpmu_cxt->pmu_enable->fixed_ctr_enable[i];
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
-        pmu_enable |= core2_vpmu_cxt->pmu_enable->arch_pmc_enable[i];
-    pmu_enable |= core2_vpmu_cxt->pmu_enable->ds_area_enable;
+    pmu_enable += (core2_vpmu_cxt->ds_area != 0);
     if ( pmu_enable )
         vpmu_set(vpmu, VPMU_RUNNING);
     else
@@ -551,7 +520,6 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
     }
 
-    core2_vpmu_save_msr_context(v, type, index, msr_content);
     if ( type != MSR_TYPE_GLOBAL )
     {
         u64 mask;
@@ -567,7 +535,7 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
             if  ( msr == MSR_IA32_DS_AREA )
                 break;
             /* 4 bits per counter, currently 3 fixed counters implemented. */
-            mask = ~((1ull << (VPMU_CORE2_NUM_FIXED * FIXED_CTR_CTRL_BITS)) - 1);
+            mask = ~((1ull << (fixed_pmc_cnt * FIXED_CTR_CTRL_BITS)) - 1);
             if (msr_content & mask)
                 inject_gp = 1;
             break;
@@ -652,7 +620,7 @@ static void core2_vpmu_do_cpuid(unsigned int input,
 static void core2_vpmu_dump(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    int i, num;
+    int i;
     struct core2_vpmu_context *core2_vpmu_cxt = NULL;
     u64 val;
 
@@ -670,26 +638,24 @@ static void core2_vpmu_dump(struct vcpu *v)
 
     printk("    vPMU running\n");
     core2_vpmu_cxt = vpmu->context;
-    num = core2_get_pmc_count();
+
     /* Print the contents of the counter and its configuration msr. */
-    for ( i = 0; i < num; i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
     {
         struct arch_msr_pair* msr_pair = core2_vpmu_cxt->arch_msr_pair;
-        if ( core2_vpmu_cxt->pmu_enable->arch_pmc_enable[i] )
-            printk("      general_%d: 0x%016lx ctrl: 0x%016lx\n",
-                             i, msr_pair[i].counter, msr_pair[i].control);
+        printk("      general_%d: 0x%016lx ctrl: 0x%016lx\n",
+               i, msr_pair[i].counter, msr_pair[i].control);
     }
     /*
      * The configuration of the fixed counter is 4 bits each in the
      * MSR_CORE_PERF_FIXED_CTR_CTRL.
      */
-    val = core2_vpmu_cxt->ctrls[MSR_CORE_PERF_FIXED_CTR_CTRL_IDX];
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    val = core2_vpmu_cxt->fixed_ctrl;
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
     {
-        if ( core2_vpmu_cxt->pmu_enable->fixed_ctr_enable[i] )
-            printk("      fixed_%d:   0x%016lx ctrl: 0x%lx\n",
-                             i, core2_vpmu_cxt->fix_counters[i],
-                             val & FIXED_CTR_CTRL_MASK);
+        printk("      fixed_%d:   0x%016lx ctrl: 0x%lx\n",
+               i, core2_vpmu_cxt->fix_counters[i],
+               val & FIXED_CTR_CTRL_MASK);
         val >>= FIXED_CTR_CTRL_BITS;
     }
 }
@@ -707,7 +673,7 @@ static int core2_vpmu_do_interrupt(struct cpu_user_regs *regs)
         if ( is_pmc_quirk )
             handle_pmc_quirk(msr_content);
         core2_vpmu_cxt->global_ovf_status |= msr_content;
-        msr_content = 0xC000000700000000 | ((1 << core2_get_pmc_count()) - 1);
+        msr_content = 0xC000000700000000 | ((1 << arch_pmc_cnt) - 1);
         wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, msr_content);
     }
     else

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

Quoted 8 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -0
@@ -770,18 +736,23 @@ static int core2_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
         }
     }
 func_out:
+
+    arch_pmc_cnt = core2_get_arch_pmc_count();
+    fixed_pmc_cnt = core2_get_fixed_pmc_count();
+    if ( fixed_pmc_cnt > VPMU_CORE2_MAX_FIXED_PMCS )
+        fixed_pmc_cnt = VPMU_CORE2_MAX_FIXED_PMCS;

Maybe printing a message when limiting the number of counters?

Dietmar.

Quoted 66 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -2
     check_pmc_quirk();
+
     return 0;
 }
 
 static void core2_vpmu_destroy(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    struct core2_vpmu_context *core2_vpmu_cxt = vpmu->context;
 
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
-    xfree(core2_vpmu_cxt->pmu_enable);
+
     xfree(vpmu->context);
     if ( cpu_has_vmx_msr_bitmap && is_hvm_domain(v->domain) )
         core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
xen/include/asm-x86/hvm/vmx/vmcs.h +2 -0
index f30e5ac..5971613 100644
@@ -470,7 +470,9 @@ void vmx_enable_intercept_for_msr(struct vcpu *v, u32 msr, int type);
 int vmx_read_guest_msr(u32 msr, u64 *val);
 int vmx_write_guest_msr(u32 msr, u64 val);
 int vmx_add_guest_msr(u32 msr);
+void vmx_rm_guest_msr(u32 msr);
 int vmx_add_host_load_msr(u32 msr);
+void vmx_rm_host_load_msr(u32 msr);
 void vmx_vmcs_switch(struct vmcs_struct *from, struct vmcs_struct *to);
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector);
 void vmx_clear_eoi_exit_bitmap(struct vcpu *v, u8 vector);
xen/include/asm-x86/hvm/vmx/vpmu_core2.h +0 -19
index 60b05fd..410372d 100644
@@ -23,29 +23,10 @@
 #ifndef __ASM_X86_HVM_VPMU_CORE_H_
 #define __ASM_X86_HVM_VPMU_CORE_H_
 
-/* Currently only 3 fixed counters are supported. */
-#define VPMU_CORE2_NUM_FIXED 3
-/* Currently only 3 Non-architectual Performance Control MSRs */
-#define VPMU_CORE2_NUM_CTRLS 3
-
 struct arch_msr_pair {
     u64 counter;
     u64 control;
 };
 
-struct core2_pmu_enable {
-    char ds_area_enable;
-    char fixed_ctr_enable[VPMU_CORE2_NUM_FIXED];
-    char arch_pmc_enable[1];
-};
-
-struct core2_vpmu_context {
-    struct core2_pmu_enable *pmu_enable;
-    u64 fix_counters[VPMU_CORE2_NUM_FIXED];
-    u64 ctrls[VPMU_CORE2_NUM_CTRLS];
-    u64 global_ovf_status;
-    struct arch_msr_pair arch_msr_pair[1];
-};
-
 #endif /* __ASM_X86_HVM_VPMU_CORE_H_ */
 
Signature · 1 line
Company details: http://ts.fujitsu.com/imprint.html
#15 lore raw

Re: [PATCH v2 05/13] intel/VPMU: Clean up Intel VPMU code

In reply to Boris Ostrovsky
Quoted 191 lines · Boris Ostrovsky

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

Call core2_get_pmc_count() once, during initialization.

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

---
 xen/arch/x86/hvm/vmx/vmcs.c              |  59 +++++++
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 289 ++++++++++++++-----------------
 xen/include/asm-x86/hvm/vmx/vmcs.h       |   2 +
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  19 --
 4 files changed, 191 insertions(+), 178 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +59 -0
index de9f592..756bc13 100644
@@ -1136,6 +1136,36 @@ int vmx_add_guest_msr(u32 msr)
     return 0;
 }
 
+void vmx_rm_guest_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int i, idx, msr_count = curr->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.msr_area;
+
+    if ( msr_area == NULL )
+        return;
+    
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+    
+    for ( i = idx; i < msr_count - 1; i++ )
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);
+    }
+    msr_area[msr_count - 1].index = 0;
+
+    curr->arch.hvm_vmx.msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_STORE_COUNT, msr_count);
+    __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, msr_count);
+
+    return;
+}
+
 int vmx_add_host_load_msr(u32 msr)
 {
     struct vcpu *curr = current;
@@ -1166,6 +1196,35 @@ int vmx_add_host_load_msr(u32 msr)
     return 0;
 }
 
+void vmx_rm_host_load_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int i, idx,  msr_count = curr->arch.hvm_vmx.host_msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.host_msr_area;
+
+    if ( msr_area == NULL )
+        return;
+
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+    
+    for ( i = idx; i < msr_count - 1; i++ )
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);
+    }
+    msr_area[msr_count - 1].index = 0;    
+
+    curr->arch.hvm_vmx.host_msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_LOAD_COUNT, msr_count);
+
+    return;
+}
+
 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector)
 {
     int index, offset, changed;
xen/arch/x86/hvm/vmx/vpmu_core2.c +23 -35
index 101888d..50f784f 100644
@@ -65,6 +65,26 @@
 #define PMU_FIXED_WIDTH_MASK     (((1 << PMU_FIXED_WIDTH_BITS) -1) << PMU_FIXED_WIDTH_SHIFT)
 
 /*
+ * MSR_CORE_PERF_FIXED_CTR_CTRL contains the configuration of all fixed
+ * counters. 4 bits for every counter.
+ */
+#define FIXED_CTR_CTRL_BITS 4
+#define FIXED_CTR_CTRL_MASK ((1 << FIXED_CTR_CTRL_BITS) - 1)
+
+#define VPMU_CORE2_MAX_FIXED_PMCS     4
+struct core2_vpmu_context {
+    u64 fixed_ctrl;
+    u64 ds_area;
+    u64 pebs_enable;
+    u64 global_ovf_status;
+    u64 fix_counters[VPMU_CORE2_MAX_FIXED_PMCS];
+    struct arch_msr_pair arch_msr_pair[1];
+};
+
+static int arch_pmc_cnt; /* Number of general-purpose performance counters */
+static int fixed_pmc_cnt; /* Number of fixed performance counters */
+
+/*
  * QUIRK to workaround an issue on various family 6 cpus.
  * The issue leads to endless PMC interrupt loops on the processor.
  * If the interrupt handler is running and a pmc reaches the value 0, this
@@ -84,11 +104,8 @@ static void check_pmc_quirk(void)
         is_pmc_quirk = 0;    
 }
 
-static int core2_get_pmc_count(void);
 static void handle_pmc_quirk(u64 msr_content)
 {
-    int num_gen_pmc = core2_get_pmc_count();
-    int num_fix_pmc  = 3;
     int i;
     u64 val;
 
@@ -96,7 +113,7 @@ static void handle_pmc_quirk(u64 msr_content)
         return;
 
     val = msr_content;
-    for ( i = 0; i < num_gen_pmc; i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
     {
         if ( val & 0x1 )
         {
@@ -108,7 +125,7 @@ static void handle_pmc_quirk(u64 msr_content)
         val >>= 1;
     }
     val = msr_content >> 32;
-    for ( i = 0; i < num_fix_pmc; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
     {
         if ( val & 0x1 )
         {
@@ -121,65 +138,32 @@ static void handle_pmc_quirk(u64 msr_content)
     }
 }
 
-static const u32 core2_fix_counters_msr[] = {
-    MSR_CORE_PERF_FIXED_CTR0,
-    MSR_CORE_PERF_FIXED_CTR1,
-    MSR_CORE_PERF_FIXED_CTR2
-};
-
 /*
- * MSR_CORE_PERF_FIXED_CTR_CTRL contains the configuration of all fixed
- * counters. 4 bits for every counter.
+ * Read the number of general counters via CPUID.EAX[0xa].EAX[8..15]
  */
-#define FIXED_CTR_CTRL_BITS 4
-#define FIXED_CTR_CTRL_MASK ((1 << FIXED_CTR_CTRL_BITS) - 1)
-
-/* The index into the core2_ctrls_msr[] of this MSR used in core2_vpmu_dump() */
-#define MSR_CORE_PERF_FIXED_CTR_CTRL_IDX 0
-
-/* Core 2 Non-architectual Performance Control MSRs. */
-static const u32 core2_ctrls_msr[] = {
-    MSR_CORE_PERF_FIXED_CTR_CTRL,
-    MSR_IA32_PEBS_ENABLE,
-    MSR_IA32_DS_AREA
-};
-
-struct pmumsr {
-    unsigned int num;
-    const u32 *msr;
-};
-
-static const struct pmumsr core2_fix_counters = {
-    VPMU_CORE2_NUM_FIXED,
-    core2_fix_counters_msr
-};

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

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +4 -5
+static int core2_get_arch_pmc_count(void)
+{
+    u32 eax, ebx, ecx, edx;
 
-static const struct pmumsr core2_ctrls = {
-    VPMU_CORE2_NUM_CTRLS,
-    core2_ctrls_msr
-};
-static int arch_pmc_cnt;
+    cpuid(0xa, &eax, &ebx, &ecx, &edx);

Could you use cpuid_eax ?

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

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

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -7
     u32 eax, ebx, ecx, edx;
 
-    if ( arch_pmc_cnt == 0 )
-    {
-        cpuid(0xa, &eax, &ebx, &ecx, &edx);
-        arch_pmc_cnt = (eax & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT;
-    }
-
-    return arch_pmc_cnt;
+    cpuid(0xa, &eax, &ebx, &ecx, &edx);

Ditto here.

#16 lore raw

Re: [PATCH v2 05/13] intel/VPMU: Clean up Intel VPMU code

In reply to Boris Ostrovsky
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vmx/vmcs.c +7 -0
+void vmx_rm_guest_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int i, idx, msr_count = curr->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.msr_area;
+
+    if ( msr_area == NULL )

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1155

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +10 -0
+        return;
+    
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+    
+    for ( i = idx; i < msr_count - 1; i++ )

"idx" not being used further down anymore, why do you need another loop variable here?

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1158

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmcs.c +3 -0
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);

This is clearly a side effect of the function call no-one would expect. Why do you do this?

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1166

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmcs.c +8 -0
+    }
+    msr_area[msr_count - 1].index = 0;
+
+    curr->arch.hvm_vmx.msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_STORE_COUNT, msr_count);
+    __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, msr_count);
+
+    return;

Pointless "return".

All the same comments apply to vmx_rm_host_load_msr().

xen/arch/x86/hvm/vmx/vpmu_core2.c line 85

Quoted 2 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -0
+static int arch_pmc_cnt; /* Number of general-purpose performance counters */
+static int fixed_pmc_cnt; /* Number of fixed performance counters */

unsigned? __read_mostly?

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

Quoted 11 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +3 -3
@@ -248,13 +230,13 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
     int i;
 
     /* Allow Read/Write PMU Counters MSR Directly. */
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
     {
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]), msr_bitmap);
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]),
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i), msr_bitmap);
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i),

Dropping the static array will make the handling here quite a bit more complicated should there ever appear a second dis-contiguous MSR range.

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

Quoted 13 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -3
@@ -262,32 +244,37 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
     }
 
     /* Allow Read PMU Non-global Controls Directly. */
-    for ( i = 0; i < core2_ctrls.num; i++ )
-        clear_bit(msraddr_to_bitpos(core2_ctrls.msr[i]), msr_bitmap);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
         clear_bit(msraddr_to_bitpos(MSR_P6_EVNTSEL0+i), msr_bitmap);
+
+    clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR_CTRL), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_PEBS_ENABLE), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_DS_AREA), msr_bitmap);

As you can see, this is already the case here.

The patch description doesn't really say _why_ you do this. Jan

#17 lore raw

Re: [PATCH v2 05/13] intel/VPMU: Clean up Intel VPMU code

Quoted 21 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vmx/vmcs.c +7 -0
+void vmx_rm_guest_msr(u32 msr)
+{
+    struct vcpu *curr = current;
+    unsigned int i, idx, msr_count = curr->arch.hvm_vmx.msr_count;
+    struct vmx_msr_entry *msr_area = curr->arch.hvm_vmx.msr_area;
+
+    if ( msr_area == NULL )

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1155

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +10 -0
+        return;
+
+    for ( idx = 0; idx < msr_count; idx++ )
+        if ( msr_area[idx].index == msr )
+            break;
+
+    if ( idx == msr_count )
+        return;
+
+    for ( i = idx; i < msr_count - 1; i++ )

"idx" not being used further down anymore, why do you need another loop variable here?

Quoted 5 lines

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1158

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmcs.c +3 -0
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);

This is clearly a side effect of the function call no-one would expect. Why do you do this?

I don't understand what you are trying to say here.

(And this is wrong, instead of rdmsr it should be

        msr_area[i].data = msr_area[i + 1].data;

)

Quoted 20 lines

xen/arch/x86/hvm/vmx/vmcs.c int vmx_add_guest_msr(u32 msr) · line 1166

Quoted 9 lines
xen/arch/x86/hvm/vmx/vmcs.c +8 -0
+    }
+    msr_area[msr_count - 1].index = 0;
+
+    curr->arch.hvm_vmx.msr_count = --msr_count;
+    __vmwrite(VM_EXIT_MSR_STORE_COUNT, msr_count);
+    __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, msr_count);
+
+    return;

Pointless "return".

All the same comments apply to vmx_rm_host_load_msr().

xen/arch/x86/hvm/vmx/vpmu_core2.c line 85

Quoted 2 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -0
+static int arch_pmc_cnt; /* Number of general-purpose performance counters */
+static int fixed_pmc_cnt; /* Number of fixed performance counters */

unsigned? __read_mostly?

Quoted 3 lines
(quoted diff) +0 -0
@@ -248,13 +230,13 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
      int i;
 
Quoted 11 lines

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

Quoted 8 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +3 -3
      /* Allow Read/Write PMU Counters MSR Directly. */
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
      {
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]), msr_bitmap);
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]),
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i), msr_bitmap);
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i),

Dropping the static array will make the handling here quite a bit more complicated should there ever appear a second dis-contiguous MSR range.

Fixed counters range should always be contiguous per Intel SDM.

Quoted 14 lines

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

Quoted 13 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -3
@@ -262,32 +244,37 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
      }
 
      /* Allow Read PMU Non-global Controls Directly. */
-    for ( i = 0; i < core2_ctrls.num; i++ )
-        clear_bit(msraddr_to_bitpos(core2_ctrls.msr[i]), msr_bitmap);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
          clear_bit(msraddr_to_bitpos(MSR_P6_EVNTSEL0+i), msr_bitmap);
+
+    clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR_CTRL), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_PEBS_ENABLE), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_DS_AREA), msr_bitmap);

As you can see, this is already the case here.

This is a different set of MSRs from from what you've commented on above.

-boris

Quoted 4 lines

The patch description doesn't really say _why_ you do this. Jan

#18 lore raw

Re: [PATCH v2 05/13] intel/VPMU: Clean up Intel VPMU code

Quoted 6 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/hvm/vmx/vmcs.c +3 -0
+    {
+        msr_area[i].index = msr_area[i + 1].index;
+        rdmsrl(msr_area[i].index, msr_area[i].data);
Quoted 8 lines
Quoted 2 lines

This is clearly a side effect of the function call no-one would expect. Why do you do this?

I don't understand what you are trying to say here.

(And this is wrong, instead of rdmsr it should be

         msr_area[i].data = msr_area[i + 1].data;

)

That was the very point - doing an MSR read here is clearly an unexpected side effect.

Quoted 11 lines
Quoted 10 lines

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

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -3
@@ -248,13 +230,13 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
      int i;
 
      /* Allow Read/Write PMU Counters MSR Directly. */
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
      {
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]), msr_bitmap);
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]),
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i), 

msr_bitmap);

Quoted 6 lines
Quoted 4 lines

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

Quoted 1 line
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -0
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i),

Dropping the static array will make the handling here quite a bit more complicated should there ever appear a second dis-contiguous MSR range.

Fixed counters range should always be contiguous per Intel SDM.

Until the current range runs out...

Quoted 15 lines
Quoted 14 lines

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

Quoted 13 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -3
@@ -262,32 +244,37 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
      }
 
      /* Allow Read PMU Non-global Controls Directly. */
-    for ( i = 0; i < core2_ctrls.num; i++ )
-        clear_bit(msraddr_to_bitpos(core2_ctrls.msr[i]), msr_bitmap);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
          clear_bit(msraddr_to_bitpos(MSR_P6_EVNTSEL0+i), msr_bitmap);
+
+    clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR_CTRL), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_PEBS_ENABLE), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_DS_AREA), msr_bitmap);

As you can see, this is already the case here.

Quoted 1 line

This is a different set of MSRs from from what you've commented on above.

Sure, but the effect of breaking up a loop into individual operations is seen quite nicely here.

Jan

#19 lore raw

Re: [PATCH v2 05/13] intel/VPMU: Clean up Intel VPMU code

Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines · Boris Ostrovsky
(quoted diff) +0 -0
@@ -248,13 +230,13 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
       int i;
 
Quoted 7 lines

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

Quoted 7 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -3
       /* Allow Read/Write PMU Counters MSR Directly. */
-    for ( i = 0; i < core2_fix_counters.num; i++ )
+    for ( i = 0; i < fixed_pmc_cnt; i++ )
       {
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]), msr_bitmap);
-        clear_bit(msraddr_to_bitpos(core2_fix_counters.msr[i]),
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i),

msr_bitmap);

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

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

Quoted 1 line
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -0
+        clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR0 + i),

Dropping the static array will make the handling here quite a bit more complicated should there ever appear a second dis-contiguous MSR range.

Fixed counters range should always be contiguous per Intel SDM.

Until the current range runs out...

Well, there are 58 free addresses currently available in this range...

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

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

Quoted 13 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -3
@@ -262,32 +244,37 @@ static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
       }
 
       /* Allow Read PMU Non-global Controls Directly. */
-    for ( i = 0; i < core2_ctrls.num; i++ )
-        clear_bit(msraddr_to_bitpos(core2_ctrls.msr[i]), msr_bitmap);
-    for ( i = 0; i < core2_get_pmc_count(); i++ )
+    for ( i = 0; i < arch_pmc_cnt; i++ )
           clear_bit(msraddr_to_bitpos(MSR_P6_EVNTSEL0+i), msr_bitmap);
+
+    clear_bit(msraddr_to_bitpos(MSR_CORE_PERF_FIXED_CTR_CTRL), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_PEBS_ENABLE), msr_bitmap);
+    clear_bit(msraddr_to_bitpos(MSR_IA32_DS_AREA), msr_bitmap);

As you can see, this is already the case here.

Quoted 3 lines
Quoted 1 line

This is a different set of MSRs from from what you've commented on above.

Sure, but the effect of breaking up a loop into individual operations is seen quite nicely here.

Yes, but unlike fixed counters above, the registers in what used to be in core2_ctrls.msr are responsible for different things. And in certain cases we want to access one register but not the other.

An example is in current version of vpmu_dump():
val = core2_vpmu_cxt->ctrls[MSR_CORE_PERF_FIXED_CTR_CTRL_IDX];

We had to add another macro (MSR_CORE_PERF_FIXED_CTR_CTRL_IDX).

So I think that separating these registers explicitly makes sense.

-boris

#20 lore raw

PATCH v2 6/13 x86/PMU: Add public xenpmu.h

In reply to Boris Ostrovsky

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

---
 xen/arch/x86/hvm/svm/vpmu.c              | 15 +++-----
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 43 ++++++++++++----------
 xen/arch/x86/hvm/vpmu.c                  |  1 +
 xen/arch/x86/oprofile/op_model_ppro.c    |  6 +++-
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h | 32 -----------------
 xen/include/asm-x86/hvm/vpmu.h           | 10 ++----
 xen/include/public/arch-x86/xenpmu-x86.h | 62 ++++++++++++++++++++++++++++++++
 xen/include/public/xenpmu.h              | 38 ++++++++++++++++++++
 8 files changed, 136 insertions(+), 71 deletions(-)
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 create mode 100644 xen/include/public/arch-x86/xenpmu-x86.h
 create mode 100644 xen/include/public/xenpmu.h
xen/arch/x86/hvm/svm/vpmu.c +4 -11
index a09930e..25532d0 100644
@@ -30,10 +30,7 @@
 #include <asm/apic.h>
 #include <asm/hvm/vlapic.h>
 #include <asm/hvm/vpmu.h>
-
-#define F10H_NUM_COUNTERS 4
-#define F15H_NUM_COUNTERS 6
-#define MAX_NUM_COUNTERS F15H_NUM_COUNTERS
+#include <public/xenpmu.h>
 
 #define MSR_F10H_EVNTSEL_GO_SHIFT   40
 #define MSR_F10H_EVNTSEL_EN_SHIFT   22
@@ -49,6 +46,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,13 +83,6 @@ static const u32 AMD_F15H_CTRLS[] = {
     MSR_AMD_FAM15H_EVNTSEL5
 };
 
-/* storage for context switching */
-struct amd_vpmu_context {
-    u64 counters[MAX_NUM_COUNTERS];
-    u64 ctrls[MAX_NUM_COUNTERS];
-    bool_t msr_bitmap_set;
-};
-
 static inline int get_pmu_reg_type(u32 addr)
 {
     if ( (addr >= MSR_K7_EVNTSEL0) && (addr <= MSR_K7_EVNTSEL3) )
xen/arch/x86/hvm/vmx/vpmu_core2.c +24 -19

93 lines not on this page · raw message

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

62 lines not on this page · raw message

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

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

Am Freitag 20 September 2013, 05:42:05 schrieb Boris Ostrovsky:

Quoted 295 lines

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

---
 xen/arch/x86/hvm/svm/vpmu.c              | 15 +++-----
 xen/arch/x86/hvm/vmx/vpmu_core2.c        | 43 ++++++++++++----------
 xen/arch/x86/hvm/vpmu.c                  |  1 +
 xen/arch/x86/oprofile/op_model_ppro.c    |  6 +++-
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h | 32 -----------------
 xen/include/asm-x86/hvm/vpmu.h           | 10 ++----
 xen/include/public/arch-x86/xenpmu-x86.h | 62 ++++++++++++++++++++++++++++++++
 xen/include/public/xenpmu.h              | 38 ++++++++++++++++++++
 8 files changed, 136 insertions(+), 71 deletions(-)
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 create mode 100644 xen/include/public/arch-x86/xenpmu-x86.h
 create mode 100644 xen/include/public/xenpmu.h
xen/arch/x86/hvm/svm/vpmu.c +4 -11
index a09930e..25532d0 100644
@@ -30,10 +30,7 @@
 #include <asm/apic.h>
 #include <asm/hvm/vlapic.h>
 #include <asm/hvm/vpmu.h>
-
-#define F10H_NUM_COUNTERS 4
-#define F15H_NUM_COUNTERS 6
-#define MAX_NUM_COUNTERS F15H_NUM_COUNTERS
+#include <public/xenpmu.h>
 
 #define MSR_F10H_EVNTSEL_GO_SHIFT   40
 #define MSR_F10H_EVNTSEL_EN_SHIFT   22
@@ -49,6 +46,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,13 +83,6 @@ static const u32 AMD_F15H_CTRLS[] = {
     MSR_AMD_FAM15H_EVNTSEL5
 };
 
-/* storage for context switching */
-struct amd_vpmu_context {
-    u64 counters[MAX_NUM_COUNTERS];
-    u64 ctrls[MAX_NUM_COUNTERS];
-    bool_t msr_bitmap_set;
-};
-
 static inline int get_pmu_reg_type(u32 addr)
 {
     if ( (addr >= MSR_K7_EVNTSEL0) && (addr <= MSR_K7_EVNTSEL3) )
xen/arch/x86/hvm/vmx/vpmu_core2.c +24 -19
index 50f784f..7d1da3f 100644
@@ -35,8 +35,8 @@
 #include <asm/hvm/vmx/vmcs.h>
 #include <public/sched.h>
 #include <public/hvm/save.h>
+#include <public/xenpmu.h>
 #include <asm/hvm/vpmu.h>
-#include <asm/hvm/vmx/vpmu_core2.h>
 
 /*
  * See Intel SDM Vol 2a Instruction Set Reference chapter 3 for CPUID
@@ -64,6 +64,10 @@
 #define PMU_FIXED_WIDTH_BITS     8  /* 8 bits 5..12 */
 #define PMU_FIXED_WIDTH_MASK     (((1 << PMU_FIXED_WIDTH_BITS) -1) << PMU_FIXED_WIDTH_SHIFT)
 
+/* Intel-specific VPMU features */
+#define VPMU_CPU_HAS_DS                     0x100 /* Has Debug Store */
+#define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
+
 /*
  * MSR_CORE_PERF_FIXED_CTR_CTRL contains the configuration of all fixed
  * counters. 4 bits for every counter.
@@ -71,16 +75,6 @@
 #define FIXED_CTR_CTRL_BITS 4
 #define FIXED_CTR_CTRL_MASK ((1 << FIXED_CTR_CTRL_BITS) - 1)
 
-#define VPMU_CORE2_MAX_FIXED_PMCS     4
-struct core2_vpmu_context {
-    u64 fixed_ctrl;
-    u64 ds_area;
-    u64 pebs_enable;
-    u64 global_ovf_status;
-    u64 fix_counters[VPMU_CORE2_MAX_FIXED_PMCS];
-    struct arch_msr_pair arch_msr_pair[1];
-};
-
 static int arch_pmc_cnt; /* Number of general-purpose performance counters */
 static int fixed_pmc_cnt; /* Number of fixed performance counters */
 
@@ -225,6 +219,7 @@ static int is_core2_vpmu_msr(u32 msr_index, int *type, int *index)
     return 0;
 }
 
+#define msraddr_to_bitpos(x) (((x)&0xffff) + ((x)>>31)*0x2000)
 static void core2_vpmu_set_msr_bitmap(unsigned long *msr_bitmap)
 {
     int i;
@@ -349,8 +344,7 @@ static int core2_vpmu_alloc_resource(struct vcpu *v)
     vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL,
                  core2_calc_intial_glb_ctrl_msr());
 
-    core2_vpmu_cxt = xzalloc_bytes(sizeof(struct core2_vpmu_context) +
-                    (arch_pmc_cnt-1)*sizeof(struct arch_msr_pair));
+    core2_vpmu_cxt = xzalloc_bytes(sizeof(struct core2_vpmu_context));
     if ( !core2_vpmu_cxt )
         goto out_err;
 
@@ -614,6 +608,18 @@ static void core2_vpmu_do_cpuid(unsigned int input,
                 *ecx |= cpufeat_mask(X86_FEATURE_DSCPL);
         }
     }
+    else if ( input == 0xa )
+    {
+        /* Limit number of counters to max that we support */
+        if ( ((*eax & PMU_GENERAL_NR_MASK) >> PMU_GENERAL_NR_SHIFT) >
+             XENPMU_CORE2_MAX_ARCH_PMCS )
+            *eax = (*eax & ~PMU_GENERAL_NR_MASK) |
+                (XENPMU_CORE2_MAX_ARCH_PMCS << PMU_GENERAL_NR_SHIFT);
+        if ( ((*edx & PMU_FIXED_NR_MASK) >> PMU_FIXED_NR_SHIFT) >
+             XENPMU_CORE2_MAX_FIXED_PMCS )
+            *eax = (*eax & ~PMU_FIXED_NR_MASK) |
+                (XENPMU_CORE2_MAX_FIXED_PMCS << PMU_FIXED_NR_SHIFT);
+    }
 }
 
 /* Dump vpmu info on console, called in the context of keyhandler 'q'. */
@@ -641,11 +647,10 @@ static void core2_vpmu_dump(struct vcpu *v)
 
     /* Print the contents of the counter and its configuration msr. */
     for ( i = 0; i < arch_pmc_cnt; i++ )
-    {
-        struct arch_msr_pair* msr_pair = core2_vpmu_cxt->arch_msr_pair;
         printk("      general_%d: 0x%016lx ctrl: 0x%016lx\n",
-               i, msr_pair[i].counter, msr_pair[i].control);
-    }
+               i, core2_vpmu_cxt->arch_msr_pair[i].counter,
+               core2_vpmu_cxt->arch_msr_pair[i].control);
+
     /*
      * The configuration of the fixed counter is 4 bits each in the
      * MSR_CORE_PERF_FIXED_CTR_CTRL.
@@ -739,8 +744,8 @@ func_out:
 
     arch_pmc_cnt = core2_get_arch_pmc_count();
     fixed_pmc_cnt = core2_get_fixed_pmc_count();
-    if ( fixed_pmc_cnt > VPMU_CORE2_MAX_FIXED_PMCS )
-        fixed_pmc_cnt = VPMU_CORE2_MAX_FIXED_PMCS;
+    if ( fixed_pmc_cnt > XENPMU_CORE2_MAX_FIXED_PMCS )
+        fixed_pmc_cnt = XENPMU_CORE2_MAX_FIXED_PMCS;
     check_pmc_quirk();
 
     return 0;
xen/arch/x86/hvm/vpmu.c +1 -0
index d6a9ff6..fa8cfd7 100644
@@ -31,6 +31,7 @@
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/apic.h>
+#include <public/xenpmu.h>
 
 /*
  * "vpmu" :     vpmu generally enabled
xen/arch/x86/oprofile/op_model_ppro.c +5 -1
index 3225937..5aae2e7 100644
@@ -20,11 +20,15 @@
 #include <asm/regs.h>
 #include <asm/current.h>
 #include <asm/hvm/vpmu.h>
-#include <asm/hvm/vmx/vpmu_core2.h>
 
 #include "op_x86_model.h"
 #include "op_counter.h"
 
+struct arch_msr_pair {
+    u64 counter;
+    u64 control;
+};
+
 /*
  * Intel "Architectural Performance Monitoring" CPUID
  * detection/enumeration details:
xen/include/asm-x86/hvm/vmx/vpmu_core2.h deleted +0 -32
deleted file mode 100644
index 410372d..0000000
@@ -1,32 +0,0 @@
-
-/*
- * vpmu_core2.h: CORE 2 specific PMU virtualization for HVM domain.
- *
- * Copyright (c) 2007, Intel Corporation.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms and conditions of the GNU General Public License,
- * version 2, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307 USA.
- *
- * Author: Haitao Shan <[email protected]>
- */
-
-#ifndef __ASM_X86_HVM_VPMU_CORE_H_
-#define __ASM_X86_HVM_VPMU_CORE_H_
-
-struct arch_msr_pair {
-    u64 counter;
-    u64 control;
-};
-
-#endif /* __ASM_X86_HVM_VPMU_CORE_H_ */
-
xen/include/asm-x86/hvm/vpmu.h +2 -8
index 674cdad..50cdc4f 100644
@@ -22,6 +22,8 @@
 #ifndef __ASM_X86_HVM_VPMU_H_
 #define __ASM_X86_HVM_VPMU_H_
 
+#include <public/xenpmu.h>
+
 /*
  * Flag bits given as a string on the hypervisor boot parameter 'vpmu'.
  * See arch/x86/hvm/vpmu.c.
@@ -29,12 +31,9 @@
 #define VPMU_BOOT_ENABLED 0x1    /* vpmu generally enabled. */
 #define VPMU_BOOT_BTS     0x2    /* Intel BTS feature wanted. */
 
-
-#define msraddr_to_bitpos(x) (((x)&0xffff) + ((x)>>31)*0x2000)
 #define vcpu_vpmu(vcpu)   (&((vcpu)->arch.hvm_vcpu.vpmu))
 #define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, \
                                           arch.hvm_vcpu.vpmu))
-#define vpmu_domain(vpmu) (vpmu_vcpu(vpmu)->domain)
 
 #define MSR_TYPE_COUNTER            0
 #define MSR_TYPE_CTRL               1
@@ -76,11 +75,6 @@ struct vpmu_struct {
 #define VPMU_FROZEN                         0x10  /* Stop counters while VCPU is not running */
 #define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x20
 
-/* VPMU features */
-#define VPMU_CPU_HAS_DS                     0x100 /* Has Debug Store */
-#define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
-
-
 #define vpmu_set(_vpmu, _x)         ((_vpmu)->flags |= (_x))
 #define vpmu_reset(_vpmu, _x)       ((_vpmu)->flags &= ~(_x))
 #define vpmu_is_set(_vpmu, _x)      ((_vpmu)->flags & (_x))
xen/include/public/arch-x86/xenpmu-x86.h new file +17 -0
new file mode 100644
index 0000000..04e02b3
@@ -0,0 +1,62 @@
+#ifndef __XEN_PUBLIC_ARCH_X86_PMU_H__
+#define __XEN_PUBLIC_ARCH_X86_PMU_H__
+
+/* x86-specific PMU definitions */
+
+
+/* AMD PMU registers and structures */
+#define XENPMU_AMD_MAX_COUNTERS        16 /* To accommodate more counters in */
+                                          /* the future (e.g. NB counters)   */
+struct amd_vpmu_context {
+    uint64_t counters[XENPMU_AMD_MAX_COUNTERS];
+    uint64_t ctrls[XENPMU_AMD_MAX_COUNTERS];
+    uint8_t msr_bitmap_set;               /* Used by HVM only */
+};
+
+/* Intel PMU registers and structures */
+#define XENPMU_CORE2_MAX_ARCH_PMCS     16

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

Quoted 10 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +10 -0
+#define XENPMU_CORE2_MAX_FIXED_PMCS    4
+struct core2_vpmu_context {
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t global_ovf_status;
+    uint64_t fixed_ctrl;
+    uint64_t ds_area;
+    uint64_t pebs_enable;
+    uint64_t debugctl;

What is debugctl for? I couldn't find it in the other patches.

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

Quoted 8 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +8 -0
+    uint64_t fix_counters[XENPMU_CORE2_MAX_FIXED_PMCS];
+    struct {
+        uint64_t counter;
+        uint64_t control;
+    } arch_msr_pair[XENPMU_CORE2_MAX_ARCH_PMCS];
+};
+
+#define MAX(x, y)                 ((x) > (y) ? (x) : (y))

Maybe using MAX() from xen/kernel.h ?

Dietmar.

Quoted 72 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +27 -0
+#define XENPMU_MAX_CTXT_SZ        MAX(sizeof(struct amd_vpmu_context),\
+                                      sizeof(struct core2_vpmu_context))
+#define XENPMU_CTXT_PAD_SZ        (((XENPMU_MAX_CTXT_SZ + 64) & ~63) + 128)
+struct arch_xenpmu {
+    union {
+        struct cpu_user_regs regs;
+        uint8_t pad2[256];
+    };
+    union {
+        struct amd_vpmu_context amd;
+        struct core2_vpmu_context intel;
+        uint8_t pad1[XENPMU_CTXT_PAD_SZ];
+    };
+};
+typedef struct arch_xenpmu arch_xenpmu_t;
+
+#endif /* __XEN_PUBLIC_ARCH_X86_PMU_H__ */
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
xen/include/public/xenpmu.h new file +38 -0
new file mode 100644
index 0000000..fbacd7e
@@ -0,0 +1,38 @@
+#ifndef __XEN_PUBLIC_XENPMU_H__
+#define __XEN_PUBLIC_XENPMU_H__
+
+#include "xen.h"
+#if defined(__i386__) || defined(__x86_64__)
+#include "arch-x86/xenpmu-x86.h"
+#elif defined (__arm__) || defined (__aarch64__)
+#include "arch-arm.h"
+#else
+#error "Unsupported architecture"
+#endif
+
+#define XENPMU_VER_MAJ    0
+#define XENPMU_VER_MIN    0
+
+
+/* Shared between hypervisor and PV domain */
+struct xenpmu_data {
+    uint32_t domain_id;
+    uint32_t vcpu_id;
+    uint32_t pcpu_id;
+    uint32_t pmu_flags;
+
+    arch_xenpmu_t pmu;
+};
+typedef struct xenpmu_data xenpmu_data_t;
+
+#endif /* __XEN_PUBLIC_XENPMU_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 1 line
Company details: http://ts.fujitsu.com/imprint.html
#22 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Dietmar Hahn

Am Freitag 20 September 2013, 05:42:05 schrieb Boris Ostrovsky:

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

Quoted 8 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +8 -0
+    uint64_t fix_counters[XENPMU_CORE2_MAX_FIXED_PMCS];
+    struct {
+        uint64_t counter;
+        uint64_t control;
+    } arch_msr_pair[XENPMU_CORE2_MAX_ARCH_PMCS];
+};
+
+#define MAX(x, y)                 ((x) > (y) ? (x) : (y))

Maybe using MAX() from xen/kernel.h ?

Certainly not - this is a public header; a definition of MAX() is as misplaced here.

Jan

#23 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Dietmar Hahn

Am Freitag 20 September 2013, 05:42:05 schrieb Boris Ostrovsky:

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

Quoted 8 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +8 -0
+    uint64_t fix_counters[XENPMU_CORE2_MAX_FIXED_PMCS];
+    struct {
+        uint64_t counter;
+        uint64_t control;
+    } arch_msr_pair[XENPMU_CORE2_MAX_ARCH_PMCS];
+};
+
+#define MAX(x, y)                 ((x) > (y) ? (x) : (y))

Maybe using MAX() from xen/kernel.h ?

Quoted 3 lines

Certainly not - this is a public header; a definition of MAX() is as misplaced here.

Since I use MAX only once and only here I will just drop it and change XENPMU_MAX_CTXT_SZ which is

#define XENPMU_MAX_CTXT_SZ MAX(sizeof(struct amd_vpmu_context),\
sizeof(struct core2_vpmu_context))

into

#define XENPMU_MAX_CTXT_SZ (sizeof(struct amd_vpmu_context) >\
sizeof(struct core2_vpmu_context) ?\
sizeof(struct amd_vpmu_context) :\
sizeof(struct core2_vpmu_context))

-boris

#24 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

In reply to Dietmar Hahn
Quoted 15 lines · Dietmar Hahn

Am Freitag 20 September 2013, 05:42:05 schrieb Boris Ostrovsky:

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

Quoted 13 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +13 -0
+
+/* Intel PMU registers and structures */
+#define XENPMU_CORE2_MAX_ARCH_PMCS     16
+#define XENPMU_CORE2_MAX_FIXED_PMCS    4
+struct core2_vpmu_context {
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t global_ovf_status;
+    uint64_t fixed_ctrl;
+    uint64_t ds_area;
+    uint64_t pebs_enable;
+    uint64_t debugctl;

What is debugctl for? I couldn't find it in the other patches.

Right. I added it because I am pretty sure it will be needed to make BTS and PEBS work for PV. And because this structure is part of the interface I thought I should put it in now.

-boris

#25 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

In reply to Boris Ostrovsky

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

Quoted 6 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/public/arch-x86/xenpmu-x86.h new file +5 -0
+struct amd_vpmu_context {
+    uint64_t counters[XENPMU_AMD_MAX_COUNTERS];
+    uint64_t ctrls[XENPMU_AMD_MAX_COUNTERS];
+    uint8_t msr_bitmap_set;               /* Used by HVM only */
+};

sizeof() this will not be the same for this in a 64-bit an a 32-bit guest. Are you intentionally creating a need for translation here?

Quoted 9 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +9 -0
+
+/* Intel PMU registers and structures */
+#define XENPMU_CORE2_MAX_ARCH_PMCS     16
+#define XENPMU_CORE2_MAX_FIXED_PMCS    4
+struct core2_vpmu_context {
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t global_ovf_status;

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

Quoted 10 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +10 -0
+    uint64_t fixed_ctrl;
+    uint64_t ds_area;
+    uint64_t pebs_enable;
+    uint64_t debugctl;
+    uint64_t fix_counters[XENPMU_CORE2_MAX_FIXED_PMCS];
+    struct {
+        uint64_t counter;
+        uint64_t control;
+    } arch_msr_pair[XENPMU_CORE2_MAX_ARCH_PMCS];
+};

I realize that using embedded arrays in both AMD and Intel structures makes things easier to implement, but it reduces forward compatibility. I'd therefore prefer those to be made handles.

Quoted 1 line

+#define MAX(x, y) ((x) > (y) ? (x) : (y))

I think we already agreed in the context of someone else's reply that this has to go away.

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

Quoted 3 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +3 -0
+struct arch_xenpmu {
+    union {
+        struct cpu_user_regs regs;

Oh, so you need to do translation for 32-bit guests anyway...

xen/include/public/xenpmu.h line 8

Quoted 11 lines
xen/include/public/xenpmu.h new file +8 -0
@@ -0,0 +1,38 @@
+#ifndef __XEN_PUBLIC_XENPMU_H__
+#define __XEN_PUBLIC_XENPMU_H__
+
+#include "xen.h"
+#if defined(__i386__) || defined(__x86_64__)
+#include "arch-x86/xenpmu-x86.h"
+#elif defined (__arm__) || defined (__aarch64__)
+#include "arch-arm.h"

???

Jan

#26 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

On 09/25/2013 10:04 AM, Jan Beulich wrote

Quoted 4 lines
Quoted 4 lines
xen/include/public/xenpmu.h new file +1 -0
@@ -0,0 +1,38 @@
+#ifndef __XEN_PUBLIC_XENPMU_H__
Quoted 8 lines

xen/include/public/xenpmu.h line 8

Quoted 7 lines
xen/include/public/xenpmu.h new file +7 -0
+#define __XEN_PUBLIC_XENPMU_H__
+
+#include "xen.h"
+#if defined(__i386__) || defined(__x86_64__)
+#include "arch-x86/xenpmu-x86.h"
+#elif defined (__arm__) || defined (__aarch64__)
+#include "arch-arm.h"

???

I need to define arch_xenpmu_t in all architectures. This is what the shared structure looks like:

/* Shared between hypervisor and PV domain */

struct xenpmu_data {
    uint32_t domain_id;
    uint32_t vcpu_id;
    uint32_t pcpu_id;
    uint32_t pmu_flags;

    arch_xenpmu_t pmu;
};

-boris

#27 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

Quoted 16 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

On 09/25/2013 10:04 AM, Jan Beulich wrote

Quoted 4 lines
Quoted 4 lines
xen/include/public/xenpmu.h new file +1 -0
@@ -0,0 +1,38 @@
+#ifndef __XEN_PUBLIC_XENPMU_H__
Quoted 8 lines

xen/include/public/xenpmu.h line 8

Quoted 7 lines
xen/include/public/xenpmu.h new file +7 -0
+#define __XEN_PUBLIC_XENPMU_H__
+
+#include "xen.h"
+#if defined(__i386__) || defined(__x86_64__)
+#include "arch-x86/xenpmu-x86.h"
+#elif defined (__arm__) || defined (__aarch64__)
+#include "arch-arm.h"

???

Quoted 1 line

I need to define arch_xenpmu_t in all architectures.

Right, but why not arch-arm/xenpmu-arm.h? (And maybe you could stick a few more arms here and there - read: why the x86 duplication in the x86 name?)

Jan

#28 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

In reply to Jan Beulich
Quoted 24 lines · Jan Beulich

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

Quoted 6 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/public/arch-x86/xenpmu-x86.h new file +5 -0
+struct amd_vpmu_context {
+    uint64_t counters[XENPMU_AMD_MAX_COUNTERS];
+    uint64_t ctrls[XENPMU_AMD_MAX_COUNTERS];
+    uint8_t msr_bitmap_set;               /* Used by HVM only */
+};

sizeof() this will not be the same for this in a 64-bit an a 32-bit guest. Are you intentionally creating a need for translation here?

Quoted 15 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +15 -0
+
+/* Intel PMU registers and structures */
+#define XENPMU_CORE2_MAX_ARCH_PMCS     16
+#define XENPMU_CORE2_MAX_FIXED_PMCS    4
+struct core2_vpmu_context {
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t global_ovf_status;
+    uint64_t fixed_ctrl;
+    uint64_t ds_area;
+    uint64_t pebs_enable;
+    uint64_t debugctl;
+    uint64_t fix_counters[XENPMU_CORE2_MAX_FIXED_PMCS];
+    struct {
Quoted 8 lines

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

Quoted 4 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +4 -0
+        uint64_t counter;
+        uint64_t control;
+    } arch_msr_pair[XENPMU_CORE2_MAX_ARCH_PMCS];
+};

I realize that using embedded arrays in both AMD and Intel structures makes things easier to implement, but it reduces forward compatibility. I'd therefore prefer those to be made handles.

(I missed this comment earlier).

This is not done because it's easier but because I need to keep the structure in a shared page.

-boris

#29 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

Quoted 26 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/public/arch-x86/xenpmu-x86.h new file +14 -0
+/* Intel PMU registers and structures */
+#define XENPMU_CORE2_MAX_ARCH_PMCS     16
+#define XENPMU_CORE2_MAX_FIXED_PMCS    4
+struct core2_vpmu_context {
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t global_ovf_status;
+    uint64_t fixed_ctrl;
+    uint64_t ds_area;
+    uint64_t pebs_enable;
+    uint64_t debugctl;
+    uint64_t fix_counters[XENPMU_CORE2_MAX_FIXED_PMCS];
+    struct {
Quoted 8 lines

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

Quoted 4 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +4 -0
+        uint64_t counter;
+        uint64_t control;
+    } arch_msr_pair[XENPMU_CORE2_MAX_ARCH_PMCS];
+};

I realize that using embedded arrays in both AMD and Intel structures makes things easier to implement, but it reduces forward compatibility. I'd therefore prefer those to be made handles.

Quoted 4 lines

(I missed this comment earlier).

This is not done because it's easier but because I need to keep the structure in a shared page.

Then a more dynamic layout (with just the array base offset in the page specified in the structure) would still be preferable as being more extensible.

Jan

#30 lore raw

Re: [PATCH v2 06/13] x86/PMU: Add public xenpmu.h

Quoted 25 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 23 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/public/arch-x86/xenpmu-x86.h new file +8 -0
+/* Intel PMU registers and structures */
+#define XENPMU_CORE2_MAX_ARCH_PMCS     16
+#define XENPMU_CORE2_MAX_FIXED_PMCS    4
+struct core2_vpmu_context {
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t global_ovf_status;

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

Quoted 10 lines
xen/include/public/arch-x86/xenpmu-x86.h new file +10 -0
+    uint64_t fixed_ctrl;
+    uint64_t ds_area;
+    uint64_t pebs_enable;
+    uint64_t debugctl;
+    uint64_t fix_counters[XENPMU_CORE2_MAX_FIXED_PMCS];
+    struct {
+        uint64_t counter;
+        uint64_t control;
+    } arch_msr_pair[XENPMU_CORE2_MAX_ARCH_PMCS];
+};

I realize that using embedded arrays in both AMD and Intel structures makes things easier to implement, but it reduces forward compatibility. I'd therefore prefer those to be made handles.

Quoted 9 lines
Quoted 4 lines

(I missed this comment earlier).

This is not done because it's easier but because I need to keep the structure in a shared page.

Then a more dynamic layout (with just the array base offset in the page specified in the structure) would still be preferable as being more extensible.

I'll see how I can do this, I am afraid this may end up being a bit too convoluted (the arrays will still need to be part of some structure with decribes a context).

-boris

#31 lore raw

PATCH v2 7/13 x86/PMU: 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 | 4 ++--
 3 files changed, 4 insertions(+), 5 deletions(-)
xen/include/asm-x86/domain.h +2 -0
index d79464d..4f2247e 100644
@@ -397,6 +397,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 e8b8cd7..207f65d 100644
@@ -139,9 +139,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 -2
index 50cdc4f..051f4f0 100644
@@ -31,9 +31,9 @@
 #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 vcpu_vpmu(vcpu)   (&((vcpu)->arch.vpmu))
 #define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, \
-                                          arch.hvm_vcpu.vpmu))
+                                          arch.vpmu))
 
 #define MSR_TYPE_COUNTER            0
 #define MSR_TYPE_CTRL               1
Signature · 1 line
1.8.1.4
#32 lore raw

Re: [PATCH v2 07/13] x86/PMU: Make vpmu not HVM-specific

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

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/asm-x86/hvm/vpmu.h +2 -2
@@ -31,9 +31,9 @@
 #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 vcpu_vpmu(vcpu)   (&((vcpu)->arch.vpmu))
 #define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, \
-                                          arch.hvm_vcpu.vpmu))
+                                          arch.vpmu))

This clearly needs to be moved out of a HVM specific header the too.

Jan

#33 lore raw

Re: [PATCH v2 07/13] x86/PMU: Make vpmu not HVM-specific

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/asm-x86/hvm/vpmu.h +0 -0
@@ -31,9 +31,9 @@
  #define VPMU_BOOT_ENABLED 0x1    /* vpmu generally enabled. */
  #define VPMU_BOOT_BTS     0x2    /* Intel BTS feature wanted. */
 
Quoted 6 lines

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

Quoted 5 lines
xen/include/asm-x86/hvm/vpmu.h +2 -2
-#define vcpu_vpmu(vcpu)   (&((vcpu)->arch.hvm_vcpu.vpmu))
+#define vcpu_vpmu(vcpu)   (&((vcpu)->arch.vpmu))
  #define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, \
-                                          arch.hvm_vcpu.vpmu))
+                                          arch.vpmu))

This clearly needs to be moved out of a HVM specific header the too.

The last patch of the series moves all vpmu* files up from HVM.

-boris

#34 lore raw

Re: [PATCH v2 07/13] x86/PMU: Make vpmu not HVM-specific

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/asm-x86/hvm/vpmu.h +0 -0
@@ -31,9 +31,9 @@
  #define VPMU_BOOT_ENABLED 0x1    /* vpmu generally enabled. */
  #define VPMU_BOOT_BTS     0x2    /* Intel BTS feature wanted. */
 
Quoted 8 lines
Quoted 6 lines

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

Quoted 5 lines
xen/include/asm-x86/hvm/vpmu.h +2 -2
-#define vcpu_vpmu(vcpu)   (&((vcpu)->arch.hvm_vcpu.vpmu))
+#define vcpu_vpmu(vcpu)   (&((vcpu)->arch.vpmu))
  #define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, \
-                                          arch.hvm_vcpu.vpmu))
+                                          arch.vpmu))

This clearly needs to be moved out of a HVM specific header the too.

The last patch of the series moves all vpmu* files up from HVM.

Yes, I got to that by now. Kind of counter intuitive though.

Jan

#35 lore raw

PATCH v2 8/13 x86/PMU: Interface for setting PMU mode and flags

In reply to Boris Ostrovsky

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

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

Mode and flags are set via new PMU hypercall.

---
 xen/arch/x86/hvm/svm/vpmu.c        |  2 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c  |  4 +-
 xen/arch/x86/hvm/vpmu.c            | 79 ++++++++++++++++++++++++++++++++++----
 xen/arch/x86/x86_64/compat/entry.S |  4 ++
 xen/arch/x86/x86_64/entry.S        |  4 ++
 xen/include/asm-x86/hvm/vpmu.h     |  9 +----
 xen/include/public/xen.h           |  1 +
 xen/include/public/xenpmu.h        | 53 +++++++++++++++++++++++++
 xen/include/xen/hypercall.h        |  4 ++
 9 files changed, 142 insertions(+), 18 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +1 -1
index 25532d0..99d0137 100644
@@ -461,7 +461,7 @@ int svm_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
     int ret = 0;
 
     /* vpmu enabled? */
-    if ( !vpmu_flags )
+    if ( vpmu_flags == XENPMU_MODE_OFF )
         return 0;
 
     switch ( family )
xen/arch/x86/hvm/vmx/vpmu_core2.c +2 -2
index 7d1da3f..7dae451 100644
@@ -702,7 +702,7 @@ static int core2_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
     u64 msr_content;
     struct cpuinfo_x86 *c = &current_cpu_data;
 
-    if ( !(vpmu_flags & VPMU_BOOT_BTS) )
+    if ( !(vpmu_flags & XENPMU_FLAGS_INTEL_BTS) )
         goto func_out;
     /* Check the 'Debug Store' feature in the CPUID.EAX[1]:EDX[21] */
     if ( cpu_has(c, X86_FEATURE_DS) )
@@ -823,7 +823,7 @@ int vmx_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
     int ret = 0;
 
     vpmu->arch_vpmu_ops = &core2_no_vpmu_ops;
-    if ( !vpmu_flags )
+    if ( vpmu_flags == XENPMU_MODE_OFF )
         return 0;
 
     if ( family == 6 )
xen/arch/x86/hvm/vpmu.c +71 -8

120 lines not on this page · raw message

xen/arch/x86/x86_64/compat/entry.S +4 -0
index c0afe2c..bc03ffe 100644
@@ -413,6 +413,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
@@ -461,6 +463,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 5beeccb..2944427 100644
@@ -762,6 +762,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
@@ -810,6 +812,8 @@ ENTRY(hypercall_args_table)
         .byte 1 /* do_domctl            */
         .byte 2 /* do_kexec             */
         .byte 1 /* do_tmem_op           */
+        .byte 0 /* reserved for XenClient */
+        .byte 2 /* do_xenpmu_op         */  /* 40 */
         .rept __HYPERVISOR_arch_0-(.-hypercall_args_table)
         .byte 0 /* do_ni_hypercall      */
         .endr
xen/include/asm-x86/hvm/vpmu.h +2 -7
index 051f4f0..87cef41 100644
@@ -24,13 +24,6 @@
 
 #include <public/xenpmu.h>
 
-/*
- * Flag bits given as a string on the hypervisor boot parameter 'vpmu'.
- * See arch/x86/hvm/vpmu.c.
- */
-#define VPMU_BOOT_ENABLED 0x1    /* vpmu generally enabled. */
-#define VPMU_BOOT_BTS     0x2    /* Intel BTS feature wanted. */
-
 #define vcpu_vpmu(vcpu)   (&((vcpu)->arch.vpmu))
 #define vpmu_vcpu(vpmu)   (container_of((vpmu), struct vcpu, \
                                           arch.vpmu))
@@ -95,5 +88,7 @@ void vpmu_dump(struct vcpu *v);
 extern int acquire_pmu_ownership(int pmu_ownership);
 extern void release_pmu_ownership(int pmu_ownership);
 
+extern uint32_t vpmu_mode;
+
 #endif /* __ASM_X86_HVM_VPMU_H_*/
 
xen/include/public/xen.h +1 -0
index 3cab74f..7f56560 100644
@@ -101,6 +101,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define __HYPERVISOR_kexec_op             37
 #define __HYPERVISOR_tmem_op              38
 #define __HYPERVISOR_xc_reserved_op       39 /* reserved for XenClient */
+#define __HYPERVISOR_xenpmu_op            40
 
 /* Architecture-specific hypercall definitions. */
 #define __HYPERVISOR_arch_0               48
xen/include/public/xenpmu.h +53 -0

59 lines not on this page · raw message

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

Re: [PATCH v2 08/13] x86/PMU: Interface for setting PMU mode and flags

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

Quoted 11 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/public/xen.h +1 -0
@@ -101,6 +101,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define __HYPERVISOR_kexec_op             37
 #define __HYPERVISOR_tmem_op              38
 #define __HYPERVISOR_xc_reserved_op       39 /* reserved for XenClient */
+#define __HYPERVISOR_xenpmu_op            40
 
 /* Architecture-specific hypercall definitions. */
 #define __HYPERVISOR_arch_0               48

I wonder whether you wouldn't better use an arch-specific hypercall number here - there's really very little that's generic in what I have seen so far.

xen/include/public/xen.h

Quoted 13 lines
xen/include/public/xen.h +13 -0
+/* Parameters structure for HYPERVISOR_xenpmu_op call */
+struct xenpmu_params {
+    /* IN/OUT parameters */
+    union {
+        struct version {
+            uint8_t maj;
+            uint8_t min;
+        } version;
+        uint64_t pad;
+    };
+    union {
+        uint64_t val;
+        void *valp;

Without there also being a handle here I can't see how you could make use of the pointer.

xen/include/public/xen.h

Quoted 4 lines
xen/include/public/xen.h +4 -0
+    };
+
+    /* IN parameters */
+    uint64_t vcpu;

Do you really want a 64-bit quantity here?

xen/include/public/xen.h do_tmem_op( · line 144

Quoted 6 lines
xen/include/public/xen.h +2 -0
@@ -139,6 +140,9 @@ do_tmem_op(
 extern long
 do_xenoprof_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);
 
+extern long
+do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);

This seems wrong - the interface above makes it so that you would only ever pass this a struct xenpmu_params, so the handle should be of that kind from the beginning.

Jan

#37 lore raw

Re: [PATCH v2 08/13] x86/PMU: Interface for setting PMU mode and flags

Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/include/public/xen.h +1 -0
@@ -101,6 +101,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
  #define __HYPERVISOR_kexec_op             37
  #define __HYPERVISOR_tmem_op              38
  #define __HYPERVISOR_xc_reserved_op       39 /* reserved for XenClient */
+#define __HYPERVISOR_xenpmu_op            40
 
Quoted 5 lines

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

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

I wonder whether you wouldn't better use an arch-specific hypercall number here - there's really very little that's generic in what I have seen so far.

Some of xenpmu commands (querying PMU mode/flags, PMU initialization/de-initialization) seem generic to me. They may call arch-specific routines.

Quoted 7 lines
Quoted 7 lines
xen/include/public/xen.h +7 -0
+/* Parameters structure for HYPERVISOR_xenpmu_op call */
+struct xenpmu_params {
+    /* IN/OUT parameters */
+    union {
+        struct version {
+            uint8_t maj;
+            uint8_t min;
Quoted 8 lines

xen/include/public/xen.h

Quoted 6 lines
xen/include/public/xen.h +6 -0
+        } version;
+        uint64_t pad;
+    };
+    union {
+        uint64_t val;
+        void *valp;

Without there also being a handle here I can't see how you could make use of the pointer.

Tha't really a placeholder, valp is not used anywhere. I can replace it with XEN_GUEST_HANDLE.

-boris

#38 lore raw

PATCH v2 9/13 x86/PMU: Initialize PMU for PV guests

In reply to Boris Ostrovsky

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

---
 xen/arch/x86/hvm/svm/vpmu.c       |  34 ++++++------
 xen/arch/x86/hvm/vmx/vpmu_core2.c |  51 ++++++++++++------
 xen/arch/x86/hvm/vpmu.c           | 111 +++++++++++++++++++++++++++++++++++++-
 xen/common/event_channel.c        |   1 +
 xen/include/asm-x86/hvm/vpmu.h    |   1 +
 xen/include/public/xen.h          |   1 +
 xen/include/public/xenpmu.h       |   2 +
 xen/include/xen/softirq.h         |   1 +
 8 files changed, 170 insertions(+), 32 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +19 -15
index 99d0137..527a1de 100644
@@ -370,14 +370,19 @@ static int amd_vpmu_initialise(struct vcpu *v)
 	 }
     }
 
-    ctxt = xzalloc(struct amd_vpmu_context);
-    if ( !ctxt )
+    if ( is_hvm_domain(v->domain) )
     {
-        gdprintk(XENLOG_WARNING, "Insufficient memory for PMU, "
-            " PMU feature is unavailable on domain %d vcpu %d.\n",
-            v->vcpu_id, v->domain->domain_id);
-        return -ENOMEM;
+        ctxt = xzalloc(struct amd_vpmu_context);
+        if ( !ctxt )
+        {
+            gdprintk(XENLOG_WARNING, "Insufficient memory for PMU, "
+                " PMU feature is unavailable on domain %d vcpu %d.\n",
+                v->vcpu_id, v->domain->domain_id);
+                return -ENOMEM;
+        }
     }
+    else
+        ctxt = &v->arch.vpmu.xenpmu_data->pmu.amd;
 
     vpmu->context = ctxt;
     vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
@@ -391,18 +396,17 @@ static void amd_vpmu_destroy(struct vcpu *v)
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         return;
 
-    if ( is_hvm_domain(v->domain) &&
-         ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
-        amd_vpmu_unset_msr_bitmap(v);
-
-    xfree(vpmu->context);
-    vpmu_reset(vpmu, VPMU_CONTEXT_ALLOCATED);
-
-    if ( vpmu_is_set(vpmu, VPMU_RUNNING) )
+    if ( is_hvm_domain(v->domain) )
     {
-        vpmu_reset(vpmu, VPMU_RUNNING);
+        if ( ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
+            amd_vpmu_unset_msr_bitmap(v);
+
+        xfree(vpmu->context);
         release_pmu_ownship(PMU_OWNER_HVM);
     }
+
+    vpmu->context = NULL;
+    vpmu_clear(vpmu);
 }
 
 /* VPMU part of the 'q' keyhandler */
xen/arch/x86/hvm/vmx/vpmu_core2.c +35 -16

78 lines not on this page · raw message

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

142 lines not on this page · raw message

xen/common/event_channel.c +1 -0
index 64c976b..9ee6e5a 100644
@@ -107,6 +107,7 @@ static int virq_is_global(uint32_t virq)
     case VIRQ_TIMER:
     case VIRQ_DEBUG:
     case VIRQ_XENOPROF:
+    case VIRQ_XENPMU:
         rc = 0;
         break;
     case VIRQ_ARCH_0 ... VIRQ_ARCH_7:
xen/include/asm-x86/hvm/vpmu.h +1 -0
index 87cef41..e046afd 100644
@@ -58,6 +58,7 @@ struct vpmu_struct {
     u32 hw_lapic_lvtpc;
     void *context;
     struct arch_vpmu_ops *arch_vpmu_ops;
+    xenpmu_data_t *xenpmu_data;
 };
 
 /* VPMU states */
xen/include/public/xen.h +1 -0
index 7f56560..91d3db2 100644
@@ -161,6 +161,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define VIRQ_MEM_EVENT  10 /* G. (DOM0) A memory event has occured           */
 #define VIRQ_XC_RESERVED 11 /* G. Reserved for XenClient                     */
 #define VIRQ_ENOMEM     12 /* G. (DOM0) Low on heap memory       */
+#define VIRQ_XENPMU     13 /* V.  PMC interrupt                              */
 
 /* Architecture-specific VIRQ definitions. */
 #define VIRQ_ARCH_0    16
xen/include/public/xenpmu.h +2 -0
index 7f5c65c..ec49097 100644
@@ -25,6 +25,8 @@
 #define XENPMU_mode_set        1
 #define XENPMU_flags_get       2
 #define XENPMU_flags_set       3
+#define XENPMU_init            4
+#define XENPMU_finish          5
 /* ` } */
 
 /* Parameters structure for HYPERVISOR_xenpmu_op call */
xen/include/xen/softirq.h +1 -0
index 0c0d481..5829fa4 100644
@@ -8,6 +8,7 @@ enum {
     NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ,
     RCU_SOFTIRQ,
     TASKLET_SOFTIRQ,
+    PMU_SOFTIRQ,
     NR_COMMON_SOFTIRQS
 };
 
Signature · 1 line
1.8.1.4
#39 lore raw

PATCH v2 10/13 x86/PMU: Add support for PMU registes handling on PV guests

In reply to Boris Ostrovsky

Intercept accesses to PMU MSRs and LVTPC APIC vector (only APIC_LVT_MASKED bit is processed) and process them in VPMU module.

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

---
 xen/arch/x86/domain.c             |  3 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 90 ++++++++++++++++++++++++++++++---------
 xen/arch/x86/hvm/vpmu.c           | 16 +++++++
 xen/arch/x86/traps.c              | 39 ++++++++++++++++-
 xen/include/public/xenpmu.h       |  1 +
 5 files changed, 125 insertions(+), 24 deletions(-)
xen/arch/x86/domain.c +1 -2
index e119d7b..36f4192 100644
@@ -1940,8 +1940,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 +70 -20

177 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +16 -0
index 69aaa7b..4638193 100644
@@ -70,6 +70,14 @@ static void __init parse_vpmu_param(char *s)
     }
 }
 
+static void vpmu_lvtpc_update(uint32_t val)
+{
+     struct vpmu_struct *vpmu = vcpu_vpmu(current);
+
+     vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
+     apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+}
+
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
@@ -428,6 +436,14 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg)
             return -EFAULT;
         pvpmu_finish(current->domain, &pmu_params);
         break;
+
+    case XENPMU_lvtpc_set:
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        vpmu_lvtpc_update((uint32_t)pmu_params.val);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/arch/x86/traps.c +37 -2
index 57dbd0c..f378a24 100644
@@ -71,6 +71,7 @@
 #include <asm/apic.h>
 #include <asm/mc146818rtc.h>
 #include <asm/hpet.h>
+#include <asm/hvm/vpmu.h>
 #include <public/arch-x86/cpuid.h>
 #include <xsm/xsm.h>
 
@@ -871,7 +872,6 @@ static void pv_cpuid(struct cpu_user_regs *regs)
         break;
 
     case 0x00000005: /* MONITOR/MWAIT */
-    case 0x0000000a: /* Architectural Performance Monitor Features */
     case 0x0000000b: /* Extended Topology Enumeration */
     case 0x8000000a: /* SVM revision and features */
     case 0x8000001b: /* Instruction Based Sampling */
@@ -880,7 +880,9 @@ static void pv_cpuid(struct cpu_user_regs *regs)
     unsupported:
         a = b = c = d = 0;
         break;
-
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        vpmu_do_cpuid(0xa, &a, &b, &c, &d);
+        break;
     default:
         (void)cpuid_hypervisor_leaves(regs->eax, 0, &a, &b, &c, &d);
         break;
@@ -2486,6 +2488,17 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             if ( wrmsr_safe(regs->ecx, msr_content) != 0 )
                 goto fail;
             break;
+        case MSR_P6_PERFCTR0...MSR_P6_PERFCTR1:
+        case MSR_P6_EVNTSEL0...MSR_P6_EVNTSEL1:
+        case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+        case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( !vpmu_do_wrmsr(regs->ecx, msr_content) )
+            {
+                if ( (vpmu_mode & XENPMU_MODE_PRIV) && (v->domain == dom0) )
+                    goto invalid;
+            }
+            break;
         default:
             if ( wrmsr_hypervisor_regs(regs->ecx, msr_content) == 1 )
                 break;
@@ -2574,6 +2587,24 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             regs->eax = (uint32_t)msr_content;
             regs->edx = (uint32_t)(msr_content >> 32);
             break;
+        case MSR_IA32_PERF_CAPABILITIES:
+            if ( rdmsr_safe(regs->ecx, msr_content) )
+                goto fail;
+            /* Full-Width Writes not supported */
+            regs->eax = (uint32_t)msr_content & ~(1 << 13);
+            regs->edx = (uint32_t)(msr_content >> 32);
+            break;
+        case MSR_P6_PERFCTR0...MSR_P6_PERFCTR1:
+        case MSR_P6_EVNTSEL0...MSR_P6_EVNTSEL1:
+        case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+        case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( vpmu_do_rdmsr(regs->ecx, &msr_content) ) {
+                regs->eax = (uint32_t)msr_content;
+                regs->edx = (uint32_t)(msr_content >> 32);
+                break;
+            }
+            goto rdmsr_normal;
         default:
             if ( rdmsr_hypervisor_regs(regs->ecx, &val) )
             {
@@ -2606,6 +2637,10 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         pv_cpuid(regs);
         break;
 
+    case 0x33: /* RDPMC */
+        rdpmc(regs->ecx, regs->eax, regs->edx);
+        break;
+
     default:
         goto fail;
     }
xen/include/public/xenpmu.h +1 -0
index ec49097..0060670 100644
@@ -27,6 +27,7 @@
 #define XENPMU_flags_set       3
 #define XENPMU_init            4
 #define XENPMU_finish          5
+#define XENPMU_lvtpc_set       6
 /* ` } */
 
 /* Parameters structure for HYPERVISOR_xenpmu_op call */
Signature · 1 line
1.8.1.4
#40 lore raw

Re: [PATCH v2 10/13] x86/PMU: Add support for PMU registes handling on PV guests

Am Freitag 20 September 2013, 05:42:09 schrieb Boris Ostrovsky:

Quoted 81 lines

Intercept accesses to PMU MSRs and LVTPC APIC vector (only APIC_LVT_MASKED bit is processed) and process them in VPMU module.

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

---
 xen/arch/x86/domain.c             |  3 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c | 90 ++++++++++++++++++++++++++++++---------
 xen/arch/x86/hvm/vpmu.c           | 16 +++++++
 xen/arch/x86/traps.c              | 39 ++++++++++++++++-
 xen/include/public/xenpmu.h       |  1 +
 5 files changed, 125 insertions(+), 24 deletions(-)
xen/arch/x86/domain.c +1 -2
index e119d7b..36f4192 100644
@@ -1940,8 +1940,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 +15 -1
index 5726610..ebbb516 100644
@@ -27,6 +27,7 @@
 #include <asm/regs.h>
 #include <asm/types.h>
 #include <asm/apic.h>
+#include <asm/traps.h>
 #include <asm/msr.h>
 #include <asm/msr-index.h>
 #include <asm/hvm/support.h>
@@ -281,6 +282,9 @@ static inline void __core2_vpmu_save(struct vcpu *v)
         rdmsrl(MSR_CORE_PERF_FIXED_CTR0 + i, core2_vpmu_cxt->fix_counters[i]);
     for ( i = 0; i < arch_pmc_cnt; i++ )
         rdmsrl(MSR_IA32_PERFCTR0 + i, core2_vpmu_cxt->arch_msr_pair[i].counter);
+
+    if ( !is_hvm_domain(v->domain) )
+        rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, core2_vpmu_cxt->global_status);
 }
 
 static int core2_vpmu_save(struct vcpu *v)
@@ -290,10 +294,14 @@ static int core2_vpmu_save(struct vcpu *v)
     if ( !vpmu_is_set_all(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED) )
         return 0;
 
+    if ( !is_hvm_domain(v->domain) )
+        wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+
     __core2_vpmu_save(v);
 
     /* Unset PMU MSR bitmap to trap lazy load. */
-    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) && cpu_has_vmx_msr_bitmap )
+    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) && cpu_has_vmx_msr_bitmap 
+        && is_hvm_domain(v->domain) )
         core2_vpmu_unset_msr_bitmap(v->arch.hvm_vmx.msr_bitmap);
 
     return 1;
@@ -315,6 +323,12 @@ static inline void __core2_vpmu_load(struct vcpu *v)
 
     for ( i = 0; i < arch_pmc_cnt; i++ )
         wrmsrl(MSR_P6_EVNTSEL0+i, core2_vpmu_cxt->arch_msr_pair[i].control);
+
+    if ( !is_hvm_domain(v->domain) )
+    {
+        wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, core2_vpmu_cxt->global_ovf_ctrl);
+        wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, core2_vpmu_cxt->global_ctrl);
+    }
 }
 
 static void core2_vpmu_load(struct vcpu *v)

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

Quoted 9 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -1
@@ -421,7 +435,12 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
                 if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_BTS) )
                     return 1;
                 gdprintk(XENLOG_WARNING, "Debug Store is not supported on this cpu\n");
-                hvm_inject_hw_exception(TRAP_gp_fault, 0);
+
+                if ( is_hvm_domain(v->domain) )
+                    hvm_inject_hw_exception(TRAP_gp_fault, 0);
+                else
+                    send_guest_trap(v->domain, v->vcpu_id, TRAP_gp_fault);

Maybe use a macro or function for these 4 lines?

Quoted 8 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -0
+
                 return 0;
             }
         }
@@ -433,11 +452,15 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     {
     case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
         core2_vpmu_cxt->global_ovf_status &= ~msr_content;

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

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +5 -1
+        core2_vpmu_cxt->global_ovf_ctrl = msr_content;
         return 1;
     case MSR_CORE_PERF_GLOBAL_STATUS:
         gdprintk(XENLOG_INFO, "Can not write readonly MSR: "
                  "MSR_PERF_GLOBAL_STATUS(0x38E)!\n");
-        hvm_inject_hw_exception(TRAP_gp_fault, 0);
+        if ( is_hvm_domain(v->domain) )
+            hvm_inject_hw_exception(TRAP_gp_fault, 0);
+        else
+            send_guest_trap(v->domain, v->vcpu_id, TRAP_gp_fault);

Macro/function?

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

Quoted 12 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +4 -1
         return 1;
     case MSR_IA32_PEBS_ENABLE:
         if ( msr_content & 1 )
@@ -453,7 +476,10 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
                 gdprintk(XENLOG_WARNING,
                          "Illegal address for IA32_DS_AREA: %#" PRIx64 "x\n",
                          msr_content);
-                hvm_inject_hw_exception(TRAP_gp_fault, 0);
+                if ( is_hvm_domain(v->domain) )
+                    hvm_inject_hw_exception(TRAP_gp_fault, 0);
+                else
+                    send_guest_trap(v->domain, v->vcpu_id, TRAP_gp_fault);       

Macro/function and trailing spaces?

Quoted 65 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +22 -12
                 return 1;
             }
             core2_vpmu_cxt->ds_area = msr_content;
@@ -478,10 +504,14 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
             non_global_ctrl >>= FIXED_CTR_CTRL_BITS;
             global_ctrl >>= 1;
         }
+        core2_vpmu_cxt->global_ctrl = msr_content;
         break;
     case MSR_CORE_PERF_FIXED_CTR_CTRL:
         non_global_ctrl = msr_content;
-        vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
+        if ( is_hvm_domain(v->domain) )
+            vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
+        else
+            rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl);
         global_ctrl >>= 32;
         for ( i = 0; i < fixed_pmc_cnt; i++ )
         {
@@ -495,7 +525,10 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         tmp = msr - MSR_P6_EVNTSEL0;
         if ( tmp >= 0 && tmp < arch_pmc_cnt )
         {
-            vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
+            if ( is_hvm_domain(v->domain) )
+                vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &global_ctrl);
+            else
+                rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl);
             core2_vpmu_cxt->arch_msr_pair[tmp].control = msr_content;
             for ( i = 0; i < arch_pmc_cnt && !pmu_enable; i++ )
                 pmu_enable += (global_ctrl >> i) &
@@ -509,17 +542,20 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     else
         vpmu_reset(vpmu, VPMU_RUNNING);
 
-    /* Setup LVTPC in local apic */
-    if ( vpmu_is_set(vpmu, VPMU_RUNNING) &&
-         is_vlapic_lvtpc_enabled(vcpu_vlapic(v)) )
-    {
-        apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
-    }
-    else
+    if ( is_hvm_domain(v->domain) )
     {
-        apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+        /* Setup LVTPC in local apic */
+        if ( vpmu_is_set(vpmu, VPMU_RUNNING) &&
+             is_vlapic_lvtpc_enabled(vcpu_vlapic(v)) )
+        {
+            apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR);
+            vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
+        }
+        else
+        {
+            apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
+            vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+        }
     }
 
     if ( type != MSR_TYPE_GLOBAL )
@@ -547,13 +583,24 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
                 inject_gp = 1;
             break;

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

Quoted 10 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +7 -2
         }
-        if (inject_gp)
-            hvm_inject_hw_exception(TRAP_gp_fault, 0);
+
+        if (inject_gp) 
+        {
+           if ( is_hvm_domain(v->domain) )
+               hvm_inject_hw_exception(TRAP_gp_fault, 0);
+           else
+               send_guest_trap(v->domain, v->vcpu_id, TRAP_gp_fault);

Macro/function?

Dietmar.

Quoted 159 lines
xen/arch/x86/hvm/vmx/vpmu_core2.c +11 -2
+        }
         else
             wrmsrl(msr, msr_content);
     }
     else
-        vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+    {
+       if ( is_hvm_domain(v->domain) )
+           vmx_write_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+       else
+           wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+    }
 
     return 1;
 }
@@ -577,7 +624,10 @@ static int core2_vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
             *msr_content = core2_vpmu_cxt->global_ovf_status;
             break;
         case MSR_CORE_PERF_GLOBAL_CTRL:
-            vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+            if ( is_hvm_domain(v->domain) )
+                vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
+            else
+                rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, *msr_content);
             break;
         default:
             rdmsrl(msr, *msr_content);
xen/arch/x86/hvm/vpmu.c +16 -0
index 69aaa7b..4638193 100644
@@ -70,6 +70,14 @@ static void __init parse_vpmu_param(char *s)
     }
 }
 
+static void vpmu_lvtpc_update(uint32_t val)
+{
+     struct vpmu_struct *vpmu = vcpu_vpmu(current);
+
+     vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
+     apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+}
+
 int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(current);
@@ -428,6 +436,14 @@ long do_xenpmu_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg)
             return -EFAULT;
         pvpmu_finish(current->domain, &pmu_params);
         break;
+
+    case XENPMU_lvtpc_set:
+        if ( copy_from_guest(&pmu_params, arg, 1) )
+            return -EFAULT;
+
+        vpmu_lvtpc_update((uint32_t)pmu_params.val);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/arch/x86/traps.c +37 -2
index 57dbd0c..f378a24 100644
@@ -71,6 +71,7 @@
 #include <asm/apic.h>
 #include <asm/mc146818rtc.h>
 #include <asm/hpet.h>
+#include <asm/hvm/vpmu.h>
 #include <public/arch-x86/cpuid.h>
 #include <xsm/xsm.h>
 
@@ -871,7 +872,6 @@ static void pv_cpuid(struct cpu_user_regs *regs)
         break;
 
     case 0x00000005: /* MONITOR/MWAIT */
-    case 0x0000000a: /* Architectural Performance Monitor Features */
     case 0x0000000b: /* Extended Topology Enumeration */
     case 0x8000000a: /* SVM revision and features */
     case 0x8000001b: /* Instruction Based Sampling */
@@ -880,7 +880,9 @@ static void pv_cpuid(struct cpu_user_regs *regs)
     unsupported:
         a = b = c = d = 0;
         break;
-
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        vpmu_do_cpuid(0xa, &a, &b, &c, &d);
+        break;
     default:
         (void)cpuid_hypervisor_leaves(regs->eax, 0, &a, &b, &c, &d);
         break;
@@ -2486,6 +2488,17 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             if ( wrmsr_safe(regs->ecx, msr_content) != 0 )
                 goto fail;
             break;
+        case MSR_P6_PERFCTR0...MSR_P6_PERFCTR1:
+        case MSR_P6_EVNTSEL0...MSR_P6_EVNTSEL1:
+        case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+        case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( !vpmu_do_wrmsr(regs->ecx, msr_content) )
+            {
+                if ( (vpmu_mode & XENPMU_MODE_PRIV) && (v->domain == dom0) )
+                    goto invalid;
+            }
+            break;
         default:
             if ( wrmsr_hypervisor_regs(regs->ecx, msr_content) == 1 )
                 break;
@@ -2574,6 +2587,24 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             regs->eax = (uint32_t)msr_content;
             regs->edx = (uint32_t)(msr_content >> 32);
             break;
+        case MSR_IA32_PERF_CAPABILITIES:
+            if ( rdmsr_safe(regs->ecx, msr_content) )
+                goto fail;
+            /* Full-Width Writes not supported */
+            regs->eax = (uint32_t)msr_content & ~(1 << 13);
+            regs->edx = (uint32_t)(msr_content >> 32);
+            break;
+        case MSR_P6_PERFCTR0...MSR_P6_PERFCTR1:
+        case MSR_P6_EVNTSEL0...MSR_P6_EVNTSEL1:
+        case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+        case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( vpmu_do_rdmsr(regs->ecx, &msr_content) ) {
+                regs->eax = (uint32_t)msr_content;
+                regs->edx = (uint32_t)(msr_content >> 32);
+                break;
+            }
+            goto rdmsr_normal;
         default:
             if ( rdmsr_hypervisor_regs(regs->ecx, &val) )
             {
@@ -2606,6 +2637,10 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         pv_cpuid(regs);
         break;
 
+    case 0x33: /* RDPMC */
+        rdpmc(regs->ecx, regs->eax, regs->edx);
+        break;
+
     default:
         goto fail;
     }
xen/include/public/xenpmu.h +1 -0
index ec49097..0060670 100644
@@ -27,6 +27,7 @@
 #define XENPMU_flags_set       3
 #define XENPMU_init            4
 #define XENPMU_finish          5
+#define XENPMU_lvtpc_set       6
 /* ` } */
 
 /* Parameters structure for HYPERVISOR_xenpmu_op call */
Signature · 1 line
Company details: http://ts.fujitsu.com/imprint.html
#41 lore raw

Re: [PATCH v2 10/13] x86/PMU: Add support for PMU registes handling on PV guests

In reply to Boris Ostrovsky
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Intercept accesses to PMU MSRs and LVTPC APIC vector (only APIC_LVT_MASKED bit is processed) and process them in VPMU module.

Having scrolled through this more than once, I still can't see where any APIC interception is happening here.

xen/arch/x86/traps.c static int emulate_privileged_op(struct cpu_user_regs *regs) · line 2498

Quoted 12 lines
xen/arch/x86/traps.c +8 -0
@@ -2486,6 +2488,17 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             if ( wrmsr_safe(regs->ecx, msr_content) != 0 )
                 goto fail;
             break;
+        case MSR_P6_PERFCTR0...MSR_P6_PERFCTR1:
+        case MSR_P6_EVNTSEL0...MSR_P6_EVNTSEL1:
+        case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+        case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( !vpmu_do_wrmsr(regs->ecx, msr_content) )
+            {
+                if ( (vpmu_mode & XENPMU_MODE_PRIV) && (v->domain == dom0) )

This is identical to checking ->dom_id against zero, yet we started moving away from that model.

xen/arch/x86/traps.c static int emulate_privileged_op(struct cpu_user_regs *regs) · line 2595

Quoted 10 lines
xen/arch/x86/traps.c +6 -0
@@ -2574,6 +2587,24 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
             regs->eax = (uint32_t)msr_content;
             regs->edx = (uint32_t)(msr_content >> 32);
             break;
+        case MSR_IA32_PERF_CAPABILITIES:
+            if ( rdmsr_safe(regs->ecx, msr_content) )
+                goto fail;
+            /* Full-Width Writes not supported */
+            regs->eax = (uint32_t)msr_content & ~(1 << 13);
+            regs->edx = (uint32_t)(msr_content >> 32);

Rather than black listing, please white list know good features here.

xen/arch/x86/traps.c static int emulate_privileged_op(struct cpu_user_regs *regs) · line 2602

Quoted 7 lines
xen/arch/x86/traps.c +7 -0
+            break;
+        case MSR_P6_PERFCTR0...MSR_P6_PERFCTR1:
+        case MSR_P6_EVNTSEL0...MSR_P6_EVNTSEL1:
+        case MSR_CORE_PERF_FIXED_CTR0...MSR_CORE_PERF_FIXED_CTR2:
+        case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+        case MSR_AMD_FAM15H_EVNTSEL0...MSR_AMD_FAM15H_PERFCTR5:
+            if ( vpmu_do_rdmsr(regs->ecx, &msr_content) ) {

Coding style.

xen/arch/x86/traps.c static int emulate_privileged_op(struct cpu_user_regs *regs) · line 2642

Quoted 3 lines
xen/arch/x86/traps.c +3 -0
+    case 0x33: /* RDPMC */
+        rdpmc(regs->ecx, regs->eax, regs->edx);
+        break;

This will #GP on invalid counter index, i.e. you're creating a DoS here.

Jan

#42 lore raw

Re: [PATCH v2 10/13] x86/PMU: Add support for PMU registes handling on PV guests

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

Intercept accesses to PMU MSRs and LVTPC APIC vector (only APIC_LVT_MASKED bit is processed) and process them in VPMU module.

Having scrolled through this more than once, I still can't see where any APIC interception is happening here.

It's not. This commit message is a a leftover from pre-v1 implementation. LVTPC is now updated via an explicit hypercall.

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/traps.c +0 -0
@@ -2574,6 +2587,24 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
              regs->eax = (uint32_t)msr_content;
              regs->edx = (uint32_t)(msr_content >> 32);
              break;
Quoted 8 lines

xen/arch/x86/traps.c static int emulate_privileged_op(struct cpu_user_regs *regs) · line 2595

Quoted 6 lines
xen/arch/x86/traps.c +6 -0
+        case MSR_IA32_PERF_CAPABILITIES:
+            if ( rdmsr_safe(regs->ecx, msr_content) )
+                goto fail;
+            /* Full-Width Writes not supported */
+            regs->eax = (uint32_t)msr_content & ~(1 << 13);
+            regs->edx = (uint32_t)(msr_content >> 32);

Rather than black listing, please white list know good features here.

This 'case' is gone after I rebased to latest sources (we implement full-width writes now).

-boris

#43 lore raw

PATCH v2 11/13 x86/PMU: Handle PMU interrupts for PV guests

In reply to Boris Ostrovsky

Add support for handling PMU interrupts for PV guests, make these interrupts NMI instead of PMU_APIC_VECTOR vector. Depending on vpmu_mode forward the interrupts to appropriate guest (mode is VPMU_ON) or to dom0 (VPMU_DOM0).

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

---
 xen/arch/x86/apic.c                            |  13 ---
 xen/arch/x86/hvm/svm/vpmu.c                    |   8 +-
 xen/arch/x86/hvm/vmx/vpmu_core2.c              |   8 +-
 xen/arch/x86/hvm/vpmu.c                        | 111 +++++++++++++++++++++++--
 xen/include/asm-x86/hvm/vpmu.h                 |   1 +
 xen/include/asm-x86/irq.h                      |   1 -
 xen/include/asm-x86/mach-default/irq_vectors.h |   1 -
 xen/include/public/xenpmu.h                    |   1 +
 8 files changed, 115 insertions(+), 29 deletions(-)
xen/arch/x86/apic.c +0 -13
index a52a0e8..9675e76 100644
@@ -125,9 +125,6 @@ void __init apic_intr_init(void)
     /* IPI vectors for APIC spurious and error interrupts */
     set_direct_apic_vector(SPURIOUS_APIC_VECTOR, spurious_interrupt);
     set_direct_apic_vector(ERROR_APIC_VECTOR, error_interrupt);
-
-    /* Performance Counters Interrupt */
-    set_direct_apic_vector(PMU_APIC_VECTOR, pmu_apic_interrupt);
 }
 
 /* Using APIC to generate smp_local_timer_interrupt? */
@@ -1368,16 +1365,6 @@ void error_interrupt(struct cpu_user_regs *regs)
 }
 
 /*
- * This interrupt handles performance counters interrupt
- */
-
-void pmu_apic_interrupt(struct cpu_user_regs *regs)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt(regs);
-}
-
-/*
  * This initializes the IO-APIC and APIC hardware if this is
  * a UP kernel.
  */
xen/arch/x86/hvm/svm/vpmu.c +4 -4
index 527a1de..3993a95 100644
@@ -283,8 +283,8 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
             return 1;
         vpmu_set(vpmu, VPMU_RUNNING);
-        apic_write(APIC_LVTPC, PMU_APIC_VECTOR);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
+        apic_write(APIC_LVTPC, APIC_DM_NMI);
+        vpmu->hw_lapic_lvtpc = APIC_DM_NMI;
 
         if ( is_hvm_domain(v->domain) &&
              !((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
@@ -295,8 +295,8 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
     if ( (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
         (is_pmu_enabled(msr_content) == 0) && vpmu_is_set(vpmu, VPMU_RUNNING) )
     {
-        apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
-        vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+        apic_write(APIC_LVTPC, APIC_DM_NMI | APIC_LVT_MASKED);
+        vpmu->hw_lapic_lvtpc = APIC_DM_NMI | APIC_LVT_MASKED;
         vpmu_reset(vpmu, VPMU_RUNNING);
         if ( is_hvm_domain(v->domain) &&
              ((struct amd_vpmu_context *)vpmu->context)->msr_bitmap_set )
xen/arch/x86/hvm/vmx/vpmu_core2.c +4 -4
index ebbb516..27f0807 100644
@@ -548,13 +548,13 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         if ( vpmu_is_set(vpmu, VPMU_RUNNING) &&
              is_vlapic_lvtpc_enabled(vcpu_vlapic(v)) )
         {
-            apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR);
-            vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR;
+            apic_write_around(APIC_LVTPC, APIC_DM_NMI);
+            vpmu->hw_lapic_lvtpc = APIC_DM_NMI;
         }
         else
         {
-            apic_write_around(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
-            vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+            apic_write_around(APIC_LVTPC, APIC_DM_NMI | APIC_LVT_MASKED);
+            vpmu->hw_lapic_lvtpc = APIC_DM_NMI | APIC_LVT_MASKED;
         }
     }
 
xen/arch/x86/hvm/vpmu.c +105 -6

167 lines not on this page · raw message

xen/include/asm-x86/hvm/vpmu.h +1 -0
index e046afd..348fc9a 100644
@@ -68,6 +68,7 @@ struct vpmu_struct {
 #define VPMU_CONTEXT_SAVE                   0x8   /* Force context save */
 #define VPMU_FROZEN                         0x10  /* Stop counters while VCPU is not running */
 #define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x20
+#define VPMU_WAIT_FOR_FLUSH                 0x40  /* PV guest waits for XENPMU_flush */
 
 #define vpmu_set(_vpmu, _x)         ((_vpmu)->flags |= (_x))
 #define vpmu_reset(_vpmu, _x)       ((_vpmu)->flags &= ~(_x))
xen/include/asm-x86/irq.h +0 -1
index 7f5da06..e582a72 100644
@@ -88,7 +88,6 @@ void invalidate_interrupt(struct cpu_user_regs *regs);
 void call_function_interrupt(struct cpu_user_regs *regs);
 void apic_timer_interrupt(struct cpu_user_regs *regs);
 void error_interrupt(struct cpu_user_regs *regs);
-void pmu_apic_interrupt(struct cpu_user_regs *regs);
 void spurious_interrupt(struct cpu_user_regs *regs);
 void irq_move_cleanup_interrupt(struct cpu_user_regs *regs);
 
xen/include/asm-x86/mach-default/irq_vectors.h +0 -1
index 992e00c..46dcfaf 100644
@@ -8,7 +8,6 @@
 #define EVENT_CHECK_VECTOR	0xfc
 #define CALL_FUNCTION_VECTOR	0xfb
 #define LOCAL_TIMER_VECTOR	0xfa
-#define PMU_APIC_VECTOR 	0xf9
 /*
  * High-priority dynamically-allocated vectors. For interrupts that
  * must be higher priority than any guest-bound interrupt.
xen/include/public/xenpmu.h +1 -0
index 0060670..f05fdfa 100644
@@ -28,6 +28,7 @@
 #define XENPMU_init            4
 #define XENPMU_finish          5
 #define XENPMU_lvtpc_set       6
+#define XENPMU_flush           7 /* Write cached MSR values to HW     */
 /* ` } */
 
 /* Parameters structure for HYPERVISOR_xenpmu_op call */
Signature · 1 line
1.8.1.4
#44 lore raw

Re: [PATCH v2 11/13] x86/PMU: Handle PMU interrupts for PV guests

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

Add support for handling PMU interrupts for PV guests, make these interrupts NMI instead of PMU_APIC_VECTOR vector. Depending on vpmu_mode forward the interrupts to appropriate guest (mode is VPMU_ON) or to dom0 (VPMU_DOM0).

Is using NMIs here a necessity? I guess not, in which case I'd really like this to be a (perhaps even non-default) option controllable via command line option.

xen/arch/x86/apic.c void error_interrupt(struct cpu_user_regs *regs) · line 1378

Quoted 8 lines

- * This interrupt handles performance counters interrupt
- */

xen/arch/x86/apic.c +0 -6
-
-void pmu_apic_interrupt(struct cpu_user_regs *regs)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt(regs);
-}

So this was the only caller of vpmu_do_interrupt(); no new one gets added in this patch afaics, and I don't recall having seen addition of another caller in earlier patches. What's the deal?

Quoted 91 lines
xen/arch/x86/hvm/vpmu.c +82 -3
@@ -99,17 +106,97 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 int vpmu_do_interrupt(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vpmu_struct *vpmu;
 
-    if ( vpmu->arch_vpmu_ops )
+
+    /* dom0 will handle this interrupt */
+    if ( (vpmu_mode & XENPMU_MODE_PRIV) ||
+        (v->domain->domain_id >= DOMID_FIRST_RESERVED) )
+    {
+            if ( smp_processor_id() >= dom0->max_vcpus )
+                return 0;
+            v = dom0->vcpu[smp_processor_id()];
+    }
+
+    vpmu = vcpu_vpmu(v);
+    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+        return 0;
+
+    if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
+    {
+        /* PV guest or dom0 is doing system profiling */
+        void *p;
+        struct cpu_user_regs *gregs;
+
+        p = &v->arch.vpmu.xenpmu_data->pmu.regs;
+
+        /* PV guest will be reading PMU MSRs from xenpmu_data */
+        vpmu_save_force(v);
+
+        /* Store appropriate registers in xenpmu_data
+         *
+         * Note: '!current->is_running' is possible when 'set_current(next)'
+         * for the (HVM) guest has been called but 'reset_stack_and_jump()'
+         * has not (i.e. the guest is not actually running yet).
+         */
+        if ( !is_hvm_domain(current->domain) ||
+             ((vpmu_mode & XENPMU_MODE_PRIV) && !current->is_running) )
+        {
+            /*
+             * 32-bit dom0 cannot process Xen's addresses (which are 64 bit)
+             * and therefore we treat it the same way as a non-priviledged
+             * PV 32-bit domain.
+             */
+            if ( is_pv_32bit_domain(current->domain) )
+            {
+                struct compat_cpu_user_regs cmp;
+
+                gregs = guest_cpu_user_regs();
+                XLAT_cpu_user_regs(&cmp, gregs);
+                memcpy(p, &cmp, sizeof(struct compat_cpu_user_regs));
+            }
+            else if ( (current->domain != dom0) && !is_idle_vcpu(current) &&
+                !(vpmu_mode & XENPMU_MODE_PRIV) )
+            {
+                /* PV guest */
+                gregs = guest_cpu_user_regs();
+                memcpy(p, gregs, sizeof(struct cpu_user_regs));
+            }
+            else
+                memcpy(p, regs, sizeof(struct cpu_user_regs));
+        }
+        else
+        {
+            /* HVM guest */
+            struct segment_register cs;
+
+            gregs = guest_cpu_user_regs();
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs->cs = cs.attr.fields.dpl;
+
+            memcpy(p, gregs, sizeof(struct cpu_user_regs));
+        }
+
+        v->arch.vpmu.xenpmu_data->domain_id = current->domain->domain_id;
+        v->arch.vpmu.xenpmu_data->vcpu_id = current->vcpu_id;
+        v->arch.vpmu.xenpmu_data->pcpu_id = smp_processor_id();
+
+        raise_softirq(PMU_SOFTIRQ);
+        vpmu_set(vpmu, VPMU_WAIT_FOR_FLUSH);
+
+        return 1;
+    }
+    else  if ( vpmu->arch_vpmu_ops )
     {
-        struct vlapic *vlapic = vcpu_vlapic(v);
+        /* HVM guest */
+        struct vlapic *vlapic;

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +1 -0
         u32 vlapic_lvtpc;
         unsigned char int_vec;
 
         if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) )
             return 0;
 
+        vlapic = vcpu_vlapic(v);
         if ( !is_vlapic_lvtpc_enabled(vlapic) )
             return 1;
 

Assuming the plan is to run this in NMI context - this is _a lot_ of stuff you want to do. Did you carefully audit all paths for being NMI-safe?

Jan

#45 lore raw

Re: [PATCH v2 11/13] x86/PMU: Handle PMU interrupts for PV guests

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

Add support for handling PMU interrupts for PV guests, make these interrupts NMI instead of PMU_APIC_VECTOR vector. Depending on vpmu_mode forward the interrupts to appropriate guest (mode is VPMU_ON) or to dom0 (VPMU_DOM0).

Is using NMIs here a necessity? I guess not, in which case I'd really like this to be a (perhaps even non-default) option controllable via command line option.

xen/arch/x86/apic.c void error_interrupt(struct cpu_user_regs *regs) · line 1378

Quoted 8 lines

- * This interrupt handles performance counters interrupt
- */

xen/arch/x86/apic.c +0 -6
-
-void pmu_apic_interrupt(struct cpu_user_regs *regs)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt(regs);
-}

So this was the only caller of vpmu_do_interrupt(); no new one gets added in this patch afaics, and I don't recall having seen addition of another caller in earlier patches. What's the deal?

Quoted 91 lines
xen/arch/x86/hvm/vpmu.c +82 -3
@@ -99,17 +106,97 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 int vpmu_do_interrupt(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vpmu_struct *vpmu;
 
-    if ( vpmu->arch_vpmu_ops )
+
+    /* dom0 will handle this interrupt */
+    if ( (vpmu_mode & XENPMU_MODE_PRIV) ||
+        (v->domain->domain_id >= DOMID_FIRST_RESERVED) )
+    {
+            if ( smp_processor_id() >= dom0->max_vcpus )
+                return 0;
+            v = dom0->vcpu[smp_processor_id()];
+    }
+
+    vpmu = vcpu_vpmu(v);
+    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+        return 0;
+
+    if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
+    {
+        /* PV guest or dom0 is doing system profiling */
+        void *p;
+        struct cpu_user_regs *gregs;
+
+        p = &v->arch.vpmu.xenpmu_data->pmu.regs;
+
+        /* PV guest will be reading PMU MSRs from xenpmu_data */
+        vpmu_save_force(v);
+
+        /* Store appropriate registers in xenpmu_data
+         *
+         * Note: '!current->is_running' is possible when 'set_current(next)'
+         * for the (HVM) guest has been called but 'reset_stack_and_jump()'
+         * has not (i.e. the guest is not actually running yet).
+         */
+        if ( !is_hvm_domain(current->domain) ||
+             ((vpmu_mode & XENPMU_MODE_PRIV) && !current->is_running) )
+        {
+            /*
+             * 32-bit dom0 cannot process Xen's addresses (which are 64 bit)
+             * and therefore we treat it the same way as a non-priviledged
+             * PV 32-bit domain.
+             */
+            if ( is_pv_32bit_domain(current->domain) )
+            {
+                struct compat_cpu_user_regs cmp;
+
+                gregs = guest_cpu_user_regs();
+                XLAT_cpu_user_regs(&cmp, gregs);
+                memcpy(p, &cmp, sizeof(struct compat_cpu_user_regs));
+            }
+            else if ( (current->domain != dom0) && !is_idle_vcpu(current) &&
+                !(vpmu_mode & XENPMU_MODE_PRIV) )
+            {
+                /* PV guest */
+                gregs = guest_cpu_user_regs();
+                memcpy(p, gregs, sizeof(struct cpu_user_regs));
+            }
+            else
+                memcpy(p, regs, sizeof(struct cpu_user_regs));
+        }
+        else
+        {
+            /* HVM guest */
+            struct segment_register cs;
+
+            gregs = guest_cpu_user_regs();
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs->cs = cs.attr.fields.dpl;
+
+            memcpy(p, gregs, sizeof(struct cpu_user_regs));
+        }
+
+        v->arch.vpmu.xenpmu_data->domain_id = current->domain->domain_id;
+        v->arch.vpmu.xenpmu_data->vcpu_id = current->vcpu_id;
+        v->arch.vpmu.xenpmu_data->pcpu_id = smp_processor_id();
+
+        raise_softirq(PMU_SOFTIRQ);
+        vpmu_set(vpmu, VPMU_WAIT_FOR_FLUSH);
+
+        return 1;
+    }
+    else  if ( vpmu->arch_vpmu_ops )
     {
-        struct vlapic *vlapic = vcpu_vlapic(v);
+        /* HVM guest */
+        struct vlapic *vlapic;

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +1 -0
         u32 vlapic_lvtpc;
         unsigned char int_vec;
 
         if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) )
             return 0;
 
+        vlapic = vcpu_vlapic(v);
         if ( !is_vlapic_lvtpc_enabled(vlapic) )
             return 1;
 

Assuming the plan is to run this in NMI context - this is _a lot_ of stuff you want to do. Did you carefully audit all paths for being NMI-safe?

Quoted 1 line

Jan

vpmu_save() is not safe from an NMI context, as its non-NMI context uses local_irq_disable() to achieve consistency.

~Andrew

#46 lore raw

Re: [PATCH v2 11/13] x86/PMU: Handle PMU interrupts for PV guests

Quoted 126 lines · Andrew Cooper
Quoted 125 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Add support for handling PMU interrupts for PV guests, make these interrupts NMI instead of PMU_APIC_VECTOR vector. Depending on vpmu_mode forward the interrupts to appropriate guest (mode is VPMU_ON) or to dom0 (VPMU_DOM0).

Is using NMIs here a necessity? I guess not, in which case I'd really like this to be a (perhaps even non-default) option controllable via command line option.

xen/arch/x86/apic.c void error_interrupt(struct cpu_user_regs *regs) · line 1378

Quoted 8 lines

- * This interrupt handles performance counters interrupt
- */

xen/arch/x86/apic.c +0 -6
-
-void pmu_apic_interrupt(struct cpu_user_regs *regs)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt(regs);
-}

So this was the only caller of vpmu_do_interrupt(); no new one gets added in this patch afaics, and I don't recall having seen addition of another caller in earlier patches. What's the deal?

Quoted 91 lines
xen/arch/x86/hvm/vpmu.c +82 -3
@@ -99,17 +106,97 @@ int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
  int vpmu_do_interrupt(struct cpu_user_regs *regs)
  {
      struct vcpu *v = current;
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
+    struct vpmu_struct *vpmu;
 
-    if ( vpmu->arch_vpmu_ops )
+
+    /* dom0 will handle this interrupt */
+    if ( (vpmu_mode & XENPMU_MODE_PRIV) ||
+        (v->domain->domain_id >= DOMID_FIRST_RESERVED) )
+    {
+            if ( smp_processor_id() >= dom0->max_vcpus )
+                return 0;
+            v = dom0->vcpu[smp_processor_id()];
+    }
+
+    vpmu = vcpu_vpmu(v);
+    if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+        return 0;
+
+    if ( !is_hvm_domain(v->domain) || (vpmu_mode & XENPMU_MODE_PRIV) )
+    {
+        /* PV guest or dom0 is doing system profiling */
+        void *p;
+        struct cpu_user_regs *gregs;
+
+        p = &v->arch.vpmu.xenpmu_data->pmu.regs;
+
+        /* PV guest will be reading PMU MSRs from xenpmu_data */
+        vpmu_save_force(v);
+
+        /* Store appropriate registers in xenpmu_data
+         *
+         * Note: '!current->is_running' is possible when 'set_current(next)'
+         * for the (HVM) guest has been called but 'reset_stack_and_jump()'
+         * has not (i.e. the guest is not actually running yet).
+         */
+        if ( !is_hvm_domain(current->domain) ||
+             ((vpmu_mode & XENPMU_MODE_PRIV) && !current->is_running) )
+        {
+            /*
+             * 32-bit dom0 cannot process Xen's addresses (which are 64 bit)
+             * and therefore we treat it the same way as a non-priviledged
+             * PV 32-bit domain.
+             */
+            if ( is_pv_32bit_domain(current->domain) )
+            {
+                struct compat_cpu_user_regs cmp;
+
+                gregs = guest_cpu_user_regs();
+                XLAT_cpu_user_regs(&cmp, gregs);
+                memcpy(p, &cmp, sizeof(struct compat_cpu_user_regs));
+            }
+            else if ( (current->domain != dom0) && !is_idle_vcpu(current) &&
+                !(vpmu_mode & XENPMU_MODE_PRIV) )
+            {
+                /* PV guest */
+                gregs = guest_cpu_user_regs();
+                memcpy(p, gregs, sizeof(struct cpu_user_regs));
+            }
+            else
+                memcpy(p, regs, sizeof(struct cpu_user_regs));
+        }
+        else
+        {
+            /* HVM guest */
+            struct segment_register cs;
+
+            gregs = guest_cpu_user_regs();
+            hvm_get_segment_register(current, x86_seg_cs, &cs);
+            gregs->cs = cs.attr.fields.dpl;
+
+            memcpy(p, gregs, sizeof(struct cpu_user_regs));
+        }
+
+        v->arch.vpmu.xenpmu_data->domain_id = current->domain->domain_id;
+        v->arch.vpmu.xenpmu_data->vcpu_id = current->vcpu_id;
+        v->arch.vpmu.xenpmu_data->pcpu_id = smp_processor_id();
+
+        raise_softirq(PMU_SOFTIRQ);
+        vpmu_set(vpmu, VPMU_WAIT_FOR_FLUSH);
+
+        return 1;
+    }
+    else  if ( vpmu->arch_vpmu_ops )
      {
-        struct vlapic *vlapic = vcpu_vlapic(v);
+        /* HVM guest */
+        struct vlapic *vlapic;

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

Quoted 10 lines
xen/arch/x86/hvm/vpmu.c +1 -0
          u32 vlapic_lvtpc;
          unsigned char int_vec;
 
          if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) )
              return 0;
 
+        vlapic = vcpu_vlapic(v);
          if ( !is_vlapic_lvtpc_enabled(vlapic) )
              return 1;
 

Assuming the plan is to run this in NMI context - this is _a lot_ of stuff you want to do. Did you carefully audit all paths for being NMI-safe?

Quoted 3 lines
Quoted 1 line

Jan

vpmu_save() is not safe from an NMI context, as its non-NMI context uses local_irq_disable() to achieve consistency.

Sigh... hvm_get_segment_register() also appears to be unsafe. I'll will need to move it somewhere else.

-boris

#47 lore raw

Re: [PATCH v2 11/13] x86/PMU: Handle PMU interrupts for PV guests

In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

Add support for handling PMU interrupts for PV guests, make these interrupts NMI instead of PMU_APIC_VECTOR vector. Depending on vpmu_mode forward the interrupts to appropriate guest (mode is VPMU_ON) or to dom0 (VPMU_DOM0).

Is using NMIs here a necessity? I guess not, in which case I'd really like this to be a (perhaps even non-default) option controllable via command line option.

It is not a necessity but using NMIs will allow us to profile code that runs with interrupts disabled.

Quoted 11 lines

xen/arch/x86/apic.c void error_interrupt(struct cpu_user_regs *regs) · line 1378

Quoted 8 lines

- * This interrupt handles performance counters interrupt
- */

xen/arch/x86/apic.c +0 -6
-
-void pmu_apic_interrupt(struct cpu_user_regs *regs)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt(regs);
-}

So this was the only caller of vpmu_do_interrupt(); no new one gets added in this patch afaics, and I don't recall having seen addition of another caller in earlier patches. What's the deal?

It's in 09/13:

+int pmu_nmi_interrupt(struct cpu_user_regs *regs, int cpu) +{
+ return vpmu_do_interrupt(regs);
+}
+

-boris

#48 lore raw

Re: [PATCH v2 11/13] x86/PMU: Handle PMU interrupts for PV guests

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

Add support for handling PMU interrupts for PV guests, make these interrupts NMI instead of PMU_APIC_VECTOR vector. Depending on vpmu_mode forward the interrupts to appropriate guest (mode is VPMU_ON) or to dom0 (VPMU_DOM0).

Quoted 6 lines
Quoted 3 lines

Is using NMIs here a necessity? I guess not, in which case I'd really like this to be a (perhaps even non-default) option controllable via command line option.

It is not a necessity but using NMIs will allow us to profile code that runs with interrupts disabled.

So my request stands to make this optional, default off.

Quoted 15 lines
Quoted 11 lines

xen/arch/x86/apic.c void error_interrupt(struct cpu_user_regs *regs) · line 1378

Quoted 8 lines

- * This interrupt handles performance counters interrupt
- */

xen/arch/x86/apic.c +0 -6
-
-void pmu_apic_interrupt(struct cpu_user_regs *regs)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt(regs);
-}

So this was the only caller of vpmu_do_interrupt(); no new one gets added in this patch afaics, and I don't recall having seen addition of another caller in earlier patches. What's the deal?

It's in 09/13:

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

Quoted 5 lines
xen/arch/x86/hvm/vpmu.c +5 -0
+int pmu_nmi_interrupt(struct cpu_user_regs *regs, int cpu)
+{
+    return vpmu_do_interrupt(regs);
+}
+

Ah. Then please try to break up your changes in a way that logically connected things stay together. The change you quote above can't make much sense earlier than in patch 11.

Jan

#49 lore raw

PATCH v2 12/13 x86/PMU: Save VPMU state for PV guests during context switch

In reply to Boris Ostrovsky

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

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

PATCH v2 13/13 x86/PMU: Move vpmu files up from hvm directory

In reply to Boris Ostrovsky

Since VPMU is now used by both HVM and PV we should move it up from HVM subtree:
xen/arch/x86/hvm/vpmu.c => xen/arch/x86/vpmu.c xen/arch/x86/hvm/vmx/vpmu_core2.c => xen/arch/x86/vpmu_intel.c xen/arch/x86/hvm/svm/vpmu.c => xen/arch/x86/vpmu_amd.c xen/include/asm-x86/hvm/vpmu.h => xen/include/asm-x86/vpmu.h

No code changes (except for adjusting Makefiles and paths for #includes).

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

489 lines not on this page · raw message

xen/arch/x86/hvm/vmx/Makefile +0 -1
index 373b3d9..04a29ce 100644
@@ -3,5 +3,4 @@ obj-y += intr.o
 obj-y += realmode.o
 obj-y += vmcs.o
 obj-y += vmx.o
-obj-y += vpmu_core2.o
 obj-y += vvmx.o
xen/arch/x86/hvm/vmx/vpmu_core2.c deleted +0 -936

936 lines not on this page · raw message

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

549 lines not on this page · raw message

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

549 lines not on this page · raw message

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

440 lines not on this page · raw message

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

936 lines not on this page · raw message

xen/include/asm-x86/domain.h +1 -0
index 4f2247e..0b79d39 100644

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

xen/include/asm-x86/hvm/vmx/vmcs.h +0 -1
index 5971613..beb959f 100644

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

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

96 lines not on this page · raw message

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

96 lines not on this page · raw message

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

Signature · 1 line
1.8.1.4

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