1 /* $NetBSD: exec_aout.c,v 1.25 2002/10/05 22:34:05 chs 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.25 2002/10/05 22:34:05 chs 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 int error; 110 111 epp->ep_taddr = USRTEXT; 112 epp->ep_tsize = execp->a_text; 113 epp->ep_daddr = epp->ep_taddr + execp->a_text; 114 epp->ep_dsize = execp->a_data + execp->a_bss; 115 epp->ep_entry = execp->a_entry; 116 117 error = vn_marktext(epp->ep_vp); 118 if (error) 119 return (error); 120 121 /* set up command for text segment */ 122 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text), 123 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE); 124 125 /* set up command for data segment */ 126 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data), 127 epp->ep_daddr, epp->ep_vp, execp->a_text, 128 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 129 130 /* set up command for bss segment */ 131 if (execp->a_bss > 0) 132 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 133 epp->ep_daddr + execp->a_data, NULLVP, 0, 134 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 135 136 return exec_aout_setup_stack(p, epp); 137 } 138 139 /* 140 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package 141 */ 142 143 int 144 exec_aout_prep_nmagic(struct proc *p, struct exec_package *epp) 145 { 146 struct exec *execp = epp->ep_hdr; 147 long bsize, baddr; 148 149 epp->ep_taddr = USRTEXT; 150 epp->ep_tsize = execp->a_text; 151 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ); 152 epp->ep_dsize = execp->a_data + execp->a_bss; 153 epp->ep_entry = execp->a_entry; 154 155 /* set up command for text segment */ 156 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 157 epp->ep_taddr, epp->ep_vp, sizeof(struct exec), 158 VM_PROT_READ|VM_PROT_EXECUTE); 159 160 /* set up command for data segment */ 161 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 162 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec), 163 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 164 165 /* set up command for bss segment */ 166 baddr = round_page(epp->ep_daddr + execp->a_data); 167 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 168 if (bsize > 0) 169 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 170 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 171 172 return exec_aout_setup_stack(p, epp); 173 } 174 175 /* 176 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package 177 */ 178 179 int 180 exec_aout_prep_omagic(struct proc *p, struct exec_package *epp) 181 { 182 struct exec *execp = epp->ep_hdr; 183 long dsize, bsize, baddr; 184 185 epp->ep_taddr = USRTEXT; 186 epp->ep_tsize = execp->a_text; 187 epp->ep_daddr = epp->ep_taddr + execp->a_text; 188 epp->ep_dsize = execp->a_data + execp->a_bss; 189 epp->ep_entry = execp->a_entry; 190 191 /* set up command for text and data segments */ 192 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 193 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 194 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 195 196 /* set up command for bss segment */ 197 baddr = round_page(epp->ep_daddr + execp->a_data); 198 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 199 if (bsize > 0) 200 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 201 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 202 203 /* 204 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 205 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 206 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 207 * respectively to page boundaries. 208 * Compensate `ep_dsize' for the amount of data covered by the last 209 * text page. 210 */ 211 dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text); 212 epp->ep_dsize = (dsize > 0) ? dsize : 0; 213 return exec_aout_setup_stack(p, epp); 214 } 215 216 /* 217 * exec_aout_setup_stack(): Set up the stack segment for an a.out 218 * executable. 219 * 220 * Note that the ep_ssize parameter must be set to be the current stack 221 * limit; this is adjusted in the body of execve() to yield the 222 * appropriate stack segment usage once the argument length is 223 * calculated. 224 * 225 * This function returns an int for uniformity with other (future) formats' 226 * stack setup functions. They might have errors to return. 227 */ 228 229 int 230 exec_aout_setup_stack(struct proc *p, struct exec_package *epp) 231 { 232 233 epp->ep_maxsaddr = USRSTACK - MAXSSIZ; 234 epp->ep_minsaddr = USRSTACK; 235 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur; 236 237 /* 238 * set up commands for stack. note that this takes *two*, one to 239 * map the part of the stack which we can access, and one to map 240 * the part which we can't. 241 * 242 * arguably, it could be made into one, but that would require the 243 * addition of another mapping proc, which is unnecessary 244 * 245 * note that in memory, things assumed to be: 0 ... ep_maxsaddr 246 * <stack> ep_minsaddr 247 */ 248 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 249 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr), 250 epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE); 251 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize, 252 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0, 253 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 254 255 return 0; 256 } 257