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 <vdso/datapage.h>
21 extern char vdso64_start[], vdso64_end[];
22 static unsigned int vdso_pages;
23 static struct page **vdso_pagelist;
26 struct vdso_data data[CS_BASES];
28 } vdso_data_store __page_aligned_data;
30 struct vdso_data *vdso_data = vdso_data_store.data;
32 unsigned int __read_mostly vdso_enabled = 1;
34 static int __init vdso_setup(char *str)
38 if (!kstrtobool(str, &enabled))
39 vdso_enabled = enabled;
42 __setup("vdso=", vdso_setup);
44 static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
45 struct vm_area_struct *vma, struct vm_fault *vmf)
47 if (vmf->pgoff >= vdso_pages)
48 return VM_FAULT_SIGBUS;
50 vmf->page = vdso_pagelist[vmf->pgoff];
55 static int vdso_mremap(const struct vm_special_mapping *sm,
56 struct vm_area_struct *vma)
58 current->mm->context.vdso_base = vma->vm_start;
62 static const struct vm_special_mapping vdso_mapping = {
65 .mremap = vdso_mremap,
68 int vdso_getcpu_init(void)
70 set_tod_programmable_field(smp_processor_id());
73 early_initcall(vdso_getcpu_init); /* Must be called before SMP init */
75 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
77 struct mm_struct *mm = current->mm;
78 struct vm_area_struct *vma;
79 unsigned long vdso_base;
82 if (!vdso_enabled || is_compat_task())
84 if (mmap_write_lock_killable(mm))
86 vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
88 if (IS_ERR_VALUE(vdso_base))
91 * our vma flags don't have VM_WRITE so by default, the process
92 * isn't allowed to write those pages.
93 * gdb can break that with ptrace interface, and thus trigger COW
94 * on those pages but it's then your responsibility to never do that
95 * on the "data" page of the vDSO or you'll stop getting kernel
96 * updates and your nice userland gettimeofday will be totally dead.
97 * It's fine to use that for setting breakpoints in the vDSO code
100 vma = _install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
102 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
107 current->mm->context.vdso_base = vdso_base;
110 mmap_write_unlock(mm);
114 static int __init vdso_init(void)
118 vdso_pages = ((vdso64_end - vdso64_start) >> PAGE_SHIFT) + 1;
119 /* Make sure pages are in the correct state */
120 vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *),
122 if (!vdso_pagelist) {
126 for (i = 0; i < vdso_pages - 1; i++) {
127 struct page *pg = virt_to_page(vdso64_start + i * PAGE_SIZE);
129 vdso_pagelist[i] = pg;
131 vdso_pagelist[vdso_pages - 1] = virt_to_page(vdso_data);
132 vdso_pagelist[vdso_pages] = NULL;
133 get_page(virt_to_page(vdso_data));
136 arch_initcall(vdso_init);