vclock_gettime.c 8.3 KB

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