s390/vdso: remove superfluous check
[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          * pick a base address for the vDSO in process space. We try to put
123          * it at vdso_base which is the "natural" base for it, but we might
124          * fail and end up putting it elsewhere.
125          */
126         if (mmap_write_lock_killable(mm))
127                 return -EINTR;
128         vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
129         if (IS_ERR_VALUE(vdso_base)) {
130                 rc = vdso_base;
131                 goto out_up;
132         }
133
134         /*
135          * our vma flags don't have VM_WRITE so by default, the process
136          * isn't allowed to write those pages.
137          * gdb can break that with ptrace interface, and thus trigger COW
138          * on those pages but it's then your responsibility to never do that
139          * on the "data" page of the vDSO or you'll stop getting kernel
140          * updates and your nice userland gettimeofday will be totally dead.
141          * It's fine to use that for setting breakpoints in the vDSO code
142          * pages though.
143          */
144         vma = _install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
145                                        VM_READ|VM_EXEC|
146                                        VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
147                                        &vdso_mapping);
148         if (IS_ERR(vma)) {
149                 rc = PTR_ERR(vma);
150                 goto out_up;
151         }
152
153         current->mm->context.vdso_base = vdso_base;
154         rc = 0;
155
156 out_up:
157         mmap_write_unlock(mm);
158         return rc;
159 }
160
161 static int __init vdso_init(void)
162 {
163         int i;
164
165         /* Calculate the size of the 64 bit vDSO */
166         vdso64_pages = ((&vdso64_end - &vdso64_start) >> PAGE_SHIFT) + 1;
167
168         /* Make sure pages are in the correct state */
169         vdso64_pagelist = kcalloc(vdso64_pages + 1, sizeof(struct page *),
170                                   GFP_KERNEL);
171         if (!vdso64_pagelist) {
172                 vdso_enabled = 0;
173                 return -ENOMEM;
174         }
175         for (i = 0; i < vdso64_pages - 1; i++) {
176                 struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
177                 get_page(pg);
178                 vdso64_pagelist[i] = pg;
179         }
180         vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
181         vdso64_pagelist[vdso64_pages] = NULL;
182
183         get_page(virt_to_page(vdso_data));
184
185         return 0;
186 }
187 arch_initcall(vdso_init);