xref: /freebsd/sys/vm/vm_unix.c (revision e8a1ec3e059000c939e42cc3c22036fad0474ac5)
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>
54*e8a1ec3eSEd Maste #include <sys/syscallsubr.h>
555f816602SMarcel Moolenaar #include <sys/sysent.h>
5686e92ee7SJohn Baldwin #include <sys/sysproto.h>
5723955314SAlfred Perlstein #include <sys/systm.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <vm/vm.h>
60efeaf95aSDavid Greenman #include <vm/vm_param.h>
61efeaf95aSDavid Greenman #include <vm/pmap.h>
62efeaf95aSDavid Greenman #include <vm/vm_map.h>
6326f9a767SRodney W. Grimes 
64d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
659da5364eSBrooks Davis struct break_args {
669ed1bde7SBruce Evans 	char *nsize;
67df8bae1dSRodney W. Grimes };
68d2d3e875SBruce Evans #endif
69df8bae1dSRodney W. Grimes int
709da5364eSBrooks Davis sys_break(struct thread *td, struct break_args *uap)
71df8bae1dSRodney W. Grimes {
72758d46cfSBrooks Davis #if !defined(__aarch64__) && !defined(__riscv__)
73*e8a1ec3eSEd Maste 	uintptr_t addr;
74*e8a1ec3eSEd Maste 	int error;
75*e8a1ec3eSEd Maste 
76*e8a1ec3eSEd Maste 	addr = (uintptr_t)uap->nsize;
77*e8a1ec3eSEd Maste 	error = kern_break(td, &addr);
78*e8a1ec3eSEd Maste 	if (error == 0)
79*e8a1ec3eSEd Maste 		td->td_retval[0] = addr;
80*e8a1ec3eSEd Maste 	return (error);
81*e8a1ec3eSEd Maste #else /* defined(__aarch64__) || defined(__riscv__) */
82*e8a1ec3eSEd Maste 	return (ENOSYS);
83*e8a1ec3eSEd Maste #endif /* defined(__aarch64__) || defined(__riscv__) */
84*e8a1ec3eSEd Maste }
85*e8a1ec3eSEd Maste 
86*e8a1ec3eSEd Maste int
87*e8a1ec3eSEd Maste kern_break(struct thread *td, uintptr_t *addr)
88*e8a1ec3eSEd Maste {
89b40ce416SJulian Elischer 	struct vmspace *vm = td->td_proc->p_vmspace;
90cde4a725SAndrey Zonov 	vm_map_t map = &vm->vm_map;
91f0e2953eSJohn Dyson 	vm_offset_t new, old, base;
927e19eda4SAndrey Zonov 	rlim_t datalim, lmemlim, vmemlim;
935f816602SMarcel Moolenaar 	int prot, rv;
940cddd8f0SMatthew Dillon 	int error = 0;
95abd498aaSBruce M Simpson 	boolean_t do_map_wirefuture;
960cddd8f0SMatthew Dillon 
97f6f6d240SMateusz Guzik 	datalim = lim_cur(td, RLIMIT_DATA);
98f6f6d240SMateusz Guzik 	lmemlim = lim_cur(td, RLIMIT_MEMLOCK);
99f6f6d240SMateusz Guzik 	vmemlim = lim_cur(td, RLIMIT_VMEM);
10091d5354aSJohn Baldwin 
101abd498aaSBruce M Simpson 	do_map_wirefuture = FALSE;
102*e8a1ec3eSEd Maste 	new = round_page(*addr);
103cde4a725SAndrey Zonov 	vm_map_lock(map);
104df8bae1dSRodney W. Grimes 
105f0e2953eSJohn Dyson 	base = round_page((vm_offset_t) vm->vm_daddr);
106a77c9610SDavid Malone 	old = base + ctob(vm->vm_dsize);
107f0e2953eSJohn Dyson 	if (new > base) {
108a77c9610SDavid Malone 		/*
1093d66f138SAlan Cox 		 * Check the resource limit, but allow a process to reduce
1103d66f138SAlan Cox 		 * its usage, even if it remains over the limit.
111a77c9610SDavid Malone 		 */
11291d5354aSJohn Baldwin 		if (new - base > datalim && new > old) {
1130cddd8f0SMatthew Dillon 			error = ENOMEM;
1140cddd8f0SMatthew Dillon 			goto done;
1150cddd8f0SMatthew Dillon 		}
116cde4a725SAndrey Zonov 		if (new > vm_map_max(map)) {
1170cddd8f0SMatthew Dillon 			error = ENOMEM;
1180cddd8f0SMatthew Dillon 			goto done;
1190cddd8f0SMatthew Dillon 		}
120f0e2953eSJohn Dyson 	} else if (new < base) {
121f0e2953eSJohn Dyson 		/*
1229f9c9b22SMark Johnston 		 * Simply return the current break address without
1239f9c9b22SMark Johnston 		 * modifying any state.  This is an ad-hoc interface
1249f9c9b22SMark Johnston 		 * used by libc to determine the initial break address,
1259f9c9b22SMark Johnston 		 * avoiding a dependency on magic features in the system
1269f9c9b22SMark Johnston 		 * linker.
127f0e2953eSJohn Dyson 		 */
1289f9c9b22SMark Johnston 		new = old;
1290cddd8f0SMatthew Dillon 		goto done;
130f0e2953eSJohn Dyson 	}
1319f9c9b22SMark Johnston 
132f0e2953eSJohn Dyson 	if (new > old) {
133cde4a725SAndrey Zonov 		if (!old_mlock && map->flags & MAP_WIREFUTURE) {
134cde4a725SAndrey Zonov 			if (ptoa(pmap_wired_count(map->pmap)) +
1357e19eda4SAndrey Zonov 			    (new - old) > lmemlim) {
1367e19eda4SAndrey Zonov 				error = ENOMEM;
1377e19eda4SAndrey Zonov 				goto done;
1387e19eda4SAndrey Zonov 			}
1397e19eda4SAndrey Zonov 		}
140cde4a725SAndrey Zonov 		if (map->size + (new - old) > vmemlim) {
141070f64feSMatthew Dillon 			error = ENOMEM;
142070f64feSMatthew Dillon 			goto done;
143070f64feSMatthew Dillon 		}
144afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1454b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
1461ba5ad42SEdward Tomasz Napierala 			PROC_LOCK(td->td_proc);
1471ba5ad42SEdward Tomasz Napierala 			error = racct_set(td->td_proc, RACCT_DATA, new - base);
1481ba5ad42SEdward Tomasz Napierala 			if (error != 0) {
1491ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
1501ba5ad42SEdward Tomasz Napierala 				error = ENOMEM;
1511ba5ad42SEdward Tomasz Napierala 				goto done;
1521ba5ad42SEdward Tomasz Napierala 			}
1531ba5ad42SEdward Tomasz Napierala 			error = racct_set(td->td_proc, RACCT_VMEM,
154cde4a725SAndrey Zonov 			    map->size + (new - old));
1551ba5ad42SEdward Tomasz Napierala 			if (error != 0) {
1564b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc, RACCT_DATA,
1574b5c9cf6SEdward Tomasz Napierala 				    old - base);
1581ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
1591ba5ad42SEdward Tomasz Napierala 				error = ENOMEM;
1601ba5ad42SEdward Tomasz Napierala 				goto done;
1611ba5ad42SEdward Tomasz Napierala 			}
162cde4a725SAndrey Zonov 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1637e19eda4SAndrey Zonov 				error = racct_set(td->td_proc, RACCT_MEMLOCK,
1644b5c9cf6SEdward Tomasz Napierala 				    ptoa(pmap_wired_count(map->pmap)) +
1654b5c9cf6SEdward Tomasz Napierala 				    (new - old));
1667e19eda4SAndrey Zonov 				if (error != 0) {
1677e19eda4SAndrey Zonov 					racct_set_force(td->td_proc, RACCT_DATA,
1687e19eda4SAndrey Zonov 					    old - base);
1697e19eda4SAndrey Zonov 					racct_set_force(td->td_proc, RACCT_VMEM,
170cde4a725SAndrey Zonov 					    map->size);
1717e19eda4SAndrey Zonov 					PROC_UNLOCK(td->td_proc);
1727e19eda4SAndrey Zonov 					error = ENOMEM;
1737e19eda4SAndrey Zonov 					goto done;
1747e19eda4SAndrey Zonov 				}
1757e19eda4SAndrey Zonov 			}
1761ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
1774b5c9cf6SEdward Tomasz Napierala 		}
178afcc55f3SEdward Tomasz Napierala #endif
1795f816602SMarcel Moolenaar 		prot = VM_PROT_RW;
1805f816602SMarcel Moolenaar #ifdef COMPAT_FREEBSD32
181e7d939bdSMarcel Moolenaar #if defined(__amd64__)
182126b36a2SKonstantin Belousov 		if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32))
1835f816602SMarcel Moolenaar 			prot |= VM_PROT_EXECUTE;
1845f816602SMarcel Moolenaar #endif
1855f816602SMarcel Moolenaar #endif
186cde4a725SAndrey Zonov 		rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL, 0);
187df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
188afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1894b5c9cf6SEdward Tomasz Napierala 			if (racct_enable) {
1901ba5ad42SEdward Tomasz Napierala 				PROC_LOCK(td->td_proc);
1914b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc,
1924b5c9cf6SEdward Tomasz Napierala 				    RACCT_DATA, old - base);
1934b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc,
1944b5c9cf6SEdward Tomasz Napierala 				    RACCT_VMEM, map->size);
195cde4a725SAndrey Zonov 				if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1964b5c9cf6SEdward Tomasz Napierala 					racct_set_force(td->td_proc,
1974b5c9cf6SEdward Tomasz Napierala 					    RACCT_MEMLOCK,
198cde4a725SAndrey Zonov 					    ptoa(pmap_wired_count(map->pmap)));
1997e19eda4SAndrey Zonov 				}
2001ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
2014b5c9cf6SEdward Tomasz Napierala 			}
202afcc55f3SEdward Tomasz Napierala #endif
2030cddd8f0SMatthew Dillon 			error = ENOMEM;
2040cddd8f0SMatthew Dillon 			goto done;
205df8bae1dSRodney W. Grimes 		}
2065375be18SAlan Cox 		vm->vm_dsize += btoc(new - old);
207abd498aaSBruce M Simpson 		/*
208abd498aaSBruce M Simpson 		 * Handle the MAP_WIREFUTURE case for legacy applications,
209abd498aaSBruce M Simpson 		 * by marking the newly mapped range of pages as wired.
210abd498aaSBruce M Simpson 		 * We are not required to perform a corresponding
211abd498aaSBruce M Simpson 		 * vm_map_unwire() before vm_map_delete() below, as
212abd498aaSBruce M Simpson 		 * it will forcibly unwire the pages in the range.
213abd498aaSBruce M Simpson 		 *
214abd498aaSBruce M Simpson 		 * XXX If the pages cannot be wired, no error is returned.
215abd498aaSBruce M Simpson 		 */
2169da5364eSBrooks Davis 		if ((map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
217abd498aaSBruce M Simpson 			do_map_wirefuture = TRUE;
218f0e2953eSJohn Dyson 	} else if (new < old) {
219cde4a725SAndrey Zonov 		rv = vm_map_delete(map, new, old);
220df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
2210cddd8f0SMatthew Dillon 			error = ENOMEM;
2220cddd8f0SMatthew Dillon 			goto done;
223df8bae1dSRodney W. Grimes 		}
224f0e2953eSJohn Dyson 		vm->vm_dsize -= btoc(old - new);
225afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2264b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
2271ba5ad42SEdward Tomasz Napierala 			PROC_LOCK(td->td_proc);
2281ba5ad42SEdward Tomasz Napierala 			racct_set_force(td->td_proc, RACCT_DATA, new - base);
229cde4a725SAndrey Zonov 			racct_set_force(td->td_proc, RACCT_VMEM, map->size);
230cde4a725SAndrey Zonov 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
2317e19eda4SAndrey Zonov 				racct_set_force(td->td_proc, RACCT_MEMLOCK,
232cde4a725SAndrey Zonov 				    ptoa(pmap_wired_count(map->pmap)));
2337e19eda4SAndrey Zonov 			}
2341ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
2354b5c9cf6SEdward Tomasz Napierala 		}
236afcc55f3SEdward Tomasz Napierala #endif
237bb10bb49SJohn Baldwin 	}
2380cddd8f0SMatthew Dillon done:
239cde4a725SAndrey Zonov 	vm_map_unlock(map);
240abd498aaSBruce M Simpson 
241abd498aaSBruce M Simpson 	if (do_map_wirefuture)
242cde4a725SAndrey Zonov 		(void) vm_map_wire(map, old, new,
243abd498aaSBruce M Simpson 		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
244abd498aaSBruce M Simpson 
2459f9c9b22SMark Johnston 	if (error == 0)
246*e8a1ec3eSEd Maste 		*addr = new;
2479f9c9b22SMark Johnston 
2480cddd8f0SMatthew Dillon 	return (error);
249df8bae1dSRodney W. Grimes }
250df8bae1dSRodney W. Grimes 
2517351a8bdSBrooks Davis #ifdef COMPAT_FREEBSD11
252df8bae1dSRodney W. Grimes int
2537351a8bdSBrooks Davis freebsd11_vadvise(struct thread *td, struct freebsd11_vadvise_args *uap)
254df8bae1dSRodney W. Grimes {
2557351a8bdSBrooks Davis 
256df8bae1dSRodney W. Grimes 	return (EINVAL);
257df8bae1dSRodney W. Grimes }
2587351a8bdSBrooks Davis #endif
259