1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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__FBSDID("$FreeBSD$");
30
31#include <sys/elf.h>
32#include <sys/time.h>
33#include <sys/vdso.h>
34#include <errno.h>
35#include <strings.h>
36#include <time.h>
37#include <machine/atomic.h>
38#include "libc_private.h"
39
40static int
41tc_delta(const struct vdso_timehands *th, u_int *delta)
42{
43	int error;
44	u_int tc;
45
46	error = __vdso_gettc(th, &tc);
47	if (error == 0)
48		*delta = (tc - th->th_offset_count) & th->th_counter_mask;
49	return (error);
50}
51
52/*
53 * Calculate the absolute or boot-relative time from the
54 * machine-specific fast timecounter and the published timehands
55 * structure read from the shared page.
56 *
57 * The lockless reading scheme is similar to the one used to read the
58 * in-kernel timehands, see sys/kern/kern_tc.c:binuptime().  This code
59 * is based on the kernel implementation.
60 */
61static int
62binuptime(struct bintime *bt, struct vdso_timekeep *tk, int abs)
63{
64	struct vdso_timehands *th;
65	uint32_t curr, gen;
66	uint64_t scale, x;
67	u_int delta, scale_bits;
68	int error;
69
70	do {
71		if (!tk->tk_enabled)
72			return (ENOSYS);
73
74		curr = atomic_load_acq_32(&tk->tk_current);
75		th = &tk->tk_th[curr];
76		gen = atomic_load_acq_32(&th->th_gen);
77		*bt = th->th_offset;
78		error = tc_delta(th, &delta);
79		if (error == EAGAIN)
80			continue;
81		if (error != 0)
82			return (error);
83		scale = th->th_scale;
84#ifdef _LP64
85		scale_bits = ffsl(scale);
86#else
87		scale_bits = ffsll(scale);
88#endif
89		if (__predict_false(scale_bits + fls(delta) > 63)) {
90			x = (scale >> 32) * delta;
91			scale &= 0xffffffff;
92			bt->sec += x >> 32;
93			bintime_addx(bt, x << 32);
94		}
95		bintime_addx(bt, scale * delta);
96		if (abs)
97			bintime_add(bt, &th->th_boottime);
98
99		/*
100		 * Ensure that the load of th_offset is completed
101		 * before the load of th_gen.
102		 */
103		atomic_thread_fence_acq();
104	} while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
105	return (0);
106}
107
108static struct vdso_timekeep *tk;
109
110#pragma weak __vdso_gettimeofday
111int
112__vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
113{
114	struct bintime bt;
115	int error;
116
117	if (tz != NULL)
118		return (ENOSYS);
119	if (tk == NULL) {
120		error = __vdso_gettimekeep(&tk);
121		if (error != 0 || tk == NULL)
122			return (ENOSYS);
123	}
124	if (tk->tk_ver != VDSO_TK_VER_CURR)
125		return (ENOSYS);
126	error = binuptime(&bt, tk, 1);
127	if (error != 0)
128		return (error);
129	bintime2timeval(&bt, tv);
130	return (0);
131}
132
133#pragma weak __vdso_clock_gettime
134int
135__vdso_clock_gettime(clockid_t clock_id, struct timespec *ts)
136{
137	struct bintime bt;
138	int abs, error;
139
140	if (tk == NULL) {
141		error = _elf_aux_info(AT_TIMEKEEP, &tk, sizeof(tk));
142		if (error != 0 || tk == NULL)
143			return (ENOSYS);
144	}
145	if (tk->tk_ver != VDSO_TK_VER_CURR)
146		return (ENOSYS);
147	switch (clock_id) {
148	case CLOCK_REALTIME:
149	case CLOCK_REALTIME_PRECISE:
150	case CLOCK_REALTIME_FAST:
151	case CLOCK_SECOND:
152		abs = 1;
153		break;
154	case CLOCK_MONOTONIC:
155	case CLOCK_MONOTONIC_PRECISE:
156	case CLOCK_MONOTONIC_FAST:
157	case CLOCK_UPTIME:
158	case CLOCK_UPTIME_PRECISE:
159	case CLOCK_UPTIME_FAST:
160		abs = 0;
161		break;
162	default:
163		return (ENOSYS);
164	}
165	error = binuptime(&bt, tk, abs);
166	if (error != 0)
167		return (error);
168	bintime2timespec(&bt, ts);
169	if (clock_id == CLOCK_SECOND)
170		ts->tv_nsec = 0;
171	return (0);
172}
173