13911ee2cSEd Maste /*- 23911ee2cSEd Maste * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 33911ee2cSEd Maste * 43911ee2cSEd Maste * Copyright (c) 1994-1996 Søren Schmidt 53911ee2cSEd Maste * Copyright (c) 2018 Turing Robotic Industries Inc. 63911ee2cSEd Maste * 73911ee2cSEd Maste * Redistribution and use in source and binary forms, with or without 83911ee2cSEd Maste * modification, are permitted provided that the following conditions 93911ee2cSEd Maste * are met: 103911ee2cSEd Maste * 1. Redistributions of source code must retain the above copyright 113911ee2cSEd Maste * notice, this list of conditions and the following disclaimer. 123911ee2cSEd Maste * 2. Redistributions in binary form must reproduce the above copyright 133911ee2cSEd Maste * notice, this list of conditions and the following disclaimer in the 143911ee2cSEd Maste * documentation and/or other materials provided with the distribution. 153911ee2cSEd Maste * 163911ee2cSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 173911ee2cSEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 183911ee2cSEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 193911ee2cSEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 203911ee2cSEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 213911ee2cSEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 223911ee2cSEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 233911ee2cSEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 243911ee2cSEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 253911ee2cSEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 263911ee2cSEd Maste * SUCH DAMAGE. 273911ee2cSEd Maste */ 283911ee2cSEd Maste 293911ee2cSEd Maste #include <sys/cdefs.h> 303911ee2cSEd Maste __FBSDID("$FreeBSD$"); 313911ee2cSEd Maste 323911ee2cSEd Maste #include <sys/param.h> 333911ee2cSEd Maste #include <sys/systm.h> 343911ee2cSEd Maste #include <sys/cdefs.h> 353911ee2cSEd Maste #include <sys/elf.h> 363911ee2cSEd Maste #include <sys/exec.h> 373911ee2cSEd Maste #include <sys/imgact.h> 383911ee2cSEd Maste #include <sys/imgact_elf.h> 393911ee2cSEd Maste #include <sys/kernel.h> 403911ee2cSEd Maste #include <sys/lock.h> 413911ee2cSEd Maste #include <sys/module.h> 423911ee2cSEd Maste #include <sys/mutex.h> 433911ee2cSEd Maste #include <sys/proc.h> 443911ee2cSEd Maste #include <sys/signalvar.h> 453911ee2cSEd Maste #include <sys/sysctl.h> 463911ee2cSEd Maste #include <sys/sysent.h> 473911ee2cSEd Maste 483911ee2cSEd Maste #include <vm/vm_param.h> 493911ee2cSEd Maste 503911ee2cSEd Maste #include <arm64/linux/linux.h> 513911ee2cSEd Maste #include <arm64/linux/linux_proto.h> 523911ee2cSEd Maste #include <compat/linux/linux_dtrace.h> 533911ee2cSEd Maste #include <compat/linux/linux_emul.h> 543911ee2cSEd Maste #include <compat/linux/linux_ioctl.h> 553911ee2cSEd Maste #include <compat/linux/linux_mib.h> 563911ee2cSEd Maste #include <compat/linux/linux_misc.h> 573911ee2cSEd Maste #include <compat/linux/linux_vdso.h> 583911ee2cSEd Maste 593911ee2cSEd Maste MODULE_VERSION(linux64elf, 1); 603911ee2cSEd Maste 613911ee2cSEd Maste const char *linux_kplatform; 623911ee2cSEd Maste static int linux_szsigcode; 633911ee2cSEd Maste static vm_object_t linux_shared_page_obj; 643911ee2cSEd Maste static char *linux_shared_page_mapping; 653911ee2cSEd Maste extern char _binary_linux_locore_o_start; 663911ee2cSEd Maste extern char _binary_linux_locore_o_end; 673911ee2cSEd Maste 683911ee2cSEd Maste extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 693911ee2cSEd Maste 703911ee2cSEd Maste SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 713911ee2cSEd Maste 7203b0d68cSJohn Baldwin static int linux_copyout_strings(struct image_params *imgp, 73*31174518SJohn Baldwin uintptr_t *stack_base); 74*31174518SJohn Baldwin static int linux_elf_fixup(uintptr_t *stack_base, 753911ee2cSEd Maste struct image_params *iparams); 763911ee2cSEd Maste static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 773911ee2cSEd Maste static void linux_vdso_install(const void *param); 783911ee2cSEd Maste static void linux_vdso_deinstall(const void *param); 793911ee2cSEd Maste static void linux_set_syscall_retval(struct thread *td, int error); 803911ee2cSEd Maste static int linux_fetch_syscall_args(struct thread *td); 813911ee2cSEd Maste static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 82*31174518SJohn Baldwin uintptr_t stack); 833911ee2cSEd Maste static int linux_vsyscall(struct thread *td); 843911ee2cSEd Maste 853911ee2cSEd Maste /* DTrace init */ 863911ee2cSEd Maste LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 873911ee2cSEd Maste 883911ee2cSEd Maste /* DTrace probes */ 893911ee2cSEd Maste LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int"); 903911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo); 915caa67faSJohn Baldwin LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo); 923911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo); 933911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo); 943911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo); 953911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vsyscall, todo); 963911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo); 973911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo); 983911ee2cSEd Maste 993911ee2cSEd Maste /* LINUXTODO: do we have traps to translate? */ 1003911ee2cSEd Maste static int 1013911ee2cSEd Maste linux_translate_traps(int signal, int trap_code) 1023911ee2cSEd Maste { 1033911ee2cSEd Maste 1043911ee2cSEd Maste LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code); 1053911ee2cSEd Maste return (signal); 1063911ee2cSEd Maste } 1073911ee2cSEd Maste 1083911ee2cSEd Maste LINUX_VDSO_SYM_CHAR(linux_platform); 1093911ee2cSEd Maste 1103911ee2cSEd Maste static int 1113911ee2cSEd Maste linux_fetch_syscall_args(struct thread *td) 1123911ee2cSEd Maste { 1133911ee2cSEd Maste struct proc *p; 1143911ee2cSEd Maste struct syscall_args *sa; 1153911ee2cSEd Maste register_t *ap; 1163911ee2cSEd Maste 1173911ee2cSEd Maste p = td->td_proc; 1183911ee2cSEd Maste ap = td->td_frame->tf_x; 1193911ee2cSEd Maste sa = &td->td_sa; 1203911ee2cSEd Maste 1213911ee2cSEd Maste sa->code = td->td_frame->tf_x[8]; 1223911ee2cSEd Maste /* LINUXTODO: generic syscall? */ 1233911ee2cSEd Maste if (sa->code >= p->p_sysent->sv_size) 1243911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[0]; 1253911ee2cSEd Maste else 1263911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[sa->code]; 1273911ee2cSEd Maste 1283911ee2cSEd Maste sa->narg = sa->callp->sy_narg; 1293911ee2cSEd Maste if (sa->narg > 8) 1303911ee2cSEd Maste panic("ARM64TODO: Could we have more than 8 args?"); 1313911ee2cSEd Maste memcpy(sa->args, ap, 8 * sizeof(register_t)); 1323911ee2cSEd Maste 1333911ee2cSEd Maste td->td_retval[0] = 0; 1343911ee2cSEd Maste return (0); 1353911ee2cSEd Maste } 1363911ee2cSEd Maste 1373911ee2cSEd Maste static void 1383911ee2cSEd Maste linux_set_syscall_retval(struct thread *td, int error) 1393911ee2cSEd Maste { 1403911ee2cSEd Maste 1418e5d76e6SAndrew Turner td->td_retval[1] = td->td_frame->tf_x[1]; 1428e5d76e6SAndrew Turner cpu_set_syscall_retval(td, error); 1433911ee2cSEd Maste } 1443911ee2cSEd Maste 14503b0d68cSJohn Baldwin static int 146*31174518SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t *base) 1473911ee2cSEd Maste { 1483911ee2cSEd Maste Elf_Auxargs *args; 1493911ee2cSEd Maste Elf_Auxinfo *argarray, *pos; 1505caa67faSJohn Baldwin u_long auxlen; 1513911ee2cSEd Maste struct proc *p; 15203b0d68cSJohn Baldwin int error, issetugid; 1533911ee2cSEd Maste 1545caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo); 1553911ee2cSEd Maste p = imgp->proc; 1563911ee2cSEd Maste 1573911ee2cSEd Maste args = (Elf64_Auxargs *)imgp->auxargs; 1583911ee2cSEd Maste argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 1593911ee2cSEd Maste M_WAITOK | M_ZERO); 1603911ee2cSEd Maste 1613911ee2cSEd Maste issetugid = p->p_flag & P_SUGID ? 1 : 0; 1623911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, 1633911ee2cSEd Maste imgp->proc->p_sysent->sv_shared_page_base); 1643911ee2cSEd Maste #if 0 /* LINUXTODO: implement arm64 LINUX_AT_HWCAP */ 1653911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature); 1663911ee2cSEd Maste #endif 1673911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 1683911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 1693911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 1703911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 1713911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 1723911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_BASE, args->base); 1733911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 1743911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 1753911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 1763911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 1773911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 1783911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 1793911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 1803911ee2cSEd Maste #if 0 /* LINUXTODO: implement arm64 LINUX_AT_PLATFORM */ 1813911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 1823911ee2cSEd Maste #endif 1833911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, imgp->canary); 1843911ee2cSEd Maste if (imgp->execpathp != 0) 1853911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, imgp->execpathp); 1863911ee2cSEd Maste if (args->execfd != -1) 1873911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 1883911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_NULL, 0); 1893911ee2cSEd Maste free(imgp->auxargs, M_TEMP); 1903911ee2cSEd Maste imgp->auxargs = NULL; 1913911ee2cSEd Maste KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 1923911ee2cSEd Maste 1935caa67faSJohn Baldwin auxlen = sizeof(*argarray) * (pos - argarray); 1945caa67faSJohn Baldwin *base -= auxlen; 19503b0d68cSJohn Baldwin error = copyout(argarray, (void *)*base, auxlen); 1963911ee2cSEd Maste free(argarray, M_TEMP); 19703b0d68cSJohn Baldwin return (error); 1985caa67faSJohn Baldwin } 1995caa67faSJohn Baldwin 2005caa67faSJohn Baldwin static int 201*31174518SJohn Baldwin linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp) 2025caa67faSJohn Baldwin { 2035caa67faSJohn Baldwin 2045caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo); 2053911ee2cSEd Maste 2063911ee2cSEd Maste return (0); 2073911ee2cSEd Maste } 2083911ee2cSEd Maste 2093911ee2cSEd Maste /* 2103911ee2cSEd Maste * Copy strings out to the new process address space, constructing new arg 2113911ee2cSEd Maste * and env vector tables. Return a pointer to the base so that it can be used 2123911ee2cSEd Maste * as the initial stack pointer. 2133911ee2cSEd Maste * LINUXTODO: deduplicate against other linuxulator archs 2143911ee2cSEd Maste */ 21503b0d68cSJohn Baldwin static int 216*31174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 2173911ee2cSEd Maste { 2183911ee2cSEd Maste char **vectp; 219*31174518SJohn Baldwin char *stringp; 220*31174518SJohn Baldwin uintptr_t *destp, *ustringp; 2213911ee2cSEd Maste struct ps_strings *arginfo; 2223911ee2cSEd Maste char canary[LINUX_AT_RANDOM_LEN]; 2233911ee2cSEd Maste size_t execpath_len; 2243911ee2cSEd Maste struct proc *p; 22503b0d68cSJohn Baldwin int argc, envc, error; 2263911ee2cSEd Maste 2273911ee2cSEd Maste /* Calculate string base and vector table pointers. */ 2283911ee2cSEd Maste if (imgp->execpath != NULL && imgp->auxargs != NULL) 2293911ee2cSEd Maste execpath_len = strlen(imgp->execpath) + 1; 2303911ee2cSEd Maste else 2313911ee2cSEd Maste execpath_len = 0; 2323911ee2cSEd Maste 2333911ee2cSEd Maste p = imgp->proc; 2343911ee2cSEd Maste arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 235*31174518SJohn Baldwin destp = (uintptr_t)arginfo; 2363911ee2cSEd Maste 2373911ee2cSEd Maste if (execpath_len != 0) { 238*31174518SJohn Baldwin destp -= execpath_len; 239*31174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 240*31174518SJohn Baldwin imgp->execpathp = destp; 241*31174518SJohn Baldwin error = copyout(imgp->execpath, (void *)destp, execpath_len); 24203b0d68cSJohn Baldwin if (error != 0) 24303b0d68cSJohn Baldwin return (error); 2443911ee2cSEd Maste } 2453911ee2cSEd Maste 2463911ee2cSEd Maste /* Prepare the canary for SSP. */ 2473911ee2cSEd Maste arc4rand(canary, sizeof(canary), 0); 248*31174518SJohn Baldwin destp -= roundup(sizeof(canary), sizeof(void *)); 249*31174518SJohn Baldwin imgp->canary = destp; 250*31174518SJohn Baldwin error = copyout(canary, (void *)destp, sizeof(canary)); 25103b0d68cSJohn Baldwin if (error != 0) 25203b0d68cSJohn Baldwin return (error); 2533911ee2cSEd Maste 254*31174518SJohn Baldwin /* Allocate room for the argument and environment strings. */ 255*31174518SJohn Baldwin destp -= ARG_MAX - imgp->args->stringspace; 256*31174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 257*31174518SJohn Baldwin ustringp = destp; 258*31174518SJohn Baldwin 25903b0d68cSJohn Baldwin if (imgp->auxargs) { 260*31174518SJohn Baldwin error = imgp->sysent->sv_copyout_auxargs(imgp, &destp); 26103b0d68cSJohn Baldwin if (error != 0) 26203b0d68cSJohn Baldwin return (error); 26303b0d68cSJohn Baldwin } 2643911ee2cSEd Maste 265*31174518SJohn Baldwin vectp = (char **)destp; 266*31174518SJohn Baldwin 2673911ee2cSEd Maste /* 2683911ee2cSEd Maste * Allocate room for argc and the argv[] and env vectors including the 2693911ee2cSEd Maste * terminating NULL pointers. 2703911ee2cSEd Maste */ 2713911ee2cSEd Maste vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1; 2723911ee2cSEd Maste vectp = (char **)STACKALIGN(vectp); 2733911ee2cSEd Maste 2743911ee2cSEd Maste /* vectp also becomes our initial stack base. */ 275*31174518SJohn Baldwin *stack_base = (uintptr_t)vectp; 2763911ee2cSEd Maste 2773911ee2cSEd Maste stringp = imgp->args->begin_argv; 2783911ee2cSEd Maste argc = imgp->args->argc; 2793911ee2cSEd Maste envc = imgp->args->envc; 2803911ee2cSEd Maste 2813911ee2cSEd Maste /* Copy out strings - arguments and environment. */ 282*31174518SJohn Baldwin error = copyout(stringp, (void *)ustringp, 283*31174518SJohn Baldwin ARG_MAX - imgp->args->stringspace); 28403b0d68cSJohn Baldwin if (error != 0) 28503b0d68cSJohn Baldwin return (error); 2863911ee2cSEd Maste 2873911ee2cSEd Maste /* Fill in "ps_strings" struct for ps, w, etc. */ 28803b0d68cSJohn Baldwin if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 28903b0d68cSJohn Baldwin suword(&arginfo->ps_nargvstr, argc) != 0) 29003b0d68cSJohn Baldwin return (EFAULT); 2913911ee2cSEd Maste 29203b0d68cSJohn Baldwin if (suword(vectp++, argc) != 0) 29303b0d68cSJohn Baldwin return (EFAULT); 29403b0d68cSJohn Baldwin 2953911ee2cSEd Maste /* Fill in argument portion of vector table. */ 2963911ee2cSEd Maste for (; argc > 0; --argc) { 297*31174518SJohn Baldwin if (suword(vectp++, ustringp) != 0) 29803b0d68cSJohn Baldwin return (EFAULT); 2993911ee2cSEd Maste while (*stringp++ != 0) 300*31174518SJohn Baldwin ustringp++; 301*31174518SJohn Baldwin ustringp++; 3023911ee2cSEd Maste } 3033911ee2cSEd Maste 3043911ee2cSEd Maste /* A null vector table pointer separates the argp's from the envp's. */ 30503b0d68cSJohn Baldwin if (suword(vectp++, 0) != 0) 30603b0d68cSJohn Baldwin return (EFAULT); 3073911ee2cSEd Maste 30803b0d68cSJohn Baldwin if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 30903b0d68cSJohn Baldwin suword(&arginfo->ps_nenvstr, envc) != 0) 31003b0d68cSJohn Baldwin return (EFAULT); 3113911ee2cSEd Maste 3123911ee2cSEd Maste /* Fill in environment portion of vector table. */ 3133911ee2cSEd Maste for (; envc > 0; --envc) { 314*31174518SJohn Baldwin if (suword(vectp++, ustringp) != 0) 31503b0d68cSJohn Baldwin return (EFAULT); 3163911ee2cSEd Maste while (*stringp++ != 0) 317*31174518SJohn Baldwin ustringp++; 318*31174518SJohn Baldwin ustringp++; 3193911ee2cSEd Maste } 3203911ee2cSEd Maste 3213911ee2cSEd Maste /* The end of the vector table is a null pointer. */ 32203b0d68cSJohn Baldwin if (suword(vectp, 0) != 0) 32303b0d68cSJohn Baldwin return (EFAULT); 32403b0d68cSJohn Baldwin 32503b0d68cSJohn Baldwin return (0); 3263911ee2cSEd Maste } 3273911ee2cSEd Maste 3283911ee2cSEd Maste /* 3293911ee2cSEd Maste * Reset registers to default values on exec. 3303911ee2cSEd Maste */ 3313911ee2cSEd Maste static void 332*31174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp, 333*31174518SJohn Baldwin uintptr_t stack) 3343911ee2cSEd Maste { 3353911ee2cSEd Maste struct trapframe *regs = td->td_frame; 3363911ee2cSEd Maste 3373911ee2cSEd Maste /* LINUXTODO: validate */ 3383911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo); 3393911ee2cSEd Maste 3403911ee2cSEd Maste memset(regs, 0, sizeof(*regs)); 3413911ee2cSEd Maste /* glibc start.S registers function pointer in x0 with atexit. */ 3423911ee2cSEd Maste regs->tf_sp = stack; 3433911ee2cSEd Maste #if 0 /* LINUXTODO: See if this is used. */ 3443911ee2cSEd Maste regs->tf_lr = imgp->entry_addr; 3453911ee2cSEd Maste #else 3463911ee2cSEd Maste regs->tf_lr = 0xffffffffffffffff; 3473911ee2cSEd Maste #endif 3483911ee2cSEd Maste regs->tf_elr = imgp->entry_addr; 3493911ee2cSEd Maste } 3503911ee2cSEd Maste 3513911ee2cSEd Maste int 3523911ee2cSEd Maste linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 3533911ee2cSEd Maste { 3543911ee2cSEd Maste 3553911ee2cSEd Maste /* LINUXTODO: implement */ 3563911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo); 3573911ee2cSEd Maste return (EDOOFUS); 3583911ee2cSEd Maste } 3593911ee2cSEd Maste 3603911ee2cSEd Maste static void 3613911ee2cSEd Maste linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 3623911ee2cSEd Maste { 3633911ee2cSEd Maste 3643911ee2cSEd Maste /* LINUXTODO: implement */ 3653911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo); 3663911ee2cSEd Maste } 3673911ee2cSEd Maste 3683911ee2cSEd Maste static int 3693911ee2cSEd Maste linux_vsyscall(struct thread *td) 3703911ee2cSEd Maste { 3713911ee2cSEd Maste 3723911ee2cSEd Maste /* LINUXTODO: implement */ 3733911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_vsyscall, todo); 3743911ee2cSEd Maste return (EDOOFUS); 3753911ee2cSEd Maste } 3763911ee2cSEd Maste 3773911ee2cSEd Maste struct sysentvec elf_linux_sysvec = { 3783911ee2cSEd Maste .sv_size = LINUX_SYS_MAXSYSCALL, 3793911ee2cSEd Maste .sv_table = linux_sysent, 3803911ee2cSEd Maste .sv_errsize = ELAST + 1, 3813911ee2cSEd Maste .sv_errtbl = linux_errtbl, 3823911ee2cSEd Maste .sv_transtrap = linux_translate_traps, 3833911ee2cSEd Maste .sv_fixup = linux_elf_fixup, 3843911ee2cSEd Maste .sv_sendsig = linux_rt_sendsig, 3853911ee2cSEd Maste .sv_sigcode = &_binary_linux_locore_o_start, 3863911ee2cSEd Maste .sv_szsigcode = &linux_szsigcode, 3873911ee2cSEd Maste .sv_name = "Linux ELF64", 3883911ee2cSEd Maste .sv_coredump = elf64_coredump, 3893911ee2cSEd Maste .sv_imgact_try = linux_exec_imgact_try, 3903911ee2cSEd Maste .sv_minsigstksz = LINUX_MINSIGSTKSZ, 3913911ee2cSEd Maste .sv_minuser = VM_MIN_ADDRESS, 3923911ee2cSEd Maste .sv_maxuser = VM_MAXUSER_ADDRESS, 3933911ee2cSEd Maste .sv_usrstack = USRSTACK, 3943911ee2cSEd Maste .sv_psstrings = PS_STRINGS, /* XXX */ 395d4f55cc8SEd Maste .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 3965caa67faSJohn Baldwin .sv_copyout_auxargs = linux_copyout_auxargs, 3973911ee2cSEd Maste .sv_copyout_strings = linux_copyout_strings, 3983911ee2cSEd Maste .sv_setregs = linux_exec_setregs, 3993911ee2cSEd Maste .sv_fixlimit = NULL, 4003911ee2cSEd Maste .sv_maxssiz = NULL, 4013911ee2cSEd Maste .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP, 4023911ee2cSEd Maste .sv_set_syscall_retval = linux_set_syscall_retval, 4033911ee2cSEd Maste .sv_fetch_syscall_args = linux_fetch_syscall_args, 4043911ee2cSEd Maste .sv_syscallnames = NULL, 4053911ee2cSEd Maste .sv_shared_page_base = SHAREDPAGE, 4063911ee2cSEd Maste .sv_shared_page_len = PAGE_SIZE, 4073911ee2cSEd Maste .sv_schedtail = linux_schedtail, 4083911ee2cSEd Maste .sv_thread_detach = linux_thread_detach, 4093911ee2cSEd Maste .sv_trap = linux_vsyscall, 4103911ee2cSEd Maste }; 4113911ee2cSEd Maste 4123911ee2cSEd Maste static void 4133911ee2cSEd Maste linux_vdso_install(const void *param) 4143911ee2cSEd Maste { 4153911ee2cSEd Maste 4163911ee2cSEd Maste linux_szsigcode = (&_binary_linux_locore_o_end - 4173911ee2cSEd Maste &_binary_linux_locore_o_start); 4183911ee2cSEd Maste 4193911ee2cSEd Maste if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 4203911ee2cSEd Maste panic("invalid Linux VDSO size\n"); 4213911ee2cSEd Maste 4223911ee2cSEd Maste __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 4233911ee2cSEd Maste 4243911ee2cSEd Maste linux_shared_page_obj = __elfN(linux_shared_page_init) 4253911ee2cSEd Maste (&linux_shared_page_mapping); 4263911ee2cSEd Maste 4273911ee2cSEd Maste __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 4283911ee2cSEd Maste 4293911ee2cSEd Maste memcpy(linux_shared_page_mapping, elf_linux_sysvec.sv_sigcode, 4303911ee2cSEd Maste linux_szsigcode); 4313911ee2cSEd Maste elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 4323911ee2cSEd Maste 4333911ee2cSEd Maste printf("LINUXTODO: %s: fix linux_kplatform\n", __func__); 4343911ee2cSEd Maste #if 0 4353911ee2cSEd Maste linux_kplatform = linux_shared_page_mapping + 4363911ee2cSEd Maste (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base); 4373911ee2cSEd Maste #else 4383911ee2cSEd Maste linux_kplatform = "arm64"; 4393911ee2cSEd Maste #endif 4403911ee2cSEd Maste } 4413911ee2cSEd Maste SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY, 4423911ee2cSEd Maste linux_vdso_install, NULL); 4433911ee2cSEd Maste 4443911ee2cSEd Maste static void 4453911ee2cSEd Maste linux_vdso_deinstall(const void *param) 4463911ee2cSEd Maste { 4473911ee2cSEd Maste 4483911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_vdso_deinstall, todo); 4493911ee2cSEd Maste __elfN(linux_shared_page_fini)(linux_shared_page_obj); 4503911ee2cSEd Maste } 4513911ee2cSEd Maste SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 4523911ee2cSEd Maste linux_vdso_deinstall, NULL); 4533911ee2cSEd Maste 4543911ee2cSEd Maste static char GNU_ABI_VENDOR[] = "GNU"; 4553911ee2cSEd Maste static int GNU_ABI_LINUX = 0; 4563911ee2cSEd Maste 4573911ee2cSEd Maste /* LINUXTODO: deduplicate */ 4583911ee2cSEd Maste static bool 4593911ee2cSEd Maste linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 4603911ee2cSEd Maste { 4613911ee2cSEd Maste const Elf32_Word *desc; 4623911ee2cSEd Maste uintptr_t p; 4633911ee2cSEd Maste 4643911ee2cSEd Maste p = (uintptr_t)(note + 1); 4653911ee2cSEd Maste p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 4663911ee2cSEd Maste 4673911ee2cSEd Maste desc = (const Elf32_Word *)p; 4683911ee2cSEd Maste if (desc[0] != GNU_ABI_LINUX) 4693911ee2cSEd Maste return (false); 4703911ee2cSEd Maste 4713911ee2cSEd Maste *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 4723911ee2cSEd Maste return (true); 4733911ee2cSEd Maste } 4743911ee2cSEd Maste 4753911ee2cSEd Maste static Elf_Brandnote linux64_brandnote = { 4763911ee2cSEd Maste .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 4773911ee2cSEd Maste .hdr.n_descsz = 16, 4783911ee2cSEd Maste .hdr.n_type = 1, 4793911ee2cSEd Maste .vendor = GNU_ABI_VENDOR, 4803911ee2cSEd Maste .flags = BN_TRANSLATE_OSREL, 4813911ee2cSEd Maste .trans_osrel = linux_trans_osrel 4823911ee2cSEd Maste }; 4833911ee2cSEd Maste 4843911ee2cSEd Maste static Elf64_Brandinfo linux_glibc2brand = { 4853911ee2cSEd Maste .brand = ELFOSABI_LINUX, 4863911ee2cSEd Maste .machine = EM_AARCH64, 4873911ee2cSEd Maste .compat_3_brand = "Linux", 4883911ee2cSEd Maste .emul_path = "/compat/linux", 4893911ee2cSEd Maste .interp_path = "/lib64/ld-linux-x86-64.so.2", 4903911ee2cSEd Maste .sysvec = &elf_linux_sysvec, 4913911ee2cSEd Maste .interp_newpath = NULL, 4923911ee2cSEd Maste .brand_note = &linux64_brandnote, 4933911ee2cSEd Maste .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 4943911ee2cSEd Maste }; 4953911ee2cSEd Maste 4963911ee2cSEd Maste Elf64_Brandinfo *linux_brandlist[] = { 4973911ee2cSEd Maste &linux_glibc2brand, 4983911ee2cSEd Maste NULL 4993911ee2cSEd Maste }; 5003911ee2cSEd Maste 5013911ee2cSEd Maste static int 5023911ee2cSEd Maste linux64_elf_modevent(module_t mod, int type, void *data) 5033911ee2cSEd Maste { 5043911ee2cSEd Maste Elf64_Brandinfo **brandinfo; 5053911ee2cSEd Maste struct linux_ioctl_handler**lihp; 5063911ee2cSEd Maste int error; 5073911ee2cSEd Maste 5083911ee2cSEd Maste error = 0; 5093911ee2cSEd Maste switch(type) { 5103911ee2cSEd Maste case MOD_LOAD: 5113911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 5123911ee2cSEd Maste ++brandinfo) 5133911ee2cSEd Maste if (elf64_insert_brand_entry(*brandinfo) < 0) 5143911ee2cSEd Maste error = EINVAL; 5153911ee2cSEd Maste if (error == 0) { 5163911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set) 5173911ee2cSEd Maste linux_ioctl_register_handler(*lihp); 5183911ee2cSEd Maste stclohz = (stathz ? stathz : hz); 5193911ee2cSEd Maste if (bootverbose) 5203911ee2cSEd Maste printf("Linux arm64 ELF exec handler installed\n"); 5213911ee2cSEd Maste } 5223911ee2cSEd Maste break; 5233911ee2cSEd Maste case MOD_UNLOAD: 5243911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 5253911ee2cSEd Maste ++brandinfo) 5263911ee2cSEd Maste if (elf64_brand_inuse(*brandinfo)) 5273911ee2cSEd Maste error = EBUSY; 5283911ee2cSEd Maste if (error == 0) { 5293911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; 5303911ee2cSEd Maste *brandinfo != NULL; ++brandinfo) 5313911ee2cSEd Maste if (elf64_remove_brand_entry(*brandinfo) < 0) 5323911ee2cSEd Maste error = EINVAL; 5333911ee2cSEd Maste } 5343911ee2cSEd Maste if (error == 0) { 5353911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set) 5363911ee2cSEd Maste linux_ioctl_unregister_handler(*lihp); 5373911ee2cSEd Maste if (bootverbose) 5383911ee2cSEd Maste printf("Linux ELF exec handler removed\n"); 5393911ee2cSEd Maste } else 5403911ee2cSEd Maste printf("Could not deinstall ELF interpreter entry\n"); 5413911ee2cSEd Maste break; 5423911ee2cSEd Maste default: 5433911ee2cSEd Maste return (EOPNOTSUPP); 5443911ee2cSEd Maste } 5453911ee2cSEd Maste return (error); 5463911ee2cSEd Maste } 5473911ee2cSEd Maste 5483911ee2cSEd Maste static moduledata_t linux64_elf_mod = { 5493911ee2cSEd Maste "linux64elf", 5503911ee2cSEd Maste linux64_elf_modevent, 5513911ee2cSEd Maste 0 5523911ee2cSEd Maste }; 5533911ee2cSEd Maste 5543911ee2cSEd Maste DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 5553911ee2cSEd Maste MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 5563911ee2cSEd Maste FEATURE(linux64, "AArch64 Linux 64bit support"); 557