2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2013-2021 Dmitry Chagin <dchagin@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
29 #define __ELF_WORD_SIZE 32
31 #define __ELF_WORD_SIZE 64
34 #include <sys/param.h>
36 #include <sys/imgact.h>
39 #include <sys/rwlock.h>
40 #include <sys/sysent.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>
50 #include <compat/linux/linux_vdso.h>
52 SLIST_HEAD(, linux_vdso_sym) __elfN(linux_vdso_syms) =
53 SLIST_HEAD_INITIALIZER(__elfN(linux_vdso_syms));
56 __elfN(linux_vdso_sym_init)(struct linux_vdso_sym *s)
59 SLIST_INSERT_HEAD(&__elfN(linux_vdso_syms), s, sym);
63 __elfN(linux_shared_page_init)(char **mapping, vm_size_t size)
70 pages = size / PAGE_SIZE;
72 addr = kva_alloc(size);
73 obj = vm_pager_allocate(OBJT_PHYS, 0, size,
74 VM_PROT_DEFAULT, 0, NULL);
76 for (n = 0; n < pages; n++) {
77 m = vm_page_grab(obj, n,
81 pmap_qenter(addr + n * PAGE_SIZE, &m, 1);
83 VM_OBJECT_WUNLOCK(obj);
84 *mapping = (char *)addr;
89 __elfN(linux_shared_page_fini)(vm_object_t obj, void *mapping,
94 va = (vm_offset_t)mapping;
95 pmap_qremove(va, size / PAGE_SIZE);
97 vm_object_deallocate(obj);
101 __elfN(linux_vdso_fixup)(char *base, vm_offset_t offset)
103 struct linux_vdso_sym *lsym;
104 const Elf_Shdr *shdr;
107 char *strtab, *symname;
110 ehdr = (Elf_Ehdr *)base;
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);
120 shdr = (const Elf_Shdr *)(base + ehdr->e_shoff);
123 for (i = 0; i < ehdr->e_shnum; i++) {
124 if (shdr[i].sh_size == 0)
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);
135 ehdr->e_ident[EI_OSABI] = ELFOSABI_LINUX;
138 * VDSO is readonly mapped to the process VA and
139 * can't be relocated by rtld.
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;
155 linux_map_vdso(struct proc *p, vm_object_t obj, vm_offset_t base,
156 vm_offset_t size, struct image_params *imgp)
158 struct vmspace *vmspace;
162 MPASS((imgp->sysent->sv_flags & SV_ABI_MASK) == SV_ABI_LINUX);
165 vmspace = p->p_vmspace;
166 map = &vmspace->vm_map;
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));