0bb287ae0f0406a4284d598d08ae4fe275a61b76
[linux-2.6-microblaze.git] / arch / s390 / kernel / vdso.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * vdso setup for s390
4  *
5  *  Copyright IBM Corp. 2008
6  *  Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7  */
8
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/stddef.h>
16 #include <linux/unistd.h>
17 #include <linux/slab.h>
18 #include <linux/user.h>
19 #include <linux/elf.h>
20 #include <linux/security.h>
21 #include <linux/memblock.h>
22 #include <linux/compat.h>
23 #include <linux/binfmts.h>
24 #include <vdso/datapage.h>
25 #include <asm/asm-offsets.h>
26 #include <asm/processor.h>
27 #include <asm/mmu.h>
28 #include <asm/mmu_context.h>
29 #include <asm/sections.h>
30 #include <asm/vdso.h>
31 #include <asm/facility.h>
32 #include <asm/timex.h>
33
34 extern char vdso64_start, vdso64_end;
35 static void *vdso64_kbase = &vdso64_start;
36 static unsigned int vdso64_pages;
37 static struct page **vdso64_pagelist;
38
39 /*
40  * Should the kernel map a VDSO page into processes and pass its
41  * address down to glibc upon exec()?
42  */
43 unsigned int __read_mostly vdso_enabled = 1;
44
45 static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
46                       struct vm_area_struct *vma, struct vm_fault *vmf)
47 {
48         struct page **vdso_pagelist;
49         unsigned long vdso_pages;
50
51         vdso_pagelist = vdso64_pagelist;
52         vdso_pages = vdso64_pages;
53
54         if (vmf->pgoff >= vdso_pages)
55                 return VM_FAULT_SIGBUS;
56
57         vmf->page = vdso_pagelist[vmf->pgoff];
58         get_page(vmf->page);
59         return 0;
60 }
61
62 static int vdso_mremap(const struct vm_special_mapping *sm,
63                        struct vm_area_struct *vma)
64 {
65         current->mm->context.vdso_base = vma->vm_start;
66
67         return 0;
68 }
69
70 static const struct vm_special_mapping vdso_mapping = {
71         .name = "[vdso]",
72         .fault = vdso_fault,
73         .mremap = vdso_mremap,
74 };
75
76 static int __init vdso_setup(char *str)
77 {
78         bool enabled;
79
80         if (!kstrtobool(str, &enabled))
81                 vdso_enabled = enabled;
82         return 1;
83 }
84 __setup("vdso=", vdso_setup);
85
86 /*
87  * The vdso data page
88  */
89 static union {
90         struct vdso_data        data[CS_BASES];
91         u8                      page[PAGE_SIZE];
92 } vdso_data_store __page_aligned_data;
93 struct vdso_data *vdso_data = vdso_data_store.data;
94
95 int vdso_getcpu_init(void)
96 {
97         set_tod_programmable_field(smp_processor_id());
98         return 0;
99 }
100 early_initcall(vdso_getcpu_init); /* Must be called before SMP init */
101
102 /*
103  * This is called from binfmt_elf, we create the special vma for the
104  * vDSO and insert it into the mm struct tree
105  */
106 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
107 {
108         struct mm_struct *mm = current->mm;
109         struct vm_area_struct *vma;
110         unsigned long vdso_pages;
111         unsigned long vdso_base;
112         int rc;
113
114         if (!vdso_enabled)
115                 return 0;
116
117         if (is_compat_task())
118                 return 0;
119
120         vdso_pages = vdso64_pages;
121         /*
122          * vDSO has a problem and was disabled, just don't "enable" it for
123          * the process
124          */
125         if (vdso_pages == 0)
126                 return 0;
127
128         /*
129          * pick a base address for the vDSO in process space. We try to put
130          * it at vdso_base which is the "natural" base for it, but we might
131          * fail and end up putting it elsewhere.
132          */
133         if (mmap_write_lock_killable(mm))
134                 return -EINTR;
135         vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
136         if (IS_ERR_VALUE(vdso_base)) {
137                 rc = vdso_base;
138                 goto out_up;
139         }
140
141         /*
142          * our vma flags don't have VM_WRITE so by default, the process
143          * isn't allowed to write those pages.
144          * gdb can break that with ptrace interface, and thus trigger COW
145          * on those pages but it's then your responsibility to never do that
146          * on the "data" page of the vDSO or you'll stop getting kernel
147          * updates and your nice userland gettimeofday will be totally dead.
148          * It's fine to use that for setting breakpoints in the vDSO code
149          * pages though.
150          */
151         vma = _install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
152                                        VM_READ|VM_EXEC|
153                                        VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
154                                        &vdso_mapping);
155         if (IS_ERR(vma)) {
156                 rc = PTR_ERR(vma);
157                 goto out_up;
158         }
159
160         current->mm->context.vdso_base = vdso_base;
161         rc = 0;
162
163 out_up:
164         mmap_write_unlock(mm);
165         return rc;
166 }
167
168 static int __init vdso_init(void)
169 {
170         int i;
171
172         /* Calculate the size of the 64 bit vDSO */
173         vdso64_pages = ((&vdso64_end - &vdso64_start
174                          + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
175
176         /* Make sure pages are in the correct state */
177         vdso64_pagelist = kcalloc(vdso64_pages + 1, sizeof(struct page *),
178                                   GFP_KERNEL);
179         BUG_ON(vdso64_pagelist == NULL);
180         for (i = 0; i < vdso64_pages - 1; i++) {
181                 struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
182                 get_page(pg);
183                 vdso64_pagelist[i] = pg;
184         }
185         vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
186         vdso64_pagelist[vdso64_pages] = NULL;
187
188         get_page(virt_to_page(vdso_data));
189
190         return 0;
191 }
192 arch_initcall(vdso_init);