2 * Copyright (c) 2000-2009 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>
67 #include <libkern/OSAtomic.h>
69 #include <mach/mach_types.h>
70 #include <mach/kern_return.h>
71 #include <mach/message.h> /* for error codes */
72 #include <mach/vm_param.h>
73 #include <mach/vm_behavior.h>
74 #include <mach/memory_object.h>
75 /* For memory_object_data_{request,unlock} */
78 #include <kern/kern_types.h>
79 #include <kern/host_statistics.h>
80 #include <kern/counters.h>
81 #include <kern/task.h>
82 #include <kern/thread.h>
83 #include <kern/sched_prim.h>
84 #include <kern/host.h>
86 #include <kern/mach_param.h>
87 #include <kern/macro_help.h>
88 #include <kern/zalloc.h>
89 #include <kern/misc_protos.h>
91 #include <vm/vm_fault.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_kern.h>
97 #include <vm/vm_pageout.h>
98 #include <vm/vm_protos.h>
99 #include <vm/vm_external.h>
100 #include <vm/memory_object.h>
101 #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
102 #include <vm/vm_shared_region.h>
104 #define VM_FAULT_CLASSIFY 0
106 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
108 int vm_object_pagein_throttle
= 16;
111 * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
112 * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts
113 * of memory if they're buggy and can run the system completely out of swap space. If this happens, we
114 * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps
115 * keep the UI active so that the user has a chance to kill the offending task before the system
118 * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
119 * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold
120 * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a
121 * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
124 boolean_t
thread_is_io_throttled(void);
126 uint64_t vm_hard_throttle_threshold
;
128 extern unsigned int dp_pages_free
, dp_pages_reserve
;
130 #define NEED_TO_HARD_THROTTLE_THIS_TASK() (((dp_pages_free + dp_pages_reserve < 2000) && \
131 (get_task_resident_size(current_task()) > vm_hard_throttle_threshold) && \
132 (current_task() != kernel_task) && VM_DYNAMIC_PAGING_ENABLED(memory_manager_default)) || \
133 (vm_page_free_count < vm_page_throttle_limit && thread_is_io_throttled() && \
134 (get_task_resident_size(current_task()) > vm_hard_throttle_threshold)))
137 #define HARD_THROTTLE_DELAY 20000 /* 20000 us == 20 ms */
138 #define SOFT_THROTTLE_DELAY 2000 /* 2000 us == 2 ms */
143 boolean_t
current_thread_aborted(void);
145 /* Forward declarations of internal routines. */
146 extern kern_return_t
vm_fault_wire_fast(
149 vm_map_entry_t entry
,
151 vm_map_offset_t pmap_addr
);
153 extern void vm_fault_continue(void);
155 extern void vm_fault_copy_cleanup(
159 extern void vm_fault_copy_dst_cleanup(
162 #if VM_FAULT_CLASSIFY
163 extern void vm_fault_classify(vm_object_t object
,
164 vm_object_offset_t offset
,
165 vm_prot_t fault_type
);
167 extern void vm_fault_classify_init(void);
170 unsigned long vm_pmap_enter_blocked
= 0;
171 unsigned long vm_pmap_enter_retried
= 0;
173 unsigned long vm_cs_validates
= 0;
174 unsigned long vm_cs_revalidates
= 0;
175 unsigned long vm_cs_query_modified
= 0;
176 unsigned long vm_cs_validated_dirtied
= 0;
177 unsigned long vm_cs_bitmap_validated
= 0;
178 #if CONFIG_ENFORCE_SIGNED_CODE
179 int cs_enforcement_disable
=0;
181 static const int cs_enforcement_disable
=1;
185 * Routine: vm_fault_init
187 * Initialize our private data structures.
193 #if CONFIG_ENFORCE_SIGNED_CODE
194 PE_parse_boot_argn("cs_enforcement_disable", &cs_enforcement_disable
,
195 sizeof (cs_enforcement_disable
));
197 PE_parse_boot_argn("cs_debug", &cs_debug
, sizeof (cs_debug
));
201 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
202 * computed as a percentage of available memory, and the percentage used is scaled inversely with
203 * the amount of memory. The pertange runs between 10% and 35%. We use 35% for small memory systems
204 * and reduce the value down to 10% for very large memory configurations. This helps give us a
205 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
206 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
209 vm_hard_throttle_threshold
= sane_size
* (35 - MIN((int)(sane_size
/ (1024*1024*1024)), 25)) / 100;
213 * Routine: vm_fault_cleanup
215 * Clean up the result of vm_fault_page.
217 * The paging reference for "object" is released.
218 * "object" is unlocked.
219 * If "top_page" is not null, "top_page" is
220 * freed and the paging reference for the object
221 * containing it is released.
224 * "object" must be locked.
228 register vm_object_t object
,
229 register vm_page_t top_page
)
231 vm_object_paging_end(object
);
232 vm_object_unlock(object
);
234 if (top_page
!= VM_PAGE_NULL
) {
235 object
= top_page
->object
;
237 vm_object_lock(object
);
238 VM_PAGE_FREE(top_page
);
239 vm_object_paging_end(object
);
240 vm_object_unlock(object
);
244 #if MACH_CLUSTER_STATS
245 #define MAXCLUSTERPAGES 16
247 unsigned long pages_in_cluster
;
248 unsigned long pages_at_higher_offsets
;
249 unsigned long pages_at_lower_offsets
;
250 } cluster_stats_in
[MAXCLUSTERPAGES
];
251 #define CLUSTER_STAT(clause) clause
252 #define CLUSTER_STAT_HIGHER(x) \
253 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
254 #define CLUSTER_STAT_LOWER(x) \
255 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
256 #define CLUSTER_STAT_CLUSTER(x) \
257 ((cluster_stats_in[(x)].pages_in_cluster)++)
258 #else /* MACH_CLUSTER_STATS */
259 #define CLUSTER_STAT(clause)
260 #endif /* MACH_CLUSTER_STATS */
262 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
265 boolean_t vm_page_deactivate_behind
= TRUE
;
267 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
269 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
270 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
271 /* we use it to size an array on the stack */
273 int vm_default_behind
= VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW
;
275 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
278 * vm_page_is_sequential
280 * Determine if sequential access is in progress
281 * in accordance with the behavior specified.
282 * Update state to indicate current access pattern.
284 * object must have at least the shared lock held
288 vm_fault_is_sequential(
290 vm_object_offset_t offset
,
291 vm_behavior_t behavior
)
293 vm_object_offset_t last_alloc
;
297 last_alloc
= object
->last_alloc
;
298 sequential
= object
->sequential
;
299 orig_sequential
= sequential
;
302 case VM_BEHAVIOR_RANDOM
:
304 * reset indicator of sequential behavior
309 case VM_BEHAVIOR_SEQUENTIAL
:
310 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
312 * advance indicator of sequential behavior
314 if (sequential
< MAX_SEQUENTIAL_RUN
)
315 sequential
+= PAGE_SIZE
;
318 * reset indicator of sequential behavior
324 case VM_BEHAVIOR_RSEQNTL
:
325 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
327 * advance indicator of sequential behavior
329 if (sequential
> -MAX_SEQUENTIAL_RUN
)
330 sequential
-= PAGE_SIZE
;
333 * reset indicator of sequential behavior
339 case VM_BEHAVIOR_DEFAULT
:
341 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
343 * advance indicator of sequential behavior
347 if (sequential
< MAX_SEQUENTIAL_RUN
)
348 sequential
+= PAGE_SIZE
;
350 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
352 * advance indicator of sequential behavior
356 if (sequential
> -MAX_SEQUENTIAL_RUN
)
357 sequential
-= PAGE_SIZE
;
360 * reset indicator of sequential behavior
366 if (sequential
!= orig_sequential
) {
367 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
369 * if someone else has already updated object->sequential
370 * don't bother trying to update it or object->last_alloc
376 * I'd like to do this with a OSCompareAndSwap64, but that
377 * doesn't exist for PPC... however, it shouldn't matter
378 * that much... last_alloc is maintained so that we can determine
379 * if a sequential access pattern is taking place... if only
380 * one thread is banging on this object, no problem with the unprotected
381 * update... if 2 or more threads are banging away, we run the risk of
382 * someone seeing a mangled update... however, in the face of multiple
383 * accesses, no sequential access pattern can develop anyway, so we
384 * haven't lost any real info.
386 object
->last_alloc
= offset
;
390 int vm_page_deactivate_behind_count
= 0;
393 * vm_page_deactivate_behind
395 * Determine if sequential access is in progress
396 * in accordance with the behavior specified. If
397 * so, compute a potential page to deactivate and
400 * object must be locked.
402 * return TRUE if we actually deactivate a page
406 vm_fault_deactivate_behind(
408 vm_object_offset_t offset
,
409 vm_behavior_t behavior
)
412 int pages_in_run
= 0;
413 int max_pages_in_run
= 0;
415 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
416 vm_object_offset_t run_offset
= 0;
417 vm_object_offset_t pg_offset
= 0;
419 vm_page_t page_run
[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
];
423 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
426 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
428 * Do not deactivate pages from the kernel object: they
429 * are not intended to become pageable.
430 * or we've disabled the deactivate behind mechanism
434 if ((sequential_run
= object
->sequential
)) {
435 if (sequential_run
< 0) {
436 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
437 sequential_run
= 0 - sequential_run
;
439 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
443 case VM_BEHAVIOR_RANDOM
:
445 case VM_BEHAVIOR_SEQUENTIAL
:
446 if (sequential_run
>= (int)PAGE_SIZE
) {
447 run_offset
= 0 - PAGE_SIZE_64
;
448 max_pages_in_run
= 1;
451 case VM_BEHAVIOR_RSEQNTL
:
452 if (sequential_run
>= (int)PAGE_SIZE
) {
453 run_offset
= PAGE_SIZE_64
;
454 max_pages_in_run
= 1;
457 case VM_BEHAVIOR_DEFAULT
:
459 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
462 * determine if the run of sequential accesss has been
463 * long enough on an object with default access behavior
464 * to consider it for deactivation
466 if ((uint64_t)sequential_run
>= behind
&& (sequential_run
% (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
* PAGE_SIZE
)) == 0) {
468 * the comparisons between offset and behind are done
469 * in this kind of odd fashion in order to prevent wrap around
472 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
473 if (offset
>= behind
) {
474 run_offset
= 0 - behind
;
475 pg_offset
= PAGE_SIZE_64
;
476 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
479 if (offset
< -behind
) {
481 pg_offset
= 0 - PAGE_SIZE_64
;
482 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
489 for (n
= 0; n
< max_pages_in_run
; n
++) {
490 m
= vm_page_lookup(object
, offset
+ run_offset
+ (n
* pg_offset
));
492 if (m
&& !m
->laundry
&& !m
->busy
&& !m
->no_cache
&& !m
->throttled
&& !m
->fictitious
&& !m
->absent
) {
493 page_run
[pages_in_run
++] = m
;
494 pmap_clear_reference(m
->phys_page
);
498 vm_page_lockspin_queues();
500 for (n
= 0; n
< pages_in_run
; n
++) {
504 vm_page_deactivate_internal(m
, FALSE
);
506 vm_page_deactivate_behind_count
++;
508 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
511 vm_page_unlock_queues();
520 vm_page_throttled(void)
522 clock_sec_t elapsed_sec
;
524 clock_usec_t tv_usec
;
526 thread_t thread
= current_thread();
528 if (thread
->options
& TH_OPT_VMPRIV
)
531 thread
->t_page_creation_count
++;
533 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
534 return (HARD_THROTTLE_DELAY
);
536 if (vm_page_free_count
< vm_page_throttle_limit
&&
537 thread
->t_page_creation_count
> vm_page_creation_throttle
) {
539 clock_get_system_microtime(&tv_sec
, &tv_usec
);
541 elapsed_sec
= tv_sec
- thread
->t_page_creation_time
;
543 if (elapsed_sec
<= 6 || (thread
->t_page_creation_count
/ elapsed_sec
) >= (vm_page_creation_throttle
/ 6)) {
545 if (elapsed_sec
>= 60) {
547 * we'll reset our stats to give a well behaved app
548 * that was unlucky enough to accumulate a bunch of pages
549 * over a long period of time a chance to get out of
550 * the throttled state... we reset the counter and timestamp
551 * so that if it stays under the rate limit for the next second
552 * it will be back in our good graces... if it exceeds it, it
553 * will remain in the throttled state
555 thread
->t_page_creation_time
= tv_sec
;
556 thread
->t_page_creation_count
= (vm_page_creation_throttle
/ 6) * 5;
558 ++vm_page_throttle_count
;
560 return (SOFT_THROTTLE_DELAY
);
562 thread
->t_page_creation_time
= tv_sec
;
563 thread
->t_page_creation_count
= 0;
570 * check for various conditions that would
571 * prevent us from creating a ZF page...
572 * cleanup is based on being called from vm_fault_page
574 * object must be locked
575 * object == m->object
577 static vm_fault_return_t
578 vm_fault_check(vm_object_t object
, vm_page_t m
, vm_page_t first_m
, boolean_t interruptible_state
)
582 if (object
->shadow_severed
||
583 VM_OBJECT_PURGEABLE_FAULT_ERROR(object
)) {
586 * 1. the shadow chain was severed,
587 * 2. the purgeable object is volatile or empty and is marked
588 * to fault on access while volatile.
589 * Just have to return an error at this point
591 if (m
!= VM_PAGE_NULL
)
593 vm_fault_cleanup(object
, first_m
);
595 thread_interrupt_level(interruptible_state
);
597 return (VM_FAULT_MEMORY_ERROR
);
599 if (vm_backing_store_low
) {
601 * are we protecting the system from
602 * backing store exhaustion. If so
603 * sleep unless we are privileged.
605 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
607 if (m
!= VM_PAGE_NULL
)
609 vm_fault_cleanup(object
, first_m
);
611 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
613 thread_block(THREAD_CONTINUE_NULL
);
614 thread_interrupt_level(interruptible_state
);
616 return (VM_FAULT_RETRY
);
619 if ((throttle_delay
= vm_page_throttled())) {
621 * we're throttling zero-fills...
622 * treat this as if we couldn't grab a page
624 if (m
!= VM_PAGE_NULL
)
626 vm_fault_cleanup(object
, first_m
);
628 VM_DEBUG_EVENT(vmf_check_zfdelay
, VMF_CHECK_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
630 delay(throttle_delay
);
632 if (current_thread_aborted()) {
633 thread_interrupt_level(interruptible_state
);
634 return VM_FAULT_INTERRUPTED
;
636 thread_interrupt_level(interruptible_state
);
638 return (VM_FAULT_MEMORY_SHORTAGE
);
640 return (VM_FAULT_SUCCESS
);
645 * do the work to zero fill a page and
646 * inject it into the correct paging queue
648 * m->object must be locked
649 * page queue lock must NOT be held
652 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
654 int my_fault
= DBG_ZERO_FILL_FAULT
;
657 * This is is a zero-fill page fault...
659 * Checking the page lock is a waste of
660 * time; this page was absent, so
661 * it can't be page locked by a pager.
663 * we also consider it undefined
664 * with respect to instruction
665 * execution. i.e. it is the responsibility
666 * of higher layers to call for an instruction
667 * sync after changing the contents and before
668 * sending a program into this area. We
669 * choose this approach for performance
673 m
->cs_validated
= FALSE
;
674 m
->cs_tainted
= FALSE
;
676 if (no_zero_fill
== TRUE
) {
677 my_fault
= DBG_NZF_PAGE_FAULT
;
679 vm_page_zero_fill(m
);
681 VM_STAT_INCR(zero_fill_count
);
682 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
685 assert(m
->object
!= kernel_object
);
686 //assert(m->pageq.next == NULL && m->pageq.prev == NULL);
688 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) &&
689 (m
->object
->purgable
== VM_PURGABLE_DENY
||
690 m
->object
->purgable
== VM_PURGABLE_NONVOLATILE
||
691 m
->object
->purgable
== VM_PURGABLE_VOLATILE
)) {
693 vm_page_lockspin_queues();
695 assert(!VM_PAGE_WIRED(m
));
698 * can't be on the pageout queue since we don't
699 * have a pager to try and clean to
701 assert(!m
->pageout_queue
);
703 VM_PAGE_QUEUES_REMOVE(m
);
705 queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
707 vm_page_throttled_count
++;
709 vm_page_unlock_queues();
716 * Routine: vm_fault_page
718 * Find the resident page for the virtual memory
719 * specified by the given virtual memory object
721 * Additional arguments:
722 * The required permissions for the page is given
723 * in "fault_type". Desired permissions are included
725 * fault_info is passed along to determine pagein cluster
726 * limits... it contains the expected reference pattern,
727 * cluster size if available, etc...
729 * If the desired page is known to be resident (for
730 * example, because it was previously wired down), asserting
731 * the "unwiring" parameter will speed the search.
733 * If the operation can be interrupted (by thread_abort
734 * or thread_terminate), then the "interruptible"
735 * parameter should be asserted.
738 * The page containing the proper data is returned
742 * The source object must be locked and referenced,
743 * and must donate one paging reference. The reference
744 * is not affected. The paging reference and lock are
747 * If the call succeeds, the object in which "result_page"
748 * resides is left locked and holding a paging reference.
749 * If this is not the original object, a busy page in the
750 * original object is returned in "top_page", to prevent other
751 * callers from pursuing this same data, along with a paging
752 * reference for the original object. The "top_page" should
753 * be destroyed when this guarantee is no longer required.
754 * The "result_page" is also left busy. It is not removed
755 * from the pageout queues.
757 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
758 * fault succeeded but there's no VM page (i.e. the VM object
759 * does not actually hold VM pages, but device memory or
760 * large pages). The object is still locked and we still hold a
761 * paging_in_progress reference.
763 unsigned int vm_fault_page_blocked_access
= 0;
764 unsigned int vm_fault_page_forced_retry
= 0;
769 vm_object_t first_object
, /* Object to begin search */
770 vm_object_offset_t first_offset
, /* Offset into object */
771 vm_prot_t fault_type
, /* What access is requested */
772 boolean_t must_be_resident
,/* Must page be resident? */
773 /* Modifies in place: */
774 vm_prot_t
*protection
, /* Protection for mapping */
776 vm_page_t
*result_page
, /* Page found, if successful */
777 vm_page_t
*top_page
, /* Page in top object, if
778 * not result_page. */
779 int *type_of_fault
, /* if non-null, fill in with type of fault
780 * COW, zero-fill, etc... returned in trace point */
781 /* More arguments: */
782 kern_return_t
*error_code
, /* code if page is in error */
783 boolean_t no_zero_fill
, /* don't zero fill absent pages */
785 boolean_t data_supply
, /* treat as data_supply if
786 * it is a write fault and a full
787 * page is provided */
789 __unused boolean_t data_supply
,
791 vm_object_fault_info_t fault_info
)
795 vm_object_offset_t offset
;
797 vm_object_t next_object
;
798 vm_object_t copy_object
;
799 boolean_t look_for_page
;
800 boolean_t force_fault_retry
= FALSE
;
801 vm_prot_t access_required
= fault_type
;
802 vm_prot_t wants_copy_flag
;
803 CLUSTER_STAT(int pages_at_higher_offsets
;)
804 CLUSTER_STAT(int pages_at_lower_offsets
;)
805 kern_return_t wait_result
;
806 boolean_t interruptible_state
;
807 boolean_t data_already_requested
= FALSE
;
808 vm_behavior_t orig_behavior
;
809 vm_size_t orig_cluster_size
;
810 vm_fault_return_t error
;
812 uint32_t try_failed_count
;
813 int interruptible
; /* how may fault be interrupted? */
814 memory_object_t pager
;
815 vm_fault_return_t retval
;
818 * MACH page map - an optional optimization where a bit map is maintained
819 * by the VM subsystem for internal objects to indicate which pages of
820 * the object currently reside on backing store. This existence map
821 * duplicates information maintained by the vnode pager. It is
822 * created at the time of the first pageout against the object, i.e.
823 * at the same time pager for the object is created. The optimization
824 * is designed to eliminate pager interaction overhead, if it is
825 * 'known' that the page does not exist on backing store.
827 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
828 * either marked as paged out in the existence map for the object or no
829 * existence map exists for the object. MUST_ASK_PAGER() is one of the
830 * criteria in the decision to invoke the pager. It is also used as one
831 * of the criteria to terminate the scan for adjacent pages in a clustered
832 * pagein operation. Note that MUST_ASK_PAGER() always evaluates to TRUE for
833 * permanent objects. Note also that if the pager for an internal object
834 * has not been created, the pager is not invoked regardless of the value
835 * of MUST_ASK_PAGER() and that clustered pagein scans are only done on an object
836 * for which a pager has been created.
838 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
839 * is marked as paged out in the existence map for the object. PAGED_OUT()
840 * PAGED_OUT() is used to determine if a page has already been pushed
841 * into a copy object in order to avoid a redundant page out operation.
844 #define MUST_ASK_PAGER(o, f) (vm_external_state_get((o)->existence_map, (f)) \
845 != VM_EXTERNAL_STATE_ABSENT)
846 #define PAGED_OUT(o, f) (vm_external_state_get((o)->existence_map, (f)) \
847 == VM_EXTERNAL_STATE_EXISTS)
849 #define MUST_ASK_PAGER(o, f) (TRUE)
850 #define PAGED_OUT(o, f) (FALSE)
856 #define RELEASE_PAGE(m) \
858 PAGE_WAKEUP_DONE(m); \
859 if (!m->active && !m->inactive && !m->throttled) { \
860 vm_page_lockspin_queues(); \
861 if (!m->active && !m->inactive && !m->throttled) \
862 vm_page_activate(m); \
863 vm_page_unlock_queues(); \
868 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
871 interruptible
= fault_info
->interruptible
;
872 interruptible_state
= thread_interrupt_level(interruptible
);
875 * INVARIANTS (through entire routine):
877 * 1) At all times, we must either have the object
878 * lock or a busy page in some object to prevent
879 * some other thread from trying to bring in
882 * Note that we cannot hold any locks during the
883 * pager access or when waiting for memory, so
884 * we use a busy page then.
886 * 2) To prevent another thread from racing us down the
887 * shadow chain and entering a new page in the top
888 * object before we do, we must keep a busy page in
889 * the top object while following the shadow chain.
891 * 3) We must increment paging_in_progress on any object
892 * for which we have a busy page before dropping
895 * 4) We leave busy pages on the pageout queues.
896 * If the pageout daemon comes across a busy page,
897 * it will remove the page from the pageout queues.
900 object
= first_object
;
901 offset
= first_offset
;
902 first_m
= VM_PAGE_NULL
;
903 access_required
= fault_type
;
907 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
908 object
, offset
, fault_type
, *protection
, 0);
911 * default type of fault
913 my_fault
= DBG_CACHE_HIT_FAULT
;
917 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
919 if (!object
->alive
) {
921 * object is no longer valid
922 * clean up and return error
924 vm_fault_cleanup(object
, first_m
);
925 thread_interrupt_level(interruptible_state
);
927 return (VM_FAULT_MEMORY_ERROR
);
930 if (!object
->pager_created
&& object
->phys_contiguous
) {
932 * A physically-contiguous object without a pager:
933 * must be a "large page" object. We do not deal
934 * with VM pages for this object.
937 goto phys_contig_object
;
940 if (object
->blocked_access
) {
942 * Access to this VM object has been blocked.
943 * Replace our "paging_in_progress" reference with
944 * a "activity_in_progress" reference and wait for
945 * access to be unblocked.
947 vm_object_activity_begin(object
);
948 vm_object_paging_end(object
);
949 while (object
->blocked_access
) {
950 vm_object_sleep(object
,
951 VM_OBJECT_EVENT_UNBLOCKED
,
954 vm_fault_page_blocked_access
++;
955 vm_object_paging_begin(object
);
956 vm_object_activity_end(object
);
960 * See whether the page at 'offset' is resident
962 m
= vm_page_lookup(object
, offset
);
964 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
966 if (m
!= VM_PAGE_NULL
) {
970 * The page is being brought in,
971 * wait for it and then retry.
974 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
976 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
979 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
982 counter(c_vm_fault_page_block_busy_kernel
++);
984 if (wait_result
!= THREAD_AWAKENED
) {
985 vm_fault_cleanup(object
, first_m
);
986 thread_interrupt_level(interruptible_state
);
988 if (wait_result
== THREAD_RESTART
)
989 return (VM_FAULT_RETRY
);
991 return (VM_FAULT_INTERRUPTED
);
999 vm_pageout_steal_laundry(m
, FALSE
);
1001 if (m
->phys_page
== vm_page_guard_addr
) {
1003 * Guard page: off limits !
1005 if (fault_type
== VM_PROT_NONE
) {
1007 * The fault is not requesting any
1008 * access to the guard page, so it must
1009 * be just to wire or unwire it.
1010 * Let's pretend it succeeded...
1014 assert(first_m
== VM_PAGE_NULL
);
1015 *top_page
= first_m
;
1017 *type_of_fault
= DBG_GUARD_FAULT
;
1018 return VM_FAULT_SUCCESS
;
1021 * The fault requests access to the
1022 * guard page: let's deny that !
1024 vm_fault_cleanup(object
, first_m
);
1025 thread_interrupt_level(interruptible_state
);
1026 return VM_FAULT_MEMORY_ERROR
;
1032 * The page is in error, give up now.
1035 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1038 *error_code
= KERN_MEMORY_ERROR
;
1041 vm_fault_cleanup(object
, first_m
);
1042 thread_interrupt_level(interruptible_state
);
1044 return (VM_FAULT_MEMORY_ERROR
);
1048 * The pager wants us to restart
1049 * at the top of the chain,
1050 * typically because it has moved the
1051 * page to another pager, then do so.
1054 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1058 vm_fault_cleanup(object
, first_m
);
1059 thread_interrupt_level(interruptible_state
);
1061 return (VM_FAULT_RETRY
);
1065 * The page isn't busy, but is absent,
1066 * therefore it's deemed "unavailable".
1068 * Remove the non-existent page (unless it's
1069 * in the top object) and move on down to the
1070 * next object (if there is one).
1073 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1075 next_object
= object
->shadow
;
1077 if (next_object
== VM_OBJECT_NULL
) {
1079 * Absent page at bottom of shadow
1080 * chain; zero fill the page we left
1081 * busy in the first object, and free
1084 assert(!must_be_resident
);
1087 * check for any conditions that prevent
1088 * us from creating a new zero-fill page
1089 * vm_fault_check will do all of the
1090 * fault cleanup in the case of an error condition
1091 * including resetting the thread_interrupt_level
1093 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
1095 if (error
!= VM_FAULT_SUCCESS
)
1099 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
1104 if (object
!= first_object
) {
1106 * free the absent page we just found
1111 * drop reference and lock on current object
1113 vm_object_paging_end(object
);
1114 vm_object_unlock(object
);
1117 * grab the original page we
1118 * 'soldered' in place and
1119 * retake lock on 'first_object'
1122 first_m
= VM_PAGE_NULL
;
1124 object
= first_object
;
1125 offset
= first_offset
;
1127 vm_object_lock(object
);
1130 * we're going to use the absent page we just found
1131 * so convert it to a 'busy' page
1137 * zero-fill the page and put it on
1138 * the correct paging queue
1140 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1142 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1147 if (must_be_resident
)
1148 vm_object_paging_end(object
);
1149 else if (object
!= first_object
) {
1150 vm_object_paging_end(object
);
1157 vm_page_lockspin_queues();
1159 assert(!m
->pageout_queue
);
1160 VM_PAGE_QUEUES_REMOVE(m
);
1162 vm_page_unlock_queues();
1165 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1168 offset
+object
->vo_shadow_offset
,0);
1170 offset
+= object
->vo_shadow_offset
;
1171 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1172 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1173 access_required
= VM_PROT_READ
;
1175 vm_object_lock(next_object
);
1176 vm_object_unlock(object
);
1177 object
= next_object
;
1178 vm_object_paging_begin(object
);
1181 * reset to default type of fault
1183 my_fault
= DBG_CACHE_HIT_FAULT
;
1189 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1190 && (fault_type
& VM_PROT_WRITE
)) {
1192 * This is a copy-on-write fault that will
1193 * cause us to revoke access to this page, but
1194 * this page is in the process of being cleaned
1195 * in a clustered pageout. We must wait until
1196 * the cleaning operation completes before
1197 * revoking access to the original page,
1198 * otherwise we might attempt to remove a
1202 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1205 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1209 * take an extra ref so that object won't die
1211 vm_object_reference_locked(object
);
1213 vm_fault_cleanup(object
, first_m
);
1215 counter(c_vm_fault_page_block_backoff_kernel
++);
1216 vm_object_lock(object
);
1217 assert(object
->ref_count
> 0);
1219 m
= vm_page_lookup(object
, offset
);
1221 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
1222 PAGE_ASSERT_WAIT(m
, interruptible
);
1224 vm_object_unlock(object
);
1225 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1226 vm_object_deallocate(object
);
1230 vm_object_unlock(object
);
1232 vm_object_deallocate(object
);
1233 thread_interrupt_level(interruptible_state
);
1235 return (VM_FAULT_RETRY
);
1238 if (type_of_fault
== NULL
&& m
->speculative
&&
1239 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1241 * If we were passed a non-NULL pointer for
1242 * "type_of_fault", than we came from
1243 * vm_fault... we'll let it deal with
1244 * this condition, since it
1245 * needs to see m->speculative to correctly
1246 * account the pageins, otherwise...
1247 * take it off the speculative queue, we'll
1248 * let the caller of vm_fault_page deal
1249 * with getting it onto the correct queue
1251 * If the caller specified in fault_info that
1252 * it wants a "stealth" fault, we also leave
1253 * the page in the speculative queue.
1255 vm_page_lockspin_queues();
1257 VM_PAGE_QUEUES_REMOVE(m
);
1258 vm_page_unlock_queues();
1264 * the user needs access to a page that we
1265 * encrypted before paging it out.
1266 * Decrypt the page now.
1267 * Keep it busy to prevent anyone from
1268 * accessing it during the decryption.
1271 vm_page_decrypt(m
, 0);
1272 assert(object
== m
->object
);
1274 PAGE_WAKEUP_DONE(m
);
1277 * Retry from the top, in case
1278 * something changed while we were
1283 ASSERT_PAGE_DECRYPTED(m
);
1285 if (m
->object
->code_signed
) {
1288 * We just paged in a page from a signed
1289 * memory object but we don't need to
1290 * validate it now. We'll validate it if
1291 * when it gets mapped into a user address
1292 * space for the first time or when the page
1293 * gets copied to another object as a result
1294 * of a copy-on-write.
1299 * We mark the page busy and leave it on
1300 * the pageout queues. If the pageout
1301 * deamon comes across it, then it will
1302 * remove the page from the queue, but not the object
1305 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1308 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1309 object
, offset
, m
, 0, 0);
1319 * we get here when there is no page present in the object at
1320 * the offset we're interested in... we'll allocate a page
1321 * at this point if the pager associated with
1322 * this object can provide the data or we're the top object...
1323 * object is locked; m == NULL
1325 if (must_be_resident
)
1326 goto dont_look_for_page
;
1328 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
) == TRUE
) && !data_supply
);
1331 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1333 if (!look_for_page
&& object
== first_object
&& !object
->phys_contiguous
) {
1335 * Allocate a new page for this object/offset pair as a placeholder
1339 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1341 if (m
== VM_PAGE_NULL
) {
1343 vm_fault_cleanup(object
, first_m
);
1344 thread_interrupt_level(interruptible_state
);
1346 return (VM_FAULT_MEMORY_SHORTAGE
);
1349 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1350 vm_page_insert_internal(m
, object
, offset
, FALSE
, TRUE
, TRUE
);
1352 vm_page_insert(m
, object
, offset
);
1355 if (look_for_page
) {
1359 * If the memory manager is not ready, we
1360 * cannot make requests.
1362 if (!object
->pager_ready
) {
1364 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1366 if (m
!= VM_PAGE_NULL
)
1370 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1371 object
, offset
, 0, 0, 0);
1374 * take an extra ref so object won't die
1376 vm_object_reference_locked(object
);
1377 vm_fault_cleanup(object
, first_m
);
1378 counter(c_vm_fault_page_block_backoff_kernel
++);
1380 vm_object_lock(object
);
1381 assert(object
->ref_count
> 0);
1383 if (!object
->pager_ready
) {
1384 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1386 vm_object_unlock(object
);
1387 if (wait_result
== THREAD_WAITING
)
1388 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1389 vm_object_deallocate(object
);
1393 vm_object_unlock(object
);
1394 vm_object_deallocate(object
);
1395 thread_interrupt_level(interruptible_state
);
1397 return (VM_FAULT_RETRY
);
1400 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1402 * If there are too many outstanding page
1403 * requests pending on this external object, we
1404 * wait for them to be resolved now.
1407 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1409 if (m
!= VM_PAGE_NULL
)
1412 * take an extra ref so object won't die
1414 vm_object_reference_locked(object
);
1416 vm_fault_cleanup(object
, first_m
);
1418 counter(c_vm_fault_page_block_backoff_kernel
++);
1420 vm_object_lock(object
);
1421 assert(object
->ref_count
> 0);
1423 if (object
->paging_in_progress
>= vm_object_pagein_throttle
) {
1424 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS
, interruptible
);
1426 vm_object_unlock(object
);
1427 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1428 vm_object_deallocate(object
);
1432 vm_object_unlock(object
);
1433 vm_object_deallocate(object
);
1434 thread_interrupt_level(interruptible_state
);
1436 return (VM_FAULT_RETRY
);
1439 if (m
!= VM_PAGE_NULL
) {
1445 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1449 * It's possible someone called vm_object_destroy while we weren't
1450 * holding the object lock. If that has happened, then bail out
1454 pager
= object
->pager
;
1456 if (pager
== MEMORY_OBJECT_NULL
) {
1457 vm_fault_cleanup(object
, first_m
);
1458 thread_interrupt_level(interruptible_state
);
1459 return VM_FAULT_MEMORY_ERROR
;
1463 * We have an absent page in place for the faulting offset,
1464 * so we can release the object lock.
1467 vm_object_unlock(object
);
1470 * If this object uses a copy_call strategy,
1471 * and we are interested in a copy of this object
1472 * (having gotten here only by following a
1473 * shadow chain), then tell the memory manager
1474 * via a flag added to the desired_access
1475 * parameter, so that it can detect a race
1476 * between our walking down the shadow chain
1477 * and its pushing pages up into a copy of
1478 * the object that it manages.
1480 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
)
1481 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1483 wants_copy_flag
= VM_PROT_NONE
;
1486 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1488 access_required
| wants_copy_flag
, 0);
1490 if (object
->copy
== first_object
) {
1492 * if we issue the memory_object_data_request in
1493 * this state, we are subject to a deadlock with
1494 * the underlying filesystem if it is trying to
1495 * shrink the file resulting in a push of pages
1496 * into the copy object... that push will stall
1497 * on the placeholder page, and if the pushing thread
1498 * is holding a lock that is required on the pagein
1499 * path (such as a truncate lock), we'll deadlock...
1500 * to avoid this potential deadlock, we throw away
1501 * our placeholder page before calling memory_object_data_request
1502 * and force this thread to retry the vm_fault_page after
1503 * we have issued the I/O. the second time through this path
1504 * we will find the page already in the cache (presumably still
1505 * busy waiting for the I/O to complete) and then complete
1506 * the fault w/o having to go through memory_object_data_request again
1508 assert(first_m
!= VM_PAGE_NULL
);
1509 assert(first_m
->object
== first_object
);
1511 vm_object_lock(first_object
);
1512 VM_PAGE_FREE(first_m
);
1513 vm_object_paging_end(first_object
);
1514 vm_object_unlock(first_object
);
1516 first_m
= VM_PAGE_NULL
;
1517 force_fault_retry
= TRUE
;
1519 vm_fault_page_forced_retry
++;
1522 if (data_already_requested
== TRUE
) {
1523 orig_behavior
= fault_info
->behavior
;
1524 orig_cluster_size
= fault_info
->cluster_size
;
1526 fault_info
->behavior
= VM_BEHAVIOR_RANDOM
;
1527 fault_info
->cluster_size
= PAGE_SIZE
;
1530 * Call the memory manager to retrieve the data.
1532 rc
= memory_object_data_request(
1534 offset
+ object
->paging_offset
,
1536 access_required
| wants_copy_flag
,
1537 (memory_object_fault_info_t
)fault_info
);
1539 if (data_already_requested
== TRUE
) {
1540 fault_info
->behavior
= orig_behavior
;
1541 fault_info
->cluster_size
= orig_cluster_size
;
1543 data_already_requested
= TRUE
;
1546 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1548 vm_object_lock(object
);
1550 if (rc
!= KERN_SUCCESS
) {
1552 vm_fault_cleanup(object
, first_m
);
1553 thread_interrupt_level(interruptible_state
);
1555 return ((rc
== MACH_SEND_INTERRUPTED
) ?
1556 VM_FAULT_INTERRUPTED
:
1557 VM_FAULT_MEMORY_ERROR
);
1560 clock_usec_t tv_usec
;
1562 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1563 current_thread()->t_page_creation_time
= tv_sec
;
1564 current_thread()->t_page_creation_count
= 0;
1566 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_flags
& TH_SFLAG_ABORT
)) {
1568 vm_fault_cleanup(object
, first_m
);
1569 thread_interrupt_level(interruptible_state
);
1571 return (VM_FAULT_INTERRUPTED
);
1573 if (force_fault_retry
== TRUE
) {
1575 vm_fault_cleanup(object
, first_m
);
1576 thread_interrupt_level(interruptible_state
);
1578 return (VM_FAULT_RETRY
);
1580 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1582 * No page here means that the object we
1583 * initially looked up was "physically
1584 * contiguous" (i.e. device memory). However,
1585 * with Virtual VRAM, the object might not
1586 * be backed by that device memory anymore,
1587 * so we're done here only if the object is
1588 * still "phys_contiguous".
1589 * Otherwise, if the object is no longer
1590 * "phys_contiguous", we need to retry the
1591 * page fault against the object's new backing
1592 * store (different memory object).
1598 * potentially a pagein fault
1599 * if we make it through the state checks
1600 * above, than we'll count it as such
1602 my_fault
= DBG_PAGEIN_FAULT
;
1605 * Retry with same object/offset, since new data may
1606 * be in a different page (i.e., m is meaningless at
1613 * We get here if the object has no pager, or an existence map
1614 * exists and indicates the page isn't present on the pager
1615 * or we're unwiring a page. If a pager exists, but there
1616 * is no existence map, then the m->absent case above handles
1617 * the ZF case when the pager can't provide the page
1620 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1622 if (object
== first_object
)
1625 assert(m
== VM_PAGE_NULL
);
1628 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1632 next_object
= object
->shadow
;
1634 if (next_object
== VM_OBJECT_NULL
) {
1636 * we've hit the bottom of the shadown chain,
1637 * fill the page in the top object with zeros.
1639 assert(!must_be_resident
);
1641 if (object
!= first_object
) {
1642 vm_object_paging_end(object
);
1643 vm_object_unlock(object
);
1645 object
= first_object
;
1646 offset
= first_offset
;
1647 vm_object_lock(object
);
1650 assert(m
->object
== object
);
1651 first_m
= VM_PAGE_NULL
;
1654 * check for any conditions that prevent
1655 * us from creating a new zero-fill page
1656 * vm_fault_check will do all of the
1657 * fault cleanup in the case of an error condition
1658 * including resetting the thread_interrupt_level
1660 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
1662 if (error
!= VM_FAULT_SUCCESS
)
1665 if (m
== VM_PAGE_NULL
) {
1668 if (m
== VM_PAGE_NULL
) {
1669 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1670 thread_interrupt_level(interruptible_state
);
1672 return (VM_FAULT_MEMORY_SHORTAGE
);
1674 vm_page_insert(m
, object
, offset
);
1676 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1678 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1684 * Move on to the next object. Lock the next
1685 * object before unlocking the current one.
1687 if ((object
!= first_object
) || must_be_resident
)
1688 vm_object_paging_end(object
);
1690 offset
+= object
->vo_shadow_offset
;
1691 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1692 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1693 access_required
= VM_PROT_READ
;
1695 vm_object_lock(next_object
);
1696 vm_object_unlock(object
);
1698 object
= next_object
;
1699 vm_object_paging_begin(object
);
1704 * PAGE HAS BEEN FOUND.
1707 * busy, so that we can play with it;
1708 * not absent, so that nobody else will fill it;
1709 * possibly eligible for pageout;
1711 * The top-level page (first_m) is:
1712 * VM_PAGE_NULL if the page was found in the
1714 * busy, not absent, and ineligible for pageout.
1716 * The current object (object) is locked. A paging
1717 * reference is held for the current and top-level
1722 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1724 #if EXTRA_ASSERTIONS
1725 assert(m
->busy
&& !m
->absent
);
1726 assert((first_m
== VM_PAGE_NULL
) ||
1727 (first_m
->busy
&& !first_m
->absent
&&
1728 !first_m
->active
&& !first_m
->inactive
));
1729 #endif /* EXTRA_ASSERTIONS */
1733 * If we found a page, we must have decrypted it before we
1736 ASSERT_PAGE_DECRYPTED(m
);
1739 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1741 first_object
, first_m
);
1744 * If the page is being written, but isn't
1745 * already owned by the top-level object,
1746 * we have to copy it into a new page owned
1747 * by the top-level object.
1749 if (object
!= first_object
) {
1752 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1754 if (fault_type
& VM_PROT_WRITE
) {
1758 * We only really need to copy if we
1761 assert(!must_be_resident
);
1764 * are we protecting the system from
1765 * backing store exhaustion. If so
1766 * sleep unless we are privileged.
1768 if (vm_backing_store_low
) {
1769 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1772 vm_fault_cleanup(object
, first_m
);
1774 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1776 thread_block(THREAD_CONTINUE_NULL
);
1777 thread_interrupt_level(interruptible_state
);
1779 return (VM_FAULT_RETRY
);
1783 * If we try to collapse first_object at this
1784 * point, we may deadlock when we try to get
1785 * the lock on an intermediate object (since we
1786 * have the bottom object locked). We can't
1787 * unlock the bottom object, because the page
1788 * we found may move (by collapse) if we do.
1790 * Instead, we first copy the page. Then, when
1791 * we have no more use for the bottom object,
1792 * we unlock it and try to collapse.
1794 * Note that we copy the page even if we didn't
1795 * need to... that's the breaks.
1799 * Allocate a page for the copy
1801 copy_m
= vm_page_grab();
1803 if (copy_m
== VM_PAGE_NULL
) {
1806 vm_fault_cleanup(object
, first_m
);
1807 thread_interrupt_level(interruptible_state
);
1809 return (VM_FAULT_MEMORY_SHORTAGE
);
1812 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
1816 vm_page_copy(m
, copy_m
);
1819 * If another map is truly sharing this
1820 * page with us, we have to flush all
1821 * uses of the original page, since we
1822 * can't distinguish those which want the
1823 * original from those which need the
1826 * XXXO If we know that only one map has
1827 * access to this page, then we could
1828 * avoid the pmap_disconnect() call.
1831 pmap_disconnect(m
->phys_page
);
1833 assert(!m
->cleaning
);
1836 * We no longer need the old page or object.
1838 PAGE_WAKEUP_DONE(m
);
1839 vm_object_paging_end(object
);
1840 vm_object_unlock(object
);
1842 my_fault
= DBG_COW_FAULT
;
1843 VM_STAT_INCR(cow_faults
);
1844 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
1845 current_task()->cow_faults
++;
1847 object
= first_object
;
1848 offset
= first_offset
;
1850 vm_object_lock(object
);
1852 * get rid of the place holder
1853 * page that we soldered in earlier
1855 VM_PAGE_FREE(first_m
);
1856 first_m
= VM_PAGE_NULL
;
1859 * and replace it with the
1860 * page we just copied into
1862 assert(copy_m
->busy
);
1863 vm_page_insert(copy_m
, object
, offset
);
1864 SET_PAGE_DIRTY(copy_m
, TRUE
);
1868 * Now that we've gotten the copy out of the
1869 * way, let's try to collapse the top object.
1870 * But we have to play ugly games with
1871 * paging_in_progress to do that...
1873 vm_object_paging_end(object
);
1874 vm_object_collapse(object
, offset
, TRUE
);
1875 vm_object_paging_begin(object
);
1878 *protection
&= (~VM_PROT_WRITE
);
1881 * Now check whether the page needs to be pushed into the
1882 * copy object. The use of asymmetric copy on write for
1883 * shared temporary objects means that we may do two copies to
1884 * satisfy the fault; one above to get the page from a
1885 * shadowed object, and one here to push it into the copy.
1887 try_failed_count
= 0;
1889 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
1890 vm_object_offset_t copy_offset
;
1894 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1897 * If the page is being written, but hasn't been
1898 * copied to the copy-object, we have to copy it there.
1900 if ((fault_type
& VM_PROT_WRITE
) == 0) {
1901 *protection
&= ~VM_PROT_WRITE
;
1906 * If the page was guaranteed to be resident,
1907 * we must have already performed the copy.
1909 if (must_be_resident
)
1913 * Try to get the lock on the copy_object.
1915 if (!vm_object_lock_try(copy_object
)) {
1917 vm_object_unlock(object
);
1920 mutex_pause(try_failed_count
); /* wait a bit */
1921 vm_object_lock(object
);
1925 try_failed_count
= 0;
1928 * Make another reference to the copy-object,
1929 * to keep it from disappearing during the
1932 vm_object_reference_locked(copy_object
);
1935 * Does the page exist in the copy?
1937 copy_offset
= first_offset
- copy_object
->vo_shadow_offset
;
1939 if (copy_object
->vo_size
<= copy_offset
)
1941 * Copy object doesn't cover this page -- do nothing.
1944 else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
1946 * Page currently exists in the copy object
1950 * If the page is being brought
1951 * in, wait for it and then retry.
1956 * take an extra ref so object won't die
1958 vm_object_reference_locked(copy_object
);
1959 vm_object_unlock(copy_object
);
1960 vm_fault_cleanup(object
, first_m
);
1961 counter(c_vm_fault_page_block_backoff_kernel
++);
1963 vm_object_lock(copy_object
);
1964 assert(copy_object
->ref_count
> 0);
1965 VM_OBJ_RES_DECR(copy_object
);
1966 vm_object_lock_assert_exclusive(copy_object
);
1967 copy_object
->ref_count
--;
1968 assert(copy_object
->ref_count
> 0);
1969 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
1972 * it's OK if the "copy_m" page is encrypted,
1973 * because we're not moving it nor handling its
1976 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
1977 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
1979 vm_object_unlock(copy_object
);
1980 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1981 vm_object_deallocate(copy_object
);
1985 vm_object_unlock(copy_object
);
1986 vm_object_deallocate(copy_object
);
1987 thread_interrupt_level(interruptible_state
);
1989 return (VM_FAULT_RETRY
);
1993 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
1995 * If PAGED_OUT is TRUE, then the page used to exist
1996 * in the copy-object, and has already been paged out.
1997 * We don't need to repeat this. If PAGED_OUT is
1998 * FALSE, then either we don't know (!pager_created,
1999 * for example) or it hasn't been paged out.
2000 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2001 * We must copy the page to the copy object.
2004 if (vm_backing_store_low
) {
2006 * we are protecting the system from
2007 * backing store exhaustion. If so
2008 * sleep unless we are privileged.
2010 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
2011 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
2014 VM_OBJ_RES_DECR(copy_object
);
2015 vm_object_lock_assert_exclusive(copy_object
);
2016 copy_object
->ref_count
--;
2017 assert(copy_object
->ref_count
> 0);
2019 vm_object_unlock(copy_object
);
2020 vm_fault_cleanup(object
, first_m
);
2021 thread_block(THREAD_CONTINUE_NULL
);
2022 thread_interrupt_level(interruptible_state
);
2024 return (VM_FAULT_RETRY
);
2028 * Allocate a page for the copy
2030 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
2032 if (copy_m
== VM_PAGE_NULL
) {
2035 VM_OBJ_RES_DECR(copy_object
);
2036 vm_object_lock_assert_exclusive(copy_object
);
2037 copy_object
->ref_count
--;
2038 assert(copy_object
->ref_count
> 0);
2040 vm_object_unlock(copy_object
);
2041 vm_fault_cleanup(object
, first_m
);
2042 thread_interrupt_level(interruptible_state
);
2044 return (VM_FAULT_MEMORY_SHORTAGE
);
2047 * Must copy page into copy-object.
2049 vm_page_copy(m
, copy_m
);
2052 * If the old page was in use by any users
2053 * of the copy-object, it must be removed
2054 * from all pmaps. (We can't know which
2058 pmap_disconnect(m
->phys_page
);
2061 * If there's a pager, then immediately
2062 * page out this page, using the "initialize"
2063 * option. Else, we use the copy.
2065 if ((!copy_object
->pager_created
)
2067 || vm_external_state_get(copy_object
->existence_map
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2071 vm_page_lockspin_queues();
2072 assert(!m
->cleaning
);
2073 vm_page_activate(copy_m
);
2074 vm_page_unlock_queues();
2076 SET_PAGE_DIRTY(copy_m
, TRUE
);
2077 PAGE_WAKEUP_DONE(copy_m
);
2079 } else if (copy_object
->internal
) {
2081 * For internal objects check with the pager to see
2082 * if the page already exists in the backing store.
2083 * If yes, then we can drop the copy page. If not,
2084 * then we'll activate it, mark it dirty and keep it
2088 kern_return_t kr
= KERN_SUCCESS
;
2090 memory_object_t copy_pager
= copy_object
->pager
;
2091 assert(copy_pager
!= MEMORY_OBJECT_NULL
);
2092 vm_object_paging_begin(copy_object
);
2094 vm_object_unlock(copy_object
);
2096 kr
= memory_object_data_request(
2098 copy_offset
+ copy_object
->paging_offset
,
2099 0, /* Only query the pager. */
2103 vm_object_lock(copy_object
);
2105 vm_object_paging_end(copy_object
);
2108 * Since we dropped the copy_object's lock,
2109 * check whether we'll have to deallocate
2112 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2113 vm_object_unlock(copy_object
);
2114 vm_object_deallocate(copy_object
);
2115 vm_object_lock(object
);
2119 if (kr
== KERN_SUCCESS
) {
2121 * The pager has the page. We don't want to overwrite
2122 * that page by sending this one out to the backing store.
2123 * So we drop the copy page.
2125 VM_PAGE_FREE(copy_m
);
2129 * The pager doesn't have the page. We'll keep this one
2130 * around in the copy object. It might get sent out to
2131 * the backing store under memory pressure.
2133 vm_page_lockspin_queues();
2134 assert(!m
->cleaning
);
2135 vm_page_activate(copy_m
);
2136 vm_page_unlock_queues();
2138 SET_PAGE_DIRTY(copy_m
, TRUE
);
2139 PAGE_WAKEUP_DONE(copy_m
);
2143 assert(copy_m
->busy
== TRUE
);
2144 assert(!m
->cleaning
);
2147 * dirty is protected by the object lock
2149 SET_PAGE_DIRTY(copy_m
, TRUE
);
2152 * The page is already ready for pageout:
2153 * not on pageout queues and busy.
2154 * Unlock everything except the
2155 * copy_object itself.
2157 vm_object_unlock(object
);
2160 * Write the page to the copy-object,
2161 * flushing it from the kernel.
2163 vm_pageout_initialize_page(copy_m
);
2166 * Since the pageout may have
2167 * temporarily dropped the
2168 * copy_object's lock, we
2169 * check whether we'll have
2170 * to deallocate the hard way.
2172 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2173 vm_object_unlock(copy_object
);
2174 vm_object_deallocate(copy_object
);
2175 vm_object_lock(object
);
2180 * Pick back up the old object's
2181 * lock. [It is safe to do so,
2182 * since it must be deeper in the
2185 vm_object_lock(object
);
2189 * Because we're pushing a page upward
2190 * in the object tree, we must restart
2191 * any faults that are waiting here.
2192 * [Note that this is an expansion of
2193 * PAGE_WAKEUP that uses the THREAD_RESTART
2194 * wait result]. Can't turn off the page's
2195 * busy bit because we're not done with it.
2199 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2203 * The reference count on copy_object must be
2204 * at least 2: one for our extra reference,
2205 * and at least one from the outside world
2206 * (we checked that when we last locked
2209 vm_object_lock_assert_exclusive(copy_object
);
2210 copy_object
->ref_count
--;
2211 assert(copy_object
->ref_count
> 0);
2213 VM_OBJ_RES_DECR(copy_object
);
2214 vm_object_unlock(copy_object
);
2221 *top_page
= first_m
;
2224 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2225 object
, offset
, m
, first_m
, 0);
2227 if (m
!= VM_PAGE_NULL
) {
2228 retval
= VM_FAULT_SUCCESS
;
2229 if (my_fault
== DBG_PAGEIN_FAULT
) {
2231 VM_STAT_INCR(pageins
);
2232 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
2233 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
2234 current_task()->pageins
++;
2236 if (m
->object
->internal
) {
2237 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
2238 my_fault
= DBG_PAGEIND_FAULT
;
2240 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
2241 my_fault
= DBG_PAGEINV_FAULT
;
2245 * evaluate access pattern and update state
2246 * vm_fault_deactivate_behind depends on the
2247 * state being up to date
2249 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2251 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2254 *type_of_fault
= my_fault
;
2256 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2257 assert(first_m
== VM_PAGE_NULL
);
2258 assert(object
== first_object
);
2261 thread_interrupt_level(interruptible_state
);
2264 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2269 thread_interrupt_level(interruptible_state
);
2271 if (wait_result
== THREAD_INTERRUPTED
)
2272 return (VM_FAULT_INTERRUPTED
);
2273 return (VM_FAULT_RETRY
);
2282 * When soft faulting a page, we have to validate the page if:
2283 * 1. the page is being mapped in user space
2284 * 2. the page hasn't already been found to be "tainted"
2285 * 3. the page belongs to a code-signed object
2286 * 4. the page has not been validated yet or has been mapped for write.
2288 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page) \
2289 ((pmap) != kernel_pmap /*1*/ && \
2290 !(page)->cs_tainted /*2*/ && \
2291 (page)->object->code_signed /*3*/ && \
2292 (!(page)->cs_validated || (page)->wpmapped /*4*/))
2296 * page queue lock must NOT be held
2297 * m->object must be locked
2299 * NOTE: m->object could be locked "shared" only if we are called
2300 * from vm_fault() as part of a soft fault. If so, we must be
2301 * careful not to modify the VM object in any way that is not
2302 * legal under a shared lock...
2304 unsigned long cs_enter_tainted_rejected
= 0;
2305 unsigned long cs_enter_tainted_accepted
= 0;
2307 vm_fault_enter(vm_page_t m
,
2309 vm_map_offset_t vaddr
,
2311 vm_prot_t fault_type
,
2313 boolean_t change_wiring
,
2315 boolean_t cs_bypass
,
2316 boolean_t
*need_retry
,
2319 kern_return_t kr
, pe_result
;
2320 boolean_t previously_pmapped
= m
->pmapped
;
2321 boolean_t must_disconnect
= 0;
2322 boolean_t map_is_switched
, map_is_switch_protected
;
2324 vm_object_lock_assert_held(m
->object
);
2326 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2329 if (m
->phys_page
== vm_page_guard_addr
) {
2330 assert(m
->fictitious
);
2331 return KERN_SUCCESS
;
2334 if (*type_of_fault
== DBG_ZERO_FILL_FAULT
) {
2336 vm_object_lock_assert_exclusive(m
->object
);
2338 } else if ((fault_type
& VM_PROT_WRITE
) == 0) {
2340 * This is not a "write" fault, so we
2341 * might not have taken the object lock
2342 * exclusively and we might not be able
2343 * to update the "wpmapped" bit in
2345 * Let's just grant read access to
2346 * the page for now and we'll
2347 * soft-fault again if we need write
2350 prot
&= ~VM_PROT_WRITE
;
2352 if (m
->pmapped
== FALSE
) {
2354 if ((*type_of_fault
== DBG_CACHE_HIT_FAULT
) && m
->clustered
) {
2356 * found it in the cache, but this
2357 * is the first fault-in of the page (m->pmapped == FALSE)
2358 * so it must have come in as part of
2359 * a cluster... account 1 pagein against it
2361 VM_STAT_INCR(pageins
);
2362 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
2364 if (m
->object
->internal
) {
2365 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
2366 *type_of_fault
= DBG_PAGEIND_FAULT
;
2368 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
2369 *type_of_fault
= DBG_PAGEINV_FAULT
;
2372 current_task()->pageins
++;
2374 VM_PAGE_CONSUME_CLUSTERED(m
);
2378 if (*type_of_fault
!= DBG_COW_FAULT
) {
2379 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2381 if (pmap
== kernel_pmap
) {
2382 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2386 /* Validate code signature if necessary. */
2387 if (VM_FAULT_NEED_CS_VALIDATION(pmap
, m
)) {
2388 vm_object_lock_assert_exclusive(m
->object
);
2390 if (m
->cs_validated
) {
2391 vm_cs_revalidates
++;
2394 /* VM map is locked, so 1 ref will remain on VM object -
2395 * so no harm if vm_page_validate_cs drops the object lock */
2396 vm_page_validate_cs(m
);
2399 #define page_immutable(m,prot) ((m)->cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/)
2401 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2402 (pmap
== vm_map_pmap(current_thread()->map
)));
2403 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2405 /* If the map is switched, and is switch-protected, we must protect
2406 * some pages from being write-faulted: immutable pages because by
2407 * definition they may not be written, and executable pages because that
2408 * would provide a way to inject unsigned code.
2409 * If the page is immutable, we can simply return. However, we can't
2410 * immediately determine whether a page is executable anywhere. But,
2411 * we can disconnect it everywhere and remove the executable protection
2412 * from the current map. We do that below right before we do the
2415 if(!cs_enforcement_disable
&& map_is_switched
&&
2416 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2417 (prot
& VM_PROT_WRITE
))
2419 return KERN_CODESIGN_ERROR
;
2422 /* A page could be tainted, or pose a risk of being tainted later.
2423 * Check whether the receiving process wants it, and make it feel
2424 * the consequences (that hapens in cs_invalid_page()).
2425 * For CS Enforcement, two other conditions will
2426 * cause that page to be tainted as well:
2427 * - pmapping an unsigned page executable - this means unsigned code;
2428 * - writeable mapping of a validated page - the content of that page
2429 * can be changed without the kernel noticing, therefore unsigned
2430 * code can be created
2432 if (m
->cs_tainted
||
2433 (( !cs_enforcement_disable
&& !cs_bypass
) &&
2434 (/* The page is unsigned and wants to be executable */
2435 (!m
->cs_validated
&& (prot
& VM_PROT_EXECUTE
)) ||
2436 /* The page should be immutable, but is in danger of being modified
2437 * This is the case where we want policy from the code directory -
2438 * is the page immutable or not? For now we have to assume that
2439 * code pages will be immutable, data pages not.
2440 * We'll assume a page is a code page if it has a code directory
2441 * and we fault for execution.
2442 * That is good enough since if we faulted the code page for
2443 * writing in another map before, it is wpmapped; if we fault
2444 * it for writing in this map later it will also be faulted for executing
2445 * at the same time; and if we fault for writing in another map
2446 * later, we will disconnect it from this pmap so we'll notice
2449 (page_immutable(m
, prot
) && ((prot
& VM_PROT_WRITE
) || m
->wpmapped
))
2453 /* We will have a tainted page. Have to handle the special case
2454 * of a switched map now. If the map is not switched, standard
2455 * procedure applies - call cs_invalid_page().
2456 * If the map is switched, the real owner is invalid already.
2457 * There is no point in invalidating the switching process since
2458 * it will not be executing from the map. So we don't call
2459 * cs_invalid_page() in that case. */
2460 boolean_t reject_page
;
2461 if(map_is_switched
) {
2462 assert(pmap
==vm_map_pmap(current_thread()->map
));
2463 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2464 reject_page
= FALSE
;
2466 reject_page
= cs_invalid_page((addr64_t
) vaddr
);
2470 /* reject the tainted page: abort the page fault */
2471 kr
= KERN_CODESIGN_ERROR
;
2472 cs_enter_tainted_rejected
++;
2474 /* proceed with the tainted page */
2476 /* Page might have been tainted before or not; now it
2477 * definitively is. If the page wasn't tainted, we must
2478 * disconnect it from all pmaps later. */
2479 must_disconnect
= !m
->cs_tainted
;
2480 m
->cs_tainted
= TRUE
;
2481 cs_enter_tainted_accepted
++;
2483 if (cs_debug
|| kr
!= KERN_SUCCESS
) {
2484 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2485 "page %p obj %p off 0x%llx *** INVALID PAGE ***\n",
2486 (long long)vaddr
, m
, m
->object
, m
->offset
);
2490 /* proceed with the valid page */
2494 /* If we have a KERN_SUCCESS from the previous checks, we either have
2495 * a good page, or a tainted page that has been accepted by the process.
2496 * In both cases the page will be entered into the pmap.
2497 * If the page is writeable, we need to disconnect it from other pmaps
2498 * now so those processes can take note.
2500 if (kr
== KERN_SUCCESS
) {
2502 * NOTE: we may only hold the vm_object lock SHARED
2503 * at this point, but the update of pmapped is ok
2504 * since this is the ONLY bit updated behind the SHARED
2505 * lock... however, we need to figure out how to do an atomic
2506 * update on a bit field to make this less fragile... right
2507 * now I don't know how to coerce 'C' to give me the offset info
2508 * that's needed for an AtomicCompareAndSwap
2511 if(vm_page_is_slideable(m
)) {
2512 boolean_t was_busy
= m
->busy
;
2514 kr
= vm_page_slide(m
, 0);
2517 PAGE_WAKEUP_DONE(m
);
2519 if (kr
!= KERN_SUCCESS
) {
2521 * This page has not been slid correctly,
2522 * do not do the pmap_enter() !
2523 * Let vm_fault_enter() return the error
2524 * so the caller can fail the fault.
2526 goto after_the_pmap_enter
;
2530 if (fault_type
& VM_PROT_WRITE
) {
2532 if (m
->wpmapped
== FALSE
) {
2533 vm_object_lock_assert_exclusive(m
->object
);
2537 if (must_disconnect
) {
2539 * We can only get here
2540 * because of the CSE logic
2542 assert(cs_enforcement_disable
== FALSE
);
2543 pmap_disconnect(m
->phys_page
);
2545 * If we are faulting for a write, we can clear
2546 * the execute bit - that will ensure the page is
2547 * checked again before being executable, which
2548 * protects against a map switch.
2549 * This only happens the first time the page
2550 * gets tainted, so we won't get stuck here
2551 * to make an already writeable page executable.
2554 prot
&= ~VM_PROT_EXECUTE
;
2559 /* Prevent a deadlock by not
2560 * holding the object lock if we need to wait for a page in
2561 * pmap_enter() - <rdar://problem/7138958> */
2562 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
, 0,
2563 wired
, PMAP_OPTIONS_NOWAIT
, pe_result
);
2565 if(pe_result
== KERN_RESOURCE_SHORTAGE
) {
2569 * this will be non-null in the case where we hold the lock
2570 * on the top-object in this chain... we can't just drop
2571 * the lock on the object we're inserting the page into
2572 * and recall the PMAP_ENTER since we can still cause
2573 * a deadlock if one of the critical paths tries to
2574 * acquire the lock on the top-object and we're blocked
2575 * in PMAP_ENTER waiting for memory... our only recourse
2576 * is to deal with it at a higher level where we can
2580 vm_pmap_enter_retried
++;
2581 goto after_the_pmap_enter
;
2583 /* The nonblocking version of pmap_enter did not succeed.
2584 * and we don't need to drop other locks and retry
2585 * at the level above us, so
2586 * use the blocking version instead. Requires marking
2587 * the page busy and unlocking the object */
2588 boolean_t was_busy
= m
->busy
;
2590 vm_object_unlock(m
->object
);
2592 PMAP_ENTER(pmap
, vaddr
, m
, prot
, fault_type
, 0, wired
);
2594 /* Take the object lock again. */
2595 vm_object_lock(m
->object
);
2597 /* If the page was busy, someone else will wake it up.
2598 * Otherwise, we have to do it now. */
2601 PAGE_WAKEUP_DONE(m
);
2603 vm_pmap_enter_blocked
++;
2607 after_the_pmap_enter
:
2609 * Hold queues lock to manipulate
2610 * the page queues. Change wiring
2613 if (change_wiring
) {
2614 vm_page_lockspin_queues();
2617 if (kr
== KERN_SUCCESS
) {
2621 vm_page_unwire(m
, TRUE
);
2623 vm_page_unlock_queues();
2626 if (kr
!= KERN_SUCCESS
) {
2627 vm_page_lockspin_queues();
2628 vm_page_deactivate(m
);
2629 vm_page_unlock_queues();
2631 if (((!m
->active
&& !m
->inactive
) || m
->clean_queue
|| no_cache
) && !VM_PAGE_WIRED(m
) && !m
->throttled
) {
2633 if ( vm_page_local_q
&& !no_cache
&& (*type_of_fault
== DBG_COW_FAULT
|| *type_of_fault
== DBG_ZERO_FILL_FAULT
) ) {
2638 * we got a local queue to stuff this new page on...
2639 * its safe to manipulate local and local_id at this point
2640 * since we're behind an exclusive object lock and the
2641 * page is not on any global queue.
2643 * we'll use the current cpu number to select the queue
2644 * note that we don't need to disable preemption... we're
2645 * going to behind the local queue's lock to do the real
2650 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
2652 VPL_LOCK(&lq
->vpl_lock
);
2654 queue_enter(&lq
->vpl_queue
, m
, vm_page_t
, pageq
);
2659 VPL_UNLOCK(&lq
->vpl_lock
);
2661 if (lq
->vpl_count
> vm_page_local_q_soft_limit
) {
2663 * we're beyond the soft limit for the local queue
2664 * vm_page_reactivate_local will 'try' to take
2665 * the global page queue lock... if it can't that's
2666 * ok... we'll let the queue continue to grow up
2667 * to the hard limit... at that point we'll wait
2668 * for the lock... once we've got the lock, we'll
2669 * transfer all of the pages from the local queue
2670 * to the global active queue
2672 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
2677 vm_page_lockspin_queues();
2679 * test again now that we hold the page queue lock
2681 if (!VM_PAGE_WIRED(m
)) {
2682 if (m
->clean_queue
) {
2683 VM_PAGE_QUEUES_REMOVE(m
);
2685 vm_pageout_cleaned_reactivated
++;
2686 vm_pageout_cleaned_fault_reactivated
++;
2689 if ((!m
->active
&& !m
->inactive
) || no_cache
) {
2691 * If this is a no_cache mapping and the page has never been
2692 * mapped before or was previously a no_cache page, then we
2693 * want to leave pages in the speculative state so that they
2694 * can be readily recycled if free memory runs low. Otherwise
2695 * the page is activated as normal.
2698 if (no_cache
&& (!previously_pmapped
|| m
->no_cache
)) {
2701 if (!m
->speculative
)
2702 vm_page_speculate(m
, FALSE
);
2704 } else if (!m
->active
&& !m
->inactive
) {
2706 vm_page_activate(m
);
2710 vm_page_unlock_queues();
2721 * Handle page faults, including pseudo-faults
2722 * used to change the wiring status of pages.
2724 * Explicit continuations have been removed.
2726 * vm_fault and vm_fault_page save mucho state
2727 * in the moral equivalent of a closure. The state
2728 * structure is allocated when first entering vm_fault
2729 * and deallocated when leaving vm_fault.
2732 extern int _map_enter_debug
;
2734 unsigned long vm_fault_collapse_total
= 0;
2735 unsigned long vm_fault_collapse_skipped
= 0;
2740 vm_map_offset_t vaddr
,
2741 vm_prot_t fault_type
,
2742 boolean_t change_wiring
,
2745 vm_map_offset_t caller_pmap_addr
)
2747 vm_map_version_t version
; /* Map version for verificiation */
2748 boolean_t wired
; /* Should mapping be wired down? */
2749 vm_object_t object
; /* Top-level object */
2750 vm_object_offset_t offset
; /* Top-level offset */
2751 vm_prot_t prot
; /* Protection for mapping */
2752 vm_object_t old_copy_object
; /* Saved copy object */
2753 vm_page_t result_page
; /* Result of vm_fault_page */
2754 vm_page_t top_page
; /* Placeholder page */
2757 vm_page_t m
; /* Fast access to result_page */
2758 kern_return_t error_code
;
2759 vm_object_t cur_object
;
2760 vm_object_offset_t cur_offset
;
2762 vm_object_t new_object
;
2765 boolean_t interruptible_state
;
2766 vm_map_t real_map
= map
;
2767 vm_map_t original_map
= map
;
2768 vm_prot_t original_fault_type
;
2769 struct vm_object_fault_info fault_info
;
2770 boolean_t need_collapse
= FALSE
;
2771 boolean_t need_retry
= FALSE
;
2772 int object_lock_type
= 0;
2773 int cur_object_lock_type
;
2774 vm_object_t top_object
= VM_OBJECT_NULL
;
2778 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2779 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
2780 (int)((uint64_t)vaddr
>> 32),
2782 (map
== kernel_map
),
2786 if (get_preemption_level() != 0) {
2787 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2788 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
2789 (int)((uint64_t)vaddr
>> 32),
2795 return (KERN_FAILURE
);
2798 interruptible_state
= thread_interrupt_level(interruptible
);
2800 VM_STAT_INCR(faults
);
2801 current_task()->faults
++;
2802 original_fault_type
= fault_type
;
2804 if (fault_type
& VM_PROT_WRITE
)
2805 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2807 object_lock_type
= OBJECT_LOCK_SHARED
;
2809 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
2813 * assume we will hit a page in the cache
2814 * otherwise, explicitly override with
2815 * the real fault type once we determine it
2817 type_of_fault
= DBG_CACHE_HIT_FAULT
;
2820 * Find the backing store object and offset into
2821 * it to begin the search.
2823 fault_type
= original_fault_type
;
2825 vm_map_lock_read(map
);
2827 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
2828 object_lock_type
, &version
,
2829 &object
, &offset
, &prot
, &wired
,
2833 if (kr
!= KERN_SUCCESS
) {
2834 vm_map_unlock_read(map
);
2837 pmap
= real_map
->pmap
;
2838 fault_info
.interruptible
= interruptible
;
2839 fault_info
.stealth
= FALSE
;
2840 fault_info
.io_sync
= FALSE
;
2841 fault_info
.mark_zf_absent
= FALSE
;
2842 fault_info
.batch_pmap_op
= FALSE
;
2845 * If the page is wired, we must fault for the current protection
2846 * value, to avoid further faults.
2849 fault_type
= prot
| VM_PROT_WRITE
;
2851 * since we're treating this fault as a 'write'
2852 * we must hold the top object lock exclusively
2854 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2856 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2858 if (vm_object_lock_upgrade(object
) == FALSE
) {
2860 * couldn't upgrade, so explictly
2861 * take the lock exclusively
2863 vm_object_lock(object
);
2868 #if VM_FAULT_CLASSIFY
2870 * Temporary data gathering code
2872 vm_fault_classify(object
, offset
, fault_type
);
2875 * Fast fault code. The basic idea is to do as much as
2876 * possible while holding the map lock and object locks.
2877 * Busy pages are not used until the object lock has to
2878 * be dropped to do something (copy, zero fill, pmap enter).
2879 * Similarly, paging references aren't acquired until that
2880 * point, and object references aren't used.
2882 * If we can figure out what to do
2883 * (zero fill, copy on write, pmap enter) while holding
2884 * the locks, then it gets done. Otherwise, we give up,
2885 * and use the original fault path (which doesn't hold
2886 * the map lock, and relies on busy pages).
2887 * The give up cases include:
2888 * - Have to talk to pager.
2889 * - Page is busy, absent or in error.
2890 * - Pager has locked out desired access.
2891 * - Fault needs to be restarted.
2892 * - Have to push page into copy object.
2894 * The code is an infinite loop that moves one level down
2895 * the shadow chain each time. cur_object and cur_offset
2896 * refer to the current object being examined. object and offset
2897 * are the original object from the map. The loop is at the
2898 * top level if and only if object and cur_object are the same.
2900 * Invariants: Map lock is held throughout. Lock is held on
2901 * original object and cur_object (if different) when
2902 * continuing or exiting loop.
2908 * If this page is to be inserted in a copy delay object
2909 * for writing, and if the object has a copy, then the
2910 * copy delay strategy is implemented in the slow fault page.
2912 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
2913 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
))
2914 goto handle_copy_delay
;
2916 cur_object
= object
;
2917 cur_offset
= offset
;
2920 if (!cur_object
->pager_created
&&
2921 cur_object
->phys_contiguous
) /* superpage */
2924 if (cur_object
->blocked_access
) {
2926 * Access to this VM object has been blocked.
2927 * Let the slow path handle it.
2932 m
= vm_page_lookup(cur_object
, cur_offset
);
2934 if (m
!= VM_PAGE_NULL
) {
2936 wait_result_t result
;
2939 * in order to do the PAGE_ASSERT_WAIT, we must
2940 * have object that 'm' belongs to locked exclusively
2942 if (object
!= cur_object
) {
2943 vm_object_unlock(object
);
2945 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2947 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2949 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2951 * couldn't upgrade so go do a full retry
2952 * immediately since we've already dropped
2953 * the top object lock associated with this page
2954 * and the current one got dropped due to the
2955 * failed upgrade... the state is no longer valid
2957 vm_map_unlock_read(map
);
2958 if (real_map
!= map
)
2959 vm_map_unlock(real_map
);
2964 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2966 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2968 if (vm_object_lock_upgrade(object
) == FALSE
) {
2970 * couldn't upgrade, so explictly take the lock
2971 * exclusively and go relookup the page since we
2972 * will have dropped the object lock and
2973 * a different thread could have inserted
2974 * a page at this offset
2975 * no need for a full retry since we're
2976 * at the top level of the object chain
2978 vm_object_lock(object
);
2983 vm_map_unlock_read(map
);
2984 if (real_map
!= map
)
2985 vm_map_unlock(real_map
);
2987 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
2989 vm_object_unlock(cur_object
);
2991 if (result
== THREAD_WAITING
) {
2992 result
= thread_block(THREAD_CONTINUE_NULL
);
2994 counter(c_vm_fault_page_block_busy_kernel
++);
2996 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
3003 if (object
!= cur_object
) {
3004 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3005 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3007 vm_object_unlock(object
);
3008 vm_object_unlock(cur_object
);
3010 vm_map_unlock_read(map
);
3011 if (real_map
!= map
)
3012 vm_map_unlock(real_map
);
3017 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3019 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3021 if (vm_object_lock_upgrade(object
) == FALSE
) {
3023 * couldn't upgrade, so explictly take the lock
3024 * exclusively and go relookup the page since we
3025 * will have dropped the object lock and
3026 * a different thread could have inserted
3027 * a page at this offset
3028 * no need for a full retry since we're
3029 * at the top level of the object chain
3031 vm_object_lock(object
);
3038 vm_pageout_steal_laundry(m
, FALSE
);
3041 if (m
->phys_page
== vm_page_guard_addr
) {
3043 * Guard page: let the slow path deal with it
3047 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
)) {
3049 * Unusual case... let the slow path deal with it
3053 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m
->object
)) {
3054 if (object
!= cur_object
)
3055 vm_object_unlock(object
);
3056 vm_map_unlock_read(map
);
3057 if (real_map
!= map
)
3058 vm_map_unlock(real_map
);
3059 vm_object_unlock(cur_object
);
3060 kr
= KERN_MEMORY_ERROR
;
3067 * We've soft-faulted (because it's not in the page
3068 * table) on an encrypted page.
3069 * Keep the page "busy" so that no one messes with
3070 * it during the decryption.
3071 * Release the extra locks we're holding, keep only
3072 * the page's VM object lock.
3074 * in order to set 'busy' on 'm', we must
3075 * have object that 'm' belongs to locked exclusively
3077 if (object
!= cur_object
) {
3078 vm_object_unlock(object
);
3080 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3082 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3084 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3086 * couldn't upgrade so go do a full retry
3087 * immediately since we've already dropped
3088 * the top object lock associated with this page
3089 * and the current one got dropped due to the
3090 * failed upgrade... the state is no longer valid
3092 vm_map_unlock_read(map
);
3093 if (real_map
!= map
)
3094 vm_map_unlock(real_map
);
3099 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3101 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3103 if (vm_object_lock_upgrade(object
) == FALSE
) {
3105 * couldn't upgrade, so explictly take the lock
3106 * exclusively and go relookup the page since we
3107 * will have dropped the object lock and
3108 * a different thread could have inserted
3109 * a page at this offset
3110 * no need for a full retry since we're
3111 * at the top level of the object chain
3113 vm_object_lock(object
);
3120 vm_map_unlock_read(map
);
3121 if (real_map
!= map
)
3122 vm_map_unlock(real_map
);
3124 vm_page_decrypt(m
, 0);
3127 PAGE_WAKEUP_DONE(m
);
3129 vm_object_unlock(cur_object
);
3131 * Retry from the top, in case anything
3132 * changed while we were decrypting...
3136 ASSERT_PAGE_DECRYPTED(m
);
3138 if(vm_page_is_slideable(m
)) {
3140 * We might need to slide this page, and so,
3141 * we want to hold the VM object exclusively.
3143 if (object
!= cur_object
) {
3144 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3145 vm_object_unlock(object
);
3146 vm_object_unlock(cur_object
);
3148 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3150 vm_map_unlock_read(map
);
3151 if (real_map
!= map
)
3152 vm_map_unlock(real_map
);
3156 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3158 vm_object_unlock(object
);
3159 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3160 vm_map_unlock_read(map
);
3165 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
)) {
3166 upgrade_for_validation
:
3168 * We might need to validate this page
3169 * against its code signature, so we
3170 * want to hold the VM object exclusively.
3172 if (object
!= cur_object
) {
3173 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3174 vm_object_unlock(object
);
3175 vm_object_unlock(cur_object
);
3177 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3179 vm_map_unlock_read(map
);
3180 if (real_map
!= map
)
3181 vm_map_unlock(real_map
);
3186 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3188 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3190 if (vm_object_lock_upgrade(object
) == FALSE
) {
3192 * couldn't upgrade, so explictly take the lock
3193 * exclusively and go relookup the page since we
3194 * will have dropped the object lock and
3195 * a different thread could have inserted
3196 * a page at this offset
3197 * no need for a full retry since we're
3198 * at the top level of the object chain
3200 vm_object_lock(object
);
3207 * Two cases of map in faults:
3208 * - At top level w/o copy object.
3209 * - Read fault anywhere.
3210 * --> must disallow write.
3213 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
3218 if ((fault_type
& VM_PROT_WRITE
) == 0) {
3220 if (object
!= cur_object
) {
3222 * We still need to hold the top object
3223 * lock here to prevent a race between
3224 * a read fault (taking only "shared"
3225 * locks) and a write fault (taking
3226 * an "exclusive" lock on the top
3228 * Otherwise, as soon as we release the
3229 * top lock, the write fault could
3230 * proceed and actually complete before
3231 * the read fault, and the copied page's
3232 * translation could then be overwritten
3233 * by the read fault's translation for
3234 * the original page.
3236 * Let's just record what the top object
3237 * is and we'll release it later.
3239 top_object
= object
;
3242 * switch to the object that has the new page
3244 object
= cur_object
;
3245 object_lock_type
= cur_object_lock_type
;
3249 * prepare for the pmap_enter...
3250 * object and map are both locked
3251 * m contains valid data
3252 * object == m->object
3253 * cur_object == NULL or it's been unlocked
3254 * no paging references on either object or cur_object
3257 kr
= vm_fault_enter(m
,
3264 fault_info
.no_cache
,
3265 fault_info
.cs_bypass
,
3266 (top_object
!= VM_OBJECT_NULL
? &need_retry
: NULL
),
3269 kr
= vm_fault_enter(m
,
3276 fault_info
.no_cache
,
3277 fault_info
.cs_bypass
,
3278 (top_object
!= VM_OBJECT_NULL
? &need_retry
: NULL
),
3282 if (top_object
!= VM_OBJECT_NULL
) {
3284 * It's safe to drop the top object
3285 * now that we've done our
3286 * vm_fault_enter(). Any other fault
3287 * in progress for that virtual
3288 * address will either find our page
3289 * and translation or put in a new page
3292 vm_object_unlock(top_object
);
3293 top_object
= VM_OBJECT_NULL
;
3296 if (need_collapse
== TRUE
)
3297 vm_object_collapse(object
, offset
, TRUE
);
3299 if (need_retry
== FALSE
&&
3300 (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
)) {
3302 * evaluate access pattern and update state
3303 * vm_fault_deactivate_behind depends on the
3304 * state being up to date
3306 vm_fault_is_sequential(object
, cur_offset
, fault_info
.behavior
);
3308 vm_fault_deactivate_behind(object
, cur_offset
, fault_info
.behavior
);
3311 * That's it, clean up and return.
3314 PAGE_WAKEUP_DONE(m
);
3316 vm_object_unlock(object
);
3318 vm_map_unlock_read(map
);
3319 if (real_map
!= map
)
3320 vm_map_unlock(real_map
);
3322 if (need_retry
== TRUE
) {
3324 * vm_fault_enter couldn't complete the PMAP_ENTER...
3325 * at this point we don't hold any locks so it's safe
3326 * to ask the pmap layer to expand the page table to
3327 * accommodate this mapping... once expanded, we'll
3328 * re-drive the fault which should result in vm_fault_enter
3329 * being able to successfully enter the mapping this time around
3331 (void)pmap_enter_options(pmap
, vaddr
, 0, 0, 0, 0, 0, PMAP_OPTIONS_NOENTER
);
3339 * COPY ON WRITE FAULT
3341 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
3343 if ((throttle_delay
= vm_page_throttled())) {
3345 * drop all of our locks...
3346 * wait until the free queue is
3347 * pumped back up and then
3350 if (object
!= cur_object
)
3351 vm_object_unlock(cur_object
);
3352 vm_object_unlock(object
);
3353 vm_map_unlock_read(map
);
3354 if (real_map
!= map
)
3355 vm_map_unlock(real_map
);
3357 VM_DEBUG_EVENT(vmf_cowdelay
, VMF_COWDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
3359 delay(throttle_delay
);
3361 if (!current_thread_aborted() && vm_page_wait((change_wiring
) ?
3369 * If objects match, then
3370 * object->copy must not be NULL (else control
3371 * would be in previous code block), and we
3372 * have a potential push into the copy object
3373 * with which we can't cope with here.
3375 if (cur_object
== object
) {
3377 * must take the slow path to
3378 * deal with the copy push
3384 * This is now a shadow based copy on write
3385 * fault -- it requires a copy up the shadow
3389 if ((cur_object_lock_type
== OBJECT_LOCK_SHARED
) &&
3390 VM_FAULT_NEED_CS_VALIDATION(NULL
, m
)) {
3391 goto upgrade_for_validation
;
3395 * Allocate a page in the original top level
3396 * object. Give up if allocate fails. Also
3397 * need to remember current page, as it's the
3398 * source of the copy.
3400 * at this point we hold locks on both
3401 * object and cur_object... no need to take
3402 * paging refs or mark pages BUSY since
3403 * we don't drop either object lock until
3404 * the page has been copied and inserted
3409 if (m
== VM_PAGE_NULL
) {
3411 * no free page currently available...
3412 * must take the slow path
3417 * Now do the copy. Mark the source page busy...
3419 * NOTE: This code holds the map lock across
3422 vm_page_copy(cur_m
, m
);
3423 vm_page_insert(m
, object
, offset
);
3424 SET_PAGE_DIRTY(m
, FALSE
);
3427 * Now cope with the source page and object
3429 if (object
->ref_count
> 1 && cur_m
->pmapped
)
3430 pmap_disconnect(cur_m
->phys_page
);
3432 need_collapse
= TRUE
;
3434 if (!cur_object
->internal
&&
3435 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3437 * The object from which we've just
3438 * copied a page is most probably backed
3439 * by a vnode. We don't want to waste too
3440 * much time trying to collapse the VM objects
3441 * and create a bottleneck when several tasks
3442 * map the same file.
3444 if (cur_object
->copy
== object
) {
3446 * Shared mapping or no COW yet.
3447 * We can never collapse a copy
3448 * object into its backing object.
3450 need_collapse
= FALSE
;
3451 } else if (cur_object
->copy
== object
->shadow
&&
3452 object
->shadow
->resident_page_count
== 0) {
3454 * Shared mapping after a COW occurred.
3456 need_collapse
= FALSE
;
3459 vm_object_unlock(cur_object
);
3461 if (need_collapse
== FALSE
)
3462 vm_fault_collapse_skipped
++;
3463 vm_fault_collapse_total
++;
3465 type_of_fault
= DBG_COW_FAULT
;
3466 VM_STAT_INCR(cow_faults
);
3467 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
3468 current_task()->cow_faults
++;
3474 * No page at cur_object, cur_offset... m == NULL
3476 if (cur_object
->pager_created
) {
3477 if (MUST_ASK_PAGER(cur_object
, cur_offset
) == TRUE
) {
3479 * May have to talk to a pager...
3480 * take the slow path.
3485 * existence map present and indicates
3486 * that the pager doesn't have this page
3489 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
3491 * Zero fill fault. Page gets
3492 * inserted into the original object.
3494 if (cur_object
->shadow_severed
||
3495 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
))
3497 if (object
!= cur_object
)
3498 vm_object_unlock(cur_object
);
3499 vm_object_unlock(object
);
3501 vm_map_unlock_read(map
);
3502 if (real_map
!= map
)
3503 vm_map_unlock(real_map
);
3505 kr
= KERN_MEMORY_ERROR
;
3508 if ((throttle_delay
= vm_page_throttled())) {
3510 * drop all of our locks...
3511 * wait until the free queue is
3512 * pumped back up and then
3515 if (object
!= cur_object
)
3516 vm_object_unlock(cur_object
);
3517 vm_object_unlock(object
);
3518 vm_map_unlock_read(map
);
3519 if (real_map
!= map
)
3520 vm_map_unlock(real_map
);
3522 VM_DEBUG_EVENT(vmf_zfdelay
, VMF_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
3524 delay(throttle_delay
);
3526 if (!current_thread_aborted() && vm_page_wait((change_wiring
) ?
3533 if (vm_backing_store_low
) {
3535 * we are protecting the system from
3536 * backing store exhaustion...
3537 * must take the slow path if we're
3540 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
))
3543 if (cur_object
!= object
) {
3544 vm_object_unlock(cur_object
);
3546 cur_object
= object
;
3548 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3550 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3552 if (vm_object_lock_upgrade(object
) == FALSE
) {
3554 * couldn't upgrade so do a full retry on the fault
3555 * since we dropped the object lock which
3556 * could allow another thread to insert
3557 * a page at this offset
3559 vm_map_unlock_read(map
);
3560 if (real_map
!= map
)
3561 vm_map_unlock(real_map
);
3566 m
= vm_page_alloc(object
, offset
);
3568 if (m
== VM_PAGE_NULL
) {
3570 * no free page currently available...
3571 * must take the slow path
3577 * Now zero fill page...
3578 * the page is probably going to
3579 * be written soon, so don't bother
3580 * to clear the modified bit
3582 * NOTE: This code holds the map
3583 * lock across the zero fill.
3585 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
3590 * On to the next level in the shadow chain
3592 cur_offset
+= cur_object
->vo_shadow_offset
;
3593 new_object
= cur_object
->shadow
;
3596 * take the new_object's lock with the indicated state
3598 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
)
3599 vm_object_lock_shared(new_object
);
3601 vm_object_lock(new_object
);
3603 if (cur_object
!= object
)
3604 vm_object_unlock(cur_object
);
3606 cur_object
= new_object
;
3612 * Cleanup from fast fault failure. Drop any object
3613 * lock other than original and drop map lock.
3615 if (object
!= cur_object
)
3616 vm_object_unlock(cur_object
);
3619 * must own the object lock exclusively at this point
3621 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3622 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3624 if (vm_object_lock_upgrade(object
) == FALSE
) {
3626 * couldn't upgrade, so explictly
3627 * take the lock exclusively
3628 * no need to retry the fault at this
3629 * point since "vm_fault_page" will
3630 * completely re-evaluate the state
3632 vm_object_lock(object
);
3637 vm_map_unlock_read(map
);
3638 if (real_map
!= map
)
3639 vm_map_unlock(real_map
);
3642 * Make a reference to this object to
3643 * prevent its disposal while we are messing with
3644 * it. Once we have the reference, the map is free
3645 * to be diddled. Since objects reference their
3646 * shadows (and copies), they will stay around as well.
3648 vm_object_reference_locked(object
);
3649 vm_object_paging_begin(object
);
3651 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
3655 kr
= vm_fault_page(object
, offset
, fault_type
,
3656 (change_wiring
&& !wired
),
3657 &prot
, &result_page
, &top_page
,
3659 &error_code
, map
->no_zero_fill
,
3660 FALSE
, &fault_info
);
3663 * if kr != VM_FAULT_SUCCESS, then the paging reference
3664 * has been dropped and the object unlocked... the ref_count
3667 * if kr == VM_FAULT_SUCCESS, then the paging reference
3668 * is still held along with the ref_count on the original object
3670 * the object is returned locked with a paging reference
3672 * if top_page != NULL, then it's BUSY and the
3673 * object it belongs to has a paging reference
3674 * but is returned unlocked
3676 if (kr
!= VM_FAULT_SUCCESS
&&
3677 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
3679 * we didn't succeed, lose the object reference immediately.
3681 vm_object_deallocate(object
);
3684 * See why we failed, and take corrective action.
3687 case VM_FAULT_MEMORY_SHORTAGE
:
3688 if (vm_page_wait((change_wiring
) ?
3695 case VM_FAULT_INTERRUPTED
:
3698 case VM_FAULT_RETRY
:
3700 case VM_FAULT_MEMORY_ERROR
:
3704 kr
= KERN_MEMORY_ERROR
;
3707 panic("vm_fault: unexpected error 0x%x from "
3708 "vm_fault_page()\n", kr
);
3713 if (m
!= VM_PAGE_NULL
) {
3714 assert((change_wiring
&& !wired
) ?
3715 (top_page
== VM_PAGE_NULL
) :
3716 ((top_page
== VM_PAGE_NULL
) == (m
->object
== object
)));
3720 * What to do with the resulting page from vm_fault_page
3721 * if it doesn't get entered into the physical map:
3723 #define RELEASE_PAGE(m) \
3725 PAGE_WAKEUP_DONE(m); \
3726 if (!m->active && !m->inactive && !m->throttled) { \
3727 vm_page_lockspin_queues(); \
3728 if (!m->active && !m->inactive && !m->throttled) \
3729 vm_page_activate(m); \
3730 vm_page_unlock_queues(); \
3735 * We must verify that the maps have not changed
3736 * since our last lookup.
3738 if (m
!= VM_PAGE_NULL
) {
3739 old_copy_object
= m
->object
->copy
;
3740 vm_object_unlock(m
->object
);
3742 old_copy_object
= VM_OBJECT_NULL
;
3743 vm_object_unlock(object
);
3747 * no object locks are held at this point
3749 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
3750 vm_object_t retry_object
;
3751 vm_object_offset_t retry_offset
;
3752 vm_prot_t retry_prot
;
3755 * To avoid trying to write_lock the map while another
3756 * thread has it read_locked (in vm_map_pageable), we
3757 * do not try for write permission. If the page is
3758 * still writable, we will get write permission. If it
3759 * is not, or has been marked needs_copy, we enter the
3760 * mapping without write permission, and will merely
3761 * take another fault.
3764 vm_map_lock_read(map
);
3766 kr
= vm_map_lookup_locked(&map
, vaddr
,
3767 fault_type
& ~VM_PROT_WRITE
,
3768 OBJECT_LOCK_EXCLUSIVE
, &version
,
3769 &retry_object
, &retry_offset
, &retry_prot
,
3773 pmap
= real_map
->pmap
;
3775 if (kr
!= KERN_SUCCESS
) {
3776 vm_map_unlock_read(map
);
3778 if (m
!= VM_PAGE_NULL
) {
3780 * retake the lock so that
3781 * we can drop the paging reference
3782 * in vm_fault_cleanup and do the
3783 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3785 vm_object_lock(m
->object
);
3789 vm_fault_cleanup(m
->object
, top_page
);
3792 * retake the lock so that
3793 * we can drop the paging reference
3794 * in vm_fault_cleanup
3796 vm_object_lock(object
);
3798 vm_fault_cleanup(object
, top_page
);
3800 vm_object_deallocate(object
);
3804 vm_object_unlock(retry_object
);
3806 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
3808 vm_map_unlock_read(map
);
3809 if (real_map
!= map
)
3810 vm_map_unlock(real_map
);
3812 if (m
!= VM_PAGE_NULL
) {
3814 * retake the lock so that
3815 * we can drop the paging reference
3816 * in vm_fault_cleanup and do the
3817 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3819 vm_object_lock(m
->object
);
3823 vm_fault_cleanup(m
->object
, top_page
);
3826 * retake the lock so that
3827 * we can drop the paging reference
3828 * in vm_fault_cleanup
3830 vm_object_lock(object
);
3832 vm_fault_cleanup(object
, top_page
);
3834 vm_object_deallocate(object
);
3839 * Check whether the protection has changed or the object
3840 * has been copied while we left the map unlocked.
3844 if (m
!= VM_PAGE_NULL
) {
3845 vm_object_lock(m
->object
);
3847 if (m
->object
->copy
!= old_copy_object
) {
3849 * The copy object changed while the top-level object
3850 * was unlocked, so take away write permission.
3852 prot
&= ~VM_PROT_WRITE
;
3855 vm_object_lock(object
);
3858 * If we want to wire down this page, but no longer have
3859 * adequate permissions, we must start all over.
3861 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
3863 vm_map_verify_done(map
, &version
);
3864 if (real_map
!= map
)
3865 vm_map_unlock(real_map
);
3867 if (m
!= VM_PAGE_NULL
) {
3870 vm_fault_cleanup(m
->object
, top_page
);
3872 vm_fault_cleanup(object
, top_page
);
3874 vm_object_deallocate(object
);
3878 if (m
!= VM_PAGE_NULL
) {
3880 * Put this page into the physical map.
3881 * We had to do the unlock above because pmap_enter
3882 * may cause other faults. The page may be on
3883 * the pageout queues. If the pageout daemon comes
3884 * across the page, it will remove it from the queues.
3887 kr
= vm_fault_enter(m
,
3894 fault_info
.no_cache
,
3895 fault_info
.cs_bypass
,
3899 kr
= vm_fault_enter(m
,
3906 fault_info
.no_cache
,
3907 fault_info
.cs_bypass
,
3911 if (kr
!= KERN_SUCCESS
) {
3912 /* abort this page fault */
3913 vm_map_verify_done(map
, &version
);
3914 if (real_map
!= map
)
3915 vm_map_unlock(real_map
);
3916 PAGE_WAKEUP_DONE(m
);
3917 vm_fault_cleanup(m
->object
, top_page
);
3918 vm_object_deallocate(object
);
3923 vm_map_entry_t entry
;
3924 vm_map_offset_t laddr
;
3925 vm_map_offset_t ldelta
, hdelta
;
3928 * do a pmap block mapping from the physical address
3933 /* While we do not worry about execution protection in */
3934 /* general, certian pages may have instruction execution */
3935 /* disallowed. We will check here, and if not allowed */
3936 /* to execute, we return with a protection failure. */
3938 if ((fault_type
& VM_PROT_EXECUTE
) &&
3939 (!pmap_eligible_for_execute((ppnum_t
)(object
->vo_shadow_offset
>> 12)))) {
3941 vm_map_verify_done(map
, &version
);
3943 if (real_map
!= map
)
3944 vm_map_unlock(real_map
);
3946 vm_fault_cleanup(object
, top_page
);
3947 vm_object_deallocate(object
);
3949 kr
= KERN_PROTECTION_FAILURE
;
3954 if (real_map
!= map
)
3955 vm_map_unlock(real_map
);
3957 if (original_map
!= map
) {
3958 vm_map_unlock_read(map
);
3959 vm_map_lock_read(original_map
);
3965 hdelta
= 0xFFFFF000;
3966 ldelta
= 0xFFFFF000;
3968 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
3969 if (ldelta
> (laddr
- entry
->vme_start
))
3970 ldelta
= laddr
- entry
->vme_start
;
3971 if (hdelta
> (entry
->vme_end
- laddr
))
3972 hdelta
= entry
->vme_end
- laddr
;
3973 if (entry
->is_sub_map
) {
3975 laddr
= (laddr
- entry
->vme_start
)
3977 vm_map_lock_read(entry
->object
.sub_map
);
3979 if (map
!= real_map
)
3980 vm_map_unlock_read(map
);
3981 if (entry
->use_pmap
) {
3982 vm_map_unlock_read(real_map
);
3983 real_map
= entry
->object
.sub_map
;
3985 map
= entry
->object
.sub_map
;
3992 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
3993 (entry
->object
.vm_object
!= NULL
) &&
3994 (entry
->object
.vm_object
== object
)) {
3996 int superpage
= (!object
->pager_created
&& object
->phys_contiguous
)? VM_MEM_SUPERPAGE
: 0;
3999 * Set up a block mapped area
4001 assert((uint32_t)((ldelta
+ hdelta
) >> 12) == ((ldelta
+ hdelta
) >> 12));
4002 pmap_map_block(caller_pmap
,
4003 (addr64_t
)(caller_pmap_addr
- ldelta
),
4004 (ppnum_t
)((((vm_map_offset_t
) (entry
->object
.vm_object
->vo_shadow_offset
)) +
4005 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12),
4006 (uint32_t)((ldelta
+ hdelta
) >> 12), prot
,
4007 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
4010 * Set up a block mapped area
4012 assert((uint32_t)((ldelta
+ hdelta
) >> 12) == ((ldelta
+ hdelta
) >> 12));
4013 pmap_map_block(real_map
->pmap
,
4014 (addr64_t
)(vaddr
- ldelta
),
4015 (ppnum_t
)((((vm_map_offset_t
)(entry
->object
.vm_object
->vo_shadow_offset
)) +
4016 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12),
4017 (uint32_t)((ldelta
+ hdelta
) >> 12), prot
,
4018 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
4024 * Unlock everything, and return
4026 vm_map_verify_done(map
, &version
);
4027 if (real_map
!= map
)
4028 vm_map_unlock(real_map
);
4030 if (m
!= VM_PAGE_NULL
) {
4031 PAGE_WAKEUP_DONE(m
);
4033 vm_fault_cleanup(m
->object
, top_page
);
4035 vm_fault_cleanup(object
, top_page
);
4037 vm_object_deallocate(object
);
4043 thread_interrupt_level(interruptible_state
);
4045 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
4046 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
4047 (int)((uint64_t)vaddr
>> 32),
4059 * Wire down a range of virtual addresses in a map.
4064 vm_map_entry_t entry
,
4066 vm_map_offset_t pmap_addr
)
4069 register vm_map_offset_t va
;
4070 register vm_map_offset_t end_addr
= entry
->vme_end
;
4071 register kern_return_t rc
;
4073 assert(entry
->in_transition
);
4075 if ((entry
->object
.vm_object
!= NULL
) &&
4076 !entry
->is_sub_map
&&
4077 entry
->object
.vm_object
->phys_contiguous
) {
4078 return KERN_SUCCESS
;
4082 * Inform the physical mapping system that the
4083 * range of addresses may not fault, so that
4084 * page tables and such can be locked down as well.
4087 pmap_pageable(pmap
, pmap_addr
,
4088 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
4091 * We simulate a fault to get the page and enter it
4092 * in the physical map.
4095 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
4096 if ((rc
= vm_fault_wire_fast(
4097 map
, va
, entry
, pmap
,
4098 pmap_addr
+ (va
- entry
->vme_start
)
4099 )) != KERN_SUCCESS
) {
4100 rc
= vm_fault(map
, va
, VM_PROT_NONE
, TRUE
,
4101 (pmap
== kernel_pmap
) ?
4102 THREAD_UNINT
: THREAD_ABORTSAFE
,
4103 pmap
, pmap_addr
+ (va
- entry
->vme_start
));
4104 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
4107 if (rc
!= KERN_SUCCESS
) {
4108 struct vm_map_entry tmp_entry
= *entry
;
4110 /* unwire wired pages */
4111 tmp_entry
.vme_end
= va
;
4112 vm_fault_unwire(map
,
4113 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
4118 return KERN_SUCCESS
;
4124 * Unwire a range of virtual addresses in a map.
4129 vm_map_entry_t entry
,
4130 boolean_t deallocate
,
4132 vm_map_offset_t pmap_addr
)
4134 register vm_map_offset_t va
;
4135 register vm_map_offset_t end_addr
= entry
->vme_end
;
4137 struct vm_object_fault_info fault_info
;
4139 object
= (entry
->is_sub_map
)
4140 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
4143 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
4144 * do anything since such memory is wired by default. So we don't have
4145 * anything to undo here.
4148 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
)
4151 fault_info
.interruptible
= THREAD_UNINT
;
4152 fault_info
.behavior
= entry
->behavior
;
4153 fault_info
.user_tag
= entry
->alias
;
4154 fault_info
.lo_offset
= entry
->offset
;
4155 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
4156 fault_info
.no_cache
= entry
->no_cache
;
4157 fault_info
.stealth
= TRUE
;
4158 fault_info
.io_sync
= FALSE
;
4159 fault_info
.cs_bypass
= FALSE
;
4160 fault_info
.mark_zf_absent
= FALSE
;
4161 fault_info
.batch_pmap_op
= FALSE
;
4164 * Since the pages are wired down, we must be able to
4165 * get their mappings from the physical map system.
4168 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
4170 if (object
== VM_OBJECT_NULL
) {
4172 pmap_change_wiring(pmap
,
4173 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
4175 (void) vm_fault(map
, va
, VM_PROT_NONE
,
4176 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
4179 vm_page_t result_page
;
4181 vm_object_t result_object
;
4182 vm_fault_return_t result
;
4184 if (end_addr
- va
> (vm_size_t
) -1) {
4185 /* 32-bit overflow */
4186 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
4188 fault_info
.cluster_size
= (vm_size_t
) (end_addr
- va
);
4189 assert(fault_info
.cluster_size
== end_addr
- va
);
4193 prot
= VM_PROT_NONE
;
4195 vm_object_lock(object
);
4196 vm_object_paging_begin(object
);
4198 "vm_fault_unwire -> vm_fault_page\n",
4200 result
= vm_fault_page(
4202 entry
->offset
+ (va
- entry
->vme_start
),
4204 &prot
, &result_page
, &top_page
,
4206 NULL
, map
->no_zero_fill
,
4207 FALSE
, &fault_info
);
4208 } while (result
== VM_FAULT_RETRY
);
4211 * If this was a mapping to a file on a device that has been forcibly
4212 * unmounted, then we won't get a page back from vm_fault_page(). Just
4213 * move on to the next one in case the remaining pages are mapped from
4214 * different objects. During a forced unmount, the object is terminated
4215 * so the alive flag will be false if this happens. A forced unmount will
4216 * will occur when an external disk is unplugged before the user does an
4217 * eject, so we don't want to panic in that situation.
4220 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
)
4223 if (result
!= VM_FAULT_SUCCESS
)
4224 panic("vm_fault_unwire: failure");
4226 result_object
= result_page
->object
;
4229 assert(result_page
->phys_page
!=
4230 vm_page_fictitious_addr
);
4231 pmap_disconnect(result_page
->phys_page
);
4232 VM_PAGE_FREE(result_page
);
4234 if ((pmap
) && (result_page
->phys_page
!= vm_page_guard_addr
))
4235 pmap_change_wiring(pmap
,
4236 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
4239 if (VM_PAGE_WIRED(result_page
)) {
4240 vm_page_lockspin_queues();
4241 vm_page_unwire(result_page
, TRUE
);
4242 vm_page_unlock_queues();
4244 if(entry
->zero_wired_pages
) {
4245 pmap_zero_page(result_page
->phys_page
);
4246 entry
->zero_wired_pages
= FALSE
;
4249 PAGE_WAKEUP_DONE(result_page
);
4251 vm_fault_cleanup(result_object
, top_page
);
4256 * Inform the physical mapping system that the range
4257 * of addresses may fault, so that page tables and
4258 * such may be unwired themselves.
4261 pmap_pageable(pmap
, pmap_addr
,
4262 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
4267 * vm_fault_wire_fast:
4269 * Handle common case of a wire down page fault at the given address.
4270 * If successful, the page is inserted into the associated physical map.
4271 * The map entry is passed in to avoid the overhead of a map lookup.
4273 * NOTE: the given address should be truncated to the
4274 * proper page address.
4276 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
4277 * a standard error specifying why the fault is fatal is returned.
4279 * The map in question must be referenced, and remains so.
4280 * Caller has a read lock on the map.
4282 * This is a stripped version of vm_fault() for wiring pages. Anything
4283 * other than the common case will return KERN_FAILURE, and the caller
4284 * is expected to call vm_fault().
4288 __unused vm_map_t map
,
4290 vm_map_entry_t entry
,
4292 vm_map_offset_t pmap_addr
)
4295 vm_object_offset_t offset
;
4296 register vm_page_t m
;
4298 thread_t thread
= current_thread();
4302 VM_STAT_INCR(faults
);
4304 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
)
4305 thread
->task
->faults
++;
4312 #define RELEASE_PAGE(m) { \
4313 PAGE_WAKEUP_DONE(m); \
4314 vm_page_lockspin_queues(); \
4315 vm_page_unwire(m, TRUE); \
4316 vm_page_unlock_queues(); \
4320 #undef UNLOCK_THINGS
4321 #define UNLOCK_THINGS { \
4322 vm_object_paging_end(object); \
4323 vm_object_unlock(object); \
4326 #undef UNLOCK_AND_DEALLOCATE
4327 #define UNLOCK_AND_DEALLOCATE { \
4329 vm_object_deallocate(object); \
4332 * Give up and have caller do things the hard way.
4336 UNLOCK_AND_DEALLOCATE; \
4337 return(KERN_FAILURE); \
4342 * If this entry is not directly to a vm_object, bail out.
4344 if (entry
->is_sub_map
)
4345 return(KERN_FAILURE
);
4348 * Find the backing store object and offset into it.
4351 object
= entry
->object
.vm_object
;
4352 offset
= (va
- entry
->vme_start
) + entry
->offset
;
4353 prot
= entry
->protection
;
4356 * Make a reference to this object to prevent its
4357 * disposal while we are messing with it.
4360 vm_object_lock(object
);
4361 vm_object_reference_locked(object
);
4362 vm_object_paging_begin(object
);
4365 * INVARIANTS (through entire routine):
4367 * 1) At all times, we must either have the object
4368 * lock or a busy page in some object to prevent
4369 * some other thread from trying to bring in
4372 * 2) Once we have a busy page, we must remove it from
4373 * the pageout queues, so that the pageout daemon
4374 * will not grab it away.
4379 * Look for page in top-level object. If it's not there or
4380 * there's something going on, give up.
4381 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
4382 * decrypt the page before wiring it down.
4384 m
= vm_page_lookup(object
, offset
);
4385 if ((m
== VM_PAGE_NULL
) || (m
->busy
) || (m
->encrypted
) ||
4386 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
4390 ASSERT_PAGE_DECRYPTED(m
);
4392 if (m
->fictitious
&&
4393 m
->phys_page
== vm_page_guard_addr
) {
4395 * Guard pages are fictitious pages and are never
4396 * entered into a pmap, so let's say it's been wired...
4403 * Wire the page down now. All bail outs beyond this
4404 * point must unwire the page.
4407 vm_page_lockspin_queues();
4409 vm_page_unlock_queues();
4412 * Mark page busy for other threads.
4419 * Give up if the page is being written and there's a copy object
4421 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
4427 * Put this page into the physical map.
4429 type_of_fault
= DBG_CACHE_HIT_FAULT
;
4430 kr
= vm_fault_enter(m
,
4444 * Unlock everything, and return
4447 PAGE_WAKEUP_DONE(m
);
4448 UNLOCK_AND_DEALLOCATE
;
4455 * Routine: vm_fault_copy_cleanup
4457 * Release a page used by vm_fault_copy.
4461 vm_fault_copy_cleanup(
4465 vm_object_t object
= page
->object
;
4467 vm_object_lock(object
);
4468 PAGE_WAKEUP_DONE(page
);
4469 if (!page
->active
&& !page
->inactive
&& !page
->throttled
) {
4470 vm_page_lockspin_queues();
4471 if (!page
->active
&& !page
->inactive
&& !page
->throttled
)
4472 vm_page_activate(page
);
4473 vm_page_unlock_queues();
4475 vm_fault_cleanup(object
, top_page
);
4479 vm_fault_copy_dst_cleanup(
4484 if (page
!= VM_PAGE_NULL
) {
4485 object
= page
->object
;
4486 vm_object_lock(object
);
4487 vm_page_lockspin_queues();
4488 vm_page_unwire(page
, TRUE
);
4489 vm_page_unlock_queues();
4490 vm_object_paging_end(object
);
4491 vm_object_unlock(object
);
4496 * Routine: vm_fault_copy
4499 * Copy pages from one virtual memory object to another --
4500 * neither the source nor destination pages need be resident.
4502 * Before actually copying a page, the version associated with
4503 * the destination address map wil be verified.
4505 * In/out conditions:
4506 * The caller must hold a reference, but not a lock, to
4507 * each of the source and destination objects and to the
4511 * Returns KERN_SUCCESS if no errors were encountered in
4512 * reading or writing the data. Returns KERN_INTERRUPTED if
4513 * the operation was interrupted (only possible if the
4514 * "interruptible" argument is asserted). Other return values
4515 * indicate a permanent error in copying the data.
4517 * The actual amount of data copied will be returned in the
4518 * "copy_size" argument. In the event that the destination map
4519 * verification failed, this amount may be less than the amount
4524 vm_object_t src_object
,
4525 vm_object_offset_t src_offset
,
4526 vm_map_size_t
*copy_size
, /* INOUT */
4527 vm_object_t dst_object
,
4528 vm_object_offset_t dst_offset
,
4530 vm_map_version_t
*dst_version
,
4533 vm_page_t result_page
;
4536 vm_page_t src_top_page
;
4540 vm_page_t dst_top_page
;
4543 vm_map_size_t amount_left
;
4544 vm_object_t old_copy_object
;
4545 kern_return_t error
= 0;
4546 vm_fault_return_t result
;
4548 vm_map_size_t part_size
;
4549 struct vm_object_fault_info fault_info_src
;
4550 struct vm_object_fault_info fault_info_dst
;
4553 * In order not to confuse the clustered pageins, align
4554 * the different offsets on a page boundary.
4559 *copy_size -= amount_left; \
4563 amount_left
= *copy_size
;
4565 fault_info_src
.interruptible
= interruptible
;
4566 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
4567 fault_info_src
.user_tag
= 0;
4568 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
4569 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
4570 fault_info_src
.no_cache
= FALSE
;
4571 fault_info_src
.stealth
= TRUE
;
4572 fault_info_src
.io_sync
= FALSE
;
4573 fault_info_src
.cs_bypass
= FALSE
;
4574 fault_info_src
.mark_zf_absent
= FALSE
;
4575 fault_info_src
.batch_pmap_op
= FALSE
;
4577 fault_info_dst
.interruptible
= interruptible
;
4578 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
4579 fault_info_dst
.user_tag
= 0;
4580 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
4581 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
4582 fault_info_dst
.no_cache
= FALSE
;
4583 fault_info_dst
.stealth
= TRUE
;
4584 fault_info_dst
.io_sync
= FALSE
;
4585 fault_info_dst
.cs_bypass
= FALSE
;
4586 fault_info_dst
.mark_zf_absent
= FALSE
;
4587 fault_info_dst
.batch_pmap_op
= FALSE
;
4589 do { /* while (amount_left > 0) */
4591 * There may be a deadlock if both source and destination
4592 * pages are the same. To avoid this deadlock, the copy must
4593 * start by getting the destination page in order to apply
4594 * COW semantics if any.
4597 RetryDestinationFault
: ;
4599 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
4601 vm_object_lock(dst_object
);
4602 vm_object_paging_begin(dst_object
);
4604 if (amount_left
> (vm_size_t
) -1) {
4605 /* 32-bit overflow */
4606 fault_info_dst
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
4608 fault_info_dst
.cluster_size
= (vm_size_t
) amount_left
;
4609 assert(fault_info_dst
.cluster_size
== amount_left
);
4612 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
4613 result
= vm_fault_page(dst_object
,
4614 vm_object_trunc_page(dst_offset
),
4615 VM_PROT_WRITE
|VM_PROT_READ
,
4617 &dst_prot
, &dst_page
, &dst_top_page
,
4620 dst_map
->no_zero_fill
,
4621 FALSE
, &fault_info_dst
);
4623 case VM_FAULT_SUCCESS
:
4625 case VM_FAULT_RETRY
:
4626 goto RetryDestinationFault
;
4627 case VM_FAULT_MEMORY_SHORTAGE
:
4628 if (vm_page_wait(interruptible
))
4629 goto RetryDestinationFault
;
4631 case VM_FAULT_INTERRUPTED
:
4632 RETURN(MACH_SEND_INTERRUPTED
);
4633 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
4634 /* success but no VM page: fail the copy */
4635 vm_object_paging_end(dst_object
);
4636 vm_object_unlock(dst_object
);
4638 case VM_FAULT_MEMORY_ERROR
:
4642 return(KERN_MEMORY_ERROR
);
4644 panic("vm_fault_copy: unexpected error 0x%x from "
4645 "vm_fault_page()\n", result
);
4647 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
4649 old_copy_object
= dst_page
->object
->copy
;
4652 * There exists the possiblity that the source and
4653 * destination page are the same. But we can't
4654 * easily determine that now. If they are the
4655 * same, the call to vm_fault_page() for the
4656 * destination page will deadlock. To prevent this we
4657 * wire the page so we can drop busy without having
4658 * the page daemon steal the page. We clean up the
4659 * top page but keep the paging reference on the object
4660 * holding the dest page so it doesn't go away.
4663 vm_page_lockspin_queues();
4664 vm_page_wire(dst_page
);
4665 vm_page_unlock_queues();
4666 PAGE_WAKEUP_DONE(dst_page
);
4667 vm_object_unlock(dst_page
->object
);
4669 if (dst_top_page
!= VM_PAGE_NULL
) {
4670 vm_object_lock(dst_object
);
4671 VM_PAGE_FREE(dst_top_page
);
4672 vm_object_paging_end(dst_object
);
4673 vm_object_unlock(dst_object
);
4678 if (src_object
== VM_OBJECT_NULL
) {
4680 * No source object. We will just
4681 * zero-fill the page in dst_object.
4683 src_page
= VM_PAGE_NULL
;
4684 result_page
= VM_PAGE_NULL
;
4686 vm_object_lock(src_object
);
4687 src_page
= vm_page_lookup(src_object
,
4688 vm_object_trunc_page(src_offset
));
4689 if (src_page
== dst_page
) {
4690 src_prot
= dst_prot
;
4691 result_page
= VM_PAGE_NULL
;
4693 src_prot
= VM_PROT_READ
;
4694 vm_object_paging_begin(src_object
);
4696 if (amount_left
> (vm_size_t
) -1) {
4697 /* 32-bit overflow */
4698 fault_info_src
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
4700 fault_info_src
.cluster_size
= (vm_size_t
) amount_left
;
4701 assert(fault_info_src
.cluster_size
== amount_left
);
4705 "vm_fault_copy(2) -> vm_fault_page\n",
4707 result
= vm_fault_page(
4709 vm_object_trunc_page(src_offset
),
4710 VM_PROT_READ
, FALSE
,
4712 &result_page
, &src_top_page
,
4713 (int *)0, &error
, FALSE
,
4714 FALSE
, &fault_info_src
);
4717 case VM_FAULT_SUCCESS
:
4719 case VM_FAULT_RETRY
:
4720 goto RetrySourceFault
;
4721 case VM_FAULT_MEMORY_SHORTAGE
:
4722 if (vm_page_wait(interruptible
))
4723 goto RetrySourceFault
;
4725 case VM_FAULT_INTERRUPTED
:
4726 vm_fault_copy_dst_cleanup(dst_page
);
4727 RETURN(MACH_SEND_INTERRUPTED
);
4728 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
4729 /* success but no VM page: fail */
4730 vm_object_paging_end(src_object
);
4731 vm_object_unlock(src_object
);
4733 case VM_FAULT_MEMORY_ERROR
:
4734 vm_fault_copy_dst_cleanup(dst_page
);
4738 return(KERN_MEMORY_ERROR
);
4740 panic("vm_fault_copy(2): unexpected "
4742 "vm_fault_page()\n", result
);
4746 assert((src_top_page
== VM_PAGE_NULL
) ==
4747 (result_page
->object
== src_object
));
4749 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
4750 vm_object_unlock(result_page
->object
);
4753 if (!vm_map_verify(dst_map
, dst_version
)) {
4754 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4755 vm_fault_copy_cleanup(result_page
, src_top_page
);
4756 vm_fault_copy_dst_cleanup(dst_page
);
4760 vm_object_lock(dst_page
->object
);
4762 if (dst_page
->object
->copy
!= old_copy_object
) {
4763 vm_object_unlock(dst_page
->object
);
4764 vm_map_verify_done(dst_map
, dst_version
);
4765 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4766 vm_fault_copy_cleanup(result_page
, src_top_page
);
4767 vm_fault_copy_dst_cleanup(dst_page
);
4770 vm_object_unlock(dst_page
->object
);
4773 * Copy the page, and note that it is dirty
4777 if (!page_aligned(src_offset
) ||
4778 !page_aligned(dst_offset
) ||
4779 !page_aligned(amount_left
)) {
4781 vm_object_offset_t src_po
,
4784 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
4785 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
4787 if (dst_po
> src_po
) {
4788 part_size
= PAGE_SIZE
- dst_po
;
4790 part_size
= PAGE_SIZE
- src_po
;
4792 if (part_size
> (amount_left
)){
4793 part_size
= amount_left
;
4796 if (result_page
== VM_PAGE_NULL
) {
4797 assert((vm_offset_t
) dst_po
== dst_po
);
4798 assert((vm_size_t
) part_size
== part_size
);
4799 vm_page_part_zero_fill(dst_page
,
4800 (vm_offset_t
) dst_po
,
4801 (vm_size_t
) part_size
);
4803 assert((vm_offset_t
) src_po
== src_po
);
4804 assert((vm_offset_t
) dst_po
== dst_po
);
4805 assert((vm_size_t
) part_size
== part_size
);
4806 vm_page_part_copy(result_page
,
4807 (vm_offset_t
) src_po
,
4809 (vm_offset_t
) dst_po
,
4810 (vm_size_t
)part_size
);
4811 if(!dst_page
->dirty
){
4812 vm_object_lock(dst_object
);
4813 SET_PAGE_DIRTY(dst_page
, TRUE
);
4814 vm_object_unlock(dst_page
->object
);
4819 part_size
= PAGE_SIZE
;
4821 if (result_page
== VM_PAGE_NULL
)
4822 vm_page_zero_fill(dst_page
);
4824 vm_object_lock(result_page
->object
);
4825 vm_page_copy(result_page
, dst_page
);
4826 vm_object_unlock(result_page
->object
);
4828 if(!dst_page
->dirty
){
4829 vm_object_lock(dst_object
);
4830 SET_PAGE_DIRTY(dst_page
, TRUE
);
4831 vm_object_unlock(dst_page
->object
);
4838 * Unlock everything, and return
4841 vm_map_verify_done(dst_map
, dst_version
);
4843 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4844 vm_fault_copy_cleanup(result_page
, src_top_page
);
4845 vm_fault_copy_dst_cleanup(dst_page
);
4847 amount_left
-= part_size
;
4848 src_offset
+= part_size
;
4849 dst_offset
+= part_size
;
4850 } while (amount_left
> 0);
4852 RETURN(KERN_SUCCESS
);
4858 #if VM_FAULT_CLASSIFY
4860 * Temporary statistics gathering support.
4864 * Statistics arrays:
4866 #define VM_FAULT_TYPES_MAX 5
4867 #define VM_FAULT_LEVEL_MAX 8
4869 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
4871 #define VM_FAULT_TYPE_ZERO_FILL 0
4872 #define VM_FAULT_TYPE_MAP_IN 1
4873 #define VM_FAULT_TYPE_PAGER 2
4874 #define VM_FAULT_TYPE_COPY 3
4875 #define VM_FAULT_TYPE_OTHER 4
4879 vm_fault_classify(vm_object_t object
,
4880 vm_object_offset_t offset
,
4881 vm_prot_t fault_type
)
4883 int type
, level
= 0;
4887 m
= vm_page_lookup(object
, offset
);
4888 if (m
!= VM_PAGE_NULL
) {
4889 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
) {
4890 type
= VM_FAULT_TYPE_OTHER
;
4893 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
4894 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
4895 type
= VM_FAULT_TYPE_MAP_IN
;
4898 type
= VM_FAULT_TYPE_COPY
;
4902 if (object
->pager_created
) {
4903 type
= VM_FAULT_TYPE_PAGER
;
4906 if (object
->shadow
== VM_OBJECT_NULL
) {
4907 type
= VM_FAULT_TYPE_ZERO_FILL
;
4911 offset
+= object
->vo_shadow_offset
;
4912 object
= object
->shadow
;
4918 if (level
> VM_FAULT_LEVEL_MAX
)
4919 level
= VM_FAULT_LEVEL_MAX
;
4921 vm_fault_stats
[type
][level
] += 1;
4926 /* cleanup routine to call from debugger */
4929 vm_fault_classify_init(void)
4933 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
4934 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
4935 vm_fault_stats
[type
][level
] = 0;
4941 #endif /* VM_FAULT_CLASSIFY */
4944 extern int cs_validation
;
4947 vm_page_validate_cs_mapped(
4952 vm_object_offset_t offset
;
4954 memory_object_t pager
;
4956 boolean_t validated
, tainted
;
4959 vm_object_lock_assert_exclusive(page
->object
);
4961 if (!cs_validation
) {
4965 if (page
->wpmapped
&& !page
->cs_tainted
) {
4967 * This page was mapped for "write" access sometime in the
4968 * past and could still be modifiable in the future.
4969 * Consider it tainted.
4970 * [ If the page was already found to be "tainted", no
4971 * need to re-validate. ]
4973 page
->cs_validated
= TRUE
;
4974 page
->cs_tainted
= TRUE
;
4976 printf("CODESIGNING: vm_page_validate_cs: "
4977 "page %p obj %p off 0x%llx "
4979 page
, page
->object
, page
->offset
);
4981 vm_cs_validated_dirtied
++;
4984 if (page
->cs_validated
) {
4990 object
= page
->object
;
4991 assert(object
->code_signed
);
4992 offset
= page
->offset
;
4994 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
4996 * The object is terminating and we don't have its pager
4997 * so we can't validate the data...
5002 * Since we get here to validate a page that was brought in by
5003 * the pager, we know that this pager is all setup and ready
5006 assert(!object
->internal
);
5007 assert(object
->pager
!= NULL
);
5008 assert(object
->pager_ready
);
5010 pager
= object
->pager
;
5011 assert(object
->paging_in_progress
);
5012 kr
= vnode_pager_get_object_cs_blobs(pager
, &blobs
);
5013 if (kr
!= KERN_SUCCESS
) {
5017 /* verify the SHA1 hash for this page */
5018 validated
= cs_validate_page(blobs
,
5020 offset
+ object
->paging_offset
,
5021 (const void *)kaddr
,
5024 page
->cs_validated
= validated
;
5026 page
->cs_tainted
= tainted
;
5031 vm_page_validate_cs(
5035 vm_object_offset_t offset
;
5036 vm_map_offset_t koffset
;
5037 vm_map_size_t ksize
;
5040 boolean_t busy_page
;
5042 vm_object_lock_assert_held(page
->object
);
5044 if (!cs_validation
) {
5048 if (page
->wpmapped
&& !page
->cs_tainted
) {
5049 vm_object_lock_assert_exclusive(page
->object
);
5052 * This page was mapped for "write" access sometime in the
5053 * past and could still be modifiable in the future.
5054 * Consider it tainted.
5055 * [ If the page was already found to be "tainted", no
5056 * need to re-validate. ]
5058 page
->cs_validated
= TRUE
;
5059 page
->cs_tainted
= TRUE
;
5061 printf("CODESIGNING: vm_page_validate_cs: "
5062 "page %p obj %p off 0x%llx "
5064 page
, page
->object
, page
->offset
);
5066 vm_cs_validated_dirtied
++;
5069 if (page
->cs_validated
) {
5073 #if CHECK_CS_VALIDATION_BITMAP
5074 if ( vnode_pager_cs_check_validation_bitmap( page
->object
->pager
, trunc_page(page
->offset
+ page
->object
->paging_offset
), CS_BITMAP_CHECK
) == KERN_SUCCESS
) {
5075 page
->cs_validated
= TRUE
;
5076 page
->cs_tainted
= FALSE
;
5077 vm_cs_bitmap_validated
++;
5081 vm_object_lock_assert_exclusive(page
->object
);
5083 object
= page
->object
;
5084 assert(object
->code_signed
);
5085 offset
= page
->offset
;
5087 busy_page
= page
->busy
;
5089 /* keep page busy while we map (and unlock) the VM object */
5094 * Take a paging reference on the VM object
5095 * to protect it from collapse or bypass,
5096 * and keep it from disappearing too.
5098 vm_object_paging_begin(object
);
5100 /* map the page in the kernel address space */
5102 ksize
= PAGE_SIZE_64
;
5103 kr
= vm_paging_map_object(&koffset
,
5109 FALSE
); /* can't unlock object ! */
5110 if (kr
!= KERN_SUCCESS
) {
5111 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr
);
5113 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
5115 /* validate the mapped page */
5116 vm_page_validate_cs_mapped(page
, (const void *) kaddr
);
5118 #if CHECK_CS_VALIDATION_BITMAP
5119 if ( page
->cs_validated
== TRUE
&& page
->cs_tainted
== FALSE
) {
5120 vnode_pager_cs_check_validation_bitmap( object
->pager
, trunc_page( offset
+ object
->paging_offset
), CS_BITMAP_SET
);
5124 assert(object
== page
->object
);
5125 vm_object_lock_assert_exclusive(object
);
5128 PAGE_WAKEUP_DONE(page
);
5131 /* unmap the map from the kernel address space */
5132 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
5137 vm_object_paging_end(object
);