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