BSD 4_4 release
[unix-history] / usr / src / sys / vm / vm_mmap.c
index c628823..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.24 (Berkeley) %G%
+ *     @(#)vm_mmap.c   8.1 (Berkeley) 6/18/93
  */
 
 /*
  */
 
 /*
@@ -21,6 +47,7 @@
 #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/file.h>
 #include <sys/proc.h>
 #include <sys/vnode.h>
 #include <sys/file.h>
@@ -70,23 +97,13 @@ sstk(p, uap, retval)
        return (EOPNOTSUPP);
 }
 
        return (EOPNOTSUPP);
 }
 
-struct mmap_args {
-       caddr_t addr;
-       size_t  len;
-       int     prot;
-       int     flags;
-       int     fd;
-       long    pad;
-       off_t   pos;
-};
-
-#ifdef COMPAT_43
+#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
 struct getpagesize_args {
        int     dummy;
 };
 /* ARGSUSED */
 int
 struct getpagesize_args {
        int     dummy;
 };
 /* ARGSUSED */
 int
-getpagesize(p, uap, retval)
+ogetpagesize(p, uap, retval)
        struct proc *p;
        struct getpagesize_args *uap;
        int *retval;
        struct proc *p;
        struct getpagesize_args *uap;
        int *retval;
@@ -95,8 +112,20 @@ getpagesize(p, uap, retval)
        *retval = PAGE_SIZE;
        return (0);
 }
        *retval = PAGE_SIZE;
        return (0);
 }
+#endif /* COMPAT_43 || COMPAT_SUNOS */
 
 
-struct osmmap_args {
+struct mmap_args {
+       caddr_t addr;
+       size_t  len;
+       int     prot;
+       int     flags;
+       int     fd;
+       long    pad;
+       off_t   pos;
+};
+
+#ifdef COMPAT_43
+struct ommap_args {
        caddr_t addr;
        int     len;
        int     prot;
        caddr_t addr;
        int     len;
        int     prot;
@@ -105,9 +134,9 @@ struct osmmap_args {
        long    pos;
 };
 int
        long    pos;
 };
 int
-osmmap(p, uap, retval)
+ommap(p, uap, retval)
        struct proc *p;
        struct proc *p;
-       register struct osmmap_args *uap;
+       register struct ommap_args *uap;
        int *retval;
 {
        struct mmap_args nargs;
        int *retval;
 {
        struct mmap_args nargs;
@@ -145,12 +174,12 @@ osmmap(p, uap, retval)
                nargs.flags |= MAP_INHERIT;
        nargs.fd = uap->fd;
        nargs.pos = uap->pos;
                nargs.flags |= MAP_INHERIT;
        nargs.fd = uap->fd;
        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;
@@ -164,11 +193,12 @@ smmap(p, uap, retval)
        caddr_t handle;
        int flags, error;
 
        caddr_t handle;
        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",
        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",
-                      p->p_pid, uap->addr, uap->len, uap->prot,
+                      p->p_pid, uap->addr, uap->len, prot,
                       flags, uap->fd, (vm_offset_t)uap->pos);
 #endif
        /*
                       flags, uap->fd, (vm_offset_t)uap->pos);
 #endif
        /*
@@ -176,8 +206,8 @@ smmap(p, uap, retval)
         * Size is implicitly rounded to a page boundary.
         */
        addr = (vm_offset_t) uap->addr;
         * Size is implicitly rounded to a page boundary.
         */
        addr = (vm_offset_t) uap->addr;
-       if (((flags & MAP_FIXED) && (addr & PAGE_MASK)) || uap->len < 0 ||
-           ((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);
        /*
@@ -201,53 +231,52 @@ smmap(p, uap, retval)
         */
        if (addr == 0 && (flags & MAP_FIXED) == 0)
                addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
         */
        if (addr == 0 && (flags & MAP_FIXED) == 0)
                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 (flags & MAP_ANON) {
        if (flags & MAP_ANON) {
+               /*
+                * Mapping blank space is trivial.
+                */
                handle = NULL;
                maxprot = VM_PROT_ALL;
        } else {
                /*
                 * Mapping file, get fp for validation.
                handle = NULL;
                maxprot = VM_PROT_ALL;
        } else {
                /*
                 * 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);
-               /*
-                * Ensure that file protection and desired protection
-                * are compatible.  Note that we only worry about writability
-                * if mapping is shared.
-                */
-               if ((uap->prot & PROT_READ) && (fp->f_flag & FREAD) == 0 ||
-                   ((flags & MAP_SHARED) &&
-                    (uap->prot & PROT_WRITE) && (fp->f_flag & FWRITE) == 0))
-                       return(EACCES);
-               handle = (caddr_t)vp;
+                       return (EINVAL);
                /*
                /*
-                * Set maximum protection as dictated by the open file.
+                * 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?
                 */
                 * XXX use the vnode instead?  Problem is: what credentials
                 * do we use for determination?  What if proc does a setuid?
                 */
-               maxprot = 0;
+               maxprot = VM_PROT_EXECUTE;      /* ??? */
                if (fp->f_flag & FREAD)
                if (fp->f_flag & FREAD)
-                       maxprot |= VM_PROT_READ|VM_PROT_EXECUTE;
-               if (fp->f_flag & FWRITE)
+                       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;
                        maxprot |= VM_PROT_WRITE;
+               handle = (caddr_t)vp;
        }
        }
-       prot = uap->prot & VM_PROT_ALL;
        error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
        error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
-                       flags, handle, (vm_offset_t)uap->pos);
+           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 {
 }
 
 struct msync_args {
@@ -453,6 +482,77 @@ 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.
 /*
  * Internal version of mmap.
  * Currently used by mmap, exec, and sys5 shared memory.
@@ -471,7 +571,7 @@ vm_mmap(map, addr, size, prot, maxprot, 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;