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