csky: Reconstruct VDSO framework
[linux-2.6-microblaze.git] / arch / csky / kernel / vdso.c
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 #include <asm/vdso.h>
12
13 extern char vdso_start[], vdso_end[];
14
15 static unsigned int vdso_pages;
16 static struct page **vdso_pagelist;
17
18 /*
19  * The vDSO data page.
20  */
21 static union {
22         struct vdso_data        data;
23         u8                      page[PAGE_SIZE];
24 } vdso_data_store __page_aligned_data;
25 struct vdso_data *vdso_data = &vdso_data_store.data;
26
27 static int __init vdso_init(void)
28 {
29         unsigned int i;
30
31         vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
32         vdso_pagelist =
33                 kcalloc(vdso_pages + 1, sizeof(struct page *), GFP_KERNEL);
34         if (unlikely(vdso_pagelist == NULL)) {
35                 pr_err("vdso: pagelist allocation failed\n");
36                 return -ENOMEM;
37         }
38
39         for (i = 0; i < vdso_pages; i++) {
40                 struct page *pg;
41
42                 pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
43                 vdso_pagelist[i] = pg;
44         }
45         vdso_pagelist[i] = virt_to_page(vdso_data);
46
47         return 0;
48 }
49 arch_initcall(vdso_init);
50
51 int arch_setup_additional_pages(struct linux_binprm *bprm,
52         int uses_interp)
53 {
54         struct mm_struct *mm = current->mm;
55         unsigned long vdso_base, vdso_len;
56         int ret;
57
58         vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
59
60         mmap_write_lock(mm);
61         vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
62         if (IS_ERR_VALUE(vdso_base)) {
63                 ret = vdso_base;
64                 goto end;
65         }
66
67         /*
68          * Put vDSO base into mm struct. We need to do this before calling
69          * install_special_mapping or the perf counter mmap tracking code
70          * will fail to recognise it as a vDSO (since arch_vma_name fails).
71          */
72         mm->context.vdso = (void *)vdso_base;
73
74         ret =
75            install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
76                 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
77                 vdso_pagelist);
78
79         if (unlikely(ret)) {
80                 mm->context.vdso = NULL;
81                 goto end;
82         }
83
84         vdso_base += (vdso_pages << PAGE_SHIFT);
85         ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
86                 (VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
87
88         if (unlikely(ret))
89                 mm->context.vdso = NULL;
90 end:
91         mmap_write_unlock(mm);
92         return ret;
93 }
94
95 const char *arch_vma_name(struct vm_area_struct *vma)
96 {
97         if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso))
98                 return "[vdso]";
99         if (vma->vm_mm && (vma->vm_start ==
100                            (long)vma->vm_mm->context.vdso + PAGE_SIZE))
101                 return "[vdso_data]";
102         return NULL;
103 }