datapage.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/time.h>
  7. #include <uapi/linux/types.h>
  8. #include <uapi/asm-generic/errno-base.h>
  9. #include <vdso/bits.h>
  10. #include <vdso/clocksource.h>
  11. #include <vdso/ktime.h>
  12. #include <vdso/limits.h>
  13. #include <vdso/math64.h>
  14. #include <vdso/processor.h>
  15. #include <vdso/time.h>
  16. #include <vdso/time32.h>
  17. #include <vdso/time64.h>
  18. #ifdef CONFIG_ARCH_HAS_VDSO_DATA
  19. #include <asm/vdso/data.h>
  20. #else
  21. struct arch_vdso_data {};
  22. #endif
  23. #define VDSO_BASES (CLOCK_TAI + 1)
  24. #define VDSO_HRES (BIT(CLOCK_REALTIME) | \
  25. BIT(CLOCK_MONOTONIC) | \
  26. BIT(CLOCK_BOOTTIME) | \
  27. BIT(CLOCK_TAI))
  28. #define VDSO_COARSE (BIT(CLOCK_REALTIME_COARSE) | \
  29. BIT(CLOCK_MONOTONIC_COARSE))
  30. #define VDSO_RAW (BIT(CLOCK_MONOTONIC_RAW))
  31. #define CS_HRES_COARSE 0
  32. #define CS_RAW 1
  33. #define CS_BASES (CS_RAW + 1)
  34. /**
  35. * struct vdso_timestamp - basetime per clock_id
  36. * @sec: seconds
  37. * @nsec: nanoseconds
  38. *
  39. * There is one vdso_timestamp object in vvar for each vDSO-accelerated
  40. * clock_id. For high-resolution clocks, this encodes the time
  41. * corresponding to vdso_data.cycle_last. For coarse clocks this encodes
  42. * the actual time.
  43. *
  44. * To be noticed that for highres clocks nsec is left-shifted by
  45. * vdso_data.cs[x].shift.
  46. */
  47. struct vdso_timestamp {
  48. u64 sec;
  49. u64 nsec;
  50. };
  51. /**
  52. * struct vdso_data - vdso datapage representation
  53. * @seq: timebase sequence counter
  54. * @clock_mode: clock mode
  55. * @cycle_last: timebase at clocksource init
  56. * @max_cycles: maximum cycles which won't overflow 64bit multiplication
  57. * @mask: clocksource mask
  58. * @mult: clocksource multiplier
  59. * @shift: clocksource shift
  60. * @basetime[clock_id]: basetime per clock_id
  61. * @offset[clock_id]: time namespace offset per clock_id
  62. * @tz_minuteswest: minutes west of Greenwich
  63. * @tz_dsttime: type of DST correction
  64. * @hrtimer_res: hrtimer resolution
  65. * @__unused: unused
  66. * @arch_data: architecture specific data (optional, defaults
  67. * to an empty struct)
  68. *
  69. * vdso_data will be accessed by 64 bit and compat code at the same time
  70. * so we should be careful before modifying this structure.
  71. *
  72. * The ordering of the struct members is optimized to have fast access to the
  73. * often required struct members which are related to CLOCK_REALTIME and
  74. * CLOCK_MONOTONIC. This information is stored in the first cache lines.
  75. *
  76. * @basetime is used to store the base time for the system wide time getter
  77. * VVAR page.
  78. *
  79. * @offset is used by the special time namespace VVAR pages which are
  80. * installed instead of the real VVAR page. These namespace pages must set
  81. * @seq to 1 and @clock_mode to VDSO_CLOCKMODE_TIMENS to force the code into
  82. * the time namespace slow path. The namespace aware functions retrieve the
  83. * real system wide VVAR page, read host time and add the per clock offset.
  84. * For clocks which are not affected by time namespace adjustment the
  85. * offset must be zero.
  86. */
  87. struct vdso_data {
  88. u32 seq;
  89. s32 clock_mode;
  90. u64 cycle_last;
  91. #ifdef CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT
  92. u64 max_cycles;
  93. #endif
  94. u64 mask;
  95. u32 mult;
  96. u32 shift;
  97. union {
  98. struct vdso_timestamp basetime[VDSO_BASES];
  99. struct timens_offset offset[VDSO_BASES];
  100. };
  101. s32 tz_minuteswest;
  102. s32 tz_dsttime;
  103. u32 hrtimer_res;
  104. u32 __unused;
  105. struct arch_vdso_data arch_data;
  106. };
  107. /**
  108. * struct vdso_rng_data - vdso RNG state information
  109. * @generation: counter representing the number of RNG reseeds
  110. * @is_ready: boolean signaling whether the RNG is initialized
  111. */
  112. struct vdso_rng_data {
  113. u64 generation;
  114. u8 is_ready;
  115. };
  116. /*
  117. * We use the hidden visibility to prevent the compiler from generating a GOT
  118. * relocation. Not only is going through a GOT useless (the entry couldn't and
  119. * must not be overridden by another library), it does not even work: the linker
  120. * cannot generate an absolute address to the data page.
  121. *
  122. * With the hidden visibility, the compiler simply generates a PC-relative
  123. * relocation, and this is what we need.
  124. */
  125. extern struct vdso_data _vdso_data[CS_BASES] __attribute__((visibility("hidden")));
  126. extern struct vdso_data _timens_data[CS_BASES] __attribute__((visibility("hidden")));
  127. extern struct vdso_rng_data _vdso_rng_data __attribute__((visibility("hidden")));
  128. /**
  129. * union vdso_data_store - Generic vDSO data page
  130. */
  131. union vdso_data_store {
  132. struct vdso_data data[CS_BASES];
  133. u8 page[1U << CONFIG_PAGE_SHIFT];
  134. };
  135. /*
  136. * The generic vDSO implementation requires that gettimeofday.h
  137. * provides:
  138. * - __arch_get_vdso_data(): to get the vdso datapage.
  139. * - __arch_get_hw_counter(): to get the hw counter based on the
  140. * clock_mode.
  141. * - gettimeofday_fallback(): fallback for gettimeofday.
  142. * - clock_gettime_fallback(): fallback for clock_gettime.
  143. * - clock_getres_fallback(): fallback for clock_getres.
  144. */
  145. #ifdef ENABLE_COMPAT_VDSO
  146. #include <asm/vdso/compat_gettimeofday.h>
  147. #else
  148. #include <asm/vdso/gettimeofday.h>
  149. #endif /* ENABLE_COMPAT_VDSO */
  150. #endif /* !__ASSEMBLY__ */
  151. #endif /* __VDSO_DATAPAGE_H */