vclock_gettime.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #include <asm/pvclock.h>
  24. #define gtod (&VVAR(vsyscall_gtod_data))
  25. notrace static cycle_t vread_tsc(void)
  26. {
  27. cycle_t ret;
  28. u64 last;
  29. /*
  30. * Empirically, a fence (of type that depends on the CPU)
  31. * before rdtsc is enough to ensure that rdtsc is ordered
  32. * with respect to loads. The various CPU manuals are unclear
  33. * as to whether rdtsc can be reordered with later loads,
  34. * but no one has ever seen it happen.
  35. */
  36. rdtsc_barrier();
  37. ret = (cycle_t)vget_cycles();
  38. last = VVAR(vsyscall_gtod_data).clock.cycle_last;
  39. if (likely(ret >= last))
  40. return ret;
  41. /*
  42. * GCC likes to generate cmov here, but this branch is extremely
  43. * predictable (it's just a funciton of time and the likely is
  44. * very likely) and there's a data dependence, so force GCC
  45. * to generate a branch instead. I don't barrier() because
  46. * we don't actually need a barrier, and if this function
  47. * ever gets inlined it will generate worse code.
  48. */
  49. asm volatile ("");
  50. return last;
  51. }
  52. static notrace cycle_t vread_hpet(void)
  53. {
  54. return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + HPET_COUNTER);
  55. }
  56. #ifdef CONFIG_PARAVIRT_CLOCK
  57. static notrace const struct pvclock_vsyscall_time_info *get_pvti(int cpu)
  58. {
  59. const struct pvclock_vsyscall_time_info *pvti_base;
  60. int idx = cpu / (PAGE_SIZE/PVTI_SIZE);
  61. int offset = cpu % (PAGE_SIZE/PVTI_SIZE);
  62. BUG_ON(PVCLOCK_FIXMAP_BEGIN + idx > PVCLOCK_FIXMAP_END);
  63. pvti_base = (struct pvclock_vsyscall_time_info *)
  64. __fix_to_virt(PVCLOCK_FIXMAP_BEGIN+idx);
  65. return &pvti_base[offset];
  66. }
  67. static notrace cycle_t vread_pvclock(int *mode)
  68. {
  69. const struct pvclock_vsyscall_time_info *pvti;
  70. cycle_t ret;
  71. u64 last;
  72. u32 version;
  73. u32 migrate_count;
  74. u8 flags;
  75. unsigned cpu, cpu1;
  76. /*
  77. * When looping to get a consistent (time-info, tsc) pair, we
  78. * also need to deal with the possibility we can switch vcpus,
  79. * so make sure we always re-fetch time-info for the current vcpu.
  80. */
  81. do {
  82. cpu = __getcpu() & VGETCPU_CPU_MASK;
  83. /* TODO: We can put vcpu id into higher bits of pvti.version.
  84. * This will save a couple of cycles by getting rid of
  85. * __getcpu() calls (Gleb).
  86. */
  87. pvti = get_pvti(cpu);
  88. migrate_count = pvti->migrate_count;
  89. version = __pvclock_read_cycles(&pvti->pvti, &ret, &flags);
  90. /*
  91. * Test we're still on the cpu as well as the version.
  92. * We could have been migrated just after the first
  93. * vgetcpu but before fetching the version, so we
  94. * wouldn't notice a version change.
  95. */
  96. cpu1 = __getcpu() & VGETCPU_CPU_MASK;
  97. } while (unlikely(cpu != cpu1 ||
  98. (pvti->pvti.version & 1) ||
  99. pvti->pvti.version != version ||
  100. pvti->migrate_count != migrate_count));
  101. if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT)))
  102. *mode = VCLOCK_NONE;
  103. /* refer to tsc.c read_tsc() comment for rationale */
  104. last = VVAR(vsyscall_gtod_data).clock.cycle_last;
  105. if (likely(ret >= last))
  106. return ret;
  107. return last;
  108. }
  109. #endif
  110. notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
  111. {
  112. long ret;
  113. asm("syscall" : "=a" (ret) :
  114. "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
  115. return ret;
  116. }
  117. notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
  118. {
  119. long ret;
  120. asm("syscall" : "=a" (ret) :
  121. "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
  122. return ret;
  123. }
  124. notrace static inline u64 vgetsns(int *mode)
  125. {
  126. long v;
  127. cycles_t cycles;
  128. if (gtod->clock.vclock_mode == VCLOCK_TSC)
  129. cycles = vread_tsc();
  130. else if (gtod->clock.vclock_mode == VCLOCK_HPET)
  131. cycles = vread_hpet();
  132. #ifdef CONFIG_PARAVIRT_CLOCK
  133. else if (gtod->clock.vclock_mode == VCLOCK_PVCLOCK)
  134. cycles = vread_pvclock(mode);
  135. #endif
  136. else
  137. return 0;
  138. v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
  139. return v * gtod->clock.mult;
  140. }
  141. /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
  142. notrace static int __always_inline do_realtime(struct timespec *ts)
  143. {
  144. unsigned long seq;
  145. u64 ns;
  146. int mode;
  147. ts->tv_nsec = 0;
  148. do {
  149. seq = read_seqcount_begin(&gtod->seq);
  150. mode = gtod->clock.vclock_mode;
  151. ts->tv_sec = gtod->wall_time_sec;
  152. ns = gtod->wall_time_snsec;
  153. ns += vgetsns(&mode);
  154. ns >>= gtod->clock.shift;
  155. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  156. timespec_add_ns(ts, ns);
  157. return mode;
  158. }
  159. notrace static int do_monotonic(struct timespec *ts)
  160. {
  161. unsigned long seq;
  162. u64 ns;
  163. int mode;
  164. ts->tv_nsec = 0;
  165. do {
  166. seq = read_seqcount_begin(&gtod->seq);
  167. mode = gtod->clock.vclock_mode;
  168. ts->tv_sec = gtod->monotonic_time_sec;
  169. ns = gtod->monotonic_time_snsec;
  170. ns += vgetsns(&mode);
  171. ns >>= gtod->clock.shift;
  172. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  173. timespec_add_ns(ts, ns);
  174. return mode;
  175. }
  176. notrace static int do_realtime_coarse(struct timespec *ts)
  177. {
  178. unsigned long seq;
  179. do {
  180. seq = read_seqcount_begin(&gtod->seq);
  181. ts->tv_sec = gtod->wall_time_coarse.tv_sec;
  182. ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
  183. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  184. return 0;
  185. }
  186. notrace static int do_monotonic_coarse(struct timespec *ts)
  187. {
  188. unsigned long seq;
  189. do {
  190. seq = read_seqcount_begin(&gtod->seq);
  191. ts->tv_sec = gtod->monotonic_time_coarse.tv_sec;
  192. ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec;
  193. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  194. return 0;
  195. }
  196. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  197. {
  198. int ret = VCLOCK_NONE;
  199. switch (clock) {
  200. case CLOCK_REALTIME:
  201. ret = do_realtime(ts);
  202. break;
  203. case CLOCK_MONOTONIC:
  204. ret = do_monotonic(ts);
  205. break;
  206. case CLOCK_REALTIME_COARSE:
  207. return do_realtime_coarse(ts);
  208. case CLOCK_MONOTONIC_COARSE:
  209. return do_monotonic_coarse(ts);
  210. }
  211. if (ret == VCLOCK_NONE)
  212. return vdso_fallback_gettime(clock, ts);
  213. return 0;
  214. }
  215. int clock_gettime(clockid_t, struct timespec *)
  216. __attribute__((weak, alias("__vdso_clock_gettime")));
  217. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  218. {
  219. long ret = VCLOCK_NONE;
  220. if (likely(tv != NULL)) {
  221. BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
  222. offsetof(struct timespec, tv_nsec) ||
  223. sizeof(*tv) != sizeof(struct timespec));
  224. ret = do_realtime((struct timespec *)tv);
  225. tv->tv_usec /= 1000;
  226. }
  227. if (unlikely(tz != NULL)) {
  228. /* Avoid memcpy. Some old compilers fail to inline it */
  229. tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
  230. tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
  231. }
  232. if (ret == VCLOCK_NONE)
  233. return vdso_fallback_gtod(tv, tz);
  234. return 0;
  235. }
  236. int gettimeofday(struct timeval *, struct timezone *)
  237. __attribute__((weak, alias("__vdso_gettimeofday")));
  238. /*
  239. * This will break when the xtime seconds get inaccurate, but that is
  240. * unlikely
  241. */
  242. notrace time_t __vdso_time(time_t *t)
  243. {
  244. /* This is atomic on x86_64 so we don't need any locks. */
  245. time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
  246. if (t)
  247. *t = result;
  248. return result;
  249. }
  250. int time(time_t *t)
  251. __attribute__((weak, alias("__vdso_time")));