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