xref: /openbmc/linux/arch/mips/kernel/vdso.c (revision 554af0c3)
1c52d0d30SDavid Daney /*
2ebb5e78cSAlex Smith  * Copyright (C) 2015 Imagination Technologies
3ebb5e78cSAlex Smith  * Author: Alex Smith <alex.smith@imgtec.com>
4c52d0d30SDavid Daney  *
5ebb5e78cSAlex Smith  * This program is free software; you can redistribute it and/or modify it
6ebb5e78cSAlex Smith  * under the terms of the GNU General Public License as published by the
7ebb5e78cSAlex Smith  * Free Software Foundation;  either version 2 of the  License, or (at your
8ebb5e78cSAlex Smith  * option) any later version.
9c52d0d30SDavid Daney  */
10c52d0d30SDavid Daney 
11c52d0d30SDavid Daney #include <linux/binfmts.h>
12c52d0d30SDavid Daney #include <linux/elf.h>
13ebb5e78cSAlex Smith #include <linux/err.h>
14ebb5e78cSAlex Smith #include <linux/init.h>
15a7f4df4eSAlex Smith #include <linux/ioport.h>
16a7f4df4eSAlex Smith #include <linux/irqchip/mips-gic.h>
17ebb5e78cSAlex Smith #include <linux/mm.h>
18ebb5e78cSAlex Smith #include <linux/sched.h>
19ebb5e78cSAlex Smith #include <linux/slab.h>
20a7f4df4eSAlex Smith #include <linux/timekeeper_internal.h>
21c52d0d30SDavid Daney 
22ebb5e78cSAlex Smith #include <asm/abi.h>
23c52d0d30SDavid Daney #include <asm/vdso.h>
24ebb5e78cSAlex Smith 
25ebb5e78cSAlex Smith /* Kernel-provided data used by the VDSO. */
26ebb5e78cSAlex Smith static union mips_vdso_data vdso_data __page_aligned_data;
27c52d0d30SDavid Daney 
28c52d0d30SDavid Daney /*
29a7f4df4eSAlex Smith  * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as
30ebb5e78cSAlex Smith  * what we map and where within the area they are mapped is determined at
31ebb5e78cSAlex Smith  * runtime.
32c52d0d30SDavid Daney  */
33ebb5e78cSAlex Smith static struct page *no_pages[] = { NULL };
34ebb5e78cSAlex Smith static struct vm_special_mapping vdso_vvar_mapping = {
35ebb5e78cSAlex Smith 	.name = "[vvar]",
36ebb5e78cSAlex Smith 	.pages = no_pages,
37ebb5e78cSAlex Smith };
38c52d0d30SDavid Daney 
39ebb5e78cSAlex Smith static void __init init_vdso_image(struct mips_vdso_image *image)
40c52d0d30SDavid Daney {
41ebb5e78cSAlex Smith 	unsigned long num_pages, i;
42554af0c3SJames Hogan 	unsigned long data_pfn;
43ebb5e78cSAlex Smith 
44ebb5e78cSAlex Smith 	BUG_ON(!PAGE_ALIGNED(image->data));
45ebb5e78cSAlex Smith 	BUG_ON(!PAGE_ALIGNED(image->size));
46ebb5e78cSAlex Smith 
47ebb5e78cSAlex Smith 	num_pages = image->size / PAGE_SIZE;
48ebb5e78cSAlex Smith 
49554af0c3SJames Hogan 	data_pfn = __phys_to_pfn(__pa_symbol(image->data));
50554af0c3SJames Hogan 	for (i = 0; i < num_pages; i++)
51554af0c3SJames Hogan 		image->mapping.pages[i] = pfn_to_page(data_pfn + i);
52c52d0d30SDavid Daney }
53c52d0d30SDavid Daney 
54c52d0d30SDavid Daney static int __init init_vdso(void)
55c52d0d30SDavid Daney {
56ebb5e78cSAlex Smith 	init_vdso_image(&vdso_image);
57c52d0d30SDavid Daney 
58ebb5e78cSAlex Smith #ifdef CONFIG_MIPS32_O32
59ebb5e78cSAlex Smith 	init_vdso_image(&vdso_image_o32);
60c52d0d30SDavid Daney #endif
61c52d0d30SDavid Daney 
62ebb5e78cSAlex Smith #ifdef CONFIG_MIPS32_N32
63ebb5e78cSAlex Smith 	init_vdso_image(&vdso_image_n32);
64ebb5e78cSAlex Smith #endif
65c52d0d30SDavid Daney 
66c52d0d30SDavid Daney 	return 0;
67c52d0d30SDavid Daney }
681ed84537SDavid Daney subsys_initcall(init_vdso);
69c52d0d30SDavid Daney 
70a7f4df4eSAlex Smith void update_vsyscall(struct timekeeper *tk)
71a7f4df4eSAlex Smith {
72a7f4df4eSAlex Smith 	vdso_data_write_begin(&vdso_data);
73a7f4df4eSAlex Smith 
74a7f4df4eSAlex Smith 	vdso_data.xtime_sec = tk->xtime_sec;
75a7f4df4eSAlex Smith 	vdso_data.xtime_nsec = tk->tkr_mono.xtime_nsec;
76a7f4df4eSAlex Smith 	vdso_data.wall_to_mono_sec = tk->wall_to_monotonic.tv_sec;
77a7f4df4eSAlex Smith 	vdso_data.wall_to_mono_nsec = tk->wall_to_monotonic.tv_nsec;
78a7f4df4eSAlex Smith 	vdso_data.cs_shift = tk->tkr_mono.shift;
79a7f4df4eSAlex Smith 
80a7f4df4eSAlex Smith 	vdso_data.clock_mode = tk->tkr_mono.clock->archdata.vdso_clock_mode;
81a7f4df4eSAlex Smith 	if (vdso_data.clock_mode != VDSO_CLOCK_NONE) {
82a7f4df4eSAlex Smith 		vdso_data.cs_mult = tk->tkr_mono.mult;
83a7f4df4eSAlex Smith 		vdso_data.cs_cycle_last = tk->tkr_mono.cycle_last;
84a7f4df4eSAlex Smith 		vdso_data.cs_mask = tk->tkr_mono.mask;
85a7f4df4eSAlex Smith 	}
86a7f4df4eSAlex Smith 
87a7f4df4eSAlex Smith 	vdso_data_write_end(&vdso_data);
88a7f4df4eSAlex Smith }
89a7f4df4eSAlex Smith 
90a7f4df4eSAlex Smith void update_vsyscall_tz(void)
91a7f4df4eSAlex Smith {
92a7f4df4eSAlex Smith 	if (vdso_data.clock_mode != VDSO_CLOCK_NONE) {
93a7f4df4eSAlex Smith 		vdso_data.tz_minuteswest = sys_tz.tz_minuteswest;
94a7f4df4eSAlex Smith 		vdso_data.tz_dsttime = sys_tz.tz_dsttime;
95a7f4df4eSAlex Smith 	}
96a7f4df4eSAlex Smith }
97a7f4df4eSAlex Smith 
98c52d0d30SDavid Daney int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
99c52d0d30SDavid Daney {
100ebb5e78cSAlex Smith 	struct mips_vdso_image *image = current->thread.abi->vdso;
101c52d0d30SDavid Daney 	struct mm_struct *mm = current->mm;
102a7f4df4eSAlex Smith 	unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr;
103ebb5e78cSAlex Smith 	struct vm_area_struct *vma;
104a7f4df4eSAlex Smith 	struct resource gic_res;
105ebb5e78cSAlex Smith 	int ret;
106c52d0d30SDavid Daney 
10769048176SMichal Hocko 	if (down_write_killable(&mm->mmap_sem))
10869048176SMichal Hocko 		return -EINTR;
109c52d0d30SDavid Daney 
110432c6bacSPaul Burton 	/* Map delay slot emulation page */
111432c6bacSPaul Burton 	base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
112432c6bacSPaul Burton 			   VM_READ|VM_WRITE|VM_EXEC|
113432c6bacSPaul Burton 			   VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
114432c6bacSPaul Burton 			   0);
115432c6bacSPaul Burton 	if (IS_ERR_VALUE(base)) {
116432c6bacSPaul Burton 		ret = base;
117432c6bacSPaul Burton 		goto out;
118432c6bacSPaul Burton 	}
119432c6bacSPaul Burton 
120a7f4df4eSAlex Smith 	/*
121a7f4df4eSAlex Smith 	 * Determine total area size. This includes the VDSO data itself, the
122a7f4df4eSAlex Smith 	 * data page, and the GIC user page if present. Always create a mapping
123a7f4df4eSAlex Smith 	 * for the GIC user area if the GIC is present regardless of whether it
124a7f4df4eSAlex Smith 	 * is the current clocksource, in case it comes into use later on. We
125a7f4df4eSAlex Smith 	 * only map a page even though the total area is 64K, as we only need
126a7f4df4eSAlex Smith 	 * the counter registers at the start.
127a7f4df4eSAlex Smith 	 */
128a7f4df4eSAlex Smith 	gic_size = gic_present ? PAGE_SIZE : 0;
129a7f4df4eSAlex Smith 	vvar_size = gic_size + PAGE_SIZE;
130a7f4df4eSAlex Smith 	size = vvar_size + image->size;
131a7f4df4eSAlex Smith 
132a7f4df4eSAlex Smith 	base = get_unmapped_area(NULL, 0, size, 0, 0);
133ebb5e78cSAlex Smith 	if (IS_ERR_VALUE(base)) {
134ebb5e78cSAlex Smith 		ret = base;
135ebb5e78cSAlex Smith 		goto out;
136c52d0d30SDavid Daney 	}
137c52d0d30SDavid Daney 
138a7f4df4eSAlex Smith 	data_addr = base + gic_size;
139a7f4df4eSAlex Smith 	vdso_addr = data_addr + PAGE_SIZE;
140ebb5e78cSAlex Smith 
141a7f4df4eSAlex Smith 	vma = _install_special_mapping(mm, base, vvar_size,
142ebb5e78cSAlex Smith 				       VM_READ | VM_MAYREAD,
143ebb5e78cSAlex Smith 				       &vdso_vvar_mapping);
144ebb5e78cSAlex Smith 	if (IS_ERR(vma)) {
145ebb5e78cSAlex Smith 		ret = PTR_ERR(vma);
146ebb5e78cSAlex Smith 		goto out;
147ebb5e78cSAlex Smith 	}
148ebb5e78cSAlex Smith 
149a7f4df4eSAlex Smith 	/* Map GIC user page. */
150a7f4df4eSAlex Smith 	if (gic_size) {
151a7f4df4eSAlex Smith 		ret = gic_get_usm_range(&gic_res);
152a7f4df4eSAlex Smith 		if (ret)
153a7f4df4eSAlex Smith 			goto out;
154a7f4df4eSAlex Smith 
155a7f4df4eSAlex Smith 		ret = io_remap_pfn_range(vma, base,
156a7f4df4eSAlex Smith 					 gic_res.start >> PAGE_SHIFT,
157a7f4df4eSAlex Smith 					 gic_size,
158a7f4df4eSAlex Smith 					 pgprot_noncached(PAGE_READONLY));
159a7f4df4eSAlex Smith 		if (ret)
160a7f4df4eSAlex Smith 			goto out;
161a7f4df4eSAlex Smith 	}
162a7f4df4eSAlex Smith 
163ebb5e78cSAlex Smith 	/* Map data page. */
164a7f4df4eSAlex Smith 	ret = remap_pfn_range(vma, data_addr,
165ebb5e78cSAlex Smith 			      virt_to_phys(&vdso_data) >> PAGE_SHIFT,
166ebb5e78cSAlex Smith 			      PAGE_SIZE, PAGE_READONLY);
167ebb5e78cSAlex Smith 	if (ret)
168ebb5e78cSAlex Smith 		goto out;
169ebb5e78cSAlex Smith 
170ebb5e78cSAlex Smith 	/* Map VDSO image. */
171ebb5e78cSAlex Smith 	vma = _install_special_mapping(mm, vdso_addr, image->size,
172c52d0d30SDavid Daney 				       VM_READ | VM_EXEC |
173909af768SJason Baron 				       VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
174ebb5e78cSAlex Smith 				       &image->mapping);
175ebb5e78cSAlex Smith 	if (IS_ERR(vma)) {
176ebb5e78cSAlex Smith 		ret = PTR_ERR(vma);
177ebb5e78cSAlex Smith 		goto out;
178c52d0d30SDavid Daney 	}
179c52d0d30SDavid Daney 
180ebb5e78cSAlex Smith 	mm->context.vdso = (void *)vdso_addr;
181ebb5e78cSAlex Smith 	ret = 0;
182ebb5e78cSAlex Smith 
183ebb5e78cSAlex Smith out:
184ebb5e78cSAlex Smith 	up_write(&mm->mmap_sem);
185ebb5e78cSAlex Smith 	return ret;
186c52d0d30SDavid Daney }
187