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