mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / arm / kernel / vdso.c
blob0001742c131d965908fa7c9572482862dd269eba
1 /*
2 * Adapted from arm64 version.
4 * Copyright (C) 2012 ARM Limited
5 * Copyright (C) 2015 Mentor Graphics Corporation.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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>
38 #define MAX_SYMNAME 64
40 static struct page **vdso_text_pagelist;
42 extern char vdso_start[], vdso_end[];
44 /* Total number of pages needed for the data and text portions of the VDSO. */
45 unsigned int vdso_total_pages __ro_after_init;
48 * The VDSO data page.
50 static union vdso_data_store vdso_data_store __page_aligned_data;
51 static struct vdso_data *vdso_data = &vdso_data_store.data;
53 static struct page *vdso_data_page __ro_after_init;
54 static const struct vm_special_mapping vdso_data_mapping = {
55 .name = "[vvar]",
56 .pages = &vdso_data_page,
59 static int vdso_mremap(const struct vm_special_mapping *sm,
60 struct vm_area_struct *new_vma)
62 unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
63 unsigned long vdso_size;
65 /* without VVAR page */
66 vdso_size = (vdso_total_pages - 1) << PAGE_SHIFT;
68 if (vdso_size != new_size)
69 return -EINVAL;
71 current->mm->context.vdso = new_vma->vm_start;
73 return 0;
76 static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
77 .name = "[vdso]",
78 .mremap = vdso_mremap,
81 struct elfinfo {
82 Elf32_Ehdr *hdr; /* ptr to ELF */
83 Elf32_Sym *dynsym; /* ptr to .dynsym section */
84 unsigned long dynsymsize; /* size of .dynsym section */
85 char *dynstr; /* ptr to .dynstr section */
88 /* Cached result of boot-time check for whether the arch timer exists,
89 * and if so, whether the virtual counter is useable.
91 static bool cntvct_ok __ro_after_init;
93 static bool __init cntvct_functional(void)
95 struct device_node *np;
96 bool ret = false;
98 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
99 goto out;
101 /* The arm_arch_timer core should export
102 * arch_timer_use_virtual or similar so we don't have to do
103 * this.
105 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
106 if (!np)
107 np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
108 if (!np)
109 goto out_put;
111 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
112 goto out_put;
114 ret = true;
116 out_put:
117 of_node_put(np);
118 out:
119 return ret;
122 static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
123 unsigned long *size)
125 Elf32_Shdr *sechdrs;
126 unsigned int i;
127 char *secnames;
129 /* Grab section headers and strings so we can tell who is who */
130 sechdrs = (void *)ehdr + ehdr->e_shoff;
131 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
133 /* Find the section they want */
134 for (i = 1; i < ehdr->e_shnum; i++) {
135 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
136 if (size)
137 *size = sechdrs[i].sh_size;
138 return (void *)ehdr + sechdrs[i].sh_offset;
142 if (size)
143 *size = 0;
144 return NULL;
147 static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
149 unsigned int i;
151 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
152 char name[MAX_SYMNAME], *c;
154 if (lib->dynsym[i].st_name == 0)
155 continue;
156 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
157 MAX_SYMNAME);
158 c = strchr(name, '@');
159 if (c)
160 *c = 0;
161 if (strcmp(symname, name) == 0)
162 return &lib->dynsym[i];
164 return NULL;
167 static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
169 Elf32_Sym *sym;
171 sym = find_symbol(lib, symname);
172 if (!sym)
173 return;
175 sym->st_name = 0;
178 static void __init patch_vdso(void *ehdr)
180 struct elfinfo einfo;
182 einfo = (struct elfinfo) {
183 .hdr = ehdr,
186 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
187 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
189 /* If the virtual counter is absent or non-functional we don't
190 * want programs to incur the slight additional overhead of
191 * dispatching through the VDSO only to fall back to syscalls.
193 if (!cntvct_ok) {
194 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
195 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
199 static int __init vdso_init(void)
201 unsigned int text_pages;
202 int i;
204 if (memcmp(vdso_start, "\177ELF", 4)) {
205 pr_err("VDSO is not a valid ELF object!\n");
206 return -ENOEXEC;
209 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
210 pr_debug("vdso: %i text pages at base %p\n", text_pages, vdso_start);
212 /* Allocate the VDSO text pagelist */
213 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
214 GFP_KERNEL);
215 if (vdso_text_pagelist == NULL)
216 return -ENOMEM;
218 /* Grab the VDSO data page. */
219 vdso_data_page = virt_to_page(vdso_data);
221 /* Grab the VDSO text pages. */
222 for (i = 0; i < text_pages; i++) {
223 struct page *page;
225 page = virt_to_page(vdso_start + i * PAGE_SIZE);
226 vdso_text_pagelist[i] = page;
229 vdso_text_mapping.pages = vdso_text_pagelist;
231 vdso_total_pages = 1; /* for the data/vvar page */
232 vdso_total_pages += text_pages;
234 cntvct_ok = cntvct_functional();
236 patch_vdso(vdso_start);
238 return 0;
240 arch_initcall(vdso_init);
242 static int install_vvar(struct mm_struct *mm, unsigned long addr)
244 struct vm_area_struct *vma;
246 vma = _install_special_mapping(mm, addr, PAGE_SIZE,
247 VM_READ | VM_MAYREAD,
248 &vdso_data_mapping);
250 return PTR_ERR_OR_ZERO(vma);
253 /* assumes mmap_sem is write-locked */
254 void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
256 struct vm_area_struct *vma;
257 unsigned long len;
259 mm->context.vdso = 0;
261 if (vdso_text_pagelist == NULL)
262 return;
264 if (install_vvar(mm, addr))
265 return;
267 /* Account for vvar page. */
268 addr += PAGE_SIZE;
269 len = (vdso_total_pages - 1) << PAGE_SHIFT;
271 vma = _install_special_mapping(mm, addr, len,
272 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
273 &vdso_text_mapping);
275 if (!IS_ERR(vma))
276 mm->context.vdso = addr;
279 static void vdso_write_begin(struct vdso_data *vdata)
281 ++vdso_data->seq_count;
282 smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
285 static void vdso_write_end(struct vdso_data *vdata)
287 smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
288 ++vdso_data->seq_count;
291 static bool tk_is_cntvct(const struct timekeeper *tk)
293 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
294 return false;
296 if (!tk->tkr_mono.clock->archdata.vdso_direct)
297 return false;
299 return true;
303 * update_vsyscall - update the vdso data page
305 * Increment the sequence counter, making it odd, indicating to
306 * userspace that an update is in progress. Update the fields used
307 * for coarse clocks and, if the architected system timer is in use,
308 * the fields used for high precision clocks. Increment the sequence
309 * counter again, making it even, indicating to userspace that the
310 * update is finished.
312 * Userspace is expected to sample seq_count before reading any other
313 * fields from the data page. If seq_count is odd, userspace is
314 * expected to wait until it becomes even. After copying data from
315 * the page, userspace must sample seq_count again; if it has changed
316 * from its previous value, userspace must retry the whole sequence.
318 * Calls to update_vsyscall are serialized by the timekeeping core.
320 void update_vsyscall(struct timekeeper *tk)
322 struct timespec64 *wtm = &tk->wall_to_monotonic;
324 if (!cntvct_ok) {
325 /* The entry points have been zeroed, so there is no
326 * point in updating the data page.
328 return;
331 vdso_write_begin(vdso_data);
333 vdso_data->tk_is_cntvct = tk_is_cntvct(tk);
334 vdso_data->xtime_coarse_sec = tk->xtime_sec;
335 vdso_data->xtime_coarse_nsec = (u32)(tk->tkr_mono.xtime_nsec >>
336 tk->tkr_mono.shift);
337 vdso_data->wtm_clock_sec = wtm->tv_sec;
338 vdso_data->wtm_clock_nsec = wtm->tv_nsec;
340 if (vdso_data->tk_is_cntvct) {
341 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
342 vdso_data->xtime_clock_sec = tk->xtime_sec;
343 vdso_data->xtime_clock_snsec = tk->tkr_mono.xtime_nsec;
344 vdso_data->cs_mult = tk->tkr_mono.mult;
345 vdso_data->cs_shift = tk->tkr_mono.shift;
346 vdso_data->cs_mask = tk->tkr_mono.mask;
349 vdso_write_end(vdso_data);
351 flush_dcache_page(virt_to_page(vdso_data));
354 void update_vsyscall_tz(void)
356 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
357 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
358 flush_dcache_page(virt_to_page(vdso_data));