12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c52d0d30SDavid Daney /*
3ebb5e78cSAlex Smith * Copyright (C) 2015 Imagination Technologies
4ebb5e78cSAlex Smith * Author: Alex Smith <alex.smith@imgtec.com>
5c52d0d30SDavid Daney */
6c52d0d30SDavid Daney
7c52d0d30SDavid Daney #include <linux/binfmts.h>
8c52d0d30SDavid Daney #include <linux/elf.h>
9ebb5e78cSAlex Smith #include <linux/err.h>
10ebb5e78cSAlex Smith #include <linux/init.h>
11a7f4df4eSAlex Smith #include <linux/ioport.h>
120f02cfbcSPaul Burton #include <linux/kernel.h>
13ebb5e78cSAlex Smith #include <linux/mm.h>
14ea7e0480SPaul Burton #include <linux/random.h>
15ebb5e78cSAlex Smith #include <linux/sched.h>
16ebb5e78cSAlex Smith #include <linux/slab.h>
17a7f4df4eSAlex Smith #include <linux/timekeeper_internal.h>
18c52d0d30SDavid Daney
19ebb5e78cSAlex Smith #include <asm/abi.h>
2000578cd8SPaul Burton #include <asm/mips-cps.h>
210f02cfbcSPaul Burton #include <asm/page.h>
22c52d0d30SDavid Daney #include <asm/vdso.h>
2324640f23SVincenzo Frascino #include <vdso/helpers.h>
2424640f23SVincenzo Frascino #include <vdso/vsyscall.h>
25ebb5e78cSAlex Smith
26ebb5e78cSAlex Smith /* Kernel-provided data used by the VDSO. */
2724640f23SVincenzo Frascino static union mips_vdso_data mips_vdso_data __page_aligned_data;
2824640f23SVincenzo Frascino struct vdso_data *vdso_data = mips_vdso_data.data;
29c52d0d30SDavid Daney
30c52d0d30SDavid Daney /*
31a7f4df4eSAlex Smith * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as
32ebb5e78cSAlex Smith * what we map and where within the area they are mapped is determined at
33ebb5e78cSAlex Smith * runtime.
34c52d0d30SDavid Daney */
35ebb5e78cSAlex Smith static struct page *no_pages[] = { NULL };
36ebb5e78cSAlex Smith static struct vm_special_mapping vdso_vvar_mapping = {
37ebb5e78cSAlex Smith .name = "[vvar]",
38ebb5e78cSAlex Smith .pages = no_pages,
39ebb5e78cSAlex Smith };
40c52d0d30SDavid Daney
init_vdso_image(struct mips_vdso_image * image)41ebb5e78cSAlex Smith static void __init init_vdso_image(struct mips_vdso_image *image)
42c52d0d30SDavid Daney {
43ebb5e78cSAlex Smith unsigned long num_pages, i;
44554af0c3SJames Hogan unsigned long data_pfn;
45ebb5e78cSAlex Smith
46ebb5e78cSAlex Smith BUG_ON(!PAGE_ALIGNED(image->data));
47ebb5e78cSAlex Smith BUG_ON(!PAGE_ALIGNED(image->size));
48ebb5e78cSAlex Smith
49ebb5e78cSAlex Smith num_pages = image->size / PAGE_SIZE;
50ebb5e78cSAlex Smith
51554af0c3SJames Hogan data_pfn = __phys_to_pfn(__pa_symbol(image->data));
52554af0c3SJames Hogan for (i = 0; i < num_pages; i++)
53554af0c3SJames Hogan image->mapping.pages[i] = pfn_to_page(data_pfn + i);
54c52d0d30SDavid Daney }
55c52d0d30SDavid Daney
init_vdso(void)56c52d0d30SDavid Daney static int __init init_vdso(void)
57c52d0d30SDavid Daney {
58ebb5e78cSAlex Smith init_vdso_image(&vdso_image);
59c52d0d30SDavid Daney
60ebb5e78cSAlex Smith #ifdef CONFIG_MIPS32_O32
61ebb5e78cSAlex Smith init_vdso_image(&vdso_image_o32);
62c52d0d30SDavid Daney #endif
63c52d0d30SDavid Daney
64ebb5e78cSAlex Smith #ifdef CONFIG_MIPS32_N32
65ebb5e78cSAlex Smith init_vdso_image(&vdso_image_n32);
66ebb5e78cSAlex Smith #endif
67c52d0d30SDavid Daney
68c52d0d30SDavid Daney return 0;
69c52d0d30SDavid Daney }
701ed84537SDavid Daney subsys_initcall(init_vdso);
71c52d0d30SDavid Daney
vdso_base(void)72ea7e0480SPaul Burton static unsigned long vdso_base(void)
73ea7e0480SPaul Burton {
74aebdc6ffSYousong Zhou unsigned long base = STACK_TOP;
75ea7e0480SPaul Burton
76aebdc6ffSYousong Zhou if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
77ea7e0480SPaul Burton /* Skip the delay slot emulation page */
78aebdc6ffSYousong Zhou base += PAGE_SIZE;
79aebdc6ffSYousong Zhou }
80ea7e0480SPaul Burton
81ea7e0480SPaul Burton if (current->flags & PF_RANDOMIZE) {
82*8032bf12SJason A. Donenfeld base += get_random_u32_below(VDSO_RANDOMIZE_SIZE);
83ea7e0480SPaul Burton base = PAGE_ALIGN(base);
84ea7e0480SPaul Burton }
85ea7e0480SPaul Burton
86ea7e0480SPaul Burton return base;
87ea7e0480SPaul Burton }
88ea7e0480SPaul Burton
arch_setup_additional_pages(struct linux_binprm * bprm,int uses_interp)89c52d0d30SDavid Daney int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
90c52d0d30SDavid Daney {
91ebb5e78cSAlex Smith struct mips_vdso_image *image = current->thread.abi->vdso;
92c52d0d30SDavid Daney struct mm_struct *mm = current->mm;
93dfad83cbSFlorian Fainelli unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr, gic_pfn, gic_base;
94ebb5e78cSAlex Smith struct vm_area_struct *vma;
95ebb5e78cSAlex Smith int ret;
96c52d0d30SDavid Daney
97d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm))
9869048176SMichal Hocko return -EINTR;
99c52d0d30SDavid Daney
100aebdc6ffSYousong Zhou if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
101432c6bacSPaul Burton /* Map delay slot emulation page */
102432c6bacSPaul Burton base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
103adcc81f1SPaul Burton VM_READ | VM_EXEC |
104432c6bacSPaul Burton VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
105897ab3e0SMike Rapoport 0, NULL);
106432c6bacSPaul Burton if (IS_ERR_VALUE(base)) {
107432c6bacSPaul Burton ret = base;
108432c6bacSPaul Burton goto out;
109432c6bacSPaul Burton }
110aebdc6ffSYousong Zhou }
111432c6bacSPaul Burton
112a7f4df4eSAlex Smith /*
113a7f4df4eSAlex Smith * Determine total area size. This includes the VDSO data itself, the
114a7f4df4eSAlex Smith * data page, and the GIC user page if present. Always create a mapping
115a7f4df4eSAlex Smith * for the GIC user area if the GIC is present regardless of whether it
116a7f4df4eSAlex Smith * is the current clocksource, in case it comes into use later on. We
117a7f4df4eSAlex Smith * only map a page even though the total area is 64K, as we only need
118a7f4df4eSAlex Smith * the counter registers at the start.
119a7f4df4eSAlex Smith */
12000578cd8SPaul Burton gic_size = mips_gic_present() ? PAGE_SIZE : 0;
121a7f4df4eSAlex Smith vvar_size = gic_size + PAGE_SIZE;
122a7f4df4eSAlex Smith size = vvar_size + image->size;
123a7f4df4eSAlex Smith
1240f02cfbcSPaul Burton /*
1250f02cfbcSPaul Burton * Find a region that's large enough for us to perform the
1260f02cfbcSPaul Burton * colour-matching alignment below.
1270f02cfbcSPaul Burton */
1280f02cfbcSPaul Burton if (cpu_has_dc_aliases)
1290f02cfbcSPaul Burton size += shm_align_mask + 1;
1300f02cfbcSPaul Burton
131ea7e0480SPaul Burton base = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
132ebb5e78cSAlex Smith if (IS_ERR_VALUE(base)) {
133ebb5e78cSAlex Smith ret = base;
134ebb5e78cSAlex Smith goto out;
135c52d0d30SDavid Daney }
136c52d0d30SDavid Daney
1370f02cfbcSPaul Burton /*
1380f02cfbcSPaul Burton * If we suffer from dcache aliasing, ensure that the VDSO data page
1390f02cfbcSPaul Burton * mapping is coloured the same as the kernel's mapping of that memory.
1400f02cfbcSPaul Burton * This ensures that when the kernel updates the VDSO data userland
1410f02cfbcSPaul Burton * will observe it without requiring cache invalidations.
1420f02cfbcSPaul Burton */
1430f02cfbcSPaul Burton if (cpu_has_dc_aliases) {
1440f02cfbcSPaul Burton base = __ALIGN_MASK(base, shm_align_mask);
14524640f23SVincenzo Frascino base += ((unsigned long)vdso_data - gic_size) & shm_align_mask;
1460f02cfbcSPaul Burton }
1470f02cfbcSPaul Burton
148a7f4df4eSAlex Smith data_addr = base + gic_size;
149a7f4df4eSAlex Smith vdso_addr = data_addr + PAGE_SIZE;
150ebb5e78cSAlex Smith
151a7f4df4eSAlex Smith vma = _install_special_mapping(mm, base, vvar_size,
152ebb5e78cSAlex Smith VM_READ | VM_MAYREAD,
153ebb5e78cSAlex Smith &vdso_vvar_mapping);
154ebb5e78cSAlex Smith if (IS_ERR(vma)) {
155ebb5e78cSAlex Smith ret = PTR_ERR(vma);
156ebb5e78cSAlex Smith goto out;
157ebb5e78cSAlex Smith }
158ebb5e78cSAlex Smith
159a7f4df4eSAlex Smith /* Map GIC user page. */
160a7f4df4eSAlex Smith if (gic_size) {
161dfad83cbSFlorian Fainelli gic_base = (unsigned long)mips_gic_base + MIPS_GIC_USER_OFS;
1628baa6512SFlorian Fainelli gic_pfn = PFN_DOWN(__pa(gic_base));
163a7f4df4eSAlex Smith
16400578cd8SPaul Burton ret = io_remap_pfn_range(vma, base, gic_pfn, gic_size,
165724d554aSThomas Bogendoerfer pgprot_noncached(vma->vm_page_prot));
166a7f4df4eSAlex Smith if (ret)
167a7f4df4eSAlex Smith goto out;
168a7f4df4eSAlex Smith }
169a7f4df4eSAlex Smith
170ebb5e78cSAlex Smith /* Map data page. */
171a7f4df4eSAlex Smith ret = remap_pfn_range(vma, data_addr,
17224640f23SVincenzo Frascino virt_to_phys(vdso_data) >> PAGE_SHIFT,
173724d554aSThomas Bogendoerfer PAGE_SIZE, vma->vm_page_prot);
174ebb5e78cSAlex Smith if (ret)
175ebb5e78cSAlex Smith goto out;
176ebb5e78cSAlex Smith
177ebb5e78cSAlex Smith /* Map VDSO image. */
178ebb5e78cSAlex Smith vma = _install_special_mapping(mm, vdso_addr, image->size,
179c52d0d30SDavid Daney VM_READ | VM_EXEC |
180909af768SJason Baron VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
181ebb5e78cSAlex Smith &image->mapping);
182ebb5e78cSAlex Smith if (IS_ERR(vma)) {
183ebb5e78cSAlex Smith ret = PTR_ERR(vma);
184ebb5e78cSAlex Smith goto out;
185c52d0d30SDavid Daney }
186c52d0d30SDavid Daney
187ebb5e78cSAlex Smith mm->context.vdso = (void *)vdso_addr;
188ebb5e78cSAlex Smith ret = 0;
189ebb5e78cSAlex Smith
190ebb5e78cSAlex Smith out:
191d8ed45c5SMichel Lespinasse mmap_write_unlock(mm);
192ebb5e78cSAlex Smith return ret;
193c52d0d30SDavid Daney }
194