arm64: vdso: clean up vdso_pagelist initialization
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / vdso.c
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
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>
31 #include <linux/timekeeper_internal.h>
32 #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
39 extern char vdso_start, vdso_end;
40 static unsigned long vdso_pages;
41 static struct page **vdso_pagelist;
42
43 /*
44  * The vDSO data page.
45  */
46 static union {
47         struct vdso_data        data;
48         u8                      page[PAGE_SIZE];
49 } vdso_data_store __page_aligned_data;
50 struct 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  */
56 static struct page *vectors_page[1];
57
58 static int alloc_vectors_page(void)
59 {
60         extern char __kuser_helper_start[], __kuser_helper_end[];
61         int kuser_sz = __kuser_helper_end - __kuser_helper_start;
62         unsigned long vpage;
63
64         vpage = get_zeroed_page(GFP_ATOMIC);
65
66         if (!vpage)
67                 return -ENOMEM;
68
69         /* kuser helpers */
70         memcpy((void *)vpage + 0x1000 - kuser_sz, __kuser_helper_start,
71                 kuser_sz);
72
73         /* sigreturn code */
74         memcpy((void *)vpage + AARCH32_KERN_SIGRET_CODE_OFFSET,
75                 aarch32_sigret_code, sizeof(aarch32_sigret_code));
76
77         flush_icache_range(vpage, vpage + PAGE_SIZE);
78         vectors_page[0] = virt_to_page(vpage);
79
80         return 0;
81 }
82 arch_initcall(alloc_vectors_page);
83
84 int aarch32_setup_vectors_page(struct linux_binprm *bprm, int uses_interp)
85 {
86         struct mm_struct *mm = current->mm;
87         unsigned long addr = AARCH32_VECTORS_BASE;
88         int ret;
89
90         down_write(&mm->mmap_sem);
91         current->mm->context.vdso = (void *)addr;
92
93         /* Map vectors page at the high address. */
94         ret = install_special_mapping(mm, addr, PAGE_SIZE,
95                                       VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC,
96                                       vectors_page);
97
98         up_write(&mm->mmap_sem);
99
100         return ret;
101 }
102 #endif /* CONFIG_COMPAT */
103
104 static int __init vdso_init(void)
105 {
106         int i;
107
108         if (memcmp(&vdso_start, "\177ELF", 4)) {
109                 pr_err("vDSO is not a valid ELF object!\n");
110                 return -EINVAL;
111         }
112
113         vdso_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
114         pr_info("vdso: %ld pages (%ld code, %ld data) at base %p\n",
115                 vdso_pages + 1, vdso_pages, 1L, &vdso_start);
116
117         /* Allocate the vDSO pagelist, plus a page for the data. */
118         vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *),
119                                 GFP_KERNEL);
120         if (vdso_pagelist == NULL)
121                 return -ENOMEM;
122
123         /* Grab the vDSO code pages. */
124         for (i = 0; i < vdso_pages; i++)
125                 vdso_pagelist[i] = virt_to_page(&vdso_start + i * PAGE_SIZE);
126
127         /* Grab the vDSO data page. */
128         vdso_pagelist[i] = virt_to_page(vdso_data);
129
130         return 0;
131 }
132 arch_initcall(vdso_init);
133
134 int arch_setup_additional_pages(struct linux_binprm *bprm,
135                                 int uses_interp)
136 {
137         struct mm_struct *mm = current->mm;
138         unsigned long vdso_base, vdso_mapping_len;
139         int ret;
140
141         /* Be sure to map the data page */
142         vdso_mapping_len = (vdso_pages + 1) << PAGE_SHIFT;
143
144         down_write(&mm->mmap_sem);
145         vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
146         if (IS_ERR_VALUE(vdso_base)) {
147                 ret = vdso_base;
148                 goto up_fail;
149         }
150         mm->context.vdso = (void *)vdso_base;
151
152         ret = install_special_mapping(mm, vdso_base, vdso_mapping_len,
153                                       VM_READ|VM_EXEC|
154                                       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
155                                       vdso_pagelist);
156         if (ret) {
157                 mm->context.vdso = NULL;
158                 goto up_fail;
159         }
160
161 up_fail:
162         up_write(&mm->mmap_sem);
163
164         return ret;
165 }
166
167 const char *arch_vma_name(struct vm_area_struct *vma)
168 {
169         /*
170          * We can re-use the vdso pointer in mm_context_t for identifying
171          * the vectors page for compat applications. The vDSO will always
172          * sit above TASK_UNMAPPED_BASE and so we don't need to worry about
173          * it conflicting with the vectors base.
174          */
175         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) {
176 #ifdef CONFIG_COMPAT
177                 if (vma->vm_start == AARCH32_VECTORS_BASE)
178                         return "[vectors]";
179 #endif
180                 return "[vdso]";
181         }
182
183         return NULL;
184 }
185
186 /*
187  * We define AT_SYSINFO_EHDR, so we need these function stubs to keep
188  * Linux happy.
189  */
190 int in_gate_area_no_mm(unsigned long addr)
191 {
192         return 0;
193 }
194
195 int in_gate_area(struct mm_struct *mm, unsigned long addr)
196 {
197         return 0;
198 }
199
200 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
201 {
202         return NULL;
203 }
204
205 /*
206  * Update the vDSO data page to keep in sync with kernel timekeeping.
207  */
208 void update_vsyscall(struct timekeeper *tk)
209 {
210         struct timespec xtime_coarse;
211         u32 use_syscall = strcmp(tk->clock->name, "arch_sys_counter");
212
213         ++vdso_data->tb_seq_count;
214         smp_wmb();
215
216         xtime_coarse = __current_kernel_time();
217         vdso_data->use_syscall                  = use_syscall;
218         vdso_data->xtime_coarse_sec             = xtime_coarse.tv_sec;
219         vdso_data->xtime_coarse_nsec            = xtime_coarse.tv_nsec;
220         vdso_data->wtm_clock_sec                = tk->wall_to_monotonic.tv_sec;
221         vdso_data->wtm_clock_nsec               = tk->wall_to_monotonic.tv_nsec;
222
223         if (!use_syscall) {
224                 vdso_data->cs_cycle_last        = tk->clock->cycle_last;
225                 vdso_data->xtime_clock_sec      = tk->xtime_sec;
226                 vdso_data->xtime_clock_nsec     = tk->xtime_nsec;
227                 vdso_data->cs_mult              = tk->mult;
228                 vdso_data->cs_shift             = tk->shift;
229         }
230
231         smp_wmb();
232         ++vdso_data->tb_seq_count;
233 }
234
235 void update_vsyscall_tz(void)
236 {
237         vdso_data->tz_minuteswest       = sys_tz.tz_minuteswest;
238         vdso_data->tz_dsttime           = sys_tz.tz_dsttime;
239 }