vm_unix.c revision 116226
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 116226 2003-06-11 23:50:51Z obrien $");
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;
821541Srgrimes
8398460Salc	new = round_page((vm_offset_t)uap->nsize);
8498460Salc	vm_map_lock(&vm->vm_map);
8579224Sdillon
8616679Sdyson	base = round_page((vm_offset_t) vm->vm_daddr);
8766748Sdwmalone	old = base + ctob(vm->vm_dsize);
8816679Sdyson	if (new > base) {
8966748Sdwmalone		/*
9098498Salc		 * Check the resource limit, but allow a process to reduce
9198498Salc		 * its usage, even if it remains over the limit.
9266748Sdwmalone		 */
9398498Salc		if (new - base > td->td_proc->p_rlimit[RLIMIT_DATA].rlim_cur &&
9498498Salc		    new > old) {
9579224Sdillon			error = ENOMEM;
9679224Sdillon			goto done;
9779224Sdillon		}
98103767Sjake		if (new > vm_map_max(&vm->vm_map)) {
9979224Sdillon			error = ENOMEM;
10079224Sdillon			goto done;
10179224Sdillon		}
10216679Sdyson	} else if (new < base) {
10316679Sdyson		/*
10416679Sdyson		 * This is simply an invalid value.  If someone wants to
10516679Sdyson		 * do fancy address space manipulations, mmap and munmap
10616679Sdyson		 * can do most of what the user would want.
10716679Sdyson		 */
10879224Sdillon		error = EINVAL;
10979224Sdillon		goto done;
11016679Sdyson	}
11116679Sdyson	if (new > old) {
11298833Sdillon		if (vm->vm_map.size + (new - old) >
11398833Sdillon		    td->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
11498833Sdillon			error = ENOMEM;
11598833Sdillon			goto done;
11698833Sdillon		}
11798460Salc		rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new,
11898460Salc		    VM_PROT_ALL, VM_PROT_ALL, 0);
1191541Srgrimes		if (rv != KERN_SUCCESS) {
12079224Sdillon			error = ENOMEM;
12179224Sdillon			goto done;
1221541Srgrimes		}
12398460Salc		vm->vm_dsize += btoc(new - old);
12416679Sdyson	} else if (new < old) {
12598460Salc		rv = vm_map_delete(&vm->vm_map, new, old);
1261541Srgrimes		if (rv != KERN_SUCCESS) {
12779224Sdillon			error = ENOMEM;
12879224Sdillon			goto done;
1291541Srgrimes		}
13016679Sdyson		vm->vm_dsize -= btoc(old - new);
1311541Srgrimes	}
13279224Sdillondone:
13398460Salc	vm_map_unlock(&vm->vm_map);
13479224Sdillon	return (error);
1351541Srgrimes}
1361541Srgrimes
13712221Sbde#ifndef _SYS_SYSPROTO_H_
1381541Srgrimesstruct ovadvise_args {
1395455Sdg	int anom;
1401541Srgrimes};
14112221Sbde#endif
1421549Srgrimes
14382697Sdillon/*
14482697Sdillon * MPSAFE
14582697Sdillon */
1461541Srgrimes/* ARGSUSED */
1471541Srgrimesint
14883366Sjulianovadvise(td, uap)
14983366Sjulian	struct thread *td;
1501541Srgrimes	struct ovadvise_args *uap;
1511541Srgrimes{
15279224Sdillon	/* START_GIANT_OPTIONAL */
15379224Sdillon	/* END_GIANT_OPTIONAL */
1541541Srgrimes	return (EINVAL);
1551541Srgrimes}
156