1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1988 University of Utah. 3df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 4df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 7df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 8df8bae1dSRodney W. Grimes * Science Department. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$ 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94 41851c12ffSJohn Dyson * $Id: vm_mmap.c,v 1.52 1996/10/24 02:56:23 dyson Exp $ 42df8bae1dSRodney W. Grimes */ 43df8bae1dSRodney W. Grimes 44df8bae1dSRodney W. Grimes /* 45df8bae1dSRodney W. Grimes * Mapped file (mmap) interface to VM 46df8bae1dSRodney W. Grimes */ 47df8bae1dSRodney W. Grimes 48df8bae1dSRodney W. Grimes #include <sys/param.h> 49df8bae1dSRodney W. Grimes #include <sys/systm.h> 50d2d3e875SBruce Evans #include <sys/sysproto.h> 51df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 52df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 54df8bae1dSRodney W. Grimes #include <sys/vnode.h> 55df8bae1dSRodney W. Grimes #include <sys/file.h> 56df8bae1dSRodney W. Grimes #include <sys/mman.h> 57df8bae1dSRodney W. Grimes #include <sys/conf.h> 58efeaf95aSDavid Greenman #include <sys/vmmeter.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes #include <miscfs/specfs/specdev.h> 61df8bae1dSRodney W. Grimes 62df8bae1dSRodney W. Grimes #include <vm/vm.h> 63efeaf95aSDavid Greenman #include <vm/vm_param.h> 64efeaf95aSDavid Greenman #include <vm/vm_prot.h> 65efeaf95aSDavid Greenman #include <vm/vm_inherit.h> 66efeaf95aSDavid Greenman #include <vm/lock.h> 67efeaf95aSDavid Greenman #include <vm/pmap.h> 68efeaf95aSDavid Greenman #include <vm/vm_map.h> 69efeaf95aSDavid Greenman #include <vm/vm_object.h> 70df8bae1dSRodney W. Grimes #include <vm/vm_pager.h> 71b5e8ce9fSBruce Evans #include <vm/vm_pageout.h> 72efeaf95aSDavid Greenman #include <vm/vm_extern.h> 73bd7e5f99SJohn Dyson #include <vm/vm_kern.h> 74867a482dSJohn Dyson #include <vm/vm_page.h> 75df8bae1dSRodney W. Grimes 76d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 77df8bae1dSRodney W. Grimes struct sbrk_args { 78df8bae1dSRodney W. Grimes int incr; 79df8bae1dSRodney W. Grimes }; 80d2d3e875SBruce Evans #endif 810d94caffSDavid Greenman 82df8bae1dSRodney W. Grimes /* ARGSUSED */ 83df8bae1dSRodney W. Grimes int 84df8bae1dSRodney W. Grimes sbrk(p, uap, retval) 85df8bae1dSRodney W. Grimes struct proc *p; 86df8bae1dSRodney W. Grimes struct sbrk_args *uap; 87df8bae1dSRodney W. Grimes int *retval; 88df8bae1dSRodney W. Grimes { 89df8bae1dSRodney W. Grimes 90df8bae1dSRodney W. Grimes /* Not yet implemented */ 91df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 92df8bae1dSRodney W. Grimes } 93df8bae1dSRodney W. Grimes 94d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 95df8bae1dSRodney W. Grimes struct sstk_args { 96df8bae1dSRodney W. Grimes int incr; 97df8bae1dSRodney W. Grimes }; 98d2d3e875SBruce Evans #endif 990d94caffSDavid Greenman 100df8bae1dSRodney W. Grimes /* ARGSUSED */ 101df8bae1dSRodney W. Grimes int 102df8bae1dSRodney W. Grimes sstk(p, uap, retval) 103df8bae1dSRodney W. Grimes struct proc *p; 104df8bae1dSRodney W. Grimes struct sstk_args *uap; 105df8bae1dSRodney W. Grimes int *retval; 106df8bae1dSRodney W. Grimes { 107df8bae1dSRodney W. Grimes 108df8bae1dSRodney W. Grimes /* Not yet implemented */ 109df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 110df8bae1dSRodney W. Grimes } 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 113d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 114df8bae1dSRodney W. Grimes struct getpagesize_args { 115df8bae1dSRodney W. Grimes int dummy; 116df8bae1dSRodney W. Grimes }; 117d2d3e875SBruce Evans #endif 1180d94caffSDavid Greenman 119df8bae1dSRodney W. Grimes /* ARGSUSED */ 120df8bae1dSRodney W. Grimes int 121df8bae1dSRodney W. Grimes ogetpagesize(p, uap, retval) 122df8bae1dSRodney W. Grimes struct proc *p; 123df8bae1dSRodney W. Grimes struct getpagesize_args *uap; 124df8bae1dSRodney W. Grimes int *retval; 125df8bae1dSRodney W. Grimes { 126df8bae1dSRodney W. Grimes 127df8bae1dSRodney W. Grimes *retval = PAGE_SIZE; 128df8bae1dSRodney W. Grimes return (0); 129df8bae1dSRodney W. Grimes } 130df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 131df8bae1dSRodney W. Grimes 132d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 133df8bae1dSRodney W. Grimes struct mmap_args { 134df8bae1dSRodney W. Grimes caddr_t addr; 135df8bae1dSRodney W. Grimes size_t len; 136df8bae1dSRodney W. Grimes int prot; 137df8bae1dSRodney W. Grimes int flags; 138df8bae1dSRodney W. Grimes int fd; 139df8bae1dSRodney W. Grimes long pad; 140df8bae1dSRodney W. Grimes off_t pos; 141df8bae1dSRodney W. Grimes }; 142d2d3e875SBruce Evans #endif 143df8bae1dSRodney W. Grimes 144df8bae1dSRodney W. Grimes int 145df8bae1dSRodney W. Grimes mmap(p, uap, retval) 146df8bae1dSRodney W. Grimes struct proc *p; 147df8bae1dSRodney W. Grimes register struct mmap_args *uap; 148df8bae1dSRodney W. Grimes int *retval; 149df8bae1dSRodney W. Grimes { 150df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 151df8bae1dSRodney W. Grimes register struct file *fp; 152df8bae1dSRodney W. Grimes struct vnode *vp; 153df8bae1dSRodney W. Grimes vm_offset_t addr; 1549154ee6aSPeter Wemm vm_size_t size, pageoff; 155df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 156df8bae1dSRodney W. Grimes caddr_t handle; 157df8bae1dSRodney W. Grimes int flags, error; 158df8bae1dSRodney W. Grimes 159df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 160df8bae1dSRodney W. Grimes flags = uap->flags; 161df8bae1dSRodney W. Grimes /* 1620d94caffSDavid Greenman * Address (if FIXED) must be page aligned. Size is implicitly rounded 1630d94caffSDavid Greenman * to a page boundary. 164df8bae1dSRodney W. Grimes */ 165df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 166df8bae1dSRodney W. Grimes if (((flags & MAP_FIXED) && (addr & PAGE_MASK)) || 167df8bae1dSRodney W. Grimes (ssize_t) uap->len < 0 || ((flags & MAP_ANON) && uap->fd != -1)) 168df8bae1dSRodney W. Grimes return (EINVAL); 1699154ee6aSPeter Wemm 1709154ee6aSPeter Wemm /* 1719154ee6aSPeter Wemm * Round page if not already disallowed by above test 1729154ee6aSPeter Wemm * XXX: Is there any point in the MAP_FIXED align requirement above? 1739154ee6aSPeter Wemm */ 1749154ee6aSPeter Wemm size = uap->len; 1759154ee6aSPeter Wemm pageoff = (addr & PAGE_MASK); 1769154ee6aSPeter Wemm addr -= pageoff; 1779154ee6aSPeter Wemm size += pageoff; 1789154ee6aSPeter Wemm size = (vm_size_t) round_page(size); 1799154ee6aSPeter Wemm 180df8bae1dSRodney W. Grimes /* 1810d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 1820d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 183df8bae1dSRodney W. Grimes */ 184df8bae1dSRodney W. Grimes if (flags & MAP_FIXED) { 185bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 186df8bae1dSRodney W. Grimes return (EINVAL); 18726f9a767SRodney W. Grimes #ifndef i386 188df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 189df8bae1dSRodney W. Grimes return (EINVAL); 19026f9a767SRodney W. Grimes #endif 191bbc0ec52SDavid Greenman if (addr + size < addr) 192df8bae1dSRodney W. Grimes return (EINVAL); 193df8bae1dSRodney W. Grimes } 194df8bae1dSRodney W. Grimes /* 1950d94caffSDavid Greenman * XXX if no hint provided for a non-fixed mapping place it after the 1960d94caffSDavid Greenman * end of the largest possible heap. 197df8bae1dSRodney W. Grimes * 1980d94caffSDavid Greenman * There should really be a pmap call to determine a reasonable location. 199df8bae1dSRodney W. Grimes */ 200df8bae1dSRodney W. Grimes if (addr == 0 && (flags & MAP_FIXED) == 0) 201df8bae1dSRodney W. Grimes addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ); 202df8bae1dSRodney W. Grimes if (flags & MAP_ANON) { 203df8bae1dSRodney W. Grimes /* 204df8bae1dSRodney W. Grimes * Mapping blank space is trivial. 205df8bae1dSRodney W. Grimes */ 206df8bae1dSRodney W. Grimes handle = NULL; 207df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 208df8bae1dSRodney W. Grimes } else { 209df8bae1dSRodney W. Grimes /* 2100d94caffSDavid Greenman * Mapping file, get fp for validation. Obtain vnode and make 2110d94caffSDavid Greenman * sure it is of appropriate type. 212df8bae1dSRodney W. Grimes */ 213df8bae1dSRodney W. Grimes if (((unsigned) uap->fd) >= fdp->fd_nfiles || 214df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL) 215df8bae1dSRodney W. Grimes return (EBADF); 216df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_VNODE) 217df8bae1dSRodney W. Grimes return (EINVAL); 218df8bae1dSRodney W. Grimes vp = (struct vnode *) fp->f_data; 219df8bae1dSRodney W. Grimes if (vp->v_type != VREG && vp->v_type != VCHR) 220df8bae1dSRodney W. Grimes return (EINVAL); 221df8bae1dSRodney W. Grimes /* 2220d94caffSDavid Greenman * XXX hack to handle use of /dev/zero to map anon memory (ala 2230d94caffSDavid Greenman * SunOS). 224df8bae1dSRodney W. Grimes */ 225df8bae1dSRodney W. Grimes if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) { 226df8bae1dSRodney W. Grimes handle = NULL; 227df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 228df8bae1dSRodney W. Grimes flags |= MAP_ANON; 229df8bae1dSRodney W. Grimes } else { 230df8bae1dSRodney W. Grimes /* 231df8bae1dSRodney W. Grimes * Ensure that file and memory protections are 232df8bae1dSRodney W. Grimes * compatible. Note that we only worry about 233df8bae1dSRodney W. Grimes * writability if mapping is shared; in this case, 234df8bae1dSRodney W. Grimes * current and max prot are dictated by the open file. 235df8bae1dSRodney W. Grimes * XXX use the vnode instead? Problem is: what 2360d94caffSDavid Greenman * credentials do we use for determination? What if 2370d94caffSDavid Greenman * proc does a setuid? 238df8bae1dSRodney W. Grimes */ 239df8bae1dSRodney W. Grimes maxprot = VM_PROT_EXECUTE; /* ??? */ 240df8bae1dSRodney W. Grimes if (fp->f_flag & FREAD) 241df8bae1dSRodney W. Grimes maxprot |= VM_PROT_READ; 242df8bae1dSRodney W. Grimes else if (prot & PROT_READ) 243df8bae1dSRodney W. Grimes return (EACCES); 244df8bae1dSRodney W. Grimes if (flags & MAP_SHARED) { 245df8bae1dSRodney W. Grimes if (fp->f_flag & FWRITE) 246df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 247df8bae1dSRodney W. Grimes else if (prot & PROT_WRITE) 248df8bae1dSRodney W. Grimes return (EACCES); 249df8bae1dSRodney W. Grimes } else 250df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 251df8bae1dSRodney W. Grimes handle = (caddr_t) vp; 252df8bae1dSRodney W. Grimes } 253df8bae1dSRodney W. Grimes } 254df8bae1dSRodney W. Grimes error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot, 255a316d390SJohn Dyson flags, handle, uap->pos); 256df8bae1dSRodney W. Grimes if (error == 0) 257df8bae1dSRodney W. Grimes *retval = (int) addr; 258df8bae1dSRodney W. Grimes return (error); 259df8bae1dSRodney W. Grimes } 260df8bae1dSRodney W. Grimes 26105f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43 262d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 26305f0fdd2SPoul-Henning Kamp struct ommap_args { 26405f0fdd2SPoul-Henning Kamp caddr_t addr; 26505f0fdd2SPoul-Henning Kamp int len; 26605f0fdd2SPoul-Henning Kamp int prot; 26705f0fdd2SPoul-Henning Kamp int flags; 26805f0fdd2SPoul-Henning Kamp int fd; 26905f0fdd2SPoul-Henning Kamp long pos; 27005f0fdd2SPoul-Henning Kamp }; 271d2d3e875SBruce Evans #endif 27205f0fdd2SPoul-Henning Kamp int 27305f0fdd2SPoul-Henning Kamp ommap(p, uap, retval) 27405f0fdd2SPoul-Henning Kamp struct proc *p; 27505f0fdd2SPoul-Henning Kamp register struct ommap_args *uap; 27605f0fdd2SPoul-Henning Kamp int *retval; 27705f0fdd2SPoul-Henning Kamp { 27805f0fdd2SPoul-Henning Kamp struct mmap_args nargs; 27905f0fdd2SPoul-Henning Kamp static const char cvtbsdprot[8] = { 28005f0fdd2SPoul-Henning Kamp 0, 28105f0fdd2SPoul-Henning Kamp PROT_EXEC, 28205f0fdd2SPoul-Henning Kamp PROT_WRITE, 28305f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE, 28405f0fdd2SPoul-Henning Kamp PROT_READ, 28505f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_READ, 28605f0fdd2SPoul-Henning Kamp PROT_WRITE | PROT_READ, 28705f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE | PROT_READ, 28805f0fdd2SPoul-Henning Kamp }; 2890d94caffSDavid Greenman 29005f0fdd2SPoul-Henning Kamp #define OMAP_ANON 0x0002 29105f0fdd2SPoul-Henning Kamp #define OMAP_COPY 0x0020 29205f0fdd2SPoul-Henning Kamp #define OMAP_SHARED 0x0010 29305f0fdd2SPoul-Henning Kamp #define OMAP_FIXED 0x0100 29405f0fdd2SPoul-Henning Kamp #define OMAP_INHERIT 0x0800 29505f0fdd2SPoul-Henning Kamp 29605f0fdd2SPoul-Henning Kamp nargs.addr = uap->addr; 29705f0fdd2SPoul-Henning Kamp nargs.len = uap->len; 29805f0fdd2SPoul-Henning Kamp nargs.prot = cvtbsdprot[uap->prot & 0x7]; 29905f0fdd2SPoul-Henning Kamp nargs.flags = 0; 30005f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_ANON) 30105f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_ANON; 30205f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_COPY) 30305f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_COPY; 30405f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_SHARED) 30505f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_SHARED; 30605f0fdd2SPoul-Henning Kamp else 30705f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_PRIVATE; 30805f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_FIXED) 30905f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_FIXED; 31005f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_INHERIT) 31105f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_INHERIT; 31205f0fdd2SPoul-Henning Kamp nargs.fd = uap->fd; 31305f0fdd2SPoul-Henning Kamp nargs.pos = uap->pos; 31405f0fdd2SPoul-Henning Kamp return (mmap(p, &nargs, retval)); 31505f0fdd2SPoul-Henning Kamp } 31605f0fdd2SPoul-Henning Kamp #endif /* COMPAT_43 */ 31705f0fdd2SPoul-Henning Kamp 31805f0fdd2SPoul-Henning Kamp 319d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 320df8bae1dSRodney W. Grimes struct msync_args { 321df8bae1dSRodney W. Grimes caddr_t addr; 322df8bae1dSRodney W. Grimes int len; 323e6c6af11SDavid Greenman int flags; 324df8bae1dSRodney W. Grimes }; 325d2d3e875SBruce Evans #endif 326df8bae1dSRodney W. Grimes int 327df8bae1dSRodney W. Grimes msync(p, uap, retval) 328df8bae1dSRodney W. Grimes struct proc *p; 329df8bae1dSRodney W. Grimes struct msync_args *uap; 330df8bae1dSRodney W. Grimes int *retval; 331df8bae1dSRodney W. Grimes { 332df8bae1dSRodney W. Grimes vm_offset_t addr; 333dabee6feSPeter Wemm vm_size_t size, pageoff; 334e6c6af11SDavid Greenman int flags; 335df8bae1dSRodney W. Grimes vm_map_t map; 336df8bae1dSRodney W. Grimes int rv; 337df8bae1dSRodney W. Grimes 338df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 3399154ee6aSPeter Wemm size = uap->len; 340e6c6af11SDavid Greenman flags = uap->flags; 341e6c6af11SDavid Greenman 342dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 343dabee6feSPeter Wemm addr -= pageoff; 344dabee6feSPeter Wemm size += pageoff; 345dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 3469154ee6aSPeter Wemm if (addr + size < addr) 347dabee6feSPeter Wemm return(EINVAL); 348dabee6feSPeter Wemm 349dabee6feSPeter Wemm if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 3501e62bc63SDavid Greenman return (EINVAL); 3511e62bc63SDavid Greenman 3529154ee6aSPeter Wemm map = &p->p_vmspace->vm_map; 3539154ee6aSPeter Wemm 354df8bae1dSRodney W. Grimes /* 355df8bae1dSRodney W. Grimes * XXX Gak! If size is zero we are supposed to sync "all modified 3560d94caffSDavid Greenman * pages with the region containing addr". Unfortunately, we don't 3570d94caffSDavid Greenman * really keep track of individual mmaps so we approximate by flushing 3580d94caffSDavid Greenman * the range of the map entry containing addr. This can be incorrect 3590d94caffSDavid Greenman * if the region splits or is coalesced with a neighbor. 360df8bae1dSRodney W. Grimes */ 361df8bae1dSRodney W. Grimes if (size == 0) { 362df8bae1dSRodney W. Grimes vm_map_entry_t entry; 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes vm_map_lock_read(map); 365df8bae1dSRodney W. Grimes rv = vm_map_lookup_entry(map, addr, &entry); 366df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 367fbcfcdf7SDavid Greenman if (rv == FALSE) 368df8bae1dSRodney W. Grimes return (EINVAL); 369df8bae1dSRodney W. Grimes addr = entry->start; 370df8bae1dSRodney W. Grimes size = entry->end - entry->start; 371df8bae1dSRodney W. Grimes } 372e6c6af11SDavid Greenman 373df8bae1dSRodney W. Grimes /* 374df8bae1dSRodney W. Grimes * Clean the pages and interpret the return value. 375df8bae1dSRodney W. Grimes */ 3766c534ad8SDavid Greenman rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 377e6c6af11SDavid Greenman (flags & MS_INVALIDATE) != 0); 378e6c6af11SDavid Greenman 379df8bae1dSRodney W. Grimes switch (rv) { 380df8bae1dSRodney W. Grimes case KERN_SUCCESS: 381df8bae1dSRodney W. Grimes break; 382df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 383df8bae1dSRodney W. Grimes return (EINVAL); /* Sun returns ENOMEM? */ 384df8bae1dSRodney W. Grimes case KERN_FAILURE: 385df8bae1dSRodney W. Grimes return (EIO); 386df8bae1dSRodney W. Grimes default: 387df8bae1dSRodney W. Grimes return (EINVAL); 388df8bae1dSRodney W. Grimes } 389e6c6af11SDavid Greenman 390df8bae1dSRodney W. Grimes return (0); 391df8bae1dSRodney W. Grimes } 392df8bae1dSRodney W. Grimes 393d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 394df8bae1dSRodney W. Grimes struct munmap_args { 395df8bae1dSRodney W. Grimes caddr_t addr; 3969154ee6aSPeter Wemm size_t len; 397df8bae1dSRodney W. Grimes }; 398d2d3e875SBruce Evans #endif 399df8bae1dSRodney W. Grimes int 400df8bae1dSRodney W. Grimes munmap(p, uap, retval) 401df8bae1dSRodney W. Grimes register struct proc *p; 402df8bae1dSRodney W. Grimes register struct munmap_args *uap; 403df8bae1dSRodney W. Grimes int *retval; 404df8bae1dSRodney W. Grimes { 405df8bae1dSRodney W. Grimes vm_offset_t addr; 406dabee6feSPeter Wemm vm_size_t size, pageoff; 407df8bae1dSRodney W. Grimes vm_map_t map; 408df8bae1dSRodney W. Grimes 409df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 4109154ee6aSPeter Wemm size = uap->len; 411dabee6feSPeter Wemm 412dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 413dabee6feSPeter Wemm addr -= pageoff; 414dabee6feSPeter Wemm size += pageoff; 415dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 4169154ee6aSPeter Wemm if (addr + size < addr) 417df8bae1dSRodney W. Grimes return(EINVAL); 4189154ee6aSPeter Wemm 419df8bae1dSRodney W. Grimes if (size == 0) 420df8bae1dSRodney W. Grimes return (0); 421dabee6feSPeter Wemm 422df8bae1dSRodney W. Grimes /* 4230d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 4240d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 425df8bae1dSRodney W. Grimes */ 426bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 427df8bae1dSRodney W. Grimes return (EINVAL); 42826f9a767SRodney W. Grimes #ifndef i386 429df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 430df8bae1dSRodney W. Grimes return (EINVAL); 43126f9a767SRodney W. Grimes #endif 432bbc0ec52SDavid Greenman if (addr + size < addr) 433df8bae1dSRodney W. Grimes return (EINVAL); 434df8bae1dSRodney W. Grimes map = &p->p_vmspace->vm_map; 435df8bae1dSRodney W. Grimes /* 436df8bae1dSRodney W. Grimes * Make sure entire range is allocated. 437df8bae1dSRodney W. Grimes */ 438df8bae1dSRodney W. Grimes if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) 439df8bae1dSRodney W. Grimes return (EINVAL); 440df8bae1dSRodney W. Grimes /* returns nothing but KERN_SUCCESS anyway */ 441df8bae1dSRodney W. Grimes (void) vm_map_remove(map, addr, addr + size); 442df8bae1dSRodney W. Grimes return (0); 443df8bae1dSRodney W. Grimes } 444df8bae1dSRodney W. Grimes 445df8bae1dSRodney W. Grimes void 44690324b07SDavid Greenman munmapfd(p, fd) 44790324b07SDavid Greenman struct proc *p; 448df8bae1dSRodney W. Grimes int fd; 449df8bae1dSRodney W. Grimes { 450df8bae1dSRodney W. Grimes /* 451c4ed5a07SDavid Greenman * XXX should unmap any regions mapped to this file 452df8bae1dSRodney W. Grimes */ 45390324b07SDavid Greenman p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED; 454df8bae1dSRodney W. Grimes } 455df8bae1dSRodney W. Grimes 456d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 457df8bae1dSRodney W. Grimes struct mprotect_args { 458df8bae1dSRodney W. Grimes caddr_t addr; 4599154ee6aSPeter Wemm size_t len; 460df8bae1dSRodney W. Grimes int prot; 461df8bae1dSRodney W. Grimes }; 462d2d3e875SBruce Evans #endif 463df8bae1dSRodney W. Grimes int 464df8bae1dSRodney W. Grimes mprotect(p, uap, retval) 465df8bae1dSRodney W. Grimes struct proc *p; 466df8bae1dSRodney W. Grimes struct mprotect_args *uap; 467df8bae1dSRodney W. Grimes int *retval; 468df8bae1dSRodney W. Grimes { 469df8bae1dSRodney W. Grimes vm_offset_t addr; 470dabee6feSPeter Wemm vm_size_t size, pageoff; 471df8bae1dSRodney W. Grimes register vm_prot_t prot; 472df8bae1dSRodney W. Grimes 473df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 4749154ee6aSPeter Wemm size = uap->len; 475df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 476df8bae1dSRodney W. Grimes 477dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 478dabee6feSPeter Wemm addr -= pageoff; 479dabee6feSPeter Wemm size += pageoff; 480dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 4819154ee6aSPeter Wemm if (addr + size < addr) 482dabee6feSPeter Wemm return(EINVAL); 483dabee6feSPeter Wemm 484df8bae1dSRodney W. Grimes switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot, 485df8bae1dSRodney W. Grimes FALSE)) { 486df8bae1dSRodney W. Grimes case KERN_SUCCESS: 487df8bae1dSRodney W. Grimes return (0); 488df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 489df8bae1dSRodney W. Grimes return (EACCES); 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes return (EINVAL); 492df8bae1dSRodney W. Grimes } 493df8bae1dSRodney W. Grimes 494d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 495dabee6feSPeter Wemm struct minherit_args { 496dabee6feSPeter Wemm caddr_t addr; 4979154ee6aSPeter Wemm size_t len; 498dabee6feSPeter Wemm int inherit; 499dabee6feSPeter Wemm }; 500dabee6feSPeter Wemm #endif 501dabee6feSPeter Wemm int 502dabee6feSPeter Wemm minherit(p, uap, retval) 503dabee6feSPeter Wemm struct proc *p; 504dabee6feSPeter Wemm struct minherit_args *uap; 505dabee6feSPeter Wemm int *retval; 506dabee6feSPeter Wemm { 507dabee6feSPeter Wemm vm_offset_t addr; 508dabee6feSPeter Wemm vm_size_t size, pageoff; 509dabee6feSPeter Wemm register vm_inherit_t inherit; 510dabee6feSPeter Wemm 511dabee6feSPeter Wemm addr = (vm_offset_t)uap->addr; 5129154ee6aSPeter Wemm size = uap->len; 513dabee6feSPeter Wemm inherit = uap->inherit; 514dabee6feSPeter Wemm 515dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 516dabee6feSPeter Wemm addr -= pageoff; 517dabee6feSPeter Wemm size += pageoff; 518dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 5199154ee6aSPeter Wemm if (addr + size < addr) 520dabee6feSPeter Wemm return(EINVAL); 521dabee6feSPeter Wemm 522dabee6feSPeter Wemm switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size, 523dabee6feSPeter Wemm inherit)) { 524dabee6feSPeter Wemm case KERN_SUCCESS: 525dabee6feSPeter Wemm return (0); 526dabee6feSPeter Wemm case KERN_PROTECTION_FAILURE: 527dabee6feSPeter Wemm return (EACCES); 528dabee6feSPeter Wemm } 529dabee6feSPeter Wemm return (EINVAL); 530dabee6feSPeter Wemm } 531dabee6feSPeter Wemm 532dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 533df8bae1dSRodney W. Grimes struct madvise_args { 534df8bae1dSRodney W. Grimes caddr_t addr; 5359154ee6aSPeter Wemm size_t len; 536df8bae1dSRodney W. Grimes int behav; 537df8bae1dSRodney W. Grimes }; 538d2d3e875SBruce Evans #endif 5390d94caffSDavid Greenman 540df8bae1dSRodney W. Grimes /* ARGSUSED */ 541df8bae1dSRodney W. Grimes int 542df8bae1dSRodney W. Grimes madvise(p, uap, retval) 543df8bae1dSRodney W. Grimes struct proc *p; 544df8bae1dSRodney W. Grimes struct madvise_args *uap; 545df8bae1dSRodney W. Grimes int *retval; 546df8bae1dSRodney W. Grimes { 547867a482dSJohn Dyson vm_map_t map; 548867a482dSJohn Dyson pmap_t pmap; 549f35329acSJohn Dyson vm_offset_t start, end; 550867a482dSJohn Dyson /* 551867a482dSJohn Dyson * Check for illegal addresses. Watch out for address wrap... Note 552867a482dSJohn Dyson * that VM_*_ADDRESS are not constants due to casts (argh). 553867a482dSJohn Dyson */ 554867a482dSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && 555867a482dSJohn Dyson ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS) 556867a482dSJohn Dyson return (EINVAL); 557867a482dSJohn Dyson #ifndef i386 558867a482dSJohn Dyson if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS) 559867a482dSJohn Dyson return (EINVAL); 560867a482dSJohn Dyson #endif 561867a482dSJohn Dyson if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr) 562867a482dSJohn Dyson return (EINVAL); 563867a482dSJohn Dyson 564867a482dSJohn Dyson /* 565867a482dSJohn Dyson * Since this routine is only advisory, we default to conservative 566867a482dSJohn Dyson * behavior. 567867a482dSJohn Dyson */ 568cd6eea25SDavid Greenman start = trunc_page((vm_offset_t) uap->addr); 569cd6eea25SDavid Greenman end = round_page((vm_offset_t) uap->addr + uap->len); 570867a482dSJohn Dyson 571867a482dSJohn Dyson map = &p->p_vmspace->vm_map; 572867a482dSJohn Dyson pmap = &p->p_vmspace->vm_pmap; 573867a482dSJohn Dyson 574867a482dSJohn Dyson vm_map_madvise(map, pmap, start, end, uap->behav); 575df8bae1dSRodney W. Grimes 576867a482dSJohn Dyson return (0); 577df8bae1dSRodney W. Grimes } 578df8bae1dSRodney W. Grimes 579d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 580df8bae1dSRodney W. Grimes struct mincore_args { 581df8bae1dSRodney W. Grimes caddr_t addr; 5829154ee6aSPeter Wemm size_t len; 583df8bae1dSRodney W. Grimes char *vec; 584df8bae1dSRodney W. Grimes }; 585d2d3e875SBruce Evans #endif 5860d94caffSDavid Greenman 587df8bae1dSRodney W. Grimes /* ARGSUSED */ 588df8bae1dSRodney W. Grimes int 589df8bae1dSRodney W. Grimes mincore(p, uap, retval) 590df8bae1dSRodney W. Grimes struct proc *p; 591df8bae1dSRodney W. Grimes struct mincore_args *uap; 592df8bae1dSRodney W. Grimes int *retval; 593df8bae1dSRodney W. Grimes { 594867a482dSJohn Dyson vm_offset_t addr, first_addr; 595867a482dSJohn Dyson vm_offset_t end, cend; 596867a482dSJohn Dyson pmap_t pmap; 597867a482dSJohn Dyson vm_map_t map; 59802c04a2fSJohn Dyson char *vec; 599867a482dSJohn Dyson int error; 600867a482dSJohn Dyson int vecindex, lastvecindex; 601867a482dSJohn Dyson register vm_map_entry_t current; 602867a482dSJohn Dyson vm_map_entry_t entry; 603867a482dSJohn Dyson int mincoreinfo; 604df8bae1dSRodney W. Grimes 605867a482dSJohn Dyson /* 606867a482dSJohn Dyson * Make sure that the addresses presented are valid for user 607867a482dSJohn Dyson * mode. 608867a482dSJohn Dyson */ 609867a482dSJohn Dyson first_addr = addr = trunc_page((vm_offset_t) uap->addr); 6109154ee6aSPeter Wemm end = addr + (vm_size_t)round_page(uap->len); 61102c04a2fSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS) 61202c04a2fSJohn Dyson return (EINVAL); 61302c04a2fSJohn Dyson if (end < addr) 61402c04a2fSJohn Dyson return (EINVAL); 61502c04a2fSJohn Dyson 616867a482dSJohn Dyson /* 617867a482dSJohn Dyson * Address of byte vector 618867a482dSJohn Dyson */ 61902c04a2fSJohn Dyson vec = uap->vec; 620867a482dSJohn Dyson 621867a482dSJohn Dyson map = &p->p_vmspace->vm_map; 622867a482dSJohn Dyson pmap = &p->p_vmspace->vm_pmap; 623867a482dSJohn Dyson 624867a482dSJohn Dyson vm_map_lock(map); 625867a482dSJohn Dyson 626867a482dSJohn Dyson /* 627867a482dSJohn Dyson * Not needed here 628867a482dSJohn Dyson */ 629867a482dSJohn Dyson #if 0 630867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, addr, end); 631867a482dSJohn Dyson #endif 632867a482dSJohn Dyson 633867a482dSJohn Dyson if (!vm_map_lookup_entry(map, addr, &entry)) 634867a482dSJohn Dyson entry = entry->next; 635867a482dSJohn Dyson 636867a482dSJohn Dyson /* 637867a482dSJohn Dyson * Do this on a map entry basis so that if the pages are not 638867a482dSJohn Dyson * in the current processes address space, we can easily look 639867a482dSJohn Dyson * up the pages elsewhere. 640867a482dSJohn Dyson */ 641867a482dSJohn Dyson lastvecindex = -1; 642867a482dSJohn Dyson for(current = entry; 643867a482dSJohn Dyson (current != &map->header) && (current->start < end); 644867a482dSJohn Dyson current = current->next) { 645867a482dSJohn Dyson 646867a482dSJohn Dyson /* 647867a482dSJohn Dyson * ignore submaps (for now) or null objects 648867a482dSJohn Dyson */ 649867a482dSJohn Dyson if (current->is_a_map || current->is_sub_map || 650867a482dSJohn Dyson current->object.vm_object == NULL) 651867a482dSJohn Dyson continue; 652867a482dSJohn Dyson 653867a482dSJohn Dyson /* 654867a482dSJohn Dyson * limit this scan to the current map entry and the 655867a482dSJohn Dyson * limits for the mincore call 656867a482dSJohn Dyson */ 657867a482dSJohn Dyson if (addr < current->start) 658867a482dSJohn Dyson addr = current->start; 659867a482dSJohn Dyson cend = current->end; 660867a482dSJohn Dyson if (cend > end) 661867a482dSJohn Dyson cend = end; 662867a482dSJohn Dyson 663867a482dSJohn Dyson /* 664867a482dSJohn Dyson * scan this entry one page at a time 665867a482dSJohn Dyson */ 666867a482dSJohn Dyson while(addr < cend) { 667867a482dSJohn Dyson /* 668867a482dSJohn Dyson * Check pmap first, it is likely faster, also 669867a482dSJohn Dyson * it can provide info as to whether we are the 670867a482dSJohn Dyson * one referencing or modifying the page. 671867a482dSJohn Dyson */ 672867a482dSJohn Dyson mincoreinfo = pmap_mincore(pmap, addr); 673867a482dSJohn Dyson if (!mincoreinfo) { 674867a482dSJohn Dyson vm_pindex_t pindex; 675867a482dSJohn Dyson vm_ooffset_t offset; 676867a482dSJohn Dyson vm_page_t m; 677867a482dSJohn Dyson /* 678867a482dSJohn Dyson * calculate the page index into the object 679867a482dSJohn Dyson */ 680867a482dSJohn Dyson offset = current->offset + (addr - current->start); 681867a482dSJohn Dyson pindex = OFF_TO_IDX(offset); 682867a482dSJohn Dyson m = vm_page_lookup(current->object.vm_object, 683867a482dSJohn Dyson pindex); 684867a482dSJohn Dyson /* 685867a482dSJohn Dyson * if the page is resident, then gather information about 686867a482dSJohn Dyson * it. 687867a482dSJohn Dyson */ 688867a482dSJohn Dyson if (m) { 689867a482dSJohn Dyson mincoreinfo = MINCORE_INCORE; 690867a482dSJohn Dyson if (m->dirty || 69167bf6868SJohn Dyson pmap_is_modified(VM_PAGE_TO_PHYS(m))) 692867a482dSJohn Dyson mincoreinfo |= MINCORE_MODIFIED_OTHER; 693867a482dSJohn Dyson if ((m->flags & PG_REFERENCED) || 69467bf6868SJohn Dyson pmap_is_referenced(VM_PAGE_TO_PHYS(m))) 695867a482dSJohn Dyson mincoreinfo |= MINCORE_REFERENCED_OTHER; 69602c04a2fSJohn Dyson } 697867a482dSJohn Dyson } 698867a482dSJohn Dyson 699867a482dSJohn Dyson /* 700867a482dSJohn Dyson * calculate index into user supplied byte vector 701867a482dSJohn Dyson */ 702867a482dSJohn Dyson vecindex = OFF_TO_IDX(addr - first_addr); 703867a482dSJohn Dyson 704867a482dSJohn Dyson /* 705867a482dSJohn Dyson * If we have skipped map entries, we need to make sure that 706867a482dSJohn Dyson * the byte vector is zeroed for those skipped entries. 707867a482dSJohn Dyson */ 708867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 709867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 710867a482dSJohn Dyson if (error) { 711867a482dSJohn Dyson vm_map_unlock(map); 712867a482dSJohn Dyson return (EFAULT); 713867a482dSJohn Dyson } 714867a482dSJohn Dyson ++lastvecindex; 715867a482dSJohn Dyson } 716867a482dSJohn Dyson 717867a482dSJohn Dyson /* 718867a482dSJohn Dyson * Pass the page information to the user 719867a482dSJohn Dyson */ 720867a482dSJohn Dyson error = subyte( vec + vecindex, mincoreinfo); 721867a482dSJohn Dyson if (error) { 722867a482dSJohn Dyson vm_map_unlock(map); 723867a482dSJohn Dyson return (EFAULT); 724867a482dSJohn Dyson } 725867a482dSJohn Dyson lastvecindex = vecindex; 72602c04a2fSJohn Dyson addr += PAGE_SIZE; 72702c04a2fSJohn Dyson } 728867a482dSJohn Dyson } 729867a482dSJohn Dyson 730867a482dSJohn Dyson /* 731867a482dSJohn Dyson * Zero the last entries in the byte vector. 732867a482dSJohn Dyson */ 733867a482dSJohn Dyson vecindex = OFF_TO_IDX(end - first_addr); 734867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 735867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 736867a482dSJohn Dyson if (error) { 737867a482dSJohn Dyson vm_map_unlock(map); 738867a482dSJohn Dyson return (EFAULT); 739867a482dSJohn Dyson } 740867a482dSJohn Dyson ++lastvecindex; 741867a482dSJohn Dyson } 742867a482dSJohn Dyson 743867a482dSJohn Dyson vm_map_unlock(map); 74402c04a2fSJohn Dyson return (0); 745df8bae1dSRodney W. Grimes } 746df8bae1dSRodney W. Grimes 747d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 748df8bae1dSRodney W. Grimes struct mlock_args { 749df8bae1dSRodney W. Grimes caddr_t addr; 750df8bae1dSRodney W. Grimes size_t len; 751df8bae1dSRodney W. Grimes }; 752d2d3e875SBruce Evans #endif 753df8bae1dSRodney W. Grimes int 754df8bae1dSRodney W. Grimes mlock(p, uap, retval) 755df8bae1dSRodney W. Grimes struct proc *p; 756df8bae1dSRodney W. Grimes struct mlock_args *uap; 757df8bae1dSRodney W. Grimes int *retval; 758df8bae1dSRodney W. Grimes { 759df8bae1dSRodney W. Grimes vm_offset_t addr; 760dabee6feSPeter Wemm vm_size_t size, pageoff; 761df8bae1dSRodney W. Grimes int error; 762df8bae1dSRodney W. Grimes 763df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 7649154ee6aSPeter Wemm size = uap->len; 7659154ee6aSPeter Wemm 766dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 767dabee6feSPeter Wemm addr -= pageoff; 768dabee6feSPeter Wemm size += pageoff; 769dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 770dabee6feSPeter Wemm 771dabee6feSPeter Wemm /* disable wrap around */ 7729154ee6aSPeter Wemm if (addr + size < addr) 773df8bae1dSRodney W. Grimes return (EINVAL); 774dabee6feSPeter Wemm 775df8bae1dSRodney W. Grimes if (atop(size) + cnt.v_wire_count > vm_page_max_wired) 776df8bae1dSRodney W. Grimes return (EAGAIN); 7779154ee6aSPeter Wemm 778df8bae1dSRodney W. Grimes #ifdef pmap_wired_count 779df8bae1dSRodney W. Grimes if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 780df8bae1dSRodney W. Grimes p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 781df8bae1dSRodney W. Grimes return (EAGAIN); 782df8bae1dSRodney W. Grimes #else 78305f0fdd2SPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 78405f0fdd2SPoul-Henning Kamp if (error) 785df8bae1dSRodney W. Grimes return (error); 786df8bae1dSRodney W. Grimes #endif 787df8bae1dSRodney W. Grimes 788df8bae1dSRodney W. Grimes error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE); 789df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes 792d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 793df8bae1dSRodney W. Grimes struct munlock_args { 794df8bae1dSRodney W. Grimes caddr_t addr; 795df8bae1dSRodney W. Grimes size_t len; 796df8bae1dSRodney W. Grimes }; 797d2d3e875SBruce Evans #endif 798df8bae1dSRodney W. Grimes int 799df8bae1dSRodney W. Grimes munlock(p, uap, retval) 800df8bae1dSRodney W. Grimes struct proc *p; 801df8bae1dSRodney W. Grimes struct munlock_args *uap; 802df8bae1dSRodney W. Grimes int *retval; 803df8bae1dSRodney W. Grimes { 804df8bae1dSRodney W. Grimes vm_offset_t addr; 805dabee6feSPeter Wemm vm_size_t size, pageoff; 806df8bae1dSRodney W. Grimes int error; 807df8bae1dSRodney W. Grimes 808df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 8099154ee6aSPeter Wemm size = uap->len; 8109154ee6aSPeter Wemm 811dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 812dabee6feSPeter Wemm addr -= pageoff; 813dabee6feSPeter Wemm size += pageoff; 814dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 815dabee6feSPeter Wemm 816dabee6feSPeter Wemm /* disable wrap around */ 8179154ee6aSPeter Wemm if (addr + size < addr) 818df8bae1dSRodney W. Grimes return (EINVAL); 819dabee6feSPeter Wemm 820df8bae1dSRodney W. Grimes #ifndef pmap_wired_count 82105f0fdd2SPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 82205f0fdd2SPoul-Henning Kamp if (error) 823df8bae1dSRodney W. Grimes return (error); 824df8bae1dSRodney W. Grimes #endif 825df8bae1dSRodney W. Grimes 826df8bae1dSRodney W. Grimes error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE); 827df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 828df8bae1dSRodney W. Grimes } 829df8bae1dSRodney W. Grimes 830df8bae1dSRodney W. Grimes /* 831df8bae1dSRodney W. Grimes * Internal version of mmap. 832df8bae1dSRodney W. Grimes * Currently used by mmap, exec, and sys5 shared memory. 833df8bae1dSRodney W. Grimes * Handle is either a vnode pointer or NULL for MAP_ANON. 834df8bae1dSRodney W. Grimes */ 835df8bae1dSRodney W. Grimes int 836df8bae1dSRodney W. Grimes vm_mmap(map, addr, size, prot, maxprot, flags, handle, foff) 837df8bae1dSRodney W. Grimes register vm_map_t map; 838df8bae1dSRodney W. Grimes register vm_offset_t *addr; 839df8bae1dSRodney W. Grimes register vm_size_t size; 840df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 841df8bae1dSRodney W. Grimes register int flags; 842df8bae1dSRodney W. Grimes caddr_t handle; /* XXX should be vp */ 843a316d390SJohn Dyson vm_ooffset_t foff; 844df8bae1dSRodney W. Grimes { 845df8bae1dSRodney W. Grimes boolean_t fitit; 846fcae040bSJohn Dyson vm_object_t object; 847df8bae1dSRodney W. Grimes struct vnode *vp = NULL; 84824a1cce3SDavid Greenman objtype_t type; 849df8bae1dSRodney W. Grimes int rv = KERN_SUCCESS; 850bd7e5f99SJohn Dyson vm_ooffset_t objsize; 851bd7e5f99SJohn Dyson int docow; 85206cb7259SDavid Greenman struct proc *p = curproc; 853df8bae1dSRodney W. Grimes 854df8bae1dSRodney W. Grimes if (size == 0) 855df8bae1dSRodney W. Grimes return (0); 856df8bae1dSRodney W. Grimes 85706cb7259SDavid Greenman objsize = size = round_page(size); 858df8bae1dSRodney W. Grimes 859df8bae1dSRodney W. Grimes /* 860bc9ad247SDavid Greenman * We currently can only deal with page aligned file offsets. 861bc9ad247SDavid Greenman * The check is here rather than in the syscall because the 862bc9ad247SDavid Greenman * kernel calls this function internally for other mmaping 863bc9ad247SDavid Greenman * operations (such as in exec) and non-aligned offsets will 864bc9ad247SDavid Greenman * cause pmap inconsistencies...so we want to be sure to 865bc9ad247SDavid Greenman * disallow this in all cases. 866bc9ad247SDavid Greenman */ 867bc9ad247SDavid Greenman if (foff & PAGE_MASK) 868bc9ad247SDavid Greenman return (EINVAL); 869bc9ad247SDavid Greenman 87006cb7259SDavid Greenman if ((flags & MAP_FIXED) == 0) { 87106cb7259SDavid Greenman fitit = TRUE; 87206cb7259SDavid Greenman *addr = round_page(*addr); 87306cb7259SDavid Greenman } else { 87406cb7259SDavid Greenman if (*addr != trunc_page(*addr)) 87506cb7259SDavid Greenman return (EINVAL); 87606cb7259SDavid Greenman fitit = FALSE; 87706cb7259SDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 87806cb7259SDavid Greenman } 87906cb7259SDavid Greenman 880bc9ad247SDavid Greenman /* 88124a1cce3SDavid Greenman * Lookup/allocate object. 882df8bae1dSRodney W. Grimes */ 8835f55e841SDavid Greenman if (flags & MAP_ANON) { 884851c12ffSJohn Dyson type = OBJT_DEFAULT; 8855f55e841SDavid Greenman /* 8865f55e841SDavid Greenman * Unnamed anonymous regions always start at 0. 8875f55e841SDavid Greenman */ 88867bf6868SJohn Dyson if (handle == 0) 8895f55e841SDavid Greenman foff = 0; 8905f55e841SDavid Greenman } else { 891df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 892df8bae1dSRodney W. Grimes if (vp->v_type == VCHR) { 89324a1cce3SDavid Greenman type = OBJT_DEVICE; 894df8bae1dSRodney W. Grimes handle = (caddr_t) vp->v_rdev; 89506cb7259SDavid Greenman } else { 89606cb7259SDavid Greenman struct vattr vat; 89706cb7259SDavid Greenman int error; 89806cb7259SDavid Greenman 89906cb7259SDavid Greenman error = VOP_GETATTR(vp, &vat, p->p_ucred, p); 90006cb7259SDavid Greenman if (error) 90106cb7259SDavid Greenman return (error); 902bd7e5f99SJohn Dyson objsize = round_page(vat.va_size); 90324a1cce3SDavid Greenman type = OBJT_VNODE; 904df8bae1dSRodney W. Grimes } 90506cb7259SDavid Greenman } 90667bf6868SJohn Dyson object = vm_pager_allocate(type, handle, OFF_TO_IDX(objsize), prot, foff); 90724a1cce3SDavid Greenman if (object == NULL) 90824a1cce3SDavid Greenman return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 909df8bae1dSRodney W. Grimes 9105850152dSJohn Dyson /* 9118f2ec877SDavid Greenman * Force device mappings to be shared. 9125850152dSJohn Dyson */ 9138f2ec877SDavid Greenman if (type == OBJT_DEVICE) { 9148f2ec877SDavid Greenman flags &= ~(MAP_PRIVATE|MAP_COPY); 9155850152dSJohn Dyson flags |= MAP_SHARED; 9168f2ec877SDavid Greenman } 9175850152dSJohn Dyson 918bd7e5f99SJohn Dyson docow = 0; 9195850152dSJohn Dyson if ((flags & (MAP_ANON|MAP_SHARED)) == 0) { 920fcae040bSJohn Dyson docow = MAP_COPY_ON_WRITE | MAP_COPY_NEEDED; 921bd7e5f99SJohn Dyson } 9225850152dSJohn Dyson 923bd7e5f99SJohn Dyson rv = vm_map_find(map, object, foff, addr, size, fitit, 924bd7e5f99SJohn Dyson prot, maxprot, docow); 925bd7e5f99SJohn Dyson 92667bf6868SJohn Dyson 927df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 9287fb0c17eSDavid Greenman /* 92924a1cce3SDavid Greenman * Lose the object reference. Will destroy the 93024a1cce3SDavid Greenman * object if it's an unnamed anonymous mapping 93124a1cce3SDavid Greenman * or named anonymous without other references. 9327fb0c17eSDavid Greenman */ 933df8bae1dSRodney W. Grimes vm_object_deallocate(object); 934df8bae1dSRodney W. Grimes goto out; 935df8bae1dSRodney W. Grimes } 936e17bed12SJohn Dyson 937df8bae1dSRodney W. Grimes /* 9387fb0c17eSDavid Greenman * "Pre-fault" resident pages. 9397fb0c17eSDavid Greenman */ 940b18bfc3dSJohn Dyson if ((type == OBJT_VNODE) && (map->pmap != NULL)) { 941a316d390SJohn Dyson pmap_object_init_pt(map->pmap, *addr, 942867a482dSJohn Dyson object, (vm_pindex_t) OFF_TO_IDX(foff), size, 1); 943df8bae1dSRodney W. Grimes } 9447fb0c17eSDavid Greenman 945df8bae1dSRodney W. Grimes /* 946df8bae1dSRodney W. Grimes * Shared memory is also shared with children. 947df8bae1dSRodney W. Grimes */ 9485850152dSJohn Dyson if (flags & (MAP_SHARED|MAP_INHERIT)) { 949df8bae1dSRodney W. Grimes rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 950df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 9517fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 952df8bae1dSRodney W. Grimes goto out; 953df8bae1dSRodney W. Grimes } 954df8bae1dSRodney W. Grimes } 955df8bae1dSRodney W. Grimes out: 956df8bae1dSRodney W. Grimes switch (rv) { 957df8bae1dSRodney W. Grimes case KERN_SUCCESS: 958df8bae1dSRodney W. Grimes return (0); 959df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 960df8bae1dSRodney W. Grimes case KERN_NO_SPACE: 961df8bae1dSRodney W. Grimes return (ENOMEM); 962df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 963df8bae1dSRodney W. Grimes return (EACCES); 964df8bae1dSRodney W. Grimes default: 965df8bae1dSRodney W. Grimes return (EINVAL); 966df8bae1dSRodney W. Grimes } 967df8bae1dSRodney W. Grimes } 968