vclock_gettime.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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;
  145. u64 last;
  146. /*
  147. * Empirically, a fence (of type that depends on the CPU)
  148. * before rdtsc is enough to ensure that rdtsc is ordered
  149. * with respect to loads. The various CPU manuals are unclear
  150. * as to whether rdtsc can be reordered with later loads,
  151. * but no one has ever seen it happen.
  152. */
  153. rdtsc_barrier();
  154. ret = (cycle_t)__native_read_tsc();
  155. last = gtod->cycle_last;
  156. if (likely(ret >= last))
  157. return ret;
  158. /*
  159. * GCC likes to generate cmov here, but this branch is extremely
  160. * predictable (it's just a funciton of time and the likely is
  161. * very likely) and there's a data dependence, so force GCC
  162. * to generate a branch instead. I don't barrier() because
  163. * we don't actually need a barrier, and if this function
  164. * ever gets inlined it will generate worse code.
  165. */
  166. asm volatile ("");
  167. return last;
  168. }
  169. notrace static inline u64 vgetsns(int *mode)
  170. {
  171. u64 v;
  172. cycles_t cycles;
  173. if (gtod->vclock_mode == VCLOCK_TSC)
  174. cycles = vread_tsc();
  175. #ifdef CONFIG_HPET_TIMER
  176. else if (gtod->vclock_mode == VCLOCK_HPET)
  177. cycles = vread_hpet();
  178. #endif
  179. #ifdef CONFIG_PARAVIRT_CLOCK
  180. else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
  181. cycles = vread_pvclock(mode);
  182. #endif
  183. else
  184. return 0;
  185. v = (cycles - gtod->cycle_last) & gtod->mask;
  186. return v * gtod->mult;
  187. }
  188. /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
  189. notrace static int __always_inline do_realtime(struct timespec *ts)
  190. {
  191. unsigned long seq;
  192. u64 ns;
  193. int mode;
  194. do {
  195. seq = gtod_read_begin(gtod);
  196. mode = gtod->vclock_mode;
  197. ts->tv_sec = gtod->wall_time_sec;
  198. ns = gtod->wall_time_snsec;
  199. ns += vgetsns(&mode);
  200. ns >>= gtod->shift;
  201. } while (unlikely(gtod_read_retry(gtod, seq)));
  202. ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  203. ts->tv_nsec = ns;
  204. return mode;
  205. }
  206. notrace static int __always_inline do_monotonic(struct timespec *ts)
  207. {
  208. unsigned long seq;
  209. u64 ns;
  210. int mode;
  211. do {
  212. seq = gtod_read_begin(gtod);
  213. mode = gtod->vclock_mode;
  214. ts->tv_sec = gtod->monotonic_time_sec;
  215. ns = gtod->monotonic_time_snsec;
  216. ns += vgetsns(&mode);
  217. ns >>= gtod->shift;
  218. } while (unlikely(gtod_read_retry(gtod, seq)));
  219. ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  220. ts->tv_nsec = ns;
  221. return mode;
  222. }
  223. notrace static void do_realtime_coarse(struct timespec *ts)
  224. {
  225. unsigned long seq;
  226. do {
  227. seq = gtod_read_begin(gtod);
  228. ts->tv_sec = gtod->wall_time_coarse_sec;
  229. ts->tv_nsec = gtod->wall_time_coarse_nsec;
  230. } while (unlikely(gtod_read_retry(gtod, seq)));
  231. }
  232. notrace static void do_monotonic_coarse(struct timespec *ts)
  233. {
  234. unsigned long seq;
  235. do {
  236. seq = gtod_read_begin(gtod);
  237. ts->tv_sec = gtod->monotonic_time_coarse_sec;
  238. ts->tv_nsec = gtod->monotonic_time_coarse_nsec;
  239. } while (unlikely(gtod_read_retry(gtod, seq)));
  240. }
  241. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  242. {
  243. switch (clock) {
  244. case CLOCK_REALTIME:
  245. if (do_realtime(ts) == VCLOCK_NONE)
  246. goto fallback;
  247. break;
  248. case CLOCK_MONOTONIC:
  249. if (do_monotonic(ts) == VCLOCK_NONE)
  250. goto fallback;
  251. break;
  252. case CLOCK_REALTIME_COARSE:
  253. do_realtime_coarse(ts);
  254. break;
  255. case CLOCK_MONOTONIC_COARSE:
  256. do_monotonic_coarse(ts);
  257. break;
  258. default:
  259. goto fallback;
  260. }
  261. return 0;
  262. fallback:
  263. return vdso_fallback_gettime(clock, ts);
  264. }
  265. int clock_gettime(clockid_t, struct timespec *)
  266. __attribute__((weak, alias("__vdso_clock_gettime")));
  267. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  268. {
  269. if (likely(tv != NULL)) {
  270. if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
  271. return vdso_fallback_gtod(tv, tz);
  272. tv->tv_usec /= 1000;
  273. }
  274. if (unlikely(tz != NULL)) {
  275. tz->tz_minuteswest = gtod->tz_minuteswest;
  276. tz->tz_dsttime = gtod->tz_dsttime;
  277. }
  278. return 0;
  279. }
  280. int gettimeofday(struct timeval *, struct timezone *)
  281. __attribute__((weak, alias("__vdso_gettimeofday")));
  282. /*
  283. * This will break when the xtime seconds get inaccurate, but that is
  284. * unlikely
  285. */
  286. notrace time_t __vdso_time(time_t *t)
  287. {
  288. /* This is atomic on x86 so we don't need any locks. */
  289. time_t result = ACCESS_ONCE(gtod->wall_time_sec);
  290. if (t)
  291. *t = result;
  292. return result;
  293. }
  294. int time(time_t *t)
  295. __attribute__((weak, alias("__vdso_time")));