15933f6d2SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
219f9a34fSPaul Mundt /*
358862699SUwe Kleine-König * arch/sh/kernel/vsyscall/vsyscall.c
419f9a34fSPaul Mundt *
519f9a34fSPaul Mundt * Copyright (C) 2006 Paul Mundt
619f9a34fSPaul Mundt *
719f9a34fSPaul Mundt * vDSO randomization
819f9a34fSPaul Mundt * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
919f9a34fSPaul Mundt */
1019f9a34fSPaul Mundt #include <linux/mm.h>
1119f9a34fSPaul Mundt #include <linux/kernel.h>
1219f9a34fSPaul Mundt #include <linux/init.h>
1319f9a34fSPaul Mundt #include <linux/gfp.h>
1419f9a34fSPaul Mundt #include <linux/module.h>
1519f9a34fSPaul Mundt #include <linux/elf.h>
16f75522ceSManuel Lauss #include <linux/sched.h>
17e06c4e57SPaul Mundt #include <linux/err.h>
1819f9a34fSPaul Mundt
1919f9a34fSPaul Mundt /*
2019f9a34fSPaul Mundt * Should the kernel map a VDSO page into processes and pass its
2119f9a34fSPaul Mundt * address down to glibc upon exec()?
2219f9a34fSPaul Mundt */
2319f9a34fSPaul Mundt unsigned int __read_mostly vdso_enabled = 1;
2419f9a34fSPaul Mundt EXPORT_SYMBOL_GPL(vdso_enabled);
2519f9a34fSPaul Mundt
vdso_setup(char * s)2619f9a34fSPaul Mundt static int __init vdso_setup(char *s)
2719f9a34fSPaul Mundt {
2819f9a34fSPaul Mundt vdso_enabled = simple_strtoul(s, NULL, 0);
2919f9a34fSPaul Mundt return 1;
3019f9a34fSPaul Mundt }
3119f9a34fSPaul Mundt __setup("vdso=", vdso_setup);
3219f9a34fSPaul Mundt
3319f9a34fSPaul Mundt /*
3419f9a34fSPaul Mundt * These symbols are defined by vsyscall.o to mark the bounds
3519f9a34fSPaul Mundt * of the ELF DSO images included therein.
3619f9a34fSPaul Mundt */
3719f9a34fSPaul Mundt extern const char vsyscall_trapa_start, vsyscall_trapa_end;
382affc857SPaul Mundt static struct page *syscall_pages[1];
3919f9a34fSPaul Mundt
vsyscall_init(void)4019f9a34fSPaul Mundt int __init vsyscall_init(void)
4119f9a34fSPaul Mundt {
422affc857SPaul Mundt void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
432affc857SPaul Mundt syscall_pages[0] = virt_to_page(syscall_page);
4419f9a34fSPaul Mundt
4519f9a34fSPaul Mundt /*
4619f9a34fSPaul Mundt * XXX: Map this page to a fixmap entry if we get around
4719f9a34fSPaul Mundt * to adding the page to ELF core dumps
4819f9a34fSPaul Mundt */
4919f9a34fSPaul Mundt
5019f9a34fSPaul Mundt memcpy(syscall_page,
5119f9a34fSPaul Mundt &vsyscall_trapa_start,
5219f9a34fSPaul Mundt &vsyscall_trapa_end - &vsyscall_trapa_start);
5319f9a34fSPaul Mundt
5419f9a34fSPaul Mundt return 0;
5519f9a34fSPaul Mundt }
5619f9a34fSPaul Mundt
5719f9a34fSPaul Mundt /* Setup a VMA at program startup for the vsyscall page */
arch_setup_additional_pages(struct linux_binprm * bprm,int uses_interp)58fc5243d9SMartin Schwidefsky int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
5919f9a34fSPaul Mundt {
6019f9a34fSPaul Mundt struct mm_struct *mm = current->mm;
6119f9a34fSPaul Mundt unsigned long addr;
6219f9a34fSPaul Mundt int ret;
6319f9a34fSPaul Mundt
64*d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm))
6569048176SMichal Hocko return -EINTR;
6669048176SMichal Hocko
6719f9a34fSPaul Mundt addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
6819f9a34fSPaul Mundt if (IS_ERR_VALUE(addr)) {
6919f9a34fSPaul Mundt ret = addr;
7019f9a34fSPaul Mundt goto up_fail;
7119f9a34fSPaul Mundt }
7219f9a34fSPaul Mundt
732affc857SPaul Mundt ret = install_special_mapping(mm, addr, PAGE_SIZE,
742affc857SPaul Mundt VM_READ | VM_EXEC |
75909af768SJason Baron VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
762affc857SPaul Mundt syscall_pages);
772affc857SPaul Mundt if (unlikely(ret))
7819f9a34fSPaul Mundt goto up_fail;
7919f9a34fSPaul Mundt
8019f9a34fSPaul Mundt current->mm->context.vdso = (void *)addr;
8119f9a34fSPaul Mundt
8219f9a34fSPaul Mundt up_fail:
83*d8ed45c5SMichel Lespinasse mmap_write_unlock(mm);
8419f9a34fSPaul Mundt return ret;
8519f9a34fSPaul Mundt }
8619f9a34fSPaul Mundt
arch_vma_name(struct vm_area_struct * vma)8719f9a34fSPaul Mundt const char *arch_vma_name(struct vm_area_struct *vma)
8819f9a34fSPaul Mundt {
8919f9a34fSPaul Mundt if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
9019f9a34fSPaul Mundt return "[vdso]";
9119f9a34fSPaul Mundt
9219f9a34fSPaul Mundt return NULL;
9319f9a34fSPaul Mundt }
94