xref: /linux/arch/s390/kernel/vdso.c (revision 5b193d6a8973655f19de87a7873ce31dee2d5fe6)
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/binfmts.h>
10 #include <linux/elf.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/smp.h>
18 #include <linux/random.h>
19 #include <linux/vdso_datastore.h>
20 #include <vdso/datapage.h>
21 #include <asm/alternative.h>
22 #include <asm/vdso.h>
23 
24 extern char vdso_start[], vdso_end[];
25 
26 static int vdso_mremap(const struct vm_special_mapping *sm,
27 		       struct vm_area_struct *vma)
28 {
29 	current->mm->context.vdso_base = vma->vm_start;
30 	return 0;
31 }
32 
33 static struct vm_special_mapping vdso_mapping = {
34 	.name = "[vdso]",
35 	.mremap = vdso_mremap,
36 };
37 
38 int vdso_getcpu_init(void)
39 {
40 	set_tod_programmable_field(smp_processor_id());
41 	return 0;
42 }
43 early_initcall(vdso_getcpu_init); /* Must be called before SMP init */
44 
45 static int map_vdso(unsigned long addr, unsigned long vdso_mapping_len)
46 {
47 	unsigned long vvar_start, vdso_text_start, vdso_text_len;
48 	struct mm_struct *mm = current->mm;
49 	struct vm_area_struct *vma;
50 	int rc;
51 
52 	BUILD_BUG_ON(VDSO_NR_PAGES != __VDSO_PAGES);
53 	if (mmap_write_lock_killable(mm))
54 		return -EINTR;
55 
56 	vdso_text_len = vdso_end - vdso_start;
57 	vvar_start = get_unmapped_area(NULL, addr, vdso_mapping_len, 0, 0);
58 	rc = vvar_start;
59 	if (IS_ERR_VALUE(vvar_start))
60 		goto out;
61 	vma = vdso_install_vvar_mapping(mm, vvar_start);
62 	rc = PTR_ERR(vma);
63 	if (IS_ERR(vma))
64 		goto out;
65 	vdso_text_start = vvar_start + VDSO_NR_PAGES * PAGE_SIZE;
66 	/* VM_MAYWRITE for COW so gdb can set breakpoints */
67 	vma = _install_special_mapping(mm, vdso_text_start, vdso_text_len,
68 				       VM_READ|VM_EXEC|VM_SEALED_SYSMAP|
69 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
70 				       &vdso_mapping);
71 	if (IS_ERR(vma)) {
72 		do_munmap(mm, vvar_start, PAGE_SIZE, NULL);
73 		rc = PTR_ERR(vma);
74 	} else {
75 		current->mm->context.vdso_base = vdso_text_start;
76 		rc = 0;
77 	}
78 out:
79 	mmap_write_unlock(mm);
80 	return rc;
81 }
82 
83 static unsigned long vdso_addr(unsigned long start, unsigned long len)
84 {
85 	unsigned long addr, end, offset;
86 
87 	/*
88 	 * Round up the start address. It can start out unaligned as a result
89 	 * of stack start randomization.
90 	 */
91 	start = PAGE_ALIGN(start);
92 
93 	/* Round the lowest possible end address up to a PMD boundary. */
94 	end = (start + len + PMD_SIZE - 1) & PMD_MASK;
95 	if (end >= VDSO_BASE)
96 		end = VDSO_BASE;
97 	end -= len;
98 
99 	if (end > start) {
100 		offset = get_random_u32_below(((end - start) >> PAGE_SHIFT) + 1);
101 		addr = start + (offset << PAGE_SHIFT);
102 	} else {
103 		addr = start;
104 	}
105 	return addr;
106 }
107 
108 unsigned long vdso_text_size(void)
109 {
110 	return PAGE_ALIGN(vdso_end - vdso_start);
111 }
112 
113 unsigned long vdso_size(void)
114 {
115 	return vdso_text_size() + VDSO_NR_PAGES * PAGE_SIZE;
116 }
117 
118 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
119 {
120 	unsigned long addr = VDSO_BASE;
121 	unsigned long size = vdso_size();
122 
123 	if (current->flags & PF_RANDOMIZE)
124 		addr = vdso_addr(current->mm->start_stack + PAGE_SIZE, size);
125 	return map_vdso(addr, size);
126 }
127 
128 static struct page ** __init vdso_setup_pages(void *start, void *end)
129 {
130 	int pages = (end - start) >> PAGE_SHIFT;
131 	struct page **pagelist;
132 	int i;
133 
134 	pagelist = kzalloc_objs(struct page *, pages + 1);
135 	if (!pagelist)
136 		panic("%s: Cannot allocate page list for VDSO", __func__);
137 	for (i = 0; i < pages; i++)
138 		pagelist[i] = virt_to_page(start + i * PAGE_SIZE);
139 	return pagelist;
140 }
141 
142 static void vdso_apply_alternatives(void)
143 {
144 	const struct elf64_shdr *alt, *shdr;
145 	struct alt_instr *start, *end;
146 	const struct elf64_hdr *hdr;
147 
148 	hdr = (struct elf64_hdr *)vdso_start;
149 	shdr = (void *)hdr + hdr->e_shoff;
150 	alt = find_section(hdr, shdr, ".altinstructions");
151 	if (!alt)
152 		return;
153 	start = (void *)hdr + alt->sh_offset;
154 	end = (void *)hdr + alt->sh_offset + alt->sh_size;
155 	apply_alternatives(start, end);
156 }
157 
158 static int __init vdso_init(void)
159 {
160 	vdso_apply_alternatives();
161 	vdso_mapping.pages = vdso_setup_pages(vdso_start, vdso_end);
162 	return 0;
163 }
164 arch_initcall(vdso_init);
165