2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: vm/memory_object.c
60 * Author: Michael Wayne Young
62 * External memory management interface control functions.
66 * Interface dependencies:
69 #include <mach/std_types.h> /* For pointer_t */
70 #include <mach/mach_types.h>
73 #include <mach/kern_return.h>
74 #include <mach/memory_object.h>
75 #include <mach/memory_object_default.h>
76 #include <mach/memory_object_control_server.h>
77 #include <mach/host_priv_server.h>
78 #include <mach/boolean.h>
79 #include <mach/vm_prot.h>
80 #include <mach/message.h>
83 * Implementation dependencies:
85 #include <string.h> /* For memcpy() */
88 #include <kern/host.h>
89 #include <kern/thread.h> /* For current_thread() */
90 #include <kern/ipc_mig.h>
91 #include <kern/misc_protos.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_fault.h>
95 #include <vm/memory_object.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_pageout.h>
98 #include <vm/pmap.h> /* For pmap_clear_modify */
99 #include <vm/vm_kern.h> /* For kernel_map, vm_move */
100 #include <vm/vm_map.h> /* For vm_map_pageable */
101 #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
102 #include <vm/vm_shared_region.h>
104 #include <vm/vm_external.h>
106 #include <vm/vm_protos.h>
109 memory_object_default_t memory_manager_default
= MEMORY_OBJECT_DEFAULT_NULL
;
110 decl_lck_mtx_data(, memory_manager_default_lock
)
114 * Routine: memory_object_should_return_page
117 * Determine whether the given page should be returned,
118 * based on the page's state and on the given return policy.
120 * We should return the page if one of the following is true:
122 * 1. Page is dirty and should_return is not RETURN_NONE.
123 * 2. Page is precious and should_return is RETURN_ALL.
124 * 3. Should_return is RETURN_ANYTHING.
126 * As a side effect, m->dirty will be made consistent
127 * with pmap_is_modified(m), if should_return is not
128 * MEMORY_OBJECT_RETURN_NONE.
131 #define memory_object_should_return_page(m, should_return) \
132 (should_return != MEMORY_OBJECT_RETURN_NONE && \
133 (((m)->dirty || ((m)->dirty = pmap_is_modified((m)->phys_page))) || \
134 ((m)->precious && (should_return) == MEMORY_OBJECT_RETURN_ALL) || \
135 (should_return) == MEMORY_OBJECT_RETURN_ANYTHING))
137 typedef int memory_object_lock_result_t
;
139 #define MEMORY_OBJECT_LOCK_RESULT_DONE 0
140 #define MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK 1
141 #define MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN 2
142 #define MEMORY_OBJECT_LOCK_RESULT_MUST_FREE 3
144 memory_object_lock_result_t
memory_object_lock_page(
146 memory_object_return_t should_return
,
147 boolean_t should_flush
,
151 * Routine: memory_object_lock_page
154 * Perform the appropriate lock operations on the
155 * given page. See the description of
156 * "memory_object_lock_request" for the meanings
159 * Returns an indication that the operation
160 * completed, blocked, or that the page must
163 memory_object_lock_result_t
164 memory_object_lock_page(
166 memory_object_return_t should_return
,
167 boolean_t should_flush
,
170 XPR(XPR_MEMORY_OBJECT
,
171 "m_o_lock_page, page 0x%X rtn %d flush %d prot %d\n",
172 m
, should_return
, should_flush
, prot
, 0);
175 if (m
->busy
|| m
->cleaning
)
176 return (MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK
);
179 vm_pageout_steal_laundry(m
, FALSE
);
182 * Don't worry about pages for which the kernel
183 * does not have any data.
185 if (m
->absent
|| m
->error
|| m
->restart
) {
186 if (m
->error
&& should_flush
&& !VM_PAGE_WIRED(m
)) {
188 * dump the page, pager wants us to
189 * clean it up and there is no
190 * relevant data to return
192 return (MEMORY_OBJECT_LOCK_RESULT_MUST_FREE
);
194 return (MEMORY_OBJECT_LOCK_RESULT_DONE
);
196 assert(!m
->fictitious
);
198 if (VM_PAGE_WIRED(m
)) {
200 * The page is wired... just clean or return the page if needed.
201 * Wired pages don't get flushed or disconnected from the pmap.
203 if (memory_object_should_return_page(m
, should_return
))
204 return (MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN
);
206 return (MEMORY_OBJECT_LOCK_RESULT_DONE
);
211 * must do the pmap_disconnect before determining the
212 * need to return the page... otherwise it's possible
213 * for the page to go from the clean to the dirty state
214 * after we've made our decision
216 if (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
) {
217 SET_PAGE_DIRTY(m
, FALSE
);
221 * If we are decreasing permission, do it now;
222 * let the fault handler take care of increases
223 * (pmap_page_protect may not increase protection).
225 if (prot
!= VM_PROT_NO_CHANGE
)
226 pmap_page_protect(m
->phys_page
, VM_PROT_ALL
& ~prot
);
229 * Handle returning dirty or precious pages
231 if (memory_object_should_return_page(m
, should_return
)) {
233 * we use to do a pmap_disconnect here in support
234 * of memory_object_lock_request, but that routine
235 * no longer requires this... in any event, in
236 * our world, it would turn into a big noop since
237 * we don't lock the page in any way and as soon
238 * as we drop the object lock, the page can be
239 * faulted back into an address space
242 * pmap_disconnect(m->phys_page);
244 return (MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN
);
248 * Handle flushing clean pages
251 return (MEMORY_OBJECT_LOCK_RESULT_MUST_FREE
);
254 * we use to deactivate clean pages at this point,
255 * but we do not believe that an msync should change
256 * the 'age' of a page in the cache... here is the
257 * original comment and code concerning this...
259 * XXX Make clean but not flush a paging hint,
260 * and deactivate the pages. This is a hack
261 * because it overloads flush/clean with
262 * implementation-dependent meaning. This only
263 * happens to pages that are already clean.
265 * if (vm_page_deactivate_hint && (should_return != MEMORY_OBJECT_RETURN_NONE))
266 * return (MEMORY_OBJECT_LOCK_RESULT_MUST_DEACTIVATE);
269 return (MEMORY_OBJECT_LOCK_RESULT_DONE
);
275 * Routine: memory_object_lock_request [user interface]
278 * Control use of the data associated with the given
279 * memory object. For each page in the given range,
280 * perform the following operations, in order:
281 * 1) restrict access to the page (disallow
282 * forms specified by "prot");
283 * 2) return data to the manager (if "should_return"
284 * is RETURN_DIRTY and the page is dirty, or
285 * "should_return" is RETURN_ALL and the page
286 * is either dirty or precious); and,
287 * 3) flush the cached copy (if "should_flush"
289 * The set of pages is defined by a starting offset
290 * ("offset") and size ("size"). Only pages with the
291 * same page alignment as the starting offset are
294 * A single acknowledgement is sent (to the "reply_to"
295 * port) when these actions are complete. If successful,
296 * the naked send right for reply_to is consumed.
300 memory_object_lock_request(
301 memory_object_control_t control
,
302 memory_object_offset_t offset
,
303 memory_object_size_t size
,
304 memory_object_offset_t
* resid_offset
,
306 memory_object_return_t should_return
,
313 * Check for bogus arguments.
315 object
= memory_object_control_to_vm_object(control
);
316 if (object
== VM_OBJECT_NULL
)
317 return (KERN_INVALID_ARGUMENT
);
319 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
320 return (KERN_INVALID_ARGUMENT
);
322 size
= round_page_64(size
);
325 * Lock the object, and acquire a paging reference to
326 * prevent the memory_object reference from being released.
328 vm_object_lock(object
);
329 vm_object_paging_begin(object
);
331 if (flags
& MEMORY_OBJECT_DATA_FLUSH_ALL
) {
332 if ((should_return
!= MEMORY_OBJECT_RETURN_NONE
) || offset
|| object
->copy
) {
333 flags
&= ~MEMORY_OBJECT_DATA_FLUSH_ALL
;
334 flags
|= MEMORY_OBJECT_DATA_FLUSH
;
337 offset
-= object
->paging_offset
;
339 if (flags
& MEMORY_OBJECT_DATA_FLUSH_ALL
)
340 vm_object_reap_pages(object
, REAP_DATA_FLUSH
);
342 (void)vm_object_update(object
, offset
, size
, resid_offset
,
343 io_errno
, should_return
, flags
, prot
);
345 vm_object_paging_end(object
);
346 vm_object_unlock(object
);
348 return (KERN_SUCCESS
);
352 * memory_object_release_name: [interface]
354 * Enforces name semantic on memory_object reference count decrement
355 * This routine should not be called unless the caller holds a name
356 * reference gained through the memory_object_named_create or the
357 * memory_object_rename call.
358 * If the TERMINATE_IDLE flag is set, the call will return if the
359 * reference count is not 1. i.e. idle with the only remaining reference
361 * If the decision is made to proceed the name field flag is set to
362 * false and the reference count is decremented. If the RESPECT_CACHE
363 * flag is set and the reference count has gone to zero, the
364 * memory_object is checked to see if it is cacheable otherwise when
365 * the reference count is zero, it is simply terminated.
369 memory_object_release_name(
370 memory_object_control_t control
,
375 object
= memory_object_control_to_vm_object(control
);
376 if (object
== VM_OBJECT_NULL
)
377 return (KERN_INVALID_ARGUMENT
);
379 return vm_object_release_name(object
, flags
);
385 * Routine: memory_object_destroy [user interface]
387 * Shut down a memory object, despite the
388 * presence of address map (or other) references
392 memory_object_destroy(
393 memory_object_control_t control
,
394 kern_return_t reason
)
398 object
= memory_object_control_to_vm_object(control
);
399 if (object
== VM_OBJECT_NULL
)
400 return (KERN_INVALID_ARGUMENT
);
402 return (vm_object_destroy(object
, reason
));
406 * Routine: vm_object_sync
408 * Kernel internal function to synch out pages in a given
409 * range within an object to its memory manager. Much the
410 * same as memory_object_lock_request but page protection
413 * If the should_flush and should_return flags are true pages
414 * are flushed, that is dirty & precious pages are written to
415 * the memory manager and then discarded. If should_return
416 * is false, only precious pages are returned to the memory
419 * If should flush is false and should_return true, the memory
420 * manager's copy of the pages is updated. If should_return
421 * is also false, only the precious pages are updated. This
422 * last option is of limited utility.
425 * FALSE if no pages were returned to the pager
432 vm_object_offset_t offset
,
433 vm_object_size_t size
,
434 boolean_t should_flush
,
435 boolean_t should_return
,
436 boolean_t should_iosync
)
442 "vm_o_sync, object 0x%X, offset 0x%X size 0x%x flush %d rtn %d\n",
443 object
, offset
, size
, should_flush
, should_return
);
446 * Lock the object, and acquire a paging reference to
447 * prevent the memory_object and control ports from
450 vm_object_lock(object
);
451 vm_object_paging_begin(object
);
454 flags
= MEMORY_OBJECT_DATA_FLUSH
;
456 * This flush is from an msync(), not a truncate(), so the
457 * contents of the file are not affected.
458 * MEMORY_OBECT_DATA_NO_CHANGE lets vm_object_update() know
459 * that the data is not changed and that there's no need to
460 * push the old contents to a copy object.
462 flags
|= MEMORY_OBJECT_DATA_NO_CHANGE
;
467 flags
|= MEMORY_OBJECT_IO_SYNC
;
469 rv
= vm_object_update(object
, offset
, (vm_object_size_t
)size
, NULL
, NULL
,
471 MEMORY_OBJECT_RETURN_ALL
:
472 MEMORY_OBJECT_RETURN_NONE
,
477 vm_object_paging_end(object
);
478 vm_object_unlock(object
);
484 #define LIST_REQ_PAGEOUT_PAGES(object, data_cnt, po, ro, ioerr, iosync) \
488 memory_object_t pager; \
490 if (object->object_slid) { \
491 panic("Objects with slid pages not allowed\n"); \
494 if ((pager = (object)->pager) != MEMORY_OBJECT_NULL) { \
495 vm_object_paging_begin(object); \
496 vm_object_unlock(object); \
499 upl_flags = UPL_MSYNC | UPL_IOSYNC; \
501 upl_flags = UPL_MSYNC; \
503 (void) memory_object_data_return(pager, \
505 (memory_object_cluster_size_t)data_cnt, \
512 vm_object_lock(object); \
513 vm_object_paging_end(object); \
520 vm_object_update_extent(
522 vm_object_offset_t offset
,
523 vm_object_offset_t offset_end
,
524 vm_object_offset_t
*offset_resid
,
526 boolean_t should_flush
,
527 memory_object_return_t should_return
,
528 boolean_t should_iosync
,
533 vm_object_offset_t paging_offset
= 0;
534 vm_object_offset_t next_offset
= offset
;
535 memory_object_lock_result_t page_lock_result
;
536 memory_object_cluster_size_t data_cnt
= 0;
537 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
538 struct vm_page_delayed_work
*dwp
;
544 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
547 offset
< offset_end
&& object
->resident_page_count
;
548 offset
+= PAGE_SIZE_64
) {
551 * Limit the number of pages to be cleaned at once to a contiguous
552 * run, or at most MAX_UPL_TRANSFER_BYTES
555 if ((data_cnt
>= MAX_UPL_TRANSFER_BYTES
) || (next_offset
!= offset
)) {
558 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
562 LIST_REQ_PAGEOUT_PAGES(object
, data_cnt
,
563 paging_offset
, offset_resid
, io_errno
, should_iosync
);
567 while ((m
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
571 page_lock_result
= memory_object_lock_page(m
, should_return
, should_flush
, prot
);
573 if (data_cnt
&& page_lock_result
!= MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN
) {
575 * End of a run of dirty/precious pages.
578 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
582 LIST_REQ_PAGEOUT_PAGES(object
, data_cnt
,
583 paging_offset
, offset_resid
, io_errno
, should_iosync
);
585 * LIST_REQ_PAGEOUT_PAGES will drop the object lock which will
586 * allow the state of page 'm' to change... we need to re-lookup
593 switch (page_lock_result
) {
595 case MEMORY_OBJECT_LOCK_RESULT_DONE
:
598 case MEMORY_OBJECT_LOCK_RESULT_MUST_FREE
:
599 dwp
->dw_mask
|= DW_vm_page_free
;
602 case MEMORY_OBJECT_LOCK_RESULT_MUST_BLOCK
:
603 PAGE_SLEEP(object
, m
, THREAD_UNINT
);
606 case MEMORY_OBJECT_LOCK_RESULT_MUST_RETURN
:
608 paging_offset
= offset
;
610 data_cnt
+= PAGE_SIZE
;
611 next_offset
= offset
+ PAGE_SIZE_64
;
614 * wired pages shouldn't be flushed and
615 * since they aren't on any queue,
616 * no need to remove them
618 if (!VM_PAGE_WIRED(m
)) {
622 * add additional state for the flush
627 * we use to remove the page from the queues at this
628 * point, but we do not believe that an msync
629 * should cause the 'age' of a page to be changed
632 * dwp->dw_mask |= DW_VM_PAGE_QUEUES_REMOVE;
639 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
, dw_count
);
641 if (dw_count
>= dw_limit
) {
642 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
651 * We have completed the scan for applicable pages.
652 * Clean any pages that have been saved.
655 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
658 LIST_REQ_PAGEOUT_PAGES(object
, data_cnt
,
659 paging_offset
, offset_resid
, io_errno
, should_iosync
);
667 * Routine: vm_object_update
669 * Work function for m_o_lock_request(), vm_o_sync().
671 * Called with object locked and paging ref taken.
676 vm_object_offset_t offset
,
677 vm_object_size_t size
,
678 vm_object_offset_t
*resid_offset
,
680 memory_object_return_t should_return
,
682 vm_prot_t protection
)
684 vm_object_t copy_object
= VM_OBJECT_NULL
;
685 boolean_t data_returned
= FALSE
;
686 boolean_t update_cow
;
687 boolean_t should_flush
= (flags
& MEMORY_OBJECT_DATA_FLUSH
) ? TRUE
: FALSE
;
688 boolean_t should_iosync
= (flags
& MEMORY_OBJECT_IO_SYNC
) ? TRUE
: FALSE
;
689 vm_fault_return_t result
;
692 #define MAX_EXTENTS 8
693 #define EXTENT_SIZE (1024 * 1024 * 256)
694 #define RESIDENT_LIMIT (1024 * 32)
696 vm_object_offset_t e_base
;
697 vm_object_offset_t e_min
;
698 vm_object_offset_t e_max
;
699 } extents
[MAX_EXTENTS
];
702 * To avoid blocking while scanning for pages, save
703 * dirty pages to be cleaned all at once.
705 * XXXO A similar strategy could be used to limit the
706 * number of times that a scan must be restarted for
707 * other reasons. Those pages that would require blocking
708 * could be temporarily collected in another list, or
709 * their offsets could be recorded in a small array.
713 * XXX NOTE: May want to consider converting this to a page list
714 * XXX vm_map_copy interface. Need to understand object
715 * XXX coalescing implications before doing so.
718 update_cow
= ((flags
& MEMORY_OBJECT_DATA_FLUSH
)
719 && (!(flags
& MEMORY_OBJECT_DATA_NO_CHANGE
) &&
720 !(flags
& MEMORY_OBJECT_DATA_PURGE
)))
721 || (flags
& MEMORY_OBJECT_COPY_SYNC
);
723 if (update_cow
|| (flags
& (MEMORY_OBJECT_DATA_PURGE
| MEMORY_OBJECT_DATA_SYNC
))) {
726 while ((copy_object
= object
->copy
) != VM_OBJECT_NULL
) {
728 * need to do a try here since we're swimming upstream
729 * against the normal lock ordering... however, we need
730 * to hold the object stable until we gain control of the
731 * copy object so we have to be careful how we approach this
733 if (vm_object_lock_try(copy_object
)) {
735 * we 'won' the lock on the copy object...
736 * no need to hold the object lock any longer...
737 * take a real reference on the copy object because
738 * we're going to call vm_fault_page on it which may
739 * under certain conditions drop the lock and the paging
740 * reference we're about to take... the reference
741 * will keep the copy object from going away if that happens
743 vm_object_unlock(object
);
744 vm_object_reference_locked(copy_object
);
747 vm_object_unlock(object
);
750 mutex_pause(collisions
);
752 vm_object_lock(object
);
755 if ((copy_object
!= VM_OBJECT_NULL
&& update_cow
) || (flags
& MEMORY_OBJECT_DATA_SYNC
)) {
757 vm_map_size_t copy_size
;
758 vm_map_offset_t copy_offset
;
762 kern_return_t error
= 0;
763 struct vm_object_fault_info fault_info
;
765 if (copy_object
!= VM_OBJECT_NULL
) {
767 * translate offset with respect to shadow's offset
769 copy_offset
= (offset
>= copy_object
->vo_shadow_offset
) ?
770 (vm_map_offset_t
)(offset
- copy_object
->vo_shadow_offset
) :
773 if (copy_offset
> copy_object
->vo_size
)
774 copy_offset
= copy_object
->vo_size
;
777 * clip size with respect to shadow offset
779 if (offset
>= copy_object
->vo_shadow_offset
) {
781 } else if (size
>= copy_object
->vo_shadow_offset
- offset
) {
782 copy_size
= size
- (copy_object
->vo_shadow_offset
- offset
);
787 if (copy_offset
+ copy_size
> copy_object
->vo_size
) {
788 if (copy_object
->vo_size
>= copy_offset
) {
789 copy_size
= copy_object
->vo_size
- copy_offset
;
794 copy_size
+=copy_offset
;
797 copy_object
= object
;
799 copy_size
= offset
+ size
;
800 copy_offset
= offset
;
802 fault_info
.interruptible
= THREAD_UNINT
;
803 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
804 fault_info
.user_tag
= 0;
805 fault_info
.pmap_options
= 0;
806 fault_info
.lo_offset
= copy_offset
;
807 fault_info
.hi_offset
= copy_size
;
808 fault_info
.no_cache
= FALSE
;
809 fault_info
.stealth
= TRUE
;
810 fault_info
.io_sync
= FALSE
;
811 fault_info
.cs_bypass
= FALSE
;
812 fault_info
.mark_zf_absent
= FALSE
;
813 fault_info
.batch_pmap_op
= FALSE
;
815 vm_object_paging_begin(copy_object
);
817 for (i
= copy_offset
; i
< copy_size
; i
+= PAGE_SIZE
) {
818 RETRY_COW_OF_LOCK_REQUEST
:
819 fault_info
.cluster_size
= (vm_size_t
) (copy_size
- i
);
820 assert(fault_info
.cluster_size
== copy_size
- i
);
822 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
824 result
= vm_fault_page(copy_object
, i
,
825 VM_PROT_WRITE
|VM_PROT_READ
,
827 FALSE
, /* page not looked up */
837 case VM_FAULT_SUCCESS
:
840 page
->object
, top_page
);
841 vm_object_lock(copy_object
);
842 vm_object_paging_begin(copy_object
);
847 vm_page_lockspin_queues();
851 vm_page_deactivate(page
);
852 vm_page_unlock_queues();
854 PAGE_WAKEUP_DONE(page
);
857 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
858 vm_object_lock(copy_object
);
859 vm_object_paging_begin(copy_object
);
860 goto RETRY_COW_OF_LOCK_REQUEST
;
861 case VM_FAULT_INTERRUPTED
:
862 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
863 vm_object_lock(copy_object
);
864 vm_object_paging_begin(copy_object
);
865 goto RETRY_COW_OF_LOCK_REQUEST
;
866 case VM_FAULT_MEMORY_SHORTAGE
:
868 prot
= VM_PROT_WRITE
|VM_PROT_READ
;
869 vm_object_lock(copy_object
);
870 vm_object_paging_begin(copy_object
);
871 goto RETRY_COW_OF_LOCK_REQUEST
;
872 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
873 /* success but no VM page: fail */
874 vm_object_paging_end(copy_object
);
875 vm_object_unlock(copy_object
);
877 case VM_FAULT_MEMORY_ERROR
:
878 if (object
!= copy_object
)
879 vm_object_deallocate(copy_object
);
880 vm_object_lock(object
);
881 goto BYPASS_COW_COPYIN
;
883 panic("vm_object_update: unexpected error 0x%x"
884 " from vm_fault_page()\n", result
);
888 vm_object_paging_end(copy_object
);
890 if ((flags
& (MEMORY_OBJECT_DATA_SYNC
| MEMORY_OBJECT_COPY_SYNC
))) {
891 if (copy_object
!= VM_OBJECT_NULL
&& copy_object
!= object
) {
892 vm_object_unlock(copy_object
);
893 vm_object_deallocate(copy_object
);
894 vm_object_lock(object
);
898 if (copy_object
!= VM_OBJECT_NULL
&& copy_object
!= object
) {
899 if ((flags
& MEMORY_OBJECT_DATA_PURGE
)) {
900 copy_object
->shadow_severed
= TRUE
;
901 copy_object
->shadowed
= FALSE
;
902 copy_object
->shadow
= NULL
;
904 * delete the ref the COW was holding on the target object
906 vm_object_deallocate(object
);
908 vm_object_unlock(copy_object
);
909 vm_object_deallocate(copy_object
);
910 vm_object_lock(object
);
915 * when we have a really large range to check relative
916 * to the number of actual resident pages, we'd like
917 * to use the resident page list to drive our checks
918 * however, the object lock will get dropped while processing
919 * the page which means the resident queue can change which
920 * means we can't walk the queue as we process the pages
921 * we also want to do the processing in offset order to allow
922 * 'runs' of pages to be collected if we're being told to
923 * flush to disk... the resident page queue is NOT ordered.
925 * a temporary solution (until we figure out how to deal with
926 * large address spaces more generically) is to pre-flight
927 * the resident page queue (if it's small enough) and develop
928 * a collection of extents (that encompass actual resident pages)
929 * to visit. This will at least allow us to deal with some of the
930 * more pathological cases in a more efficient manner. The current
931 * worst case (a single resident page at the end of an extremely large
932 * range) can take minutes to complete for ranges in the terrabyte
933 * category... since this routine is called when truncating a file,
934 * and we currently support files up to 16 Tbytes in size, this
935 * is not a theoretical problem
938 if ((object
->resident_page_count
< RESIDENT_LIMIT
) &&
939 (atop_64(size
) > (unsigned)(object
->resident_page_count
/(8 * MAX_EXTENTS
)))) {
941 vm_object_offset_t start
;
942 vm_object_offset_t end
;
943 vm_object_size_t e_mask
;
949 e_mask
= ~((vm_object_size_t
)(EXTENT_SIZE
- 1));
951 m
= (vm_page_t
) queue_first(&object
->memq
);
953 while (!queue_end(&object
->memq
, (queue_entry_t
) m
)) {
954 next
= (vm_page_t
) queue_next(&m
->listq
);
956 if ((m
->offset
>= start
) && (m
->offset
< end
)) {
958 * this is a page we're interested in
959 * try to fit it into a current extent
961 for (n
= 0; n
< num_of_extents
; n
++) {
962 if ((m
->offset
& e_mask
) == extents
[n
].e_base
) {
964 * use (PAGE_SIZE - 1) to determine the
965 * max offset so that we don't wrap if
966 * we're at the last page of the space
968 if (m
->offset
< extents
[n
].e_min
)
969 extents
[n
].e_min
= m
->offset
;
970 else if ((m
->offset
+ (PAGE_SIZE
- 1)) > extents
[n
].e_max
)
971 extents
[n
].e_max
= m
->offset
+ (PAGE_SIZE
- 1);
975 if (n
== num_of_extents
) {
977 * didn't find a current extent that can encompass
980 if (n
< MAX_EXTENTS
) {
982 * if we still have room,
983 * create a new extent
985 extents
[n
].e_base
= m
->offset
& e_mask
;
986 extents
[n
].e_min
= m
->offset
;
987 extents
[n
].e_max
= m
->offset
+ (PAGE_SIZE
- 1);
992 * no room to create a new extent...
993 * fall back to a single extent based
994 * on the min and max page offsets
995 * we find in the range we're interested in...
996 * first, look through the extent list and
997 * develop the overall min and max for the
998 * pages we've looked at up to this point
1000 for (n
= 1; n
< num_of_extents
; n
++) {
1001 if (extents
[n
].e_min
< extents
[0].e_min
)
1002 extents
[0].e_min
= extents
[n
].e_min
;
1003 if (extents
[n
].e_max
> extents
[0].e_max
)
1004 extents
[0].e_max
= extents
[n
].e_max
;
1007 * now setup to run through the remaining pages
1008 * to determine the overall min and max
1009 * offset for the specified range
1011 extents
[0].e_base
= 0;
1016 * by continuing, we'll reprocess the
1017 * page that forced us to abandon trying
1018 * to develop multiple extents
1027 extents
[0].e_min
= offset
;
1028 extents
[0].e_max
= offset
+ (size
- 1);
1032 for (n
= 0; n
< num_of_extents
; n
++) {
1033 if (vm_object_update_extent(object
, extents
[n
].e_min
, extents
[n
].e_max
, resid_offset
, io_errno
,
1034 should_flush
, should_return
, should_iosync
, protection
))
1035 data_returned
= TRUE
;
1037 return (data_returned
);
1042 * Routine: memory_object_synchronize_completed [user interface]
1044 * Tell kernel that previously synchronized data
1045 * (memory_object_synchronize) has been queue or placed on the
1048 * Note: there may be multiple synchronize requests for a given
1049 * memory object outstanding but they will not overlap.
1053 memory_object_synchronize_completed(
1054 memory_object_control_t control
,
1055 memory_object_offset_t offset
,
1056 memory_object_size_t length
)
1061 object
= memory_object_control_to_vm_object(control
);
1063 XPR(XPR_MEMORY_OBJECT
,
1064 "m_o_sync_completed, object 0x%X, offset 0x%X length 0x%X\n",
1065 object
, offset
, length
, 0, 0);
1068 * Look for bogus arguments
1071 if (object
== VM_OBJECT_NULL
)
1072 return (KERN_INVALID_ARGUMENT
);
1074 vm_object_lock(object
);
1077 * search for sync request structure
1079 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
1080 if (msr
->offset
== offset
&& msr
->length
== length
) {
1081 queue_remove(&object
->msr_q
, msr
, msync_req_t
, msr_q
);
1084 }/* queue_iterate */
1086 if (queue_end(&object
->msr_q
, (queue_entry_t
)msr
)) {
1087 vm_object_unlock(object
);
1088 return KERN_INVALID_ARGUMENT
;
1092 vm_object_unlock(object
);
1093 msr
->flag
= VM_MSYNC_DONE
;
1095 thread_wakeup((event_t
) msr
);
1097 return KERN_SUCCESS
;
1098 }/* memory_object_synchronize_completed */
1100 static kern_return_t
1101 vm_object_set_attributes_common(
1103 boolean_t may_cache
,
1104 memory_object_copy_strategy_t copy_strategy
,
1105 boolean_t temporary
,
1106 __unused boolean_t silent_overwrite
,
1107 boolean_t advisory_pageout
)
1109 boolean_t object_became_ready
;
1111 XPR(XPR_MEMORY_OBJECT
,
1112 "m_o_set_attr_com, object 0x%X flg %x strat %d\n",
1113 object
, (may_cache
&1)|((temporary
&1)<1), copy_strategy
, 0, 0);
1115 if (object
== VM_OBJECT_NULL
)
1116 return(KERN_INVALID_ARGUMENT
);
1119 * Verify the attributes of importance
1122 switch(copy_strategy
) {
1123 case MEMORY_OBJECT_COPY_NONE
:
1124 case MEMORY_OBJECT_COPY_DELAY
:
1127 return(KERN_INVALID_ARGUMENT
);
1135 vm_object_lock(object
);
1138 * Copy the attributes
1140 assert(!object
->internal
);
1141 object_became_ready
= !object
->pager_ready
;
1142 object
->copy_strategy
= copy_strategy
;
1143 object
->can_persist
= may_cache
;
1144 object
->temporary
= temporary
;
1145 // object->silent_overwrite = silent_overwrite;
1146 object
->advisory_pageout
= advisory_pageout
;
1149 * Wake up anyone waiting for the ready attribute
1150 * to become asserted.
1153 if (object_became_ready
) {
1154 object
->pager_ready
= TRUE
;
1155 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
1158 vm_object_unlock(object
);
1160 return(KERN_SUCCESS
);
1164 * Set the memory object attribute as provided.
1166 * XXX This routine cannot be completed until the vm_msync, clean
1167 * in place, and cluster work is completed. See ifdef notyet
1168 * below and note that vm_object_set_attributes_common()
1169 * may have to be expanded.
1172 memory_object_change_attributes(
1173 memory_object_control_t control
,
1174 memory_object_flavor_t flavor
,
1175 memory_object_info_t attributes
,
1176 mach_msg_type_number_t count
)
1179 kern_return_t result
= KERN_SUCCESS
;
1180 boolean_t temporary
;
1181 boolean_t may_cache
;
1182 boolean_t invalidate
;
1183 memory_object_copy_strategy_t copy_strategy
;
1184 boolean_t silent_overwrite
;
1185 boolean_t advisory_pageout
;
1187 object
= memory_object_control_to_vm_object(control
);
1188 if (object
== VM_OBJECT_NULL
)
1189 return (KERN_INVALID_ARGUMENT
);
1191 vm_object_lock(object
);
1193 temporary
= object
->temporary
;
1194 may_cache
= object
->can_persist
;
1195 copy_strategy
= object
->copy_strategy
;
1196 // silent_overwrite = object->silent_overwrite;
1197 silent_overwrite
= FALSE
;
1198 advisory_pageout
= object
->advisory_pageout
;
1200 invalidate
= object
->invalidate
;
1202 vm_object_unlock(object
);
1205 case OLD_MEMORY_OBJECT_BEHAVIOR_INFO
:
1207 old_memory_object_behave_info_t behave
;
1209 if (count
!= OLD_MEMORY_OBJECT_BEHAVE_INFO_COUNT
) {
1210 result
= KERN_INVALID_ARGUMENT
;
1214 behave
= (old_memory_object_behave_info_t
) attributes
;
1216 temporary
= behave
->temporary
;
1217 invalidate
= behave
->invalidate
;
1218 copy_strategy
= behave
->copy_strategy
;
1223 case MEMORY_OBJECT_BEHAVIOR_INFO
:
1225 memory_object_behave_info_t behave
;
1227 if (count
!= MEMORY_OBJECT_BEHAVE_INFO_COUNT
) {
1228 result
= KERN_INVALID_ARGUMENT
;
1232 behave
= (memory_object_behave_info_t
) attributes
;
1234 temporary
= behave
->temporary
;
1235 invalidate
= behave
->invalidate
;
1236 copy_strategy
= behave
->copy_strategy
;
1237 silent_overwrite
= behave
->silent_overwrite
;
1238 advisory_pageout
= behave
->advisory_pageout
;
1242 case MEMORY_OBJECT_PERFORMANCE_INFO
:
1244 memory_object_perf_info_t perf
;
1246 if (count
!= MEMORY_OBJECT_PERF_INFO_COUNT
) {
1247 result
= KERN_INVALID_ARGUMENT
;
1251 perf
= (memory_object_perf_info_t
) attributes
;
1253 may_cache
= perf
->may_cache
;
1258 case OLD_MEMORY_OBJECT_ATTRIBUTE_INFO
:
1260 old_memory_object_attr_info_t attr
;
1262 if (count
!= OLD_MEMORY_OBJECT_ATTR_INFO_COUNT
) {
1263 result
= KERN_INVALID_ARGUMENT
;
1267 attr
= (old_memory_object_attr_info_t
) attributes
;
1269 may_cache
= attr
->may_cache
;
1270 copy_strategy
= attr
->copy_strategy
;
1275 case MEMORY_OBJECT_ATTRIBUTE_INFO
:
1277 memory_object_attr_info_t attr
;
1279 if (count
!= MEMORY_OBJECT_ATTR_INFO_COUNT
) {
1280 result
= KERN_INVALID_ARGUMENT
;
1284 attr
= (memory_object_attr_info_t
) attributes
;
1286 copy_strategy
= attr
->copy_strategy
;
1287 may_cache
= attr
->may_cache_object
;
1288 temporary
= attr
->temporary
;
1294 result
= KERN_INVALID_ARGUMENT
;
1298 if (result
!= KERN_SUCCESS
)
1301 if (copy_strategy
== MEMORY_OBJECT_COPY_TEMPORARY
) {
1302 copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
1309 * XXX may_cache may become a tri-valued variable to handle
1310 * XXX uncache if not in use.
1312 return (vm_object_set_attributes_common(object
,
1321 memory_object_get_attributes(
1322 memory_object_control_t control
,
1323 memory_object_flavor_t flavor
,
1324 memory_object_info_t attributes
, /* pointer to OUT array */
1325 mach_msg_type_number_t
*count
) /* IN/OUT */
1327 kern_return_t ret
= KERN_SUCCESS
;
1330 object
= memory_object_control_to_vm_object(control
);
1331 if (object
== VM_OBJECT_NULL
)
1332 return (KERN_INVALID_ARGUMENT
);
1334 vm_object_lock(object
);
1337 case OLD_MEMORY_OBJECT_BEHAVIOR_INFO
:
1339 old_memory_object_behave_info_t behave
;
1341 if (*count
< OLD_MEMORY_OBJECT_BEHAVE_INFO_COUNT
) {
1342 ret
= KERN_INVALID_ARGUMENT
;
1346 behave
= (old_memory_object_behave_info_t
) attributes
;
1347 behave
->copy_strategy
= object
->copy_strategy
;
1348 behave
->temporary
= object
->temporary
;
1349 #if notyet /* remove when vm_msync complies and clean in place fini */
1350 behave
->invalidate
= object
->invalidate
;
1352 behave
->invalidate
= FALSE
;
1355 *count
= OLD_MEMORY_OBJECT_BEHAVE_INFO_COUNT
;
1359 case MEMORY_OBJECT_BEHAVIOR_INFO
:
1361 memory_object_behave_info_t behave
;
1363 if (*count
< MEMORY_OBJECT_BEHAVE_INFO_COUNT
) {
1364 ret
= KERN_INVALID_ARGUMENT
;
1368 behave
= (memory_object_behave_info_t
) attributes
;
1369 behave
->copy_strategy
= object
->copy_strategy
;
1370 behave
->temporary
= object
->temporary
;
1371 #if notyet /* remove when vm_msync complies and clean in place fini */
1372 behave
->invalidate
= object
->invalidate
;
1374 behave
->invalidate
= FALSE
;
1376 behave
->advisory_pageout
= object
->advisory_pageout
;
1377 // behave->silent_overwrite = object->silent_overwrite;
1378 behave
->silent_overwrite
= FALSE
;
1379 *count
= MEMORY_OBJECT_BEHAVE_INFO_COUNT
;
1383 case MEMORY_OBJECT_PERFORMANCE_INFO
:
1385 memory_object_perf_info_t perf
;
1387 if (*count
< MEMORY_OBJECT_PERF_INFO_COUNT
) {
1388 ret
= KERN_INVALID_ARGUMENT
;
1392 perf
= (memory_object_perf_info_t
) attributes
;
1393 perf
->cluster_size
= PAGE_SIZE
;
1394 perf
->may_cache
= object
->can_persist
;
1396 *count
= MEMORY_OBJECT_PERF_INFO_COUNT
;
1400 case OLD_MEMORY_OBJECT_ATTRIBUTE_INFO
:
1402 old_memory_object_attr_info_t attr
;
1404 if (*count
< OLD_MEMORY_OBJECT_ATTR_INFO_COUNT
) {
1405 ret
= KERN_INVALID_ARGUMENT
;
1409 attr
= (old_memory_object_attr_info_t
) attributes
;
1410 attr
->may_cache
= object
->can_persist
;
1411 attr
->copy_strategy
= object
->copy_strategy
;
1413 *count
= OLD_MEMORY_OBJECT_ATTR_INFO_COUNT
;
1417 case MEMORY_OBJECT_ATTRIBUTE_INFO
:
1419 memory_object_attr_info_t attr
;
1421 if (*count
< MEMORY_OBJECT_ATTR_INFO_COUNT
) {
1422 ret
= KERN_INVALID_ARGUMENT
;
1426 attr
= (memory_object_attr_info_t
) attributes
;
1427 attr
->copy_strategy
= object
->copy_strategy
;
1428 attr
->cluster_size
= PAGE_SIZE
;
1429 attr
->may_cache_object
= object
->can_persist
;
1430 attr
->temporary
= object
->temporary
;
1432 *count
= MEMORY_OBJECT_ATTR_INFO_COUNT
;
1437 ret
= KERN_INVALID_ARGUMENT
;
1441 vm_object_unlock(object
);
1448 memory_object_iopl_request(
1450 memory_object_offset_t offset
,
1451 upl_size_t
*upl_size
,
1453 upl_page_info_array_t user_page_list
,
1454 unsigned int *page_list_count
,
1461 caller_flags
= *flags
;
1463 if (caller_flags
& ~UPL_VALID_FLAGS
) {
1465 * For forward compatibility's sake,
1466 * reject any unknown flag.
1468 return KERN_INVALID_VALUE
;
1471 if (ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
1472 vm_named_entry_t named_entry
;
1474 named_entry
= (vm_named_entry_t
)port
->ip_kobject
;
1475 /* a few checks to make sure user is obeying rules */
1476 if(*upl_size
== 0) {
1477 if(offset
>= named_entry
->size
)
1478 return(KERN_INVALID_RIGHT
);
1479 *upl_size
= (upl_size_t
)(named_entry
->size
- offset
);
1480 if (*upl_size
!= named_entry
->size
- offset
)
1481 return KERN_INVALID_ARGUMENT
;
1483 if(caller_flags
& UPL_COPYOUT_FROM
) {
1484 if((named_entry
->protection
& VM_PROT_READ
)
1486 return(KERN_INVALID_RIGHT
);
1489 if((named_entry
->protection
&
1490 (VM_PROT_READ
| VM_PROT_WRITE
))
1491 != (VM_PROT_READ
| VM_PROT_WRITE
)) {
1492 return(KERN_INVALID_RIGHT
);
1495 if(named_entry
->size
< (offset
+ *upl_size
))
1496 return(KERN_INVALID_ARGUMENT
);
1498 /* the callers parameter offset is defined to be the */
1499 /* offset from beginning of named entry offset in object */
1500 offset
= offset
+ named_entry
->offset
;
1502 if (named_entry
->is_sub_map
||
1503 named_entry
->is_copy
)
1504 return KERN_INVALID_ARGUMENT
;
1506 named_entry_lock(named_entry
);
1508 if (named_entry
->is_pager
) {
1509 object
= vm_object_enter(named_entry
->backing
.pager
,
1510 named_entry
->offset
+ named_entry
->size
,
1511 named_entry
->internal
,
1514 if (object
== VM_OBJECT_NULL
) {
1515 named_entry_unlock(named_entry
);
1516 return(KERN_INVALID_OBJECT
);
1519 /* JMM - drop reference on pager here? */
1521 /* create an extra reference for the named entry */
1522 vm_object_lock(object
);
1523 vm_object_reference_locked(object
);
1524 named_entry
->backing
.object
= object
;
1525 named_entry
->is_pager
= FALSE
;
1526 named_entry_unlock(named_entry
);
1528 /* wait for object to be ready */
1529 while (!object
->pager_ready
) {
1530 vm_object_wait(object
,
1531 VM_OBJECT_EVENT_PAGER_READY
,
1533 vm_object_lock(object
);
1535 vm_object_unlock(object
);
1537 /* This is the case where we are going to map */
1538 /* an already mapped object. If the object is */
1539 /* not ready it is internal. An external */
1540 /* object cannot be mapped until it is ready */
1541 /* we can therefore avoid the ready check */
1543 object
= named_entry
->backing
.object
;
1544 vm_object_reference(object
);
1545 named_entry_unlock(named_entry
);
1547 } else if (ip_kotype(port
) == IKOT_MEM_OBJ_CONTROL
) {
1548 memory_object_control_t control
;
1549 control
= (memory_object_control_t
) port
;
1550 if (control
== NULL
)
1551 return (KERN_INVALID_ARGUMENT
);
1552 object
= memory_object_control_to_vm_object(control
);
1553 if (object
== VM_OBJECT_NULL
)
1554 return (KERN_INVALID_ARGUMENT
);
1555 vm_object_reference(object
);
1557 return KERN_INVALID_ARGUMENT
;
1559 if (object
== VM_OBJECT_NULL
)
1560 return (KERN_INVALID_ARGUMENT
);
1562 if (!object
->private) {
1563 if (object
->phys_contiguous
) {
1564 *flags
= UPL_PHYS_CONTIG
;
1569 *flags
= UPL_DEV_MEMORY
| UPL_PHYS_CONTIG
;
1572 ret
= vm_object_iopl_request(object
,
1579 vm_object_deallocate(object
);
1584 * Routine: memory_object_upl_request [interface]
1586 * Cause the population of a portion of a vm_object.
1587 * Depending on the nature of the request, the pages
1588 * returned may be contain valid data or be uninitialized.
1593 memory_object_upl_request(
1594 memory_object_control_t control
,
1595 memory_object_offset_t offset
,
1598 upl_page_info_array_t user_page_list
,
1599 unsigned int *page_list_count
,
1604 object
= memory_object_control_to_vm_object(control
);
1605 if (object
== VM_OBJECT_NULL
)
1606 return (KERN_TERMINATED
);
1608 return vm_object_upl_request(object
,
1618 * Routine: memory_object_super_upl_request [interface]
1620 * Cause the population of a portion of a vm_object
1621 * in much the same way as memory_object_upl_request.
1622 * Depending on the nature of the request, the pages
1623 * returned may be contain valid data or be uninitialized.
1624 * However, the region may be expanded up to the super
1625 * cluster size provided.
1629 memory_object_super_upl_request(
1630 memory_object_control_t control
,
1631 memory_object_offset_t offset
,
1633 upl_size_t super_cluster
,
1635 upl_page_info_t
*user_page_list
,
1636 unsigned int *page_list_count
,
1641 object
= memory_object_control_to_vm_object(control
);
1642 if (object
== VM_OBJECT_NULL
)
1643 return (KERN_INVALID_ARGUMENT
);
1645 return vm_object_super_upl_request(object
,
1656 memory_object_cluster_size(memory_object_control_t control
, memory_object_offset_t
*start
,
1657 vm_size_t
*length
, uint32_t *io_streaming
, memory_object_fault_info_t fault_info
)
1661 object
= memory_object_control_to_vm_object(control
);
1663 if (object
== VM_OBJECT_NULL
|| object
->paging_offset
> *start
)
1664 return (KERN_INVALID_ARGUMENT
);
1666 *start
-= object
->paging_offset
;
1668 vm_object_cluster_size(object
, (vm_object_offset_t
*)start
, length
, (vm_object_fault_info_t
)fault_info
, io_streaming
);
1670 *start
+= object
->paging_offset
;
1672 return (KERN_SUCCESS
);
1676 int vm_stat_discard_cleared_reply
= 0;
1677 int vm_stat_discard_cleared_unset
= 0;
1678 int vm_stat_discard_cleared_too_late
= 0;
1683 * Routine: host_default_memory_manager [interface]
1685 * set/get the default memory manager port and default cluster
1688 * If successful, consumes the supplied naked send right.
1691 host_default_memory_manager(
1692 host_priv_t host_priv
,
1693 memory_object_default_t
*default_manager
,
1694 __unused memory_object_cluster_size_t cluster_size
)
1696 memory_object_default_t current_manager
;
1697 memory_object_default_t new_manager
;
1698 memory_object_default_t returned_manager
;
1699 kern_return_t result
= KERN_SUCCESS
;
1701 if (host_priv
== HOST_PRIV_NULL
)
1702 return(KERN_INVALID_HOST
);
1704 assert(host_priv
== &realhost
);
1706 new_manager
= *default_manager
;
1707 lck_mtx_lock(&memory_manager_default_lock
);
1708 current_manager
= memory_manager_default
;
1709 returned_manager
= MEMORY_OBJECT_DEFAULT_NULL
;
1711 if (new_manager
== MEMORY_OBJECT_DEFAULT_NULL
) {
1713 * Retrieve the current value.
1715 returned_manager
= current_manager
;
1716 memory_object_default_reference(returned_manager
);
1720 * If this is the first non-null manager, start
1721 * up the internal pager support.
1723 if (current_manager
== MEMORY_OBJECT_DEFAULT_NULL
) {
1724 result
= vm_pageout_internal_start();
1725 if (result
!= KERN_SUCCESS
)
1730 * Retrieve the current value,
1731 * and replace it with the supplied value.
1732 * We return the old reference to the caller
1733 * but we have to take a reference on the new
1736 returned_manager
= current_manager
;
1737 memory_manager_default
= new_manager
;
1738 memory_object_default_reference(new_manager
);
1741 * In case anyone's been waiting for a memory
1742 * manager to be established, wake them up.
1745 thread_wakeup((event_t
) &memory_manager_default
);
1748 * Now that we have a default pager for anonymous memory,
1749 * reactivate all the throttled pages (i.e. dirty pages with
1752 if (current_manager
== MEMORY_OBJECT_DEFAULT_NULL
)
1754 vm_page_reactivate_all_throttled();
1758 lck_mtx_unlock(&memory_manager_default_lock
);
1760 *default_manager
= returned_manager
;
1765 * Routine: memory_manager_default_reference
1767 * Returns a naked send right for the default
1768 * memory manager. The returned right is always
1769 * valid (not IP_NULL or IP_DEAD).
1772 __private_extern__ memory_object_default_t
1773 memory_manager_default_reference(void)
1775 memory_object_default_t current_manager
;
1777 lck_mtx_lock(&memory_manager_default_lock
);
1778 current_manager
= memory_manager_default
;
1779 while (current_manager
== MEMORY_OBJECT_DEFAULT_NULL
) {
1782 res
= lck_mtx_sleep(&memory_manager_default_lock
,
1784 (event_t
) &memory_manager_default
,
1786 assert(res
== THREAD_AWAKENED
);
1787 current_manager
= memory_manager_default
;
1789 memory_object_default_reference(current_manager
);
1790 lck_mtx_unlock(&memory_manager_default_lock
);
1792 return current_manager
;
1796 * Routine: memory_manager_default_check
1799 * Check whether a default memory manager has been set
1800 * up yet, or not. Returns KERN_SUCCESS if dmm exists,
1801 * and KERN_FAILURE if dmm does not exist.
1803 * If there is no default memory manager, log an error,
1804 * but only the first time.
1807 __private_extern__ kern_return_t
1808 memory_manager_default_check(void)
1810 memory_object_default_t current
;
1812 lck_mtx_lock(&memory_manager_default_lock
);
1813 current
= memory_manager_default
;
1814 if (current
== MEMORY_OBJECT_DEFAULT_NULL
) {
1815 static boolean_t logged
; /* initialized to 0 */
1816 boolean_t complain
= !logged
;
1818 lck_mtx_unlock(&memory_manager_default_lock
);
1820 printf("Warning: No default memory manager\n");
1821 return(KERN_FAILURE
);
1823 lck_mtx_unlock(&memory_manager_default_lock
);
1824 return(KERN_SUCCESS
);
1828 __private_extern__
void
1829 memory_manager_default_init(void)
1831 memory_manager_default
= MEMORY_OBJECT_DEFAULT_NULL
;
1832 lck_mtx_init(&memory_manager_default_lock
, &vm_object_lck_grp
, &vm_object_lck_attr
);
1837 /* Allow manipulation of individual page state. This is actually part of */
1838 /* the UPL regimen but takes place on the object rather than on a UPL */
1841 memory_object_page_op(
1842 memory_object_control_t control
,
1843 memory_object_offset_t offset
,
1845 ppnum_t
*phys_entry
,
1850 object
= memory_object_control_to_vm_object(control
);
1851 if (object
== VM_OBJECT_NULL
)
1852 return (KERN_INVALID_ARGUMENT
);
1854 return vm_object_page_op(object
, offset
, ops
, phys_entry
, flags
);
1858 * memory_object_range_op offers performance enhancement over
1859 * memory_object_page_op for page_op functions which do not require page
1860 * level state to be returned from the call. Page_op was created to provide
1861 * a low-cost alternative to page manipulation via UPLs when only a single
1862 * page was involved. The range_op call establishes the ability in the _op
1863 * family of functions to work on multiple pages where the lack of page level
1864 * state handling allows the caller to avoid the overhead of the upl structures.
1868 memory_object_range_op(
1869 memory_object_control_t control
,
1870 memory_object_offset_t offset_beg
,
1871 memory_object_offset_t offset_end
,
1877 object
= memory_object_control_to_vm_object(control
);
1878 if (object
== VM_OBJECT_NULL
)
1879 return (KERN_INVALID_ARGUMENT
);
1881 return vm_object_range_op(object
,
1885 (uint32_t *) range
);
1890 memory_object_mark_used(
1891 memory_object_control_t control
)
1895 if (control
== NULL
)
1898 object
= memory_object_control_to_vm_object(control
);
1900 if (object
!= VM_OBJECT_NULL
)
1901 vm_object_cache_remove(object
);
1906 memory_object_mark_unused(
1907 memory_object_control_t control
,
1908 __unused boolean_t rage
)
1912 if (control
== NULL
)
1915 object
= memory_object_control_to_vm_object(control
);
1917 if (object
!= VM_OBJECT_NULL
)
1918 vm_object_cache_add(object
);
1922 memory_object_mark_io_tracking(
1923 memory_object_control_t control
)
1927 if (control
== NULL
)
1929 object
= memory_object_control_to_vm_object(control
);
1931 if (object
!= VM_OBJECT_NULL
) {
1932 vm_object_lock(object
);
1933 object
->io_tracking
= TRUE
;
1934 vm_object_unlock(object
);
1939 memory_object_pages_resident(
1940 memory_object_control_t control
,
1941 boolean_t
* has_pages_resident
)
1945 *has_pages_resident
= FALSE
;
1947 object
= memory_object_control_to_vm_object(control
);
1948 if (object
== VM_OBJECT_NULL
)
1949 return (KERN_INVALID_ARGUMENT
);
1951 if (object
->resident_page_count
)
1952 *has_pages_resident
= TRUE
;
1954 return (KERN_SUCCESS
);
1958 memory_object_signed(
1959 memory_object_control_t control
,
1960 boolean_t is_signed
)
1964 object
= memory_object_control_to_vm_object(control
);
1965 if (object
== VM_OBJECT_NULL
)
1966 return KERN_INVALID_ARGUMENT
;
1968 vm_object_lock(object
);
1969 object
->code_signed
= is_signed
;
1970 vm_object_unlock(object
);
1972 return KERN_SUCCESS
;
1976 memory_object_is_signed(
1977 memory_object_control_t control
)
1979 boolean_t is_signed
;
1982 object
= memory_object_control_to_vm_object(control
);
1983 if (object
== VM_OBJECT_NULL
)
1986 vm_object_lock_shared(object
);
1987 is_signed
= object
->code_signed
;
1988 vm_object_unlock(object
);
1994 memory_object_is_slid(
1995 memory_object_control_t control
)
1997 vm_object_t object
= VM_OBJECT_NULL
;
1999 object
= memory_object_control_to_vm_object(control
);
2000 if (object
== VM_OBJECT_NULL
)
2003 return object
->object_slid
;
2006 static zone_t mem_obj_control_zone
;
2008 __private_extern__
void
2009 memory_object_control_bootstrap(void)
2013 i
= (vm_size_t
) sizeof (struct memory_object_control
);
2014 mem_obj_control_zone
= zinit (i
, 8192*i
, 4096, "mem_obj_control");
2015 zone_change(mem_obj_control_zone
, Z_CALLERACCT
, FALSE
);
2016 zone_change(mem_obj_control_zone
, Z_NOENCRYPT
, TRUE
);
2020 __private_extern__ memory_object_control_t
2021 memory_object_control_allocate(
2024 memory_object_control_t control
;
2026 control
= (memory_object_control_t
)zalloc(mem_obj_control_zone
);
2027 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
2028 control
->moc_object
= object
;
2029 control
->moc_ikot
= IKOT_MEM_OBJ_CONTROL
; /* fake ip_kotype */
2034 __private_extern__
void
2035 memory_object_control_collapse(
2036 memory_object_control_t control
,
2039 assert((control
->moc_object
!= VM_OBJECT_NULL
) &&
2040 (control
->moc_object
!= object
));
2041 control
->moc_object
= object
;
2044 __private_extern__ vm_object_t
2045 memory_object_control_to_vm_object(
2046 memory_object_control_t control
)
2048 if (control
== MEMORY_OBJECT_CONTROL_NULL
||
2049 control
->moc_ikot
!= IKOT_MEM_OBJ_CONTROL
)
2050 return VM_OBJECT_NULL
;
2052 return (control
->moc_object
);
2055 memory_object_control_t
2056 convert_port_to_mo_control(
2057 __unused mach_port_t port
)
2059 return MEMORY_OBJECT_CONTROL_NULL
;
2064 convert_mo_control_to_port(
2065 __unused memory_object_control_t control
)
2067 return MACH_PORT_NULL
;
2071 memory_object_control_reference(
2072 __unused memory_object_control_t control
)
2078 * We only every issue one of these references, so kill it
2079 * when that gets released (should switch the real reference
2080 * counting in true port-less EMMI).
2083 memory_object_control_deallocate(
2084 memory_object_control_t control
)
2086 zfree(mem_obj_control_zone
, control
);
2090 memory_object_control_disable(
2091 memory_object_control_t control
)
2093 assert(control
->moc_object
!= VM_OBJECT_NULL
);
2094 control
->moc_object
= VM_OBJECT_NULL
;
2098 memory_object_default_reference(
2099 memory_object_default_t dmm
)
2101 ipc_port_make_send(dmm
);
2105 memory_object_default_deallocate(
2106 memory_object_default_t dmm
)
2108 ipc_port_release_send(dmm
);
2112 convert_port_to_memory_object(
2113 __unused mach_port_t port
)
2115 return (MEMORY_OBJECT_NULL
);
2120 convert_memory_object_to_port(
2121 __unused memory_object_t object
)
2123 return (MACH_PORT_NULL
);
2127 /* Routine memory_object_reference */
2128 void memory_object_reference(
2129 memory_object_t memory_object
)
2131 (memory_object
->mo_pager_ops
->memory_object_reference
)(
2135 /* Routine memory_object_deallocate */
2136 void memory_object_deallocate(
2137 memory_object_t memory_object
)
2139 (memory_object
->mo_pager_ops
->memory_object_deallocate
)(
2144 /* Routine memory_object_init */
2145 kern_return_t memory_object_init
2147 memory_object_t memory_object
,
2148 memory_object_control_t memory_control
,
2149 memory_object_cluster_size_t memory_object_page_size
2152 return (memory_object
->mo_pager_ops
->memory_object_init
)(
2155 memory_object_page_size
);
2158 /* Routine memory_object_terminate */
2159 kern_return_t memory_object_terminate
2161 memory_object_t memory_object
2164 return (memory_object
->mo_pager_ops
->memory_object_terminate
)(
2168 /* Routine memory_object_data_request */
2169 kern_return_t memory_object_data_request
2171 memory_object_t memory_object
,
2172 memory_object_offset_t offset
,
2173 memory_object_cluster_size_t length
,
2174 vm_prot_t desired_access
,
2175 memory_object_fault_info_t fault_info
2178 return (memory_object
->mo_pager_ops
->memory_object_data_request
)(
2186 /* Routine memory_object_data_return */
2187 kern_return_t memory_object_data_return
2189 memory_object_t memory_object
,
2190 memory_object_offset_t offset
,
2191 memory_object_cluster_size_t size
,
2192 memory_object_offset_t
*resid_offset
,
2195 boolean_t kernel_copy
,
2199 return (memory_object
->mo_pager_ops
->memory_object_data_return
)(
2210 /* Routine memory_object_data_initialize */
2211 kern_return_t memory_object_data_initialize
2213 memory_object_t memory_object
,
2214 memory_object_offset_t offset
,
2215 memory_object_cluster_size_t size
2218 return (memory_object
->mo_pager_ops
->memory_object_data_initialize
)(
2224 /* Routine memory_object_data_unlock */
2225 kern_return_t memory_object_data_unlock
2227 memory_object_t memory_object
,
2228 memory_object_offset_t offset
,
2229 memory_object_size_t size
,
2230 vm_prot_t desired_access
2233 return (memory_object
->mo_pager_ops
->memory_object_data_unlock
)(
2240 /* Routine memory_object_synchronize */
2241 kern_return_t memory_object_synchronize
2243 memory_object_t memory_object
,
2244 memory_object_offset_t offset
,
2245 memory_object_size_t size
,
2246 vm_sync_t sync_flags
2249 return (memory_object
->mo_pager_ops
->memory_object_synchronize
)(
2258 * memory_object_map() is called by VM (in vm_map_enter() and its variants)
2259 * each time a "named" VM object gets mapped directly or indirectly
2260 * (copy-on-write mapping). A "named" VM object has an extra reference held
2261 * by the pager to keep it alive until the pager decides that the
2262 * memory object (and its VM object) can be reclaimed.
2263 * VM calls memory_object_last_unmap() (in vm_object_deallocate()) when all
2264 * the mappings of that memory object have been removed.
2266 * For a given VM object, calls to memory_object_map() and memory_object_unmap()
2267 * are serialized (through object->mapping_in_progress), to ensure that the
2268 * pager gets a consistent view of the mapping status of the memory object.
2270 * This allows the pager to keep track of how many times a memory object
2271 * has been mapped and with which protections, to decide when it can be
2275 /* Routine memory_object_map */
2276 kern_return_t memory_object_map
2278 memory_object_t memory_object
,
2282 return (memory_object
->mo_pager_ops
->memory_object_map
)(
2287 /* Routine memory_object_last_unmap */
2288 kern_return_t memory_object_last_unmap
2290 memory_object_t memory_object
2293 return (memory_object
->mo_pager_ops
->memory_object_last_unmap
)(
2297 /* Routine memory_object_data_reclaim */
2298 kern_return_t memory_object_data_reclaim
2300 memory_object_t memory_object
,
2301 boolean_t reclaim_backing_store
2304 if (memory_object
->mo_pager_ops
->memory_object_data_reclaim
== NULL
)
2305 return KERN_NOT_SUPPORTED
;
2306 return (memory_object
->mo_pager_ops
->memory_object_data_reclaim
)(
2308 reclaim_backing_store
);
2311 /* Routine memory_object_create */
2312 kern_return_t memory_object_create
2314 memory_object_default_t default_memory_manager
,
2315 vm_size_t new_memory_object_size
,
2316 memory_object_t
*new_memory_object
2319 return default_pager_memory_object_create(default_memory_manager
,
2320 new_memory_object_size
,
2325 convert_port_to_upl(
2331 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UPL
)) {
2335 upl
= (upl_t
) port
->ip_kobject
;
2344 convert_upl_to_port(
2347 return MACH_PORT_NULL
;
2350 __private_extern__
void
2352 __unused ipc_port_t port
,
2353 __unused mach_port_mscount_t mscount
)