xref: /freebsd/sys/arm64/linux/linux_sysvec.c (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
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;
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 
446 	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
447 	    catcher, sig);
448 
449 	/* Allocate and validate space for the signal handler context. */
450 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
451 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
452 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
453 		    td->td_sigstk.ss_size);
454 #if defined(COMPAT_43)
455 		td->td_sigstk.ss_flags |= SS_ONSTACK;
456 #endif
457 	} else {
458 		fp = (struct l_sigframe *)td->td_frame->tf_sp;
459 	}
460 
461 	/* Make room, keeping the stack aligned */
462 	fp--;
463 	fp = (struct l_sigframe *)STACKALIGN(fp);
464 
465 	get_mcontext(td, &uc.uc_mcontext, 0);
466 	uc.uc_sigmask = *mask;
467 
468 	uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
469 	uc_stack.ss_size = td->td_sigstk.ss_size;
470 	uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
471 	    (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
472 	mtx_unlock(&psp->ps_mtx);
473 	PROC_UNLOCK(td->td_proc);
474 
475 	/* Fill in the frame to copy out */
476 	frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO);
477 
478 	memcpy(&frame->sf.sf_uc.uc_sc.regs, tf->tf_x, sizeof(tf->tf_x));
479 	frame->sf.sf_uc.uc_sc.regs[30] = tf->tf_lr;
480 	frame->sf.sf_uc.uc_sc.sp = tf->tf_sp;
481 	frame->sf.sf_uc.uc_sc.pc = tf->tf_lr;
482 	frame->sf.sf_uc.uc_sc.pstate = tf->tf_spsr;
483 	frame->sf.sf_uc.uc_sc.fault_address = (register_t)ksi->ksi_addr;
484 
485 	/* Stack frame for unwinding */
486 	frame->fp = tf->tf_x[29];
487 	frame->lr = tf->tf_lr;
488 
489 	/* Translate the signal. */
490 	sig = bsd_to_linux_signal(sig);
491 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame->sf.sf_si, sig);
492 	bsd_to_linux_sigset(mask, &frame->sf.sf_uc.uc_sigmask);
493 
494 	/*
495 	 * Prepare fpsimd & esr. Does not check sizes, as
496 	 * __reserved is big enougth.
497 	 */
498 	scr = (uint8_t *)&frame->sf.sf_uc.uc_sc.__reserved;
499 #ifdef VFP
500 	fpsimd = (struct l_fpsimd_context *) scr;
501 	fpsimd->head.magic = L_FPSIMD_MAGIC;
502 	fpsimd->head.size = sizeof(struct l_fpsimd_context);
503 	fpsimd->fpsr = uc.uc_mcontext.mc_fpregs.fp_sr;
504 	fpsimd->fpcr = uc.uc_mcontext.mc_fpregs.fp_cr;
505 
506 	memcpy(fpsimd->vregs, &uc.uc_mcontext.mc_fpregs.fp_q,
507 	    sizeof(uc.uc_mcontext.mc_fpregs.fp_q));
508 	scr += roundup(sizeof(struct l_fpsimd_context), 16);
509 #endif
510 	if (ksi->ksi_addr != 0) {
511 		esr = (struct l_esr_context *) scr;
512 		esr->head.magic = L_ESR_MAGIC;
513 		esr->head.size = sizeof(struct l_esr_context);
514 		esr->esr = tf->tf_esr;
515 	}
516 
517 	memcpy(&frame->sf.sf_uc.uc_stack, &uc_stack, sizeof(uc_stack));
518 	memcpy(&frame->uc, &uc, sizeof(uc));
519 
520 	/* Copy the sigframe out to the user's stack. */
521 	if (copyout(frame, fp, sizeof(*fp)) != 0) {
522 		/* Process has trashed its stack. Kill it. */
523 		free(frame, M_LINUX);
524 		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
525 		PROC_LOCK(p);
526 		sigexit(td, SIGILL);
527 	}
528 	free(frame, M_LINUX);
529 
530 	tf->tf_x[0]= sig;
531 	tf->tf_x[1] = (register_t)&fp->sf.sf_si;
532 	tf->tf_x[2] = (register_t)&fp->sf.sf_uc;
533 	tf->tf_x[8] = (register_t)catcher;
534 	tf->tf_sp = (register_t)fp;
535 	tf->tf_elr = (register_t)linux_vdso_sigcode;
536 
537 	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
538 	    tf->tf_sp);
539 
540 	PROC_LOCK(p);
541 	mtx_lock(&psp->ps_mtx);
542 }
543 
544 struct sysentvec elf_linux_sysvec = {
545 	.sv_size	= LINUX_SYS_MAXSYSCALL,
546 	.sv_table	= linux_sysent,
547 	.sv_fixup	= linux_elf_fixup,
548 	.sv_sendsig	= linux_rt_sendsig,
549 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
550 	.sv_szsigcode	= &linux_szsigcode,
551 	.sv_name	= "Linux ELF64",
552 	.sv_coredump	= elf64_coredump,
553 	.sv_elf_core_osabi = ELFOSABI_NONE,
554 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
555 	.sv_elf_core_prepare_notes = linux64_prepare_notes,
556 	.sv_imgact_try	= linux_exec_imgact_try,
557 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
558 	.sv_minuser	= VM_MIN_ADDRESS,
559 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
560 	.sv_usrstack	= LINUX_USRSTACK,
561 	.sv_psstrings	= LINUX_PS_STRINGS,
562 	.sv_psstringssz	= sizeof(struct ps_strings),
563 	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
564 	.sv_copyout_auxargs = linux_copyout_auxargs,
565 	.sv_copyout_strings = linux_copyout_strings,
566 	.sv_setregs	= linux_exec_setregs,
567 	.sv_fixlimit	= NULL,
568 	.sv_maxssiz	= NULL,
569 	.sv_flags	= SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
570 	    SV_SIG_WAITNDQ | SV_TIMEKEEP,
571 	.sv_set_syscall_retval = linux_set_syscall_retval,
572 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
573 	.sv_syscallnames = NULL,
574 	.sv_shared_page_base = LINUX_SHAREDPAGE,
575 	.sv_shared_page_len = PAGE_SIZE,
576 	.sv_schedtail	= linux_schedtail,
577 	.sv_thread_detach = linux_thread_detach,
578 	.sv_trap	= NULL,
579 	.sv_hwcap	= &elf_hwcap,
580 	.sv_hwcap2	= &elf_hwcap2,
581 	.sv_onexec	= linux_on_exec_vmspace,
582 	.sv_onexit	= linux_on_exit,
583 	.sv_ontdexit	= linux_thread_dtor,
584 	.sv_setid_allowed = &linux_setid_allowed_query,
585 };
586 
587 static int
588 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
589 {
590 	int error;
591 
592 	error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
593 	    LINUX_VDSOPAGE_SIZE, imgp);
594 	if (error == 0)
595 		linux_on_exec(p, imgp);
596 	return (error);
597 }
598 
599 /*
600  * linux_vdso_install() and linux_exec_sysvec_init() must be called
601  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
602  */
603 static void
604 linux_exec_sysvec_init(void *param)
605 {
606 	l_uintptr_t *ktimekeep_base;
607 	struct sysentvec *sv;
608 	ptrdiff_t tkoff;
609 
610 	sv = param;
611 	/* Fill timekeep_base */
612 	exec_sysvec_init(sv);
613 
614 	tkoff = kern_timekeep_base - linux_vdso_base;
615 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
616 	*ktimekeep_base = sv->sv_timekeep_base;
617 }
618 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
619     linux_exec_sysvec_init, &elf_linux_sysvec);
620 
621 static void
622 linux_vdso_install(const void *param)
623 {
624 	char *vdso_start = &_binary_linux_vdso_so_o_start;
625 	char *vdso_end = &_binary_linux_vdso_so_o_end;
626 
627 	linux_szsigcode = vdso_end - vdso_start;
628 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
629 
630 	linux_vdso_base = LINUX_VDSOPAGE;
631 
632 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
633 
634 	linux_vdso_obj = __elfN(linux_shared_page_init)
635 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
636 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
637 
638 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
639 }
640 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
641     linux_vdso_install, NULL);
642 
643 static void
644 linux_vdso_deinstall(const void *param)
645 {
646 
647 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
648 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
649 }
650 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
651     linux_vdso_deinstall, NULL);
652 
653 static void
654 linux_vdso_reloc(char *mapping, Elf_Addr offset)
655 {
656 	Elf_Size rtype, symidx;
657 	const Elf_Rela *rela;
658 	const Elf_Shdr *shdr;
659 	const Elf_Ehdr *ehdr;
660 	Elf_Addr *where;
661 	Elf_Addr addr, addend;
662 	int i, relacnt;
663 
664 	MPASS(offset != 0);
665 
666 	relacnt = 0;
667 	ehdr = (const Elf_Ehdr *)mapping;
668 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
669 	for (i = 0; i < ehdr->e_shnum; i++)
670 	{
671 		switch (shdr[i].sh_type) {
672 		case SHT_REL:
673 			printf("Linux Aarch64 vDSO: unexpected Rel section\n");
674 			break;
675 		case SHT_RELA:
676 			rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
677 			relacnt = shdr[i].sh_size / sizeof(*rela);
678 		}
679 	}
680 
681 	for (i = 0; i < relacnt; i++, rela++) {
682 		where = (Elf_Addr *)(mapping + rela->r_offset);
683 		addend = rela->r_addend;
684 		rtype = ELF_R_TYPE(rela->r_info);
685 		symidx = ELF_R_SYM(rela->r_info);
686 
687 		switch (rtype) {
688 		case R_AARCH64_NONE:	/* none */
689 			break;
690 
691 		case R_AARCH64_RELATIVE:	/* B + A */
692 			addr = (Elf_Addr)(mapping + addend);
693 			if (*where != addr)
694 				*where = addr;
695 			break;
696 		default:
697 			printf("Linux Aarch64 vDSO: unexpected relocation type %ld, "
698 			    "symbol index %ld\n", rtype, symidx);
699 		}
700 	}
701 }
702 
703 static char GNU_ABI_VENDOR[] = "GNU";
704 static int GNU_ABI_LINUX = 0;
705 
706 /* LINUXTODO: deduplicate */
707 static bool
708 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
709 {
710 	const Elf32_Word *desc;
711 	uintptr_t p;
712 
713 	p = (uintptr_t)(note + 1);
714 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
715 
716 	desc = (const Elf32_Word *)p;
717 	if (desc[0] != GNU_ABI_LINUX)
718 		return (false);
719 
720 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
721 	return (true);
722 }
723 
724 static Elf_Brandnote linux64_brandnote = {
725 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
726 	.hdr.n_descsz	= 16,
727 	.hdr.n_type	= 1,
728 	.vendor		= GNU_ABI_VENDOR,
729 	.flags		= BN_TRANSLATE_OSREL,
730 	.trans_osrel	= linux_trans_osrel
731 };
732 
733 static Elf64_Brandinfo linux_glibc2brand = {
734 	.brand		= ELFOSABI_LINUX,
735 	.machine	= EM_AARCH64,
736 	.compat_3_brand	= "Linux",
737 	.emul_path	= linux_emul_path,
738 	.interp_path	= "/lib64/ld-linux-x86-64.so.2",
739 	.sysvec		= &elf_linux_sysvec,
740 	.interp_newpath	= NULL,
741 	.brand_note	= &linux64_brandnote,
742 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
743 };
744 
745 Elf64_Brandinfo *linux_brandlist[] = {
746 	&linux_glibc2brand,
747 	NULL
748 };
749 
750 static int
751 linux64_elf_modevent(module_t mod, int type, void *data)
752 {
753 	Elf64_Brandinfo **brandinfo;
754 	struct linux_ioctl_handler**lihp;
755 	int error;
756 
757 	error = 0;
758 	switch(type) {
759 	case MOD_LOAD:
760 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
761 		    ++brandinfo)
762 			if (elf64_insert_brand_entry(*brandinfo) < 0)
763 				error = EINVAL;
764 		if (error == 0) {
765 			SET_FOREACH(lihp, linux_ioctl_handler_set)
766 				linux_ioctl_register_handler(*lihp);
767 			stclohz = (stathz ? stathz : hz);
768 			if (bootverbose)
769 				printf("Linux arm64 ELF exec handler installed\n");
770 		}
771 		break;
772 	case MOD_UNLOAD:
773 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
774 		    ++brandinfo)
775 			if (elf64_brand_inuse(*brandinfo))
776 				error = EBUSY;
777 		if (error == 0) {
778 			for (brandinfo = &linux_brandlist[0];
779 			    *brandinfo != NULL; ++brandinfo)
780 				if (elf64_remove_brand_entry(*brandinfo) < 0)
781 					error = EINVAL;
782 		}
783 		if (error == 0) {
784 			SET_FOREACH(lihp, linux_ioctl_handler_set)
785 				linux_ioctl_unregister_handler(*lihp);
786 			if (bootverbose)
787 				printf("Linux arm64 ELF exec handler removed\n");
788 		} else
789 			printf("Could not deinstall Linux arm64 ELF interpreter entry\n");
790 		break;
791 	default:
792 		return (EOPNOTSUPP);
793 	}
794 	return (error);
795 }
796 
797 static moduledata_t linux64_elf_mod = {
798 	"linux64elf",
799 	linux64_elf_modevent,
800 	0
801 };
802 
803 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
804 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
805 FEATURE(linux64, "AArch64 Linux 64bit support");
806