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