Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2015 Imagination Technologies
0004  * Author: Alex Smith <alex.smith@imgtec.com>
0005  */
0006 
0007 #include <linux/binfmts.h>
0008 #include <linux/elf.h>
0009 #include <linux/err.h>
0010 #include <linux/init.h>
0011 #include <linux/ioport.h>
0012 #include <linux/kernel.h>
0013 #include <linux/mm.h>
0014 #include <linux/random.h>
0015 #include <linux/sched.h>
0016 #include <linux/slab.h>
0017 #include <linux/timekeeper_internal.h>
0018 
0019 #include <asm/abi.h>
0020 #include <asm/mips-cps.h>
0021 #include <asm/page.h>
0022 #include <asm/vdso.h>
0023 #include <vdso/helpers.h>
0024 #include <vdso/vsyscall.h>
0025 
0026 /* Kernel-provided data used by the VDSO. */
0027 static union mips_vdso_data mips_vdso_data __page_aligned_data;
0028 struct vdso_data *vdso_data = mips_vdso_data.data;
0029 
0030 /*
0031  * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as
0032  * what we map and where within the area they are mapped is determined at
0033  * runtime.
0034  */
0035 static struct page *no_pages[] = { NULL };
0036 static struct vm_special_mapping vdso_vvar_mapping = {
0037     .name = "[vvar]",
0038     .pages = no_pages,
0039 };
0040 
0041 static void __init init_vdso_image(struct mips_vdso_image *image)
0042 {
0043     unsigned long num_pages, i;
0044     unsigned long data_pfn;
0045 
0046     BUG_ON(!PAGE_ALIGNED(image->data));
0047     BUG_ON(!PAGE_ALIGNED(image->size));
0048 
0049     num_pages = image->size / PAGE_SIZE;
0050 
0051     data_pfn = __phys_to_pfn(__pa_symbol(image->data));
0052     for (i = 0; i < num_pages; i++)
0053         image->mapping.pages[i] = pfn_to_page(data_pfn + i);
0054 }
0055 
0056 static int __init init_vdso(void)
0057 {
0058     init_vdso_image(&vdso_image);
0059 
0060 #ifdef CONFIG_MIPS32_O32
0061     init_vdso_image(&vdso_image_o32);
0062 #endif
0063 
0064 #ifdef CONFIG_MIPS32_N32
0065     init_vdso_image(&vdso_image_n32);
0066 #endif
0067 
0068     return 0;
0069 }
0070 subsys_initcall(init_vdso);
0071 
0072 static unsigned long vdso_base(void)
0073 {
0074     unsigned long base = STACK_TOP;
0075 
0076     if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
0077         /* Skip the delay slot emulation page */
0078         base += PAGE_SIZE;
0079     }
0080 
0081     if (current->flags & PF_RANDOMIZE) {
0082         base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1);
0083         base = PAGE_ALIGN(base);
0084     }
0085 
0086     return base;
0087 }
0088 
0089 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
0090 {
0091     struct mips_vdso_image *image = current->thread.abi->vdso;
0092     struct mm_struct *mm = current->mm;
0093     unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr, gic_pfn, gic_base;
0094     struct vm_area_struct *vma;
0095     int ret;
0096 
0097     if (mmap_write_lock_killable(mm))
0098         return -EINTR;
0099 
0100     if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
0101         /* Map delay slot emulation page */
0102         base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
0103                 VM_READ | VM_EXEC |
0104                 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
0105                 0, NULL);
0106         if (IS_ERR_VALUE(base)) {
0107             ret = base;
0108             goto out;
0109         }
0110     }
0111 
0112     /*
0113      * Determine total area size. This includes the VDSO data itself, the
0114      * data page, and the GIC user page if present. Always create a mapping
0115      * for the GIC user area if the GIC is present regardless of whether it
0116      * is the current clocksource, in case it comes into use later on. We
0117      * only map a page even though the total area is 64K, as we only need
0118      * the counter registers at the start.
0119      */
0120     gic_size = mips_gic_present() ? PAGE_SIZE : 0;
0121     vvar_size = gic_size + PAGE_SIZE;
0122     size = vvar_size + image->size;
0123 
0124     /*
0125      * Find a region that's large enough for us to perform the
0126      * colour-matching alignment below.
0127      */
0128     if (cpu_has_dc_aliases)
0129         size += shm_align_mask + 1;
0130 
0131     base = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
0132     if (IS_ERR_VALUE(base)) {
0133         ret = base;
0134         goto out;
0135     }
0136 
0137     /*
0138      * If we suffer from dcache aliasing, ensure that the VDSO data page
0139      * mapping is coloured the same as the kernel's mapping of that memory.
0140      * This ensures that when the kernel updates the VDSO data userland
0141      * will observe it without requiring cache invalidations.
0142      */
0143     if (cpu_has_dc_aliases) {
0144         base = __ALIGN_MASK(base, shm_align_mask);
0145         base += ((unsigned long)vdso_data - gic_size) & shm_align_mask;
0146     }
0147 
0148     data_addr = base + gic_size;
0149     vdso_addr = data_addr + PAGE_SIZE;
0150 
0151     vma = _install_special_mapping(mm, base, vvar_size,
0152                        VM_READ | VM_MAYREAD,
0153                        &vdso_vvar_mapping);
0154     if (IS_ERR(vma)) {
0155         ret = PTR_ERR(vma);
0156         goto out;
0157     }
0158 
0159     /* Map GIC user page. */
0160     if (gic_size) {
0161         gic_base = (unsigned long)mips_gic_base + MIPS_GIC_USER_OFS;
0162         gic_pfn = PFN_DOWN(__pa(gic_base));
0163 
0164         ret = io_remap_pfn_range(vma, base, gic_pfn, gic_size,
0165                      pgprot_noncached(vma->vm_page_prot));
0166         if (ret)
0167             goto out;
0168     }
0169 
0170     /* Map data page. */
0171     ret = remap_pfn_range(vma, data_addr,
0172                   virt_to_phys(vdso_data) >> PAGE_SHIFT,
0173                   PAGE_SIZE, vma->vm_page_prot);
0174     if (ret)
0175         goto out;
0176 
0177     /* Map VDSO image. */
0178     vma = _install_special_mapping(mm, vdso_addr, image->size,
0179                        VM_READ | VM_EXEC |
0180                        VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
0181                        &image->mapping);
0182     if (IS_ERR(vma)) {
0183         ret = PTR_ERR(vma);
0184         goto out;
0185     }
0186 
0187     mm->context.vdso = (void *)vdso_addr;
0188     ret = 0;
0189 
0190 out:
0191     mmap_write_unlock(mm);
0192     return ret;
0193 }