1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright 1996-1998 John D. Polstra. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/kernel.h> 30 #include <sys/systm.h> 31 #include <sys/elf.h> 32 #include <sys/exec.h> 33 #include <sys/imgact.h> 34 #include <sys/malloc.h> 35 #include <sys/proc.h> 36 #include <sys/namei.h> 37 #include <sys/fcntl.h> 38 #include <sys/reg.h> 39 #include <sys/sysent.h> 40 #include <sys/imgact_elf.h> 41 #include <sys/jail.h> 42 #include <sys/smp.h> 43 #include <sys/syscall.h> 44 #include <sys/signalvar.h> 45 #include <sys/vnode.h> 46 #include <sys/linker.h> 47 48 #include <vm/vm.h> 49 #include <vm/vm_param.h> 50 #include <vm/pmap.h> 51 #include <vm/vm_map.h> 52 53 #include <machine/altivec.h> 54 #include <machine/cpu.h> 55 #include <machine/fpu.h> 56 #include <machine/elf.h> 57 #include <machine/md_var.h> 58 59 #include <powerpc/powerpc/elf_common.c> 60 61 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp, 62 uintptr_t stack); 63 64 struct sysentvec elf64_freebsd_sysvec_v1 = { 65 .sv_size = SYS_MAXSYSCALL, 66 .sv_table = sysent, 67 .sv_fixup = __elfN(freebsd_fixup), 68 .sv_sendsig = sendsig, 69 .sv_sigcode = sigcode64, 70 .sv_szsigcode = &szsigcode64, 71 .sv_name = "FreeBSD ELF64", 72 .sv_coredump = __elfN(coredump), 73 .sv_elf_core_osabi = ELFOSABI_FREEBSD, 74 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR, 75 .sv_elf_core_prepare_notes = __elfN(prepare_notes), 76 .sv_minsigstksz = MINSIGSTKSZ, 77 .sv_minuser = VM_MIN_ADDRESS, 78 .sv_maxuser = VM_MAXUSER_ADDRESS, 79 .sv_usrstack = USRSTACK, 80 .sv_psstrings = PS_STRINGS, 81 .sv_psstringssz = sizeof(struct ps_strings), 82 .sv_stackprot = VM_PROT_ALL, 83 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs), 84 .sv_copyout_strings = exec_copyout_strings, 85 .sv_setregs = exec_setregs_funcdesc, 86 .sv_fixlimit = NULL, 87 .sv_maxssiz = NULL, 88 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR | 89 SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS, 90 .sv_set_syscall_retval = cpu_set_syscall_retval, 91 .sv_fetch_syscall_args = cpu_fetch_syscall_args, 92 .sv_syscallnames = syscallnames, 93 .sv_shared_page_base = SHAREDPAGE, 94 .sv_shared_page_len = PAGE_SIZE, 95 .sv_schedtail = NULL, 96 .sv_thread_detach = NULL, 97 .sv_trap = NULL, 98 .sv_hwcap = &cpu_features, 99 .sv_hwcap2 = &cpu_features2, 100 .sv_onexec_old = exec_onexec_old, 101 .sv_onexit = exit_onexit, 102 .sv_regset_begin = SET_BEGIN(__elfN(regset)), 103 .sv_regset_end = SET_LIMIT(__elfN(regset)), 104 }; 105 106 struct sysentvec elf64_freebsd_sysvec_v2 = { 107 .sv_size = SYS_MAXSYSCALL, 108 .sv_table = sysent, 109 .sv_fixup = __elfN(freebsd_fixup), 110 .sv_sendsig = sendsig, 111 .sv_sigcode = sigcode64, /* Fixed up in ppc64_init_sysvecs(). */ 112 .sv_szsigcode = &szsigcode64, 113 .sv_name = "FreeBSD ELF64 V2", 114 .sv_coredump = __elfN(coredump), 115 .sv_elf_core_osabi = ELFOSABI_FREEBSD, 116 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR, 117 .sv_elf_core_prepare_notes = __elfN(prepare_notes), 118 .sv_minsigstksz = MINSIGSTKSZ, 119 .sv_minuser = VM_MIN_ADDRESS, 120 .sv_maxuser = VM_MAXUSER_ADDRESS, 121 .sv_usrstack = USRSTACK, 122 .sv_psstrings = PS_STRINGS, 123 .sv_psstringssz = sizeof(struct ps_strings), 124 .sv_stackprot = VM_PROT_ALL, 125 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs), 126 .sv_copyout_strings = exec_copyout_strings, 127 .sv_setregs = exec_setregs, 128 .sv_fixlimit = NULL, 129 .sv_maxssiz = NULL, 130 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | 131 SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS, 132 .sv_set_syscall_retval = cpu_set_syscall_retval, 133 .sv_fetch_syscall_args = cpu_fetch_syscall_args, 134 .sv_syscallnames = syscallnames, 135 .sv_shared_page_base = SHAREDPAGE, 136 .sv_shared_page_len = PAGE_SIZE, 137 .sv_schedtail = NULL, 138 .sv_thread_detach = NULL, 139 .sv_trap = NULL, 140 .sv_hwcap = &cpu_features, 141 .sv_hwcap2 = &cpu_features2, 142 .sv_onexec_old = exec_onexec_old, 143 .sv_onexit = exit_onexit, 144 .sv_regset_begin = SET_BEGIN(__elfN(regset)), 145 .sv_regset_end = SET_LIMIT(__elfN(regset)), 146 }; 147 148 static bool ppc64_elfv1_header_match(struct image_params *params, 149 int32_t *, uint32_t *); 150 static bool ppc64_elfv2_header_match(struct image_params *params, 151 int32_t *, uint32_t *); 152 153 static Elf64_Brandinfo freebsd_brand_info_elfv1 = { 154 .brand = ELFOSABI_FREEBSD, 155 .machine = EM_PPC64, 156 .compat_3_brand = "FreeBSD", 157 .interp_path = "/libexec/ld-elf.so.1", 158 .sysvec = &elf64_freebsd_sysvec_v1, 159 .interp_newpath = NULL, 160 .brand_note = &elf64_freebsd_brandnote, 161 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 162 .header_supported = &ppc64_elfv1_header_match 163 }; 164 165 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY, 166 (sysinit_cfunc_t) elf64_insert_brand_entry, 167 &freebsd_brand_info_elfv1); 168 169 static Elf64_Brandinfo freebsd_brand_info_elfv2 = { 170 .brand = ELFOSABI_FREEBSD, 171 .machine = EM_PPC64, 172 .compat_3_brand = "FreeBSD", 173 .interp_path = "/libexec/ld-elf.so.1", 174 .sysvec = &elf64_freebsd_sysvec_v2, 175 .interp_newpath = NULL, 176 .brand_note = &elf64_freebsd_brandnote, 177 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 178 .header_supported = &ppc64_elfv2_header_match 179 }; 180 181 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY, 182 (sysinit_cfunc_t) elf64_insert_brand_entry, 183 &freebsd_brand_info_elfv2); 184 185 static Elf64_Brandinfo freebsd_brand_oinfo = { 186 .brand = ELFOSABI_FREEBSD, 187 .machine = EM_PPC64, 188 .compat_3_brand = "FreeBSD", 189 .interp_path = "/usr/libexec/ld-elf.so.1", 190 .sysvec = &elf64_freebsd_sysvec_v1, 191 .interp_newpath = NULL, 192 .brand_note = &elf64_freebsd_brandnote, 193 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 194 .header_supported = &ppc64_elfv1_header_match 195 }; 196 197 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY, 198 (sysinit_cfunc_t) elf64_insert_brand_entry, 199 &freebsd_brand_oinfo); 200 201 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase); 202 203 static void 204 ppc64_init_sysvecs(void *arg) 205 { 206 exec_sysvec_init(&elf64_freebsd_sysvec_v2); 207 exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2, 208 &elf64_freebsd_sysvec_v1); 209 /* 210 * Adjust elfv2 sigcode after elfv1 sysvec is initialized. 211 * exec_sysvec_init_secondary() assumes secondary sysvecs use 212 * identical signal code, and skips allocating a second copy. 213 * Since the ELFv2 trampoline is a strict subset of the ELFv1 code, 214 * we can work around this by adjusting the offset. This also 215 * avoids two copies of the trampoline code being allocated! 216 */ 217 elf64_freebsd_sysvec_v2.sv_sigcode_offset += 218 (uintptr_t)sigcode64_elfv2 - (uintptr_t)&sigcode64; 219 elf64_freebsd_sysvec_v2.sv_szsigcode = &szsigcode64_elfv2; 220 } 221 SYSINIT(elf64_sysvec, SI_SUB_EXEC, SI_ORDER_ANY, ppc64_init_sysvecs, NULL); 222 223 static bool 224 ppc64_elfv1_header_match(struct image_params *params, int32_t *osrel __unused, 225 uint32_t *fctl0 __unused) 226 { 227 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header; 228 int abi = (hdr->e_flags & 3); 229 230 return (abi == 0 || abi == 1); 231 } 232 233 static bool 234 ppc64_elfv2_header_match(struct image_params *params, int32_t *osrel __unused, 235 uint32_t *fctl0 __unused) 236 { 237 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header; 238 int abi = (hdr->e_flags & 3); 239 240 return (abi == 2); 241 } 242 243 static void 244 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp, 245 uintptr_t stack) 246 { 247 struct trapframe *tf; 248 register_t entry_desc[3]; 249 250 tf = trapframe(td); 251 exec_setregs(td, imgp, stack); 252 253 /* 254 * For 64-bit ELFv1, we need to disentangle the function 255 * descriptor 256 * 257 * 0. entry point 258 * 1. TOC value (r2) 259 * 2. Environment pointer (r11) 260 */ 261 262 (void)copyin((void *)imgp->entry_addr, entry_desc, 263 sizeof(entry_desc)); 264 tf->srr0 = entry_desc[0] + imgp->reloc_base; 265 tf->fixreg[2] = entry_desc[1] + imgp->reloc_base; 266 tf->fixreg[11] = entry_desc[2] + imgp->reloc_base; 267 } 268 269 void 270 elf64_dump_thread(struct thread *td, void *dst, size_t *off) 271 { 272 size_t len; 273 struct pcb *pcb; 274 uint64_t vshr[32]; 275 uint64_t *vsr_dw1; 276 int vsr_idx; 277 278 len = 0; 279 pcb = td->td_pcb; 280 281 if (pcb->pcb_flags & PCB_VEC) { 282 save_vec_nodrop(td); 283 if (dst != NULL) { 284 len += elf64_populate_note(NT_PPC_VMX, 285 &pcb->pcb_vec, (char *)dst + len, 286 sizeof(pcb->pcb_vec), NULL); 287 } else 288 len += elf64_populate_note(NT_PPC_VMX, NULL, NULL, 289 sizeof(pcb->pcb_vec), NULL); 290 } 291 292 if (pcb->pcb_flags & PCB_VSX) { 293 save_fpu_nodrop(td); 294 if (dst != NULL) { 295 /* 296 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and 297 * VSR32-VSR63 overlap with VR0-VR31, so we only copy 298 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31. 299 */ 300 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) { 301 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2]; 302 vshr[vsr_idx] = *vsr_dw1; 303 } 304 len += elf64_populate_note(NT_PPC_VSX, 305 vshr, (char *)dst + len, 306 sizeof(vshr), NULL); 307 } else 308 len += elf64_populate_note(NT_PPC_VSX, NULL, NULL, 309 sizeof(vshr), NULL); 310 } 311 312 *off = len; 313 } 314 315 bool 316 elf_is_ifunc_reloc(Elf_Size r_info) 317 { 318 319 return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE); 320 } 321 322 /* Process one elf relocation with addend. */ 323 static int 324 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 325 int type, int local, elf_lookup_fn lookup) 326 { 327 Elf_Addr *where; 328 Elf_Addr addr; 329 Elf_Addr addend, val; 330 Elf_Word rtype, symidx; 331 const Elf_Rela *rela; 332 int error; 333 334 switch (type) { 335 case ELF_RELOC_REL: 336 panic("PPC only supports RELA relocations"); 337 break; 338 case ELF_RELOC_RELA: 339 rela = (const Elf_Rela *)data; 340 where = (Elf_Addr *) (relocbase + rela->r_offset); 341 addend = rela->r_addend; 342 rtype = ELF_R_TYPE(rela->r_info); 343 symidx = ELF_R_SYM(rela->r_info); 344 break; 345 default: 346 panic("elf_reloc: unknown relocation mode %d\n", type); 347 } 348 349 switch (rtype) { 350 case R_PPC_NONE: 351 break; 352 353 case R_PPC64_ADDR64: /* doubleword64 S + A */ 354 error = lookup(lf, symidx, 1, &addr); 355 if (error != 0) 356 return (-1); 357 addr += addend; 358 *where = addr; 359 break; 360 361 case R_PPC_RELATIVE: /* doubleword64 B + A */ 362 *where = elf_relocaddr(lf, relocbase + addend); 363 break; 364 365 case R_PPC_JMP_SLOT: /* function descriptor copy */ 366 lookup(lf, symidx, 1, &addr); 367 #if !defined(_CALL_ELF) || _CALL_ELF == 1 368 memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr)); 369 #else 370 *where = addr; 371 #endif 372 __asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory"); 373 break; 374 375 case R_PPC_IRELATIVE: 376 addr = relocbase + addend; 377 val = ((Elf64_Addr (*)(void))addr)(); 378 if (*where != val) 379 *where = val; 380 break; 381 382 default: 383 printf("kldload: unexpected relocation type %d, " 384 "symbol index %d\n", (int)rtype, symidx); 385 return (-1); 386 } 387 return (0); 388 } 389 390 void 391 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase) 392 { 393 Elf_Rela *rela = NULL, *relalim; 394 Elf_Addr relasz = 0; 395 Elf_Addr *where; 396 397 /* 398 * Extract the rela/relasz values from the dynamic section 399 */ 400 for (; dynp->d_tag != DT_NULL; dynp++) { 401 switch (dynp->d_tag) { 402 case DT_RELA: 403 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr); 404 break; 405 case DT_RELASZ: 406 relasz = dynp->d_un.d_val; 407 break; 408 } 409 } 410 411 /* 412 * Relocate these values 413 */ 414 relalim = (Elf_Rela *)((caddr_t)rela + relasz); 415 for (; rela < relalim; rela++) { 416 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE) 417 continue; 418 where = (Elf_Addr *)(relocbase + rela->r_offset); 419 *where = (Elf_Addr)(relocbase + rela->r_addend); 420 } 421 } 422 423 int 424 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 425 elf_lookup_fn lookup) 426 { 427 428 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 429 } 430 431 int 432 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 433 int type, elf_lookup_fn lookup) 434 { 435 436 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup)); 437 } 438 439 int 440 elf_cpu_load_file(linker_file_t lf) 441 { 442 /* Only sync the cache for non-kernel modules */ 443 if (lf->id != 1) 444 __syncicache(lf->address, lf->size); 445 return (0); 446 } 447 448 int 449 elf_cpu_unload_file(linker_file_t lf __unused) 450 { 451 452 return (0); 453 } 454 455 int 456 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused) 457 { 458 459 return (0); 460 } 461