futex: Add sys_futex_wake()
[linux-2.6-block.git] / arch / mips / kernel / vdso.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
c52d0d30 2/*
ebb5e78c
AS
3 * Copyright (C) 2015 Imagination Technologies
4 * Author: Alex Smith <alex.smith@imgtec.com>
c52d0d30
DD
5 */
6
c52d0d30
DD
7#include <linux/binfmts.h>
8#include <linux/elf.h>
ebb5e78c
AS
9#include <linux/err.h>
10#include <linux/init.h>
a7f4df4e 11#include <linux/ioport.h>
0f02cfbc 12#include <linux/kernel.h>
ebb5e78c 13#include <linux/mm.h>
ea7e0480 14#include <linux/random.h>
ebb5e78c
AS
15#include <linux/sched.h>
16#include <linux/slab.h>
a7f4df4e 17#include <linux/timekeeper_internal.h>
c52d0d30 18
ebb5e78c 19#include <asm/abi.h>
00578cd8 20#include <asm/mips-cps.h>
0f02cfbc 21#include <asm/page.h>
c52d0d30 22#include <asm/vdso.h>
24640f23
VF
23#include <vdso/helpers.h>
24#include <vdso/vsyscall.h>
ebb5e78c
AS
25
26/* Kernel-provided data used by the VDSO. */
24640f23
VF
27static union mips_vdso_data mips_vdso_data __page_aligned_data;
28struct vdso_data *vdso_data = mips_vdso_data.data;
c52d0d30
DD
29
30/*
a7f4df4e 31 * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as
ebb5e78c
AS
32 * what we map and where within the area they are mapped is determined at
33 * runtime.
c52d0d30 34 */
ebb5e78c
AS
35static struct page *no_pages[] = { NULL };
36static struct vm_special_mapping vdso_vvar_mapping = {
37 .name = "[vvar]",
38 .pages = no_pages,
39};
c52d0d30 40
ebb5e78c 41static void __init init_vdso_image(struct mips_vdso_image *image)
c52d0d30 42{
ebb5e78c 43 unsigned long num_pages, i;
554af0c3 44 unsigned long data_pfn;
ebb5e78c
AS
45
46 BUG_ON(!PAGE_ALIGNED(image->data));
47 BUG_ON(!PAGE_ALIGNED(image->size));
48
49 num_pages = image->size / PAGE_SIZE;
50
554af0c3
JH
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);
c52d0d30
DD
54}
55
56static int __init init_vdso(void)
57{
ebb5e78c
AS
58 init_vdso_image(&vdso_image);
59
60#ifdef CONFIG_MIPS32_O32
61 init_vdso_image(&vdso_image_o32);
c52d0d30
DD
62#endif
63
ebb5e78c
AS
64#ifdef CONFIG_MIPS32_N32
65 init_vdso_image(&vdso_image_n32);
66#endif
c52d0d30 67
c52d0d30
DD
68 return 0;
69}
1ed84537 70subsys_initcall(init_vdso);
c52d0d30 71
ea7e0480
PB
72static unsigned long vdso_base(void)
73{
aebdc6ff 74 unsigned long base = STACK_TOP;
ea7e0480 75
aebdc6ff
YZ
76 if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
77 /* Skip the delay slot emulation page */
78 base += PAGE_SIZE;
79 }
ea7e0480
PB
80
81 if (current->flags & PF_RANDOMIZE) {
8032bf12 82 base += get_random_u32_below(VDSO_RANDOMIZE_SIZE);
ea7e0480
PB
83 base = PAGE_ALIGN(base);
84 }
85
86 return base;
87}
88
c52d0d30
DD
89int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
90{
ebb5e78c 91 struct mips_vdso_image *image = current->thread.abi->vdso;
c52d0d30 92 struct mm_struct *mm = current->mm;
dfad83cb 93 unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr, gic_pfn, gic_base;
ebb5e78c
AS
94 struct vm_area_struct *vma;
95 int ret;
c52d0d30 96
d8ed45c5 97 if (mmap_write_lock_killable(mm))
69048176 98 return -EINTR;
c52d0d30 99
aebdc6ff
YZ
100 if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
101 /* Map delay slot emulation page */
102 base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
103 VM_READ | VM_EXEC |
104 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
105 0, NULL);
106 if (IS_ERR_VALUE(base)) {
107 ret = base;
108 goto out;
109 }
432c6bac
PB
110 }
111
a7f4df4e
AS
112 /*
113 * Determine total area size. This includes the VDSO data itself, the
114 * data page, and the GIC user page if present. Always create a mapping
115 * for the GIC user area if the GIC is present regardless of whether it
116 * is the current clocksource, in case it comes into use later on. We
117 * only map a page even though the total area is 64K, as we only need
118 * the counter registers at the start.
119 */
00578cd8 120 gic_size = mips_gic_present() ? PAGE_SIZE : 0;
a7f4df4e
AS
121 vvar_size = gic_size + PAGE_SIZE;
122 size = vvar_size + image->size;
123
0f02cfbc
PB
124 /*
125 * Find a region that's large enough for us to perform the
126 * colour-matching alignment below.
127 */
128 if (cpu_has_dc_aliases)
129 size += shm_align_mask + 1;
130
ea7e0480 131 base = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
ebb5e78c
AS
132 if (IS_ERR_VALUE(base)) {
133 ret = base;
134 goto out;
c52d0d30
DD
135 }
136
0f02cfbc
PB
137 /*
138 * If we suffer from dcache aliasing, ensure that the VDSO data page
139 * mapping is coloured the same as the kernel's mapping of that memory.
140 * This ensures that when the kernel updates the VDSO data userland
141 * will observe it without requiring cache invalidations.
142 */
143 if (cpu_has_dc_aliases) {
144 base = __ALIGN_MASK(base, shm_align_mask);
24640f23 145 base += ((unsigned long)vdso_data - gic_size) & shm_align_mask;
0f02cfbc
PB
146 }
147
a7f4df4e
AS
148 data_addr = base + gic_size;
149 vdso_addr = data_addr + PAGE_SIZE;
ebb5e78c 150
a7f4df4e 151 vma = _install_special_mapping(mm, base, vvar_size,
ebb5e78c
AS
152 VM_READ | VM_MAYREAD,
153 &vdso_vvar_mapping);
154 if (IS_ERR(vma)) {
155 ret = PTR_ERR(vma);
156 goto out;
157 }
c52d0d30 158
a7f4df4e
AS
159 /* Map GIC user page. */
160 if (gic_size) {
dfad83cb 161 gic_base = (unsigned long)mips_gic_base + MIPS_GIC_USER_OFS;
8baa6512 162 gic_pfn = PFN_DOWN(__pa(gic_base));
a7f4df4e 163
00578cd8 164 ret = io_remap_pfn_range(vma, base, gic_pfn, gic_size,
724d554a 165 pgprot_noncached(vma->vm_page_prot));
a7f4df4e
AS
166 if (ret)
167 goto out;
168 }
169
ebb5e78c 170 /* Map data page. */
a7f4df4e 171 ret = remap_pfn_range(vma, data_addr,
24640f23 172 virt_to_phys(vdso_data) >> PAGE_SHIFT,
724d554a 173 PAGE_SIZE, vma->vm_page_prot);
c52d0d30 174 if (ret)
ebb5e78c
AS
175 goto out;
176
177 /* Map VDSO image. */
178 vma = _install_special_mapping(mm, vdso_addr, image->size,
179 VM_READ | VM_EXEC |
180 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
181 &image->mapping);
182 if (IS_ERR(vma)) {
183 ret = PTR_ERR(vma);
184 goto out;
185 }
c52d0d30 186
ebb5e78c
AS
187 mm->context.vdso = (void *)vdso_addr;
188 ret = 0;
c52d0d30 189
ebb5e78c 190out:
d8ed45c5 191 mmap_write_unlock(mm);
c52d0d30
DD
192 return ret;
193}