xref: /freebsd/sys/vm/vm_unix.c (revision 5f81660285be720d5396988e5e5a97a826c812f9)
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.
18df8bae1dSRodney W. Grimes  * 4. 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 
39*5f816602SMarcel Moolenaar #include "opt_compat.h"
40*5f816602SMarcel 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>
54*5f816602SMarcel 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;
79f0e2953eSJohn Dyson 	vm_offset_t new, old, base;
8091d5354aSJohn Baldwin 	rlim_t datalim, vmemlim;
81*5f816602SMarcel Moolenaar 	int prot, rv;
820cddd8f0SMatthew Dillon 	int error = 0;
83abd498aaSBruce M Simpson 	boolean_t do_map_wirefuture;
840cddd8f0SMatthew Dillon 
8591d5354aSJohn Baldwin 	PROC_LOCK(td->td_proc);
8691d5354aSJohn Baldwin 	datalim = lim_cur(td->td_proc, RLIMIT_DATA);
8791d5354aSJohn Baldwin 	vmemlim = lim_cur(td->td_proc, RLIMIT_VMEM);
8891d5354aSJohn Baldwin 	PROC_UNLOCK(td->td_proc);
8991d5354aSJohn Baldwin 
90abd498aaSBruce M Simpson 	do_map_wirefuture = FALSE;
915375be18SAlan Cox 	new = round_page((vm_offset_t)uap->nsize);
925375be18SAlan Cox 	vm_map_lock(&vm->vm_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 		}
10505ba50f5SJake Burkholder 		if (new > vm_map_max(&vm->vm_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) {
11991d5354aSJohn Baldwin 		if (vm->vm_map.size + (new - old) > vmemlim) {
120070f64feSMatthew Dillon 			error = ENOMEM;
121070f64feSMatthew Dillon 			goto done;
122070f64feSMatthew Dillon 		}
123afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1241ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1251ba5ad42SEdward Tomasz Napierala 		error = racct_set(td->td_proc, RACCT_DATA, new - base);
1261ba5ad42SEdward Tomasz Napierala 		if (error != 0) {
1271ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
1281ba5ad42SEdward Tomasz Napierala 			error = ENOMEM;
1291ba5ad42SEdward Tomasz Napierala 			goto done;
1301ba5ad42SEdward Tomasz Napierala 		}
1311ba5ad42SEdward Tomasz Napierala 		error = racct_set(td->td_proc, RACCT_VMEM,
1321ba5ad42SEdward Tomasz Napierala 		    vm->vm_map.size + (new - old));
1331ba5ad42SEdward Tomasz Napierala 		if (error != 0) {
1341ba5ad42SEdward Tomasz Napierala 			racct_set_force(td->td_proc, RACCT_DATA, old - base);
1351ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
1361ba5ad42SEdward Tomasz Napierala 			error = ENOMEM;
1371ba5ad42SEdward Tomasz Napierala 			goto done;
1381ba5ad42SEdward Tomasz Napierala 		}
1391ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
140afcc55f3SEdward Tomasz Napierala #endif
141*5f816602SMarcel Moolenaar 		prot = VM_PROT_RW;
142*5f816602SMarcel Moolenaar #ifdef COMPAT_FREEBSD32
143*5f816602SMarcel Moolenaar #if defined(__amd64__) || defined(__ia64__)
144*5f816602SMarcel Moolenaar 		if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
145*5f816602SMarcel Moolenaar 			prot |= VM_PROT_EXECUTE;
146*5f816602SMarcel Moolenaar #endif
147*5f816602SMarcel Moolenaar #endif
1485375be18SAlan Cox 		rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new,
149*5f816602SMarcel Moolenaar 		    prot, VM_PROT_ALL, 0);
150df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
151afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1521ba5ad42SEdward Tomasz Napierala 			PROC_LOCK(td->td_proc);
1531ba5ad42SEdward Tomasz Napierala 			racct_set_force(td->td_proc, RACCT_DATA, old - base);
1541ba5ad42SEdward Tomasz Napierala 			racct_set_force(td->td_proc, RACCT_VMEM, vm->vm_map.size);
1551ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(td->td_proc);
156afcc55f3SEdward Tomasz Napierala #endif
1570cddd8f0SMatthew Dillon 			error = ENOMEM;
1580cddd8f0SMatthew Dillon 			goto done;
159df8bae1dSRodney W. Grimes 		}
1605375be18SAlan Cox 		vm->vm_dsize += btoc(new - old);
161abd498aaSBruce M Simpson 		/*
162abd498aaSBruce M Simpson 		 * Handle the MAP_WIREFUTURE case for legacy applications,
163abd498aaSBruce M Simpson 		 * by marking the newly mapped range of pages as wired.
164abd498aaSBruce M Simpson 		 * We are not required to perform a corresponding
165abd498aaSBruce M Simpson 		 * vm_map_unwire() before vm_map_delete() below, as
166abd498aaSBruce M Simpson 		 * it will forcibly unwire the pages in the range.
167abd498aaSBruce M Simpson 		 *
168abd498aaSBruce M Simpson 		 * XXX If the pages cannot be wired, no error is returned.
169abd498aaSBruce M Simpson 		 */
170abd498aaSBruce M Simpson 		if ((vm->vm_map.flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) {
171abd498aaSBruce M Simpson 			if (bootverbose)
172abd498aaSBruce M Simpson 				printf("obreak: MAP_WIREFUTURE set\n");
173abd498aaSBruce M Simpson 			do_map_wirefuture = TRUE;
174abd498aaSBruce M Simpson 		}
175f0e2953eSJohn Dyson 	} else if (new < old) {
176655c3490SKonstantin Belousov 		rv = vm_map_delete(&vm->vm_map, new, old);
177df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
1780cddd8f0SMatthew Dillon 			error = ENOMEM;
1790cddd8f0SMatthew Dillon 			goto done;
180df8bae1dSRodney W. Grimes 		}
181f0e2953eSJohn Dyson 		vm->vm_dsize -= btoc(old - new);
182afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1831ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1841ba5ad42SEdward Tomasz Napierala 		racct_set_force(td->td_proc, RACCT_DATA, new - base);
1851ba5ad42SEdward Tomasz Napierala 		racct_set_force(td->td_proc, RACCT_VMEM, vm->vm_map.size);
1861ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
187afcc55f3SEdward Tomasz Napierala #endif
188bb10bb49SJohn Baldwin 	}
1890cddd8f0SMatthew Dillon done:
1905375be18SAlan Cox 	vm_map_unlock(&vm->vm_map);
191abd498aaSBruce M Simpson 
192abd498aaSBruce M Simpson 	if (do_map_wirefuture)
193abd498aaSBruce M Simpson 		(void) vm_map_wire(&vm->vm_map, old, new,
194abd498aaSBruce M Simpson 		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
195abd498aaSBruce M Simpson 
1960cddd8f0SMatthew Dillon 	return (error);
197df8bae1dSRodney W. Grimes }
198df8bae1dSRodney W. Grimes 
199d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
200df8bae1dSRodney W. Grimes struct ovadvise_args {
201df8bae1dSRodney W. Grimes 	int anom;
202df8bae1dSRodney W. Grimes };
203d2d3e875SBruce Evans #endif
20426f9a767SRodney W. Grimes 
2056a33d53cSMatthew Dillon /*
2066a33d53cSMatthew Dillon  * MPSAFE
2076a33d53cSMatthew Dillon  */
208df8bae1dSRodney W. Grimes /* ARGSUSED */
209df8bae1dSRodney W. Grimes int
2108451d0ddSKip Macy sys_ovadvise(td, uap)
211b40ce416SJulian Elischer 	struct thread *td;
212df8bae1dSRodney W. Grimes 	struct ovadvise_args *uap;
213df8bae1dSRodney W. Grimes {
2140cddd8f0SMatthew Dillon 	/* START_GIANT_OPTIONAL */
2150cddd8f0SMatthew Dillon 	/* END_GIANT_OPTIONAL */
216df8bae1dSRodney W. Grimes 	return (EINVAL);
217df8bae1dSRodney W. Grimes }
218