vm_unix.c revision 103767
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
4150477Speter * $FreeBSD: head/sys/vm/vm_unix.c 103767 2002-09-21 22:07:17Z jake $
421541Srgrimes */
431541Srgrimes
441541Srgrimes/*
451541Srgrimes * Traditional sbrk/grow interface to VM
461541Srgrimes */
4777139Sjhb
481541Srgrimes#include <sys/param.h>
4976166Smarkm#include <sys/lock.h>
5076981Sjhb#include <sys/mutex.h>
511541Srgrimes#include <sys/proc.h>
521541Srgrimes#include <sys/resourcevar.h>
5376981Sjhb#include <sys/sysproto.h>
5476827Salfred#include <sys/systm.h>
551541Srgrimes
561541Srgrimes#include <vm/vm.h>
5712662Sdg#include <vm/vm_param.h>
5812662Sdg#include <vm/pmap.h>
5912662Sdg#include <vm/vm_map.h>
601541Srgrimes
6112221Sbde#ifndef _SYS_SYSPROTO_H_
621541Srgrimesstruct obreak_args {
6312206Sbde	char *nsize;
641541Srgrimes};
6512221Sbde#endif
661549Srgrimes
6782697Sdillon/*
6882697Sdillon * MPSAFE
6982697Sdillon */
701541Srgrimes/* ARGSUSED */
711541Srgrimesint
7283366Sjulianobreak(td, uap)
7383366Sjulian	struct thread *td;
741541Srgrimes	struct obreak_args *uap;
751541Srgrimes{
7683366Sjulian	struct vmspace *vm = td->td_proc->p_vmspace;
7716679Sdyson	vm_offset_t new, old, base;
781541Srgrimes	int rv;
7979224Sdillon	int error = 0;
801541Srgrimes
8198460Salc	new = round_page((vm_offset_t)uap->nsize);
8298460Salc	vm_map_lock(&vm->vm_map);
8379224Sdillon
8416679Sdyson	base = round_page((vm_offset_t) vm->vm_daddr);
8566748Sdwmalone	old = base + ctob(vm->vm_dsize);
8616679Sdyson	if (new > base) {
8766748Sdwmalone		/*
8898498Salc		 * Check the resource limit, but allow a process to reduce
8998498Salc		 * its usage, even if it remains over the limit.
9066748Sdwmalone		 */
9198498Salc		if (new - base > td->td_proc->p_rlimit[RLIMIT_DATA].rlim_cur &&
9298498Salc		    new > old) {
9379224Sdillon			error = ENOMEM;
9479224Sdillon			goto done;
9579224Sdillon		}
96103767Sjake		if (new > vm_map_max(&vm->vm_map)) {
9779224Sdillon			error = ENOMEM;
9879224Sdillon			goto done;
9979224Sdillon		}
10016679Sdyson	} else if (new < base) {
10116679Sdyson		/*
10216679Sdyson		 * This is simply an invalid value.  If someone wants to
10316679Sdyson		 * do fancy address space manipulations, mmap and munmap
10416679Sdyson		 * can do most of what the user would want.
10516679Sdyson		 */
10679224Sdillon		error = EINVAL;
10779224Sdillon		goto done;
10816679Sdyson	}
10916679Sdyson	if (new > old) {
11098833Sdillon		if (vm->vm_map.size + (new - old) >
11198833Sdillon		    td->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
11298833Sdillon			error = ENOMEM;
11398833Sdillon			goto done;
11498833Sdillon		}
11598460Salc		rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new,
11698460Salc		    VM_PROT_ALL, VM_PROT_ALL, 0);
1171541Srgrimes		if (rv != KERN_SUCCESS) {
11879224Sdillon			error = ENOMEM;
11979224Sdillon			goto done;
1201541Srgrimes		}
12198460Salc		vm->vm_dsize += btoc(new - old);
12216679Sdyson	} else if (new < old) {
12398460Salc		rv = vm_map_delete(&vm->vm_map, new, old);
1241541Srgrimes		if (rv != KERN_SUCCESS) {
12579224Sdillon			error = ENOMEM;
12679224Sdillon			goto done;
1271541Srgrimes		}
12816679Sdyson		vm->vm_dsize -= btoc(old - new);
1291541Srgrimes	}
13079224Sdillondone:
13198460Salc	vm_map_unlock(&vm->vm_map);
13279224Sdillon	return (error);
1331541Srgrimes}
1341541Srgrimes
13512221Sbde#ifndef _SYS_SYSPROTO_H_
1361541Srgrimesstruct ovadvise_args {
1375455Sdg	int anom;
1381541Srgrimes};
13912221Sbde#endif
1401549Srgrimes
14182697Sdillon/*
14282697Sdillon * MPSAFE
14382697Sdillon */
1441541Srgrimes/* ARGSUSED */
1451541Srgrimesint
14683366Sjulianovadvise(td, uap)
14783366Sjulian	struct thread *td;
1481541Srgrimes	struct ovadvise_args *uap;
1491541Srgrimes{
15079224Sdillon	/* START_GIANT_OPTIONAL */
15179224Sdillon	/* END_GIANT_OPTIONAL */
1521541Srgrimes	return (EINVAL);
1531541Srgrimes}
154