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 41cd6eea25SDavid Greenman * $Id: vm_mmap.c,v 1.49 1996/07/30 03:08:12 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 576df8bae1dSRodney W. Grimes /* Not yet implemented */ 577867a482dSJohn Dyson return (0); 578df8bae1dSRodney W. Grimes } 579df8bae1dSRodney W. Grimes 580d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 581df8bae1dSRodney W. Grimes struct mincore_args { 582df8bae1dSRodney W. Grimes caddr_t addr; 5839154ee6aSPeter Wemm size_t len; 584df8bae1dSRodney W. Grimes char *vec; 585df8bae1dSRodney W. Grimes }; 586d2d3e875SBruce Evans #endif 5870d94caffSDavid Greenman 588df8bae1dSRodney W. Grimes /* ARGSUSED */ 589df8bae1dSRodney W. Grimes int 590df8bae1dSRodney W. Grimes mincore(p, uap, retval) 591df8bae1dSRodney W. Grimes struct proc *p; 592df8bae1dSRodney W. Grimes struct mincore_args *uap; 593df8bae1dSRodney W. Grimes int *retval; 594df8bae1dSRodney W. Grimes { 595867a482dSJohn Dyson vm_offset_t addr, first_addr; 596867a482dSJohn Dyson vm_offset_t end, cend; 597867a482dSJohn Dyson pmap_t pmap; 598867a482dSJohn Dyson vm_map_t map; 59902c04a2fSJohn Dyson char *vec; 600867a482dSJohn Dyson int error; 601867a482dSJohn Dyson int vecindex, lastvecindex; 602867a482dSJohn Dyson register vm_map_entry_t current; 603867a482dSJohn Dyson vm_map_entry_t entry; 604867a482dSJohn Dyson int mincoreinfo; 605df8bae1dSRodney W. Grimes 606867a482dSJohn Dyson /* 607867a482dSJohn Dyson * Make sure that the addresses presented are valid for user 608867a482dSJohn Dyson * mode. 609867a482dSJohn Dyson */ 610867a482dSJohn Dyson first_addr = addr = trunc_page((vm_offset_t) uap->addr); 6119154ee6aSPeter Wemm end = addr + (vm_size_t)round_page(uap->len); 61202c04a2fSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS) 61302c04a2fSJohn Dyson return (EINVAL); 61402c04a2fSJohn Dyson if (end < addr) 61502c04a2fSJohn Dyson return (EINVAL); 61602c04a2fSJohn Dyson 617867a482dSJohn Dyson /* 618867a482dSJohn Dyson * Address of byte vector 619867a482dSJohn Dyson */ 62002c04a2fSJohn Dyson vec = uap->vec; 621867a482dSJohn Dyson 622867a482dSJohn Dyson map = &p->p_vmspace->vm_map; 623867a482dSJohn Dyson pmap = &p->p_vmspace->vm_pmap; 624867a482dSJohn Dyson 625867a482dSJohn Dyson vm_map_lock(map); 626867a482dSJohn Dyson 627867a482dSJohn Dyson /* 628867a482dSJohn Dyson * Not needed here 629867a482dSJohn Dyson */ 630867a482dSJohn Dyson #if 0 631867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, addr, end); 632867a482dSJohn Dyson #endif 633867a482dSJohn Dyson 634867a482dSJohn Dyson if (!vm_map_lookup_entry(map, addr, &entry)) 635867a482dSJohn Dyson entry = entry->next; 636867a482dSJohn Dyson 637867a482dSJohn Dyson /* 638867a482dSJohn Dyson * Do this on a map entry basis so that if the pages are not 639867a482dSJohn Dyson * in the current processes address space, we can easily look 640867a482dSJohn Dyson * up the pages elsewhere. 641867a482dSJohn Dyson */ 642867a482dSJohn Dyson lastvecindex = -1; 643867a482dSJohn Dyson for(current = entry; 644867a482dSJohn Dyson (current != &map->header) && (current->start < end); 645867a482dSJohn Dyson current = current->next) { 646867a482dSJohn Dyson 647867a482dSJohn Dyson /* 648867a482dSJohn Dyson * ignore submaps (for now) or null objects 649867a482dSJohn Dyson */ 650867a482dSJohn Dyson if (current->is_a_map || current->is_sub_map || 651867a482dSJohn Dyson current->object.vm_object == NULL) 652867a482dSJohn Dyson continue; 653867a482dSJohn Dyson 654867a482dSJohn Dyson /* 655867a482dSJohn Dyson * limit this scan to the current map entry and the 656867a482dSJohn Dyson * limits for the mincore call 657867a482dSJohn Dyson */ 658867a482dSJohn Dyson if (addr < current->start) 659867a482dSJohn Dyson addr = current->start; 660867a482dSJohn Dyson cend = current->end; 661867a482dSJohn Dyson if (cend > end) 662867a482dSJohn Dyson cend = end; 663867a482dSJohn Dyson 664867a482dSJohn Dyson /* 665867a482dSJohn Dyson * scan this entry one page at a time 666867a482dSJohn Dyson */ 667867a482dSJohn Dyson while(addr < cend) { 668867a482dSJohn Dyson /* 669867a482dSJohn Dyson * Check pmap first, it is likely faster, also 670867a482dSJohn Dyson * it can provide info as to whether we are the 671867a482dSJohn Dyson * one referencing or modifying the page. 672867a482dSJohn Dyson */ 673867a482dSJohn Dyson mincoreinfo = pmap_mincore(pmap, addr); 674867a482dSJohn Dyson if (!mincoreinfo) { 675867a482dSJohn Dyson vm_pindex_t pindex; 676867a482dSJohn Dyson vm_ooffset_t offset; 677867a482dSJohn Dyson vm_page_t m; 678867a482dSJohn Dyson /* 679867a482dSJohn Dyson * calculate the page index into the object 680867a482dSJohn Dyson */ 681867a482dSJohn Dyson offset = current->offset + (addr - current->start); 682867a482dSJohn Dyson pindex = OFF_TO_IDX(offset); 683867a482dSJohn Dyson m = vm_page_lookup(current->object.vm_object, 684867a482dSJohn Dyson pindex); 685867a482dSJohn Dyson /* 686867a482dSJohn Dyson * if the page is resident, then gather information about 687867a482dSJohn Dyson * it. 688867a482dSJohn Dyson */ 689867a482dSJohn Dyson if (m) { 690867a482dSJohn Dyson mincoreinfo = MINCORE_INCORE; 691867a482dSJohn Dyson if (m->dirty || 69267bf6868SJohn Dyson pmap_is_modified(VM_PAGE_TO_PHYS(m))) 693867a482dSJohn Dyson mincoreinfo |= MINCORE_MODIFIED_OTHER; 694867a482dSJohn Dyson if ((m->flags & PG_REFERENCED) || 69567bf6868SJohn Dyson pmap_is_referenced(VM_PAGE_TO_PHYS(m))) 696867a482dSJohn Dyson mincoreinfo |= MINCORE_REFERENCED_OTHER; 69702c04a2fSJohn Dyson } 698867a482dSJohn Dyson } 699867a482dSJohn Dyson 700867a482dSJohn Dyson /* 701867a482dSJohn Dyson * calculate index into user supplied byte vector 702867a482dSJohn Dyson */ 703867a482dSJohn Dyson vecindex = OFF_TO_IDX(addr - first_addr); 704867a482dSJohn Dyson 705867a482dSJohn Dyson /* 706867a482dSJohn Dyson * If we have skipped map entries, we need to make sure that 707867a482dSJohn Dyson * the byte vector is zeroed for those skipped entries. 708867a482dSJohn Dyson */ 709867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 710867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 711867a482dSJohn Dyson if (error) { 712867a482dSJohn Dyson vm_map_unlock(map); 713867a482dSJohn Dyson return (EFAULT); 714867a482dSJohn Dyson } 715867a482dSJohn Dyson ++lastvecindex; 716867a482dSJohn Dyson } 717867a482dSJohn Dyson 718867a482dSJohn Dyson /* 719867a482dSJohn Dyson * Pass the page information to the user 720867a482dSJohn Dyson */ 721867a482dSJohn Dyson error = subyte( vec + vecindex, mincoreinfo); 722867a482dSJohn Dyson if (error) { 723867a482dSJohn Dyson vm_map_unlock(map); 724867a482dSJohn Dyson return (EFAULT); 725867a482dSJohn Dyson } 726867a482dSJohn Dyson lastvecindex = vecindex; 72702c04a2fSJohn Dyson addr += PAGE_SIZE; 72802c04a2fSJohn Dyson } 729867a482dSJohn Dyson } 730867a482dSJohn Dyson 731867a482dSJohn Dyson /* 732867a482dSJohn Dyson * Zero the last entries in the byte vector. 733867a482dSJohn Dyson */ 734867a482dSJohn Dyson vecindex = OFF_TO_IDX(end - first_addr); 735867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 736867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 737867a482dSJohn Dyson if (error) { 738867a482dSJohn Dyson vm_map_unlock(map); 739867a482dSJohn Dyson return (EFAULT); 740867a482dSJohn Dyson } 741867a482dSJohn Dyson ++lastvecindex; 742867a482dSJohn Dyson } 743867a482dSJohn Dyson 744867a482dSJohn Dyson vm_map_unlock(map); 74502c04a2fSJohn Dyson return (0); 746df8bae1dSRodney W. Grimes } 747df8bae1dSRodney W. Grimes 748d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 749df8bae1dSRodney W. Grimes struct mlock_args { 750df8bae1dSRodney W. Grimes caddr_t addr; 751df8bae1dSRodney W. Grimes size_t len; 752df8bae1dSRodney W. Grimes }; 753d2d3e875SBruce Evans #endif 754df8bae1dSRodney W. Grimes int 755df8bae1dSRodney W. Grimes mlock(p, uap, retval) 756df8bae1dSRodney W. Grimes struct proc *p; 757df8bae1dSRodney W. Grimes struct mlock_args *uap; 758df8bae1dSRodney W. Grimes int *retval; 759df8bae1dSRodney W. Grimes { 760df8bae1dSRodney W. Grimes vm_offset_t addr; 761dabee6feSPeter Wemm vm_size_t size, pageoff; 762df8bae1dSRodney W. Grimes int error; 763df8bae1dSRodney W. Grimes 764df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 7659154ee6aSPeter Wemm size = uap->len; 7669154ee6aSPeter Wemm 767dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 768dabee6feSPeter Wemm addr -= pageoff; 769dabee6feSPeter Wemm size += pageoff; 770dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 771dabee6feSPeter Wemm 772dabee6feSPeter Wemm /* disable wrap around */ 7739154ee6aSPeter Wemm if (addr + size < addr) 774df8bae1dSRodney W. Grimes return (EINVAL); 775dabee6feSPeter Wemm 776df8bae1dSRodney W. Grimes if (atop(size) + cnt.v_wire_count > vm_page_max_wired) 777df8bae1dSRodney W. Grimes return (EAGAIN); 7789154ee6aSPeter Wemm 779df8bae1dSRodney W. Grimes #ifdef pmap_wired_count 780df8bae1dSRodney W. Grimes if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 781df8bae1dSRodney W. Grimes p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 782df8bae1dSRodney W. Grimes return (EAGAIN); 783df8bae1dSRodney W. Grimes #else 78405f0fdd2SPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 78505f0fdd2SPoul-Henning Kamp if (error) 786df8bae1dSRodney W. Grimes return (error); 787df8bae1dSRodney W. Grimes #endif 788df8bae1dSRodney W. Grimes 789df8bae1dSRodney W. Grimes error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE); 790df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 791df8bae1dSRodney W. Grimes } 792df8bae1dSRodney W. Grimes 793d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 794df8bae1dSRodney W. Grimes struct munlock_args { 795df8bae1dSRodney W. Grimes caddr_t addr; 796df8bae1dSRodney W. Grimes size_t len; 797df8bae1dSRodney W. Grimes }; 798d2d3e875SBruce Evans #endif 799df8bae1dSRodney W. Grimes int 800df8bae1dSRodney W. Grimes munlock(p, uap, retval) 801df8bae1dSRodney W. Grimes struct proc *p; 802df8bae1dSRodney W. Grimes struct munlock_args *uap; 803df8bae1dSRodney W. Grimes int *retval; 804df8bae1dSRodney W. Grimes { 805df8bae1dSRodney W. Grimes vm_offset_t addr; 806dabee6feSPeter Wemm vm_size_t size, pageoff; 807df8bae1dSRodney W. Grimes int error; 808df8bae1dSRodney W. Grimes 809df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 8109154ee6aSPeter Wemm size = uap->len; 8119154ee6aSPeter Wemm 812dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 813dabee6feSPeter Wemm addr -= pageoff; 814dabee6feSPeter Wemm size += pageoff; 815dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 816dabee6feSPeter Wemm 817dabee6feSPeter Wemm /* disable wrap around */ 8189154ee6aSPeter Wemm if (addr + size < addr) 819df8bae1dSRodney W. Grimes return (EINVAL); 820dabee6feSPeter Wemm 821df8bae1dSRodney W. Grimes #ifndef pmap_wired_count 82205f0fdd2SPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 82305f0fdd2SPoul-Henning Kamp if (error) 824df8bae1dSRodney W. Grimes return (error); 825df8bae1dSRodney W. Grimes #endif 826df8bae1dSRodney W. Grimes 827df8bae1dSRodney W. Grimes error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE); 828df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 829df8bae1dSRodney W. Grimes } 830df8bae1dSRodney W. Grimes 831df8bae1dSRodney W. Grimes /* 832df8bae1dSRodney W. Grimes * Internal version of mmap. 833df8bae1dSRodney W. Grimes * Currently used by mmap, exec, and sys5 shared memory. 834df8bae1dSRodney W. Grimes * Handle is either a vnode pointer or NULL for MAP_ANON. 835df8bae1dSRodney W. Grimes */ 836df8bae1dSRodney W. Grimes int 837df8bae1dSRodney W. Grimes vm_mmap(map, addr, size, prot, maxprot, flags, handle, foff) 838df8bae1dSRodney W. Grimes register vm_map_t map; 839df8bae1dSRodney W. Grimes register vm_offset_t *addr; 840df8bae1dSRodney W. Grimes register vm_size_t size; 841df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 842df8bae1dSRodney W. Grimes register int flags; 843df8bae1dSRodney W. Grimes caddr_t handle; /* XXX should be vp */ 844a316d390SJohn Dyson vm_ooffset_t foff; 845df8bae1dSRodney W. Grimes { 846df8bae1dSRodney W. Grimes boolean_t fitit; 84767bf6868SJohn Dyson vm_object_t object, object2; 848df8bae1dSRodney W. Grimes struct vnode *vp = NULL; 84924a1cce3SDavid Greenman objtype_t type; 850df8bae1dSRodney W. Grimes int rv = KERN_SUCCESS; 851bd7e5f99SJohn Dyson vm_ooffset_t objsize; 852bd7e5f99SJohn Dyson int docow; 85306cb7259SDavid Greenman struct proc *p = curproc; 854df8bae1dSRodney W. Grimes 855df8bae1dSRodney W. Grimes if (size == 0) 856df8bae1dSRodney W. Grimes return (0); 857df8bae1dSRodney W. Grimes 85806cb7259SDavid Greenman objsize = size = round_page(size); 859df8bae1dSRodney W. Grimes 860df8bae1dSRodney W. Grimes /* 861bc9ad247SDavid Greenman * We currently can only deal with page aligned file offsets. 862bc9ad247SDavid Greenman * The check is here rather than in the syscall because the 863bc9ad247SDavid Greenman * kernel calls this function internally for other mmaping 864bc9ad247SDavid Greenman * operations (such as in exec) and non-aligned offsets will 865bc9ad247SDavid Greenman * cause pmap inconsistencies...so we want to be sure to 866bc9ad247SDavid Greenman * disallow this in all cases. 867bc9ad247SDavid Greenman */ 868bc9ad247SDavid Greenman if (foff & PAGE_MASK) 869bc9ad247SDavid Greenman return (EINVAL); 870bc9ad247SDavid Greenman 87106cb7259SDavid Greenman if ((flags & MAP_FIXED) == 0) { 87206cb7259SDavid Greenman fitit = TRUE; 87306cb7259SDavid Greenman *addr = round_page(*addr); 87406cb7259SDavid Greenman } else { 87506cb7259SDavid Greenman if (*addr != trunc_page(*addr)) 87606cb7259SDavid Greenman return (EINVAL); 87706cb7259SDavid Greenman fitit = FALSE; 87806cb7259SDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 87906cb7259SDavid Greenman } 88006cb7259SDavid Greenman 881bc9ad247SDavid Greenman /* 88224a1cce3SDavid Greenman * Lookup/allocate object. 883df8bae1dSRodney W. Grimes */ 8845f55e841SDavid Greenman if (flags & MAP_ANON) { 88567bf6868SJohn Dyson type = OBJT_SWAP; 8865f55e841SDavid Greenman /* 8875f55e841SDavid Greenman * Unnamed anonymous regions always start at 0. 8885f55e841SDavid Greenman */ 88967bf6868SJohn Dyson if (handle == 0) 8905f55e841SDavid Greenman foff = 0; 8915f55e841SDavid Greenman } else { 892df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 893df8bae1dSRodney W. Grimes if (vp->v_type == VCHR) { 89424a1cce3SDavid Greenman type = OBJT_DEVICE; 895df8bae1dSRodney W. Grimes handle = (caddr_t) vp->v_rdev; 89606cb7259SDavid Greenman } else { 89706cb7259SDavid Greenman struct vattr vat; 89806cb7259SDavid Greenman int error; 89906cb7259SDavid Greenman 90006cb7259SDavid Greenman error = VOP_GETATTR(vp, &vat, p->p_ucred, p); 90106cb7259SDavid Greenman if (error) 90206cb7259SDavid Greenman return (error); 903bd7e5f99SJohn Dyson objsize = round_page(vat.va_size); 90424a1cce3SDavid Greenman type = OBJT_VNODE; 905df8bae1dSRodney W. Grimes } 90606cb7259SDavid Greenman } 90767bf6868SJohn Dyson object = vm_pager_allocate(type, handle, OFF_TO_IDX(objsize), prot, foff); 90824a1cce3SDavid Greenman if (object == NULL) 90924a1cce3SDavid Greenman return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 910df8bae1dSRodney W. Grimes 9115850152dSJohn Dyson /* 9128f2ec877SDavid Greenman * Force device mappings to be shared. 9135850152dSJohn Dyson */ 9148f2ec877SDavid Greenman if (type == OBJT_DEVICE) { 9158f2ec877SDavid Greenman flags &= ~(MAP_PRIVATE|MAP_COPY); 9165850152dSJohn Dyson flags |= MAP_SHARED; 9178f2ec877SDavid Greenman } 9185850152dSJohn Dyson 91967bf6868SJohn Dyson object2 = NULL; 920bd7e5f99SJohn Dyson docow = 0; 9215850152dSJohn Dyson if ((flags & (MAP_ANON|MAP_SHARED)) == 0) { 92267bf6868SJohn Dyson docow = MAP_COPY_ON_WRITE; 92367bf6868SJohn Dyson if (objsize < size) { 92467bf6868SJohn Dyson object2 = vm_object_allocate( OBJT_DEFAULT, 92567bf6868SJohn Dyson OFF_TO_IDX(size - (foff & ~PAGE_MASK))); 92667bf6868SJohn Dyson object2->backing_object = object; 92767bf6868SJohn Dyson object2->backing_object_offset = foff; 92867bf6868SJohn Dyson TAILQ_INSERT_TAIL(&object->shadow_head, 92967bf6868SJohn Dyson object2, shadow_list); 93067bf6868SJohn Dyson ++object->shadow_count; 93167bf6868SJohn Dyson } else { 93267bf6868SJohn Dyson docow |= MAP_COPY_NEEDED; 93367bf6868SJohn Dyson } 934bd7e5f99SJohn Dyson } 9355850152dSJohn Dyson 93667bf6868SJohn Dyson if (object2) 93767bf6868SJohn Dyson rv = vm_map_find(map, object2, 0, addr, size, fitit, 93867bf6868SJohn Dyson prot, maxprot, docow); 93967bf6868SJohn Dyson else 940bd7e5f99SJohn Dyson rv = vm_map_find(map, object, foff, addr, size, fitit, 941bd7e5f99SJohn Dyson prot, maxprot, docow); 942bd7e5f99SJohn Dyson 94367bf6868SJohn Dyson 944df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 9457fb0c17eSDavid Greenman /* 94624a1cce3SDavid Greenman * Lose the object reference. Will destroy the 94724a1cce3SDavid Greenman * object if it's an unnamed anonymous mapping 94824a1cce3SDavid Greenman * or named anonymous without other references. 9497fb0c17eSDavid Greenman */ 95067bf6868SJohn Dyson if (object2) 95167bf6868SJohn Dyson vm_object_deallocate(object2); 95267bf6868SJohn Dyson else 953df8bae1dSRodney W. Grimes vm_object_deallocate(object); 954df8bae1dSRodney W. Grimes goto out; 955df8bae1dSRodney W. Grimes } 956e17bed12SJohn Dyson 957df8bae1dSRodney W. Grimes /* 9587fb0c17eSDavid Greenman * "Pre-fault" resident pages. 9597fb0c17eSDavid Greenman */ 960b18bfc3dSJohn Dyson if ((type == OBJT_VNODE) && (map->pmap != NULL)) { 961a316d390SJohn Dyson pmap_object_init_pt(map->pmap, *addr, 962867a482dSJohn Dyson object, (vm_pindex_t) OFF_TO_IDX(foff), size, 1); 963df8bae1dSRodney W. Grimes } 9647fb0c17eSDavid Greenman 965df8bae1dSRodney W. Grimes /* 966df8bae1dSRodney W. Grimes * Shared memory is also shared with children. 967df8bae1dSRodney W. Grimes */ 9685850152dSJohn Dyson if (flags & (MAP_SHARED|MAP_INHERIT)) { 969df8bae1dSRodney W. Grimes rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 970df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 9717fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 972df8bae1dSRodney W. Grimes goto out; 973df8bae1dSRodney W. Grimes } 974df8bae1dSRodney W. Grimes } 975df8bae1dSRodney W. Grimes out: 976df8bae1dSRodney W. Grimes switch (rv) { 977df8bae1dSRodney W. Grimes case KERN_SUCCESS: 978df8bae1dSRodney W. Grimes return (0); 979df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 980df8bae1dSRodney W. Grimes case KERN_NO_SPACE: 981df8bae1dSRodney W. Grimes return (ENOMEM); 982df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 983df8bae1dSRodney W. Grimes return (EACCES); 984df8bae1dSRodney W. Grimes default: 985df8bae1dSRodney W. Grimes return (EINVAL); 986df8bae1dSRodney W. Grimes } 987df8bae1dSRodney W. Grimes } 988