Loading...
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#ifdef GENERIC_TIME_VSYSCALL
12#include <vdso/datapage.h>
13#else
14#include <asm/vdso.h>
15#endif
16
17extern char vdso_start[], vdso_end[];
18
19static unsigned int vdso_pages;
20static struct page **vdso_pagelist;
21
22/*
23 * The vDSO data page.
24 */
25static union {
26 struct vdso_data data;
27 u8 page[PAGE_SIZE];
28} vdso_data_store __page_aligned_data;
29struct vdso_data *vdso_data = &vdso_data_store.data;
30
31static int __init vdso_init(void)
32{
33 unsigned int i;
34
35 vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
36 vdso_pagelist =
37 kcalloc(vdso_pages + 1, sizeof(struct page *), GFP_KERNEL);
38 if (unlikely(vdso_pagelist == NULL)) {
39 pr_err("vdso: pagelist allocation failed\n");
40 return -ENOMEM;
41 }
42
43 for (i = 0; i < vdso_pages; i++) {
44 struct page *pg;
45
46 pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
47 vdso_pagelist[i] = pg;
48 }
49 vdso_pagelist[i] = virt_to_page(vdso_data);
50
51 return 0;
52}
53arch_initcall(vdso_init);
54
55int arch_setup_additional_pages(struct linux_binprm *bprm,
56 int uses_interp)
57{
58 struct mm_struct *mm = current->mm;
59 unsigned long vdso_base, vdso_len;
60 int ret;
61
62 vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
63
64 mmap_write_lock(mm);
65 vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
66 if (IS_ERR_VALUE(vdso_base)) {
67 ret = vdso_base;
68 goto end;
69 }
70
71 /*
72 * Put vDSO base into mm struct. We need to do this before calling
73 * install_special_mapping or the perf counter mmap tracking code
74 * will fail to recognise it as a vDSO (since arch_vma_name fails).
75 */
76 mm->context.vdso = (void *)vdso_base;
77
78 ret =
79 install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
80 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
81 vdso_pagelist);
82
83 if (unlikely(ret)) {
84 mm->context.vdso = NULL;
85 goto end;
86 }
87
88 vdso_base += (vdso_pages << PAGE_SHIFT);
89 ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
90 (VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
91
92 if (unlikely(ret))
93 mm->context.vdso = NULL;
94end:
95 mmap_write_unlock(mm);
96 return ret;
97}
98
99const char *arch_vma_name(struct vm_area_struct *vma)
100{
101 if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso))
102 return "[vdso]";
103 if (vma->vm_mm && (vma->vm_start ==
104 (long)vma->vm_mm->context.vdso + PAGE_SIZE))
105 return "[vdso_data]";
106 return NULL;
107}
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4#include <linux/kernel.h>
5#include <linux/err.h>
6#include <linux/sched.h>
7#include <linux/mm.h>
8#include <linux/init.h>
9#include <linux/binfmts.h>
10#include <linux/elf.h>
11#include <linux/vmalloc.h>
12#include <linux/unistd.h>
13#include <linux/uaccess.h>
14
15#include <asm/vdso.h>
16#include <asm/cacheflush.h>
17
18static struct page *vdso_page;
19
20static int __init init_vdso(void)
21{
22 struct csky_vdso *vdso;
23 int err = 0;
24
25 vdso_page = alloc_page(GFP_KERNEL);
26 if (!vdso_page)
27 panic("Cannot allocate vdso");
28
29 vdso = vmap(&vdso_page, 1, 0, PAGE_KERNEL);
30 if (!vdso)
31 panic("Cannot map vdso");
32
33 clear_page(vdso);
34
35 err = setup_vdso_page(vdso->rt_signal_retcode);
36 if (err)
37 panic("Cannot set signal return code, err: %x.", err);
38
39 dcache_wb_range((unsigned long)vdso, (unsigned long)vdso + 16);
40
41 vunmap(vdso);
42
43 return 0;
44}
45subsys_initcall(init_vdso);
46
47int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
48{
49 int ret;
50 unsigned long addr;
51 struct mm_struct *mm = current->mm;
52
53 down_write(&mm->mmap_sem);
54
55 addr = get_unmapped_area(NULL, STACK_TOP, PAGE_SIZE, 0, 0);
56 if (IS_ERR_VALUE(addr)) {
57 ret = addr;
58 goto up_fail;
59 }
60
61 ret = install_special_mapping(
62 mm,
63 addr,
64 PAGE_SIZE,
65 VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
66 &vdso_page);
67 if (ret)
68 goto up_fail;
69
70 mm->context.vdso = (void *)addr;
71
72up_fail:
73 up_write(&mm->mmap_sem);
74 return ret;
75}
76
77const char *arch_vma_name(struct vm_area_struct *vma)
78{
79 if (vma->vm_mm == NULL)
80 return NULL;
81
82 if (vma->vm_start == (long)vma->vm_mm->context.vdso)
83 return "[vdso]";
84 else
85 return NULL;
86}