blob: c0f315ecfa7c08fe6213f1311bf2cf4363d00945 [file] [log] [blame]
Will Deacon9031fef2012-03-05 11:49:31 +00001/*
2 * VDSO implementation for AArch64 and vector page setup for AArch32.
3 *
4 * Copyright (C) 2012 ARM Limited
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Will Deacon <will.deacon@arm.com>
19 */
20
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +080021#include <linux/cache.h>
Will Deacon9031fef2012-03-05 11:49:31 +000022#include <linux/clocksource.h>
23#include <linux/elf.h>
24#include <linux/err.h>
25#include <linux/errno.h>
26#include <linux/gfp.h>
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +080027#include <linux/kernel.h>
Will Deacon9031fef2012-03-05 11:49:31 +000028#include <linux/mm.h>
29#include <linux/sched.h>
30#include <linux/signal.h>
31#include <linux/slab.h>
Catalin Marinasc60b0c22012-10-16 11:44:53 +010032#include <linux/timekeeper_internal.h>
Will Deacon9031fef2012-03-05 11:49:31 +000033#include <linux/vmalloc.h>
34
35#include <asm/cacheflush.h>
36#include <asm/signal32.h>
37#include <asm/vdso.h>
38#include <asm/vdso_datapage.h>
39
Kees Cook830d3a72019-07-05 20:47:20 +020040extern char vdso_start[], vdso_end[];
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +080041static unsigned long vdso_pages __ro_after_init;
Will Deacon9031fef2012-03-05 11:49:31 +000042
43/*
44 * The vDSO data page.
45 */
46static union {
47 struct vdso_data data;
48 u8 page[PAGE_SIZE];
49} vdso_data_store __page_aligned_data;
50struct vdso_data *vdso_data = &vdso_data_store.data;
51
52#ifdef CONFIG_COMPAT
53/*
54 * Create and map the vectors page for AArch32 tasks.
55 */
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +080056static struct page *vectors_page[1] __ro_after_init;
Will Deacon9031fef2012-03-05 11:49:31 +000057
Jisheng Zhang1aed28f2016-08-15 14:45:44 +080058static int __init alloc_vectors_page(void)
Will Deacon9031fef2012-03-05 11:49:31 +000059{
60 extern char __kuser_helper_start[], __kuser_helper_end[];
Matthew Leacha1d5eba2013-10-11 14:52:14 +010061 extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
62
Will Deacon9031fef2012-03-05 11:49:31 +000063 int kuser_sz = __kuser_helper_end - __kuser_helper_start;
Matthew Leacha1d5eba2013-10-11 14:52:14 +010064 int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
Will Deacon9031fef2012-03-05 11:49:31 +000065 unsigned long vpage;
66
67 vpage = get_zeroed_page(GFP_ATOMIC);
68
69 if (!vpage)
70 return -ENOMEM;
71
72 /* kuser helpers */
73 memcpy((void *)vpage + 0x1000 - kuser_sz, __kuser_helper_start,
74 kuser_sz);
75
76 /* sigreturn code */
77 memcpy((void *)vpage + AARCH32_KERN_SIGRET_CODE_OFFSET,
Matthew Leacha1d5eba2013-10-11 14:52:14 +010078 __aarch32_sigret_code_start, sigret_sz);
Will Deacon9031fef2012-03-05 11:49:31 +000079
80 flush_icache_range(vpage, vpage + PAGE_SIZE);
81 vectors_page[0] = virt_to_page(vpage);
82
83 return 0;
84}
85arch_initcall(alloc_vectors_page);
86
87int aarch32_setup_vectors_page(struct linux_binprm *bprm, int uses_interp)
88{
89 struct mm_struct *mm = current->mm;
90 unsigned long addr = AARCH32_VECTORS_BASE;
Jisheng Zhangb6d081b2016-08-15 14:45:45 +080091 static const struct vm_special_mapping spec = {
Will Deacon2fea7f62014-07-09 19:22:12 +010092 .name = "[vectors]",
93 .pages = vectors_page,
94
95 };
96 void *ret;
Will Deacon9031fef2012-03-05 11:49:31 +000097
Michal Hocko69048172016-05-23 16:25:54 -070098 if (down_write_killable(&mm->mmap_sem))
99 return -EINTR;
Will Deacon9031fef2012-03-05 11:49:31 +0000100 current->mm->context.vdso = (void *)addr;
101
102 /* Map vectors page at the high address. */
Will Deacon2fea7f62014-07-09 19:22:12 +0100103 ret = _install_special_mapping(mm, addr, PAGE_SIZE,
104 VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC,
105 &spec);
Will Deacon9031fef2012-03-05 11:49:31 +0000106
107 up_write(&mm->mmap_sem);
108
Will Deacon2fea7f62014-07-09 19:22:12 +0100109 return PTR_ERR_OR_ZERO(ret);
Will Deacon9031fef2012-03-05 11:49:31 +0000110}
111#endif /* CONFIG_COMPAT */
112
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +0800113static struct vm_special_mapping vdso_spec[2] __ro_after_init = {
114 {
115 .name = "[vvar]",
116 },
117 {
118 .name = "[vdso]",
119 },
120};
Will Deacon2fea7f62014-07-09 19:22:12 +0100121
Will Deacon9031fef2012-03-05 11:49:31 +0000122static int __init vdso_init(void)
123{
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000124 int i;
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +0800125 struct page **vdso_pagelist;
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000126
Kees Cook830d3a72019-07-05 20:47:20 +0200127 if (memcmp(vdso_start, "\177ELF", 4)) {
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000128 pr_err("vDSO is not a valid ELF object!\n");
129 return -EINVAL;
130 }
Will Deacon9031fef2012-03-05 11:49:31 +0000131
Kees Cook830d3a72019-07-05 20:47:20 +0200132 vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
Will Deacon601255a2014-07-09 19:22:13 +0100133 pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n",
Kees Cook830d3a72019-07-05 20:47:20 +0200134 vdso_pages + 1, vdso_pages, vdso_start, 1L, vdso_data);
Will Deacon9031fef2012-03-05 11:49:31 +0000135
136 /* Allocate the vDSO pagelist, plus a page for the data. */
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000137 vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *),
Will Deacon9031fef2012-03-05 11:49:31 +0000138 GFP_KERNEL);
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000139 if (vdso_pagelist == NULL)
Will Deacon9031fef2012-03-05 11:49:31 +0000140 return -ENOMEM;
Will Deacon9031fef2012-03-05 11:49:31 +0000141
Will Deacon601255a2014-07-09 19:22:13 +0100142 /* Grab the vDSO data page. */
Ard Biesheuvel97bbb542016-03-30 16:45:56 +0200143 vdso_pagelist[0] = pfn_to_page(PHYS_PFN(__pa(vdso_data)));
Will Deacon601255a2014-07-09 19:22:13 +0100144
Will Deacon9031fef2012-03-05 11:49:31 +0000145 /* Grab the vDSO code pages. */
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000146 for (i = 0; i < vdso_pages; i++)
Kees Cook830d3a72019-07-05 20:47:20 +0200147 vdso_pagelist[i + 1] = pfn_to_page(PHYS_PFN(__pa(vdso_start)) + i);
Will Deacon9031fef2012-03-05 11:49:31 +0000148
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +0800149 vdso_spec[0].pages = &vdso_pagelist[0];
150 vdso_spec[1].pages = &vdso_pagelist[1];
Will Deacon2fea7f62014-07-09 19:22:12 +0100151
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000152 return 0;
Will Deacon9031fef2012-03-05 11:49:31 +0000153}
154arch_initcall(vdso_init);
155
156int arch_setup_additional_pages(struct linux_binprm *bprm,
157 int uses_interp)
158{
159 struct mm_struct *mm = current->mm;
Will Deacon87154932014-07-09 19:22:11 +0100160 unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
Will Deacon2fea7f62014-07-09 19:22:12 +0100161 void *ret;
Will Deacon9031fef2012-03-05 11:49:31 +0000162
Will Deacon87154932014-07-09 19:22:11 +0100163 vdso_text_len = vdso_pages << PAGE_SHIFT;
Will Deacon9031fef2012-03-05 11:49:31 +0000164 /* Be sure to map the data page */
Will Deacon87154932014-07-09 19:22:11 +0100165 vdso_mapping_len = vdso_text_len + PAGE_SIZE;
Will Deacon9031fef2012-03-05 11:49:31 +0000166
Michal Hocko69048172016-05-23 16:25:54 -0700167 if (down_write_killable(&mm->mmap_sem))
168 return -EINTR;
Will Deacon9031fef2012-03-05 11:49:31 +0000169 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
170 if (IS_ERR_VALUE(vdso_base)) {
Will Deacon2fea7f62014-07-09 19:22:12 +0100171 ret = ERR_PTR(vdso_base);
Will Deacon9031fef2012-03-05 11:49:31 +0000172 goto up_fail;
173 }
Will Deacon601255a2014-07-09 19:22:13 +0100174 ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE,
175 VM_READ|VM_MAYREAD,
Will Deacon2fea7f62014-07-09 19:22:12 +0100176 &vdso_spec[0]);
177 if (IS_ERR(ret))
Will Deacon9031fef2012-03-05 11:49:31 +0000178 goto up_fail;
Will Deacon87154932014-07-09 19:22:11 +0100179
Will Deacon601255a2014-07-09 19:22:13 +0100180 vdso_base += PAGE_SIZE;
181 mm->context.vdso = (void *)vdso_base;
182 ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
183 VM_READ|VM_EXEC|
184 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
Will Deacon2fea7f62014-07-09 19:22:12 +0100185 &vdso_spec[1]);
186 if (IS_ERR(ret))
Will Deacon87154932014-07-09 19:22:11 +0100187 goto up_fail;
188
Will Deacon601255a2014-07-09 19:22:13 +0100189
Will Deacon87154932014-07-09 19:22:11 +0100190 up_write(&mm->mmap_sem);
191 return 0;
Will Deacon9031fef2012-03-05 11:49:31 +0000192
193up_fail:
Will Deacon87154932014-07-09 19:22:11 +0100194 mm->context.vdso = NULL;
Will Deacon9031fef2012-03-05 11:49:31 +0000195 up_write(&mm->mmap_sem);
Will Deacon2fea7f62014-07-09 19:22:12 +0100196 return PTR_ERR(ret);
Will Deacon9031fef2012-03-05 11:49:31 +0000197}
198
199/*
Will Deacon9031fef2012-03-05 11:49:31 +0000200 * Update the vDSO data page to keep in sync with kernel timekeeping.
201 */
Catalin Marinasc60b0c22012-10-16 11:44:53 +0100202void update_vsyscall(struct timekeeper *tk)
Will Deacon9031fef2012-03-05 11:49:31 +0000203{
Scott Wood1d8f51d2016-09-22 03:35:18 -0500204 u32 use_syscall = !tk->tkr_mono.clock->archdata.vdso_direct;
Will Deacon9031fef2012-03-05 11:49:31 +0000205
206 ++vdso_data->tb_seq_count;
207 smp_wmb();
208
Will Deacon9031fef2012-03-05 11:49:31 +0000209 vdso_data->use_syscall = use_syscall;
Nathan Lynch878854a2015-08-07 21:03:23 -0500210 vdso_data->xtime_coarse_sec = tk->xtime_sec;
211 vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >>
212 tk->tkr_mono.shift;
Nathan Lynchd4022a32014-02-03 19:48:52 +0000213 vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec;
214 vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;
Will Deacon9031fef2012-03-05 11:49:31 +0000215
Vincenzo Frascino9f641ee2019-04-16 17:14:30 +0100216 /* Read without the seqlock held by clock_getres() */
217 WRITE_ONCE(vdso_data->hrtimer_res, hrtimer_resolution);
218
Will Deacon9031fef2012-03-05 11:49:31 +0000219 if (!use_syscall) {
Kevin Brodsky49eea432016-07-12 11:24:00 +0100220 /* tkr_mono.cycle_last == tkr_raw.cycle_last */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100221 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
Kevin Brodsky49eea432016-07-12 11:24:00 +0100222 vdso_data->raw_time_sec = tk->raw_time.tv_sec;
Will Deacon99f66b52017-06-08 16:44:22 -0700223 vdso_data->raw_time_nsec = (tk->raw_time.tv_nsec <<
224 tk->tkr_raw.shift) +
225 tk->tkr_raw.xtime_nsec;
Catalin Marinasc60b0c22012-10-16 11:44:53 +0100226 vdso_data->xtime_clock_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100227 vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
Kevin Brodsky49eea432016-07-12 11:24:00 +0100228 vdso_data->cs_mono_mult = tk->tkr_mono.mult;
229 vdso_data->cs_raw_mult = tk->tkr_raw.mult;
230 /* tkr_mono.shift == tkr_raw.shift */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100231 vdso_data->cs_shift = tk->tkr_mono.shift;
Will Deacon9031fef2012-03-05 11:49:31 +0000232 }
233
234 smp_wmb();
235 ++vdso_data->tb_seq_count;
236}
237
238void update_vsyscall_tz(void)
239{
Will Deacon9031fef2012-03-05 11:49:31 +0000240 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
241 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
Will Deacon9031fef2012-03-05 11:49:31 +0000242}