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