vm_unix.c revision 82697
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 82697 2001-08-31 22:10:03Z dillon $
421541Srgrimes */
431541Srgrimes
441541Srgrimes/*
451541Srgrimes * Traditional sbrk/grow interface to VM
461541Srgrimes */
4777139Sjhb#include "opt_bleed.h"
4877139Sjhb
491541Srgrimes#include <sys/param.h>
5076166Smarkm#include <sys/lock.h>
5176981Sjhb#include <sys/mutex.h>
521541Srgrimes#include <sys/proc.h>
531541Srgrimes#include <sys/resourcevar.h>
5476981Sjhb#include <sys/sysproto.h>
5576827Salfred#include <sys/systm.h>
561541Srgrimes
571541Srgrimes#include <vm/vm.h>
5812662Sdg#include <vm/vm_param.h>
5912662Sdg#include <vm/pmap.h>
6012662Sdg#include <vm/vm_map.h>
611541Srgrimes
6212221Sbde#ifndef _SYS_SYSPROTO_H_
631541Srgrimesstruct obreak_args {
6412206Sbde	char *nsize;
651541Srgrimes};
6612221Sbde#endif
671549Srgrimes
6882697Sdillon/*
6982697Sdillon * MPSAFE
7082697Sdillon */
711541Srgrimes/* ARGSUSED */
721541Srgrimesint
7330994Sphkobreak(p, uap)
741541Srgrimes	struct proc *p;
751541Srgrimes	struct obreak_args *uap;
761541Srgrimes{
7779242Sdillon	struct vmspace *vm = p->p_vmspace;
7816679Sdyson	vm_offset_t new, old, base;
791541Srgrimes	int rv;
8079224Sdillon	int error = 0;
811541Srgrimes
8279224Sdillon	mtx_lock(&Giant);	/* syscall marked mp-safe but isn't */
8379224Sdillon
8416679Sdyson	base = round_page((vm_offset_t) vm->vm_daddr);
8540286Sdg	new = round_page((vm_offset_t)uap->nsize);
8666748Sdwmalone	old = base + ctob(vm->vm_dsize);
8716679Sdyson	if (new > base) {
8866748Sdwmalone		/*
8966748Sdwmalone		 * We check resource limits here, but alow processes to
9066748Sdwmalone		 * reduce their usage, even if they remain over the limit.
9166748Sdwmalone		 */
9266748Sdwmalone		if (new > old &&
9379224Sdillon		    (new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
9479224Sdillon			error = ENOMEM;
9579224Sdillon			goto done;
9679224Sdillon		}
9779224Sdillon		if (new >= VM_MAXUSER_ADDRESS) {
9879224Sdillon			error = ENOMEM;
9979224Sdillon			goto done;
10079224Sdillon		}
10116679Sdyson	} else if (new < base) {
10216679Sdyson		/*
10316679Sdyson		 * This is simply an invalid value.  If someone wants to
10416679Sdyson		 * do fancy address space manipulations, mmap and munmap
10516679Sdyson		 * can do most of what the user would want.
10616679Sdyson		 */
10779224Sdillon		error = EINVAL;
10879224Sdillon		goto done;
10916679Sdyson	}
11016679Sdyson
11116679Sdyson	if (new > old) {
11216679Sdyson		vm_size_t diff;
11344206Sdillon
11416679Sdyson		diff = new - old;
11513490Sdyson		rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
11643751Sdillon			VM_PROT_ALL, VM_PROT_ALL, 0);
1171541Srgrimes		if (rv != KERN_SUCCESS) {
11879224Sdillon			error = ENOMEM;
11979224Sdillon			goto done;
1201541Srgrimes		}
1211541Srgrimes		vm->vm_dsize += btoc(diff);
12216679Sdyson	} else if (new < old) {
12316679Sdyson		rv = vm_map_remove(&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:
13179224Sdillon	mtx_unlock(&Giant);
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
14630994Sphkovadvise(p, uap)
1471541Srgrimes	struct proc *p;
1481541Srgrimes	struct ovadvise_args *uap;
1491541Srgrimes{
15079224Sdillon	/* START_GIANT_OPTIONAL */
15179224Sdillon	/* END_GIANT_OPTIONAL */
1521541Srgrimes	return (EINVAL);
1531541Srgrimes}
154