vclock_gettime.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2006 Andi Kleen, SUSE Labs.
  3. * Subject to the GNU Public License, v.2
  4. *
  5. * Fast user context implementation of clock_gettime, gettimeofday, and time.
  6. *
  7. * The code should have no internal unresolved relocations.
  8. * Check with readelf after changing.
  9. * Also alternative() doesn't work.
  10. */
  11. /*
  12. * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
  13. */
  14. /* Disable profiling for userspace code: */
  15. #ifndef DISABLE_BRANCH_PROFILING
  16. #define DISABLE_BRANCH_PROFILING
  17. #endif
  18. #include <linux/kernel.h>
  19. #include <linux/time.h>
  20. #include <linux/string.h>
  21. #include <asm/io.h>
  22. #include <asm/unistd.h>
  23. #include <asm/timex.h>
  24. #include <asm/clocksource.h>
  25. #include <asm/vvar.h>
  26. #undef TICK_PRIV_BIT
  27. #ifdef CONFIG_SPARC64
  28. #define TICK_PRIV_BIT (1UL << 63)
  29. #else
  30. #define TICK_PRIV_BIT (1ULL << 63)
  31. #endif
  32. #ifdef CONFIG_SPARC64
  33. #define SYSCALL_STRING \
  34. "ta 0x6d;" \
  35. "bcs,a 1f;" \
  36. " sub %%g0, %%o0, %%o0;" \
  37. "1:"
  38. #else
  39. #define SYSCALL_STRING \
  40. "ta 0x10;" \
  41. "bcs,a 1f;" \
  42. " sub %%g0, %%o0, %%o0;" \
  43. "1:"
  44. #endif
  45. #define SYSCALL_CLOBBERS \
  46. "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \
  47. "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \
  48. "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
  49. "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", \
  50. "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46", \
  51. "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62", \
  52. "cc", "memory"
  53. /*
  54. * Compute the vvar page's address in the process address space, and return it
  55. * as a pointer to the vvar_data.
  56. */
  57. static notrace noinline struct vvar_data *
  58. get_vvar_data(void)
  59. {
  60. unsigned long ret;
  61. /*
  62. * vdso data page is the first vDSO page so grab the return address
  63. * and move up a page to get to the data page.
  64. */
  65. ret = (unsigned long)__builtin_return_address(0);
  66. ret &= ~(8192 - 1);
  67. ret -= 8192;
  68. return (struct vvar_data *) ret;
  69. }
  70. static notrace long
  71. vdso_fallback_gettime(long clock, struct timespec *ts)
  72. {
  73. register long num __asm__("g1") = __NR_clock_gettime;
  74. register long o0 __asm__("o0") = clock;
  75. register long o1 __asm__("o1") = (long) ts;
  76. __asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
  77. "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
  78. return o0;
  79. }
  80. static notrace __always_inline long
  81. vdso_fallback_gettimeofday(struct timeval *tv, struct timezone *tz)
  82. {
  83. register long num __asm__("g1") = __NR_gettimeofday;
  84. register long o0 __asm__("o0") = (long) tv;
  85. register long o1 __asm__("o1") = (long) tz;
  86. __asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
  87. "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
  88. return o0;
  89. }
  90. #ifdef CONFIG_SPARC64
  91. static notrace noinline u64
  92. vread_tick(void) {
  93. u64 ret;
  94. __asm__ __volatile__("rd %%asr24, %0 \n"
  95. ".section .vread_tick_patch, \"ax\" \n"
  96. "rd %%tick, %0 \n"
  97. ".previous \n"
  98. : "=&r" (ret));
  99. return ret & ~TICK_PRIV_BIT;
  100. }
  101. #else
  102. static notrace noinline u64
  103. vread_tick(void)
  104. {
  105. unsigned int lo, hi;
  106. __asm__ __volatile__("rd %%asr24, %%g1\n\t"
  107. "srlx %%g1, 32, %1\n\t"
  108. "srl %%g1, 0, %0\n"
  109. ".section .vread_tick_patch, \"ax\" \n"
  110. "rd %%tick, %%g1\n"
  111. ".previous \n"
  112. : "=&r" (lo), "=&r" (hi)
  113. :
  114. : "g1");
  115. return lo | ((u64)hi << 32);
  116. }
  117. #endif
  118. static notrace inline u64
  119. vgetsns(struct vvar_data *vvar)
  120. {
  121. u64 v;
  122. u64 cycles;
  123. cycles = vread_tick();
  124. v = (cycles - vvar->clock.cycle_last) & vvar->clock.mask;
  125. return v * vvar->clock.mult;
  126. }
  127. static notrace noinline int
  128. do_realtime(struct vvar_data *vvar, struct timespec *ts)
  129. {
  130. unsigned long seq;
  131. u64 ns;
  132. ts->tv_nsec = 0;
  133. do {
  134. seq = vvar_read_begin(vvar);
  135. ts->tv_sec = vvar->wall_time_sec;
  136. ns = vvar->wall_time_snsec;
  137. ns += vgetsns(vvar);
  138. ns >>= vvar->clock.shift;
  139. } while (unlikely(vvar_read_retry(vvar, seq)));
  140. timespec_add_ns(ts, ns);
  141. return 0;
  142. }
  143. static notrace noinline int
  144. do_monotonic(struct vvar_data *vvar, struct timespec *ts)
  145. {
  146. unsigned long seq;
  147. u64 ns;
  148. ts->tv_nsec = 0;
  149. do {
  150. seq = vvar_read_begin(vvar);
  151. ts->tv_sec = vvar->monotonic_time_sec;
  152. ns = vvar->monotonic_time_snsec;
  153. ns += vgetsns(vvar);
  154. ns >>= vvar->clock.shift;
  155. } while (unlikely(vvar_read_retry(vvar, seq)));
  156. timespec_add_ns(ts, ns);
  157. return 0;
  158. }
  159. static notrace noinline int
  160. do_realtime_coarse(struct vvar_data *vvar, struct timespec *ts)
  161. {
  162. unsigned long seq;
  163. do {
  164. seq = vvar_read_begin(vvar);
  165. ts->tv_sec = vvar->wall_time_coarse_sec;
  166. ts->tv_nsec = vvar->wall_time_coarse_nsec;
  167. } while (unlikely(vvar_read_retry(vvar, seq)));
  168. return 0;
  169. }
  170. static notrace noinline int
  171. do_monotonic_coarse(struct vvar_data *vvar, struct timespec *ts)
  172. {
  173. unsigned long seq;
  174. do {
  175. seq = vvar_read_begin(vvar);
  176. ts->tv_sec = vvar->monotonic_time_coarse_sec;
  177. ts->tv_nsec = vvar->monotonic_time_coarse_nsec;
  178. } while (unlikely(vvar_read_retry(vvar, seq)));
  179. return 0;
  180. }
  181. notrace int
  182. __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  183. {
  184. struct vvar_data *vvd = get_vvar_data();
  185. switch (clock) {
  186. case CLOCK_REALTIME:
  187. if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
  188. break;
  189. return do_realtime(vvd, ts);
  190. case CLOCK_MONOTONIC:
  191. if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
  192. break;
  193. return do_monotonic(vvd, ts);
  194. case CLOCK_REALTIME_COARSE:
  195. return do_realtime_coarse(vvd, ts);
  196. case CLOCK_MONOTONIC_COARSE:
  197. return do_monotonic_coarse(vvd, ts);
  198. }
  199. /*
  200. * Unknown clock ID ? Fall back to the syscall.
  201. */
  202. return vdso_fallback_gettime(clock, ts);
  203. }
  204. int
  205. clock_gettime(clockid_t, struct timespec *)
  206. __attribute__((weak, alias("__vdso_clock_gettime")));
  207. notrace int
  208. __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  209. {
  210. struct vvar_data *vvd = get_vvar_data();
  211. if (likely(vvd->vclock_mode != VCLOCK_NONE)) {
  212. if (likely(tv != NULL)) {
  213. union tstv_t {
  214. struct timespec ts;
  215. struct timeval tv;
  216. } *tstv = (union tstv_t *) tv;
  217. do_realtime(vvd, &tstv->ts);
  218. /*
  219. * Assign before dividing to ensure that the division is
  220. * done in the type of tv_usec, not tv_nsec.
  221. *
  222. * There cannot be > 1 billion usec in a second:
  223. * do_realtime() has already distributed such overflow
  224. * into tv_sec. So we can assign it to an int safely.
  225. */
  226. tstv->tv.tv_usec = tstv->ts.tv_nsec;
  227. tstv->tv.tv_usec /= 1000;
  228. }
  229. if (unlikely(tz != NULL)) {
  230. /* Avoid memcpy. Some old compilers fail to inline it */
  231. tz->tz_minuteswest = vvd->tz_minuteswest;
  232. tz->tz_dsttime = vvd->tz_dsttime;
  233. }
  234. return 0;
  235. }
  236. return vdso_fallback_gettimeofday(tv, tz);
  237. }
  238. int
  239. gettimeofday(struct timeval *, struct timezone *)
  240. __attribute__((weak, alias("__vdso_gettimeofday")));