vclock_gettime.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. * 32 Bit compat layer by Stefani Seibold <stefani@seibold.net>
  8. * sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany
  9. *
  10. * The code should have no internal unresolved relocations.
  11. * Check with readelf after changing.
  12. */
  13. #include <uapi/linux/time.h>
  14. #include <asm/vgtod.h>
  15. #include <asm/vvar.h>
  16. #include <asm/unistd.h>
  17. #include <asm/msr.h>
  18. #include <asm/pvclock.h>
  19. #include <asm/mshyperv.h>
  20. #include <linux/math64.h>
  21. #include <linux/time.h>
  22. #include <linux/kernel.h>
  23. #define gtod (&VVAR(vsyscall_gtod_data))
  24. extern int __vdso_clock_gettime(clockid_t clock, struct timespec *ts);
  25. extern int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz);
  26. extern time_t __vdso_time(time_t *t);
  27. #ifdef CONFIG_PARAVIRT_CLOCK
  28. extern u8 pvclock_page
  29. __attribute__((visibility("hidden")));
  30. #endif
  31. #ifdef CONFIG_HYPERV_TSCPAGE
  32. extern u8 hvclock_page
  33. __attribute__((visibility("hidden")));
  34. #endif
  35. #ifndef BUILD_VDSO32
  36. notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
  37. {
  38. long ret;
  39. asm("syscall" : "=a" (ret) :
  40. "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory");
  41. return ret;
  42. }
  43. notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
  44. {
  45. long ret;
  46. asm("syscall" : "=a" (ret) :
  47. "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
  48. return ret;
  49. }
  50. #else
  51. notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
  52. {
  53. long ret;
  54. asm(
  55. "mov %%ebx, %%edx \n"
  56. "mov %2, %%ebx \n"
  57. "call __kernel_vsyscall \n"
  58. "mov %%edx, %%ebx \n"
  59. : "=a" (ret)
  60. : "0" (__NR_clock_gettime), "g" (clock), "c" (ts)
  61. : "memory", "edx");
  62. return ret;
  63. }
  64. notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
  65. {
  66. long ret;
  67. asm(
  68. "mov %%ebx, %%edx \n"
  69. "mov %2, %%ebx \n"
  70. "call __kernel_vsyscall \n"
  71. "mov %%edx, %%ebx \n"
  72. : "=a" (ret)
  73. : "0" (__NR_gettimeofday), "g" (tv), "c" (tz)
  74. : "memory", "edx");
  75. return ret;
  76. }
  77. #endif
  78. #ifdef CONFIG_PARAVIRT_CLOCK
  79. static notrace const struct pvclock_vsyscall_time_info *get_pvti0(void)
  80. {
  81. return (const struct pvclock_vsyscall_time_info *)&pvclock_page;
  82. }
  83. static notrace u64 vread_pvclock(int *mode)
  84. {
  85. const struct pvclock_vcpu_time_info *pvti = &get_pvti0()->pvti;
  86. u64 ret;
  87. u64 last;
  88. u32 version;
  89. /*
  90. * Note: The kernel and hypervisor must guarantee that cpu ID
  91. * number maps 1:1 to per-CPU pvclock time info.
  92. *
  93. * Because the hypervisor is entirely unaware of guest userspace
  94. * preemption, it cannot guarantee that per-CPU pvclock time
  95. * info is updated if the underlying CPU changes or that that
  96. * version is increased whenever underlying CPU changes.
  97. *
  98. * On KVM, we are guaranteed that pvti updates for any vCPU are
  99. * atomic as seen by *all* vCPUs. This is an even stronger
  100. * guarantee than we get with a normal seqlock.
  101. *
  102. * On Xen, we don't appear to have that guarantee, but Xen still
  103. * supplies a valid seqlock using the version field.
  104. *
  105. * We only do pvclock vdso timing at all if
  106. * PVCLOCK_TSC_STABLE_BIT is set, and we interpret that bit to
  107. * mean that all vCPUs have matching pvti and that the TSC is
  108. * synced, so we can just look at vCPU 0's pvti.
  109. */
  110. do {
  111. version = pvclock_read_begin(pvti);
  112. if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT))) {
  113. *mode = VCLOCK_NONE;
  114. return 0;
  115. }
  116. ret = __pvclock_read_cycles(pvti, rdtsc_ordered());
  117. } while (pvclock_read_retry(pvti, version));
  118. /* refer to vread_tsc() comment for rationale */
  119. last = gtod->cycle_last;
  120. if (likely(ret >= last))
  121. return ret;
  122. return last;
  123. }
  124. #endif
  125. #ifdef CONFIG_HYPERV_TSCPAGE
  126. static notrace u64 vread_hvclock(int *mode)
  127. {
  128. const struct ms_hyperv_tsc_page *tsc_pg =
  129. (const struct ms_hyperv_tsc_page *)&hvclock_page;
  130. u64 current_tick = hv_read_tsc_page(tsc_pg);
  131. if (current_tick != U64_MAX)
  132. return current_tick;
  133. *mode = VCLOCK_NONE;
  134. return 0;
  135. }
  136. #endif
  137. notrace static u64 vread_tsc(void)
  138. {
  139. u64 ret = (u64)rdtsc_ordered();
  140. u64 last = gtod->cycle_last;
  141. if (likely(ret >= last))
  142. return ret;
  143. /*
  144. * GCC likes to generate cmov here, but this branch is extremely
  145. * predictable (it's just a function of time and the likely is
  146. * very likely) and there's a data dependence, so force GCC
  147. * to generate a branch instead. I don't barrier() because
  148. * we don't actually need a barrier, and if this function
  149. * ever gets inlined it will generate worse code.
  150. */
  151. asm volatile ("");
  152. return last;
  153. }
  154. notrace static inline u64 vgetsns(int *mode)
  155. {
  156. u64 v;
  157. cycles_t cycles;
  158. if (gtod->vclock_mode == VCLOCK_TSC)
  159. cycles = vread_tsc();
  160. #ifdef CONFIG_PARAVIRT_CLOCK
  161. else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
  162. cycles = vread_pvclock(mode);
  163. #endif
  164. #ifdef CONFIG_HYPERV_TSCPAGE
  165. else if (gtod->vclock_mode == VCLOCK_HVCLOCK)
  166. cycles = vread_hvclock(mode);
  167. #endif
  168. else
  169. return 0;
  170. v = (cycles - gtod->cycle_last) & gtod->mask;
  171. return v * gtod->mult;
  172. }
  173. /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
  174. notrace static int __always_inline do_realtime(struct timespec *ts)
  175. {
  176. unsigned long seq;
  177. u64 ns;
  178. int mode;
  179. do {
  180. seq = gtod_read_begin(gtod);
  181. mode = gtod->vclock_mode;
  182. ts->tv_sec = gtod->wall_time_sec;
  183. ns = gtod->wall_time_snsec;
  184. ns += vgetsns(&mode);
  185. ns >>= gtod->shift;
  186. } while (unlikely(gtod_read_retry(gtod, seq)));
  187. ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  188. ts->tv_nsec = ns;
  189. return mode;
  190. }
  191. notrace static int __always_inline do_monotonic(struct timespec *ts)
  192. {
  193. unsigned long seq;
  194. u64 ns;
  195. int mode;
  196. do {
  197. seq = gtod_read_begin(gtod);
  198. mode = gtod->vclock_mode;
  199. ts->tv_sec = gtod->monotonic_time_sec;
  200. ns = gtod->monotonic_time_snsec;
  201. ns += vgetsns(&mode);
  202. ns >>= gtod->shift;
  203. } while (unlikely(gtod_read_retry(gtod, seq)));
  204. ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  205. ts->tv_nsec = ns;
  206. return mode;
  207. }
  208. notrace static void do_realtime_coarse(struct timespec *ts)
  209. {
  210. unsigned long seq;
  211. do {
  212. seq = gtod_read_begin(gtod);
  213. ts->tv_sec = gtod->wall_time_coarse_sec;
  214. ts->tv_nsec = gtod->wall_time_coarse_nsec;
  215. } while (unlikely(gtod_read_retry(gtod, seq)));
  216. }
  217. notrace static void do_monotonic_coarse(struct timespec *ts)
  218. {
  219. unsigned long seq;
  220. do {
  221. seq = gtod_read_begin(gtod);
  222. ts->tv_sec = gtod->monotonic_time_coarse_sec;
  223. ts->tv_nsec = gtod->monotonic_time_coarse_nsec;
  224. } while (unlikely(gtod_read_retry(gtod, seq)));
  225. }
  226. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  227. {
  228. switch (clock) {
  229. case CLOCK_REALTIME:
  230. if (do_realtime(ts) == VCLOCK_NONE)
  231. goto fallback;
  232. break;
  233. case CLOCK_MONOTONIC:
  234. if (do_monotonic(ts) == VCLOCK_NONE)
  235. goto fallback;
  236. break;
  237. case CLOCK_REALTIME_COARSE:
  238. do_realtime_coarse(ts);
  239. break;
  240. case CLOCK_MONOTONIC_COARSE:
  241. do_monotonic_coarse(ts);
  242. break;
  243. default:
  244. goto fallback;
  245. }
  246. return 0;
  247. fallback:
  248. return vdso_fallback_gettime(clock, ts);
  249. }
  250. int clock_gettime(clockid_t, struct timespec *)
  251. __attribute__((weak, alias("__vdso_clock_gettime")));
  252. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  253. {
  254. if (likely(tv != NULL)) {
  255. if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
  256. return vdso_fallback_gtod(tv, tz);
  257. tv->tv_usec /= 1000;
  258. }
  259. if (unlikely(tz != NULL)) {
  260. tz->tz_minuteswest = gtod->tz_minuteswest;
  261. tz->tz_dsttime = gtod->tz_dsttime;
  262. }
  263. return 0;
  264. }
  265. int gettimeofday(struct timeval *, struct timezone *)
  266. __attribute__((weak, alias("__vdso_gettimeofday")));
  267. /*
  268. * This will break when the xtime seconds get inaccurate, but that is
  269. * unlikely
  270. */
  271. notrace time_t __vdso_time(time_t *t)
  272. {
  273. /* This is atomic on x86 so we don't need any locks. */
  274. time_t result = READ_ONCE(gtod->wall_time_sec);
  275. if (t)
  276. *t = result;
  277. return result;
  278. }
  279. time_t time(time_t *t)
  280. __attribute__((weak, alias("__vdso_time")));