xref: /openbmc/linux/arch/sparc/vdso/vclock_gettime.c (revision 82210fc7)
17e300dabSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29a08862aSNagarathnam Muthusamy /*
39a08862aSNagarathnam Muthusamy  * Copyright 2006 Andi Kleen, SUSE Labs.
49a08862aSNagarathnam Muthusamy  *
59a08862aSNagarathnam Muthusamy  * Fast user context implementation of clock_gettime, gettimeofday, and time.
69a08862aSNagarathnam Muthusamy  *
79a08862aSNagarathnam Muthusamy  * The code should have no internal unresolved relocations.
89a08862aSNagarathnam Muthusamy  * Check with readelf after changing.
99a08862aSNagarathnam Muthusamy  * Also alternative() doesn't work.
109a08862aSNagarathnam Muthusamy  */
119a08862aSNagarathnam Muthusamy /*
129a08862aSNagarathnam Muthusamy  * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
139a08862aSNagarathnam Muthusamy  */
149a08862aSNagarathnam Muthusamy 
159a08862aSNagarathnam Muthusamy #include <linux/kernel.h>
169a08862aSNagarathnam Muthusamy #include <linux/time.h>
179a08862aSNagarathnam Muthusamy #include <linux/string.h>
189a08862aSNagarathnam Muthusamy #include <asm/io.h>
199a08862aSNagarathnam Muthusamy #include <asm/unistd.h>
209a08862aSNagarathnam Muthusamy #include <asm/timex.h>
219a08862aSNagarathnam Muthusamy #include <asm/clocksource.h>
229a08862aSNagarathnam Muthusamy #include <asm/vvar.h>
239a08862aSNagarathnam Muthusamy 
24776ca154SDavid S. Miller #ifdef	CONFIG_SPARC64
259a08862aSNagarathnam Muthusamy #define SYSCALL_STRING							\
269a08862aSNagarathnam Muthusamy 	"ta	0x6d;"							\
27776ca154SDavid S. Miller 	"bcs,a	1f;"							\
289a08862aSNagarathnam Muthusamy 	" sub	%%g0, %%o0, %%o0;"					\
29776ca154SDavid S. Miller 	"1:"
30776ca154SDavid S. Miller #else
31776ca154SDavid S. Miller #define SYSCALL_STRING							\
32776ca154SDavid S. Miller 	"ta	0x10;"							\
33776ca154SDavid S. Miller 	"bcs,a	1f;"							\
34776ca154SDavid S. Miller 	" sub	%%g0, %%o0, %%o0;"					\
35776ca154SDavid S. Miller 	"1:"
36776ca154SDavid S. Miller #endif
379a08862aSNagarathnam Muthusamy 
389a08862aSNagarathnam Muthusamy #define SYSCALL_CLOBBERS						\
399a08862aSNagarathnam Muthusamy 	"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",			\
409a08862aSNagarathnam Muthusamy 	"f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",		\
419a08862aSNagarathnam Muthusamy 	"f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",		\
429a08862aSNagarathnam Muthusamy 	"f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",		\
439a08862aSNagarathnam Muthusamy 	"f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46",		\
449a08862aSNagarathnam Muthusamy 	"f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62",		\
459a08862aSNagarathnam Muthusamy 	"cc", "memory"
469a08862aSNagarathnam Muthusamy 
479a08862aSNagarathnam Muthusamy /*
489a08862aSNagarathnam Muthusamy  * Compute the vvar page's address in the process address space, and return it
499a08862aSNagarathnam Muthusamy  * as a pointer to the vvar_data.
509a08862aSNagarathnam Muthusamy  */
get_vvar_data(void)51794b88e0SDavid S. Miller notrace static __always_inline struct vvar_data *get_vvar_data(void)
529a08862aSNagarathnam Muthusamy {
539a08862aSNagarathnam Muthusamy 	unsigned long ret;
549a08862aSNagarathnam Muthusamy 
559a08862aSNagarathnam Muthusamy 	/*
56794b88e0SDavid S. Miller 	 * vdso data page is the first vDSO page so grab the PC
579a08862aSNagarathnam Muthusamy 	 * and move up a page to get to the data page.
589a08862aSNagarathnam Muthusamy 	 */
59794b88e0SDavid S. Miller 	__asm__("rd %%pc, %0" : "=r" (ret));
609a08862aSNagarathnam Muthusamy 	ret &= ~(8192 - 1);
619a08862aSNagarathnam Muthusamy 	ret -= 8192;
629a08862aSNagarathnam Muthusamy 
639a08862aSNagarathnam Muthusamy 	return (struct vvar_data *) ret;
649a08862aSNagarathnam Muthusamy }
659a08862aSNagarathnam Muthusamy 
vdso_fallback_gettime(long clock,struct __kernel_old_timespec * ts)6682210fc7SArnd Bergmann notrace static long vdso_fallback_gettime(long clock, struct __kernel_old_timespec *ts)
679a08862aSNagarathnam Muthusamy {
689a08862aSNagarathnam Muthusamy 	register long num __asm__("g1") = __NR_clock_gettime;
699a08862aSNagarathnam Muthusamy 	register long o0 __asm__("o0") = clock;
709a08862aSNagarathnam Muthusamy 	register long o1 __asm__("o1") = (long) ts;
719a08862aSNagarathnam Muthusamy 
729a08862aSNagarathnam Muthusamy 	__asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
739a08862aSNagarathnam Muthusamy 			     "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
749a08862aSNagarathnam Muthusamy 	return o0;
759a08862aSNagarathnam Muthusamy }
769a08862aSNagarathnam Muthusamy 
vdso_fallback_gettimeofday(struct __kernel_old_timeval * tv,struct timezone * tz)77ddccf40fSArnd Bergmann notrace static long vdso_fallback_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
789a08862aSNagarathnam Muthusamy {
799a08862aSNagarathnam Muthusamy 	register long num __asm__("g1") = __NR_gettimeofday;
809a08862aSNagarathnam Muthusamy 	register long o0 __asm__("o0") = (long) tv;
819a08862aSNagarathnam Muthusamy 	register long o1 __asm__("o1") = (long) tz;
829a08862aSNagarathnam Muthusamy 
839a08862aSNagarathnam Muthusamy 	__asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
849a08862aSNagarathnam Muthusamy 			     "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
859a08862aSNagarathnam Muthusamy 	return o0;
869a08862aSNagarathnam Muthusamy }
879a08862aSNagarathnam Muthusamy 
889a08862aSNagarathnam Muthusamy #ifdef	CONFIG_SPARC64
vread_tick(void)89794b88e0SDavid S. Miller notrace static __always_inline u64 vread_tick(void)
90794b88e0SDavid S. Miller {
919a08862aSNagarathnam Muthusamy 	u64	ret;
929a08862aSNagarathnam Muthusamy 
93caf539cdSDavid S. Miller 	__asm__ __volatile__("rd %%tick, %0" : "=r" (ret));
94caf539cdSDavid S. Miller 	return ret;
95caf539cdSDavid S. Miller }
96caf539cdSDavid S. Miller 
vread_tick_stick(void)97caf539cdSDavid S. Miller notrace static __always_inline u64 vread_tick_stick(void)
98caf539cdSDavid S. Miller {
99caf539cdSDavid S. Miller 	u64	ret;
100caf539cdSDavid S. Miller 
101caf539cdSDavid S. Miller 	__asm__ __volatile__("rd %%asr24, %0" : "=r" (ret));
1023fe5d7e8SDavid S. Miller 	return ret;
1039a08862aSNagarathnam Muthusamy }
1049a08862aSNagarathnam Muthusamy #else
vread_tick(void)105794b88e0SDavid S. Miller notrace static __always_inline u64 vread_tick(void)
1069a08862aSNagarathnam Muthusamy {
1072f6c9bf3SDavid S. Miller 	register unsigned long long ret asm("o4");
1089a08862aSNagarathnam Muthusamy 
109caf539cdSDavid S. Miller 	__asm__ __volatile__("rd %%tick, %L0\n\t"
110caf539cdSDavid S. Miller 			     "srlx %L0, 32, %H0"
111caf539cdSDavid S. Miller 			     : "=r" (ret));
112caf539cdSDavid S. Miller 	return ret;
113caf539cdSDavid S. Miller }
114caf539cdSDavid S. Miller 
vread_tick_stick(void)115caf539cdSDavid S. Miller notrace static __always_inline u64 vread_tick_stick(void)
116caf539cdSDavid S. Miller {
117caf539cdSDavid S. Miller 	register unsigned long long ret asm("o4");
118caf539cdSDavid S. Miller 
119caf539cdSDavid S. Miller 	__asm__ __volatile__("rd %%asr24, %L0\n\t"
120caf539cdSDavid S. Miller 			     "srlx %L0, 32, %H0"
1212f6c9bf3SDavid S. Miller 			     : "=r" (ret));
1222f6c9bf3SDavid S. Miller 	return ret;
1239a08862aSNagarathnam Muthusamy }
1249a08862aSNagarathnam Muthusamy #endif
1259a08862aSNagarathnam Muthusamy 
vgetsns(struct vvar_data * vvar)126794b88e0SDavid S. Miller notrace static __always_inline u64 vgetsns(struct vvar_data *vvar)
1279a08862aSNagarathnam Muthusamy {
1289a08862aSNagarathnam Muthusamy 	u64 v;
1299a08862aSNagarathnam Muthusamy 	u64 cycles;
1309a08862aSNagarathnam Muthusamy 
1319a08862aSNagarathnam Muthusamy 	cycles = vread_tick();
1329a08862aSNagarathnam Muthusamy 	v = (cycles - vvar->clock.cycle_last) & vvar->clock.mask;
1339a08862aSNagarathnam Muthusamy 	return v * vvar->clock.mult;
1349a08862aSNagarathnam Muthusamy }
1359a08862aSNagarathnam Muthusamy 
vgetsns_stick(struct vvar_data * vvar)136caf539cdSDavid S. Miller notrace static __always_inline u64 vgetsns_stick(struct vvar_data *vvar)
137caf539cdSDavid S. Miller {
138caf539cdSDavid S. Miller 	u64 v;
139caf539cdSDavid S. Miller 	u64 cycles;
140caf539cdSDavid S. Miller 
141caf539cdSDavid S. Miller 	cycles = vread_tick_stick();
142caf539cdSDavid S. Miller 	v = (cycles - vvar->clock.cycle_last) & vvar->clock.mask;
143caf539cdSDavid S. Miller 	return v * vvar->clock.mult;
144caf539cdSDavid S. Miller }
145caf539cdSDavid S. Miller 
do_realtime(struct vvar_data * vvar,struct __kernel_old_timespec * ts)146794b88e0SDavid S. Miller notrace static __always_inline int do_realtime(struct vvar_data *vvar,
14782210fc7SArnd Bergmann 					       struct __kernel_old_timespec *ts)
1489a08862aSNagarathnam Muthusamy {
1499a08862aSNagarathnam Muthusamy 	unsigned long seq;
1509a08862aSNagarathnam Muthusamy 	u64 ns;
1519a08862aSNagarathnam Muthusamy 
1529a08862aSNagarathnam Muthusamy 	do {
1539a08862aSNagarathnam Muthusamy 		seq = vvar_read_begin(vvar);
1549a08862aSNagarathnam Muthusamy 		ts->tv_sec = vvar->wall_time_sec;
1559a08862aSNagarathnam Muthusamy 		ns = vvar->wall_time_snsec;
1569a08862aSNagarathnam Muthusamy 		ns += vgetsns(vvar);
1579a08862aSNagarathnam Muthusamy 		ns >>= vvar->clock.shift;
1589a08862aSNagarathnam Muthusamy 	} while (unlikely(vvar_read_retry(vvar, seq)));
1599a08862aSNagarathnam Muthusamy 
16019832d24SDavid S. Miller 	ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
16119832d24SDavid S. Miller 	ts->tv_nsec = ns;
1629a08862aSNagarathnam Muthusamy 
1639a08862aSNagarathnam Muthusamy 	return 0;
1649a08862aSNagarathnam Muthusamy }
1659a08862aSNagarathnam Muthusamy 
do_realtime_stick(struct vvar_data * vvar,struct __kernel_old_timespec * ts)166caf539cdSDavid S. Miller notrace static __always_inline int do_realtime_stick(struct vvar_data *vvar,
16782210fc7SArnd Bergmann 						     struct __kernel_old_timespec *ts)
168caf539cdSDavid S. Miller {
169caf539cdSDavid S. Miller 	unsigned long seq;
170caf539cdSDavid S. Miller 	u64 ns;
171caf539cdSDavid S. Miller 
172caf539cdSDavid S. Miller 	do {
173caf539cdSDavid S. Miller 		seq = vvar_read_begin(vvar);
174caf539cdSDavid S. Miller 		ts->tv_sec = vvar->wall_time_sec;
175caf539cdSDavid S. Miller 		ns = vvar->wall_time_snsec;
176caf539cdSDavid S. Miller 		ns += vgetsns_stick(vvar);
177caf539cdSDavid S. Miller 		ns >>= vvar->clock.shift;
178caf539cdSDavid S. Miller 	} while (unlikely(vvar_read_retry(vvar, seq)));
179caf539cdSDavid S. Miller 
180caf539cdSDavid S. Miller 	ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
181caf539cdSDavid S. Miller 	ts->tv_nsec = ns;
182caf539cdSDavid S. Miller 
183caf539cdSDavid S. Miller 	return 0;
184caf539cdSDavid S. Miller }
185caf539cdSDavid S. Miller 
do_monotonic(struct vvar_data * vvar,struct __kernel_old_timespec * ts)186794b88e0SDavid S. Miller notrace static __always_inline int do_monotonic(struct vvar_data *vvar,
18782210fc7SArnd Bergmann 						struct __kernel_old_timespec *ts)
1889a08862aSNagarathnam Muthusamy {
1899a08862aSNagarathnam Muthusamy 	unsigned long seq;
1909a08862aSNagarathnam Muthusamy 	u64 ns;
1919a08862aSNagarathnam Muthusamy 
1929a08862aSNagarathnam Muthusamy 	do {
1939a08862aSNagarathnam Muthusamy 		seq = vvar_read_begin(vvar);
1949a08862aSNagarathnam Muthusamy 		ts->tv_sec = vvar->monotonic_time_sec;
1959a08862aSNagarathnam Muthusamy 		ns = vvar->monotonic_time_snsec;
1969a08862aSNagarathnam Muthusamy 		ns += vgetsns(vvar);
1979a08862aSNagarathnam Muthusamy 		ns >>= vvar->clock.shift;
1989a08862aSNagarathnam Muthusamy 	} while (unlikely(vvar_read_retry(vvar, seq)));
1999a08862aSNagarathnam Muthusamy 
20019832d24SDavid S. Miller 	ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
20119832d24SDavid S. Miller 	ts->tv_nsec = ns;
2029a08862aSNagarathnam Muthusamy 
2039a08862aSNagarathnam Muthusamy 	return 0;
2049a08862aSNagarathnam Muthusamy }
2059a08862aSNagarathnam Muthusamy 
do_monotonic_stick(struct vvar_data * vvar,struct __kernel_old_timespec * ts)206caf539cdSDavid S. Miller notrace static __always_inline int do_monotonic_stick(struct vvar_data *vvar,
20782210fc7SArnd Bergmann 						      struct __kernel_old_timespec *ts)
208caf539cdSDavid S. Miller {
209caf539cdSDavid S. Miller 	unsigned long seq;
210caf539cdSDavid S. Miller 	u64 ns;
211caf539cdSDavid S. Miller 
212caf539cdSDavid S. Miller 	do {
213caf539cdSDavid S. Miller 		seq = vvar_read_begin(vvar);
214caf539cdSDavid S. Miller 		ts->tv_sec = vvar->monotonic_time_sec;
215caf539cdSDavid S. Miller 		ns = vvar->monotonic_time_snsec;
216caf539cdSDavid S. Miller 		ns += vgetsns_stick(vvar);
217caf539cdSDavid S. Miller 		ns >>= vvar->clock.shift;
218caf539cdSDavid S. Miller 	} while (unlikely(vvar_read_retry(vvar, seq)));
219caf539cdSDavid S. Miller 
220caf539cdSDavid S. Miller 	ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
221caf539cdSDavid S. Miller 	ts->tv_nsec = ns;
222caf539cdSDavid S. Miller 
223caf539cdSDavid S. Miller 	return 0;
224caf539cdSDavid S. Miller }
225caf539cdSDavid S. Miller 
do_realtime_coarse(struct vvar_data * vvar,struct __kernel_old_timespec * ts)226794b88e0SDavid S. Miller notrace static int do_realtime_coarse(struct vvar_data *vvar,
22782210fc7SArnd Bergmann 				      struct __kernel_old_timespec *ts)
2289a08862aSNagarathnam Muthusamy {
2299a08862aSNagarathnam Muthusamy 	unsigned long seq;
2309a08862aSNagarathnam Muthusamy 
2319a08862aSNagarathnam Muthusamy 	do {
2329a08862aSNagarathnam Muthusamy 		seq = vvar_read_begin(vvar);
2339a08862aSNagarathnam Muthusamy 		ts->tv_sec = vvar->wall_time_coarse_sec;
2349a08862aSNagarathnam Muthusamy 		ts->tv_nsec = vvar->wall_time_coarse_nsec;
2359a08862aSNagarathnam Muthusamy 	} while (unlikely(vvar_read_retry(vvar, seq)));
2369a08862aSNagarathnam Muthusamy 	return 0;
2379a08862aSNagarathnam Muthusamy }
2389a08862aSNagarathnam Muthusamy 
do_monotonic_coarse(struct vvar_data * vvar,struct __kernel_old_timespec * ts)239794b88e0SDavid S. Miller notrace static int do_monotonic_coarse(struct vvar_data *vvar,
24082210fc7SArnd Bergmann 				       struct __kernel_old_timespec *ts)
2419a08862aSNagarathnam Muthusamy {
2429a08862aSNagarathnam Muthusamy 	unsigned long seq;
2439a08862aSNagarathnam Muthusamy 
2449a08862aSNagarathnam Muthusamy 	do {
2459a08862aSNagarathnam Muthusamy 		seq = vvar_read_begin(vvar);
2469a08862aSNagarathnam Muthusamy 		ts->tv_sec = vvar->monotonic_time_coarse_sec;
2479a08862aSNagarathnam Muthusamy 		ts->tv_nsec = vvar->monotonic_time_coarse_nsec;
2489a08862aSNagarathnam Muthusamy 	} while (unlikely(vvar_read_retry(vvar, seq)));
2499a08862aSNagarathnam Muthusamy 
2509a08862aSNagarathnam Muthusamy 	return 0;
2519a08862aSNagarathnam Muthusamy }
2529a08862aSNagarathnam Muthusamy 
2539a08862aSNagarathnam Muthusamy notrace int
__vdso_clock_gettime(clockid_t clock,struct __kernel_old_timespec * ts)25482210fc7SArnd Bergmann __vdso_clock_gettime(clockid_t clock, struct __kernel_old_timespec *ts)
2559a08862aSNagarathnam Muthusamy {
2569a08862aSNagarathnam Muthusamy 	struct vvar_data *vvd = get_vvar_data();
2579a08862aSNagarathnam Muthusamy 
2589a08862aSNagarathnam Muthusamy 	switch (clock) {
2599a08862aSNagarathnam Muthusamy 	case CLOCK_REALTIME:
2609a08862aSNagarathnam Muthusamy 		if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
2619a08862aSNagarathnam Muthusamy 			break;
2629a08862aSNagarathnam Muthusamy 		return do_realtime(vvd, ts);
2639a08862aSNagarathnam Muthusamy 	case CLOCK_MONOTONIC:
2649a08862aSNagarathnam Muthusamy 		if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
2659a08862aSNagarathnam Muthusamy 			break;
2669a08862aSNagarathnam Muthusamy 		return do_monotonic(vvd, ts);
2679a08862aSNagarathnam Muthusamy 	case CLOCK_REALTIME_COARSE:
2689a08862aSNagarathnam Muthusamy 		return do_realtime_coarse(vvd, ts);
2699a08862aSNagarathnam Muthusamy 	case CLOCK_MONOTONIC_COARSE:
2709a08862aSNagarathnam Muthusamy 		return do_monotonic_coarse(vvd, ts);
2719a08862aSNagarathnam Muthusamy 	}
2729a08862aSNagarathnam Muthusamy 	/*
2739a08862aSNagarathnam Muthusamy 	 * Unknown clock ID ? Fall back to the syscall.
2749a08862aSNagarathnam Muthusamy 	 */
2759a08862aSNagarathnam Muthusamy 	return vdso_fallback_gettime(clock, ts);
2769a08862aSNagarathnam Muthusamy }
2779a08862aSNagarathnam Muthusamy int
27882210fc7SArnd Bergmann clock_gettime(clockid_t, struct __kernel_old_timespec *)
2799a08862aSNagarathnam Muthusamy 	__attribute__((weak, alias("__vdso_clock_gettime")));
2809a08862aSNagarathnam Muthusamy 
2819a08862aSNagarathnam Muthusamy notrace int
__vdso_clock_gettime_stick(clockid_t clock,struct __kernel_old_timespec * ts)28282210fc7SArnd Bergmann __vdso_clock_gettime_stick(clockid_t clock, struct __kernel_old_timespec *ts)
283caf539cdSDavid S. Miller {
284caf539cdSDavid S. Miller 	struct vvar_data *vvd = get_vvar_data();
285caf539cdSDavid S. Miller 
286caf539cdSDavid S. Miller 	switch (clock) {
287caf539cdSDavid S. Miller 	case CLOCK_REALTIME:
288caf539cdSDavid S. Miller 		if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
289caf539cdSDavid S. Miller 			break;
290caf539cdSDavid S. Miller 		return do_realtime_stick(vvd, ts);
291caf539cdSDavid S. Miller 	case CLOCK_MONOTONIC:
292caf539cdSDavid S. Miller 		if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
293caf539cdSDavid S. Miller 			break;
294caf539cdSDavid S. Miller 		return do_monotonic_stick(vvd, ts);
295caf539cdSDavid S. Miller 	case CLOCK_REALTIME_COARSE:
296caf539cdSDavid S. Miller 		return do_realtime_coarse(vvd, ts);
297caf539cdSDavid S. Miller 	case CLOCK_MONOTONIC_COARSE:
298caf539cdSDavid S. Miller 		return do_monotonic_coarse(vvd, ts);
299caf539cdSDavid S. Miller 	}
300caf539cdSDavid S. Miller 	/*
301caf539cdSDavid S. Miller 	 * Unknown clock ID ? Fall back to the syscall.
302caf539cdSDavid S. Miller 	 */
303caf539cdSDavid S. Miller 	return vdso_fallback_gettime(clock, ts);
304caf539cdSDavid S. Miller }
305caf539cdSDavid S. Miller 
306caf539cdSDavid S. Miller notrace int
__vdso_gettimeofday(struct __kernel_old_timeval * tv,struct timezone * tz)307ddccf40fSArnd Bergmann __vdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
3089a08862aSNagarathnam Muthusamy {
3099a08862aSNagarathnam Muthusamy 	struct vvar_data *vvd = get_vvar_data();
3109a08862aSNagarathnam Muthusamy 
3119a08862aSNagarathnam Muthusamy 	if (likely(vvd->vclock_mode != VCLOCK_NONE)) {
3129a08862aSNagarathnam Muthusamy 		if (likely(tv != NULL)) {
3139a08862aSNagarathnam Muthusamy 			union tstv_t {
31482210fc7SArnd Bergmann 				struct __kernel_old_timespec ts;
315ddccf40fSArnd Bergmann 				struct __kernel_old_timeval tv;
3169a08862aSNagarathnam Muthusamy 			} *tstv = (union tstv_t *) tv;
3179a08862aSNagarathnam Muthusamy 			do_realtime(vvd, &tstv->ts);
3189a08862aSNagarathnam Muthusamy 			/*
3199a08862aSNagarathnam Muthusamy 			 * Assign before dividing to ensure that the division is
3209a08862aSNagarathnam Muthusamy 			 * done in the type of tv_usec, not tv_nsec.
3219a08862aSNagarathnam Muthusamy 			 *
3229a08862aSNagarathnam Muthusamy 			 * There cannot be > 1 billion usec in a second:
3239a08862aSNagarathnam Muthusamy 			 * do_realtime() has already distributed such overflow
3249a08862aSNagarathnam Muthusamy 			 * into tv_sec.  So we can assign it to an int safely.
3259a08862aSNagarathnam Muthusamy 			 */
3269a08862aSNagarathnam Muthusamy 			tstv->tv.tv_usec = tstv->ts.tv_nsec;
3279a08862aSNagarathnam Muthusamy 			tstv->tv.tv_usec /= 1000;
3289a08862aSNagarathnam Muthusamy 		}
3299a08862aSNagarathnam Muthusamy 		if (unlikely(tz != NULL)) {
3309a08862aSNagarathnam Muthusamy 			/* Avoid memcpy. Some old compilers fail to inline it */
3319a08862aSNagarathnam Muthusamy 			tz->tz_minuteswest = vvd->tz_minuteswest;
3329a08862aSNagarathnam Muthusamy 			tz->tz_dsttime = vvd->tz_dsttime;
3339a08862aSNagarathnam Muthusamy 		}
3349a08862aSNagarathnam Muthusamy 		return 0;
3359a08862aSNagarathnam Muthusamy 	}
3369a08862aSNagarathnam Muthusamy 	return vdso_fallback_gettimeofday(tv, tz);
3379a08862aSNagarathnam Muthusamy }
3389a08862aSNagarathnam Muthusamy int
339ddccf40fSArnd Bergmann gettimeofday(struct __kernel_old_timeval *, struct timezone *)
3409a08862aSNagarathnam Muthusamy 	__attribute__((weak, alias("__vdso_gettimeofday")));
341caf539cdSDavid S. Miller 
342caf539cdSDavid S. Miller notrace int
__vdso_gettimeofday_stick(struct __kernel_old_timeval * tv,struct timezone * tz)343ddccf40fSArnd Bergmann __vdso_gettimeofday_stick(struct __kernel_old_timeval *tv, struct timezone *tz)
344caf539cdSDavid S. Miller {
345caf539cdSDavid S. Miller 	struct vvar_data *vvd = get_vvar_data();
346caf539cdSDavid S. Miller 
347caf539cdSDavid S. Miller 	if (likely(vvd->vclock_mode != VCLOCK_NONE)) {
348caf539cdSDavid S. Miller 		if (likely(tv != NULL)) {
349caf539cdSDavid S. Miller 			union tstv_t {
35082210fc7SArnd Bergmann 				struct __kernel_old_timespec ts;
351ddccf40fSArnd Bergmann 				struct __kernel_old_timeval tv;
352caf539cdSDavid S. Miller 			} *tstv = (union tstv_t *) tv;
353caf539cdSDavid S. Miller 			do_realtime_stick(vvd, &tstv->ts);
354caf539cdSDavid S. Miller 			/*
355caf539cdSDavid S. Miller 			 * Assign before dividing to ensure that the division is
356caf539cdSDavid S. Miller 			 * done in the type of tv_usec, not tv_nsec.
357caf539cdSDavid S. Miller 			 *
358caf539cdSDavid S. Miller 			 * There cannot be > 1 billion usec in a second:
359caf539cdSDavid S. Miller 			 * do_realtime() has already distributed such overflow
360caf539cdSDavid S. Miller 			 * into tv_sec.  So we can assign it to an int safely.
361caf539cdSDavid S. Miller 			 */
362caf539cdSDavid S. Miller 			tstv->tv.tv_usec = tstv->ts.tv_nsec;
363caf539cdSDavid S. Miller 			tstv->tv.tv_usec /= 1000;
364caf539cdSDavid S. Miller 		}
365caf539cdSDavid S. Miller 		if (unlikely(tz != NULL)) {
366caf539cdSDavid S. Miller 			/* Avoid memcpy. Some old compilers fail to inline it */
367caf539cdSDavid S. Miller 			tz->tz_minuteswest = vvd->tz_minuteswest;
368caf539cdSDavid S. Miller 			tz->tz_dsttime = vvd->tz_dsttime;
369caf539cdSDavid S. Miller 		}
370caf539cdSDavid S. Miller 		return 0;
371caf539cdSDavid S. Miller 	}
372caf539cdSDavid S. Miller 	return vdso_fallback_gettimeofday(tv, tz);
373caf539cdSDavid S. Miller }
374