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 <errno.h>
18 #include <stdint.h>
19 #include "syscall.h"
20 #include "atomic.h"
21
22 #ifdef VDSO_CGR_SYM
23
24 static void *volatile vdso_cgr;
25
cgr_init(clockid_t clk,struct timespec * ts)26 static int cgr_init(clockid_t clk, struct timespec *ts)
27 {
28 __get_vdso_info();
29 void *p = __get_vdso_addr(VDSO_CGR_VER, VDSO_CGR_SYM);
30 int (*f)(clockid_t, struct timespec *) =
31 (int (*)(clockid_t, struct timespec *))p;
32 a_cas_p(&vdso_cgr, (void *)cgr_init, p);
33 return f ? f(clk, ts) : -ENOSYS;
34 }
35
36 static void *volatile vdso_cgr = (void *)cgr_init;
37
38 #endif
39
clock_getres(clockid_t clk,struct timespec * ts)40 int clock_getres(clockid_t clk, struct timespec *ts)
41 {
42 int r;
43 #ifdef VDSO_CGR_SYM
44 int (*f)(clockid_t, struct timespec *) =
45 (int (*)(clockid_t, struct timespec *))vdso_cgr;
46 if (f) {
47 r = f(clk, ts);
48 if (!r) return r;
49 if (r == -EINVAL) return __syscall_ret(r);
50 }
51 #endif
52
53 #ifdef SYS_clock_getres_time64
54 /* On a 32-bit arch, use the old syscall if it exists. */
55 if (SYS_clock_getres != SYS_clock_getres_time64) {
56 long ts32[2];
57 r = __syscall(SYS_clock_getres, clk, ts32);
58 if (!r && ts) {
59 ts->tv_sec = ts32[0];
60 ts->tv_nsec = ts32[1];
61 }
62 return __syscall_ret(r);
63 }
64 #endif
65 /* If reaching this point, it's a 64-bit arch or time64-only
66 * 32-bit arch and we can get result directly into timespec. */
67 return syscall(SYS_clock_getres, clk, ts);
68 }
69