vm_unix.c revision 225617
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 225617 2011-09-16 13:58:51Z kmacy $");
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
71sys_obreak(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#ifdef RACCT
121		PROC_LOCK(td->td_proc);
122		error = racct_set(td->td_proc, RACCT_DATA, new - base);
123		if (error != 0) {
124			PROC_UNLOCK(td->td_proc);
125			error = ENOMEM;
126			goto done;
127		}
128		error = racct_set(td->td_proc, RACCT_VMEM,
129		    vm->vm_map.size + (new - old));
130		if (error != 0) {
131			racct_set_force(td->td_proc, RACCT_DATA, old - base);
132			PROC_UNLOCK(td->td_proc);
133			error = ENOMEM;
134			goto done;
135		}
136		PROC_UNLOCK(td->td_proc);
137#endif
138		rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new,
139		    VM_PROT_RW, VM_PROT_ALL, 0);
140		if (rv != KERN_SUCCESS) {
141#ifdef RACCT
142			PROC_LOCK(td->td_proc);
143			racct_set_force(td->td_proc, RACCT_DATA, old - base);
144			racct_set_force(td->td_proc, RACCT_VMEM, vm->vm_map.size);
145			PROC_UNLOCK(td->td_proc);
146#endif
147			error = ENOMEM;
148			goto done;
149		}
150		vm->vm_dsize += btoc(new - old);
151		/*
152		 * Handle the MAP_WIREFUTURE case for legacy applications,
153		 * by marking the newly mapped range of pages as wired.
154		 * We are not required to perform a corresponding
155		 * vm_map_unwire() before vm_map_delete() below, as
156		 * it will forcibly unwire the pages in the range.
157		 *
158		 * XXX If the pages cannot be wired, no error is returned.
159		 */
160		if ((vm->vm_map.flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) {
161			if (bootverbose)
162				printf("obreak: MAP_WIREFUTURE set\n");
163			do_map_wirefuture = TRUE;
164		}
165	} else if (new < old) {
166		rv = vm_map_delete(&vm->vm_map, new, old);
167		if (rv != KERN_SUCCESS) {
168			error = ENOMEM;
169			goto done;
170		}
171		vm->vm_dsize -= btoc(old - new);
172#ifdef RACCT
173		PROC_LOCK(td->td_proc);
174		racct_set_force(td->td_proc, RACCT_DATA, new - base);
175		racct_set_force(td->td_proc, RACCT_VMEM, vm->vm_map.size);
176		PROC_UNLOCK(td->td_proc);
177#endif
178	}
179done:
180	vm_map_unlock(&vm->vm_map);
181
182	if (do_map_wirefuture)
183		(void) vm_map_wire(&vm->vm_map, old, new,
184		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
185
186	return (error);
187}
188
189#ifndef _SYS_SYSPROTO_H_
190struct ovadvise_args {
191	int anom;
192};
193#endif
194
195/*
196 * MPSAFE
197 */
198/* ARGSUSED */
199int
200sys_ovadvise(td, uap)
201	struct thread *td;
202	struct ovadvise_args *uap;
203{
204	/* START_GIANT_OPTIONAL */
205	/* END_GIANT_OPTIONAL */
206	return (EINVAL);
207}
208