xref: /freebsd/sys/arm64/linux/linux_sysvec.c (revision 3f9d360c82e0724bfb61346038236bf15c5d4d84)
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/lock.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44 #include <sys/stddef.h>
45 #include <sys/signalvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/sysent.h>
48 
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_param.h>
56 
57 #include <arm64/linux/linux.h>
58 #include <arm64/linux/linux_proto.h>
59 #include <compat/linux/linux_dtrace.h>
60 #include <compat/linux/linux_emul.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_util.h>
65 #include <compat/linux/linux_vdso.h>
66 
67 #include <machine/md_var.h>
68 
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 
96 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
97 
98 static int	linux_copyout_strings(struct image_params *imgp,
99 		    uintptr_t *stack_base);
100 static int	linux_elf_fixup(uintptr_t *stack_base,
101 		    struct image_params *iparams);
102 static bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
103 static void	linux_vdso_install(const void *param);
104 static void	linux_vdso_deinstall(const void *param);
105 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
106 static void	linux_set_syscall_retval(struct thread *td, int error);
107 static int	linux_fetch_syscall_args(struct thread *td);
108 static void	linux_exec_setregs(struct thread *td, struct image_params *imgp,
109 		    uintptr_t stack);
110 static void	linux_exec_sysvec_init(void *param);
111 static int	linux_on_exec_vmspace(struct proc *p,
112 		    struct image_params *imgp);
113 
114 /* DTrace init */
115 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
116 
117 /* DTrace probes */
118 LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int");
119 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo);
120 LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo);
121 LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo);
122 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo);
123 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo);
124 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo);
125 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo);
126 
127 LINUX_VDSO_SYM_CHAR(linux_platform);
128 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
129 LINUX_VDSO_SYM_INTPTR(__kernel_rt_sigreturn);
130 
131 /* LINUXTODO: do we have traps to translate? */
132 static int
133 linux_translate_traps(int signal, int trap_code)
134 {
135 
136 	LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code);
137 	return (signal);
138 }
139 
140 static int
141 linux_fetch_syscall_args(struct thread *td)
142 {
143 	struct proc *p;
144 	struct syscall_args *sa;
145 	register_t *ap;
146 
147 	p = td->td_proc;
148 	ap = td->td_frame->tf_x;
149 	sa = &td->td_sa;
150 
151 	sa->code = td->td_frame->tf_x[8];
152 	sa->original_code = sa->code;
153 	/* LINUXTODO: generic syscall? */
154 	if (sa->code >= p->p_sysent->sv_size)
155 		sa->callp = &p->p_sysent->sv_table[0];
156 	else
157 		sa->callp = &p->p_sysent->sv_table[sa->code];
158 
159 	if (sa->callp->sy_narg > MAXARGS)
160 		panic("ARM64TODO: Could we have more than %d args?", MAXARGS);
161 	memcpy(sa->args, ap, MAXARGS * sizeof(register_t));
162 
163 	td->td_retval[0] = 0;
164 	return (0);
165 }
166 
167 static void
168 linux_set_syscall_retval(struct thread *td, int error)
169 {
170 
171 	td->td_retval[1] = td->td_frame->tf_x[1];
172 	cpu_set_syscall_retval(td, error);
173 
174 	if (__predict_false(error != 0)) {
175 		if (error != ERESTART && error != EJUSTRETURN)
176 			td->td_frame->tf_x[0] = bsd_to_linux_errno(error);
177 	}
178 }
179 
180 static int
181 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
182 {
183 	Elf_Auxargs *args;
184 	Elf_Auxinfo *argarray, *pos;
185 	struct proc *p;
186 	int error, issetugid;
187 
188 	LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo);
189 	p = imgp->proc;
190 
191 	args = (Elf64_Auxargs *)imgp->auxargs;
192 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
193 	    M_WAITOK | M_ZERO);
194 
195 	issetugid = p->p_flag & P_SUGID ? 1 : 0;
196 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
197 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap);
198 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
199 	AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
200 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
201 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
202 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
203 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
204 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
205 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
206 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
207 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
208 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
209 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
210 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
211 	AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
212 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2);
213 	if (imgp->execpathp != 0)
214 		AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
215 	if (args->execfd != -1)
216 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
217 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
218 	AUXARGS_ENTRY(pos, AT_NULL, 0);
219 
220 	free(imgp->auxargs, M_TEMP);
221 	imgp->auxargs = NULL;
222 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
223 
224 	error = copyout(argarray, (void *)base,
225 	    sizeof(*argarray) * LINUX_AT_COUNT);
226 	free(argarray, M_TEMP);
227 	return (error);
228 }
229 
230 static int
231 linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp)
232 {
233 
234 	LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo);
235 
236 	return (0);
237 }
238 
239 /*
240  * Copy strings out to the new process address space, constructing new arg
241  * and env vector tables. Return a pointer to the base so that it can be used
242  * as the initial stack pointer.
243  * LINUXTODO: deduplicate against other linuxulator archs
244  */
245 static int
246 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
247 {
248 	char **vectp;
249 	char *stringp;
250 	uintptr_t destp, ustringp;
251 	struct ps_strings *arginfo;
252 	char canary[LINUX_AT_RANDOM_LEN];
253 	size_t execpath_len;
254 	struct proc *p;
255 	int argc, envc, error;
256 
257 	/* Calculate string base and vector table pointers. */
258 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
259 		execpath_len = strlen(imgp->execpath) + 1;
260 	else
261 		execpath_len = 0;
262 
263 	p = imgp->proc;
264 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
265 	destp = (uintptr_t)arginfo;
266 
267 	if (execpath_len != 0) {
268 		destp -= execpath_len;
269 		destp = rounddown2(destp, sizeof(void *));
270 		imgp->execpathp = (void *)destp;
271 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
272 		if (error != 0)
273 			return (error);
274 	}
275 
276 	/* Prepare the canary for SSP. */
277 	arc4rand(canary, sizeof(canary), 0);
278 	destp -= roundup(sizeof(canary), sizeof(void *));
279 	imgp->canary = (void *)destp;
280 	error = copyout(canary, imgp->canary, sizeof(canary));
281 	if (error != 0)
282 		return (error);
283 
284 	/* Allocate room for the argument and environment strings. */
285 	destp -= ARG_MAX - imgp->args->stringspace;
286 	destp = rounddown2(destp, sizeof(void *));
287 	ustringp = destp;
288 
289 	if (imgp->auxargs) {
290 		/*
291 		 * Allocate room on the stack for the ELF auxargs
292 		 * array.  It has up to LINUX_AT_COUNT entries.
293 		 */
294 		destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo);
295 		destp = rounddown2(destp, sizeof(void *));
296 	}
297 
298 	vectp = (char **)destp;
299 
300 	/*
301 	 * Allocate room for argc and the argv[] and env vectors including the
302 	 * terminating NULL pointers.
303 	 */
304 	vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1;
305 	vectp = (char **)STACKALIGN(vectp);
306 
307 	/* vectp also becomes our initial stack base. */
308 	*stack_base = (uintptr_t)vectp;
309 
310 	stringp = imgp->args->begin_argv;
311 	argc = imgp->args->argc;
312 	envc = imgp->args->envc;
313 
314 	/* Copy out strings - arguments and environment. */
315 	error = copyout(stringp, (void *)ustringp,
316 	    ARG_MAX - imgp->args->stringspace);
317 	if (error != 0)
318 		return (error);
319 
320 	/* Fill in "ps_strings" struct for ps, w, etc. */
321 	if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
322 	    suword(&arginfo->ps_nargvstr, argc) != 0)
323 		return (EFAULT);
324 
325 	if (suword(vectp++, argc) != 0)
326 		return (EFAULT);
327 
328 	/* Fill in argument portion of vector table. */
329 	for (; argc > 0; --argc) {
330 		if (suword(vectp++, ustringp) != 0)
331 			return (EFAULT);
332 		while (*stringp++ != 0)
333 			ustringp++;
334 		ustringp++;
335 	}
336 
337 	/* A null vector table pointer separates the argp's from the envp's. */
338 	if (suword(vectp++, 0) != 0)
339 		return (EFAULT);
340 
341 	if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
342 	    suword(&arginfo->ps_nenvstr, envc) != 0)
343 		return (EFAULT);
344 
345 	/* Fill in environment portion of vector table. */
346 	for (; envc > 0; --envc) {
347 		if (suword(vectp++, ustringp) != 0)
348 			return (EFAULT);
349 		while (*stringp++ != 0)
350 			ustringp++;
351 		ustringp++;
352 	}
353 
354 	/* The end of the vector table is a null pointer. */
355 	if (suword(vectp, 0) != 0)
356 		return (EFAULT);
357 
358 	if (imgp->auxargs) {
359 		vectp++;
360 		error = imgp->sysent->sv_copyout_auxargs(imgp,
361 		    (uintptr_t)vectp);
362 		if (error != 0)
363 			return (error);
364 	}
365 
366 	return (0);
367 }
368 
369 /*
370  * Reset registers to default values on exec.
371  */
372 static void
373 linux_exec_setregs(struct thread *td, struct image_params *imgp,
374     uintptr_t stack)
375 {
376 	struct trapframe *regs = td->td_frame;
377 	struct pcb *pcb = td->td_pcb;
378 
379 	/* LINUXTODO: validate */
380 	LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo);
381 
382 	memset(regs, 0, sizeof(*regs));
383 	/* glibc start.S registers function pointer in x0 with atexit. */
384         regs->tf_sp = stack;
385 #if 0	/* LINUXTODO: See if this is used. */
386 	regs->tf_lr = imgp->entry_addr;
387 #else
388         regs->tf_lr = 0xffffffffffffffff;
389 #endif
390         regs->tf_elr = imgp->entry_addr;
391 
392 	pcb->pcb_tpidr_el0 = 0;
393 	pcb->pcb_tpidrro_el0 = 0;
394 	WRITE_SPECIALREG(tpidrro_el0, 0);
395 	WRITE_SPECIALREG(tpidr_el0, 0);
396 
397 #ifdef VFP
398 	vfp_reset_state(td, pcb);
399 #endif
400 
401 	/*
402 	 * Clear debug register state. It is not applicable to the new process.
403 	 */
404 	bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
405 }
406 
407 int
408 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
409 {
410 
411 	/* LINUXTODO: implement */
412 	LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo);
413 	return (EDOOFUS);
414 }
415 
416 static void
417 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
418 {
419 
420 	/* LINUXTODO: implement */
421 	LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo);
422 }
423 
424 struct sysentvec elf_linux_sysvec = {
425 	.sv_size	= LINUX_SYS_MAXSYSCALL,
426 	.sv_table	= linux_sysent,
427 	.sv_transtrap	= linux_translate_traps,
428 	.sv_fixup	= linux_elf_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_imgact_try	= linux_exec_imgact_try,
438 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
439 	.sv_minuser	= VM_MIN_ADDRESS,
440 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
441 	.sv_usrstack	= LINUX_USRSTACK,
442 	.sv_psstrings	= LINUX_PS_STRINGS,
443 	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
444 	.sv_copyout_auxargs = linux_copyout_auxargs,
445 	.sv_copyout_strings = linux_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 = NULL,
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	= &elf_hwcap,
460 	.sv_hwcap2	= &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 		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_timekeep_base;
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 char GNU_ABI_VENDOR[] = "GNU";
584 static int GNU_ABI_LINUX = 0;
585 
586 /* LINUXTODO: deduplicate */
587 static bool
588 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
589 {
590 	const Elf32_Word *desc;
591 	uintptr_t p;
592 
593 	p = (uintptr_t)(note + 1);
594 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
595 
596 	desc = (const Elf32_Word *)p;
597 	if (desc[0] != GNU_ABI_LINUX)
598 		return (false);
599 
600 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
601 	return (true);
602 }
603 
604 static Elf_Brandnote linux64_brandnote = {
605 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
606 	.hdr.n_descsz	= 16,
607 	.hdr.n_type	= 1,
608 	.vendor		= GNU_ABI_VENDOR,
609 	.flags		= BN_TRANSLATE_OSREL,
610 	.trans_osrel	= linux_trans_osrel
611 };
612 
613 static Elf64_Brandinfo linux_glibc2brand = {
614 	.brand		= ELFOSABI_LINUX,
615 	.machine	= EM_AARCH64,
616 	.compat_3_brand	= "Linux",
617 	.emul_path	= linux_emul_path,
618 	.interp_path	= "/lib64/ld-linux-x86-64.so.2",
619 	.sysvec		= &elf_linux_sysvec,
620 	.interp_newpath	= NULL,
621 	.brand_note	= &linux64_brandnote,
622 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
623 };
624 
625 Elf64_Brandinfo *linux_brandlist[] = {
626 	&linux_glibc2brand,
627 	NULL
628 };
629 
630 static int
631 linux64_elf_modevent(module_t mod, int type, void *data)
632 {
633 	Elf64_Brandinfo **brandinfo;
634 	struct linux_ioctl_handler**lihp;
635 	int error;
636 
637 	error = 0;
638 	switch(type) {
639 	case MOD_LOAD:
640 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
641 		    ++brandinfo)
642 			if (elf64_insert_brand_entry(*brandinfo) < 0)
643 				error = EINVAL;
644 		if (error == 0) {
645 			SET_FOREACH(lihp, linux_ioctl_handler_set)
646 				linux_ioctl_register_handler(*lihp);
647 			stclohz = (stathz ? stathz : hz);
648 			if (bootverbose)
649 				printf("Linux arm64 ELF exec handler installed\n");
650 		}
651 		break;
652 	case MOD_UNLOAD:
653 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
654 		    ++brandinfo)
655 			if (elf64_brand_inuse(*brandinfo))
656 				error = EBUSY;
657 		if (error == 0) {
658 			for (brandinfo = &linux_brandlist[0];
659 			    *brandinfo != NULL; ++brandinfo)
660 				if (elf64_remove_brand_entry(*brandinfo) < 0)
661 					error = EINVAL;
662 		}
663 		if (error == 0) {
664 			SET_FOREACH(lihp, linux_ioctl_handler_set)
665 				linux_ioctl_unregister_handler(*lihp);
666 			if (bootverbose)
667 				printf("Linux arm64 ELF exec handler removed\n");
668 		} else
669 			printf("Could not deinstall Linux arm64 ELF interpreter entry\n");
670 		break;
671 	default:
672 		return (EOPNOTSUPP);
673 	}
674 	return (error);
675 }
676 
677 static moduledata_t linux64_elf_mod = {
678 	"linux64elf",
679 	linux64_elf_modevent,
680 	0
681 };
682 
683 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
684 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
685 FEATURE(linux64, "AArch64 Linux 64bit support");
686