]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/x86/sys/__vdso_gettc.c
x86 vdso gettc: eliminate duplicated code in ifunc selectors.
[FreeBSD/FreeBSD.git] / lib / libc / x86 / sys / __vdso_gettc.c
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$");
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/specialreg.h>
49 #include <dev/acpica/acpi_hpet.h>
50 #ifdef WANT_HYPERV
51 #include <dev/hyperv/hyperv.h>
52 #endif
53 #include <x86/ifunc.h>
54 #include "libc_private.h"
55
56 static inline u_int
57 rdtsc_low(const struct vdso_timehands *th)
58 {
59         u_int rv;
60
61         __asm __volatile("rdtsc; shrd %%cl, %%edx, %0"
62             : "=a" (rv) : "c" (th->th_x86_shift) : "edx");
63         return (rv);
64 }
65
66 static u_int
67 rdtsc_low_mb_lfence(const struct vdso_timehands *th)
68 {
69         lfence();
70         return (rdtsc_low(th));
71 }
72
73 static u_int
74 rdtsc_low_mb_mfence(const struct vdso_timehands *th)
75 {
76         mfence();
77         return (rdtsc_low(th));
78 }
79
80 static u_int
81 rdtsc_low_mb_none(const struct vdso_timehands *th)
82 {
83         return (rdtsc_low(th));
84 }
85
86 static u_int
87 rdtsc32_mb_lfence(void)
88 {
89         lfence();
90         return (rdtsc32());
91 }
92
93 static u_int
94 rdtsc32_mb_mfence(void)
95 {
96         mfence();
97         return (rdtsc32());
98 }
99
100 static u_int
101 rdtsc32_mb_none(void)
102 {
103         return (rdtsc32());
104 }
105
106 struct tsc_selector_tag {
107         u_int (*ts_rdtsc32)(void);
108         u_int (*ts_rdtsc_low)(const struct vdso_timehands *);
109 };
110
111 static const struct tsc_selector_tag tsc_selector[] = {
112         [0] = {                         /* Intel or AMD Zen+, LFENCE */
113                 .ts_rdtsc32 =   rdtsc32_mb_lfence,
114                 .ts_rdtsc_low = rdtsc_low_mb_lfence,
115         },
116         [1] = {                         /* AMD, MFENCE */
117                 .ts_rdtsc32 =   rdtsc32_mb_mfence,
118                 .ts_rdtsc_low = rdtsc_low_mb_mfence,
119         },
120         [2] = {                         /* No SSE2 */
121                 .ts_rdtsc32 = rdtsc32_mb_none,
122                 .ts_rdtsc_low = rdtsc_low_mb_none,
123         },
124 };
125
126 static int
127 tsc_selector_idx(u_int cpu_feature)
128 {
129         u_int amd_feature, cpu_exthigh, cpu_id, p[4], v[3];
130         static const char amd_id[] = "AuthenticAMD";
131         static const char hygon_id[] = "HygonGenuine";
132         bool amd_cpu;
133
134         if (cpu_feature == 0)
135                 return (2);     /* should not happen due to RDTSC */
136
137         do_cpuid(0, p);
138         v[0] = p[1];
139         v[1] = p[3];
140         v[2] = p[2];
141         amd_cpu = memcmp(v, amd_id, sizeof(amd_id) - 1) == 0 ||
142             memcmp(v, hygon_id, sizeof(hygon_id) - 1) == 0;
143
144         do_cpuid(1, p);
145         cpu_id = p[0];
146
147         if (cpu_feature != 0) {
148                 do_cpuid(0x80000000, p);
149                 cpu_exthigh = p[0];
150         } else {
151                 cpu_exthigh = 0;
152         }
153         if (cpu_exthigh >= 0x80000001) {
154                 do_cpuid(0x80000001, p);
155                 amd_feature = p[3];
156         } else {
157                 amd_feature = 0;
158         }
159
160         if ((cpu_feature & CPUID_SSE2) == 0)
161                 return (2);
162         return (amd_cpu ? 1 : 0);
163 }
164
165 DEFINE_UIFUNC(static, u_int, __vdso_gettc_rdtsc_low,
166     (const struct vdso_timehands *th))
167 {
168         return (tsc_selector[tsc_selector_idx(cpu_feature)].ts_rdtsc_low);
169 }
170
171 DEFINE_UIFUNC(static, u_int, __vdso_gettc_rdtsc32, (void))
172 {
173         return (tsc_selector[tsc_selector_idx(cpu_feature)].ts_rdtsc32);
174 }
175
176 #define HPET_DEV_MAP_MAX        10
177 static volatile char *hpet_dev_map[HPET_DEV_MAP_MAX];
178
179 static void
180 __vdso_init_hpet(uint32_t u)
181 {
182         static const char devprefix[] = "/dev/hpet";
183         char devname[64], *c, *c1, t;
184         volatile char *new_map, *old_map;
185         unsigned int mode;
186         uint32_t u1;
187         int fd;
188
189         c1 = c = stpcpy(devname, devprefix);
190         u1 = u;
191         do {
192                 *c++ = u1 % 10 + '0';
193                 u1 /= 10;
194         } while (u1 != 0);
195         *c = '\0';
196         for (c--; c1 != c; c1++, c--) {
197                 t = *c1;
198                 *c1 = *c;
199                 *c = t;
200         }
201
202         old_map = hpet_dev_map[u];
203         if (old_map != NULL)
204                 return;
205
206         /*
207          * Explicitely check for the capability mode to avoid
208          * triggering trap_enocap on the device open by absolute path.
209          */
210         if ((cap_getmode(&mode) == 0 && mode != 0) ||
211             (fd = _open(devname, O_RDONLY)) == -1) {
212                 /* Prevent the caller from re-entering. */
213                 atomic_cmpset_rel_ptr((volatile uintptr_t *)&hpet_dev_map[u],
214                     (uintptr_t)old_map, (uintptr_t)MAP_FAILED);
215                 return;
216         }
217
218         new_map = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
219         _close(fd);
220         if (atomic_cmpset_rel_ptr((volatile uintptr_t *)&hpet_dev_map[u],
221             (uintptr_t)old_map, (uintptr_t)new_map) == 0 &&
222             new_map != MAP_FAILED)
223                 munmap((void *)new_map, PAGE_SIZE);
224 }
225
226 #ifdef WANT_HYPERV
227
228 #define HYPERV_REFTSC_DEVPATH   "/dev/" HYPERV_REFTSC_DEVNAME
229
230 /*
231  * NOTE:
232  * We use 'NULL' for this variable to indicate that initialization
233  * is required.  And if this variable is 'MAP_FAILED', then Hyper-V
234  * reference TSC can not be used, e.g. in misconfigured jail.
235  */
236 static struct hyperv_reftsc *hyperv_ref_tsc;
237
238 static void
239 __vdso_init_hyperv_tsc(void)
240 {
241         int fd;
242         unsigned int mode;
243
244         if (cap_getmode(&mode) == 0 && mode != 0)
245                 goto fail;
246
247         fd = _open(HYPERV_REFTSC_DEVPATH, O_RDONLY);
248         if (fd < 0)
249                 goto fail;
250         hyperv_ref_tsc = mmap(NULL, sizeof(*hyperv_ref_tsc), PROT_READ,
251             MAP_SHARED, fd, 0);
252         _close(fd);
253
254         return;
255 fail:
256         /* Prevent the caller from re-entering. */
257         hyperv_ref_tsc = MAP_FAILED;
258 }
259
260 static int
261 __vdso_hyperv_tsc(struct hyperv_reftsc *tsc_ref, u_int *tc)
262 {
263         uint64_t disc, ret, tsc, scale;
264         uint32_t seq;
265         int64_t ofs;
266
267         while ((seq = atomic_load_acq_int(&tsc_ref->tsc_seq)) != 0) {
268                 scale = tsc_ref->tsc_scale;
269                 ofs = tsc_ref->tsc_ofs;
270
271                 mfence();       /* XXXKIB */
272                 tsc = rdtsc();
273
274                 /* ret = ((tsc * scale) >> 64) + ofs */
275                 __asm__ __volatile__ ("mulq %3" :
276                     "=d" (ret), "=a" (disc) :
277                     "a" (tsc), "r" (scale));
278                 ret += ofs;
279
280                 atomic_thread_fence_acq();
281                 if (tsc_ref->tsc_seq == seq) {
282                         *tc = ret;
283                         return (0);
284                 }
285
286                 /* Sequence changed; re-sync. */
287         }
288         return (ENOSYS);
289 }
290
291 #endif  /* WANT_HYPERV */
292
293 #pragma weak __vdso_gettc
294 int
295 __vdso_gettc(const struct vdso_timehands *th, u_int *tc)
296 {
297         volatile char *map;
298         uint32_t idx;
299
300         switch (th->th_algo) {
301         case VDSO_TH_ALGO_X86_TSC:
302                 *tc = th->th_x86_shift > 0 ? __vdso_gettc_rdtsc_low(th) :
303                     __vdso_gettc_rdtsc32();
304                 return (0);
305         case VDSO_TH_ALGO_X86_HPET:
306                 idx = th->th_x86_hpet_idx;
307                 if (idx >= HPET_DEV_MAP_MAX)
308                         return (ENOSYS);
309                 map = (volatile char *)atomic_load_acq_ptr(
310                     (volatile uintptr_t *)&hpet_dev_map[idx]);
311                 if (map == NULL) {
312                         __vdso_init_hpet(idx);
313                         map = (volatile char *)atomic_load_acq_ptr(
314                             (volatile uintptr_t *)&hpet_dev_map[idx]);
315                 }
316                 if (map == MAP_FAILED)
317                         return (ENOSYS);
318                 *tc = *(volatile uint32_t *)(map + HPET_MAIN_COUNTER);
319                 return (0);
320 #ifdef WANT_HYPERV
321         case VDSO_TH_ALGO_X86_HVTSC:
322                 if (hyperv_ref_tsc == NULL)
323                         __vdso_init_hyperv_tsc();
324                 if (hyperv_ref_tsc == MAP_FAILED)
325                         return (ENOSYS);
326                 return (__vdso_hyperv_tsc(hyperv_ref_tsc, tc));
327 #endif
328         default:
329                 return (ENOSYS);
330         }
331 }
332
333 #pragma weak __vdso_gettimekeep
334 int
335 __vdso_gettimekeep(struct vdso_timekeep **tk)
336 {
337
338         return (_elf_aux_info(AT_TIMEKEEP, tk, sizeof(*tk)));
339 }