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