1dd3ef10eSGuo Ren // SPDX-License-Identifier: GPL-2.0
2dd3ef10eSGuo Ren // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3dd3ef10eSGuo Ren
4dd3ef10eSGuo Ren #include <linux/binfmts.h>
5dd3ef10eSGuo Ren #include <linux/elf.h>
687f3248cSGuo Ren #include <linux/err.h>
787f3248cSGuo Ren #include <linux/mm.h>
887f3248cSGuo Ren #include <linux/slab.h>
9dd3ef10eSGuo Ren
1087f3248cSGuo Ren #include <asm/page.h>
11*0d3b051aSGuo Ren #ifdef GENERIC_TIME_VSYSCALL
12*0d3b051aSGuo Ren #include <vdso/datapage.h>
13*0d3b051aSGuo Ren #else
14dd3ef10eSGuo Ren #include <asm/vdso.h>
15*0d3b051aSGuo Ren #endif
16dd3ef10eSGuo Ren
1787f3248cSGuo Ren extern char vdso_start[], vdso_end[];
18dd3ef10eSGuo Ren
1987f3248cSGuo Ren static unsigned int vdso_pages;
2087f3248cSGuo Ren static struct page **vdso_pagelist;
2187f3248cSGuo Ren
2287f3248cSGuo Ren /*
2387f3248cSGuo Ren * The vDSO data page.
2487f3248cSGuo Ren */
2587f3248cSGuo Ren static union {
2687f3248cSGuo Ren struct vdso_data data;
2787f3248cSGuo Ren u8 page[PAGE_SIZE];
2887f3248cSGuo Ren } vdso_data_store __page_aligned_data;
2987f3248cSGuo Ren struct vdso_data *vdso_data = &vdso_data_store.data;
3087f3248cSGuo Ren
vdso_init(void)3187f3248cSGuo Ren static int __init vdso_init(void)
32dd3ef10eSGuo Ren {
3387f3248cSGuo Ren unsigned int i;
34dd3ef10eSGuo Ren
3587f3248cSGuo Ren vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
3687f3248cSGuo Ren vdso_pagelist =
3787f3248cSGuo Ren kcalloc(vdso_pages + 1, sizeof(struct page *), GFP_KERNEL);
3887f3248cSGuo Ren if (unlikely(vdso_pagelist == NULL)) {
3987f3248cSGuo Ren pr_err("vdso: pagelist allocation failed\n");
4087f3248cSGuo Ren return -ENOMEM;
4187f3248cSGuo Ren }
42dd3ef10eSGuo Ren
4387f3248cSGuo Ren for (i = 0; i < vdso_pages; i++) {
4487f3248cSGuo Ren struct page *pg;
45dd3ef10eSGuo Ren
4687f3248cSGuo Ren pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
4787f3248cSGuo Ren vdso_pagelist[i] = pg;
4887f3248cSGuo Ren }
4987f3248cSGuo Ren vdso_pagelist[i] = virt_to_page(vdso_data);
50dd3ef10eSGuo Ren
51dd3ef10eSGuo Ren return 0;
52dd3ef10eSGuo Ren }
5387f3248cSGuo Ren arch_initcall(vdso_init);
54dd3ef10eSGuo Ren
arch_setup_additional_pages(struct linux_binprm * bprm,int uses_interp)5587f3248cSGuo Ren int arch_setup_additional_pages(struct linux_binprm *bprm,
5687f3248cSGuo Ren int uses_interp)
57dd3ef10eSGuo Ren {
58dd3ef10eSGuo Ren struct mm_struct *mm = current->mm;
5987f3248cSGuo Ren unsigned long vdso_base, vdso_len;
6087f3248cSGuo Ren int ret;
6187f3248cSGuo Ren
6287f3248cSGuo Ren vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
63dd3ef10eSGuo Ren
64d8ed45c5SMichel Lespinasse mmap_write_lock(mm);
6587f3248cSGuo Ren vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
6687f3248cSGuo Ren if (IS_ERR_VALUE(vdso_base)) {
6787f3248cSGuo Ren ret = vdso_base;
6887f3248cSGuo Ren goto end;
69dd3ef10eSGuo Ren }
70dd3ef10eSGuo Ren
7187f3248cSGuo Ren /*
7287f3248cSGuo Ren * Put vDSO base into mm struct. We need to do this before calling
7387f3248cSGuo Ren * install_special_mapping or the perf counter mmap tracking code
7487f3248cSGuo Ren * will fail to recognise it as a vDSO (since arch_vma_name fails).
7587f3248cSGuo Ren */
7687f3248cSGuo Ren mm->context.vdso = (void *)vdso_base;
77dd3ef10eSGuo Ren
7887f3248cSGuo Ren ret =
7987f3248cSGuo Ren install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
8087f3248cSGuo Ren (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
8187f3248cSGuo Ren vdso_pagelist);
82dd3ef10eSGuo Ren
8387f3248cSGuo Ren if (unlikely(ret)) {
8487f3248cSGuo Ren mm->context.vdso = NULL;
8587f3248cSGuo Ren goto end;
8687f3248cSGuo Ren }
8787f3248cSGuo Ren
8887f3248cSGuo Ren vdso_base += (vdso_pages << PAGE_SHIFT);
8987f3248cSGuo Ren ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
9087f3248cSGuo Ren (VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
9187f3248cSGuo Ren
9287f3248cSGuo Ren if (unlikely(ret))
9387f3248cSGuo Ren mm->context.vdso = NULL;
9487f3248cSGuo Ren end:
95d8ed45c5SMichel Lespinasse mmap_write_unlock(mm);
96dd3ef10eSGuo Ren return ret;
97dd3ef10eSGuo Ren }
98dd3ef10eSGuo Ren
arch_vma_name(struct vm_area_struct * vma)99dd3ef10eSGuo Ren const char *arch_vma_name(struct vm_area_struct *vma)
100dd3ef10eSGuo Ren {
10187f3248cSGuo Ren if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso))
102dd3ef10eSGuo Ren return "[vdso]";
10387f3248cSGuo Ren if (vma->vm_mm && (vma->vm_start ==
10487f3248cSGuo Ren (long)vma->vm_mm->context.vdso + PAGE_SIZE))
10587f3248cSGuo Ren return "[vdso_data]";
106dd3ef10eSGuo Ren return NULL;
107dd3ef10eSGuo Ren }
108