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
482a454b54SMark Johnston #include <vm/vm.h>
492a454b54SMark Johnston #include <vm/pmap.h>
503911ee2cSEd Maste #include <vm/vm_param.h>
512a454b54SMark Johnston #include <vm/vm_map.h>
523911ee2cSEd Maste
533911ee2cSEd Maste #include <arm64/linux/linux.h>
543911ee2cSEd Maste #include <arm64/linux/linux_proto.h>
553911ee2cSEd Maste #include <compat/linux/linux_dtrace.h>
563911ee2cSEd Maste #include <compat/linux/linux_emul.h>
573911ee2cSEd Maste #include <compat/linux/linux_ioctl.h>
583911ee2cSEd Maste #include <compat/linux/linux_mib.h>
593911ee2cSEd Maste #include <compat/linux/linux_misc.h>
60b5f20658SEdward Tomasz Napierala #include <compat/linux/linux_util.h>
613911ee2cSEd Maste #include <compat/linux/linux_vdso.h>
623911ee2cSEd Maste
635d5616acSEdward Tomasz Napierala #include <machine/md_var.h>
645d5616acSEdward Tomasz Napierala
6555872d47SAlex Richardson #ifdef VFP
6655872d47SAlex Richardson #include <machine/vfp.h>
6755872d47SAlex Richardson #endif
6855872d47SAlex Richardson
693911ee2cSEd Maste MODULE_VERSION(linux64elf, 1);
703911ee2cSEd Maste
713911ee2cSEd Maste const char *linux_kplatform;
723911ee2cSEd Maste static int linux_szsigcode;
733911ee2cSEd Maste static vm_object_t linux_shared_page_obj;
743911ee2cSEd Maste static char *linux_shared_page_mapping;
753911ee2cSEd Maste extern char _binary_linux_locore_o_start;
763911ee2cSEd Maste extern char _binary_linux_locore_o_end;
773911ee2cSEd Maste
783911ee2cSEd Maste extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
793911ee2cSEd Maste
803911ee2cSEd Maste SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
813911ee2cSEd Maste
8203b0d68cSJohn Baldwin static int linux_copyout_strings(struct image_params *imgp,
8331174518SJohn Baldwin uintptr_t *stack_base);
8431174518SJohn Baldwin static int linux_elf_fixup(uintptr_t *stack_base,
853911ee2cSEd Maste struct image_params *iparams);
863911ee2cSEd Maste static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
873911ee2cSEd Maste static void linux_vdso_install(const void *param);
883911ee2cSEd Maste static void linux_vdso_deinstall(const void *param);
893911ee2cSEd Maste static void linux_set_syscall_retval(struct thread *td, int error);
903911ee2cSEd Maste static int linux_fetch_syscall_args(struct thread *td);
913911ee2cSEd Maste static void linux_exec_setregs(struct thread *td, struct image_params *imgp,
9231174518SJohn Baldwin uintptr_t stack);
933911ee2cSEd Maste
943911ee2cSEd Maste /* DTrace init */
953911ee2cSEd Maste LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
963911ee2cSEd Maste
973911ee2cSEd Maste /* DTrace probes */
983911ee2cSEd Maste LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int");
993911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo);
1005caa67faSJohn Baldwin LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo);
1013911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo);
1023911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo);
1033911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo);
1043911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo);
1053911ee2cSEd Maste LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo);
1063911ee2cSEd Maste
1073911ee2cSEd Maste /* LINUXTODO: do we have traps to translate? */
1083911ee2cSEd Maste static int
linux_translate_traps(int signal,int trap_code)1093911ee2cSEd Maste linux_translate_traps(int signal, int trap_code)
1103911ee2cSEd Maste {
1113911ee2cSEd Maste
1123911ee2cSEd Maste LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code);
1133911ee2cSEd Maste return (signal);
1143911ee2cSEd Maste }
1153911ee2cSEd Maste
1163911ee2cSEd Maste LINUX_VDSO_SYM_CHAR(linux_platform);
1173911ee2cSEd Maste
1183911ee2cSEd Maste static int
linux_fetch_syscall_args(struct thread * td)1193911ee2cSEd Maste linux_fetch_syscall_args(struct thread *td)
1203911ee2cSEd Maste {
1213911ee2cSEd Maste struct proc *p;
1223911ee2cSEd Maste struct syscall_args *sa;
1233911ee2cSEd Maste register_t *ap;
1243911ee2cSEd Maste
1253911ee2cSEd Maste p = td->td_proc;
1263911ee2cSEd Maste ap = td->td_frame->tf_x;
1273911ee2cSEd Maste sa = &td->td_sa;
1283911ee2cSEd Maste
1293911ee2cSEd Maste sa->code = td->td_frame->tf_x[8];
1303911ee2cSEd Maste /* LINUXTODO: generic syscall? */
1313911ee2cSEd Maste if (sa->code >= p->p_sysent->sv_size)
1323911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[0];
1333911ee2cSEd Maste else
1343911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[sa->code];
1353911ee2cSEd Maste
1361e2521ffSEdward Tomasz Napierala if (sa->callp->sy_narg > MAXARGS)
1371e2521ffSEdward Tomasz Napierala panic("ARM64TODO: Could we have more than %d args?", MAXARGS);
1381e2521ffSEdward Tomasz Napierala memcpy(sa->args, ap, MAXARGS * sizeof(register_t));
1393911ee2cSEd Maste
1403911ee2cSEd Maste td->td_retval[0] = 0;
1413911ee2cSEd Maste return (0);
1423911ee2cSEd Maste }
1433911ee2cSEd Maste
1443911ee2cSEd Maste static void
linux_set_syscall_retval(struct thread * td,int error)1453911ee2cSEd Maste linux_set_syscall_retval(struct thread *td, int error)
1463911ee2cSEd Maste {
1473911ee2cSEd Maste
1488e5d76e6SAndrew Turner td->td_retval[1] = td->td_frame->tf_x[1];
1498e5d76e6SAndrew Turner cpu_set_syscall_retval(td, error);
150c26391f4SEdward Tomasz Napierala
151c26391f4SEdward Tomasz Napierala if (__predict_false(error != 0)) {
152866b1f51SEdward Tomasz Napierala if (error != ERESTART && error != EJUSTRETURN)
153866b1f51SEdward Tomasz Napierala td->td_frame->tf_x[0] = bsd_to_linux_errno(error);
154c26391f4SEdward Tomasz Napierala }
1553911ee2cSEd Maste }
1563911ee2cSEd Maste
15703b0d68cSJohn Baldwin static int
linux_copyout_auxargs(struct image_params * imgp,uintptr_t base)158d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
1593911ee2cSEd Maste {
1603911ee2cSEd Maste Elf_Auxargs *args;
1613911ee2cSEd Maste Elf_Auxinfo *argarray, *pos;
1623911ee2cSEd Maste struct proc *p;
16303b0d68cSJohn Baldwin int error, issetugid;
1643911ee2cSEd Maste
1655caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo);
1663911ee2cSEd Maste p = imgp->proc;
1673911ee2cSEd Maste
1683911ee2cSEd Maste args = (Elf64_Auxargs *)imgp->auxargs;
1693911ee2cSEd Maste argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
1703911ee2cSEd Maste M_WAITOK | M_ZERO);
1713911ee2cSEd Maste
1723911ee2cSEd Maste issetugid = p->p_flag & P_SUGID ? 1 : 0;
1733911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
1743911ee2cSEd Maste imgp->proc->p_sysent->sv_shared_page_base);
1755d5616acSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap);
176b36eaf0fSEdward Tomasz Napierala AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1773911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
1783911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1793911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1803911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1813911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_BASE, args->base);
1823911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1833911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1843911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
1853911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
1863911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
1873911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
1883911ee2cSEd Maste AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
189b24e6ac8SBrooks Davis AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
1905d5616acSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2);
1913911ee2cSEd Maste if (imgp->execpathp != 0)
192b24e6ac8SBrooks Davis AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
1933911ee2cSEd Maste if (args->execfd != -1)
1943911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
195b36eaf0fSEdward Tomasz Napierala AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
1963911ee2cSEd Maste AUXARGS_ENTRY(pos, AT_NULL, 0);
197b36eaf0fSEdward Tomasz Napierala
1983911ee2cSEd Maste free(imgp->auxargs, M_TEMP);
1993911ee2cSEd Maste imgp->auxargs = NULL;
2003911ee2cSEd Maste KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
2013911ee2cSEd Maste
202d8010b11SJohn Baldwin error = copyout(argarray, (void *)base,
203d8010b11SJohn Baldwin sizeof(*argarray) * LINUX_AT_COUNT);
2043911ee2cSEd Maste free(argarray, M_TEMP);
20503b0d68cSJohn Baldwin return (error);
2065caa67faSJohn Baldwin }
2075caa67faSJohn Baldwin
2085caa67faSJohn Baldwin static int
linux_elf_fixup(uintptr_t * stack_base,struct image_params * imgp)20931174518SJohn Baldwin linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp)
2105caa67faSJohn Baldwin {
2115caa67faSJohn Baldwin
2125caa67faSJohn Baldwin LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo);
2133911ee2cSEd Maste
2143911ee2cSEd Maste return (0);
2153911ee2cSEd Maste }
2163911ee2cSEd Maste
2173911ee2cSEd Maste /*
2183911ee2cSEd Maste * Copy strings out to the new process address space, constructing new arg
2193911ee2cSEd Maste * and env vector tables. Return a pointer to the base so that it can be used
2203911ee2cSEd Maste * as the initial stack pointer.
2213911ee2cSEd Maste * LINUXTODO: deduplicate against other linuxulator archs
2223911ee2cSEd Maste */
22303b0d68cSJohn Baldwin static int
linux_copyout_strings(struct image_params * imgp,uintptr_t * stack_base)22431174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
2253911ee2cSEd Maste {
2263911ee2cSEd Maste char **vectp;
22731174518SJohn Baldwin char *stringp;
2280386b6c8SLi-Wen Hsu uintptr_t destp, ustringp;
2293911ee2cSEd Maste struct ps_strings *arginfo;
2303911ee2cSEd Maste char canary[LINUX_AT_RANDOM_LEN];
2313911ee2cSEd Maste size_t execpath_len;
2323911ee2cSEd Maste struct proc *p;
23303b0d68cSJohn Baldwin int argc, envc, error;
2343911ee2cSEd Maste
2353911ee2cSEd Maste p = imgp->proc;
236d2476114SMark Johnston arginfo = (struct ps_strings *)PROC_PS_STRINGS(p);
23731174518SJohn Baldwin destp = (uintptr_t)arginfo;
2383911ee2cSEd Maste
2391562fe49SMark Johnston if (imgp->execpath != NULL && imgp->auxargs != NULL) {
2401562fe49SMark Johnston execpath_len = strlen(imgp->execpath) + 1;
24131174518SJohn Baldwin destp -= execpath_len;
24231174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *));
243b24e6ac8SBrooks Davis imgp->execpathp = (void *)destp;
244b24e6ac8SBrooks Davis error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
24503b0d68cSJohn Baldwin if (error != 0)
24603b0d68cSJohn Baldwin return (error);
2473911ee2cSEd Maste }
2483911ee2cSEd Maste
2493911ee2cSEd Maste /* Prepare the canary for SSP. */
2503911ee2cSEd Maste arc4rand(canary, sizeof(canary), 0);
25131174518SJohn Baldwin destp -= roundup(sizeof(canary), sizeof(void *));
252b24e6ac8SBrooks Davis imgp->canary = (void *)destp;
253b24e6ac8SBrooks Davis error = copyout(canary, imgp->canary, sizeof(canary));
25403b0d68cSJohn Baldwin if (error != 0)
25503b0d68cSJohn Baldwin return (error);
2563911ee2cSEd Maste
25731174518SJohn Baldwin /* Allocate room for the argument and environment strings. */
25831174518SJohn Baldwin destp -= ARG_MAX - imgp->args->stringspace;
25931174518SJohn Baldwin destp = rounddown2(destp, sizeof(void *));
26031174518SJohn Baldwin ustringp = destp;
26131174518SJohn Baldwin
26203b0d68cSJohn Baldwin if (imgp->auxargs) {
263d8010b11SJohn Baldwin /*
264d8010b11SJohn Baldwin * Allocate room on the stack for the ELF auxargs
265d8010b11SJohn Baldwin * array. It has up to LINUX_AT_COUNT entries.
266d8010b11SJohn Baldwin */
267d8010b11SJohn Baldwin destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo);
268d8010b11SJohn Baldwin destp = rounddown2(destp, sizeof(void *));
26903b0d68cSJohn Baldwin }
2703911ee2cSEd Maste
27131174518SJohn Baldwin vectp = (char **)destp;
27231174518SJohn Baldwin
2733911ee2cSEd Maste /*
2743911ee2cSEd Maste * Allocate room for argc and the argv[] and env vectors including the
2753911ee2cSEd Maste * terminating NULL pointers.
2763911ee2cSEd Maste */
2773911ee2cSEd Maste vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1;
2783911ee2cSEd Maste vectp = (char **)STACKALIGN(vectp);
2793911ee2cSEd Maste
2803911ee2cSEd Maste /* vectp also becomes our initial stack base. */
28131174518SJohn Baldwin *stack_base = (uintptr_t)vectp;
2823911ee2cSEd Maste
2833911ee2cSEd Maste stringp = imgp->args->begin_argv;
2843911ee2cSEd Maste argc = imgp->args->argc;
2853911ee2cSEd Maste envc = imgp->args->envc;
2863911ee2cSEd Maste
2873911ee2cSEd Maste /* Copy out strings - arguments and environment. */
28831174518SJohn Baldwin error = copyout(stringp, (void *)ustringp,
28931174518SJohn Baldwin ARG_MAX - imgp->args->stringspace);
29003b0d68cSJohn Baldwin if (error != 0)
29103b0d68cSJohn Baldwin return (error);
2923911ee2cSEd Maste
2933911ee2cSEd Maste /* Fill in "ps_strings" struct for ps, w, etc. */
29403b0d68cSJohn Baldwin if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
29503b0d68cSJohn Baldwin suword(&arginfo->ps_nargvstr, argc) != 0)
29603b0d68cSJohn Baldwin return (EFAULT);
2973911ee2cSEd Maste
29803b0d68cSJohn Baldwin if (suword(vectp++, argc) != 0)
29903b0d68cSJohn Baldwin return (EFAULT);
30003b0d68cSJohn Baldwin
3013911ee2cSEd Maste /* Fill in argument portion of vector table. */
3023911ee2cSEd Maste for (; argc > 0; --argc) {
30331174518SJohn Baldwin if (suword(vectp++, ustringp) != 0)
30403b0d68cSJohn Baldwin return (EFAULT);
3053911ee2cSEd Maste while (*stringp++ != 0)
30631174518SJohn Baldwin ustringp++;
30731174518SJohn Baldwin ustringp++;
3083911ee2cSEd Maste }
3093911ee2cSEd Maste
3103911ee2cSEd Maste /* A null vector table pointer separates the argp's from the envp's. */
31103b0d68cSJohn Baldwin if (suword(vectp++, 0) != 0)
31203b0d68cSJohn Baldwin return (EFAULT);
3133911ee2cSEd Maste
31403b0d68cSJohn Baldwin if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
31503b0d68cSJohn Baldwin suword(&arginfo->ps_nenvstr, envc) != 0)
31603b0d68cSJohn Baldwin return (EFAULT);
3173911ee2cSEd Maste
3183911ee2cSEd Maste /* Fill in environment portion of vector table. */
3193911ee2cSEd Maste for (; envc > 0; --envc) {
32031174518SJohn Baldwin if (suword(vectp++, ustringp) != 0)
32103b0d68cSJohn Baldwin return (EFAULT);
3223911ee2cSEd Maste while (*stringp++ != 0)
32331174518SJohn Baldwin ustringp++;
32431174518SJohn Baldwin ustringp++;
3253911ee2cSEd Maste }
3263911ee2cSEd Maste
3273911ee2cSEd Maste /* The end of the vector table is a null pointer. */
32803b0d68cSJohn Baldwin if (suword(vectp, 0) != 0)
32903b0d68cSJohn Baldwin return (EFAULT);
33003b0d68cSJohn Baldwin
331d8010b11SJohn Baldwin if (imgp->auxargs) {
332d8010b11SJohn Baldwin vectp++;
333d8010b11SJohn Baldwin error = imgp->sysent->sv_copyout_auxargs(imgp,
334d8010b11SJohn Baldwin (uintptr_t)vectp);
335d8010b11SJohn Baldwin if (error != 0)
336d8010b11SJohn Baldwin return (error);
337d8010b11SJohn Baldwin }
338d8010b11SJohn Baldwin
33903b0d68cSJohn Baldwin return (0);
3403911ee2cSEd Maste }
3413911ee2cSEd Maste
3423911ee2cSEd Maste /*
3433911ee2cSEd Maste * Reset registers to default values on exec.
3443911ee2cSEd Maste */
3453911ee2cSEd Maste static void
linux_exec_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)34631174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp,
34731174518SJohn Baldwin uintptr_t stack)
3483911ee2cSEd Maste {
3493911ee2cSEd Maste struct trapframe *regs = td->td_frame;
350c0422e6cSMitchell Horne struct pcb *pcb = td->td_pcb;
3513911ee2cSEd Maste
3523911ee2cSEd Maste /* LINUXTODO: validate */
3533911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo);
3543911ee2cSEd Maste
3553911ee2cSEd Maste memset(regs, 0, sizeof(*regs));
3563911ee2cSEd Maste /* glibc start.S registers function pointer in x0 with atexit. */
3573911ee2cSEd Maste regs->tf_sp = stack;
3583911ee2cSEd Maste #if 0 /* LINUXTODO: See if this is used. */
3593911ee2cSEd Maste regs->tf_lr = imgp->entry_addr;
3603911ee2cSEd Maste #else
3613911ee2cSEd Maste regs->tf_lr = 0xffffffffffffffff;
3623911ee2cSEd Maste #endif
3633911ee2cSEd Maste regs->tf_elr = imgp->entry_addr;
36455872d47SAlex Richardson
365c0422e6cSMitchell Horne pcb->pcb_tpidr_el0 = 0;
366c0422e6cSMitchell Horne pcb->pcb_tpidrro_el0 = 0;
367c6b96921SJohn Baldwin WRITE_SPECIALREG(tpidrro_el0, 0);
368c6b96921SJohn Baldwin WRITE_SPECIALREG(tpidr_el0, 0);
369c6b96921SJohn Baldwin
37055872d47SAlex Richardson #ifdef VFP
371c0422e6cSMitchell Horne vfp_reset_state(td, pcb);
37255872d47SAlex Richardson #endif
373c0422e6cSMitchell Horne
374c0422e6cSMitchell Horne /*
375c0422e6cSMitchell Horne * Clear debug register state. It is not applicable to the new process.
376c0422e6cSMitchell Horne */
377c0422e6cSMitchell Horne bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
3783911ee2cSEd Maste }
3793911ee2cSEd Maste
3803911ee2cSEd Maste int
linux_rt_sigreturn(struct thread * td,struct linux_rt_sigreturn_args * args)3813911ee2cSEd Maste linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
3823911ee2cSEd Maste {
3833911ee2cSEd Maste
3843911ee2cSEd Maste /* LINUXTODO: implement */
3853911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo);
3863911ee2cSEd Maste return (EDOOFUS);
3873911ee2cSEd Maste }
3883911ee2cSEd Maste
3893911ee2cSEd Maste static void
linux_rt_sendsig(sig_t catcher,ksiginfo_t * ksi,sigset_t * mask)3903911ee2cSEd Maste linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
3913911ee2cSEd Maste {
3923911ee2cSEd Maste
3933911ee2cSEd Maste /* LINUXTODO: implement */
3943911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo);
3953911ee2cSEd Maste }
3963911ee2cSEd Maste
3973911ee2cSEd Maste struct sysentvec elf_linux_sysvec = {
3983911ee2cSEd Maste .sv_size = LINUX_SYS_MAXSYSCALL,
3993911ee2cSEd Maste .sv_table = linux_sysent,
4003911ee2cSEd Maste .sv_transtrap = linux_translate_traps,
4013911ee2cSEd Maste .sv_fixup = linux_elf_fixup,
4023911ee2cSEd Maste .sv_sendsig = linux_rt_sendsig,
4033911ee2cSEd Maste .sv_sigcode = &_binary_linux_locore_o_start,
4043911ee2cSEd Maste .sv_szsigcode = &linux_szsigcode,
4053911ee2cSEd Maste .sv_name = "Linux ELF64",
4063911ee2cSEd Maste .sv_coredump = elf64_coredump,
4073911ee2cSEd Maste .sv_imgact_try = linux_exec_imgact_try,
4083911ee2cSEd Maste .sv_minsigstksz = LINUX_MINSIGSTKSZ,
4093911ee2cSEd Maste .sv_minuser = VM_MIN_ADDRESS,
4103911ee2cSEd Maste .sv_maxuser = VM_MAXUSER_ADDRESS,
4113911ee2cSEd Maste .sv_usrstack = USRSTACK,
4123911ee2cSEd Maste .sv_psstrings = PS_STRINGS, /* XXX */
413947e8491SMark Johnston .sv_psstringssz = sizeof(struct ps_strings),
414d4f55cc8SEd Maste .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
4155caa67faSJohn Baldwin .sv_copyout_auxargs = linux_copyout_auxargs,
4163911ee2cSEd Maste .sv_copyout_strings = linux_copyout_strings,
4173911ee2cSEd Maste .sv_setregs = linux_exec_setregs,
4183911ee2cSEd Maste .sv_fixlimit = NULL,
4193911ee2cSEd Maste .sv_maxssiz = NULL,
42052d8029eSKonstantin Belousov .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
42152d8029eSKonstantin Belousov SV_SIG_WAITNDQ,
4223911ee2cSEd Maste .sv_set_syscall_retval = linux_set_syscall_retval,
4233911ee2cSEd Maste .sv_fetch_syscall_args = linux_fetch_syscall_args,
4243911ee2cSEd Maste .sv_syscallnames = NULL,
4253911ee2cSEd Maste .sv_shared_page_base = SHAREDPAGE,
4263911ee2cSEd Maste .sv_shared_page_len = PAGE_SIZE,
4273911ee2cSEd Maste .sv_schedtail = linux_schedtail,
4283911ee2cSEd Maste .sv_thread_detach = linux_thread_detach,
429*95f24ec8SEdward Tomasz Napierala .sv_trap = NULL,
4305d5616acSEdward Tomasz Napierala .sv_hwcap = &elf_hwcap,
4315d5616acSEdward Tomasz Napierala .sv_hwcap2 = &elf_hwcap2,
4324815f175SKonstantin Belousov .sv_onexec = linux_on_exec,
4334815f175SKonstantin Belousov .sv_onexit = linux_on_exit,
4344815f175SKonstantin Belousov .sv_ontdexit = linux_thread_dtor,
435dc107fe1SKonstantin Belousov .sv_setid_allowed = &linux_setid_allowed_query,
4363911ee2cSEd Maste };
4373911ee2cSEd Maste
4383911ee2cSEd Maste static void
linux_vdso_install(const void * param)4393911ee2cSEd Maste linux_vdso_install(const void *param)
4403911ee2cSEd Maste {
4413911ee2cSEd Maste
4423911ee2cSEd Maste linux_szsigcode = (&_binary_linux_locore_o_end -
4433911ee2cSEd Maste &_binary_linux_locore_o_start);
4443911ee2cSEd Maste
4453911ee2cSEd Maste if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
4463911ee2cSEd Maste panic("invalid Linux VDSO size\n");
4473911ee2cSEd Maste
4483911ee2cSEd Maste __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
4493911ee2cSEd Maste
4503911ee2cSEd Maste linux_shared_page_obj = __elfN(linux_shared_page_init)
4513911ee2cSEd Maste (&linux_shared_page_mapping);
4523911ee2cSEd Maste
4533911ee2cSEd Maste __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
4543911ee2cSEd Maste
4553911ee2cSEd Maste memcpy(linux_shared_page_mapping, elf_linux_sysvec.sv_sigcode,
4563911ee2cSEd Maste linux_szsigcode);
4573911ee2cSEd Maste elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
4583911ee2cSEd Maste
4593911ee2cSEd Maste linux_kplatform = linux_shared_page_mapping +
4603911ee2cSEd Maste (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
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
linux_vdso_deinstall(const void * param)4663911ee2cSEd Maste linux_vdso_deinstall(const void *param)
4673911ee2cSEd Maste {
4683911ee2cSEd Maste
4693911ee2cSEd Maste LIN_SDT_PROBE0(sysvec, linux_vdso_deinstall, todo);
4703df2766aSMark Johnston __elfN(linux_shared_page_fini)(linux_shared_page_obj,
4713df2766aSMark 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
linux_trans_osrel(const Elf_Note * note,int32_t * osrel)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
linux64_elf_modevent(module_t mod,int type,void * data)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