xref: /freebsd/sys/arm64/linux/linux_sysvec.c (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1994-1996 Søren Schmidt
5  * Copyright (c) 2018 Turing Robotic Industries Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/cdefs.h>
35 #include <sys/elf.h>
36 #include <sys/exec.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/ktr.h>
41 #include <sys/lock.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/stddef.h>
46 #include <sys/signalvar.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_param.h>
58 
59 #include <arm64/linux/linux.h>
60 #include <arm64/linux/linux_proto.h>
61 #include <compat/linux/linux_dtrace.h>
62 #include <compat/linux/linux_emul.h>
63 #include <compat/linux/linux_fork.h>
64 #include <compat/linux/linux_ioctl.h>
65 #include <compat/linux/linux_mib.h>
66 #include <compat/linux/linux_misc.h>
67 #include <compat/linux/linux_signal.h>
68 #include <compat/linux/linux_util.h>
69 #include <compat/linux/linux_vdso.h>
70 
71 #include <machine/md_var.h>
72 
73 #ifdef VFP
74 #include <machine/vfp.h>
75 #endif
76 
77 MODULE_VERSION(linux64elf, 1);
78 
79 #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
80 #define	LINUX_VDSOPAGE		(VM_MAXUSER_ADDRESS - \
81 				    LINUX_VDSOPAGE_SIZE)
82 #define	LINUX_SHAREDPAGE	(LINUX_VDSOPAGE - PAGE_SIZE)
83 				/*
84 				 * PAGE_SIZE - the size
85 				 * of the native SHAREDPAGE
86 				 */
87 #define	LINUX_USRSTACK		LINUX_SHAREDPAGE
88 #define	LINUX_PS_STRINGS	(LINUX_USRSTACK - \
89 				    sizeof(struct ps_strings))
90 
91 static int linux_szsigcode;
92 static vm_object_t linux_vdso_obj;
93 static char *linux_vdso_mapping;
94 extern char _binary_linux_vdso_so_o_start;
95 extern char _binary_linux_vdso_so_o_end;
96 static vm_offset_t linux_vdso_base;
97 
98 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
99 
100 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
101 
102 static int	linux_copyout_strings(struct image_params *imgp,
103 		    uintptr_t *stack_base);
104 static int	linux_elf_fixup(uintptr_t *stack_base,
105 		    struct image_params *iparams);
106 static bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
107 static void	linux_vdso_install(const void *param);
108 static void	linux_vdso_deinstall(const void *param);
109 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
110 static void	linux_set_syscall_retval(struct thread *td, int error);
111 static int	linux_fetch_syscall_args(struct thread *td);
112 static void	linux_exec_setregs(struct thread *td, struct image_params *imgp,
113 		    uintptr_t stack);
114 static void	linux_exec_sysvec_init(void *param);
115 static int	linux_on_exec_vmspace(struct proc *p,
116 		    struct image_params *imgp);
117 
118 /* DTrace init */
119 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
120 
121 /* DTrace probes */
122 LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int");
123 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo);
124 LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo);
125 LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo);
126 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo);
127 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo);
128 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo);
129 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo);
130 
131 LINUX_VDSO_SYM_CHAR(linux_platform);
132 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
133 LINUX_VDSO_SYM_INTPTR(__kernel_rt_sigreturn);
134 
135 /* LINUXTODO: do we have traps to translate? */
136 static int
137 linux_translate_traps(int signal, int trap_code)
138 {
139 
140 	LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code);
141 	return (signal);
142 }
143 
144 static int
145 linux_fetch_syscall_args(struct thread *td)
146 {
147 	struct proc *p;
148 	struct syscall_args *sa;
149 	register_t *ap;
150 
151 	p = td->td_proc;
152 	ap = td->td_frame->tf_x;
153 	sa = &td->td_sa;
154 
155 	sa->code = td->td_frame->tf_x[8];
156 	sa->original_code = sa->code;
157 	/* LINUXTODO: generic syscall? */
158 	if (sa->code >= p->p_sysent->sv_size)
159 		sa->callp = &p->p_sysent->sv_table[0];
160 	else
161 		sa->callp = &p->p_sysent->sv_table[sa->code];
162 
163 	if (sa->callp->sy_narg > MAXARGS)
164 		panic("ARM64TODO: Could we have more than %d args?", MAXARGS);
165 	memcpy(sa->args, ap, MAXARGS * sizeof(register_t));
166 
167 	td->td_retval[0] = 0;
168 	return (0);
169 }
170 
171 static void
172 linux_set_syscall_retval(struct thread *td, int error)
173 {
174 
175 	td->td_retval[1] = td->td_frame->tf_x[1];
176 	cpu_set_syscall_retval(td, error);
177 
178 	if (__predict_false(error != 0)) {
179 		if (error != ERESTART && error != EJUSTRETURN)
180 			td->td_frame->tf_x[0] = bsd_to_linux_errno(error);
181 	}
182 }
183 
184 static int
185 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
186 {
187 	Elf_Auxargs *args;
188 	Elf_Auxinfo *argarray, *pos;
189 	struct proc *p;
190 	int error, issetugid;
191 
192 	LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo);
193 	p = imgp->proc;
194 
195 	args = (Elf64_Auxargs *)imgp->auxargs;
196 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
197 	    M_WAITOK | M_ZERO);
198 
199 	issetugid = p->p_flag & P_SUGID ? 1 : 0;
200 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
201 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap);
202 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
203 	AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
204 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
205 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
206 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
207 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
208 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
209 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
210 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
211 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
212 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
213 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
214 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
215 	AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
216 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2);
217 	if (imgp->execpathp != 0)
218 		AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
219 	if (args->execfd != -1)
220 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
221 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
222 	AUXARGS_ENTRY(pos, AT_NULL, 0);
223 
224 	free(imgp->auxargs, M_TEMP);
225 	imgp->auxargs = NULL;
226 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
227 
228 	error = copyout(argarray, (void *)base,
229 	    sizeof(*argarray) * LINUX_AT_COUNT);
230 	free(argarray, M_TEMP);
231 	return (error);
232 }
233 
234 static int
235 linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp)
236 {
237 
238 	LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo);
239 
240 	return (0);
241 }
242 
243 /*
244  * Copy strings out to the new process address space, constructing new arg
245  * and env vector tables. Return a pointer to the base so that it can be used
246  * as the initial stack pointer.
247  * LINUXTODO: deduplicate against other linuxulator archs
248  */
249 static int
250 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
251 {
252 	char **vectp;
253 	char *stringp;
254 	uintptr_t destp, ustringp;
255 	struct ps_strings *arginfo;
256 	char canary[LINUX_AT_RANDOM_LEN];
257 	size_t execpath_len;
258 	struct proc *p;
259 	int argc, envc, error;
260 
261 	/* Calculate string base and vector table pointers. */
262 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
263 		execpath_len = strlen(imgp->execpath) + 1;
264 	else
265 		execpath_len = 0;
266 
267 	p = imgp->proc;
268 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
269 	destp = (uintptr_t)arginfo;
270 
271 	if (execpath_len != 0) {
272 		destp -= execpath_len;
273 		destp = rounddown2(destp, sizeof(void *));
274 		imgp->execpathp = (void *)destp;
275 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
276 		if (error != 0)
277 			return (error);
278 	}
279 
280 	/* Prepare the canary for SSP. */
281 	arc4rand(canary, sizeof(canary), 0);
282 	destp -= roundup(sizeof(canary), sizeof(void *));
283 	imgp->canary = (void *)destp;
284 	error = copyout(canary, imgp->canary, sizeof(canary));
285 	if (error != 0)
286 		return (error);
287 
288 	/* Allocate room for the argument and environment strings. */
289 	destp -= ARG_MAX - imgp->args->stringspace;
290 	destp = rounddown2(destp, sizeof(void *));
291 	ustringp = destp;
292 
293 	if (imgp->auxargs) {
294 		/*
295 		 * Allocate room on the stack for the ELF auxargs
296 		 * array.  It has up to LINUX_AT_COUNT entries.
297 		 */
298 		destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo);
299 		destp = rounddown2(destp, sizeof(void *));
300 	}
301 
302 	vectp = (char **)destp;
303 
304 	/*
305 	 * Allocate room for argc and the argv[] and env vectors including the
306 	 * terminating NULL pointers.
307 	 */
308 	vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1;
309 	vectp = (char **)STACKALIGN(vectp);
310 
311 	/* vectp also becomes our initial stack base. */
312 	*stack_base = (uintptr_t)vectp;
313 
314 	stringp = imgp->args->begin_argv;
315 	argc = imgp->args->argc;
316 	envc = imgp->args->envc;
317 
318 	/* Copy out strings - arguments and environment. */
319 	error = copyout(stringp, (void *)ustringp,
320 	    ARG_MAX - imgp->args->stringspace);
321 	if (error != 0)
322 		return (error);
323 
324 	/* Fill in "ps_strings" struct for ps, w, etc. */
325 	if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
326 	    suword(&arginfo->ps_nargvstr, argc) != 0)
327 		return (EFAULT);
328 
329 	if (suword(vectp++, argc) != 0)
330 		return (EFAULT);
331 
332 	/* Fill in argument portion of vector table. */
333 	for (; argc > 0; --argc) {
334 		if (suword(vectp++, ustringp) != 0)
335 			return (EFAULT);
336 		while (*stringp++ != 0)
337 			ustringp++;
338 		ustringp++;
339 	}
340 
341 	/* A null vector table pointer separates the argp's from the envp's. */
342 	if (suword(vectp++, 0) != 0)
343 		return (EFAULT);
344 
345 	if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
346 	    suword(&arginfo->ps_nenvstr, envc) != 0)
347 		return (EFAULT);
348 
349 	/* Fill in environment portion of vector table. */
350 	for (; envc > 0; --envc) {
351 		if (suword(vectp++, ustringp) != 0)
352 			return (EFAULT);
353 		while (*stringp++ != 0)
354 			ustringp++;
355 		ustringp++;
356 	}
357 
358 	/* The end of the vector table is a null pointer. */
359 	if (suword(vectp, 0) != 0)
360 		return (EFAULT);
361 
362 	if (imgp->auxargs) {
363 		vectp++;
364 		error = imgp->sysent->sv_copyout_auxargs(imgp,
365 		    (uintptr_t)vectp);
366 		if (error != 0)
367 			return (error);
368 	}
369 
370 	return (0);
371 }
372 
373 /*
374  * Reset registers to default values on exec.
375  */
376 static void
377 linux_exec_setregs(struct thread *td, struct image_params *imgp,
378     uintptr_t stack)
379 {
380 	struct trapframe *regs = td->td_frame;
381 	struct pcb *pcb = td->td_pcb;
382 
383 	/* LINUXTODO: validate */
384 	LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo);
385 
386 	memset(regs, 0, sizeof(*regs));
387 	/* glibc start.S registers function pointer in x0 with atexit. */
388         regs->tf_sp = stack;
389 #if 0	/* LINUXTODO: See if this is used. */
390 	regs->tf_lr = imgp->entry_addr;
391 #else
392         regs->tf_lr = 0xffffffffffffffff;
393 #endif
394         regs->tf_elr = imgp->entry_addr;
395 
396 	pcb->pcb_tpidr_el0 = 0;
397 	pcb->pcb_tpidrro_el0 = 0;
398 	WRITE_SPECIALREG(tpidrro_el0, 0);
399 	WRITE_SPECIALREG(tpidr_el0, 0);
400 
401 #ifdef VFP
402 	vfp_reset_state(td, pcb);
403 #endif
404 
405 	/*
406 	 * Clear debug register state. It is not applicable to the new process.
407 	 */
408 	bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
409 }
410 
411 int
412 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
413 {
414 	struct l_sigframe frame;
415 	struct trapframe *tf;
416 	int error;
417 
418 	tf = td->td_frame;
419 
420 	if (copyin((void *)tf->tf_sp, &frame, sizeof(frame)))
421 		return (EFAULT);
422 
423 	error = set_mcontext(td, &frame.sf_uc.uc_mcontext);
424 	if (error != 0)
425 		return (error);
426 
427 	/* Restore signal mask. */
428 	kern_sigprocmask(td, SIG_SETMASK, &frame.sf_uc.uc_sigmask, NULL, 0);
429 
430 	return (EJUSTRETURN);
431 }
432 
433 static void
434 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
435 {
436 	struct thread *td;
437 	struct proc *p;
438 	struct trapframe *tf;
439 	struct l_sigframe *fp, frame;
440 	struct sigacts *psp;
441 	int onstack, sig;
442 	uint32_t spsr;
443 
444 	td = curthread;
445 	p = td->td_proc;
446 	PROC_LOCK_ASSERT(p, MA_OWNED);
447 
448 	sig = ksi->ksi_signo;
449 	psp = p->p_sigacts;
450 	mtx_assert(&psp->ps_mtx, MA_OWNED);
451 
452 	tf = td->td_frame;
453 	onstack = sigonstack(tf->tf_sp);
454 
455 	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
456 	    catcher, sig);
457 
458 	/* Allocate and validate space for the signal handler context. */
459 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
460 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
461 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
462 		    td->td_sigstk.ss_size);
463 #if defined(COMPAT_43)
464 		td->td_sigstk.ss_flags |= SS_ONSTACK;
465 #endif
466 	} else {
467 		fp = (struct l_sigframe *)td->td_frame->tf_sp;
468 	}
469 
470 	/* Make room, keeping the stack aligned */
471 	fp--;
472 	fp = (struct l_sigframe *)STACKALIGN(fp);
473 
474 	/* Fill in the frame to copy out */
475 	bzero(&frame, sizeof(frame));
476 	get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
477 	spsr = frame.sf_uc.uc_mcontext.mc_gpregs.gp_spsr;
478 
479 	/* Translate the signal. */
480 	sig = bsd_to_linux_signal(sig);
481 
482 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
483 	frame.sf_uc.uc_sigmask = *mask;
484 	frame.sf_uc.uc_stack = td->td_sigstk;
485 	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
486 	    (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
487 	mtx_unlock(&psp->ps_mtx);
488 	PROC_UNLOCK(td->td_proc);
489 
490 	/* Copy the sigframe out to the user's stack. */
491 	if (copyout(&frame, fp, sizeof(*fp)) != 0) {
492 		/* Process has trashed its stack. Kill it. */
493 		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
494 		PROC_LOCK(p);
495 		sigexit(td, SIGILL);
496 	}
497 
498 	tf->tf_x[0]= sig;
499 	tf->tf_x[1] = (register_t)&fp->sf_si;
500 	tf->tf_x[2] = (register_t)&fp->sf_uc;
501 
502 	tf->tf_elr = (register_t)catcher;
503 	tf->tf_sp = (register_t)fp;
504 	tf->tf_lr = (register_t)__kernel_rt_sigreturn;
505 
506 	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
507 	    tf->tf_sp);
508 
509 	PROC_LOCK(p);
510 	mtx_lock(&psp->ps_mtx);
511 }
512 
513 struct sysentvec elf_linux_sysvec = {
514 	.sv_size	= LINUX_SYS_MAXSYSCALL,
515 	.sv_table	= linux_sysent,
516 	.sv_transtrap	= linux_translate_traps,
517 	.sv_fixup	= linux_elf_fixup,
518 	.sv_sendsig	= linux_rt_sendsig,
519 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
520 	.sv_szsigcode	= &linux_szsigcode,
521 	.sv_name	= "Linux ELF64",
522 	.sv_coredump	= elf64_coredump,
523 	.sv_elf_core_osabi = ELFOSABI_NONE,
524 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
525 	.sv_elf_core_prepare_notes = linux64_prepare_notes,
526 	.sv_imgact_try	= linux_exec_imgact_try,
527 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
528 	.sv_minuser	= VM_MIN_ADDRESS,
529 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
530 	.sv_usrstack	= LINUX_USRSTACK,
531 	.sv_psstrings	= LINUX_PS_STRINGS,
532 	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
533 	.sv_copyout_auxargs = linux_copyout_auxargs,
534 	.sv_copyout_strings = linux_copyout_strings,
535 	.sv_setregs	= linux_exec_setregs,
536 	.sv_fixlimit	= NULL,
537 	.sv_maxssiz	= NULL,
538 	.sv_flags	= SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
539 	    SV_SIG_WAITNDQ | SV_TIMEKEEP,
540 	.sv_set_syscall_retval = linux_set_syscall_retval,
541 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
542 	.sv_syscallnames = NULL,
543 	.sv_shared_page_base = LINUX_SHAREDPAGE,
544 	.sv_shared_page_len = PAGE_SIZE,
545 	.sv_schedtail	= linux_schedtail,
546 	.sv_thread_detach = linux_thread_detach,
547 	.sv_trap	= NULL,
548 	.sv_hwcap	= &elf_hwcap,
549 	.sv_hwcap2	= &elf_hwcap2,
550 	.sv_onexec	= linux_on_exec_vmspace,
551 	.sv_onexit	= linux_on_exit,
552 	.sv_ontdexit	= linux_thread_dtor,
553 	.sv_setid_allowed = &linux_setid_allowed_query,
554 };
555 
556 static int
557 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
558 {
559 	int error;
560 
561 	error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
562 	    LINUX_VDSOPAGE_SIZE, imgp);
563 	if (error == 0)
564 		linux_on_exec(p, imgp);
565 	return (error);
566 }
567 
568 /*
569  * linux_vdso_install() and linux_exec_sysvec_init() must be called
570  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
571  */
572 static void
573 linux_exec_sysvec_init(void *param)
574 {
575 	l_uintptr_t *ktimekeep_base;
576 	struct sysentvec *sv;
577 	ptrdiff_t tkoff;
578 
579 	sv = param;
580 	/* Fill timekeep_base */
581 	exec_sysvec_init(sv);
582 
583 	tkoff = kern_timekeep_base - linux_vdso_base;
584 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
585 	*ktimekeep_base = sv->sv_timekeep_base;
586 }
587 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
588     linux_exec_sysvec_init, &elf_linux_sysvec);
589 
590 static void
591 linux_vdso_install(const void *param)
592 {
593 	char *vdso_start = &_binary_linux_vdso_so_o_start;
594 	char *vdso_end = &_binary_linux_vdso_so_o_end;
595 
596 	linux_szsigcode = vdso_end - vdso_start;
597 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
598 
599 	linux_vdso_base = LINUX_VDSOPAGE;
600 
601 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
602 
603 	linux_vdso_obj = __elfN(linux_shared_page_init)
604 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
605 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
606 
607 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
608 }
609 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
610     linux_vdso_install, NULL);
611 
612 static void
613 linux_vdso_deinstall(const void *param)
614 {
615 
616 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
617 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
618 }
619 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
620     linux_vdso_deinstall, NULL);
621 
622 static void
623 linux_vdso_reloc(char *mapping, Elf_Addr offset)
624 {
625 	Elf_Size rtype, symidx;
626 	const Elf_Rela *rela;
627 	const Elf_Shdr *shdr;
628 	const Elf_Ehdr *ehdr;
629 	Elf_Addr *where;
630 	Elf_Addr addr, addend;
631 	int i, relacnt;
632 
633 	MPASS(offset != 0);
634 
635 	relacnt = 0;
636 	ehdr = (const Elf_Ehdr *)mapping;
637 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
638 	for (i = 0; i < ehdr->e_shnum; i++)
639 	{
640 		switch (shdr[i].sh_type) {
641 		case SHT_REL:
642 			printf("Linux Aarch64 vDSO: unexpected Rel section\n");
643 			break;
644 		case SHT_RELA:
645 			rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
646 			relacnt = shdr[i].sh_size / sizeof(*rela);
647 		}
648 	}
649 
650 	for (i = 0; i < relacnt; i++, rela++) {
651 		where = (Elf_Addr *)(mapping + rela->r_offset);
652 		addend = rela->r_addend;
653 		rtype = ELF_R_TYPE(rela->r_info);
654 		symidx = ELF_R_SYM(rela->r_info);
655 
656 		switch (rtype) {
657 		case R_AARCH64_NONE:	/* none */
658 			break;
659 
660 		case R_AARCH64_RELATIVE:	/* B + A */
661 			addr = (Elf_Addr)(mapping + addend);
662 			if (*where != addr)
663 				*where = addr;
664 			break;
665 		default:
666 			printf("Linux Aarch64 vDSO: unexpected relocation type %ld, "
667 			    "symbol index %ld\n", rtype, symidx);
668 		}
669 	}
670 }
671 
672 static char GNU_ABI_VENDOR[] = "GNU";
673 static int GNU_ABI_LINUX = 0;
674 
675 /* LINUXTODO: deduplicate */
676 static bool
677 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
678 {
679 	const Elf32_Word *desc;
680 	uintptr_t p;
681 
682 	p = (uintptr_t)(note + 1);
683 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
684 
685 	desc = (const Elf32_Word *)p;
686 	if (desc[0] != GNU_ABI_LINUX)
687 		return (false);
688 
689 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
690 	return (true);
691 }
692 
693 static Elf_Brandnote linux64_brandnote = {
694 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
695 	.hdr.n_descsz	= 16,
696 	.hdr.n_type	= 1,
697 	.vendor		= GNU_ABI_VENDOR,
698 	.flags		= BN_TRANSLATE_OSREL,
699 	.trans_osrel	= linux_trans_osrel
700 };
701 
702 static Elf64_Brandinfo linux_glibc2brand = {
703 	.brand		= ELFOSABI_LINUX,
704 	.machine	= EM_AARCH64,
705 	.compat_3_brand	= "Linux",
706 	.emul_path	= linux_emul_path,
707 	.interp_path	= "/lib64/ld-linux-x86-64.so.2",
708 	.sysvec		= &elf_linux_sysvec,
709 	.interp_newpath	= NULL,
710 	.brand_note	= &linux64_brandnote,
711 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
712 };
713 
714 Elf64_Brandinfo *linux_brandlist[] = {
715 	&linux_glibc2brand,
716 	NULL
717 };
718 
719 static int
720 linux64_elf_modevent(module_t mod, int type, void *data)
721 {
722 	Elf64_Brandinfo **brandinfo;
723 	struct linux_ioctl_handler**lihp;
724 	int error;
725 
726 	error = 0;
727 	switch(type) {
728 	case MOD_LOAD:
729 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
730 		    ++brandinfo)
731 			if (elf64_insert_brand_entry(*brandinfo) < 0)
732 				error = EINVAL;
733 		if (error == 0) {
734 			SET_FOREACH(lihp, linux_ioctl_handler_set)
735 				linux_ioctl_register_handler(*lihp);
736 			stclohz = (stathz ? stathz : hz);
737 			if (bootverbose)
738 				printf("Linux arm64 ELF exec handler installed\n");
739 		}
740 		break;
741 	case MOD_UNLOAD:
742 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
743 		    ++brandinfo)
744 			if (elf64_brand_inuse(*brandinfo))
745 				error = EBUSY;
746 		if (error == 0) {
747 			for (brandinfo = &linux_brandlist[0];
748 			    *brandinfo != NULL; ++brandinfo)
749 				if (elf64_remove_brand_entry(*brandinfo) < 0)
750 					error = EINVAL;
751 		}
752 		if (error == 0) {
753 			SET_FOREACH(lihp, linux_ioctl_handler_set)
754 				linux_ioctl_unregister_handler(*lihp);
755 			if (bootverbose)
756 				printf("Linux arm64 ELF exec handler removed\n");
757 		} else
758 			printf("Could not deinstall Linux arm64 ELF interpreter entry\n");
759 		break;
760 	default:
761 		return (EOPNOTSUPP);
762 	}
763 	return (error);
764 }
765 
766 static moduledata_t linux64_elf_mod = {
767 	"linux64elf",
768 	linux64_elf_modevent,
769 	0
770 };
771 
772 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
773 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
774 FEATURE(linux64, "AArch64 Linux 64bit support");
775