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