diff --git a/lib/libc/sys/__vdso_gettimeofday.c b/lib/libc/sys/__vdso_gettimeofday.c --- a/lib/libc/sys/__vdso_gettimeofday.c +++ b/lib/libc/sys/__vdso_gettimeofday.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -59,7 +60,7 @@ * is based on the kernel implementation. */ static int -binuptime(struct bintime *bt, struct vdso_timekeep *tk, int abs) +binuptime(struct bintime *bt, struct vdso_timekeep *tk, bool abs) { struct vdso_timehands *th; uint32_t curr, gen; @@ -105,6 +106,30 @@ return (0); } +static int +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 struct vdso_timekeep *tk; #pragma weak __vdso_gettimeofday @@ -135,7 +160,7 @@ __vdso_clock_gettime(clockid_t clock_id, struct timespec *ts) { struct bintime bt; - int abs, error; + int error; if (tk == NULL) { error = _elf_aux_info(AT_TIMEKEEP, &tk, sizeof(tk)); @@ -149,20 +174,22 @@ case CLOCK_REALTIME_PRECISE: case CLOCK_REALTIME_FAST: case CLOCK_SECOND: - abs = 1; + error = binuptime(&bt, tk, true); break; case CLOCK_MONOTONIC: case CLOCK_MONOTONIC_PRECISE: - case CLOCK_MONOTONIC_FAST: case CLOCK_UPTIME: case CLOCK_UPTIME_PRECISE: + error = binuptime(&bt, tk, false); + break; + case CLOCK_MONOTONIC_FAST: case CLOCK_UPTIME_FAST: - abs = 0; + error = getnanouptime(&bt, tk); break; default: - return (ENOSYS); + error = ENOSYS; + break; } - error = binuptime(&bt, tk, abs); if (error != 0) return (error); bintime2timespec(&bt, ts);