1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/cache.h>
21#include <linux/elf.h>
22#include <linux/err.h>
23#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/of.h>
26#include <linux/printk.h>
27#include <linux/slab.h>
28#include <linux/timekeeper_internal.h>
29#include <linux/vmalloc.h>
30#include <asm/arch_timer.h>
31#include <asm/barrier.h>
32#include <asm/cacheflush.h>
33#include <asm/page.h>
34#include <asm/vdso.h>
35#include <asm/vdso_datapage.h>
36#include <clocksource/arm_arch_timer.h>
37
38#define MAX_SYMNAME 64
39
40static struct page **vdso_text_pagelist;
41
42
43unsigned int vdso_total_pages __ro_after_init;
44
45
46
47
48static union vdso_data_store vdso_data_store __page_aligned_data;
49static struct vdso_data *vdso_data = &vdso_data_store.data;
50
51static struct page *vdso_data_page __ro_after_init;
52static const struct vm_special_mapping vdso_data_mapping = {
53 .name = "[vvar]",
54 .pages = &vdso_data_page,
55};
56
57static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
58 .name = "[vdso]",
59};
60
61struct elfinfo {
62 Elf32_Ehdr *hdr;
63 Elf32_Sym *dynsym;
64 unsigned long dynsymsize;
65 char *dynstr;
66};
67
68
69
70
71static bool cntvct_ok __ro_after_init;
72
73static bool __init cntvct_functional(void)
74{
75 struct device_node *np;
76 bool ret = false;
77
78 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
79 goto out;
80
81
82
83
84
85 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
86 if (!np)
87 goto out_put;
88
89 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
90 goto out_put;
91
92 ret = true;
93
94out_put:
95 of_node_put(np);
96out:
97 return ret;
98}
99
100static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
101 unsigned long *size)
102{
103 Elf32_Shdr *sechdrs;
104 unsigned int i;
105 char *secnames;
106
107
108 sechdrs = (void *)ehdr + ehdr->e_shoff;
109 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
110
111
112 for (i = 1; i < ehdr->e_shnum; i++) {
113 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
114 if (size)
115 *size = sechdrs[i].sh_size;
116 return (void *)ehdr + sechdrs[i].sh_offset;
117 }
118 }
119
120 if (size)
121 *size = 0;
122 return NULL;
123}
124
125static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
126{
127 unsigned int i;
128
129 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
130 char name[MAX_SYMNAME], *c;
131
132 if (lib->dynsym[i].st_name == 0)
133 continue;
134 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
135 MAX_SYMNAME);
136 c = strchr(name, '@');
137 if (c)
138 *c = 0;
139 if (strcmp(symname, name) == 0)
140 return &lib->dynsym[i];
141 }
142 return NULL;
143}
144
145static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
146{
147 Elf32_Sym *sym;
148
149 sym = find_symbol(lib, symname);
150 if (!sym)
151 return;
152
153 sym->st_name = 0;
154}
155
156static void __init patch_vdso(void *ehdr)
157{
158 struct elfinfo einfo;
159
160 einfo = (struct elfinfo) {
161 .hdr = ehdr,
162 };
163
164 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
165 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
166
167
168
169
170
171 if (!cntvct_ok) {
172 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
173 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
174 }
175}
176
177static int __init vdso_init(void)
178{
179 unsigned int text_pages;
180 int i;
181
182 if (memcmp(&vdso_start, "\177ELF", 4)) {
183 pr_err("VDSO is not a valid ELF object!\n");
184 return -ENOEXEC;
185 }
186
187 text_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
188 pr_debug("vdso: %i text pages at base %p\n", text_pages, &vdso_start);
189
190
191 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
192 GFP_KERNEL);
193 if (vdso_text_pagelist == NULL)
194 return -ENOMEM;
195
196
197 vdso_data_page = virt_to_page(vdso_data);
198
199
200 for (i = 0; i < text_pages; i++) {
201 struct page *page;
202
203 page = virt_to_page(&vdso_start + i * PAGE_SIZE);
204 vdso_text_pagelist[i] = page;
205 }
206
207 vdso_text_mapping.pages = vdso_text_pagelist;
208
209 vdso_total_pages = 1;
210 vdso_total_pages += text_pages;
211
212 cntvct_ok = cntvct_functional();
213
214 patch_vdso(&vdso_start);
215
216 return 0;
217}
218arch_initcall(vdso_init);
219
220static int install_vvar(struct mm_struct *mm, unsigned long addr)
221{
222 struct vm_area_struct *vma;
223
224 vma = _install_special_mapping(mm, addr, PAGE_SIZE,
225 VM_READ | VM_MAYREAD,
226 &vdso_data_mapping);
227
228 return PTR_ERR_OR_ZERO(vma);
229}
230
231
232void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
233{
234 struct vm_area_struct *vma;
235 unsigned long len;
236
237 mm->context.vdso = 0;
238
239 if (vdso_text_pagelist == NULL)
240 return;
241
242 if (install_vvar(mm, addr))
243 return;
244
245
246 addr += PAGE_SIZE;
247 len = (vdso_total_pages - 1) << PAGE_SHIFT;
248
249 vma = _install_special_mapping(mm, addr, len,
250 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
251 &vdso_text_mapping);
252
253 if (!IS_ERR(vma))
254 mm->context.vdso = addr;
255}
256
257static void vdso_write_begin(struct vdso_data *vdata)
258{
259 ++vdso_data->seq_count;
260 smp_wmb();
261}
262
263static void vdso_write_end(struct vdso_data *vdata)
264{
265 smp_wmb();
266 ++vdso_data->seq_count;
267}
268
269static bool tk_is_cntvct(const struct timekeeper *tk)
270{
271 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
272 return false;
273
274 if (!tk->tkr_mono.clock->archdata.vdso_direct)
275 return false;
276
277 return true;
278}
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298void update_vsyscall(struct timekeeper *tk)
299{
300 struct timespec64 *wtm = &tk->wall_to_monotonic;
301
302 if (!cntvct_ok) {
303
304
305
306 return;
307 }
308
309 vdso_write_begin(vdso_data);
310
311 vdso_data->tk_is_cntvct = tk_is_cntvct(tk);
312 vdso_data->xtime_coarse_sec = tk->xtime_sec;
313 vdso_data->xtime_coarse_nsec = (u32)(tk->tkr_mono.xtime_nsec >>
314 tk->tkr_mono.shift);
315 vdso_data->wtm_clock_sec = wtm->tv_sec;
316 vdso_data->wtm_clock_nsec = wtm->tv_nsec;
317
318 if (vdso_data->tk_is_cntvct) {
319 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
320 vdso_data->xtime_clock_sec = tk->xtime_sec;
321 vdso_data->xtime_clock_snsec = tk->tkr_mono.xtime_nsec;
322 vdso_data->cs_mult = tk->tkr_mono.mult;
323 vdso_data->cs_shift = tk->tkr_mono.shift;
324 vdso_data->cs_mask = tk->tkr_mono.mask;
325 }
326
327 vdso_write_end(vdso_data);
328
329 flush_dcache_page(virt_to_page(vdso_data));
330}
331
332void update_vsyscall_tz(void)
333{
334 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
335 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
336 flush_dcache_page(virt_to_page(vdso_data));
337}
338