vclock_gettime.c 5.4 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 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. /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
  83. notrace static int __always_inline do_realtime(struct timespec *ts)
  84. {
  85. unsigned long seq, ns;
  86. int mode;
  87. do {
  88. seq = read_seqcount_begin(&gtod->seq);
  89. mode = gtod->clock.vclock_mode;
  90. ts->tv_sec = gtod->wall_time_sec;
  91. ts->tv_nsec = gtod->wall_time_nsec;
  92. ns = vgetns();
  93. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  94. timespec_add_ns(ts, ns);
  95. return mode;
  96. }
  97. notrace static int do_monotonic(struct timespec *ts)
  98. {
  99. unsigned long seq, ns;
  100. int mode;
  101. do {
  102. seq = read_seqcount_begin(&gtod->seq);
  103. mode = gtod->clock.vclock_mode;
  104. ts->tv_sec = gtod->monotonic_time_sec;
  105. ts->tv_nsec = gtod->monotonic_time_nsec;
  106. ns = vgetns();
  107. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  108. timespec_add_ns(ts, ns);
  109. return mode;
  110. }
  111. notrace static int do_realtime_coarse(struct timespec *ts)
  112. {
  113. unsigned long seq;
  114. do {
  115. seq = read_seqcount_begin(&gtod->seq);
  116. ts->tv_sec = gtod->wall_time_coarse.tv_sec;
  117. ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
  118. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  119. return 0;
  120. }
  121. notrace static int do_monotonic_coarse(struct timespec *ts)
  122. {
  123. unsigned long seq;
  124. do {
  125. seq = read_seqcount_begin(&gtod->seq);
  126. ts->tv_sec = gtod->monotonic_time_coarse.tv_sec;
  127. ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec;
  128. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  129. return 0;
  130. }
  131. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  132. {
  133. int ret = VCLOCK_NONE;
  134. switch (clock) {
  135. case CLOCK_REALTIME:
  136. ret = do_realtime(ts);
  137. break;
  138. case CLOCK_MONOTONIC:
  139. ret = do_monotonic(ts);
  140. break;
  141. case CLOCK_REALTIME_COARSE:
  142. return do_realtime_coarse(ts);
  143. case CLOCK_MONOTONIC_COARSE:
  144. return do_monotonic_coarse(ts);
  145. }
  146. if (ret == VCLOCK_NONE)
  147. return vdso_fallback_gettime(clock, ts);
  148. return 0;
  149. }
  150. int clock_gettime(clockid_t, struct timespec *)
  151. __attribute__((weak, alias("__vdso_clock_gettime")));
  152. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  153. {
  154. long ret = VCLOCK_NONE;
  155. if (likely(tv != NULL)) {
  156. BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
  157. offsetof(struct timespec, tv_nsec) ||
  158. sizeof(*tv) != sizeof(struct timespec));
  159. ret = do_realtime((struct timespec *)tv);
  160. tv->tv_usec /= 1000;
  161. }
  162. if (unlikely(tz != NULL)) {
  163. /* Avoid memcpy. Some old compilers fail to inline it */
  164. tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
  165. tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
  166. }
  167. if (ret == VCLOCK_NONE)
  168. return vdso_fallback_gtod(tv, tz);
  169. return 0;
  170. }
  171. int gettimeofday(struct timeval *, struct timezone *)
  172. __attribute__((weak, alias("__vdso_gettimeofday")));
  173. /*
  174. * This will break when the xtime seconds get inaccurate, but that is
  175. * unlikely
  176. */
  177. notrace time_t __vdso_time(time_t *t)
  178. {
  179. /* This is atomic on x86_64 so we don't need any locks. */
  180. time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
  181. if (t)
  182. *t = result;
  183. return result;
  184. }
  185. int time(time_t *t)
  186. __attribute__((weak, alias("__vdso_time")));