1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 * $FreeBSD$ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/systm.h> 33 #include <sys/exec.h> 34 #include <sys/imgact.h> 35 #include <sys/malloc.h> 36 #include <sys/proc.h> 37 #include <sys/namei.h> 38 #include <sys/fcntl.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 51 #include <machine/altivec.h> 52 #include <machine/cpu.h> 53 #include <machine/fpu.h> 54 #include <machine/elf.h> 55 #include <machine/md_var.h> 56 57 #include <powerpc/powerpc/elf_common.c> 58 59 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp, 60 uintptr_t stack); 61 62 struct sysentvec elf64_freebsd_sysvec_v1 = { 63 .sv_size = SYS_MAXSYSCALL, 64 .sv_table = sysent, 65 .sv_errsize = 0, 66 .sv_errtbl = NULL, 67 .sv_transtrap = NULL, 68 .sv_fixup = __elfN(freebsd_fixup), 69 .sv_sendsig = sendsig, 70 .sv_sigcode = sigcode64, 71 .sv_szsigcode = &szsigcode64, 72 .sv_name = "FreeBSD ELF64", 73 .sv_coredump = __elfN(coredump), 74 .sv_imgact_try = NULL, 75 .sv_minsigstksz = MINSIGSTKSZ, 76 .sv_minuser = VM_MIN_ADDRESS, 77 .sv_maxuser = VM_MAXUSER_ADDRESS, 78 .sv_usrstack = USRSTACK, 79 .sv_psstrings = PS_STRINGS, 80 .sv_stackprot = VM_PROT_ALL, 81 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs), 82 .sv_copyout_strings = exec_copyout_strings, 83 .sv_setregs = exec_setregs_funcdesc, 84 .sv_fixlimit = NULL, 85 .sv_maxssiz = NULL, 86 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR, 87 .sv_set_syscall_retval = cpu_set_syscall_retval, 88 .sv_fetch_syscall_args = cpu_fetch_syscall_args, 89 .sv_syscallnames = syscallnames, 90 .sv_shared_page_base = SHAREDPAGE, 91 .sv_shared_page_len = PAGE_SIZE, 92 .sv_schedtail = NULL, 93 .sv_thread_detach = NULL, 94 .sv_trap = NULL, 95 .sv_hwcap = &cpu_features, 96 .sv_hwcap2 = &cpu_features2, 97 }; 98 INIT_SYSENTVEC(elf64_sysvec_v1, &elf64_freebsd_sysvec_v1); 99 100 struct sysentvec elf64_freebsd_sysvec_v2 = { 101 .sv_size = SYS_MAXSYSCALL, 102 .sv_table = sysent, 103 .sv_errsize = 0, 104 .sv_errtbl = NULL, 105 .sv_transtrap = NULL, 106 .sv_fixup = __elfN(freebsd_fixup), 107 .sv_sendsig = sendsig, 108 .sv_sigcode = sigcode64_elfv2, 109 .sv_szsigcode = &szsigcode64_elfv2, 110 .sv_name = "FreeBSD ELF64 V2", 111 .sv_coredump = __elfN(coredump), 112 .sv_imgact_try = NULL, 113 .sv_minsigstksz = MINSIGSTKSZ, 114 .sv_minuser = VM_MIN_ADDRESS, 115 .sv_maxuser = VM_MAXUSER_ADDRESS, 116 .sv_usrstack = USRSTACK, 117 .sv_psstrings = PS_STRINGS, 118 .sv_stackprot = VM_PROT_ALL, 119 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs), 120 .sv_copyout_strings = exec_copyout_strings, 121 .sv_setregs = exec_setregs, 122 .sv_fixlimit = NULL, 123 .sv_maxssiz = NULL, 124 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP, 125 .sv_set_syscall_retval = cpu_set_syscall_retval, 126 .sv_fetch_syscall_args = cpu_fetch_syscall_args, 127 .sv_syscallnames = syscallnames, 128 .sv_shared_page_base = SHAREDPAGE, 129 .sv_shared_page_len = PAGE_SIZE, 130 .sv_schedtail = NULL, 131 .sv_thread_detach = NULL, 132 .sv_trap = NULL, 133 .sv_hwcap = &cpu_features, 134 .sv_hwcap2 = &cpu_features2, 135 }; 136 INIT_SYSENTVEC(elf64_sysvec_v2, &elf64_freebsd_sysvec_v2); 137 138 static boolean_t ppc64_elfv1_header_match(struct image_params *params); 139 static boolean_t ppc64_elfv2_header_match(struct image_params *params); 140 141 static Elf64_Brandinfo freebsd_brand_info_elfv1 = { 142 .brand = ELFOSABI_FREEBSD, 143 .machine = EM_PPC64, 144 .compat_3_brand = "FreeBSD", 145 .emul_path = NULL, 146 .interp_path = "/libexec/ld-elf.so.1", 147 .sysvec = &elf64_freebsd_sysvec_v1, 148 .interp_newpath = NULL, 149 .brand_note = &elf64_freebsd_brandnote, 150 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 151 .header_supported = &ppc64_elfv1_header_match 152 }; 153 154 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY, 155 (sysinit_cfunc_t) elf64_insert_brand_entry, 156 &freebsd_brand_info_elfv1); 157 158 static Elf64_Brandinfo freebsd_brand_info_elfv2 = { 159 .brand = ELFOSABI_FREEBSD, 160 .machine = EM_PPC64, 161 .compat_3_brand = "FreeBSD", 162 .emul_path = NULL, 163 .interp_path = "/libexec/ld-elf.so.1", 164 .sysvec = &elf64_freebsd_sysvec_v2, 165 .interp_newpath = NULL, 166 .brand_note = &elf64_freebsd_brandnote, 167 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 168 .header_supported = &ppc64_elfv2_header_match 169 }; 170 171 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY, 172 (sysinit_cfunc_t) elf64_insert_brand_entry, 173 &freebsd_brand_info_elfv2); 174 175 static Elf64_Brandinfo freebsd_brand_oinfo = { 176 .brand = ELFOSABI_FREEBSD, 177 .machine = EM_PPC64, 178 .compat_3_brand = "FreeBSD", 179 .emul_path = NULL, 180 .interp_path = "/usr/libexec/ld-elf.so.1", 181 .sysvec = &elf64_freebsd_sysvec_v1, 182 .interp_newpath = NULL, 183 .brand_note = &elf64_freebsd_brandnote, 184 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 185 .header_supported = &ppc64_elfv1_header_match 186 }; 187 188 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY, 189 (sysinit_cfunc_t) elf64_insert_brand_entry, 190 &freebsd_brand_oinfo); 191 192 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase); 193 194 static boolean_t 195 ppc64_elfv1_header_match(struct image_params *params) 196 { 197 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header; 198 int abi = (hdr->e_flags & 3); 199 200 return (abi == 0 || abi == 1); 201 } 202 203 static boolean_t 204 ppc64_elfv2_header_match(struct image_params *params) 205 { 206 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header; 207 int abi = (hdr->e_flags & 3); 208 209 return (abi == 2); 210 } 211 212 static void 213 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp, 214 uintptr_t stack) 215 { 216 struct trapframe *tf; 217 register_t entry_desc[3]; 218 219 tf = trapframe(td); 220 exec_setregs(td, imgp, stack); 221 222 /* 223 * For 64-bit ELFv1, we need to disentangle the function 224 * descriptor 225 * 226 * 0. entry point 227 * 1. TOC value (r2) 228 * 2. Environment pointer (r11) 229 */ 230 231 (void)copyin((void *)imgp->entry_addr, entry_desc, 232 sizeof(entry_desc)); 233 tf->srr0 = entry_desc[0] + imgp->reloc_base; 234 tf->fixreg[2] = entry_desc[1] + imgp->reloc_base; 235 tf->fixreg[11] = entry_desc[2] + imgp->reloc_base; 236 } 237 238 void 239 elf64_dump_thread(struct thread *td, void *dst, size_t *off) 240 { 241 size_t len; 242 struct pcb *pcb; 243 uint64_t vshr[32]; 244 uint64_t *vsr_dw1; 245 int vsr_idx; 246 247 len = 0; 248 pcb = td->td_pcb; 249 250 if (pcb->pcb_flags & PCB_VEC) { 251 save_vec_nodrop(td); 252 if (dst != NULL) { 253 len += elf64_populate_note(NT_PPC_VMX, 254 &pcb->pcb_vec, (char *)dst + len, 255 sizeof(pcb->pcb_vec), NULL); 256 } else 257 len += elf64_populate_note(NT_PPC_VMX, NULL, NULL, 258 sizeof(pcb->pcb_vec), NULL); 259 } 260 261 if (pcb->pcb_flags & PCB_VSX) { 262 save_fpu_nodrop(td); 263 if (dst != NULL) { 264 /* 265 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and 266 * VSR32-VSR63 overlap with VR0-VR31, so we only copy 267 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31. 268 */ 269 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) { 270 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2]; 271 vshr[vsr_idx] = *vsr_dw1; 272 } 273 len += elf64_populate_note(NT_PPC_VSX, 274 vshr, (char *)dst + len, 275 sizeof(vshr), NULL); 276 } else 277 len += elf64_populate_note(NT_PPC_VSX, NULL, NULL, 278 sizeof(vshr), NULL); 279 } 280 281 *off = len; 282 } 283 284 bool 285 elf_is_ifunc_reloc(Elf_Size r_info __unused) 286 { 287 288 return (false); 289 } 290 291 /* Process one elf relocation with addend. */ 292 static int 293 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 294 int type, int local, elf_lookup_fn lookup) 295 { 296 Elf_Addr *where; 297 Elf_Addr addr; 298 Elf_Addr addend; 299 Elf_Word rtype, symidx; 300 const Elf_Rela *rela; 301 int error; 302 303 switch (type) { 304 case ELF_RELOC_REL: 305 panic("PPC only supports RELA relocations"); 306 break; 307 case ELF_RELOC_RELA: 308 rela = (const Elf_Rela *)data; 309 where = (Elf_Addr *) (relocbase + rela->r_offset); 310 addend = rela->r_addend; 311 rtype = ELF_R_TYPE(rela->r_info); 312 symidx = ELF_R_SYM(rela->r_info); 313 break; 314 default: 315 panic("elf_reloc: unknown relocation mode %d\n", type); 316 } 317 318 switch (rtype) { 319 320 case R_PPC_NONE: 321 break; 322 323 case R_PPC64_ADDR64: /* doubleword64 S + A */ 324 error = lookup(lf, symidx, 1, &addr); 325 if (error != 0) 326 return -1; 327 addr += addend; 328 *where = addr; 329 break; 330 331 case R_PPC_RELATIVE: /* doubleword64 B + A */ 332 *where = elf_relocaddr(lf, relocbase + addend); 333 break; 334 335 case R_PPC_JMP_SLOT: /* function descriptor copy */ 336 lookup(lf, symidx, 1, &addr); 337 #if !defined(_CALL_ELF) || _CALL_ELF == 1 338 memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr)); 339 #else 340 *where = addr; 341 #endif 342 __asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory"); 343 break; 344 345 default: 346 printf("kldload: unexpected relocation type %d\n", 347 (int) rtype); 348 return -1; 349 } 350 return(0); 351 } 352 353 void 354 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase) 355 { 356 Elf_Rela *rela = NULL, *relalim; 357 Elf_Addr relasz = 0; 358 Elf_Addr *where; 359 360 /* 361 * Extract the rela/relasz values from the dynamic section 362 */ 363 for (; dynp->d_tag != DT_NULL; dynp++) { 364 switch (dynp->d_tag) { 365 case DT_RELA: 366 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr); 367 break; 368 case DT_RELASZ: 369 relasz = dynp->d_un.d_val; 370 break; 371 } 372 } 373 374 /* 375 * Relocate these values 376 */ 377 relalim = (Elf_Rela *)((caddr_t)rela + relasz); 378 for (; rela < relalim; rela++) { 379 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE) 380 continue; 381 where = (Elf_Addr *)(relocbase + rela->r_offset); 382 *where = (Elf_Addr)(relocbase + rela->r_addend); 383 } 384 } 385 386 int 387 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 388 elf_lookup_fn lookup) 389 { 390 391 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 392 } 393 394 int 395 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 396 int type, elf_lookup_fn lookup) 397 { 398 399 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup)); 400 } 401 402 int 403 elf_cpu_load_file(linker_file_t lf) 404 { 405 /* Only sync the cache for non-kernel modules */ 406 if (lf->id != 1) 407 __syncicache(lf->address, lf->size); 408 return (0); 409 } 410 411 int 412 elf_cpu_unload_file(linker_file_t lf __unused) 413 { 414 415 return (0); 416 } 417 418 int 419 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused) 420 { 421 422 return (0); 423 } 424