vclock_gettime.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #ifndef BUILD_VDSO32
  34. #include <linux/kernel.h>
  35. #include <asm/vsyscall.h>
  36. #include <asm/fixmap.h>
  37. #include <asm/pvclock.h>
  38. notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
  39. {
  40. long ret;
  41. asm("syscall" : "=a" (ret) :
  42. "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory");
  43. return ret;
  44. }
  45. notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
  46. {
  47. long ret;
  48. asm("syscall" : "=a" (ret) :
  49. "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
  50. return ret;
  51. }
  52. #ifdef CONFIG_PARAVIRT_CLOCK
  53. static notrace const struct pvclock_vsyscall_time_info *get_pvti(int cpu)
  54. {
  55. const struct pvclock_vsyscall_time_info *pvti_base;
  56. int idx = cpu / (PAGE_SIZE/PVTI_SIZE);
  57. int offset = cpu % (PAGE_SIZE/PVTI_SIZE);
  58. BUG_ON(PVCLOCK_FIXMAP_BEGIN + idx > PVCLOCK_FIXMAP_END);
  59. pvti_base = (struct pvclock_vsyscall_time_info *)
  60. __fix_to_virt(PVCLOCK_FIXMAP_BEGIN+idx);
  61. return &pvti_base[offset];
  62. }
  63. static notrace cycle_t vread_pvclock(int *mode)
  64. {
  65. const struct pvclock_vsyscall_time_info *pvti;
  66. cycle_t ret;
  67. u64 last;
  68. u32 version;
  69. u8 flags;
  70. unsigned cpu, cpu1;
  71. /*
  72. * Note: hypervisor must guarantee that:
  73. * 1. cpu ID number maps 1:1 to per-CPU pvclock time info.
  74. * 2. that per-CPU pvclock time info is updated if the
  75. * underlying CPU changes.
  76. * 3. that version is increased whenever underlying CPU
  77. * changes.
  78. *
  79. */
  80. do {
  81. cpu = __getcpu() & VGETCPU_CPU_MASK;
  82. /* TODO: We can put vcpu id into higher bits of pvti.version.
  83. * This will save a couple of cycles by getting rid of
  84. * __getcpu() calls (Gleb).
  85. */
  86. pvti = get_pvti(cpu);
  87. version = __pvclock_read_cycles(&pvti->pvti, &ret, &flags);
  88. /*
  89. * Test we're still on the cpu as well as the version.
  90. * We could have been migrated just after the first
  91. * vgetcpu but before fetching the version, so we
  92. * wouldn't notice a version change.
  93. */
  94. cpu1 = __getcpu() & VGETCPU_CPU_MASK;
  95. } while (unlikely(cpu != cpu1 ||
  96. (pvti->pvti.version & 1) ||
  97. pvti->pvti.version != version));
  98. if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT)))
  99. *mode = VCLOCK_NONE;
  100. /* refer to tsc.c read_tsc() comment for rationale */
  101. last = gtod->cycle_last;
  102. if (likely(ret >= last))
  103. return ret;
  104. return last;
  105. }
  106. #endif
  107. #else
  108. notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
  109. {
  110. long ret;
  111. asm(
  112. "mov %%ebx, %%edx \n"
  113. "mov %2, %%ebx \n"
  114. "call __kernel_vsyscall \n"
  115. "mov %%edx, %%ebx \n"
  116. : "=a" (ret)
  117. : "0" (__NR_clock_gettime), "g" (clock), "c" (ts)
  118. : "memory", "edx");
  119. return ret;
  120. }
  121. notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
  122. {
  123. long ret;
  124. asm(
  125. "mov %%ebx, %%edx \n"
  126. "mov %2, %%ebx \n"
  127. "call __kernel_vsyscall \n"
  128. "mov %%edx, %%ebx \n"
  129. : "=a" (ret)
  130. : "0" (__NR_gettimeofday), "g" (tv), "c" (tz)
  131. : "memory", "edx");
  132. return ret;
  133. }
  134. #ifdef CONFIG_PARAVIRT_CLOCK
  135. static notrace cycle_t vread_pvclock(int *mode)
  136. {
  137. *mode = VCLOCK_NONE;
  138. return 0;
  139. }
  140. #endif
  141. #endif
  142. notrace static cycle_t vread_tsc(void)
  143. {
  144. cycle_t ret = (cycle_t)rdtsc_ordered();
  145. u64 last = gtod->cycle_last;
  146. if (likely(ret >= last))
  147. return ret;
  148. /*
  149. * GCC likes to generate cmov here, but this branch is extremely
  150. * predictable (it's just a funciton of time and the likely is
  151. * very likely) and there's a data dependence, so force GCC
  152. * to generate a branch instead. I don't barrier() because
  153. * we don't actually need a barrier, and if this function
  154. * ever gets inlined it will generate worse code.
  155. */
  156. asm volatile ("");
  157. return last;
  158. }
  159. notrace static inline u64 vgetsns(int *mode)
  160. {
  161. u64 v;
  162. cycles_t cycles;
  163. if (gtod->vclock_mode == VCLOCK_TSC)
  164. cycles = vread_tsc();
  165. #ifdef CONFIG_HPET_TIMER
  166. else if (gtod->vclock_mode == VCLOCK_HPET)
  167. cycles = vread_hpet();
  168. #endif
  169. #ifdef CONFIG_PARAVIRT_CLOCK
  170. else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
  171. cycles = vread_pvclock(mode);
  172. #endif
  173. else
  174. return 0;
  175. v = (cycles - gtod->cycle_last) & gtod->mask;
  176. return v * gtod->mult;
  177. }
  178. /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
  179. notrace static int __always_inline do_realtime(struct timespec *ts)
  180. {
  181. unsigned long seq;
  182. u64 ns;
  183. int mode;
  184. do {
  185. seq = gtod_read_begin(gtod);
  186. mode = gtod->vclock_mode;
  187. ts->tv_sec = gtod->wall_time_sec;
  188. ns = gtod->wall_time_snsec;
  189. ns += vgetsns(&mode);
  190. ns >>= gtod->shift;
  191. } while (unlikely(gtod_read_retry(gtod, seq)));
  192. ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  193. ts->tv_nsec = ns;
  194. return mode;
  195. }
  196. notrace static int __always_inline do_monotonic(struct timespec *ts)
  197. {
  198. unsigned long seq;
  199. u64 ns;
  200. int mode;
  201. do {
  202. seq = gtod_read_begin(gtod);
  203. mode = gtod->vclock_mode;
  204. ts->tv_sec = gtod->monotonic_time_sec;
  205. ns = gtod->monotonic_time_snsec;
  206. ns += vgetsns(&mode);
  207. ns >>= gtod->shift;
  208. } while (unlikely(gtod_read_retry(gtod, seq)));
  209. ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  210. ts->tv_nsec = ns;
  211. return mode;
  212. }
  213. notrace static void do_realtime_coarse(struct timespec *ts)
  214. {
  215. unsigned long seq;
  216. do {
  217. seq = gtod_read_begin(gtod);
  218. ts->tv_sec = gtod->wall_time_coarse_sec;
  219. ts->tv_nsec = gtod->wall_time_coarse_nsec;
  220. } while (unlikely(gtod_read_retry(gtod, seq)));
  221. }
  222. notrace static void do_monotonic_coarse(struct timespec *ts)
  223. {
  224. unsigned long seq;
  225. do {
  226. seq = gtod_read_begin(gtod);
  227. ts->tv_sec = gtod->monotonic_time_coarse_sec;
  228. ts->tv_nsec = gtod->monotonic_time_coarse_nsec;
  229. } while (unlikely(gtod_read_retry(gtod, seq)));
  230. }
  231. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  232. {
  233. switch (clock) {
  234. case CLOCK_REALTIME:
  235. if (do_realtime(ts) == VCLOCK_NONE)
  236. goto fallback;
  237. break;
  238. case CLOCK_MONOTONIC:
  239. if (do_monotonic(ts) == VCLOCK_NONE)
  240. goto fallback;
  241. break;
  242. case CLOCK_REALTIME_COARSE:
  243. do_realtime_coarse(ts);
  244. break;
  245. case CLOCK_MONOTONIC_COARSE:
  246. do_monotonic_coarse(ts);
  247. break;
  248. default:
  249. goto fallback;
  250. }
  251. return 0;
  252. fallback:
  253. return vdso_fallback_gettime(clock, ts);
  254. }
  255. int clock_gettime(clockid_t, struct timespec *)
  256. __attribute__((weak, alias("__vdso_clock_gettime")));
  257. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  258. {
  259. if (likely(tv != NULL)) {
  260. if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
  261. return vdso_fallback_gtod(tv, tz);
  262. tv->tv_usec /= 1000;
  263. }
  264. if (unlikely(tz != NULL)) {
  265. tz->tz_minuteswest = gtod->tz_minuteswest;
  266. tz->tz_dsttime = gtod->tz_dsttime;
  267. }
  268. return 0;
  269. }
  270. int gettimeofday(struct timeval *, struct timezone *)
  271. __attribute__((weak, alias("__vdso_gettimeofday")));
  272. /*
  273. * This will break when the xtime seconds get inaccurate, but that is
  274. * unlikely
  275. */
  276. notrace time_t __vdso_time(time_t *t)
  277. {
  278. /* This is atomic on x86 so we don't need any locks. */
  279. time_t result = ACCESS_ONCE(gtod->wall_time_sec);
  280. if (t)
  281. *t = result;
  282. return result;
  283. }
  284. int time(time_t *t)
  285. __attribute__((weak, alias("__vdso_time")));