19dcf90f8SEd Schouten /*- 28c9c3144SOlivier Houchard * Copyright (c) 2014, 2015 The FreeBSD Foundation. 38c9c3144SOlivier Houchard * Copyright (c) 2014, 2017 Andrew Turner. 48c9c3144SOlivier Houchard * Copyright (c) 2018 Olivier Houchard 58c9c3144SOlivier Houchard * All rights reserved. 68c9c3144SOlivier Houchard * 78c9c3144SOlivier Houchard * This software was developed by Andrew Turner under 88c9c3144SOlivier Houchard * sponsorship from the FreeBSD Foundation. 98c9c3144SOlivier Houchard * 108c9c3144SOlivier Houchard * Portions of this software were developed by Konstantin Belousov 118c9c3144SOlivier Houchard * under sponsorship from the FreeBSD Foundation. 129dcf90f8SEd Schouten * 139dcf90f8SEd Schouten * Redistribution and use in source and binary forms, with or without 149dcf90f8SEd Schouten * modification, are permitted provided that the following conditions 159dcf90f8SEd Schouten * are met: 169dcf90f8SEd Schouten * 1. Redistributions of source code must retain the above copyright 179dcf90f8SEd Schouten * notice, this list of conditions and the following disclaimer. 189dcf90f8SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright 199dcf90f8SEd Schouten * notice, this list of conditions and the following disclaimer in the 209dcf90f8SEd Schouten * documentation and/or other materials provided with the distribution. 219dcf90f8SEd Schouten * 229dcf90f8SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 239dcf90f8SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 249dcf90f8SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 259dcf90f8SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 269dcf90f8SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 279dcf90f8SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 289dcf90f8SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 299dcf90f8SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 309dcf90f8SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 319dcf90f8SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 329dcf90f8SEd Schouten * SUCH DAMAGE. 339dcf90f8SEd Schouten */ 349dcf90f8SEd Schouten 359dcf90f8SEd Schouten #include <sys/cdefs.h> 369dcf90f8SEd Schouten __FBSDID("$FreeBSD$"); 379dcf90f8SEd Schouten 389dcf90f8SEd Schouten #define __ELF_WORD_SIZE 32 398c9c3144SOlivier Houchard 408c9c3144SOlivier Houchard #include <sys/param.h> 418c9c3144SOlivier Houchard #include <sys/kernel.h> 428c9c3144SOlivier Houchard #include <sys/systm.h> 438c9c3144SOlivier Houchard #include <sys/exec.h> 448c9c3144SOlivier Houchard #include <sys/imgact.h> 458c9c3144SOlivier Houchard #include <sys/linker.h> 468c9c3144SOlivier Houchard #include <sys/proc.h> 478c9c3144SOlivier Houchard #include <sys/sysent.h> 489dcf90f8SEd Schouten #include <sys/imgact_elf.h> 498c9c3144SOlivier Houchard #include <sys/syscall.h> 508c9c3144SOlivier Houchard #include <sys/signalvar.h> 518c9c3144SOlivier Houchard #include <sys/vnode.h> 528c9c3144SOlivier Houchard 538c9c3144SOlivier Houchard #include <machine/elf.h> 548c9c3144SOlivier Houchard 558c9c3144SOlivier Houchard #include <compat/freebsd32/freebsd32_util.h> 568c9c3144SOlivier Houchard 578c9c3144SOlivier Houchard #define FREEBSD32_MINUSER 0x00001000 588c9c3144SOlivier Houchard #define FREEBSD32_MAXUSER ((1ul << 32) - PAGE_SIZE) 598c9c3144SOlivier Houchard #define FREEBSD32_SHAREDPAGE (FREEBSD32_MAXUSER - PAGE_SIZE) 608c9c3144SOlivier Houchard #define FREEBSD32_USRSTACK FREEBSD32_SHAREDPAGE 618c9c3144SOlivier Houchard 628c9c3144SOlivier Houchard extern const char *freebsd32_syscallnames[]; 638c9c3144SOlivier Houchard 648c9c3144SOlivier Houchard extern char aarch32_sigcode[]; 658c9c3144SOlivier Houchard extern int sz_aarch32_sigcode; 668c9c3144SOlivier Houchard 678c9c3144SOlivier Houchard static int freebsd32_fetch_syscall_args(struct thread *td); 688c9c3144SOlivier Houchard static void freebsd32_setregs(struct thread *td, struct image_params *imgp, 698c9c3144SOlivier Houchard u_long stack); 708c9c3144SOlivier Houchard static void freebsd32_set_syscall_retval(struct thread *, int); 718c9c3144SOlivier Houchard 728c9c3144SOlivier Houchard static boolean_t elf32_arm_abi_supported(struct image_params *); 738c9c3144SOlivier Houchard 748c9c3144SOlivier Houchard extern void freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 758c9c3144SOlivier Houchard 768c9c3144SOlivier Houchard static struct sysentvec elf32_freebsd_sysvec = { 778c9c3144SOlivier Houchard .sv_size = SYS_MAXSYSCALL, 788c9c3144SOlivier Houchard .sv_table = freebsd32_sysent, 798c9c3144SOlivier Houchard .sv_errsize = 0, 808c9c3144SOlivier Houchard .sv_errtbl = NULL, 818c9c3144SOlivier Houchard .sv_transtrap = NULL, 828c9c3144SOlivier Houchard .sv_fixup = elf32_freebsd_fixup, 838c9c3144SOlivier Houchard .sv_sendsig = freebsd32_sendsig, 848c9c3144SOlivier Houchard .sv_sigcode = aarch32_sigcode, 858c9c3144SOlivier Houchard .sv_szsigcode = &sz_aarch32_sigcode, 868c9c3144SOlivier Houchard .sv_name = "FreeBSD ELF32", 878c9c3144SOlivier Houchard .sv_coredump = elf32_coredump, 888c9c3144SOlivier Houchard .sv_imgact_try = NULL, 898c9c3144SOlivier Houchard .sv_minsigstksz = MINSIGSTKSZ, 908c9c3144SOlivier Houchard .sv_minuser = FREEBSD32_MINUSER, 918c9c3144SOlivier Houchard .sv_maxuser = FREEBSD32_MAXUSER, 928c9c3144SOlivier Houchard .sv_usrstack = FREEBSD32_USRSTACK, 938c9c3144SOlivier Houchard .sv_psstrings = FREEBSD32_PS_STRINGS, 948c9c3144SOlivier Houchard .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 958c9c3144SOlivier Houchard .sv_copyout_strings = freebsd32_copyout_strings, 968c9c3144SOlivier Houchard .sv_setregs = freebsd32_setregs, 978c9c3144SOlivier Houchard .sv_fixlimit = NULL, // XXX 988c9c3144SOlivier Houchard .sv_maxssiz = NULL, 998c9c3144SOlivier Houchard .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_TIMEKEEP, 1008c9c3144SOlivier Houchard .sv_set_syscall_retval = freebsd32_set_syscall_retval, 1018c9c3144SOlivier Houchard .sv_fetch_syscall_args = freebsd32_fetch_syscall_args, 1028c9c3144SOlivier Houchard .sv_syscallnames = freebsd32_syscallnames, 1038c9c3144SOlivier Houchard .sv_shared_page_base = FREEBSD32_SHAREDPAGE, 1048c9c3144SOlivier Houchard .sv_shared_page_len = PAGE_SIZE, 1058c9c3144SOlivier Houchard .sv_schedtail = NULL, 1068c9c3144SOlivier Houchard .sv_thread_detach = NULL, 1078c9c3144SOlivier Houchard .sv_trap = NULL, 1088c9c3144SOlivier Houchard }; 1098c9c3144SOlivier Houchard INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec); 1108c9c3144SOlivier Houchard 1118c9c3144SOlivier Houchard static Elf32_Brandinfo freebsd32_brand_info = { 1128c9c3144SOlivier Houchard .brand = ELFOSABI_FREEBSD, 1138c9c3144SOlivier Houchard .machine = EM_ARM, 1148c9c3144SOlivier Houchard .compat_3_brand = "FreeBSD", 1158c9c3144SOlivier Houchard .emul_path = NULL, 1168c9c3144SOlivier Houchard .interp_path = "/libexec/ld-elf.so.1", 1178c9c3144SOlivier Houchard .sysvec = &elf32_freebsd_sysvec, 11824718606SJustin Hibbits .interp_newpath = "/libexec/ld-elf32.so.1", 1198c9c3144SOlivier Houchard .brand_note = &elf32_freebsd_brandnote, 1208c9c3144SOlivier Houchard .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 1218c9c3144SOlivier Houchard .header_supported= elf32_arm_abi_supported, 1228c9c3144SOlivier Houchard }; 1238c9c3144SOlivier Houchard 1248c9c3144SOlivier Houchard SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST, 1258c9c3144SOlivier Houchard (sysinit_cfunc_t)elf32_insert_brand_entry, &freebsd32_brand_info); 1268c9c3144SOlivier Houchard 1278c9c3144SOlivier Houchard static boolean_t 1288c9c3144SOlivier Houchard elf32_arm_abi_supported(struct image_params *imgp) 1298c9c3144SOlivier Houchard { 1308c9c3144SOlivier Houchard const Elf32_Ehdr *hdr; 1318c9c3144SOlivier Houchard 1328c9c3144SOlivier Houchard /* Check if we support AArch32 */ 1338c9c3144SOlivier Houchard if (ID_AA64PFR0_EL0(READ_SPECIALREG(id_aa64pfr0_el1)) != 1348c9c3144SOlivier Houchard ID_AA64PFR0_EL0_64_32) 1358c9c3144SOlivier Houchard return (FALSE); 1368c9c3144SOlivier Houchard 1378c9c3144SOlivier Houchard #define EF_ARM_EABI_VERSION(x) (((x) & EF_ARM_EABIMASK) >> 24) 1388c9c3144SOlivier Houchard #define EF_ARM_EABI_FREEBSD_MIN 4 1398c9c3144SOlivier Houchard hdr = (const Elf32_Ehdr *)imgp->image_header; 1408c9c3144SOlivier Houchard if (EF_ARM_EABI_VERSION(hdr->e_flags) < EF_ARM_EABI_FREEBSD_MIN) { 1418c9c3144SOlivier Houchard if (bootverbose) 1428c9c3144SOlivier Houchard uprintf("Attempting to execute non EABI binary " 1438c9c3144SOlivier Houchard "(rev %d) image %s", 1448c9c3144SOlivier Houchard EF_ARM_EABI_VERSION(hdr->e_flags), 1458c9c3144SOlivier Houchard imgp->args->fname); 1468c9c3144SOlivier Houchard return (FALSE); 1478c9c3144SOlivier Houchard } 1488c9c3144SOlivier Houchard 1498c9c3144SOlivier Houchard return (TRUE); 1508c9c3144SOlivier Houchard } 1518c9c3144SOlivier Houchard 1528c9c3144SOlivier Houchard static int 1538c9c3144SOlivier Houchard freebsd32_fetch_syscall_args(struct thread *td) 1548c9c3144SOlivier Houchard { 1558c9c3144SOlivier Houchard struct proc *p; 1568c9c3144SOlivier Houchard register_t *ap; 1578c9c3144SOlivier Houchard struct syscall_args *sa; 1588c9c3144SOlivier Houchard int error, i, nap; 1598c9c3144SOlivier Houchard unsigned int args[4]; 1608c9c3144SOlivier Houchard 1618c9c3144SOlivier Houchard nap = 4; 1628c9c3144SOlivier Houchard p = td->td_proc; 1638c9c3144SOlivier Houchard ap = td->td_frame->tf_x; 1648c9c3144SOlivier Houchard sa = &td->td_sa; 1658c9c3144SOlivier Houchard 1668c9c3144SOlivier Houchard /* r7 is the syscall id */ 1678c9c3144SOlivier Houchard sa->code = td->td_frame->tf_x[7]; 1688c9c3144SOlivier Houchard 1698c9c3144SOlivier Houchard if (sa->code == SYS_syscall) { 1708c9c3144SOlivier Houchard sa->code = *ap++; 1718c9c3144SOlivier Houchard nap--; 1728c9c3144SOlivier Houchard } else if (sa->code == SYS___syscall) { 1738c9c3144SOlivier Houchard sa->code = ap[1]; 1748c9c3144SOlivier Houchard nap -= 2; 1758c9c3144SOlivier Houchard ap += 2; 1768c9c3144SOlivier Houchard } 1778c9c3144SOlivier Houchard 1788c9c3144SOlivier Houchard if (sa->code >= p->p_sysent->sv_size) 1798c9c3144SOlivier Houchard sa->callp = &p->p_sysent->sv_table[0]; 1808c9c3144SOlivier Houchard else 1818c9c3144SOlivier Houchard sa->callp = &p->p_sysent->sv_table[sa->code]; 1828c9c3144SOlivier Houchard 1838c9c3144SOlivier Houchard sa->narg = sa->callp->sy_narg; 1848c9c3144SOlivier Houchard for (i = 0; i < nap; i++) 1858c9c3144SOlivier Houchard sa->args[i] = ap[i]; 1868c9c3144SOlivier Houchard if (sa->narg > nap) { 1878c9c3144SOlivier Houchard if ((sa->narg - nap) > nitems(args)) 1888c9c3144SOlivier Houchard panic("Too many system call arguiments"); 1898c9c3144SOlivier Houchard error = copyin((void *)td->td_frame->tf_x[13], args, 1908c9c3144SOlivier Houchard (sa->narg - nap) * sizeof(int)); 1918c9c3144SOlivier Houchard for (i = 0; i < (sa->narg - nap); i++) 1928c9c3144SOlivier Houchard sa->args[i + nap] = args[i]; 1938c9c3144SOlivier Houchard } 1948c9c3144SOlivier Houchard 1958c9c3144SOlivier Houchard td->td_retval[0] = 0; 1968c9c3144SOlivier Houchard td->td_retval[1] = 0; 1978c9c3144SOlivier Houchard 1988c9c3144SOlivier Houchard return (0); 1998c9c3144SOlivier Houchard } 2008c9c3144SOlivier Houchard 2018c9c3144SOlivier Houchard static void 2028c9c3144SOlivier Houchard freebsd32_set_syscall_retval(struct thread *td, int error) 2038c9c3144SOlivier Houchard { 2048c9c3144SOlivier Houchard struct trapframe *frame; 2058c9c3144SOlivier Houchard 2068c9c3144SOlivier Houchard frame = td->td_frame; 2078c9c3144SOlivier Houchard switch (error) { 2088c9c3144SOlivier Houchard case 0: 2098c9c3144SOlivier Houchard frame->tf_x[0] = td->td_retval[0]; 2108c9c3144SOlivier Houchard frame->tf_x[1] = td->td_retval[1]; 2118c9c3144SOlivier Houchard frame->tf_spsr &= ~PSR_C; 2128c9c3144SOlivier Houchard break; 2138c9c3144SOlivier Houchard case ERESTART: 2148c9c3144SOlivier Houchard /* 2158c9c3144SOlivier Houchard * Reconstruct the pc to point at the swi. 2168c9c3144SOlivier Houchard */ 2178c9c3144SOlivier Houchard if ((frame->tf_spsr & PSR_T) != 0) 2188c9c3144SOlivier Houchard frame->tf_elr -= 2; //THUMB_INSN_SIZE; 2198c9c3144SOlivier Houchard else 2208c9c3144SOlivier Houchard frame->tf_elr -= 4; //INSN_SIZE; 2218c9c3144SOlivier Houchard break; 2228c9c3144SOlivier Houchard case EJUSTRETURN: 2238c9c3144SOlivier Houchard /* nothing to do */ 2248c9c3144SOlivier Houchard break; 2258c9c3144SOlivier Houchard default: 2268c9c3144SOlivier Houchard frame->tf_x[0] = error; 2278c9c3144SOlivier Houchard frame->tf_spsr |= PSR_C; 2288c9c3144SOlivier Houchard break; 2298c9c3144SOlivier Houchard } 2308c9c3144SOlivier Houchard } 2318c9c3144SOlivier Houchard 2328c9c3144SOlivier Houchard static void 2338c9c3144SOlivier Houchard freebsd32_setregs(struct thread *td, struct image_params *imgp, 2348c9c3144SOlivier Houchard u_long stack) 2358c9c3144SOlivier Houchard { 2368c9c3144SOlivier Houchard struct trapframe *tf = td->td_frame; 2378c9c3144SOlivier Houchard 2388c9c3144SOlivier Houchard memset(tf, 0, sizeof(struct trapframe)); 2398c9c3144SOlivier Houchard 2408c9c3144SOlivier Houchard /* 2418c9c3144SOlivier Houchard * We need to set x0 for init as it doesn't call 2428c9c3144SOlivier Houchard * cpu_set_syscall_retval to copy the value. We also 2438c9c3144SOlivier Houchard * need to set td_retval for the cases where we do. 2448c9c3144SOlivier Houchard */ 2458c9c3144SOlivier Houchard tf->tf_x[0] = stack; 2468c9c3144SOlivier Houchard /* SP_usr is mapped to x13 */ 2478c9c3144SOlivier Houchard tf->tf_x[13] = stack; 2488c9c3144SOlivier Houchard /* LR_usr is mapped to x14 */ 2498c9c3144SOlivier Houchard tf->tf_x[14] = imgp->entry_addr; 2508c9c3144SOlivier Houchard tf->tf_elr = imgp->entry_addr; 2518c9c3144SOlivier Houchard tf->tf_spsr = PSR_M_32; 2528c9c3144SOlivier Houchard } 2539dcf90f8SEd Schouten 2549dcf90f8SEd Schouten void 2558c9c3144SOlivier Houchard elf32_dump_thread(struct thread *td, void *dst, size_t *off) 2569dcf90f8SEd Schouten { 2578c9c3144SOlivier Houchard /* XXX: VFP */ 2589dcf90f8SEd Schouten } 259