1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <time.h>
17 #include <sys/time.h>
18 #include <time.h>
19 #include <errno.h>
20 #include <stdint.h>
21 #include "syscall.h"
22 #include "atomic.h"
23
24 #ifdef VDSO_GTD_SYM
25
26 static void *volatile vdso_gtd;
27
gtd_init(struct timeval * tv,void * tz)28 static int gtd_init(struct timeval *tv, void *tz)
29 {
30 __get_vdso_info();
31 void *p = __get_vdso_addr(VDSO_GTD_VER, VDSO_GTD_SYM);
32 int (*f)(struct timeval *, void *) =
33 (int (*)(struct timval *, void *))p;
34 a_cas_p(&vdso_gtd, (void *)gtd_init, p);
35 return f ? f(tv, tz) : -ENOSYS;
36 }
37
38 static void *volatile vdso_gtd = (void *)gtd_init;
39
40 #endif
41
gettimeofday(struct timeval * restrict tv,void * restrict tz)42 int gettimeofday(struct timeval *restrict tv, void *restrict tz)
43 {
44 #ifdef VDSO_GTD_SYM
45 int r;
46 int (*f)(struct timeval *, void *) =
47 (int (*)(struct timeval *, void *))vdso_gtd;
48 if (f) {
49 r = f(tv, tz);
50 if (!r) return r;
51 if (r == -EINVAL) return __syscall_ret(r);
52 }
53 #endif
54
55 struct timespec ts;
56 if (!tv) return 0;
57 clock_gettime(CLOCK_REALTIME, &ts);
58 tv->tv_sec = ts.tv_sec;
59 tv->tv_usec = (int)ts.tv_nsec / 1000;
60 return 0;
61 }
62