Annotation of Gnu-Mach/vm/memory_object.c, revision 1.1.1.6

1.1       root        1: /*
                      2:  * Mach Operating System
                      3:  * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University.
                      4:  * Copyright (c) 1993,1994 The University of Utah and
                      5:  * the Computer Systems Laboratory (CSL).
                      6:  * All rights reserved.
                      7:  *
                      8:  * Permission to use, copy, modify and distribute this software and its
                      9:  * documentation is hereby granted, provided that both the copyright
                     10:  * notice and this permission notice appear in all copies of the
                     11:  * software, derivative works or modified versions, and any portions
                     12:  * thereof, and that both notices appear in supporting documentation.
                     13:  *
                     14:  * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF
                     15:  * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
                     16:  * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
                     17:  * THIS SOFTWARE.
                     18:  *
                     19:  * Carnegie Mellon requests users of this software to return to
                     20:  *
                     21:  *  Software Distribution Coordinator  or  [email protected]
                     22:  *  School of Computer Science
                     23:  *  Carnegie Mellon University
                     24:  *  Pittsburgh PA 15213-3890
                     25:  *
                     26:  * any improvements or extensions that they make and grant Carnegie Mellon
                     27:  * the rights to redistribute these changes.
                     28:  */
                     29: /*
                     30:  *     File:   vm/memory_object.c
                     31:  *     Author: Michael Wayne Young
                     32:  *
                     33:  *     External memory management interface control functions.
                     34:  */
                     35: 
                     36: /*
                     37:  *     Interface dependencies:
                     38:  */
                     39: 
                     40: #include <mach/std_types.h>    /* For pointer_t */
                     41: #include <mach/mach_types.h>
                     42: 
                     43: #include <mach/kern_return.h>
1.1.1.3   root       44: #include <vm/vm_map.h>
1.1       root       45: #include <vm/vm_object.h>
                     46: #include <mach/memory_object.h>
                     47: #include <mach/boolean.h>
                     48: #include <mach/vm_prot.h>
                     49: #include <mach/message.h>
                     50: 
1.1.1.3   root       51: #include <vm/memory_object_user.user.h>
                     52: #include <vm/memory_object_default.user.h>
1.1       root       53: 
                     54: /*
                     55:  *     Implementation dependencies:
                     56:  */
                     57: #include <vm/memory_object.h>
                     58: #include <vm/vm_page.h>
                     59: #include <vm/vm_pageout.h>
                     60: #include <vm/pmap.h>           /* For copy_to_phys, pmap_clear_modify */
1.1.1.3   root       61: #include <kern/debug.h>                /* For panic() */
1.1       root       62: #include <kern/thread.h>               /* For current_thread() */
                     63: #include <kern/host.h>
                     64: #include <vm/vm_kern.h>                /* For kernel_map, vm_move */
                     65: #include <vm/vm_map.h>         /* For vm_map_pageable */
                     66: #include <ipc/ipc_port.h>
                     67: 
                     68: #if    MACH_PAGEMAP
1.1.1.2   root       69: #include <vm/vm_external.h>
                     70: #endif /* MACH_PAGEMAP */
1.1       root       71: 
                     72: typedef        int             memory_object_lock_result_t; /* moved from below */
                     73: 
                     74: 
                     75: ipc_port_t     memory_manager_default = IP_NULL;
                     76: decl_simple_lock_data(,memory_manager_default_lock)
                     77: 
                     78: /*
                     79:  *     Important note:
                     80:  *             All of these routines gain a reference to the
                     81:  *             object (first argument) as part of the automatic
                     82:  *             argument conversion. Explicit deallocation is necessary.
                     83:  */
                     84: 
1.1.1.4   root       85: kern_return_t memory_object_data_supply(
                     86:        vm_object_t             object,
                     87:        vm_offset_t             offset,
                     88:        vm_map_copy_t           data_copy,
                     89:        unsigned int            data_cnt,
                     90:        vm_prot_t               lock_value,
                     91:        boolean_t               precious,
                     92:        ipc_port_t              reply_to,
                     93:        mach_msg_type_name_t    reply_to_type)
1.1       root       94: {
                     95:        kern_return_t   result = KERN_SUCCESS;
                     96:        vm_offset_t     error_offset = 0;
                     97:        vm_page_t       m;
                     98:        vm_page_t       data_m;
                     99:        vm_size_t       original_length;
                    100:        vm_offset_t     original_offset;
                    101:        vm_page_t       *page_list;
                    102:        boolean_t       was_absent;
                    103:        vm_map_copy_t   orig_copy = data_copy;
                    104: 
                    105:        /*
                    106:         *      Look for bogus arguments
                    107:         */
                    108: 
                    109:        if (object == VM_OBJECT_NULL) {
                    110:                return(KERN_INVALID_ARGUMENT);
                    111:        }
                    112: 
                    113:        if (lock_value & ~VM_PROT_ALL) {
                    114:                vm_object_deallocate(object);
                    115:                return(KERN_INVALID_ARGUMENT);
                    116:        }
                    117: 
                    118:        if ((data_cnt % PAGE_SIZE) != 0) {
                    119:            vm_object_deallocate(object);
                    120:            return(KERN_INVALID_ARGUMENT);
                    121:        }
                    122: 
                    123:        /*
                    124:         *      Adjust the offset from the memory object to the offset
                    125:         *      within the vm_object.
                    126:         */
                    127: 
                    128:        original_length = data_cnt;
                    129:        original_offset = offset;
                    130: 
                    131:        assert(data_copy->type == VM_MAP_COPY_PAGE_LIST);
                    132:        page_list = &data_copy->cpy_page_list[0];
                    133: 
                    134:        vm_object_lock(object);
                    135:        vm_object_paging_begin(object);
                    136:        offset -= object->paging_offset;
                    137: 
                    138:        /*
                    139:         *      Loop over copy stealing pages for pagein.
                    140:         */
                    141: 
                    142:        for (; data_cnt > 0 ; data_cnt -= PAGE_SIZE, offset += PAGE_SIZE) {
                    143: 
                    144:                assert(data_copy->cpy_npages > 0);
                    145:                data_m = *page_list;
                    146: 
                    147:                if (data_m == VM_PAGE_NULL || data_m->tabled ||
                    148:                    data_m->error || data_m->absent || data_m->fictitious) {
                    149: 
                    150:                        panic("Data_supply: bad page");
                    151:                      }
                    152: 
                    153:                /*
                    154:                 *      Look up target page and check its state.
                    155:                 */
                    156: 
                    157: retry_lookup:
                    158:                m = vm_page_lookup(object,offset);
                    159:                if (m == VM_PAGE_NULL) {
                    160:                    was_absent = FALSE;
                    161:                }
                    162:                else {
                    163:                    if (m->absent && m->busy) {
                    164: 
                    165:                        /*
                    166:                         *      Page was requested.  Free the busy
                    167:                         *      page waiting for it.  Insertion
                    168:                         *      of new page happens below.
                    169:                         */
                    170: 
                    171:                        VM_PAGE_FREE(m);
                    172:                        was_absent = TRUE;
                    173:                    }
                    174:                    else {
                    175: 
                    176:                        /*
                    177:                         *      Have to wait for page that is busy and
                    178:                         *      not absent.  This is probably going to
                    179:                         *      be an error, but go back and check.
                    180:                         */
                    181:                        if (m->busy) {
                    182:                                PAGE_ASSERT_WAIT(m, FALSE);
                    183:                                vm_object_unlock(object);
                    184:                                thread_block((void (*)()) 0);
                    185:                                vm_object_lock(object);
                    186:                                goto retry_lookup;
                    187:                        }
                    188: 
                    189:                        /*
                    190:                         *      Page already present; error.
                    191:                         *      This is an error if data is precious.
                    192:                         */
                    193:                        result = KERN_MEMORY_PRESENT;
                    194:                        error_offset = offset + object->paging_offset;
                    195: 
                    196:                        break;
                    197:                    }
                    198:                }
                    199: 
                    200:                /*
                    201:                 *      Ok to pagein page.  Target object now has no page
                    202:                 *      at offset.  Set the page parameters, then drop
                    203:                 *      in new page and set up pageout state.  Object is
                    204:                 *      still locked here.
                    205:                 *
                    206:                 *      Must clear busy bit in page before inserting it.
                    207:                 *      Ok to skip wakeup logic because nobody else
                    208:                 *      can possibly know about this page.
                    209:                 */
                    210: 
                    211:                data_m->busy = FALSE;
                    212:                data_m->dirty = FALSE;
                    213:                pmap_clear_modify(data_m->phys_addr);
                    214: 
                    215:                data_m->page_lock = lock_value;
                    216:                data_m->unlock_request = VM_PROT_NONE;
                    217:                data_m->precious = precious;
                    218: 
                    219:                vm_page_lock_queues();
                    220:                vm_page_insert(data_m, object, offset);
                    221: 
                    222:                if (was_absent)
                    223:                        vm_page_activate(data_m);
                    224:                else
                    225:                        vm_page_deactivate(data_m);
                    226: 
                    227:                vm_page_unlock_queues();
                    228: 
                    229:                /*
                    230:                 *      Null out this page list entry, and advance to next
                    231:                 *      page.
                    232:                 */
                    233: 
                    234:                *page_list++ = VM_PAGE_NULL;
                    235: 
                    236:                if (--(data_copy->cpy_npages) == 0 &&
                    237:                    vm_map_copy_has_cont(data_copy)) {
                    238:                        vm_map_copy_t   new_copy;
                    239: 
                    240:                        vm_object_unlock(object);
                    241: 
                    242:                        vm_map_copy_invoke_cont(data_copy, &new_copy, &result);
                    243: 
                    244:                        if (result == KERN_SUCCESS) {
                    245: 
                    246:                            /*
                    247:                             *  Consume on success requires that
                    248:                             *  we keep the original vm_map_copy
                    249:                             *  around in case something fails.
                    250:                             *  Free the old copy if it's not the original
                    251:                             */
                    252:                            if (data_copy != orig_copy) {
                    253:                                vm_map_copy_discard(data_copy);
                    254:                            }
                    255: 
                    256:                            if ((data_copy = new_copy) != VM_MAP_COPY_NULL)
                    257:                                page_list = &data_copy->cpy_page_list[0];
                    258: 
                    259:                            vm_object_lock(object);
                    260:                        }
                    261:                        else {
                    262:                            vm_object_lock(object);
                    263:                            error_offset = offset + object->paging_offset +
                    264:                                                PAGE_SIZE;
                    265:                            break;
                    266:                        }
                    267:                }
                    268:        }
                    269: 
                    270:        /*
                    271:         *      Send reply if one was requested.
                    272:         */
                    273:        vm_object_paging_end(object);
                    274:        vm_object_unlock(object);
                    275: 
                    276:        if (vm_map_copy_has_cont(data_copy))
                    277:                vm_map_copy_abort_cont(data_copy);
                    278: 
                    279:        if (IP_VALID(reply_to)) {
                    280:                memory_object_supply_completed(
                    281:                                reply_to, reply_to_type,
                    282:                                object->pager_request,
                    283:                                original_offset,
                    284:                                original_length,
                    285:                                result,
                    286:                                error_offset);
                    287:        }
                    288: 
                    289:        vm_object_deallocate(object);
                    290: 
                    291:        /*
                    292:         *      Consume on success:  The final data copy must be
                    293:         *      be discarded if it is not the original.  The original
                    294:         *      gets discarded only if this routine succeeds.
                    295:         */
                    296:        if (data_copy != orig_copy)
                    297:                vm_map_copy_discard(data_copy);
                    298:        if (result == KERN_SUCCESS)
                    299:                vm_map_copy_discard(orig_copy);
                    300: 
                    301: 
                    302:        return(result);
                    303: }
                    304: 
1.1.1.4   root      305: kern_return_t memory_object_data_error(
                    306:        vm_object_t     object,
                    307:        vm_offset_t     offset,
                    308:        vm_size_t       size,
                    309:        kern_return_t   error_value)
1.1       root      310: {
                    311:        if (object == VM_OBJECT_NULL)
                    312:                return(KERN_INVALID_ARGUMENT);
                    313: 
                    314:        if (size != round_page(size))
                    315:                return(KERN_INVALID_ARGUMENT);
                    316: 
                    317:        vm_object_lock(object);
                    318:        offset -= object->paging_offset;
                    319: 
                    320:        while (size != 0) {
1.1.1.4   root      321:                vm_page_t m;
1.1       root      322: 
                    323:                m = vm_page_lookup(object, offset);
                    324:                if ((m != VM_PAGE_NULL) && m->busy && m->absent) {
                    325:                        m->error = TRUE;
                    326:                        m->absent = FALSE;
                    327:                        vm_object_absent_release(object);
                    328: 
                    329:                        PAGE_WAKEUP_DONE(m);
                    330: 
                    331:                        vm_page_lock_queues();
                    332:                        vm_page_activate(m);
                    333:                        vm_page_unlock_queues();
                    334:                }
                    335: 
                    336:                size -= PAGE_SIZE;
                    337:                offset += PAGE_SIZE;
                    338:         }
                    339:        vm_object_unlock(object);
                    340: 
                    341:        vm_object_deallocate(object);
                    342:        return(KERN_SUCCESS);
                    343: }
                    344: 
1.1.1.4   root      345: kern_return_t memory_object_data_unavailable(
                    346:        vm_object_t     object,
                    347:        vm_offset_t     offset,
                    348:        vm_size_t       size)
1.1       root      349: {
                    350: #if    MACH_PAGEMAP
                    351:        vm_external_t   existence_info = VM_EXTERNAL_NULL;
1.1.1.2   root      352: #endif /* MACH_PAGEMAP */
1.1       root      353: 
                    354:        if (object == VM_OBJECT_NULL)
                    355:                return(KERN_INVALID_ARGUMENT);
                    356: 
                    357:        if (size != round_page(size))
                    358:                return(KERN_INVALID_ARGUMENT);
                    359: 
                    360: #if    MACH_PAGEMAP
1.1.1.2   root      361:        if ((offset == 0) && (size > VM_EXTERNAL_LARGE_SIZE) &&
1.1       root      362:            (object->existence_info == VM_EXTERNAL_NULL)) {
                    363:                existence_info = vm_external_create(VM_EXTERNAL_SMALL_SIZE);
                    364:        }
1.1.1.2   root      365: #endif /* MACH_PAGEMAP */
1.1       root      366: 
                    367:        vm_object_lock(object);
                    368: #if    MACH_PAGEMAP
                    369:        if (existence_info != VM_EXTERNAL_NULL) {
                    370:                object->existence_info = existence_info;
                    371:        }
                    372:        if ((offset == 0) && (size > VM_EXTERNAL_LARGE_SIZE)) {
                    373:                vm_object_unlock(object);
                    374:                vm_object_deallocate(object);
                    375:                return(KERN_SUCCESS);
                    376:        }
1.1.1.2   root      377: #endif /* MACH_PAGEMAP */
1.1       root      378:        offset -= object->paging_offset;
                    379: 
                    380:        while (size != 0) {
1.1.1.4   root      381:                vm_page_t m;
1.1       root      382: 
                    383:                /*
                    384:                 *      We're looking for pages that are both busy and
                    385:                 *      absent (waiting to be filled), converting them
                    386:                 *      to just absent.
                    387:                 *
                    388:                 *      Pages that are just busy can be ignored entirely.
                    389:                 */
                    390: 
                    391:                m = vm_page_lookup(object, offset);
                    392:                if ((m != VM_PAGE_NULL) && m->busy && m->absent) {
                    393:                        PAGE_WAKEUP_DONE(m);
                    394: 
                    395:                        vm_page_lock_queues();
                    396:                        vm_page_activate(m);
                    397:                        vm_page_unlock_queues();
                    398:                }
                    399:                size -= PAGE_SIZE;
                    400:                offset += PAGE_SIZE;
                    401:        }
                    402: 
                    403:        vm_object_unlock(object);
                    404: 
                    405:        vm_object_deallocate(object);
                    406:        return(KERN_SUCCESS);
                    407: }
                    408: 
                    409: /*
                    410:  *     Routine:        memory_object_lock_page
                    411:  *
                    412:  *     Description:
                    413:  *             Perform the appropriate lock operations on the
                    414:  *             given page.  See the description of
                    415:  *             "memory_object_lock_request" for the meanings
                    416:  *             of the arguments.
                    417:  *
                    418:  *             Returns an indication that the operation
                    419:  *             completed, blocked, or that the page must
                    420:  *             be cleaned.
                    421:  */
                    422: 
                    423: #define        MEMORY_OBJECT_LOCK_RESULT_DONE          0
                    424: #define        MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK    1
                    425: #define        MEMORY_OBJECT_LOCK_RESULT_MUST_CLEAN    2
                    426: #define        MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN   3
                    427: 
1.1.1.4   root      428: memory_object_lock_result_t memory_object_lock_page(
                    429:        vm_page_t               m,
                    430:        memory_object_return_t  should_return,
                    431:        boolean_t               should_flush,
                    432:        vm_prot_t               prot)
1.1       root      433: {
                    434:        /*
                    435:         *      Don't worry about pages for which the kernel
                    436:         *      does not have any data.
                    437:         */
                    438: 
                    439:        if (m->absent)
                    440:                return(MEMORY_OBJECT_LOCK_RESULT_DONE);
                    441: 
                    442:        /*
                    443:         *      If we cannot change access to the page,
                    444:         *      either because a mapping is in progress
                    445:         *      (busy page) or because a mapping has been
                    446:         *      wired, then give up.
                    447:         */
                    448: 
                    449:        if (m->busy)
                    450:                return(MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK);
                    451: 
                    452:        assert(!m->fictitious);
                    453: 
                    454:        if (m->wire_count != 0) {
                    455:                /*
                    456:                 *      If no change would take place
                    457:                 *      anyway, return successfully.
                    458:                 *
                    459:                 *      No change means:
                    460:                 *              Not flushing AND
                    461:                 *              No change to page lock [2 checks]  AND
                    462:                 *              Don't need to send page to manager
                    463:                 *
                    464:                 *      Don't need to send page to manager means:
                    465:                 *              No clean or return request OR (
                    466:                 *                  Page is not dirty [2 checks] AND (
                    467:                 *                      Page is not precious OR
                    468:                 *                      No request to return precious pages ))
1.1.1.2   root      469:                 *
1.1       root      470:                 *      Now isn't that straightforward and obvious ?? ;-)
                    471:                 *
                    472:                 * XXX  This doesn't handle sending a copy of a wired
                    473:                 * XXX  page to the pager, but that will require some
                    474:                 * XXX  significant surgery.
                    475:                 */
                    476: 
                    477:                if (!should_flush &&
                    478:                    ((m->page_lock == prot) || (prot == VM_PROT_NO_CHANGE)) &&
                    479:                    ((should_return == MEMORY_OBJECT_RETURN_NONE) ||
                    480:                     (!m->dirty && !pmap_is_modified(m->phys_addr) &&
                    481:                      (!m->precious ||
                    482:                       should_return != MEMORY_OBJECT_RETURN_ALL)))) {
                    483:                        /*
                    484:                         *      Restart page unlock requests,
                    485:                         *      even though no change took place.
                    486:                         *      [Memory managers may be expecting
                    487:                         *      to see new requests.]
                    488:                         */
                    489:                        m->unlock_request = VM_PROT_NONE;
                    490:                        PAGE_WAKEUP(m);
                    491: 
                    492:                        return(MEMORY_OBJECT_LOCK_RESULT_DONE);
                    493:                }
                    494: 
                    495:                return(MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK);
                    496:        }
                    497: 
                    498:        /*
                    499:         *      If the page is to be flushed, allow
                    500:         *      that to be done as part of the protection.
                    501:         */
                    502: 
                    503:        if (should_flush)
                    504:                prot = VM_PROT_ALL;
                    505: 
                    506:        /*
                    507:         *      Set the page lock.
                    508:         *
                    509:         *      If we are decreasing permission, do it now;
                    510:         *      let the fault handler take care of increases
                    511:         *      (pmap_page_protect may not increase protection).
                    512:         */
                    513: 
                    514:        if (prot != VM_PROT_NO_CHANGE) {
                    515:                if ((m->page_lock ^ prot) & prot) {
                    516:                        pmap_page_protect(m->phys_addr, VM_PROT_ALL & ~prot);
                    517:                }
                    518:                m->page_lock = prot;
                    519: 
                    520:                /*
                    521:                 *      Restart any past unlock requests, even if no
                    522:                 *      change resulted.  If the manager explicitly
                    523:                 *      requested no protection change, then it is assumed
                    524:                 *      to be remembering past requests.
                    525:                 */
                    526: 
                    527:                m->unlock_request = VM_PROT_NONE;
                    528:                PAGE_WAKEUP(m);
                    529:        }
                    530: 
                    531:        /*
                    532:         *      Handle cleaning.
                    533:         */
                    534: 
                    535:        if (should_return != MEMORY_OBJECT_RETURN_NONE) {
                    536:                /*
                    537:                 *      Check whether the page is dirty.  If
                    538:                 *      write permission has not been removed,
                    539:                 *      this may have unpredictable results.
                    540:                 */
                    541: 
                    542:                if (!m->dirty)
                    543:                        m->dirty = pmap_is_modified(m->phys_addr);
                    544: 
                    545:                if (m->dirty || (m->precious &&
                    546:                                 should_return == MEMORY_OBJECT_RETURN_ALL)) {
                    547:                        /*
                    548:                         *      If we weren't planning
                    549:                         *      to flush the page anyway,
                    550:                         *      we may need to remove the
                    551:                         *      page from the pageout
                    552:                         *      system and from physical
                    553:                         *      maps now.
                    554:                         */
                    555: 
                    556:                        vm_page_lock_queues();
                    557:                        VM_PAGE_QUEUES_REMOVE(m);
                    558:                        vm_page_unlock_queues();
                    559: 
                    560:                        if (!should_flush)
                    561:                                pmap_page_protect(m->phys_addr,
                    562:                                                VM_PROT_NONE);
                    563: 
                    564:                        /*
                    565:                         *      Cleaning a page will cause
                    566:                         *      it to be flushed.
                    567:                         */
                    568: 
                    569:                        if (m->dirty)
                    570:                                return(MEMORY_OBJECT_LOCK_RESULT_MUST_CLEAN);
                    571:                        else
                    572:                                return(MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN);
                    573:                }
                    574:        }
                    575: 
                    576:        /*
                    577:         *      Handle flushing
                    578:         */
                    579: 
                    580:        if (should_flush) {
                    581:                VM_PAGE_FREE(m);
                    582:        } else {
                    583:                extern boolean_t vm_page_deactivate_hint;
                    584: 
                    585:                /*
                    586:                 *      XXX Make clean but not flush a paging hint,
                    587:                 *      and deactivate the pages.  This is a hack
                    588:                 *      because it overloads flush/clean with
                    589:                 *      implementation-dependent meaning.  This only
                    590:                 *      happens to pages that are already clean.
                    591:                 */
                    592: 
                    593:                if (vm_page_deactivate_hint &&
                    594:                    (should_return != MEMORY_OBJECT_RETURN_NONE)) {
                    595:                        vm_page_lock_queues();
                    596:                        vm_page_deactivate(m);
                    597:                        vm_page_unlock_queues();
                    598:                }
                    599:        }
                    600: 
                    601:        return(MEMORY_OBJECT_LOCK_RESULT_DONE);
                    602: }
                    603: 
                    604: /*
                    605:  *     Routine:        memory_object_lock_request [user interface]
                    606:  *
                    607:  *     Description:
                    608:  *             Control use of the data associated with the given
                    609:  *             memory object.  For each page in the given range,
                    610:  *             perform the following operations, in order:
                    611:  *                     1)  restrict access to the page (disallow
                    612:  *                         forms specified by "prot");
                    613:  *                     2)  return data to the manager (if "should_return"
                    614:  *                         is RETURN_DIRTY and the page is dirty, or
                    615:  *                         "should_return" is RETURN_ALL and the page
                    616:  *                         is either dirty or precious); and,
                    617:  *                     3)  flush the cached copy (if "should_flush"
                    618:  *                         is asserted).
                    619:  *             The set of pages is defined by a starting offset
                    620:  *             ("offset") and size ("size").  Only pages with the
                    621:  *             same page alignment as the starting offset are
                    622:  *             considered.
                    623:  *
                    624:  *             A single acknowledgement is sent (to the "reply_to"
                    625:  *             port) when these actions are complete.  If successful,
                    626:  *             the naked send right for reply_to is consumed.
                    627:  */
                    628: 
                    629: kern_return_t
1.1.1.4   root      630: memory_object_lock_request(
                    631:        vm_object_t             object,
                    632:        vm_offset_t             offset,
                    633:        vm_size_t               size,
                    634:        memory_object_return_t  should_return,
                    635:        boolean_t               should_flush,
                    636:        vm_prot_t               prot,
                    637:        ipc_port_t              reply_to,
                    638:        mach_msg_type_name_t    reply_to_type)
1.1       root      639: {
1.1.1.4   root      640:        vm_page_t               m;
1.1       root      641:        vm_offset_t             original_offset = offset;
                    642:        vm_size_t               original_size = size;
                    643:        vm_offset_t             paging_offset = 0;
                    644:        vm_object_t             new_object = VM_OBJECT_NULL;
                    645:        vm_offset_t             new_offset = 0;
                    646:        vm_offset_t             last_offset = offset;
                    647:        int                     page_lock_result;
                    648:        int                     pageout_action = 0; /* '=0' to quiet lint */
                    649: 
                    650: #define        DATA_WRITE_MAX  32
                    651:        vm_page_t               holding_pages[DATA_WRITE_MAX];
                    652: 
                    653:        /*
                    654:         *      Check for bogus arguments.
                    655:         */
                    656:        if (object == VM_OBJECT_NULL ||
                    657:                ((prot & ~VM_PROT_ALL) != 0 && prot != VM_PROT_NO_CHANGE))
                    658:            return (KERN_INVALID_ARGUMENT);
                    659: 
                    660:        size = round_page(size);
                    661: 
                    662:        /*
                    663:         *      Lock the object, and acquire a paging reference to
                    664:         *      prevent the memory_object and control ports from
                    665:         *      being destroyed.
                    666:         */
                    667: 
                    668:        vm_object_lock(object);
                    669:        vm_object_paging_begin(object);
                    670:        offset -= object->paging_offset;
                    671: 
                    672:        /*
                    673:         *      To avoid blocking while scanning for pages, save
                    674:         *      dirty pages to be cleaned all at once.
                    675:         *
                    676:         *      XXXO A similar strategy could be used to limit the
                    677:         *      number of times that a scan must be restarted for
                    678:         *      other reasons.  Those pages that would require blocking
                    679:         *      could be temporarily collected in another list, or
                    680:         *      their offsets could be recorded in a small array.
                    681:         */
                    682: 
                    683:        /*
                    684:         * XXX  NOTE: May want to consider converting this to a page list
                    685:         * XXX  vm_map_copy interface.  Need to understand object
                    686:         * XXX  coalescing implications before doing so.
                    687:         */
                    688: 
                    689: #define        PAGEOUT_PAGES                                                   \
                    690: MACRO_BEGIN                                                            \
                    691:        vm_map_copy_t           copy;                                   \
1.1.1.5   root      692:        unsigned                i;                                      \
1.1.1.4   root      693:        vm_page_t               hp;                                     \
1.1       root      694:                                                                        \
                    695:        vm_object_unlock(object);                                       \
                    696:                                                                        \
                    697:        (void) vm_map_copyin_object(new_object, 0, new_offset, &copy);  \
                    698:                                                                        \
1.1.1.6 ! root      699:        (void) memory_object_data_return(                               \
        !           700:                object->pager,                                          \
        !           701:                object->pager_request,                                  \
        !           702:                paging_offset,                                          \
        !           703:                (pointer_t) copy,                                       \
        !           704:                new_offset,                                             \
1.1       root      705:             (pageout_action == MEMORY_OBJECT_LOCK_RESULT_MUST_CLEAN),  \
1.1.1.6 ! root      706:                !should_flush);                                         \
1.1       root      707:                                                                        \
                    708:        vm_object_lock(object);                                         \
                    709:                                                                        \
                    710:        for (i = 0; i < atop(new_offset); i++) {                        \
                    711:            hp = holding_pages[i];                                      \
                    712:            if (hp != VM_PAGE_NULL)                                     \
                    713:                VM_PAGE_FREE(hp);                                       \
                    714:        }                                                               \
                    715:                                                                        \
                    716:        new_object = VM_OBJECT_NULL;                                    \
                    717: MACRO_END
                    718: 
                    719:        for (;
                    720:             size != 0;
                    721:             size -= PAGE_SIZE, offset += PAGE_SIZE)
                    722:        {
                    723:            /*
                    724:             *  Limit the number of pages to be cleaned at once.
                    725:             */
                    726:            if (new_object != VM_OBJECT_NULL &&
                    727:                    new_offset >= PAGE_SIZE * DATA_WRITE_MAX)
                    728:            {
                    729:                PAGEOUT_PAGES;
                    730:            }
                    731: 
                    732:            while ((m = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
                    733:                switch ((page_lock_result = memory_object_lock_page(m,
                    734:                                        should_return,
                    735:                                        should_flush,
                    736:                                        prot)))
                    737:                {
                    738:                    case MEMORY_OBJECT_LOCK_RESULT_DONE:
                    739:                        /*
                    740:                         *      End of a cluster of dirty pages.
                    741:                         */
                    742:                        if (new_object != VM_OBJECT_NULL) {
                    743:                            PAGEOUT_PAGES;
                    744:                            continue;
                    745:                        }
                    746:                        break;
                    747: 
                    748:                    case MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK:
                    749:                        /*
                    750:                         *      Since it is necessary to block,
                    751:                         *      clean any dirty pages now.
                    752:                         */
                    753:                        if (new_object != VM_OBJECT_NULL) {
                    754:                            PAGEOUT_PAGES;
                    755:                            continue;
                    756:                        }
                    757: 
                    758:                        PAGE_ASSERT_WAIT(m, FALSE);
                    759:                        vm_object_unlock(object);
                    760:                        thread_block((void (*)()) 0);
                    761:                        vm_object_lock(object);
                    762:                        continue;
                    763: 
                    764:                    case MEMORY_OBJECT_LOCK_RESULT_MUST_CLEAN:
                    765:                    case MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN:
                    766:                        /*
                    767:                         * The clean and return cases are similar.
                    768:                         *
                    769:                         * Mark the page busy since we unlock the
                    770:                         * object below.
                    771:                         */
                    772:                        m->busy = TRUE;
                    773: 
                    774:                        /*
                    775:                         * if this would form a discontiguous block,
                    776:                         * clean the old pages and start anew.
                    777:                         *
                    778:                         * NOTE: The first time through here, new_object
                    779:                         * is null, hiding the fact that pageout_action
                    780:                         * is not initialized.
                    781:                         */
                    782:                        if (new_object != VM_OBJECT_NULL &&
                    783:                            (last_offset != offset ||
                    784:                             pageout_action != page_lock_result)) {
                    785:                                PAGEOUT_PAGES;
                    786:                        }
                    787: 
                    788:                        vm_object_unlock(object);
                    789: 
                    790:                        /*
                    791:                         *      If we have not already allocated an object
                    792:                         *      for a range of pages to be written, do so
                    793:                         *      now.
                    794:                         */
                    795:                        if (new_object == VM_OBJECT_NULL) {
                    796:                            new_object = vm_object_allocate(original_size);
                    797:                            new_offset = 0;
                    798:                            paging_offset = m->offset +
                    799:                                        object->paging_offset;
                    800:                            pageout_action = page_lock_result;
                    801:                        }
                    802: 
                    803:                        /*
                    804:                         *      Move or copy the dirty page into the
                    805:                         *      new object.
                    806:                         */
                    807:                        m = vm_pageout_setup(m,
                    808:                                        m->offset + object->paging_offset,
                    809:                                        new_object,
                    810:                                        new_offset,
                    811:                                        should_flush);
                    812: 
                    813:                        /*
                    814:                         *      Save the holding page if there is one.
                    815:                         */
                    816:                        holding_pages[atop(new_offset)] = m;
                    817:                        new_offset += PAGE_SIZE;
                    818:                        last_offset = offset + PAGE_SIZE;
                    819: 
                    820:                        vm_object_lock(object);
                    821:                        break;
                    822:                }
                    823:                break;
                    824:            }
                    825:        }
                    826: 
                    827:        /*
                    828:         *      We have completed the scan for applicable pages.
                    829:         *      Clean any pages that have been saved.
                    830:         */
                    831:        if (new_object != VM_OBJECT_NULL) {
                    832:            PAGEOUT_PAGES;
                    833:        }
                    834: 
                    835:        if (IP_VALID(reply_to)) {
                    836:                vm_object_unlock(object);
                    837: 
                    838:                /* consumes our naked send-once/send right for reply_to */
                    839:                (void) memory_object_lock_completed(reply_to, reply_to_type,
                    840:                        object->pager_request, original_offset, original_size);
                    841: 
                    842:                vm_object_lock(object);
                    843:        }
                    844: 
                    845:        vm_object_paging_end(object);
                    846:        vm_object_unlock(object);
                    847:        vm_object_deallocate(object);
                    848: 
                    849:        return (KERN_SUCCESS);
                    850: }
                    851: 
1.1.1.6 ! root      852: static kern_return_t
1.1.1.4   root      853: memory_object_set_attributes_common(
                    854:        vm_object_t     object,
                    855:        boolean_t       may_cache,
1.1.1.6 ! root      856:        memory_object_copy_strategy_t copy_strategy)
1.1       root      857: {
                    858:        if (object == VM_OBJECT_NULL)
                    859:                return(KERN_INVALID_ARGUMENT);
                    860: 
                    861:        /*
                    862:         *      Verify the attributes of importance
                    863:         */
                    864: 
                    865:        switch(copy_strategy) {
                    866:                case MEMORY_OBJECT_COPY_NONE:
                    867:                case MEMORY_OBJECT_COPY_CALL:
                    868:                case MEMORY_OBJECT_COPY_DELAY:
                    869:                case MEMORY_OBJECT_COPY_TEMPORARY:
                    870:                        break;
                    871:                default:
                    872:                        vm_object_deallocate(object);
                    873:                        return(KERN_INVALID_ARGUMENT);
                    874:        }
                    875: 
                    876:        if (may_cache)
                    877:                may_cache = TRUE;
                    878: 
                    879:        vm_object_lock(object);
                    880: 
                    881:        /*
                    882:         *      Wake up anyone waiting for the ready attribute
                    883:         *      to become asserted.
                    884:         */
                    885: 
1.1.1.6 ! root      886:        if (!object->pager_ready) {
1.1       root      887:                vm_object_wakeup(object, VM_OBJECT_EVENT_PAGER_READY);
                    888:        }
                    889: 
                    890:        /*
                    891:         *      Copy the attributes
                    892:         */
                    893: 
                    894:        object->can_persist = may_cache;
1.1.1.6 ! root      895:        object->pager_ready = TRUE;
1.1       root      896:        if (copy_strategy == MEMORY_OBJECT_COPY_TEMPORARY) {
                    897:                object->temporary = TRUE;
                    898:        } else {
                    899:                object->copy_strategy = copy_strategy;
                    900:        }
                    901: 
                    902:        vm_object_unlock(object);
                    903: 
                    904:        vm_object_deallocate(object);
                    905: 
                    906:        return(KERN_SUCCESS);
                    907: }
                    908: 
                    909: /*
                    910:  * XXX rpd claims that reply_to could be obviated in favor of a client
                    911:  * XXX stub that made change_attributes an RPC.  Need investigation.
                    912:  */
                    913: 
1.1.1.4   root      914: kern_return_t  memory_object_change_attributes(
                    915:        vm_object_t             object,
                    916:        boolean_t               may_cache,
                    917:        memory_object_copy_strategy_t copy_strategy,
                    918:        ipc_port_t              reply_to,
                    919:        mach_msg_type_name_t    reply_to_type)
1.1       root      920: {
                    921:        kern_return_t   result;
                    922: 
                    923:        /*
                    924:         *      Do the work and throw away our object reference.  It
                    925:         *      is important that the object reference be deallocated
                    926:         *      BEFORE sending the reply.  The whole point of the reply
                    927:         *      is that it shows up after the terminate message that
                    928:         *      may be generated by setting the object uncacheable.
                    929:         *
                    930:         * XXX  may_cache may become a tri-valued variable to handle
                    931:         * XXX  uncache if not in use.
                    932:         */
1.1.1.6 ! root      933:        result = memory_object_set_attributes_common(object, may_cache,
        !           934:                                                     copy_strategy);
1.1       root      935: 
                    936:        if (IP_VALID(reply_to)) {
                    937: 
                    938:                /* consumes our naked send-once/send right for reply_to */
                    939:                (void) memory_object_change_completed(reply_to, reply_to_type,
                    940:                        may_cache, copy_strategy);
                    941: 
                    942:        }
                    943: 
                    944:        return(result);
                    945: }
                    946: 
1.1.1.4   root      947: kern_return_t  memory_object_ready(
                    948:        vm_object_t     object,
                    949:        boolean_t       may_cache,
                    950:        memory_object_copy_strategy_t copy_strategy)
1.1       root      951: {
1.1.1.6 ! root      952:        return memory_object_set_attributes_common(object, may_cache,
        !           953:                                                   copy_strategy);
1.1       root      954: }
                    955: 
1.1.1.4   root      956: kern_return_t  memory_object_get_attributes(
                    957:        vm_object_t     object,
                    958:        boolean_t       *object_ready,
                    959:        boolean_t       *may_cache,
                    960:        memory_object_copy_strategy_t *copy_strategy)
1.1       root      961: {
                    962:        if (object == VM_OBJECT_NULL)
                    963:                return(KERN_INVALID_ARGUMENT);
                    964: 
                    965:        vm_object_lock(object);
                    966:        *may_cache = object->can_persist;
                    967:        *object_ready = object->pager_ready;
                    968:        *copy_strategy = object->copy_strategy;
                    969:        vm_object_unlock(object);
                    970: 
                    971:        vm_object_deallocate(object);
                    972: 
                    973:        return(KERN_SUCCESS);
                    974: }
                    975: 
                    976: /*
                    977:  *     If successful, consumes the supplied naked send right.
                    978:  */
                    979: kern_return_t  vm_set_default_memory_manager(host, default_manager)
1.1.1.4   root      980:        const host_t    host;
1.1       root      981:        ipc_port_t      *default_manager;
                    982: {
                    983:        ipc_port_t current_manager;
                    984:        ipc_port_t new_manager;
                    985:        ipc_port_t returned_manager;
                    986: 
                    987:        if (host == HOST_NULL)
                    988:                return(KERN_INVALID_HOST);
                    989: 
                    990:        new_manager = *default_manager;
                    991:        simple_lock(&memory_manager_default_lock);
                    992:        current_manager = memory_manager_default;
                    993: 
                    994:        if (new_manager == IP_NULL) {
                    995:                /*
                    996:                 *      Retrieve the current value.
                    997:                 */
                    998: 
                    999:                returned_manager = ipc_port_copy_send(current_manager);
                   1000:        } else {
                   1001:                /*
                   1002:                 *      Retrieve the current value,
                   1003:                 *      and replace it with the supplied value.
                   1004:                 *      We consume the supplied naked send right.
                   1005:                 */
                   1006: 
                   1007:                returned_manager = current_manager;
                   1008:                memory_manager_default = new_manager;
                   1009: 
                   1010:                /*
                   1011:                 *      In case anyone's been waiting for a memory
                   1012:                 *      manager to be established, wake them up.
                   1013:                 */
                   1014: 
                   1015:                thread_wakeup((event_t) &memory_manager_default);
                   1016:        }
                   1017: 
                   1018:        simple_unlock(&memory_manager_default_lock);
                   1019: 
                   1020:        *default_manager = returned_manager;
                   1021:        return(KERN_SUCCESS);
                   1022: }
                   1023: 
                   1024: /*
                   1025:  *     Routine:        memory_manager_default_reference
                   1026:  *     Purpose:
                   1027:  *             Returns a naked send right for the default
                   1028:  *             memory manager.  The returned right is always
                   1029:  *             valid (not IP_NULL or IP_DEAD).
                   1030:  */
                   1031: 
1.1.1.3   root     1032: ipc_port_t     memory_manager_default_reference(void)
1.1       root     1033: {
                   1034:        ipc_port_t current_manager;
                   1035: 
                   1036:        simple_lock(&memory_manager_default_lock);
                   1037: 
                   1038:        while (current_manager = ipc_port_copy_send(memory_manager_default),
                   1039:               !IP_VALID(current_manager)) {
                   1040:                thread_sleep((event_t) &memory_manager_default,
                   1041:                             simple_lock_addr(memory_manager_default_lock),
                   1042:                             FALSE);
                   1043:                simple_lock(&memory_manager_default_lock);
                   1044:        }
                   1045: 
                   1046:        simple_unlock(&memory_manager_default_lock);
                   1047: 
                   1048:        return current_manager;
                   1049: }
                   1050: 
                   1051: /*
                   1052:  *     Routine:        memory_manager_default_port
                   1053:  *     Purpose:
                   1054:  *             Returns true if the receiver for the port
                   1055:  *             is the default memory manager.
                   1056:  *
                   1057:  *             This is a hack to let ds_read_done
                   1058:  *             know when it should keep memory wired.
                   1059:  */
                   1060: 
                   1061: boolean_t      memory_manager_default_port(port)
1.1.1.4   root     1062:        const ipc_port_t port;
1.1       root     1063: {
                   1064:        ipc_port_t current;
                   1065:        boolean_t result;
                   1066: 
                   1067:        simple_lock(&memory_manager_default_lock);
                   1068:        current = memory_manager_default;
                   1069:        if (IP_VALID(current)) {
                   1070:                /*
                   1071:                 *      There is no point in bothering to lock
                   1072:                 *      both ports, which would be painful to do.
                   1073:                 *      If the receive rights are moving around,
                   1074:                 *      we might be inaccurate.
                   1075:                 */
                   1076: 
                   1077:                result = port->ip_receiver == current->ip_receiver;
                   1078:        } else
                   1079:                result = FALSE;
                   1080:        simple_unlock(&memory_manager_default_lock);
                   1081: 
                   1082:        return result;
                   1083: }
                   1084: 
1.1.1.3   root     1085: void           memory_manager_default_init(void)
1.1       root     1086: {
                   1087:        memory_manager_default = IP_NULL;
                   1088:        simple_lock_init(&memory_manager_default_lock);
                   1089: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.