xref: /linux/arch/arm/kernel/vdso.c (revision ac84bac4062e7fc24f5e2c61c6a414b2a00a29ad)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Adapted from arm64 version.
4  *
5  * Copyright (C) 2012 ARM Limited
6  * Copyright (C) 2015 Mentor Graphics Corporation.
7  */
8 
9 #include <linux/cache.h>
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 #include <vdso/helpers.h>
27 #include <vdso/vsyscall.h>
28 
29 #define MAX_SYMNAME	64
30 
31 static struct page **vdso_text_pagelist;
32 
33 extern char vdso_start[], vdso_end[];
34 
35 /* Total number of pages needed for the data and text portions of the VDSO. */
36 unsigned int vdso_total_pages __ro_after_init;
37 
38 /*
39  * The VDSO data page.
40  */
41 static union vdso_data_store vdso_data_store __page_aligned_data;
42 struct vdso_data *vdso_data = vdso_data_store.data;
43 
44 static struct page *vdso_data_page __ro_after_init;
45 static const struct vm_special_mapping vdso_data_mapping = {
46 	.name = "[vvar]",
47 	.pages = &vdso_data_page,
48 };
49 
50 static int vdso_mremap(const struct vm_special_mapping *sm,
51 		struct vm_area_struct *new_vma)
52 {
53 	unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
54 	unsigned long vdso_size;
55 
56 	/* without VVAR page */
57 	vdso_size = (vdso_total_pages - 1) << PAGE_SHIFT;
58 
59 	if (vdso_size != new_size)
60 		return -EINVAL;
61 
62 	current->mm->context.vdso = new_vma->vm_start;
63 
64 	return 0;
65 }
66 
67 static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
68 	.name = "[vdso]",
69 	.mremap = vdso_mremap,
70 };
71 
72 struct elfinfo {
73 	Elf32_Ehdr	*hdr;		/* ptr to ELF */
74 	Elf32_Sym	*dynsym;	/* ptr to .dynsym section */
75 	unsigned long	dynsymsize;	/* size of .dynsym section */
76 	char		*dynstr;	/* ptr to .dynstr section */
77 };
78 
79 /* Cached result of boot-time check for whether the arch timer exists,
80  * and if so, whether the virtual counter is useable.
81  */
82 bool cntvct_ok __ro_after_init;
83 
84 static bool __init cntvct_functional(void)
85 {
86 	struct device_node *np;
87 	bool ret = false;
88 
89 	if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
90 		goto out;
91 
92 	/* The arm_arch_timer core should export
93 	 * arch_timer_use_virtual or similar so we don't have to do
94 	 * this.
95 	 */
96 	np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
97 	if (!np)
98 		np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
99 	if (!np)
100 		goto out_put;
101 
102 	if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
103 		goto out_put;
104 
105 	ret = true;
106 
107 out_put:
108 	of_node_put(np);
109 out:
110 	return ret;
111 }
112 
113 static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
114 				  unsigned long *size)
115 {
116 	Elf32_Shdr *sechdrs;
117 	unsigned int i;
118 	char *secnames;
119 
120 	/* Grab section headers and strings so we can tell who is who */
121 	sechdrs = (void *)ehdr + ehdr->e_shoff;
122 	secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
123 
124 	/* Find the section they want */
125 	for (i = 1; i < ehdr->e_shnum; i++) {
126 		if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
127 			if (size)
128 				*size = sechdrs[i].sh_size;
129 			return (void *)ehdr + sechdrs[i].sh_offset;
130 		}
131 	}
132 
133 	if (size)
134 		*size = 0;
135 	return NULL;
136 }
137 
138 static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
139 {
140 	unsigned int i;
141 
142 	for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
143 		char name[MAX_SYMNAME], *c;
144 
145 		if (lib->dynsym[i].st_name == 0)
146 			continue;
147 		strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
148 			MAX_SYMNAME);
149 		c = strchr(name, '@');
150 		if (c)
151 			*c = 0;
152 		if (strcmp(symname, name) == 0)
153 			return &lib->dynsym[i];
154 	}
155 	return NULL;
156 }
157 
158 static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
159 {
160 	Elf32_Sym *sym;
161 
162 	sym = find_symbol(lib, symname);
163 	if (!sym)
164 		return;
165 
166 	sym->st_name = 0;
167 }
168 
169 static void __init patch_vdso(void *ehdr)
170 {
171 	struct elfinfo einfo;
172 
173 	einfo = (struct elfinfo) {
174 		.hdr = ehdr,
175 	};
176 
177 	einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
178 	einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
179 
180 	/* If the virtual counter is absent or non-functional we don't
181 	 * want programs to incur the slight additional overhead of
182 	 * dispatching through the VDSO only to fall back to syscalls.
183 	 */
184 	if (!cntvct_ok) {
185 		vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
186 		vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
187 	}
188 }
189 
190 static int __init vdso_init(void)
191 {
192 	unsigned int text_pages;
193 	int i;
194 
195 	if (memcmp(vdso_start, "\177ELF", 4)) {
196 		pr_err("VDSO is not a valid ELF object!\n");
197 		return -ENOEXEC;
198 	}
199 
200 	text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
201 
202 	/* Allocate the VDSO text pagelist */
203 	vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
204 				     GFP_KERNEL);
205 	if (vdso_text_pagelist == NULL)
206 		return -ENOMEM;
207 
208 	/* Grab the VDSO data page. */
209 	vdso_data_page = virt_to_page(vdso_data);
210 
211 	/* Grab the VDSO text pages. */
212 	for (i = 0; i < text_pages; i++) {
213 		struct page *page;
214 
215 		page = virt_to_page(vdso_start + i * PAGE_SIZE);
216 		vdso_text_pagelist[i] = page;
217 	}
218 
219 	vdso_text_mapping.pages = vdso_text_pagelist;
220 
221 	vdso_total_pages = 1; /* for the data/vvar page */
222 	vdso_total_pages += text_pages;
223 
224 	cntvct_ok = cntvct_functional();
225 
226 	patch_vdso(vdso_start);
227 
228 	return 0;
229 }
230 arch_initcall(vdso_init);
231 
232 static int install_vvar(struct mm_struct *mm, unsigned long addr)
233 {
234 	struct vm_area_struct *vma;
235 
236 	vma = _install_special_mapping(mm, addr, PAGE_SIZE,
237 				       VM_READ | VM_MAYREAD,
238 				       &vdso_data_mapping);
239 
240 	return PTR_ERR_OR_ZERO(vma);
241 }
242 
243 /* assumes mmap_sem is write-locked */
244 void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
245 {
246 	struct vm_area_struct *vma;
247 	unsigned long len;
248 
249 	mm->context.vdso = 0;
250 
251 	if (vdso_text_pagelist == NULL)
252 		return;
253 
254 	if (install_vvar(mm, addr))
255 		return;
256 
257 	/* Account for vvar page. */
258 	addr += PAGE_SIZE;
259 	len = (vdso_total_pages - 1) << PAGE_SHIFT;
260 
261 	vma = _install_special_mapping(mm, addr, len,
262 		VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
263 		&vdso_text_mapping);
264 
265 	if (!IS_ERR(vma))
266 		mm->context.vdso = addr;
267 }
268 
269