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> 40ccc510b4SEdward Tomasz Napierala #include <sys/ktr.h> 413911ee2cSEd Maste #include <sys/lock.h> 423911ee2cSEd Maste #include <sys/module.h> 433911ee2cSEd Maste #include <sys/mutex.h> 443911ee2cSEd Maste #include <sys/proc.h> 459931033bSDmitry Chagin #include <sys/stddef.h> 463911ee2cSEd Maste #include <sys/signalvar.h> 47ccc510b4SEdward Tomasz Napierala #include <sys/syscallsubr.h> 483911ee2cSEd Maste #include <sys/sysctl.h> 493911ee2cSEd Maste #include <sys/sysent.h> 503911ee2cSEd Maste 519931033bSDmitry Chagin #include <vm/vm.h> 529931033bSDmitry Chagin #include <vm/pmap.h> 539931033bSDmitry Chagin #include <vm/vm_map.h> 549931033bSDmitry Chagin #include <vm/vm_extern.h> 559931033bSDmitry Chagin #include <vm/vm_object.h> 569931033bSDmitry Chagin #include <vm/vm_page.h> 573911ee2cSEd Maste #include <vm/vm_param.h> 583911ee2cSEd Maste 593911ee2cSEd Maste #include <arm64/linux/linux.h> 603911ee2cSEd Maste #include <arm64/linux/linux_proto.h> 613911ee2cSEd Maste #include <compat/linux/linux_dtrace.h> 623911ee2cSEd Maste #include <compat/linux/linux_emul.h> 630a4b664aSDmitry Chagin #include <compat/linux/linux_fork.h> 643911ee2cSEd Maste #include <compat/linux/linux_ioctl.h> 653911ee2cSEd Maste #include <compat/linux/linux_mib.h> 663911ee2cSEd Maste #include <compat/linux/linux_misc.h> 67ccc510b4SEdward Tomasz Napierala #include <compat/linux/linux_signal.h> 68b5f20658SEdward Tomasz Napierala #include <compat/linux/linux_util.h> 693911ee2cSEd Maste #include <compat/linux/linux_vdso.h> 703911ee2cSEd Maste 7121f24617SDmitry Chagin #include <arm64/linux/linux_sigframe.h> 7221f24617SDmitry Chagin 73b501b2aeSEdward Tomasz Napierala #include <machine/md_var.h> 74b501b2aeSEdward Tomasz Napierala 75953a7d7cSAlex Richardson #ifdef VFP 76953a7d7cSAlex Richardson #include <machine/vfp.h> 77953a7d7cSAlex Richardson #endif 78953a7d7cSAlex Richardson 793911ee2cSEd Maste MODULE_VERSION(linux64elf, 1); 803911ee2cSEd Maste 819931033bSDmitry Chagin #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 829931033bSDmitry Chagin #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - \ 839931033bSDmitry Chagin LINUX_VDSOPAGE_SIZE) 849931033bSDmitry Chagin #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE) 859931033bSDmitry Chagin /* 869931033bSDmitry Chagin * PAGE_SIZE - the size 879931033bSDmitry Chagin * of the native SHAREDPAGE 889931033bSDmitry Chagin */ 899931033bSDmitry Chagin #define LINUX_USRSTACK LINUX_SHAREDPAGE 909931033bSDmitry Chagin #define LINUX_PS_STRINGS (LINUX_USRSTACK - \ 919931033bSDmitry Chagin sizeof(struct ps_strings)) 929931033bSDmitry Chagin 933911ee2cSEd Maste static int linux_szsigcode; 949931033bSDmitry Chagin static vm_object_t linux_vdso_obj; 959931033bSDmitry Chagin static char *linux_vdso_mapping; 969931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_start; 979931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_end; 989931033bSDmitry Chagin static vm_offset_t linux_vdso_base; 993911ee2cSEd Maste 1003911ee2cSEd Maste extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 1013911ee2cSEd Maste 1023911ee2cSEd Maste SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 1033911ee2cSEd Maste 10403b0d68cSJohn Baldwin static int linux_copyout_strings(struct image_params *imgp, 10531174518SJohn Baldwin uintptr_t *stack_base); 10631174518SJohn Baldwin static int linux_elf_fixup(uintptr_t *stack_base, 1073911ee2cSEd Maste struct image_params *iparams); 1083911ee2cSEd Maste static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 1093911ee2cSEd Maste static void linux_vdso_install(const void *param); 1103911ee2cSEd Maste static void linux_vdso_deinstall(const void *param); 1119931033bSDmitry Chagin static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 1123911ee2cSEd Maste static void linux_set_syscall_retval(struct thread *td, int error); 1133911ee2cSEd Maste static int linux_fetch_syscall_args(struct thread *td); 1143911ee2cSEd Maste static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 11531174518SJohn Baldwin uintptr_t stack); 1169931033bSDmitry Chagin static void linux_exec_sysvec_init(void *param); 1175fd9cd53SDmitry Chagin static int linux_on_exec_vmspace(struct proc *p, 1185fd9cd53SDmitry Chagin struct image_params *imgp); 1193911ee2cSEd Maste 1203911ee2cSEd Maste /* DTrace init */ 1213911ee2cSEd Maste LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 1223911ee2cSEd Maste 1233911ee2cSEd Maste /* DTrace probes */ 1243911ee2cSEd Maste LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int"); 1253911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo); 1265caa67faSJohn Baldwin LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo); 1273911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo); 1283911ee2cSEd Maste 1299931033bSDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform); 1309931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 131c56480a8SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode); 1329931033bSDmitry Chagin 1333911ee2cSEd Maste /* LINUXTODO: do we have traps to translate? */ 1343911ee2cSEd Maste static int 1353911ee2cSEd Maste linux_translate_traps(int signal, int trap_code) 1363911ee2cSEd Maste { 1373911ee2cSEd Maste 1383911ee2cSEd Maste LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code); 1393911ee2cSEd Maste return (signal); 1403911ee2cSEd Maste } 1413911ee2cSEd Maste 1423911ee2cSEd Maste static int 1433911ee2cSEd Maste linux_fetch_syscall_args(struct thread *td) 1443911ee2cSEd Maste { 1453911ee2cSEd Maste struct proc *p; 1463911ee2cSEd Maste struct syscall_args *sa; 1473911ee2cSEd Maste register_t *ap; 1483911ee2cSEd Maste 1493911ee2cSEd Maste p = td->td_proc; 1503911ee2cSEd Maste ap = td->td_frame->tf_x; 1513911ee2cSEd Maste sa = &td->td_sa; 1523911ee2cSEd Maste 1533911ee2cSEd Maste sa->code = td->td_frame->tf_x[8]; 154cf98bc28SDavid Chisnall sa->original_code = sa->code; 1553911ee2cSEd Maste /* LINUXTODO: generic syscall? */ 1563911ee2cSEd Maste if (sa->code >= p->p_sysent->sv_size) 1573911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[0]; 1583911ee2cSEd Maste else 1593911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[sa->code]; 1603911ee2cSEd Maste 161adb12675SBrooks Davis if (sa->callp->sy_narg > nitems(sa->args)) 162adb12675SBrooks Davis panic("ARM64TODO: Could we have more than %zu args?", 163adb12675SBrooks Davis nitems(sa->args)); 164adb12675SBrooks Davis memcpy(sa->args, ap, nitems(sa->args) * sizeof(register_t)); 1653911ee2cSEd Maste 1663911ee2cSEd Maste td->td_retval[0] = 0; 1673911ee2cSEd Maste return (0); 1683911ee2cSEd Maste } 1693911ee2cSEd Maste 1703911ee2cSEd Maste static void 1713911ee2cSEd Maste linux_set_syscall_retval(struct thread *td, int error) 1723911ee2cSEd Maste { 1733911ee2cSEd Maste 1748e5d76e6SAndrew Turner td->td_retval[1] = td->td_frame->tf_x[1]; 1758e5d76e6SAndrew Turner cpu_set_syscall_retval(td, error); 176c26391f4SEdward Tomasz Napierala 177c26391f4SEdward Tomasz Napierala if (__predict_false(error != 0)) { 178866b1f51SEdward Tomasz Napierala if (error != ERESTART && error != EJUSTRETURN) 179866b1f51SEdward Tomasz Napierala td->td_frame->tf_x[0] = bsd_to_linux_errno(error); 180c26391f4SEdward Tomasz Napierala } 1813911ee2cSEd Maste } 1823911ee2cSEd Maste 18303b0d68cSJohn Baldwin static int 184d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 1853911ee2cSEd Maste { 1863911ee2cSEd Maste Elf_Auxargs *args; 1873911ee2cSEd Maste Elf_Auxinfo *argarray, *pos; 1883911ee2cSEd Maste struct proc *p; 18903b0d68cSJohn Baldwin int error, issetugid; 1903911ee2cSEd Maste 1915caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo); 1923911ee2cSEd Maste p = imgp->proc; 1933911ee2cSEd Maste 1943911ee2cSEd Maste args = (Elf64_Auxargs *)imgp->auxargs; 1953911ee2cSEd Maste argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 1963911ee2cSEd Maste M_WAITOK | M_ZERO); 1973911ee2cSEd Maste 1983911ee2cSEd Maste issetugid = p->p_flag & P_SUGID ? 1 : 0; 1999931033bSDmitry Chagin AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 200*390c9ea0SDmitry Chagin AUXARGS_ENTRY(pos, LINUX_AT_MINSIGSTKSZ, LINUX_MINSIGSTKSZ); 201b501b2aeSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap); 202aa462cabSEdward Tomasz Napierala AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 2033911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 2043911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 2053911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 2063911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 2073911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_BASE, args->base); 2083911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 2093911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 2103911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 2113911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 2123911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 2133911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 2143911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 215b24e6ac8SBrooks Davis AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 216b501b2aeSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2); 2173911ee2cSEd Maste if (imgp->execpathp != 0) 218b24e6ac8SBrooks Davis AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 2193911ee2cSEd Maste if (args->execfd != -1) 2203911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 221aa462cabSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 2223911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_NULL, 0); 223aa462cabSEdward Tomasz Napierala 2243911ee2cSEd Maste free(imgp->auxargs, M_TEMP); 2253911ee2cSEd Maste imgp->auxargs = NULL; 2263911ee2cSEd Maste KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 2273911ee2cSEd Maste 228d8010b11SJohn Baldwin error = copyout(argarray, (void *)base, 229d8010b11SJohn Baldwin sizeof(*argarray) * LINUX_AT_COUNT); 2303911ee2cSEd Maste free(argarray, M_TEMP); 23103b0d68cSJohn Baldwin return (error); 2325caa67faSJohn Baldwin } 2335caa67faSJohn Baldwin 2345caa67faSJohn Baldwin static int 23531174518SJohn Baldwin linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp) 2365caa67faSJohn Baldwin { 2375caa67faSJohn Baldwin 2385caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo); 2393911ee2cSEd Maste 2403911ee2cSEd Maste return (0); 2413911ee2cSEd Maste } 2423911ee2cSEd Maste 2433911ee2cSEd Maste /* 2443911ee2cSEd Maste * Copy strings out to the new process address space, constructing new arg 2453911ee2cSEd Maste * and env vector tables. Return a pointer to the base so that it can be used 2463911ee2cSEd Maste * as the initial stack pointer. 2473911ee2cSEd Maste * LINUXTODO: deduplicate against other linuxulator archs 2483911ee2cSEd Maste */ 24903b0d68cSJohn Baldwin static int 25031174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 2513911ee2cSEd Maste { 2523911ee2cSEd Maste char **vectp; 25331174518SJohn Baldwin char *stringp; 2540386b6c8SLi-Wen Hsu uintptr_t destp, ustringp; 2553911ee2cSEd Maste struct ps_strings *arginfo; 2563911ee2cSEd Maste char canary[LINUX_AT_RANDOM_LEN]; 2573911ee2cSEd Maste size_t execpath_len; 2583911ee2cSEd Maste struct proc *p; 25903b0d68cSJohn Baldwin int argc, envc, error; 2603911ee2cSEd Maste 2613911ee2cSEd Maste p = imgp->proc; 262706f4a81SMark Johnston arginfo = (struct ps_strings *)PROC_PS_STRINGS(p); 26331174518SJohn Baldwin destp = (uintptr_t)arginfo; 2643911ee2cSEd Maste 265f04a0960SMark Johnston if (imgp->execpath != NULL && imgp->auxargs != NULL) { 266f04a0960SMark Johnston execpath_len = strlen(imgp->execpath) + 1; 26731174518SJohn Baldwin destp -= execpath_len; 26831174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 269b24e6ac8SBrooks Davis imgp->execpathp = (void *)destp; 270b24e6ac8SBrooks Davis error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 27103b0d68cSJohn Baldwin if (error != 0) 27203b0d68cSJohn Baldwin return (error); 2733911ee2cSEd Maste } 2743911ee2cSEd Maste 2753911ee2cSEd Maste /* Prepare the canary for SSP. */ 2763911ee2cSEd Maste arc4rand(canary, sizeof(canary), 0); 27731174518SJohn Baldwin destp -= roundup(sizeof(canary), sizeof(void *)); 278b24e6ac8SBrooks Davis imgp->canary = (void *)destp; 279b24e6ac8SBrooks Davis error = copyout(canary, imgp->canary, sizeof(canary)); 28003b0d68cSJohn Baldwin if (error != 0) 28103b0d68cSJohn Baldwin return (error); 2823911ee2cSEd Maste 28331174518SJohn Baldwin /* Allocate room for the argument and environment strings. */ 28431174518SJohn Baldwin destp -= ARG_MAX - imgp->args->stringspace; 28531174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 28631174518SJohn Baldwin ustringp = destp; 28731174518SJohn Baldwin 28803b0d68cSJohn Baldwin if (imgp->auxargs) { 289d8010b11SJohn Baldwin /* 290d8010b11SJohn Baldwin * Allocate room on the stack for the ELF auxargs 291d8010b11SJohn Baldwin * array. It has up to LINUX_AT_COUNT entries. 292d8010b11SJohn Baldwin */ 293d8010b11SJohn Baldwin destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo); 294d8010b11SJohn Baldwin destp = rounddown2(destp, sizeof(void *)); 29503b0d68cSJohn Baldwin } 2963911ee2cSEd Maste 29731174518SJohn Baldwin vectp = (char **)destp; 29831174518SJohn Baldwin 2993911ee2cSEd Maste /* 3003911ee2cSEd Maste * Allocate room for argc and the argv[] and env vectors including the 3013911ee2cSEd Maste * terminating NULL pointers. 3023911ee2cSEd Maste */ 3033911ee2cSEd Maste vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1; 3043911ee2cSEd Maste vectp = (char **)STACKALIGN(vectp); 3053911ee2cSEd Maste 3063911ee2cSEd Maste /* vectp also becomes our initial stack base. */ 30731174518SJohn Baldwin *stack_base = (uintptr_t)vectp; 3083911ee2cSEd Maste 3093911ee2cSEd Maste stringp = imgp->args->begin_argv; 3103911ee2cSEd Maste argc = imgp->args->argc; 3113911ee2cSEd Maste envc = imgp->args->envc; 3123911ee2cSEd Maste 3133911ee2cSEd Maste /* Copy out strings - arguments and environment. */ 31431174518SJohn Baldwin error = copyout(stringp, (void *)ustringp, 31531174518SJohn Baldwin ARG_MAX - imgp->args->stringspace); 31603b0d68cSJohn Baldwin if (error != 0) 31703b0d68cSJohn Baldwin return (error); 3183911ee2cSEd Maste 3193911ee2cSEd Maste /* Fill in "ps_strings" struct for ps, w, etc. */ 32003b0d68cSJohn Baldwin if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 32103b0d68cSJohn Baldwin suword(&arginfo->ps_nargvstr, argc) != 0) 32203b0d68cSJohn Baldwin return (EFAULT); 3233911ee2cSEd Maste 32403b0d68cSJohn Baldwin if (suword(vectp++, argc) != 0) 32503b0d68cSJohn Baldwin return (EFAULT); 32603b0d68cSJohn Baldwin 3273911ee2cSEd Maste /* Fill in argument portion of vector table. */ 3283911ee2cSEd Maste for (; argc > 0; --argc) { 32931174518SJohn Baldwin if (suword(vectp++, ustringp) != 0) 33003b0d68cSJohn Baldwin return (EFAULT); 3313911ee2cSEd Maste while (*stringp++ != 0) 33231174518SJohn Baldwin ustringp++; 33331174518SJohn Baldwin ustringp++; 3343911ee2cSEd Maste } 3353911ee2cSEd Maste 3363911ee2cSEd Maste /* A null vector table pointer separates the argp's from the envp's. */ 33703b0d68cSJohn Baldwin if (suword(vectp++, 0) != 0) 33803b0d68cSJohn Baldwin return (EFAULT); 3393911ee2cSEd Maste 34003b0d68cSJohn Baldwin if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 34103b0d68cSJohn Baldwin suword(&arginfo->ps_nenvstr, envc) != 0) 34203b0d68cSJohn Baldwin return (EFAULT); 3433911ee2cSEd Maste 3443911ee2cSEd Maste /* Fill in environment portion of vector table. */ 3453911ee2cSEd Maste for (; envc > 0; --envc) { 34631174518SJohn Baldwin if (suword(vectp++, ustringp) != 0) 34703b0d68cSJohn Baldwin return (EFAULT); 3483911ee2cSEd Maste while (*stringp++ != 0) 34931174518SJohn Baldwin ustringp++; 35031174518SJohn Baldwin ustringp++; 3513911ee2cSEd Maste } 3523911ee2cSEd Maste 3533911ee2cSEd Maste /* The end of the vector table is a null pointer. */ 35403b0d68cSJohn Baldwin if (suword(vectp, 0) != 0) 35503b0d68cSJohn Baldwin return (EFAULT); 35603b0d68cSJohn Baldwin 357d8010b11SJohn Baldwin if (imgp->auxargs) { 358d8010b11SJohn Baldwin vectp++; 359d8010b11SJohn Baldwin error = imgp->sysent->sv_copyout_auxargs(imgp, 360d8010b11SJohn Baldwin (uintptr_t)vectp); 361d8010b11SJohn Baldwin if (error != 0) 362d8010b11SJohn Baldwin return (error); 363d8010b11SJohn Baldwin } 364d8010b11SJohn Baldwin 36503b0d68cSJohn Baldwin return (0); 3663911ee2cSEd Maste } 3673911ee2cSEd Maste 3683911ee2cSEd Maste /* 3693911ee2cSEd Maste * Reset registers to default values on exec. 3703911ee2cSEd Maste */ 3713911ee2cSEd Maste static void 37231174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp, 37331174518SJohn Baldwin uintptr_t stack) 3743911ee2cSEd Maste { 3753911ee2cSEd Maste struct trapframe *regs = td->td_frame; 376a2a8b582SMitchell Horne struct pcb *pcb = td->td_pcb; 3773911ee2cSEd Maste 3783911ee2cSEd Maste /* LINUXTODO: validate */ 3793911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo); 3803911ee2cSEd Maste 3813911ee2cSEd Maste memset(regs, 0, sizeof(*regs)); 3823911ee2cSEd Maste /* glibc start.S registers function pointer in x0 with atexit. */ 3833911ee2cSEd Maste regs->tf_sp = stack; 3843911ee2cSEd Maste #if 0 /* LINUXTODO: See if this is used. */ 3853911ee2cSEd Maste regs->tf_lr = imgp->entry_addr; 3863911ee2cSEd Maste #else 3873911ee2cSEd Maste regs->tf_lr = 0xffffffffffffffff; 3883911ee2cSEd Maste #endif 3893911ee2cSEd Maste regs->tf_elr = imgp->entry_addr; 390953a7d7cSAlex Richardson 391a2a8b582SMitchell Horne pcb->pcb_tpidr_el0 = 0; 392a2a8b582SMitchell Horne pcb->pcb_tpidrro_el0 = 0; 3930723b409SJohn Baldwin WRITE_SPECIALREG(tpidrro_el0, 0); 3940723b409SJohn Baldwin WRITE_SPECIALREG(tpidr_el0, 0); 3950723b409SJohn Baldwin 396953a7d7cSAlex Richardson #ifdef VFP 397a2a8b582SMitchell Horne vfp_reset_state(td, pcb); 398953a7d7cSAlex Richardson #endif 399a2a8b582SMitchell Horne 400a2a8b582SMitchell Horne /* 401a2a8b582SMitchell Horne * Clear debug register state. It is not applicable to the new process. 402a2a8b582SMitchell Horne */ 403a2a8b582SMitchell Horne bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs)); 4043911ee2cSEd Maste } 4053911ee2cSEd Maste 4063911ee2cSEd Maste int 4073911ee2cSEd Maste linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 4083911ee2cSEd Maste { 409c56480a8SDmitry Chagin struct l_sigframe *frame; 410c56480a8SDmitry Chagin ucontext_t uc; 411ccc510b4SEdward Tomasz Napierala struct trapframe *tf; 412ccc510b4SEdward Tomasz Napierala int error; 4133911ee2cSEd Maste 414ccc510b4SEdward Tomasz Napierala tf = td->td_frame; 415c56480a8SDmitry Chagin frame = (struct l_sigframe *)tf->tf_sp; 416ccc510b4SEdward Tomasz Napierala 417c56480a8SDmitry Chagin if (copyin((void *)&frame->uc, &uc, sizeof(uc))) 418ccc510b4SEdward Tomasz Napierala return (EFAULT); 419ccc510b4SEdward Tomasz Napierala 420c56480a8SDmitry Chagin error = set_mcontext(td, &uc.uc_mcontext); 421ccc510b4SEdward Tomasz Napierala if (error != 0) 422ccc510b4SEdward Tomasz Napierala return (error); 423ccc510b4SEdward Tomasz Napierala 424ccc510b4SEdward Tomasz Napierala /* Restore signal mask. */ 425c56480a8SDmitry Chagin kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0); 426ccc510b4SEdward Tomasz Napierala 427ccc510b4SEdward Tomasz Napierala return (EJUSTRETURN); 4283911ee2cSEd Maste } 4293911ee2cSEd Maste 4303911ee2cSEd Maste static void 4313911ee2cSEd Maste linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 4323911ee2cSEd Maste { 433ccc510b4SEdward Tomasz Napierala struct thread *td; 434ccc510b4SEdward Tomasz Napierala struct proc *p; 435ccc510b4SEdward Tomasz Napierala struct trapframe *tf; 436c56480a8SDmitry Chagin struct l_sigframe *fp, *frame; 437c56480a8SDmitry Chagin struct l_fpsimd_context *fpsimd; 438c56480a8SDmitry Chagin struct l_esr_context *esr; 439c56480a8SDmitry Chagin l_stack_t uc_stack; 440c56480a8SDmitry Chagin ucontext_t uc; 441c56480a8SDmitry Chagin uint8_t *scr; 442ccc510b4SEdward Tomasz Napierala struct sigacts *psp; 443ccc510b4SEdward Tomasz Napierala int onstack, sig; 4443911ee2cSEd Maste 445ccc510b4SEdward Tomasz Napierala td = curthread; 446ccc510b4SEdward Tomasz Napierala p = td->td_proc; 447ccc510b4SEdward Tomasz Napierala PROC_LOCK_ASSERT(p, MA_OWNED); 448ccc510b4SEdward Tomasz Napierala 449ccc510b4SEdward Tomasz Napierala sig = ksi->ksi_signo; 450ccc510b4SEdward Tomasz Napierala psp = p->p_sigacts; 451ccc510b4SEdward Tomasz Napierala mtx_assert(&psp->ps_mtx, MA_OWNED); 452ccc510b4SEdward Tomasz Napierala 453ccc510b4SEdward Tomasz Napierala tf = td->td_frame; 454ccc510b4SEdward Tomasz Napierala onstack = sigonstack(tf->tf_sp); 455ccc510b4SEdward Tomasz Napierala 456ccc510b4SEdward Tomasz Napierala CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm, 457ccc510b4SEdward Tomasz Napierala catcher, sig); 458ccc510b4SEdward Tomasz Napierala 459ccc510b4SEdward Tomasz Napierala /* Allocate and validate space for the signal handler context. */ 460ccc510b4SEdward Tomasz Napierala if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack && 461ccc510b4SEdward Tomasz Napierala SIGISMEMBER(psp->ps_sigonstack, sig)) { 462ccc510b4SEdward Tomasz Napierala fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 463ccc510b4SEdward Tomasz Napierala td->td_sigstk.ss_size); 464ccc510b4SEdward Tomasz Napierala #if defined(COMPAT_43) 465ccc510b4SEdward Tomasz Napierala td->td_sigstk.ss_flags |= SS_ONSTACK; 466ccc510b4SEdward Tomasz Napierala #endif 467ccc510b4SEdward Tomasz Napierala } else { 468ccc510b4SEdward Tomasz Napierala fp = (struct l_sigframe *)td->td_frame->tf_sp; 469ccc510b4SEdward Tomasz Napierala } 470ccc510b4SEdward Tomasz Napierala 471ccc510b4SEdward Tomasz Napierala /* Make room, keeping the stack aligned */ 472ccc510b4SEdward Tomasz Napierala fp--; 473ccc510b4SEdward Tomasz Napierala fp = (struct l_sigframe *)STACKALIGN(fp); 474ccc510b4SEdward Tomasz Napierala 475c56480a8SDmitry Chagin get_mcontext(td, &uc.uc_mcontext, 0); 476c56480a8SDmitry Chagin uc.uc_sigmask = *mask; 477ccc510b4SEdward Tomasz Napierala 478c56480a8SDmitry Chagin uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 479c56480a8SDmitry Chagin uc_stack.ss_size = td->td_sigstk.ss_size; 480c56480a8SDmitry Chagin uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ? 481c56480a8SDmitry Chagin (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 482ccc510b4SEdward Tomasz Napierala mtx_unlock(&psp->ps_mtx); 483ccc510b4SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 484ccc510b4SEdward Tomasz Napierala 485c56480a8SDmitry Chagin /* Fill in the frame to copy out */ 486c56480a8SDmitry Chagin frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO); 487c56480a8SDmitry Chagin 488c56480a8SDmitry Chagin memcpy(&frame->sf.sf_uc.uc_sc.regs, tf->tf_x, sizeof(tf->tf_x)); 489c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.regs[30] = tf->tf_lr; 490c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.sp = tf->tf_sp; 491c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.pc = tf->tf_lr; 492c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.pstate = tf->tf_spsr; 493c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.fault_address = (register_t)ksi->ksi_addr; 494c56480a8SDmitry Chagin 495c56480a8SDmitry Chagin /* Stack frame for unwinding */ 496c56480a8SDmitry Chagin frame->fp = tf->tf_x[29]; 497c56480a8SDmitry Chagin frame->lr = tf->tf_lr; 498c56480a8SDmitry Chagin 499c56480a8SDmitry Chagin /* Translate the signal. */ 500c56480a8SDmitry Chagin sig = bsd_to_linux_signal(sig); 501c56480a8SDmitry Chagin siginfo_to_lsiginfo(&ksi->ksi_info, &frame->sf.sf_si, sig); 502c56480a8SDmitry Chagin bsd_to_linux_sigset(mask, &frame->sf.sf_uc.uc_sigmask); 503c56480a8SDmitry Chagin 504c56480a8SDmitry Chagin /* 505c56480a8SDmitry Chagin * Prepare fpsimd & esr. Does not check sizes, as 506c56480a8SDmitry Chagin * __reserved is big enougth. 507c56480a8SDmitry Chagin */ 508c56480a8SDmitry Chagin scr = (uint8_t *)&frame->sf.sf_uc.uc_sc.__reserved; 509c56480a8SDmitry Chagin #ifdef VFP 510c56480a8SDmitry Chagin fpsimd = (struct l_fpsimd_context *) scr; 511c56480a8SDmitry Chagin fpsimd->head.magic = L_FPSIMD_MAGIC; 512c56480a8SDmitry Chagin fpsimd->head.size = sizeof(struct l_fpsimd_context); 513c56480a8SDmitry Chagin fpsimd->fpsr = uc.uc_mcontext.mc_fpregs.fp_sr; 514c56480a8SDmitry Chagin fpsimd->fpcr = uc.uc_mcontext.mc_fpregs.fp_cr; 515c56480a8SDmitry Chagin 516c56480a8SDmitry Chagin memcpy(fpsimd->vregs, &uc.uc_mcontext.mc_fpregs.fp_q, 517c56480a8SDmitry Chagin sizeof(uc.uc_mcontext.mc_fpregs.fp_q)); 518c56480a8SDmitry Chagin scr += roundup(sizeof(struct l_fpsimd_context), 16); 519c56480a8SDmitry Chagin #endif 520c56480a8SDmitry Chagin if (ksi->ksi_addr != 0) { 521c56480a8SDmitry Chagin esr = (struct l_esr_context *) scr; 522c56480a8SDmitry Chagin esr->head.magic = L_ESR_MAGIC; 523c56480a8SDmitry Chagin esr->head.size = sizeof(struct l_esr_context); 524c56480a8SDmitry Chagin esr->esr = tf->tf_esr; 525c56480a8SDmitry Chagin } 526c56480a8SDmitry Chagin 527c56480a8SDmitry Chagin memcpy(&frame->sf.sf_uc.uc_stack, &uc_stack, sizeof(uc_stack)); 528c56480a8SDmitry Chagin memcpy(&frame->uc, &uc, sizeof(uc)); 529c56480a8SDmitry Chagin 530ccc510b4SEdward Tomasz Napierala /* Copy the sigframe out to the user's stack. */ 531c56480a8SDmitry Chagin if (copyout(frame, fp, sizeof(*fp)) != 0) { 532ccc510b4SEdward Tomasz Napierala /* Process has trashed its stack. Kill it. */ 533c56480a8SDmitry Chagin free(frame, M_LINUX); 534ccc510b4SEdward Tomasz Napierala CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp); 535ccc510b4SEdward Tomasz Napierala PROC_LOCK(p); 536ccc510b4SEdward Tomasz Napierala sigexit(td, SIGILL); 537ccc510b4SEdward Tomasz Napierala } 538c56480a8SDmitry Chagin free(frame, M_LINUX); 539ccc510b4SEdward Tomasz Napierala 540ccc510b4SEdward Tomasz Napierala tf->tf_x[0]= sig; 541c56480a8SDmitry Chagin tf->tf_x[1] = (register_t)&fp->sf.sf_si; 542c56480a8SDmitry Chagin tf->tf_x[2] = (register_t)&fp->sf.sf_uc; 543c56480a8SDmitry Chagin tf->tf_x[8] = (register_t)catcher; 544ccc510b4SEdward Tomasz Napierala tf->tf_sp = (register_t)fp; 545c56480a8SDmitry Chagin tf->tf_elr = (register_t)linux_vdso_sigcode; 546ccc510b4SEdward Tomasz Napierala 547ccc510b4SEdward Tomasz Napierala CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr, 548ccc510b4SEdward Tomasz Napierala tf->tf_sp); 549ccc510b4SEdward Tomasz Napierala 550ccc510b4SEdward Tomasz Napierala PROC_LOCK(p); 551ccc510b4SEdward Tomasz Napierala mtx_lock(&psp->ps_mtx); 5523911ee2cSEd Maste } 5533911ee2cSEd Maste 5543911ee2cSEd Maste struct sysentvec elf_linux_sysvec = { 5553911ee2cSEd Maste .sv_size = LINUX_SYS_MAXSYSCALL, 5563911ee2cSEd Maste .sv_table = linux_sysent, 5573911ee2cSEd Maste .sv_transtrap = linux_translate_traps, 5583911ee2cSEd Maste .sv_fixup = linux_elf_fixup, 5593911ee2cSEd Maste .sv_sendsig = linux_rt_sendsig, 5609931033bSDmitry Chagin .sv_sigcode = &_binary_linux_vdso_so_o_start, 5613911ee2cSEd Maste .sv_szsigcode = &linux_szsigcode, 5623911ee2cSEd Maste .sv_name = "Linux ELF64", 5633911ee2cSEd Maste .sv_coredump = elf64_coredump, 564435754a5SEdward Tomasz Napierala .sv_elf_core_osabi = ELFOSABI_NONE, 56545d99014SEdward Tomasz Napierala .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 56645d99014SEdward Tomasz Napierala .sv_elf_core_prepare_notes = linux64_prepare_notes, 5673911ee2cSEd Maste .sv_imgact_try = linux_exec_imgact_try, 5683911ee2cSEd Maste .sv_minsigstksz = LINUX_MINSIGSTKSZ, 5693911ee2cSEd Maste .sv_minuser = VM_MIN_ADDRESS, 5703911ee2cSEd Maste .sv_maxuser = VM_MAXUSER_ADDRESS, 5719931033bSDmitry Chagin .sv_usrstack = LINUX_USRSTACK, 5729931033bSDmitry Chagin .sv_psstrings = LINUX_PS_STRINGS, 5733fc21fddSMark Johnston .sv_psstringssz = sizeof(struct ps_strings), 574d4f55cc8SEd Maste .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 5755caa67faSJohn Baldwin .sv_copyout_auxargs = linux_copyout_auxargs, 5763911ee2cSEd Maste .sv_copyout_strings = linux_copyout_strings, 5773911ee2cSEd Maste .sv_setregs = linux_exec_setregs, 5783911ee2cSEd Maste .sv_fixlimit = NULL, 5793911ee2cSEd Maste .sv_maxssiz = NULL, 580870e197dSKonstantin Belousov .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 5819931033bSDmitry Chagin SV_SIG_WAITNDQ | SV_TIMEKEEP, 5823911ee2cSEd Maste .sv_set_syscall_retval = linux_set_syscall_retval, 5833911ee2cSEd Maste .sv_fetch_syscall_args = linux_fetch_syscall_args, 5843911ee2cSEd Maste .sv_syscallnames = NULL, 5859931033bSDmitry Chagin .sv_shared_page_base = LINUX_SHAREDPAGE, 5863911ee2cSEd Maste .sv_shared_page_len = PAGE_SIZE, 5873911ee2cSEd Maste .sv_schedtail = linux_schedtail, 5883911ee2cSEd Maste .sv_thread_detach = linux_thread_detach, 58984a3963dSEdward Tomasz Napierala .sv_trap = NULL, 590b501b2aeSEdward Tomasz Napierala .sv_hwcap = &elf_hwcap, 591b501b2aeSEdward Tomasz Napierala .sv_hwcap2 = &elf_hwcap2, 5925fd9cd53SDmitry Chagin .sv_onexec = linux_on_exec_vmspace, 5934815f175SKonstantin Belousov .sv_onexit = linux_on_exit, 5944815f175SKonstantin Belousov .sv_ontdexit = linux_thread_dtor, 595598f6fb4SKonstantin Belousov .sv_setid_allowed = &linux_setid_allowed_query, 5963911ee2cSEd Maste }; 5973911ee2cSEd Maste 5985fd9cd53SDmitry Chagin static int 5995fd9cd53SDmitry Chagin linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 6005fd9cd53SDmitry Chagin { 6019931033bSDmitry Chagin int error; 6025fd9cd53SDmitry Chagin 6039931033bSDmitry Chagin error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base, 6049931033bSDmitry Chagin LINUX_VDSOPAGE_SIZE, imgp); 6059931033bSDmitry Chagin if (error == 0) 6065fd9cd53SDmitry Chagin linux_on_exec(p, imgp); 6079931033bSDmitry Chagin return (error); 6085fd9cd53SDmitry Chagin } 6095fd9cd53SDmitry Chagin 61009cffde9SDmitry Chagin /* 61109cffde9SDmitry Chagin * linux_vdso_install() and linux_exec_sysvec_init() must be called 61209cffde9SDmitry Chagin * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 61309cffde9SDmitry Chagin */ 6143911ee2cSEd Maste static void 6159931033bSDmitry Chagin linux_exec_sysvec_init(void *param) 6169931033bSDmitry Chagin { 6179931033bSDmitry Chagin l_uintptr_t *ktimekeep_base; 6189931033bSDmitry Chagin struct sysentvec *sv; 6199931033bSDmitry Chagin ptrdiff_t tkoff; 6209931033bSDmitry Chagin 6219931033bSDmitry Chagin sv = param; 6229931033bSDmitry Chagin /* Fill timekeep_base */ 6239931033bSDmitry Chagin exec_sysvec_init(sv); 6249931033bSDmitry Chagin 6259931033bSDmitry Chagin tkoff = kern_timekeep_base - linux_vdso_base; 6269931033bSDmitry Chagin ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 6279931033bSDmitry Chagin *ktimekeep_base = sv->sv_timekeep_base; 6289931033bSDmitry Chagin } 62909cffde9SDmitry Chagin SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 6309931033bSDmitry Chagin linux_exec_sysvec_init, &elf_linux_sysvec); 6319931033bSDmitry Chagin 6329931033bSDmitry Chagin static void 6333911ee2cSEd Maste linux_vdso_install(const void *param) 6343911ee2cSEd Maste { 6359931033bSDmitry Chagin char *vdso_start = &_binary_linux_vdso_so_o_start; 6369931033bSDmitry Chagin char *vdso_end = &_binary_linux_vdso_so_o_end; 6373911ee2cSEd Maste 6389931033bSDmitry Chagin linux_szsigcode = vdso_end - vdso_start; 6399931033bSDmitry Chagin MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 6403911ee2cSEd Maste 6419931033bSDmitry Chagin linux_vdso_base = LINUX_VDSOPAGE; 6423911ee2cSEd Maste 6439931033bSDmitry Chagin __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 6443911ee2cSEd Maste 6459931033bSDmitry Chagin linux_vdso_obj = __elfN(linux_shared_page_init) 6469931033bSDmitry Chagin (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 6479931033bSDmitry Chagin bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 6483911ee2cSEd Maste 6499931033bSDmitry Chagin linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 6503911ee2cSEd Maste } 65109cffde9SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 6523911ee2cSEd Maste linux_vdso_install, NULL); 6533911ee2cSEd Maste 6543911ee2cSEd Maste static void 6553911ee2cSEd Maste linux_vdso_deinstall(const void *param) 6563911ee2cSEd Maste { 6573911ee2cSEd Maste 6589931033bSDmitry Chagin __elfN(linux_shared_page_fini)(linux_vdso_obj, 6599931033bSDmitry Chagin linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 6603911ee2cSEd Maste } 6613911ee2cSEd Maste SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 6623911ee2cSEd Maste linux_vdso_deinstall, NULL); 6633911ee2cSEd Maste 6649931033bSDmitry Chagin static void 6659931033bSDmitry Chagin linux_vdso_reloc(char *mapping, Elf_Addr offset) 6669931033bSDmitry Chagin { 6679931033bSDmitry Chagin Elf_Size rtype, symidx; 6689931033bSDmitry Chagin const Elf_Rela *rela; 6699931033bSDmitry Chagin const Elf_Shdr *shdr; 6709931033bSDmitry Chagin const Elf_Ehdr *ehdr; 6719931033bSDmitry Chagin Elf_Addr *where; 6729931033bSDmitry Chagin Elf_Addr addr, addend; 6739931033bSDmitry Chagin int i, relacnt; 6749931033bSDmitry Chagin 6759931033bSDmitry Chagin MPASS(offset != 0); 6769931033bSDmitry Chagin 6779931033bSDmitry Chagin relacnt = 0; 6789931033bSDmitry Chagin ehdr = (const Elf_Ehdr *)mapping; 6799931033bSDmitry Chagin shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 6809931033bSDmitry Chagin for (i = 0; i < ehdr->e_shnum; i++) 6819931033bSDmitry Chagin { 6829931033bSDmitry Chagin switch (shdr[i].sh_type) { 6839931033bSDmitry Chagin case SHT_REL: 6849931033bSDmitry Chagin printf("Linux Aarch64 vDSO: unexpected Rel section\n"); 6859931033bSDmitry Chagin break; 6869931033bSDmitry Chagin case SHT_RELA: 6879931033bSDmitry Chagin rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset); 6889931033bSDmitry Chagin relacnt = shdr[i].sh_size / sizeof(*rela); 6899931033bSDmitry Chagin } 6909931033bSDmitry Chagin } 6919931033bSDmitry Chagin 6929931033bSDmitry Chagin for (i = 0; i < relacnt; i++, rela++) { 6939931033bSDmitry Chagin where = (Elf_Addr *)(mapping + rela->r_offset); 6949931033bSDmitry Chagin addend = rela->r_addend; 6959931033bSDmitry Chagin rtype = ELF_R_TYPE(rela->r_info); 6969931033bSDmitry Chagin symidx = ELF_R_SYM(rela->r_info); 6979931033bSDmitry Chagin 6989931033bSDmitry Chagin switch (rtype) { 6999931033bSDmitry Chagin case R_AARCH64_NONE: /* none */ 7009931033bSDmitry Chagin break; 7019931033bSDmitry Chagin 7029931033bSDmitry Chagin case R_AARCH64_RELATIVE: /* B + A */ 7039931033bSDmitry Chagin addr = (Elf_Addr)(mapping + addend); 7049931033bSDmitry Chagin if (*where != addr) 7059931033bSDmitry Chagin *where = addr; 7069931033bSDmitry Chagin break; 7079931033bSDmitry Chagin default: 7089931033bSDmitry Chagin printf("Linux Aarch64 vDSO: unexpected relocation type %ld, " 7099931033bSDmitry Chagin "symbol index %ld\n", rtype, symidx); 7109931033bSDmitry Chagin } 7119931033bSDmitry Chagin } 7129931033bSDmitry Chagin } 7139931033bSDmitry Chagin 7143911ee2cSEd Maste static char GNU_ABI_VENDOR[] = "GNU"; 7153911ee2cSEd Maste static int GNU_ABI_LINUX = 0; 7163911ee2cSEd Maste 7173911ee2cSEd Maste /* LINUXTODO: deduplicate */ 7183911ee2cSEd Maste static bool 7193911ee2cSEd Maste linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 7203911ee2cSEd Maste { 7213911ee2cSEd Maste const Elf32_Word *desc; 7223911ee2cSEd Maste uintptr_t p; 7233911ee2cSEd Maste 7243911ee2cSEd Maste p = (uintptr_t)(note + 1); 7253911ee2cSEd Maste p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 7263911ee2cSEd Maste 7273911ee2cSEd Maste desc = (const Elf32_Word *)p; 7283911ee2cSEd Maste if (desc[0] != GNU_ABI_LINUX) 7293911ee2cSEd Maste return (false); 7303911ee2cSEd Maste 7313911ee2cSEd Maste *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 7323911ee2cSEd Maste return (true); 7333911ee2cSEd Maste } 7343911ee2cSEd Maste 7353911ee2cSEd Maste static Elf_Brandnote linux64_brandnote = { 7363911ee2cSEd Maste .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 7373911ee2cSEd Maste .hdr.n_descsz = 16, 7383911ee2cSEd Maste .hdr.n_type = 1, 7393911ee2cSEd Maste .vendor = GNU_ABI_VENDOR, 7403911ee2cSEd Maste .flags = BN_TRANSLATE_OSREL, 7413911ee2cSEd Maste .trans_osrel = linux_trans_osrel 7423911ee2cSEd Maste }; 7433911ee2cSEd Maste 7443911ee2cSEd Maste static Elf64_Brandinfo linux_glibc2brand = { 7453911ee2cSEd Maste .brand = ELFOSABI_LINUX, 7463911ee2cSEd Maste .machine = EM_AARCH64, 7473911ee2cSEd Maste .compat_3_brand = "Linux", 748b5f20658SEdward Tomasz Napierala .emul_path = linux_emul_path, 7493911ee2cSEd Maste .interp_path = "/lib64/ld-linux-x86-64.so.2", 7503911ee2cSEd Maste .sysvec = &elf_linux_sysvec, 7513911ee2cSEd Maste .interp_newpath = NULL, 7523911ee2cSEd Maste .brand_note = &linux64_brandnote, 7533911ee2cSEd Maste .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 7543911ee2cSEd Maste }; 7553911ee2cSEd Maste 7563911ee2cSEd Maste Elf64_Brandinfo *linux_brandlist[] = { 7573911ee2cSEd Maste &linux_glibc2brand, 7583911ee2cSEd Maste NULL 7593911ee2cSEd Maste }; 7603911ee2cSEd Maste 7613911ee2cSEd Maste static int 7623911ee2cSEd Maste linux64_elf_modevent(module_t mod, int type, void *data) 7633911ee2cSEd Maste { 7643911ee2cSEd Maste Elf64_Brandinfo **brandinfo; 7653911ee2cSEd Maste struct linux_ioctl_handler**lihp; 7663911ee2cSEd Maste int error; 7673911ee2cSEd Maste 7683911ee2cSEd Maste error = 0; 7693911ee2cSEd Maste switch(type) { 7703911ee2cSEd Maste case MOD_LOAD: 7713911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 7723911ee2cSEd Maste ++brandinfo) 7733911ee2cSEd Maste if (elf64_insert_brand_entry(*brandinfo) < 0) 7743911ee2cSEd Maste error = EINVAL; 7753911ee2cSEd Maste if (error == 0) { 7763911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set) 7773911ee2cSEd Maste linux_ioctl_register_handler(*lihp); 7783911ee2cSEd Maste stclohz = (stathz ? stathz : hz); 7793911ee2cSEd Maste if (bootverbose) 7803911ee2cSEd Maste printf("Linux arm64 ELF exec handler installed\n"); 7813911ee2cSEd Maste } 7823911ee2cSEd Maste break; 7833911ee2cSEd Maste case MOD_UNLOAD: 7843911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 7853911ee2cSEd Maste ++brandinfo) 7863911ee2cSEd Maste if (elf64_brand_inuse(*brandinfo)) 7873911ee2cSEd Maste error = EBUSY; 7883911ee2cSEd Maste if (error == 0) { 7893911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; 7903911ee2cSEd Maste *brandinfo != NULL; ++brandinfo) 7913911ee2cSEd Maste if (elf64_remove_brand_entry(*brandinfo) < 0) 7923911ee2cSEd Maste error = EINVAL; 7933911ee2cSEd Maste } 7943911ee2cSEd Maste if (error == 0) { 7953911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set) 7963911ee2cSEd Maste linux_ioctl_unregister_handler(*lihp); 7973911ee2cSEd Maste if (bootverbose) 798ae8330b4SDmitry Chagin printf("Linux arm64 ELF exec handler removed\n"); 7993911ee2cSEd Maste } else 800ae8330b4SDmitry Chagin printf("Could not deinstall Linux arm64 ELF interpreter entry\n"); 8013911ee2cSEd Maste break; 8023911ee2cSEd Maste default: 8033911ee2cSEd Maste return (EOPNOTSUPP); 8043911ee2cSEd Maste } 8053911ee2cSEd Maste return (error); 8063911ee2cSEd Maste } 8073911ee2cSEd Maste 8083911ee2cSEd Maste static moduledata_t linux64_elf_mod = { 8093911ee2cSEd Maste "linux64elf", 8103911ee2cSEd Maste linux64_elf_modevent, 8113911ee2cSEd Maste 0 8123911ee2cSEd Maste }; 8133911ee2cSEd Maste 8143911ee2cSEd Maste DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 8153911ee2cSEd Maste MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 8163911ee2cSEd Maste FEATURE(linux64, "AArch64 Linux 64bit support"); 817