xref: /kvm-unit-tests/x86/hyperv_clock.c (revision 0d6cb7b205c32a78385345a12789cd53cbe11c0f)
1 #include "libcflat.h"
2 #include "smp.h"
3 #include "atomic.h"
4 #include "processor.h"
5 #include "hyperv.h"
6 #include "vm.h"
7 #include "alloc_page.h"
8 
9 #define MAX_CPU 4
10 #define TICKS_PER_SEC (1000000000 / 100)
11 
12 struct hv_reference_tsc_page *hv_clock;
13 
14 /*
15  * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
16  * yielding a 64-bit result.
17  */
18 static inline u64 scale_delta(u64 delta, u64 mul_frac)
19 {
20 	u64 product, unused;
21 
22 	__asm__ (
23 		"mulq %3"
24 		: "=d" (product), "=a" (unused) : "1" (delta), "rm" ((u64)mul_frac) );
25 
26 	return product;
27 }
28 
29 static u64 hvclock_tsc_to_ticks(struct hv_reference_tsc_page *shadow, uint64_t tsc)
30 {
31 	u64 delta = tsc;
32 	return scale_delta(delta, shadow->tsc_scale) + shadow->tsc_offset;
33 }
34 
35 /*
36  * Reads a consistent set of time-base values from hypervisor,
37  * into a shadow data area.
38  */
39 static void hvclock_get_time_values(struct hv_reference_tsc_page *shadow,
40 				    struct hv_reference_tsc_page *page)
41 {
42 	int seq;
43 	do {
44 		seq = page->tsc_sequence;
45 		rmb();		/* fetch version before data */
46 		*shadow = *page;
47 		rmb();		/* test version after fetching data */
48 	} while (shadow->tsc_sequence != seq);
49 }
50 
51 static uint64_t hv_clock_read(void)
52 {
53 	struct hv_reference_tsc_page shadow;
54 
55 	hvclock_get_time_values(&shadow, hv_clock);
56 	return hvclock_tsc_to_ticks(&shadow, rdtsc());
57 }
58 
59 bool ok[MAX_CPU];
60 uint64_t loops[MAX_CPU];
61 
62 #define iabs(x)   ((x) < 0 ? -(x) : (x))
63 
64 static void hv_clock_test(void *data)
65 {
66 	int i = (long)data;
67 	uint64_t t = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
68 	uint64_t end = t + 3 * TICKS_PER_SEC;
69 	uint64_t msr_sample = t + TICKS_PER_SEC;
70 	int min_delta = 123456, max_delta = -123456;
71 	bool got_drift = false;
72 	bool got_warp = false;
73 
74 	ok[i] = true;
75 	do {
76 		uint64_t now = hv_clock_read();
77 		int delta = rdmsr(HV_X64_MSR_TIME_REF_COUNT) - now;
78 
79 		min_delta = delta < min_delta ? delta : min_delta;
80 		if (t < msr_sample) {
81 			max_delta = delta > max_delta ? delta: max_delta;
82 		} else if (delta < 0 || delta > max_delta * 3 / 2) {
83 			printf("suspecting drift on CPU %d? delta = %d, acceptable [0, %d)\n", i,
84 			       delta, max_delta);
85 			ok[i] = false;
86 			got_drift = true;
87 			max_delta *= 2;
88 		}
89 
90 		if (now < t && !got_warp) {
91 			printf("warp on CPU %d!\n", i);
92 			ok[i] = false;
93 			got_warp = true;
94 			break;
95 		}
96 		t = now;
97 	} while(t < end);
98 
99 	if (!got_drift)
100 		printf("delta on CPU %d was %d...%d\n", i, min_delta, max_delta);
101 	barrier();
102 }
103 
104 static void check_test(int ncpus)
105 {
106 	int i;
107 	bool pass;
108 
109 	for (i = ncpus - 1; i >= 0; i--)
110 		on_cpu_async(i, hv_clock_test, (void *)(long)i);
111 
112 	while (cpus_active() > 1)
113 		pause();
114 
115 	pass = true;
116 	for (i = ncpus - 1; i >= 0; i--)
117 		pass &= ok[i];
118 
119 	report(pass, "TSC reference precision test");
120 }
121 
122 static void hv_perf_test(void *data)
123 {
124 	int i = (long)data;
125 	uint64_t t = hv_clock_read();
126 	uint64_t end = t + 1000000000 / 100;
127 	uint64_t local_loops = 0;
128 
129 	do {
130 		t = hv_clock_read();
131 		local_loops++;
132 	} while(t < end);
133 
134 	loops[i] = local_loops;
135 }
136 
137 static void perf_test(int ncpus)
138 {
139 	int i;
140 	uint64_t total_loops;
141 
142 	for (i = ncpus - 1; i >= 0; i--)
143 		on_cpu_async(i, hv_perf_test, (void *)(long)i);
144 
145 	while (cpus_active() > 1)
146 		pause();
147 
148 	total_loops = 0;
149 	for (i = ncpus - 1; i >= 0; i--)
150 		total_loops += loops[i];
151 	printf("iterations/sec:  %" PRId64"\n", total_loops / ncpus);
152 }
153 
154 int main(int ac, char **av)
155 {
156 	int nerr = 0;
157 	int ncpus;
158 	struct hv_reference_tsc_page shadow;
159 	uint64_t tsc1, t1, tsc2, t2;
160 	uint64_t ref1, ref2;
161 
162 	if (!hv_time_ref_counter_supported()) {
163 		report_skip("time reference counter is unsupported");
164 		return report_summary();
165 	}
166 
167 	setup_vm();
168 
169 	ncpus = cpu_count();
170 	if (ncpus > MAX_CPU)
171 		report_abort("number cpus exceeds %d", MAX_CPU);
172 
173 	hv_clock = alloc_page();
174 	wrmsr(HV_X64_MSR_REFERENCE_TSC, (u64)(uintptr_t)hv_clock | 1);
175 	report(rdmsr(HV_X64_MSR_REFERENCE_TSC) == ((u64)(uintptr_t)hv_clock | 1),
176 	       "MSR value after enabling");
177 
178 	hvclock_get_time_values(&shadow, hv_clock);
179 	if (shadow.tsc_sequence == 0 || shadow.tsc_sequence == 0xFFFFFFFF) {
180 		printf("Reference TSC page not available\n");
181 		exit(1);
182 	}
183 
184 	printf("sequence: %u. scale: %" PRIx64" offset: %" PRId64"\n",
185 	       shadow.tsc_sequence, shadow.tsc_scale, shadow.tsc_offset);
186 	ref1 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
187 	tsc1 = rdtsc();
188 	t1 = hvclock_tsc_to_ticks(&shadow, tsc1);
189 	printf("refcnt %" PRId64", TSC %" PRIx64", TSC reference %" PRId64"\n",
190 	       ref1, tsc1, t1);
191 
192 	do
193 		ref2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
194 	while (ref2 < ref1 + 2 * TICKS_PER_SEC);
195 
196 	tsc2 = rdtsc();
197 	t2 = hvclock_tsc_to_ticks(&shadow, tsc2);
198 	printf("refcnt %" PRId64" (delta %" PRId64"), TSC %" PRIx64", "
199 	       "TSC reference %" PRId64" (delta %" PRId64")\n",
200 	       ref2, ref2 - ref1, tsc2, t2, t2 - t1);
201 
202 	check_test(ncpus);
203 	perf_test(ncpus);
204 
205 	wrmsr(HV_X64_MSR_REFERENCE_TSC, 0LL);
206 	report(rdmsr(HV_X64_MSR_REFERENCE_TSC) == 0,
207 	       "MSR value after disabling");
208 
209 	return nerr > 0 ? 1 : 0;
210 }
211