2 * Copyright (c) 2000-2007 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.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Page fault handling module.
65 #include <mach_cluster_stats.h>
66 #include <mach_pagemap.h>
68 #include <libkern/OSAtomic.h>
70 #include <mach/mach_types.h>
71 #include <mach/kern_return.h>
72 #include <mach/message.h> /* for error codes */
73 #include <mach/vm_param.h>
74 #include <mach/vm_behavior.h>
75 #include <mach/memory_object.h>
76 /* For memory_object_data_{request,unlock} */
79 #include <kern/kern_types.h>
80 #include <kern/host_statistics.h>
81 #include <kern/counters.h>
82 #include <kern/task.h>
83 #include <kern/thread.h>
84 #include <kern/sched_prim.h>
85 #include <kern/host.h>
87 #include <kern/mach_param.h>
88 #include <kern/macro_help.h>
89 #include <kern/zalloc.h>
90 #include <kern/misc_protos.h>
92 #include <ppc/proc_reg.h>
94 #include <vm/vm_fault.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_kern.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_protos.h>
102 #include <vm/vm_external.h>
103 #include <vm/memory_object.h>
104 #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
106 #include <sys/kdebug.h>
108 #define VM_FAULT_CLASSIFY 0
110 /* Zero-filled pages are marked "m->zero_fill" and put on the
111 * special zero-fill inactive queue only if they belong to
112 * an object at least this big.
114 #define VM_ZF_OBJECT_SIZE_THRESHOLD (0x200000)
116 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
118 int vm_object_pagein_throttle
= 16;
123 extern struct db_watchpoint
*db_watchpoint_list
;
124 #endif /* MACH_KDB */
127 /* Forward declarations of internal routines. */
128 extern kern_return_t
vm_fault_wire_fast(
131 vm_map_entry_t entry
,
133 vm_map_offset_t pmap_addr
);
135 extern void vm_fault_continue(void);
137 extern void vm_fault_copy_cleanup(
141 extern void vm_fault_copy_dst_cleanup(
144 #if VM_FAULT_CLASSIFY
145 extern void vm_fault_classify(vm_object_t object
,
146 vm_object_offset_t offset
,
147 vm_prot_t fault_type
);
149 extern void vm_fault_classify_init(void);
153 unsigned long vm_cs_validates
= 0;
154 unsigned long vm_cs_revalidates
= 0;
155 unsigned long vm_cs_query_modified
= 0;
156 unsigned long vm_cs_validated_dirtied
= 0;
158 #if CONFIG_ENFORCE_SIGNED_CODE
160 const int cs_enforcement_disable
=0;
162 int cs_enforcement_disable
=1;
167 * Routine: vm_fault_init
169 * Initialize our private data structures.
175 #if CONFIG_ENFORCE_SIGNED_CODE
176 PE_parse_boot_argn("cs_enforcement_disable", &cs_enforcement_disable
, sizeof (cs_enforcement_disable
));
178 PE_parse_boot_argn("cs_debug", &cs_debug
, sizeof (cs_debug
));
183 * Routine: vm_fault_cleanup
185 * Clean up the result of vm_fault_page.
187 * The paging reference for "object" is released.
188 * "object" is unlocked.
189 * If "top_page" is not null, "top_page" is
190 * freed and the paging reference for the object
191 * containing it is released.
194 * "object" must be locked.
198 register vm_object_t object
,
199 register vm_page_t top_page
)
201 vm_object_paging_end(object
);
202 vm_object_unlock(object
);
204 if (top_page
!= VM_PAGE_NULL
) {
205 object
= top_page
->object
;
207 vm_object_lock(object
);
208 VM_PAGE_FREE(top_page
);
209 vm_object_paging_end(object
);
210 vm_object_unlock(object
);
214 #if MACH_CLUSTER_STATS
215 #define MAXCLUSTERPAGES 16
217 unsigned long pages_in_cluster
;
218 unsigned long pages_at_higher_offsets
;
219 unsigned long pages_at_lower_offsets
;
220 } cluster_stats_in
[MAXCLUSTERPAGES
];
221 #define CLUSTER_STAT(clause) clause
222 #define CLUSTER_STAT_HIGHER(x) \
223 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
224 #define CLUSTER_STAT_LOWER(x) \
225 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
226 #define CLUSTER_STAT_CLUSTER(x) \
227 ((cluster_stats_in[(x)].pages_in_cluster)++)
228 #else /* MACH_CLUSTER_STATS */
229 #define CLUSTER_STAT(clause)
230 #endif /* MACH_CLUSTER_STATS */
232 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
235 boolean_t vm_page_deactivate_behind
= TRUE
;
237 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
239 int vm_default_ahead
= 0;
240 int vm_default_behind
= MAX_UPL_TRANSFER
;
242 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
245 * vm_page_is_sequential
247 * Determine if sequential access is in progress
248 * in accordance with the behavior specified.
249 * Update state to indicate current access pattern.
251 * object must have at least the shared lock held
255 vm_fault_is_sequential(
257 vm_object_offset_t offset
,
258 vm_behavior_t behavior
)
260 vm_object_offset_t last_alloc
;
264 last_alloc
= object
->last_alloc
;
265 sequential
= object
->sequential
;
266 orig_sequential
= sequential
;
269 case VM_BEHAVIOR_RANDOM
:
271 * reset indicator of sequential behavior
276 case VM_BEHAVIOR_SEQUENTIAL
:
277 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
279 * advance indicator of sequential behavior
281 if (sequential
< MAX_SEQUENTIAL_RUN
)
282 sequential
+= PAGE_SIZE
;
285 * reset indicator of sequential behavior
291 case VM_BEHAVIOR_RSEQNTL
:
292 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
294 * advance indicator of sequential behavior
296 if (sequential
> -MAX_SEQUENTIAL_RUN
)
297 sequential
-= PAGE_SIZE
;
300 * reset indicator of sequential behavior
306 case VM_BEHAVIOR_DEFAULT
:
308 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
310 * advance indicator of sequential behavior
314 if (sequential
< MAX_SEQUENTIAL_RUN
)
315 sequential
+= PAGE_SIZE
;
317 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
319 * advance indicator of sequential behavior
323 if (sequential
> -MAX_SEQUENTIAL_RUN
)
324 sequential
-= PAGE_SIZE
;
327 * reset indicator of sequential behavior
333 if (sequential
!= orig_sequential
) {
334 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
336 * if someone else has already updated object->sequential
337 * don't bother trying to update it or object->last_alloc
343 * I'd like to do this with a OSCompareAndSwap64, but that
344 * doesn't exist for PPC... however, it shouldn't matter
345 * that much... last_alloc is maintained so that we can determine
346 * if a sequential access pattern is taking place... if only
347 * one thread is banging on this object, no problem with the unprotected
348 * update... if 2 or more threads are banging away, we run the risk of
349 * someone seeing a mangled update... however, in the face of multiple
350 * accesses, no sequential access pattern can develop anyway, so we
351 * haven't lost any real info.
353 object
->last_alloc
= offset
;
358 * vm_page_deactivate_behind
360 * Determine if sequential access is in progress
361 * in accordance with the behavior specified. If
362 * so, compute a potential page to deactivate and
365 * object must be locked.
367 * return TRUE if we actually deactivate a page
371 vm_fault_deactivate_behind(
373 vm_object_offset_t offset
,
374 vm_behavior_t behavior
)
378 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
381 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
384 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
386 * Do not deactivate pages from the kernel object: they
387 * are not intended to become pageable.
388 * or we've disabled the deactivate behind mechanism
392 if ((sequential_run
= object
->sequential
)) {
393 if (sequential_run
< 0) {
394 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
395 sequential_run
= 0 - sequential_run
;
397 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
401 case VM_BEHAVIOR_RANDOM
:
403 case VM_BEHAVIOR_SEQUENTIAL
:
404 if (sequential_run
>= (int)PAGE_SIZE
)
405 m
= vm_page_lookup(object
, offset
- PAGE_SIZE_64
);
407 case VM_BEHAVIOR_RSEQNTL
:
408 if (sequential_run
>= (int)PAGE_SIZE
)
409 m
= vm_page_lookup(object
, offset
+ PAGE_SIZE_64
);
411 case VM_BEHAVIOR_DEFAULT
:
413 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
416 * determine if the run of sequential accesss has been
417 * long enough on an object with default access behavior
418 * to consider it for deactivation
420 if ((uint64_t)sequential_run
>= behind
) {
421 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
422 if (offset
>= behind
)
423 m
= vm_page_lookup(object
, offset
- behind
);
425 if (offset
< -behind
)
426 m
= vm_page_lookup(object
, offset
+ behind
);
433 if (!m
->busy
&& !m
->no_cache
&& !m
->throttled
&& !m
->fictitious
&& !m
->absent
) {
434 pmap_clear_reference(m
->phys_page
);
435 m
->deactivated
= TRUE
;
437 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
447 * check for various conditions that would
448 * prevent us from creating a ZF page...
449 * cleanup is based on being called from vm_fault_page
451 * object must be locked
452 * object == m->object
454 static vm_fault_return_t
455 vm_fault_check(vm_object_t object
, vm_page_t m
, vm_page_t first_m
, boolean_t interruptible_state
)
457 if (object
->shadow_severed
) {
459 * the shadow chain was severed
460 * just have to return an error at this point
462 if (m
!= VM_PAGE_NULL
)
464 vm_fault_cleanup(object
, first_m
);
466 thread_interrupt_level(interruptible_state
);
468 return (VM_FAULT_MEMORY_ERROR
);
470 if (vm_backing_store_low
) {
472 * are we protecting the system from
473 * backing store exhaustion. If so
474 * sleep unless we are privileged.
476 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
478 if (m
!= VM_PAGE_NULL
)
480 vm_fault_cleanup(object
, first_m
);
482 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
484 thread_block(THREAD_CONTINUE_NULL
);
485 thread_interrupt_level(interruptible_state
);
487 return (VM_FAULT_RETRY
);
490 if (VM_PAGE_ZFILL_THROTTLED()) {
492 * we're throttling zero-fills...
493 * treat this as if we couldn't grab a page
495 if (m
!= VM_PAGE_NULL
)
497 vm_fault_cleanup(object
, first_m
);
499 thread_interrupt_level(interruptible_state
);
501 return (VM_FAULT_MEMORY_SHORTAGE
);
503 return (VM_FAULT_SUCCESS
);
508 * do the work to zero fill a page and
509 * inject it into the correct paging queue
511 * m->object must be locked
512 * page queue lock must NOT be held
515 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
517 int my_fault
= DBG_ZERO_FILL_FAULT
;
520 * This is is a zero-fill page fault...
522 * Checking the page lock is a waste of
523 * time; this page was absent, so
524 * it can't be page locked by a pager.
526 * we also consider it undefined
527 * with respect to instruction
528 * execution. i.e. it is the responsibility
529 * of higher layers to call for an instruction
530 * sync after changing the contents and before
531 * sending a program into this area. We
532 * choose this approach for performance
536 m
->cs_validated
= FALSE
;
537 m
->cs_tainted
= FALSE
;
539 if (no_zero_fill
== TRUE
)
540 my_fault
= DBG_NZF_PAGE_FAULT
;
542 vm_page_zero_fill(m
);
544 VM_STAT_INCR(zero_fill_count
);
545 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
548 assert(m
->object
!= kernel_object
);
549 //assert(m->pageq.next == NULL && m->pageq.prev == NULL);
551 if (!IP_VALID(memory_manager_default
) &&
552 (m
->object
->purgable
== VM_PURGABLE_DENY
||
553 m
->object
->purgable
== VM_PURGABLE_NONVOLATILE
||
554 m
->object
->purgable
== VM_PURGABLE_VOLATILE
)) {
555 vm_page_lock_queues();
557 queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
559 vm_page_throttled_count
++;
561 vm_page_unlock_queues();
563 if (m
->object
->size
> VM_ZF_OBJECT_SIZE_THRESHOLD
) {
565 OSAddAtomic(1, (SInt32
*)&vm_zf_count
);
573 * Routine: vm_fault_page
575 * Find the resident page for the virtual memory
576 * specified by the given virtual memory object
578 * Additional arguments:
579 * The required permissions for the page is given
580 * in "fault_type". Desired permissions are included
582 * fault_info is passed along to determine pagein cluster
583 * limits... it contains the expected reference pattern,
584 * cluster size if available, etc...
586 * If the desired page is known to be resident (for
587 * example, because it was previously wired down), asserting
588 * the "unwiring" parameter will speed the search.
590 * If the operation can be interrupted (by thread_abort
591 * or thread_terminate), then the "interruptible"
592 * parameter should be asserted.
595 * The page containing the proper data is returned
599 * The source object must be locked and referenced,
600 * and must donate one paging reference. The reference
601 * is not affected. The paging reference and lock are
604 * If the call succeeds, the object in which "result_page"
605 * resides is left locked and holding a paging reference.
606 * If this is not the original object, a busy page in the
607 * original object is returned in "top_page", to prevent other
608 * callers from pursuing this same data, along with a paging
609 * reference for the original object. The "top_page" should
610 * be destroyed when this guarantee is no longer required.
611 * The "result_page" is also left busy. It is not removed
612 * from the pageout queues.
618 vm_object_t first_object
, /* Object to begin search */
619 vm_object_offset_t first_offset
, /* Offset into object */
620 vm_prot_t fault_type
, /* What access is requested */
621 boolean_t must_be_resident
,/* Must page be resident? */
622 /* Modifies in place: */
623 vm_prot_t
*protection
, /* Protection for mapping */
625 vm_page_t
*result_page
, /* Page found, if successful */
626 vm_page_t
*top_page
, /* Page in top object, if
627 * not result_page. */
628 int *type_of_fault
, /* if non-null, fill in with type of fault
629 * COW, zero-fill, etc... returned in trace point */
630 /* More arguments: */
631 kern_return_t
*error_code
, /* code if page is in error */
632 boolean_t no_zero_fill
, /* don't zero fill absent pages */
634 boolean_t data_supply
, /* treat as data_supply if
635 * it is a write fault and a full
636 * page is provided */
638 __unused boolean_t data_supply
,
640 vm_object_fault_info_t fault_info
)
644 vm_object_offset_t offset
;
646 vm_object_t next_object
;
647 vm_object_t copy_object
;
648 boolean_t look_for_page
;
649 vm_prot_t access_required
= fault_type
;
650 vm_prot_t wants_copy_flag
;
651 CLUSTER_STAT(int pages_at_higher_offsets
;)
652 CLUSTER_STAT(int pages_at_lower_offsets
;)
653 kern_return_t wait_result
;
654 boolean_t interruptible_state
;
655 vm_fault_return_t error
;
657 uint32_t try_failed_count
;
658 int interruptible
; /* how may fault be interrupted? */
659 memory_object_t pager
;
662 * MACH page map - an optional optimization where a bit map is maintained
663 * by the VM subsystem for internal objects to indicate which pages of
664 * the object currently reside on backing store. This existence map
665 * duplicates information maintained by the vnode pager. It is
666 * created at the time of the first pageout against the object, i.e.
667 * at the same time pager for the object is created. The optimization
668 * is designed to eliminate pager interaction overhead, if it is
669 * 'known' that the page does not exist on backing store.
671 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
672 * either marked as paged out in the existence map for the object or no
673 * existence map exists for the object. MUST_ASK_PAGER() is one of the
674 * criteria in the decision to invoke the pager. It is also used as one
675 * of the criteria to terminate the scan for adjacent pages in a clustered
676 * pagein operation. Note that MUST_ASK_PAGER() always evaluates to TRUE for
677 * permanent objects. Note also that if the pager for an internal object
678 * has not been created, the pager is not invoked regardless of the value
679 * of MUST_ASK_PAGER() and that clustered pagein scans are only done on an object
680 * for which a pager has been created.
682 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
683 * is marked as paged out in the existence map for the object. PAGED_OUT()
684 * PAGED_OUT() is used to determine if a page has already been pushed
685 * into a copy object in order to avoid a redundant page out operation.
688 #define MUST_ASK_PAGER(o, f) (vm_external_state_get((o)->existence_map, (f)) \
689 != VM_EXTERNAL_STATE_ABSENT)
690 #define PAGED_OUT(o, f) (vm_external_state_get((o)->existence_map, (f)) \
691 == VM_EXTERNAL_STATE_EXISTS)
693 #define MUST_ASK_PAGER(o, f) (TRUE)
694 #define PAGED_OUT(o, f) (FALSE)
700 #define PREPARE_RELEASE_PAGE(m) \
702 vm_page_lock_queues(); \
705 #define DO_RELEASE_PAGE(m) \
707 PAGE_WAKEUP_DONE(m); \
708 if (!m->active && !m->inactive && !m->throttled)\
709 vm_page_activate(m); \
710 vm_page_unlock_queues(); \
713 #define RELEASE_PAGE(m) \
715 PREPARE_RELEASE_PAGE(m); \
716 DO_RELEASE_PAGE(m); \
720 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
726 * If there are watchpoints set, then
727 * we don't want to give away write permission
728 * on a read fault. Make the task write fault,
729 * so that the watchpoint code notices the access.
731 if (db_watchpoint_list
) {
733 * If we aren't asking for write permission,
734 * then don't give it away. We're using write
735 * faults to set the dirty bit.
737 if (!(fault_type
& VM_PROT_WRITE
))
738 *protection
&= ~VM_PROT_WRITE
;
740 #endif /* MACH_KDB */
742 interruptible
= fault_info
->interruptible
;
743 interruptible_state
= thread_interrupt_level(interruptible
);
746 * INVARIANTS (through entire routine):
748 * 1) At all times, we must either have the object
749 * lock or a busy page in some object to prevent
750 * some other thread from trying to bring in
753 * Note that we cannot hold any locks during the
754 * pager access or when waiting for memory, so
755 * we use a busy page then.
757 * 2) To prevent another thread from racing us down the
758 * shadow chain and entering a new page in the top
759 * object before we do, we must keep a busy page in
760 * the top object while following the shadow chain.
762 * 3) We must increment paging_in_progress on any object
763 * for which we have a busy page before dropping
766 * 4) We leave busy pages on the pageout queues.
767 * If the pageout daemon comes across a busy page,
768 * it will remove the page from the pageout queues.
771 object
= first_object
;
772 offset
= first_offset
;
773 first_m
= VM_PAGE_NULL
;
774 access_required
= fault_type
;
778 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
779 (integer_t
)object
, offset
, fault_type
, *protection
, 0);
782 * default type of fault
784 my_fault
= DBG_CACHE_HIT_FAULT
;
788 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
790 if (!object
->alive
) {
792 * object is no longer valid
793 * clean up and return error
795 vm_fault_cleanup(object
, first_m
);
796 thread_interrupt_level(interruptible_state
);
798 return (VM_FAULT_MEMORY_ERROR
);
802 * See whether the page at 'offset' is resident
804 m
= vm_page_lookup(object
, offset
);
806 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
808 if (m
!= VM_PAGE_NULL
) {
812 * The page is being brought in,
813 * wait for it and then retry.
815 * A possible optimization: if the page
816 * is known to be resident, we can ignore
817 * pages that are absent (regardless of
818 * whether they're busy).
821 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
823 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
825 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
826 (integer_t
)object
, offset
,
828 counter(c_vm_fault_page_block_busy_kernel
++);
830 if (wait_result
!= THREAD_AWAKENED
) {
831 vm_fault_cleanup(object
, first_m
);
832 thread_interrupt_level(interruptible_state
);
834 if (wait_result
== THREAD_RESTART
)
835 return (VM_FAULT_RETRY
);
837 return (VM_FAULT_INTERRUPTED
);
842 if (m
->phys_page
== vm_page_guard_addr
) {
844 * Guard page: off limits !
846 if (fault_type
== VM_PROT_NONE
) {
848 * The fault is not requesting any
849 * access to the guard page, so it must
850 * be just to wire or unwire it.
851 * Let's pretend it succeeded...
855 assert(first_m
== VM_PAGE_NULL
);
858 *type_of_fault
= DBG_GUARD_FAULT
;
859 return VM_FAULT_SUCCESS
;
862 * The fault requests access to the
863 * guard page: let's deny that !
865 vm_fault_cleanup(object
, first_m
);
866 thread_interrupt_level(interruptible_state
);
867 return VM_FAULT_MEMORY_ERROR
;
873 * The page is in error, give up now.
876 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
879 *error_code
= KERN_MEMORY_ERROR
;
882 vm_fault_cleanup(object
, first_m
);
883 thread_interrupt_level(interruptible_state
);
885 return (VM_FAULT_MEMORY_ERROR
);
889 * The pager wants us to restart
890 * at the top of the chain,
891 * typically because it has moved the
892 * page to another pager, then do so.
895 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
899 vm_fault_cleanup(object
, first_m
);
900 thread_interrupt_level(interruptible_state
);
902 return (VM_FAULT_RETRY
);
906 * The page isn't busy, but is absent,
907 * therefore it's deemed "unavailable".
909 * Remove the non-existent page (unless it's
910 * in the top object) and move on down to the
911 * next object (if there is one).
914 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
916 next_object
= object
->shadow
;
918 if (next_object
== VM_OBJECT_NULL
) {
920 * Absent page at bottom of shadow
921 * chain; zero fill the page we left
922 * busy in the first object, and free
925 assert(!must_be_resident
);
928 * check for any conditions that prevent
929 * us from creating a new zero-fill page
930 * vm_fault_check will do all of the
931 * fault cleanup in the case of an error condition
932 * including resetting the thread_interrupt_level
934 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
936 if (error
!= VM_FAULT_SUCCESS
)
940 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
941 (integer_t
)object
, offset
,
943 (integer_t
)first_object
, 0);
945 if (object
!= first_object
) {
947 * free the absent page we just found
952 * drop reference and lock on current object
954 vm_object_paging_end(object
);
955 vm_object_unlock(object
);
958 * grab the original page we
959 * 'soldered' in place and
960 * retake lock on 'first_object'
963 first_m
= VM_PAGE_NULL
;
965 object
= first_object
;
966 offset
= first_offset
;
968 vm_object_lock(object
);
971 * we're going to use the absent page we just found
972 * so convert it to a 'busy' page
978 * zero-fill the page and put it on
979 * the correct paging queue
981 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
985 if (must_be_resident
)
986 vm_object_paging_end(object
);
987 else if (object
!= first_object
) {
988 vm_object_paging_end(object
);
995 vm_page_lockspin_queues();
996 VM_PAGE_QUEUES_REMOVE(m
);
997 vm_page_unlock_queues();
1000 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1001 (integer_t
)object
, offset
,
1002 (integer_t
)next_object
,
1003 offset
+object
->shadow_offset
,0);
1005 offset
+= object
->shadow_offset
;
1006 fault_info
->lo_offset
+= object
->shadow_offset
;
1007 fault_info
->hi_offset
+= object
->shadow_offset
;
1008 access_required
= VM_PROT_READ
;
1010 vm_object_lock(next_object
);
1011 vm_object_unlock(object
);
1012 object
= next_object
;
1013 vm_object_paging_begin(object
);
1016 * reset to default type of fault
1018 my_fault
= DBG_CACHE_HIT_FAULT
;
1024 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1025 && (fault_type
& VM_PROT_WRITE
)) {
1027 * This is a copy-on-write fault that will
1028 * cause us to revoke access to this page, but
1029 * this page is in the process of being cleaned
1030 * in a clustered pageout. We must wait until
1031 * the cleaning operation completes before
1032 * revoking access to the original page,
1033 * otherwise we might attempt to remove a
1037 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1040 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1041 (integer_t
)object
, offset
,
1042 (integer_t
)m
, 0, 0);
1044 * take an extra ref so that object won't die
1046 vm_object_reference_locked(object
);
1048 vm_fault_cleanup(object
, first_m
);
1050 counter(c_vm_fault_page_block_backoff_kernel
++);
1051 vm_object_lock(object
);
1052 assert(object
->ref_count
> 0);
1054 m
= vm_page_lookup(object
, offset
);
1056 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
1057 PAGE_ASSERT_WAIT(m
, interruptible
);
1059 vm_object_unlock(object
);
1060 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1061 vm_object_deallocate(object
);
1065 vm_object_unlock(object
);
1067 vm_object_deallocate(object
);
1068 thread_interrupt_level(interruptible_state
);
1070 return (VM_FAULT_RETRY
);
1073 if (type_of_fault
== NULL
&& m
->speculative
) {
1075 * If we were passed a non-NULL pointer for
1076 * "type_of_fault", than we came from
1077 * vm_fault... we'll let it deal with
1078 * this condition, since it
1079 * needs to see m->speculative to correctly
1080 * account the pageins, otherwise...
1081 * take it off the speculative queue, we'll
1082 * let the caller of vm_fault_page deal
1083 * with getting it onto the correct queue
1085 vm_page_lockspin_queues();
1086 VM_PAGE_QUEUES_REMOVE(m
);
1087 vm_page_unlock_queues();
1093 * the user needs access to a page that we
1094 * encrypted before paging it out.
1095 * Decrypt the page now.
1096 * Keep it busy to prevent anyone from
1097 * accessing it during the decryption.
1100 vm_page_decrypt(m
, 0);
1101 assert(object
== m
->object
);
1103 PAGE_WAKEUP_DONE(m
);
1106 * Retry from the top, in case
1107 * something changed while we were
1112 ASSERT_PAGE_DECRYPTED(m
);
1114 if (m
->object
->code_signed
) {
1117 * We just paged in a page from a signed
1118 * memory object but we don't need to
1119 * validate it now. We'll validate it if
1120 * when it gets mapped into a user address
1121 * space for the first time or when the page
1122 * gets copied to another object as a result
1123 * of a copy-on-write.
1128 * We mark the page busy and leave it on
1129 * the pageout queues. If the pageout
1130 * deamon comes across it, then it will
1131 * remove the page from the queue, but not the object
1134 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1137 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1138 (integer_t
)object
, offset
, (integer_t
)m
, 0, 0);
1148 * we get here when there is no page present in the object at
1149 * the offset we're interested in... we'll allocate a page
1150 * at this point if the pager associated with
1151 * this object can provide the data or we're the top object...
1152 * object is locked; m == NULL
1154 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
) == TRUE
) && !data_supply
);
1157 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1159 if ((look_for_page
|| (object
== first_object
)) && !must_be_resident
&& !object
->phys_contiguous
) {
1161 * Allocate a new page for this object/offset pair
1165 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1167 if (m
== VM_PAGE_NULL
) {
1169 vm_fault_cleanup(object
, first_m
);
1170 thread_interrupt_level(interruptible_state
);
1172 return (VM_FAULT_MEMORY_SHORTAGE
);
1174 vm_page_insert(m
, object
, offset
);
1176 if (look_for_page
&& !must_be_resident
) {
1180 * If the memory manager is not ready, we
1181 * cannot make requests.
1183 if (!object
->pager_ready
) {
1185 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1187 if (m
!= VM_PAGE_NULL
)
1191 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1192 (integer_t
)object
, offset
, 0, 0, 0);
1195 * take an extra ref so object won't die
1197 vm_object_reference_locked(object
);
1198 vm_fault_cleanup(object
, first_m
);
1199 counter(c_vm_fault_page_block_backoff_kernel
++);
1201 vm_object_lock(object
);
1202 assert(object
->ref_count
> 0);
1204 if (!object
->pager_ready
) {
1205 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1207 vm_object_unlock(object
);
1208 if (wait_result
== THREAD_WAITING
)
1209 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1210 vm_object_deallocate(object
);
1214 vm_object_unlock(object
);
1215 vm_object_deallocate(object
);
1216 thread_interrupt_level(interruptible_state
);
1218 return (VM_FAULT_RETRY
);
1221 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1223 * If there are too many outstanding page
1224 * requests pending on this external object, we
1225 * wait for them to be resolved now.
1228 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1230 if (m
!= VM_PAGE_NULL
)
1233 * take an extra ref so object won't die
1235 vm_object_reference_locked(object
);
1237 vm_fault_cleanup(object
, first_m
);
1239 counter(c_vm_fault_page_block_backoff_kernel
++);
1241 vm_object_lock(object
);
1242 assert(object
->ref_count
> 0);
1244 if (object
->paging_in_progress
> vm_object_pagein_throttle
) {
1245 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
, interruptible
);
1247 vm_object_unlock(object
);
1248 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1249 vm_object_deallocate(object
);
1253 vm_object_unlock(object
);
1254 vm_object_deallocate(object
);
1255 thread_interrupt_level(interruptible_state
);
1257 return (VM_FAULT_RETRY
);
1260 if (m
!= VM_PAGE_NULL
) {
1262 * Indicate that the page is waiting for data
1263 * from the memory manager.
1265 m
->list_req_pending
= TRUE
;
1270 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1274 * It's possible someone called vm_object_destroy while we weren't
1275 * holding the object lock. If that has happened, then bail out
1279 pager
= object
->pager
;
1281 if (pager
== MEMORY_OBJECT_NULL
) {
1282 vm_fault_cleanup(object
, first_m
);
1283 thread_interrupt_level(interruptible_state
);
1284 return VM_FAULT_MEMORY_ERROR
;
1288 * We have an absent page in place for the faulting offset,
1289 * so we can release the object lock.
1292 vm_object_unlock(object
);
1295 * If this object uses a copy_call strategy,
1296 * and we are interested in a copy of this object
1297 * (having gotten here only by following a
1298 * shadow chain), then tell the memory manager
1299 * via a flag added to the desired_access
1300 * parameter, so that it can detect a race
1301 * between our walking down the shadow chain
1302 * and its pushing pages up into a copy of
1303 * the object that it manages.
1305 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
)
1306 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1308 wants_copy_flag
= VM_PROT_NONE
;
1311 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1312 (integer_t
)object
, offset
, (integer_t
)m
,
1313 access_required
| wants_copy_flag
, 0);
1316 * Call the memory manager to retrieve the data.
1318 rc
= memory_object_data_request(
1320 offset
+ object
->paging_offset
,
1322 access_required
| wants_copy_flag
,
1323 (memory_object_fault_info_t
)fault_info
);
1326 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1328 vm_object_lock(object
);
1330 if (rc
!= KERN_SUCCESS
) {
1332 vm_fault_cleanup(object
, first_m
);
1333 thread_interrupt_level(interruptible_state
);
1335 return ((rc
== MACH_SEND_INTERRUPTED
) ?
1336 VM_FAULT_INTERRUPTED
:
1337 VM_FAULT_MEMORY_ERROR
);
1339 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_mode
& TH_MODE_ABORT
)) {
1341 vm_fault_cleanup(object
, first_m
);
1342 thread_interrupt_level(interruptible_state
);
1344 return (VM_FAULT_INTERRUPTED
);
1346 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1348 * No page here means that the object we
1349 * initially looked up was "physically
1350 * contiguous" (i.e. device memory). However,
1351 * with Virtual VRAM, the object might not
1352 * be backed by that device memory anymore,
1353 * so we're done here only if the object is
1354 * still "phys_contiguous".
1355 * Otherwise, if the object is no longer
1356 * "phys_contiguous", we need to retry the
1357 * page fault against the object's new backing
1358 * store (different memory object).
1363 * potentially a pagein fault
1364 * if we make it through the state checks
1365 * above, than we'll count it as such
1367 my_fault
= DBG_PAGEIN_FAULT
;
1370 * Retry with same object/offset, since new data may
1371 * be in a different page (i.e., m is meaningless at
1378 * We get here if the object has no pager, or an existence map
1379 * exists and indicates the page isn't present on the pager
1380 * or we're unwiring a page. If a pager exists, but there
1381 * is no existence map, then the m->absent case above handles
1382 * the ZF case when the pager can't provide the page
1385 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1387 if (object
== first_object
)
1390 assert(m
== VM_PAGE_NULL
);
1393 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1394 (integer_t
)object
, offset
, (integer_t
)m
,
1395 (integer_t
)object
->shadow
, 0);
1397 next_object
= object
->shadow
;
1399 if (next_object
== VM_OBJECT_NULL
) {
1401 * we've hit the bottom of the shadown chain,
1402 * fill the page in the top object with zeros.
1404 assert(!must_be_resident
);
1406 if (object
!= first_object
) {
1407 vm_object_paging_end(object
);
1408 vm_object_unlock(object
);
1410 object
= first_object
;
1411 offset
= first_offset
;
1412 vm_object_lock(object
);
1415 assert(m
->object
== object
);
1416 first_m
= VM_PAGE_NULL
;
1419 * check for any conditions that prevent
1420 * us from creating a new zero-fill page
1421 * vm_fault_check will do all of the
1422 * fault cleanup in the case of an error condition
1423 * including resetting the thread_interrupt_level
1425 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
1427 if (error
!= VM_FAULT_SUCCESS
)
1430 if (m
== VM_PAGE_NULL
) {
1433 if (m
== VM_PAGE_NULL
) {
1434 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1435 thread_interrupt_level(interruptible_state
);
1437 return (VM_FAULT_MEMORY_SHORTAGE
);
1439 vm_page_insert(m
, object
, offset
);
1441 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1447 * Move on to the next object. Lock the next
1448 * object before unlocking the current one.
1450 if ((object
!= first_object
) || must_be_resident
)
1451 vm_object_paging_end(object
);
1453 offset
+= object
->shadow_offset
;
1454 fault_info
->lo_offset
+= object
->shadow_offset
;
1455 fault_info
->hi_offset
+= object
->shadow_offset
;
1456 access_required
= VM_PROT_READ
;
1458 vm_object_lock(next_object
);
1459 vm_object_unlock(object
);
1461 object
= next_object
;
1462 vm_object_paging_begin(object
);
1467 * PAGE HAS BEEN FOUND.
1470 * busy, so that we can play with it;
1471 * not absent, so that nobody else will fill it;
1472 * possibly eligible for pageout;
1474 * The top-level page (first_m) is:
1475 * VM_PAGE_NULL if the page was found in the
1477 * busy, not absent, and ineligible for pageout.
1479 * The current object (object) is locked. A paging
1480 * reference is held for the current and top-level
1485 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1487 #if EXTRA_ASSERTIONS
1488 if (m
!= VM_PAGE_NULL
) {
1489 assert(m
->busy
&& !m
->absent
);
1490 assert((first_m
== VM_PAGE_NULL
) ||
1491 (first_m
->busy
&& !first_m
->absent
&&
1492 !first_m
->active
&& !first_m
->inactive
));
1494 #endif /* EXTRA_ASSERTIONS */
1498 * If we found a page, we must have decrypted it before we
1501 if (m
!= VM_PAGE_NULL
) {
1502 ASSERT_PAGE_DECRYPTED(m
);
1506 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1507 (integer_t
)object
, offset
, (integer_t
)m
,
1508 (integer_t
)first_object
, (integer_t
)first_m
);
1511 * If the page is being written, but isn't
1512 * already owned by the top-level object,
1513 * we have to copy it into a new page owned
1514 * by the top-level object.
1516 if ((object
!= first_object
) && (m
!= VM_PAGE_NULL
)) {
1519 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1521 if (fault_type
& VM_PROT_WRITE
) {
1525 * We only really need to copy if we
1528 assert(!must_be_resident
);
1531 * are we protecting the system from
1532 * backing store exhaustion. If so
1533 * sleep unless we are privileged.
1535 if (vm_backing_store_low
) {
1536 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1539 vm_fault_cleanup(object
, first_m
);
1541 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1543 thread_block(THREAD_CONTINUE_NULL
);
1544 thread_interrupt_level(interruptible_state
);
1546 return (VM_FAULT_RETRY
);
1550 * If we try to collapse first_object at this
1551 * point, we may deadlock when we try to get
1552 * the lock on an intermediate object (since we
1553 * have the bottom object locked). We can't
1554 * unlock the bottom object, because the page
1555 * we found may move (by collapse) if we do.
1557 * Instead, we first copy the page. Then, when
1558 * we have no more use for the bottom object,
1559 * we unlock it and try to collapse.
1561 * Note that we copy the page even if we didn't
1562 * need to... that's the breaks.
1566 * Allocate a page for the copy
1568 copy_m
= vm_page_grab();
1570 if (copy_m
== VM_PAGE_NULL
) {
1573 vm_fault_cleanup(object
, first_m
);
1574 thread_interrupt_level(interruptible_state
);
1576 return (VM_FAULT_MEMORY_SHORTAGE
);
1579 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
1580 (integer_t
)object
, offset
,
1581 (integer_t
)m
, (integer_t
)copy_m
, 0);
1583 vm_page_copy(m
, copy_m
);
1586 * If another map is truly sharing this
1587 * page with us, we have to flush all
1588 * uses of the original page, since we
1589 * can't distinguish those which want the
1590 * original from those which need the
1593 * XXXO If we know that only one map has
1594 * access to this page, then we could
1595 * avoid the pmap_disconnect() call.
1598 pmap_disconnect(m
->phys_page
);
1600 assert(!m
->cleaning
);
1603 * We no longer need the old page or object.
1605 PAGE_WAKEUP_DONE(m
);
1606 vm_object_paging_end(object
);
1607 vm_object_unlock(object
);
1609 my_fault
= DBG_COW_FAULT
;
1610 VM_STAT_INCR(cow_faults
);
1611 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
1612 current_task()->cow_faults
++;
1614 object
= first_object
;
1615 offset
= first_offset
;
1617 vm_object_lock(object
);
1619 * get rid of the place holder
1620 * page that we soldered in earlier
1622 VM_PAGE_FREE(first_m
);
1623 first_m
= VM_PAGE_NULL
;
1626 * and replace it with the
1627 * page we just copied into
1629 assert(copy_m
->busy
);
1630 vm_page_insert(copy_m
, object
, offset
);
1631 copy_m
->dirty
= TRUE
;
1635 * Now that we've gotten the copy out of the
1636 * way, let's try to collapse the top object.
1637 * But we have to play ugly games with
1638 * paging_in_progress to do that...
1640 vm_object_paging_end(object
);
1641 vm_object_collapse(object
, offset
, TRUE
);
1642 vm_object_paging_begin(object
);
1645 *protection
&= (~VM_PROT_WRITE
);
1648 * Now check whether the page needs to be pushed into the
1649 * copy object. The use of asymmetric copy on write for
1650 * shared temporary objects means that we may do two copies to
1651 * satisfy the fault; one above to get the page from a
1652 * shadowed object, and one here to push it into the copy.
1654 try_failed_count
= 0;
1656 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
&& (m
!= VM_PAGE_NULL
)) {
1657 vm_object_offset_t copy_offset
;
1661 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1664 * If the page is being written, but hasn't been
1665 * copied to the copy-object, we have to copy it there.
1667 if ((fault_type
& VM_PROT_WRITE
) == 0) {
1668 *protection
&= ~VM_PROT_WRITE
;
1673 * If the page was guaranteed to be resident,
1674 * we must have already performed the copy.
1676 if (must_be_resident
)
1680 * Try to get the lock on the copy_object.
1682 if (!vm_object_lock_try(copy_object
)) {
1684 vm_object_unlock(object
);
1687 mutex_pause(try_failed_count
); /* wait a bit */
1688 vm_object_lock(object
);
1692 try_failed_count
= 0;
1695 * Make another reference to the copy-object,
1696 * to keep it from disappearing during the
1699 vm_object_reference_locked(copy_object
);
1702 * Does the page exist in the copy?
1704 copy_offset
= first_offset
- copy_object
->shadow_offset
;
1706 if (copy_object
->size
<= copy_offset
)
1708 * Copy object doesn't cover this page -- do nothing.
1711 else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
1713 * Page currently exists in the copy object
1717 * If the page is being brought
1718 * in, wait for it and then retry.
1723 * take an extra ref so object won't die
1725 vm_object_reference_locked(copy_object
);
1726 vm_object_unlock(copy_object
);
1727 vm_fault_cleanup(object
, first_m
);
1728 counter(c_vm_fault_page_block_backoff_kernel
++);
1730 vm_object_lock(copy_object
);
1731 assert(copy_object
->ref_count
> 0);
1732 VM_OBJ_RES_DECR(copy_object
);
1733 vm_object_lock_assert_exclusive(copy_object
);
1734 copy_object
->ref_count
--;
1735 assert(copy_object
->ref_count
> 0);
1736 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
1739 * it's OK if the "copy_m" page is encrypted,
1740 * because we're not moving it nor handling its
1743 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
1744 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
1746 vm_object_unlock(copy_object
);
1747 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1748 vm_object_deallocate(copy_object
);
1752 vm_object_unlock(copy_object
);
1753 vm_object_deallocate(copy_object
);
1754 thread_interrupt_level(interruptible_state
);
1756 return (VM_FAULT_RETRY
);
1760 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
1762 * If PAGED_OUT is TRUE, then the page used to exist
1763 * in the copy-object, and has already been paged out.
1764 * We don't need to repeat this. If PAGED_OUT is
1765 * FALSE, then either we don't know (!pager_created,
1766 * for example) or it hasn't been paged out.
1767 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
1768 * We must copy the page to the copy object.
1771 if (vm_backing_store_low
) {
1773 * we are protecting the system from
1774 * backing store exhaustion. If so
1775 * sleep unless we are privileged.
1777 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1778 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1781 VM_OBJ_RES_DECR(copy_object
);
1782 vm_object_lock_assert_exclusive(copy_object
);
1783 copy_object
->ref_count
--;
1784 assert(copy_object
->ref_count
> 0);
1786 vm_object_unlock(copy_object
);
1787 vm_fault_cleanup(object
, first_m
);
1788 thread_block(THREAD_CONTINUE_NULL
);
1789 thread_interrupt_level(interruptible_state
);
1791 return (VM_FAULT_RETRY
);
1795 * Allocate a page for the copy
1797 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
1799 if (copy_m
== VM_PAGE_NULL
) {
1802 VM_OBJ_RES_DECR(copy_object
);
1803 vm_object_lock_assert_exclusive(copy_object
);
1804 copy_object
->ref_count
--;
1805 assert(copy_object
->ref_count
> 0);
1807 vm_object_unlock(copy_object
);
1808 vm_fault_cleanup(object
, first_m
);
1809 thread_interrupt_level(interruptible_state
);
1811 return (VM_FAULT_MEMORY_SHORTAGE
);
1814 * Must copy page into copy-object.
1816 vm_page_copy(m
, copy_m
);
1819 * If the old page was in use by any users
1820 * of the copy-object, it must be removed
1821 * from all pmaps. (We can't know which
1825 pmap_disconnect(m
->phys_page
);
1828 * If there's a pager, then immediately
1829 * page out this page, using the "initialize"
1830 * option. Else, we use the copy.
1832 if ((!copy_object
->pager_created
)
1834 || vm_external_state_get(copy_object
->existence_map
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
1838 vm_page_lockspin_queues();
1839 assert(!m
->cleaning
);
1840 vm_page_activate(copy_m
);
1841 vm_page_unlock_queues();
1843 copy_m
->dirty
= TRUE
;
1844 PAGE_WAKEUP_DONE(copy_m
);
1847 assert(copy_m
->busy
== TRUE
);
1848 assert(!m
->cleaning
);
1851 * dirty is protected by the object lock
1853 copy_m
->dirty
= TRUE
;
1856 * The page is already ready for pageout:
1857 * not on pageout queues and busy.
1858 * Unlock everything except the
1859 * copy_object itself.
1861 vm_object_unlock(object
);
1864 * Write the page to the copy-object,
1865 * flushing it from the kernel.
1867 vm_pageout_initialize_page(copy_m
);
1870 * Since the pageout may have
1871 * temporarily dropped the
1872 * copy_object's lock, we
1873 * check whether we'll have
1874 * to deallocate the hard way.
1876 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
1877 vm_object_unlock(copy_object
);
1878 vm_object_deallocate(copy_object
);
1879 vm_object_lock(object
);
1884 * Pick back up the old object's
1885 * lock. [It is safe to do so,
1886 * since it must be deeper in the
1889 vm_object_lock(object
);
1892 * Because we're pushing a page upward
1893 * in the object tree, we must restart
1894 * any faults that are waiting here.
1895 * [Note that this is an expansion of
1896 * PAGE_WAKEUP that uses the THREAD_RESTART
1897 * wait result]. Can't turn off the page's
1898 * busy bit because we're not done with it.
1902 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
1906 * The reference count on copy_object must be
1907 * at least 2: one for our extra reference,
1908 * and at least one from the outside world
1909 * (we checked that when we last locked
1912 vm_object_lock_assert_exclusive(copy_object
);
1913 copy_object
->ref_count
--;
1914 assert(copy_object
->ref_count
> 0);
1916 VM_OBJ_RES_DECR(copy_object
);
1917 vm_object_unlock(copy_object
);
1922 *top_page
= first_m
;
1925 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
1926 (integer_t
)object
, offset
, (integer_t
)m
, (integer_t
)first_m
, 0);
1928 if (m
!= VM_PAGE_NULL
) {
1929 if (my_fault
== DBG_PAGEIN_FAULT
) {
1931 VM_STAT_INCR(pageins
);
1932 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
1933 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1934 current_task()->pageins
++;
1936 if (m
->object
->internal
) {
1937 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
1939 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
1943 * evaluate access pattern and update state
1944 * vm_fault_deactivate_behind depends on the
1945 * state being up to date
1947 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
1949 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
1952 *type_of_fault
= my_fault
;
1954 vm_object_unlock(object
);
1956 thread_interrupt_level(interruptible_state
);
1959 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
1961 return (VM_FAULT_SUCCESS
);
1964 thread_interrupt_level(interruptible_state
);
1966 if (wait_result
== THREAD_INTERRUPTED
)
1967 return (VM_FAULT_INTERRUPTED
);
1968 return (VM_FAULT_RETRY
);
1977 * When soft faulting a page, we have to validate the page if:
1978 * 1. the page is being mapped in user space
1979 * 2. the page hasn't already been found to be "tainted"
1980 * 3. the page belongs to a code-signed object
1981 * 4. the page has not been validated yet or has been mapped for write.
1983 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page) \
1984 ((pmap) != kernel_pmap /*1*/ && \
1985 !(page)->cs_tainted /*2*/ && \
1986 (page)->object->code_signed /*3*/ && \
1987 (!(page)->cs_validated || (page)->wpmapped /*4*/))
1991 * page queue lock must NOT be held
1992 * m->object must be locked
1994 * NOTE: m->object could be locked "shared" only if we are called
1995 * from vm_fault() as part of a soft fault. If so, we must be
1996 * careful not to modify the VM object in any way that is not
1997 * legal under a shared lock...
1999 unsigned long cs_enter_tainted_rejected
= 0;
2000 unsigned long cs_enter_tainted_accepted
= 0;
2002 vm_fault_enter(vm_page_t m
,
2004 vm_map_offset_t vaddr
,
2007 boolean_t change_wiring
,
2011 unsigned int cache_attr
;
2013 boolean_t previously_pmapped
= m
->pmapped
;
2015 vm_object_lock_assert_held(m
->object
);
2017 mutex_assert(&vm_page_queue_lock
, MA_NOTOWNED
);
2020 if (m
->phys_page
== vm_page_guard_addr
) {
2021 assert(m
->fictitious
);
2022 return KERN_SUCCESS
;
2025 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
2027 if (m
->pmapped
== FALSE
) {
2029 * This is the first time this page is being
2030 * mapped in an address space (pmapped == FALSE).
2032 * Part of that page may still be in the data cache
2033 * and not flushed to memory. In case we end up
2034 * accessing that page via the instruction cache,
2035 * we need to ensure that the 2 caches are in sync.
2037 pmap_sync_page_data_phys(m
->phys_page
);
2039 if ((*type_of_fault
== DBG_CACHE_HIT_FAULT
) && m
->clustered
) {
2041 * found it in the cache, but this
2042 * is the first fault-in of the page (m->pmapped == FALSE)
2043 * so it must have come in as part of
2044 * a cluster... account 1 pagein against it
2046 VM_STAT_INCR(pageins
);
2047 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
2049 if (m
->object
->internal
) {
2050 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
2052 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
2055 current_task()->pageins
++;
2057 *type_of_fault
= DBG_PAGEIN_FAULT
;
2059 VM_PAGE_CONSUME_CLUSTERED(m
);
2061 } else if (cache_attr
!= VM_WIMG_DEFAULT
)
2062 pmap_sync_page_attributes_phys(m
->phys_page
);
2064 if (*type_of_fault
!= DBG_COW_FAULT
) {
2065 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2067 if (pmap
== kernel_pmap
) {
2068 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2072 if (VM_FAULT_NEED_CS_VALIDATION(pmap
, m
)) {
2073 vm_object_lock_assert_exclusive(m
->object
);
2075 if (m
->cs_validated
) {
2076 vm_cs_revalidates
++;
2079 /* VM map is locked, so 1 ref will remain on VM object */
2080 vm_page_validate_cs(m
);
2083 if (m
->cs_tainted
/* always invalidate a tainted page */
2084 #if CONFIG_ENFORCE_SIGNED_CODE
2086 * Code Signing enforcement invalidates an executable page that
2087 * has no code directory, and thus could not be validated.
2089 || ((prot
& VM_PROT_EXECUTE
) && !m
->cs_validated
)
2094 * This page has been tainted and can not be trusted.
2095 * Let's notify the current process and let it take any
2096 * necessary precautions before we enter the tainted page
2097 * into its address space.
2100 #if CONFIG_ENFORCE_SIGNED_CODE
2101 if (!cs_enforcement_disable
) {
2103 if (cs_invalid_page((addr64_t
) vaddr
)) {
2104 /* reject the tainted page: abort the page fault */
2105 kr
= KERN_MEMORY_ERROR
;
2106 cs_enter_tainted_rejected
++;
2108 /* proceed with the tainted page */
2110 cs_enter_tainted_accepted
++;
2112 #if CONFIG_ENFORCE_SIGNED_CODE
2115 if (cs_debug
|| kr
!= KERN_SUCCESS
) {
2116 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2117 "page %p obj %p off 0x%llx *** INVALID PAGE ***\n",
2118 (long long)vaddr
, m
, m
->object
, m
->offset
);
2121 /* proceed with the valid page */
2125 if (kr
== KERN_SUCCESS
) {
2127 * NOTE: we may only hold the vm_object lock SHARED
2128 * at this point, but the update of pmapped is ok
2129 * since this is the ONLY bit updated behind the SHARED
2130 * lock... however, we need to figure out how to do an atomic
2131 * update on a bit field to make this less fragile... right
2132 * now I don't know how to coerce 'C' to give me the offset info
2133 * that's needed for an AtomicCompareAndSwap
2136 if (prot
& VM_PROT_WRITE
) {
2137 vm_object_lock_assert_exclusive(m
->object
);
2141 PMAP_ENTER(pmap
, vaddr
, m
, prot
, cache_attr
, wired
);
2145 * Hold queues lock to manipulate
2146 * the page queues. Change wiring
2149 if (change_wiring
) {
2150 vm_page_lockspin_queues();
2153 if (kr
== KERN_SUCCESS
) {
2159 vm_page_unlock_queues();
2162 if (kr
!= KERN_SUCCESS
) {
2163 vm_page_lock_queues();
2164 vm_page_deactivate(m
);
2165 vm_page_unlock_queues();
2167 if (((!m
->active
&& !m
->inactive
) || no_cache
) && !m
->wire_count
&& !m
->throttled
) {
2168 vm_page_lockspin_queues();
2170 * test again now that we hold the page queue lock
2172 if (((!m
->active
&& !m
->inactive
) || no_cache
) && !m
->wire_count
) {
2175 * If this is a no_cache mapping and the page has never been
2176 * mapped before or was previously a no_cache page, then we
2177 * want to leave pages in the speculative state so that they
2178 * can be readily recycled if free memory runs low. Otherwise
2179 * the page is activated as normal.
2182 if (no_cache
&& (!previously_pmapped
|| m
->no_cache
)) {
2185 if (m
->active
|| m
->inactive
)
2186 VM_PAGE_QUEUES_REMOVE(m
);
2188 if (!m
->speculative
)
2189 vm_page_speculate(m
, TRUE
);
2191 } else if (!m
->active
&& !m
->inactive
)
2192 vm_page_activate(m
);
2196 vm_page_unlock_queues();
2207 * Handle page faults, including pseudo-faults
2208 * used to change the wiring status of pages.
2210 * Explicit continuations have been removed.
2212 * vm_fault and vm_fault_page save mucho state
2213 * in the moral equivalent of a closure. The state
2214 * structure is allocated when first entering vm_fault
2215 * and deallocated when leaving vm_fault.
2218 extern int _map_enter_debug
;
2220 unsigned long vm_fault_collapse_total
= 0;
2221 unsigned long vm_fault_collapse_skipped
= 0;
2226 vm_map_offset_t vaddr
,
2227 vm_prot_t fault_type
,
2228 boolean_t change_wiring
,
2231 vm_map_offset_t caller_pmap_addr
)
2233 vm_map_version_t version
; /* Map version for verificiation */
2234 boolean_t wired
; /* Should mapping be wired down? */
2235 vm_object_t object
; /* Top-level object */
2236 vm_object_offset_t offset
; /* Top-level offset */
2237 vm_prot_t prot
; /* Protection for mapping */
2238 vm_object_t old_copy_object
; /* Saved copy object */
2239 vm_page_t result_page
; /* Result of vm_fault_page */
2240 vm_page_t top_page
; /* Placeholder page */
2243 vm_page_t m
; /* Fast access to result_page */
2244 kern_return_t error_code
;
2245 vm_object_t cur_object
;
2246 vm_object_offset_t cur_offset
;
2248 vm_object_t new_object
;
2251 boolean_t interruptible_state
;
2252 vm_map_t real_map
= map
;
2253 vm_map_t original_map
= map
;
2254 vm_prot_t original_fault_type
;
2255 struct vm_object_fault_info fault_info
;
2256 boolean_t need_collapse
= FALSE
;
2257 int object_lock_type
= 0;
2258 int cur_object_lock_type
;
2261 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
2262 (int)((uint64_t)vaddr
>> 32),
2268 if (get_preemption_level() != 0) {
2269 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
2270 (int)((uint64_t)vaddr
>> 32),
2276 return (KERN_FAILURE
);
2278 interruptible_state
= thread_interrupt_level(interruptible
);
2280 VM_STAT_INCR(faults
);
2281 current_task()->faults
++;
2282 original_fault_type
= fault_type
;
2284 if (fault_type
& VM_PROT_WRITE
)
2285 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2287 object_lock_type
= OBJECT_LOCK_SHARED
;
2289 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
2293 * assume we will hit a page in the cache
2294 * otherwise, explicitly override with
2295 * the real fault type once we determine it
2297 type_of_fault
= DBG_CACHE_HIT_FAULT
;
2300 * Find the backing store object and offset into
2301 * it to begin the search.
2303 fault_type
= original_fault_type
;
2305 vm_map_lock_read(map
);
2307 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
2308 object_lock_type
, &version
,
2309 &object
, &offset
, &prot
, &wired
,
2313 if (kr
!= KERN_SUCCESS
) {
2314 vm_map_unlock_read(map
);
2317 pmap
= real_map
->pmap
;
2318 fault_info
.interruptible
= interruptible
;
2321 * If the page is wired, we must fault for the current protection
2322 * value, to avoid further faults.
2325 fault_type
= prot
| VM_PROT_WRITE
;
2327 * since we're treating this fault as a 'write'
2328 * we must hold the top object lock exclusively
2330 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2332 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2334 if (vm_object_lock_upgrade(object
) == FALSE
) {
2336 * couldn't upgrade, so explictly
2337 * take the lock exclusively
2339 vm_object_lock(object
);
2344 #if VM_FAULT_CLASSIFY
2346 * Temporary data gathering code
2348 vm_fault_classify(object
, offset
, fault_type
);
2351 * Fast fault code. The basic idea is to do as much as
2352 * possible while holding the map lock and object locks.
2353 * Busy pages are not used until the object lock has to
2354 * be dropped to do something (copy, zero fill, pmap enter).
2355 * Similarly, paging references aren't acquired until that
2356 * point, and object references aren't used.
2358 * If we can figure out what to do
2359 * (zero fill, copy on write, pmap enter) while holding
2360 * the locks, then it gets done. Otherwise, we give up,
2361 * and use the original fault path (which doesn't hold
2362 * the map lock, and relies on busy pages).
2363 * The give up cases include:
2364 * - Have to talk to pager.
2365 * - Page is busy, absent or in error.
2366 * - Pager has locked out desired access.
2367 * - Fault needs to be restarted.
2368 * - Have to push page into copy object.
2370 * The code is an infinite loop that moves one level down
2371 * the shadow chain each time. cur_object and cur_offset
2372 * refer to the current object being examined. object and offset
2373 * are the original object from the map. The loop is at the
2374 * top level if and only if object and cur_object are the same.
2376 * Invariants: Map lock is held throughout. Lock is held on
2377 * original object and cur_object (if different) when
2378 * continuing or exiting loop.
2384 * If this page is to be inserted in a copy delay object
2385 * for writing, and if the object has a copy, then the
2386 * copy delay strategy is implemented in the slow fault page.
2388 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
2389 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
))
2390 goto handle_copy_delay
;
2392 cur_object
= object
;
2393 cur_offset
= offset
;
2396 m
= vm_page_lookup(cur_object
, cur_offset
);
2398 if (m
!= VM_PAGE_NULL
) {
2400 wait_result_t result
;
2403 * in order to do the PAGE_ASSERT_WAIT, we must
2404 * have object that 'm' belongs to locked exclusively
2406 if (object
!= cur_object
) {
2407 vm_object_unlock(object
);
2409 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2411 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2413 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2415 * couldn't upgrade so go do a full retry
2416 * immediately since we've already dropped
2417 * the top object lock associated with this page
2418 * and the current one got dropped due to the
2419 * failed upgrade... the state is no longer valid
2421 vm_map_unlock_read(map
);
2422 if (real_map
!= map
)
2423 vm_map_unlock(real_map
);
2428 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2430 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2432 if (vm_object_lock_upgrade(object
) == FALSE
) {
2434 * couldn't upgrade, so explictly take the lock
2435 * exclusively and go relookup the page since we
2436 * will have dropped the object lock and
2437 * a different thread could have inserted
2438 * a page at this offset
2439 * no need for a full retry since we're
2440 * at the top level of the object chain
2442 vm_object_lock(object
);
2447 vm_map_unlock_read(map
);
2448 if (real_map
!= map
)
2449 vm_map_unlock(real_map
);
2451 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
2453 vm_object_unlock(cur_object
);
2455 if (result
== THREAD_WAITING
) {
2456 result
= thread_block(THREAD_CONTINUE_NULL
);
2458 counter(c_vm_fault_page_block_busy_kernel
++);
2460 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
2466 if (m
->phys_page
== vm_page_guard_addr
) {
2468 * Guard page: let the slow path deal with it
2472 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
)) {
2474 * Unusual case... let the slow path deal with it
2481 * We've soft-faulted (because it's not in the page
2482 * table) on an encrypted page.
2483 * Keep the page "busy" so that no one messes with
2484 * it during the decryption.
2485 * Release the extra locks we're holding, keep only
2486 * the page's VM object lock.
2488 * in order to set 'busy' on 'm', we must
2489 * have object that 'm' belongs to locked exclusively
2491 if (object
!= cur_object
) {
2492 vm_object_unlock(object
);
2494 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2496 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2498 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2500 * couldn't upgrade so go do a full retry
2501 * immediately since we've already dropped
2502 * the top object lock associated with this page
2503 * and the current one got dropped due to the
2504 * failed upgrade... the state is no longer valid
2506 vm_map_unlock_read(map
);
2507 if (real_map
!= map
)
2508 vm_map_unlock(real_map
);
2513 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2515 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2517 if (vm_object_lock_upgrade(object
) == FALSE
) {
2519 * couldn't upgrade, so explictly take the lock
2520 * exclusively and go relookup the page since we
2521 * will have dropped the object lock and
2522 * a different thread could have inserted
2523 * a page at this offset
2524 * no need for a full retry since we're
2525 * at the top level of the object chain
2527 vm_object_lock(object
);
2534 vm_map_unlock_read(map
);
2535 if (real_map
!= map
)
2536 vm_map_unlock(real_map
);
2538 vm_page_decrypt(m
, 0);
2541 PAGE_WAKEUP_DONE(m
);
2543 vm_object_unlock(cur_object
);
2545 * Retry from the top, in case anything
2546 * changed while we were decrypting...
2550 ASSERT_PAGE_DECRYPTED(m
);
2552 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
)) {
2554 * We might need to validate this page
2555 * against its code signature, so we
2556 * want to hold the VM object exclusively.
2558 if (object
!= cur_object
) {
2559 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2560 vm_object_unlock(object
);
2561 vm_object_unlock(cur_object
);
2563 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2565 vm_map_unlock_read(map
);
2566 if (real_map
!= map
)
2567 vm_map_unlock(real_map
);
2572 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2574 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2576 if (vm_object_lock_upgrade(object
) == FALSE
) {
2578 * couldn't upgrade, so explictly take the lock
2579 * exclusively and go relookup the page since we
2580 * will have dropped the object lock and
2581 * a different thread could have inserted
2582 * a page at this offset
2583 * no need for a full retry since we're
2584 * at the top level of the object chain
2586 vm_object_lock(object
);
2593 * Two cases of map in faults:
2594 * - At top level w/o copy object.
2595 * - Read fault anywhere.
2596 * --> must disallow write.
2599 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
2600 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2602 * This is not a "write" fault, so we
2603 * might not have taken the object lock
2604 * exclusively and we might not be able
2605 * to update the "wpmapped" bit in
2607 * Let's just grant read access to
2608 * the page for now and we'll
2609 * soft-fault again if we need write
2612 prot
&= ~VM_PROT_WRITE
;
2617 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2619 prot
&= ~VM_PROT_WRITE
;
2622 * Set up to map the page...
2623 * mark the page busy, drop
2624 * unneeded object lock
2626 if (object
!= cur_object
) {
2628 * don't need the original object anymore
2630 vm_object_unlock(object
);
2633 * switch to the object that has the new page
2635 object
= cur_object
;
2636 object_lock_type
= cur_object_lock_type
;
2640 * prepare for the pmap_enter...
2641 * object and map are both locked
2642 * m contains valid data
2643 * object == m->object
2644 * cur_object == NULL or it's been unlocked
2645 * no paging references on either object or cur_object
2648 if (db_watchpoint_list
&& (fault_type
& VM_PROT_WRITE
) == 0)
2649 prot
&= ~VM_PROT_WRITE
;
2652 kr
= vm_fault_enter(m
,
2658 fault_info
.no_cache
,
2661 kr
= vm_fault_enter(m
,
2667 fault_info
.no_cache
,
2671 if (need_collapse
== TRUE
)
2672 vm_object_collapse(object
, offset
, TRUE
);
2674 if (type_of_fault
== DBG_PAGEIN_FAULT
) {
2676 * evaluate access pattern and update state
2677 * vm_fault_deactivate_behind depends on the
2678 * state being up to date
2680 vm_fault_is_sequential(object
, cur_offset
, fault_info
.behavior
);
2682 vm_fault_deactivate_behind(object
, cur_offset
, fault_info
.behavior
);
2685 * That's it, clean up and return.
2688 PAGE_WAKEUP_DONE(m
);
2690 vm_object_unlock(object
);
2692 vm_map_unlock_read(map
);
2693 if (real_map
!= map
)
2694 vm_map_unlock(real_map
);
2699 * COPY ON WRITE FAULT
2701 * If objects match, then
2702 * object->copy must not be NULL (else control
2703 * would be in previous code block), and we
2704 * have a potential push into the copy object
2705 * with which we can't cope with here.
2707 if (cur_object
== object
) {
2709 * must take the slow path to
2710 * deal with the copy push
2714 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
2717 * This is now a shadow based copy on write
2718 * fault -- it requires a copy up the shadow
2721 * Allocate a page in the original top level
2722 * object. Give up if allocate fails. Also
2723 * need to remember current page, as it's the
2724 * source of the copy.
2726 * at this point we hold locks on both
2727 * object and cur_object... no need to take
2728 * paging refs or mark pages BUSY since
2729 * we don't drop either object lock until
2730 * the page has been copied and inserted
2735 if (m
== VM_PAGE_NULL
) {
2737 * no free page currently available...
2738 * must take the slow path
2743 * Now do the copy. Mark the source page busy...
2745 * NOTE: This code holds the map lock across
2748 vm_page_copy(cur_m
, m
);
2749 vm_page_insert(m
, object
, offset
);
2753 * Now cope with the source page and object
2755 if (object
->ref_count
> 1 && cur_m
->pmapped
)
2756 pmap_disconnect(cur_m
->phys_page
);
2758 need_collapse
= TRUE
;
2760 if (!cur_object
->internal
&&
2761 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
2763 * The object from which we've just
2764 * copied a page is most probably backed
2765 * by a vnode. We don't want to waste too
2766 * much time trying to collapse the VM objects
2767 * and create a bottleneck when several tasks
2768 * map the same file.
2770 if (cur_object
->copy
== object
) {
2772 * Shared mapping or no COW yet.
2773 * We can never collapse a copy
2774 * object into its backing object.
2776 need_collapse
= FALSE
;
2777 } else if (cur_object
->copy
== object
->shadow
&&
2778 object
->shadow
->resident_page_count
== 0) {
2780 * Shared mapping after a COW occurred.
2782 need_collapse
= FALSE
;
2785 vm_object_unlock(cur_object
);
2787 if (need_collapse
== FALSE
)
2788 vm_fault_collapse_skipped
++;
2789 vm_fault_collapse_total
++;
2791 type_of_fault
= DBG_COW_FAULT
;
2792 VM_STAT_INCR(cow_faults
);
2793 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2794 current_task()->cow_faults
++;
2800 * No page at cur_object, cur_offset... m == NULL
2802 if (cur_object
->pager_created
) {
2803 if (MUST_ASK_PAGER(cur_object
, cur_offset
) == TRUE
) {
2805 * May have to talk to a pager...
2806 * take the slow path.
2811 * existence map present and indicates
2812 * that the pager doesn't have this page
2815 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
2817 * Zero fill fault. Page gets
2818 * inserted into the original object.
2820 if (cur_object
->shadow_severed
) {
2822 if (object
!= cur_object
)
2823 vm_object_unlock(cur_object
);
2824 vm_object_unlock(object
);
2826 vm_map_unlock_read(map
);
2827 if (real_map
!= map
)
2828 vm_map_unlock(real_map
);
2830 kr
= KERN_MEMORY_ERROR
;
2833 if (VM_PAGE_ZFILL_THROTTLED()) {
2835 * drop all of our locks...
2836 * wait until the free queue is
2837 * pumped back up and then
2840 if (object
!= cur_object
)
2841 vm_object_unlock(cur_object
);
2842 vm_object_unlock(object
);
2843 vm_map_unlock_read(map
);
2844 if (real_map
!= map
)
2845 vm_map_unlock(real_map
);
2847 if (vm_page_wait((change_wiring
) ?
2855 if (vm_backing_store_low
) {
2857 * we are protecting the system from
2858 * backing store exhaustion...
2859 * must take the slow path if we're
2862 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
))
2865 if (cur_object
!= object
) {
2866 vm_object_unlock(cur_object
);
2868 cur_object
= object
;
2870 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2872 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2874 if (vm_object_lock_upgrade(object
) == FALSE
) {
2876 * couldn't upgrade so do a full retry on the fault
2877 * since we dropped the object lock which
2878 * could allow another thread to insert
2879 * a page at this offset
2881 vm_map_unlock_read(map
);
2882 if (real_map
!= map
)
2883 vm_map_unlock(real_map
);
2888 m
= vm_page_alloc(object
, offset
);
2890 if (m
== VM_PAGE_NULL
) {
2892 * no free page currently available...
2893 * must take the slow path
2899 * Now zero fill page...
2900 * the page is probably going to
2901 * be written soon, so don't bother
2902 * to clear the modified bit
2904 * NOTE: This code holds the map
2905 * lock across the zero fill.
2907 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
2912 * On to the next level in the shadow chain
2914 cur_offset
+= cur_object
->shadow_offset
;
2915 new_object
= cur_object
->shadow
;
2918 * take the new_object's lock with the indicated state
2920 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
)
2921 vm_object_lock_shared(new_object
);
2923 vm_object_lock(new_object
);
2925 if (cur_object
!= object
)
2926 vm_object_unlock(cur_object
);
2928 cur_object
= new_object
;
2934 * Cleanup from fast fault failure. Drop any object
2935 * lock other than original and drop map lock.
2937 if (object
!= cur_object
)
2938 vm_object_unlock(cur_object
);
2941 * must own the object lock exclusively at this point
2943 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2944 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2946 if (vm_object_lock_upgrade(object
) == FALSE
) {
2948 * couldn't upgrade, so explictly
2949 * take the lock exclusively
2950 * no need to retry the fault at this
2951 * point since "vm_fault_page" will
2952 * completely re-evaluate the state
2954 vm_object_lock(object
);
2959 vm_map_unlock_read(map
);
2960 if (real_map
!= map
)
2961 vm_map_unlock(real_map
);
2964 * Make a reference to this object to
2965 * prevent its disposal while we are messing with
2966 * it. Once we have the reference, the map is free
2967 * to be diddled. Since objects reference their
2968 * shadows (and copies), they will stay around as well.
2970 vm_object_reference_locked(object
);
2971 vm_object_paging_begin(object
);
2973 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
2977 kr
= vm_fault_page(object
, offset
, fault_type
,
2978 (change_wiring
&& !wired
),
2979 &prot
, &result_page
, &top_page
,
2981 &error_code
, map
->no_zero_fill
,
2982 FALSE
, &fault_info
);
2985 * if kr != VM_FAULT_SUCCESS, then the paging reference
2986 * has been dropped and the object unlocked... the ref_count
2989 * if kr == VM_FAULT_SUCCESS, then the paging reference
2990 * is still held along with the ref_count on the original object
2992 * if m != NULL, then the object it belongs to
2993 * is returned locked with a paging reference
2995 * if top_page != NULL, then it's BUSY and the
2996 * object it belongs to has a paging reference
2997 * but is returned unlocked
2999 if (kr
!= VM_FAULT_SUCCESS
) {
3001 * we didn't succeed, lose the object reference immediately.
3003 vm_object_deallocate(object
);
3006 * See why we failed, and take corrective action.
3009 case VM_FAULT_MEMORY_SHORTAGE
:
3010 if (vm_page_wait((change_wiring
) ?
3017 case VM_FAULT_INTERRUPTED
:
3020 case VM_FAULT_RETRY
:
3022 case VM_FAULT_MEMORY_ERROR
:
3026 kr
= KERN_MEMORY_ERROR
;
3032 if (m
!= VM_PAGE_NULL
) {
3033 assert((change_wiring
&& !wired
) ?
3034 (top_page
== VM_PAGE_NULL
) :
3035 ((top_page
== VM_PAGE_NULL
) == (m
->object
== object
)));
3039 * What to do with the resulting page from vm_fault_page
3040 * if it doesn't get entered into the physical map:
3042 #define RELEASE_PAGE(m) \
3044 PAGE_WAKEUP_DONE(m); \
3045 vm_page_lockspin_queues(); \
3046 if (!m->active && !m->inactive && !m->throttled)\
3047 vm_page_activate(m); \
3048 vm_page_unlock_queues(); \
3052 * We must verify that the maps have not changed
3053 * since our last lookup.
3055 if (m
!= VM_PAGE_NULL
) {
3056 old_copy_object
= m
->object
->copy
;
3057 vm_object_unlock(m
->object
);
3059 old_copy_object
= VM_OBJECT_NULL
;
3062 * no object locks are held at this point
3064 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
3065 vm_object_t retry_object
;
3066 vm_object_offset_t retry_offset
;
3067 vm_prot_t retry_prot
;
3070 * To avoid trying to write_lock the map while another
3071 * thread has it read_locked (in vm_map_pageable), we
3072 * do not try for write permission. If the page is
3073 * still writable, we will get write permission. If it
3074 * is not, or has been marked needs_copy, we enter the
3075 * mapping without write permission, and will merely
3076 * take another fault.
3079 vm_map_lock_read(map
);
3081 kr
= vm_map_lookup_locked(&map
, vaddr
,
3082 fault_type
& ~VM_PROT_WRITE
,
3083 OBJECT_LOCK_EXCLUSIVE
, &version
,
3084 &retry_object
, &retry_offset
, &retry_prot
,
3088 pmap
= real_map
->pmap
;
3090 if (kr
!= KERN_SUCCESS
) {
3091 vm_map_unlock_read(map
);
3093 if (m
!= VM_PAGE_NULL
) {
3095 * retake the lock so that
3096 * we can drop the paging reference
3097 * in vm_fault_cleanup and do the
3098 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3100 vm_object_lock(m
->object
);
3104 vm_fault_cleanup(m
->object
, top_page
);
3107 * retake the lock so that
3108 * we can drop the paging reference
3109 * in vm_fault_cleanup
3111 vm_object_lock(object
);
3113 vm_fault_cleanup(object
, top_page
);
3115 vm_object_deallocate(object
);
3119 vm_object_unlock(retry_object
);
3121 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
3123 vm_map_unlock_read(map
);
3124 if (real_map
!= map
)
3125 vm_map_unlock(real_map
);
3127 if (m
!= VM_PAGE_NULL
) {
3129 * retake the lock so that
3130 * we can drop the paging reference
3131 * in vm_fault_cleanup and do the
3132 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3134 vm_object_lock(m
->object
);
3138 vm_fault_cleanup(m
->object
, top_page
);
3141 * retake the lock so that
3142 * we can drop the paging reference
3143 * in vm_fault_cleanup
3145 vm_object_lock(object
);
3147 vm_fault_cleanup(object
, top_page
);
3149 vm_object_deallocate(object
);
3154 * Check whether the protection has changed or the object
3155 * has been copied while we left the map unlocked.
3159 if (m
!= VM_PAGE_NULL
) {
3160 vm_object_lock(m
->object
);
3162 if (m
->object
->copy
!= old_copy_object
) {
3164 * The copy object changed while the top-level object
3165 * was unlocked, so take away write permission.
3167 prot
&= ~VM_PROT_WRITE
;
3170 vm_object_lock(object
);
3173 * If we want to wire down this page, but no longer have
3174 * adequate permissions, we must start all over.
3176 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
3178 vm_map_verify_done(map
, &version
);
3179 if (real_map
!= map
)
3180 vm_map_unlock(real_map
);
3182 if (m
!= VM_PAGE_NULL
) {
3185 vm_fault_cleanup(m
->object
, top_page
);
3187 vm_fault_cleanup(object
, top_page
);
3189 vm_object_deallocate(object
);
3193 if (m
!= VM_PAGE_NULL
) {
3195 * Put this page into the physical map.
3196 * We had to do the unlock above because pmap_enter
3197 * may cause other faults. The page may be on
3198 * the pageout queues. If the pageout daemon comes
3199 * across the page, it will remove it from the queues.
3202 kr
= vm_fault_enter(m
,
3208 fault_info
.no_cache
,
3211 kr
= vm_fault_enter(m
,
3217 fault_info
.no_cache
,
3220 if (kr
!= KERN_SUCCESS
) {
3221 /* abort this page fault */
3222 vm_map_verify_done(map
, &version
);
3223 if (real_map
!= map
)
3224 vm_map_unlock(real_map
);
3225 PAGE_WAKEUP_DONE(m
);
3226 vm_fault_cleanup(m
->object
, top_page
);
3227 vm_object_deallocate(object
);
3232 vm_map_entry_t entry
;
3233 vm_map_offset_t laddr
;
3234 vm_map_offset_t ldelta
, hdelta
;
3237 * do a pmap block mapping from the physical address
3242 /* While we do not worry about execution protection in */
3243 /* general, certian pages may have instruction execution */
3244 /* disallowed. We will check here, and if not allowed */
3245 /* to execute, we return with a protection failure. */
3247 if ((fault_type
& VM_PROT_EXECUTE
) &&
3248 (!pmap_eligible_for_execute((ppnum_t
)(object
->shadow_offset
>> 12)))) {
3250 vm_map_verify_done(map
, &version
);
3252 if (real_map
!= map
)
3253 vm_map_unlock(real_map
);
3255 vm_fault_cleanup(object
, top_page
);
3256 vm_object_deallocate(object
);
3258 kr
= KERN_PROTECTION_FAILURE
;
3263 if (real_map
!= map
)
3264 vm_map_unlock(real_map
);
3266 if (original_map
!= map
) {
3267 vm_map_unlock_read(map
);
3268 vm_map_lock_read(original_map
);
3274 hdelta
= 0xFFFFF000;
3275 ldelta
= 0xFFFFF000;
3277 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
3278 if (ldelta
> (laddr
- entry
->vme_start
))
3279 ldelta
= laddr
- entry
->vme_start
;
3280 if (hdelta
> (entry
->vme_end
- laddr
))
3281 hdelta
= entry
->vme_end
- laddr
;
3282 if (entry
->is_sub_map
) {
3284 laddr
= (laddr
- entry
->vme_start
)
3286 vm_map_lock_read(entry
->object
.sub_map
);
3288 if (map
!= real_map
)
3289 vm_map_unlock_read(map
);
3290 if (entry
->use_pmap
) {
3291 vm_map_unlock_read(real_map
);
3292 real_map
= entry
->object
.sub_map
;
3294 map
= entry
->object
.sub_map
;
3301 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
3302 (entry
->object
.vm_object
!= NULL
) &&
3303 (entry
->object
.vm_object
== object
)) {
3307 * Set up a block mapped area
3309 pmap_map_block(caller_pmap
,
3310 (addr64_t
)(caller_pmap_addr
- ldelta
),
3311 (((vm_map_offset_t
) (entry
->object
.vm_object
->shadow_offset
)) +
3312 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12,
3313 ((ldelta
+ hdelta
) >> 12), prot
,
3314 (VM_WIMG_MASK
& (int)object
->wimg_bits
), 0);
3317 * Set up a block mapped area
3319 pmap_map_block(real_map
->pmap
,
3320 (addr64_t
)(vaddr
- ldelta
),
3321 (((vm_map_offset_t
)(entry
->object
.vm_object
->shadow_offset
)) +
3322 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12,
3323 ((ldelta
+ hdelta
) >> 12), prot
,
3324 (VM_WIMG_MASK
& (int)object
->wimg_bits
), 0);
3330 * Unlock everything, and return
3332 vm_map_verify_done(map
, &version
);
3333 if (real_map
!= map
)
3334 vm_map_unlock(real_map
);
3336 if (m
!= VM_PAGE_NULL
) {
3337 PAGE_WAKEUP_DONE(m
);
3339 vm_fault_cleanup(m
->object
, top_page
);
3341 vm_fault_cleanup(object
, top_page
);
3343 vm_object_deallocate(object
);
3349 thread_interrupt_level(interruptible_state
);
3351 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3352 (int)((uint64_t)vaddr
>> 32),
3364 * Wire down a range of virtual addresses in a map.
3369 vm_map_entry_t entry
,
3371 vm_map_offset_t pmap_addr
)
3374 register vm_map_offset_t va
;
3375 register vm_map_offset_t end_addr
= entry
->vme_end
;
3376 register kern_return_t rc
;
3378 assert(entry
->in_transition
);
3380 if ((entry
->object
.vm_object
!= NULL
) &&
3381 !entry
->is_sub_map
&&
3382 entry
->object
.vm_object
->phys_contiguous
) {
3383 return KERN_SUCCESS
;
3387 * Inform the physical mapping system that the
3388 * range of addresses may not fault, so that
3389 * page tables and such can be locked down as well.
3392 pmap_pageable(pmap
, pmap_addr
,
3393 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
3396 * We simulate a fault to get the page and enter it
3397 * in the physical map.
3400 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3401 if ((rc
= vm_fault_wire_fast(
3402 map
, va
, entry
, pmap
,
3403 pmap_addr
+ (va
- entry
->vme_start
)
3404 )) != KERN_SUCCESS
) {
3405 rc
= vm_fault(map
, va
, VM_PROT_NONE
, TRUE
,
3406 (pmap
== kernel_pmap
) ?
3407 THREAD_UNINT
: THREAD_ABORTSAFE
,
3408 pmap
, pmap_addr
+ (va
- entry
->vme_start
));
3409 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
3412 if (rc
!= KERN_SUCCESS
) {
3413 struct vm_map_entry tmp_entry
= *entry
;
3415 /* unwire wired pages */
3416 tmp_entry
.vme_end
= va
;
3417 vm_fault_unwire(map
,
3418 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
3423 return KERN_SUCCESS
;
3429 * Unwire a range of virtual addresses in a map.
3434 vm_map_entry_t entry
,
3435 boolean_t deallocate
,
3437 vm_map_offset_t pmap_addr
)
3439 register vm_map_offset_t va
;
3440 register vm_map_offset_t end_addr
= entry
->vme_end
;
3442 struct vm_object_fault_info fault_info
;
3444 object
= (entry
->is_sub_map
)
3445 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
3448 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
3449 * do anything since such memory is wired by default. So we don't have
3450 * anything to undo here.
3453 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
)
3456 fault_info
.interruptible
= THREAD_UNINT
;
3457 fault_info
.behavior
= entry
->behavior
;
3458 fault_info
.user_tag
= entry
->alias
;
3459 fault_info
.lo_offset
= entry
->offset
;
3460 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
3461 fault_info
.no_cache
= entry
->no_cache
;
3464 * Since the pages are wired down, we must be able to
3465 * get their mappings from the physical map system.
3468 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3470 if (object
== VM_OBJECT_NULL
) {
3472 pmap_change_wiring(pmap
,
3473 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3475 (void) vm_fault(map
, va
, VM_PROT_NONE
,
3476 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
3479 vm_page_t result_page
;
3481 vm_object_t result_object
;
3482 vm_fault_return_t result
;
3484 fault_info
.cluster_size
= end_addr
- va
;
3487 prot
= VM_PROT_NONE
;
3489 vm_object_lock(object
);
3490 vm_object_paging_begin(object
);
3492 "vm_fault_unwire -> vm_fault_page\n",
3494 result
= vm_fault_page(
3496 entry
->offset
+ (va
- entry
->vme_start
),
3498 &prot
, &result_page
, &top_page
,
3500 NULL
, map
->no_zero_fill
,
3501 FALSE
, &fault_info
);
3502 } while (result
== VM_FAULT_RETRY
);
3505 * If this was a mapping to a file on a device that has been forcibly
3506 * unmounted, then we won't get a page back from vm_fault_page(). Just
3507 * move on to the next one in case the remaining pages are mapped from
3508 * different objects. During a forced unmount, the object is terminated
3509 * so the alive flag will be false if this happens. A forced unmount will
3510 * will occur when an external disk is unplugged before the user does an
3511 * eject, so we don't want to panic in that situation.
3514 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
)
3517 if (result
!= VM_FAULT_SUCCESS
)
3518 panic("vm_fault_unwire: failure");
3520 result_object
= result_page
->object
;
3522 if ((pmap
) && (result_page
->phys_page
!= vm_page_guard_addr
)) {
3523 pmap_change_wiring(pmap
,
3524 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3527 assert(result_page
->phys_page
!=
3528 vm_page_fictitious_addr
);
3529 pmap_disconnect(result_page
->phys_page
);
3530 VM_PAGE_FREE(result_page
);
3532 vm_page_lockspin_queues();
3533 vm_page_unwire(result_page
);
3534 vm_page_unlock_queues();
3535 PAGE_WAKEUP_DONE(result_page
);
3537 vm_fault_cleanup(result_object
, top_page
);
3542 * Inform the physical mapping system that the range
3543 * of addresses may fault, so that page tables and
3544 * such may be unwired themselves.
3547 pmap_pageable(pmap
, pmap_addr
,
3548 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
3553 * vm_fault_wire_fast:
3555 * Handle common case of a wire down page fault at the given address.
3556 * If successful, the page is inserted into the associated physical map.
3557 * The map entry is passed in to avoid the overhead of a map lookup.
3559 * NOTE: the given address should be truncated to the
3560 * proper page address.
3562 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
3563 * a standard error specifying why the fault is fatal is returned.
3565 * The map in question must be referenced, and remains so.
3566 * Caller has a read lock on the map.
3568 * This is a stripped version of vm_fault() for wiring pages. Anything
3569 * other than the common case will return KERN_FAILURE, and the caller
3570 * is expected to call vm_fault().
3574 __unused vm_map_t map
,
3576 vm_map_entry_t entry
,
3578 vm_map_offset_t pmap_addr
)
3581 vm_object_offset_t offset
;
3582 register vm_page_t m
;
3584 thread_t thread
= current_thread();
3588 VM_STAT_INCR(faults
);
3590 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
)
3591 thread
->task
->faults
++;
3598 #define RELEASE_PAGE(m) { \
3599 PAGE_WAKEUP_DONE(m); \
3600 vm_page_lockspin_queues(); \
3601 vm_page_unwire(m); \
3602 vm_page_unlock_queues(); \
3606 #undef UNLOCK_THINGS
3607 #define UNLOCK_THINGS { \
3608 vm_object_paging_end(object); \
3609 vm_object_unlock(object); \
3612 #undef UNLOCK_AND_DEALLOCATE
3613 #define UNLOCK_AND_DEALLOCATE { \
3615 vm_object_deallocate(object); \
3618 * Give up and have caller do things the hard way.
3622 UNLOCK_AND_DEALLOCATE; \
3623 return(KERN_FAILURE); \
3628 * If this entry is not directly to a vm_object, bail out.
3630 if (entry
->is_sub_map
)
3631 return(KERN_FAILURE
);
3634 * Find the backing store object and offset into it.
3637 object
= entry
->object
.vm_object
;
3638 offset
= (va
- entry
->vme_start
) + entry
->offset
;
3639 prot
= entry
->protection
;
3642 * Make a reference to this object to prevent its
3643 * disposal while we are messing with it.
3646 vm_object_lock(object
);
3647 vm_object_reference_locked(object
);
3648 vm_object_paging_begin(object
);
3651 * INVARIANTS (through entire routine):
3653 * 1) At all times, we must either have the object
3654 * lock or a busy page in some object to prevent
3655 * some other thread from trying to bring in
3658 * 2) Once we have a busy page, we must remove it from
3659 * the pageout queues, so that the pageout daemon
3660 * will not grab it away.
3665 * Look for page in top-level object. If it's not there or
3666 * there's something going on, give up.
3667 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
3668 * decrypt the page before wiring it down.
3670 m
= vm_page_lookup(object
, offset
);
3671 if ((m
== VM_PAGE_NULL
) || (m
->busy
) || (m
->encrypted
) ||
3672 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
3676 ASSERT_PAGE_DECRYPTED(m
);
3678 if (m
->fictitious
&&
3679 m
->phys_page
== vm_page_guard_addr
) {
3681 * Guard pages are fictitious pages and are never
3682 * entered into a pmap, so let's say it's been wired...
3689 * Wire the page down now. All bail outs beyond this
3690 * point must unwire the page.
3693 vm_page_lockspin_queues();
3695 vm_page_unlock_queues();
3698 * Mark page busy for other threads.
3705 * Give up if the page is being written and there's a copy object
3707 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
3713 * Put this page into the physical map.
3715 type_of_fault
= DBG_CACHE_HIT_FAULT
;
3716 kr
= vm_fault_enter(m
,
3727 * Unlock everything, and return
3730 PAGE_WAKEUP_DONE(m
);
3731 UNLOCK_AND_DEALLOCATE
;
3738 * Routine: vm_fault_copy_cleanup
3740 * Release a page used by vm_fault_copy.
3744 vm_fault_copy_cleanup(
3748 vm_object_t object
= page
->object
;
3750 vm_object_lock(object
);
3751 PAGE_WAKEUP_DONE(page
);
3752 vm_page_lockspin_queues();
3753 if (!page
->active
&& !page
->inactive
&& !page
->throttled
)
3754 vm_page_activate(page
);
3755 vm_page_unlock_queues();
3756 vm_fault_cleanup(object
, top_page
);
3760 vm_fault_copy_dst_cleanup(
3765 if (page
!= VM_PAGE_NULL
) {
3766 object
= page
->object
;
3767 vm_object_lock(object
);
3768 vm_page_lockspin_queues();
3769 vm_page_unwire(page
);
3770 vm_page_unlock_queues();
3771 vm_object_paging_end(object
);
3772 vm_object_unlock(object
);
3777 * Routine: vm_fault_copy
3780 * Copy pages from one virtual memory object to another --
3781 * neither the source nor destination pages need be resident.
3783 * Before actually copying a page, the version associated with
3784 * the destination address map wil be verified.
3786 * In/out conditions:
3787 * The caller must hold a reference, but not a lock, to
3788 * each of the source and destination objects and to the
3792 * Returns KERN_SUCCESS if no errors were encountered in
3793 * reading or writing the data. Returns KERN_INTERRUPTED if
3794 * the operation was interrupted (only possible if the
3795 * "interruptible" argument is asserted). Other return values
3796 * indicate a permanent error in copying the data.
3798 * The actual amount of data copied will be returned in the
3799 * "copy_size" argument. In the event that the destination map
3800 * verification failed, this amount may be less than the amount
3805 vm_object_t src_object
,
3806 vm_object_offset_t src_offset
,
3807 vm_map_size_t
*copy_size
, /* INOUT */
3808 vm_object_t dst_object
,
3809 vm_object_offset_t dst_offset
,
3811 vm_map_version_t
*dst_version
,
3814 vm_page_t result_page
;
3817 vm_page_t src_top_page
;
3821 vm_page_t dst_top_page
;
3824 vm_map_size_t amount_left
;
3825 vm_object_t old_copy_object
;
3826 kern_return_t error
= 0;
3828 vm_map_size_t part_size
;
3829 struct vm_object_fault_info fault_info_src
;
3830 struct vm_object_fault_info fault_info_dst
;
3833 * In order not to confuse the clustered pageins, align
3834 * the different offsets on a page boundary.
3839 *copy_size -= amount_left; \
3843 amount_left
= *copy_size
;
3845 fault_info_src
.interruptible
= interruptible
;
3846 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
3847 fault_info_src
.user_tag
= 0;
3848 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
3849 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
3850 fault_info_src
.no_cache
= FALSE
;
3852 fault_info_dst
.interruptible
= interruptible
;
3853 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
3854 fault_info_dst
.user_tag
= 0;
3855 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
3856 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
3857 fault_info_dst
.no_cache
= FALSE
;
3859 do { /* while (amount_left > 0) */
3861 * There may be a deadlock if both source and destination
3862 * pages are the same. To avoid this deadlock, the copy must
3863 * start by getting the destination page in order to apply
3864 * COW semantics if any.
3867 RetryDestinationFault
: ;
3869 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
3871 vm_object_lock(dst_object
);
3872 vm_object_paging_begin(dst_object
);
3874 fault_info_dst
.cluster_size
= amount_left
;
3876 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
3877 switch (vm_fault_page(dst_object
,
3878 vm_object_trunc_page(dst_offset
),
3879 VM_PROT_WRITE
|VM_PROT_READ
,
3881 &dst_prot
, &dst_page
, &dst_top_page
,
3884 dst_map
->no_zero_fill
,
3885 FALSE
, &fault_info_dst
)) {
3886 case VM_FAULT_SUCCESS
:
3888 case VM_FAULT_RETRY
:
3889 goto RetryDestinationFault
;
3890 case VM_FAULT_MEMORY_SHORTAGE
:
3891 if (vm_page_wait(interruptible
))
3892 goto RetryDestinationFault
;
3894 case VM_FAULT_INTERRUPTED
:
3895 RETURN(MACH_SEND_INTERRUPTED
);
3896 case VM_FAULT_MEMORY_ERROR
:
3900 return(KERN_MEMORY_ERROR
);
3902 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
3904 old_copy_object
= dst_page
->object
->copy
;
3907 * There exists the possiblity that the source and
3908 * destination page are the same. But we can't
3909 * easily determine that now. If they are the
3910 * same, the call to vm_fault_page() for the
3911 * destination page will deadlock. To prevent this we
3912 * wire the page so we can drop busy without having
3913 * the page daemon steal the page. We clean up the
3914 * top page but keep the paging reference on the object
3915 * holding the dest page so it doesn't go away.
3918 vm_page_lockspin_queues();
3919 vm_page_wire(dst_page
);
3920 vm_page_unlock_queues();
3921 PAGE_WAKEUP_DONE(dst_page
);
3922 vm_object_unlock(dst_page
->object
);
3924 if (dst_top_page
!= VM_PAGE_NULL
) {
3925 vm_object_lock(dst_object
);
3926 VM_PAGE_FREE(dst_top_page
);
3927 vm_object_paging_end(dst_object
);
3928 vm_object_unlock(dst_object
);
3933 if (src_object
== VM_OBJECT_NULL
) {
3935 * No source object. We will just
3936 * zero-fill the page in dst_object.
3938 src_page
= VM_PAGE_NULL
;
3939 result_page
= VM_PAGE_NULL
;
3941 vm_object_lock(src_object
);
3942 src_page
= vm_page_lookup(src_object
,
3943 vm_object_trunc_page(src_offset
));
3944 if (src_page
== dst_page
) {
3945 src_prot
= dst_prot
;
3946 result_page
= VM_PAGE_NULL
;
3948 src_prot
= VM_PROT_READ
;
3949 vm_object_paging_begin(src_object
);
3951 fault_info_src
.cluster_size
= amount_left
;
3954 "vm_fault_copy(2) -> vm_fault_page\n",
3956 switch (vm_fault_page(
3958 vm_object_trunc_page(src_offset
),
3959 VM_PROT_READ
, FALSE
,
3961 &result_page
, &src_top_page
,
3962 (int *)0, &error
, FALSE
,
3963 FALSE
, &fault_info_src
)) {
3965 case VM_FAULT_SUCCESS
:
3967 case VM_FAULT_RETRY
:
3968 goto RetrySourceFault
;
3969 case VM_FAULT_MEMORY_SHORTAGE
:
3970 if (vm_page_wait(interruptible
))
3971 goto RetrySourceFault
;
3973 case VM_FAULT_INTERRUPTED
:
3974 vm_fault_copy_dst_cleanup(dst_page
);
3975 RETURN(MACH_SEND_INTERRUPTED
);
3976 case VM_FAULT_MEMORY_ERROR
:
3977 vm_fault_copy_dst_cleanup(dst_page
);
3981 return(KERN_MEMORY_ERROR
);
3985 assert((src_top_page
== VM_PAGE_NULL
) ==
3986 (result_page
->object
== src_object
));
3988 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
3989 vm_object_unlock(result_page
->object
);
3992 if (!vm_map_verify(dst_map
, dst_version
)) {
3993 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3994 vm_fault_copy_cleanup(result_page
, src_top_page
);
3995 vm_fault_copy_dst_cleanup(dst_page
);
3999 vm_object_lock(dst_page
->object
);
4001 if (dst_page
->object
->copy
!= old_copy_object
) {
4002 vm_object_unlock(dst_page
->object
);
4003 vm_map_verify_done(dst_map
, dst_version
);
4004 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4005 vm_fault_copy_cleanup(result_page
, src_top_page
);
4006 vm_fault_copy_dst_cleanup(dst_page
);
4009 vm_object_unlock(dst_page
->object
);
4012 * Copy the page, and note that it is dirty
4016 if (!page_aligned(src_offset
) ||
4017 !page_aligned(dst_offset
) ||
4018 !page_aligned(amount_left
)) {
4020 vm_object_offset_t src_po
,
4023 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
4024 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
4026 if (dst_po
> src_po
) {
4027 part_size
= PAGE_SIZE
- dst_po
;
4029 part_size
= PAGE_SIZE
- src_po
;
4031 if (part_size
> (amount_left
)){
4032 part_size
= amount_left
;
4035 if (result_page
== VM_PAGE_NULL
) {
4036 vm_page_part_zero_fill(dst_page
,
4039 vm_page_part_copy(result_page
, src_po
,
4040 dst_page
, dst_po
, part_size
);
4041 if(!dst_page
->dirty
){
4042 vm_object_lock(dst_object
);
4043 dst_page
->dirty
= TRUE
;
4044 vm_object_unlock(dst_page
->object
);
4049 part_size
= PAGE_SIZE
;
4051 if (result_page
== VM_PAGE_NULL
)
4052 vm_page_zero_fill(dst_page
);
4054 vm_page_copy(result_page
, dst_page
);
4055 if(!dst_page
->dirty
){
4056 vm_object_lock(dst_object
);
4057 dst_page
->dirty
= TRUE
;
4058 vm_object_unlock(dst_page
->object
);
4065 * Unlock everything, and return
4068 vm_map_verify_done(dst_map
, dst_version
);
4070 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4071 vm_fault_copy_cleanup(result_page
, src_top_page
);
4072 vm_fault_copy_dst_cleanup(dst_page
);
4074 amount_left
-= part_size
;
4075 src_offset
+= part_size
;
4076 dst_offset
+= part_size
;
4077 } while (amount_left
> 0);
4079 RETURN(KERN_SUCCESS
);
4085 #if VM_FAULT_CLASSIFY
4087 * Temporary statistics gathering support.
4091 * Statistics arrays:
4093 #define VM_FAULT_TYPES_MAX 5
4094 #define VM_FAULT_LEVEL_MAX 8
4096 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
4098 #define VM_FAULT_TYPE_ZERO_FILL 0
4099 #define VM_FAULT_TYPE_MAP_IN 1
4100 #define VM_FAULT_TYPE_PAGER 2
4101 #define VM_FAULT_TYPE_COPY 3
4102 #define VM_FAULT_TYPE_OTHER 4
4106 vm_fault_classify(vm_object_t object
,
4107 vm_object_offset_t offset
,
4108 vm_prot_t fault_type
)
4110 int type
, level
= 0;
4114 m
= vm_page_lookup(object
, offset
);
4115 if (m
!= VM_PAGE_NULL
) {
4116 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
) {
4117 type
= VM_FAULT_TYPE_OTHER
;
4120 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
4121 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
4122 type
= VM_FAULT_TYPE_MAP_IN
;
4125 type
= VM_FAULT_TYPE_COPY
;
4129 if (object
->pager_created
) {
4130 type
= VM_FAULT_TYPE_PAGER
;
4133 if (object
->shadow
== VM_OBJECT_NULL
) {
4134 type
= VM_FAULT_TYPE_ZERO_FILL
;
4138 offset
+= object
->shadow_offset
;
4139 object
= object
->shadow
;
4145 if (level
> VM_FAULT_LEVEL_MAX
)
4146 level
= VM_FAULT_LEVEL_MAX
;
4148 vm_fault_stats
[type
][level
] += 1;
4153 /* cleanup routine to call from debugger */
4156 vm_fault_classify_init(void)
4160 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
4161 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
4162 vm_fault_stats
[type
][level
] = 0;
4168 #endif /* VM_FAULT_CLASSIFY */
4171 extern int cs_validation
;
4174 vm_page_validate_cs_mapped(
4179 vm_object_offset_t offset
;
4181 memory_object_t pager
;
4183 boolean_t validated
, tainted
;
4186 vm_object_lock_assert_exclusive(page
->object
);
4188 if (!cs_validation
) {
4192 if (page
->wpmapped
&& !page
->cs_tainted
) {
4194 * This page was mapped for "write" access sometime in the
4195 * past and could still be modifiable in the future.
4196 * Consider it tainted.
4197 * [ If the page was already found to be "tainted", no
4198 * need to re-validate. ]
4200 page
->cs_validated
= TRUE
;
4201 page
->cs_tainted
= TRUE
;
4203 printf("CODESIGNING: vm_page_validate_cs: "
4204 "page %p obj %p off 0x%llx "
4206 page
, page
->object
, page
->offset
);
4208 vm_cs_validated_dirtied
++;
4211 if (page
->cs_validated
) {
4217 object
= page
->object
;
4218 assert(object
->code_signed
);
4219 offset
= page
->offset
;
4221 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
4223 * The object is terminating and we don't have its pager
4224 * so we can't validate the data...
4229 * Since we get here to validate a page that was brought in by
4230 * the pager, we know that this pager is all setup and ready
4233 assert(!object
->internal
);
4234 assert(object
->pager
!= NULL
);
4235 assert(object
->pager_ready
);
4237 pager
= object
->pager
;
4239 kr
= vnode_pager_get_object_cs_blobs(pager
, &blobs
);
4240 if (kr
!= KERN_SUCCESS
) {
4244 /* verify the SHA1 hash for this page */
4245 validated
= cs_validate_page(blobs
,
4246 offset
+ object
->paging_offset
,
4247 (const void *)kaddr
,
4250 page
->cs_validated
= validated
;
4252 page
->cs_tainted
= tainted
;
4257 vm_page_validate_cs(
4261 vm_object_offset_t offset
;
4262 vm_map_offset_t koffset
;
4263 vm_map_size_t ksize
;
4266 boolean_t busy_page
;
4268 vm_object_lock_assert_held(page
->object
);
4270 if (!cs_validation
) {
4274 if (page
->wpmapped
&& !page
->cs_tainted
) {
4275 vm_object_lock_assert_exclusive(page
->object
);
4278 * This page was mapped for "write" access sometime in the
4279 * past and could still be modifiable in the future.
4280 * Consider it tainted.
4281 * [ If the page was already found to be "tainted", no
4282 * need to re-validate. ]
4284 page
->cs_validated
= TRUE
;
4285 page
->cs_tainted
= TRUE
;
4287 printf("CODESIGNING: vm_page_validate_cs: "
4288 "page %p obj %p off 0x%llx "
4290 page
, page
->object
, page
->offset
);
4292 vm_cs_validated_dirtied
++;
4295 if (page
->cs_validated
) {
4299 vm_object_lock_assert_exclusive(page
->object
);
4301 object
= page
->object
;
4302 assert(object
->code_signed
);
4303 offset
= page
->offset
;
4305 busy_page
= page
->busy
;
4307 /* keep page busy while we map (and unlock) the VM object */
4312 * Take a paging reference on the VM object
4313 * to protect it from collapse or bypass,
4314 * and keep it from disappearing too.
4316 vm_object_paging_begin(object
);
4318 /* map the page in the kernel address space */
4320 ksize
= PAGE_SIZE_64
;
4321 kr
= vm_paging_map_object(&koffset
,
4327 FALSE
); /* can't unlock object ! */
4328 if (kr
!= KERN_SUCCESS
) {
4329 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr
);
4331 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
4333 /* validate the mapped page */
4334 vm_page_validate_cs_mapped(page
, (const void *) kaddr
);
4337 assert(object
== page
->object
);
4338 vm_object_lock_assert_exclusive(object
);
4341 PAGE_WAKEUP_DONE(page
);
4344 /* unmap the map from the kernel address space */
4345 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
4350 vm_object_paging_end(object
);