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