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 48190609ddSJohn Baldwin #include "opt_bleed.h" 495591b823SEivind Eklund #include "opt_compat.h" 50e9822d92SJoerg Wunsch #include "opt_rlimit.h" 51e9822d92SJoerg Wunsch 52df8bae1dSRodney W. Grimes #include <sys/param.h> 53df8bae1dSRodney W. Grimes #include <sys/systm.h> 54fb919e4dSMark Murray #include <sys/kernel.h> 55fb919e4dSMark Murray #include <sys/lock.h> 5623955314SAlfred Perlstein #include <sys/mutex.h> 57d2d3e875SBruce Evans #include <sys/sysproto.h> 58df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 59df8bae1dSRodney W. Grimes #include <sys/proc.h> 60df8bae1dSRodney W. Grimes #include <sys/vnode.h> 613ac4d1efSBruce Evans #include <sys/fcntl.h> 62df8bae1dSRodney W. Grimes #include <sys/file.h> 63df8bae1dSRodney W. Grimes #include <sys/mman.h> 64df8bae1dSRodney W. Grimes #include <sys/conf.h> 654183b6b6SPeter Wemm #include <sys/stat.h> 66efeaf95aSDavid Greenman #include <sys/vmmeter.h> 671f6889a1SMatthew Dillon #include <sys/sysctl.h> 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes #include <vm/vm.h> 70efeaf95aSDavid Greenman #include <vm/vm_param.h> 71efeaf95aSDavid Greenman #include <vm/pmap.h> 72efeaf95aSDavid Greenman #include <vm/vm_map.h> 73efeaf95aSDavid Greenman #include <vm/vm_object.h> 741c7c3c6aSMatthew Dillon #include <vm/vm_page.h> 75df8bae1dSRodney W. Grimes #include <vm/vm_pager.h> 76b5e8ce9fSBruce Evans #include <vm/vm_pageout.h> 77efeaf95aSDavid Greenman #include <vm/vm_extern.h> 78867a482dSJohn Dyson #include <vm/vm_page.h> 791f6889a1SMatthew Dillon #include <vm/vm_kern.h> 80df8bae1dSRodney W. Grimes 81d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 82df8bae1dSRodney W. Grimes struct sbrk_args { 83df8bae1dSRodney W. Grimes int incr; 84df8bae1dSRodney W. Grimes }; 85d2d3e875SBruce Evans #endif 860d94caffSDavid Greenman 871f6889a1SMatthew Dillon static int max_proc_mmap; 881f6889a1SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, ""); 891f6889a1SMatthew Dillon 901f6889a1SMatthew Dillon /* 911f6889a1SMatthew Dillon * Set the maximum number of vm_map_entry structures per process. Roughly 921f6889a1SMatthew Dillon * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100 931f6889a1SMatthew Dillon * of our KVM malloc space still results in generous limits. We want a 941f6889a1SMatthew Dillon * default that is good enough to prevent the kernel running out of resources 951f6889a1SMatthew Dillon * if attacked from compromised user account but generous enough such that 961f6889a1SMatthew Dillon * multi-threaded processes are not unduly inconvenienced. 971f6889a1SMatthew Dillon */ 981f6889a1SMatthew Dillon 991f6889a1SMatthew Dillon static void vmmapentry_rsrc_init __P((void *)); 1001f6889a1SMatthew Dillon SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL) 1011f6889a1SMatthew Dillon 1021f6889a1SMatthew Dillon static void 1031f6889a1SMatthew Dillon vmmapentry_rsrc_init(dummy) 1041f6889a1SMatthew Dillon void *dummy; 1051f6889a1SMatthew Dillon { 1061f6889a1SMatthew Dillon max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry); 1071f6889a1SMatthew Dillon max_proc_mmap /= 100; 1081f6889a1SMatthew Dillon } 1091f6889a1SMatthew Dillon 110df8bae1dSRodney W. Grimes /* ARGSUSED */ 111df8bae1dSRodney W. Grimes int 112cb226aaaSPoul-Henning Kamp sbrk(p, uap) 113df8bae1dSRodney W. Grimes struct proc *p; 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 128df8bae1dSRodney W. Grimes /* ARGSUSED */ 129df8bae1dSRodney W. Grimes int 130cb226aaaSPoul-Henning Kamp sstk(p, uap) 131df8bae1dSRodney W. Grimes struct proc *p; 132df8bae1dSRodney W. Grimes struct sstk_args *uap; 133df8bae1dSRodney W. Grimes { 134df8bae1dSRodney W. Grimes /* Not yet implemented */ 1350cddd8f0SMatthew Dillon /* mtx_lock(&Giant); */ 1360cddd8f0SMatthew Dillon /* mtx_unlock(&Giant); */ 137df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 138df8bae1dSRodney W. Grimes } 139df8bae1dSRodney W. Grimes 140df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 141d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 142df8bae1dSRodney W. Grimes struct getpagesize_args { 143df8bae1dSRodney W. Grimes int dummy; 144df8bae1dSRodney W. Grimes }; 145d2d3e875SBruce Evans #endif 1460d94caffSDavid Greenman 147df8bae1dSRodney W. Grimes /* ARGSUSED */ 148df8bae1dSRodney W. Grimes int 149cb226aaaSPoul-Henning Kamp ogetpagesize(p, uap) 150df8bae1dSRodney W. Grimes struct proc *p; 151df8bae1dSRodney W. Grimes struct getpagesize_args *uap; 152df8bae1dSRodney W. Grimes { 1530cddd8f0SMatthew Dillon /* MP SAFE */ 154cb226aaaSPoul-Henning Kamp p->p_retval[0] = PAGE_SIZE; 155df8bae1dSRodney W. Grimes return (0); 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 158df8bae1dSRodney W. Grimes 15954f42e4bSPeter Wemm 16054f42e4bSPeter Wemm /* 16154f42e4bSPeter Wemm * Memory Map (mmap) system call. Note that the file offset 16254f42e4bSPeter Wemm * and address are allowed to be NOT page aligned, though if 16354f42e4bSPeter Wemm * the MAP_FIXED flag it set, both must have the same remainder 16454f42e4bSPeter Wemm * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not 16554f42e4bSPeter Wemm * page-aligned, the actual mapping starts at trunc_page(addr) 16654f42e4bSPeter Wemm * and the return value is adjusted up by the page offset. 167b4309055SMatthew Dillon * 168b4309055SMatthew Dillon * Generally speaking, only character devices which are themselves 169b4309055SMatthew Dillon * memory-based, such as a video framebuffer, can be mmap'd. Otherwise 170b4309055SMatthew Dillon * there would be no cache coherency between a descriptor and a VM mapping 171b4309055SMatthew Dillon * both to the same character device. 172b4309055SMatthew Dillon * 173b4309055SMatthew Dillon * Block devices can be mmap'd no matter what they represent. Cache coherency 174b4309055SMatthew Dillon * is maintained as long as you do not write directly to the underlying 175b4309055SMatthew Dillon * character device. 17654f42e4bSPeter Wemm */ 177d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 178df8bae1dSRodney W. Grimes struct mmap_args { 179651bb817SAlexander Langer void *addr; 180df8bae1dSRodney W. Grimes size_t len; 181df8bae1dSRodney W. Grimes int prot; 182df8bae1dSRodney W. Grimes int flags; 183df8bae1dSRodney W. Grimes int fd; 184df8bae1dSRodney W. Grimes long pad; 185df8bae1dSRodney W. Grimes off_t pos; 186df8bae1dSRodney W. Grimes }; 187d2d3e875SBruce Evans #endif 188df8bae1dSRodney W. Grimes 189df8bae1dSRodney W. Grimes int 190cb226aaaSPoul-Henning Kamp mmap(p, uap) 191df8bae1dSRodney W. Grimes struct proc *p; 192df8bae1dSRodney W. Grimes register struct mmap_args *uap; 193df8bae1dSRodney W. Grimes { 194df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 195279d7226SMatthew Dillon register struct file *fp = NULL; 196df8bae1dSRodney W. Grimes struct vnode *vp; 197df8bae1dSRodney W. Grimes vm_offset_t addr; 1989154ee6aSPeter Wemm vm_size_t size, pageoff; 199df8bae1dSRodney W. Grimes vm_prot_t prot, maxprot; 200651bb817SAlexander Langer void *handle; 201df8bae1dSRodney W. Grimes int flags, error; 202c8bdd56bSGuido van Rooij int disablexworkaround; 20354f42e4bSPeter Wemm off_t pos; 2041f6889a1SMatthew Dillon struct vmspace *vms = p->p_vmspace; 2059ff5ce6bSBoris Popov vm_object_t obj; 206df8bae1dSRodney W. Grimes 20754f42e4bSPeter Wemm addr = (vm_offset_t) uap->addr; 20854f42e4bSPeter Wemm size = uap->len; 209df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 210df8bae1dSRodney W. Grimes flags = uap->flags; 21154f42e4bSPeter Wemm pos = uap->pos; 21254f42e4bSPeter Wemm 21354f42e4bSPeter Wemm /* make sure mapping fits into numeric range etc */ 214fc565456SDmitrij Tejblum if ((ssize_t) uap->len < 0 || 21554f42e4bSPeter Wemm ((flags & MAP_ANON) && uap->fd != -1)) 216df8bae1dSRodney W. Grimes return (EINVAL); 2179154ee6aSPeter Wemm 2182267af78SJulian Elischer if (flags & MAP_STACK) { 2192267af78SJulian Elischer if ((uap->fd != -1) || 2202267af78SJulian Elischer ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE))) 2212267af78SJulian Elischer return (EINVAL); 2222267af78SJulian Elischer flags |= MAP_ANON; 2232267af78SJulian Elischer pos = 0; 2242907af2aSJulian Elischer } 2252907af2aSJulian Elischer 2269154ee6aSPeter Wemm /* 22754f42e4bSPeter Wemm * Align the file position to a page boundary, 22854f42e4bSPeter Wemm * and save its page offset component. 2299154ee6aSPeter Wemm */ 23054f42e4bSPeter Wemm pageoff = (pos & PAGE_MASK); 23154f42e4bSPeter Wemm pos -= pageoff; 23254f42e4bSPeter Wemm 23354f42e4bSPeter Wemm /* Adjust size for rounding (on both ends). */ 23454f42e4bSPeter Wemm size += pageoff; /* low end... */ 23554f42e4bSPeter Wemm size = (vm_size_t) round_page(size); /* hi end */ 2369154ee6aSPeter Wemm 237df8bae1dSRodney W. Grimes /* 2380d94caffSDavid Greenman * Check for illegal addresses. Watch out for address wrap... Note 2390d94caffSDavid Greenman * that VM_*_ADDRESS are not constants due to casts (argh). 240df8bae1dSRodney W. Grimes */ 241df8bae1dSRodney W. Grimes if (flags & MAP_FIXED) { 24254f42e4bSPeter Wemm /* 24354f42e4bSPeter Wemm * The specified address must have the same remainder 24454f42e4bSPeter Wemm * as the file offset taken modulo PAGE_SIZE, so it 24554f42e4bSPeter Wemm * should be aligned after adjustment by pageoff. 24654f42e4bSPeter Wemm */ 24754f42e4bSPeter Wemm addr -= pageoff; 24854f42e4bSPeter Wemm if (addr & PAGE_MASK) 24954f42e4bSPeter Wemm return (EINVAL); 25054f42e4bSPeter Wemm /* Address range must be all in user VM space. */ 251bbc0ec52SDavid Greenman if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 252df8bae1dSRodney W. Grimes return (EINVAL); 25326f9a767SRodney W. Grimes #ifndef i386 254df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 255df8bae1dSRodney W. Grimes return (EINVAL); 25626f9a767SRodney W. Grimes #endif 257bbc0ec52SDavid Greenman if (addr + size < addr) 258df8bae1dSRodney W. Grimes return (EINVAL); 259df8bae1dSRodney W. Grimes } 260df8bae1dSRodney W. Grimes /* 26154f42e4bSPeter Wemm * XXX for non-fixed mappings where no hint is provided or 26254f42e4bSPeter Wemm * the hint would fall in the potential heap space, 26354f42e4bSPeter Wemm * place it after the end of the largest possible heap. 264df8bae1dSRodney W. Grimes * 26554f42e4bSPeter Wemm * There should really be a pmap call to determine a reasonable 26654f42e4bSPeter Wemm * location. 267df8bae1dSRodney W. Grimes */ 268d28ab90fSLuoqi Chen else if (addr == 0 || 2691f6889a1SMatthew Dillon (addr >= round_page((vm_offset_t)vms->vm_taddr) && 2701f6889a1SMatthew Dillon addr < round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ))) 2711f6889a1SMatthew Dillon addr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ); 27254f42e4bSPeter Wemm 2730cddd8f0SMatthew Dillon mtx_lock(&Giant); /* syscall marked mp-safe but isn't */ 274df8bae1dSRodney W. Grimes if (flags & MAP_ANON) { 275df8bae1dSRodney W. Grimes /* 276df8bae1dSRodney W. Grimes * Mapping blank space is trivial. 277df8bae1dSRodney W. Grimes */ 278df8bae1dSRodney W. Grimes handle = NULL; 279df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 28054f42e4bSPeter Wemm pos = 0; 281df8bae1dSRodney W. Grimes } else { 282df8bae1dSRodney W. Grimes /* 2830d94caffSDavid Greenman * Mapping file, get fp for validation. Obtain vnode and make 2840d94caffSDavid Greenman * sure it is of appropriate type. 285df8bae1dSRodney W. Grimes */ 286df8bae1dSRodney W. Grimes if (((unsigned) uap->fd) >= fdp->fd_nfiles || 287e4ca250dSJohn Baldwin (fp = fdp->fd_ofiles[uap->fd]) == NULL) { 288e4ca250dSJohn Baldwin mtx_unlock(&Giant); 289df8bae1dSRodney W. Grimes return (EBADF); 290e4ca250dSJohn Baldwin } 291e4ca250dSJohn Baldwin if (fp->f_type != DTYPE_VNODE) { 292e4ca250dSJohn Baldwin mtx_unlock(&Giant); 293df8bae1dSRodney W. Grimes return (EINVAL); 294e4ca250dSJohn Baldwin } 295279d7226SMatthew Dillon 296279d7226SMatthew Dillon /* 297279d7226SMatthew Dillon * don't let the descriptor disappear on us if we block 298279d7226SMatthew Dillon */ 299279d7226SMatthew Dillon fhold(fp); 300279d7226SMatthew Dillon 301aa543039SGarrett Wollman /* 302aa543039SGarrett Wollman * POSIX shared-memory objects are defined to have 303aa543039SGarrett Wollman * kernel persistence, and are not defined to support 304aa543039SGarrett Wollman * read(2)/write(2) -- or even open(2). Thus, we can 305aa543039SGarrett Wollman * use MAP_ASYNC to trade on-disk coherence for speed. 306aa543039SGarrett Wollman * The shm_open(3) library routine turns on the FPOSIXSHM 307aa543039SGarrett Wollman * flag to request this behavior. 308aa543039SGarrett Wollman */ 309aa543039SGarrett Wollman if (fp->f_flag & FPOSIXSHM) 310aa543039SGarrett Wollman flags |= MAP_NOSYNC; 311df8bae1dSRodney W. Grimes vp = (struct vnode *) fp->f_data; 312e4ca250dSJohn Baldwin if (vp->v_type != VREG && vp->v_type != VCHR) { 313e4ca250dSJohn Baldwin error = EINVAL; 314e4ca250dSJohn Baldwin goto done; 315e4ca250dSJohn Baldwin } 3169ff5ce6bSBoris Popov if (vp->v_type == VREG) { 3179ff5ce6bSBoris Popov /* 3189ff5ce6bSBoris Popov * Get the proper underlying object 3199ff5ce6bSBoris Popov */ 3200cddd8f0SMatthew Dillon if (VOP_GETVOBJECT(vp, &obj) != 0) { 3210cddd8f0SMatthew Dillon error = EINVAL; 3220cddd8f0SMatthew Dillon goto done; 3230cddd8f0SMatthew Dillon } 3249ff5ce6bSBoris Popov vp = (struct vnode*)obj->handle; 3259ff5ce6bSBoris Popov } 326df8bae1dSRodney W. Grimes /* 3270d94caffSDavid Greenman * XXX hack to handle use of /dev/zero to map anon memory (ala 3280d94caffSDavid Greenman * SunOS). 329df8bae1dSRodney W. Grimes */ 3302589f249SMark Murray if ((vp->v_type == VCHR) && 3312589f249SMark Murray (vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON)) { 332df8bae1dSRodney W. Grimes handle = NULL; 333df8bae1dSRodney W. Grimes maxprot = VM_PROT_ALL; 334df8bae1dSRodney W. Grimes flags |= MAP_ANON; 33554f42e4bSPeter Wemm pos = 0; 336df8bae1dSRodney W. Grimes } else { 337df8bae1dSRodney W. Grimes /* 338c8bdd56bSGuido van Rooij * cdevs does not provide private mappings of any kind. 339c8bdd56bSGuido van Rooij */ 340c8bdd56bSGuido van Rooij /* 341c8bdd56bSGuido van Rooij * However, for XIG X server to continue to work, 342c8bdd56bSGuido van Rooij * we should allow the superuser to do it anyway. 343c8bdd56bSGuido van Rooij * We only allow it at securelevel < 1. 344c8bdd56bSGuido van Rooij * (Because the XIG X server writes directly to video 345c8bdd56bSGuido van Rooij * memory via /dev/mem, it should never work at any 346c8bdd56bSGuido van Rooij * other securelevel. 347c8bdd56bSGuido van Rooij * XXX this will have to go 348c8bdd56bSGuido van Rooij */ 349c8bdd56bSGuido van Rooij if (securelevel >= 1) 350c8bdd56bSGuido van Rooij disablexworkaround = 1; 351c8bdd56bSGuido van Rooij else 352f711d546SPoul-Henning Kamp disablexworkaround = suser(p); 353c8bdd56bSGuido van Rooij if (vp->v_type == VCHR && disablexworkaround && 354279d7226SMatthew Dillon (flags & (MAP_PRIVATE|MAP_COPY))) { 355279d7226SMatthew Dillon error = EINVAL; 356279d7226SMatthew Dillon goto done; 357279d7226SMatthew Dillon } 358c8bdd56bSGuido van Rooij /* 359df8bae1dSRodney W. Grimes * Ensure that file and memory protections are 360df8bae1dSRodney W. Grimes * compatible. Note that we only worry about 361df8bae1dSRodney W. Grimes * writability if mapping is shared; in this case, 362df8bae1dSRodney W. Grimes * current and max prot are dictated by the open file. 363df8bae1dSRodney W. Grimes * XXX use the vnode instead? Problem is: what 3640d94caffSDavid Greenman * credentials do we use for determination? What if 3650d94caffSDavid Greenman * proc does a setuid? 366df8bae1dSRodney W. Grimes */ 367df8bae1dSRodney W. Grimes maxprot = VM_PROT_EXECUTE; /* ??? */ 368279d7226SMatthew Dillon if (fp->f_flag & FREAD) { 369df8bae1dSRodney W. Grimes maxprot |= VM_PROT_READ; 370279d7226SMatthew Dillon } else if (prot & PROT_READ) { 371279d7226SMatthew Dillon error = EACCES; 372279d7226SMatthew Dillon goto done; 373279d7226SMatthew Dillon } 374c8bdd56bSGuido van Rooij /* 375c8bdd56bSGuido van Rooij * If we are sharing potential changes (either via 376c8bdd56bSGuido van Rooij * MAP_SHARED or via the implicit sharing of character 377c8bdd56bSGuido van Rooij * device mappings), and we are trying to get write 378c8bdd56bSGuido van Rooij * permission although we opened it without asking 379c8bdd56bSGuido van Rooij * for it, bail out. Check for superuser, only if 380c8bdd56bSGuido van Rooij * we're at securelevel < 1, to allow the XIG X server 381c8bdd56bSGuido van Rooij * to continue to work. 382c8bdd56bSGuido van Rooij */ 38305feb99fSGuido van Rooij 38405feb99fSGuido van Rooij if ((flags & MAP_SHARED) != 0 || 38505feb99fSGuido van Rooij (vp->v_type == VCHR && disablexworkaround)) { 38605feb99fSGuido van Rooij if ((fp->f_flag & FWRITE) != 0) { 3874183b6b6SPeter Wemm struct vattr va; 38805feb99fSGuido van Rooij if ((error = 38905feb99fSGuido van Rooij VOP_GETATTR(vp, &va, 390279d7226SMatthew Dillon p->p_ucred, p))) { 391279d7226SMatthew Dillon goto done; 392279d7226SMatthew Dillon } 39305feb99fSGuido van Rooij if ((va.va_flags & 394279d7226SMatthew Dillon (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0) { 395df8bae1dSRodney W. Grimes maxprot |= VM_PROT_WRITE; 396279d7226SMatthew Dillon } else if (prot & PROT_WRITE) { 397279d7226SMatthew Dillon error = EPERM; 398279d7226SMatthew Dillon goto done; 399279d7226SMatthew Dillon } 400279d7226SMatthew Dillon } else if ((prot & PROT_WRITE) != 0) { 401279d7226SMatthew Dillon error = EACCES; 402279d7226SMatthew Dillon goto done; 403279d7226SMatthew Dillon } 404279d7226SMatthew Dillon } else { 40505feb99fSGuido van Rooij maxprot |= VM_PROT_WRITE; 406279d7226SMatthew Dillon } 40705feb99fSGuido van Rooij 408651bb817SAlexander Langer handle = (void *)vp; 409df8bae1dSRodney W. Grimes } 410df8bae1dSRodney W. Grimes } 4111f6889a1SMatthew Dillon 4121f6889a1SMatthew Dillon /* 4131f6889a1SMatthew Dillon * Do not allow more then a certain number of vm_map_entry structures 4141f6889a1SMatthew Dillon * per process. Scale with the number of rforks sharing the map 4151f6889a1SMatthew Dillon * to make the limit reasonable for threads. 4161f6889a1SMatthew Dillon */ 4171f6889a1SMatthew Dillon if (max_proc_mmap && 4181f6889a1SMatthew Dillon vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) { 419279d7226SMatthew Dillon error = ENOMEM; 420279d7226SMatthew Dillon goto done; 4211f6889a1SMatthew Dillon } 4221f6889a1SMatthew Dillon 423e4ca250dSJohn Baldwin mtx_unlock(&Giant); 4241f6889a1SMatthew Dillon error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot, 42554f42e4bSPeter Wemm flags, handle, pos); 426df8bae1dSRodney W. Grimes if (error == 0) 427711458e3SDoug Rabson p->p_retval[0] = (register_t) (addr + pageoff); 428e4ca250dSJohn Baldwin mtx_lock(&Giant); 429279d7226SMatthew Dillon done: 430279d7226SMatthew Dillon if (fp) 431279d7226SMatthew Dillon fdrop(fp, p); 432e4ca250dSJohn Baldwin mtx_unlock(&Giant); 433df8bae1dSRodney W. Grimes return (error); 434df8bae1dSRodney W. Grimes } 435df8bae1dSRodney W. Grimes 43605f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43 437d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 43805f0fdd2SPoul-Henning Kamp struct ommap_args { 43905f0fdd2SPoul-Henning Kamp caddr_t addr; 44005f0fdd2SPoul-Henning Kamp int len; 44105f0fdd2SPoul-Henning Kamp int prot; 44205f0fdd2SPoul-Henning Kamp int flags; 44305f0fdd2SPoul-Henning Kamp int fd; 44405f0fdd2SPoul-Henning Kamp long pos; 44505f0fdd2SPoul-Henning Kamp }; 446d2d3e875SBruce Evans #endif 44705f0fdd2SPoul-Henning Kamp int 448cb226aaaSPoul-Henning Kamp ommap(p, uap) 44905f0fdd2SPoul-Henning Kamp struct proc *p; 45005f0fdd2SPoul-Henning Kamp register struct ommap_args *uap; 45105f0fdd2SPoul-Henning Kamp { 45205f0fdd2SPoul-Henning Kamp struct mmap_args nargs; 45305f0fdd2SPoul-Henning Kamp static const char cvtbsdprot[8] = { 45405f0fdd2SPoul-Henning Kamp 0, 45505f0fdd2SPoul-Henning Kamp PROT_EXEC, 45605f0fdd2SPoul-Henning Kamp PROT_WRITE, 45705f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE, 45805f0fdd2SPoul-Henning Kamp PROT_READ, 45905f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_READ, 46005f0fdd2SPoul-Henning Kamp PROT_WRITE | PROT_READ, 46105f0fdd2SPoul-Henning Kamp PROT_EXEC | PROT_WRITE | PROT_READ, 46205f0fdd2SPoul-Henning Kamp }; 4630d94caffSDavid Greenman 46405f0fdd2SPoul-Henning Kamp #define OMAP_ANON 0x0002 46505f0fdd2SPoul-Henning Kamp #define OMAP_COPY 0x0020 46605f0fdd2SPoul-Henning Kamp #define OMAP_SHARED 0x0010 46705f0fdd2SPoul-Henning Kamp #define OMAP_FIXED 0x0100 46805f0fdd2SPoul-Henning Kamp #define OMAP_INHERIT 0x0800 46905f0fdd2SPoul-Henning Kamp 47005f0fdd2SPoul-Henning Kamp nargs.addr = uap->addr; 47105f0fdd2SPoul-Henning Kamp nargs.len = uap->len; 47205f0fdd2SPoul-Henning Kamp nargs.prot = cvtbsdprot[uap->prot & 0x7]; 47305f0fdd2SPoul-Henning Kamp nargs.flags = 0; 47405f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_ANON) 47505f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_ANON; 47605f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_COPY) 47705f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_COPY; 47805f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_SHARED) 47905f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_SHARED; 48005f0fdd2SPoul-Henning Kamp else 48105f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_PRIVATE; 48205f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_FIXED) 48305f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_FIXED; 48405f0fdd2SPoul-Henning Kamp if (uap->flags & OMAP_INHERIT) 48505f0fdd2SPoul-Henning Kamp nargs.flags |= MAP_INHERIT; 48605f0fdd2SPoul-Henning Kamp nargs.fd = uap->fd; 48705f0fdd2SPoul-Henning Kamp nargs.pos = uap->pos; 488cb226aaaSPoul-Henning Kamp return (mmap(p, &nargs)); 48905f0fdd2SPoul-Henning Kamp } 49005f0fdd2SPoul-Henning Kamp #endif /* COMPAT_43 */ 49105f0fdd2SPoul-Henning Kamp 49205f0fdd2SPoul-Henning Kamp 493d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 494df8bae1dSRodney W. Grimes struct msync_args { 495651bb817SAlexander Langer void *addr; 496df8bae1dSRodney W. Grimes int len; 497e6c6af11SDavid Greenman int flags; 498df8bae1dSRodney W. Grimes }; 499d2d3e875SBruce Evans #endif 500df8bae1dSRodney W. Grimes int 501cb226aaaSPoul-Henning Kamp msync(p, uap) 502df8bae1dSRodney W. Grimes struct proc *p; 503df8bae1dSRodney W. Grimes struct msync_args *uap; 504df8bae1dSRodney W. Grimes { 505df8bae1dSRodney W. Grimes vm_offset_t addr; 506dabee6feSPeter Wemm vm_size_t size, pageoff; 507e6c6af11SDavid Greenman int flags; 508df8bae1dSRodney W. Grimes vm_map_t map; 509df8bae1dSRodney W. Grimes int rv; 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 5129154ee6aSPeter Wemm size = uap->len; 513e6c6af11SDavid Greenman flags = uap->flags; 514e6c6af11SDavid Greenman 515dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 516dabee6feSPeter Wemm addr -= pageoff; 517dabee6feSPeter Wemm size += pageoff; 518dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 5199154ee6aSPeter Wemm if (addr + size < addr) 520dabee6feSPeter Wemm return(EINVAL); 521dabee6feSPeter Wemm 522dabee6feSPeter Wemm if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 5231e62bc63SDavid Greenman return (EINVAL); 5241e62bc63SDavid Greenman 5250cddd8f0SMatthew Dillon mtx_lock(&Giant); 5260cddd8f0SMatthew Dillon 5279154ee6aSPeter Wemm map = &p->p_vmspace->vm_map; 5289154ee6aSPeter Wemm 529df8bae1dSRodney W. Grimes /* 530df8bae1dSRodney W. Grimes * XXX Gak! If size is zero we are supposed to sync "all modified 5310d94caffSDavid Greenman * pages with the region containing addr". Unfortunately, we don't 5320d94caffSDavid Greenman * really keep track of individual mmaps so we approximate by flushing 5330d94caffSDavid Greenman * the range of the map entry containing addr. This can be incorrect 5340d94caffSDavid Greenman * if the region splits or is coalesced with a neighbor. 535df8bae1dSRodney W. Grimes */ 536df8bae1dSRodney W. Grimes if (size == 0) { 537df8bae1dSRodney W. Grimes vm_map_entry_t entry; 538df8bae1dSRodney W. Grimes 539df8bae1dSRodney W. Grimes vm_map_lock_read(map); 540df8bae1dSRodney W. Grimes rv = vm_map_lookup_entry(map, addr, &entry); 541df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 54223955314SAlfred Perlstein if (rv == FALSE) { 543190609ddSJohn Baldwin mtx_unlock(&Giant); 544df8bae1dSRodney W. Grimes return (EINVAL); 54523955314SAlfred Perlstein } 546df8bae1dSRodney W. Grimes addr = entry->start; 547df8bae1dSRodney W. Grimes size = entry->end - entry->start; 548df8bae1dSRodney W. Grimes } 549e6c6af11SDavid Greenman 550df8bae1dSRodney W. Grimes /* 551df8bae1dSRodney W. Grimes * Clean the pages and interpret the return value. 552df8bae1dSRodney W. Grimes */ 5536c534ad8SDavid Greenman rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 554e6c6af11SDavid Greenman (flags & MS_INVALIDATE) != 0); 555e6c6af11SDavid Greenman 556190609ddSJohn Baldwin mtx_unlock(&Giant); 5570cddd8f0SMatthew Dillon 558df8bae1dSRodney W. Grimes switch (rv) { 559df8bae1dSRodney W. Grimes case KERN_SUCCESS: 560df8bae1dSRodney W. Grimes break; 561df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 562df8bae1dSRodney W. Grimes return (EINVAL); /* Sun returns ENOMEM? */ 563df8bae1dSRodney W. Grimes case KERN_FAILURE: 564df8bae1dSRodney W. Grimes return (EIO); 565df8bae1dSRodney W. Grimes default: 566df8bae1dSRodney W. Grimes return (EINVAL); 567df8bae1dSRodney W. Grimes } 568e6c6af11SDavid Greenman 569df8bae1dSRodney W. Grimes return (0); 570df8bae1dSRodney W. Grimes } 571df8bae1dSRodney W. Grimes 572d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 573df8bae1dSRodney W. Grimes struct munmap_args { 574651bb817SAlexander Langer void *addr; 5759154ee6aSPeter Wemm size_t len; 576df8bae1dSRodney W. Grimes }; 577d2d3e875SBruce Evans #endif 578df8bae1dSRodney W. Grimes int 579cb226aaaSPoul-Henning Kamp munmap(p, uap) 580df8bae1dSRodney W. Grimes register struct proc *p; 581df8bae1dSRodney W. Grimes register 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); 60626f9a767SRodney W. Grimes #ifndef i386 607df8bae1dSRodney W. Grimes if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS) 608df8bae1dSRodney W. Grimes return (EINVAL); 60926f9a767SRodney W. Grimes #endif 6100cddd8f0SMatthew Dillon mtx_lock(&Giant); 611df8bae1dSRodney W. Grimes map = &p->p_vmspace->vm_map; 612df8bae1dSRodney W. Grimes /* 613df8bae1dSRodney W. Grimes * Make sure entire range is allocated. 614df8bae1dSRodney W. Grimes */ 61523955314SAlfred Perlstein if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) { 616e4ca250dSJohn Baldwin mtx_unlock(&Giant); 617df8bae1dSRodney W. Grimes return (EINVAL); 61823955314SAlfred Perlstein } 619df8bae1dSRodney W. Grimes /* returns nothing but KERN_SUCCESS anyway */ 620df8bae1dSRodney W. Grimes (void) vm_map_remove(map, addr, addr + size); 621e4ca250dSJohn Baldwin mtx_unlock(&Giant); 622df8bae1dSRodney W. Grimes return (0); 623df8bae1dSRodney W. Grimes } 624df8bae1dSRodney W. Grimes 625279d7226SMatthew Dillon #if 0 626df8bae1dSRodney W. Grimes void 62790324b07SDavid Greenman munmapfd(p, fd) 62890324b07SDavid Greenman struct proc *p; 629df8bae1dSRodney W. Grimes int fd; 630df8bae1dSRodney W. Grimes { 631df8bae1dSRodney W. Grimes /* 632c4ed5a07SDavid Greenman * XXX should unmap any regions mapped to this file 633df8bae1dSRodney W. Grimes */ 63490324b07SDavid Greenman p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED; 635df8bae1dSRodney W. Grimes } 636279d7226SMatthew Dillon #endif 637df8bae1dSRodney W. Grimes 638d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 639df8bae1dSRodney W. Grimes struct mprotect_args { 640651bb817SAlexander Langer const void *addr; 6419154ee6aSPeter Wemm size_t len; 642df8bae1dSRodney W. Grimes int prot; 643df8bae1dSRodney W. Grimes }; 644d2d3e875SBruce Evans #endif 645df8bae1dSRodney W. Grimes int 646cb226aaaSPoul-Henning Kamp mprotect(p, uap) 647df8bae1dSRodney W. Grimes struct proc *p; 648df8bae1dSRodney W. Grimes struct mprotect_args *uap; 649df8bae1dSRodney W. Grimes { 650df8bae1dSRodney W. Grimes vm_offset_t addr; 651dabee6feSPeter Wemm vm_size_t size, pageoff; 652df8bae1dSRodney W. Grimes register vm_prot_t prot; 65323955314SAlfred Perlstein int ret; 654df8bae1dSRodney W. Grimes 655df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 6569154ee6aSPeter Wemm size = uap->len; 657df8bae1dSRodney W. Grimes prot = uap->prot & VM_PROT_ALL; 658d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 659d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 660d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 661d0aea04fSJohn Dyson #endif 662df8bae1dSRodney W. Grimes 663dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 664dabee6feSPeter Wemm addr -= pageoff; 665dabee6feSPeter Wemm size += pageoff; 666dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 6679154ee6aSPeter Wemm if (addr + size < addr) 668dabee6feSPeter Wemm return(EINVAL); 669dabee6feSPeter Wemm 670e4ca250dSJohn Baldwin mtx_lock(&Giant); 67123955314SAlfred Perlstein ret = vm_map_protect(&p->p_vmspace->vm_map, addr, 67223955314SAlfred Perlstein addr + size, prot, FALSE); 673e4ca250dSJohn Baldwin mtx_unlock(&Giant); 67423955314SAlfred Perlstein switch (ret) { 675df8bae1dSRodney W. Grimes case KERN_SUCCESS: 676df8bae1dSRodney W. Grimes return (0); 677df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 678df8bae1dSRodney W. Grimes return (EACCES); 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes return (EINVAL); 681df8bae1dSRodney W. Grimes } 682df8bae1dSRodney W. Grimes 683d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 684dabee6feSPeter Wemm struct minherit_args { 685651bb817SAlexander Langer void *addr; 6869154ee6aSPeter Wemm size_t len; 687dabee6feSPeter Wemm int inherit; 688dabee6feSPeter Wemm }; 689dabee6feSPeter Wemm #endif 690dabee6feSPeter Wemm int 691cb226aaaSPoul-Henning Kamp minherit(p, uap) 692dabee6feSPeter Wemm struct proc *p; 693dabee6feSPeter Wemm struct minherit_args *uap; 694dabee6feSPeter Wemm { 695dabee6feSPeter Wemm vm_offset_t addr; 696dabee6feSPeter Wemm vm_size_t size, pageoff; 697dabee6feSPeter Wemm register vm_inherit_t inherit; 69823955314SAlfred Perlstein int ret; 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 711190609ddSJohn Baldwin mtx_lock(&Giant); 71223955314SAlfred Perlstein ret = vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size, 71323955314SAlfred Perlstein inherit); 714190609ddSJohn Baldwin mtx_unlock(&Giant); 71523955314SAlfred Perlstein 71623955314SAlfred Perlstein switch (ret) { 717dabee6feSPeter Wemm case KERN_SUCCESS: 718dabee6feSPeter Wemm return (0); 719dabee6feSPeter Wemm case KERN_PROTECTION_FAILURE: 720dabee6feSPeter Wemm return (EACCES); 721dabee6feSPeter Wemm } 722dabee6feSPeter Wemm return (EINVAL); 723dabee6feSPeter Wemm } 724dabee6feSPeter Wemm 725dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 726df8bae1dSRodney W. Grimes struct madvise_args { 727651bb817SAlexander Langer void *addr; 7289154ee6aSPeter Wemm size_t len; 729df8bae1dSRodney W. Grimes int behav; 730df8bae1dSRodney W. Grimes }; 731d2d3e875SBruce Evans #endif 7320d94caffSDavid Greenman 733df8bae1dSRodney W. Grimes /* ARGSUSED */ 734df8bae1dSRodney W. Grimes int 735cb226aaaSPoul-Henning Kamp madvise(p, uap) 736df8bae1dSRodney W. Grimes struct proc *p; 737df8bae1dSRodney W. Grimes struct madvise_args *uap; 738df8bae1dSRodney W. Grimes { 739f35329acSJohn Dyson vm_offset_t start, end; 74023955314SAlfred Perlstein int ret; 741b4309055SMatthew Dillon 742b4309055SMatthew Dillon /* 743b4309055SMatthew Dillon * Check for illegal behavior 744b4309055SMatthew Dillon */ 7459730a5daSPaul Saab if (uap->behav < 0 || uap->behav > MADV_CORE) 746b4309055SMatthew Dillon return (EINVAL); 747867a482dSJohn Dyson /* 748867a482dSJohn Dyson * Check for illegal addresses. Watch out for address wrap... Note 749867a482dSJohn Dyson * that VM_*_ADDRESS are not constants due to casts (argh). 750867a482dSJohn Dyson */ 751867a482dSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && 752867a482dSJohn Dyson ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS) 753867a482dSJohn Dyson return (EINVAL); 754867a482dSJohn Dyson #ifndef i386 755867a482dSJohn Dyson if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS) 756867a482dSJohn Dyson return (EINVAL); 757867a482dSJohn Dyson #endif 758867a482dSJohn Dyson if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr) 759867a482dSJohn Dyson return (EINVAL); 760867a482dSJohn Dyson 761867a482dSJohn Dyson /* 762867a482dSJohn Dyson * Since this routine is only advisory, we default to conservative 763867a482dSJohn Dyson * behavior. 764867a482dSJohn Dyson */ 765cd6eea25SDavid Greenman start = trunc_page((vm_offset_t) uap->addr); 766cd6eea25SDavid Greenman end = round_page((vm_offset_t) uap->addr + uap->len); 767867a482dSJohn Dyson 768190609ddSJohn Baldwin mtx_lock(&Giant); 76923955314SAlfred Perlstein ret = vm_map_madvise(&p->p_vmspace->vm_map, start, end, uap->behav); 770190609ddSJohn Baldwin mtx_unlock(&Giant); 77123955314SAlfred Perlstein return (ret ? EINVAL : 0); 772df8bae1dSRodney W. Grimes } 773df8bae1dSRodney W. Grimes 774d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 775df8bae1dSRodney W. Grimes struct mincore_args { 776651bb817SAlexander Langer const void *addr; 7779154ee6aSPeter Wemm size_t len; 778df8bae1dSRodney W. Grimes char *vec; 779df8bae1dSRodney W. Grimes }; 780d2d3e875SBruce Evans #endif 7810d94caffSDavid Greenman 782df8bae1dSRodney W. Grimes /* ARGSUSED */ 783df8bae1dSRodney W. Grimes int 784cb226aaaSPoul-Henning Kamp mincore(p, uap) 785df8bae1dSRodney W. Grimes struct proc *p; 786df8bae1dSRodney W. Grimes struct mincore_args *uap; 787df8bae1dSRodney W. Grimes { 788867a482dSJohn Dyson vm_offset_t addr, first_addr; 789867a482dSJohn Dyson vm_offset_t end, cend; 790867a482dSJohn Dyson pmap_t pmap; 791867a482dSJohn Dyson vm_map_t map; 79202c04a2fSJohn Dyson char *vec; 793867a482dSJohn Dyson int error; 794867a482dSJohn Dyson int vecindex, lastvecindex; 795867a482dSJohn Dyson register vm_map_entry_t current; 796867a482dSJohn Dyson vm_map_entry_t entry; 797867a482dSJohn Dyson int mincoreinfo; 798dd2622a8SAlan Cox unsigned int timestamp; 799df8bae1dSRodney W. Grimes 800867a482dSJohn Dyson /* 801867a482dSJohn Dyson * Make sure that the addresses presented are valid for user 802867a482dSJohn Dyson * mode. 803867a482dSJohn Dyson */ 804867a482dSJohn Dyson first_addr = addr = trunc_page((vm_offset_t) uap->addr); 8059154ee6aSPeter Wemm end = addr + (vm_size_t)round_page(uap->len); 80602c04a2fSJohn Dyson if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS) 80702c04a2fSJohn Dyson return (EINVAL); 80802c04a2fSJohn Dyson if (end < addr) 80902c04a2fSJohn Dyson return (EINVAL); 81002c04a2fSJohn Dyson 811867a482dSJohn Dyson /* 812867a482dSJohn Dyson * Address of byte vector 813867a482dSJohn Dyson */ 81402c04a2fSJohn Dyson vec = uap->vec; 815867a482dSJohn Dyson 816190609ddSJohn Baldwin mtx_lock(&Giant); 8170cddd8f0SMatthew Dillon map = &p->p_vmspace->vm_map; 818b1028ad1SLuoqi Chen pmap = vmspace_pmap(p->p_vmspace); 819867a482dSJohn Dyson 820eff50fcdSAlan Cox vm_map_lock_read(map); 821dd2622a8SAlan Cox RestartScan: 822dd2622a8SAlan Cox timestamp = map->timestamp; 823867a482dSJohn Dyson 824867a482dSJohn Dyson if (!vm_map_lookup_entry(map, addr, &entry)) 825867a482dSJohn Dyson entry = entry->next; 826867a482dSJohn Dyson 827867a482dSJohn Dyson /* 828867a482dSJohn Dyson * Do this on a map entry basis so that if the pages are not 829867a482dSJohn Dyson * in the current processes address space, we can easily look 830867a482dSJohn Dyson * up the pages elsewhere. 831867a482dSJohn Dyson */ 832867a482dSJohn Dyson lastvecindex = -1; 833867a482dSJohn Dyson for(current = entry; 834867a482dSJohn Dyson (current != &map->header) && (current->start < end); 835867a482dSJohn Dyson current = current->next) { 836867a482dSJohn Dyson 837867a482dSJohn Dyson /* 838867a482dSJohn Dyson * ignore submaps (for now) or null objects 839867a482dSJohn Dyson */ 8409fdfe602SMatthew Dillon if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) || 841867a482dSJohn Dyson current->object.vm_object == NULL) 842867a482dSJohn Dyson continue; 843867a482dSJohn Dyson 844867a482dSJohn Dyson /* 845867a482dSJohn Dyson * limit this scan to the current map entry and the 846867a482dSJohn Dyson * limits for the mincore call 847867a482dSJohn Dyson */ 848867a482dSJohn Dyson if (addr < current->start) 849867a482dSJohn Dyson addr = current->start; 850867a482dSJohn Dyson cend = current->end; 851867a482dSJohn Dyson if (cend > end) 852867a482dSJohn Dyson cend = end; 853867a482dSJohn Dyson 854867a482dSJohn Dyson /* 855867a482dSJohn Dyson * scan this entry one page at a time 856867a482dSJohn Dyson */ 857867a482dSJohn Dyson while(addr < cend) { 858867a482dSJohn Dyson /* 859867a482dSJohn Dyson * Check pmap first, it is likely faster, also 860867a482dSJohn Dyson * it can provide info as to whether we are the 861867a482dSJohn Dyson * one referencing or modifying the page. 862867a482dSJohn Dyson */ 863867a482dSJohn Dyson mincoreinfo = pmap_mincore(pmap, addr); 864867a482dSJohn Dyson if (!mincoreinfo) { 865867a482dSJohn Dyson vm_pindex_t pindex; 866867a482dSJohn Dyson vm_ooffset_t offset; 867867a482dSJohn Dyson vm_page_t m; 868867a482dSJohn Dyson /* 869867a482dSJohn Dyson * calculate the page index into the object 870867a482dSJohn Dyson */ 871867a482dSJohn Dyson offset = current->offset + (addr - current->start); 872867a482dSJohn Dyson pindex = OFF_TO_IDX(offset); 873867a482dSJohn Dyson m = vm_page_lookup(current->object.vm_object, 874867a482dSJohn Dyson pindex); 875867a482dSJohn Dyson /* 876867a482dSJohn Dyson * if the page is resident, then gather information about 877867a482dSJohn Dyson * it. 878867a482dSJohn Dyson */ 879867a482dSJohn Dyson if (m) { 880867a482dSJohn Dyson mincoreinfo = MINCORE_INCORE; 881867a482dSJohn Dyson if (m->dirty || 8820385347cSPeter Wemm pmap_is_modified(m)) 883867a482dSJohn Dyson mincoreinfo |= MINCORE_MODIFIED_OTHER; 884867a482dSJohn Dyson if ((m->flags & PG_REFERENCED) || 8850385347cSPeter Wemm pmap_ts_referenced(m)) { 886e69763a3SDoug Rabson vm_page_flag_set(m, PG_REFERENCED); 887867a482dSJohn Dyson mincoreinfo |= MINCORE_REFERENCED_OTHER; 88802c04a2fSJohn Dyson } 889867a482dSJohn Dyson } 8909b5a5d81SJohn Dyson } 891867a482dSJohn Dyson 892867a482dSJohn Dyson /* 893dd2622a8SAlan Cox * subyte may page fault. In case it needs to modify 894dd2622a8SAlan Cox * the map, we release the lock. 895dd2622a8SAlan Cox */ 896dd2622a8SAlan Cox vm_map_unlock_read(map); 897dd2622a8SAlan Cox 898dd2622a8SAlan Cox /* 899867a482dSJohn Dyson * calculate index into user supplied byte vector 900867a482dSJohn Dyson */ 901867a482dSJohn Dyson vecindex = OFF_TO_IDX(addr - first_addr); 902867a482dSJohn Dyson 903867a482dSJohn Dyson /* 904867a482dSJohn Dyson * If we have skipped map entries, we need to make sure that 905867a482dSJohn Dyson * the byte vector is zeroed for those skipped entries. 906867a482dSJohn Dyson */ 907867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 908867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 909867a482dSJohn Dyson if (error) { 910190609ddSJohn Baldwin mtx_unlock(&Giant); 911867a482dSJohn Dyson return (EFAULT); 912867a482dSJohn Dyson } 913867a482dSJohn Dyson ++lastvecindex; 914867a482dSJohn Dyson } 915867a482dSJohn Dyson 916867a482dSJohn Dyson /* 917867a482dSJohn Dyson * Pass the page information to the user 918867a482dSJohn Dyson */ 919867a482dSJohn Dyson error = subyte( vec + vecindex, mincoreinfo); 920867a482dSJohn Dyson if (error) { 921190609ddSJohn Baldwin mtx_unlock(&Giant); 922867a482dSJohn Dyson return (EFAULT); 923867a482dSJohn Dyson } 924dd2622a8SAlan Cox 925dd2622a8SAlan Cox /* 926dd2622a8SAlan Cox * If the map has changed, due to the subyte, the previous 927dd2622a8SAlan Cox * output may be invalid. 928dd2622a8SAlan Cox */ 929dd2622a8SAlan Cox vm_map_lock_read(map); 930dd2622a8SAlan Cox if (timestamp != map->timestamp) 931dd2622a8SAlan Cox goto RestartScan; 932dd2622a8SAlan Cox 933867a482dSJohn Dyson lastvecindex = vecindex; 93402c04a2fSJohn Dyson addr += PAGE_SIZE; 93502c04a2fSJohn Dyson } 936867a482dSJohn Dyson } 937867a482dSJohn Dyson 938867a482dSJohn Dyson /* 939dd2622a8SAlan Cox * subyte may page fault. In case it needs to modify 940dd2622a8SAlan Cox * the map, we release the lock. 941dd2622a8SAlan Cox */ 942dd2622a8SAlan Cox vm_map_unlock_read(map); 943dd2622a8SAlan Cox 944dd2622a8SAlan Cox /* 945867a482dSJohn Dyson * Zero the last entries in the byte vector. 946867a482dSJohn Dyson */ 947867a482dSJohn Dyson vecindex = OFF_TO_IDX(end - first_addr); 948867a482dSJohn Dyson while((lastvecindex + 1) < vecindex) { 949867a482dSJohn Dyson error = subyte( vec + lastvecindex, 0); 950867a482dSJohn Dyson if (error) { 951190609ddSJohn Baldwin mtx_unlock(&Giant); 952867a482dSJohn Dyson return (EFAULT); 953867a482dSJohn Dyson } 954867a482dSJohn Dyson ++lastvecindex; 955867a482dSJohn Dyson } 956867a482dSJohn Dyson 957dd2622a8SAlan Cox /* 958dd2622a8SAlan Cox * If the map has changed, due to the subyte, the previous 959dd2622a8SAlan Cox * output may be invalid. 960dd2622a8SAlan Cox */ 961dd2622a8SAlan Cox vm_map_lock_read(map); 962dd2622a8SAlan Cox if (timestamp != map->timestamp) 963dd2622a8SAlan Cox goto RestartScan; 964eff50fcdSAlan Cox vm_map_unlock_read(map); 965190609ddSJohn Baldwin mtx_unlock(&Giant); 966dd2622a8SAlan Cox 96702c04a2fSJohn Dyson return (0); 968df8bae1dSRodney W. Grimes } 969df8bae1dSRodney W. Grimes 970d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 971df8bae1dSRodney W. Grimes struct mlock_args { 972651bb817SAlexander Langer const void *addr; 973df8bae1dSRodney W. Grimes size_t len; 974df8bae1dSRodney W. Grimes }; 975d2d3e875SBruce Evans #endif 976df8bae1dSRodney W. Grimes int 977cb226aaaSPoul-Henning Kamp mlock(p, uap) 978df8bae1dSRodney W. Grimes struct proc *p; 979df8bae1dSRodney W. Grimes struct mlock_args *uap; 980df8bae1dSRodney W. Grimes { 981df8bae1dSRodney W. Grimes vm_offset_t addr; 982dabee6feSPeter Wemm vm_size_t size, pageoff; 983df8bae1dSRodney W. Grimes int error; 984df8bae1dSRodney W. Grimes 985df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 9869154ee6aSPeter Wemm size = uap->len; 9879154ee6aSPeter Wemm 988dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 989dabee6feSPeter Wemm addr -= pageoff; 990dabee6feSPeter Wemm size += pageoff; 991dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 992dabee6feSPeter Wemm 993dabee6feSPeter Wemm /* disable wrap around */ 9949154ee6aSPeter Wemm if (addr + size < addr) 995df8bae1dSRodney W. Grimes return (EINVAL); 996dabee6feSPeter Wemm 997df8bae1dSRodney W. Grimes if (atop(size) + cnt.v_wire_count > vm_page_max_wired) 998df8bae1dSRodney W. Grimes return (EAGAIN); 9999154ee6aSPeter Wemm 1000df8bae1dSRodney W. Grimes #ifdef pmap_wired_count 1001df8bae1dSRodney W. Grimes if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 1002df8bae1dSRodney W. Grimes p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 10034a40e3d4SJohn Dyson return (ENOMEM); 1004df8bae1dSRodney W. Grimes #else 1005f711d546SPoul-Henning Kamp error = suser(p); 100605f0fdd2SPoul-Henning Kamp if (error) 1007df8bae1dSRodney W. Grimes return (error); 1008df8bae1dSRodney W. Grimes #endif 1009df8bae1dSRodney W. Grimes 1010190609ddSJohn Baldwin mtx_lock(&Giant); 101123955314SAlfred Perlstein error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, 101223955314SAlfred Perlstein addr + size, FALSE); 1013190609ddSJohn Baldwin mtx_unlock(&Giant); 1014df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 1015df8bae1dSRodney W. Grimes } 1016df8bae1dSRodney W. Grimes 1017d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 10184a40e3d4SJohn Dyson struct mlockall_args { 10194a40e3d4SJohn Dyson int how; 10204a40e3d4SJohn Dyson }; 10214a40e3d4SJohn Dyson #endif 10224a40e3d4SJohn Dyson 10234a40e3d4SJohn Dyson int 1024cb226aaaSPoul-Henning Kamp mlockall(p, uap) 10254a40e3d4SJohn Dyson struct proc *p; 10264a40e3d4SJohn Dyson struct mlockall_args *uap; 10274a40e3d4SJohn Dyson { 10280cddd8f0SMatthew Dillon /* mtx_lock(&Giant); */ 10290cddd8f0SMatthew Dillon /* mtx_unlock(&Giant); */ 10304a40e3d4SJohn Dyson return 0; 10314a40e3d4SJohn Dyson } 10324a40e3d4SJohn Dyson 10334a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_ 10344a40e3d4SJohn Dyson struct mlockall_args { 10354a40e3d4SJohn Dyson int how; 10364a40e3d4SJohn Dyson }; 10374a40e3d4SJohn Dyson #endif 10384a40e3d4SJohn Dyson 10394a40e3d4SJohn Dyson int 1040cb226aaaSPoul-Henning Kamp munlockall(p, uap) 10414a40e3d4SJohn Dyson struct proc *p; 10424a40e3d4SJohn Dyson struct munlockall_args *uap; 10434a40e3d4SJohn Dyson { 10440cddd8f0SMatthew Dillon /* mtx_lock(&Giant); */ 10450cddd8f0SMatthew Dillon /* mtx_unlock(&Giant); */ 10464a40e3d4SJohn Dyson return 0; 10474a40e3d4SJohn Dyson } 10484a40e3d4SJohn Dyson 10494a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_ 1050df8bae1dSRodney W. Grimes struct munlock_args { 1051651bb817SAlexander Langer const void *addr; 1052df8bae1dSRodney W. Grimes size_t len; 1053df8bae1dSRodney W. Grimes }; 1054d2d3e875SBruce Evans #endif 1055df8bae1dSRodney W. Grimes int 1056cb226aaaSPoul-Henning Kamp munlock(p, uap) 1057df8bae1dSRodney W. Grimes struct proc *p; 1058df8bae1dSRodney W. Grimes struct munlock_args *uap; 1059df8bae1dSRodney W. Grimes { 1060df8bae1dSRodney W. Grimes vm_offset_t addr; 1061dabee6feSPeter Wemm vm_size_t size, pageoff; 1062df8bae1dSRodney W. Grimes int error; 1063df8bae1dSRodney W. Grimes 1064df8bae1dSRodney W. Grimes addr = (vm_offset_t) uap->addr; 10659154ee6aSPeter Wemm size = uap->len; 10669154ee6aSPeter Wemm 1067dabee6feSPeter Wemm pageoff = (addr & PAGE_MASK); 1068dabee6feSPeter Wemm addr -= pageoff; 1069dabee6feSPeter Wemm size += pageoff; 1070dabee6feSPeter Wemm size = (vm_size_t) round_page(size); 1071dabee6feSPeter Wemm 1072dabee6feSPeter Wemm /* disable wrap around */ 10739154ee6aSPeter Wemm if (addr + size < addr) 1074df8bae1dSRodney W. Grimes return (EINVAL); 1075dabee6feSPeter Wemm 1076df8bae1dSRodney W. Grimes #ifndef pmap_wired_count 1077f711d546SPoul-Henning Kamp error = suser(p); 107805f0fdd2SPoul-Henning Kamp if (error) 1079df8bae1dSRodney W. Grimes return (error); 1080df8bae1dSRodney W. Grimes #endif 1081df8bae1dSRodney W. Grimes 1082190609ddSJohn Baldwin mtx_lock(&Giant); 108323955314SAlfred Perlstein error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, 108423955314SAlfred Perlstein addr + size, TRUE); 1085190609ddSJohn Baldwin mtx_unlock(&Giant); 1086df8bae1dSRodney W. Grimes return (error == KERN_SUCCESS ? 0 : ENOMEM); 1087df8bae1dSRodney W. Grimes } 1088df8bae1dSRodney W. Grimes 1089df8bae1dSRodney W. Grimes /* 1090df8bae1dSRodney W. Grimes * Internal version of mmap. 1091df8bae1dSRodney W. Grimes * Currently used by mmap, exec, and sys5 shared memory. 1092df8bae1dSRodney W. Grimes * Handle is either a vnode pointer or NULL for MAP_ANON. 1093df8bae1dSRodney W. Grimes */ 1094df8bae1dSRodney W. Grimes int 1095b9dcd593SBruce Evans vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot, 1096b9dcd593SBruce Evans vm_prot_t maxprot, int flags, 1097651bb817SAlexander Langer void *handle, 1098b9dcd593SBruce Evans vm_ooffset_t foff) 1099df8bae1dSRodney W. Grimes { 1100df8bae1dSRodney W. Grimes boolean_t fitit; 1101fcae040bSJohn Dyson vm_object_t object; 1102df8bae1dSRodney W. Grimes struct vnode *vp = NULL; 110324a1cce3SDavid Greenman objtype_t type; 1104df8bae1dSRodney W. Grimes int rv = KERN_SUCCESS; 1105bd7e5f99SJohn Dyson vm_ooffset_t objsize; 1106bd7e5f99SJohn Dyson int docow; 110706cb7259SDavid Greenman struct proc *p = curproc; 1108df8bae1dSRodney W. Grimes 1109df8bae1dSRodney W. Grimes if (size == 0) 1110df8bae1dSRodney W. Grimes return (0); 1111df8bae1dSRodney W. Grimes 111206cb7259SDavid Greenman objsize = size = round_page(size); 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes /* 1115bc9ad247SDavid Greenman * We currently can only deal with page aligned file offsets. 1116bc9ad247SDavid Greenman * The check is here rather than in the syscall because the 1117bc9ad247SDavid Greenman * kernel calls this function internally for other mmaping 1118bc9ad247SDavid Greenman * operations (such as in exec) and non-aligned offsets will 1119bc9ad247SDavid Greenman * cause pmap inconsistencies...so we want to be sure to 1120bc9ad247SDavid Greenman * disallow this in all cases. 1121bc9ad247SDavid Greenman */ 1122bc9ad247SDavid Greenman if (foff & PAGE_MASK) 1123bc9ad247SDavid Greenman return (EINVAL); 1124bc9ad247SDavid Greenman 112506cb7259SDavid Greenman if ((flags & MAP_FIXED) == 0) { 112606cb7259SDavid Greenman fitit = TRUE; 112706cb7259SDavid Greenman *addr = round_page(*addr); 1128e4ca250dSJohn Baldwin mtx_lock(&Giant); 112906cb7259SDavid Greenman } else { 113006cb7259SDavid Greenman if (*addr != trunc_page(*addr)) 113106cb7259SDavid Greenman return (EINVAL); 113206cb7259SDavid Greenman fitit = FALSE; 1133e4ca250dSJohn Baldwin mtx_lock(&Giant); 113406cb7259SDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 113506cb7259SDavid Greenman } 113606cb7259SDavid Greenman 1137bc9ad247SDavid Greenman /* 113824a1cce3SDavid Greenman * Lookup/allocate object. 1139df8bae1dSRodney W. Grimes */ 11405f55e841SDavid Greenman if (flags & MAP_ANON) { 1141851c12ffSJohn Dyson type = OBJT_DEFAULT; 11425f55e841SDavid Greenman /* 11435f55e841SDavid Greenman * Unnamed anonymous regions always start at 0. 11445f55e841SDavid Greenman */ 114567bf6868SJohn Dyson if (handle == 0) 11465f55e841SDavid Greenman foff = 0; 11475f55e841SDavid Greenman } else { 1148df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 1149df8bae1dSRodney W. Grimes if (vp->v_type == VCHR) { 115024a1cce3SDavid Greenman type = OBJT_DEVICE; 1151a23d65bfSBruce Evans handle = (void *)(intptr_t)vp->v_rdev; 115206cb7259SDavid Greenman } else { 115306cb7259SDavid Greenman struct vattr vat; 115406cb7259SDavid Greenman int error; 115506cb7259SDavid Greenman 115606cb7259SDavid Greenman error = VOP_GETATTR(vp, &vat, p->p_ucred, p); 1157e4ca250dSJohn Baldwin if (error) { 115823955314SAlfred Perlstein mtx_unlock(&Giant); 115906cb7259SDavid Greenman return (error); 1160e4ca250dSJohn Baldwin } 1161bd7e5f99SJohn Dyson objsize = round_page(vat.va_size); 116224a1cce3SDavid Greenman type = OBJT_VNODE; 116300d76afeSGuido van Rooij /* 116400d76afeSGuido van Rooij * if it is a regular file without any references 116500d76afeSGuido van Rooij * we do not need to sync it. 116600d76afeSGuido van Rooij */ 116700d76afeSGuido van Rooij if (vp->v_type == VREG && vat.va_nlink == 0) { 116800d76afeSGuido van Rooij flags |= MAP_NOSYNC; 116900d76afeSGuido van Rooij } 1170df8bae1dSRodney W. Grimes } 117106cb7259SDavid Greenman } 117294328e90SJohn Dyson 117394328e90SJohn Dyson if (handle == NULL) { 117494328e90SJohn Dyson object = NULL; 11754738fa09SAlan Cox docow = 0; 117694328e90SJohn Dyson } else { 11770a0a85b3SJohn Dyson object = vm_pager_allocate(type, 11786cde7a16SDavid Greenman handle, objsize, prot, foff); 1179e4ca250dSJohn Baldwin if (object == NULL) { 1180e4ca250dSJohn Baldwin mtx_unlock(&Giant); 118124a1cce3SDavid Greenman return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 1182e4ca250dSJohn Baldwin } 11834738fa09SAlan Cox docow = MAP_PREFAULT_PARTIAL; 118494328e90SJohn Dyson } 1185df8bae1dSRodney W. Grimes 11865850152dSJohn Dyson /* 11878f2ec877SDavid Greenman * Force device mappings to be shared. 11885850152dSJohn Dyson */ 118924964514SPeter Wemm if (type == OBJT_DEVICE || type == OBJT_PHYS) { 11908f2ec877SDavid Greenman flags &= ~(MAP_PRIVATE|MAP_COPY); 11915850152dSJohn Dyson flags |= MAP_SHARED; 11928f2ec877SDavid Greenman } 11935850152dSJohn Dyson 11944f79d873SMatthew Dillon if ((flags & (MAP_ANON|MAP_SHARED)) == 0) 11954738fa09SAlan Cox docow |= MAP_COPY_ON_WRITE; 11964f79d873SMatthew Dillon if (flags & MAP_NOSYNC) 11974f79d873SMatthew Dillon docow |= MAP_DISABLE_SYNCER; 11989730a5daSPaul Saab if (flags & MAP_NOCORE) 11999730a5daSPaul Saab docow |= MAP_DISABLE_COREDUMP; 12005850152dSJohn Dyson 1201d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC) 1202d0aea04fSJohn Dyson if (prot & VM_PROT_READ) 1203d0aea04fSJohn Dyson prot |= VM_PROT_EXECUTE; 1204d0aea04fSJohn Dyson 1205d0aea04fSJohn Dyson if (maxprot & VM_PROT_READ) 1206d0aea04fSJohn Dyson maxprot |= VM_PROT_EXECUTE; 1207d0aea04fSJohn Dyson #endif 1208d0aea04fSJohn Dyson 1209e4ca250dSJohn Baldwin if (fitit) 12100a0a85b3SJohn Dyson *addr = pmap_addr_hint(object, *addr, size); 12110a0a85b3SJohn Dyson 12122267af78SJulian Elischer if (flags & MAP_STACK) 12132267af78SJulian Elischer rv = vm_map_stack (map, *addr, size, prot, 12142267af78SJulian Elischer maxprot, docow); 12152267af78SJulian Elischer else 1216bd7e5f99SJohn Dyson rv = vm_map_find(map, object, foff, addr, size, fitit, 1217bd7e5f99SJohn Dyson prot, maxprot, docow); 1218bd7e5f99SJohn Dyson 1219e4ca250dSJohn Baldwin if (rv != KERN_SUCCESS) 12207fb0c17eSDavid Greenman /* 122124a1cce3SDavid Greenman * Lose the object reference. Will destroy the 122224a1cce3SDavid Greenman * object if it's an unnamed anonymous mapping 122324a1cce3SDavid Greenman * or named anonymous without other references. 12247fb0c17eSDavid Greenman */ 1225df8bae1dSRodney W. Grimes vm_object_deallocate(object); 1226e17bed12SJohn Dyson 1227df8bae1dSRodney W. Grimes /* 1228df8bae1dSRodney W. Grimes * Shared memory is also shared with children. 1229df8bae1dSRodney W. Grimes */ 1230e4ca250dSJohn Baldwin else if (flags & (MAP_SHARED|MAP_INHERIT)) { 1231df8bae1dSRodney W. Grimes rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 1232e4ca250dSJohn Baldwin if (rv != KERN_SUCCESS) 12337fb0c17eSDavid Greenman (void) vm_map_remove(map, *addr, *addr + size); 1234df8bae1dSRodney W. Grimes } 1235e4ca250dSJohn Baldwin mtx_unlock(&Giant); 1236df8bae1dSRodney W. Grimes switch (rv) { 1237df8bae1dSRodney W. Grimes case KERN_SUCCESS: 1238df8bae1dSRodney W. Grimes return (0); 1239df8bae1dSRodney W. Grimes case KERN_INVALID_ADDRESS: 1240df8bae1dSRodney W. Grimes case KERN_NO_SPACE: 1241df8bae1dSRodney W. Grimes return (ENOMEM); 1242df8bae1dSRodney W. Grimes case KERN_PROTECTION_FAILURE: 1243df8bae1dSRodney W. Grimes return (EACCES); 1244df8bae1dSRodney W. Grimes default: 1245df8bae1dSRodney W. Grimes return (EINVAL); 1246df8bae1dSRodney W. Grimes } 1247df8bae1dSRodney W. Grimes } 1248