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