Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 2 | /* |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 3 | * Copyright (C) 2015 Imagination Technologies |
| 4 | * Author: Alex Smith <alex.smith@imgtec.com> |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 7 | #include <linux/binfmts.h> |
| 8 | #include <linux/elf.h> |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 9 | #include <linux/err.h> |
| 10 | #include <linux/init.h> |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 11 | #include <linux/ioport.h> |
Paul Burton | 0f02cfb | 2018-08-30 11:01:21 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 13 | #include <linux/mm.h> |
Paul Burton | ea7e048 | 2018-09-25 15:51:26 -0700 | [diff] [blame] | 14 | #include <linux/random.h> |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 15 | #include <linux/sched.h> |
| 16 | #include <linux/slab.h> |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 17 | #include <linux/timekeeper_internal.h> |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 18 | |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 19 | #include <asm/abi.h> |
Paul Burton | 00578cd | 2017-08-12 21:36:30 -0700 | [diff] [blame] | 20 | #include <asm/mips-cps.h> |
Paul Burton | 0f02cfb | 2018-08-30 11:01:21 -0700 | [diff] [blame] | 21 | #include <asm/page.h> |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 22 | #include <asm/vdso.h> |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 23 | |
| 24 | /* Kernel-provided data used by the VDSO. */ |
| 25 | static union mips_vdso_data vdso_data __page_aligned_data; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 26 | |
| 27 | /* |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 28 | * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 29 | * what we map and where within the area they are mapped is determined at |
| 30 | * runtime. |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 31 | */ |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 32 | static struct page *no_pages[] = { NULL }; |
| 33 | static struct vm_special_mapping vdso_vvar_mapping = { |
| 34 | .name = "[vvar]", |
| 35 | .pages = no_pages, |
| 36 | }; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 37 | |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 38 | static void __init init_vdso_image(struct mips_vdso_image *image) |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 39 | { |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 40 | unsigned long num_pages, i; |
James Hogan | 554af0c | 2016-09-07 13:37:01 +0100 | [diff] [blame] | 41 | unsigned long data_pfn; |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 42 | |
| 43 | BUG_ON(!PAGE_ALIGNED(image->data)); |
| 44 | BUG_ON(!PAGE_ALIGNED(image->size)); |
| 45 | |
| 46 | num_pages = image->size / PAGE_SIZE; |
| 47 | |
James Hogan | 554af0c | 2016-09-07 13:37:01 +0100 | [diff] [blame] | 48 | data_pfn = __phys_to_pfn(__pa_symbol(image->data)); |
| 49 | for (i = 0; i < num_pages; i++) |
| 50 | image->mapping.pages[i] = pfn_to_page(data_pfn + i); |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int __init init_vdso(void) |
| 54 | { |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 55 | init_vdso_image(&vdso_image); |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 56 | |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_MIPS32_O32 |
| 58 | init_vdso_image(&vdso_image_o32); |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 59 | #endif |
| 60 | |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 61 | #ifdef CONFIG_MIPS32_N32 |
| 62 | init_vdso_image(&vdso_image_n32); |
| 63 | #endif |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 64 | |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | } |
David Daney | 1ed8453 | 2010-06-16 15:00:28 -0700 | [diff] [blame] | 67 | subsys_initcall(init_vdso); |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 68 | |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 69 | void update_vsyscall(struct timekeeper *tk) |
| 70 | { |
| 71 | vdso_data_write_begin(&vdso_data); |
| 72 | |
| 73 | vdso_data.xtime_sec = tk->xtime_sec; |
| 74 | vdso_data.xtime_nsec = tk->tkr_mono.xtime_nsec; |
| 75 | vdso_data.wall_to_mono_sec = tk->wall_to_monotonic.tv_sec; |
| 76 | vdso_data.wall_to_mono_nsec = tk->wall_to_monotonic.tv_nsec; |
| 77 | vdso_data.cs_shift = tk->tkr_mono.shift; |
| 78 | |
| 79 | vdso_data.clock_mode = tk->tkr_mono.clock->archdata.vdso_clock_mode; |
| 80 | if (vdso_data.clock_mode != VDSO_CLOCK_NONE) { |
| 81 | vdso_data.cs_mult = tk->tkr_mono.mult; |
| 82 | vdso_data.cs_cycle_last = tk->tkr_mono.cycle_last; |
| 83 | vdso_data.cs_mask = tk->tkr_mono.mask; |
| 84 | } |
| 85 | |
| 86 | vdso_data_write_end(&vdso_data); |
| 87 | } |
| 88 | |
| 89 | void update_vsyscall_tz(void) |
| 90 | { |
| 91 | if (vdso_data.clock_mode != VDSO_CLOCK_NONE) { |
| 92 | vdso_data.tz_minuteswest = sys_tz.tz_minuteswest; |
| 93 | vdso_data.tz_dsttime = sys_tz.tz_dsttime; |
| 94 | } |
| 95 | } |
| 96 | |
Paul Burton | ea7e048 | 2018-09-25 15:51:26 -0700 | [diff] [blame] | 97 | static unsigned long vdso_base(void) |
| 98 | { |
| 99 | unsigned long base; |
| 100 | |
| 101 | /* Skip the delay slot emulation page */ |
| 102 | base = STACK_TOP + PAGE_SIZE; |
| 103 | |
| 104 | if (current->flags & PF_RANDOMIZE) { |
| 105 | base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1); |
| 106 | base = PAGE_ALIGN(base); |
| 107 | } |
| 108 | |
| 109 | return base; |
| 110 | } |
| 111 | |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 112 | int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) |
| 113 | { |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 114 | struct mips_vdso_image *image = current->thread.abi->vdso; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 115 | struct mm_struct *mm = current->mm; |
Paul Burton | 00578cd | 2017-08-12 21:36:30 -0700 | [diff] [blame] | 116 | unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr, gic_pfn; |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 117 | struct vm_area_struct *vma; |
| 118 | int ret; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 119 | |
Michal Hocko | 6904817 | 2016-05-23 16:25:54 -0700 | [diff] [blame] | 120 | if (down_write_killable(&mm->mmap_sem)) |
| 121 | return -EINTR; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 122 | |
Paul Burton | 432c6ba | 2016-07-08 11:06:19 +0100 | [diff] [blame] | 123 | /* Map delay slot emulation page */ |
| 124 | base = mmap_region(NULL, STACK_TOP, PAGE_SIZE, |
Paul Burton | adcc81f | 2018-12-20 17:45:43 +0000 | [diff] [blame] | 125 | VM_READ | VM_EXEC | |
| 126 | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC, |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 127 | 0, NULL); |
Paul Burton | 432c6ba | 2016-07-08 11:06:19 +0100 | [diff] [blame] | 128 | if (IS_ERR_VALUE(base)) { |
| 129 | ret = base; |
| 130 | goto out; |
| 131 | } |
| 132 | |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 133 | /* |
| 134 | * Determine total area size. This includes the VDSO data itself, the |
| 135 | * data page, and the GIC user page if present. Always create a mapping |
| 136 | * for the GIC user area if the GIC is present regardless of whether it |
| 137 | * is the current clocksource, in case it comes into use later on. We |
| 138 | * only map a page even though the total area is 64K, as we only need |
| 139 | * the counter registers at the start. |
| 140 | */ |
Paul Burton | 00578cd | 2017-08-12 21:36:30 -0700 | [diff] [blame] | 141 | gic_size = mips_gic_present() ? PAGE_SIZE : 0; |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 142 | vvar_size = gic_size + PAGE_SIZE; |
| 143 | size = vvar_size + image->size; |
| 144 | |
Paul Burton | 0f02cfb | 2018-08-30 11:01:21 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Find a region that's large enough for us to perform the |
| 147 | * colour-matching alignment below. |
| 148 | */ |
| 149 | if (cpu_has_dc_aliases) |
| 150 | size += shm_align_mask + 1; |
| 151 | |
Paul Burton | ea7e048 | 2018-09-25 15:51:26 -0700 | [diff] [blame] | 152 | base = get_unmapped_area(NULL, vdso_base(), size, 0, 0); |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 153 | if (IS_ERR_VALUE(base)) { |
| 154 | ret = base; |
| 155 | goto out; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Paul Burton | 0f02cfb | 2018-08-30 11:01:21 -0700 | [diff] [blame] | 158 | /* |
| 159 | * If we suffer from dcache aliasing, ensure that the VDSO data page |
| 160 | * mapping is coloured the same as the kernel's mapping of that memory. |
| 161 | * This ensures that when the kernel updates the VDSO data userland |
| 162 | * will observe it without requiring cache invalidations. |
| 163 | */ |
| 164 | if (cpu_has_dc_aliases) { |
| 165 | base = __ALIGN_MASK(base, shm_align_mask); |
| 166 | base += ((unsigned long)&vdso_data - gic_size) & shm_align_mask; |
| 167 | } |
| 168 | |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 169 | data_addr = base + gic_size; |
| 170 | vdso_addr = data_addr + PAGE_SIZE; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 171 | |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 172 | vma = _install_special_mapping(mm, base, vvar_size, |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 173 | VM_READ | VM_MAYREAD, |
| 174 | &vdso_vvar_mapping); |
| 175 | if (IS_ERR(vma)) { |
| 176 | ret = PTR_ERR(vma); |
| 177 | goto out; |
| 178 | } |
| 179 | |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 180 | /* Map GIC user page. */ |
| 181 | if (gic_size) { |
Paul Burton | 00578cd | 2017-08-12 21:36:30 -0700 | [diff] [blame] | 182 | gic_pfn = virt_to_phys(mips_gic_base + MIPS_GIC_USER_OFS) >> PAGE_SHIFT; |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 183 | |
Paul Burton | 00578cd | 2017-08-12 21:36:30 -0700 | [diff] [blame] | 184 | ret = io_remap_pfn_range(vma, base, gic_pfn, gic_size, |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 185 | pgprot_noncached(PAGE_READONLY)); |
| 186 | if (ret) |
| 187 | goto out; |
| 188 | } |
| 189 | |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 190 | /* Map data page. */ |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 191 | ret = remap_pfn_range(vma, data_addr, |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 192 | virt_to_phys(&vdso_data) >> PAGE_SHIFT, |
| 193 | PAGE_SIZE, PAGE_READONLY); |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 194 | if (ret) |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 195 | goto out; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 196 | |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 197 | /* Map VDSO image. */ |
| 198 | vma = _install_special_mapping(mm, vdso_addr, image->size, |
| 199 | VM_READ | VM_EXEC | |
| 200 | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC, |
| 201 | &image->mapping); |
| 202 | if (IS_ERR(vma)) { |
| 203 | ret = PTR_ERR(vma); |
| 204 | goto out; |
| 205 | } |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 206 | |
Alex Smith | ebb5e78 | 2015-10-21 09:54:38 +0100 | [diff] [blame] | 207 | mm->context.vdso = (void *)vdso_addr; |
| 208 | ret = 0; |
| 209 | |
| 210 | out: |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 211 | up_write(&mm->mmap_sem); |
| 212 | return ret; |
| 213 | } |