xref: /openbmc/linux/arch/riscv/kernel/vdso.c (revision 26ba4e57)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
4  *                    <benh@kernel.crashing.org>
5  * Copyright (C) 2012 ARM Limited
6  * Copyright (C) 2015 Regents of the University of California
7  */
8 
9 #include <linux/mm.h>
10 #include <linux/slab.h>
11 #include <linux/binfmts.h>
12 #include <linux/err.h>
13 
14 #include <asm/vdso.h>
15 
16 extern char vdso_start[], vdso_end[];
17 
18 static unsigned int vdso_pages;
19 static struct page **vdso_pagelist;
20 
21 /*
22  * The vDSO data page.
23  */
24 static union {
25 	struct vdso_data	data;
26 	u8			page[PAGE_SIZE];
27 } vdso_data_store __page_aligned_data;
28 struct vdso_data *vdso_data = &vdso_data_store.data;
29 
30 static int __init vdso_init(void)
31 {
32 	unsigned int i;
33 
34 	vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
35 	vdso_pagelist =
36 		kcalloc(vdso_pages + 1, sizeof(struct page *), GFP_KERNEL);
37 	if (unlikely(vdso_pagelist == NULL)) {
38 		pr_err("vdso: pagelist allocation failed\n");
39 		return -ENOMEM;
40 	}
41 
42 	for (i = 0; i < vdso_pages; i++) {
43 		struct page *pg;
44 
45 		pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
46 		vdso_pagelist[i] = pg;
47 	}
48 	vdso_pagelist[i] = virt_to_page(vdso_data);
49 
50 	return 0;
51 }
52 arch_initcall(vdso_init);
53 
54 int arch_setup_additional_pages(struct linux_binprm *bprm,
55 	int uses_interp)
56 {
57 	struct mm_struct *mm = current->mm;
58 	unsigned long vdso_base, vdso_len;
59 	int ret;
60 
61 	vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
62 
63 	down_write(&mm->mmap_sem);
64 	vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
65 	if (IS_ERR_VALUE(vdso_base)) {
66 		ret = vdso_base;
67 		goto end;
68 	}
69 
70 	/*
71 	 * Put vDSO base into mm struct. We need to do this before calling
72 	 * install_special_mapping or the perf counter mmap tracking code
73 	 * will fail to recognise it as a vDSO (since arch_vma_name fails).
74 	 */
75 	mm->context.vdso = (void *)vdso_base;
76 
77 	ret = install_special_mapping(mm, vdso_base, vdso_len,
78 		(VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
79 		vdso_pagelist);
80 
81 	if (unlikely(ret))
82 		mm->context.vdso = NULL;
83 
84 end:
85 	up_write(&mm->mmap_sem);
86 	return ret;
87 }
88 
89 const char *arch_vma_name(struct vm_area_struct *vma)
90 {
91 	if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso))
92 		return "[vdso]";
93 	return NULL;
94 }
95