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