xref: /kvm-unit-tests/x86/tsc.c (revision 7d36db351752e29ad27eaafe3f102de7064e429b)
1 #include "libcflat.h"
2 
3 u64 rdtsc(void)
4 {
5 	unsigned a, d;
6 
7 	asm volatile("rdtsc" : "=a"(a), "=d"(d));
8 	return a | (u64)d << 32;
9 }
10 
11 void wrtsc(u64 tsc)
12 {
13 	unsigned a = tsc, d = tsc >> 32;
14 
15 	asm volatile("wrmsr" : : "a"(a), "d"(d), "c"(0x10));
16 }
17 
18 void test_wrtsc(u64 t1)
19 {
20 	u64 t2;
21 
22 	wrtsc(t1);
23 	t2 = rdtsc();
24 	printf("rdtsc after wrtsc(%lld): %lld\n", t1, t2);
25 }
26 
27 int main()
28 {
29 	u64 t1, t2;
30 
31 	t1 = rdtsc();
32 	t2 = rdtsc();
33 	printf("rdtsc latency %lld\n", (unsigned)(t2 - t1));
34 
35 	test_wrtsc(0);
36 	test_wrtsc(100000000000ull);
37 	return 0;
38 }
39