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