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