1 /* $OpenBSD: exec_subr.c,v 1.12 2001/06/27 04:49:40 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 <vm/vm.h> 45 46 #include <uvm/uvm.h> 47 48 #ifdef DEBUG 49 /* 50 * new_vmcmd(): 51 * create a new vmcmd structure and fill in its fields based 52 * on function call arguments. make sure objects ref'd by 53 * the vmcmd are 'held'. 54 * 55 * If not debugging, this is a macro, so it's expanded inline. 56 */ 57 58 void 59 new_vmcmd(evsp, proc, len, addr, vp, offset, prot) 60 struct exec_vmcmd_set *evsp; 61 int (*proc) __P((struct proc * p, struct exec_vmcmd *)); 62 u_long len; 63 u_long addr; 64 struct vnode *vp; 65 u_long offset; 66 u_int prot; 67 { 68 struct exec_vmcmd *vcp; 69 70 if (evsp->evs_used >= evsp->evs_cnt) 71 vmcmdset_extend(evsp); 72 vcp = &evsp->evs_cmds[evsp->evs_used++]; 73 vcp->ev_proc = proc; 74 vcp->ev_len = len; 75 vcp->ev_addr = addr; 76 if ((vcp->ev_vp = vp) != NULL) 77 vref(vp); 78 vcp->ev_offset = offset; 79 vcp->ev_prot = prot; 80 } 81 #endif /* DEBUG */ 82 83 void 84 vmcmdset_extend(evsp) 85 struct exec_vmcmd_set *evsp; 86 { 87 struct exec_vmcmd *nvcp; 88 u_int ocnt; 89 90 #ifdef DIAGNOSTIC 91 if (evsp->evs_used < evsp->evs_cnt) 92 panic("vmcmdset_extend: not necessary"); 93 #endif 94 95 ocnt = evsp->evs_cnt; 96 KASSERT(ocnt > 0); 97 /* figure out number of entries in new set */ 98 evsp->evs_cnt += ocnt; 99 100 /* reallocate the command set */ 101 nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd), M_EXEC, 102 M_WAITOK); 103 bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd))); 104 if (evsp->evs_cmds != evsp->evs_start) 105 free(evsp->evs_cmds, M_EXEC); 106 evsp->evs_cmds = nvcp; 107 } 108 109 void 110 kill_vmcmds(evsp) 111 struct exec_vmcmd_set *evsp; 112 { 113 struct exec_vmcmd *vcp; 114 int i; 115 116 for (i = 0; i < evsp->evs_used; i++) { 117 vcp = &evsp->evs_cmds[i]; 118 if (vcp->ev_vp != NULLVP) 119 vrele(vcp->ev_vp); 120 } 121 122 /* 123 * Free old vmcmds and restet the array. 124 */ 125 evsp->evs_used = 0; 126 if (evsp->evs_cmds != evsp->evs_start) 127 free(evsp->evs_cmds, M_EXEC); 128 evsp->evs_cmds = evsp->evs_start; 129 evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE; 130 } 131 132 /* 133 * vmcmd_map_pagedvn(): 134 * handle vmcmd which specifies that a vnode should be mmap'd. 135 * appropriate for handling demand-paged text and data segments. 136 */ 137 138 int 139 vmcmd_map_pagedvn(p, cmd) 140 struct proc *p; 141 struct exec_vmcmd *cmd; 142 { 143 /* 144 * note that if you're going to map part of an process as being 145 * paged from a vnode, that vnode had damn well better be marked as 146 * VTEXT. that's handled in the routine which sets up the vmcmd to 147 * call this routine. 148 */ 149 struct uvm_object *uobj; 150 int retval; 151 152 /* 153 * map the vnode in using uvm_map. 154 */ 155 156 if (cmd->ev_len == 0) 157 return(0); 158 if (cmd->ev_offset & PAGE_MASK) 159 return(EINVAL); 160 if (cmd->ev_addr & PAGE_MASK) 161 return(EINVAL); 162 if (cmd->ev_len & PAGE_MASK) 163 return(EINVAL); 164 165 /* 166 * first, attach to the object 167 */ 168 169 uobj = uvn_attach((void *) cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE); 170 if (uobj == NULL) 171 return(ENOMEM); 172 173 /* 174 * do the map 175 */ 176 177 retval = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len, 178 uobj, cmd->ev_offset, 179 UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY, 180 UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED)); 181 182 /* 183 * check for error 184 */ 185 186 if (retval == KERN_SUCCESS) 187 return(0); 188 189 /* 190 * error: detach from object 191 */ 192 193 uobj->pgops->pgo_detach(uobj); 194 return(EINVAL); 195 } 196 197 /* 198 * vmcmd_map_readvn(): 199 * handle vmcmd which specifies that a vnode should be read from. 200 * appropriate for non-demand-paged text/data segments, i.e. impure 201 * objects (a la OMAGIC and NMAGIC). 202 */ 203 int 204 vmcmd_map_readvn(p, cmd) 205 struct proc *p; 206 struct exec_vmcmd *cmd; 207 { 208 int error; 209 210 if (cmd->ev_len == 0) 211 return(KERN_SUCCESS); /* XXXCDC: should it happen? */ 212 213 cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */ 214 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, 215 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 216 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 217 UVM_ADV_NORMAL, 218 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)); 219 220 if (error) 221 return error; 222 223 error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr, 224 cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT|IO_NODELOCKED, 225 p->p_ucred, NULL, p); 226 if (error) 227 return error; 228 229 if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) { 230 /* 231 * we had to map in the area at PROT_ALL so that vn_rdwr() 232 * could write to it. however, the caller seems to want 233 * it mapped read-only, so now we are going to have to call 234 * uvm_map_protect() to fix up the protection. ICK. 235 */ 236 return(uvm_map_protect(&p->p_vmspace->vm_map, 237 trunc_page(cmd->ev_addr), 238 round_page(cmd->ev_addr + cmd->ev_len), 239 cmd->ev_prot, FALSE)); 240 } else { 241 return(KERN_SUCCESS); 242 } 243 } 244 245 /* 246 * vmcmd_map_zero(): 247 * handle vmcmd which specifies a zero-filled address space region. The 248 * address range must be first allocated, then protected appropriately. 249 */ 250 251 int 252 vmcmd_map_zero(p, cmd) 253 struct proc *p; 254 struct exec_vmcmd *cmd; 255 { 256 int error; 257 258 if (cmd->ev_len == 0) 259 return(KERN_SUCCESS); /* XXXCDC: should it happen? */ 260 261 cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */ 262 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, 263 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 264 UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY, 265 UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_COPYONW)); 266 267 if (error) 268 return error; 269 270 return(KERN_SUCCESS); 271 } 272 273 /* 274 * exec_setup_stack(): Set up the stack segment for an a.out 275 * executable. 276 * 277 * Note that the ep_ssize parameter must be set to be the current stack 278 * limit; this is adjusted in the body of execve() to yield the 279 * appropriate stack segment usage once the argument length is 280 * calculated. 281 * 282 * This function returns an int for uniformity with other (future) formats' 283 * stack setup functions. They might have errors to return. 284 */ 285 286 int 287 exec_setup_stack(p, epp) 288 struct proc *p; 289 struct exec_package *epp; 290 { 291 292 #ifdef MACHINE_STACK_GROWS_UP 293 epp->ep_maxsaddr = USRSTACK + MAXSSIZ; 294 #else 295 epp->ep_maxsaddr = USRSTACK - MAXSSIZ; 296 #endif 297 epp->ep_minsaddr = USRSTACK; 298 epp->ep_ssize = round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur); 299 300 /* 301 * set up commands for stack. note that this takes *two*, one to 302 * map the part of the stack which we can access, and one to map 303 * the part which we can't. 304 * 305 * arguably, it could be made into one, but that would require the 306 * addition of another mapping proc, which is unnecessary 307 * 308 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr 309 * <stack> ep_minsaddr 310 */ 311 #ifdef MACHINE_STACK_GROWS_UP 312 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 313 (epp->ep_maxsaddr - (epp->ep_minsaddr + epp->ep_ssize)), 314 epp->ep_minsaddr + epp->ep_ssize, NULLVP, 0, VM_PROT_NONE); 315 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize, 316 epp->ep_minsaddr, NULLVP, 0, 317 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 318 #else 319 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 320 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr), 321 epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE); 322 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize, 323 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0, 324 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 325 #endif 326 327 return 0; 328 } 329