]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/x86/sys/__vdso_gettc.c
x86 vdso gettc: reorganize ifunctions.
[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 DEFINE_UIFUNC(static, u_int, __vdso_gettc_rdtsc_low,
87     (const struct vdso_timehands *th))
88 {
89         u_int p[4];
90         /* Not a typo, string matches our do_cpuid() registers use. */
91         static const char intel_id[] = "GenuntelineI";
92
93         if ((cpu_feature & CPUID_SSE2) == 0)
94                 return (rdtsc_low_mb_none);
95         do_cpuid(0, p);
96         return (memcmp(p + 1, intel_id, sizeof(intel_id) - 1) == 0 ?
97             rdtsc_low_mb_lfence : rdtsc_low_mb_mfence);
98 }
99
100 static u_int
101 rdtsc32_mb_lfence(void)
102 {
103         lfence();
104         return (rdtsc32());
105 }
106
107 static u_int
108 rdtsc32_mb_mfence(void)
109 {
110         mfence();
111         return (rdtsc32());
112 }
113
114 static u_int
115 rdtsc32_mb_none(void)
116 {
117         return (rdtsc32());
118 }
119
120 DEFINE_UIFUNC(static, u_int, __vdso_gettc_rdtsc32, (void))
121 {
122         u_int p[4];
123         /* Not a typo, string matches our do_cpuid() registers use. */
124         static const char intel_id[] = "GenuntelineI";
125
126         if ((cpu_feature & CPUID_SSE2) == 0)
127                 return (rdtsc32_mb_none);
128         do_cpuid(0, p);
129         return (memcmp(p + 1, intel_id, sizeof(intel_id) - 1) == 0 ?
130             rdtsc32_mb_lfence : rdtsc32_mb_mfence);
131 }
132
133 #define HPET_DEV_MAP_MAX        10
134 static volatile char *hpet_dev_map[HPET_DEV_MAP_MAX];
135
136 static void
137 __vdso_init_hpet(uint32_t u)
138 {
139         static const char devprefix[] = "/dev/hpet";
140         char devname[64], *c, *c1, t;
141         volatile char *new_map, *old_map;
142         unsigned int mode;
143         uint32_t u1;
144         int fd;
145
146         c1 = c = stpcpy(devname, devprefix);
147         u1 = u;
148         do {
149                 *c++ = u1 % 10 + '0';
150                 u1 /= 10;
151         } while (u1 != 0);
152         *c = '\0';
153         for (c--; c1 != c; c1++, c--) {
154                 t = *c1;
155                 *c1 = *c;
156                 *c = t;
157         }
158
159         old_map = hpet_dev_map[u];
160         if (old_map != NULL)
161                 return;
162
163         /*
164          * Explicitely check for the capability mode to avoid
165          * triggering trap_enocap on the device open by absolute path.
166          */
167         if ((cap_getmode(&mode) == 0 && mode != 0) ||
168             (fd = _open(devname, O_RDONLY)) == -1) {
169                 /* Prevent the caller from re-entering. */
170                 atomic_cmpset_rel_ptr((volatile uintptr_t *)&hpet_dev_map[u],
171                     (uintptr_t)old_map, (uintptr_t)MAP_FAILED);
172                 return;
173         }
174
175         new_map = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
176         _close(fd);
177         if (atomic_cmpset_rel_ptr((volatile uintptr_t *)&hpet_dev_map[u],
178             (uintptr_t)old_map, (uintptr_t)new_map) == 0 &&
179             new_map != MAP_FAILED)
180                 munmap((void *)new_map, PAGE_SIZE);
181 }
182
183 #ifdef WANT_HYPERV
184
185 #define HYPERV_REFTSC_DEVPATH   "/dev/" HYPERV_REFTSC_DEVNAME
186
187 /*
188  * NOTE:
189  * We use 'NULL' for this variable to indicate that initialization
190  * is required.  And if this variable is 'MAP_FAILED', then Hyper-V
191  * reference TSC can not be used, e.g. in misconfigured jail.
192  */
193 static struct hyperv_reftsc *hyperv_ref_tsc;
194
195 static void
196 __vdso_init_hyperv_tsc(void)
197 {
198         int fd;
199         unsigned int mode;
200
201         if (cap_getmode(&mode) == 0 && mode != 0)
202                 goto fail;
203
204         fd = _open(HYPERV_REFTSC_DEVPATH, O_RDONLY);
205         if (fd < 0)
206                 goto fail;
207         hyperv_ref_tsc = mmap(NULL, sizeof(*hyperv_ref_tsc), PROT_READ,
208             MAP_SHARED, fd, 0);
209         _close(fd);
210
211         return;
212 fail:
213         /* Prevent the caller from re-entering. */
214         hyperv_ref_tsc = MAP_FAILED;
215 }
216
217 static int
218 __vdso_hyperv_tsc(struct hyperv_reftsc *tsc_ref, u_int *tc)
219 {
220         uint64_t disc, ret, tsc, scale;
221         uint32_t seq;
222         int64_t ofs;
223
224         while ((seq = atomic_load_acq_int(&tsc_ref->tsc_seq)) != 0) {
225                 scale = tsc_ref->tsc_scale;
226                 ofs = tsc_ref->tsc_ofs;
227
228                 mfence();       /* XXXKIB */
229                 tsc = rdtsc();
230
231                 /* ret = ((tsc * scale) >> 64) + ofs */
232                 __asm__ __volatile__ ("mulq %3" :
233                     "=d" (ret), "=a" (disc) :
234                     "a" (tsc), "r" (scale));
235                 ret += ofs;
236
237                 atomic_thread_fence_acq();
238                 if (tsc_ref->tsc_seq == seq) {
239                         *tc = ret;
240                         return (0);
241                 }
242
243                 /* Sequence changed; re-sync. */
244         }
245         return (ENOSYS);
246 }
247
248 #endif  /* WANT_HYPERV */
249
250 #pragma weak __vdso_gettc
251 int
252 __vdso_gettc(const struct vdso_timehands *th, u_int *tc)
253 {
254         volatile char *map;
255         uint32_t idx;
256
257         switch (th->th_algo) {
258         case VDSO_TH_ALGO_X86_TSC:
259                 *tc = th->th_x86_shift > 0 ? __vdso_gettc_rdtsc_low(th) :
260                     __vdso_gettc_rdtsc32();
261                 return (0);
262         case VDSO_TH_ALGO_X86_HPET:
263                 idx = th->th_x86_hpet_idx;
264                 if (idx >= HPET_DEV_MAP_MAX)
265                         return (ENOSYS);
266                 map = (volatile char *)atomic_load_acq_ptr(
267                     (volatile uintptr_t *)&hpet_dev_map[idx]);
268                 if (map == NULL) {
269                         __vdso_init_hpet(idx);
270                         map = (volatile char *)atomic_load_acq_ptr(
271                             (volatile uintptr_t *)&hpet_dev_map[idx]);
272                 }
273                 if (map == MAP_FAILED)
274                         return (ENOSYS);
275                 *tc = *(volatile uint32_t *)(map + HPET_MAIN_COUNTER);
276                 return (0);
277 #ifdef WANT_HYPERV
278         case VDSO_TH_ALGO_X86_HVTSC:
279                 if (hyperv_ref_tsc == NULL)
280                         __vdso_init_hyperv_tsc();
281                 if (hyperv_ref_tsc == MAP_FAILED)
282                         return (ENOSYS);
283                 return (__vdso_hyperv_tsc(hyperv_ref_tsc, tc));
284 #endif
285         default:
286                 return (ENOSYS);
287         }
288 }
289
290 #pragma weak __vdso_gettimekeep
291 int
292 __vdso_gettimekeep(struct vdso_timekeep **tk)
293 {
294
295         return (_elf_aux_info(AT_TIMEKEEP, tk, sizeof(*tk)));
296 }