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/lock.h> 41 #include <sys/module.h> 42 #include <sys/mutex.h> 43 #include <sys/proc.h> 44 #include <sys/signalvar.h> 45 #include <sys/sysctl.h> 46 #include <sys/sysent.h> 47 48 #include <vm/vm_param.h> 49 50 #include <arm64/linux/linux.h> 51 #include <arm64/linux/linux_proto.h> 52 #include <compat/linux/linux_dtrace.h> 53 #include <compat/linux/linux_emul.h> 54 #include <compat/linux/linux_ioctl.h> 55 #include <compat/linux/linux_mib.h> 56 #include <compat/linux/linux_misc.h> 57 #include <compat/linux/linux_util.h> 58 #include <compat/linux/linux_vdso.h> 59 60 #include <machine/md_var.h> 61 62 #ifdef VFP 63 #include <machine/vfp.h> 64 #endif 65 66 MODULE_VERSION(linux64elf, 1); 67 68 static int linux_szsigcode; 69 static vm_object_t linux_shared_page_obj; 70 static char *linux_shared_page_mapping; 71 extern char _binary_linux_locore_o_start; 72 extern char _binary_linux_locore_o_end; 73 74 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 75 76 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 77 78 static int linux_copyout_strings(struct image_params *imgp, 79 uintptr_t *stack_base); 80 static int linux_elf_fixup(uintptr_t *stack_base, 81 struct image_params *iparams); 82 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 83 static void linux_vdso_install(const void *param); 84 static void linux_vdso_deinstall(const void *param); 85 static void linux_set_syscall_retval(struct thread *td, int error); 86 static int linux_fetch_syscall_args(struct thread *td); 87 static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 88 uintptr_t stack); 89 90 /* DTrace init */ 91 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 92 93 /* DTrace probes */ 94 LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int"); 95 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo); 96 LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo); 97 LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo); 98 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo); 99 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo); 100 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo); 101 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo); 102 103 /* LINUXTODO: do we have traps to translate? */ 104 static int 105 linux_translate_traps(int signal, int trap_code) 106 { 107 108 LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code); 109 return (signal); 110 } 111 112 LINUX_VDSO_SYM_CHAR(linux_platform); 113 114 static int 115 linux_fetch_syscall_args(struct thread *td) 116 { 117 struct proc *p; 118 struct syscall_args *sa; 119 register_t *ap; 120 121 p = td->td_proc; 122 ap = td->td_frame->tf_x; 123 sa = &td->td_sa; 124 125 sa->code = td->td_frame->tf_x[8]; 126 /* LINUXTODO: generic syscall? */ 127 if (sa->code >= p->p_sysent->sv_size) 128 sa->callp = &p->p_sysent->sv_table[0]; 129 else 130 sa->callp = &p->p_sysent->sv_table[sa->code]; 131 132 if (sa->callp->sy_narg > MAXARGS) 133 panic("ARM64TODO: Could we have more than %d args?", MAXARGS); 134 memcpy(sa->args, ap, MAXARGS * sizeof(register_t)); 135 136 td->td_retval[0] = 0; 137 return (0); 138 } 139 140 static void 141 linux_set_syscall_retval(struct thread *td, int error) 142 { 143 144 td->td_retval[1] = td->td_frame->tf_x[1]; 145 cpu_set_syscall_retval(td, error); 146 147 if (__predict_false(error != 0)) { 148 if (error != ERESTART && error != EJUSTRETURN) 149 td->td_frame->tf_x[0] = bsd_to_linux_errno(error); 150 } 151 } 152 153 static int 154 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 155 { 156 Elf_Auxargs *args; 157 Elf_Auxinfo *argarray, *pos; 158 struct proc *p; 159 int error, issetugid; 160 161 LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo); 162 p = imgp->proc; 163 164 args = (Elf64_Auxargs *)imgp->auxargs; 165 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 166 M_WAITOK | M_ZERO); 167 168 issetugid = p->p_flag & P_SUGID ? 1 : 0; 169 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, 170 imgp->proc->p_sysent->sv_shared_page_base); 171 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap); 172 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 173 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 174 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 175 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 176 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 177 AUXARGS_ENTRY(pos, AT_BASE, args->base); 178 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 179 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 180 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 181 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 182 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 183 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 184 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 185 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 186 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2); 187 if (imgp->execpathp != 0) 188 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 189 if (args->execfd != -1) 190 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 191 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 192 AUXARGS_ENTRY(pos, AT_NULL, 0); 193 194 free(imgp->auxargs, M_TEMP); 195 imgp->auxargs = NULL; 196 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 197 198 error = copyout(argarray, (void *)base, 199 sizeof(*argarray) * LINUX_AT_COUNT); 200 free(argarray, M_TEMP); 201 return (error); 202 } 203 204 static int 205 linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp) 206 { 207 208 LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo); 209 210 return (0); 211 } 212 213 /* 214 * Copy strings out to the new process address space, constructing new arg 215 * and env vector tables. Return a pointer to the base so that it can be used 216 * as the initial stack pointer. 217 * LINUXTODO: deduplicate against other linuxulator archs 218 */ 219 static int 220 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 221 { 222 char **vectp; 223 char *stringp; 224 uintptr_t destp, ustringp; 225 struct ps_strings *arginfo; 226 char canary[LINUX_AT_RANDOM_LEN]; 227 size_t execpath_len; 228 struct proc *p; 229 int argc, envc, error; 230 231 /* Calculate string base and vector table pointers. */ 232 if (imgp->execpath != NULL && imgp->auxargs != NULL) 233 execpath_len = strlen(imgp->execpath) + 1; 234 else 235 execpath_len = 0; 236 237 p = imgp->proc; 238 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 239 destp = (uintptr_t)arginfo; 240 241 if (execpath_len != 0) { 242 destp -= execpath_len; 243 destp = rounddown2(destp, sizeof(void *)); 244 imgp->execpathp = (void *)destp; 245 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 246 if (error != 0) 247 return (error); 248 } 249 250 /* Prepare the canary for SSP. */ 251 arc4rand(canary, sizeof(canary), 0); 252 destp -= roundup(sizeof(canary), sizeof(void *)); 253 imgp->canary = (void *)destp; 254 error = copyout(canary, imgp->canary, sizeof(canary)); 255 if (error != 0) 256 return (error); 257 258 /* Allocate room for the argument and environment strings. */ 259 destp -= ARG_MAX - imgp->args->stringspace; 260 destp = rounddown2(destp, sizeof(void *)); 261 ustringp = destp; 262 263 if (imgp->auxargs) { 264 /* 265 * Allocate room on the stack for the ELF auxargs 266 * array. It has up to LINUX_AT_COUNT entries. 267 */ 268 destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo); 269 destp = rounddown2(destp, sizeof(void *)); 270 } 271 272 vectp = (char **)destp; 273 274 /* 275 * Allocate room for argc and the argv[] and env vectors including the 276 * terminating NULL pointers. 277 */ 278 vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1; 279 vectp = (char **)STACKALIGN(vectp); 280 281 /* vectp also becomes our initial stack base. */ 282 *stack_base = (uintptr_t)vectp; 283 284 stringp = imgp->args->begin_argv; 285 argc = imgp->args->argc; 286 envc = imgp->args->envc; 287 288 /* Copy out strings - arguments and environment. */ 289 error = copyout(stringp, (void *)ustringp, 290 ARG_MAX - imgp->args->stringspace); 291 if (error != 0) 292 return (error); 293 294 /* Fill in "ps_strings" struct for ps, w, etc. */ 295 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 296 suword(&arginfo->ps_nargvstr, argc) != 0) 297 return (EFAULT); 298 299 if (suword(vectp++, argc) != 0) 300 return (EFAULT); 301 302 /* Fill in argument portion of vector table. */ 303 for (; argc > 0; --argc) { 304 if (suword(vectp++, ustringp) != 0) 305 return (EFAULT); 306 while (*stringp++ != 0) 307 ustringp++; 308 ustringp++; 309 } 310 311 /* A null vector table pointer separates the argp's from the envp's. */ 312 if (suword(vectp++, 0) != 0) 313 return (EFAULT); 314 315 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 316 suword(&arginfo->ps_nenvstr, envc) != 0) 317 return (EFAULT); 318 319 /* Fill in environment portion of vector table. */ 320 for (; envc > 0; --envc) { 321 if (suword(vectp++, ustringp) != 0) 322 return (EFAULT); 323 while (*stringp++ != 0) 324 ustringp++; 325 ustringp++; 326 } 327 328 /* The end of the vector table is a null pointer. */ 329 if (suword(vectp, 0) != 0) 330 return (EFAULT); 331 332 if (imgp->auxargs) { 333 vectp++; 334 error = imgp->sysent->sv_copyout_auxargs(imgp, 335 (uintptr_t)vectp); 336 if (error != 0) 337 return (error); 338 } 339 340 return (0); 341 } 342 343 /* 344 * Reset registers to default values on exec. 345 */ 346 static void 347 linux_exec_setregs(struct thread *td, struct image_params *imgp, 348 uintptr_t stack) 349 { 350 struct trapframe *regs = td->td_frame; 351 struct pcb *pcb = td->td_pcb; 352 353 /* LINUXTODO: validate */ 354 LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo); 355 356 memset(regs, 0, sizeof(*regs)); 357 /* glibc start.S registers function pointer in x0 with atexit. */ 358 regs->tf_sp = stack; 359 #if 0 /* LINUXTODO: See if this is used. */ 360 regs->tf_lr = imgp->entry_addr; 361 #else 362 regs->tf_lr = 0xffffffffffffffff; 363 #endif 364 regs->tf_elr = imgp->entry_addr; 365 366 pcb->pcb_tpidr_el0 = 0; 367 pcb->pcb_tpidrro_el0 = 0; 368 WRITE_SPECIALREG(tpidrro_el0, 0); 369 WRITE_SPECIALREG(tpidr_el0, 0); 370 371 #ifdef VFP 372 vfp_reset_state(td, pcb); 373 #endif 374 375 /* 376 * Clear debug register state. It is not applicable to the new process. 377 */ 378 bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs)); 379 } 380 381 int 382 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 383 { 384 385 /* LINUXTODO: implement */ 386 LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo); 387 return (EDOOFUS); 388 } 389 390 static void 391 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 392 { 393 394 /* LINUXTODO: implement */ 395 LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo); 396 } 397 398 struct sysentvec elf_linux_sysvec = { 399 .sv_size = LINUX_SYS_MAXSYSCALL, 400 .sv_table = linux_sysent, 401 .sv_transtrap = linux_translate_traps, 402 .sv_fixup = linux_elf_fixup, 403 .sv_sendsig = linux_rt_sendsig, 404 .sv_sigcode = &_binary_linux_locore_o_start, 405 .sv_szsigcode = &linux_szsigcode, 406 .sv_name = "Linux ELF64", 407 .sv_coredump = elf64_coredump, 408 .sv_elf_core_osabi = ELFOSABI_NONE, 409 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 410 .sv_elf_core_prepare_notes = linux64_prepare_notes, 411 .sv_imgact_try = linux_exec_imgact_try, 412 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 413 .sv_minuser = VM_MIN_ADDRESS, 414 .sv_maxuser = VM_MAXUSER_ADDRESS, 415 .sv_usrstack = USRSTACK, 416 .sv_psstrings = PS_STRINGS, /* XXX */ 417 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 418 .sv_copyout_auxargs = linux_copyout_auxargs, 419 .sv_copyout_strings = linux_copyout_strings, 420 .sv_setregs = linux_exec_setregs, 421 .sv_fixlimit = NULL, 422 .sv_maxssiz = NULL, 423 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 424 SV_SIG_WAITNDQ, 425 .sv_set_syscall_retval = linux_set_syscall_retval, 426 .sv_fetch_syscall_args = linux_fetch_syscall_args, 427 .sv_syscallnames = NULL, 428 .sv_shared_page_base = SHAREDPAGE, 429 .sv_shared_page_len = PAGE_SIZE, 430 .sv_schedtail = linux_schedtail, 431 .sv_thread_detach = linux_thread_detach, 432 .sv_trap = NULL, 433 .sv_hwcap = &elf_hwcap, 434 .sv_hwcap2 = &elf_hwcap2, 435 .sv_onexec = linux_on_exec, 436 .sv_onexit = linux_on_exit, 437 .sv_ontdexit = linux_thread_dtor, 438 .sv_setid_allowed = &linux_setid_allowed_query, 439 }; 440 441 static void 442 linux_vdso_install(const void *param) 443 { 444 445 linux_szsigcode = (&_binary_linux_locore_o_end - 446 &_binary_linux_locore_o_start); 447 448 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 449 panic("invalid Linux VDSO size\n"); 450 451 __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 452 453 linux_shared_page_obj = __elfN(linux_shared_page_init) 454 (&linux_shared_page_mapping); 455 456 __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 457 458 memcpy(linux_shared_page_mapping, elf_linux_sysvec.sv_sigcode, 459 linux_szsigcode); 460 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 461 } 462 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY, 463 linux_vdso_install, NULL); 464 465 static void 466 linux_vdso_deinstall(const void *param) 467 { 468 469 LIN_SDT_PROBE0(sysvec, linux_vdso_deinstall, todo); 470 __elfN(linux_shared_page_fini)(linux_shared_page_obj, 471 linux_shared_page_mapping); 472 } 473 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 474 linux_vdso_deinstall, NULL); 475 476 static char GNU_ABI_VENDOR[] = "GNU"; 477 static int GNU_ABI_LINUX = 0; 478 479 /* LINUXTODO: deduplicate */ 480 static bool 481 linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 482 { 483 const Elf32_Word *desc; 484 uintptr_t p; 485 486 p = (uintptr_t)(note + 1); 487 p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 488 489 desc = (const Elf32_Word *)p; 490 if (desc[0] != GNU_ABI_LINUX) 491 return (false); 492 493 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 494 return (true); 495 } 496 497 static Elf_Brandnote linux64_brandnote = { 498 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 499 .hdr.n_descsz = 16, 500 .hdr.n_type = 1, 501 .vendor = GNU_ABI_VENDOR, 502 .flags = BN_TRANSLATE_OSREL, 503 .trans_osrel = linux_trans_osrel 504 }; 505 506 static Elf64_Brandinfo linux_glibc2brand = { 507 .brand = ELFOSABI_LINUX, 508 .machine = EM_AARCH64, 509 .compat_3_brand = "Linux", 510 .emul_path = linux_emul_path, 511 .interp_path = "/lib64/ld-linux-x86-64.so.2", 512 .sysvec = &elf_linux_sysvec, 513 .interp_newpath = NULL, 514 .brand_note = &linux64_brandnote, 515 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 516 }; 517 518 Elf64_Brandinfo *linux_brandlist[] = { 519 &linux_glibc2brand, 520 NULL 521 }; 522 523 static int 524 linux64_elf_modevent(module_t mod, int type, void *data) 525 { 526 Elf64_Brandinfo **brandinfo; 527 struct linux_ioctl_handler**lihp; 528 int error; 529 530 error = 0; 531 switch(type) { 532 case MOD_LOAD: 533 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 534 ++brandinfo) 535 if (elf64_insert_brand_entry(*brandinfo) < 0) 536 error = EINVAL; 537 if (error == 0) { 538 SET_FOREACH(lihp, linux_ioctl_handler_set) 539 linux_ioctl_register_handler(*lihp); 540 stclohz = (stathz ? stathz : hz); 541 if (bootverbose) 542 printf("Linux arm64 ELF exec handler installed\n"); 543 } 544 break; 545 case MOD_UNLOAD: 546 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 547 ++brandinfo) 548 if (elf64_brand_inuse(*brandinfo)) 549 error = EBUSY; 550 if (error == 0) { 551 for (brandinfo = &linux_brandlist[0]; 552 *brandinfo != NULL; ++brandinfo) 553 if (elf64_remove_brand_entry(*brandinfo) < 0) 554 error = EINVAL; 555 } 556 if (error == 0) { 557 SET_FOREACH(lihp, linux_ioctl_handler_set) 558 linux_ioctl_unregister_handler(*lihp); 559 if (bootverbose) 560 printf("Linux ELF exec handler removed\n"); 561 } else 562 printf("Could not deinstall ELF interpreter entry\n"); 563 break; 564 default: 565 return (EOPNOTSUPP); 566 } 567 return (error); 568 } 569 570 static moduledata_t linux64_elf_mod = { 571 "linux64elf", 572 linux64_elf_modevent, 573 0 574 }; 575 576 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 577 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 578 FEATURE(linux64, "AArch64 Linux 64bit support"); 579