| 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; |
| 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 | |
| 34 | static int cgt_init(clockid_t clk, struct timespec *ts) |
| 35 | { |
| 36 | 	void *p = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM); |
| 37 | #ifdef VDSO_CGT32_SYM |
| 38 | 	if (!p) { |
| 39 | 		void *q = __vdsosym(VDSO_CGT32_VER, VDSO_CGT32_SYM); |
| 40 | 		if (q) { |
| 41 | 			a_cas_p(&vdso_func_32, 0, q); |
| 42 | 			p = cgt_time32_wrap; |
| 43 | 		} |
| 44 | 	} |
| 45 | #ifdef VDSO_CGT_WORKAROUND |
| 46 | 	if (!__vdsosym(VDSO_CGT32_VER, VDSO_CGT32_SYM)) p = 0; |
| 47 | #endif |
| 48 | #endif |
| 49 | 	int (*f)(clockid_t, struct timespec *) = |
| 50 | 		(int (*)(clockid_t, struct timespec *))p; |
| 51 | 	a_cas_p(&vdso_func, (void *)cgt_init, p); |
| 52 | 	return f ? f(clk, ts) : -ENOSYS; |
| 53 | } |
| 54 | |
| 55 | static void *volatile vdso_func = (void *)cgt_init; |
| 56 | |
| 57 | #endif |
| 58 | |
| 59 | int __clock_gettime(clockid_t clk, struct timespec *ts) |
| 60 | { |
| 61 | 	int r; |
| 62 | |
| 63 | #ifdef VDSO_CGT_SYM |
| 64 | 	int (*f)(clockid_t, struct timespec *) = |
| 65 | 		(int (*)(clockid_t, struct timespec *))vdso_func; |
| 66 | 	if (f) { |
| 67 | 		r = f(clk, ts); |
| 68 | 		if (!r) return r; |
| 69 | 		if (r == -EINVAL) return __syscall_ret(r); |
| 70 | 		/* Fall through on errors other than EINVAL. Some buggy |
| 71 | 		 * vdso implementations return ENOSYS for clocks they |
| 72 | 		 * can't handle, rather than making the syscall. This |
| 73 | 		 * also handles the case where cgt_init fails to find |
| 74 | 		 * a vdso function to use. */ |
| 75 | 	} |
| 76 | #endif |
| 77 | |
| 78 | #ifdef SYS_clock_gettime64 |
| 79 | 	r = -ENOSYS; |
| 80 | 	if (sizeof(time_t) > 4) |
| 81 | 		r = __syscall(SYS_clock_gettime64, clk, ts); |
| 82 | 	if (SYS_clock_gettime == SYS_clock_gettime64 || r!=-ENOSYS) |
| 83 | 		return __syscall_ret(r); |
| 84 | 	long ts32[2]; |
| 85 | 	r = __syscall(SYS_clock_gettime, clk, ts32); |
| 86 | #ifdef SYS_gettimeofday |
| 87 | 	if (r==-ENOSYS && clk==CLOCK_REALTIME) { |
| 88 | 		r = __syscall(SYS_gettimeofday, ts32, 0); |
| 89 | 		ts32[1] *= 1000; |
| 90 | 	} |
| 91 | #endif |
| 92 | 	if (!r) { |
| 93 | 		ts->tv_sec = ts32[0]; |
| 94 | 		ts->tv_nsec = ts32[1]; |
| 95 | 		return r; |
| 96 | 	} |
| 97 | 	return __syscall_ret(r); |
| 98 | #else |
| 99 | 	r = __syscall(SYS_clock_gettime, clk, ts); |
| 100 | #ifdef SYS_gettimeofday |
| 101 | 	if (r == -ENOSYS) { |
| 102 | 		if (clk == CLOCK_REALTIME) { |
| 103 | 			__syscall(SYS_gettimeofday, ts, 0); |
| 104 | 			ts->tv_nsec = (int)ts->tv_nsec * 1000; |
| 105 | 			return 0; |
| 106 | 		} |
| 107 | 		r = -EINVAL; |
| 108 | 	} |
| 109 | #endif |
| 110 | 	return __syscall_ret(r); |
| 111 | #endif |
| 112 | } |
| 113 | |
| 114 | weak_alias(__clock_gettime, clock_gettime); |