kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH kvm-unit-tests] x86: tsc: add test for RDTSCP
@ 2015-11-12 11:12 Paolo Bonzini
  2015-11-13  1:42 ` Wanpeng Li
  2015-11-13  1:48 ` Wanpeng Li
  0 siblings, 2 replies; 4+ messages in thread
From: Paolo Bonzini @ 2015-11-12 11:12 UTC (permalink / raw)
  To: kvm

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 lib/x86/processor.h | 16 ++++++++++++++++
 x86/tsc.c           | 23 +++++++++++++++++++++++
 x86/unittests.cfg   |  1 +
 3 files changed, 40 insertions(+)

diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index 7973879..b9cb753 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -2,6 +2,7 @@
 #define LIBCFLAT_PROCESSOR_H
 
 #include "libcflat.h"
+#include "msr.h"
 #include <stdint.h>
 
 #ifdef __x86_64__
@@ -340,6 +341,21 @@ static inline unsigned long long rdtsc()
 	return r;
 }
 
+static inline unsigned long long rdtscp(u32 *aux)
+{
+       long long r;
+
+#ifdef __x86_64__
+       unsigned a, d;
+
+       asm volatile ("rdtscp" : "=a"(a), "=d"(d), "=c"(*aux));
+       r = a | ((long long)d << 32);
+#else
+       asm volatile ("rdtscp" : "=A"(r), "=c"(*aux));
+#endif
+       return r;
+}
+
 static inline void wrtsc(u64 tsc)
 {
 	unsigned a = tsc, d = tsc >> 32;
diff --git a/x86/tsc.c b/x86/tsc.c
index 58f332d..c71dc2a 100644
--- a/x86/tsc.c
+++ b/x86/tsc.c
@@ -1,6 +1,13 @@
 #include "libcflat.h"
 #include "processor.h"
 
+#define CPUID_80000001_EDX_RDTSCP	    (1 << 27)
+int check_cpuid_80000001_edx(unsigned int bit)
+{
+    return (cpuid(0x80000001).d & bit) != 0;
+}
+
+
 void test_wrtsc(u64 t1)
 {
 	u64 t2;
@@ -10,6 +17,15 @@ void test_wrtsc(u64 t1)
 	printf("rdtsc after wrtsc(%lld): %lld\n", t1, t2);
 }
 
+void test_rdtscp(u64 aux)
+{
+       u32 ecx;
+
+       wrmsr(MSR_TSC_AUX, aux);
+       rdtscp(&ecx);
+       report("Test RDTSCP %d", ecx == aux, aux);
+}
+
 int main()
 {
 	u64 t1, t2;
@@ -20,5 +36,12 @@ int main()
 
 	test_wrtsc(0);
 	test_wrtsc(100000000000ull);
+
+	if (check_cpuid_80000001_edx(CPUID_80000001_EDX_RDTSCP)) {
+		test_rdtscp(0);
+		test_rdtscp(10);
+		test_rdtscp(0x100);
+	} else
+		printf("rdtscp not supported\n");
 	return 0;
 }
diff --git a/x86/unittests.cfg b/x86/unittests.cfg
index ac652fc..fd7e9b2 100644
--- a/x86/unittests.cfg
+++ b/x86/unittests.cfg
@@ -114,6 +114,7 @@ file = sieve.flat
 
 [tsc]
 file = tsc.flat
+extra_params = -cpu kvm64,+rdtscp
 
 [tsc_adjust]
 file = tsc_adjust.flat
-- 
2.5.0


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

end of thread, other threads:[~2015-11-13  9:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-12 11:12 [PATCH kvm-unit-tests] x86: tsc: add test for RDTSCP Paolo Bonzini
2015-11-13  1:42 ` Wanpeng Li
2015-11-13  1:48 ` Wanpeng Li
2015-11-13  9:23   ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).