[BACK]Return to uvm_mmap.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / uvm

Annotation of src/sys/uvm/uvm_mmap.c, Revision 1.120

1.120   ! christos    1: /*     $NetBSD: uvm_mmap.c,v 1.119 2007/12/20 23:03:14 dsl Exp $       */
1.1       mrg         2:
                      3: /*
                      4:  * Copyright (c) 1997 Charles D. Cranor and Washington University.
1.51      chs         5:  * Copyright (c) 1991, 1993 The Regents of the University of California.
1.1       mrg         6:  * Copyright (c) 1988 University of Utah.
1.51      chs         7:  *
1.1       mrg         8:  * All rights reserved.
                      9:  *
                     10:  * This code is derived from software contributed to Berkeley by
                     11:  * the Systems Programming Group of the University of Utah Computer
                     12:  * Science Department.
                     13:  *
                     14:  * Redistribution and use in source and binary forms, with or without
                     15:  * modification, are permitted provided that the following conditions
                     16:  * are met:
                     17:  * 1. Redistributions of source code must retain the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer.
                     19:  * 2. Redistributions in binary form must reproduce the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer in the
                     21:  *    documentation and/or other materials provided with the distribution.
                     22:  * 3. All advertising materials mentioning features or use of this software
                     23:  *    must display the following acknowledgement:
                     24:  *      This product includes software developed by the Charles D. Cranor,
1.51      chs        25:  *     Washington University, University of California, Berkeley and
1.1       mrg        26:  *     its contributors.
                     27:  * 4. Neither the name of the University nor the names of its contributors
                     28:  *    may be used to endorse or promote products derived from this software
                     29:  *    without specific prior written permission.
                     30:  *
                     31:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     32:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     33:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     34:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     35:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     36:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     37:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     38:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     39:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     40:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     41:  * SUCH DAMAGE.
                     42:  *
                     43:  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
                     44:  *      @(#)vm_mmap.c   8.5 (Berkeley) 5/19/94
1.3       mrg        45:  * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
1.1       mrg        46:  */
                     47:
                     48: /*
                     49:  * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
                     50:  * function.
                     51:  */
1.60      lukem      52:
                     53: #include <sys/cdefs.h>
1.120   ! christos   54: __KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.119 2007/12/20 23:03:14 dsl Exp $");
1.80      jdolecek   55:
                     56: #include "opt_compat_netbsd.h"
1.97      elad       57: #include "opt_pax.h"
1.99      elad       58: #include "veriexec.h"
1.60      lukem      59:
1.1       mrg        60: #include <sys/param.h>
                     61: #include <sys/systm.h>
                     62: #include <sys/file.h>
                     63: #include <sys/filedesc.h>
                     64: #include <sys/resourcevar.h>
                     65: #include <sys/mman.h>
                     66: #include <sys/mount.h>
                     67: #include <sys/proc.h>
                     68: #include <sys/malloc.h>
                     69: #include <sys/vnode.h>
                     70: #include <sys/conf.h>
1.9       mrg        71: #include <sys/stat.h>
1.99      elad       72:
                     73: #if NVERIEXEC > 0
                     74: #include <sys/verified_exec.h>
                     75: #endif /* NVERIEXEC > 0 */
1.97      elad       76:
                     77: #ifdef PAX_MPROTECT
                     78: #include <sys/pax.h>
                     79: #endif /* PAX_MPROTECT */
1.1       mrg        80:
                     81: #include <miscfs/specfs/specdev.h>
                     82:
                     83: #include <sys/syscallargs.h>
                     84:
                     85: #include <uvm/uvm.h>
                     86: #include <uvm/uvm_device.h>
                     87:
1.80      jdolecek   88: #ifndef COMPAT_ZERODEV
1.81      dsl        89: #define COMPAT_ZERODEV(dev)    (0)
1.80      jdolecek   90: #endif
1.1       mrg        91:
1.115     yamt       92: static int
                     93: range_test(vaddr_t addr, vsize_t size, bool ismmap)
                     94: {
                     95:        vaddr_t vm_min_address = VM_MIN_ADDRESS;
                     96:        vaddr_t vm_max_address = VM_MAXUSER_ADDRESS;
                     97:        vaddr_t eaddr = addr + size;
                     98:
                     99:        if (addr < vm_min_address)
                    100:                return EINVAL;
                    101:        if (eaddr > vm_max_address)
                    102:                return ismmap ? EFBIG : EINVAL;
                    103:        if (addr > eaddr) /* no wrapping! */
                    104:                return ismmap ? EOVERFLOW : EINVAL;
                    105:        return 0;
                    106: }
1.110     christos  107:
1.1       mrg       108: /*
                    109:  * unimplemented VM system calls:
                    110:  */
                    111:
                    112: /*
                    113:  * sys_sbrk: sbrk system call.
                    114:  */
                    115:
                    116: /* ARGSUSED */
1.6       mrg       117: int
1.119     dsl       118: sys_sbrk(struct lwp *l, const struct sys_sbrk_args *uap, register_t *retval)
1.1       mrg       119: {
1.119     dsl       120:        /* {
1.33      kleink    121:                syscallarg(intptr_t) incr;
1.119     dsl       122:        } */
1.6       mrg       123:
1.17      kleink    124:        return (ENOSYS);
1.1       mrg       125: }
                    126:
                    127: /*
                    128:  * sys_sstk: sstk system call.
                    129:  */
                    130:
                    131: /* ARGSUSED */
1.6       mrg       132: int
1.119     dsl       133: sys_sstk(struct lwp *l, const struct sys_sstk_args *uap, register_t *retval)
1.1       mrg       134: {
1.119     dsl       135:        /* {
1.20      mrg       136:                syscallarg(int) incr;
1.119     dsl       137:        } */
1.6       mrg       138:
1.17      kleink    139:        return (ENOSYS);
1.1       mrg       140: }
                    141:
                    142: /*
                    143:  * sys_mincore: determine if pages are in core or not.
                    144:  */
                    145:
                    146: /* ARGSUSED */
1.6       mrg       147: int
1.119     dsl       148: sys_mincore(struct lwp *l, const struct sys_mincore_args *uap, register_t *retval)
1.1       mrg       149: {
1.119     dsl       150:        /* {
1.22      thorpej   151:                syscallarg(void *) addr;
1.20      mrg       152:                syscallarg(size_t) len;
                    153:                syscallarg(char *) vec;
1.119     dsl       154:        } */
1.67      thorpej   155:        struct proc *p = l->l_proc;
1.56      chs       156:        struct vm_page *pg;
1.22      thorpej   157:        char *vec, pgi;
                    158:        struct uvm_object *uobj;
                    159:        struct vm_amap *amap;
                    160:        struct vm_anon *anon;
1.53      chs       161:        struct vm_map_entry *entry;
1.22      thorpej   162:        vaddr_t start, end, lim;
1.53      chs       163:        struct vm_map *map;
1.22      thorpej   164:        vsize_t len;
                    165:        int error = 0, npgs;
                    166:
                    167:        map = &p->p_vmspace->vm_map;
                    168:
                    169:        start = (vaddr_t)SCARG(uap, addr);
                    170:        len = SCARG(uap, len);
                    171:        vec = SCARG(uap, vec);
                    172:
                    173:        if (start & PAGE_MASK)
                    174:                return (EINVAL);
                    175:        len = round_page(len);
                    176:        end = start + len;
                    177:        if (end <= start)
                    178:                return (EINVAL);
                    179:
                    180:        /*
                    181:         * Lock down vec, so our returned status isn't outdated by
                    182:         * storing the status byte for a page.
                    183:         */
1.50      chs       184:
1.62      chs       185:        npgs = len >> PAGE_SHIFT;
1.100     chs       186:        error = uvm_vslock(p->p_vmspace, vec, npgs, VM_PROT_WRITE);
1.62      chs       187:        if (error) {
                    188:                return error;
                    189:        }
1.22      thorpej   190:        vm_map_lock_read(map);
                    191:
1.107     thorpej   192:        if (uvm_map_lookup_entry(map, start, &entry) == false) {
1.22      thorpej   193:                error = ENOMEM;
                    194:                goto out;
                    195:        }
                    196:
                    197:        for (/* nothing */;
                    198:             entry != &map->header && entry->start < end;
                    199:             entry = entry->next) {
1.49      chs       200:                KASSERT(!UVM_ET_ISSUBMAP(entry));
                    201:                KASSERT(start >= entry->start);
                    202:
1.22      thorpej   203:                /* Make sure there are no holes. */
                    204:                if (entry->end < end &&
                    205:                     (entry->next == &map->header ||
                    206:                      entry->next->start > entry->end)) {
                    207:                        error = ENOMEM;
                    208:                        goto out;
                    209:                }
1.6       mrg       210:
1.22      thorpej   211:                lim = end < entry->end ? end : entry->end;
                    212:
                    213:                /*
1.31      thorpej   214:                 * Special case for objects with no "real" pages.  Those
                    215:                 * are always considered resident (mapped devices).
1.22      thorpej   216:                 */
1.50      chs       217:
1.22      thorpej   218:                if (UVM_ET_ISOBJ(entry)) {
1.49      chs       219:                        KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj));
1.79      yamt      220:                        if (UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
1.22      thorpej   221:                                for (/* nothing */; start < lim;
                    222:                                     start += PAGE_SIZE, vec++)
                    223:                                        subyte(vec, 1);
                    224:                                continue;
                    225:                        }
                    226:                }
                    227:
1.32      thorpej   228:                amap = entry->aref.ar_amap;     /* top layer */
                    229:                uobj = entry->object.uvm_obj;   /* bottom layer */
1.22      thorpej   230:
                    231:                if (amap != NULL)
                    232:                        amap_lock(amap);
                    233:                if (uobj != NULL)
                    234:                        simple_lock(&uobj->vmobjlock);
                    235:
                    236:                for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) {
                    237:                        pgi = 0;
                    238:                        if (amap != NULL) {
                    239:                                /* Check the top layer first. */
                    240:                                anon = amap_lookup(&entry->aref,
                    241:                                    start - entry->start);
                    242:                                /* Don't need to lock anon here. */
1.91      yamt      243:                                if (anon != NULL && anon->an_page != NULL) {
1.50      chs       244:
1.22      thorpej   245:                                        /*
                    246:                                         * Anon has the page for this entry
                    247:                                         * offset.
                    248:                                         */
1.50      chs       249:
1.22      thorpej   250:                                        pgi = 1;
                    251:                                }
                    252:                        }
                    253:                        if (uobj != NULL && pgi == 0) {
                    254:                                /* Check the bottom layer. */
1.56      chs       255:                                pg = uvm_pagelookup(uobj,
1.22      thorpej   256:                                    entry->offset + (start - entry->start));
1.56      chs       257:                                if (pg != NULL) {
1.50      chs       258:
1.22      thorpej   259:                                        /*
                    260:                                         * Object has the page for this entry
                    261:                                         * offset.
                    262:                                         */
1.50      chs       263:
1.22      thorpej   264:                                        pgi = 1;
                    265:                                }
                    266:                        }
                    267:                        (void) subyte(vec, pgi);
                    268:                }
                    269:                if (uobj != NULL)
1.27      thorpej   270:                        simple_unlock(&uobj->vmobjlock);
1.22      thorpej   271:                if (amap != NULL)
                    272:                        amap_unlock(amap);
                    273:        }
                    274:
                    275:  out:
                    276:        vm_map_unlock_read(map);
1.100     chs       277:        uvm_vsunlock(p->p_vmspace, SCARG(uap, vec), npgs);
1.22      thorpej   278:        return (error);
1.1       mrg       279: }
                    280:
                    281: /*
                    282:  * sys_mmap: mmap system call.
                    283:  *
1.64      atatat    284:  * => file offset and address may not be page aligned
1.1       mrg       285:  *    - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
                    286:  *    - if address isn't page aligned the mapping starts at trunc_page(addr)
                    287:  *      and the return value is adjusted up by the page offset.
                    288:  */
                    289:
1.6       mrg       290: int
1.119     dsl       291: sys_mmap(struct lwp *l, const struct sys_mmap_args *uap, register_t *retval)
1.6       mrg       292: {
1.119     dsl       293:        /* {
1.108     christos  294:                syscallarg(void *) addr;
1.6       mrg       295:                syscallarg(size_t) len;
                    296:                syscallarg(int) prot;
                    297:                syscallarg(int) flags;
                    298:                syscallarg(int) fd;
                    299:                syscallarg(long) pad;
                    300:                syscallarg(off_t) pos;
1.119     dsl       301:        } */
1.67      thorpej   302:        struct proc *p = l->l_proc;
1.12      eeh       303:        vaddr_t addr;
1.9       mrg       304:        struct vattr va;
1.6       mrg       305:        off_t pos;
1.12      eeh       306:        vsize_t size, pageoff;
1.6       mrg       307:        vm_prot_t prot, maxprot;
                    308:        int flags, fd;
1.110     christos  309:        vaddr_t defaddr;
1.40      augustss  310:        struct filedesc *fdp = p->p_fd;
1.116     ad        311:        struct file *fp = NULL;
1.6       mrg       312:        struct vnode *vp;
1.50      chs       313:        void *handle;
1.6       mrg       314:        int error;
1.120   ! christos  315: #ifdef PAX_ASLR
        !           316:        vaddr_t orig_addr;
        !           317: #endif /* PAX_ASLR */
1.6       mrg       318:
                    319:        /*
                    320:         * first, extract syscall args from the uap.
                    321:         */
                    322:
1.50      chs       323:        addr = (vaddr_t)SCARG(uap, addr);
                    324:        size = (vsize_t)SCARG(uap, len);
1.6       mrg       325:        prot = SCARG(uap, prot) & VM_PROT_ALL;
                    326:        flags = SCARG(uap, flags);
                    327:        fd = SCARG(uap, fd);
                    328:        pos = SCARG(uap, pos);
                    329:
1.120   ! christos  330: #ifdef PAX_ASLR
        !           331:        orig_addr = addr;
        !           332: #endif /* PAX_ASLR */
        !           333:
1.6       mrg       334:        /*
1.24      thorpej   335:         * Fixup the old deprecated MAP_COPY into MAP_PRIVATE, and
                    336:         * validate the flags.
                    337:         */
                    338:        if (flags & MAP_COPY)
                    339:                flags = (flags & ~MAP_COPY) | MAP_PRIVATE;
                    340:        if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE))
                    341:                return (EINVAL);
                    342:
                    343:        /*
1.6       mrg       344:         * align file position and save offset.  adjust size.
                    345:         */
                    346:
                    347:        pageoff = (pos & PAGE_MASK);
                    348:        pos  -= pageoff;
                    349:        size += pageoff;                        /* add offset */
1.50      chs       350:        size = (vsize_t)round_page(size);       /* round up */
1.6       mrg       351:
                    352:        /*
1.51      chs       353:         * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
1.6       mrg       354:         */
                    355:        if (flags & MAP_FIXED) {
                    356:
                    357:                /* ensure address and file offset are aligned properly */
                    358:                addr -= pageoff;
                    359:                if (addr & PAGE_MASK)
                    360:                        return (EINVAL);
                    361:
1.115     yamt      362:                error = range_test(addr, size, true);
                    363:                if (error)
                    364:                        return error;
1.75      christos  365:        } else if (addr == 0 || !(flags & MAP_TRYFIXED)) {
1.6       mrg       366:
                    367:                /*
1.68      atatat    368:                 * not fixed: make sure we skip over the largest
                    369:                 * possible heap for non-topdown mapping arrangements.
                    370:                 * we will refine our guess later (e.g. to account for
                    371:                 * VAC, etc)
1.6       mrg       372:                 */
1.46      chs       373:
1.89      fvdl      374:                defaddr = p->p_emul->e_vm_default_addr(p,
                    375:                    (vaddr_t)p->p_vmspace->vm_daddr, size);
                    376:
1.68      atatat    377:                if (addr == 0 ||
                    378:                    !(p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
1.89      fvdl      379:                        addr = MAX(addr, defaddr);
1.68      atatat    380:                else
1.89      fvdl      381:                        addr = MIN(addr, defaddr);
1.6       mrg       382:        }
                    383:
                    384:        /*
                    385:         * check for file mappings (i.e. not anonymous) and verify file.
                    386:         */
                    387:
                    388:        if ((flags & MAP_ANON) == 0) {
                    389:
1.54      thorpej   390:                if ((fp = fd_getfile(fdp, fd)) == NULL)
                    391:                        return (EBADF);
1.116     ad        392:                if (fp->f_type != DTYPE_VNODE) {
                    393:                        mutex_exit(&fp->f_lock);
1.7       kleink    394:                        return (ENODEV);                /* only mmap vnodes! */
1.116     ad        395:                }
1.6       mrg       396:                vp = (struct vnode *)fp->f_data;        /* convert to vnode */
                    397:
1.11      thorpej   398:                if (vp->v_type != VREG && vp->v_type != VCHR &&
1.116     ad        399:                    vp->v_type != VBLK) {
                    400:                        mutex_exit(&fp->f_lock);
1.11      thorpej   401:                        return (ENODEV);  /* only REG/CHR/BLK support mmap */
1.116     ad        402:                }
                    403:                if (vp->v_type != VCHR && pos < 0) {
                    404:                        mutex_exit(&fp->f_lock);
1.61      chs       405:                        return (EINVAL);
1.116     ad        406:                }
                    407:                if (vp->v_type != VCHR && (pos + size) < pos) {
                    408:                        mutex_exit(&fp->f_lock);
1.39      kleink    409:                        return (EOVERFLOW);             /* no offset wrapping */
1.116     ad        410:                }
1.6       mrg       411:
                    412:                /* special case: catch SunOS style /dev/zero */
1.80      jdolecek  413:                if (vp->v_type == VCHR
                    414:                    && (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
1.6       mrg       415:                        flags |= MAP_ANON;
1.116     ad        416:                        mutex_exit(&fp->f_lock);
                    417:                        fp = NULL;
1.6       mrg       418:                        goto is_anon;
                    419:                }
                    420:
                    421:                /*
                    422:                 * Old programs may not select a specific sharing type, so
                    423:                 * default to an appropriate one.
                    424:                 *
                    425:                 * XXX: how does MAP_ANON fit in the picture?
                    426:                 */
1.24      thorpej   427:                if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
1.8       tv        428: #if defined(DEBUG)
1.6       mrg       429:                        printf("WARNING: defaulted mmap() share type to "
1.71      gmcgarry  430:                           "%s (pid %d command %s)\n", vp->v_type == VCHR ?
1.6       mrg       431:                           "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
                    432:                            p->p_comm);
1.1       mrg       433: #endif
1.6       mrg       434:                        if (vp->v_type == VCHR)
                    435:                                flags |= MAP_SHARED;    /* for a device */
                    436:                        else
                    437:                                flags |= MAP_PRIVATE;   /* for a file */
                    438:                }
                    439:
1.51      chs       440:                /*
1.6       mrg       441:                 * MAP_PRIVATE device mappings don't make sense (and aren't
                    442:                 * supported anyway).  However, some programs rely on this,
                    443:                 * so just change it to MAP_SHARED.
                    444:                 */
                    445:                if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
                    446:                        flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
                    447:                }
1.1       mrg       448:
1.6       mrg       449:                /*
                    450:                 * now check protection
                    451:                 */
                    452:
1.48      thorpej   453:                maxprot = VM_PROT_EXECUTE;
1.6       mrg       454:
                    455:                /* check read access */
                    456:                if (fp->f_flag & FREAD)
                    457:                        maxprot |= VM_PROT_READ;
1.116     ad        458:                else if (prot & PROT_READ) {
                    459:                        mutex_exit(&fp->f_lock);
1.6       mrg       460:                        return (EACCES);
1.116     ad        461:                }
                    462:                FILE_USE(fp);
1.6       mrg       463:
1.9       mrg       464:                /* check write access, shared case first */
1.6       mrg       465:                if (flags & MAP_SHARED) {
1.9       mrg       466:                        /*
                    467:                         * if the file is writable, only add PROT_WRITE to
                    468:                         * maxprot if the file is not immutable, append-only.
                    469:                         * otherwise, if we have asked for PROT_WRITE, return
                    470:                         * EPERM.
                    471:                         */
                    472:                        if (fp->f_flag & FWRITE) {
                    473:                                if ((error =
1.118     pooka     474:                                    VOP_GETATTR(vp, &va, l->l_cred))) {
1.116     ad        475:                                        FILE_UNUSE(fp, l);
1.9       mrg       476:                                        return (error);
1.116     ad        477:                                }
1.84      hannken   478:                                if ((va.va_flags &
                    479:                                    (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
1.9       mrg       480:                                        maxprot |= VM_PROT_WRITE;
1.116     ad        481:                                else if (prot & PROT_WRITE) {
                    482:                                        FILE_UNUSE(fp, l);
1.9       mrg       483:                                        return (EPERM);
1.116     ad        484:                                }
1.9       mrg       485:                        }
1.116     ad        486:                        else if (prot & PROT_WRITE) {
                    487:                                FILE_UNUSE(fp, l);
1.6       mrg       488:                                return (EACCES);
1.116     ad        489:                        }
1.6       mrg       490:                } else {
                    491:                        /* MAP_PRIVATE mappings can always write to */
                    492:                        maxprot |= VM_PROT_WRITE;
                    493:                }
1.50      chs       494:                handle = vp;
1.1       mrg       495:
1.6       mrg       496:        } else {                /* MAP_ANON case */
1.24      thorpej   497:                /*
                    498:                 * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
                    499:                 */
1.6       mrg       500:                if (fd != -1)
                    501:                        return (EINVAL);
1.1       mrg       502:
1.24      thorpej   503:  is_anon:              /* label for SunOS style /dev/zero */
1.6       mrg       504:                handle = NULL;
                    505:                maxprot = VM_PROT_ALL;
                    506:                pos = 0;
1.28      cgd       507:        }
                    508:
                    509:        /*
                    510:         * XXX (in)sanity check.  We don't do proper datasize checking
                    511:         * XXX for anonymous (or private writable) mmap().  However,
                    512:         * XXX know that if we're trying to allocate more than the amount
                    513:         * XXX remaining under our current data size limit, _that_ should
                    514:         * XXX be disallowed.
                    515:         */
                    516:        if ((flags & MAP_ANON) != 0 ||
                    517:            ((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) {
                    518:                if (size >
1.50      chs       519:                    (p->p_rlimit[RLIMIT_DATA].rlim_cur -
                    520:                     ctob(p->p_vmspace->vm_dsize))) {
1.116     ad        521:                        if (fp != NULL)
                    522:                                FILE_UNUSE(fp, l);
1.28      cgd       523:                        return (ENOMEM);
                    524:                }
1.6       mrg       525:        }
                    526:
1.112     elad      527: #if NVERIEXEC > 0
                    528:        if (handle != NULL) {
                    529:                /*
                    530:                 * Check if the file can be executed indirectly.
                    531:                 *
                    532:                 * XXX: This gives false warnings about "Incorrect access type"
                    533:                 * XXX: if the mapping is not executable. Harmless, but will be
                    534:                 * XXX: fixed as part of other changes.
                    535:                 */
                    536:                if (veriexec_verify(l, handle, "(mmap)", VERIEXEC_INDIRECT,
                    537:                    NULL)) {
                    538:                        /*
                    539:                         * Don't allow executable mappings if we can't
                    540:                         * indirectly execute the file.
                    541:                         */
1.116     ad        542:                        if (prot & VM_PROT_EXECUTE) {
                    543:                                if (fp != NULL)
                    544:                                        FILE_UNUSE(fp, l);
1.112     elad      545:                                return (EPERM);
1.116     ad        546:                        }
1.112     elad      547:
                    548:                        /*
                    549:                         * Strip the executable bit from 'maxprot' to make sure
                    550:                         * it can't be made executable later.
                    551:                         */
                    552:                        maxprot &= ~VM_PROT_EXECUTE;
                    553:                }
                    554:        }
                    555: #endif /* NVERIEXEC > 0 */
                    556:
1.97      elad      557: #ifdef PAX_MPROTECT
                    558:        pax_mprotect(l, &prot, &maxprot);
                    559: #endif /* PAX_MPROTECT */
                    560:
1.120   ! christos  561: #ifdef PAX_ASLR
        !           562:        pax_aslr(l, &addr, orig_addr, flags);
        !           563: #endif /* PAX_ASLR */
        !           564:
1.6       mrg       565:        /*
                    566:         * now let kernel internal function uvm_mmap do the work.
                    567:         */
                    568:
                    569:        error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
1.25      thorpej   570:            flags, handle, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
1.6       mrg       571:
                    572:        if (error == 0)
                    573:                /* remember to add offset */
                    574:                *retval = (register_t)(addr + pageoff);
1.1       mrg       575:
1.116     ad        576:        if (fp != NULL)
                    577:                FILE_UNUSE(fp, l);
                    578:
1.6       mrg       579:        return (error);
1.1       mrg       580: }
                    581:
                    582: /*
                    583:  * sys___msync13: the msync system call (a front-end for flush)
                    584:  */
                    585:
1.6       mrg       586: int
1.119     dsl       587: sys___msync13(struct lwp *l, const struct sys___msync13_args *uap, register_t *retval)
1.6       mrg       588: {
1.119     dsl       589:        /* {
1.108     christos  590:                syscallarg(void *) addr;
1.6       mrg       591:                syscallarg(size_t) len;
                    592:                syscallarg(int) flags;
1.119     dsl       593:        } */
1.67      thorpej   594:        struct proc *p = l->l_proc;
1.12      eeh       595:        vaddr_t addr;
                    596:        vsize_t size, pageoff;
1.53      chs       597:        struct vm_map *map;
1.50      chs       598:        int error, rv, flags, uvmflags;
1.6       mrg       599:
                    600:        /*
                    601:         * extract syscall args from the uap
                    602:         */
                    603:
1.12      eeh       604:        addr = (vaddr_t)SCARG(uap, addr);
                    605:        size = (vsize_t)SCARG(uap, len);
1.6       mrg       606:        flags = SCARG(uap, flags);
                    607:
                    608:        /* sanity check flags */
                    609:        if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
1.77      chs       610:            (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
                    611:            (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
                    612:                return (EINVAL);
1.6       mrg       613:        if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
1.77      chs       614:                flags |= MS_SYNC;
1.1       mrg       615:
1.6       mrg       616:        /*
1.50      chs       617:         * align the address to a page boundary and adjust the size accordingly.
1.6       mrg       618:         */
                    619:
                    620:        pageoff = (addr & PAGE_MASK);
                    621:        addr -= pageoff;
                    622:        size += pageoff;
1.50      chs       623:        size = (vsize_t)round_page(size);
1.6       mrg       624:
1.115     yamt      625:        error = range_test(addr, size, false);
                    626:        if (error)
                    627:                return error;
1.6       mrg       628:
                    629:        /*
                    630:         * get map
                    631:         */
                    632:
                    633:        map = &p->p_vmspace->vm_map;
                    634:
                    635:        /*
                    636:         * XXXCDC: do we really need this semantic?
                    637:         *
                    638:         * XXX Gak!  If size is zero we are supposed to sync "all modified
                    639:         * pages with the region containing addr".  Unfortunately, we
                    640:         * don't really keep track of individual mmaps so we approximate
                    641:         * by flushing the range of the map entry containing addr.
                    642:         * This can be incorrect if the region splits or is coalesced
                    643:         * with a neighbor.
                    644:         */
1.50      chs       645:
1.6       mrg       646:        if (size == 0) {
1.53      chs       647:                struct vm_map_entry *entry;
1.51      chs       648:
1.6       mrg       649:                vm_map_lock_read(map);
                    650:                rv = uvm_map_lookup_entry(map, addr, &entry);
1.107     thorpej   651:                if (rv == true) {
1.6       mrg       652:                        addr = entry->start;
                    653:                        size = entry->end - entry->start;
                    654:                }
                    655:                vm_map_unlock_read(map);
1.107     thorpej   656:                if (rv == false)
1.6       mrg       657:                        return (EINVAL);
                    658:        }
                    659:
                    660:        /*
                    661:         * translate MS_ flags into PGO_ flags
                    662:         */
1.50      chs       663:
1.34      thorpej   664:        uvmflags = PGO_CLEANIT;
                    665:        if (flags & MS_INVALIDATE)
                    666:                uvmflags |= PGO_FREE;
1.6       mrg       667:        if (flags & MS_SYNC)
                    668:                uvmflags |= PGO_SYNCIO;
                    669:
1.50      chs       670:        error = uvm_map_clean(map, addr, addr+size, uvmflags);
                    671:        return error;
1.1       mrg       672: }
                    673:
                    674: /*
                    675:  * sys_munmap: unmap a users memory
                    676:  */
                    677:
1.6       mrg       678: int
1.119     dsl       679: sys_munmap(struct lwp *l, const struct sys_munmap_args *uap, register_t *retval)
1.6       mrg       680: {
1.119     dsl       681:        /* {
1.108     christos  682:                syscallarg(void *) addr;
1.6       mrg       683:                syscallarg(size_t) len;
1.119     dsl       684:        } */
1.67      thorpej   685:        struct proc *p = l->l_proc;
1.12      eeh       686:        vaddr_t addr;
                    687:        vsize_t size, pageoff;
1.53      chs       688:        struct vm_map *map;
1.6       mrg       689:        struct vm_map_entry *dead_entries;
1.115     yamt      690:        int error;
1.6       mrg       691:
                    692:        /*
1.50      chs       693:         * get syscall args.
1.6       mrg       694:         */
                    695:
1.50      chs       696:        addr = (vaddr_t)SCARG(uap, addr);
                    697:        size = (vsize_t)SCARG(uap, len);
1.51      chs       698:
1.6       mrg       699:        /*
1.50      chs       700:         * align the address to a page boundary and adjust the size accordingly.
1.6       mrg       701:         */
                    702:
                    703:        pageoff = (addr & PAGE_MASK);
                    704:        addr -= pageoff;
                    705:        size += pageoff;
1.50      chs       706:        size = (vsize_t)round_page(size);
1.6       mrg       707:
                    708:        if (size == 0)
                    709:                return (0);
                    710:
1.115     yamt      711:        error = range_test(addr, size, false);
                    712:        if (error)
                    713:                return error;
1.110     christos  714:
1.6       mrg       715:        map = &p->p_vmspace->vm_map;
                    716:
                    717:        /*
1.51      chs       718:         * interesting system call semantic: make sure entire range is
1.6       mrg       719:         * allocated before allowing an unmap.
                    720:         */
                    721:
1.50      chs       722:        vm_map_lock(map);
1.66      mycroft   723: #if 0
1.6       mrg       724:        if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
                    725:                vm_map_unlock(map);
                    726:                return (EINVAL);
                    727:        }
1.66      mycroft   728: #endif
1.90      yamt      729:        uvm_unmap_remove(map, addr, addr + size, &dead_entries, NULL, 0);
1.50      chs       730:        vm_map_unlock(map);
1.6       mrg       731:        if (dead_entries != NULL)
                    732:                uvm_unmap_detach(dead_entries, 0);
                    733:        return (0);
1.1       mrg       734: }
                    735:
                    736: /*
                    737:  * sys_mprotect: the mprotect system call
                    738:  */
                    739:
1.6       mrg       740: int
1.119     dsl       741: sys_mprotect(struct lwp *l, const struct sys_mprotect_args *uap, register_t *retval)
1.6       mrg       742: {
1.119     dsl       743:        /* {
1.108     christos  744:                syscallarg(void *) addr;
1.76      chs       745:                syscallarg(size_t) len;
1.6       mrg       746:                syscallarg(int) prot;
1.119     dsl       747:        } */
1.67      thorpej   748:        struct proc *p = l->l_proc;
1.12      eeh       749:        vaddr_t addr;
                    750:        vsize_t size, pageoff;
1.6       mrg       751:        vm_prot_t prot;
1.50      chs       752:        int error;
1.6       mrg       753:
                    754:        /*
                    755:         * extract syscall args from uap
                    756:         */
                    757:
1.12      eeh       758:        addr = (vaddr_t)SCARG(uap, addr);
                    759:        size = (vsize_t)SCARG(uap, len);
1.6       mrg       760:        prot = SCARG(uap, prot) & VM_PROT_ALL;
                    761:
                    762:        /*
1.50      chs       763:         * align the address to a page boundary and adjust the size accordingly.
1.6       mrg       764:         */
1.50      chs       765:
1.6       mrg       766:        pageoff = (addr & PAGE_MASK);
                    767:        addr -= pageoff;
                    768:        size += pageoff;
1.76      chs       769:        size = round_page(size);
1.50      chs       770:
1.115     yamt      771:        error = range_test(addr, size, false);
                    772:        if (error)
                    773:                return error;
1.110     christos  774:
1.50      chs       775:        error = uvm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
1.107     thorpej   776:                                false);
1.50      chs       777:        return error;
1.1       mrg       778: }
                    779:
                    780: /*
                    781:  * sys_minherit: the minherit system call
                    782:  */
                    783:
1.6       mrg       784: int
1.119     dsl       785: sys_minherit(struct lwp *l, const struct sys_minherit_args *uap, register_t *retval)
1.6       mrg       786: {
1.119     dsl       787:        /* {
1.108     christos  788:                syscallarg(void *) addr;
1.6       mrg       789:                syscallarg(int) len;
                    790:                syscallarg(int) inherit;
1.119     dsl       791:        } */
1.67      thorpej   792:        struct proc *p = l->l_proc;
1.12      eeh       793:        vaddr_t addr;
                    794:        vsize_t size, pageoff;
1.40      augustss  795:        vm_inherit_t inherit;
1.50      chs       796:        int error;
1.51      chs       797:
1.12      eeh       798:        addr = (vaddr_t)SCARG(uap, addr);
                    799:        size = (vsize_t)SCARG(uap, len);
1.6       mrg       800:        inherit = SCARG(uap, inherit);
1.50      chs       801:
1.6       mrg       802:        /*
1.50      chs       803:         * align the address to a page boundary and adjust the size accordingly.
1.6       mrg       804:         */
                    805:
                    806:        pageoff = (addr & PAGE_MASK);
                    807:        addr -= pageoff;
                    808:        size += pageoff;
1.50      chs       809:        size = (vsize_t)round_page(size);
1.6       mrg       810:
1.115     yamt      811:        error = range_test(addr, size, false);
                    812:        if (error)
                    813:                return error;
1.110     christos  814:
1.50      chs       815:        error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size,
                    816:                                inherit);
                    817:        return error;
1.21      mrg       818: }
                    819:
                    820: /*
                    821:  * sys_madvise: give advice about memory usage.
                    822:  */
                    823:
                    824: /* ARGSUSED */
                    825: int
1.119     dsl       826: sys_madvise(struct lwp *l, const struct sys_madvise_args *uap, register_t *retval)
1.21      mrg       827: {
1.119     dsl       828:        /* {
1.108     christos  829:                syscallarg(void *) addr;
1.21      mrg       830:                syscallarg(size_t) len;
                    831:                syscallarg(int) behav;
1.119     dsl       832:        } */
1.67      thorpej   833:        struct proc *p = l->l_proc;
1.21      mrg       834:        vaddr_t addr;
                    835:        vsize_t size, pageoff;
1.50      chs       836:        int advice, error;
1.51      chs       837:
1.21      mrg       838:        addr = (vaddr_t)SCARG(uap, addr);
                    839:        size = (vsize_t)SCARG(uap, len);
                    840:        advice = SCARG(uap, behav);
                    841:
                    842:        /*
                    843:         * align the address to a page boundary, and adjust the size accordingly
                    844:         */
1.50      chs       845:
1.21      mrg       846:        pageoff = (addr & PAGE_MASK);
                    847:        addr -= pageoff;
                    848:        size += pageoff;
1.50      chs       849:        size = (vsize_t)round_page(size);
1.21      mrg       850:
1.115     yamt      851:        error = range_test(addr, size, false);
                    852:        if (error)
                    853:                return error;
1.29      thorpej   854:
                    855:        switch (advice) {
                    856:        case MADV_NORMAL:
                    857:        case MADV_RANDOM:
                    858:        case MADV_SEQUENTIAL:
1.50      chs       859:                error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size,
1.29      thorpej   860:                    advice);
                    861:                break;
                    862:
                    863:        case MADV_WILLNEED:
1.50      chs       864:
1.29      thorpej   865:                /*
                    866:                 * Activate all these pages, pre-faulting them in if
                    867:                 * necessary.
                    868:                 */
                    869:                /*
                    870:                 * XXX IMPLEMENT ME.
                    871:                 * Should invent a "weak" mode for uvm_fault()
                    872:                 * which would only do the PGO_LOCKED pgo_get().
                    873:                 */
1.50      chs       874:
1.29      thorpej   875:                return (0);
                    876:
                    877:        case MADV_DONTNEED:
1.50      chs       878:
1.29      thorpej   879:                /*
                    880:                 * Deactivate all these pages.  We don't need them
                    881:                 * any more.  We don't, however, toss the data in
                    882:                 * the pages.
                    883:                 */
1.50      chs       884:
                    885:                error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
1.29      thorpej   886:                    PGO_DEACTIVATE);
                    887:                break;
                    888:
                    889:        case MADV_FREE:
1.50      chs       890:
1.29      thorpej   891:                /*
                    892:                 * These pages contain no valid data, and may be
1.45      soren     893:                 * garbage-collected.  Toss all resources, including
1.30      thorpej   894:                 * any swap space in use.
1.29      thorpej   895:                 */
1.50      chs       896:
                    897:                error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
1.29      thorpej   898:                    PGO_FREE);
                    899:                break;
                    900:
                    901:        case MADV_SPACEAVAIL:
1.50      chs       902:
1.29      thorpej   903:                /*
                    904:                 * XXXMRG What is this?  I think it's:
                    905:                 *
                    906:                 *      Ensure that we have allocated backing-store
                    907:                 *      for these pages.
                    908:                 *
                    909:                 * This is going to require changes to the page daemon,
                    910:                 * as it will free swap space allocated to pages in core.
                    911:                 * There's also what to do for device/file/anonymous memory.
                    912:                 */
1.50      chs       913:
1.29      thorpej   914:                return (EINVAL);
                    915:
                    916:        default:
1.21      mrg       917:                return (EINVAL);
1.29      thorpej   918:        }
                    919:
1.50      chs       920:        return error;
1.1       mrg       921: }
                    922:
                    923: /*
                    924:  * sys_mlock: memory lock
                    925:  */
                    926:
1.6       mrg       927: int
1.119     dsl       928: sys_mlock(struct lwp *l, const struct sys_mlock_args *uap, register_t *retval)
1.6       mrg       929: {
1.119     dsl       930:        /* {
1.10      kleink    931:                syscallarg(const void *) addr;
1.6       mrg       932:                syscallarg(size_t) len;
1.119     dsl       933:        } */
1.67      thorpej   934:        struct proc *p = l->l_proc;
1.12      eeh       935:        vaddr_t addr;
                    936:        vsize_t size, pageoff;
1.6       mrg       937:        int error;
                    938:
                    939:        /*
                    940:         * extract syscall args from uap
                    941:         */
1.50      chs       942:
1.12      eeh       943:        addr = (vaddr_t)SCARG(uap, addr);
                    944:        size = (vsize_t)SCARG(uap, len);
1.6       mrg       945:
                    946:        /*
                    947:         * align the address to a page boundary and adjust the size accordingly
                    948:         */
1.50      chs       949:
1.6       mrg       950:        pageoff = (addr & PAGE_MASK);
                    951:        addr -= pageoff;
                    952:        size += pageoff;
1.50      chs       953:        size = (vsize_t)round_page(size);
1.51      chs       954:
1.115     yamt      955:        error = range_test(addr, size, false);
                    956:        if (error)
                    957:                return error;
1.1       mrg       958:
1.6       mrg       959:        if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
                    960:                return (EAGAIN);
1.1       mrg       961:
1.6       mrg       962:        if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
                    963:                        p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
                    964:                return (EAGAIN);
1.1       mrg       965:
1.107     thorpej   966:        error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, false,
1.35      thorpej   967:            0);
1.85      briggs    968:        if (error == EFAULT)
                    969:                error = ENOMEM;
1.50      chs       970:        return error;
1.1       mrg       971: }
                    972:
                    973: /*
                    974:  * sys_munlock: unlock wired pages
                    975:  */
                    976:
1.6       mrg       977: int
1.119     dsl       978: sys_munlock(struct lwp *l, const struct sys_munlock_args *uap, register_t *retval)
1.6       mrg       979: {
1.119     dsl       980:        /* {
1.10      kleink    981:                syscallarg(const void *) addr;
1.6       mrg       982:                syscallarg(size_t) len;
1.119     dsl       983:        } */
1.67      thorpej   984:        struct proc *p = l->l_proc;
1.12      eeh       985:        vaddr_t addr;
                    986:        vsize_t size, pageoff;
1.6       mrg       987:        int error;
                    988:
                    989:        /*
                    990:         * extract syscall args from uap
                    991:         */
                    992:
1.12      eeh       993:        addr = (vaddr_t)SCARG(uap, addr);
                    994:        size = (vsize_t)SCARG(uap, len);
1.6       mrg       995:
                    996:        /*
                    997:         * align the address to a page boundary, and adjust the size accordingly
                    998:         */
1.50      chs       999:
1.6       mrg      1000:        pageoff = (addr & PAGE_MASK);
                   1001:        addr -= pageoff;
                   1002:        size += pageoff;
1.50      chs      1003:        size = (vsize_t)round_page(size);
1.6       mrg      1004:
1.115     yamt     1005:        error = range_test(addr, size, false);
                   1006:        if (error)
                   1007:                return error;
1.1       mrg      1008:
1.107     thorpej  1009:        error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, true,
1.35      thorpej  1010:            0);
1.85      briggs   1011:        if (error == EFAULT)
                   1012:                error = ENOMEM;
1.50      chs      1013:        return error;
1.22      thorpej  1014: }
                   1015:
                   1016: /*
                   1017:  * sys_mlockall: lock all pages mapped into an address space.
                   1018:  */
                   1019:
                   1020: int
1.119     dsl      1021: sys_mlockall(struct lwp *l, const struct sys_mlockall_args *uap, register_t *retval)
1.22      thorpej  1022: {
1.119     dsl      1023:        /* {
1.22      thorpej  1024:                syscallarg(int) flags;
1.119     dsl      1025:        } */
1.67      thorpej  1026:        struct proc *p = l->l_proc;
1.22      thorpej  1027:        int error, flags;
                   1028:
                   1029:        flags = SCARG(uap, flags);
                   1030:
                   1031:        if (flags == 0 ||
                   1032:            (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
                   1033:                return (EINVAL);
                   1034:
1.25      thorpej  1035:        error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags,
                   1036:            p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
1.22      thorpej  1037:        return (error);
                   1038: }
                   1039:
                   1040: /*
                   1041:  * sys_munlockall: unlock all pages mapped into an address space.
                   1042:  */
                   1043:
                   1044: int
1.119     dsl      1045: sys_munlockall(struct lwp *l, const void *v, register_t *retval)
1.22      thorpej  1046: {
1.67      thorpej  1047:        struct proc *p = l->l_proc;
1.22      thorpej  1048:
                   1049:        (void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
                   1050:        return (0);
1.1       mrg      1051: }
                   1052:
                   1053: /*
                   1054:  * uvm_mmap: internal version of mmap
                   1055:  *
1.56      chs      1056:  * - used by sys_mmap and various framebuffers
                   1057:  * - handle is a vnode pointer or NULL for MAP_ANON
1.1       mrg      1058:  * - caller must page-align the file offset
                   1059:  */
                   1060:
1.6       mrg      1061: int
1.25      thorpej  1062: uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff, locklimit)
1.53      chs      1063:        struct vm_map *map;
1.12      eeh      1064:        vaddr_t *addr;
                   1065:        vsize_t size;
1.6       mrg      1066:        vm_prot_t prot, maxprot;
                   1067:        int flags;
1.50      chs      1068:        void *handle;
1.38      kleink   1069:        voff_t foff;
1.25      thorpej  1070:        vsize_t locklimit;
1.6       mrg      1071: {
                   1072:        struct uvm_object *uobj;
                   1073:        struct vnode *vp;
1.70      matt     1074:        vaddr_t align = 0;
1.50      chs      1075:        int error;
1.6       mrg      1076:        int advice = UVM_ADV_NORMAL;
                   1077:        uvm_flag_t uvmflag = 0;
1.106     thorpej  1078:        bool needwritemap;
1.6       mrg      1079:
                   1080:        /*
                   1081:         * check params
                   1082:         */
                   1083:
                   1084:        if (size == 0)
                   1085:                return(0);
                   1086:        if (foff & PAGE_MASK)
                   1087:                return(EINVAL);
                   1088:        if ((prot & maxprot) != prot)
                   1089:                return(EINVAL);
                   1090:
                   1091:        /*
                   1092:         * for non-fixed mappings, round off the suggested address.
                   1093:         * for fixed mappings, check alignment and zap old mappings.
                   1094:         */
                   1095:
                   1096:        if ((flags & MAP_FIXED) == 0) {
1.56      chs      1097:                *addr = round_page(*addr);
1.6       mrg      1098:        } else {
                   1099:                if (*addr & PAGE_MASK)
                   1100:                        return(EINVAL);
                   1101:                uvmflag |= UVM_FLAG_FIXED;
1.56      chs      1102:                (void) uvm_unmap(map, *addr, *addr + size);
1.6       mrg      1103:        }
                   1104:
                   1105:        /*
1.70      matt     1106:         * Try to see if any requested alignment can even be attemped.
                   1107:         * Make sure we can express the alignment (asking for a >= 4GB
                   1108:         * alignment on an ILP32 architecure make no sense) and the
                   1109:         * alignment is at least for a page sized quanitiy.  If the
                   1110:         * request was for a fixed mapping, make sure supplied address
                   1111:         * adheres to the request alignment.
                   1112:         */
                   1113:        align = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
                   1114:        if (align) {
                   1115:                if (align >= sizeof(vaddr_t) * NBBY)
                   1116:                        return(EINVAL);
                   1117:                align = 1L << align;
                   1118:                if (align < PAGE_SIZE)
                   1119:                        return(EINVAL);
1.88      chs      1120:                if (align >= vm_map_max(map))
1.70      matt     1121:                        return(ENOMEM);
                   1122:                if (flags & MAP_FIXED) {
                   1123:                        if ((*addr & (align-1)) != 0)
                   1124:                                return(EINVAL);
                   1125:                        align = 0;
                   1126:                }
                   1127:        }
                   1128:
                   1129:        /*
1.6       mrg      1130:         * handle anon vs. non-anon mappings.   for non-anon mappings attach
                   1131:         * to underlying vm object.
                   1132:         */
                   1133:
                   1134:        if (flags & MAP_ANON) {
1.95      christos 1135:                KASSERT(handle == NULL);
1.36      thorpej  1136:                foff = UVM_UNKNOWN_OFFSET;
1.6       mrg      1137:                uobj = NULL;
                   1138:                if ((flags & MAP_SHARED) == 0)
                   1139:                        /* XXX: defer amap create */
                   1140:                        uvmflag |= UVM_FLAG_COPYONW;
                   1141:                else
                   1142:                        /* shared: create amap now */
                   1143:                        uvmflag |= UVM_FLAG_OVERLAY;
                   1144:
                   1145:        } else {
1.95      christos 1146:                KASSERT(handle != NULL);
1.50      chs      1147:                vp = (struct vnode *)handle;
1.59      thorpej  1148:
                   1149:                /*
                   1150:                 * Don't allow mmap for EXEC if the file system
                   1151:                 * is mounted NOEXEC.
                   1152:                 */
                   1153:                if ((prot & PROT_EXEC) != 0 &&
                   1154:                    (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0)
                   1155:                        return (EACCES);
                   1156:
1.6       mrg      1157:                if (vp->v_type != VCHR) {
1.118     pooka    1158:                        error = VOP_MMAP(vp, prot, curlwp->l_cred);
1.55      chs      1159:                        if (error) {
                   1160:                                return error;
                   1161:                        }
1.113     pooka    1162:                        vref(vp);
                   1163:                        uobj = &vp->v_uobj;
1.57      thorpej  1164:
                   1165:                        /*
                   1166:                         * If the vnode is being mapped with PROT_EXEC,
                   1167:                         * then mark it as text.
                   1168:                         */
1.117     ad       1169:                        if (prot & PROT_EXEC) {
                   1170:                                simple_lock(&uobj->vmobjlock);
1.58      thorpej  1171:                                vn_markexec(vp);
1.117     ad       1172:                                simple_unlock(&uobj->vmobjlock);
                   1173:                        }
1.6       mrg      1174:                } else {
1.83      darrenr  1175:                        int i = maxprot;
                   1176:
1.48      thorpej  1177:                        /*
                   1178:                         * XXX Some devices don't like to be mapped with
1.83      darrenr  1179:                         * XXX PROT_EXEC or PROT_WRITE, but we don't really
                   1180:                         * XXX have a better way of handling this, right now
1.48      thorpej  1181:                         */
1.83      darrenr  1182:                        do {
                   1183:                                uobj = udv_attach((void *) &vp->v_rdev,
                   1184:                                    (flags & MAP_SHARED) ? i :
                   1185:                                    (i & ~VM_PROT_WRITE), foff, size);
                   1186:                                i--;
                   1187:                        } while ((uobj == NULL) && (i > 0));
1.6       mrg      1188:                        advice = UVM_ADV_RANDOM;
                   1189:                }
                   1190:                if (uobj == NULL)
1.11      thorpej  1191:                        return((vp->v_type == VREG) ? ENOMEM : EINVAL);
1.92      yamt     1192:                if ((flags & MAP_SHARED) == 0) {
1.6       mrg      1193:                        uvmflag |= UVM_FLAG_COPYONW;
1.100     chs      1194:                }
                   1195:
                   1196:                /*
                   1197:                 * Set vnode flags to indicate the new kinds of mapping.
                   1198:                 * We take the vnode lock in exclusive mode here to serialize
                   1199:                 * with direct I/O.
                   1200:                 */
                   1201:
1.117     ad       1202:                simple_lock(&vp->v_interlock);
                   1203:                needwritemap = (vp->v_iflag & VI_WRMAP) == 0 &&
1.100     chs      1204:                        (flags & MAP_SHARED) != 0 &&
                   1205:                        (maxprot & VM_PROT_WRITE) != 0;
1.117     ad       1206:                if ((vp->v_iflag & VI_MAPPED) == 0 || needwritemap) {
                   1207:                        vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK);
1.92      yamt     1208:                        simple_lock(&vp->v_interlock);
1.117     ad       1209:                        vp->v_iflag |= VI_MAPPED;
                   1210:                        vp->v_vflag |= VV_MAPPED;
1.100     chs      1211:                        if (needwritemap) {
1.117     ad       1212:                                vp->v_iflag |= VI_WRMAP;
1.100     chs      1213:                        }
1.92      yamt     1214:                        simple_unlock(&vp->v_interlock);
1.100     chs      1215:                        VOP_UNLOCK(vp, 0);
1.117     ad       1216:                } else
                   1217:                        simple_unlock(&vp->v_interlock);
1.6       mrg      1218:        }
                   1219:
1.51      chs      1220:        uvmflag = UVM_MAPFLAG(prot, maxprot,
1.1       mrg      1221:                        (flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
                   1222:                        advice, uvmflag);
1.70      matt     1223:        error = uvm_map(map, addr, size, uobj, foff, align, uvmflag);
1.50      chs      1224:        if (error) {
                   1225:                if (uobj)
                   1226:                        uobj->pgops->pgo_detach(uobj);
                   1227:                return error;
                   1228:        }
1.1       mrg      1229:
1.6       mrg      1230:        /*
1.50      chs      1231:         * POSIX 1003.1b -- if our address space was configured
                   1232:         * to lock all future mappings, wire the one we just made.
1.78      thorpej  1233:         *
                   1234:         * Also handle the MAP_WIRED flag here.
1.6       mrg      1235:         */
                   1236:
1.50      chs      1237:        if (prot == VM_PROT_NONE) {
1.6       mrg      1238:
1.25      thorpej  1239:                /*
1.50      chs      1240:                 * No more work to do in this case.
1.25      thorpej  1241:                 */
                   1242:
1.50      chs      1243:                return (0);
                   1244:        }
                   1245:        vm_map_lock(map);
1.78      thorpej  1246:        if ((flags & MAP_WIRED) != 0 || (map->flags & VM_MAP_WIREFUTURE) != 0) {
1.87      chs      1247:                if (atop(size) + uvmexp.wired > uvmexp.wiredmax ||
                   1248:                    (locklimit != 0 &&
                   1249:                     size + ptoa(pmap_wired_count(vm_map_pmap(map))) >
                   1250:                     locklimit)) {
1.50      chs      1251:                        vm_map_unlock(map);
                   1252:                        uvm_unmap(map, *addr, *addr + size);
                   1253:                        return ENOMEM;
1.25      thorpej  1254:                }
                   1255:
1.50      chs      1256:                /*
                   1257:                 * uvm_map_pageable() always returns the map unlocked.
                   1258:                 */
1.25      thorpej  1259:
1.50      chs      1260:                error = uvm_map_pageable(map, *addr, *addr + size,
1.107     thorpej  1261:                                         false, UVM_LK_ENTER);
1.50      chs      1262:                if (error) {
                   1263:                        uvm_unmap(map, *addr, *addr + size);
                   1264:                        return error;
                   1265:                }
1.25      thorpej  1266:                return (0);
                   1267:        }
1.50      chs      1268:        vm_map_unlock(map);
                   1269:        return 0;
1.1       mrg      1270: }
1.89      fvdl     1271:
                   1272: vaddr_t
1.102     yamt     1273: uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
1.89      fvdl     1274: {
1.102     yamt     1275:
1.89      fvdl     1276:        return VM_DEFAULT_ADDRESS(base, sz);
                   1277: }

CVSweb <webmaster@jp.NetBSD.org>