xref: /freebsd/sys/arm64/linux/linux_sysvec.c (revision 069ac18495ad8fde2748bc94b0f80a50250bb01d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #define	__ELF_WORD_SIZE	64
30 
31 #include <sys/param.h>
32 #include <sys/elf.h>
33 #include <sys/exec.h>
34 #include <sys/imgact.h>
35 #include <sys/imgact_elf.h>
36 #include <sys/kernel.h>
37 #include <sys/ktr.h>
38 #include <sys/lock.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/stddef.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysctl.h>
45 #include <sys/sysent.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 
50 #include <arm64/linux/linux.h>
51 #include <arm64/linux/linux_proto.h>
52 #include <compat/linux/linux_elf.h>
53 #include <compat/linux/linux_emul.h>
54 #include <compat/linux/linux_fork.h>
55 #include <compat/linux/linux_ioctl.h>
56 #include <compat/linux/linux_mib.h>
57 #include <compat/linux/linux_misc.h>
58 #include <compat/linux/linux_signal.h>
59 #include <compat/linux/linux_util.h>
60 #include <compat/linux/linux_vdso.h>
61 
62 #include <arm64/linux/linux_sigframe.h>
63 
64 #include <machine/md_var.h>
65 #include <machine/pcb.h>
66 #ifdef VFP
67 #include <machine/vfp.h>
68 #endif
69 
70 MODULE_VERSION(linux64elf, 1);
71 
72 #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
73 #define	LINUX_VDSOPAGE		(VM_MAXUSER_ADDRESS - \
74 				    LINUX_VDSOPAGE_SIZE)
75 #define	LINUX_SHAREDPAGE	(LINUX_VDSOPAGE - PAGE_SIZE)
76 				/*
77 				 * PAGE_SIZE - the size
78 				 * of the native SHAREDPAGE
79 				 */
80 #define	LINUX_USRSTACK		LINUX_SHAREDPAGE
81 #define	LINUX_PS_STRINGS	(LINUX_USRSTACK - \
82 				    sizeof(struct ps_strings))
83 
84 static int linux_szsigcode;
85 static vm_object_t linux_vdso_obj;
86 static char *linux_vdso_mapping;
87 extern char _binary_linux_vdso_so_o_start;
88 extern char _binary_linux_vdso_so_o_end;
89 static vm_offset_t linux_vdso_base;
90 
91 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
92 extern const char *linux_syscallnames[];
93 
94 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
95 
96 static void	linux_vdso_install(const void *param);
97 static void	linux_vdso_deinstall(const void *param);
98 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
99 static void	linux_set_syscall_retval(struct thread *td, int error);
100 static int	linux_fetch_syscall_args(struct thread *td);
101 static void	linux_exec_setregs(struct thread *td, struct image_params *imgp,
102 		    uintptr_t stack);
103 static void	linux_exec_sysvec_init(void *param);
104 static int	linux_on_exec_vmspace(struct proc *p,
105 		    struct image_params *imgp);
106 
107 LINUX_VDSO_SYM_CHAR(linux_platform);
108 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
109 LINUX_VDSO_SYM_INTPTR(__user_rt_sigreturn);
110 
111 static int
112 linux_fetch_syscall_args(struct thread *td)
113 {
114 	struct proc *p;
115 	struct syscall_args *sa;
116 	register_t *ap;
117 
118 	p = td->td_proc;
119 	ap = td->td_frame->tf_x;
120 	sa = &td->td_sa;
121 
122 	sa->code = td->td_frame->tf_x[8];
123 	sa->original_code = sa->code;
124 
125 	if (sa->code >= p->p_sysent->sv_size)
126 		sa->callp = &nosys_sysent;
127 	else
128 		sa->callp = &p->p_sysent->sv_table[sa->code];
129 
130 	if (sa->callp->sy_narg > nitems(sa->args))
131 		panic("ARM64TODO: Could we have more than %zu args?",
132 		    nitems(sa->args));
133 	memcpy(sa->args, ap, nitems(sa->args) * sizeof(register_t));
134 
135 	td->td_retval[0] = 0;
136 	return (0);
137 }
138 
139 static void
140 linux_set_syscall_retval(struct thread *td, int error)
141 {
142 
143 	td->td_retval[1] = td->td_frame->tf_x[1];
144 	cpu_set_syscall_retval(td, error);
145 
146 	if (__predict_false(error != 0)) {
147 		if (error != ERESTART && error != EJUSTRETURN)
148 			td->td_frame->tf_x[0] = bsd_to_linux_errno(error);
149 	}
150 }
151 
152 void
153 linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
154 {
155 
156 	AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
157 	AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap);
158 	AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2);
159 	AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
160 }
161 
162 /*
163  * Reset registers to default values on exec.
164  */
165 static void
166 linux_exec_setregs(struct thread *td, struct image_params *imgp,
167     uintptr_t stack)
168 {
169 	struct trapframe *regs = td->td_frame;
170 	struct pcb *pcb = td->td_pcb;
171 
172 	memset(regs, 0, sizeof(*regs));
173 	regs->tf_sp = stack;
174 	regs->tf_elr = imgp->entry_addr;
175 	pcb->pcb_tpidr_el0 = 0;
176 	pcb->pcb_tpidrro_el0 = 0;
177 	WRITE_SPECIALREG(tpidrro_el0, 0);
178 	WRITE_SPECIALREG(tpidr_el0, 0);
179 
180 #ifdef VFP
181 	vfp_reset_state(td, pcb);
182 #endif
183 
184 	/*
185 	 * Clear debug register state. It is not applicable to the new process.
186 	 */
187 	bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
188 }
189 
190 static bool
191 linux_parse_sigreturn_ctx(struct thread *td, struct l_sigcontext *sc)
192 {
193 	struct l_fpsimd_context *fpsimd;
194 	struct _l_aarch64_ctx *ctx;
195 	int offset;
196 
197 	offset = 0;
198 	while (1) {
199 		/* The offset must be 16 byte aligned */
200 		if ((offset & 15) != 0)
201 			return (false);
202 
203 		/* Check for buffer overflow of the ctx */
204 		if ((offset + sizeof(*ctx)) >
205 		    sizeof(sc->__reserved))
206 			return (false);
207 
208 		ctx = (struct _l_aarch64_ctx *)&sc->__reserved[offset];
209 
210 		/* Check for buffer overflow of the data */
211 		if ((offset + ctx->size) > sizeof(sc->__reserved))
212 			return (false);
213 
214 		switch(ctx->magic) {
215 		case 0:
216 			if (ctx->size != 0)
217 				return (false);
218 			return (true);
219 		case L_ESR_MAGIC:
220 			/* Ignore */
221 			break;
222 #ifdef VFP
223 		case L_FPSIMD_MAGIC:
224 			fpsimd = (struct l_fpsimd_context *)ctx;
225 
226 			/*
227 			 * Discard any vfp state for the current thread, we
228 			 * are about to override it.
229 			 */
230 			critical_enter();
231 			vfp_discard(td);
232 			critical_exit();
233 
234 			td->td_pcb->pcb_fpustate.vfp_fpcr = fpsimd->fpcr;
235 			td->td_pcb->pcb_fpustate.vfp_fpsr = fpsimd->fpsr;
236 			memcpy(td->td_pcb->pcb_fpustate.vfp_regs,
237 			    fpsimd->vregs, sizeof(fpsimd->vregs));
238 
239 			break;
240 #endif
241 		default:
242 			return (false);
243 		}
244 
245 		offset += ctx->size;
246 	}
247 
248 }
249 
250 int
251 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
252 {
253 	struct l_rt_sigframe *sf;
254 	struct l_sigframe *frame;
255 	struct trapframe *tf;
256 	sigset_t bmask;
257 	int error;
258 
259 	sf = malloc(sizeof(*sf), M_LINUX, M_WAITOK | M_ZERO);
260 
261 	tf = td->td_frame;
262 	frame = (struct l_sigframe *)tf->tf_sp;
263 	error = copyin((void *)&frame->sf, sf, sizeof(*sf));
264 	if (error != 0) {
265 		free(sf, M_LINUX);
266 		return (error);
267 	}
268 
269 	memcpy(tf->tf_x, sf->sf_uc.uc_sc.regs, sizeof(tf->tf_x));
270 	tf->tf_lr = sf->sf_uc.uc_sc.regs[30];
271 	tf->tf_sp = sf->sf_uc.uc_sc.sp;
272 	tf->tf_elr = sf->sf_uc.uc_sc.pc;
273 
274 	if ((sf->sf_uc.uc_sc.pstate & PSR_M_MASK) != PSR_M_EL0t ||
275 	    (sf->sf_uc.uc_sc.pstate & PSR_AARCH32) != 0 ||
276 	    (sf->sf_uc.uc_sc.pstate & PSR_DAIF) !=
277 	    (td->td_frame->tf_spsr & PSR_DAIF))
278 		goto einval;
279 	tf->tf_spsr = sf->sf_uc.uc_sc.pstate;
280 
281 	if (!linux_parse_sigreturn_ctx(td, &sf->sf_uc.uc_sc))
282 		goto einval;
283 
284 	/* Restore signal mask. */
285 	linux_to_bsd_sigset(&sf->sf_uc.uc_sigmask, &bmask);
286 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
287 	free(sf, M_LINUX);
288 
289 	return (EJUSTRETURN);
290 einval:
291 	free(sf, M_LINUX);
292 	return (EINVAL);
293 }
294 
295 static void
296 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
297 {
298 	struct thread *td;
299 	struct proc *p;
300 	struct trapframe *tf;
301 	struct l_sigframe *fp, *frame;
302 	struct l_fpsimd_context *fpsimd;
303 	struct l_esr_context *esr;
304 	l_stack_t uc_stack;
305 	ucontext_t uc;
306 	uint8_t *scr;
307 	struct sigacts *psp;
308 	int onstack, sig, issiginfo;
309 
310 	td = curthread;
311 	p = td->td_proc;
312 	PROC_LOCK_ASSERT(p, MA_OWNED);
313 
314 	sig = ksi->ksi_signo;
315 	psp = p->p_sigacts;
316 	mtx_assert(&psp->ps_mtx, MA_OWNED);
317 
318 	tf = td->td_frame;
319 	onstack = sigonstack(tf->tf_sp);
320 	issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
321 
322 	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
323 	    catcher, sig);
324 
325 	/* Allocate and validate space for the signal handler context. */
326 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
327 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
328 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
329 		    td->td_sigstk.ss_size);
330 #if defined(COMPAT_43)
331 		td->td_sigstk.ss_flags |= SS_ONSTACK;
332 #endif
333 	} else {
334 		fp = (struct l_sigframe *)td->td_frame->tf_sp;
335 	}
336 
337 	/* Make room, keeping the stack aligned */
338 	fp--;
339 	fp = (struct l_sigframe *)STACKALIGN(fp);
340 
341 	get_mcontext(td, &uc.uc_mcontext, 0);
342 	uc.uc_sigmask = *mask;
343 
344 	uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
345 	uc_stack.ss_size = td->td_sigstk.ss_size;
346 	uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
347 	    (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
348 	mtx_unlock(&psp->ps_mtx);
349 	PROC_UNLOCK(td->td_proc);
350 
351 	/* Fill in the frame to copy out */
352 	frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO);
353 
354 	memcpy(&frame->sf.sf_uc.uc_sc.regs, tf->tf_x, sizeof(tf->tf_x));
355 	frame->sf.sf_uc.uc_sc.regs[30] = tf->tf_lr;
356 	frame->sf.sf_uc.uc_sc.sp = tf->tf_sp;
357 	frame->sf.sf_uc.uc_sc.pc = tf->tf_elr;
358 	frame->sf.sf_uc.uc_sc.pstate = tf->tf_spsr;
359 	frame->sf.sf_uc.uc_sc.fault_address = (register_t)ksi->ksi_addr;
360 
361 	/* Stack frame for unwinding */
362 	frame->fp = tf->tf_x[29];
363 	frame->lr = tf->tf_elr;
364 
365 	/* Translate the signal. */
366 	sig = bsd_to_linux_signal(sig);
367 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame->sf.sf_si, sig);
368 	bsd_to_linux_sigset(mask, &frame->sf.sf_uc.uc_sigmask);
369 
370 	/*
371 	 * Prepare fpsimd & esr. Does not check sizes, as
372 	 * __reserved is big enougth.
373 	 */
374 	scr = (uint8_t *)&frame->sf.sf_uc.uc_sc.__reserved;
375 #ifdef VFP
376 	fpsimd = (struct l_fpsimd_context *) scr;
377 	fpsimd->head.magic = L_FPSIMD_MAGIC;
378 	fpsimd->head.size = sizeof(struct l_fpsimd_context);
379 	fpsimd->fpsr = uc.uc_mcontext.mc_fpregs.fp_sr;
380 	fpsimd->fpcr = uc.uc_mcontext.mc_fpregs.fp_cr;
381 
382 	memcpy(fpsimd->vregs, &uc.uc_mcontext.mc_fpregs.fp_q,
383 	    sizeof(uc.uc_mcontext.mc_fpregs.fp_q));
384 	scr += roundup(sizeof(struct l_fpsimd_context), 16);
385 #endif
386 	if (ksi->ksi_addr != 0) {
387 		esr = (struct l_esr_context *) scr;
388 		esr->head.magic = L_ESR_MAGIC;
389 		esr->head.size = sizeof(struct l_esr_context);
390 		esr->esr = tf->tf_esr;
391 	}
392 
393 	memcpy(&frame->sf.sf_uc.uc_stack, &uc_stack, sizeof(uc_stack));
394 
395 	/* Copy the sigframe out to the user's stack. */
396 	if (copyout(frame, fp, sizeof(*fp)) != 0) {
397 		/* Process has trashed its stack. Kill it. */
398 		free(frame, M_LINUX);
399 		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
400 		PROC_LOCK(p);
401 		sigexit(td, SIGILL);
402 	}
403 	free(frame, M_LINUX);
404 
405 	tf->tf_x[0]= sig;
406 	if (issiginfo) {
407 		tf->tf_x[1] = (register_t)&fp->sf.sf_si;
408 		tf->tf_x[2] = (register_t)&fp->sf.sf_uc;
409 	} else {
410 		tf->tf_x[1] = 0;
411 		tf->tf_x[2] = 0;
412 	}
413 	tf->tf_x[29] = (register_t)&fp->fp;
414 	tf->tf_elr = (register_t)catcher;
415 	tf->tf_sp = (register_t)fp;
416 	tf->tf_lr = (register_t)__user_rt_sigreturn;
417 
418 	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
419 	    tf->tf_sp);
420 
421 	PROC_LOCK(p);
422 	mtx_lock(&psp->ps_mtx);
423 }
424 
425 struct sysentvec elf_linux_sysvec = {
426 	.sv_size	= LINUX_SYS_MAXSYSCALL,
427 	.sv_table	= linux_sysent,
428 	.sv_fixup	= __elfN(freebsd_fixup),
429 	.sv_sendsig	= linux_rt_sendsig,
430 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
431 	.sv_szsigcode	= &linux_szsigcode,
432 	.sv_name	= "Linux ELF64",
433 	.sv_coredump	= elf64_coredump,
434 	.sv_elf_core_osabi = ELFOSABI_NONE,
435 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
436 	.sv_elf_core_prepare_notes = linux64_prepare_notes,
437 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
438 	.sv_minuser	= VM_MIN_ADDRESS,
439 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
440 	.sv_usrstack	= LINUX_USRSTACK,
441 	.sv_psstrings	= LINUX_PS_STRINGS,
442 	.sv_psstringssz	= sizeof(struct ps_strings),
443 	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
444 	.sv_copyout_auxargs = __linuxN(copyout_auxargs),
445 	.sv_copyout_strings = __linuxN(copyout_strings),
446 	.sv_setregs	= linux_exec_setregs,
447 	.sv_fixlimit	= NULL,
448 	.sv_maxssiz	= NULL,
449 	.sv_flags	= SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
450 	    SV_SIG_WAITNDQ | SV_TIMEKEEP,
451 	.sv_set_syscall_retval = linux_set_syscall_retval,
452 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
453 	.sv_syscallnames = linux_syscallnames,
454 	.sv_shared_page_base = LINUX_SHAREDPAGE,
455 	.sv_shared_page_len = PAGE_SIZE,
456 	.sv_schedtail	= linux_schedtail,
457 	.sv_thread_detach = linux_thread_detach,
458 	.sv_trap	= NULL,
459 	.sv_hwcap	= &linux_elf_hwcap,
460 	.sv_hwcap2	= &linux_elf_hwcap2,
461 	.sv_onexec	= linux_on_exec_vmspace,
462 	.sv_onexit	= linux_on_exit,
463 	.sv_ontdexit	= linux_thread_dtor,
464 	.sv_setid_allowed = &linux_setid_allowed_query,
465 };
466 
467 static int
468 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
469 {
470 	int error;
471 
472 	error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
473 	    LINUX_VDSOPAGE_SIZE, imgp);
474 	if (error == 0)
475 		error = linux_on_exec(p, imgp);
476 	return (error);
477 }
478 
479 /*
480  * linux_vdso_install() and linux_exec_sysvec_init() must be called
481  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
482  */
483 static void
484 linux_exec_sysvec_init(void *param)
485 {
486 	l_uintptr_t *ktimekeep_base;
487 	struct sysentvec *sv;
488 	ptrdiff_t tkoff;
489 
490 	sv = param;
491 	/* Fill timekeep_base */
492 	exec_sysvec_init(sv);
493 
494 	tkoff = kern_timekeep_base - linux_vdso_base;
495 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
496 	*ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
497 }
498 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
499     linux_exec_sysvec_init, &elf_linux_sysvec);
500 
501 static void
502 linux_vdso_install(const void *param)
503 {
504 	char *vdso_start = &_binary_linux_vdso_so_o_start;
505 	char *vdso_end = &_binary_linux_vdso_so_o_end;
506 
507 	linux_szsigcode = vdso_end - vdso_start;
508 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
509 
510 	linux_vdso_base = LINUX_VDSOPAGE;
511 
512 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
513 
514 	linux_vdso_obj = __elfN(linux_shared_page_init)
515 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
516 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
517 
518 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
519 }
520 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
521     linux_vdso_install, NULL);
522 
523 static void
524 linux_vdso_deinstall(const void *param)
525 {
526 
527 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
528 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
529 }
530 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
531     linux_vdso_deinstall, NULL);
532 
533 static void
534 linux_vdso_reloc(char *mapping, Elf_Addr offset)
535 {
536 	Elf_Size rtype, symidx;
537 	const Elf_Rela *rela;
538 	const Elf_Shdr *shdr;
539 	const Elf_Ehdr *ehdr;
540 	Elf_Addr *where;
541 	Elf_Addr addr, addend;
542 	int i, relacnt;
543 
544 	MPASS(offset != 0);
545 
546 	relacnt = 0;
547 	ehdr = (const Elf_Ehdr *)mapping;
548 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
549 	for (i = 0; i < ehdr->e_shnum; i++)
550 	{
551 		switch (shdr[i].sh_type) {
552 		case SHT_REL:
553 			printf("Linux Aarch64 vDSO: unexpected Rel section\n");
554 			break;
555 		case SHT_RELA:
556 			rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
557 			relacnt = shdr[i].sh_size / sizeof(*rela);
558 		}
559 	}
560 
561 	for (i = 0; i < relacnt; i++, rela++) {
562 		where = (Elf_Addr *)(mapping + rela->r_offset);
563 		addend = rela->r_addend;
564 		rtype = ELF_R_TYPE(rela->r_info);
565 		symidx = ELF_R_SYM(rela->r_info);
566 
567 		switch (rtype) {
568 		case R_AARCH64_NONE:	/* none */
569 			break;
570 
571 		case R_AARCH64_RELATIVE:	/* B + A */
572 			addr = (Elf_Addr)(mapping + addend);
573 			if (*where != addr)
574 				*where = addr;
575 			break;
576 		default:
577 			printf("Linux Aarch64 vDSO: unexpected relocation type %ld, "
578 			    "symbol index %ld\n", rtype, symidx);
579 		}
580 	}
581 }
582 
583 static Elf_Brandnote linux64_brandnote = {
584 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
585 	.hdr.n_descsz	= 16,
586 	.hdr.n_type	= 1,
587 	.vendor		= GNU_ABI_VENDOR,
588 	.flags		= BN_TRANSLATE_OSREL,
589 	.trans_osrel	= linux_trans_osrel
590 };
591 
592 static Elf64_Brandinfo linux_glibc2brand = {
593 	.brand		= ELFOSABI_LINUX,
594 	.machine	= EM_AARCH64,
595 	.compat_3_brand	= "Linux",
596 	.interp_path	= "/lib64/ld-linux-x86-64.so.2",
597 	.sysvec		= &elf_linux_sysvec,
598 	.interp_newpath	= NULL,
599 	.brand_note	= &linux64_brandnote,
600 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
601 };
602 
603 Elf64_Brandinfo *linux_brandlist[] = {
604 	&linux_glibc2brand,
605 	NULL
606 };
607 
608 static int
609 linux64_elf_modevent(module_t mod, int type, void *data)
610 {
611 	Elf64_Brandinfo **brandinfo;
612 	struct linux_ioctl_handler**lihp;
613 	int error;
614 
615 	error = 0;
616 	switch(type) {
617 	case MOD_LOAD:
618 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
619 		    ++brandinfo)
620 			if (elf64_insert_brand_entry(*brandinfo) < 0)
621 				error = EINVAL;
622 		if (error == 0) {
623 			SET_FOREACH(lihp, linux_ioctl_handler_set)
624 				linux_ioctl_register_handler(*lihp);
625 			stclohz = (stathz ? stathz : hz);
626 			if (bootverbose)
627 				printf("Linux arm64 ELF exec handler installed\n");
628 		}
629 		break;
630 	case MOD_UNLOAD:
631 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
632 		    ++brandinfo)
633 			if (elf64_brand_inuse(*brandinfo))
634 				error = EBUSY;
635 		if (error == 0) {
636 			for (brandinfo = &linux_brandlist[0];
637 			    *brandinfo != NULL; ++brandinfo)
638 				if (elf64_remove_brand_entry(*brandinfo) < 0)
639 					error = EINVAL;
640 		}
641 		if (error == 0) {
642 			SET_FOREACH(lihp, linux_ioctl_handler_set)
643 				linux_ioctl_unregister_handler(*lihp);
644 			if (bootverbose)
645 				printf("Linux arm64 ELF exec handler removed\n");
646 		} else
647 			printf("Could not deinstall Linux arm64 ELF interpreter entry\n");
648 		break;
649 	default:
650 		return (EOPNOTSUPP);
651 	}
652 	return (error);
653 }
654 
655 static moduledata_t linux64_elf_mod = {
656 	"linux64elf",
657 	linux64_elf_modevent,
658 	0
659 };
660 
661 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
662 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
663 FEATURE(linux64, "AArch64 Linux 64bit support");
664