vm_unix.c revision 76827
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 76827 2001-05-19 01:28:09Z alfred $
421541Srgrimes */
431541Srgrimes
441541Srgrimes/*
451541Srgrimes * Traditional sbrk/grow interface to VM
461541Srgrimes */
471541Srgrimes#include <sys/param.h>
4876166Smarkm#include <sys/lock.h>
4912221Sbde#include <sys/sysproto.h>
501541Srgrimes#include <sys/proc.h>
511541Srgrimes#include <sys/resourcevar.h>
5276827Salfred#include <sys/lock.h>
5376827Salfred#include <sys/mutex.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
671541Srgrimes/* ARGSUSED */
681541Srgrimesint
6930994Sphkobreak(p, uap)
701541Srgrimes	struct proc *p;
711541Srgrimes	struct obreak_args *uap;
721541Srgrimes{
731541Srgrimes	register struct vmspace *vm = p->p_vmspace;
7416679Sdyson	vm_offset_t new, old, base;
751541Srgrimes	int rv;
761541Srgrimes
7716679Sdyson	base = round_page((vm_offset_t) vm->vm_daddr);
7840286Sdg	new = round_page((vm_offset_t)uap->nsize);
7966748Sdwmalone	old = base + ctob(vm->vm_dsize);
8016679Sdyson	if (new > base) {
8166748Sdwmalone		/*
8266748Sdwmalone		 * We check resource limits here, but alow processes to
8366748Sdwmalone		 * reduce their usage, even if they remain over the limit.
8466748Sdwmalone		 */
8566748Sdwmalone		if (new > old &&
8666748Sdwmalone		    (new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur)
8716679Sdyson			return ENOMEM;
8816679Sdyson		if (new >= VM_MAXUSER_ADDRESS)
8916679Sdyson			return (ENOMEM);
9016679Sdyson	} else if (new < base) {
9116679Sdyson		/*
9216679Sdyson		 * This is simply an invalid value.  If someone wants to
9316679Sdyson		 * do fancy address space manipulations, mmap and munmap
9416679Sdyson		 * can do most of what the user would want.
9516679Sdyson		 */
9616679Sdyson		return EINVAL;
9716679Sdyson	}
9816679Sdyson
9976827Salfred	mtx_lock(&vm_mtx);
10016679Sdyson	if (new > old) {
10116679Sdyson		vm_size_t diff;
10244206Sdillon
10316679Sdyson		diff = new - old;
10413490Sdyson		rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
10543751Sdillon			VM_PROT_ALL, VM_PROT_ALL, 0);
1061541Srgrimes		if (rv != KERN_SUCCESS) {
10776827Salfred			mtx_unlock(&vm_mtx);
1085455Sdg			return (ENOMEM);
1091541Srgrimes		}
1101541Srgrimes		vm->vm_dsize += btoc(diff);
11116679Sdyson	} else if (new < old) {
11216679Sdyson		rv = vm_map_remove(&vm->vm_map, new, old);
1131541Srgrimes		if (rv != KERN_SUCCESS) {
11476827Salfred			mtx_unlock(&vm_mtx);
1155455Sdg			return (ENOMEM);
1161541Srgrimes		}
11716679Sdyson		vm->vm_dsize -= btoc(old - new);
1181541Srgrimes	}
11976827Salfred	mtx_unlock(&vm_mtx);
1205455Sdg	return (0);
1211541Srgrimes}
1221541Srgrimes
12312221Sbde#ifndef _SYS_SYSPROTO_H_
1241541Srgrimesstruct ovadvise_args {
1255455Sdg	int anom;
1261541Srgrimes};
12712221Sbde#endif
1281549Srgrimes
1291541Srgrimes/* ARGSUSED */
1301541Srgrimesint
13130994Sphkovadvise(p, uap)
1321541Srgrimes	struct proc *p;
1331541Srgrimes	struct ovadvise_args *uap;
1341541Srgrimes{
1351541Srgrimes
1361541Srgrimes	return (EINVAL);
1371541Srgrimes}
138