xref: /kvm-unit-tests/x86/tsc.c (revision 4363f1d9a646a5c7ea673bee8fc33ca6f2cddbd8)
1 #include "libcflat.h"
2 #include "processor.h"
3 
4 #define CPUID_80000001_EDX_RDTSCP	    (1 << 27)
5 int check_cpuid_80000001_edx(unsigned int bit)
6 {
7     return (cpuid(0x80000001).d & bit) != 0;
8 }
9 
10 
11 void test_wrtsc(u64 t1)
12 {
13 	u64 t2;
14 
15 	wrtsc(t1);
16 	t2 = rdtsc();
17 	printf("rdtsc after wrtsc(%" PRId64 "): %" PRId64 "\n", t1, t2);
18 }
19 
20 void test_rdtscp(u64 aux)
21 {
22        u32 ecx;
23 
24        wrmsr(MSR_TSC_AUX, aux);
25        rdtscp(&ecx);
26        report("Test RDTSCP %" PRIu64, ecx == aux, aux);
27 }
28 
29 int main()
30 {
31 	u64 t1, t2;
32 
33 	t1 = rdtsc();
34 	t2 = rdtsc();
35 	printf("rdtsc latency %u\n", (unsigned)(t2 - t1));
36 
37 	test_wrtsc(0);
38 	test_wrtsc(100000000000ull);
39 
40 	if (check_cpuid_80000001_edx(CPUID_80000001_EDX_RDTSCP)) {
41 		test_rdtscp(0);
42 		test_rdtscp(10);
43 		test_rdtscp(0x100);
44 	} else
45 		printf("rdtscp not supported\n");
46 	return report_summary();
47 }
48