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