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