xref: /freebsd-13-stable/lib/libc/sys/__vdso_gettimeofday.c (revision 3d497e17ebd33fe0f58d773e35ab994d750258d6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/elf.h>
30 #include <sys/time.h>
31 #include <sys/vdso.h>
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <strings.h>
35 #include <time.h>
36 #include <machine/atomic.h>
37 #include "libc_private.h"
38 
39 static int
tc_delta(const struct vdso_timehands * th,u_int * delta)40 tc_delta(const struct vdso_timehands *th, u_int *delta)
41 {
42 	int error;
43 	u_int tc;
44 
45 	error = __vdso_gettc(th, &tc);
46 	if (error == 0)
47 		*delta = (tc - th->th_offset_count) & th->th_counter_mask;
48 	return (error);
49 }
50 
51 /*
52  * Calculate the absolute or boot-relative time from the
53  * machine-specific fast timecounter and the published timehands
54  * structure read from the shared page.
55  *
56  * The lockless reading scheme is similar to the one used to read the
57  * in-kernel timehands, see sys/kern/kern_tc.c:binuptime().  This code
58  * is based on the kernel implementation.
59  */
60 static int
binuptime(struct bintime * bt,struct vdso_timekeep * tk,bool abs)61 binuptime(struct bintime *bt, struct vdso_timekeep *tk, bool abs)
62 {
63 	struct vdso_timehands *th;
64 	uint32_t curr, gen;
65 	uint64_t scale, x;
66 	u_int delta, scale_bits;
67 	int error;
68 
69 	do {
70 		if (!tk->tk_enabled)
71 			return (ENOSYS);
72 
73 		curr = atomic_load_acq_32(&tk->tk_current);
74 		th = &tk->tk_th[curr];
75 		gen = atomic_load_acq_32(&th->th_gen);
76 		*bt = th->th_offset;
77 		error = tc_delta(th, &delta);
78 		if (error == EAGAIN)
79 			continue;
80 		if (error != 0)
81 			return (error);
82 		scale = th->th_scale;
83 #ifdef _LP64
84 		scale_bits = flsl(scale);
85 #else
86 		scale_bits = flsll(scale);
87 #endif
88 		if (__predict_false(scale_bits + fls(delta) > 63)) {
89 			x = (scale >> 32) * delta;
90 			scale &= 0xffffffff;
91 			bt->sec += x >> 32;
92 			bintime_addx(bt, x << 32);
93 		}
94 		bintime_addx(bt, scale * delta);
95 		if (abs)
96 			bintime_add(bt, &th->th_boottime);
97 
98 		/*
99 		 * Ensure that the load of th_offset is completed
100 		 * before the load of th_gen.
101 		 */
102 		atomic_thread_fence_acq();
103 	} while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
104 	return (0);
105 }
106 
107 static int
getnanouptime(struct bintime * bt,struct vdso_timekeep * tk)108 getnanouptime(struct bintime *bt, struct vdso_timekeep *tk)
109 {
110 	struct vdso_timehands *th;
111 	uint32_t curr, gen;
112 
113 	do {
114 		if (!tk->tk_enabled)
115 			return (ENOSYS);
116 
117 		curr = atomic_load_acq_32(&tk->tk_current);
118 		th = &tk->tk_th[curr];
119 		gen = atomic_load_acq_32(&th->th_gen);
120 		*bt = th->th_offset;
121 
122 		/*
123 		 * Ensure that the load of th_offset is completed
124 		 * before the load of th_gen.
125 		 */
126 		atomic_thread_fence_acq();
127 	} while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
128 	return (0);
129 }
130 
131 static struct vdso_timekeep *tk;
132 
133 #pragma weak __vdso_gettimeofday
134 int
__vdso_gettimeofday(struct timeval * tv,struct timezone * tz)135 __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
136 {
137 	struct bintime bt;
138 	int error;
139 
140 	if (tz != NULL)
141 		return (ENOSYS);
142 	if (tk == NULL) {
143 		error = __vdso_gettimekeep(&tk);
144 		if (error != 0 || tk == NULL)
145 			return (ENOSYS);
146 	}
147 	if (tk->tk_ver != VDSO_TK_VER_CURR)
148 		return (ENOSYS);
149 	error = binuptime(&bt, tk, true);
150 	if (error != 0)
151 		return (error);
152 	bintime2timeval(&bt, tv);
153 	return (0);
154 }
155 
156 #pragma weak __vdso_clock_gettime
157 int
__vdso_clock_gettime(clockid_t clock_id,struct timespec * ts)158 __vdso_clock_gettime(clockid_t clock_id, struct timespec *ts)
159 {
160 	struct bintime bt;
161 	int error;
162 
163 	if (tk == NULL) {
164 		error = _elf_aux_info(AT_TIMEKEEP, &tk, sizeof(tk));
165 		if (error != 0 || tk == NULL)
166 			return (ENOSYS);
167 	}
168 	if (tk->tk_ver != VDSO_TK_VER_CURR)
169 		return (ENOSYS);
170 	switch (clock_id) {
171 	case CLOCK_REALTIME:
172 	case CLOCK_REALTIME_PRECISE:
173 	case CLOCK_REALTIME_FAST:
174 	case CLOCK_SECOND:
175 		error = binuptime(&bt, tk, true);
176 		break;
177 	case CLOCK_MONOTONIC:
178 	case CLOCK_MONOTONIC_PRECISE:
179 	case CLOCK_UPTIME:
180 	case CLOCK_UPTIME_PRECISE:
181 		error = binuptime(&bt, tk, false);
182 		break;
183 	case CLOCK_MONOTONIC_FAST:
184 	case CLOCK_UPTIME_FAST:
185 		error = getnanouptime(&bt, tk);
186 		break;
187 	default:
188 		error = ENOSYS;
189 		break;
190 	}
191 	if (error != 0)
192 		return (error);
193 	bintime2timespec(&bt, ts);
194 	if (clock_id == CLOCK_SECOND)
195 		ts->tv_nsec = 0;
196 	return (0);
197 }
198