xref: /freebsd/sys/compat/linux/linux_vdso.c (revision c47116e90951621196c72b7def866cadfdabf80e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013-2021 Dmitry Chagin <dchagin@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
30 #define	__ELF_WORD_SIZE	32
31 #else
32 #define	__ELF_WORD_SIZE	64
33 #endif
34 
35 #include <sys/param.h>
36 #include <sys/elf.h>
37 #include <sys/imgact.h>
38 #include <sys/lock.h>
39 #include <sys/proc.h>
40 #include <sys/rwlock.h>
41 #include <sys/sysent.h>
42 
43 #include <vm/pmap.h>
44 #include <vm/vm_extern.h>
45 #include <vm/vm_map.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_pager.h>
49 #include <vm/vm_param.h>
50 
51 #include <compat/linux/linux_vdso.h>
52 
53 SLIST_HEAD(, linux_vdso_sym) __elfN(linux_vdso_syms) =
54     SLIST_HEAD_INITIALIZER(__elfN(linux_vdso_syms));
55 
56 void
57 __elfN(linux_vdso_sym_init)(struct linux_vdso_sym *s)
58 {
59 
60 	SLIST_INSERT_HEAD(&__elfN(linux_vdso_syms), s, sym);
61 }
62 
63 vm_object_t
64 __elfN(linux_shared_page_init)(char **mapping, vm_size_t size)
65 {
66 	vm_page_t m;
67 	vm_object_t obj;
68 	vm_offset_t addr;
69 	size_t n, pages;
70 
71 	pages = size / PAGE_SIZE;
72 
73 	addr = kva_alloc(size);
74 	obj = vm_pager_allocate(OBJT_PHYS, 0, size,
75 	    VM_PROT_DEFAULT, 0, NULL);
76 	VM_OBJECT_WLOCK(obj);
77 	for (n = 0; n < pages; n++) {
78 		m = vm_page_grab(obj, n,
79 		    VM_ALLOC_ZERO);
80 		vm_page_valid(m);
81 		vm_page_xunbusy(m);
82 		pmap_qenter(addr + n * PAGE_SIZE, &m, 1);
83 	}
84 	VM_OBJECT_WUNLOCK(obj);
85 	*mapping = (char *)addr;
86 	return (obj);
87 }
88 
89 void
90 __elfN(linux_shared_page_fini)(vm_object_t obj, void *mapping,
91     vm_size_t size)
92 {
93 	vm_offset_t va;
94 
95 	va = (vm_offset_t)mapping;
96 	pmap_qremove(va, size / PAGE_SIZE);
97 	kva_free(va, size);
98 	vm_object_deallocate(obj);
99 }
100 
101 void
102 __elfN(linux_vdso_fixup)(char *base, vm_offset_t offset)
103 {
104 	struct linux_vdso_sym *lsym;
105 	const Elf_Shdr *shdr;
106 	Elf_Ehdr *ehdr;
107 	Elf_Sym *dsym, *sym;
108 	char *strtab, *symname;
109 	int i, symcnt;
110 
111 	ehdr = (Elf_Ehdr *)base;
112 
113 	MPASS(IS_ELF(*ehdr));
114 	MPASS(ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS);
115 	MPASS(ehdr->e_ident[EI_DATA] == ELF_TARG_DATA);
116 	MPASS(ehdr->e_ident[EI_VERSION] == EV_CURRENT);
117 	MPASS(ehdr->e_shentsize == sizeof(Elf_Shdr));
118 	MPASS(ehdr->e_shoff != 0);
119 	MPASS(ehdr->e_type == ET_DYN);
120 
121 	shdr = (const Elf_Shdr *)(base + ehdr->e_shoff);
122 
123 	dsym = NULL;
124 	for (i = 0; i < ehdr->e_shnum; i++) {
125 		if (shdr[i].sh_size == 0)
126 			continue;
127 		if (shdr[i].sh_type == SHT_DYNSYM) {
128 			dsym = (Elf_Sym *)(base + shdr[i].sh_offset);
129 			strtab = base + shdr[shdr[i].sh_link].sh_offset;
130 			symcnt = shdr[i].sh_size / sizeof(*dsym);
131 			break;
132 		}
133 	}
134 	MPASS(dsym != NULL);
135 
136 	ehdr->e_ident[EI_OSABI] = ELFOSABI_LINUX;
137 
138 	/*
139 	 * VDSO is readonly mapped to the process VA and
140 	 * can't be relocated by rtld.
141 	 */
142 	SLIST_FOREACH(lsym, &__elfN(linux_vdso_syms), sym) {
143 		for (i = 0, sym = dsym; i < symcnt; i++, sym++) {
144 			symname = strtab + sym->st_name;
145 			if (strncmp(lsym->symname, symname, lsym->size) == 0) {
146 				sym->st_value += offset;
147 				*lsym->ptr = sym->st_value;
148 				break;
149 
150 			}
151 		}
152 	}
153 }
154 
155 int
156 linux_map_vdso(struct proc *p, vm_object_t obj, vm_offset_t base,
157     vm_offset_t size, struct image_params *imgp)
158 {
159 	struct vmspace *vmspace;
160 	vm_map_t map;
161 	int error;
162 
163 	MPASS((imgp->sysent->sv_flags & SV_ABI_MASK) == SV_ABI_LINUX);
164 	MPASS(obj != NULL);
165 
166 	vmspace = p->p_vmspace;
167 	map = &vmspace->vm_map;
168 
169 	vm_object_reference(obj);
170 	error = vm_map_fixed(map, obj, 0, base, size,
171 	    VM_PROT_READ | VM_PROT_EXECUTE,
172 	    VM_PROT_READ | VM_PROT_EXECUTE,
173 	    MAP_INHERIT_SHARE | MAP_ACC_NO_CHARGE);
174 	if (error != KERN_SUCCESS) {
175 		vm_object_deallocate(obj);
176 		return (vm_mmap_to_errno(error));
177 	}
178 	return (0);
179 }
180