Home | History | Annotate | Line # | Download | only in uvm
uvm_mmap.c revision 1.13
      1 /*	$NetBSD: uvm_mmap.c,v 1.13 1998/09/30 12:07:51 tv Exp $	*/
      2 
      3 /*
      4  * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
      5  *         >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
      6  */
      7 /*
      8  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      9  * Copyright (c) 1991, 1993 The Regents of the University of California.
     10  * Copyright (c) 1988 University of Utah.
     11  *
     12  * All rights reserved.
     13  *
     14  * This code is derived from software contributed to Berkeley by
     15  * the Systems Programming Group of the University of Utah Computer
     16  * Science Department.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  * 3. All advertising materials mentioning features or use of this software
     27  *    must display the following acknowledgement:
     28  *      This product includes software developed by the Charles D. Cranor,
     29  *	Washington University, University of California, Berkeley and
     30  *	its contributors.
     31  * 4. Neither the name of the University nor the names of its contributors
     32  *    may be used to endorse or promote products derived from this software
     33  *    without specific prior written permission.
     34  *
     35  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     45  * SUCH DAMAGE.
     46  *
     47  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
     48  *      @(#)vm_mmap.c   8.5 (Berkeley) 5/19/94
     49  * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
     50  */
     51 
     52 /*
     53  * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
     54  * function.
     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 <vm/vm.h>
     72 #include <vm/vm_page.h>
     73 #include <vm/vm_kern.h>
     74 
     75 #include <sys/syscallargs.h>
     76 
     77 #include <uvm/uvm.h>
     78 #include <uvm/uvm_device.h>
     79 #include <uvm/uvm_vnode.h>
     80 
     81 
     82 /*
     83  * unimplemented VM system calls:
     84  */
     85 
     86 /*
     87  * sys_sbrk: sbrk system call.
     88  */
     89 
     90 /* ARGSUSED */
     91 int
     92 sys_sbrk(p, v, retval)
     93 	struct proc *p;
     94 	void *v;
     95 	register_t *retval;
     96 {
     97 #if 0
     98 	struct sys_sbrk_args /* {
     99 			  syscallarg(int) incr;
    100 			  } */ *uap = v;
    101 #endif
    102 
    103 	return (EOPNOTSUPP);
    104 }
    105 
    106 /*
    107  * sys_sstk: sstk system call.
    108  */
    109 
    110 /* ARGSUSED */
    111 int
    112 sys_sstk(p, v, retval)
    113 	struct proc *p;
    114 	void *v;
    115 	register_t *retval;
    116 {
    117 #if 0
    118 	struct sys_sstk_args /* {
    119 			  syscallarg(int) incr;
    120 			  } */ *uap = v;
    121 #endif
    122 
    123 	return (EOPNOTSUPP);
    124 }
    125 
    126 /*
    127  * sys_madvise: give advice about memory usage.
    128  */
    129 
    130 /* ARGSUSED */
    131 int
    132 sys_madvise(p, v, retval)
    133 	struct proc *p;
    134 	void *v;
    135 	register_t *retval;
    136 {
    137 #if 0
    138 	struct sys_madvise_args /* {
    139 			     syscallarg(caddr_t) addr;
    140 			     syscallarg(size_t) len;
    141 			     syscallarg(int) behav;
    142 			     } */ *uap = v;
    143 #endif
    144 
    145 	/*
    146 	 * Not supported, but harmless to declare "success".
    147 	 * Some emulations expect a working madvise().
    148 	 */
    149 	return (0);
    150 }
    151 
    152 /*
    153  * sys_mincore: determine if pages are in core or not.
    154  */
    155 
    156 /* ARGSUSED */
    157 int
    158 sys_mincore(p, v, retval)
    159 	struct proc *p;
    160 	void *v;
    161 	register_t *retval;
    162 {
    163 #if 0
    164 	struct sys_mincore_args /* {
    165 			     syscallarg(caddr_t) addr;
    166 			     syscallarg(size_t) len;
    167 			     syscallarg(char *) vec;
    168 			     } */ *uap = v;
    169 #endif
    170 
    171 	return (EOPNOTSUPP);
    172 }
    173 
    174 #if 0
    175 /*
    176  * munmapfd: unmap file descriptor
    177  *
    178  * XXX: is this acutally a useful function?   could it be useful?
    179  */
    180 
    181 void
    182 munmapfd(p, fd)
    183 	struct proc *p;
    184 	int fd;
    185 {
    186 
    187 	/*
    188 	 * XXX should vm_deallocate any regions mapped to this file
    189 	 */
    190 	p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
    191 }
    192 #endif
    193 
    194 /*
    195  * sys_mmap: mmap system call.
    196  *
    197  * => file offest and address may not be page aligned
    198  *    - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
    199  *    - if address isn't page aligned the mapping starts at trunc_page(addr)
    200  *      and the return value is adjusted up by the page offset.
    201  */
    202 
    203 int
    204 sys_mmap(p, v, retval)
    205 	struct proc *p;
    206 	void *v;
    207 	register_t *retval;
    208 {
    209 	register struct sys_mmap_args /* {
    210 		syscallarg(caddr_t) addr;
    211 		syscallarg(size_t) len;
    212 		syscallarg(int) prot;
    213 		syscallarg(int) flags;
    214 		syscallarg(int) fd;
    215 		syscallarg(long) pad;
    216 		syscallarg(off_t) pos;
    217 	} */ *uap = v;
    218 	vaddr_t addr;
    219 	struct vattr va;
    220 	off_t pos;
    221 	vsize_t size, pageoff;
    222 	vm_prot_t prot, maxprot;
    223 	int flags, fd;
    224 	vaddr_t vm_min_address = VM_MIN_ADDRESS;
    225 	register struct filedesc *fdp = p->p_fd;
    226 	register struct file *fp;
    227 	struct vnode *vp;
    228 	caddr_t handle;
    229 	int error;
    230 
    231 	/*
    232 	 * first, extract syscall args from the uap.
    233 	 */
    234 
    235 	addr = (vaddr_t) SCARG(uap, addr);
    236 	size = (vsize_t) SCARG(uap, len);
    237 	prot = SCARG(uap, prot) & VM_PROT_ALL;
    238 	flags = SCARG(uap, flags);
    239 	fd = SCARG(uap, fd);
    240 	pos = SCARG(uap, pos);
    241 
    242 	/*
    243 	 * make sure that the newsize fits within a vaddr_t
    244 	 * XXX: need to revise addressing data types
    245 	 */
    246 	if (pos + size > (vaddr_t)-PAGE_SIZE) {
    247 #ifdef DEBUG
    248 		printf("mmap: pos=%qx, size=%x too big\n", pos, (int)size);
    249 #endif
    250 		return (EINVAL);
    251 	}
    252 
    253 	/*
    254 	 * align file position and save offset.  adjust size.
    255 	 */
    256 
    257 	pageoff = (pos & PAGE_MASK);
    258 	pos  -= pageoff;
    259 	size += pageoff;			/* add offset */
    260 	size = (vsize_t) round_page(size);	/* round up */
    261 	if ((ssize_t) size < 0)
    262 		return (EINVAL);			/* don't allow wrap */
    263 
    264 	/*
    265 	 * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
    266 	 */
    267 
    268 	if (flags & MAP_FIXED) {
    269 
    270 		/* ensure address and file offset are aligned properly */
    271 		addr -= pageoff;
    272 		if (addr & PAGE_MASK)
    273 			return (EINVAL);
    274 
    275 		if (VM_MAXUSER_ADDRESS > 0 &&
    276 		    (addr + size) > VM_MAXUSER_ADDRESS)
    277 			return (EINVAL);
    278 		if (vm_min_address > 0 && addr < vm_min_address)
    279 			return (EINVAL);
    280 		if (addr > addr + size)
    281 			return (EINVAL);		/* no wrapping! */
    282 
    283 	} else {
    284 
    285 		/*
    286 		 * not fixed: make sure we skip over the largest possible heap.
    287 		 * we will refine our guess later (e.g. to account for VAC, etc)
    288 		 */
    289 		if (addr < round_page(p->p_vmspace->vm_daddr + MAXDSIZ))
    290 			addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
    291 	}
    292 
    293 	/*
    294 	 * check for file mappings (i.e. not anonymous) and verify file.
    295 	 */
    296 
    297 	if ((flags & MAP_ANON) == 0) {
    298 
    299 		if (fd < 0 || fd >= fdp->fd_nfiles)
    300 			return(EBADF);		/* failed range check? */
    301 		fp = fdp->fd_ofiles[fd];	/* convert to file pointer */
    302 		if (fp == NULL)
    303 			return(EBADF);
    304 
    305 		if (fp->f_type != DTYPE_VNODE)
    306 			return (ENODEV);		/* only mmap vnodes! */
    307 		vp = (struct vnode *)fp->f_data;	/* convert to vnode */
    308 
    309 		if (vp->v_type != VREG && vp->v_type != VCHR &&
    310 		    vp->v_type != VBLK)
    311 			return (ENODEV);  /* only REG/CHR/BLK support mmap */
    312 
    313 		/* special case: catch SunOS style /dev/zero */
    314 		if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
    315 			flags |= MAP_ANON;
    316 			goto is_anon;
    317 		}
    318 
    319 		/*
    320 		 * Old programs may not select a specific sharing type, so
    321 		 * default to an appropriate one.
    322 		 *
    323 		 * XXX: how does MAP_ANON fit in the picture?
    324 		 */
    325 		if ((flags & (MAP_SHARED|MAP_PRIVATE|MAP_COPY)) == 0) {
    326 #if defined(DEBUG)
    327 			printf("WARNING: defaulted mmap() share type to "
    328 			   "%s (pid %d comm %s)\n", vp->v_type == VCHR ?
    329 			   "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
    330 			    p->p_comm);
    331 #endif
    332 			if (vp->v_type == VCHR)
    333 				flags |= MAP_SHARED;	/* for a device */
    334 			else
    335 				flags |= MAP_PRIVATE;	/* for a file */
    336 		}
    337 
    338 		/*
    339 		 * MAP_PRIVATE device mappings don't make sense (and aren't
    340 		 * supported anyway).  However, some programs rely on this,
    341 		 * so just change it to MAP_SHARED.
    342 		 */
    343 		if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
    344 #if defined(DIAGNOSTIC)
    345 			printf("WARNING: converted MAP_PRIVATE device mapping "
    346 			    "to MAP_SHARED (pid %d comm %s)\n", p->p_pid,
    347 			    p->p_comm);
    348 #endif
    349 			flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
    350 		}
    351 
    352 		/*
    353 		 * now check protection
    354 		 */
    355 
    356 		maxprot = VM_PROT_EXECUTE;
    357 
    358 		/* check read access */
    359 		if (fp->f_flag & FREAD)
    360 			maxprot |= VM_PROT_READ;
    361 		else if (prot & PROT_READ)
    362 			return (EACCES);
    363 
    364 		/* check write access, shared case first */
    365 		if (flags & MAP_SHARED) {
    366 			/*
    367 			 * if the file is writable, only add PROT_WRITE to
    368 			 * maxprot if the file is not immutable, append-only.
    369 			 * otherwise, if we have asked for PROT_WRITE, return
    370 			 * EPERM.
    371 			 */
    372 			if (fp->f_flag & FWRITE) {
    373 				if ((error =
    374 				    VOP_GETATTR(vp, &va, p->p_ucred, p)))
    375 					return (error);
    376 				if ((va.va_flags & (IMMUTABLE|APPEND)) == 0)
    377 					maxprot |= VM_PROT_WRITE;
    378 				else if (prot & PROT_WRITE)
    379 					return (EPERM);
    380 			}
    381 			else if (prot & PROT_WRITE)
    382 				return (EACCES);
    383 		} else {
    384 			/* MAP_PRIVATE mappings can always write to */
    385 			maxprot |= VM_PROT_WRITE;
    386 		}
    387 
    388 		/*
    389 		 * set handle to vnode
    390 		 */
    391 
    392 		handle = (caddr_t)vp;
    393 
    394 	} else {		/* MAP_ANON case */
    395 
    396 		if (fd != -1)
    397 			return (EINVAL);
    398 
    399 is_anon:		/* label for SunOS style /dev/zero */
    400 		handle = NULL;
    401 		maxprot = VM_PROT_ALL;
    402 		pos = 0;
    403 	}
    404 
    405 	/*
    406 	 * now let kernel internal function uvm_mmap do the work.
    407 	 */
    408 
    409 	error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
    410 	    flags, handle, pos);
    411 
    412 	if (error == 0)
    413 		/* remember to add offset */
    414 		*retval = (register_t)(addr + pageoff);
    415 
    416 	return (error);
    417 }
    418 
    419 /*
    420  * sys___msync13: the msync system call (a front-end for flush)
    421  */
    422 
    423 int
    424 sys___msync13(p, v, retval)
    425 	struct proc *p;
    426 	void *v;
    427 	register_t *retval;
    428 {
    429 	struct sys___msync13_args /* {
    430 		syscallarg(caddr_t) addr;
    431 		syscallarg(size_t) len;
    432 		syscallarg(int) flags;
    433 	} */ *uap = v;
    434 	vaddr_t addr;
    435 	vsize_t size, pageoff;
    436 	vm_map_t map;
    437 	int rv, flags, uvmflags;
    438 
    439 	/*
    440 	 * extract syscall args from the uap
    441 	 */
    442 
    443 	addr = (vaddr_t)SCARG(uap, addr);
    444 	size = (vsize_t)SCARG(uap, len);
    445 	flags = SCARG(uap, flags);
    446 
    447 	/* sanity check flags */
    448 	if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
    449 			(flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
    450 			(flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
    451 	  return (EINVAL);
    452 	if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
    453 	  flags |= MS_SYNC;
    454 
    455 	/*
    456 	 * align the address to a page boundary, and adjust the size accordingly
    457 	 */
    458 
    459 	pageoff = (addr & PAGE_MASK);
    460 	addr -= pageoff;
    461 	size += pageoff;
    462 	size = (vsize_t) round_page(size);
    463 
    464 	/* disallow wrap-around. */
    465 	if (addr + size < addr)
    466 		return (EINVAL);
    467 
    468 	/*
    469 	 * get map
    470 	 */
    471 
    472 	map = &p->p_vmspace->vm_map;
    473 
    474 	/*
    475 	 * XXXCDC: do we really need this semantic?
    476 	 *
    477 	 * XXX Gak!  If size is zero we are supposed to sync "all modified
    478 	 * pages with the region containing addr".  Unfortunately, we
    479 	 * don't really keep track of individual mmaps so we approximate
    480 	 * by flushing the range of the map entry containing addr.
    481 	 * This can be incorrect if the region splits or is coalesced
    482 	 * with a neighbor.
    483 	 */
    484 	if (size == 0) {
    485 		vm_map_entry_t entry;
    486 
    487 		vm_map_lock_read(map);
    488 		rv = uvm_map_lookup_entry(map, addr, &entry);
    489 		if (rv == TRUE) {
    490 			addr = entry->start;
    491 			size = entry->end - entry->start;
    492 		}
    493 		vm_map_unlock_read(map);
    494 		if (rv == FALSE)
    495 			return (EINVAL);
    496 	}
    497 
    498 	/*
    499 	 * translate MS_ flags into PGO_ flags
    500 	 */
    501 	uvmflags = (flags & MS_INVALIDATE) ? PGO_FREE : 0;
    502 	if (flags & MS_SYNC)
    503 		uvmflags |= PGO_SYNCIO;
    504 	else
    505 		uvmflags |= PGO_SYNCIO;	 /* XXXCDC: force sync for now! */
    506 
    507 	/*
    508 	 * doit!
    509 	 */
    510 	rv = uvm_map_clean(map, addr, addr+size, uvmflags);
    511 
    512 	/*
    513 	 * and return...
    514 	 */
    515 	switch (rv) {
    516 	case KERN_SUCCESS:
    517 		return(0);
    518 	case KERN_INVALID_ADDRESS:
    519 		return (ENOMEM);
    520 	case KERN_FAILURE:
    521 		return (EIO);
    522 	case KERN_PAGES_LOCKED:	/* XXXCDC: uvm doesn't return this */
    523 		return (EBUSY);
    524 	default:
    525 		return (EINVAL);
    526 	}
    527 	/*NOTREACHED*/
    528 }
    529 
    530 /*
    531  * sys_munmap: unmap a users memory
    532  */
    533 
    534 int
    535 sys_munmap(p, v, retval)
    536 	register struct proc *p;
    537 	void *v;
    538 	register_t *retval;
    539 {
    540 	register struct sys_munmap_args /* {
    541 		syscallarg(caddr_t) addr;
    542 		syscallarg(size_t) len;
    543 	} */ *uap = v;
    544 	vaddr_t addr;
    545 	vsize_t size, pageoff;
    546 	vm_map_t map;
    547 	vaddr_t vm_min_address = VM_MIN_ADDRESS;
    548 	struct vm_map_entry *dead_entries;
    549 
    550 	/*
    551 	 * get syscall args...
    552 	 */
    553 
    554 	addr = (vaddr_t) SCARG(uap, addr);
    555 	size = (vsize_t) SCARG(uap, len);
    556 
    557 	/*
    558 	 * align the address to a page boundary, and adjust the size accordingly
    559 	 */
    560 
    561 	pageoff = (addr & PAGE_MASK);
    562 	addr -= pageoff;
    563 	size += pageoff;
    564 	size = (vsize_t) round_page(size);
    565 
    566 	if ((int)size < 0)
    567 		return (EINVAL);
    568 	if (size == 0)
    569 		return (0);
    570 
    571 	/*
    572 	 * Check for illegal addresses.  Watch out for address wrap...
    573 	 * Note that VM_*_ADDRESS are not constants due to casts (argh).
    574 	 */
    575 	if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
    576 		return (EINVAL);
    577 	if (vm_min_address > 0 && addr < vm_min_address)
    578 		return (EINVAL);
    579 	if (addr > addr + size)
    580 		return (EINVAL);
    581 	map = &p->p_vmspace->vm_map;
    582 
    583 
    584 	vm_map_lock(map);	/* lock map so we can checkprot */
    585 
    586 	/*
    587 	 * interesting system call semantic: make sure entire range is
    588 	 * allocated before allowing an unmap.
    589 	 */
    590 
    591 	if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
    592 		vm_map_unlock(map);
    593 		return (EINVAL);
    594 	}
    595 
    596 	/*
    597 	 * doit!
    598 	 */
    599 	(void) uvm_unmap_remove(map, addr, addr + size, 0, &dead_entries);
    600 
    601 	vm_map_unlock(map);	/* and unlock */
    602 
    603 	if (dead_entries != NULL)
    604 		uvm_unmap_detach(dead_entries, 0);
    605 
    606 	return (0);
    607 }
    608 
    609 /*
    610  * sys_mprotect: the mprotect system call
    611  */
    612 
    613 int
    614 sys_mprotect(p, v, retval)
    615 	struct proc *p;
    616 	void *v;
    617 	register_t *retval;
    618 {
    619 	struct sys_mprotect_args /* {
    620 		syscallarg(caddr_t) addr;
    621 		syscallarg(int) len;
    622 		syscallarg(int) prot;
    623 	} */ *uap = v;
    624 	vaddr_t addr;
    625 	vsize_t size, pageoff;
    626 	vm_prot_t prot;
    627 	int rv;
    628 
    629 	/*
    630 	 * extract syscall args from uap
    631 	 */
    632 
    633 	addr = (vaddr_t)SCARG(uap, addr);
    634 	size = (vsize_t)SCARG(uap, len);
    635 	prot = SCARG(uap, prot) & VM_PROT_ALL;
    636 
    637 	/*
    638 	 * align the address to a page boundary, and adjust the size accordingly
    639 	 */
    640 	pageoff = (addr & PAGE_MASK);
    641 	addr -= pageoff;
    642 	size += pageoff;
    643 	size = (vsize_t) round_page(size);
    644 	if ((int)size < 0)
    645 		return (EINVAL);
    646 
    647 	/*
    648 	 * doit
    649 	 */
    650 
    651 	rv = uvm_map_protect(&p->p_vmspace->vm_map,
    652 			   addr, addr+size, prot, FALSE);
    653 
    654 	if (rv == KERN_SUCCESS)
    655 		return (0);
    656 	if (rv == KERN_PROTECTION_FAILURE)
    657 		return (EACCES);
    658 	return (EINVAL);
    659 }
    660 
    661 /*
    662  * sys_minherit: the minherit system call
    663  */
    664 
    665 int
    666 sys_minherit(p, v, retval)
    667 	struct proc *p;
    668 	void *v;
    669 	register_t *retval;
    670 {
    671 	struct sys_minherit_args /* {
    672 		syscallarg(caddr_t) addr;
    673 		syscallarg(int) len;
    674 		syscallarg(int) inherit;
    675 	} */ *uap = v;
    676 	vaddr_t addr;
    677 	vsize_t size, pageoff;
    678 	register vm_inherit_t inherit;
    679 
    680 	addr = (vaddr_t)SCARG(uap, addr);
    681 	size = (vsize_t)SCARG(uap, len);
    682 	inherit = SCARG(uap, inherit);
    683 	/*
    684 	 * align the address to a page boundary, and adjust the size accordingly
    685 	 */
    686 
    687 	pageoff = (addr & PAGE_MASK);
    688 	addr -= pageoff;
    689 	size += pageoff;
    690 	size = (vsize_t) round_page(size);
    691 
    692 	if ((int)size < 0)
    693 		return (EINVAL);
    694 
    695 	switch (uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
    696 			 inherit)) {
    697 	case KERN_SUCCESS:
    698 		return (0);
    699 	case KERN_PROTECTION_FAILURE:
    700 		return (EACCES);
    701 	}
    702 	return (EINVAL);
    703 }
    704 
    705 /*
    706  * sys_mlock: memory lock
    707  */
    708 
    709 int
    710 sys_mlock(p, v, retval)
    711 	struct proc *p;
    712 	void *v;
    713 	register_t *retval;
    714 {
    715 	struct sys_mlock_args /* {
    716 		syscallarg(const void *) addr;
    717 		syscallarg(size_t) len;
    718 	} */ *uap = v;
    719 	vaddr_t addr;
    720 	vsize_t size, pageoff;
    721 	int error;
    722 
    723 	/*
    724 	 * extract syscall args from uap
    725 	 */
    726 	addr = (vaddr_t)SCARG(uap, addr);
    727 	size = (vsize_t)SCARG(uap, len);
    728 
    729 	/*
    730 	 * align the address to a page boundary and adjust the size accordingly
    731 	 */
    732 	pageoff = (addr & PAGE_MASK);
    733 	addr -= pageoff;
    734 	size += pageoff;
    735 	size = (vsize_t) round_page(size);
    736 
    737 	/* disallow wrap-around. */
    738 	if (addr + (int)size < addr)
    739 		return (EINVAL);
    740 
    741 	if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
    742 		return (EAGAIN);
    743 
    744 #ifdef pmap_wired_count
    745 	if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
    746 			p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
    747 		return (EAGAIN);
    748 #else
    749 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    750 		return (error);
    751 #endif
    752 
    753 	error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE);
    754 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
    755 }
    756 
    757 /*
    758  * sys_munlock: unlock wired pages
    759  */
    760 
    761 int
    762 sys_munlock(p, v, retval)
    763 	struct proc *p;
    764 	void *v;
    765 	register_t *retval;
    766 {
    767 	struct sys_munlock_args /* {
    768 		syscallarg(const void *) addr;
    769 		syscallarg(size_t) len;
    770 	} */ *uap = v;
    771 	vaddr_t addr;
    772 	vsize_t size, pageoff;
    773 	int error;
    774 
    775 	/*
    776 	 * extract syscall args from uap
    777 	 */
    778 
    779 	addr = (vaddr_t)SCARG(uap, addr);
    780 	size = (vsize_t)SCARG(uap, len);
    781 
    782 	/*
    783 	 * align the address to a page boundary, and adjust the size accordingly
    784 	 */
    785 	pageoff = (addr & PAGE_MASK);
    786 	addr -= pageoff;
    787 	size += pageoff;
    788 	size = (vsize_t) round_page(size);
    789 
    790 	/* disallow wrap-around. */
    791 	if (addr + (int)size < addr)
    792 		return (EINVAL);
    793 
    794 #ifndef pmap_wired_count
    795 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    796 		return (error);
    797 #endif
    798 
    799 	error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE);
    800 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
    801 }
    802 
    803 /*
    804  * uvm_mmap: internal version of mmap
    805  *
    806  * - used by sys_mmap, exec, and sysv shm
    807  * - handle is a vnode pointer or NULL for MAP_ANON (XXX: not true,
    808  *	sysv shm uses "named anonymous memory")
    809  * - caller must page-align the file offset
    810  */
    811 
    812 int
    813 uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff)
    814 	vm_map_t map;
    815 	vaddr_t *addr;
    816 	vsize_t size;
    817 	vm_prot_t prot, maxprot;
    818 	int flags;
    819 	caddr_t handle;		/* XXX: VNODE? */
    820 	vaddr_t foff;
    821 {
    822 	struct uvm_object *uobj;
    823 	struct vnode *vp;
    824 	int retval;
    825 	int advice = UVM_ADV_NORMAL;
    826 	uvm_flag_t uvmflag = 0;
    827 
    828 	/*
    829 	 * check params
    830 	 */
    831 
    832 	if (size == 0)
    833 		return(0);
    834 	if (foff & PAGE_MASK)
    835 		return(EINVAL);
    836 	if ((prot & maxprot) != prot)
    837 		return(EINVAL);
    838 
    839 	/*
    840 	 * for non-fixed mappings, round off the suggested address.
    841 	 * for fixed mappings, check alignment and zap old mappings.
    842 	 */
    843 
    844 	if ((flags & MAP_FIXED) == 0) {
    845 		*addr = round_page(*addr);	/* round */
    846 	} else {
    847 
    848 		if (*addr & PAGE_MASK)
    849 			return(EINVAL);
    850 		uvmflag |= UVM_FLAG_FIXED;
    851 		(void) uvm_unmap(map, *addr, *addr + size, 0);	/* zap! */
    852 	}
    853 
    854 	/*
    855 	 * handle anon vs. non-anon mappings.   for non-anon mappings attach
    856 	 * to underlying vm object.
    857 	 */
    858 
    859 	if (flags & MAP_ANON) {
    860 
    861 		foff = UVM_UNKNOWN_OFFSET;
    862 		uobj = NULL;
    863 		if ((flags & MAP_SHARED) == 0)
    864 			/* XXX: defer amap create */
    865 			uvmflag |= UVM_FLAG_COPYONW;
    866 		else
    867 			/* shared: create amap now */
    868 			uvmflag |= UVM_FLAG_OVERLAY;
    869 
    870 	} else {
    871 
    872 		vp = (struct vnode *) handle;	/* get vnode */
    873 		if (vp->v_type != VCHR) {
    874 			uobj = uvn_attach((void *) vp, (flags & MAP_SHARED) ?
    875 			   maxprot : (maxprot & ~VM_PROT_WRITE));
    876 
    877 			/*
    878 			 * XXXCDC: hack from old code
    879 			 * don't allow vnodes which have been mapped
    880 			 * shared-writeable to persist [forces them to be
    881 			 * flushed out when last reference goes].
    882 			 * XXXCDC: interesting side effect: avoids a bug.
    883 			 * note that in WRITE [ufs_readwrite.c] that we
    884 			 * allocate buffer, uncache, and then do the write.
    885 			 * the problem with this is that if the uncache causes
    886 			 * VM data to be flushed to the same area of the file
    887 			 * we are writing to... in that case we've got the
    888 			 * buffer locked and our process goes to sleep forever.
    889 			 *
    890 			 * XXXCDC: checking maxprot protects us from the
    891 			 * "persistbug" program but this is not a long term
    892 			 * solution.
    893 			 *
    894 			 * XXXCDC: we don't bother calling uncache with the vp
    895 			 * VOP_LOCKed since we know that we are already
    896 			 * holding a valid reference to the uvn (from the
    897 			 * uvn_attach above), and thus it is impossible for
    898 			 * the uncache to kill the uvn and trigger I/O.
    899 			 */
    900 			if (flags & MAP_SHARED) {
    901 				if ((prot & VM_PROT_WRITE) ||
    902 				    (maxprot & VM_PROT_WRITE)) {
    903 					uvm_vnp_uncache(vp);
    904 				}
    905 			}
    906 
    907 		} else {
    908 			uobj = udv_attach((void *) &vp->v_rdev,
    909 			    (flags & MAP_SHARED) ?
    910 			    maxprot : (maxprot & ~VM_PROT_WRITE));
    911 			advice = UVM_ADV_RANDOM;
    912 		}
    913 
    914 		if (uobj == NULL)
    915 			return((vp->v_type == VREG) ? ENOMEM : EINVAL);
    916 
    917 		if ((flags & MAP_SHARED) == 0)
    918 			uvmflag |= UVM_FLAG_COPYONW;
    919 	}
    920 
    921 	/*
    922 	 * set up mapping flags
    923 	 */
    924 
    925 	uvmflag = UVM_MAPFLAG(prot, maxprot,
    926 			(flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
    927 			advice, uvmflag);
    928 
    929 	/*
    930 	 * do it!
    931 	 */
    932 
    933 	retval = uvm_map(map, addr, size, uobj, foff, uvmflag);
    934 
    935 	if (retval == KERN_SUCCESS)
    936 		return(0);
    937 
    938 	/*
    939 	 * errors: first detach from the uobj, if any.
    940 	 */
    941 
    942 	if (uobj)
    943 		uobj->pgops->pgo_detach(uobj);
    944 
    945 	switch (retval) {
    946 	case KERN_INVALID_ADDRESS:
    947 	case KERN_NO_SPACE:
    948 		return(ENOMEM);
    949 	case KERN_PROTECTION_FAILURE:
    950 		return(EACCES);
    951 	}
    952 	return(EINVAL);
    953 }
    954