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>
18 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
19 #include <vdso/datapage.h>
25 extern char vdso_start[], vdso_end[];
27 extern char compat_vdso_start[], compat_vdso_end[];
31 VVAR_DATA_PAGE_OFFSET,
32 VVAR_TIMENS_PAGE_OFFSET,
41 #define VVAR_SIZE (VVAR_NR_PAGES << PAGE_SHIFT)
47 struct vdso_data data;
49 } vdso_data_store __page_aligned_data;
50 struct vdso_data *vdso_data = &vdso_data_store.data;
54 const char *vdso_code_start;
55 const char *vdso_code_end;
56 unsigned long vdso_pages;
58 struct vm_special_mapping *dm;
60 struct vm_special_mapping *cm;
63 static struct __vdso_info vdso_info;
65 static struct __vdso_info compat_vdso_info;
68 static int vdso_mremap(const struct vm_special_mapping *sm,
69 struct vm_area_struct *new_vma)
71 current->mm->context.vdso = (void *)new_vma->vm_start;
76 static void __init __vdso_init(struct __vdso_info *vdso_info)
79 struct page **vdso_pagelist;
82 if (memcmp(vdso_info->vdso_code_start, "\177ELF", 4))
83 panic("vDSO is not a valid ELF object!\n");
85 vdso_info->vdso_pages = (
86 vdso_info->vdso_code_end -
87 vdso_info->vdso_code_start) >>
90 vdso_pagelist = kcalloc(vdso_info->vdso_pages,
91 sizeof(struct page *),
93 if (vdso_pagelist == NULL)
94 panic("vDSO kcalloc failed!\n");
96 /* Grab the vDSO code pages. */
97 pfn = sym_to_pfn(vdso_info->vdso_code_start);
99 for (i = 0; i < vdso_info->vdso_pages; i++)
100 vdso_pagelist[i] = pfn_to_page(pfn + i);
102 vdso_info->cm->pages = vdso_pagelist;
105 #ifdef CONFIG_TIME_NS
106 struct vdso_data *arch_get_vdso_data(void *vvar_page)
108 return (struct vdso_data *)(vvar_page);
112 * The vvar mapping contains data for a specific time namespace, so when a task
113 * changes namespace we must unmap its vvar data for the old namespace.
114 * Subsequent faults will map in data for the new namespace.
116 * For more details see timens_setup_vdso_data().
118 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
120 struct mm_struct *mm = task->mm;
121 struct vm_area_struct *vma;
122 VMA_ITERATOR(vmi, mm, 0);
126 for_each_vma(vmi, vma) {
127 unsigned long size = vma->vm_end - vma->vm_start;
129 if (vma_is_special_mapping(vma, vdso_info.dm))
130 zap_page_range(vma, vma->vm_start, size);
132 if (vma_is_special_mapping(vma, compat_vdso_info.dm))
133 zap_page_range(vma, vma->vm_start, size);
137 mmap_read_unlock(mm);
142 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
143 struct vm_area_struct *vma, struct vm_fault *vmf)
145 struct page *timens_page = find_timens_vvar_page(vma);
148 switch (vmf->pgoff) {
149 case VVAR_DATA_PAGE_OFFSET:
151 pfn = page_to_pfn(timens_page);
153 pfn = sym_to_pfn(vdso_data);
155 #ifdef CONFIG_TIME_NS
156 case VVAR_TIMENS_PAGE_OFFSET:
158 * If a task belongs to a time namespace then a namespace
159 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
160 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
162 * See also the comment near timens_setup_vdso_data().
165 return VM_FAULT_SIGBUS;
166 pfn = sym_to_pfn(vdso_data);
168 #endif /* CONFIG_TIME_NS */
170 return VM_FAULT_SIGBUS;
173 return vmf_insert_pfn(vma, vmf->address, pfn);
176 static struct vm_special_mapping rv_vdso_maps[] __ro_after_init = {
177 [RV_VDSO_MAP_VVAR] = {
181 [RV_VDSO_MAP_VDSO] = {
183 .mremap = vdso_mremap,
187 static struct __vdso_info vdso_info __ro_after_init = {
189 .vdso_code_start = vdso_start,
190 .vdso_code_end = vdso_end,
191 .dm = &rv_vdso_maps[RV_VDSO_MAP_VVAR],
192 .cm = &rv_vdso_maps[RV_VDSO_MAP_VDSO],
196 static struct vm_special_mapping rv_compat_vdso_maps[] __ro_after_init = {
197 [RV_VDSO_MAP_VVAR] = {
201 [RV_VDSO_MAP_VDSO] = {
203 .mremap = vdso_mremap,
207 static struct __vdso_info compat_vdso_info __ro_after_init = {
208 .name = "compat_vdso",
209 .vdso_code_start = compat_vdso_start,
210 .vdso_code_end = compat_vdso_end,
211 .dm = &rv_compat_vdso_maps[RV_VDSO_MAP_VVAR],
212 .cm = &rv_compat_vdso_maps[RV_VDSO_MAP_VDSO],
216 static int __init vdso_init(void)
218 __vdso_init(&vdso_info);
220 __vdso_init(&compat_vdso_info);
225 arch_initcall(vdso_init);
227 static int __setup_additional_pages(struct mm_struct *mm,
228 struct linux_binprm *bprm,
230 struct __vdso_info *vdso_info)
232 unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
235 BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
237 vdso_text_len = vdso_info->vdso_pages << PAGE_SHIFT;
238 /* Be sure to map the data page */
239 vdso_mapping_len = vdso_text_len + VVAR_SIZE;
241 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
242 if (IS_ERR_VALUE(vdso_base)) {
243 ret = ERR_PTR(vdso_base);
247 ret = _install_special_mapping(mm, vdso_base, VVAR_SIZE,
248 (VM_READ | VM_MAYREAD | VM_PFNMAP), vdso_info->dm);
252 vdso_base += VVAR_SIZE;
253 mm->context.vdso = (void *)vdso_base;
256 _install_special_mapping(mm, vdso_base, vdso_text_len,
257 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
266 mm->context.vdso = NULL;
271 int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
274 struct mm_struct *mm = current->mm;
277 if (mmap_write_lock_killable(mm))
280 ret = __setup_additional_pages(mm, bprm, uses_interp,
282 mmap_write_unlock(mm);
288 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
290 struct mm_struct *mm = current->mm;
293 if (mmap_write_lock_killable(mm))
296 ret = __setup_additional_pages(mm, bprm, uses_interp, &vdso_info);
297 mmap_write_unlock(mm);