|
1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2 | 2 | // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd. |
3 | 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 | 4 | #include <linux/binfmts.h> |
10 | 5 | #include <linux/elf.h> |
11 | | -#include <linux/vmalloc.h> |
12 | | -#include <linux/unistd.h> |
13 | | -#include <linux/uaccess.h> |
| 6 | +#include <linux/err.h> |
| 7 | +#include <linux/mm.h> |
| 8 | +#include <linux/slab.h> |
14 | 9 |
|
| 10 | +#include <asm/page.h> |
15 | 11 | #include <asm/vdso.h> |
16 | | -#include <asm/cacheflush.h> |
17 | 12 |
|
18 | | -static struct page *vdso_page; |
| 13 | +extern char vdso_start[], vdso_end[]; |
19 | 14 |
|
20 | | -static 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"); |
| 15 | +static unsigned int vdso_pages; |
| 16 | +static struct page **vdso_pagelist; |
28 | 17 |
|
29 | | - vdso = vmap(&vdso_page, 1, 0, PAGE_KERNEL); |
30 | | - if (!vdso) |
31 | | - panic("Cannot map vdso"); |
| 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; |
32 | 26 |
|
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); |
| 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 | 38 |
|
39 | | - dcache_wb_range((unsigned long)vdso, (unsigned long)vdso + 16); |
| 39 | + for (i = 0; i < vdso_pages; i++) { |
| 40 | + struct page *pg; |
40 | 41 |
|
41 | | - vunmap(vdso); |
| 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); |
42 | 46 |
|
43 | 47 | return 0; |
44 | 48 | } |
45 | | -subsys_initcall(init_vdso); |
| 49 | +arch_initcall(vdso_init); |
46 | 50 |
|
47 | | -int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) |
| 51 | +int arch_setup_additional_pages(struct linux_binprm *bprm, |
| 52 | + int uses_interp) |
48 | 53 | { |
49 | | - int ret; |
50 | | - unsigned long addr; |
51 | 54 | struct mm_struct *mm = current->mm; |
| 55 | + unsigned long vdso_base, vdso_len; |
| 56 | + int ret; |
52 | 57 |
|
53 | | - mmap_write_lock(mm); |
| 58 | + vdso_len = (vdso_pages + 1) << PAGE_SHIFT; |
54 | 59 |
|
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; |
| 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; |
59 | 65 | } |
60 | 66 |
|
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; |
| 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 | + } |
69 | 83 |
|
70 | | - mm->context.vdso = (void *)addr; |
| 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]); |
71 | 87 |
|
72 | | -up_fail: |
| 88 | + if (unlikely(ret)) |
| 89 | + mm->context.vdso = NULL; |
| 90 | +end: |
73 | 91 | mmap_write_unlock(mm); |
74 | 92 | return ret; |
75 | 93 | } |
76 | 94 |
|
77 | 95 | const char *arch_vma_name(struct vm_area_struct *vma) |
78 | 96 | { |
79 | | - if (vma->vm_mm == NULL) |
80 | | - return NULL; |
81 | | - |
82 | | - if (vma->vm_start == (long)vma->vm_mm->context.vdso) |
| 97 | + if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso)) |
83 | 98 | return "[vdso]"; |
84 | | - else |
85 | | - return NULL; |
| 99 | + if (vma->vm_mm && (vma->vm_start == |
| 100 | + (long)vma->vm_mm->context.vdso + PAGE_SIZE)) |
| 101 | + return "[vdso_data]"; |
| 102 | + return NULL; |
86 | 103 | } |
0 commit comments