1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4 #include <linux/binfmts.h>
5 #include <linux/elf.h>
6 #include <linux/err.h>
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9
10 #include <asm/page.h>
11
12 extern char vdso_start[], vdso_end[];
13
14 static unsigned int vdso_pages;
15 static struct page **vdso_pagelist;
16
vdso_init(void)17 static int __init vdso_init(void)
18 {
19 unsigned int i;
20
21 vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
22 vdso_pagelist =
23 kcalloc(vdso_pages, sizeof(struct page *), GFP_KERNEL);
24 if (unlikely(vdso_pagelist == NULL)) {
25 pr_err("vdso: pagelist allocation failed\n");
26 return -ENOMEM;
27 }
28
29 for (i = 0; i < vdso_pages; i++) {
30 struct page *pg;
31
32 pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
33 vdso_pagelist[i] = pg;
34 }
35
36 return 0;
37 }
38 arch_initcall(vdso_init);
39
arch_setup_additional_pages(struct linux_binprm * bprm,int uses_interp)40 int arch_setup_additional_pages(struct linux_binprm *bprm,
41 int uses_interp)
42 {
43 struct vm_area_struct *vma;
44 struct mm_struct *mm = current->mm;
45 unsigned long vdso_base, vdso_len;
46 int ret;
47 static struct vm_special_mapping vdso_mapping = {
48 .name = "[vdso]",
49 };
50
51 vdso_len = vdso_pages << PAGE_SHIFT;
52
53 mmap_write_lock(mm);
54 vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
55 if (IS_ERR_VALUE(vdso_base)) {
56 ret = vdso_base;
57 goto end;
58 }
59
60 /*
61 * Put vDSO base into mm struct. We need to do this before calling
62 * install_special_mapping or the perf counter mmap tracking code
63 * will fail to recognise it as a vDSO (since arch_vma_name fails).
64 */
65 mm->context.vdso = (void *)vdso_base;
66
67 vdso_mapping.pages = vdso_pagelist;
68 vma =
69 _install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
70 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
71 &vdso_mapping);
72
73 if (IS_ERR(vma)) {
74 ret = PTR_ERR(vma);
75 mm->context.vdso = NULL;
76 goto end;
77 }
78
79 vdso_base += (vdso_pages << PAGE_SHIFT);
80 ret = 0;
81 end:
82 mmap_write_unlock(mm);
83 return ret;
84 }
85