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
39df8bae1dSRodney W. Grimes /*
40df8bae1dSRodney W. Grimes * Traditional sbrk/grow interface to VM
41df8bae1dSRodney W. Grimes */
42190609ddSJohn Baldwin
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44fb919e4dSMark Murray #include <sys/lock.h>
4586e92ee7SJohn Baldwin #include <sys/mutex.h>
46df8bae1dSRodney W. Grimes #include <sys/proc.h>
471ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
48df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
49e8a1ec3eSEd Maste #include <sys/syscallsubr.h>
505f816602SMarcel Moolenaar #include <sys/sysent.h>
5186e92ee7SJohn Baldwin #include <sys/sysproto.h>
5223955314SAlfred Perlstein #include <sys/systm.h>
53a7f67facSKonstantin Belousov #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */
54a7f67facSKonstantin Belousov #include <machine/md_var.h>
55a7f67facSKonstantin Belousov #endif
56df8bae1dSRodney W. Grimes
57df8bae1dSRodney W. Grimes #include <vm/vm.h>
58efeaf95aSDavid Greenman #include <vm/vm_param.h>
59efeaf95aSDavid Greenman #include <vm/pmap.h>
60efeaf95aSDavid Greenman #include <vm/vm_map.h>
6126f9a767SRodney W. Grimes
62d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
639da5364eSBrooks Davis struct break_args {
649ed1bde7SBruce Evans char *nsize;
65df8bae1dSRodney W. Grimes };
66d2d3e875SBruce Evans #endif
67df8bae1dSRodney W. Grimes int
sys_break(struct thread * td,struct break_args * uap)689da5364eSBrooks Davis sys_break(struct thread *td, struct break_args *uap)
69df8bae1dSRodney W. Grimes {
7003154adeSLi-Wen Hsu #if !defined(__aarch64__) && !defined(__riscv)
71e8a1ec3eSEd Maste uintptr_t addr;
72e8a1ec3eSEd Maste int error;
73e8a1ec3eSEd Maste
74e8a1ec3eSEd Maste addr = (uintptr_t)uap->nsize;
75e8a1ec3eSEd Maste error = kern_break(td, &addr);
76e8a1ec3eSEd Maste if (error == 0)
77e8a1ec3eSEd Maste td->td_retval[0] = addr;
78e8a1ec3eSEd Maste return (error);
7903154adeSLi-Wen Hsu #else /* defined(__aarch64__) || defined(__riscv) */
80e8a1ec3eSEd Maste return (ENOSYS);
8103154adeSLi-Wen Hsu #endif /* defined(__aarch64__) || defined(__riscv) */
82e8a1ec3eSEd Maste }
83e8a1ec3eSEd Maste
84e8a1ec3eSEd Maste int
kern_break(struct thread * td,uintptr_t * addr)85e8a1ec3eSEd Maste kern_break(struct thread *td, uintptr_t *addr)
86e8a1ec3eSEd Maste {
87b40ce416SJulian Elischer struct vmspace *vm = td->td_proc->p_vmspace;
88cde4a725SAndrey Zonov vm_map_t map = &vm->vm_map;
89f0e2953eSJohn Dyson vm_offset_t new, old, base;
907e19eda4SAndrey Zonov rlim_t datalim, lmemlim, vmemlim;
915f816602SMarcel Moolenaar int prot, rv;
920cddd8f0SMatthew Dillon int error = 0;
930cddd8f0SMatthew Dillon
94f6f6d240SMateusz Guzik datalim = lim_cur(td, RLIMIT_DATA);
95f6f6d240SMateusz Guzik lmemlim = lim_cur(td, RLIMIT_MEMLOCK);
96f6f6d240SMateusz Guzik vmemlim = lim_cur(td, RLIMIT_VMEM);
9791d5354aSJohn Baldwin
98e8a1ec3eSEd Maste new = round_page(*addr);
99cde4a725SAndrey Zonov vm_map_lock(map);
100df8bae1dSRodney W. Grimes
101f0e2953eSJohn Dyson base = round_page((vm_offset_t) vm->vm_daddr);
102a77c9610SDavid Malone old = base + ctob(vm->vm_dsize);
103f0e2953eSJohn Dyson if (new > base) {
104a77c9610SDavid Malone /*
1053d66f138SAlan Cox * Check the resource limit, but allow a process to reduce
1063d66f138SAlan Cox * its usage, even if it remains over the limit.
107a77c9610SDavid Malone */
10891d5354aSJohn Baldwin if (new - base > datalim && new > old) {
1090cddd8f0SMatthew Dillon error = ENOMEM;
1100cddd8f0SMatthew Dillon goto done;
1110cddd8f0SMatthew Dillon }
112cde4a725SAndrey Zonov if (new > vm_map_max(map)) {
1130cddd8f0SMatthew Dillon error = ENOMEM;
1140cddd8f0SMatthew Dillon goto done;
1150cddd8f0SMatthew Dillon }
116f0e2953eSJohn Dyson } else if (new < base) {
117f0e2953eSJohn Dyson /*
1189f9c9b22SMark Johnston * Simply return the current break address without
1199f9c9b22SMark Johnston * modifying any state. This is an ad-hoc interface
1209f9c9b22SMark Johnston * used by libc to determine the initial break address,
1219f9c9b22SMark Johnston * avoiding a dependency on magic features in the system
1229f9c9b22SMark Johnston * linker.
123f0e2953eSJohn Dyson */
1249f9c9b22SMark Johnston new = old;
1250cddd8f0SMatthew Dillon goto done;
126f0e2953eSJohn Dyson }
1279f9c9b22SMark Johnston
128f0e2953eSJohn Dyson if (new > old) {
129cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) {
130cde4a725SAndrey Zonov if (ptoa(pmap_wired_count(map->pmap)) +
1317e19eda4SAndrey Zonov (new - old) > lmemlim) {
1327e19eda4SAndrey Zonov error = ENOMEM;
1337e19eda4SAndrey Zonov goto done;
1347e19eda4SAndrey Zonov }
1357e19eda4SAndrey Zonov }
136cde4a725SAndrey Zonov if (map->size + (new - old) > vmemlim) {
137070f64feSMatthew Dillon error = ENOMEM;
138070f64feSMatthew Dillon goto done;
139070f64feSMatthew Dillon }
140afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1414b5c9cf6SEdward Tomasz Napierala if (racct_enable) {
1421ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc);
1431ba5ad42SEdward Tomasz Napierala error = racct_set(td->td_proc, RACCT_DATA, new - base);
1441ba5ad42SEdward Tomasz Napierala if (error != 0) {
1451ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc);
1461ba5ad42SEdward Tomasz Napierala error = ENOMEM;
1471ba5ad42SEdward Tomasz Napierala goto done;
1481ba5ad42SEdward Tomasz Napierala }
1491ba5ad42SEdward Tomasz Napierala error = racct_set(td->td_proc, RACCT_VMEM,
150cde4a725SAndrey Zonov map->size + (new - old));
1511ba5ad42SEdward Tomasz Napierala if (error != 0) {
1524b5c9cf6SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA,
1534b5c9cf6SEdward Tomasz Napierala old - base);
1541ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc);
1551ba5ad42SEdward Tomasz Napierala error = ENOMEM;
1561ba5ad42SEdward Tomasz Napierala goto done;
1571ba5ad42SEdward Tomasz Napierala }
158cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1597e19eda4SAndrey Zonov error = racct_set(td->td_proc, RACCT_MEMLOCK,
1604b5c9cf6SEdward Tomasz Napierala ptoa(pmap_wired_count(map->pmap)) +
1614b5c9cf6SEdward Tomasz Napierala (new - old));
1627e19eda4SAndrey Zonov if (error != 0) {
1637e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_DATA,
1647e19eda4SAndrey Zonov old - base);
1657e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_VMEM,
166cde4a725SAndrey Zonov map->size);
1677e19eda4SAndrey Zonov PROC_UNLOCK(td->td_proc);
1687e19eda4SAndrey Zonov error = ENOMEM;
1697e19eda4SAndrey Zonov goto done;
1707e19eda4SAndrey Zonov }
1717e19eda4SAndrey Zonov }
1721ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc);
1734b5c9cf6SEdward Tomasz Napierala }
174afcc55f3SEdward Tomasz Napierala #endif
1755f816602SMarcel Moolenaar prot = VM_PROT_RW;
1765dddee2dSKonstantin Belousov #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
177126b36a2SKonstantin Belousov if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32))
1785f816602SMarcel Moolenaar prot |= VM_PROT_EXECUTE;
1795f816602SMarcel Moolenaar #endif
18054a3a114SMark Johnston rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL,
18154a3a114SMark Johnston 0);
18254a3a114SMark Johnston if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
18354a3a114SMark Johnston rv = vm_map_wire_locked(map, old, new,
18454a3a114SMark Johnston VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
18554a3a114SMark Johnston if (rv != KERN_SUCCESS)
186*e8f77c20SKonstantin Belousov (void)vm_map_delete(map, old, new);
18754a3a114SMark Johnston }
188df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) {
189afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1904b5c9cf6SEdward Tomasz Napierala if (racct_enable) {
1911ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc);
1924b5c9cf6SEdward Tomasz Napierala racct_set_force(td->td_proc,
1934b5c9cf6SEdward Tomasz Napierala RACCT_DATA, old - base);
1944b5c9cf6SEdward Tomasz Napierala racct_set_force(td->td_proc,
1954b5c9cf6SEdward Tomasz Napierala RACCT_VMEM, map->size);
196cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1974b5c9cf6SEdward Tomasz Napierala racct_set_force(td->td_proc,
1984b5c9cf6SEdward Tomasz Napierala RACCT_MEMLOCK,
199cde4a725SAndrey Zonov ptoa(pmap_wired_count(map->pmap)));
2007e19eda4SAndrey Zonov }
2011ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc);
2024b5c9cf6SEdward Tomasz Napierala }
203afcc55f3SEdward Tomasz Napierala #endif
2040cddd8f0SMatthew Dillon error = ENOMEM;
2050cddd8f0SMatthew Dillon goto done;
206df8bae1dSRodney W. Grimes }
2075375be18SAlan Cox vm->vm_dsize += btoc(new - old);
208f0e2953eSJohn Dyson } else if (new < old) {
209cde4a725SAndrey Zonov rv = vm_map_delete(map, new, old);
210df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) {
2110cddd8f0SMatthew Dillon error = ENOMEM;
2120cddd8f0SMatthew Dillon goto done;
213df8bae1dSRodney W. Grimes }
214f0e2953eSJohn Dyson vm->vm_dsize -= btoc(old - new);
215afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2164b5c9cf6SEdward Tomasz Napierala if (racct_enable) {
2171ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc);
2181ba5ad42SEdward Tomasz Napierala racct_set_force(td->td_proc, RACCT_DATA, new - base);
219cde4a725SAndrey Zonov racct_set_force(td->td_proc, RACCT_VMEM, map->size);
220cde4a725SAndrey Zonov if (!old_mlock && map->flags & MAP_WIREFUTURE) {
2217e19eda4SAndrey Zonov racct_set_force(td->td_proc, RACCT_MEMLOCK,
222cde4a725SAndrey Zonov ptoa(pmap_wired_count(map->pmap)));
2237e19eda4SAndrey Zonov }
2241ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc);
2254b5c9cf6SEdward Tomasz Napierala }
226afcc55f3SEdward Tomasz Napierala #endif
227bb10bb49SJohn Baldwin }
2280cddd8f0SMatthew Dillon done:
229cde4a725SAndrey Zonov vm_map_unlock(map);
230abd498aaSBruce M Simpson
2319f9c9b22SMark Johnston if (error == 0)
232e8a1ec3eSEd Maste *addr = new;
2339f9c9b22SMark Johnston
2340cddd8f0SMatthew Dillon return (error);
235df8bae1dSRodney W. Grimes }
236df8bae1dSRodney W. Grimes
2377351a8bdSBrooks Davis #ifdef COMPAT_FREEBSD11
238df8bae1dSRodney W. Grimes int
freebsd11_vadvise(struct thread * td,struct freebsd11_vadvise_args * uap)2397351a8bdSBrooks Davis freebsd11_vadvise(struct thread *td, struct freebsd11_vadvise_args *uap)
240df8bae1dSRodney W. Grimes {
2417351a8bdSBrooks Davis
242df8bae1dSRodney W. Grimes return (EINVAL);
243df8bae1dSRodney W. Grimes }
2447351a8bdSBrooks Davis #endif
245