vclock_gettime.c 8.4 KB

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