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, sm: &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, addr: 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, addr: vvar_start, len: VVAR_NR_PAGES*PAGE_SIZE, |
164 | VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP| |
165 | VM_PFNMAP, |
166 | spec: &vvar_mapping); |
167 | rc = PTR_ERR(ptr: vma); |
168 | if (IS_ERR(ptr: 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, addr: vdso_text_start, len: vdso_text_len, |
173 | VM_READ|VM_EXEC| |
174 | VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, |
175 | spec: vdso_mapping); |
176 | if (IS_ERR(ptr: vma)) { |
177 | do_munmap(mm, vvar_start, PAGE_SIZE, NULL); |
178 | rc = PTR_ERR(ptr: 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(ceil: ((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_size(void) |
214 | { |
215 | unsigned long size = VVAR_NR_PAGES * PAGE_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 | int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) |
225 | { |
226 | unsigned long addr = VDSO_BASE; |
227 | unsigned long size = vdso_size(); |
228 | |
229 | if (current->flags & PF_RANDOMIZE) |
230 | addr = vdso_addr(current->mm->start_stack + PAGE_SIZE, len: size); |
231 | return map_vdso(addr, vdso_mapping_len: size); |
232 | } |
233 | |
234 | static struct page ** __init vdso_setup_pages(void *start, void *end) |
235 | { |
236 | int pages = (end - start) >> PAGE_SHIFT; |
237 | struct page **pagelist; |
238 | int i; |
239 | |
240 | pagelist = kcalloc(n: pages + 1, size: sizeof(struct page *), GFP_KERNEL); |
241 | if (!pagelist) |
242 | panic(fmt: "%s: Cannot allocate page list for VDSO" , __func__); |
243 | for (i = 0; i < pages; i++) |
244 | pagelist[i] = virt_to_page(start + i * PAGE_SIZE); |
245 | return pagelist; |
246 | } |
247 | |
248 | static int __init vdso_init(void) |
249 | { |
250 | vdso64_mapping.pages = vdso_setup_pages(start: vdso64_start, end: vdso64_end); |
251 | if (IS_ENABLED(CONFIG_COMPAT)) |
252 | vdso32_mapping.pages = vdso_setup_pages(start: vdso32_start, end: vdso32_end); |
253 | return 0; |
254 | } |
255 | arch_initcall(vdso_init); |
256 | |