vm_unix.c revision 118771
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1988 University of Utah.
31541Srgrimes * Copyright (c) 1991, 1993
41541Srgrimes *	The Regents of the University of California.  All rights reserved.
51541Srgrimes *
61541Srgrimes * This code is derived from software contributed to Berkeley by
71541Srgrimes * the Systems Programming Group of the University of Utah Computer
81541Srgrimes * Science Department.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
1958705Scharnier *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Berkeley and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
381541Srgrimes * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
391541Srgrimes *
401541Srgrimes *	@(#)vm_unix.c	8.1 (Berkeley) 6/11/93
411541Srgrimes */
421541Srgrimes
431541Srgrimes/*
441541Srgrimes * Traditional sbrk/grow interface to VM
451541Srgrimes */
4677139Sjhb
47116226Sobrien#include <sys/cdefs.h>
48116226Sobrien__FBSDID("$FreeBSD: head/sys/vm/vm_unix.c 118771 2003-08-11 07:14:08Z bms $");
49116226Sobrien
501541Srgrimes#include <sys/param.h>
5176166Smarkm#include <sys/lock.h>
5276981Sjhb#include <sys/mutex.h>
531541Srgrimes#include <sys/proc.h>
541541Srgrimes#include <sys/resourcevar.h>
5576981Sjhb#include <sys/sysproto.h>
5676827Salfred#include <sys/systm.h>
571541Srgrimes
581541Srgrimes#include <vm/vm.h>
5912662Sdg#include <vm/vm_param.h>
6012662Sdg#include <vm/pmap.h>
6112662Sdg#include <vm/vm_map.h>
621541Srgrimes
6312221Sbde#ifndef _SYS_SYSPROTO_H_
641541Srgrimesstruct obreak_args {
6512206Sbde	char *nsize;
661541Srgrimes};
6712221Sbde#endif
681549Srgrimes
6982697Sdillon/*
7082697Sdillon * MPSAFE
7182697Sdillon */
721541Srgrimes/* ARGSUSED */
731541Srgrimesint
7483366Sjulianobreak(td, uap)
7583366Sjulian	struct thread *td;
761541Srgrimes	struct obreak_args *uap;
771541Srgrimes{
7883366Sjulian	struct vmspace *vm = td->td_proc->p_vmspace;
7916679Sdyson	vm_offset_t new, old, base;
801541Srgrimes	int rv;
8179224Sdillon	int error = 0;
82118771Sbms	boolean_t do_map_wirefuture;
831541Srgrimes
84118771Sbms	do_map_wirefuture = FALSE;
8598460Salc	new = round_page((vm_offset_t)uap->nsize);
8698460Salc	vm_map_lock(&vm->vm_map);
8779224Sdillon
8816679Sdyson	base = round_page((vm_offset_t) vm->vm_daddr);
8966748Sdwmalone	old = base + ctob(vm->vm_dsize);
9016679Sdyson	if (new > base) {
9166748Sdwmalone		/*
9298498Salc		 * Check the resource limit, but allow a process to reduce
9398498Salc		 * its usage, even if it remains over the limit.
9466748Sdwmalone		 */
9598498Salc		if (new - base > td->td_proc->p_rlimit[RLIMIT_DATA].rlim_cur &&
9698498Salc		    new > old) {
9779224Sdillon			error = ENOMEM;
9879224Sdillon			goto done;
9979224Sdillon		}
100103767Sjake		if (new > vm_map_max(&vm->vm_map)) {
10179224Sdillon			error = ENOMEM;
10279224Sdillon			goto done;
10379224Sdillon		}
10416679Sdyson	} else if (new < base) {
10516679Sdyson		/*
10616679Sdyson		 * This is simply an invalid value.  If someone wants to
10716679Sdyson		 * do fancy address space manipulations, mmap and munmap
10816679Sdyson		 * can do most of what the user would want.
10916679Sdyson		 */
11079224Sdillon		error = EINVAL;
11179224Sdillon		goto done;
11216679Sdyson	}
11316679Sdyson	if (new > old) {
11498833Sdillon		if (vm->vm_map.size + (new - old) >
11598833Sdillon		    td->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
11698833Sdillon			error = ENOMEM;
11798833Sdillon			goto done;
11898833Sdillon		}
11998460Salc		rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new,
12098460Salc		    VM_PROT_ALL, VM_PROT_ALL, 0);
1211541Srgrimes		if (rv != KERN_SUCCESS) {
12279224Sdillon			error = ENOMEM;
12379224Sdillon			goto done;
1241541Srgrimes		}
12598460Salc		vm->vm_dsize += btoc(new - old);
126118771Sbms		/*
127118771Sbms		 * Handle the MAP_WIREFUTURE case for legacy applications,
128118771Sbms		 * by marking the newly mapped range of pages as wired.
129118771Sbms		 * We are not required to perform a corresponding
130118771Sbms		 * vm_map_unwire() before vm_map_delete() below, as
131118771Sbms		 * it will forcibly unwire the pages in the range.
132118771Sbms		 *
133118771Sbms		 * XXX If the pages cannot be wired, no error is returned.
134118771Sbms		 */
135118771Sbms		if ((vm->vm_map.flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) {
136118771Sbms			if (bootverbose)
137118771Sbms				printf("obreak: MAP_WIREFUTURE set\n");
138118771Sbms			do_map_wirefuture = TRUE;
139118771Sbms		}
14016679Sdyson	} else if (new < old) {
14198460Salc		rv = vm_map_delete(&vm->vm_map, new, old);
1421541Srgrimes		if (rv != KERN_SUCCESS) {
14379224Sdillon			error = ENOMEM;
14479224Sdillon			goto done;
1451541Srgrimes		}
14616679Sdyson		vm->vm_dsize -= btoc(old - new);
1471541Srgrimes	}
14879224Sdillondone:
14998460Salc	vm_map_unlock(&vm->vm_map);
150118771Sbms
151118771Sbms	if (do_map_wirefuture)
152118771Sbms		(void) vm_map_wire(&vm->vm_map, old, new,
153118771Sbms		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
154118771Sbms
15579224Sdillon	return (error);
1561541Srgrimes}
1571541Srgrimes
15812221Sbde#ifndef _SYS_SYSPROTO_H_
1591541Srgrimesstruct ovadvise_args {
1605455Sdg	int anom;
1611541Srgrimes};
16212221Sbde#endif
1631549Srgrimes
16482697Sdillon/*
16582697Sdillon * MPSAFE
16682697Sdillon */
1671541Srgrimes/* ARGSUSED */
1681541Srgrimesint
16983366Sjulianovadvise(td, uap)
17083366Sjulian	struct thread *td;
1711541Srgrimes	struct ovadvise_args *uap;
1721541Srgrimes{
17379224Sdillon	/* START_GIANT_OPTIONAL */
17479224Sdillon	/* END_GIANT_OPTIONAL */
1751541Srgrimes	return (EINVAL);
1761541Srgrimes}
177