xref: /linux/arch/riscv/kernel/vdso.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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/elf.h>
10 #include <linux/mm.h>
11 #include <linux/slab.h>
12 #include <linux/binfmts.h>
13 #include <linux/err.h>
14 #include <asm/page.h>
15 #include <asm/vdso.h>
16 #include <linux/vdso_datastore.h>
17 #include <vdso/datapage.h>
18 #include <vdso/vsyscall.h>
19 
20 #define VVAR_SIZE  (VDSO_NR_PAGES << PAGE_SHIFT)
21 
22 struct __vdso_info {
23 	const char *name;
24 	const char *vdso_code_start;
25 	const char *vdso_code_end;
26 	unsigned long vdso_pages;
27 	/* Code Mapping */
28 	struct vm_special_mapping *cm;
29 };
30 
31 static struct __vdso_info vdso_info;
32 #ifdef CONFIG_COMPAT
33 static struct __vdso_info compat_vdso_info;
34 #endif
35 
36 static int vdso_mremap(const struct vm_special_mapping *sm,
37 		       struct vm_area_struct *new_vma)
38 {
39 	current->mm->context.vdso = (void *)new_vma->vm_start;
40 
41 	return 0;
42 }
43 
44 static void __init __vdso_init(struct __vdso_info *vdso_info)
45 {
46 	unsigned int i;
47 	struct page **vdso_pagelist;
48 	unsigned long pfn;
49 
50 	if (memcmp(vdso_info->vdso_code_start, "\177ELF", 4))
51 		panic("vDSO is not a valid ELF object!\n");
52 
53 	vdso_info->vdso_pages = (
54 		vdso_info->vdso_code_end -
55 		vdso_info->vdso_code_start) >>
56 		PAGE_SHIFT;
57 
58 	vdso_pagelist = kzalloc_objs(struct page *, vdso_info->vdso_pages,
59 				     GFP_KERNEL);
60 	if (vdso_pagelist == NULL)
61 		panic("vDSO kcalloc failed!\n");
62 
63 	/* Grab the vDSO code pages. */
64 	pfn = sym_to_pfn(vdso_info->vdso_code_start);
65 
66 	for (i = 0; i < vdso_info->vdso_pages; i++)
67 		vdso_pagelist[i] = pfn_to_page(pfn + i);
68 
69 	vdso_info->cm->pages = vdso_pagelist;
70 }
71 
72 static struct vm_special_mapping rv_vdso_map __ro_after_init = {
73 	.name   = "[vdso]",
74 	.mremap = vdso_mremap,
75 };
76 
77 static struct __vdso_info vdso_info __ro_after_init = {
78 	.name = "vdso",
79 	.vdso_code_start = vdso_start,
80 	.vdso_code_end = vdso_end,
81 	.cm = &rv_vdso_map,
82 };
83 
84 #ifdef CONFIG_COMPAT
85 static struct vm_special_mapping rv_compat_vdso_map __ro_after_init = {
86 	.name   = "[vdso]",
87 	.mremap = vdso_mremap,
88 };
89 
90 static struct __vdso_info compat_vdso_info __ro_after_init = {
91 	.name = "compat_vdso",
92 	.vdso_code_start = compat_vdso_start,
93 	.vdso_code_end = compat_vdso_end,
94 	.cm = &rv_compat_vdso_map,
95 };
96 #endif
97 
98 static int __init vdso_init(void)
99 {
100 	/* Hart implements zimop, expose cfi compiled vdso */
101 	if (IS_ENABLED(CONFIG_RISCV_USER_CFI) &&
102 	    riscv_has_extension_unlikely(RISCV_ISA_EXT_ZIMOP)) {
103 		vdso_info.vdso_code_start = vdso_cfi_start;
104 		vdso_info.vdso_code_end = vdso_cfi_end;
105 	}
106 
107 	__vdso_init(&vdso_info);
108 #ifdef CONFIG_COMPAT
109 	__vdso_init(&compat_vdso_info);
110 #endif
111 
112 	return 0;
113 }
114 arch_initcall(vdso_init);
115 
116 static int __setup_additional_pages(struct mm_struct *mm,
117 				    struct linux_binprm *bprm,
118 				    int uses_interp,
119 				    struct __vdso_info *vdso_info)
120 {
121 	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
122 	void *ret;
123 
124 	BUILD_BUG_ON(VDSO_NR_PAGES != __VDSO_PAGES);
125 
126 	vdso_text_len = vdso_info->vdso_pages << PAGE_SHIFT;
127 	/* Be sure to map the data page */
128 	vdso_mapping_len = vdso_text_len + VVAR_SIZE;
129 
130 	vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
131 	if (IS_ERR_VALUE(vdso_base)) {
132 		ret = ERR_PTR(vdso_base);
133 		goto up_fail;
134 	}
135 
136 	ret = vdso_install_vvar_mapping(mm, vdso_base);
137 	if (IS_ERR(ret))
138 		goto up_fail;
139 
140 	vdso_base += VVAR_SIZE;
141 	mm->context.vdso = (void *)vdso_base;
142 
143 	ret =
144 	   _install_special_mapping(mm, vdso_base, vdso_text_len,
145 		(VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC | VM_SEALED_SYSMAP),
146 		vdso_info->cm);
147 
148 	if (IS_ERR(ret))
149 		goto up_fail;
150 
151 	return 0;
152 
153 up_fail:
154 	mm->context.vdso = NULL;
155 	return PTR_ERR(ret);
156 }
157 
158 #ifdef CONFIG_COMPAT
159 int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
160 				       int uses_interp)
161 {
162 	struct mm_struct *mm = current->mm;
163 	int ret;
164 
165 	if (mmap_write_lock_killable(mm))
166 		return -EINTR;
167 
168 	ret = __setup_additional_pages(mm, bprm, uses_interp,
169 							&compat_vdso_info);
170 	mmap_write_unlock(mm);
171 
172 	return ret;
173 }
174 #endif
175 
176 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
177 {
178 	struct mm_struct *mm = current->mm;
179 	int ret;
180 
181 	if (mmap_write_lock_killable(mm))
182 		return -EINTR;
183 
184 	ret = __setup_additional_pages(mm, bprm, uses_interp, &vdso_info);
185 	mmap_write_unlock(mm);
186 
187 	return ret;
188 }
189