xref: /openbmc/linux/arch/arm64/kernel/vdso.c (revision 62975d27)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VDSO implementations.
4  *
5  * Copyright (C) 2012 ARM Limited
6  *
7  * Author: Will Deacon <will.deacon@arm.com>
8  */
9 
10 #include <linux/cache.h>
11 #include <linux/clocksource.h>
12 #include <linux/elf.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/gfp.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/sched.h>
19 #include <linux/signal.h>
20 #include <linux/slab.h>
21 #include <linux/timekeeper_internal.h>
22 #include <linux/vmalloc.h>
23 #include <vdso/datapage.h>
24 #include <vdso/helpers.h>
25 #include <vdso/vsyscall.h>
26 
27 #include <asm/cacheflush.h>
28 #include <asm/signal32.h>
29 #include <asm/vdso.h>
30 
31 extern char vdso_start[], vdso_end[];
32 #ifdef CONFIG_COMPAT_VDSO
33 extern char vdso32_start[], vdso32_end[];
34 #endif /* CONFIG_COMPAT_VDSO */
35 
36 enum vdso_abi {
37 	VDSO_ABI_AA64,
38 #ifdef CONFIG_COMPAT_VDSO
39 	VDSO_ABI_AA32,
40 #endif /* CONFIG_COMPAT_VDSO */
41 };
42 
43 struct vdso_abi_info {
44 	const char *name;
45 	const char *vdso_code_start;
46 	const char *vdso_code_end;
47 	unsigned long vdso_pages;
48 	/* Data Mapping */
49 	struct vm_special_mapping *dm;
50 	/* Code Mapping */
51 	struct vm_special_mapping *cm;
52 };
53 
54 static struct vdso_abi_info vdso_info[] __ro_after_init = {
55 	[VDSO_ABI_AA64] = {
56 		.name = "vdso",
57 		.vdso_code_start = vdso_start,
58 		.vdso_code_end = vdso_end,
59 	},
60 #ifdef CONFIG_COMPAT_VDSO
61 	[VDSO_ABI_AA32] = {
62 		.name = "vdso32",
63 		.vdso_code_start = vdso32_start,
64 		.vdso_code_end = vdso32_end,
65 	},
66 #endif /* CONFIG_COMPAT_VDSO */
67 };
68 
69 /*
70  * The vDSO data page.
71  */
72 static union {
73 	struct vdso_data	data[CS_BASES];
74 	u8			page[PAGE_SIZE];
75 } vdso_data_store __page_aligned_data;
76 struct vdso_data *vdso_data = vdso_data_store.data;
77 
78 static int __vdso_remap(enum vdso_abi abi,
79 			const struct vm_special_mapping *sm,
80 			struct vm_area_struct *new_vma)
81 {
82 	unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
83 	unsigned long vdso_size = vdso_info[abi].vdso_code_end -
84 				  vdso_info[abi].vdso_code_start;
85 
86 	if (vdso_size != new_size)
87 		return -EINVAL;
88 
89 	current->mm->context.vdso = (void *)new_vma->vm_start;
90 
91 	return 0;
92 }
93 
94 static int __vdso_init(enum vdso_abi abi)
95 {
96 	int i;
97 	struct page **vdso_pagelist;
98 	unsigned long pfn;
99 
100 	if (memcmp(vdso_info[abi].vdso_code_start, "\177ELF", 4)) {
101 		pr_err("vDSO is not a valid ELF object!\n");
102 		return -EINVAL;
103 	}
104 
105 	vdso_info[abi].vdso_pages = (
106 			vdso_info[abi].vdso_code_end -
107 			vdso_info[abi].vdso_code_start) >>
108 			PAGE_SHIFT;
109 
110 	/* Allocate the vDSO pagelist, plus a page for the data. */
111 	vdso_pagelist = kcalloc(vdso_info[abi].vdso_pages + 1,
112 				sizeof(struct page *),
113 				GFP_KERNEL);
114 	if (vdso_pagelist == NULL)
115 		return -ENOMEM;
116 
117 	/* Grab the vDSO data page. */
118 	vdso_pagelist[0] = phys_to_page(__pa_symbol(vdso_data));
119 
120 
121 	/* Grab the vDSO code pages. */
122 	pfn = sym_to_pfn(vdso_info[abi].vdso_code_start);
123 
124 	for (i = 0; i < vdso_info[abi].vdso_pages; i++)
125 		vdso_pagelist[i + 1] = pfn_to_page(pfn + i);
126 
127 	vdso_info[abi].dm->pages = &vdso_pagelist[0];
128 	vdso_info[abi].cm->pages = &vdso_pagelist[1];
129 
130 	return 0;
131 }
132 
133 static int __setup_additional_pages(enum vdso_abi abi,
134 				    struct mm_struct *mm,
135 				    struct linux_binprm *bprm,
136 				    int uses_interp)
137 {
138 	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
139 	unsigned long gp_flags = 0;
140 	void *ret;
141 
142 	vdso_text_len = vdso_info[abi].vdso_pages << PAGE_SHIFT;
143 	/* Be sure to map the data page */
144 	vdso_mapping_len = vdso_text_len + PAGE_SIZE;
145 
146 	vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
147 	if (IS_ERR_VALUE(vdso_base)) {
148 		ret = ERR_PTR(vdso_base);
149 		goto up_fail;
150 	}
151 
152 	ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE,
153 				       VM_READ|VM_MAYREAD,
154 				       vdso_info[abi].dm);
155 	if (IS_ERR(ret))
156 		goto up_fail;
157 
158 	if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) && system_supports_bti())
159 		gp_flags = VM_ARM64_BTI;
160 
161 	vdso_base += PAGE_SIZE;
162 	mm->context.vdso = (void *)vdso_base;
163 	ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
164 				       VM_READ|VM_EXEC|gp_flags|
165 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
166 				       vdso_info[abi].cm);
167 	if (IS_ERR(ret))
168 		goto up_fail;
169 
170 	return 0;
171 
172 up_fail:
173 	mm->context.vdso = NULL;
174 	return PTR_ERR(ret);
175 }
176 
177 #ifdef CONFIG_COMPAT
178 /*
179  * Create and map the vectors page for AArch32 tasks.
180  */
181 #ifdef CONFIG_COMPAT_VDSO
182 static int aarch32_vdso_mremap(const struct vm_special_mapping *sm,
183 		struct vm_area_struct *new_vma)
184 {
185 	return __vdso_remap(VDSO_ABI_AA32, sm, new_vma);
186 }
187 #endif /* CONFIG_COMPAT_VDSO */
188 
189 enum aarch32_map {
190 	AA32_MAP_VECTORS, /* kuser helpers */
191 #ifdef CONFIG_COMPAT_VDSO
192 	AA32_MAP_VVAR,
193 	AA32_MAP_VDSO,
194 #endif
195 	AA32_MAP_SIGPAGE
196 };
197 
198 static struct page *aarch32_vectors_page __ro_after_init;
199 static struct page *aarch32_sig_page __ro_after_init;
200 
201 static struct vm_special_mapping aarch32_vdso_maps[] = {
202 	[AA32_MAP_VECTORS] = {
203 		.name	= "[vectors]", /* ABI */
204 		.pages	= &aarch32_vectors_page,
205 	},
206 #ifdef CONFIG_COMPAT_VDSO
207 	[AA32_MAP_VVAR] = {
208 		.name = "[vvar]",
209 	},
210 	[AA32_MAP_VDSO] = {
211 		.name = "[vdso]",
212 		.mremap = aarch32_vdso_mremap,
213 	},
214 #endif /* CONFIG_COMPAT_VDSO */
215 	[AA32_MAP_SIGPAGE] = {
216 		.name	= "[sigpage]", /* ABI */
217 		.pages	= &aarch32_sig_page,
218 	},
219 };
220 
221 static int aarch32_alloc_kuser_vdso_page(void)
222 {
223 	extern char __kuser_helper_start[], __kuser_helper_end[];
224 	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
225 	unsigned long vdso_page;
226 
227 	if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
228 		return 0;
229 
230 	vdso_page = get_zeroed_page(GFP_ATOMIC);
231 	if (!vdso_page)
232 		return -ENOMEM;
233 
234 	memcpy((void *)(vdso_page + 0x1000 - kuser_sz), __kuser_helper_start,
235 	       kuser_sz);
236 	aarch32_vectors_page = virt_to_page(vdso_page);
237 	flush_dcache_page(aarch32_vectors_page);
238 	return 0;
239 }
240 
241 static int aarch32_alloc_sigpage(void)
242 {
243 	extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
244 	int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
245 	unsigned long sigpage;
246 
247 	sigpage = get_zeroed_page(GFP_ATOMIC);
248 	if (!sigpage)
249 		return -ENOMEM;
250 
251 	memcpy((void *)sigpage, __aarch32_sigret_code_start, sigret_sz);
252 	aarch32_sig_page = virt_to_page(sigpage);
253 	flush_dcache_page(aarch32_sig_page);
254 	return 0;
255 }
256 
257 #ifdef CONFIG_COMPAT_VDSO
258 static int __aarch32_alloc_vdso_pages(void)
259 {
260 	vdso_info[VDSO_ABI_AA32].dm = &aarch32_vdso_maps[AA32_MAP_VVAR];
261 	vdso_info[VDSO_ABI_AA32].cm = &aarch32_vdso_maps[AA32_MAP_VDSO];
262 
263 	return __vdso_init(VDSO_ABI_AA32);
264 }
265 #endif /* CONFIG_COMPAT_VDSO */
266 
267 static int __init aarch32_alloc_vdso_pages(void)
268 {
269 	int ret;
270 
271 #ifdef CONFIG_COMPAT_VDSO
272 	ret = __aarch32_alloc_vdso_pages();
273 	if (ret)
274 		return ret;
275 #endif
276 
277 	ret = aarch32_alloc_sigpage();
278 	if (ret)
279 		return ret;
280 
281 	return aarch32_alloc_kuser_vdso_page();
282 }
283 arch_initcall(aarch32_alloc_vdso_pages);
284 
285 static int aarch32_kuser_helpers_setup(struct mm_struct *mm)
286 {
287 	void *ret;
288 
289 	if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
290 		return 0;
291 
292 	/*
293 	 * Avoid VM_MAYWRITE for compatibility with arch/arm/, where it's
294 	 * not safe to CoW the page containing the CPU exception vectors.
295 	 */
296 	ret = _install_special_mapping(mm, AARCH32_VECTORS_BASE, PAGE_SIZE,
297 				       VM_READ | VM_EXEC |
298 				       VM_MAYREAD | VM_MAYEXEC,
299 				       &aarch32_vdso_maps[AA32_MAP_VECTORS]);
300 
301 	return PTR_ERR_OR_ZERO(ret);
302 }
303 
304 static int aarch32_sigreturn_setup(struct mm_struct *mm)
305 {
306 	unsigned long addr;
307 	void *ret;
308 
309 	addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
310 	if (IS_ERR_VALUE(addr)) {
311 		ret = ERR_PTR(addr);
312 		goto out;
313 	}
314 
315 	/*
316 	 * VM_MAYWRITE is required to allow gdb to Copy-on-Write and
317 	 * set breakpoints.
318 	 */
319 	ret = _install_special_mapping(mm, addr, PAGE_SIZE,
320 				       VM_READ | VM_EXEC | VM_MAYREAD |
321 				       VM_MAYWRITE | VM_MAYEXEC,
322 				       &aarch32_vdso_maps[AA32_MAP_SIGPAGE]);
323 	if (IS_ERR(ret))
324 		goto out;
325 
326 	mm->context.sigpage = (void *)addr;
327 
328 out:
329 	return PTR_ERR_OR_ZERO(ret);
330 }
331 
332 int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
333 {
334 	struct mm_struct *mm = current->mm;
335 	int ret;
336 
337 	if (mmap_write_lock_killable(mm))
338 		return -EINTR;
339 
340 	ret = aarch32_kuser_helpers_setup(mm);
341 	if (ret)
342 		goto out;
343 
344 #ifdef CONFIG_COMPAT_VDSO
345 	ret = __setup_additional_pages(VDSO_ABI_AA32,
346 				       mm,
347 				       bprm,
348 				       uses_interp);
349 	if (ret)
350 		goto out;
351 #endif /* CONFIG_COMPAT_VDSO */
352 
353 	ret = aarch32_sigreturn_setup(mm);
354 out:
355 	mmap_write_unlock(mm);
356 	return ret;
357 }
358 #endif /* CONFIG_COMPAT */
359 
360 static int vdso_mremap(const struct vm_special_mapping *sm,
361 		struct vm_area_struct *new_vma)
362 {
363 	return __vdso_remap(VDSO_ABI_AA64, sm, new_vma);
364 }
365 
366 enum aarch64_map {
367 	AA64_MAP_VVAR,
368 	AA64_MAP_VDSO,
369 };
370 
371 static struct vm_special_mapping aarch64_vdso_maps[] __ro_after_init = {
372 	[AA64_MAP_VVAR] = {
373 		.name	= "[vvar]",
374 	},
375 	[AA64_MAP_VDSO] = {
376 		.name	= "[vdso]",
377 		.mremap = vdso_mremap,
378 	},
379 };
380 
381 static int __init vdso_init(void)
382 {
383 	vdso_info[VDSO_ABI_AA64].dm = &aarch64_vdso_maps[AA64_MAP_VVAR];
384 	vdso_info[VDSO_ABI_AA64].cm = &aarch64_vdso_maps[AA64_MAP_VDSO];
385 
386 	return __vdso_init(VDSO_ABI_AA64);
387 }
388 arch_initcall(vdso_init);
389 
390 int arch_setup_additional_pages(struct linux_binprm *bprm,
391 				int uses_interp)
392 {
393 	struct mm_struct *mm = current->mm;
394 	int ret;
395 
396 	if (mmap_write_lock_killable(mm))
397 		return -EINTR;
398 
399 	ret = __setup_additional_pages(VDSO_ABI_AA64,
400 				       mm,
401 				       bprm,
402 				       uses_interp);
403 
404 	mmap_write_unlock(mm);
405 
406 	return ret;
407 }
408