160727d8bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1988 University of Utah. 5df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 6df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 9df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 10df8bae1dSRodney W. Grimes * Science Department. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 20fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$ 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 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> 54e8a1ec3eSEd Maste #include <sys/syscallsubr.h> 555f816602SMarcel Moolenaar #include <sys/sysent.h> 5686e92ee7SJohn Baldwin #include <sys/sysproto.h> 5723955314SAlfred Perlstein #include <sys/systm.h> 58*a7f67facSKonstantin Belousov #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */ 59*a7f67facSKonstantin Belousov #include <machine/md_var.h> 60*a7f67facSKonstantin Belousov #endif 61df8bae1dSRodney W. Grimes 62df8bae1dSRodney W. Grimes #include <vm/vm.h> 63efeaf95aSDavid Greenman #include <vm/vm_param.h> 64efeaf95aSDavid Greenman #include <vm/pmap.h> 65efeaf95aSDavid Greenman #include <vm/vm_map.h> 6626f9a767SRodney W. Grimes 67d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 689da5364eSBrooks Davis struct break_args { 699ed1bde7SBruce Evans char *nsize; 70df8bae1dSRodney W. Grimes }; 71d2d3e875SBruce Evans #endif 72df8bae1dSRodney W. Grimes int 739da5364eSBrooks Davis sys_break(struct thread *td, struct break_args *uap) 74df8bae1dSRodney W. Grimes { 7503154adeSLi-Wen Hsu #if !defined(__aarch64__) && !defined(__riscv) 76e8a1ec3eSEd Maste uintptr_t addr; 77e8a1ec3eSEd Maste int error; 78e8a1ec3eSEd Maste 79e8a1ec3eSEd Maste addr = (uintptr_t)uap->nsize; 80e8a1ec3eSEd Maste error = kern_break(td, &addr); 81e8a1ec3eSEd Maste if (error == 0) 82e8a1ec3eSEd Maste td->td_retval[0] = addr; 83e8a1ec3eSEd Maste return (error); 8403154adeSLi-Wen Hsu #else /* defined(__aarch64__) || defined(__riscv) */ 85e8a1ec3eSEd Maste return (ENOSYS); 8603154adeSLi-Wen Hsu #endif /* defined(__aarch64__) || defined(__riscv) */ 87e8a1ec3eSEd Maste } 88e8a1ec3eSEd Maste 89e8a1ec3eSEd Maste int 90e8a1ec3eSEd Maste kern_break(struct thread *td, uintptr_t *addr) 91e8a1ec3eSEd Maste { 92b40ce416SJulian Elischer struct vmspace *vm = td->td_proc->p_vmspace; 93cde4a725SAndrey Zonov vm_map_t map = &vm->vm_map; 94f0e2953eSJohn Dyson vm_offset_t new, old, base; 957e19eda4SAndrey Zonov rlim_t datalim, lmemlim, vmemlim; 965f816602SMarcel Moolenaar int prot, rv; 970cddd8f0SMatthew Dillon int error = 0; 98abd498aaSBruce M Simpson boolean_t do_map_wirefuture; 990cddd8f0SMatthew Dillon 100f6f6d240SMateusz Guzik datalim = lim_cur(td, RLIMIT_DATA); 101f6f6d240SMateusz Guzik lmemlim = lim_cur(td, RLIMIT_MEMLOCK); 102f6f6d240SMateusz Guzik vmemlim = lim_cur(td, RLIMIT_VMEM); 10391d5354aSJohn Baldwin 104abd498aaSBruce M Simpson do_map_wirefuture = FALSE; 105e8a1ec3eSEd Maste new = round_page(*addr); 106cde4a725SAndrey Zonov vm_map_lock(map); 107df8bae1dSRodney W. Grimes 108f0e2953eSJohn Dyson base = round_page((vm_offset_t) vm->vm_daddr); 109a77c9610SDavid Malone old = base + ctob(vm->vm_dsize); 110f0e2953eSJohn Dyson if (new > base) { 111a77c9610SDavid Malone /* 1123d66f138SAlan Cox * Check the resource limit, but allow a process to reduce 1133d66f138SAlan Cox * its usage, even if it remains over the limit. 114a77c9610SDavid Malone */ 11591d5354aSJohn Baldwin if (new - base > datalim && new > old) { 1160cddd8f0SMatthew Dillon error = ENOMEM; 1170cddd8f0SMatthew Dillon goto done; 1180cddd8f0SMatthew Dillon } 119cde4a725SAndrey Zonov if (new > vm_map_max(map)) { 1200cddd8f0SMatthew Dillon error = ENOMEM; 1210cddd8f0SMatthew Dillon goto done; 1220cddd8f0SMatthew Dillon } 123f0e2953eSJohn Dyson } else if (new < base) { 124f0e2953eSJohn Dyson /* 1259f9c9b22SMark Johnston * Simply return the current break address without 1269f9c9b22SMark Johnston * modifying any state. This is an ad-hoc interface 1279f9c9b22SMark Johnston * used by libc to determine the initial break address, 1289f9c9b22SMark Johnston * avoiding a dependency on magic features in the system 1299f9c9b22SMark Johnston * linker. 130f0e2953eSJohn Dyson */ 1319f9c9b22SMark Johnston new = old; 1320cddd8f0SMatthew Dillon goto done; 133f0e2953eSJohn Dyson } 1349f9c9b22SMark Johnston 135f0e2953eSJohn Dyson if (new > old) { 136cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) { 137cde4a725SAndrey Zonov if (ptoa(pmap_wired_count(map->pmap)) + 1387e19eda4SAndrey Zonov (new - old) > lmemlim) { 1397e19eda4SAndrey Zonov error = ENOMEM; 1407e19eda4SAndrey Zonov goto done; 1417e19eda4SAndrey Zonov } 1427e19eda4SAndrey Zonov } 143cde4a725SAndrey Zonov if (map->size + (new - old) > vmemlim) { 144070f64feSMatthew Dillon error = ENOMEM; 145070f64feSMatthew Dillon goto done; 146070f64feSMatthew Dillon } 147afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1484b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 1491ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1501ba5ad42SEdward Tomasz Napierala error = racct_set(td->td_proc, RACCT_DATA, new - base); 1511ba5ad42SEdward Tomasz Napierala if (error != 0) { 1521ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 1531ba5ad42SEdward Tomasz Napierala error = ENOMEM; 1541ba5ad42SEdward Tomasz Napierala goto done; 1551ba5ad42SEdward Tomasz Napierala } 1561ba5ad42SEdward Tomasz Napierala error = racct_set(td->td_proc, RACCT_VMEM, 157cde4a725SAndrey Zonov map->size + (new - old)); 1581ba5ad42SEdward Tomasz Napierala if (error != 0) { 1594b5c9cf6SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, 1604b5c9cf6SEdward Tomasz Napierala old - base); 1611ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 1621ba5ad42SEdward Tomasz Napierala error = ENOMEM; 1631ba5ad42SEdward Tomasz Napierala goto done; 1641ba5ad42SEdward Tomasz Napierala } 165cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) { 1667e19eda4SAndrey Zonov error = racct_set(td->td_proc, RACCT_MEMLOCK, 1674b5c9cf6SEdward Tomasz Napierala ptoa(pmap_wired_count(map->pmap)) + 1684b5c9cf6SEdward Tomasz Napierala (new - old)); 1697e19eda4SAndrey Zonov if (error != 0) { 1707e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_DATA, 1717e19eda4SAndrey Zonov old - base); 1727e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_VMEM, 173cde4a725SAndrey Zonov map->size); 1747e19eda4SAndrey Zonov PROC_UNLOCK(td->td_proc); 1757e19eda4SAndrey Zonov error = ENOMEM; 1767e19eda4SAndrey Zonov goto done; 1777e19eda4SAndrey Zonov } 1787e19eda4SAndrey Zonov } 1791ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 1804b5c9cf6SEdward Tomasz Napierala } 181afcc55f3SEdward Tomasz Napierala #endif 1825f816602SMarcel Moolenaar prot = VM_PROT_RW; 1835f816602SMarcel Moolenaar #ifdef COMPAT_FREEBSD32 184e7d939bdSMarcel Moolenaar #if defined(__amd64__) 185126b36a2SKonstantin Belousov if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32)) 1865f816602SMarcel Moolenaar prot |= VM_PROT_EXECUTE; 1875f816602SMarcel Moolenaar #endif 1885f816602SMarcel Moolenaar #endif 189cde4a725SAndrey Zonov rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL, 0); 190df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 191afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1924b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 1931ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1944b5c9cf6SEdward Tomasz Napierala racct_set_force(td->td_proc, 1954b5c9cf6SEdward Tomasz Napierala RACCT_DATA, old - base); 1964b5c9cf6SEdward Tomasz Napierala racct_set_force(td->td_proc, 1974b5c9cf6SEdward Tomasz Napierala RACCT_VMEM, map->size); 198cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) { 1994b5c9cf6SEdward Tomasz Napierala racct_set_force(td->td_proc, 2004b5c9cf6SEdward Tomasz Napierala RACCT_MEMLOCK, 201cde4a725SAndrey Zonov ptoa(pmap_wired_count(map->pmap))); 2027e19eda4SAndrey Zonov } 2031ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 2044b5c9cf6SEdward Tomasz Napierala } 205afcc55f3SEdward Tomasz Napierala #endif 2060cddd8f0SMatthew Dillon error = ENOMEM; 2070cddd8f0SMatthew Dillon goto done; 208df8bae1dSRodney W. Grimes } 2095375be18SAlan Cox vm->vm_dsize += btoc(new - old); 210abd498aaSBruce M Simpson /* 211abd498aaSBruce M Simpson * Handle the MAP_WIREFUTURE case for legacy applications, 212abd498aaSBruce M Simpson * by marking the newly mapped range of pages as wired. 213abd498aaSBruce M Simpson * We are not required to perform a corresponding 214abd498aaSBruce M Simpson * vm_map_unwire() before vm_map_delete() below, as 215abd498aaSBruce M Simpson * it will forcibly unwire the pages in the range. 216abd498aaSBruce M Simpson * 217abd498aaSBruce M Simpson * XXX If the pages cannot be wired, no error is returned. 218abd498aaSBruce M Simpson */ 2199da5364eSBrooks Davis if ((map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) 220abd498aaSBruce M Simpson do_map_wirefuture = TRUE; 221f0e2953eSJohn Dyson } else if (new < old) { 222cde4a725SAndrey Zonov rv = vm_map_delete(map, new, old); 223df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 2240cddd8f0SMatthew Dillon error = ENOMEM; 2250cddd8f0SMatthew Dillon goto done; 226df8bae1dSRodney W. Grimes } 227f0e2953eSJohn Dyson vm->vm_dsize -= btoc(old - new); 228afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2294b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 2301ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 2311ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, new - base); 232cde4a725SAndrey Zonov racct_set_force(td->td_proc, RACCT_VMEM, map->size); 233cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) { 2347e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_MEMLOCK, 235cde4a725SAndrey Zonov ptoa(pmap_wired_count(map->pmap))); 2367e19eda4SAndrey Zonov } 2371ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 2384b5c9cf6SEdward Tomasz Napierala } 239afcc55f3SEdward Tomasz Napierala #endif 240bb10bb49SJohn Baldwin } 2410cddd8f0SMatthew Dillon done: 242cde4a725SAndrey Zonov vm_map_unlock(map); 243abd498aaSBruce M Simpson 244abd498aaSBruce M Simpson if (do_map_wirefuture) 245cde4a725SAndrey Zonov (void) vm_map_wire(map, old, new, 246abd498aaSBruce M Simpson VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 247abd498aaSBruce M Simpson 2489f9c9b22SMark Johnston if (error == 0) 249e8a1ec3eSEd Maste *addr = new; 2509f9c9b22SMark Johnston 2510cddd8f0SMatthew Dillon return (error); 252df8bae1dSRodney W. Grimes } 253df8bae1dSRodney W. Grimes 2547351a8bdSBrooks Davis #ifdef COMPAT_FREEBSD11 255df8bae1dSRodney W. Grimes int 2567351a8bdSBrooks Davis freebsd11_vadvise(struct thread *td, struct freebsd11_vadvise_args *uap) 257df8bae1dSRodney W. Grimes { 2587351a8bdSBrooks Davis 259df8bae1dSRodney W. Grimes return (EINVAL); 260df8bae1dSRodney W. Grimes } 2617351a8bdSBrooks Davis #endif 262