vclock_gettime.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. */
  10. /* Disable profiling for userspace code: */
  11. #define DISABLE_BRANCH_PROFILING
  12. #include <linux/kernel.h>
  13. #include <linux/posix-timers.h>
  14. #include <linux/time.h>
  15. #include <linux/string.h>
  16. #include <asm/vsyscall.h>
  17. #include <asm/fixmap.h>
  18. #include <asm/vgtod.h>
  19. #include <asm/timex.h>
  20. #include <asm/hpet.h>
  21. #include <asm/unistd.h>
  22. #include <asm/io.h>
  23. #define gtod (&VVAR(vsyscall_gtod_data))
  24. notrace static cycle_t vread_tsc(void)
  25. {
  26. cycle_t ret;
  27. u64 last;
  28. /*
  29. * Empirically, a fence (of type that depends on the CPU)
  30. * before rdtsc is enough to ensure that rdtsc is ordered
  31. * with respect to loads. The various CPU manuals are unclear
  32. * as to whether rdtsc can be reordered with later loads,
  33. * but no one has ever seen it happen.
  34. */
  35. rdtsc_barrier();
  36. ret = (cycle_t)vget_cycles();
  37. last = VVAR(vsyscall_gtod_data).clock.cycle_last;
  38. if (likely(ret >= last))
  39. return ret;
  40. /*
  41. * GCC likes to generate cmov here, but this branch is extremely
  42. * predictable (it's just a funciton of time and the likely is
  43. * very likely) and there's a data dependence, so force GCC
  44. * to generate a branch instead. I don't barrier() because
  45. * we don't actually need a barrier, and if this function
  46. * ever gets inlined it will generate worse code.
  47. */
  48. asm volatile ("");
  49. return last;
  50. }
  51. static notrace cycle_t vread_hpet(void)
  52. {
  53. return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
  54. }
  55. notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
  56. {
  57. long ret;
  58. asm("syscall" : "=a" (ret) :
  59. "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
  60. return ret;
  61. }
  62. notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
  63. {
  64. long ret;
  65. asm("syscall" : "=a" (ret) :
  66. "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
  67. return ret;
  68. }
  69. notrace static inline long vgetns(void)
  70. {
  71. long v;
  72. cycles_t cycles;
  73. if (gtod->clock.vclock_mode == VCLOCK_TSC)
  74. cycles = vread_tsc();
  75. else if (gtod->clock.vclock_mode == VCLOCK_HPET)
  76. cycles = vread_hpet();
  77. else
  78. return 0;
  79. v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
  80. return (v * gtod->clock.mult) >> gtod->clock.shift;
  81. }
  82. notrace static noinline int do_realtime(struct timespec *ts)
  83. {
  84. unsigned long seq, ns;
  85. int mode;
  86. do {
  87. seq = read_seqcount_begin(&gtod->seq);
  88. mode = gtod->clock.vclock_mode;
  89. ts->tv_sec = gtod->wall_time_sec;
  90. ts->tv_nsec = gtod->wall_time_nsec;
  91. ns = vgetns();
  92. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  93. timespec_add_ns(ts, ns);
  94. return mode;
  95. }
  96. notrace static noinline int do_monotonic(struct timespec *ts)
  97. {
  98. unsigned long seq, ns, secs;
  99. int mode;
  100. do {
  101. seq = read_seqcount_begin(&gtod->seq);
  102. mode = gtod->clock.vclock_mode;
  103. secs = gtod->wall_time_sec;
  104. ns = gtod->wall_time_nsec + vgetns();
  105. secs += gtod->wall_to_monotonic.tv_sec;
  106. ns += gtod->wall_to_monotonic.tv_nsec;
  107. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  108. /* wall_time_nsec, vgetns(), and wall_to_monotonic.tv_nsec
  109. * are all guaranteed to be nonnegative.
  110. */
  111. while (ns >= NSEC_PER_SEC) {
  112. ns -= NSEC_PER_SEC;
  113. ++secs;
  114. }
  115. ts->tv_sec = secs;
  116. ts->tv_nsec = ns;
  117. return mode;
  118. }
  119. notrace static noinline int do_realtime_coarse(struct timespec *ts)
  120. {
  121. unsigned long seq;
  122. do {
  123. seq = read_seqcount_begin(&gtod->seq);
  124. ts->tv_sec = gtod->wall_time_coarse.tv_sec;
  125. ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
  126. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  127. return 0;
  128. }
  129. notrace static noinline int do_monotonic_coarse(struct timespec *ts)
  130. {
  131. unsigned long seq, ns, secs;
  132. do {
  133. seq = read_seqcount_begin(&gtod->seq);
  134. secs = gtod->wall_time_coarse.tv_sec;
  135. ns = gtod->wall_time_coarse.tv_nsec;
  136. secs += gtod->wall_to_monotonic.tv_sec;
  137. ns += gtod->wall_to_monotonic.tv_nsec;
  138. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  139. /* wall_time_nsec and wall_to_monotonic.tv_nsec are
  140. * guaranteed to be between 0 and NSEC_PER_SEC.
  141. */
  142. if (ns >= NSEC_PER_SEC) {
  143. ns -= NSEC_PER_SEC;
  144. ++secs;
  145. }
  146. ts->tv_sec = secs;
  147. ts->tv_nsec = ns;
  148. return 0;
  149. }
  150. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  151. {
  152. int ret = VCLOCK_NONE;
  153. switch (clock) {
  154. case CLOCK_REALTIME:
  155. ret = do_realtime(ts);
  156. break;
  157. case CLOCK_MONOTONIC:
  158. ret = do_monotonic(ts);
  159. break;
  160. case CLOCK_REALTIME_COARSE:
  161. return do_realtime_coarse(ts);
  162. case CLOCK_MONOTONIC_COARSE:
  163. return do_monotonic_coarse(ts);
  164. }
  165. if (ret == VCLOCK_NONE)
  166. return vdso_fallback_gettime(clock, ts);
  167. return 0;
  168. }
  169. int clock_gettime(clockid_t, struct timespec *)
  170. __attribute__((weak, alias("__vdso_clock_gettime")));
  171. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  172. {
  173. long ret = VCLOCK_NONE;
  174. if (likely(tv != NULL)) {
  175. BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
  176. offsetof(struct timespec, tv_nsec) ||
  177. sizeof(*tv) != sizeof(struct timespec));
  178. ret = do_realtime((struct timespec *)tv);
  179. tv->tv_usec /= 1000;
  180. }
  181. if (unlikely(tz != NULL)) {
  182. /* Avoid memcpy. Some old compilers fail to inline it */
  183. tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
  184. tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
  185. }
  186. if (ret == VCLOCK_NONE)
  187. return vdso_fallback_gtod(tv, tz);
  188. return 0;
  189. }
  190. int gettimeofday(struct timeval *, struct timezone *)
  191. __attribute__((weak, alias("__vdso_gettimeofday")));
  192. /*
  193. * This will break when the xtime seconds get inaccurate, but that is
  194. * unlikely
  195. */
  196. notrace time_t __vdso_time(time_t *t)
  197. {
  198. /* This is atomic on x86_64 so we don't need any locks. */
  199. time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
  200. if (t)
  201. *t = result;
  202. return result;
  203. }
  204. int time(time_t *t)
  205. __attribute__((weak, alias("__vdso_time")));