vclock_gettime.c 6.3 KB

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