Merge branch 'for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[linux-2.6-block.git] / arch / arm64 / kernel / vdso.c
CommitLineData
9031fefd
WD
1/*
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
5a9e3e15 21#include <linux/cache.h>
9031fefd
WD
22#include <linux/clocksource.h>
23#include <linux/elf.h>
24#include <linux/err.h>
25#include <linux/errno.h>
26#include <linux/gfp.h>
5a9e3e15 27#include <linux/kernel.h>
9031fefd
WD
28#include <linux/mm.h>
29#include <linux/sched.h>
30#include <linux/signal.h>
31#include <linux/slab.h>
c60b0c28 32#include <linux/timekeeper_internal.h>
9031fefd
WD
33#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
dbbb08f5 40extern char vdso_start[], vdso_end[];
5a9e3e15 41static unsigned long vdso_pages __ro_after_init;
9031fefd
WD
42
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 */
5a9e3e15 56static struct page *vectors_page[1] __ro_after_init;
9031fefd 57
1aed28f9 58static int __init alloc_vectors_page(void)
9031fefd
WD
59{
60 extern char __kuser_helper_start[], __kuser_helper_end[];
a1d5ebaf
ML
61 extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
62
9031fefd 63 int kuser_sz = __kuser_helper_end - __kuser_helper_start;
a1d5ebaf 64 int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
9031fefd
WD
65 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,
a1d5ebaf 78 __aarch32_sigret_code_start, sigret_sz);
9031fefd
WD
79
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;
b6d081bd 91 static const struct vm_special_mapping spec = {
2fea7f6c
WD
92 .name = "[vectors]",
93 .pages = vectors_page,
94
95 };
96 void *ret;
9031fefd 97
69048176
MH
98 if (down_write_killable(&mm->mmap_sem))
99 return -EINTR;
9031fefd
WD
100 current->mm->context.vdso = (void *)addr;
101
102 /* Map vectors page at the high address. */
2fea7f6c
WD
103 ret = _install_special_mapping(mm, addr, PAGE_SIZE,
104 VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC,
105 &spec);
9031fefd
WD
106
107 up_write(&mm->mmap_sem);
108
2fea7f6c 109 return PTR_ERR_OR_ZERO(ret);
9031fefd
WD
110}
111#endif /* CONFIG_COMPAT */
112
73958695
DS
113static int vdso_mremap(const struct vm_special_mapping *sm,
114 struct vm_area_struct *new_vma)
115{
116 unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
117 unsigned long vdso_size = vdso_end - vdso_start;
118
119 if (vdso_size != new_size)
120 return -EINVAL;
121
122 current->mm->context.vdso = (void *)new_vma->vm_start;
123
124 return 0;
125}
126
5a9e3e15
JZ
127static struct vm_special_mapping vdso_spec[2] __ro_after_init = {
128 {
129 .name = "[vvar]",
130 },
131 {
132 .name = "[vdso]",
73958695 133 .mremap = vdso_mremap,
5a9e3e15
JZ
134 },
135};
2fea7f6c 136
9031fefd
WD
137static int __init vdso_init(void)
138{
16fb1a9b 139 int i;
5a9e3e15 140 struct page **vdso_pagelist;
2077be67 141 unsigned long pfn;
16fb1a9b 142
dbbb08f5 143 if (memcmp(vdso_start, "\177ELF", 4)) {
16fb1a9b
NL
144 pr_err("vDSO is not a valid ELF object!\n");
145 return -EINVAL;
146 }
9031fefd 147
dbbb08f5 148 vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
601255ae 149 pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n",
dbbb08f5 150 vdso_pages + 1, vdso_pages, vdso_start, 1L, vdso_data);
9031fefd
WD
151
152 /* Allocate the vDSO pagelist, plus a page for the data. */
16fb1a9b 153 vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *),
9031fefd 154 GFP_KERNEL);
16fb1a9b 155 if (vdso_pagelist == NULL)
9031fefd 156 return -ENOMEM;
9031fefd 157
601255ae 158 /* Grab the vDSO data page. */
2077be67
LA
159 vdso_pagelist[0] = phys_to_page(__pa_symbol(vdso_data));
160
601255ae 161
9031fefd 162 /* Grab the vDSO code pages. */
dbbb08f5 163 pfn = sym_to_pfn(vdso_start);
2077be67 164
16fb1a9b 165 for (i = 0; i < vdso_pages; i++)
2077be67 166 vdso_pagelist[i + 1] = pfn_to_page(pfn + i);
9031fefd 167
5a9e3e15
JZ
168 vdso_spec[0].pages = &vdso_pagelist[0];
169 vdso_spec[1].pages = &vdso_pagelist[1];
2fea7f6c 170
16fb1a9b 171 return 0;
9031fefd
WD
172}
173arch_initcall(vdso_init);
174
175int arch_setup_additional_pages(struct linux_binprm *bprm,
176 int uses_interp)
177{
178 struct mm_struct *mm = current->mm;
87154938 179 unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
2fea7f6c 180 void *ret;
9031fefd 181
87154938 182 vdso_text_len = vdso_pages << PAGE_SHIFT;
9031fefd 183 /* Be sure to map the data page */
87154938 184 vdso_mapping_len = vdso_text_len + PAGE_SIZE;
9031fefd 185
69048176
MH
186 if (down_write_killable(&mm->mmap_sem))
187 return -EINTR;
9031fefd
WD
188 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
189 if (IS_ERR_VALUE(vdso_base)) {
2fea7f6c 190 ret = ERR_PTR(vdso_base);
9031fefd
WD
191 goto up_fail;
192 }
601255ae
WD
193 ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE,
194 VM_READ|VM_MAYREAD,
2fea7f6c
WD
195 &vdso_spec[0]);
196 if (IS_ERR(ret))
87154938
WD
197 goto up_fail;
198
601255ae
WD
199 vdso_base += PAGE_SIZE;
200 mm->context.vdso = (void *)vdso_base;
201 ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
202 VM_READ|VM_EXEC|
203 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
2fea7f6c
WD
204 &vdso_spec[1]);
205 if (IS_ERR(ret))
9031fefd 206 goto up_fail;
9031fefd 207
601255ae 208
9031fefd 209 up_write(&mm->mmap_sem);
87154938 210 return 0;
9031fefd 211
87154938
WD
212up_fail:
213 mm->context.vdso = NULL;
214 up_write(&mm->mmap_sem);
2fea7f6c 215 return PTR_ERR(ret);
9031fefd
WD
216}
217
9031fefd
WD
218/*
219 * Update the vDSO data page to keep in sync with kernel timekeeping.
220 */
c60b0c28 221void update_vsyscall(struct timekeeper *tk)
9031fefd 222{
1d8f51d4 223 u32 use_syscall = !tk->tkr_mono.clock->archdata.vdso_direct;
9031fefd
WD
224
225 ++vdso_data->tb_seq_count;
226 smp_wmb();
227
9031fefd 228 vdso_data->use_syscall = use_syscall;
878854a3
NL
229 vdso_data->xtime_coarse_sec = tk->xtime_sec;
230 vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >>
231 tk->tkr_mono.shift;
d4022a33
NL
232 vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec;
233 vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;
9031fefd
WD
234
235 if (!use_syscall) {
49eea433 236 /* tkr_mono.cycle_last == tkr_raw.cycle_last */
876e7881 237 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
fc6eead7
JS
238 vdso_data->raw_time_sec = tk->raw_sec;
239 vdso_data->raw_time_nsec = tk->tkr_raw.xtime_nsec;
c60b0c28 240 vdso_data->xtime_clock_sec = tk->xtime_sec;
876e7881 241 vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
49eea433
KB
242 vdso_data->cs_mono_mult = tk->tkr_mono.mult;
243 vdso_data->cs_raw_mult = tk->tkr_raw.mult;
244 /* tkr_mono.shift == tkr_raw.shift */
876e7881 245 vdso_data->cs_shift = tk->tkr_mono.shift;
9031fefd
WD
246 }
247
248 smp_wmb();
249 ++vdso_data->tb_seq_count;
250}
251
252void update_vsyscall_tz(void)
253{
9031fefd
WD
254 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
255 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
9031fefd 256}