^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015 Imagination Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Alex Smith <alex.smith@imgtec.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/binfmts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/timekeeper_internal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/abi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/mips-cps.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/vdso.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <vdso/helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <vdso/vsyscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Kernel-provided data used by the VDSO. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static union mips_vdso_data mips_vdso_data __page_aligned_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct vdso_data *vdso_data = mips_vdso_data.data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * what we map and where within the area they are mapped is determined at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * runtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct page *no_pages[] = { NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static struct vm_special_mapping vdso_vvar_mapping = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .name = "[vvar]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .pages = no_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void __init init_vdso_image(struct mips_vdso_image *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned long num_pages, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned long data_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) BUG_ON(!PAGE_ALIGNED(image->data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) BUG_ON(!PAGE_ALIGNED(image->size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) num_pages = image->size / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) data_pfn = __phys_to_pfn(__pa_symbol(image->data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for (i = 0; i < num_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) image->mapping.pages[i] = pfn_to_page(data_pfn + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int __init init_vdso(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) init_vdso_image(&vdso_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #ifdef CONFIG_MIPS32_O32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) init_vdso_image(&vdso_image_o32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #ifdef CONFIG_MIPS32_N32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) init_vdso_image(&vdso_image_n32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) subsys_initcall(init_vdso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static unsigned long vdso_base(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned long base = STACK_TOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* Skip the delay slot emulation page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) base += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (current->flags & PF_RANDOMIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) base = PAGE_ALIGN(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct mips_vdso_image *image = current->thread.abi->vdso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct mm_struct *mm = current->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr, gic_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (mmap_write_lock_killable(mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* Map delay slot emulation page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) VM_READ | VM_EXEC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (IS_ERR_VALUE(base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Determine total area size. This includes the VDSO data itself, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * data page, and the GIC user page if present. Always create a mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * for the GIC user area if the GIC is present regardless of whether it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * is the current clocksource, in case it comes into use later on. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * only map a page even though the total area is 64K, as we only need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * the counter registers at the start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) gic_size = mips_gic_present() ? PAGE_SIZE : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) vvar_size = gic_size + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) size = vvar_size + image->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * Find a region that's large enough for us to perform the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * colour-matching alignment below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (cpu_has_dc_aliases)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) size += shm_align_mask + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) base = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (IS_ERR_VALUE(base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * If we suffer from dcache aliasing, ensure that the VDSO data page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * mapping is coloured the same as the kernel's mapping of that memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * This ensures that when the kernel updates the VDSO data userland
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * will observe it without requiring cache invalidations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (cpu_has_dc_aliases) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) base = __ALIGN_MASK(base, shm_align_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) base += ((unsigned long)vdso_data - gic_size) & shm_align_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) data_addr = base + gic_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) vdso_addr = data_addr + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) vma = _install_special_mapping(mm, base, vvar_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) VM_READ | VM_MAYREAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) &vdso_vvar_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (IS_ERR(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ret = PTR_ERR(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* Map GIC user page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (gic_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) gic_pfn = virt_to_phys(mips_gic_base + MIPS_GIC_USER_OFS) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ret = io_remap_pfn_range(vma, base, gic_pfn, gic_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pgprot_noncached(PAGE_READONLY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* Map data page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = remap_pfn_range(vma, data_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) virt_to_phys(vdso_data) >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) PAGE_SIZE, PAGE_READONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Map VDSO image. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) vma = _install_special_mapping(mm, vdso_addr, image->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) VM_READ | VM_EXEC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) &image->mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (IS_ERR(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ret = PTR_ERR(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) mm->context.vdso = (void *)vdso_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mmap_write_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }