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