]> git.ipfire.org Git - thirdparty/linux.git/blame - arch/arm/kernel/vdso.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 234
[thirdparty/linux.git] / arch / arm / kernel / vdso.c
CommitLineData
caab277b 1// SPDX-License-Identifier: GPL-2.0-only
ecf99a43
NL
2/*
3 * Adapted from arm64 version.
4 *
5 * Copyright (C) 2012 ARM Limited
6 * Copyright (C) 2015 Mentor Graphics Corporation.
ecf99a43
NL
7 */
8
92bb8d5d 9#include <linux/cache.h>
ecf99a43
NL
10#include <linux/elf.h>
11#include <linux/err.h>
12#include <linux/kernel.h>
13#include <linux/mm.h>
14#include <linux/of.h>
15#include <linux/printk.h>
16#include <linux/slab.h>
17#include <linux/timekeeper_internal.h>
18#include <linux/vmalloc.h>
19#include <asm/arch_timer.h>
20#include <asm/barrier.h>
21#include <asm/cacheflush.h>
22#include <asm/page.h>
23#include <asm/vdso.h>
24#include <asm/vdso_datapage.h>
25#include <clocksource/arm_arch_timer.h>
26
27#define MAX_SYMNAME 64
28
29static struct page **vdso_text_pagelist;
30
73b9160d
JP
31extern char vdso_start[], vdso_end[];
32
ecf99a43 33/* Total number of pages needed for the data and text portions of the VDSO. */
92bb8d5d 34unsigned int vdso_total_pages __ro_after_init;
ecf99a43
NL
35
36/*
37 * The VDSO data page.
38 */
39static union vdso_data_store vdso_data_store __page_aligned_data;
40static struct vdso_data *vdso_data = &vdso_data_store.data;
41
92bb8d5d
JZ
42static struct page *vdso_data_page __ro_after_init;
43static const struct vm_special_mapping vdso_data_mapping = {
ecf99a43
NL
44 .name = "[vvar]",
45 .pages = &vdso_data_page,
46};
47
280e87e9
DS
48static int vdso_mremap(const struct vm_special_mapping *sm,
49 struct vm_area_struct *new_vma)
50{
51 unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
52 unsigned long vdso_size;
53
54 /* without VVAR page */
55 vdso_size = (vdso_total_pages - 1) << PAGE_SHIFT;
56
57 if (vdso_size != new_size)
58 return -EINVAL;
59
60 current->mm->context.vdso = new_vma->vm_start;
61
62 return 0;
63}
64
92bb8d5d 65static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
ecf99a43 66 .name = "[vdso]",
280e87e9 67 .mremap = vdso_mremap,
ecf99a43
NL
68};
69
70struct elfinfo {
71 Elf32_Ehdr *hdr; /* ptr to ELF */
72 Elf32_Sym *dynsym; /* ptr to .dynsym section */
73 unsigned long dynsymsize; /* size of .dynsym section */
74 char *dynstr; /* ptr to .dynstr section */
75};
76
77/* Cached result of boot-time check for whether the arch timer exists,
78 * and if so, whether the virtual counter is useable.
79 */
92bb8d5d 80static bool cntvct_ok __ro_after_init;
ecf99a43
NL
81
82static bool __init cntvct_functional(void)
83{
84 struct device_node *np;
85 bool ret = false;
86
87 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
88 goto out;
89
90 /* The arm_arch_timer core should export
91 * arch_timer_use_virtual or similar so we don't have to do
92 * this.
93 */
94 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
95 if (!np)
96 goto out_put;
97
98 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
99 goto out_put;
100
101 ret = true;
102
103out_put:
104 of_node_put(np);
105out:
106 return ret;
107}
108
109static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
110 unsigned long *size)
111{
112 Elf32_Shdr *sechdrs;
113 unsigned int i;
114 char *secnames;
115
116 /* Grab section headers and strings so we can tell who is who */
117 sechdrs = (void *)ehdr + ehdr->e_shoff;
118 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
119
120 /* Find the section they want */
121 for (i = 1; i < ehdr->e_shnum; i++) {
122 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
123 if (size)
124 *size = sechdrs[i].sh_size;
125 return (void *)ehdr + sechdrs[i].sh_offset;
126 }
127 }
128
129 if (size)
130 *size = 0;
131 return NULL;
132}
133
134static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
135{
136 unsigned int i;
137
138 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
139 char name[MAX_SYMNAME], *c;
140
141 if (lib->dynsym[i].st_name == 0)
142 continue;
143 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
144 MAX_SYMNAME);
145 c = strchr(name, '@');
146 if (c)
147 *c = 0;
148 if (strcmp(symname, name) == 0)
149 return &lib->dynsym[i];
150 }
151 return NULL;
152}
153
154static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
155{
156 Elf32_Sym *sym;
157
158 sym = find_symbol(lib, symname);
159 if (!sym)
160 return;
161
162 sym->st_name = 0;
163}
164
165static void __init patch_vdso(void *ehdr)
166{
167 struct elfinfo einfo;
168
169 einfo = (struct elfinfo) {
170 .hdr = ehdr,
171 };
172
173 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
174 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
175
176 /* If the virtual counter is absent or non-functional we don't
177 * want programs to incur the slight additional overhead of
178 * dispatching through the VDSO only to fall back to syscalls.
179 */
180 if (!cntvct_ok) {
181 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
182 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
183 }
184}
185
186static int __init vdso_init(void)
187{
188 unsigned int text_pages;
189 int i;
190
73b9160d 191 if (memcmp(vdso_start, "\177ELF", 4)) {
ecf99a43
NL
192 pr_err("VDSO is not a valid ELF object!\n");
193 return -ENOEXEC;
194 }
195
73b9160d
JP
196 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
197 pr_debug("vdso: %i text pages at base %p\n", text_pages, vdso_start);
ecf99a43
NL
198
199 /* Allocate the VDSO text pagelist */
200 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
201 GFP_KERNEL);
202 if (vdso_text_pagelist == NULL)
203 return -ENOMEM;
204
205 /* Grab the VDSO data page. */
206 vdso_data_page = virt_to_page(vdso_data);
207
208 /* Grab the VDSO text pages. */
209 for (i = 0; i < text_pages; i++) {
210 struct page *page;
211
73b9160d 212 page = virt_to_page(vdso_start + i * PAGE_SIZE);
ecf99a43
NL
213 vdso_text_pagelist[i] = page;
214 }
215
216 vdso_text_mapping.pages = vdso_text_pagelist;
217
218 vdso_total_pages = 1; /* for the data/vvar page */
219 vdso_total_pages += text_pages;
220
221 cntvct_ok = cntvct_functional();
222
73b9160d 223 patch_vdso(vdso_start);
ecf99a43
NL
224
225 return 0;
226}
227arch_initcall(vdso_init);
228
229static int install_vvar(struct mm_struct *mm, unsigned long addr)
230{
231 struct vm_area_struct *vma;
232
233 vma = _install_special_mapping(mm, addr, PAGE_SIZE,
234 VM_READ | VM_MAYREAD,
235 &vdso_data_mapping);
236
38fc2f6c 237 return PTR_ERR_OR_ZERO(vma);
ecf99a43
NL
238}
239
240/* assumes mmap_sem is write-locked */
241void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
242{
243 struct vm_area_struct *vma;
244 unsigned long len;
245
246 mm->context.vdso = 0;
247
248 if (vdso_text_pagelist == NULL)
249 return;
250
251 if (install_vvar(mm, addr))
252 return;
253
254 /* Account for vvar page. */
255 addr += PAGE_SIZE;
256 len = (vdso_total_pages - 1) << PAGE_SHIFT;
257
258 vma = _install_special_mapping(mm, addr, len,
259 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
260 &vdso_text_mapping);
261
262 if (!IS_ERR(vma))
263 mm->context.vdso = addr;
264}
265
266static void vdso_write_begin(struct vdso_data *vdata)
267{
268 ++vdso_data->seq_count;
269 smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
270}
271
272static void vdso_write_end(struct vdso_data *vdata)
273{
274 smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
275 ++vdso_data->seq_count;
276}
277
278static bool tk_is_cntvct(const struct timekeeper *tk)
279{
280 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
281 return false;
282
1d8f51d4 283 if (!tk->tkr_mono.clock->archdata.vdso_direct)
ecf99a43
NL
284 return false;
285
286 return true;
287}
288
289/**
290 * update_vsyscall - update the vdso data page
291 *
292 * Increment the sequence counter, making it odd, indicating to
293 * userspace that an update is in progress. Update the fields used
294 * for coarse clocks and, if the architected system timer is in use,
295 * the fields used for high precision clocks. Increment the sequence
296 * counter again, making it even, indicating to userspace that the
297 * update is finished.
298 *
299 * Userspace is expected to sample seq_count before reading any other
300 * fields from the data page. If seq_count is odd, userspace is
301 * expected to wait until it becomes even. After copying data from
302 * the page, userspace must sample seq_count again; if it has changed
303 * from its previous value, userspace must retry the whole sequence.
304 *
305 * Calls to update_vsyscall are serialized by the timekeeping core.
306 */
307void update_vsyscall(struct timekeeper *tk)
308{
ecf99a43
NL
309 struct timespec64 *wtm = &tk->wall_to_monotonic;
310
311 if (!cntvct_ok) {
312 /* The entry points have been zeroed, so there is no
313 * point in updating the data page.
314 */
315 return;
316 }
317
318 vdso_write_begin(vdso_data);
319
ecf99a43 320 vdso_data->tk_is_cntvct = tk_is_cntvct(tk);
09edea4f
NL
321 vdso_data->xtime_coarse_sec = tk->xtime_sec;
322 vdso_data->xtime_coarse_nsec = (u32)(tk->tkr_mono.xtime_nsec >>
323 tk->tkr_mono.shift);
ecf99a43
NL
324 vdso_data->wtm_clock_sec = wtm->tv_sec;
325 vdso_data->wtm_clock_nsec = wtm->tv_nsec;
326
327 if (vdso_data->tk_is_cntvct) {
bb0fd7ab 328 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
ecf99a43 329 vdso_data->xtime_clock_sec = tk->xtime_sec;
bb0fd7ab
LT
330 vdso_data->xtime_clock_snsec = tk->tkr_mono.xtime_nsec;
331 vdso_data->cs_mult = tk->tkr_mono.mult;
332 vdso_data->cs_shift = tk->tkr_mono.shift;
333 vdso_data->cs_mask = tk->tkr_mono.mask;
ecf99a43
NL
334 }
335
336 vdso_write_end(vdso_data);
337
338 flush_dcache_page(virt_to_page(vdso_data));
339}
340
341void update_vsyscall_tz(void)
342{
343 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
344 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
345 flush_dcache_page(virt_to_page(vdso_data));
346}