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