xref: /freebsd/sys/arm64/linux/linux_sysvec.c (revision 681ce946)
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 > nitems(sa->args))
164 		panic("ARM64TODO: Could we have more than %zu args?",
165 		    nitems(sa->args));
166 	memcpy(sa->args, ap, nitems(sa->args) * sizeof(register_t));
167 
168 	td->td_retval[0] = 0;
169 	return (0);
170 }
171 
172 static void
173 linux_set_syscall_retval(struct thread *td, int error)
174 {
175 
176 	td->td_retval[1] = td->td_frame->tf_x[1];
177 	cpu_set_syscall_retval(td, error);
178 
179 	if (__predict_false(error != 0)) {
180 		if (error != ERESTART && error != EJUSTRETURN)
181 			td->td_frame->tf_x[0] = bsd_to_linux_errno(error);
182 	}
183 }
184 
185 static int
186 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
187 {
188 	Elf_Auxargs *args;
189 	Elf_Auxinfo *argarray, *pos;
190 	struct proc *p;
191 	int error, issetugid;
192 
193 	LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo);
194 	p = imgp->proc;
195 
196 	args = (Elf64_Auxargs *)imgp->auxargs;
197 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
198 	    M_WAITOK | M_ZERO);
199 
200 	issetugid = p->p_flag & P_SUGID ? 1 : 0;
201 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
202 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap);
203 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
204 	AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
205 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
206 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
207 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
208 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
209 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
210 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
211 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
212 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
213 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
214 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
215 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
216 	AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
217 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2);
218 	if (imgp->execpathp != 0)
219 		AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
220 	if (args->execfd != -1)
221 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
222 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
223 	AUXARGS_ENTRY(pos, AT_NULL, 0);
224 
225 	free(imgp->auxargs, M_TEMP);
226 	imgp->auxargs = NULL;
227 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
228 
229 	error = copyout(argarray, (void *)base,
230 	    sizeof(*argarray) * LINUX_AT_COUNT);
231 	free(argarray, M_TEMP);
232 	return (error);
233 }
234 
235 static int
236 linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp)
237 {
238 
239 	LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo);
240 
241 	return (0);
242 }
243 
244 /*
245  * Copy strings out to the new process address space, constructing new arg
246  * and env vector tables. Return a pointer to the base so that it can be used
247  * as the initial stack pointer.
248  * LINUXTODO: deduplicate against other linuxulator archs
249  */
250 static int
251 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
252 {
253 	char **vectp;
254 	char *stringp;
255 	uintptr_t destp, ustringp;
256 	struct ps_strings *arginfo;
257 	char canary[LINUX_AT_RANDOM_LEN];
258 	size_t execpath_len;
259 	struct proc *p;
260 	int argc, envc, error;
261 
262 	/* Calculate string base and vector table pointers. */
263 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
264 		execpath_len = strlen(imgp->execpath) + 1;
265 	else
266 		execpath_len = 0;
267 
268 	p = imgp->proc;
269 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
270 	destp = (uintptr_t)arginfo;
271 
272 	if (execpath_len != 0) {
273 		destp -= execpath_len;
274 		destp = rounddown2(destp, sizeof(void *));
275 		imgp->execpathp = (void *)destp;
276 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
277 		if (error != 0)
278 			return (error);
279 	}
280 
281 	/* Prepare the canary for SSP. */
282 	arc4rand(canary, sizeof(canary), 0);
283 	destp -= roundup(sizeof(canary), sizeof(void *));
284 	imgp->canary = (void *)destp;
285 	error = copyout(canary, imgp->canary, sizeof(canary));
286 	if (error != 0)
287 		return (error);
288 
289 	/* Allocate room for the argument and environment strings. */
290 	destp -= ARG_MAX - imgp->args->stringspace;
291 	destp = rounddown2(destp, sizeof(void *));
292 	ustringp = destp;
293 
294 	if (imgp->auxargs) {
295 		/*
296 		 * Allocate room on the stack for the ELF auxargs
297 		 * array.  It has up to LINUX_AT_COUNT entries.
298 		 */
299 		destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo);
300 		destp = rounddown2(destp, sizeof(void *));
301 	}
302 
303 	vectp = (char **)destp;
304 
305 	/*
306 	 * Allocate room for argc and the argv[] and env vectors including the
307 	 * terminating NULL pointers.
308 	 */
309 	vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1;
310 	vectp = (char **)STACKALIGN(vectp);
311 
312 	/* vectp also becomes our initial stack base. */
313 	*stack_base = (uintptr_t)vectp;
314 
315 	stringp = imgp->args->begin_argv;
316 	argc = imgp->args->argc;
317 	envc = imgp->args->envc;
318 
319 	/* Copy out strings - arguments and environment. */
320 	error = copyout(stringp, (void *)ustringp,
321 	    ARG_MAX - imgp->args->stringspace);
322 	if (error != 0)
323 		return (error);
324 
325 	/* Fill in "ps_strings" struct for ps, w, etc. */
326 	if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
327 	    suword(&arginfo->ps_nargvstr, argc) != 0)
328 		return (EFAULT);
329 
330 	if (suword(vectp++, argc) != 0)
331 		return (EFAULT);
332 
333 	/* Fill in argument portion of vector table. */
334 	for (; argc > 0; --argc) {
335 		if (suword(vectp++, ustringp) != 0)
336 			return (EFAULT);
337 		while (*stringp++ != 0)
338 			ustringp++;
339 		ustringp++;
340 	}
341 
342 	/* A null vector table pointer separates the argp's from the envp's. */
343 	if (suword(vectp++, 0) != 0)
344 		return (EFAULT);
345 
346 	if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
347 	    suword(&arginfo->ps_nenvstr, envc) != 0)
348 		return (EFAULT);
349 
350 	/* Fill in environment portion of vector table. */
351 	for (; envc > 0; --envc) {
352 		if (suword(vectp++, ustringp) != 0)
353 			return (EFAULT);
354 		while (*stringp++ != 0)
355 			ustringp++;
356 		ustringp++;
357 	}
358 
359 	/* The end of the vector table is a null pointer. */
360 	if (suword(vectp, 0) != 0)
361 		return (EFAULT);
362 
363 	if (imgp->auxargs) {
364 		vectp++;
365 		error = imgp->sysent->sv_copyout_auxargs(imgp,
366 		    (uintptr_t)vectp);
367 		if (error != 0)
368 			return (error);
369 	}
370 
371 	return (0);
372 }
373 
374 /*
375  * Reset registers to default values on exec.
376  */
377 static void
378 linux_exec_setregs(struct thread *td, struct image_params *imgp,
379     uintptr_t stack)
380 {
381 	struct trapframe *regs = td->td_frame;
382 	struct pcb *pcb = td->td_pcb;
383 
384 	/* LINUXTODO: validate */
385 	LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo);
386 
387 	memset(regs, 0, sizeof(*regs));
388 	/* glibc start.S registers function pointer in x0 with atexit. */
389         regs->tf_sp = stack;
390 #if 0	/* LINUXTODO: See if this is used. */
391 	regs->tf_lr = imgp->entry_addr;
392 #else
393         regs->tf_lr = 0xffffffffffffffff;
394 #endif
395         regs->tf_elr = imgp->entry_addr;
396 
397 	pcb->pcb_tpidr_el0 = 0;
398 	pcb->pcb_tpidrro_el0 = 0;
399 	WRITE_SPECIALREG(tpidrro_el0, 0);
400 	WRITE_SPECIALREG(tpidr_el0, 0);
401 
402 #ifdef VFP
403 	vfp_reset_state(td, pcb);
404 #endif
405 
406 	/*
407 	 * Clear debug register state. It is not applicable to the new process.
408 	 */
409 	bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
410 }
411 
412 int
413 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
414 {
415 	struct l_sigframe frame;
416 	struct trapframe *tf;
417 	int error;
418 
419 	tf = td->td_frame;
420 
421 	if (copyin((void *)tf->tf_sp, &frame, sizeof(frame)))
422 		return (EFAULT);
423 
424 	error = set_mcontext(td, &frame.sf_uc.uc_mcontext);
425 	if (error != 0)
426 		return (error);
427 
428 	/* Restore signal mask. */
429 	kern_sigprocmask(td, SIG_SETMASK, &frame.sf_uc.uc_sigmask, NULL, 0);
430 
431 	return (EJUSTRETURN);
432 }
433 
434 static void
435 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
436 {
437 	struct thread *td;
438 	struct proc *p;
439 	struct trapframe *tf;
440 	struct l_sigframe *fp, frame;
441 	struct sigacts *psp;
442 	int onstack, sig;
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 
478 	/* Translate the signal. */
479 	sig = bsd_to_linux_signal(sig);
480 
481 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
482 	frame.sf_uc.uc_sigmask = *mask;
483 	frame.sf_uc.uc_stack = td->td_sigstk;
484 	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
485 	    (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
486 	mtx_unlock(&psp->ps_mtx);
487 	PROC_UNLOCK(td->td_proc);
488 
489 	/* Copy the sigframe out to the user's stack. */
490 	if (copyout(&frame, fp, sizeof(*fp)) != 0) {
491 		/* Process has trashed its stack. Kill it. */
492 		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
493 		PROC_LOCK(p);
494 		sigexit(td, SIGILL);
495 	}
496 
497 	tf->tf_x[0]= sig;
498 	tf->tf_x[1] = (register_t)&fp->sf_si;
499 	tf->tf_x[2] = (register_t)&fp->sf_uc;
500 
501 	tf->tf_elr = (register_t)catcher;
502 	tf->tf_sp = (register_t)fp;
503 	tf->tf_lr = (register_t)__kernel_rt_sigreturn;
504 
505 	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
506 	    tf->tf_sp);
507 
508 	PROC_LOCK(p);
509 	mtx_lock(&psp->ps_mtx);
510 }
511 
512 struct sysentvec elf_linux_sysvec = {
513 	.sv_size	= LINUX_SYS_MAXSYSCALL,
514 	.sv_table	= linux_sysent,
515 	.sv_transtrap	= linux_translate_traps,
516 	.sv_fixup	= linux_elf_fixup,
517 	.sv_sendsig	= linux_rt_sendsig,
518 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
519 	.sv_szsigcode	= &linux_szsigcode,
520 	.sv_name	= "Linux ELF64",
521 	.sv_coredump	= elf64_coredump,
522 	.sv_elf_core_osabi = ELFOSABI_NONE,
523 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
524 	.sv_elf_core_prepare_notes = linux64_prepare_notes,
525 	.sv_imgact_try	= linux_exec_imgact_try,
526 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
527 	.sv_minuser	= VM_MIN_ADDRESS,
528 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
529 	.sv_usrstack	= LINUX_USRSTACK,
530 	.sv_psstrings	= LINUX_PS_STRINGS,
531 	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
532 	.sv_copyout_auxargs = linux_copyout_auxargs,
533 	.sv_copyout_strings = linux_copyout_strings,
534 	.sv_setregs	= linux_exec_setregs,
535 	.sv_fixlimit	= NULL,
536 	.sv_maxssiz	= NULL,
537 	.sv_flags	= SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
538 	    SV_SIG_WAITNDQ | SV_TIMEKEEP,
539 	.sv_set_syscall_retval = linux_set_syscall_retval,
540 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
541 	.sv_syscallnames = NULL,
542 	.sv_shared_page_base = LINUX_SHAREDPAGE,
543 	.sv_shared_page_len = PAGE_SIZE,
544 	.sv_schedtail	= linux_schedtail,
545 	.sv_thread_detach = linux_thread_detach,
546 	.sv_trap	= NULL,
547 	.sv_hwcap	= &elf_hwcap,
548 	.sv_hwcap2	= &elf_hwcap2,
549 	.sv_onexec	= linux_on_exec_vmspace,
550 	.sv_onexit	= linux_on_exit,
551 	.sv_ontdexit	= linux_thread_dtor,
552 	.sv_setid_allowed = &linux_setid_allowed_query,
553 };
554 
555 static int
556 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
557 {
558 	int error;
559 
560 	error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
561 	    LINUX_VDSOPAGE_SIZE, imgp);
562 	if (error == 0)
563 		linux_on_exec(p, imgp);
564 	return (error);
565 }
566 
567 /*
568  * linux_vdso_install() and linux_exec_sysvec_init() must be called
569  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
570  */
571 static void
572 linux_exec_sysvec_init(void *param)
573 {
574 	l_uintptr_t *ktimekeep_base;
575 	struct sysentvec *sv;
576 	ptrdiff_t tkoff;
577 
578 	sv = param;
579 	/* Fill timekeep_base */
580 	exec_sysvec_init(sv);
581 
582 	tkoff = kern_timekeep_base - linux_vdso_base;
583 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
584 	*ktimekeep_base = sv->sv_timekeep_base;
585 }
586 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
587     linux_exec_sysvec_init, &elf_linux_sysvec);
588 
589 static void
590 linux_vdso_install(const void *param)
591 {
592 	char *vdso_start = &_binary_linux_vdso_so_o_start;
593 	char *vdso_end = &_binary_linux_vdso_so_o_end;
594 
595 	linux_szsigcode = vdso_end - vdso_start;
596 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
597 
598 	linux_vdso_base = LINUX_VDSOPAGE;
599 
600 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
601 
602 	linux_vdso_obj = __elfN(linux_shared_page_init)
603 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
604 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
605 
606 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
607 }
608 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
609     linux_vdso_install, NULL);
610 
611 static void
612 linux_vdso_deinstall(const void *param)
613 {
614 
615 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
616 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
617 }
618 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
619     linux_vdso_deinstall, NULL);
620 
621 static void
622 linux_vdso_reloc(char *mapping, Elf_Addr offset)
623 {
624 	Elf_Size rtype, symidx;
625 	const Elf_Rela *rela;
626 	const Elf_Shdr *shdr;
627 	const Elf_Ehdr *ehdr;
628 	Elf_Addr *where;
629 	Elf_Addr addr, addend;
630 	int i, relacnt;
631 
632 	MPASS(offset != 0);
633 
634 	relacnt = 0;
635 	ehdr = (const Elf_Ehdr *)mapping;
636 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
637 	for (i = 0; i < ehdr->e_shnum; i++)
638 	{
639 		switch (shdr[i].sh_type) {
640 		case SHT_REL:
641 			printf("Linux Aarch64 vDSO: unexpected Rel section\n");
642 			break;
643 		case SHT_RELA:
644 			rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
645 			relacnt = shdr[i].sh_size / sizeof(*rela);
646 		}
647 	}
648 
649 	for (i = 0; i < relacnt; i++, rela++) {
650 		where = (Elf_Addr *)(mapping + rela->r_offset);
651 		addend = rela->r_addend;
652 		rtype = ELF_R_TYPE(rela->r_info);
653 		symidx = ELF_R_SYM(rela->r_info);
654 
655 		switch (rtype) {
656 		case R_AARCH64_NONE:	/* none */
657 			break;
658 
659 		case R_AARCH64_RELATIVE:	/* B + A */
660 			addr = (Elf_Addr)(mapping + addend);
661 			if (*where != addr)
662 				*where = addr;
663 			break;
664 		default:
665 			printf("Linux Aarch64 vDSO: unexpected relocation type %ld, "
666 			    "symbol index %ld\n", rtype, symidx);
667 		}
668 	}
669 }
670 
671 static char GNU_ABI_VENDOR[] = "GNU";
672 static int GNU_ABI_LINUX = 0;
673 
674 /* LINUXTODO: deduplicate */
675 static bool
676 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
677 {
678 	const Elf32_Word *desc;
679 	uintptr_t p;
680 
681 	p = (uintptr_t)(note + 1);
682 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
683 
684 	desc = (const Elf32_Word *)p;
685 	if (desc[0] != GNU_ABI_LINUX)
686 		return (false);
687 
688 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
689 	return (true);
690 }
691 
692 static Elf_Brandnote linux64_brandnote = {
693 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
694 	.hdr.n_descsz	= 16,
695 	.hdr.n_type	= 1,
696 	.vendor		= GNU_ABI_VENDOR,
697 	.flags		= BN_TRANSLATE_OSREL,
698 	.trans_osrel	= linux_trans_osrel
699 };
700 
701 static Elf64_Brandinfo linux_glibc2brand = {
702 	.brand		= ELFOSABI_LINUX,
703 	.machine	= EM_AARCH64,
704 	.compat_3_brand	= "Linux",
705 	.emul_path	= linux_emul_path,
706 	.interp_path	= "/lib64/ld-linux-x86-64.so.2",
707 	.sysvec		= &elf_linux_sysvec,
708 	.interp_newpath	= NULL,
709 	.brand_note	= &linux64_brandnote,
710 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
711 };
712 
713 Elf64_Brandinfo *linux_brandlist[] = {
714 	&linux_glibc2brand,
715 	NULL
716 };
717 
718 static int
719 linux64_elf_modevent(module_t mod, int type, void *data)
720 {
721 	Elf64_Brandinfo **brandinfo;
722 	struct linux_ioctl_handler**lihp;
723 	int error;
724 
725 	error = 0;
726 	switch(type) {
727 	case MOD_LOAD:
728 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
729 		    ++brandinfo)
730 			if (elf64_insert_brand_entry(*brandinfo) < 0)
731 				error = EINVAL;
732 		if (error == 0) {
733 			SET_FOREACH(lihp, linux_ioctl_handler_set)
734 				linux_ioctl_register_handler(*lihp);
735 			stclohz = (stathz ? stathz : hz);
736 			if (bootverbose)
737 				printf("Linux arm64 ELF exec handler installed\n");
738 		}
739 		break;
740 	case MOD_UNLOAD:
741 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
742 		    ++brandinfo)
743 			if (elf64_brand_inuse(*brandinfo))
744 				error = EBUSY;
745 		if (error == 0) {
746 			for (brandinfo = &linux_brandlist[0];
747 			    *brandinfo != NULL; ++brandinfo)
748 				if (elf64_remove_brand_entry(*brandinfo) < 0)
749 					error = EINVAL;
750 		}
751 		if (error == 0) {
752 			SET_FOREACH(lihp, linux_ioctl_handler_set)
753 				linux_ioctl_unregister_handler(*lihp);
754 			if (bootverbose)
755 				printf("Linux arm64 ELF exec handler removed\n");
756 		} else
757 			printf("Could not deinstall Linux arm64 ELF interpreter entry\n");
758 		break;
759 	default:
760 		return (EOPNOTSUPP);
761 	}
762 	return (error);
763 }
764 
765 static moduledata_t linux64_elf_mod = {
766 	"linux64elf",
767 	linux64_elf_modevent,
768 	0
769 };
770 
771 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
772 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
773 FEATURE(linux64, "AArch64 Linux 64bit support");
774