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 41d0aea04fSJohn Dyson * $Id: vm_mmap.c,v 1.56 1996/12/28 22:40:44 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 48e9822d92SJoerg Wunsch #include "opt_rlimit.h" 49e9822d92SJoerg Wunsch 50df8bae1dSRodney W. Grimes #include <sys/param.h> 51df8bae1dSRodney W. Grimes #include <sys/systm.h> 52d2d3e875SBruce Evans #include <sys/sysproto.h> 53df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 54df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 55df8bae1dSRodney W. Grimes #include <sys/proc.h> 56df8bae1dSRodney W. Grimes #include <sys/vnode.h> 57df8bae1dSRodney W. Grimes #include <sys/file.h> 58df8bae1dSRodney W. Grimes #include <sys/mman.h> 59df8bae1dSRodney W. Grimes #include <sys/conf.h> 60efeaf95aSDavid Greenman #include <sys/vmmeter.h> 61df8bae1dSRodney W. Grimes 62df8bae1dSRodney W. Grimes #include <miscfs/specfs/specdev.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #include <vm/vm.h> 65efeaf95aSDavid Greenman #include <vm/vm_param.h> 66efeaf95aSDavid Greenman #include <vm/vm_prot.h> 67efeaf95aSDavid Greenman #include <vm/vm_inherit.h> 68efeaf95aSDavid Greenman #include <vm/lock.h> 69efeaf95aSDavid Greenman #include <vm/pmap.h> 70efeaf95aSDavid Greenman #include <vm/vm_map.h> 71efeaf95aSDavid Greenman #include <vm/vm_object.h> 72df8bae1dSRodney W. Grimes #include <vm/vm_pager.h> 73b5e8ce9fSBruce Evans #include <vm/vm_pageout.h> 74efeaf95aSDavid Greenman #include <vm/vm_extern.h> 75bd7e5f99SJohn Dyson #include <vm/vm_kern.h> 76867a482dSJohn Dyson #include <vm/vm_page.h> 77df8bae1dSRodney W. Grimes 78d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 79df8bae1dSRodney W. Grimes struct sbrk_args { 80df8bae1dSRodney W. Grimes int incr; 81df8bae1dSRodney W. Grimes }; 82d2d3e875SBruce Evans #endif 830d94caffSDavid Greenman 84df8bae1dSRodney W. Grimes /* ARGSUSED */ 85df8bae1dSRodney W. Grimes int 86df8bae1dSRodney W. Grimes sbrk(p, uap, retval) 87df8bae1dSRodney W. Grimes struct proc *p; 88df8bae1dSRodney W. Grimes struct sbrk_args *uap; 89df8bae1dSRodney W. Grimes int *retval; 90df8bae1dSRodney W. Grimes { 91df8bae1dSRodney W. Grimes 92df8bae1dSRodney W. Grimes /* Not yet implemented */ 93df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 94df8bae1dSRodney W. Grimes } 95df8bae1dSRodney W. Grimes 96d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 97df8bae1dSRodney W. Grimes struct sstk_args { 98df8bae1dSRodney W. Grimes int incr; 99df8bae1dSRodney W. Grimes }; 100d2d3e875SBruce Evans #endif 1010d94caffSDavid Greenman 102df8bae1dSRodney W. Grimes /* ARGSUSED */ 103df8bae1dSRodney W. Grimes int 104df8bae1dSRodney W. Grimes sstk(p, uap, retval) 105df8bae1dSRodney W. Grimes struct proc *p; 106df8bae1dSRodney W. Grimes struct sstk_args *uap; 107df8bae1dSRodney W. Grimes int *retval; 108df8bae1dSRodney W. Grimes { 109df8bae1dSRodney W. Grimes 110df8bae1dSRodney W. Grimes /* Not yet implemented */ 111df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 114df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 115d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 116df8bae1dSRodney W. Grimes struct getpagesize_args { 117df8bae1dSRodney W. Grimes int dummy; 118df8bae1dSRodney W. Grimes }; 119d2d3e875SBruce Evans #endif 1200d94caffSDavid Greenman 121df8bae1dSRodney W. Grimes /* ARGSUSED */ 122df8bae1dSRodney W. Grimes int 123df8bae1dSRodney W. Grimes ogetpagesize(p, uap, retval) 124df8bae1dSRodney W. Grimes struct proc *p; 125df8bae1dSRodney W. Grimes struct getpagesize_args *uap; 126df8bae1dSRodney W. Grimes int *retval; 127df8bae1dSRodney W. Grimes { 128df8bae1dSRodney W. Grimes 129df8bae1dSRodney W. Grimes *retval = PAGE_SIZE; 130df8bae1dSRodney W. Grimes return (0); 131df8bae1dSRodney W. Grimes } 132df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 133df8bae1dSRodney W. Grimes 134d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 135df8bae1dSRodney W. Grimes struct mmap_args { 136df8bae1dSRodney W. Grimes caddr_t addr; 137df8bae1dSRodney W. Grimes size_t len; 138df8bae1dSRodney W. Grimes int prot; 139df8bae1dSRodney W. Grimes int flags; 140df8bae1dSRodney W. Grimes int fd; 141df8bae1dSRodney W. Grimes long pad; 142df8bae1dSRodney W. Grimes off_t pos; 143df8bae1dSRodney W. Grimes }; 144d2d3e875SBruce Evans #endif 145df8bae1dSRodney W. Grimes 146df8bae1dSRodney W. Grimes int 147df8bae1dSRodney W. Grimes mmap(p, uap, retval) 148df8bae1dSRodney W. Grimes struct proc *p; 149df8bae1dSRodney W. Grimes register struct mmap_args *uap; 150df8bae1dSRodney W. Grimes int *retval; 151df8bae1dSRodney W. Grimes { 152df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 153df8bae1dSRodney W. Grimes register struct file *fp; 154df8bae1dSRodney W. Grimes struct vnode *vp; 155df8bae1dSRodney W. Grimes vm_offset_t addr; 1569154ee6aSPeter Wemm vm_size_t size, pageoff; 157df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 158df8bae1dSRodney W. Grimes caddr_t handle; 159df8bae1dSRodney W. Grimes int flags, error; 160df8bae1dSRodney W. Grimes 161df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 162df8bae1dSRodney W. Grimes flags = uap->flags; 163df8bae1dSRodney W. Grimes /* 1640d94caffSDavid Greenman * Address (if FIXED) must be page aligned. Size is implicitly rounded 1650d94caffSDavid Greenman * to a page boundary. 166df8bae1dSRodney W. Grimes */ 167df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 168df8bae1dSRodney W. Grimes if (((flags & MAP_FIXED) && (addr & PAGE_MASK)) || 169df8bae1dSRodney W. Grimes (ssize_t) uap->len < 0 || ((flags & MAP_ANON) && uap->fd != -1)) 170df8bae1dSRodney W. Grimes return (EINVAL); 1719154ee6aSPeter Wemm 1729154ee6aSPeter Wemm /* 1739154ee6aSPeter Wemm * Round page if not already disallowed by above test 1749154ee6aSPeter Wemm * XXX: Is there any point in the MAP_FIXED align requirement above? 1759154ee6aSPeter Wemm */ 1769154ee6aSPeter Wemm size = uap->len; 1779154ee6aSPeter Wemm pageoff = (addr & PAGE_MASK); 1789154ee6aSPeter Wemm addr -= pageoff; 1799154ee6aSPeter Wemm size += pageoff; 1809154ee6aSPeter Wemm size = (vm_size_t) round_page(size); 1819154ee6aSPeter Wemm 182df8bae1dSRodney W. Grimes /* 1830d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 1840d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 185df8bae1dSRodney W. Grimes */ 186df8bae1dSRodney W. Grimes if (flags & MAP_FIXED) { 187bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 188df8bae1dSRodney W. Grimes return (EINVAL); 18926f9a767SRodney W. Grimes #ifndef i386 190df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 191df8bae1dSRodney W. Grimes return (EINVAL); 19226f9a767SRodney W. Grimes #endif 193bbc0ec52SDavid Greenman if (addr + size < addr) 194df8bae1dSRodney W. Grimes return (EINVAL); 195df8bae1dSRodney W. Grimes } 196df8bae1dSRodney W. Grimes /* 1970d94caffSDavid Greenman * XXX if no hint provided for a non-fixed mapping place it after the 1980d94caffSDavid Greenman * end of the largest possible heap. 199df8bae1dSRodney W. Grimes * 2000d94caffSDavid Greenman * There should really be a pmap call to determine a reasonable location. 201df8bae1dSRodney W. Grimes */ 202df8bae1dSRodney W. Grimes if (addr == 0 && (flags & MAP_FIXED) == 0) 203df8bae1dSRodney W. Grimes addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ); 204df8bae1dSRodney W. Grimes if (flags & MAP_ANON) { 205df8bae1dSRodney W. Grimes /* 206df8bae1dSRodney W. Grimes * Mapping blank space is trivial. 207df8bae1dSRodney W. Grimes */ 208df8bae1dSRodney W. Grimes handle = NULL; 209df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 210df8bae1dSRodney W. Grimes } else { 211df8bae1dSRodney W. Grimes /* 2120d94caffSDavid Greenman * Mapping file, get fp for validation. Obtain vnode and make 2130d94caffSDavid Greenman * sure it is of appropriate type. 214df8bae1dSRodney W. Grimes */ 215df8bae1dSRodney W. Grimes if (((unsigned) uap->fd) >= fdp->fd_nfiles || 216df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL) 217df8bae1dSRodney W. Grimes return (EBADF); 218df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_VNODE) 219df8bae1dSRodney W. Grimes return (EINVAL); 220df8bae1dSRodney W. Grimes vp = (struct vnode *) fp->f_data; 221df8bae1dSRodney W. Grimes if (vp->v_type != VREG && vp->v_type != VCHR) 222df8bae1dSRodney W. Grimes return (EINVAL); 223df8bae1dSRodney W. Grimes /* 2240d94caffSDavid Greenman * XXX hack to handle use of /dev/zero to map anon memory (ala 2250d94caffSDavid Greenman * SunOS). 226df8bae1dSRodney W. Grimes */ 227df8bae1dSRodney W. Grimes if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) { 228df8bae1dSRodney W. Grimes handle = NULL; 229df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 230df8bae1dSRodney W. Grimes flags |= MAP_ANON; 231df8bae1dSRodney W. Grimes } else { 232df8bae1dSRodney W. Grimes /* 233df8bae1dSRodney W. Grimes * Ensure that file and memory protections are 234df8bae1dSRodney W. Grimes * compatible. Note that we only worry about 235df8bae1dSRodney W. Grimes * writability if mapping is shared; in this case, 236df8bae1dSRodney W. Grimes * current and max prot are dictated by the open file. 237df8bae1dSRodney W. Grimes * XXX use the vnode instead? Problem is: what 2380d94caffSDavid Greenman * credentials do we use for determination? What if 2390d94caffSDavid Greenman * proc does a setuid? 240df8bae1dSRodney W. Grimes */ 241df8bae1dSRodney W. Grimes maxprot = VM_PROT_EXECUTE; /* ??? */ 242df8bae1dSRodney W. Grimes if (fp->f_flag & FREAD) 243df8bae1dSRodney W. Grimes maxprot |= VM_PROT_READ; 244df8bae1dSRodney W. Grimes else if (prot & PROT_READ) 245df8bae1dSRodney W. Grimes return (EACCES); 246df8bae1dSRodney W. Grimes if (flags & MAP_SHARED) { 247df8bae1dSRodney W. Grimes if (fp->f_flag & FWRITE) 248df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 249df8bae1dSRodney W. Grimes else if (prot & PROT_WRITE) 250df8bae1dSRodney W. Grimes return (EACCES); 251df8bae1dSRodney W. Grimes } else 252df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 253df8bae1dSRodney W. Grimes handle = (caddr_t) vp; 254df8bae1dSRodney W. Grimes } 255df8bae1dSRodney W. Grimes } 256df8bae1dSRodney W. Grimes error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot, 257a316d390SJohn Dyson flags, handle, uap->pos); 258df8bae1dSRodney W. Grimes if (error == 0) 259df8bae1dSRodney W. Grimes *retval = (int) addr; 260df8bae1dSRodney W. Grimes return (error); 261df8bae1dSRodney W. Grimes } 262df8bae1dSRodney W. Grimes 26305f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43 264d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 26505f0fdd2SPoul-Henning Kamp struct ommap_args { 26605f0fdd2SPoul-Henning Kamp caddr_t addr; 26705f0fdd2SPoul-Henning Kamp int len; 26805f0fdd2SPoul-Henning Kamp int prot; 26905f0fdd2SPoul-Henning Kamp int flags; 27005f0fdd2SPoul-Henning Kamp int fd; 27105f0fdd2SPoul-Henning Kamp long pos; 27205f0fdd2SPoul-Henning Kamp }; 273d2d3e875SBruce Evans #endif 27405f0fdd2SPoul-Henning Kamp int 27505f0fdd2SPoul-Henning Kamp ommap(p, uap, retval) 27605f0fdd2SPoul-Henning Kamp struct proc *p; 27705f0fdd2SPoul-Henning Kamp register struct ommap_args *uap; 27805f0fdd2SPoul-Henning Kamp int *retval; 27905f0fdd2SPoul-Henning Kamp { 28005f0fdd2SPoul-Henning Kamp struct mmap_args nargs; 28105f0fdd2SPoul-Henning Kamp static const char cvtbsdprot[8] = { 28205f0fdd2SPoul-Henning Kamp 0, 28305f0fdd2SPoul-Henning Kamp PROT_EXEC, 28405f0fdd2SPoul-Henning Kamp PROT_WRITE, 28505f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE, 28605f0fdd2SPoul-Henning Kamp PROT_READ, 28705f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_READ, 28805f0fdd2SPoul-Henning Kamp PROT_WRITE | PROT_READ, 28905f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE | PROT_READ, 29005f0fdd2SPoul-Henning Kamp }; 2910d94caffSDavid Greenman 29205f0fdd2SPoul-Henning Kamp #define OMAP_ANON 0x0002 29305f0fdd2SPoul-Henning Kamp #define OMAP_COPY 0x0020 29405f0fdd2SPoul-Henning Kamp #define OMAP_SHARED 0x0010 29505f0fdd2SPoul-Henning Kamp #define OMAP_FIXED 0x0100 29605f0fdd2SPoul-Henning Kamp #define OMAP_INHERIT 0x0800 29705f0fdd2SPoul-Henning Kamp 29805f0fdd2SPoul-Henning Kamp nargs.addr = uap->addr; 29905f0fdd2SPoul-Henning Kamp nargs.len = uap->len; 30005f0fdd2SPoul-Henning Kamp nargs.prot = cvtbsdprot[uap->prot & 0x7]; 30105f0fdd2SPoul-Henning Kamp nargs.flags = 0; 30205f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_ANON) 30305f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_ANON; 30405f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_COPY) 30505f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_COPY; 30605f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_SHARED) 30705f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_SHARED; 30805f0fdd2SPoul-Henning Kamp else 30905f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_PRIVATE; 31005f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_FIXED) 31105f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_FIXED; 31205f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_INHERIT) 31305f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_INHERIT; 31405f0fdd2SPoul-Henning Kamp nargs.fd = uap->fd; 31505f0fdd2SPoul-Henning Kamp nargs.pos = uap->pos; 31605f0fdd2SPoul-Henning Kamp return (mmap(p, &nargs, retval)); 31705f0fdd2SPoul-Henning Kamp } 31805f0fdd2SPoul-Henning Kamp #endif /* COMPAT_43 */ 31905f0fdd2SPoul-Henning Kamp 32005f0fdd2SPoul-Henning Kamp 321d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 322df8bae1dSRodney W. Grimes struct msync_args { 323df8bae1dSRodney W. Grimes caddr_t addr; 324df8bae1dSRodney W. Grimes int len; 325e6c6af11SDavid Greenman int flags; 326df8bae1dSRodney W. Grimes }; 327d2d3e875SBruce Evans #endif 328df8bae1dSRodney W. Grimes int 329df8bae1dSRodney W. Grimes msync(p, uap, retval) 330df8bae1dSRodney W. Grimes struct proc *p; 331df8bae1dSRodney W. Grimes struct msync_args *uap; 332df8bae1dSRodney W. Grimes int *retval; 333df8bae1dSRodney W. Grimes { 334df8bae1dSRodney W. Grimes vm_offset_t addr; 335dabee6feSPeter Wemm vm_size_t size, pageoff; 336e6c6af11SDavid Greenman int flags; 337df8bae1dSRodney W. Grimes vm_map_t map; 338df8bae1dSRodney W. Grimes int rv; 339df8bae1dSRodney W. Grimes 340df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 3419154ee6aSPeter Wemm size = uap->len; 342e6c6af11SDavid Greenman flags = uap->flags; 343e6c6af11SDavid Greenman 344dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 345dabee6feSPeter Wemm addr -= pageoff; 346dabee6feSPeter Wemm size += pageoff; 347dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 3489154ee6aSPeter Wemm if (addr + size < addr) 349dabee6feSPeter Wemm return(EINVAL); 350dabee6feSPeter Wemm 351dabee6feSPeter Wemm if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 3521e62bc63SDavid Greenman return (EINVAL); 3531e62bc63SDavid Greenman 3549154ee6aSPeter Wemm map = &p->p_vmspace->vm_map; 3559154ee6aSPeter Wemm 356df8bae1dSRodney W. Grimes /* 357df8bae1dSRodney W. Grimes * XXX Gak! If size is zero we are supposed to sync "all modified 3580d94caffSDavid Greenman * pages with the region containing addr". Unfortunately, we don't 3590d94caffSDavid Greenman * really keep track of individual mmaps so we approximate by flushing 3600d94caffSDavid Greenman * the range of the map entry containing addr. This can be incorrect 3610d94caffSDavid Greenman * if the region splits or is coalesced with a neighbor. 362df8bae1dSRodney W. Grimes */ 363df8bae1dSRodney W. Grimes if (size == 0) { 364df8bae1dSRodney W. Grimes vm_map_entry_t entry; 365df8bae1dSRodney W. Grimes 366df8bae1dSRodney W. Grimes vm_map_lock_read(map); 367df8bae1dSRodney W. Grimes rv = vm_map_lookup_entry(map, addr, &entry); 368df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 369fbcfcdf7SDavid Greenman if (rv == FALSE) 370df8bae1dSRodney W. Grimes return (EINVAL); 371df8bae1dSRodney W. Grimes addr = entry->start; 372df8bae1dSRodney W. Grimes size = entry->end - entry->start; 373df8bae1dSRodney W. Grimes } 374e6c6af11SDavid Greenman 375df8bae1dSRodney W. Grimes /* 376df8bae1dSRodney W. Grimes * Clean the pages and interpret the return value. 377df8bae1dSRodney W. Grimes */ 3786c534ad8SDavid Greenman rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 379e6c6af11SDavid Greenman (flags & MS_INVALIDATE) != 0); 380e6c6af11SDavid Greenman 381df8bae1dSRodney W. Grimes switch (rv) { 382df8bae1dSRodney W. Grimes case KERN_SUCCESS: 383df8bae1dSRodney W. Grimes break; 384df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 385df8bae1dSRodney W. Grimes return (EINVAL); /* Sun returns ENOMEM? */ 386df8bae1dSRodney W. Grimes case KERN_FAILURE: 387df8bae1dSRodney W. Grimes return (EIO); 388df8bae1dSRodney W. Grimes default: 389df8bae1dSRodney W. Grimes return (EINVAL); 390df8bae1dSRodney W. Grimes } 391e6c6af11SDavid Greenman 392df8bae1dSRodney W. Grimes return (0); 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes 395d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 396df8bae1dSRodney W. Grimes struct munmap_args { 397df8bae1dSRodney W. Grimes caddr_t addr; 3989154ee6aSPeter Wemm size_t len; 399df8bae1dSRodney W. Grimes }; 400d2d3e875SBruce Evans #endif 401df8bae1dSRodney W. Grimes int 402df8bae1dSRodney W. Grimes munmap(p, uap, retval) 403df8bae1dSRodney W. Grimes register struct proc *p; 404df8bae1dSRodney W. Grimes register struct munmap_args *uap; 405df8bae1dSRodney W. Grimes int *retval; 406df8bae1dSRodney W. Grimes { 407df8bae1dSRodney W. Grimes vm_offset_t addr; 408dabee6feSPeter Wemm vm_size_t size, pageoff; 409df8bae1dSRodney W. Grimes vm_map_t map; 410df8bae1dSRodney W. Grimes 411df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 4129154ee6aSPeter Wemm size = uap->len; 413dabee6feSPeter Wemm 414dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 415dabee6feSPeter Wemm addr -= pageoff; 416dabee6feSPeter Wemm size += pageoff; 417dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 4189154ee6aSPeter Wemm if (addr + size < addr) 419df8bae1dSRodney W. Grimes return(EINVAL); 4209154ee6aSPeter Wemm 421df8bae1dSRodney W. Grimes if (size == 0) 422df8bae1dSRodney W. Grimes return (0); 423dabee6feSPeter Wemm 424df8bae1dSRodney W. Grimes /* 4250d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 4260d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 427df8bae1dSRodney W. Grimes */ 428bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 429df8bae1dSRodney W. Grimes return (EINVAL); 43026f9a767SRodney W. Grimes #ifndef i386 431df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 432df8bae1dSRodney W. Grimes return (EINVAL); 43326f9a767SRodney W. Grimes #endif 434bbc0ec52SDavid Greenman if (addr + size < addr) 435df8bae1dSRodney W. Grimes return (EINVAL); 436df8bae1dSRodney W. Grimes map = &p->p_vmspace->vm_map; 437df8bae1dSRodney W. Grimes /* 438df8bae1dSRodney W. Grimes * Make sure entire range is allocated. 439df8bae1dSRodney W. Grimes */ 440df8bae1dSRodney W. Grimes if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) 441df8bae1dSRodney W. Grimes return (EINVAL); 442df8bae1dSRodney W. Grimes /* returns nothing but KERN_SUCCESS anyway */ 443df8bae1dSRodney W. Grimes (void) vm_map_remove(map, addr, addr + size); 444df8bae1dSRodney W. Grimes return (0); 445df8bae1dSRodney W. Grimes } 446df8bae1dSRodney W. Grimes 447df8bae1dSRodney W. Grimes void 44890324b07SDavid Greenman munmapfd(p, fd) 44990324b07SDavid Greenman struct proc *p; 450df8bae1dSRodney W. Grimes int fd; 451df8bae1dSRodney W. Grimes { 452df8bae1dSRodney W. Grimes /* 453c4ed5a07SDavid Greenman * XXX should unmap any regions mapped to this file 454df8bae1dSRodney W. Grimes */ 45590324b07SDavid Greenman p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED; 456df8bae1dSRodney W. Grimes } 457df8bae1dSRodney W. Grimes 458d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 459df8bae1dSRodney W. Grimes struct mprotect_args { 460df8bae1dSRodney W. Grimes caddr_t addr; 4619154ee6aSPeter Wemm size_t len; 462df8bae1dSRodney W. Grimes int prot; 463df8bae1dSRodney W. Grimes }; 464d2d3e875SBruce Evans #endif 465df8bae1dSRodney W. Grimes int 466df8bae1dSRodney W. Grimes mprotect(p, uap, retval) 467df8bae1dSRodney W. Grimes struct proc *p; 468df8bae1dSRodney W. Grimes struct mprotect_args *uap; 469df8bae1dSRodney W. Grimes int *retval; 470df8bae1dSRodney W. Grimes { 471df8bae1dSRodney W. Grimes vm_offset_t addr; 472dabee6feSPeter Wemm vm_size_t size, pageoff; 473df8bae1dSRodney W. Grimes register vm_prot_t prot; 474df8bae1dSRodney W. Grimes 475df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 4769154ee6aSPeter Wemm size = uap->len; 477df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 478d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 479d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 480d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 481d0aea04fSJohn Dyson #endif 482df8bae1dSRodney W. Grimes 483dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 484dabee6feSPeter Wemm addr -= pageoff; 485dabee6feSPeter Wemm size += pageoff; 486dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 4879154ee6aSPeter Wemm if (addr + size < addr) 488dabee6feSPeter Wemm return(EINVAL); 489dabee6feSPeter Wemm 490df8bae1dSRodney W. Grimes switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot, 491df8bae1dSRodney W. Grimes FALSE)) { 492df8bae1dSRodney W. Grimes case KERN_SUCCESS: 493df8bae1dSRodney W. Grimes return (0); 494df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 495df8bae1dSRodney W. Grimes return (EACCES); 496df8bae1dSRodney W. Grimes } 497df8bae1dSRodney W. Grimes return (EINVAL); 498df8bae1dSRodney W. Grimes } 499df8bae1dSRodney W. Grimes 500d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 501dabee6feSPeter Wemm struct minherit_args { 502dabee6feSPeter Wemm caddr_t addr; 5039154ee6aSPeter Wemm size_t len; 504dabee6feSPeter Wemm int inherit; 505dabee6feSPeter Wemm }; 506dabee6feSPeter Wemm #endif 507dabee6feSPeter Wemm int 508dabee6feSPeter Wemm minherit(p, uap, retval) 509dabee6feSPeter Wemm struct proc *p; 510dabee6feSPeter Wemm struct minherit_args *uap; 511dabee6feSPeter Wemm int *retval; 512dabee6feSPeter Wemm { 513dabee6feSPeter Wemm vm_offset_t addr; 514dabee6feSPeter Wemm vm_size_t size, pageoff; 515dabee6feSPeter Wemm register vm_inherit_t inherit; 516dabee6feSPeter Wemm 517dabee6feSPeter Wemm addr = (vm_offset_t)uap->addr; 5189154ee6aSPeter Wemm size = uap->len; 519dabee6feSPeter Wemm inherit = uap->inherit; 520dabee6feSPeter Wemm 521dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 522dabee6feSPeter Wemm addr -= pageoff; 523dabee6feSPeter Wemm size += pageoff; 524dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 5259154ee6aSPeter Wemm if (addr + size < addr) 526dabee6feSPeter Wemm return(EINVAL); 527dabee6feSPeter Wemm 528dabee6feSPeter Wemm switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size, 529dabee6feSPeter Wemm inherit)) { 530dabee6feSPeter Wemm case KERN_SUCCESS: 531dabee6feSPeter Wemm return (0); 532dabee6feSPeter Wemm case KERN_PROTECTION_FAILURE: 533dabee6feSPeter Wemm return (EACCES); 534dabee6feSPeter Wemm } 535dabee6feSPeter Wemm return (EINVAL); 536dabee6feSPeter Wemm } 537dabee6feSPeter Wemm 538dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 539df8bae1dSRodney W. Grimes struct madvise_args { 540df8bae1dSRodney W. Grimes caddr_t addr; 5419154ee6aSPeter Wemm size_t len; 542df8bae1dSRodney W. Grimes int behav; 543df8bae1dSRodney W. Grimes }; 544d2d3e875SBruce Evans #endif 5450d94caffSDavid Greenman 546df8bae1dSRodney W. Grimes /* ARGSUSED */ 547df8bae1dSRodney W. Grimes int 548df8bae1dSRodney W. Grimes madvise(p, uap, retval) 549df8bae1dSRodney W. Grimes struct proc *p; 550df8bae1dSRodney W. Grimes struct madvise_args *uap; 551df8bae1dSRodney W. Grimes int *retval; 552df8bae1dSRodney W. Grimes { 553867a482dSJohn Dyson vm_map_t map; 554867a482dSJohn Dyson pmap_t pmap; 555f35329acSJohn Dyson vm_offset_t start, end; 556867a482dSJohn Dyson /* 557867a482dSJohn Dyson * Check for illegal addresses. Watch out for address wrap... Note 558867a482dSJohn Dyson * that VM_*_ADDRESS are not constants due to casts (argh). 559867a482dSJohn Dyson */ 560867a482dSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && 561867a482dSJohn Dyson ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS) 562867a482dSJohn Dyson return (EINVAL); 563867a482dSJohn Dyson #ifndef i386 564867a482dSJohn Dyson if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS) 565867a482dSJohn Dyson return (EINVAL); 566867a482dSJohn Dyson #endif 567867a482dSJohn Dyson if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr) 568867a482dSJohn Dyson return (EINVAL); 569867a482dSJohn Dyson 570867a482dSJohn Dyson /* 571867a482dSJohn Dyson * Since this routine is only advisory, we default to conservative 572867a482dSJohn Dyson * behavior. 573867a482dSJohn Dyson */ 574cd6eea25SDavid Greenman start = trunc_page((vm_offset_t) uap->addr); 575cd6eea25SDavid Greenman end = round_page((vm_offset_t) uap->addr + uap->len); 576867a482dSJohn Dyson 577867a482dSJohn Dyson map = &p->p_vmspace->vm_map; 578867a482dSJohn Dyson pmap = &p->p_vmspace->vm_pmap; 579867a482dSJohn Dyson 580867a482dSJohn Dyson vm_map_madvise(map, pmap, start, end, uap->behav); 581df8bae1dSRodney W. Grimes 582867a482dSJohn Dyson return (0); 583df8bae1dSRodney W. Grimes } 584df8bae1dSRodney W. Grimes 585d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 586df8bae1dSRodney W. Grimes struct mincore_args { 587df8bae1dSRodney W. Grimes caddr_t addr; 5889154ee6aSPeter Wemm size_t len; 589df8bae1dSRodney W. Grimes char *vec; 590df8bae1dSRodney W. Grimes }; 591d2d3e875SBruce Evans #endif 5920d94caffSDavid Greenman 593df8bae1dSRodney W. Grimes /* ARGSUSED */ 594df8bae1dSRodney W. Grimes int 595df8bae1dSRodney W. Grimes mincore(p, uap, retval) 596df8bae1dSRodney W. Grimes struct proc *p; 597df8bae1dSRodney W. Grimes struct mincore_args *uap; 598df8bae1dSRodney W. Grimes int *retval; 599df8bae1dSRodney W. Grimes { 600867a482dSJohn Dyson vm_offset_t addr, first_addr; 601867a482dSJohn Dyson vm_offset_t end, cend; 602867a482dSJohn Dyson pmap_t pmap; 603867a482dSJohn Dyson vm_map_t map; 60402c04a2fSJohn Dyson char *vec; 605867a482dSJohn Dyson int error; 606867a482dSJohn Dyson int vecindex, lastvecindex; 607867a482dSJohn Dyson register vm_map_entry_t current; 608867a482dSJohn Dyson vm_map_entry_t entry; 609867a482dSJohn Dyson int mincoreinfo; 610df8bae1dSRodney W. Grimes 611867a482dSJohn Dyson /* 612867a482dSJohn Dyson * Make sure that the addresses presented are valid for user 613867a482dSJohn Dyson * mode. 614867a482dSJohn Dyson */ 615867a482dSJohn Dyson first_addr = addr = trunc_page((vm_offset_t) uap->addr); 6169154ee6aSPeter Wemm end = addr + (vm_size_t)round_page(uap->len); 61702c04a2fSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS) 61802c04a2fSJohn Dyson return (EINVAL); 61902c04a2fSJohn Dyson if (end < addr) 62002c04a2fSJohn Dyson return (EINVAL); 62102c04a2fSJohn Dyson 622867a482dSJohn Dyson /* 623867a482dSJohn Dyson * Address of byte vector 624867a482dSJohn Dyson */ 62502c04a2fSJohn Dyson vec = uap->vec; 626867a482dSJohn Dyson 627867a482dSJohn Dyson map = &p->p_vmspace->vm_map; 628867a482dSJohn Dyson pmap = &p->p_vmspace->vm_pmap; 629867a482dSJohn Dyson 630867a482dSJohn Dyson vm_map_lock(map); 631867a482dSJohn Dyson 632867a482dSJohn Dyson /* 633867a482dSJohn Dyson * Not needed here 634867a482dSJohn Dyson */ 635867a482dSJohn Dyson #if 0 636867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, addr, end); 637867a482dSJohn Dyson #endif 638867a482dSJohn Dyson 639867a482dSJohn Dyson if (!vm_map_lookup_entry(map, addr, &entry)) 640867a482dSJohn Dyson entry = entry->next; 641867a482dSJohn Dyson 642867a482dSJohn Dyson /* 643867a482dSJohn Dyson * Do this on a map entry basis so that if the pages are not 644867a482dSJohn Dyson * in the current processes address space, we can easily look 645867a482dSJohn Dyson * up the pages elsewhere. 646867a482dSJohn Dyson */ 647867a482dSJohn Dyson lastvecindex = -1; 648867a482dSJohn Dyson for(current = entry; 649867a482dSJohn Dyson (current != &map->header) && (current->start < end); 650867a482dSJohn Dyson current = current->next) { 651867a482dSJohn Dyson 652867a482dSJohn Dyson /* 653867a482dSJohn Dyson * ignore submaps (for now) or null objects 654867a482dSJohn Dyson */ 655867a482dSJohn Dyson if (current->is_a_map || current->is_sub_map || 656867a482dSJohn Dyson current->object.vm_object == NULL) 657867a482dSJohn Dyson continue; 658867a482dSJohn Dyson 659867a482dSJohn Dyson /* 660867a482dSJohn Dyson * limit this scan to the current map entry and the 661867a482dSJohn Dyson * limits for the mincore call 662867a482dSJohn Dyson */ 663867a482dSJohn Dyson if (addr < current->start) 664867a482dSJohn Dyson addr = current->start; 665867a482dSJohn Dyson cend = current->end; 666867a482dSJohn Dyson if (cend > end) 667867a482dSJohn Dyson cend = end; 668867a482dSJohn Dyson 669867a482dSJohn Dyson /* 670867a482dSJohn Dyson * scan this entry one page at a time 671867a482dSJohn Dyson */ 672867a482dSJohn Dyson while(addr < cend) { 673867a482dSJohn Dyson /* 674867a482dSJohn Dyson * Check pmap first, it is likely faster, also 675867a482dSJohn Dyson * it can provide info as to whether we are the 676867a482dSJohn Dyson * one referencing or modifying the page. 677867a482dSJohn Dyson */ 678867a482dSJohn Dyson mincoreinfo = pmap_mincore(pmap, addr); 679867a482dSJohn Dyson if (!mincoreinfo) { 680867a482dSJohn Dyson vm_pindex_t pindex; 681867a482dSJohn Dyson vm_ooffset_t offset; 682867a482dSJohn Dyson vm_page_t m; 683867a482dSJohn Dyson /* 684867a482dSJohn Dyson * calculate the page index into the object 685867a482dSJohn Dyson */ 686867a482dSJohn Dyson offset = current->offset + (addr - current->start); 687867a482dSJohn Dyson pindex = OFF_TO_IDX(offset); 688867a482dSJohn Dyson m = vm_page_lookup(current->object.vm_object, 689867a482dSJohn Dyson pindex); 690867a482dSJohn Dyson /* 691867a482dSJohn Dyson * if the page is resident, then gather information about 692867a482dSJohn Dyson * it. 693867a482dSJohn Dyson */ 694867a482dSJohn Dyson if (m) { 695867a482dSJohn Dyson mincoreinfo = MINCORE_INCORE; 696867a482dSJohn Dyson if (m->dirty || 69767bf6868SJohn Dyson pmap_is_modified(VM_PAGE_TO_PHYS(m))) 698867a482dSJohn Dyson mincoreinfo |= MINCORE_MODIFIED_OTHER; 699867a482dSJohn Dyson if ((m->flags & PG_REFERENCED) || 70067bf6868SJohn Dyson pmap_is_referenced(VM_PAGE_TO_PHYS(m))) 701867a482dSJohn Dyson mincoreinfo |= MINCORE_REFERENCED_OTHER; 70202c04a2fSJohn Dyson } 703867a482dSJohn Dyson } 704867a482dSJohn Dyson 705867a482dSJohn Dyson /* 706867a482dSJohn Dyson * calculate index into user supplied byte vector 707867a482dSJohn Dyson */ 708867a482dSJohn Dyson vecindex = OFF_TO_IDX(addr - first_addr); 709867a482dSJohn Dyson 710867a482dSJohn Dyson /* 711867a482dSJohn Dyson * If we have skipped map entries, we need to make sure that 712867a482dSJohn Dyson * the byte vector is zeroed for those skipped entries. 713867a482dSJohn Dyson */ 714867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 715867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 716867a482dSJohn Dyson if (error) { 717867a482dSJohn Dyson vm_map_unlock(map); 718867a482dSJohn Dyson return (EFAULT); 719867a482dSJohn Dyson } 720867a482dSJohn Dyson ++lastvecindex; 721867a482dSJohn Dyson } 722867a482dSJohn Dyson 723867a482dSJohn Dyson /* 724867a482dSJohn Dyson * Pass the page information to the user 725867a482dSJohn Dyson */ 726867a482dSJohn Dyson error = subyte( vec + vecindex, mincoreinfo); 727867a482dSJohn Dyson if (error) { 728867a482dSJohn Dyson vm_map_unlock(map); 729867a482dSJohn Dyson return (EFAULT); 730867a482dSJohn Dyson } 731867a482dSJohn Dyson lastvecindex = vecindex; 73202c04a2fSJohn Dyson addr += PAGE_SIZE; 73302c04a2fSJohn Dyson } 734867a482dSJohn Dyson } 735867a482dSJohn Dyson 736867a482dSJohn Dyson /* 737867a482dSJohn Dyson * Zero the last entries in the byte vector. 738867a482dSJohn Dyson */ 739867a482dSJohn Dyson vecindex = OFF_TO_IDX(end - first_addr); 740867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 741867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 742867a482dSJohn Dyson if (error) { 743867a482dSJohn Dyson vm_map_unlock(map); 744867a482dSJohn Dyson return (EFAULT); 745867a482dSJohn Dyson } 746867a482dSJohn Dyson ++lastvecindex; 747867a482dSJohn Dyson } 748867a482dSJohn Dyson 749867a482dSJohn Dyson vm_map_unlock(map); 75002c04a2fSJohn Dyson return (0); 751df8bae1dSRodney W. Grimes } 752df8bae1dSRodney W. Grimes 753d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 754df8bae1dSRodney W. Grimes struct mlock_args { 755df8bae1dSRodney W. Grimes caddr_t addr; 756df8bae1dSRodney W. Grimes size_t len; 757df8bae1dSRodney W. Grimes }; 758d2d3e875SBruce Evans #endif 759df8bae1dSRodney W. Grimes int 760df8bae1dSRodney W. Grimes mlock(p, uap, retval) 761df8bae1dSRodney W. Grimes struct proc *p; 762df8bae1dSRodney W. Grimes struct mlock_args *uap; 763df8bae1dSRodney W. Grimes int *retval; 764df8bae1dSRodney W. Grimes { 765df8bae1dSRodney W. Grimes vm_offset_t addr; 766dabee6feSPeter Wemm vm_size_t size, pageoff; 767df8bae1dSRodney W. Grimes int error; 768df8bae1dSRodney W. Grimes 769df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 7709154ee6aSPeter Wemm size = uap->len; 7719154ee6aSPeter Wemm 772dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 773dabee6feSPeter Wemm addr -= pageoff; 774dabee6feSPeter Wemm size += pageoff; 775dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 776dabee6feSPeter Wemm 777dabee6feSPeter Wemm /* disable wrap around */ 7789154ee6aSPeter Wemm if (addr + size < addr) 779df8bae1dSRodney W. Grimes return (EINVAL); 780dabee6feSPeter Wemm 781df8bae1dSRodney W. Grimes if (atop(size) + cnt.v_wire_count > vm_page_max_wired) 782df8bae1dSRodney W. Grimes return (EAGAIN); 7839154ee6aSPeter Wemm 784df8bae1dSRodney W. Grimes #ifdef pmap_wired_count 785df8bae1dSRodney W. Grimes if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 786df8bae1dSRodney W. Grimes p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 787df8bae1dSRodney W. Grimes return (EAGAIN); 788df8bae1dSRodney W. Grimes #else 78905f0fdd2SPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 79005f0fdd2SPoul-Henning Kamp if (error) 791df8bae1dSRodney W. Grimes return (error); 792df8bae1dSRodney W. Grimes #endif 793df8bae1dSRodney W. Grimes 7947aaaa4fdSJohn Dyson error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE); 795df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 796df8bae1dSRodney W. Grimes } 797df8bae1dSRodney W. Grimes 798d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 799df8bae1dSRodney W. Grimes struct munlock_args { 800df8bae1dSRodney W. Grimes caddr_t addr; 801df8bae1dSRodney W. Grimes size_t len; 802df8bae1dSRodney W. Grimes }; 803d2d3e875SBruce Evans #endif 804df8bae1dSRodney W. Grimes int 805df8bae1dSRodney W. Grimes munlock(p, uap, retval) 806df8bae1dSRodney W. Grimes struct proc *p; 807df8bae1dSRodney W. Grimes struct munlock_args *uap; 808df8bae1dSRodney W. Grimes int *retval; 809df8bae1dSRodney W. Grimes { 810df8bae1dSRodney W. Grimes vm_offset_t addr; 811dabee6feSPeter Wemm vm_size_t size, pageoff; 812df8bae1dSRodney W. Grimes int error; 813df8bae1dSRodney W. Grimes 814df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 8159154ee6aSPeter Wemm size = uap->len; 8169154ee6aSPeter Wemm 817dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 818dabee6feSPeter Wemm addr -= pageoff; 819dabee6feSPeter Wemm size += pageoff; 820dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 821dabee6feSPeter Wemm 822dabee6feSPeter Wemm /* disable wrap around */ 8239154ee6aSPeter Wemm if (addr + size < addr) 824df8bae1dSRodney W. Grimes return (EINVAL); 825dabee6feSPeter Wemm 826df8bae1dSRodney W. Grimes #ifndef pmap_wired_count 82705f0fdd2SPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 82805f0fdd2SPoul-Henning Kamp if (error) 829df8bae1dSRodney W. Grimes return (error); 830df8bae1dSRodney W. Grimes #endif 831df8bae1dSRodney W. Grimes 8327aaaa4fdSJohn Dyson error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE); 833df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 834df8bae1dSRodney W. Grimes } 835df8bae1dSRodney W. Grimes 836df8bae1dSRodney W. Grimes /* 837df8bae1dSRodney W. Grimes * Internal version of mmap. 838df8bae1dSRodney W. Grimes * Currently used by mmap, exec, and sys5 shared memory. 839df8bae1dSRodney W. Grimes * Handle is either a vnode pointer or NULL for MAP_ANON. 840df8bae1dSRodney W. Grimes */ 841df8bae1dSRodney W. Grimes int 842df8bae1dSRodney W. Grimes vm_mmap(map, addr, size, prot, maxprot, flags, handle, foff) 843df8bae1dSRodney W. Grimes register vm_map_t map; 844df8bae1dSRodney W. Grimes register vm_offset_t *addr; 845df8bae1dSRodney W. Grimes register vm_size_t size; 846df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 847df8bae1dSRodney W. Grimes register int flags; 848df8bae1dSRodney W. Grimes caddr_t handle; /* XXX should be vp */ 849a316d390SJohn Dyson vm_ooffset_t foff; 850df8bae1dSRodney W. Grimes { 851df8bae1dSRodney W. Grimes boolean_t fitit; 852fcae040bSJohn Dyson vm_object_t object; 853df8bae1dSRodney W. Grimes struct vnode *vp = NULL; 85424a1cce3SDavid Greenman objtype_t type; 855df8bae1dSRodney W. Grimes int rv = KERN_SUCCESS; 856bd7e5f99SJohn Dyson vm_ooffset_t objsize; 857bd7e5f99SJohn Dyson int docow; 85806cb7259SDavid Greenman struct proc *p = curproc; 859df8bae1dSRodney W. Grimes 860df8bae1dSRodney W. Grimes if (size == 0) 861df8bae1dSRodney W. Grimes return (0); 862df8bae1dSRodney W. Grimes 86306cb7259SDavid Greenman objsize = size = round_page(size); 864df8bae1dSRodney W. Grimes 865df8bae1dSRodney W. Grimes /* 866bc9ad247SDavid Greenman * We currently can only deal with page aligned file offsets. 867bc9ad247SDavid Greenman * The check is here rather than in the syscall because the 868bc9ad247SDavid Greenman * kernel calls this function internally for other mmaping 869bc9ad247SDavid Greenman * operations (such as in exec) and non-aligned offsets will 870bc9ad247SDavid Greenman * cause pmap inconsistencies...so we want to be sure to 871bc9ad247SDavid Greenman * disallow this in all cases. 872bc9ad247SDavid Greenman */ 873bc9ad247SDavid Greenman if (foff & PAGE_MASK) 874bc9ad247SDavid Greenman return (EINVAL); 875bc9ad247SDavid Greenman 87606cb7259SDavid Greenman if ((flags & MAP_FIXED) == 0) { 87706cb7259SDavid Greenman fitit = TRUE; 87806cb7259SDavid Greenman *addr = round_page(*addr); 87906cb7259SDavid Greenman } else { 88006cb7259SDavid Greenman if (*addr != trunc_page(*addr)) 88106cb7259SDavid Greenman return (EINVAL); 88206cb7259SDavid Greenman fitit = FALSE; 88306cb7259SDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 88406cb7259SDavid Greenman } 88506cb7259SDavid Greenman 886bc9ad247SDavid Greenman /* 88724a1cce3SDavid Greenman * Lookup/allocate object. 888df8bae1dSRodney W. Grimes */ 8895f55e841SDavid Greenman if (flags & MAP_ANON) { 890851c12ffSJohn Dyson type = OBJT_DEFAULT; 8915f55e841SDavid Greenman /* 8925f55e841SDavid Greenman * Unnamed anonymous regions always start at 0. 8935f55e841SDavid Greenman */ 89467bf6868SJohn Dyson if (handle == 0) 8955f55e841SDavid Greenman foff = 0; 8965f55e841SDavid Greenman } else { 897df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 898df8bae1dSRodney W. Grimes if (vp->v_type == VCHR) { 89924a1cce3SDavid Greenman type = OBJT_DEVICE; 900df8bae1dSRodney W. Grimes handle = (caddr_t) vp->v_rdev; 90106cb7259SDavid Greenman } else { 90206cb7259SDavid Greenman struct vattr vat; 90306cb7259SDavid Greenman int error; 90406cb7259SDavid Greenman 90506cb7259SDavid Greenman error = VOP_GETATTR(vp, &vat, p->p_ucred, p); 90606cb7259SDavid Greenman if (error) 90706cb7259SDavid Greenman return (error); 908bd7e5f99SJohn Dyson objsize = round_page(vat.va_size); 90924a1cce3SDavid Greenman type = OBJT_VNODE; 910df8bae1dSRodney W. Grimes } 91106cb7259SDavid Greenman } 91294328e90SJohn Dyson 91394328e90SJohn Dyson if (handle == NULL) { 91494328e90SJohn Dyson object = NULL; 91594328e90SJohn Dyson } else { 91667bf6868SJohn Dyson object = vm_pager_allocate(type, handle, OFF_TO_IDX(objsize), prot, foff); 91724a1cce3SDavid Greenman if (object == NULL) 91824a1cce3SDavid Greenman return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 91994328e90SJohn Dyson } 920df8bae1dSRodney W. Grimes 9215850152dSJohn Dyson /* 9228f2ec877SDavid Greenman * Force device mappings to be shared. 9235850152dSJohn Dyson */ 9248f2ec877SDavid Greenman if (type == OBJT_DEVICE) { 9258f2ec877SDavid Greenman flags &= ~(MAP_PRIVATE|MAP_COPY); 9265850152dSJohn Dyson flags |= MAP_SHARED; 9278f2ec877SDavid Greenman } 9285850152dSJohn Dyson 929bd7e5f99SJohn Dyson docow = 0; 9305850152dSJohn Dyson if ((flags & (MAP_ANON|MAP_SHARED)) == 0) { 931fcae040bSJohn Dyson docow = MAP_COPY_ON_WRITE | MAP_COPY_NEEDED; 932bd7e5f99SJohn Dyson } 9335850152dSJohn Dyson 934d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 935d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 936d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 937d0aea04fSJohn Dyson 938d0aea04fSJohn Dyson if (maxprot & VM_PROT_READ) 939d0aea04fSJohn Dyson maxprot |= VM_PROT_EXECUTE; 940d0aea04fSJohn Dyson #endif 941d0aea04fSJohn Dyson 942bd7e5f99SJohn Dyson rv = vm_map_find(map, object, foff, addr, size, fitit, 943bd7e5f99SJohn Dyson prot, maxprot, docow); 944bd7e5f99SJohn Dyson 94567bf6868SJohn Dyson 946df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 9477fb0c17eSDavid Greenman /* 94824a1cce3SDavid Greenman * Lose the object reference. Will destroy the 94924a1cce3SDavid Greenman * object if it's an unnamed anonymous mapping 95024a1cce3SDavid Greenman * or named anonymous without other references. 9517fb0c17eSDavid Greenman */ 952df8bae1dSRodney W. Grimes vm_object_deallocate(object); 953df8bae1dSRodney W. Grimes goto out; 954df8bae1dSRodney W. Grimes } 955e17bed12SJohn Dyson 956df8bae1dSRodney W. Grimes /* 9577fb0c17eSDavid Greenman * "Pre-fault" resident pages. 9587fb0c17eSDavid Greenman */ 95994328e90SJohn Dyson if ((type == OBJT_VNODE) && (map->pmap != NULL) && (object != NULL)) { 960a316d390SJohn Dyson pmap_object_init_pt(map->pmap, *addr, 961867a482dSJohn Dyson object, (vm_pindex_t) OFF_TO_IDX(foff), size, 1); 962df8bae1dSRodney W. Grimes } 9637fb0c17eSDavid Greenman 964df8bae1dSRodney W. Grimes /* 965df8bae1dSRodney W. Grimes * Shared memory is also shared with children. 966df8bae1dSRodney W. Grimes */ 9675850152dSJohn Dyson if (flags & (MAP_SHARED|MAP_INHERIT)) { 968df8bae1dSRodney W. Grimes rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 969df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 9707fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 971df8bae1dSRodney W. Grimes goto out; 972df8bae1dSRodney W. Grimes } 973df8bae1dSRodney W. Grimes } 974df8bae1dSRodney W. Grimes out: 975df8bae1dSRodney W. Grimes switch (rv) { 976df8bae1dSRodney W. Grimes case KERN_SUCCESS: 977df8bae1dSRodney W. Grimes return (0); 978df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 979df8bae1dSRodney W. Grimes case KERN_NO_SPACE: 980df8bae1dSRodney W. Grimes return (ENOMEM); 981df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 982df8bae1dSRodney W. Grimes return (EACCES); 983df8bae1dSRodney W. Grimes default: 984df8bae1dSRodney W. Grimes return (EINVAL); 985df8bae1dSRodney W. Grimes } 986df8bae1dSRodney W. Grimes } 987