1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright IBM Corp. 2008
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
14 #include <linux/smp.h>
15 #include <linux/stddef.h>
16 #include <linux/unistd.h>
17 #include <linux/slab.h>
18 #include <linux/user.h>
19 #include <linux/elf.h>
20 #include <linux/security.h>
21 #include <linux/memblock.h>
22 #include <linux/compat.h>
23 #include <linux/binfmts.h>
24 #include <vdso/datapage.h>
25 #include <asm/asm-offsets.h>
26 #include <asm/processor.h>
28 #include <asm/mmu_context.h>
29 #include <asm/sections.h>
31 #include <asm/facility.h>
32 #include <asm/timex.h>
34 extern char vdso64_start, vdso64_end;
35 static void *vdso64_kbase = &vdso64_start;
36 static unsigned int vdso64_pages;
37 static struct page **vdso64_pagelist;
40 * Should the kernel map a VDSO page into processes and pass its
41 * address down to glibc upon exec()?
43 unsigned int __read_mostly vdso_enabled = 1;
45 static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
46 struct vm_area_struct *vma, struct vm_fault *vmf)
48 struct page **vdso_pagelist;
49 unsigned long vdso_pages;
51 vdso_pagelist = vdso64_pagelist;
52 vdso_pages = vdso64_pages;
54 if (vmf->pgoff >= vdso_pages)
55 return VM_FAULT_SIGBUS;
57 vmf->page = vdso_pagelist[vmf->pgoff];
62 static int vdso_mremap(const struct vm_special_mapping *sm,
63 struct vm_area_struct *vma)
65 current->mm->context.vdso_base = vma->vm_start;
70 static const struct vm_special_mapping vdso_mapping = {
73 .mremap = vdso_mremap,
76 static int __init vdso_setup(char *str)
80 if (!kstrtobool(str, &enabled))
81 vdso_enabled = enabled;
84 __setup("vdso=", vdso_setup);
90 struct vdso_data data[CS_BASES];
92 } vdso_data_store __page_aligned_data;
93 struct vdso_data *vdso_data = vdso_data_store.data;
95 int vdso_getcpu_init(void)
97 set_tod_programmable_field(smp_processor_id());
100 early_initcall(vdso_getcpu_init); /* Must be called before SMP init */
103 * This is called from binfmt_elf, we create the special vma for the
104 * vDSO and insert it into the mm struct tree
106 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
108 struct mm_struct *mm = current->mm;
109 struct vm_area_struct *vma;
110 unsigned long vdso_pages;
111 unsigned long vdso_base;
117 if (is_compat_task())
120 vdso_pages = vdso64_pages;
122 * vDSO has a problem and was disabled, just don't "enable" it for
129 * pick a base address for the vDSO in process space. We try to put
130 * it at vdso_base which is the "natural" base for it, but we might
131 * fail and end up putting it elsewhere.
133 if (mmap_write_lock_killable(mm))
135 vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
136 if (IS_ERR_VALUE(vdso_base)) {
142 * our vma flags don't have VM_WRITE so by default, the process
143 * isn't allowed to write those pages.
144 * gdb can break that with ptrace interface, and thus trigger COW
145 * on those pages but it's then your responsibility to never do that
146 * on the "data" page of the vDSO or you'll stop getting kernel
147 * updates and your nice userland gettimeofday will be totally dead.
148 * It's fine to use that for setting breakpoints in the vDSO code
151 vma = _install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
153 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
160 current->mm->context.vdso_base = vdso_base;
164 mmap_write_unlock(mm);
168 static int __init vdso_init(void)
172 /* Calculate the size of the 64 bit vDSO */
173 vdso64_pages = ((&vdso64_end - &vdso64_start
174 + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
176 /* Make sure pages are in the correct state */
177 vdso64_pagelist = kcalloc(vdso64_pages + 1, sizeof(struct page *),
179 BUG_ON(vdso64_pagelist == NULL);
180 for (i = 0; i < vdso64_pages - 1; i++) {
181 struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
183 vdso64_pagelist[i] = pg;
185 vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
186 vdso64_pagelist[vdso64_pages] = NULL;
188 get_page(virt_to_page(vdso_data));
192 arch_initcall(vdso_init);