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