vclock_gettime.c 7.2 KB

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