19031fefdSWill Deacon /* 20d747f65SVincenzo Frascino * VDSO implementations. 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 */ 560d747f65SVincenzo Frascino #define C_VECTORS 0 570d747f65SVincenzo Frascino #define C_SIGPAGE 1 580d747f65SVincenzo Frascino #define C_PAGES (C_SIGPAGE + 1) 590d747f65SVincenzo Frascino static struct page *aarch32_vdso_pages[C_PAGES] __ro_after_init; 600d747f65SVincenzo Frascino static const struct vm_special_mapping aarch32_vdso_spec[C_PAGES] = { 610d747f65SVincenzo Frascino { 620d747f65SVincenzo Frascino .name = "[vectors]", /* ABI */ 630d747f65SVincenzo Frascino .pages = &aarch32_vdso_pages[C_VECTORS], 640d747f65SVincenzo Frascino }, 650d747f65SVincenzo Frascino { 660d747f65SVincenzo Frascino .name = "[sigpage]", /* ABI */ 670d747f65SVincenzo Frascino .pages = &aarch32_vdso_pages[C_SIGPAGE], 680d747f65SVincenzo Frascino }, 690d747f65SVincenzo Frascino }; 709031fefdSWill Deacon 710d747f65SVincenzo Frascino static int __init aarch32_alloc_vdso_pages(void) 729031fefdSWill Deacon { 739031fefdSWill Deacon extern char __kuser_helper_start[], __kuser_helper_end[]; 74a1d5ebafSMatthew Leach extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[]; 75a1d5ebafSMatthew Leach 769031fefdSWill Deacon int kuser_sz = __kuser_helper_end - __kuser_helper_start; 77a1d5ebafSMatthew Leach int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start; 780d747f65SVincenzo Frascino unsigned long vdso_pages[C_PAGES]; 799031fefdSWill Deacon 800d747f65SVincenzo Frascino vdso_pages[C_VECTORS] = get_zeroed_page(GFP_ATOMIC); 810d747f65SVincenzo Frascino if (!vdso_pages[C_VECTORS]) 829031fefdSWill Deacon return -ENOMEM; 839031fefdSWill Deacon 840d747f65SVincenzo Frascino vdso_pages[C_SIGPAGE] = get_zeroed_page(GFP_ATOMIC); 850d747f65SVincenzo Frascino if (!vdso_pages[C_SIGPAGE]) { 860d747f65SVincenzo Frascino free_page(vdso_pages[C_VECTORS]); 870d747f65SVincenzo Frascino return -ENOMEM; 880d747f65SVincenzo Frascino } 890d747f65SVincenzo Frascino 909031fefdSWill Deacon /* kuser helpers */ 910d747f65SVincenzo Frascino memcpy((void *)(vdso_pages[C_VECTORS] + 0x1000 - kuser_sz), 920d747f65SVincenzo Frascino __kuser_helper_start, 939031fefdSWill Deacon kuser_sz); 949031fefdSWill Deacon 959031fefdSWill Deacon /* sigreturn code */ 960d747f65SVincenzo Frascino memcpy((void *)vdso_pages[C_SIGPAGE], __aarch32_sigret_code_start, 970d747f65SVincenzo Frascino sigret_sz); 989031fefdSWill Deacon 990d747f65SVincenzo Frascino flush_icache_range(vdso_pages[C_VECTORS], 1000d747f65SVincenzo Frascino vdso_pages[C_VECTORS] + PAGE_SIZE); 1010d747f65SVincenzo Frascino flush_icache_range(vdso_pages[C_SIGPAGE], 1020d747f65SVincenzo Frascino vdso_pages[C_SIGPAGE] + PAGE_SIZE); 1030d747f65SVincenzo Frascino 1040d747f65SVincenzo Frascino aarch32_vdso_pages[C_VECTORS] = virt_to_page(vdso_pages[C_VECTORS]); 1050d747f65SVincenzo Frascino aarch32_vdso_pages[C_SIGPAGE] = virt_to_page(vdso_pages[C_SIGPAGE]); 1069031fefdSWill Deacon 1079031fefdSWill Deacon return 0; 1089031fefdSWill Deacon } 1090d747f65SVincenzo Frascino arch_initcall(aarch32_alloc_vdso_pages); 1109031fefdSWill Deacon 1110d747f65SVincenzo Frascino static int aarch32_kuser_helpers_setup(struct mm_struct *mm) 1120d747f65SVincenzo Frascino { 1130d747f65SVincenzo Frascino void *ret; 1140d747f65SVincenzo Frascino 1150d747f65SVincenzo Frascino /* 1160d747f65SVincenzo Frascino * Avoid VM_MAYWRITE for compatibility with arch/arm/, where it's 1170d747f65SVincenzo Frascino * not safe to CoW the page containing the CPU exception vectors. 1180d747f65SVincenzo Frascino */ 1190d747f65SVincenzo Frascino ret = _install_special_mapping(mm, AARCH32_VECTORS_BASE, PAGE_SIZE, 1200d747f65SVincenzo Frascino VM_READ | VM_EXEC | 1210d747f65SVincenzo Frascino VM_MAYREAD | VM_MAYEXEC, 1220d747f65SVincenzo Frascino &aarch32_vdso_spec[C_VECTORS]); 1230d747f65SVincenzo Frascino 1240d747f65SVincenzo Frascino return PTR_ERR_OR_ZERO(ret); 1250d747f65SVincenzo Frascino } 1260d747f65SVincenzo Frascino 1270d747f65SVincenzo Frascino static int aarch32_sigreturn_setup(struct mm_struct *mm) 1280d747f65SVincenzo Frascino { 1290d747f65SVincenzo Frascino unsigned long addr; 1300d747f65SVincenzo Frascino void *ret; 1310d747f65SVincenzo Frascino 1320d747f65SVincenzo Frascino addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0); 1330d747f65SVincenzo Frascino if (IS_ERR_VALUE(addr)) { 1340d747f65SVincenzo Frascino ret = ERR_PTR(addr); 1350d747f65SVincenzo Frascino goto out; 1360d747f65SVincenzo Frascino } 1370d747f65SVincenzo Frascino 1380d747f65SVincenzo Frascino /* 1390d747f65SVincenzo Frascino * VM_MAYWRITE is required to allow gdb to Copy-on-Write and 1400d747f65SVincenzo Frascino * set breakpoints. 1410d747f65SVincenzo Frascino */ 1420d747f65SVincenzo Frascino ret = _install_special_mapping(mm, addr, PAGE_SIZE, 1430d747f65SVincenzo Frascino VM_READ | VM_EXEC | VM_MAYREAD | 1440d747f65SVincenzo Frascino VM_MAYWRITE | VM_MAYEXEC, 1450d747f65SVincenzo Frascino &aarch32_vdso_spec[C_SIGPAGE]); 1460d747f65SVincenzo Frascino if (IS_ERR(ret)) 1470d747f65SVincenzo Frascino goto out; 1480d747f65SVincenzo Frascino 1490d747f65SVincenzo Frascino mm->context.vdso = (void *)addr; 1500d747f65SVincenzo Frascino 1510d747f65SVincenzo Frascino out: 1520d747f65SVincenzo Frascino return PTR_ERR_OR_ZERO(ret); 1530d747f65SVincenzo Frascino } 1540d747f65SVincenzo Frascino 1550d747f65SVincenzo Frascino int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 1569031fefdSWill Deacon { 1579031fefdSWill Deacon struct mm_struct *mm = current->mm; 1580d747f65SVincenzo Frascino int ret; 1599031fefdSWill Deacon 16069048176SMichal Hocko if (down_write_killable(&mm->mmap_sem)) 16169048176SMichal Hocko return -EINTR; 1629031fefdSWill Deacon 1630d747f65SVincenzo Frascino ret = aarch32_kuser_helpers_setup(mm); 1640d747f65SVincenzo Frascino if (ret) 1650d747f65SVincenzo Frascino goto out; 1669031fefdSWill Deacon 1670d747f65SVincenzo Frascino ret = aarch32_sigreturn_setup(mm); 1680d747f65SVincenzo Frascino 1690d747f65SVincenzo Frascino out: 1709031fefdSWill Deacon up_write(&mm->mmap_sem); 1710d747f65SVincenzo Frascino return ret; 1729031fefdSWill Deacon } 1739031fefdSWill Deacon #endif /* CONFIG_COMPAT */ 1749031fefdSWill Deacon 17573958695SDmitry Safonov static int vdso_mremap(const struct vm_special_mapping *sm, 17673958695SDmitry Safonov struct vm_area_struct *new_vma) 17773958695SDmitry Safonov { 17873958695SDmitry Safonov unsigned long new_size = new_vma->vm_end - new_vma->vm_start; 17973958695SDmitry Safonov unsigned long vdso_size = vdso_end - vdso_start; 18073958695SDmitry Safonov 18173958695SDmitry Safonov if (vdso_size != new_size) 18273958695SDmitry Safonov return -EINVAL; 18373958695SDmitry Safonov 18473958695SDmitry Safonov current->mm->context.vdso = (void *)new_vma->vm_start; 18573958695SDmitry Safonov 18673958695SDmitry Safonov return 0; 18773958695SDmitry Safonov } 18873958695SDmitry Safonov 1895a9e3e15SJisheng Zhang static struct vm_special_mapping vdso_spec[2] __ro_after_init = { 1905a9e3e15SJisheng Zhang { 1915a9e3e15SJisheng Zhang .name = "[vvar]", 1925a9e3e15SJisheng Zhang }, 1935a9e3e15SJisheng Zhang { 1945a9e3e15SJisheng Zhang .name = "[vdso]", 19573958695SDmitry Safonov .mremap = vdso_mremap, 1965a9e3e15SJisheng Zhang }, 1975a9e3e15SJisheng Zhang }; 1982fea7f6cSWill Deacon 1999031fefdSWill Deacon static int __init vdso_init(void) 2009031fefdSWill Deacon { 20116fb1a9bSNathan Lynch int i; 2025a9e3e15SJisheng Zhang struct page **vdso_pagelist; 2032077be67SLaura Abbott unsigned long pfn; 20416fb1a9bSNathan Lynch 205dbbb08f5SKees Cook if (memcmp(vdso_start, "\177ELF", 4)) { 20616fb1a9bSNathan Lynch pr_err("vDSO is not a valid ELF object!\n"); 20716fb1a9bSNathan Lynch return -EINVAL; 20816fb1a9bSNathan Lynch } 2099031fefdSWill Deacon 210dbbb08f5SKees Cook vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT; 2119031fefdSWill Deacon 2129031fefdSWill Deacon /* Allocate the vDSO pagelist, plus a page for the data. */ 21316fb1a9bSNathan Lynch vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *), 2149031fefdSWill Deacon GFP_KERNEL); 21516fb1a9bSNathan Lynch if (vdso_pagelist == NULL) 2169031fefdSWill Deacon return -ENOMEM; 2179031fefdSWill Deacon 218601255aeSWill Deacon /* Grab the vDSO data page. */ 2192077be67SLaura Abbott vdso_pagelist[0] = phys_to_page(__pa_symbol(vdso_data)); 2202077be67SLaura Abbott 221601255aeSWill Deacon 2229031fefdSWill Deacon /* Grab the vDSO code pages. */ 223dbbb08f5SKees Cook pfn = sym_to_pfn(vdso_start); 2242077be67SLaura Abbott 22516fb1a9bSNathan Lynch for (i = 0; i < vdso_pages; i++) 2262077be67SLaura Abbott vdso_pagelist[i + 1] = pfn_to_page(pfn + i); 2279031fefdSWill Deacon 2285a9e3e15SJisheng Zhang vdso_spec[0].pages = &vdso_pagelist[0]; 2295a9e3e15SJisheng Zhang vdso_spec[1].pages = &vdso_pagelist[1]; 2302fea7f6cSWill Deacon 23116fb1a9bSNathan Lynch return 0; 2329031fefdSWill Deacon } 2339031fefdSWill Deacon arch_initcall(vdso_init); 2349031fefdSWill Deacon 2359031fefdSWill Deacon int arch_setup_additional_pages(struct linux_binprm *bprm, 2369031fefdSWill Deacon int uses_interp) 2379031fefdSWill Deacon { 2389031fefdSWill Deacon struct mm_struct *mm = current->mm; 23987154938SWill Deacon unsigned long vdso_base, vdso_text_len, vdso_mapping_len; 2402fea7f6cSWill Deacon void *ret; 2419031fefdSWill Deacon 24287154938SWill Deacon vdso_text_len = vdso_pages << PAGE_SHIFT; 2439031fefdSWill Deacon /* Be sure to map the data page */ 24487154938SWill Deacon vdso_mapping_len = vdso_text_len + PAGE_SIZE; 2459031fefdSWill Deacon 24669048176SMichal Hocko if (down_write_killable(&mm->mmap_sem)) 24769048176SMichal Hocko return -EINTR; 2489031fefdSWill Deacon vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0); 2499031fefdSWill Deacon if (IS_ERR_VALUE(vdso_base)) { 2502fea7f6cSWill Deacon ret = ERR_PTR(vdso_base); 2519031fefdSWill Deacon goto up_fail; 2529031fefdSWill Deacon } 253601255aeSWill Deacon ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE, 254601255aeSWill Deacon VM_READ|VM_MAYREAD, 2552fea7f6cSWill Deacon &vdso_spec[0]); 2562fea7f6cSWill Deacon if (IS_ERR(ret)) 2579031fefdSWill Deacon goto up_fail; 25887154938SWill Deacon 259601255aeSWill Deacon vdso_base += PAGE_SIZE; 260601255aeSWill Deacon mm->context.vdso = (void *)vdso_base; 261601255aeSWill Deacon ret = _install_special_mapping(mm, vdso_base, vdso_text_len, 262601255aeSWill Deacon VM_READ|VM_EXEC| 263601255aeSWill Deacon VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 2642fea7f6cSWill Deacon &vdso_spec[1]); 2652fea7f6cSWill Deacon if (IS_ERR(ret)) 26687154938SWill Deacon goto up_fail; 26787154938SWill Deacon 268601255aeSWill Deacon 26987154938SWill Deacon up_write(&mm->mmap_sem); 27087154938SWill Deacon return 0; 2719031fefdSWill Deacon 2729031fefdSWill Deacon up_fail: 27387154938SWill Deacon mm->context.vdso = NULL; 2749031fefdSWill Deacon up_write(&mm->mmap_sem); 2752fea7f6cSWill Deacon return PTR_ERR(ret); 2769031fefdSWill Deacon } 2779031fefdSWill Deacon 2789031fefdSWill Deacon /* 2799031fefdSWill Deacon * Update the vDSO data page to keep in sync with kernel timekeeping. 2809031fefdSWill Deacon */ 281c60b0c28SCatalin Marinas void update_vsyscall(struct timekeeper *tk) 2829031fefdSWill Deacon { 2831d8f51d4SScott Wood u32 use_syscall = !tk->tkr_mono.clock->archdata.vdso_direct; 2849031fefdSWill Deacon 2859031fefdSWill Deacon ++vdso_data->tb_seq_count; 2869031fefdSWill Deacon smp_wmb(); 2879031fefdSWill Deacon 2889031fefdSWill Deacon vdso_data->use_syscall = use_syscall; 289878854a3SNathan Lynch vdso_data->xtime_coarse_sec = tk->xtime_sec; 290878854a3SNathan Lynch vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >> 291878854a3SNathan Lynch tk->tkr_mono.shift; 292d4022a33SNathan Lynch vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec; 293d4022a33SNathan Lynch vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec; 2949031fefdSWill Deacon 29581fb8736SVincenzo Frascino /* Read without the seqlock held by clock_getres() */ 29681fb8736SVincenzo Frascino WRITE_ONCE(vdso_data->hrtimer_res, hrtimer_resolution); 29781fb8736SVincenzo Frascino 2989031fefdSWill Deacon if (!use_syscall) { 29949eea433SKevin Brodsky /* tkr_mono.cycle_last == tkr_raw.cycle_last */ 300876e7881SPeter Zijlstra vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last; 301fc6eead7SJohn Stultz vdso_data->raw_time_sec = tk->raw_sec; 302fc6eead7SJohn Stultz vdso_data->raw_time_nsec = tk->tkr_raw.xtime_nsec; 303c60b0c28SCatalin Marinas vdso_data->xtime_clock_sec = tk->xtime_sec; 304876e7881SPeter Zijlstra vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec; 30549eea433SKevin Brodsky vdso_data->cs_mono_mult = tk->tkr_mono.mult; 30649eea433SKevin Brodsky vdso_data->cs_raw_mult = tk->tkr_raw.mult; 30749eea433SKevin Brodsky /* tkr_mono.shift == tkr_raw.shift */ 308876e7881SPeter Zijlstra vdso_data->cs_shift = tk->tkr_mono.shift; 3099031fefdSWill Deacon } 3109031fefdSWill Deacon 3119031fefdSWill Deacon smp_wmb(); 3129031fefdSWill Deacon ++vdso_data->tb_seq_count; 3139031fefdSWill Deacon } 3149031fefdSWill Deacon 3159031fefdSWill Deacon void update_vsyscall_tz(void) 3169031fefdSWill Deacon { 3179031fefdSWill Deacon vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; 3189031fefdSWill Deacon vdso_data->tz_dsttime = sys_tz.tz_dsttime; 3199031fefdSWill Deacon } 320