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
);
1553 memory_object_control_t control
;
1554 control
= (memory_object_control_t
)port
->ip_kobject
;
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 if (object
== VM_OBJECT_NULL
)
1563 return (KERN_INVALID_ARGUMENT
);
1565 if (!object
->private) {
1566 if (*upl_size
> (MAX_UPL_TRANSFER
*PAGE_SIZE
))
1567 *upl_size
= (MAX_UPL_TRANSFER
*PAGE_SIZE
);
1568 if (object
->phys_contiguous
) {
1569 *flags
= UPL_PHYS_CONTIG
;
1574 *flags
= UPL_DEV_MEMORY
| UPL_PHYS_CONTIG
;
1577 ret
= vm_object_iopl_request(object
,
1584 vm_object_deallocate(object
);
1589 * Routine: memory_object_upl_request [interface]
1591 * Cause the population of a portion of a vm_object.
1592 * Depending on the nature of the request, the pages
1593 * returned may be contain valid data or be uninitialized.
1598 memory_object_upl_request(
1599 memory_object_control_t control
,
1600 memory_object_offset_t offset
,
1603 upl_page_info_array_t user_page_list
,
1604 unsigned int *page_list_count
,
1609 object
= memory_object_control_to_vm_object(control
);
1610 if (object
== VM_OBJECT_NULL
)
1611 return (KERN_INVALID_ARGUMENT
);
1613 return vm_object_upl_request(object
,
1623 * Routine: memory_object_super_upl_request [interface]
1625 * Cause the population of a portion of a vm_object
1626 * in much the same way as memory_object_upl_request.
1627 * Depending on the nature of the request, the pages
1628 * returned may be contain valid data or be uninitialized.
1629 * However, the region may be expanded up to the super
1630 * cluster size provided.
1634 memory_object_super_upl_request(
1635 memory_object_control_t control
,
1636 memory_object_offset_t offset
,
1638 upl_size_t super_cluster
,
1640 upl_page_info_t
*user_page_list
,
1641 unsigned int *page_list_count
,
1646 object
= memory_object_control_to_vm_object(control
);
1647 if (object
== VM_OBJECT_NULL
)
1648 return (KERN_INVALID_ARGUMENT
);
1650 return vm_object_super_upl_request(object
,
1660 int vm_stat_discard_cleared_reply
= 0;
1661 int vm_stat_discard_cleared_unset
= 0;
1662 int vm_stat_discard_cleared_too_late
= 0;
1667 * Routine: host_default_memory_manager [interface]
1669 * set/get the default memory manager port and default cluster
1672 * If successful, consumes the supplied naked send right.
1675 host_default_memory_manager(
1676 host_priv_t host_priv
,
1677 memory_object_default_t
*default_manager
,
1678 memory_object_cluster_size_t cluster_size
)
1680 memory_object_default_t current_manager
;
1681 memory_object_default_t new_manager
;
1682 memory_object_default_t returned_manager
;
1684 if (host_priv
== HOST_PRIV_NULL
)
1685 return(KERN_INVALID_HOST
);
1687 assert(host_priv
== &realhost
);
1689 new_manager
= *default_manager
;
1690 mutex_lock(&memory_manager_default_lock
);
1691 current_manager
= memory_manager_default
;
1693 if (new_manager
== MEMORY_OBJECT_DEFAULT_NULL
) {
1695 * Retrieve the current value.
1697 memory_object_default_reference(current_manager
);
1698 returned_manager
= current_manager
;
1701 * Retrieve the current value,
1702 * and replace it with the supplied value.
1703 * We return the old reference to the caller
1704 * but we have to take a reference on the new
1708 returned_manager
= current_manager
;
1709 memory_manager_default
= new_manager
;
1710 memory_object_default_reference(new_manager
);
1712 if (cluster_size
% PAGE_SIZE
!= 0) {
1714 mutex_unlock(&memory_manager_default_lock
);
1715 return KERN_INVALID_ARGUMENT
;
1717 cluster_size
= round_page_32(cluster_size
);
1720 memory_manager_default_cluster
= cluster_size
;
1723 * In case anyone's been waiting for a memory
1724 * manager to be established, wake them up.
1727 thread_wakeup((event_t
) &memory_manager_default
);
1730 mutex_unlock(&memory_manager_default_lock
);
1732 *default_manager
= returned_manager
;
1733 return(KERN_SUCCESS
);
1737 * Routine: memory_manager_default_reference
1739 * Returns a naked send right for the default
1740 * memory manager. The returned right is always
1741 * valid (not IP_NULL or IP_DEAD).
1744 __private_extern__ memory_object_default_t
1745 memory_manager_default_reference(
1746 memory_object_cluster_size_t
*cluster_size
)
1748 memory_object_default_t current_manager
;
1750 mutex_lock(&memory_manager_default_lock
);
1751 current_manager
= memory_manager_default
;
1752 while (current_manager
== MEMORY_OBJECT_DEFAULT_NULL
) {
1755 res
= thread_sleep_mutex((event_t
) &memory_manager_default
,
1756 &memory_manager_default_lock
,
1758 assert(res
== THREAD_AWAKENED
);
1759 current_manager
= memory_manager_default
;
1761 memory_object_default_reference(current_manager
);
1762 *cluster_size
= memory_manager_default_cluster
;
1763 mutex_unlock(&memory_manager_default_lock
);
1765 return current_manager
;
1769 * Routine: memory_manager_default_check
1772 * Check whether a default memory manager has been set
1773 * up yet, or not. Returns KERN_SUCCESS if dmm exists,
1774 * and KERN_FAILURE if dmm does not exist.
1776 * If there is no default memory manager, log an error,
1777 * but only the first time.
1780 __private_extern__ kern_return_t
1781 memory_manager_default_check(void)
1783 memory_object_default_t current
;
1785 mutex_lock(&memory_manager_default_lock
);
1786 current
= memory_manager_default
;
1787 if (current
== MEMORY_OBJECT_DEFAULT_NULL
) {
1788 static boolean_t logged
; /* initialized to 0 */
1789 boolean_t complain
= !logged
;
1791 mutex_unlock(&memory_manager_default_lock
);
1793 printf("Warning: No default memory manager\n");
1794 return(KERN_FAILURE
);
1796 mutex_unlock(&memory_manager_default_lock
);
1797 return(KERN_SUCCESS
);
1801 __private_extern__
void
1802 memory_manager_default_init(void)
1804 memory_manager_default
= MEMORY_OBJECT_DEFAULT_NULL
;
1805 mutex_init(&memory_manager_default_lock
, 0);
1810 /* Allow manipulation of individual page state. This is actually part of */
1811 /* the UPL regimen but takes place on the object rather than on a UPL */
1814 memory_object_page_op(
1815 memory_object_control_t control
,
1816 memory_object_offset_t offset
,
1818 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 vm_object_lock(object
);
1831 if(ops
& UPL_POP_PHYSICAL
) {
1832 if(object
->phys_contiguous
) {
1834 *phys_entry
= (ppnum_t
)
1835 (object
->shadow_offset
>> 12);
1837 vm_object_unlock(object
);
1838 return KERN_SUCCESS
;
1840 vm_object_unlock(object
);
1841 return KERN_INVALID_OBJECT
;
1844 if(object
->phys_contiguous
) {
1845 vm_object_unlock(object
);
1846 return KERN_INVALID_OBJECT
;
1850 if((dst_page
= vm_page_lookup(object
,offset
)) == VM_PAGE_NULL
) {
1851 vm_object_unlock(object
);
1852 return KERN_FAILURE
;
1855 /* Sync up on getting the busy bit */
1856 if((dst_page
->busy
|| dst_page
->cleaning
) &&
1857 (((ops
& UPL_POP_SET
) &&
1858 (ops
& UPL_POP_BUSY
)) || (ops
& UPL_POP_DUMP
))) {
1859 /* someone else is playing with the page, we will */
1861 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
1865 if (ops
& UPL_POP_DUMP
) {
1866 vm_page_lock_queues();
1868 if (dst_page
->no_isync
== FALSE
)
1869 pmap_disconnect(dst_page
->phys_page
);
1870 vm_page_free(dst_page
);
1872 vm_page_unlock_queues();
1879 /* Get the condition of flags before requested ops */
1880 /* are undertaken */
1882 if(dst_page
->dirty
) *flags
|= UPL_POP_DIRTY
;
1883 if(dst_page
->pageout
) *flags
|= UPL_POP_PAGEOUT
;
1884 if(dst_page
->precious
) *flags
|= UPL_POP_PRECIOUS
;
1885 if(dst_page
->absent
) *flags
|= UPL_POP_ABSENT
;
1886 if(dst_page
->busy
) *flags
|= UPL_POP_BUSY
;
1889 /* The caller should have made a call either contingent with */
1890 /* or prior to this call to set UPL_POP_BUSY */
1891 if(ops
& UPL_POP_SET
) {
1892 /* The protection granted with this assert will */
1893 /* not be complete. If the caller violates the */
1894 /* convention and attempts to change page state */
1895 /* without first setting busy we may not see it */
1896 /* because the page may already be busy. However */
1897 /* if such violations occur we will assert sooner */
1899 assert(dst_page
->busy
|| (ops
& UPL_POP_BUSY
));
1900 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= TRUE
;
1901 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= TRUE
;
1902 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= TRUE
;
1903 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= TRUE
;
1904 if (ops
& UPL_POP_BUSY
) dst_page
->busy
= TRUE
;
1907 if(ops
& UPL_POP_CLR
) {
1908 assert(dst_page
->busy
);
1909 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= FALSE
;
1910 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= FALSE
;
1911 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= FALSE
;
1912 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= FALSE
;
1913 if (ops
& UPL_POP_BUSY
) {
1914 dst_page
->busy
= FALSE
;
1915 PAGE_WAKEUP(dst_page
);
1919 if (dst_page
->encrypted
) {
1922 * We need to decrypt this encrypted page before the
1923 * caller can access its contents.
1924 * But if the caller really wants to access the page's
1925 * contents, they have to keep the page "busy".
1926 * Otherwise, the page could get recycled or re-encrypted
1929 if ((ops
& UPL_POP_SET
) && (ops
& UPL_POP_BUSY
) &&
1932 * The page is stable enough to be accessed by
1933 * the caller, so make sure its contents are
1936 vm_page_decrypt(dst_page
, 0);
1939 * The page is not busy, so don't bother
1940 * decrypting it, since anything could
1941 * happen to it between now and when the
1942 * caller wants to access it.
1943 * We should not give the caller access
1946 assert(!phys_entry
);
1952 * The physical page number will remain valid
1953 * only if the page is kept busy.
1954 * ENCRYPTED SWAP: make sure we don't let the
1955 * caller access an encrypted page.
1957 assert(dst_page
->busy
);
1958 assert(!dst_page
->encrypted
);
1959 *phys_entry
= dst_page
->phys_page
;
1965 vm_object_unlock(object
);
1966 return KERN_SUCCESS
;
1971 * memory_object_range_op offers performance enhancement over
1972 * memory_object_page_op for page_op functions which do not require page
1973 * level state to be returned from the call. Page_op was created to provide
1974 * a low-cost alternative to page manipulation via UPLs when only a single
1975 * page was involved. The range_op call establishes the ability in the _op
1976 * family of functions to work on multiple pages where the lack of page level
1977 * state handling allows the caller to avoid the overhead of the upl structures.
1981 memory_object_range_op(
1982 memory_object_control_t control
,
1983 memory_object_offset_t offset_beg
,
1984 memory_object_offset_t offset_end
,
1988 memory_object_offset_t offset
;
1992 object
= memory_object_control_to_vm_object(control
);
1993 if (object
== VM_OBJECT_NULL
)
1994 return (KERN_INVALID_ARGUMENT
);
1996 if (object
->resident_page_count
== 0) {
1998 if (ops
& UPL_ROP_PRESENT
)
2001 *range
= offset_end
- offset_beg
;
2003 return KERN_SUCCESS
;
2005 vm_object_lock(object
);
2007 if (object
->phys_contiguous
) {
2008 vm_object_unlock(object
);
2009 return KERN_INVALID_OBJECT
;
2012 offset
= offset_beg
;
2014 while (offset
< offset_end
) {
2015 dst_page
= vm_page_lookup(object
, offset
);
2016 if (dst_page
!= VM_PAGE_NULL
) {
2017 if (ops
& UPL_ROP_DUMP
) {
2018 if (dst_page
->busy
|| dst_page
->cleaning
) {
2020 * someone else is playing with the
2021 * page, we will have to wait
2024 dst_page
, THREAD_UNINT
);
2026 * need to relook the page up since it's
2027 * state may have changed while we slept
2028 * it might even belong to a different object
2033 vm_page_lock_queues();
2035 if (dst_page
->no_isync
== FALSE
)
2036 pmap_disconnect(dst_page
->phys_page
);
2037 vm_page_free(dst_page
);
2039 vm_page_unlock_queues();
2040 } else if (ops
& UPL_ROP_ABSENT
)
2042 } else if (ops
& UPL_ROP_PRESENT
)
2045 offset
+= PAGE_SIZE
;
2047 vm_object_unlock(object
);
2050 *range
= offset
- offset_beg
;
2052 return KERN_SUCCESS
;
2057 memory_object_pages_resident(
2058 memory_object_control_t control
,
2059 boolean_t
* has_pages_resident
)
2063 *has_pages_resident
= FALSE
;
2065 object
= memory_object_control_to_vm_object(control
);
2066 if (object
== VM_OBJECT_NULL
)
2067 return (KERN_INVALID_ARGUMENT
);
2069 if (object
->resident_page_count
)
2070 *has_pages_resident
= TRUE
;
2072 return (KERN_SUCCESS
);
2076 static zone_t mem_obj_control_zone
;
2078 __private_extern__
void
2079 memory_object_control_bootstrap(void)
2083 i
= (vm_size_t
) sizeof (struct memory_object_control
);
2084 mem_obj_control_zone
= zinit (i
, 8192*i
, 4096, "mem_obj_control");
2088 __private_extern__ memory_object_control_t
2089 memory_object_control_allocate(
2092 memory_object_control_t control
;
2094 control
= (memory_object_control_t
)zalloc(mem_obj_control_zone
);
2095 if (control
!= MEMORY_OBJECT_CONTROL_NULL
)
2096 control
->object
= object
;
2100 __private_extern__
void
2101 memory_object_control_collapse(
2102 memory_object_control_t control
,
2105 assert((control
->object
!= VM_OBJECT_NULL
) &&
2106 (control
->object
!= object
));
2107 control
->object
= object
;
2110 __private_extern__ vm_object_t
2111 memory_object_control_to_vm_object(
2112 memory_object_control_t control
)
2114 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2115 return VM_OBJECT_NULL
;
2117 return (control
->object
);
2120 memory_object_control_t
2121 convert_port_to_mo_control(
2122 __unused mach_port_t port
)
2124 return MEMORY_OBJECT_CONTROL_NULL
;
2129 convert_mo_control_to_port(
2130 __unused memory_object_control_t control
)
2132 return MACH_PORT_NULL
;
2136 memory_object_control_reference(
2137 __unused memory_object_control_t control
)
2143 * We only every issue one of these references, so kill it
2144 * when that gets released (should switch the real reference
2145 * counting in true port-less EMMI).
2148 memory_object_control_deallocate(
2149 memory_object_control_t control
)
2151 zfree(mem_obj_control_zone
, control
);
2155 memory_object_control_disable(
2156 memory_object_control_t control
)
2158 assert(control
->object
!= VM_OBJECT_NULL
);
2159 control
->object
= VM_OBJECT_NULL
;
2163 memory_object_default_reference(
2164 memory_object_default_t dmm
)
2166 ipc_port_make_send(dmm
);
2170 memory_object_default_deallocate(
2171 memory_object_default_t dmm
)
2173 ipc_port_release_send(dmm
);
2177 convert_port_to_memory_object(
2178 __unused mach_port_t port
)
2180 return (MEMORY_OBJECT_NULL
);
2185 convert_memory_object_to_port(
2186 __unused memory_object_t object
)
2188 return (MACH_PORT_NULL
);
2192 /* Routine memory_object_reference */
2193 void memory_object_reference(
2194 memory_object_t memory_object
)
2198 if (memory_object
->pager
== &vnode_pager_workaround
) {
2199 vnode_pager_reference(memory_object
);
2200 } else if (memory_object
->pager
== &device_pager_workaround
) {
2201 device_pager_reference(memory_object
);
2204 dp_memory_object_reference(memory_object
);
2207 /* Routine memory_object_deallocate */
2208 void memory_object_deallocate(
2209 memory_object_t memory_object
)
2213 if (memory_object
->pager
== &vnode_pager_workaround
) {
2214 vnode_pager_deallocate(memory_object
);
2215 } else if (memory_object
->pager
== &device_pager_workaround
) {
2216 device_pager_deallocate(memory_object
);
2219 dp_memory_object_deallocate(memory_object
);
2223 /* Routine memory_object_init */
2224 kern_return_t memory_object_init
2226 memory_object_t memory_object
,
2227 memory_object_control_t memory_control
,
2228 memory_object_cluster_size_t memory_object_page_size
2232 if (memory_object
->pager
== &vnode_pager_workaround
) {
2233 return vnode_pager_init(memory_object
,
2235 memory_object_page_size
);
2236 } else if (memory_object
->pager
== &device_pager_workaround
) {
2237 return device_pager_init(memory_object
,
2239 memory_object_page_size
);
2242 return dp_memory_object_init(memory_object
,
2244 memory_object_page_size
);
2247 /* Routine memory_object_terminate */
2248 kern_return_t memory_object_terminate
2250 memory_object_t memory_object
2254 if (memory_object
->pager
== &vnode_pager_workaround
) {
2255 return vnode_pager_terminate(memory_object
);
2256 } else if (memory_object
->pager
== &device_pager_workaround
) {
2257 return device_pager_terminate(memory_object
);
2260 return dp_memory_object_terminate(memory_object
);
2263 /* Routine memory_object_data_request */
2264 kern_return_t memory_object_data_request
2266 memory_object_t memory_object
,
2267 memory_object_offset_t offset
,
2268 memory_object_cluster_size_t length
,
2269 vm_prot_t desired_access
2273 if (memory_object
->pager
== &vnode_pager_workaround
) {
2274 return vnode_pager_data_request(memory_object
,
2278 } else if (memory_object
->pager
== &device_pager_workaround
) {
2279 return device_pager_data_request(memory_object
,
2285 return dp_memory_object_data_request(memory_object
,
2291 /* Routine memory_object_data_return */
2292 kern_return_t memory_object_data_return
2294 memory_object_t memory_object
,
2295 memory_object_offset_t offset
,
2297 memory_object_offset_t
*resid_offset
,
2300 boolean_t kernel_copy
,
2305 if (memory_object
->pager
== &vnode_pager_workaround
) {
2306 return vnode_pager_data_return(memory_object
,
2314 } else if (memory_object
->pager
== &device_pager_workaround
) {
2316 return device_pager_data_return(memory_object
,
2326 return dp_memory_object_data_return(memory_object
,
2337 /* Routine memory_object_data_initialize */
2338 kern_return_t memory_object_data_initialize
2340 memory_object_t memory_object
,
2341 memory_object_offset_t offset
,
2346 if (memory_object
->pager
== &vnode_pager_workaround
) {
2347 return vnode_pager_data_initialize(memory_object
,
2350 } else if (memory_object
->pager
== &device_pager_workaround
) {
2351 return device_pager_data_initialize(memory_object
,
2356 return dp_memory_object_data_initialize(memory_object
,
2361 /* Routine memory_object_data_unlock */
2362 kern_return_t memory_object_data_unlock
2364 memory_object_t memory_object
,
2365 memory_object_offset_t offset
,
2367 vm_prot_t desired_access
2371 if (memory_object
->pager
== &vnode_pager_workaround
) {
2372 return vnode_pager_data_unlock(memory_object
,
2376 } else if (memory_object
->pager
== &device_pager_workaround
) {
2377 return device_pager_data_unlock(memory_object
,
2383 return dp_memory_object_data_unlock(memory_object
,
2389 /* Routine memory_object_synchronize */
2390 kern_return_t memory_object_synchronize
2392 memory_object_t memory_object
,
2393 memory_object_offset_t offset
,
2395 vm_sync_t sync_flags
2399 if (memory_object
->pager
== &vnode_pager_workaround
) {
2400 return vnode_pager_synchronize(memory_object
,
2404 } else if (memory_object
->pager
== &device_pager_workaround
) {
2405 return device_pager_synchronize(memory_object
,
2411 return dp_memory_object_synchronize(memory_object
,
2417 /* Routine memory_object_unmap */
2418 kern_return_t memory_object_unmap
2420 memory_object_t memory_object
2424 if (memory_object
->pager
== &vnode_pager_workaround
) {
2425 return vnode_pager_unmap(memory_object
);
2426 } else if (memory_object
->pager
== &device_pager_workaround
) {
2427 return device_pager_unmap(memory_object
);
2430 return dp_memory_object_unmap(memory_object
);
2433 /* Routine memory_object_create */
2434 kern_return_t memory_object_create
2436 memory_object_default_t default_memory_manager
,
2437 vm_size_t new_memory_object_size
,
2438 memory_object_t
*new_memory_object
2441 return default_pager_memory_object_create(default_memory_manager
,
2442 new_memory_object_size
,
2447 convert_port_to_upl(
2453 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UPL
)) {
2457 upl
= (upl_t
) port
->ip_kobject
;
2466 convert_upl_to_port(
2469 return MACH_PORT_NULL
;
2472 __private_extern__
void
2474 __unused ipc_port_t port
,
2475 __unused mach_port_mscount_t mscount
)