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