xref: /freebsd/sys/vm/vm_unix.c (revision 9da5364ed9633258f31bfa1235dd1ae7881bb956)
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>
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_
64*9da5364eSBrooks Davis struct break_args {
659ed1bde7SBruce Evans 	char *nsize;
66df8bae1dSRodney W. Grimes };
67d2d3e875SBruce Evans #endif
68df8bae1dSRodney W. Grimes int
69*9da5364eSBrooks Davis sys_break(struct thread *td, struct break_args *uap)
70df8bae1dSRodney W. Grimes {
71758d46cfSBrooks Davis #if !defined(__aarch64__) && !defined(__riscv__)
72b40ce416SJulian Elischer 	struct vmspace *vm = td->td_proc->p_vmspace;
73cde4a725SAndrey Zonov 	vm_map_t map = &vm->vm_map;
74f0e2953eSJohn Dyson 	vm_offset_t new, old, base;
757e19eda4SAndrey Zonov 	rlim_t datalim, lmemlim, vmemlim;
765f816602SMarcel Moolenaar 	int prot, rv;
770cddd8f0SMatthew Dillon 	int error = 0;
78abd498aaSBruce M Simpson 	boolean_t do_map_wirefuture;
790cddd8f0SMatthew Dillon 
80f6f6d240SMateusz Guzik 	datalim = lim_cur(td, RLIMIT_DATA);
81f6f6d240SMateusz Guzik 	lmemlim = lim_cur(td, RLIMIT_MEMLOCK);
82f6f6d240SMateusz Guzik 	vmemlim = lim_cur(td, RLIMIT_VMEM);
8391d5354aSJohn Baldwin 
84abd498aaSBruce M Simpson 	do_map_wirefuture = FALSE;
855375be18SAlan Cox 	new = round_page((vm_offset_t)uap->nsize);
86cde4a725SAndrey Zonov 	vm_map_lock(map);
87df8bae1dSRodney W. Grimes 
88f0e2953eSJohn Dyson 	base = round_page((vm_offset_t) vm->vm_daddr);
89a77c9610SDavid Malone 	old = base + ctob(vm->vm_dsize);
90f0e2953eSJohn Dyson 	if (new > base) {
91a77c9610SDavid Malone 		/*
923d66f138SAlan Cox 		 * Check the resource limit, but allow a process to reduce
933d66f138SAlan Cox 		 * its usage, even if it remains over the limit.
94a77c9610SDavid Malone 		 */
9591d5354aSJohn Baldwin 		if (new - base > datalim && new > old) {
960cddd8f0SMatthew Dillon 			error = ENOMEM;
970cddd8f0SMatthew Dillon 			goto done;
980cddd8f0SMatthew Dillon 		}
99cde4a725SAndrey Zonov 		if (new > vm_map_max(map)) {
1000cddd8f0SMatthew Dillon 			error = ENOMEM;
1010cddd8f0SMatthew Dillon 			goto done;
1020cddd8f0SMatthew Dillon 		}
103f0e2953eSJohn Dyson 	} else if (new < base) {
104f0e2953eSJohn Dyson 		/*
1059f9c9b22SMark Johnston 		 * Simply return the current break address without
1069f9c9b22SMark Johnston 		 * modifying any state.  This is an ad-hoc interface
1079f9c9b22SMark Johnston 		 * used by libc to determine the initial break address,
1089f9c9b22SMark Johnston 		 * avoiding a dependency on magic features in the system
1099f9c9b22SMark Johnston 		 * linker.
110f0e2953eSJohn Dyson 		 */
1119f9c9b22SMark Johnston 		new = old;
1120cddd8f0SMatthew Dillon 		goto done;
113f0e2953eSJohn Dyson 	}
1149f9c9b22SMark Johnston 
115f0e2953eSJohn Dyson 	if (new > old) {
116cde4a725SAndrey Zonov 		if (!old_mlock && map->flags & MAP_WIREFUTURE) {
117cde4a725SAndrey Zonov 			if (ptoa(pmap_wired_count(map->pmap)) +
1187e19eda4SAndrey Zonov 			    (new - old) > lmemlim) {
1197e19eda4SAndrey Zonov 				error = ENOMEM;
1207e19eda4SAndrey Zonov 				goto done;
1217e19eda4SAndrey Zonov 			}
1227e19eda4SAndrey Zonov 		}
123cde4a725SAndrey Zonov 		if (map->size + (new - old) > vmemlim) {
124070f64feSMatthew Dillon 			error = ENOMEM;
125070f64feSMatthew Dillon 			goto done;
126070f64feSMatthew Dillon 		}
127afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1284b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
1291ba5ad42SEdward Tomasz Napierala 			PROC_LOCK(td->td_proc);
1301ba5ad42SEdward Tomasz Napierala 			error = racct_set(td->td_proc, RACCT_DATA, new - base);
1311ba5ad42SEdward Tomasz Napierala 			if (error != 0) {
1321ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
1331ba5ad42SEdward Tomasz Napierala 				error = ENOMEM;
1341ba5ad42SEdward Tomasz Napierala 				goto done;
1351ba5ad42SEdward Tomasz Napierala 			}
1361ba5ad42SEdward Tomasz Napierala 			error = racct_set(td->td_proc, RACCT_VMEM,
137cde4a725SAndrey Zonov 			    map->size + (new - old));
1381ba5ad42SEdward Tomasz Napierala 			if (error != 0) {
1394b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc, RACCT_DATA,
1404b5c9cf6SEdward Tomasz Napierala 				    old - base);
1411ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
1421ba5ad42SEdward Tomasz Napierala 				error = ENOMEM;
1431ba5ad42SEdward Tomasz Napierala 				goto done;
1441ba5ad42SEdward Tomasz Napierala 			}
145cde4a725SAndrey Zonov 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1467e19eda4SAndrey Zonov 				error = racct_set(td->td_proc, RACCT_MEMLOCK,
1474b5c9cf6SEdward Tomasz Napierala 				    ptoa(pmap_wired_count(map->pmap)) +
1484b5c9cf6SEdward Tomasz Napierala 				    (new - old));
1497e19eda4SAndrey Zonov 				if (error != 0) {
1507e19eda4SAndrey Zonov 					racct_set_force(td->td_proc, RACCT_DATA,
1517e19eda4SAndrey Zonov 					    old - base);
1527e19eda4SAndrey Zonov 					racct_set_force(td->td_proc, RACCT_VMEM,
153cde4a725SAndrey Zonov 					    map->size);
1547e19eda4SAndrey Zonov 					PROC_UNLOCK(td->td_proc);
1557e19eda4SAndrey Zonov 					error = ENOMEM;
1567e19eda4SAndrey Zonov 					goto done;
1577e19eda4SAndrey Zonov 				}
1587e19eda4SAndrey Zonov 			}
1591ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
1604b5c9cf6SEdward Tomasz Napierala 		}
161afcc55f3SEdward Tomasz Napierala #endif
1625f816602SMarcel Moolenaar 		prot = VM_PROT_RW;
1635f816602SMarcel Moolenaar #ifdef COMPAT_FREEBSD32
164e7d939bdSMarcel Moolenaar #if defined(__amd64__)
165126b36a2SKonstantin Belousov 		if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32))
1665f816602SMarcel Moolenaar 			prot |= VM_PROT_EXECUTE;
1675f816602SMarcel Moolenaar #endif
1685f816602SMarcel Moolenaar #endif
169cde4a725SAndrey Zonov 		rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL, 0);
170df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
171afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1724b5c9cf6SEdward Tomasz Napierala 			if (racct_enable) {
1731ba5ad42SEdward Tomasz Napierala 				PROC_LOCK(td->td_proc);
1744b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc,
1754b5c9cf6SEdward Tomasz Napierala 				    RACCT_DATA, old - base);
1764b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc,
1774b5c9cf6SEdward Tomasz Napierala 				    RACCT_VMEM, map->size);
178cde4a725SAndrey Zonov 				if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1794b5c9cf6SEdward Tomasz Napierala 					racct_set_force(td->td_proc,
1804b5c9cf6SEdward Tomasz Napierala 					    RACCT_MEMLOCK,
181cde4a725SAndrey Zonov 					    ptoa(pmap_wired_count(map->pmap)));
1827e19eda4SAndrey Zonov 				}
1831ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
1844b5c9cf6SEdward Tomasz Napierala 			}
185afcc55f3SEdward Tomasz Napierala #endif
1860cddd8f0SMatthew Dillon 			error = ENOMEM;
1870cddd8f0SMatthew Dillon 			goto done;
188df8bae1dSRodney W. Grimes 		}
1895375be18SAlan Cox 		vm->vm_dsize += btoc(new - old);
190abd498aaSBruce M Simpson 		/*
191abd498aaSBruce M Simpson 		 * Handle the MAP_WIREFUTURE case for legacy applications,
192abd498aaSBruce M Simpson 		 * by marking the newly mapped range of pages as wired.
193abd498aaSBruce M Simpson 		 * We are not required to perform a corresponding
194abd498aaSBruce M Simpson 		 * vm_map_unwire() before vm_map_delete() below, as
195abd498aaSBruce M Simpson 		 * it will forcibly unwire the pages in the range.
196abd498aaSBruce M Simpson 		 *
197abd498aaSBruce M Simpson 		 * XXX If the pages cannot be wired, no error is returned.
198abd498aaSBruce M Simpson 		 */
199*9da5364eSBrooks Davis 		if ((map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
200abd498aaSBruce M Simpson 			do_map_wirefuture = TRUE;
201f0e2953eSJohn Dyson 	} else if (new < old) {
202cde4a725SAndrey Zonov 		rv = vm_map_delete(map, new, old);
203df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
2040cddd8f0SMatthew Dillon 			error = ENOMEM;
2050cddd8f0SMatthew Dillon 			goto done;
206df8bae1dSRodney W. Grimes 		}
207f0e2953eSJohn Dyson 		vm->vm_dsize -= btoc(old - new);
208afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2094b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
2101ba5ad42SEdward Tomasz Napierala 			PROC_LOCK(td->td_proc);
2111ba5ad42SEdward Tomasz Napierala 			racct_set_force(td->td_proc, RACCT_DATA, new - base);
212cde4a725SAndrey Zonov 			racct_set_force(td->td_proc, RACCT_VMEM, map->size);
213cde4a725SAndrey Zonov 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
2147e19eda4SAndrey Zonov 				racct_set_force(td->td_proc, RACCT_MEMLOCK,
215cde4a725SAndrey Zonov 				    ptoa(pmap_wired_count(map->pmap)));
2167e19eda4SAndrey Zonov 			}
2171ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
2184b5c9cf6SEdward Tomasz Napierala 		}
219afcc55f3SEdward Tomasz Napierala #endif
220bb10bb49SJohn Baldwin 	}
2210cddd8f0SMatthew Dillon done:
222cde4a725SAndrey Zonov 	vm_map_unlock(map);
223abd498aaSBruce M Simpson 
224abd498aaSBruce M Simpson 	if (do_map_wirefuture)
225cde4a725SAndrey Zonov 		(void) vm_map_wire(map, old, new,
226abd498aaSBruce M Simpson 		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
227abd498aaSBruce M Simpson 
2289f9c9b22SMark Johnston 	if (error == 0)
2299f9c9b22SMark Johnston 		td->td_retval[0] = new;
2309f9c9b22SMark Johnston 
2310cddd8f0SMatthew Dillon 	return (error);
232758d46cfSBrooks Davis #else /* defined(__aarch64__) || defined(__riscv__) */
233758d46cfSBrooks Davis 	return (ENOSYS);
234758d46cfSBrooks Davis #endif /* defined(__aarch64__) || defined(__riscv__) */
235df8bae1dSRodney W. Grimes }
236df8bae1dSRodney W. Grimes 
2377351a8bdSBrooks Davis #ifdef COMPAT_FREEBSD11
238df8bae1dSRodney W. Grimes int
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