Merge tag 'x86-asm-2024-03-11' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[linux-2.6-block.git] / arch / arm64 / kernel / vdso.c
CommitLineData
caab277b 1// SPDX-License-Identifier: GPL-2.0-only
9031fefd 2/*
0d747f65 3 * VDSO implementations.
9031fefd
WD
4 *
5 * Copyright (C) 2012 ARM Limited
6 *
9031fefd
WD
7 * Author: Will Deacon <will.deacon@arm.com>
8 */
9
5a9e3e15 10#include <linux/cache.h>
9031fefd
WD
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>
5a9e3e15 16#include <linux/kernel.h>
9031fefd
WD
17#include <linux/mm.h>
18#include <linux/sched.h>
19#include <linux/signal.h>
20#include <linux/slab.h>
ee3cda8e 21#include <linux/time_namespace.h>
c60b0c28 22#include <linux/timekeeper_internal.h>
9031fefd 23#include <linux/vmalloc.h>
28b1a824
VF
24#include <vdso/datapage.h>
25#include <vdso/helpers.h>
26#include <vdso/vsyscall.h>
9031fefd
WD
27
28#include <asm/cacheflush.h>
29#include <asm/signal32.h>
30#include <asm/vdso.h>
9031fefd 31
d3418f38
MR
32enum vdso_abi {
33 VDSO_ABI_AA64,
d3418f38 34 VDSO_ABI_AA32,
c7aa2d71 35};
c7aa2d71 36
3503d56c
AV
37enum vvar_pages {
38 VVAR_DATA_PAGE_OFFSET,
39 VVAR_TIMENS_PAGE_OFFSET,
40 VVAR_NR_PAGES,
41};
42
d3418f38 43struct vdso_abi_info {
c7aa2d71
VF
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
d3418f38
MR
54static struct vdso_abi_info vdso_info[] __ro_after_init = {
55 [VDSO_ABI_AA64] = {
c7aa2d71
VF
56 .name = "vdso",
57 .vdso_code_start = vdso_start,
58 .vdso_code_end = vdso_end,
59 },
7c1deeeb 60#ifdef CONFIG_COMPAT_VDSO
d3418f38 61 [VDSO_ABI_AA32] = {
7c1deeeb
VF
62 .name = "vdso32",
63 .vdso_code_start = vdso32_start,
64 .vdso_code_end = vdso32_end,
65 },
66#endif /* CONFIG_COMPAT_VDSO */
c7aa2d71 67};
9031fefd
WD
68
69/*
70 * The vDSO data page.
71 */
d0fba048 72static union vdso_data_store vdso_data_store __page_aligned_data;
28b1a824 73struct vdso_data *vdso_data = vdso_data_store.data;
9031fefd 74
871402e0
DS
75static int vdso_mremap(const struct vm_special_mapping *sm,
76 struct vm_area_struct *new_vma)
c7aa2d71 77{
c7aa2d71
VF
78 current->mm->context.vdso = (void *)new_vma->vm_start;
79
80 return 0;
81}
82
a7dcf58a 83static int __init __vdso_init(enum vdso_abi abi)
c7aa2d71
VF
84{
85 int i;
86 struct page **vdso_pagelist;
87 unsigned long pfn;
88
d3418f38 89 if (memcmp(vdso_info[abi].vdso_code_start, "\177ELF", 4)) {
c7aa2d71
VF
90 pr_err("vDSO is not a valid ELF object!\n");
91 return -EINVAL;
92 }
93
d3418f38
MR
94 vdso_info[abi].vdso_pages = (
95 vdso_info[abi].vdso_code_end -
96 vdso_info[abi].vdso_code_start) >>
c7aa2d71
VF
97 PAGE_SHIFT;
98
d53b5c01 99 vdso_pagelist = kcalloc(vdso_info[abi].vdso_pages,
c7aa2d71
VF
100 sizeof(struct page *),
101 GFP_KERNEL);
102 if (vdso_pagelist == NULL)
103 return -ENOMEM;
104
c7aa2d71 105 /* Grab the vDSO code pages. */
d3418f38 106 pfn = sym_to_pfn(vdso_info[abi].vdso_code_start);
c7aa2d71 107
d3418f38 108 for (i = 0; i < vdso_info[abi].vdso_pages; i++)
d53b5c01 109 vdso_pagelist[i] = pfn_to_page(pfn + i);
c7aa2d71 110
d53b5c01 111 vdso_info[abi].cm->pages = vdso_pagelist;
c7aa2d71
VF
112
113 return 0;
114}
115
1b6867d2 116#ifdef CONFIG_TIME_NS
3503d56c
AV
117struct vdso_data *arch_get_vdso_data(void *vvar_page)
118{
119 return (struct vdso_data *)(vvar_page);
120}
121
1b6867d2
AV
122/*
123 * The vvar mapping contains data for a specific time namespace, so when a task
124 * changes namespace we must unmap its vvar data for the old namespace.
125 * Subsequent faults will map in data for the new namespace.
126 *
127 * For more details see timens_setup_vdso_data().
128 */
129int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
130{
131 struct mm_struct *mm = task->mm;
132 struct vm_area_struct *vma;
de2b84d2 133 VMA_ITERATOR(vmi, mm, 0);
1b6867d2
AV
134
135 mmap_read_lock(mm);
136
de2b84d2 137 for_each_vma(vmi, vma) {
1b6867d2 138 if (vma_is_special_mapping(vma, vdso_info[VDSO_ABI_AA64].dm))
e9adcfec 139 zap_vma_pages(vma);
1b6867d2
AV
140#ifdef CONFIG_COMPAT_VDSO
141 if (vma_is_special_mapping(vma, vdso_info[VDSO_ABI_AA32].dm))
e9adcfec 142 zap_vma_pages(vma);
1b6867d2
AV
143#endif
144 }
145
146 mmap_read_unlock(mm);
147 return 0;
148}
149#endif
150
d53b5c01
AV
151static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
152 struct vm_area_struct *vma, struct vm_fault *vmf)
153{
ee3cda8e
AV
154 struct page *timens_page = find_timens_vvar_page(vma);
155 unsigned long pfn;
156
157 switch (vmf->pgoff) {
158 case VVAR_DATA_PAGE_OFFSET:
159 if (timens_page)
160 pfn = page_to_pfn(timens_page);
161 else
162 pfn = sym_to_pfn(vdso_data);
163 break;
164#ifdef CONFIG_TIME_NS
165 case VVAR_TIMENS_PAGE_OFFSET:
166 /*
167 * If a task belongs to a time namespace then a namespace
168 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
169 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
170 * offset.
171 * See also the comment near timens_setup_vdso_data().
172 */
173 if (!timens_page)
174 return VM_FAULT_SIGBUS;
175 pfn = sym_to_pfn(vdso_data);
176 break;
177#endif /* CONFIG_TIME_NS */
178 default:
179 return VM_FAULT_SIGBUS;
180 }
181
182 return vmf_insert_pfn(vma, vmf->address, pfn);
d53b5c01
AV
183}
184
d3418f38 185static int __setup_additional_pages(enum vdso_abi abi,
c7aa2d71
VF
186 struct mm_struct *mm,
187 struct linux_binprm *bprm,
188 int uses_interp)
189{
190 unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
bf740a90 191 unsigned long gp_flags = 0;
c7aa2d71
VF
192 void *ret;
193
3503d56c
AV
194 BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
195
d3418f38 196 vdso_text_len = vdso_info[abi].vdso_pages << PAGE_SHIFT;
c7aa2d71 197 /* Be sure to map the data page */
3503d56c 198 vdso_mapping_len = vdso_text_len + VVAR_NR_PAGES * PAGE_SIZE;
c7aa2d71
VF
199
200 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
201 if (IS_ERR_VALUE(vdso_base)) {
202 ret = ERR_PTR(vdso_base);
203 goto up_fail;
204 }
205
3503d56c 206 ret = _install_special_mapping(mm, vdso_base, VVAR_NR_PAGES * PAGE_SIZE,
d53b5c01 207 VM_READ|VM_MAYREAD|VM_PFNMAP,
d3418f38 208 vdso_info[abi].dm);
c7aa2d71
VF
209 if (IS_ERR(ret))
210 goto up_fail;
211
bbbb6577 212 if (system_supports_bti_kernel())
bf740a90
MB
213 gp_flags = VM_ARM64_BTI;
214
3503d56c 215 vdso_base += VVAR_NR_PAGES * PAGE_SIZE;
c7aa2d71
VF
216 mm->context.vdso = (void *)vdso_base;
217 ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
bf740a90 218 VM_READ|VM_EXEC|gp_flags|
c7aa2d71 219 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
d3418f38 220 vdso_info[abi].cm);
c7aa2d71
VF
221 if (IS_ERR(ret))
222 goto up_fail;
223
224 return 0;
225
226up_fail:
227 mm->context.vdso = NULL;
228 return PTR_ERR(ret);
229}
230
9031fefd
WD
231#ifdef CONFIG_COMPAT
232/*
233 * Create and map the vectors page for AArch32 tasks.
234 */
1d09094a
MR
235enum aarch32_map {
236 AA32_MAP_VECTORS, /* kuser helpers */
0cbc2659 237 AA32_MAP_SIGPAGE,
1d09094a
MR
238 AA32_MAP_VVAR,
239 AA32_MAP_VDSO,
1d09094a 240};
74fc72e7
MR
241
242static struct page *aarch32_vectors_page __ro_after_init;
74fc72e7 243static struct page *aarch32_sig_page __ro_after_init;
74fc72e7 244
7adbf10e
WD
245static int aarch32_sigpage_mremap(const struct vm_special_mapping *sm,
246 struct vm_area_struct *new_vma)
247{
248 current->mm->context.sigpage = (void *)new_vma->vm_start;
249
250 return 0;
251}
252
1d09094a
MR
253static struct vm_special_mapping aarch32_vdso_maps[] = {
254 [AA32_MAP_VECTORS] = {
0d747f65 255 .name = "[vectors]", /* ABI */
74fc72e7 256 .pages = &aarch32_vectors_page,
0d747f65 257 },
0cbc2659
WD
258 [AA32_MAP_SIGPAGE] = {
259 .name = "[sigpage]", /* ABI */
260 .pages = &aarch32_sig_page,
7adbf10e 261 .mremap = aarch32_sigpage_mremap,
0cbc2659 262 },
1d09094a 263 [AA32_MAP_VVAR] = {
7c1deeeb 264 .name = "[vvar]",
d53b5c01 265 .fault = vvar_fault,
7c1deeeb 266 },
1d09094a 267 [AA32_MAP_VDSO] = {
7c1deeeb 268 .name = "[vdso]",
871402e0 269 .mremap = vdso_mremap,
7c1deeeb 270 },
0d747f65 271};
9031fefd 272
1255a734 273static int aarch32_alloc_kuser_vdso_page(void)
9031fefd
WD
274{
275 extern char __kuser_helper_start[], __kuser_helper_end[];
276 int kuser_sz = __kuser_helper_end - __kuser_helper_start;
1255a734 277 unsigned long vdso_page;
9031fefd 278
af1b3cf2
VF
279 if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
280 return 0;
281
7cd6ca1d 282 vdso_page = get_zeroed_page(GFP_KERNEL);
1255a734 283 if (!vdso_page)
0d747f65 284 return -ENOMEM;
9031fefd 285
1255a734 286 memcpy((void *)(vdso_page + 0x1000 - kuser_sz), __kuser_helper_start,
0d747f65 287 kuser_sz);
b0abde80 288 aarch32_vectors_page = virt_to_page((void *)vdso_page);
1255a734
VF
289 return 0;
290}
291
6e554abd 292#define COMPAT_SIGPAGE_POISON_WORD 0xe7fddef1
a39060b0 293static int aarch32_alloc_sigpage(void)
1255a734
VF
294{
295 extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
296 int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
6e554abd
WD
297 __le32 poison = cpu_to_le32(COMPAT_SIGPAGE_POISON_WORD);
298 void *sigpage;
9031fefd 299
6e554abd 300 sigpage = (void *)__get_free_page(GFP_KERNEL);
1255a734
VF
301 if (!sigpage)
302 return -ENOMEM;
0d747f65 303
6e554abd
WD
304 memset32(sigpage, (__force u32)poison, PAGE_SIZE / sizeof(poison));
305 memcpy(sigpage, __aarch32_sigret_code_start, sigret_sz);
74fc72e7 306 aarch32_sig_page = virt_to_page(sigpage);
a39060b0
WD
307 return 0;
308}
9031fefd 309
a7dcf58a 310static int __init __aarch32_alloc_vdso_pages(void)
a39060b0 311{
0cbc2659
WD
312
313 if (!IS_ENABLED(CONFIG_COMPAT_VDSO))
314 return 0;
315
a39060b0
WD
316 vdso_info[VDSO_ABI_AA32].dm = &aarch32_vdso_maps[AA32_MAP_VVAR];
317 vdso_info[VDSO_ABI_AA32].cm = &aarch32_vdso_maps[AA32_MAP_VDSO];
9031fefd 318
a39060b0 319 return __vdso_init(VDSO_ABI_AA32);
9031fefd 320}
7c1deeeb
VF
321
322static int __init aarch32_alloc_vdso_pages(void)
323{
a39060b0
WD
324 int ret;
325
a39060b0
WD
326 ret = __aarch32_alloc_vdso_pages();
327 if (ret)
328 return ret;
a39060b0
WD
329
330 ret = aarch32_alloc_sigpage();
331 if (ret)
332 return ret;
333
334 return aarch32_alloc_kuser_vdso_page();
7c1deeeb 335}
0d747f65 336arch_initcall(aarch32_alloc_vdso_pages);
9031fefd 337
0d747f65 338static int aarch32_kuser_helpers_setup(struct mm_struct *mm)
9031fefd 339{
0d747f65 340 void *ret;
2fea7f6c 341
af1b3cf2
VF
342 if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
343 return 0;
344
0d747f65
VF
345 /*
346 * Avoid VM_MAYWRITE for compatibility with arch/arm/, where it's
347 * not safe to CoW the page containing the CPU exception vectors.
348 */
349 ret = _install_special_mapping(mm, AARCH32_VECTORS_BASE, PAGE_SIZE,
350 VM_READ | VM_EXEC |
351 VM_MAYREAD | VM_MAYEXEC,
1d09094a 352 &aarch32_vdso_maps[AA32_MAP_VECTORS]);
0d747f65
VF
353
354 return PTR_ERR_OR_ZERO(ret);
355}
356
357static int aarch32_sigreturn_setup(struct mm_struct *mm)
358{
359 unsigned long addr;
2fea7f6c 360 void *ret;
9031fefd 361
0d747f65
VF
362 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
363 if (IS_ERR_VALUE(addr)) {
364 ret = ERR_PTR(addr);
365 goto out;
366 }
9031fefd 367
0d747f65
VF
368 /*
369 * VM_MAYWRITE is required to allow gdb to Copy-on-Write and
370 * set breakpoints.
371 */
2fea7f6c 372 ret = _install_special_mapping(mm, addr, PAGE_SIZE,
0d747f65
VF
373 VM_READ | VM_EXEC | VM_MAYREAD |
374 VM_MAYWRITE | VM_MAYEXEC,
1d09094a 375 &aarch32_vdso_maps[AA32_MAP_SIGPAGE]);
0d747f65
VF
376 if (IS_ERR(ret))
377 goto out;
9031fefd 378
a39060b0 379 mm->context.sigpage = (void *)addr;
9031fefd 380
0d747f65 381out:
2fea7f6c 382 return PTR_ERR_OR_ZERO(ret);
9031fefd 383}
0d747f65
VF
384
385int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
386{
387 struct mm_struct *mm = current->mm;
388 int ret;
389
d8ed45c5 390 if (mmap_write_lock_killable(mm))
0d747f65
VF
391 return -EINTR;
392
393 ret = aarch32_kuser_helpers_setup(mm);
394 if (ret)
395 goto out;
396
0cbc2659 397 if (IS_ENABLED(CONFIG_COMPAT_VDSO)) {
2a30aca8 398 ret = __setup_additional_pages(VDSO_ABI_AA32, mm, bprm,
0cbc2659
WD
399 uses_interp);
400 if (ret)
401 goto out;
402 }
0d747f65 403
a39060b0 404 ret = aarch32_sigreturn_setup(mm);
0d747f65 405out:
d8ed45c5 406 mmap_write_unlock(mm);
0d747f65
VF
407 return ret;
408}
9031fefd
WD
409#endif /* CONFIG_COMPAT */
410
1d09094a
MR
411enum aarch64_map {
412 AA64_MAP_VVAR,
413 AA64_MAP_VDSO,
414};
415
416static struct vm_special_mapping aarch64_vdso_maps[] __ro_after_init = {
417 [AA64_MAP_VVAR] = {
5a9e3e15 418 .name = "[vvar]",
d53b5c01 419 .fault = vvar_fault,
5a9e3e15 420 },
1d09094a 421 [AA64_MAP_VDSO] = {
5a9e3e15 422 .name = "[vdso]",
73958695 423 .mremap = vdso_mremap,
5a9e3e15
JZ
424 },
425};
2fea7f6c 426
9031fefd
WD
427static int __init vdso_init(void)
428{
1d09094a
MR
429 vdso_info[VDSO_ABI_AA64].dm = &aarch64_vdso_maps[AA64_MAP_VVAR];
430 vdso_info[VDSO_ABI_AA64].cm = &aarch64_vdso_maps[AA64_MAP_VDSO];
601255ae 431
d3418f38 432 return __vdso_init(VDSO_ABI_AA64);
9031fefd
WD
433}
434arch_initcall(vdso_init);
435
2a30aca8 436int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
9031fefd
WD
437{
438 struct mm_struct *mm = current->mm;
c7aa2d71 439 int ret;
9031fefd 440
d8ed45c5 441 if (mmap_write_lock_killable(mm))
69048176 442 return -EINTR;
9031fefd 443
2a30aca8 444 ret = __setup_additional_pages(VDSO_ABI_AA64, mm, bprm, uses_interp);
d8ed45c5 445 mmap_write_unlock(mm);
9031fefd 446
c7aa2d71 447 return ret;
9031fefd 448}