1 /* $OpenBSD: uvm_unix.c,v 1.52 2014/11/17 03:15:58 deraadt Exp $ */ 2 /* $NetBSD: uvm_unix.c,v 1.18 2000/09/13 15:00:25 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1991, 1993 The Regents of the University of California. 7 * Copyright (c) 1988 University of Utah. 8 * 9 * All rights reserved. 10 * 11 * This code is derived from software contributed to Berkeley by 12 * the Systems Programming Group of the University of Utah Computer 13 * Science Department. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 3. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$ 40 * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93 41 * from: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp 42 */ 43 44 /* 45 * uvm_unix.c: traditional sbrk/grow interface to vm. 46 */ 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/proc.h> 51 #include <sys/resourcevar.h> 52 #include <sys/vnode.h> 53 #include <sys/core.h> 54 55 #include <sys/mount.h> 56 #include <sys/syscallargs.h> 57 58 #include <uvm/uvm.h> 59 60 /* 61 * sys_obreak: set break 62 */ 63 64 int 65 sys_obreak(struct proc *p, void *v, register_t *retval) 66 { 67 struct sys_obreak_args /* { 68 syscallarg(char *) nsize; 69 } */ *uap = v; 70 struct vmspace *vm = p->p_vmspace; 71 vaddr_t new, old, base; 72 int error; 73 74 base = (vaddr_t)vm->vm_daddr; 75 new = round_page((vaddr_t)SCARG(uap, nsize)); 76 if (new < base || (new - base) > p->p_rlimit[RLIMIT_DATA].rlim_cur) 77 return (ENOMEM); 78 79 old = round_page(base + ptoa(vm->vm_dsize)); 80 81 if (new == old) 82 return (0); 83 84 /* grow or shrink? */ 85 if (new > old) { 86 error = uvm_map(&vm->vm_map, &old, new - old, NULL, 87 UVM_UNKNOWN_OFFSET, 0, 88 UVM_MAPFLAG(PROT_READ | PROT_WRITE, 89 PROT_READ | PROT_WRITE | PROT_EXEC, UVM_INH_COPY, 90 POSIX_MADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED| 91 UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)); 92 if (error) { 93 uprintf("sbrk: grow %ld failed, error = %d\n", 94 new - old, error); 95 return (ENOMEM); 96 } 97 vm->vm_dsize += atop(new - old); 98 } else { 99 uvm_deallocate(&vm->vm_map, new, old - new); 100 vm->vm_dsize -= atop(old - new); 101 } 102 103 return (0); 104 } 105 106 /* 107 * uvm_grow: enlarge the "stack segment" to include sp. 108 */ 109 void 110 uvm_grow(struct proc *p, vaddr_t sp) 111 { 112 struct vmspace *vm = p->p_vmspace; 113 int si; 114 115 /* For user defined stacks (from sendsig). */ 116 if (sp < (vaddr_t)vm->vm_maxsaddr) 117 return; 118 119 /* For common case of already allocated (from trap). */ 120 #ifdef MACHINE_STACK_GROWS_UP 121 if (sp < USRSTACK + ptoa(vm->vm_ssize)) 122 #else 123 if (sp >= USRSTACK - ptoa(vm->vm_ssize)) 124 #endif 125 return; 126 127 /* Really need to check vs limit and increment stack size if ok. */ 128 #ifdef MACHINE_STACK_GROWS_UP 129 si = atop(sp - USRSTACK) - vm->vm_ssize + 1; 130 #else 131 si = atop(USRSTACK - sp) - vm->vm_ssize; 132 #endif 133 if (vm->vm_ssize + si <= atop(p->p_rlimit[RLIMIT_STACK].rlim_cur)) 134 vm->vm_ssize += si; 135 } 136 137 #ifndef SMALL_KERNEL 138 139 /* 140 * uvm_coredump: dump core! 141 */ 142 143 int 144 uvm_coredump(struct proc *p, struct vnode *vp, struct ucred *cred, 145 struct core *chdr) 146 { 147 struct vmspace *vm = p->p_vmspace; 148 vm_map_t map = &vm->vm_map; 149 vm_map_entry_t entry, safe; 150 vaddr_t start, end, top; 151 struct coreseg cseg; 152 off_t offset, coffset; 153 int csize, chunk, flag, error = 0; 154 155 offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize; 156 157 RB_FOREACH_SAFE(entry, uvm_map_addr, &map->addr, safe) { 158 /* should never happen for a user process */ 159 if (UVM_ET_ISSUBMAP(entry)) { 160 panic("uvm_coredump: user process with submap?"); 161 } 162 163 if (!(entry->protection & PROT_WRITE) && 164 entry->start != p->p_p->ps_sigcode) 165 continue; 166 167 /* Don't dump mmaped devices. */ 168 if (entry->object.uvm_obj != NULL && 169 UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) 170 continue; 171 172 start = entry->start; 173 end = entry->end; 174 175 if (start >= VM_MAXUSER_ADDRESS) 176 continue; 177 178 if (end > VM_MAXUSER_ADDRESS) 179 end = VM_MAXUSER_ADDRESS; 180 181 #ifdef MACHINE_STACK_GROWS_UP 182 if (USRSTACK <= start && start < (USRSTACK + MAXSSIZ)) { 183 top = round_page(USRSTACK + ptoa(vm->vm_ssize)); 184 if (end > top) 185 end = top; 186 187 if (start >= end) 188 continue; 189 #else 190 if (start >= (vaddr_t)vm->vm_maxsaddr) { 191 top = trunc_page(USRSTACK - ptoa(vm->vm_ssize)); 192 if (start < top) 193 start = top; 194 195 if (start >= end) 196 continue; 197 #endif 198 flag = CORE_STACK; 199 } else 200 flag = CORE_DATA; 201 202 /* Set up a new core file segment. */ 203 CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag); 204 cseg.c_addr = start; 205 cseg.c_size = end - start; 206 207 error = vn_rdwr(UIO_WRITE, vp, 208 (caddr_t)&cseg, chdr->c_seghdrsize, 209 offset, UIO_SYSSPACE, IO_UNIT, cred, NULL, p); 210 /* 211 * We might get an EFAULT on objects mapped beyond 212 * EOF. Ignore the error. 213 */ 214 if (error && error != EFAULT) 215 break; 216 217 offset += chdr->c_seghdrsize; 218 219 coffset = 0; 220 csize = (int)cseg.c_size; 221 do { 222 if (p->p_siglist & sigmask(SIGKILL)) 223 return (EINTR); 224 225 /* Rest of the loop sleeps with lock held, so... */ 226 yield(); 227 228 chunk = MIN(csize, MAXPHYS); 229 error = vn_rdwr(UIO_WRITE, vp, 230 (caddr_t)(u_long)cseg.c_addr + coffset, 231 chunk, offset + coffset, UIO_USERSPACE, 232 IO_UNIT, cred, NULL, p); 233 if (error) 234 return (error); 235 236 coffset += chunk; 237 csize -= chunk; 238 } while (csize > 0); 239 offset += cseg.c_size; 240 241 /* Discard the memory */ 242 uvm_unmap(map, cseg.c_addr, cseg.c_addr + cseg.c_size); 243 244 chdr->c_nseg++; 245 } 246 247 return (error); 248 } 249 250 int 251 uvm_coredump_walkmap(struct proc *p, void *iocookie, 252 int (*func)(struct proc *, void *, struct uvm_coredump_state *), 253 void *cookie) 254 { 255 struct uvm_coredump_state state; 256 struct vmspace *vm = p->p_vmspace; 257 struct vm_map *map = &vm->vm_map; 258 struct vm_map_entry *entry; 259 vaddr_t top; 260 int error; 261 262 RB_FOREACH(entry, uvm_map_addr, &map->addr) { 263 state.cookie = cookie; 264 state.prot = entry->protection; 265 state.flags = 0; 266 267 /* should never happen for a user process */ 268 if (UVM_ET_ISSUBMAP(entry)) { 269 panic("uvm_coredump: user process with submap?"); 270 } 271 272 if (!(entry->protection & PROT_WRITE) && 273 entry->start != p->p_p->ps_sigcode) 274 continue; 275 276 /* Don't dump mmaped devices. */ 277 if (entry->object.uvm_obj != NULL && 278 UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) 279 continue; 280 281 state.start = entry->start; 282 state.realend = entry->end; 283 state.end = entry->end; 284 285 if (state.start >= VM_MAXUSER_ADDRESS) 286 continue; 287 288 if (state.end > VM_MAXUSER_ADDRESS) 289 state.end = VM_MAXUSER_ADDRESS; 290 291 #ifdef MACHINE_STACK_GROWS_UP 292 if (USRSTACK <= state.start && 293 state.start < (USRSTACK + MAXSSIZ)) { 294 top = round_page(USRSTACK + ptoa(vm->vm_ssize)); 295 if (state.end > top) 296 state.end = top; 297 298 if (state.start >= state.end) 299 continue; 300 #else 301 if (state.start >= (vaddr_t)vm->vm_maxsaddr) { 302 top = trunc_page(USRSTACK - ptoa(vm->vm_ssize)); 303 if (state.start < top) 304 state.start = top; 305 306 if (state.start >= state.end) 307 continue; 308 #endif 309 state.flags |= UVM_COREDUMP_STACK; 310 } 311 312 error = (*func)(p, iocookie, &state); 313 if (error) 314 return (error); 315 } 316 317 return (0); 318 } 319 320 #endif /* !SMALL_KERNEL */ 321