Home | History | Annotate | Line # | Download | only in uvm
uvm_mmap.c revision 1.89
      1 /*	$NetBSD: uvm_mmap.c,v 1.89 2005/03/26 05:12:36 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * Copyright (c) 1991, 1993 The Regents of the University of California.
      6  * Copyright (c) 1988 University of Utah.
      7  *
      8  * All rights reserved.
      9  *
     10  * This code is derived from software contributed to Berkeley by
     11  * the Systems Programming Group of the University of Utah Computer
     12  * Science Department.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *      This product includes software developed by the Charles D. Cranor,
     25  *	Washington University, University of California, Berkeley and
     26  *	its contributors.
     27  * 4. Neither the name of the University nor the names of its contributors
     28  *    may be used to endorse or promote products derived from this software
     29  *    without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     41  * SUCH DAMAGE.
     42  *
     43  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
     44  *      @(#)vm_mmap.c   8.5 (Berkeley) 5/19/94
     45  * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
     46  */
     47 
     48 /*
     49  * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
     50  * function.
     51  */
     52 
     53 #include <sys/cdefs.h>
     54 __KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.89 2005/03/26 05:12:36 fvdl Exp $");
     55 
     56 #include "opt_compat_netbsd.h"
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/file.h>
     61 #include <sys/filedesc.h>
     62 #include <sys/resourcevar.h>
     63 #include <sys/mman.h>
     64 #include <sys/mount.h>
     65 #include <sys/proc.h>
     66 #include <sys/malloc.h>
     67 #include <sys/vnode.h>
     68 #include <sys/conf.h>
     69 #include <sys/stat.h>
     70 
     71 #include <miscfs/specfs/specdev.h>
     72 
     73 #include <sys/sa.h>
     74 #include <sys/syscallargs.h>
     75 
     76 #include <uvm/uvm.h>
     77 #include <uvm/uvm_device.h>
     78 
     79 #ifndef COMPAT_ZERODEV
     80 #define COMPAT_ZERODEV(dev)	(0)
     81 #endif
     82 
     83 /*
     84  * unimplemented VM system calls:
     85  */
     86 
     87 /*
     88  * sys_sbrk: sbrk system call.
     89  */
     90 
     91 /* ARGSUSED */
     92 int
     93 sys_sbrk(l, v, retval)
     94 	struct lwp *l;
     95 	void *v;
     96 	register_t *retval;
     97 {
     98 #if 0
     99 	struct sys_sbrk_args /* {
    100 		syscallarg(intptr_t) incr;
    101 	} */ *uap = v;
    102 #endif
    103 
    104 	return (ENOSYS);
    105 }
    106 
    107 /*
    108  * sys_sstk: sstk system call.
    109  */
    110 
    111 /* ARGSUSED */
    112 int
    113 sys_sstk(l, v, retval)
    114 	struct lwp *l;
    115 	void *v;
    116 	register_t *retval;
    117 {
    118 #if 0
    119 	struct sys_sstk_args /* {
    120 		syscallarg(int) incr;
    121 	} */ *uap = v;
    122 #endif
    123 
    124 	return (ENOSYS);
    125 }
    126 
    127 /*
    128  * sys_mincore: determine if pages are in core or not.
    129  */
    130 
    131 /* ARGSUSED */
    132 int
    133 sys_mincore(l, v, retval)
    134 	struct lwp *l;
    135 	void *v;
    136 	register_t *retval;
    137 {
    138 	struct sys_mincore_args /* {
    139 		syscallarg(void *) addr;
    140 		syscallarg(size_t) len;
    141 		syscallarg(char *) vec;
    142 	} */ *uap = v;
    143 	struct proc *p = l->l_proc;
    144 	struct vm_page *pg;
    145 	char *vec, pgi;
    146 	struct uvm_object *uobj;
    147 	struct vm_amap *amap;
    148 	struct vm_anon *anon;
    149 	struct vm_map_entry *entry;
    150 	vaddr_t start, end, lim;
    151 	struct vm_map *map;
    152 	vsize_t len;
    153 	int error = 0, npgs;
    154 
    155 	map = &p->p_vmspace->vm_map;
    156 
    157 	start = (vaddr_t)SCARG(uap, addr);
    158 	len = SCARG(uap, len);
    159 	vec = SCARG(uap, vec);
    160 
    161 	if (start & PAGE_MASK)
    162 		return (EINVAL);
    163 	len = round_page(len);
    164 	end = start + len;
    165 	if (end <= start)
    166 		return (EINVAL);
    167 
    168 	/*
    169 	 * Lock down vec, so our returned status isn't outdated by
    170 	 * storing the status byte for a page.
    171 	 */
    172 
    173 	npgs = len >> PAGE_SHIFT;
    174 	error = uvm_vslock(p, vec, npgs, VM_PROT_WRITE);
    175 	if (error) {
    176 		return error;
    177 	}
    178 	vm_map_lock_read(map);
    179 
    180 	if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
    181 		error = ENOMEM;
    182 		goto out;
    183 	}
    184 
    185 	for (/* nothing */;
    186 	     entry != &map->header && entry->start < end;
    187 	     entry = entry->next) {
    188 		KASSERT(!UVM_ET_ISSUBMAP(entry));
    189 		KASSERT(start >= entry->start);
    190 
    191 		/* Make sure there are no holes. */
    192 		if (entry->end < end &&
    193 		     (entry->next == &map->header ||
    194 		      entry->next->start > entry->end)) {
    195 			error = ENOMEM;
    196 			goto out;
    197 		}
    198 
    199 		lim = end < entry->end ? end : entry->end;
    200 
    201 		/*
    202 		 * Special case for objects with no "real" pages.  Those
    203 		 * are always considered resident (mapped devices).
    204 		 */
    205 
    206 		if (UVM_ET_ISOBJ(entry)) {
    207 			KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj));
    208 			if (UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
    209 				for (/* nothing */; start < lim;
    210 				     start += PAGE_SIZE, vec++)
    211 					subyte(vec, 1);
    212 				continue;
    213 			}
    214 		}
    215 
    216 		amap = entry->aref.ar_amap;	/* top layer */
    217 		uobj = entry->object.uvm_obj;	/* bottom layer */
    218 
    219 		if (amap != NULL)
    220 			amap_lock(amap);
    221 		if (uobj != NULL)
    222 			simple_lock(&uobj->vmobjlock);
    223 
    224 		for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) {
    225 			pgi = 0;
    226 			if (amap != NULL) {
    227 				/* Check the top layer first. */
    228 				anon = amap_lookup(&entry->aref,
    229 				    start - entry->start);
    230 				/* Don't need to lock anon here. */
    231 				if (anon != NULL && anon->u.an_page != NULL) {
    232 
    233 					/*
    234 					 * Anon has the page for this entry
    235 					 * offset.
    236 					 */
    237 
    238 					pgi = 1;
    239 				}
    240 			}
    241 			if (uobj != NULL && pgi == 0) {
    242 				/* Check the bottom layer. */
    243 				pg = uvm_pagelookup(uobj,
    244 				    entry->offset + (start - entry->start));
    245 				if (pg != NULL) {
    246 
    247 					/*
    248 					 * Object has the page for this entry
    249 					 * offset.
    250 					 */
    251 
    252 					pgi = 1;
    253 				}
    254 			}
    255 			(void) subyte(vec, pgi);
    256 		}
    257 		if (uobj != NULL)
    258 			simple_unlock(&uobj->vmobjlock);
    259 		if (amap != NULL)
    260 			amap_unlock(amap);
    261 	}
    262 
    263  out:
    264 	vm_map_unlock_read(map);
    265 	uvm_vsunlock(p, SCARG(uap, vec), npgs);
    266 	return (error);
    267 }
    268 
    269 /*
    270  * sys_mmap: mmap system call.
    271  *
    272  * => file offset and address may not be page aligned
    273  *    - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
    274  *    - if address isn't page aligned the mapping starts at trunc_page(addr)
    275  *      and the return value is adjusted up by the page offset.
    276  */
    277 
    278 int
    279 sys_mmap(l, v, retval)
    280 	struct lwp *l;
    281 	void *v;
    282 	register_t *retval;
    283 {
    284 	struct sys_mmap_args /* {
    285 		syscallarg(caddr_t) addr;
    286 		syscallarg(size_t) len;
    287 		syscallarg(int) prot;
    288 		syscallarg(int) flags;
    289 		syscallarg(int) fd;
    290 		syscallarg(long) pad;
    291 		syscallarg(off_t) pos;
    292 	} */ *uap = v;
    293 	struct proc *p = l->l_proc;
    294 	vaddr_t addr;
    295 	struct vattr va;
    296 	off_t pos;
    297 	vsize_t size, pageoff;
    298 	vm_prot_t prot, maxprot;
    299 	int flags, fd;
    300 	vaddr_t vm_min_address = VM_MIN_ADDRESS, defaddr;
    301 	struct filedesc *fdp = p->p_fd;
    302 	struct file *fp;
    303 	struct vnode *vp;
    304 	void *handle;
    305 	int error;
    306 
    307 	/*
    308 	 * first, extract syscall args from the uap.
    309 	 */
    310 
    311 	addr = (vaddr_t)SCARG(uap, addr);
    312 	size = (vsize_t)SCARG(uap, len);
    313 	prot = SCARG(uap, prot) & VM_PROT_ALL;
    314 	flags = SCARG(uap, flags);
    315 	fd = SCARG(uap, fd);
    316 	pos = SCARG(uap, pos);
    317 
    318 	/*
    319 	 * Fixup the old deprecated MAP_COPY into MAP_PRIVATE, and
    320 	 * validate the flags.
    321 	 */
    322 	if (flags & MAP_COPY)
    323 		flags = (flags & ~MAP_COPY) | MAP_PRIVATE;
    324 	if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE))
    325 		return (EINVAL);
    326 
    327 	/*
    328 	 * align file position and save offset.  adjust size.
    329 	 */
    330 
    331 	pageoff = (pos & PAGE_MASK);
    332 	pos  -= pageoff;
    333 	size += pageoff;			/* add offset */
    334 	size = (vsize_t)round_page(size);	/* round up */
    335 	if ((ssize_t) size < 0)
    336 		return (EINVAL);			/* don't allow wrap */
    337 
    338 	/*
    339 	 * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
    340 	 */
    341 
    342 	if (flags & MAP_FIXED) {
    343 
    344 		/* ensure address and file offset are aligned properly */
    345 		addr -= pageoff;
    346 		if (addr & PAGE_MASK)
    347 			return (EINVAL);
    348 
    349 		if (VM_MAXUSER_ADDRESS > 0 &&
    350 		    (addr + size) > VM_MAXUSER_ADDRESS)
    351 			return (EFBIG);
    352 		if (vm_min_address > 0 && addr < vm_min_address)
    353 			return (EINVAL);
    354 		if (addr > addr + size)
    355 			return (EOVERFLOW);		/* no wrapping! */
    356 
    357 	} else if (addr == 0 || !(flags & MAP_TRYFIXED)) {
    358 
    359 		/*
    360 		 * not fixed: make sure we skip over the largest
    361 		 * possible heap for non-topdown mapping arrangements.
    362 		 * we will refine our guess later (e.g. to account for
    363 		 * VAC, etc)
    364 		 */
    365 
    366 		defaddr = p->p_emul->e_vm_default_addr(p,
    367 		    (vaddr_t)p->p_vmspace->vm_daddr, size);
    368 
    369 		if (addr == 0 ||
    370 		    !(p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
    371 			addr = MAX(addr, defaddr);
    372 		else
    373 			addr = MIN(addr, defaddr);
    374 	}
    375 
    376 	/*
    377 	 * check for file mappings (i.e. not anonymous) and verify file.
    378 	 */
    379 
    380 	if ((flags & MAP_ANON) == 0) {
    381 
    382 		if ((fp = fd_getfile(fdp, fd)) == NULL)
    383 			return (EBADF);
    384 
    385 		simple_unlock(&fp->f_slock);
    386 
    387 		if (fp->f_type != DTYPE_VNODE)
    388 			return (ENODEV);		/* only mmap vnodes! */
    389 		vp = (struct vnode *)fp->f_data;	/* convert to vnode */
    390 
    391 		if (vp->v_type != VREG && vp->v_type != VCHR &&
    392 		    vp->v_type != VBLK)
    393 			return (ENODEV);  /* only REG/CHR/BLK support mmap */
    394 
    395 		if (vp->v_type != VCHR && pos < 0)
    396 			return (EINVAL);
    397 
    398 		if (vp->v_type != VCHR && (pos + size) < pos)
    399 			return (EOVERFLOW);		/* no offset wrapping */
    400 
    401 		/* special case: catch SunOS style /dev/zero */
    402 		if (vp->v_type == VCHR
    403 		    && (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
    404 			flags |= MAP_ANON;
    405 			goto is_anon;
    406 		}
    407 
    408 		/*
    409 		 * Old programs may not select a specific sharing type, so
    410 		 * default to an appropriate one.
    411 		 *
    412 		 * XXX: how does MAP_ANON fit in the picture?
    413 		 */
    414 		if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
    415 #if defined(DEBUG)
    416 			printf("WARNING: defaulted mmap() share type to "
    417 			   "%s (pid %d command %s)\n", vp->v_type == VCHR ?
    418 			   "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
    419 			    p->p_comm);
    420 #endif
    421 			if (vp->v_type == VCHR)
    422 				flags |= MAP_SHARED;	/* for a device */
    423 			else
    424 				flags |= MAP_PRIVATE;	/* for a file */
    425 		}
    426 
    427 		/*
    428 		 * MAP_PRIVATE device mappings don't make sense (and aren't
    429 		 * supported anyway).  However, some programs rely on this,
    430 		 * so just change it to MAP_SHARED.
    431 		 */
    432 		if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
    433 			flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
    434 		}
    435 
    436 		/*
    437 		 * now check protection
    438 		 */
    439 
    440 		maxprot = VM_PROT_EXECUTE;
    441 
    442 		/* check read access */
    443 		if (fp->f_flag & FREAD)
    444 			maxprot |= VM_PROT_READ;
    445 		else if (prot & PROT_READ)
    446 			return (EACCES);
    447 
    448 		/* check write access, shared case first */
    449 		if (flags & MAP_SHARED) {
    450 			/*
    451 			 * if the file is writable, only add PROT_WRITE to
    452 			 * maxprot if the file is not immutable, append-only.
    453 			 * otherwise, if we have asked for PROT_WRITE, return
    454 			 * EPERM.
    455 			 */
    456 			if (fp->f_flag & FWRITE) {
    457 				if ((error =
    458 				    VOP_GETATTR(vp, &va, p->p_ucred, p)))
    459 					return (error);
    460 				if ((va.va_flags &
    461 				    (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
    462 					maxprot |= VM_PROT_WRITE;
    463 				else if (prot & PROT_WRITE)
    464 					return (EPERM);
    465 			}
    466 			else if (prot & PROT_WRITE)
    467 				return (EACCES);
    468 		} else {
    469 			/* MAP_PRIVATE mappings can always write to */
    470 			maxprot |= VM_PROT_WRITE;
    471 		}
    472 		handle = vp;
    473 
    474 	} else {		/* MAP_ANON case */
    475 		/*
    476 		 * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
    477 		 */
    478 		if (fd != -1)
    479 			return (EINVAL);
    480 
    481  is_anon:		/* label for SunOS style /dev/zero */
    482 		handle = NULL;
    483 		maxprot = VM_PROT_ALL;
    484 		pos = 0;
    485 	}
    486 
    487 	/*
    488 	 * XXX (in)sanity check.  We don't do proper datasize checking
    489 	 * XXX for anonymous (or private writable) mmap().  However,
    490 	 * XXX know that if we're trying to allocate more than the amount
    491 	 * XXX remaining under our current data size limit, _that_ should
    492 	 * XXX be disallowed.
    493 	 */
    494 	if ((flags & MAP_ANON) != 0 ||
    495 	    ((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) {
    496 		if (size >
    497 		    (p->p_rlimit[RLIMIT_DATA].rlim_cur -
    498 		     ctob(p->p_vmspace->vm_dsize))) {
    499 			return (ENOMEM);
    500 		}
    501 	}
    502 
    503 	/*
    504 	 * now let kernel internal function uvm_mmap do the work.
    505 	 */
    506 
    507 	error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
    508 	    flags, handle, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    509 
    510 	if (error == 0)
    511 		/* remember to add offset */
    512 		*retval = (register_t)(addr + pageoff);
    513 
    514 	return (error);
    515 }
    516 
    517 /*
    518  * sys___msync13: the msync system call (a front-end for flush)
    519  */
    520 
    521 int
    522 sys___msync13(l, v, retval)
    523 	struct lwp *l;
    524 	void *v;
    525 	register_t *retval;
    526 {
    527 	struct sys___msync13_args /* {
    528 		syscallarg(caddr_t) addr;
    529 		syscallarg(size_t) len;
    530 		syscallarg(int) flags;
    531 	} */ *uap = v;
    532 	struct proc *p = l->l_proc;
    533 	vaddr_t addr;
    534 	vsize_t size, pageoff;
    535 	struct vm_map *map;
    536 	int error, rv, flags, uvmflags;
    537 
    538 	/*
    539 	 * extract syscall args from the uap
    540 	 */
    541 
    542 	addr = (vaddr_t)SCARG(uap, addr);
    543 	size = (vsize_t)SCARG(uap, len);
    544 	flags = SCARG(uap, flags);
    545 
    546 	/* sanity check flags */
    547 	if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
    548 	    (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
    549 	    (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
    550 		return (EINVAL);
    551 	if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
    552 		flags |= MS_SYNC;
    553 
    554 	/*
    555 	 * align the address to a page boundary and adjust the size accordingly.
    556 	 */
    557 
    558 	pageoff = (addr & PAGE_MASK);
    559 	addr -= pageoff;
    560 	size += pageoff;
    561 	size = (vsize_t)round_page(size);
    562 
    563 	/* disallow wrap-around. */
    564 	if (addr + size < addr)
    565 		return (EINVAL);
    566 
    567 	/*
    568 	 * get map
    569 	 */
    570 
    571 	map = &p->p_vmspace->vm_map;
    572 
    573 	/*
    574 	 * XXXCDC: do we really need this semantic?
    575 	 *
    576 	 * XXX Gak!  If size is zero we are supposed to sync "all modified
    577 	 * pages with the region containing addr".  Unfortunately, we
    578 	 * don't really keep track of individual mmaps so we approximate
    579 	 * by flushing the range of the map entry containing addr.
    580 	 * This can be incorrect if the region splits or is coalesced
    581 	 * with a neighbor.
    582 	 */
    583 
    584 	if (size == 0) {
    585 		struct vm_map_entry *entry;
    586 
    587 		vm_map_lock_read(map);
    588 		rv = uvm_map_lookup_entry(map, addr, &entry);
    589 		if (rv == TRUE) {
    590 			addr = entry->start;
    591 			size = entry->end - entry->start;
    592 		}
    593 		vm_map_unlock_read(map);
    594 		if (rv == FALSE)
    595 			return (EINVAL);
    596 	}
    597 
    598 	/*
    599 	 * translate MS_ flags into PGO_ flags
    600 	 */
    601 
    602 	uvmflags = PGO_CLEANIT;
    603 	if (flags & MS_INVALIDATE)
    604 		uvmflags |= PGO_FREE;
    605 	if (flags & MS_SYNC)
    606 		uvmflags |= PGO_SYNCIO;
    607 	else
    608 		uvmflags |= PGO_SYNCIO;	 /* XXXCDC: force sync for now! */
    609 
    610 	error = uvm_map_clean(map, addr, addr+size, uvmflags);
    611 	return error;
    612 }
    613 
    614 /*
    615  * sys_munmap: unmap a users memory
    616  */
    617 
    618 int
    619 sys_munmap(l, v, retval)
    620 	struct lwp *l;
    621 	void *v;
    622 	register_t *retval;
    623 {
    624 	struct sys_munmap_args /* {
    625 		syscallarg(caddr_t) addr;
    626 		syscallarg(size_t) len;
    627 	} */ *uap = v;
    628 	struct proc *p = l->l_proc;
    629 	vaddr_t addr;
    630 	vsize_t size, pageoff;
    631 	struct vm_map *map;
    632 	vaddr_t vm_min_address = VM_MIN_ADDRESS;
    633 	struct vm_map_entry *dead_entries;
    634 
    635 	/*
    636 	 * get syscall args.
    637 	 */
    638 
    639 	addr = (vaddr_t)SCARG(uap, addr);
    640 	size = (vsize_t)SCARG(uap, len);
    641 
    642 	/*
    643 	 * align the address to a page boundary and adjust the size accordingly.
    644 	 */
    645 
    646 	pageoff = (addr & PAGE_MASK);
    647 	addr -= pageoff;
    648 	size += pageoff;
    649 	size = (vsize_t)round_page(size);
    650 
    651 	if ((int)size < 0)
    652 		return (EINVAL);
    653 	if (size == 0)
    654 		return (0);
    655 
    656 	/*
    657 	 * Check for illegal addresses.  Watch out for address wrap...
    658 	 * Note that VM_*_ADDRESS are not constants due to casts (argh).
    659 	 */
    660 	if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
    661 		return (EINVAL);
    662 	if (vm_min_address > 0 && addr < vm_min_address)
    663 		return (EINVAL);
    664 	if (addr > addr + size)
    665 		return (EINVAL);
    666 	map = &p->p_vmspace->vm_map;
    667 
    668 	/*
    669 	 * interesting system call semantic: make sure entire range is
    670 	 * allocated before allowing an unmap.
    671 	 */
    672 
    673 	vm_map_lock(map);
    674 #if 0
    675 	if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
    676 		vm_map_unlock(map);
    677 		return (EINVAL);
    678 	}
    679 #endif
    680 	uvm_unmap_remove(map, addr, addr + size, &dead_entries, NULL);
    681 	vm_map_unlock(map);
    682 	if (dead_entries != NULL)
    683 		uvm_unmap_detach(dead_entries, 0);
    684 	return (0);
    685 }
    686 
    687 /*
    688  * sys_mprotect: the mprotect system call
    689  */
    690 
    691 int
    692 sys_mprotect(l, v, retval)
    693 	struct lwp *l;
    694 	void *v;
    695 	register_t *retval;
    696 {
    697 	struct sys_mprotect_args /* {
    698 		syscallarg(caddr_t) addr;
    699 		syscallarg(size_t) len;
    700 		syscallarg(int) prot;
    701 	} */ *uap = v;
    702 	struct proc *p = l->l_proc;
    703 	vaddr_t addr;
    704 	vsize_t size, pageoff;
    705 	vm_prot_t prot;
    706 	int error;
    707 
    708 	/*
    709 	 * extract syscall args from uap
    710 	 */
    711 
    712 	addr = (vaddr_t)SCARG(uap, addr);
    713 	size = (vsize_t)SCARG(uap, len);
    714 	prot = SCARG(uap, prot) & VM_PROT_ALL;
    715 
    716 	/*
    717 	 * align the address to a page boundary and adjust the size accordingly.
    718 	 */
    719 
    720 	pageoff = (addr & PAGE_MASK);
    721 	addr -= pageoff;
    722 	size += pageoff;
    723 	size = round_page(size);
    724 
    725 	error = uvm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
    726 				FALSE);
    727 	return error;
    728 }
    729 
    730 /*
    731  * sys_minherit: the minherit system call
    732  */
    733 
    734 int
    735 sys_minherit(l, v, retval)
    736 	struct lwp *l;
    737 	void *v;
    738 	register_t *retval;
    739 {
    740 	struct sys_minherit_args /* {
    741 		syscallarg(caddr_t) addr;
    742 		syscallarg(int) len;
    743 		syscallarg(int) inherit;
    744 	} */ *uap = v;
    745 	struct proc *p = l->l_proc;
    746 	vaddr_t addr;
    747 	vsize_t size, pageoff;
    748 	vm_inherit_t inherit;
    749 	int error;
    750 
    751 	addr = (vaddr_t)SCARG(uap, addr);
    752 	size = (vsize_t)SCARG(uap, len);
    753 	inherit = SCARG(uap, inherit);
    754 
    755 	/*
    756 	 * align the address to a page boundary and adjust the size accordingly.
    757 	 */
    758 
    759 	pageoff = (addr & PAGE_MASK);
    760 	addr -= pageoff;
    761 	size += pageoff;
    762 	size = (vsize_t)round_page(size);
    763 
    764 	if ((int)size < 0)
    765 		return (EINVAL);
    766 	error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size,
    767 				inherit);
    768 	return error;
    769 }
    770 
    771 /*
    772  * sys_madvise: give advice about memory usage.
    773  */
    774 
    775 /* ARGSUSED */
    776 int
    777 sys_madvise(l, v, retval)
    778 	struct lwp *l;
    779 	void *v;
    780 	register_t *retval;
    781 {
    782 	struct sys_madvise_args /* {
    783 		syscallarg(caddr_t) addr;
    784 		syscallarg(size_t) len;
    785 		syscallarg(int) behav;
    786 	} */ *uap = v;
    787 	struct proc *p = l->l_proc;
    788 	vaddr_t addr;
    789 	vsize_t size, pageoff;
    790 	int advice, error;
    791 
    792 	addr = (vaddr_t)SCARG(uap, addr);
    793 	size = (vsize_t)SCARG(uap, len);
    794 	advice = SCARG(uap, behav);
    795 
    796 	/*
    797 	 * align the address to a page boundary, and adjust the size accordingly
    798 	 */
    799 
    800 	pageoff = (addr & PAGE_MASK);
    801 	addr -= pageoff;
    802 	size += pageoff;
    803 	size = (vsize_t)round_page(size);
    804 
    805 	if ((ssize_t)size <= 0)
    806 		return (EINVAL);
    807 
    808 	switch (advice) {
    809 	case MADV_NORMAL:
    810 	case MADV_RANDOM:
    811 	case MADV_SEQUENTIAL:
    812 		error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size,
    813 		    advice);
    814 		break;
    815 
    816 	case MADV_WILLNEED:
    817 
    818 		/*
    819 		 * Activate all these pages, pre-faulting them in if
    820 		 * necessary.
    821 		 */
    822 		/*
    823 		 * XXX IMPLEMENT ME.
    824 		 * Should invent a "weak" mode for uvm_fault()
    825 		 * which would only do the PGO_LOCKED pgo_get().
    826 		 */
    827 
    828 		return (0);
    829 
    830 	case MADV_DONTNEED:
    831 
    832 		/*
    833 		 * Deactivate all these pages.  We don't need them
    834 		 * any more.  We don't, however, toss the data in
    835 		 * the pages.
    836 		 */
    837 
    838 		error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
    839 		    PGO_DEACTIVATE);
    840 		break;
    841 
    842 	case MADV_FREE:
    843 
    844 		/*
    845 		 * These pages contain no valid data, and may be
    846 		 * garbage-collected.  Toss all resources, including
    847 		 * any swap space in use.
    848 		 */
    849 
    850 		error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
    851 		    PGO_FREE);
    852 		break;
    853 
    854 	case MADV_SPACEAVAIL:
    855 
    856 		/*
    857 		 * XXXMRG What is this?  I think it's:
    858 		 *
    859 		 *	Ensure that we have allocated backing-store
    860 		 *	for these pages.
    861 		 *
    862 		 * This is going to require changes to the page daemon,
    863 		 * as it will free swap space allocated to pages in core.
    864 		 * There's also what to do for device/file/anonymous memory.
    865 		 */
    866 
    867 		return (EINVAL);
    868 
    869 	default:
    870 		return (EINVAL);
    871 	}
    872 
    873 	return error;
    874 }
    875 
    876 /*
    877  * sys_mlock: memory lock
    878  */
    879 
    880 int
    881 sys_mlock(l, v, retval)
    882 	struct lwp *l;
    883 	void *v;
    884 	register_t *retval;
    885 {
    886 	struct sys_mlock_args /* {
    887 		syscallarg(const void *) addr;
    888 		syscallarg(size_t) len;
    889 	} */ *uap = v;
    890 	struct proc *p = l->l_proc;
    891 	vaddr_t addr;
    892 	vsize_t size, pageoff;
    893 	int error;
    894 
    895 	/*
    896 	 * extract syscall args from uap
    897 	 */
    898 
    899 	addr = (vaddr_t)SCARG(uap, addr);
    900 	size = (vsize_t)SCARG(uap, len);
    901 
    902 	/*
    903 	 * align the address to a page boundary and adjust the size accordingly
    904 	 */
    905 
    906 	pageoff = (addr & PAGE_MASK);
    907 	addr -= pageoff;
    908 	size += pageoff;
    909 	size = (vsize_t)round_page(size);
    910 
    911 	/* disallow wrap-around. */
    912 	if (addr + size < addr)
    913 		return (EINVAL);
    914 
    915 	if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
    916 		return (EAGAIN);
    917 
    918 	if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
    919 			p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
    920 		return (EAGAIN);
    921 
    922 	error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE,
    923 	    0);
    924 	if (error == EFAULT)
    925 		error = ENOMEM;
    926 	return error;
    927 }
    928 
    929 /*
    930  * sys_munlock: unlock wired pages
    931  */
    932 
    933 int
    934 sys_munlock(l, v, retval)
    935 	struct lwp *l;
    936 	void *v;
    937 	register_t *retval;
    938 {
    939 	struct sys_munlock_args /* {
    940 		syscallarg(const void *) addr;
    941 		syscallarg(size_t) len;
    942 	} */ *uap = v;
    943 	struct proc *p = l->l_proc;
    944 	vaddr_t addr;
    945 	vsize_t size, pageoff;
    946 	int error;
    947 
    948 	/*
    949 	 * extract syscall args from uap
    950 	 */
    951 
    952 	addr = (vaddr_t)SCARG(uap, addr);
    953 	size = (vsize_t)SCARG(uap, len);
    954 
    955 	/*
    956 	 * align the address to a page boundary, and adjust the size accordingly
    957 	 */
    958 
    959 	pageoff = (addr & PAGE_MASK);
    960 	addr -= pageoff;
    961 	size += pageoff;
    962 	size = (vsize_t)round_page(size);
    963 
    964 	/* disallow wrap-around. */
    965 	if (addr + size < addr)
    966 		return (EINVAL);
    967 
    968 	error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE,
    969 	    0);
    970 	if (error == EFAULT)
    971 		error = ENOMEM;
    972 	return error;
    973 }
    974 
    975 /*
    976  * sys_mlockall: lock all pages mapped into an address space.
    977  */
    978 
    979 int
    980 sys_mlockall(l, v, retval)
    981 	struct lwp *l;
    982 	void *v;
    983 	register_t *retval;
    984 {
    985 	struct sys_mlockall_args /* {
    986 		syscallarg(int) flags;
    987 	} */ *uap = v;
    988 	struct proc *p = l->l_proc;
    989 	int error, flags;
    990 
    991 	flags = SCARG(uap, flags);
    992 
    993 	if (flags == 0 ||
    994 	    (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
    995 		return (EINVAL);
    996 
    997 	error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags,
    998 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    999 	return (error);
   1000 }
   1001 
   1002 /*
   1003  * sys_munlockall: unlock all pages mapped into an address space.
   1004  */
   1005 
   1006 int
   1007 sys_munlockall(l, v, retval)
   1008 	struct lwp *l;
   1009 	void *v;
   1010 	register_t *retval;
   1011 {
   1012 	struct proc *p = l->l_proc;
   1013 
   1014 	(void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
   1015 	return (0);
   1016 }
   1017 
   1018 /*
   1019  * uvm_mmap: internal version of mmap
   1020  *
   1021  * - used by sys_mmap and various framebuffers
   1022  * - handle is a vnode pointer or NULL for MAP_ANON
   1023  * - caller must page-align the file offset
   1024  */
   1025 
   1026 int
   1027 uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff, locklimit)
   1028 	struct vm_map *map;
   1029 	vaddr_t *addr;
   1030 	vsize_t size;
   1031 	vm_prot_t prot, maxprot;
   1032 	int flags;
   1033 	void *handle;
   1034 	voff_t foff;
   1035 	vsize_t locklimit;
   1036 {
   1037 	struct uvm_object *uobj;
   1038 	struct vnode *vp;
   1039 	vaddr_t align = 0;
   1040 	int error;
   1041 	int advice = UVM_ADV_NORMAL;
   1042 	uvm_flag_t uvmflag = 0;
   1043 
   1044 	/*
   1045 	 * check params
   1046 	 */
   1047 
   1048 	if (size == 0)
   1049 		return(0);
   1050 	if (foff & PAGE_MASK)
   1051 		return(EINVAL);
   1052 	if ((prot & maxprot) != prot)
   1053 		return(EINVAL);
   1054 
   1055 	/*
   1056 	 * for non-fixed mappings, round off the suggested address.
   1057 	 * for fixed mappings, check alignment and zap old mappings.
   1058 	 */
   1059 
   1060 	if ((flags & MAP_FIXED) == 0) {
   1061 		*addr = round_page(*addr);
   1062 	} else {
   1063 		if (*addr & PAGE_MASK)
   1064 			return(EINVAL);
   1065 		uvmflag |= UVM_FLAG_FIXED;
   1066 		(void) uvm_unmap(map, *addr, *addr + size);
   1067 	}
   1068 
   1069 	/*
   1070 	 * Try to see if any requested alignment can even be attemped.
   1071 	 * Make sure we can express the alignment (asking for a >= 4GB
   1072 	 * alignment on an ILP32 architecure make no sense) and the
   1073 	 * alignment is at least for a page sized quanitiy.  If the
   1074 	 * request was for a fixed mapping, make sure supplied address
   1075 	 * adheres to the request alignment.
   1076 	 */
   1077 	align = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
   1078 	if (align) {
   1079 		if (align >= sizeof(vaddr_t) * NBBY)
   1080 			return(EINVAL);
   1081 		align = 1L << align;
   1082 		if (align < PAGE_SIZE)
   1083 			return(EINVAL);
   1084 		if (align >= vm_map_max(map))
   1085 			return(ENOMEM);
   1086 		if (flags & MAP_FIXED) {
   1087 			if ((*addr & (align-1)) != 0)
   1088 				return(EINVAL);
   1089 			align = 0;
   1090 		}
   1091 	}
   1092 
   1093 	/*
   1094 	 * handle anon vs. non-anon mappings.   for non-anon mappings attach
   1095 	 * to underlying vm object.
   1096 	 */
   1097 
   1098 	if (flags & MAP_ANON) {
   1099 		foff = UVM_UNKNOWN_OFFSET;
   1100 		uobj = NULL;
   1101 		if ((flags & MAP_SHARED) == 0)
   1102 			/* XXX: defer amap create */
   1103 			uvmflag |= UVM_FLAG_COPYONW;
   1104 		else
   1105 			/* shared: create amap now */
   1106 			uvmflag |= UVM_FLAG_OVERLAY;
   1107 
   1108 	} else {
   1109 		vp = (struct vnode *)handle;
   1110 
   1111 		/*
   1112 		 * Don't allow mmap for EXEC if the file system
   1113 		 * is mounted NOEXEC.
   1114 		 */
   1115 		if ((prot & PROT_EXEC) != 0 &&
   1116 		    (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0)
   1117 			return (EACCES);
   1118 
   1119 		if (vp->v_type != VCHR) {
   1120 			error = VOP_MMAP(vp, 0, curproc->p_ucred, curproc);
   1121 			if (error) {
   1122 				return error;
   1123 			}
   1124 
   1125 			uobj = uvn_attach((void *)vp, (flags & MAP_SHARED) ?
   1126 			   maxprot : (maxprot & ~VM_PROT_WRITE));
   1127 
   1128 			/* XXX for now, attach doesn't gain a ref */
   1129 			VREF(vp);
   1130 
   1131 			/*
   1132 			 * If the vnode is being mapped with PROT_EXEC,
   1133 			 * then mark it as text.
   1134 			 */
   1135 			if (prot & PROT_EXEC)
   1136 				vn_markexec(vp);
   1137 		} else {
   1138 			int i = maxprot;
   1139 
   1140 			/*
   1141 			 * XXX Some devices don't like to be mapped with
   1142 			 * XXX PROT_EXEC or PROT_WRITE, but we don't really
   1143 			 * XXX have a better way of handling this, right now
   1144 			 */
   1145 			do {
   1146 				uobj = udv_attach((void *) &vp->v_rdev,
   1147 				    (flags & MAP_SHARED) ? i :
   1148 				    (i & ~VM_PROT_WRITE), foff, size);
   1149 				i--;
   1150 			} while ((uobj == NULL) && (i > 0));
   1151 			advice = UVM_ADV_RANDOM;
   1152 		}
   1153 		if (uobj == NULL)
   1154 			return((vp->v_type == VREG) ? ENOMEM : EINVAL);
   1155 		if ((flags & MAP_SHARED) == 0)
   1156 			uvmflag |= UVM_FLAG_COPYONW;
   1157 	}
   1158 
   1159 	uvmflag = UVM_MAPFLAG(prot, maxprot,
   1160 			(flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
   1161 			advice, uvmflag);
   1162 	error = uvm_map(map, addr, size, uobj, foff, align, uvmflag);
   1163 	if (error) {
   1164 		if (uobj)
   1165 			uobj->pgops->pgo_detach(uobj);
   1166 		return error;
   1167 	}
   1168 
   1169 	/*
   1170 	 * POSIX 1003.1b -- if our address space was configured
   1171 	 * to lock all future mappings, wire the one we just made.
   1172 	 *
   1173 	 * Also handle the MAP_WIRED flag here.
   1174 	 */
   1175 
   1176 	if (prot == VM_PROT_NONE) {
   1177 
   1178 		/*
   1179 		 * No more work to do in this case.
   1180 		 */
   1181 
   1182 		return (0);
   1183 	}
   1184 	vm_map_lock(map);
   1185 	if ((flags & MAP_WIRED) != 0 || (map->flags & VM_MAP_WIREFUTURE) != 0) {
   1186 		if (atop(size) + uvmexp.wired > uvmexp.wiredmax ||
   1187 		    (locklimit != 0 &&
   1188 		     size + ptoa(pmap_wired_count(vm_map_pmap(map))) >
   1189 		     locklimit)) {
   1190 			vm_map_unlock(map);
   1191 			uvm_unmap(map, *addr, *addr + size);
   1192 			return ENOMEM;
   1193 		}
   1194 
   1195 		/*
   1196 		 * uvm_map_pageable() always returns the map unlocked.
   1197 		 */
   1198 
   1199 		error = uvm_map_pageable(map, *addr, *addr + size,
   1200 					 FALSE, UVM_LK_ENTER);
   1201 		if (error) {
   1202 			uvm_unmap(map, *addr, *addr + size);
   1203 			return error;
   1204 		}
   1205 		return (0);
   1206 	}
   1207 	vm_map_unlock(map);
   1208 	return 0;
   1209 }
   1210 
   1211 vaddr_t
   1212 uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
   1213 {
   1214 	return VM_DEFAULT_ADDRESS(base, sz);
   1215 }
   1216