19031fefdSWill Deacon /* 29031fefdSWill Deacon * VDSO implementation for AArch64 and vector page setup for AArch32. 39031fefdSWill Deacon * 49031fefdSWill Deacon * Copyright (C) 2012 ARM Limited 59031fefdSWill Deacon * 69031fefdSWill Deacon * This program is free software; you can redistribute it and/or modify 79031fefdSWill Deacon * it under the terms of the GNU General Public License version 2 as 89031fefdSWill Deacon * published by the Free Software Foundation. 99031fefdSWill Deacon * 109031fefdSWill Deacon * This program is distributed in the hope that it will be useful, 119031fefdSWill Deacon * but WITHOUT ANY WARRANTY; without even the implied warranty of 129031fefdSWill Deacon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 139031fefdSWill Deacon * GNU General Public License for more details. 149031fefdSWill Deacon * 159031fefdSWill Deacon * You should have received a copy of the GNU General Public License 169031fefdSWill Deacon * along with this program. If not, see <http://www.gnu.org/licenses/>. 179031fefdSWill Deacon * 189031fefdSWill Deacon * Author: Will Deacon <will.deacon@arm.com> 199031fefdSWill Deacon */ 209031fefdSWill Deacon 215a9e3e15SJisheng Zhang #include <linux/cache.h> 229031fefdSWill Deacon #include <linux/clocksource.h> 239031fefdSWill Deacon #include <linux/elf.h> 249031fefdSWill Deacon #include <linux/err.h> 259031fefdSWill Deacon #include <linux/errno.h> 269031fefdSWill Deacon #include <linux/gfp.h> 275a9e3e15SJisheng Zhang #include <linux/kernel.h> 289031fefdSWill Deacon #include <linux/mm.h> 299031fefdSWill Deacon #include <linux/sched.h> 309031fefdSWill Deacon #include <linux/signal.h> 319031fefdSWill Deacon #include <linux/slab.h> 32c60b0c28SCatalin Marinas #include <linux/timekeeper_internal.h> 339031fefdSWill Deacon #include <linux/vmalloc.h> 349031fefdSWill Deacon 359031fefdSWill Deacon #include <asm/cacheflush.h> 369031fefdSWill Deacon #include <asm/signal32.h> 379031fefdSWill Deacon #include <asm/vdso.h> 389031fefdSWill Deacon #include <asm/vdso_datapage.h> 399031fefdSWill Deacon 40dbbb08f5SKees Cook extern char vdso_start[], vdso_end[]; 415a9e3e15SJisheng Zhang static unsigned long vdso_pages __ro_after_init; 429031fefdSWill Deacon 439031fefdSWill Deacon /* 449031fefdSWill Deacon * The vDSO data page. 459031fefdSWill Deacon */ 469031fefdSWill Deacon static union { 479031fefdSWill Deacon struct vdso_data data; 489031fefdSWill Deacon u8 page[PAGE_SIZE]; 499031fefdSWill Deacon } vdso_data_store __page_aligned_data; 509031fefdSWill Deacon struct vdso_data *vdso_data = &vdso_data_store.data; 519031fefdSWill Deacon 529031fefdSWill Deacon #ifdef CONFIG_COMPAT 539031fefdSWill Deacon /* 549031fefdSWill Deacon * Create and map the vectors page for AArch32 tasks. 559031fefdSWill Deacon */ 565a9e3e15SJisheng Zhang static struct page *vectors_page[1] __ro_after_init; 579031fefdSWill Deacon 581aed28f9SJisheng Zhang static int __init alloc_vectors_page(void) 599031fefdSWill Deacon { 609031fefdSWill Deacon extern char __kuser_helper_start[], __kuser_helper_end[]; 61a1d5ebafSMatthew Leach extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[]; 62a1d5ebafSMatthew Leach 639031fefdSWill Deacon int kuser_sz = __kuser_helper_end - __kuser_helper_start; 64a1d5ebafSMatthew Leach int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start; 659031fefdSWill Deacon unsigned long vpage; 669031fefdSWill Deacon 679031fefdSWill Deacon vpage = get_zeroed_page(GFP_ATOMIC); 689031fefdSWill Deacon 699031fefdSWill Deacon if (!vpage) 709031fefdSWill Deacon return -ENOMEM; 719031fefdSWill Deacon 729031fefdSWill Deacon /* kuser helpers */ 739031fefdSWill Deacon memcpy((void *)vpage + 0x1000 - kuser_sz, __kuser_helper_start, 749031fefdSWill Deacon kuser_sz); 759031fefdSWill Deacon 769031fefdSWill Deacon /* sigreturn code */ 779031fefdSWill Deacon memcpy((void *)vpage + AARCH32_KERN_SIGRET_CODE_OFFSET, 78a1d5ebafSMatthew Leach __aarch32_sigret_code_start, sigret_sz); 799031fefdSWill Deacon 809031fefdSWill Deacon flush_icache_range(vpage, vpage + PAGE_SIZE); 819031fefdSWill Deacon vectors_page[0] = virt_to_page(vpage); 829031fefdSWill Deacon 839031fefdSWill Deacon return 0; 849031fefdSWill Deacon } 859031fefdSWill Deacon arch_initcall(alloc_vectors_page); 869031fefdSWill Deacon 879031fefdSWill Deacon int aarch32_setup_vectors_page(struct linux_binprm *bprm, int uses_interp) 889031fefdSWill Deacon { 899031fefdSWill Deacon struct mm_struct *mm = current->mm; 909031fefdSWill Deacon unsigned long addr = AARCH32_VECTORS_BASE; 91b6d081bdSJisheng Zhang static const struct vm_special_mapping spec = { 922fea7f6cSWill Deacon .name = "[vectors]", 932fea7f6cSWill Deacon .pages = vectors_page, 942fea7f6cSWill Deacon 952fea7f6cSWill Deacon }; 962fea7f6cSWill Deacon void *ret; 979031fefdSWill Deacon 9869048176SMichal Hocko if (down_write_killable(&mm->mmap_sem)) 9969048176SMichal Hocko return -EINTR; 1009031fefdSWill Deacon current->mm->context.vdso = (void *)addr; 1019031fefdSWill Deacon 1029031fefdSWill Deacon /* Map vectors page at the high address. */ 1032fea7f6cSWill Deacon ret = _install_special_mapping(mm, addr, PAGE_SIZE, 1049031fefdSWill Deacon VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC, 1052fea7f6cSWill Deacon &spec); 1069031fefdSWill Deacon 1079031fefdSWill Deacon up_write(&mm->mmap_sem); 1089031fefdSWill Deacon 1092fea7f6cSWill Deacon return PTR_ERR_OR_ZERO(ret); 1109031fefdSWill Deacon } 1119031fefdSWill Deacon #endif /* CONFIG_COMPAT */ 1129031fefdSWill Deacon 11373958695SDmitry Safonov static int vdso_mremap(const struct vm_special_mapping *sm, 11473958695SDmitry Safonov struct vm_area_struct *new_vma) 11573958695SDmitry Safonov { 11673958695SDmitry Safonov unsigned long new_size = new_vma->vm_end - new_vma->vm_start; 11773958695SDmitry Safonov unsigned long vdso_size = vdso_end - vdso_start; 11873958695SDmitry Safonov 11973958695SDmitry Safonov if (vdso_size != new_size) 12073958695SDmitry Safonov return -EINVAL; 12173958695SDmitry Safonov 12273958695SDmitry Safonov current->mm->context.vdso = (void *)new_vma->vm_start; 12373958695SDmitry Safonov 12473958695SDmitry Safonov return 0; 12573958695SDmitry Safonov } 12673958695SDmitry Safonov 1275a9e3e15SJisheng Zhang static struct vm_special_mapping vdso_spec[2] __ro_after_init = { 1285a9e3e15SJisheng Zhang { 1295a9e3e15SJisheng Zhang .name = "[vvar]", 1305a9e3e15SJisheng Zhang }, 1315a9e3e15SJisheng Zhang { 1325a9e3e15SJisheng Zhang .name = "[vdso]", 13373958695SDmitry Safonov .mremap = vdso_mremap, 1345a9e3e15SJisheng Zhang }, 1355a9e3e15SJisheng Zhang }; 1362fea7f6cSWill Deacon 1379031fefdSWill Deacon static int __init vdso_init(void) 1389031fefdSWill Deacon { 13916fb1a9bSNathan Lynch int i; 1405a9e3e15SJisheng Zhang struct page **vdso_pagelist; 1412077be67SLaura Abbott unsigned long pfn; 14216fb1a9bSNathan Lynch 143dbbb08f5SKees Cook if (memcmp(vdso_start, "\177ELF", 4)) { 14416fb1a9bSNathan Lynch pr_err("vDSO is not a valid ELF object!\n"); 14516fb1a9bSNathan Lynch return -EINVAL; 14616fb1a9bSNathan Lynch } 1479031fefdSWill Deacon 148dbbb08f5SKees Cook vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT; 149601255aeSWill Deacon pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n", 150dbbb08f5SKees Cook vdso_pages + 1, vdso_pages, vdso_start, 1L, vdso_data); 1519031fefdSWill Deacon 1529031fefdSWill Deacon /* Allocate the vDSO pagelist, plus a page for the data. */ 15316fb1a9bSNathan Lynch vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *), 1549031fefdSWill Deacon GFP_KERNEL); 15516fb1a9bSNathan Lynch if (vdso_pagelist == NULL) 1569031fefdSWill Deacon return -ENOMEM; 1579031fefdSWill Deacon 158601255aeSWill Deacon /* Grab the vDSO data page. */ 1592077be67SLaura Abbott vdso_pagelist[0] = phys_to_page(__pa_symbol(vdso_data)); 1602077be67SLaura Abbott 161601255aeSWill Deacon 1629031fefdSWill Deacon /* Grab the vDSO code pages. */ 163dbbb08f5SKees Cook pfn = sym_to_pfn(vdso_start); 1642077be67SLaura Abbott 16516fb1a9bSNathan Lynch for (i = 0; i < vdso_pages; i++) 1662077be67SLaura Abbott vdso_pagelist[i + 1] = pfn_to_page(pfn + i); 1679031fefdSWill Deacon 1685a9e3e15SJisheng Zhang vdso_spec[0].pages = &vdso_pagelist[0]; 1695a9e3e15SJisheng Zhang vdso_spec[1].pages = &vdso_pagelist[1]; 1702fea7f6cSWill Deacon 17116fb1a9bSNathan Lynch return 0; 1729031fefdSWill Deacon } 1739031fefdSWill Deacon arch_initcall(vdso_init); 1749031fefdSWill Deacon 1759031fefdSWill Deacon int arch_setup_additional_pages(struct linux_binprm *bprm, 1769031fefdSWill Deacon int uses_interp) 1779031fefdSWill Deacon { 1789031fefdSWill Deacon struct mm_struct *mm = current->mm; 17987154938SWill Deacon unsigned long vdso_base, vdso_text_len, vdso_mapping_len; 1802fea7f6cSWill Deacon void *ret; 1819031fefdSWill Deacon 18287154938SWill Deacon vdso_text_len = vdso_pages << PAGE_SHIFT; 1839031fefdSWill Deacon /* Be sure to map the data page */ 18487154938SWill Deacon vdso_mapping_len = vdso_text_len + PAGE_SIZE; 1859031fefdSWill Deacon 18669048176SMichal Hocko if (down_write_killable(&mm->mmap_sem)) 18769048176SMichal Hocko return -EINTR; 1889031fefdSWill Deacon vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0); 1899031fefdSWill Deacon if (IS_ERR_VALUE(vdso_base)) { 1902fea7f6cSWill Deacon ret = ERR_PTR(vdso_base); 1919031fefdSWill Deacon goto up_fail; 1929031fefdSWill Deacon } 193601255aeSWill Deacon ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE, 194601255aeSWill Deacon VM_READ|VM_MAYREAD, 1952fea7f6cSWill Deacon &vdso_spec[0]); 1962fea7f6cSWill Deacon if (IS_ERR(ret)) 1979031fefdSWill Deacon goto up_fail; 19887154938SWill Deacon 199601255aeSWill Deacon vdso_base += PAGE_SIZE; 200601255aeSWill Deacon mm->context.vdso = (void *)vdso_base; 201601255aeSWill Deacon ret = _install_special_mapping(mm, vdso_base, vdso_text_len, 202601255aeSWill Deacon VM_READ|VM_EXEC| 203601255aeSWill Deacon VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 2042fea7f6cSWill Deacon &vdso_spec[1]); 2052fea7f6cSWill Deacon if (IS_ERR(ret)) 20687154938SWill Deacon goto up_fail; 20787154938SWill Deacon 208601255aeSWill Deacon 20987154938SWill Deacon up_write(&mm->mmap_sem); 21087154938SWill Deacon return 0; 2119031fefdSWill Deacon 2129031fefdSWill Deacon up_fail: 21387154938SWill Deacon mm->context.vdso = NULL; 2149031fefdSWill Deacon up_write(&mm->mmap_sem); 2152fea7f6cSWill Deacon return PTR_ERR(ret); 2169031fefdSWill Deacon } 2179031fefdSWill Deacon 2189031fefdSWill Deacon /* 2199031fefdSWill Deacon * Update the vDSO data page to keep in sync with kernel timekeeping. 2209031fefdSWill Deacon */ 221c60b0c28SCatalin Marinas void update_vsyscall(struct timekeeper *tk) 2229031fefdSWill Deacon { 2231d8f51d4SScott Wood u32 use_syscall = !tk->tkr_mono.clock->archdata.vdso_direct; 2249031fefdSWill Deacon 2259031fefdSWill Deacon ++vdso_data->tb_seq_count; 2269031fefdSWill Deacon smp_wmb(); 2279031fefdSWill Deacon 2289031fefdSWill Deacon vdso_data->use_syscall = use_syscall; 229878854a3SNathan Lynch vdso_data->xtime_coarse_sec = tk->xtime_sec; 230878854a3SNathan Lynch vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >> 231878854a3SNathan Lynch tk->tkr_mono.shift; 232d4022a33SNathan Lynch vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec; 233d4022a33SNathan Lynch vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec; 2349031fefdSWill Deacon 2359031fefdSWill Deacon if (!use_syscall) { 23649eea433SKevin Brodsky /* tkr_mono.cycle_last == tkr_raw.cycle_last */ 237876e7881SPeter Zijlstra vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last; 238fc6eead7SJohn Stultz vdso_data->raw_time_sec = tk->raw_sec; 239fc6eead7SJohn Stultz vdso_data->raw_time_nsec = tk->tkr_raw.xtime_nsec; 240c60b0c28SCatalin Marinas vdso_data->xtime_clock_sec = tk->xtime_sec; 241876e7881SPeter Zijlstra vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec; 24249eea433SKevin Brodsky vdso_data->cs_mono_mult = tk->tkr_mono.mult; 24349eea433SKevin Brodsky vdso_data->cs_raw_mult = tk->tkr_raw.mult; 24449eea433SKevin Brodsky /* tkr_mono.shift == tkr_raw.shift */ 245876e7881SPeter Zijlstra vdso_data->cs_shift = tk->tkr_mono.shift; 2469031fefdSWill Deacon } 2479031fefdSWill Deacon 2489031fefdSWill Deacon smp_wmb(); 2499031fefdSWill Deacon ++vdso_data->tb_seq_count; 2509031fefdSWill Deacon } 2519031fefdSWill Deacon 2529031fefdSWill Deacon void update_vsyscall_tz(void) 2539031fefdSWill Deacon { 2549031fefdSWill Deacon vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; 2559031fefdSWill Deacon vdso_data->tz_dsttime = sys_tz.tz_dsttime; 2569031fefdSWill Deacon } 257