blob: 84cafbc3eb5452ca621190c3cde0305852b2f315 [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
21#include <linux/kernel.h>
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>
27#include <linux/mm.h>
28#include <linux/sched.h>
29#include <linux/signal.h>
30#include <linux/slab.h>
Catalin Marinasc60b0c22012-10-16 11:44:53 +010031#include <linux/timekeeper_internal.h>
Will Deacon9031fef2012-03-05 11:49:31 +000032#include <linux/vmalloc.h>
33
34#include <asm/cacheflush.h>
35#include <asm/signal32.h>
36#include <asm/vdso.h>
37#include <asm/vdso_datapage.h>
38
39extern char vdso_start, vdso_end;
40static unsigned long vdso_pages;
41static struct page **vdso_pagelist;
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 */
56static struct page *vectors_page[1];
57
58static int alloc_vectors_page(void)
59{
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;
91 int ret;
92
93 down_write(&mm->mmap_sem);
94 current->mm->context.vdso = (void *)addr;
95
96 /* Map vectors page at the high address. */
97 ret = install_special_mapping(mm, addr, PAGE_SIZE,
98 VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC,
99 vectors_page);
100
101 up_write(&mm->mmap_sem);
102
103 return ret;
104}
105#endif /* CONFIG_COMPAT */
106
107static int __init vdso_init(void)
108{
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000109 int i;
110
111 if (memcmp(&vdso_start, "\177ELF", 4)) {
112 pr_err("vDSO is not a valid ELF object!\n");
113 return -EINVAL;
114 }
Will Deacon9031fef2012-03-05 11:49:31 +0000115
116 vdso_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
117 pr_info("vdso: %ld pages (%ld code, %ld data) at base %p\n",
118 vdso_pages + 1, vdso_pages, 1L, &vdso_start);
119
120 /* Allocate the vDSO pagelist, plus a page for the data. */
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000121 vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *),
Will Deacon9031fef2012-03-05 11:49:31 +0000122 GFP_KERNEL);
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000123 if (vdso_pagelist == NULL)
Will Deacon9031fef2012-03-05 11:49:31 +0000124 return -ENOMEM;
Will Deacon9031fef2012-03-05 11:49:31 +0000125
126 /* Grab the vDSO code pages. */
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000127 for (i = 0; i < vdso_pages; i++)
128 vdso_pagelist[i] = virt_to_page(&vdso_start + i * PAGE_SIZE);
Will Deacon9031fef2012-03-05 11:49:31 +0000129
130 /* Grab the vDSO data page. */
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000131 vdso_pagelist[i] = virt_to_page(vdso_data);
Will Deacon9031fef2012-03-05 11:49:31 +0000132
Nathan Lynch16fb1a92014-02-11 22:28:42 +0000133 return 0;
Will Deacon9031fef2012-03-05 11:49:31 +0000134}
135arch_initcall(vdso_init);
136
137int arch_setup_additional_pages(struct linux_binprm *bprm,
138 int uses_interp)
139{
140 struct mm_struct *mm = current->mm;
Will Deacon87154932014-07-09 19:22:11 +0100141 unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
Will Deacon9031fef2012-03-05 11:49:31 +0000142 int ret;
143
Will Deacon87154932014-07-09 19:22:11 +0100144 vdso_text_len = vdso_pages << PAGE_SHIFT;
Will Deacon9031fef2012-03-05 11:49:31 +0000145 /* Be sure to map the data page */
Will Deacon87154932014-07-09 19:22:11 +0100146 vdso_mapping_len = vdso_text_len + PAGE_SIZE;
Will Deacon9031fef2012-03-05 11:49:31 +0000147
148 down_write(&mm->mmap_sem);
149 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
150 if (IS_ERR_VALUE(vdso_base)) {
151 ret = vdso_base;
152 goto up_fail;
153 }
154 mm->context.vdso = (void *)vdso_base;
155
Will Deacon87154932014-07-09 19:22:11 +0100156 ret = install_special_mapping(mm, vdso_base, vdso_text_len,
Will Deacon9031fef2012-03-05 11:49:31 +0000157 VM_READ|VM_EXEC|
158 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
159 vdso_pagelist);
Will Deacon87154932014-07-09 19:22:11 +0100160 if (ret)
Will Deacon9031fef2012-03-05 11:49:31 +0000161 goto up_fail;
Will Deacon87154932014-07-09 19:22:11 +0100162
163 vdso_base += vdso_text_len;
164 ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
165 VM_READ|VM_MAYREAD,
166 vdso_pagelist + vdso_pages);
167 if (ret)
168 goto up_fail;
169
170 up_write(&mm->mmap_sem);
171 return 0;
Will Deacon9031fef2012-03-05 11:49:31 +0000172
173up_fail:
Will Deacon87154932014-07-09 19:22:11 +0100174 mm->context.vdso = NULL;
Will Deacon9031fef2012-03-05 11:49:31 +0000175 up_write(&mm->mmap_sem);
Will Deacon9031fef2012-03-05 11:49:31 +0000176 return ret;
177}
178
179const char *arch_vma_name(struct vm_area_struct *vma)
180{
Will Deacon87154932014-07-09 19:22:11 +0100181 unsigned long vdso_text;
182
183 if (!vma->vm_mm)
184 return NULL;
185
186 vdso_text = (unsigned long)vma->vm_mm->context.vdso;
187
Will Deacon9031fef2012-03-05 11:49:31 +0000188 /*
189 * We can re-use the vdso pointer in mm_context_t for identifying
190 * the vectors page for compat applications. The vDSO will always
191 * sit above TASK_UNMAPPED_BASE and so we don't need to worry about
192 * it conflicting with the vectors base.
193 */
Will Deacon87154932014-07-09 19:22:11 +0100194 if (vma->vm_start == vdso_text) {
Will Deacon9031fef2012-03-05 11:49:31 +0000195#ifdef CONFIG_COMPAT
196 if (vma->vm_start == AARCH32_VECTORS_BASE)
197 return "[vectors]";
198#endif
199 return "[vdso]";
Will Deacon87154932014-07-09 19:22:11 +0100200 } else if (vma->vm_start == (vdso_text + (vdso_pages << PAGE_SHIFT))) {
201 return "[vvar]";
Will Deacon9031fef2012-03-05 11:49:31 +0000202 }
203
204 return NULL;
205}
206
207/*
208 * We define AT_SYSINFO_EHDR, so we need these function stubs to keep
209 * Linux happy.
210 */
211int in_gate_area_no_mm(unsigned long addr)
212{
213 return 0;
214}
215
216int in_gate_area(struct mm_struct *mm, unsigned long addr)
217{
218 return 0;
219}
220
221struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
222{
223 return NULL;
224}
225
226/*
227 * Update the vDSO data page to keep in sync with kernel timekeeping.
228 */
Catalin Marinasc60b0c22012-10-16 11:44:53 +0100229void update_vsyscall(struct timekeeper *tk)
Will Deacon9031fef2012-03-05 11:49:31 +0000230{
231 struct timespec xtime_coarse;
Catalin Marinasc60b0c22012-10-16 11:44:53 +0100232 u32 use_syscall = strcmp(tk->clock->name, "arch_sys_counter");
Will Deacon9031fef2012-03-05 11:49:31 +0000233
234 ++vdso_data->tb_seq_count;
235 smp_wmb();
236
237 xtime_coarse = __current_kernel_time();
238 vdso_data->use_syscall = use_syscall;
239 vdso_data->xtime_coarse_sec = xtime_coarse.tv_sec;
240 vdso_data->xtime_coarse_nsec = xtime_coarse.tv_nsec;
Nathan Lynchd4022a32014-02-03 19:48:52 +0000241 vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec;
242 vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;
Will Deacon9031fef2012-03-05 11:49:31 +0000243
244 if (!use_syscall) {
Catalin Marinasc60b0c22012-10-16 11:44:53 +0100245 vdso_data->cs_cycle_last = tk->clock->cycle_last;
246 vdso_data->xtime_clock_sec = tk->xtime_sec;
Will Deacon45a79052012-11-29 22:33:29 +0000247 vdso_data->xtime_clock_nsec = tk->xtime_nsec;
Catalin Marinasc60b0c22012-10-16 11:44:53 +0100248 vdso_data->cs_mult = tk->mult;
249 vdso_data->cs_shift = tk->shift;
Will Deacon9031fef2012-03-05 11:49:31 +0000250 }
251
252 smp_wmb();
253 ++vdso_data->tb_seq_count;
254}
255
256void update_vsyscall_tz(void)
257{
Will Deacon9031fef2012-03-05 11:49:31 +0000258 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
259 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
Will Deacon9031fef2012-03-05 11:49:31 +0000260}