1bdc37934SDmitry Chagin /*-
2*c47116e9SDmitry Chagin * SPDX-License-Identifier: BSD-2-Clause
3*c47116e9SDmitry Chagin *
41ca6b15bSDmitry Chagin * Copyright (c) 2013-2021 Dmitry Chagin <dchagin@FreeBSD.org>
5bdc37934SDmitry Chagin *
6bdc37934SDmitry Chagin * Redistribution and use in source and binary forms, with or without
7bdc37934SDmitry Chagin * modification, are permitted provided that the following conditions
8bdc37934SDmitry Chagin * are met:
9bdc37934SDmitry Chagin * 1. Redistributions of source code must retain the above copyright
10*c47116e9SDmitry Chagin * notice, this list of conditions and the following disclaimer.
11bdc37934SDmitry Chagin * 2. Redistributions in binary form must reproduce the above copyright
12bdc37934SDmitry Chagin * notice, this list of conditions and the following disclaimer in the
13bdc37934SDmitry Chagin * documentation and/or other materials provided with the distribution.
14bdc37934SDmitry Chagin *
15*c47116e9SDmitry Chagin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*c47116e9SDmitry Chagin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*c47116e9SDmitry Chagin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*c47116e9SDmitry Chagin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*c47116e9SDmitry Chagin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*c47116e9SDmitry Chagin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*c47116e9SDmitry Chagin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*c47116e9SDmitry Chagin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*c47116e9SDmitry Chagin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*c47116e9SDmitry Chagin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*c47116e9SDmitry Chagin * SUCH DAMAGE.
26bdc37934SDmitry Chagin */
27bdc37934SDmitry Chagin
2836204c30SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
29bdc37934SDmitry Chagin #define __ELF_WORD_SIZE 32
3036204c30SDmitry Chagin #else
3136204c30SDmitry Chagin #define __ELF_WORD_SIZE 64
3236204c30SDmitry Chagin #endif
33bdc37934SDmitry Chagin
34bdc37934SDmitry Chagin #include <sys/param.h>
35bdc37934SDmitry Chagin #include <sys/elf.h>
369931033bSDmitry Chagin #include <sys/imgact.h>
37d8e53d94SDmitry Chagin #include <sys/lock.h>
389931033bSDmitry Chagin #include <sys/proc.h>
39bdc37934SDmitry Chagin #include <sys/rwlock.h>
40bdc37934SDmitry Chagin #include <sys/sysent.h>
41bdc37934SDmitry Chagin
42bdc37934SDmitry Chagin #include <vm/pmap.h>
43bdc37934SDmitry Chagin #include <vm/vm_extern.h>
44bdc37934SDmitry Chagin #include <vm/vm_map.h>
45bdc37934SDmitry Chagin #include <vm/vm_object.h>
46bdc37934SDmitry Chagin #include <vm/vm_page.h>
47bdc37934SDmitry Chagin #include <vm/vm_pager.h>
48d8e53d94SDmitry Chagin #include <vm/vm_param.h>
49bdc37934SDmitry Chagin
50bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
51bdc37934SDmitry Chagin
52bdc37934SDmitry Chagin SLIST_HEAD(, linux_vdso_sym) __elfN(linux_vdso_syms) =
53bdc37934SDmitry Chagin SLIST_HEAD_INITIALIZER(__elfN(linux_vdso_syms));
54bdc37934SDmitry Chagin
55bdc37934SDmitry Chagin void
__elfN(linux_vdso_sym_init)56bdc37934SDmitry Chagin __elfN(linux_vdso_sym_init)(struct linux_vdso_sym *s)
57bdc37934SDmitry Chagin {
58bdc37934SDmitry Chagin
59bdc37934SDmitry Chagin SLIST_INSERT_HEAD(&__elfN(linux_vdso_syms), s, sym);
60bdc37934SDmitry Chagin }
61bdc37934SDmitry Chagin
62bdc37934SDmitry Chagin vm_object_t
__elfN(linux_shared_page_init)639931033bSDmitry Chagin __elfN(linux_shared_page_init)(char **mapping, vm_size_t size)
64bdc37934SDmitry Chagin {
65bdc37934SDmitry Chagin vm_page_t m;
66bdc37934SDmitry Chagin vm_object_t obj;
67bdc37934SDmitry Chagin vm_offset_t addr;
689931033bSDmitry Chagin size_t n, pages;
69bdc37934SDmitry Chagin
709931033bSDmitry Chagin pages = size / PAGE_SIZE;
719931033bSDmitry Chagin
729931033bSDmitry Chagin addr = kva_alloc(size);
739931033bSDmitry Chagin obj = vm_pager_allocate(OBJT_PHYS, 0, size,
74bdc37934SDmitry Chagin VM_PROT_DEFAULT, 0, NULL);
75bdc37934SDmitry Chagin VM_OBJECT_WLOCK(obj);
769931033bSDmitry Chagin for (n = 0; n < pages; n++) {
779931033bSDmitry Chagin m = vm_page_grab(obj, n,
789931033bSDmitry Chagin VM_ALLOC_ZERO);
794504268aSJeff Roberson vm_page_valid(m);
804504268aSJeff Roberson vm_page_xunbusy(m);
819931033bSDmitry Chagin pmap_qenter(addr + n * PAGE_SIZE, &m, 1);
829931033bSDmitry Chagin }
839931033bSDmitry Chagin VM_OBJECT_WUNLOCK(obj);
84bdc37934SDmitry Chagin *mapping = (char *)addr;
85bdc37934SDmitry Chagin return (obj);
86bdc37934SDmitry Chagin }
87bdc37934SDmitry Chagin
88bdc37934SDmitry Chagin void
__elfN(linux_shared_page_fini)899931033bSDmitry Chagin __elfN(linux_shared_page_fini)(vm_object_t obj, void *mapping,
909931033bSDmitry Chagin vm_size_t size)
91bdc37934SDmitry Chagin {
920fc8a796SMark Johnston vm_offset_t va;
93bdc37934SDmitry Chagin
940fc8a796SMark Johnston va = (vm_offset_t)mapping;
959931033bSDmitry Chagin pmap_qremove(va, size / PAGE_SIZE);
969931033bSDmitry Chagin kva_free(va, size);
97bdc37934SDmitry Chagin vm_object_deallocate(obj);
98bdc37934SDmitry Chagin }
99bdc37934SDmitry Chagin
100bdc37934SDmitry Chagin void
__elfN(linux_vdso_fixup)1019931033bSDmitry Chagin __elfN(linux_vdso_fixup)(char *base, vm_offset_t offset)
102bdc37934SDmitry Chagin {
1039931033bSDmitry Chagin struct linux_vdso_sym *lsym;
1049931033bSDmitry Chagin const Elf_Shdr *shdr;
105bdc37934SDmitry Chagin Elf_Ehdr *ehdr;
1069931033bSDmitry Chagin Elf_Sym *dsym, *sym;
1079931033bSDmitry Chagin char *strtab, *symname;
1089931033bSDmitry Chagin int i, symcnt;
109bdc37934SDmitry Chagin
1109931033bSDmitry Chagin ehdr = (Elf_Ehdr *)base;
111bdc37934SDmitry Chagin
1129931033bSDmitry Chagin MPASS(IS_ELF(*ehdr));
1139931033bSDmitry Chagin MPASS(ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS);
1149931033bSDmitry Chagin MPASS(ehdr->e_ident[EI_DATA] == ELF_TARG_DATA);
1159931033bSDmitry Chagin MPASS(ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1169931033bSDmitry Chagin MPASS(ehdr->e_shentsize == sizeof(Elf_Shdr));
1179931033bSDmitry Chagin MPASS(ehdr->e_shoff != 0);
1189931033bSDmitry Chagin MPASS(ehdr->e_type == ET_DYN);
119bdc37934SDmitry Chagin
1209931033bSDmitry Chagin shdr = (const Elf_Shdr *)(base + ehdr->e_shoff);
121bdc37934SDmitry Chagin
1229931033bSDmitry Chagin dsym = NULL;
123bdc37934SDmitry Chagin for (i = 0; i < ehdr->e_shnum; i++) {
124bdc37934SDmitry Chagin if (shdr[i].sh_size == 0)
125bdc37934SDmitry Chagin continue;
126bdc37934SDmitry Chagin if (shdr[i].sh_type == SHT_DYNSYM) {
1279931033bSDmitry Chagin dsym = (Elf_Sym *)(base + shdr[i].sh_offset);
1289931033bSDmitry Chagin strtab = base + shdr[shdr[i].sh_link].sh_offset;
1299931033bSDmitry Chagin symcnt = shdr[i].sh_size / sizeof(*dsym);
1309931033bSDmitry Chagin break;
131bdc37934SDmitry Chagin }
132bdc37934SDmitry Chagin }
1339931033bSDmitry Chagin MPASS(dsym != NULL);
134bdc37934SDmitry Chagin
135bdc37934SDmitry Chagin ehdr->e_ident[EI_OSABI] = ELFOSABI_LINUX;
136bdc37934SDmitry Chagin
1379931033bSDmitry Chagin /*
1389931033bSDmitry Chagin * VDSO is readonly mapped to the process VA and
1399931033bSDmitry Chagin * can't be relocated by rtld.
1409931033bSDmitry Chagin */
1419931033bSDmitry Chagin SLIST_FOREACH(lsym, &__elfN(linux_vdso_syms), sym) {
1429931033bSDmitry Chagin for (i = 0, sym = dsym; i < symcnt; i++, sym++) {
143bdc37934SDmitry Chagin symname = strtab + sym->st_name;
1449931033bSDmitry Chagin if (strncmp(lsym->symname, symname, lsym->size) == 0) {
1459931033bSDmitry Chagin sym->st_value += offset;
1469931033bSDmitry Chagin *lsym->ptr = sym->st_value;
147bdc37934SDmitry Chagin break;
1489931033bSDmitry Chagin
149bdc37934SDmitry Chagin }
150bdc37934SDmitry Chagin }
151bdc37934SDmitry Chagin }
1529931033bSDmitry Chagin }
1539931033bSDmitry Chagin
1549931033bSDmitry Chagin int
linux_map_vdso(struct proc * p,vm_object_t obj,vm_offset_t base,vm_offset_t size,struct image_params * imgp)1559931033bSDmitry Chagin linux_map_vdso(struct proc *p, vm_object_t obj, vm_offset_t base,
1569931033bSDmitry Chagin vm_offset_t size, struct image_params *imgp)
1579931033bSDmitry Chagin {
1589931033bSDmitry Chagin struct vmspace *vmspace;
1599931033bSDmitry Chagin vm_map_t map;
1609931033bSDmitry Chagin int error;
1619931033bSDmitry Chagin
1629931033bSDmitry Chagin MPASS((imgp->sysent->sv_flags & SV_ABI_MASK) == SV_ABI_LINUX);
1639931033bSDmitry Chagin MPASS(obj != NULL);
1649931033bSDmitry Chagin
1659931033bSDmitry Chagin vmspace = p->p_vmspace;
1669931033bSDmitry Chagin map = &vmspace->vm_map;
1679931033bSDmitry Chagin
1689931033bSDmitry Chagin vm_object_reference(obj);
1699931033bSDmitry Chagin error = vm_map_fixed(map, obj, 0, base, size,
1709931033bSDmitry Chagin VM_PROT_READ | VM_PROT_EXECUTE,
1719931033bSDmitry Chagin VM_PROT_READ | VM_PROT_EXECUTE,
1729931033bSDmitry Chagin MAP_INHERIT_SHARE | MAP_ACC_NO_CHARGE);
1739931033bSDmitry Chagin if (error != KERN_SUCCESS) {
1749931033bSDmitry Chagin vm_object_deallocate(obj);
1759931033bSDmitry Chagin return (vm_mmap_to_errno(error));
1769931033bSDmitry Chagin }
1779931033bSDmitry Chagin return (0);
1789931033bSDmitry Chagin }
179