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