1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1988 University of Utah. 3df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 4df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 7df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 8df8bae1dSRodney W. Grimes * Science Department. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 195929bcfaSPhilippe Charnier * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$ 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93 41df8bae1dSRodney W. Grimes */ 42df8bae1dSRodney W. Grimes 43df8bae1dSRodney W. Grimes /* 44df8bae1dSRodney W. Grimes * Traditional sbrk/grow interface to VM 45df8bae1dSRodney W. Grimes */ 46190609ddSJohn Baldwin 47874651b1SDavid E. O'Brien #include <sys/cdefs.h> 48874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 49874651b1SDavid E. O'Brien 50df8bae1dSRodney W. Grimes #include <sys/param.h> 51fb919e4dSMark Murray #include <sys/lock.h> 5286e92ee7SJohn Baldwin #include <sys/mutex.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 54df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 5586e92ee7SJohn Baldwin #include <sys/sysproto.h> 5623955314SAlfred Perlstein #include <sys/systm.h> 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes #include <vm/vm.h> 59efeaf95aSDavid Greenman #include <vm/vm_param.h> 60efeaf95aSDavid Greenman #include <vm/pmap.h> 61efeaf95aSDavid Greenman #include <vm/vm_map.h> 6226f9a767SRodney W. Grimes 63d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 64df8bae1dSRodney W. Grimes struct obreak_args { 659ed1bde7SBruce Evans char *nsize; 66df8bae1dSRodney W. Grimes }; 67d2d3e875SBruce Evans #endif 6826f9a767SRodney W. Grimes 696a33d53cSMatthew Dillon /* 706a33d53cSMatthew Dillon * MPSAFE 716a33d53cSMatthew Dillon */ 72df8bae1dSRodney W. Grimes /* ARGSUSED */ 73df8bae1dSRodney W. Grimes int 74b40ce416SJulian Elischer obreak(td, uap) 75b40ce416SJulian Elischer struct thread *td; 76df8bae1dSRodney W. Grimes struct obreak_args *uap; 77df8bae1dSRodney W. Grimes { 78b40ce416SJulian Elischer struct vmspace *vm = td->td_proc->p_vmspace; 79f0e2953eSJohn Dyson vm_offset_t new, old, base; 8091d5354aSJohn Baldwin rlim_t datalim, vmemlim; 81df8bae1dSRodney W. Grimes int rv; 820cddd8f0SMatthew Dillon int error = 0; 83abd498aaSBruce M Simpson boolean_t do_map_wirefuture; 840cddd8f0SMatthew Dillon 8591d5354aSJohn Baldwin PROC_LOCK(td->td_proc); 8691d5354aSJohn Baldwin datalim = lim_cur(td->td_proc, RLIMIT_DATA); 8791d5354aSJohn Baldwin vmemlim = lim_cur(td->td_proc, RLIMIT_VMEM); 8891d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 8991d5354aSJohn Baldwin 90abd498aaSBruce M Simpson do_map_wirefuture = FALSE; 915375be18SAlan Cox new = round_page((vm_offset_t)uap->nsize); 925375be18SAlan Cox vm_map_lock(&vm->vm_map); 93df8bae1dSRodney W. Grimes 94f0e2953eSJohn Dyson base = round_page((vm_offset_t) vm->vm_daddr); 95a77c9610SDavid Malone old = base + ctob(vm->vm_dsize); 96f0e2953eSJohn Dyson if (new > base) { 97a77c9610SDavid Malone /* 983d66f138SAlan Cox * Check the resource limit, but allow a process to reduce 993d66f138SAlan Cox * its usage, even if it remains over the limit. 100a77c9610SDavid Malone */ 10191d5354aSJohn Baldwin if (new - base > datalim && new > old) { 1020cddd8f0SMatthew Dillon error = ENOMEM; 1030cddd8f0SMatthew Dillon goto done; 1040cddd8f0SMatthew Dillon } 10505ba50f5SJake Burkholder if (new > vm_map_max(&vm->vm_map)) { 1060cddd8f0SMatthew Dillon error = ENOMEM; 1070cddd8f0SMatthew Dillon goto done; 1080cddd8f0SMatthew Dillon } 109f0e2953eSJohn Dyson } else if (new < base) { 110f0e2953eSJohn Dyson /* 111f0e2953eSJohn Dyson * This is simply an invalid value. If someone wants to 112f0e2953eSJohn Dyson * do fancy address space manipulations, mmap and munmap 113f0e2953eSJohn Dyson * can do most of what the user would want. 114f0e2953eSJohn Dyson */ 1150cddd8f0SMatthew Dillon error = EINVAL; 1160cddd8f0SMatthew Dillon goto done; 117f0e2953eSJohn Dyson } 118f0e2953eSJohn Dyson if (new > old) { 11991d5354aSJohn Baldwin if (vm->vm_map.size + (new - old) > vmemlim) { 120070f64feSMatthew Dillon error = ENOMEM; 121070f64feSMatthew Dillon goto done; 122070f64feSMatthew Dillon } 1235375be18SAlan Cox rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new, 1245313b05fSMatthew Dillon VM_PROT_ALL, VM_PROT_ALL, 0); 125df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 1260cddd8f0SMatthew Dillon error = ENOMEM; 1270cddd8f0SMatthew Dillon goto done; 128df8bae1dSRodney W. Grimes } 1295375be18SAlan Cox vm->vm_dsize += btoc(new - old); 130abd498aaSBruce M Simpson /* 131abd498aaSBruce M Simpson * Handle the MAP_WIREFUTURE case for legacy applications, 132abd498aaSBruce M Simpson * by marking the newly mapped range of pages as wired. 133abd498aaSBruce M Simpson * We are not required to perform a corresponding 134abd498aaSBruce M Simpson * vm_map_unwire() before vm_map_delete() below, as 135abd498aaSBruce M Simpson * it will forcibly unwire the pages in the range. 136abd498aaSBruce M Simpson * 137abd498aaSBruce M Simpson * XXX If the pages cannot be wired, no error is returned. 138abd498aaSBruce M Simpson */ 139abd498aaSBruce M Simpson if ((vm->vm_map.flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) { 140abd498aaSBruce M Simpson if (bootverbose) 141abd498aaSBruce M Simpson printf("obreak: MAP_WIREFUTURE set\n"); 142abd498aaSBruce M Simpson do_map_wirefuture = TRUE; 143abd498aaSBruce M Simpson } 144f0e2953eSJohn Dyson } else if (new < old) { 1455375be18SAlan Cox rv = vm_map_delete(&vm->vm_map, new, old); 146df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 1470cddd8f0SMatthew Dillon error = ENOMEM; 1480cddd8f0SMatthew Dillon goto done; 149df8bae1dSRodney W. Grimes } 150f0e2953eSJohn Dyson vm->vm_dsize -= btoc(old - new); 151bb10bb49SJohn Baldwin } 1520cddd8f0SMatthew Dillon done: 1535375be18SAlan Cox vm_map_unlock(&vm->vm_map); 154abd498aaSBruce M Simpson 155abd498aaSBruce M Simpson if (do_map_wirefuture) 156abd498aaSBruce M Simpson (void) vm_map_wire(&vm->vm_map, old, new, 157abd498aaSBruce M Simpson VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 158abd498aaSBruce M Simpson 1590cddd8f0SMatthew Dillon return (error); 160df8bae1dSRodney W. Grimes } 161df8bae1dSRodney W. Grimes 162d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 163df8bae1dSRodney W. Grimes struct ovadvise_args { 164df8bae1dSRodney W. Grimes int anom; 165df8bae1dSRodney W. Grimes }; 166d2d3e875SBruce Evans #endif 16726f9a767SRodney W. Grimes 1686a33d53cSMatthew Dillon /* 1696a33d53cSMatthew Dillon * MPSAFE 1706a33d53cSMatthew Dillon */ 171df8bae1dSRodney W. Grimes /* ARGSUSED */ 172df8bae1dSRodney W. Grimes int 173b40ce416SJulian Elischer ovadvise(td, uap) 174b40ce416SJulian Elischer struct thread *td; 175df8bae1dSRodney W. Grimes struct ovadvise_args *uap; 176df8bae1dSRodney W. Grimes { 1770cddd8f0SMatthew Dillon /* START_GIANT_OPTIONAL */ 1780cddd8f0SMatthew Dillon /* END_GIANT_OPTIONAL */ 179df8bae1dSRodney W. Grimes return (EINVAL); 180df8bae1dSRodney W. Grimes } 181