vclock_gettime.c 8.2 KB

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