Loading...
1/*
2 * Adapted from arm64 version.
3 *
4 * Copyright (C) 2012 ARM Limited
5 * Copyright (C) 2015 Mentor Graphics Corporation.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/cache.h>
21#include <linux/elf.h>
22#include <linux/err.h>
23#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/of.h>
26#include <linux/printk.h>
27#include <linux/slab.h>
28#include <linux/timekeeper_internal.h>
29#include <linux/vmalloc.h>
30#include <asm/arch_timer.h>
31#include <asm/barrier.h>
32#include <asm/cacheflush.h>
33#include <asm/page.h>
34#include <asm/vdso.h>
35#include <asm/vdso_datapage.h>
36#include <clocksource/arm_arch_timer.h>
37
38#define MAX_SYMNAME 64
39
40static struct page **vdso_text_pagelist;
41
42extern char vdso_start[], vdso_end[];
43
44/* Total number of pages needed for the data and text portions of the VDSO. */
45unsigned int vdso_total_pages __ro_after_init;
46
47/*
48 * The VDSO data page.
49 */
50static union vdso_data_store vdso_data_store __page_aligned_data;
51static struct vdso_data *vdso_data = &vdso_data_store.data;
52
53static struct page *vdso_data_page __ro_after_init;
54static const struct vm_special_mapping vdso_data_mapping = {
55 .name = "[vvar]",
56 .pages = &vdso_data_page,
57};
58
59static int vdso_mremap(const struct vm_special_mapping *sm,
60 struct vm_area_struct *new_vma)
61{
62 unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
63 unsigned long vdso_size;
64
65 /* without VVAR page */
66 vdso_size = (vdso_total_pages - 1) << PAGE_SHIFT;
67
68 if (vdso_size != new_size)
69 return -EINVAL;
70
71 current->mm->context.vdso = new_vma->vm_start;
72
73 return 0;
74}
75
76static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
77 .name = "[vdso]",
78 .mremap = vdso_mremap,
79};
80
81struct elfinfo {
82 Elf32_Ehdr *hdr; /* ptr to ELF */
83 Elf32_Sym *dynsym; /* ptr to .dynsym section */
84 unsigned long dynsymsize; /* size of .dynsym section */
85 char *dynstr; /* ptr to .dynstr section */
86};
87
88/* Cached result of boot-time check for whether the arch timer exists,
89 * and if so, whether the virtual counter is useable.
90 */
91static bool cntvct_ok __ro_after_init;
92
93static bool __init cntvct_functional(void)
94{
95 struct device_node *np;
96 bool ret = false;
97
98 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
99 goto out;
100
101 /* The arm_arch_timer core should export
102 * arch_timer_use_virtual or similar so we don't have to do
103 * this.
104 */
105 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
106 if (!np)
107 goto out_put;
108
109 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
110 goto out_put;
111
112 ret = true;
113
114out_put:
115 of_node_put(np);
116out:
117 return ret;
118}
119
120static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
121 unsigned long *size)
122{
123 Elf32_Shdr *sechdrs;
124 unsigned int i;
125 char *secnames;
126
127 /* Grab section headers and strings so we can tell who is who */
128 sechdrs = (void *)ehdr + ehdr->e_shoff;
129 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
130
131 /* Find the section they want */
132 for (i = 1; i < ehdr->e_shnum; i++) {
133 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
134 if (size)
135 *size = sechdrs[i].sh_size;
136 return (void *)ehdr + sechdrs[i].sh_offset;
137 }
138 }
139
140 if (size)
141 *size = 0;
142 return NULL;
143}
144
145static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
146{
147 unsigned int i;
148
149 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
150 char name[MAX_SYMNAME], *c;
151
152 if (lib->dynsym[i].st_name == 0)
153 continue;
154 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
155 MAX_SYMNAME);
156 c = strchr(name, '@');
157 if (c)
158 *c = 0;
159 if (strcmp(symname, name) == 0)
160 return &lib->dynsym[i];
161 }
162 return NULL;
163}
164
165static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
166{
167 Elf32_Sym *sym;
168
169 sym = find_symbol(lib, symname);
170 if (!sym)
171 return;
172
173 sym->st_name = 0;
174}
175
176static void __init patch_vdso(void *ehdr)
177{
178 struct elfinfo einfo;
179
180 einfo = (struct elfinfo) {
181 .hdr = ehdr,
182 };
183
184 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
185 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
186
187 /* If the virtual counter is absent or non-functional we don't
188 * want programs to incur the slight additional overhead of
189 * dispatching through the VDSO only to fall back to syscalls.
190 */
191 if (!cntvct_ok) {
192 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
193 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
194 }
195}
196
197static int __init vdso_init(void)
198{
199 unsigned int text_pages;
200 int i;
201
202 if (memcmp(vdso_start, "\177ELF", 4)) {
203 pr_err("VDSO is not a valid ELF object!\n");
204 return -ENOEXEC;
205 }
206
207 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
208 pr_debug("vdso: %i text pages at base %p\n", text_pages, vdso_start);
209
210 /* Allocate the VDSO text pagelist */
211 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
212 GFP_KERNEL);
213 if (vdso_text_pagelist == NULL)
214 return -ENOMEM;
215
216 /* Grab the VDSO data page. */
217 vdso_data_page = virt_to_page(vdso_data);
218
219 /* Grab the VDSO text pages. */
220 for (i = 0; i < text_pages; i++) {
221 struct page *page;
222
223 page = virt_to_page(vdso_start + i * PAGE_SIZE);
224 vdso_text_pagelist[i] = page;
225 }
226
227 vdso_text_mapping.pages = vdso_text_pagelist;
228
229 vdso_total_pages = 1; /* for the data/vvar page */
230 vdso_total_pages += text_pages;
231
232 cntvct_ok = cntvct_functional();
233
234 patch_vdso(vdso_start);
235
236 return 0;
237}
238arch_initcall(vdso_init);
239
240static int install_vvar(struct mm_struct *mm, unsigned long addr)
241{
242 struct vm_area_struct *vma;
243
244 vma = _install_special_mapping(mm, addr, PAGE_SIZE,
245 VM_READ | VM_MAYREAD,
246 &vdso_data_mapping);
247
248 return PTR_ERR_OR_ZERO(vma);
249}
250
251/* assumes mmap_sem is write-locked */
252void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
253{
254 struct vm_area_struct *vma;
255 unsigned long len;
256
257 mm->context.vdso = 0;
258
259 if (vdso_text_pagelist == NULL)
260 return;
261
262 if (install_vvar(mm, addr))
263 return;
264
265 /* Account for vvar page. */
266 addr += PAGE_SIZE;
267 len = (vdso_total_pages - 1) << PAGE_SHIFT;
268
269 vma = _install_special_mapping(mm, addr, len,
270 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
271 &vdso_text_mapping);
272
273 if (!IS_ERR(vma))
274 mm->context.vdso = addr;
275}
276
277static void vdso_write_begin(struct vdso_data *vdata)
278{
279 ++vdso_data->seq_count;
280 smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
281}
282
283static void vdso_write_end(struct vdso_data *vdata)
284{
285 smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
286 ++vdso_data->seq_count;
287}
288
289static bool tk_is_cntvct(const struct timekeeper *tk)
290{
291 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
292 return false;
293
294 if (!tk->tkr_mono.clock->archdata.vdso_direct)
295 return false;
296
297 return true;
298}
299
300/**
301 * update_vsyscall - update the vdso data page
302 *
303 * Increment the sequence counter, making it odd, indicating to
304 * userspace that an update is in progress. Update the fields used
305 * for coarse clocks and, if the architected system timer is in use,
306 * the fields used for high precision clocks. Increment the sequence
307 * counter again, making it even, indicating to userspace that the
308 * update is finished.
309 *
310 * Userspace is expected to sample seq_count before reading any other
311 * fields from the data page. If seq_count is odd, userspace is
312 * expected to wait until it becomes even. After copying data from
313 * the page, userspace must sample seq_count again; if it has changed
314 * from its previous value, userspace must retry the whole sequence.
315 *
316 * Calls to update_vsyscall are serialized by the timekeeping core.
317 */
318void update_vsyscall(struct timekeeper *tk)
319{
320 struct timespec64 *wtm = &tk->wall_to_monotonic;
321
322 if (!cntvct_ok) {
323 /* The entry points have been zeroed, so there is no
324 * point in updating the data page.
325 */
326 return;
327 }
328
329 vdso_write_begin(vdso_data);
330
331 vdso_data->tk_is_cntvct = tk_is_cntvct(tk);
332 vdso_data->xtime_coarse_sec = tk->xtime_sec;
333 vdso_data->xtime_coarse_nsec = (u32)(tk->tkr_mono.xtime_nsec >>
334 tk->tkr_mono.shift);
335 vdso_data->wtm_clock_sec = wtm->tv_sec;
336 vdso_data->wtm_clock_nsec = wtm->tv_nsec;
337
338 if (vdso_data->tk_is_cntvct) {
339 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
340 vdso_data->xtime_clock_sec = tk->xtime_sec;
341 vdso_data->xtime_clock_snsec = tk->tkr_mono.xtime_nsec;
342 vdso_data->cs_mult = tk->tkr_mono.mult;
343 vdso_data->cs_shift = tk->tkr_mono.shift;
344 vdso_data->cs_mask = tk->tkr_mono.mask;
345 }
346
347 vdso_write_end(vdso_data);
348
349 flush_dcache_page(virt_to_page(vdso_data));
350}
351
352void update_vsyscall_tz(void)
353{
354 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
355 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
356 flush_dcache_page(virt_to_page(vdso_data));
357}
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/elf.h>
11#include <linux/err.h>
12#include <linux/kernel.h>
13#include <linux/mm.h>
14#include <linux/of.h>
15#include <linux/printk.h>
16#include <linux/slab.h>
17#include <linux/timekeeper_internal.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 <asm/vdso_datapage.h>
25#include <clocksource/arm_arch_timer.h>
26
27#define MAX_SYMNAME 64
28
29static struct page **vdso_text_pagelist;
30
31extern char vdso_start[], vdso_end[];
32
33/* Total number of pages needed for the data and text portions of the VDSO. */
34unsigned int vdso_total_pages __ro_after_init;
35
36/*
37 * The VDSO data page.
38 */
39static union vdso_data_store vdso_data_store __page_aligned_data;
40static struct vdso_data *vdso_data = &vdso_data_store.data;
41
42static struct page *vdso_data_page __ro_after_init;
43static const struct vm_special_mapping vdso_data_mapping = {
44 .name = "[vvar]",
45 .pages = &vdso_data_page,
46};
47
48static int vdso_mremap(const struct vm_special_mapping *sm,
49 struct vm_area_struct *new_vma)
50{
51 unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
52 unsigned long vdso_size;
53
54 /* without VVAR page */
55 vdso_size = (vdso_total_pages - 1) << PAGE_SHIFT;
56
57 if (vdso_size != new_size)
58 return -EINVAL;
59
60 current->mm->context.vdso = new_vma->vm_start;
61
62 return 0;
63}
64
65static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
66 .name = "[vdso]",
67 .mremap = vdso_mremap,
68};
69
70struct elfinfo {
71 Elf32_Ehdr *hdr; /* ptr to ELF */
72 Elf32_Sym *dynsym; /* ptr to .dynsym section */
73 unsigned long dynsymsize; /* size of .dynsym section */
74 char *dynstr; /* ptr to .dynstr section */
75};
76
77/* Cached result of boot-time check for whether the arch timer exists,
78 * and if so, whether the virtual counter is useable.
79 */
80static bool cntvct_ok __ro_after_init;
81
82static bool __init cntvct_functional(void)
83{
84 struct device_node *np;
85 bool ret = false;
86
87 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
88 goto out;
89
90 /* The arm_arch_timer core should export
91 * arch_timer_use_virtual or similar so we don't have to do
92 * this.
93 */
94 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
95 if (!np)
96 goto out_put;
97
98 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
99 goto out_put;
100
101 ret = true;
102
103out_put:
104 of_node_put(np);
105out:
106 return ret;
107}
108
109static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
110 unsigned long *size)
111{
112 Elf32_Shdr *sechdrs;
113 unsigned int i;
114 char *secnames;
115
116 /* Grab section headers and strings so we can tell who is who */
117 sechdrs = (void *)ehdr + ehdr->e_shoff;
118 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
119
120 /* Find the section they want */
121 for (i = 1; i < ehdr->e_shnum; i++) {
122 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
123 if (size)
124 *size = sechdrs[i].sh_size;
125 return (void *)ehdr + sechdrs[i].sh_offset;
126 }
127 }
128
129 if (size)
130 *size = 0;
131 return NULL;
132}
133
134static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
135{
136 unsigned int i;
137
138 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
139 char name[MAX_SYMNAME], *c;
140
141 if (lib->dynsym[i].st_name == 0)
142 continue;
143 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
144 MAX_SYMNAME);
145 c = strchr(name, '@');
146 if (c)
147 *c = 0;
148 if (strcmp(symname, name) == 0)
149 return &lib->dynsym[i];
150 }
151 return NULL;
152}
153
154static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
155{
156 Elf32_Sym *sym;
157
158 sym = find_symbol(lib, symname);
159 if (!sym)
160 return;
161
162 sym->st_name = 0;
163}
164
165static void __init patch_vdso(void *ehdr)
166{
167 struct elfinfo einfo;
168
169 einfo = (struct elfinfo) {
170 .hdr = ehdr,
171 };
172
173 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
174 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
175
176 /* If the virtual counter is absent or non-functional we don't
177 * want programs to incur the slight additional overhead of
178 * dispatching through the VDSO only to fall back to syscalls.
179 */
180 if (!cntvct_ok) {
181 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
182 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
183 }
184}
185
186static int __init vdso_init(void)
187{
188 unsigned int text_pages;
189 int i;
190
191 if (memcmp(vdso_start, "\177ELF", 4)) {
192 pr_err("VDSO is not a valid ELF object!\n");
193 return -ENOEXEC;
194 }
195
196 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
197
198 /* Allocate the VDSO text pagelist */
199 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
200 GFP_KERNEL);
201 if (vdso_text_pagelist == NULL)
202 return -ENOMEM;
203
204 /* Grab the VDSO data page. */
205 vdso_data_page = virt_to_page(vdso_data);
206
207 /* Grab the VDSO text pages. */
208 for (i = 0; i < text_pages; i++) {
209 struct page *page;
210
211 page = virt_to_page(vdso_start + i * PAGE_SIZE);
212 vdso_text_pagelist[i] = page;
213 }
214
215 vdso_text_mapping.pages = vdso_text_pagelist;
216
217 vdso_total_pages = 1; /* for the data/vvar page */
218 vdso_total_pages += text_pages;
219
220 cntvct_ok = cntvct_functional();
221
222 patch_vdso(vdso_start);
223
224 return 0;
225}
226arch_initcall(vdso_init);
227
228static int install_vvar(struct mm_struct *mm, unsigned long addr)
229{
230 struct vm_area_struct *vma;
231
232 vma = _install_special_mapping(mm, addr, PAGE_SIZE,
233 VM_READ | VM_MAYREAD,
234 &vdso_data_mapping);
235
236 return PTR_ERR_OR_ZERO(vma);
237}
238
239/* assumes mmap_sem is write-locked */
240void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
241{
242 struct vm_area_struct *vma;
243 unsigned long len;
244
245 mm->context.vdso = 0;
246
247 if (vdso_text_pagelist == NULL)
248 return;
249
250 if (install_vvar(mm, addr))
251 return;
252
253 /* Account for vvar page. */
254 addr += PAGE_SIZE;
255 len = (vdso_total_pages - 1) << PAGE_SHIFT;
256
257 vma = _install_special_mapping(mm, addr, len,
258 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
259 &vdso_text_mapping);
260
261 if (!IS_ERR(vma))
262 mm->context.vdso = addr;
263}
264
265static void vdso_write_begin(struct vdso_data *vdata)
266{
267 ++vdso_data->seq_count;
268 smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
269}
270
271static void vdso_write_end(struct vdso_data *vdata)
272{
273 smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
274 ++vdso_data->seq_count;
275}
276
277static bool tk_is_cntvct(const struct timekeeper *tk)
278{
279 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
280 return false;
281
282 if (!tk->tkr_mono.clock->archdata.vdso_direct)
283 return false;
284
285 return true;
286}
287
288/**
289 * update_vsyscall - update the vdso data page
290 *
291 * Increment the sequence counter, making it odd, indicating to
292 * userspace that an update is in progress. Update the fields used
293 * for coarse clocks and, if the architected system timer is in use,
294 * the fields used for high precision clocks. Increment the sequence
295 * counter again, making it even, indicating to userspace that the
296 * update is finished.
297 *
298 * Userspace is expected to sample seq_count before reading any other
299 * fields from the data page. If seq_count is odd, userspace is
300 * expected to wait until it becomes even. After copying data from
301 * the page, userspace must sample seq_count again; if it has changed
302 * from its previous value, userspace must retry the whole sequence.
303 *
304 * Calls to update_vsyscall are serialized by the timekeeping core.
305 */
306void update_vsyscall(struct timekeeper *tk)
307{
308 struct timespec64 *wtm = &tk->wall_to_monotonic;
309
310 if (!cntvct_ok) {
311 /* The entry points have been zeroed, so there is no
312 * point in updating the data page.
313 */
314 return;
315 }
316
317 vdso_write_begin(vdso_data);
318
319 vdso_data->tk_is_cntvct = tk_is_cntvct(tk);
320 vdso_data->xtime_coarse_sec = tk->xtime_sec;
321 vdso_data->xtime_coarse_nsec = (u32)(tk->tkr_mono.xtime_nsec >>
322 tk->tkr_mono.shift);
323 vdso_data->wtm_clock_sec = wtm->tv_sec;
324 vdso_data->wtm_clock_nsec = wtm->tv_nsec;
325
326 if (vdso_data->tk_is_cntvct) {
327 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
328 vdso_data->xtime_clock_sec = tk->xtime_sec;
329 vdso_data->xtime_clock_snsec = tk->tkr_mono.xtime_nsec;
330 vdso_data->cs_mult = tk->tkr_mono.mult;
331 vdso_data->cs_shift = tk->tkr_mono.shift;
332 vdso_data->cs_mask = tk->tkr_mono.mask;
333 }
334
335 vdso_write_end(vdso_data);
336
337 flush_dcache_page(virt_to_page(vdso_data));
338}
339
340void update_vsyscall_tz(void)
341{
342 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
343 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
344 flush_dcache_page(virt_to_page(vdso_data));
345}