• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <time.h>
2 #include <errno.h>
3 #include <stdint.h>
4 #include "syscall.h"
5 #include "atomic.h"
6 
7 #ifdef VDSO_CGT_SYM
8 
9 static void *volatile vdso_func;
10 
11 #ifdef VDSO_CGT32_SYM
12 static void *volatile vdso_func_32;
cgt_time32_wrap(clockid_t clk,struct timespec * ts)13 static int cgt_time32_wrap(clockid_t clk, struct timespec *ts)
14 {
15 	long ts32[2];
16 	int (*f)(clockid_t, long[2]) =
17 		(int (*)(clockid_t, long[2]))vdso_func_32;
18 	int r = f(clk, ts32);
19 	if (!r) {
20 		/* Fallback to syscalls if time32 overflowed. Maybe
21 		 * we lucked out and somehow migrated to a kernel with
22 		 * time64 syscalls available. */
23 		if (ts32[0] < 0) {
24 			a_cas_p(&vdso_func, (void *)cgt_time32_wrap, 0);
25 			return -ENOSYS;
26 		}
27 		ts->tv_sec = ts32[0];
28 		ts->tv_nsec = ts32[1];
29 	}
30 	return r;
31 }
32 #endif
33 
cgt_init(clockid_t clk,struct timespec * ts)34 static int cgt_init(clockid_t clk, struct timespec *ts)
35 {
36 #ifndef __LITEOS__
37 	__get_vdso_info();
38 	void *p = __get_vdso_addr(VDSO_CGT_VER, VDSO_CGT_SYM);
39 #else
40 	void *p = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM);
41 #endif
42 #ifdef VDSO_CGT32_SYM
43 	if (!p) {
44 #ifndef __LITEOS__
45 		void *q = __get_vdso_addr(VDSO_CGT32_VER, VDSO_CGT32_SYM);
46 #else
47 		void *q = __vdsosym(VDSO_CGT32_VER, VDSO_CGT32_SYM);
48 #endif
49 		if (q) {
50 			a_cas_p(&vdso_func_32, 0, q);
51 			p = cgt_time32_wrap;
52 		}
53 	}
54 #ifdef VDSO_CGT_WORKAROUND
55 #ifndef __LITEOS__
56 	if (!__get_vdso_addr(VDSO_CGT32_VER, VDSO_CGT32_SYM)) p = 0;
57 #else
58 	if (!__vdsosym(VDSO_CGT32_VER, VDSO_CGT32_SYM)) p = 0;
59 #endif
60 #endif
61 #endif
62 	int (*f)(clockid_t, struct timespec *) =
63 		(int (*)(clockid_t, struct timespec *))p;
64 	a_cas_p(&vdso_func, (void *)cgt_init, p);
65 	return f ? f(clk, ts) : -ENOSYS;
66 }
67 
68 static void *volatile vdso_func = (void *)cgt_init;
69 
70 #endif
71 
__clock_gettime(clockid_t clk,struct timespec * ts)72 int __clock_gettime(clockid_t clk, struct timespec *ts)
73 {
74 	int r;
75 
76 #ifdef VDSO_CGT_SYM
77 	int (*f)(clockid_t, struct timespec *) =
78 		(int (*)(clockid_t, struct timespec *))vdso_func;
79 	if (f) {
80 		r = f(clk, ts);
81 		if (!r) return r;
82 		if (r == -EINVAL) return __syscall_ret(r);
83 		/* Fall through on errors other than EINVAL. Some buggy
84 		 * vdso implementations return ENOSYS for clocks they
85 		 * can't handle, rather than making the syscall. This
86 		 * also handles the case where cgt_init fails to find
87 		 * a vdso function to use. */
88 	}
89 #endif
90 
91 #ifdef SYS_clock_gettime64
92 	r = -ENOSYS;
93 	if (sizeof(time_t) > 4)
94 		r = __syscall(SYS_clock_gettime64, clk, ts);
95 	if (SYS_clock_gettime == SYS_clock_gettime64 || r!=-ENOSYS)
96 		return __syscall_ret(r);
97 	long ts32[2];
98 	r = __syscall(SYS_clock_gettime, clk, ts32);
99 #ifdef SYS_gettimeofday
100 	if (r==-ENOSYS && clk==CLOCK_REALTIME) {
101 		r = __syscall(SYS_gettimeofday, ts32, 0);
102 		ts32[1] *= 1000;
103 	}
104 #endif
105 	if (!r) {
106 		ts->tv_sec = ts32[0];
107 		ts->tv_nsec = ts32[1];
108 		return r;
109 	}
110 	return __syscall_ret(r);
111 #else
112 	r = __syscall(SYS_clock_gettime, clk, ts);
113 #ifdef SYS_gettimeofday
114 	if (r == -ENOSYS) {
115 		if (clk == CLOCK_REALTIME) {
116 			__syscall(SYS_gettimeofday, ts, 0);
117 			ts->tv_nsec = (int)ts->tv_nsec * 1000;
118 			return 0;
119 		}
120 		r = -EINVAL;
121 	}
122 #endif
123 	return __syscall_ret(r);
124 #endif
125 }
126 
127 weak_alias(__clock_gettime, clock_gettime);
128