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> 57b5f20658SEdward Tomasz Napierala #include <compat/linux/linux_util.h> 583911ee2cSEd Maste #include <compat/linux/linux_vdso.h> 593911ee2cSEd Maste 60b501b2aeSEdward Tomasz Napierala #include <machine/md_var.h> 61b501b2aeSEdward Tomasz Napierala 62953a7d7cSAlex Richardson #ifdef VFP 63953a7d7cSAlex Richardson #include <machine/vfp.h> 64953a7d7cSAlex Richardson #endif 65953a7d7cSAlex Richardson 663911ee2cSEd Maste MODULE_VERSION(linux64elf, 1); 673911ee2cSEd Maste 683911ee2cSEd Maste static int linux_szsigcode; 693911ee2cSEd Maste static vm_object_t linux_shared_page_obj; 703911ee2cSEd Maste static char *linux_shared_page_mapping; 713911ee2cSEd Maste extern char _binary_linux_locore_o_start; 723911ee2cSEd Maste extern char _binary_linux_locore_o_end; 733911ee2cSEd Maste 743911ee2cSEd Maste extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 753911ee2cSEd Maste 763911ee2cSEd Maste SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 773911ee2cSEd Maste 7803b0d68cSJohn Baldwin static int linux_copyout_strings(struct image_params *imgp, 7931174518SJohn Baldwin uintptr_t *stack_base); 8031174518SJohn Baldwin static int linux_elf_fixup(uintptr_t *stack_base, 813911ee2cSEd Maste struct image_params *iparams); 823911ee2cSEd Maste static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 833911ee2cSEd Maste static void linux_vdso_install(const void *param); 843911ee2cSEd Maste static void linux_vdso_deinstall(const void *param); 853911ee2cSEd Maste static void linux_set_syscall_retval(struct thread *td, int error); 863911ee2cSEd Maste static int linux_fetch_syscall_args(struct thread *td); 873911ee2cSEd Maste static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 8831174518SJohn Baldwin uintptr_t stack); 893911ee2cSEd Maste 903911ee2cSEd Maste /* DTrace init */ 913911ee2cSEd Maste LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 923911ee2cSEd Maste 933911ee2cSEd Maste /* DTrace probes */ 943911ee2cSEd Maste LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int"); 953911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo); 965caa67faSJohn Baldwin LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo); 973911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo); 983911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo); 993911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo); 1003911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo); 1013911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo); 1023911ee2cSEd Maste 1033911ee2cSEd Maste /* LINUXTODO: do we have traps to translate? */ 1043911ee2cSEd Maste static int 1053911ee2cSEd Maste linux_translate_traps(int signal, int trap_code) 1063911ee2cSEd Maste { 1073911ee2cSEd Maste 1083911ee2cSEd Maste LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code); 1093911ee2cSEd Maste return (signal); 1103911ee2cSEd Maste } 1113911ee2cSEd Maste 1123911ee2cSEd Maste LINUX_VDSO_SYM_CHAR(linux_platform); 1133911ee2cSEd Maste 1143911ee2cSEd Maste static int 1153911ee2cSEd Maste linux_fetch_syscall_args(struct thread *td) 1163911ee2cSEd Maste { 1173911ee2cSEd Maste struct proc *p; 1183911ee2cSEd Maste struct syscall_args *sa; 1193911ee2cSEd Maste register_t *ap; 1203911ee2cSEd Maste 1213911ee2cSEd Maste p = td->td_proc; 1223911ee2cSEd Maste ap = td->td_frame->tf_x; 1233911ee2cSEd Maste sa = &td->td_sa; 1243911ee2cSEd Maste 1253911ee2cSEd Maste sa->code = td->td_frame->tf_x[8]; 1263911ee2cSEd Maste /* LINUXTODO: generic syscall? */ 1273911ee2cSEd Maste if (sa->code >= p->p_sysent->sv_size) 1283911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[0]; 1293911ee2cSEd Maste else 1303911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[sa->code]; 1313911ee2cSEd Maste 1321e2521ffSEdward Tomasz Napierala if (sa->callp->sy_narg > MAXARGS) 1331e2521ffSEdward Tomasz Napierala panic("ARM64TODO: Could we have more than %d args?", MAXARGS); 1341e2521ffSEdward Tomasz Napierala memcpy(sa->args, ap, MAXARGS * sizeof(register_t)); 1353911ee2cSEd Maste 1363911ee2cSEd Maste td->td_retval[0] = 0; 1373911ee2cSEd Maste return (0); 1383911ee2cSEd Maste } 1393911ee2cSEd Maste 1403911ee2cSEd Maste static void 1413911ee2cSEd Maste linux_set_syscall_retval(struct thread *td, int error) 1423911ee2cSEd Maste { 1433911ee2cSEd Maste 1448e5d76e6SAndrew Turner td->td_retval[1] = td->td_frame->tf_x[1]; 1458e5d76e6SAndrew Turner cpu_set_syscall_retval(td, error); 146c26391f4SEdward Tomasz Napierala 147c26391f4SEdward Tomasz Napierala if (__predict_false(error != 0)) { 148866b1f51SEdward Tomasz Napierala if (error != ERESTART && error != EJUSTRETURN) 149866b1f51SEdward Tomasz Napierala td->td_frame->tf_x[0] = bsd_to_linux_errno(error); 150c26391f4SEdward Tomasz Napierala } 1513911ee2cSEd Maste } 1523911ee2cSEd Maste 15303b0d68cSJohn Baldwin static int 154d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 1553911ee2cSEd Maste { 1563911ee2cSEd Maste Elf_Auxargs *args; 1573911ee2cSEd Maste Elf_Auxinfo *argarray, *pos; 1583911ee2cSEd Maste struct proc *p; 15903b0d68cSJohn Baldwin int error, issetugid; 1603911ee2cSEd Maste 1615caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo); 1623911ee2cSEd Maste p = imgp->proc; 1633911ee2cSEd Maste 1643911ee2cSEd Maste args = (Elf64_Auxargs *)imgp->auxargs; 1653911ee2cSEd Maste argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 1663911ee2cSEd Maste M_WAITOK | M_ZERO); 1673911ee2cSEd Maste 1683911ee2cSEd Maste issetugid = p->p_flag & P_SUGID ? 1 : 0; 1693911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, 1703911ee2cSEd Maste imgp->proc->p_sysent->sv_shared_page_base); 171b501b2aeSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap); 172aa462cabSEdward Tomasz Napierala AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 1733911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 1743911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 1753911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 1763911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 1773911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_BASE, args->base); 1783911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 1793911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 1803911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 1813911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 1823911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 1833911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 1843911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 185b24e6ac8SBrooks Davis AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 186b501b2aeSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2); 1873911ee2cSEd Maste if (imgp->execpathp != 0) 188b24e6ac8SBrooks Davis AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 1893911ee2cSEd Maste if (args->execfd != -1) 1903911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 191aa462cabSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 1923911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_NULL, 0); 193aa462cabSEdward Tomasz Napierala 1943911ee2cSEd Maste free(imgp->auxargs, M_TEMP); 1953911ee2cSEd Maste imgp->auxargs = NULL; 1963911ee2cSEd Maste KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 1973911ee2cSEd Maste 198d8010b11SJohn Baldwin error = copyout(argarray, (void *)base, 199d8010b11SJohn Baldwin sizeof(*argarray) * LINUX_AT_COUNT); 2003911ee2cSEd Maste free(argarray, M_TEMP); 20103b0d68cSJohn Baldwin return (error); 2025caa67faSJohn Baldwin } 2035caa67faSJohn Baldwin 2045caa67faSJohn Baldwin static int 20531174518SJohn Baldwin linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp) 2065caa67faSJohn Baldwin { 2075caa67faSJohn Baldwin 2085caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo); 2093911ee2cSEd Maste 2103911ee2cSEd Maste return (0); 2113911ee2cSEd Maste } 2123911ee2cSEd Maste 2133911ee2cSEd Maste /* 2143911ee2cSEd Maste * Copy strings out to the new process address space, constructing new arg 2153911ee2cSEd Maste * and env vector tables. Return a pointer to the base so that it can be used 2163911ee2cSEd Maste * as the initial stack pointer. 2173911ee2cSEd Maste * LINUXTODO: deduplicate against other linuxulator archs 2183911ee2cSEd Maste */ 21903b0d68cSJohn Baldwin static int 22031174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 2213911ee2cSEd Maste { 2223911ee2cSEd Maste char **vectp; 22331174518SJohn Baldwin char *stringp; 2240386b6c8SLi-Wen Hsu uintptr_t destp, ustringp; 2253911ee2cSEd Maste struct ps_strings *arginfo; 2263911ee2cSEd Maste char canary[LINUX_AT_RANDOM_LEN]; 2273911ee2cSEd Maste size_t execpath_len; 2283911ee2cSEd Maste struct proc *p; 22903b0d68cSJohn Baldwin int argc, envc, error; 2303911ee2cSEd Maste 2313911ee2cSEd Maste /* Calculate string base and vector table pointers. */ 2323911ee2cSEd Maste if (imgp->execpath != NULL && imgp->auxargs != NULL) 2333911ee2cSEd Maste execpath_len = strlen(imgp->execpath) + 1; 2343911ee2cSEd Maste else 2353911ee2cSEd Maste execpath_len = 0; 2363911ee2cSEd Maste 2373911ee2cSEd Maste p = imgp->proc; 2383911ee2cSEd Maste arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 23931174518SJohn Baldwin destp = (uintptr_t)arginfo; 2403911ee2cSEd Maste 2413911ee2cSEd Maste if (execpath_len != 0) { 24231174518SJohn Baldwin destp -= execpath_len; 24331174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 244b24e6ac8SBrooks Davis imgp->execpathp = (void *)destp; 245b24e6ac8SBrooks Davis error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 24603b0d68cSJohn Baldwin if (error != 0) 24703b0d68cSJohn Baldwin return (error); 2483911ee2cSEd Maste } 2493911ee2cSEd Maste 2503911ee2cSEd Maste /* Prepare the canary for SSP. */ 2513911ee2cSEd Maste arc4rand(canary, sizeof(canary), 0); 25231174518SJohn Baldwin destp -= roundup(sizeof(canary), sizeof(void *)); 253b24e6ac8SBrooks Davis imgp->canary = (void *)destp; 254b24e6ac8SBrooks Davis error = copyout(canary, imgp->canary, sizeof(canary)); 25503b0d68cSJohn Baldwin if (error != 0) 25603b0d68cSJohn Baldwin return (error); 2573911ee2cSEd Maste 25831174518SJohn Baldwin /* Allocate room for the argument and environment strings. */ 25931174518SJohn Baldwin destp -= ARG_MAX - imgp->args->stringspace; 26031174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 26131174518SJohn Baldwin ustringp = destp; 26231174518SJohn Baldwin 26303b0d68cSJohn Baldwin if (imgp->auxargs) { 264d8010b11SJohn Baldwin /* 265d8010b11SJohn Baldwin * Allocate room on the stack for the ELF auxargs 266d8010b11SJohn Baldwin * array. It has up to LINUX_AT_COUNT entries. 267d8010b11SJohn Baldwin */ 268d8010b11SJohn Baldwin destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo); 269d8010b11SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 27003b0d68cSJohn Baldwin } 2713911ee2cSEd Maste 27231174518SJohn Baldwin vectp = (char **)destp; 27331174518SJohn Baldwin 2743911ee2cSEd Maste /* 2753911ee2cSEd Maste * Allocate room for argc and the argv[] and env vectors including the 2763911ee2cSEd Maste * terminating NULL pointers. 2773911ee2cSEd Maste */ 2783911ee2cSEd Maste vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1; 2793911ee2cSEd Maste vectp = (char **)STACKALIGN(vectp); 2803911ee2cSEd Maste 2813911ee2cSEd Maste /* vectp also becomes our initial stack base. */ 28231174518SJohn Baldwin *stack_base = (uintptr_t)vectp; 2833911ee2cSEd Maste 2843911ee2cSEd Maste stringp = imgp->args->begin_argv; 2853911ee2cSEd Maste argc = imgp->args->argc; 2863911ee2cSEd Maste envc = imgp->args->envc; 2873911ee2cSEd Maste 2883911ee2cSEd Maste /* Copy out strings - arguments and environment. */ 28931174518SJohn Baldwin error = copyout(stringp, (void *)ustringp, 29031174518SJohn Baldwin ARG_MAX - imgp->args->stringspace); 29103b0d68cSJohn Baldwin if (error != 0) 29203b0d68cSJohn Baldwin return (error); 2933911ee2cSEd Maste 2943911ee2cSEd Maste /* Fill in "ps_strings" struct for ps, w, etc. */ 29503b0d68cSJohn Baldwin if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 29603b0d68cSJohn Baldwin suword(&arginfo->ps_nargvstr, argc) != 0) 29703b0d68cSJohn Baldwin return (EFAULT); 2983911ee2cSEd Maste 29903b0d68cSJohn Baldwin if (suword(vectp++, argc) != 0) 30003b0d68cSJohn Baldwin return (EFAULT); 30103b0d68cSJohn Baldwin 3023911ee2cSEd Maste /* Fill in argument portion of vector table. */ 3033911ee2cSEd Maste for (; argc > 0; --argc) { 30431174518SJohn Baldwin if (suword(vectp++, ustringp) != 0) 30503b0d68cSJohn Baldwin return (EFAULT); 3063911ee2cSEd Maste while (*stringp++ != 0) 30731174518SJohn Baldwin ustringp++; 30831174518SJohn Baldwin ustringp++; 3093911ee2cSEd Maste } 3103911ee2cSEd Maste 3113911ee2cSEd Maste /* A null vector table pointer separates the argp's from the envp's. */ 31203b0d68cSJohn Baldwin if (suword(vectp++, 0) != 0) 31303b0d68cSJohn Baldwin return (EFAULT); 3143911ee2cSEd Maste 31503b0d68cSJohn Baldwin if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 31603b0d68cSJohn Baldwin suword(&arginfo->ps_nenvstr, envc) != 0) 31703b0d68cSJohn Baldwin return (EFAULT); 3183911ee2cSEd Maste 3193911ee2cSEd Maste /* Fill in environment portion of vector table. */ 3203911ee2cSEd Maste for (; envc > 0; --envc) { 32131174518SJohn Baldwin if (suword(vectp++, ustringp) != 0) 32203b0d68cSJohn Baldwin return (EFAULT); 3233911ee2cSEd Maste while (*stringp++ != 0) 32431174518SJohn Baldwin ustringp++; 32531174518SJohn Baldwin ustringp++; 3263911ee2cSEd Maste } 3273911ee2cSEd Maste 3283911ee2cSEd Maste /* The end of the vector table is a null pointer. */ 32903b0d68cSJohn Baldwin if (suword(vectp, 0) != 0) 33003b0d68cSJohn Baldwin return (EFAULT); 33103b0d68cSJohn Baldwin 332d8010b11SJohn Baldwin if (imgp->auxargs) { 333d8010b11SJohn Baldwin vectp++; 334d8010b11SJohn Baldwin error = imgp->sysent->sv_copyout_auxargs(imgp, 335d8010b11SJohn Baldwin (uintptr_t)vectp); 336d8010b11SJohn Baldwin if (error != 0) 337d8010b11SJohn Baldwin return (error); 338d8010b11SJohn Baldwin } 339d8010b11SJohn Baldwin 34003b0d68cSJohn Baldwin return (0); 3413911ee2cSEd Maste } 3423911ee2cSEd Maste 3433911ee2cSEd Maste /* 3443911ee2cSEd Maste * Reset registers to default values on exec. 3453911ee2cSEd Maste */ 3463911ee2cSEd Maste static void 34731174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp, 34831174518SJohn Baldwin uintptr_t stack) 3493911ee2cSEd Maste { 3503911ee2cSEd Maste struct trapframe *regs = td->td_frame; 351a2a8b582SMitchell Horne struct pcb *pcb = td->td_pcb; 3523911ee2cSEd Maste 3533911ee2cSEd Maste /* LINUXTODO: validate */ 3543911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo); 3553911ee2cSEd Maste 3563911ee2cSEd Maste memset(regs, 0, sizeof(*regs)); 3573911ee2cSEd Maste /* glibc start.S registers function pointer in x0 with atexit. */ 3583911ee2cSEd Maste regs->tf_sp = stack; 3593911ee2cSEd Maste #if 0 /* LINUXTODO: See if this is used. */ 3603911ee2cSEd Maste regs->tf_lr = imgp->entry_addr; 3613911ee2cSEd Maste #else 3623911ee2cSEd Maste regs->tf_lr = 0xffffffffffffffff; 3633911ee2cSEd Maste #endif 3643911ee2cSEd Maste regs->tf_elr = imgp->entry_addr; 365953a7d7cSAlex Richardson 366a2a8b582SMitchell Horne pcb->pcb_tpidr_el0 = 0; 367a2a8b582SMitchell Horne pcb->pcb_tpidrro_el0 = 0; 3680723b409SJohn Baldwin WRITE_SPECIALREG(tpidrro_el0, 0); 3690723b409SJohn Baldwin WRITE_SPECIALREG(tpidr_el0, 0); 3700723b409SJohn Baldwin 371953a7d7cSAlex Richardson #ifdef VFP 372a2a8b582SMitchell Horne vfp_reset_state(td, pcb); 373953a7d7cSAlex Richardson #endif 374a2a8b582SMitchell Horne 375a2a8b582SMitchell Horne /* 376a2a8b582SMitchell Horne * Clear debug register state. It is not applicable to the new process. 377a2a8b582SMitchell Horne */ 378a2a8b582SMitchell Horne bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs)); 3793911ee2cSEd Maste } 3803911ee2cSEd Maste 3813911ee2cSEd Maste int 3823911ee2cSEd Maste linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 3833911ee2cSEd Maste { 3843911ee2cSEd Maste 3853911ee2cSEd Maste /* LINUXTODO: implement */ 3863911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo); 3873911ee2cSEd Maste return (EDOOFUS); 3883911ee2cSEd Maste } 3893911ee2cSEd Maste 3903911ee2cSEd Maste static void 3913911ee2cSEd Maste linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 3923911ee2cSEd Maste { 3933911ee2cSEd Maste 3943911ee2cSEd Maste /* LINUXTODO: implement */ 3953911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo); 3963911ee2cSEd Maste } 3973911ee2cSEd Maste 3983911ee2cSEd Maste struct sysentvec elf_linux_sysvec = { 3993911ee2cSEd Maste .sv_size = LINUX_SYS_MAXSYSCALL, 4003911ee2cSEd Maste .sv_table = linux_sysent, 4013911ee2cSEd Maste .sv_transtrap = linux_translate_traps, 4023911ee2cSEd Maste .sv_fixup = linux_elf_fixup, 4033911ee2cSEd Maste .sv_sendsig = linux_rt_sendsig, 4043911ee2cSEd Maste .sv_sigcode = &_binary_linux_locore_o_start, 4053911ee2cSEd Maste .sv_szsigcode = &linux_szsigcode, 4063911ee2cSEd Maste .sv_name = "Linux ELF64", 4073911ee2cSEd Maste .sv_coredump = elf64_coredump, 408435754a5SEdward Tomasz Napierala .sv_elf_core_osabi = ELFOSABI_NONE, 40945d99014SEdward Tomasz Napierala .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 41045d99014SEdward Tomasz Napierala .sv_elf_core_prepare_notes = linux64_prepare_notes, 4113911ee2cSEd Maste .sv_imgact_try = linux_exec_imgact_try, 4123911ee2cSEd Maste .sv_minsigstksz = LINUX_MINSIGSTKSZ, 4133911ee2cSEd Maste .sv_minuser = VM_MIN_ADDRESS, 4143911ee2cSEd Maste .sv_maxuser = VM_MAXUSER_ADDRESS, 4153911ee2cSEd Maste .sv_usrstack = USRSTACK, 4163911ee2cSEd Maste .sv_psstrings = PS_STRINGS, /* XXX */ 417d4f55cc8SEd Maste .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 4185caa67faSJohn Baldwin .sv_copyout_auxargs = linux_copyout_auxargs, 4193911ee2cSEd Maste .sv_copyout_strings = linux_copyout_strings, 4203911ee2cSEd Maste .sv_setregs = linux_exec_setregs, 4213911ee2cSEd Maste .sv_fixlimit = NULL, 4223911ee2cSEd Maste .sv_maxssiz = NULL, 423870e197dSKonstantin Belousov .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 424870e197dSKonstantin Belousov SV_SIG_WAITNDQ, 4253911ee2cSEd Maste .sv_set_syscall_retval = linux_set_syscall_retval, 4263911ee2cSEd Maste .sv_fetch_syscall_args = linux_fetch_syscall_args, 4273911ee2cSEd Maste .sv_syscallnames = NULL, 4283911ee2cSEd Maste .sv_shared_page_base = SHAREDPAGE, 4293911ee2cSEd Maste .sv_shared_page_len = PAGE_SIZE, 4303911ee2cSEd Maste .sv_schedtail = linux_schedtail, 4313911ee2cSEd Maste .sv_thread_detach = linux_thread_detach, 432*84a3963dSEdward Tomasz Napierala .sv_trap = NULL, 433b501b2aeSEdward Tomasz Napierala .sv_hwcap = &elf_hwcap, 434b501b2aeSEdward Tomasz Napierala .sv_hwcap2 = &elf_hwcap2, 4354815f175SKonstantin Belousov .sv_onexec = linux_on_exec, 4364815f175SKonstantin Belousov .sv_onexit = linux_on_exit, 4374815f175SKonstantin Belousov .sv_ontdexit = linux_thread_dtor, 438598f6fb4SKonstantin Belousov .sv_setid_allowed = &linux_setid_allowed_query, 4393911ee2cSEd Maste }; 4403911ee2cSEd Maste 4413911ee2cSEd Maste static void 4423911ee2cSEd Maste linux_vdso_install(const void *param) 4433911ee2cSEd Maste { 4443911ee2cSEd Maste 4453911ee2cSEd Maste linux_szsigcode = (&_binary_linux_locore_o_end - 4463911ee2cSEd Maste &_binary_linux_locore_o_start); 4473911ee2cSEd Maste 4483911ee2cSEd Maste if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 4493911ee2cSEd Maste panic("invalid Linux VDSO size\n"); 4503911ee2cSEd Maste 4513911ee2cSEd Maste __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 4523911ee2cSEd Maste 4533911ee2cSEd Maste linux_shared_page_obj = __elfN(linux_shared_page_init) 4543911ee2cSEd Maste (&linux_shared_page_mapping); 4553911ee2cSEd Maste 4563911ee2cSEd Maste __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 4573911ee2cSEd Maste 4583911ee2cSEd Maste memcpy(linux_shared_page_mapping, elf_linux_sysvec.sv_sigcode, 4593911ee2cSEd Maste linux_szsigcode); 4603911ee2cSEd Maste elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 4613911ee2cSEd Maste } 4623911ee2cSEd Maste SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY, 4633911ee2cSEd Maste linux_vdso_install, NULL); 4643911ee2cSEd Maste 4653911ee2cSEd Maste static void 4663911ee2cSEd Maste linux_vdso_deinstall(const void *param) 4673911ee2cSEd Maste { 4683911ee2cSEd Maste 4693911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_vdso_deinstall, todo); 4700fc8a796SMark Johnston __elfN(linux_shared_page_fini)(linux_shared_page_obj, 4710fc8a796SMark Johnston linux_shared_page_mapping); 4723911ee2cSEd Maste } 4733911ee2cSEd Maste SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 4743911ee2cSEd Maste linux_vdso_deinstall, NULL); 4753911ee2cSEd Maste 4763911ee2cSEd Maste static char GNU_ABI_VENDOR[] = "GNU"; 4773911ee2cSEd Maste static int GNU_ABI_LINUX = 0; 4783911ee2cSEd Maste 4793911ee2cSEd Maste /* LINUXTODO: deduplicate */ 4803911ee2cSEd Maste static bool 4813911ee2cSEd Maste linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 4823911ee2cSEd Maste { 4833911ee2cSEd Maste const Elf32_Word *desc; 4843911ee2cSEd Maste uintptr_t p; 4853911ee2cSEd Maste 4863911ee2cSEd Maste p = (uintptr_t)(note + 1); 4873911ee2cSEd Maste p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 4883911ee2cSEd Maste 4893911ee2cSEd Maste desc = (const Elf32_Word *)p; 4903911ee2cSEd Maste if (desc[0] != GNU_ABI_LINUX) 4913911ee2cSEd Maste return (false); 4923911ee2cSEd Maste 4933911ee2cSEd Maste *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 4943911ee2cSEd Maste return (true); 4953911ee2cSEd Maste } 4963911ee2cSEd Maste 4973911ee2cSEd Maste static Elf_Brandnote linux64_brandnote = { 4983911ee2cSEd Maste .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 4993911ee2cSEd Maste .hdr.n_descsz = 16, 5003911ee2cSEd Maste .hdr.n_type = 1, 5013911ee2cSEd Maste .vendor = GNU_ABI_VENDOR, 5023911ee2cSEd Maste .flags = BN_TRANSLATE_OSREL, 5033911ee2cSEd Maste .trans_osrel = linux_trans_osrel 5043911ee2cSEd Maste }; 5053911ee2cSEd Maste 5063911ee2cSEd Maste static Elf64_Brandinfo linux_glibc2brand = { 5073911ee2cSEd Maste .brand = ELFOSABI_LINUX, 5083911ee2cSEd Maste .machine = EM_AARCH64, 5093911ee2cSEd Maste .compat_3_brand = "Linux", 510b5f20658SEdward Tomasz Napierala .emul_path = linux_emul_path, 5113911ee2cSEd Maste .interp_path = "/lib64/ld-linux-x86-64.so.2", 5123911ee2cSEd Maste .sysvec = &elf_linux_sysvec, 5133911ee2cSEd Maste .interp_newpath = NULL, 5143911ee2cSEd Maste .brand_note = &linux64_brandnote, 5153911ee2cSEd Maste .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 5163911ee2cSEd Maste }; 5173911ee2cSEd Maste 5183911ee2cSEd Maste Elf64_Brandinfo *linux_brandlist[] = { 5193911ee2cSEd Maste &linux_glibc2brand, 5203911ee2cSEd Maste NULL 5213911ee2cSEd Maste }; 5223911ee2cSEd Maste 5233911ee2cSEd Maste static int 5243911ee2cSEd Maste linux64_elf_modevent(module_t mod, int type, void *data) 5253911ee2cSEd Maste { 5263911ee2cSEd Maste Elf64_Brandinfo **brandinfo; 5273911ee2cSEd Maste struct linux_ioctl_handler**lihp; 5283911ee2cSEd Maste int error; 5293911ee2cSEd Maste 5303911ee2cSEd Maste error = 0; 5313911ee2cSEd Maste switch(type) { 5323911ee2cSEd Maste case MOD_LOAD: 5333911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 5343911ee2cSEd Maste ++brandinfo) 5353911ee2cSEd Maste if (elf64_insert_brand_entry(*brandinfo) < 0) 5363911ee2cSEd Maste error = EINVAL; 5373911ee2cSEd Maste if (error == 0) { 5383911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set) 5393911ee2cSEd Maste linux_ioctl_register_handler(*lihp); 5403911ee2cSEd Maste stclohz = (stathz ? stathz : hz); 5413911ee2cSEd Maste if (bootverbose) 5423911ee2cSEd Maste printf("Linux arm64 ELF exec handler installed\n"); 5433911ee2cSEd Maste } 5443911ee2cSEd Maste break; 5453911ee2cSEd Maste case MOD_UNLOAD: 5463911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 5473911ee2cSEd Maste ++brandinfo) 5483911ee2cSEd Maste if (elf64_brand_inuse(*brandinfo)) 5493911ee2cSEd Maste error = EBUSY; 5503911ee2cSEd Maste if (error == 0) { 5513911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; 5523911ee2cSEd Maste *brandinfo != NULL; ++brandinfo) 5533911ee2cSEd Maste if (elf64_remove_brand_entry(*brandinfo) < 0) 5543911ee2cSEd Maste error = EINVAL; 5553911ee2cSEd Maste } 5563911ee2cSEd Maste if (error == 0) { 5573911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set) 5583911ee2cSEd Maste linux_ioctl_unregister_handler(*lihp); 5593911ee2cSEd Maste if (bootverbose) 5603911ee2cSEd Maste printf("Linux ELF exec handler removed\n"); 5613911ee2cSEd Maste } else 5623911ee2cSEd Maste printf("Could not deinstall ELF interpreter entry\n"); 5633911ee2cSEd Maste break; 5643911ee2cSEd Maste default: 5653911ee2cSEd Maste return (EOPNOTSUPP); 5663911ee2cSEd Maste } 5673911ee2cSEd Maste return (error); 5683911ee2cSEd Maste } 5693911ee2cSEd Maste 5703911ee2cSEd Maste static moduledata_t linux64_elf_mod = { 5713911ee2cSEd Maste "linux64elf", 5723911ee2cSEd Maste linux64_elf_modevent, 5733911ee2cSEd Maste 0 5743911ee2cSEd Maste }; 5753911ee2cSEd Maste 5763911ee2cSEd Maste DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 5773911ee2cSEd Maste MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 5783911ee2cSEd Maste FEATURE(linux64, "AArch64 Linux 64bit support"); 579