public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH kvm-unit-tests 0/4] PMU tests
@ 2011-06-13 12:28 Avi Kivity
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 1/4] Build tests with optimization enabled Avi Kivity
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Avi Kivity @ 2011-06-13 12:28 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

Following is a simple unit test for the PMU emulation.

Avi Kivity (4):
  Build tests with optimization enabled
  startup: prepare CPU for floating point operation
  Add rdpmc instruction accessor
  Add PMU test

 Makefile              |    2 +-
 config-x86-common.mak |    4 ++-
 lib/x86/processor.h   |    8 ++++++
 lib/x86/smp.c         |    9 +++++++
 x86/cstart.S          |    2 +
 x86/cstart64.S        |    2 +
 x86/pmu.c             |   63 +++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 88 insertions(+), 2 deletions(-)
 create mode 100644 x86/pmu.c

-- 
1.7.5.3


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH kvm-unit-tests 1/4] Build tests with optimization enabled
  2011-06-13 12:28 [PATCH kvm-unit-tests 0/4] PMU tests Avi Kivity
@ 2011-06-13 12:28 ` Avi Kivity
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 2/4] startup: prepare CPU for floating point operation Avi Kivity
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Avi Kivity @ 2011-06-13 12:28 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 Makefile |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/Makefile b/Makefile
index b6e8759..7bce541 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ DESTDIR := $(PREFIX)/share/qemu/tests
 .PHONY: arch_clean clean
 
 #make sure env CFLAGS variable is not used
-CFLAGS = -g
+CFLAGS = -g -O2
 
 libgcc := $(shell $(CC) --print-libgcc-file-name)
 
-- 
1.7.5.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH kvm-unit-tests 2/4] startup: prepare CPU for floating point operation
  2011-06-13 12:28 [PATCH kvm-unit-tests 0/4] PMU tests Avi Kivity
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 1/4] Build tests with optimization enabled Avi Kivity
@ 2011-06-13 12:28 ` Avi Kivity
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 3/4] Add rdpmc instruction accessor Avi Kivity
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 4/4] Add PMU test Avi Kivity
  3 siblings, 0 replies; 5+ messages in thread
From: Avi Kivity @ 2011-06-13 12:28 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

- Clear cr0.ts
- Enable cr4.osxsave and cr4.osxmmexcpt (for sse)

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 lib/x86/smp.c  |    9 +++++++++
 x86/cstart.S   |    2 ++
 x86/cstart64.S |    2 ++
 3 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/lib/x86/smp.c b/lib/x86/smp.c
index d41c332..5e91f1a 100644
--- a/lib/x86/smp.c
+++ b/lib/x86/smp.c
@@ -3,6 +3,7 @@
 #include "smp.h"
 #include "apic.h"
 #include "fwcfg.h"
+#include "processor.h"
 
 #define IPI_VECTOR 0x20
 
@@ -124,6 +125,14 @@ void on_cpu_async(int cpu, void (*function)(void *data), void *data)
     __on_cpu(cpu, function, data, 0);
 }
 
+void __setup_cpu(void *data)
+{
+    write_cr4(read_cr4()
+	      | 0x200 /* OSFXSR; for SSE */
+	      | 0x400 /* OSXMMEXCPT, ditto */
+	      );
+    asm volatile ("clts");
+}
 
 void smp_init(void)
 {
diff --git a/x86/cstart.S b/x86/cstart.S
index bc8d563..36ad01f 100644
--- a/x86/cstart.S
+++ b/x86/cstart.S
@@ -121,6 +121,7 @@ ap_start32:
 	sti
 	nop
 	lock incw cpu_online_count
+	call __setup_cpu
 
 1:	hlt
 	jmp 1b
@@ -172,6 +173,7 @@ smp_init:
 1:	pause
 	cmpw %ax, cpu_online_count
 	jne 1b
+	call __setup_cpu
 smp_init_done:
 	ret
 
diff --git a/x86/cstart64.S b/x86/cstart64.S
index 71014d8..7d88dc5 100644
--- a/x86/cstart64.S
+++ b/x86/cstart64.S
@@ -170,6 +170,7 @@ ap_start64:
 	call load_tss
 	call enable_apic
 	call enable_x2apic
+	call __setup_cpu
 	sti
 	nop
 	lock incw cpu_online_count
@@ -234,6 +235,7 @@ smp_init:
 1:	pause
 	cmpw %ax, cpu_online_count
 	jne 1b
+	call __setup_cpu
 smp_init_done:
 	ret
 
-- 
1.7.5.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH kvm-unit-tests 3/4] Add rdpmc instruction accessor
  2011-06-13 12:28 [PATCH kvm-unit-tests 0/4] PMU tests Avi Kivity
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 1/4] Build tests with optimization enabled Avi Kivity
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 2/4] startup: prepare CPU for floating point operation Avi Kivity
@ 2011-06-13 12:28 ` Avi Kivity
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 4/4] Add PMU test Avi Kivity
  3 siblings, 0 replies; 5+ messages in thread
From: Avi Kivity @ 2011-06-13 12:28 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 lib/x86/processor.h |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index f69f9ff..c7e1afb 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -2,6 +2,7 @@
 #define LIBCFLAT_PROCESSOR_H
 
 #include "libcflat.h"
+#include <stdint.h>
 
 struct descriptor_table_ptr {
     u16 limit;
@@ -99,6 +100,13 @@ static inline void wrmsr(u32 index, u64 val)
     asm volatile ("wrmsr" : : "a"(a), "d"(d), "c"(index) : "memory");
 }
 
+static inline uint64_t rdpmc(uint32_t index)
+{
+    uint32_t a, d;
+    asm volatile ("rdpmc" : "=a"(a), "=d"(d) : "c"(index));
+    return a | ((uint64_t)d << 32);
+}
+
 static inline void write_cr0(ulong val)
 {
     asm volatile ("mov %0, %%cr0" : : "r"(val) : "memory");
-- 
1.7.5.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH kvm-unit-tests 4/4] Add PMU test
  2011-06-13 12:28 [PATCH kvm-unit-tests 0/4] PMU tests Avi Kivity
                   ` (2 preceding siblings ...)
  2011-06-13 12:28 ` [PATCH kvm-unit-tests 3/4] Add rdpmc instruction accessor Avi Kivity
@ 2011-06-13 12:28 ` Avi Kivity
  3 siblings, 0 replies; 5+ messages in thread
From: Avi Kivity @ 2011-06-13 12:28 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 config-x86-common.mak |    4 ++-
 x86/pmu.c             |   63 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 1 deletions(-)
 create mode 100644 x86/pmu.c

diff --git a/config-x86-common.mak b/config-x86-common.mak
index 033bae0..7932b5c 100644
--- a/config-x86-common.mak
+++ b/config-x86-common.mak
@@ -34,7 +34,7 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \
                $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \
                $(TEST_DIR)/kvmclock_test.flat  $(TEST_DIR)/eventinj.flat \
-               $(TEST_DIR)/s3.flat
+               $(TEST_DIR)/s3.flat $(TEST_DIR)/pmu.flat
 
 tests-common += api/api-sample
 tests-common += api/dirty-log
@@ -85,6 +85,8 @@ $(TEST_DIR)/eventinj.elf: $(cstart.o) $(TEST_DIR)/eventinj.o
 
 $(TEST_DIR)/s3.elf: $(cstart.o) $(TEST_DIR)/s3.o
 
+$(TEST_DIR)/pmu.elf: $(cstart.o) $(TEST_DIR)/pmu.o
+
 arch_clean:
 	$(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat $(TEST_DIR)/*.elf \
 	$(TEST_DIR)/.*.d $(TEST_DIR)/lib/.*.d $(TEST_DIR)/lib/*.o
diff --git a/x86/pmu.c b/x86/pmu.c
new file mode 100644
index 0000000..1513ea7
--- /dev/null
+++ b/x86/pmu.c
@@ -0,0 +1,63 @@
+
+#include "x86/msr.h"
+#include "x86/processor.h"
+#include "libcflat.h"
+#include <stdint.h>
+
+#define N 1000000
+
+static int tests, failures;
+
+static void loop()
+{
+    unsigned long tmp;
+
+    asm volatile("1: nop; nop; nop; nop; nop; nop; nop; nop; nop; loop 1b"
+		 : "=c"(tmp) : "0"(N));
+
+}
+
+static uint64_t measure(uint32_t sel_unit)
+{
+    wrmsr(MSR_IA32_PERFCTR0, 0);
+    wrmsr(MSR_P6_EVNTSEL0, 0x430000 | sel_unit);  // enable
+    loop();
+    wrmsr(MSR_P6_EVNTSEL0, sel_unit); // disable
+    return rdmsr(MSR_IA32_PERFCTR0);
+}
+
+static void report(const char *name, bool pass)
+{
+    printf("%s: pmu %s\n", pass ? "PASS" : "FAIL", name);
+    tests += 1;
+    failures += !pass;
+}
+
+static void check(const char *name, uint32_t sel_unit,
+		  float min, float max)
+{
+    bool pass;
+    uint64_t n;
+
+    n = measure(sel_unit);
+    pass = n >= min * N && n <= max * N;
+    report(name, pass);
+}
+
+static void check_rdpmc(void)
+{
+    uint64_t val = 0x123456789ull;
+    wrmsr(MSR_IA32_PERFCTR0, val);
+    report("rdpmc", rdpmc(0) == (u32)val);
+}
+
+int main(int ac, char **av)
+{
+    check("cycles", 0x003c, 1, 15);
+    check("branches", 0x00c4, 1, 1.1);
+    check("branch misses", 0x00c5, 0, 0.1);
+    check("instructions", 0x00c0, 10, 10.2);
+    check_rdpmc();
+    printf("\n%d tests, %d failures\n", tests, failures);
+    return !failures ? 0 : 1;
+}
-- 
1.7.5.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-06-13 12:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-06-13 12:28 [PATCH kvm-unit-tests 0/4] PMU tests Avi Kivity
2011-06-13 12:28 ` [PATCH kvm-unit-tests 1/4] Build tests with optimization enabled Avi Kivity
2011-06-13 12:28 ` [PATCH kvm-unit-tests 2/4] startup: prepare CPU for floating point operation Avi Kivity
2011-06-13 12:28 ` [PATCH kvm-unit-tests 3/4] Add rdpmc instruction accessor Avi Kivity
2011-06-13 12:28 ` [PATCH kvm-unit-tests 4/4] Add PMU test Avi Kivity

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox