160727d8bSWarner Losh /*- 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 * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$ 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 395f816602SMarcel Moolenaar #include "opt_compat.h" 405f816602SMarcel Moolenaar 41df8bae1dSRodney W. Grimes /* 42df8bae1dSRodney W. Grimes * Traditional sbrk/grow interface to VM 43df8bae1dSRodney W. Grimes */ 44190609ddSJohn Baldwin 45874651b1SDavid E. O'Brien #include <sys/cdefs.h> 46874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 47874651b1SDavid E. O'Brien 48df8bae1dSRodney W. Grimes #include <sys/param.h> 49fb919e4dSMark Murray #include <sys/lock.h> 5086e92ee7SJohn Baldwin #include <sys/mutex.h> 51df8bae1dSRodney W. Grimes #include <sys/proc.h> 521ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 53df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 545f816602SMarcel Moolenaar #include <sys/sysent.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 748451d0ddSKip Macy sys_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; 80*7e19eda4SAndrey Zonov rlim_t datalim, lmemlim, vmemlim; 815f816602SMarcel Moolenaar int prot, 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); 87*7e19eda4SAndrey Zonov lmemlim = lim_cur(td->td_proc, RLIMIT_MEMLOCK); 8891d5354aSJohn Baldwin vmemlim = lim_cur(td->td_proc, RLIMIT_VMEM); 8991d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 9091d5354aSJohn Baldwin 91abd498aaSBruce M Simpson do_map_wirefuture = FALSE; 925375be18SAlan Cox new = round_page((vm_offset_t)uap->nsize); 935375be18SAlan Cox vm_map_lock(&vm->vm_map); 94df8bae1dSRodney W. Grimes 95f0e2953eSJohn Dyson base = round_page((vm_offset_t) vm->vm_daddr); 96a77c9610SDavid Malone old = base + ctob(vm->vm_dsize); 97f0e2953eSJohn Dyson if (new > base) { 98a77c9610SDavid Malone /* 993d66f138SAlan Cox * Check the resource limit, but allow a process to reduce 1003d66f138SAlan Cox * its usage, even if it remains over the limit. 101a77c9610SDavid Malone */ 10291d5354aSJohn Baldwin if (new - base > datalim && new > old) { 1030cddd8f0SMatthew Dillon error = ENOMEM; 1040cddd8f0SMatthew Dillon goto done; 1050cddd8f0SMatthew Dillon } 10605ba50f5SJake Burkholder if (new > vm_map_max(&vm->vm_map)) { 1070cddd8f0SMatthew Dillon error = ENOMEM; 1080cddd8f0SMatthew Dillon goto done; 1090cddd8f0SMatthew Dillon } 110f0e2953eSJohn Dyson } else if (new < base) { 111f0e2953eSJohn Dyson /* 112f0e2953eSJohn Dyson * This is simply an invalid value. If someone wants to 113f0e2953eSJohn Dyson * do fancy address space manipulations, mmap and munmap 114f0e2953eSJohn Dyson * can do most of what the user would want. 115f0e2953eSJohn Dyson */ 1160cddd8f0SMatthew Dillon error = EINVAL; 1170cddd8f0SMatthew Dillon goto done; 118f0e2953eSJohn Dyson } 119f0e2953eSJohn Dyson if (new > old) { 120*7e19eda4SAndrey Zonov if (!old_mlock && vm->vm_map.flags & MAP_WIREFUTURE) { 121*7e19eda4SAndrey Zonov if (ptoa(vmspace_wired_count(td->td_proc->p_vmspace)) + 122*7e19eda4SAndrey Zonov (new - old) > lmemlim) { 123*7e19eda4SAndrey Zonov error = ENOMEM; 124*7e19eda4SAndrey Zonov goto done; 125*7e19eda4SAndrey Zonov } 126*7e19eda4SAndrey Zonov } 12791d5354aSJohn Baldwin if (vm->vm_map.size + (new - old) > vmemlim) { 128070f64feSMatthew Dillon error = ENOMEM; 129070f64feSMatthew Dillon goto done; 130070f64feSMatthew Dillon } 131afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1321ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1331ba5ad42SEdward Tomasz Napierala error = racct_set(td->td_proc, RACCT_DATA, new - base); 1341ba5ad42SEdward Tomasz Napierala if (error != 0) { 1351ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 1361ba5ad42SEdward Tomasz Napierala error = ENOMEM; 1371ba5ad42SEdward Tomasz Napierala goto done; 1381ba5ad42SEdward Tomasz Napierala } 1391ba5ad42SEdward Tomasz Napierala error = racct_set(td->td_proc, RACCT_VMEM, 1401ba5ad42SEdward Tomasz Napierala vm->vm_map.size + (new - old)); 1411ba5ad42SEdward Tomasz Napierala if (error != 0) { 1421ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, old - base); 1431ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 1441ba5ad42SEdward Tomasz Napierala error = ENOMEM; 1451ba5ad42SEdward Tomasz Napierala goto done; 1461ba5ad42SEdward Tomasz Napierala } 147*7e19eda4SAndrey Zonov if (!old_mlock && vm->vm_map.flags & MAP_WIREFUTURE) { 148*7e19eda4SAndrey Zonov error = racct_set(td->td_proc, RACCT_MEMLOCK, 149*7e19eda4SAndrey Zonov ptoa(vmspace_wired_count(td->td_proc->p_vmspace)) + 150*7e19eda4SAndrey Zonov (new - old)); 151*7e19eda4SAndrey Zonov if (error != 0) { 152*7e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_DATA, 153*7e19eda4SAndrey Zonov old - base); 154*7e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_VMEM, 155*7e19eda4SAndrey Zonov vm->vm_map.size); 156*7e19eda4SAndrey Zonov PROC_UNLOCK(td->td_proc); 157*7e19eda4SAndrey Zonov error = ENOMEM; 158*7e19eda4SAndrey Zonov goto done; 159*7e19eda4SAndrey Zonov } 160*7e19eda4SAndrey Zonov } 1611ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 162afcc55f3SEdward Tomasz Napierala #endif 1635f816602SMarcel Moolenaar prot = VM_PROT_RW; 1645f816602SMarcel Moolenaar #ifdef COMPAT_FREEBSD32 1655f816602SMarcel Moolenaar #if defined(__amd64__) || defined(__ia64__) 166126b36a2SKonstantin Belousov if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32)) 1675f816602SMarcel Moolenaar prot |= VM_PROT_EXECUTE; 1685f816602SMarcel Moolenaar #endif 1695f816602SMarcel Moolenaar #endif 1705375be18SAlan Cox rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new, 1715f816602SMarcel Moolenaar prot, VM_PROT_ALL, 0); 172df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 173afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1741ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1751ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, old - base); 1761ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_VMEM, vm->vm_map.size); 177*7e19eda4SAndrey Zonov if (!old_mlock && vm->vm_map.flags & MAP_WIREFUTURE) { 178*7e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_MEMLOCK, 179*7e19eda4SAndrey Zonov ptoa(vmspace_wired_count( 180*7e19eda4SAndrey Zonov td->td_proc->p_vmspace))); 181*7e19eda4SAndrey Zonov } 1821ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 183afcc55f3SEdward Tomasz Napierala #endif 1840cddd8f0SMatthew Dillon error = ENOMEM; 1850cddd8f0SMatthew Dillon goto done; 186df8bae1dSRodney W. Grimes } 1875375be18SAlan Cox vm->vm_dsize += btoc(new - old); 188abd498aaSBruce M Simpson /* 189abd498aaSBruce M Simpson * Handle the MAP_WIREFUTURE case for legacy applications, 190abd498aaSBruce M Simpson * by marking the newly mapped range of pages as wired. 191abd498aaSBruce M Simpson * We are not required to perform a corresponding 192abd498aaSBruce M Simpson * vm_map_unwire() before vm_map_delete() below, as 193abd498aaSBruce M Simpson * it will forcibly unwire the pages in the range. 194abd498aaSBruce M Simpson * 195abd498aaSBruce M Simpson * XXX If the pages cannot be wired, no error is returned. 196abd498aaSBruce M Simpson */ 197abd498aaSBruce M Simpson if ((vm->vm_map.flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) { 198abd498aaSBruce M Simpson if (bootverbose) 199abd498aaSBruce M Simpson printf("obreak: MAP_WIREFUTURE set\n"); 200abd498aaSBruce M Simpson do_map_wirefuture = TRUE; 201abd498aaSBruce M Simpson } 202f0e2953eSJohn Dyson } else if (new < old) { 203655c3490SKonstantin Belousov rv = vm_map_delete(&vm->vm_map, new, old); 204df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 2050cddd8f0SMatthew Dillon error = ENOMEM; 2060cddd8f0SMatthew Dillon goto done; 207df8bae1dSRodney W. Grimes } 208f0e2953eSJohn Dyson vm->vm_dsize -= btoc(old - new); 209afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2101ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 2111ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, new - base); 2121ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_VMEM, vm->vm_map.size); 213*7e19eda4SAndrey Zonov if (!old_mlock && vm->vm_map.flags & MAP_WIREFUTURE) { 214*7e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_MEMLOCK, 215*7e19eda4SAndrey Zonov ptoa(vmspace_wired_count(td->td_proc->p_vmspace))); 216*7e19eda4SAndrey Zonov } 2171ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 218afcc55f3SEdward Tomasz Napierala #endif 219bb10bb49SJohn Baldwin } 2200cddd8f0SMatthew Dillon done: 2215375be18SAlan Cox vm_map_unlock(&vm->vm_map); 222abd498aaSBruce M Simpson 223abd498aaSBruce M Simpson if (do_map_wirefuture) 224abd498aaSBruce M Simpson (void) vm_map_wire(&vm->vm_map, old, new, 225abd498aaSBruce M Simpson VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 226abd498aaSBruce M Simpson 2270cddd8f0SMatthew Dillon return (error); 228df8bae1dSRodney W. Grimes } 229df8bae1dSRodney W. Grimes 230d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 231df8bae1dSRodney W. Grimes struct ovadvise_args { 232df8bae1dSRodney W. Grimes int anom; 233df8bae1dSRodney W. Grimes }; 234d2d3e875SBruce Evans #endif 23526f9a767SRodney W. Grimes 2366a33d53cSMatthew Dillon /* 2376a33d53cSMatthew Dillon * MPSAFE 2386a33d53cSMatthew Dillon */ 239df8bae1dSRodney W. Grimes /* ARGSUSED */ 240df8bae1dSRodney W. Grimes int 2418451d0ddSKip Macy sys_ovadvise(td, uap) 242b40ce416SJulian Elischer struct thread *td; 243df8bae1dSRodney W. Grimes struct ovadvise_args *uap; 244df8bae1dSRodney W. Grimes { 2450cddd8f0SMatthew Dillon /* START_GIANT_OPTIONAL */ 2460cddd8f0SMatthew Dillon /* END_GIANT_OPTIONAL */ 247df8bae1dSRodney W. Grimes return (EINVAL); 248df8bae1dSRodney W. Grimes } 249