xref: /openbmc/linux/arch/arm64/kernel/vdso.c (revision c60b0c28)
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[];
619031fefdSWill Deacon 	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
629031fefdSWill Deacon 	unsigned long vpage;
639031fefdSWill Deacon 
649031fefdSWill Deacon 	vpage = get_zeroed_page(GFP_ATOMIC);
659031fefdSWill Deacon 
669031fefdSWill Deacon 	if (!vpage)
679031fefdSWill Deacon 		return -ENOMEM;
689031fefdSWill Deacon 
699031fefdSWill Deacon 	/* kuser helpers */
709031fefdSWill Deacon 	memcpy((void *)vpage + 0x1000 - kuser_sz, __kuser_helper_start,
719031fefdSWill Deacon 		kuser_sz);
729031fefdSWill Deacon 
739031fefdSWill Deacon 	/* sigreturn code */
749031fefdSWill Deacon 	memcpy((void *)vpage + AARCH32_KERN_SIGRET_CODE_OFFSET,
759031fefdSWill Deacon 		aarch32_sigret_code, sizeof(aarch32_sigret_code));
769031fefdSWill Deacon 
779031fefdSWill Deacon 	flush_icache_range(vpage, vpage + PAGE_SIZE);
789031fefdSWill Deacon 	vectors_page[0] = virt_to_page(vpage);
799031fefdSWill Deacon 
809031fefdSWill Deacon 	return 0;
819031fefdSWill Deacon }
829031fefdSWill Deacon arch_initcall(alloc_vectors_page);
839031fefdSWill Deacon 
849031fefdSWill Deacon int aarch32_setup_vectors_page(struct linux_binprm *bprm, int uses_interp)
859031fefdSWill Deacon {
869031fefdSWill Deacon 	struct mm_struct *mm = current->mm;
879031fefdSWill Deacon 	unsigned long addr = AARCH32_VECTORS_BASE;
889031fefdSWill Deacon 	int ret;
899031fefdSWill Deacon 
909031fefdSWill Deacon 	down_write(&mm->mmap_sem);
919031fefdSWill Deacon 	current->mm->context.vdso = (void *)addr;
929031fefdSWill Deacon 
939031fefdSWill Deacon 	/* Map vectors page at the high address. */
949031fefdSWill Deacon 	ret = install_special_mapping(mm, addr, PAGE_SIZE,
959031fefdSWill Deacon 				      VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC,
969031fefdSWill Deacon 				      vectors_page);
979031fefdSWill Deacon 
989031fefdSWill Deacon 	up_write(&mm->mmap_sem);
999031fefdSWill Deacon 
1009031fefdSWill Deacon 	return ret;
1019031fefdSWill Deacon }
1029031fefdSWill Deacon #endif /* CONFIG_COMPAT */
1039031fefdSWill Deacon 
1049031fefdSWill Deacon static int __init vdso_init(void)
1059031fefdSWill Deacon {
1069031fefdSWill Deacon 	struct page *pg;
1079031fefdSWill Deacon 	char *vbase;
1089031fefdSWill Deacon 	int i, ret = 0;
1099031fefdSWill Deacon 
1109031fefdSWill Deacon 	vdso_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
1119031fefdSWill Deacon 	pr_info("vdso: %ld pages (%ld code, %ld data) at base %p\n",
1129031fefdSWill Deacon 		vdso_pages + 1, vdso_pages, 1L, &vdso_start);
1139031fefdSWill Deacon 
1149031fefdSWill Deacon 	/* Allocate the vDSO pagelist, plus a page for the data. */
1159031fefdSWill Deacon 	vdso_pagelist = kzalloc(sizeof(struct page *) * (vdso_pages + 1),
1169031fefdSWill Deacon 				GFP_KERNEL);
1179031fefdSWill Deacon 	if (vdso_pagelist == NULL) {
1189031fefdSWill Deacon 		pr_err("Failed to allocate vDSO pagelist!\n");
1199031fefdSWill Deacon 		return -ENOMEM;
1209031fefdSWill Deacon 	}
1219031fefdSWill Deacon 
1229031fefdSWill Deacon 	/* Grab the vDSO code pages. */
1239031fefdSWill Deacon 	for (i = 0; i < vdso_pages; i++) {
1249031fefdSWill Deacon 		pg = virt_to_page(&vdso_start + i*PAGE_SIZE);
1259031fefdSWill Deacon 		ClearPageReserved(pg);
1269031fefdSWill Deacon 		get_page(pg);
1279031fefdSWill Deacon 		vdso_pagelist[i] = pg;
1289031fefdSWill Deacon 	}
1299031fefdSWill Deacon 
1309031fefdSWill Deacon 	/* Sanity check the shared object header. */
1319031fefdSWill Deacon 	vbase = vmap(vdso_pagelist, 1, 0, PAGE_KERNEL);
1329031fefdSWill Deacon 	if (vbase == NULL) {
1339031fefdSWill Deacon 		pr_err("Failed to map vDSO pagelist!\n");
1349031fefdSWill Deacon 		return -ENOMEM;
1359031fefdSWill Deacon 	} else if (memcmp(vbase, "\177ELF", 4)) {
1369031fefdSWill Deacon 		pr_err("vDSO is not a valid ELF object!\n");
1379031fefdSWill Deacon 		ret = -EINVAL;
1389031fefdSWill Deacon 		goto unmap;
1399031fefdSWill Deacon 	}
1409031fefdSWill Deacon 
1419031fefdSWill Deacon 	/* Grab the vDSO data page. */
1429031fefdSWill Deacon 	pg = virt_to_page(vdso_data);
1439031fefdSWill Deacon 	get_page(pg);
1449031fefdSWill Deacon 	vdso_pagelist[i] = pg;
1459031fefdSWill Deacon 
1469031fefdSWill Deacon unmap:
1479031fefdSWill Deacon 	vunmap(vbase);
1489031fefdSWill Deacon 	return ret;
1499031fefdSWill Deacon }
1509031fefdSWill Deacon arch_initcall(vdso_init);
1519031fefdSWill Deacon 
1529031fefdSWill Deacon int arch_setup_additional_pages(struct linux_binprm *bprm,
1539031fefdSWill Deacon 				int uses_interp)
1549031fefdSWill Deacon {
1559031fefdSWill Deacon 	struct mm_struct *mm = current->mm;
1569031fefdSWill Deacon 	unsigned long vdso_base, vdso_mapping_len;
1579031fefdSWill Deacon 	int ret;
1589031fefdSWill Deacon 
1599031fefdSWill Deacon 	/* Be sure to map the data page */
1609031fefdSWill Deacon 	vdso_mapping_len = (vdso_pages + 1) << PAGE_SHIFT;
1619031fefdSWill Deacon 
1629031fefdSWill Deacon 	down_write(&mm->mmap_sem);
1639031fefdSWill Deacon 	vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
1649031fefdSWill Deacon 	if (IS_ERR_VALUE(vdso_base)) {
1659031fefdSWill Deacon 		ret = vdso_base;
1669031fefdSWill Deacon 		goto up_fail;
1679031fefdSWill Deacon 	}
1689031fefdSWill Deacon 	mm->context.vdso = (void *)vdso_base;
1699031fefdSWill Deacon 
1709031fefdSWill Deacon 	ret = install_special_mapping(mm, vdso_base, vdso_mapping_len,
1719031fefdSWill Deacon 				      VM_READ|VM_EXEC|
1729031fefdSWill Deacon 				      VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
1739031fefdSWill Deacon 				      vdso_pagelist);
1749031fefdSWill Deacon 	if (ret) {
1759031fefdSWill Deacon 		mm->context.vdso = NULL;
1769031fefdSWill Deacon 		goto up_fail;
1779031fefdSWill Deacon 	}
1789031fefdSWill Deacon 
1799031fefdSWill Deacon up_fail:
1809031fefdSWill Deacon 	up_write(&mm->mmap_sem);
1819031fefdSWill Deacon 
1829031fefdSWill Deacon 	return ret;
1839031fefdSWill Deacon }
1849031fefdSWill Deacon 
1859031fefdSWill Deacon const char *arch_vma_name(struct vm_area_struct *vma)
1869031fefdSWill Deacon {
1879031fefdSWill Deacon 	/*
1889031fefdSWill Deacon 	 * We can re-use the vdso pointer in mm_context_t for identifying
1899031fefdSWill Deacon 	 * the vectors page for compat applications. The vDSO will always
1909031fefdSWill Deacon 	 * sit above TASK_UNMAPPED_BASE and so we don't need to worry about
1919031fefdSWill Deacon 	 * it conflicting with the vectors base.
1929031fefdSWill Deacon 	 */
1939031fefdSWill Deacon 	if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) {
1949031fefdSWill Deacon #ifdef CONFIG_COMPAT
1959031fefdSWill Deacon 		if (vma->vm_start == AARCH32_VECTORS_BASE)
1969031fefdSWill Deacon 			return "[vectors]";
1979031fefdSWill Deacon #endif
1989031fefdSWill Deacon 		return "[vdso]";
1999031fefdSWill Deacon 	}
2009031fefdSWill Deacon 
2019031fefdSWill Deacon 	return NULL;
2029031fefdSWill Deacon }
2039031fefdSWill Deacon 
2049031fefdSWill Deacon /*
2059031fefdSWill Deacon  * We define AT_SYSINFO_EHDR, so we need these function stubs to keep
2069031fefdSWill Deacon  * Linux happy.
2079031fefdSWill Deacon  */
2089031fefdSWill Deacon int in_gate_area_no_mm(unsigned long addr)
2099031fefdSWill Deacon {
2109031fefdSWill Deacon 	return 0;
2119031fefdSWill Deacon }
2129031fefdSWill Deacon 
2139031fefdSWill Deacon int in_gate_area(struct mm_struct *mm, unsigned long addr)
2149031fefdSWill Deacon {
2159031fefdSWill Deacon 	return 0;
2169031fefdSWill Deacon }
2179031fefdSWill Deacon 
2189031fefdSWill Deacon struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
2199031fefdSWill Deacon {
2209031fefdSWill Deacon 	return NULL;
2219031fefdSWill Deacon }
2229031fefdSWill Deacon 
2239031fefdSWill Deacon /*
2249031fefdSWill Deacon  * Update the vDSO data page to keep in sync with kernel timekeeping.
2259031fefdSWill Deacon  */
226c60b0c28SCatalin Marinas void update_vsyscall(struct timekeeper *tk)
2279031fefdSWill Deacon {
2289031fefdSWill Deacon 	struct timespec xtime_coarse;
229c60b0c28SCatalin Marinas 	u32 use_syscall = strcmp(tk->clock->name, "arch_sys_counter");
2309031fefdSWill Deacon 
2319031fefdSWill Deacon 	++vdso_data->tb_seq_count;
2329031fefdSWill Deacon 	smp_wmb();
2339031fefdSWill Deacon 
2349031fefdSWill Deacon 	xtime_coarse = __current_kernel_time();
2359031fefdSWill Deacon 	vdso_data->use_syscall			= use_syscall;
2369031fefdSWill Deacon 	vdso_data->xtime_coarse_sec		= xtime_coarse.tv_sec;
2379031fefdSWill Deacon 	vdso_data->xtime_coarse_nsec		= xtime_coarse.tv_nsec;
2389031fefdSWill Deacon 
2399031fefdSWill Deacon 	if (!use_syscall) {
240c60b0c28SCatalin Marinas 		vdso_data->cs_cycle_last	= tk->clock->cycle_last;
241c60b0c28SCatalin Marinas 		vdso_data->xtime_clock_sec	= tk->xtime_sec;
242c60b0c28SCatalin Marinas 		vdso_data->xtime_clock_nsec	= tk->xtime_nsec >> tk->shift;
243c60b0c28SCatalin Marinas 		vdso_data->cs_mult		= tk->mult;
244c60b0c28SCatalin Marinas 		vdso_data->cs_shift		= tk->shift;
245c60b0c28SCatalin Marinas 		vdso_data->wtm_clock_sec	= tk->wall_to_monotonic.tv_sec;
246c60b0c28SCatalin Marinas 		vdso_data->wtm_clock_nsec	= tk->wall_to_monotonic.tv_nsec;
2479031fefdSWill Deacon 	}
2489031fefdSWill Deacon 
2499031fefdSWill Deacon 	smp_wmb();
2509031fefdSWill Deacon 	++vdso_data->tb_seq_count;
2519031fefdSWill Deacon }
2529031fefdSWill Deacon 
2539031fefdSWill Deacon void update_vsyscall_tz(void)
2549031fefdSWill Deacon {
2559031fefdSWill Deacon 	++vdso_data->tb_seq_count;
2569031fefdSWill Deacon 	smp_wmb();
2579031fefdSWill Deacon 	vdso_data->tz_minuteswest	= sys_tz.tz_minuteswest;
2589031fefdSWill Deacon 	vdso_data->tz_dsttime		= sys_tz.tz_dsttime;
2599031fefdSWill Deacon 	smp_wmb();
2609031fefdSWill Deacon 	++vdso_data->tb_seq_count;
2619031fefdSWill Deacon }
262