xref: /freebsd/sys/compat/linux/linux_vdso.c (revision c47116e90951621196c72b7def866cadfdabf80e)
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 
28bdc37934SDmitry Chagin #include <sys/cdefs.h>
2936204c30SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
30bdc37934SDmitry Chagin #define	__ELF_WORD_SIZE	32
3136204c30SDmitry Chagin #else
3236204c30SDmitry Chagin #define	__ELF_WORD_SIZE	64
3336204c30SDmitry Chagin #endif
34bdc37934SDmitry Chagin 
35bdc37934SDmitry Chagin #include <sys/param.h>
36bdc37934SDmitry Chagin #include <sys/elf.h>
379931033bSDmitry Chagin #include <sys/imgact.h>
38d8e53d94SDmitry Chagin #include <sys/lock.h>
399931033bSDmitry Chagin #include <sys/proc.h>
40bdc37934SDmitry Chagin #include <sys/rwlock.h>
41bdc37934SDmitry Chagin #include <sys/sysent.h>
42bdc37934SDmitry Chagin 
43bdc37934SDmitry Chagin #include <vm/pmap.h>
44bdc37934SDmitry Chagin #include <vm/vm_extern.h>
45bdc37934SDmitry Chagin #include <vm/vm_map.h>
46bdc37934SDmitry Chagin #include <vm/vm_object.h>
47bdc37934SDmitry Chagin #include <vm/vm_page.h>
48bdc37934SDmitry Chagin #include <vm/vm_pager.h>
49d8e53d94SDmitry Chagin #include <vm/vm_param.h>
50bdc37934SDmitry Chagin 
51bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
52bdc37934SDmitry Chagin 
53bdc37934SDmitry Chagin SLIST_HEAD(, linux_vdso_sym) __elfN(linux_vdso_syms) =
54bdc37934SDmitry Chagin     SLIST_HEAD_INITIALIZER(__elfN(linux_vdso_syms));
55bdc37934SDmitry Chagin 
56bdc37934SDmitry Chagin void
57bdc37934SDmitry Chagin __elfN(linux_vdso_sym_init)(struct linux_vdso_sym *s)
58bdc37934SDmitry Chagin {
59bdc37934SDmitry Chagin 
60bdc37934SDmitry Chagin 	SLIST_INSERT_HEAD(&__elfN(linux_vdso_syms), s, sym);
61bdc37934SDmitry Chagin }
62bdc37934SDmitry Chagin 
63bdc37934SDmitry Chagin vm_object_t
649931033bSDmitry Chagin __elfN(linux_shared_page_init)(char **mapping, vm_size_t size)
65bdc37934SDmitry Chagin {
66bdc37934SDmitry Chagin 	vm_page_t m;
67bdc37934SDmitry Chagin 	vm_object_t obj;
68bdc37934SDmitry Chagin 	vm_offset_t addr;
699931033bSDmitry Chagin 	size_t n, pages;
70bdc37934SDmitry Chagin 
719931033bSDmitry Chagin 	pages = size / PAGE_SIZE;
729931033bSDmitry Chagin 
739931033bSDmitry Chagin 	addr = kva_alloc(size);
749931033bSDmitry Chagin 	obj = vm_pager_allocate(OBJT_PHYS, 0, size,
75bdc37934SDmitry Chagin 	    VM_PROT_DEFAULT, 0, NULL);
76bdc37934SDmitry Chagin 	VM_OBJECT_WLOCK(obj);
779931033bSDmitry Chagin 	for (n = 0; n < pages; n++) {
789931033bSDmitry Chagin 		m = vm_page_grab(obj, n,
799931033bSDmitry Chagin 		    VM_ALLOC_ZERO);
804504268aSJeff Roberson 		vm_page_valid(m);
814504268aSJeff Roberson 		vm_page_xunbusy(m);
829931033bSDmitry Chagin 		pmap_qenter(addr + n * PAGE_SIZE, &m, 1);
839931033bSDmitry Chagin 	}
849931033bSDmitry Chagin 	VM_OBJECT_WUNLOCK(obj);
85bdc37934SDmitry Chagin 	*mapping = (char *)addr;
86bdc37934SDmitry Chagin 	return (obj);
87bdc37934SDmitry Chagin }
88bdc37934SDmitry Chagin 
89bdc37934SDmitry Chagin void
909931033bSDmitry Chagin __elfN(linux_shared_page_fini)(vm_object_t obj, void *mapping,
919931033bSDmitry Chagin     vm_size_t size)
92bdc37934SDmitry Chagin {
930fc8a796SMark Johnston 	vm_offset_t va;
94bdc37934SDmitry Chagin 
950fc8a796SMark Johnston 	va = (vm_offset_t)mapping;
969931033bSDmitry Chagin 	pmap_qremove(va, size / PAGE_SIZE);
979931033bSDmitry Chagin 	kva_free(va, size);
98bdc37934SDmitry Chagin 	vm_object_deallocate(obj);
99bdc37934SDmitry Chagin }
100bdc37934SDmitry Chagin 
101bdc37934SDmitry Chagin void
1029931033bSDmitry Chagin __elfN(linux_vdso_fixup)(char *base, vm_offset_t offset)
103bdc37934SDmitry Chagin {
1049931033bSDmitry Chagin 	struct linux_vdso_sym *lsym;
1059931033bSDmitry Chagin 	const Elf_Shdr *shdr;
106bdc37934SDmitry Chagin 	Elf_Ehdr *ehdr;
1079931033bSDmitry Chagin 	Elf_Sym *dsym, *sym;
1089931033bSDmitry Chagin 	char *strtab, *symname;
1099931033bSDmitry Chagin 	int i, symcnt;
110bdc37934SDmitry Chagin 
1119931033bSDmitry Chagin 	ehdr = (Elf_Ehdr *)base;
112bdc37934SDmitry Chagin 
1139931033bSDmitry Chagin 	MPASS(IS_ELF(*ehdr));
1149931033bSDmitry Chagin 	MPASS(ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS);
1159931033bSDmitry Chagin 	MPASS(ehdr->e_ident[EI_DATA] == ELF_TARG_DATA);
1169931033bSDmitry Chagin 	MPASS(ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1179931033bSDmitry Chagin 	MPASS(ehdr->e_shentsize == sizeof(Elf_Shdr));
1189931033bSDmitry Chagin 	MPASS(ehdr->e_shoff != 0);
1199931033bSDmitry Chagin 	MPASS(ehdr->e_type == ET_DYN);
120bdc37934SDmitry Chagin 
1219931033bSDmitry Chagin 	shdr = (const Elf_Shdr *)(base + ehdr->e_shoff);
122bdc37934SDmitry Chagin 
1239931033bSDmitry Chagin 	dsym = NULL;
124bdc37934SDmitry Chagin 	for (i = 0; i < ehdr->e_shnum; i++) {
125bdc37934SDmitry Chagin 		if (shdr[i].sh_size == 0)
126bdc37934SDmitry Chagin 			continue;
127bdc37934SDmitry Chagin 		if (shdr[i].sh_type == SHT_DYNSYM) {
1289931033bSDmitry Chagin 			dsym = (Elf_Sym *)(base + shdr[i].sh_offset);
1299931033bSDmitry Chagin 			strtab = base + shdr[shdr[i].sh_link].sh_offset;
1309931033bSDmitry Chagin 			symcnt = shdr[i].sh_size / sizeof(*dsym);
1319931033bSDmitry Chagin 			break;
132bdc37934SDmitry Chagin 		}
133bdc37934SDmitry Chagin 	}
1349931033bSDmitry Chagin 	MPASS(dsym != NULL);
135bdc37934SDmitry Chagin 
136bdc37934SDmitry Chagin 	ehdr->e_ident[EI_OSABI] = ELFOSABI_LINUX;
137bdc37934SDmitry Chagin 
1389931033bSDmitry Chagin 	/*
1399931033bSDmitry Chagin 	 * VDSO is readonly mapped to the process VA and
1409931033bSDmitry Chagin 	 * can't be relocated by rtld.
1419931033bSDmitry Chagin 	 */
1429931033bSDmitry Chagin 	SLIST_FOREACH(lsym, &__elfN(linux_vdso_syms), sym) {
1439931033bSDmitry Chagin 		for (i = 0, sym = dsym; i < symcnt; i++, sym++) {
144bdc37934SDmitry Chagin 			symname = strtab + sym->st_name;
1459931033bSDmitry Chagin 			if (strncmp(lsym->symname, symname, lsym->size) == 0) {
1469931033bSDmitry Chagin 				sym->st_value += offset;
1479931033bSDmitry Chagin 				*lsym->ptr = sym->st_value;
148bdc37934SDmitry Chagin 				break;
1499931033bSDmitry Chagin 
150bdc37934SDmitry Chagin 			}
151bdc37934SDmitry Chagin 		}
152bdc37934SDmitry Chagin 	}
1539931033bSDmitry Chagin }
1549931033bSDmitry Chagin 
1559931033bSDmitry Chagin int
1569931033bSDmitry Chagin linux_map_vdso(struct proc *p, vm_object_t obj, vm_offset_t base,
1579931033bSDmitry Chagin     vm_offset_t size, struct image_params *imgp)
1589931033bSDmitry Chagin {
1599931033bSDmitry Chagin 	struct vmspace *vmspace;
1609931033bSDmitry Chagin 	vm_map_t map;
1619931033bSDmitry Chagin 	int error;
1629931033bSDmitry Chagin 
1639931033bSDmitry Chagin 	MPASS((imgp->sysent->sv_flags & SV_ABI_MASK) == SV_ABI_LINUX);
1649931033bSDmitry Chagin 	MPASS(obj != NULL);
1659931033bSDmitry Chagin 
1669931033bSDmitry Chagin 	vmspace = p->p_vmspace;
1679931033bSDmitry Chagin 	map = &vmspace->vm_map;
1689931033bSDmitry Chagin 
1699931033bSDmitry Chagin 	vm_object_reference(obj);
1709931033bSDmitry Chagin 	error = vm_map_fixed(map, obj, 0, base, size,
1719931033bSDmitry Chagin 	    VM_PROT_READ | VM_PROT_EXECUTE,
1729931033bSDmitry Chagin 	    VM_PROT_READ | VM_PROT_EXECUTE,
1739931033bSDmitry Chagin 	    MAP_INHERIT_SHARE | MAP_ACC_NO_CHARGE);
1749931033bSDmitry Chagin 	if (error != KERN_SUCCESS) {
1759931033bSDmitry Chagin 		vm_object_deallocate(obj);
1769931033bSDmitry Chagin 		return (vm_mmap_to_errno(error));
1779931033bSDmitry Chagin 	}
1789931033bSDmitry Chagin 	return (0);
1799931033bSDmitry Chagin }
180