1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Adapted from arm64 version.
4  *
5  * Copyright (C) 2012 ARM Limited
6  * Copyright (C) 2015 Mentor Graphics Corporation.
7  */
8 
9 #include <linux/cache.h>
10 #include <linux/vdso_datastore.h>
11 #include <linux/elf.h>
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/of.h>
16 #include <linux/printk.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <asm/arch_timer.h>
20 #include <asm/barrier.h>
21 #include <asm/cacheflush.h>
22 #include <asm/page.h>
23 #include <asm/vdso.h>
24 #include <clocksource/arm_arch_timer.h>
25 #include <vdso/helpers.h>
26 #include <vdso/vsyscall.h>
27 
28 #define MAX_SYMNAME	64
29 
30 static struct page **vdso_text_pagelist;
31 
32 extern char vdso_start[], vdso_end[];
33 
34 /* Total number of pages needed for the data and text portions of the VDSO. */
35 unsigned int vdso_total_pages __ro_after_init;
36 
37 static int vdso_mremap(const struct vm_special_mapping *sm,
38 		struct vm_area_struct *new_vma)
39 {
40 	current->mm->context.vdso = new_vma->vm_start;
41 
42 	return 0;
43 }
44 
45 static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
46 	.name = "[vdso]",
47 	.mremap = vdso_mremap,
48 };
49 
50 struct elfinfo {
51 	Elf32_Ehdr	*hdr;		/* ptr to ELF */
52 	Elf32_Sym	*dynsym;	/* ptr to .dynsym section */
53 	unsigned long	dynsymsize;	/* size of .dynsym section */
54 	char		*dynstr;	/* ptr to .dynstr section */
55 };
56 
57 /* Cached result of boot-time check for whether the arch timer exists,
58  * and if so, whether the virtual counter is useable.
59  */
60 bool cntvct_ok __ro_after_init;
61 
62 static bool __init cntvct_functional(void)
63 {
64 	struct device_node *np;
65 	bool ret = false;
66 
67 	if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
68 		goto out;
69 
70 	/* The arm_arch_timer core should export
71 	 * arch_timer_use_virtual or similar so we don't have to do
72 	 * this.
73 	 */
74 	np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
75 	if (!np)
76 		np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
77 	if (!np)
78 		goto out_put;
79 
80 	if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
81 		goto out_put;
82 
83 	ret = true;
84 
85 out_put:
86 	of_node_put(np);
87 out:
88 	return ret;
89 }
90 
91 static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
92 				  unsigned long *size)
93 {
94 	Elf32_Shdr *sechdrs;
95 	unsigned int i;
96 	char *secnames;
97 
98 	/* Grab section headers and strings so we can tell who is who */
99 	sechdrs = (void *)ehdr + ehdr->e_shoff;
100 	secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
101 
102 	/* Find the section they want */
103 	for (i = 1; i < ehdr->e_shnum; i++) {
104 		if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
105 			if (size)
106 				*size = sechdrs[i].sh_size;
107 			return (void *)ehdr + sechdrs[i].sh_offset;
108 		}
109 	}
110 
111 	if (size)
112 		*size = 0;
113 	return NULL;
114 }
115 
116 static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
117 {
118 	unsigned int i;
119 
120 	for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
121 		char name[MAX_SYMNAME], *c;
122 
123 		if (lib->dynsym[i].st_name == 0)
124 			continue;
125 		strscpy(name, lib->dynstr + lib->dynsym[i].st_name,
126 			MAX_SYMNAME);
127 		c = strchr(name, '@');
128 		if (c)
129 			*c = 0;
130 		if (strcmp(symname, name) == 0)
131 			return &lib->dynsym[i];
132 	}
133 	return NULL;
134 }
135 
136 static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
137 {
138 	Elf32_Sym *sym;
139 
140 	sym = find_symbol(lib, symname);
141 	if (!sym)
142 		return;
143 
144 	sym->st_name = 0;
145 }
146 
147 static void __init patch_vdso(void *ehdr)
148 {
149 	struct elfinfo einfo;
150 
151 	einfo = (struct elfinfo) {
152 		.hdr = ehdr,
153 	};
154 
155 	einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
156 	einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
157 
158 	/* If the virtual counter is absent or non-functional we don't
159 	 * want programs to incur the slight additional overhead of
160 	 * dispatching through the VDSO only to fall back to syscalls.
161 	 */
162 	if (!cntvct_ok) {
163 		vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
164 		vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
165 		vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64");
166 	}
167 }
168 
169 static int __init vdso_init(void)
170 {
171 	unsigned int text_pages;
172 	int i;
173 
174 	if (memcmp(vdso_start, "\177ELF", 4)) {
175 		pr_err("VDSO is not a valid ELF object!\n");
176 		return -ENOEXEC;
177 	}
178 
179 	text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
180 
181 	/* Allocate the VDSO text pagelist */
182 	vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
183 				     GFP_KERNEL);
184 	if (vdso_text_pagelist == NULL)
185 		return -ENOMEM;
186 
187 	/* Grab the VDSO text pages. */
188 	for (i = 0; i < text_pages; i++) {
189 		struct page *page;
190 
191 		page = virt_to_page(vdso_start + i * PAGE_SIZE);
192 		vdso_text_pagelist[i] = page;
193 	}
194 
195 	vdso_text_mapping.pages = vdso_text_pagelist;
196 
197 	vdso_total_pages = VDSO_NR_PAGES; /* for the data/vvar pages */
198 	vdso_total_pages += text_pages;
199 
200 	cntvct_ok = cntvct_functional();
201 
202 	patch_vdso(vdso_start);
203 
204 	return 0;
205 }
206 arch_initcall(vdso_init);
207 
208 static_assert(__VDSO_PAGES == VDSO_NR_PAGES);
209 
210 /* assumes mmap_lock is write-locked */
211 void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
212 {
213 	struct vm_area_struct *vma;
214 	unsigned long len;
215 
216 	mm->context.vdso = 0;
217 
218 	if (vdso_text_pagelist == NULL)
219 		return;
220 
221 	if (IS_ERR(vdso_install_vvar_mapping(mm, addr)))
222 		return;
223 
224 	/* Account for vvar pages. */
225 	addr += VDSO_NR_PAGES * PAGE_SIZE;
226 	len = (vdso_total_pages - VDSO_NR_PAGES) << PAGE_SHIFT;
227 
228 	vma = _install_special_mapping(mm, addr, len,
229 		VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
230 		&vdso_text_mapping);
231 
232 	if (!IS_ERR(vma))
233 		mm->context.vdso = addr;
234 }
235 
236