19a08862aSNagarathnam Muthusamy /* 29a08862aSNagarathnam Muthusamy * Copyright 2006 Andi Kleen, SUSE Labs. 39a08862aSNagarathnam Muthusamy * Subject to the GNU Public License, v.2 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 /* Disable profiling for userspace code: */ 169a08862aSNagarathnam Muthusamy #ifndef DISABLE_BRANCH_PROFILING 179a08862aSNagarathnam Muthusamy #define DISABLE_BRANCH_PROFILING 189a08862aSNagarathnam Muthusamy #endif 199a08862aSNagarathnam Muthusamy 209a08862aSNagarathnam Muthusamy #include <linux/kernel.h> 219a08862aSNagarathnam Muthusamy #include <linux/time.h> 229a08862aSNagarathnam Muthusamy #include <linux/string.h> 239a08862aSNagarathnam Muthusamy #include <asm/io.h> 249a08862aSNagarathnam Muthusamy #include <asm/unistd.h> 259a08862aSNagarathnam Muthusamy #include <asm/timex.h> 269a08862aSNagarathnam Muthusamy #include <asm/clocksource.h> 279a08862aSNagarathnam Muthusamy #include <asm/vvar.h> 289a08862aSNagarathnam Muthusamy 29776ca154SDavid S. Miller #ifdef CONFIG_SPARC64 309a08862aSNagarathnam Muthusamy #define SYSCALL_STRING \ 319a08862aSNagarathnam Muthusamy "ta 0x6d;" \ 32776ca154SDavid S. Miller "bcs,a 1f;" \ 339a08862aSNagarathnam Muthusamy " sub %%g0, %%o0, %%o0;" \ 34776ca154SDavid S. Miller "1:" 35776ca154SDavid S. Miller #else 36776ca154SDavid S. Miller #define SYSCALL_STRING \ 37776ca154SDavid S. Miller "ta 0x10;" \ 38776ca154SDavid S. Miller "bcs,a 1f;" \ 39776ca154SDavid S. Miller " sub %%g0, %%o0, %%o0;" \ 40776ca154SDavid S. Miller "1:" 41776ca154SDavid S. Miller #endif 429a08862aSNagarathnam Muthusamy 439a08862aSNagarathnam Muthusamy #define SYSCALL_CLOBBERS \ 449a08862aSNagarathnam Muthusamy "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \ 459a08862aSNagarathnam Muthusamy "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \ 469a08862aSNagarathnam Muthusamy "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \ 479a08862aSNagarathnam Muthusamy "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", \ 489a08862aSNagarathnam Muthusamy "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46", \ 499a08862aSNagarathnam Muthusamy "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62", \ 509a08862aSNagarathnam Muthusamy "cc", "memory" 519a08862aSNagarathnam Muthusamy 529a08862aSNagarathnam Muthusamy /* 539a08862aSNagarathnam Muthusamy * Compute the vvar page's address in the process address space, and return it 549a08862aSNagarathnam Muthusamy * as a pointer to the vvar_data. 559a08862aSNagarathnam Muthusamy */ 56794b88e0SDavid S. Miller notrace static __always_inline struct vvar_data *get_vvar_data(void) 579a08862aSNagarathnam Muthusamy { 589a08862aSNagarathnam Muthusamy unsigned long ret; 599a08862aSNagarathnam Muthusamy 609a08862aSNagarathnam Muthusamy /* 61794b88e0SDavid S. Miller * vdso data page is the first vDSO page so grab the PC 629a08862aSNagarathnam Muthusamy * and move up a page to get to the data page. 639a08862aSNagarathnam Muthusamy */ 64794b88e0SDavid S. Miller __asm__("rd %%pc, %0" : "=r" (ret)); 659a08862aSNagarathnam Muthusamy ret &= ~(8192 - 1); 669a08862aSNagarathnam Muthusamy ret -= 8192; 679a08862aSNagarathnam Muthusamy 689a08862aSNagarathnam Muthusamy return (struct vvar_data *) ret; 699a08862aSNagarathnam Muthusamy } 709a08862aSNagarathnam Muthusamy 71794b88e0SDavid S. Miller notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) 729a08862aSNagarathnam Muthusamy { 739a08862aSNagarathnam Muthusamy register long num __asm__("g1") = __NR_clock_gettime; 749a08862aSNagarathnam Muthusamy register long o0 __asm__("o0") = clock; 759a08862aSNagarathnam Muthusamy register long o1 __asm__("o1") = (long) ts; 769a08862aSNagarathnam Muthusamy 779a08862aSNagarathnam Muthusamy __asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num), 789a08862aSNagarathnam Muthusamy "0" (o0), "r" (o1) : SYSCALL_CLOBBERS); 799a08862aSNagarathnam Muthusamy return o0; 809a08862aSNagarathnam Muthusamy } 819a08862aSNagarathnam Muthusamy 82794b88e0SDavid S. Miller notrace static long vdso_fallback_gettimeofday(struct timeval *tv, struct timezone *tz) 839a08862aSNagarathnam Muthusamy { 849a08862aSNagarathnam Muthusamy register long num __asm__("g1") = __NR_gettimeofday; 859a08862aSNagarathnam Muthusamy register long o0 __asm__("o0") = (long) tv; 869a08862aSNagarathnam Muthusamy register long o1 __asm__("o1") = (long) tz; 879a08862aSNagarathnam Muthusamy 889a08862aSNagarathnam Muthusamy __asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num), 899a08862aSNagarathnam Muthusamy "0" (o0), "r" (o1) : SYSCALL_CLOBBERS); 909a08862aSNagarathnam Muthusamy return o0; 919a08862aSNagarathnam Muthusamy } 929a08862aSNagarathnam Muthusamy 939a08862aSNagarathnam Muthusamy #ifdef CONFIG_SPARC64 94794b88e0SDavid S. Miller notrace static __always_inline u64 vread_tick(void) 95794b88e0SDavid S. Miller { 969a08862aSNagarathnam Muthusamy u64 ret; 979a08862aSNagarathnam Muthusamy 982f6c9bf3SDavid S. Miller __asm__ __volatile__("1:\n\t" 992f6c9bf3SDavid S. Miller "rd %%tick, %0\n\t" 1002f6c9bf3SDavid S. Miller ".pushsection .tick_patch, \"a\"\n\t" 1012f6c9bf3SDavid S. Miller ".word 1b - ., 1f - .\n\t" 1022f6c9bf3SDavid S. Miller ".popsection\n\t" 1032f6c9bf3SDavid S. Miller ".pushsection .tick_patch_replacement, \"ax\"\n\t" 1042f6c9bf3SDavid S. Miller "1:\n\t" 1052f6c9bf3SDavid S. Miller "rd %%asr24, %0\n\t" 1062f6c9bf3SDavid S. Miller ".popsection\n" 1072f6c9bf3SDavid S. Miller : "=r" (ret)); 1083fe5d7e8SDavid S. Miller return ret; 1099a08862aSNagarathnam Muthusamy } 1109a08862aSNagarathnam Muthusamy #else 111794b88e0SDavid S. Miller notrace static __always_inline u64 vread_tick(void) 1129a08862aSNagarathnam Muthusamy { 1132f6c9bf3SDavid S. Miller register unsigned long long ret asm("o4"); 1149a08862aSNagarathnam Muthusamy 1152f6c9bf3SDavid S. Miller __asm__ __volatile__("1:\n\t" 1162f6c9bf3SDavid S. Miller "rd %%tick, %L0\n\t" 1172f6c9bf3SDavid S. Miller "srlx %L0, 32, %H0\n\t" 1182f6c9bf3SDavid S. Miller ".pushsection .tick_patch, \"a\"\n\t" 1192f6c9bf3SDavid S. Miller ".word 1b - ., 1f - .\n\t" 1202f6c9bf3SDavid S. Miller ".popsection\n\t" 1212f6c9bf3SDavid S. Miller ".pushsection .tick_patch_replacement, \"ax\"\n\t" 1222f6c9bf3SDavid S. Miller "1:\n\t" 1232f6c9bf3SDavid S. Miller "rd %%asr24, %L0\n\t" 1242f6c9bf3SDavid S. Miller ".popsection\n" 1252f6c9bf3SDavid S. Miller : "=r" (ret)); 1262f6c9bf3SDavid S. Miller return ret; 1279a08862aSNagarathnam Muthusamy } 1289a08862aSNagarathnam Muthusamy #endif 1299a08862aSNagarathnam Muthusamy 130794b88e0SDavid S. Miller notrace static __always_inline u64 vgetsns(struct vvar_data *vvar) 1319a08862aSNagarathnam Muthusamy { 1329a08862aSNagarathnam Muthusamy u64 v; 1339a08862aSNagarathnam Muthusamy u64 cycles; 1349a08862aSNagarathnam Muthusamy 1359a08862aSNagarathnam Muthusamy cycles = vread_tick(); 1369a08862aSNagarathnam Muthusamy v = (cycles - vvar->clock.cycle_last) & vvar->clock.mask; 1379a08862aSNagarathnam Muthusamy return v * vvar->clock.mult; 1389a08862aSNagarathnam Muthusamy } 1399a08862aSNagarathnam Muthusamy 140794b88e0SDavid S. Miller notrace static __always_inline int do_realtime(struct vvar_data *vvar, 141794b88e0SDavid S. Miller struct timespec *ts) 1429a08862aSNagarathnam Muthusamy { 1439a08862aSNagarathnam Muthusamy unsigned long seq; 1449a08862aSNagarathnam Muthusamy u64 ns; 1459a08862aSNagarathnam Muthusamy 1469a08862aSNagarathnam Muthusamy ts->tv_nsec = 0; 1479a08862aSNagarathnam Muthusamy do { 1489a08862aSNagarathnam Muthusamy seq = vvar_read_begin(vvar); 1499a08862aSNagarathnam Muthusamy ts->tv_sec = vvar->wall_time_sec; 1509a08862aSNagarathnam Muthusamy ns = vvar->wall_time_snsec; 1519a08862aSNagarathnam Muthusamy ns += vgetsns(vvar); 1529a08862aSNagarathnam Muthusamy ns >>= vvar->clock.shift; 1539a08862aSNagarathnam Muthusamy } while (unlikely(vvar_read_retry(vvar, seq))); 1549a08862aSNagarathnam Muthusamy 1559a08862aSNagarathnam Muthusamy timespec_add_ns(ts, ns); 1569a08862aSNagarathnam Muthusamy 1579a08862aSNagarathnam Muthusamy return 0; 1589a08862aSNagarathnam Muthusamy } 1599a08862aSNagarathnam Muthusamy 160794b88e0SDavid S. Miller notrace static __always_inline int do_monotonic(struct vvar_data *vvar, 161794b88e0SDavid S. Miller struct timespec *ts) 1629a08862aSNagarathnam Muthusamy { 1639a08862aSNagarathnam Muthusamy unsigned long seq; 1649a08862aSNagarathnam Muthusamy u64 ns; 1659a08862aSNagarathnam Muthusamy 1669a08862aSNagarathnam Muthusamy ts->tv_nsec = 0; 1679a08862aSNagarathnam Muthusamy do { 1689a08862aSNagarathnam Muthusamy seq = vvar_read_begin(vvar); 1699a08862aSNagarathnam Muthusamy ts->tv_sec = vvar->monotonic_time_sec; 1709a08862aSNagarathnam Muthusamy ns = vvar->monotonic_time_snsec; 1719a08862aSNagarathnam Muthusamy ns += vgetsns(vvar); 1729a08862aSNagarathnam Muthusamy ns >>= vvar->clock.shift; 1739a08862aSNagarathnam Muthusamy } while (unlikely(vvar_read_retry(vvar, seq))); 1749a08862aSNagarathnam Muthusamy 1759a08862aSNagarathnam Muthusamy timespec_add_ns(ts, ns); 1769a08862aSNagarathnam Muthusamy 1779a08862aSNagarathnam Muthusamy return 0; 1789a08862aSNagarathnam Muthusamy } 1799a08862aSNagarathnam Muthusamy 180794b88e0SDavid S. Miller notrace static int do_realtime_coarse(struct vvar_data *vvar, 181794b88e0SDavid S. Miller struct timespec *ts) 1829a08862aSNagarathnam Muthusamy { 1839a08862aSNagarathnam Muthusamy unsigned long seq; 1849a08862aSNagarathnam Muthusamy 1859a08862aSNagarathnam Muthusamy do { 1869a08862aSNagarathnam Muthusamy seq = vvar_read_begin(vvar); 1879a08862aSNagarathnam Muthusamy ts->tv_sec = vvar->wall_time_coarse_sec; 1889a08862aSNagarathnam Muthusamy ts->tv_nsec = vvar->wall_time_coarse_nsec; 1899a08862aSNagarathnam Muthusamy } while (unlikely(vvar_read_retry(vvar, seq))); 1909a08862aSNagarathnam Muthusamy return 0; 1919a08862aSNagarathnam Muthusamy } 1929a08862aSNagarathnam Muthusamy 193794b88e0SDavid S. Miller notrace static int do_monotonic_coarse(struct vvar_data *vvar, 194794b88e0SDavid S. Miller struct timespec *ts) 1959a08862aSNagarathnam Muthusamy { 1969a08862aSNagarathnam Muthusamy unsigned long seq; 1979a08862aSNagarathnam Muthusamy 1989a08862aSNagarathnam Muthusamy do { 1999a08862aSNagarathnam Muthusamy seq = vvar_read_begin(vvar); 2009a08862aSNagarathnam Muthusamy ts->tv_sec = vvar->monotonic_time_coarse_sec; 2019a08862aSNagarathnam Muthusamy ts->tv_nsec = vvar->monotonic_time_coarse_nsec; 2029a08862aSNagarathnam Muthusamy } while (unlikely(vvar_read_retry(vvar, seq))); 2039a08862aSNagarathnam Muthusamy 2049a08862aSNagarathnam Muthusamy return 0; 2059a08862aSNagarathnam Muthusamy } 2069a08862aSNagarathnam Muthusamy 2079a08862aSNagarathnam Muthusamy notrace int 2089a08862aSNagarathnam Muthusamy __vdso_clock_gettime(clockid_t clock, struct timespec *ts) 2099a08862aSNagarathnam Muthusamy { 2109a08862aSNagarathnam Muthusamy struct vvar_data *vvd = get_vvar_data(); 2119a08862aSNagarathnam Muthusamy 2129a08862aSNagarathnam Muthusamy switch (clock) { 2139a08862aSNagarathnam Muthusamy case CLOCK_REALTIME: 2149a08862aSNagarathnam Muthusamy if (unlikely(vvd->vclock_mode == VCLOCK_NONE)) 2159a08862aSNagarathnam Muthusamy break; 2169a08862aSNagarathnam Muthusamy return do_realtime(vvd, ts); 2179a08862aSNagarathnam Muthusamy case CLOCK_MONOTONIC: 2189a08862aSNagarathnam Muthusamy if (unlikely(vvd->vclock_mode == VCLOCK_NONE)) 2199a08862aSNagarathnam Muthusamy break; 2209a08862aSNagarathnam Muthusamy return do_monotonic(vvd, ts); 2219a08862aSNagarathnam Muthusamy case CLOCK_REALTIME_COARSE: 2229a08862aSNagarathnam Muthusamy return do_realtime_coarse(vvd, ts); 2239a08862aSNagarathnam Muthusamy case CLOCK_MONOTONIC_COARSE: 2249a08862aSNagarathnam Muthusamy return do_monotonic_coarse(vvd, ts); 2259a08862aSNagarathnam Muthusamy } 2269a08862aSNagarathnam Muthusamy /* 2279a08862aSNagarathnam Muthusamy * Unknown clock ID ? Fall back to the syscall. 2289a08862aSNagarathnam Muthusamy */ 2299a08862aSNagarathnam Muthusamy return vdso_fallback_gettime(clock, ts); 2309a08862aSNagarathnam Muthusamy } 2319a08862aSNagarathnam Muthusamy int 2329a08862aSNagarathnam Muthusamy clock_gettime(clockid_t, struct timespec *) 2339a08862aSNagarathnam Muthusamy __attribute__((weak, alias("__vdso_clock_gettime"))); 2349a08862aSNagarathnam Muthusamy 2359a08862aSNagarathnam Muthusamy notrace int 2369a08862aSNagarathnam Muthusamy __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) 2379a08862aSNagarathnam Muthusamy { 2389a08862aSNagarathnam Muthusamy struct vvar_data *vvd = get_vvar_data(); 2399a08862aSNagarathnam Muthusamy 2409a08862aSNagarathnam Muthusamy if (likely(vvd->vclock_mode != VCLOCK_NONE)) { 2419a08862aSNagarathnam Muthusamy if (likely(tv != NULL)) { 2429a08862aSNagarathnam Muthusamy union tstv_t { 2439a08862aSNagarathnam Muthusamy struct timespec ts; 2449a08862aSNagarathnam Muthusamy struct timeval tv; 2459a08862aSNagarathnam Muthusamy } *tstv = (union tstv_t *) tv; 2469a08862aSNagarathnam Muthusamy do_realtime(vvd, &tstv->ts); 2479a08862aSNagarathnam Muthusamy /* 2489a08862aSNagarathnam Muthusamy * Assign before dividing to ensure that the division is 2499a08862aSNagarathnam Muthusamy * done in the type of tv_usec, not tv_nsec. 2509a08862aSNagarathnam Muthusamy * 2519a08862aSNagarathnam Muthusamy * There cannot be > 1 billion usec in a second: 2529a08862aSNagarathnam Muthusamy * do_realtime() has already distributed such overflow 2539a08862aSNagarathnam Muthusamy * into tv_sec. So we can assign it to an int safely. 2549a08862aSNagarathnam Muthusamy */ 2559a08862aSNagarathnam Muthusamy tstv->tv.tv_usec = tstv->ts.tv_nsec; 2569a08862aSNagarathnam Muthusamy tstv->tv.tv_usec /= 1000; 2579a08862aSNagarathnam Muthusamy } 2589a08862aSNagarathnam Muthusamy if (unlikely(tz != NULL)) { 2599a08862aSNagarathnam Muthusamy /* Avoid memcpy. Some old compilers fail to inline it */ 2609a08862aSNagarathnam Muthusamy tz->tz_minuteswest = vvd->tz_minuteswest; 2619a08862aSNagarathnam Muthusamy tz->tz_dsttime = vvd->tz_dsttime; 2629a08862aSNagarathnam Muthusamy } 2639a08862aSNagarathnam Muthusamy return 0; 2649a08862aSNagarathnam Muthusamy } 2659a08862aSNagarathnam Muthusamy return vdso_fallback_gettimeofday(tv, tz); 2669a08862aSNagarathnam Muthusamy } 2679a08862aSNagarathnam Muthusamy int 2689a08862aSNagarathnam Muthusamy gettimeofday(struct timeval *, struct timezone *) 2699a08862aSNagarathnam Muthusamy __attribute__((weak, alias("__vdso_gettimeofday"))); 270