1 /* $NetBSD: linux_exec_aout.c,v 1.68 2015/10/18 16:59:19 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * based on exec_aout.c, sunos_exec.c and svr4_exec.c 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.68 2015/10/18 16:59:19 maxv Exp $"); 38 39 #ifdef _KERNEL_OPT 40 #include "opt_execfmt.h" 41 #endif 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/proc.h> 47 #include <sys/namei.h> 48 #include <sys/vnode.h> 49 #include <sys/mount.h> 50 #include <sys/exec.h> 51 52 #include <sys/mman.h> 53 #include <sys/syscallargs.h> 54 55 #include <sys/cpu.h> 56 #include <machine/reg.h> 57 58 #include <compat/linux/common/linux_types.h> 59 #include <compat/linux/common/linux_signal.h> 60 #include <compat/linux/common/linux_util.h> 61 #include <compat/linux/common/linux_exec.h> 62 #include <compat/linux/common/linux_machdep.h> 63 64 #include <compat/linux/linux_syscallargs.h> 65 #include <compat/linux/linux_syscall.h> 66 67 int linux_aout_copyargs(struct lwp *, struct exec_package *, 68 struct ps_strings *, char **, void *); 69 70 static int exec_linux_aout_prep_zmagic(struct lwp *, 71 struct exec_package *); 72 static int exec_linux_aout_prep_nmagic(struct lwp *, 73 struct exec_package *); 74 static int exec_linux_aout_prep_omagic(struct lwp *, 75 struct exec_package *); 76 static int exec_linux_aout_prep_qmagic(struct lwp *, 77 struct exec_package *); 78 79 int 80 linux_aout_copyargs(struct lwp *l, struct exec_package *pack, 81 struct ps_strings *arginfo, char **stackp, void *argp) 82 { 83 char **cpp = (char **)*stackp; 84 char **stk = (char **)*stackp; 85 char *dp, *sp; 86 size_t len; 87 void *nullp = NULL; 88 int argc = arginfo->ps_nargvstr; 89 int envc = arginfo->ps_nenvstr; 90 int error; 91 92 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0) 93 return error; 94 95 /* leave room for envp and argv */ 96 cpp += 2; 97 if ((error = copyout(&cpp, &stk[1], sizeof (cpp))) != 0) 98 return error; 99 100 dp = (char *) (cpp + argc + envc + 2); 101 sp = argp; 102 103 /* XXX don't copy them out, remap them! */ 104 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 105 106 for (; --argc >= 0; sp += len, dp += len) 107 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 || 108 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) 109 return error; 110 111 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) 112 return error; 113 114 if ((error = copyout(&cpp, &stk[2], sizeof (cpp))) != 0) 115 return error; 116 117 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 118 119 for (; --envc >= 0; sp += len, dp += len) 120 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 || 121 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) 122 return error; 123 124 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) 125 return error; 126 127 *stackp = (char *)cpp; 128 return 0; 129 } 130 131 int 132 exec_linux_aout_makecmds(struct lwp *l, struct exec_package *epp) 133 { 134 struct exec *linux_ep = epp->ep_hdr; 135 int machtype, magic; 136 int error = ENOEXEC; 137 138 if (epp->ep_hdrvalid < sizeof(struct exec)) 139 return ENOEXEC; 140 141 magic = LINUX_N_MAGIC(linux_ep); 142 machtype = LINUX_N_MACHTYPE(linux_ep); 143 144 if (machtype != LINUX_MID_MACHINE) 145 return (ENOEXEC); 146 147 switch (magic) { 148 case QMAGIC: 149 error = exec_linux_aout_prep_qmagic(l, epp); 150 break; 151 case ZMAGIC: 152 error = exec_linux_aout_prep_zmagic(l, epp); 153 break; 154 case NMAGIC: 155 error = exec_linux_aout_prep_nmagic(l, epp); 156 break; 157 case OMAGIC: 158 error = exec_linux_aout_prep_omagic(l, epp); 159 break; 160 } 161 return error; 162 } 163 164 /* 165 * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400 166 * is very likely not page aligned on most architectures, it is treated 167 * as an NMAGIC here. XXX 168 */ 169 170 static int 171 exec_linux_aout_prep_zmagic(struct lwp *l, struct exec_package *epp) 172 { 173 struct exec *execp = epp->ep_hdr; 174 175 epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC); 176 epp->ep_tsize = execp->a_text; 177 epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC); 178 epp->ep_dsize = execp->a_data + execp->a_bss; 179 epp->ep_entry = execp->a_entry; 180 181 /* set up command for text segment */ 182 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 183 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC), 184 VM_PROT_READ|VM_PROT_EXECUTE); 185 186 /* set up command for data segment */ 187 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 188 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC), 189 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 190 191 /* set up command for bss segment */ 192 if (execp->a_bss) 193 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 194 epp->ep_daddr + execp->a_data, NULLVP, 0, 195 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 196 197 return (*epp->ep_esch->es_setup_stack)(l, epp); 198 } 199 200 /* 201 * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package. 202 * Not different from the normal stuff. 203 */ 204 205 static int 206 exec_linux_aout_prep_nmagic(struct lwp *l, struct exec_package *epp) 207 { 208 struct exec *execp = epp->ep_hdr; 209 long bsize, baddr; 210 211 epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC); 212 epp->ep_tsize = execp->a_text; 213 epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC); 214 epp->ep_dsize = execp->a_data + execp->a_bss; 215 epp->ep_entry = execp->a_entry; 216 217 /* set up command for text segment */ 218 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 219 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC), 220 VM_PROT_READ|VM_PROT_EXECUTE); 221 222 /* set up command for data segment */ 223 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 224 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC), 225 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 226 227 /* set up command for bss segment */ 228 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 229 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 230 if (bsize > 0) 231 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 232 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 233 234 return (*epp->ep_esch->es_setup_stack)(l, epp); 235 } 236 237 /* 238 * exec_aout_prep_omagic(): Prepare Linux OMAGIC package. 239 * Business as usual. 240 */ 241 242 static int 243 exec_linux_aout_prep_omagic(struct lwp *l, struct exec_package *epp) 244 { 245 struct exec *execp = epp->ep_hdr; 246 long dsize, bsize, baddr; 247 248 epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC); 249 epp->ep_tsize = execp->a_text; 250 epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC); 251 epp->ep_dsize = execp->a_data + execp->a_bss; 252 epp->ep_entry = execp->a_entry; 253 254 /* set up command for text and data segments */ 255 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 256 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 257 LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 258 259 /* set up command for bss segment */ 260 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 261 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 262 if (bsize > 0) 263 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 264 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 265 266 /* 267 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 268 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 269 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 270 * respectively to page boundaries. 271 * Compensate `ep_dsize' for the amount of data covered by the last 272 * text page. 273 */ 274 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, 275 PAGE_SIZE); 276 epp->ep_dsize = (dsize > 0) ? dsize : 0; 277 return (*epp->ep_esch->es_setup_stack)(l, epp); 278 } 279 280 static int 281 exec_linux_aout_prep_qmagic(struct lwp *l, struct exec_package *epp) 282 { 283 struct exec *execp = epp->ep_hdr; 284 int error; 285 286 epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC); 287 epp->ep_tsize = execp->a_text; 288 epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC); 289 epp->ep_dsize = execp->a_data + execp->a_bss; 290 epp->ep_entry = execp->a_entry; 291 292 error = vn_marktext(epp->ep_vp); 293 if (error) 294 return (error); 295 296 /* set up command for text segment */ 297 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 298 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC), 299 VM_PROT_READ|VM_PROT_EXECUTE); 300 301 /* set up command for data segment */ 302 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 303 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC), 304 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 305 306 /* set up command for bss segment */ 307 if (execp->a_bss) 308 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 309 epp->ep_daddr + execp->a_data, NULLVP, 0, 310 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 311 312 return (*epp->ep_esch->es_setup_stack)(l, epp); 313 } 314