2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * File: vm/memory_object.c
62 * Author: Michael Wayne Young
64 * External memory management interface control functions.
67 #include <advisory_pageout.h>
70 * Interface dependencies:
73 #include <mach/std_types.h> /* For pointer_t */
74 #include <mach/mach_types.h>
77 #include <mach/kern_return.h>
78 #include <mach/memory_object.h>
79 #include <mach/memory_object_default.h>
80 #include <mach/memory_object_control_server.h>
81 #include <mach/host_priv_server.h>
82 #include <mach/boolean.h>
83 #include <mach/vm_prot.h>
84 #include <mach/message.h>
87 * Implementation dependencies:
89 #include <string.h> /* For memcpy() */
92 #include <kern/host.h>
93 #include <kern/thread.h> /* For current_thread() */
94 #include <kern/ipc_mig.h>
95 #include <kern/misc_protos.h>
97 #include <vm/vm_object.h>
98 #include <vm/vm_fault.h>
99 #include <vm/memory_object.h>
100 #include <vm/vm_page.h>
101 #include <vm/vm_pageout.h>
102 #include <vm/pmap.h> /* For pmap_clear_modify */
103 #include <vm/vm_kern.h> /* For kernel_map, vm_move */
104 #include <vm/vm_map.h> /* For vm_map_pageable */
107 #include <vm/vm_external.h>
108 #endif /* MACH_PAGEMAP */
110 #include <vm/vm_protos.h>
113 memory_object_default_t memory_manager_default
= MEMORY_OBJECT_DEFAULT_NULL
;
114 vm_size_t memory_manager_default_cluster
= 0;
115 decl_mutex_data(, memory_manager_default_lock
)
119 * Routine: memory_object_should_return_page
122 * Determine whether the given page should be returned,
123 * based on the page's state and on the given return policy.
125 * We should return the page if one of the following is true:
127 * 1. Page is dirty and should_return is not RETURN_NONE.
128 * 2. Page is precious and should_return is RETURN_ALL.
129 * 3. Should_return is RETURN_ANYTHING.
131 * As a side effect, m->dirty will be made consistent
132 * with pmap_is_modified(m), if should_return is not
133 * MEMORY_OBJECT_RETURN_NONE.
136 #define memory_object_should_return_page(m, should_return) \
137 (should_return != MEMORY_OBJECT_RETURN_NONE && \
138 (((m)->dirty || ((m)->dirty = pmap_is_modified((m)->phys_page))) || \
139 ((m)->precious && (should_return) == MEMORY_OBJECT_RETURN_ALL) || \
140 (should_return) == MEMORY_OBJECT_RETURN_ANYTHING))
142 typedef int memory_object_lock_result_t
;
144 #define MEMORY_OBJECT_LOCK_RESULT_DONE 0
145 #define MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK 1
146 #define MEMORY_OBJECT_LOCK_RESULT_MUST_CLEAN 2
147 #define MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN 3
149 memory_object_lock_result_t
memory_object_lock_page(
151 memory_object_return_t should_return
,
152 boolean_t should_flush
,
156 * Routine: memory_object_lock_page
159 * Perform the appropriate lock operations on the
160 * given page. See the description of
161 * "memory_object_lock_request" for the meanings
164 * Returns an indication that the operation
165 * completed, blocked, or that the page must
168 memory_object_lock_result_t
169 memory_object_lock_page(
171 memory_object_return_t should_return
,
172 boolean_t should_flush
,
175 XPR(XPR_MEMORY_OBJECT
,
176 "m_o_lock_page, page 0x%X rtn %d flush %d prot %d\n",
177 (integer_t
)m
, should_return
, should_flush
, prot
, 0);
180 * If we cannot change access to the page,
181 * either because a mapping is in progress
182 * (busy page) or because a mapping has been
183 * wired, then give up.
186 if (m
->busy
|| m
->cleaning
)
187 return(MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK
);
190 * Don't worry about pages for which the kernel
191 * does not have any data.
194 if (m
->absent
|| m
->error
|| m
->restart
) {
195 if(m
->error
&& should_flush
) {
196 /* dump the page, pager wants us to */
197 /* clean it up and there is no */
198 /* relevant data to return */
199 if(m
->wire_count
== 0) {
201 return(MEMORY_OBJECT_LOCK_RESULT_DONE
);
204 return(MEMORY_OBJECT_LOCK_RESULT_DONE
);
208 assert(!m
->fictitious
);
210 if (m
->wire_count
!= 0) {
212 * If no change would take place
213 * anyway, return successfully.
217 * No change to page lock [2 checks] AND
218 * Should not return page
220 * XXX This doesn't handle sending a copy of a wired
221 * XXX page to the pager, but that will require some
222 * XXX significant surgery.
225 (m
->page_lock
== prot
|| prot
== VM_PROT_NO_CHANGE
) &&
226 ! memory_object_should_return_page(m
, should_return
)) {
229 * Restart page unlock requests,
230 * even though no change took place.
231 * [Memory managers may be expecting
232 * to see new requests.]
234 m
->unlock_request
= VM_PROT_NONE
;
237 return(MEMORY_OBJECT_LOCK_RESULT_DONE
);
240 return(MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK
);
244 * If the page is to be flushed, allow
245 * that to be done as part of the protection.
254 * If we are decreasing permission, do it now;
255 * let the fault handler take care of increases
256 * (pmap_page_protect may not increase protection).
259 if (prot
!= VM_PROT_NO_CHANGE
) {
260 if ((m
->page_lock
^ prot
) & prot
) {
261 pmap_page_protect(m
->phys_page
, VM_PROT_ALL
& ~prot
);
264 /* code associated with the vestigial
265 * memory_object_data_unlock
268 m
->lock_supplied
= TRUE
;
269 if (prot
!= VM_PROT_NONE
)
275 * Restart any past unlock requests, even if no
276 * change resulted. If the manager explicitly
277 * requested no protection change, then it is assumed
278 * to be remembering past requests.
281 m
->unlock_request
= VM_PROT_NONE
;
287 * Handle page returning.
290 if (memory_object_should_return_page(m
, should_return
)) {
293 * If we weren't planning
294 * to flush the page anyway,
295 * we may need to remove the
296 * page from the pageout
297 * system and from physical
301 vm_page_lock_queues();
302 VM_PAGE_QUEUES_REMOVE(m
);
303 vm_page_unlock_queues();
306 pmap_disconnect(m
->phys_page
);
309 return(MEMORY_OBJECT_LOCK_RESULT_MUST_CLEAN
);
311 return(MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN
);
322 * XXX Make clean but not flush a paging hint,
323 * and deactivate the pages. This is a hack
324 * because it overloads flush/clean with
325 * implementation-dependent meaning. This only
326 * happens to pages that are already clean.
329 if (vm_page_deactivate_hint
&&
330 (should_return
!= MEMORY_OBJECT_RETURN_NONE
)) {
331 vm_page_lock_queues();
332 vm_page_deactivate(m
);
333 vm_page_unlock_queues();
337 return(MEMORY_OBJECT_LOCK_RESULT_DONE
);
340 #define LIST_REQ_PAGEOUT_PAGES(object, data_cnt, action, po, ro, ioerr, iosync) \
343 register int upl_flags; \
345 vm_object_unlock(object); \
348 upl_flags = UPL_MSYNC | UPL_IOSYNC; \
350 upl_flags = UPL_MSYNC; \
352 (void) memory_object_data_return(object->pager, \
357 (action == MEMORY_OBJECT_LOCK_RESULT_MUST_CLEAN), \
361 vm_object_lock(object); \
365 * Routine: memory_object_lock_request [user interface]
368 * Control use of the data associated with the given
369 * memory object. For each page in the given range,
370 * perform the following operations, in order:
371 * 1) restrict access to the page (disallow
372 * forms specified by "prot");
373 * 2) return data to the manager (if "should_return"
374 * is RETURN_DIRTY and the page is dirty, or
375 * "should_return" is RETURN_ALL and the page
376 * is either dirty or precious); and,
377 * 3) flush the cached copy (if "should_flush"
379 * The set of pages is defined by a starting offset
380 * ("offset") and size ("size"). Only pages with the
381 * same page alignment as the starting offset are
384 * A single acknowledgement is sent (to the "reply_to"
385 * port) when these actions are complete. If successful,
386 * the naked send right for reply_to is consumed.
390 memory_object_lock_request(
391 memory_object_control_t control
,
392 memory_object_offset_t offset
,
393 memory_object_size_t size
,
394 memory_object_offset_t
* resid_offset
,
396 memory_object_return_t should_return
,
401 __unused boolean_t should_flush
;
403 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
405 XPR(XPR_MEMORY_OBJECT
,
406 "m_o_lock_request, control 0x%X off 0x%X size 0x%X flags %X prot %X\n",
407 (integer_t
)control
, offset
, size
,
408 (((should_return
&1)<<1)|should_flush
), prot
);
411 * Check for bogus arguments.
413 object
= memory_object_control_to_vm_object(control
);
414 if (object
== VM_OBJECT_NULL
)
415 return (KERN_INVALID_ARGUMENT
);
417 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
418 return (KERN_INVALID_ARGUMENT
);
420 size
= round_page_64(size
);
423 * Lock the object, and acquire a paging reference to
424 * prevent the memory_object reference from being released.
426 vm_object_lock(object
);
427 vm_object_paging_begin(object
);
428 offset
-= object
->paging_offset
;
430 (void)vm_object_update(object
,
431 offset
, size
, resid_offset
, io_errno
, should_return
, flags
, prot
);
433 vm_object_paging_end(object
);
434 vm_object_unlock(object
);
436 return (KERN_SUCCESS
);
440 * memory_object_release_name: [interface]
442 * Enforces name semantic on memory_object reference count decrement
443 * This routine should not be called unless the caller holds a name
444 * reference gained through the memory_object_named_create or the
445 * memory_object_rename call.
446 * If the TERMINATE_IDLE flag is set, the call will return if the
447 * reference count is not 1. i.e. idle with the only remaining reference
449 * If the decision is made to proceed the name field flag is set to
450 * false and the reference count is decremented. If the RESPECT_CACHE
451 * flag is set and the reference count has gone to zero, the
452 * memory_object is checked to see if it is cacheable otherwise when
453 * the reference count is zero, it is simply terminated.
457 memory_object_release_name(
458 memory_object_control_t control
,
463 object
= memory_object_control_to_vm_object(control
);
464 if (object
== VM_OBJECT_NULL
)
465 return (KERN_INVALID_ARGUMENT
);
467 return vm_object_release_name(object
, flags
);
473 * Routine: memory_object_destroy [user interface]
475 * Shut down a memory object, despite the
476 * presence of address map (or other) references
480 memory_object_destroy(
481 memory_object_control_t control
,
482 kern_return_t reason
)
486 object
= memory_object_control_to_vm_object(control
);
487 if (object
== VM_OBJECT_NULL
)
488 return (KERN_INVALID_ARGUMENT
);
490 return (vm_object_destroy(object
, reason
));
494 * Routine: vm_object_sync
496 * Kernel internal function to synch out pages in a given
497 * range within an object to its memory manager. Much the
498 * same as memory_object_lock_request but page protection
501 * If the should_flush and should_return flags are true pages
502 * are flushed, that is dirty & precious pages are written to
503 * the memory manager and then discarded. If should_return
504 * is false, only precious pages are returned to the memory
507 * If should flush is false and should_return true, the memory
508 * manager's copy of the pages is updated. If should_return
509 * is also false, only the precious pages are updated. This
510 * last option is of limited utility.
513 * FALSE if no pages were returned to the pager
520 vm_object_offset_t offset
,
521 vm_object_size_t size
,
522 boolean_t should_flush
,
523 boolean_t should_return
,
524 boolean_t should_iosync
)
530 "vm_o_sync, object 0x%X, offset 0x%X size 0x%x flush %d rtn %d\n",
531 (integer_t
)object
, offset
, size
, should_flush
, should_return
);
534 * Lock the object, and acquire a paging reference to
535 * prevent the memory_object and control ports from
538 vm_object_lock(object
);
539 vm_object_paging_begin(object
);
542 flags
= MEMORY_OBJECT_DATA_FLUSH
;
547 flags
|= MEMORY_OBJECT_IO_SYNC
;
549 rv
= vm_object_update(object
, offset
, (vm_object_size_t
)size
, NULL
, NULL
,
551 MEMORY_OBJECT_RETURN_ALL
:
552 MEMORY_OBJECT_RETURN_NONE
,
557 vm_object_paging_end(object
);
558 vm_object_unlock(object
);
566 vm_object_update_extent(
568 vm_object_offset_t offset
,
569 vm_object_offset_t offset_end
,
570 vm_object_offset_t
*offset_resid
,
572 boolean_t should_flush
,
573 memory_object_return_t should_return
,
574 boolean_t should_iosync
,
579 vm_size_t data_cnt
= 0;
580 vm_object_offset_t paging_offset
= 0;
581 vm_object_offset_t last_offset
= offset
;
582 memory_object_lock_result_t page_lock_result
;
583 memory_object_lock_result_t pageout_action
;
585 pageout_action
= MEMORY_OBJECT_LOCK_RESULT_DONE
;
588 offset
< offset_end
&& object
->resident_page_count
;
589 offset
+= PAGE_SIZE_64
) {
592 * Limit the number of pages to be cleaned at once.
594 if (data_cnt
>= PAGE_SIZE
* MAX_UPL_TRANSFER
) {
595 LIST_REQ_PAGEOUT_PAGES(object
, data_cnt
,
596 pageout_action
, paging_offset
, offset_resid
, io_errno
, should_iosync
);
600 while ((m
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
601 page_lock_result
= memory_object_lock_page(m
, should_return
, should_flush
, prot
);
603 XPR(XPR_MEMORY_OBJECT
,
604 "m_o_update: lock_page, obj 0x%X offset 0x%X result %d\n",
605 (integer_t
)object
, offset
, page_lock_result
, 0, 0);
607 switch (page_lock_result
)
609 case MEMORY_OBJECT_LOCK_RESULT_DONE
:
611 * End of a cluster of dirty pages.
614 LIST_REQ_PAGEOUT_PAGES(object
,
615 data_cnt
, pageout_action
,
616 paging_offset
, offset_resid
, io_errno
, should_iosync
);
622 case MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK
:
624 * Since it is necessary to block,
625 * clean any dirty pages now.
628 LIST_REQ_PAGEOUT_PAGES(object
,
629 data_cnt
, pageout_action
,
630 paging_offset
, offset_resid
, io_errno
, should_iosync
);
634 PAGE_SLEEP(object
, m
, THREAD_UNINT
);
637 case MEMORY_OBJECT_LOCK_RESULT_MUST_CLEAN
:
638 case MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN
:
640 * The clean and return cases are similar.
642 * if this would form a discontiguous block,
643 * clean the old pages and start anew.
645 * Mark the page busy since we will unlock the
646 * object if we issue the LIST_REQ_PAGEOUT
650 ((last_offset
!= offset
) || (pageout_action
!= page_lock_result
))) {
651 LIST_REQ_PAGEOUT_PAGES(object
,
652 data_cnt
, pageout_action
,
653 paging_offset
, offset_resid
, io_errno
, should_iosync
);
659 PAGE_SLEEP(object
, m
, THREAD_UNINT
);
663 pageout_action
= page_lock_result
;
664 paging_offset
= offset
;
666 data_cnt
+= PAGE_SIZE
;
667 last_offset
= offset
+ PAGE_SIZE_64
;
669 vm_page_lock_queues();
673 m
->list_req_pending
= TRUE
;
678 * and add additional state
685 vm_page_unlock_queues();
694 * We have completed the scan for applicable pages.
695 * Clean any pages that have been saved.
698 LIST_REQ_PAGEOUT_PAGES(object
,
699 data_cnt
, pageout_action
, paging_offset
, offset_resid
, io_errno
, should_iosync
);
707 * Routine: vm_object_update
709 * Work function for m_o_lock_request(), vm_o_sync().
711 * Called with object locked and paging ref taken.
715 register vm_object_t object
,
716 register vm_object_offset_t offset
,
717 register vm_object_size_t size
,
718 register vm_object_offset_t
*resid_offset
,
720 memory_object_return_t should_return
,
722 vm_prot_t protection
)
724 vm_object_t copy_object
;
725 boolean_t data_returned
= FALSE
;
726 boolean_t update_cow
;
727 boolean_t should_flush
= (flags
& MEMORY_OBJECT_DATA_FLUSH
) ? TRUE
: FALSE
;
728 boolean_t should_iosync
= (flags
& MEMORY_OBJECT_IO_SYNC
) ? TRUE
: FALSE
;
731 #define MAX_EXTENTS 8
732 #define EXTENT_SIZE (1024 * 1024 * 256)
733 #define RESIDENT_LIMIT (1024 * 32)
735 vm_object_offset_t e_base
;
736 vm_object_offset_t e_min
;
737 vm_object_offset_t e_max
;
738 } extents
[MAX_EXTENTS
];
741 * To avoid blocking while scanning for pages, save
742 * dirty pages to be cleaned all at once.
744 * XXXO A similar strategy could be used to limit the
745 * number of times that a scan must be restarted for
746 * other reasons. Those pages that would require blocking
747 * could be temporarily collected in another list, or
748 * their offsets could be recorded in a small array.
752 * XXX NOTE: May want to consider converting this to a page list
753 * XXX vm_map_copy interface. Need to understand object
754 * XXX coalescing implications before doing so.
757 update_cow
= ((flags
& MEMORY_OBJECT_DATA_FLUSH
)
758 && (!(flags
& MEMORY_OBJECT_DATA_NO_CHANGE
) &&
759 !(flags
& MEMORY_OBJECT_DATA_PURGE
)))
760 || (flags
& MEMORY_OBJECT_COPY_SYNC
);
763 if((((copy_object
= object
->copy
) != NULL
) && update_cow
) ||
764 (flags
& MEMORY_OBJECT_DATA_SYNC
)) {
766 vm_map_size_t copy_size
;
767 vm_map_offset_t copy_offset
;
771 kern_return_t error
= 0;
773 if(copy_object
!= NULL
) {
774 /* translate offset with respect to shadow's offset */
775 copy_offset
= (offset
>= copy_object
->shadow_offset
)?
776 (vm_map_offset_t
)(offset
- copy_object
->shadow_offset
) :
778 if(copy_offset
> copy_object
->size
)
779 copy_offset
= copy_object
->size
;
781 /* clip size with respect to shadow offset */
782 if (offset
>= copy_object
->shadow_offset
) {
784 } else if (size
>= copy_object
->shadow_offset
- offset
) {
786 (copy_object
->shadow_offset
- offset
);
791 if (copy_offset
+ copy_size
> copy_object
->size
) {
792 if (copy_object
->size
>= copy_offset
) {
793 copy_size
= copy_object
->size
- copy_offset
;
799 copy_size
+=copy_offset
;
801 vm_object_unlock(object
);
802 vm_object_lock(copy_object
);
804 copy_object
= object
;
806 copy_size
= offset
+ size
;
807 copy_offset
= offset
;
810 vm_object_paging_begin(copy_object
);
811 for (i
=copy_offset
; i
<copy_size
; i
+=PAGE_SIZE
) {
812 RETRY_COW_OF_LOCK_REQUEST
:
813 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
814 switch (vm_fault_page(copy_object
, i
,
815 VM_PROT_WRITE
|VM_PROT_READ
,
819 copy_offset
+copy_size
,
820 VM_BEHAVIOR_SEQUENTIAL
,
829 case VM_FAULT_SUCCESS
:
832 page
->object
, top_page
);
833 PAGE_WAKEUP_DONE(page
);
834 vm_page_lock_queues();
835 if (!page
->active
&& !page
->inactive
)
836 vm_page_activate(page
);
837 vm_page_unlock_queues();
838 vm_object_lock(copy_object
);
839 vm_object_paging_begin(copy_object
);
841 PAGE_WAKEUP_DONE(page
);
842 vm_page_lock_queues();
843 if (!page
->active
&& !page
->inactive
)
844 vm_page_activate(page
);
845 vm_page_unlock_queues();
849 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
850 vm_object_lock(copy_object
);
851 vm_object_paging_begin(copy_object
);
852 goto RETRY_COW_OF_LOCK_REQUEST
;
853 case VM_FAULT_INTERRUPTED
:
854 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
855 vm_object_lock(copy_object
);
856 vm_object_paging_begin(copy_object
);
857 goto RETRY_COW_OF_LOCK_REQUEST
;
858 case VM_FAULT_MEMORY_SHORTAGE
:
860 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
861 vm_object_lock(copy_object
);
862 vm_object_paging_begin(copy_object
);
863 goto RETRY_COW_OF_LOCK_REQUEST
;
864 case VM_FAULT_FICTITIOUS_SHORTAGE
:
865 vm_page_more_fictitious();
866 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
867 vm_object_lock(copy_object
);
868 vm_object_paging_begin(copy_object
);
869 goto RETRY_COW_OF_LOCK_REQUEST
;
870 case VM_FAULT_MEMORY_ERROR
:
871 vm_object_lock(object
);
872 goto BYPASS_COW_COPYIN
;
876 vm_object_paging_end(copy_object
);
877 if(copy_object
!= object
) {
878 vm_object_unlock(copy_object
);
879 vm_object_lock(object
);
882 if((flags
& (MEMORY_OBJECT_DATA_SYNC
| MEMORY_OBJECT_COPY_SYNC
))) {
885 if(((copy_object
= object
->copy
) != NULL
) &&
886 (flags
& MEMORY_OBJECT_DATA_PURGE
)) {
887 copy_object
->shadow_severed
= TRUE
;
888 copy_object
->shadowed
= FALSE
;
889 copy_object
->shadow
= NULL
;
890 /* delete the ref the COW was holding on the target object */
891 vm_object_deallocate(object
);
896 * when we have a really large range to check relative
897 * to the number of actual resident pages, we'd like
898 * to use the resident page list to drive our checks
899 * however, the object lock will get dropped while processing
900 * the page which means the resident queue can change which
901 * means we can't walk the queue as we process the pages
902 * we also want to do the processing in offset order to allow
903 * 'runs' of pages to be collected if we're being told to
904 * flush to disk... the resident page queue is NOT ordered.
906 * a temporary solution (until we figure out how to deal with
907 * large address spaces more generically) is to pre-flight
908 * the resident page queue (if it's small enough) and develop
909 * a collection of extents (that encompass actual resident pages)
910 * to visit. This will at least allow us to deal with some of the
911 * more pathological cases in a more efficient manner. The current
912 * worst case (a single resident page at the end of an extremely large
913 * range) can take minutes to complete for ranges in the terrabyte
914 * category... since this routine is called when truncating a file,
915 * and we currently support files up to 16 Tbytes in size, this
916 * is not a theoretical problem
919 if ((object
->resident_page_count
< RESIDENT_LIMIT
) &&
920 (atop_64(size
) > (unsigned)(object
->resident_page_count
/(8 * MAX_EXTENTS
)))) {
922 vm_object_offset_t start
;
923 vm_object_offset_t end
;
924 vm_object_size_t e_mask
;
930 e_mask
= ~((vm_object_size_t
)(EXTENT_SIZE
- 1));
932 m
= (vm_page_t
) queue_first(&object
->memq
);
934 while (!queue_end(&object
->memq
, (queue_entry_t
) m
)) {
935 next
= (vm_page_t
) queue_next(&m
->listq
);
937 if ((m
->offset
>= start
) && (m
->offset
< end
)) {
939 * this is a page we're interested in
940 * try to fit it into a current extent
942 for (n
= 0; n
< num_of_extents
; n
++) {
943 if ((m
->offset
& e_mask
) == extents
[n
].e_base
) {
945 * use (PAGE_SIZE - 1) to determine the
946 * max offset so that we don't wrap if
947 * we're at the last page of the space
949 if (m
->offset
< extents
[n
].e_min
)
950 extents
[n
].e_min
= m
->offset
;
951 else if ((m
->offset
+ (PAGE_SIZE
- 1)) > extents
[n
].e_max
)
952 extents
[n
].e_max
= m
->offset
+ (PAGE_SIZE
- 1);
956 if (n
== num_of_extents
) {
958 * didn't find a current extent that can encompass
961 if (n
< MAX_EXTENTS
) {
963 * if we still have room,
964 * create a new extent
966 extents
[n
].e_base
= m
->offset
& e_mask
;
967 extents
[n
].e_min
= m
->offset
;
968 extents
[n
].e_max
= m
->offset
+ (PAGE_SIZE
- 1);
973 * no room to create a new extent...
974 * fall back to a single extent based
975 * on the min and max page offsets
976 * we find in the range we're interested in...
977 * first, look through the extent list and
978 * develop the overall min and max for the
979 * pages we've looked at up to this point
981 for (n
= 1; n
< num_of_extents
; n
++) {
982 if (extents
[n
].e_min
< extents
[0].e_min
)
983 extents
[0].e_min
= extents
[n
].e_min
;
984 if (extents
[n
].e_max
> extents
[0].e_max
)
985 extents
[0].e_max
= extents
[n
].e_max
;
988 * now setup to run through the remaining pages
989 * to determine the overall min and max
990 * offset for the specified range
992 extents
[0].e_base
= 0;
997 * by continuing, we'll reprocess the
998 * page that forced us to abandon trying
999 * to develop multiple extents
1008 extents
[0].e_min
= offset
;
1009 extents
[0].e_max
= offset
+ (size
- 1);
1013 for (n
= 0; n
< num_of_extents
; n
++) {
1014 if (vm_object_update_extent(object
, extents
[n
].e_min
, extents
[n
].e_max
, resid_offset
, io_errno
,
1015 should_flush
, should_return
, should_iosync
, protection
))
1016 data_returned
= TRUE
;
1018 return (data_returned
);
1023 * Routine: memory_object_synchronize_completed [user interface]
1025 * Tell kernel that previously synchronized data
1026 * (memory_object_synchronize) has been queue or placed on the
1029 * Note: there may be multiple synchronize requests for a given
1030 * memory object outstanding but they will not overlap.
1034 memory_object_synchronize_completed(
1035 memory_object_control_t control
,
1036 memory_object_offset_t offset
,
1042 object
= memory_object_control_to_vm_object(control
);
1044 XPR(XPR_MEMORY_OBJECT
,
1045 "m_o_sync_completed, object 0x%X, offset 0x%X length 0x%X\n",
1046 (integer_t
)object
, offset
, length
, 0, 0);
1049 * Look for bogus arguments
1052 if (object
== VM_OBJECT_NULL
)
1053 return (KERN_INVALID_ARGUMENT
);
1055 vm_object_lock(object
);
1058 * search for sync request structure
1060 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
1061 if (msr
->offset
== offset
&& msr
->length
== length
) {
1062 queue_remove(&object
->msr_q
, msr
, msync_req_t
, msr_q
);
1065 }/* queue_iterate */
1067 if (queue_end(&object
->msr_q
, (queue_entry_t
)msr
)) {
1068 vm_object_unlock(object
);
1069 return KERN_INVALID_ARGUMENT
;
1073 vm_object_unlock(object
);
1074 msr
->flag
= VM_MSYNC_DONE
;
1076 thread_wakeup((event_t
) msr
);
1078 return KERN_SUCCESS
;
1079 }/* memory_object_synchronize_completed */
1081 static kern_return_t
1082 vm_object_set_attributes_common(
1084 boolean_t may_cache
,
1085 memory_object_copy_strategy_t copy_strategy
,
1086 boolean_t temporary
,
1087 memory_object_cluster_size_t cluster_size
,
1088 boolean_t silent_overwrite
,
1089 boolean_t advisory_pageout
)
1091 boolean_t object_became_ready
;
1093 XPR(XPR_MEMORY_OBJECT
,
1094 "m_o_set_attr_com, object 0x%X flg %x strat %d\n",
1095 (integer_t
)object
, (may_cache
&1)|((temporary
&1)<1), copy_strategy
, 0, 0);
1097 if (object
== VM_OBJECT_NULL
)
1098 return(KERN_INVALID_ARGUMENT
);
1101 * Verify the attributes of importance
1104 switch(copy_strategy
) {
1105 case MEMORY_OBJECT_COPY_NONE
:
1106 case MEMORY_OBJECT_COPY_DELAY
:
1109 return(KERN_INVALID_ARGUMENT
);
1112 #if !ADVISORY_PAGEOUT
1113 if (silent_overwrite
|| advisory_pageout
)
1114 return(KERN_INVALID_ARGUMENT
);
1116 #endif /* !ADVISORY_PAGEOUT */
1121 if (cluster_size
!= 0) {
1122 int pages_per_cluster
;
1123 pages_per_cluster
= atop_32(cluster_size
);
1125 * Cluster size must be integral multiple of page size,
1126 * and be a power of 2 number of pages.
1128 if ((cluster_size
& (PAGE_SIZE
-1)) ||
1129 ((pages_per_cluster
-1) & pages_per_cluster
))
1130 return KERN_INVALID_ARGUMENT
;
1133 vm_object_lock(object
);
1136 * Copy the attributes
1138 assert(!object
->internal
);
1139 object_became_ready
= !object
->pager_ready
;
1140 object
->copy_strategy
= copy_strategy
;
1141 object
->can_persist
= may_cache
;
1142 object
->temporary
= temporary
;
1143 object
->silent_overwrite
= silent_overwrite
;
1144 object
->advisory_pageout
= advisory_pageout
;
1145 if (cluster_size
== 0)
1146 cluster_size
= PAGE_SIZE
;
1147 object
->cluster_size
= cluster_size
;
1149 assert(cluster_size
>= PAGE_SIZE
&&
1150 cluster_size
% PAGE_SIZE
== 0);
1153 * Wake up anyone waiting for the ready attribute
1154 * to become asserted.
1157 if (object_became_ready
) {
1158 object
->pager_ready
= TRUE
;
1159 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
1162 vm_object_unlock(object
);
1164 return(KERN_SUCCESS
);
1168 * Set the memory object attribute as provided.
1170 * XXX This routine cannot be completed until the vm_msync, clean
1171 * in place, and cluster work is completed. See ifdef notyet
1172 * below and note that vm_object_set_attributes_common()
1173 * may have to be expanded.
1176 memory_object_change_attributes(
1177 memory_object_control_t control
,
1178 memory_object_flavor_t flavor
,
1179 memory_object_info_t attributes
,
1180 mach_msg_type_number_t count
)
1183 kern_return_t result
= KERN_SUCCESS
;
1184 boolean_t temporary
;
1185 boolean_t may_cache
;
1186 boolean_t invalidate
;
1187 memory_object_cluster_size_t cluster_size
;
1188 memory_object_copy_strategy_t copy_strategy
;
1189 boolean_t silent_overwrite
;
1190 boolean_t advisory_pageout
;
1192 object
= memory_object_control_to_vm_object(control
);
1193 if (object
== VM_OBJECT_NULL
)
1194 return (KERN_INVALID_ARGUMENT
);
1196 vm_object_lock(object
);
1198 temporary
= object
->temporary
;
1199 may_cache
= object
->can_persist
;
1200 copy_strategy
= object
->copy_strategy
;
1201 silent_overwrite
= object
->silent_overwrite
;
1202 advisory_pageout
= object
->advisory_pageout
;
1204 invalidate
= object
->invalidate
;
1206 cluster_size
= object
->cluster_size
;
1207 vm_object_unlock(object
);
1210 case OLD_MEMORY_OBJECT_BEHAVIOR_INFO
:
1212 old_memory_object_behave_info_t behave
;
1214 if (count
!= OLD_MEMORY_OBJECT_BEHAVE_INFO_COUNT
) {
1215 result
= KERN_INVALID_ARGUMENT
;
1219 behave
= (old_memory_object_behave_info_t
) attributes
;
1221 temporary
= behave
->temporary
;
1222 invalidate
= behave
->invalidate
;
1223 copy_strategy
= behave
->copy_strategy
;
1228 case MEMORY_OBJECT_BEHAVIOR_INFO
:
1230 memory_object_behave_info_t behave
;
1232 if (count
!= MEMORY_OBJECT_BEHAVE_INFO_COUNT
) {
1233 result
= KERN_INVALID_ARGUMENT
;
1237 behave
= (memory_object_behave_info_t
) attributes
;
1239 temporary
= behave
->temporary
;
1240 invalidate
= behave
->invalidate
;
1241 copy_strategy
= behave
->copy_strategy
;
1242 silent_overwrite
= behave
->silent_overwrite
;
1243 advisory_pageout
= behave
->advisory_pageout
;
1247 case MEMORY_OBJECT_PERFORMANCE_INFO
:
1249 memory_object_perf_info_t perf
;
1251 if (count
!= MEMORY_OBJECT_PERF_INFO_COUNT
) {
1252 result
= KERN_INVALID_ARGUMENT
;
1256 perf
= (memory_object_perf_info_t
) attributes
;
1258 may_cache
= perf
->may_cache
;
1259 cluster_size
= round_page_32(perf
->cluster_size
);
1264 case OLD_MEMORY_OBJECT_ATTRIBUTE_INFO
:
1266 old_memory_object_attr_info_t attr
;
1268 if (count
!= OLD_MEMORY_OBJECT_ATTR_INFO_COUNT
) {
1269 result
= KERN_INVALID_ARGUMENT
;
1273 attr
= (old_memory_object_attr_info_t
) attributes
;
1275 may_cache
= attr
->may_cache
;
1276 copy_strategy
= attr
->copy_strategy
;
1277 cluster_size
= page_size
;
1282 case MEMORY_OBJECT_ATTRIBUTE_INFO
:
1284 memory_object_attr_info_t attr
;
1286 if (count
!= MEMORY_OBJECT_ATTR_INFO_COUNT
) {
1287 result
= KERN_INVALID_ARGUMENT
;
1291 attr
= (memory_object_attr_info_t
) attributes
;
1293 copy_strategy
= attr
->copy_strategy
;
1294 may_cache
= attr
->may_cache_object
;
1295 cluster_size
= attr
->cluster_size
;
1296 temporary
= attr
->temporary
;
1302 result
= KERN_INVALID_ARGUMENT
;
1306 if (result
!= KERN_SUCCESS
)
1309 if (copy_strategy
== MEMORY_OBJECT_COPY_TEMPORARY
) {
1310 copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
1317 * XXX may_cache may become a tri-valued variable to handle
1318 * XXX uncache if not in use.
1320 return (vm_object_set_attributes_common(object
,
1330 memory_object_get_attributes(
1331 memory_object_control_t control
,
1332 memory_object_flavor_t flavor
,
1333 memory_object_info_t attributes
, /* pointer to OUT array */
1334 mach_msg_type_number_t
*count
) /* IN/OUT */
1336 kern_return_t ret
= KERN_SUCCESS
;
1339 object
= memory_object_control_to_vm_object(control
);
1340 if (object
== VM_OBJECT_NULL
)
1341 return (KERN_INVALID_ARGUMENT
);
1343 vm_object_lock(object
);
1346 case OLD_MEMORY_OBJECT_BEHAVIOR_INFO
:
1348 old_memory_object_behave_info_t behave
;
1350 if (*count
< OLD_MEMORY_OBJECT_BEHAVE_INFO_COUNT
) {
1351 ret
= KERN_INVALID_ARGUMENT
;
1355 behave
= (old_memory_object_behave_info_t
) attributes
;
1356 behave
->copy_strategy
= object
->copy_strategy
;
1357 behave
->temporary
= object
->temporary
;
1358 #if notyet /* remove when vm_msync complies and clean in place fini */
1359 behave
->invalidate
= object
->invalidate
;
1361 behave
->invalidate
= FALSE
;
1364 *count
= OLD_MEMORY_OBJECT_BEHAVE_INFO_COUNT
;
1368 case MEMORY_OBJECT_BEHAVIOR_INFO
:
1370 memory_object_behave_info_t behave
;
1372 if (*count
< MEMORY_OBJECT_BEHAVE_INFO_COUNT
) {
1373 ret
= KERN_INVALID_ARGUMENT
;
1377 behave
= (memory_object_behave_info_t
) attributes
;
1378 behave
->copy_strategy
= object
->copy_strategy
;
1379 behave
->temporary
= object
->temporary
;
1380 #if notyet /* remove when vm_msync complies and clean in place fini */
1381 behave
->invalidate
= object
->invalidate
;
1383 behave
->invalidate
= FALSE
;
1385 behave
->advisory_pageout
= object
->advisory_pageout
;
1386 behave
->silent_overwrite
= object
->silent_overwrite
;
1387 *count
= MEMORY_OBJECT_BEHAVE_INFO_COUNT
;
1391 case MEMORY_OBJECT_PERFORMANCE_INFO
:
1393 memory_object_perf_info_t perf
;
1395 if (*count
< MEMORY_OBJECT_PERF_INFO_COUNT
) {
1396 ret
= KERN_INVALID_ARGUMENT
;
1400 perf
= (memory_object_perf_info_t
) attributes
;
1401 perf
->cluster_size
= object
->cluster_size
;
1402 perf
->may_cache
= object
->can_persist
;
1404 *count
= MEMORY_OBJECT_PERF_INFO_COUNT
;
1408 case OLD_MEMORY_OBJECT_ATTRIBUTE_INFO
:
1410 old_memory_object_attr_info_t attr
;
1412 if (*count
< OLD_MEMORY_OBJECT_ATTR_INFO_COUNT
) {
1413 ret
= KERN_INVALID_ARGUMENT
;
1417 attr
= (old_memory_object_attr_info_t
) attributes
;
1418 attr
->may_cache
= object
->can_persist
;
1419 attr
->copy_strategy
= object
->copy_strategy
;
1421 *count
= OLD_MEMORY_OBJECT_ATTR_INFO_COUNT
;
1425 case MEMORY_OBJECT_ATTRIBUTE_INFO
:
1427 memory_object_attr_info_t attr
;
1429 if (*count
< MEMORY_OBJECT_ATTR_INFO_COUNT
) {
1430 ret
= KERN_INVALID_ARGUMENT
;
1434 attr
= (memory_object_attr_info_t
) attributes
;
1435 attr
->copy_strategy
= object
->copy_strategy
;
1436 attr
->cluster_size
= object
->cluster_size
;
1437 attr
->may_cache_object
= object
->can_persist
;
1438 attr
->temporary
= object
->temporary
;
1440 *count
= MEMORY_OBJECT_ATTR_INFO_COUNT
;
1445 ret
= KERN_INVALID_ARGUMENT
;
1449 vm_object_unlock(object
);
1456 memory_object_iopl_request(
1458 memory_object_offset_t offset
,
1459 upl_size_t
*upl_size
,
1461 upl_page_info_array_t user_page_list
,
1462 unsigned int *page_list_count
,
1469 caller_flags
= *flags
;
1471 if (caller_flags
& ~UPL_VALID_FLAGS
) {
1473 * For forward compatibility's sake,
1474 * reject any unknown flag.
1476 return KERN_INVALID_VALUE
;
1479 if (ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
1480 vm_named_entry_t named_entry
;
1482 named_entry
= (vm_named_entry_t
)port
->ip_kobject
;
1483 /* a few checks to make sure user is obeying rules */
1484 if(*upl_size
== 0) {
1485 if(offset
>= named_entry
->size
)
1486 return(KERN_INVALID_RIGHT
);
1487 *upl_size
= named_entry
->size
- offset
;
1489 if(caller_flags
& UPL_COPYOUT_FROM
) {
1490 if((named_entry
->protection
& VM_PROT_READ
)
1492 return(KERN_INVALID_RIGHT
);
1495 if((named_entry
->protection
&
1496 (VM_PROT_READ
| VM_PROT_WRITE
))
1497 != (VM_PROT_READ
| VM_PROT_WRITE
)) {
1498 return(KERN_INVALID_RIGHT
);
1501 if(named_entry
->size
< (offset
+ *upl_size
))
1502 return(KERN_INVALID_ARGUMENT
);
1504 /* the callers parameter offset is defined to be the */
1505 /* offset from beginning of named entry offset in object */
1506 offset
= offset
+ named_entry
->offset
;
1508 if(named_entry
->is_sub_map
)
1509 return (KERN_INVALID_ARGUMENT
);
1511 named_entry_lock(named_entry
);
1513 if (named_entry
->is_pager
) {
1514 object
= vm_object_enter(named_entry
->backing
.pager
,
1515 named_entry
->offset
+ named_entry
->size
,
1516 named_entry
->internal
,
1519 if (object
== VM_OBJECT_NULL
) {
1520 named_entry_unlock(named_entry
);
1521 return(KERN_INVALID_OBJECT
);
1524 /* JMM - drop reference on pager here? */
1526 /* create an extra reference for the named entry */
1527 vm_object_lock(object
);
1528 vm_object_reference_locked(object
);
1529 named_entry
->backing
.object
= object
;
1530 named_entry
->is_pager
= FALSE
;
1531 named_entry_unlock(named_entry
);
1533 /* wait for object to be ready */
1534 while (!object
->pager_ready
) {
1535 vm_object_wait(object
,
1536 VM_OBJECT_EVENT_PAGER_READY
,
1538 vm_object_lock(object
);
1540 vm_object_unlock(object
);
1542 /* This is the case where we are going to map */
1543 /* an already mapped object. If the object is */
1544 /* not ready it is internal. An external */
1545 /* object cannot be mapped until it is ready */
1546 /* we can therefore avoid the ready check */
1548 object
= named_entry
->backing
.object
;
1549 vm_object_reference(object
);
1550 named_entry_unlock(named_entry
);
1552 } else if (ip_kotype(port
) == IKOT_MEM_OBJ_CONTROL
) {
1553 memory_object_control_t control
;
1554 control
= (memory_object_control_t
) port
;
1555 if (control
== NULL
)
1556 return (KERN_INVALID_ARGUMENT
);
1557 object
= memory_object_control_to_vm_object(control
);
1558 if (object
== VM_OBJECT_NULL
)
1559 return (KERN_INVALID_ARGUMENT
);
1560 vm_object_reference(object
);
1562 return KERN_INVALID_ARGUMENT
;
1564 if (object
== VM_OBJECT_NULL
)
1565 return (KERN_INVALID_ARGUMENT
);
1567 if (!object
->private) {
1568 if (*upl_size
> (MAX_UPL_TRANSFER
*PAGE_SIZE
))
1569 *upl_size
= (MAX_UPL_TRANSFER
*PAGE_SIZE
);
1570 if (object
->phys_contiguous
) {
1571 *flags
= UPL_PHYS_CONTIG
;
1576 *flags
= UPL_DEV_MEMORY
| UPL_PHYS_CONTIG
;
1579 ret
= vm_object_iopl_request(object
,
1586 vm_object_deallocate(object
);
1591 * Routine: memory_object_upl_request [interface]
1593 * Cause the population of a portion of a vm_object.
1594 * Depending on the nature of the request, the pages
1595 * returned may be contain valid data or be uninitialized.
1600 memory_object_upl_request(
1601 memory_object_control_t control
,
1602 memory_object_offset_t offset
,
1605 upl_page_info_array_t user_page_list
,
1606 unsigned int *page_list_count
,
1611 object
= memory_object_control_to_vm_object(control
);
1612 if (object
== VM_OBJECT_NULL
)
1613 return (KERN_INVALID_ARGUMENT
);
1615 return vm_object_upl_request(object
,
1625 * Routine: memory_object_super_upl_request [interface]
1627 * Cause the population of a portion of a vm_object
1628 * in much the same way as memory_object_upl_request.
1629 * Depending on the nature of the request, the pages
1630 * returned may be contain valid data or be uninitialized.
1631 * However, the region may be expanded up to the super
1632 * cluster size provided.
1636 memory_object_super_upl_request(
1637 memory_object_control_t control
,
1638 memory_object_offset_t offset
,
1640 upl_size_t super_cluster
,
1642 upl_page_info_t
*user_page_list
,
1643 unsigned int *page_list_count
,
1648 object
= memory_object_control_to_vm_object(control
);
1649 if (object
== VM_OBJECT_NULL
)
1650 return (KERN_INVALID_ARGUMENT
);
1652 return vm_object_super_upl_request(object
,
1662 int vm_stat_discard_cleared_reply
= 0;
1663 int vm_stat_discard_cleared_unset
= 0;
1664 int vm_stat_discard_cleared_too_late
= 0;
1669 * Routine: host_default_memory_manager [interface]
1671 * set/get the default memory manager port and default cluster
1674 * If successful, consumes the supplied naked send right.
1677 host_default_memory_manager(
1678 host_priv_t host_priv
,
1679 memory_object_default_t
*default_manager
,
1680 memory_object_cluster_size_t cluster_size
)
1682 memory_object_default_t current_manager
;
1683 memory_object_default_t new_manager
;
1684 memory_object_default_t returned_manager
;
1686 if (host_priv
== HOST_PRIV_NULL
)
1687 return(KERN_INVALID_HOST
);
1689 assert(host_priv
== &realhost
);
1691 new_manager
= *default_manager
;
1692 mutex_lock(&memory_manager_default_lock
);
1693 current_manager
= memory_manager_default
;
1695 if (new_manager
== MEMORY_OBJECT_DEFAULT_NULL
) {
1697 * Retrieve the current value.
1699 memory_object_default_reference(current_manager
);
1700 returned_manager
= current_manager
;
1703 * Retrieve the current value,
1704 * and replace it with the supplied value.
1705 * We return the old reference to the caller
1706 * but we have to take a reference on the new
1710 returned_manager
= current_manager
;
1711 memory_manager_default
= new_manager
;
1712 memory_object_default_reference(new_manager
);
1714 if (cluster_size
% PAGE_SIZE
!= 0) {
1716 mutex_unlock(&memory_manager_default_lock
);
1717 return KERN_INVALID_ARGUMENT
;
1719 cluster_size
= round_page_32(cluster_size
);
1722 memory_manager_default_cluster
= cluster_size
;
1725 * In case anyone's been waiting for a memory
1726 * manager to be established, wake them up.
1729 thread_wakeup((event_t
) &memory_manager_default
);
1732 mutex_unlock(&memory_manager_default_lock
);
1734 *default_manager
= returned_manager
;
1735 return(KERN_SUCCESS
);
1739 * Routine: memory_manager_default_reference
1741 * Returns a naked send right for the default
1742 * memory manager. The returned right is always
1743 * valid (not IP_NULL or IP_DEAD).
1746 __private_extern__ memory_object_default_t
1747 memory_manager_default_reference(
1748 memory_object_cluster_size_t
*cluster_size
)
1750 memory_object_default_t current_manager
;
1752 mutex_lock(&memory_manager_default_lock
);
1753 current_manager
= memory_manager_default
;
1754 while (current_manager
== MEMORY_OBJECT_DEFAULT_NULL
) {
1757 res
= thread_sleep_mutex((event_t
) &memory_manager_default
,
1758 &memory_manager_default_lock
,
1760 assert(res
== THREAD_AWAKENED
);
1761 current_manager
= memory_manager_default
;
1763 memory_object_default_reference(current_manager
);
1764 *cluster_size
= memory_manager_default_cluster
;
1765 mutex_unlock(&memory_manager_default_lock
);
1767 return current_manager
;
1771 * Routine: memory_manager_default_check
1774 * Check whether a default memory manager has been set
1775 * up yet, or not. Returns KERN_SUCCESS if dmm exists,
1776 * and KERN_FAILURE if dmm does not exist.
1778 * If there is no default memory manager, log an error,
1779 * but only the first time.
1782 __private_extern__ kern_return_t
1783 memory_manager_default_check(void)
1785 memory_object_default_t current
;
1787 mutex_lock(&memory_manager_default_lock
);
1788 current
= memory_manager_default
;
1789 if (current
== MEMORY_OBJECT_DEFAULT_NULL
) {
1790 static boolean_t logged
; /* initialized to 0 */
1791 boolean_t complain
= !logged
;
1793 mutex_unlock(&memory_manager_default_lock
);
1795 printf("Warning: No default memory manager\n");
1796 return(KERN_FAILURE
);
1798 mutex_unlock(&memory_manager_default_lock
);
1799 return(KERN_SUCCESS
);
1803 __private_extern__
void
1804 memory_manager_default_init(void)
1806 memory_manager_default
= MEMORY_OBJECT_DEFAULT_NULL
;
1807 mutex_init(&memory_manager_default_lock
, 0);
1812 /* Allow manipulation of individual page state. This is actually part of */
1813 /* the UPL regimen but takes place on the object rather than on a UPL */
1816 memory_object_page_op(
1817 memory_object_control_t control
,
1818 memory_object_offset_t offset
,
1820 ppnum_t
*phys_entry
,
1825 object
= memory_object_control_to_vm_object(control
);
1826 if (object
== VM_OBJECT_NULL
)
1827 return (KERN_INVALID_ARGUMENT
);
1829 return vm_object_page_op(object
, offset
, ops
, phys_entry
, flags
);
1833 * memory_object_range_op offers performance enhancement over
1834 * memory_object_page_op for page_op functions which do not require page
1835 * level state to be returned from the call. Page_op was created to provide
1836 * a low-cost alternative to page manipulation via UPLs when only a single
1837 * page was involved. The range_op call establishes the ability in the _op
1838 * family of functions to work on multiple pages where the lack of page level
1839 * state handling allows the caller to avoid the overhead of the upl structures.
1843 memory_object_range_op(
1844 memory_object_control_t control
,
1845 memory_object_offset_t offset_beg
,
1846 memory_object_offset_t offset_end
,
1852 object
= memory_object_control_to_vm_object(control
);
1853 if (object
== VM_OBJECT_NULL
)
1854 return (KERN_INVALID_ARGUMENT
);
1856 return vm_object_range_op(object
,
1865 memory_object_pages_resident(
1866 memory_object_control_t control
,
1867 boolean_t
* has_pages_resident
)
1871 *has_pages_resident
= FALSE
;
1873 object
= memory_object_control_to_vm_object(control
);
1874 if (object
== VM_OBJECT_NULL
)
1875 return (KERN_INVALID_ARGUMENT
);
1877 if (object
->resident_page_count
)
1878 *has_pages_resident
= TRUE
;
1880 return (KERN_SUCCESS
);
1884 static zone_t mem_obj_control_zone
;
1886 __private_extern__
void
1887 memory_object_control_bootstrap(void)
1891 i
= (vm_size_t
) sizeof (struct memory_object_control
);
1892 mem_obj_control_zone
= zinit (i
, 8192*i
, 4096, "mem_obj_control");
1896 __private_extern__ memory_object_control_t
1897 memory_object_control_allocate(
1900 memory_object_control_t control
;
1902 control
= (memory_object_control_t
)zalloc(mem_obj_control_zone
);
1903 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1904 control
->moc_object
= object
;
1905 control
->moc_ikot
= IKOT_MEM_OBJ_CONTROL
; /* fake ip_kotype */
1910 __private_extern__
void
1911 memory_object_control_collapse(
1912 memory_object_control_t control
,
1915 assert((control
->moc_object
!= VM_OBJECT_NULL
) &&
1916 (control
->moc_object
!= object
));
1917 control
->moc_object
= object
;
1920 __private_extern__ vm_object_t
1921 memory_object_control_to_vm_object(
1922 memory_object_control_t control
)
1924 if (control
== MEMORY_OBJECT_CONTROL_NULL
||
1925 control
->moc_ikot
!= IKOT_MEM_OBJ_CONTROL
)
1926 return VM_OBJECT_NULL
;
1928 return (control
->moc_object
);
1931 memory_object_control_t
1932 convert_port_to_mo_control(
1933 __unused mach_port_t port
)
1935 return MEMORY_OBJECT_CONTROL_NULL
;
1940 convert_mo_control_to_port(
1941 __unused memory_object_control_t control
)
1943 return MACH_PORT_NULL
;
1947 memory_object_control_reference(
1948 __unused memory_object_control_t control
)
1954 * We only every issue one of these references, so kill it
1955 * when that gets released (should switch the real reference
1956 * counting in true port-less EMMI).
1959 memory_object_control_deallocate(
1960 memory_object_control_t control
)
1962 zfree(mem_obj_control_zone
, control
);
1966 memory_object_control_disable(
1967 memory_object_control_t control
)
1969 assert(control
->moc_object
!= VM_OBJECT_NULL
);
1970 control
->moc_object
= VM_OBJECT_NULL
;
1974 memory_object_default_reference(
1975 memory_object_default_t dmm
)
1977 ipc_port_make_send(dmm
);
1981 memory_object_default_deallocate(
1982 memory_object_default_t dmm
)
1984 ipc_port_release_send(dmm
);
1988 convert_port_to_memory_object(
1989 __unused mach_port_t port
)
1991 return (MEMORY_OBJECT_NULL
);
1996 convert_memory_object_to_port(
1997 __unused memory_object_t object
)
1999 return (MACH_PORT_NULL
);
2003 /* Routine memory_object_reference */
2004 void memory_object_reference(
2005 memory_object_t memory_object
)
2007 (memory_object
->mo_pager_ops
->memory_object_reference
)(
2011 /* Routine memory_object_deallocate */
2012 void memory_object_deallocate(
2013 memory_object_t memory_object
)
2015 (memory_object
->mo_pager_ops
->memory_object_deallocate
)(
2020 /* Routine memory_object_init */
2021 kern_return_t memory_object_init
2023 memory_object_t memory_object
,
2024 memory_object_control_t memory_control
,
2025 memory_object_cluster_size_t memory_object_page_size
2028 return (memory_object
->mo_pager_ops
->memory_object_init
)(
2031 memory_object_page_size
);
2034 /* Routine memory_object_terminate */
2035 kern_return_t memory_object_terminate
2037 memory_object_t memory_object
2040 return (memory_object
->mo_pager_ops
->memory_object_terminate
)(
2044 /* Routine memory_object_data_request */
2045 kern_return_t memory_object_data_request
2047 memory_object_t memory_object
,
2048 memory_object_offset_t offset
,
2049 memory_object_cluster_size_t length
,
2050 vm_prot_t desired_access
2053 return (memory_object
->mo_pager_ops
->memory_object_data_request
)(
2060 /* Routine memory_object_data_return */
2061 kern_return_t memory_object_data_return
2063 memory_object_t memory_object
,
2064 memory_object_offset_t offset
,
2066 memory_object_offset_t
*resid_offset
,
2069 boolean_t kernel_copy
,
2073 return (memory_object
->mo_pager_ops
->memory_object_data_return
)(
2084 /* Routine memory_object_data_initialize */
2085 kern_return_t memory_object_data_initialize
2087 memory_object_t memory_object
,
2088 memory_object_offset_t offset
,
2092 return (memory_object
->mo_pager_ops
->memory_object_data_initialize
)(
2098 /* Routine memory_object_data_unlock */
2099 kern_return_t memory_object_data_unlock
2101 memory_object_t memory_object
,
2102 memory_object_offset_t offset
,
2104 vm_prot_t desired_access
2107 return (memory_object
->mo_pager_ops
->memory_object_data_unlock
)(
2114 /* Routine memory_object_synchronize */
2115 kern_return_t memory_object_synchronize
2117 memory_object_t memory_object
,
2118 memory_object_offset_t offset
,
2120 vm_sync_t sync_flags
2123 return (memory_object
->mo_pager_ops
->memory_object_synchronize
)(
2130 /* Routine memory_object_unmap */
2131 kern_return_t memory_object_unmap
2133 memory_object_t memory_object
2136 return (memory_object
->mo_pager_ops
->memory_object_unmap
)(
2140 /* Routine memory_object_create */
2141 kern_return_t memory_object_create
2143 memory_object_default_t default_memory_manager
,
2144 vm_size_t new_memory_object_size
,
2145 memory_object_t
*new_memory_object
2148 return default_pager_memory_object_create(default_memory_manager
,
2149 new_memory_object_size
,
2154 convert_port_to_upl(
2160 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UPL
)) {
2164 upl
= (upl_t
) port
->ip_kobject
;
2173 convert_upl_to_port(
2176 return MACH_PORT_NULL
;
2179 __private_extern__
void
2181 __unused ipc_port_t port
,
2182 __unused mach_port_mscount_t mscount
)