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