vclock_gettime.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 inline long vgetns(void)
  63. {
  64. long v;
  65. cycles_t cycles;
  66. if (gtod->clock.vclock_mode == VCLOCK_TSC)
  67. cycles = vread_tsc();
  68. else
  69. cycles = vread_hpet();
  70. v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
  71. return (v * gtod->clock.mult) >> gtod->clock.shift;
  72. }
  73. notrace static noinline int do_realtime(struct timespec *ts)
  74. {
  75. unsigned long seq, ns;
  76. do {
  77. seq = read_seqbegin(&gtod->lock);
  78. ts->tv_sec = gtod->wall_time_sec;
  79. ts->tv_nsec = gtod->wall_time_nsec;
  80. ns = vgetns();
  81. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  82. timespec_add_ns(ts, ns);
  83. return 0;
  84. }
  85. notrace static noinline int do_monotonic(struct timespec *ts)
  86. {
  87. unsigned long seq, ns, secs;
  88. do {
  89. seq = read_seqbegin(&gtod->lock);
  90. secs = gtod->wall_time_sec;
  91. ns = gtod->wall_time_nsec + vgetns();
  92. secs += gtod->wall_to_monotonic.tv_sec;
  93. ns += gtod->wall_to_monotonic.tv_nsec;
  94. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  95. /* wall_time_nsec, vgetns(), and wall_to_monotonic.tv_nsec
  96. * are all guaranteed to be nonnegative.
  97. */
  98. while (ns >= NSEC_PER_SEC) {
  99. ns -= NSEC_PER_SEC;
  100. ++secs;
  101. }
  102. ts->tv_sec = secs;
  103. ts->tv_nsec = ns;
  104. return 0;
  105. }
  106. notrace static noinline int do_realtime_coarse(struct timespec *ts)
  107. {
  108. unsigned long seq;
  109. do {
  110. seq = read_seqbegin(&gtod->lock);
  111. ts->tv_sec = gtod->wall_time_coarse.tv_sec;
  112. ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
  113. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  114. return 0;
  115. }
  116. notrace static noinline int do_monotonic_coarse(struct timespec *ts)
  117. {
  118. unsigned long seq, ns, secs;
  119. do {
  120. seq = read_seqbegin(&gtod->lock);
  121. secs = gtod->wall_time_coarse.tv_sec;
  122. ns = gtod->wall_time_coarse.tv_nsec;
  123. secs += gtod->wall_to_monotonic.tv_sec;
  124. ns += gtod->wall_to_monotonic.tv_nsec;
  125. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  126. /* wall_time_nsec and wall_to_monotonic.tv_nsec are
  127. * guaranteed to be between 0 and NSEC_PER_SEC.
  128. */
  129. if (ns >= NSEC_PER_SEC) {
  130. ns -= NSEC_PER_SEC;
  131. ++secs;
  132. }
  133. ts->tv_sec = secs;
  134. ts->tv_nsec = ns;
  135. return 0;
  136. }
  137. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  138. {
  139. switch (clock) {
  140. case CLOCK_REALTIME:
  141. if (likely(gtod->clock.vclock_mode != VCLOCK_NONE))
  142. return do_realtime(ts);
  143. break;
  144. case CLOCK_MONOTONIC:
  145. if (likely(gtod->clock.vclock_mode != VCLOCK_NONE))
  146. return do_monotonic(ts);
  147. break;
  148. case CLOCK_REALTIME_COARSE:
  149. return do_realtime_coarse(ts);
  150. case CLOCK_MONOTONIC_COARSE:
  151. return do_monotonic_coarse(ts);
  152. }
  153. return vdso_fallback_gettime(clock, ts);
  154. }
  155. int clock_gettime(clockid_t, struct timespec *)
  156. __attribute__((weak, alias("__vdso_clock_gettime")));
  157. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  158. {
  159. long ret;
  160. if (likely(gtod->clock.vclock_mode != VCLOCK_NONE)) {
  161. if (likely(tv != NULL)) {
  162. BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
  163. offsetof(struct timespec, tv_nsec) ||
  164. sizeof(*tv) != sizeof(struct timespec));
  165. do_realtime((struct timespec *)tv);
  166. tv->tv_usec /= 1000;
  167. }
  168. if (unlikely(tz != NULL)) {
  169. /* Avoid memcpy. Some old compilers fail to inline it */
  170. tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
  171. tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
  172. }
  173. return 0;
  174. }
  175. asm("syscall" : "=a" (ret) :
  176. "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
  177. return ret;
  178. }
  179. int gettimeofday(struct timeval *, struct timezone *)
  180. __attribute__((weak, alias("__vdso_gettimeofday")));
  181. /*
  182. * This will break when the xtime seconds get inaccurate, but that is
  183. * unlikely
  184. */
  185. notrace time_t __vdso_time(time_t *t)
  186. {
  187. /* This is atomic on x86_64 so we don't need any locks. */
  188. time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
  189. if (t)
  190. *t = result;
  191. return result;
  192. }
  193. int time(time_t *t)
  194. __attribute__((weak, alias("__vdso_time")));