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 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/cdefs.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/signalvar.h> 47 #include <sys/syscallsubr.h> 48 #include <sys/sysctl.h> 49 #include <sys/sysent.h> 50 51 #include <vm/vm.h> 52 #include <vm/pmap.h> 53 #include <vm/vm_map.h> 54 #include <vm/vm_extern.h> 55 #include <vm/vm_object.h> 56 #include <vm/vm_page.h> 57 #include <vm/vm_param.h> 58 59 #include <arm64/linux/linux.h> 60 #include <arm64/linux/linux_proto.h> 61 #include <compat/linux/linux_dtrace.h> 62 #include <compat/linux/linux_emul.h> 63 #include <compat/linux/linux_fork.h> 64 #include <compat/linux/linux_ioctl.h> 65 #include <compat/linux/linux_mib.h> 66 #include <compat/linux/linux_misc.h> 67 #include <compat/linux/linux_signal.h> 68 #include <compat/linux/linux_util.h> 69 #include <compat/linux/linux_vdso.h> 70 71 #include <arm64/linux/linux_sigframe.h> 72 73 #include <machine/md_var.h> 74 75 #ifdef VFP 76 #include <machine/vfp.h> 77 #endif 78 79 MODULE_VERSION(linux64elf, 1); 80 81 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 82 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - \ 83 LINUX_VDSOPAGE_SIZE) 84 #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE) 85 /* 86 * PAGE_SIZE - the size 87 * of the native SHAREDPAGE 88 */ 89 #define LINUX_USRSTACK LINUX_SHAREDPAGE 90 #define LINUX_PS_STRINGS (LINUX_USRSTACK - \ 91 sizeof(struct ps_strings)) 92 93 static int linux_szsigcode; 94 static vm_object_t linux_vdso_obj; 95 static char *linux_vdso_mapping; 96 extern char _binary_linux_vdso_so_o_start; 97 extern char _binary_linux_vdso_so_o_end; 98 static vm_offset_t linux_vdso_base; 99 100 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 101 102 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 103 104 static int linux_copyout_strings(struct image_params *imgp, 105 uintptr_t *stack_base); 106 static int linux_elf_fixup(uintptr_t *stack_base, 107 struct image_params *iparams); 108 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 109 static void linux_vdso_install(const void *param); 110 static void linux_vdso_deinstall(const void *param); 111 static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 112 static void linux_set_syscall_retval(struct thread *td, int error); 113 static int linux_fetch_syscall_args(struct thread *td); 114 static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 115 uintptr_t stack); 116 static void linux_exec_sysvec_init(void *param); 117 static int linux_on_exec_vmspace(struct proc *p, 118 struct image_params *imgp); 119 120 /* DTrace init */ 121 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 122 123 /* DTrace probes */ 124 LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int"); 125 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo); 126 LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo); 127 LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo); 128 129 LINUX_VDSO_SYM_CHAR(linux_platform); 130 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 131 LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode); 132 133 /* LINUXTODO: do we have traps to translate? */ 134 static int 135 linux_translate_traps(int signal, int trap_code) 136 { 137 138 LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code); 139 return (signal); 140 } 141 142 static int 143 linux_fetch_syscall_args(struct thread *td) 144 { 145 struct proc *p; 146 struct syscall_args *sa; 147 register_t *ap; 148 149 p = td->td_proc; 150 ap = td->td_frame->tf_x; 151 sa = &td->td_sa; 152 153 sa->code = td->td_frame->tf_x[8]; 154 sa->original_code = sa->code; 155 /* LINUXTODO: generic syscall? */ 156 if (sa->code >= p->p_sysent->sv_size) 157 sa->callp = &p->p_sysent->sv_table[0]; 158 else 159 sa->callp = &p->p_sysent->sv_table[sa->code]; 160 161 if (sa->callp->sy_narg > nitems(sa->args)) 162 panic("ARM64TODO: Could we have more than %zu args?", 163 nitems(sa->args)); 164 memcpy(sa->args, ap, nitems(sa->args) * sizeof(register_t)); 165 166 td->td_retval[0] = 0; 167 return (0); 168 } 169 170 static void 171 linux_set_syscall_retval(struct thread *td, int error) 172 { 173 174 td->td_retval[1] = td->td_frame->tf_x[1]; 175 cpu_set_syscall_retval(td, error); 176 177 if (__predict_false(error != 0)) { 178 if (error != ERESTART && error != EJUSTRETURN) 179 td->td_frame->tf_x[0] = bsd_to_linux_errno(error); 180 } 181 } 182 183 static int 184 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 185 { 186 Elf_Auxargs *args; 187 Elf_Auxinfo *argarray, *pos; 188 struct proc *p; 189 int error, issetugid; 190 191 LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo); 192 p = imgp->proc; 193 194 args = (Elf64_Auxargs *)imgp->auxargs; 195 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 196 M_WAITOK | M_ZERO); 197 198 issetugid = p->p_flag & P_SUGID ? 1 : 0; 199 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 200 AUXARGS_ENTRY(pos, LINUX_AT_MINSIGSTKSZ, LINUX_MINSIGSTKSZ); 201 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap); 202 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 203 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 204 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 205 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 206 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 207 AUXARGS_ENTRY(pos, AT_BASE, args->base); 208 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 209 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 210 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 211 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 212 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 213 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 214 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 215 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 216 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2); 217 if (imgp->execpathp != 0) 218 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 219 if (args->execfd != -1) 220 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 221 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 222 AUXARGS_ENTRY(pos, AT_NULL, 0); 223 224 free(imgp->auxargs, M_TEMP); 225 imgp->auxargs = NULL; 226 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 227 228 error = copyout(argarray, (void *)base, 229 sizeof(*argarray) * LINUX_AT_COUNT); 230 free(argarray, M_TEMP); 231 return (error); 232 } 233 234 static int 235 linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp) 236 { 237 238 LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo); 239 240 return (0); 241 } 242 243 /* 244 * Copy strings out to the new process address space, constructing new arg 245 * and env vector tables. Return a pointer to the base so that it can be used 246 * as the initial stack pointer. 247 * LINUXTODO: deduplicate against other linuxulator archs 248 */ 249 static int 250 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 251 { 252 char **vectp; 253 char *stringp; 254 uintptr_t destp, ustringp; 255 struct ps_strings *arginfo; 256 char canary[LINUX_AT_RANDOM_LEN]; 257 size_t execpath_len; 258 struct proc *p; 259 int argc, envc, error; 260 261 p = imgp->proc; 262 arginfo = (struct ps_strings *)PROC_PS_STRINGS(p); 263 destp = (uintptr_t)arginfo; 264 265 if (imgp->execpath != NULL && imgp->auxargs != NULL) { 266 execpath_len = strlen(imgp->execpath) + 1; 267 destp -= execpath_len; 268 destp = rounddown2(destp, sizeof(void *)); 269 imgp->execpathp = (void *)destp; 270 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 271 if (error != 0) 272 return (error); 273 } 274 275 /* Prepare the canary for SSP. */ 276 arc4rand(canary, sizeof(canary), 0); 277 destp -= roundup(sizeof(canary), sizeof(void *)); 278 imgp->canary = (void *)destp; 279 error = copyout(canary, imgp->canary, sizeof(canary)); 280 if (error != 0) 281 return (error); 282 283 /* Allocate room for the argument and environment strings. */ 284 destp -= ARG_MAX - imgp->args->stringspace; 285 destp = rounddown2(destp, sizeof(void *)); 286 ustringp = destp; 287 288 if (imgp->auxargs) { 289 /* 290 * Allocate room on the stack for the ELF auxargs 291 * array. It has up to LINUX_AT_COUNT entries. 292 */ 293 destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo); 294 destp = rounddown2(destp, sizeof(void *)); 295 } 296 297 vectp = (char **)destp; 298 299 /* 300 * Allocate room for argc and the argv[] and env vectors including the 301 * terminating NULL pointers. 302 */ 303 vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1; 304 vectp = (char **)STACKALIGN(vectp); 305 306 /* vectp also becomes our initial stack base. */ 307 *stack_base = (uintptr_t)vectp; 308 309 stringp = imgp->args->begin_argv; 310 argc = imgp->args->argc; 311 envc = imgp->args->envc; 312 313 /* Copy out strings - arguments and environment. */ 314 error = copyout(stringp, (void *)ustringp, 315 ARG_MAX - imgp->args->stringspace); 316 if (error != 0) 317 return (error); 318 319 /* Fill in "ps_strings" struct for ps, w, etc. */ 320 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 321 suword(&arginfo->ps_nargvstr, argc) != 0) 322 return (EFAULT); 323 324 if (suword(vectp++, argc) != 0) 325 return (EFAULT); 326 327 /* Fill in argument portion of vector table. */ 328 for (; argc > 0; --argc) { 329 if (suword(vectp++, ustringp) != 0) 330 return (EFAULT); 331 while (*stringp++ != 0) 332 ustringp++; 333 ustringp++; 334 } 335 336 /* A null vector table pointer separates the argp's from the envp's. */ 337 if (suword(vectp++, 0) != 0) 338 return (EFAULT); 339 340 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 341 suword(&arginfo->ps_nenvstr, envc) != 0) 342 return (EFAULT); 343 344 /* Fill in environment portion of vector table. */ 345 for (; envc > 0; --envc) { 346 if (suword(vectp++, ustringp) != 0) 347 return (EFAULT); 348 while (*stringp++ != 0) 349 ustringp++; 350 ustringp++; 351 } 352 353 /* The end of the vector table is a null pointer. */ 354 if (suword(vectp, 0) != 0) 355 return (EFAULT); 356 357 if (imgp->auxargs) { 358 vectp++; 359 error = imgp->sysent->sv_copyout_auxargs(imgp, 360 (uintptr_t)vectp); 361 if (error != 0) 362 return (error); 363 } 364 365 return (0); 366 } 367 368 /* 369 * Reset registers to default values on exec. 370 */ 371 static void 372 linux_exec_setregs(struct thread *td, struct image_params *imgp, 373 uintptr_t stack) 374 { 375 struct trapframe *regs = td->td_frame; 376 struct pcb *pcb = td->td_pcb; 377 378 /* LINUXTODO: validate */ 379 LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo); 380 381 memset(regs, 0, sizeof(*regs)); 382 /* glibc start.S registers function pointer in x0 with atexit. */ 383 regs->tf_sp = stack; 384 #if 0 /* LINUXTODO: See if this is used. */ 385 regs->tf_lr = imgp->entry_addr; 386 #else 387 regs->tf_lr = 0xffffffffffffffff; 388 #endif 389 regs->tf_elr = imgp->entry_addr; 390 391 pcb->pcb_tpidr_el0 = 0; 392 pcb->pcb_tpidrro_el0 = 0; 393 WRITE_SPECIALREG(tpidrro_el0, 0); 394 WRITE_SPECIALREG(tpidr_el0, 0); 395 396 #ifdef VFP 397 vfp_reset_state(td, pcb); 398 #endif 399 400 /* 401 * Clear debug register state. It is not applicable to the new process. 402 */ 403 bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs)); 404 } 405 406 int 407 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 408 { 409 struct l_sigframe *frame; 410 ucontext_t uc; 411 struct trapframe *tf; 412 int error; 413 414 tf = td->td_frame; 415 frame = (struct l_sigframe *)tf->tf_sp; 416 417 if (copyin((void *)&frame->uc, &uc, sizeof(uc))) 418 return (EFAULT); 419 420 error = set_mcontext(td, &uc.uc_mcontext); 421 if (error != 0) 422 return (error); 423 424 /* Restore signal mask. */ 425 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0); 426 427 return (EJUSTRETURN); 428 } 429 430 static void 431 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 432 { 433 struct thread *td; 434 struct proc *p; 435 struct trapframe *tf; 436 struct l_sigframe *fp, *frame; 437 struct l_fpsimd_context *fpsimd; 438 struct l_esr_context *esr; 439 l_stack_t uc_stack; 440 ucontext_t uc; 441 uint8_t *scr; 442 struct sigacts *psp; 443 int onstack, sig; 444 445 td = curthread; 446 p = td->td_proc; 447 PROC_LOCK_ASSERT(p, MA_OWNED); 448 449 sig = ksi->ksi_signo; 450 psp = p->p_sigacts; 451 mtx_assert(&psp->ps_mtx, MA_OWNED); 452 453 tf = td->td_frame; 454 onstack = sigonstack(tf->tf_sp); 455 456 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm, 457 catcher, sig); 458 459 /* Allocate and validate space for the signal handler context. */ 460 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack && 461 SIGISMEMBER(psp->ps_sigonstack, sig)) { 462 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 463 td->td_sigstk.ss_size); 464 #if defined(COMPAT_43) 465 td->td_sigstk.ss_flags |= SS_ONSTACK; 466 #endif 467 } else { 468 fp = (struct l_sigframe *)td->td_frame->tf_sp; 469 } 470 471 /* Make room, keeping the stack aligned */ 472 fp--; 473 fp = (struct l_sigframe *)STACKALIGN(fp); 474 475 get_mcontext(td, &uc.uc_mcontext, 0); 476 uc.uc_sigmask = *mask; 477 478 uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 479 uc_stack.ss_size = td->td_sigstk.ss_size; 480 uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ? 481 (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 482 mtx_unlock(&psp->ps_mtx); 483 PROC_UNLOCK(td->td_proc); 484 485 /* Fill in the frame to copy out */ 486 frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO); 487 488 memcpy(&frame->sf.sf_uc.uc_sc.regs, tf->tf_x, sizeof(tf->tf_x)); 489 frame->sf.sf_uc.uc_sc.regs[30] = tf->tf_lr; 490 frame->sf.sf_uc.uc_sc.sp = tf->tf_sp; 491 frame->sf.sf_uc.uc_sc.pc = tf->tf_lr; 492 frame->sf.sf_uc.uc_sc.pstate = tf->tf_spsr; 493 frame->sf.sf_uc.uc_sc.fault_address = (register_t)ksi->ksi_addr; 494 495 /* Stack frame for unwinding */ 496 frame->fp = tf->tf_x[29]; 497 frame->lr = tf->tf_lr; 498 499 /* Translate the signal. */ 500 sig = bsd_to_linux_signal(sig); 501 siginfo_to_lsiginfo(&ksi->ksi_info, &frame->sf.sf_si, sig); 502 bsd_to_linux_sigset(mask, &frame->sf.sf_uc.uc_sigmask); 503 504 /* 505 * Prepare fpsimd & esr. Does not check sizes, as 506 * __reserved is big enougth. 507 */ 508 scr = (uint8_t *)&frame->sf.sf_uc.uc_sc.__reserved; 509 #ifdef VFP 510 fpsimd = (struct l_fpsimd_context *) scr; 511 fpsimd->head.magic = L_FPSIMD_MAGIC; 512 fpsimd->head.size = sizeof(struct l_fpsimd_context); 513 fpsimd->fpsr = uc.uc_mcontext.mc_fpregs.fp_sr; 514 fpsimd->fpcr = uc.uc_mcontext.mc_fpregs.fp_cr; 515 516 memcpy(fpsimd->vregs, &uc.uc_mcontext.mc_fpregs.fp_q, 517 sizeof(uc.uc_mcontext.mc_fpregs.fp_q)); 518 scr += roundup(sizeof(struct l_fpsimd_context), 16); 519 #endif 520 if (ksi->ksi_addr != 0) { 521 esr = (struct l_esr_context *) scr; 522 esr->head.magic = L_ESR_MAGIC; 523 esr->head.size = sizeof(struct l_esr_context); 524 esr->esr = tf->tf_esr; 525 } 526 527 memcpy(&frame->sf.sf_uc.uc_stack, &uc_stack, sizeof(uc_stack)); 528 memcpy(&frame->uc, &uc, sizeof(uc)); 529 530 /* Copy the sigframe out to the user's stack. */ 531 if (copyout(frame, fp, sizeof(*fp)) != 0) { 532 /* Process has trashed its stack. Kill it. */ 533 free(frame, M_LINUX); 534 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp); 535 PROC_LOCK(p); 536 sigexit(td, SIGILL); 537 } 538 free(frame, M_LINUX); 539 540 tf->tf_x[0]= sig; 541 tf->tf_x[1] = (register_t)&fp->sf.sf_si; 542 tf->tf_x[2] = (register_t)&fp->sf.sf_uc; 543 tf->tf_x[8] = (register_t)catcher; 544 tf->tf_sp = (register_t)fp; 545 tf->tf_elr = (register_t)linux_vdso_sigcode; 546 547 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr, 548 tf->tf_sp); 549 550 PROC_LOCK(p); 551 mtx_lock(&psp->ps_mtx); 552 } 553 554 struct sysentvec elf_linux_sysvec = { 555 .sv_size = LINUX_SYS_MAXSYSCALL, 556 .sv_table = linux_sysent, 557 .sv_transtrap = linux_translate_traps, 558 .sv_fixup = linux_elf_fixup, 559 .sv_sendsig = linux_rt_sendsig, 560 .sv_sigcode = &_binary_linux_vdso_so_o_start, 561 .sv_szsigcode = &linux_szsigcode, 562 .sv_name = "Linux ELF64", 563 .sv_coredump = elf64_coredump, 564 .sv_elf_core_osabi = ELFOSABI_NONE, 565 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 566 .sv_elf_core_prepare_notes = linux64_prepare_notes, 567 .sv_imgact_try = linux_exec_imgact_try, 568 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 569 .sv_minuser = VM_MIN_ADDRESS, 570 .sv_maxuser = VM_MAXUSER_ADDRESS, 571 .sv_usrstack = LINUX_USRSTACK, 572 .sv_psstrings = LINUX_PS_STRINGS, 573 .sv_psstringssz = sizeof(struct ps_strings), 574 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 575 .sv_copyout_auxargs = linux_copyout_auxargs, 576 .sv_copyout_strings = linux_copyout_strings, 577 .sv_setregs = linux_exec_setregs, 578 .sv_fixlimit = NULL, 579 .sv_maxssiz = NULL, 580 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 581 SV_SIG_WAITNDQ | SV_TIMEKEEP, 582 .sv_set_syscall_retval = linux_set_syscall_retval, 583 .sv_fetch_syscall_args = linux_fetch_syscall_args, 584 .sv_syscallnames = NULL, 585 .sv_shared_page_base = LINUX_SHAREDPAGE, 586 .sv_shared_page_len = PAGE_SIZE, 587 .sv_schedtail = linux_schedtail, 588 .sv_thread_detach = linux_thread_detach, 589 .sv_trap = NULL, 590 .sv_hwcap = &elf_hwcap, 591 .sv_hwcap2 = &elf_hwcap2, 592 .sv_onexec = linux_on_exec_vmspace, 593 .sv_onexit = linux_on_exit, 594 .sv_ontdexit = linux_thread_dtor, 595 .sv_setid_allowed = &linux_setid_allowed_query, 596 }; 597 598 static int 599 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 600 { 601 int error; 602 603 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base, 604 LINUX_VDSOPAGE_SIZE, imgp); 605 if (error == 0) 606 linux_on_exec(p, imgp); 607 return (error); 608 } 609 610 /* 611 * linux_vdso_install() and linux_exec_sysvec_init() must be called 612 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 613 */ 614 static void 615 linux_exec_sysvec_init(void *param) 616 { 617 l_uintptr_t *ktimekeep_base; 618 struct sysentvec *sv; 619 ptrdiff_t tkoff; 620 621 sv = param; 622 /* Fill timekeep_base */ 623 exec_sysvec_init(sv); 624 625 tkoff = kern_timekeep_base - linux_vdso_base; 626 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 627 *ktimekeep_base = sv->sv_timekeep_base; 628 } 629 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 630 linux_exec_sysvec_init, &elf_linux_sysvec); 631 632 static void 633 linux_vdso_install(const void *param) 634 { 635 char *vdso_start = &_binary_linux_vdso_so_o_start; 636 char *vdso_end = &_binary_linux_vdso_so_o_end; 637 638 linux_szsigcode = vdso_end - vdso_start; 639 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 640 641 linux_vdso_base = LINUX_VDSOPAGE; 642 643 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 644 645 linux_vdso_obj = __elfN(linux_shared_page_init) 646 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 647 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 648 649 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 650 } 651 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 652 linux_vdso_install, NULL); 653 654 static void 655 linux_vdso_deinstall(const void *param) 656 { 657 658 __elfN(linux_shared_page_fini)(linux_vdso_obj, 659 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 660 } 661 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 662 linux_vdso_deinstall, NULL); 663 664 static void 665 linux_vdso_reloc(char *mapping, Elf_Addr offset) 666 { 667 Elf_Size rtype, symidx; 668 const Elf_Rela *rela; 669 const Elf_Shdr *shdr; 670 const Elf_Ehdr *ehdr; 671 Elf_Addr *where; 672 Elf_Addr addr, addend; 673 int i, relacnt; 674 675 MPASS(offset != 0); 676 677 relacnt = 0; 678 ehdr = (const Elf_Ehdr *)mapping; 679 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 680 for (i = 0; i < ehdr->e_shnum; i++) 681 { 682 switch (shdr[i].sh_type) { 683 case SHT_REL: 684 printf("Linux Aarch64 vDSO: unexpected Rel section\n"); 685 break; 686 case SHT_RELA: 687 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset); 688 relacnt = shdr[i].sh_size / sizeof(*rela); 689 } 690 } 691 692 for (i = 0; i < relacnt; i++, rela++) { 693 where = (Elf_Addr *)(mapping + rela->r_offset); 694 addend = rela->r_addend; 695 rtype = ELF_R_TYPE(rela->r_info); 696 symidx = ELF_R_SYM(rela->r_info); 697 698 switch (rtype) { 699 case R_AARCH64_NONE: /* none */ 700 break; 701 702 case R_AARCH64_RELATIVE: /* B + A */ 703 addr = (Elf_Addr)(mapping + addend); 704 if (*where != addr) 705 *where = addr; 706 break; 707 default: 708 printf("Linux Aarch64 vDSO: unexpected relocation type %ld, " 709 "symbol index %ld\n", rtype, symidx); 710 } 711 } 712 } 713 714 static char GNU_ABI_VENDOR[] = "GNU"; 715 static int GNU_ABI_LINUX = 0; 716 717 /* LINUXTODO: deduplicate */ 718 static bool 719 linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 720 { 721 const Elf32_Word *desc; 722 uintptr_t p; 723 724 p = (uintptr_t)(note + 1); 725 p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 726 727 desc = (const Elf32_Word *)p; 728 if (desc[0] != GNU_ABI_LINUX) 729 return (false); 730 731 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 732 return (true); 733 } 734 735 static Elf_Brandnote linux64_brandnote = { 736 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 737 .hdr.n_descsz = 16, 738 .hdr.n_type = 1, 739 .vendor = GNU_ABI_VENDOR, 740 .flags = BN_TRANSLATE_OSREL, 741 .trans_osrel = linux_trans_osrel 742 }; 743 744 static Elf64_Brandinfo linux_glibc2brand = { 745 .brand = ELFOSABI_LINUX, 746 .machine = EM_AARCH64, 747 .compat_3_brand = "Linux", 748 .emul_path = linux_emul_path, 749 .interp_path = "/lib64/ld-linux-x86-64.so.2", 750 .sysvec = &elf_linux_sysvec, 751 .interp_newpath = NULL, 752 .brand_note = &linux64_brandnote, 753 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 754 }; 755 756 Elf64_Brandinfo *linux_brandlist[] = { 757 &linux_glibc2brand, 758 NULL 759 }; 760 761 static int 762 linux64_elf_modevent(module_t mod, int type, void *data) 763 { 764 Elf64_Brandinfo **brandinfo; 765 struct linux_ioctl_handler**lihp; 766 int error; 767 768 error = 0; 769 switch(type) { 770 case MOD_LOAD: 771 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 772 ++brandinfo) 773 if (elf64_insert_brand_entry(*brandinfo) < 0) 774 error = EINVAL; 775 if (error == 0) { 776 SET_FOREACH(lihp, linux_ioctl_handler_set) 777 linux_ioctl_register_handler(*lihp); 778 stclohz = (stathz ? stathz : hz); 779 if (bootverbose) 780 printf("Linux arm64 ELF exec handler installed\n"); 781 } 782 break; 783 case MOD_UNLOAD: 784 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 785 ++brandinfo) 786 if (elf64_brand_inuse(*brandinfo)) 787 error = EBUSY; 788 if (error == 0) { 789 for (brandinfo = &linux_brandlist[0]; 790 *brandinfo != NULL; ++brandinfo) 791 if (elf64_remove_brand_entry(*brandinfo) < 0) 792 error = EINVAL; 793 } 794 if (error == 0) { 795 SET_FOREACH(lihp, linux_ioctl_handler_set) 796 linux_ioctl_unregister_handler(*lihp); 797 if (bootverbose) 798 printf("Linux arm64 ELF exec handler removed\n"); 799 } else 800 printf("Could not deinstall Linux arm64 ELF interpreter entry\n"); 801 break; 802 default: 803 return (EOPNOTSUPP); 804 } 805 return (error); 806 } 807 808 static moduledata_t linux64_elf_mod = { 809 "linux64elf", 810 linux64_elf_modevent, 811 0 812 }; 813 814 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 815 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 816 FEATURE(linux64, "AArch64 Linux 64bit support"); 817