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