vclock_gettime.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 u64 vgetsns(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;
  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;
  86. u64 ns;
  87. int mode;
  88. ts->tv_nsec = 0;
  89. do {
  90. seq = read_seqcount_begin(&gtod->seq);
  91. mode = gtod->clock.vclock_mode;
  92. ts->tv_sec = gtod->wall_time_sec;
  93. ns = gtod->wall_time_snsec;
  94. ns += vgetsns();
  95. ns >>= gtod->clock.shift;
  96. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  97. timespec_add_ns(ts, ns);
  98. return mode;
  99. }
  100. notrace static int do_monotonic(struct timespec *ts)
  101. {
  102. unsigned long seq;
  103. u64 ns;
  104. int mode;
  105. ts->tv_nsec = 0;
  106. do {
  107. seq = read_seqcount_begin(&gtod->seq);
  108. mode = gtod->clock.vclock_mode;
  109. ts->tv_sec = gtod->monotonic_time_sec;
  110. ns = gtod->monotonic_time_snsec;
  111. ns += vgetsns();
  112. ns >>= gtod->clock.shift;
  113. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  114. timespec_add_ns(ts, ns);
  115. return mode;
  116. }
  117. notrace static int do_realtime_coarse(struct timespec *ts)
  118. {
  119. unsigned long seq;
  120. do {
  121. seq = read_seqcount_begin(&gtod->seq);
  122. ts->tv_sec = gtod->wall_time_coarse.tv_sec;
  123. ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
  124. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  125. return 0;
  126. }
  127. notrace static int do_monotonic_coarse(struct timespec *ts)
  128. {
  129. unsigned long seq;
  130. do {
  131. seq = read_seqcount_begin(&gtod->seq);
  132. ts->tv_sec = gtod->monotonic_time_coarse.tv_sec;
  133. ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec;
  134. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  135. return 0;
  136. }
  137. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  138. {
  139. int ret = VCLOCK_NONE;
  140. switch (clock) {
  141. case CLOCK_REALTIME:
  142. ret = do_realtime(ts);
  143. break;
  144. case CLOCK_MONOTONIC:
  145. ret = do_monotonic(ts);
  146. break;
  147. case CLOCK_REALTIME_COARSE:
  148. return do_realtime_coarse(ts);
  149. case CLOCK_MONOTONIC_COARSE:
  150. return do_monotonic_coarse(ts);
  151. }
  152. if (ret == VCLOCK_NONE)
  153. return vdso_fallback_gettime(clock, ts);
  154. return 0;
  155. }
  156. int clock_gettime(clockid_t, struct timespec *)
  157. __attribute__((weak, alias("__vdso_clock_gettime")));
  158. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  159. {
  160. long ret = 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. ret = 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. if (ret == VCLOCK_NONE)
  174. return vdso_fallback_gtod(tv, tz);
  175. return 0;
  176. }
  177. int gettimeofday(struct timeval *, struct timezone *)
  178. __attribute__((weak, alias("__vdso_gettimeofday")));
  179. /*
  180. * This will break when the xtime seconds get inaccurate, but that is
  181. * unlikely
  182. */
  183. notrace time_t __vdso_time(time_t *t)
  184. {
  185. /* This is atomic on x86_64 so we don't need any locks. */
  186. time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
  187. if (t)
  188. *t = result;
  189. return result;
  190. }
  191. int time(time_t *t)
  192. __attribute__((weak, alias("__vdso_time")));