1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
5 * Copyright (C) 2012 ARM Limited
6 * Copyright (C) 2015 Regents of the University of California
11 #include <linux/slab.h>
12 #include <linux/binfmts.h>
13 #include <linux/err.h>
16 #include <linux/time_namespace.h>
17 #include <vdso/datapage.h>
20 VVAR_DATA_PAGE_OFFSET,
21 VVAR_TIMENS_PAGE_OFFSET,
30 #define VVAR_SIZE (VVAR_NR_PAGES << PAGE_SHIFT)
36 struct vdso_data data;
38 } vdso_data_store __page_aligned_data;
39 struct vdso_data *vdso_data = &vdso_data_store.data;
43 const char *vdso_code_start;
44 const char *vdso_code_end;
45 unsigned long vdso_pages;
47 struct vm_special_mapping *dm;
49 struct vm_special_mapping *cm;
52 static struct __vdso_info vdso_info;
54 static struct __vdso_info compat_vdso_info;
57 static int vdso_mremap(const struct vm_special_mapping *sm,
58 struct vm_area_struct *new_vma)
60 current->mm->context.vdso = (void *)new_vma->vm_start;
65 static void __init __vdso_init(struct __vdso_info *vdso_info)
68 struct page **vdso_pagelist;
71 if (memcmp(vdso_info->vdso_code_start, "\177ELF", 4))
72 panic("vDSO is not a valid ELF object!\n");
74 vdso_info->vdso_pages = (
75 vdso_info->vdso_code_end -
76 vdso_info->vdso_code_start) >>
79 vdso_pagelist = kcalloc(vdso_info->vdso_pages,
80 sizeof(struct page *),
82 if (vdso_pagelist == NULL)
83 panic("vDSO kcalloc failed!\n");
85 /* Grab the vDSO code pages. */
86 pfn = sym_to_pfn(vdso_info->vdso_code_start);
88 for (i = 0; i < vdso_info->vdso_pages; i++)
89 vdso_pagelist[i] = pfn_to_page(pfn + i);
91 vdso_info->cm->pages = vdso_pagelist;
95 struct vdso_data *arch_get_vdso_data(void *vvar_page)
97 return (struct vdso_data *)(vvar_page);
101 * The vvar mapping contains data for a specific time namespace, so when a task
102 * changes namespace we must unmap its vvar data for the old namespace.
103 * Subsequent faults will map in data for the new namespace.
105 * For more details see timens_setup_vdso_data().
107 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
109 struct mm_struct *mm = task->mm;
110 struct vm_area_struct *vma;
111 VMA_ITERATOR(vmi, mm, 0);
115 for_each_vma(vmi, vma) {
116 if (vma_is_special_mapping(vma, vdso_info.dm))
119 if (vma_is_special_mapping(vma, compat_vdso_info.dm))
124 mmap_read_unlock(mm);
129 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
130 struct vm_area_struct *vma, struct vm_fault *vmf)
132 struct page *timens_page = find_timens_vvar_page(vma);
135 switch (vmf->pgoff) {
136 case VVAR_DATA_PAGE_OFFSET:
138 pfn = page_to_pfn(timens_page);
140 pfn = sym_to_pfn(vdso_data);
142 #ifdef CONFIG_TIME_NS
143 case VVAR_TIMENS_PAGE_OFFSET:
145 * If a task belongs to a time namespace then a namespace
146 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
147 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
149 * See also the comment near timens_setup_vdso_data().
152 return VM_FAULT_SIGBUS;
153 pfn = sym_to_pfn(vdso_data);
155 #endif /* CONFIG_TIME_NS */
157 return VM_FAULT_SIGBUS;
160 return vmf_insert_pfn(vma, vmf->address, pfn);
163 static struct vm_special_mapping rv_vdso_maps[] __ro_after_init = {
164 [RV_VDSO_MAP_VVAR] = {
168 [RV_VDSO_MAP_VDSO] = {
170 .mremap = vdso_mremap,
174 static struct __vdso_info vdso_info __ro_after_init = {
176 .vdso_code_start = vdso_start,
177 .vdso_code_end = vdso_end,
178 .dm = &rv_vdso_maps[RV_VDSO_MAP_VVAR],
179 .cm = &rv_vdso_maps[RV_VDSO_MAP_VDSO],
183 static struct vm_special_mapping rv_compat_vdso_maps[] __ro_after_init = {
184 [RV_VDSO_MAP_VVAR] = {
188 [RV_VDSO_MAP_VDSO] = {
190 .mremap = vdso_mremap,
194 static struct __vdso_info compat_vdso_info __ro_after_init = {
195 .name = "compat_vdso",
196 .vdso_code_start = compat_vdso_start,
197 .vdso_code_end = compat_vdso_end,
198 .dm = &rv_compat_vdso_maps[RV_VDSO_MAP_VVAR],
199 .cm = &rv_compat_vdso_maps[RV_VDSO_MAP_VDSO],
203 static int __init vdso_init(void)
205 __vdso_init(&vdso_info);
207 __vdso_init(&compat_vdso_info);
212 arch_initcall(vdso_init);
214 static int __setup_additional_pages(struct mm_struct *mm,
215 struct linux_binprm *bprm,
217 struct __vdso_info *vdso_info)
219 unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
222 BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
224 vdso_text_len = vdso_info->vdso_pages << PAGE_SHIFT;
225 /* Be sure to map the data page */
226 vdso_mapping_len = vdso_text_len + VVAR_SIZE;
228 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
229 if (IS_ERR_VALUE(vdso_base)) {
230 ret = ERR_PTR(vdso_base);
234 ret = _install_special_mapping(mm, vdso_base, VVAR_SIZE,
235 (VM_READ | VM_MAYREAD | VM_PFNMAP), vdso_info->dm);
239 vdso_base += VVAR_SIZE;
240 mm->context.vdso = (void *)vdso_base;
243 _install_special_mapping(mm, vdso_base, vdso_text_len,
244 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
253 mm->context.vdso = NULL;
258 int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
261 struct mm_struct *mm = current->mm;
264 if (mmap_write_lock_killable(mm))
267 ret = __setup_additional_pages(mm, bprm, uses_interp,
269 mmap_write_unlock(mm);
275 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
277 struct mm_struct *mm = current->mm;
280 if (mmap_write_lock_killable(mm))
283 ret = __setup_additional_pages(mm, bprm, uses_interp, &vdso_info);
284 mmap_write_unlock(mm);