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