1 /*
2 * Copyright (C) 2015 Imagination Technologies
3 * Author: Alex Smith <alex.smith@imgtec.com>
4 *
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.
9 */
10
11 #include <linux/binfmts.h>
12 #include <linux/elf.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/ioport.h>
16 #include <linux/mm.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/timekeeper_internal.h>
20
21 #include <asm/abi.h>
22 #include <asm/vdso.h>
23
24 /* Kernel-provided data used by the VDSO. */
25 static union mips_vdso_data vdso_data __page_aligned_data;
26
27 /*
28 * Mapping for the VDSO data pages. The real pages are mapped manually, as
29 * what we map and where within the area they are mapped is determined at
30 * runtime.
31 */
32 static struct page *no_pages[] = { NULL };
33 static struct vm_special_mapping vdso_vvar_mapping = {
34 .name = "[vvar]",
35 .pages = no_pages,
36 };
37
init_vdso_image(struct mips_vdso_image * image)38 static void __init init_vdso_image(struct mips_vdso_image *image)
39 {
40 unsigned long num_pages, i;
41 unsigned long data_pfn;
42
43 BUG_ON(!PAGE_ALIGNED(image->data));
44 BUG_ON(!PAGE_ALIGNED(image->size));
45
46 num_pages = image->size / PAGE_SIZE;
47
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);
51 }
52
init_vdso(void)53 static int __init init_vdso(void)
54 {
55 init_vdso_image(&vdso_image);
56
57 #ifdef CONFIG_MIPS32_O32
58 init_vdso_image(&vdso_image_o32);
59 #endif
60
61 #ifdef CONFIG_MIPS32_N32
62 init_vdso_image(&vdso_image_n32);
63 #endif
64
65 return 0;
66 }
67 subsys_initcall(init_vdso);
68
update_vsyscall(struct timekeeper * tk)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
update_vsyscall_tz(void)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
arch_setup_additional_pages(struct linux_binprm * bprm,int uses_interp)97 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
98 {
99 struct mips_vdso_image *image = current->thread.abi->vdso;
100 struct mm_struct *mm = current->mm;
101 unsigned long vvar_size, size, base, data_addr, vdso_addr;
102 struct vm_area_struct *vma;
103 int ret;
104
105 down_write(&mm->mmap_sem);
106
107 /* Map delay slot emulation page */
108 base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
109 VM_READ|VM_WRITE|VM_EXEC|
110 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
111 0);
112 if (IS_ERR_VALUE(base)) {
113 ret = base;
114 goto out;
115 }
116
117 /*
118 * Determine total area size. This includes the VDSO data itself and the
119 * data page.
120 */
121 vvar_size = PAGE_SIZE;
122 size = vvar_size + image->size;
123
124 base = get_unmapped_area(NULL, 0, size, 0, 0);
125 if (IS_ERR_VALUE(base)) {
126 ret = base;
127 goto out;
128 }
129
130 data_addr = base;
131 vdso_addr = data_addr + PAGE_SIZE;
132
133 vma = _install_special_mapping(mm, base, vvar_size,
134 VM_READ | VM_MAYREAD,
135 &vdso_vvar_mapping);
136 if (IS_ERR(vma)) {
137 ret = PTR_ERR(vma);
138 goto out;
139 }
140
141 /* Map data page. */
142 ret = remap_pfn_range(vma, data_addr,
143 virt_to_phys(&vdso_data) >> PAGE_SHIFT,
144 PAGE_SIZE, PAGE_READONLY);
145 if (ret)
146 goto out;
147
148 /* Map VDSO image. */
149 vma = _install_special_mapping(mm, vdso_addr, image->size,
150 VM_READ | VM_EXEC |
151 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
152 &image->mapping);
153 if (IS_ERR(vma)) {
154 ret = PTR_ERR(vma);
155 goto out;
156 }
157
158 mm->context.vdso = (void *)vdso_addr;
159 ret = 0;
160
161 out:
162 up_write(&mm->mmap_sem);
163 return ret;
164 }
165