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

Annotation of src/sys/uvm/uvm_map.c, Revision 1.152

1.152   ! simonb      1: /*     $NetBSD: uvm_map.c,v 1.151 2003/11/13 02:44:02 chs Exp $        */
1.1       mrg         2:
1.98      chs         3: /*
1.1       mrg         4:  * Copyright (c) 1997 Charles D. Cranor and Washington University.
1.98      chs         5:  * Copyright (c) 1991, 1993, The Regents of the University of California.
1.1       mrg         6:  *
                      7:  * All rights reserved.
                      8:  *
                      9:  * This code is derived from software contributed to Berkeley by
                     10:  * The Mach Operating System project at Carnegie-Mellon University.
                     11:  *
                     12:  * Redistribution and use in source and binary forms, with or without
                     13:  * modification, are permitted provided that the following conditions
                     14:  * are met:
                     15:  * 1. Redistributions of source code must retain the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer.
                     17:  * 2. Redistributions in binary form must reproduce the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer in the
                     19:  *    documentation and/or other materials provided with the distribution.
                     20:  * 3. All advertising materials mentioning features or use of this software
                     21:  *    must display the following acknowledgement:
                     22:  *     This product includes software developed by Charles D. Cranor,
1.98      chs        23:  *      Washington University, the University of California, Berkeley and
1.1       mrg        24:  *      its contributors.
                     25:  * 4. Neither the name of the University nor the names of its contributors
                     26:  *    may be used to endorse or promote products derived from this software
                     27:  *    without specific prior written permission.
                     28:  *
                     29:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     30:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     31:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     32:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     33:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     34:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     35:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     36:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     37:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     38:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     39:  * SUCH DAMAGE.
                     40:  *
                     41:  *     @(#)vm_map.c    8.3 (Berkeley) 1/12/94
1.3       mrg        42:  * from: Id: uvm_map.c,v 1.1.2.27 1998/02/07 01:16:54 chs Exp
1.1       mrg        43:  *
                     44:  *
                     45:  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
                     46:  * All rights reserved.
1.98      chs        47:  *
1.1       mrg        48:  * Permission to use, copy, modify and distribute this software and
                     49:  * its documentation is hereby granted, provided that both the copyright
                     50:  * notice and this permission notice appear in all copies of the
                     51:  * software, derivative works or modified versions, and any portions
                     52:  * thereof, and that both notices appear in supporting documentation.
1.98      chs        53:  *
                     54:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     55:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
1.1       mrg        56:  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1.98      chs        57:  *
1.1       mrg        58:  * Carnegie Mellon requests users of this software to return to
                     59:  *
                     60:  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
                     61:  *  School of Computer Science
                     62:  *  Carnegie Mellon University
                     63:  *  Pittsburgh PA 15213-3890
                     64:  *
                     65:  * any improvements or extensions that they make and grant Carnegie the
                     66:  * rights to redistribute these changes.
                     67:  */
                     68:
1.114     lukem      69: /*
                     70:  * uvm_map.c: uvm map operations
                     71:  */
                     72:
                     73: #include <sys/cdefs.h>
1.152   ! simonb     74: __KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.151 2003/11/13 02:44:02 chs Exp $");
1.114     lukem      75:
1.21      jonathan   76: #include "opt_ddb.h"
1.6       mrg        77: #include "opt_uvmhist.h"
1.31      tron       78: #include "opt_sysv.h"
1.1       mrg        79:
                     80: #include <sys/param.h>
                     81: #include <sys/systm.h>
                     82: #include <sys/mman.h>
                     83: #include <sys/proc.h>
                     84: #include <sys/malloc.h>
1.25      thorpej    85: #include <sys/pool.h>
1.104     chs        86: #include <sys/kernel.h>
1.112     thorpej    87: #include <sys/mount.h>
1.109     thorpej    88: #include <sys/vnode.h>
1.1       mrg        89:
                     90: #ifdef SYSVSHM
                     91: #include <sys/shm.h>
                     92: #endif
                     93:
                     94: #define UVM_MAP
                     95: #include <uvm/uvm.h>
1.151     chs        96: #undef RB_AUGMENT
                     97: #define        RB_AUGMENT(x)   uvm_rb_augment(x)
1.21      jonathan   98:
                     99: #ifdef DDB
                    100: #include <uvm/uvm_ddb.h>
                    101: #endif
                    102:
1.106     chs       103: extern struct vm_map *pager_map;
1.1       mrg       104:
1.121     atatat    105: struct uvm_cnt map_ubackmerge, map_uforwmerge;
1.125     atatat    106: struct uvm_cnt map_ubimerge, map_unomerge;
1.121     atatat    107: struct uvm_cnt map_kbackmerge, map_kforwmerge;
1.125     atatat    108: struct uvm_cnt map_kbimerge, map_knomerge;
1.121     atatat    109: struct uvm_cnt uvm_map_call, uvm_mlk_call, uvm_mlk_hint;
1.87      enami     110: const char vmmapbsy[] = "vmmapbsy";
1.1       mrg       111:
                    112: /*
1.25      thorpej   113:  * pool for vmspace structures.
                    114:  */
                    115:
                    116: struct pool uvm_vmspace_pool;
                    117:
1.26      thorpej   118: /*
                    119:  * pool for dynamically-allocated map entries.
                    120:  */
                    121:
                    122: struct pool uvm_map_entry_pool;
1.104     chs       123: struct pool uvm_map_entry_kmem_pool;
1.130     thorpej   124:
                    125: MALLOC_DEFINE(M_VMMAP, "VM map", "VM map structures");
                    126: MALLOC_DEFINE(M_VMPMAP, "VM pmap", "VM pmap");
1.25      thorpej   127:
1.40      thorpej   128: #ifdef PMAP_GROWKERNEL
                    129: /*
                    130:  * This global represents the end of the kernel virtual address
                    131:  * space.  If we want to exceed this, we must grow the kernel
                    132:  * virtual address space dynamically.
                    133:  *
                    134:  * Note, this variable is locked by kernel_map's lock.
                    135:  */
                    136: vaddr_t uvm_maxkaddr;
                    137: #endif
                    138:
1.25      thorpej   139: /*
1.1       mrg       140:  * macros
                    141:  */
                    142:
                    143: /*
                    144:  * uvm_map_entry_link: insert entry into a map
                    145:  *
                    146:  * => map must be locked
                    147:  */
1.10      mrg       148: #define uvm_map_entry_link(map, after_where, entry) do { \
1.144     yamt      149:        KASSERT(entry->start < entry->end); \
1.10      mrg       150:        (map)->nentries++; \
                    151:        (entry)->prev = (after_where); \
                    152:        (entry)->next = (after_where)->next; \
                    153:        (entry)->prev->next = (entry); \
                    154:        (entry)->next->prev = (entry); \
1.144     yamt      155:        uvm_rb_insert((map), (entry)); \
1.124     perry     156: } while (/*CONSTCOND*/ 0)
1.10      mrg       157:
1.1       mrg       158: /*
                    159:  * uvm_map_entry_unlink: remove entry from a map
                    160:  *
                    161:  * => map must be locked
                    162:  */
1.10      mrg       163: #define uvm_map_entry_unlink(map, entry) do { \
                    164:        (map)->nentries--; \
                    165:        (entry)->next->prev = (entry)->prev; \
                    166:        (entry)->prev->next = (entry)->next; \
1.144     yamt      167:        uvm_rb_remove((map), (entry)); \
1.124     perry     168: } while (/*CONSTCOND*/ 0)
1.1       mrg       169:
                    170: /*
                    171:  * SAVE_HINT: saves the specified entry as the hint for future lookups.
                    172:  *
                    173:  * => map need not be locked (protected by hint_lock).
                    174:  */
1.82      thorpej   175: #define SAVE_HINT(map,check,value) do { \
1.10      mrg       176:        simple_lock(&(map)->hint_lock); \
1.82      thorpej   177:        if ((map)->hint == (check)) \
                    178:                (map)->hint = (value); \
1.10      mrg       179:        simple_unlock(&(map)->hint_lock); \
1.124     perry     180: } while (/*CONSTCOND*/ 0)
1.1       mrg       181:
                    182: /*
                    183:  * VM_MAP_RANGE_CHECK: check and correct range
                    184:  *
                    185:  * => map must at least be read locked
                    186:  */
                    187:
1.10      mrg       188: #define VM_MAP_RANGE_CHECK(map, start, end) do { \
1.139     enami     189:        if (start < vm_map_min(map))            \
                    190:                start = vm_map_min(map);        \
                    191:        if (end > vm_map_max(map))              \
                    192:                end = vm_map_max(map);          \
                    193:        if (start > end)                        \
                    194:                start = end;                    \
1.124     perry     195: } while (/*CONSTCOND*/ 0)
1.1       mrg       196:
                    197: /*
                    198:  * local prototypes
                    199:  */
                    200:
1.138     enami     201: static struct vm_map_entry *
                    202:                uvm_mapent_alloc(struct vm_map *, int);
                    203: static void    uvm_mapent_copy(struct vm_map_entry *, struct vm_map_entry *);
                    204: static void    uvm_mapent_free(struct vm_map_entry *);
                    205: static void    uvm_map_entry_unwire(struct vm_map *, struct vm_map_entry *);
                    206: static void    uvm_map_reference_amap(struct vm_map_entry *, int);
1.140     enami     207: static int     uvm_map_space_avail(vaddr_t *, vsize_t, voff_t, vsize_t, int,
                    208:                    struct vm_map_entry *);
1.138     enami     209: static void    uvm_map_unreference_amap(struct vm_map_entry *, int);
1.1       mrg       210:
1.144     yamt      211: int _uvm_tree_sanity(struct vm_map *, const char *);
                    212: static vsize_t uvm_rb_subtree_space(const struct vm_map_entry *);
                    213:
                    214: static __inline int
                    215: uvm_compare(const struct vm_map_entry *a, const struct vm_map_entry *b)
                    216: {
                    217:
                    218:        if (a->start < b->start)
                    219:                return (-1);
                    220:        else if (a->start > b->start)
                    221:                return (1);
                    222:
                    223:        return (0);
                    224: }
                    225:
                    226: static __inline void
                    227: uvm_rb_augment(struct vm_map_entry *entry)
                    228: {
                    229:
                    230:        entry->space = uvm_rb_subtree_space(entry);
                    231: }
                    232:
                    233: RB_PROTOTYPE(uvm_tree, vm_map_entry, rb_entry, uvm_compare);
                    234:
                    235: RB_GENERATE(uvm_tree, vm_map_entry, rb_entry, uvm_compare);
                    236:
                    237: static __inline vsize_t
                    238: uvm_rb_space(const struct vm_map *map, const struct vm_map_entry *entry)
                    239: {
                    240:        /* XXX map is not used */
                    241:
                    242:        KASSERT(entry->next != NULL);
                    243:        return entry->next->start - entry->end;
                    244: }
                    245:
                    246: static vsize_t
                    247: uvm_rb_subtree_space(const struct vm_map_entry *entry)
                    248: {
                    249:        vaddr_t space, tmp;
                    250:
                    251:        space = entry->ownspace;
                    252:        if (RB_LEFT(entry, rb_entry)) {
                    253:                tmp = RB_LEFT(entry, rb_entry)->space;
                    254:                if (tmp > space)
                    255:                        space = tmp;
                    256:        }
                    257:
                    258:        if (RB_RIGHT(entry, rb_entry)) {
                    259:                tmp = RB_RIGHT(entry, rb_entry)->space;
                    260:                if (tmp > space)
                    261:                        space = tmp;
                    262:        }
                    263:
                    264:        return (space);
                    265: }
                    266:
                    267: static __inline void
                    268: uvm_rb_fixup(struct vm_map *map, struct vm_map_entry *entry)
                    269: {
                    270:        /* We need to traverse to the very top */
                    271:        do {
                    272:                entry->ownspace = uvm_rb_space(map, entry);
                    273:                entry->space = uvm_rb_subtree_space(entry);
                    274:        } while ((entry = RB_PARENT(entry, rb_entry)) != NULL);
                    275: }
                    276:
                    277: static __inline void
                    278: uvm_rb_insert(struct vm_map *map, struct vm_map_entry *entry)
                    279: {
                    280:        vaddr_t space = uvm_rb_space(map, entry);
                    281:        struct vm_map_entry *tmp;
                    282:
                    283:        entry->ownspace = entry->space = space;
                    284:        tmp = RB_INSERT(uvm_tree, &(map)->rbhead, entry);
                    285: #ifdef DIAGNOSTIC
                    286:        if (tmp != NULL)
                    287:                panic("uvm_rb_insert: duplicate entry?");
                    288: #endif
                    289:        uvm_rb_fixup(map, entry);
                    290:        if (entry->prev != &map->header)
                    291:                uvm_rb_fixup(map, entry->prev);
                    292: }
                    293:
                    294: static __inline void
                    295: uvm_rb_remove(struct vm_map *map, struct vm_map_entry *entry)
                    296: {
                    297:        struct vm_map_entry *parent;
                    298:
                    299:        parent = RB_PARENT(entry, rb_entry);
                    300:        RB_REMOVE(uvm_tree, &(map)->rbhead, entry);
                    301:        if (entry->prev != &map->header)
                    302:                uvm_rb_fixup(map, entry->prev);
                    303:        if (parent)
                    304:                uvm_rb_fixup(map, parent);
                    305: }
                    306:
                    307: #ifdef DEBUG
                    308: #define uvm_tree_sanity(x,y) _uvm_tree_sanity(x,y)
                    309: #else
                    310: #define uvm_tree_sanity(x,y)
                    311: #endif
                    312:
                    313: int
                    314: _uvm_tree_sanity(struct vm_map *map, const char *name)
                    315: {
                    316:        struct vm_map_entry *tmp, *trtmp;
                    317:        int n = 0, i = 1;
                    318:
                    319:        RB_FOREACH(tmp, uvm_tree, &map->rbhead) {
                    320:                if (tmp->ownspace != uvm_rb_space(map, tmp)) {
                    321:                        printf("%s: %d/%d ownspace %lx != %lx %s\n",
                    322:                            name, n + 1, map->nentries,
                    323:                            (ulong)tmp->ownspace, (ulong)uvm_rb_space(map, tmp),
                    324:                            tmp->next == &map->header ? "(last)" : "");
                    325:                        goto error;
                    326:                }
                    327:        }
                    328:        trtmp = NULL;
                    329:        RB_FOREACH(tmp, uvm_tree, &map->rbhead) {
                    330:                if (tmp->space != uvm_rb_subtree_space(tmp)) {
                    331:                        printf("%s: space %lx != %lx\n",
                    332:                            name, (ulong)tmp->space,
                    333:                            (ulong)uvm_rb_subtree_space(tmp));
                    334:                        goto error;
                    335:                }
                    336:                if (trtmp != NULL && trtmp->start >= tmp->start) {
                    337:                        printf("%s: corrupt: 0x%lx >= 0x%lx\n",
                    338:                            name, trtmp->start, tmp->start);
                    339:                        goto error;
                    340:                }
                    341:                n++;
                    342:
                    343:                trtmp = tmp;
                    344:        }
                    345:
                    346:        if (n != map->nentries) {
                    347:                printf("%s: nentries: %d vs %d\n",
                    348:                    name, n, map->nentries);
                    349:                goto error;
                    350:        }
                    351:
                    352:        for (tmp = map->header.next; tmp && tmp != &map->header;
                    353:            tmp = tmp->next, i++) {
                    354:                trtmp = RB_FIND(uvm_tree, &map->rbhead, tmp);
                    355:                if (trtmp != tmp) {
                    356:                        printf("%s: lookup: %d: %p - %p: %p\n",
                    357:                            name, i, tmp, trtmp,
                    358:                            RB_PARENT(tmp, rb_entry));
                    359:                        goto error;
                    360:                }
                    361:        }
                    362:
                    363:        return (0);
                    364:  error:
                    365: #ifdef DDB
                    366:        /* handy breakpoint location for error case */
                    367:        __asm(".globl treesanity_label\ntreesanity_label:");
                    368: #endif
                    369:        return (-1);
                    370: }
                    371:
1.1       mrg       372: /*
                    373:  * local inlines
                    374:  */
                    375:
                    376: /*
                    377:  * uvm_mapent_alloc: allocate a map entry
                    378:  */
                    379:
1.99      chs       380: static __inline struct vm_map_entry *
1.138     enami     381: uvm_mapent_alloc(struct vm_map *map, int flags)
1.10      mrg       382: {
1.99      chs       383:        struct vm_map_entry *me;
1.10      mrg       384:        int s;
1.127     thorpej   385:        int pflags = (flags & UVM_FLAG_NOWAIT) ? PR_NOWAIT : PR_WAITOK;
1.104     chs       386:        UVMHIST_FUNC("uvm_mapent_alloc"); UVMHIST_CALLED(maphist);
1.1       mrg       387:
1.104     chs       388:        if (map->flags & VM_MAP_INTRSAFE || cold) {
                    389:                s = splvm();
1.10      mrg       390:                simple_lock(&uvm.kentry_lock);
                    391:                me = uvm.kentry_free;
1.139     enami     392:                if (me)
                    393:                        uvm.kentry_free = me->next;
1.10      mrg       394:                simple_unlock(&uvm.kentry_lock);
                    395:                splx(s);
1.126     bouyer    396:                if (__predict_false(me == NULL)) {
1.104     chs       397:                        panic("uvm_mapent_alloc: out of static map entries, "
1.139     enami     398:                            "check MAX_KMAPENT (currently %d)",
                    399:                            MAX_KMAPENT);
1.104     chs       400:                }
1.10      mrg       401:                me->flags = UVM_MAP_STATIC;
1.104     chs       402:        } else if (map == kernel_map) {
1.126     bouyer    403:                me = pool_get(&uvm_map_entry_kmem_pool, pflags);
                    404:                if (__predict_false(me == NULL))
                    405:                        return NULL;
1.104     chs       406:                me->flags = UVM_MAP_KMEM;
                    407:        } else {
1.126     bouyer    408:                me = pool_get(&uvm_map_entry_pool, pflags);
                    409:                if (__predict_false(me == NULL))
                    410:                        return NULL;
1.104     chs       411:                me->flags = 0;
1.10      mrg       412:        }
1.1       mrg       413:
1.104     chs       414:        UVMHIST_LOG(maphist, "<- new entry=0x%x [kentry=%d]", me,
                    415:            ((map->flags & VM_MAP_INTRSAFE) != 0 || map == kernel_map), 0, 0);
1.139     enami     416:        return (me);
1.1       mrg       417: }
                    418:
                    419: /*
                    420:  * uvm_mapent_free: free map entry
                    421:  */
                    422:
1.10      mrg       423: static __inline void
1.138     enami     424: uvm_mapent_free(struct vm_map_entry *me)
1.1       mrg       425: {
1.10      mrg       426:        int s;
1.104     chs       427:        UVMHIST_FUNC("uvm_mapent_free"); UVMHIST_CALLED(maphist);
                    428:
1.98      chs       429:        UVMHIST_LOG(maphist,"<- freeing map entry=0x%x [flags=%d]",
1.1       mrg       430:                me, me->flags, 0, 0);
1.104     chs       431:        if (me->flags & UVM_MAP_STATIC) {
                    432:                s = splvm();
1.10      mrg       433:                simple_lock(&uvm.kentry_lock);
                    434:                me->next = uvm.kentry_free;
                    435:                uvm.kentry_free = me;
                    436:                simple_unlock(&uvm.kentry_lock);
                    437:                splx(s);
1.104     chs       438:        } else if (me->flags & UVM_MAP_KMEM) {
                    439:                pool_put(&uvm_map_entry_kmem_pool, me);
                    440:        } else {
                    441:                pool_put(&uvm_map_entry_pool, me);
1.10      mrg       442:        }
1.1       mrg       443: }
                    444:
                    445: /*
                    446:  * uvm_mapent_copy: copy a map entry, preserving flags
                    447:  */
                    448:
1.10      mrg       449: static __inline void
1.138     enami     450: uvm_mapent_copy(struct vm_map_entry *src, struct vm_map_entry *dst)
1.10      mrg       451: {
1.139     enami     452:
1.106     chs       453:        memcpy(dst, src, ((char *)&src->uvm_map_entry_stop_copy) -
1.139     enami     454:            ((char *)src));
1.1       mrg       455: }
                    456:
                    457: /*
                    458:  * uvm_map_entry_unwire: unwire a map entry
                    459:  *
                    460:  * => map should be locked by caller
                    461:  */
                    462:
1.10      mrg       463: static __inline void
1.138     enami     464: uvm_map_entry_unwire(struct vm_map *map, struct vm_map_entry *entry)
1.10      mrg       465: {
1.139     enami     466:
1.10      mrg       467:        entry->wired_count = 0;
1.57      thorpej   468:        uvm_fault_unwire_locked(map, entry->start, entry->end);
1.1       mrg       469: }
                    470:
1.85      chs       471:
                    472: /*
                    473:  * wrapper for calling amap_ref()
                    474:  */
                    475: static __inline void
1.138     enami     476: uvm_map_reference_amap(struct vm_map_entry *entry, int flags)
1.85      chs       477: {
1.139     enami     478:
1.99      chs       479:        amap_ref(entry->aref.ar_amap, entry->aref.ar_pageoff,
1.139     enami     480:            (entry->end - entry->start) >> PAGE_SHIFT, flags);
1.85      chs       481: }
                    482:
                    483:
                    484: /*
1.98      chs       485:  * wrapper for calling amap_unref()
1.85      chs       486:  */
                    487: static __inline void
1.138     enami     488: uvm_map_unreference_amap(struct vm_map_entry *entry, int flags)
1.85      chs       489: {
1.139     enami     490:
1.99      chs       491:        amap_unref(entry->aref.ar_amap, entry->aref.ar_pageoff,
1.139     enami     492:            (entry->end - entry->start) >> PAGE_SHIFT, flags);
1.85      chs       493: }
                    494:
                    495:
1.1       mrg       496: /*
                    497:  * uvm_map_init: init mapping system at boot time.   note that we allocate
1.99      chs       498:  * and init the static pool of struct vm_map_entry *'s for the kernel here.
1.1       mrg       499:  */
                    500:
1.10      mrg       501: void
1.138     enami     502: uvm_map_init(void)
1.1       mrg       503: {
1.10      mrg       504:        static struct vm_map_entry kernel_map_entry[MAX_KMAPENT];
1.1       mrg       505: #if defined(UVMHIST)
1.10      mrg       506:        static struct uvm_history_ent maphistbuf[100];
                    507:        static struct uvm_history_ent pdhistbuf[100];
1.1       mrg       508: #endif
1.10      mrg       509:        int lcv;
                    510:
                    511:        /*
                    512:         * first, init logging system.
                    513:         */
1.1       mrg       514:
1.10      mrg       515:        UVMHIST_FUNC("uvm_map_init");
                    516:        UVMHIST_INIT_STATIC(maphist, maphistbuf);
                    517:        UVMHIST_INIT_STATIC(pdhist, pdhistbuf);
                    518:        UVMHIST_CALLED(maphist);
                    519:        UVMHIST_LOG(maphist,"<starting uvm map system>", 0, 0, 0, 0);
1.139     enami     520:        UVMCNT_INIT(uvm_map_call, UVMCNT_CNT, 0,
1.10      mrg       521:            "# uvm_map() successful calls", 0);
1.121     atatat    522:
                    523:        UVMCNT_INIT(map_ubackmerge, UVMCNT_CNT, 0,
                    524:            "# uvm_map() back umerges", 0);
                    525:        UVMCNT_INIT(map_uforwmerge, UVMCNT_CNT, 0,
                    526:            "# uvm_map() forward umerges", 0);
                    527:        UVMCNT_INIT(map_ubimerge, UVMCNT_CNT, 0,
                    528:            "# uvm_map() dual umerge", 0);
                    529:        UVMCNT_INIT(map_unomerge, UVMCNT_CNT, 0,
                    530:            "# uvm_map() no umerge", 0);
                    531:
                    532:        UVMCNT_INIT(map_kbackmerge, UVMCNT_CNT, 0,
                    533:            "# uvm_map() back kmerges", 0);
                    534:        UVMCNT_INIT(map_kforwmerge, UVMCNT_CNT, 0,
                    535:            "# uvm_map() forward kmerges", 0);
                    536:        UVMCNT_INIT(map_kbimerge, UVMCNT_CNT, 0,
                    537:            "# uvm_map() dual kmerge", 0);
                    538:        UVMCNT_INIT(map_knomerge, UVMCNT_CNT, 0,
                    539:            "# uvm_map() no kmerge", 0);
                    540:
1.139     enami     541:        UVMCNT_INIT(uvm_mlk_call, UVMCNT_CNT, 0, "# map lookup calls", 0);
                    542:        UVMCNT_INIT(uvm_mlk_hint, UVMCNT_CNT, 0, "# map lookup hint hits", 0);
1.10      mrg       543:
                    544:        /*
                    545:         * now set up static pool of kernel map entrys ...
                    546:         */
                    547:
                    548:        simple_lock_init(&uvm.kentry_lock);
                    549:        uvm.kentry_free = NULL;
                    550:        for (lcv = 0 ; lcv < MAX_KMAPENT ; lcv++) {
                    551:                kernel_map_entry[lcv].next = uvm.kentry_free;
                    552:                uvm.kentry_free = &kernel_map_entry[lcv];
                    553:        }
1.1       mrg       554:
1.25      thorpej   555:        /*
                    556:         * initialize the map-related pools.
                    557:         */
                    558:        pool_init(&uvm_vmspace_pool, sizeof(struct vmspace),
1.118     thorpej   559:            0, 0, 0, "vmsppl", &pool_allocator_nointr);
1.26      thorpej   560:        pool_init(&uvm_map_entry_pool, sizeof(struct vm_map_entry),
1.118     thorpej   561:            0, 0, 0, "vmmpepl", &pool_allocator_nointr);
1.104     chs       562:        pool_init(&uvm_map_entry_kmem_pool, sizeof(struct vm_map_entry),
1.118     thorpej   563:            0, 0, 0, "vmmpekpl", NULL);
1.1       mrg       564: }
                    565:
                    566: /*
                    567:  * clippers
                    568:  */
                    569:
                    570: /*
                    571:  * uvm_map_clip_start: ensure that the entry begins at or after
                    572:  *     the starting address, if it doesn't we split the entry.
1.98      chs       573:  *
1.1       mrg       574:  * => caller should use UVM_MAP_CLIP_START macro rather than calling
                    575:  *    this directly
                    576:  * => map must be locked by caller
                    577:  */
                    578:
1.99      chs       579: void
1.138     enami     580: uvm_map_clip_start(struct vm_map *map, struct vm_map_entry *entry,
                    581:     vaddr_t start)
1.1       mrg       582: {
1.99      chs       583:        struct vm_map_entry *new_entry;
1.24      eeh       584:        vaddr_t new_adj;
1.1       mrg       585:
                    586:        /* uvm_map_simplify_entry(map, entry); */ /* XXX */
                    587:
1.144     yamt      588:        uvm_tree_sanity(map, "clip_start entry");
                    589:
1.10      mrg       590:        /*
                    591:         * Split off the front portion.  note that we must insert the new
                    592:         * entry BEFORE this one, so that this entry has the specified
1.1       mrg       593:         * starting address.
1.10      mrg       594:         */
1.1       mrg       595:
1.126     bouyer    596:        new_entry = uvm_mapent_alloc(map, 0);
1.1       mrg       597:        uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
1.85      chs       598:
1.98      chs       599:        new_entry->end = start;
1.1       mrg       600:        new_adj = start - new_entry->start;
                    601:        if (entry->object.uvm_obj)
1.10      mrg       602:                entry->offset += new_adj;       /* shift start over */
1.144     yamt      603:
                    604:        /* Does not change order for the RB tree */
1.10      mrg       605:        entry->start = start;
1.1       mrg       606:
                    607:        if (new_entry->aref.ar_amap) {
1.10      mrg       608:                amap_splitref(&new_entry->aref, &entry->aref, new_adj);
1.1       mrg       609:        }
                    610:
1.10      mrg       611:        uvm_map_entry_link(map, entry->prev, new_entry);
1.85      chs       612:
1.29      chuck     613:        if (UVM_ET_ISSUBMAP(entry)) {
                    614:                /* ... unlikely to happen, but play it safe */
                    615:                 uvm_map_reference(new_entry->object.sub_map);
1.1       mrg       616:        } else {
1.98      chs       617:                if (UVM_ET_ISOBJ(entry) &&
1.10      mrg       618:                    entry->object.uvm_obj->pgops &&
                    619:                    entry->object.uvm_obj->pgops->pgo_reference)
                    620:                        entry->object.uvm_obj->pgops->pgo_reference(
                    621:                            entry->object.uvm_obj);
1.1       mrg       622:        }
1.144     yamt      623:
                    624:        uvm_tree_sanity(map, "clip_start leave");
1.1       mrg       625: }
                    626:
                    627: /*
                    628:  * uvm_map_clip_end: ensure that the entry ends at or before
                    629:  *     the ending address, if it does't we split the reference
1.98      chs       630:  *
1.1       mrg       631:  * => caller should use UVM_MAP_CLIP_END macro rather than calling
                    632:  *    this directly
                    633:  * => map must be locked by caller
                    634:  */
                    635:
1.10      mrg       636: void
1.138     enami     637: uvm_map_clip_end(struct vm_map *map, struct vm_map_entry *entry, vaddr_t end)
1.1       mrg       638: {
1.99      chs       639:        struct vm_map_entry *   new_entry;
1.24      eeh       640:        vaddr_t new_adj; /* #bytes we move start forward */
1.1       mrg       641:
1.144     yamt      642:        uvm_tree_sanity(map, "clip_end entry");
1.1       mrg       643:        /*
                    644:         *      Create a new entry and insert it
                    645:         *      AFTER the specified entry
                    646:         */
                    647:
1.126     bouyer    648:        new_entry = uvm_mapent_alloc(map, 0);
1.1       mrg       649:        uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
                    650:
                    651:        new_entry->start = entry->end = end;
                    652:        new_adj = end - entry->start;
                    653:        if (new_entry->object.uvm_obj)
                    654:                new_entry->offset += new_adj;
                    655:
1.10      mrg       656:        if (entry->aref.ar_amap)
                    657:                amap_splitref(&entry->aref, &new_entry->aref, new_adj);
1.1       mrg       658:
1.144     yamt      659:        uvm_rb_fixup(map, entry);
                    660:
1.1       mrg       661:        uvm_map_entry_link(map, entry, new_entry);
                    662:
1.29      chuck     663:        if (UVM_ET_ISSUBMAP(entry)) {
                    664:                /* ... unlikely to happen, but play it safe */
1.139     enami     665:                uvm_map_reference(new_entry->object.sub_map);
1.1       mrg       666:        } else {
1.10      mrg       667:                if (UVM_ET_ISOBJ(entry) &&
                    668:                    entry->object.uvm_obj->pgops &&
                    669:                    entry->object.uvm_obj->pgops->pgo_reference)
                    670:                        entry->object.uvm_obj->pgops->pgo_reference(
                    671:                            entry->object.uvm_obj);
1.1       mrg       672:        }
1.144     yamt      673:
                    674:        uvm_tree_sanity(map, "clip_end leave");
1.1       mrg       675: }
                    676:
                    677:
                    678: /*
                    679:  *   M A P   -   m a i n   e n t r y   p o i n t
                    680:  */
                    681: /*
                    682:  * uvm_map: establish a valid mapping in a map
                    683:  *
                    684:  * => assume startp is page aligned.
                    685:  * => assume size is a multiple of PAGE_SIZE.
                    686:  * => assume sys_mmap provides enough of a "hint" to have us skip
                    687:  *     over text/data/bss area.
                    688:  * => map must be unlocked (we will lock it)
                    689:  * => <uobj,uoffset> value meanings (4 cases):
1.139     enami     690:  *      [1] <NULL,uoffset>             == uoffset is a hint for PMAP_PREFER
1.1       mrg       691:  *      [2] <NULL,UVM_UNKNOWN_OFFSET>  == don't PMAP_PREFER
                    692:  *      [3] <uobj,uoffset>             == normal mapping
                    693:  *      [4] <uobj,UVM_UNKNOWN_OFFSET>  == uvm_map finds offset based on VA
1.98      chs       694:  *
1.1       mrg       695:  *    case [4] is for kernel mappings where we don't know the offset until
1.8       chuck     696:  *    we've found a virtual address.   note that kernel object offsets are
                    697:  *    always relative to vm_map_min(kernel_map).
1.81      thorpej   698:  *
                    699:  * => if `align' is non-zero, we try to align the virtual address to
                    700:  *     the specified alignment.  this is only a hint; if we can't
                    701:  *     do it, the address will be unaligned.  this is provided as
                    702:  *     a mechanism for large pages.
                    703:  *
1.1       mrg       704:  * => XXXCDC: need way to map in external amap?
                    705:  */
                    706:
1.10      mrg       707: int
1.138     enami     708: uvm_map(struct vm_map *map, vaddr_t *startp /* IN/OUT */, vsize_t size,
                    709:     struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags)
1.10      mrg       710: {
1.99      chs       711:        struct vm_map_entry *prev_entry, *new_entry;
1.127     thorpej   712:        const int amapwaitflag = (flags & UVM_FLAG_NOWAIT) ?
1.126     bouyer    713:            AMAP_EXTEND_NOWAIT : 0;
1.10      mrg       714:        vm_prot_t prot = UVM_PROTECTION(flags), maxprot =
                    715:            UVM_MAXPROTECTION(flags);
                    716:        vm_inherit_t inherit = UVM_INHERIT(flags);
                    717:        int advice = UVM_ADVICE(flags);
1.121     atatat    718:        int error, merged = 0, kmap = (vm_map_pmap(map) == pmap_kernel());
1.10      mrg       719:        UVMHIST_FUNC("uvm_map");
                    720:        UVMHIST_CALLED(maphist);
1.1       mrg       721:
1.10      mrg       722:        UVMHIST_LOG(maphist, "(map=0x%x, *startp=0x%x, size=%d, flags=0x%x)",
                    723:            map, *startp, size, flags);
                    724:        UVMHIST_LOG(maphist, "  uobj/offset 0x%x/%d", uobj, uoffset,0,0);
1.107     chs       725:
                    726:        /*
                    727:         * detect a popular device driver bug.
                    728:         */
                    729:
1.139     enami     730:        KASSERT(doing_shutdown || curlwp != NULL ||
1.129     christos  731:            (map->flags & VM_MAP_INTRSAFE));
1.1       mrg       732:
1.10      mrg       733:        /*
1.144     yamt      734:         * zero-sized mapping doesn't make any sense.
                    735:         */
                    736:        KASSERT(size > 0);
                    737:
                    738:        uvm_tree_sanity(map, "map entry");
                    739:
                    740:        /*
1.106     chs       741:         * check sanity of protection code
1.10      mrg       742:         */
1.1       mrg       743:
1.10      mrg       744:        if ((prot & maxprot) != prot) {
1.98      chs       745:                UVMHIST_LOG(maphist, "<- prot. failure:  prot=0x%x, max=0x%x",
1.10      mrg       746:                prot, maxprot,0,0);
1.94      chs       747:                return EACCES;
1.10      mrg       748:        }
1.1       mrg       749:
1.10      mrg       750:        /*
1.106     chs       751:         * for pager_map, allocate the new entry first to avoid sleeping
                    752:         * for memory while we have the map locked.
                    753:         */
                    754:
                    755:        new_entry = NULL;
                    756:        if (map == pager_map) {
1.127     thorpej   757:                new_entry = uvm_mapent_alloc(map, (flags & UVM_FLAG_NOWAIT));
1.142     enami     758:                if (__predict_false(new_entry == NULL))
1.126     bouyer    759:                        return ENOMEM;
1.106     chs       760:        }
                    761:
                    762:        /*
                    763:         * figure out where to put new VM range
1.10      mrg       764:         */
1.1       mrg       765:
1.10      mrg       766:        if (vm_map_lock_try(map) == FALSE) {
1.106     chs       767:                if (flags & UVM_FLAG_TRYLOCK) {
                    768:                        if (new_entry) {
                    769:                                uvm_mapent_free(new_entry);
                    770:                        }
1.94      chs       771:                        return EAGAIN;
1.106     chs       772:                }
1.10      mrg       773:                vm_map_lock(map); /* could sleep here */
                    774:        }
1.98      chs       775:        if ((prev_entry = uvm_map_findspace(map, *startp, size, startp,
1.81      thorpej   776:            uobj, uoffset, align, flags)) == NULL) {
1.10      mrg       777:                UVMHIST_LOG(maphist,"<- uvm_map_findspace failed!",0,0,0,0);
                    778:                vm_map_unlock(map);
1.106     chs       779:                if (new_entry) {
                    780:                        uvm_mapent_free(new_entry);
                    781:                }
1.94      chs       782:                return ENOMEM;
1.10      mrg       783:        }
1.1       mrg       784:
1.40      thorpej   785: #ifdef PMAP_GROWKERNEL
1.152   ! simonb    786:        /*
        !           787:         * If the kernel pmap can't map the requested space,
        !           788:         * then allocate more resources for it.
        !           789:         */
        !           790:        if (map == kernel_map && uvm_maxkaddr < (*startp + size))
        !           791:                uvm_maxkaddr = pmap_growkernel(*startp + size);
1.10      mrg       792: #endif
                    793:
                    794:        UVMCNT_INCR(uvm_map_call);
                    795:
                    796:        /*
                    797:         * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER
1.98      chs       798:         * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET.   in
                    799:         * either case we want to zero it  before storing it in the map entry
1.10      mrg       800:         * (because it looks strange and confusing when debugging...)
1.98      chs       801:         *
                    802:         * if uobj is not null
1.10      mrg       803:         *   if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping
                    804:         *      and we do not need to change uoffset.
                    805:         *   if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset
                    806:         *      now (based on the starting address of the map).   this case is
                    807:         *      for kernel object mappings where we don't know the offset until
                    808:         *      the virtual address is found (with uvm_map_findspace).   the
                    809:         *      offset is the distance we are from the start of the map.
                    810:         */
                    811:
                    812:        if (uobj == NULL) {
                    813:                uoffset = 0;
                    814:        } else {
                    815:                if (uoffset == UVM_UNKNOWN_OFFSET) {
1.85      chs       816:                        KASSERT(UVM_OBJ_IS_KERN_OBJECT(uobj));
1.10      mrg       817:                        uoffset = *startp - vm_map_min(kernel_map);
                    818:                }
                    819:        }
                    820:
                    821:        /*
1.106     chs       822:         * try and insert in map by extending previous entry, if possible.
1.10      mrg       823:         * XXX: we don't try and pull back the next entry.   might be useful
                    824:         * for a stack, but we are currently allocating our stack in advance.
                    825:         */
                    826:
1.121     atatat    827:        if (flags & UVM_FLAG_NOMERGE)
                    828:                goto nomerge;
                    829:
                    830:        if (prev_entry->end == *startp &&
                    831:            prev_entry != &map->header &&
1.10      mrg       832:            prev_entry->object.uvm_obj == uobj) {
                    833:
                    834:                if (uobj && prev_entry->offset +
                    835:                    (prev_entry->end - prev_entry->start) != uoffset)
1.121     atatat    836:                        goto forwardmerge;
1.10      mrg       837:
1.29      chuck     838:                if (UVM_ET_ISSUBMAP(prev_entry))
1.121     atatat    839:                        goto forwardmerge;
1.10      mrg       840:
1.98      chs       841:                if (prev_entry->protection != prot ||
1.10      mrg       842:                    prev_entry->max_protection != maxprot)
1.121     atatat    843:                        goto forwardmerge;
1.10      mrg       844:
                    845:                if (prev_entry->inheritance != inherit ||
                    846:                    prev_entry->advice != advice)
1.121     atatat    847:                        goto forwardmerge;
1.10      mrg       848:
1.56      thorpej   849:                /* wiring status must match (new area is unwired) */
1.55      thorpej   850:                if (VM_MAPENT_ISWIRED(prev_entry))
1.121     atatat    851:                        goto forwardmerge;
1.10      mrg       852:
                    853:                /*
1.98      chs       854:                 * can't extend a shared amap.  note: no need to lock amap to
1.34      chuck     855:                 * look at refs since we don't care about its exact value.
1.10      mrg       856:                 * if it is one (i.e. we have only reference) it will stay there
                    857:                 */
1.85      chs       858:
1.10      mrg       859:                if (prev_entry->aref.ar_amap &&
1.34      chuck     860:                    amap_refs(prev_entry->aref.ar_amap) != 1) {
1.121     atatat    861:                        goto forwardmerge;
1.10      mrg       862:                }
1.85      chs       863:
1.119     chs       864:                if (prev_entry->aref.ar_amap) {
1.139     enami     865:                        error = amap_extend(prev_entry, size,
1.126     bouyer    866:                            amapwaitflag | AMAP_EXTEND_FORWARDS);
1.119     chs       867:                        if (error) {
                    868:                                vm_map_unlock(map);
                    869:                                if (new_entry) {
                    870:                                        uvm_mapent_free(new_entry);
                    871:                                }
                    872:                                return error;
                    873:                        }
                    874:                }
1.10      mrg       875:
1.121     atatat    876:                if (kmap)
                    877:                        UVMCNT_INCR(map_kbackmerge);
                    878:                else
                    879:                        UVMCNT_INCR(map_ubackmerge);
1.10      mrg       880:                UVMHIST_LOG(maphist,"  starting back merge", 0, 0, 0, 0);
                    881:
                    882:                /*
                    883:                 * drop our reference to uobj since we are extending a reference
                    884:                 * that we already have (the ref count can not drop to zero).
                    885:                 */
1.119     chs       886:
1.10      mrg       887:                if (uobj && uobj->pgops->pgo_detach)
                    888:                        uobj->pgops->pgo_detach(uobj);
                    889:
                    890:                prev_entry->end += size;
1.145     yamt      891:                uvm_rb_fixup(map, prev_entry);
                    892:
                    893:                uvm_tree_sanity(map, "map backmerged");
1.10      mrg       894:
                    895:                UVMHIST_LOG(maphist,"<- done (via backmerge)!", 0, 0, 0, 0);
1.106     chs       896:                if (new_entry) {
                    897:                        uvm_mapent_free(new_entry);
1.121     atatat    898:                        new_entry = NULL;
1.106     chs       899:                }
1.121     atatat    900:                merged++;
1.106     chs       901:        }
1.10      mrg       902:
1.121     atatat    903: forwardmerge:
                    904:        if (prev_entry->next->start == (*startp + size) &&
                    905:            prev_entry->next != &map->header &&
                    906:            prev_entry->next->object.uvm_obj == uobj) {
                    907:
                    908:                if (uobj && prev_entry->next->offset != uoffset + size)
                    909:                        goto nomerge;
                    910:
                    911:                if (UVM_ET_ISSUBMAP(prev_entry->next))
                    912:                        goto nomerge;
                    913:
                    914:                if (prev_entry->next->protection != prot ||
                    915:                    prev_entry->next->max_protection != maxprot)
                    916:                        goto nomerge;
                    917:
                    918:                if (prev_entry->next->inheritance != inherit ||
                    919:                    prev_entry->next->advice != advice)
                    920:                        goto nomerge;
                    921:
                    922:                /* wiring status must match (new area is unwired) */
                    923:                if (VM_MAPENT_ISWIRED(prev_entry->next))
                    924:                        goto nomerge;
                    925:
                    926:                /*
                    927:                 * can't extend a shared amap.  note: no need to lock amap to
                    928:                 * look at refs since we don't care about its exact value.
1.122     atatat    929:                 * if it is one (i.e. we have only reference) it will stay there.
                    930:                 *
                    931:                 * note that we also can't merge two amaps, so if we
                    932:                 * merged with the previous entry which has an amap,
                    933:                 * and the next entry also has an amap, we give up.
                    934:                 *
1.125     atatat    935:                 * Interesting cases:
                    936:                 * amap, new, amap -> give up second merge (single fwd extend)
                    937:                 * amap, new, none -> double forward extend (extend again here)
                    938:                 * none, new, amap -> double backward extend (done here)
                    939:                 * uobj, new, amap -> single backward extend (done here)
                    940:                 *
1.122     atatat    941:                 * XXX should we attempt to deal with someone refilling
                    942:                 * the deallocated region between two entries that are
                    943:                 * backed by the same amap (ie, arefs is 2, "prev" and
                    944:                 * "next" refer to it, and adding this allocation will
                    945:                 * close the hole, thus restoring arefs to 1 and
                    946:                 * deallocating the "next" vm_map_entry)?  -- @@@
1.121     atatat    947:                 */
                    948:
                    949:                if (prev_entry->next->aref.ar_amap &&
1.122     atatat    950:                    (amap_refs(prev_entry->next->aref.ar_amap) != 1 ||
                    951:                     (merged && prev_entry->aref.ar_amap))) {
1.121     atatat    952:                        goto nomerge;
                    953:                }
                    954:
1.122     atatat    955:                if (merged) {
1.123     atatat    956:                        /*
                    957:                         * Try to extend the amap of the previous entry to
                    958:                         * cover the next entry as well.  If it doesn't work
                    959:                         * just skip on, don't actually give up, since we've
                    960:                         * already completed the back merge.
                    961:                         */
1.125     atatat    962:                        if (prev_entry->aref.ar_amap) {
                    963:                                if (amap_extend(prev_entry,
                    964:                                    prev_entry->next->end -
                    965:                                    prev_entry->next->start,
1.126     bouyer    966:                                    amapwaitflag | AMAP_EXTEND_FORWARDS))
1.142     enami     967:                                        goto nomerge;
1.125     atatat    968:                        }
                    969:
                    970:                        /*
                    971:                         * Try to extend the amap of the *next* entry
                    972:                         * back to cover the new allocation *and* the
                    973:                         * previous entry as well (the previous merge
                    974:                         * didn't have an amap already otherwise we
                    975:                         * wouldn't be checking here for an amap).  If
                    976:                         * it doesn't work just skip on, again, don't
                    977:                         * actually give up, since we've already
                    978:                         * completed the back merge.
                    979:                         */
                    980:                        else if (prev_entry->next->aref.ar_amap) {
                    981:                                if (amap_extend(prev_entry->next,
                    982:                                    prev_entry->end -
1.141     atatat    983:                                    prev_entry->start,
1.126     bouyer    984:                                    amapwaitflag | AMAP_EXTEND_BACKWARDS))
1.142     enami     985:                                        goto nomerge;
1.125     atatat    986:                        }
                    987:                } else {
                    988:                        /*
                    989:                         * Pull the next entry's amap backwards to cover this
                    990:                         * new allocation.
                    991:                         */
                    992:                        if (prev_entry->next->aref.ar_amap) {
                    993:                                error = amap_extend(prev_entry->next, size,
1.126     bouyer    994:                                    amapwaitflag | AMAP_EXTEND_BACKWARDS);
1.125     atatat    995:                                if (error) {
                    996:                                        vm_map_unlock(map);
                    997:                                        if (new_entry) {
                    998:                                                uvm_mapent_free(new_entry);
                    999:                                        }
                   1000:                                        return error;
                   1001:                                }
                   1002:                        }
1.122     atatat   1003:                }
                   1004:
1.121     atatat   1005:                if (merged) {
                   1006:                        if (kmap) {
                   1007:                                UVMCNT_DECR(map_kbackmerge);
                   1008:                                UVMCNT_INCR(map_kbimerge);
                   1009:                        } else {
1.122     atatat   1010:                                UVMCNT_DECR(map_ubackmerge);
                   1011:                                UVMCNT_INCR(map_ubimerge);
1.121     atatat   1012:                        }
1.122     atatat   1013:                } else {
1.121     atatat   1014:                        if (kmap)
                   1015:                                UVMCNT_INCR(map_kforwmerge);
                   1016:                        else
                   1017:                                UVMCNT_INCR(map_uforwmerge);
                   1018:                }
                   1019:                UVMHIST_LOG(maphist,"  starting forward merge", 0, 0, 0, 0);
1.10      mrg      1020:
1.121     atatat   1021:                /*
                   1022:                 * drop our reference to uobj since we are extending a reference
                   1023:                 * that we already have (the ref count can not drop to zero).
                   1024:                 * (if merged, we've already detached)
                   1025:                 */
                   1026:                if (uobj && uobj->pgops->pgo_detach && !merged)
                   1027:                        uobj->pgops->pgo_detach(uobj);
1.1       mrg      1028:
1.121     atatat   1029:                if (merged) {
                   1030:                        struct vm_map_entry *dead = prev_entry->next;
                   1031:                        prev_entry->end = dead->end;
                   1032:                        uvm_map_entry_unlink(map, dead);
1.125     atatat   1033:                        if (dead->aref.ar_amap != NULL) {
                   1034:                                prev_entry->aref = dead->aref;
                   1035:                                dead->aref.ar_amap = NULL;
                   1036:                        }
1.121     atatat   1037:                        uvm_mapent_free(dead);
                   1038:                } else {
                   1039:                        prev_entry->next->start -= size;
1.145     yamt     1040:                        if (prev_entry != &map->header)
                   1041:                                uvm_rb_fixup(map, prev_entry);
1.121     atatat   1042:                        if (uobj)
                   1043:                                prev_entry->next->offset = uoffset;
                   1044:                }
1.145     yamt     1045:
                   1046:                uvm_tree_sanity(map, "map forwardmerged");
1.1       mrg      1047:
1.121     atatat   1048:                UVMHIST_LOG(maphist,"<- done forwardmerge", 0, 0, 0, 0);
                   1049:                if (new_entry) {
                   1050:                        uvm_mapent_free(new_entry);
                   1051:                        new_entry = NULL;
                   1052:                }
                   1053:                merged++;
1.106     chs      1054:        }
1.121     atatat   1055:
                   1056: nomerge:
                   1057:        if (!merged) {
                   1058:                UVMHIST_LOG(maphist,"  allocating new map entry", 0, 0, 0, 0);
                   1059:                if (kmap)
                   1060:                        UVMCNT_INCR(map_knomerge);
                   1061:                else
                   1062:                        UVMCNT_INCR(map_unomerge);
1.106     chs      1063:
1.10      mrg      1064:                /*
1.121     atatat   1065:                 * allocate new entry and link it in.
1.10      mrg      1066:                 */
1.106     chs      1067:
1.121     atatat   1068:                if (new_entry == NULL) {
1.126     bouyer   1069:                        new_entry = uvm_mapent_alloc(map,
1.127     thorpej  1070:                                (flags & UVM_FLAG_NOWAIT));
1.126     bouyer   1071:                        if (__predict_false(new_entry == NULL)) {
                   1072:                                vm_map_unlock(map);
                   1073:                                return ENOMEM;
                   1074:                        }
1.121     atatat   1075:                }
                   1076:                new_entry->start = *startp;
                   1077:                new_entry->end = new_entry->start + size;
                   1078:                new_entry->object.uvm_obj = uobj;
                   1079:                new_entry->offset = uoffset;
                   1080:
                   1081:                if (uobj)
                   1082:                        new_entry->etype = UVM_ET_OBJ;
                   1083:                else
                   1084:                        new_entry->etype = 0;
                   1085:
                   1086:                if (flags & UVM_FLAG_COPYONW) {
                   1087:                        new_entry->etype |= UVM_ET_COPYONWRITE;
                   1088:                        if ((flags & UVM_FLAG_OVERLAY) == 0)
                   1089:                                new_entry->etype |= UVM_ET_NEEDSCOPY;
                   1090:                }
                   1091:
                   1092:                new_entry->protection = prot;
                   1093:                new_entry->max_protection = maxprot;
                   1094:                new_entry->inheritance = inherit;
                   1095:                new_entry->wired_count = 0;
                   1096:                new_entry->advice = advice;
                   1097:                if (flags & UVM_FLAG_OVERLAY) {
                   1098:
                   1099:                        /*
                   1100:                         * to_add: for BSS we overallocate a little since we
                   1101:                         * are likely to extend
                   1102:                         */
                   1103:
                   1104:                        vaddr_t to_add = (flags & UVM_FLAG_AMAPPAD) ?
                   1105:                                UVM_AMAP_CHUNK << PAGE_SHIFT : 0;
1.126     bouyer   1106:                        struct vm_amap *amap = amap_alloc(size, to_add,
1.127     thorpej  1107:                            (flags & UVM_FLAG_NOWAIT) ? M_NOWAIT : M_WAITOK);
1.126     bouyer   1108:                        if (__predict_false(amap == NULL)) {
                   1109:                                vm_map_unlock(map);
                   1110:                                uvm_mapent_free(new_entry);
                   1111:                                return ENOMEM;
                   1112:                        }
1.121     atatat   1113:                        new_entry->aref.ar_pageoff = 0;
                   1114:                        new_entry->aref.ar_amap = amap;
                   1115:                } else {
                   1116:                        new_entry->aref.ar_pageoff = 0;
                   1117:                        new_entry->aref.ar_amap = NULL;
                   1118:                }
                   1119:                uvm_map_entry_link(map, prev_entry, new_entry);
1.1       mrg      1120:
1.121     atatat   1121:                /*
                   1122:                 * Update the free space hint
                   1123:                 */
1.10      mrg      1124:
1.121     atatat   1125:                if ((map->first_free == prev_entry) &&
                   1126:                    (prev_entry->end >= new_entry->start))
                   1127:                        map->first_free = new_entry;
                   1128:        }
1.10      mrg      1129:
1.146     yamt     1130:        map->size += size;
                   1131:
1.10      mrg      1132:        UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
1.1       mrg      1133:        vm_map_unlock(map);
1.94      chs      1134:        return 0;
1.1       mrg      1135: }
                   1136:
                   1137: /*
                   1138:  * uvm_map_lookup_entry: find map entry at or before an address
                   1139:  *
                   1140:  * => map must at least be read-locked by caller
                   1141:  * => entry is returned in "entry"
                   1142:  * => return value is true if address is in the returned entry
                   1143:  */
                   1144:
1.10      mrg      1145: boolean_t
1.138     enami    1146: uvm_map_lookup_entry(struct vm_map *map, vaddr_t address,
                   1147:     struct vm_map_entry **entry        /* OUT */)
1.1       mrg      1148: {
1.99      chs      1149:        struct vm_map_entry *cur;
1.144     yamt     1150:        boolean_t use_tree = FALSE;
1.1       mrg      1151:        UVMHIST_FUNC("uvm_map_lookup_entry");
                   1152:        UVMHIST_CALLED(maphist);
                   1153:
                   1154:        UVMHIST_LOG(maphist,"(map=0x%x,addr=0x%x,ent=0x%x)",
1.10      mrg      1155:            map, address, entry, 0);
1.1       mrg      1156:
                   1157:        /*
1.10      mrg      1158:         * start looking either from the head of the
                   1159:         * list, or from the hint.
1.1       mrg      1160:         */
                   1161:
                   1162:        simple_lock(&map->hint_lock);
                   1163:        cur = map->hint;
                   1164:        simple_unlock(&map->hint_lock);
                   1165:
                   1166:        if (cur == &map->header)
                   1167:                cur = cur->next;
                   1168:
                   1169:        UVMCNT_INCR(uvm_mlk_call);
                   1170:        if (address >= cur->start) {
1.99      chs      1171:
1.139     enami    1172:                /*
1.10      mrg      1173:                 * go from hint to end of list.
1.1       mrg      1174:                 *
1.10      mrg      1175:                 * but first, make a quick check to see if
                   1176:                 * we are already looking at the entry we
                   1177:                 * want (which is usually the case).
                   1178:                 * note also that we don't need to save the hint
                   1179:                 * here... it is the same hint (unless we are
                   1180:                 * at the header, in which case the hint didn't
                   1181:                 * buy us anything anyway).
1.1       mrg      1182:                 */
1.99      chs      1183:
1.144     yamt     1184:                if (cur != &map->header && cur->end > address) {
1.1       mrg      1185:                        UVMCNT_INCR(uvm_mlk_hint);
                   1186:                        *entry = cur;
                   1187:                        UVMHIST_LOG(maphist,"<- got it via hint (0x%x)",
1.10      mrg      1188:                            cur, 0, 0, 0);
                   1189:                        return (TRUE);
1.1       mrg      1190:                }
1.144     yamt     1191:
                   1192:                if (map->nentries > 30)
                   1193:                        use_tree = TRUE;
1.10      mrg      1194:        } else {
1.99      chs      1195:
1.139     enami    1196:                /*
1.144     yamt     1197:                 * invalid hint.  use tree.
1.1       mrg      1198:                 */
1.144     yamt     1199:                use_tree = TRUE;
                   1200:        }
                   1201:
                   1202:        uvm_tree_sanity(map, __func__);
                   1203:
                   1204:        if (use_tree) {
                   1205:                struct vm_map_entry *prev = &map->header;
                   1206:                cur = RB_ROOT(&map->rbhead);
1.99      chs      1207:
1.144     yamt     1208:                /*
                   1209:                 * Simple lookup in the tree.  Happens when the hint is
                   1210:                 * invalid, or nentries reach a threshold.
                   1211:                 */
                   1212:                while (cur) {
                   1213:                        if (address >= cur->start) {
                   1214:                                if (address < cur->end) {
                   1215:                                        *entry = cur;
                   1216:                                        goto got;
                   1217:                                }
                   1218:                                prev = cur;
                   1219:                                cur = RB_RIGHT(cur, rb_entry);
                   1220:                        } else
                   1221:                                cur = RB_LEFT(cur, rb_entry);
                   1222:                }
                   1223:                *entry = prev;
                   1224:                goto failed;
1.1       mrg      1225:        }
                   1226:
                   1227:        /*
1.10      mrg      1228:         * search linearly
1.1       mrg      1229:         */
                   1230:
1.144     yamt     1231:        while (cur != &map->header) {
1.1       mrg      1232:                if (cur->end > address) {
                   1233:                        if (address >= cur->start) {
1.139     enami    1234:                                /*
1.10      mrg      1235:                                 * save this lookup for future
                   1236:                                 * hints, and return
1.1       mrg      1237:                                 */
                   1238:
                   1239:                                *entry = cur;
1.144     yamt     1240: got:
                   1241:                                SAVE_HINT(map, map->hint, *entry);
1.1       mrg      1242:                                UVMHIST_LOG(maphist,"<- search got it (0x%x)",
1.10      mrg      1243:                                        cur, 0, 0, 0);
1.144     yamt     1244:                                KDASSERT((*entry)->start <= address);
                   1245:                                KDASSERT(address < (*entry)->end);
1.10      mrg      1246:                                return (TRUE);
1.1       mrg      1247:                        }
                   1248:                        break;
                   1249:                }
                   1250:                cur = cur->next;
                   1251:        }
                   1252:        *entry = cur->prev;
1.144     yamt     1253: failed:
1.82      thorpej  1254:        SAVE_HINT(map, map->hint, *entry);
1.1       mrg      1255:        UVMHIST_LOG(maphist,"<- failed!",0,0,0,0);
1.147     yamt     1256:        KDASSERT((*entry) == &map->header || (*entry)->end <= address);
1.144     yamt     1257:        KDASSERT((*entry)->next == &map->header ||
                   1258:            address < (*entry)->next->start);
1.10      mrg      1259:        return (FALSE);
1.1       mrg      1260: }
                   1261:
                   1262: /*
1.140     enami    1263:  * See if the range between start and start + length fits in the gap
                   1264:  * entry->next->start and entry->end.  Returns 1 if fits, 0 if doesn't
                   1265:  * fit, and -1 address wraps around.
                   1266:  */
                   1267: static __inline int
                   1268: uvm_map_space_avail(vaddr_t *start, vsize_t length, voff_t uoffset,
                   1269:     vsize_t align, int topdown, struct vm_map_entry *entry)
                   1270: {
                   1271:        vaddr_t end;
                   1272:
                   1273: #ifdef PMAP_PREFER
                   1274:        /*
                   1275:         * push start address forward as needed to avoid VAC alias problems.
                   1276:         * we only do this if a valid offset is specified.
                   1277:         */
                   1278:
                   1279:        if (uoffset != UVM_UNKNOWN_OFFSET)
                   1280:                PMAP_PREFER(uoffset, start);
                   1281: #endif
                   1282:        if (align != 0) {
                   1283:                if ((*start & (align - 1)) != 0) {
                   1284:                        if (topdown)
                   1285:                                *start &= ~(align - 1);
                   1286:                        else
                   1287:                                *start = roundup(*start, align);
                   1288:                }
                   1289:                /*
                   1290:                 * XXX Should we PMAP_PREFER() here again?
                   1291:                 */
                   1292:        }
                   1293:
                   1294:        /*
                   1295:         * Find the end of the proposed new region.  Be sure we didn't
                   1296:         * wrap around the address; if so, we lose.  Otherwise, if the
                   1297:         * proposed new region fits before the next entry, we win.
                   1298:         */
                   1299:
                   1300:        end = *start + length;
                   1301:        if (end < *start)
                   1302:                return (-1);
                   1303:
                   1304:        if (entry->next->start >= end && *start >= entry->end)
                   1305:                return (1);
                   1306:
                   1307:        return (0);
                   1308: }
                   1309:
                   1310: /*
1.1       mrg      1311:  * uvm_map_findspace: find "length" sized space in "map".
                   1312:  *
1.81      thorpej  1313:  * => "hint" is a hint about where we want it, unless FINDSPACE_FIXED is
                   1314:  *     set (in which case we insist on using "hint").
1.1       mrg      1315:  * => "result" is VA returned
                   1316:  * => uobj/uoffset are to be used to handle VAC alignment, if required
1.81      thorpej  1317:  * => if `align' is non-zero, we attempt to align to that value.
1.1       mrg      1318:  * => caller must at least have read-locked map
                   1319:  * => returns NULL on failure, or pointer to prev. map entry if success
                   1320:  * => note this is a cross between the old vm_map_findspace and vm_map_find
                   1321:  */
                   1322:
1.99      chs      1323: struct vm_map_entry *
1.138     enami    1324: uvm_map_findspace(struct vm_map *map, vaddr_t hint, vsize_t length,
                   1325:     vaddr_t *result /* OUT */, struct uvm_object *uobj, voff_t uoffset,
                   1326:     vsize_t align, int flags)
1.1       mrg      1327: {
1.140     enami    1328:        struct vm_map_entry *entry;
1.144     yamt     1329:        struct vm_map_entry *child, *prev, *tmp;
1.140     enami    1330:        vaddr_t orig_hint;
1.131     atatat   1331:        const int topdown = map->flags & VM_MAP_TOPDOWN;
1.1       mrg      1332:        UVMHIST_FUNC("uvm_map_findspace");
                   1333:        UVMHIST_CALLED(maphist);
                   1334:
1.98      chs      1335:        UVMHIST_LOG(maphist, "(map=0x%x, hint=0x%x, len=%d, flags=0x%x)",
1.140     enami    1336:            map, hint, length, flags);
1.85      chs      1337:        KASSERT((align & (align - 1)) == 0);
                   1338:        KASSERT((flags & UVM_FLAG_FIXED) == 0 || align == 0);
1.81      thorpej  1339:
1.144     yamt     1340:        uvm_tree_sanity(map, "map_findspace entry");
                   1341:
1.81      thorpej  1342:        /*
                   1343:         * remember the original hint.  if we are aligning, then we
                   1344:         * may have to try again with no alignment constraint if
                   1345:         * we fail the first time.
                   1346:         */
1.85      chs      1347:
1.81      thorpej  1348:        orig_hint = hint;
1.1       mrg      1349:        if (hint < map->min_offset) {   /* check ranges ... */
1.81      thorpej  1350:                if (flags & UVM_FLAG_FIXED) {
1.1       mrg      1351:                        UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0);
1.139     enami    1352:                        return (NULL);
1.1       mrg      1353:                }
                   1354:                hint = map->min_offset;
                   1355:        }
                   1356:        if (hint > map->max_offset) {
                   1357:                UVMHIST_LOG(maphist,"<- VA 0x%x > range [0x%x->0x%x]",
1.140     enami    1358:                    hint, map->min_offset, map->max_offset, 0);
1.139     enami    1359:                return (NULL);
1.1       mrg      1360:        }
                   1361:
                   1362:        /*
                   1363:         * Look for the first possible address; if there's already
                   1364:         * something at this address, we have to start after it.
                   1365:         */
                   1366:
1.131     atatat   1367:        /*
                   1368:         * @@@: there are four, no, eight cases to consider.
                   1369:         *
                   1370:         * 0: found,     fixed,     bottom up -> fail
                   1371:         * 1: found,     fixed,     top down  -> fail
1.140     enami    1372:         * 2: found,     not fixed, bottom up -> start after entry->end,
                   1373:         *                                       loop up
                   1374:         * 3: found,     not fixed, top down  -> start before entry->start,
                   1375:         *                                       loop down
                   1376:         * 4: not found, fixed,     bottom up -> check entry->next->start, fail
                   1377:         * 5: not found, fixed,     top down  -> check entry->next->start, fail
                   1378:         * 6: not found, not fixed, bottom up -> check entry->next->start,
                   1379:         *                                       loop up
                   1380:         * 7: not found, not fixed, top down  -> check entry->next->start,
                   1381:         *                                       loop down
1.131     atatat   1382:         *
                   1383:         * as you can see, it reduces to roughly five cases, and that
                   1384:         * adding top down mapping only adds one unique case (without
                   1385:         * it, there would be four cases).
                   1386:         */
                   1387:
1.81      thorpej  1388:        if ((flags & UVM_FLAG_FIXED) == 0 && hint == map->min_offset) {
1.140     enami    1389:                entry = map->first_free;
1.1       mrg      1390:        } else {
1.140     enami    1391:                if (uvm_map_lookup_entry(map, hint, &entry)) {
1.1       mrg      1392:                        /* "hint" address already in use ... */
1.81      thorpej  1393:                        if (flags & UVM_FLAG_FIXED) {
1.140     enami    1394:                                UVMHIST_LOG(maphist, "<- fixed & VA in use",
1.10      mrg      1395:                                    0, 0, 0, 0);
1.139     enami    1396:                                return (NULL);
1.1       mrg      1397:                        }
1.140     enami    1398:                        if (topdown)
                   1399:                                /* Start from lower gap. */
                   1400:                                entry = entry->prev;
                   1401:                } else if (flags & UVM_FLAG_FIXED) {
                   1402:                        if (entry->next->start >= hint + length &&
                   1403:                            hint + length > hint)
                   1404:                                goto found;
                   1405:
                   1406:                        /* "hint" address is gap but too small */
                   1407:                        UVMHIST_LOG(maphist, "<- fixed mapping failed",
                   1408:                            0, 0, 0, 0);
                   1409:                        return (NULL); /* only one shot at it ... */
                   1410:                } else {
                   1411:                        /*
                   1412:                         * See if given hint fits in this gap.
                   1413:                         */
                   1414:                        switch (uvm_map_space_avail(&hint, length,
                   1415:                            uoffset, align, topdown, entry)) {
                   1416:                        case 1:
                   1417:                                goto found;
                   1418:                        case -1:
                   1419:                                goto wraparound;
                   1420:                        }
                   1421:
1.148     yamt     1422:                        if (topdown) {
1.140     enami    1423:                                /*
                   1424:                                 * Still there is a chance to fit
                   1425:                                 * if hint > entry->end.
                   1426:                                 */
1.148     yamt     1427:                        } else {
                   1428:                                /* Start from higer gap. */
                   1429:                                entry = entry->next;
                   1430:                                if (entry == &map->header)
                   1431:                                        goto notfound;
1.140     enami    1432:                                goto nextgap;
1.148     yamt     1433:                        }
1.1       mrg      1434:                }
                   1435:        }
                   1436:
                   1437:        /*
1.144     yamt     1438:         * Note that all UVM_FLAGS_FIXED case is already handled.
                   1439:         */
                   1440:        KDASSERT((flags & UVM_FLAG_FIXED) == 0);
                   1441:
                   1442:        /* Try to find the space in the red-black tree */
                   1443:
                   1444:        /* Check slot before any entry */
                   1445:        hint = topdown ? entry->next->start - length : entry->end;
                   1446:        switch (uvm_map_space_avail(&hint, length, uoffset, align,
                   1447:            topdown, entry)) {
                   1448:        case 1:
                   1449:                goto found;
                   1450:        case -1:
                   1451:                goto wraparound;
                   1452:        }
                   1453:
                   1454: nextgap:
1.148     yamt     1455:        KDASSERT((flags & UVM_FLAG_FIXED) == 0);
1.144     yamt     1456:        /* If there is not enough space in the whole tree, we fail */
                   1457:        tmp = RB_ROOT(&map->rbhead);
                   1458:        if (tmp == NULL || tmp->space < length)
                   1459:                goto notfound;
                   1460:
                   1461:        prev = NULL; /* previous candidate */
                   1462:
                   1463:        /* Find an entry close to hint that has enough space */
                   1464:        for (; tmp;) {
                   1465:                KASSERT(tmp->next->start == tmp->end + tmp->ownspace);
                   1466:                if (topdown) {
                   1467:                        if (tmp->next->start < hint + length &&
                   1468:                            (prev == NULL || tmp->end > prev->end)) {
                   1469:                                if (tmp->ownspace >= length)
                   1470:                                        prev = tmp;
                   1471:                                else if ((child = RB_LEFT(tmp, rb_entry))
                   1472:                                    != NULL && child->space >= length)
                   1473:                                        prev = tmp;
                   1474:                        }
                   1475:                } else {
                   1476:                        if (tmp->end >= hint &&
                   1477:                            (prev == NULL || tmp->end < prev->end)) {
                   1478:                                if (tmp->ownspace >= length)
                   1479:                                        prev = tmp;
                   1480:                                else if ((child = RB_RIGHT(tmp, rb_entry))
                   1481:                                    != NULL && child->space >= length)
                   1482:                                        prev = tmp;
                   1483:                        }
                   1484:                }
                   1485:                if (tmp->next->start < hint + length)
                   1486:                        child = RB_RIGHT(tmp, rb_entry);
                   1487:                else if (tmp->end > hint)
                   1488:                        child = RB_LEFT(tmp, rb_entry);
                   1489:                else {
                   1490:                        if (tmp->ownspace >= length)
                   1491:                                break;
                   1492:                        if (topdown)
                   1493:                                child = RB_LEFT(tmp, rb_entry);
                   1494:                        else
                   1495:                                child = RB_RIGHT(tmp, rb_entry);
                   1496:                }
                   1497:                if (child == NULL || child->space < length)
                   1498:                        break;
                   1499:                tmp = child;
                   1500:        }
                   1501:
1.148     yamt     1502:        if (tmp != NULL && tmp->start < hint && hint < tmp->next->start) {
1.144     yamt     1503:                /*
                   1504:                 * Check if the entry that we found satifies the
                   1505:                 * space requirement
                   1506:                 */
1.148     yamt     1507:                if (topdown) {
1.149     yamt     1508:                        if (hint > tmp->next->start - length)
                   1509:                                hint = tmp->next->start - length;
1.148     yamt     1510:                } else {
1.149     yamt     1511:                        if (hint < tmp->end)
                   1512:                                hint = tmp->end;
1.148     yamt     1513:                }
                   1514:                switch (uvm_map_space_avail(&hint, length, uoffset, align,
                   1515:                    topdown, tmp)) {
                   1516:                case 1:
1.144     yamt     1517:                        entry = tmp;
                   1518:                        goto found;
1.148     yamt     1519:                case -1:
                   1520:                        goto wraparound;
1.144     yamt     1521:                }
                   1522:                if (tmp->ownspace >= length)
                   1523:                        goto listsearch;
                   1524:        }
                   1525:        if (prev == NULL)
                   1526:                goto notfound;
                   1527:
1.148     yamt     1528:        if (topdown) {
1.150     yamt     1529:                KASSERT(orig_hint >= prev->next->start - length ||
1.148     yamt     1530:                    prev->next->start - length > prev->next->start);
                   1531:                hint = prev->next->start - length;
                   1532:        } else {
1.150     yamt     1533:                KASSERT(orig_hint <= prev->end);
1.148     yamt     1534:                hint = prev->end;
                   1535:        }
                   1536:        switch (uvm_map_space_avail(&hint, length, uoffset, align,
                   1537:            topdown, prev)) {
                   1538:        case 1:
1.144     yamt     1539:                entry = prev;
                   1540:                goto found;
1.148     yamt     1541:        case -1:
                   1542:                goto wraparound;
1.144     yamt     1543:        }
                   1544:        if (prev->ownspace >= length)
                   1545:                goto listsearch;
                   1546:
                   1547:        if (topdown)
                   1548:                tmp = RB_LEFT(prev, rb_entry);
                   1549:        else
                   1550:                tmp = RB_RIGHT(prev, rb_entry);
                   1551:        for (;;) {
                   1552:                KASSERT(tmp && tmp->space >= length);
                   1553:                if (topdown)
                   1554:                        child = RB_RIGHT(tmp, rb_entry);
                   1555:                else
                   1556:                        child = RB_LEFT(tmp, rb_entry);
                   1557:                if (child && child->space >= length) {
                   1558:                        tmp = child;
                   1559:                        continue;
                   1560:                }
                   1561:                if (tmp->ownspace >= length)
                   1562:                        break;
                   1563:                if (topdown)
                   1564:                        tmp = RB_LEFT(tmp, rb_entry);
                   1565:                else
                   1566:                        tmp = RB_RIGHT(tmp, rb_entry);
                   1567:        }
                   1568:
1.148     yamt     1569:        if (topdown) {
1.150     yamt     1570:                KASSERT(orig_hint >= tmp->next->start - length ||
1.148     yamt     1571:                    tmp->next->start - length > tmp->next->start);
                   1572:                hint = tmp->next->start - length;
                   1573:        } else {
1.150     yamt     1574:                KASSERT(orig_hint <= tmp->end);
1.148     yamt     1575:                hint = tmp->end;
                   1576:        }
1.144     yamt     1577:        switch (uvm_map_space_avail(&hint, length, uoffset, align,
                   1578:            topdown, tmp)) {
                   1579:        case 1:
                   1580:                entry = tmp;
                   1581:                goto found;
1.148     yamt     1582:        case -1:
                   1583:                goto wraparound;
1.144     yamt     1584:        }
                   1585:
                   1586:        /*
                   1587:         * The tree fails to find an entry because of offset or alignment
                   1588:         * restrictions.  Search the list instead.
                   1589:         */
                   1590:  listsearch:
                   1591:        /*
1.1       mrg      1592:         * Look through the rest of the map, trying to fit a new region in
                   1593:         * the gap between existing regions, or after the very last region.
1.140     enami    1594:         * note: entry->end = base VA of current gap,
                   1595:         *       entry->next->start = VA of end of current gap
1.1       mrg      1596:         */
1.99      chs      1597:
1.140     enami    1598:        for (;;) {
                   1599:                /* Update hint for current gap. */
                   1600:                hint = topdown ? entry->next->start - length : entry->end;
                   1601:
                   1602:                /* See if it fits. */
                   1603:                switch (uvm_map_space_avail(&hint, length, uoffset, align,
                   1604:                    topdown, entry)) {
                   1605:                case 1:
                   1606:                        goto found;
                   1607:                case -1:
                   1608:                        goto wraparound;
                   1609:                }
                   1610:
                   1611:                /* Advance to next/previous gap */
                   1612:                if (topdown) {
                   1613:                        if (entry == &map->header) {
                   1614:                                UVMHIST_LOG(maphist, "<- failed (off start)",
                   1615:                                    0,0,0,0);
                   1616:                                goto notfound;
1.134     matt     1617:                        }
1.140     enami    1618:                        entry = entry->prev;
                   1619:                } else {
                   1620:                        entry = entry->next;
                   1621:                        if (entry == &map->header) {
                   1622:                                UVMHIST_LOG(maphist, "<- failed (off end)",
1.81      thorpej  1623:                                    0,0,0,0);
1.140     enami    1624:                                goto notfound;
1.81      thorpej  1625:                        }
1.1       mrg      1626:                }
                   1627:        }
1.140     enami    1628:
                   1629:  found:
1.82      thorpej  1630:        SAVE_HINT(map, map->hint, entry);
1.1       mrg      1631:        *result = hint;
                   1632:        UVMHIST_LOG(maphist,"<- got it!  (result=0x%x)", hint, 0,0,0);
1.148     yamt     1633:        KASSERT( topdown || hint >= orig_hint);
                   1634:        KASSERT(!topdown || hint <= orig_hint);
1.144     yamt     1635:        KASSERT(entry->end <= hint);
                   1636:        KASSERT(hint + length <= entry->next->start);
1.1       mrg      1637:        return (entry);
1.140     enami    1638:
                   1639:  wraparound:
                   1640:        UVMHIST_LOG(maphist, "<- failed (wrap around)", 0,0,0,0);
                   1641:
                   1642:  notfound:
                   1643:        if (align != 0) {
                   1644:                UVMHIST_LOG(maphist, "calling recursively, no align",
                   1645:                    0,0,0,0);
                   1646:                return (uvm_map_findspace(map, orig_hint,
                   1647:                    length, result, uobj, uoffset, 0, flags));
                   1648:        }
                   1649:        return (NULL);
1.1       mrg      1650: }
                   1651:
                   1652: /*
                   1653:  *   U N M A P   -   m a i n   h e l p e r   f u n c t i o n s
                   1654:  */
                   1655:
                   1656: /*
                   1657:  * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
                   1658:  *
1.98      chs      1659:  * => caller must check alignment and size
1.1       mrg      1660:  * => map must be locked by caller
                   1661:  * => we return a list of map entries that we've remove from the map
                   1662:  *    in "entry_list"
                   1663:  */
                   1664:
1.94      chs      1665: void
1.138     enami    1666: uvm_unmap_remove(struct vm_map *map, vaddr_t start, vaddr_t end,
                   1667:     struct vm_map_entry **entry_list /* OUT */)
1.10      mrg      1668: {
1.99      chs      1669:        struct vm_map_entry *entry, *first_entry, *next;
1.24      eeh      1670:        vaddr_t len;
1.99      chs      1671:        UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist);
1.10      mrg      1672:
                   1673:        UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)",
                   1674:            map, start, end, 0);
                   1675:        VM_MAP_RANGE_CHECK(map, start, end);
                   1676:
1.144     yamt     1677:        uvm_tree_sanity(map, "unmap_remove entry");
                   1678:
1.10      mrg      1679:        /*
                   1680:         * find first entry
                   1681:         */
1.99      chs      1682:
1.10      mrg      1683:        if (uvm_map_lookup_entry(map, start, &first_entry) == TRUE) {
1.29      chuck    1684:                /* clip and go... */
1.10      mrg      1685:                entry = first_entry;
                   1686:                UVM_MAP_CLIP_START(map, entry, start);
                   1687:                /* critical!  prevents stale hint */
1.82      thorpej  1688:                SAVE_HINT(map, entry, entry->prev);
1.10      mrg      1689:        } else {
                   1690:                entry = first_entry->next;
                   1691:        }
                   1692:
                   1693:        /*
                   1694:         * Save the free space hint
                   1695:         */
                   1696:
                   1697:        if (map->first_free->start >= start)
                   1698:                map->first_free = entry->prev;
                   1699:
                   1700:        /*
                   1701:         * note: we now re-use first_entry for a different task.  we remove
                   1702:         * a number of map entries from the map and save them in a linked
                   1703:         * list headed by "first_entry".  once we remove them from the map
                   1704:         * the caller should unlock the map and drop the references to the
                   1705:         * backing objects [c.f. uvm_unmap_detach].  the object is to
1.100     wiz      1706:         * separate unmapping from reference dropping.  why?
1.10      mrg      1707:         *   [1] the map has to be locked for unmapping
                   1708:         *   [2] the map need not be locked for reference dropping
                   1709:         *   [3] dropping references may trigger pager I/O, and if we hit
                   1710:         *       a pager that does synchronous I/O we may have to wait for it.
                   1711:         *   [4] we would like all waiting for I/O to occur with maps unlocked
1.98      chs      1712:         *       so that we don't block other threads.
1.10      mrg      1713:         */
1.99      chs      1714:
1.10      mrg      1715:        first_entry = NULL;
1.106     chs      1716:        *entry_list = NULL;
1.10      mrg      1717:
                   1718:        /*
1.98      chs      1719:         * break up the area into map entry sized regions and unmap.  note
1.10      mrg      1720:         * that all mappings have to be removed before we can even consider
                   1721:         * dropping references to amaps or VM objects (otherwise we could end
                   1722:         * up with a mapping to a page on the free list which would be very bad)
                   1723:         */
                   1724:
                   1725:        while ((entry != &map->header) && (entry->start < end)) {
1.98      chs      1726:                UVM_MAP_CLIP_END(map, entry, end);
1.10      mrg      1727:                next = entry->next;
                   1728:                len = entry->end - entry->start;
1.81      thorpej  1729:
1.10      mrg      1730:                /*
                   1731:                 * unwire before removing addresses from the pmap; otherwise
                   1732:                 * unwiring will put the entries back into the pmap (XXX).
                   1733:                 */
1.1       mrg      1734:
1.106     chs      1735:                if (VM_MAPENT_ISWIRED(entry)) {
1.10      mrg      1736:                        uvm_map_entry_unwire(map, entry);
1.106     chs      1737:                }
                   1738:                if ((map->flags & VM_MAP_PAGEABLE) == 0) {
1.10      mrg      1739:
1.106     chs      1740:                        /*
                   1741:                         * if the map is non-pageable, any pages mapped there
                   1742:                         * must be wired and entered with pmap_kenter_pa(),
                   1743:                         * and we should free any such pages immediately.
                   1744:                         * this is mostly used for kmem_map and mb_map.
                   1745:                         */
1.99      chs      1746:
1.106     chs      1747:                        uvm_km_pgremove_intrsafe(entry->start, entry->end);
                   1748:                        pmap_kremove(entry->start, len);
                   1749:                } else if (UVM_ET_ISOBJ(entry) &&
                   1750:                           UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) {
1.85      chs      1751:                        KASSERT(vm_map_pmap(map) == pmap_kernel());
1.1       mrg      1752:
1.10      mrg      1753:                        /*
                   1754:                         * note: kernel object mappings are currently used in
                   1755:                         * two ways:
                   1756:                         *  [1] "normal" mappings of pages in the kernel object
                   1757:                         *  [2] uvm_km_valloc'd allocations in which we
                   1758:                         *      pmap_enter in some non-kernel-object page
                   1759:                         *      (e.g. vmapbuf).
                   1760:                         *
                   1761:                         * for case [1], we need to remove the mapping from
                   1762:                         * the pmap and then remove the page from the kernel
                   1763:                         * object (because, once pages in a kernel object are
                   1764:                         * unmapped they are no longer needed, unlike, say,
                   1765:                         * a vnode where you might want the data to persist
                   1766:                         * until flushed out of a queue).
                   1767:                         *
                   1768:                         * for case [2], we need to remove the mapping from
                   1769:                         * the pmap.  there shouldn't be any pages at the
                   1770:                         * specified offset in the kernel object [but it
                   1771:                         * doesn't hurt to call uvm_km_pgremove just to be
                   1772:                         * safe?]
                   1773:                         *
1.98      chs      1774:                         * uvm_km_pgremove currently does the following:
                   1775:                         *   for pages in the kernel object in range:
1.43      thorpej  1776:                         *     - drops the swap slot
1.10      mrg      1777:                         *     - uvm_pagefree the page
                   1778:                         */
                   1779:
                   1780:                        /*
1.43      thorpej  1781:                         * remove mappings from pmap and drop the pages
                   1782:                         * from the object.  offsets are always relative
                   1783:                         * to vm_map_min(kernel_map).
1.10      mrg      1784:                         */
1.99      chs      1785:
1.106     chs      1786:                        pmap_remove(pmap_kernel(), entry->start,
                   1787:                            entry->start + len);
                   1788:                        uvm_km_pgremove(entry->object.uvm_obj,
                   1789:                            entry->start - vm_map_min(kernel_map),
                   1790:                            entry->end - vm_map_min(kernel_map));
1.10      mrg      1791:
                   1792:                        /*
                   1793:                         * null out kernel_object reference, we've just
                   1794:                         * dropped it
                   1795:                         */
1.99      chs      1796:
1.10      mrg      1797:                        entry->etype &= ~UVM_ET_OBJ;
1.106     chs      1798:                        entry->object.uvm_obj = NULL;
                   1799:                } else if (UVM_ET_ISOBJ(entry) || entry->aref.ar_amap) {
1.99      chs      1800:
1.29      chuck    1801:                        /*
1.139     enami    1802:                         * remove mappings the standard way.
                   1803:                         */
1.99      chs      1804:
1.29      chuck    1805:                        pmap_remove(map->pmap, entry->start, entry->end);
1.10      mrg      1806:                }
                   1807:
                   1808:                /*
1.98      chs      1809:                 * remove entry from map and put it on our list of entries
1.106     chs      1810:                 * that we've nuked.  then go to next entry.
1.10      mrg      1811:                 */
1.99      chs      1812:
1.10      mrg      1813:                UVMHIST_LOG(maphist, "  removed map entry 0x%x", entry, 0, 0,0);
1.82      thorpej  1814:
                   1815:                /* critical!  prevents stale hint */
                   1816:                SAVE_HINT(map, entry, entry->prev);
                   1817:
1.10      mrg      1818:                uvm_map_entry_unlink(map, entry);
1.146     yamt     1819:                KASSERT(map->size >= len);
1.10      mrg      1820:                map->size -= len;
1.131     atatat   1821:                entry->prev = NULL;
1.10      mrg      1822:                entry->next = first_entry;
                   1823:                first_entry = entry;
1.106     chs      1824:                entry = next;
1.10      mrg      1825:        }
1.120     chs      1826:        if ((map->flags & VM_MAP_DYING) == 0) {
                   1827:                pmap_update(vm_map_pmap(map));
                   1828:        }
1.10      mrg      1829:
1.144     yamt     1830:        uvm_tree_sanity(map, "unmap_remove leave");
                   1831:
1.10      mrg      1832:        /*
                   1833:         * now we've cleaned up the map and are ready for the caller to drop
1.98      chs      1834:         * references to the mapped objects.
1.10      mrg      1835:         */
                   1836:
                   1837:        *entry_list = first_entry;
                   1838:        UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
1.1       mrg      1839: }
                   1840:
                   1841: /*
                   1842:  * uvm_unmap_detach: drop references in a chain of map entries
                   1843:  *
                   1844:  * => we will free the map entries as we traverse the list.
                   1845:  */
                   1846:
1.10      mrg      1847: void
1.138     enami    1848: uvm_unmap_detach(struct vm_map_entry *first_entry, int flags)
1.1       mrg      1849: {
1.99      chs      1850:        struct vm_map_entry *next_entry;
1.10      mrg      1851:        UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist);
1.1       mrg      1852:
1.10      mrg      1853:        while (first_entry) {
1.85      chs      1854:                KASSERT(!VM_MAPENT_ISWIRED(first_entry));
1.10      mrg      1855:                UVMHIST_LOG(maphist,
1.98      chs      1856:                    "  detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d",
                   1857:                    first_entry, first_entry->aref.ar_amap,
1.29      chuck    1858:                    first_entry->object.uvm_obj,
                   1859:                    UVM_ET_ISSUBMAP(first_entry));
1.1       mrg      1860:
1.10      mrg      1861:                /*
                   1862:                 * drop reference to amap, if we've got one
                   1863:                 */
                   1864:
                   1865:                if (first_entry->aref.ar_amap)
1.85      chs      1866:                        uvm_map_unreference_amap(first_entry, flags);
1.10      mrg      1867:
                   1868:                /*
                   1869:                 * drop reference to our backing object, if we've got one
                   1870:                 */
1.85      chs      1871:
1.120     chs      1872:                KASSERT(!UVM_ET_ISSUBMAP(first_entry));
                   1873:                if (UVM_ET_ISOBJ(first_entry) &&
                   1874:                    first_entry->object.uvm_obj->pgops->pgo_detach) {
                   1875:                        (*first_entry->object.uvm_obj->pgops->pgo_detach)
                   1876:                                (first_entry->object.uvm_obj);
1.10      mrg      1877:                }
                   1878:                next_entry = first_entry->next;
                   1879:                uvm_mapent_free(first_entry);
                   1880:                first_entry = next_entry;
                   1881:        }
                   1882:        UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
1.1       mrg      1883: }
                   1884:
                   1885: /*
                   1886:  *   E X T R A C T I O N   F U N C T I O N S
                   1887:  */
                   1888:
1.98      chs      1889: /*
1.1       mrg      1890:  * uvm_map_reserve: reserve space in a vm_map for future use.
                   1891:  *
1.98      chs      1892:  * => we reserve space in a map by putting a dummy map entry in the
1.1       mrg      1893:  *    map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
                   1894:  * => map should be unlocked (we will write lock it)
                   1895:  * => we return true if we were able to reserve space
                   1896:  * => XXXCDC: should be inline?
                   1897:  */
                   1898:
1.10      mrg      1899: int
1.138     enami    1900: uvm_map_reserve(struct vm_map *map, vsize_t size,
                   1901:     vaddr_t offset     /* hint for pmap_prefer */,
                   1902:     vsize_t align      /* alignment hint */,
                   1903:     vaddr_t *raddr     /* IN:hint, OUT: reserved VA */)
1.1       mrg      1904: {
1.98      chs      1905:        UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist);
1.85      chs      1906:
1.10      mrg      1907:        UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)",
1.139     enami    1908:            map,size,offset,raddr);
1.85      chs      1909:
1.10      mrg      1910:        size = round_page(size);
                   1911:        if (*raddr < vm_map_min(map))
1.139     enami    1912:                *raddr = vm_map_min(map);               /* hint */
1.85      chs      1913:
1.10      mrg      1914:        /*
                   1915:         * reserve some virtual space.
                   1916:         */
1.85      chs      1917:
1.81      thorpej  1918:        if (uvm_map(map, raddr, size, NULL, offset, 0,
1.10      mrg      1919:            UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
1.94      chs      1920:            UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
1.10      mrg      1921:            UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
                   1922:                return (FALSE);
1.98      chs      1923:        }
1.85      chs      1924:
1.10      mrg      1925:        UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0);
                   1926:        return (TRUE);
1.1       mrg      1927: }
                   1928:
                   1929: /*
1.98      chs      1930:  * uvm_map_replace: replace a reserved (blank) area of memory with
1.1       mrg      1931:  * real mappings.
                   1932:  *
1.98      chs      1933:  * => caller must WRITE-LOCK the map
1.1       mrg      1934:  * => we return TRUE if replacement was a success
                   1935:  * => we expect the newents chain to have nnewents entrys on it and
                   1936:  *    we expect newents->prev to point to the last entry on the list
                   1937:  * => note newents is allowed to be NULL
                   1938:  */
                   1939:
1.10      mrg      1940: int
1.138     enami    1941: uvm_map_replace(struct vm_map *map, vaddr_t start, vaddr_t end,
                   1942:     struct vm_map_entry *newents, int nnewents)
1.10      mrg      1943: {
1.99      chs      1944:        struct vm_map_entry *oldent, *last;
1.1       mrg      1945:
1.144     yamt     1946:        uvm_tree_sanity(map, "map_replace entry");
                   1947:
1.10      mrg      1948:        /*
                   1949:         * first find the blank map entry at the specified address
                   1950:         */
1.85      chs      1951:
1.10      mrg      1952:        if (!uvm_map_lookup_entry(map, start, &oldent)) {
1.139     enami    1953:                return (FALSE);
1.10      mrg      1954:        }
1.85      chs      1955:
1.10      mrg      1956:        /*
                   1957:         * check to make sure we have a proper blank entry
                   1958:         */
1.1       mrg      1959:
1.98      chs      1960:        if (oldent->start != start || oldent->end != end ||
1.10      mrg      1961:            oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) {
                   1962:                return (FALSE);
                   1963:        }
1.1       mrg      1964:
                   1965: #ifdef DIAGNOSTIC
1.99      chs      1966:
1.10      mrg      1967:        /*
                   1968:         * sanity check the newents chain
                   1969:         */
1.99      chs      1970:
1.10      mrg      1971:        {
1.99      chs      1972:                struct vm_map_entry *tmpent = newents;
1.10      mrg      1973:                int nent = 0;
1.24      eeh      1974:                vaddr_t cur = start;
1.10      mrg      1975:
                   1976:                while (tmpent) {
                   1977:                        nent++;
                   1978:                        if (tmpent->start < cur)
                   1979:                                panic("uvm_map_replace1");
                   1980:                        if (tmpent->start > tmpent->end || tmpent->end > end) {
                   1981:                printf("tmpent->start=0x%lx, tmpent->end=0x%lx, end=0x%lx\n",
                   1982:                            tmpent->start, tmpent->end, end);
                   1983:                                panic("uvm_map_replace2");
                   1984:                        }
                   1985:                        cur = tmpent->end;
                   1986:                        if (tmpent->next) {
                   1987:                                if (tmpent->next->prev != tmpent)
                   1988:                                        panic("uvm_map_replace3");
                   1989:                        } else {
                   1990:                                if (newents->prev != tmpent)
                   1991:                                        panic("uvm_map_replace4");
                   1992:                        }
                   1993:                        tmpent = tmpent->next;
                   1994:                }
                   1995:                if (nent != nnewents)
                   1996:                        panic("uvm_map_replace5");
                   1997:        }
                   1998: #endif
                   1999:
                   2000:        /*
                   2001:         * map entry is a valid blank!   replace it.   (this does all the
                   2002:         * work of map entry link/unlink...).
                   2003:         */
                   2004:
                   2005:        if (newents) {
1.99      chs      2006:                last = newents->prev;
1.10      mrg      2007:
                   2008:                /* critical: flush stale hints out of map */
1.82      thorpej  2009:                SAVE_HINT(map, map->hint, newents);
1.10      mrg      2010:                if (map->first_free == oldent)
                   2011:                        map->first_free = last;
                   2012:
                   2013:                last->next = oldent->next;
                   2014:                last->next->prev = last;
1.144     yamt     2015:
                   2016:                /* Fix RB tree */
                   2017:                uvm_rb_remove(map, oldent);
                   2018:
1.10      mrg      2019:                newents->prev = oldent->prev;
                   2020:                newents->prev->next = newents;
                   2021:                map->nentries = map->nentries + (nnewents - 1);
                   2022:
1.144     yamt     2023:                /* Fixup the RB tree */
                   2024:                {
                   2025:                        int i;
                   2026:                        struct vm_map_entry *tmp;
                   2027:
                   2028:                        tmp = newents;
                   2029:                        for (i = 0; i < nnewents && tmp; i++) {
                   2030:                                uvm_rb_insert(map, tmp);
                   2031:                                tmp = tmp->next;
                   2032:                        }
                   2033:                }
1.10      mrg      2034:        } else {
                   2035:
                   2036:                /* critical: flush stale hints out of map */
1.82      thorpej  2037:                SAVE_HINT(map, map->hint, oldent->prev);
1.10      mrg      2038:                if (map->first_free == oldent)
                   2039:                        map->first_free = oldent->prev;
                   2040:
                   2041:                /* NULL list of new entries: just remove the old one */
                   2042:                uvm_map_entry_unlink(map, oldent);
                   2043:        }
                   2044:
1.144     yamt     2045:        uvm_tree_sanity(map, "map_replace leave");
1.10      mrg      2046:
                   2047:        /*
                   2048:         * now we can free the old blank entry, unlock the map and return.
                   2049:         */
1.1       mrg      2050:
1.10      mrg      2051:        uvm_mapent_free(oldent);
1.139     enami    2052:        return (TRUE);
1.1       mrg      2053: }
                   2054:
                   2055: /*
                   2056:  * uvm_map_extract: extract a mapping from a map and put it somewhere
                   2057:  *     (maybe removing the old mapping)
                   2058:  *
                   2059:  * => maps should be unlocked (we will write lock them)
                   2060:  * => returns 0 on success, error code otherwise
                   2061:  * => start must be page aligned
                   2062:  * => len must be page sized
                   2063:  * => flags:
                   2064:  *      UVM_EXTRACT_REMOVE: remove mappings from srcmap
                   2065:  *      UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
                   2066:  *      UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
                   2067:  *      UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
                   2068:  *    >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
                   2069:  *    >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
                   2070:  *             be used from within the kernel in a kernel level map <<<
                   2071:  */
                   2072:
1.10      mrg      2073: int
1.138     enami    2074: uvm_map_extract(struct vm_map *srcmap, vaddr_t start, vsize_t len,
                   2075:     struct vm_map *dstmap, vaddr_t *dstaddrp, int flags)
1.10      mrg      2076: {
1.24      eeh      2077:        vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge,
1.10      mrg      2078:            oldstart;
1.99      chs      2079:        struct vm_map_entry *chain, *endchain, *entry, *orig_entry, *newentry,
                   2080:            *deadentry, *oldentry;
1.24      eeh      2081:        vsize_t elen;
1.10      mrg      2082:        int nchain, error, copy_ok;
                   2083:        UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist);
1.85      chs      2084:
1.10      mrg      2085:        UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start,
                   2086:            len,0);
                   2087:        UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0);
                   2088:
1.144     yamt     2089:        uvm_tree_sanity(srcmap, "map_extract src enter");
                   2090:        uvm_tree_sanity(dstmap, "map_extract dst enter");
                   2091:
1.10      mrg      2092:        /*
                   2093:         * step 0: sanity check: start must be on a page boundary, length
                   2094:         * must be page sized.  can't ask for CONTIG/QREF if you asked for
                   2095:         * REMOVE.
                   2096:         */
                   2097:
1.85      chs      2098:        KASSERT((start & PAGE_MASK) == 0 && (len & PAGE_MASK) == 0);
                   2099:        KASSERT((flags & UVM_EXTRACT_REMOVE) == 0 ||
                   2100:                (flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) == 0);
1.10      mrg      2101:
                   2102:        /*
                   2103:         * step 1: reserve space in the target map for the extracted area
                   2104:         */
                   2105:
1.76      pk       2106:        dstaddr = vm_map_min(dstmap);
1.81      thorpej  2107:        if (uvm_map_reserve(dstmap, len, start, 0, &dstaddr) == FALSE)
1.139     enami    2108:                return (ENOMEM);
1.10      mrg      2109:        *dstaddrp = dstaddr;    /* pass address back to caller */
                   2110:        UVMHIST_LOG(maphist, "  dstaddr=0x%x", dstaddr,0,0,0);
                   2111:
                   2112:        /*
1.98      chs      2113:         * step 2: setup for the extraction process loop by init'ing the
1.10      mrg      2114:         * map entry chain, locking src map, and looking up the first useful
                   2115:         * entry in the map.
                   2116:         */
1.1       mrg      2117:
1.10      mrg      2118:        end = start + len;
                   2119:        newend = dstaddr + len;
                   2120:        chain = endchain = NULL;
                   2121:        nchain = 0;
                   2122:        vm_map_lock(srcmap);
                   2123:
                   2124:        if (uvm_map_lookup_entry(srcmap, start, &entry)) {
                   2125:
                   2126:                /* "start" is within an entry */
                   2127:                if (flags & UVM_EXTRACT_QREF) {
1.85      chs      2128:
1.10      mrg      2129:                        /*
                   2130:                         * for quick references we don't clip the entry, so
                   2131:                         * the entry may map space "before" the starting
                   2132:                         * virtual address... this is the "fudge" factor
                   2133:                         * (which can be non-zero only the first time
                   2134:                         * through the "while" loop in step 3).
                   2135:                         */
1.85      chs      2136:
1.10      mrg      2137:                        fudge = start - entry->start;
                   2138:                } else {
1.85      chs      2139:
1.10      mrg      2140:                        /*
                   2141:                         * normal reference: we clip the map to fit (thus
                   2142:                         * fudge is zero)
                   2143:                         */
1.85      chs      2144:
1.10      mrg      2145:                        UVM_MAP_CLIP_START(srcmap, entry, start);
1.82      thorpej  2146:                        SAVE_HINT(srcmap, srcmap->hint, entry->prev);
1.10      mrg      2147:                        fudge = 0;
                   2148:                }
1.85      chs      2149:        } else {
1.1       mrg      2150:
1.10      mrg      2151:                /* "start" is not within an entry ... skip to next entry */
                   2152:                if (flags & UVM_EXTRACT_CONTIG) {
                   2153:                        error = EINVAL;
                   2154:                        goto bad;    /* definite hole here ... */
                   2155:                }
1.1       mrg      2156:
1.10      mrg      2157:                entry = entry->next;
                   2158:                fudge = 0;
                   2159:        }
1.85      chs      2160:
1.10      mrg      2161:        /* save values from srcmap for step 6 */
                   2162:        orig_entry = entry;
                   2163:        orig_fudge = fudge;
1.1       mrg      2164:
1.10      mrg      2165:        /*
                   2166:         * step 3: now start looping through the map entries, extracting
                   2167:         * as we go.
                   2168:         */
1.1       mrg      2169:
1.10      mrg      2170:        while (entry->start < end && entry != &srcmap->header) {
1.85      chs      2171:
1.10      mrg      2172:                /* if we are not doing a quick reference, clip it */
                   2173:                if ((flags & UVM_EXTRACT_QREF) == 0)
                   2174:                        UVM_MAP_CLIP_END(srcmap, entry, end);
                   2175:
                   2176:                /* clear needs_copy (allow chunking) */
                   2177:                if (UVM_ET_ISNEEDSCOPY(entry)) {
                   2178:                        if (fudge)
                   2179:                                oldstart = entry->start;
                   2180:                        else
                   2181:                                oldstart = 0;   /* XXX: gcc */
                   2182:                        amap_copy(srcmap, entry, M_NOWAIT, TRUE, start, end);
                   2183:                        if (UVM_ET_ISNEEDSCOPY(entry)) {  /* failed? */
                   2184:                                error = ENOMEM;
                   2185:                                goto bad;
                   2186:                        }
1.85      chs      2187:
1.10      mrg      2188:                        /* amap_copy could clip (during chunk)!  update fudge */
                   2189:                        if (fudge) {
                   2190:                                fudge = fudge - (entry->start - oldstart);
                   2191:                                orig_fudge = fudge;
                   2192:                        }
                   2193:                }
1.1       mrg      2194:
1.10      mrg      2195:                /* calculate the offset of this from "start" */
                   2196:                oldoffset = (entry->start + fudge) - start;
1.1       mrg      2197:
1.10      mrg      2198:                /* allocate a new map entry */
1.126     bouyer   2199:                newentry = uvm_mapent_alloc(dstmap, 0);
1.10      mrg      2200:                if (newentry == NULL) {
                   2201:                        error = ENOMEM;
                   2202:                        goto bad;
                   2203:                }
                   2204:
                   2205:                /* set up new map entry */
                   2206:                newentry->next = NULL;
                   2207:                newentry->prev = endchain;
                   2208:                newentry->start = dstaddr + oldoffset;
                   2209:                newentry->end =
                   2210:                    newentry->start + (entry->end - (entry->start + fudge));
1.37      chs      2211:                if (newentry->end > newend || newentry->end < newentry->start)
1.10      mrg      2212:                        newentry->end = newend;
                   2213:                newentry->object.uvm_obj = entry->object.uvm_obj;
                   2214:                if (newentry->object.uvm_obj) {
                   2215:                        if (newentry->object.uvm_obj->pgops->pgo_reference)
                   2216:                                newentry->object.uvm_obj->pgops->
                   2217:                                    pgo_reference(newentry->object.uvm_obj);
                   2218:                                newentry->offset = entry->offset + fudge;
                   2219:                } else {
                   2220:                        newentry->offset = 0;
                   2221:                }
                   2222:                newentry->etype = entry->etype;
1.98      chs      2223:                newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ?
                   2224:                        entry->max_protection : entry->protection;
1.10      mrg      2225:                newentry->max_protection = entry->max_protection;
                   2226:                newentry->inheritance = entry->inheritance;
                   2227:                newentry->wired_count = 0;
                   2228:                newentry->aref.ar_amap = entry->aref.ar_amap;
                   2229:                if (newentry->aref.ar_amap) {
1.34      chuck    2230:                        newentry->aref.ar_pageoff =
                   2231:                            entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT);
1.85      chs      2232:                        uvm_map_reference_amap(newentry, AMAP_SHARED |
1.10      mrg      2233:                            ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0));
                   2234:                } else {
1.34      chuck    2235:                        newentry->aref.ar_pageoff = 0;
1.10      mrg      2236:                }
                   2237:                newentry->advice = entry->advice;
                   2238:
                   2239:                /* now link it on the chain */
                   2240:                nchain++;
                   2241:                if (endchain == NULL) {
                   2242:                        chain = endchain = newentry;
                   2243:                } else {
                   2244:                        endchain->next = newentry;
                   2245:                        endchain = newentry;
                   2246:                }
                   2247:
                   2248:                /* end of 'while' loop! */
1.98      chs      2249:                if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end &&
1.10      mrg      2250:                    (entry->next == &srcmap->header ||
                   2251:                    entry->next->start != entry->end)) {
                   2252:                        error = EINVAL;
                   2253:                        goto bad;
                   2254:                }
                   2255:                entry = entry->next;
                   2256:                fudge = 0;
                   2257:        }
                   2258:
                   2259:        /*
                   2260:         * step 4: close off chain (in format expected by uvm_map_replace)
                   2261:         */
                   2262:
                   2263:        if (chain)
                   2264:                chain->prev = endchain;
                   2265:
                   2266:        /*
                   2267:         * step 5: attempt to lock the dest map so we can pmap_copy.
1.98      chs      2268:         * note usage of copy_ok:
1.10      mrg      2269:         *   1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
                   2270:         *   0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
                   2271:         */
1.85      chs      2272:
1.10      mrg      2273:        if (srcmap == dstmap || vm_map_lock_try(dstmap) == TRUE) {
                   2274:                copy_ok = 1;
                   2275:                if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
                   2276:                    nchain)) {
                   2277:                        if (srcmap != dstmap)
                   2278:                                vm_map_unlock(dstmap);
                   2279:                        error = EIO;
                   2280:                        goto bad;
                   2281:                }
                   2282:        } else {
                   2283:                copy_ok = 0;
                   2284:                /* replace defered until step 7 */
                   2285:        }
                   2286:
                   2287:        /*
                   2288:         * step 6: traverse the srcmap a second time to do the following:
                   2289:         *  - if we got a lock on the dstmap do pmap_copy
                   2290:         *  - if UVM_EXTRACT_REMOVE remove the entries
                   2291:         * we make use of orig_entry and orig_fudge (saved in step 2)
                   2292:         */
                   2293:
                   2294:        if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) {
                   2295:
                   2296:                /* purge possible stale hints from srcmap */
                   2297:                if (flags & UVM_EXTRACT_REMOVE) {
1.82      thorpej  2298:                        SAVE_HINT(srcmap, srcmap->hint, orig_entry->prev);
1.10      mrg      2299:                        if (srcmap->first_free->start >= start)
                   2300:                                srcmap->first_free = orig_entry->prev;
                   2301:                }
                   2302:
                   2303:                entry = orig_entry;
                   2304:                fudge = orig_fudge;
                   2305:                deadentry = NULL;       /* for UVM_EXTRACT_REMOVE */
                   2306:
                   2307:                while (entry->start < end && entry != &srcmap->header) {
                   2308:                        if (copy_ok) {
1.74      thorpej  2309:                                oldoffset = (entry->start + fudge) - start;
1.90      chs      2310:                                elen = MIN(end, entry->end) -
1.74      thorpej  2311:                                    (entry->start + fudge);
                   2312:                                pmap_copy(dstmap->pmap, srcmap->pmap,
                   2313:                                    dstaddr + oldoffset, elen,
                   2314:                                    entry->start + fudge);
1.10      mrg      2315:                        }
                   2316:
1.74      thorpej  2317:                        /* we advance "entry" in the following if statement */
1.10      mrg      2318:                        if (flags & UVM_EXTRACT_REMOVE) {
1.98      chs      2319:                                pmap_remove(srcmap->pmap, entry->start,
1.20      chuck    2320:                                                entry->end);
1.139     enami    2321:                                oldentry = entry;       /* save entry */
                   2322:                                entry = entry->next;    /* advance */
1.20      chuck    2323:                                uvm_map_entry_unlink(srcmap, oldentry);
                   2324:                                                        /* add to dead list */
                   2325:                                oldentry->next = deadentry;
                   2326:                                deadentry = oldentry;
1.139     enami    2327:                        } else {
                   2328:                                entry = entry->next;            /* advance */
1.10      mrg      2329:                        }
                   2330:
                   2331:                        /* end of 'while' loop */
                   2332:                        fudge = 0;
                   2333:                }
1.105     chris    2334:                pmap_update(srcmap->pmap);
1.10      mrg      2335:
                   2336:                /*
                   2337:                 * unlock dstmap.  we will dispose of deadentry in
                   2338:                 * step 7 if needed
                   2339:                 */
1.85      chs      2340:
1.10      mrg      2341:                if (copy_ok && srcmap != dstmap)
                   2342:                        vm_map_unlock(dstmap);
                   2343:
1.99      chs      2344:        } else {
                   2345:                deadentry = NULL;
1.10      mrg      2346:        }
                   2347:
                   2348:        /*
                   2349:         * step 7: we are done with the source map, unlock.   if copy_ok
                   2350:         * is 0 then we have not replaced the dummy mapping in dstmap yet
                   2351:         * and we need to do so now.
                   2352:         */
                   2353:
                   2354:        vm_map_unlock(srcmap);
                   2355:        if ((flags & UVM_EXTRACT_REMOVE) && deadentry)
                   2356:                uvm_unmap_detach(deadentry, 0);   /* dispose of old entries */
                   2357:
                   2358:        /* now do the replacement if we didn't do it in step 5 */
                   2359:        if (copy_ok == 0) {
                   2360:                vm_map_lock(dstmap);
                   2361:                error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
                   2362:                    nchain);
                   2363:                vm_map_unlock(dstmap);
                   2364:
                   2365:                if (error == FALSE) {
                   2366:                        error = EIO;
                   2367:                        goto bad2;
                   2368:                }
                   2369:        }
1.144     yamt     2370:
                   2371:        uvm_tree_sanity(srcmap, "map_extract src leave");
                   2372:        uvm_tree_sanity(dstmap, "map_extract dst leave");
                   2373:
1.139     enami    2374:        return (0);
1.10      mrg      2375:
                   2376:        /*
                   2377:         * bad: failure recovery
                   2378:         */
                   2379: bad:
                   2380:        vm_map_unlock(srcmap);
                   2381: bad2:                  /* src already unlocked */
                   2382:        if (chain)
                   2383:                uvm_unmap_detach(chain,
                   2384:                    (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0);
1.144     yamt     2385:
                   2386:        uvm_tree_sanity(srcmap, "map_extract src err leave");
                   2387:        uvm_tree_sanity(dstmap, "map_extract dst err leave");
                   2388:
1.29      chuck    2389:        uvm_unmap(dstmap, dstaddr, dstaddr+len);   /* ??? */
1.139     enami    2390:        return (error);
1.10      mrg      2391: }
                   2392:
                   2393: /* end of extraction functions */
1.1       mrg      2394:
                   2395: /*
                   2396:  * uvm_map_submap: punch down part of a map into a submap
                   2397:  *
                   2398:  * => only the kernel_map is allowed to be submapped
                   2399:  * => the purpose of submapping is to break up the locking granularity
                   2400:  *     of a larger map
                   2401:  * => the range specified must have been mapped previously with a uvm_map()
                   2402:  *     call [with uobj==NULL] to create a blank map entry in the main map.
                   2403:  *     [And it had better still be blank!]
                   2404:  * => maps which contain submaps should never be copied or forked.
1.98      chs      2405:  * => to remove a submap, use uvm_unmap() on the main map
1.1       mrg      2406:  *     and then uvm_map_deallocate() the submap.
                   2407:  * => main map must be unlocked.
                   2408:  * => submap must have been init'd and have a zero reference count.
                   2409:  *     [need not be locked as we don't actually reference it]
                   2410:  */
1.85      chs      2411:
1.10      mrg      2412: int
1.138     enami    2413: uvm_map_submap(struct vm_map *map, vaddr_t start, vaddr_t end,
                   2414:     struct vm_map *submap)
1.10      mrg      2415: {
1.99      chs      2416:        struct vm_map_entry *entry;
1.94      chs      2417:        int error;
1.1       mrg      2418:
1.10      mrg      2419:        vm_map_lock(map);
1.85      chs      2420:        VM_MAP_RANGE_CHECK(map, start, end);
1.1       mrg      2421:
1.10      mrg      2422:        if (uvm_map_lookup_entry(map, start, &entry)) {
                   2423:                UVM_MAP_CLIP_START(map, entry, start);
                   2424:                UVM_MAP_CLIP_END(map, entry, end);              /* to be safe */
1.94      chs      2425:        } else {
1.10      mrg      2426:                entry = NULL;
                   2427:        }
1.1       mrg      2428:
1.98      chs      2429:        if (entry != NULL &&
1.10      mrg      2430:            entry->start == start && entry->end == end &&
                   2431:            entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL &&
                   2432:            !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) {
1.29      chuck    2433:                entry->etype |= UVM_ET_SUBMAP;
1.10      mrg      2434:                entry->object.sub_map = submap;
                   2435:                entry->offset = 0;
                   2436:                uvm_map_reference(submap);
1.94      chs      2437:                error = 0;
1.10      mrg      2438:        } else {
1.94      chs      2439:                error = EINVAL;
1.10      mrg      2440:        }
                   2441:        vm_map_unlock(map);
1.94      chs      2442:        return error;
1.1       mrg      2443: }
                   2444:
                   2445:
                   2446: /*
                   2447:  * uvm_map_protect: change map protection
                   2448:  *
                   2449:  * => set_max means set max_protection.
                   2450:  * => map must be unlocked.
                   2451:  */
                   2452:
1.139     enami    2453: #define MASK(entry)    (UVM_ET_ISCOPYONWRITE(entry) ? \
1.36      mycroft  2454:                         ~VM_PROT_WRITE : VM_PROT_ALL)
1.1       mrg      2455:
1.10      mrg      2456: int
1.138     enami    2457: uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
                   2458:     vm_prot_t new_prot, boolean_t set_max)
1.10      mrg      2459: {
1.99      chs      2460:        struct vm_map_entry *current, *entry;
1.94      chs      2461:        int error = 0;
1.10      mrg      2462:        UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist);
                   2463:        UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)",
1.85      chs      2464:                    map, start, end, new_prot);
                   2465:
1.10      mrg      2466:        vm_map_lock(map);
                   2467:        VM_MAP_RANGE_CHECK(map, start, end);
                   2468:        if (uvm_map_lookup_entry(map, start, &entry)) {
                   2469:                UVM_MAP_CLIP_START(map, entry, start);
                   2470:        } else {
                   2471:                entry = entry->next;
                   2472:        }
                   2473:
1.1       mrg      2474:        /*
1.10      mrg      2475:         * make a first pass to check for protection violations.
1.1       mrg      2476:         */
                   2477:
1.10      mrg      2478:        current = entry;
                   2479:        while ((current != &map->header) && (current->start < end)) {
1.65      thorpej  2480:                if (UVM_ET_ISSUBMAP(current)) {
1.94      chs      2481:                        error = EINVAL;
1.65      thorpej  2482:                        goto out;
                   2483:                }
1.10      mrg      2484:                if ((new_prot & current->max_protection) != new_prot) {
1.94      chs      2485:                        error = EACCES;
1.65      thorpej  2486:                        goto out;
1.112     thorpej  2487:                }
                   2488:                /*
                   2489:                 * Don't allow VM_PROT_EXECUTE to be set on entries that
                   2490:                 * point to vnodes that are associated with a NOEXEC file
                   2491:                 * system.
                   2492:                 */
                   2493:                if (UVM_ET_ISOBJ(current) &&
                   2494:                    UVM_OBJ_IS_VNODE(current->object.uvm_obj)) {
                   2495:                        struct vnode *vp =
                   2496:                            (struct vnode *) current->object.uvm_obj;
                   2497:
                   2498:                        if ((new_prot & VM_PROT_EXECUTE) != 0 &&
                   2499:                            (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
                   2500:                                error = EACCES;
                   2501:                                goto out;
                   2502:                        }
1.10      mrg      2503:                }
1.65      thorpej  2504:                current = current->next;
1.10      mrg      2505:        }
                   2506:
                   2507:        /* go back and fix up protections (no need to clip this time). */
                   2508:
                   2509:        current = entry;
                   2510:        while ((current != &map->header) && (current->start < end)) {
                   2511:                vm_prot_t old_prot;
1.85      chs      2512:
1.10      mrg      2513:                UVM_MAP_CLIP_END(map, current, end);
                   2514:                old_prot = current->protection;
                   2515:                if (set_max)
                   2516:                        current->protection =
                   2517:                            (current->max_protection = new_prot) & old_prot;
                   2518:                else
                   2519:                        current->protection = new_prot;
                   2520:
                   2521:                /*
1.98      chs      2522:                 * update physical map if necessary.  worry about copy-on-write
1.10      mrg      2523:                 * here -- CHECK THIS XXX
                   2524:                 */
                   2525:
                   2526:                if (current->protection != old_prot) {
1.29      chuck    2527:                        /* update pmap! */
                   2528:                        pmap_protect(map->pmap, current->start, current->end,
                   2529:                            current->protection & MASK(entry));
1.109     thorpej  2530:
                   2531:                        /*
                   2532:                         * If this entry points at a vnode, and the
                   2533:                         * protection includes VM_PROT_EXECUTE, mark
1.111     thorpej  2534:                         * the vnode as VEXECMAP.
1.109     thorpej  2535:                         */
                   2536:                        if (UVM_ET_ISOBJ(current)) {
                   2537:                                struct uvm_object *uobj =
                   2538:                                    current->object.uvm_obj;
                   2539:
                   2540:                                if (UVM_OBJ_IS_VNODE(uobj) &&
                   2541:                                    (current->protection & VM_PROT_EXECUTE))
1.110     thorpej  2542:                                        vn_markexec((struct vnode *) uobj);
1.109     thorpej  2543:                        }
1.65      thorpej  2544:                }
1.10      mrg      2545:
1.65      thorpej  2546:                /*
                   2547:                 * If the map is configured to lock any future mappings,
                   2548:                 * wire this entry now if the old protection was VM_PROT_NONE
                   2549:                 * and the new protection is not VM_PROT_NONE.
                   2550:                 */
                   2551:
                   2552:                if ((map->flags & VM_MAP_WIREFUTURE) != 0 &&
                   2553:                    VM_MAPENT_ISWIRED(entry) == 0 &&
                   2554:                    old_prot == VM_PROT_NONE &&
                   2555:                    new_prot != VM_PROT_NONE) {
                   2556:                        if (uvm_map_pageable(map, entry->start,
                   2557:                            entry->end, FALSE,
1.94      chs      2558:                            UVM_LK_ENTER|UVM_LK_EXIT) != 0) {
1.99      chs      2559:
1.65      thorpej  2560:                                /*
                   2561:                                 * If locking the entry fails, remember the
                   2562:                                 * error if it's the first one.  Note we
                   2563:                                 * still continue setting the protection in
1.94      chs      2564:                                 * the map, but will return the error
                   2565:                                 * condition regardless.
1.65      thorpej  2566:                                 *
                   2567:                                 * XXX Ignore what the actual error is,
                   2568:                                 * XXX just call it a resource shortage
                   2569:                                 * XXX so that it doesn't get confused
                   2570:                                 * XXX what uvm_map_protect() itself would
                   2571:                                 * XXX normally return.
                   2572:                                 */
1.99      chs      2573:
1.94      chs      2574:                                error = ENOMEM;
1.65      thorpej  2575:                        }
1.10      mrg      2576:                }
                   2577:                current = current->next;
                   2578:        }
1.105     chris    2579:        pmap_update(map->pmap);
1.85      chs      2580:
1.65      thorpej  2581:  out:
1.10      mrg      2582:        vm_map_unlock(map);
1.94      chs      2583:        UVMHIST_LOG(maphist, "<- done, error=%d",error,0,0,0);
                   2584:        return error;
1.1       mrg      2585: }
                   2586:
                   2587: #undef  MASK
                   2588:
1.98      chs      2589: /*
1.1       mrg      2590:  * uvm_map_inherit: set inheritance code for range of addrs in map.
                   2591:  *
                   2592:  * => map must be unlocked
                   2593:  * => note that the inherit code is used during a "fork".  see fork
                   2594:  *     code for details.
                   2595:  */
                   2596:
1.10      mrg      2597: int
1.138     enami    2598: uvm_map_inherit(struct vm_map *map, vaddr_t start, vaddr_t end,
                   2599:     vm_inherit_t new_inheritance)
1.10      mrg      2600: {
1.99      chs      2601:        struct vm_map_entry *entry, *temp_entry;
1.10      mrg      2602:        UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist);
                   2603:        UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)",
                   2604:            map, start, end, new_inheritance);
                   2605:
                   2606:        switch (new_inheritance) {
1.80      wiz      2607:        case MAP_INHERIT_NONE:
                   2608:        case MAP_INHERIT_COPY:
                   2609:        case MAP_INHERIT_SHARE:
1.10      mrg      2610:                break;
                   2611:        default:
                   2612:                UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
1.94      chs      2613:                return EINVAL;
1.10      mrg      2614:        }
1.1       mrg      2615:
1.10      mrg      2616:        vm_map_lock(map);
                   2617:        VM_MAP_RANGE_CHECK(map, start, end);
                   2618:        if (uvm_map_lookup_entry(map, start, &temp_entry)) {
                   2619:                entry = temp_entry;
                   2620:                UVM_MAP_CLIP_START(map, entry, start);
                   2621:        }  else {
                   2622:                entry = temp_entry->next;
                   2623:        }
                   2624:        while ((entry != &map->header) && (entry->start < end)) {
                   2625:                UVM_MAP_CLIP_END(map, entry, end);
                   2626:                entry->inheritance = new_inheritance;
                   2627:                entry = entry->next;
                   2628:        }
                   2629:        vm_map_unlock(map);
                   2630:        UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
1.94      chs      2631:        return 0;
1.41      mrg      2632: }
                   2633:
1.98      chs      2634: /*
1.41      mrg      2635:  * uvm_map_advice: set advice code for range of addrs in map.
                   2636:  *
                   2637:  * => map must be unlocked
                   2638:  */
                   2639:
                   2640: int
1.138     enami    2641: uvm_map_advice(struct vm_map *map, vaddr_t start, vaddr_t end, int new_advice)
1.41      mrg      2642: {
1.99      chs      2643:        struct vm_map_entry *entry, *temp_entry;
1.41      mrg      2644:        UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist);
                   2645:        UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)",
                   2646:            map, start, end, new_advice);
                   2647:
                   2648:        vm_map_lock(map);
                   2649:        VM_MAP_RANGE_CHECK(map, start, end);
                   2650:        if (uvm_map_lookup_entry(map, start, &temp_entry)) {
                   2651:                entry = temp_entry;
                   2652:                UVM_MAP_CLIP_START(map, entry, start);
                   2653:        } else {
                   2654:                entry = temp_entry->next;
                   2655:        }
1.61      thorpej  2656:
                   2657:        /*
                   2658:         * XXXJRT: disallow holes?
                   2659:         */
                   2660:
1.41      mrg      2661:        while ((entry != &map->header) && (entry->start < end)) {
                   2662:                UVM_MAP_CLIP_END(map, entry, end);
                   2663:
                   2664:                switch (new_advice) {
                   2665:                case MADV_NORMAL:
                   2666:                case MADV_RANDOM:
                   2667:                case MADV_SEQUENTIAL:
                   2668:                        /* nothing special here */
                   2669:                        break;
                   2670:
                   2671:                default:
1.50      mrg      2672:                        vm_map_unlock(map);
1.41      mrg      2673:                        UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
1.94      chs      2674:                        return EINVAL;
1.41      mrg      2675:                }
                   2676:                entry->advice = new_advice;
                   2677:                entry = entry->next;
                   2678:        }
                   2679:
                   2680:        vm_map_unlock(map);
                   2681:        UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
1.94      chs      2682:        return 0;
1.1       mrg      2683: }
                   2684:
                   2685: /*
                   2686:  * uvm_map_pageable: sets the pageability of a range in a map.
                   2687:  *
1.56      thorpej  2688:  * => wires map entries.  should not be used for transient page locking.
                   2689:  *     for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()).
1.1       mrg      2690:  * => regions sepcified as not pageable require lock-down (wired) memory
                   2691:  *     and page tables.
1.59      thorpej  2692:  * => map must never be read-locked
                   2693:  * => if islocked is TRUE, map is already write-locked
                   2694:  * => we always unlock the map, since we must downgrade to a read-lock
                   2695:  *     to call uvm_fault_wire()
1.1       mrg      2696:  * => XXXCDC: check this and try and clean it up.
                   2697:  */
                   2698:
1.19      kleink   2699: int
1.138     enami    2700: uvm_map_pageable(struct vm_map *map, vaddr_t start, vaddr_t end,
                   2701:     boolean_t new_pageable, int lockflags)
1.1       mrg      2702: {
1.99      chs      2703:        struct vm_map_entry *entry, *start_entry, *failed_entry;
1.10      mrg      2704:        int rv;
1.60      thorpej  2705: #ifdef DIAGNOSTIC
                   2706:        u_int timestamp_save;
                   2707: #endif
1.10      mrg      2708:        UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist);
                   2709:        UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)",
1.85      chs      2710:                    map, start, end, new_pageable);
                   2711:        KASSERT(map->flags & VM_MAP_PAGEABLE);
1.45      thorpej  2712:
1.64      thorpej  2713:        if ((lockflags & UVM_LK_ENTER) == 0)
1.59      thorpej  2714:                vm_map_lock(map);
1.10      mrg      2715:        VM_MAP_RANGE_CHECK(map, start, end);
                   2716:
1.98      chs      2717:        /*
1.10      mrg      2718:         * only one pageability change may take place at one time, since
                   2719:         * uvm_fault_wire assumes it will be called only once for each
                   2720:         * wiring/unwiring.  therefore, we have to make sure we're actually
                   2721:         * changing the pageability for the entire region.  we do so before
1.98      chs      2722:         * making any changes.
1.10      mrg      2723:         */
                   2724:
                   2725:        if (uvm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1.64      thorpej  2726:                if ((lockflags & UVM_LK_EXIT) == 0)
                   2727:                        vm_map_unlock(map);
1.85      chs      2728:
1.94      chs      2729:                UVMHIST_LOG(maphist,"<- done (fault)",0,0,0,0);
                   2730:                return EFAULT;
1.10      mrg      2731:        }
                   2732:        entry = start_entry;
                   2733:
1.98      chs      2734:        /*
1.100     wiz      2735:         * handle wiring and unwiring separately.
1.10      mrg      2736:         */
1.1       mrg      2737:
1.56      thorpej  2738:        if (new_pageable) {             /* unwire */
1.10      mrg      2739:                UVM_MAP_CLIP_START(map, entry, start);
1.85      chs      2740:
1.10      mrg      2741:                /*
                   2742:                 * unwiring.  first ensure that the range to be unwired is
1.98      chs      2743:                 * really wired down and that there are no holes.
1.10      mrg      2744:                 */
1.85      chs      2745:
1.10      mrg      2746:                while ((entry != &map->header) && (entry->start < end)) {
                   2747:                        if (entry->wired_count == 0 ||
                   2748:                            (entry->end < end &&
1.55      thorpej  2749:                             (entry->next == &map->header ||
                   2750:                              entry->next->start > entry->end))) {
1.64      thorpej  2751:                                if ((lockflags & UVM_LK_EXIT) == 0)
                   2752:                                        vm_map_unlock(map);
1.94      chs      2753:                                UVMHIST_LOG(maphist, "<- done (INVAL)",0,0,0,0);
                   2754:                                return EINVAL;
1.10      mrg      2755:                        }
                   2756:                        entry = entry->next;
                   2757:                }
                   2758:
1.98      chs      2759:                /*
1.56      thorpej  2760:                 * POSIX 1003.1b - a single munlock call unlocks a region,
                   2761:                 * regardless of the number of mlock calls made on that
                   2762:                 * region.
1.10      mrg      2763:                 */
1.85      chs      2764:
1.10      mrg      2765:                entry = start_entry;
                   2766:                while ((entry != &map->header) && (entry->start < end)) {
                   2767:                        UVM_MAP_CLIP_END(map, entry, end);
1.56      thorpej  2768:                        if (VM_MAPENT_ISWIRED(entry))
1.10      mrg      2769:                                uvm_map_entry_unwire(map, entry);
                   2770:                        entry = entry->next;
                   2771:                }
1.64      thorpej  2772:                if ((lockflags & UVM_LK_EXIT) == 0)
                   2773:                        vm_map_unlock(map);
1.10      mrg      2774:                UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
1.94      chs      2775:                return 0;
1.10      mrg      2776:        }
                   2777:
                   2778:        /*
                   2779:         * wire case: in two passes [XXXCDC: ugly block of code here]
                   2780:         *
                   2781:         * 1: holding the write lock, we create any anonymous maps that need
                   2782:         *    to be created.  then we clip each map entry to the region to
1.98      chs      2783:         *    be wired and increment its wiring count.
1.10      mrg      2784:         *
                   2785:         * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
1.56      thorpej  2786:         *    in the pages for any newly wired area (wired_count == 1).
1.10      mrg      2787:         *
                   2788:         *    downgrading to a read lock for uvm_fault_wire avoids a possible
                   2789:         *    deadlock with another thread that may have faulted on one of
                   2790:         *    the pages to be wired (it would mark the page busy, blocking
                   2791:         *    us, then in turn block on the map lock that we hold).  because
                   2792:         *    of problems in the recursive lock package, we cannot upgrade
                   2793:         *    to a write lock in vm_map_lookup.  thus, any actions that
                   2794:         *    require the write lock must be done beforehand.  because we
                   2795:         *    keep the read lock on the map, the copy-on-write status of the
                   2796:         *    entries we modify here cannot change.
                   2797:         */
                   2798:
                   2799:        while ((entry != &map->header) && (entry->start < end)) {
1.55      thorpej  2800:                if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
1.85      chs      2801:
                   2802:                        /*
1.10      mrg      2803:                         * perform actions of vm_map_lookup that need the
                   2804:                         * write lock on the map: create an anonymous map
                   2805:                         * for a copy-on-write region, or an anonymous map
1.29      chuck    2806:                         * for a zero-fill region.  (XXXCDC: submap case
                   2807:                         * ok?)
1.10      mrg      2808:                         */
1.85      chs      2809:
1.29      chuck    2810:                        if (!UVM_ET_ISSUBMAP(entry)) {  /* not submap */
1.98      chs      2811:                                if (UVM_ET_ISNEEDSCOPY(entry) &&
1.117     chs      2812:                                    ((entry->max_protection & VM_PROT_WRITE) ||
1.54      thorpej  2813:                                     (entry->object.uvm_obj == NULL))) {
1.10      mrg      2814:                                        amap_copy(map, entry, M_WAITOK, TRUE,
1.98      chs      2815:                                            start, end);
1.10      mrg      2816:                                        /* XXXCDC: wait OK? */
                   2817:                                }
                   2818:                        }
1.55      thorpej  2819:                }
1.10      mrg      2820:                UVM_MAP_CLIP_START(map, entry, start);
                   2821:                UVM_MAP_CLIP_END(map, entry, end);
                   2822:                entry->wired_count++;
                   2823:
                   2824:                /*
1.98      chs      2825:                 * Check for holes
1.10      mrg      2826:                 */
1.85      chs      2827:
1.54      thorpej  2828:                if (entry->protection == VM_PROT_NONE ||
                   2829:                    (entry->end < end &&
                   2830:                     (entry->next == &map->header ||
                   2831:                      entry->next->start > entry->end))) {
1.85      chs      2832:
1.10      mrg      2833:                        /*
                   2834:                         * found one.  amap creation actions do not need to
1.98      chs      2835:                         * be undone, but the wired counts need to be restored.
1.10      mrg      2836:                         */
1.85      chs      2837:
1.10      mrg      2838:                        while (entry != &map->header && entry->end > start) {
                   2839:                                entry->wired_count--;
                   2840:                                entry = entry->prev;
                   2841:                        }
1.64      thorpej  2842:                        if ((lockflags & UVM_LK_EXIT) == 0)
                   2843:                                vm_map_unlock(map);
1.10      mrg      2844:                        UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0);
1.94      chs      2845:                        return EINVAL;
1.10      mrg      2846:                }
                   2847:                entry = entry->next;
                   2848:        }
                   2849:
                   2850:        /*
                   2851:         * Pass 2.
                   2852:         */
1.51      thorpej  2853:
1.60      thorpej  2854: #ifdef DIAGNOSTIC
                   2855:        timestamp_save = map->timestamp;
                   2856: #endif
                   2857:        vm_map_busy(map);
1.51      thorpej  2858:        vm_map_downgrade(map);
1.10      mrg      2859:
                   2860:        rv = 0;
                   2861:        entry = start_entry;
                   2862:        while (entry != &map->header && entry->start < end) {
1.51      thorpej  2863:                if (entry->wired_count == 1) {
1.44      thorpej  2864:                        rv = uvm_fault_wire(map, entry->start, entry->end,
1.117     chs      2865:                            VM_FAULT_WIREMAX, entry->max_protection);
1.10      mrg      2866:                        if (rv) {
1.94      chs      2867:
1.51      thorpej  2868:                                /*
                   2869:                                 * wiring failed.  break out of the loop.
                   2870:                                 * we'll clean up the map below, once we
                   2871:                                 * have a write lock again.
                   2872:                                 */
1.94      chs      2873:
1.51      thorpej  2874:                                break;
1.10      mrg      2875:                        }
                   2876:                }
                   2877:                entry = entry->next;
                   2878:        }
                   2879:
1.139     enami    2880:        if (rv) {       /* failed? */
1.85      chs      2881:
1.52      thorpej  2882:                /*
                   2883:                 * Get back to an exclusive (write) lock.
                   2884:                 */
1.85      chs      2885:
1.52      thorpej  2886:                vm_map_upgrade(map);
1.60      thorpej  2887:                vm_map_unbusy(map);
                   2888:
                   2889: #ifdef DIAGNOSTIC
                   2890:                if (timestamp_save != map->timestamp)
                   2891:                        panic("uvm_map_pageable: stale map");
                   2892: #endif
1.10      mrg      2893:
1.51      thorpej  2894:                /*
                   2895:                 * first drop the wiring count on all the entries
                   2896:                 * which haven't actually been wired yet.
                   2897:                 */
1.85      chs      2898:
1.54      thorpej  2899:                failed_entry = entry;
                   2900:                while (entry != &map->header && entry->start < end) {
1.51      thorpej  2901:                        entry->wired_count--;
1.54      thorpej  2902:                        entry = entry->next;
                   2903:                }
1.51      thorpej  2904:
                   2905:                /*
1.54      thorpej  2906:                 * now, unwire all the entries that were successfully
                   2907:                 * wired above.
1.51      thorpej  2908:                 */
1.85      chs      2909:
1.54      thorpej  2910:                entry = start_entry;
                   2911:                while (entry != failed_entry) {
                   2912:                        entry->wired_count--;
1.55      thorpej  2913:                        if (VM_MAPENT_ISWIRED(entry) == 0)
1.54      thorpej  2914:                                uvm_map_entry_unwire(map, entry);
                   2915:                        entry = entry->next;
                   2916:                }
1.64      thorpej  2917:                if ((lockflags & UVM_LK_EXIT) == 0)
                   2918:                        vm_map_unlock(map);
1.10      mrg      2919:                UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0);
1.139     enami    2920:                return (rv);
1.10      mrg      2921:        }
1.51      thorpej  2922:
1.52      thorpej  2923:        /* We are holding a read lock here. */
1.64      thorpej  2924:        if ((lockflags & UVM_LK_EXIT) == 0) {
                   2925:                vm_map_unbusy(map);
                   2926:                vm_map_unlock_read(map);
                   2927:        } else {
1.85      chs      2928:
1.64      thorpej  2929:                /*
                   2930:                 * Get back to an exclusive (write) lock.
                   2931:                 */
1.85      chs      2932:
1.64      thorpej  2933:                vm_map_upgrade(map);
                   2934:                vm_map_unbusy(map);
                   2935:        }
                   2936:
1.10      mrg      2937:        UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
1.94      chs      2938:        return 0;
1.1       mrg      2939: }
                   2940:
                   2941: /*
1.54      thorpej  2942:  * uvm_map_pageable_all: special case of uvm_map_pageable - affects
                   2943:  * all mapped regions.
                   2944:  *
                   2945:  * => map must not be locked.
                   2946:  * => if no flags are specified, all regions are unwired.
                   2947:  * => XXXJRT: has some of the same problems as uvm_map_pageable() above.
                   2948:  */
                   2949:
                   2950: int
1.138     enami    2951: uvm_map_pageable_all(struct vm_map *map, int flags, vsize_t limit)
1.54      thorpej  2952: {
1.99      chs      2953:        struct vm_map_entry *entry, *failed_entry;
1.54      thorpej  2954:        vsize_t size;
                   2955:        int rv;
1.60      thorpej  2956: #ifdef DIAGNOSTIC
                   2957:        u_int timestamp_save;
                   2958: #endif
1.54      thorpej  2959:        UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist);
                   2960:        UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0);
                   2961:
1.85      chs      2962:        KASSERT(map->flags & VM_MAP_PAGEABLE);
1.54      thorpej  2963:
                   2964:        vm_map_lock(map);
                   2965:
                   2966:        /*
                   2967:         * handle wiring and unwiring separately.
                   2968:         */
                   2969:
                   2970:        if (flags == 0) {                       /* unwire */
1.99      chs      2971:
1.54      thorpej  2972:                /*
1.56      thorpej  2973:                 * POSIX 1003.1b -- munlockall unlocks all regions,
                   2974:                 * regardless of how many times mlockall has been called.
1.54      thorpej  2975:                 */
1.99      chs      2976:
1.54      thorpej  2977:                for (entry = map->header.next; entry != &map->header;
                   2978:                     entry = entry->next) {
1.56      thorpej  2979:                        if (VM_MAPENT_ISWIRED(entry))
                   2980:                                uvm_map_entry_unwire(map, entry);
1.54      thorpej  2981:                }
1.61      thorpej  2982:                vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
1.54      thorpej  2983:                vm_map_unlock(map);
                   2984:                UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
1.94      chs      2985:                return 0;
1.54      thorpej  2986:        }
                   2987:
                   2988:        if (flags & MCL_FUTURE) {
1.99      chs      2989:
1.54      thorpej  2990:                /*
                   2991:                 * must wire all future mappings; remember this.
                   2992:                 */
1.99      chs      2993:
1.61      thorpej  2994:                vm_map_modflags(map, VM_MAP_WIREFUTURE, 0);
1.54      thorpej  2995:        }
                   2996:
                   2997:        if ((flags & MCL_CURRENT) == 0) {
1.99      chs      2998:
1.54      thorpej  2999:                /*
                   3000:                 * no more work to do!
                   3001:                 */
1.99      chs      3002:
1.54      thorpej  3003:                UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0);
                   3004:                vm_map_unlock(map);
1.94      chs      3005:                return 0;
1.54      thorpej  3006:        }
                   3007:
                   3008:        /*
                   3009:         * wire case: in three passes [XXXCDC: ugly block of code here]
                   3010:         *
                   3011:         * 1: holding the write lock, count all pages mapped by non-wired
                   3012:         *    entries.  if this would cause us to go over our limit, we fail.
                   3013:         *
                   3014:         * 2: still holding the write lock, we create any anonymous maps that
                   3015:         *    need to be created.  then we increment its wiring count.
                   3016:         *
                   3017:         * 3: we downgrade to a read lock, and call uvm_fault_wire to fault
1.56      thorpej  3018:         *    in the pages for any newly wired area (wired_count == 1).
1.54      thorpej  3019:         *
                   3020:         *    downgrading to a read lock for uvm_fault_wire avoids a possible
                   3021:         *    deadlock with another thread that may have faulted on one of
                   3022:         *    the pages to be wired (it would mark the page busy, blocking
                   3023:         *    us, then in turn block on the map lock that we hold).  because
                   3024:         *    of problems in the recursive lock package, we cannot upgrade
                   3025:         *    to a write lock in vm_map_lookup.  thus, any actions that
                   3026:         *    require the write lock must be done beforehand.  because we
                   3027:         *    keep the read lock on the map, the copy-on-write status of the
                   3028:         *    entries we modify here cannot change.
                   3029:         */
                   3030:
                   3031:        for (size = 0, entry = map->header.next; entry != &map->header;
                   3032:             entry = entry->next) {
                   3033:                if (entry->protection != VM_PROT_NONE &&
1.55      thorpej  3034:                    VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
1.54      thorpej  3035:                        size += entry->end - entry->start;
                   3036:                }
                   3037:        }
                   3038:
                   3039:        if (atop(size) + uvmexp.wired > uvmexp.wiredmax) {
                   3040:                vm_map_unlock(map);
1.94      chs      3041:                return ENOMEM;
1.54      thorpej  3042:        }
                   3043:
                   3044:        /* XXX non-pmap_wired_count case must be handled by caller */
                   3045: #ifdef pmap_wired_count
                   3046:        if (limit != 0 &&
                   3047:            (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) {
                   3048:                vm_map_unlock(map);
1.94      chs      3049:                return ENOMEM;
1.54      thorpej  3050:        }
                   3051: #endif
                   3052:
                   3053:        /*
                   3054:         * Pass 2.
                   3055:         */
                   3056:
                   3057:        for (entry = map->header.next; entry != &map->header;
                   3058:             entry = entry->next) {
                   3059:                if (entry->protection == VM_PROT_NONE)
                   3060:                        continue;
1.55      thorpej  3061:                if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
1.99      chs      3062:
1.54      thorpej  3063:                        /*
                   3064:                         * perform actions of vm_map_lookup that need the
                   3065:                         * write lock on the map: create an anonymous map
                   3066:                         * for a copy-on-write region, or an anonymous map
                   3067:                         * for a zero-fill region.  (XXXCDC: submap case
                   3068:                         * ok?)
                   3069:                         */
1.99      chs      3070:
1.54      thorpej  3071:                        if (!UVM_ET_ISSUBMAP(entry)) {  /* not submap */
1.98      chs      3072:                                if (UVM_ET_ISNEEDSCOPY(entry) &&
1.117     chs      3073:                                    ((entry->max_protection & VM_PROT_WRITE) ||
1.54      thorpej  3074:                                     (entry->object.uvm_obj == NULL))) {
                   3075:                                        amap_copy(map, entry, M_WAITOK, TRUE,
                   3076:                                            entry->start, entry->end);
                   3077:                                        /* XXXCDC: wait OK? */
                   3078:                                }
                   3079:                        }
1.55      thorpej  3080:                }
1.54      thorpej  3081:                entry->wired_count++;
                   3082:        }
                   3083:
                   3084:        /*
                   3085:         * Pass 3.
                   3086:         */
                   3087:
1.60      thorpej  3088: #ifdef DIAGNOSTIC
                   3089:        timestamp_save = map->timestamp;
                   3090: #endif
                   3091:        vm_map_busy(map);
1.54      thorpej  3092:        vm_map_downgrade(map);
                   3093:
1.94      chs      3094:        rv = 0;
1.54      thorpej  3095:        for (entry = map->header.next; entry != &map->header;
                   3096:             entry = entry->next) {
                   3097:                if (entry->wired_count == 1) {
                   3098:                        rv = uvm_fault_wire(map, entry->start, entry->end,
1.117     chs      3099:                            VM_FAULT_WIREMAX, entry->max_protection);
1.54      thorpej  3100:                        if (rv) {
1.99      chs      3101:
1.54      thorpej  3102:                                /*
                   3103:                                 * wiring failed.  break out of the loop.
                   3104:                                 * we'll clean up the map below, once we
                   3105:                                 * have a write lock again.
                   3106:                                 */
1.99      chs      3107:
1.54      thorpej  3108:                                break;
                   3109:                        }
                   3110:                }
                   3111:        }
                   3112:
1.99      chs      3113:        if (rv) {
                   3114:
1.54      thorpej  3115:                /*
                   3116:                 * Get back an exclusive (write) lock.
                   3117:                 */
1.99      chs      3118:
1.54      thorpej  3119:                vm_map_upgrade(map);
1.60      thorpej  3120:                vm_map_unbusy(map);
                   3121:
                   3122: #ifdef DIAGNOSTIC
                   3123:                if (timestamp_save != map->timestamp)
                   3124:                        panic("uvm_map_pageable_all: stale map");
                   3125: #endif
1.54      thorpej  3126:
                   3127:                /*
                   3128:                 * first drop the wiring count on all the entries
                   3129:                 * which haven't actually been wired yet.
1.67      thorpej  3130:                 *
                   3131:                 * Skip VM_PROT_NONE entries like we did above.
1.54      thorpej  3132:                 */
1.99      chs      3133:
1.54      thorpej  3134:                failed_entry = entry;
                   3135:                for (/* nothing */; entry != &map->header;
1.67      thorpej  3136:                     entry = entry->next) {
                   3137:                        if (entry->protection == VM_PROT_NONE)
                   3138:                                continue;
1.54      thorpej  3139:                        entry->wired_count--;
1.67      thorpej  3140:                }
1.54      thorpej  3141:
                   3142:                /*
                   3143:                 * now, unwire all the entries that were successfully
                   3144:                 * wired above.
1.67      thorpej  3145:                 *
                   3146:                 * Skip VM_PROT_NONE entries like we did above.
1.54      thorpej  3147:                 */
1.99      chs      3148:
1.54      thorpej  3149:                for (entry = map->header.next; entry != failed_entry;
                   3150:                     entry = entry->next) {
1.67      thorpej  3151:                        if (entry->protection == VM_PROT_NONE)
                   3152:                                continue;
1.54      thorpej  3153:                        entry->wired_count--;
1.67      thorpej  3154:                        if (VM_MAPENT_ISWIRED(entry))
1.54      thorpej  3155:                                uvm_map_entry_unwire(map, entry);
                   3156:                }
                   3157:                vm_map_unlock(map);
                   3158:                UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0);
                   3159:                return (rv);
                   3160:        }
                   3161:
                   3162:        /* We are holding a read lock here. */
1.60      thorpej  3163:        vm_map_unbusy(map);
1.54      thorpej  3164:        vm_map_unlock_read(map);
                   3165:
                   3166:        UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
1.94      chs      3167:        return 0;
1.54      thorpej  3168: }
                   3169:
                   3170: /*
1.61      thorpej  3171:  * uvm_map_clean: clean out a map range
1.1       mrg      3172:  *
                   3173:  * => valid flags:
1.61      thorpej  3174:  *   if (flags & PGO_CLEANIT): dirty pages are cleaned first
1.1       mrg      3175:  *   if (flags & PGO_SYNCIO): dirty pages are written synchronously
                   3176:  *   if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
                   3177:  *   if (flags & PGO_FREE): any cached pages are freed after clean
                   3178:  * => returns an error if any part of the specified range isn't mapped
1.98      chs      3179:  * => never a need to flush amap layer since the anonymous memory has
1.61      thorpej  3180:  *     no permanent home, but may deactivate pages there
                   3181:  * => called from sys_msync() and sys_madvise()
1.1       mrg      3182:  * => caller must not write-lock map (read OK).
                   3183:  * => we may sleep while cleaning if SYNCIO [with map read-locked]
                   3184:  */
                   3185:
1.10      mrg      3186: int
1.138     enami    3187: uvm_map_clean(struct vm_map *map, vaddr_t start, vaddr_t end, int flags)
1.10      mrg      3188: {
1.99      chs      3189:        struct vm_map_entry *current, *entry;
1.61      thorpej  3190:        struct uvm_object *uobj;
                   3191:        struct vm_amap *amap;
                   3192:        struct vm_anon *anon;
                   3193:        struct vm_page *pg;
                   3194:        vaddr_t offset;
1.24      eeh      3195:        vsize_t size;
1.106     chs      3196:        int error, refs;
1.10      mrg      3197:        UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist);
1.85      chs      3198:
1.10      mrg      3199:        UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)",
1.85      chs      3200:                    map, start, end, flags);
                   3201:        KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) !=
                   3202:                (PGO_FREE|PGO_DEACTIVATE));
1.61      thorpej  3203:
1.10      mrg      3204:        vm_map_lock_read(map);
                   3205:        VM_MAP_RANGE_CHECK(map, start, end);
1.61      thorpej  3206:        if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
1.10      mrg      3207:                vm_map_unlock_read(map);
1.94      chs      3208:                return EFAULT;
1.10      mrg      3209:        }
                   3210:
                   3211:        /*
                   3212:         * Make a first pass to check for holes.
                   3213:         */
1.85      chs      3214:
1.10      mrg      3215:        for (current = entry; current->start < end; current = current->next) {
                   3216:                if (UVM_ET_ISSUBMAP(current)) {
                   3217:                        vm_map_unlock_read(map);
1.94      chs      3218:                        return EINVAL;
1.10      mrg      3219:                }
1.90      chs      3220:                if (end <= current->end) {
                   3221:                        break;
                   3222:                }
                   3223:                if (current->end != current->next->start) {
1.10      mrg      3224:                        vm_map_unlock_read(map);
1.94      chs      3225:                        return EFAULT;
1.10      mrg      3226:                }
                   3227:        }
                   3228:
1.94      chs      3229:        error = 0;
1.90      chs      3230:        for (current = entry; start < end; current = current->next) {
1.61      thorpej  3231:                amap = current->aref.ar_amap;   /* top layer */
                   3232:                uobj = current->object.uvm_obj; /* bottom layer */
1.85      chs      3233:                KASSERT(start >= current->start);
1.1       mrg      3234:
1.10      mrg      3235:                /*
1.61      thorpej  3236:                 * No amap cleaning necessary if:
                   3237:                 *
                   3238:                 *      (1) There's no amap.
                   3239:                 *
                   3240:                 *      (2) We're not deactivating or freeing pages.
1.10      mrg      3241:                 */
1.85      chs      3242:
1.90      chs      3243:                if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0)
1.61      thorpej  3244:                        goto flush_object;
                   3245:
                   3246:                amap_lock(amap);
                   3247:                offset = start - current->start;
1.90      chs      3248:                size = MIN(end, current->end) - start;
                   3249:                for ( ; size != 0; size -= PAGE_SIZE, offset += PAGE_SIZE) {
1.61      thorpej  3250:                        anon = amap_lookup(&current->aref, offset);
                   3251:                        if (anon == NULL)
                   3252:                                continue;
                   3253:
                   3254:                        simple_lock(&anon->an_lock);
1.63      thorpej  3255:                        pg = anon->u.an_page;
                   3256:                        if (pg == NULL) {
                   3257:                                simple_unlock(&anon->an_lock);
                   3258:                                continue;
                   3259:                        }
                   3260:
1.61      thorpej  3261:                        switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
1.85      chs      3262:
1.61      thorpej  3263:                        /*
1.115     chs      3264:                         * In these first 3 cases, we just deactivate the page.
1.61      thorpej  3265:                         */
1.85      chs      3266:
1.61      thorpej  3267:                        case PGO_CLEANIT|PGO_FREE:
                   3268:                        case PGO_CLEANIT|PGO_DEACTIVATE:
                   3269:                        case PGO_DEACTIVATE:
1.68      thorpej  3270:  deactivate_it:
1.61      thorpej  3271:                                /*
1.115     chs      3272:                                 * skip the page if it's loaned or wired,
                   3273:                                 * since it shouldn't be on a paging queue
                   3274:                                 * at all in these cases.
1.61      thorpej  3275:                                 */
1.85      chs      3276:
1.115     chs      3277:                                uvm_lock_pageq();
                   3278:                                if (pg->loan_count != 0 ||
                   3279:                                    pg->wire_count != 0) {
1.61      thorpej  3280:                                        uvm_unlock_pageq();
                   3281:                                        simple_unlock(&anon->an_lock);
                   3282:                                        continue;
                   3283:                                }
1.85      chs      3284:                                KASSERT(pg->uanon == anon);
1.89      thorpej  3285:                                pmap_clear_reference(pg);
1.61      thorpej  3286:                                uvm_pagedeactivate(pg);
                   3287:                                uvm_unlock_pageq();
                   3288:                                simple_unlock(&anon->an_lock);
                   3289:                                continue;
                   3290:
                   3291:                        case PGO_FREE:
1.85      chs      3292:
1.68      thorpej  3293:                                /*
                   3294:                                 * If there are multiple references to
                   3295:                                 * the amap, just deactivate the page.
                   3296:                                 */
1.85      chs      3297:
1.68      thorpej  3298:                                if (amap_refs(amap) > 1)
                   3299:                                        goto deactivate_it;
                   3300:
1.115     chs      3301:                                /* skip the page if it's wired */
1.62      thorpej  3302:                                if (pg->wire_count != 0) {
                   3303:                                        simple_unlock(&anon->an_lock);
                   3304:                                        continue;
                   3305:                                }
1.66      thorpej  3306:                                amap_unadd(&current->aref, offset);
1.61      thorpej  3307:                                refs = --anon->an_ref;
                   3308:                                simple_unlock(&anon->an_lock);
                   3309:                                if (refs == 0)
                   3310:                                        uvm_anfree(anon);
                   3311:                                continue;
                   3312:                        }
                   3313:                }
                   3314:                amap_unlock(amap);
1.1       mrg      3315:
1.61      thorpej  3316:  flush_object:
1.10      mrg      3317:                /*
1.33      chuck    3318:                 * flush pages if we've got a valid backing object.
1.116     chs      3319:                 * note that we must always clean object pages before
                   3320:                 * freeing them since otherwise we could reveal stale
                   3321:                 * data from files.
1.10      mrg      3322:                 */
1.1       mrg      3323:
1.61      thorpej  3324:                offset = current->offset + (start - current->start);
1.90      chs      3325:                size = MIN(end, current->end) - start;
1.61      thorpej  3326:                if (uobj != NULL) {
                   3327:                        simple_lock(&uobj->vmobjlock);
1.136     thorpej  3328:                        if (uobj->pgops->pgo_put != NULL)
                   3329:                                error = (uobj->pgops->pgo_put)(uobj, offset,
                   3330:                                    offset + size, flags | PGO_CLEANIT);
                   3331:                        else
                   3332:                                error = 0;
1.10      mrg      3333:                }
                   3334:                start += size;
                   3335:        }
1.1       mrg      3336:        vm_map_unlock_read(map);
1.98      chs      3337:        return (error);
1.1       mrg      3338: }
                   3339:
                   3340:
                   3341: /*
                   3342:  * uvm_map_checkprot: check protection in map
                   3343:  *
                   3344:  * => must allow specified protection in a fully allocated region.
                   3345:  * => map must be read or write locked by caller.
                   3346:  */
                   3347:
1.10      mrg      3348: boolean_t
1.138     enami    3349: uvm_map_checkprot(struct vm_map *map, vaddr_t start, vaddr_t end,
                   3350:     vm_prot_t protection)
1.10      mrg      3351: {
1.99      chs      3352:        struct vm_map_entry *entry;
                   3353:        struct vm_map_entry *tmp_entry;
1.10      mrg      3354:
1.94      chs      3355:        if (!uvm_map_lookup_entry(map, start, &tmp_entry)) {
1.139     enami    3356:                return (FALSE);
1.94      chs      3357:        }
                   3358:        entry = tmp_entry;
                   3359:        while (start < end) {
                   3360:                if (entry == &map->header) {
1.139     enami    3361:                        return (FALSE);
1.94      chs      3362:                }
1.85      chs      3363:
1.10      mrg      3364:                /*
                   3365:                 * no holes allowed
                   3366:                 */
                   3367:
1.94      chs      3368:                if (start < entry->start) {
1.139     enami    3369:                        return (FALSE);
1.94      chs      3370:                }
1.10      mrg      3371:
                   3372:                /*
                   3373:                 * check protection associated with entry
                   3374:                 */
1.1       mrg      3375:
1.94      chs      3376:                if ((entry->protection & protection) != protection) {
1.139     enami    3377:                        return (FALSE);
1.94      chs      3378:                }
                   3379:                start = entry->end;
                   3380:                entry = entry->next;
                   3381:        }
1.139     enami    3382:        return (TRUE);
1.1       mrg      3383: }
                   3384:
                   3385: /*
                   3386:  * uvmspace_alloc: allocate a vmspace structure.
                   3387:  *
                   3388:  * - structure includes vm_map and pmap
                   3389:  * - XXX: no locking on this structure
                   3390:  * - refcnt set to 1, rest must be init'd by caller
                   3391:  */
1.10      mrg      3392: struct vmspace *
1.138     enami    3393: uvmspace_alloc(vaddr_t min, vaddr_t max)
1.10      mrg      3394: {
                   3395:        struct vmspace *vm;
                   3396:        UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist);
                   3397:
1.25      thorpej  3398:        vm = pool_get(&uvm_vmspace_pool, PR_WAITOK);
1.101     chs      3399:        uvmspace_init(vm, NULL, min, max);
1.15      thorpej  3400:        UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0);
                   3401:        return (vm);
                   3402: }
                   3403:
                   3404: /*
                   3405:  * uvmspace_init: initialize a vmspace structure.
                   3406:  *
                   3407:  * - XXX: no locking on this structure
1.132     matt     3408:  * - refcnt set to 1, rest must be init'd by caller
1.15      thorpej  3409:  */
                   3410: void
1.138     enami    3411: uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t min, vaddr_t max)
1.15      thorpej  3412: {
                   3413:        UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist);
                   3414:
1.23      perry    3415:        memset(vm, 0, sizeof(*vm));
1.131     atatat   3416:        uvm_map_setup(&vm->vm_map, min, max, VM_MAP_PAGEABLE
                   3417: #ifdef __USING_TOPDOWN_VM
                   3418:            | VM_MAP_TOPDOWN
                   3419: #endif
                   3420:            );
1.15      thorpej  3421:        if (pmap)
                   3422:                pmap_reference(pmap);
                   3423:        else
                   3424:                pmap = pmap_create();
                   3425:        vm->vm_map.pmap = pmap;
1.10      mrg      3426:        vm->vm_refcnt = 1;
1.15      thorpej  3427:        UVMHIST_LOG(maphist,"<- done",0,0,0,0);
1.1       mrg      3428: }
                   3429:
                   3430: /*
                   3431:  * uvmspace_share: share a vmspace between two proceses
                   3432:  *
                   3433:  * - XXX: no locking on vmspace
                   3434:  * - used for vfork, threads(?)
                   3435:  */
                   3436:
1.10      mrg      3437: void
1.138     enami    3438: uvmspace_share(struct proc *p1, struct proc *p2)
1.1       mrg      3439: {
1.139     enami    3440:
1.10      mrg      3441:        p2->p_vmspace = p1->p_vmspace;
                   3442:        p1->p_vmspace->vm_refcnt++;
1.1       mrg      3443: }
                   3444:
                   3445: /*
                   3446:  * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace
                   3447:  *
                   3448:  * - XXX: no locking on vmspace
                   3449:  */
                   3450:
1.10      mrg      3451: void
1.138     enami    3452: uvmspace_unshare(struct lwp *l)
1.10      mrg      3453: {
1.128     thorpej  3454:        struct proc *p = l->l_proc;
1.10      mrg      3455:        struct vmspace *nvm, *ovm = p->p_vmspace;
1.85      chs      3456:
1.10      mrg      3457:        if (ovm->vm_refcnt == 1)
                   3458:                /* nothing to do: vmspace isn't shared in the first place */
                   3459:                return;
1.85      chs      3460:
1.10      mrg      3461:        /* make a new vmspace, still holding old one */
                   3462:        nvm = uvmspace_fork(ovm);
                   3463:
1.128     thorpej  3464:        pmap_deactivate(l);             /* unbind old vmspace */
1.98      chs      3465:        p->p_vmspace = nvm;
1.128     thorpej  3466:        pmap_activate(l);               /* switch to new vmspace */
1.13      thorpej  3467:
1.10      mrg      3468:        uvmspace_free(ovm);             /* drop reference to old vmspace */
1.1       mrg      3469: }
                   3470:
                   3471: /*
                   3472:  * uvmspace_exec: the process wants to exec a new program
                   3473:  *
                   3474:  * - XXX: no locking on vmspace
                   3475:  */
                   3476:
1.10      mrg      3477: void
1.138     enami    3478: uvmspace_exec(struct lwp *l, vaddr_t start, vaddr_t end)
1.1       mrg      3479: {
1.128     thorpej  3480:        struct proc *p = l->l_proc;
1.10      mrg      3481:        struct vmspace *nvm, *ovm = p->p_vmspace;
1.99      chs      3482:        struct vm_map *map = &ovm->vm_map;
1.1       mrg      3483:
1.71      chs      3484: #ifdef __sparc__
1.10      mrg      3485:        /* XXX cgd 960926: the sparc #ifdef should be a MD hook */
1.128     thorpej  3486:        kill_user_windows(l);   /* before stack addresses go away */
1.1       mrg      3487: #endif
                   3488:
1.10      mrg      3489:        /*
                   3490:         * see if more than one process is using this vmspace...
                   3491:         */
1.1       mrg      3492:
1.10      mrg      3493:        if (ovm->vm_refcnt == 1) {
1.1       mrg      3494:
1.10      mrg      3495:                /*
                   3496:                 * if p is the only process using its vmspace then we can safely
                   3497:                 * recycle that vmspace for the program that is being exec'd.
                   3498:                 */
1.1       mrg      3499:
                   3500: #ifdef SYSVSHM
1.10      mrg      3501:                /*
                   3502:                 * SYSV SHM semantics require us to kill all segments on an exec
                   3503:                 */
1.99      chs      3504:
1.10      mrg      3505:                if (ovm->vm_shm)
                   3506:                        shmexit(ovm);
                   3507: #endif
1.54      thorpej  3508:
                   3509:                /*
                   3510:                 * POSIX 1003.1b -- "lock future mappings" is revoked
                   3511:                 * when a process execs another program image.
                   3512:                 */
1.99      chs      3513:
1.61      thorpej  3514:                vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
1.10      mrg      3515:
                   3516:                /*
                   3517:                 * now unmap the old program
                   3518:                 */
1.99      chs      3519:
1.120     chs      3520:                pmap_remove_all(map->pmap);
1.91      eeh      3521:                uvm_unmap(map, map->min_offset, map->max_offset);
1.144     yamt     3522:                KASSERT(map->header.prev == &map->header);
                   3523:                KASSERT(map->nentries == 0);
1.93      eeh      3524:
                   3525:                /*
                   3526:                 * resize the map
                   3527:                 */
1.99      chs      3528:
1.93      eeh      3529:                map->min_offset = start;
                   3530:                map->max_offset = end;
1.10      mrg      3531:        } else {
                   3532:
                   3533:                /*
                   3534:                 * p's vmspace is being shared, so we can't reuse it for p since
                   3535:                 * it is still being used for others.   allocate a new vmspace
                   3536:                 * for p
                   3537:                 */
1.99      chs      3538:
1.101     chs      3539:                nvm = uvmspace_alloc(start, end);
1.1       mrg      3540:
1.10      mrg      3541:                /*
                   3542:                 * install new vmspace and drop our ref to the old one.
                   3543:                 */
                   3544:
1.128     thorpej  3545:                pmap_deactivate(l);
1.10      mrg      3546:                p->p_vmspace = nvm;
1.128     thorpej  3547:                pmap_activate(l);
1.13      thorpej  3548:
1.10      mrg      3549:                uvmspace_free(ovm);
                   3550:        }
1.1       mrg      3551: }
                   3552:
                   3553: /*
                   3554:  * uvmspace_free: free a vmspace data structure
                   3555:  *
                   3556:  * - XXX: no locking on vmspace
                   3557:  */
                   3558:
1.10      mrg      3559: void
1.138     enami    3560: uvmspace_free(struct vmspace *vm)
1.1       mrg      3561: {
1.99      chs      3562:        struct vm_map_entry *dead_entries;
1.120     chs      3563:        struct vm_map *map;
1.10      mrg      3564:        UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist);
1.1       mrg      3565:
1.10      mrg      3566:        UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0);
1.120     chs      3567:        if (--vm->vm_refcnt > 0) {
                   3568:                return;
                   3569:        }
1.99      chs      3570:
1.120     chs      3571:        /*
                   3572:         * at this point, there should be no other references to the map.
                   3573:         * delete all of the mappings, then destroy the pmap.
                   3574:         */
1.99      chs      3575:
1.120     chs      3576:        map = &vm->vm_map;
                   3577:        map->flags |= VM_MAP_DYING;
                   3578:        pmap_remove_all(map->pmap);
1.92      thorpej  3579: #ifdef SYSVSHM
1.120     chs      3580:        /* Get rid of any SYSV shared memory segments. */
                   3581:        if (vm->vm_shm != NULL)
                   3582:                shmexit(vm);
1.92      thorpej  3583: #endif
1.120     chs      3584:        if (map->nentries) {
                   3585:                uvm_unmap_remove(map, map->min_offset, map->max_offset,
                   3586:                    &dead_entries);
                   3587:                if (dead_entries != NULL)
                   3588:                        uvm_unmap_detach(dead_entries, 0);
1.10      mrg      3589:        }
1.146     yamt     3590:        KASSERT(map->nentries == 0);
                   3591:        KASSERT(map->size == 0);
1.120     chs      3592:        pmap_destroy(map->pmap);
                   3593:        pool_put(&uvm_vmspace_pool, vm);
1.1       mrg      3594: }
                   3595:
                   3596: /*
                   3597:  *   F O R K   -   m a i n   e n t r y   p o i n t
                   3598:  */
                   3599: /*
                   3600:  * uvmspace_fork: fork a process' main map
                   3601:  *
                   3602:  * => create a new vmspace for child process from parent.
                   3603:  * => parent's map must not be locked.
                   3604:  */
                   3605:
1.10      mrg      3606: struct vmspace *
1.138     enami    3607: uvmspace_fork(struct vmspace *vm1)
1.10      mrg      3608: {
                   3609:        struct vmspace *vm2;
1.99      chs      3610:        struct vm_map *old_map = &vm1->vm_map;
                   3611:        struct vm_map *new_map;
                   3612:        struct vm_map_entry *old_entry;
                   3613:        struct vm_map_entry *new_entry;
1.10      mrg      3614:        UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist);
1.1       mrg      3615:
1.10      mrg      3616:        vm_map_lock(old_map);
1.1       mrg      3617:
1.101     chs      3618:        vm2 = uvmspace_alloc(old_map->min_offset, old_map->max_offset);
1.23      perry    3619:        memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy,
1.143     junyoung 3620:            (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
1.10      mrg      3621:        new_map = &vm2->vm_map;           /* XXX */
                   3622:
                   3623:        old_entry = old_map->header.next;
                   3624:
                   3625:        /*
                   3626:         * go entry-by-entry
                   3627:         */
1.1       mrg      3628:
1.10      mrg      3629:        while (old_entry != &old_map->header) {
1.1       mrg      3630:
1.10      mrg      3631:                /*
                   3632:                 * first, some sanity checks on the old entry
                   3633:                 */
1.99      chs      3634:
1.94      chs      3635:                KASSERT(!UVM_ET_ISSUBMAP(old_entry));
                   3636:                KASSERT(UVM_ET_ISCOPYONWRITE(old_entry) ||
                   3637:                        !UVM_ET_ISNEEDSCOPY(old_entry));
1.1       mrg      3638:
1.10      mrg      3639:                switch (old_entry->inheritance) {
1.80      wiz      3640:                case MAP_INHERIT_NONE:
1.99      chs      3641:
1.10      mrg      3642:                        /*
                   3643:                         * drop the mapping
                   3644:                         */
1.99      chs      3645:
1.10      mrg      3646:                        break;
                   3647:
1.80      wiz      3648:                case MAP_INHERIT_SHARE:
1.99      chs      3649:
1.10      mrg      3650:                        /*
                   3651:                         * share the mapping: this means we want the old and
                   3652:                         * new entries to share amaps and backing objects.
                   3653:                         */
                   3654:                        /*
                   3655:                         * if the old_entry needs a new amap (due to prev fork)
                   3656:                         * then we need to allocate it now so that we have
                   3657:                         * something we own to share with the new_entry.   [in
                   3658:                         * other words, we need to clear needs_copy]
                   3659:                         */
                   3660:
                   3661:                        if (UVM_ET_ISNEEDSCOPY(old_entry)) {
                   3662:                                /* get our own amap, clears needs_copy */
                   3663:                                amap_copy(old_map, old_entry, M_WAITOK, FALSE,
1.98      chs      3664:                                    0, 0);
1.10      mrg      3665:                                /* XXXCDC: WAITOK??? */
                   3666:                        }
                   3667:
1.126     bouyer   3668:                        new_entry = uvm_mapent_alloc(new_map, 0);
1.10      mrg      3669:                        /* old_entry -> new_entry */
                   3670:                        uvm_mapent_copy(old_entry, new_entry);
                   3671:
                   3672:                        /* new pmap has nothing wired in it */
                   3673:                        new_entry->wired_count = 0;
                   3674:
                   3675:                        /*
1.29      chuck    3676:                         * gain reference to object backing the map (can't
                   3677:                         * be a submap, already checked this case).
1.10      mrg      3678:                         */
1.99      chs      3679:
1.10      mrg      3680:                        if (new_entry->aref.ar_amap)
1.85      chs      3681:                                uvm_map_reference_amap(new_entry, AMAP_SHARED);
1.10      mrg      3682:
                   3683:                        if (new_entry->object.uvm_obj &&
                   3684:                            new_entry->object.uvm_obj->pgops->pgo_reference)
                   3685:                                new_entry->object.uvm_obj->
                   3686:                                    pgops->pgo_reference(
                   3687:                                        new_entry->object.uvm_obj);
                   3688:
                   3689:                        /* insert entry at end of new_map's entry list */
                   3690:                        uvm_map_entry_link(new_map, new_map->header.prev,
                   3691:                            new_entry);
                   3692:
                   3693:                        break;
                   3694:
1.80      wiz      3695:                case MAP_INHERIT_COPY:
1.10      mrg      3696:
                   3697:                        /*
                   3698:                         * copy-on-write the mapping (using mmap's
                   3699:                         * MAP_PRIVATE semantics)
1.29      chuck    3700:                         *
1.98      chs      3701:                         * allocate new_entry, adjust reference counts.
1.29      chuck    3702:                         * (note that new references are read-only).
1.10      mrg      3703:                         */
                   3704:
1.126     bouyer   3705:                        new_entry = uvm_mapent_alloc(new_map, 0);
1.10      mrg      3706:                        /* old_entry -> new_entry */
                   3707:                        uvm_mapent_copy(old_entry, new_entry);
                   3708:
                   3709:                        if (new_entry->aref.ar_amap)
1.85      chs      3710:                                uvm_map_reference_amap(new_entry, 0);
1.10      mrg      3711:
                   3712:                        if (new_entry->object.uvm_obj &&
                   3713:                            new_entry->object.uvm_obj->pgops->pgo_reference)
                   3714:                                new_entry->object.uvm_obj->pgops->pgo_reference
                   3715:                                    (new_entry->object.uvm_obj);
                   3716:
                   3717:                        /* new pmap has nothing wired in it */
                   3718:                        new_entry->wired_count = 0;
                   3719:
                   3720:                        new_entry->etype |=
                   3721:                            (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY);
                   3722:                        uvm_map_entry_link(new_map, new_map->header.prev,
                   3723:                            new_entry);
1.85      chs      3724:
1.14      chuck    3725:                        /*
1.10      mrg      3726:                         * the new entry will need an amap.  it will either
                   3727:                         * need to be copied from the old entry or created
1.14      chuck    3728:                         * from scratch (if the old entry does not have an
                   3729:                         * amap).  can we defer this process until later
                   3730:                         * (by setting "needs_copy") or do we need to copy
                   3731:                         * the amap now?
1.10      mrg      3732:                         *
1.14      chuck    3733:                         * we must copy the amap now if any of the following
1.10      mrg      3734:                         * conditions hold:
1.14      chuck    3735:                         * 1. the old entry has an amap and that amap is
                   3736:                         *    being shared.  this means that the old (parent)
1.98      chs      3737:                         *    process is sharing the amap with another
1.14      chuck    3738:                         *    process.  if we do not clear needs_copy here
                   3739:                         *    we will end up in a situation where both the
                   3740:                         *    parent and child process are refering to the
1.98      chs      3741:                         *    same amap with "needs_copy" set.  if the
1.14      chuck    3742:                         *    parent write-faults, the fault routine will
                   3743:                         *    clear "needs_copy" in the parent by allocating
1.98      chs      3744:                         *    a new amap.   this is wrong because the
1.14      chuck    3745:                         *    parent is supposed to be sharing the old amap
                   3746:                         *    and the new amap will break that.
1.10      mrg      3747:                         *
1.14      chuck    3748:                         * 2. if the old entry has an amap and a non-zero
                   3749:                         *    wire count then we are going to have to call
1.98      chs      3750:                         *    amap_cow_now to avoid page faults in the
1.14      chuck    3751:                         *    parent process.   since amap_cow_now requires
                   3752:                         *    "needs_copy" to be clear we might as well
                   3753:                         *    clear it here as well.
1.10      mrg      3754:                         *
                   3755:                         */
                   3756:
1.14      chuck    3757:                        if (old_entry->aref.ar_amap != NULL) {
1.99      chs      3758:                                if ((amap_flags(old_entry->aref.ar_amap) &
                   3759:                                     AMAP_SHARED) != 0 ||
                   3760:                                    VM_MAPENT_ISWIRED(old_entry)) {
                   3761:
                   3762:                                        amap_copy(new_map, new_entry, M_WAITOK,
                   3763:                                            FALSE, 0, 0);
                   3764:                                        /* XXXCDC: M_WAITOK ... ok? */
                   3765:                                }
1.10      mrg      3766:                        }
1.85      chs      3767:
1.10      mrg      3768:                        /*
1.14      chuck    3769:                         * if the parent's entry is wired down, then the
                   3770:                         * parent process does not want page faults on
                   3771:                         * access to that memory.  this means that we
                   3772:                         * cannot do copy-on-write because we can't write
                   3773:                         * protect the old entry.   in this case we
                   3774:                         * resolve all copy-on-write faults now, using
                   3775:                         * amap_cow_now.   note that we have already
                   3776:                         * allocated any needed amap (above).
1.10      mrg      3777:                         */
                   3778:
1.55      thorpej  3779:                        if (VM_MAPENT_ISWIRED(old_entry)) {
1.1       mrg      3780:
1.98      chs      3781:                          /*
1.14      chuck    3782:                           * resolve all copy-on-write faults now
1.98      chs      3783:                           * (note that there is nothing to do if
1.14      chuck    3784:                           * the old mapping does not have an amap).
                   3785:                           */
                   3786:                          if (old_entry->aref.ar_amap)
                   3787:                            amap_cow_now(new_map, new_entry);
                   3788:
1.98      chs      3789:                        } else {
1.14      chuck    3790:
                   3791:                          /*
                   3792:                           * setup mappings to trigger copy-on-write faults
                   3793:                           * we must write-protect the parent if it has
                   3794:                           * an amap and it is not already "needs_copy"...
                   3795:                           * if it is already "needs_copy" then the parent
                   3796:                           * has already been write-protected by a previous
                   3797:                           * fork operation.
                   3798:                           */
                   3799:
1.113     chs      3800:                          if (old_entry->aref.ar_amap &&
                   3801:                              !UVM_ET_ISNEEDSCOPY(old_entry)) {
1.14      chuck    3802:                              if (old_entry->max_protection & VM_PROT_WRITE) {
                   3803:                                pmap_protect(old_map->pmap,
                   3804:                                             old_entry->start,
                   3805:                                             old_entry->end,
                   3806:                                             old_entry->protection &
                   3807:                                             ~VM_PROT_WRITE);
1.105     chris    3808:                                pmap_update(old_map->pmap);
1.14      chuck    3809:                              }
                   3810:                              old_entry->etype |= UVM_ET_NEEDSCOPY;
                   3811:                          }
1.10      mrg      3812:                        }
                   3813:                        break;
1.14      chuck    3814:                }  /* end of switch statement */
1.10      mrg      3815:                old_entry = old_entry->next;
1.1       mrg      3816:        }
                   3817:
1.10      mrg      3818:        new_map->size = old_map->size;
1.98      chs      3819:        vm_map_unlock(old_map);
1.1       mrg      3820:
                   3821: #ifdef SYSVSHM
1.10      mrg      3822:        if (vm1->vm_shm)
                   3823:                shmfork(vm1, vm2);
1.39      thorpej  3824: #endif
                   3825:
                   3826: #ifdef PMAP_FORK
                   3827:        pmap_fork(vm1->vm_map.pmap, vm2->vm_map.pmap);
1.1       mrg      3828: #endif
                   3829:
1.10      mrg      3830:        UVMHIST_LOG(maphist,"<- done",0,0,0,0);
1.139     enami    3831:        return (vm2);
1.1       mrg      3832: }
                   3833:
                   3834:
                   3835: #if defined(DDB)
                   3836:
                   3837: /*
                   3838:  * DDB hooks
                   3839:  */
                   3840:
                   3841: /*
                   3842:  * uvm_map_printit: actually prints the map
                   3843:  */
                   3844:
1.10      mrg      3845: void
1.138     enami    3846: uvm_map_printit(struct vm_map *map, boolean_t full,
                   3847:     void (*pr)(const char *, ...))
1.10      mrg      3848: {
1.99      chs      3849:        struct vm_map_entry *entry;
1.10      mrg      3850:
                   3851:        (*pr)("MAP %p: [0x%lx->0x%lx]\n", map, map->min_offset,map->max_offset);
1.53      thorpej  3852:        (*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n",
                   3853:            map->nentries, map->size, map->ref_count, map->timestamp,
                   3854:            map->flags);
1.98      chs      3855:        (*pr)("\tpmap=%p(resident=%d)\n", map->pmap,
1.16      chuck    3856:            pmap_resident_count(map->pmap));
1.10      mrg      3857:        if (!full)
                   3858:                return;
                   3859:        for (entry = map->header.next; entry != &map->header;
                   3860:            entry = entry->next) {
1.70      kleink   3861:                (*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n",
1.10      mrg      3862:                    entry, entry->start, entry->end, entry->object.uvm_obj,
1.85      chs      3863:                    (long long)entry->offset, entry->aref.ar_amap,
                   3864:                    entry->aref.ar_pageoff);
1.10      mrg      3865:                (*pr)(
1.85      chs      3866:                    "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, "
                   3867:                    "wc=%d, adv=%d\n",
1.10      mrg      3868:                    (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
1.98      chs      3869:                    (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
1.10      mrg      3870:                    (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
                   3871:                    entry->protection, entry->max_protection,
                   3872:                    entry->inheritance, entry->wired_count, entry->advice);
                   3873:        }
1.98      chs      3874: }
1.1       mrg      3875:
                   3876: /*
                   3877:  * uvm_object_printit: actually prints the object
                   3878:  */
                   3879:
1.10      mrg      3880: void
1.138     enami    3881: uvm_object_printit(struct uvm_object *uobj, boolean_t full,
                   3882:     void (*pr)(const char *, ...))
1.10      mrg      3883: {
                   3884:        struct vm_page *pg;
                   3885:        int cnt = 0;
                   3886:
1.71      chs      3887:        (*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ",
                   3888:            uobj, uobj->vmobjlock.lock_data, uobj->pgops, uobj->uo_npages);
1.42      thorpej  3889:        if (UVM_OBJ_IS_KERN_OBJECT(uobj))
1.10      mrg      3890:                (*pr)("refs=<SYSTEM>\n");
                   3891:        else
                   3892:                (*pr)("refs=%d\n", uobj->uo_refs);
                   3893:
1.71      chs      3894:        if (!full) {
                   3895:                return;
                   3896:        }
1.10      mrg      3897:        (*pr)("  PAGES <pg,offset>:\n  ");
1.106     chs      3898:        TAILQ_FOREACH(pg, &uobj->memq, listq) {
                   3899:                cnt++;
1.85      chs      3900:                (*pr)("<%p,0x%llx> ", pg, (long long)pg->offset);
1.106     chs      3901:                if ((cnt % 3) == 0) {
1.71      chs      3902:                        (*pr)("\n  ");
                   3903:                }
                   3904:        }
1.106     chs      3905:        if ((cnt % 3) != 0) {
1.71      chs      3906:                (*pr)("\n");
1.10      mrg      3907:        }
1.98      chs      3908: }
1.1       mrg      3909:
                   3910: /*
                   3911:  * uvm_page_printit: actually print the page
                   3912:  */
                   3913:
1.86      chs      3914: static const char page_flagbits[] =
1.106     chs      3915:        "\20\1BUSY\2WANTED\3TABLED\4CLEAN\5PAGEOUT\6RELEASED\7FAKE\10RDONLY"
1.86      chs      3916:        "\11ZERO\15PAGER1";
                   3917: static const char page_pqflagbits[] =
1.106     chs      3918:        "\20\1FREE\2INACTIVE\3ACTIVE\5ANON\6AOBJ";
1.86      chs      3919:
1.10      mrg      3920: void
1.138     enami    3921: uvm_page_printit(struct vm_page *pg, boolean_t full,
                   3922:     void (*pr)(const char *, ...))
1.10      mrg      3923: {
1.85      chs      3924:        struct vm_page *tpg;
1.10      mrg      3925:        struct uvm_object *uobj;
                   3926:        struct pglist *pgl;
1.71      chs      3927:        char pgbuf[128];
                   3928:        char pqbuf[128];
1.1       mrg      3929:
1.10      mrg      3930:        (*pr)("PAGE %p:\n", pg);
1.71      chs      3931:        bitmask_snprintf(pg->flags, page_flagbits, pgbuf, sizeof(pgbuf));
                   3932:        bitmask_snprintf(pg->pqflags, page_pqflagbits, pqbuf, sizeof(pqbuf));
1.106     chs      3933:        (*pr)("  flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n",
1.137     yamt     3934:            pgbuf, pqbuf, pg->wire_count, (long)VM_PAGE_TO_PHYS(pg));
1.86      chs      3935:        (*pr)("  uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n",
                   3936:            pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count);
1.1       mrg      3937: #if defined(UVM_PAGE_TRKOWN)
1.10      mrg      3938:        if (pg->flags & PG_BUSY)
                   3939:                (*pr)("  owning process = %d, tag=%s\n",
                   3940:                    pg->owner, pg->owner_tag);
                   3941:        else
                   3942:                (*pr)("  page not busy, no owner\n");
1.1       mrg      3943: #else
1.10      mrg      3944:        (*pr)("  [page ownership tracking disabled]\n");
1.1       mrg      3945: #endif
                   3946:
1.10      mrg      3947:        if (!full)
                   3948:                return;
                   3949:
                   3950:        /* cross-verify object/anon */
                   3951:        if ((pg->pqflags & PQ_FREE) == 0) {
                   3952:                if (pg->pqflags & PQ_ANON) {
                   3953:                        if (pg->uanon == NULL || pg->uanon->u.an_page != pg)
1.85      chs      3954:                            (*pr)("  >>> ANON DOES NOT POINT HERE <<< (%p)\n",
1.10      mrg      3955:                                (pg->uanon) ? pg->uanon->u.an_page : NULL);
                   3956:                        else
                   3957:                                (*pr)("  anon backpointer is OK\n");
                   3958:                } else {
                   3959:                        uobj = pg->uobject;
                   3960:                        if (uobj) {
                   3961:                                (*pr)("  checking object list\n");
1.85      chs      3962:                                TAILQ_FOREACH(tpg, &uobj->memq, listq) {
                   3963:                                        if (tpg == pg) {
                   3964:                                                break;
                   3965:                                        }
1.10      mrg      3966:                                }
1.85      chs      3967:                                if (tpg)
1.10      mrg      3968:                                        (*pr)("  page found on object list\n");
                   3969:                                else
                   3970:                        (*pr)("  >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
                   3971:                        }
                   3972:                }
                   3973:        }
1.1       mrg      3974:
1.10      mrg      3975:        /* cross-verify page queue */
1.73      thorpej  3976:        if (pg->pqflags & PQ_FREE) {
                   3977:                int fl = uvm_page_lookup_freelist(pg);
1.96      thorpej  3978:                int color = VM_PGCOLOR_BUCKET(pg);
                   3979:                pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[
                   3980:                    ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN];
1.85      chs      3981:        } else if (pg->pqflags & PQ_INACTIVE) {
1.97      ross     3982:                pgl = &uvm.page_inactive;
1.85      chs      3983:        } else if (pg->pqflags & PQ_ACTIVE) {
1.10      mrg      3984:                pgl = &uvm.page_active;
1.139     enami    3985:        } else {
1.10      mrg      3986:                pgl = NULL;
1.85      chs      3987:        }
1.10      mrg      3988:
                   3989:        if (pgl) {
                   3990:                (*pr)("  checking pageq list\n");
1.85      chs      3991:                TAILQ_FOREACH(tpg, pgl, pageq) {
                   3992:                        if (tpg == pg) {
                   3993:                                break;
                   3994:                        }
1.10      mrg      3995:                }
1.85      chs      3996:                if (tpg)
1.10      mrg      3997:                        (*pr)("  page found on pageq list\n");
                   3998:                else
                   3999:                        (*pr)("  >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
                   4000:        }
1.1       mrg      4001: }
                   4002: #endif

CVSweb <webmaster@jp.NetBSD.org>