vm_unix.c revision 79224
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 79224 2001-07-04 16:20:28Z 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
681541Srgrimes/* ARGSUSED */
691541Srgrimesint
7030994Sphkobreak(p, uap)
711541Srgrimes	struct proc *p;
721541Srgrimes	struct obreak_args *uap;
731541Srgrimes{
741541Srgrimes	register struct vmspace *vm = p->p_vmspace;
7516679Sdyson	vm_offset_t new, old, base;
761541Srgrimes	int rv;
7779224Sdillon	int error = 0;
781541Srgrimes
7979224Sdillon	mtx_lock(&Giant);	/* syscall marked mp-safe but isn't */
8079224Sdillon
8116679Sdyson	base = round_page((vm_offset_t) vm->vm_daddr);
8240286Sdg	new = round_page((vm_offset_t)uap->nsize);
8366748Sdwmalone	old = base + ctob(vm->vm_dsize);
8416679Sdyson	if (new > base) {
8566748Sdwmalone		/*
8666748Sdwmalone		 * We check resource limits here, but alow processes to
8766748Sdwmalone		 * reduce their usage, even if they remain over the limit.
8866748Sdwmalone		 */
8966748Sdwmalone		if (new > old &&
9079224Sdillon		    (new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
9179224Sdillon			error = ENOMEM;
9279224Sdillon			goto done;
9379224Sdillon		}
9479224Sdillon		if (new >= VM_MAXUSER_ADDRESS) {
9579224Sdillon			error = ENOMEM;
9679224Sdillon			goto done;
9779224Sdillon		}
9816679Sdyson	} else if (new < base) {
9916679Sdyson		/*
10016679Sdyson		 * This is simply an invalid value.  If someone wants to
10116679Sdyson		 * do fancy address space manipulations, mmap and munmap
10216679Sdyson		 * can do most of what the user would want.
10316679Sdyson		 */
10479224Sdillon		error = EINVAL;
10579224Sdillon		goto done;
10616679Sdyson	}
10716679Sdyson
10816679Sdyson	if (new > old) {
10916679Sdyson		vm_size_t diff;
11044206Sdillon
11116679Sdyson		diff = new - old;
11213490Sdyson		rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
11343751Sdillon			VM_PROT_ALL, VM_PROT_ALL, 0);
1141541Srgrimes		if (rv != KERN_SUCCESS) {
11579224Sdillon			error = ENOMEM;
11679224Sdillon			goto done;
1171541Srgrimes		}
1181541Srgrimes		vm->vm_dsize += btoc(diff);
11916679Sdyson	} else if (new < old) {
12016679Sdyson		rv = vm_map_remove(&vm->vm_map, new, old);
1211541Srgrimes		if (rv != KERN_SUCCESS) {
12279224Sdillon			error = ENOMEM;
12379224Sdillon			goto done;
1241541Srgrimes		}
12516679Sdyson		vm->vm_dsize -= btoc(old - new);
1261541Srgrimes	}
12779224Sdillondone:
12879224Sdillon	mtx_unlock(&Giant);
12979224Sdillon	return (error);
1301541Srgrimes}
1311541Srgrimes
13212221Sbde#ifndef _SYS_SYSPROTO_H_
1331541Srgrimesstruct ovadvise_args {
1345455Sdg	int anom;
1351541Srgrimes};
13612221Sbde#endif
1371549Srgrimes
1381541Srgrimes/* ARGSUSED */
1391541Srgrimesint
14030994Sphkovadvise(p, uap)
1411541Srgrimes	struct proc *p;
1421541Srgrimes	struct ovadvise_args *uap;
1431541Srgrimes{
14479224Sdillon	/* START_GIANT_OPTIONAL */
14579224Sdillon	/* END_GIANT_OPTIONAL */
1461541Srgrimes	return (EINVAL);
1471541Srgrimes}
148