xref: /linux/arch/loongarch/kernel/vdso.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author: Huacai Chen <chenhuacai@loongson.cn>
4  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
5  */
6 
7 #include <linux/binfmts.h>
8 #include <linux/elf.h>
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/ioport.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/random.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/timekeeper_internal.h>
18 
19 #include <asm/page.h>
20 #include <asm/vdso.h>
21 #include <vdso/helpers.h>
22 #include <vdso/vsyscall.h>
23 #include <generated/vdso-offsets.h>
24 
25 extern char vdso_start[], vdso_end[];
26 
27 /* Kernel-provided data used by the VDSO. */
28 static union loongarch_vdso_data {
29 	u8 page[PAGE_SIZE];
30 	struct vdso_data data[CS_BASES];
31 } loongarch_vdso_data __page_aligned_data;
32 struct vdso_data *vdso_data = loongarch_vdso_data.data;
33 static struct page *vdso_pages[] = { NULL };
34 
35 static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
36 {
37 	current->mm->context.vdso = (void *)(new_vma->vm_start);
38 
39 	return 0;
40 }
41 
42 struct loongarch_vdso_info vdso_info = {
43 	.vdso = vdso_start,
44 	.size = PAGE_SIZE,
45 	.code_mapping = {
46 		.name = "[vdso]",
47 		.pages = vdso_pages,
48 		.mremap = vdso_mremap,
49 	},
50 	.data_mapping = {
51 		.name = "[vvar]",
52 	},
53 	.offset_sigreturn = vdso_offset_sigreturn,
54 };
55 
56 static int __init init_vdso(void)
57 {
58 	unsigned long i, pfn;
59 
60 	BUG_ON(!PAGE_ALIGNED(vdso_info.vdso));
61 	BUG_ON(!PAGE_ALIGNED(vdso_info.size));
62 
63 	pfn = __phys_to_pfn(__pa_symbol(vdso_info.vdso));
64 	for (i = 0; i < vdso_info.size / PAGE_SIZE; i++)
65 		vdso_info.code_mapping.pages[i] = pfn_to_page(pfn + i);
66 
67 	return 0;
68 }
69 subsys_initcall(init_vdso);
70 
71 static unsigned long vdso_base(void)
72 {
73 	unsigned long base = STACK_TOP;
74 
75 	if (current->flags & PF_RANDOMIZE) {
76 		base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1);
77 		base = PAGE_ALIGN(base);
78 	}
79 
80 	return base;
81 }
82 
83 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
84 {
85 	int ret;
86 	unsigned long vvar_size, size, data_addr, vdso_addr;
87 	struct mm_struct *mm = current->mm;
88 	struct vm_area_struct *vma;
89 	struct loongarch_vdso_info *info = current->thread.vdso;
90 
91 	if (mmap_write_lock_killable(mm))
92 		return -EINTR;
93 
94 	/*
95 	 * Determine total area size. This includes the VDSO data itself
96 	 * and the data page.
97 	 */
98 	vvar_size = PAGE_SIZE;
99 	size = vvar_size + info->size;
100 
101 	data_addr = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
102 	if (IS_ERR_VALUE(data_addr)) {
103 		ret = data_addr;
104 		goto out;
105 	}
106 	vdso_addr = data_addr + PAGE_SIZE;
107 
108 	vma = _install_special_mapping(mm, data_addr, vvar_size,
109 				       VM_READ | VM_MAYREAD,
110 				       &info->data_mapping);
111 	if (IS_ERR(vma)) {
112 		ret = PTR_ERR(vma);
113 		goto out;
114 	}
115 
116 	/* Map VDSO data page. */
117 	ret = remap_pfn_range(vma, data_addr,
118 			      virt_to_phys(vdso_data) >> PAGE_SHIFT,
119 			      PAGE_SIZE, PAGE_READONLY);
120 	if (ret)
121 		goto out;
122 
123 	/* Map VDSO code page. */
124 	vma = _install_special_mapping(mm, vdso_addr, info->size,
125 				       VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
126 				       &info->code_mapping);
127 	if (IS_ERR(vma)) {
128 		ret = PTR_ERR(vma);
129 		goto out;
130 	}
131 
132 	mm->context.vdso = (void *)vdso_addr;
133 	ret = 0;
134 
135 out:
136 	mmap_write_unlock(mm);
137 	return ret;
138 }
139