BSD 4_4 release
[unix-history] / usr / src / sys / vm / vm_mmap.c
index 9e2525f..060aeb1 100644 (file)
@@ -1,17 +1,43 @@
 /*
  * Copyright (c) 1988 University of Utah.
 /*
  * Copyright (c) 1988 University of Utah.
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * the Systems Programming Group of the University of Utah Computer
  * Science Department.
  *
  *
  * This code is derived from software contributed to Berkeley by
  * the Systems Programming Group of the University of Utah Computer
  * Science Department.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  *
  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
  *
  *
  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
  *
- *     @(#)vm_mmap.c   7.13 (Berkeley) %G%
+ *     @(#)vm_mmap.c   8.1 (Berkeley) 6/18/93
  */
 
 /*
  */
 
 /*
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/filedesc.h>
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/filedesc.h>
+#include <sys/resourcevar.h>
 #include <sys/proc.h>
 #include <sys/vnode.h>
 #include <sys/proc.h>
 #include <sys/vnode.h>
-#include <sys/specdev.h>
 #include <sys/file.h>
 #include <sys/mman.h>
 #include <sys/conf.h>
 
 #include <sys/file.h>
 #include <sys/mman.h>
 #include <sys/conf.h>
 
+#include <miscfs/specfs/specdev.h>
+
 #include <vm/vm.h>
 #include <vm/vm_pager.h>
 #include <vm/vm_prot.h>
 #include <vm/vm.h>
 #include <vm/vm_pager.h>
 #include <vm/vm_prot.h>
@@ -39,25 +67,29 @@ int mmapdebug = 0;
 #define MDB_MAPIT      0x04
 #endif
 
 #define MDB_MAPIT      0x04
 #endif
 
+struct sbrk_args {
+       int     incr;
+};
 /* ARGSUSED */
 int
 /* ARGSUSED */
 int
-getpagesize(p, uap, retval)
+sbrk(p, uap, retval)
        struct proc *p;
        struct proc *p;
-       void *uap;
+       struct sbrk_args *uap;
        int *retval;
 {
 
        int *retval;
 {
 
-       *retval = PAGE_SIZE;
-       return (0);
+       /* Not yet implemented */
+       return (EOPNOTSUPP);
 }
 
 }
 
+struct sstk_args {
+       int     incr;
+};
 /* ARGSUSED */
 int
 /* ARGSUSED */
 int
-sbrk(p, uap, retval)
+sstk(p, uap, retval)
        struct proc *p;
        struct proc *p;
-       struct args {
-               int     incr;
-       } *uap;
+       struct sstk_args *uap;
        int *retval;
 {
 
        int *retval;
 {
 
@@ -65,23 +97,26 @@ sbrk(p, uap, retval)
        return (EOPNOTSUPP);
 }
 
        return (EOPNOTSUPP);
 }
 
+#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
+struct getpagesize_args {
+       int     dummy;
+};
 /* ARGSUSED */
 int
 /* ARGSUSED */
 int
-sstk(p, uap, retval)
+ogetpagesize(p, uap, retval)
        struct proc *p;
        struct proc *p;
-       struct args {
-               int     incr;
-       } *uap;
+       struct getpagesize_args *uap;
        int *retval;
 {
 
        int *retval;
 {
 
-       /* Not yet implemented */
-       return (EOPNOTSUPP);
+       *retval = PAGE_SIZE;
+       return (0);
 }
 }
+#endif /* COMPAT_43 || COMPAT_SUNOS */
 
 struct mmap_args {
        caddr_t addr;
 
 struct mmap_args {
        caddr_t addr;
-       int     len;
+       size_t  len;
        int     prot;
        int     flags;
        int     fd;
        int     prot;
        int     flags;
        int     fd;
@@ -90,20 +125,20 @@ struct mmap_args {
 };
 
 #ifdef COMPAT_43
 };
 
 #ifdef COMPAT_43
+struct ommap_args {
+       caddr_t addr;
+       int     len;
+       int     prot;
+       int     flags;
+       int     fd;
+       long    pos;
+};
 int
 int
-osmmap(p, uap, retval)
+ommap(p, uap, retval)
        struct proc *p;
        struct proc *p;
-       register struct args {
-               caddr_t addr;
-               int     len;
-               int     prot;
-               int     flags;
-               int     fd;
-               off_t   pos;            /* XXX becomes long */
-       } *uap;
+       register struct ommap_args *uap;
        int *retval;
 {
        int *retval;
 {
-       long spos;
        struct mmap_args nargs;
        static const char cvtbsdprot[8] = {
                0,
        struct mmap_args nargs;
        static const char cvtbsdprot[8] = {
                0,
@@ -138,16 +173,13 @@ osmmap(p, uap, retval)
        if (uap->flags & OMAP_INHERIT)
                nargs.flags |= MAP_INHERIT;
        nargs.fd = uap->fd;
        if (uap->flags & OMAP_INHERIT)
                nargs.flags |= MAP_INHERIT;
        nargs.fd = uap->fd;
-       spos = uap->pos >> 32;          /* XXX */
-       if (spos > 0)                   /* XXX */
-               uap->pos = spos;        /* XXX */
        nargs.pos = uap->pos;
        nargs.pos = uap->pos;
-       return (smmap(p, &nargs, retval));
+       return (mmap(p, &nargs, retval));
 }
 #endif
 
 int
 }
 #endif
 
 int
-smmap(p, uap, retval)
+mmap(p, uap, retval)
        struct proc *p;
        register struct mmap_args *uap;
        int *retval;
        struct proc *p;
        register struct mmap_args *uap;
        int *retval;
@@ -157,25 +189,39 @@ smmap(p, uap, retval)
        struct vnode *vp;
        vm_offset_t addr;
        vm_size_t size;
        struct vnode *vp;
        vm_offset_t addr;
        vm_size_t size;
-       vm_prot_t prot;
+       vm_prot_t prot, maxprot;
        caddr_t handle;
        caddr_t handle;
-       int error;
+       int flags, error;
 
 
+       prot = uap->prot & VM_PROT_ALL;
+       flags = uap->flags;
 #ifdef DEBUG
        if (mmapdebug & MDB_FOLLOW)
                printf("mmap(%d): addr %x len %x pro %x flg %x fd %d pos %x\n",
 #ifdef DEBUG
        if (mmapdebug & MDB_FOLLOW)
                printf("mmap(%d): addr %x len %x pro %x flg %x fd %d pos %x\n",
-                      p->p_pid, uap->addr, uap->len, uap->prot,
-                      uap->flags, uap->fd, uap->pos);
+                      p->p_pid, uap->addr, uap->len, prot,
+                      flags, uap->fd, (vm_offset_t)uap->pos);
 #endif
        /*
         * Address (if FIXED) must be page aligned.
         * Size is implicitly rounded to a page boundary.
         */
        addr = (vm_offset_t) uap->addr;
 #endif
        /*
         * Address (if FIXED) must be page aligned.
         * Size is implicitly rounded to a page boundary.
         */
        addr = (vm_offset_t) uap->addr;
-       if (((uap->flags & MAP_FIXED) && (addr & PAGE_MASK)) || uap->len < 0 ||
-           ((uap->flags & MAP_ANON) && uap->fd != -1))
+       if (((flags & MAP_FIXED) && (addr & PAGE_MASK)) ||
+           (ssize_t)uap->len < 0 || ((flags & MAP_ANON) && uap->fd != -1))
                return (EINVAL);
        size = (vm_size_t) round_page(uap->len);
                return (EINVAL);
        size = (vm_size_t) round_page(uap->len);
+       /*
+        * Check for illegal addresses.  Watch out for address wrap...
+        * Note that VM_*_ADDRESS are not constants due to casts (argh).
+        */
+       if (flags & MAP_FIXED) {
+               if (VM_MAXUSER_ADDRESS > 0 && addr + size >= VM_MAXUSER_ADDRESS)
+                       return (EINVAL);
+               if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
+                       return (EINVAL);
+               if (addr > addr + size)
+                       return (EINVAL);
+       }
        /*
         * XXX if no hint provided for a non-fixed mapping place it after
         * the end of the largest possible heap.
        /*
         * XXX if no hint provided for a non-fixed mapping place it after
         * the end of the largest possible heap.
@@ -183,56 +229,64 @@ smmap(p, uap, retval)
         * There should really be a pmap call to determine a reasonable
         * location.
         */
         * There should really be a pmap call to determine a reasonable
         * location.
         */
-       if (addr == 0 && (uap->flags & MAP_FIXED) == 0)
+       if (addr == 0 && (flags & MAP_FIXED) == 0)
                addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
                addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
-       /*
-        * If we are mapping a file we need to check various
-        * file/vnode related things.
-        */
-       if (uap->flags & MAP_ANON)
+       if (flags & MAP_ANON) {
+               /*
+                * Mapping blank space is trivial.
+                */
                handle = NULL;
                handle = NULL;
-       else {
+               maxprot = VM_PROT_ALL;
+       } else {
                /*
                 * Mapping file, get fp for validation.
                /*
                 * Mapping file, get fp for validation.
-                * Obtain vnode and make sure it is of appropriate type
+                * Obtain vnode and make sure it is of appropriate type.
                 */
                if (((unsigned)uap->fd) >= fdp->fd_nfiles ||
                    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
                 */
                if (((unsigned)uap->fd) >= fdp->fd_nfiles ||
                    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
-                       return(EBADF);
+                       return (EBADF);
                if (fp->f_type != DTYPE_VNODE)
                if (fp->f_type != DTYPE_VNODE)
-                       return(EINVAL);
+                       return (EINVAL);
                vp = (struct vnode *)fp->f_data;
                if (vp->v_type != VREG && vp->v_type != VCHR)
                vp = (struct vnode *)fp->f_data;
                if (vp->v_type != VREG && vp->v_type != VCHR)
-                       return(EINVAL);
+                       return (EINVAL);
                /*
                /*
-                * Ensure that file protection and desired protection
-                * are compatible.  Note that we only worry about writability
-                * if mapping is shared.
+                * Ensure that file and memory protections are compatible.
+                * Note that we only worry about writability if mapping is
+                * shared; in this case, current and max prot are dictated
+                * by the open file.
+                * XXX use the vnode instead?  Problem is: what credentials
+                * do we use for determination?  What if proc does a setuid?
                 */
                 */
-               if ((uap->prot & PROT_READ) && (fp->f_flag & FREAD) == 0 ||
-                   ((uap->flags & MAP_SHARED) &&
-                    (uap->prot & PROT_WRITE) && (fp->f_flag & FWRITE) == 0))
-                       return(EACCES);
+               maxprot = VM_PROT_EXECUTE;      /* ??? */
+               if (fp->f_flag & FREAD)
+                       maxprot |= VM_PROT_READ;
+               else if (prot & PROT_READ)
+                       return (EACCES);
+               if (flags & MAP_SHARED) {
+                       if (fp->f_flag & FWRITE)
+                               maxprot |= VM_PROT_WRITE;
+                       else if (prot & PROT_WRITE)
+                               return (EACCES);
+               } else
+                       maxprot |= VM_PROT_WRITE;
                handle = (caddr_t)vp;
        }
                handle = (caddr_t)vp;
        }
-       /*
-        * Map protections to MACH style
-        */
-       prot = uap->prot & VM_PROT_ALL;
-       error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot,
-                       uap->flags, handle, (vm_offset_t)uap->pos);
+       error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
+           flags, handle, (vm_offset_t)uap->pos);
        if (error == 0)
        if (error == 0)
-               *retval = (int) addr;
-       return(error);
+               *retval = (int)addr;
+       return (error);
 }
 
 }
 
+struct msync_args {
+       caddr_t addr;
+       int     len;
+};
 int
 msync(p, uap, retval)
        struct proc *p;
 int
 msync(p, uap, retval)
        struct proc *p;
-       struct args {
-               caddr_t addr;
-               int     len;
-       } *uap;
+       struct msync_args *uap;
        int *retval;
 {
        vm_offset_t addr, objoff, oaddr;
        int *retval;
 {
        vm_offset_t addr, objoff, oaddr;
@@ -299,13 +353,14 @@ msync(p, uap, retval)
        return(0);
 }
 
        return(0);
 }
 
+struct munmap_args {
+       caddr_t addr;
+       int     len;
+};
 int
 munmap(p, uap, retval)
        register struct proc *p;
 int
 munmap(p, uap, retval)
        register struct proc *p;
-       register struct args {
-               caddr_t addr;
-               int     len;
-       } *uap;
+       register struct munmap_args *uap;
        int *retval;
 {
        vm_offset_t addr;
        int *retval;
 {
        vm_offset_t addr;
@@ -323,7 +378,17 @@ munmap(p, uap, retval)
        size = (vm_size_t) round_page(uap->len);
        if (size == 0)
                return(0);
        size = (vm_size_t) round_page(uap->len);
        if (size == 0)
                return(0);
-       if (!vm_map_is_allocated(&p->p_vmspace->vm_map, addr, addr+size,
+       /*
+        * Check for illegal addresses.  Watch out for address wrap...
+        * Note that VM_*_ADDRESS are not constants due to casts (argh).
+        */
+       if (VM_MAXUSER_ADDRESS > 0 && addr + size >= VM_MAXUSER_ADDRESS)
+               return (EINVAL);
+       if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
+               return (EINVAL);
+       if (addr > addr + size)
+               return (EINVAL);
+       if (!vm_map_is_allocated(&p->p_vmspace->vm_map, addr, addr + size,
            FALSE))
                return(EINVAL);
        /* returns nothing but KERN_SUCCESS anyway */
            FALSE))
                return(EINVAL);
        /* returns nothing but KERN_SUCCESS anyway */
@@ -346,14 +411,15 @@ munmapfd(fd)
        curproc->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
 }
 
        curproc->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
 }
 
+struct mprotect_args {
+       caddr_t addr;
+       int     len;
+       int     prot;
+};
 int
 mprotect(p, uap, retval)
        struct proc *p;
 int
 mprotect(p, uap, retval)
        struct proc *p;
-       struct args {
-               caddr_t addr;
-               int     len;
-               int     prot;
-       } *uap;
+       struct mprotect_args *uap;
        int *retval;
 {
        vm_offset_t addr;
        int *retval;
 {
        vm_offset_t addr;
@@ -366,20 +432,11 @@ mprotect(p, uap, retval)
                       p->p_pid, uap->addr, uap->len, uap->prot);
 #endif
 
                       p->p_pid, uap->addr, uap->len, uap->prot);
 #endif
 
-       addr = (vm_offset_t) uap->addr;
+       addr = (vm_offset_t)uap->addr;
        if ((addr & PAGE_MASK) || uap->len < 0)
                return(EINVAL);
        if ((addr & PAGE_MASK) || uap->len < 0)
                return(EINVAL);
-       size = (vm_size_t) uap->len;
-       /*
-        * Map protections
-        */
-       prot = VM_PROT_NONE;
-       if (uap->prot & PROT_READ)
-               prot |= VM_PROT_READ;
-       if (uap->prot & PROT_WRITE)
-               prot |= VM_PROT_WRITE;
-       if (uap->prot & PROT_EXEC)
-               prot |= VM_PROT_EXECUTE;
+       size = (vm_size_t)uap->len;
+       prot = uap->prot & VM_PROT_ALL;
 
        switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr+size, prot,
            FALSE)) {
 
        switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr+size, prot,
            FALSE)) {
@@ -391,15 +448,16 @@ mprotect(p, uap, retval)
        return (EINVAL);
 }
 
        return (EINVAL);
 }
 
+struct madvise_args {
+       caddr_t addr;
+       int     len;
+       int     behav;
+};
 /* ARGSUSED */
 int
 madvise(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 int
 madvise(p, uap, retval)
        struct proc *p;
-       struct args {
-               caddr_t addr;
-               int     len;
-               int     behav;
-       } *uap;
+       struct madvise_args *uap;
        int *retval;
 {
 
        int *retval;
 {
 
@@ -407,15 +465,16 @@ madvise(p, uap, retval)
        return (EOPNOTSUPP);
 }
 
        return (EOPNOTSUPP);
 }
 
+struct mincore_args {
+       caddr_t addr;
+       int     len;
+       char    *vec;
+};
 /* ARGSUSED */
 int
 mincore(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 int
 mincore(p, uap, retval)
        struct proc *p;
-       struct args {
-               caddr_t addr;
-               int     len;
-               char    *vec;
-       } *uap;
+       struct mincore_args *uap;
        int *retval;
 {
 
        int *retval;
 {
 
@@ -423,17 +482,88 @@ mincore(p, uap, retval)
        return (EOPNOTSUPP);
 }
 
        return (EOPNOTSUPP);
 }
 
+struct mlock_args {
+       caddr_t addr;
+       size_t  len;
+};
+int
+mlock(p, uap, retval)
+       struct proc *p;
+       struct mlock_args *uap;
+       int *retval;
+{
+       vm_offset_t addr;
+       vm_size_t size;
+       int error;
+       extern int vm_page_max_wired;
+
+#ifdef DEBUG
+       if (mmapdebug & MDB_FOLLOW)
+               printf("mlock(%d): addr %x len %x\n",
+                      p->p_pid, uap->addr, uap->len);
+#endif
+       addr = (vm_offset_t)uap->addr;
+       if ((addr & PAGE_MASK) || uap->addr + uap->len < uap->addr)
+               return (EINVAL);
+       size = round_page((vm_size_t)uap->len);
+       if (atop(size) + cnt.v_wire_count > vm_page_max_wired)
+               return (EAGAIN);
+#ifdef pmap_wired_count
+       if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
+           p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
+               return (EAGAIN);
+#else
+       if (error = suser(p->p_ucred, &p->p_acflag))
+               return (error);
+#endif
+
+       error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE);
+       return (error == KERN_SUCCESS ? 0 : ENOMEM);
+}
+
+struct munlock_args {
+       caddr_t addr;
+       size_t  len;
+};
+int
+munlock(p, uap, retval)
+       struct proc *p;
+       struct munlock_args *uap;
+       int *retval;
+{
+       vm_offset_t addr;
+       vm_size_t size;
+       int error;
+
+#ifdef DEBUG
+       if (mmapdebug & MDB_FOLLOW)
+               printf("munlock(%d): addr %x len %x\n",
+                      p->p_pid, uap->addr, uap->len);
+#endif
+       addr = (vm_offset_t)uap->addr;
+       if ((addr & PAGE_MASK) || uap->addr + uap->len < uap->addr)
+               return (EINVAL);
+#ifndef pmap_wired_count
+       if (error = suser(p->p_ucred, &p->p_acflag))
+               return (error);
+#endif
+       size = round_page((vm_size_t)uap->len);
+
+       error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE);
+       return (error == KERN_SUCCESS ? 0 : ENOMEM);
+}
+
 /*
  * Internal version of mmap.
  * Currently used by mmap, exec, and sys5 shared memory.
  * Handle is either a vnode pointer or NULL for MAP_ANON.
  */
 int
 /*
  * Internal version of mmap.
  * Currently used by mmap, exec, and sys5 shared memory.
  * Handle is either a vnode pointer or NULL for MAP_ANON.
  */
 int
-vm_mmap(map, addr, size, prot, flags, handle, foff)
+vm_mmap(map, addr, size, prot, maxprot, flags, handle, foff)
        register vm_map_t map;
        register vm_offset_t *addr;
        register vm_size_t size;
        register vm_map_t map;
        register vm_offset_t *addr;
        register vm_size_t size;
-       vm_prot_t prot;
+       vm_prot_t prot, maxprot;
        register int flags;
        caddr_t handle;         /* XXX should be vp */
        vm_offset_t foff;
        register int flags;
        caddr_t handle;         /* XXX should be vp */
        vm_offset_t foff;
@@ -441,7 +571,7 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
        register vm_pager_t pager;
        boolean_t fitit;
        vm_object_t object;
        register vm_pager_t pager;
        boolean_t fitit;
        vm_object_t object;
-       struct vnode *vp;
+       struct vnode *vp = NULL;
        int type;
        int rv = KERN_SUCCESS;
 
        int type;
        int rv = KERN_SUCCESS;
 
@@ -453,7 +583,7 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
                *addr = round_page(*addr);
        } else {
                fitit = FALSE;
                *addr = round_page(*addr);
        } else {
                fitit = FALSE;
-               (void) vm_deallocate(map, *addr, size);
+               (void)vm_deallocate(map, *addr, size);
        }
 
        /*
        }
 
        /*
@@ -485,7 +615,7 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
         */
        if (flags & MAP_ANON) {
                rv = vm_allocate_with_pager(map, addr, size, fitit,
         */
        if (flags & MAP_ANON) {
                rv = vm_allocate_with_pager(map, addr, size, fitit,
-                                           pager, (vm_offset_t)foff, TRUE);
+                                           pager, foff, TRUE);
                if (rv != KERN_SUCCESS) {
                        if (handle == NULL)
                                vm_pager_deallocate(pager);
                if (rv != KERN_SUCCESS) {
                        if (handle == NULL)
                                vm_pager_deallocate(pager);
@@ -496,6 +626,9 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
                /*
                 * Don't cache anonymous objects.
                 * Loses the reference gained by vm_pager_allocate.
                /*
                 * Don't cache anonymous objects.
                 * Loses the reference gained by vm_pager_allocate.
+                * Note that object will be NULL when handle == NULL,
+                * this is ok since vm_allocate_with_pager has made
+                * sure that these objects are uncached.
                 */
                (void) pager_cache(object, FALSE);
 #ifdef DEBUG
                 */
                (void) pager_cache(object, FALSE);
 #ifdef DEBUG
@@ -510,7 +643,7 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
         */
        else if (vp->v_type == VCHR) {
                rv = vm_allocate_with_pager(map, addr, size, fitit,
         */
        else if (vp->v_type == VCHR) {
                rv = vm_allocate_with_pager(map, addr, size, fitit,
-                                           pager, (vm_offset_t)foff, FALSE);
+                                           pager, foff, FALSE);
                /*
                 * Uncache the object and lose the reference gained
                 * by vm_pager_allocate().  If the call to
                /*
                 * Uncache the object and lose the reference gained
                 * by vm_pager_allocate().  If the call to
@@ -540,7 +673,7 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
                if (flags & MAP_SHARED) {
                        rv = vm_allocate_with_pager(map, addr, size,
                                                    fitit, pager,
                if (flags & MAP_SHARED) {
                        rv = vm_allocate_with_pager(map, addr, size,
                                                    fitit, pager,
-                                                   (vm_offset_t)foff, FALSE);
+                                                   foff, FALSE);
                        if (rv != KERN_SUCCESS) {
                                vm_object_deallocate(object);
                                goto out;
                        if (rv != KERN_SUCCESS) {
                                vm_object_deallocate(object);
                                goto out;
@@ -579,7 +712,7 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
                        off = VM_MIN_ADDRESS;
                        rv = vm_allocate_with_pager(tmap, &off, size,
                                                    TRUE, pager,
                        off = VM_MIN_ADDRESS;
                        rv = vm_allocate_with_pager(tmap, &off, size,
                                                    TRUE, pager,
-                                                   (vm_offset_t)foff, FALSE);
+                                                   foff, FALSE);
                        if (rv != KERN_SUCCESS) {
                                vm_object_deallocate(object);
                                vm_map_deallocate(tmap);
                        if (rv != KERN_SUCCESS) {
                                vm_object_deallocate(object);
                                vm_map_deallocate(tmap);
@@ -626,8 +759,7 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
                         * sharing map is involved.  So we cheat and write
                         * protect everything ourselves.
                         */
                         * sharing map is involved.  So we cheat and write
                         * protect everything ourselves.
                         */
-                       vm_object_pmap_copy(object, (vm_offset_t)foff,
-                                           (vm_offset_t)foff+size);
+                       vm_object_pmap_copy(object, foff, foff + size);
                        vm_object_deallocate(object);
                        vm_map_deallocate(tmap);
                        if (rv != KERN_SUCCESS)
                        vm_object_deallocate(object);
                        vm_map_deallocate(tmap);
                        if (rv != KERN_SUCCESS)
@@ -641,18 +773,16 @@ vm_mmap(map, addr, size, prot, flags, handle, foff)
        }
        /*
         * Correct protection (default is VM_PROT_ALL).
        }
        /*
         * Correct protection (default is VM_PROT_ALL).
-        * Note that we set the maximum protection.  This may not be
-        * entirely correct.  Maybe the maximum protection should be based
-        * on the object permissions where it makes sense (e.g. a vnode).
-        *
-        * Changed my mind: leave max prot at VM_PROT_ALL.
+        * If maxprot is different than prot, we must set both explicitly.
         */
         */
-       if (prot != VM_PROT_ALL) {
+       rv = KERN_SUCCESS;
+       if (maxprot != VM_PROT_ALL)
+               rv = vm_map_protect(map, *addr, *addr+size, maxprot, TRUE);
+       if (rv == KERN_SUCCESS && prot != maxprot)
                rv = vm_map_protect(map, *addr, *addr+size, prot, FALSE);
                rv = vm_map_protect(map, *addr, *addr+size, prot, FALSE);
-               if (rv != KERN_SUCCESS) {
-                       (void) vm_deallocate(map, *addr, size);
-                       goto out;
-               }
+       if (rv != KERN_SUCCESS) {
+               (void) vm_deallocate(map, *addr, size);
+               goto out;
        }
        /*
         * Shared memory is also shared with children.
        }
        /*
         * Shared memory is also shared with children.