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; 79*cde4a725SAndrey Zonov vm_map_t map = &vm->vm_map; 80f0e2953eSJohn Dyson vm_offset_t new, old, base; 817e19eda4SAndrey Zonov rlim_t datalim, lmemlim, vmemlim; 825f816602SMarcel Moolenaar int prot, rv; 830cddd8f0SMatthew Dillon int error = 0; 84abd498aaSBruce M Simpson boolean_t do_map_wirefuture; 850cddd8f0SMatthew Dillon 8691d5354aSJohn Baldwin PROC_LOCK(td->td_proc); 8791d5354aSJohn Baldwin datalim = lim_cur(td->td_proc, RLIMIT_DATA); 887e19eda4SAndrey Zonov lmemlim = lim_cur(td->td_proc, RLIMIT_MEMLOCK); 8991d5354aSJohn Baldwin vmemlim = lim_cur(td->td_proc, RLIMIT_VMEM); 9091d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 9191d5354aSJohn Baldwin 92abd498aaSBruce M Simpson do_map_wirefuture = FALSE; 935375be18SAlan Cox new = round_page((vm_offset_t)uap->nsize); 94*cde4a725SAndrey Zonov vm_map_lock(map); 95df8bae1dSRodney W. Grimes 96f0e2953eSJohn Dyson base = round_page((vm_offset_t) vm->vm_daddr); 97a77c9610SDavid Malone old = base + ctob(vm->vm_dsize); 98f0e2953eSJohn Dyson if (new > base) { 99a77c9610SDavid Malone /* 1003d66f138SAlan Cox * Check the resource limit, but allow a process to reduce 1013d66f138SAlan Cox * its usage, even if it remains over the limit. 102a77c9610SDavid Malone */ 10391d5354aSJohn Baldwin if (new - base > datalim && new > old) { 1040cddd8f0SMatthew Dillon error = ENOMEM; 1050cddd8f0SMatthew Dillon goto done; 1060cddd8f0SMatthew Dillon } 107*cde4a725SAndrey Zonov if (new > vm_map_max(map)) { 1080cddd8f0SMatthew Dillon error = ENOMEM; 1090cddd8f0SMatthew Dillon goto done; 1100cddd8f0SMatthew Dillon } 111f0e2953eSJohn Dyson } else if (new < base) { 112f0e2953eSJohn Dyson /* 113f0e2953eSJohn Dyson * This is simply an invalid value. If someone wants to 114f0e2953eSJohn Dyson * do fancy address space manipulations, mmap and munmap 115f0e2953eSJohn Dyson * can do most of what the user would want. 116f0e2953eSJohn Dyson */ 1170cddd8f0SMatthew Dillon error = EINVAL; 1180cddd8f0SMatthew Dillon goto done; 119f0e2953eSJohn Dyson } 120f0e2953eSJohn Dyson if (new > old) { 121*cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) { 122*cde4a725SAndrey Zonov if (ptoa(pmap_wired_count(map->pmap)) + 1237e19eda4SAndrey Zonov (new - old) > lmemlim) { 1247e19eda4SAndrey Zonov error = ENOMEM; 1257e19eda4SAndrey Zonov goto done; 1267e19eda4SAndrey Zonov } 1277e19eda4SAndrey Zonov } 128*cde4a725SAndrey Zonov if (map->size + (new - old) > vmemlim) { 129070f64feSMatthew Dillon error = ENOMEM; 130070f64feSMatthew Dillon goto done; 131070f64feSMatthew Dillon } 132afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1331ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1341ba5ad42SEdward Tomasz Napierala error = racct_set(td->td_proc, RACCT_DATA, new - base); 1351ba5ad42SEdward Tomasz Napierala if (error != 0) { 1361ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 1371ba5ad42SEdward Tomasz Napierala error = ENOMEM; 1381ba5ad42SEdward Tomasz Napierala goto done; 1391ba5ad42SEdward Tomasz Napierala } 1401ba5ad42SEdward Tomasz Napierala error = racct_set(td->td_proc, RACCT_VMEM, 141*cde4a725SAndrey Zonov map->size + (new - old)); 1421ba5ad42SEdward Tomasz Napierala if (error != 0) { 1431ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, old - base); 1441ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 1451ba5ad42SEdward Tomasz Napierala error = ENOMEM; 1461ba5ad42SEdward Tomasz Napierala goto done; 1471ba5ad42SEdward Tomasz Napierala } 148*cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) { 1497e19eda4SAndrey Zonov error = racct_set(td->td_proc, RACCT_MEMLOCK, 150*cde4a725SAndrey Zonov ptoa(pmap_wired_count(map->pmap)) + (new - old)); 1517e19eda4SAndrey Zonov if (error != 0) { 1527e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_DATA, 1537e19eda4SAndrey Zonov old - base); 1547e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_VMEM, 155*cde4a725SAndrey Zonov map->size); 1567e19eda4SAndrey Zonov PROC_UNLOCK(td->td_proc); 1577e19eda4SAndrey Zonov error = ENOMEM; 1587e19eda4SAndrey Zonov goto done; 1597e19eda4SAndrey Zonov } 1607e19eda4SAndrey 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 170*cde4a725SAndrey Zonov rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL, 0); 171df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 172afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1731ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1741ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, old - base); 175*cde4a725SAndrey Zonov racct_set_force(td->td_proc, RACCT_VMEM, map->size); 176*cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) { 1777e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_MEMLOCK, 178*cde4a725SAndrey Zonov ptoa(pmap_wired_count(map->pmap))); 1797e19eda4SAndrey Zonov } 1801ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 181afcc55f3SEdward Tomasz Napierala #endif 1820cddd8f0SMatthew Dillon error = ENOMEM; 1830cddd8f0SMatthew Dillon goto done; 184df8bae1dSRodney W. Grimes } 1855375be18SAlan Cox vm->vm_dsize += btoc(new - old); 186abd498aaSBruce M Simpson /* 187abd498aaSBruce M Simpson * Handle the MAP_WIREFUTURE case for legacy applications, 188abd498aaSBruce M Simpson * by marking the newly mapped range of pages as wired. 189abd498aaSBruce M Simpson * We are not required to perform a corresponding 190abd498aaSBruce M Simpson * vm_map_unwire() before vm_map_delete() below, as 191abd498aaSBruce M Simpson * it will forcibly unwire the pages in the range. 192abd498aaSBruce M Simpson * 193abd498aaSBruce M Simpson * XXX If the pages cannot be wired, no error is returned. 194abd498aaSBruce M Simpson */ 195*cde4a725SAndrey Zonov if ((map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) { 196abd498aaSBruce M Simpson if (bootverbose) 197abd498aaSBruce M Simpson printf("obreak: MAP_WIREFUTURE set\n"); 198abd498aaSBruce M Simpson do_map_wirefuture = TRUE; 199abd498aaSBruce M Simpson } 200f0e2953eSJohn Dyson } else if (new < old) { 201*cde4a725SAndrey Zonov rv = vm_map_delete(map, new, old); 202df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 2030cddd8f0SMatthew Dillon error = ENOMEM; 2040cddd8f0SMatthew Dillon goto done; 205df8bae1dSRodney W. Grimes } 206f0e2953eSJohn Dyson vm->vm_dsize -= btoc(old - new); 207afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2081ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 2091ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, new - base); 210*cde4a725SAndrey Zonov racct_set_force(td->td_proc, RACCT_VMEM, map->size); 211*cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) { 2127e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_MEMLOCK, 213*cde4a725SAndrey Zonov ptoa(pmap_wired_count(map->pmap))); 2147e19eda4SAndrey Zonov } 2151ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 216afcc55f3SEdward Tomasz Napierala #endif 217bb10bb49SJohn Baldwin } 2180cddd8f0SMatthew Dillon done: 219*cde4a725SAndrey Zonov vm_map_unlock(map); 220abd498aaSBruce M Simpson 221abd498aaSBruce M Simpson if (do_map_wirefuture) 222*cde4a725SAndrey Zonov (void) vm_map_wire(map, old, new, 223abd498aaSBruce M Simpson VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 224abd498aaSBruce M Simpson 2250cddd8f0SMatthew Dillon return (error); 226df8bae1dSRodney W. Grimes } 227df8bae1dSRodney W. Grimes 228d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 229df8bae1dSRodney W. Grimes struct ovadvise_args { 230df8bae1dSRodney W. Grimes int anom; 231df8bae1dSRodney W. Grimes }; 232d2d3e875SBruce Evans #endif 23326f9a767SRodney W. Grimes 2346a33d53cSMatthew Dillon /* 2356a33d53cSMatthew Dillon * MPSAFE 2366a33d53cSMatthew Dillon */ 237df8bae1dSRodney W. Grimes /* ARGSUSED */ 238df8bae1dSRodney W. Grimes int 2398451d0ddSKip Macy sys_ovadvise(td, uap) 240b40ce416SJulian Elischer struct thread *td; 241df8bae1dSRodney W. Grimes struct ovadvise_args *uap; 242df8bae1dSRodney W. Grimes { 2430cddd8f0SMatthew Dillon /* START_GIANT_OPTIONAL */ 2440cddd8f0SMatthew Dillon /* END_GIANT_OPTIONAL */ 245df8bae1dSRodney W. Grimes return (EINVAL); 246df8bae1dSRodney W. Grimes } 247