xref: /openbmc/linux/arch/arm64/kernel/vdso.c (revision af1b3cf2)
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 
711255a734SVincenzo Frascino static int aarch32_alloc_kuser_vdso_page(void)
729031fefdSWill Deacon {
739031fefdSWill Deacon 	extern char __kuser_helper_start[], __kuser_helper_end[];
749031fefdSWill Deacon 	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
751255a734SVincenzo Frascino 	unsigned long vdso_page;
769031fefdSWill Deacon 
77af1b3cf2SVincenzo Frascino 	if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
78af1b3cf2SVincenzo Frascino 		return 0;
79af1b3cf2SVincenzo Frascino 
801255a734SVincenzo Frascino 	vdso_page = get_zeroed_page(GFP_ATOMIC);
811255a734SVincenzo Frascino 	if (!vdso_page)
829031fefdSWill Deacon 		return -ENOMEM;
839031fefdSWill Deacon 
841255a734SVincenzo Frascino 	memcpy((void *)(vdso_page + 0x1000 - kuser_sz), __kuser_helper_start,
851255a734SVincenzo Frascino 	       kuser_sz);
861255a734SVincenzo Frascino 	aarch32_vdso_pages[C_VECTORS] = virt_to_page(vdso_page);
871255a734SVincenzo Frascino 	flush_dcache_page(aarch32_vdso_pages[C_VECTORS]);
881255a734SVincenzo Frascino 	return 0;
890d747f65SVincenzo Frascino }
900d747f65SVincenzo Frascino 
911255a734SVincenzo Frascino static int __init aarch32_alloc_vdso_pages(void)
921255a734SVincenzo Frascino {
931255a734SVincenzo Frascino 	extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
941255a734SVincenzo Frascino 	int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
951255a734SVincenzo Frascino 	unsigned long sigpage;
961255a734SVincenzo Frascino 	int ret;
979031fefdSWill Deacon 
981255a734SVincenzo Frascino 	sigpage = get_zeroed_page(GFP_ATOMIC);
991255a734SVincenzo Frascino 	if (!sigpage)
1001255a734SVincenzo Frascino 		return -ENOMEM;
1019031fefdSWill Deacon 
1021255a734SVincenzo Frascino 	memcpy((void *)sigpage, __aarch32_sigret_code_start, sigret_sz);
1031255a734SVincenzo Frascino 	aarch32_vdso_pages[C_SIGPAGE] = virt_to_page(sigpage);
1041255a734SVincenzo Frascino 	flush_dcache_page(aarch32_vdso_pages[C_SIGPAGE]);
1050d747f65SVincenzo Frascino 
1061255a734SVincenzo Frascino 	ret = aarch32_alloc_kuser_vdso_page();
1071255a734SVincenzo Frascino 	if (ret)
1081255a734SVincenzo Frascino 		free_page(sigpage);
1099031fefdSWill Deacon 
1101255a734SVincenzo Frascino 	return ret;
1119031fefdSWill Deacon }
1120d747f65SVincenzo Frascino arch_initcall(aarch32_alloc_vdso_pages);
1139031fefdSWill Deacon 
1140d747f65SVincenzo Frascino static int aarch32_kuser_helpers_setup(struct mm_struct *mm)
1150d747f65SVincenzo Frascino {
1160d747f65SVincenzo Frascino 	void *ret;
1170d747f65SVincenzo Frascino 
118af1b3cf2SVincenzo Frascino 	if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
119af1b3cf2SVincenzo Frascino 		return 0;
120af1b3cf2SVincenzo Frascino 
1210d747f65SVincenzo Frascino 	/*
1220d747f65SVincenzo Frascino 	 * Avoid VM_MAYWRITE for compatibility with arch/arm/, where it's
1230d747f65SVincenzo Frascino 	 * not safe to CoW the page containing the CPU exception vectors.
1240d747f65SVincenzo Frascino 	 */
1250d747f65SVincenzo Frascino 	ret = _install_special_mapping(mm, AARCH32_VECTORS_BASE, PAGE_SIZE,
1260d747f65SVincenzo Frascino 				       VM_READ | VM_EXEC |
1270d747f65SVincenzo Frascino 				       VM_MAYREAD | VM_MAYEXEC,
1280d747f65SVincenzo Frascino 				       &aarch32_vdso_spec[C_VECTORS]);
1290d747f65SVincenzo Frascino 
1300d747f65SVincenzo Frascino 	return PTR_ERR_OR_ZERO(ret);
1310d747f65SVincenzo Frascino }
1320d747f65SVincenzo Frascino 
1330d747f65SVincenzo Frascino static int aarch32_sigreturn_setup(struct mm_struct *mm)
1340d747f65SVincenzo Frascino {
1350d747f65SVincenzo Frascino 	unsigned long addr;
1360d747f65SVincenzo Frascino 	void *ret;
1370d747f65SVincenzo Frascino 
1380d747f65SVincenzo Frascino 	addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
1390d747f65SVincenzo Frascino 	if (IS_ERR_VALUE(addr)) {
1400d747f65SVincenzo Frascino 		ret = ERR_PTR(addr);
1410d747f65SVincenzo Frascino 		goto out;
1420d747f65SVincenzo Frascino 	}
1430d747f65SVincenzo Frascino 
1440d747f65SVincenzo Frascino 	/*
1450d747f65SVincenzo Frascino 	 * VM_MAYWRITE is required to allow gdb to Copy-on-Write and
1460d747f65SVincenzo Frascino 	 * set breakpoints.
1470d747f65SVincenzo Frascino 	 */
1480d747f65SVincenzo Frascino 	ret = _install_special_mapping(mm, addr, PAGE_SIZE,
1490d747f65SVincenzo Frascino 				       VM_READ | VM_EXEC | VM_MAYREAD |
1500d747f65SVincenzo Frascino 				       VM_MAYWRITE | VM_MAYEXEC,
1510d747f65SVincenzo Frascino 				       &aarch32_vdso_spec[C_SIGPAGE]);
1520d747f65SVincenzo Frascino 	if (IS_ERR(ret))
1530d747f65SVincenzo Frascino 		goto out;
1540d747f65SVincenzo Frascino 
1550d747f65SVincenzo Frascino 	mm->context.vdso = (void *)addr;
1560d747f65SVincenzo Frascino 
1570d747f65SVincenzo Frascino out:
1580d747f65SVincenzo Frascino 	return PTR_ERR_OR_ZERO(ret);
1590d747f65SVincenzo Frascino }
1600d747f65SVincenzo Frascino 
1610d747f65SVincenzo Frascino int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
1629031fefdSWill Deacon {
1639031fefdSWill Deacon 	struct mm_struct *mm = current->mm;
1640d747f65SVincenzo Frascino 	int ret;
1659031fefdSWill Deacon 
16669048176SMichal Hocko 	if (down_write_killable(&mm->mmap_sem))
16769048176SMichal Hocko 		return -EINTR;
1689031fefdSWill Deacon 
1690d747f65SVincenzo Frascino 	ret = aarch32_kuser_helpers_setup(mm);
1700d747f65SVincenzo Frascino 	if (ret)
1710d747f65SVincenzo Frascino 		goto out;
1729031fefdSWill Deacon 
1730d747f65SVincenzo Frascino 	ret = aarch32_sigreturn_setup(mm);
1740d747f65SVincenzo Frascino 
1750d747f65SVincenzo Frascino out:
1769031fefdSWill Deacon 	up_write(&mm->mmap_sem);
1770d747f65SVincenzo Frascino 	return ret;
1789031fefdSWill Deacon }
1799031fefdSWill Deacon #endif /* CONFIG_COMPAT */
1809031fefdSWill Deacon 
18173958695SDmitry Safonov static int vdso_mremap(const struct vm_special_mapping *sm,
18273958695SDmitry Safonov 		struct vm_area_struct *new_vma)
18373958695SDmitry Safonov {
18473958695SDmitry Safonov 	unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
18573958695SDmitry Safonov 	unsigned long vdso_size = vdso_end - vdso_start;
18673958695SDmitry Safonov 
18773958695SDmitry Safonov 	if (vdso_size != new_size)
18873958695SDmitry Safonov 		return -EINVAL;
18973958695SDmitry Safonov 
19073958695SDmitry Safonov 	current->mm->context.vdso = (void *)new_vma->vm_start;
19173958695SDmitry Safonov 
19273958695SDmitry Safonov 	return 0;
19373958695SDmitry Safonov }
19473958695SDmitry Safonov 
1955a9e3e15SJisheng Zhang static struct vm_special_mapping vdso_spec[2] __ro_after_init = {
1965a9e3e15SJisheng Zhang 	{
1975a9e3e15SJisheng Zhang 		.name	= "[vvar]",
1985a9e3e15SJisheng Zhang 	},
1995a9e3e15SJisheng Zhang 	{
2005a9e3e15SJisheng Zhang 		.name	= "[vdso]",
20173958695SDmitry Safonov 		.mremap = vdso_mremap,
2025a9e3e15SJisheng Zhang 	},
2035a9e3e15SJisheng Zhang };
2042fea7f6cSWill Deacon 
2059031fefdSWill Deacon static int __init vdso_init(void)
2069031fefdSWill Deacon {
20716fb1a9bSNathan Lynch 	int i;
2085a9e3e15SJisheng Zhang 	struct page **vdso_pagelist;
2092077be67SLaura Abbott 	unsigned long pfn;
21016fb1a9bSNathan Lynch 
211dbbb08f5SKees Cook 	if (memcmp(vdso_start, "\177ELF", 4)) {
21216fb1a9bSNathan Lynch 		pr_err("vDSO is not a valid ELF object!\n");
21316fb1a9bSNathan Lynch 		return -EINVAL;
21416fb1a9bSNathan Lynch 	}
2159031fefdSWill Deacon 
216dbbb08f5SKees Cook 	vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
2179031fefdSWill Deacon 
2189031fefdSWill Deacon 	/* Allocate the vDSO pagelist, plus a page for the data. */
21916fb1a9bSNathan Lynch 	vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *),
2209031fefdSWill Deacon 				GFP_KERNEL);
22116fb1a9bSNathan Lynch 	if (vdso_pagelist == NULL)
2229031fefdSWill Deacon 		return -ENOMEM;
2239031fefdSWill Deacon 
224601255aeSWill Deacon 	/* Grab the vDSO data page. */
2252077be67SLaura Abbott 	vdso_pagelist[0] = phys_to_page(__pa_symbol(vdso_data));
2262077be67SLaura Abbott 
227601255aeSWill Deacon 
2289031fefdSWill Deacon 	/* Grab the vDSO code pages. */
229dbbb08f5SKees Cook 	pfn = sym_to_pfn(vdso_start);
2302077be67SLaura Abbott 
23116fb1a9bSNathan Lynch 	for (i = 0; i < vdso_pages; i++)
2322077be67SLaura Abbott 		vdso_pagelist[i + 1] = pfn_to_page(pfn + i);
2339031fefdSWill Deacon 
2345a9e3e15SJisheng Zhang 	vdso_spec[0].pages = &vdso_pagelist[0];
2355a9e3e15SJisheng Zhang 	vdso_spec[1].pages = &vdso_pagelist[1];
2362fea7f6cSWill Deacon 
23716fb1a9bSNathan Lynch 	return 0;
2389031fefdSWill Deacon }
2399031fefdSWill Deacon arch_initcall(vdso_init);
2409031fefdSWill Deacon 
2419031fefdSWill Deacon int arch_setup_additional_pages(struct linux_binprm *bprm,
2429031fefdSWill Deacon 				int uses_interp)
2439031fefdSWill Deacon {
2449031fefdSWill Deacon 	struct mm_struct *mm = current->mm;
24587154938SWill Deacon 	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
2462fea7f6cSWill Deacon 	void *ret;
2479031fefdSWill Deacon 
24887154938SWill Deacon 	vdso_text_len = vdso_pages << PAGE_SHIFT;
2499031fefdSWill Deacon 	/* Be sure to map the data page */
25087154938SWill Deacon 	vdso_mapping_len = vdso_text_len + PAGE_SIZE;
2519031fefdSWill Deacon 
25269048176SMichal Hocko 	if (down_write_killable(&mm->mmap_sem))
25369048176SMichal Hocko 		return -EINTR;
2549031fefdSWill Deacon 	vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
2559031fefdSWill Deacon 	if (IS_ERR_VALUE(vdso_base)) {
2562fea7f6cSWill Deacon 		ret = ERR_PTR(vdso_base);
2579031fefdSWill Deacon 		goto up_fail;
2589031fefdSWill Deacon 	}
259601255aeSWill Deacon 	ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE,
260601255aeSWill Deacon 				       VM_READ|VM_MAYREAD,
2612fea7f6cSWill Deacon 				       &vdso_spec[0]);
2622fea7f6cSWill Deacon 	if (IS_ERR(ret))
2639031fefdSWill Deacon 		goto up_fail;
26487154938SWill Deacon 
265601255aeSWill Deacon 	vdso_base += PAGE_SIZE;
266601255aeSWill Deacon 	mm->context.vdso = (void *)vdso_base;
267601255aeSWill Deacon 	ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
268601255aeSWill Deacon 				       VM_READ|VM_EXEC|
269601255aeSWill Deacon 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
2702fea7f6cSWill Deacon 				       &vdso_spec[1]);
2712fea7f6cSWill Deacon 	if (IS_ERR(ret))
27287154938SWill Deacon 		goto up_fail;
27387154938SWill Deacon 
274601255aeSWill Deacon 
27587154938SWill Deacon 	up_write(&mm->mmap_sem);
27687154938SWill Deacon 	return 0;
2779031fefdSWill Deacon 
2789031fefdSWill Deacon up_fail:
27987154938SWill Deacon 	mm->context.vdso = NULL;
2809031fefdSWill Deacon 	up_write(&mm->mmap_sem);
2812fea7f6cSWill Deacon 	return PTR_ERR(ret);
2829031fefdSWill Deacon }
2839031fefdSWill Deacon 
2849031fefdSWill Deacon /*
2859031fefdSWill Deacon  * Update the vDSO data page to keep in sync with kernel timekeeping.
2869031fefdSWill Deacon  */
287c60b0c28SCatalin Marinas void update_vsyscall(struct timekeeper *tk)
2889031fefdSWill Deacon {
2891d8f51d4SScott Wood 	u32 use_syscall = !tk->tkr_mono.clock->archdata.vdso_direct;
2909031fefdSWill Deacon 
2919031fefdSWill Deacon 	++vdso_data->tb_seq_count;
2929031fefdSWill Deacon 	smp_wmb();
2939031fefdSWill Deacon 
2949031fefdSWill Deacon 	vdso_data->use_syscall			= use_syscall;
295878854a3SNathan Lynch 	vdso_data->xtime_coarse_sec		= tk->xtime_sec;
296878854a3SNathan Lynch 	vdso_data->xtime_coarse_nsec		= tk->tkr_mono.xtime_nsec >>
297878854a3SNathan Lynch 							tk->tkr_mono.shift;
298d4022a33SNathan Lynch 	vdso_data->wtm_clock_sec		= tk->wall_to_monotonic.tv_sec;
299d4022a33SNathan Lynch 	vdso_data->wtm_clock_nsec		= tk->wall_to_monotonic.tv_nsec;
3009031fefdSWill Deacon 
30181fb8736SVincenzo Frascino 	/* Read without the seqlock held by clock_getres() */
30281fb8736SVincenzo Frascino 	WRITE_ONCE(vdso_data->hrtimer_res, hrtimer_resolution);
30381fb8736SVincenzo Frascino 
3049031fefdSWill Deacon 	if (!use_syscall) {
30549eea433SKevin Brodsky 		/* tkr_mono.cycle_last == tkr_raw.cycle_last */
306876e7881SPeter Zijlstra 		vdso_data->cs_cycle_last	= tk->tkr_mono.cycle_last;
307fc6eead7SJohn Stultz 		vdso_data->raw_time_sec         = tk->raw_sec;
308fc6eead7SJohn Stultz 		vdso_data->raw_time_nsec        = tk->tkr_raw.xtime_nsec;
309c60b0c28SCatalin Marinas 		vdso_data->xtime_clock_sec	= tk->xtime_sec;
310876e7881SPeter Zijlstra 		vdso_data->xtime_clock_nsec	= tk->tkr_mono.xtime_nsec;
31149eea433SKevin Brodsky 		vdso_data->cs_mono_mult		= tk->tkr_mono.mult;
31249eea433SKevin Brodsky 		vdso_data->cs_raw_mult		= tk->tkr_raw.mult;
31349eea433SKevin Brodsky 		/* tkr_mono.shift == tkr_raw.shift */
314876e7881SPeter Zijlstra 		vdso_data->cs_shift		= tk->tkr_mono.shift;
3159031fefdSWill Deacon 	}
3169031fefdSWill Deacon 
3179031fefdSWill Deacon 	smp_wmb();
3189031fefdSWill Deacon 	++vdso_data->tb_seq_count;
3199031fefdSWill Deacon }
3209031fefdSWill Deacon 
3219031fefdSWill Deacon void update_vsyscall_tz(void)
3229031fefdSWill Deacon {
3239031fefdSWill Deacon 	vdso_data->tz_minuteswest	= sys_tz.tz_minuteswest;
3249031fefdSWill Deacon 	vdso_data->tz_dsttime		= sys_tz.tz_dsttime;
3259031fefdSWill Deacon }
326