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 50df8bae1dSRodney W. Grimes #include <sys/param.h> 51df8bae1dSRodney W. Grimes #include <sys/systm.h> 52fb919e4dSMark Murray #include <sys/kernel.h> 53fb919e4dSMark Murray #include <sys/lock.h> 5423955314SAlfred Perlstein #include <sys/mutex.h> 55d2d3e875SBruce Evans #include <sys/sysproto.h> 56df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 57df8bae1dSRodney W. Grimes #include <sys/proc.h> 58df8bae1dSRodney W. Grimes #include <sys/vnode.h> 593ac4d1efSBruce Evans #include <sys/fcntl.h> 60df8bae1dSRodney W. Grimes #include <sys/file.h> 61df8bae1dSRodney W. Grimes #include <sys/mman.h> 62df8bae1dSRodney W. Grimes #include <sys/conf.h> 634183b6b6SPeter Wemm #include <sys/stat.h> 64efeaf95aSDavid Greenman #include <sys/vmmeter.h> 651f6889a1SMatthew Dillon #include <sys/sysctl.h> 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes #include <vm/vm.h> 68efeaf95aSDavid Greenman #include <vm/vm_param.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 */ 9611caded3SAlfred Perlstein static void vmmapentry_rsrc_init(void *); 971f6889a1SMatthew Dillon SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL) 981f6889a1SMatthew Dillon 991f6889a1SMatthew Dillon static void 1001f6889a1SMatthew Dillon vmmapentry_rsrc_init(dummy) 1011f6889a1SMatthew Dillon void *dummy; 1021f6889a1SMatthew Dillon { 1031f6889a1SMatthew Dillon max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry); 1041f6889a1SMatthew Dillon max_proc_mmap /= 100; 1051f6889a1SMatthew Dillon } 1061f6889a1SMatthew Dillon 107d2c60af8SMatthew Dillon /* 108d2c60af8SMatthew Dillon * MPSAFE 109d2c60af8SMatthew Dillon */ 110df8bae1dSRodney W. Grimes /* ARGSUSED */ 111df8bae1dSRodney W. Grimes int 112b40ce416SJulian Elischer sbrk(td, uap) 113b40ce416SJulian Elischer struct thread *td; 114df8bae1dSRodney W. Grimes struct sbrk_args *uap; 115df8bae1dSRodney W. Grimes { 116df8bae1dSRodney W. Grimes /* Not yet implemented */ 1170cddd8f0SMatthew Dillon /* mtx_lock(&Giant); */ 1180cddd8f0SMatthew Dillon /* mtx_unlock(&Giant); */ 119df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 120df8bae1dSRodney W. Grimes } 121df8bae1dSRodney W. Grimes 122d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 123df8bae1dSRodney W. Grimes struct sstk_args { 124df8bae1dSRodney W. Grimes int incr; 125df8bae1dSRodney W. Grimes }; 126d2d3e875SBruce Evans #endif 1270d94caffSDavid Greenman 128d2c60af8SMatthew Dillon /* 129d2c60af8SMatthew Dillon * MPSAFE 130d2c60af8SMatthew Dillon */ 131df8bae1dSRodney W. Grimes /* ARGSUSED */ 132df8bae1dSRodney W. Grimes int 133b40ce416SJulian Elischer sstk(td, uap) 134b40ce416SJulian Elischer struct thread *td; 135df8bae1dSRodney W. Grimes struct sstk_args *uap; 136df8bae1dSRodney W. Grimes { 137df8bae1dSRodney W. Grimes /* Not yet implemented */ 1380cddd8f0SMatthew Dillon /* mtx_lock(&Giant); */ 1390cddd8f0SMatthew Dillon /* mtx_unlock(&Giant); */ 140df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 141df8bae1dSRodney W. Grimes } 142df8bae1dSRodney W. Grimes 143df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 144d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 145df8bae1dSRodney W. Grimes struct getpagesize_args { 146df8bae1dSRodney W. Grimes int dummy; 147df8bae1dSRodney W. Grimes }; 148d2d3e875SBruce Evans #endif 1490d94caffSDavid Greenman 150df8bae1dSRodney W. Grimes /* ARGSUSED */ 151df8bae1dSRodney W. Grimes int 152b40ce416SJulian Elischer ogetpagesize(td, uap) 153b40ce416SJulian Elischer struct thread *td; 154df8bae1dSRodney W. Grimes struct getpagesize_args *uap; 155df8bae1dSRodney W. Grimes { 1560cddd8f0SMatthew Dillon /* MP SAFE */ 157b40ce416SJulian Elischer td->td_retval[0] = PAGE_SIZE; 158df8bae1dSRodney W. Grimes return (0); 159df8bae1dSRodney W. Grimes } 160df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 161df8bae1dSRodney W. Grimes 16254f42e4bSPeter Wemm 16354f42e4bSPeter Wemm /* 16454f42e4bSPeter Wemm * Memory Map (mmap) system call. Note that the file offset 16554f42e4bSPeter Wemm * and address are allowed to be NOT page aligned, though if 16654f42e4bSPeter Wemm * the MAP_FIXED flag it set, both must have the same remainder 16754f42e4bSPeter Wemm * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not 16854f42e4bSPeter Wemm * page-aligned, the actual mapping starts at trunc_page(addr) 16954f42e4bSPeter Wemm * and the return value is adjusted up by the page offset. 170b4309055SMatthew Dillon * 171b4309055SMatthew Dillon * Generally speaking, only character devices which are themselves 172b4309055SMatthew Dillon * memory-based, such as a video framebuffer, can be mmap'd. Otherwise 173b4309055SMatthew Dillon * there would be no cache coherency between a descriptor and a VM mapping 174b4309055SMatthew Dillon * both to the same character device. 175b4309055SMatthew Dillon * 176b4309055SMatthew Dillon * Block devices can be mmap'd no matter what they represent. Cache coherency 177b4309055SMatthew Dillon * is maintained as long as you do not write directly to the underlying 178b4309055SMatthew Dillon * character device. 17954f42e4bSPeter Wemm */ 180d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 181df8bae1dSRodney W. Grimes struct mmap_args { 182651bb817SAlexander Langer void *addr; 183df8bae1dSRodney W. Grimes size_t len; 184df8bae1dSRodney W. Grimes int prot; 185df8bae1dSRodney W. Grimes int flags; 186df8bae1dSRodney W. Grimes int fd; 187df8bae1dSRodney W. Grimes long pad; 188df8bae1dSRodney W. Grimes off_t pos; 189df8bae1dSRodney W. Grimes }; 190d2d3e875SBruce Evans #endif 191df8bae1dSRodney W. Grimes 192d2c60af8SMatthew Dillon /* 193d2c60af8SMatthew Dillon * MPSAFE 194d2c60af8SMatthew Dillon */ 195df8bae1dSRodney W. Grimes int 196b40ce416SJulian Elischer mmap(td, uap) 197b40ce416SJulian Elischer struct thread *td; 19854d92145SMatthew Dillon struct mmap_args *uap; 199df8bae1dSRodney W. Grimes { 20054d92145SMatthew Dillon struct file *fp = NULL; 201df8bae1dSRodney W. Grimes struct vnode *vp; 202df8bae1dSRodney W. Grimes vm_offset_t addr; 2039154ee6aSPeter Wemm vm_size_t size, pageoff; 204df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 205651bb817SAlexander Langer void *handle; 206df8bae1dSRodney W. Grimes int flags, error; 207c8bdd56bSGuido van Rooij int disablexworkaround; 20854f42e4bSPeter Wemm off_t pos; 209b40ce416SJulian Elischer struct vmspace *vms = td->td_proc->p_vmspace; 2109ff5ce6bSBoris Popov vm_object_t obj; 211df8bae1dSRodney W. Grimes 21254f42e4bSPeter Wemm addr = (vm_offset_t) uap->addr; 21354f42e4bSPeter Wemm size = uap->len; 214df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 215df8bae1dSRodney W. Grimes flags = uap->flags; 21654f42e4bSPeter Wemm pos = uap->pos; 21754f42e4bSPeter Wemm 218426da3bcSAlfred Perlstein fp = NULL; 21954f42e4bSPeter Wemm /* make sure mapping fits into numeric range etc */ 220fc565456SDmitrij Tejblum if ((ssize_t) uap->len < 0 || 22154f42e4bSPeter Wemm ((flags & MAP_ANON) && uap->fd != -1)) 222df8bae1dSRodney W. Grimes return (EINVAL); 2239154ee6aSPeter Wemm 2242267af78SJulian Elischer if (flags & MAP_STACK) { 2252267af78SJulian Elischer if ((uap->fd != -1) || 2262267af78SJulian Elischer ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE))) 2272267af78SJulian Elischer return (EINVAL); 2282267af78SJulian Elischer flags |= MAP_ANON; 2292267af78SJulian Elischer pos = 0; 2302907af2aSJulian Elischer } 2312907af2aSJulian Elischer 2329154ee6aSPeter Wemm /* 23354f42e4bSPeter Wemm * Align the file position to a page boundary, 23454f42e4bSPeter Wemm * and save its page offset component. 2359154ee6aSPeter Wemm */ 23654f42e4bSPeter Wemm pageoff = (pos & PAGE_MASK); 23754f42e4bSPeter Wemm pos -= pageoff; 23854f42e4bSPeter Wemm 23954f42e4bSPeter Wemm /* Adjust size for rounding (on both ends). */ 24054f42e4bSPeter Wemm size += pageoff; /* low end... */ 24154f42e4bSPeter Wemm size = (vm_size_t) round_page(size); /* hi end */ 2429154ee6aSPeter Wemm 243df8bae1dSRodney W. Grimes /* 2440d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 2450d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 246df8bae1dSRodney W. Grimes */ 247df8bae1dSRodney W. Grimes if (flags & MAP_FIXED) { 24854f42e4bSPeter Wemm /* 24954f42e4bSPeter Wemm * The specified address must have the same remainder 25054f42e4bSPeter Wemm * as the file offset taken modulo PAGE_SIZE, so it 25154f42e4bSPeter Wemm * should be aligned after adjustment by pageoff. 25254f42e4bSPeter Wemm */ 25354f42e4bSPeter Wemm addr -= pageoff; 25454f42e4bSPeter Wemm if (addr & PAGE_MASK) 25554f42e4bSPeter Wemm return (EINVAL); 25654f42e4bSPeter Wemm /* Address range must be all in user VM space. */ 257bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 258df8bae1dSRodney W. Grimes return (EINVAL); 25999b9331aSAlfred Perlstein #ifndef __i386__ 260df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 261df8bae1dSRodney W. Grimes return (EINVAL); 26226f9a767SRodney W. Grimes #endif 263bbc0ec52SDavid Greenman if (addr + size < addr) 264df8bae1dSRodney W. Grimes return (EINVAL); 265df8bae1dSRodney W. Grimes } 266df8bae1dSRodney W. Grimes /* 26754f42e4bSPeter Wemm * XXX for non-fixed mappings where no hint is provided or 26854f42e4bSPeter Wemm * the hint would fall in the potential heap space, 26954f42e4bSPeter Wemm * place it after the end of the largest possible heap. 270df8bae1dSRodney W. Grimes * 27154f42e4bSPeter Wemm * There should really be a pmap call to determine a reasonable 27254f42e4bSPeter Wemm * location. 273df8bae1dSRodney W. Grimes */ 274d28ab90fSLuoqi Chen else if (addr == 0 || 2751f6889a1SMatthew Dillon (addr >= round_page((vm_offset_t)vms->vm_taddr) && 276cbc89bfbSPaul Saab addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))) 277cbc89bfbSPaul Saab addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz); 27854f42e4bSPeter Wemm 2790cddd8f0SMatthew Dillon mtx_lock(&Giant); /* syscall marked mp-safe but isn't */ 280df8bae1dSRodney W. Grimes if (flags & MAP_ANON) { 281df8bae1dSRodney W. Grimes /* 282df8bae1dSRodney W. Grimes * Mapping blank space is trivial. 283df8bae1dSRodney W. Grimes */ 284df8bae1dSRodney W. Grimes handle = NULL; 285df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 28654f42e4bSPeter Wemm pos = 0; 287df8bae1dSRodney W. Grimes } else { 288df8bae1dSRodney W. Grimes /* 2890d94caffSDavid Greenman * Mapping file, get fp for validation. Obtain vnode and make 2900d94caffSDavid Greenman * sure it is of appropriate type. 291426da3bcSAlfred Perlstein * don't let the descriptor disappear on us if we block 292df8bae1dSRodney W. Grimes */ 293a4db4953SAlfred Perlstein if ((error = fget(td, uap->fd, &fp)) != 0) 294426da3bcSAlfred Perlstein goto done; 295e4ca250dSJohn Baldwin if (fp->f_type != DTYPE_VNODE) { 296d2c60af8SMatthew Dillon error = EINVAL; 297426da3bcSAlfred Perlstein goto done; 298e4ca250dSJohn Baldwin } 299279d7226SMatthew Dillon 300279d7226SMatthew Dillon /* 301aa543039SGarrett Wollman * POSIX shared-memory objects are defined to have 302aa543039SGarrett Wollman * kernel persistence, and are not defined to support 303aa543039SGarrett Wollman * read(2)/write(2) -- or even open(2). Thus, we can 304aa543039SGarrett Wollman * use MAP_ASYNC to trade on-disk coherence for speed. 305aa543039SGarrett Wollman * The shm_open(3) library routine turns on the FPOSIXSHM 306aa543039SGarrett Wollman * flag to request this behavior. 307aa543039SGarrett Wollman */ 308aa543039SGarrett Wollman if (fp->f_flag & FPOSIXSHM) 309aa543039SGarrett Wollman flags |= MAP_NOSYNC; 310df8bae1dSRodney W. Grimes vp = (struct vnode *) fp->f_data; 311e4ca250dSJohn Baldwin if (vp->v_type != VREG && vp->v_type != VCHR) { 312e4ca250dSJohn Baldwin error = EINVAL; 313e4ca250dSJohn Baldwin goto done; 314e4ca250dSJohn Baldwin } 3159ff5ce6bSBoris Popov if (vp->v_type == VREG) { 3169ff5ce6bSBoris Popov /* 3179ff5ce6bSBoris Popov * Get the proper underlying object 3189ff5ce6bSBoris Popov */ 3190cddd8f0SMatthew Dillon if (VOP_GETVOBJECT(vp, &obj) != 0) { 3200cddd8f0SMatthew Dillon error = EINVAL; 3210cddd8f0SMatthew Dillon goto done; 3220cddd8f0SMatthew Dillon } 3239ff5ce6bSBoris Popov vp = (struct vnode*)obj->handle; 3249ff5ce6bSBoris Popov } 325df8bae1dSRodney W. Grimes /* 3260d94caffSDavid Greenman * XXX hack to handle use of /dev/zero to map anon memory (ala 3270d94caffSDavid Greenman * SunOS). 328df8bae1dSRodney W. Grimes */ 3292589f249SMark Murray if ((vp->v_type == VCHR) && 3302589f249SMark Murray (vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON)) { 331df8bae1dSRodney W. Grimes handle = NULL; 332df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 333df8bae1dSRodney W. Grimes flags |= MAP_ANON; 33454f42e4bSPeter Wemm pos = 0; 335df8bae1dSRodney W. Grimes } else { 336df8bae1dSRodney W. Grimes /* 337c8bdd56bSGuido van Rooij * cdevs does not provide private mappings of any kind. 338c8bdd56bSGuido van Rooij */ 339c8bdd56bSGuido van Rooij /* 340c8bdd56bSGuido van Rooij * However, for XIG X server to continue to work, 341c8bdd56bSGuido van Rooij * we should allow the superuser to do it anyway. 342c8bdd56bSGuido van Rooij * We only allow it at securelevel < 1. 343c8bdd56bSGuido van Rooij * (Because the XIG X server writes directly to video 344c8bdd56bSGuido van Rooij * memory via /dev/mem, it should never work at any 345c8bdd56bSGuido van Rooij * other securelevel. 346c8bdd56bSGuido van Rooij * XXX this will have to go 347c8bdd56bSGuido van Rooij */ 348a854ed98SJohn Baldwin if (securelevel_ge(td->td_ucred, 1)) 349c8bdd56bSGuido van Rooij disablexworkaround = 1; 350c8bdd56bSGuido van Rooij else 35144731cabSJohn Baldwin disablexworkaround = suser(td); 352c8bdd56bSGuido van Rooij if (vp->v_type == VCHR && disablexworkaround && 353279d7226SMatthew Dillon (flags & (MAP_PRIVATE|MAP_COPY))) { 354279d7226SMatthew Dillon error = EINVAL; 355279d7226SMatthew Dillon goto done; 356279d7226SMatthew Dillon } 357c8bdd56bSGuido van Rooij /* 358df8bae1dSRodney W. Grimes * Ensure that file and memory protections are 359df8bae1dSRodney W. Grimes * compatible. Note that we only worry about 360df8bae1dSRodney W. Grimes * writability if mapping is shared; in this case, 361df8bae1dSRodney W. Grimes * current and max prot are dictated by the open file. 362df8bae1dSRodney W. Grimes * XXX use the vnode instead? Problem is: what 3630d94caffSDavid Greenman * credentials do we use for determination? What if 3640d94caffSDavid Greenman * proc does a setuid? 365df8bae1dSRodney W. Grimes */ 366df8bae1dSRodney W. Grimes maxprot = VM_PROT_EXECUTE; /* ??? */ 367279d7226SMatthew Dillon if (fp->f_flag & FREAD) { 368df8bae1dSRodney W. Grimes maxprot |= VM_PROT_READ; 369279d7226SMatthew Dillon } else if (prot & PROT_READ) { 370279d7226SMatthew Dillon error = EACCES; 371279d7226SMatthew Dillon goto done; 372279d7226SMatthew Dillon } 373c8bdd56bSGuido van Rooij /* 374c8bdd56bSGuido van Rooij * If we are sharing potential changes (either via 375c8bdd56bSGuido van Rooij * MAP_SHARED or via the implicit sharing of character 376c8bdd56bSGuido van Rooij * device mappings), and we are trying to get write 377c8bdd56bSGuido van Rooij * permission although we opened it without asking 378c8bdd56bSGuido van Rooij * for it, bail out. Check for superuser, only if 379c8bdd56bSGuido van Rooij * we're at securelevel < 1, to allow the XIG X server 380c8bdd56bSGuido van Rooij * to continue to work. 381c8bdd56bSGuido van Rooij */ 38205feb99fSGuido van Rooij if ((flags & MAP_SHARED) != 0 || 38305feb99fSGuido van Rooij (vp->v_type == VCHR && disablexworkaround)) { 38405feb99fSGuido van Rooij if ((fp->f_flag & FWRITE) != 0) { 3854183b6b6SPeter Wemm struct vattr va; 38605feb99fSGuido van Rooij if ((error = 38705feb99fSGuido van Rooij VOP_GETATTR(vp, &va, 388a854ed98SJohn Baldwin td->td_ucred, td))) { 389279d7226SMatthew Dillon goto done; 390279d7226SMatthew Dillon } 39105feb99fSGuido van Rooij if ((va.va_flags & 392279d7226SMatthew Dillon (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0) { 393df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 394279d7226SMatthew Dillon } else if (prot & PROT_WRITE) { 395279d7226SMatthew Dillon error = EPERM; 396279d7226SMatthew Dillon goto done; 397279d7226SMatthew Dillon } 398279d7226SMatthew Dillon } else if ((prot & PROT_WRITE) != 0) { 399279d7226SMatthew Dillon error = EACCES; 400279d7226SMatthew Dillon goto done; 401279d7226SMatthew Dillon } 402279d7226SMatthew Dillon } else { 40305feb99fSGuido van Rooij maxprot |= VM_PROT_WRITE; 404279d7226SMatthew Dillon } 40505feb99fSGuido van Rooij 406651bb817SAlexander Langer handle = (void *)vp; 407df8bae1dSRodney W. Grimes } 408df8bae1dSRodney W. Grimes } 4091f6889a1SMatthew Dillon 4101f6889a1SMatthew Dillon /* 4111f6889a1SMatthew Dillon * Do not allow more then a certain number of vm_map_entry structures 4121f6889a1SMatthew Dillon * per process. Scale with the number of rforks sharing the map 4131f6889a1SMatthew Dillon * to make the limit reasonable for threads. 4141f6889a1SMatthew Dillon */ 4151f6889a1SMatthew Dillon if (max_proc_mmap && 4161f6889a1SMatthew Dillon vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) { 417279d7226SMatthew Dillon error = ENOMEM; 418279d7226SMatthew Dillon goto done; 4191f6889a1SMatthew Dillon } 4201f6889a1SMatthew Dillon 421e4ca250dSJohn Baldwin mtx_unlock(&Giant); 4221f6889a1SMatthew Dillon error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot, 42354f42e4bSPeter Wemm flags, handle, pos); 424df8bae1dSRodney W. Grimes if (error == 0) 425b40ce416SJulian Elischer td->td_retval[0] = (register_t) (addr + pageoff); 426e4ca250dSJohn Baldwin mtx_lock(&Giant); 427279d7226SMatthew Dillon done: 428279d7226SMatthew Dillon if (fp) 429b40ce416SJulian Elischer fdrop(fp, td); 430e4ca250dSJohn Baldwin mtx_unlock(&Giant); 431df8bae1dSRodney W. Grimes return (error); 432df8bae1dSRodney W. Grimes } 433df8bae1dSRodney W. Grimes 43405f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43 435d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 43605f0fdd2SPoul-Henning Kamp struct ommap_args { 43705f0fdd2SPoul-Henning Kamp caddr_t addr; 43805f0fdd2SPoul-Henning Kamp int len; 43905f0fdd2SPoul-Henning Kamp int prot; 44005f0fdd2SPoul-Henning Kamp int flags; 44105f0fdd2SPoul-Henning Kamp int fd; 44205f0fdd2SPoul-Henning Kamp long pos; 44305f0fdd2SPoul-Henning Kamp }; 444d2d3e875SBruce Evans #endif 44505f0fdd2SPoul-Henning Kamp int 446b40ce416SJulian Elischer ommap(td, uap) 447b40ce416SJulian Elischer struct thread *td; 44854d92145SMatthew Dillon struct ommap_args *uap; 44905f0fdd2SPoul-Henning Kamp { 45005f0fdd2SPoul-Henning Kamp struct mmap_args nargs; 45105f0fdd2SPoul-Henning Kamp static const char cvtbsdprot[8] = { 45205f0fdd2SPoul-Henning Kamp 0, 45305f0fdd2SPoul-Henning Kamp PROT_EXEC, 45405f0fdd2SPoul-Henning Kamp PROT_WRITE, 45505f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE, 45605f0fdd2SPoul-Henning Kamp PROT_READ, 45705f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_READ, 45805f0fdd2SPoul-Henning Kamp PROT_WRITE | PROT_READ, 45905f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE | PROT_READ, 46005f0fdd2SPoul-Henning Kamp }; 4610d94caffSDavid Greenman 46205f0fdd2SPoul-Henning Kamp #define OMAP_ANON 0x0002 46305f0fdd2SPoul-Henning Kamp #define OMAP_COPY 0x0020 46405f0fdd2SPoul-Henning Kamp #define OMAP_SHARED 0x0010 46505f0fdd2SPoul-Henning Kamp #define OMAP_FIXED 0x0100 46605f0fdd2SPoul-Henning Kamp 46705f0fdd2SPoul-Henning Kamp nargs.addr = uap->addr; 46805f0fdd2SPoul-Henning Kamp nargs.len = uap->len; 46905f0fdd2SPoul-Henning Kamp nargs.prot = cvtbsdprot[uap->prot & 0x7]; 47005f0fdd2SPoul-Henning Kamp nargs.flags = 0; 47105f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_ANON) 47205f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_ANON; 47305f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_COPY) 47405f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_COPY; 47505f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_SHARED) 47605f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_SHARED; 47705f0fdd2SPoul-Henning Kamp else 47805f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_PRIVATE; 47905f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_FIXED) 48005f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_FIXED; 48105f0fdd2SPoul-Henning Kamp nargs.fd = uap->fd; 48205f0fdd2SPoul-Henning Kamp nargs.pos = uap->pos; 483b40ce416SJulian Elischer return (mmap(td, &nargs)); 48405f0fdd2SPoul-Henning Kamp } 48505f0fdd2SPoul-Henning Kamp #endif /* COMPAT_43 */ 48605f0fdd2SPoul-Henning Kamp 48705f0fdd2SPoul-Henning Kamp 488d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 489df8bae1dSRodney W. Grimes struct msync_args { 490651bb817SAlexander Langer void *addr; 491df8bae1dSRodney W. Grimes int len; 492e6c6af11SDavid Greenman int flags; 493df8bae1dSRodney W. Grimes }; 494d2d3e875SBruce Evans #endif 495d2c60af8SMatthew Dillon /* 496d2c60af8SMatthew Dillon * MPSAFE 497d2c60af8SMatthew Dillon */ 498df8bae1dSRodney W. Grimes int 499b40ce416SJulian Elischer msync(td, uap) 500b40ce416SJulian Elischer struct thread *td; 501df8bae1dSRodney W. Grimes struct msync_args *uap; 502df8bae1dSRodney W. Grimes { 503df8bae1dSRodney W. Grimes vm_offset_t addr; 504dabee6feSPeter Wemm vm_size_t size, pageoff; 505e6c6af11SDavid Greenman int flags; 506df8bae1dSRodney W. Grimes vm_map_t map; 507df8bae1dSRodney W. Grimes int rv; 508df8bae1dSRodney W. Grimes 509df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 5109154ee6aSPeter Wemm size = uap->len; 511e6c6af11SDavid Greenman flags = uap->flags; 512e6c6af11SDavid Greenman 513dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 514dabee6feSPeter Wemm addr -= pageoff; 515dabee6feSPeter Wemm size += pageoff; 516dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 5179154ee6aSPeter Wemm if (addr + size < addr) 518dabee6feSPeter Wemm return (EINVAL); 519dabee6feSPeter Wemm 520dabee6feSPeter Wemm if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 5211e62bc63SDavid Greenman return (EINVAL); 5221e62bc63SDavid Greenman 5230cddd8f0SMatthew Dillon mtx_lock(&Giant); 5240cddd8f0SMatthew Dillon 525b40ce416SJulian Elischer map = &td->td_proc->p_vmspace->vm_map; 5269154ee6aSPeter Wemm 527df8bae1dSRodney W. Grimes /* 528df8bae1dSRodney W. Grimes * XXX Gak! If size is zero we are supposed to sync "all modified 5290d94caffSDavid Greenman * pages with the region containing addr". Unfortunately, we don't 5300d94caffSDavid Greenman * really keep track of individual mmaps so we approximate by flushing 5310d94caffSDavid Greenman * the range of the map entry containing addr. This can be incorrect 5320d94caffSDavid Greenman * if the region splits or is coalesced with a neighbor. 533df8bae1dSRodney W. Grimes */ 534df8bae1dSRodney W. Grimes if (size == 0) { 535df8bae1dSRodney W. Grimes vm_map_entry_t entry; 536df8bae1dSRodney W. Grimes 537df8bae1dSRodney W. Grimes vm_map_lock_read(map); 538df8bae1dSRodney W. Grimes rv = vm_map_lookup_entry(map, addr, &entry); 539df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 54023955314SAlfred Perlstein if (rv == FALSE) { 541d2c60af8SMatthew Dillon rv = -1; 542d2c60af8SMatthew Dillon goto done2; 54323955314SAlfred Perlstein } 544df8bae1dSRodney W. Grimes addr = entry->start; 545df8bae1dSRodney W. Grimes size = entry->end - entry->start; 546df8bae1dSRodney W. Grimes } 547e6c6af11SDavid Greenman 548df8bae1dSRodney W. Grimes /* 549df8bae1dSRodney W. Grimes * Clean the pages and interpret the return value. 550df8bae1dSRodney W. Grimes */ 5516c534ad8SDavid Greenman rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 552e6c6af11SDavid Greenman (flags & MS_INVALIDATE) != 0); 553e6c6af11SDavid Greenman 554d2c60af8SMatthew Dillon done2: 555190609ddSJohn Baldwin mtx_unlock(&Giant); 5560cddd8f0SMatthew Dillon 557df8bae1dSRodney W. Grimes switch (rv) { 558df8bae1dSRodney W. Grimes case KERN_SUCCESS: 559d2c60af8SMatthew Dillon return (0); 560df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 561df8bae1dSRodney W. Grimes return (EINVAL); /* Sun returns ENOMEM? */ 562df8bae1dSRodney W. Grimes case KERN_FAILURE: 563df8bae1dSRodney W. Grimes return (EIO); 564df8bae1dSRodney W. Grimes default: 565df8bae1dSRodney W. Grimes return (EINVAL); 566df8bae1dSRodney W. Grimes } 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes 569d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 570df8bae1dSRodney W. Grimes struct munmap_args { 571651bb817SAlexander Langer void *addr; 5729154ee6aSPeter Wemm size_t len; 573df8bae1dSRodney W. Grimes }; 574d2d3e875SBruce Evans #endif 575d2c60af8SMatthew Dillon /* 576d2c60af8SMatthew Dillon * MPSAFE 577d2c60af8SMatthew Dillon */ 578df8bae1dSRodney W. Grimes int 579b40ce416SJulian Elischer munmap(td, uap) 580b40ce416SJulian Elischer struct thread *td; 58154d92145SMatthew Dillon struct munmap_args *uap; 582df8bae1dSRodney W. Grimes { 583df8bae1dSRodney W. Grimes vm_offset_t addr; 584dabee6feSPeter Wemm vm_size_t size, pageoff; 585df8bae1dSRodney W. Grimes vm_map_t map; 586df8bae1dSRodney W. Grimes 587df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 5889154ee6aSPeter Wemm size = uap->len; 589dabee6feSPeter Wemm 590dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 591dabee6feSPeter Wemm addr -= pageoff; 592dabee6feSPeter Wemm size += pageoff; 593dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 5949154ee6aSPeter Wemm if (addr + size < addr) 595df8bae1dSRodney W. Grimes return (EINVAL); 5969154ee6aSPeter Wemm 597df8bae1dSRodney W. Grimes if (size == 0) 598df8bae1dSRodney W. Grimes return (0); 599dabee6feSPeter Wemm 600df8bae1dSRodney W. Grimes /* 6010d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 6020d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 603df8bae1dSRodney W. Grimes */ 604bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 605df8bae1dSRodney W. Grimes return (EINVAL); 60699b9331aSAlfred Perlstein #ifndef __i386__ 607df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 608df8bae1dSRodney W. Grimes return (EINVAL); 60926f9a767SRodney W. Grimes #endif 610b40ce416SJulian Elischer map = &td->td_proc->p_vmspace->vm_map; 611df8bae1dSRodney W. Grimes /* 612df8bae1dSRodney W. Grimes * Make sure entire range is allocated. 613df8bae1dSRodney W. Grimes */ 6148c5c5d04SAlan Cox if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) 615df8bae1dSRodney W. Grimes return (EINVAL); 6168c5c5d04SAlan Cox 617df8bae1dSRodney W. Grimes /* returns nothing but KERN_SUCCESS anyway */ 618df8bae1dSRodney W. Grimes (void) vm_map_remove(map, addr, addr + size); 619df8bae1dSRodney W. Grimes return (0); 620df8bae1dSRodney W. Grimes } 621df8bae1dSRodney W. Grimes 622279d7226SMatthew Dillon #if 0 623df8bae1dSRodney W. Grimes void 624b40ce416SJulian Elischer munmapfd(td, fd) 625b40ce416SJulian Elischer struct thread *td; 626df8bae1dSRodney W. Grimes int fd; 627df8bae1dSRodney W. Grimes { 628df8bae1dSRodney W. Grimes /* 629c4ed5a07SDavid Greenman * XXX should unmap any regions mapped to this file 630df8bae1dSRodney W. Grimes */ 631426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 632b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED; 633426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 634df8bae1dSRodney W. Grimes } 635279d7226SMatthew Dillon #endif 636df8bae1dSRodney W. Grimes 637d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 638df8bae1dSRodney W. Grimes struct mprotect_args { 639651bb817SAlexander Langer const void *addr; 6409154ee6aSPeter Wemm size_t len; 641df8bae1dSRodney W. Grimes int prot; 642df8bae1dSRodney W. Grimes }; 643d2d3e875SBruce Evans #endif 644d2c60af8SMatthew Dillon /* 645d2c60af8SMatthew Dillon * MPSAFE 646d2c60af8SMatthew Dillon */ 647df8bae1dSRodney W. Grimes int 648b40ce416SJulian Elischer mprotect(td, uap) 649b40ce416SJulian Elischer struct thread *td; 650df8bae1dSRodney W. Grimes struct mprotect_args *uap; 651df8bae1dSRodney W. Grimes { 652df8bae1dSRodney W. Grimes vm_offset_t addr; 653dabee6feSPeter Wemm vm_size_t size, pageoff; 65454d92145SMatthew Dillon vm_prot_t prot; 655df8bae1dSRodney W. Grimes 656df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 6579154ee6aSPeter Wemm size = uap->len; 658df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 659d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 660d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 661d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 662d0aea04fSJohn Dyson #endif 663df8bae1dSRodney W. Grimes 664dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 665dabee6feSPeter Wemm addr -= pageoff; 666dabee6feSPeter Wemm size += pageoff; 667dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 6689154ee6aSPeter Wemm if (addr + size < addr) 669dabee6feSPeter Wemm return (EINVAL); 670dabee6feSPeter Wemm 67143285049SAlan Cox switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr, 67243285049SAlan Cox addr + size, prot, FALSE)) { 673df8bae1dSRodney W. Grimes case KERN_SUCCESS: 674df8bae1dSRodney W. Grimes return (0); 675df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 676df8bae1dSRodney W. Grimes return (EACCES); 677df8bae1dSRodney W. Grimes } 678df8bae1dSRodney W. Grimes return (EINVAL); 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes 681d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 682dabee6feSPeter Wemm struct minherit_args { 683651bb817SAlexander Langer void *addr; 6849154ee6aSPeter Wemm size_t len; 685dabee6feSPeter Wemm int inherit; 686dabee6feSPeter Wemm }; 687dabee6feSPeter Wemm #endif 688d2c60af8SMatthew Dillon /* 689d2c60af8SMatthew Dillon * MPSAFE 690d2c60af8SMatthew Dillon */ 691dabee6feSPeter Wemm int 692b40ce416SJulian Elischer minherit(td, uap) 693b40ce416SJulian Elischer struct thread *td; 694dabee6feSPeter Wemm struct minherit_args *uap; 695dabee6feSPeter Wemm { 696dabee6feSPeter Wemm vm_offset_t addr; 697dabee6feSPeter Wemm vm_size_t size, pageoff; 69854d92145SMatthew Dillon vm_inherit_t inherit; 699dabee6feSPeter Wemm 700dabee6feSPeter Wemm addr = (vm_offset_t)uap->addr; 7019154ee6aSPeter Wemm size = uap->len; 702dabee6feSPeter Wemm inherit = uap->inherit; 703dabee6feSPeter Wemm 704dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 705dabee6feSPeter Wemm addr -= pageoff; 706dabee6feSPeter Wemm size += pageoff; 707dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 7089154ee6aSPeter Wemm if (addr + size < addr) 709dabee6feSPeter Wemm return (EINVAL); 710dabee6feSPeter Wemm 711e0be79afSAlan Cox switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr, 712e0be79afSAlan Cox addr + size, inherit)) { 713dabee6feSPeter Wemm case KERN_SUCCESS: 714dabee6feSPeter Wemm return (0); 715dabee6feSPeter Wemm case KERN_PROTECTION_FAILURE: 716dabee6feSPeter Wemm return (EACCES); 717dabee6feSPeter Wemm } 718dabee6feSPeter Wemm return (EINVAL); 719dabee6feSPeter Wemm } 720dabee6feSPeter Wemm 721dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 722df8bae1dSRodney W. Grimes struct madvise_args { 723651bb817SAlexander Langer void *addr; 7249154ee6aSPeter Wemm size_t len; 725df8bae1dSRodney W. Grimes int behav; 726df8bae1dSRodney W. Grimes }; 727d2d3e875SBruce Evans #endif 7280d94caffSDavid Greenman 729d2c60af8SMatthew Dillon /* 730d2c60af8SMatthew Dillon * MPSAFE 731d2c60af8SMatthew Dillon */ 732df8bae1dSRodney W. Grimes /* ARGSUSED */ 733df8bae1dSRodney W. Grimes int 734b40ce416SJulian Elischer madvise(td, uap) 735b40ce416SJulian Elischer struct thread *td; 736df8bae1dSRodney W. Grimes struct madvise_args *uap; 737df8bae1dSRodney W. Grimes { 738f35329acSJohn Dyson vm_offset_t start, end; 739b4309055SMatthew Dillon 740b4309055SMatthew Dillon /* 741b4309055SMatthew Dillon * Check for illegal behavior 742b4309055SMatthew Dillon */ 7439730a5daSPaul Saab if (uap->behav < 0 || uap->behav > MADV_CORE) 744b4309055SMatthew Dillon return (EINVAL); 745867a482dSJohn Dyson /* 746867a482dSJohn Dyson * Check for illegal addresses. Watch out for address wrap... Note 747867a482dSJohn Dyson * that VM_*_ADDRESS are not constants due to casts (argh). 748867a482dSJohn Dyson */ 749867a482dSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && 750867a482dSJohn Dyson ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS) 751867a482dSJohn Dyson return (EINVAL); 75299b9331aSAlfred Perlstein #ifndef __i386__ 753867a482dSJohn Dyson if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS) 754867a482dSJohn Dyson return (EINVAL); 755867a482dSJohn Dyson #endif 756867a482dSJohn Dyson if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr) 757867a482dSJohn Dyson return (EINVAL); 758867a482dSJohn Dyson 759867a482dSJohn Dyson /* 760867a482dSJohn Dyson * Since this routine is only advisory, we default to conservative 761867a482dSJohn Dyson * behavior. 762867a482dSJohn Dyson */ 763cd6eea25SDavid Greenman start = trunc_page((vm_offset_t) uap->addr); 764cd6eea25SDavid Greenman end = round_page((vm_offset_t) uap->addr + uap->len); 765867a482dSJohn Dyson 766094f6d26SAlan Cox if (vm_map_madvise(&td->td_proc->p_vmspace->vm_map, start, end, 767094f6d26SAlan Cox uap->behav)) 768094f6d26SAlan Cox return (EINVAL); 769094f6d26SAlan Cox return (0); 770df8bae1dSRodney W. Grimes } 771df8bae1dSRodney W. Grimes 772d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 773df8bae1dSRodney W. Grimes struct mincore_args { 774651bb817SAlexander Langer const void *addr; 7759154ee6aSPeter Wemm size_t len; 776df8bae1dSRodney W. Grimes char *vec; 777df8bae1dSRodney W. Grimes }; 778d2d3e875SBruce Evans #endif 7790d94caffSDavid Greenman 780d2c60af8SMatthew Dillon /* 781d2c60af8SMatthew Dillon * MPSAFE 782d2c60af8SMatthew Dillon */ 783df8bae1dSRodney W. Grimes /* ARGSUSED */ 784df8bae1dSRodney W. Grimes int 785b40ce416SJulian Elischer mincore(td, uap) 786b40ce416SJulian Elischer struct thread *td; 787df8bae1dSRodney W. Grimes struct mincore_args *uap; 788df8bae1dSRodney W. Grimes { 789867a482dSJohn Dyson vm_offset_t addr, first_addr; 790867a482dSJohn Dyson vm_offset_t end, cend; 791867a482dSJohn Dyson pmap_t pmap; 792867a482dSJohn Dyson vm_map_t map; 79302c04a2fSJohn Dyson char *vec; 794d2c60af8SMatthew Dillon int error = 0; 795867a482dSJohn Dyson int vecindex, lastvecindex; 79654d92145SMatthew Dillon vm_map_entry_t current; 797867a482dSJohn Dyson vm_map_entry_t entry; 798867a482dSJohn Dyson int mincoreinfo; 799dd2622a8SAlan Cox unsigned int timestamp; 800df8bae1dSRodney W. Grimes 801867a482dSJohn Dyson /* 802867a482dSJohn Dyson * Make sure that the addresses presented are valid for user 803867a482dSJohn Dyson * mode. 804867a482dSJohn Dyson */ 805867a482dSJohn Dyson first_addr = addr = trunc_page((vm_offset_t) uap->addr); 8069154ee6aSPeter Wemm end = addr + (vm_size_t)round_page(uap->len); 80702c04a2fSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS) 80802c04a2fSJohn Dyson return (EINVAL); 80902c04a2fSJohn Dyson if (end < addr) 81002c04a2fSJohn Dyson return (EINVAL); 81102c04a2fSJohn Dyson 812867a482dSJohn Dyson /* 813867a482dSJohn Dyson * Address of byte vector 814867a482dSJohn Dyson */ 81502c04a2fSJohn Dyson vec = uap->vec; 816867a482dSJohn Dyson 817190609ddSJohn Baldwin mtx_lock(&Giant); 818b40ce416SJulian Elischer map = &td->td_proc->p_vmspace->vm_map; 819b40ce416SJulian Elischer pmap = vmspace_pmap(td->td_proc->p_vmspace); 820867a482dSJohn Dyson 821eff50fcdSAlan Cox vm_map_lock_read(map); 822dd2622a8SAlan Cox RestartScan: 823dd2622a8SAlan Cox timestamp = map->timestamp; 824867a482dSJohn Dyson 825867a482dSJohn Dyson if (!vm_map_lookup_entry(map, addr, &entry)) 826867a482dSJohn Dyson entry = entry->next; 827867a482dSJohn Dyson 828867a482dSJohn Dyson /* 829867a482dSJohn Dyson * Do this on a map entry basis so that if the pages are not 830867a482dSJohn Dyson * in the current processes address space, we can easily look 831867a482dSJohn Dyson * up the pages elsewhere. 832867a482dSJohn Dyson */ 833867a482dSJohn Dyson lastvecindex = -1; 834867a482dSJohn Dyson for (current = entry; 835867a482dSJohn Dyson (current != &map->header) && (current->start < end); 836867a482dSJohn Dyson current = current->next) { 837867a482dSJohn Dyson 838867a482dSJohn Dyson /* 839867a482dSJohn Dyson * ignore submaps (for now) or null objects 840867a482dSJohn Dyson */ 8419fdfe602SMatthew Dillon if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) || 842867a482dSJohn Dyson current->object.vm_object == NULL) 843867a482dSJohn Dyson continue; 844867a482dSJohn Dyson 845867a482dSJohn Dyson /* 846867a482dSJohn Dyson * limit this scan to the current map entry and the 847867a482dSJohn Dyson * limits for the mincore call 848867a482dSJohn Dyson */ 849867a482dSJohn Dyson if (addr < current->start) 850867a482dSJohn Dyson addr = current->start; 851867a482dSJohn Dyson cend = current->end; 852867a482dSJohn Dyson if (cend > end) 853867a482dSJohn Dyson cend = end; 854867a482dSJohn Dyson 855867a482dSJohn Dyson /* 856867a482dSJohn Dyson * scan this entry one page at a time 857867a482dSJohn Dyson */ 858867a482dSJohn Dyson while (addr < cend) { 859867a482dSJohn Dyson /* 860867a482dSJohn Dyson * Check pmap first, it is likely faster, also 861867a482dSJohn Dyson * it can provide info as to whether we are the 862867a482dSJohn Dyson * one referencing or modifying the page. 863867a482dSJohn Dyson */ 864867a482dSJohn Dyson mincoreinfo = pmap_mincore(pmap, addr); 865867a482dSJohn Dyson if (!mincoreinfo) { 866867a482dSJohn Dyson vm_pindex_t pindex; 867867a482dSJohn Dyson vm_ooffset_t offset; 868867a482dSJohn Dyson vm_page_t m; 869867a482dSJohn Dyson /* 870867a482dSJohn Dyson * calculate the page index into the object 871867a482dSJohn Dyson */ 872867a482dSJohn Dyson offset = current->offset + (addr - current->start); 873867a482dSJohn Dyson pindex = OFF_TO_IDX(offset); 874867a482dSJohn Dyson m = vm_page_lookup(current->object.vm_object, 875867a482dSJohn Dyson pindex); 876867a482dSJohn Dyson /* 877867a482dSJohn Dyson * if the page is resident, then gather information about 878867a482dSJohn Dyson * it. 879867a482dSJohn Dyson */ 880867a482dSJohn Dyson if (m) { 881867a482dSJohn Dyson mincoreinfo = MINCORE_INCORE; 882867a482dSJohn Dyson if (m->dirty || 8830385347cSPeter Wemm pmap_is_modified(m)) 884867a482dSJohn Dyson mincoreinfo |= MINCORE_MODIFIED_OTHER; 885867a482dSJohn Dyson if ((m->flags & PG_REFERENCED) || 8860385347cSPeter Wemm pmap_ts_referenced(m)) { 887e69763a3SDoug Rabson vm_page_flag_set(m, PG_REFERENCED); 888867a482dSJohn Dyson mincoreinfo |= MINCORE_REFERENCED_OTHER; 88902c04a2fSJohn Dyson } 890867a482dSJohn Dyson } 8919b5a5d81SJohn Dyson } 892867a482dSJohn Dyson 893867a482dSJohn Dyson /* 894dd2622a8SAlan Cox * subyte may page fault. In case it needs to modify 895dd2622a8SAlan Cox * the map, we release the lock. 896dd2622a8SAlan Cox */ 897dd2622a8SAlan Cox vm_map_unlock_read(map); 898dd2622a8SAlan Cox 899dd2622a8SAlan Cox /* 900867a482dSJohn Dyson * calculate index into user supplied byte vector 901867a482dSJohn Dyson */ 902867a482dSJohn Dyson vecindex = OFF_TO_IDX(addr - first_addr); 903867a482dSJohn Dyson 904867a482dSJohn Dyson /* 905867a482dSJohn Dyson * If we have skipped map entries, we need to make sure that 906867a482dSJohn Dyson * the byte vector is zeroed for those skipped entries. 907867a482dSJohn Dyson */ 908867a482dSJohn Dyson while ((lastvecindex + 1) < vecindex) { 909867a482dSJohn Dyson error = subyte(vec + lastvecindex, 0); 910867a482dSJohn Dyson if (error) { 911d2c60af8SMatthew Dillon error = EFAULT; 912d2c60af8SMatthew Dillon goto done2; 913867a482dSJohn Dyson } 914867a482dSJohn Dyson ++lastvecindex; 915867a482dSJohn Dyson } 916867a482dSJohn Dyson 917867a482dSJohn Dyson /* 918867a482dSJohn Dyson * Pass the page information to the user 919867a482dSJohn Dyson */ 920867a482dSJohn Dyson error = subyte(vec + vecindex, mincoreinfo); 921867a482dSJohn Dyson if (error) { 922d2c60af8SMatthew Dillon error = EFAULT; 923d2c60af8SMatthew Dillon goto done2; 924867a482dSJohn Dyson } 925dd2622a8SAlan Cox 926dd2622a8SAlan Cox /* 927dd2622a8SAlan Cox * If the map has changed, due to the subyte, the previous 928dd2622a8SAlan Cox * output may be invalid. 929dd2622a8SAlan Cox */ 930dd2622a8SAlan Cox vm_map_lock_read(map); 931dd2622a8SAlan Cox if (timestamp != map->timestamp) 932dd2622a8SAlan Cox goto RestartScan; 933dd2622a8SAlan Cox 934867a482dSJohn Dyson lastvecindex = vecindex; 93502c04a2fSJohn Dyson addr += PAGE_SIZE; 93602c04a2fSJohn Dyson } 937867a482dSJohn Dyson } 938867a482dSJohn Dyson 939867a482dSJohn Dyson /* 940dd2622a8SAlan Cox * subyte may page fault. In case it needs to modify 941dd2622a8SAlan Cox * the map, we release the lock. 942dd2622a8SAlan Cox */ 943dd2622a8SAlan Cox vm_map_unlock_read(map); 944dd2622a8SAlan Cox 945dd2622a8SAlan Cox /* 946867a482dSJohn Dyson * Zero the last entries in the byte vector. 947867a482dSJohn Dyson */ 948867a482dSJohn Dyson vecindex = OFF_TO_IDX(end - first_addr); 949867a482dSJohn Dyson while ((lastvecindex + 1) < vecindex) { 950867a482dSJohn Dyson error = subyte(vec + lastvecindex, 0); 951867a482dSJohn Dyson if (error) { 952d2c60af8SMatthew Dillon error = EFAULT; 953d2c60af8SMatthew Dillon goto done2; 954867a482dSJohn Dyson } 955867a482dSJohn Dyson ++lastvecindex; 956867a482dSJohn Dyson } 957867a482dSJohn Dyson 958dd2622a8SAlan Cox /* 959dd2622a8SAlan Cox * If the map has changed, due to the subyte, the previous 960dd2622a8SAlan Cox * output may be invalid. 961dd2622a8SAlan Cox */ 962dd2622a8SAlan Cox vm_map_lock_read(map); 963dd2622a8SAlan Cox if (timestamp != map->timestamp) 964dd2622a8SAlan Cox goto RestartScan; 965eff50fcdSAlan Cox vm_map_unlock_read(map); 966d2c60af8SMatthew Dillon done2: 967190609ddSJohn Baldwin mtx_unlock(&Giant); 968d2c60af8SMatthew Dillon return (error); 969df8bae1dSRodney W. Grimes } 970df8bae1dSRodney W. Grimes 971d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 972df8bae1dSRodney W. Grimes struct mlock_args { 973651bb817SAlexander Langer const void *addr; 974df8bae1dSRodney W. Grimes size_t len; 975df8bae1dSRodney W. Grimes }; 976d2d3e875SBruce Evans #endif 977d2c60af8SMatthew Dillon /* 978d2c60af8SMatthew Dillon * MPSAFE 979d2c60af8SMatthew Dillon */ 980df8bae1dSRodney W. Grimes int 981b40ce416SJulian Elischer mlock(td, uap) 982b40ce416SJulian Elischer struct thread *td; 983df8bae1dSRodney W. Grimes struct mlock_args *uap; 984df8bae1dSRodney W. Grimes { 985df8bae1dSRodney W. Grimes vm_offset_t addr; 986dabee6feSPeter Wemm vm_size_t size, pageoff; 987df8bae1dSRodney W. Grimes int error; 988df8bae1dSRodney W. Grimes 989df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 9909154ee6aSPeter Wemm size = uap->len; 9919154ee6aSPeter Wemm 992dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 993dabee6feSPeter Wemm addr -= pageoff; 994dabee6feSPeter Wemm size += pageoff; 995dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 996dabee6feSPeter Wemm 997dabee6feSPeter Wemm /* disable wrap around */ 9989154ee6aSPeter Wemm if (addr + size < addr) 999df8bae1dSRodney W. Grimes return (EINVAL); 1000dabee6feSPeter Wemm 1001df8bae1dSRodney W. Grimes if (atop(size) + cnt.v_wire_count > vm_page_max_wired) 1002df8bae1dSRodney W. Grimes return (EAGAIN); 10039154ee6aSPeter Wemm 1004df8bae1dSRodney W. Grimes #ifdef pmap_wired_count 1005b40ce416SJulian Elischer if (size + ptoa(pmap_wired_count(vm_map_pmap(&td->td_proc->p_vmspace->vm_map))) > 1006b40ce416SJulian Elischer td->td_proc->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 10074a40e3d4SJohn Dyson return (ENOMEM); 1008df8bae1dSRodney W. Grimes #else 100944731cabSJohn Baldwin error = suser(td); 101005f0fdd2SPoul-Henning Kamp if (error) 1011df8bae1dSRodney W. Grimes return (error); 1012df8bae1dSRodney W. Grimes #endif 1013df8bae1dSRodney W. Grimes 1014190609ddSJohn Baldwin mtx_lock(&Giant); 10151d7cf06cSAlan Cox error = vm_map_wire(&td->td_proc->p_vmspace->vm_map, addr, 10161d7cf06cSAlan Cox addr + size, TRUE); 1017190609ddSJohn Baldwin mtx_unlock(&Giant); 1018df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 1019df8bae1dSRodney W. Grimes } 1020df8bae1dSRodney W. Grimes 1021d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 10224a40e3d4SJohn Dyson struct mlockall_args { 10234a40e3d4SJohn Dyson int how; 10244a40e3d4SJohn Dyson }; 10254a40e3d4SJohn Dyson #endif 10264a40e3d4SJohn Dyson 1027d2c60af8SMatthew Dillon /* 1028d2c60af8SMatthew Dillon * MPSAFE 1029d2c60af8SMatthew Dillon */ 10304a40e3d4SJohn Dyson int 1031b40ce416SJulian Elischer mlockall(td, uap) 1032b40ce416SJulian Elischer struct thread *td; 10334a40e3d4SJohn Dyson struct mlockall_args *uap; 10344a40e3d4SJohn Dyson { 10350cddd8f0SMatthew Dillon /* mtx_lock(&Giant); */ 10360cddd8f0SMatthew Dillon /* mtx_unlock(&Giant); */ 10374a40e3d4SJohn Dyson return 0; 10384a40e3d4SJohn Dyson } 10394a40e3d4SJohn Dyson 10404a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_ 1041fa721254SAlfred Perlstein struct munlockall_args { 10424a40e3d4SJohn Dyson int how; 10434a40e3d4SJohn Dyson }; 10444a40e3d4SJohn Dyson #endif 10454a40e3d4SJohn Dyson 1046d2c60af8SMatthew Dillon /* 1047d2c60af8SMatthew Dillon * MPSAFE 1048d2c60af8SMatthew Dillon */ 10494a40e3d4SJohn Dyson int 1050b40ce416SJulian Elischer munlockall(td, uap) 1051b40ce416SJulian Elischer struct thread *td; 10524a40e3d4SJohn Dyson struct munlockall_args *uap; 10534a40e3d4SJohn Dyson { 10540cddd8f0SMatthew Dillon /* mtx_lock(&Giant); */ 10550cddd8f0SMatthew Dillon /* mtx_unlock(&Giant); */ 10564a40e3d4SJohn Dyson return 0; 10574a40e3d4SJohn Dyson } 10584a40e3d4SJohn Dyson 10594a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_ 1060df8bae1dSRodney W. Grimes struct munlock_args { 1061651bb817SAlexander Langer const void *addr; 1062df8bae1dSRodney W. Grimes size_t len; 1063df8bae1dSRodney W. Grimes }; 1064d2d3e875SBruce Evans #endif 1065d2c60af8SMatthew Dillon /* 1066d2c60af8SMatthew Dillon * MPSAFE 1067d2c60af8SMatthew Dillon */ 1068df8bae1dSRodney W. Grimes int 1069b40ce416SJulian Elischer munlock(td, uap) 1070b40ce416SJulian Elischer struct thread *td; 1071df8bae1dSRodney W. Grimes struct munlock_args *uap; 1072df8bae1dSRodney W. Grimes { 1073df8bae1dSRodney W. Grimes vm_offset_t addr; 1074dabee6feSPeter Wemm vm_size_t size, pageoff; 1075df8bae1dSRodney W. Grimes int error; 1076df8bae1dSRodney W. Grimes 1077df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 10789154ee6aSPeter Wemm size = uap->len; 10799154ee6aSPeter Wemm 1080dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 1081dabee6feSPeter Wemm addr -= pageoff; 1082dabee6feSPeter Wemm size += pageoff; 1083dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 1084dabee6feSPeter Wemm 1085dabee6feSPeter Wemm /* disable wrap around */ 10869154ee6aSPeter Wemm if (addr + size < addr) 1087df8bae1dSRodney W. Grimes return (EINVAL); 1088dabee6feSPeter Wemm 1089df8bae1dSRodney W. Grimes #ifndef pmap_wired_count 109044731cabSJohn Baldwin error = suser(td); 109105f0fdd2SPoul-Henning Kamp if (error) 1092df8bae1dSRodney W. Grimes return (error); 1093df8bae1dSRodney W. Grimes #endif 1094df8bae1dSRodney W. Grimes 1095190609ddSJohn Baldwin mtx_lock(&Giant); 10961d7cf06cSAlan Cox error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, addr, 109723955314SAlfred Perlstein addr + size, TRUE); 1098190609ddSJohn Baldwin mtx_unlock(&Giant); 1099df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 1100df8bae1dSRodney W. Grimes } 1101df8bae1dSRodney W. Grimes 1102df8bae1dSRodney W. Grimes /* 1103d2c60af8SMatthew Dillon * vm_mmap() 1104d2c60af8SMatthew Dillon * 1105d2c60af8SMatthew Dillon * MPSAFE 1106d2c60af8SMatthew Dillon * 1107d2c60af8SMatthew Dillon * Internal version of mmap. Currently used by mmap, exec, and sys5 1108d2c60af8SMatthew Dillon * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON. 1109df8bae1dSRodney W. Grimes */ 1110df8bae1dSRodney W. Grimes int 1111b9dcd593SBruce Evans vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot, 1112b9dcd593SBruce Evans vm_prot_t maxprot, int flags, 1113651bb817SAlexander Langer void *handle, 1114b9dcd593SBruce Evans vm_ooffset_t foff) 1115df8bae1dSRodney W. Grimes { 1116df8bae1dSRodney W. Grimes boolean_t fitit; 1117fcae040bSJohn Dyson vm_object_t object; 1118df8bae1dSRodney W. Grimes struct vnode *vp = NULL; 111924a1cce3SDavid Greenman objtype_t type; 1120df8bae1dSRodney W. Grimes int rv = KERN_SUCCESS; 1121bd7e5f99SJohn Dyson vm_ooffset_t objsize; 1122bd7e5f99SJohn Dyson int docow; 1123b40ce416SJulian Elischer struct thread *td = curthread; 1124df8bae1dSRodney W. Grimes 1125df8bae1dSRodney W. Grimes if (size == 0) 1126df8bae1dSRodney W. Grimes return (0); 1127df8bae1dSRodney W. Grimes 112806cb7259SDavid Greenman objsize = size = round_page(size); 1129df8bae1dSRodney W. Grimes 1130df8bae1dSRodney W. Grimes /* 1131bc9ad247SDavid Greenman * We currently can only deal with page aligned file offsets. 1132bc9ad247SDavid Greenman * The check is here rather than in the syscall because the 1133bc9ad247SDavid Greenman * kernel calls this function internally for other mmaping 1134bc9ad247SDavid Greenman * operations (such as in exec) and non-aligned offsets will 1135bc9ad247SDavid Greenman * cause pmap inconsistencies...so we want to be sure to 1136bc9ad247SDavid Greenman * disallow this in all cases. 1137bc9ad247SDavid Greenman */ 1138bc9ad247SDavid Greenman if (foff & PAGE_MASK) 1139bc9ad247SDavid Greenman return (EINVAL); 1140bc9ad247SDavid Greenman 114106cb7259SDavid Greenman if ((flags & MAP_FIXED) == 0) { 114206cb7259SDavid Greenman fitit = TRUE; 114306cb7259SDavid Greenman *addr = round_page(*addr); 1144e4ca250dSJohn Baldwin mtx_lock(&Giant); 114506cb7259SDavid Greenman } else { 114606cb7259SDavid Greenman if (*addr != trunc_page(*addr)) 114706cb7259SDavid Greenman return (EINVAL); 114806cb7259SDavid Greenman fitit = FALSE; 1149e4ca250dSJohn Baldwin mtx_lock(&Giant); 115006cb7259SDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 115106cb7259SDavid Greenman } 115206cb7259SDavid Greenman 1153bc9ad247SDavid Greenman /* 115424a1cce3SDavid Greenman * Lookup/allocate object. 1155df8bae1dSRodney W. Grimes */ 11565f55e841SDavid Greenman if (flags & MAP_ANON) { 1157851c12ffSJohn Dyson type = OBJT_DEFAULT; 11585f55e841SDavid Greenman /* 11595f55e841SDavid Greenman * Unnamed anonymous regions always start at 0. 11605f55e841SDavid Greenman */ 116167bf6868SJohn Dyson if (handle == 0) 11625f55e841SDavid Greenman foff = 0; 11635f55e841SDavid Greenman } else { 1164df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 1165df8bae1dSRodney W. Grimes if (vp->v_type == VCHR) { 116624a1cce3SDavid Greenman type = OBJT_DEVICE; 1167a23d65bfSBruce Evans handle = (void *)(intptr_t)vp->v_rdev; 116806cb7259SDavid Greenman } else { 116906cb7259SDavid Greenman struct vattr vat; 117006cb7259SDavid Greenman int error; 117106cb7259SDavid Greenman 1172a854ed98SJohn Baldwin error = VOP_GETATTR(vp, &vat, td->td_ucred, td); 1173e4ca250dSJohn Baldwin if (error) { 117423955314SAlfred Perlstein mtx_unlock(&Giant); 117506cb7259SDavid Greenman return (error); 1176e4ca250dSJohn Baldwin } 1177bd7e5f99SJohn Dyson objsize = round_page(vat.va_size); 117824a1cce3SDavid Greenman type = OBJT_VNODE; 117900d76afeSGuido van Rooij /* 118000d76afeSGuido van Rooij * if it is a regular file without any references 118100d76afeSGuido van Rooij * we do not need to sync it. 118200d76afeSGuido van Rooij */ 118300d76afeSGuido van Rooij if (vp->v_type == VREG && vat.va_nlink == 0) { 118400d76afeSGuido van Rooij flags |= MAP_NOSYNC; 118500d76afeSGuido van Rooij } 1186df8bae1dSRodney W. Grimes } 118706cb7259SDavid Greenman } 118894328e90SJohn Dyson 118994328e90SJohn Dyson if (handle == NULL) { 119094328e90SJohn Dyson object = NULL; 11914738fa09SAlan Cox docow = 0; 119294328e90SJohn Dyson } else { 11930a0a85b3SJohn Dyson object = vm_pager_allocate(type, 11946cde7a16SDavid Greenman handle, objsize, prot, foff); 1195e4ca250dSJohn Baldwin if (object == NULL) { 1196e4ca250dSJohn Baldwin mtx_unlock(&Giant); 119724a1cce3SDavid Greenman return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 1198e4ca250dSJohn Baldwin } 11994738fa09SAlan Cox docow = MAP_PREFAULT_PARTIAL; 120094328e90SJohn Dyson } 1201df8bae1dSRodney W. Grimes 12025850152dSJohn Dyson /* 12038f2ec877SDavid Greenman * Force device mappings to be shared. 12045850152dSJohn Dyson */ 120524964514SPeter Wemm if (type == OBJT_DEVICE || type == OBJT_PHYS) { 12068f2ec877SDavid Greenman flags &= ~(MAP_PRIVATE|MAP_COPY); 12075850152dSJohn Dyson flags |= MAP_SHARED; 12088f2ec877SDavid Greenman } 12095850152dSJohn Dyson 12104f79d873SMatthew Dillon if ((flags & (MAP_ANON|MAP_SHARED)) == 0) 12114738fa09SAlan Cox docow |= MAP_COPY_ON_WRITE; 12124f79d873SMatthew Dillon if (flags & MAP_NOSYNC) 12134f79d873SMatthew Dillon docow |= MAP_DISABLE_SYNCER; 12149730a5daSPaul Saab if (flags & MAP_NOCORE) 12159730a5daSPaul Saab docow |= MAP_DISABLE_COREDUMP; 12165850152dSJohn Dyson 1217d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 1218d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 1219d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 1220d0aea04fSJohn Dyson 1221d0aea04fSJohn Dyson if (maxprot & VM_PROT_READ) 1222d0aea04fSJohn Dyson maxprot |= VM_PROT_EXECUTE; 1223d0aea04fSJohn Dyson #endif 1224d0aea04fSJohn Dyson 1225e4ca250dSJohn Baldwin if (fitit) 12260a0a85b3SJohn Dyson *addr = pmap_addr_hint(object, *addr, size); 12270a0a85b3SJohn Dyson 12282267af78SJulian Elischer if (flags & MAP_STACK) 12292267af78SJulian Elischer rv = vm_map_stack (map, *addr, size, prot, 12302267af78SJulian Elischer maxprot, docow); 12312267af78SJulian Elischer else 1232bd7e5f99SJohn Dyson rv = vm_map_find(map, object, foff, addr, size, fitit, 1233bd7e5f99SJohn Dyson prot, maxprot, docow); 1234bd7e5f99SJohn Dyson 1235d2c60af8SMatthew Dillon if (rv != KERN_SUCCESS) { 12367fb0c17eSDavid Greenman /* 123724a1cce3SDavid Greenman * Lose the object reference. Will destroy the 123824a1cce3SDavid Greenman * object if it's an unnamed anonymous mapping 123924a1cce3SDavid Greenman * or named anonymous without other references. 12407fb0c17eSDavid Greenman */ 1241df8bae1dSRodney W. Grimes vm_object_deallocate(object); 1242d2c60af8SMatthew Dillon } else if (flags & MAP_SHARED) { 1243df8bae1dSRodney W. Grimes /* 1244df8bae1dSRodney W. Grimes * Shared memory is also shared with children. 1245df8bae1dSRodney W. Grimes */ 1246df8bae1dSRodney W. Grimes rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 1247e4ca250dSJohn Baldwin if (rv != KERN_SUCCESS) 12487fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 1249df8bae1dSRodney W. Grimes } 1250e4ca250dSJohn Baldwin mtx_unlock(&Giant); 1251df8bae1dSRodney W. Grimes switch (rv) { 1252df8bae1dSRodney W. Grimes case KERN_SUCCESS: 1253df8bae1dSRodney W. Grimes return (0); 1254df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 1255df8bae1dSRodney W. Grimes case KERN_NO_SPACE: 1256df8bae1dSRodney W. Grimes return (ENOMEM); 1257df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 1258df8bae1dSRodney W. Grimes return (EACCES); 1259df8bae1dSRodney W. Grimes default: 1260df8bae1dSRodney W. Grimes return (EINVAL); 1261df8bae1dSRodney W. Grimes } 1262df8bae1dSRodney W. Grimes } 1263