2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1994-1996 Søren Schmidt
5 * Copyright (c) 2018 Turing Robotic Industries Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #define __ELF_WORD_SIZE 64
31 #include <sys/param.h>
34 #include <sys/imgact.h>
35 #include <sys/imgact_elf.h>
36 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <sys/stddef.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysctl.h>
45 #include <sys/sysent.h>
48 #include <vm/vm_param.h>
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>
62 #include <arm64/linux/linux_sigframe.h>
64 #include <machine/md_var.h>
65 #include <machine/pcb.h>
67 #include <machine/vfp.h>
70 MODULE_VERSION(linux64elf, 1);
72 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
73 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - \
75 #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE)
77 * PAGE_SIZE - the size
78 * of the native SHAREDPAGE
80 #define LINUX_USRSTACK LINUX_SHAREDPAGE
81 #define LINUX_PS_STRINGS (LINUX_USRSTACK - \
82 sizeof(struct ps_strings))
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;
91 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
92 extern const char *linux_syscallnames[];
94 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
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,
103 static void linux_exec_sysvec_init(void *param);
104 static int linux_on_exec_vmspace(struct proc *p,
105 struct image_params *imgp);
107 LINUX_VDSO_SYM_CHAR(linux_platform);
108 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
109 LINUX_VDSO_SYM_INTPTR(__user_rt_sigreturn);
112 linux_fetch_syscall_args(struct thread *td)
115 struct syscall_args *sa;
119 ap = td->td_frame->tf_x;
122 sa->code = td->td_frame->tf_x[8];
123 sa->original_code = sa->code;
125 if (sa->code >= p->p_sysent->sv_size)
126 sa->callp = &nosys_sysent;
128 sa->callp = &p->p_sysent->sv_table[sa->code];
130 if (sa->callp->sy_narg > nitems(sa->args))
131 panic("ARM64TODO: Could we have more than %zu args?",
133 memcpy(sa->args, ap, nitems(sa->args) * sizeof(register_t));
135 td->td_retval[0] = 0;
140 linux_set_syscall_retval(struct thread *td, int error)
143 td->td_retval[1] = td->td_frame->tf_x[1];
144 cpu_set_syscall_retval(td, error);
146 if (__predict_false(error != 0)) {
147 if (error != ERESTART && error != EJUSTRETURN)
148 td->td_frame->tf_x[0] = bsd_to_linux_errno(error);
153 linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
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_PLATFORM, PTROUT(linux_platform));
163 * Reset registers to default values on exec.
166 linux_exec_setregs(struct thread *td, struct image_params *imgp,
169 struct trapframe *regs = td->td_frame;
170 struct pcb *pcb = td->td_pcb;
172 memset(regs, 0, sizeof(*regs));
174 regs->tf_elr = imgp->entry_addr;
175 pcb->pcb_tpidr_el0 = 0;
176 pcb->pcb_tpidrro_el0 = 0;
177 WRITE_SPECIALREG(tpidrro_el0, 0);
178 WRITE_SPECIALREG(tpidr_el0, 0);
181 vfp_reset_state(td, pcb);
185 * Clear debug register state. It is not applicable to the new process.
187 bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
191 linux_parse_sigreturn_ctx(struct thread *td, struct l_sigcontext *sc)
193 struct l_fpsimd_context *fpsimd;
194 struct _l_aarch64_ctx *ctx;
199 /* The offset must be 16 byte aligned */
200 if ((offset & 15) != 0)
203 /* Check for buffer overflow of the ctx */
204 if ((offset + sizeof(*ctx)) >
205 sizeof(sc->__reserved))
208 ctx = (struct _l_aarch64_ctx *)&sc->__reserved[offset];
210 /* Check for buffer overflow of the data */
211 if ((offset + ctx->size) > sizeof(sc->__reserved))
224 fpsimd = (struct l_fpsimd_context *)ctx;
227 * Discard any vfp state for the current thread, we
228 * are about to override it.
234 td->td_pcb->pcb_fpustate.vfp_fpcr = fpsimd->fpcr;
235 td->td_pcb->pcb_fpustate.vfp_fpsr = fpsimd->fpsr;
236 memcpy(td->td_pcb->pcb_fpustate.vfp_regs,
237 fpsimd->vregs, sizeof(fpsimd->vregs));
251 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
253 struct l_rt_sigframe *sf;
254 struct l_sigframe *frame;
255 struct trapframe *tf;
259 sf = malloc(sizeof(*sf), M_LINUX, M_WAITOK | M_ZERO);
262 frame = (struct l_sigframe *)tf->tf_sp;
263 error = copyin((void *)&frame->sf, sf, sizeof(*sf));
269 memcpy(tf->tf_x, sf->sf_uc.uc_sc.regs, sizeof(tf->tf_x));
270 tf->tf_lr = sf->sf_uc.uc_sc.regs[30];
271 tf->tf_sp = sf->sf_uc.uc_sc.sp;
272 tf->tf_elr = sf->sf_uc.uc_sc.pc;
274 if ((sf->sf_uc.uc_sc.pstate & PSR_M_MASK) != PSR_M_EL0t ||
275 (sf->sf_uc.uc_sc.pstate & PSR_AARCH32) != 0 ||
276 (sf->sf_uc.uc_sc.pstate & PSR_DAIF) !=
277 (td->td_frame->tf_spsr & PSR_DAIF))
279 tf->tf_spsr = sf->sf_uc.uc_sc.pstate;
281 if (!linux_parse_sigreturn_ctx(td, &sf->sf_uc.uc_sc))
284 /* Restore signal mask. */
285 linux_to_bsd_sigset(&sf->sf_uc.uc_sigmask, &bmask);
286 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
289 return (EJUSTRETURN);
296 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
300 struct trapframe *tf;
301 struct l_sigframe *fp, *frame;
302 struct l_fpsimd_context *fpsimd;
303 struct l_esr_context *esr;
308 int onstack, sig, issiginfo;
312 PROC_LOCK_ASSERT(p, MA_OWNED);
314 sig = ksi->ksi_signo;
316 mtx_assert(&psp->ps_mtx, MA_OWNED);
319 onstack = sigonstack(tf->tf_sp);
320 issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
322 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
325 /* Allocate and validate space for the signal handler context. */
326 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
327 SIGISMEMBER(psp->ps_sigonstack, sig)) {
328 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
329 td->td_sigstk.ss_size);
330 #if defined(COMPAT_43)
331 td->td_sigstk.ss_flags |= SS_ONSTACK;
334 fp = (struct l_sigframe *)td->td_frame->tf_sp;
337 /* Make room, keeping the stack aligned */
339 fp = (struct l_sigframe *)STACKALIGN(fp);
341 get_mcontext(td, &uc.uc_mcontext, 0);
342 uc.uc_sigmask = *mask;
344 uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
345 uc_stack.ss_size = td->td_sigstk.ss_size;
346 uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
347 (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
348 mtx_unlock(&psp->ps_mtx);
349 PROC_UNLOCK(td->td_proc);
351 /* Fill in the frame to copy out */
352 frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO);
354 memcpy(&frame->sf.sf_uc.uc_sc.regs, tf->tf_x, sizeof(tf->tf_x));
355 frame->sf.sf_uc.uc_sc.regs[30] = tf->tf_lr;
356 frame->sf.sf_uc.uc_sc.sp = tf->tf_sp;
357 frame->sf.sf_uc.uc_sc.pc = tf->tf_elr;
358 frame->sf.sf_uc.uc_sc.pstate = tf->tf_spsr;
359 frame->sf.sf_uc.uc_sc.fault_address = (register_t)ksi->ksi_addr;
361 /* Stack frame for unwinding */
362 frame->fp = tf->tf_x[29];
363 frame->lr = tf->tf_elr;
365 /* Translate the signal. */
366 sig = bsd_to_linux_signal(sig);
367 siginfo_to_lsiginfo(&ksi->ksi_info, &frame->sf.sf_si, sig);
368 bsd_to_linux_sigset(mask, &frame->sf.sf_uc.uc_sigmask);
371 * Prepare fpsimd & esr. Does not check sizes, as
372 * __reserved is big enougth.
374 scr = (uint8_t *)&frame->sf.sf_uc.uc_sc.__reserved;
376 fpsimd = (struct l_fpsimd_context *) scr;
377 fpsimd->head.magic = L_FPSIMD_MAGIC;
378 fpsimd->head.size = sizeof(struct l_fpsimd_context);
379 fpsimd->fpsr = uc.uc_mcontext.mc_fpregs.fp_sr;
380 fpsimd->fpcr = uc.uc_mcontext.mc_fpregs.fp_cr;
382 memcpy(fpsimd->vregs, &uc.uc_mcontext.mc_fpregs.fp_q,
383 sizeof(uc.uc_mcontext.mc_fpregs.fp_q));
384 scr += roundup(sizeof(struct l_fpsimd_context), 16);
386 if (ksi->ksi_addr != 0) {
387 esr = (struct l_esr_context *) scr;
388 esr->head.magic = L_ESR_MAGIC;
389 esr->head.size = sizeof(struct l_esr_context);
390 esr->esr = tf->tf_esr;
393 memcpy(&frame->sf.sf_uc.uc_stack, &uc_stack, sizeof(uc_stack));
395 /* Copy the sigframe out to the user's stack. */
396 if (copyout(frame, fp, sizeof(*fp)) != 0) {
397 /* Process has trashed its stack. Kill it. */
398 free(frame, M_LINUX);
399 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
403 free(frame, M_LINUX);
407 tf->tf_x[1] = (register_t)&fp->sf.sf_si;
408 tf->tf_x[2] = (register_t)&fp->sf.sf_uc;
413 tf->tf_x[29] = (register_t)&fp->fp;
414 tf->tf_elr = (register_t)catcher;
415 tf->tf_sp = (register_t)fp;
416 tf->tf_lr = (register_t)__user_rt_sigreturn;
418 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
422 mtx_lock(&psp->ps_mtx);
425 struct sysentvec elf_linux_sysvec = {
426 .sv_size = LINUX_SYS_MAXSYSCALL,
427 .sv_table = linux_sysent,
428 .sv_fixup = __elfN(freebsd_fixup),
429 .sv_sendsig = linux_rt_sendsig,
430 .sv_sigcode = &_binary_linux_vdso_so_o_start,
431 .sv_szsigcode = &linux_szsigcode,
432 .sv_name = "Linux ELF64",
433 .sv_coredump = elf64_coredump,
434 .sv_elf_core_osabi = ELFOSABI_NONE,
435 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
436 .sv_elf_core_prepare_notes = linux64_prepare_notes,
437 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
438 .sv_minuser = VM_MIN_ADDRESS,
439 .sv_maxuser = VM_MAXUSER_ADDRESS,
440 .sv_usrstack = LINUX_USRSTACK,
441 .sv_psstrings = LINUX_PS_STRINGS,
442 .sv_psstringssz = sizeof(struct ps_strings),
443 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
444 .sv_copyout_auxargs = __linuxN(copyout_auxargs),
445 .sv_copyout_strings = __linuxN(copyout_strings),
446 .sv_setregs = linux_exec_setregs,
449 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
450 SV_SIG_WAITNDQ | SV_TIMEKEEP,
451 .sv_set_syscall_retval = linux_set_syscall_retval,
452 .sv_fetch_syscall_args = linux_fetch_syscall_args,
453 .sv_syscallnames = linux_syscallnames,
454 .sv_shared_page_base = LINUX_SHAREDPAGE,
455 .sv_shared_page_len = PAGE_SIZE,
456 .sv_schedtail = linux_schedtail,
457 .sv_thread_detach = linux_thread_detach,
459 .sv_hwcap = &elf_hwcap,
460 .sv_hwcap2 = &elf_hwcap2,
461 .sv_onexec = linux_on_exec_vmspace,
462 .sv_onexit = linux_on_exit,
463 .sv_ontdexit = linux_thread_dtor,
464 .sv_setid_allowed = &linux_setid_allowed_query,
468 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
472 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
473 LINUX_VDSOPAGE_SIZE, imgp);
475 error = linux_on_exec(p, imgp);
480 * linux_vdso_install() and linux_exec_sysvec_init() must be called
481 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
484 linux_exec_sysvec_init(void *param)
486 l_uintptr_t *ktimekeep_base;
487 struct sysentvec *sv;
491 /* Fill timekeep_base */
492 exec_sysvec_init(sv);
494 tkoff = kern_timekeep_base - linux_vdso_base;
495 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
496 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
498 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
499 linux_exec_sysvec_init, &elf_linux_sysvec);
502 linux_vdso_install(const void *param)
504 char *vdso_start = &_binary_linux_vdso_so_o_start;
505 char *vdso_end = &_binary_linux_vdso_so_o_end;
507 linux_szsigcode = vdso_end - vdso_start;
508 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
510 linux_vdso_base = LINUX_VDSOPAGE;
512 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
514 linux_vdso_obj = __elfN(linux_shared_page_init)
515 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
516 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
518 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
520 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
521 linux_vdso_install, NULL);
524 linux_vdso_deinstall(const void *param)
527 __elfN(linux_shared_page_fini)(linux_vdso_obj,
528 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
530 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
531 linux_vdso_deinstall, NULL);
534 linux_vdso_reloc(char *mapping, Elf_Addr offset)
536 Elf_Size rtype, symidx;
537 const Elf_Rela *rela;
538 const Elf_Shdr *shdr;
539 const Elf_Ehdr *ehdr;
541 Elf_Addr addr, addend;
547 ehdr = (const Elf_Ehdr *)mapping;
548 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
549 for (i = 0; i < ehdr->e_shnum; i++)
551 switch (shdr[i].sh_type) {
553 printf("Linux Aarch64 vDSO: unexpected Rel section\n");
556 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
557 relacnt = shdr[i].sh_size / sizeof(*rela);
561 for (i = 0; i < relacnt; i++, rela++) {
562 where = (Elf_Addr *)(mapping + rela->r_offset);
563 addend = rela->r_addend;
564 rtype = ELF_R_TYPE(rela->r_info);
565 symidx = ELF_R_SYM(rela->r_info);
568 case R_AARCH64_NONE: /* none */
571 case R_AARCH64_RELATIVE: /* B + A */
572 addr = (Elf_Addr)(mapping + addend);
577 printf("Linux Aarch64 vDSO: unexpected relocation type %ld, "
578 "symbol index %ld\n", rtype, symidx);
583 static Elf_Brandnote linux64_brandnote = {
584 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
587 .vendor = GNU_ABI_VENDOR,
588 .flags = BN_TRANSLATE_OSREL,
589 .trans_osrel = linux_trans_osrel
592 static Elf64_Brandinfo linux_glibc2brand = {
593 .brand = ELFOSABI_LINUX,
594 .machine = EM_AARCH64,
595 .compat_3_brand = "Linux",
596 .interp_path = "/lib64/ld-linux-x86-64.so.2",
597 .sysvec = &elf_linux_sysvec,
598 .interp_newpath = NULL,
599 .brand_note = &linux64_brandnote,
600 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
603 Elf64_Brandinfo *linux_brandlist[] = {
609 linux64_elf_modevent(module_t mod, int type, void *data)
611 Elf64_Brandinfo **brandinfo;
612 struct linux_ioctl_handler**lihp;
618 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
620 if (elf64_insert_brand_entry(*brandinfo) < 0)
623 SET_FOREACH(lihp, linux_ioctl_handler_set)
624 linux_ioctl_register_handler(*lihp);
625 stclohz = (stathz ? stathz : hz);
627 printf("Linux arm64 ELF exec handler installed\n");
631 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
633 if (elf64_brand_inuse(*brandinfo))
636 for (brandinfo = &linux_brandlist[0];
637 *brandinfo != NULL; ++brandinfo)
638 if (elf64_remove_brand_entry(*brandinfo) < 0)
642 SET_FOREACH(lihp, linux_ioctl_handler_set)
643 linux_ioctl_unregister_handler(*lihp);
645 printf("Linux arm64 ELF exec handler removed\n");
647 printf("Could not deinstall Linux arm64 ELF interpreter entry\n");
655 static moduledata_t linux64_elf_mod = {
657 linux64_elf_modevent,
661 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
662 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
663 FEATURE(linux64, "AArch64 Linux 64bit support");