1 /* $NetBSD: netbsd32_exec_aout.c,v 1.25 2010/04/23 15:19:20 rmind Exp $ */ 2 /* from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */ 3 4 /* 5 * Copyright (c) 1998, 2001 Matthew R. Green. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Copyright (c) 1993, 1994 Christopher G. Demetriou 31 * All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. All advertising materials mentioning features or use of this software 42 * must display the following acknowledgement: 43 * This product includes software developed by Christopher G. Demetriou. 44 * 4. The name of the author may not be used to endorse or promote products 45 * derived from this software without specific prior written permission 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57 */ 58 59 #include <sys/cdefs.h> 60 __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_aout.c,v 1.25 2010/04/23 15:19:20 rmind Exp $"); 61 62 #include <sys/param.h> 63 #include <sys/systm.h> 64 #include <sys/proc.h> 65 #include <sys/vnode.h> 66 #include <sys/exec.h> 67 #include <sys/exec_aout.h> 68 #include <sys/resourcevar.h> 69 #include <sys/signal.h> 70 #include <sys/signalvar.h> 71 72 #include <compat/netbsd32/netbsd32.h> 73 #ifndef EXEC_AOUT 74 #define EXEC_AOUT 75 #endif 76 #include <compat/netbsd32/netbsd32_exec.h> 77 78 #include <machine/frame.h> 79 #include <machine/netbsd32_machdep.h> 80 81 int netbsd32_copyinargs(struct exec_package *, struct ps_strings *, 82 void *, size_t, const void *, const void *); 83 84 /* 85 * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format 86 * executable. 87 * 88 * Given a lwp pointer and an exec package pointer, see if the referent 89 * of the epp is in netbsd32 a.out format. Check 'standard' magic 90 * numbers for this architecture. 91 * 92 * This function, in the former case, or the hook, in the latter, is 93 * responsible for creating a set of vmcmds which can be used to build 94 * the process's vm space and inserting them into the exec package. 95 */ 96 97 int 98 exec_netbsd32_makecmds(struct lwp *l, struct exec_package *epp) 99 { 100 netbsd32_u_long midmag, magic; 101 u_short mid; 102 int error; 103 struct netbsd32_exec *execp = epp->ep_hdr; 104 105 if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec)) 106 return ENOEXEC; 107 108 midmag = (netbsd32_u_long)ntohl(execp->a_midmag); 109 mid = (midmag >> 16) & 0x3ff; 110 magic = midmag & 0xffff; 111 112 midmag = mid << 16 | magic; 113 114 /* this is already needed by setup_stack() */ 115 epp->ep_flags |= EXEC_32; 116 117 switch (midmag) { 118 case (NETBSD32_MID_MACHINE << 16) | ZMAGIC: 119 error = netbsd32_exec_aout_prep_zmagic(l, epp); 120 break; 121 case (NETBSD32_MID_MACHINE << 16) | NMAGIC: 122 error = netbsd32_exec_aout_prep_nmagic(l, epp); 123 break; 124 case (NETBSD32_MID_MACHINE << 16) | OMAGIC: 125 error = netbsd32_exec_aout_prep_omagic(l, epp); 126 break; 127 default: 128 /* Invalid magic */ 129 error = ENOEXEC; 130 break; 131 } 132 133 if (error) { 134 kill_vmcmds(&epp->ep_vmcmds); 135 epp->ep_flags &= ~EXEC_32; 136 } 137 return error; 138 } 139 140 /* 141 * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's 142 * exec package 143 * 144 * First, set of the various offsets/lengths in the exec package. 145 * 146 * Then, mark the text image busy (so it can be demand paged) or error 147 * out if this is not possible. Finally, set up vmcmds for the 148 * text, data, bss, and stack segments. 149 */ 150 151 int 152 netbsd32_exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp) 153 { 154 struct netbsd32_exec *execp = epp->ep_hdr; 155 int error; 156 157 epp->ep_taddr = AOUT_LDPGSZ; 158 epp->ep_tsize = execp->a_text; 159 epp->ep_daddr = epp->ep_taddr + execp->a_text; 160 epp->ep_dsize = execp->a_data + execp->a_bss; 161 epp->ep_entry = execp->a_entry; 162 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 163 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 164 165 error = vn_marktext(epp->ep_vp); 166 if (error) 167 return (error); 168 169 /* set up command for text segment */ 170 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 171 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE); 172 173 /* set up command for data segment */ 174 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 175 epp->ep_daddr, epp->ep_vp, execp->a_text, 176 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 177 178 /* set up command for bss segment */ 179 if (execp->a_bss > 0) 180 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 181 epp->ep_daddr + execp->a_data, NULLVP, 0, 182 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 183 184 return (*epp->ep_esch->es_setup_stack)(l, epp); 185 } 186 187 /* 188 * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's 189 * exec package 190 */ 191 192 int 193 netbsd32_exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp) 194 { 195 struct netbsd32_exec *execp = epp->ep_hdr; 196 long bsize, baddr; 197 198 epp->ep_taddr = AOUT_LDPGSZ; 199 epp->ep_tsize = execp->a_text; 200 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ); 201 epp->ep_dsize = execp->a_data + execp->a_bss; 202 epp->ep_entry = execp->a_entry; 203 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 204 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 205 206 /* set up command for text segment */ 207 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 208 epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec), 209 VM_PROT_READ|VM_PROT_EXECUTE); 210 211 /* set up command for data segment */ 212 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 213 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec), 214 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 215 216 /* set up command for bss segment */ 217 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 218 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 219 if (bsize > 0) 220 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 221 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 222 223 return (*epp->ep_esch->es_setup_stack)(l, epp); 224 } 225 226 /* 227 * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's 228 * exec package 229 */ 230 231 int 232 netbsd32_exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp) 233 { 234 struct netbsd32_exec *execp = epp->ep_hdr; 235 long dsize, bsize, baddr; 236 237 epp->ep_taddr = AOUT_LDPGSZ; 238 epp->ep_tsize = execp->a_text; 239 epp->ep_daddr = epp->ep_taddr + execp->a_text; 240 epp->ep_dsize = execp->a_data + execp->a_bss; 241 epp->ep_entry = execp->a_entry; 242 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 243 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 244 245 /* set up command for text and data segments */ 246 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 247 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 248 sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 249 250 /* set up command for bss segment */ 251 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 252 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 253 if (bsize > 0) 254 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 255 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 256 257 /* 258 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 259 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 260 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 261 * respectively to page boundaries. 262 * Compensate `ep_dsize' for the amount of data covered by the last 263 * text page. 264 */ 265 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, 266 PAGE_SIZE); 267 epp->ep_dsize = (dsize > 0) ? dsize : 0; 268 return (*epp->ep_esch->es_setup_stack)(l, epp); 269 } 270