2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
38 #include <machine/atomic.h>
39 #include "libc_private.h"
42 tc_delta(const struct vdso_timehands *th, u_int *delta)
47 error = __vdso_gettc(th, &tc);
49 *delta = (tc - th->th_offset_count) & th->th_counter_mask;
54 * Calculate the absolute or boot-relative time from the
55 * machine-specific fast timecounter and the published timehands
56 * structure read from the shared page.
58 * The lockless reading scheme is similar to the one used to read the
59 * in-kernel timehands, see sys/kern/kern_tc.c:binuptime(). This code
60 * is based on the kernel implementation.
63 binuptime(struct bintime *bt, struct vdso_timekeep *tk, bool abs)
65 struct vdso_timehands *th;
68 u_int delta, scale_bits;
75 curr = atomic_load_acq_32(&tk->tk_current);
76 th = &tk->tk_th[curr];
77 gen = atomic_load_acq_32(&th->th_gen);
79 error = tc_delta(th, &delta);
86 scale_bits = flsl(scale);
88 scale_bits = flsll(scale);
90 if (__predict_false(scale_bits + fls(delta) > 63)) {
91 x = (scale >> 32) * delta;
94 bintime_addx(bt, x << 32);
96 bintime_addx(bt, scale * delta);
98 bintime_add(bt, &th->th_boottime);
101 * Ensure that the load of th_offset is completed
102 * before the load of th_gen.
104 atomic_thread_fence_acq();
105 } while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
110 getnanouptime(struct bintime *bt, struct vdso_timekeep *tk)
112 struct vdso_timehands *th;
119 curr = atomic_load_acq_32(&tk->tk_current);
120 th = &tk->tk_th[curr];
121 gen = atomic_load_acq_32(&th->th_gen);
125 * Ensure that the load of th_offset is completed
126 * before the load of th_gen.
128 atomic_thread_fence_acq();
129 } while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
133 static struct vdso_timekeep *tk;
135 #pragma weak __vdso_gettimeofday
137 __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
145 error = __vdso_gettimekeep(&tk);
146 if (error != 0 || tk == NULL)
149 if (tk->tk_ver != VDSO_TK_VER_CURR)
151 error = binuptime(&bt, tk, true);
154 bintime2timeval(&bt, tv);
158 #pragma weak __vdso_clock_gettime
160 __vdso_clock_gettime(clockid_t clock_id, struct timespec *ts)
166 error = _elf_aux_info(AT_TIMEKEEP, &tk, sizeof(tk));
167 if (error != 0 || tk == NULL)
170 if (tk->tk_ver != VDSO_TK_VER_CURR)
174 case CLOCK_REALTIME_PRECISE:
175 case CLOCK_REALTIME_FAST:
177 error = binuptime(&bt, tk, true);
179 case CLOCK_MONOTONIC:
180 case CLOCK_MONOTONIC_PRECISE:
182 case CLOCK_UPTIME_PRECISE:
183 error = binuptime(&bt, tk, false);
185 case CLOCK_MONOTONIC_FAST:
186 case CLOCK_UPTIME_FAST:
187 error = getnanouptime(&bt, tk);
195 bintime2timespec(&bt, ts);
196 if (clock_id == CLOCK_SECOND)