1 /* $OpenBSD: exec_subr.c,v 1.17 2001/12/19 08:58:06 art Exp $ */ 2 /* $NetBSD: exec_subr.c,v 1.9 1994/12/04 03:10:42 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1993, 1994 Christopher G. Demetriou 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Christopher G. Demetriou. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/proc.h> 37 #include <sys/malloc.h> 38 #include <sys/vnode.h> 39 #include <sys/filedesc.h> 40 #include <sys/exec.h> 41 #include <sys/mman.h> 42 #include <sys/resourcevar.h> 43 44 #include <uvm/uvm.h> 45 46 #ifdef DEBUG 47 /* 48 * new_vmcmd(): 49 * create a new vmcmd structure and fill in its fields based 50 * on function call arguments. make sure objects ref'd by 51 * the vmcmd are 'held'. 52 * 53 * If not debugging, this is a macro, so it's expanded inline. 54 */ 55 56 void 57 new_vmcmd(evsp, proc, len, addr, vp, offset, prot) 58 struct exec_vmcmd_set *evsp; 59 int (*proc) __P((struct proc * p, struct exec_vmcmd *)); 60 u_long len; 61 u_long addr; 62 struct vnode *vp; 63 u_long offset; 64 u_int prot; 65 { 66 struct exec_vmcmd *vcp; 67 68 if (evsp->evs_used >= evsp->evs_cnt) 69 vmcmdset_extend(evsp); 70 vcp = &evsp->evs_cmds[evsp->evs_used++]; 71 vcp->ev_proc = proc; 72 vcp->ev_len = len; 73 vcp->ev_addr = addr; 74 if ((vcp->ev_vp = vp) != NULL) 75 vref(vp); 76 vcp->ev_offset = offset; 77 vcp->ev_prot = prot; 78 } 79 #endif /* DEBUG */ 80 81 void 82 vmcmdset_extend(evsp) 83 struct exec_vmcmd_set *evsp; 84 { 85 struct exec_vmcmd *nvcp; 86 u_int ocnt; 87 88 #ifdef DIAGNOSTIC 89 if (evsp->evs_used < evsp->evs_cnt) 90 panic("vmcmdset_extend: not necessary"); 91 #endif 92 93 ocnt = evsp->evs_cnt; 94 KASSERT(ocnt > 0); 95 /* figure out number of entries in new set */ 96 evsp->evs_cnt += ocnt; 97 98 /* reallocate the command set */ 99 nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd), M_EXEC, 100 M_WAITOK); 101 bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd))); 102 if (evsp->evs_cmds != evsp->evs_start) 103 free(evsp->evs_cmds, M_EXEC); 104 evsp->evs_cmds = nvcp; 105 } 106 107 void 108 kill_vmcmds(evsp) 109 struct exec_vmcmd_set *evsp; 110 { 111 struct exec_vmcmd *vcp; 112 int i; 113 114 for (i = 0; i < evsp->evs_used; i++) { 115 vcp = &evsp->evs_cmds[i]; 116 if (vcp->ev_vp != NULLVP) 117 vrele(vcp->ev_vp); 118 } 119 120 /* 121 * Free old vmcmds and restet the array. 122 */ 123 evsp->evs_used = 0; 124 if (evsp->evs_cmds != evsp->evs_start) 125 free(evsp->evs_cmds, M_EXEC); 126 evsp->evs_cmds = evsp->evs_start; 127 evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE; 128 } 129 130 /* 131 * vmcmd_map_pagedvn(): 132 * handle vmcmd which specifies that a vnode should be mmap'd. 133 * appropriate for handling demand-paged text and data segments. 134 */ 135 136 int 137 vmcmd_map_pagedvn(p, cmd) 138 struct proc *p; 139 struct exec_vmcmd *cmd; 140 { 141 /* 142 * note that if you're going to map part of an process as being 143 * paged from a vnode, that vnode had damn well better be marked as 144 * VTEXT. that's handled in the routine which sets up the vmcmd to 145 * call this routine. 146 */ 147 struct uvm_object *uobj; 148 int retval; 149 150 /* 151 * map the vnode in using uvm_map. 152 */ 153 154 if (cmd->ev_len == 0) 155 return(0); 156 if (cmd->ev_offset & PAGE_MASK) 157 return(EINVAL); 158 if (cmd->ev_addr & PAGE_MASK) 159 return(EINVAL); 160 if (cmd->ev_len & PAGE_MASK) 161 return(EINVAL); 162 163 /* 164 * first, attach to the object 165 */ 166 167 uobj = uvn_attach((void *) cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE); 168 if (uobj == NULL) 169 return(ENOMEM); 170 171 /* 172 * do the map 173 */ 174 175 retval = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len, 176 uobj, cmd->ev_offset, 0, 177 UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY, 178 UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED)); 179 180 /* 181 * check for error 182 */ 183 184 if (retval == KERN_SUCCESS) 185 return(0); 186 187 /* 188 * error: detach from object 189 */ 190 191 uobj->pgops->pgo_detach(uobj); 192 return(EINVAL); 193 } 194 195 /* 196 * vmcmd_map_readvn(): 197 * handle vmcmd which specifies that a vnode should be read from. 198 * appropriate for non-demand-paged text/data segments, i.e. impure 199 * objects (a la OMAGIC and NMAGIC). 200 */ 201 int 202 vmcmd_map_readvn(p, cmd) 203 struct proc *p; 204 struct exec_vmcmd *cmd; 205 { 206 int error; 207 208 if (cmd->ev_len == 0) 209 return (0); 210 211 cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */ 212 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, 213 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0, 214 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 215 UVM_ADV_NORMAL, 216 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)); 217 218 if (error) 219 return (error); 220 221 error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr, 222 cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT|IO_NODELOCKED, 223 p->p_ucred, NULL, p); 224 if (error) 225 return (error); 226 227 if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) { 228 /* 229 * we had to map in the area at PROT_ALL so that vn_rdwr() 230 * could write to it. however, the caller seems to want 231 * it mapped read-only, so now we are going to have to call 232 * uvm_map_protect() to fix up the protection. ICK. 233 */ 234 return (uvm_map_protect(&p->p_vmspace->vm_map, 235 trunc_page(cmd->ev_addr), 236 round_page(cmd->ev_addr + cmd->ev_len), 237 cmd->ev_prot, FALSE)); 238 } 239 return (0); 240 } 241 242 /* 243 * vmcmd_map_zero(): 244 * handle vmcmd which specifies a zero-filled address space region. The 245 * address range must be first allocated, then protected appropriately. 246 */ 247 248 int 249 vmcmd_map_zero(p, cmd) 250 struct proc *p; 251 struct exec_vmcmd *cmd; 252 { 253 int error; 254 255 if (cmd->ev_len == 0) 256 return (0); 257 258 cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */ 259 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, 260 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0, 261 UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY, 262 UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_COPYONW)); 263 264 if (error) 265 return error; 266 267 return (0); 268 } 269 270 /* 271 * exec_setup_stack(): Set up the stack segment for an a.out 272 * executable. 273 * 274 * Note that the ep_ssize parameter must be set to be the current stack 275 * limit; this is adjusted in the body of execve() to yield the 276 * appropriate stack segment usage once the argument length is 277 * calculated. 278 * 279 * This function returns an int for uniformity with other (future) formats' 280 * stack setup functions. They might have errors to return. 281 */ 282 283 int 284 exec_setup_stack(p, epp) 285 struct proc *p; 286 struct exec_package *epp; 287 { 288 289 #ifdef MACHINE_STACK_GROWS_UP 290 epp->ep_maxsaddr = USRSTACK + MAXSSIZ; 291 #else 292 epp->ep_maxsaddr = USRSTACK - MAXSSIZ; 293 #endif 294 epp->ep_minsaddr = USRSTACK; 295 epp->ep_ssize = round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur); 296 297 /* 298 * set up commands for stack. note that this takes *two*, one to 299 * map the part of the stack which we can access, and one to map 300 * the part which we can't. 301 * 302 * arguably, it could be made into one, but that would require the 303 * addition of another mapping proc, which is unnecessary 304 * 305 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr 306 * <stack> ep_minsaddr 307 */ 308 #ifdef MACHINE_STACK_GROWS_UP 309 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 310 (epp->ep_maxsaddr - (epp->ep_minsaddr + epp->ep_ssize)), 311 epp->ep_minsaddr + epp->ep_ssize, NULLVP, 0, VM_PROT_NONE); 312 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize, 313 epp->ep_minsaddr, NULLVP, 0, 314 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 315 #else 316 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 317 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr), 318 epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE); 319 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize, 320 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0, 321 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 322 #endif 323 324 return 0; 325 } 326