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