2 * Copyright (c) 2013-2021 Dmitry Chagin <dchagin@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer
9 * in this position and unchanged.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include "opt_compat.h"
31 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
32 #define __ELF_WORD_SIZE 32
34 #define __ELF_WORD_SIZE 64
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/imgact.h>
41 #include <sys/kernel.h>
43 #include <sys/rwlock.h>
44 #include <sys/queue.h>
45 #include <sys/sysent.h>
47 #include <vm/vm_param.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pager.h>
55 #include <compat/linux/linux_vdso.h>
57 SLIST_HEAD(, linux_vdso_sym) __elfN(linux_vdso_syms) =
58 SLIST_HEAD_INITIALIZER(__elfN(linux_vdso_syms));
61 __elfN(linux_vdso_sym_init)(struct linux_vdso_sym *s)
64 SLIST_INSERT_HEAD(&__elfN(linux_vdso_syms), s, sym);
68 __elfN(linux_shared_page_init)(char **mapping, vm_size_t size)
75 pages = size / PAGE_SIZE;
77 addr = kva_alloc(size);
78 obj = vm_pager_allocate(OBJT_PHYS, 0, size,
79 VM_PROT_DEFAULT, 0, NULL);
81 for (n = 0; n < pages; n++) {
82 m = vm_page_grab(obj, n,
86 pmap_qenter(addr + n * PAGE_SIZE, &m, 1);
88 VM_OBJECT_WUNLOCK(obj);
89 *mapping = (char *)addr;
94 __elfN(linux_shared_page_fini)(vm_object_t obj, void *mapping,
99 va = (vm_offset_t)mapping;
100 pmap_qremove(va, size / PAGE_SIZE);
102 vm_object_deallocate(obj);
106 __elfN(linux_vdso_fixup)(char *base, vm_offset_t offset)
108 struct linux_vdso_sym *lsym;
109 const Elf_Shdr *shdr;
112 char *strtab, *symname;
115 ehdr = __DECONST(Elf_Ehdr *, base);
117 MPASS(IS_ELF(*ehdr));
118 MPASS(ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS);
119 MPASS(ehdr->e_ident[EI_DATA] == ELF_TARG_DATA);
120 MPASS(ehdr->e_ident[EI_VERSION] == EV_CURRENT);
121 MPASS(ehdr->e_shentsize == sizeof(Elf_Shdr));
122 MPASS(ehdr->e_shoff != 0);
123 MPASS(ehdr->e_type == ET_DYN);
125 shdr = (const Elf_Shdr *)(base + ehdr->e_shoff);
128 for (i = 0; i < ehdr->e_shnum; i++) {
129 if (shdr[i].sh_size == 0)
131 if (shdr[i].sh_type == SHT_DYNSYM) {
132 dsym = (Elf_Sym *)(base + shdr[i].sh_offset);
133 strtab = base + shdr[shdr[i].sh_link].sh_offset;
134 symcnt = shdr[i].sh_size / sizeof(*dsym);
140 ehdr->e_ident[EI_OSABI] = ELFOSABI_LINUX;
142 * VDSO is readonly mapped to the process VA and
143 * can't be relocated by rtld.
145 SLIST_FOREACH(lsym, &__elfN(linux_vdso_syms), sym) {
146 for (i = 0, sym = dsym; i < symcnt; i++, sym++) {
147 symname = strtab + sym->st_name;
148 if (strncmp(lsym->symname, symname, lsym->size) == 0) {
149 sym->st_value += offset;
150 *lsym->ptr = sym->st_value;
159 linux_map_vdso(struct proc *p, vm_object_t obj, vm_offset_t base,
160 vm_offset_t size, struct image_params *imgp)
162 struct vmspace *vmspace;
166 MPASS((imgp->sysent->sv_flags & SV_ABI_MASK) == SV_ABI_LINUX);
169 vmspace = p->p_vmspace;
170 map = &vmspace->vm_map;
172 vm_object_reference(obj);
173 error = vm_map_fixed(map, obj, 0, base, size,
174 VM_PROT_READ | VM_PROT_EXECUTE,
175 VM_PROT_READ | VM_PROT_EXECUTE,
176 MAP_INHERIT_SHARE | MAP_ACC_NO_CHARGE);
177 if (error != KERN_SUCCESS) {
178 vm_object_deallocate(obj);
179 return (vm_mmap_to_errno(error));