aboutsummaryrefslogtreecommitdiff
path: root/sys/compat/linux/linux_vdso_gtod.inc
blob: 8a3840edd87d745218a6bd012d4d2a2e71d2e3fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
 * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

static int
fls(int mask)
{

	if (mask == 0)
		return (0);
	return ((__builtin_clz(mask) ^ 0x1f) + 1);
}

#ifdef _LP64
static int
flsl(long mask)
{
	int bit;

	if (mask == 0)
		return (0);
	for (bit = 1; mask != 1; bit++)
		mask = (unsigned long)mask >> 1;
	return (bit);
}
#else
static int
flsll(long long mask)
{
	int bit;

	if (mask == 0)
		return (0);
	for (bit = 1; mask != 1; bit++)
		mask = (unsigned long long)mask >> 1;
	return (bit);
}
#endif

static int
__vdso_native_to_linux_timespec(struct l_timespec *lts,
    struct timespec *nts)
{

#ifdef COMPAT_LINUX32
	if (nts->tv_sec > INT_MAX || nts->tv_sec < INT_MIN)
		return (LINUX_EOVERFLOW);
#endif
	lts->tv_sec = nts->tv_sec;
	lts->tv_nsec = nts->tv_nsec;
	return (0);
}

static int
__vdso_native_to_linux_timeval(l_timeval *ltv,
    struct timeval *ntv)
{

#ifdef COMPAT_LINUX32
	if (ntv->tv_sec > INT_MAX || ntv->tv_sec < INT_MIN)
		return (LINUX_EOVERFLOW);
#endif
	ltv->tv_sec = ntv->tv_sec;
	ltv->tv_usec = ntv->tv_usec;
	return (0);
}


#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
static int
__vdso_native_to_linux_timespec64(struct l_timespec64 *lts,
    struct timespec *nts)
{

	lts->tv_sec = nts->tv_sec;
	lts->tv_nsec = nts->tv_nsec;
	return (0);
}
#endif

static int
__vdso_linux_to_native_clockid(clockid_t *n, clockid_t l)
{

	switch (l) {
	case LINUX_CLOCK_REALTIME:
		*n = CLOCK_REALTIME;
		break;
	case LINUX_CLOCK_MONOTONIC:
		*n = CLOCK_MONOTONIC;
		break;
	case LINUX_CLOCK_REALTIME_COARSE:
		*n = CLOCK_REALTIME_FAST;
		break;
	case LINUX_CLOCK_MONOTONIC_COARSE:
	case LINUX_CLOCK_MONOTONIC_RAW:
		*n = CLOCK_MONOTONIC_FAST;
		break;
	case LINUX_CLOCK_BOOTTIME:
		*n = CLOCK_UPTIME;
		break;
	default:
		return (LINUX_EINVAL);
	}
	return (0);
}

/*
 * The code below adapted from
 * lib/libc/sys/__vdso_gettimeofday.c
 */

static inline void
__vdso_gettimekeep(struct vdso_timekeep **tk)
{

	*tk = (struct vdso_timekeep *)kern_timekeep_base;
}

static int
tc_delta(const struct vdso_timehands *th, u_int *delta)
{
	int error;
	u_int tc;

	error = __vdso_gettc(th, &tc);
	if (error == 0)
		*delta = (tc - th->th_offset_count) & th->th_counter_mask;
	return (error);
}

/*
 * Calculate the absolute or boot-relative time from the
 * machine-specific fast timecounter and the published timehands
 * structure read from the shared page.
 *
 * The lockless reading scheme is similar to the one used to read the
 * in-kernel timehands, see sys/kern/kern_tc.c:binuptime().  This code
 * is based on the kernel implementation.
 */
static int
freebsd_binuptime(struct bintime *bt, struct vdso_timekeep *tk, bool abs)
{
	struct vdso_timehands *th;
	uint32_t curr, gen;
	uint64_t scale, x;
	u_int delta, scale_bits;
	int error;

	do {
		if (!tk->tk_enabled)
			return (ENOSYS);

		curr = atomic_load_acq_32(&tk->tk_current);
		th = &tk->tk_th[curr];
		gen = atomic_load_acq_32(&th->th_gen);
		*bt = th->th_offset;
		error = tc_delta(th, &delta);
		if (error == EAGAIN)
			continue;
		if (error != 0)
			return (error);
		scale = th->th_scale;
#ifdef _LP64
		scale_bits = flsl(scale);
#else
		scale_bits = flsll(scale);
#endif
		if (__predict_false(scale_bits + fls(delta) > 63)) {
			x = (scale >> 32) * delta;
			scale &= 0xffffffff;
			bt->sec += x >> 32;
			bintime_addx(bt, x << 32);
		}
		bintime_addx(bt, scale * delta);
		if (abs)
			bintime_add(bt, &th->th_boottime);

		/*
		 * Ensure that the load of th_offset is completed
		 * before the load of th_gen.
		 */
		atomic_thread_fence_acq();
	} while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
	return (0);
}

static int
freebsd_getnanouptime(struct bintime *bt, struct vdso_timekeep *tk)
{
	struct vdso_timehands *th;
	uint32_t curr, gen;

	do {
		if (!tk->tk_enabled)
			return (ENOSYS);

		curr = atomic_load_acq_32(&tk->tk_current);
		th = &tk->tk_th[curr];
		gen = atomic_load_acq_32(&th->th_gen);
		*bt = th->th_offset;

		/*
		 * Ensure that the load of th_offset is completed
		 * before the load of th_gen.
		 */
		atomic_thread_fence_acq();
	} while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
	return (0);
}

static int
freebsd_gettimeofday(struct timeval *tv, struct timezone *tz)
{
	struct vdso_timekeep *tk;
	struct bintime bt;
	int error;

	if (tz != NULL)
		return (ENOSYS);
	__vdso_gettimekeep(&tk);
	if (tk == NULL)
		return (ENOSYS);
	if (tk->tk_ver != VDSO_TK_VER_CURR)
		return (ENOSYS);
	error = freebsd_binuptime(&bt, tk, true);
	if (error == 0)
		bintime2timeval(&bt, tv);
	return (error);
}

static int
freebsd_clock_gettime(clockid_t clock_id, struct timespec *ts)
{
	struct vdso_timekeep *tk;
	struct bintime bt;
	int error;

	__vdso_gettimekeep(&tk);
	if (tk == NULL)
		return (ENOSYS);
	if (tk->tk_ver != VDSO_TK_VER_CURR)
		return (ENOSYS);
	switch (clock_id) {
	case CLOCK_REALTIME:
	case CLOCK_REALTIME_PRECISE:
	case CLOCK_REALTIME_FAST:
		error = freebsd_binuptime(&bt, tk, true);
		break;
	case CLOCK_MONOTONIC:
	case CLOCK_MONOTONIC_PRECISE:
	case CLOCK_UPTIME:
	case CLOCK_UPTIME_PRECISE:
		error = freebsd_binuptime(&bt, tk, false);
		break;
	case CLOCK_MONOTONIC_FAST:
	case CLOCK_UPTIME_FAST:
		error = freebsd_getnanouptime(&bt, tk);
		break;
	default:
		error = ENOSYS;
		break;
	}
	if (error == 0)
		bintime2timespec(&bt, ts);
	return (error);
}

/*
 * Linux vDSO interfaces
 *
 */
int
__vdso_clock_gettime(clockid_t clock_id, struct l_timespec *lts)
{
	struct timespec ts;
	clockid_t which;
	int error;

	error = __vdso_linux_to_native_clockid(&which, clock_id);
	if (error != 0)
		return (__vdso_clock_gettime_fallback(clock_id, lts));
	error = freebsd_clock_gettime(which, &ts);
	if (error == 0)
		return (-__vdso_native_to_linux_timespec(lts, &ts));
	else
		return (__vdso_clock_gettime_fallback(clock_id, lts));
}

int
__vdso_gettimeofday(l_timeval *ltv, struct timezone *tz)
{
	struct timeval tv;
	int error;

	error = freebsd_gettimeofday(&tv, tz);
	if (error != 0)
		return (__vdso_gettimeofday_fallback(ltv, tz));
	return (-__vdso_native_to_linux_timeval(ltv, &tv));
}

int
__vdso_clock_getres(clockid_t clock_id, struct l_timespec *lts)
{

	return (__vdso_clock_getres_fallback(clock_id, lts));
}

#if defined(__i386__) || defined(COMPAT_LINUX32)
int
__vdso_clock_gettime64(clockid_t clock_id, struct l_timespec64 *lts)
{
	struct timespec ts;
	clockid_t which;
	int error;

	error = __vdso_linux_to_native_clockid(&which, clock_id);
	if (error != 0)
		return (__vdso_clock_gettime64_fallback(clock_id, lts));
	error = freebsd_clock_gettime(which, &ts);
	if (error == 0)
		return(-__vdso_native_to_linux_timespec64(lts, &ts));
	else
		return(__vdso_clock_gettime64_fallback(clock_id, lts));
}

int clock_gettime64(clockid_t clock_id, struct l_timespec64 *lts)
    __attribute__((weak, alias("__vdso_clock_gettime64")));
#endif

#if defined(__i386__) || defined(__amd64__)
int
__vdso_getcpu(uint32_t *cpu, uint32_t *node, void *cache)
{
	int ret;

	if (node != NULL)
		return (__vdso_getcpu_fallback(cpu, node, cache));
	ret = __vdso_getcpu_try();
	if (ret < 0)
		return (__vdso_getcpu_fallback(cpu, node, cache));
	*cpu = ret;
	return (0);
}
#endif

#if defined(__i386__) || defined(__amd64__)
int
__vdso_time(long *tm)
{
	struct timeval tv;
	int error;

	error = freebsd_gettimeofday(&tv, NULL);
	if (error != 0)
		return (__vdso_time_fallback(tm));
	if (tm != NULL)
		*tm = tv.tv_sec;
	return (tv.tv_sec);
}
#endif