vm_unix.c revision 220373
1/*-
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
35 *
36 *	@(#)vm_unix.c	8.1 (Berkeley) 6/11/93
37 */
38
39/*
40 * Traditional sbrk/grow interface to VM
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/sys/vm/vm_unix.c 220373 2011-04-05 20:23:59Z trasz $");
45
46#include <sys/param.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/proc.h>
50#include <sys/racct.h>
51#include <sys/resourcevar.h>
52#include <sys/sysproto.h>
53#include <sys/systm.h>
54
55#include <vm/vm.h>
56#include <vm/vm_param.h>
57#include <vm/pmap.h>
58#include <vm/vm_map.h>
59
60#ifndef _SYS_SYSPROTO_H_
61struct obreak_args {
62	char *nsize;
63};
64#endif
65
66/*
67 * MPSAFE
68 */
69/* ARGSUSED */
70int
71obreak(td, uap)
72	struct thread *td;
73	struct obreak_args *uap;
74{
75	struct vmspace *vm = td->td_proc->p_vmspace;
76	vm_offset_t new, old, base;
77	rlim_t datalim, vmemlim;
78	int rv;
79	int error = 0;
80	boolean_t do_map_wirefuture;
81
82	PROC_LOCK(td->td_proc);
83	datalim = lim_cur(td->td_proc, RLIMIT_DATA);
84	vmemlim = lim_cur(td->td_proc, RLIMIT_VMEM);
85	PROC_UNLOCK(td->td_proc);
86
87	do_map_wirefuture = FALSE;
88	new = round_page((vm_offset_t)uap->nsize);
89	vm_map_lock(&vm->vm_map);
90
91	base = round_page((vm_offset_t) vm->vm_daddr);
92	old = base + ctob(vm->vm_dsize);
93	if (new > base) {
94		/*
95		 * Check the resource limit, but allow a process to reduce
96		 * its usage, even if it remains over the limit.
97		 */
98		if (new - base > datalim && new > old) {
99			error = ENOMEM;
100			goto done;
101		}
102		if (new > vm_map_max(&vm->vm_map)) {
103			error = ENOMEM;
104			goto done;
105		}
106	} else if (new < base) {
107		/*
108		 * This is simply an invalid value.  If someone wants to
109		 * do fancy address space manipulations, mmap and munmap
110		 * can do most of what the user would want.
111		 */
112		error = EINVAL;
113		goto done;
114	}
115	if (new > old) {
116		if (vm->vm_map.size + (new - old) > vmemlim) {
117			error = ENOMEM;
118			goto done;
119		}
120		PROC_LOCK(td->td_proc);
121		error = racct_set(td->td_proc, RACCT_DATA, new - base);
122		if (error != 0) {
123			PROC_UNLOCK(td->td_proc);
124			error = ENOMEM;
125			goto done;
126		}
127		error = racct_set(td->td_proc, RACCT_VMEM,
128		    vm->vm_map.size + (new - old));
129		if (error != 0) {
130			racct_set_force(td->td_proc, RACCT_DATA, old - base);
131			PROC_UNLOCK(td->td_proc);
132			error = ENOMEM;
133			goto done;
134		}
135		PROC_UNLOCK(td->td_proc);
136		rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new,
137		    VM_PROT_RW, VM_PROT_ALL, 0);
138		if (rv != KERN_SUCCESS) {
139			PROC_LOCK(td->td_proc);
140			racct_set_force(td->td_proc, RACCT_DATA, old - base);
141			racct_set_force(td->td_proc, RACCT_VMEM, vm->vm_map.size);
142			PROC_UNLOCK(td->td_proc);
143			error = ENOMEM;
144			goto done;
145		}
146		vm->vm_dsize += btoc(new - old);
147		/*
148		 * Handle the MAP_WIREFUTURE case for legacy applications,
149		 * by marking the newly mapped range of pages as wired.
150		 * We are not required to perform a corresponding
151		 * vm_map_unwire() before vm_map_delete() below, as
152		 * it will forcibly unwire the pages in the range.
153		 *
154		 * XXX If the pages cannot be wired, no error is returned.
155		 */
156		if ((vm->vm_map.flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) {
157			if (bootverbose)
158				printf("obreak: MAP_WIREFUTURE set\n");
159			do_map_wirefuture = TRUE;
160		}
161	} else if (new < old) {
162		rv = vm_map_delete(&vm->vm_map, new, old);
163		if (rv != KERN_SUCCESS) {
164			error = ENOMEM;
165			goto done;
166		}
167		vm->vm_dsize -= btoc(old - new);
168		PROC_LOCK(td->td_proc);
169		racct_set_force(td->td_proc, RACCT_DATA, new - base);
170		racct_set_force(td->td_proc, RACCT_VMEM, vm->vm_map.size);
171		PROC_UNLOCK(td->td_proc);
172	}
173done:
174	vm_map_unlock(&vm->vm_map);
175
176	if (do_map_wirefuture)
177		(void) vm_map_wire(&vm->vm_map, old, new,
178		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
179
180	return (error);
181}
182
183#ifndef _SYS_SYSPROTO_H_
184struct ovadvise_args {
185	int anom;
186};
187#endif
188
189/*
190 * MPSAFE
191 */
192/* ARGSUSED */
193int
194ovadvise(td, uap)
195	struct thread *td;
196	struct ovadvise_args *uap;
197{
198	/* START_GIANT_OPTIONAL */
199	/* END_GIANT_OPTIONAL */
200	return (EINVAL);
201}
202