]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/__vdso_gettimeofday.c
sockets: add MSG_TRUNC flag handling for recvfrom()/recvmsg().
[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 <stdbool.h>
36 #include <strings.h>
37 #include <time.h>
38 #include <machine/atomic.h>
39 #include "libc_private.h"
40
41 static int
42 tc_delta(const struct vdso_timehands *th, u_int *delta)
43 {
44         int error;
45         u_int tc;
46
47         error = __vdso_gettc(th, &tc);
48         if (error == 0)
49                 *delta = (tc - th->th_offset_count) & th->th_counter_mask;
50         return (error);
51 }
52
53 /*
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.
57  *
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.
61  */
62 static int
63 binuptime(struct bintime *bt, struct vdso_timekeep *tk, bool abs)
64 {
65         struct vdso_timehands *th;
66         uint32_t curr, gen;
67         uint64_t scale, x;
68         u_int delta, scale_bits;
69         int error;
70
71         do {
72                 if (!tk->tk_enabled)
73                         return (ENOSYS);
74
75                 curr = atomic_load_acq_32(&tk->tk_current);
76                 th = &tk->tk_th[curr];
77                 gen = atomic_load_acq_32(&th->th_gen);
78                 *bt = th->th_offset;
79                 error = tc_delta(th, &delta);
80                 if (error == EAGAIN)
81                         continue;
82                 if (error != 0)
83                         return (error);
84                 scale = th->th_scale;
85 #ifdef _LP64
86                 scale_bits = flsl(scale);
87 #else
88                 scale_bits = flsll(scale);
89 #endif
90                 if (__predict_false(scale_bits + fls(delta) > 63)) {
91                         x = (scale >> 32) * delta;
92                         scale &= 0xffffffff;
93                         bt->sec += x >> 32;
94                         bintime_addx(bt, x << 32);
95                 }
96                 bintime_addx(bt, scale * delta);
97                 if (abs)
98                         bintime_add(bt, &th->th_boottime);
99
100                 /*
101                  * Ensure that the load of th_offset is completed
102                  * before the load of th_gen.
103                  */
104                 atomic_thread_fence_acq();
105         } while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
106         return (0);
107 }
108
109 static int
110 getnanouptime(struct bintime *bt, struct vdso_timekeep *tk)
111 {
112         struct vdso_timehands *th;
113         uint32_t curr, gen;
114
115         do {
116                 if (!tk->tk_enabled)
117                         return (ENOSYS);
118
119                 curr = atomic_load_acq_32(&tk->tk_current);
120                 th = &tk->tk_th[curr];
121                 gen = atomic_load_acq_32(&th->th_gen);
122                 *bt = th->th_offset;
123
124                 /*
125                  * Ensure that the load of th_offset is completed
126                  * before the load of th_gen.
127                  */
128                 atomic_thread_fence_acq();
129         } while (curr != tk->tk_current || gen == 0 || gen != th->th_gen);
130         return (0);
131 }
132
133 static struct vdso_timekeep *tk;
134
135 #pragma weak __vdso_gettimeofday
136 int
137 __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
138 {
139         struct bintime bt;
140         int error;
141
142         if (tz != NULL)
143                 return (ENOSYS);
144         if (tk == NULL) {
145                 error = __vdso_gettimekeep(&tk);
146                 if (error != 0 || tk == NULL)
147                         return (ENOSYS);
148         }
149         if (tk->tk_ver != VDSO_TK_VER_CURR)
150                 return (ENOSYS);
151         error = binuptime(&bt, tk, true);
152         if (error != 0)
153                 return (error);
154         bintime2timeval(&bt, tv);
155         return (0);
156 }
157
158 #pragma weak __vdso_clock_gettime
159 int
160 __vdso_clock_gettime(clockid_t clock_id, struct timespec *ts)
161 {
162         struct bintime bt;
163         int error;
164
165         if (tk == NULL) {
166                 error = _elf_aux_info(AT_TIMEKEEP, &tk, sizeof(tk));
167                 if (error != 0 || tk == NULL)
168                         return (ENOSYS);
169         }
170         if (tk->tk_ver != VDSO_TK_VER_CURR)
171                 return (ENOSYS);
172         switch (clock_id) {
173         case CLOCK_REALTIME:
174         case CLOCK_REALTIME_PRECISE:
175         case CLOCK_REALTIME_FAST:
176         case CLOCK_SECOND:
177                 error = binuptime(&bt, tk, true);
178                 break;
179         case CLOCK_MONOTONIC:
180         case CLOCK_MONOTONIC_PRECISE:
181         case CLOCK_UPTIME:
182         case CLOCK_UPTIME_PRECISE:
183                 error = binuptime(&bt, tk, false);
184                 break;
185         case CLOCK_MONOTONIC_FAST:
186         case CLOCK_UPTIME_FAST:
187                 error = getnanouptime(&bt, tk);
188                 break;
189         default:
190                 error = ENOSYS;
191                 break;
192         }
193         if (error != 0)
194                 return (error);
195         bintime2timespec(&bt, ts);
196         if (clock_id == CLOCK_SECOND)
197                 ts->tv_nsec = 0;
198         return (0);
199 }