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