| 1 | |
| 2 | |
| 3 | * Copyright (C) 2015 Imagination Technologies
|
| 4 | * Author: Alex Smith <[email protected]>
|
| 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 |
|
| 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 |
|
| 93 | base = do_mmap(NULL, STACK_TOP, PAGE_SIZE, PROT_READ | PROT_EXEC,
|
| 94 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED,
|
| 95 | EMPTY_VMA_FLAGS, 0, &unused, 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 && IS_ENABLED(CONFIG_MIPS_GENERIC_GETTIMEOFDAY)) {
|
| 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 | if (IS_ENABLED(CONFIG_MIPS_GENERIC_GETTIMEOFDAY)) {
|
| 141 | vma = vdso_install_vvar_mapping(mm, data_addr);
|
| 142 | if (IS_ERR(vma)) {
|
| 143 | ret = PTR_ERR(vma);
|
| 144 | goto out;
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 |
|
| 149 | if (gic_size) {
|
| 150 | gic_base = (unsigned long)mips_gic_base + MIPS_GIC_USER_OFS;
|
| 151 | gic_pfn = PFN_DOWN(__pa(gic_base));
|
| 152 | static const struct vm_special_mapping gic_mapping = {
|
| 153 | .name = "[gic]",
|
| 154 | .pages = (struct page **) { NULL },
|
| 155 | };
|
| 156 |
|
| 157 | vma = _install_special_mapping(mm, base, gic_size, VM_READ | VM_MAYREAD,
|
| 158 | &gic_mapping);
|
| 159 | if (IS_ERR(vma)) {
|
| 160 | ret = PTR_ERR(vma);
|
| 161 | goto out;
|
| 162 | }
|
| 163 |
|
| 164 | ret = io_remap_pfn_range(vma, base, gic_pfn, gic_size,
|
| 165 | pgprot_noncached(vma->vm_page_prot));
|
| 166 | if (ret)
|
| 167 | goto out;
|
| 168 | }
|
| 169 |
|
| 170 |
|
| 171 | vma = _install_special_mapping(mm, vdso_addr, image->size,
|
| 172 | VM_READ | VM_EXEC |
|
| 173 | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
|
| 174 | &image->mapping);
|
| 175 | if (IS_ERR(vma)) {
|
| 176 | ret = PTR_ERR(vma);
|
| 177 | goto out;
|
| 178 | }
|
| 179 |
|
| 180 | mm->context.vdso = (void *)vdso_addr;
|
| 181 | ret = 0;
|
| 182 |
|
| 183 | out:
|
| 184 | mmap_write_unlock(mm);
|
| 185 | return ret;
|
| 186 | }
|