FROMLIST: [PATCH v5 10/12] arm64: vdso: replace gettimeofday.S with global vgettimeof...
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / arch / arm64 / kernel / vdso.c
1 /*
2 * Additional userspace pages setup for AArch64 and AArch32.
3 * - AArch64: vDSO pages setup, vDSO data page update.
4 * - AArch32: sigreturn and kuser helpers pages setup.
5 *
6 * Copyright (C) 2012 ARM Limited
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Author: Will Deacon <will.deacon@arm.com>
21 */
22
23 #include <linux/kernel.h>
24 #include <linux/clocksource.h>
25 #include <linux/elf.h>
26 #include <linux/err.h>
27 #include <linux/errno.h>
28 #include <linux/gfp.h>
29 #include <linux/mm.h>
30 #include <linux/sched.h>
31 #include <linux/signal.h>
32 #include <linux/slab.h>
33 #include <linux/timekeeper_internal.h>
34 #include <linux/vmalloc.h>
35
36 #include <asm/cacheflush.h>
37 #include <asm/signal32.h>
38 #include <asm/vdso.h>
39 #include <asm/vdso_datapage.h>
40
41 extern char vdso_start, vdso_end;
42 static unsigned long vdso_pages;
43 static struct page **vdso_pagelist;
44
45 /*
46 * The vDSO data page.
47 */
48 static union {
49 struct vdso_data data;
50 u8 page[PAGE_SIZE];
51 } vdso_data_store __page_aligned_data;
52 struct vdso_data *vdso_data = &vdso_data_store.data;
53
54 #ifdef CONFIG_COMPAT
55 /*
56 * Create and map the vectors page for AArch32 tasks.
57 */
58 static struct page *vectors_page[] __ro_after_init;
59 static const struct vm_special_mapping compat_vdso_spec[] = {
60 {
61 /* Must be named [sigpage] for compatibility with arm. */
62 .name = "[sigpage]",
63 .pages = &vectors_page[0],
64 },
65 #ifdef CONFIG_KUSER_HELPERS
66 {
67 .name = "[kuserhelpers]",
68 .pages = &vectors_page[1],
69 },
70 #endif
71 };
72 static struct page *vectors_page[ARRAY_SIZE(compat_vdso_spec)] __ro_after_init;
73
74 static int __init alloc_vectors_page(void)
75 {
76 #ifdef CONFIG_KUSER_HELPERS
77 extern char __kuser_helper_start[], __kuser_helper_end[];
78 size_t kuser_sz = __kuser_helper_end - __kuser_helper_start;
79 unsigned long kuser_vpage;
80 #endif
81
82 extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
83 size_t sigret_sz =
84 __aarch32_sigret_code_end - __aarch32_sigret_code_start;
85 unsigned long sigret_vpage;
86
87 sigret_vpage = get_zeroed_page(GFP_ATOMIC);
88 if (!sigret_vpage)
89 return -ENOMEM;
90
91 #ifdef CONFIG_KUSER_HELPERS
92 kuser_vpage = get_zeroed_page(GFP_ATOMIC);
93 if (!kuser_vpage) {
94 free_page(sigret_vpage);
95 return -ENOMEM;
96 }
97 #endif
98
99 /* sigreturn code */
100 memcpy((void *)sigret_vpage, __aarch32_sigret_code_start, sigret_sz);
101 flush_icache_range(sigret_vpage, sigret_vpage + PAGE_SIZE);
102 vectors_page[0] = virt_to_page(sigret_vpage);
103
104 #ifdef CONFIG_KUSER_HELPERS
105 /* kuser helpers */
106 memcpy((void *)kuser_vpage + 0x1000 - kuser_sz, __kuser_helper_start,
107 kuser_sz);
108 flush_icache_range(kuser_vpage, kuser_vpage + PAGE_SIZE);
109 vectors_page[1] = virt_to_page(kuser_vpage);
110 #endif
111
112 return 0;
113 }
114 arch_initcall(alloc_vectors_page);
115
116 int aarch32_setup_vectors_page(struct linux_binprm *bprm, int uses_interp)
117 {
118 struct mm_struct *mm = current->mm;
119 unsigned long addr;
120 void *ret;
121
122 down_write(&mm->mmap_sem);
123 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
124 if (IS_ERR_VALUE(addr)) {
125 ret = ERR_PTR(addr);
126 goto out;
127 }
128
129 ret = _install_special_mapping(mm, addr, PAGE_SIZE,
130 VM_READ|VM_EXEC|
131 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
132 &compat_vdso_spec[0]);
133 if (IS_ERR(ret))
134 goto out;
135
136 current->mm->context.vdso = (void *)addr;
137
138 #ifdef CONFIG_KUSER_HELPERS
139 /* Map the kuser helpers at the ABI-defined high address. */
140 ret = _install_special_mapping(mm, AARCH32_KUSER_HELPERS_BASE,
141 PAGE_SIZE,
142 VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC,
143 &compat_vdso_spec[1]);
144 #endif
145 out:
146 up_write(&mm->mmap_sem);
147
148 return PTR_ERR_OR_ZERO(ret);
149 }
150 #endif /* CONFIG_COMPAT */
151
152 static struct vm_special_mapping vdso_spec[2];
153
154 static int __init vdso_init(void)
155 {
156 int i;
157 unsigned long pfn;
158
159 if (memcmp(&vdso_start, "\177ELF", 4)) {
160 pr_err("vDSO is not a valid ELF object!\n");
161 return -EINVAL;
162 }
163
164 vdso_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
165 pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n",
166 vdso_pages + 1, vdso_pages, &vdso_start, 1L, vdso_data);
167
168 /* Allocate the vDSO pagelist, plus a page for the data. */
169 vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *),
170 GFP_KERNEL);
171 if (vdso_pagelist == NULL)
172 return -ENOMEM;
173
174 /* Grab the vDSO data page. */
175 vdso_pagelist[0] = phys_to_page(__pa_symbol(vdso_data));
176
177
178 /* Grab the vDSO code pages. */
179 pfn = sym_to_pfn(&vdso_start);
180
181 for (i = 0; i < vdso_pages; i++)
182 vdso_pagelist[i + 1] = pfn_to_page(pfn + i);
183
184 /* Populate the special mapping structures */
185 vdso_spec[0] = (struct vm_special_mapping) {
186 .name = "[vvar]",
187 .pages = vdso_pagelist,
188 };
189
190 vdso_spec[1] = (struct vm_special_mapping) {
191 .name = "[vdso]",
192 .pages = &vdso_pagelist[1],
193 };
194
195 return 0;
196 }
197 arch_initcall(vdso_init);
198
199 int arch_setup_additional_pages(struct linux_binprm *bprm,
200 int uses_interp)
201 {
202 struct mm_struct *mm = current->mm;
203 unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
204 void *ret;
205
206 vdso_text_len = vdso_pages << PAGE_SHIFT;
207 /* Be sure to map the data page */
208 vdso_mapping_len = vdso_text_len + PAGE_SIZE;
209
210 down_write(&mm->mmap_sem);
211 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
212 if (IS_ERR_VALUE(vdso_base)) {
213 ret = ERR_PTR(vdso_base);
214 goto up_fail;
215 }
216 ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE,
217 VM_READ|VM_MAYREAD,
218 &vdso_spec[0]);
219 if (IS_ERR(ret))
220 goto up_fail;
221
222 vdso_base += PAGE_SIZE;
223 mm->context.vdso = (void *)vdso_base;
224 ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
225 VM_READ|VM_EXEC|
226 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
227 &vdso_spec[1]);
228 if (IS_ERR(ret))
229 goto up_fail;
230
231
232 up_write(&mm->mmap_sem);
233 return 0;
234
235 up_fail:
236 mm->context.vdso = NULL;
237 up_write(&mm->mmap_sem);
238 return PTR_ERR(ret);
239 }
240
241 /*
242 * Update the vDSO data page to keep in sync with kernel timekeeping.
243 */
244 void update_vsyscall(struct timekeeper *tk)
245 {
246 u32 use_syscall = strcmp(tk->tkr_mono.clock->name, "arch_sys_counter");
247
248 ++vdso_data->tb_seq_count;
249 smp_wmb();
250
251 vdso_data->use_syscall = use_syscall;
252 vdso_data->xtime_coarse_sec = tk->xtime_sec;
253 vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >>
254 tk->tkr_mono.shift;
255 vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec;
256 vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;
257
258 if (!use_syscall) {
259 /* tkr_mono.cycle_last == tkr_raw.cycle_last */
260 vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
261 vdso_data->raw_time_sec = tk->raw_sec;
262 vdso_data->raw_time_nsec = tk->tkr_raw.xtime_nsec;
263 vdso_data->xtime_clock_sec = tk->xtime_sec;
264 vdso_data->xtime_clock_snsec = tk->tkr_mono.xtime_nsec;
265 /* tkr_raw.xtime_nsec == 0 */
266 vdso_data->cs_mono_mult = tk->tkr_mono.mult;
267 vdso_data->cs_raw_mult = tk->tkr_raw.mult;
268 /* tkr_mono.shift == tkr_raw.shift */
269 vdso_data->cs_shift = tk->tkr_mono.shift;
270 }
271
272 smp_wmb();
273 ++vdso_data->tb_seq_count;
274 }
275
276 void update_vsyscall_tz(void)
277 {
278 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
279 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
280 }