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