gettimeofday.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Fast user context implementation of clock_gettime, gettimeofday, and time.
  4. *
  5. * Copyright (C) 2019 ARM Limited.
  6. * Copyright 2006 Andi Kleen, SUSE Labs.
  7. * 32 Bit compat layer by Stefani Seibold <[email protected]>
  8. * sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany
  9. */
  10. #ifndef __ASM_VDSO_GETTIMEOFDAY_H
  11. #define __ASM_VDSO_GETTIMEOFDAY_H
  12. #ifndef __ASSEMBLY__
  13. #include <uapi/linux/time.h>
  14. #include <asm/vgtod.h>
  15. #include <asm/vvar.h>
  16. #include <asm/unistd.h>
  17. #include <asm/msr.h>
  18. #include <asm/pvclock.h>
  19. #include <clocksource/hyperv_timer.h>
  20. #define __vdso_data (VVAR(_vdso_data))
  21. #define __timens_vdso_data (TIMENS(_vdso_data))
  22. #define VDSO_HAS_TIME 1
  23. #define VDSO_HAS_CLOCK_GETRES 1
  24. /*
  25. * Declare the memory-mapped vclock data pages. These come from hypervisors.
  26. * If we ever reintroduce something like direct access to an MMIO clock like
  27. * the HPET again, it will go here as well.
  28. *
  29. * A load from any of these pages will segfault if the clock in question is
  30. * disabled, so appropriate compiler barriers and checks need to be used
  31. * to prevent stray loads.
  32. *
  33. * These declarations MUST NOT be const. The compiler will assume that
  34. * an extern const variable has genuinely constant contents, and the
  35. * resulting code won't work, since the whole point is that these pages
  36. * change over time, possibly while we're accessing them.
  37. */
  38. #ifdef CONFIG_PARAVIRT_CLOCK
  39. /*
  40. * This is the vCPU 0 pvclock page. We only use pvclock from the vDSO
  41. * if the hypervisor tells us that all vCPUs can get valid data from the
  42. * vCPU 0 page.
  43. */
  44. extern struct pvclock_vsyscall_time_info pvclock_page
  45. __attribute__((visibility("hidden")));
  46. #endif
  47. #ifdef CONFIG_HYPERV_TIMER
  48. extern struct ms_hyperv_tsc_page hvclock_page
  49. __attribute__((visibility("hidden")));
  50. #endif
  51. #ifdef CONFIG_TIME_NS
  52. static __always_inline
  53. const struct vdso_data *__arch_get_timens_vdso_data(const struct vdso_data *vd)
  54. {
  55. return __timens_vdso_data;
  56. }
  57. #endif
  58. #ifndef BUILD_VDSO32
  59. static __always_inline
  60. long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
  61. {
  62. long ret;
  63. asm ("syscall" : "=a" (ret), "=m" (*_ts) :
  64. "0" (__NR_clock_gettime), "D" (_clkid), "S" (_ts) :
  65. "rcx", "r11");
  66. return ret;
  67. }
  68. static __always_inline
  69. long gettimeofday_fallback(struct __kernel_old_timeval *_tv,
  70. struct timezone *_tz)
  71. {
  72. long ret;
  73. asm("syscall" : "=a" (ret) :
  74. "0" (__NR_gettimeofday), "D" (_tv), "S" (_tz) : "memory");
  75. return ret;
  76. }
  77. static __always_inline
  78. long clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
  79. {
  80. long ret;
  81. asm ("syscall" : "=a" (ret), "=m" (*_ts) :
  82. "0" (__NR_clock_getres), "D" (_clkid), "S" (_ts) :
  83. "rcx", "r11");
  84. return ret;
  85. }
  86. #else
  87. static __always_inline
  88. long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
  89. {
  90. long ret;
  91. asm (
  92. "mov %%ebx, %%edx \n"
  93. "mov %[clock], %%ebx \n"
  94. "call __kernel_vsyscall \n"
  95. "mov %%edx, %%ebx \n"
  96. : "=a" (ret), "=m" (*_ts)
  97. : "0" (__NR_clock_gettime64), [clock] "g" (_clkid), "c" (_ts)
  98. : "edx");
  99. return ret;
  100. }
  101. static __always_inline
  102. long clock_gettime32_fallback(clockid_t _clkid, struct old_timespec32 *_ts)
  103. {
  104. long ret;
  105. asm (
  106. "mov %%ebx, %%edx \n"
  107. "mov %[clock], %%ebx \n"
  108. "call __kernel_vsyscall \n"
  109. "mov %%edx, %%ebx \n"
  110. : "=a" (ret), "=m" (*_ts)
  111. : "0" (__NR_clock_gettime), [clock] "g" (_clkid), "c" (_ts)
  112. : "edx");
  113. return ret;
  114. }
  115. static __always_inline
  116. long gettimeofday_fallback(struct __kernel_old_timeval *_tv,
  117. struct timezone *_tz)
  118. {
  119. long ret;
  120. asm(
  121. "mov %%ebx, %%edx \n"
  122. "mov %2, %%ebx \n"
  123. "call __kernel_vsyscall \n"
  124. "mov %%edx, %%ebx \n"
  125. : "=a" (ret)
  126. : "0" (__NR_gettimeofday), "g" (_tv), "c" (_tz)
  127. : "memory", "edx");
  128. return ret;
  129. }
  130. static __always_inline long
  131. clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
  132. {
  133. long ret;
  134. asm (
  135. "mov %%ebx, %%edx \n"
  136. "mov %[clock], %%ebx \n"
  137. "call __kernel_vsyscall \n"
  138. "mov %%edx, %%ebx \n"
  139. : "=a" (ret), "=m" (*_ts)
  140. : "0" (__NR_clock_getres_time64), [clock] "g" (_clkid), "c" (_ts)
  141. : "edx");
  142. return ret;
  143. }
  144. static __always_inline
  145. long clock_getres32_fallback(clockid_t _clkid, struct old_timespec32 *_ts)
  146. {
  147. long ret;
  148. asm (
  149. "mov %%ebx, %%edx \n"
  150. "mov %[clock], %%ebx \n"
  151. "call __kernel_vsyscall \n"
  152. "mov %%edx, %%ebx \n"
  153. : "=a" (ret), "=m" (*_ts)
  154. : "0" (__NR_clock_getres), [clock] "g" (_clkid), "c" (_ts)
  155. : "edx");
  156. return ret;
  157. }
  158. #endif
  159. #ifdef CONFIG_PARAVIRT_CLOCK
  160. static u64 vread_pvclock(void)
  161. {
  162. const struct pvclock_vcpu_time_info *pvti = &pvclock_page.pvti;
  163. u32 version;
  164. u64 ret;
  165. /*
  166. * Note: The kernel and hypervisor must guarantee that cpu ID
  167. * number maps 1:1 to per-CPU pvclock time info.
  168. *
  169. * Because the hypervisor is entirely unaware of guest userspace
  170. * preemption, it cannot guarantee that per-CPU pvclock time
  171. * info is updated if the underlying CPU changes or that that
  172. * version is increased whenever underlying CPU changes.
  173. *
  174. * On KVM, we are guaranteed that pvti updates for any vCPU are
  175. * atomic as seen by *all* vCPUs. This is an even stronger
  176. * guarantee than we get with a normal seqlock.
  177. *
  178. * On Xen, we don't appear to have that guarantee, but Xen still
  179. * supplies a valid seqlock using the version field.
  180. *
  181. * We only do pvclock vdso timing at all if
  182. * PVCLOCK_TSC_STABLE_BIT is set, and we interpret that bit to
  183. * mean that all vCPUs have matching pvti and that the TSC is
  184. * synced, so we can just look at vCPU 0's pvti.
  185. */
  186. do {
  187. version = pvclock_read_begin(pvti);
  188. if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT)))
  189. return U64_MAX;
  190. ret = __pvclock_read_cycles(pvti, rdtsc_ordered());
  191. } while (pvclock_read_retry(pvti, version));
  192. return ret;
  193. }
  194. #endif
  195. #ifdef CONFIG_HYPERV_TIMER
  196. static u64 vread_hvclock(void)
  197. {
  198. return hv_read_tsc_page(&hvclock_page);
  199. }
  200. #endif
  201. static inline u64 __arch_get_hw_counter(s32 clock_mode,
  202. const struct vdso_data *vd)
  203. {
  204. if (likely(clock_mode == VDSO_CLOCKMODE_TSC))
  205. return (u64)rdtsc_ordered();
  206. /*
  207. * For any memory-mapped vclock type, we need to make sure that gcc
  208. * doesn't cleverly hoist a load before the mode check. Otherwise we
  209. * might end up touching the memory-mapped page even if the vclock in
  210. * question isn't enabled, which will segfault. Hence the barriers.
  211. */
  212. #ifdef CONFIG_PARAVIRT_CLOCK
  213. if (clock_mode == VDSO_CLOCKMODE_PVCLOCK) {
  214. barrier();
  215. return vread_pvclock();
  216. }
  217. #endif
  218. #ifdef CONFIG_HYPERV_TIMER
  219. if (clock_mode == VDSO_CLOCKMODE_HVCLOCK) {
  220. barrier();
  221. return vread_hvclock();
  222. }
  223. #endif
  224. return U64_MAX;
  225. }
  226. static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
  227. {
  228. return __vdso_data;
  229. }
  230. static inline bool arch_vdso_clocksource_ok(const struct vdso_data *vd)
  231. {
  232. return true;
  233. }
  234. #define vdso_clocksource_ok arch_vdso_clocksource_ok
  235. /*
  236. * Clocksource read value validation to handle PV and HyperV clocksources
  237. * which can be invalidated asynchronously and indicate invalidation by
  238. * returning U64_MAX, which can be effectively tested by checking for a
  239. * negative value after casting it to s64.
  240. */
  241. static inline bool arch_vdso_cycles_ok(u64 cycles)
  242. {
  243. return (s64)cycles >= 0;
  244. }
  245. #define vdso_cycles_ok arch_vdso_cycles_ok
  246. /*
  247. * x86 specific delta calculation.
  248. *
  249. * The regular implementation assumes that clocksource reads are globally
  250. * monotonic. The TSC can be slightly off across sockets which can cause
  251. * the regular delta calculation (@cycles - @last) to return a huge time
  252. * jump.
  253. *
  254. * Therefore it needs to be verified that @cycles are greater than
  255. * @last. If not then use @last, which is the base time of the current
  256. * conversion period.
  257. *
  258. * This variant also removes the masking of the subtraction because the
  259. * clocksource mask of all VDSO capable clocksources on x86 is U64_MAX
  260. * which would result in a pointless operation. The compiler cannot
  261. * optimize it away as the mask comes from the vdso data and is not compile
  262. * time constant.
  263. */
  264. static __always_inline
  265. u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult)
  266. {
  267. if (cycles > last)
  268. return (cycles - last) * mult;
  269. return 0;
  270. }
  271. #define vdso_calc_delta vdso_calc_delta
  272. #endif /* !__ASSEMBLY__ */
  273. #endif /* __ASM_VDSO_GETTIMEOFDAY_H */