13911ee2cSEd Maste /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
33911ee2cSEd Maste *
43911ee2cSEd Maste * Copyright (c) 1994-1996 Søren Schmidt
53911ee2cSEd Maste * Copyright (c) 2018 Turing Robotic Industries Inc.
63911ee2cSEd Maste *
73911ee2cSEd Maste * Redistribution and use in source and binary forms, with or without
83911ee2cSEd Maste * modification, are permitted provided that the following conditions
93911ee2cSEd Maste * are met:
103911ee2cSEd Maste * 1. Redistributions of source code must retain the above copyright
113911ee2cSEd Maste * notice, this list of conditions and the following disclaimer.
123911ee2cSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
133911ee2cSEd Maste * notice, this list of conditions and the following disclaimer in the
143911ee2cSEd Maste * documentation and/or other materials provided with the distribution.
153911ee2cSEd Maste *
163911ee2cSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173911ee2cSEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183911ee2cSEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193911ee2cSEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203911ee2cSEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213911ee2cSEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223911ee2cSEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233911ee2cSEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243911ee2cSEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253911ee2cSEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263911ee2cSEd Maste * SUCH DAMAGE.
273911ee2cSEd Maste */
283911ee2cSEd Maste
2974465145SDmitry Chagin #define __ELF_WORD_SIZE 64
3074465145SDmitry Chagin
313911ee2cSEd Maste #include <sys/param.h>
323911ee2cSEd Maste #include <sys/elf.h>
333911ee2cSEd Maste #include <sys/exec.h>
343911ee2cSEd Maste #include <sys/imgact.h>
353911ee2cSEd Maste #include <sys/imgact_elf.h>
363911ee2cSEd Maste #include <sys/kernel.h>
37ccc510b4SEdward Tomasz Napierala #include <sys/ktr.h>
383911ee2cSEd Maste #include <sys/lock.h>
393911ee2cSEd Maste #include <sys/module.h>
403911ee2cSEd Maste #include <sys/mutex.h>
413911ee2cSEd Maste #include <sys/proc.h>
429931033bSDmitry Chagin #include <sys/stddef.h>
43ccc510b4SEdward Tomasz Napierala #include <sys/syscallsubr.h>
443911ee2cSEd Maste #include <sys/sysctl.h>
453911ee2cSEd Maste #include <sys/sysent.h>
463911ee2cSEd Maste
4755d3e181SDmitry Chagin #include <vm/vm.h>
48027d727dSDmitry Chagin #include <vm/vm_param.h>
493911ee2cSEd Maste
503911ee2cSEd Maste #include <arm64/linux/linux.h>
513911ee2cSEd Maste #include <arm64/linux/linux_proto.h>
5274465145SDmitry Chagin #include <compat/linux/linux_elf.h>
533911ee2cSEd Maste #include <compat/linux/linux_emul.h>
540a4b664aSDmitry Chagin #include <compat/linux/linux_fork.h>
553911ee2cSEd Maste #include <compat/linux/linux_ioctl.h>
563911ee2cSEd Maste #include <compat/linux/linux_mib.h>
573911ee2cSEd Maste #include <compat/linux/linux_misc.h>
58ccc510b4SEdward Tomasz Napierala #include <compat/linux/linux_signal.h>
59b5f20658SEdward Tomasz Napierala #include <compat/linux/linux_util.h>
603911ee2cSEd Maste #include <compat/linux/linux_vdso.h>
613911ee2cSEd Maste
6221f24617SDmitry Chagin #include <arm64/linux/linux_sigframe.h>
6321f24617SDmitry Chagin
64b501b2aeSEdward Tomasz Napierala #include <machine/md_var.h>
652555f175SKonstantin Belousov #include <machine/pcb.h>
66953a7d7cSAlex Richardson #ifdef VFP
67953a7d7cSAlex Richardson #include <machine/vfp.h>
68953a7d7cSAlex Richardson #endif
69953a7d7cSAlex Richardson
703911ee2cSEd Maste MODULE_VERSION(linux64elf, 1);
713911ee2cSEd Maste
729931033bSDmitry Chagin #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
739931033bSDmitry Chagin #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - \
749931033bSDmitry Chagin LINUX_VDSOPAGE_SIZE)
759931033bSDmitry Chagin #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE)
769931033bSDmitry Chagin /*
779931033bSDmitry Chagin * PAGE_SIZE - the size
789931033bSDmitry Chagin * of the native SHAREDPAGE
799931033bSDmitry Chagin */
809931033bSDmitry Chagin #define LINUX_USRSTACK LINUX_SHAREDPAGE
819931033bSDmitry Chagin #define LINUX_PS_STRINGS (LINUX_USRSTACK - \
829931033bSDmitry Chagin sizeof(struct ps_strings))
839931033bSDmitry Chagin
843911ee2cSEd Maste static int linux_szsigcode;
859931033bSDmitry Chagin static vm_object_t linux_vdso_obj;
869931033bSDmitry Chagin static char *linux_vdso_mapping;
879931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_start;
889931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_end;
899931033bSDmitry Chagin static vm_offset_t linux_vdso_base;
903911ee2cSEd Maste
913911ee2cSEd Maste extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
921da65dcbSMitchell Horne extern const char *linux_syscallnames[];
933911ee2cSEd Maste
943911ee2cSEd Maste SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
953911ee2cSEd Maste
963911ee2cSEd Maste static void linux_vdso_install(const void *param);
973911ee2cSEd Maste static void linux_vdso_deinstall(const void *param);
989931033bSDmitry Chagin static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
993911ee2cSEd Maste static void linux_set_syscall_retval(struct thread *td, int error);
1003911ee2cSEd Maste static int linux_fetch_syscall_args(struct thread *td);
1013911ee2cSEd Maste static void linux_exec_setregs(struct thread *td, struct image_params *imgp,
10231174518SJohn Baldwin uintptr_t stack);
1039931033bSDmitry Chagin static void linux_exec_sysvec_init(void *param);
1045fd9cd53SDmitry Chagin static int linux_on_exec_vmspace(struct proc *p,
1055fd9cd53SDmitry Chagin struct image_params *imgp);
1063911ee2cSEd Maste
1079931033bSDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
1089931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
109d957343fSDmitry Chagin LINUX_VDSO_SYM_INTPTR(__user_rt_sigreturn);
1109931033bSDmitry Chagin
1113911ee2cSEd Maste static int
linux_fetch_syscall_args(struct thread * td)1123911ee2cSEd Maste linux_fetch_syscall_args(struct thread *td)
1133911ee2cSEd Maste {
1143911ee2cSEd Maste struct proc *p;
1153911ee2cSEd Maste struct syscall_args *sa;
1163911ee2cSEd Maste register_t *ap;
1173911ee2cSEd Maste
1183911ee2cSEd Maste p = td->td_proc;
1193911ee2cSEd Maste ap = td->td_frame->tf_x;
1203911ee2cSEd Maste sa = &td->td_sa;
1213911ee2cSEd Maste
1223911ee2cSEd Maste sa->code = td->td_frame->tf_x[8];
123cf98bc28SDavid Chisnall sa->original_code = sa->code;
12403f5bd1eSDmitry Chagin
1253911ee2cSEd Maste if (sa->code >= p->p_sysent->sv_size)
12639024a89SKonstantin Belousov sa->callp = &nosys_sysent;
1273911ee2cSEd Maste else
1283911ee2cSEd Maste sa->callp = &p->p_sysent->sv_table[sa->code];
1293911ee2cSEd Maste
130adb12675SBrooks Davis if (sa->callp->sy_narg > nitems(sa->args))
131adb12675SBrooks Davis panic("ARM64TODO: Could we have more than %zu args?",
132adb12675SBrooks Davis nitems(sa->args));
133adb12675SBrooks Davis memcpy(sa->args, ap, nitems(sa->args) * sizeof(register_t));
1343911ee2cSEd Maste
1353911ee2cSEd Maste td->td_retval[0] = 0;
1363911ee2cSEd Maste return (0);
1373911ee2cSEd Maste }
1383911ee2cSEd Maste
1393911ee2cSEd Maste static void
linux_set_syscall_retval(struct thread * td,int error)1403911ee2cSEd Maste linux_set_syscall_retval(struct thread *td, int error)
1413911ee2cSEd Maste {
1423911ee2cSEd Maste
1438e5d76e6SAndrew Turner td->td_retval[1] = td->td_frame->tf_x[1];
1448e5d76e6SAndrew Turner cpu_set_syscall_retval(td, error);
145c26391f4SEdward Tomasz Napierala
146c26391f4SEdward Tomasz Napierala if (__predict_false(error != 0)) {
147866b1f51SEdward Tomasz Napierala if (error != ERESTART && error != EJUSTRETURN)
148866b1f51SEdward Tomasz Napierala td->td_frame->tf_x[0] = bsd_to_linux_errno(error);
149c26391f4SEdward Tomasz Napierala }
1503911ee2cSEd Maste }
1513911ee2cSEd Maste
1527d8c9839SDmitry Chagin void
linux64_arch_copyout_auxargs(struct image_params * imgp,Elf_Auxinfo ** pos)1537d8c9839SDmitry Chagin linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
1543911ee2cSEd Maste {
1553911ee2cSEd Maste
1567d8c9839SDmitry Chagin AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
1577d8c9839SDmitry Chagin AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap);
1587d8c9839SDmitry Chagin AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2);
1597d8c9839SDmitry Chagin AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
1605caa67faSJohn Baldwin }
1615caa67faSJohn Baldwin
1623911ee2cSEd Maste /*
1633911ee2cSEd Maste * Reset registers to default values on exec.
1643911ee2cSEd Maste */
1653911ee2cSEd Maste static void
linux_exec_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)16631174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp,
16731174518SJohn Baldwin uintptr_t stack)
1683911ee2cSEd Maste {
1693911ee2cSEd Maste struct trapframe *regs = td->td_frame;
170a2a8b582SMitchell Horne struct pcb *pcb = td->td_pcb;
1713911ee2cSEd Maste
1723911ee2cSEd Maste memset(regs, 0, sizeof(*regs));
1733911ee2cSEd Maste regs->tf_sp = stack;
1743911ee2cSEd Maste regs->tf_elr = imgp->entry_addr;
175a2a8b582SMitchell Horne pcb->pcb_tpidr_el0 = 0;
176a2a8b582SMitchell Horne pcb->pcb_tpidrro_el0 = 0;
1770723b409SJohn Baldwin WRITE_SPECIALREG(tpidrro_el0, 0);
1780723b409SJohn Baldwin WRITE_SPECIALREG(tpidr_el0, 0);
1790723b409SJohn Baldwin
180953a7d7cSAlex Richardson #ifdef VFP
181a2a8b582SMitchell Horne vfp_reset_state(td, pcb);
182953a7d7cSAlex Richardson #endif
183a2a8b582SMitchell Horne
184a2a8b582SMitchell Horne /*
185a2a8b582SMitchell Horne * Clear debug register state. It is not applicable to the new process.
186a2a8b582SMitchell Horne */
187a2a8b582SMitchell Horne bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
1883911ee2cSEd Maste }
1893911ee2cSEd Maste
190070a4ff8SAndrew Turner static bool
linux_parse_sigreturn_ctx(struct thread * td,struct l_sigcontext * sc)191070a4ff8SAndrew Turner linux_parse_sigreturn_ctx(struct thread *td, struct l_sigcontext *sc)
192070a4ff8SAndrew Turner {
193070a4ff8SAndrew Turner struct l_fpsimd_context *fpsimd;
194070a4ff8SAndrew Turner struct _l_aarch64_ctx *ctx;
195070a4ff8SAndrew Turner int offset;
196070a4ff8SAndrew Turner
197070a4ff8SAndrew Turner offset = 0;
198070a4ff8SAndrew Turner while (1) {
199070a4ff8SAndrew Turner /* The offset must be 16 byte aligned */
200070a4ff8SAndrew Turner if ((offset & 15) != 0)
201070a4ff8SAndrew Turner return (false);
202070a4ff8SAndrew Turner
203070a4ff8SAndrew Turner /* Check for buffer overflow of the ctx */
204070a4ff8SAndrew Turner if ((offset + sizeof(*ctx)) >
205070a4ff8SAndrew Turner sizeof(sc->__reserved))
206070a4ff8SAndrew Turner return (false);
207070a4ff8SAndrew Turner
208070a4ff8SAndrew Turner ctx = (struct _l_aarch64_ctx *)&sc->__reserved[offset];
209070a4ff8SAndrew Turner
210070a4ff8SAndrew Turner /* Check for buffer overflow of the data */
211070a4ff8SAndrew Turner if ((offset + ctx->size) > sizeof(sc->__reserved))
212070a4ff8SAndrew Turner return (false);
213070a4ff8SAndrew Turner
214070a4ff8SAndrew Turner switch(ctx->magic) {
215070a4ff8SAndrew Turner case 0:
216070a4ff8SAndrew Turner if (ctx->size != 0)
217070a4ff8SAndrew Turner return (false);
218070a4ff8SAndrew Turner return (true);
219070a4ff8SAndrew Turner case L_ESR_MAGIC:
220070a4ff8SAndrew Turner /* Ignore */
221070a4ff8SAndrew Turner break;
222070a4ff8SAndrew Turner #ifdef VFP
223070a4ff8SAndrew Turner case L_FPSIMD_MAGIC:
224070a4ff8SAndrew Turner fpsimd = (struct l_fpsimd_context *)ctx;
225070a4ff8SAndrew Turner
226070a4ff8SAndrew Turner /*
227070a4ff8SAndrew Turner * Discard any vfp state for the current thread, we
228070a4ff8SAndrew Turner * are about to override it.
229070a4ff8SAndrew Turner */
230070a4ff8SAndrew Turner critical_enter();
231070a4ff8SAndrew Turner vfp_discard(td);
232070a4ff8SAndrew Turner critical_exit();
233070a4ff8SAndrew Turner
234070a4ff8SAndrew Turner td->td_pcb->pcb_fpustate.vfp_fpcr = fpsimd->fpcr;
235070a4ff8SAndrew Turner td->td_pcb->pcb_fpustate.vfp_fpsr = fpsimd->fpsr;
236070a4ff8SAndrew Turner memcpy(td->td_pcb->pcb_fpustate.vfp_regs,
237070a4ff8SAndrew Turner fpsimd->vregs, sizeof(fpsimd->vregs));
238070a4ff8SAndrew Turner
239070a4ff8SAndrew Turner break;
240070a4ff8SAndrew Turner #endif
241070a4ff8SAndrew Turner default:
242070a4ff8SAndrew Turner return (false);
243070a4ff8SAndrew Turner }
244070a4ff8SAndrew Turner
245070a4ff8SAndrew Turner offset += ctx->size;
246070a4ff8SAndrew Turner }
247070a4ff8SAndrew Turner
248070a4ff8SAndrew Turner }
249070a4ff8SAndrew Turner
2503911ee2cSEd Maste int
linux_rt_sigreturn(struct thread * td,struct linux_rt_sigreturn_args * args)2513911ee2cSEd Maste linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
2523911ee2cSEd Maste {
253070a4ff8SAndrew Turner struct l_rt_sigframe *sf;
254c56480a8SDmitry Chagin struct l_sigframe *frame;
255ccc510b4SEdward Tomasz Napierala struct trapframe *tf;
256070a4ff8SAndrew Turner sigset_t bmask;
257ccc510b4SEdward Tomasz Napierala int error;
2583911ee2cSEd Maste
259070a4ff8SAndrew Turner sf = malloc(sizeof(*sf), M_LINUX, M_WAITOK | M_ZERO);
260070a4ff8SAndrew Turner
261ccc510b4SEdward Tomasz Napierala tf = td->td_frame;
262c56480a8SDmitry Chagin frame = (struct l_sigframe *)tf->tf_sp;
263070a4ff8SAndrew Turner error = copyin((void *)&frame->sf, sf, sizeof(*sf));
264070a4ff8SAndrew Turner if (error != 0) {
265070a4ff8SAndrew Turner free(sf, M_LINUX);
266ccc510b4SEdward Tomasz Napierala return (error);
267070a4ff8SAndrew Turner }
268070a4ff8SAndrew Turner
269070a4ff8SAndrew Turner memcpy(tf->tf_x, sf->sf_uc.uc_sc.regs, sizeof(tf->tf_x));
270070a4ff8SAndrew Turner tf->tf_lr = sf->sf_uc.uc_sc.regs[30];
271070a4ff8SAndrew Turner tf->tf_sp = sf->sf_uc.uc_sc.sp;
272070a4ff8SAndrew Turner tf->tf_elr = sf->sf_uc.uc_sc.pc;
273070a4ff8SAndrew Turner
274070a4ff8SAndrew Turner if ((sf->sf_uc.uc_sc.pstate & PSR_M_MASK) != PSR_M_EL0t ||
275070a4ff8SAndrew Turner (sf->sf_uc.uc_sc.pstate & PSR_AARCH32) != 0 ||
276070a4ff8SAndrew Turner (sf->sf_uc.uc_sc.pstate & PSR_DAIF) !=
277070a4ff8SAndrew Turner (td->td_frame->tf_spsr & PSR_DAIF))
278070a4ff8SAndrew Turner goto einval;
279070a4ff8SAndrew Turner tf->tf_spsr = sf->sf_uc.uc_sc.pstate;
280070a4ff8SAndrew Turner
281070a4ff8SAndrew Turner if (!linux_parse_sigreturn_ctx(td, &sf->sf_uc.uc_sc))
282070a4ff8SAndrew Turner goto einval;
283ccc510b4SEdward Tomasz Napierala
284ccc510b4SEdward Tomasz Napierala /* Restore signal mask. */
285070a4ff8SAndrew Turner linux_to_bsd_sigset(&sf->sf_uc.uc_sigmask, &bmask);
286070a4ff8SAndrew Turner kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
287070a4ff8SAndrew Turner free(sf, M_LINUX);
288ccc510b4SEdward Tomasz Napierala
289ccc510b4SEdward Tomasz Napierala return (EJUSTRETURN);
290070a4ff8SAndrew Turner einval:
291070a4ff8SAndrew Turner free(sf, M_LINUX);
292070a4ff8SAndrew Turner return (EINVAL);
2933911ee2cSEd Maste }
2943911ee2cSEd Maste
2953911ee2cSEd Maste static void
linux_rt_sendsig(sig_t catcher,ksiginfo_t * ksi,sigset_t * mask)2963911ee2cSEd Maste linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
2973911ee2cSEd Maste {
298ccc510b4SEdward Tomasz Napierala struct thread *td;
299ccc510b4SEdward Tomasz Napierala struct proc *p;
300ccc510b4SEdward Tomasz Napierala struct trapframe *tf;
301c56480a8SDmitry Chagin struct l_sigframe *fp, *frame;
302c56480a8SDmitry Chagin struct l_fpsimd_context *fpsimd;
303c56480a8SDmitry Chagin struct l_esr_context *esr;
304c56480a8SDmitry Chagin l_stack_t uc_stack;
305c56480a8SDmitry Chagin ucontext_t uc;
306c56480a8SDmitry Chagin uint8_t *scr;
307ccc510b4SEdward Tomasz Napierala struct sigacts *psp;
308109fd18aSDmitry Chagin int onstack, sig, issiginfo;
3093911ee2cSEd Maste
310ccc510b4SEdward Tomasz Napierala td = curthread;
311ccc510b4SEdward Tomasz Napierala p = td->td_proc;
312ccc510b4SEdward Tomasz Napierala PROC_LOCK_ASSERT(p, MA_OWNED);
313ccc510b4SEdward Tomasz Napierala
314ccc510b4SEdward Tomasz Napierala sig = ksi->ksi_signo;
315ccc510b4SEdward Tomasz Napierala psp = p->p_sigacts;
316ccc510b4SEdward Tomasz Napierala mtx_assert(&psp->ps_mtx, MA_OWNED);
317ccc510b4SEdward Tomasz Napierala
318ccc510b4SEdward Tomasz Napierala tf = td->td_frame;
319ccc510b4SEdward Tomasz Napierala onstack = sigonstack(tf->tf_sp);
320109fd18aSDmitry Chagin issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
321ccc510b4SEdward Tomasz Napierala
322ccc510b4SEdward Tomasz Napierala CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
323ccc510b4SEdward Tomasz Napierala catcher, sig);
324ccc510b4SEdward Tomasz Napierala
325ccc510b4SEdward Tomasz Napierala /* Allocate and validate space for the signal handler context. */
326ccc510b4SEdward Tomasz Napierala if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
327ccc510b4SEdward Tomasz Napierala SIGISMEMBER(psp->ps_sigonstack, sig)) {
328ccc510b4SEdward Tomasz Napierala fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
329ccc510b4SEdward Tomasz Napierala td->td_sigstk.ss_size);
330ccc510b4SEdward Tomasz Napierala #if defined(COMPAT_43)
331ccc510b4SEdward Tomasz Napierala td->td_sigstk.ss_flags |= SS_ONSTACK;
332ccc510b4SEdward Tomasz Napierala #endif
333ccc510b4SEdward Tomasz Napierala } else {
334ccc510b4SEdward Tomasz Napierala fp = (struct l_sigframe *)td->td_frame->tf_sp;
335ccc510b4SEdward Tomasz Napierala }
336ccc510b4SEdward Tomasz Napierala
337ccc510b4SEdward Tomasz Napierala /* Make room, keeping the stack aligned */
338ccc510b4SEdward Tomasz Napierala fp--;
339ccc510b4SEdward Tomasz Napierala fp = (struct l_sigframe *)STACKALIGN(fp);
340ccc510b4SEdward Tomasz Napierala
341c56480a8SDmitry Chagin get_mcontext(td, &uc.uc_mcontext, 0);
342c56480a8SDmitry Chagin uc.uc_sigmask = *mask;
343ccc510b4SEdward Tomasz Napierala
344c56480a8SDmitry Chagin uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
345c56480a8SDmitry Chagin uc_stack.ss_size = td->td_sigstk.ss_size;
346c56480a8SDmitry Chagin uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
347c56480a8SDmitry Chagin (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
348ccc510b4SEdward Tomasz Napierala mtx_unlock(&psp->ps_mtx);
349ccc510b4SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc);
350ccc510b4SEdward Tomasz Napierala
351c56480a8SDmitry Chagin /* Fill in the frame to copy out */
352c56480a8SDmitry Chagin frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO);
353c56480a8SDmitry Chagin
354c56480a8SDmitry Chagin memcpy(&frame->sf.sf_uc.uc_sc.regs, tf->tf_x, sizeof(tf->tf_x));
355c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.regs[30] = tf->tf_lr;
356c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.sp = tf->tf_sp;
357bf3a14b4SDmitry Chagin frame->sf.sf_uc.uc_sc.pc = tf->tf_elr;
358c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.pstate = tf->tf_spsr;
359c56480a8SDmitry Chagin frame->sf.sf_uc.uc_sc.fault_address = (register_t)ksi->ksi_addr;
360c56480a8SDmitry Chagin
361c56480a8SDmitry Chagin /* Stack frame for unwinding */
362c56480a8SDmitry Chagin frame->fp = tf->tf_x[29];
3632cdeb89eSDmitry Chagin frame->lr = tf->tf_elr;
364c56480a8SDmitry Chagin
365c56480a8SDmitry Chagin /* Translate the signal. */
366c56480a8SDmitry Chagin sig = bsd_to_linux_signal(sig);
367c56480a8SDmitry Chagin siginfo_to_lsiginfo(&ksi->ksi_info, &frame->sf.sf_si, sig);
368c56480a8SDmitry Chagin bsd_to_linux_sigset(mask, &frame->sf.sf_uc.uc_sigmask);
369c56480a8SDmitry Chagin
370c56480a8SDmitry Chagin /*
371c56480a8SDmitry Chagin * Prepare fpsimd & esr. Does not check sizes, as
372c56480a8SDmitry Chagin * __reserved is big enougth.
373c56480a8SDmitry Chagin */
374c56480a8SDmitry Chagin scr = (uint8_t *)&frame->sf.sf_uc.uc_sc.__reserved;
375c56480a8SDmitry Chagin #ifdef VFP
376c56480a8SDmitry Chagin fpsimd = (struct l_fpsimd_context *) scr;
377c56480a8SDmitry Chagin fpsimd->head.magic = L_FPSIMD_MAGIC;
378c56480a8SDmitry Chagin fpsimd->head.size = sizeof(struct l_fpsimd_context);
379c56480a8SDmitry Chagin fpsimd->fpsr = uc.uc_mcontext.mc_fpregs.fp_sr;
380c56480a8SDmitry Chagin fpsimd->fpcr = uc.uc_mcontext.mc_fpregs.fp_cr;
381c56480a8SDmitry Chagin
382c56480a8SDmitry Chagin memcpy(fpsimd->vregs, &uc.uc_mcontext.mc_fpregs.fp_q,
383c56480a8SDmitry Chagin sizeof(uc.uc_mcontext.mc_fpregs.fp_q));
384c56480a8SDmitry Chagin scr += roundup(sizeof(struct l_fpsimd_context), 16);
385c56480a8SDmitry Chagin #endif
386c56480a8SDmitry Chagin if (ksi->ksi_addr != 0) {
387c56480a8SDmitry Chagin esr = (struct l_esr_context *) scr;
388c56480a8SDmitry Chagin esr->head.magic = L_ESR_MAGIC;
389c56480a8SDmitry Chagin esr->head.size = sizeof(struct l_esr_context);
390c56480a8SDmitry Chagin esr->esr = tf->tf_esr;
391c56480a8SDmitry Chagin }
392c56480a8SDmitry Chagin
393c56480a8SDmitry Chagin memcpy(&frame->sf.sf_uc.uc_stack, &uc_stack, sizeof(uc_stack));
394c56480a8SDmitry Chagin
395ccc510b4SEdward Tomasz Napierala /* Copy the sigframe out to the user's stack. */
396c56480a8SDmitry Chagin if (copyout(frame, fp, sizeof(*fp)) != 0) {
397ccc510b4SEdward Tomasz Napierala /* Process has trashed its stack. Kill it. */
398c56480a8SDmitry Chagin free(frame, M_LINUX);
399ccc510b4SEdward Tomasz Napierala CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
400ccc510b4SEdward Tomasz Napierala PROC_LOCK(p);
401ccc510b4SEdward Tomasz Napierala sigexit(td, SIGILL);
402ccc510b4SEdward Tomasz Napierala }
403c56480a8SDmitry Chagin free(frame, M_LINUX);
404ccc510b4SEdward Tomasz Napierala
405ccc510b4SEdward Tomasz Napierala tf->tf_x[0]= sig;
406109fd18aSDmitry Chagin if (issiginfo) {
407c56480a8SDmitry Chagin tf->tf_x[1] = (register_t)&fp->sf.sf_si;
408c56480a8SDmitry Chagin tf->tf_x[2] = (register_t)&fp->sf.sf_uc;
409109fd18aSDmitry Chagin } else {
410109fd18aSDmitry Chagin tf->tf_x[1] = 0;
411109fd18aSDmitry Chagin tf->tf_x[2] = 0;
412109fd18aSDmitry Chagin }
4132cdeb89eSDmitry Chagin tf->tf_x[29] = (register_t)&fp->fp;
414d957343fSDmitry Chagin tf->tf_elr = (register_t)catcher;
415ccc510b4SEdward Tomasz Napierala tf->tf_sp = (register_t)fp;
416d957343fSDmitry Chagin tf->tf_lr = (register_t)__user_rt_sigreturn;
417ccc510b4SEdward Tomasz Napierala
418ccc510b4SEdward Tomasz Napierala CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
419ccc510b4SEdward Tomasz Napierala tf->tf_sp);
420ccc510b4SEdward Tomasz Napierala
421ccc510b4SEdward Tomasz Napierala PROC_LOCK(p);
422ccc510b4SEdward Tomasz Napierala mtx_lock(&psp->ps_mtx);
4233911ee2cSEd Maste }
4243911ee2cSEd Maste
4253911ee2cSEd Maste struct sysentvec elf_linux_sysvec = {
4263911ee2cSEd Maste .sv_size = LINUX_SYS_MAXSYSCALL,
4273911ee2cSEd Maste .sv_table = linux_sysent,
4286039e966SDmitry Chagin .sv_fixup = __elfN(freebsd_fixup),
4293911ee2cSEd Maste .sv_sendsig = linux_rt_sendsig,
4309931033bSDmitry Chagin .sv_sigcode = &_binary_linux_vdso_so_o_start,
4313911ee2cSEd Maste .sv_szsigcode = &linux_szsigcode,
4323911ee2cSEd Maste .sv_name = "Linux ELF64",
4333911ee2cSEd Maste .sv_coredump = elf64_coredump,
434435754a5SEdward Tomasz Napierala .sv_elf_core_osabi = ELFOSABI_NONE,
43545d99014SEdward Tomasz Napierala .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
43645d99014SEdward Tomasz Napierala .sv_elf_core_prepare_notes = linux64_prepare_notes,
4373911ee2cSEd Maste .sv_minsigstksz = LINUX_MINSIGSTKSZ,
4383911ee2cSEd Maste .sv_minuser = VM_MIN_ADDRESS,
4393911ee2cSEd Maste .sv_maxuser = VM_MAXUSER_ADDRESS,
4409931033bSDmitry Chagin .sv_usrstack = LINUX_USRSTACK,
4419931033bSDmitry Chagin .sv_psstrings = LINUX_PS_STRINGS,
4423fc21fddSMark Johnston .sv_psstringssz = sizeof(struct ps_strings),
443d4f55cc8SEd Maste .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
4447d8c9839SDmitry Chagin .sv_copyout_auxargs = __linuxN(copyout_auxargs),
4456039e966SDmitry Chagin .sv_copyout_strings = __linuxN(copyout_strings),
4463911ee2cSEd Maste .sv_setregs = linux_exec_setregs,
4473911ee2cSEd Maste .sv_fixlimit = NULL,
4483911ee2cSEd Maste .sv_maxssiz = NULL,
449870e197dSKonstantin Belousov .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
4509931033bSDmitry Chagin SV_SIG_WAITNDQ | SV_TIMEKEEP,
4513911ee2cSEd Maste .sv_set_syscall_retval = linux_set_syscall_retval,
4523911ee2cSEd Maste .sv_fetch_syscall_args = linux_fetch_syscall_args,
4531da65dcbSMitchell Horne .sv_syscallnames = linux_syscallnames,
4549931033bSDmitry Chagin .sv_shared_page_base = LINUX_SHAREDPAGE,
4553911ee2cSEd Maste .sv_shared_page_len = PAGE_SIZE,
4563911ee2cSEd Maste .sv_schedtail = linux_schedtail,
4573911ee2cSEd Maste .sv_thread_detach = linux_thread_detach,
45884a3963dSEdward Tomasz Napierala .sv_trap = NULL,
459*e6dbc99dSAndrew Turner .sv_hwcap = &linux_elf_hwcap,
460*e6dbc99dSAndrew Turner .sv_hwcap2 = &linux_elf_hwcap2,
4615fd9cd53SDmitry Chagin .sv_onexec = linux_on_exec_vmspace,
4624815f175SKonstantin Belousov .sv_onexit = linux_on_exit,
4634815f175SKonstantin Belousov .sv_ontdexit = linux_thread_dtor,
464598f6fb4SKonstantin Belousov .sv_setid_allowed = &linux_setid_allowed_query,
4653911ee2cSEd Maste };
4663911ee2cSEd Maste
4675fd9cd53SDmitry Chagin static int
linux_on_exec_vmspace(struct proc * p,struct image_params * imgp)4685fd9cd53SDmitry Chagin linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
4695fd9cd53SDmitry Chagin {
4709931033bSDmitry Chagin int error;
4715fd9cd53SDmitry Chagin
4729931033bSDmitry Chagin error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
4739931033bSDmitry Chagin LINUX_VDSOPAGE_SIZE, imgp);
4749931033bSDmitry Chagin if (error == 0)
475fd745e1dSDmitry Chagin error = linux_on_exec(p, imgp);
4769931033bSDmitry Chagin return (error);
4775fd9cd53SDmitry Chagin }
4785fd9cd53SDmitry Chagin
47909cffde9SDmitry Chagin /*
48009cffde9SDmitry Chagin * linux_vdso_install() and linux_exec_sysvec_init() must be called
48109cffde9SDmitry Chagin * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
48209cffde9SDmitry Chagin */
4833911ee2cSEd Maste static void
linux_exec_sysvec_init(void * param)4849931033bSDmitry Chagin linux_exec_sysvec_init(void *param)
4859931033bSDmitry Chagin {
4869931033bSDmitry Chagin l_uintptr_t *ktimekeep_base;
4879931033bSDmitry Chagin struct sysentvec *sv;
4889931033bSDmitry Chagin ptrdiff_t tkoff;
4899931033bSDmitry Chagin
4909931033bSDmitry Chagin sv = param;
4919931033bSDmitry Chagin /* Fill timekeep_base */
4929931033bSDmitry Chagin exec_sysvec_init(sv);
4939931033bSDmitry Chagin
4949931033bSDmitry Chagin tkoff = kern_timekeep_base - linux_vdso_base;
4959931033bSDmitry Chagin ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
496361971fbSKornel Dulęba *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
4979931033bSDmitry Chagin }
49809cffde9SDmitry Chagin SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
4999931033bSDmitry Chagin linux_exec_sysvec_init, &elf_linux_sysvec);
5009931033bSDmitry Chagin
5019931033bSDmitry Chagin static void
linux_vdso_install(const void * param)5023911ee2cSEd Maste linux_vdso_install(const void *param)
5033911ee2cSEd Maste {
5049931033bSDmitry Chagin char *vdso_start = &_binary_linux_vdso_so_o_start;
5059931033bSDmitry Chagin char *vdso_end = &_binary_linux_vdso_so_o_end;
5063911ee2cSEd Maste
5079931033bSDmitry Chagin linux_szsigcode = vdso_end - vdso_start;
5089931033bSDmitry Chagin MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
5093911ee2cSEd Maste
5109931033bSDmitry Chagin linux_vdso_base = LINUX_VDSOPAGE;
5113911ee2cSEd Maste
5129931033bSDmitry Chagin __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
5133911ee2cSEd Maste
5149931033bSDmitry Chagin linux_vdso_obj = __elfN(linux_shared_page_init)
5159931033bSDmitry Chagin (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
5169931033bSDmitry Chagin bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
5173911ee2cSEd Maste
5189931033bSDmitry Chagin linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
5193911ee2cSEd Maste }
52009cffde9SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
5213911ee2cSEd Maste linux_vdso_install, NULL);
5223911ee2cSEd Maste
5233911ee2cSEd Maste static void
linux_vdso_deinstall(const void * param)5243911ee2cSEd Maste linux_vdso_deinstall(const void *param)
5253911ee2cSEd Maste {
5263911ee2cSEd Maste
5279931033bSDmitry Chagin __elfN(linux_shared_page_fini)(linux_vdso_obj,
5289931033bSDmitry Chagin linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
5293911ee2cSEd Maste }
5303911ee2cSEd Maste SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
5313911ee2cSEd Maste linux_vdso_deinstall, NULL);
5323911ee2cSEd Maste
5339931033bSDmitry Chagin static void
linux_vdso_reloc(char * mapping,Elf_Addr offset)5349931033bSDmitry Chagin linux_vdso_reloc(char *mapping, Elf_Addr offset)
5359931033bSDmitry Chagin {
5369931033bSDmitry Chagin Elf_Size rtype, symidx;
5379931033bSDmitry Chagin const Elf_Rela *rela;
5389931033bSDmitry Chagin const Elf_Shdr *shdr;
5399931033bSDmitry Chagin const Elf_Ehdr *ehdr;
5409931033bSDmitry Chagin Elf_Addr *where;
5419931033bSDmitry Chagin Elf_Addr addr, addend;
5429931033bSDmitry Chagin int i, relacnt;
5439931033bSDmitry Chagin
5449931033bSDmitry Chagin MPASS(offset != 0);
5459931033bSDmitry Chagin
5469931033bSDmitry Chagin relacnt = 0;
5479931033bSDmitry Chagin ehdr = (const Elf_Ehdr *)mapping;
5489931033bSDmitry Chagin shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
5499931033bSDmitry Chagin for (i = 0; i < ehdr->e_shnum; i++)
5509931033bSDmitry Chagin {
5519931033bSDmitry Chagin switch (shdr[i].sh_type) {
5529931033bSDmitry Chagin case SHT_REL:
5539931033bSDmitry Chagin printf("Linux Aarch64 vDSO: unexpected Rel section\n");
5549931033bSDmitry Chagin break;
5559931033bSDmitry Chagin case SHT_RELA:
5569931033bSDmitry Chagin rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
5579931033bSDmitry Chagin relacnt = shdr[i].sh_size / sizeof(*rela);
5589931033bSDmitry Chagin }
5599931033bSDmitry Chagin }
5609931033bSDmitry Chagin
5619931033bSDmitry Chagin for (i = 0; i < relacnt; i++, rela++) {
5629931033bSDmitry Chagin where = (Elf_Addr *)(mapping + rela->r_offset);
5639931033bSDmitry Chagin addend = rela->r_addend;
5649931033bSDmitry Chagin rtype = ELF_R_TYPE(rela->r_info);
5659931033bSDmitry Chagin symidx = ELF_R_SYM(rela->r_info);
5669931033bSDmitry Chagin
5679931033bSDmitry Chagin switch (rtype) {
5689931033bSDmitry Chagin case R_AARCH64_NONE: /* none */
5699931033bSDmitry Chagin break;
5709931033bSDmitry Chagin
5719931033bSDmitry Chagin case R_AARCH64_RELATIVE: /* B + A */
5729931033bSDmitry Chagin addr = (Elf_Addr)(mapping + addend);
5739931033bSDmitry Chagin if (*where != addr)
5749931033bSDmitry Chagin *where = addr;
5759931033bSDmitry Chagin break;
5769931033bSDmitry Chagin default:
5779931033bSDmitry Chagin printf("Linux Aarch64 vDSO: unexpected relocation type %ld, "
5789931033bSDmitry Chagin "symbol index %ld\n", rtype, symidx);
5799931033bSDmitry Chagin }
5809931033bSDmitry Chagin }
5819931033bSDmitry Chagin }
5829931033bSDmitry Chagin
5833911ee2cSEd Maste static Elf_Brandnote linux64_brandnote = {
5843911ee2cSEd Maste .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
5853911ee2cSEd Maste .hdr.n_descsz = 16,
5863911ee2cSEd Maste .hdr.n_type = 1,
5873911ee2cSEd Maste .vendor = GNU_ABI_VENDOR,
5883911ee2cSEd Maste .flags = BN_TRANSLATE_OSREL,
5893911ee2cSEd Maste .trans_osrel = linux_trans_osrel
5903911ee2cSEd Maste };
5913911ee2cSEd Maste
5923911ee2cSEd Maste static Elf64_Brandinfo linux_glibc2brand = {
5933911ee2cSEd Maste .brand = ELFOSABI_LINUX,
5943911ee2cSEd Maste .machine = EM_AARCH64,
5953911ee2cSEd Maste .compat_3_brand = "Linux",
5963911ee2cSEd Maste .interp_path = "/lib64/ld-linux-x86-64.so.2",
5973911ee2cSEd Maste .sysvec = &elf_linux_sysvec,
5983911ee2cSEd Maste .interp_newpath = NULL,
5993911ee2cSEd Maste .brand_note = &linux64_brandnote,
6003911ee2cSEd Maste .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
6013911ee2cSEd Maste };
6023911ee2cSEd Maste
6033911ee2cSEd Maste Elf64_Brandinfo *linux_brandlist[] = {
6043911ee2cSEd Maste &linux_glibc2brand,
6053911ee2cSEd Maste NULL
6063911ee2cSEd Maste };
6073911ee2cSEd Maste
6083911ee2cSEd Maste static int
linux64_elf_modevent(module_t mod,int type,void * data)6093911ee2cSEd Maste linux64_elf_modevent(module_t mod, int type, void *data)
6103911ee2cSEd Maste {
6113911ee2cSEd Maste Elf64_Brandinfo **brandinfo;
6123911ee2cSEd Maste struct linux_ioctl_handler**lihp;
6133911ee2cSEd Maste int error;
6143911ee2cSEd Maste
6153911ee2cSEd Maste error = 0;
6163911ee2cSEd Maste switch(type) {
6173911ee2cSEd Maste case MOD_LOAD:
6183911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
6193911ee2cSEd Maste ++brandinfo)
6203911ee2cSEd Maste if (elf64_insert_brand_entry(*brandinfo) < 0)
6213911ee2cSEd Maste error = EINVAL;
6223911ee2cSEd Maste if (error == 0) {
6233911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set)
6243911ee2cSEd Maste linux_ioctl_register_handler(*lihp);
6253911ee2cSEd Maste stclohz = (stathz ? stathz : hz);
6263911ee2cSEd Maste if (bootverbose)
6273911ee2cSEd Maste printf("Linux arm64 ELF exec handler installed\n");
6283911ee2cSEd Maste }
6293911ee2cSEd Maste break;
6303911ee2cSEd Maste case MOD_UNLOAD:
6313911ee2cSEd Maste for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
6323911ee2cSEd Maste ++brandinfo)
6333911ee2cSEd Maste if (elf64_brand_inuse(*brandinfo))
6343911ee2cSEd Maste error = EBUSY;
6353911ee2cSEd Maste if (error == 0) {
6363911ee2cSEd Maste for (brandinfo = &linux_brandlist[0];
6373911ee2cSEd Maste *brandinfo != NULL; ++brandinfo)
6383911ee2cSEd Maste if (elf64_remove_brand_entry(*brandinfo) < 0)
6393911ee2cSEd Maste error = EINVAL;
6403911ee2cSEd Maste }
6413911ee2cSEd Maste if (error == 0) {
6423911ee2cSEd Maste SET_FOREACH(lihp, linux_ioctl_handler_set)
6433911ee2cSEd Maste linux_ioctl_unregister_handler(*lihp);
6443911ee2cSEd Maste if (bootverbose)
645ae8330b4SDmitry Chagin printf("Linux arm64 ELF exec handler removed\n");
6463911ee2cSEd Maste } else
647ae8330b4SDmitry Chagin printf("Could not deinstall Linux arm64 ELF interpreter entry\n");
6483911ee2cSEd Maste break;
6493911ee2cSEd Maste default:
6503911ee2cSEd Maste return (EOPNOTSUPP);
6513911ee2cSEd Maste }
6523911ee2cSEd Maste return (error);
6533911ee2cSEd Maste }
6543911ee2cSEd Maste
6553911ee2cSEd Maste static moduledata_t linux64_elf_mod = {
6563911ee2cSEd Maste "linux64elf",
6573911ee2cSEd Maste linux64_elf_modevent,
6583911ee2cSEd Maste 0
6593911ee2cSEd Maste };
6603911ee2cSEd Maste
6613911ee2cSEd Maste DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
6623911ee2cSEd Maste MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
6633911ee2cSEd Maste FEATURE(linux64, "AArch64 Linux 64bit support");
664