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