1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright IBM Corp. 2008
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
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>
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>
23 extern char vdso64_start[], vdso64_end[];
24 extern char vdso32_start[], vdso32_end[];
26 static struct vm_special_mapping vvar_mapping;
29 struct vdso_data data[CS_BASES];
31 } vdso_data_store __page_aligned_data;
33 struct vdso_data *vdso_data = vdso_data_store.data;
36 VVAR_DATA_PAGE_OFFSET,
37 VVAR_TIMENS_PAGE_OFFSET,
42 struct vdso_data *arch_get_vdso_data(void *vvar_page)
44 return (struct vdso_data *)(vvar_page);
47 static struct page *find_timens_vvar_page(struct vm_area_struct *vma)
49 if (likely(vma->vm_mm == current->mm))
50 return current->nsproxy->time_ns->vvar_page;
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()
58 WARN(1, "vvar_page accessed remotely");
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.
69 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
71 struct mm_struct *mm = task->mm;
72 VMA_ITERATOR(vmi, mm, 0);
73 struct vm_area_struct *vma;
76 for_each_vma(vmi, vma) {
77 unsigned long size = vma->vm_end - vma->vm_start;
79 if (!vma_is_special_mapping(vma, &vvar_mapping))
81 zap_page_range(vma, vma->vm_start, size);
88 static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
94 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
95 struct vm_area_struct *vma, struct vm_fault *vmf)
97 struct page *timens_page = find_timens_vvar_page(vma);
98 unsigned long addr, pfn;
101 switch (vmf->pgoff) {
102 case VVAR_DATA_PAGE_OFFSET:
103 pfn = virt_to_pfn(vdso_data);
106 * Fault in VVAR page too, since it will be accessed
107 * to get clock data anyway.
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))
113 pfn = page_to_pfn(timens_page);
116 #ifdef CONFIG_TIME_NS
117 case VVAR_TIMENS_PAGE_OFFSET:
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
123 * See also the comment near timens_setup_vdso_data().
126 return VM_FAULT_SIGBUS;
127 pfn = virt_to_pfn(vdso_data);
129 #endif /* CONFIG_TIME_NS */
131 return VM_FAULT_SIGBUS;
133 return vmf_insert_pfn(vma, vmf->address, pfn);
136 static int vdso_mremap(const struct vm_special_mapping *sm,
137 struct vm_area_struct *vma)
139 current->mm->context.vdso_base = vma->vm_start;
143 static struct vm_special_mapping vvar_mapping = {
148 static struct vm_special_mapping vdso64_mapping = {
150 .mremap = vdso_mremap,
153 static struct vm_special_mapping vdso32_mapping = {
155 .mremap = vdso_mremap,
158 int vdso_getcpu_init(void)
160 set_tod_programmable_field(smp_processor_id());
163 early_initcall(vdso_getcpu_init); /* Must be called before SMP init */
165 static int map_vdso(unsigned long addr, unsigned long vdso_mapping_len)
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;
173 BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
174 if (mmap_write_lock_killable(mm))
177 if (is_compat_task()) {
178 vdso_text_len = vdso32_end - vdso32_start;
179 vdso_mapping = &vdso32_mapping;
181 vdso_text_len = vdso64_end - vdso64_start;
182 vdso_mapping = &vdso64_mapping;
184 vvar_start = get_unmapped_area(NULL, addr, vdso_mapping_len, 0, 0);
186 if (IS_ERR_VALUE(vvar_start))
188 vma = _install_special_mapping(mm, vvar_start, VVAR_NR_PAGES*PAGE_SIZE,
189 VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
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,
199 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
202 do_munmap(mm, vvar_start, PAGE_SIZE, NULL);
205 current->mm->context.vdso_base = vdso_text_start;
209 mmap_write_unlock(mm);
213 static unsigned long vdso_addr(unsigned long start, unsigned long len)
215 unsigned long addr, end, offset;
218 * Round up the start address. It can start out unaligned as a result
219 * of stack start randomization.
221 start = PAGE_ALIGN(start);
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)
230 offset = prandom_u32_max(((end - start) >> PAGE_SHIFT) + 1);
231 addr = start + (offset << PAGE_SHIFT);
238 unsigned long vdso_size(void)
240 unsigned long size = VVAR_NR_PAGES * PAGE_SIZE;
242 if (is_compat_task())
243 size += vdso32_end - vdso32_start;
245 size += vdso64_end - vdso64_start;
246 return PAGE_ALIGN(size);
249 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
251 unsigned long addr = VDSO_BASE;
252 unsigned long size = vdso_size();
254 if (current->flags & PF_RANDOMIZE)
255 addr = vdso_addr(current->mm->start_stack + PAGE_SIZE, size);
256 return map_vdso(addr, size);
259 static struct page ** __init vdso_setup_pages(void *start, void *end)
261 int pages = (end - start) >> PAGE_SHIFT;
262 struct page **pagelist;
265 pagelist = kcalloc(pages + 1, sizeof(struct page *), GFP_KERNEL);
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);
273 static int __init vdso_init(void)
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);
280 arch_initcall(vdso_init);