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.h> 49 #include <vm/pmap.h> 50 #include <vm/vm_param.h> 51 #include <vm/vm_map.h> 52 53 #include <arm64/linux/linux.h> 54 #include <arm64/linux/linux_proto.h> 55 #include <compat/linux/linux_dtrace.h> 56 #include <compat/linux/linux_emul.h> 57 #include <compat/linux/linux_ioctl.h> 58 #include <compat/linux/linux_mib.h> 59 #include <compat/linux/linux_misc.h> 60 #include <compat/linux/linux_util.h> 61 #include <compat/linux/linux_vdso.h> 62 63 #include <machine/md_var.h> 64 65 #ifdef VFP 66 #include <machine/vfp.h> 67 #endif 68 69 MODULE_VERSION(linux64elf, 1); 70 71 const char *linux_kplatform; 72 static int linux_szsigcode; 73 static vm_object_t linux_shared_page_obj; 74 static char *linux_shared_page_mapping; 75 extern char _binary_linux_locore_o_start; 76 extern char _binary_linux_locore_o_end; 77 78 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 79 80 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 81 82 static int linux_copyout_strings(struct image_params *imgp, 83 uintptr_t *stack_base); 84 static int linux_elf_fixup(uintptr_t *stack_base, 85 struct image_params *iparams); 86 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 87 static void linux_vdso_install(const void *param); 88 static void linux_vdso_deinstall(const void *param); 89 static void linux_set_syscall_retval(struct thread *td, int error); 90 static int linux_fetch_syscall_args(struct thread *td); 91 static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 92 uintptr_t stack); 93 94 /* DTrace init */ 95 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 96 97 /* DTrace probes */ 98 LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int"); 99 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo); 100 LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo); 101 LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo); 102 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo); 103 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo); 104 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo); 105 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo); 106 107 /* LINUXTODO: do we have traps to translate? */ 108 static int 109 linux_translate_traps(int signal, int trap_code) 110 { 111 112 LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code); 113 return (signal); 114 } 115 116 LINUX_VDSO_SYM_CHAR(linux_platform); 117 118 static int 119 linux_fetch_syscall_args(struct thread *td) 120 { 121 struct proc *p; 122 struct syscall_args *sa; 123 register_t *ap; 124 125 p = td->td_proc; 126 ap = td->td_frame->tf_x; 127 sa = &td->td_sa; 128 129 sa->code = td->td_frame->tf_x[8]; 130 /* LINUXTODO: generic syscall? */ 131 if (sa->code >= p->p_sysent->sv_size) 132 sa->callp = &p->p_sysent->sv_table[0]; 133 else 134 sa->callp = &p->p_sysent->sv_table[sa->code]; 135 136 if (sa->callp->sy_narg > MAXARGS) 137 panic("ARM64TODO: Could we have more than %d args?", MAXARGS); 138 memcpy(sa->args, ap, MAXARGS * sizeof(register_t)); 139 140 td->td_retval[0] = 0; 141 return (0); 142 } 143 144 static void 145 linux_set_syscall_retval(struct thread *td, int error) 146 { 147 148 td->td_retval[1] = td->td_frame->tf_x[1]; 149 cpu_set_syscall_retval(td, error); 150 151 if (__predict_false(error != 0)) { 152 if (error != ERESTART && error != EJUSTRETURN) 153 td->td_frame->tf_x[0] = bsd_to_linux_errno(error); 154 } 155 } 156 157 static int 158 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 159 { 160 Elf_Auxargs *args; 161 Elf_Auxinfo *argarray, *pos; 162 struct proc *p; 163 int error, issetugid; 164 165 LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo); 166 p = imgp->proc; 167 168 args = (Elf64_Auxargs *)imgp->auxargs; 169 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 170 M_WAITOK | M_ZERO); 171 172 issetugid = p->p_flag & P_SUGID ? 1 : 0; 173 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, 174 imgp->proc->p_sysent->sv_shared_page_base); 175 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap); 176 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 177 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 178 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 179 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 180 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 181 AUXARGS_ENTRY(pos, AT_BASE, args->base); 182 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 183 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 184 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 185 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 186 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 187 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 188 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 189 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 190 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2); 191 if (imgp->execpathp != 0) 192 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 193 if (args->execfd != -1) 194 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 195 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 196 AUXARGS_ENTRY(pos, AT_NULL, 0); 197 198 free(imgp->auxargs, M_TEMP); 199 imgp->auxargs = NULL; 200 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 201 202 error = copyout(argarray, (void *)base, 203 sizeof(*argarray) * LINUX_AT_COUNT); 204 free(argarray, M_TEMP); 205 return (error); 206 } 207 208 static int 209 linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp) 210 { 211 212 LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo); 213 214 return (0); 215 } 216 217 /* 218 * Copy strings out to the new process address space, constructing new arg 219 * and env vector tables. Return a pointer to the base so that it can be used 220 * as the initial stack pointer. 221 * LINUXTODO: deduplicate against other linuxulator archs 222 */ 223 static int 224 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 225 { 226 char **vectp; 227 char *stringp; 228 uintptr_t destp, ustringp; 229 struct ps_strings *arginfo; 230 char canary[LINUX_AT_RANDOM_LEN]; 231 size_t execpath_len; 232 struct proc *p; 233 int argc, envc, error; 234 235 p = imgp->proc; 236 arginfo = (struct ps_strings *)PROC_PS_STRINGS(p); 237 destp = (uintptr_t)arginfo; 238 239 if (imgp->execpath != NULL && imgp->auxargs != NULL) { 240 execpath_len = strlen(imgp->execpath) + 1; 241 destp -= execpath_len; 242 destp = rounddown2(destp, sizeof(void *)); 243 imgp->execpathp = (void *)destp; 244 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 245 if (error != 0) 246 return (error); 247 } 248 249 /* Prepare the canary for SSP. */ 250 arc4rand(canary, sizeof(canary), 0); 251 destp -= roundup(sizeof(canary), sizeof(void *)); 252 imgp->canary = (void *)destp; 253 error = copyout(canary, imgp->canary, sizeof(canary)); 254 if (error != 0) 255 return (error); 256 257 /* Allocate room for the argument and environment strings. */ 258 destp -= ARG_MAX - imgp->args->stringspace; 259 destp = rounddown2(destp, sizeof(void *)); 260 ustringp = destp; 261 262 if (imgp->auxargs) { 263 /* 264 * Allocate room on the stack for the ELF auxargs 265 * array. It has up to LINUX_AT_COUNT entries. 266 */ 267 destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo); 268 destp = rounddown2(destp, sizeof(void *)); 269 } 270 271 vectp = (char **)destp; 272 273 /* 274 * Allocate room for argc and the argv[] and env vectors including the 275 * terminating NULL pointers. 276 */ 277 vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1; 278 vectp = (char **)STACKALIGN(vectp); 279 280 /* vectp also becomes our initial stack base. */ 281 *stack_base = (uintptr_t)vectp; 282 283 stringp = imgp->args->begin_argv; 284 argc = imgp->args->argc; 285 envc = imgp->args->envc; 286 287 /* Copy out strings - arguments and environment. */ 288 error = copyout(stringp, (void *)ustringp, 289 ARG_MAX - imgp->args->stringspace); 290 if (error != 0) 291 return (error); 292 293 /* Fill in "ps_strings" struct for ps, w, etc. */ 294 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 295 suword(&arginfo->ps_nargvstr, argc) != 0) 296 return (EFAULT); 297 298 if (suword(vectp++, argc) != 0) 299 return (EFAULT); 300 301 /* Fill in argument portion of vector table. */ 302 for (; argc > 0; --argc) { 303 if (suword(vectp++, ustringp) != 0) 304 return (EFAULT); 305 while (*stringp++ != 0) 306 ustringp++; 307 ustringp++; 308 } 309 310 /* A null vector table pointer separates the argp's from the envp's. */ 311 if (suword(vectp++, 0) != 0) 312 return (EFAULT); 313 314 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 315 suword(&arginfo->ps_nenvstr, envc) != 0) 316 return (EFAULT); 317 318 /* Fill in environment portion of vector table. */ 319 for (; envc > 0; --envc) { 320 if (suword(vectp++, ustringp) != 0) 321 return (EFAULT); 322 while (*stringp++ != 0) 323 ustringp++; 324 ustringp++; 325 } 326 327 /* The end of the vector table is a null pointer. */ 328 if (suword(vectp, 0) != 0) 329 return (EFAULT); 330 331 if (imgp->auxargs) { 332 vectp++; 333 error = imgp->sysent->sv_copyout_auxargs(imgp, 334 (uintptr_t)vectp); 335 if (error != 0) 336 return (error); 337 } 338 339 return (0); 340 } 341 342 /* 343 * Reset registers to default values on exec. 344 */ 345 static void 346 linux_exec_setregs(struct thread *td, struct image_params *imgp, 347 uintptr_t stack) 348 { 349 struct trapframe *regs = td->td_frame; 350 struct pcb *pcb = td->td_pcb; 351 352 /* LINUXTODO: validate */ 353 LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo); 354 355 memset(regs, 0, sizeof(*regs)); 356 /* glibc start.S registers function pointer in x0 with atexit. */ 357 regs->tf_sp = stack; 358 #if 0 /* LINUXTODO: See if this is used. */ 359 regs->tf_lr = imgp->entry_addr; 360 #else 361 regs->tf_lr = 0xffffffffffffffff; 362 #endif 363 regs->tf_elr = imgp->entry_addr; 364 365 pcb->pcb_tpidr_el0 = 0; 366 pcb->pcb_tpidrro_el0 = 0; 367 WRITE_SPECIALREG(tpidrro_el0, 0); 368 WRITE_SPECIALREG(tpidr_el0, 0); 369 370 #ifdef VFP 371 vfp_reset_state(td, pcb); 372 #endif 373 374 /* 375 * Clear debug register state. It is not applicable to the new process. 376 */ 377 bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs)); 378 } 379 380 int 381 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 382 { 383 384 /* LINUXTODO: implement */ 385 LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo); 386 return (EDOOFUS); 387 } 388 389 static void 390 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 391 { 392 393 /* LINUXTODO: implement */ 394 LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo); 395 } 396 397 struct sysentvec elf_linux_sysvec = { 398 .sv_size = LINUX_SYS_MAXSYSCALL, 399 .sv_table = linux_sysent, 400 .sv_transtrap = linux_translate_traps, 401 .sv_fixup = linux_elf_fixup, 402 .sv_sendsig = linux_rt_sendsig, 403 .sv_sigcode = &_binary_linux_locore_o_start, 404 .sv_szsigcode = &linux_szsigcode, 405 .sv_name = "Linux ELF64", 406 .sv_coredump = elf64_coredump, 407 .sv_imgact_try = linux_exec_imgact_try, 408 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 409 .sv_minuser = VM_MIN_ADDRESS, 410 .sv_maxuser = VM_MAXUSER_ADDRESS, 411 .sv_usrstack = USRSTACK, 412 .sv_psstrings = PS_STRINGS, /* XXX */ 413 .sv_psstringssz = sizeof(struct ps_strings), 414 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 415 .sv_copyout_auxargs = linux_copyout_auxargs, 416 .sv_copyout_strings = linux_copyout_strings, 417 .sv_setregs = linux_exec_setregs, 418 .sv_fixlimit = NULL, 419 .sv_maxssiz = NULL, 420 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 421 SV_SIG_WAITNDQ, 422 .sv_set_syscall_retval = linux_set_syscall_retval, 423 .sv_fetch_syscall_args = linux_fetch_syscall_args, 424 .sv_syscallnames = NULL, 425 .sv_shared_page_base = SHAREDPAGE, 426 .sv_shared_page_len = PAGE_SIZE, 427 .sv_schedtail = linux_schedtail, 428 .sv_thread_detach = linux_thread_detach, 429 .sv_trap = NULL, 430 .sv_hwcap = &elf_hwcap, 431 .sv_hwcap2 = &elf_hwcap2, 432 .sv_onexec = linux_on_exec, 433 .sv_onexit = linux_on_exit, 434 .sv_ontdexit = linux_thread_dtor, 435 .sv_setid_allowed = &linux_setid_allowed_query, 436 }; 437 438 static void 439 linux_vdso_install(const void *param) 440 { 441 442 linux_szsigcode = (&_binary_linux_locore_o_end - 443 &_binary_linux_locore_o_start); 444 445 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 446 panic("invalid Linux VDSO size\n"); 447 448 __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 449 450 linux_shared_page_obj = __elfN(linux_shared_page_init) 451 (&linux_shared_page_mapping); 452 453 __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 454 455 memcpy(linux_shared_page_mapping, elf_linux_sysvec.sv_sigcode, 456 linux_szsigcode); 457 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 458 459 linux_kplatform = linux_shared_page_mapping + 460 (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base); 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