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