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 4124a1cce3SDavid Greenman * $Id: vm_mmap.c,v 1.25 1995/07/09 06:58:01 davidg 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> 50df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 51df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 52df8bae1dSRodney W. Grimes #include <sys/proc.h> 53df8bae1dSRodney W. Grimes #include <sys/vnode.h> 54df8bae1dSRodney W. Grimes #include <sys/file.h> 55df8bae1dSRodney W. Grimes #include <sys/mman.h> 56df8bae1dSRodney W. Grimes #include <sys/conf.h> 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes #include <miscfs/specfs/specdev.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes #include <vm/vm.h> 61df8bae1dSRodney W. Grimes #include <vm/vm_pager.h> 62b5e8ce9fSBruce Evans #include <vm/vm_pageout.h> 63df8bae1dSRodney W. Grimes #include <vm/vm_prot.h> 64df8bae1dSRodney W. Grimes 65f720dc2cSDavid Greenman void pmap_object_init_pt(); 66f720dc2cSDavid Greenman 67df8bae1dSRodney W. Grimes struct sbrk_args { 68df8bae1dSRodney W. Grimes int incr; 69df8bae1dSRodney W. Grimes }; 700d94caffSDavid Greenman 71df8bae1dSRodney W. Grimes /* ARGSUSED */ 72df8bae1dSRodney W. Grimes int 73df8bae1dSRodney W. Grimes sbrk(p, uap, retval) 74df8bae1dSRodney W. Grimes struct proc *p; 75df8bae1dSRodney W. Grimes struct sbrk_args *uap; 76df8bae1dSRodney W. Grimes int *retval; 77df8bae1dSRodney W. Grimes { 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes /* Not yet implemented */ 80df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 81df8bae1dSRodney W. Grimes } 82df8bae1dSRodney W. Grimes 83df8bae1dSRodney W. Grimes struct sstk_args { 84df8bae1dSRodney W. Grimes int incr; 85df8bae1dSRodney W. Grimes }; 860d94caffSDavid Greenman 87df8bae1dSRodney W. Grimes /* ARGSUSED */ 88df8bae1dSRodney W. Grimes int 89df8bae1dSRodney W. Grimes sstk(p, uap, retval) 90df8bae1dSRodney W. Grimes struct proc *p; 91df8bae1dSRodney W. Grimes struct sstk_args *uap; 92df8bae1dSRodney W. Grimes int *retval; 93df8bae1dSRodney W. Grimes { 94df8bae1dSRodney W. Grimes 95df8bae1dSRodney W. Grimes /* Not yet implemented */ 96df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 97df8bae1dSRodney W. Grimes } 98df8bae1dSRodney W. Grimes 99df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 100df8bae1dSRodney W. Grimes struct getpagesize_args { 101df8bae1dSRodney W. Grimes int dummy; 102df8bae1dSRodney W. Grimes }; 1030d94caffSDavid Greenman 104df8bae1dSRodney W. Grimes /* ARGSUSED */ 105df8bae1dSRodney W. Grimes int 106df8bae1dSRodney W. Grimes ogetpagesize(p, uap, retval) 107df8bae1dSRodney W. Grimes struct proc *p; 108df8bae1dSRodney W. Grimes struct getpagesize_args *uap; 109df8bae1dSRodney W. Grimes int *retval; 110df8bae1dSRodney W. Grimes { 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes *retval = PAGE_SIZE; 113df8bae1dSRodney W. Grimes return (0); 114df8bae1dSRodney W. Grimes } 115df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 116df8bae1dSRodney W. Grimes 117df8bae1dSRodney W. Grimes struct mmap_args { 118df8bae1dSRodney W. Grimes caddr_t addr; 119df8bae1dSRodney W. Grimes size_t len; 120df8bae1dSRodney W. Grimes int prot; 121df8bae1dSRodney W. Grimes int flags; 122df8bae1dSRodney W. Grimes int fd; 123df8bae1dSRodney W. Grimes long pad; 124df8bae1dSRodney W. Grimes off_t pos; 125df8bae1dSRodney W. Grimes }; 126df8bae1dSRodney W. Grimes 127df8bae1dSRodney W. Grimes int 128df8bae1dSRodney W. Grimes mmap(p, uap, retval) 129df8bae1dSRodney W. Grimes struct proc *p; 130df8bae1dSRodney W. Grimes register struct mmap_args *uap; 131df8bae1dSRodney W. Grimes int *retval; 132df8bae1dSRodney W. Grimes { 133df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 134df8bae1dSRodney W. Grimes register struct file *fp; 135df8bae1dSRodney W. Grimes struct vnode *vp; 136df8bae1dSRodney W. Grimes vm_offset_t addr; 137df8bae1dSRodney W. Grimes vm_size_t size; 138df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 139df8bae1dSRodney W. Grimes caddr_t handle; 140df8bae1dSRodney W. Grimes int flags, error; 141df8bae1dSRodney W. Grimes 142df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 143df8bae1dSRodney W. Grimes flags = uap->flags; 144df8bae1dSRodney W. Grimes /* 1450d94caffSDavid Greenman * Address (if FIXED) must be page aligned. Size is implicitly rounded 1460d94caffSDavid Greenman * to a page boundary. 147df8bae1dSRodney W. Grimes */ 148df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 149df8bae1dSRodney W. Grimes if (((flags & MAP_FIXED) && (addr & PAGE_MASK)) || 150df8bae1dSRodney W. Grimes (ssize_t) uap->len < 0 || ((flags & MAP_ANON) && uap->fd != -1)) 151df8bae1dSRodney W. Grimes return (EINVAL); 152df8bae1dSRodney W. Grimes size = (vm_size_t) round_page(uap->len); 153df8bae1dSRodney W. Grimes /* 1540d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 1550d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 156df8bae1dSRodney W. Grimes */ 157df8bae1dSRodney W. Grimes if (flags & MAP_FIXED) { 158bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 159df8bae1dSRodney W. Grimes return (EINVAL); 16026f9a767SRodney W. Grimes #ifndef i386 161df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 162df8bae1dSRodney W. Grimes return (EINVAL); 16326f9a767SRodney W. Grimes #endif 164bbc0ec52SDavid Greenman if (addr + size < addr) 165df8bae1dSRodney W. Grimes return (EINVAL); 166df8bae1dSRodney W. Grimes } 167df8bae1dSRodney W. Grimes /* 1680d94caffSDavid Greenman * XXX if no hint provided for a non-fixed mapping place it after the 1690d94caffSDavid Greenman * end of the largest possible heap. 170df8bae1dSRodney W. Grimes * 1710d94caffSDavid Greenman * There should really be a pmap call to determine a reasonable location. 172df8bae1dSRodney W. Grimes */ 173df8bae1dSRodney W. Grimes if (addr == 0 && (flags & MAP_FIXED) == 0) 174df8bae1dSRodney W. Grimes addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ); 175df8bae1dSRodney W. Grimes if (flags & MAP_ANON) { 176df8bae1dSRodney W. Grimes /* 177df8bae1dSRodney W. Grimes * Mapping blank space is trivial. 178df8bae1dSRodney W. Grimes */ 179df8bae1dSRodney W. Grimes handle = NULL; 180df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 181df8bae1dSRodney W. Grimes } else { 182df8bae1dSRodney W. Grimes /* 1830d94caffSDavid Greenman * Mapping file, get fp for validation. Obtain vnode and make 1840d94caffSDavid Greenman * sure it is of appropriate type. 185df8bae1dSRodney W. Grimes */ 186df8bae1dSRodney W. Grimes if (((unsigned) uap->fd) >= fdp->fd_nfiles || 187df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL) 188df8bae1dSRodney W. Grimes return (EBADF); 189df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_VNODE) 190df8bae1dSRodney W. Grimes return (EINVAL); 191df8bae1dSRodney W. Grimes vp = (struct vnode *) fp->f_data; 192df8bae1dSRodney W. Grimes if (vp->v_type != VREG && vp->v_type != VCHR) 193df8bae1dSRodney W. Grimes return (EINVAL); 194df8bae1dSRodney W. Grimes /* 1950d94caffSDavid Greenman * XXX hack to handle use of /dev/zero to map anon memory (ala 1960d94caffSDavid Greenman * SunOS). 197df8bae1dSRodney W. Grimes */ 198df8bae1dSRodney W. Grimes if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) { 199df8bae1dSRodney W. Grimes handle = NULL; 200df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 201df8bae1dSRodney W. Grimes flags |= MAP_ANON; 202df8bae1dSRodney W. Grimes } else { 203df8bae1dSRodney W. Grimes /* 204df8bae1dSRodney W. Grimes * Ensure that file and memory protections are 205df8bae1dSRodney W. Grimes * compatible. Note that we only worry about 206df8bae1dSRodney W. Grimes * writability if mapping is shared; in this case, 207df8bae1dSRodney W. Grimes * current and max prot are dictated by the open file. 208df8bae1dSRodney W. Grimes * XXX use the vnode instead? Problem is: what 2090d94caffSDavid Greenman * credentials do we use for determination? What if 2100d94caffSDavid Greenman * proc does a setuid? 211df8bae1dSRodney W. Grimes */ 212df8bae1dSRodney W. Grimes maxprot = VM_PROT_EXECUTE; /* ??? */ 213df8bae1dSRodney W. Grimes if (fp->f_flag & FREAD) 214df8bae1dSRodney W. Grimes maxprot |= VM_PROT_READ; 215df8bae1dSRodney W. Grimes else if (prot & PROT_READ) 216df8bae1dSRodney W. Grimes return (EACCES); 217df8bae1dSRodney W. Grimes if (flags & MAP_SHARED) { 218df8bae1dSRodney W. Grimes if (fp->f_flag & FWRITE) 219df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 220df8bae1dSRodney W. Grimes else if (prot & PROT_WRITE) 221df8bae1dSRodney W. Grimes return (EACCES); 222df8bae1dSRodney W. Grimes } else 223df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 224df8bae1dSRodney W. Grimes handle = (caddr_t) vp; 225df8bae1dSRodney W. Grimes } 226df8bae1dSRodney W. Grimes } 227df8bae1dSRodney W. Grimes error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot, 228df8bae1dSRodney W. Grimes flags, handle, (vm_offset_t) uap->pos); 229df8bae1dSRodney W. Grimes if (error == 0) 230df8bae1dSRodney W. Grimes *retval = (int) addr; 231df8bae1dSRodney W. Grimes return (error); 232df8bae1dSRodney W. Grimes } 233df8bae1dSRodney W. Grimes 23405f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43 23505f0fdd2SPoul-Henning Kamp struct ommap_args { 23605f0fdd2SPoul-Henning Kamp caddr_t addr; 23705f0fdd2SPoul-Henning Kamp int len; 23805f0fdd2SPoul-Henning Kamp int prot; 23905f0fdd2SPoul-Henning Kamp int flags; 24005f0fdd2SPoul-Henning Kamp int fd; 24105f0fdd2SPoul-Henning Kamp long pos; 24205f0fdd2SPoul-Henning Kamp }; 24305f0fdd2SPoul-Henning Kamp int 24405f0fdd2SPoul-Henning Kamp ommap(p, uap, retval) 24505f0fdd2SPoul-Henning Kamp struct proc *p; 24605f0fdd2SPoul-Henning Kamp register struct ommap_args *uap; 24705f0fdd2SPoul-Henning Kamp int *retval; 24805f0fdd2SPoul-Henning Kamp { 24905f0fdd2SPoul-Henning Kamp struct mmap_args nargs; 25005f0fdd2SPoul-Henning Kamp static const char cvtbsdprot[8] = { 25105f0fdd2SPoul-Henning Kamp 0, 25205f0fdd2SPoul-Henning Kamp PROT_EXEC, 25305f0fdd2SPoul-Henning Kamp PROT_WRITE, 25405f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE, 25505f0fdd2SPoul-Henning Kamp PROT_READ, 25605f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_READ, 25705f0fdd2SPoul-Henning Kamp PROT_WRITE | PROT_READ, 25805f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE | PROT_READ, 25905f0fdd2SPoul-Henning Kamp }; 2600d94caffSDavid Greenman 26105f0fdd2SPoul-Henning Kamp #define OMAP_ANON 0x0002 26205f0fdd2SPoul-Henning Kamp #define OMAP_COPY 0x0020 26305f0fdd2SPoul-Henning Kamp #define OMAP_SHARED 0x0010 26405f0fdd2SPoul-Henning Kamp #define OMAP_FIXED 0x0100 26505f0fdd2SPoul-Henning Kamp #define OMAP_INHERIT 0x0800 26605f0fdd2SPoul-Henning Kamp 26705f0fdd2SPoul-Henning Kamp nargs.addr = uap->addr; 26805f0fdd2SPoul-Henning Kamp nargs.len = uap->len; 26905f0fdd2SPoul-Henning Kamp nargs.prot = cvtbsdprot[uap->prot & 0x7]; 27005f0fdd2SPoul-Henning Kamp nargs.flags = 0; 27105f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_ANON) 27205f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_ANON; 27305f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_COPY) 27405f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_COPY; 27505f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_SHARED) 27605f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_SHARED; 27705f0fdd2SPoul-Henning Kamp else 27805f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_PRIVATE; 27905f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_FIXED) 28005f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_FIXED; 28105f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_INHERIT) 28205f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_INHERIT; 28305f0fdd2SPoul-Henning Kamp nargs.fd = uap->fd; 28405f0fdd2SPoul-Henning Kamp nargs.pos = uap->pos; 28505f0fdd2SPoul-Henning Kamp return (mmap(p, &nargs, retval)); 28605f0fdd2SPoul-Henning Kamp } 28705f0fdd2SPoul-Henning Kamp #endif /* COMPAT_43 */ 28805f0fdd2SPoul-Henning Kamp 28905f0fdd2SPoul-Henning Kamp 290df8bae1dSRodney W. Grimes struct msync_args { 291df8bae1dSRodney W. Grimes caddr_t addr; 292df8bae1dSRodney W. Grimes int len; 293e6c6af11SDavid Greenman int flags; 294df8bae1dSRodney W. Grimes }; 295df8bae1dSRodney W. Grimes int 296df8bae1dSRodney W. Grimes msync(p, uap, retval) 297df8bae1dSRodney W. Grimes struct proc *p; 298df8bae1dSRodney W. Grimes struct msync_args *uap; 299df8bae1dSRodney W. Grimes int *retval; 300df8bae1dSRodney W. Grimes { 301df8bae1dSRodney W. Grimes vm_offset_t addr; 302df8bae1dSRodney W. Grimes vm_size_t size; 303e6c6af11SDavid Greenman int flags; 304df8bae1dSRodney W. Grimes vm_map_t map; 305df8bae1dSRodney W. Grimes int rv; 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes map = &p->p_vmspace->vm_map; 308df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 309df8bae1dSRodney W. Grimes size = (vm_size_t) uap->len; 310e6c6af11SDavid Greenman flags = uap->flags; 311e6c6af11SDavid Greenman 3121e62bc63SDavid Greenman if (((int) addr & PAGE_MASK) || addr + size < addr || 3131e62bc63SDavid Greenman (flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 3141e62bc63SDavid Greenman return (EINVAL); 3151e62bc63SDavid Greenman 316df8bae1dSRodney W. Grimes /* 317df8bae1dSRodney W. Grimes * XXX Gak! If size is zero we are supposed to sync "all modified 3180d94caffSDavid Greenman * pages with the region containing addr". Unfortunately, we don't 3190d94caffSDavid Greenman * really keep track of individual mmaps so we approximate by flushing 3200d94caffSDavid Greenman * the range of the map entry containing addr. This can be incorrect 3210d94caffSDavid Greenman * if the region splits or is coalesced with a neighbor. 322df8bae1dSRodney W. Grimes */ 323df8bae1dSRodney W. Grimes if (size == 0) { 324df8bae1dSRodney W. Grimes vm_map_entry_t entry; 325df8bae1dSRodney W. Grimes 326df8bae1dSRodney W. Grimes vm_map_lock_read(map); 327df8bae1dSRodney W. Grimes rv = vm_map_lookup_entry(map, addr, &entry); 328df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 329fbcfcdf7SDavid Greenman if (rv == FALSE) 330df8bae1dSRodney W. Grimes return (EINVAL); 331df8bae1dSRodney W. Grimes addr = entry->start; 332df8bae1dSRodney W. Grimes size = entry->end - entry->start; 333df8bae1dSRodney W. Grimes } 334e6c6af11SDavid Greenman 335df8bae1dSRodney W. Grimes /* 336df8bae1dSRodney W. Grimes * Clean the pages and interpret the return value. 337df8bae1dSRodney W. Grimes */ 3386c534ad8SDavid Greenman rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 339e6c6af11SDavid Greenman (flags & MS_INVALIDATE) != 0); 340e6c6af11SDavid Greenman 341df8bae1dSRodney W. Grimes switch (rv) { 342df8bae1dSRodney W. Grimes case KERN_SUCCESS: 343df8bae1dSRodney W. Grimes break; 344df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 345df8bae1dSRodney W. Grimes return (EINVAL); /* Sun returns ENOMEM? */ 346df8bae1dSRodney W. Grimes case KERN_FAILURE: 347df8bae1dSRodney W. Grimes return (EIO); 348df8bae1dSRodney W. Grimes default: 349df8bae1dSRodney W. Grimes return (EINVAL); 350df8bae1dSRodney W. Grimes } 351e6c6af11SDavid Greenman 352df8bae1dSRodney W. Grimes return (0); 353df8bae1dSRodney W. Grimes } 354df8bae1dSRodney W. Grimes 355df8bae1dSRodney W. Grimes struct munmap_args { 356df8bae1dSRodney W. Grimes caddr_t addr; 357df8bae1dSRodney W. Grimes int len; 358df8bae1dSRodney W. Grimes }; 359df8bae1dSRodney W. Grimes int 360df8bae1dSRodney W. Grimes munmap(p, uap, retval) 361df8bae1dSRodney W. Grimes register struct proc *p; 362df8bae1dSRodney W. Grimes register struct munmap_args *uap; 363df8bae1dSRodney W. Grimes int *retval; 364df8bae1dSRodney W. Grimes { 365df8bae1dSRodney W. Grimes vm_offset_t addr; 366df8bae1dSRodney W. Grimes vm_size_t size; 367df8bae1dSRodney W. Grimes vm_map_t map; 368df8bae1dSRodney W. Grimes 369df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 370df8bae1dSRodney W. Grimes if ((addr & PAGE_MASK) || uap->len < 0) 371df8bae1dSRodney W. Grimes return (EINVAL); 372df8bae1dSRodney W. Grimes size = (vm_size_t) round_page(uap->len); 373df8bae1dSRodney W. Grimes if (size == 0) 374df8bae1dSRodney W. Grimes return (0); 375df8bae1dSRodney W. Grimes /* 3760d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 3770d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 378df8bae1dSRodney W. Grimes */ 379bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 380df8bae1dSRodney W. Grimes return (EINVAL); 38126f9a767SRodney W. Grimes #ifndef i386 382df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 383df8bae1dSRodney W. Grimes return (EINVAL); 38426f9a767SRodney W. Grimes #endif 385bbc0ec52SDavid Greenman if (addr + size < addr) 386df8bae1dSRodney W. Grimes return (EINVAL); 387df8bae1dSRodney W. Grimes map = &p->p_vmspace->vm_map; 388df8bae1dSRodney W. Grimes /* 389df8bae1dSRodney W. Grimes * Make sure entire range is allocated. 390df8bae1dSRodney W. Grimes */ 391df8bae1dSRodney W. Grimes if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) 392df8bae1dSRodney W. Grimes return (EINVAL); 393df8bae1dSRodney W. Grimes /* returns nothing but KERN_SUCCESS anyway */ 394df8bae1dSRodney W. Grimes (void) vm_map_remove(map, addr, addr + size); 395df8bae1dSRodney W. Grimes return (0); 396df8bae1dSRodney W. Grimes } 397df8bae1dSRodney W. Grimes 398df8bae1dSRodney W. Grimes void 39990324b07SDavid Greenman munmapfd(p, fd) 40090324b07SDavid Greenman struct proc *p; 401df8bae1dSRodney W. Grimes int fd; 402df8bae1dSRodney W. Grimes { 403df8bae1dSRodney W. Grimes /* 404c4ed5a07SDavid Greenman * XXX should unmap any regions mapped to this file 405df8bae1dSRodney W. Grimes */ 40690324b07SDavid Greenman p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED; 407df8bae1dSRodney W. Grimes } 408df8bae1dSRodney W. Grimes 409df8bae1dSRodney W. Grimes struct mprotect_args { 410df8bae1dSRodney W. Grimes caddr_t addr; 411df8bae1dSRodney W. Grimes int len; 412df8bae1dSRodney W. Grimes int prot; 413df8bae1dSRodney W. Grimes }; 414df8bae1dSRodney W. Grimes int 415df8bae1dSRodney W. Grimes mprotect(p, uap, retval) 416df8bae1dSRodney W. Grimes struct proc *p; 417df8bae1dSRodney W. Grimes struct mprotect_args *uap; 418df8bae1dSRodney W. Grimes int *retval; 419df8bae1dSRodney W. Grimes { 420df8bae1dSRodney W. Grimes vm_offset_t addr; 421df8bae1dSRodney W. Grimes vm_size_t size; 422df8bae1dSRodney W. Grimes register vm_prot_t prot; 423df8bae1dSRodney W. Grimes 424df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 425df8bae1dSRodney W. Grimes if ((addr & PAGE_MASK) || uap->len < 0) 426df8bae1dSRodney W. Grimes return (EINVAL); 427df8bae1dSRodney W. Grimes size = (vm_size_t) uap->len; 428df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 429df8bae1dSRodney W. Grimes 430df8bae1dSRodney W. Grimes switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot, 431df8bae1dSRodney W. Grimes FALSE)) { 432df8bae1dSRodney W. Grimes case KERN_SUCCESS: 433df8bae1dSRodney W. Grimes return (0); 434df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 435df8bae1dSRodney W. Grimes return (EACCES); 436df8bae1dSRodney W. Grimes } 437df8bae1dSRodney W. Grimes return (EINVAL); 438df8bae1dSRodney W. Grimes } 439df8bae1dSRodney W. Grimes 440df8bae1dSRodney W. Grimes struct madvise_args { 441df8bae1dSRodney W. Grimes caddr_t addr; 442df8bae1dSRodney W. Grimes int len; 443df8bae1dSRodney W. Grimes int behav; 444df8bae1dSRodney W. Grimes }; 4450d94caffSDavid Greenman 446df8bae1dSRodney W. Grimes /* ARGSUSED */ 447df8bae1dSRodney W. Grimes int 448df8bae1dSRodney W. Grimes madvise(p, uap, retval) 449df8bae1dSRodney W. Grimes struct proc *p; 450df8bae1dSRodney W. Grimes struct madvise_args *uap; 451df8bae1dSRodney W. Grimes int *retval; 452df8bae1dSRodney W. Grimes { 453df8bae1dSRodney W. Grimes 454df8bae1dSRodney W. Grimes /* Not yet implemented */ 455df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 456df8bae1dSRodney W. Grimes } 457df8bae1dSRodney W. Grimes 458df8bae1dSRodney W. Grimes struct mincore_args { 459df8bae1dSRodney W. Grimes caddr_t addr; 460df8bae1dSRodney W. Grimes int len; 461df8bae1dSRodney W. Grimes char *vec; 462df8bae1dSRodney W. Grimes }; 4630d94caffSDavid Greenman 464df8bae1dSRodney W. Grimes /* ARGSUSED */ 465df8bae1dSRodney W. Grimes int 466df8bae1dSRodney W. Grimes mincore(p, uap, retval) 467df8bae1dSRodney W. Grimes struct proc *p; 468df8bae1dSRodney W. Grimes struct mincore_args *uap; 469df8bae1dSRodney W. Grimes int *retval; 470df8bae1dSRodney W. Grimes { 471df8bae1dSRodney W. Grimes 472df8bae1dSRodney W. Grimes /* Not yet implemented */ 473df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 474df8bae1dSRodney W. Grimes } 475df8bae1dSRodney W. Grimes 476df8bae1dSRodney W. Grimes struct mlock_args { 477df8bae1dSRodney W. Grimes caddr_t addr; 478df8bae1dSRodney W. Grimes size_t len; 479df8bae1dSRodney W. Grimes }; 480df8bae1dSRodney W. Grimes int 481df8bae1dSRodney W. Grimes mlock(p, uap, retval) 482df8bae1dSRodney W. Grimes struct proc *p; 483df8bae1dSRodney W. Grimes struct mlock_args *uap; 484df8bae1dSRodney W. Grimes int *retval; 485df8bae1dSRodney W. Grimes { 486df8bae1dSRodney W. Grimes vm_offset_t addr; 487df8bae1dSRodney W. Grimes vm_size_t size; 488df8bae1dSRodney W. Grimes int error; 489df8bae1dSRodney W. Grimes 490df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 491df8bae1dSRodney W. Grimes if ((addr & PAGE_MASK) || uap->addr + uap->len < uap->addr) 492df8bae1dSRodney W. Grimes return (EINVAL); 493df8bae1dSRodney W. Grimes size = round_page((vm_size_t) uap->len); 494df8bae1dSRodney W. Grimes if (atop(size) + cnt.v_wire_count > vm_page_max_wired) 495df8bae1dSRodney W. Grimes return (EAGAIN); 496df8bae1dSRodney W. Grimes #ifdef pmap_wired_count 497df8bae1dSRodney W. Grimes if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 498df8bae1dSRodney W. Grimes p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 499df8bae1dSRodney W. Grimes return (EAGAIN); 500df8bae1dSRodney W. Grimes #else 50105f0fdd2SPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 50205f0fdd2SPoul-Henning Kamp if (error) 503df8bae1dSRodney W. Grimes return (error); 504df8bae1dSRodney W. Grimes #endif 505df8bae1dSRodney W. Grimes 506df8bae1dSRodney W. Grimes error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE); 507df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 508df8bae1dSRodney W. Grimes } 509df8bae1dSRodney W. Grimes 510df8bae1dSRodney W. Grimes struct munlock_args { 511df8bae1dSRodney W. Grimes caddr_t addr; 512df8bae1dSRodney W. Grimes size_t len; 513df8bae1dSRodney W. Grimes }; 514df8bae1dSRodney W. Grimes int 515df8bae1dSRodney W. Grimes munlock(p, uap, retval) 516df8bae1dSRodney W. Grimes struct proc *p; 517df8bae1dSRodney W. Grimes struct munlock_args *uap; 518df8bae1dSRodney W. Grimes int *retval; 519df8bae1dSRodney W. Grimes { 520df8bae1dSRodney W. Grimes vm_offset_t addr; 521df8bae1dSRodney W. Grimes vm_size_t size; 522df8bae1dSRodney W. Grimes int error; 523df8bae1dSRodney W. Grimes 524df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 525df8bae1dSRodney W. Grimes if ((addr & PAGE_MASK) || uap->addr + uap->len < uap->addr) 526df8bae1dSRodney W. Grimes return (EINVAL); 527df8bae1dSRodney W. Grimes #ifndef pmap_wired_count 52805f0fdd2SPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 52905f0fdd2SPoul-Henning Kamp if (error) 530df8bae1dSRodney W. Grimes return (error); 531df8bae1dSRodney W. Grimes #endif 532df8bae1dSRodney W. Grimes size = round_page((vm_size_t) uap->len); 533df8bae1dSRodney W. Grimes 534df8bae1dSRodney W. Grimes error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE); 535df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 536df8bae1dSRodney W. Grimes } 537df8bae1dSRodney W. Grimes 538df8bae1dSRodney W. Grimes /* 539df8bae1dSRodney W. Grimes * Internal version of mmap. 540df8bae1dSRodney W. Grimes * Currently used by mmap, exec, and sys5 shared memory. 541df8bae1dSRodney W. Grimes * Handle is either a vnode pointer or NULL for MAP_ANON. 542df8bae1dSRodney W. Grimes */ 543df8bae1dSRodney W. Grimes int 544df8bae1dSRodney W. Grimes vm_mmap(map, addr, size, prot, maxprot, flags, handle, foff) 545df8bae1dSRodney W. Grimes register vm_map_t map; 546df8bae1dSRodney W. Grimes register vm_offset_t *addr; 547df8bae1dSRodney W. Grimes register vm_size_t size; 548df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 549df8bae1dSRodney W. Grimes register int flags; 550df8bae1dSRodney W. Grimes caddr_t handle; /* XXX should be vp */ 551df8bae1dSRodney W. Grimes vm_offset_t foff; 552df8bae1dSRodney W. Grimes { 553df8bae1dSRodney W. Grimes boolean_t fitit; 554df8bae1dSRodney W. Grimes vm_object_t object; 555df8bae1dSRodney W. Grimes struct vnode *vp = NULL; 55624a1cce3SDavid Greenman objtype_t type; 557df8bae1dSRodney W. Grimes int rv = KERN_SUCCESS; 55806cb7259SDavid Greenman vm_size_t objsize; 55906cb7259SDavid Greenman struct proc *p = curproc; 560df8bae1dSRodney W. Grimes 561df8bae1dSRodney W. Grimes if (size == 0) 562df8bae1dSRodney W. Grimes return (0); 563df8bae1dSRodney W. Grimes 56406cb7259SDavid Greenman objsize = size = round_page(size); 565df8bae1dSRodney W. Grimes 566df8bae1dSRodney W. Grimes /* 567bc9ad247SDavid Greenman * We currently can only deal with page aligned file offsets. 568bc9ad247SDavid Greenman * The check is here rather than in the syscall because the 569bc9ad247SDavid Greenman * kernel calls this function internally for other mmaping 570bc9ad247SDavid Greenman * operations (such as in exec) and non-aligned offsets will 571bc9ad247SDavid Greenman * cause pmap inconsistencies...so we want to be sure to 572bc9ad247SDavid Greenman * disallow this in all cases. 573bc9ad247SDavid Greenman */ 574bc9ad247SDavid Greenman if (foff & PAGE_MASK) 575bc9ad247SDavid Greenman return (EINVAL); 576bc9ad247SDavid Greenman 57706cb7259SDavid Greenman if ((flags & MAP_FIXED) == 0) { 57806cb7259SDavid Greenman fitit = TRUE; 57906cb7259SDavid Greenman *addr = round_page(*addr); 58006cb7259SDavid Greenman } else { 58106cb7259SDavid Greenman if (*addr != trunc_page(*addr)) 58206cb7259SDavid Greenman return (EINVAL); 58306cb7259SDavid Greenman fitit = FALSE; 58406cb7259SDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 58506cb7259SDavid Greenman } 58606cb7259SDavid Greenman 587bc9ad247SDavid Greenman /* 58824a1cce3SDavid Greenman * Lookup/allocate object. 589df8bae1dSRodney W. Grimes */ 5905f55e841SDavid Greenman if (flags & MAP_ANON) { 59124a1cce3SDavid Greenman type = OBJT_SWAP; 5925f55e841SDavid Greenman /* 5935f55e841SDavid Greenman * Unnamed anonymous regions always start at 0. 5945f55e841SDavid Greenman */ 5955f55e841SDavid Greenman if (handle == 0) 5965f55e841SDavid Greenman foff = 0; 5975f55e841SDavid Greenman } else { 598df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 599df8bae1dSRodney W. Grimes if (vp->v_type == VCHR) { 60024a1cce3SDavid Greenman type = OBJT_DEVICE; 601df8bae1dSRodney W. Grimes handle = (caddr_t) vp->v_rdev; 60206cb7259SDavid Greenman } else { 60306cb7259SDavid Greenman struct vattr vat; 60406cb7259SDavid Greenman int error; 60506cb7259SDavid Greenman 60606cb7259SDavid Greenman error = VOP_GETATTR(vp, &vat, p->p_ucred, p); 60706cb7259SDavid Greenman if (error) 60806cb7259SDavid Greenman return (error); 60906cb7259SDavid Greenman objsize = vat.va_size; 61024a1cce3SDavid Greenman type = OBJT_VNODE; 611df8bae1dSRodney W. Grimes } 61206cb7259SDavid Greenman } 61324a1cce3SDavid Greenman object = vm_pager_allocate(type, handle, objsize, prot, foff); 61424a1cce3SDavid Greenman if (object == NULL) 61524a1cce3SDavid Greenman return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 616df8bae1dSRodney W. Grimes 617df8bae1dSRodney W. Grimes /* 6187fb0c17eSDavid Greenman * Anonymous memory, shared file, or character special file. 619df8bae1dSRodney W. Grimes */ 62024a1cce3SDavid Greenman if ((flags & (MAP_ANON|MAP_SHARED)) || (type == OBJT_DEVICE)) { 6217fb0c17eSDavid Greenman rv = vm_map_find(map, object, foff, addr, size, fitit); 622df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 6237fb0c17eSDavid Greenman /* 62424a1cce3SDavid Greenman * Lose the object reference. Will destroy the 62524a1cce3SDavid Greenman * object if it's an unnamed anonymous mapping 62624a1cce3SDavid Greenman * or named anonymous without other references. 6277fb0c17eSDavid Greenman */ 628df8bae1dSRodney W. Grimes vm_object_deallocate(object); 629df8bae1dSRodney W. Grimes goto out; 630df8bae1dSRodney W. Grimes } 631df8bae1dSRodney W. Grimes } 632df8bae1dSRodney W. Grimes /* 63350ce2102SDavid Greenman * mmap a COW regular file 634df8bae1dSRodney W. Grimes */ 635df8bae1dSRodney W. Grimes else { 63650ce2102SDavid Greenman vm_map_entry_t entry; 63724a1cce3SDavid Greenman vm_object_t private_object; 63850ce2102SDavid Greenman 639f2da180fSDavid Greenman /* 640f2da180fSDavid Greenman * Create a new object and make the original object 641f2da180fSDavid Greenman * the backing object. NOTE: the object reference gained 642f2da180fSDavid Greenman * above is now changed into the reference held by 64324a1cce3SDavid Greenman * private_object. Since we don't map 'object', we want 644f2da180fSDavid Greenman * only this one reference. 645f2da180fSDavid Greenman */ 64624a1cce3SDavid Greenman private_object = vm_object_allocate(OBJT_DEFAULT, object->size); 64724a1cce3SDavid Greenman private_object->backing_object = object; 64824a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->shadow_head, 64924a1cce3SDavid Greenman private_object, shadow_list); 65050ce2102SDavid Greenman 65124a1cce3SDavid Greenman rv = vm_map_find(map, private_object, foff, addr, size, fitit); 65250ce2102SDavid Greenman if (rv != KERN_SUCCESS) { 65324a1cce3SDavid Greenman vm_object_deallocate(private_object); 65450ce2102SDavid Greenman goto out; 65550ce2102SDavid Greenman } 65650ce2102SDavid Greenman 65750ce2102SDavid Greenman if (!vm_map_lookup_entry(map, *addr, &entry)) { 658edf8a815SDavid Greenman panic("vm_mmap: missing map entry!!!"); 65950ce2102SDavid Greenman } 66050ce2102SDavid Greenman entry->copy_on_write = TRUE; 66150ce2102SDavid Greenman 66250ce2102SDavid Greenman /* 66350ce2102SDavid Greenman * set pages COW and protect for read access only 66450ce2102SDavid Greenman */ 66550ce2102SDavid Greenman vm_object_pmap_copy(object, foff, foff + size); 66650ce2102SDavid Greenman 667df8bae1dSRodney W. Grimes } 6687fb0c17eSDavid Greenman 6697fb0c17eSDavid Greenman /* 6707fb0c17eSDavid Greenman * "Pre-fault" resident pages. 6717fb0c17eSDavid Greenman */ 67224a1cce3SDavid Greenman if ((type == OBJT_VNODE) && (map->pmap != NULL)) { 6737fb0c17eSDavid Greenman pmap_object_init_pt(map->pmap, *addr, object, foff, size); 674df8bae1dSRodney W. Grimes } 6757fb0c17eSDavid Greenman 676df8bae1dSRodney W. Grimes /* 6770d94caffSDavid Greenman * Correct protection (default is VM_PROT_ALL). If maxprot is 6780d94caffSDavid Greenman * different than prot, we must set both explicitly. 679df8bae1dSRodney W. Grimes */ 680df8bae1dSRodney W. Grimes rv = KERN_SUCCESS; 681df8bae1dSRodney W. Grimes if (maxprot != VM_PROT_ALL) 682df8bae1dSRodney W. Grimes rv = vm_map_protect(map, *addr, *addr + size, maxprot, TRUE); 683df8bae1dSRodney W. Grimes if (rv == KERN_SUCCESS && prot != maxprot) 684df8bae1dSRodney W. Grimes rv = vm_map_protect(map, *addr, *addr + size, prot, FALSE); 685df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 6867fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 687df8bae1dSRodney W. Grimes goto out; 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes /* 690df8bae1dSRodney W. Grimes * Shared memory is also shared with children. 691df8bae1dSRodney W. Grimes */ 692df8bae1dSRodney W. Grimes if (flags & MAP_SHARED) { 693df8bae1dSRodney W. Grimes rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 694df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 6957fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 696df8bae1dSRodney W. Grimes goto out; 697df8bae1dSRodney W. Grimes } 698df8bae1dSRodney W. Grimes } 699df8bae1dSRodney W. Grimes out: 700df8bae1dSRodney W. Grimes switch (rv) { 701df8bae1dSRodney W. Grimes case KERN_SUCCESS: 702df8bae1dSRodney W. Grimes return (0); 703df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 704df8bae1dSRodney W. Grimes case KERN_NO_SPACE: 705df8bae1dSRodney W. Grimes return (ENOMEM); 706df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 707df8bae1dSRodney W. Grimes return (EACCES); 708df8bae1dSRodney W. Grimes default: 709df8bae1dSRodney W. Grimes return (EINVAL); 710df8bae1dSRodney W. Grimes } 711df8bae1dSRodney W. Grimes } 712