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
vdso_mremap(const struct vm_special_mapping * sm,struct vm_area_struct * new_vma)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 /* Boot-time check for whether the arch timer exists, and if so,
58 * whether the virtual counter is usable.
59 */
cntvct_functional(void)60 static bool __init cntvct_functional(void)
61 {
62 struct device_node *np;
63 bool ret = false;
64
65 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
66 goto out;
67
68 /* The arm_arch_timer core should export
69 * arch_timer_use_virtual or similar so we don't have to do
70 * this.
71 */
72 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
73 if (!np)
74 np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
75 if (!np)
76 goto out_put;
77
78 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
79 goto out_put;
80
81 ret = true;
82
83 out_put:
84 of_node_put(np);
85 out:
86 return ret;
87 }
88
find_section(Elf32_Ehdr * ehdr,const char * name,unsigned long * size)89 static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
90 unsigned long *size)
91 {
92 Elf32_Shdr *sechdrs;
93 unsigned int i;
94 char *secnames;
95
96 /* Grab section headers and strings so we can tell who is who */
97 sechdrs = (void *)ehdr + ehdr->e_shoff;
98 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
99
100 /* Find the section they want */
101 for (i = 1; i < ehdr->e_shnum; i++) {
102 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
103 if (size)
104 *size = sechdrs[i].sh_size;
105 return (void *)ehdr + sechdrs[i].sh_offset;
106 }
107 }
108
109 if (size)
110 *size = 0;
111 return NULL;
112 }
113
find_symbol(struct elfinfo * lib,const char * symname)114 static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
115 {
116 unsigned int i;
117
118 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
119 char name[MAX_SYMNAME], *c;
120
121 if (lib->dynsym[i].st_name == 0)
122 continue;
123 strscpy(name, lib->dynstr + lib->dynsym[i].st_name,
124 MAX_SYMNAME);
125 c = strchr(name, '@');
126 if (c)
127 *c = 0;
128 if (strcmp(symname, name) == 0)
129 return &lib->dynsym[i];
130 }
131 return NULL;
132 }
133
vdso_nullpatch_one(struct elfinfo * lib,const char * symname)134 static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
135 {
136 Elf32_Sym *sym;
137
138 sym = find_symbol(lib, symname);
139 if (!sym)
140 return;
141
142 sym->st_name = 0;
143 }
144
patch_vdso(void * ehdr)145 static void __init patch_vdso(void *ehdr)
146 {
147 struct elfinfo einfo;
148
149 einfo = (struct elfinfo) {
150 .hdr = ehdr,
151 };
152
153 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
154 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
155
156 /* If the virtual counter is absent or non-functional we don't
157 * want programs to incur the slight additional overhead of
158 * dispatching through the VDSO only to fall back to syscalls.
159 */
160 if (!cntvct_functional()) {
161 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
162 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
163 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64");
164 }
165 }
166
vdso_init(void)167 static int __init vdso_init(void)
168 {
169 unsigned int text_pages;
170 int i;
171
172 if (memcmp(vdso_start, "\177ELF", 4)) {
173 pr_err("VDSO is not a valid ELF object!\n");
174 return -ENOEXEC;
175 }
176
177 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
178
179 /* Allocate the VDSO text pagelist */
180 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
181 GFP_KERNEL);
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 */
arm_install_vdso(struct mm_struct * mm,unsigned long addr)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