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