]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/__vdso_gettimeofday.c
Merge compiler-rt trunk r366426, resolve conflicts, and add
[FreeBSD/FreeBSD.git] / lib / libc / sys / __vdso_gettimeofday.c
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 <time.h>
36 #include <machine/atomic.h>
37 #include "libc_private.h"
38
39 static int
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
61 binuptime(struct bintime *bt, struct vdso_timekeep *tk, int abs)
62 {
63         struct vdso_timehands *th;
64         uint32_t curr, gen;
65         u_int delta;
66         int error;
67
68         do {
69                 if (!tk->tk_enabled)
70                         return (ENOSYS);
71
72                 curr = atomic_load_acq_32(&tk->tk_current);
73                 th = &tk->tk_th[curr];
74                 gen = atomic_load_acq_32(&th->th_gen);
75                 *bt = th->th_offset;
76                 error = tc_delta(th, &delta);
77                 if (error == EAGAIN)
78                         continue;
79                 if (error != 0)
80                         return (error);
81                 bintime_addx(bt, th->th_scale * delta);
82                 if (abs)
83                         bintime_add(bt, &th->th_boottime);
84
85                 /*
86                  * Ensure that the load of th_offset is completed
87                  * before the load of th_gen.
88                  */
89                 atomic_thread_fence_acq();
90         } while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
91         return (0);
92 }
93
94 static struct vdso_timekeep *tk;
95
96 #pragma weak __vdso_gettimeofday
97 int
98 __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
99 {
100         struct bintime bt;
101         int error;
102
103         if (tz != NULL)
104                 return (ENOSYS);
105         if (tk == NULL) {
106                 error = __vdso_gettimekeep(&tk);
107                 if (error != 0 || tk == NULL)
108                         return (ENOSYS);
109         }
110         if (tk->tk_ver != VDSO_TK_VER_CURR)
111                 return (ENOSYS);
112         error = binuptime(&bt, tk, 1);
113         if (error != 0)
114                 return (error);
115         bintime2timeval(&bt, tv);
116         return (0);
117 }
118
119 #pragma weak __vdso_clock_gettime
120 int
121 __vdso_clock_gettime(clockid_t clock_id, struct timespec *ts)
122 {
123         struct bintime bt;
124         int abs, error;
125
126         if (tk == NULL) {
127                 error = _elf_aux_info(AT_TIMEKEEP, &tk, sizeof(tk));
128                 if (error != 0 || tk == NULL)
129                         return (ENOSYS);
130         }
131         if (tk->tk_ver != VDSO_TK_VER_CURR)
132                 return (ENOSYS);
133         switch (clock_id) {
134         case CLOCK_REALTIME:
135         case CLOCK_REALTIME_PRECISE:
136         case CLOCK_REALTIME_FAST:
137         case CLOCK_SECOND:
138                 abs = 1;
139                 break;
140         case CLOCK_MONOTONIC:
141         case CLOCK_MONOTONIC_PRECISE:
142         case CLOCK_MONOTONIC_FAST:
143         case CLOCK_UPTIME:
144         case CLOCK_UPTIME_PRECISE:
145         case CLOCK_UPTIME_FAST:
146                 abs = 0;
147                 break;
148         default:
149                 return (ENOSYS);
150         }
151         error = binuptime(&bt, tk, abs);
152         if (error != 0)
153                 return (error);
154         bintime2timespec(&bt, ts);
155         if (clock_id == CLOCK_SECOND)
156                 ts->tv_nsec = 0;
157         return (0);
158 }