Skip to contentSkip to conversation

x86/PMU: Xen PMU PV support

PATCH Superseded Boris Ostrovsky 4 participants 30 messages Sep 10, 2013 – Sep 12, 2013

v1 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

cover x86/PMU: Xen PMU PV support

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

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

A few notes that may help reviewing:

* A shared data structure (xenpmu_data_t) between each PV VPCU and hypervisor CPU is used for passing registers' values as well as PMU state at the time of PMU interrupt.

* PMU interrupts are taken by hypervisor at NMI level for both HVM and PV.

* Guest's interrupt handler does not read/write PMU MSRs directly. Instead, it accesses xenpmu_data_t and flushes it to HW it before returning.

* PMU mode is controlled at runtime via /sys/hypervisor/pmu/pmu/{pmu_mode,pmu_flags} in addition to 'vpmu' boot option (which is preserved for back compatibility). The following modes are provided:
* disable: VPMU is off
* enable: VPMU is on. Guests can profile themselves, dom0 profiles itself and Xen
* priv_enable: dom0 only profiling. dom0 collects samples for everyone. Sampling in guests is suspended.

* /proc/xen/xensyms file exports hypervisor's symbols to dom0 (similar to /proc/kallsyms)

* VPMU infrastructure is now used for both HVM and PV and therefore has been moved up from hvm subtree

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/x86/Makefile                          |   9 +-
 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          |   3 +-
 xen/arch/x86/platform_hypercall.c              |  14 +
 xen/arch/x86/traps.c                           |  38 +-
 xen/arch/x86/vpmu.c                            | 545 ++++++++++++++
 xen/arch/x86/vpmu_amd.c                        | 486 +++++++++++++
 xen/arch/x86/vpmu_intel.c                      | 938 +++++++++++++++++++++++++
 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                          |  10 +-
 xen/common/symbols-dummy.c                     |   1 +
 xen/common/symbols.c                           |  78 +-
 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                     |  97 +++
 xen/include/public/platform.h                  |  21 +
 xen/include/public/xen.h                       |   2 +
 xen/include/public/xenpmu.h                    | 138 ++++
 xen/include/xen/hypercall.h                    |   4 +
 xen/include/xen/softirq.h                      |   1 +
 xen/include/xen/symbols.h                      |   4 +
 xen/tools/symbols.c                            |   4 +
 41 files changed, 2465 insertions(+), 1856 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/xenpmu.h
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/13 Export hypervisor symbols

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

---
 xen/arch/x86/Makefile                    |  8 ++--
 xen/arch/x86/platform_hypercall.c        | 14 ++++++
 xen/arch/x86/x86_64/platform_hypercall.c |  2 +-
 xen/common/symbols-dummy.c               |  1 +
 xen/common/symbols.c                     | 78 ++++++++++++++++++++++++++++++--
 xen/include/public/platform.h            | 21 +++++++++
 xen/include/xen/symbols.h                |  4 ++
 xen/tools/symbols.c                      |  4 ++
 8 files changed, 123 insertions(+), 9 deletions(-)
xen/arch/x86/Makefile +4 -4
index d502bdf..a27ac44 100644
@@ -102,11 +102,11 @@ $(BASEDIR)/common/symbols-dummy.o:
 $(TARGET)-syms: prelink.o xen.lds $(BASEDIR)/common/symbols-dummy.o
 	$(LD) $(LDFLAGS) -T xen.lds -N prelink.o \
 	    $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0
-	$(NM) -n $(@D)/.$(@F).0 | $(BASEDIR)/tools/symbols >$(@D)/.$(@F).0.S
+	$(NM) -n $(@D)/.$(@F).0 | $(BASEDIR)/tools/symbols --all-symbols >$(@D)/.$(@F).0.S
 	$(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).0.o
 	$(LD) $(LDFLAGS) -T xen.lds -N prelink.o \
 	    $(@D)/.$(@F).0.o -o $(@D)/.$(@F).1
-	$(NM) -n $(@D)/.$(@F).1 | $(BASEDIR)/tools/symbols >$(@D)/.$(@F).1.S
+	$(NM) -n $(@D)/.$(@F).1 | $(BASEDIR)/tools/symbols --all-symbols >$(@D)/.$(@F).1.S
 	$(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).1.o
 	$(LD) $(LDFLAGS) -T xen.lds -N prelink.o \
 	    $(@D)/.$(@F).1.o -o $@
@@ -129,13 +129,13 @@ $(TARGET).efi: prelink-efi.o efi.lds efi/relocs-dummy.o $(BASEDIR)/common/symbol
 	          $(guard) $(LD) $(call EFI_LDFLAGS,$(base)) -T efi.lds -N $< efi/relocs-dummy.o \
 	                $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).$(base).0 &&) :
 	$(guard) efi/mkreloc $(foreach base,$(VIRT_BASE) $(ALT_BASE),$(@D)/.$(@F).$(base).0) >$(@D)/.$(@F).0r.S
-	$(guard) $(NM) -n $(@D)/.$(@F).$(VIRT_BASE).0 | $(guard) $(BASEDIR)/tools/symbols >$(@D)/.$(@F).0s.S
+	$(guard) $(NM) -n $(@D)/.$(@F).$(VIRT_BASE).0 | $(guard) $(BASEDIR)/tools/symbols --all-symbols >$(@D)/.$(@F).0s.S
 	$(guard) $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).0r.o $(@D)/.$(@F).0s.o
 	$(foreach base, $(VIRT_BASE) $(ALT_BASE), \
 	          $(guard) $(LD) $(call EFI_LDFLAGS,$(base)) -T efi.lds -N $< \
 	                $(@D)/.$(@F).0r.o $(@D)/.$(@F).0s.o -o $(@D)/.$(@F).$(base).1 &&) :
 	$(guard) efi/mkreloc $(foreach base,$(VIRT_BASE) $(ALT_BASE),$(@D)/.$(@F).$(base).1) >$(@D)/.$(@F).1r.S
-	$(guard) $(NM) -n $(@D)/.$(@F).$(VIRT_BASE).1 | $(guard) $(BASEDIR)/tools/symbols >$(@D)/.$(@F).1s.S
+	$(guard) $(NM) -n $(@D)/.$(@F).$(VIRT_BASE).1 | $(guard) $(BASEDIR)/tools/symbols --all-symbols >$(@D)/.$(@F).1s.S
 	$(guard) $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).1r.o $(@D)/.$(@F).1s.o
 	$(guard) $(LD) $(call EFI_LDFLAGS,$(VIRT_BASE)) -T efi.lds -N $< \
 	                $(@D)/.$(@F).1r.o $(@D)/.$(@F).1s.o -o $@
xen/arch/x86/platform_hypercall.c +14 -0
index 7175a82..492dc98 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,19 @@ ret_t do_platform_op(XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op)
     }
     break;
 
+    case XENPF_get_symbols:
+    {
+        XEN_GUEST_HANDLE(char) gbuf;
+
+        /* Buffer that holds the symbols */
+        guest_from_compat_handle(gbuf, op->u.symdata.buf);
+
+        ret = xensyms_read(guest_handle_to_param(gbuf, char), &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 +74 -4

99 lines not on this page · raw message

xen/include/public/platform.h +21 -0
index 4341f54..f1782be 100644
@@ -527,6 +527,26 @@ 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
+
+#define XENSYMS_SZ         4096
+struct xenpf_symdata {
+    /*
+     * offset into Xen's symbol data and symbol number from
+     * last call. Used only by Xen.
+     */
+    uint64_t xen_offset;
+    uint64_t xen_symnum;
+
+   /*
+    * Symbols data, formatted similar to /proc/kallsyms:
+    *   <address> <type> <name>
+    */
+    XEN_GUEST_HANDLE(char) buf;
+};
+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 +573,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..a0e36d0 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(XEN_GUEST_HANDLE_PARAM(char) gbuf, 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 v1 01/13] Export hypervisor symbols

xen/arch/x86/Makefile $(BASEDIR)/common/symbols-dummy.o: · line 105

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/Makefile +1 -1
@@ -102,11 +102,11 @@ $(BASEDIR)/common/symbols-dummy.o:
 $(TARGET)-syms: prelink.o xen.lds $(BASEDIR)/common/symbols-dummy.o
 	$(LD) $(LDFLAGS) -T xen.lds -N prelink.o \
 	    $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0
-	$(NM) -n $(@D)/.$(@F).0 | $(BASEDIR)/tools/symbols >$(@D)/.$(@F).0.S
+	$(NM) -n $(@D)/.$(@F).0 | $(BASEDIR)/tools/symbols --all-symbols >$(@D)/.$(@F).0.S

For one I can't see what use data symbols have for performance analysis.

And then I'm opposed to growing the symbol table size unconditionally for no good reason.

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

Quoted 9 lines
xen/include/public/platform.h +3 -0
@@ -527,6 +527,26 @@ 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
+
+#define XENSYMS_SZ         4096

This doesn't appear to belong into the public interface.

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

Quoted 7 lines
xen/include/public/platform.h +7 -0
+struct xenpf_symdata {
+    /*
+     * offset into Xen's symbol data and symbol number from
+     * last call. Used only by Xen.
+     */
+    uint64_t xen_offset;
+    uint64_t xen_symnum;

I wonder whether that's really a suitable mechanism.

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

Quoted 6 lines
xen/include/public/platform.h +6 -0
+
+   /*
+    * Symbols data, formatted similar to /proc/kallsyms:
+    *   <address> <type> <name>
+    */
+    XEN_GUEST_HANDLE(char) buf;

This is too simplistic: Please use a proper structure here, to allow switching the internal symbol table representation (which I have on my todo list) without having to mimic old behavior.

Jan

#3 lore raw

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

Quoted 11 lines · Jan Beulich

xen/arch/x86/Makefile $(BASEDIR)/common/symbols-dummy.o: · line 105

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/Makefile +1 -1
@@ -102,11 +102,11 @@ $(BASEDIR)/common/symbols-dummy.o:
  $(TARGET)-syms: prelink.o xen.lds $(BASEDIR)/common/symbols-dummy.o
  	$(LD) $(LDFLAGS) -T xen.lds -N prelink.o \
  	    $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0
-	$(NM) -n $(@D)/.$(@F).0 | $(BASEDIR)/tools/symbols >$(@D)/.$(@F).0.S
+	$(NM) -n $(@D)/.$(@F).0 | $(BASEDIR)/tools/symbols --all-symbols >$(@D)/.$(@F).0.S

For one I can't see what use data symbols have for performance analysis.

They are used by perf, similarly to kallsyms (usually when debug symbols from binary are not available)

Quoted 2 lines

And then I'm opposed to growing the symbol table size unconditionally for no good reason.

I think I can remove --all-symbols, it is not strictly necessary for what I plan now for perf. We may need to add it later, possibly with a config option.

Quoted 10 lines

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

Quoted 9 lines
xen/include/public/platform.h +3 -0
@@ -527,6 +527,26 @@ 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
+
+#define XENSYMS_SZ         4096

This doesn't appear to belong into the public interface.

Linux driver needs to know size of the buffer that is passed from the hypervisir. I suppose I can just use PAGE_SIZE.

Quoted 8 lines

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

Quoted 7 lines
xen/include/public/platform.h +7 -0
+struct xenpf_symdata {
+    /*
+     * offset into Xen's symbol data and symbol number from
+     * last call. Used only by Xen.
+     */
+    uint64_t xen_offset;
+    uint64_t xen_symnum;

I wonder whether that's really a suitable mechanism.

Why do you think this is not suitable?

Linux needs to keep track of position in the symbol table while it is walking over the file, otherwise we will need to keep the state in hypervisor which is much less desirable.

Quoted 10 lines

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

Quoted 7 lines
xen/include/public/platform.h +6 -0
+
+   /*
+    * Symbols data, formatted similar to /proc/kallsyms:
+    *   <address> <type> <name>
+    */
+    XEN_GUEST_HANDLE(char) buf;

This is too simplistic: Please use a proper structure here, to allow switching the internal symbol table representation (which I have on my todo list) without having to mimic old behavior.

I don't think I know what you are referring to here.

-boris

#4 lore raw

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

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

xen/arch/x86/Makefile $(BASEDIR)/common/symbols-dummy.o: · line 105

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/arch/x86/Makefile +1 -1
@@ -102,11 +102,11 @@ $(BASEDIR)/common/symbols-dummy.o:
  $(TARGET)-syms: prelink.o xen.lds $(BASEDIR)/common/symbols-dummy.o
  	$(LD) $(LDFLAGS) -T xen.lds -N prelink.o \
  	    $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0
-	$(NM) -n $(@D)/.$(@F).0 | $(BASEDIR)/tools/symbols >$(@D)/.$(@F).0.S
+	$(NM) -n $(@D)/.$(@F).0 | $(BASEDIR)/tools/symbols --all-symbols 

$(@D)/.$(@F).0.S
For one I can't see what use data symbols have for performance analysis.

Quoted 2 lines

They are used by perf, similarly to kallsyms (usually when debug symbols from binary are not available)

Right, but I specifically said _data_ symbols. I can see what code ones are going to be used for.

Quoted 6 lines
Quoted 2 lines

And then I'm opposed to growing the symbol table size unconditionally for no good reason.

I think I can remove --all-symbols, it is not strictly necessary for what I plan now for
perf. We may need to add it later, possibly with a config option.

Yes, please, as long as they're not really useful.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/include/public/platform.h +0 -0
@@ -527,6 +527,26 @@ struct xenpf_core_parking {
  typedef struct xenpf_core_parking xenpf_core_parking_t;
  DEFINE_XEN_GUEST_HANDLE(xenpf_core_parking_t);
 
Quoted 7 lines
Quoted 4 lines

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

Quoted 3 lines
xen/include/public/platform.h +3 -0
+#define XENPF_get_symbols  61
+
+#define XENSYMS_SZ         4096

This doesn't appear to belong into the public interface.

Linux driver needs to know size of the buffer that is passed from the hypervisir. I suppose I can just use PAGE_SIZE.

Buffer? Passed from the hypervisor?

And no, there's no PAGE_SIZE in the public interface as far as I'm aware.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
xen/include/public/platform.h +7 -0
+struct xenpf_symdata {
+    /*
+     * offset into Xen's symbol data and symbol number from
+     * last call. Used only by Xen.
+     */
+    uint64_t xen_offset;
+    uint64_t xen_symnum;
Quoted 7 lines
Quoted 1 line

I wonder whether that's really a suitable mechanism.

Why do you think this is not suitable?

Linux needs to keep track of position in the symbol table while it is walking over the file, otherwise we will need to keep the state in hypervisor which is much less desirable.

This could be as simple as a "give me the n-th symbol" interface. The handler in the hypervisor could cache the last symbol together with the associated data (with the assumption that there's only ever going to be one iteration in progress), invalidating the cache if the coming in index isn't one greater than the last one processed. All the caching of course is only necessary if otherwise lookup times aren't acceptable.

Quoted 11 lines
Quoted 9 lines

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

Quoted 6 lines
xen/include/public/platform.h +6 -0
+
+   /*
+    * Symbols data, formatted similar to /proc/kallsyms:
+    *   <address> <type> <name>
+    */
+    XEN_GUEST_HANDLE(char) buf;

This is too simplistic: Please use a proper structure here, to allow switching the internal symbol table representation (which I have on my todo list) without having to mimic old behavior.

I don't think I know what you are referring to here.

Rather than having a handle to a simply byte array, you ought to have a handle to a structure containing address, type, and (pointer to/handle of) name.

Jan

#5 lore raw

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

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
xen/include/public/platform.h +0 -0
@@ -527,6 +527,26 @@ struct xenpf_core_parking {
   typedef struct xenpf_core_parking xenpf_core_parking_t;
   DEFINE_XEN_GUEST_HANDLE(xenpf_core_parking_t);
 
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

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

Quoted 3 lines
xen/include/public/platform.h +3 -0
+#define XENPF_get_symbols  61
+
+#define XENSYMS_SZ         4096

This doesn't appear to belong into the public interface.

Linux driver needs to know size of the buffer that is passed from the hypervisir. I suppose I can just use PAGE_SIZE.

Buffer? Passed from the hypervisor?

As it is written now, we pass XENSYMS_SZ worth of (formatted) symbol information to dom0.

Quoted 16 lines

And no, there's no PAGE_SIZE in the public interface as far as I'm aware.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
xen/include/public/platform.h +7 -0
+struct xenpf_symdata {
+    /*
+     * offset into Xen's symbol data and symbol number from
+     * last call. Used only by Xen.
+     */
+    uint64_t xen_offset;
+    uint64_t xen_symnum;
Quoted 6 lines
Quoted 1 line

I wonder whether that's really a suitable mechanism.

Why do you think this is not suitable?

Linux needs to keep track of position in the symbol table while it is walking over the file, otherwise we will need to keep the state in hypervisor which is much less desirable.

Quoted 7 lines

This could be as simple as a "give me the n-th symbol" interface. The handler in the hypervisor could cache the last symbol together with the associated data (with the assumption that there's only ever going to be one iteration in progress), invalidating the cache if the coming in index isn't one greater than the last one processed. All the caching of course is only necessary if otherwise lookup times aren't acceptable.

That would be just having xen_symnum (and caching xen_offset in the hypervisor).

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/include/public/platform.h +6 -0
+
+   /*
+    * Symbols data, formatted similar to /proc/kallsyms:
+    *   <address> <type> <name>
+    */
+    XEN_GUEST_HANDLE(char) buf;
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

This is too simplistic: Please use a proper structure here, to allow switching the internal symbol table representation (which I have on my todo list) without having to mimic old behavior.

I don't think I know what you are referring to here.

Rather than having a handle to a simply byte array, you ought to have a handle to a structure containing address, type, and (pointer to/handle of) name.

Are you suggesting passing symbols one per hypercall? That's over 4000 hypercalls per one file read. How about requesting N next symbols?

-boris

#6 lore raw

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

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

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

Quoted 10 lines
xen/include/public/platform.h +3 -0
@@ -527,6 +527,26 @@ 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
+
+#define XENSYMS_SZ         4096

This doesn't appear to belong into the public interface.

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

Linux driver needs to know size of the buffer that is passed from the hypervisir. I suppose I can just use PAGE_SIZE.

Buffer? Passed from the hypervisor?

As it is written now, we pass XENSYMS_SZ worth of (formatted) symbol information to dom0.

Right, that what I understood, and that's what I want to avoid.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 5 lines
xen/include/public/platform.h +5 -0
+   /*
+    * Symbols data, formatted similar to /proc/kallsyms:
+    *   <address> <type> <name>
+    */
+    XEN_GUEST_HANDLE(char) buf;

This is too simplistic: Please use a proper structure here, to allow switching the internal symbol table representation (which I have on my todo list) without having to mimic old behavior.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

I don't think I know what you are referring to here.

Rather than having a handle to a simply byte array, you ought to have a handle to a structure containing address, type, and (pointer to/handle of) name.

Are you suggesting passing symbols one per hypercall? That's over 4000 hypercalls per one file read. How about requesting N next symbols?

That'd be fine too, but could be almost equally achieved with multi-calls.

Jan

#7 lore raw

PATCH 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/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             | 10 ++++++++--
 5 files changed, 13 insertions(+), 2 deletions(-)

I am not particularly happy about changes to common/schedule.c. I could define an arch-specific macro in an include file but I don't see a good place to do this. Perhaps someone could suggest a better solution.

Or maybe the ifdef is not needed at all (it was added in case something breaks on ARM).

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 +8 -2
index a8398bd..af3edbc 100644
@@ -1219,8 +1219,14 @@ static void schedule(void)
      * switch, else lost_records resume will not work properly.
      */
 
-    ASSERT(!next->is_running);
-    next->is_running = 1;
+#ifdef CONFIG_X86
+    if ( is_idle_vcpu(next) )
+    /* On x86 guests will set is_running right before they start running. */
+#endif
+    {
+        ASSERT(!next->is_running);
+        next->is_running = 1;
+    }
 
     pcpu_schedule_unlock_irq(cpu);
 
Signature · 1 line
1.8.1.4
#8 lore raw

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

Quoted 7 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
xen/common/schedule.c +0 -0
@@ -1219,8 +1219,14 @@ static void schedule(void)
      * switch, else lost_records resume will not work properly.
      */
 

xen/common/schedule.c static void schedule(void) · line 1229

Quoted 10 lines
xen/common/schedule.c +8 -2
-    ASSERT(!next->is_running);
-    next->is_running = 1;
+#ifdef CONFIG_X86
+    if ( is_idle_vcpu(next) )
+    /* On x86 guests will set is_running right before they start running. */
+#endif
+    {
+        ASSERT(!next->is_running);
+        next->is_running = 1;
+    }

I'm not sure the change as a whole is appropriate in the first place (the patch description is all but assuring that this change doesn't have unexpected side effects, namely in the individual schedulers), but this clearly is a no-go: Either do this consistently for ARM too, or drop the idea.

Jan

#9 lore raw

PATCH 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
#10 lore raw

PATCH 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
#11 lore raw

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

In reply to Boris Ostrovsky

Remove struct pmumsr and convert core2_fix_counters and core2_ctrls into arrays of u32 (MSR offsets).

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        | 218 ++++++++++++++++---------------
 xen/include/asm-x86/hvm/vmx/vmcs.h       |   2 +
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  19 ---
 4 files changed, 171 insertions(+), 127 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 +110 -108

424 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
#12 lore raw

PATCH 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              |  12 +---
 xen/arch/x86/hvm/vmx/vpmu_core2.c        |  43 +++----------
 xen/arch/x86/hvm/vpmu.c                  |   5 +-
 xen/arch/x86/oprofile/op_model_ppro.c    |   1 -
 xen/include/asm-x86/hvm/vmx/vpmu_core2.h |  32 ----------
 xen/include/asm-x86/hvm/vpmu.h           |  14 +----
 xen/include/public/xenpmu.h              | 101 +++++++++++++++++++++++++++++++
 7 files changed, 115 insertions(+), 93 deletions(-)
 delete mode 100644 xen/include/asm-x86/hvm/vmx/vpmu_core2.h
 create mode 100644 xen/include/public/xenpmu.h
xen/arch/x86/hvm/svm/vpmu.c +1 -11
index a09930e..9f9c9ea 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
@@ -83,13 +80,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 +9 -34

83 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +3 -2
index d6a9ff6..768f766 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
@@ -51,7 +52,7 @@ static void __init parse_vpmu_param(char *s)
         break;
     default:
         if ( !strcmp(s, "bts") )
-            opt_vpmu_enabled |= VPMU_BOOT_BTS;
+            opt_vpmu_enabled |= VPMU_INTEL_BTS;
         else if ( *s )
         {
             printk("VPMU: unknown flag: %s - vpmu disabled!\n", s);
@@ -59,7 +60,7 @@ static void __init parse_vpmu_param(char *s)
         }
         /* fall through */
     case 1:
-        opt_vpmu_enabled |= VPMU_BOOT_ENABLED;
+        opt_vpmu_enabled |= VPMU_ON;
         break;
     }
 }
xen/arch/x86/oprofile/op_model_ppro.c +0 -1
index 3225937..2939a40 100644
@@ -20,7 +20,6 @@
 #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"
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 +1 -13
index 674cdad..410ad12 100644
@@ -22,19 +22,12 @@
 #ifndef __ASM_X86_HVM_VPMU_H_
 #define __ASM_X86_HVM_VPMU_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. */
+#include <public/xenpmu.h>
 
 
-#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 +69,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/xenpmu.h new file +101 -0

101 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#13 lore raw

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

xen/include/public/xenpmu.h

Quoted 2 lines
xen/include/public/xenpmu.h new file +0 -0

This new file is completely unacceptable as a public header.

xen/include/public/xenpmu.h line 4

Quoted 5 lines
xen/include/public/xenpmu.h new file +4 -0
@@ -0,0 +1,101 @@
+#ifndef __XEN_PUBLIC_XENPMU_H__
+#define __XEN_PUBLIC_XENPMU_H__
+
+#include <asm/msr.h>

This is a no-go.

xen/include/public/xenpmu.h line 12

Quoted 8 lines
xen/include/public/xenpmu.h new file +8 -0
+
+#include "xen.h"
+
+#define XENPMU_VER_MAJ    0
+#define XENPMU_VER_MIN    0
+
+/* VPMU modes */
+#define VPMU_MODE_MASK     0xff

All of these defines would need XEN_ prefixes (and types would similarly need xen_). And there ought to be some association in the comment to the field(s) that these constants would actually go into:

xen/include/public/xenpmu.h line 14

Quoted 4 lines

From a cursory look I can't see this.

xen/include/public/xenpmu.h new file +2 -0
+#define VPMU_OFF           0
+/* guests can profile themselves, (dom0 profiles itself and Xen) */

Comment style.

Quoted 7 lines
xen/include/public/xenpmu.h new file +7 -0
+#define VPMU_ON            (1<<0)
+/*
+ * Only dom0 has access to VPMU and it profiles everyone: itself,
+ * the hypervisor and the guests.
+ */
+#define VPMU_PRIV          (1<<1)
+

xen/include/public/xenpmu.h line 31

Quoted 10 lines
xen/include/public/xenpmu.h new file +10 -0
+/* VPMU flags */
+#define VPMU_FLAGS_MASK    ((uint32_t)(~VPMU_MODE_MASK))
+#define VPMU_INTEL_BTS     (1<<8) /* Ignored on AMD */
+
+
+/* AMD PMU registers and structures */
+#define F10H_NUM_COUNTERS   4
+#define F15H_NUM_COUNTERS   6
+/* To accommodate more counters in the future (e.g. NB counters) */
+#define MAX_NUM_COUNTERS    16

Perhaps better to have the number of counters in the structure?

xen/include/public/xenpmu.h line 40

Quoted 9 lines
xen/include/public/xenpmu.h new file +9 -0
+struct amd_vpmu_context {
+    uint64_t counters[MAX_NUM_COUNTERS];
+    uint64_t ctrls[MAX_NUM_COUNTERS];
+    uint8_t msr_bitmap_set;
+};
+
+
+/* Intel PMU registers and structures */
+static const uint32_t core2_fix_counters_msr[] = {

You're kidding, aren't you?

Jan

#14 lore raw

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

Quoted 3 lines · Jan Beulich

xen/include/public/xenpmu.h

Quoted 2 lines
xen/include/public/xenpmu.h new file +0 -0

This new file is completely unacceptable as a public header.

Is this the same comment as IanC made? Mark up public interfaces for doc generation?

Or something else?

Quoted 44 lines

xen/include/public/xenpmu.h line 4

Quoted 6 lines
xen/include/public/xenpmu.h new file +4 -0
@@ -0,0 +1,101 @@
+#ifndef __XEN_PUBLIC_XENPMU_H__
+#define __XEN_PUBLIC_XENPMU_H__
+
+#include <asm/msr.h>

This is a no-go.

xen/include/public/xenpmu.h line 12

Quoted 8 lines
xen/include/public/xenpmu.h new file +8 -0
+
+#include "xen.h"
+
+#define XENPMU_VER_MAJ    0
+#define XENPMU_VER_MIN    0
+
+/* VPMU modes */
+#define VPMU_MODE_MASK     0xff

All of these defines would need XEN_ prefixes (and types would similarly need xen_). And there ought to be some association in the comment to the field(s) that these constants would actually go into:
From a cursory look I can't see this.

xen/include/public/xenpmu.h line 14

Quoted 2 lines
xen/include/public/xenpmu.h new file +2 -0
+#define VPMU_OFF           0
+/* guests can profile themselves, (dom0 profiles itself and Xen) */

Comment style.

Quoted 7 lines
xen/include/public/xenpmu.h new file +7 -0
+#define VPMU_ON            (1<<0)
+/*
+ * Only dom0 has access to VPMU and it profiles everyone: itself,
+ * the hypervisor and the guests.
+ */
+#define VPMU_PRIV          (1<<1)
+

xen/include/public/xenpmu.h line 31

Quoted 10 lines
xen/include/public/xenpmu.h new file +10 -0
+/* VPMU flags */
+#define VPMU_FLAGS_MASK    ((uint32_t)(~VPMU_MODE_MASK))
+#define VPMU_INTEL_BTS     (1<<8) /* Ignored on AMD */
+
+
+/* AMD PMU registers and structures */
+#define F10H_NUM_COUNTERS   4
+#define F15H_NUM_COUNTERS   6
+/* To accommodate more counters in the future (e.g. NB counters) */
+#define MAX_NUM_COUNTERS    16

Perhaps better to have the number of counters in the structure?

Quoted 10 lines

xen/include/public/xenpmu.h line 40

Quoted 9 lines
xen/include/public/xenpmu.h new file +9 -0
+struct amd_vpmu_context {
+    uint64_t counters[MAX_NUM_COUNTERS];
+    uint64_t ctrls[MAX_NUM_COUNTERS];
+    uint8_t msr_bitmap_set;
+};
+
+
+/* Intel PMU registers and structures */
+static const uint32_t core2_fix_counters_msr[] = {

You're kidding, aren't you?

This was moved from vpmu.h. I will change it to enum (or remove altogether).

-boris

#15 lore raw

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

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

xen/include/public/xenpmu.h

Quoted 2 lines
xen/include/public/xenpmu.h new file +0 -0

This new file is completely unacceptable as a public header.

Is this the same comment as IanC made? Mark up public interfaces for doc generation?

Or something else?

Something else - see the other comments I had given down to about the middle of the file. I stopped there, assuming I provided enough examples of what is bad.

Jan

#16 lore raw

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

In reply to Boris Ostrovsky

xen/include/public/xenpmu.h

Quoted 6 lines · Boris Ostrovsky
xen/include/public/xenpmu.h new file +0 -0
new file mode 100644
index 0000000..420b674

When adding new public interfaces please can we mark them up such that they get properly linked into the generate docs[1]

xen/include/public/event_channel.h is an example of a header which is already marked up and "make -C docs html/hypercall/index.htm" will build you a copy in docs/html.

[1] http://xenbits.xen.org/docs/unstable/hypercall/index.html

#17 lore raw

PATCH 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 410ad12..f5f8c9c 100644
@@ -25,9 +25,9 @@
 #include <public/xenpmu.h>
 
 
-#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
#18 lore raw

PATCH 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  |  2 +-
 xen/arch/x86/hvm/vpmu.c            | 77 ++++++++++++++++++++++++++++++++++----
 xen/arch/x86/x86_64/compat/entry.S |  4 ++
 xen/arch/x86/x86_64/entry.S        |  4 ++
 xen/include/asm-x86/hvm/vpmu.h     |  2 +
 xen/include/public/xen.h           |  1 +
 xen/include/public/xenpmu.h        | 19 ++++++++++
 xen/include/xen/hypercall.h        |  4 ++
 9 files changed, 105 insertions(+), 10 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +1 -1
index 9f9c9ea..4477f63 100644
@@ -458,7 +458,7 @@ int svm_vpmu_initialise(struct vcpu *v, unsigned int vpmu_flags)
     int ret = 0;
 
     /* vpmu enabled? */
-    if ( !vpmu_flags )
+    if ( vpmu_flags == VPMU_OFF )
         return 0;
 
     switch ( family )
xen/arch/x86/hvm/vmx/vpmu_core2.c +1 -1
index f3b6de0..66325d5 100644
@@ -824,7 +824,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 == VPMU_OFF )
         return 0;
 
     if ( family == 6 )
xen/arch/x86/hvm/vpmu.c +69 -8

118 lines not on this page · raw message

xen/arch/x86/x86_64/compat/entry.S +4 -0
index 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 -0
index f5f8c9c..cc45c70 100644
@@ -89,5 +89,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 +19 -0
index 420b674..7240a30 100644
@@ -8,6 +8,25 @@
 #define XENPMU_VER_MAJ    0
 #define XENPMU_VER_MIN    0
 
+/* HYPERVISOR_xenpmu_op commands */
+#define XENPMU_mode_get        0
+#define XENPMU_mode_set        1
+#define XENPMU_flags_get       2
+#define XENPMU_flags_set       3
+
+/* Parameters structure for HYPERVISOR_xenpmu_op call */
+typedef struct xenpmu_params {
+    union {
+        struct version {
+            uint8_t maj;
+            uint8_t min;
+        } version;
+    uint64_t pad;
+    };
+    uint64_t control;
+} xenpmu_params_t;
+
+
 /* VPMU modes */
 #define VPMU_MODE_MASK     0xff
 #define VPMU_OFF           0
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
#19 lore raw

PATCH 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 |  58 +++++++++++++-------
 xen/arch/x86/hvm/vpmu.c           | 110 +++++++++++++++++++++++++++++++++++++-
 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       |  23 ++++++--
 xen/include/xen/softirq.h         |   1 +
 8 files changed, 189 insertions(+), 40 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +19 -15
index 4477f63..c39c7a2 100644
@@ -367,14 +367,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);
@@ -388,18 +393,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 +38 -20

81 lines not on this page · raw message

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

141 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 cc45c70..46dfbc6 100644
@@ -59,6 +59,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 +19 -4
index 7240a30..ffaf3fe 100644
@@ -13,6 +13,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 */
 typedef struct xenpmu_params {
@@ -24,6 +26,8 @@ typedef struct xenpmu_params {
     uint64_t pad;
     };
     uint64_t control;
+    uint64_t mfn;
+    uint64_t vcpu;
 } xenpmu_params_t;
 
 
@@ -83,11 +87,14 @@ struct arch_msr_pair {
     uint64_t control;
 };
 struct core2_vpmu_context {
-    struct core2_pmu_enable *pmu_enable;
+    uint64_t global_ctrl;
+    uint64_t global_ovf_ctrl;
+    uint64_t global_status;
+    uint64_t global_ovf_status;
     uint64_t fix_counters[VPMU_CORE2_NUM_FIXED];
     uint64_t ctrls[VPMU_CORE2_NUM_CTRLS];
-    uint64_t global_ovf_status;
     struct arch_msr_pair arch_msr_pair[VPMU_CORE2_MAX_ARCH_PMCS];
+    struct core2_pmu_enable *pmu_enable;
 };
 
 /* PMU flags */
@@ -95,14 +102,22 @@ struct core2_vpmu_context {
 
 /* Shared between hypervisor and PV domain */
 typedef struct xenpmu_data {
-    struct cpu_user_regs regs;
-    uint16_t domain_id;
+    union {
+        struct cpu_user_regs regs;
+        uint8_t pad[256];
+    };
+    uint32_t domain_id;
     uint32_t vcpu_id;
     uint32_t pcpu_id;
     uint32_t pmu_flags;
     union {
         struct amd_vpmu_context amd;
         struct core2_vpmu_context intel;
+#define MAX(x,y) ((x) > (y) ? (x) : (y))
+#define MAX_CTXT_SZ     MAX(sizeof(struct amd_vpmu_context),\
+                            sizeof(struct core2_vpmu_context))
+#define PMU_PAD_SIZE        ((MAX_CTXT_SZ + 64) & ~63) + 128
+        uint8_t pad[PMU_PAD_SIZE]; /* a bit more than necessary */
     } pmu;
 } xenpmu_data_t;
 
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
#20 lore raw

PATCH 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 | 94 ++++++++++++++++++++++++++++++---------
 xen/arch/x86/hvm/vpmu.c           | 16 +++++++
 xen/arch/x86/traps.c              | 38 +++++++++++++++-
 xen/include/public/xenpmu.h       |  2 +
 5 files changed, 128 insertions(+), 25 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 +73 -21

177 lines not on this page · raw message

xen/arch/x86/hvm/vpmu.c +16 -0
index 04cc114..0adacce 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);
@@ -425,6 +433,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.lvtpc);
+        ret = 0;
+        break;
     }
 
     return ret;
xen/arch/x86/traps.c +36 -2
index 57dbd0c..64c9c25 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,8 @@ static void pv_cpuid(struct cpu_user_regs *regs)
     unsupported:
         a = b = c = d = 0;
         break;
-
+    case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */
+        break;
     default:
         (void)cpuid_hypervisor_leaves(regs->eax, 0, &a, &b, &c, &d);
         break;
@@ -2486,6 +2487,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 & VPMU_PRIV) && (v->domain == dom0) )
+                    goto invalid;
+            }
+            break;
         default:
             if ( wrmsr_hypervisor_regs(regs->ecx, msr_content) == 1 )
                 break;
@@ -2574,6 +2586,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 +2636,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 +2 -0
index ffaf3fe..dc8bad2 100644
@@ -15,6 +15,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 */
 typedef struct xenpmu_params {
@@ -28,6 +29,7 @@ typedef struct xenpmu_params {
     uint64_t control;
     uint64_t mfn;
     uint64_t vcpu;
+    uint64_t lvtpc;
 } xenpmu_params_t;
 
 
Signature · 1 line
1.8.1.4
#21 lore raw

PATCH 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                        | 110 +++++++++++++++++++++++--
 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, 114 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 c39c7a2..1815674 100644
@@ -280,8 +280,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 )
@@ -292,8 +292,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 489dc49..3f5941a 100644
@@ -558,13 +558,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 +104 -6

166 lines not on this page · raw message

xen/include/asm-x86/hvm/vpmu.h +1 -0
index 46dfbc6..f7f507f 100644
@@ -69,6 +69,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 dc8bad2..e70e55e 100644
@@ -16,6 +16,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 */
 typedef struct xenpmu_params {
Signature · 1 line
1.8.1.4
#22 lore raw

PATCH 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..e74ad5c 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 & VPMU_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 & VPMU_PRIV) )
         /* Must be done with interrupts enabled */
         vpmu_load(next);
 
Signature · 1 line
1.8.1.4
#23 lore raw

PATCH 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           | 486 ------------------
 xen/arch/x86/hvm/vmx/Makefile         |   1 -
 xen/arch/x86/hvm/vmx/vpmu_core2.c     | 938 ----------------------------------
 xen/arch/x86/hvm/vpmu.c               | 545 --------------------
 xen/arch/x86/oprofile/op_model_ppro.c |   2 +-
 xen/arch/x86/traps.c                  |   2 +-
 xen/arch/x86/vpmu.c                   | 545 ++++++++++++++++++++
 xen/arch/x86/vpmu_amd.c               | 486 ++++++++++++++++++
 xen/arch/x86/vpmu_intel.c             | 938 ++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h          |   1 +
 xen/include/asm-x86/hvm/vmx/vmcs.h    |   1 -
 xen/include/asm-x86/hvm/vpmu.h        |  97 ----
 xen/include/asm-x86/vpmu.h            |  97 ++++
 16 files changed, 2070 insertions(+), 2072 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 a27ac44..47d067d 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 -486

486 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 -938

938 lines not on this page · raw message

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

545 lines not on this page · raw message

xen/arch/x86/oprofile/op_model_ppro.c +1 -1
index 2939a40..9135801 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 64c9c25..bca0a37 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 +545 -0

545 lines not on this page · raw message

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

449 lines not on this page · raw message

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

938 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 -97

97 lines not on this page · raw message

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

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

#24 lore raw
In reply to Boris Ostrovsky
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

This version has following limitations:
* For accurate profiling of dom0/Xen dom0 VCPUs should be pinned.
* Hypervisor code is only profiled on processors that have running dom0 VCPUs on them.

With that I assume this is an RFC rather than full-fledged submission?

Jan

#25 lore raw
Quoted 6 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

This version has following limitations:
* For accurate profiling of dom0/Xen dom0 VCPUs should be pinned.
* Hypervisor code is only profiled on processors that have running dom0 VCPUs on them.

With that I assume this is an RFC rather than full-fledged submission?

I was thinking that this would be something like stage 1 implementation (and probably should have mentioned this in the cover letter).

For this stage I wanted to confine all changes on Linux side to xen subtrees. Properly addressing the above limitation would likely require changes in non-xen sources (change in perf file format, remote MSR access etc.).

-boris

#26 lore raw
Quoted 6 lines · Boris Ostrovsky
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

This version has following limitations:
* For accurate profiling of dom0/Xen dom0 VCPUs should be pinned.
* Hypervisor code is only profiled on processors that have running dom0 VCPUs on them.

Quoted 8 lines
Quoted 1 line

With that I assume this is an RFC rather than full-fledged submission?

I was thinking that this would be something like stage 1 implementation (and probably should have mentioned this in the cover letter).

For this stage I wanted to confine all changes on Linux side to xen subtrees. Properly addressing the above limitation would likely require changes in non-xen sources (change in perf file format, remote MSR access etc.).

I think having the vpmu stuff for PV guests is a great idea, and from a quick skim through I don't have any problems with the general approach. (Obviously some more detailed review will be needed.)

However, I'm not a fan of this method of collecting perf stuff for Xen and other VMs together in the cpu buffers for dom0. I think it's ugly, fragile, and non-scalable, and I would prefer to see if we could implement the same feature (allowing perf to analyze Xen and other vcpus) some other way. And I would rather not use it as a "stage 1", for fear that it would become entrenched.

I think at the hackathon we discussed the idea of having "fake" cpus -- each of which would correspond to either a pcpu with Xen, or a vcpu of another domain. How problematic is that approach? For phase 1 can we just do vpmu for PV guests (and add hooks to allow domains to profile themselves), and look into how to profile Xen and other VMs as a stage 2?

-George

#27 lore raw
Quoted 12 lines · George Dunlap
Quoted 11 lines · Boris Ostrovsky
Quoted 6 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

This version has following limitations:
* For accurate profiling of dom0/Xen dom0 VCPUs should be pinned.
* Hypervisor code is only profiled on processors that have running dom0 VCPUs on them.

With that I assume this is an RFC rather than full-fledged submission?

I was thinking that this would be something like stage 1 implementation (and probably should have mentioned this in the cover letter).

Quoted 7 lines
Quoted 3 lines

For this stage I wanted to confine all changes on Linux side to xen subtrees. Properly addressing the above limitation would likely require changes in non-xen sources (change in perf file format, remote MSR access etc.).

I think having the vpmu stuff for PV guests is a great idea, and from a quick skim through I don't have any problems with the general approach. (Obviously some more detailed review will be needed.)

However, I'm not a fan of this method of collecting perf stuff for Xen and other VMs together in the cpu buffers for dom0. I think it's ugly, fragile, and non-scalable, and I would prefer to see if we could implement the same feature (allowing perf to analyze Xen and other vcpus) some other way. And I would rather not use it as a "stage 1", for fear that it would become entrenched.

I can see how collecting samples for other domains may be questionable now (DOM0_PRIV mode) since at this stage there is no way to distinguish between samples for non-priviledged domains.

But why do you think that getting data for both dom0 and Xen is problematic? Someone has to process Xen's samples and who would do this if not dom0? We could store samples in separate files (e.g. perf.data.dom0 and perf.data.xen) but that's toolstack's job.

Quoted 4 lines

I think at the hackathon we discussed the idea of having "fake" cpus -- each of which would correspond to either a pcpu with Xen, or a vcpu of another domain. How problematic is that approach?

This is what I was planning to do later. Those would be "fake" CPUs in the sense that their cpuids would be something like (vcpuID | domainID) or (PCPU|<sometag>). But it would be a natural extension of what is being done now.

Quoted 1 line

For phase 1 can we just do vpmu for PV guests (and add hooks to allow domains to profile themselves), and look into how to profile Xen and other VMs as a stage 2?

-boris

#28 lore raw
Quoted 17 lines · Boris Ostrovsky
Quoted 16 lines · George Dunlap
Quoted 14 lines · Boris Ostrovsky
Quoted 6 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

This version has following limitations:
* For accurate profiling of dom0/Xen dom0 VCPUs should be pinned.
* Hypervisor code is only profiled on processors that have running dom0 VCPUs on them.

With that I assume this is an RFC rather than full-fledged submission?

I was thinking that this would be something like stage 1 implementation (and probably should have mentioned this in the cover letter).

For this stage I wanted to confine all changes on Linux side to xen subtrees. Properly addressing the above limitation would likely require changes in non-xen sources (change in perf file format, remote MSR access etc.).

Quoted 7 lines
Quoted 3 lines

I think having the vpmu stuff for PV guests is a great idea, and from a quick skim through I don't have any problems with the general approach. (Obviously some more detailed review will be needed.)

However, I'm not a fan of this method of collecting perf stuff for Xen and other VMs together in the cpu buffers for dom0. I think it's ugly, fragile, and non-scalable, and I would prefer to see if we could implement the same feature (allowing perf to analyze Xen and other vcpus) some other way. And I would rather not use it as a "stage 1", for fear that it would become entrenched.

I can see how collecting samples for other domains may be questionable now (DOM0_PRIV mode) since at this stage there is no way to distinguish between samples for non-priviledged domains.

But why do you think that getting data for both dom0 and Xen is problematic? Someone has to process Xen's samples and who would do this if not dom0? We could store samples in separate files (e.g. perf.data.dom0 and perf.data.xen) but that's toolstack's job.

It's not so much about dom0 collecting the samples and passing them on to the analysis tools; this is already what xenalyze does, in essence. It's about the requirement of having the dom0 vcpus pinned 1-1 to physical cpus: both limiting the flexibility for scheduling, and limiting the configuration flexibility wrt having dom0 vcpus < pcpus. That is what seems an ugly hack to me -- having dom0 sort of try to do something that requires hypervisor-level privileges and making a bit of a mess of it.

I'm unfortunately not familiar enough with the perf system to know exactly what it is that Linux needs to do (why, for example, you think it would need remote MSR access if dom0 weren't pinned), and how hard would be for Xen just to do that work, and provide an "adapter" that would translate Xen-specific stuff into something perf could consume. Would it be possible, for example, for dom0 to specify what needed to be collected, for Xen to generate the samples in a Xen-specific format, and then have something in dom0 that would separate the samples into one file per domain that look similar enough to a trace file that the perf system could consume it?

-George

#29 lore raw
Quoted 20 lines · George Dunlap
Quoted 19 lines · Boris Ostrovsky
Quoted 18 lines · George Dunlap
Quoted 6 lines · Boris Ostrovsky
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky

This version has following limitations:
* For accurate profiling of dom0/Xen dom0 VCPUs should be pinned.
* Hypervisor code is only profiled on processors that have running dom0 VCPUs on them.

Quoted 8 lines
Quoted 1 line

With that I assume this is an RFC rather than full-fledged submission?

I was thinking that this would be something like stage 1 implementation (and probably should have mentioned this in the cover letter).

For this stage I wanted to confine all changes on Linux side to xen subtrees. Properly addressing the above limitation would likely require changes in non-xen sources (change in perf file format, remote MSR access etc.).

I think having the vpmu stuff for PV guests is a great idea, and from a quick skim through I don't have any problems with the general approach. (Obviously some more detailed review will be needed.)

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

However, I'm not a fan of this method of collecting perf stuff for Xen and other VMs together in the cpu buffers for dom0. I think it's ugly, fragile, and non-scalable, and I would prefer to see if we could implement the same feature (allowing perf to analyze Xen and other vcpus) some other way. And I would rather not use it as a "stage 1", for fear that it would become entrenched.

I can see how collecting samples for other domains may be questionable now (DOM0_PRIV mode) since at this stage there is no way to distinguish between samples for non-priviledged domains.

But why do you think that getting data for both dom0 and Xen is problematic? Someone has to process Xen's samples and who would do this if not dom0? We could store samples in separate files (e.g. perf.data.dom0 and perf.data.xen) but that's toolstack's job.

It's not so much about dom0 collecting the samples and passing them on to the analysis tools; this is already what xenalyze does, in essence. It's about the requirement of having the dom0 vcpus pinned 1-1 to physical cpus: both limiting the flexibility for scheduling, and limiting the configuration flexibility wrt having dom0 vcpus < pcpus. That is what seems an ugly hack to me -- having dom0 sort of try to do something that requires hypervisor-level privileges and making a bit of a mess of it.

I probably should have explained the limitations better in the original message.

Pinning:

The only reason this version requires pinning is because I haven't provided hooks in Linux perf code to store both PCPU and VCPU of the sample in the perf_sample_data. And I didn't do so this because this would need to be done outside of arch/x86/xen and I decided not to go there for this stage. So for now perf still only knows about CPUs, not PCPUs or VCPUs.

Note that hypervisor already provides information about both P/VCPUs to dom0 (*) so so when I fix what I described above in Linux (kernel and perf toolstack) the right association of P/VCPUs will start working.

And pinning is not really *required*. If you don't pin you will not get accurate sample distribution of hypervisor samples in perf. For instance, if Xen's foo() was sampled on PCPU0 and then PCPU1 while dom0's VCPU0 was running on each of them perf will assime that both samples were taken on CPU0. Note again: CPU0, not P- or VCPU0).

#VCPUs < #PCPUs

This is different from pinning. The issue here is that tools (e.g. perf) need to access the PMU's MSR. And they do it with something like wrmsr(msr, value), and they assume that they are programming PMU on current processor. So if a dom0's VCPU never runs on some PCPU it currently cannot program the PMU there. One way to address this could be to have wrmsr_cpu(cpu, msr, value). And presumably on bare metal this will be patched over with regular wrmsr.

(*) Well, it doesn't. Because I forgot to add this to the code (it's one line, really) but I will in the next version.

Quoted 2 lines

I'm unfortunately not familiar enough with the perf system to know exactly what it is that Linux needs to do (why, for example, you think it would need remote MSR access if dom0 weren't pinned),

Remote MSR access is needed not because of pinning but because the tool (perf, or any other tool for that matter) needs to program the PMU on non-dom0 processors.

Quoted 1 line

and how hard would be for Xen just to do that work, and provide an "adapter" that would translate Xen-specific stuff into something perf could consume. Would it be possible, for example, for dom0 to specify what needed to be collected, for Xen to generate the samples in a Xen-specific format, and then have something in dom0 that would separate the samples into one file per domain that look similar enough to a trace file that the perf system could consume it?

Perf calculates sampling period on each sample and writes resulting value into the counter MSR (I haven't looked yet at how it uses other performance facilities such as PEBS, IBS and such).

Processing sample data is done by the toolstack and is relatively easy, we don't need Xen-specific format (once we fix the pinning issue so we know to whom a sample belongs). Programming PMU HW from exiting perf code is the challenge.

Thanks.
-boris