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