blob: 890439737374242e78a0ee8fb26bc6ddbcda0954 [file] [log] [blame]
Nathan Lynchecf99a42015-03-25 19:15:08 +01001/*
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
Jisheng Zhang92bb8d52016-08-15 09:20:21 +010020#include <linux/cache.h>
Nathan Lynchecf99a42015-03-25 19:15:08 +010021#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
Jinbum Park1475b5a2018-03-06 01:37:21 +010042extern char vdso_start[], vdso_end[];
43
Nathan Lynchecf99a42015-03-25 19:15:08 +010044/* Total number of pages needed for the data and text portions of the VDSO. */
Jisheng Zhang92bb8d52016-08-15 09:20:21 +010045unsigned int vdso_total_pages __ro_after_init;
Nathan Lynchecf99a42015-03-25 19:15:08 +010046
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
Jisheng Zhang92bb8d52016-08-15 09:20:21 +010053static struct page *vdso_data_page __ro_after_init;
54static const struct vm_special_mapping vdso_data_mapping = {
Nathan Lynchecf99a42015-03-25 19:15:08 +010055 .name = "[vvar]",
56 .pages = &vdso_data_page,
57};
58
Jisheng Zhang92bb8d52016-08-15 09:20:21 +010059static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
Nathan Lynchecf99a42015-03-25 19:15:08 +010060 .name = "[vdso]",
61};
62
63struct elfinfo {
64 Elf32_Ehdr *hdr; /* ptr to ELF */
65 Elf32_Sym *dynsym; /* ptr to .dynsym section */
66 unsigned long dynsymsize; /* size of .dynsym section */
67 char *dynstr; /* ptr to .dynstr section */
68};
69
70/* Cached result of boot-time check for whether the arch timer exists,
71 * and if so, whether the virtual counter is useable.
72 */
Jisheng Zhang92bb8d52016-08-15 09:20:21 +010073static bool cntvct_ok __ro_after_init;
Nathan Lynchecf99a42015-03-25 19:15:08 +010074
75static bool __init cntvct_functional(void)
76{
77 struct device_node *np;
78 bool ret = false;
79
80 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
81 goto out;
82
83 /* The arm_arch_timer core should export
84 * arch_timer_use_virtual or similar so we don't have to do
85 * this.
86 */
87 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
88 if (!np)
89 goto out_put;
90
91 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
92 goto out_put;
93
94 ret = true;
95
96out_put:
97 of_node_put(np);
98out:
99 return ret;
100}
101
102static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
103 unsigned long *size)
104{
105 Elf32_Shdr *sechdrs;
106 unsigned int i;
107 char *secnames;
108
109 /* Grab section headers and strings so we can tell who is who */
110 sechdrs = (void *)ehdr + ehdr->e_shoff;
111 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
112
113 /* Find the section they want */
114 for (i = 1; i < ehdr->e_shnum; i++) {
115 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
116 if (size)
117 *size = sechdrs[i].sh_size;
118 return (void *)ehdr + sechdrs[i].sh_offset;
119 }
120 }
121
122 if (size)
123 *size = 0;
124 return NULL;
125}
126
127static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
128{
129 unsigned int i;
130
131 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
132 char name[MAX_SYMNAME], *c;
133
134 if (lib->dynsym[i].st_name == 0)
135 continue;
136 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
137 MAX_SYMNAME);
138 c = strchr(name, '@');
139 if (c)
140 *c = 0;
141 if (strcmp(symname, name) == 0)
142 return &lib->dynsym[i];
143 }
144 return NULL;
145}
146
147static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
148{
149 Elf32_Sym *sym;
150
151 sym = find_symbol(lib, symname);
152 if (!sym)
153 return;
154
155 sym->st_name = 0;
156}
157
158static void __init patch_vdso(void *ehdr)
159{
160 struct elfinfo einfo;
161
162 einfo = (struct elfinfo) {
163 .hdr = ehdr,
164 };
165
166 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
167 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
168
169 /* If the virtual counter is absent or non-functional we don't
170 * want programs to incur the slight additional overhead of
171 * dispatching through the VDSO only to fall back to syscalls.
172 */
173 if (!cntvct_ok) {
174 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
175 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
176 }
177}
178
179static int __init vdso_init(void)
180{
181 unsigned int text_pages;
182 int i;
183
Jinbum Park1475b5a2018-03-06 01:37:21 +0100184 if (memcmp(vdso_start, "\177ELF", 4)) {
Nathan Lynchecf99a42015-03-25 19:15:08 +0100185 pr_err("VDSO is not a valid ELF object!\n");
186 return -ENOEXEC;
187 }
188
Jinbum Park1475b5a2018-03-06 01:37:21 +0100189 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
190 pr_debug("vdso: %i text pages at base %p\n", text_pages, vdso_start);
Nathan Lynchecf99a42015-03-25 19:15:08 +0100191
192 /* Allocate the VDSO text pagelist */
193 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
194 GFP_KERNEL);
195 if (vdso_text_pagelist == NULL)
196 return -ENOMEM;
197
198 /* Grab the VDSO data page. */
199 vdso_data_page = virt_to_page(vdso_data);
200
201 /* Grab the VDSO text pages. */
202 for (i = 0; i < text_pages; i++) {
203 struct page *page;
204
Jinbum Park1475b5a2018-03-06 01:37:21 +0100205 page = virt_to_page(vdso_start + i * PAGE_SIZE);
Nathan Lynchecf99a42015-03-25 19:15:08 +0100206 vdso_text_pagelist[i] = page;
207 }
208
209 vdso_text_mapping.pages = vdso_text_pagelist;
210
211 vdso_total_pages = 1; /* for the data/vvar page */
212 vdso_total_pages += text_pages;
213
214 cntvct_ok = cntvct_functional();
215
Jinbum Park1475b5a2018-03-06 01:37:21 +0100216 patch_vdso(vdso_start);
Nathan Lynchecf99a42015-03-25 19:15:08 +0100217
218 return 0;
219}
220arch_initcall(vdso_init);
221
222static int install_vvar(struct mm_struct *mm, unsigned long addr)
223{
224 struct vm_area_struct *vma;
225
226 vma = _install_special_mapping(mm, addr, PAGE_SIZE,
227 VM_READ | VM_MAYREAD,
228 &vdso_data_mapping);
229
Prasanna Karthik38fc2f62015-12-08 17:30:25 +0100230 return PTR_ERR_OR_ZERO(vma);
Nathan Lynchecf99a42015-03-25 19:15:08 +0100231}
232
233/* assumes mmap_sem is write-locked */
234void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
235{
236 struct vm_area_struct *vma;
237 unsigned long len;
238
239 mm->context.vdso = 0;
240
241 if (vdso_text_pagelist == NULL)
242 return;
243
244 if (install_vvar(mm, addr))
245 return;
246
247 /* Account for vvar page. */
248 addr += PAGE_SIZE;
249 len = (vdso_total_pages - 1) << PAGE_SHIFT;
250
251 vma = _install_special_mapping(mm, addr, len,
252 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
253 &vdso_text_mapping);
254
255 if (!IS_ERR(vma))
256 mm->context.vdso = addr;
257}
258
259static void vdso_write_begin(struct vdso_data *vdata)
260{
261 ++vdso_data->seq_count;
262 smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
263}
264
265static void vdso_write_end(struct vdso_data *vdata)
266{
267 smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
268 ++vdso_data->seq_count;
269}
270
271static bool tk_is_cntvct(const struct timekeeper *tk)
272{
273 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
274 return false;
275
Scott Wood1d8f51d2016-09-22 03:35:18 -0500276 if (!tk->tkr_mono.clock->archdata.vdso_direct)
Nathan Lynchecf99a42015-03-25 19:15:08 +0100277 return false;
278
279 return true;
280}
281
282/**
283 * update_vsyscall - update the vdso data page
284 *
285 * Increment the sequence counter, making it odd, indicating to
286 * userspace that an update is in progress. Update the fields used
287 * for coarse clocks and, if the architected system timer is in use,
288 * the fields used for high precision clocks. Increment the sequence
289 * counter again, making it even, indicating to userspace that the
290 * update is finished.
291 *
292 * Userspace is expected to sample seq_count before reading any other
293 * fields from the data page. If seq_count is odd, userspace is
294 * expected to wait until it becomes even. After copying data from
295 * the page, userspace must sample seq_count again; if it has changed
296 * from its previous value, userspace must retry the whole sequence.
297 *
298 * Calls to update_vsyscall are serialized by the timekeeping core.
299 */
300void update_vsyscall(struct timekeeper *tk)
301{
Nathan Lynchecf99a42015-03-25 19:15:08 +0100302 struct timespec64 *wtm = &tk->wall_to_monotonic;
303
304 if (!cntvct_ok) {
305 /* The entry points have been zeroed, so there is no
306 * point in updating the data page.
307 */
308 return;
309 }
310
311 vdso_write_begin(vdso_data);
312
Nathan Lynchecf99a42015-03-25 19:15:08 +0100313 vdso_data->tk_is_cntvct = tk_is_cntvct(tk);
Nathan Lynch09edea42015-08-10 17:36:06 +0100314 vdso_data->xtime_coarse_sec = tk->xtime_sec;
315 vdso_data->xtime_coarse_nsec = (u32)(tk->tkr_mono.xtime_nsec >>
316 tk->tkr_mono.shift);
Nathan Lynchecf99a42015-03-25 19:15:08 +0100317 vdso_data->wtm_clock_sec = wtm->tv_sec;
318 vdso_data->wtm_clock_nsec = wtm->tv_nsec;
319
320 if (vdso_data->tk_is_cntvct) {
Linus Torvaldsbb0fd7a2015-04-14 21:03:26 -0700321 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
Nathan Lynchecf99a42015-03-25 19:15:08 +0100322 vdso_data->xtime_clock_sec = tk->xtime_sec;
Linus Torvaldsbb0fd7a2015-04-14 21:03:26 -0700323 vdso_data->xtime_clock_snsec = tk->tkr_mono.xtime_nsec;
324 vdso_data->cs_mult = tk->tkr_mono.mult;
325 vdso_data->cs_shift = tk->tkr_mono.shift;
326 vdso_data->cs_mask = tk->tkr_mono.mask;
Nathan Lynchecf99a42015-03-25 19:15:08 +0100327 }
328
329 vdso_write_end(vdso_data);
330
331 flush_dcache_page(virt_to_page(vdso_data));
332}
333
334void update_vsyscall_tz(void)
335{
336 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
337 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
338 flush_dcache_page(virt_to_page(vdso_data));
339}