1 /* $OpenBSD: uvm_unix.c,v 1.22 2002/10/29 18:30:21 art 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. All advertising materials mentioning features or use of this software 24 * must display the following acknowledgement: 25 * This product includes software developed by Charles D. Cranor, 26 * Washington University, the University of California, Berkeley and 27 * its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$ 45 * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93 46 * from: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp 47 */ 48 49 /* 50 * uvm_unix.c: traditional sbrk/grow interface to vm. 51 */ 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/proc.h> 56 #include <sys/resourcevar.h> 57 #include <sys/vnode.h> 58 #include <sys/core.h> 59 60 #include <sys/mount.h> 61 #include <sys/syscallargs.h> 62 63 #include <uvm/uvm.h> 64 65 /* 66 * sys_obreak: set break 67 */ 68 69 int 70 sys_obreak(p, v, retval) 71 struct proc *p; 72 void *v; 73 register_t *retval; 74 { 75 struct sys_obreak_args /* { 76 syscallarg(char *) nsize; 77 } */ *uap = v; 78 struct vmspace *vm = p->p_vmspace; 79 vaddr_t new, old; 80 int error; 81 82 old = (vaddr_t)vm->vm_daddr; 83 new = round_page((vaddr_t)SCARG(uap, nsize)); 84 if ((new - old) > p->p_rlimit[RLIMIT_DATA].rlim_cur) 85 return (ENOMEM); 86 87 old = round_page(old + ptoa(vm->vm_dsize)); 88 89 if (new == old) 90 return (0); 91 92 /* 93 * grow or shrink? 94 */ 95 if (new > old) { 96 error = uvm_map(&vm->vm_map, &old, new - old, NULL, 97 UVM_UNKNOWN_OFFSET, 0, 98 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RWX, UVM_INH_COPY, 99 UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED| 100 UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)); 101 if (error) { 102 uprintf("sbrk: grow %ld failed, error = %d\n", 103 new - old, error); 104 return (ENOMEM); 105 } 106 vm->vm_dsize += atop(new - old); 107 } else { 108 uvm_deallocate(&vm->vm_map, new, old - new); 109 vm->vm_dsize -= atop(old - new); 110 } 111 112 return (0); 113 } 114 115 /* 116 * uvm_grow: enlarge the "stack segment" to include sp. 117 */ 118 119 int 120 uvm_grow(p, sp) 121 struct proc *p; 122 vaddr_t sp; 123 { 124 struct vmspace *vm = p->p_vmspace; 125 int si; 126 127 /* 128 * For user defined stacks (from sendsig). 129 */ 130 #ifdef MACHINE_STACK_GROWS_UP 131 if (sp > (vaddr_t)vm->vm_maxsaddr) 132 #else 133 if (sp < (vaddr_t)vm->vm_maxsaddr) 134 #endif 135 return (0); 136 137 /* 138 * For common case of already allocated (from trap). 139 */ 140 #ifdef MACHINE_STACK_GROWS_UP 141 if (sp < USRSTACK + ctob(vm->vm_ssize)) 142 #else 143 if (sp >= USRSTACK - ctob(vm->vm_ssize)) 144 #endif 145 return (1); 146 147 /* 148 * Really need to check vs limit and increment stack size if ok. 149 */ 150 #ifdef MACHINE_STACK_GROWS_UP 151 si = btoc(sp - USRSTACK) - vm->vm_ssize; 152 #else 153 si = btoc(USRSTACK-sp) - vm->vm_ssize; 154 #endif 155 if (vm->vm_ssize + si > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur)) 156 return (0); 157 vm->vm_ssize += si; 158 return (1); 159 } 160 161 /* 162 * sys_oadvise: old advice system call 163 */ 164 165 /* ARGSUSED */ 166 int 167 sys_ovadvise(p, v, retval) 168 struct proc *p; 169 void *v; 170 register_t *retval; 171 { 172 #if 0 173 struct sys_ovadvise_args /* { 174 syscallarg(int) anom; 175 } */ *uap = v; 176 #endif 177 178 return (EINVAL); 179 } 180 181 /* 182 * uvm_coredump: dump core! 183 */ 184 185 int 186 uvm_coredump(p, vp, cred, chdr) 187 struct proc *p; 188 struct vnode *vp; 189 struct ucred *cred; 190 struct core *chdr; 191 { 192 struct vmspace *vm = p->p_vmspace; 193 vm_map_t map = &vm->vm_map; 194 vm_map_entry_t entry; 195 vaddr_t start, end, maxstack; 196 struct coreseg cseg; 197 off_t offset; 198 int flag, error = 0; 199 200 offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize; 201 maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize)); 202 203 for (entry = map->header.next; entry != &map->header; 204 entry = entry->next) { 205 206 /* should never happen for a user process */ 207 if (UVM_ET_ISSUBMAP(entry)) { 208 panic("uvm_coredump: user process with submap?"); 209 } 210 211 if (!(entry->protection & VM_PROT_WRITE)) 212 continue; 213 214 start = entry->start; 215 end = entry->end; 216 217 if (start >= VM_MAXUSER_ADDRESS) 218 continue; 219 220 if (end > VM_MAXUSER_ADDRESS) 221 end = VM_MAXUSER_ADDRESS; 222 223 #ifdef MACHINE_STACK_GROWS_UP 224 if (start >= USRSTACK) { 225 start = USRSTACK; 226 #else 227 if (start >= (vaddr_t)vm->vm_maxsaddr) { 228 start = trunc_page(USRSTACK - ctob(vm->vm_ssize)); 229 #endif 230 flag = CORE_STACK; 231 if (start >= end) 232 continue; 233 } else 234 flag = CORE_DATA; 235 236 /* 237 * Set up a new core file segment. 238 */ 239 CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag); 240 cseg.c_addr = start; 241 cseg.c_size = end - start; 242 243 error = vn_rdwr(UIO_WRITE, vp, 244 (caddr_t)&cseg, chdr->c_seghdrsize, 245 offset, UIO_SYSSPACE, 246 IO_NODELOCKED|IO_UNIT, cred, NULL, p); 247 if (error) 248 break; 249 250 offset += chdr->c_seghdrsize; 251 error = vn_rdwr(UIO_WRITE, vp, 252 (caddr_t)(u_long)cseg.c_addr, (int)cseg.c_size, 253 offset, UIO_USERSPACE, 254 IO_NODELOCKED|IO_UNIT, cred, NULL, p); 255 if (error) 256 break; 257 258 offset += cseg.c_size; 259 chdr->c_nseg++; 260 } 261 262 return (error); 263 } 264 265