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 32 #define __ELF_WORD_SIZE 32 33 34 #include <sys/exec.h> 35 #include <sys/imgact.h> 36 #include <sys/malloc.h> 37 #include <sys/proc.h> 38 #include <sys/namei.h> 39 #include <sys/fcntl.h> 40 #include <sys/sysent.h> 41 #include <sys/imgact_elf.h> 42 #include <sys/jail.h> 43 #include <sys/reg.h> 44 #include <sys/smp.h> 45 #include <sys/syscall.h> 46 #include <sys/sysctl.h> 47 #include <sys/signalvar.h> 48 #include <sys/vnode.h> 49 #include <sys/linker.h> 50 51 #include <vm/vm.h> 52 #include <vm/vm_param.h> 53 #include <vm/pmap.h> 54 #include <vm/vm_map.h> 55 56 #include <machine/altivec.h> 57 #include <machine/cpu.h> 58 #include <machine/fpu.h> 59 #include <machine/elf.h> 60 #include <machine/md_var.h> 61 62 #include <powerpc/powerpc/elf_common.c> 63 64 #ifdef __powerpc64__ 65 #include <compat/freebsd32/freebsd32_proto.h> 66 #include <compat/freebsd32/freebsd32_util.h> 67 68 extern const char *freebsd32_syscallnames[]; 69 static void ppc32_fixlimit(struct rlimit *rl, int which); 70 71 static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 72 "32-bit mode"); 73 74 #define PPC32_MAXDSIZ (1024*1024*1024) 75 static u_long ppc32_maxdsiz = PPC32_MAXDSIZ; 76 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz, 77 0, ""); 78 #define PPC32_MAXSSIZ (64*1024*1024) 79 u_long ppc32_maxssiz = PPC32_MAXSSIZ; 80 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz, 81 0, ""); 82 #else 83 static void ppc32_runtime_resolve(void); 84 #endif 85 86 struct sysentvec elf32_freebsd_sysvec = { 87 .sv_size = SYS_MAXSYSCALL, 88 #ifdef __powerpc64__ 89 .sv_table = freebsd32_sysent, 90 #else 91 .sv_table = sysent, 92 #endif 93 .sv_fixup = __elfN(freebsd_fixup), 94 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs), 95 .sv_sendsig = sendsig, 96 .sv_sigcode = sigcode32, 97 .sv_szsigcode = &szsigcode32, 98 .sv_name = "FreeBSD ELF32", 99 .sv_coredump = __elfN(coredump), 100 .sv_elf_core_osabi = ELFOSABI_FREEBSD, 101 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR, 102 .sv_elf_core_prepare_notes = __elfN(prepare_notes), 103 .sv_minsigstksz = MINSIGSTKSZ, 104 .sv_minuser = VM_MIN_ADDRESS, 105 .sv_stackprot = VM_PROT_ALL, 106 #ifdef __powerpc64__ 107 .sv_maxuser = VM_MAXUSER_ADDRESS32, 108 .sv_usrstack = FREEBSD32_USRSTACK, 109 .sv_psstrings = FREEBSD32_PS_STRINGS, 110 .sv_psstringssz = sizeof(struct freebsd32_ps_strings), 111 .sv_copyout_strings = freebsd32_copyout_strings, 112 .sv_setregs = ppc32_setregs, 113 .sv_syscallnames = freebsd32_syscallnames, 114 .sv_fixlimit = ppc32_fixlimit, 115 #else 116 .sv_maxuser = VM_MAXUSER_ADDRESS, 117 .sv_usrstack = USRSTACK, 118 .sv_psstrings = PS_STRINGS, 119 .sv_psstringssz = sizeof(struct ps_strings), 120 .sv_copyout_strings = exec_copyout_strings, 121 .sv_setregs = exec_setregs, 122 .sv_syscallnames = syscallnames, 123 .sv_fixlimit = NULL, 124 #endif 125 .sv_maxssiz = NULL, 126 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_ASLR | 127 SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS, 128 .sv_set_syscall_retval = cpu_set_syscall_retval, 129 .sv_fetch_syscall_args = cpu_fetch_syscall_args, 130 .sv_shared_page_base = FREEBSD32_SHAREDPAGE, 131 .sv_shared_page_len = PAGE_SIZE, 132 .sv_schedtail = NULL, 133 .sv_thread_detach = NULL, 134 .sv_trap = NULL, 135 .sv_hwcap = &cpu_features, 136 .sv_hwcap2 = &cpu_features2, 137 .sv_onexec_old = exec_onexec_old, 138 .sv_onexit = exit_onexit, 139 .sv_regset_begin = SET_BEGIN(__elfN(regset)), 140 .sv_regset_end = SET_LIMIT(__elfN(regset)), 141 }; 142 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec); 143 144 static Elf32_Brandinfo freebsd_brand_info = { 145 .brand = ELFOSABI_FREEBSD, 146 .machine = EM_PPC, 147 .compat_3_brand = "FreeBSD", 148 .interp_path = "/libexec/ld-elf.so.1", 149 .sysvec = &elf32_freebsd_sysvec, 150 #ifdef __powerpc64__ 151 .interp_newpath = "/libexec/ld-elf32.so.1", 152 #else 153 .interp_newpath = NULL, 154 #endif 155 .brand_note = &elf32_freebsd_brandnote, 156 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 157 }; 158 159 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST, 160 (sysinit_cfunc_t) elf32_insert_brand_entry, 161 &freebsd_brand_info); 162 163 static Elf32_Brandinfo freebsd_brand_oinfo = { 164 .brand = ELFOSABI_FREEBSD, 165 .machine = EM_PPC, 166 .compat_3_brand = "FreeBSD", 167 .interp_path = "/usr/libexec/ld-elf.so.1", 168 .sysvec = &elf32_freebsd_sysvec, 169 .interp_newpath = NULL, 170 .brand_note = &elf32_freebsd_brandnote, 171 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 172 }; 173 174 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY, 175 (sysinit_cfunc_t) elf32_insert_brand_entry, 176 &freebsd_brand_oinfo); 177 178 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase); 179 180 void 181 elf32_dump_thread(struct thread *td, void *dst, size_t *off) 182 { 183 size_t len; 184 struct pcb *pcb; 185 uint64_t vshr[32]; 186 uint64_t *vsr_dw1; 187 int vsr_idx; 188 189 len = 0; 190 pcb = td->td_pcb; 191 192 if (pcb->pcb_flags & PCB_VEC) { 193 save_vec_nodrop(td); 194 if (dst != NULL) { 195 len += elf32_populate_note(NT_PPC_VMX, 196 &pcb->pcb_vec, (char *)dst + len, 197 sizeof(pcb->pcb_vec), NULL); 198 } else 199 len += elf32_populate_note(NT_PPC_VMX, NULL, NULL, 200 sizeof(pcb->pcb_vec), NULL); 201 } 202 203 if (pcb->pcb_flags & PCB_VSX) { 204 save_fpu_nodrop(td); 205 if (dst != NULL) { 206 /* 207 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and 208 * VSR32-VSR63 overlap with VR0-VR31, so we only copy 209 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31. 210 */ 211 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) { 212 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2]; 213 vshr[vsr_idx] = *vsr_dw1; 214 } 215 len += elf32_populate_note(NT_PPC_VSX, 216 vshr, (char *)dst + len, 217 sizeof(vshr), NULL); 218 } else 219 len += elf32_populate_note(NT_PPC_VSX, NULL, NULL, 220 sizeof(vshr), NULL); 221 } 222 223 *off = len; 224 } 225 226 #ifndef __powerpc64__ 227 bool 228 elf_is_ifunc_reloc(Elf_Size r_info) 229 { 230 231 return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE); 232 } 233 234 /* Process one elf relocation with addend. */ 235 static int 236 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 237 int type, int local, elf_lookup_fn lookup) 238 { 239 Elf_Addr *where; 240 Elf_Half *hwhere; 241 Elf_Addr addr; 242 Elf_Addr addend, val; 243 Elf_Word rtype, symidx; 244 const Elf_Rela *rela; 245 int error; 246 247 switch (type) { 248 case ELF_RELOC_REL: 249 panic("PPC only supports RELA relocations"); 250 break; 251 case ELF_RELOC_RELA: 252 rela = (const Elf_Rela *)data; 253 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset); 254 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset); 255 addend = rela->r_addend; 256 rtype = ELF_R_TYPE(rela->r_info); 257 symidx = ELF_R_SYM(rela->r_info); 258 break; 259 default: 260 panic("elf_reloc: unknown relocation mode %d\n", type); 261 } 262 263 switch (rtype) { 264 case R_PPC_NONE: 265 break; 266 267 case R_PPC_ADDR32: /* word32 S + A */ 268 error = lookup(lf, symidx, 1, &addr); 269 if (error != 0) 270 return (-1); 271 *where = elf_relocaddr(lf, addr + addend); 272 break; 273 274 case R_PPC_ADDR16_LO: /* #lo(S) */ 275 error = lookup(lf, symidx, 1, &addr); 276 if (error != 0) 277 return (-1); 278 /* 279 * addend values are sometimes relative to sections 280 * (i.e. .rodata) in rela, where in reality they 281 * are relative to relocbase. Detect this condition. 282 */ 283 if (addr > relocbase && addr <= (relocbase + addend)) 284 addr = relocbase; 285 addr = elf_relocaddr(lf, addr + addend); 286 *hwhere = addr & 0xffff; 287 break; 288 289 case R_PPC_ADDR16_HA: /* #ha(S) */ 290 error = lookup(lf, symidx, 1, &addr); 291 if (error != 0) 292 return (-1); 293 /* 294 * addend values are sometimes relative to sections 295 * (i.e. .rodata) in rela, where in reality they 296 * are relative to relocbase. Detect this condition. 297 */ 298 if (addr > relocbase && addr <= (relocbase + addend)) 299 addr = relocbase; 300 addr = elf_relocaddr(lf, addr + addend); 301 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0)) 302 & 0xffff; 303 break; 304 305 case R_PPC_RELATIVE: /* word32 B + A */ 306 *where = elf_relocaddr(lf, relocbase + addend); 307 break; 308 309 case R_PPC_JMP_SLOT: /* PLT jump slot entry */ 310 /* 311 * We currently only support Secure-PLT jump slots. 312 * Given that we reject BSS-PLT modules during load, we 313 * don't need to check again. 314 * The method we are using here is equivilent to 315 * LD_BIND_NOW. 316 */ 317 error = lookup(lf, symidx, 1, &addr); 318 if (error != 0) 319 return (-1); 320 *where = elf_relocaddr(lf, addr + addend); 321 break; 322 323 case R_PPC_IRELATIVE: 324 addr = relocbase + addend; 325 val = ((Elf32_Addr (*)(void))addr)(); 326 if (*where != val) 327 *where = val; 328 break; 329 330 default: 331 printf("kldload: unexpected relocation type %d, " 332 "symbol index %d\n", (int)rtype, symidx); 333 return (-1); 334 } 335 return (0); 336 } 337 338 void 339 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase) 340 { 341 Elf_Rela *rela = NULL, *relalim; 342 Elf_Addr relasz = 0; 343 Elf_Addr *where; 344 345 /* 346 * Extract the rela/relasz values from the dynamic section 347 */ 348 for (; dynp->d_tag != DT_NULL; dynp++) { 349 switch (dynp->d_tag) { 350 case DT_RELA: 351 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr); 352 break; 353 case DT_RELASZ: 354 relasz = dynp->d_un.d_val; 355 break; 356 } 357 } 358 359 /* 360 * Relocate these values 361 */ 362 relalim = (Elf_Rela *)((caddr_t)rela + relasz); 363 for (; rela < relalim; rela++) { 364 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE) 365 continue; 366 where = (Elf_Addr *)(relocbase + rela->r_offset); 367 *where = (Elf_Addr)(relocbase + rela->r_addend); 368 } 369 } 370 371 int 372 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 373 elf_lookup_fn lookup) 374 { 375 376 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 377 } 378 379 int 380 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 381 int type, elf_lookup_fn lookup) 382 { 383 384 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup)); 385 } 386 387 int 388 elf_cpu_load_file(linker_file_t lf) 389 { 390 391 /* Only sync the cache for non-kernel modules */ 392 if (lf->id != 1) 393 __syncicache(lf->address, lf->size); 394 return (0); 395 } 396 397 int 398 elf_cpu_unload_file(linker_file_t lf __unused) 399 { 400 401 return (0); 402 } 403 404 static void 405 ppc32_runtime_resolve(void) 406 { 407 408 /* 409 * Since we don't support lazy binding, panic immediately if anyone 410 * manages to call the runtime resolver. 411 */ 412 panic("kldload: Runtime resolver was called unexpectedly!"); 413 } 414 415 int 416 elf_cpu_parse_dynamic(caddr_t loadbase, Elf_Dyn *dynamic) 417 { 418 Elf_Dyn *dp; 419 bool has_plt = false; 420 bool secure_plt = false; 421 Elf_Addr *got; 422 423 for (dp = dynamic; dp->d_tag != DT_NULL; dp++) { 424 switch (dp->d_tag) { 425 case DT_PPC_GOT: 426 secure_plt = true; 427 got = (Elf_Addr *)(loadbase + dp->d_un.d_ptr); 428 /* Install runtime resolver canary. */ 429 got[1] = (Elf_Addr)ppc32_runtime_resolve; 430 got[2] = (Elf_Addr)0; 431 break; 432 case DT_PLTGOT: 433 has_plt = true; 434 break; 435 } 436 } 437 438 if (has_plt && !secure_plt) { 439 printf("kldload: BSS-PLT modules are not supported.\n"); 440 return (-1); 441 } 442 return (0); 443 } 444 #endif 445 446 #ifdef __powerpc64__ 447 static void 448 ppc32_fixlimit(struct rlimit *rl, int which) 449 { 450 switch (which) { 451 case RLIMIT_DATA: 452 if (ppc32_maxdsiz != 0) { 453 if (rl->rlim_cur > ppc32_maxdsiz) 454 rl->rlim_cur = ppc32_maxdsiz; 455 if (rl->rlim_max > ppc32_maxdsiz) 456 rl->rlim_max = ppc32_maxdsiz; 457 } 458 break; 459 case RLIMIT_STACK: 460 if (ppc32_maxssiz != 0) { 461 if (rl->rlim_cur > ppc32_maxssiz) 462 rl->rlim_cur = ppc32_maxssiz; 463 if (rl->rlim_max > ppc32_maxssiz) 464 rl->rlim_max = ppc32_maxssiz; 465 } 466 break; 467 } 468 } 469 #endif 470