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 219031fefdSWill Deacon #include <linux/kernel.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> 279031fefdSWill Deacon #include <linux/mm.h> 289031fefdSWill Deacon #include <linux/sched.h> 299031fefdSWill Deacon #include <linux/signal.h> 309031fefdSWill Deacon #include <linux/slab.h> 31c60b0c28SCatalin Marinas #include <linux/timekeeper_internal.h> 329031fefdSWill Deacon #include <linux/vmalloc.h> 339031fefdSWill Deacon 349031fefdSWill Deacon #include <asm/cacheflush.h> 359031fefdSWill Deacon #include <asm/signal32.h> 369031fefdSWill Deacon #include <asm/vdso.h> 379031fefdSWill Deacon #include <asm/vdso_datapage.h> 389031fefdSWill Deacon 399031fefdSWill Deacon extern char vdso_start, vdso_end; 409031fefdSWill Deacon static unsigned long vdso_pages; 419031fefdSWill Deacon static struct page **vdso_pagelist; 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 */ 569031fefdSWill Deacon static struct page *vectors_page[1]; 579031fefdSWill Deacon 589031fefdSWill Deacon static int 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; 912fea7f6cSWill Deacon static struct vm_special_mapping spec = { 922fea7f6cSWill Deacon .name = "[vectors]", 932fea7f6cSWill Deacon .pages = vectors_page, 942fea7f6cSWill Deacon 952fea7f6cSWill Deacon }; 962fea7f6cSWill Deacon void *ret; 979031fefdSWill Deacon 989031fefdSWill Deacon down_write(&mm->mmap_sem); 999031fefdSWill Deacon current->mm->context.vdso = (void *)addr; 1009031fefdSWill Deacon 1019031fefdSWill Deacon /* Map vectors page at the high address. */ 1022fea7f6cSWill Deacon ret = _install_special_mapping(mm, addr, PAGE_SIZE, 1039031fefdSWill Deacon VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC, 1042fea7f6cSWill Deacon &spec); 1059031fefdSWill Deacon 1069031fefdSWill Deacon up_write(&mm->mmap_sem); 1079031fefdSWill Deacon 1082fea7f6cSWill Deacon return PTR_ERR_OR_ZERO(ret); 1099031fefdSWill Deacon } 1109031fefdSWill Deacon #endif /* CONFIG_COMPAT */ 1119031fefdSWill Deacon 1122fea7f6cSWill Deacon static struct vm_special_mapping vdso_spec[2]; 1132fea7f6cSWill Deacon 1149031fefdSWill Deacon static int __init vdso_init(void) 1159031fefdSWill Deacon { 11616fb1a9bSNathan Lynch int i; 11716fb1a9bSNathan Lynch 11816fb1a9bSNathan Lynch if (memcmp(&vdso_start, "\177ELF", 4)) { 11916fb1a9bSNathan Lynch pr_err("vDSO is not a valid ELF object!\n"); 12016fb1a9bSNathan Lynch return -EINVAL; 12116fb1a9bSNathan Lynch } 1229031fefdSWill Deacon 1239031fefdSWill Deacon vdso_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT; 124601255aeSWill Deacon pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n", 125601255aeSWill Deacon vdso_pages + 1, vdso_pages, &vdso_start, 1L, vdso_data); 1269031fefdSWill Deacon 1279031fefdSWill Deacon /* Allocate the vDSO pagelist, plus a page for the data. */ 12816fb1a9bSNathan Lynch vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *), 1299031fefdSWill Deacon GFP_KERNEL); 13016fb1a9bSNathan Lynch if (vdso_pagelist == NULL) 1319031fefdSWill Deacon return -ENOMEM; 1329031fefdSWill Deacon 133601255aeSWill Deacon /* Grab the vDSO data page. */ 134601255aeSWill Deacon vdso_pagelist[0] = virt_to_page(vdso_data); 135601255aeSWill Deacon 1369031fefdSWill Deacon /* Grab the vDSO code pages. */ 13716fb1a9bSNathan Lynch for (i = 0; i < vdso_pages; i++) 138601255aeSWill Deacon vdso_pagelist[i + 1] = virt_to_page(&vdso_start + i * PAGE_SIZE); 1399031fefdSWill Deacon 1402fea7f6cSWill Deacon /* Populate the special mapping structures */ 1412fea7f6cSWill Deacon vdso_spec[0] = (struct vm_special_mapping) { 142601255aeSWill Deacon .name = "[vvar]", 1432fea7f6cSWill Deacon .pages = vdso_pagelist, 1442fea7f6cSWill Deacon }; 1452fea7f6cSWill Deacon 1462fea7f6cSWill Deacon vdso_spec[1] = (struct vm_special_mapping) { 147601255aeSWill Deacon .name = "[vdso]", 148601255aeSWill Deacon .pages = &vdso_pagelist[1], 1492fea7f6cSWill Deacon }; 1502fea7f6cSWill Deacon 15116fb1a9bSNathan Lynch return 0; 1529031fefdSWill Deacon } 1539031fefdSWill Deacon arch_initcall(vdso_init); 1549031fefdSWill Deacon 1559031fefdSWill Deacon int arch_setup_additional_pages(struct linux_binprm *bprm, 1569031fefdSWill Deacon int uses_interp) 1579031fefdSWill Deacon { 1589031fefdSWill Deacon struct mm_struct *mm = current->mm; 15987154938SWill Deacon unsigned long vdso_base, vdso_text_len, vdso_mapping_len; 1602fea7f6cSWill Deacon void *ret; 1619031fefdSWill Deacon 16287154938SWill Deacon vdso_text_len = vdso_pages << PAGE_SHIFT; 1639031fefdSWill Deacon /* Be sure to map the data page */ 16487154938SWill Deacon vdso_mapping_len = vdso_text_len + PAGE_SIZE; 1659031fefdSWill Deacon 1669031fefdSWill Deacon down_write(&mm->mmap_sem); 1679031fefdSWill Deacon vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0); 1689031fefdSWill Deacon if (IS_ERR_VALUE(vdso_base)) { 1692fea7f6cSWill Deacon ret = ERR_PTR(vdso_base); 1709031fefdSWill Deacon goto up_fail; 1719031fefdSWill Deacon } 172601255aeSWill Deacon ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE, 173601255aeSWill Deacon VM_READ|VM_MAYREAD, 1742fea7f6cSWill Deacon &vdso_spec[0]); 1752fea7f6cSWill Deacon if (IS_ERR(ret)) 1769031fefdSWill Deacon goto up_fail; 17787154938SWill Deacon 178601255aeSWill Deacon vdso_base += PAGE_SIZE; 179601255aeSWill Deacon mm->context.vdso = (void *)vdso_base; 180601255aeSWill Deacon ret = _install_special_mapping(mm, vdso_base, vdso_text_len, 181601255aeSWill Deacon VM_READ|VM_EXEC| 182601255aeSWill Deacon VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 1832fea7f6cSWill Deacon &vdso_spec[1]); 1842fea7f6cSWill Deacon if (IS_ERR(ret)) 18587154938SWill Deacon goto up_fail; 18687154938SWill Deacon 187601255aeSWill Deacon 18887154938SWill Deacon up_write(&mm->mmap_sem); 18987154938SWill Deacon return 0; 1909031fefdSWill Deacon 1919031fefdSWill Deacon up_fail: 19287154938SWill Deacon mm->context.vdso = NULL; 1939031fefdSWill Deacon up_write(&mm->mmap_sem); 1942fea7f6cSWill Deacon return PTR_ERR(ret); 1959031fefdSWill Deacon } 1969031fefdSWill Deacon 1979031fefdSWill Deacon /* 1989031fefdSWill Deacon * Update the vDSO data page to keep in sync with kernel timekeeping. 1999031fefdSWill Deacon */ 200c60b0c28SCatalin Marinas void update_vsyscall(struct timekeeper *tk) 2019031fefdSWill Deacon { 202876e7881SPeter Zijlstra u32 use_syscall = strcmp(tk->tkr_mono.clock->name, "arch_sys_counter"); 2039031fefdSWill Deacon 2049031fefdSWill Deacon ++vdso_data->tb_seq_count; 2059031fefdSWill Deacon smp_wmb(); 2069031fefdSWill Deacon 2079031fefdSWill Deacon vdso_data->use_syscall = use_syscall; 208878854a3SNathan Lynch vdso_data->xtime_coarse_sec = tk->xtime_sec; 209878854a3SNathan Lynch vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >> 210878854a3SNathan Lynch tk->tkr_mono.shift; 211d4022a33SNathan Lynch vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec; 212d4022a33SNathan Lynch vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec; 2139031fefdSWill Deacon 2149031fefdSWill Deacon if (!use_syscall) { 215876e7881SPeter Zijlstra vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last; 216c60b0c28SCatalin Marinas vdso_data->xtime_clock_sec = tk->xtime_sec; 217876e7881SPeter Zijlstra vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec; 218876e7881SPeter Zijlstra vdso_data->cs_mult = tk->tkr_mono.mult; 219876e7881SPeter Zijlstra vdso_data->cs_shift = tk->tkr_mono.shift; 2209031fefdSWill Deacon } 2219031fefdSWill Deacon 2229031fefdSWill Deacon smp_wmb(); 2239031fefdSWill Deacon ++vdso_data->tb_seq_count; 2249031fefdSWill Deacon } 2259031fefdSWill Deacon 2269031fefdSWill Deacon void update_vsyscall_tz(void) 2279031fefdSWill Deacon { 2289031fefdSWill Deacon vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; 2299031fefdSWill Deacon vdso_data->tz_dsttime = sys_tz.tz_dsttime; 2309031fefdSWill Deacon } 231