1 /* $NetBSD: netbsd32_exec_aout.c,v 1.28 2014/10/24 21:08:36 christos 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.28 2014/10/24 21:08:36 christos 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 /* 82 * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format 83 * executable. 84 * 85 * Given a lwp pointer and an exec package pointer, see if the referent 86 * of the epp is in netbsd32 a.out format. Check 'standard' magic 87 * numbers for this architecture. 88 * 89 * This function, in the former case, or the hook, in the latter, is 90 * responsible for creating a set of vmcmds which can be used to build 91 * the process's vm space and inserting them into the exec package. 92 */ 93 94 int 95 exec_netbsd32_makecmds(struct lwp *l, struct exec_package *epp) 96 { 97 netbsd32_u_long midmag, magic; 98 u_short mid; 99 int error; 100 struct netbsd32_exec *execp = epp->ep_hdr; 101 102 if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec)) 103 return ENOEXEC; 104 105 midmag = (netbsd32_u_long)ntohl(execp->a_midmag); 106 mid = (midmag >> 16) & 0x3ff; 107 magic = midmag & 0xffff; 108 109 midmag = mid << 16 | magic; 110 111 /* this is already needed by setup_stack() */ 112 epp->ep_flags |= EXEC_32; 113 114 switch (midmag) { 115 case (NETBSD32_MID_MACHINE << 16) | ZMAGIC: 116 error = netbsd32_exec_aout_prep_zmagic(l, epp); 117 break; 118 case (NETBSD32_MID_MACHINE << 16) | NMAGIC: 119 error = netbsd32_exec_aout_prep_nmagic(l, epp); 120 break; 121 case (NETBSD32_MID_MACHINE << 16) | OMAGIC: 122 error = netbsd32_exec_aout_prep_omagic(l, epp); 123 break; 124 default: 125 /* Invalid magic */ 126 error = ENOEXEC; 127 break; 128 } 129 130 if (error) { 131 kill_vmcmds(&epp->ep_vmcmds); 132 epp->ep_flags &= ~EXEC_32; 133 } else 134 epp->ep_flags &= ~EXEC_TOPDOWN_VM; 135 return error; 136 } 137 138 /* 139 * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's 140 * exec package 141 * 142 * First, set of the various offsets/lengths in the exec package. 143 * 144 * Then, mark the text image busy (so it can be demand paged) or error 145 * out if this is not possible. Finally, set up vmcmds for the 146 * text, data, bss, and stack segments. 147 */ 148 149 int 150 netbsd32_exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp) 151 { 152 struct netbsd32_exec *execp = epp->ep_hdr; 153 int error; 154 155 epp->ep_taddr = AOUT_LDPGSZ; 156 epp->ep_tsize = execp->a_text; 157 epp->ep_daddr = epp->ep_taddr + execp->a_text; 158 epp->ep_dsize = execp->a_data + execp->a_bss; 159 epp->ep_entry = execp->a_entry; 160 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 161 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 162 163 error = vn_marktext(epp->ep_vp); 164 if (error) 165 return (error); 166 167 /* set up command for text segment */ 168 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 169 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE); 170 171 /* set up command for data segment */ 172 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 173 epp->ep_daddr, epp->ep_vp, execp->a_text, 174 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 175 176 /* set up command for bss segment */ 177 if (execp->a_bss > 0) 178 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 179 epp->ep_daddr + execp->a_data, NULLVP, 0, 180 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 181 182 return (*epp->ep_esch->es_setup_stack)(l, epp); 183 } 184 185 /* 186 * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's 187 * exec package 188 */ 189 190 int 191 netbsd32_exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp) 192 { 193 struct netbsd32_exec *execp = epp->ep_hdr; 194 long bsize, baddr; 195 196 epp->ep_taddr = AOUT_LDPGSZ; 197 epp->ep_tsize = execp->a_text; 198 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ); 199 epp->ep_dsize = execp->a_data + execp->a_bss; 200 epp->ep_entry = execp->a_entry; 201 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 202 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 203 204 /* set up command for text segment */ 205 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 206 epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec), 207 VM_PROT_READ|VM_PROT_EXECUTE); 208 209 /* set up command for data segment */ 210 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 211 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec), 212 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 213 214 /* set up command for bss segment */ 215 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 216 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 217 if (bsize > 0) 218 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 219 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 220 221 return (*epp->ep_esch->es_setup_stack)(l, epp); 222 } 223 224 /* 225 * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's 226 * exec package 227 */ 228 229 int 230 netbsd32_exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp) 231 { 232 struct netbsd32_exec *execp = epp->ep_hdr; 233 long dsize, bsize, baddr; 234 235 epp->ep_taddr = AOUT_LDPGSZ; 236 epp->ep_tsize = execp->a_text; 237 epp->ep_daddr = epp->ep_taddr + execp->a_text; 238 epp->ep_dsize = execp->a_data + execp->a_bss; 239 epp->ep_entry = execp->a_entry; 240 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 241 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32; 242 243 /* set up command for text and data segments */ 244 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 245 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 246 sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 247 248 /* set up command for bss segment */ 249 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 250 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 251 if (bsize > 0) 252 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 253 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 254 255 /* 256 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 257 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 258 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 259 * respectively to page boundaries. 260 * Compensate `ep_dsize' for the amount of data covered by the last 261 * text page. 262 */ 263 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, 264 PAGE_SIZE); 265 epp->ep_dsize = (dsize > 0) ? dsize : 0; 266 return (*epp->ep_esch->es_setup_stack)(l, epp); 267 } 268