xref: /freebsd/sys/vm/vm_unix.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
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.
18*fbbd9655SWarner Losh  * 3. 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;
79cde4a725SAndrey 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 
86f6f6d240SMateusz Guzik 	datalim = lim_cur(td, RLIMIT_DATA);
87f6f6d240SMateusz Guzik 	lmemlim = lim_cur(td, RLIMIT_MEMLOCK);
88f6f6d240SMateusz Guzik 	vmemlim = lim_cur(td, RLIMIT_VMEM);
8991d5354aSJohn Baldwin 
90abd498aaSBruce M Simpson 	do_map_wirefuture = FALSE;
915375be18SAlan Cox 	new = round_page((vm_offset_t)uap->nsize);
92cde4a725SAndrey Zonov 	vm_map_lock(map);
93df8bae1dSRodney W. Grimes 
94f0e2953eSJohn Dyson 	base = round_page((vm_offset_t) vm->vm_daddr);
95a77c9610SDavid Malone 	old = base + ctob(vm->vm_dsize);
96f0e2953eSJohn Dyson 	if (new > base) {
97a77c9610SDavid Malone 		/*
983d66f138SAlan Cox 		 * Check the resource limit, but allow a process to reduce
993d66f138SAlan Cox 		 * its usage, even if it remains over the limit.
100a77c9610SDavid Malone 		 */
10191d5354aSJohn Baldwin 		if (new - base > datalim && new > old) {
1020cddd8f0SMatthew Dillon 			error = ENOMEM;
1030cddd8f0SMatthew Dillon 			goto done;
1040cddd8f0SMatthew Dillon 		}
105cde4a725SAndrey Zonov 		if (new > vm_map_max(map)) {
1060cddd8f0SMatthew Dillon 			error = ENOMEM;
1070cddd8f0SMatthew Dillon 			goto done;
1080cddd8f0SMatthew Dillon 		}
109f0e2953eSJohn Dyson 	} else if (new < base) {
110f0e2953eSJohn Dyson 		/*
111f0e2953eSJohn Dyson 		 * This is simply an invalid value.  If someone wants to
112f0e2953eSJohn Dyson 		 * do fancy address space manipulations, mmap and munmap
113f0e2953eSJohn Dyson 		 * can do most of what the user would want.
114f0e2953eSJohn Dyson 		 */
1150cddd8f0SMatthew Dillon 		error = EINVAL;
1160cddd8f0SMatthew Dillon 		goto done;
117f0e2953eSJohn Dyson 	}
118f0e2953eSJohn Dyson 	if (new > old) {
119cde4a725SAndrey Zonov 		if (!old_mlock && map->flags & MAP_WIREFUTURE) {
120cde4a725SAndrey Zonov 			if (ptoa(pmap_wired_count(map->pmap)) +
1217e19eda4SAndrey Zonov 			    (new - old) > lmemlim) {
1227e19eda4SAndrey Zonov 				error = ENOMEM;
1237e19eda4SAndrey Zonov 				goto done;
1247e19eda4SAndrey Zonov 			}
1257e19eda4SAndrey Zonov 		}
126cde4a725SAndrey Zonov 		if (map->size + (new - old) > vmemlim) {
127070f64feSMatthew Dillon 			error = ENOMEM;
128070f64feSMatthew Dillon 			goto done;
129070f64feSMatthew Dillon 		}
130afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1314b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
1321ba5ad42SEdward Tomasz Napierala 			PROC_LOCK(td->td_proc);
1331ba5ad42SEdward Tomasz Napierala 			error = racct_set(td->td_proc, RACCT_DATA, new - base);
1341ba5ad42SEdward Tomasz Napierala 			if (error != 0) {
1351ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
1361ba5ad42SEdward Tomasz Napierala 				error = ENOMEM;
1371ba5ad42SEdward Tomasz Napierala 				goto done;
1381ba5ad42SEdward Tomasz Napierala 			}
1391ba5ad42SEdward Tomasz Napierala 			error = racct_set(td->td_proc, RACCT_VMEM,
140cde4a725SAndrey Zonov 			    map->size + (new - old));
1411ba5ad42SEdward Tomasz Napierala 			if (error != 0) {
1424b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc, RACCT_DATA,
1434b5c9cf6SEdward Tomasz Napierala 				    old - base);
1441ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
1451ba5ad42SEdward Tomasz Napierala 				error = ENOMEM;
1461ba5ad42SEdward Tomasz Napierala 				goto done;
1471ba5ad42SEdward Tomasz Napierala 			}
148cde4a725SAndrey Zonov 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1497e19eda4SAndrey Zonov 				error = racct_set(td->td_proc, RACCT_MEMLOCK,
1504b5c9cf6SEdward Tomasz Napierala 				    ptoa(pmap_wired_count(map->pmap)) +
1514b5c9cf6SEdward Tomasz Napierala 				    (new - old));
1527e19eda4SAndrey Zonov 				if (error != 0) {
1537e19eda4SAndrey Zonov 					racct_set_force(td->td_proc, RACCT_DATA,
1547e19eda4SAndrey Zonov 					    old - base);
1557e19eda4SAndrey Zonov 					racct_set_force(td->td_proc, RACCT_VMEM,
156cde4a725SAndrey Zonov 					    map->size);
1577e19eda4SAndrey Zonov 					PROC_UNLOCK(td->td_proc);
1587e19eda4SAndrey Zonov 					error = ENOMEM;
1597e19eda4SAndrey Zonov 					goto done;
1607e19eda4SAndrey Zonov 				}
1617e19eda4SAndrey Zonov 			}
1621ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
1634b5c9cf6SEdward Tomasz Napierala 		}
164afcc55f3SEdward Tomasz Napierala #endif
1655f816602SMarcel Moolenaar 		prot = VM_PROT_RW;
1665f816602SMarcel Moolenaar #ifdef COMPAT_FREEBSD32
167e7d939bdSMarcel Moolenaar #if defined(__amd64__)
168126b36a2SKonstantin Belousov 		if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32))
1695f816602SMarcel Moolenaar 			prot |= VM_PROT_EXECUTE;
1705f816602SMarcel Moolenaar #endif
1715f816602SMarcel Moolenaar #endif
172cde4a725SAndrey Zonov 		rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL, 0);
173df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
174afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1754b5c9cf6SEdward Tomasz Napierala 			if (racct_enable) {
1761ba5ad42SEdward Tomasz Napierala 				PROC_LOCK(td->td_proc);
1774b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc,
1784b5c9cf6SEdward Tomasz Napierala 				    RACCT_DATA, old - base);
1794b5c9cf6SEdward Tomasz Napierala 				racct_set_force(td->td_proc,
1804b5c9cf6SEdward Tomasz Napierala 				    RACCT_VMEM, map->size);
181cde4a725SAndrey Zonov 				if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1824b5c9cf6SEdward Tomasz Napierala 					racct_set_force(td->td_proc,
1834b5c9cf6SEdward Tomasz Napierala 					    RACCT_MEMLOCK,
184cde4a725SAndrey Zonov 					    ptoa(pmap_wired_count(map->pmap)));
1857e19eda4SAndrey Zonov 				}
1861ba5ad42SEdward Tomasz Napierala 				PROC_UNLOCK(td->td_proc);
1874b5c9cf6SEdward Tomasz Napierala 			}
188afcc55f3SEdward Tomasz Napierala #endif
1890cddd8f0SMatthew Dillon 			error = ENOMEM;
1900cddd8f0SMatthew Dillon 			goto done;
191df8bae1dSRodney W. Grimes 		}
1925375be18SAlan Cox 		vm->vm_dsize += btoc(new - old);
193abd498aaSBruce M Simpson 		/*
194abd498aaSBruce M Simpson 		 * Handle the MAP_WIREFUTURE case for legacy applications,
195abd498aaSBruce M Simpson 		 * by marking the newly mapped range of pages as wired.
196abd498aaSBruce M Simpson 		 * We are not required to perform a corresponding
197abd498aaSBruce M Simpson 		 * vm_map_unwire() before vm_map_delete() below, as
198abd498aaSBruce M Simpson 		 * it will forcibly unwire the pages in the range.
199abd498aaSBruce M Simpson 		 *
200abd498aaSBruce M Simpson 		 * XXX If the pages cannot be wired, no error is returned.
201abd498aaSBruce M Simpson 		 */
202cde4a725SAndrey Zonov 		if ((map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) {
203abd498aaSBruce M Simpson 			if (bootverbose)
204abd498aaSBruce M Simpson 				printf("obreak: MAP_WIREFUTURE set\n");
205abd498aaSBruce M Simpson 			do_map_wirefuture = TRUE;
206abd498aaSBruce M Simpson 		}
207f0e2953eSJohn Dyson 	} else if (new < old) {
208cde4a725SAndrey Zonov 		rv = vm_map_delete(map, new, old);
209df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
2100cddd8f0SMatthew Dillon 			error = ENOMEM;
2110cddd8f0SMatthew Dillon 			goto done;
212df8bae1dSRodney W. Grimes 		}
213f0e2953eSJohn Dyson 		vm->vm_dsize -= btoc(old - new);
214afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2154b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
2161ba5ad42SEdward Tomasz Napierala 			PROC_LOCK(td->td_proc);
2171ba5ad42SEdward Tomasz Napierala 			racct_set_force(td->td_proc, RACCT_DATA, new - base);
218cde4a725SAndrey Zonov 			racct_set_force(td->td_proc, RACCT_VMEM, map->size);
219cde4a725SAndrey Zonov 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
2207e19eda4SAndrey Zonov 				racct_set_force(td->td_proc, RACCT_MEMLOCK,
221cde4a725SAndrey Zonov 				    ptoa(pmap_wired_count(map->pmap)));
2227e19eda4SAndrey Zonov 			}
2231ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
2244b5c9cf6SEdward Tomasz Napierala 		}
225afcc55f3SEdward Tomasz Napierala #endif
226bb10bb49SJohn Baldwin 	}
2270cddd8f0SMatthew Dillon done:
228cde4a725SAndrey Zonov 	vm_map_unlock(map);
229abd498aaSBruce M Simpson 
230abd498aaSBruce M Simpson 	if (do_map_wirefuture)
231cde4a725SAndrey Zonov 		(void) vm_map_wire(map, old, new,
232abd498aaSBruce M Simpson 		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
233abd498aaSBruce M Simpson 
2340cddd8f0SMatthew Dillon 	return (error);
235df8bae1dSRodney W. Grimes }
236df8bae1dSRodney W. Grimes 
237d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
238df8bae1dSRodney W. Grimes struct ovadvise_args {
239df8bae1dSRodney W. Grimes 	int anom;
240df8bae1dSRodney W. Grimes };
241d2d3e875SBruce Evans #endif
24226f9a767SRodney W. Grimes 
2436a33d53cSMatthew Dillon /*
2446a33d53cSMatthew Dillon  * MPSAFE
2456a33d53cSMatthew Dillon  */
246df8bae1dSRodney W. Grimes /* ARGSUSED */
247df8bae1dSRodney W. Grimes int
2488451d0ddSKip Macy sys_ovadvise(td, uap)
249b40ce416SJulian Elischer 	struct thread *td;
250df8bae1dSRodney W. Grimes 	struct ovadvise_args *uap;
251df8bae1dSRodney W. Grimes {
2520cddd8f0SMatthew Dillon 	/* START_GIANT_OPTIONAL */
2530cddd8f0SMatthew Dillon 	/* END_GIANT_OPTIONAL */
254df8bae1dSRodney W. Grimes 	return (EINVAL);
255df8bae1dSRodney W. Grimes }
256