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