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