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