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