xref: /openbmc/linux/arch/s390/kernel/vdso.c (revision 2f3f53d6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * vdso setup for s390
4  *
5  *  Copyright IBM Corp. 2008
6  *  Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7  */
8 
9 #include <linux/binfmts.h>
10 #include <linux/compat.h>
11 #include <linux/elf.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/smp.h>
18 #include <linux/time_namespace.h>
19 #include <linux/random.h>
20 #include <vdso/datapage.h>
21 #include <asm/vdso.h>
22 
23 extern char vdso64_start[], vdso64_end[];
24 extern char vdso32_start[], vdso32_end[];
25 
26 static struct vm_special_mapping vvar_mapping;
27 
28 static union {
29 	struct vdso_data	data[CS_BASES];
30 	u8			page[PAGE_SIZE];
31 } vdso_data_store __page_aligned_data;
32 
33 struct vdso_data *vdso_data = vdso_data_store.data;
34 
35 enum vvar_pages {
36 	VVAR_DATA_PAGE_OFFSET,
37 	VVAR_TIMENS_PAGE_OFFSET,
38 	VVAR_NR_PAGES,
39 };
40 
41 #ifdef CONFIG_TIME_NS
42 struct vdso_data *arch_get_vdso_data(void *vvar_page)
43 {
44 	return (struct vdso_data *)(vvar_page);
45 }
46 
47 static struct page *find_timens_vvar_page(struct vm_area_struct *vma)
48 {
49 	if (likely(vma->vm_mm == current->mm))
50 		return current->nsproxy->time_ns->vvar_page;
51 	/*
52 	 * VM_PFNMAP | VM_IO protect .fault() handler from being called
53 	 * through interfaces like /proc/$pid/mem or
54 	 * process_vm_{readv,writev}() as long as there's no .access()
55 	 * in special_mapping_vmops().
56 	 * For more details check_vma_flags() and __access_remote_vm()
57 	 */
58 	WARN(1, "vvar_page accessed remotely");
59 	return NULL;
60 }
61 
62 /*
63  * The VVAR page layout depends on whether a task belongs to the root or
64  * non-root time namespace. Whenever a task changes its namespace, the VVAR
65  * page tables are cleared and then they will be re-faulted with a
66  * corresponding layout.
67  * See also the comment near timens_setup_vdso_data() for details.
68  */
69 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
70 {
71 	struct mm_struct *mm = task->mm;
72 	VMA_ITERATOR(vmi, mm, 0);
73 	struct vm_area_struct *vma;
74 
75 	mmap_read_lock(mm);
76 	for_each_vma(vmi, vma) {
77 		unsigned long size = vma->vm_end - vma->vm_start;
78 
79 		if (!vma_is_special_mapping(vma, &vvar_mapping))
80 			continue;
81 		zap_page_range(vma, vma->vm_start, size);
82 		break;
83 	}
84 	mmap_read_unlock(mm);
85 	return 0;
86 }
87 #else
88 static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
89 {
90 	return NULL;
91 }
92 #endif
93 
94 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
95 			     struct vm_area_struct *vma, struct vm_fault *vmf)
96 {
97 	struct page *timens_page = find_timens_vvar_page(vma);
98 	unsigned long addr, pfn;
99 	vm_fault_t err;
100 
101 	switch (vmf->pgoff) {
102 	case VVAR_DATA_PAGE_OFFSET:
103 		pfn = virt_to_pfn(vdso_data);
104 		if (timens_page) {
105 			/*
106 			 * Fault in VVAR page too, since it will be accessed
107 			 * to get clock data anyway.
108 			 */
109 			addr = vmf->address + VVAR_TIMENS_PAGE_OFFSET * PAGE_SIZE;
110 			err = vmf_insert_pfn(vma, addr, pfn);
111 			if (unlikely(err & VM_FAULT_ERROR))
112 				return err;
113 			pfn = page_to_pfn(timens_page);
114 		}
115 		break;
116 #ifdef CONFIG_TIME_NS
117 	case VVAR_TIMENS_PAGE_OFFSET:
118 		/*
119 		 * If a task belongs to a time namespace then a namespace
120 		 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
121 		 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
122 		 * offset.
123 		 * See also the comment near timens_setup_vdso_data().
124 		 */
125 		if (!timens_page)
126 			return VM_FAULT_SIGBUS;
127 		pfn = virt_to_pfn(vdso_data);
128 		break;
129 #endif /* CONFIG_TIME_NS */
130 	default:
131 		return VM_FAULT_SIGBUS;
132 	}
133 	return vmf_insert_pfn(vma, vmf->address, pfn);
134 }
135 
136 static int vdso_mremap(const struct vm_special_mapping *sm,
137 		       struct vm_area_struct *vma)
138 {
139 	current->mm->context.vdso_base = vma->vm_start;
140 	return 0;
141 }
142 
143 static struct vm_special_mapping vvar_mapping = {
144 	.name = "[vvar]",
145 	.fault = vvar_fault,
146 };
147 
148 static struct vm_special_mapping vdso64_mapping = {
149 	.name = "[vdso]",
150 	.mremap = vdso_mremap,
151 };
152 
153 static struct vm_special_mapping vdso32_mapping = {
154 	.name = "[vdso]",
155 	.mremap = vdso_mremap,
156 };
157 
158 int vdso_getcpu_init(void)
159 {
160 	set_tod_programmable_field(smp_processor_id());
161 	return 0;
162 }
163 early_initcall(vdso_getcpu_init); /* Must be called before SMP init */
164 
165 static int map_vdso(unsigned long addr, unsigned long vdso_mapping_len)
166 {
167 	unsigned long vvar_start, vdso_text_start, vdso_text_len;
168 	struct vm_special_mapping *vdso_mapping;
169 	struct mm_struct *mm = current->mm;
170 	struct vm_area_struct *vma;
171 	int rc;
172 
173 	BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
174 	if (mmap_write_lock_killable(mm))
175 		return -EINTR;
176 
177 	if (is_compat_task()) {
178 		vdso_text_len = vdso32_end - vdso32_start;
179 		vdso_mapping = &vdso32_mapping;
180 	} else {
181 		vdso_text_len = vdso64_end - vdso64_start;
182 		vdso_mapping = &vdso64_mapping;
183 	}
184 	vvar_start = get_unmapped_area(NULL, addr, vdso_mapping_len, 0, 0);
185 	rc = vvar_start;
186 	if (IS_ERR_VALUE(vvar_start))
187 		goto out;
188 	vma = _install_special_mapping(mm, vvar_start, VVAR_NR_PAGES*PAGE_SIZE,
189 				       VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
190 				       VM_PFNMAP,
191 				       &vvar_mapping);
192 	rc = PTR_ERR(vma);
193 	if (IS_ERR(vma))
194 		goto out;
195 	vdso_text_start = vvar_start + VVAR_NR_PAGES * PAGE_SIZE;
196 	/* VM_MAYWRITE for COW so gdb can set breakpoints */
197 	vma = _install_special_mapping(mm, vdso_text_start, vdso_text_len,
198 				       VM_READ|VM_EXEC|
199 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
200 				       vdso_mapping);
201 	if (IS_ERR(vma)) {
202 		do_munmap(mm, vvar_start, PAGE_SIZE, NULL);
203 		rc = PTR_ERR(vma);
204 	} else {
205 		current->mm->context.vdso_base = vdso_text_start;
206 		rc = 0;
207 	}
208 out:
209 	mmap_write_unlock(mm);
210 	return rc;
211 }
212 
213 static unsigned long vdso_addr(unsigned long start, unsigned long len)
214 {
215 	unsigned long addr, end, offset;
216 
217 	/*
218 	 * Round up the start address. It can start out unaligned as a result
219 	 * of stack start randomization.
220 	 */
221 	start = PAGE_ALIGN(start);
222 
223 	/* Round the lowest possible end address up to a PMD boundary. */
224 	end = (start + len + PMD_SIZE - 1) & PMD_MASK;
225 	if (end >= VDSO_BASE)
226 		end = VDSO_BASE;
227 	end -= len;
228 
229 	if (end > start) {
230 		offset = prandom_u32_max(((end - start) >> PAGE_SHIFT) + 1);
231 		addr = start + (offset << PAGE_SHIFT);
232 	} else {
233 		addr = start;
234 	}
235 	return addr;
236 }
237 
238 unsigned long vdso_size(void)
239 {
240 	unsigned long size = VVAR_NR_PAGES * PAGE_SIZE;
241 
242 	if (is_compat_task())
243 		size += vdso32_end - vdso32_start;
244 	else
245 		size += vdso64_end - vdso64_start;
246 	return PAGE_ALIGN(size);
247 }
248 
249 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
250 {
251 	unsigned long addr = VDSO_BASE;
252 	unsigned long size = vdso_size();
253 
254 	if (current->flags & PF_RANDOMIZE)
255 		addr = vdso_addr(current->mm->start_stack + PAGE_SIZE, size);
256 	return map_vdso(addr, size);
257 }
258 
259 static struct page ** __init vdso_setup_pages(void *start, void *end)
260 {
261 	int pages = (end - start) >> PAGE_SHIFT;
262 	struct page **pagelist;
263 	int i;
264 
265 	pagelist = kcalloc(pages + 1, sizeof(struct page *), GFP_KERNEL);
266 	if (!pagelist)
267 		panic("%s: Cannot allocate page list for VDSO", __func__);
268 	for (i = 0; i < pages; i++)
269 		pagelist[i] = virt_to_page(start + i * PAGE_SIZE);
270 	return pagelist;
271 }
272 
273 static int __init vdso_init(void)
274 {
275 	vdso64_mapping.pages = vdso_setup_pages(vdso64_start, vdso64_end);
276 	if (IS_ENABLED(CONFIG_COMPAT))
277 		vdso32_mapping.pages = vdso_setup_pages(vdso32_start, vdso32_end);
278 	return 0;
279 }
280 arch_initcall(vdso_init);
281