Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 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> |
Catalin Marinas | c60b0c2 | 2012-10-16 11:44:53 +0100 | [diff] [blame] | 31 | #include <linux/timekeeper_internal.h> |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 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[]; |
Matthew Leach | a1d5eba | 2013-10-11 14:52:14 +0100 | [diff] [blame] | 61 | extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[]; |
| 62 | |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 63 | int kuser_sz = __kuser_helper_end - __kuser_helper_start; |
Matthew Leach | a1d5eba | 2013-10-11 14:52:14 +0100 | [diff] [blame] | 64 | int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 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, |
Matthew Leach | a1d5eba | 2013-10-11 14:52:14 +0100 | [diff] [blame] | 78 | __aarch32_sigret_code_start, sigret_sz); |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 79 | |
| 80 | flush_icache_range(vpage, vpage + PAGE_SIZE); |
| 81 | vectors_page[0] = virt_to_page(vpage); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | arch_initcall(alloc_vectors_page); |
| 86 | |
| 87 | int 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; |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 91 | static struct vm_special_mapping spec = { |
| 92 | .name = "[vectors]", |
| 93 | .pages = vectors_page, |
| 94 | |
| 95 | }; |
| 96 | void *ret; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 97 | |
| 98 | down_write(&mm->mmap_sem); |
| 99 | current->mm->context.vdso = (void *)addr; |
| 100 | |
| 101 | /* Map vectors page at the high address. */ |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 102 | ret = _install_special_mapping(mm, addr, PAGE_SIZE, |
| 103 | VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC, |
| 104 | &spec); |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 105 | |
| 106 | up_write(&mm->mmap_sem); |
| 107 | |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 108 | return PTR_ERR_OR_ZERO(ret); |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 109 | } |
| 110 | #endif /* CONFIG_COMPAT */ |
| 111 | |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 112 | static struct vm_special_mapping vdso_spec[2]; |
| 113 | |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 114 | static int __init vdso_init(void) |
| 115 | { |
Nathan Lynch | 16fb1a9 | 2014-02-11 22:28:42 +0000 | [diff] [blame] | 116 | int i; |
| 117 | |
| 118 | if (memcmp(&vdso_start, "\177ELF", 4)) { |
| 119 | pr_err("vDSO is not a valid ELF object!\n"); |
| 120 | return -EINVAL; |
| 121 | } |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 122 | |
| 123 | vdso_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT; |
Will Deacon | 601255a | 2014-07-09 19:22:13 +0100 | [diff] [blame] | 124 | pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n", |
| 125 | vdso_pages + 1, vdso_pages, &vdso_start, 1L, vdso_data); |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 126 | |
| 127 | /* Allocate the vDSO pagelist, plus a page for the data. */ |
Nathan Lynch | 16fb1a9 | 2014-02-11 22:28:42 +0000 | [diff] [blame] | 128 | vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *), |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 129 | GFP_KERNEL); |
Nathan Lynch | 16fb1a9 | 2014-02-11 22:28:42 +0000 | [diff] [blame] | 130 | if (vdso_pagelist == NULL) |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 131 | return -ENOMEM; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 132 | |
Will Deacon | 601255a | 2014-07-09 19:22:13 +0100 | [diff] [blame] | 133 | /* Grab the vDSO data page. */ |
| 134 | vdso_pagelist[0] = virt_to_page(vdso_data); |
| 135 | |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 136 | /* Grab the vDSO code pages. */ |
Nathan Lynch | 16fb1a9 | 2014-02-11 22:28:42 +0000 | [diff] [blame] | 137 | for (i = 0; i < vdso_pages; i++) |
Will Deacon | 601255a | 2014-07-09 19:22:13 +0100 | [diff] [blame] | 138 | vdso_pagelist[i + 1] = virt_to_page(&vdso_start + i * PAGE_SIZE); |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 139 | |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 140 | /* Populate the special mapping structures */ |
| 141 | vdso_spec[0] = (struct vm_special_mapping) { |
Will Deacon | 601255a | 2014-07-09 19:22:13 +0100 | [diff] [blame] | 142 | .name = "[vvar]", |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 143 | .pages = vdso_pagelist, |
| 144 | }; |
| 145 | |
| 146 | vdso_spec[1] = (struct vm_special_mapping) { |
Will Deacon | 601255a | 2014-07-09 19:22:13 +0100 | [diff] [blame] | 147 | .name = "[vdso]", |
| 148 | .pages = &vdso_pagelist[1], |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 149 | }; |
| 150 | |
Nathan Lynch | 16fb1a9 | 2014-02-11 22:28:42 +0000 | [diff] [blame] | 151 | return 0; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 152 | } |
| 153 | arch_initcall(vdso_init); |
| 154 | |
| 155 | int arch_setup_additional_pages(struct linux_binprm *bprm, |
| 156 | int uses_interp) |
| 157 | { |
| 158 | struct mm_struct *mm = current->mm; |
Will Deacon | 8715493 | 2014-07-09 19:22:11 +0100 | [diff] [blame] | 159 | unsigned long vdso_base, vdso_text_len, vdso_mapping_len; |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 160 | void *ret; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 161 | |
Will Deacon | 8715493 | 2014-07-09 19:22:11 +0100 | [diff] [blame] | 162 | vdso_text_len = vdso_pages << PAGE_SHIFT; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 163 | /* Be sure to map the data page */ |
Will Deacon | 8715493 | 2014-07-09 19:22:11 +0100 | [diff] [blame] | 164 | vdso_mapping_len = vdso_text_len + PAGE_SIZE; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 165 | |
| 166 | down_write(&mm->mmap_sem); |
| 167 | vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0); |
| 168 | if (IS_ERR_VALUE(vdso_base)) { |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 169 | ret = ERR_PTR(vdso_base); |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 170 | goto up_fail; |
| 171 | } |
Will Deacon | 601255a | 2014-07-09 19:22:13 +0100 | [diff] [blame] | 172 | ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE, |
| 173 | VM_READ|VM_MAYREAD, |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 174 | &vdso_spec[0]); |
| 175 | if (IS_ERR(ret)) |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 176 | goto up_fail; |
Will Deacon | 8715493 | 2014-07-09 19:22:11 +0100 | [diff] [blame] | 177 | |
Will Deacon | 601255a | 2014-07-09 19:22:13 +0100 | [diff] [blame] | 178 | vdso_base += PAGE_SIZE; |
| 179 | mm->context.vdso = (void *)vdso_base; |
| 180 | ret = _install_special_mapping(mm, vdso_base, vdso_text_len, |
| 181 | VM_READ|VM_EXEC| |
| 182 | VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 183 | &vdso_spec[1]); |
| 184 | if (IS_ERR(ret)) |
Will Deacon | 8715493 | 2014-07-09 19:22:11 +0100 | [diff] [blame] | 185 | goto up_fail; |
| 186 | |
Will Deacon | 601255a | 2014-07-09 19:22:13 +0100 | [diff] [blame] | 187 | |
Will Deacon | 8715493 | 2014-07-09 19:22:11 +0100 | [diff] [blame] | 188 | up_write(&mm->mmap_sem); |
| 189 | return 0; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 190 | |
| 191 | up_fail: |
Will Deacon | 8715493 | 2014-07-09 19:22:11 +0100 | [diff] [blame] | 192 | mm->context.vdso = NULL; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 193 | up_write(&mm->mmap_sem); |
Will Deacon | 2fea7f6 | 2014-07-09 19:22:12 +0100 | [diff] [blame] | 194 | return PTR_ERR(ret); |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 198 | * Update the vDSO data page to keep in sync with kernel timekeeping. |
| 199 | */ |
Catalin Marinas | c60b0c2 | 2012-10-16 11:44:53 +0100 | [diff] [blame] | 200 | void update_vsyscall(struct timekeeper *tk) |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 201 | { |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 202 | u32 use_syscall = strcmp(tk->tkr_mono.clock->name, "arch_sys_counter"); |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 203 | |
| 204 | ++vdso_data->tb_seq_count; |
| 205 | smp_wmb(); |
| 206 | |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 207 | vdso_data->use_syscall = use_syscall; |
Nathan Lynch | 878854a | 2015-08-07 21:03:23 -0500 | [diff] [blame] | 208 | vdso_data->xtime_coarse_sec = tk->xtime_sec; |
| 209 | vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >> |
| 210 | tk->tkr_mono.shift; |
Nathan Lynch | d4022a3 | 2014-02-03 19:48:52 +0000 | [diff] [blame] | 211 | vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec; |
| 212 | vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 213 | |
| 214 | if (!use_syscall) { |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 215 | vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last; |
Catalin Marinas | c60b0c2 | 2012-10-16 11:44:53 +0100 | [diff] [blame] | 216 | vdso_data->xtime_clock_sec = tk->xtime_sec; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 217 | vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec; |
| 218 | vdso_data->cs_mult = tk->tkr_mono.mult; |
| 219 | vdso_data->cs_shift = tk->tkr_mono.shift; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | smp_wmb(); |
| 223 | ++vdso_data->tb_seq_count; |
| 224 | } |
| 225 | |
| 226 | void update_vsyscall_tz(void) |
| 227 | { |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 228 | vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; |
| 229 | vdso_data->tz_dsttime = sys_tz.tz_dsttime; |
Will Deacon | 9031fef | 2012-03-05 11:49:31 +0000 | [diff] [blame] | 230 | } |