1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1994-1996 Søren Schmidt 5 * Copyright (c) 2018 Turing Robotic Industries Inc. 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 AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #define __ELF_WORD_SIZE 64 33 34 #include <sys/param.h> 35 #include <sys/elf.h> 36 #include <sys/exec.h> 37 #include <sys/imgact.h> 38 #include <sys/imgact_elf.h> 39 #include <sys/kernel.h> 40 #include <sys/ktr.h> 41 #include <sys/lock.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/proc.h> 45 #include <sys/stddef.h> 46 #include <sys/syscallsubr.h> 47 #include <sys/sysctl.h> 48 #include <sys/sysent.h> 49 50 #include <vm/pmap.h> 51 #include <vm/vm.h> 52 #include <vm/vm_map.h> 53 #include <vm/vm_page.h> 54 55 #include <arm64/linux/linux.h> 56 #include <arm64/linux/linux_proto.h> 57 #include <compat/linux/linux_dtrace.h> 58 #include <compat/linux/linux_elf.h> 59 #include <compat/linux/linux_emul.h> 60 #include <compat/linux/linux_fork.h> 61 #include <compat/linux/linux_ioctl.h> 62 #include <compat/linux/linux_mib.h> 63 #include <compat/linux/linux_misc.h> 64 #include <compat/linux/linux_signal.h> 65 #include <compat/linux/linux_util.h> 66 #include <compat/linux/linux_vdso.h> 67 68 #include <arm64/linux/linux_sigframe.h> 69 70 #include <machine/md_var.h> 71 #include <machine/pcb.h> 72 #ifdef VFP 73 #include <machine/vfp.h> 74 #endif 75 76 MODULE_VERSION(linux64elf, 1); 77 78 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 79 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - \ 80 LINUX_VDSOPAGE_SIZE) 81 #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE) 82 /* 83 * PAGE_SIZE - the size 84 * of the native SHAREDPAGE 85 */ 86 #define LINUX_USRSTACK LINUX_SHAREDPAGE 87 #define LINUX_PS_STRINGS (LINUX_USRSTACK - \ 88 sizeof(struct ps_strings)) 89 90 static int linux_szsigcode; 91 static vm_object_t linux_vdso_obj; 92 static char *linux_vdso_mapping; 93 extern char _binary_linux_vdso_so_o_start; 94 extern char _binary_linux_vdso_so_o_end; 95 static vm_offset_t linux_vdso_base; 96 97 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 98 extern const char *linux_syscallnames[]; 99 100 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 101 102 static void linux_vdso_install(const void *param); 103 static void linux_vdso_deinstall(const void *param); 104 static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 105 static void linux_set_syscall_retval(struct thread *td, int error); 106 static int linux_fetch_syscall_args(struct thread *td); 107 static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 108 uintptr_t stack); 109 static void linux_exec_sysvec_init(void *param); 110 static int linux_on_exec_vmspace(struct proc *p, 111 struct image_params *imgp); 112 113 /* DTrace init */ 114 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 115 116 /* DTrace probes */ 117 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo); 118 LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo); 119 120 LINUX_VDSO_SYM_CHAR(linux_platform); 121 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 122 LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode); 123 124 static int 125 linux_fetch_syscall_args(struct thread *td) 126 { 127 struct proc *p; 128 struct syscall_args *sa; 129 register_t *ap; 130 131 p = td->td_proc; 132 ap = td->td_frame->tf_x; 133 sa = &td->td_sa; 134 135 sa->code = td->td_frame->tf_x[8]; 136 sa->original_code = sa->code; 137 /* LINUXTODO: generic syscall? */ 138 if (sa->code >= p->p_sysent->sv_size) 139 sa->callp = &p->p_sysent->sv_table[0]; 140 else 141 sa->callp = &p->p_sysent->sv_table[sa->code]; 142 143 if (sa->callp->sy_narg > nitems(sa->args)) 144 panic("ARM64TODO: Could we have more than %zu args?", 145 nitems(sa->args)); 146 memcpy(sa->args, ap, nitems(sa->args) * sizeof(register_t)); 147 148 td->td_retval[0] = 0; 149 return (0); 150 } 151 152 static void 153 linux_set_syscall_retval(struct thread *td, int error) 154 { 155 156 td->td_retval[1] = td->td_frame->tf_x[1]; 157 cpu_set_syscall_retval(td, error); 158 159 if (__predict_false(error != 0)) { 160 if (error != ERESTART && error != EJUSTRETURN) 161 td->td_frame->tf_x[0] = bsd_to_linux_errno(error); 162 } 163 } 164 165 static int 166 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 167 { 168 Elf_Auxargs *args; 169 Elf_Auxinfo *argarray, *pos; 170 struct proc *p; 171 int error, issetugid; 172 173 LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo); 174 p = imgp->proc; 175 176 args = (Elf64_Auxargs *)imgp->auxargs; 177 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 178 M_WAITOK | M_ZERO); 179 180 issetugid = p->p_flag & P_SUGID ? 1 : 0; 181 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 182 AUXARGS_ENTRY(pos, LINUX_AT_MINSIGSTKSZ, LINUX_MINSIGSTKSZ); 183 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap); 184 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 185 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 186 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 187 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 188 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 189 AUXARGS_ENTRY(pos, AT_BASE, args->base); 190 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 191 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 192 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 193 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 194 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 195 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 196 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 197 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 198 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2); 199 if (imgp->execpathp != 0) 200 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 201 if (args->execfd != -1) 202 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 203 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 204 AUXARGS_ENTRY(pos, AT_NULL, 0); 205 206 free(imgp->auxargs, M_TEMP); 207 imgp->auxargs = NULL; 208 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 209 210 error = copyout(argarray, (void *)base, 211 sizeof(*argarray) * LINUX_AT_COUNT); 212 free(argarray, M_TEMP); 213 return (error); 214 } 215 216 /* 217 * Reset registers to default values on exec. 218 */ 219 static void 220 linux_exec_setregs(struct thread *td, struct image_params *imgp, 221 uintptr_t stack) 222 { 223 struct trapframe *regs = td->td_frame; 224 struct pcb *pcb = td->td_pcb; 225 226 /* LINUXTODO: validate */ 227 LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo); 228 229 memset(regs, 0, sizeof(*regs)); 230 /* glibc start.S registers function pointer in x0 with atexit. */ 231 regs->tf_sp = stack; 232 #if 0 /* LINUXTODO: See if this is used. */ 233 regs->tf_lr = imgp->entry_addr; 234 #else 235 regs->tf_lr = 0xffffffffffffffff; 236 #endif 237 regs->tf_elr = imgp->entry_addr; 238 239 pcb->pcb_tpidr_el0 = 0; 240 pcb->pcb_tpidrro_el0 = 0; 241 WRITE_SPECIALREG(tpidrro_el0, 0); 242 WRITE_SPECIALREG(tpidr_el0, 0); 243 244 #ifdef VFP 245 vfp_reset_state(td, pcb); 246 #endif 247 248 /* 249 * Clear debug register state. It is not applicable to the new process. 250 */ 251 bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs)); 252 } 253 254 int 255 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 256 { 257 struct l_sigframe *frame; 258 ucontext_t uc; 259 struct trapframe *tf; 260 int error; 261 262 tf = td->td_frame; 263 frame = (struct l_sigframe *)tf->tf_sp; 264 265 if (copyin((void *)&frame->uc, &uc, sizeof(uc))) 266 return (EFAULT); 267 268 error = set_mcontext(td, &uc.uc_mcontext); 269 if (error != 0) 270 return (error); 271 272 /* Restore signal mask. */ 273 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0); 274 275 return (EJUSTRETURN); 276 } 277 278 static void 279 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 280 { 281 struct thread *td; 282 struct proc *p; 283 struct trapframe *tf; 284 struct l_sigframe *fp, *frame; 285 struct l_fpsimd_context *fpsimd; 286 struct l_esr_context *esr; 287 l_stack_t uc_stack; 288 ucontext_t uc; 289 uint8_t *scr; 290 struct sigacts *psp; 291 int onstack, sig, issiginfo; 292 293 td = curthread; 294 p = td->td_proc; 295 PROC_LOCK_ASSERT(p, MA_OWNED); 296 297 sig = ksi->ksi_signo; 298 psp = p->p_sigacts; 299 mtx_assert(&psp->ps_mtx, MA_OWNED); 300 301 tf = td->td_frame; 302 onstack = sigonstack(tf->tf_sp); 303 issiginfo = SIGISMEMBER(psp->ps_siginfo, sig); 304 305 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm, 306 catcher, sig); 307 308 /* Allocate and validate space for the signal handler context. */ 309 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack && 310 SIGISMEMBER(psp->ps_sigonstack, sig)) { 311 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 312 td->td_sigstk.ss_size); 313 #if defined(COMPAT_43) 314 td->td_sigstk.ss_flags |= SS_ONSTACK; 315 #endif 316 } else { 317 fp = (struct l_sigframe *)td->td_frame->tf_sp; 318 } 319 320 /* Make room, keeping the stack aligned */ 321 fp--; 322 fp = (struct l_sigframe *)STACKALIGN(fp); 323 324 get_mcontext(td, &uc.uc_mcontext, 0); 325 uc.uc_sigmask = *mask; 326 327 uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 328 uc_stack.ss_size = td->td_sigstk.ss_size; 329 uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ? 330 (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 331 mtx_unlock(&psp->ps_mtx); 332 PROC_UNLOCK(td->td_proc); 333 334 /* Fill in the frame to copy out */ 335 frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO); 336 337 memcpy(&frame->sf.sf_uc.uc_sc.regs, tf->tf_x, sizeof(tf->tf_x)); 338 frame->sf.sf_uc.uc_sc.regs[30] = tf->tf_lr; 339 frame->sf.sf_uc.uc_sc.sp = tf->tf_sp; 340 frame->sf.sf_uc.uc_sc.pc = tf->tf_lr; 341 frame->sf.sf_uc.uc_sc.pstate = tf->tf_spsr; 342 frame->sf.sf_uc.uc_sc.fault_address = (register_t)ksi->ksi_addr; 343 344 /* Stack frame for unwinding */ 345 frame->fp = tf->tf_x[29]; 346 frame->lr = tf->tf_lr; 347 348 /* Translate the signal. */ 349 sig = bsd_to_linux_signal(sig); 350 siginfo_to_lsiginfo(&ksi->ksi_info, &frame->sf.sf_si, sig); 351 bsd_to_linux_sigset(mask, &frame->sf.sf_uc.uc_sigmask); 352 353 /* 354 * Prepare fpsimd & esr. Does not check sizes, as 355 * __reserved is big enougth. 356 */ 357 scr = (uint8_t *)&frame->sf.sf_uc.uc_sc.__reserved; 358 #ifdef VFP 359 fpsimd = (struct l_fpsimd_context *) scr; 360 fpsimd->head.magic = L_FPSIMD_MAGIC; 361 fpsimd->head.size = sizeof(struct l_fpsimd_context); 362 fpsimd->fpsr = uc.uc_mcontext.mc_fpregs.fp_sr; 363 fpsimd->fpcr = uc.uc_mcontext.mc_fpregs.fp_cr; 364 365 memcpy(fpsimd->vregs, &uc.uc_mcontext.mc_fpregs.fp_q, 366 sizeof(uc.uc_mcontext.mc_fpregs.fp_q)); 367 scr += roundup(sizeof(struct l_fpsimd_context), 16); 368 #endif 369 if (ksi->ksi_addr != 0) { 370 esr = (struct l_esr_context *) scr; 371 esr->head.magic = L_ESR_MAGIC; 372 esr->head.size = sizeof(struct l_esr_context); 373 esr->esr = tf->tf_esr; 374 } 375 376 memcpy(&frame->sf.sf_uc.uc_stack, &uc_stack, sizeof(uc_stack)); 377 memcpy(&frame->uc, &uc, sizeof(uc)); 378 379 /* Copy the sigframe out to the user's stack. */ 380 if (copyout(frame, fp, sizeof(*fp)) != 0) { 381 /* Process has trashed its stack. Kill it. */ 382 free(frame, M_LINUX); 383 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp); 384 PROC_LOCK(p); 385 sigexit(td, SIGILL); 386 } 387 free(frame, M_LINUX); 388 389 tf->tf_x[0]= sig; 390 if (issiginfo) { 391 tf->tf_x[1] = (register_t)&fp->sf.sf_si; 392 tf->tf_x[2] = (register_t)&fp->sf.sf_uc; 393 } else { 394 tf->tf_x[1] = 0; 395 tf->tf_x[2] = 0; 396 } 397 tf->tf_x[8] = (register_t)catcher; 398 tf->tf_sp = (register_t)fp; 399 tf->tf_elr = (register_t)linux_vdso_sigcode; 400 401 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr, 402 tf->tf_sp); 403 404 PROC_LOCK(p); 405 mtx_lock(&psp->ps_mtx); 406 } 407 408 struct sysentvec elf_linux_sysvec = { 409 .sv_size = LINUX_SYS_MAXSYSCALL, 410 .sv_table = linux_sysent, 411 .sv_fixup = __elfN(freebsd_fixup), 412 .sv_sendsig = linux_rt_sendsig, 413 .sv_sigcode = &_binary_linux_vdso_so_o_start, 414 .sv_szsigcode = &linux_szsigcode, 415 .sv_name = "Linux ELF64", 416 .sv_coredump = elf64_coredump, 417 .sv_elf_core_osabi = ELFOSABI_NONE, 418 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 419 .sv_elf_core_prepare_notes = linux64_prepare_notes, 420 .sv_imgact_try = linux_exec_imgact_try, 421 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 422 .sv_minuser = VM_MIN_ADDRESS, 423 .sv_maxuser = VM_MAXUSER_ADDRESS, 424 .sv_usrstack = LINUX_USRSTACK, 425 .sv_psstrings = LINUX_PS_STRINGS, 426 .sv_psstringssz = sizeof(struct ps_strings), 427 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 428 .sv_copyout_auxargs = linux_copyout_auxargs, 429 .sv_copyout_strings = __linuxN(copyout_strings), 430 .sv_setregs = linux_exec_setregs, 431 .sv_fixlimit = NULL, 432 .sv_maxssiz = NULL, 433 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 434 SV_SIG_WAITNDQ | SV_TIMEKEEP, 435 .sv_set_syscall_retval = linux_set_syscall_retval, 436 .sv_fetch_syscall_args = linux_fetch_syscall_args, 437 .sv_syscallnames = linux_syscallnames, 438 .sv_shared_page_base = LINUX_SHAREDPAGE, 439 .sv_shared_page_len = PAGE_SIZE, 440 .sv_schedtail = linux_schedtail, 441 .sv_thread_detach = linux_thread_detach, 442 .sv_trap = NULL, 443 .sv_hwcap = &elf_hwcap, 444 .sv_hwcap2 = &elf_hwcap2, 445 .sv_onexec = linux_on_exec_vmspace, 446 .sv_onexit = linux_on_exit, 447 .sv_ontdexit = linux_thread_dtor, 448 .sv_setid_allowed = &linux_setid_allowed_query, 449 }; 450 451 static int 452 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 453 { 454 int error; 455 456 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base, 457 LINUX_VDSOPAGE_SIZE, imgp); 458 if (error == 0) 459 linux_on_exec(p, imgp); 460 return (error); 461 } 462 463 /* 464 * linux_vdso_install() and linux_exec_sysvec_init() must be called 465 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 466 */ 467 static void 468 linux_exec_sysvec_init(void *param) 469 { 470 l_uintptr_t *ktimekeep_base; 471 struct sysentvec *sv; 472 ptrdiff_t tkoff; 473 474 sv = param; 475 /* Fill timekeep_base */ 476 exec_sysvec_init(sv); 477 478 tkoff = kern_timekeep_base - linux_vdso_base; 479 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 480 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset; 481 } 482 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 483 linux_exec_sysvec_init, &elf_linux_sysvec); 484 485 static void 486 linux_vdso_install(const void *param) 487 { 488 char *vdso_start = &_binary_linux_vdso_so_o_start; 489 char *vdso_end = &_binary_linux_vdso_so_o_end; 490 491 linux_szsigcode = vdso_end - vdso_start; 492 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 493 494 linux_vdso_base = LINUX_VDSOPAGE; 495 496 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 497 498 linux_vdso_obj = __elfN(linux_shared_page_init) 499 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 500 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 501 502 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 503 } 504 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 505 linux_vdso_install, NULL); 506 507 static void 508 linux_vdso_deinstall(const void *param) 509 { 510 511 __elfN(linux_shared_page_fini)(linux_vdso_obj, 512 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 513 } 514 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 515 linux_vdso_deinstall, NULL); 516 517 static void 518 linux_vdso_reloc(char *mapping, Elf_Addr offset) 519 { 520 Elf_Size rtype, symidx; 521 const Elf_Rela *rela; 522 const Elf_Shdr *shdr; 523 const Elf_Ehdr *ehdr; 524 Elf_Addr *where; 525 Elf_Addr addr, addend; 526 int i, relacnt; 527 528 MPASS(offset != 0); 529 530 relacnt = 0; 531 ehdr = (const Elf_Ehdr *)mapping; 532 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 533 for (i = 0; i < ehdr->e_shnum; i++) 534 { 535 switch (shdr[i].sh_type) { 536 case SHT_REL: 537 printf("Linux Aarch64 vDSO: unexpected Rel section\n"); 538 break; 539 case SHT_RELA: 540 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset); 541 relacnt = shdr[i].sh_size / sizeof(*rela); 542 } 543 } 544 545 for (i = 0; i < relacnt; i++, rela++) { 546 where = (Elf_Addr *)(mapping + rela->r_offset); 547 addend = rela->r_addend; 548 rtype = ELF_R_TYPE(rela->r_info); 549 symidx = ELF_R_SYM(rela->r_info); 550 551 switch (rtype) { 552 case R_AARCH64_NONE: /* none */ 553 break; 554 555 case R_AARCH64_RELATIVE: /* B + A */ 556 addr = (Elf_Addr)(mapping + addend); 557 if (*where != addr) 558 *where = addr; 559 break; 560 default: 561 printf("Linux Aarch64 vDSO: unexpected relocation type %ld, " 562 "symbol index %ld\n", rtype, symidx); 563 } 564 } 565 } 566 567 static Elf_Brandnote linux64_brandnote = { 568 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 569 .hdr.n_descsz = 16, 570 .hdr.n_type = 1, 571 .vendor = GNU_ABI_VENDOR, 572 .flags = BN_TRANSLATE_OSREL, 573 .trans_osrel = linux_trans_osrel 574 }; 575 576 static Elf64_Brandinfo linux_glibc2brand = { 577 .brand = ELFOSABI_LINUX, 578 .machine = EM_AARCH64, 579 .compat_3_brand = "Linux", 580 .emul_path = linux_emul_path, 581 .interp_path = "/lib64/ld-linux-x86-64.so.2", 582 .sysvec = &elf_linux_sysvec, 583 .interp_newpath = NULL, 584 .brand_note = &linux64_brandnote, 585 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 586 }; 587 588 Elf64_Brandinfo *linux_brandlist[] = { 589 &linux_glibc2brand, 590 NULL 591 }; 592 593 static int 594 linux64_elf_modevent(module_t mod, int type, void *data) 595 { 596 Elf64_Brandinfo **brandinfo; 597 struct linux_ioctl_handler**lihp; 598 int error; 599 600 error = 0; 601 switch(type) { 602 case MOD_LOAD: 603 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 604 ++brandinfo) 605 if (elf64_insert_brand_entry(*brandinfo) < 0) 606 error = EINVAL; 607 if (error == 0) { 608 SET_FOREACH(lihp, linux_ioctl_handler_set) 609 linux_ioctl_register_handler(*lihp); 610 stclohz = (stathz ? stathz : hz); 611 if (bootverbose) 612 printf("Linux arm64 ELF exec handler installed\n"); 613 } 614 break; 615 case MOD_UNLOAD: 616 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 617 ++brandinfo) 618 if (elf64_brand_inuse(*brandinfo)) 619 error = EBUSY; 620 if (error == 0) { 621 for (brandinfo = &linux_brandlist[0]; 622 *brandinfo != NULL; ++brandinfo) 623 if (elf64_remove_brand_entry(*brandinfo) < 0) 624 error = EINVAL; 625 } 626 if (error == 0) { 627 SET_FOREACH(lihp, linux_ioctl_handler_set) 628 linux_ioctl_unregister_handler(*lihp); 629 if (bootverbose) 630 printf("Linux arm64 ELF exec handler removed\n"); 631 } else 632 printf("Could not deinstall Linux arm64 ELF interpreter entry\n"); 633 break; 634 default: 635 return (EOPNOTSUPP); 636 } 637 return (error); 638 } 639 640 static moduledata_t linux64_elf_mod = { 641 "linux64elf", 642 linux64_elf_modevent, 643 0 644 }; 645 646 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 647 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 648 FEATURE(linux64, "AArch64 Linux 64bit support"); 649