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