vclock_gettime.c 8.0 KB

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