xref: /freebsd/sys/compat/linux/linux_vdso.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
29 #define	__ELF_WORD_SIZE	32
30 #else
31 #define	__ELF_WORD_SIZE	64
32 #endif
33 
34 #include <sys/param.h>
35 #include <sys/elf.h>
36 #include <sys/imgact.h>
37 #include <sys/lock.h>
38 #include <sys/proc.h>
39 #include <sys/rwlock.h>
40 #include <sys/sysent.h>
41 
42 #include <vm/pmap.h>
43 #include <vm/vm_extern.h>
44 #include <vm/vm_map.h>
45 #include <vm/vm_object.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_pager.h>
48 #include <vm/vm_param.h>
49 
50 #include <compat/linux/linux_vdso.h>
51 
52 SLIST_HEAD(, linux_vdso_sym) __elfN(linux_vdso_syms) =
53     SLIST_HEAD_INITIALIZER(__elfN(linux_vdso_syms));
54 
55 void
56 __elfN(linux_vdso_sym_init)(struct linux_vdso_sym *s)
57 {
58 
59 	SLIST_INSERT_HEAD(&__elfN(linux_vdso_syms), s, sym);
60 }
61 
62 vm_object_t
63 __elfN(linux_shared_page_init)(char **mapping, vm_size_t size)
64 {
65 	vm_page_t m;
66 	vm_object_t obj;
67 	vm_offset_t addr;
68 	size_t n, pages;
69 
70 	pages = size / PAGE_SIZE;
71 
72 	addr = kva_alloc(size);
73 	obj = vm_pager_allocate(OBJT_PHYS, 0, size,
74 	    VM_PROT_DEFAULT, 0, NULL);
75 	VM_OBJECT_WLOCK(obj);
76 	for (n = 0; n < pages; n++) {
77 		m = vm_page_grab(obj, n,
78 		    VM_ALLOC_ZERO);
79 		vm_page_valid(m);
80 		vm_page_xunbusy(m);
81 		pmap_qenter(addr + n * PAGE_SIZE, &m, 1);
82 	}
83 	VM_OBJECT_WUNLOCK(obj);
84 	*mapping = (char *)addr;
85 	return (obj);
86 }
87 
88 void
89 __elfN(linux_shared_page_fini)(vm_object_t obj, void *mapping,
90     vm_size_t size)
91 {
92 	vm_offset_t va;
93 
94 	va = (vm_offset_t)mapping;
95 	pmap_qremove(va, size / PAGE_SIZE);
96 	kva_free(va, size);
97 	vm_object_deallocate(obj);
98 }
99 
100 void
101 __elfN(linux_vdso_fixup)(char *base, vm_offset_t offset)
102 {
103 	struct linux_vdso_sym *lsym;
104 	const Elf_Shdr *shdr;
105 	Elf_Ehdr *ehdr;
106 	Elf_Sym *dsym, *sym;
107 	char *strtab, *symname;
108 	int i, symcnt;
109 
110 	ehdr = (Elf_Ehdr *)base;
111 
112 	MPASS(IS_ELF(*ehdr));
113 	MPASS(ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS);
114 	MPASS(ehdr->e_ident[EI_DATA] == ELF_TARG_DATA);
115 	MPASS(ehdr->e_ident[EI_VERSION] == EV_CURRENT);
116 	MPASS(ehdr->e_shentsize == sizeof(Elf_Shdr));
117 	MPASS(ehdr->e_shoff != 0);
118 	MPASS(ehdr->e_type == ET_DYN);
119 
120 	shdr = (const Elf_Shdr *)(base + ehdr->e_shoff);
121 
122 	dsym = NULL;
123 	for (i = 0; i < ehdr->e_shnum; i++) {
124 		if (shdr[i].sh_size == 0)
125 			continue;
126 		if (shdr[i].sh_type == SHT_DYNSYM) {
127 			dsym = (Elf_Sym *)(base + shdr[i].sh_offset);
128 			strtab = base + shdr[shdr[i].sh_link].sh_offset;
129 			symcnt = shdr[i].sh_size / sizeof(*dsym);
130 			break;
131 		}
132 	}
133 	MPASS(dsym != NULL);
134 
135 	ehdr->e_ident[EI_OSABI] = ELFOSABI_LINUX;
136 
137 	/*
138 	 * VDSO is readonly mapped to the process VA and
139 	 * can't be relocated by rtld.
140 	 */
141 	SLIST_FOREACH(lsym, &__elfN(linux_vdso_syms), sym) {
142 		for (i = 0, sym = dsym; i < symcnt; i++, sym++) {
143 			symname = strtab + sym->st_name;
144 			if (strncmp(lsym->symname, symname, lsym->size) == 0) {
145 				sym->st_value += offset;
146 				*lsym->ptr = sym->st_value;
147 				break;
148 
149 			}
150 		}
151 	}
152 }
153 
154 int
155 linux_map_vdso(struct proc *p, vm_object_t obj, vm_offset_t base,
156     vm_offset_t size, struct image_params *imgp)
157 {
158 	struct vmspace *vmspace;
159 	vm_map_t map;
160 	int error;
161 
162 	MPASS((imgp->sysent->sv_flags & SV_ABI_MASK) == SV_ABI_LINUX);
163 	MPASS(obj != NULL);
164 
165 	vmspace = p->p_vmspace;
166 	map = &vmspace->vm_map;
167 
168 	vm_object_reference(obj);
169 	error = vm_map_fixed(map, obj, 0, base, size,
170 	    VM_PROT_READ | VM_PROT_EXECUTE,
171 	    VM_PROT_READ | VM_PROT_EXECUTE,
172 	    MAP_INHERIT_SHARE | MAP_ACC_NO_CHARGE);
173 	if (error != KERN_SUCCESS) {
174 		vm_object_deallocate(obj);
175 		return (vm_mmap_to_errno(error));
176 	}
177 	return (0);
178 }
179