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> 47548a2ec4SAndrew Turner #include <sys/reg.h> 488c9c3144SOlivier Houchard #include <sys/sysent.h> 499dcf90f8SEd Schouten #include <sys/imgact_elf.h> 508c9c3144SOlivier Houchard #include <sys/syscall.h> 518c9c3144SOlivier Houchard #include <sys/signalvar.h> 528c9c3144SOlivier Houchard #include <sys/vnode.h> 538c9c3144SOlivier Houchard 548c9c3144SOlivier Houchard #include <machine/elf.h> 55953a7d7cSAlex Richardson #ifdef VFP 56953a7d7cSAlex Richardson #include <machine/vfp.h> 57953a7d7cSAlex Richardson #endif 588c9c3144SOlivier Houchard 598c9c3144SOlivier Houchard #include <compat/freebsd32/freebsd32_util.h> 608c9c3144SOlivier Houchard 618c9c3144SOlivier Houchard #define FREEBSD32_MINUSER 0x00001000 628c9c3144SOlivier Houchard #define FREEBSD32_MAXUSER ((1ul << 32) - PAGE_SIZE) 638c9c3144SOlivier Houchard #define FREEBSD32_SHAREDPAGE (FREEBSD32_MAXUSER - PAGE_SIZE) 648c9c3144SOlivier Houchard #define FREEBSD32_USRSTACK FREEBSD32_SHAREDPAGE 658c9c3144SOlivier Houchard 668c9c3144SOlivier Houchard extern const char *freebsd32_syscallnames[]; 678c9c3144SOlivier Houchard 688c9c3144SOlivier Houchard extern char aarch32_sigcode[]; 698c9c3144SOlivier Houchard extern int sz_aarch32_sigcode; 708c9c3144SOlivier Houchard 718c9c3144SOlivier Houchard static int freebsd32_fetch_syscall_args(struct thread *td); 728c9c3144SOlivier Houchard static void freebsd32_setregs(struct thread *td, struct image_params *imgp, 738c9c3144SOlivier Houchard u_long stack); 748c9c3144SOlivier Houchard static void freebsd32_set_syscall_retval(struct thread *, int); 758c9c3144SOlivier Houchard 760cad2aa2SKonstantin Belousov static boolean_t elf32_arm_abi_supported(struct image_params *, int32_t *, 770cad2aa2SKonstantin Belousov uint32_t *); 788c9c3144SOlivier Houchard 798c9c3144SOlivier Houchard extern void freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 808c9c3144SOlivier Houchard 81bbe80bffSPeter Grehan u_long __read_frequently elf32_hwcap; 82bbe80bffSPeter Grehan u_long __read_frequently elf32_hwcap2; 83bbe80bffSPeter Grehan 848c9c3144SOlivier Houchard static struct sysentvec elf32_freebsd_sysvec = { 858c9c3144SOlivier Houchard .sv_size = SYS_MAXSYSCALL, 868c9c3144SOlivier Houchard .sv_table = freebsd32_sysent, 878c9c3144SOlivier Houchard .sv_fixup = elf32_freebsd_fixup, 888c9c3144SOlivier Houchard .sv_sendsig = freebsd32_sendsig, 898c9c3144SOlivier Houchard .sv_sigcode = aarch32_sigcode, 908c9c3144SOlivier Houchard .sv_szsigcode = &sz_aarch32_sigcode, 918c9c3144SOlivier Houchard .sv_name = "FreeBSD ELF32", 928c9c3144SOlivier Houchard .sv_coredump = elf32_coredump, 93435754a5SEdward Tomasz Napierala .sv_elf_core_osabi = ELFOSABI_FREEBSD, 94435754a5SEdward Tomasz Napierala .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR, 95435754a5SEdward Tomasz Napierala .sv_elf_core_prepare_notes = elf32_prepare_notes, 968c9c3144SOlivier Houchard .sv_imgact_try = NULL, 978c9c3144SOlivier Houchard .sv_minsigstksz = MINSIGSTKSZ, 988c9c3144SOlivier Houchard .sv_minuser = FREEBSD32_MINUSER, 998c9c3144SOlivier Houchard .sv_maxuser = FREEBSD32_MAXUSER, 1008c9c3144SOlivier Houchard .sv_usrstack = FREEBSD32_USRSTACK, 1018c9c3144SOlivier Houchard .sv_psstrings = FREEBSD32_PS_STRINGS, 1023fc21fddSMark Johnston .sv_psstringssz = sizeof(struct freebsd32_ps_strings), 1038c9c3144SOlivier Houchard .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 104e3532331SJohn Baldwin .sv_copyout_auxargs = elf32_freebsd_copyout_auxargs, 1058c9c3144SOlivier Houchard .sv_copyout_strings = freebsd32_copyout_strings, 1068c9c3144SOlivier Houchard .sv_setregs = freebsd32_setregs, 1078c9c3144SOlivier Houchard .sv_fixlimit = NULL, // XXX 1088c9c3144SOlivier Houchard .sv_maxssiz = NULL, 109f8e8a06dSConrad Meyer .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_TIMEKEEP | 110f8e8a06dSConrad Meyer SV_RNG_SEED_VER, 1118c9c3144SOlivier Houchard .sv_set_syscall_retval = freebsd32_set_syscall_retval, 1128c9c3144SOlivier Houchard .sv_fetch_syscall_args = freebsd32_fetch_syscall_args, 1138c9c3144SOlivier Houchard .sv_syscallnames = freebsd32_syscallnames, 1148c9c3144SOlivier Houchard .sv_shared_page_base = FREEBSD32_SHAREDPAGE, 1158c9c3144SOlivier Houchard .sv_shared_page_len = PAGE_SIZE, 1168c9c3144SOlivier Houchard .sv_schedtail = NULL, 1178c9c3144SOlivier Houchard .sv_thread_detach = NULL, 1188c9c3144SOlivier Houchard .sv_trap = NULL, 119bbe80bffSPeter Grehan .sv_hwcap = &elf32_hwcap, 120bbe80bffSPeter Grehan .sv_hwcap2 = &elf32_hwcap2, 12128a66fc3SKonstantin Belousov .sv_onexec_old = exec_onexec_old, 12228a66fc3SKonstantin Belousov .sv_onexit = exit_onexit, 123548a2ec4SAndrew Turner .sv_regset_begin = SET_BEGIN(__elfN(regset)), 124548a2ec4SAndrew Turner .sv_regset_end = SET_LIMIT(__elfN(regset)), 1258c9c3144SOlivier Houchard }; 1268c9c3144SOlivier Houchard INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec); 1278c9c3144SOlivier Houchard 1288c9c3144SOlivier Houchard static Elf32_Brandinfo freebsd32_brand_info = { 1298c9c3144SOlivier Houchard .brand = ELFOSABI_FREEBSD, 1308c9c3144SOlivier Houchard .machine = EM_ARM, 1318c9c3144SOlivier Houchard .compat_3_brand = "FreeBSD", 1328c9c3144SOlivier Houchard .emul_path = NULL, 1338c9c3144SOlivier Houchard .interp_path = "/libexec/ld-elf.so.1", 1348c9c3144SOlivier Houchard .sysvec = &elf32_freebsd_sysvec, 13524718606SJustin Hibbits .interp_newpath = "/libexec/ld-elf32.so.1", 1368c9c3144SOlivier Houchard .brand_note = &elf32_freebsd_brandnote, 1378c9c3144SOlivier Houchard .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 1388c9c3144SOlivier Houchard .header_supported= elf32_arm_abi_supported, 1398c9c3144SOlivier Houchard }; 1408c9c3144SOlivier Houchard 1418c9c3144SOlivier Houchard SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST, 1428c9c3144SOlivier Houchard (sysinit_cfunc_t)elf32_insert_brand_entry, &freebsd32_brand_info); 1438c9c3144SOlivier Houchard 1448c9c3144SOlivier Houchard static boolean_t 1450cad2aa2SKonstantin Belousov elf32_arm_abi_supported(struct image_params *imgp, int32_t *osrel __unused, 1460cad2aa2SKonstantin Belousov uint32_t *fctl0 __unused) 1478c9c3144SOlivier Houchard { 1488c9c3144SOlivier Houchard const Elf32_Ehdr *hdr; 1498c9c3144SOlivier Houchard 1508c9c3144SOlivier Houchard /* Check if we support AArch32 */ 15144e446a1SAndrew Turner if (ID_AA64PFR0_EL0_VAL(READ_SPECIALREG(id_aa64pfr0_el1)) != 1528c9c3144SOlivier Houchard ID_AA64PFR0_EL0_64_32) 1538c9c3144SOlivier Houchard return (FALSE); 1548c9c3144SOlivier Houchard 15534784d17SWarner Losh #define EF_ARM_EABI_FREEBSD_MIN EF_ARM_EABI_VER4 1568c9c3144SOlivier Houchard hdr = (const Elf32_Ehdr *)imgp->image_header; 1578c9c3144SOlivier Houchard if (EF_ARM_EABI_VERSION(hdr->e_flags) < EF_ARM_EABI_FREEBSD_MIN) { 1588c9c3144SOlivier Houchard if (bootverbose) 1598c9c3144SOlivier Houchard uprintf("Attempting to execute non EABI binary " 1608c9c3144SOlivier Houchard "(rev %d) image %s", 1618c9c3144SOlivier Houchard EF_ARM_EABI_VERSION(hdr->e_flags), 1628c9c3144SOlivier Houchard imgp->args->fname); 1638c9c3144SOlivier Houchard return (FALSE); 1648c9c3144SOlivier Houchard } 1658c9c3144SOlivier Houchard 1668c9c3144SOlivier Houchard return (TRUE); 1678c9c3144SOlivier Houchard } 1688c9c3144SOlivier Houchard 1698c9c3144SOlivier Houchard static int 1708c9c3144SOlivier Houchard freebsd32_fetch_syscall_args(struct thread *td) 1718c9c3144SOlivier Houchard { 1728c9c3144SOlivier Houchard struct proc *p; 1738c9c3144SOlivier Houchard register_t *ap; 1748c9c3144SOlivier Houchard struct syscall_args *sa; 1751e2521ffSEdward Tomasz Napierala int error, i, nap, narg; 1768c9c3144SOlivier Houchard unsigned int args[4]; 1778c9c3144SOlivier Houchard 1788c9c3144SOlivier Houchard nap = 4; 1798c9c3144SOlivier Houchard p = td->td_proc; 1808c9c3144SOlivier Houchard ap = td->td_frame->tf_x; 1818c9c3144SOlivier Houchard sa = &td->td_sa; 1828c9c3144SOlivier Houchard 1838c9c3144SOlivier Houchard /* r7 is the syscall id */ 1848c9c3144SOlivier Houchard sa->code = td->td_frame->tf_x[7]; 185cf98bc28SDavid Chisnall sa->original_code = sa->code; 1868c9c3144SOlivier Houchard 1878c9c3144SOlivier Houchard if (sa->code == SYS_syscall) { 1888c9c3144SOlivier Houchard sa->code = *ap++; 1898c9c3144SOlivier Houchard nap--; 1908c9c3144SOlivier Houchard } else if (sa->code == SYS___syscall) { 1918c9c3144SOlivier Houchard sa->code = ap[1]; 1928c9c3144SOlivier Houchard nap -= 2; 1938c9c3144SOlivier Houchard ap += 2; 1948c9c3144SOlivier Houchard } 1958c9c3144SOlivier Houchard 1968c9c3144SOlivier Houchard if (sa->code >= p->p_sysent->sv_size) 1978c9c3144SOlivier Houchard sa->callp = &p->p_sysent->sv_table[0]; 1988c9c3144SOlivier Houchard else 1998c9c3144SOlivier Houchard sa->callp = &p->p_sysent->sv_table[sa->code]; 2008c9c3144SOlivier Houchard 2011e2521ffSEdward Tomasz Napierala narg = sa->callp->sy_narg; 2028c9c3144SOlivier Houchard for (i = 0; i < nap; i++) 2038c9c3144SOlivier Houchard sa->args[i] = ap[i]; 2041e2521ffSEdward Tomasz Napierala if (narg > nap) { 2051e2521ffSEdward Tomasz Napierala if (narg - nap > nitems(args)) 2068c9c3144SOlivier Houchard panic("Too many system call arguiments"); 2078c9c3144SOlivier Houchard error = copyin((void *)td->td_frame->tf_x[13], args, 2081e2521ffSEdward Tomasz Napierala (narg - nap) * sizeof(int)); 2095b616dafSAndrew Turner if (error != 0) 2105b616dafSAndrew Turner return (error); 2111e2521ffSEdward Tomasz Napierala for (i = 0; i < (narg - nap); i++) 2128c9c3144SOlivier Houchard sa->args[i + nap] = args[i]; 2138c9c3144SOlivier Houchard } 2148c9c3144SOlivier Houchard 2158c9c3144SOlivier Houchard td->td_retval[0] = 0; 2168c9c3144SOlivier Houchard td->td_retval[1] = 0; 2178c9c3144SOlivier Houchard 2188c9c3144SOlivier Houchard return (0); 2198c9c3144SOlivier Houchard } 2208c9c3144SOlivier Houchard 2218c9c3144SOlivier Houchard static void 2228c9c3144SOlivier Houchard freebsd32_set_syscall_retval(struct thread *td, int error) 2238c9c3144SOlivier Houchard { 2248c9c3144SOlivier Houchard struct trapframe *frame; 2258c9c3144SOlivier Houchard 2268c9c3144SOlivier Houchard frame = td->td_frame; 2278c9c3144SOlivier Houchard switch (error) { 2288c9c3144SOlivier Houchard case 0: 2298c9c3144SOlivier Houchard frame->tf_x[0] = td->td_retval[0]; 2308c9c3144SOlivier Houchard frame->tf_x[1] = td->td_retval[1]; 2318c9c3144SOlivier Houchard frame->tf_spsr &= ~PSR_C; 2328c9c3144SOlivier Houchard break; 2338c9c3144SOlivier Houchard case ERESTART: 2348c9c3144SOlivier Houchard /* 2358c9c3144SOlivier Houchard * Reconstruct the pc to point at the swi. 2368c9c3144SOlivier Houchard */ 2378c9c3144SOlivier Houchard if ((frame->tf_spsr & PSR_T) != 0) 2388c9c3144SOlivier Houchard frame->tf_elr -= 2; //THUMB_INSN_SIZE; 2398c9c3144SOlivier Houchard else 2408c9c3144SOlivier Houchard frame->tf_elr -= 4; //INSN_SIZE; 2418c9c3144SOlivier Houchard break; 2428c9c3144SOlivier Houchard case EJUSTRETURN: 2438c9c3144SOlivier Houchard /* nothing to do */ 2448c9c3144SOlivier Houchard break; 2458c9c3144SOlivier Houchard default: 2468c9c3144SOlivier Houchard frame->tf_x[0] = error; 2478c9c3144SOlivier Houchard frame->tf_spsr |= PSR_C; 2488c9c3144SOlivier Houchard break; 2498c9c3144SOlivier Houchard } 2508c9c3144SOlivier Houchard } 2518c9c3144SOlivier Houchard 2528c9c3144SOlivier Houchard static void 2538c9c3144SOlivier Houchard freebsd32_setregs(struct thread *td, struct image_params *imgp, 25431174518SJohn Baldwin uintptr_t stack) 2558c9c3144SOlivier Houchard { 2568c9c3144SOlivier Houchard struct trapframe *tf = td->td_frame; 257a2a8b582SMitchell Horne struct pcb *pcb = td->td_pcb; 2588c9c3144SOlivier Houchard 2598c9c3144SOlivier Houchard memset(tf, 0, sizeof(struct trapframe)); 2608c9c3144SOlivier Houchard 2618c9c3144SOlivier Houchard /* 2628c9c3144SOlivier Houchard * We need to set x0 for init as it doesn't call 2638c9c3144SOlivier Houchard * cpu_set_syscall_retval to copy the value. We also 2648c9c3144SOlivier Houchard * need to set td_retval for the cases where we do. 2658c9c3144SOlivier Houchard */ 2668c9c3144SOlivier Houchard tf->tf_x[0] = stack; 2678c9c3144SOlivier Houchard /* SP_usr is mapped to x13 */ 2688c9c3144SOlivier Houchard tf->tf_x[13] = stack; 2698c9c3144SOlivier Houchard /* LR_usr is mapped to x14 */ 2708c9c3144SOlivier Houchard tf->tf_x[14] = imgp->entry_addr; 2718c9c3144SOlivier Houchard tf->tf_elr = imgp->entry_addr; 2728c9c3144SOlivier Houchard tf->tf_spsr = PSR_M_32; 273712c060cSOlivier Houchard if ((uint32_t)imgp->entry_addr & 1) 274712c060cSOlivier Houchard tf->tf_spsr |= PSR_T; 275953a7d7cSAlex Richardson 276953a7d7cSAlex Richardson #ifdef VFP 277a2a8b582SMitchell Horne vfp_reset_state(td, pcb); 278953a7d7cSAlex Richardson #endif 279a2a8b582SMitchell Horne 280a2a8b582SMitchell Horne /* 281a2a8b582SMitchell Horne * Clear debug register state. It is not applicable to the new process. 282a2a8b582SMitchell Horne */ 283a2a8b582SMitchell Horne bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs)); 2848c9c3144SOlivier Houchard } 2859dcf90f8SEd Schouten 2869dcf90f8SEd Schouten void 2878c9c3144SOlivier Houchard elf32_dump_thread(struct thread *td, void *dst, size_t *off) 2889dcf90f8SEd Schouten { 2899dcf90f8SEd Schouten } 290