1 /* $NetBSD: netbsd32_exec_aout.c,v 1.10 2002/10/05 22:34:04 chs 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 * 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/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_aout.c,v 1.10 2002/10/05 22:34:04 chs Exp $"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/proc.h> 41 #include <sys/malloc.h> 42 #include <sys/vnode.h> 43 #include <sys/exec.h> 44 #include <sys/resourcevar.h> 45 #include <sys/signal.h> 46 #include <sys/signalvar.h> 47 48 #include <compat/netbsd32/netbsd32.h> 49 #ifndef EXEC_AOUT 50 #define EXEC_AOUT 51 #endif 52 #include <compat/netbsd32/netbsd32_exec.h> 53 54 #include <machine/frame.h> 55 #include <machine/netbsd32_machdep.h> 56 57 int netbsd32_copyinargs __P((struct exec_package *, struct ps_strings *, 58 void *, size_t, const void *, const void *)); 59 60 int netbsd32_exec_aout_setup_stack __P((struct proc *p, 61 struct exec_package *epp)); 62 63 /* 64 * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format 65 * executable. 66 * 67 * Given a proc pointer and an exec package pointer, see if the referent 68 * of the epp is in netbsd32 a.out format. Check 'standard' magic 69 * numbers for this architecture. 70 * 71 * This function, in the former case, or the hook, in the latter, is 72 * responsible for creating a set of vmcmds which can be used to build 73 * the process's vm space and inserting them into the exec package. 74 */ 75 76 int 77 exec_netbsd32_makecmds(p, epp) 78 struct proc *p; 79 struct exec_package *epp; 80 { 81 netbsd32_u_long midmag, magic; 82 u_short mid; 83 int error; 84 struct netbsd32_exec *execp = epp->ep_hdr; 85 86 if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec)) 87 return ENOEXEC; 88 89 midmag = (netbsd32_u_long)ntohl(execp->a_midmag); 90 mid = (midmag >> 16) & 0x3ff; 91 magic = midmag & 0xffff; 92 93 midmag = mid << 16 | magic; 94 95 switch (midmag) { 96 case (MID_SPARC << 16) | ZMAGIC: 97 error = netbsd32_exec_aout_prep_zmagic(p, epp); 98 break; 99 case (MID_SPARC << 16) | NMAGIC: 100 error = netbsd32_exec_aout_prep_nmagic(p, epp); 101 break; 102 case (MID_SPARC << 16) | OMAGIC: 103 error = netbsd32_exec_aout_prep_omagic(p, epp); 104 break; 105 default: 106 /* Invalid magic */ 107 error = ENOEXEC; 108 break; 109 } 110 111 if (error == 0) { 112 /* set up our emulation information */ 113 epp->ep_flags |= EXEC_32; 114 } else 115 kill_vmcmds(&epp->ep_vmcmds); 116 117 return error; 118 } 119 120 /* 121 * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's 122 * exec package 123 * 124 * First, set of the various offsets/lengths in the exec package. 125 * 126 * Then, mark the text image busy (so it can be demand paged) or error 127 * out if this is not possible. Finally, set up vmcmds for the 128 * text, data, bss, and stack segments. 129 */ 130 131 int 132 netbsd32_exec_aout_prep_zmagic(p, epp) 133 struct proc *p; 134 struct exec_package *epp; 135 { 136 struct netbsd32_exec *execp = epp->ep_hdr; 137 int error; 138 139 epp->ep_taddr = USRTEXT; 140 epp->ep_tsize = execp->a_text; 141 epp->ep_daddr = epp->ep_taddr + execp->a_text; 142 epp->ep_dsize = execp->a_data + execp->a_bss; 143 epp->ep_entry = execp->a_entry; 144 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 145 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 146 147 error = vn_marktext(epp->ep_vp); 148 if (error) 149 return (error); 150 151 /* set up command for text segment */ 152 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 153 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE); 154 155 /* set up command for data segment */ 156 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 157 epp->ep_daddr, epp->ep_vp, execp->a_text, 158 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 159 160 /* set up command for bss segment */ 161 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 162 epp->ep_daddr + execp->a_data, NULLVP, 0, 163 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 164 165 return netbsd32_exec_aout_setup_stack(p, epp); 166 } 167 168 /* 169 * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's 170 * exec package 171 */ 172 173 int 174 netbsd32_exec_aout_prep_nmagic(p, epp) 175 struct proc *p; 176 struct exec_package *epp; 177 { 178 struct netbsd32_exec *execp = epp->ep_hdr; 179 long bsize, baddr; 180 181 epp->ep_taddr = USRTEXT; 182 epp->ep_tsize = execp->a_text; 183 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ); 184 epp->ep_dsize = execp->a_data + execp->a_bss; 185 epp->ep_entry = execp->a_entry; 186 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 187 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 188 189 /* set up command for text segment */ 190 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 191 epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec), 192 VM_PROT_READ|VM_PROT_EXECUTE); 193 194 /* set up command for data segment */ 195 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 196 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec), 197 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 198 199 /* set up command for bss segment */ 200 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG); 201 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 202 if (bsize > 0) 203 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 204 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 205 206 return netbsd32_exec_aout_setup_stack(p, epp); 207 } 208 209 /* 210 * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's 211 * exec package 212 */ 213 214 int 215 netbsd32_exec_aout_prep_omagic(p, epp) 216 struct proc *p; 217 struct exec_package *epp; 218 { 219 struct netbsd32_exec *execp = epp->ep_hdr; 220 long dsize, bsize, baddr; 221 222 epp->ep_taddr = USRTEXT; 223 epp->ep_tsize = execp->a_text; 224 epp->ep_daddr = epp->ep_taddr + execp->a_text; 225 epp->ep_dsize = execp->a_data + execp->a_bss; 226 epp->ep_entry = execp->a_entry; 227 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 228 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 229 230 /* set up command for text and data segments */ 231 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 232 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 233 sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 234 235 /* set up command for bss segment */ 236 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG); 237 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 238 if (bsize > 0) 239 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 240 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 241 242 /* 243 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 244 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 245 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 246 * respectively to page boundaries. 247 * Compensate `ep_dsize' for the amount of data covered by the last 248 * text page. 249 */ 250 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG); 251 epp->ep_dsize = (dsize > 0) ? dsize : 0; 252 return netbsd32_exec_aout_setup_stack(p, epp); 253 } 254 255 /* 256 * netbsd32_exec_aout_setup_stack(): Set up the stack segment for an a.out 257 * executable. 258 * 259 * Note that the ep_ssize parameter must be set to be the current stack 260 * limit; this is adjusted in the body of execve() to yield the 261 * appropriate stack segment usage once the argument length is 262 * calculated. 263 * 264 * This function returns an int for uniformity with other (future) formats' 265 * stack setup functions. They might have errors to return. 266 */ 267 int 268 netbsd32_exec_aout_setup_stack(struct proc *p, struct exec_package *epp) 269 { 270 271 epp->ep_maxsaddr = USRSTACK32 - MAXSSIZ; 272 epp->ep_minsaddr = USRSTACK32; 273 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur; 274 275 /* 276 * set up commands for stack. note that this takes *two*, one to 277 * map the part of the stack which we can access, and one to map 278 * the part which we can't. 279 * 280 * arguably, it could be made into one, but that would require the 281 * addition of another mapping proc, which is unnecessary 282 * 283 * note that in memory, things assumed to be: 0 ... ep_maxsaddr 284 * <stack> ep_minsaddr 285 */ 286 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 287 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr), 288 epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE); 289 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize, 290 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0, 291 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 292 293 return 0; 294 } 295