Home | History | Annotate | Line # | Download | only in uvm
uvm_mmap.c revision 1.173
      1  1.173      maxv /*	$NetBSD: uvm_mmap.c,v 1.173 2019/08/06 08:10:27 maxv 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.173      maxv __KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.173 2019/08/06 08:10:27 maxv Exp $");
     50   1.80  jdolecek 
     51   1.80  jdolecek #include "opt_compat_netbsd.h"
     52   1.97      elad #include "opt_pax.h"
     53   1.60     lukem 
     54  1.150       chs #include <sys/types.h>
     55    1.1       mrg #include <sys/file.h>
     56    1.1       mrg #include <sys/filedesc.h>
     57    1.1       mrg #include <sys/resourcevar.h>
     58    1.1       mrg #include <sys/mman.h>
     59   1.97      elad #include <sys/pax.h>
     60    1.1       mrg 
     61    1.1       mrg #include <sys/syscallargs.h>
     62    1.1       mrg 
     63    1.1       mrg #include <uvm/uvm.h>
     64    1.1       mrg #include <uvm/uvm_device.h>
     65    1.1       mrg 
     66  1.150       chs static int uvm_mmap(struct vm_map *, vaddr_t *, vsize_t, vm_prot_t, vm_prot_t,
     67  1.161      maxv     int, int, struct uvm_object *, voff_t, vsize_t);
     68    1.1       mrg 
     69  1.115      yamt static int
     70  1.171  christos range_test(const struct vm_map *map, vaddr_t addr, vsize_t size, bool ismmap)
     71  1.115      yamt {
     72  1.158    martin 	vaddr_t vm_min_address = vm_map_min(map);
     73  1.158    martin 	vaddr_t vm_max_address = vm_map_max(map);
     74  1.115      yamt 	vaddr_t eaddr = addr + size;
     75  1.145    martin 	int res = 0;
     76  1.115      yamt 
     77  1.115      yamt 	if (addr < vm_min_address)
     78  1.115      yamt 		return EINVAL;
     79  1.115      yamt 	if (eaddr > vm_max_address)
     80  1.115      yamt 		return ismmap ? EFBIG : EINVAL;
     81  1.115      yamt 	if (addr > eaddr) /* no wrapping! */
     82  1.115      yamt 		return ismmap ? EOVERFLOW : EINVAL;
     83  1.145    martin 
     84  1.145    martin #ifdef MD_MMAP_RANGE_TEST
     85  1.145    martin 	res = MD_MMAP_RANGE_TEST(addr, eaddr);
     86  1.145    martin #endif
     87  1.145    martin 
     88  1.145    martin 	return res;
     89  1.115      yamt }
     90  1.110  christos 
     91    1.1       mrg /*
     92  1.171  christos  * align the address to a page boundary, and adjust the size accordingly
     93  1.171  christos  */
     94  1.171  christos static int
     95  1.171  christos round_and_check(const struct vm_map *map, vaddr_t *addr, vsize_t *size)
     96  1.171  christos {
     97  1.171  christos 	const vsize_t pageoff = (vsize_t)(*addr & PAGE_MASK);
     98  1.171  christos 
     99  1.171  christos 	*addr -= pageoff;
    100  1.171  christos 
    101  1.171  christos 	if (*size != 0) {
    102  1.171  christos 		*size += pageoff;
    103  1.171  christos 		*size = (vsize_t)round_page(*size);
    104  1.171  christos 	} else if (*addr + *size < *addr) {
    105  1.171  christos 		return ENOMEM;
    106  1.171  christos 	}
    107  1.171  christos 
    108  1.171  christos 	return range_test(map, *addr, *size, false);
    109  1.171  christos }
    110  1.171  christos 
    111  1.171  christos /*
    112    1.1       mrg  * sys_mincore: determine if pages are in core or not.
    113    1.1       mrg  */
    114    1.1       mrg 
    115    1.1       mrg /* ARGSUSED */
    116    1.6       mrg int
    117  1.129      yamt sys_mincore(struct lwp *l, const struct sys_mincore_args *uap,
    118  1.129      yamt     register_t *retval)
    119    1.1       mrg {
    120  1.119       dsl 	/* {
    121   1.22   thorpej 		syscallarg(void *) addr;
    122   1.20       mrg 		syscallarg(size_t) len;
    123   1.20       mrg 		syscallarg(char *) vec;
    124  1.119       dsl 	} */
    125   1.67   thorpej 	struct proc *p = l->l_proc;
    126   1.56       chs 	struct vm_page *pg;
    127   1.22   thorpej 	char *vec, pgi;
    128   1.22   thorpej 	struct uvm_object *uobj;
    129   1.22   thorpej 	struct vm_amap *amap;
    130   1.22   thorpej 	struct vm_anon *anon;
    131   1.53       chs 	struct vm_map_entry *entry;
    132   1.22   thorpej 	vaddr_t start, end, lim;
    133   1.53       chs 	struct vm_map *map;
    134   1.22   thorpej 	vsize_t len;
    135  1.173      maxv 	int error = 0;
    136  1.173      maxv 	size_t npgs;
    137   1.22   thorpej 
    138   1.22   thorpej 	map = &p->p_vmspace->vm_map;
    139   1.22   thorpej 
    140   1.22   thorpej 	start = (vaddr_t)SCARG(uap, addr);
    141   1.22   thorpej 	len = SCARG(uap, len);
    142   1.22   thorpej 	vec = SCARG(uap, vec);
    143   1.22   thorpej 
    144   1.22   thorpej 	if (start & PAGE_MASK)
    145  1.161      maxv 		return EINVAL;
    146   1.22   thorpej 	len = round_page(len);
    147   1.22   thorpej 	end = start + len;
    148   1.22   thorpej 	if (end <= start)
    149  1.161      maxv 		return EINVAL;
    150   1.22   thorpej 
    151   1.22   thorpej 	/*
    152   1.22   thorpej 	 * Lock down vec, so our returned status isn't outdated by
    153   1.22   thorpej 	 * storing the status byte for a page.
    154   1.22   thorpej 	 */
    155   1.50       chs 
    156   1.62       chs 	npgs = len >> PAGE_SHIFT;
    157  1.100       chs 	error = uvm_vslock(p->p_vmspace, vec, npgs, VM_PROT_WRITE);
    158   1.62       chs 	if (error) {
    159   1.62       chs 		return error;
    160   1.62       chs 	}
    161   1.22   thorpej 	vm_map_lock_read(map);
    162   1.22   thorpej 
    163  1.107   thorpej 	if (uvm_map_lookup_entry(map, start, &entry) == false) {
    164   1.22   thorpej 		error = ENOMEM;
    165   1.22   thorpej 		goto out;
    166   1.22   thorpej 	}
    167   1.22   thorpej 
    168   1.22   thorpej 	for (/* nothing */;
    169   1.22   thorpej 	     entry != &map->header && entry->start < end;
    170   1.22   thorpej 	     entry = entry->next) {
    171   1.49       chs 		KASSERT(!UVM_ET_ISSUBMAP(entry));
    172   1.49       chs 		KASSERT(start >= entry->start);
    173   1.49       chs 
    174   1.22   thorpej 		/* Make sure there are no holes. */
    175   1.22   thorpej 		if (entry->end < end &&
    176   1.22   thorpej 		     (entry->next == &map->header ||
    177   1.22   thorpej 		      entry->next->start > entry->end)) {
    178   1.22   thorpej 			error = ENOMEM;
    179   1.22   thorpej 			goto out;
    180   1.22   thorpej 		}
    181    1.6       mrg 
    182   1.22   thorpej 		lim = end < entry->end ? end : entry->end;
    183   1.22   thorpej 
    184   1.22   thorpej 		/*
    185   1.31   thorpej 		 * Special case for objects with no "real" pages.  Those
    186   1.31   thorpej 		 * are always considered resident (mapped devices).
    187   1.22   thorpej 		 */
    188   1.50       chs 
    189   1.22   thorpej 		if (UVM_ET_ISOBJ(entry)) {
    190   1.49       chs 			KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj));
    191   1.79      yamt 			if (UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
    192   1.22   thorpej 				for (/* nothing */; start < lim;
    193   1.22   thorpej 				     start += PAGE_SIZE, vec++)
    194  1.172   thorpej 					ustore_char(vec, 1);
    195   1.22   thorpej 				continue;
    196   1.22   thorpej 			}
    197   1.22   thorpej 		}
    198   1.22   thorpej 
    199  1.132  uebayasi 		amap = entry->aref.ar_amap;	/* upper layer */
    200  1.132  uebayasi 		uobj = entry->object.uvm_obj;	/* lower layer */
    201   1.22   thorpej 
    202   1.22   thorpej 		if (amap != NULL)
    203   1.22   thorpej 			amap_lock(amap);
    204   1.22   thorpej 		if (uobj != NULL)
    205  1.136     rmind 			mutex_enter(uobj->vmobjlock);
    206   1.22   thorpej 
    207   1.22   thorpej 		for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) {
    208   1.22   thorpej 			pgi = 0;
    209   1.22   thorpej 			if (amap != NULL) {
    210  1.132  uebayasi 				/* Check the upper layer first. */
    211   1.22   thorpej 				anon = amap_lookup(&entry->aref,
    212   1.22   thorpej 				    start - entry->start);
    213   1.22   thorpej 				/* Don't need to lock anon here. */
    214   1.91      yamt 				if (anon != NULL && anon->an_page != NULL) {
    215   1.50       chs 
    216   1.22   thorpej 					/*
    217   1.22   thorpej 					 * Anon has the page for this entry
    218   1.22   thorpej 					 * offset.
    219   1.22   thorpej 					 */
    220   1.50       chs 
    221   1.22   thorpej 					pgi = 1;
    222   1.22   thorpej 				}
    223   1.22   thorpej 			}
    224   1.22   thorpej 			if (uobj != NULL && pgi == 0) {
    225  1.132  uebayasi 				/* Check the lower layer. */
    226   1.56       chs 				pg = uvm_pagelookup(uobj,
    227   1.22   thorpej 				    entry->offset + (start - entry->start));
    228   1.56       chs 				if (pg != NULL) {
    229   1.50       chs 
    230   1.22   thorpej 					/*
    231   1.22   thorpej 					 * Object has the page for this entry
    232   1.22   thorpej 					 * offset.
    233   1.22   thorpej 					 */
    234   1.50       chs 
    235   1.22   thorpej 					pgi = 1;
    236   1.22   thorpej 				}
    237   1.22   thorpej 			}
    238  1.172   thorpej 			(void) ustore_char(vec, pgi);
    239   1.22   thorpej 		}
    240   1.22   thorpej 		if (uobj != NULL)
    241  1.136     rmind 			mutex_exit(uobj->vmobjlock);
    242   1.22   thorpej 		if (amap != NULL)
    243   1.22   thorpej 			amap_unlock(amap);
    244   1.22   thorpej 	}
    245   1.22   thorpej 
    246   1.22   thorpej  out:
    247   1.22   thorpej 	vm_map_unlock_read(map);
    248  1.100       chs 	uvm_vsunlock(p->p_vmspace, SCARG(uap, vec), npgs);
    249  1.161      maxv 	return error;
    250    1.1       mrg }
    251    1.1       mrg 
    252    1.1       mrg /*
    253    1.1       mrg  * sys_mmap: mmap system call.
    254    1.1       mrg  *
    255   1.64    atatat  * => file offset and address may not be page aligned
    256    1.1       mrg  *    - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
    257    1.1       mrg  *    - if address isn't page aligned the mapping starts at trunc_page(addr)
    258    1.1       mrg  *      and the return value is adjusted up by the page offset.
    259    1.1       mrg  */
    260    1.1       mrg 
    261    1.6       mrg int
    262  1.119       dsl sys_mmap(struct lwp *l, const struct sys_mmap_args *uap, register_t *retval)
    263    1.6       mrg {
    264  1.119       dsl 	/* {
    265  1.108  christos 		syscallarg(void *) addr;
    266    1.6       mrg 		syscallarg(size_t) len;
    267    1.6       mrg 		syscallarg(int) prot;
    268    1.6       mrg 		syscallarg(int) flags;
    269    1.6       mrg 		syscallarg(int) fd;
    270    1.6       mrg 		syscallarg(long) pad;
    271    1.6       mrg 		syscallarg(off_t) pos;
    272  1.119       dsl 	} */
    273   1.67   thorpej 	struct proc *p = l->l_proc;
    274   1.12       eeh 	vaddr_t addr;
    275    1.6       mrg 	off_t pos;
    276  1.152   mlelstv 	vsize_t size, pageoff, newsize;
    277  1.164     joerg 	vm_prot_t prot, maxprot, extraprot;
    278  1.150       chs 	int flags, fd, advice;
    279  1.110  christos 	vaddr_t defaddr;
    280  1.116        ad 	struct file *fp = NULL;
    281  1.150       chs 	struct uvm_object *uobj;
    282    1.6       mrg 	int error;
    283  1.120  christos #ifdef PAX_ASLR
    284  1.120  christos 	vaddr_t orig_addr;
    285  1.120  christos #endif /* PAX_ASLR */
    286    1.6       mrg 
    287    1.6       mrg 	/*
    288    1.6       mrg 	 * first, extract syscall args from the uap.
    289    1.6       mrg 	 */
    290    1.6       mrg 
    291   1.50       chs 	addr = (vaddr_t)SCARG(uap, addr);
    292   1.50       chs 	size = (vsize_t)SCARG(uap, len);
    293    1.6       mrg 	prot = SCARG(uap, prot) & VM_PROT_ALL;
    294  1.164     joerg 	extraprot = PROT_MPROTECT_EXTRACT(SCARG(uap, prot));
    295    1.6       mrg 	flags = SCARG(uap, flags);
    296    1.6       mrg 	fd = SCARG(uap, fd);
    297    1.6       mrg 	pos = SCARG(uap, pos);
    298    1.6       mrg 
    299  1.120  christos #ifdef PAX_ASLR
    300  1.120  christos 	orig_addr = addr;
    301  1.120  christos #endif /* PAX_ASLR */
    302  1.120  christos 
    303   1.24   thorpej 	if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE))
    304  1.161      maxv 		return EINVAL;
    305   1.24   thorpej 
    306   1.24   thorpej 	/*
    307    1.6       mrg 	 * align file position and save offset.  adjust size.
    308    1.6       mrg 	 */
    309    1.6       mrg 
    310    1.6       mrg 	pageoff = (pos & PAGE_MASK);
    311  1.152   mlelstv 	pos    -= pageoff;
    312  1.152   mlelstv 	newsize = size + pageoff;		/* add offset */
    313  1.152   mlelstv 	newsize = (vsize_t)round_page(newsize);	/* round up */
    314  1.152   mlelstv 
    315  1.152   mlelstv 	if (newsize < size)
    316  1.161      maxv 		return ENOMEM;
    317  1.152   mlelstv 	size = newsize;
    318    1.6       mrg 
    319    1.6       mrg 	/*
    320   1.51       chs 	 * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
    321    1.6       mrg 	 */
    322    1.6       mrg 	if (flags & MAP_FIXED) {
    323    1.6       mrg 		/* ensure address and file offset are aligned properly */
    324    1.6       mrg 		addr -= pageoff;
    325    1.6       mrg 		if (addr & PAGE_MASK)
    326  1.161      maxv 			return EINVAL;
    327    1.6       mrg 
    328  1.158    martin 		error = range_test(&p->p_vmspace->vm_map, addr, size, true);
    329  1.150       chs 		if (error) {
    330  1.115      yamt 			return error;
    331  1.150       chs 		}
    332   1.75  christos 	} else if (addr == 0 || !(flags & MAP_TRYFIXED)) {
    333    1.6       mrg 		/*
    334   1.68    atatat 		 * not fixed: make sure we skip over the largest
    335   1.68    atatat 		 * possible heap for non-topdown mapping arrangements.
    336   1.68    atatat 		 * we will refine our guess later (e.g. to account for
    337   1.68    atatat 		 * VAC, etc)
    338    1.6       mrg 		 */
    339   1.46       chs 
    340   1.89      fvdl 		defaddr = p->p_emul->e_vm_default_addr(p,
    341  1.154    martin 		    (vaddr_t)p->p_vmspace->vm_daddr, size,
    342  1.154    martin 		    p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);
    343   1.89      fvdl 
    344  1.161      maxv 		if (addr == 0 || !(p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
    345   1.89      fvdl 			addr = MAX(addr, defaddr);
    346   1.68    atatat 		else
    347   1.89      fvdl 			addr = MIN(addr, defaddr);
    348    1.6       mrg 	}
    349    1.6       mrg 
    350    1.6       mrg 	/*
    351    1.6       mrg 	 * check for file mappings (i.e. not anonymous) and verify file.
    352    1.6       mrg 	 */
    353    1.6       mrg 
    354  1.150       chs 	advice = UVM_ADV_NORMAL;
    355    1.6       mrg 	if ((flags & MAP_ANON) == 0) {
    356  1.122        ad 		if ((fp = fd_getfile(fd)) == NULL)
    357  1.161      maxv 			return EBADF;
    358  1.150       chs 
    359  1.150       chs 		if (fp->f_ops->fo_mmap == NULL) {
    360  1.150       chs 			error = ENODEV;
    361  1.150       chs 			goto out;
    362  1.116        ad 		}
    363  1.150       chs 		error = (*fp->f_ops->fo_mmap)(fp, &pos, size, prot, &flags,
    364  1.161      maxv 		    &advice, &uobj, &maxprot);
    365  1.150       chs 		if (error) {
    366  1.150       chs 			goto out;
    367  1.116        ad 		}
    368  1.150       chs 		if (uobj == NULL) {
    369    1.6       mrg 			flags |= MAP_ANON;
    370  1.122        ad 			fd_putfile(fd);
    371  1.116        ad 			fp = NULL;
    372    1.6       mrg 			goto is_anon;
    373    1.6       mrg 		}
    374    1.6       mrg 	} else {		/* MAP_ANON case */
    375   1.24   thorpej 		/*
    376   1.24   thorpej 		 * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
    377   1.24   thorpej 		 */
    378    1.6       mrg 		if (fd != -1)
    379  1.161      maxv 			return EINVAL;
    380    1.1       mrg 
    381   1.24   thorpej  is_anon:		/* label for SunOS style /dev/zero */
    382  1.150       chs 		uobj = NULL;
    383    1.6       mrg 		maxprot = VM_PROT_ALL;
    384    1.6       mrg 		pos = 0;
    385   1.28       cgd 	}
    386   1.28       cgd 
    387  1.164     joerg 	maxprot = PAX_MPROTECT_MAXPROTECT(l, prot, extraprot, maxprot);
    388  1.167  utkarsh0 	if (((prot | extraprot) & maxprot) != (prot | extraprot)) {
    389  1.167  utkarsh0 		error = EACCES;
    390  1.167  utkarsh0 		goto out;
    391  1.167  utkarsh0 	}
    392  1.164     joerg 	if ((error = PAX_MPROTECT_VALIDATE(l, prot)))
    393  1.167  utkarsh0 		goto out;
    394   1.97      elad 
    395  1.153      maxv 	pax_aslr_mmap(l, &addr, orig_addr, flags);
    396  1.120  christos 
    397    1.6       mrg 	/*
    398    1.6       mrg 	 * now let kernel internal function uvm_mmap do the work.
    399    1.6       mrg 	 */
    400    1.6       mrg 
    401    1.6       mrg 	error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
    402  1.150       chs 	    flags, advice, uobj, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    403    1.6       mrg 
    404  1.150       chs 	/* remember to add offset */
    405  1.150       chs 	*retval = (register_t)(addr + pageoff);
    406    1.1       mrg 
    407  1.150       chs  out:
    408  1.161      maxv 	if (fp != NULL)
    409  1.122        ad 		fd_putfile(fd);
    410  1.116        ad 
    411  1.161      maxv 	return error;
    412    1.1       mrg }
    413    1.1       mrg 
    414    1.1       mrg /*
    415    1.1       mrg  * sys___msync13: the msync system call (a front-end for flush)
    416    1.1       mrg  */
    417    1.1       mrg 
    418    1.6       mrg int
    419  1.129      yamt sys___msync13(struct lwp *l, const struct sys___msync13_args *uap,
    420  1.129      yamt     register_t *retval)
    421    1.6       mrg {
    422  1.119       dsl 	/* {
    423  1.108  christos 		syscallarg(void *) addr;
    424    1.6       mrg 		syscallarg(size_t) len;
    425    1.6       mrg 		syscallarg(int) flags;
    426  1.119       dsl 	} */
    427   1.67   thorpej 	struct proc *p = l->l_proc;
    428   1.12       eeh 	vaddr_t addr;
    429  1.171  christos 	vsize_t size;
    430   1.53       chs 	struct vm_map *map;
    431  1.159  pgoyette 	int error, flags, uvmflags;
    432  1.159  pgoyette 	bool rv;
    433    1.6       mrg 
    434    1.6       mrg 	/*
    435    1.6       mrg 	 * extract syscall args from the uap
    436    1.6       mrg 	 */
    437    1.6       mrg 
    438   1.12       eeh 	addr = (vaddr_t)SCARG(uap, addr);
    439   1.12       eeh 	size = (vsize_t)SCARG(uap, len);
    440    1.6       mrg 	flags = SCARG(uap, flags);
    441    1.6       mrg 
    442    1.6       mrg 	/* sanity check flags */
    443    1.6       mrg 	if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
    444   1.77       chs 	    (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
    445   1.77       chs 	    (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
    446  1.161      maxv 		return EINVAL;
    447    1.6       mrg 	if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
    448   1.77       chs 		flags |= MS_SYNC;
    449    1.1       mrg 
    450    1.6       mrg 	/*
    451    1.6       mrg 	 * get map
    452    1.6       mrg 	 */
    453  1.158    martin 	map = &p->p_vmspace->vm_map;
    454    1.6       mrg 
    455  1.171  christos 	if (round_and_check(map, &addr, &size))
    456  1.160      maxv 		return ENOMEM;
    457    1.6       mrg 
    458    1.6       mrg 	/*
    459    1.6       mrg 	 * XXXCDC: do we really need this semantic?
    460    1.6       mrg 	 *
    461    1.6       mrg 	 * XXX Gak!  If size is zero we are supposed to sync "all modified
    462    1.6       mrg 	 * pages with the region containing addr".  Unfortunately, we
    463    1.6       mrg 	 * don't really keep track of individual mmaps so we approximate
    464    1.6       mrg 	 * by flushing the range of the map entry containing addr.
    465    1.6       mrg 	 * This can be incorrect if the region splits or is coalesced
    466    1.6       mrg 	 * with a neighbor.
    467    1.6       mrg 	 */
    468   1.50       chs 
    469    1.6       mrg 	if (size == 0) {
    470   1.53       chs 		struct vm_map_entry *entry;
    471   1.51       chs 
    472    1.6       mrg 		vm_map_lock_read(map);
    473    1.6       mrg 		rv = uvm_map_lookup_entry(map, addr, &entry);
    474  1.107   thorpej 		if (rv == true) {
    475    1.6       mrg 			addr = entry->start;
    476    1.6       mrg 			size = entry->end - entry->start;
    477    1.6       mrg 		}
    478    1.6       mrg 		vm_map_unlock_read(map);
    479  1.107   thorpej 		if (rv == false)
    480  1.161      maxv 			return EINVAL;
    481    1.6       mrg 	}
    482    1.6       mrg 
    483    1.6       mrg 	/*
    484    1.6       mrg 	 * translate MS_ flags into PGO_ flags
    485    1.6       mrg 	 */
    486   1.50       chs 
    487   1.34   thorpej 	uvmflags = PGO_CLEANIT;
    488   1.34   thorpej 	if (flags & MS_INVALIDATE)
    489   1.34   thorpej 		uvmflags |= PGO_FREE;
    490    1.6       mrg 	if (flags & MS_SYNC)
    491    1.6       mrg 		uvmflags |= PGO_SYNCIO;
    492    1.6       mrg 
    493   1.50       chs 	error = uvm_map_clean(map, addr, addr+size, uvmflags);
    494   1.50       chs 	return error;
    495    1.1       mrg }
    496    1.1       mrg 
    497    1.1       mrg /*
    498    1.1       mrg  * sys_munmap: unmap a users memory
    499    1.1       mrg  */
    500    1.1       mrg 
    501    1.6       mrg int
    502  1.119       dsl sys_munmap(struct lwp *l, const struct sys_munmap_args *uap, register_t *retval)
    503    1.6       mrg {
    504  1.119       dsl 	/* {
    505  1.108  christos 		syscallarg(void *) addr;
    506    1.6       mrg 		syscallarg(size_t) len;
    507  1.119       dsl 	} */
    508   1.67   thorpej 	struct proc *p = l->l_proc;
    509   1.12       eeh 	vaddr_t addr;
    510  1.171  christos 	vsize_t size;
    511   1.53       chs 	struct vm_map *map;
    512    1.6       mrg 	struct vm_map_entry *dead_entries;
    513    1.6       mrg 
    514    1.6       mrg 	/*
    515   1.50       chs 	 * get syscall args.
    516    1.6       mrg 	 */
    517    1.6       mrg 
    518   1.50       chs 	addr = (vaddr_t)SCARG(uap, addr);
    519   1.50       chs 	size = (vsize_t)SCARG(uap, len);
    520   1.51       chs 
    521  1.171  christos 	map = &p->p_vmspace->vm_map;
    522    1.6       mrg 
    523  1.171  christos 	if (round_and_check(map, &addr, &size))
    524  1.171  christos 		return EINVAL;
    525    1.6       mrg 
    526    1.6       mrg 	if (size == 0)
    527  1.161      maxv 		return 0;
    528    1.6       mrg 
    529  1.161      maxv 	vm_map_lock(map);
    530  1.161      maxv #if 0
    531    1.6       mrg 	/*
    532   1.51       chs 	 * interesting system call semantic: make sure entire range is
    533    1.6       mrg 	 * allocated before allowing an unmap.
    534    1.6       mrg 	 */
    535    1.6       mrg 	if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
    536    1.6       mrg 		vm_map_unlock(map);
    537  1.161      maxv 		return EINVAL;
    538    1.6       mrg 	}
    539   1.66   mycroft #endif
    540  1.144      para 	uvm_unmap_remove(map, addr, addr + size, &dead_entries, 0);
    541   1.50       chs 	vm_map_unlock(map);
    542    1.6       mrg 	if (dead_entries != NULL)
    543    1.6       mrg 		uvm_unmap_detach(dead_entries, 0);
    544  1.161      maxv 	return 0;
    545    1.1       mrg }
    546    1.1       mrg 
    547    1.1       mrg /*
    548    1.1       mrg  * sys_mprotect: the mprotect system call
    549    1.1       mrg  */
    550    1.1       mrg 
    551    1.6       mrg int
    552  1.129      yamt sys_mprotect(struct lwp *l, const struct sys_mprotect_args *uap,
    553  1.129      yamt     register_t *retval)
    554    1.6       mrg {
    555  1.119       dsl 	/* {
    556  1.108  christos 		syscallarg(void *) addr;
    557   1.76       chs 		syscallarg(size_t) len;
    558    1.6       mrg 		syscallarg(int) prot;
    559  1.119       dsl 	} */
    560   1.67   thorpej 	struct proc *p = l->l_proc;
    561   1.12       eeh 	vaddr_t addr;
    562  1.171  christos 	vsize_t size;
    563    1.6       mrg 	vm_prot_t prot;
    564   1.50       chs 	int error;
    565    1.6       mrg 
    566    1.6       mrg 	/*
    567    1.6       mrg 	 * extract syscall args from uap
    568    1.6       mrg 	 */
    569    1.6       mrg 
    570   1.12       eeh 	addr = (vaddr_t)SCARG(uap, addr);
    571   1.12       eeh 	size = (vsize_t)SCARG(uap, len);
    572    1.6       mrg 	prot = SCARG(uap, prot) & VM_PROT_ALL;
    573    1.6       mrg 
    574  1.171  christos 	if (round_and_check(&p->p_vmspace->vm_map, &addr, &size))
    575  1.160      maxv 		return EINVAL;
    576  1.110  christos 
    577  1.164     joerg 	error = uvm_map_protect_user(l, addr, addr + size, prot);
    578   1.50       chs 	return error;
    579    1.1       mrg }
    580    1.1       mrg 
    581    1.1       mrg /*
    582    1.1       mrg  * sys_minherit: the minherit system call
    583    1.1       mrg  */
    584    1.1       mrg 
    585    1.6       mrg int
    586  1.129      yamt sys_minherit(struct lwp *l, const struct sys_minherit_args *uap,
    587  1.129      yamt    register_t *retval)
    588    1.6       mrg {
    589  1.119       dsl 	/* {
    590  1.108  christos 		syscallarg(void *) addr;
    591    1.6       mrg 		syscallarg(int) len;
    592    1.6       mrg 		syscallarg(int) inherit;
    593  1.119       dsl 	} */
    594   1.67   thorpej 	struct proc *p = l->l_proc;
    595   1.12       eeh 	vaddr_t addr;
    596  1.171  christos 	vsize_t size;
    597   1.40  augustss 	vm_inherit_t inherit;
    598   1.50       chs 	int error;
    599   1.51       chs 
    600   1.12       eeh 	addr = (vaddr_t)SCARG(uap, addr);
    601   1.12       eeh 	size = (vsize_t)SCARG(uap, len);
    602    1.6       mrg 	inherit = SCARG(uap, inherit);
    603   1.50       chs 
    604  1.171  christos 	if (round_and_check(&p->p_vmspace->vm_map, &addr, &size))
    605  1.160      maxv 		return EINVAL;
    606  1.110  christos 
    607   1.50       chs 	error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size,
    608  1.161      maxv 	    inherit);
    609   1.50       chs 	return error;
    610   1.21       mrg }
    611   1.21       mrg 
    612   1.21       mrg /*
    613   1.21       mrg  * sys_madvise: give advice about memory usage.
    614   1.21       mrg  */
    615   1.21       mrg 
    616   1.21       mrg /* ARGSUSED */
    617   1.21       mrg int
    618  1.129      yamt sys_madvise(struct lwp *l, const struct sys_madvise_args *uap,
    619  1.129      yamt    register_t *retval)
    620   1.21       mrg {
    621  1.119       dsl 	/* {
    622  1.108  christos 		syscallarg(void *) addr;
    623   1.21       mrg 		syscallarg(size_t) len;
    624   1.21       mrg 		syscallarg(int) behav;
    625  1.119       dsl 	} */
    626   1.67   thorpej 	struct proc *p = l->l_proc;
    627   1.21       mrg 	vaddr_t addr;
    628  1.171  christos 	vsize_t size;
    629   1.50       chs 	int advice, error;
    630   1.51       chs 
    631   1.21       mrg 	addr = (vaddr_t)SCARG(uap, addr);
    632   1.21       mrg 	size = (vsize_t)SCARG(uap, len);
    633   1.21       mrg 	advice = SCARG(uap, behav);
    634   1.21       mrg 
    635  1.171  christos 	if (round_and_check(&p->p_vmspace->vm_map, &addr, &size))
    636  1.160      maxv 		return EINVAL;
    637   1.29   thorpej 
    638   1.29   thorpej 	switch (advice) {
    639   1.29   thorpej 	case MADV_NORMAL:
    640   1.29   thorpej 	case MADV_RANDOM:
    641   1.29   thorpej 	case MADV_SEQUENTIAL:
    642   1.50       chs 		error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size,
    643   1.29   thorpej 		    advice);
    644   1.29   thorpej 		break;
    645   1.29   thorpej 
    646   1.29   thorpej 	case MADV_WILLNEED:
    647   1.50       chs 
    648   1.29   thorpej 		/*
    649   1.29   thorpej 		 * Activate all these pages, pre-faulting them in if
    650   1.29   thorpej 		 * necessary.
    651   1.29   thorpej 		 */
    652  1.130      yamt 		error = uvm_map_willneed(&p->p_vmspace->vm_map,
    653  1.130      yamt 		    addr, addr + size);
    654  1.130      yamt 		break;
    655   1.29   thorpej 
    656   1.29   thorpej 	case MADV_DONTNEED:
    657   1.50       chs 
    658   1.29   thorpej 		/*
    659   1.29   thorpej 		 * Deactivate all these pages.  We don't need them
    660   1.29   thorpej 		 * any more.  We don't, however, toss the data in
    661   1.29   thorpej 		 * the pages.
    662   1.29   thorpej 		 */
    663   1.50       chs 
    664   1.50       chs 		error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
    665   1.29   thorpej 		    PGO_DEACTIVATE);
    666   1.29   thorpej 		break;
    667   1.29   thorpej 
    668   1.29   thorpej 	case MADV_FREE:
    669   1.50       chs 
    670   1.29   thorpej 		/*
    671   1.29   thorpej 		 * These pages contain no valid data, and may be
    672   1.45     soren 		 * garbage-collected.  Toss all resources, including
    673   1.30   thorpej 		 * any swap space in use.
    674   1.29   thorpej 		 */
    675   1.50       chs 
    676   1.50       chs 		error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
    677   1.29   thorpej 		    PGO_FREE);
    678   1.29   thorpej 		break;
    679   1.29   thorpej 
    680   1.29   thorpej 	case MADV_SPACEAVAIL:
    681   1.50       chs 
    682   1.29   thorpej 		/*
    683   1.29   thorpej 		 * XXXMRG What is this?  I think it's:
    684   1.29   thorpej 		 *
    685   1.29   thorpej 		 *	Ensure that we have allocated backing-store
    686   1.29   thorpej 		 *	for these pages.
    687   1.29   thorpej 		 *
    688   1.29   thorpej 		 * This is going to require changes to the page daemon,
    689   1.29   thorpej 		 * as it will free swap space allocated to pages in core.
    690   1.29   thorpej 		 * There's also what to do for device/file/anonymous memory.
    691   1.29   thorpej 		 */
    692   1.50       chs 
    693  1.161      maxv 		return EINVAL;
    694   1.29   thorpej 
    695   1.29   thorpej 	default:
    696  1.161      maxv 		return EINVAL;
    697   1.29   thorpej 	}
    698   1.29   thorpej 
    699   1.50       chs 	return error;
    700    1.1       mrg }
    701    1.1       mrg 
    702    1.1       mrg /*
    703    1.1       mrg  * sys_mlock: memory lock
    704    1.1       mrg  */
    705    1.1       mrg 
    706    1.6       mrg int
    707  1.119       dsl sys_mlock(struct lwp *l, const struct sys_mlock_args *uap, register_t *retval)
    708    1.6       mrg {
    709  1.119       dsl 	/* {
    710   1.10    kleink 		syscallarg(const void *) addr;
    711    1.6       mrg 		syscallarg(size_t) len;
    712  1.119       dsl 	} */
    713   1.67   thorpej 	struct proc *p = l->l_proc;
    714   1.12       eeh 	vaddr_t addr;
    715  1.171  christos 	vsize_t size;
    716    1.6       mrg 	int error;
    717    1.6       mrg 
    718    1.6       mrg 	/*
    719    1.6       mrg 	 * extract syscall args from uap
    720    1.6       mrg 	 */
    721   1.50       chs 
    722   1.12       eeh 	addr = (vaddr_t)SCARG(uap, addr);
    723   1.12       eeh 	size = (vsize_t)SCARG(uap, len);
    724    1.6       mrg 
    725  1.171  christos 	if (round_and_check(&p->p_vmspace->vm_map, &addr, &size))
    726  1.160      maxv 		return ENOMEM;
    727    1.1       mrg 
    728    1.6       mrg 	if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
    729  1.161      maxv 		return EAGAIN;
    730    1.1       mrg 
    731    1.6       mrg 	if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
    732  1.161      maxv 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
    733  1.161      maxv 		return EAGAIN;
    734    1.1       mrg 
    735  1.107   thorpej 	error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, false,
    736   1.35   thorpej 	    0);
    737   1.85    briggs 	if (error == EFAULT)
    738   1.85    briggs 		error = ENOMEM;
    739   1.50       chs 	return error;
    740    1.1       mrg }
    741    1.1       mrg 
    742    1.1       mrg /*
    743    1.1       mrg  * sys_munlock: unlock wired pages
    744    1.1       mrg  */
    745    1.1       mrg 
    746    1.6       mrg int
    747  1.129      yamt sys_munlock(struct lwp *l, const struct sys_munlock_args *uap,
    748  1.129      yamt     register_t *retval)
    749    1.6       mrg {
    750  1.119       dsl 	/* {
    751   1.10    kleink 		syscallarg(const void *) addr;
    752    1.6       mrg 		syscallarg(size_t) len;
    753  1.119       dsl 	} */
    754   1.67   thorpej 	struct proc *p = l->l_proc;
    755   1.12       eeh 	vaddr_t addr;
    756  1.171  christos 	vsize_t size;
    757    1.6       mrg 
    758    1.6       mrg 	/*
    759    1.6       mrg 	 * extract syscall args from uap
    760    1.6       mrg 	 */
    761    1.6       mrg 
    762   1.12       eeh 	addr = (vaddr_t)SCARG(uap, addr);
    763   1.12       eeh 	size = (vsize_t)SCARG(uap, len);
    764    1.6       mrg 
    765  1.171  christos 	if (round_and_check(&p->p_vmspace->vm_map, &addr, &size))
    766  1.160      maxv 		return ENOMEM;
    767    1.1       mrg 
    768  1.171  christos 	if (uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, true, 0))
    769  1.162       kre 		return ENOMEM;
    770  1.162       kre 
    771  1.162       kre 	return 0;
    772   1.22   thorpej }
    773   1.22   thorpej 
    774   1.22   thorpej /*
    775   1.22   thorpej  * sys_mlockall: lock all pages mapped into an address space.
    776   1.22   thorpej  */
    777   1.22   thorpej 
    778   1.22   thorpej int
    779  1.129      yamt sys_mlockall(struct lwp *l, const struct sys_mlockall_args *uap,
    780  1.129      yamt     register_t *retval)
    781   1.22   thorpej {
    782  1.119       dsl 	/* {
    783   1.22   thorpej 		syscallarg(int) flags;
    784  1.119       dsl 	} */
    785   1.67   thorpej 	struct proc *p = l->l_proc;
    786   1.22   thorpej 	int error, flags;
    787   1.22   thorpej 
    788   1.22   thorpej 	flags = SCARG(uap, flags);
    789   1.22   thorpej 
    790  1.161      maxv 	if (flags == 0 || (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
    791  1.161      maxv 		return EINVAL;
    792   1.22   thorpej 
    793   1.25   thorpej 	error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags,
    794   1.25   thorpej 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    795  1.161      maxv 	return error;
    796   1.22   thorpej }
    797   1.22   thorpej 
    798   1.22   thorpej /*
    799   1.22   thorpej  * sys_munlockall: unlock all pages mapped into an address space.
    800   1.22   thorpej  */
    801   1.22   thorpej 
    802   1.22   thorpej int
    803  1.119       dsl sys_munlockall(struct lwp *l, const void *v, register_t *retval)
    804   1.22   thorpej {
    805   1.67   thorpej 	struct proc *p = l->l_proc;
    806   1.22   thorpej 
    807   1.22   thorpej 	(void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
    808  1.161      maxv 	return 0;
    809    1.1       mrg }
    810    1.1       mrg 
    811    1.1       mrg /*
    812    1.1       mrg  * uvm_mmap: internal version of mmap
    813    1.1       mrg  *
    814   1.56       chs  * - used by sys_mmap and various framebuffers
    815  1.150       chs  * - uobj is a struct uvm_object pointer or NULL for MAP_ANON
    816    1.1       mrg  * - caller must page-align the file offset
    817    1.1       mrg  */
    818    1.1       mrg 
    819    1.6       mrg int
    820  1.129      yamt uvm_mmap(struct vm_map *map, vaddr_t *addr, vsize_t size, vm_prot_t prot,
    821  1.150       chs     vm_prot_t maxprot, int flags, int advice, struct uvm_object *uobj,
    822  1.150       chs     voff_t foff, vsize_t locklimit)
    823    1.6       mrg {
    824   1.70      matt 	vaddr_t align = 0;
    825   1.50       chs 	int error;
    826    1.6       mrg 	uvm_flag_t uvmflag = 0;
    827    1.6       mrg 
    828    1.6       mrg 	/*
    829    1.6       mrg 	 * check params
    830    1.6       mrg 	 */
    831    1.6       mrg 
    832    1.6       mrg 	if (size == 0)
    833  1.161      maxv 		return 0;
    834    1.6       mrg 	if (foff & PAGE_MASK)
    835  1.161      maxv 		return EINVAL;
    836    1.6       mrg 	if ((prot & maxprot) != prot)
    837  1.161      maxv 		return EINVAL;
    838    1.6       mrg 
    839    1.6       mrg 	/*
    840    1.6       mrg 	 * for non-fixed mappings, round off the suggested address.
    841  1.165       chs 	 * for fixed mappings, check alignment.
    842    1.6       mrg 	 */
    843    1.6       mrg 
    844    1.6       mrg 	if ((flags & MAP_FIXED) == 0) {
    845   1.56       chs 		*addr = round_page(*addr);
    846    1.6       mrg 	} else {
    847    1.6       mrg 		if (*addr & PAGE_MASK)
    848  1.161      maxv 			return EINVAL;
    849  1.166       chs 		uvmflag |= UVM_FLAG_FIXED | UVM_FLAG_UNMAP;
    850    1.6       mrg 	}
    851    1.6       mrg 
    852    1.6       mrg 	/*
    853   1.70      matt 	 * Try to see if any requested alignment can even be attemped.
    854   1.70      matt 	 * Make sure we can express the alignment (asking for a >= 4GB
    855   1.70      matt 	 * alignment on an ILP32 architecure make no sense) and the
    856   1.70      matt 	 * alignment is at least for a page sized quanitiy.  If the
    857   1.70      matt 	 * request was for a fixed mapping, make sure supplied address
    858   1.70      matt 	 * adheres to the request alignment.
    859   1.70      matt 	 */
    860   1.70      matt 	align = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
    861   1.70      matt 	if (align) {
    862   1.70      matt 		if (align >= sizeof(vaddr_t) * NBBY)
    863  1.161      maxv 			return EINVAL;
    864   1.70      matt 		align = 1L << align;
    865   1.70      matt 		if (align < PAGE_SIZE)
    866  1.161      maxv 			return EINVAL;
    867   1.88       chs 		if (align >= vm_map_max(map))
    868  1.161      maxv 			return ENOMEM;
    869   1.70      matt 		if (flags & MAP_FIXED) {
    870   1.70      matt 			if ((*addr & (align-1)) != 0)
    871  1.161      maxv 				return EINVAL;
    872   1.70      matt 			align = 0;
    873   1.70      matt 		}
    874   1.70      matt 	}
    875   1.70      matt 
    876   1.70      matt 	/*
    877  1.128       mrg 	 * check resource limits
    878  1.128       mrg 	 */
    879  1.128       mrg 
    880  1.128       mrg 	if (!VM_MAP_IS_KERNEL(map) &&
    881  1.128       mrg 	    (((rlim_t)curproc->p_vmspace->vm_map.size + (rlim_t)size) >
    882  1.128       mrg 	    curproc->p_rlimit[RLIMIT_AS].rlim_cur))
    883  1.128       mrg 		return ENOMEM;
    884  1.128       mrg 
    885  1.128       mrg 	/*
    886    1.6       mrg 	 * handle anon vs. non-anon mappings.   for non-anon mappings attach
    887    1.6       mrg 	 * to underlying vm object.
    888    1.6       mrg 	 */
    889    1.6       mrg 
    890    1.6       mrg 	if (flags & MAP_ANON) {
    891  1.150       chs 		KASSERT(uobj == NULL);
    892   1.36   thorpej 		foff = UVM_UNKNOWN_OFFSET;
    893    1.6       mrg 		if ((flags & MAP_SHARED) == 0)
    894    1.6       mrg 			/* XXX: defer amap create */
    895    1.6       mrg 			uvmflag |= UVM_FLAG_COPYONW;
    896    1.6       mrg 		else
    897    1.6       mrg 			/* shared: create amap now */
    898    1.6       mrg 			uvmflag |= UVM_FLAG_OVERLAY;
    899    1.6       mrg 
    900    1.6       mrg 	} else {
    901  1.150       chs 		KASSERT(uobj != NULL);
    902   1.92      yamt 		if ((flags & MAP_SHARED) == 0) {
    903    1.6       mrg 			uvmflag |= UVM_FLAG_COPYONW;
    904  1.100       chs 		}
    905    1.6       mrg 	}
    906    1.6       mrg 
    907   1.51       chs 	uvmflag = UVM_MAPFLAG(prot, maxprot,
    908  1.161      maxv 	    (flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY, advice,
    909  1.161      maxv 	    uvmflag);
    910   1.70      matt 	error = uvm_map(map, addr, size, uobj, foff, align, uvmflag);
    911   1.50       chs 	if (error) {
    912   1.50       chs 		if (uobj)
    913   1.50       chs 			uobj->pgops->pgo_detach(uobj);
    914   1.50       chs 		return error;
    915   1.50       chs 	}
    916    1.1       mrg 
    917    1.6       mrg 	/*
    918   1.50       chs 	 * POSIX 1003.1b -- if our address space was configured
    919   1.50       chs 	 * to lock all future mappings, wire the one we just made.
    920   1.78   thorpej 	 *
    921   1.78   thorpej 	 * Also handle the MAP_WIRED flag here.
    922    1.6       mrg 	 */
    923    1.6       mrg 
    924   1.50       chs 	if (prot == VM_PROT_NONE) {
    925    1.6       mrg 
    926   1.25   thorpej 		/*
    927   1.50       chs 		 * No more work to do in this case.
    928   1.25   thorpej 		 */
    929   1.25   thorpej 
    930  1.161      maxv 		return 0;
    931   1.50       chs 	}
    932   1.78   thorpej 	if ((flags & MAP_WIRED) != 0 || (map->flags & VM_MAP_WIREFUTURE) != 0) {
    933  1.126        ad 		vm_map_lock(map);
    934   1.87       chs 		if (atop(size) + uvmexp.wired > uvmexp.wiredmax ||
    935   1.87       chs 		    (locklimit != 0 &&
    936   1.87       chs 		     size + ptoa(pmap_wired_count(vm_map_pmap(map))) >
    937   1.87       chs 		     locklimit)) {
    938   1.50       chs 			vm_map_unlock(map);
    939   1.50       chs 			uvm_unmap(map, *addr, *addr + size);
    940   1.50       chs 			return ENOMEM;
    941   1.25   thorpej 		}
    942   1.25   thorpej 
    943   1.50       chs 		/*
    944   1.50       chs 		 * uvm_map_pageable() always returns the map unlocked.
    945   1.50       chs 		 */
    946   1.25   thorpej 
    947   1.50       chs 		error = uvm_map_pageable(map, *addr, *addr + size,
    948  1.161      maxv 		    false, UVM_LK_ENTER);
    949   1.50       chs 		if (error) {
    950   1.50       chs 			uvm_unmap(map, *addr, *addr + size);
    951   1.50       chs 			return error;
    952   1.50       chs 		}
    953  1.161      maxv 		return 0;
    954   1.25   thorpej 	}
    955   1.50       chs 	return 0;
    956    1.1       mrg }
    957   1.89      fvdl 
    958   1.89      fvdl vaddr_t
    959  1.154    martin uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz, int topdown)
    960   1.89      fvdl {
    961  1.102      yamt 
    962  1.154    martin 	if (topdown)
    963  1.146  christos 		return VM_DEFAULT_ADDRESS_TOPDOWN(base, sz);
    964  1.146  christos 	else
    965  1.146  christos 		return VM_DEFAULT_ADDRESS_BOTTOMUP(base, sz);
    966   1.89      fvdl }
    967  1.150       chs 
    968  1.150       chs int
    969  1.150       chs uvm_mmap_dev(struct proc *p, void **addrp, size_t len, dev_t dev,
    970  1.150       chs     off_t off)
    971  1.150       chs {
    972  1.150       chs 	struct uvm_object *uobj;
    973  1.150       chs 	int error, flags, prot;
    974  1.150       chs 
    975  1.150       chs 	flags = MAP_SHARED;
    976  1.150       chs 	prot = VM_PROT_READ | VM_PROT_WRITE;
    977  1.150       chs 	if (*addrp)
    978  1.150       chs 		flags |= MAP_FIXED;
    979  1.150       chs 	else
    980  1.150       chs 		*addrp = (void *)p->p_emul->e_vm_default_addr(p,
    981  1.154    martin 		    (vaddr_t)p->p_vmspace->vm_daddr, len,
    982  1.154    martin 		    p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);
    983  1.150       chs 
    984  1.151       chs 	uobj = udv_attach(dev, prot, off, len);
    985  1.150       chs 	if (uobj == NULL)
    986  1.150       chs 		return EINVAL;
    987  1.150       chs 
    988  1.150       chs 	error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
    989  1.161      maxv 	    (vsize_t)len, prot, prot, flags, UVM_ADV_RANDOM, uobj, off,
    990  1.161      maxv 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    991  1.150       chs 	return error;
    992  1.150       chs }
    993  1.150       chs 
    994  1.150       chs int
    995  1.150       chs uvm_mmap_anon(struct proc *p, void **addrp, size_t len)
    996  1.150       chs {
    997  1.150       chs 	int error, flags, prot;
    998  1.150       chs 
    999  1.150       chs 	flags = MAP_PRIVATE | MAP_ANON;
   1000  1.150       chs 	prot = VM_PROT_READ | VM_PROT_WRITE;
   1001  1.150       chs 	if (*addrp)
   1002  1.150       chs 		flags |= MAP_FIXED;
   1003  1.150       chs 	else
   1004  1.150       chs 		*addrp = (void *)p->p_emul->e_vm_default_addr(p,
   1005  1.154    martin 		    (vaddr_t)p->p_vmspace->vm_daddr, len,
   1006  1.154    martin 		    p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);
   1007  1.150       chs 
   1008  1.150       chs 	error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
   1009  1.161      maxv 	    (vsize_t)len, prot, prot, flags, UVM_ADV_NORMAL, NULL, 0,
   1010  1.161      maxv 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
   1011  1.150       chs 	return error;
   1012  1.150       chs }
   1013