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