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