1 /*- 2 * Copyright (c) 2014, 2015 The FreeBSD Foundation. 3 * Copyright (c) 2014, 2017 Andrew Turner. 4 * Copyright (c) 2018 Olivier Houchard 5 * All rights reserved. 6 * 7 * This software was developed by Andrew Turner under 8 * sponsorship from the FreeBSD Foundation. 9 * 10 * Portions of this software were developed by Konstantin Belousov 11 * under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #define __ELF_WORD_SIZE 32 39 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/systm.h> 43 #include <sys/exec.h> 44 #include <sys/imgact.h> 45 #include <sys/linker.h> 46 #include <sys/proc.h> 47 #include <sys/sysent.h> 48 #include <sys/imgact_elf.h> 49 #include <sys/syscall.h> 50 #include <sys/signalvar.h> 51 #include <sys/vnode.h> 52 53 #include <machine/elf.h> 54 55 #include <compat/freebsd32/freebsd32_util.h> 56 57 #define FREEBSD32_MINUSER 0x00001000 58 #define FREEBSD32_MAXUSER ((1ul << 32) - PAGE_SIZE) 59 #define FREEBSD32_SHAREDPAGE (FREEBSD32_MAXUSER - PAGE_SIZE) 60 #define FREEBSD32_USRSTACK FREEBSD32_SHAREDPAGE 61 62 extern const char *freebsd32_syscallnames[]; 63 64 extern char aarch32_sigcode[]; 65 extern int sz_aarch32_sigcode; 66 67 static int freebsd32_fetch_syscall_args(struct thread *td); 68 static void freebsd32_setregs(struct thread *td, struct image_params *imgp, 69 u_long stack); 70 static void freebsd32_set_syscall_retval(struct thread *, int); 71 72 static boolean_t elf32_arm_abi_supported(struct image_params *); 73 74 extern void freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 75 76 static struct sysentvec elf32_freebsd_sysvec = { 77 .sv_size = SYS_MAXSYSCALL, 78 .sv_table = freebsd32_sysent, 79 .sv_errsize = 0, 80 .sv_errtbl = NULL, 81 .sv_transtrap = NULL, 82 .sv_fixup = elf32_freebsd_fixup, 83 .sv_sendsig = freebsd32_sendsig, 84 .sv_sigcode = aarch32_sigcode, 85 .sv_szsigcode = &sz_aarch32_sigcode, 86 .sv_name = "FreeBSD ELF32", 87 .sv_coredump = elf32_coredump, 88 .sv_imgact_try = NULL, 89 .sv_minsigstksz = MINSIGSTKSZ, 90 .sv_minuser = FREEBSD32_MINUSER, 91 .sv_maxuser = FREEBSD32_MAXUSER, 92 .sv_usrstack = FREEBSD32_USRSTACK, 93 .sv_psstrings = FREEBSD32_PS_STRINGS, 94 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 95 .sv_copyout_strings = freebsd32_copyout_strings, 96 .sv_setregs = freebsd32_setregs, 97 .sv_fixlimit = NULL, // XXX 98 .sv_maxssiz = NULL, 99 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_TIMEKEEP, 100 .sv_set_syscall_retval = freebsd32_set_syscall_retval, 101 .sv_fetch_syscall_args = freebsd32_fetch_syscall_args, 102 .sv_syscallnames = freebsd32_syscallnames, 103 .sv_shared_page_base = FREEBSD32_SHAREDPAGE, 104 .sv_shared_page_len = PAGE_SIZE, 105 .sv_schedtail = NULL, 106 .sv_thread_detach = NULL, 107 .sv_trap = NULL, 108 }; 109 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec); 110 111 static Elf32_Brandinfo freebsd32_brand_info = { 112 .brand = ELFOSABI_FREEBSD, 113 .machine = EM_ARM, 114 .compat_3_brand = "FreeBSD", 115 .emul_path = NULL, 116 .interp_path = "/libexec/ld-elf.so.1", 117 .sysvec = &elf32_freebsd_sysvec, 118 .interp_newpath = NULL, 119 .brand_note = &elf32_freebsd_brandnote, 120 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 121 .header_supported= elf32_arm_abi_supported, 122 }; 123 124 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST, 125 (sysinit_cfunc_t)elf32_insert_brand_entry, &freebsd32_brand_info); 126 127 static boolean_t 128 elf32_arm_abi_supported(struct image_params *imgp) 129 { 130 const Elf32_Ehdr *hdr; 131 132 /* Check if we support AArch32 */ 133 if (ID_AA64PFR0_EL0(READ_SPECIALREG(id_aa64pfr0_el1)) != 134 ID_AA64PFR0_EL0_64_32) 135 return (FALSE); 136 137 #define EF_ARM_EABI_VERSION(x) (((x) & EF_ARM_EABIMASK) >> 24) 138 #define EF_ARM_EABI_FREEBSD_MIN 4 139 hdr = (const Elf32_Ehdr *)imgp->image_header; 140 if (EF_ARM_EABI_VERSION(hdr->e_flags) < EF_ARM_EABI_FREEBSD_MIN) { 141 if (bootverbose) 142 uprintf("Attempting to execute non EABI binary " 143 "(rev %d) image %s", 144 EF_ARM_EABI_VERSION(hdr->e_flags), 145 imgp->args->fname); 146 return (FALSE); 147 } 148 149 return (TRUE); 150 } 151 152 static int 153 freebsd32_fetch_syscall_args(struct thread *td) 154 { 155 struct proc *p; 156 register_t *ap; 157 struct syscall_args *sa; 158 int error, i, nap; 159 unsigned int args[4]; 160 161 nap = 4; 162 p = td->td_proc; 163 ap = td->td_frame->tf_x; 164 sa = &td->td_sa; 165 166 /* r7 is the syscall id */ 167 sa->code = td->td_frame->tf_x[7]; 168 169 if (sa->code == SYS_syscall) { 170 sa->code = *ap++; 171 nap--; 172 } else if (sa->code == SYS___syscall) { 173 sa->code = ap[1]; 174 nap -= 2; 175 ap += 2; 176 } 177 178 if (sa->code >= p->p_sysent->sv_size) 179 sa->callp = &p->p_sysent->sv_table[0]; 180 else 181 sa->callp = &p->p_sysent->sv_table[sa->code]; 182 183 sa->narg = sa->callp->sy_narg; 184 for (i = 0; i < nap; i++) 185 sa->args[i] = ap[i]; 186 if (sa->narg > nap) { 187 if ((sa->narg - nap) > nitems(args)) 188 panic("Too many system call arguiments"); 189 error = copyin((void *)td->td_frame->tf_x[13], args, 190 (sa->narg - nap) * sizeof(int)); 191 for (i = 0; i < (sa->narg - nap); i++) 192 sa->args[i + nap] = args[i]; 193 } 194 195 td->td_retval[0] = 0; 196 td->td_retval[1] = 0; 197 198 return (0); 199 } 200 201 static void 202 freebsd32_set_syscall_retval(struct thread *td, int error) 203 { 204 struct trapframe *frame; 205 206 frame = td->td_frame; 207 switch (error) { 208 case 0: 209 frame->tf_x[0] = td->td_retval[0]; 210 frame->tf_x[1] = td->td_retval[1]; 211 frame->tf_spsr &= ~PSR_C; 212 break; 213 case ERESTART: 214 /* 215 * Reconstruct the pc to point at the swi. 216 */ 217 if ((frame->tf_spsr & PSR_T) != 0) 218 frame->tf_elr -= 2; //THUMB_INSN_SIZE; 219 else 220 frame->tf_elr -= 4; //INSN_SIZE; 221 break; 222 case EJUSTRETURN: 223 /* nothing to do */ 224 break; 225 default: 226 frame->tf_x[0] = error; 227 frame->tf_spsr |= PSR_C; 228 break; 229 } 230 } 231 232 static void 233 freebsd32_setregs(struct thread *td, struct image_params *imgp, 234 u_long stack) 235 { 236 struct trapframe *tf = td->td_frame; 237 238 memset(tf, 0, sizeof(struct trapframe)); 239 240 /* 241 * We need to set x0 for init as it doesn't call 242 * cpu_set_syscall_retval to copy the value. We also 243 * need to set td_retval for the cases where we do. 244 */ 245 tf->tf_x[0] = stack; 246 /* SP_usr is mapped to x13 */ 247 tf->tf_x[13] = stack; 248 /* LR_usr is mapped to x14 */ 249 tf->tf_x[14] = imgp->entry_addr; 250 tf->tf_elr = imgp->entry_addr; 251 tf->tf_spsr = PSR_M_32; 252 } 253 254 void 255 elf32_dump_thread(struct thread *td, void *dst, size_t *off) 256 { 257 /* XXX: VFP */ 258 } 259