xref: /kvm-unit-tests/x86/tsc.c (revision 73ee31a109362c405718ddc0f252ff01170797d4)
1 #include "libcflat.h"
2 #include "processor.h"
3 
test_wrtsc(u64 t1)4 static void test_wrtsc(u64 t1)
5 {
6 	u64 t2;
7 
8 	wrtsc(t1);
9 	t2 = rdtsc();
10 	printf("rdtsc after wrtsc(%" PRId64 "): %" PRId64 "\n", t1, t2);
11 }
12 
test_rdtscp(u64 aux)13 static void test_rdtscp(u64 aux)
14 {
15        u32 ecx;
16 
17        wrmsr(MSR_TSC_AUX, aux);
18        rdtscp(&ecx);
19        report(ecx == aux, "Test RDTSCP %" PRIu64, aux);
20 }
21 
test_rdpid(u64 aux)22 static void test_rdpid(u64 aux)
23 {
24        u32 eax;
25 
26        wrmsr(MSR_TSC_AUX, aux);
27        asm volatile (".byte 0xf3, 0x0f, 0xc7, 0xf8" : "=a" (eax));
28        report(eax == aux, "Test rdpid %%eax %" PRId64, aux);
29 }
30 
main(void)31 int main(void)
32 {
33 	u64 t1, t2;
34 
35 	t1 = rdtsc();
36 	t2 = rdtsc();
37 	printf("rdtsc latency %u\n", (unsigned)(t2 - t1));
38 
39 	test_wrtsc(0);
40 	test_wrtsc(100000000000ull);
41 
42 	if (this_cpu_has(X86_FEATURE_RDTSCP)) {
43 		test_rdtscp(0);
44 		test_rdtscp(10);
45 		test_rdtscp(0x100);
46 	} else
47 		printf("rdtscp not supported\n");
48 
49 	if (this_cpu_has(X86_FEATURE_RDPID)) {
50 		test_rdpid(0);
51 		test_rdpid(10);
52 		test_rdpid(0x100);
53 	} else
54 		printf("rdpid not supported\n");
55 	return report_summary();
56 }
57