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>
90 #include <kern/policy_internal.h>
92 #include <vm/vm_compressor.h>
93 #include <vm/vm_compressor_pager.h>
94 #include <vm/vm_fault.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_kern.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_protos.h>
102 #include <vm/vm_external.h>
103 #include <vm/memory_object.h>
104 #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
105 #include <vm/vm_shared_region.h>
107 #include <sys/codesign.h>
108 #include <sys/reason.h>
109 #include <sys/signalvar.h>
111 #include <san/kasan.h>
113 #define VM_FAULT_CLASSIFY 0
115 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
117 unsigned int vm_object_pagein_throttle
= 16;
120 * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
121 * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts
122 * of memory if they're buggy and can run the system completely out of swap space. If this happens, we
123 * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps
124 * keep the UI active so that the user has a chance to kill the offending task before the system
127 * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
128 * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold
129 * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a
130 * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
133 extern void throttle_lowpri_io(int);
135 extern struct vnode
*vnode_pager_lookup_vnode(memory_object_t
);
137 uint64_t vm_hard_throttle_threshold
;
141 #define NEED_TO_HARD_THROTTLE_THIS_TASK() (vm_wants_task_throttled(current_task()) || \
142 ((vm_page_free_count < vm_page_throttle_limit || \
143 HARD_THROTTLE_LIMIT_REACHED()) && \
144 proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO) >= THROTTLE_LEVEL_THROTTLED))
147 #define HARD_THROTTLE_DELAY 10000 /* 10000 us == 10 ms */
148 #define SOFT_THROTTLE_DELAY 200 /* 200 us == .2 ms */
150 #define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS 6
151 #define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC 20000
154 boolean_t
current_thread_aborted(void);
156 /* Forward declarations of internal routines. */
157 static kern_return_t
vm_fault_wire_fast(
162 vm_map_entry_t entry
,
164 vm_map_offset_t pmap_addr
,
165 ppnum_t
*physpage_p
);
167 static kern_return_t
vm_fault_internal(
169 vm_map_offset_t vaddr
,
170 vm_prot_t caller_prot
,
171 boolean_t change_wiring
,
175 vm_map_offset_t pmap_addr
,
176 ppnum_t
*physpage_p
);
178 static void vm_fault_copy_cleanup(
182 static void vm_fault_copy_dst_cleanup(
185 #if VM_FAULT_CLASSIFY
186 extern void vm_fault_classify(vm_object_t object
,
187 vm_object_offset_t offset
,
188 vm_prot_t fault_type
);
190 extern void vm_fault_classify_init(void);
193 unsigned long vm_pmap_enter_blocked
= 0;
194 unsigned long vm_pmap_enter_retried
= 0;
196 unsigned long vm_cs_validates
= 0;
197 unsigned long vm_cs_revalidates
= 0;
198 unsigned long vm_cs_query_modified
= 0;
199 unsigned long vm_cs_validated_dirtied
= 0;
200 unsigned long vm_cs_bitmap_validated
= 0;
202 uint64_t vm_cs_defer_to_pmap_cs
= 0;
203 uint64_t vm_cs_defer_to_pmap_cs_not
= 0;
206 void vm_pre_fault(vm_map_offset_t
);
208 extern char *kdp_compressor_decompressed_page
;
209 extern addr64_t kdp_compressor_decompressed_page_paddr
;
210 extern ppnum_t kdp_compressor_decompressed_page_ppnum
;
216 vm_rtfault_record_t
*vm_rtf_records
;
218 #define VMRTF_DEFAULT_BUFSIZE (4096)
219 #define VMRTF_NUM_RECORDS_DEFAULT (VMRTF_DEFAULT_BUFSIZE / sizeof(vm_rtfault_record_t))
220 int vmrtf_num_records
= VMRTF_NUM_RECORDS_DEFAULT
;
222 static void vm_rtfrecord_lock(void);
223 static void vm_rtfrecord_unlock(void);
224 static void vm_record_rtfault(thread_t
, uint64_t, vm_map_offset_t
, int);
226 lck_spin_t vm_rtfr_slock
;
227 extern lck_grp_t vm_page_lck_grp_bucket
;
228 extern lck_attr_t vm_page_lck_attr
;
231 * Routine: vm_fault_init
233 * Initialize our private data structures.
238 int i
, vm_compressor_temp
;
239 boolean_t need_default_val
= TRUE
;
241 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
242 * computed as a percentage of available memory, and the percentage used is scaled inversely with
243 * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems
244 * and reduce the value down to 10% for very large memory configurations. This helps give us a
245 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
246 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
249 vm_hard_throttle_threshold
= sane_size
* (35 - MIN((int)(sane_size
/ (1024 * 1024 * 1024)), 25)) / 100;
252 * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry.
255 if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp
, sizeof(vm_compressor_temp
))) {
256 for (i
= 0; i
< VM_PAGER_MAX_MODES
; i
++) {
257 if (vm_compressor_temp
> 0 &&
258 ((vm_compressor_temp
& (1 << i
)) == vm_compressor_temp
)) {
259 need_default_val
= FALSE
;
260 vm_compressor_mode
= vm_compressor_temp
;
264 if (need_default_val
) {
265 printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp
);
268 if (need_default_val
) {
269 /* If no boot arg or incorrect boot arg, try device tree. */
270 PE_get_default("kern.vm_compressor", &vm_compressor_mode
, sizeof(vm_compressor_mode
));
272 printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode
);
276 vm_rtfault_record_init(void)
278 PE_parse_boot_argn("vm_rtfault_records", &vmrtf_num_records
, sizeof(vmrtf_num_records
));
280 assert(vmrtf_num_records
>= 1);
281 vmrtf_num_records
= MAX(vmrtf_num_records
, 1);
282 size_t kallocsz
= vmrtf_num_records
* sizeof(vm_rtfault_record_t
);
283 vmrtfrs
.vm_rtf_records
= kalloc(kallocsz
);
284 bzero(vmrtfrs
.vm_rtf_records
, kallocsz
);
285 vmrtfrs
.vmrtfr_maxi
= vmrtf_num_records
- 1;
286 lck_spin_init(&vm_rtfr_slock
, &vm_page_lck_grp_bucket
, &vm_page_lck_attr
);
289 * Routine: vm_fault_cleanup
291 * Clean up the result of vm_fault_page.
293 * The paging reference for "object" is released.
294 * "object" is unlocked.
295 * If "top_page" is not null, "top_page" is
296 * freed and the paging reference for the object
297 * containing it is released.
300 * "object" must be locked.
307 vm_object_paging_end(object
);
308 vm_object_unlock(object
);
310 if (top_page
!= VM_PAGE_NULL
) {
311 object
= VM_PAGE_OBJECT(top_page
);
313 vm_object_lock(object
);
314 VM_PAGE_FREE(top_page
);
315 vm_object_paging_end(object
);
316 vm_object_unlock(object
);
320 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
323 boolean_t vm_page_deactivate_behind
= TRUE
;
325 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
327 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
328 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
329 /* we use it to size an array on the stack */
331 int vm_default_behind
= VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW
;
333 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
336 * vm_page_is_sequential
338 * Determine if sequential access is in progress
339 * in accordance with the behavior specified.
340 * Update state to indicate current access pattern.
342 * object must have at least the shared lock held
346 vm_fault_is_sequential(
348 vm_object_offset_t offset
,
349 vm_behavior_t behavior
)
351 vm_object_offset_t last_alloc
;
355 last_alloc
= object
->last_alloc
;
356 sequential
= object
->sequential
;
357 orig_sequential
= sequential
;
360 case VM_BEHAVIOR_RANDOM
:
362 * reset indicator of sequential behavior
367 case VM_BEHAVIOR_SEQUENTIAL
:
368 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
370 * advance indicator of sequential behavior
372 if (sequential
< MAX_SEQUENTIAL_RUN
) {
373 sequential
+= PAGE_SIZE
;
377 * reset indicator of sequential behavior
383 case VM_BEHAVIOR_RSEQNTL
:
384 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
386 * advance indicator of sequential behavior
388 if (sequential
> -MAX_SEQUENTIAL_RUN
) {
389 sequential
-= PAGE_SIZE
;
393 * reset indicator of sequential behavior
399 case VM_BEHAVIOR_DEFAULT
:
401 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
403 * advance indicator of sequential behavior
405 if (sequential
< 0) {
408 if (sequential
< MAX_SEQUENTIAL_RUN
) {
409 sequential
+= PAGE_SIZE
;
411 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
413 * advance indicator of sequential behavior
415 if (sequential
> 0) {
418 if (sequential
> -MAX_SEQUENTIAL_RUN
) {
419 sequential
-= PAGE_SIZE
;
423 * reset indicator of sequential behavior
429 if (sequential
!= orig_sequential
) {
430 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
432 * if someone else has already updated object->sequential
433 * don't bother trying to update it or object->last_alloc
439 * I'd like to do this with a OSCompareAndSwap64, but that
440 * doesn't exist for PPC... however, it shouldn't matter
441 * that much... last_alloc is maintained so that we can determine
442 * if a sequential access pattern is taking place... if only
443 * one thread is banging on this object, no problem with the unprotected
444 * update... if 2 or more threads are banging away, we run the risk of
445 * someone seeing a mangled update... however, in the face of multiple
446 * accesses, no sequential access pattern can develop anyway, so we
447 * haven't lost any real info.
449 object
->last_alloc
= offset
;
453 int vm_page_deactivate_behind_count
= 0;
456 * vm_page_deactivate_behind
458 * Determine if sequential access is in progress
459 * in accordance with the behavior specified. If
460 * so, compute a potential page to deactivate and
463 * object must be locked.
465 * return TRUE if we actually deactivate a page
469 vm_fault_deactivate_behind(
471 vm_object_offset_t offset
,
472 vm_behavior_t behavior
)
475 int pages_in_run
= 0;
476 int max_pages_in_run
= 0;
478 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
479 vm_object_offset_t run_offset
= 0;
480 vm_object_offset_t pg_offset
= 0;
482 vm_page_t page_run
[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
];
486 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
489 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
491 * Do not deactivate pages from the kernel object: they
492 * are not intended to become pageable.
493 * or we've disabled the deactivate behind mechanism
497 if ((sequential_run
= object
->sequential
)) {
498 if (sequential_run
< 0) {
499 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
500 sequential_run
= 0 - sequential_run
;
502 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
506 case VM_BEHAVIOR_RANDOM
:
508 case VM_BEHAVIOR_SEQUENTIAL
:
509 if (sequential_run
>= (int)PAGE_SIZE
) {
510 run_offset
= 0 - PAGE_SIZE_64
;
511 max_pages_in_run
= 1;
514 case VM_BEHAVIOR_RSEQNTL
:
515 if (sequential_run
>= (int)PAGE_SIZE
) {
516 run_offset
= PAGE_SIZE_64
;
517 max_pages_in_run
= 1;
520 case VM_BEHAVIOR_DEFAULT
:
522 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
525 * determine if the run of sequential accesss has been
526 * long enough on an object with default access behavior
527 * to consider it for deactivation
529 if ((uint64_t)sequential_run
>= behind
&& (sequential_run
% (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
* PAGE_SIZE
)) == 0) {
531 * the comparisons between offset and behind are done
532 * in this kind of odd fashion in order to prevent wrap around
535 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
536 if (offset
>= behind
) {
537 run_offset
= 0 - behind
;
538 pg_offset
= PAGE_SIZE_64
;
539 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
542 if (offset
< -behind
) {
544 pg_offset
= 0 - PAGE_SIZE_64
;
545 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
551 for (n
= 0; n
< max_pages_in_run
; n
++) {
552 m
= vm_page_lookup(object
, offset
+ run_offset
+ (n
* pg_offset
));
554 if (m
&& !m
->vmp_laundry
&& !m
->vmp_busy
&& !m
->vmp_no_cache
&& (m
->vmp_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && !m
->vmp_fictitious
&& !m
->vmp_absent
) {
555 page_run
[pages_in_run
++] = m
;
558 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
560 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
561 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
562 * new reference happens. If no futher references happen on the page after that remote TLB flushes
563 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
564 * by pageout_scan, which is just fine since the last reference would have happened quite far
565 * in the past (TLB caches don't hang around for very long), and of course could just as easily
566 * have happened before we did the deactivate_behind.
568 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m
), VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
572 vm_page_lockspin_queues();
574 for (n
= 0; n
< pages_in_run
; n
++) {
577 vm_page_deactivate_internal(m
, FALSE
);
579 vm_page_deactivate_behind_count
++;
581 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
584 vm_page_unlock_queues();
592 #if (DEVELOPMENT || DEBUG)
593 uint32_t vm_page_creation_throttled_hard
= 0;
594 uint32_t vm_page_creation_throttled_soft
= 0;
595 uint64_t vm_page_creation_throttle_avoided
= 0;
596 #endif /* DEVELOPMENT || DEBUG */
599 vm_page_throttled(boolean_t page_kept
)
601 clock_sec_t elapsed_sec
;
603 clock_usec_t tv_usec
;
605 thread_t thread
= current_thread();
607 if (thread
->options
& TH_OPT_VMPRIV
) {
611 if (thread
->t_page_creation_throttled
) {
612 thread
->t_page_creation_throttled
= 0;
614 if (page_kept
== FALSE
) {
618 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
619 #if (DEVELOPMENT || DEBUG)
620 thread
->t_page_creation_throttled_hard
++;
621 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
622 #endif /* DEVELOPMENT || DEBUG */
623 return HARD_THROTTLE_DELAY
;
626 if ((vm_page_free_count
< vm_page_throttle_limit
|| (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& SWAPPER_NEEDS_TO_UNTHROTTLE())) &&
627 thread
->t_page_creation_count
> (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
* VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
)) {
628 if (vm_page_free_wanted
== 0 && vm_page_free_wanted_privileged
== 0) {
629 #if (DEVELOPMENT || DEBUG)
630 OSAddAtomic64(1, &vm_page_creation_throttle_avoided
);
634 clock_get_system_microtime(&tv_sec
, &tv_usec
);
636 elapsed_sec
= tv_sec
- thread
->t_page_creation_time
;
638 if (elapsed_sec
<= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
||
639 (thread
->t_page_creation_count
/ elapsed_sec
) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
) {
640 if (elapsed_sec
>= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
)) {
642 * we'll reset our stats to give a well behaved app
643 * that was unlucky enough to accumulate a bunch of pages
644 * over a long period of time a chance to get out of
645 * the throttled state... we reset the counter and timestamp
646 * so that if it stays under the rate limit for the next second
647 * it will be back in our good graces... if it exceeds it, it
648 * will remain in the throttled state
650 thread
->t_page_creation_time
= tv_sec
;
651 thread
->t_page_creation_count
= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
* (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
- 1);
653 VM_PAGEOUT_DEBUG(vm_page_throttle_count
, 1);
655 thread
->t_page_creation_throttled
= 1;
657 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& HARD_THROTTLE_LIMIT_REACHED()) {
658 #if (DEVELOPMENT || DEBUG)
659 thread
->t_page_creation_throttled_hard
++;
660 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
661 #endif /* DEVELOPMENT || DEBUG */
662 return HARD_THROTTLE_DELAY
;
664 #if (DEVELOPMENT || DEBUG)
665 thread
->t_page_creation_throttled_soft
++;
666 OSAddAtomic(1, &vm_page_creation_throttled_soft
);
667 #endif /* DEVELOPMENT || DEBUG */
668 return SOFT_THROTTLE_DELAY
;
671 thread
->t_page_creation_time
= tv_sec
;
672 thread
->t_page_creation_count
= 0;
675 thread
->t_page_creation_count
++;
682 * check for various conditions that would
683 * prevent us from creating a ZF page...
684 * cleanup is based on being called from vm_fault_page
686 * object must be locked
687 * object == m->vmp_object
689 static vm_fault_return_t
690 vm_fault_check(vm_object_t object
, vm_page_t m
, vm_page_t first_m
, wait_interrupt_t interruptible_state
, boolean_t page_throttle
)
694 if (object
->shadow_severed
||
695 VM_OBJECT_PURGEABLE_FAULT_ERROR(object
)) {
698 * 1. the shadow chain was severed,
699 * 2. the purgeable object is volatile or empty and is marked
700 * to fault on access while volatile.
701 * Just have to return an error at this point
703 if (m
!= VM_PAGE_NULL
) {
706 vm_fault_cleanup(object
, first_m
);
708 thread_interrupt_level(interruptible_state
);
710 return VM_FAULT_MEMORY_ERROR
;
712 if (page_throttle
== TRUE
) {
713 if ((throttle_delay
= vm_page_throttled(FALSE
))) {
715 * we're throttling zero-fills...
716 * treat this as if we couldn't grab a page
718 if (m
!= VM_PAGE_NULL
) {
721 vm_fault_cleanup(object
, first_m
);
723 VM_DEBUG_EVENT(vmf_check_zfdelay
, VMF_CHECK_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
725 delay(throttle_delay
);
727 if (current_thread_aborted()) {
728 thread_interrupt_level(interruptible_state
);
729 return VM_FAULT_INTERRUPTED
;
731 thread_interrupt_level(interruptible_state
);
733 return VM_FAULT_MEMORY_SHORTAGE
;
736 return VM_FAULT_SUCCESS
;
741 * do the work to zero fill a page and
742 * inject it into the correct paging queue
744 * m->vmp_object must be locked
745 * page queue lock must NOT be held
748 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
750 int my_fault
= DBG_ZERO_FILL_FAULT
;
753 object
= VM_PAGE_OBJECT(m
);
756 * This is is a zero-fill page fault...
758 * Checking the page lock is a waste of
759 * time; this page was absent, so
760 * it can't be page locked by a pager.
762 * we also consider it undefined
763 * with respect to instruction
764 * execution. i.e. it is the responsibility
765 * of higher layers to call for an instruction
766 * sync after changing the contents and before
767 * sending a program into this area. We
768 * choose this approach for performance
770 m
->vmp_pmapped
= TRUE
;
772 m
->vmp_cs_validated
= FALSE
;
773 m
->vmp_cs_tainted
= FALSE
;
774 m
->vmp_cs_nx
= FALSE
;
776 if (no_zero_fill
== TRUE
) {
777 my_fault
= DBG_NZF_PAGE_FAULT
;
779 if (m
->vmp_absent
&& m
->vmp_busy
) {
783 vm_page_zero_fill(m
);
785 VM_STAT_INCR(zero_fill_count
);
786 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
788 assert(!m
->vmp_laundry
);
789 assert(object
!= kernel_object
);
790 //assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0);
792 if (!VM_DYNAMIC_PAGING_ENABLED() &&
793 (object
->purgable
== VM_PURGABLE_DENY
||
794 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
795 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
796 vm_page_lockspin_queues();
798 if (!VM_DYNAMIC_PAGING_ENABLED()) {
799 assert(!VM_PAGE_WIRED(m
));
802 * can't be on the pageout queue since we don't
803 * have a pager to try and clean to
805 vm_page_queues_remove(m
, TRUE
);
806 vm_page_check_pageable_safe(m
);
807 vm_page_queue_enter(&vm_page_queue_throttled
, m
, vmp_pageq
);
808 m
->vmp_q_state
= VM_PAGE_ON_THROTTLED_Q
;
809 vm_page_throttled_count
++;
811 vm_page_unlock_queues();
818 * Routine: vm_fault_page
820 * Find the resident page for the virtual memory
821 * specified by the given virtual memory object
823 * Additional arguments:
824 * The required permissions for the page is given
825 * in "fault_type". Desired permissions are included
827 * fault_info is passed along to determine pagein cluster
828 * limits... it contains the expected reference pattern,
829 * cluster size if available, etc...
831 * If the desired page is known to be resident (for
832 * example, because it was previously wired down), asserting
833 * the "unwiring" parameter will speed the search.
835 * If the operation can be interrupted (by thread_abort
836 * or thread_terminate), then the "interruptible"
837 * parameter should be asserted.
840 * The page containing the proper data is returned
844 * The source object must be locked and referenced,
845 * and must donate one paging reference. The reference
846 * is not affected. The paging reference and lock are
849 * If the call succeeds, the object in which "result_page"
850 * resides is left locked and holding a paging reference.
851 * If this is not the original object, a busy page in the
852 * original object is returned in "top_page", to prevent other
853 * callers from pursuing this same data, along with a paging
854 * reference for the original object. The "top_page" should
855 * be destroyed when this guarantee is no longer required.
856 * The "result_page" is also left busy. It is not removed
857 * from the pageout queues.
859 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
860 * fault succeeded but there's no VM page (i.e. the VM object
861 * does not actually hold VM pages, but device memory or
862 * large pages). The object is still locked and we still hold a
863 * paging_in_progress reference.
865 unsigned int vm_fault_page_blocked_access
= 0;
866 unsigned int vm_fault_page_forced_retry
= 0;
871 vm_object_t first_object
, /* Object to begin search */
872 vm_object_offset_t first_offset
, /* Offset into object */
873 vm_prot_t fault_type
, /* What access is requested */
874 boolean_t must_be_resident
,/* Must page be resident? */
875 boolean_t caller_lookup
, /* caller looked up page */
876 /* Modifies in place: */
877 vm_prot_t
*protection
, /* Protection for mapping */
878 vm_page_t
*result_page
, /* Page found, if successful */
880 vm_page_t
*top_page
, /* Page in top object, if
881 * not result_page. */
882 int *type_of_fault
, /* if non-null, fill in with type of fault
883 * COW, zero-fill, etc... returned in trace point */
884 /* More arguments: */
885 kern_return_t
*error_code
, /* code if page is in error */
886 boolean_t no_zero_fill
, /* don't zero fill absent pages */
887 boolean_t data_supply
, /* treat as data_supply if
888 * it is a write fault and a full
889 * page is provided */
890 vm_object_fault_info_t fault_info
)
894 vm_object_offset_t offset
;
896 vm_object_t next_object
;
897 vm_object_t copy_object
;
898 boolean_t look_for_page
;
899 boolean_t force_fault_retry
= FALSE
;
900 vm_prot_t access_required
= fault_type
;
901 vm_prot_t wants_copy_flag
;
902 kern_return_t wait_result
;
903 wait_interrupt_t interruptible_state
;
904 boolean_t data_already_requested
= FALSE
;
905 vm_behavior_t orig_behavior
;
906 vm_size_t orig_cluster_size
;
907 vm_fault_return_t error
;
909 uint32_t try_failed_count
;
910 int interruptible
; /* how may fault be interrupted? */
911 int external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
912 memory_object_t pager
;
913 vm_fault_return_t retval
;
917 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
918 * marked as paged out in the compressor pager or the pager doesn't exist.
919 * Note also that if the pager for an internal object
920 * has not been created, the pager is not invoked regardless of the value
921 * of MUST_ASK_PAGER().
923 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
924 * is marked as paged out in the compressor pager.
925 * PAGED_OUT() is used to determine if a page has already been pushed
926 * into a copy object in order to avoid a redundant page out operation.
928 #define MUST_ASK_PAGER(o, f, s) \
929 ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
931 #define PAGED_OUT(o, f) \
932 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
937 #define RELEASE_PAGE(m) \
939 PAGE_WAKEUP_DONE(m); \
940 if ( !VM_PAGE_PAGEABLE(m)) { \
941 vm_page_lockspin_queues(); \
942 if ( !VM_PAGE_PAGEABLE(m)) { \
943 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE) \
944 vm_page_deactivate(m); \
946 vm_page_activate(m); \
948 vm_page_unlock_queues(); \
953 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
956 interruptible
= fault_info
->interruptible
;
957 interruptible_state
= thread_interrupt_level(interruptible
);
960 * INVARIANTS (through entire routine):
962 * 1) At all times, we must either have the object
963 * lock or a busy page in some object to prevent
964 * some other thread from trying to bring in
967 * Note that we cannot hold any locks during the
968 * pager access or when waiting for memory, so
969 * we use a busy page then.
971 * 2) To prevent another thread from racing us down the
972 * shadow chain and entering a new page in the top
973 * object before we do, we must keep a busy page in
974 * the top object while following the shadow chain.
976 * 3) We must increment paging_in_progress on any object
977 * for which we have a busy page before dropping
980 * 4) We leave busy pages on the pageout queues.
981 * If the pageout daemon comes across a busy page,
982 * it will remove the page from the pageout queues.
985 object
= first_object
;
986 offset
= first_offset
;
987 first_m
= VM_PAGE_NULL
;
988 access_required
= fault_type
;
992 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
993 object
, offset
, fault_type
, *protection
, 0);
996 * default type of fault
998 my_fault
= DBG_CACHE_HIT_FAULT
;
1002 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1006 #if CONFIG_SECLUDED_MEMORY
1007 if (object
->can_grab_secluded
) {
1008 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
1010 #endif /* CONFIG_SECLUDED_MEMORY */
1012 if (!object
->alive
) {
1014 * object is no longer valid
1015 * clean up and return error
1017 vm_fault_cleanup(object
, first_m
);
1018 thread_interrupt_level(interruptible_state
);
1020 return VM_FAULT_MEMORY_ERROR
;
1023 if (!object
->pager_created
&& object
->phys_contiguous
) {
1025 * A physically-contiguous object without a pager:
1026 * must be a "large page" object. We do not deal
1027 * with VM pages for this object.
1029 caller_lookup
= FALSE
;
1031 goto phys_contig_object
;
1034 if (object
->blocked_access
) {
1036 * Access to this VM object has been blocked.
1037 * Replace our "paging_in_progress" reference with
1038 * a "activity_in_progress" reference and wait for
1039 * access to be unblocked.
1041 caller_lookup
= FALSE
; /* no longer valid after sleep */
1042 vm_object_activity_begin(object
);
1043 vm_object_paging_end(object
);
1044 while (object
->blocked_access
) {
1045 vm_object_sleep(object
,
1046 VM_OBJECT_EVENT_UNBLOCKED
,
1049 vm_fault_page_blocked_access
++;
1050 vm_object_paging_begin(object
);
1051 vm_object_activity_end(object
);
1055 * See whether the page at 'offset' is resident
1057 if (caller_lookup
== TRUE
) {
1059 * The caller has already looked up the page
1060 * and gave us the result in "result_page".
1061 * We can use this for the first lookup but
1062 * it loses its validity as soon as we unlock
1066 caller_lookup
= FALSE
; /* no longer valid after that */
1068 m
= vm_page_lookup(object
, offset
);
1071 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1073 if (m
!= VM_PAGE_NULL
) {
1076 * The page is being brought in,
1077 * wait for it and then retry.
1080 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1082 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
1085 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
1088 counter(c_vm_fault_page_block_busy_kernel
++);
1090 if (wait_result
!= THREAD_AWAKENED
) {
1091 vm_fault_cleanup(object
, first_m
);
1092 thread_interrupt_level(interruptible_state
);
1094 if (wait_result
== THREAD_RESTART
) {
1095 return VM_FAULT_RETRY
;
1097 return VM_FAULT_INTERRUPTED
;
1102 if (m
->vmp_laundry
) {
1103 m
->vmp_free_when_done
= FALSE
;
1105 if (!m
->vmp_cleaning
) {
1106 vm_pageout_steal_laundry(m
, FALSE
);
1109 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
1111 * Guard page: off limits !
1113 if (fault_type
== VM_PROT_NONE
) {
1115 * The fault is not requesting any
1116 * access to the guard page, so it must
1117 * be just to wire or unwire it.
1118 * Let's pretend it succeeded...
1122 assert(first_m
== VM_PAGE_NULL
);
1123 *top_page
= first_m
;
1124 if (type_of_fault
) {
1125 *type_of_fault
= DBG_GUARD_FAULT
;
1127 thread_interrupt_level(interruptible_state
);
1128 return VM_FAULT_SUCCESS
;
1131 * The fault requests access to the
1132 * guard page: let's deny that !
1134 vm_fault_cleanup(object
, first_m
);
1135 thread_interrupt_level(interruptible_state
);
1136 return VM_FAULT_MEMORY_ERROR
;
1142 * The page is in error, give up now.
1145 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1148 *error_code
= KERN_MEMORY_ERROR
;
1152 vm_fault_cleanup(object
, first_m
);
1153 thread_interrupt_level(interruptible_state
);
1155 return VM_FAULT_MEMORY_ERROR
;
1157 if (m
->vmp_restart
) {
1159 * The pager wants us to restart
1160 * at the top of the chain,
1161 * typically because it has moved the
1162 * page to another pager, then do so.
1165 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1169 vm_fault_cleanup(object
, first_m
);
1170 thread_interrupt_level(interruptible_state
);
1172 return VM_FAULT_RETRY
;
1174 if (m
->vmp_absent
) {
1176 * The page isn't busy, but is absent,
1177 * therefore it's deemed "unavailable".
1179 * Remove the non-existent page (unless it's
1180 * in the top object) and move on down to the
1181 * next object (if there is one).
1184 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1186 next_object
= object
->shadow
;
1188 if (next_object
== VM_OBJECT_NULL
) {
1190 * Absent page at bottom of shadow
1191 * chain; zero fill the page we left
1192 * busy in the first object, and free
1195 assert(!must_be_resident
);
1198 * check for any conditions that prevent
1199 * us from creating a new zero-fill page
1200 * vm_fault_check will do all of the
1201 * fault cleanup in the case of an error condition
1202 * including resetting the thread_interrupt_level
1204 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1206 if (error
!= VM_FAULT_SUCCESS
) {
1211 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
1216 if (object
!= first_object
) {
1218 * free the absent page we just found
1223 * drop reference and lock on current object
1225 vm_object_paging_end(object
);
1226 vm_object_unlock(object
);
1229 * grab the original page we
1230 * 'soldered' in place and
1231 * retake lock on 'first_object'
1234 first_m
= VM_PAGE_NULL
;
1236 object
= first_object
;
1237 offset
= first_offset
;
1239 vm_object_lock(object
);
1242 * we're going to use the absent page we just found
1243 * so convert it to a 'busy' page
1245 m
->vmp_absent
= FALSE
;
1248 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
) {
1249 m
->vmp_absent
= TRUE
;
1252 * zero-fill the page and put it on
1253 * the correct paging queue
1255 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1259 if (must_be_resident
) {
1260 vm_object_paging_end(object
);
1261 } else if (object
!= first_object
) {
1262 vm_object_paging_end(object
);
1266 m
->vmp_absent
= FALSE
;
1269 vm_page_lockspin_queues();
1270 vm_page_queues_remove(m
, FALSE
);
1271 vm_page_unlock_queues();
1274 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1277 offset
+ object
->vo_shadow_offset
, 0);
1279 offset
+= object
->vo_shadow_offset
;
1280 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1281 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1282 access_required
= VM_PROT_READ
;
1284 vm_object_lock(next_object
);
1285 vm_object_unlock(object
);
1286 object
= next_object
;
1287 vm_object_paging_begin(object
);
1290 * reset to default type of fault
1292 my_fault
= DBG_CACHE_HIT_FAULT
;
1297 if ((m
->vmp_cleaning
)
1298 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1299 && (fault_type
& VM_PROT_WRITE
)) {
1301 * This is a copy-on-write fault that will
1302 * cause us to revoke access to this page, but
1303 * this page is in the process of being cleaned
1304 * in a clustered pageout. We must wait until
1305 * the cleaning operation completes before
1306 * revoking access to the original page,
1307 * otherwise we might attempt to remove a
1311 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1314 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1318 * take an extra ref so that object won't die
1320 vm_object_reference_locked(object
);
1322 vm_fault_cleanup(object
, first_m
);
1324 counter(c_vm_fault_page_block_backoff_kernel
++);
1325 vm_object_lock(object
);
1326 assert(object
->ref_count
> 0);
1328 m
= vm_page_lookup(object
, offset
);
1330 if (m
!= VM_PAGE_NULL
&& m
->vmp_cleaning
) {
1331 PAGE_ASSERT_WAIT(m
, interruptible
);
1333 vm_object_unlock(object
);
1334 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1335 vm_object_deallocate(object
);
1339 vm_object_unlock(object
);
1341 vm_object_deallocate(object
);
1342 thread_interrupt_level(interruptible_state
);
1344 return VM_FAULT_RETRY
;
1347 if (type_of_fault
== NULL
&& (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) &&
1348 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1350 * If we were passed a non-NULL pointer for
1351 * "type_of_fault", than we came from
1352 * vm_fault... we'll let it deal with
1353 * this condition, since it
1354 * needs to see m->vmp_speculative to correctly
1355 * account the pageins, otherwise...
1356 * take it off the speculative queue, we'll
1357 * let the caller of vm_fault_page deal
1358 * with getting it onto the correct queue
1360 * If the caller specified in fault_info that
1361 * it wants a "stealth" fault, we also leave
1362 * the page in the speculative queue.
1364 vm_page_lockspin_queues();
1365 if (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) {
1366 vm_page_queues_remove(m
, FALSE
);
1368 vm_page_unlock_queues();
1370 assert(object
== VM_PAGE_OBJECT(m
));
1372 if (object
->code_signed
) {
1375 * We just paged in a page from a signed
1376 * memory object but we don't need to
1377 * validate it now. We'll validate it if
1378 * when it gets mapped into a user address
1379 * space for the first time or when the page
1380 * gets copied to another object as a result
1381 * of a copy-on-write.
1386 * We mark the page busy and leave it on
1387 * the pageout queues. If the pageout
1388 * deamon comes across it, then it will
1389 * remove the page from the queue, but not the object
1392 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1395 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1396 object
, offset
, m
, 0, 0);
1397 assert(!m
->vmp_busy
);
1398 assert(!m
->vmp_absent
);
1406 * we get here when there is no page present in the object at
1407 * the offset we're interested in... we'll allocate a page
1408 * at this point if the pager associated with
1409 * this object can provide the data or we're the top object...
1410 * object is locked; m == NULL
1413 if (must_be_resident
) {
1414 if (fault_type
== VM_PROT_NONE
&&
1415 object
== kernel_object
) {
1417 * We've been called from vm_fault_unwire()
1418 * while removing a map entry that was allocated
1419 * with KMA_KOBJECT and KMA_VAONLY. This page
1420 * is not present and there's nothing more to
1421 * do here (nothing to unwire).
1423 vm_fault_cleanup(object
, first_m
);
1424 thread_interrupt_level(interruptible_state
);
1426 return VM_FAULT_MEMORY_ERROR
;
1429 goto dont_look_for_page
;
1432 /* Don't expect to fault pages into the kernel object. */
1433 assert(object
!= kernel_object
);
1435 data_supply
= FALSE
;
1437 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
, external_state
) == TRUE
) && !data_supply
);
1440 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1442 if (!look_for_page
&& object
== first_object
&& !object
->phys_contiguous
) {
1444 * Allocate a new page for this object/offset pair as a placeholder
1446 m
= vm_page_grab_options(grab_options
);
1448 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1450 if (m
== VM_PAGE_NULL
) {
1451 vm_fault_cleanup(object
, first_m
);
1452 thread_interrupt_level(interruptible_state
);
1454 return VM_FAULT_MEMORY_SHORTAGE
;
1457 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1458 vm_page_insert_internal(m
, object
, offset
, VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1460 vm_page_insert(m
, object
, offset
);
1463 if (look_for_page
) {
1468 * If the memory manager is not ready, we
1469 * cannot make requests.
1471 if (!object
->pager_ready
) {
1473 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1475 if (m
!= VM_PAGE_NULL
) {
1480 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1481 object
, offset
, 0, 0, 0);
1484 * take an extra ref so object won't die
1486 vm_object_reference_locked(object
);
1487 vm_fault_cleanup(object
, first_m
);
1488 counter(c_vm_fault_page_block_backoff_kernel
++);
1490 vm_object_lock(object
);
1491 assert(object
->ref_count
> 0);
1493 if (!object
->pager_ready
) {
1494 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1496 vm_object_unlock(object
);
1497 if (wait_result
== THREAD_WAITING
) {
1498 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1500 vm_object_deallocate(object
);
1504 vm_object_unlock(object
);
1505 vm_object_deallocate(object
);
1506 thread_interrupt_level(interruptible_state
);
1508 return VM_FAULT_RETRY
;
1511 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1513 * If there are too many outstanding page
1514 * requests pending on this external object, we
1515 * wait for them to be resolved now.
1518 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1520 if (m
!= VM_PAGE_NULL
) {
1524 * take an extra ref so object won't die
1526 vm_object_reference_locked(object
);
1528 vm_fault_cleanup(object
, first_m
);
1530 counter(c_vm_fault_page_block_backoff_kernel
++);
1532 vm_object_lock(object
);
1533 assert(object
->ref_count
> 0);
1535 if (object
->paging_in_progress
>= vm_object_pagein_throttle
) {
1536 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS
, interruptible
);
1538 vm_object_unlock(object
);
1539 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1540 vm_object_deallocate(object
);
1544 vm_object_unlock(object
);
1545 vm_object_deallocate(object
);
1546 thread_interrupt_level(interruptible_state
);
1548 return VM_FAULT_RETRY
;
1551 if (object
->internal
) {
1552 int compressed_count_delta
;
1554 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
1556 if (m
== VM_PAGE_NULL
) {
1558 * Allocate a new page for this object/offset pair as a placeholder
1560 m
= vm_page_grab_options(grab_options
);
1562 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1564 if (m
== VM_PAGE_NULL
) {
1565 vm_fault_cleanup(object
, first_m
);
1566 thread_interrupt_level(interruptible_state
);
1568 return VM_FAULT_MEMORY_SHORTAGE
;
1571 m
->vmp_absent
= TRUE
;
1572 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1573 vm_page_insert_internal(m
, object
, offset
, VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1575 vm_page_insert(m
, object
, offset
);
1578 assert(m
->vmp_busy
);
1580 m
->vmp_absent
= TRUE
;
1581 pager
= object
->pager
;
1583 assert(object
->paging_in_progress
> 0);
1584 vm_object_unlock(object
);
1586 rc
= vm_compressor_pager_get(
1588 offset
+ object
->paging_offset
,
1589 VM_PAGE_GET_PHYS_PAGE(m
),
1592 &compressed_count_delta
);
1594 if (type_of_fault
== NULL
) {
1598 * we weren't called from vm_fault, so we
1599 * need to apply page creation throttling
1600 * do it before we re-acquire any locks
1602 if (my_fault_type
== DBG_COMPRESSOR_FAULT
) {
1603 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
1604 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 1, 0);
1605 delay(throttle_delay
);
1609 vm_object_lock(object
);
1610 assert(object
->paging_in_progress
> 0);
1612 vm_compressor_pager_count(
1614 compressed_count_delta
,
1615 FALSE
, /* shared_lock */
1620 m
->vmp_absent
= FALSE
;
1621 m
->vmp_dirty
= TRUE
;
1622 if ((object
->wimg_bits
&
1624 VM_WIMG_USE_DEFAULT
) {
1626 * If the page is not cacheable,
1627 * we can't let its contents
1628 * linger in the data cache
1629 * after the decompression.
1631 pmap_sync_page_attributes_phys(
1632 VM_PAGE_GET_PHYS_PAGE(m
));
1634 m
->vmp_written_by_kernel
= TRUE
;
1638 * If the object is purgeable, its
1639 * owner's purgeable ledgers have been
1640 * updated in vm_page_insert() but the
1641 * page was also accounted for in a
1642 * "compressed purgeable" ledger, so
1645 if (((object
->purgable
!=
1646 VM_PURGABLE_DENY
) ||
1647 object
->vo_ledger_tag
) &&
1648 (object
->vo_owner
!=
1651 * One less compressed
1652 * purgeable/tagged page.
1654 vm_object_owner_compressed_update(
1660 case KERN_MEMORY_FAILURE
:
1661 m
->vmp_unusual
= TRUE
;
1662 m
->vmp_error
= TRUE
;
1663 m
->vmp_absent
= FALSE
;
1665 case KERN_MEMORY_ERROR
:
1666 assert(m
->vmp_absent
);
1669 panic("vm_fault_page(): unexpected "
1671 "vm_compressor_pager_get()\n",
1674 PAGE_WAKEUP_DONE(m
);
1677 goto data_requested
;
1679 my_fault_type
= DBG_PAGEIN_FAULT
;
1681 if (m
!= VM_PAGE_NULL
) {
1687 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1691 * It's possible someone called vm_object_destroy while we weren't
1692 * holding the object lock. If that has happened, then bail out
1696 pager
= object
->pager
;
1698 if (pager
== MEMORY_OBJECT_NULL
) {
1699 vm_fault_cleanup(object
, first_m
);
1700 thread_interrupt_level(interruptible_state
);
1701 return VM_FAULT_MEMORY_ERROR
;
1705 * We have an absent page in place for the faulting offset,
1706 * so we can release the object lock.
1709 if (object
->object_is_shared_cache
) {
1710 set_thread_rwlock_boost();
1713 vm_object_unlock(object
);
1716 * If this object uses a copy_call strategy,
1717 * and we are interested in a copy of this object
1718 * (having gotten here only by following a
1719 * shadow chain), then tell the memory manager
1720 * via a flag added to the desired_access
1721 * parameter, so that it can detect a race
1722 * between our walking down the shadow chain
1723 * and its pushing pages up into a copy of
1724 * the object that it manages.
1726 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
) {
1727 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1729 wants_copy_flag
= VM_PROT_NONE
;
1733 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1735 access_required
| wants_copy_flag
, 0);
1737 if (object
->copy
== first_object
) {
1739 * if we issue the memory_object_data_request in
1740 * this state, we are subject to a deadlock with
1741 * the underlying filesystem if it is trying to
1742 * shrink the file resulting in a push of pages
1743 * into the copy object... that push will stall
1744 * on the placeholder page, and if the pushing thread
1745 * is holding a lock that is required on the pagein
1746 * path (such as a truncate lock), we'll deadlock...
1747 * to avoid this potential deadlock, we throw away
1748 * our placeholder page before calling memory_object_data_request
1749 * and force this thread to retry the vm_fault_page after
1750 * we have issued the I/O. the second time through this path
1751 * we will find the page already in the cache (presumably still
1752 * busy waiting for the I/O to complete) and then complete
1753 * the fault w/o having to go through memory_object_data_request again
1755 assert(first_m
!= VM_PAGE_NULL
);
1756 assert(VM_PAGE_OBJECT(first_m
) == first_object
);
1758 vm_object_lock(first_object
);
1759 VM_PAGE_FREE(first_m
);
1760 vm_object_paging_end(first_object
);
1761 vm_object_unlock(first_object
);
1763 first_m
= VM_PAGE_NULL
;
1764 force_fault_retry
= TRUE
;
1766 vm_fault_page_forced_retry
++;
1769 if (data_already_requested
== TRUE
) {
1770 orig_behavior
= fault_info
->behavior
;
1771 orig_cluster_size
= fault_info
->cluster_size
;
1773 fault_info
->behavior
= VM_BEHAVIOR_RANDOM
;
1774 fault_info
->cluster_size
= PAGE_SIZE
;
1777 * Call the memory manager to retrieve the data.
1779 rc
= memory_object_data_request(
1781 offset
+ object
->paging_offset
,
1783 access_required
| wants_copy_flag
,
1784 (memory_object_fault_info_t
)fault_info
);
1786 if (data_already_requested
== TRUE
) {
1787 fault_info
->behavior
= orig_behavior
;
1788 fault_info
->cluster_size
= orig_cluster_size
;
1790 data_already_requested
= TRUE
;
1793 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1795 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1797 vm_object_lock(object
);
1799 if (object
->object_is_shared_cache
) {
1800 clear_thread_rwlock_boost();
1804 if (rc
!= KERN_SUCCESS
) {
1805 vm_fault_cleanup(object
, first_m
);
1806 thread_interrupt_level(interruptible_state
);
1808 return (rc
== MACH_SEND_INTERRUPTED
) ?
1809 VM_FAULT_INTERRUPTED
:
1810 VM_FAULT_MEMORY_ERROR
;
1813 clock_usec_t tv_usec
;
1815 if (my_fault_type
== DBG_PAGEIN_FAULT
) {
1816 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1817 current_thread()->t_page_creation_time
= tv_sec
;
1818 current_thread()->t_page_creation_count
= 0;
1821 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_flags
& TH_SFLAG_ABORT
)) {
1822 vm_fault_cleanup(object
, first_m
);
1823 thread_interrupt_level(interruptible_state
);
1825 return VM_FAULT_INTERRUPTED
;
1827 if (force_fault_retry
== TRUE
) {
1828 vm_fault_cleanup(object
, first_m
);
1829 thread_interrupt_level(interruptible_state
);
1831 return VM_FAULT_RETRY
;
1833 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1835 * No page here means that the object we
1836 * initially looked up was "physically
1837 * contiguous" (i.e. device memory). However,
1838 * with Virtual VRAM, the object might not
1839 * be backed by that device memory anymore,
1840 * so we're done here only if the object is
1841 * still "phys_contiguous".
1842 * Otherwise, if the object is no longer
1843 * "phys_contiguous", we need to retry the
1844 * page fault against the object's new backing
1845 * store (different memory object).
1851 * potentially a pagein fault
1852 * if we make it through the state checks
1853 * above, than we'll count it as such
1855 my_fault
= my_fault_type
;
1858 * Retry with same object/offset, since new data may
1859 * be in a different page (i.e., m is meaningless at
1866 * We get here if the object has no pager, or an existence map
1867 * exists and indicates the page isn't present on the pager
1868 * or we're unwiring a page. If a pager exists, but there
1869 * is no existence map, then the m->vmp_absent case above handles
1870 * the ZF case when the pager can't provide the page
1873 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1875 if (object
== first_object
) {
1878 assert(m
== VM_PAGE_NULL
);
1882 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1886 next_object
= object
->shadow
;
1888 if (next_object
== VM_OBJECT_NULL
) {
1890 * we've hit the bottom of the shadown chain,
1891 * fill the page in the top object with zeros.
1893 assert(!must_be_resident
);
1895 if (object
!= first_object
) {
1896 vm_object_paging_end(object
);
1897 vm_object_unlock(object
);
1899 object
= first_object
;
1900 offset
= first_offset
;
1901 vm_object_lock(object
);
1904 assert(VM_PAGE_OBJECT(m
) == object
);
1905 first_m
= VM_PAGE_NULL
;
1908 * check for any conditions that prevent
1909 * us from creating a new zero-fill page
1910 * vm_fault_check will do all of the
1911 * fault cleanup in the case of an error condition
1912 * including resetting the thread_interrupt_level
1914 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1916 if (error
!= VM_FAULT_SUCCESS
) {
1920 if (m
== VM_PAGE_NULL
) {
1921 m
= vm_page_grab_options(grab_options
);
1923 if (m
== VM_PAGE_NULL
) {
1924 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1925 thread_interrupt_level(interruptible_state
);
1927 return VM_FAULT_MEMORY_SHORTAGE
;
1929 vm_page_insert(m
, object
, offset
);
1931 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
) {
1932 m
->vmp_absent
= TRUE
;
1935 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1940 * Move on to the next object. Lock the next
1941 * object before unlocking the current one.
1943 if ((object
!= first_object
) || must_be_resident
) {
1944 vm_object_paging_end(object
);
1947 offset
+= object
->vo_shadow_offset
;
1948 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1949 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1950 access_required
= VM_PROT_READ
;
1952 vm_object_lock(next_object
);
1953 vm_object_unlock(object
);
1955 object
= next_object
;
1956 vm_object_paging_begin(object
);
1961 * PAGE HAS BEEN FOUND.
1964 * busy, so that we can play with it;
1965 * not absent, so that nobody else will fill it;
1966 * possibly eligible for pageout;
1968 * The top-level page (first_m) is:
1969 * VM_PAGE_NULL if the page was found in the
1971 * busy, not absent, and ineligible for pageout.
1973 * The current object (object) is locked. A paging
1974 * reference is held for the current and top-level
1979 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1981 #if EXTRA_ASSERTIONS
1982 assert(m
->vmp_busy
&& !m
->vmp_absent
);
1983 assert((first_m
== VM_PAGE_NULL
) ||
1984 (first_m
->vmp_busy
&& !first_m
->vmp_absent
&&
1985 !first_m
->vmp_active
&& !first_m
->vmp_inactive
&& !first_m
->vmp_secluded
));
1986 #endif /* EXTRA_ASSERTIONS */
1989 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1991 first_object
, first_m
);
1994 * If the page is being written, but isn't
1995 * already owned by the top-level object,
1996 * we have to copy it into a new page owned
1997 * by the top-level object.
1999 if (object
!= first_object
) {
2001 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2003 if (fault_type
& VM_PROT_WRITE
) {
2007 * We only really need to copy if we
2010 assert(!must_be_resident
);
2013 * If we try to collapse first_object at this
2014 * point, we may deadlock when we try to get
2015 * the lock on an intermediate object (since we
2016 * have the bottom object locked). We can't
2017 * unlock the bottom object, because the page
2018 * we found may move (by collapse) if we do.
2020 * Instead, we first copy the page. Then, when
2021 * we have no more use for the bottom object,
2022 * we unlock it and try to collapse.
2024 * Note that we copy the page even if we didn't
2025 * need to... that's the breaks.
2029 * Allocate a page for the copy
2031 copy_m
= vm_page_grab_options(grab_options
);
2033 if (copy_m
== VM_PAGE_NULL
) {
2036 vm_fault_cleanup(object
, first_m
);
2037 thread_interrupt_level(interruptible_state
);
2039 return VM_FAULT_MEMORY_SHORTAGE
;
2042 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
2046 vm_page_copy(m
, copy_m
);
2049 * If another map is truly sharing this
2050 * page with us, we have to flush all
2051 * uses of the original page, since we
2052 * can't distinguish those which want the
2053 * original from those which need the
2056 * XXXO If we know that only one map has
2057 * access to this page, then we could
2058 * avoid the pmap_disconnect() call.
2060 if (m
->vmp_pmapped
) {
2061 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2064 if (m
->vmp_clustered
) {
2065 VM_PAGE_COUNT_AS_PAGEIN(m
);
2066 VM_PAGE_CONSUME_CLUSTERED(m
);
2068 assert(!m
->vmp_cleaning
);
2071 * We no longer need the old page or object.
2076 * This check helps with marking the object as having a sequential pattern
2077 * Normally we'll miss doing this below because this fault is about COW to
2078 * the first_object i.e. bring page in from disk, push to object above but
2079 * don't update the file object's sequential pattern.
2081 if (object
->internal
== FALSE
) {
2082 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2085 vm_object_paging_end(object
);
2086 vm_object_unlock(object
);
2088 my_fault
= DBG_COW_FAULT
;
2089 VM_STAT_INCR(cow_faults
);
2090 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2091 current_task()->cow_faults
++;
2093 object
= first_object
;
2094 offset
= first_offset
;
2096 vm_object_lock(object
);
2098 * get rid of the place holder
2099 * page that we soldered in earlier
2101 VM_PAGE_FREE(first_m
);
2102 first_m
= VM_PAGE_NULL
;
2105 * and replace it with the
2106 * page we just copied into
2108 assert(copy_m
->vmp_busy
);
2109 vm_page_insert(copy_m
, object
, offset
);
2110 SET_PAGE_DIRTY(copy_m
, TRUE
);
2114 * Now that we've gotten the copy out of the
2115 * way, let's try to collapse the top object.
2116 * But we have to play ugly games with
2117 * paging_in_progress to do that...
2119 vm_object_paging_end(object
);
2120 vm_object_collapse(object
, offset
, TRUE
);
2121 vm_object_paging_begin(object
);
2123 *protection
&= (~VM_PROT_WRITE
);
2127 * Now check whether the page needs to be pushed into the
2128 * copy object. The use of asymmetric copy on write for
2129 * shared temporary objects means that we may do two copies to
2130 * satisfy the fault; one above to get the page from a
2131 * shadowed object, and one here to push it into the copy.
2133 try_failed_count
= 0;
2135 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
2136 vm_object_offset_t copy_offset
;
2140 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2143 * If the page is being written, but hasn't been
2144 * copied to the copy-object, we have to copy it there.
2146 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2147 *protection
&= ~VM_PROT_WRITE
;
2152 * If the page was guaranteed to be resident,
2153 * we must have already performed the copy.
2155 if (must_be_resident
) {
2160 * Try to get the lock on the copy_object.
2162 if (!vm_object_lock_try(copy_object
)) {
2163 vm_object_unlock(object
);
2166 mutex_pause(try_failed_count
); /* wait a bit */
2167 vm_object_lock(object
);
2171 try_failed_count
= 0;
2174 * Make another reference to the copy-object,
2175 * to keep it from disappearing during the
2178 vm_object_reference_locked(copy_object
);
2181 * Does the page exist in the copy?
2183 copy_offset
= first_offset
- copy_object
->vo_shadow_offset
;
2185 if (copy_object
->vo_size
<= copy_offset
) {
2187 * Copy object doesn't cover this page -- do nothing.
2190 } else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
2192 * Page currently exists in the copy object
2194 if (copy_m
->vmp_busy
) {
2196 * If the page is being brought
2197 * in, wait for it and then retry.
2202 * take an extra ref so object won't die
2204 vm_object_reference_locked(copy_object
);
2205 vm_object_unlock(copy_object
);
2206 vm_fault_cleanup(object
, first_m
);
2207 counter(c_vm_fault_page_block_backoff_kernel
++);
2209 vm_object_lock(copy_object
);
2210 assert(copy_object
->ref_count
> 0);
2211 VM_OBJ_RES_DECR(copy_object
);
2212 vm_object_lock_assert_exclusive(copy_object
);
2213 copy_object
->ref_count
--;
2214 assert(copy_object
->ref_count
> 0);
2215 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
2217 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->vmp_busy
) {
2218 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
2220 vm_object_unlock(copy_object
);
2221 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2222 vm_object_deallocate(copy_object
);
2226 vm_object_unlock(copy_object
);
2227 vm_object_deallocate(copy_object
);
2228 thread_interrupt_level(interruptible_state
);
2230 return VM_FAULT_RETRY
;
2233 } else if (!PAGED_OUT(copy_object
, copy_offset
)) {
2235 * If PAGED_OUT is TRUE, then the page used to exist
2236 * in the copy-object, and has already been paged out.
2237 * We don't need to repeat this. If PAGED_OUT is
2238 * FALSE, then either we don't know (!pager_created,
2239 * for example) or it hasn't been paged out.
2240 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2241 * We must copy the page to the copy object.
2243 * Allocate a page for the copy
2245 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
2247 if (copy_m
== VM_PAGE_NULL
) {
2250 VM_OBJ_RES_DECR(copy_object
);
2251 vm_object_lock_assert_exclusive(copy_object
);
2252 copy_object
->ref_count
--;
2253 assert(copy_object
->ref_count
> 0);
2255 vm_object_unlock(copy_object
);
2256 vm_fault_cleanup(object
, first_m
);
2257 thread_interrupt_level(interruptible_state
);
2259 return VM_FAULT_MEMORY_SHORTAGE
;
2262 * Must copy page into copy-object.
2264 vm_page_copy(m
, copy_m
);
2267 * If the old page was in use by any users
2268 * of the copy-object, it must be removed
2269 * from all pmaps. (We can't know which
2272 if (m
->vmp_pmapped
) {
2273 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2276 if (m
->vmp_clustered
) {
2277 VM_PAGE_COUNT_AS_PAGEIN(m
);
2278 VM_PAGE_CONSUME_CLUSTERED(m
);
2281 * If there's a pager, then immediately
2282 * page out this page, using the "initialize"
2283 * option. Else, we use the copy.
2285 if ((!copy_object
->pager_ready
)
2286 || VM_COMPRESSOR_PAGER_STATE_GET(copy_object
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2288 vm_page_lockspin_queues();
2289 assert(!m
->vmp_cleaning
);
2290 vm_page_activate(copy_m
);
2291 vm_page_unlock_queues();
2293 SET_PAGE_DIRTY(copy_m
, TRUE
);
2294 PAGE_WAKEUP_DONE(copy_m
);
2296 assert(copy_m
->vmp_busy
== TRUE
);
2297 assert(!m
->vmp_cleaning
);
2300 * dirty is protected by the object lock
2302 SET_PAGE_DIRTY(copy_m
, TRUE
);
2305 * The page is already ready for pageout:
2306 * not on pageout queues and busy.
2307 * Unlock everything except the
2308 * copy_object itself.
2310 vm_object_unlock(object
);
2313 * Write the page to the copy-object,
2314 * flushing it from the kernel.
2316 vm_pageout_initialize_page(copy_m
);
2319 * Since the pageout may have
2320 * temporarily dropped the
2321 * copy_object's lock, we
2322 * check whether we'll have
2323 * to deallocate the hard way.
2325 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2326 vm_object_unlock(copy_object
);
2327 vm_object_deallocate(copy_object
);
2328 vm_object_lock(object
);
2333 * Pick back up the old object's
2334 * lock. [It is safe to do so,
2335 * since it must be deeper in the
2338 vm_object_lock(object
);
2342 * Because we're pushing a page upward
2343 * in the object tree, we must restart
2344 * any faults that are waiting here.
2345 * [Note that this is an expansion of
2346 * PAGE_WAKEUP that uses the THREAD_RESTART
2347 * wait result]. Can't turn off the page's
2348 * busy bit because we're not done with it.
2350 if (m
->vmp_wanted
) {
2351 m
->vmp_wanted
= FALSE
;
2352 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2356 * The reference count on copy_object must be
2357 * at least 2: one for our extra reference,
2358 * and at least one from the outside world
2359 * (we checked that when we last locked
2362 vm_object_lock_assert_exclusive(copy_object
);
2363 copy_object
->ref_count
--;
2364 assert(copy_object
->ref_count
> 0);
2366 VM_OBJ_RES_DECR(copy_object
);
2367 vm_object_unlock(copy_object
);
2374 *top_page
= first_m
;
2377 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2378 object
, offset
, m
, first_m
, 0);
2380 if (m
!= VM_PAGE_NULL
) {
2381 assert(VM_PAGE_OBJECT(m
) == object
);
2383 retval
= VM_FAULT_SUCCESS
;
2385 if (my_fault
== DBG_PAGEIN_FAULT
) {
2386 VM_PAGE_COUNT_AS_PAGEIN(m
);
2388 if (object
->internal
) {
2389 my_fault
= DBG_PAGEIND_FAULT
;
2391 my_fault
= DBG_PAGEINV_FAULT
;
2395 * evaluate access pattern and update state
2396 * vm_fault_deactivate_behind depends on the
2397 * state being up to date
2399 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2400 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2401 } else if (type_of_fault
== NULL
&& my_fault
== DBG_CACHE_HIT_FAULT
) {
2403 * we weren't called from vm_fault, so handle the
2404 * accounting here for hits in the cache
2406 if (m
->vmp_clustered
) {
2407 VM_PAGE_COUNT_AS_PAGEIN(m
);
2408 VM_PAGE_CONSUME_CLUSTERED(m
);
2410 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2411 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2412 } else if (my_fault
== DBG_COMPRESSOR_FAULT
|| my_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
) {
2413 VM_STAT_INCR(decompressions
);
2415 if (type_of_fault
) {
2416 *type_of_fault
= my_fault
;
2419 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2420 assert(first_m
== VM_PAGE_NULL
);
2421 assert(object
== first_object
);
2424 thread_interrupt_level(interruptible_state
);
2427 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2432 thread_interrupt_level(interruptible_state
);
2434 if (wait_result
== THREAD_INTERRUPTED
) {
2435 return VM_FAULT_INTERRUPTED
;
2437 return VM_FAULT_RETRY
;
2446 * When soft faulting a page, we have to validate the page if:
2447 * 1. the page is being mapped in user space
2448 * 2. the page hasn't already been found to be "tainted"
2449 * 3. the page belongs to a code-signed object
2450 * 4. the page has not been validated yet or has been mapped for write.
2452 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page, page_obj) \
2453 ((pmap) != kernel_pmap /*1*/ && \
2454 !(page)->vmp_cs_tainted /*2*/ && \
2455 (page_obj)->code_signed /*3*/ && \
2456 (!(page)->vmp_cs_validated || (page)->vmp_wpmapped /*4*/ ))
2460 * page queue lock must NOT be held
2461 * m->vmp_object must be locked
2463 * NOTE: m->vmp_object could be locked "shared" only if we are called
2464 * from vm_fault() as part of a soft fault. If so, we must be
2465 * careful not to modify the VM object in any way that is not
2466 * legal under a shared lock...
2468 extern int panic_on_cs_killed
;
2469 extern int proc_selfpid(void);
2470 extern char *proc_name_address(void *p
);
2471 unsigned long cs_enter_tainted_rejected
= 0;
2472 unsigned long cs_enter_tainted_accepted
= 0;
2474 vm_fault_enter(vm_page_t m
,
2476 vm_map_offset_t vaddr
,
2478 vm_prot_t caller_prot
,
2480 boolean_t change_wiring
,
2482 vm_object_fault_info_t fault_info
,
2483 boolean_t
*need_retry
,
2486 kern_return_t kr
, pe_result
;
2487 boolean_t previously_pmapped
= m
->vmp_pmapped
;
2488 boolean_t must_disconnect
= 0;
2489 boolean_t map_is_switched
, map_is_switch_protected
;
2490 boolean_t cs_violation
;
2491 int cs_enforcement_enabled
;
2492 vm_prot_t fault_type
;
2494 boolean_t no_cache
= fault_info
->no_cache
;
2495 boolean_t cs_bypass
= fault_info
->cs_bypass
;
2496 int pmap_options
= fault_info
->pmap_options
;
2498 fault_type
= change_wiring
? VM_PROT_NONE
: caller_prot
;
2499 object
= VM_PAGE_OBJECT(m
);
2501 vm_object_lock_assert_held(object
);
2504 if (pmap
== kernel_pmap
) {
2505 kasan_notify_address(vaddr
, PAGE_SIZE
);
2509 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2511 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
2512 assert(m
->vmp_fictitious
);
2513 return KERN_SUCCESS
;
2516 if (*type_of_fault
== DBG_ZERO_FILL_FAULT
) {
2517 vm_object_lock_assert_exclusive(object
);
2518 } else if ((fault_type
& VM_PROT_WRITE
) == 0 &&
2520 #if VM_OBJECT_ACCESS_TRACKING
2521 || object
->access_tracking
2522 #endif /* VM_OBJECT_ACCESS_TRACKING */
2525 * This is not a "write" fault, so we
2526 * might not have taken the object lock
2527 * exclusively and we might not be able
2528 * to update the "wpmapped" bit in
2530 * Let's just grant read access to
2531 * the page for now and we'll
2532 * soft-fault again if we need write
2536 /* This had better not be a JIT page. */
2537 if (!pmap_has_prot_policy(prot
)) {
2538 prot
&= ~VM_PROT_WRITE
;
2543 if (m
->vmp_pmapped
== FALSE
) {
2544 if (m
->vmp_clustered
) {
2545 if (*type_of_fault
== DBG_CACHE_HIT_FAULT
) {
2547 * found it in the cache, but this
2548 * is the first fault-in of the page (m->vmp_pmapped == FALSE)
2549 * so it must have come in as part of
2550 * a cluster... account 1 pagein against it
2552 if (object
->internal
) {
2553 *type_of_fault
= DBG_PAGEIND_FAULT
;
2555 *type_of_fault
= DBG_PAGEINV_FAULT
;
2558 VM_PAGE_COUNT_AS_PAGEIN(m
);
2560 VM_PAGE_CONSUME_CLUSTERED(m
);
2564 if (*type_of_fault
!= DBG_COW_FAULT
) {
2565 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2567 if (pmap
== kernel_pmap
) {
2568 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2572 /* Validate code signature if necessary. */
2574 VM_FAULT_NEED_CS_VALIDATION(pmap
, m
, object
)) {
2575 vm_object_lock_assert_exclusive(object
);
2577 if (m
->vmp_cs_validated
) {
2578 vm_cs_revalidates
++;
2581 /* VM map is locked, so 1 ref will remain on VM object -
2582 * so no harm if vm_page_validate_cs drops the object lock */
2585 if (fault_info
->pmap_cs_associated
&&
2586 pmap_cs_enforced(pmap
) &&
2587 !m
->vmp_cs_validated
&&
2588 !m
->vmp_cs_tainted
&&
2590 (prot
& VM_PROT_EXECUTE
) &&
2591 (caller_prot
& VM_PROT_EXECUTE
)) {
2593 * With pmap_cs, the pmap layer will validate the
2594 * code signature for any executable pmap mapping.
2595 * No need for us to validate this page too:
2596 * in pmap_cs we trust...
2598 vm_cs_defer_to_pmap_cs
++;
2600 vm_cs_defer_to_pmap_cs_not
++;
2601 vm_page_validate_cs(m
);
2604 vm_page_validate_cs(m
);
2605 #endif /* PMAP_CS */
2608 #define page_immutable(m, prot) ((m)->vmp_cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/ )
2609 #define page_nx(m) ((m)->vmp_cs_nx)
2611 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2612 (pmap
== vm_map_pmap(current_thread()->map
)));
2613 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2615 /* If the map is switched, and is switch-protected, we must protect
2616 * some pages from being write-faulted: immutable pages because by
2617 * definition they may not be written, and executable pages because that
2618 * would provide a way to inject unsigned code.
2619 * If the page is immutable, we can simply return. However, we can't
2620 * immediately determine whether a page is executable anywhere. But,
2621 * we can disconnect it everywhere and remove the executable protection
2622 * from the current map. We do that below right before we do the
2625 cs_enforcement_enabled
= cs_process_enforcement(NULL
);
2627 if (cs_enforcement_enabled
&& map_is_switched
&&
2628 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2629 (prot
& VM_PROT_WRITE
)) {
2630 return KERN_CODESIGN_ERROR
;
2633 if (cs_enforcement_enabled
&& page_nx(m
) && (prot
& VM_PROT_EXECUTE
)) {
2635 printf("page marked to be NX, not letting it be mapped EXEC\n");
2637 return KERN_CODESIGN_ERROR
;
2640 /* A page could be tainted, or pose a risk of being tainted later.
2641 * Check whether the receiving process wants it, and make it feel
2642 * the consequences (that hapens in cs_invalid_page()).
2643 * For CS Enforcement, two other conditions will
2644 * cause that page to be tainted as well:
2645 * - pmapping an unsigned page executable - this means unsigned code;
2646 * - writeable mapping of a validated page - the content of that page
2647 * can be changed without the kernel noticing, therefore unsigned
2648 * code can be created
2651 /* code-signing is bypassed */
2652 cs_violation
= FALSE
;
2653 } else if (m
->vmp_cs_tainted
) {
2655 cs_violation
= TRUE
;
2656 } else if (!cs_enforcement_enabled
) {
2657 /* no further code-signing enforcement */
2658 cs_violation
= FALSE
;
2659 } else if (page_immutable(m
, prot
) &&
2660 ((prot
& VM_PROT_WRITE
) ||
2663 * The page should be immutable, but is in danger of being
2665 * This is the case where we want policy from the code
2666 * directory - is the page immutable or not? For now we have
2667 * to assume that code pages will be immutable, data pages not.
2668 * We'll assume a page is a code page if it has a code directory
2669 * and we fault for execution.
2670 * That is good enough since if we faulted the code page for
2671 * writing in another map before, it is wpmapped; if we fault
2672 * it for writing in this map later it will also be faulted for
2673 * executing at the same time; and if we fault for writing in
2674 * another map later, we will disconnect it from this pmap so
2675 * we'll notice the change.
2677 cs_violation
= TRUE
;
2678 } else if (!m
->vmp_cs_validated
&&
2679 (prot
& VM_PROT_EXECUTE
)
2682 * Executable pages will be validated by pmap_cs;
2683 * in pmap_cs we trust...
2684 * If pmap_cs is turned off, this is a code-signing
2687 && !(pmap_cs_enforced(pmap
))
2688 #endif /* PMAP_CS */
2690 cs_violation
= TRUE
;
2692 cs_violation
= FALSE
;
2696 /* We will have a tainted page. Have to handle the special case
2697 * of a switched map now. If the map is not switched, standard
2698 * procedure applies - call cs_invalid_page().
2699 * If the map is switched, the real owner is invalid already.
2700 * There is no point in invalidating the switching process since
2701 * it will not be executing from the map. So we don't call
2702 * cs_invalid_page() in that case. */
2703 boolean_t reject_page
, cs_killed
;
2704 if (map_is_switched
) {
2705 assert(pmap
== vm_map_pmap(current_thread()->map
));
2706 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2707 reject_page
= FALSE
;
2710 printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s prot: 0x%x\n",
2711 object
->code_signed
? "yes" : "no",
2712 m
->vmp_cs_validated
? "yes" : "no",
2713 m
->vmp_cs_tainted
? "yes" : "no",
2714 m
->vmp_wpmapped
? "yes" : "no",
2717 reject_page
= cs_invalid_page((addr64_t
) vaddr
, &cs_killed
);
2721 /* reject the invalid page: abort the page fault */
2723 const char *procname
;
2725 vm_object_t file_object
, shadow
;
2726 vm_object_offset_t file_offset
;
2727 char *pathname
, *filename
;
2728 vm_size_t pathname_len
, filename_len
;
2729 boolean_t truncated_path
;
2730 #define __PATH_MAX 1024
2731 struct timespec mtime
, cs_mtime
;
2733 os_reason_t codesigning_exit_reason
= OS_REASON_NULL
;
2735 kr
= KERN_CODESIGN_ERROR
;
2736 cs_enter_tainted_rejected
++;
2738 /* get process name and pid */
2740 task
= current_task();
2741 pid
= proc_selfpid();
2742 if (task
->bsd_info
!= NULL
) {
2743 procname
= proc_name_address(task
->bsd_info
);
2746 /* get file's VM object */
2747 file_object
= object
;
2748 file_offset
= m
->vmp_offset
;
2749 for (shadow
= file_object
->shadow
,
2751 shadow
!= VM_OBJECT_NULL
;
2752 shadow
= file_object
->shadow
,
2754 vm_object_lock_shared(shadow
);
2755 if (file_object
!= object
) {
2756 vm_object_unlock(file_object
);
2758 file_offset
+= file_object
->vo_shadow_offset
;
2759 file_object
= shadow
;
2764 cs_mtime
.tv_sec
= 0;
2765 cs_mtime
.tv_nsec
= 0;
2767 /* get file's pathname and/or filename */
2772 truncated_path
= FALSE
;
2773 /* no pager -> no file -> no pathname, use "<nil>" in that case */
2774 if (file_object
->pager
!= NULL
) {
2775 pathname
= (char *)kalloc(__PATH_MAX
* 2);
2778 pathname_len
= __PATH_MAX
;
2779 filename
= pathname
+ pathname_len
;
2780 filename_len
= __PATH_MAX
;
2782 vnode_pager_get_object_name(file_object
->pager
,
2789 /* safety first... */
2790 pathname
[__PATH_MAX
- 1] = '\0';
2791 filename
[__PATH_MAX
- 1] = '\0';
2793 vnode_pager_get_object_mtime(file_object
->pager
,
2797 printf("CODE SIGNING: process %d[%s]: "
2798 "rejecting invalid page at address 0x%llx "
2799 "from offset 0x%llx in file \"%s%s%s\" "
2800 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2801 "(signed:%d validated:%d tainted:%d nx:%d "
2802 "wpmapped:%d dirty:%d depth:%d)\n",
2803 pid
, procname
, (addr64_t
) vaddr
,
2805 (pathname
? pathname
: "<nil>"),
2806 (truncated_path
? "/.../" : ""),
2807 (truncated_path
? filename
: ""),
2808 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2809 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2810 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2813 mtime
.tv_sec
, mtime
.tv_nsec
,
2814 object
->code_signed
,
2815 m
->vmp_cs_validated
,
2823 * We currently only generate an exit reason if cs_invalid_page directly killed a process. If cs_invalid_page
2824 * did not kill the process (more the case on desktop), vm_fault_enter will not satisfy the fault and whether the
2825 * process dies is dependent on whether there is a signal handler registered for SIGSEGV and how that handler
2826 * will deal with the segmentation fault.
2829 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
2830 pid
, OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_INVALID_PAGE
, 0, 0);
2832 codesigning_exit_reason
= os_reason_create(OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_INVALID_PAGE
);
2833 if (codesigning_exit_reason
== NULL
) {
2834 printf("vm_fault_enter: failed to allocate codesigning exit reason\n");
2836 mach_vm_address_t data_addr
= 0;
2837 struct codesigning_exit_reason_info
*ceri
= NULL
;
2838 uint32_t reason_buffer_size_estimate
= kcdata_estimate_required_buffer_size(1, sizeof(*ceri
));
2840 if (os_reason_alloc_buffer_noblock(codesigning_exit_reason
, reason_buffer_size_estimate
)) {
2841 printf("vm_fault_enter: failed to allocate buffer for codesigning exit reason\n");
2843 if (KERN_SUCCESS
== kcdata_get_memory_addr(&codesigning_exit_reason
->osr_kcd_descriptor
,
2844 EXIT_REASON_CODESIGNING_INFO
, sizeof(*ceri
), &data_addr
)) {
2845 ceri
= (struct codesigning_exit_reason_info
*)data_addr
;
2846 static_assert(__PATH_MAX
== sizeof(ceri
->ceri_pathname
));
2848 ceri
->ceri_virt_addr
= vaddr
;
2849 ceri
->ceri_file_offset
= file_offset
;
2851 strncpy((char *)&ceri
->ceri_pathname
, pathname
, sizeof(ceri
->ceri_pathname
));
2853 ceri
->ceri_pathname
[0] = '\0';
2856 strncpy((char *)&ceri
->ceri_filename
, filename
, sizeof(ceri
->ceri_filename
));
2858 ceri
->ceri_filename
[0] = '\0';
2860 ceri
->ceri_path_truncated
= (truncated_path
);
2861 ceri
->ceri_codesig_modtime_secs
= cs_mtime
.tv_sec
;
2862 ceri
->ceri_codesig_modtime_nsecs
= cs_mtime
.tv_nsec
;
2863 ceri
->ceri_page_modtime_secs
= mtime
.tv_sec
;
2864 ceri
->ceri_page_modtime_nsecs
= mtime
.tv_nsec
;
2865 ceri
->ceri_object_codesigned
= (object
->code_signed
);
2866 ceri
->ceri_page_codesig_validated
= (m
->vmp_cs_validated
);
2867 ceri
->ceri_page_codesig_tainted
= (m
->vmp_cs_tainted
);
2868 ceri
->ceri_page_codesig_nx
= (m
->vmp_cs_nx
);
2869 ceri
->ceri_page_wpmapped
= (m
->vmp_wpmapped
);
2870 ceri
->ceri_page_slid
= 0;
2871 ceri
->ceri_page_dirty
= (m
->vmp_dirty
);
2872 ceri
->ceri_page_shadow_depth
= shadow_depth
;
2874 #if DEBUG || DEVELOPMENT
2875 panic("vm_fault_enter: failed to allocate kcdata for codesigning exit reason");
2877 printf("vm_fault_enter: failed to allocate kcdata for codesigning exit reason\n");
2878 #endif /* DEBUG || DEVELOPMENT */
2879 /* Free the buffer */
2880 os_reason_alloc_buffer_noblock(codesigning_exit_reason
, 0);
2885 set_thread_exit_reason(current_thread(), codesigning_exit_reason
, FALSE
);
2887 if (panic_on_cs_killed
&&
2888 object
->object_is_shared_cache
) {
2889 panic("CODE SIGNING: process %d[%s]: "
2890 "rejecting invalid page at address 0x%llx "
2891 "from offset 0x%llx in file \"%s%s%s\" "
2892 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2893 "(signed:%d validated:%d tainted:%d nx:%d"
2894 "wpmapped:%d dirty:%d depth:%d)\n",
2895 pid
, procname
, (addr64_t
) vaddr
,
2897 (pathname
? pathname
: "<nil>"),
2898 (truncated_path
? "/.../" : ""),
2899 (truncated_path
? filename
: ""),
2900 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2901 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2902 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2905 mtime
.tv_sec
, mtime
.tv_nsec
,
2906 object
->code_signed
,
2907 m
->vmp_cs_validated
,
2915 if (file_object
!= object
) {
2916 vm_object_unlock(file_object
);
2918 if (pathname_len
!= 0) {
2919 kfree(pathname
, __PATH_MAX
* 2);
2924 /* proceed with the invalid page */
2926 if (!m
->vmp_cs_validated
&&
2927 !object
->code_signed
) {
2929 * This page has not been (fully) validated but
2930 * does not belong to a code-signed object
2931 * so it should not be forcefully considered
2933 * We're just concerned about it here because
2934 * we've been asked to "execute" it but that
2935 * does not mean that it should cause other
2937 * This happens when a debugger sets a
2938 * breakpoint and we then execute code in
2939 * that page. Marking the page as "tainted"
2940 * would cause any inspection tool ("leaks",
2941 * "vmmap", "CrashReporter", ...) to get killed
2942 * due to code-signing violation on that page,
2943 * even though they're just reading it and not
2944 * executing from it.
2948 * Page might have been tainted before or not;
2949 * now it definitively is. If the page wasn't
2950 * tainted, we must disconnect it from all
2951 * pmaps later, to force existing mappings
2952 * through that code path for re-consideration
2953 * of the validity of that page.
2955 must_disconnect
= !m
->vmp_cs_tainted
;
2956 m
->vmp_cs_tainted
= TRUE
;
2958 cs_enter_tainted_accepted
++;
2960 if (kr
!= KERN_SUCCESS
) {
2962 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2963 "*** INVALID PAGE ***\n",
2967 if (cs_enforcement_panic
) {
2968 panic("CODESIGNING: panicking on invalid page\n");
2973 /* proceed with the valid page */
2977 boolean_t page_queues_locked
= FALSE
;
2978 #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \
2980 if (! page_queues_locked) { \
2981 page_queues_locked = TRUE; \
2982 vm_page_lockspin_queues(); \
2985 #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \
2987 if (page_queues_locked) { \
2988 page_queues_locked = FALSE; \
2989 vm_page_unlock_queues(); \
2994 * Hold queues lock to manipulate
2995 * the page queues. Change wiring
2998 assert((m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) || object
!= compressor_object
);
3000 #if CONFIG_BACKGROUND_QUEUE
3001 vm_page_update_background_state(m
);
3003 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
3005 * Compressor pages are neither wired
3006 * nor pageable and should never change.
3008 assert(object
== compressor_object
);
3009 } else if (change_wiring
) {
3010 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3013 if (kr
== KERN_SUCCESS
) {
3014 vm_page_wire(m
, wire_tag
, TRUE
);
3017 vm_page_unwire(m
, TRUE
);
3019 /* we keep the page queues lock, if we need it later */
3021 if (object
->internal
== TRUE
) {
3023 * don't allow anonymous pages on
3024 * the speculative queues
3028 if (kr
!= KERN_SUCCESS
) {
3029 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3030 vm_page_deactivate(m
);
3031 /* we keep the page queues lock, if we need it later */
3032 } else if (((m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
) ||
3033 (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) ||
3034 (m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) ||
3035 ((m
->vmp_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && no_cache
)) &&
3036 !VM_PAGE_WIRED(m
)) {
3037 if (vm_page_local_q
&&
3038 (*type_of_fault
== DBG_COW_FAULT
||
3039 *type_of_fault
== DBG_ZERO_FILL_FAULT
)) {
3043 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
3045 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3046 vm_object_lock_assert_exclusive(object
);
3049 * we got a local queue to stuff this
3051 * its safe to manipulate local and
3052 * local_id at this point since we're
3053 * behind an exclusive object lock and
3054 * the page is not on any global queue.
3056 * we'll use the current cpu number to
3057 * select the queue note that we don't
3058 * need to disable preemption... we're
3059 * going to be behind the local queue's
3060 * lock to do the real work
3064 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
3066 VPL_LOCK(&lq
->vpl_lock
);
3068 vm_page_check_pageable_safe(m
);
3069 vm_page_queue_enter(&lq
->vpl_queue
, m
, vmp_pageq
);
3070 m
->vmp_q_state
= VM_PAGE_ON_ACTIVE_LOCAL_Q
;
3071 m
->vmp_local_id
= lid
;
3074 if (object
->internal
) {
3075 lq
->vpl_internal_count
++;
3077 lq
->vpl_external_count
++;
3080 VPL_UNLOCK(&lq
->vpl_lock
);
3082 if (lq
->vpl_count
> vm_page_local_q_soft_limit
) {
3084 * we're beyond the soft limit
3085 * for the local queue
3086 * vm_page_reactivate_local will
3087 * 'try' to take the global page
3088 * queue lock... if it can't
3089 * that's ok... we'll let the
3090 * queue continue to grow up
3091 * to the hard limit... at that
3092 * point we'll wait for the
3093 * lock... once we've got the
3094 * lock, we'll transfer all of
3095 * the pages from the local
3096 * queue to the global active
3099 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
3102 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3105 * test again now that we hold the
3108 if (!VM_PAGE_WIRED(m
)) {
3109 if (m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) {
3110 vm_page_queues_remove(m
, FALSE
);
3112 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated
, 1);
3113 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_fault_reactivated
, 1);
3116 if (!VM_PAGE_ACTIVE_OR_INACTIVE(m
) ||
3119 * If this is a no_cache mapping
3120 * and the page has never been
3121 * mapped before or was
3122 * previously a no_cache page,
3123 * then we want to leave pages
3124 * in the speculative state so
3125 * that they can be readily
3126 * recycled if free memory runs
3127 * low. Otherwise the page is
3128 * activated as normal.
3132 (!previously_pmapped
||
3134 m
->vmp_no_cache
= TRUE
;
3136 if (m
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
3137 vm_page_speculate(m
, FALSE
);
3139 } else if (!VM_PAGE_ACTIVE_OR_INACTIVE(m
)) {
3140 vm_page_activate(m
);
3144 /* we keep the page queues lock, if we need it later */
3148 /* we're done with the page queues lock, if we ever took it */
3149 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3152 /* If we have a KERN_SUCCESS from the previous checks, we either have
3153 * a good page, or a tainted page that has been accepted by the process.
3154 * In both cases the page will be entered into the pmap.
3155 * If the page is writeable, we need to disconnect it from other pmaps
3156 * now so those processes can take note.
3158 if (kr
== KERN_SUCCESS
) {
3160 * NOTE: we may only hold the vm_object lock SHARED
3161 * at this point, so we need the phys_page lock to
3162 * properly serialize updating the pmapped and
3165 if ((prot
& VM_PROT_EXECUTE
) && !m
->vmp_xpmapped
) {
3166 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3168 pmap_lock_phys_page(phys_page
);
3170 * go ahead and take the opportunity
3171 * to set 'pmapped' here so that we don't
3172 * need to grab this lock a 2nd time
3175 m
->vmp_pmapped
= TRUE
;
3177 if (!m
->vmp_xpmapped
) {
3178 m
->vmp_xpmapped
= TRUE
;
3180 pmap_unlock_phys_page(phys_page
);
3182 if (!object
->internal
) {
3183 OSAddAtomic(1, &vm_page_xpmapped_external_count
);
3186 #if defined(__arm__) || defined(__arm64__)
3187 pmap_sync_page_data_phys(phys_page
);
3189 if (object
->internal
&&
3190 object
->pager
!= NULL
) {
3192 * This page could have been
3193 * uncompressed by the
3194 * compressor pager and its
3195 * contents might be only in
3197 * Since it's being mapped for
3198 * "execute" for the fist time,
3199 * make sure the icache is in
3202 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3203 pmap_sync_page_data_phys(phys_page
);
3207 pmap_unlock_phys_page(phys_page
);
3210 if (m
->vmp_pmapped
== FALSE
) {
3211 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3213 pmap_lock_phys_page(phys_page
);
3214 m
->vmp_pmapped
= TRUE
;
3215 pmap_unlock_phys_page(phys_page
);
3219 if (fault_type
& VM_PROT_WRITE
) {
3220 if (m
->vmp_wpmapped
== FALSE
) {
3221 vm_object_lock_assert_exclusive(object
);
3222 if (!object
->internal
&& object
->pager
) {
3223 task_update_logical_writes(current_task(), PAGE_SIZE
, TASK_WRITE_DEFERRED
, vnode_pager_lookup_vnode(object
->pager
));
3225 m
->vmp_wpmapped
= TRUE
;
3227 if (must_disconnect
) {
3229 * We can only get here
3230 * because of the CSE logic
3232 assert(cs_enforcement_enabled
);
3233 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
3235 * If we are faulting for a write, we can clear
3236 * the execute bit - that will ensure the page is
3237 * checked again before being executable, which
3238 * protects against a map switch.
3239 * This only happens the first time the page
3240 * gets tainted, so we won't get stuck here
3241 * to make an already writeable page executable.
3244 assert(!pmap_has_prot_policy(prot
));
3245 prot
&= ~VM_PROT_EXECUTE
;
3249 assert(VM_PAGE_OBJECT(m
) == object
);
3251 #if VM_OBJECT_ACCESS_TRACKING
3252 if (object
->access_tracking
) {
3253 DTRACE_VM2(access_tracking
, vm_map_offset_t
, vaddr
, int, fault_type
);
3254 if (fault_type
& VM_PROT_WRITE
) {
3255 object
->access_tracking_writes
++;
3256 vm_object_access_tracking_writes
++;
3258 object
->access_tracking_reads
++;
3259 vm_object_access_tracking_reads
++;
3262 #endif /* VM_OBJECT_ACCESS_TRACKING */
3266 * If CS enforcement is on, we don't ask for an executable page if the
3267 * fault does not call for execution, because that can fail in
3268 * situations where the caller only actually wanted read access.
3269 * However, it may be better to instead retry without execute on
3270 * failure, or pass a flag into pmap_enter to do the right thing.
3272 // TODO: <rdar://problem/30997388> maybe do something better than masking out VM_PROT_EXECUTE on non-execute faults
3273 if (pmap_cs_enforced(pmap
) && !(caller_prot
& VM_PROT_EXECUTE
)) {
3274 prot
&= ~VM_PROT_EXECUTE
;
3278 /* Prevent a deadlock by not
3279 * holding the object lock if we need to wait for a page in
3280 * pmap_enter() - <rdar://problem/7138958> */
3281 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
, 0,
3283 pmap_options
| PMAP_OPTIONS_NOWAIT
,
3286 if (pe_result
== KERN_INVALID_ARGUMENT
&&
3287 pmap
== PMAP_NULL
&&
3290 * Wiring a page in a pmap-less VM map:
3291 * VMware's "vmmon" kernel extension does this
3293 * Let it proceed even though the PMAP_ENTER() failed.
3295 pe_result
= KERN_SUCCESS
;
3297 #endif /* __x86_64__ */
3299 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
3302 * this will be non-null in the case where we hold the lock
3303 * on the top-object in this chain... we can't just drop
3304 * the lock on the object we're inserting the page into
3305 * and recall the PMAP_ENTER since we can still cause
3306 * a deadlock if one of the critical paths tries to
3307 * acquire the lock on the top-object and we're blocked
3308 * in PMAP_ENTER waiting for memory... our only recourse
3309 * is to deal with it at a higher level where we can
3313 vm_pmap_enter_retried
++;
3314 goto after_the_pmap_enter
;
3316 /* The nonblocking version of pmap_enter did not succeed.
3317 * and we don't need to drop other locks and retry
3318 * at the level above us, so
3319 * use the blocking version instead. Requires marking
3320 * the page busy and unlocking the object */
3321 boolean_t was_busy
= m
->vmp_busy
;
3323 vm_object_lock_assert_exclusive(object
);
3326 vm_object_unlock(object
);
3328 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
,
3330 pmap_options
, pe_result
);
3332 assert(VM_PAGE_OBJECT(m
) == object
);
3334 /* Take the object lock again. */
3335 vm_object_lock(object
);
3337 /* If the page was busy, someone else will wake it up.
3338 * Otherwise, we have to do it now. */
3339 assert(m
->vmp_busy
);
3341 PAGE_WAKEUP_DONE(m
);
3343 vm_pmap_enter_blocked
++;
3349 after_the_pmap_enter
:
3354 vm_pre_fault(vm_map_offset_t vaddr
)
3356 if (pmap_find_phys(current_map()->pmap
, vaddr
) == 0) {
3357 vm_fault(current_map(), /* map */
3359 VM_PROT_READ
, /* fault_type */
3360 FALSE
, /* change_wiring */
3361 VM_KERN_MEMORY_NONE
, /* tag - not wiring */
3362 THREAD_UNINT
, /* interruptible */
3363 NULL
, /* caller_pmap */
3364 0 /* caller_pmap_addr */);
3372 * Handle page faults, including pseudo-faults
3373 * used to change the wiring status of pages.
3375 * Explicit continuations have been removed.
3377 * vm_fault and vm_fault_page save mucho state
3378 * in the moral equivalent of a closure. The state
3379 * structure is allocated when first entering vm_fault
3380 * and deallocated when leaving vm_fault.
3383 extern int _map_enter_debug
;
3384 extern uint64_t get_current_unique_pid(void);
3386 unsigned long vm_fault_collapse_total
= 0;
3387 unsigned long vm_fault_collapse_skipped
= 0;
3393 vm_map_offset_t vaddr
,
3394 vm_prot_t fault_type
,
3395 boolean_t change_wiring
,
3398 vm_map_offset_t caller_pmap_addr
)
3400 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
, vm_tag_bt(),
3401 interruptible
, caller_pmap
, caller_pmap_addr
,
3408 vm_map_offset_t vaddr
,
3409 vm_prot_t fault_type
,
3410 boolean_t change_wiring
,
3411 vm_tag_t wire_tag
, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3414 vm_map_offset_t caller_pmap_addr
)
3416 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
, wire_tag
,
3417 interruptible
, caller_pmap
, caller_pmap_addr
,
3424 vm_map_offset_t vaddr
,
3425 vm_prot_t caller_prot
,
3426 boolean_t change_wiring
,
3427 vm_tag_t wire_tag
, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3430 vm_map_offset_t caller_pmap_addr
,
3431 ppnum_t
*physpage_p
)
3433 vm_map_version_t version
; /* Map version for verificiation */
3434 boolean_t wired
; /* Should mapping be wired down? */
3435 vm_object_t object
; /* Top-level object */
3436 vm_object_offset_t offset
; /* Top-level offset */
3437 vm_prot_t prot
; /* Protection for mapping */
3438 vm_object_t old_copy_object
; /* Saved copy object */
3439 vm_page_t result_page
; /* Result of vm_fault_page */
3440 vm_page_t top_page
; /* Placeholder page */
3443 vm_page_t m
; /* Fast access to result_page */
3444 kern_return_t error_code
;
3445 vm_object_t cur_object
;
3446 vm_object_t m_object
= NULL
;
3447 vm_object_offset_t cur_offset
;
3449 vm_object_t new_object
;
3452 wait_interrupt_t interruptible_state
;
3453 vm_map_t real_map
= map
;
3454 vm_map_t original_map
= map
;
3455 boolean_t object_locks_dropped
= FALSE
;
3456 vm_prot_t fault_type
;
3457 vm_prot_t original_fault_type
;
3458 struct vm_object_fault_info fault_info
= {};
3459 boolean_t need_collapse
= FALSE
;
3460 boolean_t need_retry
= FALSE
;
3461 boolean_t
*need_retry_ptr
= NULL
;
3462 int object_lock_type
= 0;
3463 int cur_object_lock_type
;
3464 vm_object_t top_object
= VM_OBJECT_NULL
;
3465 vm_object_t written_on_object
= VM_OBJECT_NULL
;
3466 memory_object_t written_on_pager
= NULL
;
3467 vm_object_offset_t written_on_offset
= 0;
3469 int compressed_count_delta
;
3471 vm_map_offset_t trace_vaddr
;
3472 vm_map_offset_t trace_real_vaddr
;
3473 #if DEVELOPMENT || DEBUG
3474 vm_map_offset_t real_vaddr
;
3477 #endif /* DEVELOPMENT || DEBUG */
3478 trace_real_vaddr
= vaddr
;
3479 vaddr
= vm_map_trunc_page(vaddr
, PAGE_MASK
);
3481 if (map
== kernel_map
) {
3482 trace_vaddr
= VM_KERNEL_ADDRHIDE(vaddr
);
3483 trace_real_vaddr
= VM_KERNEL_ADDRHIDE(trace_real_vaddr
);
3485 trace_vaddr
= vaddr
;
3488 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3489 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
3490 ((uint64_t)trace_vaddr
>> 32),
3492 (map
== kernel_map
),
3496 if (get_preemption_level() != 0) {
3497 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3498 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3499 ((uint64_t)trace_vaddr
>> 32),
3505 return KERN_FAILURE
;
3508 thread_t cthread
= current_thread();
3509 boolean_t rtfault
= (cthread
->sched_mode
== TH_MODE_REALTIME
);
3510 uint64_t fstart
= 0;
3513 fstart
= mach_continuous_time();
3516 interruptible_state
= thread_interrupt_level(interruptible
);
3518 fault_type
= (change_wiring
? VM_PROT_NONE
: caller_prot
);
3520 VM_STAT_INCR(faults
);
3521 current_task()->faults
++;
3522 original_fault_type
= fault_type
;
3524 if (fault_type
& VM_PROT_WRITE
) {
3525 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3527 object_lock_type
= OBJECT_LOCK_SHARED
;
3530 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
3532 if ((map
== kernel_map
) && (caller_prot
& VM_PROT_WRITE
)) {
3533 if (compressor_map
) {
3534 if ((vaddr
>= vm_map_min(compressor_map
)) && (vaddr
< vm_map_max(compressor_map
))) {
3535 panic("Write fault on compressor map, va: %p type: %u bounds: %p->%p", (void *) vaddr
, caller_prot
, (void *) vm_map_min(compressor_map
), (void *) vm_map_max(compressor_map
));
3540 assert(written_on_object
== VM_OBJECT_NULL
);
3543 * assume we will hit a page in the cache
3544 * otherwise, explicitly override with
3545 * the real fault type once we determine it
3547 type_of_fault
= DBG_CACHE_HIT_FAULT
;
3550 * Find the backing store object and offset into
3551 * it to begin the search.
3553 fault_type
= original_fault_type
;
3555 vm_map_lock_read(map
);
3557 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
3558 object_lock_type
, &version
,
3559 &object
, &offset
, &prot
, &wired
,
3563 if (kr
!= KERN_SUCCESS
) {
3564 vm_map_unlock_read(map
);
3567 pmap
= real_map
->pmap
;
3568 fault_info
.interruptible
= interruptible
;
3569 fault_info
.stealth
= FALSE
;
3570 fault_info
.io_sync
= FALSE
;
3571 fault_info
.mark_zf_absent
= FALSE
;
3572 fault_info
.batch_pmap_op
= FALSE
;
3575 * If the page is wired, we must fault for the current protection
3576 * value, to avoid further faults.
3579 fault_type
= prot
| VM_PROT_WRITE
;
3581 * since we're treating this fault as a 'write'
3582 * we must hold the top object lock exclusively
3584 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3585 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3587 if (vm_object_lock_upgrade(object
) == FALSE
) {
3589 * couldn't upgrade, so explictly
3590 * take the lock exclusively
3592 vm_object_lock(object
);
3597 #if VM_FAULT_CLASSIFY
3599 * Temporary data gathering code
3601 vm_fault_classify(object
, offset
, fault_type
);
3604 * Fast fault code. The basic idea is to do as much as
3605 * possible while holding the map lock and object locks.
3606 * Busy pages are not used until the object lock has to
3607 * be dropped to do something (copy, zero fill, pmap enter).
3608 * Similarly, paging references aren't acquired until that
3609 * point, and object references aren't used.
3611 * If we can figure out what to do
3612 * (zero fill, copy on write, pmap enter) while holding
3613 * the locks, then it gets done. Otherwise, we give up,
3614 * and use the original fault path (which doesn't hold
3615 * the map lock, and relies on busy pages).
3616 * The give up cases include:
3617 * - Have to talk to pager.
3618 * - Page is busy, absent or in error.
3619 * - Pager has locked out desired access.
3620 * - Fault needs to be restarted.
3621 * - Have to push page into copy object.
3623 * The code is an infinite loop that moves one level down
3624 * the shadow chain each time. cur_object and cur_offset
3625 * refer to the current object being examined. object and offset
3626 * are the original object from the map. The loop is at the
3627 * top level if and only if object and cur_object are the same.
3629 * Invariants: Map lock is held throughout. Lock is held on
3630 * original object and cur_object (if different) when
3631 * continuing or exiting loop.
3635 #if defined(__arm64__)
3637 * Fail if reading an execute-only page in a
3638 * pmap that enforces execute-only protection.
3640 if (fault_type
== VM_PROT_READ
&&
3641 (prot
& VM_PROT_EXECUTE
) &&
3642 !(prot
& VM_PROT_READ
) &&
3643 pmap_enforces_execute_only(pmap
)) {
3644 vm_object_unlock(object
);
3645 vm_map_unlock_read(map
);
3646 if (real_map
!= map
) {
3647 vm_map_unlock(real_map
);
3649 kr
= KERN_PROTECTION_FAILURE
;
3655 * If this page is to be inserted in a copy delay object
3656 * for writing, and if the object has a copy, then the
3657 * copy delay strategy is implemented in the slow fault page.
3659 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
3660 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
)) {
3661 goto handle_copy_delay
;
3664 cur_object
= object
;
3665 cur_offset
= offset
;
3668 #if CONFIG_SECLUDED_MEMORY
3669 if (object
->can_grab_secluded
) {
3670 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
3672 #endif /* CONFIG_SECLUDED_MEMORY */
3675 if (!cur_object
->pager_created
&&
3676 cur_object
->phys_contiguous
) { /* superpage */
3680 if (cur_object
->blocked_access
) {
3682 * Access to this VM object has been blocked.
3683 * Let the slow path handle it.
3688 m
= vm_page_lookup(cur_object
, cur_offset
);
3691 if (m
!= VM_PAGE_NULL
) {
3692 m_object
= cur_object
;
3695 wait_result_t result
;
3698 * in order to do the PAGE_ASSERT_WAIT, we must
3699 * have object that 'm' belongs to locked exclusively
3701 if (object
!= cur_object
) {
3702 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3703 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3705 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3707 * couldn't upgrade so go do a full retry
3708 * immediately since we can no longer be
3709 * certain about cur_object (since we
3710 * don't hold a reference on it)...
3711 * first drop the top object lock
3713 vm_object_unlock(object
);
3715 vm_map_unlock_read(map
);
3716 if (real_map
!= map
) {
3717 vm_map_unlock(real_map
);
3723 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3724 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3726 if (vm_object_lock_upgrade(object
) == FALSE
) {
3728 * couldn't upgrade, so explictly take the lock
3729 * exclusively and go relookup the page since we
3730 * will have dropped the object lock and
3731 * a different thread could have inserted
3732 * a page at this offset
3733 * no need for a full retry since we're
3734 * at the top level of the object chain
3736 vm_object_lock(object
);
3741 if ((m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) && m_object
->internal
) {
3743 * m->vmp_busy == TRUE and the object is locked exclusively
3744 * if m->pageout_queue == TRUE after we acquire the
3745 * queues lock, we are guaranteed that it is stable on
3746 * the pageout queue and therefore reclaimable
3748 * NOTE: this is only true for the internal pageout queue
3749 * in the compressor world
3751 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3753 vm_page_lock_queues();
3755 if (m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) {
3756 vm_pageout_throttle_up(m
);
3757 vm_page_unlock_queues();
3759 PAGE_WAKEUP_DONE(m
);
3760 goto reclaimed_from_pageout
;
3762 vm_page_unlock_queues();
3764 if (object
!= cur_object
) {
3765 vm_object_unlock(object
);
3768 vm_map_unlock_read(map
);
3769 if (real_map
!= map
) {
3770 vm_map_unlock(real_map
);
3773 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
3775 vm_object_unlock(cur_object
);
3777 if (result
== THREAD_WAITING
) {
3778 result
= thread_block(THREAD_CONTINUE_NULL
);
3780 counter(c_vm_fault_page_block_busy_kernel
++);
3782 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
) {
3789 reclaimed_from_pageout
:
3790 if (m
->vmp_laundry
) {
3791 if (object
!= cur_object
) {
3792 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3793 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3795 vm_object_unlock(object
);
3796 vm_object_unlock(cur_object
);
3798 vm_map_unlock_read(map
);
3799 if (real_map
!= map
) {
3800 vm_map_unlock(real_map
);
3805 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3806 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3808 if (vm_object_lock_upgrade(object
) == FALSE
) {
3810 * couldn't upgrade, so explictly take the lock
3811 * exclusively and go relookup the page since we
3812 * will have dropped the object lock and
3813 * a different thread could have inserted
3814 * a page at this offset
3815 * no need for a full retry since we're
3816 * at the top level of the object chain
3818 vm_object_lock(object
);
3823 vm_pageout_steal_laundry(m
, FALSE
);
3826 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
3828 * Guard page: let the slow path deal with it
3832 if (m
->vmp_unusual
&& (m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_private
|| m
->vmp_absent
)) {
3834 * Unusual case... let the slow path deal with it
3838 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object
)) {
3839 if (object
!= cur_object
) {
3840 vm_object_unlock(object
);
3842 vm_map_unlock_read(map
);
3843 if (real_map
!= map
) {
3844 vm_map_unlock(real_map
);
3846 vm_object_unlock(cur_object
);
3847 kr
= KERN_MEMORY_ERROR
;
3850 assert(m_object
== VM_PAGE_OBJECT(m
));
3852 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
, m_object
) ||
3853 (physpage_p
!= NULL
&& (prot
& VM_PROT_WRITE
))) {
3854 upgrade_for_validation
:
3856 * We might need to validate this page
3857 * against its code signature, so we
3858 * want to hold the VM object exclusively.
3860 if (object
!= cur_object
) {
3861 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3862 vm_object_unlock(object
);
3863 vm_object_unlock(cur_object
);
3865 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3867 vm_map_unlock_read(map
);
3868 if (real_map
!= map
) {
3869 vm_map_unlock(real_map
);
3874 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3875 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3877 if (vm_object_lock_upgrade(object
) == FALSE
) {
3879 * couldn't upgrade, so explictly take the lock
3880 * exclusively and go relookup the page since we
3881 * will have dropped the object lock and
3882 * a different thread could have inserted
3883 * a page at this offset
3884 * no need for a full retry since we're
3885 * at the top level of the object chain
3887 vm_object_lock(object
);
3894 * Two cases of map in faults:
3895 * - At top level w/o copy object.
3896 * - Read fault anywhere.
3897 * --> must disallow write.
3900 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
3904 if ((fault_type
& VM_PROT_WRITE
) == 0) {
3905 if (!pmap_has_prot_policy(prot
)) {
3906 prot
&= ~VM_PROT_WRITE
;
3909 * For a protection that the pmap cares
3910 * about, we must hand over the full
3911 * set of protections (so that the pmap
3912 * layer can apply any desired policy).
3913 * This means that cs_bypass must be
3914 * set, as this can force us to pass
3917 assert(fault_info
.cs_bypass
);
3920 if (object
!= cur_object
) {
3922 * We still need to hold the top object
3923 * lock here to prevent a race between
3924 * a read fault (taking only "shared"
3925 * locks) and a write fault (taking
3926 * an "exclusive" lock on the top
3928 * Otherwise, as soon as we release the
3929 * top lock, the write fault could
3930 * proceed and actually complete before
3931 * the read fault, and the copied page's
3932 * translation could then be overwritten
3933 * by the read fault's translation for
3934 * the original page.
3936 * Let's just record what the top object
3937 * is and we'll release it later.
3939 top_object
= object
;
3942 * switch to the object that has the new page
3944 object
= cur_object
;
3945 object_lock_type
= cur_object_lock_type
;
3948 assert(m_object
== VM_PAGE_OBJECT(m
));
3951 * prepare for the pmap_enter...
3952 * object and map are both locked
3953 * m contains valid data
3954 * object == m->vmp_object
3955 * cur_object == NULL or it's been unlocked
3956 * no paging references on either object or cur_object
3958 if (top_object
!= VM_OBJECT_NULL
|| object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
3959 need_retry_ptr
= &need_retry
;
3961 need_retry_ptr
= NULL
;
3965 kr
= vm_fault_enter(m
,
3977 kr
= vm_fault_enter(m
,
3989 #if DEVELOPMENT || DEBUG
3993 if (m_object
->internal
) {
3994 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
3995 } else if (m_object
->object_is_shared_cache
) {
3996 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
3998 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
4001 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, event_code
, trace_real_vaddr
, (fault_info
.user_tag
<< 16) | (caller_prot
<< 8) | type_of_fault
, m
->vmp_offset
, get_current_unique_pid(), 0);
4003 DTRACE_VM6(real_fault
, vm_map_offset_t
, real_vaddr
, vm_map_offset_t
, m
->vmp_offset
, int, event_code
, int, caller_prot
, int, type_of_fault
, int, fault_info
.user_tag
);
4006 if (kr
== KERN_SUCCESS
&&
4007 physpage_p
!= NULL
) {
4008 /* for vm_map_wire_and_extract() */
4009 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
4010 if (prot
& VM_PROT_WRITE
) {
4011 vm_object_lock_assert_exclusive(m_object
);
4012 m
->vmp_dirty
= TRUE
;
4016 if (top_object
!= VM_OBJECT_NULL
) {
4018 * It's safe to drop the top object
4019 * now that we've done our
4020 * vm_fault_enter(). Any other fault
4021 * in progress for that virtual
4022 * address will either find our page
4023 * and translation or put in a new page
4026 vm_object_unlock(top_object
);
4027 top_object
= VM_OBJECT_NULL
;
4030 if (need_collapse
== TRUE
) {
4031 vm_object_collapse(object
, offset
, TRUE
);
4034 if (need_retry
== FALSE
&&
4035 (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
)) {
4037 * evaluate access pattern and update state
4038 * vm_fault_deactivate_behind depends on the
4039 * state being up to date
4041 vm_fault_is_sequential(m_object
, cur_offset
, fault_info
.behavior
);
4043 vm_fault_deactivate_behind(m_object
, cur_offset
, fault_info
.behavior
);
4046 * That's it, clean up and return.
4049 PAGE_WAKEUP_DONE(m
);
4052 if (need_retry
== FALSE
&& !m_object
->internal
&& (fault_type
& VM_PROT_WRITE
)) {
4053 vm_object_paging_begin(m_object
);
4055 assert(written_on_object
== VM_OBJECT_NULL
);
4056 written_on_object
= m_object
;
4057 written_on_pager
= m_object
->pager
;
4058 written_on_offset
= m_object
->paging_offset
+ m
->vmp_offset
;
4060 vm_object_unlock(object
);
4062 vm_map_unlock_read(map
);
4063 if (real_map
!= map
) {
4064 vm_map_unlock(real_map
);
4067 if (need_retry
== TRUE
) {
4069 * vm_fault_enter couldn't complete the PMAP_ENTER...
4070 * at this point we don't hold any locks so it's safe
4071 * to ask the pmap layer to expand the page table to
4072 * accommodate this mapping... once expanded, we'll
4073 * re-drive the fault which should result in vm_fault_enter
4074 * being able to successfully enter the mapping this time around
4076 (void)pmap_enter_options(
4077 pmap
, vaddr
, 0, 0, 0, 0, 0,
4078 PMAP_OPTIONS_NOENTER
, NULL
);
4086 * COPY ON WRITE FAULT
4088 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
4091 * If objects match, then
4092 * object->copy must not be NULL (else control
4093 * would be in previous code block), and we
4094 * have a potential push into the copy object
4095 * with which we can't cope with here.
4097 if (cur_object
== object
) {
4099 * must take the slow path to
4100 * deal with the copy push
4106 * This is now a shadow based copy on write
4107 * fault -- it requires a copy up the shadow
4110 assert(m_object
== VM_PAGE_OBJECT(m
));
4112 if ((cur_object_lock_type
== OBJECT_LOCK_SHARED
) &&
4113 VM_FAULT_NEED_CS_VALIDATION(NULL
, m
, m_object
)) {
4114 goto upgrade_for_validation
;
4118 * Allocate a page in the original top level
4119 * object. Give up if allocate fails. Also
4120 * need to remember current page, as it's the
4121 * source of the copy.
4123 * at this point we hold locks on both
4124 * object and cur_object... no need to take
4125 * paging refs or mark pages BUSY since
4126 * we don't drop either object lock until
4127 * the page has been copied and inserted
4130 m
= vm_page_grab_options(grab_options
);
4133 if (m
== VM_PAGE_NULL
) {
4135 * no free page currently available...
4136 * must take the slow path
4141 * Now do the copy. Mark the source page busy...
4143 * NOTE: This code holds the map lock across
4146 vm_page_copy(cur_m
, m
);
4147 vm_page_insert(m
, object
, offset
);
4149 SET_PAGE_DIRTY(m
, FALSE
);
4152 * Now cope with the source page and object
4154 if (object
->ref_count
> 1 && cur_m
->vmp_pmapped
) {
4155 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m
));
4158 if (cur_m
->vmp_clustered
) {
4159 VM_PAGE_COUNT_AS_PAGEIN(cur_m
);
4160 VM_PAGE_CONSUME_CLUSTERED(cur_m
);
4161 vm_fault_is_sequential(cur_object
, cur_offset
, fault_info
.behavior
);
4163 need_collapse
= TRUE
;
4165 if (!cur_object
->internal
&&
4166 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
4168 * The object from which we've just
4169 * copied a page is most probably backed
4170 * by a vnode. We don't want to waste too
4171 * much time trying to collapse the VM objects
4172 * and create a bottleneck when several tasks
4173 * map the same file.
4175 if (cur_object
->copy
== object
) {
4177 * Shared mapping or no COW yet.
4178 * We can never collapse a copy
4179 * object into its backing object.
4181 need_collapse
= FALSE
;
4182 } else if (cur_object
->copy
== object
->shadow
&&
4183 object
->shadow
->resident_page_count
== 0) {
4185 * Shared mapping after a COW occurred.
4187 need_collapse
= FALSE
;
4190 vm_object_unlock(cur_object
);
4192 if (need_collapse
== FALSE
) {
4193 vm_fault_collapse_skipped
++;
4195 vm_fault_collapse_total
++;
4197 type_of_fault
= DBG_COW_FAULT
;
4198 VM_STAT_INCR(cow_faults
);
4199 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
4200 current_task()->cow_faults
++;
4205 * No page at cur_object, cur_offset... m == NULL
4207 if (cur_object
->pager_created
) {
4208 int compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
4210 if (MUST_ASK_PAGER(cur_object
, cur_offset
, compressor_external_state
) == TRUE
) {
4212 int c_flags
= C_DONT_BLOCK
;
4213 boolean_t insert_cur_object
= FALSE
;
4216 * May have to talk to a pager...
4217 * if so, take the slow path by
4218 * doing a 'break' from the while (TRUE) loop
4220 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
4221 * if the compressor is active and the page exists there
4223 if (compressor_external_state
!= VM_EXTERNAL_STATE_EXISTS
) {
4227 if (map
== kernel_map
|| real_map
== kernel_map
) {
4229 * can't call into the compressor with the kernel_map
4230 * lock held, since the compressor may try to operate
4231 * on the kernel map in order to return an empty c_segment
4235 if (object
!= cur_object
) {
4236 if (fault_type
& VM_PROT_WRITE
) {
4239 insert_cur_object
= TRUE
;
4242 if (insert_cur_object
== TRUE
) {
4243 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4244 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4246 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
4248 * couldn't upgrade so go do a full retry
4249 * immediately since we can no longer be
4250 * certain about cur_object (since we
4251 * don't hold a reference on it)...
4252 * first drop the top object lock
4254 vm_object_unlock(object
);
4256 vm_map_unlock_read(map
);
4257 if (real_map
!= map
) {
4258 vm_map_unlock(real_map
);
4264 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4265 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4267 if (object
!= cur_object
) {
4269 * we can't go for the upgrade on the top
4270 * lock since the upgrade may block waiting
4271 * for readers to drain... since we hold
4272 * cur_object locked at this point, waiting
4273 * for the readers to drain would represent
4274 * a lock order inversion since the lock order
4275 * for objects is the reference order in the
4278 vm_object_unlock(object
);
4279 vm_object_unlock(cur_object
);
4281 vm_map_unlock_read(map
);
4282 if (real_map
!= map
) {
4283 vm_map_unlock(real_map
);
4288 if (vm_object_lock_upgrade(object
) == FALSE
) {
4290 * couldn't upgrade, so explictly take the lock
4291 * exclusively and go relookup the page since we
4292 * will have dropped the object lock and
4293 * a different thread could have inserted
4294 * a page at this offset
4295 * no need for a full retry since we're
4296 * at the top level of the object chain
4298 vm_object_lock(object
);
4303 m
= vm_page_grab_options(grab_options
);
4306 if (m
== VM_PAGE_NULL
) {
4308 * no free page currently available...
4309 * must take the slow path
4315 * The object is and remains locked
4316 * so no need to take a
4317 * "paging_in_progress" reference.
4319 boolean_t shared_lock
;
4320 if ((object
== cur_object
&&
4321 object_lock_type
== OBJECT_LOCK_EXCLUSIVE
) ||
4322 (object
!= cur_object
&&
4323 cur_object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)) {
4324 shared_lock
= FALSE
;
4329 kr
= vm_compressor_pager_get(
4332 cur_object
->paging_offset
),
4333 VM_PAGE_GET_PHYS_PAGE(m
),
4336 &compressed_count_delta
);
4338 vm_compressor_pager_count(
4340 compressed_count_delta
,
4344 if (kr
!= KERN_SUCCESS
) {
4345 vm_page_release(m
, FALSE
);
4349 m
->vmp_dirty
= TRUE
;
4352 * If the object is purgeable, its
4353 * owner's purgeable ledgers will be
4354 * updated in vm_page_insert() but the
4355 * page was also accounted for in a
4356 * "compressed purgeable" ledger, so
4359 if (object
!= cur_object
&&
4360 !insert_cur_object
) {
4362 * We're not going to insert
4363 * the decompressed page into
4364 * the object it came from.
4366 * We're dealing with a
4367 * copy-on-write fault on
4369 * We're going to decompress
4370 * the page directly into the
4371 * target "object" while
4372 * keepin the compressed
4373 * page for "cur_object", so
4374 * no ledger update in that
4377 } else if (((cur_object
->purgable
==
4378 VM_PURGABLE_DENY
) &&
4379 (!cur_object
->vo_ledger_tag
)) ||
4380 (cur_object
->vo_owner
==
4383 * "cur_object" is not purgeable
4384 * and is not ledger-taged, or
4385 * there's no owner for it,
4386 * so no owner's ledgers to
4391 * One less compressed
4392 * purgeable/tagged page for
4393 * cur_object's owner.
4395 vm_object_owner_compressed_update(
4400 if (insert_cur_object
) {
4401 vm_page_insert(m
, cur_object
, cur_offset
);
4402 m_object
= cur_object
;
4404 vm_page_insert(m
, object
, offset
);
4408 if ((m_object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_USE_DEFAULT
) {
4410 * If the page is not cacheable,
4411 * we can't let its contents
4412 * linger in the data cache
4413 * after the decompression.
4415 pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m
));
4418 type_of_fault
= my_fault_type
;
4420 VM_STAT_INCR(decompressions
);
4422 if (cur_object
!= object
) {
4423 if (insert_cur_object
) {
4424 top_object
= object
;
4426 * switch to the object that has the new page
4428 object
= cur_object
;
4429 object_lock_type
= cur_object_lock_type
;
4431 vm_object_unlock(cur_object
);
4432 cur_object
= object
;
4438 * existence map present and indicates
4439 * that the pager doesn't have this page
4442 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
4444 * Zero fill fault. Page gets
4445 * inserted into the original object.
4447 if (cur_object
->shadow_severed
||
4448 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
) ||
4449 cur_object
== compressor_object
||
4450 cur_object
== kernel_object
||
4451 cur_object
== vm_submap_object
) {
4452 if (object
!= cur_object
) {
4453 vm_object_unlock(cur_object
);
4455 vm_object_unlock(object
);
4457 vm_map_unlock_read(map
);
4458 if (real_map
!= map
) {
4459 vm_map_unlock(real_map
);
4462 kr
= KERN_MEMORY_ERROR
;
4465 if (cur_object
!= object
) {
4466 vm_object_unlock(cur_object
);
4468 cur_object
= object
;
4470 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4471 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4473 if (vm_object_lock_upgrade(object
) == FALSE
) {
4475 * couldn't upgrade so do a full retry on the fault
4476 * since we dropped the object lock which
4477 * could allow another thread to insert
4478 * a page at this offset
4480 vm_map_unlock_read(map
);
4481 if (real_map
!= map
) {
4482 vm_map_unlock(real_map
);
4488 m
= vm_page_alloc(object
, offset
);
4491 if (m
== VM_PAGE_NULL
) {
4493 * no free page currently available...
4494 * must take the slow path
4501 * Now zero fill page...
4502 * the page is probably going to
4503 * be written soon, so don't bother
4504 * to clear the modified bit
4506 * NOTE: This code holds the map
4507 * lock across the zero fill.
4509 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
4514 * On to the next level in the shadow chain
4516 cur_offset
+= cur_object
->vo_shadow_offset
;
4517 new_object
= cur_object
->shadow
;
4520 * take the new_object's lock with the indicated state
4522 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4523 vm_object_lock_shared(new_object
);
4525 vm_object_lock(new_object
);
4528 if (cur_object
!= object
) {
4529 vm_object_unlock(cur_object
);
4532 cur_object
= new_object
;
4538 * Cleanup from fast fault failure. Drop any object
4539 * lock other than original and drop map lock.
4541 if (object
!= cur_object
) {
4542 vm_object_unlock(cur_object
);
4546 * must own the object lock exclusively at this point
4548 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4549 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4551 if (vm_object_lock_upgrade(object
) == FALSE
) {
4553 * couldn't upgrade, so explictly
4554 * take the lock exclusively
4555 * no need to retry the fault at this
4556 * point since "vm_fault_page" will
4557 * completely re-evaluate the state
4559 vm_object_lock(object
);
4564 vm_map_unlock_read(map
);
4565 if (real_map
!= map
) {
4566 vm_map_unlock(real_map
);
4569 if (__improbable(object
== compressor_object
||
4570 object
== kernel_object
||
4571 object
== vm_submap_object
)) {
4573 * These objects are explicitly managed and populated by the
4574 * kernel. The virtual ranges backed by these objects should
4575 * either have wired pages or "holes" that are not supposed to
4576 * be accessed at all until they get explicitly populated.
4577 * We should never have to resolve a fault on a mapping backed
4578 * by one of these VM objects and providing a zero-filled page
4579 * would be wrong here, so let's fail the fault and let the
4580 * caller crash or recover.
4582 vm_object_unlock(object
);
4583 kr
= KERN_MEMORY_ERROR
;
4587 assert(object
!= compressor_object
);
4588 assert(object
!= kernel_object
);
4589 assert(object
!= vm_submap_object
);
4592 * Make a reference to this object to
4593 * prevent its disposal while we are messing with
4594 * it. Once we have the reference, the map is free
4595 * to be diddled. Since objects reference their
4596 * shadows (and copies), they will stay around as well.
4598 vm_object_reference_locked(object
);
4599 vm_object_paging_begin(object
);
4601 XPR(XPR_VM_FAULT
, "vm_fault -> vm_fault_page\n", 0, 0, 0, 0, 0);
4605 result_page
= VM_PAGE_NULL
;
4606 kr
= vm_fault_page(object
, offset
, fault_type
,
4607 (change_wiring
&& !wired
),
4608 FALSE
, /* page not looked up */
4609 &prot
, &result_page
, &top_page
,
4611 &error_code
, map
->no_zero_fill
,
4612 FALSE
, &fault_info
);
4615 * if kr != VM_FAULT_SUCCESS, then the paging reference
4616 * has been dropped and the object unlocked... the ref_count
4619 * if kr == VM_FAULT_SUCCESS, then the paging reference
4620 * is still held along with the ref_count on the original object
4622 * the object is returned locked with a paging reference
4624 * if top_page != NULL, then it's BUSY and the
4625 * object it belongs to has a paging reference
4626 * but is returned unlocked
4628 if (kr
!= VM_FAULT_SUCCESS
&&
4629 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
4631 * we didn't succeed, lose the object reference immediately.
4633 vm_object_deallocate(object
);
4636 * See why we failed, and take corrective action.
4639 case VM_FAULT_MEMORY_SHORTAGE
:
4640 if (vm_page_wait((change_wiring
) ?
4642 THREAD_ABORTSAFE
)) {
4648 case VM_FAULT_INTERRUPTED
:
4651 case VM_FAULT_RETRY
:
4653 case VM_FAULT_MEMORY_ERROR
:
4657 kr
= KERN_MEMORY_ERROR
;
4661 panic("vm_fault: unexpected error 0x%x from "
4662 "vm_fault_page()\n", kr
);
4668 if (m
!= VM_PAGE_NULL
) {
4669 m_object
= VM_PAGE_OBJECT(m
);
4670 assert((change_wiring
&& !wired
) ?
4671 (top_page
== VM_PAGE_NULL
) :
4672 ((top_page
== VM_PAGE_NULL
) == (m_object
== object
)));
4676 * What to do with the resulting page from vm_fault_page
4677 * if it doesn't get entered into the physical map:
4679 #define RELEASE_PAGE(m) \
4681 PAGE_WAKEUP_DONE(m); \
4682 if ( !VM_PAGE_PAGEABLE(m)) { \
4683 vm_page_lockspin_queues(); \
4684 if ( !VM_PAGE_PAGEABLE(m)) \
4685 vm_page_activate(m); \
4686 vm_page_unlock_queues(); \
4691 object_locks_dropped
= FALSE
;
4693 * We must verify that the maps have not changed
4694 * since our last lookup. vm_map_verify() needs the
4695 * map lock (shared) but we are holding object locks.
4696 * So we do a try_lock() first and, if that fails, we
4697 * drop the object locks and go in for the map lock again.
4699 if (!vm_map_try_lock_read(original_map
)) {
4700 if (m
!= VM_PAGE_NULL
) {
4701 old_copy_object
= m_object
->copy
;
4702 vm_object_unlock(m_object
);
4704 old_copy_object
= VM_OBJECT_NULL
;
4705 vm_object_unlock(object
);
4708 object_locks_dropped
= TRUE
;
4710 vm_map_lock_read(original_map
);
4713 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
4714 if (object_locks_dropped
== FALSE
) {
4715 if (m
!= VM_PAGE_NULL
) {
4716 old_copy_object
= m_object
->copy
;
4717 vm_object_unlock(m_object
);
4719 old_copy_object
= VM_OBJECT_NULL
;
4720 vm_object_unlock(object
);
4723 object_locks_dropped
= TRUE
;
4727 * no object locks are held at this point
4729 vm_object_t retry_object
;
4730 vm_object_offset_t retry_offset
;
4731 vm_prot_t retry_prot
;
4734 * To avoid trying to write_lock the map while another
4735 * thread has it read_locked (in vm_map_pageable), we
4736 * do not try for write permission. If the page is
4737 * still writable, we will get write permission. If it
4738 * is not, or has been marked needs_copy, we enter the
4739 * mapping without write permission, and will merely
4740 * take another fault.
4744 kr
= vm_map_lookup_locked(&map
, vaddr
,
4745 fault_type
& ~VM_PROT_WRITE
,
4746 OBJECT_LOCK_EXCLUSIVE
, &version
,
4747 &retry_object
, &retry_offset
, &retry_prot
,
4751 pmap
= real_map
->pmap
;
4753 if (kr
!= KERN_SUCCESS
) {
4754 vm_map_unlock_read(map
);
4756 if (m
!= VM_PAGE_NULL
) {
4757 assert(VM_PAGE_OBJECT(m
) == m_object
);
4760 * retake the lock so that
4761 * we can drop the paging reference
4762 * in vm_fault_cleanup and do the
4763 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4765 vm_object_lock(m_object
);
4769 vm_fault_cleanup(m_object
, top_page
);
4772 * retake the lock so that
4773 * we can drop the paging reference
4774 * in vm_fault_cleanup
4776 vm_object_lock(object
);
4778 vm_fault_cleanup(object
, top_page
);
4780 vm_object_deallocate(object
);
4784 vm_object_unlock(retry_object
);
4786 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
4787 vm_map_unlock_read(map
);
4788 if (real_map
!= map
) {
4789 vm_map_unlock(real_map
);
4792 if (m
!= VM_PAGE_NULL
) {
4793 assert(VM_PAGE_OBJECT(m
) == m_object
);
4796 * retake the lock so that
4797 * we can drop the paging reference
4798 * in vm_fault_cleanup and do the
4799 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4801 vm_object_lock(m_object
);
4805 vm_fault_cleanup(m_object
, top_page
);
4808 * retake the lock so that
4809 * we can drop the paging reference
4810 * in vm_fault_cleanup
4812 vm_object_lock(object
);
4814 vm_fault_cleanup(object
, top_page
);
4816 vm_object_deallocate(object
);
4821 * Check whether the protection has changed or the object
4822 * has been copied while we left the map unlocked.
4824 if (pmap_has_prot_policy(retry_prot
)) {
4825 /* If the pmap layer cares, pass the full set. */
4832 if (object_locks_dropped
== TRUE
) {
4833 if (m
!= VM_PAGE_NULL
) {
4834 vm_object_lock(m_object
);
4836 if (m_object
->copy
!= old_copy_object
) {
4838 * The copy object changed while the top-level object
4839 * was unlocked, so take away write permission.
4841 assert(!pmap_has_prot_policy(prot
));
4842 prot
&= ~VM_PROT_WRITE
;
4845 vm_object_lock(object
);
4848 object_locks_dropped
= FALSE
;
4852 * If we want to wire down this page, but no longer have
4853 * adequate permissions, we must start all over.
4855 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
4856 vm_map_unlock_read(map
);
4857 if (real_map
!= map
) {
4858 vm_map_unlock(real_map
);
4861 if (m
!= VM_PAGE_NULL
) {
4862 assert(VM_PAGE_OBJECT(m
) == m_object
);
4866 vm_fault_cleanup(m_object
, top_page
);
4868 vm_fault_cleanup(object
, top_page
);
4871 vm_object_deallocate(object
);
4875 if (m
!= VM_PAGE_NULL
) {
4877 * Put this page into the physical map.
4878 * We had to do the unlock above because pmap_enter
4879 * may cause other faults. The page may be on
4880 * the pageout queues. If the pageout daemon comes
4881 * across the page, it will remove it from the queues.
4884 kr
= vm_fault_enter(m
,
4896 kr
= vm_fault_enter(m
,
4908 assert(VM_PAGE_OBJECT(m
) == m_object
);
4910 #if DEVELOPMENT || DEBUG
4914 if (m_object
->internal
) {
4915 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
4916 } else if (m_object
->object_is_shared_cache
) {
4917 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
4919 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
4922 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, event_code
, trace_real_vaddr
, (fault_info
.user_tag
<< 16) | (caller_prot
<< 8) | type_of_fault
, m
->vmp_offset
, get_current_unique_pid(), 0);
4924 DTRACE_VM6(real_fault
, vm_map_offset_t
, real_vaddr
, vm_map_offset_t
, m
->vmp_offset
, int, event_code
, int, caller_prot
, int, type_of_fault
, int, fault_info
.user_tag
);
4927 if (kr
!= KERN_SUCCESS
) {
4928 /* abort this page fault */
4929 vm_map_unlock_read(map
);
4930 if (real_map
!= map
) {
4931 vm_map_unlock(real_map
);
4933 PAGE_WAKEUP_DONE(m
);
4934 vm_fault_cleanup(m_object
, top_page
);
4935 vm_object_deallocate(object
);
4938 if (physpage_p
!= NULL
) {
4939 /* for vm_map_wire_and_extract() */
4940 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
4941 if (prot
& VM_PROT_WRITE
) {
4942 vm_object_lock_assert_exclusive(m_object
);
4943 m
->vmp_dirty
= TRUE
;
4947 vm_map_entry_t entry
;
4948 vm_map_offset_t laddr
;
4949 vm_map_offset_t ldelta
, hdelta
;
4952 * do a pmap block mapping from the physical address
4956 if (real_map
!= map
) {
4957 vm_map_unlock(real_map
);
4960 if (original_map
!= map
) {
4961 vm_map_unlock_read(map
);
4962 vm_map_lock_read(original_map
);
4968 hdelta
= 0xFFFFF000;
4969 ldelta
= 0xFFFFF000;
4971 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
4972 if (ldelta
> (laddr
- entry
->vme_start
)) {
4973 ldelta
= laddr
- entry
->vme_start
;
4975 if (hdelta
> (entry
->vme_end
- laddr
)) {
4976 hdelta
= entry
->vme_end
- laddr
;
4978 if (entry
->is_sub_map
) {
4979 laddr
= ((laddr
- entry
->vme_start
)
4980 + VME_OFFSET(entry
));
4981 vm_map_lock_read(VME_SUBMAP(entry
));
4983 if (map
!= real_map
) {
4984 vm_map_unlock_read(map
);
4986 if (entry
->use_pmap
) {
4987 vm_map_unlock_read(real_map
);
4988 real_map
= VME_SUBMAP(entry
);
4990 map
= VME_SUBMAP(entry
);
4996 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
4997 (VME_OBJECT(entry
) != NULL
) &&
4998 (VME_OBJECT(entry
) == object
)) {
5001 if (!object
->pager_created
&&
5002 object
->phys_contiguous
&&
5003 VME_OFFSET(entry
) == 0 &&
5004 (entry
->vme_end
- entry
->vme_start
== object
->vo_size
) &&
5005 VM_MAP_PAGE_ALIGNED(entry
->vme_start
, (object
->vo_size
- 1))) {
5006 superpage
= VM_MEM_SUPERPAGE
;
5011 if (superpage
&& physpage_p
) {
5012 /* for vm_map_wire_and_extract() */
5013 *physpage_p
= (ppnum_t
)
5014 ((((vm_map_offset_t
)
5015 object
->vo_shadow_offset
)
5017 + (laddr
- entry
->vme_start
))
5023 * Set up a block mapped area
5025 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5026 kr
= pmap_map_block(caller_pmap
,
5027 (addr64_t
)(caller_pmap_addr
- ldelta
),
5028 (ppnum_t
)((((vm_map_offset_t
) (VME_OBJECT(entry
)->vo_shadow_offset
)) +
5029 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5030 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5031 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5033 if (kr
!= KERN_SUCCESS
) {
5038 * Set up a block mapped area
5040 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5041 kr
= pmap_map_block(real_map
->pmap
,
5042 (addr64_t
)(vaddr
- ldelta
),
5043 (ppnum_t
)((((vm_map_offset_t
)(VME_OBJECT(entry
)->vo_shadow_offset
)) +
5044 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5045 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5046 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5048 if (kr
!= KERN_SUCCESS
) {
5061 * TODO: could most of the done cases just use cleanup?
5065 * Unlock everything, and return
5067 vm_map_unlock_read(map
);
5068 if (real_map
!= map
) {
5069 vm_map_unlock(real_map
);
5072 if (m
!= VM_PAGE_NULL
) {
5073 assert(VM_PAGE_OBJECT(m
) == m_object
);
5075 if (!m_object
->internal
&& (fault_type
& VM_PROT_WRITE
)) {
5076 vm_object_paging_begin(m_object
);
5078 assert(written_on_object
== VM_OBJECT_NULL
);
5079 written_on_object
= m_object
;
5080 written_on_pager
= m_object
->pager
;
5081 written_on_offset
= m_object
->paging_offset
+ m
->vmp_offset
;
5083 PAGE_WAKEUP_DONE(m
);
5085 vm_fault_cleanup(m_object
, top_page
);
5087 vm_fault_cleanup(object
, top_page
);
5090 vm_object_deallocate(object
);
5095 thread_interrupt_level(interruptible_state
);
5098 * Only I/O throttle on faults which cause a pagein/swapin.
5100 if ((type_of_fault
== DBG_PAGEIND_FAULT
) || (type_of_fault
== DBG_PAGEINV_FAULT
) || (type_of_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
)) {
5101 throttle_lowpri_io(1);
5103 if (kr
== KERN_SUCCESS
&& type_of_fault
!= DBG_CACHE_HIT_FAULT
&& type_of_fault
!= DBG_GUARD_FAULT
) {
5104 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
5105 if (vm_debug_events
) {
5106 if (type_of_fault
== DBG_COMPRESSOR_FAULT
) {
5107 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5108 } else if (type_of_fault
== DBG_COW_FAULT
) {
5109 VM_DEBUG_EVENT(vmf_cowdelay
, VMF_COWDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5111 VM_DEBUG_EVENT(vmf_zfdelay
, VMF_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5114 delay(throttle_delay
);
5119 if (written_on_object
) {
5120 vnode_pager_dirtied(written_on_pager
, written_on_offset
, written_on_offset
+ PAGE_SIZE_64
);
5122 vm_object_lock(written_on_object
);
5123 vm_object_paging_end(written_on_object
);
5124 vm_object_unlock(written_on_object
);
5126 written_on_object
= VM_OBJECT_NULL
;
5130 vm_record_rtfault(cthread
, fstart
, trace_vaddr
, type_of_fault
);
5133 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5134 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
5135 ((uint64_t)trace_vaddr
>> 32),
5147 * Wire down a range of virtual addresses in a map.
5152 vm_map_entry_t entry
,
5156 vm_map_offset_t pmap_addr
,
5157 ppnum_t
*physpage_p
)
5160 vm_map_offset_t end_addr
= entry
->vme_end
;
5163 assert(entry
->in_transition
);
5165 if ((VME_OBJECT(entry
) != NULL
) &&
5166 !entry
->is_sub_map
&&
5167 VME_OBJECT(entry
)->phys_contiguous
) {
5168 return KERN_SUCCESS
;
5172 * Inform the physical mapping system that the
5173 * range of addresses may not fault, so that
5174 * page tables and such can be locked down as well.
5177 pmap_pageable(pmap
, pmap_addr
,
5178 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
5181 * We simulate a fault to get the page and enter it
5182 * in the physical map.
5185 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
5186 rc
= vm_fault_wire_fast(map
, va
, prot
, wire_tag
, entry
, pmap
,
5187 pmap_addr
+ (va
- entry
->vme_start
),
5189 if (rc
!= KERN_SUCCESS
) {
5190 rc
= vm_fault_internal(map
, va
, prot
, TRUE
, wire_tag
,
5191 ((pmap
== kernel_pmap
)
5193 : THREAD_ABORTSAFE
),
5196 (va
- entry
->vme_start
)),
5198 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
5201 if (rc
!= KERN_SUCCESS
) {
5202 struct vm_map_entry tmp_entry
= *entry
;
5204 /* unwire wired pages */
5205 tmp_entry
.vme_end
= va
;
5206 vm_fault_unwire(map
,
5207 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
5212 return KERN_SUCCESS
;
5218 * Unwire a range of virtual addresses in a map.
5223 vm_map_entry_t entry
,
5224 boolean_t deallocate
,
5226 vm_map_offset_t pmap_addr
)
5229 vm_map_offset_t end_addr
= entry
->vme_end
;
5231 struct vm_object_fault_info fault_info
= {};
5232 unsigned int unwired_pages
;
5234 object
= (entry
->is_sub_map
) ? VM_OBJECT_NULL
: VME_OBJECT(entry
);
5237 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
5238 * do anything since such memory is wired by default. So we don't have
5239 * anything to undo here.
5242 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
) {
5246 fault_info
.interruptible
= THREAD_UNINT
;
5247 fault_info
.behavior
= entry
->behavior
;
5248 fault_info
.user_tag
= VME_ALIAS(entry
);
5249 if (entry
->iokit_acct
||
5250 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
5251 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
5253 fault_info
.lo_offset
= VME_OFFSET(entry
);
5254 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + VME_OFFSET(entry
);
5255 fault_info
.no_cache
= entry
->no_cache
;
5256 fault_info
.stealth
= TRUE
;
5261 * Since the pages are wired down, we must be able to
5262 * get their mappings from the physical map system.
5265 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
5266 if (object
== VM_OBJECT_NULL
) {
5268 pmap_change_wiring(pmap
,
5269 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5271 (void) vm_fault(map
, va
, VM_PROT_NONE
,
5272 TRUE
, VM_KERN_MEMORY_NONE
, THREAD_UNINT
, pmap
, pmap_addr
);
5275 vm_page_t result_page
;
5277 vm_object_t result_object
;
5278 vm_fault_return_t result
;
5280 /* cap cluster size at maximum UPL size */
5281 upl_size_t cluster_size
;
5282 if (os_sub_overflow(end_addr
, va
, &cluster_size
)) {
5283 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
5285 fault_info
.cluster_size
= cluster_size
;
5288 prot
= VM_PROT_NONE
;
5290 vm_object_lock(object
);
5291 vm_object_paging_begin(object
);
5293 "vm_fault_unwire -> vm_fault_page\n",
5295 result_page
= VM_PAGE_NULL
;
5296 result
= vm_fault_page(
5298 (VME_OFFSET(entry
) +
5299 (va
- entry
->vme_start
)),
5301 FALSE
, /* page not looked up */
5302 &prot
, &result_page
, &top_page
,
5304 NULL
, map
->no_zero_fill
,
5305 FALSE
, &fault_info
);
5306 } while (result
== VM_FAULT_RETRY
);
5309 * If this was a mapping to a file on a device that has been forcibly
5310 * unmounted, then we won't get a page back from vm_fault_page(). Just
5311 * move on to the next one in case the remaining pages are mapped from
5312 * different objects. During a forced unmount, the object is terminated
5313 * so the alive flag will be false if this happens. A forced unmount will
5314 * will occur when an external disk is unplugged before the user does an
5315 * eject, so we don't want to panic in that situation.
5318 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
) {
5322 if (result
== VM_FAULT_MEMORY_ERROR
&&
5323 object
== kernel_object
) {
5325 * This must have been allocated with
5326 * KMA_KOBJECT and KMA_VAONLY and there's
5327 * no physical page at this offset.
5328 * We're done (no page to free).
5334 if (result
!= VM_FAULT_SUCCESS
) {
5335 panic("vm_fault_unwire: failure");
5338 result_object
= VM_PAGE_OBJECT(result_page
);
5341 assert(VM_PAGE_GET_PHYS_PAGE(result_page
) !=
5342 vm_page_fictitious_addr
);
5343 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page
));
5344 if (VM_PAGE_WIRED(result_page
)) {
5347 VM_PAGE_FREE(result_page
);
5349 if ((pmap
) && (VM_PAGE_GET_PHYS_PAGE(result_page
) != vm_page_guard_addr
)) {
5350 pmap_change_wiring(pmap
,
5351 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5355 if (VM_PAGE_WIRED(result_page
)) {
5356 vm_page_lockspin_queues();
5357 vm_page_unwire(result_page
, TRUE
);
5358 vm_page_unlock_queues();
5361 if (entry
->zero_wired_pages
) {
5362 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page
));
5363 entry
->zero_wired_pages
= FALSE
;
5366 PAGE_WAKEUP_DONE(result_page
);
5368 vm_fault_cleanup(result_object
, top_page
);
5373 * Inform the physical mapping system that the range
5374 * of addresses may fault, so that page tables and
5375 * such may be unwired themselves.
5378 pmap_pageable(pmap
, pmap_addr
,
5379 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
5381 if (kernel_object
== object
) {
5382 vm_tag_update_size(fault_info
.user_tag
, -ptoa_64(unwired_pages
));
5387 * vm_fault_wire_fast:
5389 * Handle common case of a wire down page fault at the given address.
5390 * If successful, the page is inserted into the associated physical map.
5391 * The map entry is passed in to avoid the overhead of a map lookup.
5393 * NOTE: the given address should be truncated to the
5394 * proper page address.
5396 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
5397 * a standard error specifying why the fault is fatal is returned.
5399 * The map in question must be referenced, and remains so.
5400 * Caller has a read lock on the map.
5402 * This is a stripped version of vm_fault() for wiring pages. Anything
5403 * other than the common case will return KERN_FAILURE, and the caller
5404 * is expected to call vm_fault().
5406 static kern_return_t
5408 __unused vm_map_t map
,
5410 __unused vm_prot_t caller_prot
,
5412 vm_map_entry_t entry
,
5414 vm_map_offset_t pmap_addr
,
5415 ppnum_t
*physpage_p
)
5418 vm_object_offset_t offset
;
5421 thread_t thread
= current_thread();
5424 struct vm_object_fault_info fault_info
= {};
5426 VM_STAT_INCR(faults
);
5428 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
) {
5429 thread
->task
->faults
++;
5437 #define RELEASE_PAGE(m) { \
5438 PAGE_WAKEUP_DONE(m); \
5439 vm_page_lockspin_queues(); \
5440 vm_page_unwire(m, TRUE); \
5441 vm_page_unlock_queues(); \
5445 #undef UNLOCK_THINGS
5446 #define UNLOCK_THINGS { \
5447 vm_object_paging_end(object); \
5448 vm_object_unlock(object); \
5451 #undef UNLOCK_AND_DEALLOCATE
5452 #define UNLOCK_AND_DEALLOCATE { \
5454 vm_object_deallocate(object); \
5457 * Give up and have caller do things the hard way.
5461 UNLOCK_AND_DEALLOCATE; \
5462 return(KERN_FAILURE); \
5467 * If this entry is not directly to a vm_object, bail out.
5469 if (entry
->is_sub_map
) {
5470 assert(physpage_p
== NULL
);
5471 return KERN_FAILURE
;
5475 * Find the backing store object and offset into it.
5478 object
= VME_OBJECT(entry
);
5479 offset
= (va
- entry
->vme_start
) + VME_OFFSET(entry
);
5480 prot
= entry
->protection
;
5483 * Make a reference to this object to prevent its
5484 * disposal while we are messing with it.
5487 vm_object_lock(object
);
5488 vm_object_reference_locked(object
);
5489 vm_object_paging_begin(object
);
5492 * INVARIANTS (through entire routine):
5494 * 1) At all times, we must either have the object
5495 * lock or a busy page in some object to prevent
5496 * some other thread from trying to bring in
5499 * 2) Once we have a busy page, we must remove it from
5500 * the pageout queues, so that the pageout daemon
5501 * will not grab it away.
5506 * Look for page in top-level object. If it's not there or
5507 * there's something going on, give up.
5509 m
= vm_page_lookup(object
, offset
);
5510 if ((m
== VM_PAGE_NULL
) || (m
->vmp_busy
) ||
5511 (m
->vmp_unusual
&& (m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_absent
))) {
5514 if (m
->vmp_fictitious
&&
5515 VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
5517 * Guard pages are fictitious pages and are never
5518 * entered into a pmap, so let's say it's been wired...
5525 * Wire the page down now. All bail outs beyond this
5526 * point must unwire the page.
5529 vm_page_lockspin_queues();
5530 vm_page_wire(m
, wire_tag
, TRUE
);
5531 vm_page_unlock_queues();
5534 * Mark page busy for other threads.
5536 assert(!m
->vmp_busy
);
5538 assert(!m
->vmp_absent
);
5541 * Give up if the page is being written and there's a copy object
5543 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
5548 fault_info
.user_tag
= VME_ALIAS(entry
);
5549 fault_info
.pmap_options
= 0;
5550 if (entry
->iokit_acct
||
5551 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
5552 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
5556 * Put this page into the physical map.
5558 type_of_fault
= DBG_CACHE_HIT_FAULT
;
5559 kr
= vm_fault_enter(m
,
5565 FALSE
, /* change_wiring */
5570 if (kr
!= KERN_SUCCESS
) {
5577 * Unlock everything, and return
5581 /* for vm_map_wire_and_extract() */
5582 if (kr
== KERN_SUCCESS
) {
5583 assert(object
== VM_PAGE_OBJECT(m
));
5584 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
5585 if (prot
& VM_PROT_WRITE
) {
5586 vm_object_lock_assert_exclusive(object
);
5587 m
->vmp_dirty
= TRUE
;
5594 PAGE_WAKEUP_DONE(m
);
5595 UNLOCK_AND_DEALLOCATE
;
5601 * Routine: vm_fault_copy_cleanup
5603 * Release a page used by vm_fault_copy.
5607 vm_fault_copy_cleanup(
5611 vm_object_t object
= VM_PAGE_OBJECT(page
);
5613 vm_object_lock(object
);
5614 PAGE_WAKEUP_DONE(page
);
5615 if (!VM_PAGE_PAGEABLE(page
)) {
5616 vm_page_lockspin_queues();
5617 if (!VM_PAGE_PAGEABLE(page
)) {
5618 vm_page_activate(page
);
5620 vm_page_unlock_queues();
5622 vm_fault_cleanup(object
, top_page
);
5626 vm_fault_copy_dst_cleanup(
5631 if (page
!= VM_PAGE_NULL
) {
5632 object
= VM_PAGE_OBJECT(page
);
5633 vm_object_lock(object
);
5634 vm_page_lockspin_queues();
5635 vm_page_unwire(page
, TRUE
);
5636 vm_page_unlock_queues();
5637 vm_object_paging_end(object
);
5638 vm_object_unlock(object
);
5643 * Routine: vm_fault_copy
5646 * Copy pages from one virtual memory object to another --
5647 * neither the source nor destination pages need be resident.
5649 * Before actually copying a page, the version associated with
5650 * the destination address map wil be verified.
5652 * In/out conditions:
5653 * The caller must hold a reference, but not a lock, to
5654 * each of the source and destination objects and to the
5658 * Returns KERN_SUCCESS if no errors were encountered in
5659 * reading or writing the data. Returns KERN_INTERRUPTED if
5660 * the operation was interrupted (only possible if the
5661 * "interruptible" argument is asserted). Other return values
5662 * indicate a permanent error in copying the data.
5664 * The actual amount of data copied will be returned in the
5665 * "copy_size" argument. In the event that the destination map
5666 * verification failed, this amount may be less than the amount
5671 vm_object_t src_object
,
5672 vm_object_offset_t src_offset
,
5673 vm_map_size_t
*copy_size
, /* INOUT */
5674 vm_object_t dst_object
,
5675 vm_object_offset_t dst_offset
,
5677 vm_map_version_t
*dst_version
,
5680 vm_page_t result_page
;
5683 vm_page_t src_top_page
;
5687 vm_page_t dst_top_page
;
5690 vm_map_size_t amount_left
;
5691 vm_object_t old_copy_object
;
5692 vm_object_t result_page_object
= NULL
;
5693 kern_return_t error
= 0;
5694 vm_fault_return_t result
;
5696 vm_map_size_t part_size
;
5697 struct vm_object_fault_info fault_info_src
= {};
5698 struct vm_object_fault_info fault_info_dst
= {};
5701 * In order not to confuse the clustered pageins, align
5702 * the different offsets on a page boundary.
5707 *copy_size -= amount_left; \
5711 amount_left
= *copy_size
;
5713 fault_info_src
.interruptible
= interruptible
;
5714 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5715 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
5716 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
5717 fault_info_src
.stealth
= TRUE
;
5719 fault_info_dst
.interruptible
= interruptible
;
5720 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5721 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
5722 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
5723 fault_info_dst
.stealth
= TRUE
;
5725 do { /* while (amount_left > 0) */
5727 * There may be a deadlock if both source and destination
5728 * pages are the same. To avoid this deadlock, the copy must
5729 * start by getting the destination page in order to apply
5730 * COW semantics if any.
5733 RetryDestinationFault
:;
5735 dst_prot
= VM_PROT_WRITE
| VM_PROT_READ
;
5737 vm_object_lock(dst_object
);
5738 vm_object_paging_begin(dst_object
);
5740 /* cap cluster size at maximum UPL size */
5741 upl_size_t cluster_size
;
5742 if (os_convert_overflow(amount_left
, &cluster_size
)) {
5743 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
5745 fault_info_dst
.cluster_size
= cluster_size
;
5747 XPR(XPR_VM_FAULT
, "vm_fault_copy -> vm_fault_page\n", 0, 0, 0, 0, 0);
5748 dst_page
= VM_PAGE_NULL
;
5749 result
= vm_fault_page(dst_object
,
5750 vm_object_trunc_page(dst_offset
),
5751 VM_PROT_WRITE
| VM_PROT_READ
,
5753 FALSE
, /* page not looked up */
5754 &dst_prot
, &dst_page
, &dst_top_page
,
5757 dst_map
->no_zero_fill
,
5758 FALSE
, &fault_info_dst
);
5760 case VM_FAULT_SUCCESS
:
5762 case VM_FAULT_RETRY
:
5763 goto RetryDestinationFault
;
5764 case VM_FAULT_MEMORY_SHORTAGE
:
5765 if (vm_page_wait(interruptible
)) {
5766 goto RetryDestinationFault
;
5769 case VM_FAULT_INTERRUPTED
:
5770 RETURN(MACH_SEND_INTERRUPTED
);
5771 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5772 /* success but no VM page: fail the copy */
5773 vm_object_paging_end(dst_object
);
5774 vm_object_unlock(dst_object
);
5776 case VM_FAULT_MEMORY_ERROR
:
5780 return KERN_MEMORY_ERROR
;
5783 panic("vm_fault_copy: unexpected error 0x%x from "
5784 "vm_fault_page()\n", result
);
5786 assert((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
5788 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
5789 old_copy_object
= dst_object
->copy
;
5792 * There exists the possiblity that the source and
5793 * destination page are the same. But we can't
5794 * easily determine that now. If they are the
5795 * same, the call to vm_fault_page() for the
5796 * destination page will deadlock. To prevent this we
5797 * wire the page so we can drop busy without having
5798 * the page daemon steal the page. We clean up the
5799 * top page but keep the paging reference on the object
5800 * holding the dest page so it doesn't go away.
5803 vm_page_lockspin_queues();
5804 vm_page_wire(dst_page
, VM_KERN_MEMORY_OSFMK
, TRUE
);
5805 vm_page_unlock_queues();
5806 PAGE_WAKEUP_DONE(dst_page
);
5807 vm_object_unlock(dst_object
);
5809 if (dst_top_page
!= VM_PAGE_NULL
) {
5810 vm_object_lock(dst_object
);
5811 VM_PAGE_FREE(dst_top_page
);
5812 vm_object_paging_end(dst_object
);
5813 vm_object_unlock(dst_object
);
5818 if (src_object
== VM_OBJECT_NULL
) {
5820 * No source object. We will just
5821 * zero-fill the page in dst_object.
5823 src_page
= VM_PAGE_NULL
;
5824 result_page
= VM_PAGE_NULL
;
5826 vm_object_lock(src_object
);
5827 src_page
= vm_page_lookup(src_object
,
5828 vm_object_trunc_page(src_offset
));
5829 if (src_page
== dst_page
) {
5830 src_prot
= dst_prot
;
5831 result_page
= VM_PAGE_NULL
;
5833 src_prot
= VM_PROT_READ
;
5834 vm_object_paging_begin(src_object
);
5836 /* cap cluster size at maximum UPL size */
5837 if (os_convert_overflow(amount_left
, &cluster_size
)) {
5838 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
5840 fault_info_src
.cluster_size
= cluster_size
;
5843 "vm_fault_copy(2) -> vm_fault_page\n",
5845 result_page
= VM_PAGE_NULL
;
5846 result
= vm_fault_page(
5848 vm_object_trunc_page(src_offset
),
5849 VM_PROT_READ
, FALSE
,
5850 FALSE
, /* page not looked up */
5852 &result_page
, &src_top_page
,
5853 (int *)0, &error
, FALSE
,
5854 FALSE
, &fault_info_src
);
5857 case VM_FAULT_SUCCESS
:
5859 case VM_FAULT_RETRY
:
5860 goto RetrySourceFault
;
5861 case VM_FAULT_MEMORY_SHORTAGE
:
5862 if (vm_page_wait(interruptible
)) {
5863 goto RetrySourceFault
;
5866 case VM_FAULT_INTERRUPTED
:
5867 vm_fault_copy_dst_cleanup(dst_page
);
5868 RETURN(MACH_SEND_INTERRUPTED
);
5869 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5870 /* success but no VM page: fail */
5871 vm_object_paging_end(src_object
);
5872 vm_object_unlock(src_object
);
5874 case VM_FAULT_MEMORY_ERROR
:
5875 vm_fault_copy_dst_cleanup(dst_page
);
5879 return KERN_MEMORY_ERROR
;
5882 panic("vm_fault_copy(2): unexpected "
5884 "vm_fault_page()\n", result
);
5887 result_page_object
= VM_PAGE_OBJECT(result_page
);
5888 assert((src_top_page
== VM_PAGE_NULL
) ==
5889 (result_page_object
== src_object
));
5891 assert((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
5892 vm_object_unlock(result_page_object
);
5895 vm_map_lock_read(dst_map
);
5897 if (!vm_map_verify(dst_map
, dst_version
)) {
5898 vm_map_unlock_read(dst_map
);
5899 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
5900 vm_fault_copy_cleanup(result_page
, src_top_page
);
5902 vm_fault_copy_dst_cleanup(dst_page
);
5905 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
5907 vm_object_lock(dst_object
);
5909 if (dst_object
->copy
!= old_copy_object
) {
5910 vm_object_unlock(dst_object
);
5911 vm_map_unlock_read(dst_map
);
5912 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
5913 vm_fault_copy_cleanup(result_page
, src_top_page
);
5915 vm_fault_copy_dst_cleanup(dst_page
);
5918 vm_object_unlock(dst_object
);
5921 * Copy the page, and note that it is dirty
5925 if (!page_aligned(src_offset
) ||
5926 !page_aligned(dst_offset
) ||
5927 !page_aligned(amount_left
)) {
5928 vm_object_offset_t src_po
,
5931 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
5932 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
5934 if (dst_po
> src_po
) {
5935 part_size
= PAGE_SIZE
- dst_po
;
5937 part_size
= PAGE_SIZE
- src_po
;
5939 if (part_size
> (amount_left
)) {
5940 part_size
= amount_left
;
5943 if (result_page
== VM_PAGE_NULL
) {
5944 assert((vm_offset_t
) dst_po
== dst_po
);
5945 assert((vm_size_t
) part_size
== part_size
);
5946 vm_page_part_zero_fill(dst_page
,
5947 (vm_offset_t
) dst_po
,
5948 (vm_size_t
) part_size
);
5950 assert((vm_offset_t
) src_po
== src_po
);
5951 assert((vm_offset_t
) dst_po
== dst_po
);
5952 assert((vm_size_t
) part_size
== part_size
);
5953 vm_page_part_copy(result_page
,
5954 (vm_offset_t
) src_po
,
5956 (vm_offset_t
) dst_po
,
5957 (vm_size_t
)part_size
);
5958 if (!dst_page
->vmp_dirty
) {
5959 vm_object_lock(dst_object
);
5960 SET_PAGE_DIRTY(dst_page
, TRUE
);
5961 vm_object_unlock(dst_object
);
5965 part_size
= PAGE_SIZE
;
5967 if (result_page
== VM_PAGE_NULL
) {
5968 vm_page_zero_fill(dst_page
);
5970 vm_object_lock(result_page_object
);
5971 vm_page_copy(result_page
, dst_page
);
5972 vm_object_unlock(result_page_object
);
5974 if (!dst_page
->vmp_dirty
) {
5975 vm_object_lock(dst_object
);
5976 SET_PAGE_DIRTY(dst_page
, TRUE
);
5977 vm_object_unlock(dst_object
);
5983 * Unlock everything, and return
5986 vm_map_unlock_read(dst_map
);
5988 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
5989 vm_fault_copy_cleanup(result_page
, src_top_page
);
5991 vm_fault_copy_dst_cleanup(dst_page
);
5993 amount_left
-= part_size
;
5994 src_offset
+= part_size
;
5995 dst_offset
+= part_size
;
5996 } while (amount_left
> 0);
5998 RETURN(KERN_SUCCESS
);
6004 #if VM_FAULT_CLASSIFY
6006 * Temporary statistics gathering support.
6010 * Statistics arrays:
6012 #define VM_FAULT_TYPES_MAX 5
6013 #define VM_FAULT_LEVEL_MAX 8
6015 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
6017 #define VM_FAULT_TYPE_ZERO_FILL 0
6018 #define VM_FAULT_TYPE_MAP_IN 1
6019 #define VM_FAULT_TYPE_PAGER 2
6020 #define VM_FAULT_TYPE_COPY 3
6021 #define VM_FAULT_TYPE_OTHER 4
6025 vm_fault_classify(vm_object_t object
,
6026 vm_object_offset_t offset
,
6027 vm_prot_t fault_type
)
6029 int type
, level
= 0;
6033 m
= vm_page_lookup(object
, offset
);
6034 if (m
!= VM_PAGE_NULL
) {
6035 if (m
->vmp_busy
|| m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_absent
) {
6036 type
= VM_FAULT_TYPE_OTHER
;
6039 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
6040 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
6041 type
= VM_FAULT_TYPE_MAP_IN
;
6044 type
= VM_FAULT_TYPE_COPY
;
6047 if (object
->pager_created
) {
6048 type
= VM_FAULT_TYPE_PAGER
;
6051 if (object
->shadow
== VM_OBJECT_NULL
) {
6052 type
= VM_FAULT_TYPE_ZERO_FILL
;
6056 offset
+= object
->vo_shadow_offset
;
6057 object
= object
->shadow
;
6063 if (level
> VM_FAULT_LEVEL_MAX
) {
6064 level
= VM_FAULT_LEVEL_MAX
;
6067 vm_fault_stats
[type
][level
] += 1;
6072 /* cleanup routine to call from debugger */
6075 vm_fault_classify_init(void)
6079 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
6080 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
6081 vm_fault_stats
[type
][level
] = 0;
6087 #endif /* VM_FAULT_CLASSIFY */
6090 kdp_lightweight_fault(vm_map_t map
, vm_offset_t cur_target_addr
)
6092 vm_map_entry_t entry
;
6094 vm_offset_t object_offset
;
6096 int compressor_external_state
, compressed_count_delta
;
6097 int compressor_flags
= (C_DONT_BLOCK
| C_KEEP
| C_KDP
);
6098 int my_fault_type
= VM_PROT_READ
;
6102 panic("kdp_lightweight_fault called from outside of debugger context");
6105 assert(map
!= VM_MAP_NULL
);
6107 assert((cur_target_addr
& PAGE_MASK
) == 0);
6108 if ((cur_target_addr
& PAGE_MASK
) != 0) {
6112 if (kdp_lck_rw_lock_is_acquired_exclusive(&map
->lock
)) {
6116 if (!vm_map_lookup_entry(map
, cur_target_addr
, &entry
)) {
6120 if (entry
->is_sub_map
) {
6124 object
= VME_OBJECT(entry
);
6125 if (object
== VM_OBJECT_NULL
) {
6129 object_offset
= cur_target_addr
- entry
->vme_start
+ VME_OFFSET(entry
);
6132 if (kdp_lck_rw_lock_is_acquired_exclusive(&object
->Lock
)) {
6136 if (object
->pager_created
&& (object
->paging_in_progress
||
6137 object
->activity_in_progress
)) {
6141 m
= kdp_vm_page_lookup(object
, object_offset
);
6143 if (m
!= VM_PAGE_NULL
) {
6144 if ((object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_DEFAULT
) {
6148 if (m
->vmp_laundry
|| m
->vmp_busy
|| m
->vmp_free_when_done
|| m
->vmp_absent
|| m
->vmp_error
|| m
->vmp_cleaning
||
6149 m
->vmp_overwriting
|| m
->vmp_restart
|| m
->vmp_unusual
) {
6153 assert(!m
->vmp_private
);
6154 if (m
->vmp_private
) {
6158 assert(!m
->vmp_fictitious
);
6159 if (m
->vmp_fictitious
) {
6163 assert(m
->vmp_q_state
!= VM_PAGE_USED_BY_COMPRESSOR
);
6164 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
6168 return ptoa(VM_PAGE_GET_PHYS_PAGE(m
));
6171 compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
6173 if (object
->pager_created
&& MUST_ASK_PAGER(object
, object_offset
, compressor_external_state
)) {
6174 if (compressor_external_state
== VM_EXTERNAL_STATE_EXISTS
) {
6175 kr
= vm_compressor_pager_get(object
->pager
, (object_offset
+ object
->paging_offset
),
6176 kdp_compressor_decompressed_page_ppnum
, &my_fault_type
,
6177 compressor_flags
, &compressed_count_delta
);
6178 if (kr
== KERN_SUCCESS
) {
6179 return kdp_compressor_decompressed_page_paddr
;
6186 if (object
->shadow
== VM_OBJECT_NULL
) {
6190 object_offset
+= object
->vo_shadow_offset
;
6191 object
= object
->shadow
;
6196 * vm_page_validate_cs_fast():
6197 * Performs a few quick checks to determine if the page's code signature
6198 * really needs to be fully validated. It could:
6199 * 1. have been modified (i.e. automatically tainted),
6200 * 2. have already been validated,
6201 * 3. have already been found to be tainted,
6202 * 4. no longer have a backing store.
6203 * Returns FALSE if the page needs to be fully validated.
6206 vm_page_validate_cs_fast(
6211 object
= VM_PAGE_OBJECT(page
);
6212 vm_object_lock_assert_held(object
);
6214 if (page
->vmp_wpmapped
&& !page
->vmp_cs_tainted
) {
6216 * This page was mapped for "write" access sometime in the
6217 * past and could still be modifiable in the future.
6218 * Consider it tainted.
6219 * [ If the page was already found to be "tainted", no
6220 * need to re-validate. ]
6222 vm_object_lock_assert_exclusive(object
);
6223 page
->vmp_cs_validated
= TRUE
;
6224 page
->vmp_cs_tainted
= TRUE
;
6226 printf("CODESIGNING: %s: "
6227 "page %p obj %p off 0x%llx "
6230 page
, object
, page
->vmp_offset
);
6232 vm_cs_validated_dirtied
++;
6235 if (page
->vmp_cs_validated
|| page
->vmp_cs_tainted
) {
6238 vm_object_lock_assert_exclusive(object
);
6240 #if CHECK_CS_VALIDATION_BITMAP
6243 kr
= vnode_pager_cs_check_validation_bitmap(
6245 page
->vmp_offset
+ object
->paging_offset
,
6247 if (kr
== KERN_SUCCESS
) {
6248 page
->vmp_cs_validated
= TRUE
;
6249 page
->vmp_cs_tainted
= FALSE
;
6250 vm_cs_bitmap_validated
++;
6253 #endif /* CHECK_CS_VALIDATION_BITMAP */
6255 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
6257 * The object is terminating and we don't have its pager
6258 * so we can't validate the data...
6263 /* we need to really validate this page */
6264 vm_object_lock_assert_exclusive(object
);
6269 vm_page_validate_cs_mapped_slow(
6274 memory_object_offset_t mo_offset
;
6275 memory_object_t pager
;
6276 struct vnode
*vnode
;
6277 boolean_t validated
;
6280 assert(page
->vmp_busy
);
6281 object
= VM_PAGE_OBJECT(page
);
6282 vm_object_lock_assert_exclusive(object
);
6287 * Since we get here to validate a page that was brought in by
6288 * the pager, we know that this pager is all setup and ready
6291 assert(object
->code_signed
);
6292 assert(!object
->internal
);
6293 assert(object
->pager
!= NULL
);
6294 assert(object
->pager_ready
);
6296 pager
= object
->pager
;
6297 assert(object
->paging_in_progress
);
6298 vnode
= vnode_pager_lookup_vnode(pager
);
6299 mo_offset
= page
->vmp_offset
+ object
->paging_offset
;
6301 /* verify the SHA1 hash for this page */
6303 validated
= cs_validate_range(vnode
,
6306 (const void *)((const char *)kaddr
),
6310 if (tainted
& CS_VALIDATE_TAINTED
) {
6311 page
->vmp_cs_tainted
= TRUE
;
6313 if (tainted
& CS_VALIDATE_NX
) {
6314 page
->vmp_cs_nx
= TRUE
;
6317 page
->vmp_cs_validated
= TRUE
;
6320 #if CHECK_CS_VALIDATION_BITMAP
6321 if (page
->vmp_cs_validated
&& !page
->vmp_cs_tainted
) {
6322 vnode_pager_cs_check_validation_bitmap(object
->pager
,
6326 #endif /* CHECK_CS_VALIDATION_BITMAP */
6330 vm_page_validate_cs_mapped(
6334 if (!vm_page_validate_cs_fast(page
)) {
6335 vm_page_validate_cs_mapped_slow(page
, kaddr
);
6340 vm_page_validate_cs(
6344 vm_object_offset_t offset
;
6345 vm_map_offset_t koffset
;
6346 vm_map_size_t ksize
;
6349 boolean_t busy_page
;
6350 boolean_t need_unmap
;
6352 object
= VM_PAGE_OBJECT(page
);
6353 vm_object_lock_assert_held(object
);
6355 if (vm_page_validate_cs_fast(page
)) {
6358 vm_object_lock_assert_exclusive(object
);
6360 assert(object
->code_signed
);
6361 offset
= page
->vmp_offset
;
6363 busy_page
= page
->vmp_busy
;
6365 /* keep page busy while we map (and unlock) the VM object */
6366 page
->vmp_busy
= TRUE
;
6370 * Take a paging reference on the VM object
6371 * to protect it from collapse or bypass,
6372 * and keep it from disappearing too.
6374 vm_object_paging_begin(object
);
6376 /* map the page in the kernel address space */
6377 ksize
= PAGE_SIZE_64
;
6380 kr
= vm_paging_map_object(page
,
6384 FALSE
, /* can't unlock object ! */
6388 if (kr
!= KERN_SUCCESS
) {
6389 panic("%s: could not map page: 0x%x\n", __FUNCTION__
, kr
);
6391 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
6393 /* validate the mapped page */
6394 vm_page_validate_cs_mapped_slow(page
, (const void *) kaddr
);
6396 assert(page
->vmp_busy
);
6397 assert(object
== VM_PAGE_OBJECT(page
));
6398 vm_object_lock_assert_exclusive(object
);
6401 PAGE_WAKEUP_DONE(page
);
6404 /* unmap the map from the kernel address space */
6405 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
6410 vm_object_paging_end(object
);
6414 vm_page_validate_cs_mapped_chunk(
6417 vm_offset_t chunk_offset
,
6418 vm_size_t chunk_size
,
6419 boolean_t
*validated_p
,
6420 unsigned *tainted_p
)
6423 vm_object_offset_t offset
, offset_in_page
;
6424 memory_object_t pager
;
6425 struct vnode
*vnode
;
6426 boolean_t validated
;
6429 *validated_p
= FALSE
;
6432 assert(page
->vmp_busy
);
6433 object
= VM_PAGE_OBJECT(page
);
6434 vm_object_lock_assert_exclusive(object
);
6436 assert(object
->code_signed
);
6437 offset
= page
->vmp_offset
;
6439 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
6441 * The object is terminating and we don't have its pager
6442 * so we can't validate the data...
6447 * Since we get here to validate a page that was brought in by
6448 * the pager, we know that this pager is all setup and ready
6451 assert(!object
->internal
);
6452 assert(object
->pager
!= NULL
);
6453 assert(object
->pager_ready
);
6455 pager
= object
->pager
;
6456 assert(object
->paging_in_progress
);
6457 vnode
= vnode_pager_lookup_vnode(pager
);
6459 /* verify the signature for this chunk */
6460 offset_in_page
= chunk_offset
;
6461 assert(offset_in_page
< PAGE_SIZE
);
6464 validated
= cs_validate_range(vnode
,
6466 (object
->paging_offset
+
6469 (const void *)((const char *)kaddr
6474 *validated_p
= TRUE
;
6477 *tainted_p
= tainted
;
6482 vm_rtfrecord_lock(void)
6484 lck_spin_lock(&vm_rtfr_slock
);
6488 vm_rtfrecord_unlock(void)
6490 lck_spin_unlock(&vm_rtfr_slock
);
6494 vmrtfaultinfo_bufsz(void)
6496 return vmrtf_num_records
* sizeof(vm_rtfault_record_t
);
6499 #include <kern/backtrace.h>
6502 vm_record_rtfault(thread_t cthread
, uint64_t fstart
, vm_map_offset_t fault_vaddr
, int type_of_fault
)
6504 uint64_t fend
= mach_continuous_time();
6507 uint64_t ctid
= cthread
->thread_id
;
6508 uint64_t cupid
= get_current_unique_pid();
6514 /* Capture a single-frame backtrace; this extracts just the program
6515 * counter at the point of the fault into "bpc", and should perform no
6516 * further user stack traversals, thus avoiding copyin()s and further
6519 int btr
= backtrace_thread_user(cthread
, &bpc
, 1U, &bfrs
, &u64
);
6521 if ((btr
== 0) && (bfrs
> 0)) {
6525 assert((fstart
!= 0) && fend
>= fstart
);
6526 vm_rtfrecord_lock();
6527 assert(vmrtfrs
.vmrtfr_curi
<= vmrtfrs
.vmrtfr_maxi
);
6529 vmrtfrs
.vmrtf_total
++;
6530 vm_rtfault_record_t
*cvmr
= &vmrtfrs
.vm_rtf_records
[vmrtfrs
.vmrtfr_curi
++];
6532 cvmr
->rtfabstime
= fstart
;
6533 cvmr
->rtfduration
= fend
- fstart
;
6534 cvmr
->rtfaddr
= fault_vaddr
;
6536 cvmr
->rtftype
= type_of_fault
;
6537 cvmr
->rtfupid
= cupid
;
6538 cvmr
->rtftid
= ctid
;
6540 if (vmrtfrs
.vmrtfr_curi
> vmrtfrs
.vmrtfr_maxi
) {
6541 vmrtfrs
.vmrtfr_curi
= 0;
6544 vm_rtfrecord_unlock();
6548 vmrtf_extract(uint64_t cupid
, __unused boolean_t isroot
, int vrecordsz
, void *vrecords
, int *vmrtfrv
)
6550 vm_rtfault_record_t
*cvmrd
= vrecords
;
6551 size_t residue
= vrecordsz
;
6552 int numextracted
= 0;
6553 boolean_t early_exit
= FALSE
;
6555 vm_rtfrecord_lock();
6557 for (int vmfi
= 0; vmfi
<= vmrtfrs
.vmrtfr_maxi
; vmfi
++) {
6558 if (residue
< sizeof(vm_rtfault_record_t
)) {
6563 if (vmrtfrs
.vm_rtf_records
[vmfi
].rtfupid
!= cupid
) {
6564 #if DEVELOPMENT || DEBUG
6565 if (isroot
== FALSE
) {
6570 #endif /* DEVDEBUG */
6573 *cvmrd
= vmrtfrs
.vm_rtf_records
[vmfi
];
6575 residue
-= sizeof(vm_rtfault_record_t
);
6579 vm_rtfrecord_unlock();
6581 *vmrtfrv
= numextracted
;