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