GNU Linux-libre 4.9.309-gnu1
[releases.git] / arch / arm / kernel / vdso.c
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
40 static struct page **vdso_text_pagelist;
41
42 extern char vdso_start[], vdso_end[];
43
44 /* Total number of pages needed for the data and text portions of the VDSO. */
45 unsigned int vdso_total_pages __ro_after_init;
46
47 /*
48  * The VDSO data page.
49  */
50 static union vdso_data_store vdso_data_store __page_aligned_data;
51 static struct vdso_data *vdso_data = &vdso_data_store.data;
52
53 static struct page *vdso_data_page __ro_after_init;
54 static const struct vm_special_mapping vdso_data_mapping = {
55         .name = "[vvar]",
56         .pages = &vdso_data_page,
57 };
58
59 static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
60         .name = "[vdso]",
61 };
62
63 struct 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  */
73 static bool cntvct_ok __ro_after_init;
74
75 static 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                 np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
90         if (!np)
91                 goto out_put;
92
93         if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
94                 goto out_put;
95
96         ret = true;
97
98 out_put:
99         of_node_put(np);
100 out:
101         return ret;
102 }
103
104 static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
105                                   unsigned long *size)
106 {
107         Elf32_Shdr *sechdrs;
108         unsigned int i;
109         char *secnames;
110
111         /* Grab section headers and strings so we can tell who is who */
112         sechdrs = (void *)ehdr + ehdr->e_shoff;
113         secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
114
115         /* Find the section they want */
116         for (i = 1; i < ehdr->e_shnum; i++) {
117                 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
118                         if (size)
119                                 *size = sechdrs[i].sh_size;
120                         return (void *)ehdr + sechdrs[i].sh_offset;
121                 }
122         }
123
124         if (size)
125                 *size = 0;
126         return NULL;
127 }
128
129 static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
130 {
131         unsigned int i;
132
133         for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
134                 char name[MAX_SYMNAME], *c;
135
136                 if (lib->dynsym[i].st_name == 0)
137                         continue;
138                 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
139                         MAX_SYMNAME);
140                 c = strchr(name, '@');
141                 if (c)
142                         *c = 0;
143                 if (strcmp(symname, name) == 0)
144                         return &lib->dynsym[i];
145         }
146         return NULL;
147 }
148
149 static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
150 {
151         Elf32_Sym *sym;
152
153         sym = find_symbol(lib, symname);
154         if (!sym)
155                 return;
156
157         sym->st_name = 0;
158 }
159
160 static void __init patch_vdso(void *ehdr)
161 {
162         struct elfinfo einfo;
163
164         einfo = (struct elfinfo) {
165                 .hdr = ehdr,
166         };
167
168         einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
169         einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
170
171         /* If the virtual counter is absent or non-functional we don't
172          * want programs to incur the slight additional overhead of
173          * dispatching through the VDSO only to fall back to syscalls.
174          */
175         if (!cntvct_ok) {
176                 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
177                 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
178         }
179 }
180
181 static int __init vdso_init(void)
182 {
183         unsigned int text_pages;
184         int i;
185
186         if (memcmp(vdso_start, "\177ELF", 4)) {
187                 pr_err("VDSO is not a valid ELF object!\n");
188                 return -ENOEXEC;
189         }
190
191         text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
192         pr_debug("vdso: %i text pages at base %p\n", text_pages, vdso_start);
193
194         /* Allocate the VDSO text pagelist */
195         vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
196                                      GFP_KERNEL);
197         if (vdso_text_pagelist == NULL)
198                 return -ENOMEM;
199
200         /* Grab the VDSO data page. */
201         vdso_data_page = virt_to_page(vdso_data);
202
203         /* Grab the VDSO text pages. */
204         for (i = 0; i < text_pages; i++) {
205                 struct page *page;
206
207                 page = virt_to_page(vdso_start + i * PAGE_SIZE);
208                 vdso_text_pagelist[i] = page;
209         }
210
211         vdso_text_mapping.pages = vdso_text_pagelist;
212
213         vdso_total_pages = 1; /* for the data/vvar page */
214         vdso_total_pages += text_pages;
215
216         cntvct_ok = cntvct_functional();
217
218         patch_vdso(vdso_start);
219
220         return 0;
221 }
222 arch_initcall(vdso_init);
223
224 static int install_vvar(struct mm_struct *mm, unsigned long addr)
225 {
226         struct vm_area_struct *vma;
227
228         vma = _install_special_mapping(mm, addr, PAGE_SIZE,
229                                        VM_READ | VM_MAYREAD,
230                                        &vdso_data_mapping);
231
232         return PTR_ERR_OR_ZERO(vma);
233 }
234
235 /* assumes mmap_sem is write-locked */
236 void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
237 {
238         struct vm_area_struct *vma;
239         unsigned long len;
240
241         mm->context.vdso = 0;
242
243         if (vdso_text_pagelist == NULL)
244                 return;
245
246         if (install_vvar(mm, addr))
247                 return;
248
249         /* Account for vvar page. */
250         addr += PAGE_SIZE;
251         len = (vdso_total_pages - 1) << PAGE_SHIFT;
252
253         vma = _install_special_mapping(mm, addr, len,
254                 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
255                 &vdso_text_mapping);
256
257         if (!IS_ERR(vma))
258                 mm->context.vdso = addr;
259 }
260
261 static void vdso_write_begin(struct vdso_data *vdata)
262 {
263         ++vdso_data->seq_count;
264         smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
265 }
266
267 static void vdso_write_end(struct vdso_data *vdata)
268 {
269         smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
270         ++vdso_data->seq_count;
271 }
272
273 static bool tk_is_cntvct(const struct timekeeper *tk)
274 {
275         if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
276                 return false;
277
278         if (!tk->tkr_mono.clock->archdata.vdso_direct)
279                 return false;
280
281         return true;
282 }
283
284 /**
285  * update_vsyscall - update the vdso data page
286  *
287  * Increment the sequence counter, making it odd, indicating to
288  * userspace that an update is in progress.  Update the fields used
289  * for coarse clocks and, if the architected system timer is in use,
290  * the fields used for high precision clocks.  Increment the sequence
291  * counter again, making it even, indicating to userspace that the
292  * update is finished.
293  *
294  * Userspace is expected to sample seq_count before reading any other
295  * fields from the data page.  If seq_count is odd, userspace is
296  * expected to wait until it becomes even.  After copying data from
297  * the page, userspace must sample seq_count again; if it has changed
298  * from its previous value, userspace must retry the whole sequence.
299  *
300  * Calls to update_vsyscall are serialized by the timekeeping core.
301  */
302 void update_vsyscall(struct timekeeper *tk)
303 {
304         struct timespec64 *wtm = &tk->wall_to_monotonic;
305
306         if (!cntvct_ok) {
307                 /* The entry points have been zeroed, so there is no
308                  * point in updating the data page.
309                  */
310                 return;
311         }
312
313         vdso_write_begin(vdso_data);
314
315         vdso_data->tk_is_cntvct                 = tk_is_cntvct(tk);
316         vdso_data->xtime_coarse_sec             = tk->xtime_sec;
317         vdso_data->xtime_coarse_nsec            = (u32)(tk->tkr_mono.xtime_nsec >>
318                                                         tk->tkr_mono.shift);
319         vdso_data->wtm_clock_sec                = wtm->tv_sec;
320         vdso_data->wtm_clock_nsec               = wtm->tv_nsec;
321
322         if (vdso_data->tk_is_cntvct) {
323                 vdso_data->cs_cycle_last        = tk->tkr_mono.cycle_last;
324                 vdso_data->xtime_clock_sec      = tk->xtime_sec;
325                 vdso_data->xtime_clock_snsec    = tk->tkr_mono.xtime_nsec;
326                 vdso_data->cs_mult              = tk->tkr_mono.mult;
327                 vdso_data->cs_shift             = tk->tkr_mono.shift;
328                 vdso_data->cs_mask              = tk->tkr_mono.mask;
329         }
330
331         vdso_write_end(vdso_data);
332
333         flush_dcache_page(virt_to_page(vdso_data));
334 }
335
336 void update_vsyscall_tz(void)
337 {
338         vdso_data->tz_minuteswest       = sys_tz.tz_minuteswest;
339         vdso_data->tz_dsttime           = sys_tz.tz_dsttime;
340         flush_dcache_page(virt_to_page(vdso_data));
341 }