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