blob: f9dbfb14af3358e67b3ef3e922709ab38ba0bdd8 [file] [log] [blame]
David Daneyc52d0d32010-02-18 16:13:04 -08001/*
Alex Smithebb5e782015-10-21 09:54:38 +01002 * Copyright (C) 2015 Imagination Technologies
3 * Author: Alex Smith <alex.smith@imgtec.com>
David Daneyc52d0d32010-02-18 16:13:04 -08004 *
Alex Smithebb5e782015-10-21 09:54:38 +01005 * 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 Daneyc52d0d32010-02-18 16:13:04 -08009 */
10
David Daneyc52d0d32010-02-18 16:13:04 -080011#include <linux/binfmts.h>
12#include <linux/elf.h>
Alex Smithebb5e782015-10-21 09:54:38 +010013#include <linux/err.h>
14#include <linux/init.h>
Alex Smitha7f4df42015-10-21 09:57:44 +010015#include <linux/ioport.h>
16#include <linux/irqchip/mips-gic.h>
Alex Smithebb5e782015-10-21 09:54:38 +010017#include <linux/mm.h>
18#include <linux/sched.h>
19#include <linux/slab.h>
Alex Smitha7f4df42015-10-21 09:57:44 +010020#include <linux/timekeeper_internal.h>
David Daneyc52d0d32010-02-18 16:13:04 -080021
Alex Smithebb5e782015-10-21 09:54:38 +010022#include <asm/abi.h>
David Daneyc52d0d32010-02-18 16:13:04 -080023#include <asm/vdso.h>
Alex Smithebb5e782015-10-21 09:54:38 +010024
25/* Kernel-provided data used by the VDSO. */
26static union mips_vdso_data vdso_data __page_aligned_data;
David Daneyc52d0d32010-02-18 16:13:04 -080027
28/*
Alex Smitha7f4df42015-10-21 09:57:44 +010029 * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as
Alex Smithebb5e782015-10-21 09:54:38 +010030 * what we map and where within the area they are mapped is determined at
31 * runtime.
David Daneyc52d0d32010-02-18 16:13:04 -080032 */
Alex Smithebb5e782015-10-21 09:54:38 +010033static struct page *no_pages[] = { NULL };
34static struct vm_special_mapping vdso_vvar_mapping = {
35 .name = "[vvar]",
36 .pages = no_pages,
37};
David Daneyc52d0d32010-02-18 16:13:04 -080038
Alex Smithebb5e782015-10-21 09:54:38 +010039static void __init init_vdso_image(struct mips_vdso_image *image)
David Daneyc52d0d32010-02-18 16:13:04 -080040{
Alex Smithebb5e782015-10-21 09:54:38 +010041 unsigned long num_pages, i;
James Hogan554af0c2016-09-07 13:37:01 +010042 unsigned long data_pfn;
Alex Smithebb5e782015-10-21 09:54:38 +010043
44 BUG_ON(!PAGE_ALIGNED(image->data));
45 BUG_ON(!PAGE_ALIGNED(image->size));
46
47 num_pages = image->size / PAGE_SIZE;
48
James Hogan554af0c2016-09-07 13:37:01 +010049 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 Daneyc52d0d32010-02-18 16:13:04 -080052}
53
54static int __init init_vdso(void)
55{
Alex Smithebb5e782015-10-21 09:54:38 +010056 init_vdso_image(&vdso_image);
David Daneyc52d0d32010-02-18 16:13:04 -080057
Alex Smithebb5e782015-10-21 09:54:38 +010058#ifdef CONFIG_MIPS32_O32
59 init_vdso_image(&vdso_image_o32);
David Daneyc52d0d32010-02-18 16:13:04 -080060#endif
61
Alex Smithebb5e782015-10-21 09:54:38 +010062#ifdef CONFIG_MIPS32_N32
63 init_vdso_image(&vdso_image_n32);
64#endif
David Daneyc52d0d32010-02-18 16:13:04 -080065
David Daneyc52d0d32010-02-18 16:13:04 -080066 return 0;
67}
David Daney1ed84532010-06-16 15:00:28 -070068subsys_initcall(init_vdso);
David Daneyc52d0d32010-02-18 16:13:04 -080069
Alex Smitha7f4df42015-10-21 09:57:44 +010070void 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
90void 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 Daneyc52d0d32010-02-18 16:13:04 -080098int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
99{
Alex Smithebb5e782015-10-21 09:54:38 +0100100 struct mips_vdso_image *image = current->thread.abi->vdso;
David Daneyc52d0d32010-02-18 16:13:04 -0800101 struct mm_struct *mm = current->mm;
Alex Smitha7f4df42015-10-21 09:57:44 +0100102 unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr;
Alex Smithebb5e782015-10-21 09:54:38 +0100103 struct vm_area_struct *vma;
Alex Smitha7f4df42015-10-21 09:57:44 +0100104 struct resource gic_res;
Alex Smithebb5e782015-10-21 09:54:38 +0100105 int ret;
David Daneyc52d0d32010-02-18 16:13:04 -0800106
Michal Hocko69048172016-05-23 16:25:54 -0700107 if (down_write_killable(&mm->mmap_sem))
108 return -EINTR;
David Daneyc52d0d32010-02-18 16:13:04 -0800109
Paul Burton432c6ba2016-07-08 11:06:19 +0100110 /* Map delay slot emulation page */
111 base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
112 VM_READ|VM_WRITE|VM_EXEC|
113 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
114 0);
115 if (IS_ERR_VALUE(base)) {
116 ret = base;
117 goto out;
118 }
119
Alex Smitha7f4df42015-10-21 09:57:44 +0100120 /*
121 * Determine total area size. This includes the VDSO data itself, the
122 * data page, and the GIC user page if present. Always create a mapping
123 * for the GIC user area if the GIC is present regardless of whether it
124 * is the current clocksource, in case it comes into use later on. We
125 * only map a page even though the total area is 64K, as we only need
126 * the counter registers at the start.
127 */
128 gic_size = gic_present ? PAGE_SIZE : 0;
129 vvar_size = gic_size + PAGE_SIZE;
130 size = vvar_size + image->size;
131
132 base = get_unmapped_area(NULL, 0, size, 0, 0);
Alex Smithebb5e782015-10-21 09:54:38 +0100133 if (IS_ERR_VALUE(base)) {
134 ret = base;
135 goto out;
David Daneyc52d0d32010-02-18 16:13:04 -0800136 }
137
Alex Smitha7f4df42015-10-21 09:57:44 +0100138 data_addr = base + gic_size;
139 vdso_addr = data_addr + PAGE_SIZE;
David Daneyc52d0d32010-02-18 16:13:04 -0800140
Alex Smitha7f4df42015-10-21 09:57:44 +0100141 vma = _install_special_mapping(mm, base, vvar_size,
Alex Smithebb5e782015-10-21 09:54:38 +0100142 VM_READ | VM_MAYREAD,
143 &vdso_vvar_mapping);
144 if (IS_ERR(vma)) {
145 ret = PTR_ERR(vma);
146 goto out;
147 }
148
Alex Smitha7f4df42015-10-21 09:57:44 +0100149 /* Map GIC user page. */
150 if (gic_size) {
151 ret = gic_get_usm_range(&gic_res);
152 if (ret)
153 goto out;
154
155 ret = io_remap_pfn_range(vma, base,
156 gic_res.start >> PAGE_SHIFT,
157 gic_size,
158 pgprot_noncached(PAGE_READONLY));
159 if (ret)
160 goto out;
161 }
162
Alex Smithebb5e782015-10-21 09:54:38 +0100163 /* Map data page. */
Alex Smitha7f4df42015-10-21 09:57:44 +0100164 ret = remap_pfn_range(vma, data_addr,
Alex Smithebb5e782015-10-21 09:54:38 +0100165 virt_to_phys(&vdso_data) >> PAGE_SHIFT,
166 PAGE_SIZE, PAGE_READONLY);
David Daneyc52d0d32010-02-18 16:13:04 -0800167 if (ret)
Alex Smithebb5e782015-10-21 09:54:38 +0100168 goto out;
David Daneyc52d0d32010-02-18 16:13:04 -0800169
Alex Smithebb5e782015-10-21 09:54:38 +0100170 /* Map VDSO image. */
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 }
David Daneyc52d0d32010-02-18 16:13:04 -0800179
Alex Smithebb5e782015-10-21 09:54:38 +0100180 mm->context.vdso = (void *)vdso_addr;
181 ret = 0;
182
183out:
David Daneyc52d0d32010-02-18 16:13:04 -0800184 up_write(&mm->mmap_sem);
185 return ret;
186}