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> 44*9931033bSDmitry Chagin #include <sys/stddef.h> 453911ee2cSEd Maste #include <sys/signalvar.h> 463911ee2cSEd Maste #include <sys/sysctl.h> 473911ee2cSEd Maste #include <sys/sysent.h> 483911ee2cSEd Maste 49*9931033bSDmitry Chagin #include <vm/vm.h> 50*9931033bSDmitry Chagin #include <vm/pmap.h> 51*9931033bSDmitry Chagin #include <vm/vm_map.h> 52*9931033bSDmitry Chagin #include <vm/vm_extern.h> 53*9931033bSDmitry Chagin #include <vm/vm_object.h> 54*9931033bSDmitry Chagin #include <vm/vm_page.h> 553911ee2cSEd Maste #include <vm/vm_param.h> 563911ee2cSEd Maste 573911ee2cSEd Maste #include <arm64/linux/linux.h> 583911ee2cSEd Maste #include <arm64/linux/linux_proto.h> 593911ee2cSEd Maste #include <compat/linux/linux_dtrace.h> 603911ee2cSEd Maste #include <compat/linux/linux_emul.h> 613911ee2cSEd Maste #include <compat/linux/linux_ioctl.h> 623911ee2cSEd Maste #include <compat/linux/linux_mib.h> 633911ee2cSEd Maste #include <compat/linux/linux_misc.h> 64b5f20658SEdward Tomasz Napierala #include <compat/linux/linux_util.h> 653911ee2cSEd Maste #include <compat/linux/linux_vdso.h> 663911ee2cSEd Maste 67b501b2aeSEdward Tomasz Napierala #include <machine/md_var.h> 68b501b2aeSEdward Tomasz Napierala 69953a7d7cSAlex Richardson #ifdef VFP 70953a7d7cSAlex Richardson #include <machine/vfp.h> 71953a7d7cSAlex Richardson #endif 72953a7d7cSAlex Richardson 733911ee2cSEd Maste MODULE_VERSION(linux64elf, 1); 743911ee2cSEd Maste 75*9931033bSDmitry Chagin #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 76*9931033bSDmitry Chagin #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - \ 77*9931033bSDmitry Chagin LINUX_VDSOPAGE_SIZE) 78*9931033bSDmitry Chagin #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE) 79*9931033bSDmitry Chagin /* 80*9931033bSDmitry Chagin * PAGE_SIZE - the size 81*9931033bSDmitry Chagin * of the native SHAREDPAGE 82*9931033bSDmitry Chagin */ 83*9931033bSDmitry Chagin #define LINUX_USRSTACK LINUX_SHAREDPAGE 84*9931033bSDmitry Chagin #define LINUX_PS_STRINGS (LINUX_USRSTACK - \ 85*9931033bSDmitry Chagin sizeof(struct ps_strings)) 86*9931033bSDmitry Chagin 873911ee2cSEd Maste static int linux_szsigcode; 88*9931033bSDmitry Chagin static vm_object_t linux_vdso_obj; 89*9931033bSDmitry Chagin static char *linux_vdso_mapping; 90*9931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_start; 91*9931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_end; 92*9931033bSDmitry Chagin static vm_offset_t linux_vdso_base; 933911ee2cSEd Maste 943911ee2cSEd Maste extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 953911ee2cSEd Maste 963911ee2cSEd Maste SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 973911ee2cSEd Maste 9803b0d68cSJohn Baldwin static int linux_copyout_strings(struct image_params *imgp, 9931174518SJohn Baldwin uintptr_t *stack_base); 10031174518SJohn Baldwin static int linux_elf_fixup(uintptr_t *stack_base, 1013911ee2cSEd Maste struct image_params *iparams); 1023911ee2cSEd Maste static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 1033911ee2cSEd Maste static void linux_vdso_install(const void *param); 1043911ee2cSEd Maste static void linux_vdso_deinstall(const void *param); 105*9931033bSDmitry Chagin static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 1063911ee2cSEd Maste static void linux_set_syscall_retval(struct thread *td, int error); 1073911ee2cSEd Maste static int linux_fetch_syscall_args(struct thread *td); 1083911ee2cSEd Maste static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 10931174518SJohn Baldwin uintptr_t stack); 110*9931033bSDmitry Chagin static void linux_exec_sysvec_init(void *param); 1115fd9cd53SDmitry Chagin static int linux_on_exec_vmspace(struct proc *p, 1125fd9cd53SDmitry Chagin struct image_params *imgp); 1133911ee2cSEd Maste 1143911ee2cSEd Maste /* DTrace init */ 1153911ee2cSEd Maste LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 1163911ee2cSEd Maste 1173911ee2cSEd Maste /* DTrace probes */ 1183911ee2cSEd Maste LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int"); 1193911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo); 1205caa67faSJohn Baldwin LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo); 1213911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo); 1223911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo); 1233911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo); 1243911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo); 1253911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo); 1263911ee2cSEd Maste 127*9931033bSDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform); 128*9931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 129*9931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(__kernel_rt_sigreturn); 130*9931033bSDmitry Chagin 1313911ee2cSEd Maste /* LINUXTODO: do we have traps to translate? */ 1323911ee2cSEd Maste static int 1333911ee2cSEd Maste linux_translate_traps(int signal, int trap_code) 1343911ee2cSEd Maste { 1353911ee2cSEd Maste 1363911ee2cSEd Maste LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code); 1373911ee2cSEd Maste return (signal); 1383911ee2cSEd Maste } 1393911ee2cSEd Maste 1403911ee2cSEd Maste static int 1413911ee2cSEd Maste linux_fetch_syscall_args(struct thread *td) 1423911ee2cSEd Maste { 1433911ee2cSEd Maste struct proc *p; 1443911ee2cSEd Maste struct syscall_args *sa; 1453911ee2cSEd Maste register_t *ap; 1463911ee2cSEd Maste 1473911ee2cSEd Maste p = td->td_proc; 1483911ee2cSEd Maste ap = td->td_frame->tf_x; 1493911ee2cSEd Maste sa = &td->td_sa; 1503911ee2cSEd Maste 1513911ee2cSEd Maste sa->code = td->td_frame->tf_x[8]; 152cf98bc28SDavid Chisnall sa->original_code = sa->code; 1533911ee2cSEd Maste /* LINUXTODO: generic syscall? */ 1543911ee2cSEd Maste if (sa->code >= p->p_sysent->sv_size) 1553911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[0]; 1563911ee2cSEd Maste else 1573911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[sa->code]; 1583911ee2cSEd Maste 1591e2521ffSEdward Tomasz Napierala if (sa->callp->sy_narg > MAXARGS) 1601e2521ffSEdward Tomasz Napierala panic("ARM64TODO: Could we have more than %d args?", MAXARGS); 1611e2521ffSEdward Tomasz Napierala memcpy(sa->args, ap, MAXARGS * sizeof(register_t)); 1623911ee2cSEd Maste 1633911ee2cSEd Maste td->td_retval[0] = 0; 1643911ee2cSEd Maste return (0); 1653911ee2cSEd Maste } 1663911ee2cSEd Maste 1673911ee2cSEd Maste static void 1683911ee2cSEd Maste linux_set_syscall_retval(struct thread *td, int error) 1693911ee2cSEd Maste { 1703911ee2cSEd Maste 1718e5d76e6SAndrew Turner td->td_retval[1] = td->td_frame->tf_x[1]; 1728e5d76e6SAndrew Turner cpu_set_syscall_retval(td, error); 173c26391f4SEdward Tomasz Napierala 174c26391f4SEdward Tomasz Napierala if (__predict_false(error != 0)) { 175866b1f51SEdward Tomasz Napierala if (error != ERESTART && error != EJUSTRETURN) 176866b1f51SEdward Tomasz Napierala td->td_frame->tf_x[0] = bsd_to_linux_errno(error); 177c26391f4SEdward Tomasz Napierala } 1783911ee2cSEd Maste } 1793911ee2cSEd Maste 18003b0d68cSJohn Baldwin static int 181d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 1823911ee2cSEd Maste { 1833911ee2cSEd Maste Elf_Auxargs *args; 1843911ee2cSEd Maste Elf_Auxinfo *argarray, *pos; 1853911ee2cSEd Maste struct proc *p; 18603b0d68cSJohn Baldwin int error, issetugid; 1873911ee2cSEd Maste 1885caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo); 1893911ee2cSEd Maste p = imgp->proc; 1903911ee2cSEd Maste 1913911ee2cSEd Maste args = (Elf64_Auxargs *)imgp->auxargs; 1923911ee2cSEd Maste argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 1933911ee2cSEd Maste M_WAITOK | M_ZERO); 1943911ee2cSEd Maste 1953911ee2cSEd Maste issetugid = p->p_flag & P_SUGID ? 1 : 0; 196*9931033bSDmitry Chagin AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 197b501b2aeSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap); 198aa462cabSEdward Tomasz Napierala AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 1993911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 2003911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 2013911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 2023911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 2033911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_BASE, args->base); 2043911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 2053911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 2063911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 2073911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 2083911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 2093911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 2103911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 211b24e6ac8SBrooks Davis AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 212b501b2aeSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2); 2133911ee2cSEd Maste if (imgp->execpathp != 0) 214b24e6ac8SBrooks Davis AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 2153911ee2cSEd Maste if (args->execfd != -1) 2163911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 217aa462cabSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 2183911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_NULL, 0); 219aa462cabSEdward Tomasz Napierala 2203911ee2cSEd Maste free(imgp->auxargs, M_TEMP); 2213911ee2cSEd Maste imgp->auxargs = NULL; 2223911ee2cSEd Maste KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 2233911ee2cSEd Maste 224d8010b11SJohn Baldwin error = copyout(argarray, (void *)base, 225d8010b11SJohn Baldwin sizeof(*argarray) * LINUX_AT_COUNT); 2263911ee2cSEd Maste free(argarray, M_TEMP); 22703b0d68cSJohn Baldwin return (error); 2285caa67faSJohn Baldwin } 2295caa67faSJohn Baldwin 2305caa67faSJohn Baldwin static int 23131174518SJohn Baldwin linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp) 2325caa67faSJohn Baldwin { 2335caa67faSJohn Baldwin 2345caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo); 2353911ee2cSEd Maste 2363911ee2cSEd Maste return (0); 2373911ee2cSEd Maste } 2383911ee2cSEd Maste 2393911ee2cSEd Maste /* 2403911ee2cSEd Maste * Copy strings out to the new process address space, constructing new arg 2413911ee2cSEd Maste * and env vector tables. Return a pointer to the base so that it can be used 2423911ee2cSEd Maste * as the initial stack pointer. 2433911ee2cSEd Maste * LINUXTODO: deduplicate against other linuxulator archs 2443911ee2cSEd Maste */ 24503b0d68cSJohn Baldwin static int 24631174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 2473911ee2cSEd Maste { 2483911ee2cSEd Maste char **vectp; 24931174518SJohn Baldwin char *stringp; 2500386b6c8SLi-Wen Hsu uintptr_t destp, ustringp; 2513911ee2cSEd Maste struct ps_strings *arginfo; 2523911ee2cSEd Maste char canary[LINUX_AT_RANDOM_LEN]; 2533911ee2cSEd Maste size_t execpath_len; 2543911ee2cSEd Maste struct proc *p; 25503b0d68cSJohn Baldwin int argc, envc, error; 2563911ee2cSEd Maste 2573911ee2cSEd Maste /* Calculate string base and vector table pointers. */ 2583911ee2cSEd Maste if (imgp->execpath != NULL && imgp->auxargs != NULL) 2593911ee2cSEd Maste execpath_len = strlen(imgp->execpath) + 1; 2603911ee2cSEd Maste else 2613911ee2cSEd Maste execpath_len = 0; 2623911ee2cSEd Maste 2633911ee2cSEd Maste p = imgp->proc; 2643911ee2cSEd Maste arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 26531174518SJohn Baldwin destp = (uintptr_t)arginfo; 2663911ee2cSEd Maste 2673911ee2cSEd Maste if (execpath_len != 0) { 26831174518SJohn Baldwin destp -= execpath_len; 26931174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 270b24e6ac8SBrooks Davis imgp->execpathp = (void *)destp; 271b24e6ac8SBrooks Davis error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 27203b0d68cSJohn Baldwin if (error != 0) 27303b0d68cSJohn Baldwin return (error); 2743911ee2cSEd Maste } 2753911ee2cSEd Maste 2763911ee2cSEd Maste /* Prepare the canary for SSP. */ 2773911ee2cSEd Maste arc4rand(canary, sizeof(canary), 0); 27831174518SJohn Baldwin destp -= roundup(sizeof(canary), sizeof(void *)); 279b24e6ac8SBrooks Davis imgp->canary = (void *)destp; 280b24e6ac8SBrooks Davis error = copyout(canary, imgp->canary, sizeof(canary)); 28103b0d68cSJohn Baldwin if (error != 0) 28203b0d68cSJohn Baldwin return (error); 2833911ee2cSEd Maste 28431174518SJohn Baldwin /* Allocate room for the argument and environment strings. */ 28531174518SJohn Baldwin destp -= ARG_MAX - imgp->args->stringspace; 28631174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 28731174518SJohn Baldwin ustringp = destp; 28831174518SJohn Baldwin 28903b0d68cSJohn Baldwin if (imgp->auxargs) { 290d8010b11SJohn Baldwin /* 291d8010b11SJohn Baldwin * Allocate room on the stack for the ELF auxargs 292d8010b11SJohn Baldwin * array. It has up to LINUX_AT_COUNT entries. 293d8010b11SJohn Baldwin */ 294d8010b11SJohn Baldwin destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo); 295d8010b11SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 29603b0d68cSJohn Baldwin } 2973911ee2cSEd Maste 29831174518SJohn Baldwin vectp = (char **)destp; 29931174518SJohn Baldwin 3003911ee2cSEd Maste /* 3013911ee2cSEd Maste * Allocate room for argc and the argv[] and env vectors including the 3023911ee2cSEd Maste * terminating NULL pointers. 3033911ee2cSEd Maste */ 3043911ee2cSEd Maste vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1; 3053911ee2cSEd Maste vectp = (char **)STACKALIGN(vectp); 3063911ee2cSEd Maste 3073911ee2cSEd Maste /* vectp also becomes our initial stack base. */ 30831174518SJohn Baldwin *stack_base = (uintptr_t)vectp; 3093911ee2cSEd Maste 3103911ee2cSEd Maste stringp = imgp->args->begin_argv; 3113911ee2cSEd Maste argc = imgp->args->argc; 3123911ee2cSEd Maste envc = imgp->args->envc; 3133911ee2cSEd Maste 3143911ee2cSEd Maste /* Copy out strings - arguments and environment. */ 31531174518SJohn Baldwin error = copyout(stringp, (void *)ustringp, 31631174518SJohn Baldwin ARG_MAX - imgp->args->stringspace); 31703b0d68cSJohn Baldwin if (error != 0) 31803b0d68cSJohn Baldwin return (error); 3193911ee2cSEd Maste 3203911ee2cSEd Maste /* Fill in "ps_strings" struct for ps, w, etc. */ 32103b0d68cSJohn Baldwin if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 32203b0d68cSJohn Baldwin suword(&arginfo->ps_nargvstr, argc) != 0) 32303b0d68cSJohn Baldwin return (EFAULT); 3243911ee2cSEd Maste 32503b0d68cSJohn Baldwin if (suword(vectp++, argc) != 0) 32603b0d68cSJohn Baldwin return (EFAULT); 32703b0d68cSJohn Baldwin 3283911ee2cSEd Maste /* Fill in argument portion of vector table. */ 3293911ee2cSEd Maste for (; argc > 0; --argc) { 33031174518SJohn Baldwin if (suword(vectp++, ustringp) != 0) 33103b0d68cSJohn Baldwin return (EFAULT); 3323911ee2cSEd Maste while (*stringp++ != 0) 33331174518SJohn Baldwin ustringp++; 33431174518SJohn Baldwin ustringp++; 3353911ee2cSEd Maste } 3363911ee2cSEd Maste 3373911ee2cSEd Maste /* A null vector table pointer separates the argp's from the envp's. */ 33803b0d68cSJohn Baldwin if (suword(vectp++, 0) != 0) 33903b0d68cSJohn Baldwin return (EFAULT); 3403911ee2cSEd Maste 34103b0d68cSJohn Baldwin if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 34203b0d68cSJohn Baldwin suword(&arginfo->ps_nenvstr, envc) != 0) 34303b0d68cSJohn Baldwin return (EFAULT); 3443911ee2cSEd Maste 3453911ee2cSEd Maste /* Fill in environment portion of vector table. */ 3463911ee2cSEd Maste for (; envc > 0; --envc) { 34731174518SJohn Baldwin if (suword(vectp++, ustringp) != 0) 34803b0d68cSJohn Baldwin return (EFAULT); 3493911ee2cSEd Maste while (*stringp++ != 0) 35031174518SJohn Baldwin ustringp++; 35131174518SJohn Baldwin ustringp++; 3523911ee2cSEd Maste } 3533911ee2cSEd Maste 3543911ee2cSEd Maste /* The end of the vector table is a null pointer. */ 35503b0d68cSJohn Baldwin if (suword(vectp, 0) != 0) 35603b0d68cSJohn Baldwin return (EFAULT); 35703b0d68cSJohn Baldwin 358d8010b11SJohn Baldwin if (imgp->auxargs) { 359d8010b11SJohn Baldwin vectp++; 360d8010b11SJohn Baldwin error = imgp->sysent->sv_copyout_auxargs(imgp, 361d8010b11SJohn Baldwin (uintptr_t)vectp); 362d8010b11SJohn Baldwin if (error != 0) 363d8010b11SJohn Baldwin return (error); 364d8010b11SJohn Baldwin } 365d8010b11SJohn Baldwin 36603b0d68cSJohn Baldwin return (0); 3673911ee2cSEd Maste } 3683911ee2cSEd Maste 3693911ee2cSEd Maste /* 3703911ee2cSEd Maste * Reset registers to default values on exec. 3713911ee2cSEd Maste */ 3723911ee2cSEd Maste static void 37331174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp, 37431174518SJohn Baldwin uintptr_t stack) 3753911ee2cSEd Maste { 3763911ee2cSEd Maste struct trapframe *regs = td->td_frame; 377a2a8b582SMitchell Horne struct pcb *pcb = td->td_pcb; 3783911ee2cSEd Maste 3793911ee2cSEd Maste /* LINUXTODO: validate */ 3803911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo); 3813911ee2cSEd Maste 3823911ee2cSEd Maste memset(regs, 0, sizeof(*regs)); 3833911ee2cSEd Maste /* glibc start.S registers function pointer in x0 with atexit. */ 3843911ee2cSEd Maste regs->tf_sp = stack; 3853911ee2cSEd Maste #if 0 /* LINUXTODO: See if this is used. */ 3863911ee2cSEd Maste regs->tf_lr = imgp->entry_addr; 3873911ee2cSEd Maste #else 3883911ee2cSEd Maste regs->tf_lr = 0xffffffffffffffff; 3893911ee2cSEd Maste #endif 3903911ee2cSEd Maste regs->tf_elr = imgp->entry_addr; 391953a7d7cSAlex Richardson 392a2a8b582SMitchell Horne pcb->pcb_tpidr_el0 = 0; 393a2a8b582SMitchell Horne pcb->pcb_tpidrro_el0 = 0; 3940723b409SJohn Baldwin WRITE_SPECIALREG(tpidrro_el0, 0); 3950723b409SJohn Baldwin WRITE_SPECIALREG(tpidr_el0, 0); 3960723b409SJohn Baldwin 397953a7d7cSAlex Richardson #ifdef VFP 398a2a8b582SMitchell Horne vfp_reset_state(td, pcb); 399953a7d7cSAlex Richardson #endif 400a2a8b582SMitchell Horne 401a2a8b582SMitchell Horne /* 402a2a8b582SMitchell Horne * Clear debug register state. It is not applicable to the new process. 403a2a8b582SMitchell Horne */ 404a2a8b582SMitchell Horne bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs)); 4053911ee2cSEd Maste } 4063911ee2cSEd Maste 4073911ee2cSEd Maste int 4083911ee2cSEd Maste linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 4093911ee2cSEd Maste { 4103911ee2cSEd Maste 4113911ee2cSEd Maste /* LINUXTODO: implement */ 4123911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo); 4133911ee2cSEd Maste return (EDOOFUS); 4143911ee2cSEd Maste } 4153911ee2cSEd Maste 4163911ee2cSEd Maste static void 4173911ee2cSEd Maste linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 4183911ee2cSEd Maste { 4193911ee2cSEd Maste 4203911ee2cSEd Maste /* LINUXTODO: implement */ 4213911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo); 4223911ee2cSEd Maste } 4233911ee2cSEd Maste 4243911ee2cSEd Maste struct sysentvec elf_linux_sysvec = { 4253911ee2cSEd Maste .sv_size = LINUX_SYS_MAXSYSCALL, 4263911ee2cSEd Maste .sv_table = linux_sysent, 4273911ee2cSEd Maste .sv_transtrap = linux_translate_traps, 4283911ee2cSEd Maste .sv_fixup = linux_elf_fixup, 4293911ee2cSEd Maste .sv_sendsig = linux_rt_sendsig, 430*9931033bSDmitry Chagin .sv_sigcode = &_binary_linux_vdso_so_o_start, 4313911ee2cSEd Maste .sv_szsigcode = &linux_szsigcode, 4323911ee2cSEd Maste .sv_name = "Linux ELF64", 4333911ee2cSEd Maste .sv_coredump = elf64_coredump, 434435754a5SEdward Tomasz Napierala .sv_elf_core_osabi = ELFOSABI_NONE, 43545d99014SEdward Tomasz Napierala .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 43645d99014SEdward Tomasz Napierala .sv_elf_core_prepare_notes = linux64_prepare_notes, 4373911ee2cSEd Maste .sv_imgact_try = linux_exec_imgact_try, 4383911ee2cSEd Maste .sv_minsigstksz = LINUX_MINSIGSTKSZ, 4393911ee2cSEd Maste .sv_minuser = VM_MIN_ADDRESS, 4403911ee2cSEd Maste .sv_maxuser = VM_MAXUSER_ADDRESS, 441*9931033bSDmitry Chagin .sv_usrstack = LINUX_USRSTACK, 442*9931033bSDmitry Chagin .sv_psstrings = LINUX_PS_STRINGS, 443d4f55cc8SEd Maste .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 4445caa67faSJohn Baldwin .sv_copyout_auxargs = linux_copyout_auxargs, 4453911ee2cSEd Maste .sv_copyout_strings = linux_copyout_strings, 4463911ee2cSEd Maste .sv_setregs = linux_exec_setregs, 4473911ee2cSEd Maste .sv_fixlimit = NULL, 4483911ee2cSEd Maste .sv_maxssiz = NULL, 449870e197dSKonstantin Belousov .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 450*9931033bSDmitry Chagin SV_SIG_WAITNDQ | SV_TIMEKEEP, 4513911ee2cSEd Maste .sv_set_syscall_retval = linux_set_syscall_retval, 4523911ee2cSEd Maste .sv_fetch_syscall_args = linux_fetch_syscall_args, 4533911ee2cSEd Maste .sv_syscallnames = NULL, 454*9931033bSDmitry Chagin .sv_shared_page_base = LINUX_SHAREDPAGE, 4553911ee2cSEd Maste .sv_shared_page_len = PAGE_SIZE, 4563911ee2cSEd Maste .sv_schedtail = linux_schedtail, 4573911ee2cSEd Maste .sv_thread_detach = linux_thread_detach, 45884a3963dSEdward Tomasz Napierala .sv_trap = NULL, 459b501b2aeSEdward Tomasz Napierala .sv_hwcap = &elf_hwcap, 460b501b2aeSEdward Tomasz Napierala .sv_hwcap2 = &elf_hwcap2, 4615fd9cd53SDmitry Chagin .sv_onexec = linux_on_exec_vmspace, 4624815f175SKonstantin Belousov .sv_onexit = linux_on_exit, 4634815f175SKonstantin Belousov .sv_ontdexit = linux_thread_dtor, 464598f6fb4SKonstantin Belousov .sv_setid_allowed = &linux_setid_allowed_query, 4653911ee2cSEd Maste }; 4663911ee2cSEd Maste 4675fd9cd53SDmitry Chagin static int 4685fd9cd53SDmitry Chagin linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 4695fd9cd53SDmitry Chagin { 470*9931033bSDmitry Chagin int error; 4715fd9cd53SDmitry Chagin 472*9931033bSDmitry Chagin error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base, 473*9931033bSDmitry Chagin LINUX_VDSOPAGE_SIZE, imgp); 474*9931033bSDmitry Chagin if (error == 0) 4755fd9cd53SDmitry Chagin linux_on_exec(p, imgp); 476*9931033bSDmitry Chagin return (error); 4775fd9cd53SDmitry Chagin } 4785fd9cd53SDmitry Chagin 4793911ee2cSEd Maste static void 480*9931033bSDmitry Chagin linux_exec_sysvec_init(void *param) 481*9931033bSDmitry Chagin { 482*9931033bSDmitry Chagin l_uintptr_t *ktimekeep_base; 483*9931033bSDmitry Chagin struct sysentvec *sv; 484*9931033bSDmitry Chagin ptrdiff_t tkoff; 485*9931033bSDmitry Chagin 486*9931033bSDmitry Chagin sv = param; 487*9931033bSDmitry Chagin /* Fill timekeep_base */ 488*9931033bSDmitry Chagin exec_sysvec_init(sv); 489*9931033bSDmitry Chagin 490*9931033bSDmitry Chagin tkoff = kern_timekeep_base - linux_vdso_base; 491*9931033bSDmitry Chagin ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 492*9931033bSDmitry Chagin *ktimekeep_base = sv->sv_timekeep_base; 493*9931033bSDmitry Chagin } 494*9931033bSDmitry Chagin SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC, SI_ORDER_ANY, 495*9931033bSDmitry Chagin linux_exec_sysvec_init, &elf_linux_sysvec); 496*9931033bSDmitry Chagin 497*9931033bSDmitry Chagin static void 4983911ee2cSEd Maste linux_vdso_install(const void *param) 4993911ee2cSEd Maste { 500*9931033bSDmitry Chagin char *vdso_start = &_binary_linux_vdso_so_o_start; 501*9931033bSDmitry Chagin char *vdso_end = &_binary_linux_vdso_so_o_end; 5023911ee2cSEd Maste 503*9931033bSDmitry Chagin linux_szsigcode = vdso_end - vdso_start; 504*9931033bSDmitry Chagin MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 5053911ee2cSEd Maste 506*9931033bSDmitry Chagin linux_vdso_base = LINUX_VDSOPAGE; 5073911ee2cSEd Maste 508*9931033bSDmitry Chagin __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 5093911ee2cSEd Maste 510*9931033bSDmitry Chagin linux_vdso_obj = __elfN(linux_shared_page_init) 511*9931033bSDmitry Chagin (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 512*9931033bSDmitry Chagin bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 5133911ee2cSEd Maste 514*9931033bSDmitry Chagin linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 5153911ee2cSEd Maste } 516*9931033bSDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_FIRST, 5173911ee2cSEd Maste linux_vdso_install, NULL); 5183911ee2cSEd Maste 5193911ee2cSEd Maste static void 5203911ee2cSEd Maste linux_vdso_deinstall(const void *param) 5213911ee2cSEd Maste { 5223911ee2cSEd Maste 523*9931033bSDmitry Chagin __elfN(linux_shared_page_fini)(linux_vdso_obj, 524*9931033bSDmitry Chagin linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 5253911ee2cSEd Maste } 5263911ee2cSEd Maste SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 5273911ee2cSEd Maste linux_vdso_deinstall, NULL); 5283911ee2cSEd Maste 529*9931033bSDmitry Chagin static void 530*9931033bSDmitry Chagin linux_vdso_reloc(char *mapping, Elf_Addr offset) 531*9931033bSDmitry Chagin { 532*9931033bSDmitry Chagin Elf_Size rtype, symidx; 533*9931033bSDmitry Chagin const Elf_Rela *rela; 534*9931033bSDmitry Chagin const Elf_Shdr *shdr; 535*9931033bSDmitry Chagin const Elf_Ehdr *ehdr; 536*9931033bSDmitry Chagin Elf_Addr *where; 537*9931033bSDmitry Chagin Elf_Addr addr, addend; 538*9931033bSDmitry Chagin int i, relacnt; 539*9931033bSDmitry Chagin 540*9931033bSDmitry Chagin MPASS(offset != 0); 541*9931033bSDmitry Chagin 542*9931033bSDmitry Chagin relacnt = 0; 543*9931033bSDmitry Chagin ehdr = (const Elf_Ehdr *)mapping; 544*9931033bSDmitry Chagin shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 545*9931033bSDmitry Chagin for (i = 0; i < ehdr->e_shnum; i++) 546*9931033bSDmitry Chagin { 547*9931033bSDmitry Chagin switch (shdr[i].sh_type) { 548*9931033bSDmitry Chagin case SHT_REL: 549*9931033bSDmitry Chagin printf("Linux Aarch64 vDSO: unexpected Rel section\n"); 550*9931033bSDmitry Chagin break; 551*9931033bSDmitry Chagin case SHT_RELA: 552*9931033bSDmitry Chagin rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset); 553*9931033bSDmitry Chagin relacnt = shdr[i].sh_size / sizeof(*rela); 554*9931033bSDmitry Chagin } 555*9931033bSDmitry Chagin } 556*9931033bSDmitry Chagin 557*9931033bSDmitry Chagin for (i = 0; i < relacnt; i++, rela++) { 558*9931033bSDmitry Chagin where = (Elf_Addr *)(mapping + rela->r_offset); 559*9931033bSDmitry Chagin addend = rela->r_addend; 560*9931033bSDmitry Chagin rtype = ELF_R_TYPE(rela->r_info); 561*9931033bSDmitry Chagin symidx = ELF_R_SYM(rela->r_info); 562*9931033bSDmitry Chagin 563*9931033bSDmitry Chagin switch (rtype) { 564*9931033bSDmitry Chagin case R_AARCH64_NONE: /* none */ 565*9931033bSDmitry Chagin break; 566*9931033bSDmitry Chagin 567*9931033bSDmitry Chagin case R_AARCH64_RELATIVE: /* B + A */ 568*9931033bSDmitry Chagin addr = (Elf_Addr)(mapping + addend); 569*9931033bSDmitry Chagin if (*where != addr) 570*9931033bSDmitry Chagin *where = addr; 571*9931033bSDmitry Chagin break; 572*9931033bSDmitry Chagin default: 573*9931033bSDmitry Chagin printf("Linux Aarch64 vDSO: unexpected relocation type %ld, " 574*9931033bSDmitry Chagin "symbol index %ld\n", rtype, symidx); 575*9931033bSDmitry Chagin } 576*9931033bSDmitry Chagin } 577*9931033bSDmitry Chagin } 578*9931033bSDmitry Chagin 5793911ee2cSEd Maste static char GNU_ABI_VENDOR[] = "GNU"; 5803911ee2cSEd Maste static int GNU_ABI_LINUX = 0; 5813911ee2cSEd Maste 5823911ee2cSEd Maste /* LINUXTODO: deduplicate */ 5833911ee2cSEd Maste static bool 5843911ee2cSEd Maste linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 5853911ee2cSEd Maste { 5863911ee2cSEd Maste const Elf32_Word *desc; 5873911ee2cSEd Maste uintptr_t p; 5883911ee2cSEd Maste 5893911ee2cSEd Maste p = (uintptr_t)(note + 1); 5903911ee2cSEd Maste p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 5913911ee2cSEd Maste 5923911ee2cSEd Maste desc = (const Elf32_Word *)p; 5933911ee2cSEd Maste if (desc[0] != GNU_ABI_LINUX) 5943911ee2cSEd Maste return (false); 5953911ee2cSEd Maste 5963911ee2cSEd Maste *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 5973911ee2cSEd Maste return (true); 5983911ee2cSEd Maste } 5993911ee2cSEd Maste 6003911ee2cSEd Maste static Elf_Brandnote linux64_brandnote = { 6013911ee2cSEd Maste .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 6023911ee2cSEd Maste .hdr.n_descsz = 16, 6033911ee2cSEd Maste .hdr.n_type = 1, 6043911ee2cSEd Maste .vendor = GNU_ABI_VENDOR, 6053911ee2cSEd Maste .flags = BN_TRANSLATE_OSREL, 6063911ee2cSEd Maste .trans_osrel = linux_trans_osrel 6073911ee2cSEd Maste }; 6083911ee2cSEd Maste 6093911ee2cSEd Maste static Elf64_Brandinfo linux_glibc2brand = { 6103911ee2cSEd Maste .brand = ELFOSABI_LINUX, 6113911ee2cSEd Maste .machine = EM_AARCH64, 6123911ee2cSEd Maste .compat_3_brand = "Linux", 613b5f20658SEdward Tomasz Napierala .emul_path = linux_emul_path, 6143911ee2cSEd Maste .interp_path = "/lib64/ld-linux-x86-64.so.2", 6153911ee2cSEd Maste .sysvec = &elf_linux_sysvec, 6163911ee2cSEd Maste .interp_newpath = NULL, 6173911ee2cSEd Maste .brand_note = &linux64_brandnote, 6183911ee2cSEd Maste .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 6193911ee2cSEd Maste }; 6203911ee2cSEd Maste 6213911ee2cSEd Maste Elf64_Brandinfo *linux_brandlist[] = { 6223911ee2cSEd Maste &linux_glibc2brand, 6233911ee2cSEd Maste NULL 6243911ee2cSEd Maste }; 6253911ee2cSEd Maste 6263911ee2cSEd Maste static int 6273911ee2cSEd Maste linux64_elf_modevent(module_t mod, int type, void *data) 6283911ee2cSEd Maste { 6293911ee2cSEd Maste Elf64_Brandinfo **brandinfo; 6303911ee2cSEd Maste struct linux_ioctl_handler**lihp; 6313911ee2cSEd Maste int error; 6323911ee2cSEd Maste 6333911ee2cSEd Maste error = 0; 6343911ee2cSEd Maste switch(type) { 6353911ee2cSEd Maste case MOD_LOAD: 6363911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 6373911ee2cSEd Maste ++brandinfo) 6383911ee2cSEd Maste if (elf64_insert_brand_entry(*brandinfo) < 0) 6393911ee2cSEd Maste error = EINVAL; 6403911ee2cSEd Maste if (error == 0) { 6413911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set) 6423911ee2cSEd Maste linux_ioctl_register_handler(*lihp); 6433911ee2cSEd Maste stclohz = (stathz ? stathz : hz); 6443911ee2cSEd Maste if (bootverbose) 6453911ee2cSEd Maste printf("Linux arm64 ELF exec handler installed\n"); 6463911ee2cSEd Maste } 6473911ee2cSEd Maste break; 6483911ee2cSEd Maste case MOD_UNLOAD: 6493911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 6503911ee2cSEd Maste ++brandinfo) 6513911ee2cSEd Maste if (elf64_brand_inuse(*brandinfo)) 6523911ee2cSEd Maste error = EBUSY; 6533911ee2cSEd Maste if (error == 0) { 6543911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; 6553911ee2cSEd Maste *brandinfo != NULL; ++brandinfo) 6563911ee2cSEd Maste if (elf64_remove_brand_entry(*brandinfo) < 0) 6573911ee2cSEd Maste error = EINVAL; 6583911ee2cSEd Maste } 6593911ee2cSEd Maste if (error == 0) { 6603911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set) 6613911ee2cSEd Maste linux_ioctl_unregister_handler(*lihp); 6623911ee2cSEd Maste if (bootverbose) 6633911ee2cSEd Maste printf("Linux ELF exec handler removed\n"); 6643911ee2cSEd Maste } else 6653911ee2cSEd Maste printf("Could not deinstall ELF interpreter entry\n"); 6663911ee2cSEd Maste break; 6673911ee2cSEd Maste default: 6683911ee2cSEd Maste return (EOPNOTSUPP); 6693911ee2cSEd Maste } 6703911ee2cSEd Maste return (error); 6713911ee2cSEd Maste } 6723911ee2cSEd Maste 6733911ee2cSEd Maste static moduledata_t linux64_elf_mod = { 6743911ee2cSEd Maste "linux64elf", 6753911ee2cSEd Maste linux64_elf_modevent, 6763911ee2cSEd Maste 0 6773911ee2cSEd Maste }; 6783911ee2cSEd Maste 6793911ee2cSEd Maste DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 6803911ee2cSEd Maste MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 6813911ee2cSEd Maste FEATURE(linux64, "AArch64 Linux 64bit support"); 682