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