1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
4 * <benh@kernel.crashing.org>
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>
17 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
18 #include <vdso/datapage.h>
24 extern char vdso_start[], vdso_end[];
27 VVAR_DATA_PAGE_OFFSET,
31 #define VVAR_SIZE (VVAR_NR_PAGES << PAGE_SHIFT)
33 static unsigned int vdso_pages __ro_after_init;
34 static struct page **vdso_pagelist __ro_after_init;
40 struct vdso_data data;
42 } vdso_data_store __page_aligned_data;
43 struct vdso_data *vdso_data = &vdso_data_store.data;
45 static int __init vdso_init(void)
49 vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
51 kcalloc(vdso_pages + VVAR_NR_PAGES, sizeof(struct page *), GFP_KERNEL);
52 if (unlikely(vdso_pagelist == NULL)) {
53 pr_err("vdso: pagelist allocation failed\n");
57 for (i = 0; i < vdso_pages; i++) {
60 pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
61 vdso_pagelist[i] = pg;
63 vdso_pagelist[i] = virt_to_page(vdso_data);
67 arch_initcall(vdso_init);
69 int arch_setup_additional_pages(struct linux_binprm *bprm,
72 struct mm_struct *mm = current->mm;
73 unsigned long vdso_base, vdso_len;
76 BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
78 vdso_len = (vdso_pages + VVAR_NR_PAGES) << PAGE_SHIFT;
80 if (mmap_write_lock_killable(mm))
83 vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
84 if (IS_ERR_VALUE(vdso_base)) {
89 mm->context.vdso = NULL;
90 ret = install_special_mapping(mm, vdso_base, VVAR_SIZE,
91 (VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
96 install_special_mapping(mm, vdso_base + VVAR_SIZE,
97 vdso_pages << PAGE_SHIFT,
98 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
105 * Put vDSO base into mm struct. We need to do this before calling
106 * install_special_mapping or the perf counter mmap tracking code
107 * will fail to recognise it as a vDSO (since arch_vma_name fails).
109 mm->context.vdso = (void *)vdso_base + VVAR_SIZE;
112 mmap_write_unlock(mm);
116 const char *arch_vma_name(struct vm_area_struct *vma)
118 if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso))
120 if (vma->vm_mm && (vma->vm_start ==
121 (long)vma->vm_mm->context.vdso - VVAR_SIZE))
122 return "[vdso_data]";