1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * VDSO implementations. 4 * 5 * Copyright (C) 2012 ARM Limited 6 * 7 * Author: Will Deacon <[email protected]> 8 */ 9 10 #include <linux/cache.h> 11 #include <linux/clocksource.h> 12 #include <linux/elf.h> 13 #include <linux/err.h> 14 #include <linux/errno.h> 15 #include <linux/gfp.h> 16 #include <linux/kernel.h> 17 #include <linux/mm.h> 18 #include <linux/sched.h> 19 #include <linux/signal.h> 20 #include <linux/slab.h> 21 #include <linux/timekeeper_internal.h> 22 #include <linux/vmalloc.h> 23 #include <vdso/datapage.h> 24 #include <vdso/helpers.h> 25 #include <vdso/vsyscall.h> 26 27 #include <asm/cacheflush.h> 28 #include <asm/signal32.h> 29 #include <asm/vdso.h> 30 31 extern char vdso_start[], vdso_end[]; 32 #ifdef CONFIG_COMPAT_VDSO 33 extern char vdso32_start[], vdso32_end[]; 34 #endif /* CONFIG_COMPAT_VDSO */ 35 36 enum vdso_abi { 37 VDSO_ABI_AA64, 38 #ifdef CONFIG_COMPAT_VDSO 39 VDSO_ABI_AA32, 40 #endif /* CONFIG_COMPAT_VDSO */ 41 }; 42 43 struct vdso_abi_info { 44 const char *name; 45 const char *vdso_code_start; 46 const char *vdso_code_end; 47 unsigned long vdso_pages; 48 /* Data Mapping */ 49 struct vm_special_mapping *dm; 50 /* Code Mapping */ 51 struct vm_special_mapping *cm; 52 }; 53 54 static struct vdso_abi_info vdso_info[] __ro_after_init = { 55 [VDSO_ABI_AA64] = { 56 .name = "vdso", 57 .vdso_code_start = vdso_start, 58 .vdso_code_end = vdso_end, 59 }, 60 #ifdef CONFIG_COMPAT_VDSO 61 [VDSO_ABI_AA32] = { 62 .name = "vdso32", 63 .vdso_code_start = vdso32_start, 64 .vdso_code_end = vdso32_end, 65 }, 66 #endif /* CONFIG_COMPAT_VDSO */ 67 }; 68 69 /* 70 * The vDSO data page. 71 */ 72 static union { 73 struct vdso_data data[CS_BASES]; 74 u8 page[PAGE_SIZE]; 75 } vdso_data_store __page_aligned_data; 76 struct vdso_data *vdso_data = vdso_data_store.data; 77 78 static int __vdso_remap(enum vdso_abi abi, 79 const struct vm_special_mapping *sm, 80 struct vm_area_struct *new_vma) 81 { 82 unsigned long new_size = new_vma->vm_end - new_vma->vm_start; 83 unsigned long vdso_size = vdso_info[abi].vdso_code_end - 84 vdso_info[abi].vdso_code_start; 85 86 if (vdso_size != new_size) 87 return -EINVAL; 88 89 current->mm->context.vdso = (void *)new_vma->vm_start; 90 91 return 0; 92 } 93 94 static int __vdso_init(enum vdso_abi abi) 95 { 96 int i; 97 struct page **vdso_pagelist; 98 unsigned long pfn; 99 100 if (memcmp(vdso_info[abi].vdso_code_start, "\177ELF", 4)) { 101 pr_err("vDSO is not a valid ELF object!\n"); 102 return -EINVAL; 103 } 104 105 vdso_info[abi].vdso_pages = ( 106 vdso_info[abi].vdso_code_end - 107 vdso_info[abi].vdso_code_start) >> 108 PAGE_SHIFT; 109 110 vdso_pagelist = kcalloc(vdso_info[abi].vdso_pages, 111 sizeof(struct page *), 112 GFP_KERNEL); 113 if (vdso_pagelist == NULL) 114 return -ENOMEM; 115 116 /* Grab the vDSO code pages. */ 117 pfn = sym_to_pfn(vdso_info[abi].vdso_code_start); 118 119 for (i = 0; i < vdso_info[abi].vdso_pages; i++) 120 vdso_pagelist[i] = pfn_to_page(pfn + i); 121 122 vdso_info[abi].cm->pages = vdso_pagelist; 123 124 return 0; 125 } 126 127 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm, 128 struct vm_area_struct *vma, struct vm_fault *vmf) 129 { 130 if (vmf->pgoff == 0) 131 return vmf_insert_pfn(vma, vmf->address, 132 sym_to_pfn(vdso_data)); 133 return VM_FAULT_SIGBUS; 134 } 135 136 static int __setup_additional_pages(enum vdso_abi abi, 137 struct mm_struct *mm, 138 struct linux_binprm *bprm, 139 int uses_interp) 140 { 141 unsigned long vdso_base, vdso_text_len, vdso_mapping_len; 142 unsigned long gp_flags = 0; 143 void *ret; 144 145 vdso_text_len = vdso_info[abi].vdso_pages << PAGE_SHIFT; 146 /* Be sure to map the data page */ 147 vdso_mapping_len = vdso_text_len + PAGE_SIZE; 148 149 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0); 150 if (IS_ERR_VALUE(vdso_base)) { 151 ret = ERR_PTR(vdso_base); 152 goto up_fail; 153 } 154 155 ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE, 156 VM_READ|VM_MAYREAD|VM_PFNMAP, 157 vdso_info[abi].dm); 158 if (IS_ERR(ret)) 159 goto up_fail; 160 161 if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) && system_supports_bti()) 162 gp_flags = VM_ARM64_BTI; 163 164 vdso_base += PAGE_SIZE; 165 mm->context.vdso = (void *)vdso_base; 166 ret = _install_special_mapping(mm, vdso_base, vdso_text_len, 167 VM_READ|VM_EXEC|gp_flags| 168 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 169 vdso_info[abi].cm); 170 if (IS_ERR(ret)) 171 goto up_fail; 172 173 return 0; 174 175 up_fail: 176 mm->context.vdso = NULL; 177 return PTR_ERR(ret); 178 } 179 180 #ifdef CONFIG_COMPAT 181 /* 182 * Create and map the vectors page for AArch32 tasks. 183 */ 184 #ifdef CONFIG_COMPAT_VDSO 185 static int aarch32_vdso_mremap(const struct vm_special_mapping *sm, 186 struct vm_area_struct *new_vma) 187 { 188 return __vdso_remap(VDSO_ABI_AA32, sm, new_vma); 189 } 190 #endif /* CONFIG_COMPAT_VDSO */ 191 192 enum aarch32_map { 193 AA32_MAP_VECTORS, /* kuser helpers */ 194 #ifdef CONFIG_COMPAT_VDSO 195 AA32_MAP_VVAR, 196 AA32_MAP_VDSO, 197 #endif 198 AA32_MAP_SIGPAGE 199 }; 200 201 static struct page *aarch32_vectors_page __ro_after_init; 202 static struct page *aarch32_sig_page __ro_after_init; 203 204 static struct vm_special_mapping aarch32_vdso_maps[] = { 205 [AA32_MAP_VECTORS] = { 206 .name = "[vectors]", /* ABI */ 207 .pages = &aarch32_vectors_page, 208 }, 209 #ifdef CONFIG_COMPAT_VDSO 210 [AA32_MAP_VVAR] = { 211 .name = "[vvar]", 212 .fault = vvar_fault, 213 }, 214 [AA32_MAP_VDSO] = { 215 .name = "[vdso]", 216 .mremap = aarch32_vdso_mremap, 217 }, 218 #endif /* CONFIG_COMPAT_VDSO */ 219 [AA32_MAP_SIGPAGE] = { 220 .name = "[sigpage]", /* ABI */ 221 .pages = &aarch32_sig_page, 222 }, 223 }; 224 225 static int aarch32_alloc_kuser_vdso_page(void) 226 { 227 extern char __kuser_helper_start[], __kuser_helper_end[]; 228 int kuser_sz = __kuser_helper_end - __kuser_helper_start; 229 unsigned long vdso_page; 230 231 if (!IS_ENABLED(CONFIG_KUSER_HELPERS)) 232 return 0; 233 234 vdso_page = get_zeroed_page(GFP_ATOMIC); 235 if (!vdso_page) 236 return -ENOMEM; 237 238 memcpy((void *)(vdso_page + 0x1000 - kuser_sz), __kuser_helper_start, 239 kuser_sz); 240 aarch32_vectors_page = virt_to_page(vdso_page); 241 flush_dcache_page(aarch32_vectors_page); 242 return 0; 243 } 244 245 static int aarch32_alloc_sigpage(void) 246 { 247 extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[]; 248 int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start; 249 unsigned long sigpage; 250 251 sigpage = get_zeroed_page(GFP_ATOMIC); 252 if (!sigpage) 253 return -ENOMEM; 254 255 memcpy((void *)sigpage, __aarch32_sigret_code_start, sigret_sz); 256 aarch32_sig_page = virt_to_page(sigpage); 257 flush_dcache_page(aarch32_sig_page); 258 return 0; 259 } 260 261 #ifdef CONFIG_COMPAT_VDSO 262 static int __aarch32_alloc_vdso_pages(void) 263 { 264 vdso_info[VDSO_ABI_AA32].dm = &aarch32_vdso_maps[AA32_MAP_VVAR]; 265 vdso_info[VDSO_ABI_AA32].cm = &aarch32_vdso_maps[AA32_MAP_VDSO]; 266 267 return __vdso_init(VDSO_ABI_AA32); 268 } 269 #endif /* CONFIG_COMPAT_VDSO */ 270 271 static int __init aarch32_alloc_vdso_pages(void) 272 { 273 int ret; 274 275 #ifdef CONFIG_COMPAT_VDSO 276 ret = __aarch32_alloc_vdso_pages(); 277 if (ret) 278 return ret; 279 #endif 280 281 ret = aarch32_alloc_sigpage(); 282 if (ret) 283 return ret; 284 285 return aarch32_alloc_kuser_vdso_page(); 286 } 287 arch_initcall(aarch32_alloc_vdso_pages); 288 289 static int aarch32_kuser_helpers_setup(struct mm_struct *mm) 290 { 291 void *ret; 292 293 if (!IS_ENABLED(CONFIG_KUSER_HELPERS)) 294 return 0; 295 296 /* 297 * Avoid VM_MAYWRITE for compatibility with arch/arm/, where it's 298 * not safe to CoW the page containing the CPU exception vectors. 299 */ 300 ret = _install_special_mapping(mm, AARCH32_VECTORS_BASE, PAGE_SIZE, 301 VM_READ | VM_EXEC | 302 VM_MAYREAD | VM_MAYEXEC, 303 &aarch32_vdso_maps[AA32_MAP_VECTORS]); 304 305 return PTR_ERR_OR_ZERO(ret); 306 } 307 308 static int aarch32_sigreturn_setup(struct mm_struct *mm) 309 { 310 unsigned long addr; 311 void *ret; 312 313 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0); 314 if (IS_ERR_VALUE(addr)) { 315 ret = ERR_PTR(addr); 316 goto out; 317 } 318 319 /* 320 * VM_MAYWRITE is required to allow gdb to Copy-on-Write and 321 * set breakpoints. 322 */ 323 ret = _install_special_mapping(mm, addr, PAGE_SIZE, 324 VM_READ | VM_EXEC | VM_MAYREAD | 325 VM_MAYWRITE | VM_MAYEXEC, 326 &aarch32_vdso_maps[AA32_MAP_SIGPAGE]); 327 if (IS_ERR(ret)) 328 goto out; 329 330 mm->context.sigpage = (void *)addr; 331 332 out: 333 return PTR_ERR_OR_ZERO(ret); 334 } 335 336 int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 337 { 338 struct mm_struct *mm = current->mm; 339 int ret; 340 341 if (mmap_write_lock_killable(mm)) 342 return -EINTR; 343 344 ret = aarch32_kuser_helpers_setup(mm); 345 if (ret) 346 goto out; 347 348 #ifdef CONFIG_COMPAT_VDSO 349 ret = __setup_additional_pages(VDSO_ABI_AA32, 350 mm, 351 bprm, 352 uses_interp); 353 if (ret) 354 goto out; 355 #endif /* CONFIG_COMPAT_VDSO */ 356 357 ret = aarch32_sigreturn_setup(mm); 358 out: 359 mmap_write_unlock(mm); 360 return ret; 361 } 362 #endif /* CONFIG_COMPAT */ 363 364 static int vdso_mremap(const struct vm_special_mapping *sm, 365 struct vm_area_struct *new_vma) 366 { 367 return __vdso_remap(VDSO_ABI_AA64, sm, new_vma); 368 } 369 370 enum aarch64_map { 371 AA64_MAP_VVAR, 372 AA64_MAP_VDSO, 373 }; 374 375 static struct vm_special_mapping aarch64_vdso_maps[] __ro_after_init = { 376 [AA64_MAP_VVAR] = { 377 .name = "[vvar]", 378 .fault = vvar_fault, 379 }, 380 [AA64_MAP_VDSO] = { 381 .name = "[vdso]", 382 .mremap = vdso_mremap, 383 }, 384 }; 385 386 static int __init vdso_init(void) 387 { 388 vdso_info[VDSO_ABI_AA64].dm = &aarch64_vdso_maps[AA64_MAP_VVAR]; 389 vdso_info[VDSO_ABI_AA64].cm = &aarch64_vdso_maps[AA64_MAP_VDSO]; 390 391 return __vdso_init(VDSO_ABI_AA64); 392 } 393 arch_initcall(vdso_init); 394 395 int arch_setup_additional_pages(struct linux_binprm *bprm, 396 int uses_interp) 397 { 398 struct mm_struct *mm = current->mm; 399 int ret; 400 401 if (mmap_write_lock_killable(mm)) 402 return -EINTR; 403 404 ret = __setup_additional_pages(VDSO_ABI_AA64, 405 mm, 406 bprm, 407 uses_interp); 408 409 mmap_write_unlock(mm); 410 411 return ret; 412 } 413