xref: /linux/arch/loongarch/kernel/vdso.c (revision fcb3ad4366b9c810cbb9da34c076a9a52d8aa1e0)
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/time_namespace.h>
18 
19 #include <asm/page.h>
20 #include <asm/vdso.h>
21 #include <vdso/helpers.h>
22 #include <vdso/vsyscall.h>
23 #include <vdso/datapage.h>
24 #include <generated/vdso-offsets.h>
25 
26 extern char vdso_start[], vdso_end[];
27 
28 /* Kernel-provided data used by the VDSO. */
29 static union vdso_data_store generic_vdso_data __page_aligned_data;
30 
31 static union {
32 	u8 page[LOONGARCH_VDSO_DATA_SIZE];
33 	struct loongarch_vdso_data vdata;
34 } loongarch_vdso_data __page_aligned_data;
35 
36 struct vdso_data *vdso_data = generic_vdso_data.data;
37 struct vdso_pcpu_data *vdso_pdata = loongarch_vdso_data.vdata.pdata;
38 struct vdso_rng_data *vdso_rng_data = &loongarch_vdso_data.vdata.rng_data;
39 
40 static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
41 {
42 	current->mm->context.vdso = (void *)(new_vma->vm_start);
43 
44 	return 0;
45 }
46 
47 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
48 			     struct vm_area_struct *vma, struct vm_fault *vmf)
49 {
50 	unsigned long pfn;
51 	struct page *timens_page = find_timens_vvar_page(vma);
52 
53 	switch (vmf->pgoff) {
54 	case VVAR_GENERIC_PAGE_OFFSET:
55 		if (!timens_page)
56 			pfn = sym_to_pfn(vdso_data);
57 		else
58 			pfn = page_to_pfn(timens_page);
59 		break;
60 #ifdef CONFIG_TIME_NS
61 	case VVAR_TIMENS_PAGE_OFFSET:
62 		/*
63 		 * If a task belongs to a time namespace then a namespace specific
64 		 * VVAR is mapped with the VVAR_GENERIC_PAGE_OFFSET and the real
65 		 * VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET offset.
66 		 * See also the comment near timens_setup_vdso_data().
67 		 */
68 		if (!timens_page)
69 			return VM_FAULT_SIGBUS;
70 		else
71 			pfn = sym_to_pfn(vdso_data);
72 		break;
73 #endif /* CONFIG_TIME_NS */
74 	case VVAR_LOONGARCH_PAGES_START ... VVAR_LOONGARCH_PAGES_END:
75 		pfn = sym_to_pfn(&loongarch_vdso_data) + vmf->pgoff - VVAR_LOONGARCH_PAGES_START;
76 		break;
77 	default:
78 		return VM_FAULT_SIGBUS;
79 	}
80 
81 	return vmf_insert_pfn(vma, vmf->address, pfn);
82 }
83 
84 struct loongarch_vdso_info vdso_info = {
85 	.vdso = vdso_start,
86 	.code_mapping = {
87 		.name = "[vdso]",
88 		.mremap = vdso_mremap,
89 	},
90 	.data_mapping = {
91 		.name = "[vvar]",
92 		.fault = vvar_fault,
93 	},
94 	.offset_sigreturn = vdso_offset_sigreturn,
95 };
96 
97 static int __init init_vdso(void)
98 {
99 	unsigned long i, cpu, pfn;
100 
101 	BUG_ON(!PAGE_ALIGNED(vdso_info.vdso));
102 
103 	for_each_possible_cpu(cpu)
104 		vdso_pdata[cpu].node = cpu_to_node(cpu);
105 
106 	vdso_info.size = PAGE_ALIGN(vdso_end - vdso_start);
107 	vdso_info.code_mapping.pages =
108 		kcalloc(vdso_info.size / PAGE_SIZE, sizeof(struct page *), GFP_KERNEL);
109 
110 	pfn = __phys_to_pfn(__pa_symbol(vdso_info.vdso));
111 	for (i = 0; i < vdso_info.size / PAGE_SIZE; i++)
112 		vdso_info.code_mapping.pages[i] = pfn_to_page(pfn + i);
113 
114 	return 0;
115 }
116 subsys_initcall(init_vdso);
117 
118 #ifdef CONFIG_TIME_NS
119 struct vdso_data *arch_get_vdso_data(void *vvar_page)
120 {
121 	return (struct vdso_data *)(vvar_page);
122 }
123 
124 /*
125  * The vvar mapping contains data for a specific time namespace, so when a
126  * task changes namespace we must unmap its vvar data for the old namespace.
127  * Subsequent faults will map in data for the new namespace.
128  *
129  * For more details see timens_setup_vdso_data().
130  */
131 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
132 {
133 	struct mm_struct *mm = task->mm;
134 	struct vm_area_struct *vma;
135 
136 	VMA_ITERATOR(vmi, mm, 0);
137 
138 	mmap_read_lock(mm);
139 	for_each_vma(vmi, vma) {
140 		if (vma_is_special_mapping(vma, &vdso_info.data_mapping))
141 			zap_vma_pages(vma);
142 	}
143 	mmap_read_unlock(mm);
144 
145 	return 0;
146 }
147 #endif
148 
149 static unsigned long vdso_base(void)
150 {
151 	unsigned long base = STACK_TOP;
152 
153 	if (current->flags & PF_RANDOMIZE) {
154 		base += get_random_u32_below(VDSO_RANDOMIZE_SIZE);
155 		base = PAGE_ALIGN(base);
156 	}
157 
158 	return base;
159 }
160 
161 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
162 {
163 	int ret;
164 	unsigned long size, data_addr, vdso_addr;
165 	struct mm_struct *mm = current->mm;
166 	struct vm_area_struct *vma;
167 	struct loongarch_vdso_info *info = current->thread.vdso;
168 
169 	if (mmap_write_lock_killable(mm))
170 		return -EINTR;
171 
172 	/*
173 	 * Determine total area size. This includes the VDSO data itself
174 	 * and the data pages.
175 	 */
176 	size = VVAR_SIZE + info->size;
177 
178 	data_addr = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
179 	if (IS_ERR_VALUE(data_addr)) {
180 		ret = data_addr;
181 		goto out;
182 	}
183 
184 	vma = _install_special_mapping(mm, data_addr, VVAR_SIZE,
185 				       VM_READ | VM_MAYREAD | VM_PFNMAP,
186 				       &info->data_mapping);
187 	if (IS_ERR(vma)) {
188 		ret = PTR_ERR(vma);
189 		goto out;
190 	}
191 
192 	vdso_addr = data_addr + VVAR_SIZE;
193 	vma = _install_special_mapping(mm, vdso_addr, info->size,
194 				       VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
195 				       &info->code_mapping);
196 	if (IS_ERR(vma)) {
197 		ret = PTR_ERR(vma);
198 		goto out;
199 	}
200 
201 	mm->context.vdso = (void *)vdso_addr;
202 	ret = 0;
203 
204 out:
205 	mmap_write_unlock(mm);
206 	return ret;
207 }
208