1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$ 41 * 42 * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93 43 * $FreeBSD: src/sys/vm/vm_unix.c,v 1.24.2.2 2002/07/02 20:06:19 dillon Exp $ 44 * $DragonFly: src/sys/vm/vm_unix.c,v 1.7 2006/11/07 17:51:24 dillon Exp $ 45 */ 46 47 /* 48 * Traditional sbrk/grow interface to VM 49 */ 50 #include <sys/param.h> 51 #include <sys/sysproto.h> 52 #include <sys/proc.h> 53 #include <sys/resourcevar.h> 54 55 #include <vm/vm.h> 56 #include <vm/vm_param.h> 57 #include <sys/lock.h> 58 #include <vm/pmap.h> 59 #include <vm/vm_map.h> 60 61 #include <sys/thread.h> 62 63 /* 64 * sys_obreak backs the C library sbrk call 65 * 66 * obreak_args(char *nsize) 67 * 68 * No requirements. 69 */ 70 int 71 sys_obreak(struct obreak_args *uap) 72 { 73 struct proc *p = curproc; 74 struct vmspace *vm = p->p_vmspace; 75 vm_offset_t new, old, base; 76 int rv; 77 int error; 78 79 error = 0; 80 81 lwkt_gettoken(&vm->vm_map.token); 82 83 base = round_page((vm_offset_t)vm->vm_daddr); 84 new = round_page((vm_offset_t)uap->nsize); 85 old = base + ctob(vm->vm_dsize); 86 87 if (new > base) { 88 /* 89 * We check resource limits here, but alow processes to 90 * reduce their usage, even if they remain over the limit. 91 */ 92 if (new > old && 93 (new - base) > (vm_offset_t) p->p_rlimit[RLIMIT_DATA].rlim_cur) { 94 error = ENOMEM; 95 goto done; 96 } 97 if (new >= VM_MAX_USER_ADDRESS) { 98 error = ENOMEM; 99 goto done; 100 } 101 } else if (new < base) { 102 /* 103 * This is simply an invalid value. If someone wants to 104 * do fancy address space manipulations, mmap and munmap 105 * can do most of what the user would want. 106 */ 107 error = EINVAL; 108 goto done; 109 } 110 111 if (new > old) { 112 vm_size_t diff; 113 114 diff = new - old; 115 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur) { 116 error = ENOMEM; 117 goto done; 118 } 119 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, 120 diff, PAGE_SIZE, 121 FALSE, VM_MAPTYPE_NORMAL, 122 VM_PROT_ALL, VM_PROT_ALL, 123 0); 124 if (rv != KERN_SUCCESS) { 125 error = ENOMEM; 126 goto done; 127 } 128 if (vm->vm_map.flags & MAP_WIREFUTURE) 129 vm_map_wire(&vm->vm_map, old, new, FALSE); 130 131 vm->vm_dsize += btoc(diff); 132 } else if (new < old) { 133 rv = vm_map_remove(&vm->vm_map, new, old); 134 if (rv != KERN_SUCCESS) { 135 error = ENOMEM; 136 goto done; 137 } 138 vm->vm_dsize -= btoc(old - new); 139 } 140 done: 141 lwkt_reltoken(&vm->vm_map.token); 142 143 return (error); 144 } 145