datapage.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __VDSO_DATAPAGE_H
  3. #define __VDSO_DATAPAGE_H
  4. #ifndef __ASSEMBLY__
  5. #include <linux/compiler.h>
  6. #include <uapi/linux/bits.h>
  7. #include <uapi/linux/time.h>
  8. #include <uapi/linux/types.h>
  9. #include <uapi/asm-generic/errno-base.h>
  10. #include <vdso/align.h>
  11. #include <vdso/bits.h>
  12. #include <vdso/cache.h>
  13. #include <vdso/clocksource.h>
  14. #include <vdso/ktime.h>
  15. #include <vdso/limits.h>
  16. #include <vdso/math64.h>
  17. #include <vdso/page.h>
  18. #include <vdso/processor.h>
  19. #include <vdso/time.h>
  20. #include <vdso/time32.h>
  21. #include <vdso/time64.h>
  22. #ifdef CONFIG_ARCH_HAS_VDSO_TIME_DATA
  23. #include <asm/vdso/time_data.h>
  24. #else
  25. struct arch_vdso_time_data {};
  26. #endif
  27. #if defined(CONFIG_ARCH_HAS_VDSO_ARCH_DATA)
  28. #include <asm/vdso/arch_data.h>
  29. #else
  30. struct vdso_arch_data {
  31. /* Needed for the generic code, never actually used at runtime */
  32. char __unused;
  33. };
  34. #endif
  35. #define VDSO_BASES (CLOCK_TAI + 1)
  36. #define VDSO_BASE_AUX 0
  37. #define VDSO_HRES (BIT(CLOCK_REALTIME) | \
  38. BIT(CLOCK_MONOTONIC) | \
  39. BIT(CLOCK_BOOTTIME) | \
  40. BIT(CLOCK_TAI))
  41. #define VDSO_COARSE (BIT(CLOCK_REALTIME_COARSE) | \
  42. BIT(CLOCK_MONOTONIC_COARSE))
  43. #define VDSO_RAW (BIT(CLOCK_MONOTONIC_RAW))
  44. #define VDSO_AUX __GENMASK(CLOCK_AUX_LAST, CLOCK_AUX)
  45. #define CS_HRES_COARSE 0
  46. #define CS_RAW 1
  47. #define CS_BASES (CS_RAW + 1)
  48. /**
  49. * struct vdso_timestamp - basetime per clock_id
  50. * @sec: seconds
  51. * @nsec: nanoseconds
  52. *
  53. * There is one vdso_timestamp object in vvar for each vDSO-accelerated
  54. * clock_id. For high-resolution clocks, this encodes the time
  55. * corresponding to vdso_time_data.cycle_last. For coarse clocks this encodes
  56. * the actual time.
  57. *
  58. * To be noticed that for highres clocks nsec is left-shifted by
  59. * vdso_time_data[x].shift.
  60. */
  61. struct vdso_timestamp {
  62. u64 sec;
  63. u64 nsec;
  64. };
  65. /**
  66. * struct vdso_clock - vdso per clocksource datapage representation
  67. * @seq: timebase sequence counter
  68. * @clock_mode: clock mode
  69. * @cycle_last: timebase at clocksource init
  70. * @max_cycles: maximum cycles which won't overflow 64bit multiplication
  71. * @mask: clocksource mask
  72. * @mult: clocksource multiplier
  73. * @shift: clocksource shift
  74. * @basetime[clock_id]: basetime per clock_id
  75. * @offset[clock_id]: time namespace offset per clock_id
  76. *
  77. * See also struct vdso_time_data for basic access and ordering information as
  78. * struct vdso_clock is used there.
  79. *
  80. * @basetime is used to store the base time for the system wide time getter
  81. * VVAR page.
  82. *
  83. * @offset is used by the special time namespace VVAR pages which are
  84. * installed instead of the real VVAR page. These namespace pages must set
  85. * @seq to 1 and @clock_mode to VDSO_CLOCKMODE_TIMENS to force the code into
  86. * the time namespace slow path. The namespace aware functions retrieve the
  87. * real system wide VVAR page, read host time and add the per clock offset.
  88. * For clocks which are not affected by time namespace adjustment the
  89. * offset must be zero.
  90. */
  91. struct vdso_clock {
  92. u32 seq;
  93. s32 clock_mode;
  94. u64 cycle_last;
  95. #ifdef CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT
  96. u64 max_cycles;
  97. #endif
  98. u64 mask;
  99. u32 mult;
  100. u32 shift;
  101. union {
  102. struct vdso_timestamp basetime[VDSO_BASES];
  103. struct timens_offset offset[VDSO_BASES];
  104. };
  105. };
  106. /**
  107. * struct vdso_time_data - vdso datapage representation
  108. * @arch_data: architecture specific data (optional, defaults
  109. * to an empty struct)
  110. * @clock_data: clocksource related data (array)
  111. * @aux_clock_data: auxiliary clocksource related data (array)
  112. * @tz_minuteswest: minutes west of Greenwich
  113. * @tz_dsttime: type of DST correction
  114. * @hrtimer_res: hrtimer resolution
  115. * @__unused: unused
  116. *
  117. * vdso_time_data will be accessed by 64 bit and compat code at the same time
  118. * so we should be careful before modifying this structure.
  119. *
  120. * The ordering of the struct members is optimized to have fast acces to the
  121. * often required struct members which are related to CLOCK_REALTIME and
  122. * CLOCK_MONOTONIC. This information is stored in the first cache lines.
  123. */
  124. struct vdso_time_data {
  125. struct arch_vdso_time_data arch_data;
  126. struct vdso_clock clock_data[CS_BASES];
  127. struct vdso_clock aux_clock_data[MAX_AUX_CLOCKS];
  128. s32 tz_minuteswest;
  129. s32 tz_dsttime;
  130. u32 hrtimer_res;
  131. u32 __unused;
  132. } ____cacheline_aligned;
  133. /**
  134. * struct vdso_rng_data - vdso RNG state information
  135. * @generation: counter representing the number of RNG reseeds
  136. * @is_ready: boolean signaling whether the RNG is initialized
  137. */
  138. struct vdso_rng_data {
  139. u64 generation;
  140. u8 is_ready;
  141. };
  142. /*
  143. * We use the hidden visibility to prevent the compiler from generating a GOT
  144. * relocation. Not only is going through a GOT useless (the entry couldn't and
  145. * must not be overridden by another library), it does not even work: the linker
  146. * cannot generate an absolute address to the data page.
  147. *
  148. * With the hidden visibility, the compiler simply generates a PC-relative
  149. * relocation, and this is what we need.
  150. */
  151. extern struct vdso_time_data vdso_u_time_data __attribute__((visibility("hidden")));
  152. extern struct vdso_rng_data vdso_u_rng_data __attribute__((visibility("hidden")));
  153. extern struct vdso_arch_data vdso_u_arch_data __attribute__((visibility("hidden")));
  154. extern struct vdso_time_data *vdso_k_time_data;
  155. extern struct vdso_rng_data *vdso_k_rng_data;
  156. extern struct vdso_arch_data *vdso_k_arch_data;
  157. #define VDSO_ARCH_DATA_SIZE ALIGN(sizeof(struct vdso_arch_data), PAGE_SIZE)
  158. #define VDSO_ARCH_DATA_PAGES (VDSO_ARCH_DATA_SIZE >> PAGE_SHIFT)
  159. enum vdso_pages {
  160. VDSO_TIME_PAGE_OFFSET,
  161. VDSO_TIMENS_PAGE_OFFSET,
  162. VDSO_RNG_PAGE_OFFSET,
  163. VDSO_ARCH_PAGES_START,
  164. VDSO_ARCH_PAGES_END = VDSO_ARCH_PAGES_START + VDSO_ARCH_DATA_PAGES - 1,
  165. VDSO_NR_PAGES
  166. };
  167. /*
  168. * The generic vDSO implementation requires that gettimeofday.h
  169. * provides:
  170. * - __arch_get_hw_counter(): to get the hw counter based on the
  171. * clock_mode.
  172. * - gettimeofday_fallback(): fallback for gettimeofday.
  173. * - clock_gettime_fallback(): fallback for clock_gettime.
  174. * - clock_getres_fallback(): fallback for clock_getres.
  175. */
  176. #include <asm/vdso/gettimeofday.h>
  177. #else /* !__ASSEMBLY__ */
  178. #ifdef CONFIG_VDSO_GETRANDOM
  179. #define __vdso_u_rng_data PROVIDE(vdso_u_rng_data = vdso_u_data + 2 * PAGE_SIZE);
  180. #else
  181. #define __vdso_u_rng_data
  182. #endif
  183. #ifdef CONFIG_ARCH_HAS_VDSO_ARCH_DATA
  184. #define __vdso_u_arch_data PROVIDE(vdso_u_arch_data = vdso_u_data + 3 * PAGE_SIZE);
  185. #else
  186. #define __vdso_u_arch_data
  187. #endif
  188. #define VDSO_VVAR_SYMS \
  189. PROVIDE(vdso_u_data = . - __VDSO_PAGES * PAGE_SIZE); \
  190. PROVIDE(vdso_u_time_data = vdso_u_data); \
  191. __vdso_u_rng_data \
  192. __vdso_u_arch_data \
  193. #endif /* !__ASSEMBLY__ */
  194. #endif /* __VDSO_DATAPAGE_H */