2 * Copyright (c) 2000-2020 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>
85 #include <kern/mach_param.h>
86 #include <kern/macro_help.h>
87 #include <kern/zalloc.h>
88 #include <kern/misc_protos.h>
89 #include <kern/policy_internal.h>
91 #include <vm/vm_compressor.h>
92 #include <vm/vm_compressor_pager.h>
93 #include <vm/vm_fault.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_kern.h>
99 #include <vm/vm_pageout.h>
100 #include <vm/vm_protos.h>
101 #include <vm/vm_external.h>
102 #include <vm/memory_object.h>
103 #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
104 #include <vm/vm_shared_region.h>
106 #include <sys/codesign.h>
107 #include <sys/reason.h>
108 #include <sys/signalvar.h>
110 #include <san/kasan.h>
112 #define VM_FAULT_CLASSIFY 0
114 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
116 int vm_protect_privileged_from_untrusted
= 1;
118 unsigned int vm_object_pagein_throttle
= 16;
121 * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
122 * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts
123 * of memory if they're buggy and can run the system completely out of swap space. If this happens, we
124 * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps
125 * keep the UI active so that the user has a chance to kill the offending task before the system
128 * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
129 * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold
130 * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a
131 * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
134 extern void throttle_lowpri_io(int);
136 extern struct vnode
*vnode_pager_lookup_vnode(memory_object_t
);
138 uint64_t vm_hard_throttle_threshold
;
143 NEED_TO_HARD_THROTTLE_THIS_TASK(void)
145 return vm_wants_task_throttled(current_task()) ||
146 ((vm_page_free_count
< vm_page_throttle_limit
||
147 HARD_THROTTLE_LIMIT_REACHED()) &&
148 proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
) >= THROTTLE_LEVEL_THROTTLED
);
151 #define HARD_THROTTLE_DELAY 10000 /* 10000 us == 10 ms */
152 #define SOFT_THROTTLE_DELAY 200 /* 200 us == .2 ms */
154 #define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS 6
155 #define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC 20000
158 #define VM_STAT_DECOMPRESSIONS() \
160 VM_STAT_INCR(decompressions); \
161 current_thread()->decompressions++; \
164 boolean_t
current_thread_aborted(void);
166 /* Forward declarations of internal routines. */
167 static kern_return_t
vm_fault_wire_fast(
172 vm_map_entry_t entry
,
174 vm_map_offset_t pmap_addr
,
175 ppnum_t
*physpage_p
);
177 static kern_return_t
vm_fault_internal(
179 vm_map_offset_t vaddr
,
180 vm_prot_t caller_prot
,
181 boolean_t change_wiring
,
185 vm_map_offset_t pmap_addr
,
186 ppnum_t
*physpage_p
);
188 static void vm_fault_copy_cleanup(
192 static void vm_fault_copy_dst_cleanup(
195 #if VM_FAULT_CLASSIFY
196 extern void vm_fault_classify(vm_object_t object
,
197 vm_object_offset_t offset
,
198 vm_prot_t fault_type
);
200 extern void vm_fault_classify_init(void);
203 unsigned long vm_pmap_enter_blocked
= 0;
204 unsigned long vm_pmap_enter_retried
= 0;
206 unsigned long vm_cs_validates
= 0;
207 unsigned long vm_cs_revalidates
= 0;
208 unsigned long vm_cs_query_modified
= 0;
209 unsigned long vm_cs_validated_dirtied
= 0;
210 unsigned long vm_cs_bitmap_validated
= 0;
212 void vm_pre_fault(vm_map_offset_t
, vm_prot_t
);
214 extern char *kdp_compressor_decompressed_page
;
215 extern addr64_t kdp_compressor_decompressed_page_paddr
;
216 extern ppnum_t kdp_compressor_decompressed_page_ppnum
;
222 vm_rtfault_record_t
*vm_rtf_records
;
224 #define VMRTF_DEFAULT_BUFSIZE (4096)
225 #define VMRTF_NUM_RECORDS_DEFAULT (VMRTF_DEFAULT_BUFSIZE / sizeof(vm_rtfault_record_t))
226 TUNABLE(int, vmrtf_num_records
, "vm_rtfault_records", VMRTF_NUM_RECORDS_DEFAULT
);
228 static void vm_rtfrecord_lock(void);
229 static void vm_rtfrecord_unlock(void);
230 static void vm_record_rtfault(thread_t
, uint64_t, vm_map_offset_t
, int);
232 extern lck_grp_t vm_page_lck_grp_bucket
;
233 extern lck_attr_t vm_page_lck_attr
;
234 LCK_SPIN_DECLARE_ATTR(vm_rtfr_slock
, &vm_page_lck_grp_bucket
, &vm_page_lck_attr
);
237 * Routine: vm_fault_init
239 * Initialize our private data structures.
245 int i
, vm_compressor_temp
;
246 boolean_t need_default_val
= TRUE
;
248 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
249 * computed as a percentage of available memory, and the percentage used is scaled inversely with
250 * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems
251 * and reduce the value down to 10% for very large memory configurations. This helps give us a
252 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
253 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
256 vm_hard_throttle_threshold
= sane_size
* (35 - MIN((int)(sane_size
/ (1024 * 1024 * 1024)), 25)) / 100;
259 * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry.
262 if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp
, sizeof(vm_compressor_temp
))) {
263 for (i
= 0; i
< VM_PAGER_MAX_MODES
; i
++) {
264 if (((vm_compressor_temp
& (1 << i
)) == vm_compressor_temp
)) {
265 need_default_val
= FALSE
;
266 vm_compressor_mode
= vm_compressor_temp
;
270 if (need_default_val
) {
271 printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp
);
274 if (need_default_val
) {
275 /* If no boot arg or incorrect boot arg, try device tree. */
276 PE_get_default("kern.vm_compressor", &vm_compressor_mode
, sizeof(vm_compressor_mode
));
278 printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode
);
280 PE_parse_boot_argn("vm_protect_privileged_from_untrusted",
281 &vm_protect_privileged_from_untrusted
,
282 sizeof(vm_protect_privileged_from_untrusted
));
287 vm_rtfault_record_init(void)
291 vmrtf_num_records
= MAX(vmrtf_num_records
, 1);
292 size
= vmrtf_num_records
* sizeof(vm_rtfault_record_t
);
293 vmrtfrs
.vm_rtf_records
= zalloc_permanent(size
,
294 ZALIGN(vm_rtfault_record_t
));
295 vmrtfrs
.vmrtfr_maxi
= vmrtf_num_records
- 1;
297 STARTUP(ZALLOC
, STARTUP_RANK_MIDDLE
, vm_rtfault_record_init
);
300 * Routine: vm_fault_cleanup
302 * Clean up the result of vm_fault_page.
304 * The paging reference for "object" is released.
305 * "object" is unlocked.
306 * If "top_page" is not null, "top_page" is
307 * freed and the paging reference for the object
308 * containing it is released.
311 * "object" must be locked.
318 vm_object_paging_end(object
);
319 vm_object_unlock(object
);
321 if (top_page
!= VM_PAGE_NULL
) {
322 object
= VM_PAGE_OBJECT(top_page
);
324 vm_object_lock(object
);
325 VM_PAGE_FREE(top_page
);
326 vm_object_paging_end(object
);
327 vm_object_unlock(object
);
331 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
334 boolean_t vm_page_deactivate_behind
= TRUE
;
336 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
338 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
339 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
340 /* we use it to size an array on the stack */
342 int vm_default_behind
= VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW
;
344 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
347 * vm_page_is_sequential
349 * Determine if sequential access is in progress
350 * in accordance with the behavior specified.
351 * Update state to indicate current access pattern.
353 * object must have at least the shared lock held
357 vm_fault_is_sequential(
359 vm_object_offset_t offset
,
360 vm_behavior_t behavior
)
362 vm_object_offset_t last_alloc
;
366 last_alloc
= object
->last_alloc
;
367 sequential
= object
->sequential
;
368 orig_sequential
= sequential
;
370 offset
= vm_object_trunc_page(offset
);
371 if (offset
== last_alloc
&& behavior
!= VM_BEHAVIOR_RANDOM
) {
372 /* re-faulting in the same page: no change in behavior */
377 case VM_BEHAVIOR_RANDOM
:
379 * reset indicator of sequential behavior
384 case VM_BEHAVIOR_SEQUENTIAL
:
385 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
387 * advance indicator of sequential behavior
389 if (sequential
< MAX_SEQUENTIAL_RUN
) {
390 sequential
+= PAGE_SIZE
;
394 * reset indicator of sequential behavior
400 case VM_BEHAVIOR_RSEQNTL
:
401 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
403 * advance indicator of sequential behavior
405 if (sequential
> -MAX_SEQUENTIAL_RUN
) {
406 sequential
-= PAGE_SIZE
;
410 * reset indicator of sequential behavior
416 case VM_BEHAVIOR_DEFAULT
:
418 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
420 * advance indicator of sequential behavior
422 if (sequential
< 0) {
425 if (sequential
< MAX_SEQUENTIAL_RUN
) {
426 sequential
+= PAGE_SIZE
;
428 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
430 * advance indicator of sequential behavior
432 if (sequential
> 0) {
435 if (sequential
> -MAX_SEQUENTIAL_RUN
) {
436 sequential
-= PAGE_SIZE
;
440 * reset indicator of sequential behavior
446 if (sequential
!= orig_sequential
) {
447 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
449 * if someone else has already updated object->sequential
450 * don't bother trying to update it or object->last_alloc
456 * I'd like to do this with a OSCompareAndSwap64, but that
457 * doesn't exist for PPC... however, it shouldn't matter
458 * that much... last_alloc is maintained so that we can determine
459 * if a sequential access pattern is taking place... if only
460 * one thread is banging on this object, no problem with the unprotected
461 * update... if 2 or more threads are banging away, we run the risk of
462 * someone seeing a mangled update... however, in the face of multiple
463 * accesses, no sequential access pattern can develop anyway, so we
464 * haven't lost any real info.
466 object
->last_alloc
= offset
;
470 int vm_page_deactivate_behind_count
= 0;
473 * vm_page_deactivate_behind
475 * Determine if sequential access is in progress
476 * in accordance with the behavior specified. If
477 * so, compute a potential page to deactivate and
480 * object must be locked.
482 * return TRUE if we actually deactivate a page
486 vm_fault_deactivate_behind(
488 vm_object_offset_t offset
,
489 vm_behavior_t behavior
)
492 int pages_in_run
= 0;
493 int max_pages_in_run
= 0;
495 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
496 vm_object_offset_t run_offset
= 0;
497 vm_object_offset_t pg_offset
= 0;
499 vm_page_t page_run
[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
];
503 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
505 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
|| (vm_object_trunc_page(offset
) != offset
)) {
507 * Do not deactivate pages from the kernel object: they
508 * are not intended to become pageable.
509 * or we've disabled the deactivate behind mechanism
510 * or we are dealing with an offset that is not aligned to
511 * the system's PAGE_SIZE because in that case we will
512 * handle the deactivation on the aligned offset and, thus,
513 * the full PAGE_SIZE page once. This helps us avoid the redundant
514 * deactivates and the extra faults.
518 if ((sequential_run
= object
->sequential
)) {
519 if (sequential_run
< 0) {
520 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
521 sequential_run
= 0 - sequential_run
;
523 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
527 case VM_BEHAVIOR_RANDOM
:
529 case VM_BEHAVIOR_SEQUENTIAL
:
530 if (sequential_run
>= (int)PAGE_SIZE
) {
531 run_offset
= 0 - PAGE_SIZE_64
;
532 max_pages_in_run
= 1;
535 case VM_BEHAVIOR_RSEQNTL
:
536 if (sequential_run
>= (int)PAGE_SIZE
) {
537 run_offset
= PAGE_SIZE_64
;
538 max_pages_in_run
= 1;
541 case VM_BEHAVIOR_DEFAULT
:
543 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
546 * determine if the run of sequential accesss has been
547 * long enough on an object with default access behavior
548 * to consider it for deactivation
550 if ((uint64_t)sequential_run
>= behind
&& (sequential_run
% (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
* PAGE_SIZE
)) == 0) {
552 * the comparisons between offset and behind are done
553 * in this kind of odd fashion in order to prevent wrap around
556 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
557 if (offset
>= behind
) {
558 run_offset
= 0 - behind
;
559 pg_offset
= PAGE_SIZE_64
;
560 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
563 if (offset
< -behind
) {
565 pg_offset
= 0 - PAGE_SIZE_64
;
566 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
572 for (n
= 0; n
< max_pages_in_run
; n
++) {
573 m
= vm_page_lookup(object
, offset
+ run_offset
+ (n
* pg_offset
));
575 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
) {
576 page_run
[pages_in_run
++] = m
;
579 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
581 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
582 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
583 * new reference happens. If no futher references happen on the page after that remote TLB flushes
584 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
585 * by pageout_scan, which is just fine since the last reference would have happened quite far
586 * in the past (TLB caches don't hang around for very long), and of course could just as easily
587 * have happened before we did the deactivate_behind.
589 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m
), VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
593 vm_page_lockspin_queues();
595 for (n
= 0; n
< pages_in_run
; n
++) {
598 vm_page_deactivate_internal(m
, FALSE
);
600 vm_page_deactivate_behind_count
++;
602 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
605 vm_page_unlock_queues();
613 #if (DEVELOPMENT || DEBUG)
614 uint32_t vm_page_creation_throttled_hard
= 0;
615 uint32_t vm_page_creation_throttled_soft
= 0;
616 uint64_t vm_page_creation_throttle_avoided
= 0;
617 #endif /* DEVELOPMENT || DEBUG */
620 vm_page_throttled(boolean_t page_kept
)
622 clock_sec_t elapsed_sec
;
624 clock_usec_t tv_usec
;
626 thread_t thread
= current_thread();
628 if (thread
->options
& TH_OPT_VMPRIV
) {
632 if (thread
->t_page_creation_throttled
) {
633 thread
->t_page_creation_throttled
= 0;
635 if (page_kept
== FALSE
) {
639 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
640 #if (DEVELOPMENT || DEBUG)
641 thread
->t_page_creation_throttled_hard
++;
642 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
643 #endif /* DEVELOPMENT || DEBUG */
644 return HARD_THROTTLE_DELAY
;
647 if ((vm_page_free_count
< vm_page_throttle_limit
|| (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& SWAPPER_NEEDS_TO_UNTHROTTLE())) &&
648 thread
->t_page_creation_count
> (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
* VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
)) {
649 if (vm_page_free_wanted
== 0 && vm_page_free_wanted_privileged
== 0) {
650 #if (DEVELOPMENT || DEBUG)
651 OSAddAtomic64(1, &vm_page_creation_throttle_avoided
);
655 clock_get_system_microtime(&tv_sec
, &tv_usec
);
657 elapsed_sec
= tv_sec
- thread
->t_page_creation_time
;
659 if (elapsed_sec
<= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
||
660 (thread
->t_page_creation_count
/ elapsed_sec
) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
) {
661 if (elapsed_sec
>= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
)) {
663 * we'll reset our stats to give a well behaved app
664 * that was unlucky enough to accumulate a bunch of pages
665 * over a long period of time a chance to get out of
666 * the throttled state... we reset the counter and timestamp
667 * so that if it stays under the rate limit for the next second
668 * it will be back in our good graces... if it exceeds it, it
669 * will remain in the throttled state
671 thread
->t_page_creation_time
= tv_sec
;
672 thread
->t_page_creation_count
= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
* (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
- 1);
674 VM_PAGEOUT_DEBUG(vm_page_throttle_count
, 1);
676 thread
->t_page_creation_throttled
= 1;
678 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& HARD_THROTTLE_LIMIT_REACHED()) {
679 #if (DEVELOPMENT || DEBUG)
680 thread
->t_page_creation_throttled_hard
++;
681 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
682 #endif /* DEVELOPMENT || DEBUG */
683 return HARD_THROTTLE_DELAY
;
685 #if (DEVELOPMENT || DEBUG)
686 thread
->t_page_creation_throttled_soft
++;
687 OSAddAtomic(1, &vm_page_creation_throttled_soft
);
688 #endif /* DEVELOPMENT || DEBUG */
689 return SOFT_THROTTLE_DELAY
;
692 thread
->t_page_creation_time
= tv_sec
;
693 thread
->t_page_creation_count
= 0;
696 thread
->t_page_creation_count
++;
703 * check for various conditions that would
704 * prevent us from creating a ZF page...
705 * cleanup is based on being called from vm_fault_page
707 * object must be locked
708 * object == m->vmp_object
710 static vm_fault_return_t
711 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
)
715 if (object
->shadow_severed
||
716 VM_OBJECT_PURGEABLE_FAULT_ERROR(object
)) {
719 * 1. the shadow chain was severed,
720 * 2. the purgeable object is volatile or empty and is marked
721 * to fault on access while volatile.
722 * Just have to return an error at this point
724 if (m
!= VM_PAGE_NULL
) {
727 vm_fault_cleanup(object
, first_m
);
729 thread_interrupt_level(interruptible_state
);
731 return VM_FAULT_MEMORY_ERROR
;
733 if (page_throttle
== TRUE
) {
734 if ((throttle_delay
= vm_page_throttled(FALSE
))) {
736 * we're throttling zero-fills...
737 * treat this as if we couldn't grab a page
739 if (m
!= VM_PAGE_NULL
) {
742 vm_fault_cleanup(object
, first_m
);
744 VM_DEBUG_EVENT(vmf_check_zfdelay
, VMF_CHECK_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
746 delay(throttle_delay
);
748 if (current_thread_aborted()) {
749 thread_interrupt_level(interruptible_state
);
750 return VM_FAULT_INTERRUPTED
;
752 thread_interrupt_level(interruptible_state
);
754 return VM_FAULT_MEMORY_SHORTAGE
;
757 return VM_FAULT_SUCCESS
;
761 * Clear the code signing bits on the given page_t
764 vm_fault_cs_clear(vm_page_t m
)
766 m
->vmp_cs_validated
= VMP_CS_ALL_FALSE
;
767 m
->vmp_cs_tainted
= VMP_CS_ALL_FALSE
;
768 m
->vmp_cs_nx
= VMP_CS_ALL_FALSE
;
772 * Enqueues the given page on the throttled queue.
773 * The caller must hold the vm_page_queue_lock and it will be held on return.
776 vm_fault_enqueue_throttled_locked(vm_page_t m
)
778 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
779 assert(!VM_PAGE_WIRED(m
));
782 * can't be on the pageout queue since we don't
783 * have a pager to try and clean to
785 vm_page_queues_remove(m
, TRUE
);
786 vm_page_check_pageable_safe(m
);
787 vm_page_queue_enter(&vm_page_queue_throttled
, m
, vmp_pageq
);
788 m
->vmp_q_state
= VM_PAGE_ON_THROTTLED_Q
;
789 vm_page_throttled_count
++;
793 * do the work to zero fill a page and
794 * inject it into the correct paging queue
796 * m->vmp_object must be locked
797 * page queue lock must NOT be held
800 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
802 int my_fault
= DBG_ZERO_FILL_FAULT
;
805 object
= VM_PAGE_OBJECT(m
);
808 * This is is a zero-fill page fault...
810 * Checking the page lock is a waste of
811 * time; this page was absent, so
812 * it can't be page locked by a pager.
814 * we also consider it undefined
815 * with respect to instruction
816 * execution. i.e. it is the responsibility
817 * of higher layers to call for an instruction
818 * sync after changing the contents and before
819 * sending a program into this area. We
820 * choose this approach for performance
822 vm_fault_cs_clear(m
);
823 m
->vmp_pmapped
= TRUE
;
825 if (no_zero_fill
== TRUE
) {
826 my_fault
= DBG_NZF_PAGE_FAULT
;
828 if (m
->vmp_absent
&& m
->vmp_busy
) {
832 vm_page_zero_fill(m
);
834 VM_STAT_INCR(zero_fill_count
);
835 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
837 assert(!m
->vmp_laundry
);
838 assert(object
!= kernel_object
);
839 //assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0);
840 if (!VM_DYNAMIC_PAGING_ENABLED() &&
841 (object
->purgable
== VM_PURGABLE_DENY
||
842 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
843 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
844 vm_page_lockspin_queues();
845 if (!VM_DYNAMIC_PAGING_ENABLED()) {
846 vm_fault_enqueue_throttled_locked(m
);
848 vm_page_unlock_queues();
855 * Routine: vm_fault_page
857 * Find the resident page for the virtual memory
858 * specified by the given virtual memory object
860 * Additional arguments:
861 * The required permissions for the page is given
862 * in "fault_type". Desired permissions are included
864 * fault_info is passed along to determine pagein cluster
865 * limits... it contains the expected reference pattern,
866 * cluster size if available, etc...
868 * If the desired page is known to be resident (for
869 * example, because it was previously wired down), asserting
870 * the "unwiring" parameter will speed the search.
872 * If the operation can be interrupted (by thread_abort
873 * or thread_terminate), then the "interruptible"
874 * parameter should be asserted.
877 * The page containing the proper data is returned
881 * The source object must be locked and referenced,
882 * and must donate one paging reference. The reference
883 * is not affected. The paging reference and lock are
886 * If the call succeeds, the object in which "result_page"
887 * resides is left locked and holding a paging reference.
888 * If this is not the original object, a busy page in the
889 * original object is returned in "top_page", to prevent other
890 * callers from pursuing this same data, along with a paging
891 * reference for the original object. The "top_page" should
892 * be destroyed when this guarantee is no longer required.
893 * The "result_page" is also left busy. It is not removed
894 * from the pageout queues.
896 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
897 * fault succeeded but there's no VM page (i.e. the VM object
898 * does not actually hold VM pages, but device memory or
899 * large pages). The object is still locked and we still hold a
900 * paging_in_progress reference.
902 unsigned int vm_fault_page_blocked_access
= 0;
903 unsigned int vm_fault_page_forced_retry
= 0;
908 vm_object_t first_object
, /* Object to begin search */
909 vm_object_offset_t first_offset
, /* Offset into object */
910 vm_prot_t fault_type
, /* What access is requested */
911 boolean_t must_be_resident
,/* Must page be resident? */
912 boolean_t caller_lookup
, /* caller looked up page */
913 /* Modifies in place: */
914 vm_prot_t
*protection
, /* Protection for mapping */
915 vm_page_t
*result_page
, /* Page found, if successful */
917 vm_page_t
*top_page
, /* Page in top object, if
918 * not result_page. */
919 int *type_of_fault
, /* if non-null, fill in with type of fault
920 * COW, zero-fill, etc... returned in trace point */
921 /* More arguments: */
922 kern_return_t
*error_code
, /* code if page is in error */
923 boolean_t no_zero_fill
, /* don't zero fill absent pages */
924 boolean_t data_supply
, /* treat as data_supply if
925 * it is a write fault and a full
926 * page is provided */
927 vm_object_fault_info_t fault_info
)
931 vm_object_offset_t offset
;
933 vm_object_t next_object
;
934 vm_object_t copy_object
;
935 boolean_t look_for_page
;
936 boolean_t force_fault_retry
= FALSE
;
937 vm_prot_t access_required
= fault_type
;
938 vm_prot_t wants_copy_flag
;
939 kern_return_t wait_result
;
940 wait_interrupt_t interruptible_state
;
941 boolean_t data_already_requested
= FALSE
;
942 vm_behavior_t orig_behavior
;
943 vm_size_t orig_cluster_size
;
944 vm_fault_return_t error
;
946 uint32_t try_failed_count
;
947 int interruptible
; /* how may fault be interrupted? */
948 int external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
949 memory_object_t pager
;
950 vm_fault_return_t retval
;
954 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
955 * marked as paged out in the compressor pager or the pager doesn't exist.
956 * Note also that if the pager for an internal object
957 * has not been created, the pager is not invoked regardless of the value
958 * of MUST_ASK_PAGER().
960 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
961 * is marked as paged out in the compressor pager.
962 * PAGED_OUT() is used to determine if a page has already been pushed
963 * into a copy object in order to avoid a redundant page out operation.
965 #define MUST_ASK_PAGER(o, f, s) \
966 ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
968 #define PAGED_OUT(o, f) \
969 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
974 #define RELEASE_PAGE(m) \
976 PAGE_WAKEUP_DONE(m); \
977 if ( !VM_PAGE_PAGEABLE(m)) { \
978 vm_page_lockspin_queues(); \
979 if ( !VM_PAGE_PAGEABLE(m)) { \
980 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE) \
981 vm_page_deactivate(m); \
983 vm_page_activate(m); \
985 vm_page_unlock_queues(); \
990 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
993 interruptible
= fault_info
->interruptible
;
994 interruptible_state
= thread_interrupt_level(interruptible
);
997 * INVARIANTS (through entire routine):
999 * 1) At all times, we must either have the object
1000 * lock or a busy page in some object to prevent
1001 * some other thread from trying to bring in
1004 * Note that we cannot hold any locks during the
1005 * pager access or when waiting for memory, so
1006 * we use a busy page then.
1008 * 2) To prevent another thread from racing us down the
1009 * shadow chain and entering a new page in the top
1010 * object before we do, we must keep a busy page in
1011 * the top object while following the shadow chain.
1013 * 3) We must increment paging_in_progress on any object
1014 * for which we have a busy page before dropping
1017 * 4) We leave busy pages on the pageout queues.
1018 * If the pageout daemon comes across a busy page,
1019 * it will remove the page from the pageout queues.
1022 object
= first_object
;
1023 offset
= first_offset
;
1024 first_m
= VM_PAGE_NULL
;
1025 access_required
= fault_type
;
1028 * default type of fault
1030 my_fault
= DBG_CACHE_HIT_FAULT
;
1034 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1038 #if CONFIG_SECLUDED_MEMORY
1039 if (object
->can_grab_secluded
) {
1040 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
1042 #endif /* CONFIG_SECLUDED_MEMORY */
1044 if (!object
->alive
) {
1046 * object is no longer valid
1047 * clean up and return error
1049 vm_fault_cleanup(object
, first_m
);
1050 thread_interrupt_level(interruptible_state
);
1052 return VM_FAULT_MEMORY_ERROR
;
1055 if (!object
->pager_created
&& object
->phys_contiguous
) {
1057 * A physically-contiguous object without a pager:
1058 * must be a "large page" object. We do not deal
1059 * with VM pages for this object.
1061 caller_lookup
= FALSE
;
1063 goto phys_contig_object
;
1066 if (object
->blocked_access
) {
1068 * Access to this VM object has been blocked.
1069 * Replace our "paging_in_progress" reference with
1070 * a "activity_in_progress" reference and wait for
1071 * access to be unblocked.
1073 caller_lookup
= FALSE
; /* no longer valid after sleep */
1074 vm_object_activity_begin(object
);
1075 vm_object_paging_end(object
);
1076 while (object
->blocked_access
) {
1077 vm_object_sleep(object
,
1078 VM_OBJECT_EVENT_UNBLOCKED
,
1081 vm_fault_page_blocked_access
++;
1082 vm_object_paging_begin(object
);
1083 vm_object_activity_end(object
);
1087 * See whether the page at 'offset' is resident
1089 if (caller_lookup
== TRUE
) {
1091 * The caller has already looked up the page
1092 * and gave us the result in "result_page".
1093 * We can use this for the first lookup but
1094 * it loses its validity as soon as we unlock
1098 caller_lookup
= FALSE
; /* no longer valid after that */
1100 m
= vm_page_lookup(object
, vm_object_trunc_page(offset
));
1103 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1105 if (m
!= VM_PAGE_NULL
) {
1108 * The page is being brought in,
1109 * wait for it and then retry.
1112 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1114 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
1116 counter(c_vm_fault_page_block_busy_kernel
++);
1118 if (wait_result
!= THREAD_AWAKENED
) {
1119 vm_fault_cleanup(object
, first_m
);
1120 thread_interrupt_level(interruptible_state
);
1122 if (wait_result
== THREAD_RESTART
) {
1123 return VM_FAULT_RETRY
;
1125 return VM_FAULT_INTERRUPTED
;
1130 if (m
->vmp_laundry
) {
1131 m
->vmp_free_when_done
= FALSE
;
1133 if (!m
->vmp_cleaning
) {
1134 vm_pageout_steal_laundry(m
, FALSE
);
1137 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
1139 * Guard page: off limits !
1141 if (fault_type
== VM_PROT_NONE
) {
1143 * The fault is not requesting any
1144 * access to the guard page, so it must
1145 * be just to wire or unwire it.
1146 * Let's pretend it succeeded...
1150 assert(first_m
== VM_PAGE_NULL
);
1151 *top_page
= first_m
;
1152 if (type_of_fault
) {
1153 *type_of_fault
= DBG_GUARD_FAULT
;
1155 thread_interrupt_level(interruptible_state
);
1156 return VM_FAULT_SUCCESS
;
1159 * The fault requests access to the
1160 * guard page: let's deny that !
1162 vm_fault_cleanup(object
, first_m
);
1163 thread_interrupt_level(interruptible_state
);
1164 return VM_FAULT_MEMORY_ERROR
;
1170 * The page is in error, give up now.
1173 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1176 *error_code
= KERN_MEMORY_ERROR
;
1180 vm_fault_cleanup(object
, first_m
);
1181 thread_interrupt_level(interruptible_state
);
1183 return VM_FAULT_MEMORY_ERROR
;
1185 if (m
->vmp_restart
) {
1187 * The pager wants us to restart
1188 * at the top of the chain,
1189 * typically because it has moved the
1190 * page to another pager, then do so.
1193 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1197 vm_fault_cleanup(object
, first_m
);
1198 thread_interrupt_level(interruptible_state
);
1200 return VM_FAULT_RETRY
;
1202 if (m
->vmp_absent
) {
1204 * The page isn't busy, but is absent,
1205 * therefore it's deemed "unavailable".
1207 * Remove the non-existent page (unless it's
1208 * in the top object) and move on down to the
1209 * next object (if there is one).
1212 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1214 next_object
= object
->shadow
;
1216 if (next_object
== VM_OBJECT_NULL
) {
1218 * Absent page at bottom of shadow
1219 * chain; zero fill the page we left
1220 * busy in the first object, and free
1223 assert(!must_be_resident
);
1226 * check for any conditions that prevent
1227 * us from creating a new zero-fill page
1228 * vm_fault_check will do all of the
1229 * fault cleanup in the case of an error condition
1230 * including resetting the thread_interrupt_level
1232 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1234 if (error
!= VM_FAULT_SUCCESS
) {
1238 if (object
!= first_object
) {
1240 * free the absent page we just found
1245 * drop reference and lock on current object
1247 vm_object_paging_end(object
);
1248 vm_object_unlock(object
);
1251 * grab the original page we
1252 * 'soldered' in place and
1253 * retake lock on 'first_object'
1256 first_m
= VM_PAGE_NULL
;
1258 object
= first_object
;
1259 offset
= first_offset
;
1261 vm_object_lock(object
);
1264 * we're going to use the absent page we just found
1265 * so convert it to a 'busy' page
1267 m
->vmp_absent
= FALSE
;
1270 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
) {
1271 m
->vmp_absent
= TRUE
;
1274 * zero-fill the page and put it on
1275 * the correct paging queue
1277 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1281 if (must_be_resident
) {
1282 vm_object_paging_end(object
);
1283 } else if (object
!= first_object
) {
1284 vm_object_paging_end(object
);
1288 m
->vmp_absent
= FALSE
;
1291 vm_page_lockspin_queues();
1292 vm_page_queues_remove(m
, FALSE
);
1293 vm_page_unlock_queues();
1296 offset
+= object
->vo_shadow_offset
;
1297 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1298 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1299 access_required
= VM_PROT_READ
;
1301 vm_object_lock(next_object
);
1302 vm_object_unlock(object
);
1303 object
= next_object
;
1304 vm_object_paging_begin(object
);
1307 * reset to default type of fault
1309 my_fault
= DBG_CACHE_HIT_FAULT
;
1314 if ((m
->vmp_cleaning
)
1315 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1316 && (fault_type
& VM_PROT_WRITE
)) {
1318 * This is a copy-on-write fault that will
1319 * cause us to revoke access to this page, but
1320 * this page is in the process of being cleaned
1321 * in a clustered pageout. We must wait until
1322 * the cleaning operation completes before
1323 * revoking access to the original page,
1324 * otherwise we might attempt to remove a
1328 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1331 * take an extra ref so that object won't die
1333 vm_object_reference_locked(object
);
1335 vm_fault_cleanup(object
, first_m
);
1337 counter(c_vm_fault_page_block_backoff_kernel
++);
1338 vm_object_lock(object
);
1339 assert(object
->ref_count
> 0);
1341 m
= vm_page_lookup(object
, vm_object_trunc_page(offset
));
1343 if (m
!= VM_PAGE_NULL
&& m
->vmp_cleaning
) {
1344 PAGE_ASSERT_WAIT(m
, interruptible
);
1346 vm_object_unlock(object
);
1347 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1348 vm_object_deallocate(object
);
1352 vm_object_unlock(object
);
1354 vm_object_deallocate(object
);
1355 thread_interrupt_level(interruptible_state
);
1357 return VM_FAULT_RETRY
;
1360 if (type_of_fault
== NULL
&& (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) &&
1361 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1363 * If we were passed a non-NULL pointer for
1364 * "type_of_fault", than we came from
1365 * vm_fault... we'll let it deal with
1366 * this condition, since it
1367 * needs to see m->vmp_speculative to correctly
1368 * account the pageins, otherwise...
1369 * take it off the speculative queue, we'll
1370 * let the caller of vm_fault_page deal
1371 * with getting it onto the correct queue
1373 * If the caller specified in fault_info that
1374 * it wants a "stealth" fault, we also leave
1375 * the page in the speculative queue.
1377 vm_page_lockspin_queues();
1378 if (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) {
1379 vm_page_queues_remove(m
, FALSE
);
1381 vm_page_unlock_queues();
1383 assert(object
== VM_PAGE_OBJECT(m
));
1385 if (object
->code_signed
) {
1388 * We just paged in a page from a signed
1389 * memory object but we don't need to
1390 * validate it now. We'll validate it if
1391 * when it gets mapped into a user address
1392 * space for the first time or when the page
1393 * gets copied to another object as a result
1394 * of a copy-on-write.
1399 * We mark the page busy and leave it on
1400 * the pageout queues. If the pageout
1401 * deamon comes across it, then it will
1402 * remove the page from the queue, but not the object
1405 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1407 assert(!m
->vmp_busy
);
1408 assert(!m
->vmp_absent
);
1416 * we get here when there is no page present in the object at
1417 * the offset we're interested in... we'll allocate a page
1418 * at this point if the pager associated with
1419 * this object can provide the data or we're the top object...
1420 * object is locked; m == NULL
1423 if (must_be_resident
) {
1424 if (fault_type
== VM_PROT_NONE
&&
1425 object
== kernel_object
) {
1427 * We've been called from vm_fault_unwire()
1428 * while removing a map entry that was allocated
1429 * with KMA_KOBJECT and KMA_VAONLY. This page
1430 * is not present and there's nothing more to
1431 * do here (nothing to unwire).
1433 vm_fault_cleanup(object
, first_m
);
1434 thread_interrupt_level(interruptible_state
);
1436 return VM_FAULT_MEMORY_ERROR
;
1439 goto dont_look_for_page
;
1442 /* Don't expect to fault pages into the kernel object. */
1443 assert(object
!= kernel_object
);
1445 data_supply
= FALSE
;
1447 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
, external_state
) == TRUE
) && !data_supply
);
1450 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1452 if (!look_for_page
&& object
== first_object
&& !object
->phys_contiguous
) {
1454 * Allocate a new page for this object/offset pair as a placeholder
1456 m
= vm_page_grab_options(grab_options
);
1458 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1460 if (m
== VM_PAGE_NULL
) {
1461 vm_fault_cleanup(object
, first_m
);
1462 thread_interrupt_level(interruptible_state
);
1464 return VM_FAULT_MEMORY_SHORTAGE
;
1467 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1468 vm_page_insert_internal(m
, object
,
1469 vm_object_trunc_page(offset
),
1470 VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1472 vm_page_insert(m
, object
, vm_object_trunc_page(offset
));
1475 if (look_for_page
) {
1480 * If the memory manager is not ready, we
1481 * cannot make requests.
1483 if (!object
->pager_ready
) {
1485 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1487 if (m
!= VM_PAGE_NULL
) {
1492 * take an extra ref so object won't die
1494 vm_object_reference_locked(object
);
1495 vm_fault_cleanup(object
, first_m
);
1496 counter(c_vm_fault_page_block_backoff_kernel
++);
1498 vm_object_lock(object
);
1499 assert(object
->ref_count
> 0);
1501 if (!object
->pager_ready
) {
1502 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1504 vm_object_unlock(object
);
1505 if (wait_result
== THREAD_WAITING
) {
1506 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1508 vm_object_deallocate(object
);
1512 vm_object_unlock(object
);
1513 vm_object_deallocate(object
);
1514 thread_interrupt_level(interruptible_state
);
1516 return VM_FAULT_RETRY
;
1519 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1521 * If there are too many outstanding page
1522 * requests pending on this external object, we
1523 * wait for them to be resolved now.
1526 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1528 if (m
!= VM_PAGE_NULL
) {
1532 * take an extra ref so object won't die
1534 vm_object_reference_locked(object
);
1536 vm_fault_cleanup(object
, first_m
);
1538 counter(c_vm_fault_page_block_backoff_kernel
++);
1540 vm_object_lock(object
);
1541 assert(object
->ref_count
> 0);
1543 if (object
->paging_in_progress
>= vm_object_pagein_throttle
) {
1544 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS
, interruptible
);
1546 vm_object_unlock(object
);
1547 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1548 vm_object_deallocate(object
);
1552 vm_object_unlock(object
);
1553 vm_object_deallocate(object
);
1554 thread_interrupt_level(interruptible_state
);
1556 return VM_FAULT_RETRY
;
1559 if (object
->internal
) {
1560 int compressed_count_delta
;
1562 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
1564 if (m
== VM_PAGE_NULL
) {
1566 * Allocate a new page for this object/offset pair as a placeholder
1568 m
= vm_page_grab_options(grab_options
);
1570 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1572 if (m
== VM_PAGE_NULL
) {
1573 vm_fault_cleanup(object
, first_m
);
1574 thread_interrupt_level(interruptible_state
);
1576 return VM_FAULT_MEMORY_SHORTAGE
;
1579 m
->vmp_absent
= TRUE
;
1580 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1581 vm_page_insert_internal(m
, object
, vm_object_trunc_page(offset
), VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1583 vm_page_insert(m
, object
, vm_object_trunc_page(offset
));
1586 assert(m
->vmp_busy
);
1588 m
->vmp_absent
= TRUE
;
1589 pager
= object
->pager
;
1591 assert(object
->paging_in_progress
> 0);
1592 vm_object_unlock(object
);
1594 rc
= vm_compressor_pager_get(
1596 offset
+ object
->paging_offset
,
1597 VM_PAGE_GET_PHYS_PAGE(m
),
1600 &compressed_count_delta
);
1602 if (type_of_fault
== NULL
) {
1606 * we weren't called from vm_fault, so we
1607 * need to apply page creation throttling
1608 * do it before we re-acquire any locks
1610 if (my_fault_type
== DBG_COMPRESSOR_FAULT
) {
1611 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
1612 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 1, 0);
1613 delay(throttle_delay
);
1617 vm_object_lock(object
);
1618 assert(object
->paging_in_progress
> 0);
1620 vm_compressor_pager_count(
1622 compressed_count_delta
,
1623 FALSE
, /* shared_lock */
1628 m
->vmp_absent
= FALSE
;
1629 m
->vmp_dirty
= TRUE
;
1630 if ((object
->wimg_bits
&
1632 VM_WIMG_USE_DEFAULT
) {
1634 * If the page is not cacheable,
1635 * we can't let its contents
1636 * linger in the data cache
1637 * after the decompression.
1639 pmap_sync_page_attributes_phys(
1640 VM_PAGE_GET_PHYS_PAGE(m
));
1642 m
->vmp_written_by_kernel
= TRUE
;
1646 * If the object is purgeable, its
1647 * owner's purgeable ledgers have been
1648 * updated in vm_page_insert() but the
1649 * page was also accounted for in a
1650 * "compressed purgeable" ledger, so
1653 if (((object
->purgable
!=
1654 VM_PURGABLE_DENY
) ||
1655 object
->vo_ledger_tag
) &&
1656 (object
->vo_owner
!=
1659 * One less compressed
1660 * purgeable/tagged page.
1662 vm_object_owner_compressed_update(
1668 case KERN_MEMORY_FAILURE
:
1669 m
->vmp_unusual
= TRUE
;
1670 m
->vmp_error
= TRUE
;
1671 m
->vmp_absent
= FALSE
;
1673 case KERN_MEMORY_ERROR
:
1674 assert(m
->vmp_absent
);
1677 panic("vm_fault_page(): unexpected "
1679 "vm_compressor_pager_get()\n",
1682 PAGE_WAKEUP_DONE(m
);
1685 goto data_requested
;
1687 my_fault_type
= DBG_PAGEIN_FAULT
;
1689 if (m
!= VM_PAGE_NULL
) {
1695 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1699 * It's possible someone called vm_object_destroy while we weren't
1700 * holding the object lock. If that has happened, then bail out
1704 pager
= object
->pager
;
1706 if (pager
== MEMORY_OBJECT_NULL
) {
1707 vm_fault_cleanup(object
, first_m
);
1708 thread_interrupt_level(interruptible_state
);
1709 return VM_FAULT_MEMORY_ERROR
;
1713 * We have an absent page in place for the faulting offset,
1714 * so we can release the object lock.
1717 if (object
->object_is_shared_cache
) {
1718 set_thread_rwlock_boost();
1721 vm_object_unlock(object
);
1724 * If this object uses a copy_call strategy,
1725 * and we are interested in a copy of this object
1726 * (having gotten here only by following a
1727 * shadow chain), then tell the memory manager
1728 * via a flag added to the desired_access
1729 * parameter, so that it can detect a race
1730 * between our walking down the shadow chain
1731 * and its pushing pages up into a copy of
1732 * the object that it manages.
1734 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
) {
1735 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1737 wants_copy_flag
= VM_PROT_NONE
;
1740 if (object
->copy
== first_object
) {
1742 * if we issue the memory_object_data_request in
1743 * this state, we are subject to a deadlock with
1744 * the underlying filesystem if it is trying to
1745 * shrink the file resulting in a push of pages
1746 * into the copy object... that push will stall
1747 * on the placeholder page, and if the pushing thread
1748 * is holding a lock that is required on the pagein
1749 * path (such as a truncate lock), we'll deadlock...
1750 * to avoid this potential deadlock, we throw away
1751 * our placeholder page before calling memory_object_data_request
1752 * and force this thread to retry the vm_fault_page after
1753 * we have issued the I/O. the second time through this path
1754 * we will find the page already in the cache (presumably still
1755 * busy waiting for the I/O to complete) and then complete
1756 * the fault w/o having to go through memory_object_data_request again
1758 assert(first_m
!= VM_PAGE_NULL
);
1759 assert(VM_PAGE_OBJECT(first_m
) == first_object
);
1761 vm_object_lock(first_object
);
1762 VM_PAGE_FREE(first_m
);
1763 vm_object_paging_end(first_object
);
1764 vm_object_unlock(first_object
);
1766 first_m
= VM_PAGE_NULL
;
1767 force_fault_retry
= TRUE
;
1769 vm_fault_page_forced_retry
++;
1772 if (data_already_requested
== TRUE
) {
1773 orig_behavior
= fault_info
->behavior
;
1774 orig_cluster_size
= fault_info
->cluster_size
;
1776 fault_info
->behavior
= VM_BEHAVIOR_RANDOM
;
1777 fault_info
->cluster_size
= PAGE_SIZE
;
1780 * Call the memory manager to retrieve the data.
1782 rc
= memory_object_data_request(
1784 vm_object_trunc_page(offset
) + object
->paging_offset
,
1786 access_required
| wants_copy_flag
,
1787 (memory_object_fault_info_t
)fault_info
);
1789 if (data_already_requested
== TRUE
) {
1790 fault_info
->behavior
= orig_behavior
;
1791 fault_info
->cluster_size
= orig_cluster_size
;
1793 data_already_requested
= TRUE
;
1796 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1798 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1800 vm_object_lock(object
);
1802 if (object
->object_is_shared_cache
) {
1803 clear_thread_rwlock_boost();
1807 if (rc
!= KERN_SUCCESS
) {
1808 vm_fault_cleanup(object
, first_m
);
1809 thread_interrupt_level(interruptible_state
);
1811 return (rc
== MACH_SEND_INTERRUPTED
) ?
1812 VM_FAULT_INTERRUPTED
:
1813 VM_FAULT_MEMORY_ERROR
;
1816 clock_usec_t tv_usec
;
1818 if (my_fault_type
== DBG_PAGEIN_FAULT
) {
1819 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1820 current_thread()->t_page_creation_time
= tv_sec
;
1821 current_thread()->t_page_creation_count
= 0;
1824 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_flags
& TH_SFLAG_ABORT
)) {
1825 vm_fault_cleanup(object
, first_m
);
1826 thread_interrupt_level(interruptible_state
);
1828 return VM_FAULT_INTERRUPTED
;
1830 if (force_fault_retry
== TRUE
) {
1831 vm_fault_cleanup(object
, first_m
);
1832 thread_interrupt_level(interruptible_state
);
1834 return VM_FAULT_RETRY
;
1836 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1838 * No page here means that the object we
1839 * initially looked up was "physically
1840 * contiguous" (i.e. device memory). However,
1841 * with Virtual VRAM, the object might not
1842 * be backed by that device memory anymore,
1843 * so we're done here only if the object is
1844 * still "phys_contiguous".
1845 * Otherwise, if the object is no longer
1846 * "phys_contiguous", we need to retry the
1847 * page fault against the object's new backing
1848 * store (different memory object).
1854 * potentially a pagein fault
1855 * if we make it through the state checks
1856 * above, than we'll count it as such
1858 my_fault
= my_fault_type
;
1861 * Retry with same object/offset, since new data may
1862 * be in a different page (i.e., m is meaningless at
1869 * We get here if the object has no pager, or an existence map
1870 * exists and indicates the page isn't present on the pager
1871 * or we're unwiring a page. If a pager exists, but there
1872 * is no existence map, then the m->vmp_absent case above handles
1873 * the ZF case when the pager can't provide the page
1876 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1878 if (object
== first_object
) {
1881 assert(m
== VM_PAGE_NULL
);
1884 next_object
= object
->shadow
;
1886 if (next_object
== VM_OBJECT_NULL
) {
1888 * we've hit the bottom of the shadown chain,
1889 * fill the page in the top object with zeros.
1891 assert(!must_be_resident
);
1893 if (object
!= first_object
) {
1894 vm_object_paging_end(object
);
1895 vm_object_unlock(object
);
1897 object
= first_object
;
1898 offset
= first_offset
;
1899 vm_object_lock(object
);
1902 assert(VM_PAGE_OBJECT(m
) == object
);
1903 first_m
= VM_PAGE_NULL
;
1906 * check for any conditions that prevent
1907 * us from creating a new zero-fill page
1908 * vm_fault_check will do all of the
1909 * fault cleanup in the case of an error condition
1910 * including resetting the thread_interrupt_level
1912 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1914 if (error
!= VM_FAULT_SUCCESS
) {
1918 if (m
== VM_PAGE_NULL
) {
1919 m
= vm_page_grab_options(grab_options
);
1921 if (m
== VM_PAGE_NULL
) {
1922 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1923 thread_interrupt_level(interruptible_state
);
1925 return VM_FAULT_MEMORY_SHORTAGE
;
1927 vm_page_insert(m
, object
, vm_object_trunc_page(offset
));
1929 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
) {
1930 m
->vmp_absent
= TRUE
;
1933 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1938 * Move on to the next object. Lock the next
1939 * object before unlocking the current one.
1941 if ((object
!= first_object
) || must_be_resident
) {
1942 vm_object_paging_end(object
);
1945 offset
+= object
->vo_shadow_offset
;
1946 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1947 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1948 access_required
= VM_PROT_READ
;
1950 vm_object_lock(next_object
);
1951 vm_object_unlock(object
);
1953 object
= next_object
;
1954 vm_object_paging_begin(object
);
1959 * PAGE HAS BEEN FOUND.
1962 * busy, so that we can play with it;
1963 * not absent, so that nobody else will fill it;
1964 * possibly eligible for pageout;
1966 * The top-level page (first_m) is:
1967 * VM_PAGE_NULL if the page was found in the
1969 * busy, not absent, and ineligible for pageout.
1971 * The current object (object) is locked. A paging
1972 * reference is held for the current and top-level
1977 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1979 #if EXTRA_ASSERTIONS
1980 assert(m
->vmp_busy
&& !m
->vmp_absent
);
1981 assert((first_m
== VM_PAGE_NULL
) ||
1982 (first_m
->vmp_busy
&& !first_m
->vmp_absent
&&
1983 !first_m
->vmp_active
&& !first_m
->vmp_inactive
&& !first_m
->vmp_secluded
));
1984 #endif /* EXTRA_ASSERTIONS */
1987 * If the page is being written, but isn't
1988 * already owned by the top-level object,
1989 * we have to copy it into a new page owned
1990 * by the top-level object.
1992 if (object
!= first_object
) {
1994 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1996 if (fault_type
& VM_PROT_WRITE
) {
2000 * We only really need to copy if we
2003 assert(!must_be_resident
);
2006 * If we try to collapse first_object at this
2007 * point, we may deadlock when we try to get
2008 * the lock on an intermediate object (since we
2009 * have the bottom object locked). We can't
2010 * unlock the bottom object, because the page
2011 * we found may move (by collapse) if we do.
2013 * Instead, we first copy the page. Then, when
2014 * we have no more use for the bottom object,
2015 * we unlock it and try to collapse.
2017 * Note that we copy the page even if we didn't
2018 * need to... that's the breaks.
2022 * Allocate a page for the copy
2024 copy_m
= vm_page_grab_options(grab_options
);
2026 if (copy_m
== VM_PAGE_NULL
) {
2029 vm_fault_cleanup(object
, first_m
);
2030 thread_interrupt_level(interruptible_state
);
2032 return VM_FAULT_MEMORY_SHORTAGE
;
2035 vm_page_copy(m
, copy_m
);
2038 * If another map is truly sharing this
2039 * page with us, we have to flush all
2040 * uses of the original page, since we
2041 * can't distinguish those which want the
2042 * original from those which need the
2045 * XXXO If we know that only one map has
2046 * access to this page, then we could
2047 * avoid the pmap_disconnect() call.
2049 if (m
->vmp_pmapped
) {
2050 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2053 if (m
->vmp_clustered
) {
2054 VM_PAGE_COUNT_AS_PAGEIN(m
);
2055 VM_PAGE_CONSUME_CLUSTERED(m
);
2057 assert(!m
->vmp_cleaning
);
2060 * We no longer need the old page or object.
2065 * This check helps with marking the object as having a sequential pattern
2066 * Normally we'll miss doing this below because this fault is about COW to
2067 * the first_object i.e. bring page in from disk, push to object above but
2068 * don't update the file object's sequential pattern.
2070 if (object
->internal
== FALSE
) {
2071 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2074 vm_object_paging_end(object
);
2075 vm_object_unlock(object
);
2077 my_fault
= DBG_COW_FAULT
;
2078 VM_STAT_INCR(cow_faults
);
2079 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2080 current_task()->cow_faults
++;
2082 object
= first_object
;
2083 offset
= first_offset
;
2085 vm_object_lock(object
);
2087 * get rid of the place holder
2088 * page that we soldered in earlier
2090 VM_PAGE_FREE(first_m
);
2091 first_m
= VM_PAGE_NULL
;
2094 * and replace it with the
2095 * page we just copied into
2097 assert(copy_m
->vmp_busy
);
2098 vm_page_insert(copy_m
, object
, vm_object_trunc_page(offset
));
2099 SET_PAGE_DIRTY(copy_m
, TRUE
);
2103 * Now that we've gotten the copy out of the
2104 * way, let's try to collapse the top object.
2105 * But we have to play ugly games with
2106 * paging_in_progress to do that...
2108 vm_object_paging_end(object
);
2109 vm_object_collapse(object
, vm_object_trunc_page(offset
), TRUE
);
2110 vm_object_paging_begin(object
);
2112 *protection
&= (~VM_PROT_WRITE
);
2116 * Now check whether the page needs to be pushed into the
2117 * copy object. The use of asymmetric copy on write for
2118 * shared temporary objects means that we may do two copies to
2119 * satisfy the fault; one above to get the page from a
2120 * shadowed object, and one here to push it into the copy.
2122 try_failed_count
= 0;
2124 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
2125 vm_object_offset_t copy_offset
;
2129 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2132 * If the page is being written, but hasn't been
2133 * copied to the copy-object, we have to copy it there.
2135 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2136 *protection
&= ~VM_PROT_WRITE
;
2141 * If the page was guaranteed to be resident,
2142 * we must have already performed the copy.
2144 if (must_be_resident
) {
2149 * Try to get the lock on the copy_object.
2151 if (!vm_object_lock_try(copy_object
)) {
2152 vm_object_unlock(object
);
2155 mutex_pause(try_failed_count
); /* wait a bit */
2156 vm_object_lock(object
);
2160 try_failed_count
= 0;
2163 * Make another reference to the copy-object,
2164 * to keep it from disappearing during the
2167 vm_object_reference_locked(copy_object
);
2170 * Does the page exist in the copy?
2172 copy_offset
= first_offset
- copy_object
->vo_shadow_offset
;
2173 copy_offset
= vm_object_trunc_page(copy_offset
);
2175 if (copy_object
->vo_size
<= copy_offset
) {
2177 * Copy object doesn't cover this page -- do nothing.
2180 } else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
2182 * Page currently exists in the copy object
2184 if (copy_m
->vmp_busy
) {
2186 * If the page is being brought
2187 * in, wait for it and then retry.
2192 * take an extra ref so object won't die
2194 vm_object_reference_locked(copy_object
);
2195 vm_object_unlock(copy_object
);
2196 vm_fault_cleanup(object
, first_m
);
2197 counter(c_vm_fault_page_block_backoff_kernel
++);
2199 vm_object_lock(copy_object
);
2200 assert(copy_object
->ref_count
> 0);
2201 VM_OBJ_RES_DECR(copy_object
);
2202 vm_object_lock_assert_exclusive(copy_object
);
2203 copy_object
->ref_count
--;
2204 assert(copy_object
->ref_count
> 0);
2205 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
2207 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->vmp_busy
) {
2208 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
2210 vm_object_unlock(copy_object
);
2211 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2212 vm_object_deallocate(copy_object
);
2216 vm_object_unlock(copy_object
);
2217 vm_object_deallocate(copy_object
);
2218 thread_interrupt_level(interruptible_state
);
2220 return VM_FAULT_RETRY
;
2223 } else if (!PAGED_OUT(copy_object
, copy_offset
)) {
2225 * If PAGED_OUT is TRUE, then the page used to exist
2226 * in the copy-object, and has already been paged out.
2227 * We don't need to repeat this. If PAGED_OUT is
2228 * FALSE, then either we don't know (!pager_created,
2229 * for example) or it hasn't been paged out.
2230 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2231 * We must copy the page to the copy object.
2233 * Allocate a page for the copy
2235 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
2237 if (copy_m
== VM_PAGE_NULL
) {
2240 VM_OBJ_RES_DECR(copy_object
);
2241 vm_object_lock_assert_exclusive(copy_object
);
2242 copy_object
->ref_count
--;
2243 assert(copy_object
->ref_count
> 0);
2245 vm_object_unlock(copy_object
);
2246 vm_fault_cleanup(object
, first_m
);
2247 thread_interrupt_level(interruptible_state
);
2249 return VM_FAULT_MEMORY_SHORTAGE
;
2252 * Must copy page into copy-object.
2254 vm_page_copy(m
, copy_m
);
2257 * If the old page was in use by any users
2258 * of the copy-object, it must be removed
2259 * from all pmaps. (We can't know which
2262 if (m
->vmp_pmapped
) {
2263 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2266 if (m
->vmp_clustered
) {
2267 VM_PAGE_COUNT_AS_PAGEIN(m
);
2268 VM_PAGE_CONSUME_CLUSTERED(m
);
2271 * If there's a pager, then immediately
2272 * page out this page, using the "initialize"
2273 * option. Else, we use the copy.
2275 if ((!copy_object
->pager_ready
)
2276 || VM_COMPRESSOR_PAGER_STATE_GET(copy_object
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2278 vm_page_lockspin_queues();
2279 assert(!m
->vmp_cleaning
);
2280 vm_page_activate(copy_m
);
2281 vm_page_unlock_queues();
2283 SET_PAGE_DIRTY(copy_m
, TRUE
);
2284 PAGE_WAKEUP_DONE(copy_m
);
2286 assert(copy_m
->vmp_busy
== TRUE
);
2287 assert(!m
->vmp_cleaning
);
2290 * dirty is protected by the object lock
2292 SET_PAGE_DIRTY(copy_m
, TRUE
);
2295 * The page is already ready for pageout:
2296 * not on pageout queues and busy.
2297 * Unlock everything except the
2298 * copy_object itself.
2300 vm_object_unlock(object
);
2303 * Write the page to the copy-object,
2304 * flushing it from the kernel.
2306 vm_pageout_initialize_page(copy_m
);
2309 * Since the pageout may have
2310 * temporarily dropped the
2311 * copy_object's lock, we
2312 * check whether we'll have
2313 * to deallocate the hard way.
2315 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2316 vm_object_unlock(copy_object
);
2317 vm_object_deallocate(copy_object
);
2318 vm_object_lock(object
);
2323 * Pick back up the old object's
2324 * lock. [It is safe to do so,
2325 * since it must be deeper in the
2328 vm_object_lock(object
);
2332 * Because we're pushing a page upward
2333 * in the object tree, we must restart
2334 * any faults that are waiting here.
2335 * [Note that this is an expansion of
2336 * PAGE_WAKEUP that uses the THREAD_RESTART
2337 * wait result]. Can't turn off the page's
2338 * busy bit because we're not done with it.
2340 if (m
->vmp_wanted
) {
2341 m
->vmp_wanted
= FALSE
;
2342 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2346 * The reference count on copy_object must be
2347 * at least 2: one for our extra reference,
2348 * and at least one from the outside world
2349 * (we checked that when we last locked
2352 vm_object_lock_assert_exclusive(copy_object
);
2353 copy_object
->ref_count
--;
2354 assert(copy_object
->ref_count
> 0);
2356 VM_OBJ_RES_DECR(copy_object
);
2357 vm_object_unlock(copy_object
);
2364 *top_page
= first_m
;
2366 if (m
!= VM_PAGE_NULL
) {
2367 assert(VM_PAGE_OBJECT(m
) == object
);
2369 retval
= VM_FAULT_SUCCESS
;
2371 if (my_fault
== DBG_PAGEIN_FAULT
) {
2372 VM_PAGE_COUNT_AS_PAGEIN(m
);
2374 if (object
->internal
) {
2375 my_fault
= DBG_PAGEIND_FAULT
;
2377 my_fault
= DBG_PAGEINV_FAULT
;
2381 * evaluate access pattern and update state
2382 * vm_fault_deactivate_behind depends on the
2383 * state being up to date
2385 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2386 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2387 } else if (type_of_fault
== NULL
&& my_fault
== DBG_CACHE_HIT_FAULT
) {
2389 * we weren't called from vm_fault, so handle the
2390 * accounting here for hits in the cache
2392 if (m
->vmp_clustered
) {
2393 VM_PAGE_COUNT_AS_PAGEIN(m
);
2394 VM_PAGE_CONSUME_CLUSTERED(m
);
2396 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2397 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2398 } else if (my_fault
== DBG_COMPRESSOR_FAULT
|| my_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
) {
2399 VM_STAT_DECOMPRESSIONS();
2401 if (type_of_fault
) {
2402 *type_of_fault
= my_fault
;
2405 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2406 assert(first_m
== VM_PAGE_NULL
);
2407 assert(object
== first_object
);
2410 thread_interrupt_level(interruptible_state
);
2413 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2418 thread_interrupt_level(interruptible_state
);
2420 if (wait_result
== THREAD_INTERRUPTED
) {
2421 return VM_FAULT_INTERRUPTED
;
2423 return VM_FAULT_RETRY
;
2429 extern int panic_on_cs_killed
;
2430 extern int proc_selfpid(void);
2431 extern char *proc_name_address(void *p
);
2432 unsigned long cs_enter_tainted_rejected
= 0;
2433 unsigned long cs_enter_tainted_accepted
= 0;
2437 * When soft faulting a page, we have to validate the page if:
2438 * 1. the page is being mapped in user space
2439 * 2. the page hasn't already been found to be "tainted"
2440 * 3. the page belongs to a code-signed object
2441 * 4. the page has not been validated yet or has been mapped for write.
2444 vm_fault_cs_need_validation(
2447 vm_object_t page_obj
,
2448 vm_map_size_t fault_page_size
,
2449 vm_map_offset_t fault_phys_offset
)
2451 if (pmap
== kernel_pmap
) {
2452 /* 1 - not user space */
2455 if (!page_obj
->code_signed
) {
2456 /* 3 - page does not belong to a code-signed object */
2459 if (fault_page_size
== PAGE_SIZE
) {
2460 /* looking at the whole page */
2461 assertf(fault_phys_offset
== 0,
2462 "fault_page_size 0x%llx fault_phys_offset 0x%llx\n",
2463 (uint64_t)fault_page_size
,
2464 (uint64_t)fault_phys_offset
);
2465 if (page
->vmp_cs_tainted
== VMP_CS_ALL_TRUE
) {
2466 /* 2 - page is all tainted */
2469 if (page
->vmp_cs_validated
== VMP_CS_ALL_TRUE
&&
2470 !page
->vmp_wpmapped
) {
2471 /* 4 - already fully validated and never mapped writable */
2475 /* looking at a specific sub-page */
2476 if (VMP_CS_TAINTED(page
, fault_page_size
, fault_phys_offset
)) {
2477 /* 2 - sub-page was already marked as tainted */
2480 if (VMP_CS_VALIDATED(page
, fault_page_size
, fault_phys_offset
) &&
2481 !page
->vmp_wpmapped
) {
2482 /* 4 - already validated and never mapped writable */
2486 /* page needs to be validated */
2492 vm_fault_cs_page_immutable(
2494 vm_map_size_t fault_page_size
,
2495 vm_map_offset_t fault_phys_offset
,
2496 vm_prot_t prot __unused
)
2498 if (VMP_CS_VALIDATED(m
, fault_page_size
, fault_phys_offset
)
2499 /*&& ((prot) & VM_PROT_EXECUTE)*/) {
2506 vm_fault_cs_page_nx(
2508 vm_map_size_t fault_page_size
,
2509 vm_map_offset_t fault_phys_offset
)
2511 return VMP_CS_NX(m
, fault_page_size
, fault_phys_offset
);
2515 * Check if the page being entered into the pmap violates code signing.
2517 static kern_return_t
2518 vm_fault_cs_check_violation(
2524 vm_prot_t caller_prot
,
2525 vm_map_size_t fault_page_size
,
2526 vm_map_offset_t fault_phys_offset
,
2527 vm_object_fault_info_t fault_info
,
2528 bool map_is_switched
,
2529 bool map_is_switch_protected
,
2533 #pragma unused(caller_prot)
2534 #pragma unused(fault_info)
2535 #endif /* !PMAP_CS */
2536 int cs_enforcement_enabled
;
2538 vm_fault_cs_need_validation(pmap
, m
, object
,
2539 fault_page_size
, fault_phys_offset
)) {
2540 vm_object_lock_assert_exclusive(object
);
2542 if (VMP_CS_VALIDATED(m
, fault_page_size
, fault_phys_offset
)) {
2543 vm_cs_revalidates
++;
2546 /* VM map is locked, so 1 ref will remain on VM object -
2547 * so no harm if vm_page_validate_cs drops the object lock */
2549 vm_page_validate_cs(m
, fault_page_size
, fault_phys_offset
);
2552 /* If the map is switched, and is switch-protected, we must protect
2553 * some pages from being write-faulted: immutable pages because by
2554 * definition they may not be written, and executable pages because that
2555 * would provide a way to inject unsigned code.
2556 * If the page is immutable, we can simply return. However, we can't
2557 * immediately determine whether a page is executable anywhere. But,
2558 * we can disconnect it everywhere and remove the executable protection
2559 * from the current map. We do that below right before we do the
2562 if (pmap
== kernel_pmap
) {
2563 /* kernel fault: cs_enforcement does not apply */
2564 cs_enforcement_enabled
= 0;
2566 cs_enforcement_enabled
= pmap_get_vm_map_cs_enforced(pmap
);
2569 if (cs_enforcement_enabled
&& map_is_switched
&&
2570 map_is_switch_protected
&&
2571 vm_fault_cs_page_immutable(m
, fault_page_size
, fault_phys_offset
, prot
) &&
2572 (prot
& VM_PROT_WRITE
)) {
2573 return KERN_CODESIGN_ERROR
;
2576 if (cs_enforcement_enabled
&&
2577 vm_fault_cs_page_nx(m
, fault_page_size
, fault_phys_offset
) &&
2578 (prot
& VM_PROT_EXECUTE
)) {
2580 printf("page marked to be NX, not letting it be mapped EXEC\n");
2582 return KERN_CODESIGN_ERROR
;
2585 /* A page could be tainted, or pose a risk of being tainted later.
2586 * Check whether the receiving process wants it, and make it feel
2587 * the consequences (that hapens in cs_invalid_page()).
2588 * For CS Enforcement, two other conditions will
2589 * cause that page to be tainted as well:
2590 * - pmapping an unsigned page executable - this means unsigned code;
2591 * - writeable mapping of a validated page - the content of that page
2592 * can be changed without the kernel noticing, therefore unsigned
2593 * code can be created
2596 /* code-signing is bypassed */
2597 *cs_violation
= FALSE
;
2598 } else if (VMP_CS_TAINTED(m
, fault_page_size
, fault_phys_offset
)) {
2600 *cs_violation
= TRUE
;
2601 } else if (!cs_enforcement_enabled
) {
2602 /* no further code-signing enforcement */
2603 *cs_violation
= FALSE
;
2604 } else if (vm_fault_cs_page_immutable(m
, fault_page_size
, fault_phys_offset
, prot
) &&
2605 ((prot
& VM_PROT_WRITE
) ||
2608 * The page should be immutable, but is in danger of being
2610 * This is the case where we want policy from the code
2611 * directory - is the page immutable or not? For now we have
2612 * to assume that code pages will be immutable, data pages not.
2613 * We'll assume a page is a code page if it has a code directory
2614 * and we fault for execution.
2615 * That is good enough since if we faulted the code page for
2616 * writing in another map before, it is wpmapped; if we fault
2617 * it for writing in this map later it will also be faulted for
2618 * executing at the same time; and if we fault for writing in
2619 * another map later, we will disconnect it from this pmap so
2620 * we'll notice the change.
2622 *cs_violation
= TRUE
;
2623 } else if (!VMP_CS_VALIDATED(m
, fault_page_size
, fault_phys_offset
) &&
2624 (prot
& VM_PROT_EXECUTE
)
2626 *cs_violation
= TRUE
;
2628 *cs_violation
= FALSE
;
2630 return KERN_SUCCESS
;
2634 * Handles a code signing violation by either rejecting the page or forcing a disconnect.
2635 * @param must_disconnect This value will be set to true if the caller must disconnect
2637 * @return If this function does not return KERN_SUCCESS, the caller must abort the page fault.
2639 static kern_return_t
2640 vm_fault_cs_handle_violation(
2645 vm_map_offset_t vaddr
,
2646 vm_map_size_t fault_page_size
,
2647 vm_map_offset_t fault_phys_offset
,
2648 bool map_is_switched
,
2649 bool map_is_switch_protected
,
2650 bool *must_disconnect
)
2653 #pragma unused(pmap)
2654 #pragma unused(map_is_switch_protected)
2655 #endif /* !MACH_ASSERT */
2657 * We will have a tainted page. Have to handle the special case
2658 * of a switched map now. If the map is not switched, standard
2659 * procedure applies - call cs_invalid_page().
2660 * If the map is switched, the real owner is invalid already.
2661 * There is no point in invalidating the switching process since
2662 * it will not be executing from the map. So we don't call
2663 * cs_invalid_page() in that case.
2665 boolean_t reject_page
, cs_killed
;
2667 if (map_is_switched
) {
2668 assert(pmap
== vm_map_pmap(current_thread()->map
));
2669 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2670 reject_page
= FALSE
;
2673 printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s prot: 0x%x\n",
2674 object
->code_signed
? "yes" : "no",
2675 VMP_CS_VALIDATED(m
, fault_page_size
, fault_phys_offset
) ? "yes" : "no",
2676 VMP_CS_TAINTED(m
, fault_page_size
, fault_phys_offset
) ? "yes" : "no",
2677 m
->vmp_wpmapped
? "yes" : "no",
2680 reject_page
= cs_invalid_page((addr64_t
) vaddr
, &cs_killed
);
2684 /* reject the invalid page: abort the page fault */
2686 const char *procname
;
2688 vm_object_t file_object
, shadow
;
2689 vm_object_offset_t file_offset
;
2690 char *pathname
, *filename
;
2691 vm_size_t pathname_len
, filename_len
;
2692 boolean_t truncated_path
;
2693 #define __PATH_MAX 1024
2694 struct timespec mtime
, cs_mtime
;
2696 os_reason_t codesigning_exit_reason
= OS_REASON_NULL
;
2698 kr
= KERN_CODESIGN_ERROR
;
2699 cs_enter_tainted_rejected
++;
2701 /* get process name and pid */
2703 task
= current_task();
2704 pid
= proc_selfpid();
2705 if (task
->bsd_info
!= NULL
) {
2706 procname
= proc_name_address(task
->bsd_info
);
2709 /* get file's VM object */
2710 file_object
= object
;
2711 file_offset
= m
->vmp_offset
;
2712 for (shadow
= file_object
->shadow
,
2714 shadow
!= VM_OBJECT_NULL
;
2715 shadow
= file_object
->shadow
,
2717 vm_object_lock_shared(shadow
);
2718 if (file_object
!= object
) {
2719 vm_object_unlock(file_object
);
2721 file_offset
+= file_object
->vo_shadow_offset
;
2722 file_object
= shadow
;
2727 cs_mtime
.tv_sec
= 0;
2728 cs_mtime
.tv_nsec
= 0;
2730 /* get file's pathname and/or filename */
2735 truncated_path
= FALSE
;
2736 /* no pager -> no file -> no pathname, use "<nil>" in that case */
2737 if (file_object
->pager
!= NULL
) {
2738 pathname
= kheap_alloc(KHEAP_TEMP
, __PATH_MAX
* 2, Z_WAITOK
);
2741 pathname_len
= __PATH_MAX
;
2742 filename
= pathname
+ pathname_len
;
2743 filename_len
= __PATH_MAX
;
2745 if (vnode_pager_get_object_name(file_object
->pager
,
2750 &truncated_path
) == KERN_SUCCESS
) {
2751 /* safety first... */
2752 pathname
[__PATH_MAX
- 1] = '\0';
2753 filename
[__PATH_MAX
- 1] = '\0';
2755 vnode_pager_get_object_mtime(file_object
->pager
,
2759 kheap_free(KHEAP_TEMP
, pathname
, __PATH_MAX
* 2);
2764 truncated_path
= FALSE
;
2768 printf("CODE SIGNING: process %d[%s]: "
2769 "rejecting invalid page at address 0x%llx "
2770 "from offset 0x%llx in file \"%s%s%s\" "
2771 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2772 "(signed:%d validated:%d tainted:%d nx:%d "
2773 "wpmapped:%d dirty:%d depth:%d)\n",
2774 pid
, procname
, (addr64_t
) vaddr
,
2776 (pathname
? pathname
: "<nil>"),
2777 (truncated_path
? "/.../" : ""),
2778 (truncated_path
? filename
: ""),
2779 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2780 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2781 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2784 mtime
.tv_sec
, mtime
.tv_nsec
,
2785 object
->code_signed
,
2786 VMP_CS_VALIDATED(m
, fault_page_size
, fault_phys_offset
),
2787 VMP_CS_TAINTED(m
, fault_page_size
, fault_phys_offset
),
2788 VMP_CS_NX(m
, fault_page_size
, fault_phys_offset
),
2794 * We currently only generate an exit reason if cs_invalid_page directly killed a process. If cs_invalid_page
2795 * did not kill the process (more the case on desktop), vm_fault_enter will not satisfy the fault and whether the
2796 * process dies is dependent on whether there is a signal handler registered for SIGSEGV and how that handler
2797 * will deal with the segmentation fault.
2800 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
2801 pid
, OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_INVALID_PAGE
, 0, 0);
2803 codesigning_exit_reason
= os_reason_create(OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_INVALID_PAGE
);
2804 if (codesigning_exit_reason
== NULL
) {
2805 printf("vm_fault_enter: failed to allocate codesigning exit reason\n");
2807 mach_vm_address_t data_addr
= 0;
2808 struct codesigning_exit_reason_info
*ceri
= NULL
;
2809 uint32_t reason_buffer_size_estimate
= kcdata_estimate_required_buffer_size(1, sizeof(*ceri
));
2811 if (os_reason_alloc_buffer_noblock(codesigning_exit_reason
, reason_buffer_size_estimate
)) {
2812 printf("vm_fault_enter: failed to allocate buffer for codesigning exit reason\n");
2814 if (KERN_SUCCESS
== kcdata_get_memory_addr(&codesigning_exit_reason
->osr_kcd_descriptor
,
2815 EXIT_REASON_CODESIGNING_INFO
, sizeof(*ceri
), &data_addr
)) {
2816 ceri
= (struct codesigning_exit_reason_info
*)data_addr
;
2817 static_assert(__PATH_MAX
== sizeof(ceri
->ceri_pathname
));
2819 ceri
->ceri_virt_addr
= vaddr
;
2820 ceri
->ceri_file_offset
= file_offset
;
2822 strncpy((char *)&ceri
->ceri_pathname
, pathname
, sizeof(ceri
->ceri_pathname
));
2824 ceri
->ceri_pathname
[0] = '\0';
2827 strncpy((char *)&ceri
->ceri_filename
, filename
, sizeof(ceri
->ceri_filename
));
2829 ceri
->ceri_filename
[0] = '\0';
2831 ceri
->ceri_path_truncated
= (truncated_path
? 1 : 0);
2832 ceri
->ceri_codesig_modtime_secs
= cs_mtime
.tv_sec
;
2833 ceri
->ceri_codesig_modtime_nsecs
= cs_mtime
.tv_nsec
;
2834 ceri
->ceri_page_modtime_secs
= mtime
.tv_sec
;
2835 ceri
->ceri_page_modtime_nsecs
= mtime
.tv_nsec
;
2836 ceri
->ceri_object_codesigned
= (object
->code_signed
);
2837 ceri
->ceri_page_codesig_validated
= VMP_CS_VALIDATED(m
, fault_page_size
, fault_phys_offset
);
2838 ceri
->ceri_page_codesig_tainted
= VMP_CS_TAINTED(m
, fault_page_size
, fault_phys_offset
);
2839 ceri
->ceri_page_codesig_nx
= VMP_CS_NX(m
, fault_page_size
, fault_phys_offset
);
2840 ceri
->ceri_page_wpmapped
= (m
->vmp_wpmapped
);
2841 ceri
->ceri_page_slid
= 0;
2842 ceri
->ceri_page_dirty
= (m
->vmp_dirty
);
2843 ceri
->ceri_page_shadow_depth
= shadow_depth
;
2845 #if DEBUG || DEVELOPMENT
2846 panic("vm_fault_enter: failed to allocate kcdata for codesigning exit reason");
2848 printf("vm_fault_enter: failed to allocate kcdata for codesigning exit reason\n");
2849 #endif /* DEBUG || DEVELOPMENT */
2850 /* Free the buffer */
2851 os_reason_alloc_buffer_noblock(codesigning_exit_reason
, 0);
2856 set_thread_exit_reason(current_thread(), codesigning_exit_reason
, FALSE
);
2858 if (panic_on_cs_killed
&&
2859 object
->object_is_shared_cache
) {
2860 char *tainted_contents
;
2861 vm_map_offset_t src_vaddr
;
2862 src_vaddr
= (vm_map_offset_t
) phystokv((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(m
) << PAGE_SHIFT
);
2863 tainted_contents
= kalloc(PAGE_SIZE
);
2864 bcopy((const char *)src_vaddr
, tainted_contents
, PAGE_SIZE
);
2865 printf("CODE SIGNING: tainted page %p phys 0x%x phystokv 0x%llx copied to %p\n", m
, VM_PAGE_GET_PHYS_PAGE(m
), (uint64_t)src_vaddr
, tainted_contents
);
2866 panic("CODE SIGNING: process %d[%s]: "
2867 "rejecting invalid page (phys#0x%x) at address 0x%llx "
2868 "from offset 0x%llx in file \"%s%s%s\" "
2869 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2870 "(signed:%d validated:%d tainted:%d nx:%d"
2871 "wpmapped:%d dirty:%d depth:%d)\n",
2873 VM_PAGE_GET_PHYS_PAGE(m
),
2876 (pathname
? pathname
: "<nil>"),
2877 (truncated_path
? "/.../" : ""),
2878 (truncated_path
? filename
: ""),
2879 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2880 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2881 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2884 mtime
.tv_sec
, mtime
.tv_nsec
,
2885 object
->code_signed
,
2886 VMP_CS_VALIDATED(m
, fault_page_size
, fault_phys_offset
),
2887 VMP_CS_TAINTED(m
, fault_page_size
, fault_phys_offset
),
2888 VMP_CS_NX(m
, fault_page_size
, fault_phys_offset
),
2894 if (file_object
!= object
) {
2895 vm_object_unlock(file_object
);
2897 if (pathname_len
!= 0) {
2898 kheap_free(KHEAP_TEMP
, pathname
, __PATH_MAX
* 2);
2903 /* proceed with the invalid page */
2905 if (!VMP_CS_VALIDATED(m
, fault_page_size
, fault_phys_offset
) &&
2906 !object
->code_signed
) {
2908 * This page has not been (fully) validated but
2909 * does not belong to a code-signed object
2910 * so it should not be forcefully considered
2912 * We're just concerned about it here because
2913 * we've been asked to "execute" it but that
2914 * does not mean that it should cause other
2916 * This happens when a debugger sets a
2917 * breakpoint and we then execute code in
2918 * that page. Marking the page as "tainted"
2919 * would cause any inspection tool ("leaks",
2920 * "vmmap", "CrashReporter", ...) to get killed
2921 * due to code-signing violation on that page,
2922 * even though they're just reading it and not
2923 * executing from it.
2927 * Page might have been tainted before or not;
2928 * now it definitively is. If the page wasn't
2929 * tainted, we must disconnect it from all
2930 * pmaps later, to force existing mappings
2931 * through that code path for re-consideration
2932 * of the validity of that page.
2934 if (!VMP_CS_TAINTED(m
, fault_page_size
, fault_phys_offset
)) {
2935 *must_disconnect
= TRUE
;
2936 VMP_CS_SET_TAINTED(m
, fault_page_size
, fault_phys_offset
, TRUE
);
2939 cs_enter_tainted_accepted
++;
2941 if (kr
!= KERN_SUCCESS
) {
2943 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2944 "*** INVALID PAGE ***\n",
2948 if (cs_enforcement_panic
) {
2949 panic("CODESIGNING: panicking on invalid page\n");
2957 * Check that the code signature is valid for the given page being inserted into
2960 * @param must_disconnect This value will be set to true if the caller must disconnect
2962 * @return If this function does not return KERN_SUCCESS, the caller must abort the page fault.
2964 static kern_return_t
2965 vm_fault_validate_cs(
2970 vm_map_offset_t vaddr
,
2972 vm_prot_t caller_prot
,
2973 vm_map_size_t fault_page_size
,
2974 vm_map_offset_t fault_phys_offset
,
2975 vm_object_fault_info_t fault_info
,
2976 bool *must_disconnect
)
2978 bool map_is_switched
, map_is_switch_protected
, cs_violation
;
2980 /* Validate code signature if necessary. */
2981 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2982 (pmap
== vm_map_pmap(current_thread()->map
)));
2983 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2984 kr
= vm_fault_cs_check_violation(cs_bypass
, object
, m
, pmap
,
2985 prot
, caller_prot
, fault_page_size
, fault_phys_offset
, fault_info
,
2986 map_is_switched
, map_is_switch_protected
, &cs_violation
);
2987 if (kr
!= KERN_SUCCESS
) {
2991 kr
= vm_fault_cs_handle_violation(object
, m
, pmap
, prot
, vaddr
,
2992 fault_page_size
, fault_phys_offset
,
2993 map_is_switched
, map_is_switch_protected
, must_disconnect
);
2999 * Enqueue the page on the appropriate paging queue.
3002 vm_fault_enqueue_page(
3012 assert((m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) || object
!= compressor_object
);
3013 boolean_t page_queues_locked
= FALSE
;
3014 boolean_t previously_pmapped
= m
->vmp_pmapped
;
3015 #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \
3017 if (! page_queues_locked) { \
3018 page_queues_locked = TRUE; \
3019 vm_page_lockspin_queues(); \
3022 #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \
3024 if (page_queues_locked) { \
3025 page_queues_locked = FALSE; \
3026 vm_page_unlock_queues(); \
3030 #if CONFIG_BACKGROUND_QUEUE
3031 vm_page_update_background_state(m
);
3033 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
3035 * Compressor pages are neither wired
3036 * nor pageable and should never change.
3038 assert(object
== compressor_object
);
3039 } else if (change_wiring
) {
3040 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3043 if (kr
== KERN_SUCCESS
) {
3044 vm_page_wire(m
, wire_tag
, TRUE
);
3047 vm_page_unwire(m
, TRUE
);
3049 /* we keep the page queues lock, if we need it later */
3051 if (object
->internal
== TRUE
) {
3053 * don't allow anonymous pages on
3054 * the speculative queues
3058 if (kr
!= KERN_SUCCESS
) {
3059 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3060 vm_page_deactivate(m
);
3061 /* we keep the page queues lock, if we need it later */
3062 } else if (((m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
) ||
3063 (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) ||
3064 (m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) ||
3065 ((m
->vmp_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && no_cache
)) &&
3066 !VM_PAGE_WIRED(m
)) {
3067 if (vm_page_local_q
&&
3068 (*type_of_fault
== DBG_COW_FAULT
||
3069 *type_of_fault
== DBG_ZERO_FILL_FAULT
)) {
3073 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
3075 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3076 vm_object_lock_assert_exclusive(object
);
3079 * we got a local queue to stuff this
3081 * its safe to manipulate local and
3082 * local_id at this point since we're
3083 * behind an exclusive object lock and
3084 * the page is not on any global queue.
3086 * we'll use the current cpu number to
3087 * select the queue note that we don't
3088 * need to disable preemption... we're
3089 * going to be behind the local queue's
3090 * lock to do the real work
3094 lq
= zpercpu_get_cpu(vm_page_local_q
, lid
);
3096 VPL_LOCK(&lq
->vpl_lock
);
3098 vm_page_check_pageable_safe(m
);
3099 vm_page_queue_enter(&lq
->vpl_queue
, m
, vmp_pageq
);
3100 m
->vmp_q_state
= VM_PAGE_ON_ACTIVE_LOCAL_Q
;
3101 m
->vmp_local_id
= lid
;
3104 if (object
->internal
) {
3105 lq
->vpl_internal_count
++;
3107 lq
->vpl_external_count
++;
3110 VPL_UNLOCK(&lq
->vpl_lock
);
3112 if (lq
->vpl_count
> vm_page_local_q_soft_limit
) {
3114 * we're beyond the soft limit
3115 * for the local queue
3116 * vm_page_reactivate_local will
3117 * 'try' to take the global page
3118 * queue lock... if it can't
3119 * that's ok... we'll let the
3120 * queue continue to grow up
3121 * to the hard limit... at that
3122 * point we'll wait for the
3123 * lock... once we've got the
3124 * lock, we'll transfer all of
3125 * the pages from the local
3126 * queue to the global active
3129 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
3132 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3135 * test again now that we hold the
3138 if (!VM_PAGE_WIRED(m
)) {
3139 if (m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) {
3140 vm_page_queues_remove(m
, FALSE
);
3142 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated
, 1);
3143 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_fault_reactivated
, 1);
3146 if (!VM_PAGE_ACTIVE_OR_INACTIVE(m
) ||
3149 * If this is a no_cache mapping
3150 * and the page has never been
3151 * mapped before or was
3152 * previously a no_cache page,
3153 * then we want to leave pages
3154 * in the speculative state so
3155 * that they can be readily
3156 * recycled if free memory runs
3157 * low. Otherwise the page is
3158 * activated as normal.
3162 (!previously_pmapped
||
3164 m
->vmp_no_cache
= TRUE
;
3166 if (m
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
3167 vm_page_speculate(m
, FALSE
);
3169 } else if (!VM_PAGE_ACTIVE_OR_INACTIVE(m
)) {
3170 vm_page_activate(m
);
3174 /* we keep the page queues lock, if we need it later */
3178 /* we're done with the page queues lock, if we ever took it */
3179 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3183 * Sets the pmmpped, xpmapped, and wpmapped bits on the vm_page_t and updates accounting.
3184 * @return true if the page needs to be sync'ed via pmap_sync-page_data_physo
3185 * before being inserted into the pmap.
3188 vm_fault_enter_set_mapped(
3192 vm_prot_t fault_type
)
3194 bool page_needs_sync
= false;
3196 * NOTE: we may only hold the vm_object lock SHARED
3197 * at this point, so we need the phys_page lock to
3198 * properly serialize updating the pmapped and
3201 if ((prot
& VM_PROT_EXECUTE
) && !m
->vmp_xpmapped
) {
3202 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3204 pmap_lock_phys_page(phys_page
);
3205 m
->vmp_pmapped
= TRUE
;
3207 if (!m
->vmp_xpmapped
) {
3208 m
->vmp_xpmapped
= TRUE
;
3210 pmap_unlock_phys_page(phys_page
);
3212 if (!object
->internal
) {
3213 OSAddAtomic(1, &vm_page_xpmapped_external_count
);
3216 #if defined(__arm__) || defined(__arm64__)
3217 page_needs_sync
= true;
3219 if (object
->internal
&&
3220 object
->pager
!= NULL
) {
3222 * This page could have been
3223 * uncompressed by the
3224 * compressor pager and its
3225 * contents might be only in
3227 * Since it's being mapped for
3228 * "execute" for the fist time,
3229 * make sure the icache is in
3232 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3233 page_needs_sync
= true;
3237 pmap_unlock_phys_page(phys_page
);
3240 if (m
->vmp_pmapped
== FALSE
) {
3241 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3243 pmap_lock_phys_page(phys_page
);
3244 m
->vmp_pmapped
= TRUE
;
3245 pmap_unlock_phys_page(phys_page
);
3249 if (fault_type
& VM_PROT_WRITE
) {
3250 if (m
->vmp_wpmapped
== FALSE
) {
3251 vm_object_lock_assert_exclusive(object
);
3252 if (!object
->internal
&& object
->pager
) {
3253 task_update_logical_writes(current_task(), PAGE_SIZE
, TASK_WRITE_DEFERRED
, vnode_pager_lookup_vnode(object
->pager
));
3255 m
->vmp_wpmapped
= TRUE
;
3258 return page_needs_sync
;
3262 * Try to enter the given page into the pmap.
3263 * Will retry without execute permission iff PMAP_CS is enabled and we encounter
3264 * a codesigning failure on a non-execute fault.
3266 static kern_return_t
3267 vm_fault_attempt_pmap_enter(
3269 vm_map_offset_t vaddr
,
3270 vm_map_size_t fault_page_size
,
3271 vm_map_offset_t fault_phys_offset
,
3274 vm_prot_t caller_prot
,
3275 vm_prot_t fault_type
,
3280 #pragma unused(caller_prot)
3281 #endif /* !PMAP_CS */
3283 if (fault_page_size
!= PAGE_SIZE
) {
3284 DEBUG4K_FAULT("pmap %p va 0x%llx pa 0x%llx (0x%llx+0x%llx) prot 0x%x fault_type 0x%x\n", pmap
, (uint64_t)vaddr
, (uint64_t)((((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(m
)) << PAGE_SHIFT
) + fault_phys_offset
), (uint64_t)(((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(m
)) << PAGE_SHIFT
), (uint64_t)fault_phys_offset
, *prot
, fault_type
);
3285 assertf((!(fault_phys_offset
& FOURK_PAGE_MASK
) &&
3286 fault_phys_offset
< PAGE_SIZE
),
3287 "0x%llx\n", (uint64_t)fault_phys_offset
);
3289 assertf(fault_phys_offset
== 0,
3290 "0x%llx\n", (uint64_t)fault_phys_offset
);
3293 PMAP_ENTER_OPTIONS(pmap
, vaddr
,
3295 m
, *prot
, fault_type
, 0,
3303 * Enter the given page into the pmap.
3304 * The map must be locked shared.
3305 * The vm object must NOT be locked.
3307 * @param need_retry if not null, avoid making a (potentially) blocking call into
3308 * the pmap layer. When such a call would be necessary, return true in this boolean instead.
3310 static kern_return_t
3311 vm_fault_pmap_enter(
3313 vm_map_offset_t vaddr
,
3314 vm_map_size_t fault_page_size
,
3315 vm_map_offset_t fault_phys_offset
,
3318 vm_prot_t caller_prot
,
3319 vm_prot_t fault_type
,
3322 boolean_t
*need_retry
)
3325 if (need_retry
!= NULL
) {
3327 * Although we don't hold a lock on this object, we hold a lock
3328 * on the top object in the chain. To prevent a deadlock, we
3329 * can't allow the pmap layer to block.
3331 pmap_options
|= PMAP_OPTIONS_NOWAIT
;
3333 kr
= vm_fault_attempt_pmap_enter(pmap
, vaddr
,
3334 fault_page_size
, fault_phys_offset
,
3335 m
, prot
, caller_prot
, fault_type
, wired
, pmap_options
);
3336 if (kr
== KERN_RESOURCE_SHORTAGE
) {
3339 * There's nothing we can do here since we hold the
3340 * lock on the top object in the chain. The caller
3341 * will need to deal with this by dropping that lock and retrying.
3344 vm_pmap_enter_retried
++;
3351 * Enter the given page into the pmap.
3352 * The vm map must be locked shared.
3353 * The vm object must be locked exclusive, unless this is a soft fault.
3354 * For a soft fault, the object must be locked shared or exclusive.
3356 * @param need_retry if not null, avoid making a (potentially) blocking call into
3357 * the pmap layer. When such a call would be necessary, return true in this boolean instead.
3359 static kern_return_t
3360 vm_fault_pmap_enter_with_object_lock(
3363 vm_map_offset_t vaddr
,
3364 vm_map_size_t fault_page_size
,
3365 vm_map_offset_t fault_phys_offset
,
3368 vm_prot_t caller_prot
,
3369 vm_prot_t fault_type
,
3372 boolean_t
*need_retry
)
3376 * Prevent a deadlock by not
3377 * holding the object lock if we need to wait for a page in
3378 * pmap_enter() - <rdar://problem/7138958>
3380 kr
= vm_fault_attempt_pmap_enter(pmap
, vaddr
,
3381 fault_page_size
, fault_phys_offset
,
3382 m
, prot
, caller_prot
, fault_type
, wired
, pmap_options
| PMAP_OPTIONS_NOWAIT
);
3384 if (kr
== KERN_INVALID_ARGUMENT
&&
3385 pmap
== PMAP_NULL
&&
3388 * Wiring a page in a pmap-less VM map:
3389 * VMware's "vmmon" kernel extension does this
3391 * Let it proceed even though the PMAP_ENTER() failed.
3395 #endif /* __x86_64__ */
3397 if (kr
== KERN_RESOURCE_SHORTAGE
) {
3400 * this will be non-null in the case where we hold the lock
3401 * on the top-object in this chain... we can't just drop
3402 * the lock on the object we're inserting the page into
3403 * and recall the PMAP_ENTER since we can still cause
3404 * a deadlock if one of the critical paths tries to
3405 * acquire the lock on the top-object and we're blocked
3406 * in PMAP_ENTER waiting for memory... our only recourse
3407 * is to deal with it at a higher level where we can
3411 vm_pmap_enter_retried
++;
3415 * The nonblocking version of pmap_enter did not succeed.
3416 * and we don't need to drop other locks and retry
3417 * at the level above us, so
3418 * use the blocking version instead. Requires marking
3419 * the page busy and unlocking the object
3421 boolean_t was_busy
= m
->vmp_busy
;
3423 vm_object_lock_assert_exclusive(object
);
3426 vm_object_unlock(object
);
3428 PMAP_ENTER_OPTIONS(pmap
, vaddr
,
3430 m
, *prot
, fault_type
,
3434 assert(VM_PAGE_OBJECT(m
) == object
);
3436 /* Take the object lock again. */
3437 vm_object_lock(object
);
3439 /* If the page was busy, someone else will wake it up.
3440 * Otherwise, we have to do it now. */
3441 assert(m
->vmp_busy
);
3443 PAGE_WAKEUP_DONE(m
);
3445 vm_pmap_enter_blocked
++;
3453 * Prepare to enter a page into the pmap by checking CS, protection bits,
3454 * and setting mapped bits on the page_t.
3455 * Does not modify the page's paging queue.
3457 * page queue lock must NOT be held
3458 * m->vmp_object must be locked
3460 * NOTE: m->vmp_object could be locked "shared" only if we are called
3461 * from vm_fault() as part of a soft fault.
3463 static kern_return_t
3464 vm_fault_enter_prepare(
3467 vm_map_offset_t vaddr
,
3469 vm_prot_t caller_prot
,
3470 vm_map_size_t fault_page_size
,
3471 vm_map_offset_t fault_phys_offset
,
3472 boolean_t change_wiring
,
3473 vm_prot_t fault_type
,
3474 vm_object_fault_info_t fault_info
,
3476 bool *page_needs_data_sync
)
3479 bool is_tainted
= false;
3481 boolean_t cs_bypass
= fault_info
->cs_bypass
;
3483 object
= VM_PAGE_OBJECT(m
);
3485 vm_object_lock_assert_held(object
);
3488 if (pmap
== kernel_pmap
) {
3489 kasan_notify_address(vaddr
, PAGE_SIZE
);
3493 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
3495 if (*type_of_fault
== DBG_ZERO_FILL_FAULT
) {
3496 vm_object_lock_assert_exclusive(object
);
3497 } else if ((fault_type
& VM_PROT_WRITE
) == 0 &&
3500 #if VM_OBJECT_ACCESS_TRACKING
3501 || object
->access_tracking
3502 #endif /* VM_OBJECT_ACCESS_TRACKING */
3505 * This is not a "write" fault, so we
3506 * might not have taken the object lock
3507 * exclusively and we might not be able
3508 * to update the "wpmapped" bit in
3510 * Let's just grant read access to
3511 * the page for now and we'll
3512 * soft-fault again if we need write
3516 /* This had better not be a JIT page. */
3517 if (!pmap_has_prot_policy(pmap
, fault_info
->pmap_options
& PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE
, *prot
)) {
3518 *prot
&= ~VM_PROT_WRITE
;
3523 if (m
->vmp_pmapped
== FALSE
) {
3524 if (m
->vmp_clustered
) {
3525 if (*type_of_fault
== DBG_CACHE_HIT_FAULT
) {
3527 * found it in the cache, but this
3528 * is the first fault-in of the page (m->vmp_pmapped == FALSE)
3529 * so it must have come in as part of
3530 * a cluster... account 1 pagein against it
3532 if (object
->internal
) {
3533 *type_of_fault
= DBG_PAGEIND_FAULT
;
3535 *type_of_fault
= DBG_PAGEINV_FAULT
;
3538 VM_PAGE_COUNT_AS_PAGEIN(m
);
3540 VM_PAGE_CONSUME_CLUSTERED(m
);
3544 if (*type_of_fault
!= DBG_COW_FAULT
) {
3545 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
3547 if (pmap
== kernel_pmap
) {
3548 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
3552 kr
= vm_fault_validate_cs(cs_bypass
, object
, m
, pmap
, vaddr
,
3553 *prot
, caller_prot
, fault_page_size
, fault_phys_offset
,
3554 fault_info
, &is_tainted
);
3555 if (kr
== KERN_SUCCESS
) {
3557 * We either have a good page, or a tainted page that has been accepted by the process.
3558 * In both cases the page will be entered into the pmap.
3560 *page_needs_data_sync
= vm_fault_enter_set_mapped(object
, m
, *prot
, fault_type
);
3561 if ((fault_type
& VM_PROT_WRITE
) && is_tainted
) {
3563 * This page is tainted but we're inserting it anyways.
3564 * Since it's writeable, we need to disconnect it from other pmaps
3565 * now so those processes can take note.
3569 * We can only get here
3570 * because of the CSE logic
3572 assert(pmap_get_vm_map_cs_enforced(pmap
));
3573 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
3575 * If we are faulting for a write, we can clear
3576 * the execute bit - that will ensure the page is
3577 * checked again before being executable, which
3578 * protects against a map switch.
3579 * This only happens the first time the page
3580 * gets tainted, so we won't get stuck here
3581 * to make an already writeable page executable.
3584 assert(!pmap_has_prot_policy(pmap
, fault_info
->pmap_options
& PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE
, *prot
));
3585 *prot
&= ~VM_PROT_EXECUTE
;
3588 assert(VM_PAGE_OBJECT(m
) == object
);
3590 #if VM_OBJECT_ACCESS_TRACKING
3591 if (object
->access_tracking
) {
3592 DTRACE_VM2(access_tracking
, vm_map_offset_t
, vaddr
, int, fault_type
);
3593 if (fault_type
& VM_PROT_WRITE
) {
3594 object
->access_tracking_writes
++;
3595 vm_object_access_tracking_writes
++;
3597 object
->access_tracking_reads
++;
3598 vm_object_access_tracking_reads
++;
3601 #endif /* VM_OBJECT_ACCESS_TRACKING */
3608 * page queue lock must NOT be held
3609 * m->vmp_object must be locked
3611 * NOTE: m->vmp_object could be locked "shared" only if we are called
3612 * from vm_fault() as part of a soft fault. If so, we must be
3613 * careful not to modify the VM object in any way that is not
3614 * legal under a shared lock...
3620 vm_map_offset_t vaddr
,
3621 vm_map_size_t fault_page_size
,
3622 vm_map_offset_t fault_phys_offset
,
3624 vm_prot_t caller_prot
,
3626 boolean_t change_wiring
,
3628 vm_object_fault_info_t fault_info
,
3629 boolean_t
*need_retry
,
3634 bool page_needs_data_sync
;
3635 vm_prot_t fault_type
;
3636 int pmap_options
= fault_info
->pmap_options
;
3638 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
3639 assert(m
->vmp_fictitious
);
3640 return KERN_SUCCESS
;
3643 fault_type
= change_wiring
? VM_PROT_NONE
: caller_prot
;
3645 kr
= vm_fault_enter_prepare(m
, pmap
, vaddr
, &prot
, caller_prot
,
3646 fault_page_size
, fault_phys_offset
, change_wiring
, fault_type
,
3647 fault_info
, type_of_fault
, &page_needs_data_sync
);
3648 object
= VM_PAGE_OBJECT(m
);
3650 vm_fault_enqueue_page(object
, m
, wired
, change_wiring
, wire_tag
, fault_info
->no_cache
, type_of_fault
, kr
);
3652 if (kr
== KERN_SUCCESS
) {
3653 if (page_needs_data_sync
) {
3654 pmap_sync_page_data_phys(VM_PAGE_GET_PHYS_PAGE(m
));
3657 kr
= vm_fault_pmap_enter_with_object_lock(object
, pmap
, vaddr
,
3658 fault_page_size
, fault_phys_offset
, m
,
3659 &prot
, caller_prot
, fault_type
, wired
, pmap_options
, need_retry
);
3666 vm_pre_fault(vm_map_offset_t vaddr
, vm_prot_t prot
)
3668 if (pmap_find_phys(current_map()->pmap
, vaddr
) == 0) {
3669 vm_fault(current_map(), /* map */
3671 prot
, /* fault_type */
3672 FALSE
, /* change_wiring */
3673 VM_KERN_MEMORY_NONE
, /* tag - not wiring */
3674 THREAD_UNINT
, /* interruptible */
3675 NULL
, /* caller_pmap */
3676 0 /* caller_pmap_addr */);
3684 * Handle page faults, including pseudo-faults
3685 * used to change the wiring status of pages.
3687 * Explicit continuations have been removed.
3689 * vm_fault and vm_fault_page save mucho state
3690 * in the moral equivalent of a closure. The state
3691 * structure is allocated when first entering vm_fault
3692 * and deallocated when leaving vm_fault.
3695 extern uint64_t get_current_unique_pid(void);
3697 unsigned long vm_fault_collapse_total
= 0;
3698 unsigned long vm_fault_collapse_skipped
= 0;
3704 vm_map_offset_t vaddr
,
3705 vm_prot_t fault_type
,
3706 boolean_t change_wiring
,
3709 vm_map_offset_t caller_pmap_addr
)
3711 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
,
3712 change_wiring
? vm_tag_bt() : VM_KERN_MEMORY_NONE
,
3713 interruptible
, caller_pmap
, caller_pmap_addr
,
3720 vm_map_offset_t vaddr
,
3721 vm_prot_t fault_type
,
3722 boolean_t change_wiring
,
3723 vm_tag_t wire_tag
, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3726 vm_map_offset_t caller_pmap_addr
)
3728 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
, wire_tag
,
3729 interruptible
, caller_pmap
, caller_pmap_addr
,
3734 current_proc_is_privileged(void)
3736 return csproc_get_platform_binary(current_proc());
3739 uint64_t vm_copied_on_read
= 0;
3742 * Cleanup after a vm_fault_enter.
3743 * At this point, the fault should either have failed (kr != KERN_SUCCESS)
3744 * or the page should be in the pmap and on the correct paging queue.
3747 * map must be locked shared.
3748 * m_object must be locked.
3749 * If top_object != VM_OBJECT_NULL, it must be locked.
3750 * real_map must be locked.
3753 * map will be unlocked
3754 * m_object will be unlocked
3755 * top_object will be unlocked
3756 * If real_map != map, it will be unlocked
3763 vm_object_t m_object
,
3765 vm_map_offset_t offset
,
3766 vm_map_offset_t trace_real_vaddr
,
3767 vm_object_fault_info_t fault_info
,
3768 vm_prot_t caller_prot
,
3770 vm_map_offset_t real_vaddr
,
3772 __unused vm_map_offset_t real_vaddr
,
3773 #endif /* CONFIG_DTRACE */
3775 boolean_t need_retry
,
3777 ppnum_t
*physpage_p
,
3779 vm_object_t top_object
,
3780 boolean_t need_collapse
,
3781 vm_map_offset_t cur_offset
,
3782 vm_prot_t fault_type
,
3783 vm_object_t
*written_on_object
,
3784 memory_object_t
*written_on_pager
,
3785 vm_object_offset_t
*written_on_offset
)
3788 vm_map_lock_assert_shared(map
);
3789 vm_object_lock_assert_held(m_object
);
3790 if (top_object
!= VM_OBJECT_NULL
) {
3791 vm_object_lock_assert_held(top_object
);
3793 vm_map_lock_assert_held(real_map
);
3795 if (m_object
->internal
) {
3796 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
3797 } else if (m_object
->object_is_shared_cache
) {
3798 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
3800 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
3803 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);
3804 if (need_retry
== FALSE
) {
3805 KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_FAST
), get_current_unique_pid(), 0, 0, 0, 0);
3807 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
);
3808 if (kr
== KERN_SUCCESS
&&
3809 physpage_p
!= NULL
) {
3810 /* for vm_map_wire_and_extract() */
3811 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
3812 if (prot
& VM_PROT_WRITE
) {
3813 vm_object_lock_assert_exclusive(m_object
);
3814 m
->vmp_dirty
= TRUE
;
3818 if (top_object
!= VM_OBJECT_NULL
) {
3820 * It's safe to drop the top object
3821 * now that we've done our
3822 * vm_fault_enter(). Any other fault
3823 * in progress for that virtual
3824 * address will either find our page
3825 * and translation or put in a new page
3828 vm_object_unlock(top_object
);
3829 top_object
= VM_OBJECT_NULL
;
3832 if (need_collapse
== TRUE
) {
3833 vm_object_collapse(object
, vm_object_trunc_page(offset
), TRUE
);
3836 if (need_retry
== FALSE
&&
3837 (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
)) {
3839 * evaluate access pattern and update state
3840 * vm_fault_deactivate_behind depends on the
3841 * state being up to date
3843 vm_fault_is_sequential(m_object
, cur_offset
, fault_info
->behavior
);
3845 vm_fault_deactivate_behind(m_object
, cur_offset
, fault_info
->behavior
);
3848 * That's it, clean up and return.
3851 vm_object_lock_assert_exclusive(m_object
);
3852 PAGE_WAKEUP_DONE(m
);
3855 if (need_retry
== FALSE
&& !m_object
->internal
&& (fault_type
& VM_PROT_WRITE
)) {
3856 vm_object_paging_begin(m_object
);
3858 assert(*written_on_object
== VM_OBJECT_NULL
);
3859 *written_on_object
= m_object
;
3860 *written_on_pager
= m_object
->pager
;
3861 *written_on_offset
= m_object
->paging_offset
+ m
->vmp_offset
;
3863 vm_object_unlock(object
);
3865 vm_map_unlock_read(map
);
3866 if (real_map
!= map
) {
3867 vm_map_unlock(real_map
);
3872 vm_fault_type_for_tracing(boolean_t need_copy_on_read
, int type_of_fault
)
3874 if (need_copy_on_read
&& type_of_fault
== DBG_COW_FAULT
) {
3875 return DBG_COR_FAULT
;
3877 return type_of_fault
;
3883 vm_map_offset_t vaddr
,
3884 vm_prot_t caller_prot
,
3885 boolean_t change_wiring
,
3886 vm_tag_t wire_tag
, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3889 vm_map_offset_t caller_pmap_addr
,
3890 ppnum_t
*physpage_p
)
3892 vm_map_version_t version
; /* Map version for verificiation */
3893 boolean_t wired
; /* Should mapping be wired down? */
3894 vm_object_t object
; /* Top-level object */
3895 vm_object_offset_t offset
; /* Top-level offset */
3896 vm_prot_t prot
; /* Protection for mapping */
3897 vm_object_t old_copy_object
; /* Saved copy object */
3898 vm_page_t result_page
; /* Result of vm_fault_page */
3899 vm_page_t top_page
; /* Placeholder page */
3902 vm_page_t m
; /* Fast access to result_page */
3903 kern_return_t error_code
;
3904 vm_object_t cur_object
;
3905 vm_object_t m_object
= NULL
;
3906 vm_object_offset_t cur_offset
;
3908 vm_object_t new_object
;
3911 wait_interrupt_t interruptible_state
;
3912 vm_map_t real_map
= map
;
3913 vm_map_t original_map
= map
;
3914 bool object_locks_dropped
= FALSE
;
3915 vm_prot_t fault_type
;
3916 vm_prot_t original_fault_type
;
3917 struct vm_object_fault_info fault_info
= {};
3918 bool need_collapse
= FALSE
;
3919 boolean_t need_retry
= FALSE
;
3920 boolean_t
*need_retry_ptr
= NULL
;
3921 uint8_t object_lock_type
= 0;
3922 uint8_t cur_object_lock_type
;
3923 vm_object_t top_object
= VM_OBJECT_NULL
;
3924 vm_object_t written_on_object
= VM_OBJECT_NULL
;
3925 memory_object_t written_on_pager
= NULL
;
3926 vm_object_offset_t written_on_offset
= 0;
3928 int compressed_count_delta
;
3929 uint8_t grab_options
;
3931 bool need_copy_on_read
;
3932 vm_map_offset_t trace_vaddr
;
3933 vm_map_offset_t trace_real_vaddr
;
3934 vm_map_size_t fault_page_size
;
3935 vm_map_size_t fault_page_mask
;
3936 vm_map_offset_t fault_phys_offset
;
3937 vm_map_offset_t real_vaddr
;
3938 bool resilient_media_retry
= FALSE
;
3939 vm_object_t resilient_media_object
= VM_OBJECT_NULL
;
3940 vm_object_offset_t resilient_media_offset
= (vm_object_offset_t
)-1;
3941 bool page_needs_data_sync
= false;
3943 * Was the VM object contended when vm_map_lookup_locked locked it?
3944 * If so, the zero fill path will drop the lock
3945 * NB: Ideally we would always drop the lock rather than rely on
3946 * this heuristic, but vm_object_unlock currently takes > 30 cycles.
3948 bool object_is_contended
= false;
3951 trace_real_vaddr
= vaddr
;
3953 if (VM_MAP_PAGE_SIZE(original_map
) < PAGE_SIZE
) {
3954 fault_phys_offset
= (vm_map_offset_t
)-1;
3955 fault_page_size
= VM_MAP_PAGE_SIZE(original_map
);
3956 fault_page_mask
= VM_MAP_PAGE_MASK(original_map
);
3957 if (fault_page_size
< PAGE_SIZE
) {
3958 DEBUG4K_FAULT("map %p vaddr 0x%llx caller_prot 0x%x\n", map
, (uint64_t)trace_real_vaddr
, caller_prot
);
3959 vaddr
= vm_map_trunc_page(vaddr
, fault_page_mask
);
3962 fault_phys_offset
= 0;
3963 fault_page_size
= PAGE_SIZE
;
3964 fault_page_mask
= PAGE_MASK
;
3965 vaddr
= vm_map_trunc_page(vaddr
, PAGE_MASK
);
3968 if (map
== kernel_map
) {
3969 trace_vaddr
= VM_KERNEL_ADDRHIDE(vaddr
);
3970 trace_real_vaddr
= VM_KERNEL_ADDRHIDE(trace_real_vaddr
);
3972 trace_vaddr
= vaddr
;
3975 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3976 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
3977 ((uint64_t)trace_vaddr
>> 32),
3979 (map
== kernel_map
),
3983 if (get_preemption_level() != 0) {
3984 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3985 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3986 ((uint64_t)trace_vaddr
>> 32),
3992 return KERN_FAILURE
;
3995 thread_t cthread
= current_thread();
3996 bool rtfault
= (cthread
->sched_mode
== TH_MODE_REALTIME
);
3997 uint64_t fstart
= 0;
4000 fstart
= mach_continuous_time();
4003 interruptible_state
= thread_interrupt_level(interruptible
);
4005 fault_type
= (change_wiring
? VM_PROT_NONE
: caller_prot
);
4007 VM_STAT_INCR(faults
);
4008 current_task()->faults
++;
4009 original_fault_type
= fault_type
;
4012 if (fault_type
& VM_PROT_WRITE
) {
4016 if (need_copy
|| change_wiring
) {
4017 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4019 object_lock_type
= OBJECT_LOCK_SHARED
;
4022 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
4024 if ((map
== kernel_map
) && (caller_prot
& VM_PROT_WRITE
)) {
4025 if (compressor_map
) {
4026 if ((vaddr
>= vm_map_min(compressor_map
)) && (vaddr
< vm_map_max(compressor_map
))) {
4027 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
));
4032 assert(written_on_object
== VM_OBJECT_NULL
);
4035 * assume we will hit a page in the cache
4036 * otherwise, explicitly override with
4037 * the real fault type once we determine it
4039 type_of_fault
= DBG_CACHE_HIT_FAULT
;
4042 * Find the backing store object and offset into
4043 * it to begin the search.
4045 fault_type
= original_fault_type
;
4047 vm_map_lock_read(map
);
4049 if (resilient_media_retry
) {
4051 * If we have to insert a fake zero-filled page to hide
4052 * a media failure to provide the real page, we need to
4053 * resolve any pending copy-on-write on this mapping.
4054 * VM_PROT_COPY tells vm_map_lookup_locked() to deal
4055 * with that even if this is not a "write" fault.
4058 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4061 kr
= vm_map_lookup_locked(&map
, vaddr
,
4062 (fault_type
| (need_copy
? VM_PROT_COPY
: 0)),
4063 object_lock_type
, &version
,
4064 &object
, &offset
, &prot
, &wired
,
4067 &object_is_contended
);
4069 if (kr
!= KERN_SUCCESS
) {
4070 vm_map_unlock_read(map
);
4075 pmap
= real_map
->pmap
;
4076 fault_info
.interruptible
= interruptible
;
4077 fault_info
.stealth
= FALSE
;
4078 fault_info
.io_sync
= FALSE
;
4079 fault_info
.mark_zf_absent
= FALSE
;
4080 fault_info
.batch_pmap_op
= FALSE
;
4082 if (resilient_media_retry
) {
4084 * We're retrying this fault after having detected a media
4085 * failure from a "resilient_media" mapping.
4086 * Check that the mapping is still pointing at the object
4087 * that just failed to provide a page.
4089 assert(resilient_media_object
!= VM_OBJECT_NULL
);
4090 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
4091 if (object
!= VM_OBJECT_NULL
&&
4092 object
== resilient_media_object
&&
4093 offset
== resilient_media_offset
&&
4094 fault_info
.resilient_media
) {
4096 * This mapping still points at the same object
4097 * and is still "resilient_media": proceed in
4098 * "recovery-from-media-failure" mode, where we'll
4099 * insert a zero-filled page in the top object.
4101 // printf("RESILIENT_MEDIA %s:%d recovering for object %p offset 0x%llx\n", __FUNCTION__, __LINE__, object, offset);
4103 /* not recovering: reset state */
4104 // printf("RESILIENT_MEDIA %s:%d no recovery resilient %d object %p/%p offset 0x%llx/0x%llx\n", __FUNCTION__, __LINE__, fault_info.resilient_media, object, resilient_media_object, offset, resilient_media_offset);
4105 resilient_media_retry
= FALSE
;
4106 /* release our extra reference on failed object */
4107 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
4108 vm_object_deallocate(resilient_media_object
);
4109 resilient_media_object
= VM_OBJECT_NULL
;
4110 resilient_media_offset
= (vm_object_offset_t
)-1;
4113 assert(resilient_media_object
== VM_OBJECT_NULL
);
4114 resilient_media_offset
= (vm_object_offset_t
)-1;
4118 * If the page is wired, we must fault for the current protection
4119 * value, to avoid further faults.
4122 fault_type
= prot
| VM_PROT_WRITE
;
4124 if (wired
|| need_copy
) {
4126 * since we're treating this fault as a 'write'
4127 * we must hold the top object lock exclusively
4129 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4130 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4132 if (vm_object_lock_upgrade(object
) == FALSE
) {
4134 * couldn't upgrade, so explictly
4135 * take the lock exclusively
4137 vm_object_lock(object
);
4142 #if VM_FAULT_CLASSIFY
4144 * Temporary data gathering code
4146 vm_fault_classify(object
, offset
, fault_type
);
4149 * Fast fault code. The basic idea is to do as much as
4150 * possible while holding the map lock and object locks.
4151 * Busy pages are not used until the object lock has to
4152 * be dropped to do something (copy, zero fill, pmap enter).
4153 * Similarly, paging references aren't acquired until that
4154 * point, and object references aren't used.
4156 * If we can figure out what to do
4157 * (zero fill, copy on write, pmap enter) while holding
4158 * the locks, then it gets done. Otherwise, we give up,
4159 * and use the original fault path (which doesn't hold
4160 * the map lock, and relies on busy pages).
4161 * The give up cases include:
4162 * - Have to talk to pager.
4163 * - Page is busy, absent or in error.
4164 * - Pager has locked out desired access.
4165 * - Fault needs to be restarted.
4166 * - Have to push page into copy object.
4168 * The code is an infinite loop that moves one level down
4169 * the shadow chain each time. cur_object and cur_offset
4170 * refer to the current object being examined. object and offset
4171 * are the original object from the map. The loop is at the
4172 * top level if and only if object and cur_object are the same.
4174 * Invariants: Map lock is held throughout. Lock is held on
4175 * original object and cur_object (if different) when
4176 * continuing or exiting loop.
4180 #if defined(__arm64__)
4182 * Fail if reading an execute-only page in a
4183 * pmap that enforces execute-only protection.
4185 if (fault_type
== VM_PROT_READ
&&
4186 (prot
& VM_PROT_EXECUTE
) &&
4187 !(prot
& VM_PROT_READ
) &&
4188 pmap_enforces_execute_only(pmap
)) {
4189 vm_object_unlock(object
);
4190 vm_map_unlock_read(map
);
4191 if (real_map
!= map
) {
4192 vm_map_unlock(real_map
);
4194 kr
= KERN_PROTECTION_FAILURE
;
4199 fault_phys_offset
= (vm_map_offset_t
)offset
- vm_map_trunc_page((vm_map_offset_t
)offset
, PAGE_MASK
);
4202 * If this page is to be inserted in a copy delay object
4203 * for writing, and if the object has a copy, then the
4204 * copy delay strategy is implemented in the slow fault page.
4206 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
4207 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
)) {
4208 goto handle_copy_delay
;
4211 cur_object
= object
;
4212 cur_offset
= offset
;
4215 #if CONFIG_SECLUDED_MEMORY
4216 if (object
->can_grab_secluded
) {
4217 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
4219 #endif /* CONFIG_SECLUDED_MEMORY */
4222 if (!cur_object
->pager_created
&&
4223 cur_object
->phys_contiguous
) { /* superpage */
4227 if (cur_object
->blocked_access
) {
4229 * Access to this VM object has been blocked.
4230 * Let the slow path handle it.
4235 m
= vm_page_lookup(cur_object
, vm_object_trunc_page(cur_offset
));
4238 if (m
!= VM_PAGE_NULL
) {
4239 m_object
= cur_object
;
4242 wait_result_t result
;
4245 * in order to do the PAGE_ASSERT_WAIT, we must
4246 * have object that 'm' belongs to locked exclusively
4248 if (object
!= cur_object
) {
4249 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4250 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4252 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
4254 * couldn't upgrade so go do a full retry
4255 * immediately since we can no longer be
4256 * certain about cur_object (since we
4257 * don't hold a reference on it)...
4258 * first drop the top object lock
4260 vm_object_unlock(object
);
4262 vm_map_unlock_read(map
);
4263 if (real_map
!= map
) {
4264 vm_map_unlock(real_map
);
4270 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4271 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4273 if (vm_object_lock_upgrade(object
) == FALSE
) {
4275 * couldn't upgrade, so explictly take the lock
4276 * exclusively and go relookup the page since we
4277 * will have dropped the object lock and
4278 * a different thread could have inserted
4279 * a page at this offset
4280 * no need for a full retry since we're
4281 * at the top level of the object chain
4283 vm_object_lock(object
);
4288 if ((m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) && m_object
->internal
) {
4290 * m->vmp_busy == TRUE and the object is locked exclusively
4291 * if m->pageout_queue == TRUE after we acquire the
4292 * queues lock, we are guaranteed that it is stable on
4293 * the pageout queue and therefore reclaimable
4295 * NOTE: this is only true for the internal pageout queue
4296 * in the compressor world
4298 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
4300 vm_page_lock_queues();
4302 if (m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) {
4303 vm_pageout_throttle_up(m
);
4304 vm_page_unlock_queues();
4306 PAGE_WAKEUP_DONE(m
);
4307 goto reclaimed_from_pageout
;
4309 vm_page_unlock_queues();
4311 if (object
!= cur_object
) {
4312 vm_object_unlock(object
);
4315 vm_map_unlock_read(map
);
4316 if (real_map
!= map
) {
4317 vm_map_unlock(real_map
);
4320 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
4322 vm_object_unlock(cur_object
);
4324 if (result
== THREAD_WAITING
) {
4325 result
= thread_block(THREAD_CONTINUE_NULL
);
4327 counter(c_vm_fault_page_block_busy_kernel
++);
4329 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
) {
4336 reclaimed_from_pageout
:
4337 if (m
->vmp_laundry
) {
4338 if (object
!= cur_object
) {
4339 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4340 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4342 vm_object_unlock(object
);
4343 vm_object_unlock(cur_object
);
4345 vm_map_unlock_read(map
);
4346 if (real_map
!= map
) {
4347 vm_map_unlock(real_map
);
4352 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4353 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4355 if (vm_object_lock_upgrade(object
) == FALSE
) {
4357 * couldn't upgrade, so explictly take the lock
4358 * exclusively and go relookup the page since we
4359 * will have dropped the object lock and
4360 * a different thread could have inserted
4361 * a page at this offset
4362 * no need for a full retry since we're
4363 * at the top level of the object chain
4365 vm_object_lock(object
);
4370 vm_pageout_steal_laundry(m
, FALSE
);
4373 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
4375 * Guard page: let the slow path deal with it
4379 if (m
->vmp_unusual
&& (m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_private
|| m
->vmp_absent
)) {
4381 * Unusual case... let the slow path deal with it
4385 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object
)) {
4386 if (object
!= cur_object
) {
4387 vm_object_unlock(object
);
4389 vm_map_unlock_read(map
);
4390 if (real_map
!= map
) {
4391 vm_map_unlock(real_map
);
4393 vm_object_unlock(cur_object
);
4394 kr
= KERN_MEMORY_ERROR
;
4397 assert(m_object
== VM_PAGE_OBJECT(m
));
4399 if (vm_fault_cs_need_validation(map
->pmap
, m
, m_object
,
4401 (physpage_p
!= NULL
&& (prot
& VM_PROT_WRITE
))) {
4402 upgrade_lock_and_retry
:
4404 * We might need to validate this page
4405 * against its code signature, so we
4406 * want to hold the VM object exclusively.
4408 if (object
!= cur_object
) {
4409 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4410 vm_object_unlock(object
);
4411 vm_object_unlock(cur_object
);
4413 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4415 vm_map_unlock_read(map
);
4416 if (real_map
!= map
) {
4417 vm_map_unlock(real_map
);
4422 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4423 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4425 if (vm_object_lock_upgrade(object
) == FALSE
) {
4427 * couldn't upgrade, so explictly take the lock
4428 * exclusively and go relookup the page since we
4429 * will have dropped the object lock and
4430 * a different thread could have inserted
4431 * a page at this offset
4432 * no need for a full retry since we're
4433 * at the top level of the object chain
4435 vm_object_lock(object
);
4442 * Two cases of map in faults:
4443 * - At top level w/o copy object.
4444 * - Read fault anywhere.
4445 * --> must disallow write.
4448 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
4453 !fault_info
.no_copy_on_read
&&
4454 cur_object
!= object
&&
4455 !cur_object
->internal
&&
4456 !cur_object
->pager_trusted
&&
4457 vm_protect_privileged_from_untrusted
&&
4458 !((prot
& VM_PROT_EXECUTE
) &&
4459 cur_object
->code_signed
&&
4460 pmap_get_vm_map_cs_enforced(caller_pmap
? caller_pmap
: pmap
)) &&
4461 current_proc_is_privileged()) {
4463 * We're faulting on a page in "object" and
4464 * went down the shadow chain to "cur_object"
4465 * to find out that "cur_object"'s pager
4466 * is not "trusted", i.e. we can not trust it
4467 * to always return the same contents.
4468 * Since the target is a "privileged" process,
4469 * let's treat this as a copy-on-read fault, as
4470 * if it was a copy-on-write fault.
4471 * Once "object" gets a copy of this page, it
4472 * won't have to rely on "cur_object" to
4473 * provide the contents again.
4475 * This is done by setting "need_copy" and
4476 * retrying the fault from the top with the
4477 * appropriate locking.
4479 * Special case: if the mapping is executable
4480 * and the untrusted object is code-signed and
4481 * the process is "cs_enforced", we do not
4482 * copy-on-read because that would break
4483 * code-signing enforcement expectations (an
4484 * executable page must belong to a code-signed
4485 * object) and we can rely on code-signing
4486 * to re-validate the page if it gets evicted
4487 * and paged back in.
4489 // printf("COPY-ON-READ %s:%d map %p va 0x%llx page %p object %p offset 0x%llx UNTRUSTED: need copy-on-read!\n", __FUNCTION__, __LINE__, map, (uint64_t)vaddr, m, VM_PAGE_OBJECT(m), m->vmp_offset);
4490 vm_copied_on_read
++;
4493 vm_object_unlock(object
);
4494 vm_object_unlock(cur_object
);
4495 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4496 vm_map_unlock_read(map
);
4497 if (real_map
!= map
) {
4498 vm_map_unlock(real_map
);
4503 if (!(fault_type
& VM_PROT_WRITE
) && !need_copy
) {
4504 if (!pmap_has_prot_policy(pmap
, fault_info
.pmap_options
& PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE
, prot
)) {
4505 prot
&= ~VM_PROT_WRITE
;
4508 * For a protection that the pmap cares
4509 * about, we must hand over the full
4510 * set of protections (so that the pmap
4511 * layer can apply any desired policy).
4512 * This means that cs_bypass must be
4513 * set, as this can force us to pass
4516 assert(fault_info
.cs_bypass
);
4519 if (object
!= cur_object
) {
4521 * We still need to hold the top object
4522 * lock here to prevent a race between
4523 * a read fault (taking only "shared"
4524 * locks) and a write fault (taking
4525 * an "exclusive" lock on the top
4527 * Otherwise, as soon as we release the
4528 * top lock, the write fault could
4529 * proceed and actually complete before
4530 * the read fault, and the copied page's
4531 * translation could then be overwritten
4532 * by the read fault's translation for
4533 * the original page.
4535 * Let's just record what the top object
4536 * is and we'll release it later.
4538 top_object
= object
;
4541 * switch to the object that has the new page
4543 object
= cur_object
;
4544 object_lock_type
= cur_object_lock_type
;
4547 assert(m_object
== VM_PAGE_OBJECT(m
));
4550 * prepare for the pmap_enter...
4551 * object and map are both locked
4552 * m contains valid data
4553 * object == m->vmp_object
4554 * cur_object == NULL or it's been unlocked
4555 * no paging references on either object or cur_object
4557 if (top_object
!= VM_OBJECT_NULL
|| object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4558 need_retry_ptr
= &need_retry
;
4560 need_retry_ptr
= NULL
;
4563 if (fault_page_size
< PAGE_SIZE
) {
4564 DEBUG4K_FAULT("map %p original %p pmap %p va 0x%llx caller pmap %p va 0x%llx pa 0x%llx (0x%llx+0x%llx) prot 0x%x caller_prot 0x%x\n", map
, original_map
, pmap
, (uint64_t)vaddr
, caller_pmap
, (uint64_t)caller_pmap_addr
, (uint64_t)((((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(m
)) << PAGE_SHIFT
) + fault_phys_offset
), (uint64_t)(((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(m
)) << PAGE_SHIFT
), (uint64_t)fault_phys_offset
, prot
, caller_prot
);
4565 assertf((!(fault_phys_offset
& FOURK_PAGE_MASK
) &&
4566 fault_phys_offset
< PAGE_SIZE
),
4567 "0x%llx\n", (uint64_t)fault_phys_offset
);
4569 assertf(fault_phys_offset
== 0,
4570 "0x%llx\n", (uint64_t)fault_phys_offset
);
4574 kr
= vm_fault_enter(m
,
4588 kr
= vm_fault_enter(m
,
4614 vm_fault_type_for_tracing(need_copy_on_read
, type_of_fault
),
4625 &written_on_offset
);
4626 top_object
= VM_OBJECT_NULL
;
4627 if (need_retry
== TRUE
) {
4629 * vm_fault_enter couldn't complete the PMAP_ENTER...
4630 * at this point we don't hold any locks so it's safe
4631 * to ask the pmap layer to expand the page table to
4632 * accommodate this mapping... once expanded, we'll
4633 * re-drive the fault which should result in vm_fault_enter
4634 * being able to successfully enter the mapping this time around
4636 (void)pmap_enter_options(
4637 pmap
, vaddr
, 0, 0, 0, 0, 0,
4638 PMAP_OPTIONS_NOENTER
, NULL
);
4646 * COPY ON WRITE FAULT
4648 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
4651 * If objects match, then
4652 * object->copy must not be NULL (else control
4653 * would be in previous code block), and we
4654 * have a potential push into the copy object
4655 * with which we can't cope with here.
4657 if (cur_object
== object
) {
4659 * must take the slow path to
4660 * deal with the copy push
4666 * This is now a shadow based copy on write
4667 * fault -- it requires a copy up the shadow
4670 assert(m_object
== VM_PAGE_OBJECT(m
));
4672 if ((cur_object_lock_type
== OBJECT_LOCK_SHARED
) &&
4673 vm_fault_cs_need_validation(NULL
, m
, m_object
,
4675 goto upgrade_lock_and_retry
;
4679 * Allocate a page in the original top level
4680 * object. Give up if allocate fails. Also
4681 * need to remember current page, as it's the
4682 * source of the copy.
4684 * at this point we hold locks on both
4685 * object and cur_object... no need to take
4686 * paging refs or mark pages BUSY since
4687 * we don't drop either object lock until
4688 * the page has been copied and inserted
4691 m
= vm_page_grab_options(grab_options
);
4694 if (m
== VM_PAGE_NULL
) {
4696 * no free page currently available...
4697 * must take the slow path
4702 * Now do the copy. Mark the source page busy...
4704 * NOTE: This code holds the map lock across
4707 vm_page_copy(cur_m
, m
);
4708 vm_page_insert(m
, object
, vm_object_trunc_page(offset
));
4709 if (VM_MAP_PAGE_MASK(map
) != PAGE_MASK
) {
4710 DEBUG4K_FAULT("map %p vaddr 0x%llx page %p [%p 0x%llx] copied to %p [%p 0x%llx]\n", map
, (uint64_t)vaddr
, cur_m
, VM_PAGE_OBJECT(cur_m
), cur_m
->vmp_offset
, m
, VM_PAGE_OBJECT(m
), m
->vmp_offset
);
4713 SET_PAGE_DIRTY(m
, FALSE
);
4716 * Now cope with the source page and object
4718 if (object
->ref_count
> 1 && cur_m
->vmp_pmapped
) {
4719 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m
));
4720 } else if (VM_MAP_PAGE_SIZE(map
) < PAGE_SIZE
) {
4722 * We've copied the full 16K page but we're
4723 * about to call vm_fault_enter() only for
4724 * the 4K chunk we're faulting on. The other
4725 * three 4K chunks in that page could still
4726 * be pmapped in this pmap.
4727 * Since the VM object layer thinks that the
4728 * entire page has been dealt with and the
4729 * original page might no longer be needed,
4730 * it might collapse/bypass the original VM
4731 * object and free its pages, which would be
4732 * bad (and would trigger pmap_verify_free()
4733 * assertions) if the other 4K chunks are still
4737 * XXX FBDP TODO4K: to be revisisted
4738 * Technically, we need to pmap_disconnect()
4739 * only the target pmap's mappings for the 4K
4740 * chunks of this 16K VM page. If other pmaps
4741 * have PTEs on these chunks, that means that
4742 * the associated VM map must have a reference
4743 * on the VM object, so no need to worry about
4745 * pmap_protect() for each 4K chunk would be
4746 * better but we'd have to check which chunks
4747 * are actually mapped before and after this
4749 * A full-blown pmap_disconnect() is easier
4750 * for now but not efficient.
4752 DEBUG4K_FAULT("pmap_disconnect() page %p object %p offset 0x%llx phys 0x%x\n", cur_m
, VM_PAGE_OBJECT(cur_m
), cur_m
->vmp_offset
, VM_PAGE_GET_PHYS_PAGE(cur_m
));
4753 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m
));
4756 if (cur_m
->vmp_clustered
) {
4757 VM_PAGE_COUNT_AS_PAGEIN(cur_m
);
4758 VM_PAGE_CONSUME_CLUSTERED(cur_m
);
4759 vm_fault_is_sequential(cur_object
, cur_offset
, fault_info
.behavior
);
4761 need_collapse
= TRUE
;
4763 if (!cur_object
->internal
&&
4764 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
4766 * The object from which we've just
4767 * copied a page is most probably backed
4768 * by a vnode. We don't want to waste too
4769 * much time trying to collapse the VM objects
4770 * and create a bottleneck when several tasks
4771 * map the same file.
4773 if (cur_object
->copy
== object
) {
4775 * Shared mapping or no COW yet.
4776 * We can never collapse a copy
4777 * object into its backing object.
4779 need_collapse
= FALSE
;
4780 } else if (cur_object
->copy
== object
->shadow
&&
4781 object
->shadow
->resident_page_count
== 0) {
4783 * Shared mapping after a COW occurred.
4785 need_collapse
= FALSE
;
4788 vm_object_unlock(cur_object
);
4790 if (need_collapse
== FALSE
) {
4791 vm_fault_collapse_skipped
++;
4793 vm_fault_collapse_total
++;
4795 type_of_fault
= DBG_COW_FAULT
;
4796 VM_STAT_INCR(cow_faults
);
4797 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
4798 current_task()->cow_faults
++;
4803 * No page at cur_object, cur_offset... m == NULL
4805 if (cur_object
->pager_created
) {
4806 vm_external_state_t compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
4808 if (MUST_ASK_PAGER(cur_object
, cur_offset
, compressor_external_state
) == TRUE
) {
4810 uint8_t c_flags
= C_DONT_BLOCK
;
4811 bool insert_cur_object
= FALSE
;
4814 * May have to talk to a pager...
4815 * if so, take the slow path by
4816 * doing a 'break' from the while (TRUE) loop
4818 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
4819 * if the compressor is active and the page exists there
4821 if (compressor_external_state
!= VM_EXTERNAL_STATE_EXISTS
) {
4825 if (map
== kernel_map
|| real_map
== kernel_map
) {
4827 * can't call into the compressor with the kernel_map
4828 * lock held, since the compressor may try to operate
4829 * on the kernel map in order to return an empty c_segment
4833 if (object
!= cur_object
) {
4834 if (fault_type
& VM_PROT_WRITE
) {
4837 insert_cur_object
= TRUE
;
4840 if (insert_cur_object
== TRUE
) {
4841 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4842 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4844 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
4846 * couldn't upgrade so go do a full retry
4847 * immediately since we can no longer be
4848 * certain about cur_object (since we
4849 * don't hold a reference on it)...
4850 * first drop the top object lock
4852 vm_object_unlock(object
);
4854 vm_map_unlock_read(map
);
4855 if (real_map
!= map
) {
4856 vm_map_unlock(real_map
);
4862 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4863 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4865 if (object
!= cur_object
) {
4867 * we can't go for the upgrade on the top
4868 * lock since the upgrade may block waiting
4869 * for readers to drain... since we hold
4870 * cur_object locked at this point, waiting
4871 * for the readers to drain would represent
4872 * a lock order inversion since the lock order
4873 * for objects is the reference order in the
4876 vm_object_unlock(object
);
4877 vm_object_unlock(cur_object
);
4879 vm_map_unlock_read(map
);
4880 if (real_map
!= map
) {
4881 vm_map_unlock(real_map
);
4886 if (vm_object_lock_upgrade(object
) == FALSE
) {
4888 * couldn't upgrade, so explictly take the lock
4889 * exclusively and go relookup the page since we
4890 * will have dropped the object lock and
4891 * a different thread could have inserted
4892 * a page at this offset
4893 * no need for a full retry since we're
4894 * at the top level of the object chain
4896 vm_object_lock(object
);
4901 m
= vm_page_grab_options(grab_options
);
4904 if (m
== VM_PAGE_NULL
) {
4906 * no free page currently available...
4907 * must take the slow path
4913 * The object is and remains locked
4914 * so no need to take a
4915 * "paging_in_progress" reference.
4918 if ((object
== cur_object
&&
4919 object_lock_type
== OBJECT_LOCK_EXCLUSIVE
) ||
4920 (object
!= cur_object
&&
4921 cur_object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)) {
4922 shared_lock
= FALSE
;
4927 kr
= vm_compressor_pager_get(
4929 (vm_object_trunc_page(cur_offset
)
4930 + cur_object
->paging_offset
),
4931 VM_PAGE_GET_PHYS_PAGE(m
),
4934 &compressed_count_delta
);
4936 vm_compressor_pager_count(
4938 compressed_count_delta
,
4942 if (kr
!= KERN_SUCCESS
) {
4943 vm_page_release(m
, FALSE
);
4947 * If vm_compressor_pager_get() returns
4948 * KERN_MEMORY_FAILURE, then the
4949 * compressed data is permanently lost,
4950 * so return this error immediately.
4952 if (kr
== KERN_MEMORY_FAILURE
) {
4953 if (object
!= cur_object
) {
4954 vm_object_unlock(cur_object
);
4956 vm_object_unlock(object
);
4957 vm_map_unlock_read(map
);
4958 if (real_map
!= map
) {
4959 vm_map_unlock(real_map
);
4962 } else if (kr
!= KERN_SUCCESS
) {
4965 m
->vmp_dirty
= TRUE
;
4968 * If the object is purgeable, its
4969 * owner's purgeable ledgers will be
4970 * updated in vm_page_insert() but the
4971 * page was also accounted for in a
4972 * "compressed purgeable" ledger, so
4975 if (object
!= cur_object
&&
4976 !insert_cur_object
) {
4978 * We're not going to insert
4979 * the decompressed page into
4980 * the object it came from.
4982 * We're dealing with a
4983 * copy-on-write fault on
4985 * We're going to decompress
4986 * the page directly into the
4987 * target "object" while
4988 * keepin the compressed
4989 * page for "cur_object", so
4990 * no ledger update in that
4993 } else if (((cur_object
->purgable
==
4994 VM_PURGABLE_DENY
) &&
4995 (!cur_object
->vo_ledger_tag
)) ||
4996 (cur_object
->vo_owner
==
4999 * "cur_object" is not purgeable
5000 * and is not ledger-taged, or
5001 * there's no owner for it,
5002 * so no owner's ledgers to
5007 * One less compressed
5008 * purgeable/tagged page for
5009 * cur_object's owner.
5011 vm_object_owner_compressed_update(
5016 if (insert_cur_object
) {
5017 vm_page_insert(m
, cur_object
, vm_object_trunc_page(cur_offset
));
5018 m_object
= cur_object
;
5020 vm_page_insert(m
, object
, vm_object_trunc_page(offset
));
5024 if ((m_object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_USE_DEFAULT
) {
5026 * If the page is not cacheable,
5027 * we can't let its contents
5028 * linger in the data cache
5029 * after the decompression.
5031 pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m
));
5034 type_of_fault
= my_fault_type
;
5036 VM_STAT_DECOMPRESSIONS();
5038 if (cur_object
!= object
) {
5039 if (insert_cur_object
) {
5040 top_object
= object
;
5042 * switch to the object that has the new page
5044 object
= cur_object
;
5045 object_lock_type
= cur_object_lock_type
;
5047 vm_object_unlock(cur_object
);
5048 cur_object
= object
;
5054 * existence map present and indicates
5055 * that the pager doesn't have this page
5058 if (cur_object
->shadow
== VM_OBJECT_NULL
||
5059 resilient_media_retry
) {
5061 * Zero fill fault. Page gets
5062 * inserted into the original object.
5064 if (cur_object
->shadow_severed
||
5065 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
) ||
5066 cur_object
== compressor_object
||
5067 cur_object
== kernel_object
||
5068 cur_object
== vm_submap_object
) {
5069 if (object
!= cur_object
) {
5070 vm_object_unlock(cur_object
);
5072 vm_object_unlock(object
);
5074 vm_map_unlock_read(map
);
5075 if (real_map
!= map
) {
5076 vm_map_unlock(real_map
);
5079 kr
= KERN_MEMORY_ERROR
;
5082 if (cur_object
!= object
) {
5083 vm_object_unlock(cur_object
);
5085 cur_object
= object
;
5087 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
5088 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5090 if (vm_object_lock_upgrade(object
) == FALSE
) {
5092 * couldn't upgrade so do a full retry on the fault
5093 * since we dropped the object lock which
5094 * could allow another thread to insert
5095 * a page at this offset
5097 vm_map_unlock_read(map
);
5098 if (real_map
!= map
) {
5099 vm_map_unlock(real_map
);
5105 if (!object
->internal
) {
5106 panic("%s:%d should not zero-fill page at offset 0x%llx in external object %p", __FUNCTION__
, __LINE__
, (uint64_t)offset
, object
);
5108 m
= vm_page_alloc(object
, vm_object_trunc_page(offset
));
5111 if (m
== VM_PAGE_NULL
) {
5113 * no free page currently available...
5114 * must take the slow path
5121 * Zeroing the page and entering into it into the pmap
5122 * represents a significant amount of the zero fill fault handler's work.
5124 * To improve fault scalability, we'll drop the object lock, if it appears contended,
5125 * now that we've inserted the page into the vm object.
5126 * Before dropping the lock, we need to check protection bits and set the
5127 * mapped bits on the page. Then we can mark the page busy, drop the lock,
5128 * zero it, and do the pmap enter. We'll need to reacquire the lock
5129 * to clear the busy bit and wake up any waiters.
5131 vm_fault_cs_clear(m
);
5132 m
->vmp_pmapped
= TRUE
;
5133 if (map
->no_zero_fill
) {
5134 type_of_fault
= DBG_NZF_PAGE_FAULT
;
5136 type_of_fault
= DBG_ZERO_FILL_FAULT
;
5139 pmap_t destination_pmap
;
5140 vm_map_offset_t destination_pmap_vaddr
;
5141 vm_prot_t enter_fault_type
;
5143 destination_pmap
= caller_pmap
;
5144 destination_pmap_vaddr
= caller_pmap_addr
;
5146 destination_pmap
= pmap
;
5147 destination_pmap_vaddr
= vaddr
;
5149 if (change_wiring
) {
5150 enter_fault_type
= VM_PROT_NONE
;
5152 enter_fault_type
= caller_prot
;
5154 kr
= vm_fault_enter_prepare(m
,
5156 destination_pmap_vaddr
,
5165 &page_needs_data_sync
);
5166 if (kr
!= KERN_SUCCESS
) {
5167 goto zero_fill_cleanup
;
5170 if (object_is_contended
) {
5172 * At this point the page is in the vm object, but not on a paging queue.
5173 * Since it's accessible to another thread but its contents are invalid
5174 * (it hasn't been zeroed) mark it busy before dropping the object lock.
5177 vm_object_unlock(object
);
5179 if (type_of_fault
== DBG_ZERO_FILL_FAULT
) {
5181 * Now zero fill page...
5182 * the page is probably going to
5183 * be written soon, so don't bother
5184 * to clear the modified bit
5186 * NOTE: This code holds the map
5187 * lock across the zero fill.
5189 vm_page_zero_fill(m
);
5190 VM_STAT_INCR(zero_fill_count
);
5191 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
5193 if (page_needs_data_sync
) {
5194 pmap_sync_page_data_phys(VM_PAGE_GET_PHYS_PAGE(m
));
5197 if (top_object
!= VM_OBJECT_NULL
) {
5198 need_retry_ptr
= &need_retry
;
5200 need_retry_ptr
= NULL
;
5202 if (object_is_contended
) {
5203 kr
= vm_fault_pmap_enter(destination_pmap
, destination_pmap_vaddr
,
5204 fault_page_size
, fault_phys_offset
,
5205 m
, &prot
, caller_prot
, enter_fault_type
, wired
,
5206 fault_info
.pmap_options
, need_retry_ptr
);
5207 vm_object_lock(object
);
5209 kr
= vm_fault_pmap_enter_with_object_lock(object
, destination_pmap
, destination_pmap_vaddr
,
5210 fault_page_size
, fault_phys_offset
,
5211 m
, &prot
, caller_prot
, enter_fault_type
, wired
,
5212 fault_info
.pmap_options
, need_retry_ptr
);
5216 if (!VM_DYNAMIC_PAGING_ENABLED() &&
5217 (object
->purgable
== VM_PURGABLE_DENY
||
5218 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
5219 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
5220 vm_page_lockspin_queues();
5221 if (!VM_DYNAMIC_PAGING_ENABLED()) {
5222 vm_fault_enqueue_throttled_locked(m
);
5224 vm_page_unlock_queues();
5226 vm_fault_enqueue_page(object
, m
, wired
, change_wiring
, wire_tag
, fault_info
.no_cache
, &type_of_fault
, kr
);
5250 &written_on_offset
);
5251 top_object
= VM_OBJECT_NULL
;
5252 if (need_retry
== TRUE
) {
5254 * vm_fault_enter couldn't complete the PMAP_ENTER...
5255 * at this point we don't hold any locks so it's safe
5256 * to ask the pmap layer to expand the page table to
5257 * accommodate this mapping... once expanded, we'll
5258 * re-drive the fault which should result in vm_fault_enter
5259 * being able to successfully enter the mapping this time around
5261 (void)pmap_enter_options(
5262 pmap
, vaddr
, 0, 0, 0, 0, 0,
5263 PMAP_OPTIONS_NOENTER
, NULL
);
5271 * On to the next level in the shadow chain
5273 cur_offset
+= cur_object
->vo_shadow_offset
;
5274 new_object
= cur_object
->shadow
;
5275 fault_phys_offset
= cur_offset
- vm_object_trunc_page(cur_offset
);
5278 * take the new_object's lock with the indicated state
5280 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
5281 vm_object_lock_shared(new_object
);
5283 vm_object_lock(new_object
);
5286 if (cur_object
!= object
) {
5287 vm_object_unlock(cur_object
);
5290 cur_object
= new_object
;
5296 * Cleanup from fast fault failure. Drop any object
5297 * lock other than original and drop map lock.
5299 if (object
!= cur_object
) {
5300 vm_object_unlock(cur_object
);
5304 * must own the object lock exclusively at this point
5306 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
5307 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5309 if (vm_object_lock_upgrade(object
) == FALSE
) {
5311 * couldn't upgrade, so explictly
5312 * take the lock exclusively
5313 * no need to retry the fault at this
5314 * point since "vm_fault_page" will
5315 * completely re-evaluate the state
5317 vm_object_lock(object
);
5322 vm_map_unlock_read(map
);
5323 if (real_map
!= map
) {
5324 vm_map_unlock(real_map
);
5327 if (__improbable(object
== compressor_object
||
5328 object
== kernel_object
||
5329 object
== vm_submap_object
)) {
5331 * These objects are explicitly managed and populated by the
5332 * kernel. The virtual ranges backed by these objects should
5333 * either have wired pages or "holes" that are not supposed to
5334 * be accessed at all until they get explicitly populated.
5335 * We should never have to resolve a fault on a mapping backed
5336 * by one of these VM objects and providing a zero-filled page
5337 * would be wrong here, so let's fail the fault and let the
5338 * caller crash or recover.
5340 vm_object_unlock(object
);
5341 kr
= KERN_MEMORY_ERROR
;
5345 assert(object
!= compressor_object
);
5346 assert(object
!= kernel_object
);
5347 assert(object
!= vm_submap_object
);
5349 if (resilient_media_retry
) {
5351 * We could get here if we failed to get a free page
5352 * to zero-fill and had to take the slow path again.
5353 * Reset our "recovery-from-failed-media" state.
5355 assert(resilient_media_object
!= VM_OBJECT_NULL
);
5356 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
5357 /* release our extra reference on failed object */
5358 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
5359 vm_object_deallocate(resilient_media_object
);
5360 resilient_media_object
= VM_OBJECT_NULL
;
5361 resilient_media_offset
= (vm_object_offset_t
)-1;
5362 resilient_media_retry
= FALSE
;
5366 * Make a reference to this object to
5367 * prevent its disposal while we are messing with
5368 * it. Once we have the reference, the map is free
5369 * to be diddled. Since objects reference their
5370 * shadows (and copies), they will stay around as well.
5372 vm_object_reference_locked(object
);
5373 vm_object_paging_begin(object
);
5375 set_thread_pagein_error(cthread
, 0);
5378 result_page
= VM_PAGE_NULL
;
5379 kr
= vm_fault_page(object
, offset
, fault_type
,
5380 (change_wiring
&& !wired
),
5381 FALSE
, /* page not looked up */
5382 &prot
, &result_page
, &top_page
,
5384 &error_code
, map
->no_zero_fill
,
5385 FALSE
, &fault_info
);
5388 * if kr != VM_FAULT_SUCCESS, then the paging reference
5389 * has been dropped and the object unlocked... the ref_count
5392 * if kr == VM_FAULT_SUCCESS, then the paging reference
5393 * is still held along with the ref_count on the original object
5395 * the object is returned locked with a paging reference
5397 * if top_page != NULL, then it's BUSY and the
5398 * object it belongs to has a paging reference
5399 * but is returned unlocked
5401 if (kr
!= VM_FAULT_SUCCESS
&&
5402 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
5403 if (kr
== VM_FAULT_MEMORY_ERROR
&&
5404 fault_info
.resilient_media
) {
5405 assertf(object
->internal
, "object %p", object
);
5407 * This fault failed but the mapping was
5408 * "media resilient", so we'll retry the fault in
5409 * recovery mode to get a zero-filled page in the
5411 * Keep the reference on the failing object so
5412 * that we can check that the mapping is still
5413 * pointing to it when we retry the fault.
5415 // printf("RESILIENT_MEDIA %s:%d: object %p offset 0x%llx recover from media error 0x%x kr 0x%x top_page %p result_page %p\n", __FUNCTION__, __LINE__, object, offset, error_code, kr, top_page, result_page);
5416 assert(!resilient_media_retry
); /* no double retry */
5417 assert(resilient_media_object
== VM_OBJECT_NULL
);
5418 assert(resilient_media_offset
== (vm_object_offset_t
)-1);
5419 resilient_media_retry
= TRUE
;
5420 resilient_media_object
= object
;
5421 resilient_media_offset
= offset
;
5422 // printf("FBDP %s:%d resilient_media_object %p offset 0x%llx kept reference\n", __FUNCTION__, __LINE__, resilient_media_object, resilient_mmedia_offset);
5426 * we didn't succeed, lose the object reference
5429 vm_object_deallocate(object
);
5430 object
= VM_OBJECT_NULL
; /* no longer valid */
5434 * See why we failed, and take corrective action.
5437 case VM_FAULT_MEMORY_SHORTAGE
:
5438 if (vm_page_wait((change_wiring
) ?
5440 THREAD_ABORTSAFE
)) {
5444 case VM_FAULT_INTERRUPTED
:
5447 case VM_FAULT_RETRY
:
5449 case VM_FAULT_MEMORY_ERROR
:
5453 kr
= KERN_MEMORY_ERROR
;
5457 panic("vm_fault: unexpected error 0x%x from "
5458 "vm_fault_page()\n", kr
);
5464 if (m
!= VM_PAGE_NULL
) {
5465 m_object
= VM_PAGE_OBJECT(m
);
5466 assert((change_wiring
&& !wired
) ?
5467 (top_page
== VM_PAGE_NULL
) :
5468 ((top_page
== VM_PAGE_NULL
) == (m_object
== object
)));
5472 * What to do with the resulting page from vm_fault_page
5473 * if it doesn't get entered into the physical map:
5475 #define RELEASE_PAGE(m) \
5477 PAGE_WAKEUP_DONE(m); \
5478 if ( !VM_PAGE_PAGEABLE(m)) { \
5479 vm_page_lockspin_queues(); \
5480 if ( !VM_PAGE_PAGEABLE(m)) \
5481 vm_page_activate(m); \
5482 vm_page_unlock_queues(); \
5487 object_locks_dropped
= FALSE
;
5489 * We must verify that the maps have not changed
5490 * since our last lookup. vm_map_verify() needs the
5491 * map lock (shared) but we are holding object locks.
5492 * So we do a try_lock() first and, if that fails, we
5493 * drop the object locks and go in for the map lock again.
5495 if (!vm_map_try_lock_read(original_map
)) {
5496 if (m
!= VM_PAGE_NULL
) {
5497 old_copy_object
= m_object
->copy
;
5498 vm_object_unlock(m_object
);
5500 old_copy_object
= VM_OBJECT_NULL
;
5501 vm_object_unlock(object
);
5504 object_locks_dropped
= TRUE
;
5506 vm_map_lock_read(original_map
);
5509 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
5510 if (object_locks_dropped
== FALSE
) {
5511 if (m
!= VM_PAGE_NULL
) {
5512 old_copy_object
= m_object
->copy
;
5513 vm_object_unlock(m_object
);
5515 old_copy_object
= VM_OBJECT_NULL
;
5516 vm_object_unlock(object
);
5519 object_locks_dropped
= TRUE
;
5523 * no object locks are held at this point
5525 vm_object_t retry_object
;
5526 vm_object_offset_t retry_offset
;
5527 vm_prot_t retry_prot
;
5530 * To avoid trying to write_lock the map while another
5531 * thread has it read_locked (in vm_map_pageable), we
5532 * do not try for write permission. If the page is
5533 * still writable, we will get write permission. If it
5534 * is not, or has been marked needs_copy, we enter the
5535 * mapping without write permission, and will merely
5536 * take another fault.
5540 kr
= vm_map_lookup_locked(&map
, vaddr
,
5541 fault_type
& ~VM_PROT_WRITE
,
5542 OBJECT_LOCK_EXCLUSIVE
, &version
,
5543 &retry_object
, &retry_offset
, &retry_prot
,
5548 pmap
= real_map
->pmap
;
5550 if (kr
!= KERN_SUCCESS
) {
5551 vm_map_unlock_read(map
);
5553 if (m
!= VM_PAGE_NULL
) {
5554 assert(VM_PAGE_OBJECT(m
) == m_object
);
5557 * retake the lock so that
5558 * we can drop the paging reference
5559 * in vm_fault_cleanup and do the
5560 * PAGE_WAKEUP_DONE in RELEASE_PAGE
5562 vm_object_lock(m_object
);
5566 vm_fault_cleanup(m_object
, top_page
);
5569 * retake the lock so that
5570 * we can drop the paging reference
5571 * in vm_fault_cleanup
5573 vm_object_lock(object
);
5575 vm_fault_cleanup(object
, top_page
);
5577 vm_object_deallocate(object
);
5581 vm_object_unlock(retry_object
);
5583 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
5584 vm_map_unlock_read(map
);
5585 if (real_map
!= map
) {
5586 vm_map_unlock(real_map
);
5589 if (m
!= VM_PAGE_NULL
) {
5590 assert(VM_PAGE_OBJECT(m
) == m_object
);
5593 * retake the lock so that
5594 * we can drop the paging reference
5595 * in vm_fault_cleanup and do the
5596 * PAGE_WAKEUP_DONE in RELEASE_PAGE
5598 vm_object_lock(m_object
);
5602 vm_fault_cleanup(m_object
, top_page
);
5605 * retake the lock so that
5606 * we can drop the paging reference
5607 * in vm_fault_cleanup
5609 vm_object_lock(object
);
5611 vm_fault_cleanup(object
, top_page
);
5613 vm_object_deallocate(object
);
5618 * Check whether the protection has changed or the object
5619 * has been copied while we left the map unlocked.
5621 if (pmap_has_prot_policy(pmap
, fault_info
.pmap_options
& PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE
, retry_prot
)) {
5622 /* If the pmap layer cares, pass the full set. */
5629 if (object_locks_dropped
== TRUE
) {
5630 if (m
!= VM_PAGE_NULL
) {
5631 vm_object_lock(m_object
);
5633 if (m_object
->copy
!= old_copy_object
) {
5635 * The copy object changed while the top-level object
5636 * was unlocked, so take away write permission.
5638 assert(!pmap_has_prot_policy(pmap
, fault_info
.pmap_options
& PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE
, prot
));
5639 prot
&= ~VM_PROT_WRITE
;
5642 vm_object_lock(object
);
5645 object_locks_dropped
= FALSE
;
5649 !fault_info
.no_copy_on_read
&&
5650 m
!= VM_PAGE_NULL
&&
5651 VM_PAGE_OBJECT(m
) != object
&&
5652 !VM_PAGE_OBJECT(m
)->pager_trusted
&&
5653 vm_protect_privileged_from_untrusted
&&
5654 !((prot
& VM_PROT_EXECUTE
) &&
5655 VM_PAGE_OBJECT(m
)->code_signed
&&
5656 pmap_get_vm_map_cs_enforced(caller_pmap
? caller_pmap
: pmap
)) &&
5657 current_proc_is_privileged()) {
5659 * We found the page we want in an "untrusted" VM object
5660 * down the shadow chain. Since the target is "privileged"
5661 * we want to perform a copy-on-read of that page, so that the
5662 * mapped object gets a stable copy and does not have to
5663 * rely on the "untrusted" object to provide the same
5664 * contents if the page gets reclaimed and has to be paged
5665 * in again later on.
5667 * Special case: if the mapping is executable and the untrusted
5668 * object is code-signed and the process is "cs_enforced", we
5669 * do not copy-on-read because that would break code-signing
5670 * enforcement expectations (an executable page must belong
5671 * to a code-signed object) and we can rely on code-signing
5672 * to re-validate the page if it gets evicted and paged back in.
5674 // printf("COPY-ON-READ %s:%d map %p vaddr 0x%llx obj %p offset 0x%llx found page %p (obj %p offset 0x%llx) UNTRUSTED -> need copy-on-read\n", __FUNCTION__, __LINE__, map, (uint64_t)vaddr, object, offset, m, VM_PAGE_OBJECT(m), m->vmp_offset);
5675 vm_copied_on_read
++;
5676 need_copy_on_read
= TRUE
;
5679 need_copy_on_read
= FALSE
;
5683 * If we want to wire down this page, but no longer have
5684 * adequate permissions, we must start all over.
5685 * If we decided to copy-on-read, we must also start all over.
5687 if ((wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) ||
5688 need_copy_on_read
) {
5689 vm_map_unlock_read(map
);
5690 if (real_map
!= map
) {
5691 vm_map_unlock(real_map
);
5694 if (m
!= VM_PAGE_NULL
) {
5695 assert(VM_PAGE_OBJECT(m
) == m_object
);
5699 vm_fault_cleanup(m_object
, top_page
);
5701 vm_fault_cleanup(object
, top_page
);
5704 vm_object_deallocate(object
);
5708 if (m
!= VM_PAGE_NULL
) {
5710 * Put this page into the physical map.
5711 * We had to do the unlock above because pmap_enter
5712 * may cause other faults. The page may be on
5713 * the pageout queues. If the pageout daemon comes
5714 * across the page, it will remove it from the queues.
5716 if (fault_page_size
< PAGE_SIZE
) {
5717 DEBUG4K_FAULT("map %p original %p pmap %p va 0x%llx pa 0x%llx(0x%llx+0x%llx) prot 0x%x caller_prot 0x%x\n", map
, original_map
, pmap
, (uint64_t)vaddr
, (uint64_t)((((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(m
)) << PAGE_SHIFT
) + fault_phys_offset
), (uint64_t)(((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(m
)) << PAGE_SHIFT
), (uint64_t)fault_phys_offset
, prot
, caller_prot
);
5718 assertf((!(fault_phys_offset
& FOURK_PAGE_MASK
) &&
5719 fault_phys_offset
< PAGE_SIZE
),
5720 "0x%llx\n", (uint64_t)fault_phys_offset
);
5722 assertf(fault_phys_offset
== 0,
5723 "0x%llx\n", (uint64_t)fault_phys_offset
);
5726 kr
= vm_fault_enter(m
,
5740 kr
= vm_fault_enter(m
,
5754 assert(VM_PAGE_OBJECT(m
) == m_object
);
5759 if (m_object
->internal
) {
5760 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
5761 } else if (m_object
->object_is_shared_cache
) {
5762 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
5764 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
5767 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, event_code
, trace_real_vaddr
, (fault_info
.user_tag
<< 16) | (caller_prot
<< 8) | vm_fault_type_for_tracing(need_copy_on_read
, type_of_fault
), m
->vmp_offset
, get_current_unique_pid(), 0);
5768 KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_SLOW
), get_current_unique_pid(), 0, 0, 0, 0);
5770 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
);
5772 if (kr
!= KERN_SUCCESS
) {
5773 /* abort this page fault */
5774 vm_map_unlock_read(map
);
5775 if (real_map
!= map
) {
5776 vm_map_unlock(real_map
);
5778 PAGE_WAKEUP_DONE(m
);
5779 vm_fault_cleanup(m_object
, top_page
);
5780 vm_object_deallocate(object
);
5783 if (physpage_p
!= NULL
) {
5784 /* for vm_map_wire_and_extract() */
5785 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
5786 if (prot
& VM_PROT_WRITE
) {
5787 vm_object_lock_assert_exclusive(m_object
);
5788 m
->vmp_dirty
= TRUE
;
5792 vm_map_entry_t entry
;
5793 vm_map_offset_t laddr
;
5794 vm_map_offset_t ldelta
, hdelta
;
5797 * do a pmap block mapping from the physical address
5801 if (real_map
!= map
) {
5802 vm_map_unlock(real_map
);
5805 if (original_map
!= map
) {
5806 vm_map_unlock_read(map
);
5807 vm_map_lock_read(original_map
);
5813 hdelta
= 0xFFFFF000;
5814 ldelta
= 0xFFFFF000;
5816 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
5817 if (ldelta
> (laddr
- entry
->vme_start
)) {
5818 ldelta
= laddr
- entry
->vme_start
;
5820 if (hdelta
> (entry
->vme_end
- laddr
)) {
5821 hdelta
= entry
->vme_end
- laddr
;
5823 if (entry
->is_sub_map
) {
5824 laddr
= ((laddr
- entry
->vme_start
)
5825 + VME_OFFSET(entry
));
5826 vm_map_lock_read(VME_SUBMAP(entry
));
5828 if (map
!= real_map
) {
5829 vm_map_unlock_read(map
);
5831 if (entry
->use_pmap
) {
5832 vm_map_unlock_read(real_map
);
5833 real_map
= VME_SUBMAP(entry
);
5835 map
= VME_SUBMAP(entry
);
5841 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
5842 (VME_OBJECT(entry
) != NULL
) &&
5843 (VME_OBJECT(entry
) == object
)) {
5846 if (!object
->pager_created
&&
5847 object
->phys_contiguous
&&
5848 VME_OFFSET(entry
) == 0 &&
5849 (entry
->vme_end
- entry
->vme_start
== object
->vo_size
) &&
5850 VM_MAP_PAGE_ALIGNED(entry
->vme_start
, (object
->vo_size
- 1))) {
5851 superpage
= VM_MEM_SUPERPAGE
;
5856 if (superpage
&& physpage_p
) {
5857 /* for vm_map_wire_and_extract() */
5858 *physpage_p
= (ppnum_t
)
5859 ((((vm_map_offset_t
)
5860 object
->vo_shadow_offset
)
5862 + (laddr
- entry
->vme_start
))
5868 * Set up a block mapped area
5870 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5871 kr
= pmap_map_block(caller_pmap
,
5872 (addr64_t
)(caller_pmap_addr
- ldelta
),
5873 (ppnum_t
)((((vm_map_offset_t
) (VME_OBJECT(entry
)->vo_shadow_offset
)) +
5874 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5875 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5876 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5878 if (kr
!= KERN_SUCCESS
) {
5883 * Set up a block mapped area
5885 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5886 kr
= pmap_map_block(real_map
->pmap
,
5887 (addr64_t
)(vaddr
- ldelta
),
5888 (ppnum_t
)((((vm_map_offset_t
)(VME_OBJECT(entry
)->vo_shadow_offset
)) +
5889 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5890 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5891 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5893 if (kr
!= KERN_SUCCESS
) {
5906 * TODO: could most of the done cases just use cleanup?
5910 * Unlock everything, and return
5912 vm_map_unlock_read(map
);
5913 if (real_map
!= map
) {
5914 vm_map_unlock(real_map
);
5917 if (m
!= VM_PAGE_NULL
) {
5918 assert(VM_PAGE_OBJECT(m
) == m_object
);
5920 if (!m_object
->internal
&& (fault_type
& VM_PROT_WRITE
)) {
5921 vm_object_paging_begin(m_object
);
5923 assert(written_on_object
== VM_OBJECT_NULL
);
5924 written_on_object
= m_object
;
5925 written_on_pager
= m_object
->pager
;
5926 written_on_offset
= m_object
->paging_offset
+ m
->vmp_offset
;
5928 PAGE_WAKEUP_DONE(m
);
5930 vm_fault_cleanup(m_object
, top_page
);
5932 vm_fault_cleanup(object
, top_page
);
5935 vm_object_deallocate(object
);
5940 thread_interrupt_level(interruptible_state
);
5942 if (resilient_media_object
!= VM_OBJECT_NULL
) {
5943 assert(resilient_media_retry
);
5944 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
5945 /* release extra reference on failed object */
5946 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
5947 vm_object_deallocate(resilient_media_object
);
5948 resilient_media_object
= VM_OBJECT_NULL
;
5949 resilient_media_offset
= (vm_object_offset_t
)-1;
5950 resilient_media_retry
= FALSE
;
5952 assert(!resilient_media_retry
);
5955 * Only I/O throttle on faults which cause a pagein/swapin.
5957 if ((type_of_fault
== DBG_PAGEIND_FAULT
) || (type_of_fault
== DBG_PAGEINV_FAULT
) || (type_of_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
)) {
5958 throttle_lowpri_io(1);
5960 if (kr
== KERN_SUCCESS
&& type_of_fault
!= DBG_CACHE_HIT_FAULT
&& type_of_fault
!= DBG_GUARD_FAULT
) {
5961 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
5962 if (vm_debug_events
) {
5963 if (type_of_fault
== DBG_COMPRESSOR_FAULT
) {
5964 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5965 } else if (type_of_fault
== DBG_COW_FAULT
) {
5966 VM_DEBUG_EVENT(vmf_cowdelay
, VMF_COWDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5968 VM_DEBUG_EVENT(vmf_zfdelay
, VMF_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5971 delay(throttle_delay
);
5976 if (written_on_object
) {
5977 vnode_pager_dirtied(written_on_pager
, written_on_offset
, written_on_offset
+ PAGE_SIZE_64
);
5979 vm_object_lock(written_on_object
);
5980 vm_object_paging_end(written_on_object
);
5981 vm_object_unlock(written_on_object
);
5983 written_on_object
= VM_OBJECT_NULL
;
5987 vm_record_rtfault(cthread
, fstart
, trace_vaddr
, type_of_fault
);
5990 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5991 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
5992 ((uint64_t)trace_vaddr
>> 32),
5995 vm_fault_type_for_tracing(need_copy_on_read
, type_of_fault
),
5998 if (fault_page_size
< PAGE_SIZE
&& kr
!= KERN_SUCCESS
) {
5999 DEBUG4K_FAULT("map %p original %p vaddr 0x%llx -> 0x%x\n", map
, original_map
, (uint64_t)trace_real_vaddr
, kr
);
6008 * Wire down a range of virtual addresses in a map.
6013 vm_map_entry_t entry
,
6017 vm_map_offset_t pmap_addr
,
6018 ppnum_t
*physpage_p
)
6021 vm_map_offset_t end_addr
= entry
->vme_end
;
6023 vm_map_size_t effective_page_size
;
6025 assert(entry
->in_transition
);
6027 if ((VME_OBJECT(entry
) != NULL
) &&
6028 !entry
->is_sub_map
&&
6029 VME_OBJECT(entry
)->phys_contiguous
) {
6030 return KERN_SUCCESS
;
6034 * Inform the physical mapping system that the
6035 * range of addresses may not fault, so that
6036 * page tables and such can be locked down as well.
6039 pmap_pageable(pmap
, pmap_addr
,
6040 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
6043 * We simulate a fault to get the page and enter it
6044 * in the physical map.
6047 effective_page_size
= MIN(VM_MAP_PAGE_SIZE(map
), PAGE_SIZE
);
6048 for (va
= entry
->vme_start
;
6050 va
+= effective_page_size
) {
6051 rc
= vm_fault_wire_fast(map
, va
, prot
, wire_tag
, entry
, pmap
,
6052 pmap_addr
+ (va
- entry
->vme_start
),
6054 if (rc
!= KERN_SUCCESS
) {
6055 rc
= vm_fault_internal(map
, va
, prot
, TRUE
, wire_tag
,
6056 ((pmap
== kernel_pmap
)
6058 : THREAD_ABORTSAFE
),
6061 (va
- entry
->vme_start
)),
6063 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
6066 if (rc
!= KERN_SUCCESS
) {
6067 struct vm_map_entry tmp_entry
= *entry
;
6069 /* unwire wired pages */
6070 tmp_entry
.vme_end
= va
;
6071 vm_fault_unwire(map
,
6072 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
6077 return KERN_SUCCESS
;
6083 * Unwire a range of virtual addresses in a map.
6088 vm_map_entry_t entry
,
6089 boolean_t deallocate
,
6091 vm_map_offset_t pmap_addr
)
6094 vm_map_offset_t end_addr
= entry
->vme_end
;
6096 struct vm_object_fault_info fault_info
= {};
6097 unsigned int unwired_pages
;
6098 vm_map_size_t effective_page_size
;
6100 object
= (entry
->is_sub_map
) ? VM_OBJECT_NULL
: VME_OBJECT(entry
);
6103 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
6104 * do anything since such memory is wired by default. So we don't have
6105 * anything to undo here.
6108 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
) {
6112 fault_info
.interruptible
= THREAD_UNINT
;
6113 fault_info
.behavior
= entry
->behavior
;
6114 fault_info
.user_tag
= VME_ALIAS(entry
);
6115 if (entry
->iokit_acct
||
6116 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
6117 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
6119 fault_info
.lo_offset
= VME_OFFSET(entry
);
6120 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + VME_OFFSET(entry
);
6121 fault_info
.no_cache
= entry
->no_cache
;
6122 fault_info
.stealth
= TRUE
;
6127 * Since the pages are wired down, we must be able to
6128 * get their mappings from the physical map system.
6131 effective_page_size
= MIN(VM_MAP_PAGE_SIZE(map
), PAGE_SIZE
);
6132 for (va
= entry
->vme_start
;
6134 va
+= effective_page_size
) {
6135 if (object
== VM_OBJECT_NULL
) {
6137 pmap_change_wiring(pmap
,
6138 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
6140 (void) vm_fault(map
, va
, VM_PROT_NONE
,
6141 TRUE
, VM_KERN_MEMORY_NONE
, THREAD_UNINT
, pmap
, pmap_addr
);
6144 vm_page_t result_page
;
6146 vm_object_t result_object
;
6147 vm_fault_return_t result
;
6149 /* cap cluster size at maximum UPL size */
6150 upl_size_t cluster_size
;
6151 if (os_sub_overflow(end_addr
, va
, &cluster_size
)) {
6152 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
6154 fault_info
.cluster_size
= cluster_size
;
6157 prot
= VM_PROT_NONE
;
6159 vm_object_lock(object
);
6160 vm_object_paging_begin(object
);
6161 result_page
= VM_PAGE_NULL
;
6162 result
= vm_fault_page(
6164 (VME_OFFSET(entry
) +
6165 (va
- entry
->vme_start
)),
6167 FALSE
, /* page not looked up */
6168 &prot
, &result_page
, &top_page
,
6170 NULL
, map
->no_zero_fill
,
6171 FALSE
, &fault_info
);
6172 } while (result
== VM_FAULT_RETRY
);
6175 * If this was a mapping to a file on a device that has been forcibly
6176 * unmounted, then we won't get a page back from vm_fault_page(). Just
6177 * move on to the next one in case the remaining pages are mapped from
6178 * different objects. During a forced unmount, the object is terminated
6179 * so the alive flag will be false if this happens. A forced unmount will
6180 * will occur when an external disk is unplugged before the user does an
6181 * eject, so we don't want to panic in that situation.
6184 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
) {
6188 if (result
== VM_FAULT_MEMORY_ERROR
&&
6189 object
== kernel_object
) {
6191 * This must have been allocated with
6192 * KMA_KOBJECT and KMA_VAONLY and there's
6193 * no physical page at this offset.
6194 * We're done (no page to free).
6200 if (result
!= VM_FAULT_SUCCESS
) {
6201 panic("vm_fault_unwire: failure");
6204 result_object
= VM_PAGE_OBJECT(result_page
);
6207 assert(VM_PAGE_GET_PHYS_PAGE(result_page
) !=
6208 vm_page_fictitious_addr
);
6209 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page
));
6210 if (VM_PAGE_WIRED(result_page
)) {
6213 VM_PAGE_FREE(result_page
);
6215 if ((pmap
) && (VM_PAGE_GET_PHYS_PAGE(result_page
) != vm_page_guard_addr
)) {
6216 pmap_change_wiring(pmap
,
6217 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
6221 if (VM_PAGE_WIRED(result_page
)) {
6222 vm_page_lockspin_queues();
6223 vm_page_unwire(result_page
, TRUE
);
6224 vm_page_unlock_queues();
6227 if (entry
->zero_wired_pages
) {
6228 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page
));
6229 entry
->zero_wired_pages
= FALSE
;
6232 PAGE_WAKEUP_DONE(result_page
);
6234 vm_fault_cleanup(result_object
, top_page
);
6239 * Inform the physical mapping system that the range
6240 * of addresses may fault, so that page tables and
6241 * such may be unwired themselves.
6244 pmap_pageable(pmap
, pmap_addr
,
6245 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
6247 if (kernel_object
== object
) {
6249 * Would like to make user_tag in vm_object_fault_info
6250 * vm_tag_t (unsigned short) but user_tag derives its value from
6251 * VME_ALIAS(entry) at a few places and VME_ALIAS, in turn, casts
6252 * to an _unsigned int_ which is used by non-fault_info paths throughout the
6253 * code at many places.
6255 * So, for now, an explicit truncation to unsigned short (vm_tag_t).
6257 assertf((fault_info
.user_tag
& VME_ALIAS_MASK
) == fault_info
.user_tag
,
6258 "VM Tag truncated from 0x%x to 0x%x\n", fault_info
.user_tag
, (fault_info
.user_tag
& VME_ALIAS_MASK
));
6259 vm_tag_update_size((vm_tag_t
) fault_info
.user_tag
, -ptoa_64(unwired_pages
));
6264 * vm_fault_wire_fast:
6266 * Handle common case of a wire down page fault at the given address.
6267 * If successful, the page is inserted into the associated physical map.
6268 * The map entry is passed in to avoid the overhead of a map lookup.
6270 * NOTE: the given address should be truncated to the
6271 * proper page address.
6273 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
6274 * a standard error specifying why the fault is fatal is returned.
6276 * The map in question must be referenced, and remains so.
6277 * Caller has a read lock on the map.
6279 * This is a stripped version of vm_fault() for wiring pages. Anything
6280 * other than the common case will return KERN_FAILURE, and the caller
6281 * is expected to call vm_fault().
6283 static kern_return_t
6285 __unused vm_map_t map
,
6287 __unused vm_prot_t caller_prot
,
6289 vm_map_entry_t entry
,
6291 vm_map_offset_t pmap_addr
,
6292 ppnum_t
*physpage_p
)
6295 vm_object_offset_t offset
;
6298 thread_t thread
= current_thread();
6301 vm_map_size_t fault_page_size
;
6302 vm_map_offset_t fault_phys_offset
;
6303 struct vm_object_fault_info fault_info
= {};
6305 VM_STAT_INCR(faults
);
6307 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
) {
6308 thread
->task
->faults
++;
6316 #define RELEASE_PAGE(m) { \
6317 PAGE_WAKEUP_DONE(m); \
6318 vm_page_lockspin_queues(); \
6319 vm_page_unwire(m, TRUE); \
6320 vm_page_unlock_queues(); \
6324 #undef UNLOCK_THINGS
6325 #define UNLOCK_THINGS { \
6326 vm_object_paging_end(object); \
6327 vm_object_unlock(object); \
6330 #undef UNLOCK_AND_DEALLOCATE
6331 #define UNLOCK_AND_DEALLOCATE { \
6333 vm_object_deallocate(object); \
6336 * Give up and have caller do things the hard way.
6340 UNLOCK_AND_DEALLOCATE; \
6341 return(KERN_FAILURE); \
6346 * If this entry is not directly to a vm_object, bail out.
6348 if (entry
->is_sub_map
) {
6349 assert(physpage_p
== NULL
);
6350 return KERN_FAILURE
;
6354 * Find the backing store object and offset into it.
6357 object
= VME_OBJECT(entry
);
6358 offset
= (va
- entry
->vme_start
) + VME_OFFSET(entry
);
6359 prot
= entry
->protection
;
6362 * Make a reference to this object to prevent its
6363 * disposal while we are messing with it.
6366 vm_object_lock(object
);
6367 vm_object_reference_locked(object
);
6368 vm_object_paging_begin(object
);
6371 * INVARIANTS (through entire routine):
6373 * 1) At all times, we must either have the object
6374 * lock or a busy page in some object to prevent
6375 * some other thread from trying to bring in
6378 * 2) Once we have a busy page, we must remove it from
6379 * the pageout queues, so that the pageout daemon
6380 * will not grab it away.
6385 * Look for page in top-level object. If it's not there or
6386 * there's something going on, give up.
6388 m
= vm_page_lookup(object
, vm_object_trunc_page(offset
));
6389 if ((m
== VM_PAGE_NULL
) || (m
->vmp_busy
) ||
6390 (m
->vmp_unusual
&& (m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_absent
))) {
6393 if (m
->vmp_fictitious
&&
6394 VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
6396 * Guard pages are fictitious pages and are never
6397 * entered into a pmap, so let's say it's been wired...
6404 * Wire the page down now. All bail outs beyond this
6405 * point must unwire the page.
6408 vm_page_lockspin_queues();
6409 vm_page_wire(m
, wire_tag
, TRUE
);
6410 vm_page_unlock_queues();
6413 * Mark page busy for other threads.
6415 assert(!m
->vmp_busy
);
6417 assert(!m
->vmp_absent
);
6420 * Give up if the page is being written and there's a copy object
6422 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
6427 fault_info
.user_tag
= VME_ALIAS(entry
);
6428 fault_info
.pmap_options
= 0;
6429 if (entry
->iokit_acct
||
6430 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
6431 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
6434 fault_page_size
= MIN(VM_MAP_PAGE_SIZE(map
), PAGE_SIZE
);
6435 fault_phys_offset
= offset
- vm_object_trunc_page(offset
);
6438 * Put this page into the physical map.
6440 type_of_fault
= DBG_CACHE_HIT_FAULT
;
6441 kr
= vm_fault_enter(m
,
6449 FALSE
, /* change_wiring */
6454 if (kr
!= KERN_SUCCESS
) {
6461 * Unlock everything, and return
6465 /* for vm_map_wire_and_extract() */
6466 if (kr
== KERN_SUCCESS
) {
6467 assert(object
== VM_PAGE_OBJECT(m
));
6468 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
6469 if (prot
& VM_PROT_WRITE
) {
6470 vm_object_lock_assert_exclusive(object
);
6471 m
->vmp_dirty
= TRUE
;
6478 PAGE_WAKEUP_DONE(m
);
6479 UNLOCK_AND_DEALLOCATE
;
6485 * Routine: vm_fault_copy_cleanup
6487 * Release a page used by vm_fault_copy.
6491 vm_fault_copy_cleanup(
6495 vm_object_t object
= VM_PAGE_OBJECT(page
);
6497 vm_object_lock(object
);
6498 PAGE_WAKEUP_DONE(page
);
6499 if (!VM_PAGE_PAGEABLE(page
)) {
6500 vm_page_lockspin_queues();
6501 if (!VM_PAGE_PAGEABLE(page
)) {
6502 vm_page_activate(page
);
6504 vm_page_unlock_queues();
6506 vm_fault_cleanup(object
, top_page
);
6510 vm_fault_copy_dst_cleanup(
6515 if (page
!= VM_PAGE_NULL
) {
6516 object
= VM_PAGE_OBJECT(page
);
6517 vm_object_lock(object
);
6518 vm_page_lockspin_queues();
6519 vm_page_unwire(page
, TRUE
);
6520 vm_page_unlock_queues();
6521 vm_object_paging_end(object
);
6522 vm_object_unlock(object
);
6527 * Routine: vm_fault_copy
6530 * Copy pages from one virtual memory object to another --
6531 * neither the source nor destination pages need be resident.
6533 * Before actually copying a page, the version associated with
6534 * the destination address map wil be verified.
6536 * In/out conditions:
6537 * The caller must hold a reference, but not a lock, to
6538 * each of the source and destination objects and to the
6542 * Returns KERN_SUCCESS if no errors were encountered in
6543 * reading or writing the data. Returns KERN_INTERRUPTED if
6544 * the operation was interrupted (only possible if the
6545 * "interruptible" argument is asserted). Other return values
6546 * indicate a permanent error in copying the data.
6548 * The actual amount of data copied will be returned in the
6549 * "copy_size" argument. In the event that the destination map
6550 * verification failed, this amount may be less than the amount
6555 vm_object_t src_object
,
6556 vm_object_offset_t src_offset
,
6557 vm_map_size_t
*copy_size
, /* INOUT */
6558 vm_object_t dst_object
,
6559 vm_object_offset_t dst_offset
,
6561 vm_map_version_t
*dst_version
,
6564 vm_page_t result_page
;
6567 vm_page_t src_top_page
;
6571 vm_page_t dst_top_page
;
6574 vm_map_size_t amount_left
;
6575 vm_object_t old_copy_object
;
6576 vm_object_t result_page_object
= NULL
;
6577 kern_return_t error
= 0;
6578 vm_fault_return_t result
;
6580 vm_map_size_t part_size
;
6581 struct vm_object_fault_info fault_info_src
= {};
6582 struct vm_object_fault_info fault_info_dst
= {};
6585 * In order not to confuse the clustered pageins, align
6586 * the different offsets on a page boundary.
6591 *copy_size -= amount_left; \
6595 amount_left
= *copy_size
;
6597 fault_info_src
.interruptible
= interruptible
;
6598 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6599 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
6600 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
6601 fault_info_src
.stealth
= TRUE
;
6603 fault_info_dst
.interruptible
= interruptible
;
6604 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6605 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
6606 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
6607 fault_info_dst
.stealth
= TRUE
;
6609 do { /* while (amount_left > 0) */
6611 * There may be a deadlock if both source and destination
6612 * pages are the same. To avoid this deadlock, the copy must
6613 * start by getting the destination page in order to apply
6614 * COW semantics if any.
6617 RetryDestinationFault
:;
6619 dst_prot
= VM_PROT_WRITE
| VM_PROT_READ
;
6621 vm_object_lock(dst_object
);
6622 vm_object_paging_begin(dst_object
);
6624 /* cap cluster size at maximum UPL size */
6625 upl_size_t cluster_size
;
6626 if (os_convert_overflow(amount_left
, &cluster_size
)) {
6627 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
6629 fault_info_dst
.cluster_size
= cluster_size
;
6631 dst_page
= VM_PAGE_NULL
;
6632 result
= vm_fault_page(dst_object
,
6633 vm_object_trunc_page(dst_offset
),
6634 VM_PROT_WRITE
| VM_PROT_READ
,
6636 FALSE
, /* page not looked up */
6637 &dst_prot
, &dst_page
, &dst_top_page
,
6640 dst_map
->no_zero_fill
,
6641 FALSE
, &fault_info_dst
);
6643 case VM_FAULT_SUCCESS
:
6645 case VM_FAULT_RETRY
:
6646 goto RetryDestinationFault
;
6647 case VM_FAULT_MEMORY_SHORTAGE
:
6648 if (vm_page_wait(interruptible
)) {
6649 goto RetryDestinationFault
;
6652 case VM_FAULT_INTERRUPTED
:
6653 RETURN(MACH_SEND_INTERRUPTED
);
6654 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
6655 /* success but no VM page: fail the copy */
6656 vm_object_paging_end(dst_object
);
6657 vm_object_unlock(dst_object
);
6659 case VM_FAULT_MEMORY_ERROR
:
6663 return KERN_MEMORY_ERROR
;
6666 panic("vm_fault_copy: unexpected error 0x%x from "
6667 "vm_fault_page()\n", result
);
6669 assert((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
6671 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
6672 old_copy_object
= dst_object
->copy
;
6675 * There exists the possiblity that the source and
6676 * destination page are the same. But we can't
6677 * easily determine that now. If they are the
6678 * same, the call to vm_fault_page() for the
6679 * destination page will deadlock. To prevent this we
6680 * wire the page so we can drop busy without having
6681 * the page daemon steal the page. We clean up the
6682 * top page but keep the paging reference on the object
6683 * holding the dest page so it doesn't go away.
6686 vm_page_lockspin_queues();
6687 vm_page_wire(dst_page
, VM_KERN_MEMORY_OSFMK
, TRUE
);
6688 vm_page_unlock_queues();
6689 PAGE_WAKEUP_DONE(dst_page
);
6690 vm_object_unlock(dst_object
);
6692 if (dst_top_page
!= VM_PAGE_NULL
) {
6693 vm_object_lock(dst_object
);
6694 VM_PAGE_FREE(dst_top_page
);
6695 vm_object_paging_end(dst_object
);
6696 vm_object_unlock(dst_object
);
6701 if (src_object
== VM_OBJECT_NULL
) {
6703 * No source object. We will just
6704 * zero-fill the page in dst_object.
6706 src_page
= VM_PAGE_NULL
;
6707 result_page
= VM_PAGE_NULL
;
6709 vm_object_lock(src_object
);
6710 src_page
= vm_page_lookup(src_object
,
6711 vm_object_trunc_page(src_offset
));
6712 if (src_page
== dst_page
) {
6713 src_prot
= dst_prot
;
6714 result_page
= VM_PAGE_NULL
;
6716 src_prot
= VM_PROT_READ
;
6717 vm_object_paging_begin(src_object
);
6719 /* cap cluster size at maximum UPL size */
6720 if (os_convert_overflow(amount_left
, &cluster_size
)) {
6721 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
6723 fault_info_src
.cluster_size
= cluster_size
;
6725 result_page
= VM_PAGE_NULL
;
6726 result
= vm_fault_page(
6728 vm_object_trunc_page(src_offset
),
6729 VM_PROT_READ
, FALSE
,
6730 FALSE
, /* page not looked up */
6732 &result_page
, &src_top_page
,
6733 (int *)0, &error
, FALSE
,
6734 FALSE
, &fault_info_src
);
6737 case VM_FAULT_SUCCESS
:
6739 case VM_FAULT_RETRY
:
6740 goto RetrySourceFault
;
6741 case VM_FAULT_MEMORY_SHORTAGE
:
6742 if (vm_page_wait(interruptible
)) {
6743 goto RetrySourceFault
;
6746 case VM_FAULT_INTERRUPTED
:
6747 vm_fault_copy_dst_cleanup(dst_page
);
6748 RETURN(MACH_SEND_INTERRUPTED
);
6749 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
6750 /* success but no VM page: fail */
6751 vm_object_paging_end(src_object
);
6752 vm_object_unlock(src_object
);
6754 case VM_FAULT_MEMORY_ERROR
:
6755 vm_fault_copy_dst_cleanup(dst_page
);
6759 return KERN_MEMORY_ERROR
;
6762 panic("vm_fault_copy(2): unexpected "
6764 "vm_fault_page()\n", result
);
6767 result_page_object
= VM_PAGE_OBJECT(result_page
);
6768 assert((src_top_page
== VM_PAGE_NULL
) ==
6769 (result_page_object
== src_object
));
6771 assert((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
6772 vm_object_unlock(result_page_object
);
6775 vm_map_lock_read(dst_map
);
6777 if (!vm_map_verify(dst_map
, dst_version
)) {
6778 vm_map_unlock_read(dst_map
);
6779 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6780 vm_fault_copy_cleanup(result_page
, src_top_page
);
6782 vm_fault_copy_dst_cleanup(dst_page
);
6785 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
6787 vm_object_lock(dst_object
);
6789 if (dst_object
->copy
!= old_copy_object
) {
6790 vm_object_unlock(dst_object
);
6791 vm_map_unlock_read(dst_map
);
6792 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6793 vm_fault_copy_cleanup(result_page
, src_top_page
);
6795 vm_fault_copy_dst_cleanup(dst_page
);
6798 vm_object_unlock(dst_object
);
6801 * Copy the page, and note that it is dirty
6805 if (!page_aligned(src_offset
) ||
6806 !page_aligned(dst_offset
) ||
6807 !page_aligned(amount_left
)) {
6808 vm_object_offset_t src_po
,
6811 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
6812 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
6814 if (dst_po
> src_po
) {
6815 part_size
= PAGE_SIZE
- dst_po
;
6817 part_size
= PAGE_SIZE
- src_po
;
6819 if (part_size
> (amount_left
)) {
6820 part_size
= amount_left
;
6823 if (result_page
== VM_PAGE_NULL
) {
6824 assert((vm_offset_t
) dst_po
== dst_po
);
6825 assert((vm_size_t
) part_size
== part_size
);
6826 vm_page_part_zero_fill(dst_page
,
6827 (vm_offset_t
) dst_po
,
6828 (vm_size_t
) part_size
);
6830 assert((vm_offset_t
) src_po
== src_po
);
6831 assert((vm_offset_t
) dst_po
== dst_po
);
6832 assert((vm_size_t
) part_size
== part_size
);
6833 vm_page_part_copy(result_page
,
6834 (vm_offset_t
) src_po
,
6836 (vm_offset_t
) dst_po
,
6837 (vm_size_t
)part_size
);
6838 if (!dst_page
->vmp_dirty
) {
6839 vm_object_lock(dst_object
);
6840 SET_PAGE_DIRTY(dst_page
, TRUE
);
6841 vm_object_unlock(dst_object
);
6845 part_size
= PAGE_SIZE
;
6847 if (result_page
== VM_PAGE_NULL
) {
6848 vm_page_zero_fill(dst_page
);
6850 vm_object_lock(result_page_object
);
6851 vm_page_copy(result_page
, dst_page
);
6852 vm_object_unlock(result_page_object
);
6854 if (!dst_page
->vmp_dirty
) {
6855 vm_object_lock(dst_object
);
6856 SET_PAGE_DIRTY(dst_page
, TRUE
);
6857 vm_object_unlock(dst_object
);
6863 * Unlock everything, and return
6866 vm_map_unlock_read(dst_map
);
6868 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6869 vm_fault_copy_cleanup(result_page
, src_top_page
);
6871 vm_fault_copy_dst_cleanup(dst_page
);
6873 amount_left
-= part_size
;
6874 src_offset
+= part_size
;
6875 dst_offset
+= part_size
;
6876 } while (amount_left
> 0);
6878 RETURN(KERN_SUCCESS
);
6884 #if VM_FAULT_CLASSIFY
6886 * Temporary statistics gathering support.
6890 * Statistics arrays:
6892 #define VM_FAULT_TYPES_MAX 5
6893 #define VM_FAULT_LEVEL_MAX 8
6895 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
6897 #define VM_FAULT_TYPE_ZERO_FILL 0
6898 #define VM_FAULT_TYPE_MAP_IN 1
6899 #define VM_FAULT_TYPE_PAGER 2
6900 #define VM_FAULT_TYPE_COPY 3
6901 #define VM_FAULT_TYPE_OTHER 4
6905 vm_fault_classify(vm_object_t object
,
6906 vm_object_offset_t offset
,
6907 vm_prot_t fault_type
)
6909 int type
, level
= 0;
6913 m
= vm_page_lookup(object
, offset
);
6914 if (m
!= VM_PAGE_NULL
) {
6915 if (m
->vmp_busy
|| m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_absent
) {
6916 type
= VM_FAULT_TYPE_OTHER
;
6919 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
6920 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
6921 type
= VM_FAULT_TYPE_MAP_IN
;
6924 type
= VM_FAULT_TYPE_COPY
;
6927 if (object
->pager_created
) {
6928 type
= VM_FAULT_TYPE_PAGER
;
6931 if (object
->shadow
== VM_OBJECT_NULL
) {
6932 type
= VM_FAULT_TYPE_ZERO_FILL
;
6936 offset
+= object
->vo_shadow_offset
;
6937 object
= object
->shadow
;
6943 if (level
> VM_FAULT_LEVEL_MAX
) {
6944 level
= VM_FAULT_LEVEL_MAX
;
6947 vm_fault_stats
[type
][level
] += 1;
6952 /* cleanup routine to call from debugger */
6955 vm_fault_classify_init(void)
6959 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
6960 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
6961 vm_fault_stats
[type
][level
] = 0;
6967 #endif /* VM_FAULT_CLASSIFY */
6970 kdp_lightweight_fault(vm_map_t map
, vm_offset_t cur_target_addr
)
6972 vm_map_entry_t entry
;
6974 vm_offset_t object_offset
;
6976 int compressor_external_state
, compressed_count_delta
;
6977 int compressor_flags
= (C_DONT_BLOCK
| C_KEEP
| C_KDP
);
6978 int my_fault_type
= VM_PROT_READ
;
6980 int effective_page_mask
, effective_page_size
;
6982 if (VM_MAP_PAGE_SHIFT(map
) < PAGE_SHIFT
) {
6983 effective_page_mask
= VM_MAP_PAGE_MASK(map
);
6984 effective_page_size
= VM_MAP_PAGE_SIZE(map
);
6986 effective_page_mask
= PAGE_MASK
;
6987 effective_page_size
= PAGE_SIZE
;
6991 panic("kdp_lightweight_fault called from outside of debugger context");
6994 assert(map
!= VM_MAP_NULL
);
6996 assert((cur_target_addr
& effective_page_mask
) == 0);
6997 if ((cur_target_addr
& effective_page_mask
) != 0) {
7001 if (kdp_lck_rw_lock_is_acquired_exclusive(&map
->lock
)) {
7005 if (!vm_map_lookup_entry(map
, cur_target_addr
, &entry
)) {
7009 if (entry
->is_sub_map
) {
7013 object
= VME_OBJECT(entry
);
7014 if (object
== VM_OBJECT_NULL
) {
7018 object_offset
= cur_target_addr
- entry
->vme_start
+ VME_OFFSET(entry
);
7021 if (kdp_lck_rw_lock_is_acquired_exclusive(&object
->Lock
)) {
7025 if (object
->pager_created
&& (object
->paging_in_progress
||
7026 object
->activity_in_progress
)) {
7030 m
= kdp_vm_page_lookup(object
, vm_object_trunc_page(object_offset
));
7032 if (m
!= VM_PAGE_NULL
) {
7033 if ((object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_DEFAULT
) {
7037 if (m
->vmp_laundry
|| m
->vmp_busy
|| m
->vmp_free_when_done
|| m
->vmp_absent
|| m
->vmp_error
|| m
->vmp_cleaning
||
7038 m
->vmp_overwriting
|| m
->vmp_restart
|| m
->vmp_unusual
) {
7042 assert(!m
->vmp_private
);
7043 if (m
->vmp_private
) {
7047 assert(!m
->vmp_fictitious
);
7048 if (m
->vmp_fictitious
) {
7052 assert(m
->vmp_q_state
!= VM_PAGE_USED_BY_COMPRESSOR
);
7053 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
7057 return ptoa(VM_PAGE_GET_PHYS_PAGE(m
));
7060 compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
7062 if (object
->pager_created
&& MUST_ASK_PAGER(object
, object_offset
, compressor_external_state
)) {
7063 if (compressor_external_state
== VM_EXTERNAL_STATE_EXISTS
) {
7064 kr
= vm_compressor_pager_get(object
->pager
,
7065 vm_object_trunc_page(object_offset
+ object
->paging_offset
),
7066 kdp_compressor_decompressed_page_ppnum
, &my_fault_type
,
7067 compressor_flags
, &compressed_count_delta
);
7068 if (kr
== KERN_SUCCESS
) {
7069 return kdp_compressor_decompressed_page_paddr
;
7076 if (object
->shadow
== VM_OBJECT_NULL
) {
7080 object_offset
+= object
->vo_shadow_offset
;
7081 object
= object
->shadow
;
7086 * vm_page_validate_cs_fast():
7087 * Performs a few quick checks to determine if the page's code signature
7088 * really needs to be fully validated. It could:
7089 * 1. have been modified (i.e. automatically tainted),
7090 * 2. have already been validated,
7091 * 3. have already been found to be tainted,
7092 * 4. no longer have a backing store.
7093 * Returns FALSE if the page needs to be fully validated.
7096 vm_page_validate_cs_fast(
7098 vm_map_size_t fault_page_size
,
7099 vm_map_offset_t fault_phys_offset
)
7103 object
= VM_PAGE_OBJECT(page
);
7104 vm_object_lock_assert_held(object
);
7106 if (page
->vmp_wpmapped
&&
7107 !VMP_CS_TAINTED(page
, fault_page_size
, fault_phys_offset
)) {
7109 * This page was mapped for "write" access sometime in the
7110 * past and could still be modifiable in the future.
7111 * Consider it tainted.
7112 * [ If the page was already found to be "tainted", no
7113 * need to re-validate. ]
7115 vm_object_lock_assert_exclusive(object
);
7116 VMP_CS_SET_VALIDATED(page
, fault_page_size
, fault_phys_offset
, TRUE
);
7117 VMP_CS_SET_TAINTED(page
, fault_page_size
, fault_phys_offset
, TRUE
);
7119 printf("CODESIGNING: %s: "
7120 "page %p obj %p off 0x%llx "
7123 page
, object
, page
->vmp_offset
);
7125 vm_cs_validated_dirtied
++;
7128 if (VMP_CS_VALIDATED(page
, fault_page_size
, fault_phys_offset
) ||
7129 VMP_CS_TAINTED(page
, fault_page_size
, fault_phys_offset
)) {
7132 vm_object_lock_assert_exclusive(object
);
7134 #if CHECK_CS_VALIDATION_BITMAP
7137 kr
= vnode_pager_cs_check_validation_bitmap(
7139 page
->vmp_offset
+ object
->paging_offset
,
7141 if (kr
== KERN_SUCCESS
) {
7142 page
->vmp_cs_validated
= VMP_CS_ALL_TRUE
;
7143 page
->vmp_cs_tainted
= VMP_CS_ALL_FALSE
;
7144 vm_cs_bitmap_validated
++;
7147 #endif /* CHECK_CS_VALIDATION_BITMAP */
7149 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
7151 * The object is terminating and we don't have its pager
7152 * so we can't validate the data...
7157 /* we need to really validate this page */
7158 vm_object_lock_assert_exclusive(object
);
7163 vm_page_validate_cs_mapped_slow(
7168 memory_object_offset_t mo_offset
;
7169 memory_object_t pager
;
7170 struct vnode
*vnode
;
7171 int validated
, tainted
, nx
;
7173 assert(page
->vmp_busy
);
7174 object
= VM_PAGE_OBJECT(page
);
7175 vm_object_lock_assert_exclusive(object
);
7180 * Since we get here to validate a page that was brought in by
7181 * the pager, we know that this pager is all setup and ready
7184 assert(object
->code_signed
);
7185 assert(!object
->internal
);
7186 assert(object
->pager
!= NULL
);
7187 assert(object
->pager_ready
);
7189 pager
= object
->pager
;
7190 assert(object
->paging_in_progress
);
7191 vnode
= vnode_pager_lookup_vnode(pager
);
7192 mo_offset
= page
->vmp_offset
+ object
->paging_offset
;
7194 /* verify the SHA1 hash for this page */
7198 cs_validate_page(vnode
,
7201 (const void *)((const char *)kaddr
),
7206 page
->vmp_cs_validated
|= validated
;
7207 page
->vmp_cs_tainted
|= tainted
;
7208 page
->vmp_cs_nx
|= nx
;
7210 #if CHECK_CS_VALIDATION_BITMAP
7211 if (page
->vmp_cs_validated
== VMP_CS_ALL_TRUE
&&
7212 page
->vmp_cs_tainted
== VMP_CS_ALL_FALSE
) {
7213 vnode_pager_cs_check_validation_bitmap(object
->pager
,
7217 #endif /* CHECK_CS_VALIDATION_BITMAP */
7221 vm_page_validate_cs_mapped(
7223 vm_map_size_t fault_page_size
,
7224 vm_map_offset_t fault_phys_offset
,
7227 if (!vm_page_validate_cs_fast(page
, fault_page_size
, fault_phys_offset
)) {
7228 vm_page_validate_cs_mapped_slow(page
, kaddr
);
7233 vm_page_validate_cs(
7235 vm_map_size_t fault_page_size
,
7236 vm_map_offset_t fault_phys_offset
)
7239 vm_object_offset_t offset
;
7240 vm_map_offset_t koffset
;
7241 vm_map_size_t ksize
;
7244 boolean_t busy_page
;
7245 boolean_t need_unmap
;
7247 object
= VM_PAGE_OBJECT(page
);
7248 vm_object_lock_assert_held(object
);
7250 if (vm_page_validate_cs_fast(page
, fault_page_size
, fault_phys_offset
)) {
7253 vm_object_lock_assert_exclusive(object
);
7255 assert(object
->code_signed
);
7256 offset
= page
->vmp_offset
;
7258 busy_page
= page
->vmp_busy
;
7260 /* keep page busy while we map (and unlock) the VM object */
7261 page
->vmp_busy
= TRUE
;
7265 * Take a paging reference on the VM object
7266 * to protect it from collapse or bypass,
7267 * and keep it from disappearing too.
7269 vm_object_paging_begin(object
);
7271 /* map the page in the kernel address space */
7272 ksize
= PAGE_SIZE_64
;
7275 kr
= vm_paging_map_object(page
,
7279 FALSE
, /* can't unlock object ! */
7283 if (kr
!= KERN_SUCCESS
) {
7284 panic("%s: could not map page: 0x%x\n", __FUNCTION__
, kr
);
7286 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
7288 /* validate the mapped page */
7289 vm_page_validate_cs_mapped_slow(page
, (const void *) kaddr
);
7291 assert(page
->vmp_busy
);
7292 assert(object
== VM_PAGE_OBJECT(page
));
7293 vm_object_lock_assert_exclusive(object
);
7296 PAGE_WAKEUP_DONE(page
);
7299 /* unmap the map from the kernel address space */
7300 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
7305 vm_object_paging_end(object
);
7309 vm_page_validate_cs_mapped_chunk(
7312 vm_offset_t chunk_offset
,
7313 vm_size_t chunk_size
,
7314 boolean_t
*validated_p
,
7315 unsigned *tainted_p
)
7318 vm_object_offset_t offset
, offset_in_page
;
7319 memory_object_t pager
;
7320 struct vnode
*vnode
;
7321 boolean_t validated
;
7324 *validated_p
= FALSE
;
7327 assert(page
->vmp_busy
);
7328 object
= VM_PAGE_OBJECT(page
);
7329 vm_object_lock_assert_exclusive(object
);
7331 assert(object
->code_signed
);
7332 offset
= page
->vmp_offset
;
7334 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
7336 * The object is terminating and we don't have its pager
7337 * so we can't validate the data...
7342 * Since we get here to validate a page that was brought in by
7343 * the pager, we know that this pager is all setup and ready
7346 assert(!object
->internal
);
7347 assert(object
->pager
!= NULL
);
7348 assert(object
->pager_ready
);
7350 pager
= object
->pager
;
7351 assert(object
->paging_in_progress
);
7352 vnode
= vnode_pager_lookup_vnode(pager
);
7354 /* verify the signature for this chunk */
7355 offset_in_page
= chunk_offset
;
7356 assert(offset_in_page
< PAGE_SIZE
);
7359 validated
= cs_validate_range(vnode
,
7361 (object
->paging_offset
+
7364 (const void *)((const char *)kaddr
7369 *validated_p
= TRUE
;
7372 *tainted_p
= tainted
;
7377 vm_rtfrecord_lock(void)
7379 lck_spin_lock(&vm_rtfr_slock
);
7383 vm_rtfrecord_unlock(void)
7385 lck_spin_unlock(&vm_rtfr_slock
);
7389 vmrtfaultinfo_bufsz(void)
7391 return vmrtf_num_records
* sizeof(vm_rtfault_record_t
);
7394 #include <kern/backtrace.h>
7396 __attribute__((noinline
))
7398 vm_record_rtfault(thread_t cthread
, uint64_t fstart
, vm_map_offset_t fault_vaddr
, int type_of_fault
)
7400 uint64_t fend
= mach_continuous_time();
7403 uint64_t ctid
= cthread
->thread_id
;
7404 uint64_t cupid
= get_current_unique_pid();
7410 /* Capture a single-frame backtrace; this extracts just the program
7411 * counter at the point of the fault into "bpc", and should perform no
7412 * further user stack traversals, thus avoiding copyin()s and further
7415 unsigned int bfrs
= backtrace_thread_user(cthread
, &bpc
, 1U, &btr
, &u64
, NULL
, false);
7417 if ((btr
== 0) && (bfrs
> 0)) {
7421 assert((fstart
!= 0) && fend
>= fstart
);
7422 vm_rtfrecord_lock();
7423 assert(vmrtfrs
.vmrtfr_curi
<= vmrtfrs
.vmrtfr_maxi
);
7425 vmrtfrs
.vmrtf_total
++;
7426 vm_rtfault_record_t
*cvmr
= &vmrtfrs
.vm_rtf_records
[vmrtfrs
.vmrtfr_curi
++];
7428 cvmr
->rtfabstime
= fstart
;
7429 cvmr
->rtfduration
= fend
- fstart
;
7430 cvmr
->rtfaddr
= fault_vaddr
;
7432 cvmr
->rtftype
= type_of_fault
;
7433 cvmr
->rtfupid
= cupid
;
7434 cvmr
->rtftid
= ctid
;
7436 if (vmrtfrs
.vmrtfr_curi
> vmrtfrs
.vmrtfr_maxi
) {
7437 vmrtfrs
.vmrtfr_curi
= 0;
7440 vm_rtfrecord_unlock();
7444 vmrtf_extract(uint64_t cupid
, __unused boolean_t isroot
, unsigned long vrecordsz
, void *vrecords
, unsigned long *vmrtfrv
)
7446 vm_rtfault_record_t
*cvmrd
= vrecords
;
7447 size_t residue
= vrecordsz
;
7448 size_t numextracted
= 0;
7449 boolean_t early_exit
= FALSE
;
7451 vm_rtfrecord_lock();
7453 for (int vmfi
= 0; vmfi
<= vmrtfrs
.vmrtfr_maxi
; vmfi
++) {
7454 if (residue
< sizeof(vm_rtfault_record_t
)) {
7459 if (vmrtfrs
.vm_rtf_records
[vmfi
].rtfupid
!= cupid
) {
7460 #if DEVELOPMENT || DEBUG
7461 if (isroot
== FALSE
) {
7466 #endif /* DEVDEBUG */
7469 *cvmrd
= vmrtfrs
.vm_rtf_records
[vmfi
];
7471 residue
-= sizeof(vm_rtfault_record_t
);
7475 vm_rtfrecord_unlock();
7477 *vmrtfrv
= numextracted
;