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 * Routine: vm_fault_init
155 * Initialize our private data structures.
163 * Routine: vm_fault_cleanup
165 * Clean up the result of vm_fault_page.
167 * The paging reference for "object" is released.
168 * "object" is unlocked.
169 * If "top_page" is not null, "top_page" is
170 * freed and the paging reference for the object
171 * containing it is released.
174 * "object" must be locked.
178 register vm_object_t object
,
179 register vm_page_t top_page
)
181 vm_object_paging_end(object
);
182 vm_object_unlock(object
);
184 if (top_page
!= VM_PAGE_NULL
) {
185 object
= top_page
->object
;
187 vm_object_lock(object
);
188 VM_PAGE_FREE(top_page
);
189 vm_object_paging_end(object
);
190 vm_object_unlock(object
);
194 #if MACH_CLUSTER_STATS
195 #define MAXCLUSTERPAGES 16
197 unsigned long pages_in_cluster
;
198 unsigned long pages_at_higher_offsets
;
199 unsigned long pages_at_lower_offsets
;
200 } cluster_stats_in
[MAXCLUSTERPAGES
];
201 #define CLUSTER_STAT(clause) clause
202 #define CLUSTER_STAT_HIGHER(x) \
203 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
204 #define CLUSTER_STAT_LOWER(x) \
205 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
206 #define CLUSTER_STAT_CLUSTER(x) \
207 ((cluster_stats_in[(x)].pages_in_cluster)++)
208 #else /* MACH_CLUSTER_STATS */
209 #define CLUSTER_STAT(clause)
210 #endif /* MACH_CLUSTER_STATS */
212 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
215 boolean_t vm_page_deactivate_behind
= TRUE
;
217 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
219 int vm_default_ahead
= 0;
220 int vm_default_behind
= MAX_UPL_TRANSFER
;
222 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
225 * vm_page_is_sequential
227 * Determine if sequential access is in progress
228 * in accordance with the behavior specified.
229 * Update state to indicate current access pattern.
231 * object must have at least the shared lock held
235 vm_fault_is_sequential(
237 vm_object_offset_t offset
,
238 vm_behavior_t behavior
)
240 vm_object_offset_t last_alloc
;
244 last_alloc
= object
->last_alloc
;
245 sequential
= object
->sequential
;
246 orig_sequential
= sequential
;
249 case VM_BEHAVIOR_RANDOM
:
251 * reset indicator of sequential behavior
256 case VM_BEHAVIOR_SEQUENTIAL
:
257 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
259 * advance indicator of sequential behavior
261 if (sequential
< MAX_SEQUENTIAL_RUN
)
262 sequential
+= PAGE_SIZE
;
265 * reset indicator of sequential behavior
271 case VM_BEHAVIOR_RSEQNTL
:
272 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
274 * advance indicator of sequential behavior
276 if (sequential
> -MAX_SEQUENTIAL_RUN
)
277 sequential
-= PAGE_SIZE
;
280 * reset indicator of sequential behavior
286 case VM_BEHAVIOR_DEFAULT
:
288 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
290 * advance indicator of sequential behavior
294 if (sequential
< MAX_SEQUENTIAL_RUN
)
295 sequential
+= PAGE_SIZE
;
297 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
299 * advance indicator of sequential behavior
303 if (sequential
> -MAX_SEQUENTIAL_RUN
)
304 sequential
-= PAGE_SIZE
;
307 * reset indicator of sequential behavior
313 if (sequential
!= orig_sequential
) {
314 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
316 * if someone else has already updated object->sequential
317 * don't bother trying to update it or object->last_alloc
323 * I'd like to do this with a OSCompareAndSwap64, but that
324 * doesn't exist for PPC... however, it shouldn't matter
325 * that much... last_alloc is maintained so that we can determine
326 * if a sequential access pattern is taking place... if only
327 * one thread is banging on this object, no problem with the unprotected
328 * update... if 2 or more threads are banging away, we run the risk of
329 * someone seeing a mangled update... however, in the face of multiple
330 * accesses, no sequential access pattern can develop anyway, so we
331 * haven't lost any real info.
333 object
->last_alloc
= offset
;
338 * vm_page_deactivate_behind
340 * Determine if sequential access is in progress
341 * in accordance with the behavior specified. If
342 * so, compute a potential page to deactivate and
345 * object must be locked.
347 * return TRUE if we actually deactivate a page
351 vm_fault_deactivate_behind(
353 vm_object_offset_t offset
,
354 vm_behavior_t behavior
)
358 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
361 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
364 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
366 * Do not deactivate pages from the kernel object: they
367 * are not intended to become pageable.
368 * or we've disabled the deactivate behind mechanism
372 if ((sequential_run
= object
->sequential
)) {
373 if (sequential_run
< 0) {
374 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
375 sequential_run
= 0 - sequential_run
;
377 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
381 case VM_BEHAVIOR_RANDOM
:
383 case VM_BEHAVIOR_SEQUENTIAL
:
384 if (sequential_run
>= (int)PAGE_SIZE
)
385 m
= vm_page_lookup(object
, offset
- PAGE_SIZE_64
);
387 case VM_BEHAVIOR_RSEQNTL
:
388 if (sequential_run
>= (int)PAGE_SIZE
)
389 m
= vm_page_lookup(object
, offset
+ PAGE_SIZE_64
);
391 case VM_BEHAVIOR_DEFAULT
:
393 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
396 * determine if the run of sequential accesss has been
397 * long enough on an object with default access behavior
398 * to consider it for deactivation
400 if ((uint64_t)sequential_run
>= behind
) {
401 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
402 if (offset
>= behind
)
403 m
= vm_page_lookup(object
, offset
- behind
);
405 if (offset
< -behind
)
406 m
= vm_page_lookup(object
, offset
+ behind
);
413 if (!m
->busy
&& !m
->no_cache
&& !m
->throttled
&& !m
->fictitious
&& !m
->absent
) {
414 pmap_clear_reference(m
->phys_page
);
415 m
->deactivated
= TRUE
;
417 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
427 * check for various conditions that would
428 * prevent us from creating a ZF page...
429 * cleanup is based on being called from vm_fault_page
431 * object must be locked
432 * object == m->object
434 static vm_fault_return_t
435 vm_fault_check(vm_object_t object
, vm_page_t m
, vm_page_t first_m
, boolean_t interruptible_state
)
437 if (object
->shadow_severed
) {
439 * the shadow chain was severed
440 * just have to return an error at this point
442 if (m
!= VM_PAGE_NULL
)
444 vm_fault_cleanup(object
, first_m
);
446 thread_interrupt_level(interruptible_state
);
448 return (VM_FAULT_MEMORY_ERROR
);
450 if (vm_backing_store_low
) {
452 * are we protecting the system from
453 * backing store exhaustion. If so
454 * sleep unless we are privileged.
456 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
458 if (m
!= VM_PAGE_NULL
)
460 vm_fault_cleanup(object
, first_m
);
462 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
464 thread_block(THREAD_CONTINUE_NULL
);
465 thread_interrupt_level(interruptible_state
);
467 return (VM_FAULT_RETRY
);
470 if (VM_PAGE_ZFILL_THROTTLED()) {
472 * we're throttling zero-fills...
473 * treat this as if we couldn't grab a page
475 if (m
!= VM_PAGE_NULL
)
477 vm_fault_cleanup(object
, first_m
);
479 thread_interrupt_level(interruptible_state
);
481 return (VM_FAULT_MEMORY_SHORTAGE
);
483 return (VM_FAULT_SUCCESS
);
488 * do the work to zero fill a page and
489 * inject it into the correct paging queue
491 * m->object must be locked
492 * page queue lock must NOT be held
495 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
497 int my_fault
= DBG_ZERO_FILL_FAULT
;
500 * This is is a zero-fill page fault...
502 * Checking the page lock is a waste of
503 * time; this page was absent, so
504 * it can't be page locked by a pager.
506 * we also consider it undefined
507 * with respect to instruction
508 * execution. i.e. it is the responsibility
509 * of higher layers to call for an instruction
510 * sync after changing the contents and before
511 * sending a program into this area. We
512 * choose this approach for performance
516 m
->cs_validated
= FALSE
;
517 m
->cs_tainted
= FALSE
;
519 if (no_zero_fill
== TRUE
)
520 my_fault
= DBG_NZF_PAGE_FAULT
;
522 vm_page_zero_fill(m
);
524 VM_STAT_INCR(zero_fill_count
);
525 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
528 assert(m
->object
!= kernel_object
);
529 //assert(m->pageq.next == NULL && m->pageq.prev == NULL);
531 if (!IP_VALID(memory_manager_default
) &&
532 (m
->object
->purgable
== VM_PURGABLE_DENY
||
533 m
->object
->purgable
== VM_PURGABLE_NONVOLATILE
)) {
534 vm_page_lock_queues();
536 queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
538 vm_page_throttled_count
++;
540 vm_page_unlock_queues();
542 if (m
->object
->size
> VM_ZF_OBJECT_SIZE_THRESHOLD
) {
544 OSAddAtomic(1, (SInt32
*)&vm_zf_count
);
552 * Routine: vm_fault_page
554 * Find the resident page for the virtual memory
555 * specified by the given virtual memory object
557 * Additional arguments:
558 * The required permissions for the page is given
559 * in "fault_type". Desired permissions are included
561 * fault_info is passed along to determine pagein cluster
562 * limits... it contains the expected reference pattern,
563 * cluster size if available, etc...
565 * If the desired page is known to be resident (for
566 * example, because it was previously wired down), asserting
567 * the "unwiring" parameter will speed the search.
569 * If the operation can be interrupted (by thread_abort
570 * or thread_terminate), then the "interruptible"
571 * parameter should be asserted.
574 * The page containing the proper data is returned
578 * The source object must be locked and referenced,
579 * and must donate one paging reference. The reference
580 * is not affected. The paging reference and lock are
583 * If the call succeeds, the object in which "result_page"
584 * resides is left locked and holding a paging reference.
585 * If this is not the original object, a busy page in the
586 * original object is returned in "top_page", to prevent other
587 * callers from pursuing this same data, along with a paging
588 * reference for the original object. The "top_page" should
589 * be destroyed when this guarantee is no longer required.
590 * The "result_page" is also left busy. It is not removed
591 * from the pageout queues.
597 vm_object_t first_object
, /* Object to begin search */
598 vm_object_offset_t first_offset
, /* Offset into object */
599 vm_prot_t fault_type
, /* What access is requested */
600 boolean_t must_be_resident
,/* Must page be resident? */
601 /* Modifies in place: */
602 vm_prot_t
*protection
, /* Protection for mapping */
604 vm_page_t
*result_page
, /* Page found, if successful */
605 vm_page_t
*top_page
, /* Page in top object, if
606 * not result_page. */
607 int *type_of_fault
, /* if non-null, fill in with type of fault
608 * COW, zero-fill, etc... returned in trace point */
609 /* More arguments: */
610 kern_return_t
*error_code
, /* code if page is in error */
611 boolean_t no_zero_fill
, /* don't zero fill absent pages */
613 boolean_t data_supply
, /* treat as data_supply if
614 * it is a write fault and a full
615 * page is provided */
617 __unused boolean_t data_supply
,
619 vm_object_fault_info_t fault_info
)
623 vm_object_offset_t offset
;
625 vm_object_t next_object
;
626 vm_object_t copy_object
;
627 boolean_t look_for_page
;
628 vm_prot_t access_required
= fault_type
;
629 vm_prot_t wants_copy_flag
;
630 CLUSTER_STAT(int pages_at_higher_offsets
;)
631 CLUSTER_STAT(int pages_at_lower_offsets
;)
632 kern_return_t wait_result
;
633 boolean_t interruptible_state
;
634 vm_fault_return_t error
;
636 uint32_t try_failed_count
;
637 int interruptible
; /* how may fault be interrupted? */
638 memory_object_t pager
;
641 * MACH page map - an optional optimization where a bit map is maintained
642 * by the VM subsystem for internal objects to indicate which pages of
643 * the object currently reside on backing store. This existence map
644 * duplicates information maintained by the vnode pager. It is
645 * created at the time of the first pageout against the object, i.e.
646 * at the same time pager for the object is created. The optimization
647 * is designed to eliminate pager interaction overhead, if it is
648 * 'known' that the page does not exist on backing store.
650 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
651 * either marked as paged out in the existence map for the object or no
652 * existence map exists for the object. MUST_ASK_PAGER() is one of the
653 * criteria in the decision to invoke the pager. It is also used as one
654 * of the criteria to terminate the scan for adjacent pages in a clustered
655 * pagein operation. Note that MUST_ASK_PAGER() always evaluates to TRUE for
656 * permanent objects. Note also that if the pager for an internal object
657 * has not been created, the pager is not invoked regardless of the value
658 * of MUST_ASK_PAGER() and that clustered pagein scans are only done on an object
659 * for which a pager has been created.
661 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
662 * is marked as paged out in the existence map for the object. PAGED_OUT()
663 * PAGED_OUT() is used to determine if a page has already been pushed
664 * into a copy object in order to avoid a redundant page out operation.
667 #define MUST_ASK_PAGER(o, f) (vm_external_state_get((o)->existence_map, (f)) \
668 != VM_EXTERNAL_STATE_ABSENT)
669 #define PAGED_OUT(o, f) (vm_external_state_get((o)->existence_map, (f)) \
670 == VM_EXTERNAL_STATE_EXISTS)
672 #define MUST_ASK_PAGER(o, f) (TRUE)
673 #define PAGED_OUT(o, f) (FALSE)
679 #define PREPARE_RELEASE_PAGE(m) \
681 vm_page_lock_queues(); \
684 #define DO_RELEASE_PAGE(m) \
686 PAGE_WAKEUP_DONE(m); \
687 if (!m->active && !m->inactive && !m->throttled)\
688 vm_page_activate(m); \
689 vm_page_unlock_queues(); \
692 #define RELEASE_PAGE(m) \
694 PREPARE_RELEASE_PAGE(m); \
695 DO_RELEASE_PAGE(m); \
699 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
705 * If there are watchpoints set, then
706 * we don't want to give away write permission
707 * on a read fault. Make the task write fault,
708 * so that the watchpoint code notices the access.
710 if (db_watchpoint_list
) {
712 * If we aren't asking for write permission,
713 * then don't give it away. We're using write
714 * faults to set the dirty bit.
716 if (!(fault_type
& VM_PROT_WRITE
))
717 *protection
&= ~VM_PROT_WRITE
;
719 #endif /* MACH_KDB */
721 interruptible
= fault_info
->interruptible
;
722 interruptible_state
= thread_interrupt_level(interruptible
);
725 * INVARIANTS (through entire routine):
727 * 1) At all times, we must either have the object
728 * lock or a busy page in some object to prevent
729 * some other thread from trying to bring in
732 * Note that we cannot hold any locks during the
733 * pager access or when waiting for memory, so
734 * we use a busy page then.
736 * 2) To prevent another thread from racing us down the
737 * shadow chain and entering a new page in the top
738 * object before we do, we must keep a busy page in
739 * the top object while following the shadow chain.
741 * 3) We must increment paging_in_progress on any object
742 * for which we have a busy page before dropping
745 * 4) We leave busy pages on the pageout queues.
746 * If the pageout daemon comes across a busy page,
747 * it will remove the page from the pageout queues.
750 object
= first_object
;
751 offset
= first_offset
;
752 first_m
= VM_PAGE_NULL
;
753 access_required
= fault_type
;
757 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
758 (integer_t
)object
, offset
, fault_type
, *protection
, 0);
761 * default type of fault
763 my_fault
= DBG_CACHE_HIT_FAULT
;
767 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
769 if (!object
->alive
) {
771 * object is no longer valid
772 * clean up and return error
774 vm_fault_cleanup(object
, first_m
);
775 thread_interrupt_level(interruptible_state
);
777 return (VM_FAULT_MEMORY_ERROR
);
781 * See whether the page at 'offset' is resident
783 m
= vm_page_lookup(object
, offset
);
785 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
787 if (m
!= VM_PAGE_NULL
) {
791 * The page is being brought in,
792 * wait for it and then retry.
794 * A possible optimization: if the page
795 * is known to be resident, we can ignore
796 * pages that are absent (regardless of
797 * whether they're busy).
800 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
802 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
804 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
805 (integer_t
)object
, offset
,
807 counter(c_vm_fault_page_block_busy_kernel
++);
809 if (wait_result
!= THREAD_AWAKENED
) {
810 vm_fault_cleanup(object
, first_m
);
811 thread_interrupt_level(interruptible_state
);
813 if (wait_result
== THREAD_RESTART
)
814 return (VM_FAULT_RETRY
);
816 return (VM_FAULT_INTERRUPTED
);
821 if (m
->phys_page
== vm_page_guard_addr
) {
823 * Guard page: off limits !
825 if (fault_type
== VM_PROT_NONE
) {
827 * The fault is not requesting any
828 * access to the guard page, so it must
829 * be just to wire or unwire it.
830 * Let's pretend it succeeded...
834 assert(first_m
== VM_PAGE_NULL
);
837 *type_of_fault
= DBG_GUARD_FAULT
;
838 return VM_FAULT_SUCCESS
;
841 * The fault requests access to the
842 * guard page: let's deny that !
844 vm_fault_cleanup(object
, first_m
);
845 thread_interrupt_level(interruptible_state
);
846 return VM_FAULT_MEMORY_ERROR
;
852 * The page is in error, give up now.
855 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
858 *error_code
= KERN_MEMORY_ERROR
;
861 vm_fault_cleanup(object
, first_m
);
862 thread_interrupt_level(interruptible_state
);
864 return (VM_FAULT_MEMORY_ERROR
);
868 * The pager wants us to restart
869 * at the top of the chain,
870 * typically because it has moved the
871 * page to another pager, then do so.
874 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
878 vm_fault_cleanup(object
, first_m
);
879 thread_interrupt_level(interruptible_state
);
881 return (VM_FAULT_RETRY
);
885 * The page isn't busy, but is absent,
886 * therefore it's deemed "unavailable".
888 * Remove the non-existent page (unless it's
889 * in the top object) and move on down to the
890 * next object (if there is one).
893 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
895 next_object
= object
->shadow
;
897 if (next_object
== VM_OBJECT_NULL
) {
899 * Absent page at bottom of shadow
900 * chain; zero fill the page we left
901 * busy in the first object, and free
904 assert(!must_be_resident
);
907 * check for any conditions that prevent
908 * us from creating a new zero-fill page
909 * vm_fault_check will do all of the
910 * fault cleanup in the case of an error condition
911 * including resetting the thread_interrupt_level
913 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
915 if (error
!= VM_FAULT_SUCCESS
)
919 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
920 (integer_t
)object
, offset
,
922 (integer_t
)first_object
, 0);
924 if (object
!= first_object
) {
926 * free the absent page we just found
931 * drop reference and lock on current object
933 vm_object_paging_end(object
);
934 vm_object_unlock(object
);
937 * grab the original page we
938 * 'soldered' in place and
939 * retake lock on 'first_object'
942 first_m
= VM_PAGE_NULL
;
944 object
= first_object
;
945 offset
= first_offset
;
947 vm_object_lock(object
);
950 * we're going to use the absent page we just found
951 * so convert it to a 'busy' page
957 * zero-fill the page and put it on
958 * the correct paging queue
960 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
964 if (must_be_resident
)
965 vm_object_paging_end(object
);
966 else if (object
!= first_object
) {
967 vm_object_paging_end(object
);
974 vm_page_lockspin_queues();
975 VM_PAGE_QUEUES_REMOVE(m
);
976 vm_page_unlock_queues();
979 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
980 (integer_t
)object
, offset
,
981 (integer_t
)next_object
,
982 offset
+object
->shadow_offset
,0);
984 offset
+= object
->shadow_offset
;
985 fault_info
->lo_offset
+= object
->shadow_offset
;
986 fault_info
->hi_offset
+= object
->shadow_offset
;
987 access_required
= VM_PROT_READ
;
989 vm_object_lock(next_object
);
990 vm_object_unlock(object
);
991 object
= next_object
;
992 vm_object_paging_begin(object
);
995 * reset to default type of fault
997 my_fault
= DBG_CACHE_HIT_FAULT
;
1003 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1004 && (fault_type
& VM_PROT_WRITE
)) {
1006 * This is a copy-on-write fault that will
1007 * cause us to revoke access to this page, but
1008 * this page is in the process of being cleaned
1009 * in a clustered pageout. We must wait until
1010 * the cleaning operation completes before
1011 * revoking access to the original page,
1012 * otherwise we might attempt to remove a
1016 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1019 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1020 (integer_t
)object
, offset
,
1021 (integer_t
)m
, 0, 0);
1023 * take an extra ref so that object won't die
1025 vm_object_reference_locked(object
);
1027 vm_fault_cleanup(object
, first_m
);
1029 counter(c_vm_fault_page_block_backoff_kernel
++);
1030 vm_object_lock(object
);
1031 assert(object
->ref_count
> 0);
1033 m
= vm_page_lookup(object
, offset
);
1035 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
1036 PAGE_ASSERT_WAIT(m
, interruptible
);
1038 vm_object_unlock(object
);
1039 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1040 vm_object_deallocate(object
);
1044 vm_object_unlock(object
);
1046 vm_object_deallocate(object
);
1047 thread_interrupt_level(interruptible_state
);
1049 return (VM_FAULT_RETRY
);
1052 if (type_of_fault
== NULL
&& m
->speculative
) {
1054 * If we were passed a non-NULL pointer for
1055 * "type_of_fault", than we came from
1056 * vm_fault... we'll let it deal with
1057 * this condition, since it
1058 * needs to see m->speculative to correctly
1059 * account the pageins, otherwise...
1060 * take it off the speculative queue, we'll
1061 * let the caller of vm_fault_page deal
1062 * with getting it onto the correct queue
1064 vm_page_lockspin_queues();
1065 VM_PAGE_QUEUES_REMOVE(m
);
1066 vm_page_unlock_queues();
1072 * the user needs access to a page that we
1073 * encrypted before paging it out.
1074 * Decrypt the page now.
1075 * Keep it busy to prevent anyone from
1076 * accessing it during the decryption.
1079 vm_page_decrypt(m
, 0);
1080 assert(object
== m
->object
);
1082 PAGE_WAKEUP_DONE(m
);
1085 * Retry from the top, in case
1086 * something changed while we were
1091 ASSERT_PAGE_DECRYPTED(m
);
1093 if (m
->object
->code_signed
) {
1096 * We just paged in a page from a signed
1097 * memory object but we don't need to
1098 * validate it now. We'll validate it if
1099 * when it gets mapped into a user address
1100 * space for the first time or when the page
1101 * gets copied to another object as a result
1102 * of a copy-on-write.
1107 * We mark the page busy and leave it on
1108 * the pageout queues. If the pageout
1109 * deamon comes across it, then it will
1110 * remove the page from the queue, but not the object
1113 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1116 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1117 (integer_t
)object
, offset
, (integer_t
)m
, 0, 0);
1127 * we get here when there is no page present in the object at
1128 * the offset we're interested in... we'll allocate a page
1129 * at this point if the pager associated with
1130 * this object can provide the data or we're the top object...
1131 * object is locked; m == NULL
1133 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
) == TRUE
) && !data_supply
);
1136 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1138 if ((look_for_page
|| (object
== first_object
)) && !must_be_resident
&& !object
->phys_contiguous
) {
1140 * Allocate a new page for this object/offset pair
1144 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1146 if (m
== VM_PAGE_NULL
) {
1148 vm_fault_cleanup(object
, first_m
);
1149 thread_interrupt_level(interruptible_state
);
1151 return (VM_FAULT_MEMORY_SHORTAGE
);
1153 vm_page_insert(m
, object
, offset
);
1155 if (look_for_page
&& !must_be_resident
) {
1159 * If the memory manager is not ready, we
1160 * cannot make requests.
1162 if (!object
->pager_ready
) {
1164 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1166 if (m
!= VM_PAGE_NULL
)
1170 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1171 (integer_t
)object
, offset
, 0, 0, 0);
1174 * take an extra ref so object won't die
1176 vm_object_reference_locked(object
);
1177 vm_fault_cleanup(object
, first_m
);
1178 counter(c_vm_fault_page_block_backoff_kernel
++);
1180 vm_object_lock(object
);
1181 assert(object
->ref_count
> 0);
1183 if (!object
->pager_ready
) {
1184 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1186 vm_object_unlock(object
);
1187 if (wait_result
== THREAD_WAITING
)
1188 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1189 vm_object_deallocate(object
);
1193 vm_object_unlock(object
);
1194 vm_object_deallocate(object
);
1195 thread_interrupt_level(interruptible_state
);
1197 return (VM_FAULT_RETRY
);
1200 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1202 * If there are too many outstanding page
1203 * requests pending on this external object, we
1204 * wait for them to be resolved now.
1207 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1209 if (m
!= VM_PAGE_NULL
)
1212 * take an extra ref so object won't die
1214 vm_object_reference_locked(object
);
1216 vm_fault_cleanup(object
, first_m
);
1218 counter(c_vm_fault_page_block_backoff_kernel
++);
1220 vm_object_lock(object
);
1221 assert(object
->ref_count
> 0);
1223 if (object
->paging_in_progress
> vm_object_pagein_throttle
) {
1224 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
, interruptible
);
1226 vm_object_unlock(object
);
1227 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1228 vm_object_deallocate(object
);
1232 vm_object_unlock(object
);
1233 vm_object_deallocate(object
);
1234 thread_interrupt_level(interruptible_state
);
1236 return (VM_FAULT_RETRY
);
1239 if (m
!= VM_PAGE_NULL
) {
1241 * Indicate that the page is waiting for data
1242 * from the memory manager.
1244 m
->list_req_pending
= TRUE
;
1249 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1253 * It's possible someone called vm_object_destroy while we weren't
1254 * holding the object lock. If that has happened, then bail out
1258 pager
= object
->pager
;
1260 if (pager
== MEMORY_OBJECT_NULL
) {
1261 vm_fault_cleanup(object
, first_m
);
1262 thread_interrupt_level(interruptible_state
);
1263 return VM_FAULT_MEMORY_ERROR
;
1267 * We have an absent page in place for the faulting offset,
1268 * so we can release the object lock.
1271 vm_object_unlock(object
);
1274 * If this object uses a copy_call strategy,
1275 * and we are interested in a copy of this object
1276 * (having gotten here only by following a
1277 * shadow chain), then tell the memory manager
1278 * via a flag added to the desired_access
1279 * parameter, so that it can detect a race
1280 * between our walking down the shadow chain
1281 * and its pushing pages up into a copy of
1282 * the object that it manages.
1284 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
)
1285 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1287 wants_copy_flag
= VM_PROT_NONE
;
1290 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1291 (integer_t
)object
, offset
, (integer_t
)m
,
1292 access_required
| wants_copy_flag
, 0);
1295 * Call the memory manager to retrieve the data.
1297 rc
= memory_object_data_request(
1299 offset
+ object
->paging_offset
,
1301 access_required
| wants_copy_flag
,
1302 (memory_object_fault_info_t
)fault_info
);
1305 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1307 vm_object_lock(object
);
1309 if (rc
!= KERN_SUCCESS
) {
1311 vm_fault_cleanup(object
, first_m
);
1312 thread_interrupt_level(interruptible_state
);
1314 return ((rc
== MACH_SEND_INTERRUPTED
) ?
1315 VM_FAULT_INTERRUPTED
:
1316 VM_FAULT_MEMORY_ERROR
);
1318 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_mode
& TH_MODE_ABORT
)) {
1320 vm_fault_cleanup(object
, first_m
);
1321 thread_interrupt_level(interruptible_state
);
1323 return (VM_FAULT_INTERRUPTED
);
1325 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1327 * No page here means that the object we
1328 * initially looked up was "physically
1329 * contiguous" (i.e. device memory). However,
1330 * with Virtual VRAM, the object might not
1331 * be backed by that device memory anymore,
1332 * so we're done here only if the object is
1333 * still "phys_contiguous".
1334 * Otherwise, if the object is no longer
1335 * "phys_contiguous", we need to retry the
1336 * page fault against the object's new backing
1337 * store (different memory object).
1342 * potentially a pagein fault
1343 * if we make it through the state checks
1344 * above, than we'll count it as such
1346 my_fault
= DBG_PAGEIN_FAULT
;
1349 * Retry with same object/offset, since new data may
1350 * be in a different page (i.e., m is meaningless at
1357 * We get here if the object has no pager, or an existence map
1358 * exists and indicates the page isn't present on the pager
1359 * or we're unwiring a page. If a pager exists, but there
1360 * is no existence map, then the m->absent case above handles
1361 * the ZF case when the pager can't provide the page
1364 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1366 if (object
== first_object
)
1369 assert(m
== VM_PAGE_NULL
);
1372 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1373 (integer_t
)object
, offset
, (integer_t
)m
,
1374 (integer_t
)object
->shadow
, 0);
1376 next_object
= object
->shadow
;
1378 if (next_object
== VM_OBJECT_NULL
) {
1380 * we've hit the bottom of the shadown chain,
1381 * fill the page in the top object with zeros.
1383 assert(!must_be_resident
);
1385 if (object
!= first_object
) {
1386 vm_object_paging_end(object
);
1387 vm_object_unlock(object
);
1389 object
= first_object
;
1390 offset
= first_offset
;
1391 vm_object_lock(object
);
1394 assert(m
->object
== object
);
1395 first_m
= VM_PAGE_NULL
;
1398 * check for any conditions that prevent
1399 * us from creating a new zero-fill page
1400 * vm_fault_check will do all of the
1401 * fault cleanup in the case of an error condition
1402 * including resetting the thread_interrupt_level
1404 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
1406 if (error
!= VM_FAULT_SUCCESS
)
1409 if (m
== VM_PAGE_NULL
) {
1412 if (m
== VM_PAGE_NULL
) {
1413 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1414 thread_interrupt_level(interruptible_state
);
1416 return (VM_FAULT_MEMORY_SHORTAGE
);
1418 vm_page_insert(m
, object
, offset
);
1420 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1426 * Move on to the next object. Lock the next
1427 * object before unlocking the current one.
1429 if ((object
!= first_object
) || must_be_resident
)
1430 vm_object_paging_end(object
);
1432 offset
+= object
->shadow_offset
;
1433 fault_info
->lo_offset
+= object
->shadow_offset
;
1434 fault_info
->hi_offset
+= object
->shadow_offset
;
1435 access_required
= VM_PROT_READ
;
1437 vm_object_lock(next_object
);
1438 vm_object_unlock(object
);
1440 object
= next_object
;
1441 vm_object_paging_begin(object
);
1446 * PAGE HAS BEEN FOUND.
1449 * busy, so that we can play with it;
1450 * not absent, so that nobody else will fill it;
1451 * possibly eligible for pageout;
1453 * The top-level page (first_m) is:
1454 * VM_PAGE_NULL if the page was found in the
1456 * busy, not absent, and ineligible for pageout.
1458 * The current object (object) is locked. A paging
1459 * reference is held for the current and top-level
1464 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1466 #if EXTRA_ASSERTIONS
1467 if (m
!= VM_PAGE_NULL
) {
1468 assert(m
->busy
&& !m
->absent
);
1469 assert((first_m
== VM_PAGE_NULL
) ||
1470 (first_m
->busy
&& !first_m
->absent
&&
1471 !first_m
->active
&& !first_m
->inactive
));
1473 #endif /* EXTRA_ASSERTIONS */
1477 * If we found a page, we must have decrypted it before we
1480 if (m
!= VM_PAGE_NULL
) {
1481 ASSERT_PAGE_DECRYPTED(m
);
1485 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1486 (integer_t
)object
, offset
, (integer_t
)m
,
1487 (integer_t
)first_object
, (integer_t
)first_m
);
1490 * If the page is being written, but isn't
1491 * already owned by the top-level object,
1492 * we have to copy it into a new page owned
1493 * by the top-level object.
1495 if ((object
!= first_object
) && (m
!= VM_PAGE_NULL
)) {
1498 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1500 if (fault_type
& VM_PROT_WRITE
) {
1504 * We only really need to copy if we
1507 assert(!must_be_resident
);
1510 * are we protecting the system from
1511 * backing store exhaustion. If so
1512 * sleep unless we are privileged.
1514 if (vm_backing_store_low
) {
1515 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1518 vm_fault_cleanup(object
, first_m
);
1520 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1522 thread_block(THREAD_CONTINUE_NULL
);
1523 thread_interrupt_level(interruptible_state
);
1525 return (VM_FAULT_RETRY
);
1529 * If we try to collapse first_object at this
1530 * point, we may deadlock when we try to get
1531 * the lock on an intermediate object (since we
1532 * have the bottom object locked). We can't
1533 * unlock the bottom object, because the page
1534 * we found may move (by collapse) if we do.
1536 * Instead, we first copy the page. Then, when
1537 * we have no more use for the bottom object,
1538 * we unlock it and try to collapse.
1540 * Note that we copy the page even if we didn't
1541 * need to... that's the breaks.
1545 * Allocate a page for the copy
1547 copy_m
= vm_page_grab();
1549 if (copy_m
== VM_PAGE_NULL
) {
1552 vm_fault_cleanup(object
, first_m
);
1553 thread_interrupt_level(interruptible_state
);
1555 return (VM_FAULT_MEMORY_SHORTAGE
);
1558 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
1559 (integer_t
)object
, offset
,
1560 (integer_t
)m
, (integer_t
)copy_m
, 0);
1562 vm_page_copy(m
, copy_m
);
1565 * If another map is truly sharing this
1566 * page with us, we have to flush all
1567 * uses of the original page, since we
1568 * can't distinguish those which want the
1569 * original from those which need the
1572 * XXXO If we know that only one map has
1573 * access to this page, then we could
1574 * avoid the pmap_disconnect() call.
1577 pmap_disconnect(m
->phys_page
);
1579 assert(!m
->cleaning
);
1582 * We no longer need the old page or object.
1584 PAGE_WAKEUP_DONE(m
);
1585 vm_object_paging_end(object
);
1586 vm_object_unlock(object
);
1588 my_fault
= DBG_COW_FAULT
;
1589 VM_STAT_INCR(cow_faults
);
1590 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
1591 current_task()->cow_faults
++;
1593 object
= first_object
;
1594 offset
= first_offset
;
1596 vm_object_lock(object
);
1598 * get rid of the place holder
1599 * page that we soldered in earlier
1601 VM_PAGE_FREE(first_m
);
1602 first_m
= VM_PAGE_NULL
;
1605 * and replace it with the
1606 * page we just copied into
1608 assert(copy_m
->busy
);
1609 vm_page_insert(copy_m
, object
, offset
);
1610 copy_m
->dirty
= TRUE
;
1614 * Now that we've gotten the copy out of the
1615 * way, let's try to collapse the top object.
1616 * But we have to play ugly games with
1617 * paging_in_progress to do that...
1619 vm_object_paging_end(object
);
1620 vm_object_collapse(object
, offset
, TRUE
);
1621 vm_object_paging_begin(object
);
1624 *protection
&= (~VM_PROT_WRITE
);
1627 * Now check whether the page needs to be pushed into the
1628 * copy object. The use of asymmetric copy on write for
1629 * shared temporary objects means that we may do two copies to
1630 * satisfy the fault; one above to get the page from a
1631 * shadowed object, and one here to push it into the copy.
1633 try_failed_count
= 0;
1635 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
&& (m
!= VM_PAGE_NULL
)) {
1636 vm_object_offset_t copy_offset
;
1640 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1643 * If the page is being written, but hasn't been
1644 * copied to the copy-object, we have to copy it there.
1646 if ((fault_type
& VM_PROT_WRITE
) == 0) {
1647 *protection
&= ~VM_PROT_WRITE
;
1652 * If the page was guaranteed to be resident,
1653 * we must have already performed the copy.
1655 if (must_be_resident
)
1659 * Try to get the lock on the copy_object.
1661 if (!vm_object_lock_try(copy_object
)) {
1663 vm_object_unlock(object
);
1666 mutex_pause(try_failed_count
); /* wait a bit */
1667 vm_object_lock(object
);
1671 try_failed_count
= 0;
1674 * Make another reference to the copy-object,
1675 * to keep it from disappearing during the
1678 vm_object_reference_locked(copy_object
);
1681 * Does the page exist in the copy?
1683 copy_offset
= first_offset
- copy_object
->shadow_offset
;
1685 if (copy_object
->size
<= copy_offset
)
1687 * Copy object doesn't cover this page -- do nothing.
1690 else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
1692 * Page currently exists in the copy object
1696 * If the page is being brought
1697 * in, wait for it and then retry.
1702 * take an extra ref so object won't die
1704 vm_object_reference_locked(copy_object
);
1705 vm_object_unlock(copy_object
);
1706 vm_fault_cleanup(object
, first_m
);
1707 counter(c_vm_fault_page_block_backoff_kernel
++);
1709 vm_object_lock(copy_object
);
1710 assert(copy_object
->ref_count
> 0);
1711 VM_OBJ_RES_DECR(copy_object
);
1712 vm_object_lock_assert_exclusive(copy_object
);
1713 copy_object
->ref_count
--;
1714 assert(copy_object
->ref_count
> 0);
1715 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
1718 * it's OK if the "copy_m" page is encrypted,
1719 * because we're not moving it nor handling its
1722 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
1723 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
1725 vm_object_unlock(copy_object
);
1726 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1727 vm_object_deallocate(copy_object
);
1731 vm_object_unlock(copy_object
);
1732 vm_object_deallocate(copy_object
);
1733 thread_interrupt_level(interruptible_state
);
1735 return (VM_FAULT_RETRY
);
1739 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
1741 * If PAGED_OUT is TRUE, then the page used to exist
1742 * in the copy-object, and has already been paged out.
1743 * We don't need to repeat this. If PAGED_OUT is
1744 * FALSE, then either we don't know (!pager_created,
1745 * for example) or it hasn't been paged out.
1746 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
1747 * We must copy the page to the copy object.
1750 if (vm_backing_store_low
) {
1752 * we are protecting the system from
1753 * backing store exhaustion. If so
1754 * sleep unless we are privileged.
1756 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1757 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1760 VM_OBJ_RES_DECR(copy_object
);
1761 vm_object_lock_assert_exclusive(copy_object
);
1762 copy_object
->ref_count
--;
1763 assert(copy_object
->ref_count
> 0);
1765 vm_object_unlock(copy_object
);
1766 vm_fault_cleanup(object
, first_m
);
1767 thread_block(THREAD_CONTINUE_NULL
);
1768 thread_interrupt_level(interruptible_state
);
1770 return (VM_FAULT_RETRY
);
1774 * Allocate a page for the copy
1776 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
1778 if (copy_m
== VM_PAGE_NULL
) {
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_interrupt_level(interruptible_state
);
1790 return (VM_FAULT_MEMORY_SHORTAGE
);
1793 * Must copy page into copy-object.
1795 vm_page_copy(m
, copy_m
);
1798 * If the old page was in use by any users
1799 * of the copy-object, it must be removed
1800 * from all pmaps. (We can't know which
1804 pmap_disconnect(m
->phys_page
);
1807 * If there's a pager, then immediately
1808 * page out this page, using the "initialize"
1809 * option. Else, we use the copy.
1811 if ((!copy_object
->pager_created
)
1813 || vm_external_state_get(copy_object
->existence_map
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
1817 vm_page_lockspin_queues();
1818 assert(!m
->cleaning
);
1819 vm_page_activate(copy_m
);
1820 vm_page_unlock_queues();
1822 copy_m
->dirty
= TRUE
;
1823 PAGE_WAKEUP_DONE(copy_m
);
1826 assert(copy_m
->busy
== TRUE
);
1827 assert(!m
->cleaning
);
1830 * dirty is protected by the object lock
1832 copy_m
->dirty
= TRUE
;
1835 * The page is already ready for pageout:
1836 * not on pageout queues and busy.
1837 * Unlock everything except the
1838 * copy_object itself.
1840 vm_object_unlock(object
);
1843 * Write the page to the copy-object,
1844 * flushing it from the kernel.
1846 vm_pageout_initialize_page(copy_m
);
1849 * Since the pageout may have
1850 * temporarily dropped the
1851 * copy_object's lock, we
1852 * check whether we'll have
1853 * to deallocate the hard way.
1855 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
1856 vm_object_unlock(copy_object
);
1857 vm_object_deallocate(copy_object
);
1858 vm_object_lock(object
);
1863 * Pick back up the old object's
1864 * lock. [It is safe to do so,
1865 * since it must be deeper in the
1868 vm_object_lock(object
);
1871 * Because we're pushing a page upward
1872 * in the object tree, we must restart
1873 * any faults that are waiting here.
1874 * [Note that this is an expansion of
1875 * PAGE_WAKEUP that uses the THREAD_RESTART
1876 * wait result]. Can't turn off the page's
1877 * busy bit because we're not done with it.
1881 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
1885 * The reference count on copy_object must be
1886 * at least 2: one for our extra reference,
1887 * and at least one from the outside world
1888 * (we checked that when we last locked
1891 vm_object_lock_assert_exclusive(copy_object
);
1892 copy_object
->ref_count
--;
1893 assert(copy_object
->ref_count
> 0);
1895 VM_OBJ_RES_DECR(copy_object
);
1896 vm_object_unlock(copy_object
);
1901 *top_page
= first_m
;
1904 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
1905 (integer_t
)object
, offset
, (integer_t
)m
, (integer_t
)first_m
, 0);
1907 if (m
!= VM_PAGE_NULL
) {
1908 if (my_fault
== DBG_PAGEIN_FAULT
) {
1910 VM_STAT_INCR(pageins
);
1911 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
1912 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1913 current_task()->pageins
++;
1915 if (m
->object
->internal
) {
1916 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
1918 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
1922 * evaluate access pattern and update state
1923 * vm_fault_deactivate_behind depends on the
1924 * state being up to date
1926 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
1928 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
1931 *type_of_fault
= my_fault
;
1933 vm_object_unlock(object
);
1935 thread_interrupt_level(interruptible_state
);
1938 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
1940 return (VM_FAULT_SUCCESS
);
1943 thread_interrupt_level(interruptible_state
);
1945 if (wait_result
== THREAD_INTERRUPTED
)
1946 return (VM_FAULT_INTERRUPTED
);
1947 return (VM_FAULT_RETRY
);
1955 * page queue lock must NOT be held
1956 * m->object must be locked
1958 * NOTE: m->object could be locked "shared" only if we are called
1959 * from vm_fault() as part of a soft fault. If so, we must be
1960 * careful not to modify the VM object in any way that is not
1961 * legal under a shared lock...
1963 unsigned long cs_enter_tainted_rejected
= 0;
1964 unsigned long cs_enter_tainted_accepted
= 0;
1966 vm_fault_enter(vm_page_t m
,
1968 vm_map_offset_t vaddr
,
1971 boolean_t change_wiring
,
1975 unsigned int cache_attr
;
1977 boolean_t previously_pmapped
= m
->pmapped
;
1979 vm_object_lock_assert_held(m
->object
);
1981 mutex_assert(&vm_page_queue_lock
, MA_NOTOWNED
);
1984 if (m
->phys_page
== vm_page_guard_addr
) {
1985 assert(m
->fictitious
);
1986 return KERN_SUCCESS
;
1989 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
1991 if (m
->object
->code_signed
&& !m
->cs_validated
&&
1992 pmap
!= kernel_pmap
) {
1995 * This page comes from a VM object backed by a
1996 * signed memory object and it hasn't been validated yet.
1997 * We're about to enter it into a process address space,
1998 * so we need to validate its signature now.
2000 vm_object_lock_assert_exclusive(m
->object
);
2002 /* VM map still locked, so 1 ref will remain on VM object */
2004 vm_page_validate_cs(m
);
2007 if (m
->pmapped
== FALSE
) {
2009 * This is the first time this page is being
2010 * mapped in an address space (pmapped == FALSE).
2012 * Part of that page may still be in the data cache
2013 * and not flushed to memory. In case we end up
2014 * accessing that page via the instruction cache,
2015 * we need to ensure that the 2 caches are in sync.
2017 pmap_sync_page_data_phys(m
->phys_page
);
2019 if ((*type_of_fault
== DBG_CACHE_HIT_FAULT
) && m
->clustered
) {
2021 * found it in the cache, but this
2022 * is the first fault-in of the page (m->pmapped == FALSE)
2023 * so it must have come in as part of
2024 * a cluster... account 1 pagein against it
2026 VM_STAT_INCR(pageins
);
2027 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
2029 if (m
->object
->internal
) {
2030 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
2032 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
2035 current_task()->pageins
++;
2037 *type_of_fault
= DBG_PAGEIN_FAULT
;
2039 VM_PAGE_CONSUME_CLUSTERED(m
);
2041 } else if (cache_attr
!= VM_WIMG_DEFAULT
)
2042 pmap_sync_page_attributes_phys(m
->phys_page
);
2044 if (*type_of_fault
!= DBG_COW_FAULT
) {
2045 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2047 if (pmap
== kernel_pmap
) {
2048 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2052 if (m
->cs_tainted
) {
2055 * This page has been tainted and can not be trusted.
2056 * Let's notify the current process and let it take any
2057 * necessary precautions before we enter the tainted page
2058 * into its address space.
2060 if (cs_invalid_page()) {
2061 /* reject the tainted page: abort the page fault */
2062 kr
= KERN_MEMORY_ERROR
;
2063 cs_enter_tainted_rejected
++;
2065 /* proceed with the tainted page */
2067 cs_enter_tainted_accepted
++;
2069 if (cs_debug
|| kr
!= KERN_SUCCESS
) {
2070 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2071 "page %p obj %p off 0x%llx *** TAINTED ***\n",
2072 (long long)vaddr
, m
, m
->object
, m
->offset
);
2075 /* proceed with the valid page */
2079 if (kr
== KERN_SUCCESS
) {
2081 * NOTE: we may only hold the vm_object lock SHARED
2082 * at this point, but the update of pmapped is ok
2083 * since this is the ONLY bit updated behind the SHARED
2084 * lock... however, we need to figure out how to do an atomic
2085 * update on a bit field to make this less fragile... right
2086 * now I don'w know how to coerce 'C' to give me the offset info
2087 * that's needed for an AtomicCompareAndSwap
2091 PMAP_ENTER(pmap
, vaddr
, m
, prot
, cache_attr
, wired
);
2095 * Hold queues lock to manipulate
2096 * the page queues. Change wiring
2099 if (change_wiring
) {
2100 vm_page_lockspin_queues();
2103 if (kr
== KERN_SUCCESS
) {
2109 vm_page_unlock_queues();
2112 if (kr
!= KERN_SUCCESS
) {
2113 vm_page_lock_queues();
2114 vm_page_deactivate(m
);
2115 vm_page_unlock_queues();
2117 if (((!m
->active
&& !m
->inactive
) || no_cache
) && !m
->wire_count
&& !m
->throttled
) {
2118 vm_page_lockspin_queues();
2120 * test again now that we hold the page queue lock
2122 if (((!m
->active
&& !m
->inactive
) || no_cache
) && !m
->wire_count
) {
2125 * If this is a no_cache mapping and the page has never been
2126 * mapped before or was previously a no_cache page, then we
2127 * want to leave pages in the speculative state so that they
2128 * can be readily recycled if free memory runs low. Otherwise
2129 * the page is activated as normal.
2132 if (no_cache
&& (!previously_pmapped
|| m
->no_cache
)) {
2135 if (m
->active
|| m
->inactive
)
2136 VM_PAGE_QUEUES_REMOVE(m
);
2138 if (!m
->speculative
)
2139 vm_page_speculate(m
, TRUE
);
2141 } else if (!m
->active
&& !m
->inactive
)
2142 vm_page_activate(m
);
2146 vm_page_unlock_queues();
2157 * Handle page faults, including pseudo-faults
2158 * used to change the wiring status of pages.
2160 * Explicit continuations have been removed.
2162 * vm_fault and vm_fault_page save mucho state
2163 * in the moral equivalent of a closure. The state
2164 * structure is allocated when first entering vm_fault
2165 * and deallocated when leaving vm_fault.
2168 extern int _map_enter_debug
;
2170 unsigned long vm_fault_collapse_total
= 0;
2171 unsigned long vm_fault_collapse_skipped
= 0;
2176 vm_map_offset_t vaddr
,
2177 vm_prot_t fault_type
,
2178 boolean_t change_wiring
,
2181 vm_map_offset_t caller_pmap_addr
)
2183 vm_map_version_t version
; /* Map version for verificiation */
2184 boolean_t wired
; /* Should mapping be wired down? */
2185 vm_object_t object
; /* Top-level object */
2186 vm_object_offset_t offset
; /* Top-level offset */
2187 vm_prot_t prot
; /* Protection for mapping */
2188 vm_object_t old_copy_object
; /* Saved copy object */
2189 vm_page_t result_page
; /* Result of vm_fault_page */
2190 vm_page_t top_page
; /* Placeholder page */
2193 vm_page_t m
; /* Fast access to result_page */
2194 kern_return_t error_code
;
2195 vm_object_t cur_object
;
2196 vm_object_offset_t cur_offset
;
2198 vm_object_t new_object
;
2201 boolean_t interruptible_state
;
2202 vm_map_t real_map
= map
;
2203 vm_map_t original_map
= map
;
2204 vm_prot_t original_fault_type
;
2205 struct vm_object_fault_info fault_info
;
2206 boolean_t need_collapse
= FALSE
;
2207 int object_lock_type
= 0;
2208 int cur_object_lock_type
;
2211 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
2212 (int)((uint64_t)vaddr
>> 32),
2218 if (get_preemption_level() != 0) {
2219 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
2220 (int)((uint64_t)vaddr
>> 32),
2226 return (KERN_FAILURE
);
2228 interruptible_state
= thread_interrupt_level(interruptible
);
2230 VM_STAT_INCR(faults
);
2231 current_task()->faults
++;
2232 original_fault_type
= fault_type
;
2234 if (fault_type
& VM_PROT_WRITE
)
2235 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2237 object_lock_type
= OBJECT_LOCK_SHARED
;
2239 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
2243 * assume we will hit a page in the cache
2244 * otherwise, explicitly override with
2245 * the real fault type once we determine it
2247 type_of_fault
= DBG_CACHE_HIT_FAULT
;
2250 * Find the backing store object and offset into
2251 * it to begin the search.
2253 fault_type
= original_fault_type
;
2255 vm_map_lock_read(map
);
2257 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
2258 object_lock_type
, &version
,
2259 &object
, &offset
, &prot
, &wired
,
2263 if (kr
!= KERN_SUCCESS
) {
2264 vm_map_unlock_read(map
);
2267 pmap
= real_map
->pmap
;
2268 fault_info
.interruptible
= interruptible
;
2271 * If the page is wired, we must fault for the current protection
2272 * value, to avoid further faults.
2275 fault_type
= prot
| VM_PROT_WRITE
;
2278 * since we're treating this fault as a 'write'
2279 * we must hold the top object lock exclusively
2281 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2283 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2285 if (vm_object_lock_upgrade(object
) == FALSE
) {
2287 * couldn't upgrade, so explictly
2288 * take the lock exclusively
2290 vm_object_lock(object
);
2295 #if VM_FAULT_CLASSIFY
2297 * Temporary data gathering code
2299 vm_fault_classify(object
, offset
, fault_type
);
2302 * Fast fault code. The basic idea is to do as much as
2303 * possible while holding the map lock and object locks.
2304 * Busy pages are not used until the object lock has to
2305 * be dropped to do something (copy, zero fill, pmap enter).
2306 * Similarly, paging references aren't acquired until that
2307 * point, and object references aren't used.
2309 * If we can figure out what to do
2310 * (zero fill, copy on write, pmap enter) while holding
2311 * the locks, then it gets done. Otherwise, we give up,
2312 * and use the original fault path (which doesn't hold
2313 * the map lock, and relies on busy pages).
2314 * The give up cases include:
2315 * - Have to talk to pager.
2316 * - Page is busy, absent or in error.
2317 * - Pager has locked out desired access.
2318 * - Fault needs to be restarted.
2319 * - Have to push page into copy object.
2321 * The code is an infinite loop that moves one level down
2322 * the shadow chain each time. cur_object and cur_offset
2323 * refer to the current object being examined. object and offset
2324 * are the original object from the map. The loop is at the
2325 * top level if and only if object and cur_object are the same.
2327 * Invariants: Map lock is held throughout. Lock is held on
2328 * original object and cur_object (if different) when
2329 * continuing or exiting loop.
2335 * If this page is to be inserted in a copy delay object
2336 * for writing, and if the object has a copy, then the
2337 * copy delay strategy is implemented in the slow fault page.
2339 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
2340 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
))
2341 goto handle_copy_delay
;
2343 cur_object
= object
;
2344 cur_offset
= offset
;
2347 m
= vm_page_lookup(cur_object
, cur_offset
);
2349 if (m
!= VM_PAGE_NULL
) {
2351 wait_result_t result
;
2354 * in order to do the PAGE_ASSERT_WAIT, we must
2355 * have object that 'm' belongs to locked exclusively
2357 if (object
!= cur_object
) {
2358 vm_object_unlock(object
);
2360 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2362 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2364 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2366 * couldn't upgrade so go do a full retry
2367 * immediately since we've already dropped
2368 * the top object lock associated with this page
2369 * and the current one got dropped due to the
2370 * failed upgrade... the state is no longer valid
2372 vm_map_unlock_read(map
);
2373 if (real_map
!= map
)
2374 vm_map_unlock(real_map
);
2379 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2381 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2383 if (vm_object_lock_upgrade(object
) == FALSE
) {
2385 * couldn't upgrade, so explictly take the lock
2386 * exclusively and go relookup the page since we
2387 * will have dropped the object lock and
2388 * a different thread could have inserted
2389 * a page at this offset
2390 * no need for a full retry since we're
2391 * at the top level of the object chain
2393 vm_object_lock(object
);
2398 vm_map_unlock_read(map
);
2399 if (real_map
!= map
)
2400 vm_map_unlock(real_map
);
2402 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
2404 vm_object_unlock(cur_object
);
2406 if (result
== THREAD_WAITING
) {
2407 result
= thread_block(THREAD_CONTINUE_NULL
);
2409 counter(c_vm_fault_page_block_busy_kernel
++);
2411 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
2417 if (m
->phys_page
== vm_page_guard_addr
) {
2419 * Guard page: let the slow path deal with it
2423 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
)) {
2425 * Unusual case... let the slow path deal with it
2432 * We've soft-faulted (because it's not in the page
2433 * table) on an encrypted page.
2434 * Keep the page "busy" so that no one messes with
2435 * it during the decryption.
2436 * Release the extra locks we're holding, keep only
2437 * the page's VM object lock.
2439 * in order to set 'busy' on 'm', we must
2440 * have object that 'm' belongs to locked exclusively
2442 if (object
!= cur_object
) {
2443 vm_object_unlock(object
);
2445 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2447 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2449 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2451 * couldn't upgrade so go do a full retry
2452 * immediately since we've already dropped
2453 * the top object lock associated with this page
2454 * and the current one got dropped due to the
2455 * failed upgrade... the state is no longer valid
2457 vm_map_unlock_read(map
);
2458 if (real_map
!= map
)
2459 vm_map_unlock(real_map
);
2464 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2466 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2468 if (vm_object_lock_upgrade(object
) == FALSE
) {
2470 * couldn't upgrade, so explictly take the lock
2471 * exclusively and go relookup the page since we
2472 * will have dropped the object lock and
2473 * a different thread could have inserted
2474 * a page at this offset
2475 * no need for a full retry since we're
2476 * at the top level of the object chain
2478 vm_object_lock(object
);
2485 vm_map_unlock_read(map
);
2486 if (real_map
!= map
)
2487 vm_map_unlock(real_map
);
2489 vm_page_decrypt(m
, 0);
2492 PAGE_WAKEUP_DONE(m
);
2494 vm_object_unlock(cur_object
);
2496 * Retry from the top, in case anything
2497 * changed while we were decrypting...
2501 ASSERT_PAGE_DECRYPTED(m
);
2503 if (m
->object
->code_signed
&& !m
->cs_validated
) {
2505 * We will need to validate this page
2506 * against its code signature, so we
2507 * want to hold the VM object exclusively.
2509 if (object
!= cur_object
) {
2510 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2511 vm_object_unlock(object
);
2512 vm_object_unlock(cur_object
);
2514 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2516 vm_map_unlock_read(map
);
2517 if (real_map
!= map
)
2518 vm_map_unlock(real_map
);
2523 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2525 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2527 if (vm_object_lock_upgrade(object
) == FALSE
) {
2529 * couldn't upgrade, so explictly take the lock
2530 * exclusively and go relookup the page since we
2531 * will have dropped the object lock and
2532 * a different thread could have inserted
2533 * a page at this offset
2534 * no need for a full retry since we're
2535 * at the top level of the object chain
2537 vm_object_lock(object
);
2544 * Two cases of map in faults:
2545 * - At top level w/o copy object.
2546 * - Read fault anywhere.
2547 * --> must disallow write.
2550 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
)
2553 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2555 prot
&= ~VM_PROT_WRITE
;
2558 * Set up to map the page...
2559 * mark the page busy, drop
2560 * unneeded object lock
2562 if (object
!= cur_object
) {
2564 * don't need the original object anymore
2566 vm_object_unlock(object
);
2569 * switch to the object that has the new page
2571 object
= cur_object
;
2572 object_lock_type
= cur_object_lock_type
;
2576 * prepare for the pmap_enter...
2577 * object and map are both locked
2578 * m contains valid data
2579 * object == m->object
2580 * cur_object == NULL or it's been unlocked
2581 * no paging references on either object or cur_object
2584 if (db_watchpoint_list
&& (fault_type
& VM_PROT_WRITE
) == 0)
2585 prot
&= ~VM_PROT_WRITE
;
2588 kr
= vm_fault_enter(m
,
2594 fault_info
.no_cache
,
2597 kr
= vm_fault_enter(m
,
2603 fault_info
.no_cache
,
2607 if (need_collapse
== TRUE
)
2608 vm_object_collapse(object
, offset
, TRUE
);
2610 if (type_of_fault
== DBG_PAGEIN_FAULT
) {
2612 * evaluate access pattern and update state
2613 * vm_fault_deactivate_behind depends on the
2614 * state being up to date
2616 vm_fault_is_sequential(object
, cur_offset
, fault_info
.behavior
);
2618 vm_fault_deactivate_behind(object
, cur_offset
, fault_info
.behavior
);
2621 * That's it, clean up and return.
2624 PAGE_WAKEUP_DONE(m
);
2626 vm_object_unlock(object
);
2628 vm_map_unlock_read(map
);
2629 if (real_map
!= map
)
2630 vm_map_unlock(real_map
);
2635 * COPY ON WRITE FAULT
2637 * If objects match, then
2638 * object->copy must not be NULL (else control
2639 * would be in previous code block), and we
2640 * have a potential push into the copy object
2641 * with which we can't cope with here.
2643 if (cur_object
== object
) {
2645 * must take the slow path to
2646 * deal with the copy push
2650 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
2653 * This is now a shadow based copy on write
2654 * fault -- it requires a copy up the shadow
2657 * Allocate a page in the original top level
2658 * object. Give up if allocate fails. Also
2659 * need to remember current page, as it's the
2660 * source of the copy.
2662 * at this point we hold locks on both
2663 * object and cur_object... no need to take
2664 * paging refs or mark pages BUSY since
2665 * we don't drop either object lock until
2666 * the page has been copied and inserted
2671 if (m
== VM_PAGE_NULL
) {
2673 * no free page currently available...
2674 * must take the slow path
2679 * Now do the copy. Mark the source page busy...
2681 * NOTE: This code holds the map lock across
2684 vm_page_copy(cur_m
, m
);
2685 vm_page_insert(m
, object
, offset
);
2689 * Now cope with the source page and object
2691 if (object
->ref_count
> 1 && cur_m
->pmapped
)
2692 pmap_disconnect(cur_m
->phys_page
);
2694 need_collapse
= TRUE
;
2696 if (!cur_object
->internal
&&
2697 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
2699 * The object from which we've just
2700 * copied a page is most probably backed
2701 * by a vnode. We don't want to waste too
2702 * much time trying to collapse the VM objects
2703 * and create a bottleneck when several tasks
2704 * map the same file.
2706 if (cur_object
->copy
== object
) {
2708 * Shared mapping or no COW yet.
2709 * We can never collapse a copy
2710 * object into its backing object.
2712 need_collapse
= FALSE
;
2713 } else if (cur_object
->copy
== object
->shadow
&&
2714 object
->shadow
->resident_page_count
== 0) {
2716 * Shared mapping after a COW occurred.
2718 need_collapse
= FALSE
;
2721 vm_object_unlock(cur_object
);
2723 if (need_collapse
== FALSE
)
2724 vm_fault_collapse_skipped
++;
2725 vm_fault_collapse_total
++;
2727 type_of_fault
= DBG_COW_FAULT
;
2728 VM_STAT_INCR(cow_faults
);
2729 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2730 current_task()->cow_faults
++;
2736 * No page at cur_object, cur_offset... m == NULL
2738 if (cur_object
->pager_created
) {
2739 if (MUST_ASK_PAGER(cur_object
, cur_offset
) == TRUE
) {
2741 * May have to talk to a pager...
2742 * take the slow path.
2747 * existence map present and indicates
2748 * that the pager doesn't have this page
2751 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
2753 * Zero fill fault. Page gets
2754 * inserted into the original object.
2756 if (cur_object
->shadow_severed
) {
2758 if (object
!= cur_object
)
2759 vm_object_unlock(cur_object
);
2760 vm_object_unlock(object
);
2762 vm_map_unlock_read(map
);
2763 if (real_map
!= map
)
2764 vm_map_unlock(real_map
);
2766 kr
= KERN_MEMORY_ERROR
;
2769 if (VM_PAGE_ZFILL_THROTTLED()) {
2771 * drop all of our locks...
2772 * wait until the free queue is
2773 * pumped back up and then
2776 if (object
!= cur_object
)
2777 vm_object_unlock(cur_object
);
2778 vm_object_unlock(object
);
2779 vm_map_unlock_read(map
);
2780 if (real_map
!= map
)
2781 vm_map_unlock(real_map
);
2783 if (vm_page_wait((change_wiring
) ?
2791 if (vm_backing_store_low
) {
2793 * we are protecting the system from
2794 * backing store exhaustion...
2795 * must take the slow path if we're
2798 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
))
2801 if (cur_object
!= object
) {
2802 vm_object_unlock(cur_object
);
2804 cur_object
= object
;
2806 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2808 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2810 if (vm_object_lock_upgrade(object
) == FALSE
) {
2812 * couldn't upgrade so do a full retry on the fault
2813 * since we dropped the object lock which
2814 * could allow another thread to insert
2815 * a page at this offset
2817 vm_map_unlock_read(map
);
2818 if (real_map
!= map
)
2819 vm_map_unlock(real_map
);
2824 m
= vm_page_alloc(object
, offset
);
2826 if (m
== VM_PAGE_NULL
) {
2828 * no free page currently available...
2829 * must take the slow path
2835 * Now zero fill page...
2836 * the page is probably going to
2837 * be written soon, so don't bother
2838 * to clear the modified bit
2840 * NOTE: This code holds the map
2841 * lock across the zero fill.
2843 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
2848 * On to the next level in the shadow chain
2850 cur_offset
+= cur_object
->shadow_offset
;
2851 new_object
= cur_object
->shadow
;
2854 * take the new_object's lock with the indicated state
2856 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
)
2857 vm_object_lock_shared(new_object
);
2859 vm_object_lock(new_object
);
2861 if (cur_object
!= object
)
2862 vm_object_unlock(cur_object
);
2864 cur_object
= new_object
;
2870 * Cleanup from fast fault failure. Drop any object
2871 * lock other than original and drop map lock.
2873 if (object
!= cur_object
)
2874 vm_object_unlock(cur_object
);
2877 * must own the object lock exclusively at this point
2879 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2880 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2882 if (vm_object_lock_upgrade(object
) == FALSE
) {
2884 * couldn't upgrade, so explictly
2885 * take the lock exclusively
2886 * no need to retry the fault at this
2887 * point since "vm_fault_page" will
2888 * completely re-evaluate the state
2890 vm_object_lock(object
);
2895 vm_map_unlock_read(map
);
2896 if (real_map
!= map
)
2897 vm_map_unlock(real_map
);
2900 * Make a reference to this object to
2901 * prevent its disposal while we are messing with
2902 * it. Once we have the reference, the map is free
2903 * to be diddled. Since objects reference their
2904 * shadows (and copies), they will stay around as well.
2906 vm_object_reference_locked(object
);
2907 vm_object_paging_begin(object
);
2909 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
2913 kr
= vm_fault_page(object
, offset
, fault_type
,
2914 (change_wiring
&& !wired
),
2915 &prot
, &result_page
, &top_page
,
2917 &error_code
, map
->no_zero_fill
,
2918 FALSE
, &fault_info
);
2921 * if kr != VM_FAULT_SUCCESS, then the paging reference
2922 * has been dropped and the object unlocked... the ref_count
2925 * if kr == VM_FAULT_SUCCESS, then the paging reference
2926 * is still held along with the ref_count on the original object
2928 * if m != NULL, then the object it belongs to
2929 * is returned locked with a paging reference
2931 * if top_page != NULL, then it's BUSY and the
2932 * object it belongs to has a paging reference
2933 * but is returned unlocked
2935 if (kr
!= VM_FAULT_SUCCESS
) {
2937 * we didn't succeed, lose the object reference immediately.
2939 vm_object_deallocate(object
);
2942 * See why we failed, and take corrective action.
2945 case VM_FAULT_MEMORY_SHORTAGE
:
2946 if (vm_page_wait((change_wiring
) ?
2953 case VM_FAULT_INTERRUPTED
:
2956 case VM_FAULT_RETRY
:
2958 case VM_FAULT_MEMORY_ERROR
:
2962 kr
= KERN_MEMORY_ERROR
;
2968 if (m
!= VM_PAGE_NULL
) {
2969 assert((change_wiring
&& !wired
) ?
2970 (top_page
== VM_PAGE_NULL
) :
2971 ((top_page
== VM_PAGE_NULL
) == (m
->object
== object
)));
2975 * What to do with the resulting page from vm_fault_page
2976 * if it doesn't get entered into the physical map:
2978 #define RELEASE_PAGE(m) \
2980 PAGE_WAKEUP_DONE(m); \
2981 vm_page_lockspin_queues(); \
2982 if (!m->active && !m->inactive && !m->throttled)\
2983 vm_page_activate(m); \
2984 vm_page_unlock_queues(); \
2988 * We must verify that the maps have not changed
2989 * since our last lookup.
2991 if (m
!= VM_PAGE_NULL
) {
2992 old_copy_object
= m
->object
->copy
;
2993 vm_object_unlock(m
->object
);
2995 old_copy_object
= VM_OBJECT_NULL
;
2998 * no object locks are held at this point
3000 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
3001 vm_object_t retry_object
;
3002 vm_object_offset_t retry_offset
;
3003 vm_prot_t retry_prot
;
3006 * To avoid trying to write_lock the map while another
3007 * thread has it read_locked (in vm_map_pageable), we
3008 * do not try for write permission. If the page is
3009 * still writable, we will get write permission. If it
3010 * is not, or has been marked needs_copy, we enter the
3011 * mapping without write permission, and will merely
3012 * take another fault.
3015 vm_map_lock_read(map
);
3017 kr
= vm_map_lookup_locked(&map
, vaddr
,
3018 fault_type
& ~VM_PROT_WRITE
,
3019 OBJECT_LOCK_EXCLUSIVE
, &version
,
3020 &retry_object
, &retry_offset
, &retry_prot
,
3024 pmap
= real_map
->pmap
;
3026 if (kr
!= KERN_SUCCESS
) {
3027 vm_map_unlock_read(map
);
3029 if (m
!= VM_PAGE_NULL
) {
3031 * retake the lock so that
3032 * we can drop the paging reference
3033 * in vm_fault_cleanup and do the
3034 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3036 vm_object_lock(m
->object
);
3040 vm_fault_cleanup(m
->object
, top_page
);
3043 * retake the lock so that
3044 * we can drop the paging reference
3045 * in vm_fault_cleanup
3047 vm_object_lock(object
);
3049 vm_fault_cleanup(object
, top_page
);
3051 vm_object_deallocate(object
);
3055 vm_object_unlock(retry_object
);
3057 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
3059 vm_map_unlock_read(map
);
3060 if (real_map
!= map
)
3061 vm_map_unlock(real_map
);
3063 if (m
!= VM_PAGE_NULL
) {
3065 * retake the lock so that
3066 * we can drop the paging reference
3067 * in vm_fault_cleanup and do the
3068 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3070 vm_object_lock(m
->object
);
3074 vm_fault_cleanup(m
->object
, top_page
);
3077 * retake the lock so that
3078 * we can drop the paging reference
3079 * in vm_fault_cleanup
3081 vm_object_lock(object
);
3083 vm_fault_cleanup(object
, top_page
);
3085 vm_object_deallocate(object
);
3090 * Check whether the protection has changed or the object
3091 * has been copied while we left the map unlocked.
3095 if (m
!= VM_PAGE_NULL
) {
3096 vm_object_lock(m
->object
);
3098 if (m
->object
->copy
!= old_copy_object
) {
3100 * The copy object changed while the top-level object
3101 * was unlocked, so take away write permission.
3103 prot
&= ~VM_PROT_WRITE
;
3106 vm_object_lock(object
);
3109 * If we want to wire down this page, but no longer have
3110 * adequate permissions, we must start all over.
3112 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
3114 vm_map_verify_done(map
, &version
);
3115 if (real_map
!= map
)
3116 vm_map_unlock(real_map
);
3118 if (m
!= VM_PAGE_NULL
) {
3121 vm_fault_cleanup(m
->object
, top_page
);
3123 vm_fault_cleanup(object
, top_page
);
3125 vm_object_deallocate(object
);
3129 if (m
!= VM_PAGE_NULL
) {
3131 * Put this page into the physical map.
3132 * We had to do the unlock above because pmap_enter
3133 * may cause other faults. The page may be on
3134 * the pageout queues. If the pageout daemon comes
3135 * across the page, it will remove it from the queues.
3138 kr
= vm_fault_enter(m
,
3144 fault_info
.no_cache
,
3147 kr
= vm_fault_enter(m
,
3153 fault_info
.no_cache
,
3156 if (kr
!= KERN_SUCCESS
) {
3157 /* abort this page fault */
3158 vm_map_verify_done(map
, &version
);
3159 if (real_map
!= map
)
3160 vm_map_unlock(real_map
);
3161 PAGE_WAKEUP_DONE(m
);
3162 vm_fault_cleanup(m
->object
, top_page
);
3163 vm_object_deallocate(object
);
3168 vm_map_entry_t entry
;
3169 vm_map_offset_t laddr
;
3170 vm_map_offset_t ldelta
, hdelta
;
3173 * do a pmap block mapping from the physical address
3178 /* While we do not worry about execution protection in */
3179 /* general, certian pages may have instruction execution */
3180 /* disallowed. We will check here, and if not allowed */
3181 /* to execute, we return with a protection failure. */
3183 if ((fault_type
& VM_PROT_EXECUTE
) &&
3184 (!pmap_eligible_for_execute((ppnum_t
)(object
->shadow_offset
>> 12)))) {
3186 vm_map_verify_done(map
, &version
);
3188 if (real_map
!= map
)
3189 vm_map_unlock(real_map
);
3191 vm_fault_cleanup(object
, top_page
);
3192 vm_object_deallocate(object
);
3194 kr
= KERN_PROTECTION_FAILURE
;
3199 if (real_map
!= map
)
3200 vm_map_unlock(real_map
);
3202 if (original_map
!= map
) {
3203 vm_map_unlock_read(map
);
3204 vm_map_lock_read(original_map
);
3210 hdelta
= 0xFFFFF000;
3211 ldelta
= 0xFFFFF000;
3213 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
3214 if (ldelta
> (laddr
- entry
->vme_start
))
3215 ldelta
= laddr
- entry
->vme_start
;
3216 if (hdelta
> (entry
->vme_end
- laddr
))
3217 hdelta
= entry
->vme_end
- laddr
;
3218 if (entry
->is_sub_map
) {
3220 laddr
= (laddr
- entry
->vme_start
)
3222 vm_map_lock_read(entry
->object
.sub_map
);
3224 if (map
!= real_map
)
3225 vm_map_unlock_read(map
);
3226 if (entry
->use_pmap
) {
3227 vm_map_unlock_read(real_map
);
3228 real_map
= entry
->object
.sub_map
;
3230 map
= entry
->object
.sub_map
;
3237 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
3238 (entry
->object
.vm_object
!= NULL
) &&
3239 (entry
->object
.vm_object
== object
)) {
3243 * Set up a block mapped area
3245 pmap_map_block(caller_pmap
,
3246 (addr64_t
)(caller_pmap_addr
- ldelta
),
3247 (((vm_map_offset_t
) (entry
->object
.vm_object
->shadow_offset
)) +
3248 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12,
3249 ((ldelta
+ hdelta
) >> 12), prot
,
3250 (VM_WIMG_MASK
& (int)object
->wimg_bits
), 0);
3253 * Set up a block mapped area
3255 pmap_map_block(real_map
->pmap
,
3256 (addr64_t
)(vaddr
- ldelta
),
3257 (((vm_map_offset_t
)(entry
->object
.vm_object
->shadow_offset
)) +
3258 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12,
3259 ((ldelta
+ hdelta
) >> 12), prot
,
3260 (VM_WIMG_MASK
& (int)object
->wimg_bits
), 0);
3266 * Unlock everything, and return
3268 vm_map_verify_done(map
, &version
);
3269 if (real_map
!= map
)
3270 vm_map_unlock(real_map
);
3272 if (m
!= VM_PAGE_NULL
) {
3273 PAGE_WAKEUP_DONE(m
);
3275 vm_fault_cleanup(m
->object
, top_page
);
3277 vm_fault_cleanup(object
, top_page
);
3279 vm_object_deallocate(object
);
3285 thread_interrupt_level(interruptible_state
);
3287 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3288 (int)((uint64_t)vaddr
>> 32),
3300 * Wire down a range of virtual addresses in a map.
3305 vm_map_entry_t entry
,
3307 vm_map_offset_t pmap_addr
)
3310 register vm_map_offset_t va
;
3311 register vm_map_offset_t end_addr
= entry
->vme_end
;
3312 register kern_return_t rc
;
3314 assert(entry
->in_transition
);
3316 if ((entry
->object
.vm_object
!= NULL
) &&
3317 !entry
->is_sub_map
&&
3318 entry
->object
.vm_object
->phys_contiguous
) {
3319 return KERN_SUCCESS
;
3323 * Inform the physical mapping system that the
3324 * range of addresses may not fault, so that
3325 * page tables and such can be locked down as well.
3328 pmap_pageable(pmap
, pmap_addr
,
3329 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
3332 * We simulate a fault to get the page and enter it
3333 * in the physical map.
3336 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3337 if ((rc
= vm_fault_wire_fast(
3338 map
, va
, entry
, pmap
,
3339 pmap_addr
+ (va
- entry
->vme_start
)
3340 )) != KERN_SUCCESS
) {
3341 rc
= vm_fault(map
, va
, VM_PROT_NONE
, TRUE
,
3342 (pmap
== kernel_pmap
) ?
3343 THREAD_UNINT
: THREAD_ABORTSAFE
,
3344 pmap
, pmap_addr
+ (va
- entry
->vme_start
));
3345 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
3348 if (rc
!= KERN_SUCCESS
) {
3349 struct vm_map_entry tmp_entry
= *entry
;
3351 /* unwire wired pages */
3352 tmp_entry
.vme_end
= va
;
3353 vm_fault_unwire(map
,
3354 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
3359 return KERN_SUCCESS
;
3365 * Unwire a range of virtual addresses in a map.
3370 vm_map_entry_t entry
,
3371 boolean_t deallocate
,
3373 vm_map_offset_t pmap_addr
)
3375 register vm_map_offset_t va
;
3376 register vm_map_offset_t end_addr
= entry
->vme_end
;
3378 struct vm_object_fault_info fault_info
;
3380 object
= (entry
->is_sub_map
)
3381 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
3384 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
3385 * do anything since such memory is wired by default. So we don't have
3386 * anything to undo here.
3389 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
)
3392 fault_info
.interruptible
= THREAD_UNINT
;
3393 fault_info
.behavior
= entry
->behavior
;
3394 fault_info
.user_tag
= entry
->alias
;
3395 fault_info
.lo_offset
= entry
->offset
;
3396 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
3397 fault_info
.no_cache
= entry
->no_cache
;
3400 * Since the pages are wired down, we must be able to
3401 * get their mappings from the physical map system.
3404 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3407 pmap_change_wiring(pmap
,
3408 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3410 if (object
== VM_OBJECT_NULL
) {
3411 (void) vm_fault(map
, va
, VM_PROT_NONE
,
3412 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
3415 vm_page_t result_page
;
3417 vm_object_t result_object
;
3418 vm_fault_return_t result
;
3420 fault_info
.cluster_size
= end_addr
- va
;
3423 prot
= VM_PROT_NONE
;
3425 vm_object_lock(object
);
3426 vm_object_paging_begin(object
);
3428 "vm_fault_unwire -> vm_fault_page\n",
3430 result
= vm_fault_page(
3432 entry
->offset
+ (va
- entry
->vme_start
),
3434 &prot
, &result_page
, &top_page
,
3436 NULL
, map
->no_zero_fill
,
3437 FALSE
, &fault_info
);
3438 } while (result
== VM_FAULT_RETRY
);
3441 * If this was a mapping to a file on a device that has been forcibly
3442 * unmounted, then we won't get a page back from vm_fault_page(). Just
3443 * move on to the next one in case the remaining pages are mapped from
3444 * different objects. During a forced unmount, the object is terminated
3445 * so the alive flag will be false if this happens. A forced unmount will
3446 * will occur when an external disk is unplugged before the user does an
3447 * eject, so we don't want to panic in that situation.
3450 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
)
3453 if (result
!= VM_FAULT_SUCCESS
)
3454 panic("vm_fault_unwire: failure");
3456 result_object
= result_page
->object
;
3459 assert(result_page
->phys_page
!=
3460 vm_page_fictitious_addr
);
3461 pmap_disconnect(result_page
->phys_page
);
3462 VM_PAGE_FREE(result_page
);
3464 vm_page_lockspin_queues();
3465 vm_page_unwire(result_page
);
3466 vm_page_unlock_queues();
3467 PAGE_WAKEUP_DONE(result_page
);
3469 vm_fault_cleanup(result_object
, top_page
);
3474 * Inform the physical mapping system that the range
3475 * of addresses may fault, so that page tables and
3476 * such may be unwired themselves.
3479 pmap_pageable(pmap
, pmap_addr
,
3480 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
3485 * vm_fault_wire_fast:
3487 * Handle common case of a wire down page fault at the given address.
3488 * If successful, the page is inserted into the associated physical map.
3489 * The map entry is passed in to avoid the overhead of a map lookup.
3491 * NOTE: the given address should be truncated to the
3492 * proper page address.
3494 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
3495 * a standard error specifying why the fault is fatal is returned.
3497 * The map in question must be referenced, and remains so.
3498 * Caller has a read lock on the map.
3500 * This is a stripped version of vm_fault() for wiring pages. Anything
3501 * other than the common case will return KERN_FAILURE, and the caller
3502 * is expected to call vm_fault().
3506 __unused vm_map_t map
,
3508 vm_map_entry_t entry
,
3510 vm_map_offset_t pmap_addr
)
3513 vm_object_offset_t offset
;
3514 register vm_page_t m
;
3516 thread_t thread
= current_thread();
3520 VM_STAT_INCR(faults
);
3522 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
)
3523 thread
->task
->faults
++;
3530 #define RELEASE_PAGE(m) { \
3531 PAGE_WAKEUP_DONE(m); \
3532 vm_page_lockspin_queues(); \
3533 vm_page_unwire(m); \
3534 vm_page_unlock_queues(); \
3538 #undef UNLOCK_THINGS
3539 #define UNLOCK_THINGS { \
3540 vm_object_paging_end(object); \
3541 vm_object_unlock(object); \
3544 #undef UNLOCK_AND_DEALLOCATE
3545 #define UNLOCK_AND_DEALLOCATE { \
3547 vm_object_deallocate(object); \
3550 * Give up and have caller do things the hard way.
3554 UNLOCK_AND_DEALLOCATE; \
3555 return(KERN_FAILURE); \
3560 * If this entry is not directly to a vm_object, bail out.
3562 if (entry
->is_sub_map
)
3563 return(KERN_FAILURE
);
3566 * Find the backing store object and offset into it.
3569 object
= entry
->object
.vm_object
;
3570 offset
= (va
- entry
->vme_start
) + entry
->offset
;
3571 prot
= entry
->protection
;
3574 * Make a reference to this object to prevent its
3575 * disposal while we are messing with it.
3578 vm_object_lock(object
);
3579 vm_object_reference_locked(object
);
3580 vm_object_paging_begin(object
);
3583 * INVARIANTS (through entire routine):
3585 * 1) At all times, we must either have the object
3586 * lock or a busy page in some object to prevent
3587 * some other thread from trying to bring in
3590 * 2) Once we have a busy page, we must remove it from
3591 * the pageout queues, so that the pageout daemon
3592 * will not grab it away.
3597 * Look for page in top-level object. If it's not there or
3598 * there's something going on, give up.
3599 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
3600 * decrypt the page before wiring it down.
3602 m
= vm_page_lookup(object
, offset
);
3603 if ((m
== VM_PAGE_NULL
) || (m
->busy
) || (m
->encrypted
) ||
3604 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
3608 ASSERT_PAGE_DECRYPTED(m
);
3610 if (m
->fictitious
&&
3611 m
->phys_page
== vm_page_guard_addr
) {
3613 * Guard pages are fictitious pages and are never
3614 * entered into a pmap, so let's say it's been wired...
3621 * Wire the page down now. All bail outs beyond this
3622 * point must unwire the page.
3625 vm_page_lockspin_queues();
3627 vm_page_unlock_queues();
3630 * Mark page busy for other threads.
3637 * Give up if the page is being written and there's a copy object
3639 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
3645 * Put this page into the physical map.
3647 type_of_fault
= DBG_CACHE_HIT_FAULT
;
3648 kr
= vm_fault_enter(m
,
3659 * Unlock everything, and return
3662 PAGE_WAKEUP_DONE(m
);
3663 UNLOCK_AND_DEALLOCATE
;
3670 * Routine: vm_fault_copy_cleanup
3672 * Release a page used by vm_fault_copy.
3676 vm_fault_copy_cleanup(
3680 vm_object_t object
= page
->object
;
3682 vm_object_lock(object
);
3683 PAGE_WAKEUP_DONE(page
);
3684 vm_page_lockspin_queues();
3685 if (!page
->active
&& !page
->inactive
&& !page
->throttled
)
3686 vm_page_activate(page
);
3687 vm_page_unlock_queues();
3688 vm_fault_cleanup(object
, top_page
);
3692 vm_fault_copy_dst_cleanup(
3697 if (page
!= VM_PAGE_NULL
) {
3698 object
= page
->object
;
3699 vm_object_lock(object
);
3700 vm_page_lockspin_queues();
3701 vm_page_unwire(page
);
3702 vm_page_unlock_queues();
3703 vm_object_paging_end(object
);
3704 vm_object_unlock(object
);
3709 * Routine: vm_fault_copy
3712 * Copy pages from one virtual memory object to another --
3713 * neither the source nor destination pages need be resident.
3715 * Before actually copying a page, the version associated with
3716 * the destination address map wil be verified.
3718 * In/out conditions:
3719 * The caller must hold a reference, but not a lock, to
3720 * each of the source and destination objects and to the
3724 * Returns KERN_SUCCESS if no errors were encountered in
3725 * reading or writing the data. Returns KERN_INTERRUPTED if
3726 * the operation was interrupted (only possible if the
3727 * "interruptible" argument is asserted). Other return values
3728 * indicate a permanent error in copying the data.
3730 * The actual amount of data copied will be returned in the
3731 * "copy_size" argument. In the event that the destination map
3732 * verification failed, this amount may be less than the amount
3737 vm_object_t src_object
,
3738 vm_object_offset_t src_offset
,
3739 vm_map_size_t
*copy_size
, /* INOUT */
3740 vm_object_t dst_object
,
3741 vm_object_offset_t dst_offset
,
3743 vm_map_version_t
*dst_version
,
3746 vm_page_t result_page
;
3749 vm_page_t src_top_page
;
3753 vm_page_t dst_top_page
;
3756 vm_map_size_t amount_left
;
3757 vm_object_t old_copy_object
;
3758 kern_return_t error
= 0;
3760 vm_map_size_t part_size
;
3761 struct vm_object_fault_info fault_info_src
;
3762 struct vm_object_fault_info fault_info_dst
;
3765 * In order not to confuse the clustered pageins, align
3766 * the different offsets on a page boundary.
3771 *copy_size -= amount_left; \
3775 amount_left
= *copy_size
;
3777 fault_info_src
.interruptible
= interruptible
;
3778 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
3779 fault_info_src
.user_tag
= 0;
3780 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
3781 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
3782 fault_info_src
.no_cache
= FALSE
;
3784 fault_info_dst
.interruptible
= interruptible
;
3785 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
3786 fault_info_dst
.user_tag
= 0;
3787 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
3788 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
3789 fault_info_dst
.no_cache
= FALSE
;
3791 do { /* while (amount_left > 0) */
3793 * There may be a deadlock if both source and destination
3794 * pages are the same. To avoid this deadlock, the copy must
3795 * start by getting the destination page in order to apply
3796 * COW semantics if any.
3799 RetryDestinationFault
: ;
3801 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
3803 vm_object_lock(dst_object
);
3804 vm_object_paging_begin(dst_object
);
3806 fault_info_dst
.cluster_size
= amount_left
;
3808 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
3809 switch (vm_fault_page(dst_object
,
3810 vm_object_trunc_page(dst_offset
),
3811 VM_PROT_WRITE
|VM_PROT_READ
,
3813 &dst_prot
, &dst_page
, &dst_top_page
,
3816 dst_map
->no_zero_fill
,
3817 FALSE
, &fault_info_dst
)) {
3818 case VM_FAULT_SUCCESS
:
3820 case VM_FAULT_RETRY
:
3821 goto RetryDestinationFault
;
3822 case VM_FAULT_MEMORY_SHORTAGE
:
3823 if (vm_page_wait(interruptible
))
3824 goto RetryDestinationFault
;
3826 case VM_FAULT_INTERRUPTED
:
3827 RETURN(MACH_SEND_INTERRUPTED
);
3828 case VM_FAULT_MEMORY_ERROR
:
3832 return(KERN_MEMORY_ERROR
);
3834 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
3836 old_copy_object
= dst_page
->object
->copy
;
3839 * There exists the possiblity that the source and
3840 * destination page are the same. But we can't
3841 * easily determine that now. If they are the
3842 * same, the call to vm_fault_page() for the
3843 * destination page will deadlock. To prevent this we
3844 * wire the page so we can drop busy without having
3845 * the page daemon steal the page. We clean up the
3846 * top page but keep the paging reference on the object
3847 * holding the dest page so it doesn't go away.
3850 vm_page_lockspin_queues();
3851 vm_page_wire(dst_page
);
3852 vm_page_unlock_queues();
3853 PAGE_WAKEUP_DONE(dst_page
);
3854 vm_object_unlock(dst_page
->object
);
3856 if (dst_top_page
!= VM_PAGE_NULL
) {
3857 vm_object_lock(dst_object
);
3858 VM_PAGE_FREE(dst_top_page
);
3859 vm_object_paging_end(dst_object
);
3860 vm_object_unlock(dst_object
);
3865 if (src_object
== VM_OBJECT_NULL
) {
3867 * No source object. We will just
3868 * zero-fill the page in dst_object.
3870 src_page
= VM_PAGE_NULL
;
3871 result_page
= VM_PAGE_NULL
;
3873 vm_object_lock(src_object
);
3874 src_page
= vm_page_lookup(src_object
,
3875 vm_object_trunc_page(src_offset
));
3876 if (src_page
== dst_page
) {
3877 src_prot
= dst_prot
;
3878 result_page
= VM_PAGE_NULL
;
3880 src_prot
= VM_PROT_READ
;
3881 vm_object_paging_begin(src_object
);
3883 fault_info_src
.cluster_size
= amount_left
;
3886 "vm_fault_copy(2) -> vm_fault_page\n",
3888 switch (vm_fault_page(
3890 vm_object_trunc_page(src_offset
),
3891 VM_PROT_READ
, FALSE
,
3893 &result_page
, &src_top_page
,
3894 (int *)0, &error
, FALSE
,
3895 FALSE
, &fault_info_src
)) {
3897 case VM_FAULT_SUCCESS
:
3899 case VM_FAULT_RETRY
:
3900 goto RetrySourceFault
;
3901 case VM_FAULT_MEMORY_SHORTAGE
:
3902 if (vm_page_wait(interruptible
))
3903 goto RetrySourceFault
;
3905 case VM_FAULT_INTERRUPTED
:
3906 vm_fault_copy_dst_cleanup(dst_page
);
3907 RETURN(MACH_SEND_INTERRUPTED
);
3908 case VM_FAULT_MEMORY_ERROR
:
3909 vm_fault_copy_dst_cleanup(dst_page
);
3913 return(KERN_MEMORY_ERROR
);
3917 assert((src_top_page
== VM_PAGE_NULL
) ==
3918 (result_page
->object
== src_object
));
3920 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
3921 vm_object_unlock(result_page
->object
);
3924 if (!vm_map_verify(dst_map
, dst_version
)) {
3925 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3926 vm_fault_copy_cleanup(result_page
, src_top_page
);
3927 vm_fault_copy_dst_cleanup(dst_page
);
3931 vm_object_lock(dst_page
->object
);
3933 if (dst_page
->object
->copy
!= old_copy_object
) {
3934 vm_object_unlock(dst_page
->object
);
3935 vm_map_verify_done(dst_map
, dst_version
);
3936 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3937 vm_fault_copy_cleanup(result_page
, src_top_page
);
3938 vm_fault_copy_dst_cleanup(dst_page
);
3941 vm_object_unlock(dst_page
->object
);
3944 * Copy the page, and note that it is dirty
3948 if (!page_aligned(src_offset
) ||
3949 !page_aligned(dst_offset
) ||
3950 !page_aligned(amount_left
)) {
3952 vm_object_offset_t src_po
,
3955 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
3956 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
3958 if (dst_po
> src_po
) {
3959 part_size
= PAGE_SIZE
- dst_po
;
3961 part_size
= PAGE_SIZE
- src_po
;
3963 if (part_size
> (amount_left
)){
3964 part_size
= amount_left
;
3967 if (result_page
== VM_PAGE_NULL
) {
3968 vm_page_part_zero_fill(dst_page
,
3971 vm_page_part_copy(result_page
, src_po
,
3972 dst_page
, dst_po
, part_size
);
3973 if(!dst_page
->dirty
){
3974 vm_object_lock(dst_object
);
3975 dst_page
->dirty
= TRUE
;
3976 vm_object_unlock(dst_page
->object
);
3981 part_size
= PAGE_SIZE
;
3983 if (result_page
== VM_PAGE_NULL
)
3984 vm_page_zero_fill(dst_page
);
3986 vm_page_copy(result_page
, dst_page
);
3987 if(!dst_page
->dirty
){
3988 vm_object_lock(dst_object
);
3989 dst_page
->dirty
= TRUE
;
3990 vm_object_unlock(dst_page
->object
);
3997 * Unlock everything, and return
4000 vm_map_verify_done(dst_map
, dst_version
);
4002 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4003 vm_fault_copy_cleanup(result_page
, src_top_page
);
4004 vm_fault_copy_dst_cleanup(dst_page
);
4006 amount_left
-= part_size
;
4007 src_offset
+= part_size
;
4008 dst_offset
+= part_size
;
4009 } while (amount_left
> 0);
4011 RETURN(KERN_SUCCESS
);
4017 #if VM_FAULT_CLASSIFY
4019 * Temporary statistics gathering support.
4023 * Statistics arrays:
4025 #define VM_FAULT_TYPES_MAX 5
4026 #define VM_FAULT_LEVEL_MAX 8
4028 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
4030 #define VM_FAULT_TYPE_ZERO_FILL 0
4031 #define VM_FAULT_TYPE_MAP_IN 1
4032 #define VM_FAULT_TYPE_PAGER 2
4033 #define VM_FAULT_TYPE_COPY 3
4034 #define VM_FAULT_TYPE_OTHER 4
4038 vm_fault_classify(vm_object_t object
,
4039 vm_object_offset_t offset
,
4040 vm_prot_t fault_type
)
4042 int type
, level
= 0;
4046 m
= vm_page_lookup(object
, offset
);
4047 if (m
!= VM_PAGE_NULL
) {
4048 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
) {
4049 type
= VM_FAULT_TYPE_OTHER
;
4052 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
4053 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
4054 type
= VM_FAULT_TYPE_MAP_IN
;
4057 type
= VM_FAULT_TYPE_COPY
;
4061 if (object
->pager_created
) {
4062 type
= VM_FAULT_TYPE_PAGER
;
4065 if (object
->shadow
== VM_OBJECT_NULL
) {
4066 type
= VM_FAULT_TYPE_ZERO_FILL
;
4070 offset
+= object
->shadow_offset
;
4071 object
= object
->shadow
;
4077 if (level
> VM_FAULT_LEVEL_MAX
)
4078 level
= VM_FAULT_LEVEL_MAX
;
4080 vm_fault_stats
[type
][level
] += 1;
4085 /* cleanup routine to call from debugger */
4088 vm_fault_classify_init(void)
4092 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
4093 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
4094 vm_fault_stats
[type
][level
] = 0;
4100 #endif /* VM_FAULT_CLASSIFY */
4103 extern int cs_validation
;
4106 vm_page_validate_cs(
4110 vm_object_offset_t offset
;
4111 vm_map_offset_t koffset
;
4112 vm_map_size_t ksize
;
4115 memory_object_t pager
;
4117 boolean_t validated
, tainted
;
4118 boolean_t busy_page
;
4120 vm_object_lock_assert_exclusive(page
->object
);
4121 assert(!page
->cs_validated
);
4123 if (!cs_validation
) {
4127 object
= page
->object
;
4128 assert(object
->code_signed
);
4129 offset
= page
->offset
;
4131 busy_page
= page
->busy
;
4133 /* keep page busy while we map (and unlock) the VM object */
4138 * Take a paging reference on the VM object
4139 * to protect it from collapse or bypass,
4140 * and keep it from disappearing too.
4142 vm_object_paging_begin(object
);
4144 /* map the page in the kernel address space */
4146 ksize
= PAGE_SIZE_64
;
4147 kr
= vm_paging_map_object(&koffset
,
4152 FALSE
); /* can't unlock object ! */
4153 if (kr
!= KERN_SUCCESS
) {
4154 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr
);
4156 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
4159 * Since we get here to validate a page that was brought in by
4160 * the pager, we know that this pager is all setup and ready
4163 assert(!object
->internal
);
4164 assert(object
->pager
!= NULL
);
4165 assert(object
->pager_ready
);
4167 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
4169 * The object is terminating and we don't have its pager
4170 * so we can't validate the data...
4175 pager
= object
->pager
;
4176 assert(pager
!= NULL
);
4178 kr
= vnode_pager_get_object_cs_blobs(pager
, &blobs
);
4179 if (kr
!= KERN_SUCCESS
) {
4183 /* verify the SHA1 hash for this page */
4184 validated
= cs_validate_page(blobs
,
4185 offset
+ object
->paging_offset
,
4186 (const void *)kaddr
,
4190 assert(object
== page
->object
);
4191 vm_object_lock_assert_exclusive(object
);
4193 page
->cs_validated
= validated
;
4195 page
->cs_tainted
= tainted
;
4200 PAGE_WAKEUP_DONE(page
);
4203 /* unmap the map from the kernel address space */
4204 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
4209 vm_object_paging_end(object
);