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 195929bcfaSPhilippe Charnier * 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 41c3aac50fSPeter Wemm * $FreeBSD$ 42df8bae1dSRodney W. Grimes */ 43df8bae1dSRodney W. Grimes 44df8bae1dSRodney W. Grimes /* 45df8bae1dSRodney W. Grimes * Mapped file (mmap) interface to VM 46df8bae1dSRodney W. Grimes */ 47df8bae1dSRodney W. Grimes 485591b823SEivind Eklund #include "opt_compat.h" 49e9822d92SJoerg Wunsch #include "opt_rlimit.h" 50e9822d92SJoerg Wunsch 51df8bae1dSRodney W. Grimes #include <sys/param.h> 521f6889a1SMatthew Dillon #include <sys/kernel.h> 53df8bae1dSRodney W. Grimes #include <sys/systm.h> 54d2d3e875SBruce Evans #include <sys/sysproto.h> 55df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 56df8bae1dSRodney W. Grimes #include <sys/proc.h> 57df8bae1dSRodney W. Grimes #include <sys/vnode.h> 583ac4d1efSBruce Evans #include <sys/fcntl.h> 59df8bae1dSRodney W. Grimes #include <sys/file.h> 60df8bae1dSRodney W. Grimes #include <sys/mman.h> 61df8bae1dSRodney W. Grimes #include <sys/conf.h> 624183b6b6SPeter Wemm #include <sys/stat.h> 63efeaf95aSDavid Greenman #include <sys/vmmeter.h> 641f6889a1SMatthew Dillon #include <sys/sysctl.h> 65df8bae1dSRodney W. Grimes 66df8bae1dSRodney W. Grimes #include <vm/vm.h> 67efeaf95aSDavid Greenman #include <vm/vm_param.h> 68996c772fSJohn Dyson #include <sys/lock.h> 69efeaf95aSDavid Greenman #include <vm/pmap.h> 70efeaf95aSDavid Greenman #include <vm/vm_map.h> 71efeaf95aSDavid Greenman #include <vm/vm_object.h> 721c7c3c6aSMatthew Dillon #include <vm/vm_page.h> 73df8bae1dSRodney W. Grimes #include <vm/vm_pager.h> 74b5e8ce9fSBruce Evans #include <vm/vm_pageout.h> 75efeaf95aSDavid Greenman #include <vm/vm_extern.h> 76867a482dSJohn Dyson #include <vm/vm_page.h> 771f6889a1SMatthew Dillon #include <vm/vm_kern.h> 78df8bae1dSRodney W. Grimes 79d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 80df8bae1dSRodney W. Grimes struct sbrk_args { 81df8bae1dSRodney W. Grimes int incr; 82df8bae1dSRodney W. Grimes }; 83d2d3e875SBruce Evans #endif 840d94caffSDavid Greenman 851f6889a1SMatthew Dillon static int max_proc_mmap; 861f6889a1SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, ""); 871f6889a1SMatthew Dillon 881f6889a1SMatthew Dillon /* 891f6889a1SMatthew Dillon * Set the maximum number of vm_map_entry structures per process. Roughly 901f6889a1SMatthew Dillon * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100 911f6889a1SMatthew Dillon * of our KVM malloc space still results in generous limits. We want a 921f6889a1SMatthew Dillon * default that is good enough to prevent the kernel running out of resources 931f6889a1SMatthew Dillon * if attacked from compromised user account but generous enough such that 941f6889a1SMatthew Dillon * multi-threaded processes are not unduly inconvenienced. 951f6889a1SMatthew Dillon */ 961f6889a1SMatthew Dillon 971f6889a1SMatthew Dillon static void vmmapentry_rsrc_init __P((void *)); 981f6889a1SMatthew Dillon SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL) 991f6889a1SMatthew Dillon 1001f6889a1SMatthew Dillon static void 1011f6889a1SMatthew Dillon vmmapentry_rsrc_init(dummy) 1021f6889a1SMatthew Dillon void *dummy; 1031f6889a1SMatthew Dillon { 1041f6889a1SMatthew Dillon max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry); 1051f6889a1SMatthew Dillon max_proc_mmap /= 100; 1061f6889a1SMatthew Dillon } 1071f6889a1SMatthew Dillon 108df8bae1dSRodney W. Grimes /* ARGSUSED */ 109df8bae1dSRodney W. Grimes int 110cb226aaaSPoul-Henning Kamp sbrk(p, uap) 111df8bae1dSRodney W. Grimes struct proc *p; 112df8bae1dSRodney W. Grimes struct sbrk_args *uap; 113df8bae1dSRodney W. Grimes { 114df8bae1dSRodney W. Grimes 115df8bae1dSRodney W. Grimes /* Not yet implemented */ 116df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 117df8bae1dSRodney W. Grimes } 118df8bae1dSRodney W. Grimes 119d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 120df8bae1dSRodney W. Grimes struct sstk_args { 121df8bae1dSRodney W. Grimes int incr; 122df8bae1dSRodney W. Grimes }; 123d2d3e875SBruce Evans #endif 1240d94caffSDavid Greenman 125df8bae1dSRodney W. Grimes /* ARGSUSED */ 126df8bae1dSRodney W. Grimes int 127cb226aaaSPoul-Henning Kamp sstk(p, uap) 128df8bae1dSRodney W. Grimes struct proc *p; 129df8bae1dSRodney W. Grimes struct sstk_args *uap; 130df8bae1dSRodney W. Grimes { 131df8bae1dSRodney W. Grimes 132df8bae1dSRodney W. Grimes /* Not yet implemented */ 133df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 134df8bae1dSRodney W. Grimes } 135df8bae1dSRodney W. Grimes 136df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 137d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 138df8bae1dSRodney W. Grimes struct getpagesize_args { 139df8bae1dSRodney W. Grimes int dummy; 140df8bae1dSRodney W. Grimes }; 141d2d3e875SBruce Evans #endif 1420d94caffSDavid Greenman 143df8bae1dSRodney W. Grimes /* ARGSUSED */ 144df8bae1dSRodney W. Grimes int 145cb226aaaSPoul-Henning Kamp ogetpagesize(p, uap) 146df8bae1dSRodney W. Grimes struct proc *p; 147df8bae1dSRodney W. Grimes struct getpagesize_args *uap; 148df8bae1dSRodney W. Grimes { 149df8bae1dSRodney W. Grimes 150cb226aaaSPoul-Henning Kamp p->p_retval[0] = PAGE_SIZE; 151df8bae1dSRodney W. Grimes return (0); 152df8bae1dSRodney W. Grimes } 153df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 154df8bae1dSRodney W. Grimes 15554f42e4bSPeter Wemm 15654f42e4bSPeter Wemm /* 15754f42e4bSPeter Wemm * Memory Map (mmap) system call. Note that the file offset 15854f42e4bSPeter Wemm * and address are allowed to be NOT page aligned, though if 15954f42e4bSPeter Wemm * the MAP_FIXED flag it set, both must have the same remainder 16054f42e4bSPeter Wemm * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not 16154f42e4bSPeter Wemm * page-aligned, the actual mapping starts at trunc_page(addr) 16254f42e4bSPeter Wemm * and the return value is adjusted up by the page offset. 163b4309055SMatthew Dillon * 164b4309055SMatthew Dillon * Generally speaking, only character devices which are themselves 165b4309055SMatthew Dillon * memory-based, such as a video framebuffer, can be mmap'd. Otherwise 166b4309055SMatthew Dillon * there would be no cache coherency between a descriptor and a VM mapping 167b4309055SMatthew Dillon * both to the same character device. 168b4309055SMatthew Dillon * 169b4309055SMatthew Dillon * Block devices can be mmap'd no matter what they represent. Cache coherency 170b4309055SMatthew Dillon * is maintained as long as you do not write directly to the underlying 171b4309055SMatthew Dillon * character device. 17254f42e4bSPeter Wemm */ 173d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 174df8bae1dSRodney W. Grimes struct mmap_args { 175651bb817SAlexander Langer void *addr; 176df8bae1dSRodney W. Grimes size_t len; 177df8bae1dSRodney W. Grimes int prot; 178df8bae1dSRodney W. Grimes int flags; 179df8bae1dSRodney W. Grimes int fd; 180df8bae1dSRodney W. Grimes long pad; 181df8bae1dSRodney W. Grimes off_t pos; 182df8bae1dSRodney W. Grimes }; 183d2d3e875SBruce Evans #endif 184df8bae1dSRodney W. Grimes 185df8bae1dSRodney W. Grimes int 186cb226aaaSPoul-Henning Kamp mmap(p, uap) 187df8bae1dSRodney W. Grimes struct proc *p; 188df8bae1dSRodney W. Grimes register struct mmap_args *uap; 189df8bae1dSRodney W. Grimes { 190df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 191df8bae1dSRodney W. Grimes register struct file *fp; 192df8bae1dSRodney W. Grimes struct vnode *vp; 193df8bae1dSRodney W. Grimes vm_offset_t addr; 1949154ee6aSPeter Wemm vm_size_t size, pageoff; 195df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 196651bb817SAlexander Langer void *handle; 197df8bae1dSRodney W. Grimes int flags, error; 198c8bdd56bSGuido van Rooij int disablexworkaround; 19954f42e4bSPeter Wemm off_t pos; 2001f6889a1SMatthew Dillon struct vmspace *vms = p->p_vmspace; 2019ff5ce6bSBoris Popov vm_object_t obj; 202df8bae1dSRodney W. Grimes 20354f42e4bSPeter Wemm addr = (vm_offset_t) uap->addr; 20454f42e4bSPeter Wemm size = uap->len; 205df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 206df8bae1dSRodney W. Grimes flags = uap->flags; 20754f42e4bSPeter Wemm pos = uap->pos; 20854f42e4bSPeter Wemm 20954f42e4bSPeter Wemm /* make sure mapping fits into numeric range etc */ 210fc565456SDmitrij Tejblum if ((ssize_t) uap->len < 0 || 21154f42e4bSPeter Wemm ((flags & MAP_ANON) && uap->fd != -1)) 212df8bae1dSRodney W. Grimes return (EINVAL); 2139154ee6aSPeter Wemm 2142267af78SJulian Elischer if (flags & MAP_STACK) { 2152267af78SJulian Elischer if ((uap->fd != -1) || 2162267af78SJulian Elischer ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE))) 2172267af78SJulian Elischer return (EINVAL); 2182267af78SJulian Elischer flags |= MAP_ANON; 2192267af78SJulian Elischer pos = 0; 2202907af2aSJulian Elischer } 2212907af2aSJulian Elischer 2229154ee6aSPeter Wemm /* 22354f42e4bSPeter Wemm * Align the file position to a page boundary, 22454f42e4bSPeter Wemm * and save its page offset component. 2259154ee6aSPeter Wemm */ 22654f42e4bSPeter Wemm pageoff = (pos & PAGE_MASK); 22754f42e4bSPeter Wemm pos -= pageoff; 22854f42e4bSPeter Wemm 22954f42e4bSPeter Wemm /* Adjust size for rounding (on both ends). */ 23054f42e4bSPeter Wemm size += pageoff; /* low end... */ 23154f42e4bSPeter Wemm size = (vm_size_t) round_page(size); /* hi end */ 2329154ee6aSPeter Wemm 233df8bae1dSRodney W. Grimes /* 2340d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 2350d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 236df8bae1dSRodney W. Grimes */ 237df8bae1dSRodney W. Grimes if (flags & MAP_FIXED) { 23854f42e4bSPeter Wemm /* 23954f42e4bSPeter Wemm * The specified address must have the same remainder 24054f42e4bSPeter Wemm * as the file offset taken modulo PAGE_SIZE, so it 24154f42e4bSPeter Wemm * should be aligned after adjustment by pageoff. 24254f42e4bSPeter Wemm */ 24354f42e4bSPeter Wemm addr -= pageoff; 24454f42e4bSPeter Wemm if (addr & PAGE_MASK) 24554f42e4bSPeter Wemm return (EINVAL); 24654f42e4bSPeter Wemm /* Address range must be all in user VM space. */ 247bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 248df8bae1dSRodney W. Grimes return (EINVAL); 24926f9a767SRodney W. Grimes #ifndef i386 250df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 251df8bae1dSRodney W. Grimes return (EINVAL); 25226f9a767SRodney W. Grimes #endif 253bbc0ec52SDavid Greenman if (addr + size < addr) 254df8bae1dSRodney W. Grimes return (EINVAL); 255df8bae1dSRodney W. Grimes } 256df8bae1dSRodney W. Grimes /* 25754f42e4bSPeter Wemm * XXX for non-fixed mappings where no hint is provided or 25854f42e4bSPeter Wemm * the hint would fall in the potential heap space, 25954f42e4bSPeter Wemm * place it after the end of the largest possible heap. 260df8bae1dSRodney W. Grimes * 26154f42e4bSPeter Wemm * There should really be a pmap call to determine a reasonable 26254f42e4bSPeter Wemm * location. 263df8bae1dSRodney W. Grimes */ 264d28ab90fSLuoqi Chen else if (addr == 0 || 2651f6889a1SMatthew Dillon (addr >= round_page((vm_offset_t)vms->vm_taddr) && 2661f6889a1SMatthew Dillon addr < round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ))) 2671f6889a1SMatthew Dillon addr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ); 26854f42e4bSPeter Wemm 269df8bae1dSRodney W. Grimes if (flags & MAP_ANON) { 270df8bae1dSRodney W. Grimes /* 271df8bae1dSRodney W. Grimes * Mapping blank space is trivial. 272df8bae1dSRodney W. Grimes */ 273df8bae1dSRodney W. Grimes handle = NULL; 274df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 27554f42e4bSPeter Wemm pos = 0; 276df8bae1dSRodney W. Grimes } else { 277df8bae1dSRodney W. Grimes /* 2780d94caffSDavid Greenman * Mapping file, get fp for validation. Obtain vnode and make 2790d94caffSDavid Greenman * sure it is of appropriate type. 280df8bae1dSRodney W. Grimes */ 281df8bae1dSRodney W. Grimes if (((unsigned) uap->fd) >= fdp->fd_nfiles || 282df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL) 283df8bae1dSRodney W. Grimes return (EBADF); 284df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_VNODE) 285df8bae1dSRodney W. Grimes return (EINVAL); 286aa543039SGarrett Wollman /* 287aa543039SGarrett Wollman * POSIX shared-memory objects are defined to have 288aa543039SGarrett Wollman * kernel persistence, and are not defined to support 289aa543039SGarrett Wollman * read(2)/write(2) -- or even open(2). Thus, we can 290aa543039SGarrett Wollman * use MAP_ASYNC to trade on-disk coherence for speed. 291aa543039SGarrett Wollman * The shm_open(3) library routine turns on the FPOSIXSHM 292aa543039SGarrett Wollman * flag to request this behavior. 293aa543039SGarrett Wollman */ 294aa543039SGarrett Wollman if (fp->f_flag & FPOSIXSHM) 295aa543039SGarrett Wollman flags |= MAP_NOSYNC; 296df8bae1dSRodney W. Grimes vp = (struct vnode *) fp->f_data; 297df8bae1dSRodney W. Grimes if (vp->v_type != VREG && vp->v_type != VCHR) 298df8bae1dSRodney W. Grimes return (EINVAL); 2999ff5ce6bSBoris Popov if (vp->v_type == VREG) { 3009ff5ce6bSBoris Popov /* 3019ff5ce6bSBoris Popov * Get the proper underlying object 3029ff5ce6bSBoris Popov */ 3039ff5ce6bSBoris Popov if (VOP_GETVOBJECT(vp, &obj) != 0) 3049ff5ce6bSBoris Popov return (EINVAL); 3059ff5ce6bSBoris Popov vp = (struct vnode*)obj->handle; 3069ff5ce6bSBoris Popov } 307df8bae1dSRodney W. Grimes /* 3080d94caffSDavid Greenman * XXX hack to handle use of /dev/zero to map anon memory (ala 3090d94caffSDavid Greenman * SunOS). 310df8bae1dSRodney W. Grimes */ 3112589f249SMark Murray if ((vp->v_type == VCHR) && 3122589f249SMark Murray (vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON)) { 313df8bae1dSRodney W. Grimes handle = NULL; 314df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 315df8bae1dSRodney W. Grimes flags |= MAP_ANON; 31654f42e4bSPeter Wemm pos = 0; 317df8bae1dSRodney W. Grimes } else { 318df8bae1dSRodney W. Grimes /* 319c8bdd56bSGuido van Rooij * cdevs does not provide private mappings of any kind. 320c8bdd56bSGuido van Rooij */ 321c8bdd56bSGuido van Rooij /* 322c8bdd56bSGuido van Rooij * However, for XIG X server to continue to work, 323c8bdd56bSGuido van Rooij * we should allow the superuser to do it anyway. 324c8bdd56bSGuido van Rooij * We only allow it at securelevel < 1. 325c8bdd56bSGuido van Rooij * (Because the XIG X server writes directly to video 326c8bdd56bSGuido van Rooij * memory via /dev/mem, it should never work at any 327c8bdd56bSGuido van Rooij * other securelevel. 328c8bdd56bSGuido van Rooij * XXX this will have to go 329c8bdd56bSGuido van Rooij */ 330c8bdd56bSGuido van Rooij if (securelevel >= 1) 331c8bdd56bSGuido van Rooij disablexworkaround = 1; 332c8bdd56bSGuido van Rooij else 333f711d546SPoul-Henning Kamp disablexworkaround = suser(p); 334c8bdd56bSGuido van Rooij if (vp->v_type == VCHR && disablexworkaround && 335c8bdd56bSGuido van Rooij (flags & (MAP_PRIVATE|MAP_COPY))) 336c8bdd56bSGuido van Rooij return (EINVAL); 337c8bdd56bSGuido van Rooij /* 338df8bae1dSRodney W. Grimes * Ensure that file and memory protections are 339df8bae1dSRodney W. Grimes * compatible. Note that we only worry about 340df8bae1dSRodney W. Grimes * writability if mapping is shared; in this case, 341df8bae1dSRodney W. Grimes * current and max prot are dictated by the open file. 342df8bae1dSRodney W. Grimes * XXX use the vnode instead? Problem is: what 3430d94caffSDavid Greenman * credentials do we use for determination? What if 3440d94caffSDavid Greenman * proc does a setuid? 345df8bae1dSRodney W. Grimes */ 346df8bae1dSRodney W. Grimes maxprot = VM_PROT_EXECUTE; /* ??? */ 347df8bae1dSRodney W. Grimes if (fp->f_flag & FREAD) 348df8bae1dSRodney W. Grimes maxprot |= VM_PROT_READ; 349df8bae1dSRodney W. Grimes else if (prot & PROT_READ) 350df8bae1dSRodney W. Grimes return (EACCES); 351c8bdd56bSGuido van Rooij /* 352c8bdd56bSGuido van Rooij * If we are sharing potential changes (either via 353c8bdd56bSGuido van Rooij * MAP_SHARED or via the implicit sharing of character 354c8bdd56bSGuido van Rooij * device mappings), and we are trying to get write 355c8bdd56bSGuido van Rooij * permission although we opened it without asking 356c8bdd56bSGuido van Rooij * for it, bail out. Check for superuser, only if 357c8bdd56bSGuido van Rooij * we're at securelevel < 1, to allow the XIG X server 358c8bdd56bSGuido van Rooij * to continue to work. 359c8bdd56bSGuido van Rooij */ 36005feb99fSGuido van Rooij 36105feb99fSGuido van Rooij if ((flags & MAP_SHARED) != 0 || 36205feb99fSGuido van Rooij (vp->v_type == VCHR && disablexworkaround)) { 36305feb99fSGuido van Rooij if ((fp->f_flag & FWRITE) != 0) { 3644183b6b6SPeter Wemm struct vattr va; 36505feb99fSGuido van Rooij if ((error = 36605feb99fSGuido van Rooij VOP_GETATTR(vp, &va, 36705feb99fSGuido van Rooij p->p_ucred, p))) 36805feb99fSGuido van Rooij return (error); 36905feb99fSGuido van Rooij if ((va.va_flags & 3703592b715SKirk McKusick (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0) 371df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 37205feb99fSGuido van Rooij else if (prot & PROT_WRITE) 37305feb99fSGuido van Rooij return (EPERM); 37405feb99fSGuido van Rooij } else if ((prot & PROT_WRITE) != 0) 37505feb99fSGuido van Rooij return (EACCES); 37605feb99fSGuido van Rooij } else 37705feb99fSGuido van Rooij maxprot |= VM_PROT_WRITE; 37805feb99fSGuido van Rooij 379651bb817SAlexander Langer handle = (void *)vp; 380df8bae1dSRodney W. Grimes } 381df8bae1dSRodney W. Grimes } 3821f6889a1SMatthew Dillon 3831f6889a1SMatthew Dillon /* 3841f6889a1SMatthew Dillon * Do not allow more then a certain number of vm_map_entry structures 3851f6889a1SMatthew Dillon * per process. Scale with the number of rforks sharing the map 3861f6889a1SMatthew Dillon * to make the limit reasonable for threads. 3871f6889a1SMatthew Dillon */ 3881f6889a1SMatthew Dillon if (max_proc_mmap && 3891f6889a1SMatthew Dillon vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) { 3901f6889a1SMatthew Dillon return (ENOMEM); 3911f6889a1SMatthew Dillon } 3921f6889a1SMatthew Dillon 3931f6889a1SMatthew Dillon error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot, 39454f42e4bSPeter Wemm flags, handle, pos); 395df8bae1dSRodney W. Grimes if (error == 0) 396711458e3SDoug Rabson p->p_retval[0] = (register_t) (addr + pageoff); 397df8bae1dSRodney W. Grimes return (error); 398df8bae1dSRodney W. Grimes } 399df8bae1dSRodney W. Grimes 40005f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43 401d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 40205f0fdd2SPoul-Henning Kamp struct ommap_args { 40305f0fdd2SPoul-Henning Kamp caddr_t addr; 40405f0fdd2SPoul-Henning Kamp int len; 40505f0fdd2SPoul-Henning Kamp int prot; 40605f0fdd2SPoul-Henning Kamp int flags; 40705f0fdd2SPoul-Henning Kamp int fd; 40805f0fdd2SPoul-Henning Kamp long pos; 40905f0fdd2SPoul-Henning Kamp }; 410d2d3e875SBruce Evans #endif 41105f0fdd2SPoul-Henning Kamp int 412cb226aaaSPoul-Henning Kamp ommap(p, uap) 41305f0fdd2SPoul-Henning Kamp struct proc *p; 41405f0fdd2SPoul-Henning Kamp register struct ommap_args *uap; 41505f0fdd2SPoul-Henning Kamp { 41605f0fdd2SPoul-Henning Kamp struct mmap_args nargs; 41705f0fdd2SPoul-Henning Kamp static const char cvtbsdprot[8] = { 41805f0fdd2SPoul-Henning Kamp 0, 41905f0fdd2SPoul-Henning Kamp PROT_EXEC, 42005f0fdd2SPoul-Henning Kamp PROT_WRITE, 42105f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE, 42205f0fdd2SPoul-Henning Kamp PROT_READ, 42305f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_READ, 42405f0fdd2SPoul-Henning Kamp PROT_WRITE | PROT_READ, 42505f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE | PROT_READ, 42605f0fdd2SPoul-Henning Kamp }; 4270d94caffSDavid Greenman 42805f0fdd2SPoul-Henning Kamp #define OMAP_ANON 0x0002 42905f0fdd2SPoul-Henning Kamp #define OMAP_COPY 0x0020 43005f0fdd2SPoul-Henning Kamp #define OMAP_SHARED 0x0010 43105f0fdd2SPoul-Henning Kamp #define OMAP_FIXED 0x0100 43205f0fdd2SPoul-Henning Kamp #define OMAP_INHERIT 0x0800 43305f0fdd2SPoul-Henning Kamp 43405f0fdd2SPoul-Henning Kamp nargs.addr = uap->addr; 43505f0fdd2SPoul-Henning Kamp nargs.len = uap->len; 43605f0fdd2SPoul-Henning Kamp nargs.prot = cvtbsdprot[uap->prot & 0x7]; 43705f0fdd2SPoul-Henning Kamp nargs.flags = 0; 43805f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_ANON) 43905f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_ANON; 44005f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_COPY) 44105f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_COPY; 44205f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_SHARED) 44305f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_SHARED; 44405f0fdd2SPoul-Henning Kamp else 44505f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_PRIVATE; 44605f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_FIXED) 44705f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_FIXED; 44805f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_INHERIT) 44905f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_INHERIT; 45005f0fdd2SPoul-Henning Kamp nargs.fd = uap->fd; 45105f0fdd2SPoul-Henning Kamp nargs.pos = uap->pos; 452cb226aaaSPoul-Henning Kamp return (mmap(p, &nargs)); 45305f0fdd2SPoul-Henning Kamp } 45405f0fdd2SPoul-Henning Kamp #endif /* COMPAT_43 */ 45505f0fdd2SPoul-Henning Kamp 45605f0fdd2SPoul-Henning Kamp 457d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 458df8bae1dSRodney W. Grimes struct msync_args { 459651bb817SAlexander Langer void *addr; 460df8bae1dSRodney W. Grimes int len; 461e6c6af11SDavid Greenman int flags; 462df8bae1dSRodney W. Grimes }; 463d2d3e875SBruce Evans #endif 464df8bae1dSRodney W. Grimes int 465cb226aaaSPoul-Henning Kamp msync(p, uap) 466df8bae1dSRodney W. Grimes struct proc *p; 467df8bae1dSRodney W. Grimes struct msync_args *uap; 468df8bae1dSRodney W. Grimes { 469df8bae1dSRodney W. Grimes vm_offset_t addr; 470dabee6feSPeter Wemm vm_size_t size, pageoff; 471e6c6af11SDavid Greenman int flags; 472df8bae1dSRodney W. Grimes vm_map_t map; 473df8bae1dSRodney W. Grimes int rv; 474df8bae1dSRodney W. Grimes 475df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 4769154ee6aSPeter Wemm size = uap->len; 477e6c6af11SDavid Greenman flags = uap->flags; 478e6c6af11SDavid Greenman 479dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 480dabee6feSPeter Wemm addr -= pageoff; 481dabee6feSPeter Wemm size += pageoff; 482dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 4839154ee6aSPeter Wemm if (addr + size < addr) 484dabee6feSPeter Wemm return(EINVAL); 485dabee6feSPeter Wemm 486dabee6feSPeter Wemm if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 4871e62bc63SDavid Greenman return (EINVAL); 4881e62bc63SDavid Greenman 4899154ee6aSPeter Wemm map = &p->p_vmspace->vm_map; 4909154ee6aSPeter Wemm 491df8bae1dSRodney W. Grimes /* 492df8bae1dSRodney W. Grimes * XXX Gak! If size is zero we are supposed to sync "all modified 4930d94caffSDavid Greenman * pages with the region containing addr". Unfortunately, we don't 4940d94caffSDavid Greenman * really keep track of individual mmaps so we approximate by flushing 4950d94caffSDavid Greenman * the range of the map entry containing addr. This can be incorrect 4960d94caffSDavid Greenman * if the region splits or is coalesced with a neighbor. 497df8bae1dSRodney W. Grimes */ 498df8bae1dSRodney W. Grimes if (size == 0) { 499df8bae1dSRodney W. Grimes vm_map_entry_t entry; 500df8bae1dSRodney W. Grimes 501df8bae1dSRodney W. Grimes vm_map_lock_read(map); 502df8bae1dSRodney W. Grimes rv = vm_map_lookup_entry(map, addr, &entry); 503df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 504fbcfcdf7SDavid Greenman if (rv == FALSE) 505df8bae1dSRodney W. Grimes return (EINVAL); 506df8bae1dSRodney W. Grimes addr = entry->start; 507df8bae1dSRodney W. Grimes size = entry->end - entry->start; 508df8bae1dSRodney W. Grimes } 509e6c6af11SDavid Greenman 510df8bae1dSRodney W. Grimes /* 511df8bae1dSRodney W. Grimes * Clean the pages and interpret the return value. 512df8bae1dSRodney W. Grimes */ 5136c534ad8SDavid Greenman rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 514e6c6af11SDavid Greenman (flags & MS_INVALIDATE) != 0); 515e6c6af11SDavid Greenman 516df8bae1dSRodney W. Grimes switch (rv) { 517df8bae1dSRodney W. Grimes case KERN_SUCCESS: 518df8bae1dSRodney W. Grimes break; 519df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 520df8bae1dSRodney W. Grimes return (EINVAL); /* Sun returns ENOMEM? */ 521df8bae1dSRodney W. Grimes case KERN_FAILURE: 522df8bae1dSRodney W. Grimes return (EIO); 523df8bae1dSRodney W. Grimes default: 524df8bae1dSRodney W. Grimes return (EINVAL); 525df8bae1dSRodney W. Grimes } 526e6c6af11SDavid Greenman 527df8bae1dSRodney W. Grimes return (0); 528df8bae1dSRodney W. Grimes } 529df8bae1dSRodney W. Grimes 530d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 531df8bae1dSRodney W. Grimes struct munmap_args { 532651bb817SAlexander Langer void *addr; 5339154ee6aSPeter Wemm size_t len; 534df8bae1dSRodney W. Grimes }; 535d2d3e875SBruce Evans #endif 536df8bae1dSRodney W. Grimes int 537cb226aaaSPoul-Henning Kamp munmap(p, uap) 538df8bae1dSRodney W. Grimes register struct proc *p; 539df8bae1dSRodney W. Grimes register struct munmap_args *uap; 540df8bae1dSRodney W. Grimes { 541df8bae1dSRodney W. Grimes vm_offset_t addr; 542dabee6feSPeter Wemm vm_size_t size, pageoff; 543df8bae1dSRodney W. Grimes vm_map_t map; 544df8bae1dSRodney W. Grimes 545df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 5469154ee6aSPeter Wemm size = uap->len; 547dabee6feSPeter Wemm 548dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 549dabee6feSPeter Wemm addr -= pageoff; 550dabee6feSPeter Wemm size += pageoff; 551dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 5529154ee6aSPeter Wemm if (addr + size < addr) 553df8bae1dSRodney W. Grimes return(EINVAL); 5549154ee6aSPeter Wemm 555df8bae1dSRodney W. Grimes if (size == 0) 556df8bae1dSRodney W. Grimes return (0); 557dabee6feSPeter Wemm 558df8bae1dSRodney W. Grimes /* 5590d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 5600d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 561df8bae1dSRodney W. Grimes */ 562bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 563df8bae1dSRodney W. Grimes return (EINVAL); 56426f9a767SRodney W. Grimes #ifndef i386 565df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 566df8bae1dSRodney W. Grimes return (EINVAL); 56726f9a767SRodney W. Grimes #endif 568df8bae1dSRodney W. Grimes map = &p->p_vmspace->vm_map; 569df8bae1dSRodney W. Grimes /* 570df8bae1dSRodney W. Grimes * Make sure entire range is allocated. 571df8bae1dSRodney W. Grimes */ 572df8bae1dSRodney W. Grimes if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) 573df8bae1dSRodney W. Grimes return (EINVAL); 574df8bae1dSRodney W. Grimes /* returns nothing but KERN_SUCCESS anyway */ 575df8bae1dSRodney W. Grimes (void) vm_map_remove(map, addr, addr + size); 576df8bae1dSRodney W. Grimes return (0); 577df8bae1dSRodney W. Grimes } 578df8bae1dSRodney W. Grimes 579df8bae1dSRodney W. Grimes void 58090324b07SDavid Greenman munmapfd(p, fd) 58190324b07SDavid Greenman struct proc *p; 582df8bae1dSRodney W. Grimes int fd; 583df8bae1dSRodney W. Grimes { 584df8bae1dSRodney W. Grimes /* 585c4ed5a07SDavid Greenman * XXX should unmap any regions mapped to this file 586df8bae1dSRodney W. Grimes */ 58790324b07SDavid Greenman p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED; 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes 590d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 591df8bae1dSRodney W. Grimes struct mprotect_args { 592651bb817SAlexander Langer const void *addr; 5939154ee6aSPeter Wemm size_t len; 594df8bae1dSRodney W. Grimes int prot; 595df8bae1dSRodney W. Grimes }; 596d2d3e875SBruce Evans #endif 597df8bae1dSRodney W. Grimes int 598cb226aaaSPoul-Henning Kamp mprotect(p, uap) 599df8bae1dSRodney W. Grimes struct proc *p; 600df8bae1dSRodney W. Grimes struct mprotect_args *uap; 601df8bae1dSRodney W. Grimes { 602df8bae1dSRodney W. Grimes vm_offset_t addr; 603dabee6feSPeter Wemm vm_size_t size, pageoff; 604df8bae1dSRodney W. Grimes register vm_prot_t prot; 605df8bae1dSRodney W. Grimes 606df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 6079154ee6aSPeter Wemm size = uap->len; 608df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 609d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 610d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 611d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 612d0aea04fSJohn Dyson #endif 613df8bae1dSRodney W. Grimes 614dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 615dabee6feSPeter Wemm addr -= pageoff; 616dabee6feSPeter Wemm size += pageoff; 617dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 6189154ee6aSPeter Wemm if (addr + size < addr) 619dabee6feSPeter Wemm return(EINVAL); 620dabee6feSPeter Wemm 621df8bae1dSRodney W. Grimes switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot, 622df8bae1dSRodney W. Grimes FALSE)) { 623df8bae1dSRodney W. Grimes case KERN_SUCCESS: 624df8bae1dSRodney W. Grimes return (0); 625df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 626df8bae1dSRodney W. Grimes return (EACCES); 627df8bae1dSRodney W. Grimes } 628df8bae1dSRodney W. Grimes return (EINVAL); 629df8bae1dSRodney W. Grimes } 630df8bae1dSRodney W. Grimes 631d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 632dabee6feSPeter Wemm struct minherit_args { 633651bb817SAlexander Langer void *addr; 6349154ee6aSPeter Wemm size_t len; 635dabee6feSPeter Wemm int inherit; 636dabee6feSPeter Wemm }; 637dabee6feSPeter Wemm #endif 638dabee6feSPeter Wemm int 639cb226aaaSPoul-Henning Kamp minherit(p, uap) 640dabee6feSPeter Wemm struct proc *p; 641dabee6feSPeter Wemm struct minherit_args *uap; 642dabee6feSPeter Wemm { 643dabee6feSPeter Wemm vm_offset_t addr; 644dabee6feSPeter Wemm vm_size_t size, pageoff; 645dabee6feSPeter Wemm register vm_inherit_t inherit; 646dabee6feSPeter Wemm 647dabee6feSPeter Wemm addr = (vm_offset_t)uap->addr; 6489154ee6aSPeter Wemm size = uap->len; 649dabee6feSPeter Wemm inherit = uap->inherit; 650dabee6feSPeter Wemm 651dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 652dabee6feSPeter Wemm addr -= pageoff; 653dabee6feSPeter Wemm size += pageoff; 654dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 6559154ee6aSPeter Wemm if (addr + size < addr) 656dabee6feSPeter Wemm return(EINVAL); 657dabee6feSPeter Wemm 658dabee6feSPeter Wemm switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size, 659dabee6feSPeter Wemm inherit)) { 660dabee6feSPeter Wemm case KERN_SUCCESS: 661dabee6feSPeter Wemm return (0); 662dabee6feSPeter Wemm case KERN_PROTECTION_FAILURE: 663dabee6feSPeter Wemm return (EACCES); 664dabee6feSPeter Wemm } 665dabee6feSPeter Wemm return (EINVAL); 666dabee6feSPeter Wemm } 667dabee6feSPeter Wemm 668dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 669df8bae1dSRodney W. Grimes struct madvise_args { 670651bb817SAlexander Langer void *addr; 6719154ee6aSPeter Wemm size_t len; 672df8bae1dSRodney W. Grimes int behav; 673df8bae1dSRodney W. Grimes }; 674d2d3e875SBruce Evans #endif 6750d94caffSDavid Greenman 676df8bae1dSRodney W. Grimes /* ARGSUSED */ 677df8bae1dSRodney W. Grimes int 678cb226aaaSPoul-Henning Kamp madvise(p, uap) 679df8bae1dSRodney W. Grimes struct proc *p; 680df8bae1dSRodney W. Grimes struct madvise_args *uap; 681df8bae1dSRodney W. Grimes { 682f35329acSJohn Dyson vm_offset_t start, end; 683b4309055SMatthew Dillon 684b4309055SMatthew Dillon /* 685b4309055SMatthew Dillon * Check for illegal behavior 686b4309055SMatthew Dillon */ 6879730a5daSPaul Saab if (uap->behav < 0 || uap->behav > MADV_CORE) 688b4309055SMatthew Dillon return (EINVAL); 689867a482dSJohn Dyson /* 690867a482dSJohn Dyson * Check for illegal addresses. Watch out for address wrap... Note 691867a482dSJohn Dyson * that VM_*_ADDRESS are not constants due to casts (argh). 692867a482dSJohn Dyson */ 693867a482dSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && 694867a482dSJohn Dyson ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS) 695867a482dSJohn Dyson return (EINVAL); 696867a482dSJohn Dyson #ifndef i386 697867a482dSJohn Dyson if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS) 698867a482dSJohn Dyson return (EINVAL); 699867a482dSJohn Dyson #endif 700867a482dSJohn Dyson if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr) 701867a482dSJohn Dyson return (EINVAL); 702867a482dSJohn Dyson 703867a482dSJohn Dyson /* 704867a482dSJohn Dyson * Since this routine is only advisory, we default to conservative 705867a482dSJohn Dyson * behavior. 706867a482dSJohn Dyson */ 707cd6eea25SDavid Greenman start = trunc_page((vm_offset_t) uap->addr); 708cd6eea25SDavid Greenman end = round_page((vm_offset_t) uap->addr + uap->len); 709867a482dSJohn Dyson 710b4309055SMatthew Dillon if (vm_map_madvise(&p->p_vmspace->vm_map, start, end, uap->behav)) 711b4309055SMatthew Dillon return (EINVAL); 712867a482dSJohn Dyson return (0); 713df8bae1dSRodney W. Grimes } 714df8bae1dSRodney W. Grimes 715d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 716df8bae1dSRodney W. Grimes struct mincore_args { 717651bb817SAlexander Langer const void *addr; 7189154ee6aSPeter Wemm size_t len; 719df8bae1dSRodney W. Grimes char *vec; 720df8bae1dSRodney W. Grimes }; 721d2d3e875SBruce Evans #endif 7220d94caffSDavid Greenman 723df8bae1dSRodney W. Grimes /* ARGSUSED */ 724df8bae1dSRodney W. Grimes int 725cb226aaaSPoul-Henning Kamp mincore(p, uap) 726df8bae1dSRodney W. Grimes struct proc *p; 727df8bae1dSRodney W. Grimes struct mincore_args *uap; 728df8bae1dSRodney W. Grimes { 729867a482dSJohn Dyson vm_offset_t addr, first_addr; 730867a482dSJohn Dyson vm_offset_t end, cend; 731867a482dSJohn Dyson pmap_t pmap; 732867a482dSJohn Dyson vm_map_t map; 73302c04a2fSJohn Dyson char *vec; 734867a482dSJohn Dyson int error; 735867a482dSJohn Dyson int vecindex, lastvecindex; 736867a482dSJohn Dyson register vm_map_entry_t current; 737867a482dSJohn Dyson vm_map_entry_t entry; 738867a482dSJohn Dyson int mincoreinfo; 739dd2622a8SAlan Cox unsigned int timestamp; 740df8bae1dSRodney W. Grimes 741867a482dSJohn Dyson /* 742867a482dSJohn Dyson * Make sure that the addresses presented are valid for user 743867a482dSJohn Dyson * mode. 744867a482dSJohn Dyson */ 745867a482dSJohn Dyson first_addr = addr = trunc_page((vm_offset_t) uap->addr); 7469154ee6aSPeter Wemm end = addr + (vm_size_t)round_page(uap->len); 74702c04a2fSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS) 74802c04a2fSJohn Dyson return (EINVAL); 74902c04a2fSJohn Dyson if (end < addr) 75002c04a2fSJohn Dyson return (EINVAL); 75102c04a2fSJohn Dyson 752867a482dSJohn Dyson /* 753867a482dSJohn Dyson * Address of byte vector 754867a482dSJohn Dyson */ 75502c04a2fSJohn Dyson vec = uap->vec; 756867a482dSJohn Dyson 757867a482dSJohn Dyson map = &p->p_vmspace->vm_map; 758b1028ad1SLuoqi Chen pmap = vmspace_pmap(p->p_vmspace); 759867a482dSJohn Dyson 760eff50fcdSAlan Cox vm_map_lock_read(map); 761dd2622a8SAlan Cox RestartScan: 762dd2622a8SAlan Cox timestamp = map->timestamp; 763867a482dSJohn Dyson 764867a482dSJohn Dyson if (!vm_map_lookup_entry(map, addr, &entry)) 765867a482dSJohn Dyson entry = entry->next; 766867a482dSJohn Dyson 767867a482dSJohn Dyson /* 768867a482dSJohn Dyson * Do this on a map entry basis so that if the pages are not 769867a482dSJohn Dyson * in the current processes address space, we can easily look 770867a482dSJohn Dyson * up the pages elsewhere. 771867a482dSJohn Dyson */ 772867a482dSJohn Dyson lastvecindex = -1; 773867a482dSJohn Dyson for(current = entry; 774867a482dSJohn Dyson (current != &map->header) && (current->start < end); 775867a482dSJohn Dyson current = current->next) { 776867a482dSJohn Dyson 777867a482dSJohn Dyson /* 778867a482dSJohn Dyson * ignore submaps (for now) or null objects 779867a482dSJohn Dyson */ 7809fdfe602SMatthew Dillon if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) || 781867a482dSJohn Dyson current->object.vm_object == NULL) 782867a482dSJohn Dyson continue; 783867a482dSJohn Dyson 784867a482dSJohn Dyson /* 785867a482dSJohn Dyson * limit this scan to the current map entry and the 786867a482dSJohn Dyson * limits for the mincore call 787867a482dSJohn Dyson */ 788867a482dSJohn Dyson if (addr < current->start) 789867a482dSJohn Dyson addr = current->start; 790867a482dSJohn Dyson cend = current->end; 791867a482dSJohn Dyson if (cend > end) 792867a482dSJohn Dyson cend = end; 793867a482dSJohn Dyson 794867a482dSJohn Dyson /* 795867a482dSJohn Dyson * scan this entry one page at a time 796867a482dSJohn Dyson */ 797867a482dSJohn Dyson while(addr < cend) { 798867a482dSJohn Dyson /* 799867a482dSJohn Dyson * Check pmap first, it is likely faster, also 800867a482dSJohn Dyson * it can provide info as to whether we are the 801867a482dSJohn Dyson * one referencing or modifying the page. 802867a482dSJohn Dyson */ 803867a482dSJohn Dyson mincoreinfo = pmap_mincore(pmap, addr); 804867a482dSJohn Dyson if (!mincoreinfo) { 805867a482dSJohn Dyson vm_pindex_t pindex; 806867a482dSJohn Dyson vm_ooffset_t offset; 807867a482dSJohn Dyson vm_page_t m; 808867a482dSJohn Dyson /* 809867a482dSJohn Dyson * calculate the page index into the object 810867a482dSJohn Dyson */ 811867a482dSJohn Dyson offset = current->offset + (addr - current->start); 812867a482dSJohn Dyson pindex = OFF_TO_IDX(offset); 813867a482dSJohn Dyson m = vm_page_lookup(current->object.vm_object, 814867a482dSJohn Dyson pindex); 815867a482dSJohn Dyson /* 816867a482dSJohn Dyson * if the page is resident, then gather information about 817867a482dSJohn Dyson * it. 818867a482dSJohn Dyson */ 819867a482dSJohn Dyson if (m) { 820867a482dSJohn Dyson mincoreinfo = MINCORE_INCORE; 821867a482dSJohn Dyson if (m->dirty || 8220385347cSPeter Wemm pmap_is_modified(m)) 823867a482dSJohn Dyson mincoreinfo |= MINCORE_MODIFIED_OTHER; 824867a482dSJohn Dyson if ((m->flags & PG_REFERENCED) || 8250385347cSPeter Wemm pmap_ts_referenced(m)) { 826e69763a3SDoug Rabson vm_page_flag_set(m, PG_REFERENCED); 827867a482dSJohn Dyson mincoreinfo |= MINCORE_REFERENCED_OTHER; 82802c04a2fSJohn Dyson } 829867a482dSJohn Dyson } 8309b5a5d81SJohn Dyson } 831867a482dSJohn Dyson 832867a482dSJohn Dyson /* 833dd2622a8SAlan Cox * subyte may page fault. In case it needs to modify 834dd2622a8SAlan Cox * the map, we release the lock. 835dd2622a8SAlan Cox */ 836dd2622a8SAlan Cox vm_map_unlock_read(map); 837dd2622a8SAlan Cox 838dd2622a8SAlan Cox /* 839867a482dSJohn Dyson * calculate index into user supplied byte vector 840867a482dSJohn Dyson */ 841867a482dSJohn Dyson vecindex = OFF_TO_IDX(addr - first_addr); 842867a482dSJohn Dyson 843867a482dSJohn Dyson /* 844867a482dSJohn Dyson * If we have skipped map entries, we need to make sure that 845867a482dSJohn Dyson * the byte vector is zeroed for those skipped entries. 846867a482dSJohn Dyson */ 847867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 848867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 849867a482dSJohn Dyson if (error) { 850867a482dSJohn Dyson return (EFAULT); 851867a482dSJohn Dyson } 852867a482dSJohn Dyson ++lastvecindex; 853867a482dSJohn Dyson } 854867a482dSJohn Dyson 855867a482dSJohn Dyson /* 856867a482dSJohn Dyson * Pass the page information to the user 857867a482dSJohn Dyson */ 858867a482dSJohn Dyson error = subyte( vec + vecindex, mincoreinfo); 859867a482dSJohn Dyson if (error) { 860867a482dSJohn Dyson return (EFAULT); 861867a482dSJohn Dyson } 862dd2622a8SAlan Cox 863dd2622a8SAlan Cox /* 864dd2622a8SAlan Cox * If the map has changed, due to the subyte, the previous 865dd2622a8SAlan Cox * output may be invalid. 866dd2622a8SAlan Cox */ 867dd2622a8SAlan Cox vm_map_lock_read(map); 868dd2622a8SAlan Cox if (timestamp != map->timestamp) 869dd2622a8SAlan Cox goto RestartScan; 870dd2622a8SAlan Cox 871867a482dSJohn Dyson lastvecindex = vecindex; 87202c04a2fSJohn Dyson addr += PAGE_SIZE; 87302c04a2fSJohn Dyson } 874867a482dSJohn Dyson } 875867a482dSJohn Dyson 876867a482dSJohn Dyson /* 877dd2622a8SAlan Cox * subyte may page fault. In case it needs to modify 878dd2622a8SAlan Cox * the map, we release the lock. 879dd2622a8SAlan Cox */ 880dd2622a8SAlan Cox vm_map_unlock_read(map); 881dd2622a8SAlan Cox 882dd2622a8SAlan Cox /* 883867a482dSJohn Dyson * Zero the last entries in the byte vector. 884867a482dSJohn Dyson */ 885867a482dSJohn Dyson vecindex = OFF_TO_IDX(end - first_addr); 886867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 887867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 888867a482dSJohn Dyson if (error) { 889867a482dSJohn Dyson return (EFAULT); 890867a482dSJohn Dyson } 891867a482dSJohn Dyson ++lastvecindex; 892867a482dSJohn Dyson } 893867a482dSJohn Dyson 894dd2622a8SAlan Cox /* 895dd2622a8SAlan Cox * If the map has changed, due to the subyte, the previous 896dd2622a8SAlan Cox * output may be invalid. 897dd2622a8SAlan Cox */ 898dd2622a8SAlan Cox vm_map_lock_read(map); 899dd2622a8SAlan Cox if (timestamp != map->timestamp) 900dd2622a8SAlan Cox goto RestartScan; 901eff50fcdSAlan Cox vm_map_unlock_read(map); 902dd2622a8SAlan Cox 90302c04a2fSJohn Dyson return (0); 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes 906d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 907df8bae1dSRodney W. Grimes struct mlock_args { 908651bb817SAlexander Langer const void *addr; 909df8bae1dSRodney W. Grimes size_t len; 910df8bae1dSRodney W. Grimes }; 911d2d3e875SBruce Evans #endif 912df8bae1dSRodney W. Grimes int 913cb226aaaSPoul-Henning Kamp mlock(p, uap) 914df8bae1dSRodney W. Grimes struct proc *p; 915df8bae1dSRodney W. Grimes struct mlock_args *uap; 916df8bae1dSRodney W. Grimes { 917df8bae1dSRodney W. Grimes vm_offset_t addr; 918dabee6feSPeter Wemm vm_size_t size, pageoff; 919df8bae1dSRodney W. Grimes int error; 920df8bae1dSRodney W. Grimes 921df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 9229154ee6aSPeter Wemm size = uap->len; 9239154ee6aSPeter Wemm 924dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 925dabee6feSPeter Wemm addr -= pageoff; 926dabee6feSPeter Wemm size += pageoff; 927dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 928dabee6feSPeter Wemm 929dabee6feSPeter Wemm /* disable wrap around */ 9309154ee6aSPeter Wemm if (addr + size < addr) 931df8bae1dSRodney W. Grimes return (EINVAL); 932dabee6feSPeter Wemm 933df8bae1dSRodney W. Grimes if (atop(size) + cnt.v_wire_count > vm_page_max_wired) 934df8bae1dSRodney W. Grimes return (EAGAIN); 9359154ee6aSPeter Wemm 936df8bae1dSRodney W. Grimes #ifdef pmap_wired_count 937df8bae1dSRodney W. Grimes if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 938df8bae1dSRodney W. Grimes p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 9394a40e3d4SJohn Dyson return (ENOMEM); 940df8bae1dSRodney W. Grimes #else 941f711d546SPoul-Henning Kamp error = suser(p); 94205f0fdd2SPoul-Henning Kamp if (error) 943df8bae1dSRodney W. Grimes return (error); 944df8bae1dSRodney W. Grimes #endif 945df8bae1dSRodney W. Grimes 9467aaaa4fdSJohn Dyson error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE); 947df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 948df8bae1dSRodney W. Grimes } 949df8bae1dSRodney W. Grimes 950d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 9514a40e3d4SJohn Dyson struct mlockall_args { 9524a40e3d4SJohn Dyson int how; 9534a40e3d4SJohn Dyson }; 9544a40e3d4SJohn Dyson #endif 9554a40e3d4SJohn Dyson 9564a40e3d4SJohn Dyson int 957cb226aaaSPoul-Henning Kamp mlockall(p, uap) 9584a40e3d4SJohn Dyson struct proc *p; 9594a40e3d4SJohn Dyson struct mlockall_args *uap; 9604a40e3d4SJohn Dyson { 9614a40e3d4SJohn Dyson return 0; 9624a40e3d4SJohn Dyson } 9634a40e3d4SJohn Dyson 9644a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_ 9654a40e3d4SJohn Dyson struct mlockall_args { 9664a40e3d4SJohn Dyson int how; 9674a40e3d4SJohn Dyson }; 9684a40e3d4SJohn Dyson #endif 9694a40e3d4SJohn Dyson 9704a40e3d4SJohn Dyson int 971cb226aaaSPoul-Henning Kamp munlockall(p, uap) 9724a40e3d4SJohn Dyson struct proc *p; 9734a40e3d4SJohn Dyson struct munlockall_args *uap; 9744a40e3d4SJohn Dyson { 9754a40e3d4SJohn Dyson return 0; 9764a40e3d4SJohn Dyson } 9774a40e3d4SJohn Dyson 9784a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_ 979df8bae1dSRodney W. Grimes struct munlock_args { 980651bb817SAlexander Langer const void *addr; 981df8bae1dSRodney W. Grimes size_t len; 982df8bae1dSRodney W. Grimes }; 983d2d3e875SBruce Evans #endif 984df8bae1dSRodney W. Grimes int 985cb226aaaSPoul-Henning Kamp munlock(p, uap) 986df8bae1dSRodney W. Grimes struct proc *p; 987df8bae1dSRodney W. Grimes struct munlock_args *uap; 988df8bae1dSRodney W. Grimes { 989df8bae1dSRodney W. Grimes vm_offset_t addr; 990dabee6feSPeter Wemm vm_size_t size, pageoff; 991df8bae1dSRodney W. Grimes int error; 992df8bae1dSRodney W. Grimes 993df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 9949154ee6aSPeter Wemm size = uap->len; 9959154ee6aSPeter Wemm 996dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 997dabee6feSPeter Wemm addr -= pageoff; 998dabee6feSPeter Wemm size += pageoff; 999dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 1000dabee6feSPeter Wemm 1001dabee6feSPeter Wemm /* disable wrap around */ 10029154ee6aSPeter Wemm if (addr + size < addr) 1003df8bae1dSRodney W. Grimes return (EINVAL); 1004dabee6feSPeter Wemm 1005df8bae1dSRodney W. Grimes #ifndef pmap_wired_count 1006f711d546SPoul-Henning Kamp error = suser(p); 100705f0fdd2SPoul-Henning Kamp if (error) 1008df8bae1dSRodney W. Grimes return (error); 1009df8bae1dSRodney W. Grimes #endif 1010df8bae1dSRodney W. Grimes 10117aaaa4fdSJohn Dyson error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE); 1012df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 1013df8bae1dSRodney W. Grimes } 1014df8bae1dSRodney W. Grimes 1015df8bae1dSRodney W. Grimes /* 1016df8bae1dSRodney W. Grimes * Internal version of mmap. 1017df8bae1dSRodney W. Grimes * Currently used by mmap, exec, and sys5 shared memory. 1018df8bae1dSRodney W. Grimes * Handle is either a vnode pointer or NULL for MAP_ANON. 1019df8bae1dSRodney W. Grimes */ 1020df8bae1dSRodney W. Grimes int 1021b9dcd593SBruce Evans vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot, 1022b9dcd593SBruce Evans vm_prot_t maxprot, int flags, 1023651bb817SAlexander Langer void *handle, 1024b9dcd593SBruce Evans vm_ooffset_t foff) 1025df8bae1dSRodney W. Grimes { 1026df8bae1dSRodney W. Grimes boolean_t fitit; 1027fcae040bSJohn Dyson vm_object_t object; 1028df8bae1dSRodney W. Grimes struct vnode *vp = NULL; 102924a1cce3SDavid Greenman objtype_t type; 1030df8bae1dSRodney W. Grimes int rv = KERN_SUCCESS; 1031bd7e5f99SJohn Dyson vm_ooffset_t objsize; 1032bd7e5f99SJohn Dyson int docow; 103306cb7259SDavid Greenman struct proc *p = curproc; 1034df8bae1dSRodney W. Grimes 1035df8bae1dSRodney W. Grimes if (size == 0) 1036df8bae1dSRodney W. Grimes return (0); 1037df8bae1dSRodney W. Grimes 103806cb7259SDavid Greenman objsize = size = round_page(size); 1039df8bae1dSRodney W. Grimes 1040df8bae1dSRodney W. Grimes /* 1041bc9ad247SDavid Greenman * We currently can only deal with page aligned file offsets. 1042bc9ad247SDavid Greenman * The check is here rather than in the syscall because the 1043bc9ad247SDavid Greenman * kernel calls this function internally for other mmaping 1044bc9ad247SDavid Greenman * operations (such as in exec) and non-aligned offsets will 1045bc9ad247SDavid Greenman * cause pmap inconsistencies...so we want to be sure to 1046bc9ad247SDavid Greenman * disallow this in all cases. 1047bc9ad247SDavid Greenman */ 1048bc9ad247SDavid Greenman if (foff & PAGE_MASK) 1049bc9ad247SDavid Greenman return (EINVAL); 1050bc9ad247SDavid Greenman 105106cb7259SDavid Greenman if ((flags & MAP_FIXED) == 0) { 105206cb7259SDavid Greenman fitit = TRUE; 105306cb7259SDavid Greenman *addr = round_page(*addr); 105406cb7259SDavid Greenman } else { 105506cb7259SDavid Greenman if (*addr != trunc_page(*addr)) 105606cb7259SDavid Greenman return (EINVAL); 105706cb7259SDavid Greenman fitit = FALSE; 105806cb7259SDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 105906cb7259SDavid Greenman } 106006cb7259SDavid Greenman 1061bc9ad247SDavid Greenman /* 106224a1cce3SDavid Greenman * Lookup/allocate object. 1063df8bae1dSRodney W. Grimes */ 10645f55e841SDavid Greenman if (flags & MAP_ANON) { 1065851c12ffSJohn Dyson type = OBJT_DEFAULT; 10665f55e841SDavid Greenman /* 10675f55e841SDavid Greenman * Unnamed anonymous regions always start at 0. 10685f55e841SDavid Greenman */ 106967bf6868SJohn Dyson if (handle == 0) 10705f55e841SDavid Greenman foff = 0; 10715f55e841SDavid Greenman } else { 1072df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 1073df8bae1dSRodney W. Grimes if (vp->v_type == VCHR) { 107424a1cce3SDavid Greenman type = OBJT_DEVICE; 1075a23d65bfSBruce Evans handle = (void *)(intptr_t)vp->v_rdev; 107606cb7259SDavid Greenman } else { 107706cb7259SDavid Greenman struct vattr vat; 107806cb7259SDavid Greenman int error; 107906cb7259SDavid Greenman 108006cb7259SDavid Greenman error = VOP_GETATTR(vp, &vat, p->p_ucred, p); 108106cb7259SDavid Greenman if (error) 108206cb7259SDavid Greenman return (error); 1083bd7e5f99SJohn Dyson objsize = round_page(vat.va_size); 108424a1cce3SDavid Greenman type = OBJT_VNODE; 108500d76afeSGuido van Rooij /* 108600d76afeSGuido van Rooij * if it is a regular file without any references 108700d76afeSGuido van Rooij * we do not need to sync it. 108800d76afeSGuido van Rooij */ 108900d76afeSGuido van Rooij if (vp->v_type == VREG && vat.va_nlink == 0) { 109000d76afeSGuido van Rooij flags |= MAP_NOSYNC; 109100d76afeSGuido van Rooij } 1092df8bae1dSRodney W. Grimes } 109306cb7259SDavid Greenman } 109494328e90SJohn Dyson 109594328e90SJohn Dyson if (handle == NULL) { 109694328e90SJohn Dyson object = NULL; 10974738fa09SAlan Cox docow = 0; 109894328e90SJohn Dyson } else { 10990a0a85b3SJohn Dyson object = vm_pager_allocate(type, 11006cde7a16SDavid Greenman handle, objsize, prot, foff); 110124a1cce3SDavid Greenman if (object == NULL) 110224a1cce3SDavid Greenman return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 11034738fa09SAlan Cox docow = MAP_PREFAULT_PARTIAL; 110494328e90SJohn Dyson } 1105df8bae1dSRodney W. Grimes 11065850152dSJohn Dyson /* 11078f2ec877SDavid Greenman * Force device mappings to be shared. 11085850152dSJohn Dyson */ 110924964514SPeter Wemm if (type == OBJT_DEVICE || type == OBJT_PHYS) { 11108f2ec877SDavid Greenman flags &= ~(MAP_PRIVATE|MAP_COPY); 11115850152dSJohn Dyson flags |= MAP_SHARED; 11128f2ec877SDavid Greenman } 11135850152dSJohn Dyson 11144f79d873SMatthew Dillon if ((flags & (MAP_ANON|MAP_SHARED)) == 0) 11154738fa09SAlan Cox docow |= MAP_COPY_ON_WRITE; 11164f79d873SMatthew Dillon if (flags & MAP_NOSYNC) 11174f79d873SMatthew Dillon docow |= MAP_DISABLE_SYNCER; 11189730a5daSPaul Saab if (flags & MAP_NOCORE) 11199730a5daSPaul Saab docow |= MAP_DISABLE_COREDUMP; 11205850152dSJohn Dyson 1121d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 1122d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 1123d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 1124d0aea04fSJohn Dyson 1125d0aea04fSJohn Dyson if (maxprot & VM_PROT_READ) 1126d0aea04fSJohn Dyson maxprot |= VM_PROT_EXECUTE; 1127d0aea04fSJohn Dyson #endif 1128d0aea04fSJohn Dyson 11290a0a85b3SJohn Dyson if (fitit) { 11300a0a85b3SJohn Dyson *addr = pmap_addr_hint(object, *addr, size); 11310a0a85b3SJohn Dyson } 11320a0a85b3SJohn Dyson 11332267af78SJulian Elischer if (flags & MAP_STACK) 11342267af78SJulian Elischer rv = vm_map_stack (map, *addr, size, prot, 11352267af78SJulian Elischer maxprot, docow); 11362267af78SJulian Elischer else 1137bd7e5f99SJohn Dyson rv = vm_map_find(map, object, foff, addr, size, fitit, 1138bd7e5f99SJohn Dyson prot, maxprot, docow); 1139bd7e5f99SJohn Dyson 1140df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 11417fb0c17eSDavid Greenman /* 114224a1cce3SDavid Greenman * Lose the object reference. Will destroy the 114324a1cce3SDavid Greenman * object if it's an unnamed anonymous mapping 114424a1cce3SDavid Greenman * or named anonymous without other references. 11457fb0c17eSDavid Greenman */ 1146df8bae1dSRodney W. Grimes vm_object_deallocate(object); 1147df8bae1dSRodney W. Grimes goto out; 1148df8bae1dSRodney W. Grimes } 1149e17bed12SJohn Dyson 1150df8bae1dSRodney W. Grimes /* 1151df8bae1dSRodney W. Grimes * Shared memory is also shared with children. 1152df8bae1dSRodney W. Grimes */ 11535850152dSJohn Dyson if (flags & (MAP_SHARED|MAP_INHERIT)) { 1154df8bae1dSRodney W. Grimes rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 1155df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 11567fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 1157df8bae1dSRodney W. Grimes goto out; 1158df8bae1dSRodney W. Grimes } 1159df8bae1dSRodney W. Grimes } 1160df8bae1dSRodney W. Grimes out: 1161df8bae1dSRodney W. Grimes switch (rv) { 1162df8bae1dSRodney W. Grimes case KERN_SUCCESS: 1163df8bae1dSRodney W. Grimes return (0); 1164df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 1165df8bae1dSRodney W. Grimes case KERN_NO_SPACE: 1166df8bae1dSRodney W. Grimes return (ENOMEM); 1167df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 1168df8bae1dSRodney W. Grimes return (EACCES); 1169df8bae1dSRodney W. Grimes default: 1170df8bae1dSRodney W. Grimes return (EINVAL); 1171df8bae1dSRodney W. Grimes } 1172df8bae1dSRodney W. Grimes } 1173