1 /*-
2  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
3  * Copyright (c) 2016, 2017, 2019 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Konstantin Belousov
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: stable/12/lib/libc/x86/sys/__vdso_gettc.c 370924 2021-10-17 18:50:47Z kib $");
33 
34 #include <sys/param.h>
35 #include "namespace.h"
36 #include <sys/capsicum.h>
37 #include <sys/elf.h>
38 #include <sys/fcntl.h>
39 #include <sys/mman.h>
40 #include <sys/time.h>
41 #include <sys/vdso.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include "un-namespace.h"
46 #include <machine/atomic.h>
47 #include <machine/cpufunc.h>
48 #include <machine/pvclock.h>
49 #include <machine/specialreg.h>
50 #include <dev/acpica/acpi_hpet.h>
51 #ifdef WANT_HYPERV
52 #include <dev/hyperv/hyperv.h>
53 #endif
54 #include <x86/ifunc.h>
55 #include "libc_private.h"
56 
57 static inline u_int
rdtsc_low(const struct vdso_timehands * th)58 rdtsc_low(const struct vdso_timehands *th)
59 {
60 	u_int rv;
61 
62 	__asm __volatile("rdtsc; shrd %%cl, %%edx, %0"
63 	    : "=a" (rv) : "c" (th->th_x86_shift) : "edx");
64 	return (rv);
65 }
66 
67 static inline u_int
rdtscp_low(const struct vdso_timehands * th)68 rdtscp_low(const struct vdso_timehands *th)
69 {
70 	u_int rv;
71 
72 	__asm __volatile("rdtscp; movl %%edi,%%ecx; shrd %%cl, %%edx, %0"
73 	    : "=a" (rv) : "D" (th->th_x86_shift) : "ecx", "edx");
74 	return (rv);
75 }
76 
77 static u_int
rdtsc_low_mb_lfence(const struct vdso_timehands * th)78 rdtsc_low_mb_lfence(const struct vdso_timehands *th)
79 {
80 	lfence();
81 	return (rdtsc_low(th));
82 }
83 
84 static u_int
rdtsc_low_mb_mfence(const struct vdso_timehands * th)85 rdtsc_low_mb_mfence(const struct vdso_timehands *th)
86 {
87 	mfence();
88 	return (rdtsc_low(th));
89 }
90 
91 static u_int
rdtsc_low_mb_none(const struct vdso_timehands * th)92 rdtsc_low_mb_none(const struct vdso_timehands *th)
93 {
94 	return (rdtsc_low(th));
95 }
96 
97 static u_int
rdtsc32_mb_lfence(void)98 rdtsc32_mb_lfence(void)
99 {
100 	lfence();
101 	return (rdtsc32());
102 }
103 
104 static u_int
rdtsc32_mb_mfence(void)105 rdtsc32_mb_mfence(void)
106 {
107 	mfence();
108 	return (rdtsc32());
109 }
110 
111 static u_int
rdtsc32_mb_none(void)112 rdtsc32_mb_none(void)
113 {
114 	return (rdtsc32());
115 }
116 
117 static u_int
rdtscp32_(void)118 rdtscp32_(void)
119 {
120 	return (rdtscp32());
121 }
122 
123 struct tsc_selector_tag {
124 	u_int (*ts_rdtsc32)(void);
125 	u_int (*ts_rdtsc_low)(const struct vdso_timehands *);
126 };
127 
128 static const struct tsc_selector_tag tsc_selector[] = {
129 	[0] = {				/* Intel, LFENCE */
130 		.ts_rdtsc32 =	rdtsc32_mb_lfence,
131 		.ts_rdtsc_low =	rdtsc_low_mb_lfence,
132 	},
133 	[1] = {				/* AMD, MFENCE */
134 		.ts_rdtsc32 =	rdtsc32_mb_mfence,
135 		.ts_rdtsc_low =	rdtsc_low_mb_mfence,
136 	},
137 	[2] = {				/* No SSE2 */
138 		.ts_rdtsc32 = rdtsc32_mb_none,
139 		.ts_rdtsc_low = rdtsc_low_mb_none,
140 	},
141 	[3] = {				/* RDTSCP */
142 		.ts_rdtsc32 =	rdtscp32_,
143 		.ts_rdtsc_low =	rdtscp_low,
144 	},
145 };
146 
147 static int
tsc_selector_idx(u_int cpu_feature)148 tsc_selector_idx(u_int cpu_feature)
149 {
150 	u_int amd_feature, cpu_exthigh, cpu_id, p[4], v[3];
151 	static const char amd_id[] = "AuthenticAMD";
152 	static const char hygon_id[] = "HygonGenuine";
153 	bool amd_cpu;
154 
155 	if (cpu_feature == 0)
156 		return (2);	/* should not happen due to RDTSC */
157 
158 	do_cpuid(0, p);
159 	v[0] = p[1];
160 	v[1] = p[3];
161 	v[2] = p[2];
162 	amd_cpu = memcmp(v, amd_id, sizeof(amd_id) - 1) == 0 ||
163 	    memcmp(v, hygon_id, sizeof(hygon_id) - 1) == 0;
164 
165 	do_cpuid(1, p);
166 	cpu_id = p[0];
167 
168 	if (cpu_feature != 0) {
169 		do_cpuid(0x80000000, p);
170 		cpu_exthigh = p[0];
171 	} else {
172 		cpu_exthigh = 0;
173 	}
174 	if (cpu_exthigh >= 0x80000001) {
175 		do_cpuid(0x80000001, p);
176 		amd_feature = p[3];
177 	} else {
178 		amd_feature = 0;
179 	}
180 
181 	if ((amd_feature & AMDID_RDTSCP) != 0)
182 		return (3);
183 	if ((cpu_feature & CPUID_SSE2) == 0)
184 		return (2);
185 	return (amd_cpu ? 1 : 0);
186 }
187 
188 DEFINE_UIFUNC(static, u_int, __vdso_gettc_rdtsc_low,
189     (const struct vdso_timehands *th), static)
190 {
191 	return (tsc_selector[tsc_selector_idx(cpu_feature)].ts_rdtsc_low);
192 }
193 
194 DEFINE_UIFUNC(static, u_int, __vdso_gettc_rdtsc32, (void), static)
195 {
196 	return (tsc_selector[tsc_selector_idx(cpu_feature)].ts_rdtsc32);
197 }
198 
199 #define	HPET_DEV_MAP_MAX	10
200 static volatile char *hpet_dev_map[HPET_DEV_MAP_MAX];
201 
202 static void
__vdso_init_hpet(uint32_t u)203 __vdso_init_hpet(uint32_t u)
204 {
205 	static const char devprefix[] = "/dev/hpet";
206 	char devname[64], *c, *c1, t;
207 	volatile char *new_map, *old_map;
208 	unsigned int mode;
209 	uint32_t u1;
210 	int fd;
211 
212 	c1 = c = stpcpy(devname, devprefix);
213 	u1 = u;
214 	do {
215 		*c++ = u1 % 10 + '0';
216 		u1 /= 10;
217 	} while (u1 != 0);
218 	*c = '\0';
219 	for (c--; c1 != c; c1++, c--) {
220 		t = *c1;
221 		*c1 = *c;
222 		*c = t;
223 	}
224 
225 	old_map = hpet_dev_map[u];
226 	if (old_map != NULL)
227 		return;
228 
229 	/*
230 	 * Explicitely check for the capability mode to avoid
231 	 * triggering trap_enocap on the device open by absolute path.
232 	 */
233 	if ((cap_getmode(&mode) == 0 && mode != 0) ||
234 	    (fd = _open(devname, O_RDONLY)) == -1) {
235 		/* Prevent the caller from re-entering. */
236 		atomic_cmpset_rel_ptr((volatile uintptr_t *)&hpet_dev_map[u],
237 		    (uintptr_t)old_map, (uintptr_t)MAP_FAILED);
238 		return;
239 	}
240 
241 	new_map = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
242 	_close(fd);
243 	if (atomic_cmpset_rel_ptr((volatile uintptr_t *)&hpet_dev_map[u],
244 	    (uintptr_t)old_map, (uintptr_t)new_map) == 0 &&
245 	    new_map != MAP_FAILED)
246 		munmap((void *)new_map, PAGE_SIZE);
247 }
248 
249 #ifdef WANT_HYPERV
250 
251 #define HYPERV_REFTSC_DEVPATH	"/dev/" HYPERV_REFTSC_DEVNAME
252 
253 /*
254  * NOTE:
255  * We use 'NULL' for this variable to indicate that initialization
256  * is required.  And if this variable is 'MAP_FAILED', then Hyper-V
257  * reference TSC can not be used, e.g. in misconfigured jail.
258  */
259 static struct hyperv_reftsc *hyperv_ref_tsc;
260 
261 static void
__vdso_init_hyperv_tsc(void)262 __vdso_init_hyperv_tsc(void)
263 {
264 	int fd;
265 	unsigned int mode;
266 
267 	if (cap_getmode(&mode) == 0 && mode != 0)
268 		goto fail;
269 
270 	fd = _open(HYPERV_REFTSC_DEVPATH, O_RDONLY);
271 	if (fd < 0)
272 		goto fail;
273 	hyperv_ref_tsc = mmap(NULL, sizeof(*hyperv_ref_tsc), PROT_READ,
274 	    MAP_SHARED, fd, 0);
275 	_close(fd);
276 
277 	return;
278 fail:
279 	/* Prevent the caller from re-entering. */
280 	hyperv_ref_tsc = MAP_FAILED;
281 }
282 
283 static int
__vdso_hyperv_tsc(struct hyperv_reftsc * tsc_ref,u_int * tc)284 __vdso_hyperv_tsc(struct hyperv_reftsc *tsc_ref, u_int *tc)
285 {
286 	uint64_t disc, ret, tsc, scale;
287 	uint32_t seq;
288 	int64_t ofs;
289 
290 	while ((seq = atomic_load_acq_int(&tsc_ref->tsc_seq)) != 0) {
291 		scale = tsc_ref->tsc_scale;
292 		ofs = tsc_ref->tsc_ofs;
293 
294 		mfence();	/* XXXKIB */
295 		tsc = rdtsc();
296 
297 		/* ret = ((tsc * scale) >> 64) + ofs */
298 		__asm__ __volatile__ ("mulq %3" :
299 		    "=d" (ret), "=a" (disc) :
300 		    "a" (tsc), "r" (scale));
301 		ret += ofs;
302 
303 		atomic_thread_fence_acq();
304 		if (tsc_ref->tsc_seq == seq) {
305 			*tc = ret;
306 			return (0);
307 		}
308 
309 		/* Sequence changed; re-sync. */
310 	}
311 	return (ENOSYS);
312 }
313 
314 #endif	/* WANT_HYPERV */
315 
316 static struct pvclock_vcpu_time_info *pvclock_timeinfos;
317 
318 static int
__vdso_pvclock_gettc(const struct vdso_timehands * th,u_int * tc)319 __vdso_pvclock_gettc(const struct vdso_timehands *th, u_int *tc)
320 {
321 	uint64_t delta, ns, tsc;
322 	struct pvclock_vcpu_time_info *ti;
323 	uint32_t cpuid_ti, cpuid_tsc, version;
324 	bool stable;
325 
326 	do {
327 		ti = &pvclock_timeinfos[0];
328 		version = atomic_load_acq_32(&ti->version);
329 		stable = (ti->flags & th->th_x86_pvc_stable_mask) != 0;
330 		if (stable) {
331 			tsc = rdtscp();
332 		} else {
333 			(void)rdtscp_aux(&cpuid_ti);
334 			ti = &pvclock_timeinfos[cpuid_ti];
335 			version = atomic_load_acq_32(&ti->version);
336 			tsc = rdtscp_aux(&cpuid_tsc);
337 		}
338 		delta = tsc - ti->tsc_timestamp;
339 		ns = ti->system_time + pvclock_scale_delta(delta,
340 		    ti->tsc_to_system_mul, ti->tsc_shift);
341 		atomic_thread_fence_acq();
342 	} while ((ti->version & 1) != 0 || ti->version != version ||
343 	    (!stable && cpuid_ti != cpuid_tsc));
344 	*tc = MAX(ns, th->th_x86_pvc_last_systime);
345 	return (0);
346 }
347 
348 static void
__vdso_init_pvclock_timeinfos(void)349 __vdso_init_pvclock_timeinfos(void)
350 {
351 	struct pvclock_vcpu_time_info *timeinfos;
352 	size_t len;
353 	int fd, ncpus;
354 	unsigned int mode;
355 
356 	timeinfos = MAP_FAILED;
357 	if (_elf_aux_info(AT_NCPUS, &ncpus, sizeof(ncpus)) != 0 ||
358 	    (cap_getmode(&mode) == 0 && mode != 0) ||
359 	    (fd = _open("/dev/" PVCLOCK_CDEVNAME, O_RDONLY | O_CLOEXEC)) < 0)
360 		goto leave;
361 	len = ncpus * sizeof(*pvclock_timeinfos);
362 	timeinfos = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
363 	_close(fd);
364 leave:
365 	if (atomic_cmpset_rel_ptr(
366 	    (volatile uintptr_t *)&pvclock_timeinfos, (uintptr_t)NULL,
367 	    (uintptr_t)timeinfos) == 0 && timeinfos != MAP_FAILED)
368 		(void)munmap((void *)timeinfos, len);
369 }
370 
371 #pragma weak __vdso_gettc
372 int
__vdso_gettc(const struct vdso_timehands * th,u_int * tc)373 __vdso_gettc(const struct vdso_timehands *th, u_int *tc)
374 {
375 	volatile char *map;
376 	uint32_t idx;
377 
378 	switch (th->th_algo) {
379 	case VDSO_TH_ALGO_X86_TSC:
380 		*tc = th->th_x86_shift > 0 ? __vdso_gettc_rdtsc_low(th) :
381 		    __vdso_gettc_rdtsc32();
382 		return (0);
383 	case VDSO_TH_ALGO_X86_HPET:
384 		idx = th->th_x86_hpet_idx;
385 		if (idx >= HPET_DEV_MAP_MAX)
386 			return (ENOSYS);
387 		map = (volatile char *)atomic_load_acq_ptr(
388 		    (volatile uintptr_t *)&hpet_dev_map[idx]);
389 		if (map == NULL) {
390 			__vdso_init_hpet(idx);
391 			map = (volatile char *)atomic_load_acq_ptr(
392 			    (volatile uintptr_t *)&hpet_dev_map[idx]);
393 		}
394 		if (map == MAP_FAILED)
395 			return (ENOSYS);
396 		*tc = *(volatile uint32_t *)(map + HPET_MAIN_COUNTER);
397 		return (0);
398 #ifdef WANT_HYPERV
399 	case VDSO_TH_ALGO_X86_HVTSC:
400 		if (hyperv_ref_tsc == NULL)
401 			__vdso_init_hyperv_tsc();
402 		if (hyperv_ref_tsc == MAP_FAILED)
403 			return (ENOSYS);
404 		return (__vdso_hyperv_tsc(hyperv_ref_tsc, tc));
405 #endif
406 	case VDSO_TH_ALGO_X86_PVCLK:
407 		if (pvclock_timeinfos == NULL)
408 			__vdso_init_pvclock_timeinfos();
409 		if (pvclock_timeinfos == MAP_FAILED)
410 			return (ENOSYS);
411 		return (__vdso_pvclock_gettc(th, tc));
412 	default:
413 		return (ENOSYS);
414 	}
415 }
416 
417 #pragma weak __vdso_gettimekeep
418 int
__vdso_gettimekeep(struct vdso_timekeep ** tk)419 __vdso_gettimekeep(struct vdso_timekeep **tk)
420 {
421 
422 	return (_elf_aux_info(AT_TIMEKEEP, tk, sizeof(*tk)));
423 }
424