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; 201df8bae1dSRodney W. Grimes 20254f42e4bSPeter Wemm addr = (vm_offset_t) uap->addr; 20354f42e4bSPeter Wemm size = uap->len; 204df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 205df8bae1dSRodney W. Grimes flags = uap->flags; 20654f42e4bSPeter Wemm pos = uap->pos; 20754f42e4bSPeter Wemm 20854f42e4bSPeter Wemm /* make sure mapping fits into numeric range etc */ 209fc565456SDmitrij Tejblum if ((ssize_t) uap->len < 0 || 21054f42e4bSPeter Wemm ((flags & MAP_ANON) && uap->fd != -1)) 211df8bae1dSRodney W. Grimes return (EINVAL); 2129154ee6aSPeter Wemm 2132267af78SJulian Elischer if (flags & MAP_STACK) { 2142267af78SJulian Elischer if ((uap->fd != -1) || 2152267af78SJulian Elischer ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE))) 2162267af78SJulian Elischer return (EINVAL); 2172267af78SJulian Elischer flags |= MAP_ANON; 2182267af78SJulian Elischer pos = 0; 2192907af2aSJulian Elischer } 2202907af2aSJulian Elischer 2219154ee6aSPeter Wemm /* 22254f42e4bSPeter Wemm * Align the file position to a page boundary, 22354f42e4bSPeter Wemm * and save its page offset component. 2249154ee6aSPeter Wemm */ 22554f42e4bSPeter Wemm pageoff = (pos & PAGE_MASK); 22654f42e4bSPeter Wemm pos -= pageoff; 22754f42e4bSPeter Wemm 22854f42e4bSPeter Wemm /* Adjust size for rounding (on both ends). */ 22954f42e4bSPeter Wemm size += pageoff; /* low end... */ 23054f42e4bSPeter Wemm size = (vm_size_t) round_page(size); /* hi end */ 2319154ee6aSPeter Wemm 232df8bae1dSRodney W. Grimes /* 2330d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 2340d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 235df8bae1dSRodney W. Grimes */ 236df8bae1dSRodney W. Grimes if (flags & MAP_FIXED) { 23754f42e4bSPeter Wemm /* 23854f42e4bSPeter Wemm * The specified address must have the same remainder 23954f42e4bSPeter Wemm * as the file offset taken modulo PAGE_SIZE, so it 24054f42e4bSPeter Wemm * should be aligned after adjustment by pageoff. 24154f42e4bSPeter Wemm */ 24254f42e4bSPeter Wemm addr -= pageoff; 24354f42e4bSPeter Wemm if (addr & PAGE_MASK) 24454f42e4bSPeter Wemm return (EINVAL); 24554f42e4bSPeter Wemm /* Address range must be all in user VM space. */ 246bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 247df8bae1dSRodney W. Grimes return (EINVAL); 24826f9a767SRodney W. Grimes #ifndef i386 249df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 250df8bae1dSRodney W. Grimes return (EINVAL); 25126f9a767SRodney W. Grimes #endif 252bbc0ec52SDavid Greenman if (addr + size < addr) 253df8bae1dSRodney W. Grimes return (EINVAL); 254df8bae1dSRodney W. Grimes } 255df8bae1dSRodney W. Grimes /* 25654f42e4bSPeter Wemm * XXX for non-fixed mappings where no hint is provided or 25754f42e4bSPeter Wemm * the hint would fall in the potential heap space, 25854f42e4bSPeter Wemm * place it after the end of the largest possible heap. 259df8bae1dSRodney W. Grimes * 26054f42e4bSPeter Wemm * There should really be a pmap call to determine a reasonable 26154f42e4bSPeter Wemm * location. 262df8bae1dSRodney W. Grimes */ 263d28ab90fSLuoqi Chen else if (addr == 0 || 2641f6889a1SMatthew Dillon (addr >= round_page((vm_offset_t)vms->vm_taddr) && 2651f6889a1SMatthew Dillon addr < round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ))) 2661f6889a1SMatthew Dillon addr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ); 26754f42e4bSPeter Wemm 268df8bae1dSRodney W. Grimes if (flags & MAP_ANON) { 269df8bae1dSRodney W. Grimes /* 270df8bae1dSRodney W. Grimes * Mapping blank space is trivial. 271df8bae1dSRodney W. Grimes */ 272df8bae1dSRodney W. Grimes handle = NULL; 273df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 27454f42e4bSPeter Wemm pos = 0; 275df8bae1dSRodney W. Grimes } else { 276df8bae1dSRodney W. Grimes /* 2770d94caffSDavid Greenman * Mapping file, get fp for validation. Obtain vnode and make 2780d94caffSDavid Greenman * sure it is of appropriate type. 279df8bae1dSRodney W. Grimes */ 280df8bae1dSRodney W. Grimes if (((unsigned) uap->fd) >= fdp->fd_nfiles || 281df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL) 282df8bae1dSRodney W. Grimes return (EBADF); 283df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_VNODE) 284df8bae1dSRodney W. Grimes return (EINVAL); 285aa543039SGarrett Wollman /* 286aa543039SGarrett Wollman * POSIX shared-memory objects are defined to have 287aa543039SGarrett Wollman * kernel persistence, and are not defined to support 288aa543039SGarrett Wollman * read(2)/write(2) -- or even open(2). Thus, we can 289aa543039SGarrett Wollman * use MAP_ASYNC to trade on-disk coherence for speed. 290aa543039SGarrett Wollman * The shm_open(3) library routine turns on the FPOSIXSHM 291aa543039SGarrett Wollman * flag to request this behavior. 292aa543039SGarrett Wollman */ 293aa543039SGarrett Wollman if (fp->f_flag & FPOSIXSHM) 294aa543039SGarrett Wollman flags |= MAP_NOSYNC; 295df8bae1dSRodney W. Grimes vp = (struct vnode *) fp->f_data; 296df8bae1dSRodney W. Grimes if (vp->v_type != VREG && vp->v_type != VCHR) 297df8bae1dSRodney W. Grimes return (EINVAL); 298df8bae1dSRodney W. Grimes /* 2990d94caffSDavid Greenman * XXX hack to handle use of /dev/zero to map anon memory (ala 3000d94caffSDavid Greenman * SunOS). 301df8bae1dSRodney W. Grimes */ 302df8bae1dSRodney W. Grimes if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) { 303df8bae1dSRodney W. Grimes handle = NULL; 304df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 305df8bae1dSRodney W. Grimes flags |= MAP_ANON; 30654f42e4bSPeter Wemm pos = 0; 307df8bae1dSRodney W. Grimes } else { 308df8bae1dSRodney W. Grimes /* 309c8bdd56bSGuido van Rooij * cdevs does not provide private mappings of any kind. 310c8bdd56bSGuido van Rooij */ 311c8bdd56bSGuido van Rooij /* 312c8bdd56bSGuido van Rooij * However, for XIG X server to continue to work, 313c8bdd56bSGuido van Rooij * we should allow the superuser to do it anyway. 314c8bdd56bSGuido van Rooij * We only allow it at securelevel < 1. 315c8bdd56bSGuido van Rooij * (Because the XIG X server writes directly to video 316c8bdd56bSGuido van Rooij * memory via /dev/mem, it should never work at any 317c8bdd56bSGuido van Rooij * other securelevel. 318c8bdd56bSGuido van Rooij * XXX this will have to go 319c8bdd56bSGuido van Rooij */ 320c8bdd56bSGuido van Rooij if (securelevel >= 1) 321c8bdd56bSGuido van Rooij disablexworkaround = 1; 322c8bdd56bSGuido van Rooij else 323f711d546SPoul-Henning Kamp disablexworkaround = suser(p); 324c8bdd56bSGuido van Rooij if (vp->v_type == VCHR && disablexworkaround && 325c8bdd56bSGuido van Rooij (flags & (MAP_PRIVATE|MAP_COPY))) 326c8bdd56bSGuido van Rooij return (EINVAL); 327c8bdd56bSGuido van Rooij /* 328df8bae1dSRodney W. Grimes * Ensure that file and memory protections are 329df8bae1dSRodney W. Grimes * compatible. Note that we only worry about 330df8bae1dSRodney W. Grimes * writability if mapping is shared; in this case, 331df8bae1dSRodney W. Grimes * current and max prot are dictated by the open file. 332df8bae1dSRodney W. Grimes * XXX use the vnode instead? Problem is: what 3330d94caffSDavid Greenman * credentials do we use for determination? What if 3340d94caffSDavid Greenman * proc does a setuid? 335df8bae1dSRodney W. Grimes */ 336df8bae1dSRodney W. Grimes maxprot = VM_PROT_EXECUTE; /* ??? */ 337df8bae1dSRodney W. Grimes if (fp->f_flag & FREAD) 338df8bae1dSRodney W. Grimes maxprot |= VM_PROT_READ; 339df8bae1dSRodney W. Grimes else if (prot & PROT_READ) 340df8bae1dSRodney W. Grimes return (EACCES); 341c8bdd56bSGuido van Rooij /* 342c8bdd56bSGuido van Rooij * If we are sharing potential changes (either via 343c8bdd56bSGuido van Rooij * MAP_SHARED or via the implicit sharing of character 344c8bdd56bSGuido van Rooij * device mappings), and we are trying to get write 345c8bdd56bSGuido van Rooij * permission although we opened it without asking 346c8bdd56bSGuido van Rooij * for it, bail out. Check for superuser, only if 347c8bdd56bSGuido van Rooij * we're at securelevel < 1, to allow the XIG X server 348c8bdd56bSGuido van Rooij * to continue to work. 349c8bdd56bSGuido van Rooij */ 35005feb99fSGuido van Rooij 35105feb99fSGuido van Rooij if ((flags & MAP_SHARED) != 0 || 35205feb99fSGuido van Rooij (vp->v_type == VCHR && disablexworkaround)) { 35305feb99fSGuido van Rooij if ((fp->f_flag & FWRITE) != 0) { 3544183b6b6SPeter Wemm struct vattr va; 35505feb99fSGuido van Rooij if ((error = 35605feb99fSGuido van Rooij VOP_GETATTR(vp, &va, 35705feb99fSGuido van Rooij p->p_ucred, p))) 35805feb99fSGuido van Rooij return (error); 35905feb99fSGuido van Rooij if ((va.va_flags & 36005feb99fSGuido van Rooij (IMMUTABLE|APPEND)) == 0) 361df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 36205feb99fSGuido van Rooij else if (prot & PROT_WRITE) 36305feb99fSGuido van Rooij return (EPERM); 36405feb99fSGuido van Rooij } else if ((prot & PROT_WRITE) != 0) 36505feb99fSGuido van Rooij return (EACCES); 36605feb99fSGuido van Rooij } else 36705feb99fSGuido van Rooij maxprot |= VM_PROT_WRITE; 36805feb99fSGuido van Rooij 369651bb817SAlexander Langer handle = (void *)vp; 370df8bae1dSRodney W. Grimes } 371df8bae1dSRodney W. Grimes } 3721f6889a1SMatthew Dillon 3731f6889a1SMatthew Dillon /* 3741f6889a1SMatthew Dillon * Do not allow more then a certain number of vm_map_entry structures 3751f6889a1SMatthew Dillon * per process. Scale with the number of rforks sharing the map 3761f6889a1SMatthew Dillon * to make the limit reasonable for threads. 3771f6889a1SMatthew Dillon */ 3781f6889a1SMatthew Dillon if (max_proc_mmap && 3791f6889a1SMatthew Dillon vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) { 3801f6889a1SMatthew Dillon return (ENOMEM); 3811f6889a1SMatthew Dillon } 3821f6889a1SMatthew Dillon 3831f6889a1SMatthew Dillon error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot, 38454f42e4bSPeter Wemm flags, handle, pos); 385df8bae1dSRodney W. Grimes if (error == 0) 386711458e3SDoug Rabson p->p_retval[0] = (register_t) (addr + pageoff); 387df8bae1dSRodney W. Grimes return (error); 388df8bae1dSRodney W. Grimes } 389df8bae1dSRodney W. Grimes 39005f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43 391d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 39205f0fdd2SPoul-Henning Kamp struct ommap_args { 39305f0fdd2SPoul-Henning Kamp caddr_t addr; 39405f0fdd2SPoul-Henning Kamp int len; 39505f0fdd2SPoul-Henning Kamp int prot; 39605f0fdd2SPoul-Henning Kamp int flags; 39705f0fdd2SPoul-Henning Kamp int fd; 39805f0fdd2SPoul-Henning Kamp long pos; 39905f0fdd2SPoul-Henning Kamp }; 400d2d3e875SBruce Evans #endif 40105f0fdd2SPoul-Henning Kamp int 402cb226aaaSPoul-Henning Kamp ommap(p, uap) 40305f0fdd2SPoul-Henning Kamp struct proc *p; 40405f0fdd2SPoul-Henning Kamp register struct ommap_args *uap; 40505f0fdd2SPoul-Henning Kamp { 40605f0fdd2SPoul-Henning Kamp struct mmap_args nargs; 40705f0fdd2SPoul-Henning Kamp static const char cvtbsdprot[8] = { 40805f0fdd2SPoul-Henning Kamp 0, 40905f0fdd2SPoul-Henning Kamp PROT_EXEC, 41005f0fdd2SPoul-Henning Kamp PROT_WRITE, 41105f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE, 41205f0fdd2SPoul-Henning Kamp PROT_READ, 41305f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_READ, 41405f0fdd2SPoul-Henning Kamp PROT_WRITE | PROT_READ, 41505f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE | PROT_READ, 41605f0fdd2SPoul-Henning Kamp }; 4170d94caffSDavid Greenman 41805f0fdd2SPoul-Henning Kamp #define OMAP_ANON 0x0002 41905f0fdd2SPoul-Henning Kamp #define OMAP_COPY 0x0020 42005f0fdd2SPoul-Henning Kamp #define OMAP_SHARED 0x0010 42105f0fdd2SPoul-Henning Kamp #define OMAP_FIXED 0x0100 42205f0fdd2SPoul-Henning Kamp #define OMAP_INHERIT 0x0800 42305f0fdd2SPoul-Henning Kamp 42405f0fdd2SPoul-Henning Kamp nargs.addr = uap->addr; 42505f0fdd2SPoul-Henning Kamp nargs.len = uap->len; 42605f0fdd2SPoul-Henning Kamp nargs.prot = cvtbsdprot[uap->prot & 0x7]; 42705f0fdd2SPoul-Henning Kamp nargs.flags = 0; 42805f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_ANON) 42905f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_ANON; 43005f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_COPY) 43105f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_COPY; 43205f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_SHARED) 43305f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_SHARED; 43405f0fdd2SPoul-Henning Kamp else 43505f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_PRIVATE; 43605f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_FIXED) 43705f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_FIXED; 43805f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_INHERIT) 43905f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_INHERIT; 44005f0fdd2SPoul-Henning Kamp nargs.fd = uap->fd; 44105f0fdd2SPoul-Henning Kamp nargs.pos = uap->pos; 442cb226aaaSPoul-Henning Kamp return (mmap(p, &nargs)); 44305f0fdd2SPoul-Henning Kamp } 44405f0fdd2SPoul-Henning Kamp #endif /* COMPAT_43 */ 44505f0fdd2SPoul-Henning Kamp 44605f0fdd2SPoul-Henning Kamp 447d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 448df8bae1dSRodney W. Grimes struct msync_args { 449651bb817SAlexander Langer void *addr; 450df8bae1dSRodney W. Grimes int len; 451e6c6af11SDavid Greenman int flags; 452df8bae1dSRodney W. Grimes }; 453d2d3e875SBruce Evans #endif 454df8bae1dSRodney W. Grimes int 455cb226aaaSPoul-Henning Kamp msync(p, uap) 456df8bae1dSRodney W. Grimes struct proc *p; 457df8bae1dSRodney W. Grimes struct msync_args *uap; 458df8bae1dSRodney W. Grimes { 459df8bae1dSRodney W. Grimes vm_offset_t addr; 460dabee6feSPeter Wemm vm_size_t size, pageoff; 461e6c6af11SDavid Greenman int flags; 462df8bae1dSRodney W. Grimes vm_map_t map; 463df8bae1dSRodney W. Grimes int rv; 464df8bae1dSRodney W. Grimes 465df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 4669154ee6aSPeter Wemm size = uap->len; 467e6c6af11SDavid Greenman flags = uap->flags; 468e6c6af11SDavid Greenman 469dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 470dabee6feSPeter Wemm addr -= pageoff; 471dabee6feSPeter Wemm size += pageoff; 472dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 4739154ee6aSPeter Wemm if (addr + size < addr) 474dabee6feSPeter Wemm return(EINVAL); 475dabee6feSPeter Wemm 476dabee6feSPeter Wemm if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 4771e62bc63SDavid Greenman return (EINVAL); 4781e62bc63SDavid Greenman 4799154ee6aSPeter Wemm map = &p->p_vmspace->vm_map; 4809154ee6aSPeter Wemm 481df8bae1dSRodney W. Grimes /* 482df8bae1dSRodney W. Grimes * XXX Gak! If size is zero we are supposed to sync "all modified 4830d94caffSDavid Greenman * pages with the region containing addr". Unfortunately, we don't 4840d94caffSDavid Greenman * really keep track of individual mmaps so we approximate by flushing 4850d94caffSDavid Greenman * the range of the map entry containing addr. This can be incorrect 4860d94caffSDavid Greenman * if the region splits or is coalesced with a neighbor. 487df8bae1dSRodney W. Grimes */ 488df8bae1dSRodney W. Grimes if (size == 0) { 489df8bae1dSRodney W. Grimes vm_map_entry_t entry; 490df8bae1dSRodney W. Grimes 491df8bae1dSRodney W. Grimes vm_map_lock_read(map); 492df8bae1dSRodney W. Grimes rv = vm_map_lookup_entry(map, addr, &entry); 493df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 494fbcfcdf7SDavid Greenman if (rv == FALSE) 495df8bae1dSRodney W. Grimes return (EINVAL); 496df8bae1dSRodney W. Grimes addr = entry->start; 497df8bae1dSRodney W. Grimes size = entry->end - entry->start; 498df8bae1dSRodney W. Grimes } 499e6c6af11SDavid Greenman 500df8bae1dSRodney W. Grimes /* 501df8bae1dSRodney W. Grimes * Clean the pages and interpret the return value. 502df8bae1dSRodney W. Grimes */ 5036c534ad8SDavid Greenman rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 504e6c6af11SDavid Greenman (flags & MS_INVALIDATE) != 0); 505e6c6af11SDavid Greenman 506df8bae1dSRodney W. Grimes switch (rv) { 507df8bae1dSRodney W. Grimes case KERN_SUCCESS: 508df8bae1dSRodney W. Grimes break; 509df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 510df8bae1dSRodney W. Grimes return (EINVAL); /* Sun returns ENOMEM? */ 511df8bae1dSRodney W. Grimes case KERN_FAILURE: 512df8bae1dSRodney W. Grimes return (EIO); 513df8bae1dSRodney W. Grimes default: 514df8bae1dSRodney W. Grimes return (EINVAL); 515df8bae1dSRodney W. Grimes } 516e6c6af11SDavid Greenman 517df8bae1dSRodney W. Grimes return (0); 518df8bae1dSRodney W. Grimes } 519df8bae1dSRodney W. Grimes 520d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 521df8bae1dSRodney W. Grimes struct munmap_args { 522651bb817SAlexander Langer void *addr; 5239154ee6aSPeter Wemm size_t len; 524df8bae1dSRodney W. Grimes }; 525d2d3e875SBruce Evans #endif 526df8bae1dSRodney W. Grimes int 527cb226aaaSPoul-Henning Kamp munmap(p, uap) 528df8bae1dSRodney W. Grimes register struct proc *p; 529df8bae1dSRodney W. Grimes register struct munmap_args *uap; 530df8bae1dSRodney W. Grimes { 531df8bae1dSRodney W. Grimes vm_offset_t addr; 532dabee6feSPeter Wemm vm_size_t size, pageoff; 533df8bae1dSRodney W. Grimes vm_map_t map; 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 5369154ee6aSPeter Wemm size = uap->len; 537dabee6feSPeter Wemm 538dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 539dabee6feSPeter Wemm addr -= pageoff; 540dabee6feSPeter Wemm size += pageoff; 541dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 5429154ee6aSPeter Wemm if (addr + size < addr) 543df8bae1dSRodney W. Grimes return(EINVAL); 5449154ee6aSPeter Wemm 545df8bae1dSRodney W. Grimes if (size == 0) 546df8bae1dSRodney W. Grimes return (0); 547dabee6feSPeter Wemm 548df8bae1dSRodney W. Grimes /* 5490d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 5500d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 551df8bae1dSRodney W. Grimes */ 552bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 553df8bae1dSRodney W. Grimes return (EINVAL); 55426f9a767SRodney W. Grimes #ifndef i386 555df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 556df8bae1dSRodney W. Grimes return (EINVAL); 55726f9a767SRodney W. Grimes #endif 558df8bae1dSRodney W. Grimes map = &p->p_vmspace->vm_map; 559df8bae1dSRodney W. Grimes /* 560df8bae1dSRodney W. Grimes * Make sure entire range is allocated. 561df8bae1dSRodney W. Grimes */ 562df8bae1dSRodney W. Grimes if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) 563df8bae1dSRodney W. Grimes return (EINVAL); 564df8bae1dSRodney W. Grimes /* returns nothing but KERN_SUCCESS anyway */ 565df8bae1dSRodney W. Grimes (void) vm_map_remove(map, addr, addr + size); 566df8bae1dSRodney W. Grimes return (0); 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes 569df8bae1dSRodney W. Grimes void 57090324b07SDavid Greenman munmapfd(p, fd) 57190324b07SDavid Greenman struct proc *p; 572df8bae1dSRodney W. Grimes int fd; 573df8bae1dSRodney W. Grimes { 574df8bae1dSRodney W. Grimes /* 575c4ed5a07SDavid Greenman * XXX should unmap any regions mapped to this file 576df8bae1dSRodney W. Grimes */ 57790324b07SDavid Greenman p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED; 578df8bae1dSRodney W. Grimes } 579df8bae1dSRodney W. Grimes 580d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 581df8bae1dSRodney W. Grimes struct mprotect_args { 582651bb817SAlexander Langer const void *addr; 5839154ee6aSPeter Wemm size_t len; 584df8bae1dSRodney W. Grimes int prot; 585df8bae1dSRodney W. Grimes }; 586d2d3e875SBruce Evans #endif 587df8bae1dSRodney W. Grimes int 588cb226aaaSPoul-Henning Kamp mprotect(p, uap) 589df8bae1dSRodney W. Grimes struct proc *p; 590df8bae1dSRodney W. Grimes struct mprotect_args *uap; 591df8bae1dSRodney W. Grimes { 592df8bae1dSRodney W. Grimes vm_offset_t addr; 593dabee6feSPeter Wemm vm_size_t size, pageoff; 594df8bae1dSRodney W. Grimes register vm_prot_t prot; 595df8bae1dSRodney W. Grimes 596df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 5979154ee6aSPeter Wemm size = uap->len; 598df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 599d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 600d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 601d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 602d0aea04fSJohn Dyson #endif 603df8bae1dSRodney W. Grimes 604dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 605dabee6feSPeter Wemm addr -= pageoff; 606dabee6feSPeter Wemm size += pageoff; 607dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 6089154ee6aSPeter Wemm if (addr + size < addr) 609dabee6feSPeter Wemm return(EINVAL); 610dabee6feSPeter Wemm 611df8bae1dSRodney W. Grimes switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot, 612df8bae1dSRodney W. Grimes FALSE)) { 613df8bae1dSRodney W. Grimes case KERN_SUCCESS: 614df8bae1dSRodney W. Grimes return (0); 615df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 616df8bae1dSRodney W. Grimes return (EACCES); 617df8bae1dSRodney W. Grimes } 618df8bae1dSRodney W. Grimes return (EINVAL); 619df8bae1dSRodney W. Grimes } 620df8bae1dSRodney W. Grimes 621d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 622dabee6feSPeter Wemm struct minherit_args { 623651bb817SAlexander Langer void *addr; 6249154ee6aSPeter Wemm size_t len; 625dabee6feSPeter Wemm int inherit; 626dabee6feSPeter Wemm }; 627dabee6feSPeter Wemm #endif 628dabee6feSPeter Wemm int 629cb226aaaSPoul-Henning Kamp minherit(p, uap) 630dabee6feSPeter Wemm struct proc *p; 631dabee6feSPeter Wemm struct minherit_args *uap; 632dabee6feSPeter Wemm { 633dabee6feSPeter Wemm vm_offset_t addr; 634dabee6feSPeter Wemm vm_size_t size, pageoff; 635dabee6feSPeter Wemm register vm_inherit_t inherit; 636dabee6feSPeter Wemm 637dabee6feSPeter Wemm addr = (vm_offset_t)uap->addr; 6389154ee6aSPeter Wemm size = uap->len; 639dabee6feSPeter Wemm inherit = uap->inherit; 640dabee6feSPeter Wemm 641dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 642dabee6feSPeter Wemm addr -= pageoff; 643dabee6feSPeter Wemm size += pageoff; 644dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 6459154ee6aSPeter Wemm if (addr + size < addr) 646dabee6feSPeter Wemm return(EINVAL); 647dabee6feSPeter Wemm 648dabee6feSPeter Wemm switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size, 649dabee6feSPeter Wemm inherit)) { 650dabee6feSPeter Wemm case KERN_SUCCESS: 651dabee6feSPeter Wemm return (0); 652dabee6feSPeter Wemm case KERN_PROTECTION_FAILURE: 653dabee6feSPeter Wemm return (EACCES); 654dabee6feSPeter Wemm } 655dabee6feSPeter Wemm return (EINVAL); 656dabee6feSPeter Wemm } 657dabee6feSPeter Wemm 658dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 659df8bae1dSRodney W. Grimes struct madvise_args { 660651bb817SAlexander Langer void *addr; 6619154ee6aSPeter Wemm size_t len; 662df8bae1dSRodney W. Grimes int behav; 663df8bae1dSRodney W. Grimes }; 664d2d3e875SBruce Evans #endif 6650d94caffSDavid Greenman 666df8bae1dSRodney W. Grimes /* ARGSUSED */ 667df8bae1dSRodney W. Grimes int 668cb226aaaSPoul-Henning Kamp madvise(p, uap) 669df8bae1dSRodney W. Grimes struct proc *p; 670df8bae1dSRodney W. Grimes struct madvise_args *uap; 671df8bae1dSRodney W. Grimes { 672f35329acSJohn Dyson vm_offset_t start, end; 673b4309055SMatthew Dillon 674b4309055SMatthew Dillon /* 675b4309055SMatthew Dillon * Check for illegal behavior 676b4309055SMatthew Dillon */ 6779730a5daSPaul Saab if (uap->behav < 0 || uap->behav > MADV_CORE) 678b4309055SMatthew Dillon return (EINVAL); 679867a482dSJohn Dyson /* 680867a482dSJohn Dyson * Check for illegal addresses. Watch out for address wrap... Note 681867a482dSJohn Dyson * that VM_*_ADDRESS are not constants due to casts (argh). 682867a482dSJohn Dyson */ 683867a482dSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && 684867a482dSJohn Dyson ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS) 685867a482dSJohn Dyson return (EINVAL); 686867a482dSJohn Dyson #ifndef i386 687867a482dSJohn Dyson if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS) 688867a482dSJohn Dyson return (EINVAL); 689867a482dSJohn Dyson #endif 690867a482dSJohn Dyson if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr) 691867a482dSJohn Dyson return (EINVAL); 692867a482dSJohn Dyson 693867a482dSJohn Dyson /* 694867a482dSJohn Dyson * Since this routine is only advisory, we default to conservative 695867a482dSJohn Dyson * behavior. 696867a482dSJohn Dyson */ 697cd6eea25SDavid Greenman start = trunc_page((vm_offset_t) uap->addr); 698cd6eea25SDavid Greenman end = round_page((vm_offset_t) uap->addr + uap->len); 699867a482dSJohn Dyson 700b4309055SMatthew Dillon if (vm_map_madvise(&p->p_vmspace->vm_map, start, end, uap->behav)) 701b4309055SMatthew Dillon return (EINVAL); 702867a482dSJohn Dyson return (0); 703df8bae1dSRodney W. Grimes } 704df8bae1dSRodney W. Grimes 705d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 706df8bae1dSRodney W. Grimes struct mincore_args { 707651bb817SAlexander Langer const void *addr; 7089154ee6aSPeter Wemm size_t len; 709df8bae1dSRodney W. Grimes char *vec; 710df8bae1dSRodney W. Grimes }; 711d2d3e875SBruce Evans #endif 7120d94caffSDavid Greenman 713df8bae1dSRodney W. Grimes /* ARGSUSED */ 714df8bae1dSRodney W. Grimes int 715cb226aaaSPoul-Henning Kamp mincore(p, uap) 716df8bae1dSRodney W. Grimes struct proc *p; 717df8bae1dSRodney W. Grimes struct mincore_args *uap; 718df8bae1dSRodney W. Grimes { 719867a482dSJohn Dyson vm_offset_t addr, first_addr; 720867a482dSJohn Dyson vm_offset_t end, cend; 721867a482dSJohn Dyson pmap_t pmap; 722867a482dSJohn Dyson vm_map_t map; 72302c04a2fSJohn Dyson char *vec; 724867a482dSJohn Dyson int error; 725867a482dSJohn Dyson int vecindex, lastvecindex; 726867a482dSJohn Dyson register vm_map_entry_t current; 727867a482dSJohn Dyson vm_map_entry_t entry; 728867a482dSJohn Dyson int mincoreinfo; 729dd2622a8SAlan Cox unsigned int timestamp; 730df8bae1dSRodney W. Grimes 731867a482dSJohn Dyson /* 732867a482dSJohn Dyson * Make sure that the addresses presented are valid for user 733867a482dSJohn Dyson * mode. 734867a482dSJohn Dyson */ 735867a482dSJohn Dyson first_addr = addr = trunc_page((vm_offset_t) uap->addr); 7369154ee6aSPeter Wemm end = addr + (vm_size_t)round_page(uap->len); 73702c04a2fSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS) 73802c04a2fSJohn Dyson return (EINVAL); 73902c04a2fSJohn Dyson if (end < addr) 74002c04a2fSJohn Dyson return (EINVAL); 74102c04a2fSJohn Dyson 742867a482dSJohn Dyson /* 743867a482dSJohn Dyson * Address of byte vector 744867a482dSJohn Dyson */ 74502c04a2fSJohn Dyson vec = uap->vec; 746867a482dSJohn Dyson 747867a482dSJohn Dyson map = &p->p_vmspace->vm_map; 748b1028ad1SLuoqi Chen pmap = vmspace_pmap(p->p_vmspace); 749867a482dSJohn Dyson 750eff50fcdSAlan Cox vm_map_lock_read(map); 751dd2622a8SAlan Cox RestartScan: 752dd2622a8SAlan Cox timestamp = map->timestamp; 753867a482dSJohn Dyson 754867a482dSJohn Dyson if (!vm_map_lookup_entry(map, addr, &entry)) 755867a482dSJohn Dyson entry = entry->next; 756867a482dSJohn Dyson 757867a482dSJohn Dyson /* 758867a482dSJohn Dyson * Do this on a map entry basis so that if the pages are not 759867a482dSJohn Dyson * in the current processes address space, we can easily look 760867a482dSJohn Dyson * up the pages elsewhere. 761867a482dSJohn Dyson */ 762867a482dSJohn Dyson lastvecindex = -1; 763867a482dSJohn Dyson for(current = entry; 764867a482dSJohn Dyson (current != &map->header) && (current->start < end); 765867a482dSJohn Dyson current = current->next) { 766867a482dSJohn Dyson 767867a482dSJohn Dyson /* 768867a482dSJohn Dyson * ignore submaps (for now) or null objects 769867a482dSJohn Dyson */ 7709fdfe602SMatthew Dillon if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) || 771867a482dSJohn Dyson current->object.vm_object == NULL) 772867a482dSJohn Dyson continue; 773867a482dSJohn Dyson 774867a482dSJohn Dyson /* 775867a482dSJohn Dyson * limit this scan to the current map entry and the 776867a482dSJohn Dyson * limits for the mincore call 777867a482dSJohn Dyson */ 778867a482dSJohn Dyson if (addr < current->start) 779867a482dSJohn Dyson addr = current->start; 780867a482dSJohn Dyson cend = current->end; 781867a482dSJohn Dyson if (cend > end) 782867a482dSJohn Dyson cend = end; 783867a482dSJohn Dyson 784867a482dSJohn Dyson /* 785867a482dSJohn Dyson * scan this entry one page at a time 786867a482dSJohn Dyson */ 787867a482dSJohn Dyson while(addr < cend) { 788867a482dSJohn Dyson /* 789867a482dSJohn Dyson * Check pmap first, it is likely faster, also 790867a482dSJohn Dyson * it can provide info as to whether we are the 791867a482dSJohn Dyson * one referencing or modifying the page. 792867a482dSJohn Dyson */ 793867a482dSJohn Dyson mincoreinfo = pmap_mincore(pmap, addr); 794867a482dSJohn Dyson if (!mincoreinfo) { 795867a482dSJohn Dyson vm_pindex_t pindex; 796867a482dSJohn Dyson vm_ooffset_t offset; 797867a482dSJohn Dyson vm_page_t m; 798867a482dSJohn Dyson /* 799867a482dSJohn Dyson * calculate the page index into the object 800867a482dSJohn Dyson */ 801867a482dSJohn Dyson offset = current->offset + (addr - current->start); 802867a482dSJohn Dyson pindex = OFF_TO_IDX(offset); 803867a482dSJohn Dyson m = vm_page_lookup(current->object.vm_object, 804867a482dSJohn Dyson pindex); 805867a482dSJohn Dyson /* 806867a482dSJohn Dyson * if the page is resident, then gather information about 807867a482dSJohn Dyson * it. 808867a482dSJohn Dyson */ 809867a482dSJohn Dyson if (m) { 810867a482dSJohn Dyson mincoreinfo = MINCORE_INCORE; 811867a482dSJohn Dyson if (m->dirty || 8120385347cSPeter Wemm pmap_is_modified(m)) 813867a482dSJohn Dyson mincoreinfo |= MINCORE_MODIFIED_OTHER; 814867a482dSJohn Dyson if ((m->flags & PG_REFERENCED) || 8150385347cSPeter Wemm pmap_ts_referenced(m)) { 816e69763a3SDoug Rabson vm_page_flag_set(m, PG_REFERENCED); 817867a482dSJohn Dyson mincoreinfo |= MINCORE_REFERENCED_OTHER; 81802c04a2fSJohn Dyson } 819867a482dSJohn Dyson } 8209b5a5d81SJohn Dyson } 821867a482dSJohn Dyson 822867a482dSJohn Dyson /* 823dd2622a8SAlan Cox * subyte may page fault. In case it needs to modify 824dd2622a8SAlan Cox * the map, we release the lock. 825dd2622a8SAlan Cox */ 826dd2622a8SAlan Cox vm_map_unlock_read(map); 827dd2622a8SAlan Cox 828dd2622a8SAlan Cox /* 829867a482dSJohn Dyson * calculate index into user supplied byte vector 830867a482dSJohn Dyson */ 831867a482dSJohn Dyson vecindex = OFF_TO_IDX(addr - first_addr); 832867a482dSJohn Dyson 833867a482dSJohn Dyson /* 834867a482dSJohn Dyson * If we have skipped map entries, we need to make sure that 835867a482dSJohn Dyson * the byte vector is zeroed for those skipped entries. 836867a482dSJohn Dyson */ 837867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 838867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 839867a482dSJohn Dyson if (error) { 840867a482dSJohn Dyson return (EFAULT); 841867a482dSJohn Dyson } 842867a482dSJohn Dyson ++lastvecindex; 843867a482dSJohn Dyson } 844867a482dSJohn Dyson 845867a482dSJohn Dyson /* 846867a482dSJohn Dyson * Pass the page information to the user 847867a482dSJohn Dyson */ 848867a482dSJohn Dyson error = subyte( vec + vecindex, mincoreinfo); 849867a482dSJohn Dyson if (error) { 850867a482dSJohn Dyson return (EFAULT); 851867a482dSJohn Dyson } 852dd2622a8SAlan Cox 853dd2622a8SAlan Cox /* 854dd2622a8SAlan Cox * If the map has changed, due to the subyte, the previous 855dd2622a8SAlan Cox * output may be invalid. 856dd2622a8SAlan Cox */ 857dd2622a8SAlan Cox vm_map_lock_read(map); 858dd2622a8SAlan Cox if (timestamp != map->timestamp) 859dd2622a8SAlan Cox goto RestartScan; 860dd2622a8SAlan Cox 861867a482dSJohn Dyson lastvecindex = vecindex; 86202c04a2fSJohn Dyson addr += PAGE_SIZE; 86302c04a2fSJohn Dyson } 864867a482dSJohn Dyson } 865867a482dSJohn Dyson 866867a482dSJohn Dyson /* 867dd2622a8SAlan Cox * subyte may page fault. In case it needs to modify 868dd2622a8SAlan Cox * the map, we release the lock. 869dd2622a8SAlan Cox */ 870dd2622a8SAlan Cox vm_map_unlock_read(map); 871dd2622a8SAlan Cox 872dd2622a8SAlan Cox /* 873867a482dSJohn Dyson * Zero the last entries in the byte vector. 874867a482dSJohn Dyson */ 875867a482dSJohn Dyson vecindex = OFF_TO_IDX(end - first_addr); 876867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 877867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 878867a482dSJohn Dyson if (error) { 879867a482dSJohn Dyson return (EFAULT); 880867a482dSJohn Dyson } 881867a482dSJohn Dyson ++lastvecindex; 882867a482dSJohn Dyson } 883867a482dSJohn Dyson 884dd2622a8SAlan Cox /* 885dd2622a8SAlan Cox * If the map has changed, due to the subyte, the previous 886dd2622a8SAlan Cox * output may be invalid. 887dd2622a8SAlan Cox */ 888dd2622a8SAlan Cox vm_map_lock_read(map); 889dd2622a8SAlan Cox if (timestamp != map->timestamp) 890dd2622a8SAlan Cox goto RestartScan; 891eff50fcdSAlan Cox vm_map_unlock_read(map); 892dd2622a8SAlan Cox 89302c04a2fSJohn Dyson return (0); 894df8bae1dSRodney W. Grimes } 895df8bae1dSRodney W. Grimes 896d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 897df8bae1dSRodney W. Grimes struct mlock_args { 898651bb817SAlexander Langer const void *addr; 899df8bae1dSRodney W. Grimes size_t len; 900df8bae1dSRodney W. Grimes }; 901d2d3e875SBruce Evans #endif 902df8bae1dSRodney W. Grimes int 903cb226aaaSPoul-Henning Kamp mlock(p, uap) 904df8bae1dSRodney W. Grimes struct proc *p; 905df8bae1dSRodney W. Grimes struct mlock_args *uap; 906df8bae1dSRodney W. Grimes { 907df8bae1dSRodney W. Grimes vm_offset_t addr; 908dabee6feSPeter Wemm vm_size_t size, pageoff; 909df8bae1dSRodney W. Grimes int error; 910df8bae1dSRodney W. Grimes 911df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 9129154ee6aSPeter Wemm size = uap->len; 9139154ee6aSPeter Wemm 914dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 915dabee6feSPeter Wemm addr -= pageoff; 916dabee6feSPeter Wemm size += pageoff; 917dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 918dabee6feSPeter Wemm 919dabee6feSPeter Wemm /* disable wrap around */ 9209154ee6aSPeter Wemm if (addr + size < addr) 921df8bae1dSRodney W. Grimes return (EINVAL); 922dabee6feSPeter Wemm 923df8bae1dSRodney W. Grimes if (atop(size) + cnt.v_wire_count > vm_page_max_wired) 924df8bae1dSRodney W. Grimes return (EAGAIN); 9259154ee6aSPeter Wemm 926df8bae1dSRodney W. Grimes #ifdef pmap_wired_count 927df8bae1dSRodney W. Grimes if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 928df8bae1dSRodney W. Grimes p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 9294a40e3d4SJohn Dyson return (ENOMEM); 930df8bae1dSRodney W. Grimes #else 931f711d546SPoul-Henning Kamp error = suser(p); 93205f0fdd2SPoul-Henning Kamp if (error) 933df8bae1dSRodney W. Grimes return (error); 934df8bae1dSRodney W. Grimes #endif 935df8bae1dSRodney W. Grimes 9367aaaa4fdSJohn Dyson error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE); 937df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 938df8bae1dSRodney W. Grimes } 939df8bae1dSRodney W. Grimes 940d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 9414a40e3d4SJohn Dyson struct mlockall_args { 9424a40e3d4SJohn Dyson int how; 9434a40e3d4SJohn Dyson }; 9444a40e3d4SJohn Dyson #endif 9454a40e3d4SJohn Dyson 9464a40e3d4SJohn Dyson int 947cb226aaaSPoul-Henning Kamp mlockall(p, uap) 9484a40e3d4SJohn Dyson struct proc *p; 9494a40e3d4SJohn Dyson struct mlockall_args *uap; 9504a40e3d4SJohn Dyson { 9514a40e3d4SJohn Dyson return 0; 9524a40e3d4SJohn Dyson } 9534a40e3d4SJohn Dyson 9544a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_ 9554a40e3d4SJohn Dyson struct mlockall_args { 9564a40e3d4SJohn Dyson int how; 9574a40e3d4SJohn Dyson }; 9584a40e3d4SJohn Dyson #endif 9594a40e3d4SJohn Dyson 9604a40e3d4SJohn Dyson int 961cb226aaaSPoul-Henning Kamp munlockall(p, uap) 9624a40e3d4SJohn Dyson struct proc *p; 9634a40e3d4SJohn Dyson struct munlockall_args *uap; 9644a40e3d4SJohn Dyson { 9654a40e3d4SJohn Dyson return 0; 9664a40e3d4SJohn Dyson } 9674a40e3d4SJohn Dyson 9684a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_ 969df8bae1dSRodney W. Grimes struct munlock_args { 970651bb817SAlexander Langer const void *addr; 971df8bae1dSRodney W. Grimes size_t len; 972df8bae1dSRodney W. Grimes }; 973d2d3e875SBruce Evans #endif 974df8bae1dSRodney W. Grimes int 975cb226aaaSPoul-Henning Kamp munlock(p, uap) 976df8bae1dSRodney W. Grimes struct proc *p; 977df8bae1dSRodney W. Grimes struct munlock_args *uap; 978df8bae1dSRodney W. Grimes { 979df8bae1dSRodney W. Grimes vm_offset_t addr; 980dabee6feSPeter Wemm vm_size_t size, pageoff; 981df8bae1dSRodney W. Grimes int error; 982df8bae1dSRodney W. Grimes 983df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 9849154ee6aSPeter Wemm size = uap->len; 9859154ee6aSPeter Wemm 986dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 987dabee6feSPeter Wemm addr -= pageoff; 988dabee6feSPeter Wemm size += pageoff; 989dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 990dabee6feSPeter Wemm 991dabee6feSPeter Wemm /* disable wrap around */ 9929154ee6aSPeter Wemm if (addr + size < addr) 993df8bae1dSRodney W. Grimes return (EINVAL); 994dabee6feSPeter Wemm 995df8bae1dSRodney W. Grimes #ifndef pmap_wired_count 996f711d546SPoul-Henning Kamp error = suser(p); 99705f0fdd2SPoul-Henning Kamp if (error) 998df8bae1dSRodney W. Grimes return (error); 999df8bae1dSRodney W. Grimes #endif 1000df8bae1dSRodney W. Grimes 10017aaaa4fdSJohn Dyson error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE); 1002df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 1003df8bae1dSRodney W. Grimes } 1004df8bae1dSRodney W. Grimes 1005df8bae1dSRodney W. Grimes /* 1006df8bae1dSRodney W. Grimes * Internal version of mmap. 1007df8bae1dSRodney W. Grimes * Currently used by mmap, exec, and sys5 shared memory. 1008df8bae1dSRodney W. Grimes * Handle is either a vnode pointer or NULL for MAP_ANON. 1009df8bae1dSRodney W. Grimes */ 1010df8bae1dSRodney W. Grimes int 1011b9dcd593SBruce Evans vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot, 1012b9dcd593SBruce Evans vm_prot_t maxprot, int flags, 1013651bb817SAlexander Langer void *handle, 1014b9dcd593SBruce Evans vm_ooffset_t foff) 1015df8bae1dSRodney W. Grimes { 1016df8bae1dSRodney W. Grimes boolean_t fitit; 1017fcae040bSJohn Dyson vm_object_t object; 1018df8bae1dSRodney W. Grimes struct vnode *vp = NULL; 101924a1cce3SDavid Greenman objtype_t type; 1020df8bae1dSRodney W. Grimes int rv = KERN_SUCCESS; 1021bd7e5f99SJohn Dyson vm_ooffset_t objsize; 1022bd7e5f99SJohn Dyson int docow; 102306cb7259SDavid Greenman struct proc *p = curproc; 1024df8bae1dSRodney W. Grimes 1025df8bae1dSRodney W. Grimes if (size == 0) 1026df8bae1dSRodney W. Grimes return (0); 1027df8bae1dSRodney W. Grimes 102806cb7259SDavid Greenman objsize = size = round_page(size); 1029df8bae1dSRodney W. Grimes 1030df8bae1dSRodney W. Grimes /* 1031bc9ad247SDavid Greenman * We currently can only deal with page aligned file offsets. 1032bc9ad247SDavid Greenman * The check is here rather than in the syscall because the 1033bc9ad247SDavid Greenman * kernel calls this function internally for other mmaping 1034bc9ad247SDavid Greenman * operations (such as in exec) and non-aligned offsets will 1035bc9ad247SDavid Greenman * cause pmap inconsistencies...so we want to be sure to 1036bc9ad247SDavid Greenman * disallow this in all cases. 1037bc9ad247SDavid Greenman */ 1038bc9ad247SDavid Greenman if (foff & PAGE_MASK) 1039bc9ad247SDavid Greenman return (EINVAL); 1040bc9ad247SDavid Greenman 104106cb7259SDavid Greenman if ((flags & MAP_FIXED) == 0) { 104206cb7259SDavid Greenman fitit = TRUE; 104306cb7259SDavid Greenman *addr = round_page(*addr); 104406cb7259SDavid Greenman } else { 104506cb7259SDavid Greenman if (*addr != trunc_page(*addr)) 104606cb7259SDavid Greenman return (EINVAL); 104706cb7259SDavid Greenman fitit = FALSE; 104806cb7259SDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 104906cb7259SDavid Greenman } 105006cb7259SDavid Greenman 1051bc9ad247SDavid Greenman /* 105224a1cce3SDavid Greenman * Lookup/allocate object. 1053df8bae1dSRodney W. Grimes */ 10545f55e841SDavid Greenman if (flags & MAP_ANON) { 1055851c12ffSJohn Dyson type = OBJT_DEFAULT; 10565f55e841SDavid Greenman /* 10575f55e841SDavid Greenman * Unnamed anonymous regions always start at 0. 10585f55e841SDavid Greenman */ 105967bf6868SJohn Dyson if (handle == 0) 10605f55e841SDavid Greenman foff = 0; 10615f55e841SDavid Greenman } else { 1062df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 1063df8bae1dSRodney W. Grimes if (vp->v_type == VCHR) { 106424a1cce3SDavid Greenman type = OBJT_DEVICE; 1065a23d65bfSBruce Evans handle = (void *)(intptr_t)vp->v_rdev; 106606cb7259SDavid Greenman } else { 106706cb7259SDavid Greenman struct vattr vat; 106806cb7259SDavid Greenman int error; 106906cb7259SDavid Greenman 107006cb7259SDavid Greenman error = VOP_GETATTR(vp, &vat, p->p_ucred, p); 107106cb7259SDavid Greenman if (error) 107206cb7259SDavid Greenman return (error); 1073bd7e5f99SJohn Dyson objsize = round_page(vat.va_size); 107424a1cce3SDavid Greenman type = OBJT_VNODE; 107500d76afeSGuido van Rooij /* 107600d76afeSGuido van Rooij * if it is a regular file without any references 107700d76afeSGuido van Rooij * we do not need to sync it. 107800d76afeSGuido van Rooij */ 107900d76afeSGuido van Rooij if (vp->v_type == VREG && vat.va_nlink == 0) { 108000d76afeSGuido van Rooij flags |= MAP_NOSYNC; 108100d76afeSGuido van Rooij } 1082df8bae1dSRodney W. Grimes } 108306cb7259SDavid Greenman } 108494328e90SJohn Dyson 108594328e90SJohn Dyson if (handle == NULL) { 108694328e90SJohn Dyson object = NULL; 10874738fa09SAlan Cox docow = 0; 108894328e90SJohn Dyson } else { 10890a0a85b3SJohn Dyson object = vm_pager_allocate(type, 10906cde7a16SDavid Greenman handle, objsize, prot, foff); 109124a1cce3SDavid Greenman if (object == NULL) 109224a1cce3SDavid Greenman return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 10934738fa09SAlan Cox docow = MAP_PREFAULT_PARTIAL; 109494328e90SJohn Dyson } 1095df8bae1dSRodney W. Grimes 10965850152dSJohn Dyson /* 10978f2ec877SDavid Greenman * Force device mappings to be shared. 10985850152dSJohn Dyson */ 109924964514SPeter Wemm if (type == OBJT_DEVICE || type == OBJT_PHYS) { 11008f2ec877SDavid Greenman flags &= ~(MAP_PRIVATE|MAP_COPY); 11015850152dSJohn Dyson flags |= MAP_SHARED; 11028f2ec877SDavid Greenman } 11035850152dSJohn Dyson 11044f79d873SMatthew Dillon if ((flags & (MAP_ANON|MAP_SHARED)) == 0) 11054738fa09SAlan Cox docow |= MAP_COPY_ON_WRITE; 11064f79d873SMatthew Dillon if (flags & MAP_NOSYNC) 11074f79d873SMatthew Dillon docow |= MAP_DISABLE_SYNCER; 11089730a5daSPaul Saab if (flags & MAP_NOCORE) 11099730a5daSPaul Saab docow |= MAP_DISABLE_COREDUMP; 11105850152dSJohn Dyson 1111d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 1112d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 1113d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 1114d0aea04fSJohn Dyson 1115d0aea04fSJohn Dyson if (maxprot & VM_PROT_READ) 1116d0aea04fSJohn Dyson maxprot |= VM_PROT_EXECUTE; 1117d0aea04fSJohn Dyson #endif 1118d0aea04fSJohn Dyson 11190a0a85b3SJohn Dyson if (fitit) { 11200a0a85b3SJohn Dyson *addr = pmap_addr_hint(object, *addr, size); 11210a0a85b3SJohn Dyson } 11220a0a85b3SJohn Dyson 11232267af78SJulian Elischer if (flags & MAP_STACK) 11242267af78SJulian Elischer rv = vm_map_stack (map, *addr, size, prot, 11252267af78SJulian Elischer maxprot, docow); 11262267af78SJulian Elischer else 1127bd7e5f99SJohn Dyson rv = vm_map_find(map, object, foff, addr, size, fitit, 1128bd7e5f99SJohn Dyson prot, maxprot, docow); 1129bd7e5f99SJohn Dyson 1130df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 11317fb0c17eSDavid Greenman /* 113224a1cce3SDavid Greenman * Lose the object reference. Will destroy the 113324a1cce3SDavid Greenman * object if it's an unnamed anonymous mapping 113424a1cce3SDavid Greenman * or named anonymous without other references. 11357fb0c17eSDavid Greenman */ 1136df8bae1dSRodney W. Grimes vm_object_deallocate(object); 1137df8bae1dSRodney W. Grimes goto out; 1138df8bae1dSRodney W. Grimes } 1139e17bed12SJohn Dyson 1140df8bae1dSRodney W. Grimes /* 1141df8bae1dSRodney W. Grimes * Shared memory is also shared with children. 1142df8bae1dSRodney W. Grimes */ 11435850152dSJohn Dyson if (flags & (MAP_SHARED|MAP_INHERIT)) { 1144df8bae1dSRodney W. Grimes rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 1145df8bae1dSRodney W. Grimes if (rv != KERN_SUCCESS) { 11467fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 1147df8bae1dSRodney W. Grimes goto out; 1148df8bae1dSRodney W. Grimes } 1149df8bae1dSRodney W. Grimes } 1150df8bae1dSRodney W. Grimes out: 1151df8bae1dSRodney W. Grimes switch (rv) { 1152df8bae1dSRodney W. Grimes case KERN_SUCCESS: 1153df8bae1dSRodney W. Grimes return (0); 1154df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 1155df8bae1dSRodney W. Grimes case KERN_NO_SPACE: 1156df8bae1dSRodney W. Grimes return (ENOMEM); 1157df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 1158df8bae1dSRodney W. Grimes return (EACCES); 1159df8bae1dSRodney W. Grimes default: 1160df8bae1dSRodney W. Grimes return (EINVAL); 1161df8bae1dSRodney W. Grimes } 1162df8bae1dSRodney W. Grimes } 1163